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;
const int N = (int)5e5+7;
const int M = (int)2e5+7;
const int inf = (int)1e9+7;
int n, q, a[N];
int dp[5003][5003], mx[5003][5003];
int oo[M][20], lo[M];
pair<int, int> pref[N], suf[N];
int get(int l, int r) {
if(l>r) {
return -inf;
}
int x = lo[r-l+1];
return max(oo[l][x], oo[r-(1<<x)+1][x]);
}
void solve3() {
cin >> q >> q >> q;
for(int i = 2; i < M; ++i) {
lo[i] = lo[i/2]+1;
}
for(int i = 1; i <= n; ++i) {
oo[i][0] = a[i];
pref[i] = max(pref[i-1], {a[i], i});
}
for(int i = n; i >= 1; --i) {
suf[i] = max(suf[i+1], {a[i], i});
}
for(int i = 2; i < 18; ++i) {
for(int j = 1; j <= n-(1<<i)+1; ++j) {
oo[i][j] = max(oo[i][j], oo[i+(1<<(j-1))][j-1]);
}
}
int ans = 0;
for(int i = 2; i < n; ++i) {
int nxt = suf[i+1].second;
ans = max(ans, a[i]+a[nxt]+get(max(1, i-(nxt-i)), i-1));
nxt = pref[i-1].second;
if(i+(i-nxt)<=n) {
ans = max(ans, a[i]+a[nxt]+get(i+(i-nxt), n));
}
}
cout << ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
}
if(n>5000) {
solve3();
return 0;
}
for(int i = 1; i <= n; ++i) {
for(int j = i; j <= n; ++j) {
mx[i][j] = max(mx[i][j-1], a[j]);
}
}
for(int len = 3; len <= n; ++len) {
for(int i = 1; i <= n-len+1; ++i) {
int j = i+len-1;
dp[i][j]=max(dp[i+1][j], dp[i][j-1]);
int cur = a[i]+a[j];
int m = (i+j)/2;
cur += mx[i+1][m];
dp[i][j] = max(dp[i][j], cur);
}
}
cin >> q;
while(q--) {
int l, r;
cin >> l >> r;
cout << dp[l][r] << "\n";
}
return 0;
}
# | 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... |