File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed
Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.util.* ;
3+ import java.io.* ;
4+
5+ public class boj2262 {
6+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
7+ static StringTokenizer st;
8+ static void nextLine () throws Exception {st = new StringTokenizer (br .readLine ());}
9+ static int nextInt() {return Integer . parseInt(st. nextToken());}
10+
11+ public static void main(String [] args) throws Exception {
12+ nextLine();
13+ int n = nextInt();
14+ nextLine();
15+ List<Integer > rank = new ArrayList<> ();
16+ for (int i = 0 ; i < n; i++ ) rank. add(nextInt());
17+ int answer = 0 ;
18+ while (rank. size() > 1 ) {
19+ int max = - 1 ;
20+ int idx = - 1 ;
21+
22+ for (int i = 0 ; i < rank. size(); i++ ) {
23+ if (rank. get(i) > max) {
24+ max = rank. get(i);
25+ idx = i;
26+ }
27+ }
28+
29+ int diff;
30+ if (idx == 0 ) diff = Math . abs(rank. get(idx) - rank. get(idx + 1 ));
31+ else if (idx == rank. size() - 1 ) diff = Math . abs(rank. get(idx) - rank. get(idx - 1 ));
32+ else {
33+ int left = Math . abs(rank. get(idx) - rank. get(idx - 1 ));
34+ int right = Math . abs(rank. get(idx) - rank. get(idx + 1 ));
35+ diff = Math . min(left, right);
36+ }
37+
38+ answer += diff;
39+ rank. remove(idx);
40+ }
41+
42+ System . out. println(answer);
43+ }
44+ }
45+ ```
You can’t perform that action at this time.
0 commit comments