#include<iostream>
#define ll long long
#define rep(i,a,b) for(int i = a;i < b;i++)
#define N 2000003
#define INF 1e16
using namespace std;
ll books[N],prize[N],sum[N],n,k;
ll dp[N][2];
const ll PASSED=1,NPASSED=0;
ll get_sum(ll i,ll j)
{
if(i == 0)
return sum[j];
return sum[j] - sum[i-1];
}
ll solve(int NN,int K,int A[],int B[])
{
n = NN;
k = K;
rep(i,0,n)
{
prize[i] = A[i];
books[i] = B[i];
if(i == 0)
sum[i] = books[i];
else
sum[i] = sum[i-1]+books[i];
}
/*
if you pass class i and class j and j-i < k it is optimal to pass all classes in between.
*/
// fix that class 0 is passed
dp[0][PASSED] = prize[0] - get_sum(0,k-1);
dp[0][NPASSED] = -INF;
rep(i,1,n)
{
ll nextbook = i+k-1;
// passed previous class
ll cur = dp[i-1][PASSED];
if(nextbook < n)
cur -= books[nextbook];
cur+=prize[i];
dp[i][PASSED] = cur;
//didn't pass previous class
cur = dp[i-1][NPASSED];
cur -= get_sum(i,min(n-1,nextbook)); // if we add stuff that is already in dp[i-1][NPASSED], then optimal answer isn't here since we could just pass class i-1
cur+=prize[i];
dp[i][PASSED] = max(dp[i][PASSED],cur);
dp[i][NPASSED] = max(dp[i-1][NPASSED],dp[i-1][PASSED]);
}
ll ans = max(dp[n-1][PASSED],dp[n-1][NPASSED]);
// fix that he didn't pass class 0
dp[0][NPASSED] = 0;
dp[0][PASSED] = -INF;
rep(i,1,n)
{
ll nextbook = (i+k-1)%n;
// passed previous class
ll cur = dp[i-1][PASSED];
cur -= books[nextbook];
cur+=prize[i];
dp[i][PASSED] = cur;
//didn't pass previous class
cur = dp[i-1][NPASSED];
if(nextbook < i)
cur -= get_sum(i,n-1) + get_sum(0,nextbook);
else
cur -= get_sum(i,nextbook);
cur+=prize[i];
dp[i][PASSED] = max(dp[i][PASSED],cur);
dp[i][NPASSED] = max(dp[i-1][NPASSED],dp[i-1][PASSED]);
}
ans = max(ans,dp[n-1][PASSED]);
ans = max(ans,dp[n-1][NPASSED]);
return ans;
}
# |
Verdict |
Execution time |
Memory |
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 |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
376 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 |
636 KB |
Output is correct |
8 |
Correct |
3 ms |
632 KB |
Output is correct |
9 |
Correct |
3 ms |
636 KB |
Output is correct |
10 |
Correct |
3 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
27816 KB |
Output is correct |
2 |
Correct |
5 ms |
1016 KB |
Output is correct |
3 |
Correct |
231 ms |
100916 KB |
Output is correct |
4 |
Correct |
4 ms |
1588 KB |
Output is correct |
5 |
Correct |
12 ms |
3536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
376 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 |
636 KB |
Output is correct |
8 |
Correct |
3 ms |
632 KB |
Output is correct |
9 |
Correct |
3 ms |
636 KB |
Output is correct |
10 |
Correct |
3 ms |
504 KB |
Output is correct |
11 |
Correct |
60 ms |
27816 KB |
Output is correct |
12 |
Correct |
5 ms |
1016 KB |
Output is correct |
13 |
Correct |
231 ms |
100916 KB |
Output is correct |
14 |
Correct |
4 ms |
1588 KB |
Output is correct |
15 |
Correct |
12 ms |
3536 KB |
Output is correct |
16 |
Correct |
232 ms |
100928 KB |
Output is correct |
17 |
Correct |
95 ms |
15352 KB |
Output is correct |
18 |
Correct |
168 ms |
11952 KB |
Output is correct |
19 |
Correct |
171 ms |
74604 KB |
Output is correct |
20 |
Correct |
184 ms |
116740 KB |
Output is correct |
21 |
Correct |
166 ms |
72160 KB |
Output is correct |