#include "homecoming.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
long long solve(int N, int K, int *A, int *B)
{
if (K == 1) {
long long ans = 0;
for (int i = 0; i < N; ++i) ans += max(0, A[i] - B[i]);
return ans;
}
static const long long INF = 1e17;
long long ans = accumulate(A, A + N, 0ll) - accumulate(B, B + N, 0ll);
if (ans < 0) ans = 0;
vector<long long> total(N * 2, 0); total[0] = B[0];
for (int i = 1; i < N * 2; ++i) total[i] = total[i - 1] + B[i % N];
vector<vector<long long>> dp(2);
dp[0].assign(N + 1, 0); dp[1].assign(N + 1, -INF);
dp[1][0] = 0;
for (int i = 1; i <= N; ++i) {
dp[1][i] = dp[1][i - 1];
if (i - K + 1 >= 0) dp[1][i] = max(dp[1][i], dp[0][i - K + 1] - total[i - 1] + (i - K >= 0 ? total[i - 1 - K + 1] : 0));
dp[0][i] = max(dp[0][i - 1] + A[i - 1] - B[i - 1], dp[1][i]);
ans = max(ans, dp[0][i] - total[i + K - 2] + total[i - 1]);
}
ans = max(ans, *max_element(dp[1].begin(), dp[1].end()));
dp[0].assign(N + 1, 0); dp[1].assign(N + 1, -INF);
dp[0][1] = A[0] - B[0];
for (int i = 2; i <= N; ++i) {
dp[1][i] = dp[1][i - 1];
if (i - K + 1 >= 0) dp[1][i] = max(dp[1][i], dp[0][i - K + 1] - total[i - 1] + (i - K >= 0 ? total[i - 1 - K + 1] : 0));
dp[0][i] = max(dp[0][i - 1] + A[i - 1] - B[i - 1], dp[1][i]);
}
ans = max(ans, *max_element(dp[1].begin(), dp[1].end()));
ans = max(ans, dp[0][N]);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
636 KB |
Output is correct |
7 |
Correct |
3 ms |
632 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
20088 KB |
Output is correct |
2 |
Correct |
5 ms |
632 KB |
Output is correct |
3 |
Correct |
134 ms |
16020 KB |
Output is correct |
4 |
Correct |
5 ms |
1400 KB |
Output is correct |
5 |
Correct |
12 ms |
1464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
636 KB |
Output is correct |
7 |
Correct |
3 ms |
632 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
11 |
Correct |
61 ms |
20088 KB |
Output is correct |
12 |
Correct |
5 ms |
632 KB |
Output is correct |
13 |
Correct |
134 ms |
16020 KB |
Output is correct |
14 |
Correct |
5 ms |
1400 KB |
Output is correct |
15 |
Correct |
12 ms |
1464 KB |
Output is correct |
16 |
Correct |
235 ms |
117936 KB |
Output is correct |
17 |
Correct |
104 ms |
23236 KB |
Output is correct |
18 |
Correct |
177 ms |
41492 KB |
Output is correct |
19 |
Correct |
193 ms |
66804 KB |
Output is correct |
20 |
Correct |
195 ms |
101096 KB |
Output is correct |
21 |
Correct |
182 ms |
65660 KB |
Output is correct |