Skip to content

Commit d1dbf74

Browse files
authored
[20250225] BOJ / P5 / 부분배열 고르기 / 권혁준
1 parent bddf843 commit d1dbf74

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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;
23+
static long[] A, C;
24+
static int[][] L;
25+
static boolean[] V;
26+
27+
public static void main(String[] args) throws Exception {
28+
29+
ready();
30+
solve();
31+
32+
bwEnd();
33+
34+
}
35+
36+
static void ready() throws Exception{
37+
38+
N = Integer.parseInt(br.readLine());
39+
40+
r = new int[N];
41+
C = new long[N];
42+
A = new long[N];
43+
L = new int[N][2];
44+
V = new boolean[N];
45+
nextLine();
46+
47+
48+
for(int i=0;i<N;i++) {
49+
r[i] = i;
50+
A[i] = nextLong();
51+
C[i] = A[i];
52+
L[i][0] = (int)A[i];
53+
L[i][1] = i;
54+
}
55+
56+
}
57+
58+
static void solve() throws Exception{
59+
60+
Arrays.sort(L, (a,b) -> b[0]-a[0]);
61+
62+
long ans = 0;
63+
for(int[] now : L){
64+
int v = now[0], i = now[1];
65+
if(i>0 && V[i-1]){
66+
int x = f(i), y = f(i-1);
67+
C[x] += C[y];
68+
r[y] = x;
69+
}
70+
if(i<N-1 && V[i+1]){
71+
int x = f(i), y = f(i+1);
72+
C[x] += C[y];
73+
r[y] = x;
74+
}
75+
V[i] = true;
76+
ans = Math.max(ans, v * C[f(i)]);
77+
78+
}
79+
bw.write(ans + "\n");
80+
81+
}
82+
83+
static int f(int x) {
84+
return x==r[x] ? x : (r[x]=f(r[x]));
85+
}
86+
87+
}
88+
89+
```

0 commit comments

Comments
 (0)