-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ11399.java
More file actions
75 lines (60 loc) · 1.74 KB
/
Copy pathJ11399.java
File metadata and controls
75 lines (60 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package TIL;
import java.io.*;
import java.util.*;
public class J11399 {
static int[] result;
static int max;
public static void main(String[] args) throws IOException {
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
int num = Integer.parseInt(buffer.readLine());
int[] arr = new int[num];
result = new int[num];
String[] input = buffer.readLine().split(" ");
for(int i =0; i < arr.length; i++){
arr[i] = Integer.parseInt(input[i]);
}
int first = arr[0];
max = first;
result[0] = first;
for(int i =1; i< arr.length; i++){
int index = sort(i, arr[i]);
shift(index, i);
result[index] =arr[i];
}
int[] sum = new int[num];
int total = 0;
for(int i = 0; i< result.length; i++){
total += result[i];
sum[i] += total;
}
total = 0;
for(int i = 0; i< result.length; i++){
total+= sum[i];
}
System.out.println(total);
}
public static void shift(int start, int end){
for(int i = end; i > start; i--) {
int temp = result[i - 1];
result[i - 1] = result[i];
result[i] = temp;
}
}
public static int sort(int index, int key){
boolean toggle = false;
if (max <= key){
max = key;
return index;
}
for(int i=index-1; i >=0; i--){
if(result[i]<=key){
if(result[i+1] != 0) return i+1;
}
if(result[i]>key)
toggle = true;
}
if(toggle)
return 0;
else return index;
}
}