#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
typedef long long ll;
typedef pair<ll,ll> pi;
typedef vector<ll> vi;
typedef long double ld;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef set<ll>::iterator sit;
typedef map<ll,ll>::iterator mit;
typedef pair<int,pair<int,int> > tri;
ll dp[2000050][2];
ll sum[4000050];
long long solve(int N, int K, int *A, int *B){
ll ans = 0;
//din pass first subject
dp[0][0] = 0;
dp[0][1] = -(ll)1e18;
sum[0] = B[0];
for(int i = 1; i < N; i++) sum[i] = sum[i-1] + (ll)B[i];
for(int i = N; i < 2 * N; i++) sum[i] = sum[N-1] + sum[i-N];
for(int i = 1; i < N; i++){
dp[i][0] = max(dp[i-1][0], dp[i-1][1]);
dp[i][1] = (ll)A[i] + max(dp[i-1][0] - (sum[i+K-1]-sum[i-1]), dp[i-1][1] - (ll)B[(i+K-1)%N]);
}
ans = max(ans, max(dp[N-1][0], dp[N-1][1]));
//pass the first subject
dp[0][1] = (ll)A[0] - sum[K-1];
dp[0][0] = -(ll)1e18;
for(int i = 0; i < K; i++) B[i] = 0;
sum[0] = B[0];
for(int i = 1; i < N; i++) sum[i] = sum[i-1] + (ll)B[i];
for(int i = N; i < 2 * N; i++) sum[i] = sum[N-1] + sum[i-N];
for(int i = 1; i < N ; i++){
dp[i][0] = max(dp[i-1][0], dp[i-1][1]);
dp[i][1] = (ll)A[i] + max(dp[i-1][0] - (sum[i+K-1]-sum[i-1]), dp[i-1][1] - (ll)B[(i+K-1)%N]);
//cout<<i<<' '<<dp[i][0]<<' '<<dp[i][1] <<endl;
}
ans = max(ans, max(dp[N-1][0], dp[N-1][1]));
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
416 KB |
Output is correct |
4 |
Correct |
3 ms |
444 KB |
Output is correct |
5 |
Correct |
3 ms |
620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
416 KB |
Output is correct |
4 |
Correct |
3 ms |
444 KB |
Output is correct |
5 |
Correct |
3 ms |
620 KB |
Output is correct |
6 |
Correct |
3 ms |
772 KB |
Output is correct |
7 |
Correct |
3 ms |
772 KB |
Output is correct |
8 |
Correct |
4 ms |
772 KB |
Output is correct |
9 |
Correct |
3 ms |
772 KB |
Output is correct |
10 |
Correct |
3 ms |
772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
20160 KB |
Output is correct |
2 |
Correct |
5 ms |
20160 KB |
Output is correct |
3 |
Correct |
240 ms |
79052 KB |
Output is correct |
4 |
Correct |
5 ms |
79052 KB |
Output is correct |
5 |
Correct |
15 ms |
79052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
416 KB |
Output is correct |
4 |
Correct |
3 ms |
444 KB |
Output is correct |
5 |
Correct |
3 ms |
620 KB |
Output is correct |
6 |
Correct |
3 ms |
772 KB |
Output is correct |
7 |
Correct |
3 ms |
772 KB |
Output is correct |
8 |
Correct |
4 ms |
772 KB |
Output is correct |
9 |
Correct |
3 ms |
772 KB |
Output is correct |
10 |
Correct |
3 ms |
772 KB |
Output is correct |
11 |
Correct |
78 ms |
20160 KB |
Output is correct |
12 |
Correct |
5 ms |
20160 KB |
Output is correct |
13 |
Correct |
240 ms |
79052 KB |
Output is correct |
14 |
Correct |
5 ms |
79052 KB |
Output is correct |
15 |
Correct |
15 ms |
79052 KB |
Output is correct |
16 |
Correct |
267 ms |
79052 KB |
Output is correct |
17 |
Correct |
132 ms |
79052 KB |
Output is correct |
18 |
Correct |
231 ms |
79052 KB |
Output is correct |
19 |
Correct |
209 ms |
124040 KB |
Output is correct |
20 |
Correct |
215 ms |
185688 KB |
Output is correct |
21 |
Correct |
221 ms |
185688 KB |
Output is correct |