#include "homecoming.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = (int)2e6 + 7;
const ll linf = (ll)1e18 + 7;
ll pref[N + N];
ll dp[N][2][2];
ll get(int l, int r) {
if (!l) return pref[r];
return pref[r] - pref[l - 1];
}
ll solve(int n, int k, int a[], int b[]) {
pref[0] = b[0];
for (int i = 0; i < n + n; i++) {
pref[i + 1] = pref[i] + b[(i + 1) % n];
}
for (int i = 0; i < n; i++)
for (int j = 0; j < 2; j++)
for (int l = 0; l < 2; l++)
dp[i][j][l] = -linf;
dp[0][1][1] = a[0] - pref[k - 1];
dp[0][0][0] = 0;
for (int i = 1; i < n; i++) {
for (int j = 0; j < 2; j++) {
dp[i][0][j] = max(dp[i - 1][0][j], dp[i - 1][1][j]);
if (j == 0 || i + k - 1 < n) {
dp[i][1][j] = max(dp[i - 1][0][j] + a[i] - get(i, i + k - 1), dp[i - 1][1][j] + a[i] - b[(i + k - 1) % n]);
} else {
dp[i][1][j] = max(dp[i - 1][0][j] + a[i] - get(i, n - 1), dp[i - 1][1][j] + a[i]);
}
// cout << i << ' ' << j << ' ' << dp[i][0][j] << ' ' << dp[i][1][j] << endl;
}
}
ll ans = 0;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
ans = max(ans, dp[n - 1][i][j]);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
468 KB |
Output is correct |
3 |
Correct |
3 ms |
544 KB |
Output is correct |
4 |
Correct |
2 ms |
544 KB |
Output is correct |
5 |
Correct |
3 ms |
548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
468 KB |
Output is correct |
3 |
Correct |
3 ms |
544 KB |
Output is correct |
4 |
Correct |
2 ms |
544 KB |
Output is correct |
5 |
Correct |
3 ms |
548 KB |
Output is correct |
6 |
Correct |
4 ms |
892 KB |
Output is correct |
7 |
Correct |
4 ms |
956 KB |
Output is correct |
8 |
Correct |
4 ms |
956 KB |
Output is correct |
9 |
Correct |
3 ms |
956 KB |
Output is correct |
10 |
Correct |
2 ms |
956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
28044 KB |
Output is correct |
2 |
Correct |
6 ms |
28044 KB |
Output is correct |
3 |
Correct |
343 ms |
110288 KB |
Output is correct |
4 |
Correct |
7 ms |
110288 KB |
Output is correct |
5 |
Correct |
15 ms |
110288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
468 KB |
Output is correct |
3 |
Correct |
3 ms |
544 KB |
Output is correct |
4 |
Correct |
2 ms |
544 KB |
Output is correct |
5 |
Correct |
3 ms |
548 KB |
Output is correct |
6 |
Correct |
4 ms |
892 KB |
Output is correct |
7 |
Correct |
4 ms |
956 KB |
Output is correct |
8 |
Correct |
4 ms |
956 KB |
Output is correct |
9 |
Correct |
3 ms |
956 KB |
Output is correct |
10 |
Correct |
2 ms |
956 KB |
Output is correct |
11 |
Correct |
87 ms |
28044 KB |
Output is correct |
12 |
Correct |
6 ms |
28044 KB |
Output is correct |
13 |
Correct |
343 ms |
110288 KB |
Output is correct |
14 |
Correct |
7 ms |
110288 KB |
Output is correct |
15 |
Correct |
15 ms |
110288 KB |
Output is correct |
16 |
Correct |
336 ms |
110288 KB |
Output is correct |
17 |
Correct |
139 ms |
110288 KB |
Output is correct |
18 |
Correct |
235 ms |
110288 KB |
Output is correct |
19 |
Correct |
231 ms |
110288 KB |
Output is correct |
20 |
Correct |
273 ms |
119288 KB |
Output is correct |
21 |
Correct |
274 ms |
119288 KB |
Output is correct |