#include <bits/stdc++.h>
#define f first
#define s second
#define int long long
using namespace std;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0;i < n;i++)
cin >> a[i];
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
--l, --r;
vector<int> ll(n), rr(n);
vector<int> st;
for (int i = l;i <= r;i++) {
while (!st.empty() && a[st.back()] < a[i])
st.pop_back();
if (!st.empty())
ll[i] = st.back();
else
ll[i] = -1;
st.push_back(i);
}
st.clear();
for (int i = r;i >= l;i--) {
while (!st.empty() && a[st.back()] < a[i])
st.pop_back();
if (!st.empty())
rr[i] = st.back();
else
rr[i] = -1;
st.push_back(i);
}
vector<int> p(n + 1);
for (int i = r;i >= l;i--)
p[i] = max(p[i + 1], a[i]);
int ans = 0;
for (int i = l;i <= r;i++) {
if (rr[i] == -1)
continue;
int x = rr[i] - i;
int j = rr[i] + x;
if (j <= r)
ans = max(ans, a[i] + a[rr[i]] + p[j]);
}
for (int i = l;i <= r;i++) {
if (ll[i] == -1)
continue;
int x = i - ll[i];
int j = i + x;
if (j <= r)
ans = max(ans, a[i] + a[ll[i]] + p[j]);
}
cout << ans << '\n';
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Execution timed out |
4026 ms |
3212 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
6488 KB |
Output is correct |
2 |
Correct |
20 ms |
8304 KB |
Output is correct |
3 |
Correct |
19 ms |
8404 KB |
Output is correct |
4 |
Correct |
23 ms |
6648 KB |
Output is correct |
5 |
Correct |
21 ms |
6744 KB |
Output is correct |
6 |
Correct |
19 ms |
7584 KB |
Output is correct |
7 |
Correct |
18 ms |
7516 KB |
Output is correct |
8 |
Correct |
18 ms |
7516 KB |
Output is correct |
9 |
Correct |
19 ms |
8028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Execution timed out |
4026 ms |
3212 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |