#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n + 1);
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
int l, r;
cin >> l >> r;
vector<int> suf(n + 2, 0);
for(int i = n; i >= 1; i--) {
suf[i] = max(suf[i + 1], a[i]);
}
vector<int> L(n + 1, - 1);
stack<int> s;
for(int i = 1; i <= n; i++) {
while(!s.empty() && a[s.top()] < a[i]) {
s.pop();
}
if(!s.empty()) {
L[i] = s.top();
}
s.push(i);
}
int ans = 0;
for(int i = 1; i <= n; i++) {
if(L[i] != - 1 && 2 * i - L[i] <= n) {
ans = max(ans, a[L[i]] + a[i] + suf[2 * i - L[i]]);
}
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |