Skip to content

Commit 696fa14

Browse files
authored
Merge pull request #1791 from AlgorithmWithGod/khj20006
[20260113] BOJ / G1 / 자르기 / 권혁준
2 parents 279f987 + 08a12c6 commit 696fa14

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
```cpp
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
int N;
6+
int arr[1000002]{};
7+
int left_max1[1000002]{}, left_max2[1000002]{};
8+
int right_max1[1000002]{}, right_max2[1000002]{};
9+
10+
int main() {
11+
cin.tie(0)->sync_with_stdio(0);
12+
13+
cin>>N;
14+
for(int i=1;i<=N;i++) cin>>arr[i];
15+
for(int i=1;i<=N;i++) {
16+
left_max1[i] = max(left_max1[i-1], arr[i]);
17+
if(i>1) left_max2[i] = max(left_max2[i-1], arr[i]);
18+
}
19+
for(int i=N;i>=1;i--) {
20+
right_max1[i] = max(right_max1[i+1], arr[i]);
21+
if(i<N) right_max2[i] = max(right_max2[i+1], arr[i]);
22+
}
23+
24+
int ans = 1e9;
25+
for(int i=2;i<=N-2;i++) {
26+
int left_result = min(arr[1] + left_max2[i], left_max1[i-1] + arr[i]);
27+
int right_result = min(arr[i+1] + right_max1[i+2], right_max2[i+1] + arr[N]);
28+
ans = min(ans, left_result + right_result);
29+
}
30+
31+
cout<<ans;
32+
33+
}
34+
```

0 commit comments

Comments
 (0)