#include <bits/stdc++.h>
using namespace std;
const int nx=5e5+5;
int n, a[nx], mx[nx], res, q, l, r;
stack<int> s;
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
for (int i=1; i<=n; i++) cin>>a[i];
mx[n+1]=-1e9;
for (int i=n; i>=1; i--) mx[i]=max(mx[i+1], a[i]);
cin>>q>>l>>r;
for (int i=1; i<=n; i++)
{
while (!s.empty()&&s.top()<a[i]) res=max(res, a[s.top()]+a[i]+mx[min(n+1, 2*i-s.top())]), s.pop();
if (!s.empty()) res=max(res, a[s.top()]+a[i]+mx[min(n+1, 2*i-s.top())]);
s.push(i);
//cout<<i<<' '<<res<<'\n';
}
cout<<res;
}
/*
5
5 4 3 2 1
1
1 5
15
12 96 100 61 54 66 37 34 58 21 21 1 13 50 81
1
1 15
5
5 2 1 5 3
1
1 5
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
19 ms |
3160 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |