#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';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
256 ms |
127752 KB |
Output is correct |
12 |
Correct |
245 ms |
127824 KB |
Output is correct |
13 |
Correct |
241 ms |
127864 KB |
Output is correct |
14 |
Correct |
240 ms |
127928 KB |
Output is correct |
15 |
Correct |
258 ms |
127828 KB |
Output is correct |
16 |
Correct |
240 ms |
127312 KB |
Output is correct |
17 |
Correct |
289 ms |
127056 KB |
Output is correct |
18 |
Correct |
239 ms |
127116 KB |
Output is correct |
19 |
Correct |
263 ms |
127824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
77 ms |
83284 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
256 ms |
127752 KB |
Output is correct |
12 |
Correct |
245 ms |
127824 KB |
Output is correct |
13 |
Correct |
241 ms |
127864 KB |
Output is correct |
14 |
Correct |
240 ms |
127928 KB |
Output is correct |
15 |
Correct |
258 ms |
127828 KB |
Output is correct |
16 |
Correct |
240 ms |
127312 KB |
Output is correct |
17 |
Correct |
289 ms |
127056 KB |
Output is correct |
18 |
Correct |
239 ms |
127116 KB |
Output is correct |
19 |
Correct |
263 ms |
127824 KB |
Output is correct |
20 |
Runtime error |
77 ms |
83284 KB |
Execution killed with signal 11 |
21 |
Halted |
0 ms |
0 KB |
- |