File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 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+ ```
You can’t perform that action at this time.
0 commit comments