// Andrei-Costin Constantinescu
// O(N^2) - Dynamic Programming
#include "homecoming.h"
#include <bits/stdc++.h>
using namespace std;
void subtaskAsserts(int N, int K, int a[], int b[]) {
assert(N <= 5000);
}
typedef long long int lint;
const int NMAX = 2000000;
const int VALMAX = 1000000000;
void asserts(int N, int K, int a[], int b[]) {
assert(1 <= K && K <= N && N <= NMAX);
for (int i = 0; i < N; ++ i) {
assert(0 <= a[i] && a[i] <= VALMAX);
assert(0 <= b[i] && b[i] <= VALMAX);
}
}
int A[2 * NMAX + 5], B[2 * NMAX + 5];
lint dpTaken[2 * NMAX + 5];
lint dp[2 * NMAX + 5];
lint sPartB[2 * NMAX];
inline lint getSPartB(int l, int r) {
lint sum = sPartB[r];
if (l > 0)
sum -= sPartB[l - 1];
return sum;
}
lint solve(int N, int K, int a[], int b[]) {
asserts(N, K, a, b), subtaskAsserts(N, K, a, b);
// Copy and double a, b for internal use
for (int i = 0; i < N; ++ i) {
A[i] = a[i], A[N + i] = a[i];
B[i] = b[i], B[N + i] = b[i];
}
// Compute the partial sums of B
sPartB[0] = B[0];
for (int i = 1; i < 2 * N; ++ i)
sPartB[i] = sPartB[i - 1] + B[i];
// Try to take all textbooks
lint ans = 0;
for (int i = 0; i < N; ++ i)
ans += A[i] - B[i];
// Fix a textbook we'll NOT take
for (int i = 0; i < N; ++ i) {
dp[i + N] = 0;
for (int j = i + N - 1; j >= i + 1; -- j) {
if (j + K - 1 < i + N) {
dpTaken[j] = dp[j + K];
if (j + K < i + N)
dpTaken[j] = max(dpTaken[j], dpTaken[j + 1] + A[j + 1] - B[j + K]);
}
dp[j] = dp[j + 1];
if (j + K - 1 < i + N)
dp[j] = max(dp[j], dpTaken[j] + A[j] - getSPartB(j, j + K - 1));
}
ans = max(ans, dp[i + 1]);
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
4 ms |
612 KB |
Output is correct |
3 |
Correct |
2 ms |
612 KB |
Output is correct |
4 |
Correct |
5 ms |
612 KB |
Output is correct |
5 |
Correct |
3 ms |
612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
4 ms |
612 KB |
Output is correct |
3 |
Correct |
2 ms |
612 KB |
Output is correct |
4 |
Correct |
5 ms |
612 KB |
Output is correct |
5 |
Correct |
3 ms |
612 KB |
Output is correct |
6 |
Correct |
60 ms |
796 KB |
Output is correct |
7 |
Correct |
99 ms |
948 KB |
Output is correct |
8 |
Correct |
29 ms |
948 KB |
Output is correct |
9 |
Correct |
121 ms |
948 KB |
Output is correct |
10 |
Correct |
4 ms |
948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
63 ms |
8636 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
4 ms |
612 KB |
Output is correct |
3 |
Correct |
2 ms |
612 KB |
Output is correct |
4 |
Correct |
5 ms |
612 KB |
Output is correct |
5 |
Correct |
3 ms |
612 KB |
Output is correct |
6 |
Correct |
60 ms |
796 KB |
Output is correct |
7 |
Correct |
99 ms |
948 KB |
Output is correct |
8 |
Correct |
29 ms |
948 KB |
Output is correct |
9 |
Correct |
121 ms |
948 KB |
Output is correct |
10 |
Correct |
4 ms |
948 KB |
Output is correct |
11 |
Runtime error |
63 ms |
8636 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
12 |
Halted |
0 ms |
0 KB |
- |