#include<cstdio> #include<queue> #include<cstring> using namespace std; const int maxn=5e5+10; typedef long long ll; ll a[maxn],b[maxn],sum[maxn],f[maxn]; struct Node{ int id; ll w; Node(){}; Node(int x,ll y){ id=x; w=y; } bool operator<(const Node &a)const{ return w>a.w; } }; priority_queue<Node> q1; priority_queue<Node> q2; int main(){ freopen("empire.in","r",stdin); freopen("empire.out","w",stdout); int n,k; scanf("%d%d",&n,&k); for(int i=1;i<=n;++i) scanf("%lld",&a[i]); for(int i=0;i<n;++i) scanf("%lld",&b[i]); for(int i=1;i<=n;++i) sum[i]=sum[i-1]+a[i]; memset(f,0x3f,sizeof(f)); f[0]=0; q1.push(Node(0,f[0]+b[0])); ll s; for(int i=1;i<=n;++i){ while(!q1.empty()&&i-q1.top().id>k) q1.pop(); while(!q1.empty()&&q1.top().w<(s=f[q1.top().id]+sum[i]-sum[q1.top().id])){ q2.push(Node(q1.top().id,s-sum[i])); q1.pop(); } while(!q2.empty()&&i-q2.top().id>k) q2.pop(); if(!q1.empty()) f[i]=min(f[i],q1.top().w); if(!q2.empty()) f[i]=min(f[i],q2.top().w+sum[i]); q1.push(Node(i,f[i]+b[i])); } printf("%lld ",f[n]); return 0; }