#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];
vector<int> ll(n), rr(n);
vector<int> st;
for (int i = 0;i < n;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 = n - 1;i >= 0;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);
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
--l, --r;
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 || rr[i] > r)
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 || ll[i] < l)
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 |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
756 KB |
Output is correct |
11 |
Execution timed out |
4057 ms |
7060 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
6488 KB |
Output is correct |
2 |
Correct |
21 ms |
8404 KB |
Output is correct |
3 |
Correct |
19 ms |
8400 KB |
Output is correct |
4 |
Correct |
21 ms |
6492 KB |
Output is correct |
5 |
Correct |
21 ms |
6648 KB |
Output is correct |
6 |
Correct |
18 ms |
6492 KB |
Output is correct |
7 |
Correct |
17 ms |
6640 KB |
Output is correct |
8 |
Correct |
17 ms |
6644 KB |
Output is correct |
9 |
Correct |
19 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
756 KB |
Output is correct |
11 |
Execution timed out |
4057 ms |
7060 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |