#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 <= n; i++)
mx[j][i] = -LINF;
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++){
for(int k = j+1; k <= xd; k++){
vector<int> v; v.clear();
v.pb(a[i][1]); v.pb(a[j][1]); v.pb(a[k][1]);
sort(all(v));
if(v[1]-v[0]<=v[2]-v[1])
ans = max(ans, a[i][0]+a[j][0]+a[k][0]);
}
}
}*/
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 |
5 ms |
41296 KB |
Output is correct |
2 |
Incorrect |
6 ms |
41296 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
41296 KB |
Output is correct |
2 |
Incorrect |
6 ms |
41296 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
78416 KB |
Output is correct |
2 |
Correct |
34 ms |
78416 KB |
Output is correct |
3 |
Correct |
33 ms |
78416 KB |
Output is correct |
4 |
Correct |
53 ms |
78560 KB |
Output is correct |
5 |
Correct |
48 ms |
78416 KB |
Output is correct |
6 |
Incorrect |
54 ms |
78416 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
41296 KB |
Output is correct |
2 |
Incorrect |
6 ms |
41296 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |