#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define F first
#define S second
#define endl '\n'
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
const int mod = 1e9 + 7;
const int N = 5010;
const ll inf = 1e18;
int mx[N];
int suf[N];
int a[N];
int ans[N];
int dp[N];
int32_t main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int n;
cin >> n;
for (int i=1;i<=n;i++){
cin >> a[i];
mx[i] = max(a[i], mx[i-1]);
}
dp[2] = a[1]+a[2];
for (int j=3;j<=n;j++){
dp[j] = max(dp[j-1], a[j]+mx[j-1]);
}
int l = 1;
for (int r=l+1;r<=n;r++){
int c = r + (r-l);
if (c>n) break;
suf[c] = max(dp[r], suf[c]);
}
for (int i=1;i<=n;i++){
suf[i] = max(suf[i], suf[i-1]);
}
for (int r=l+2;r<=n;r++){
ans[r] = max(ans[r-1], suf[r]+a[r]);
}
memset(suf, 0, sizeof(suf));
int q;
cin >> q;
while (q--){
int r;
cin >> l >> r;
cout << ans[r] << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
432 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
432 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
432 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |