#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n,m;
int a[500007];
namespace sub1 {
bool check() {
return m!=1;
}
int st[2000007];
void build(int id, int l, int r) {
if (l == r) {
st[id] = a[l];
return;
}
int mid = (l + r) >> 1;
build(2*id,l,mid);
build(2*id+1,mid+1,r);
st[id] = max(st[2*id],st[2*id+1]);
}
int get(int id, int l, int r, int u, int v) {
if (l > v || u > r) return -1;
if (u <= l && r <= v) return st[id];
int mid = (l + r) >> 1;
return max(get(2*id,l,mid,u,v),get(2*id+1,mid+1,r,u,v));
}
void solve() {
build(1,1,n);
while (m--) {
int l,r;
cin >> l >> r;
ll res = 0;
for (int i=l; i<=r; i++) {
for (int j=i+1; 2*j-i<=r; j++) {
res = max(res,1ll*a[i]+a[j]+get(1,1,n,2*j-i,r));
}
}
cout << res << '\n';
}
}
}
namespace sub2 {
bool check() {
return m == 1;
}
int mx[500007];
void solve() {
int l,r;
cin >> l >> r;
for (int i=r; i>=l; i--) {
mx[i] = max(mx[i+1],a[i]);
}
ll res = 0;
for (int i=l; i<=r; i++) {
for (int j=i+1; 2*j-i<=r; j++) {
res = max(res,1ll*a[i]+a[j]+mx[2*j-i]);
}
}
cout << res;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for (int i=1; i<=n; i++) {
cin >> a[i];
}
cin >> m;
if (sub1::check()) {
sub1::solve();
return 0;
}
if (sub2::check()) {
sub2::solve();
return 0;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
476 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
3 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
3 ms |
476 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
476 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
3 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
3 ms |
476 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Execution timed out |
4003 ms |
600 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4069 ms |
1880 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
476 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
3 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
3 ms |
476 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Execution timed out |
4003 ms |
600 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |