#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_map>
#include <functional>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include "homecoming.h"
using namespace std;
typedef long long llong;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<llong, llong> pll;
const llong inf = 1e18;
int n, k;
int a[2000001];
int b[2000001];
llong bs[2000001];
llong getSum(int i, int j) {
if (j <= n) return bs[j] - bs[i - 1];
return bs[j - n] + bs[n] - bs[i - 1];
}
void setMax(llong &x, llong y) {
if (x < y) x = y;
}
llong dp[2000001][2];
llong solve(int N, int K, int A[], int B[]) {
n = N;
k = K;
for (int i = 1; i <= n; ++i) {
a[i] = A[i - 1];
b[i] = B[i - 1];
bs[i] = bs[i - 1] + b[i];
}
for (int i = 0; i <= n; ++i) {
dp[i][0] = dp[i][1] = -inf;
}
llong ans1 = dp[1][0] = 0;
for (int i = 2; i <= n; ++i) {
if (k < i) setMax(dp[i][0], dp[i - k][1]);
setMax(dp[i][0], dp[i - 1][0]);
setMax(dp[i][1], dp[i - 1][1] + a[i] - b[(i + k - 2) % n + 1]);
setMax(dp[i][1], dp[i - 1][0] + a[i] - getSum(i, i + k - 1));
ans1 = max({ ans1, dp[i][0], dp[i][1] });
}
for (int i = 0; i <= n; ++i) {
dp[i][0] = dp[i][1] = -inf;
}
llong ans2 = dp[1][1] = a[1] - getSum(1, k);
for (int i = 2; i <= n; ++i) {
if (k < i) setMax(dp[i][0], dp[i - k][1]);
setMax(dp[i][0], dp[i - 1][0]);
int x = i + k - 1;
setMax(dp[i][1], dp[i - 1][1] + a[i] - (x > n ? 0 : b[x]));
setMax(dp[i][1], dp[i - 1][0] + a[i] - getSum(i, min(i + k - 1, n)));
ans2 = max({ ans2, dp[i][0], dp[i][1] });
}
return max(ans1, ans2);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
640 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
29460 KB |
Output is correct |
2 |
Correct |
3 ms |
896 KB |
Output is correct |
3 |
Correct |
208 ms |
118124 KB |
Output is correct |
4 |
Correct |
2 ms |
1408 KB |
Output is correct |
5 |
Correct |
9 ms |
3416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
640 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
54 ms |
29460 KB |
Output is correct |
12 |
Correct |
3 ms |
896 KB |
Output is correct |
13 |
Correct |
208 ms |
118124 KB |
Output is correct |
14 |
Correct |
2 ms |
1408 KB |
Output is correct |
15 |
Correct |
9 ms |
3416 KB |
Output is correct |
16 |
Correct |
180 ms |
118076 KB |
Output is correct |
17 |
Correct |
80 ms |
28060 KB |
Output is correct |
18 |
Correct |
143 ms |
41896 KB |
Output is correct |
19 |
Correct |
128 ms |
66764 KB |
Output is correct |
20 |
Correct |
176 ms |
101236 KB |
Output is correct |
21 |
Correct |
133 ms |
64328 KB |
Output is correct |