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;
using ll = long long;
const int N = 5e3 + 10;
int n;
ll a[N];
ll dp[N][N], mx[N][N];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i];
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 = 2; len <= n; len++) {
for(int l = 1; l <= n - len + 1; l++) {
int r = l + len - 1;
dp[l][r] = max({dp[l][r - 1], dp[l + 1][r], a[l] + a[r] + mx[l + 1][(l + r) / 2]});
}
}
int q;
cin >> q;
while(q--) {
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... |