#include<bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using vi = vector<ll>;
using vvi = vector<vi>;
const ll mod = 1000000007ll;
ll n, mx[5050][5050], dp[5050][5050];
vi a;
ll getmax(int l, int r) {
l = max(l, 1);
r = min(r, (int)n);
if(l > r) return 0;
if(mx[l][r]) return mx[l][r];
if(l == r) return mx[l][r] = a[l];
return mx[l][r] = max(getmax(l, r-1), a[r]);
}
ll get(int l, int r) {
if(r-l<2) return 0;
if(dp[l][r] != -1) return dp[l][r];
dp[l][r] = a[l]+a[r]+getmax(l+1, (r+l)/2);
dp[l][r] = max({dp[l][r], get(l+1, r), get(l, r-1)});
return dp[l][r];
}
int main() {
ios::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
memset(dp, -1, sizeof dp);
memset(mx, 0, sizeof mx);
cin >> n;
a.resize(n+1);
for(int i = 1; i <= n; i++) cin >> a[i];
int q, l, r;
cin >> q;
while(q--) {
cin >> l >> r;
cout << get(l, r) << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
338 ms |
399608 KB |
Output is correct |
2 |
Correct |
336 ms |
399712 KB |
Output is correct |
3 |
Correct |
334 ms |
399660 KB |
Output is correct |
4 |
Correct |
337 ms |
399668 KB |
Output is correct |
5 |
Correct |
361 ms |
399636 KB |
Output is correct |
6 |
Correct |
346 ms |
399616 KB |
Output is correct |
7 |
Correct |
357 ms |
399832 KB |
Output is correct |
8 |
Correct |
341 ms |
399736 KB |
Output is correct |
9 |
Correct |
348 ms |
399564 KB |
Output is correct |
10 |
Correct |
352 ms |
399644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
338 ms |
399608 KB |
Output is correct |
2 |
Correct |
336 ms |
399712 KB |
Output is correct |
3 |
Correct |
334 ms |
399660 KB |
Output is correct |
4 |
Correct |
337 ms |
399668 KB |
Output is correct |
5 |
Correct |
361 ms |
399636 KB |
Output is correct |
6 |
Correct |
346 ms |
399616 KB |
Output is correct |
7 |
Correct |
357 ms |
399832 KB |
Output is correct |
8 |
Correct |
341 ms |
399736 KB |
Output is correct |
9 |
Correct |
348 ms |
399564 KB |
Output is correct |
10 |
Correct |
352 ms |
399644 KB |
Output is correct |
11 |
Correct |
770 ms |
409756 KB |
Output is correct |
12 |
Correct |
779 ms |
409584 KB |
Output is correct |
13 |
Correct |
754 ms |
409600 KB |
Output is correct |
14 |
Correct |
782 ms |
409720 KB |
Output is correct |
15 |
Correct |
785 ms |
409664 KB |
Output is correct |
16 |
Correct |
757 ms |
408840 KB |
Output is correct |
17 |
Correct |
755 ms |
409024 KB |
Output is correct |
18 |
Correct |
787 ms |
409020 KB |
Output is correct |
19 |
Correct |
765 ms |
409524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
859 ms |
412240 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
338 ms |
399608 KB |
Output is correct |
2 |
Correct |
336 ms |
399712 KB |
Output is correct |
3 |
Correct |
334 ms |
399660 KB |
Output is correct |
4 |
Correct |
337 ms |
399668 KB |
Output is correct |
5 |
Correct |
361 ms |
399636 KB |
Output is correct |
6 |
Correct |
346 ms |
399616 KB |
Output is correct |
7 |
Correct |
357 ms |
399832 KB |
Output is correct |
8 |
Correct |
341 ms |
399736 KB |
Output is correct |
9 |
Correct |
348 ms |
399564 KB |
Output is correct |
10 |
Correct |
352 ms |
399644 KB |
Output is correct |
11 |
Correct |
770 ms |
409756 KB |
Output is correct |
12 |
Correct |
779 ms |
409584 KB |
Output is correct |
13 |
Correct |
754 ms |
409600 KB |
Output is correct |
14 |
Correct |
782 ms |
409720 KB |
Output is correct |
15 |
Correct |
785 ms |
409664 KB |
Output is correct |
16 |
Correct |
757 ms |
408840 KB |
Output is correct |
17 |
Correct |
755 ms |
409024 KB |
Output is correct |
18 |
Correct |
787 ms |
409020 KB |
Output is correct |
19 |
Correct |
765 ms |
409524 KB |
Output is correct |
20 |
Incorrect |
859 ms |
412240 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |