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 all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define int long long
const int N = 6000;
int sp[25][N], ans[N][N];
int get(int l, int r){
int bt = __lg(r-l);
return max(sp[bt][l], sp[bt][r-(1<<bt)+1]);
};
void solve(){
int n, q;
cin >> n;
for(int i = 1; i <= n; i++){
cin >> sp[0][i];
}
cin >> q;
vector<pair<int,int> > qu(q);
for(auto&[l,r]:qu)cin >> l >> r;
for(int i = 1; i < 24; i++){
int len = (1 << (i-1));
for(int j = 1; j <= n; j++){
sp[i][j] = max(sp[i-1][j], sp[i-1][min(n,j+len)]);
}
}
for(int i = 0; i <= n; i++){
for(int j = 0; j <= n; j++){
ans[i][j] = 0;
}
}
for(int i = 1; i <= n; i++){
for(int j = i+2; j <= n; j++){
int mid = i + ((j-i) >> 1);
ans[i][j] = sp[0][i] + sp[0][j] + get(i+1, mid);
ans[i][j] = max(ans[i][j], ans[i][j-1]);
}
}
for(int j = n; j > 0; j--){
for(int i = j-1; i > 0; i--){
ans[i][j] = max(ans[i][j], ans[i+1][j]);
}
}
for(auto [l, r] :qu)cout << ans[l][r] << '\n';
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
//cin >> t;
while ( t-- )solve();
cout << '\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... |