File tree Expand file tree Collapse file tree 1 file changed +83
-0
lines changed
Expand file tree Collapse file tree 1 file changed +83
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+
3+ import java.util.* ;
4+ import java.io.* ;
5+
6+
7+ class Main {
8+
9+ // IO field
10+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
11+ static BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System . out));
12+ static StringTokenizer st;
13+
14+ static void nextLine () throws Exception {st = new StringTokenizer (br .readLine ());}
15+ static int nextInt() {return Integer . parseInt(st. nextToken());}
16+ static long nextLong() {return Long . parseLong(st. nextToken());}
17+ static void bwEnd() throws Exception {bw. flush();bw. close();}
18+
19+ // Additional field
20+
21+ static int N ;
22+ static int [] r, A ;
23+ static long [] C ;
24+ static boolean [] V ;
25+ static int [][] info;
26+
27+ static int f(int x) {return x== r[x] ? x : (r[x]= f(r[x]));}
28+
29+ public static void main(String [] args) throws Exception {
30+
31+ ready();
32+ solve();
33+
34+ bwEnd();
35+
36+ }
37+
38+ static void ready() throws Exception {
39+
40+ N = Integer . parseInt(br. readLine());
41+ A = new int [N ];
42+ r = new int [N ];
43+ C = new long [N ];
44+ V = new boolean [N ];
45+ info = new int [N ][2 ];
46+ nextLine();
47+ for (int i= 0 ;i< N ;i++ ) {
48+ A [i] = nextInt();
49+ r[i] = i;
50+ C [i] = 1 ;
51+ info[i][0 ] = A [i];
52+ info[i][1 ] = i;
53+ }
54+ Arrays . sort(info, (a,b) - > b[0 ]- a[0 ]);
55+
56+ }
57+
58+ static void solve() throws Exception {
59+
60+ long ans = 0 ;
61+ for (int [] n: info) {
62+ int val = n[0 ], idx = n[1 ];
63+ V [idx] = true ;
64+ if (idx > 0 && V [idx- 1 ]) {
65+ int x = f(idx- 1 ), y = f(idx);
66+ C [y] += C [x];
67+ r[x] = y;
68+ }
69+
70+ if (idx < N - 1 && V [idx+ 1 ]) {
71+ int x = f(idx+ 1 ), y = f(idx);
72+ C [y] += C [x];
73+ r[x] = y;
74+ }
75+
76+ ans = Math . max(ans, C [f(idx)] * val);
77+ }
78+ bw. write(ans + " \n " );
79+ }
80+
81+ }
82+
83+ ```
You can’t perform that action at this time.
0 commit comments