#include <bits/stdc++.h>
#include "homecoming.h"
#define mp make_pair
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
const ll INF = 1e18;
struct pref_sum {
vector<ll> a;
ll n;
pref_sum() {}
pref_sum(int *A, ll s) {
n = s;
a.resize(n);
for (ll i = 0; i < n; i++)
a[i] = A[i], a[i] += i == 0 ? 0 : a[i - 1];
}
ll get(ll l, ll r) {
if (l <= r)
return a[r] - (l == 0 ? 0 : a[l - 1]);
return a[l] + get(r, n - 1);
}
};
ll n, k;
pref_sum val;
pref_sum cost;
ll cmp1(pii a, pii b) {return a.first < b.first;}
ll get_profit(ll l, ll r) {
return val.get(l, (l + k - 1) % n) - cost.get(l, r);
}
ll solve(int N, int K, int *A, int *B) {
n = N, k = K;
val = pref_sum(A, N);
cost = pref_sum(B, N);
vector<vector<ll> > dp(n, vector<ll>(2, -INF));
// take 1
dp[0][1] = val.get(0, 0) - cost.get(0, k - 1);
for (ll i = 1; i < n; i++) {
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
ll val_continue = dp[i - 1][1] + val.get(i, i);
if (i + k - 1 < n)
val_continue -= cost.get(i + k - 1, i + k - 1);
ll val_begin_new = dp[i - 1][0] + val.get(i, i) - cost.get(i, min(i + k - 1, n - 1));
dp[i][1] = max(val_continue, val_begin_new);
}
ll out = max(dp[n - 1][0], dp[n - 1][1]);
dp = vector<vector<ll> >(n, vector<ll>(2, -INF));
dp[0][0] = 0;
for (ll i = 1; i < n; i++)
{
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
ll val_continue = dp[i - 1][1] + val.get(i, i);
val_continue -= cost.get((i + k - 1) % n, (i + k - 1) % n);
ll val_begin_new = dp[i - 1][0] + val.get(i, i) - cost.get(i, (i + k - 1) % n);
dp[i][1] = max(val_continue, val_begin_new);
}
return max(out, max(dp[n - 1][0], dp[n - 1][1]));
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
1112 KB |
Output is correct |
7 |
Correct |
1 ms |
1112 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Incorrect |
1 ms |
1112 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
75 ms |
66912 KB |
Output is correct |
2 |
Correct |
4 ms |
344 KB |
Output is correct |
3 |
Runtime error |
233 ms |
262144 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
1112 KB |
Output is correct |
7 |
Correct |
1 ms |
1112 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Incorrect |
1 ms |
1112 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |