#ifndef EVAL
#include "homecoming.h"
#endif
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const int N = 2e6 + 5;
ll dp[N][2], pa[N], pb[N];
ll solve(int n, int k, int a[], int b[]){
for(int i=0;i<n;i++){
pa[i] = a[i];
if(i) pa[i] += pa[i-1];
pb[i] = b[i];
if(i) pb[i] += pb[i-1];
}
for(int i=0;i<n;i++) memset(dp[i], 254, sizeof dp[i]);
dp[0][1] = a[0] - pb[k - 1];
for(int i=1;i<n;i++){
dp[i][0] = max(dp[i-1][1], dp[i-1][0]);
dp[i][1] = max(dp[i-1][1] + a[i] - (i + k - 1 < n ? b[i + k - 1] : 0),
dp[i-1][0] + a[i] - (pb[min(i + k - 1, n - 1)] - pb[i - 1]));
}
ll res = max(dp[n - 1][0], dp[n - 1][1]);
for(int i=0;i<n;i++) memset(dp[i], 254, sizeof dp[i]);
dp[0][0] = 0;
for(int i=1;i<n;i++){
dp[i][0] = max(dp[i-1][1], dp[i-1][0]);
dp[i][1] = max(dp[i-1][1] + a[i] - b[(i + k - 1) % n],
dp[i-1][0] + a[i] - (pb[min(i + k - 1, n - 1)] - pb[i - 1] + (i + k > n ? pb[i + k - 1 - n] : 0)));
}
res = max({res, dp[n - 1][0], dp[n - 1][1]});
return res;
}
/*
1
3 2
40 80 100
140 0 20
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
564 KB |
Output is correct |
7 |
Correct |
2 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
3 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
20344 KB |
Output is correct |
2 |
Correct |
3 ms |
852 KB |
Output is correct |
3 |
Correct |
155 ms |
78524 KB |
Output is correct |
4 |
Correct |
2 ms |
1364 KB |
Output is correct |
5 |
Correct |
8 ms |
1664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
564 KB |
Output is correct |
7 |
Correct |
2 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
3 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
344 KB |
Output is correct |
11 |
Correct |
35 ms |
20344 KB |
Output is correct |
12 |
Correct |
3 ms |
852 KB |
Output is correct |
13 |
Correct |
155 ms |
78524 KB |
Output is correct |
14 |
Correct |
2 ms |
1364 KB |
Output is correct |
15 |
Correct |
8 ms |
1664 KB |
Output is correct |
16 |
Correct |
140 ms |
78556 KB |
Output is correct |
17 |
Correct |
65 ms |
10112 KB |
Output is correct |
18 |
Correct |
118 ms |
4888 KB |
Output is correct |
19 |
Correct |
96 ms |
39512 KB |
Output is correct |
20 |
Correct |
148 ms |
78584 KB |
Output is correct |
21 |
Correct |
107 ms |
39604 KB |
Output is correct |