This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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+5);
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));
}
}
for(int i = 1; i <= xd; i++){
//it can be middle?
//it can be first?
//it can be second?
}
cin >> q;
while(q--){
int l, r; cin >> l >> r;
cout << ans << "\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |