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<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 5000 + 3;
int n, lg[N];
ll a[13][N];
int get(int l, int r) {
int k = lg[r - l + 1];
return max(a[k][l], a[k][r - (1 << k) + 1]);
}
ll dp[N][N];
int main() {
cin.tie(NULL)->sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[0][i];
for (int j = 0; j < 12; ++j)
for (int i = 1; i + (2 << j) - 1 <= n; ++i)
a[j + 1][i] = max(a[j][i], a[j][i + (1 << j)]);
lg[1] = 0;
for (int i = 2; i <= n; ++i) lg[i] = lg[i >> 1] + 1;
for (int i = 1; i <= n; ++i)
for (int k = i + 2; k <= n; ++k)
dp[i][k] = a[0][i] + get(i + 1, (i + k) >> 1) + a[0][k];
for (int len = 3; len <= n; ++len)
for (int i = 1, j = len; j <= n; ++i, ++j)
dp[i][j] = max({ dp[i][j - 1], dp[i + 1][j], a[0][i] + get(i + 1,(i + j) >> 1) + a[0][j] });
int q, l, r; cin >> q; while (q--) {
cin >> l >> r;
cout << dp[l][r] << '\n';
}
}
/** /\_/\
* (= ._.)
* / >0 \>1
**/
# | 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... |