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 ff first
#define ss second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define int long long
const int N = 5e3+5;
int n, q;
int sp[20][N];
int dp[N][N];
int a[N];
int get(int l, int r){
int x = 0;
while((1<<x) <= (r-l+1))x++;
x--;
r = r-(1<<x)+1;
return max(sp[x][l], sp[x][r]);
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
sp[0][i] = a[i];
if(i > 2)dp[i-2][i] = a[i-2] + a[i-1] + a[i];
}
for(int i = 1; i < 20; 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 len = 3; len <= n; len++){
int l, r;
for(l = 1; l <= n; l++){
if(l+len>n)break;
r = l+len;
dp[l][r] = max(dp[l+1][r], dp[l][r-1]);
dp[l][r] = max(dp[l][r], a[l]+a[r]+ get(l+1, (r+l)>>1));
}
}
cin >> q;
for(int i = 0; i < q; i++){
int l, r;
cin >> l >> r;
cout << dp[l][r] << '\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... |