#include<bits/stdc++.h>
#include "homecoming.h"
using namespace std;
long long a,s,d[2000002][2],f[2000002][2],g[2000002],h[2000002],j,k,l,i,n,m;
long long get(int l,int r){
l++;r++;
//cout<<l<<" "<<r<<" "<<g[l-1]<<" "<<g[r]<<endl;
if(r>=l) return g[r]-g[l-1];
else return (g[n]-g[r-1])+g[l];
}
int gi(int i){
if(i>=n) i-=n;
return i;
}
long long solve(int N,int K,int *A,int *B){
int s1=A[0]-B[0];
n=N;
k=K;
g[1]=B[0];
g[0]=0;
for(i=2;i<=n;i++){
s1+=A[i-1]-B[i-1];
g[i]=g[i-1]+B[i-1];
}
if(n==k) return max(s1,0);
//memset(d,~0,sizeof d);
d[0][1]=-1000000000000000000ll;
d[0][0]=A[0]-g[k];
h[n-1]=A[n-1];//cout<<d[0][0]<<" "<<d[0][1]<<endl;
d[n-1][0]=0;
for(i=n-2;i>=n-k;i--) h[i]=h[i+1]+A[i];
for(i=1;i<n;i++){
if(i+k-1>=n-1) {
d[n-1][0]=max(d[n-1][0],max(d[max(i-k-1,0ll)][0],d[max(i-k-1,0ll)][1])+h[i]-get(max(i,k),n-1));
d[i][0]=0;
d[i][1]=max(d[i-1][1],d[i-1][0]);
continue;
}
d[i][0]=d[i-1][0]+A[i]-B[gi(i+k-1)];
if(d[i-1][1]!=-1 && d[i-1][1]+A[i]-get(i,gi(i+k-1))>d[i][0]) {
d[i][0]=d[i-1][1]+A[i]-get(i,gi(i+k-1));
}
d[i][1]=max(d[i-1][1],d[i-1][0]);
}
d[n-1][0]=max(d[n-1][0],d[n-k-1][0]+h[n-k]-get(n-1,n-1));//cout<<d[n-1][0]<<" "<<d[n-1][1]<<" "<<d[0][0]<<" "<<h[n-k]<<" "<<get(max(i,k),n-1)<<endl;
f[0][0]=-1000000000000000000ll;
f[0][1]=0;
for(i=1;i<n;i++){
f[i][0]=f[i-1][0]+A[i]-B[gi(i+k-1)];
//cout<<i<<"* "<<-get(i,gi(i+k-1))<<endl;
if(f[i-1][1]+A[i]-get(i,gi(i+k-1))>f[i][0]) {
f[i][0]=f[i-1][1]+A[i]-get(i,gi(i+k-1));
}
f[i][1]=max(f[i-1][1],f[i-1][0]);//cout<<f[i][0]<<" "<<f[i][1]<<endl;
}
return (long long)(max(max(d[n-1][0],d[n-1][1]),max(f[n-1][0],f[n-1][1])));
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
484 KB |
Output is correct |
3 |
Incorrect |
3 ms |
560 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
484 KB |
Output is correct |
3 |
Incorrect |
3 ms |
560 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
23988 KB |
Output is correct |
2 |
Correct |
5 ms |
23988 KB |
Output is correct |
3 |
Correct |
238 ms |
94468 KB |
Output is correct |
4 |
Incorrect |
4 ms |
94468 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
484 KB |
Output is correct |
3 |
Incorrect |
3 ms |
560 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |