This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using i64 = long long;
using namespace std;
i64 solve(int n, int k, int *A, int *B) {
vector<int> a(n), b(2 * n);
for (int i = 0; i < n; i++) {
a[i] = A[i];
b[i] = B[i];
b[i + n] = b[i];
}
i64 ans = accumulate(A, A + n, 0LL) - accumulate(B, B + n, 0LL);
for (int i = 0; i < 1; i++) {
vector<i64> pre(2 * n + 1);
for (int i = 0; i < 2 * n; i++) {
pre[i + 1] = pre[i] + b[i];
}
{
vector<i64> dp(n), keep(n);
keep[0] = a[0] - pre[k];
dp[0] = 0LL;
for (int i = 1; i < n; i++) {
keep[i] = max(dp[i - 1] + a[i] - (pre[i + k] - pre[i]), keep[i - 1] + a[i] - b[i + k - 1]);
dp[i] = max(dp[i - 1], keep[i]);
}
ans = max(ans, dp[n - 1]);
} {
vector<i64> dp(n), keep(n);
keep[0] = a[0] - pre[k];
dp[0] = keep[0];
for (int i = 1; i < n; i++) {
keep[i] = max(dp[i - 1] + a[i] - (pre[min(n, i + k)] - pre[i]), keep[i - 1] + a[i] - (i + k <= n ? b[i + k - 1] : 0));
dp[i] = max(dp[i - 1], keep[i]);
}
ans = max(ans, dp[n - 1]);
}
}
return ans;
}
/*
1
3 2
40 80 100
140 0 20
*/
long long solve(int N, int K, int *A, int *B);
#ifndef EVAL
int main() {
int T; assert(scanf("%d", &T) == 1);
for(int t = 0; t < T; t++) {
int N, K; assert(scanf("%d%d", &N, &K) == 2);
int *A = new int[N];
int *B = new int[N];
for(int i = 0; i < N; i++) assert(scanf("%d", &A[i]) == 1);
for(int i = 0; i < N; i++) assert(scanf("%d", &B[i]) == 1);
printf("%lld\n", solve(N, K, A, B));
delete[] A;
delete[] B;
}
return 0;
}
#endif
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |