#include "homecoming.h"
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int64_t solve(int n, int k, vector<int> a, vector<int> b) {
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
vector<int64_t> sumb(n + 1, 0);
for (int i = n - 1; i >= 0; --i) {
sumb[i] = sumb[i + 1] + b[i];
}
vector<int64_t> suma(n + 1, 0);
for (int i = n - 1; i >= 0; --i) {
suma[i] = suma[i + 1] + a[i];
}
int64_t fans = max((int64_t) 0, suma[0] - sumb[0]);
{ // Take n - 1
vector<int64_t> dp(n);
vector<int64_t> partDp(n, 0);
for (int i = 0; i < k; ++i) {
dp[i] = (suma[0] - suma[i + 1]) - (sumb[0] - sumb[i + 1]);
if (i > 0) {
partDp[i] = max(dp[i], partDp[i - 1]);
} else {
partDp[i] = max((int64_t) 0, dp[i]);
}
}
for (int i = k; i < n; ++i) {
dp[i] = dp[i - 1] + a[i] - b[i];
int64_t v = (i - k - 1 >= 0 ? partDp[i - k - 1]: 0) + a[i] - (sumb[i - k + 1] - sumb[i + 1]);
if (dp[i] < v) {
dp[i] = v;
}
partDp[i] = max(dp[i], partDp[i - 1]);
}
int64_t add = a[n - 1] - (sumb[n - k] - sumb[n]);
int64_t ans = max(add, dp[n - 2] + a[n - 1] - b[n - 1]);
for (int i = n - k - 2; i >= 0; --i) {
ans = max(ans, add + dp[i]);
}
fans = max(fans, ans);
}
{ // Don't take n - 1
vector<int64_t> dp(n);
vector<int64_t> partDp(n);
for (int i = 0; i < k; ++i) {
dp[i] = a[i] - (sumb[0] - sumb[i + 1]) - (sumb[n - (k - i) + 1] - sumb[n]);
if (i > 0) {
dp[i] = max(dp[i], dp[i - 1] + a[i] - b[i]);
}
if (i > 0) {
partDp[i] = max(dp[i], partDp[i - 1]);
} else {
partDp[i] = max((int64_t) 0, dp[i]);
}
}
for (int i = k; i < n; ++i) {
dp[i] = dp[i - 1] + a[i] - b[i];
int64_t v = (i - k - 1 >= 0 ? partDp[i - k - 1]: 0) + a[i] - (sumb[i - k + 1] - sumb[i + 1]);
if (dp[i] < v) {
dp[i] = v;
}
partDp[i] = max(dp[i], partDp[i - 1]);
}
int64_t ans = 0;
for (int i = 0; i < n - 1; ++i) {
ans = max(ans, dp[i]);
}
fans = max(fans, ans);
}
return fans;
}
long long solve(int n, int k, int* a, int* b) {
return solve(n, k, vector<int>(a, a + n), vector<int>(b, b + n));
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
484 KB |
Output is correct |
3 |
Incorrect |
2 ms |
484 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
484 KB |
Output is correct |
3 |
Incorrect |
2 ms |
484 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
82 ms |
23992 KB |
Output is correct |
2 |
Correct |
7 ms |
23992 KB |
Output is correct |
3 |
Correct |
262 ms |
94564 KB |
Output is correct |
4 |
Correct |
5 ms |
94564 KB |
Output is correct |
5 |
Correct |
15 ms |
94564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
484 KB |
Output is correct |
3 |
Incorrect |
2 ms |
484 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |