#ifndef EVAL
#include "homecoming.h"
#endif
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll inf = 1e17;
ll solve(int n, int k, int a[], int b[]){
vector< vector<ll> > dp(n, vector<ll>(2, -inf));
vector<ll> pref(n);
pref[0] = b[0];
for(int i = 1;i < n; i++){
pref[i] = pref[i-1] + b[i];
}
ll ans = 0;
dp[0][1] = a[0] - pref[k-1];
for(int i = 1;i < n; i++){
dp[i][0] = max(dp[i-1][0], dp[i-1][1]);
dp[i][1] = max(dp[i-1][0] + a[i] - (pref[min(n-1, i + k-1)] - pref[i-1]),
dp[i-1][1] + a[i] - (i + k-1 < n ? b[i+k-1] : 0LL));
}
ans = max(ans, dp[n-1][0]);
ans = max(ans, dp[n-1][1]);
for(int i = 0;i < n; i++) dp[i][0] = dp[i][1] = -inf;
dp[0][0] = 0;
for(int i = 1;i < n; i++){
dp[i][0] = max(dp[i-1][0], dp[i-1][1]);
dp[i][1] = max(dp[i-1][0] + a[i] - (pref[min(n-1, i + k-1)] - pref[i-1] + (i + k-1 >= n ? pref[i+k-1-n] : 0LL)),
dp[i-1][1] + a[i] - b[(i + k-1) % n]);
}
ans = max(ans, dp[n-1][0]);
ans = max(ans, dp[n-1][1]);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
35672 KB |
Output is correct |
2 |
Correct |
3 ms |
856 KB |
Output is correct |
3 |
Correct |
194 ms |
180908 KB |
Output is correct |
4 |
Correct |
2 ms |
2140 KB |
Output is correct |
5 |
Correct |
11 ms |
3468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
55 ms |
35672 KB |
Output is correct |
12 |
Correct |
3 ms |
856 KB |
Output is correct |
13 |
Correct |
194 ms |
180908 KB |
Output is correct |
14 |
Correct |
2 ms |
2140 KB |
Output is correct |
15 |
Correct |
11 ms |
3468 KB |
Output is correct |
16 |
Correct |
195 ms |
180960 KB |
Output is correct |
17 |
Correct |
89 ms |
29704 KB |
Output is correct |
18 |
Correct |
166 ms |
44696 KB |
Output is correct |
19 |
Correct |
167 ms |
98296 KB |
Output is correct |
20 |
Correct |
156 ms |
163696 KB |
Output is correct |
21 |
Correct |
165 ms |
95568 KB |
Output is correct |