#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) begin(a),end(a)
#define int long long
const int N = (int)5e5+10;
const int LINF = (int)2e18;
const int LG = 20;
int n, q, mx[LG][N];
array<int,2> a[N];
int rmq(int l, int r){
if(l>r) return -LINF;
int lg = __lg(r-l+1);
return max(mx[lg][l], mx[lg][r-(1<<lg)+1]);
}
int32_t main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n; int ans = 0;
for(int i = 1; i <= n; i++){
cin >> a[i][0];
a[i][1] = i; mx[0][i]=a[i][0];
}
for(int j = 1; j < LG; j++)
for(int i = 1; i+(1<<j)-1 <= n; i++)
mx[j][i] = max(mx[j-1][i],mx[j-1][i+(1<<(j-1))]);
sort(a+1,a+n+1); reverse(a+1,a+n+1);
int xd = min(n,LG+(int)sqrt(n));
for(int i = 1; i <= xd; i++){
for(int j = i+1; j <= xd; j++){
vector<int> v; v.clear();
v.pb(a[i][1]); v.pb(a[j][1]);
sort(all(v));
int l = v[0], r = v[1],sum=a[i][0]+a[j][0];
// k after r
ans = max(ans, sum+rmq(r+r-l,n));
// k before l
ans = max(ans, sum+rmq(max(1ll,l-(r-l)),l-1));
// k inside l..r
if(r-l>=2) ans = max(ans, sum+rmq(l+1,(r+l)/2));
}
}
cin >> q;
while(q--){
int l, r; cin >> l >> r;
cout << ans << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Incorrect |
2 ms |
14672 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Incorrect |
2 ms |
14672 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
68176 KB |
Output is correct |
2 |
Correct |
35 ms |
68176 KB |
Output is correct |
3 |
Correct |
31 ms |
68176 KB |
Output is correct |
4 |
Correct |
64 ms |
68168 KB |
Output is correct |
5 |
Correct |
54 ms |
68168 KB |
Output is correct |
6 |
Incorrect |
47 ms |
68176 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Incorrect |
2 ms |
14672 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |