#include <bits/stdc++.h>
using namespace std;
#define dbgv(v) cout<<#v<<" = "; f(i,0,v.size()) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<(x.F)<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define Add(x,y) x=(x+y)%mod
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first
#define int ll
const int N=1e6+99,inf=1e17;
int n,k,ans,a[N],b[N],c[N],d[N],dp[N],pd[N],ps[N];
void build(int s){
if(s==0){
f(i,1,n+1) a[i]=c[i];
f(i,1,n+1) b[i]=d[i];
f(i,n+1,n+n+1) b[i]=0;
a[1]+=inf;
}
else{
f(i,1,n+1) a[i]=c[i];
f(i,1,n+1) b[i]=d[i],b[i+n]=d[i];
a[1]-=inf;
}
}
int slv(int s){
dp[0]=0;
pd[0]=-inf;
f(i,1,2*n+1) ps[i]=ps[i-1]+b[i];
//dbga(a,1,n+1);
//dbga(b,1,n+n+1);
f(i,1,n+1){
pd[i]=max(a[i]-(ps[i+k-1]-ps[i-1]),pd[i-1]+a[i]-b[i+k-1]);
dp[i]=max(dp[i-1],pd[i]);
//cout<<i<<" -> "<<dp[i]<<" "<<pd[i]<<endl;
}
if(s==0){
return dp[n]-inf;
}
else{
return dp[n];
}
}
long long solve(int N,int K,int *A,int *B){
ans=0;
n=N,k=K;
f(i,1,n+1) c[i]=A[i-1],d[i]=B[i-1];
build(0);
maxm(ans,slv(0));
build(1);
maxm(ans,slv(1));
return ans;
}
/*
main(){
ios:: sync_with_stdio(0), cin.tie(0), cout.tie(0);
vector<int> A,B;
cin>>n>>k;
f(i,0,n){
int x;
cin>>x;
A.pb(x);
}
f(i,0,n){
int x;
cin>>x;
B.pb(x);
}
cout<<solve(n,k,A,B);
}
*/
/*
3 2
40 80 100
140 0 20
*/
Compilation message
/usr/bin/ld: /tmp/cc3djPlp.o: in function `main':
grader.cpp:(.text.startup+0xdf): undefined reference to `solve(int, int, int*, int*)'
collect2: error: ld returned 1 exit status