#include<cstdio>
#include<ctime>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
#define rg register
#define il inline
#define vd void
#define ll long long
#define N 100010
#define For(i,x,y) for (rg int i=(x);i<=(y);i++)
#define Dow(i,x,y) for (rg int i=(x);i>=(y);i--)
#define cross(i,k) for (rg int i=first[k];i;i=last[i])
using namespace std;
il ll max(ll x,ll y){return x>y?x:y;}
il ll min(ll x,ll y){return x<y?x:y;}
il ll read(){
    ll x=0;int ch=getchar(),f=1;
    while (!isdigit(ch)&&(ch!='-')&&(ch!=EOF)) ch=getchar();
    if (ch=='-'){f=-1;ch=getchar();}
    while (isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
    return x*f;
}
int n,m;
ll a[N],sum[N],dp[N];

int f[N][20];
il vd init(){
	int l=log(n)/log(2);
	For(j,1,l)
		For(i,1,n-(1<<j)+1)
			f[i][j]=a[f[i][j-1]]<a[f[i+(1<<j-1)][j-1]]?f[i][j-1]:f[i+(1<<j-1)][j-1];
}
il int Query(int l,int r){
	int Log=log(r-l+1)/log(2);
	return a[f[l][Log]]<a[f[r-(1<<Log)+1][Log]]?f[l][Log]:f[r-(1<<Log)+1][Log];
}

int main(){
	n=read(),m=read();
	For(i,1,n) a[i]=read(),sum[i]=sum[i-1]+a[i],f[i][0]=i;
	init();
	For(i,1,m-1) dp[i]=sum[i];
	For(i,m,n)
		dp[i]=min(dp[i-1]+a[i],dp[i-m]+sum[i]-sum[i-m]-a[Query(i-m+1,i)]);
	//For(i,1,n) printf("%lld ",dp[i]);
	printf("%lld",dp[n]);
}


//#define more
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<queue>
#ifndef more
using std::sort;
using std::pair;
#endif
#define _ll(x) scanf("%I64d",&x)
#define _pll(x) printf("%I64d\n",x)
#define __ll(x) printf("%I64d ",x)
#ifdef more
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<vector>
#include<bitset>
#include<iostream>
#include<iomanip>
#include<sstream>
#include<string>
#include<stack>
#include<list>
#include<set>
#include<map>
#define pb push_back
using namespace std;
#endif
#define emp empty()
#define mp make_pair
#define x first
#define y second
#define priq std::priority_queue
#define priq1(typ) std::priority_queue<typ,vector<typ>,greater<typ> >
#define ll long long
#define c0(x) memset(x,0,sizeof(x))
#define cl(x,a) memset(x,a,sizeof(x))
#define _(x) scanf("%d",&x)
#define _2(x,y) scanf("%d %d",&x,&y)
#define _3(x,y,z) scanf("%d %d %d",&x,&y,&z)
#define _4(x,y,z,w) scanf("%d %d %d %d",&x,&y,&z,&w)
#define _5(x,y,z,w,t) scanf("%d %d %d %d &d",&x,&y,&z,&w,&t)
#define _f(x) scanf("%f",&x)
#define _c(x) scanf(" %c",&x)
#define _c2(x,y) scanf(" %c %c",&x,&y)
#define _s(x) scanf("%s",x);
#define _p(x) printf("%d\n",x)
#define __p(x) printf("%d ",x)
#define lb(x) ((x)&-(x))
#define bt(x) ((x)&(1<<(x)))
#define __ printf
#define gtc getchar()
#define ptc(x) putchar(x)
#define BW 1000000
#define KW 10000000
#define EE 100000000
#define GG 1000000000
#define intma 2147483647
#define llma 1010000000000000000ll
#define rint register int
#define rt return
#define uint unsigned int
#define dou double
#define con continue
#define eps 0.0000000001
#define fo(i,n) for(i=1;i<=n;i++)
#define f9 for(;;)
#define f0(i,n) for(i=l;i<n;i++)
#define f01(i,l,r) for(i=l;i<=r;i++)
#define fo0(i,k) for(i=fr[k];i;i=nx[i])
#define fo1(i,l,r) for(i=l;i<=r;i++)
#define fo2(i,l,r) for(i=l;i>=r;i--)
#define opi(x) freopen(x,"r",stdin)
#define opo(x) freopen(x,"w",stdout)
#define _a1(i,n,a) for(i=1;i<=n;i++)scanf("%d",&a[i]);
#define _a2(i,n,a,b) for(i=1;i<=n;i++)scanf("%d %d",&a[i],&b[i]);
#define _a3(i,n,a,b,c) for(i=1;i<=n;i++)scanf("%d %d %d",&a[i],&b[i],&c[i]);
#define tpl template<typename T>
#define N 533333
int eq(dou a,dou b){return a<b+eps&&a>b-eps;}
int max(int a,int b){return a>b?a:b;}int min(int a,int b){return a<b?a:b;}
void _ma(int &a,int b){if(a<b){a=b;}}void _mi(int &a,int b){if(a>b){a=b;}}
int abs(int a){return a<0?-a:a;}void swap(int &a,int &b){int t=a;a=b;b=t;}
dou max(dou a,dou b){return a>b?a:b;}dou min(dou a,dou b){return a<b?a:b;}
void _ma(dou &a,dou b){if(a<b){a=b;}}void _mi(dou &a,dou b){if(a>b){a=b;}}
dou abs(dou a){return a<0?-a:a;}void swap(dou &a,dou &b){int t=a;a=b;b=t;}
ll max(ll a,ll b){return a>b?a:b;}ll min(ll a,ll b){return a<b?a:b;}
void _ma(ll &a,ll b){if(a<b){a=b;}}void _mi(ll &a,ll b){if(a>b){a=b;}}
ll abs(ll a){return a<0?-a:a;}void swap(ll &a,ll &b){int t=a;a=b;b=t;}
int n,m,j2,l[N],r[N],mi[N],l1,r1;
ll sm,s1,an;
void mt(int k,int l1,int r1)
{
	l[k]=l1;
	r[k]=r1;
	if(l1==r1)
	{
		_(mi[k]);
		sm+=mi[k];
	}
	else
	{
		mt(k*2,l1,(l1+r1)/2);
		mt(k*2+1,(l1+r1)/2+1,r1);
		mi[k]=min(mi[k*2],mi[k*2+1]);
	}
}
int qmi(int k)
{
	if(l[k]>r1||r[k]<l1)
	{
		return 1000000100;
	}
	if(l[k]>=l1&&r[k]<=r1)
	{
		return mi[k];
	}
	return min(qmi(k*2),qmi(k*2+1));
}
int main()
{
	int i,j,n1;
	_2(n,m);
	n1=n-m+1;
	mt(1,1,n);
	fo(i,m)
	{
		s1=0;
		for(j=i;j<=n1;j+=m)
		{
			l1=j;
			r1=j+m-1;
			s1+=qmi(1);
		}
		_ma(an,s1);
	}
	_pll(sm-an);
}


//#define more
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<queue>
#ifndef more
using std::sort;
using std::pair;
#endif
#define _ll(x) scanf("%I64d",&x)
#define _pll(x) printf("%I64d\n",x)
#define __ll(x) printf("%I64d ",x)
#ifdef more
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<vector>
#include<bitset>
#include<iostream>
#include<iomanip>
#include<sstream>
#include<string>
#include<stack>
#include<list>
#include<set>
#include<map>
#define pb push_back
using namespace std;
#endif
#define emp empty()
#define mp make_pair
#define x first
#define y second
#define priq std::priority_queue
#define priq1(typ) std::priority_queue<typ,vector<typ>,greater<typ> >
#define ll long long
#define c0(x) memset(x,0,sizeof(x))
#define cl(x,a) memset(x,a,sizeof(x))
#define _(x) scanf("%d",&x)
#define _2(x,y) scanf("%d %d",&x,&y)
#define _3(x,y,z) scanf("%d %d %d",&x,&y,&z)
#define _4(x,y,z,w) scanf("%d %d %d %d",&x,&y,&z,&w)
#define _5(x,y,z,w,t) scanf("%d %d %d %d &d",&x,&y,&z,&w,&t)
#define _f(x) scanf("%f",&x)
#define _c(x) scanf(" %c",&x)
#define _c2(x,y) scanf(" %c %c",&x,&y)
#define _s(x) scanf("%s",x);
#define _p(x) printf("%d\n",x)
#define __p(x) printf("%d ",x)
#define lb(x) ((x)&-(x))
#define bt(x) ((x)&(1<<(x)))
#define __ printf
#define gtc getchar()
#define ptc(x) putchar(x)
#define BW 1000000
#define KW 10000000
#define EE 100000000
#define GG 1000000000
#define intma 2147483647
#define llma 1010000000000000000ll
#define rint register int
#define rt return
#define uint unsigned int
#define dou double
#define con continue
#define eps 0.0000000001
#define fo(i,n) for(i=1;i<=n;i++)
#define f9 for(;;)
#define f0(i,n) for(i=l;i<n;i++)
#define f01(i,l,r) for(i=l;i<=r;i++)
#define fo0(i,k) for(i=fr[k];i;i=nx[i])
#define fo1(i,l,r) for(i=l;i<=r;i++)
#define fo2(i,l,r) for(i=l;i>=r;i--)
#define opi(x) freopen(x,"r",stdin)
#define opo(x) freopen(x,"w",stdout)
#define _a1(i,n,a) for(i=1;i<=n;i++)scanf("%d",&a[i]);
#define _a2(i,n,a,b) for(i=1;i<=n;i++)scanf("%d %d",&a[i],&b[i]);
#define _a3(i,n,a,b,c) for(i=1;i<=n;i++)scanf("%d %d %d",&a[i],&b[i],&c[i]);
#define tpl template<typename T>
#define N 533333
int eq(dou a,dou b){return a<b+eps&&a>b-eps;}
int max(int a,int b){return a>b?a:b;}int min(int a,int b){return a<b?a:b;}
void _ma(int &a,int b){if(a<b){a=b;}}void _mi(int &a,int b){if(a>b){a=b;}}
int abs(int a){return a<0?-a:a;}void swap(int &a,int &b){int t=a;a=b;b=t;}
dou max(dou a,dou b){return a>b?a:b;}dou min(dou a,dou b){return a<b?a:b;}
void _ma(dou &a,dou b){if(a<b){a=b;}}void _mi(dou &a,dou b){if(a>b){a=b;}}
dou abs(dou a){return a<0?-a:a;}void swap(dou &a,dou &b){int t=a;a=b;b=t;}
ll max(ll a,ll b){return a>b?a:b;}ll min(ll a,ll b){return a<b?a:b;}
void _ma(ll &a,ll b){if(a<b){a=b;}}void _mi(ll &a,ll b){if(a>b){a=b;}}
ll abs(ll a){return a<0?-a:a;}void swap(ll &a,ll &b){int t=a;a=b;b=t;}
int n,m,j2,l[N],r[N],mi[N],g[N],l1,r1;
ll f[N];
ll sm,s1,an;
void mt(int k,int l1,int r1)
{
	l[k]=l1;
	r[k]=r1;
	if(l1==r1)
	{
		_(mi[k]);
		sm+=mi[k];
	}
	else
	{
		mt(k*2,l1,(l1+r1)/2);
		mt(k*2+1,(l1+r1)/2+1,r1);
		mi[k]=min(mi[k*2],mi[k*2+1]);
	}
}
int qmi(int k)
{
	if(l[k]>r1||r[k]<l1)
	{
		return 1000000100;
	}
	if(l[k]>=l1&&r[k]<=r1)
	{
		return mi[k];
	}
	return min(qmi(k*2),qmi(k*2+1));
}
int main()
{
	int i,j,n1;
	_2(n,m);
	n1=n-m+1;
	mt(1,1,n);
	fo1(i,m,n)
	{
		l1=i-m+1;
		r1=i;
		g[i]=qmi(1);
	}
	fo1(i,m,n)
	{
		f[i]=f[i-1];
		_ma(f[i],f[i-m]+g[i]);
	}
	_pll(sm-f[n]);
}


#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long ll;
ll ans=0,f[100005];
int n,c,a[100005],g[100005];
struct node{int v,i;}p[1000005];int l=1,r=0;
void In(int v,int i)
{
    while(r>=l&&v<p[r].v)r--;\
    p[++r]=(node){v,i};
}
int main()
{
    int i,j;
    scanf("%d%d",&n,&c);
    for(i=1;i<=n;i++)scanf("%d",&a[i]),ans+=a[i];
    for(i=1;i<c;i++)In(a[i],i);
    for(i=c;i<=n;i++)
    {
        In(a[i],i);
        while(l<=r&&p[l].i<=i-c)l++;
        g[i]=p[l].v;
    }
    ll mx=0;
    for(i=0;i<c;i++)f[i]=0;
    for(i=c;i<=n;i++)
    {
        f[i]=max(f[i-1],f[i-c]+g[i]);
        mx=max(mx,f[i]);
    }
    printf("%I64d",ans-mx);
    return 0;
}


#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
ll a[100100],dp[100100],b[100100];
ll n, m;
ll func(ll x)
{
	if (x > n - m + 1)return 0;
	ll ret = dp[x];
	if (ret)return ret;
	ret = max(ret,func(x + 1));
	ret = max(ret, func(x + m ) + b[x]);
	return ret;
}
int main()
{
	ll sum = 0;
	scanf("%lld %lld", &n, &m);
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &a[i]);
		sum += a[i];
	}
	for (int i = 0; i < n; i++)
	{
		if (i + m-1 >= n)continue;
		ll gap = 9876543210;
		for (int y = 0; y < m; y++)
		{
			gap = min(gap, a[i + y]);
		}
		b[i] = gap;
	}
	printf("%lld", sum - func(0));
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
ll a[100100],dp[100100],b[100100],seg[400100];
ll n, m;
ll update(ll lo, ll val, ll node, ll x, ll y)
{
	if (lo > y || lo < x)return seg[node];
	if (x == y)return seg[node] = val;
	ll mid = (x + y) / 2;
	return seg[node] = min(update(lo, val, node * 2, x, mid), update(lo, val, node * 2 + 1, mid + 1, y));
}
ll query(ll lo, ll hi, ll node, ll x, ll y)
{
	if (y < lo || hi < x)return 99999999999999;
	if (lo <= x&&y <= hi)return seg[node];
	ll mid = (x + y) / 2;
	return min(query(lo, hi, node * 2, x, mid), query(lo, hi, node * 2 + 1, mid + 1, y));
}
ll func(ll x)
{
	if (x ==n+1)return 0;
	if (x > n + 1)return -9999999999;
	ll &ret = dp[x];
	if (ret!=-1)return ret;
	ret = max(ret,func(x + 1));
	ret = max(ret, func(x + m) + query(x, x + m - 1, 1, 1, n));
	return ret;
}
int main()
{
	memset(dp, -1, sizeof(dp));
	ll sum = 0;
	scanf("%lld %lld", &n, &m);
	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &a[i]);
		update(i, a[i], 1, 1, n);
		sum += a[i];
	}
	printf("%lld", sum - func(1));
}


/*input
8 2
1 3 4 5 5 3 4 1

*/
#include <iostream>
#include <bits/stdc++.h>

using namespace std;

long long int arr[112345],dp[112345]={0}, su=0, ans = 0;
long long int pow(long long int a, long long int b, long long int mod){
	if(b==1)return a;
	if(b==0)return 1;
	long long int ret = pow(a, b/2 ,mod);
	ret = ret * ret;
	ret %= mod;
	if(b&1){
		ret=ret*a;
		ret %= mod;
	}
	return ret;
}

multiset<long long int> mys;
int main() {
	int n,c;
	cin>>n>>c;
	for(int i=0;i<n;i++){
		scanf("%lld", &arr[i]);
		su += arr[i];
	}
	//cout<<su<<endl;
	if(c==1)cout<<0<<endl;
	else if(c>n)cout<<su<<endl;
	else{
		for(int i=0;i<c;i++){
			mys.insert(arr[i]);
		}
		dp[c-1] = *(mys.cbegin());
		for(int i=c;i<n;i++){
			mys.erase(arr[i-c]);
			mys.insert(arr[i]);
			dp[i] = max( dp[i-1], dp[i-c] + *(mys.cbegin()) );
		}
		cout<<su-dp[n-1]<<endl;
	}

}


/*input
8 2
1 1 1 1 1 1 1 1

*/
#include <iostream>
#include <bits/stdc++.h>

using namespace std;

long long int arr[112345],dp[112345]={0}, su=0, ans = 0;
long long int pow(long long int a, long long int b, long long int mod){
	if(b==1)return a;
	if(b==0)return 1;
	long long int ret = pow(a, b/2 ,mod);
	ret = ret * ret;
	ret %= mod;
	if(b&1){
		ret=ret*a;
		ret %= mod;
	}
	return ret;
}

multiset<long long int> mys;
int main() {
	int n,c;
	cin>>n>>c;
	for(int i=0;i<n;i++){
		scanf("%lld", &arr[i]);
		su += arr[i];
	}
	//cout<<su<<endl;
	if(c==1)cout<<0<<endl;
	else if(c>n)cout<<su<<endl;
	else{
		for(int i=0;i<c;i++){
			mys.insert(arr[i]);
			dp[i] = 0;
		}
		dp[c-1] = *(mys.begin());
		for(int i=c;i<n;i++){
			mys.erase(mys.find(arr[i-c]));
			mys.insert(arr[i]);
			dp[i] = max( dp[i-1], dp[i-c] + *(mys.begin()) );
		}
		cout<<su-dp[n-1]<<endl;
	}

}


/*input
8 2
1 3 4 5 5 3 4 1

*/
#include <iostream>
#include <bits/stdc++.h>

using namespace std;

long long int arr[112345],dp[112345]={0}, su=0, ans = 0;
long long int pow(long long int a, long long int b, long long int mod){
	if(b==1)return a;
	if(b==0)return 1;
	long long int ret = pow(a, b/2 ,mod);
	ret = ret * ret;
	ret %= mod;
	if(b&1){
		ret=ret*a;
		ret %= mod;
	}
	return ret;
}

multiset<long long int> mys;
int main() {
	int n,c;
	cin>>n>>c;
	for(int i=0;i<n;i++){
		scanf("%lld", &arr[i]);
		su += arr[i];
	}
	//cout<<su<<endl;
	if(c==1)cout<<0<<endl;
	else if(c>n)cout<<su<<endl;
	else{
		for(int i=0;i<c;i++){
			mys.insert(arr[i]);
		}
		dp[c-1] = *(mys.begin());
		for(int i=c;i<n;i++){
			mys.erase(arr[i-c]);
			mys.insert(arr[i]);
			dp[i] = max( dp[i-1], dp[i-c] + *(mys.begin()) );
		}
		cout<<su-dp[n-1]<<endl;
	}

}


#include <cstdio>
#include <utility>
#include <vector>
#include <queue>
#define ll long long
#define MAX 100005
#define INF 0x7FFFFFFF
using namespace std;
ll n,c,buf[MAX];

ll tot,_max;
priority_queue< pair<ll,int>,vector< pair<ll,int> >,greater< pair<ll,int> > > _min[MAX];
int main(){
	scanf("%lld %lld",&n,&c);
	for (int i=0;i<n;++i){
		scanf("%lld",&buf[i]);
		tot+=buf[i];
		_min[i/c].push(make_pair(buf[i],i));//
	}
	for (int i=0;i<c;++i){
		ll sum=0;
		for (int j=0;j<(n-i)/c;++j){
			while (_min[j].top().second<j*c+i) _min[j].pop();
			sum+=_min[j].top().first;
			_min[j].push(make_pair(buf[(j+1)*c+i],(j+1)*c+i));
		}
		if (sum>_max) _max=sum;
	}
	printf("%lld\n",tot-_max);
	return 0;
}


#include <cstdio>
#include <vector>
#include <queue>
#define ll long long
#define MAX 100005
#define INF 0x7FFFFFFF
using namespace std;
ll n,c,buf[MAX];

ll tot,_max;
priority_queue< ll,vector<ll>,greater<ll> > _min[MAX];
int main(){
	scanf("%lld %lld",&n,&c);
	for (int i=0;i<n;++i){
		scanf("%lld",&buf[i]);
		tot+=buf[i];
		_min[i/c].push(buf[i]);
	}
	for (int i=0;i<c;++i){
		ll sum=0;
		for (int j=0;j<(n-i)/c;++j){
			sum+=_min[j].top();
			_min[j].push(buf[(j+1)*c+i]);
			if (buf[j*c+i]==_min[j].top()) _min[j].pop();
		}
		if (sum>_max) _max=sum;
	}
	printf("%lld\n",tot-_max);
	return 0;
}


#include <cstdio>
#define MAX 100005
#define INF 0x7FFFFFFF
int n,c,buf[MAX];

int tot,min[MAX],max;
int main(){
	scanf("%d %d",&n,&c);
	for (int i=0;i<n;++i){
		scanf("%d",&buf[i]);
		tot+=buf[i];
		if (!min[i/c] || buf[i]<min[i/c]) min[i/c]=buf[i];
	}
	for (int i=0;i<c;++i){
		int sum=0;
		for (int j=0;j<(n-i)/c;++j){
			sum+=min[j];
			if (buf[(j+1)*c+i]<min[j]) min[j]=buf[(j+1)*c+i];
			else if (buf[j*c+i]==min[j]){
				int tmp=INF;
				for (int k=j*c+i+1;k<(j+1)*c+i+1;++k) if (buf[k]<tmp) tmp=buf[k];
				min[j]=tmp;
			}
		}
		if (sum>max) max=sum;
	}
	printf("%d\n",tot-max);
	return 0;
}


#include <cstdio>
#include <utility>
#include <vector>
#include <queue>
#define ll long long
#define MAX 100005
#define INF 0x7FFFFFFF
using namespace std;
ll n,c,buf[MAX];

ll pre[MAX],dp[MAX];
priority_queue< pair<ll,int>,vector< pair<ll,int> >,greater< pair<ll,int> > > _min;
int main(){
	scanf("%lld %lld",&n,&c);
	for (int i=1;i<=n;++i){
		scanf("%lld",&buf[i]);
		pre[i]=pre[i-1]+buf[i];
	}
	for (int i=1;i<=n;++i){
		dp[i]=dp[i-1]+buf[i];
		_min.push(make_pair(buf[i],i));
		while (_min.top().second<=i-c) _min.pop();
		if (i>=c && dp[i-c]+pre[i]-pre[i-c]-_min.top().first<dp[i]) dp[i]=dp[i-c]+pre[i]-pre[i-c]-_min.top().first;
	}
	printf("%lld\n",dp[n]);
	return 0;
}


#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
#define read(x) scanf("%I64d",&x)
template<class T>
class RMQ{
    public:
     T* a;
     int t;
     T **mn;
     T maxn;
     int *log2;
     void SetMaxn(T *maxn){
        this->maxn=*maxn;
     }
     void SetMaxn(T maxn){
        this->maxn=maxn;
     }
     void Creat(T a[],int maxn){//建立一个最大为maxn的RMQ处理类
          int k=1,p=0;
          log2=new int[maxn+10];
          for(int i=0;i<=maxn;i++)
              log2[i]=(i==0?-1:log2[i>>1]+1);
          while(k<maxn){
             k*=2;
            p+=1;
        }
        t=p;
        mn=new T*[maxn+10];
        for(int i=0;i<maxn;++i){
            mn[i] = new T[t+1];
            mn[i][0]=a[i];
            for(int j=1;j<=t;++j)
                mn[i][j]=maxn;
        }
         for(int j=1;j<=t;++j)
             for(int i=0;i+(1<<j) <= maxn;++i){
                     T sa=mn[i][j-1];
                    T sb=mn[i+(1<<(j-1))][j-1];
                     if(sa<sb)
                         mn[i][j]=sa;
                     else
                         mn[i][j]=sb;
             }
     }
    T Getx(int ql,int qr){
            int k=log2[qr-ql+1];
            return min(mn[ql][k],mn[qr-(1<<k)+1][k]);
    }
    T Getw(int ql,int qr){
            --ql,--qr;
            int k=log2[qr-ql+1];
            return min(mn[ql][k],mn[qr-(1<<k)+1][k]);
    }
} ;
typedef long long LL;
RMQ<LL> rr;
LL n,c;
LL a[100100];
LL f[100100];
LL s[100100];
int main(){
    rr.SetMaxn(0x7f7f7f7f);
    s[0]=0;
    memset(f,-1LL,sizeof(f));
    read(n);
    read(c);
    for(int i=1;i<=n;i++){
    	read(a[i]);
		s[i] = s[i-1] + a[i];
	}
    a[0]=0x7f7f7f7f;
    f[0]=0;
    rr.Creat(a,n+1);
    for(int i=1;i<=n;i++){
    	f[i] = f[i-1] + a[i];
    	LL zans = 0x7f7f7f7f;
		if(i-c>=0)
			zans = f[i-c] + s[i] - s[i-c] - rr.Getx(i-c+1,i);
    	if (f[i]>zans)
    		f[i] = zans;
    //	cout<<i<<" "<<f[i]<<endl;
	}
	printf("%I64d",f[n]);
    return 0;
}


#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
#define read(x) scanf("%I64d",&x)
template<class T>
class RMQ{
    public:
     T* a;
     int t;
     T **mn;
     T maxn;
     int *log2;
     void SetMaxn(T *maxn){
        this->maxn=*maxn;
     }
     void SetMaxn(T maxn){
        this->maxn=maxn;
     }
     void Creat(T a[],int maxn){//建立一个最大为maxn的RMQ处理类
          int k=1,p=0;
          log2=new int[maxn+10];
          for(int i=0;i<=maxn;i++)
              log2[i]=(i==0?-1:log2[i>>1]+1);
          while(k<maxn){
             k*=2;
            p+=1;
        }
        t=p;
        mn=new T*[maxn+10];
        for(int i=0;i<maxn;++i){
            mn[i] = new T[t+1];
            mn[i][0]=a[i];
            for(int j=1;j<=t;++j)
                mn[i][j]=maxn;
        }
         for(int j=1;j<=t;++j)
             for(int i=0;i+(1<<j) <= maxn;++i){
                     T sa=mn[i][j-1];
                    T sb=mn[i+(1<<(j-1))][j-1];
                     if(sa<sb)
                         mn[i][j]=sa;
                     else
                         mn[i][j]=sb;
             }
     }
    T Getx(int ql,int qr){
            int k=log2[qr-ql+1];
            return min(mn[ql][k],mn[qr-(1<<k)+1][k]);
    }
    T Getw(int ql,int qr){
            --ql,--qr;
            int k=log2[qr-ql+1];
            return min(mn[ql][k],mn[qr-(1<<k)+1][k]);
    }
} ;
typedef long long LL;
RMQ<LL> rr;
LL n,c;
LL a[100100];
LL f[100100];
LL s[100100];
int main(){
    rr.SetMaxn(1e15);
    s[0]=0;
    memset(f,-1LL,sizeof(f));
    read(n);
    read(c);
    for(int i=1;i<=n;i++){
    	read(a[i]);
		s[i] = s[i-1] + a[i];
	}
    a[0]=1e15;
    f[0]=0;
    rr.Creat(a,n+1);
    for(int i=1;i<=n;i++){
    	f[i] = f[i-1] + a[i];
    	LL zans = 1e15;
		if(i-c>=0)
			zans = f[i-c] + s[i] - s[i-c] - rr.Getx(i-c+1,i);
    	if (f[i]>zans)
    		f[i] = zans;
    //	cout<<i<<" "<<f[i]<<endl;
	}
	printf("%I64d",f[n]);
    return 0;
}


#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
#define read(x) scanf("%I64d",&x)
template<class T>
class RMQ{
    public:
     T* a;
     int t;
     T **mn;
     T maxn;
     int *log2;
     void SetMaxn(T *maxn){
        this->maxn=*maxn;
     }
     void SetMaxn(T maxn){
        this->maxn=maxn;
     }
     void Creat(T a[],int maxn){//建立一个最大为maxn的RMQ处理类
          int k=1,p=0;
          log2=new int[maxn+10];
          for(int i=0;i<=maxn;i++)
              log2[i]=(i==0?-1:log2[i>>1]+1);
          while(k<maxn){
             k*=2;
            p+=1;
        }
        t=p;
        mn=new T*[maxn+10];
        for(int i=0;i<maxn;++i){
            mn[i] = new T[t+1];
            mn[i][0]=a[i];
            for(int j=1;j<=t;++j)
                mn[i][j]=maxn;
        }
         for(int j=1;j<=t;++j)
             for(int i=0;i+(1<<j) <= maxn;++i){
                     T sa=mn[i][j-1];
                    T sb=mn[i+(1<<(j-1))][j-1];
                     if(sa<sb)
                         mn[i][j]=sa;
                     else
                         mn[i][j]=sb;
             }
     }
    T Getx(int ql,int qr){
            int k=log2[qr-ql+1];
            return min(mn[ql][k],mn[qr-(1<<k)+1][k]);
    }
    T Getw(int ql,int qr){
            --ql,--qr;
            int k=log2[qr-ql+1];
            return min(mn[ql][k],mn[qr-(1<<k)+1][k]);
    }
} ;
typedef long long LL;
RMQ<LL> rr;
LL n,c;
LL a[100100];
LL f[100100];
LL s[100100];
int main(){
    rr.SetMaxn(0x7f7f7f7f);
    s[0]=0;
    memset(f,-1LL,sizeof(f));
    read(n);
    read(c);
    for(int i=1;i<=n;i++){
    	read(a[i]);
		s[i] = s[i-1] + a[i];
	}
    a[0]=0x7f7f7f7f;
    f[0]=0;
    rr.Creat(a,n);
    for(int i=1;i<=n;i++){
    	f[i] = f[i-1] + a[i];
    	LL zans = 0x7f7f7f7f;
		if(i-c>=0)
			zans = f[i-c] + s[i] - s[i-c] - rr.Getx(i-c+1,i);
    	if (f[i]>zans)
    		f[i] = zans;
    //	cout<<i<<" "<<f[i]<<endl;
	}
	printf("%I64d",f[n]);
    return 0;
}


#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<set>
using namespace std;
typedef long long LL;
const int N=100005;
multiset<int> s;
set<int> :: iterator it;
int n,c;
LL f[N];
int a[N];
int main()
{
	scanf("%d%d",&n,&c);
	LL sum=0;
	for (int u=1;u<=n;u++)
	{
		scanf("%d",&a[u]);
		sum=sum+a[u];
	}
	for (int u=1;u<=n;u++)
	{
		f[u]=f[u-1];s.insert(a[u]);
		if (u>=c)
		{
			f[u]=max(f[u],f[u-c]+(*s.begin()));
			int tt=a[u-c+1];
			it=s.find(tt);
			s.erase(it);
		}
	}
	printf("%I64d\n",sum-f[n]);
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<set>
using namespace std;
const int N=100005;
multiset<int> s;
set<int> :: iterator it;
int n,c;
int f[N];
int a[N];
int main()
{
	scanf("%d%d",&n,&c);
	int sum=0;
	for (int u=1;u<=n;u++)
	{
		scanf("%d",&a[u]);
		sum=sum+a[u];
	}
	for (int u=1;u<=n;u++)
	{
		f[u]=f[u-1];s.insert(a[u]);
		if (u>=c)
		{
			f[u]=max(f[u],f[u-c]+(*s.begin()));
			int tt=a[u-c+1];
			it=s.find(tt);
			s.erase(it);
		}
	}
	printf("%d\n",sum-f[n]);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const long long sz=1<<17;
long long a[111111],n,c,dp[111111],sum;
long long atree[444444],dptree[444444];
void build(long long l,long long r,long long node)
{
	if (l==r)
	{
		if (l>n) atree[node]=1e18;
		else atree[node]=a[l];
		return;
	}
	long long mid=(l+r)/2;
	build(l,mid,node*2);
	build(mid+1,r,node*2+1);
	atree[node]=min(atree[node*2],atree[node*2+1]);
}
void update(long long x,long long p)
{
	long long pos=sz+x-1;
	dptree[pos]=p;pos/=2;
	while(pos)
	{
		dptree[pos]=max(dptree[pos*2],dptree[pos*2+1]);
		pos/=2;
	}
}
long long querya(long long beg,long long en,long long l,long long r,long long node)
{
	if (l>en || r<beg) return 1e18;
	if (l>=beg && r<=en) return atree[node];
	long long mid=(l+r)/2;
	return min(querya(beg,en,l,mid,node*2),querya(beg,en,mid+1,r,node*2+1));
}
long long querydp(long long beg,long long en,long long l,long long r,long long node)
{
	if (l>en || r<beg) return 0;
	if (l>=beg && r<=en) return dptree[node];
	long long mid=(l+r)/2;
	return max(querydp(beg,en,l,mid,node*2),querydp(beg,en,mid+1,r,node*2+1));
}
int main()
{
	scanf("%I64d%I64d",&n,&c);
	for (long long i=1;i<=n;i++)
	{
		scanf("%I64d",&a[i]);
		sum+=a[i];
	}
	if (c==1)
	{
		printf("0\n");
		return 0;
	}
	build(1,sz,1);
	for (long long i=c;i<=n;i++)
	{
		dp[i]=dp[i-c]+querya(i-c+1,i,1,sz,1);
		update(i,dp[i]);
	}
	printf("%I64d\n",sum-dp[n]);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const long long sz=1<<17;
long long a[111111],n,c,dp[111111],sum;
long long atree[444444],dptree[444444];
void build(long long l,long long r,long long node)
{
	if (l==r)
	{
		if (l>n) atree[node]=1e18;
		else atree[node]=a[l];
		return;
	}
	long long mid=(l+r)/2;
	build(l,mid,node*2);
	build(mid+1,r,node*2+1);
	atree[node]=min(atree[node*2],atree[node*2+1]);
}
void update(long long x,long long p)
{
	long long pos=sz+x-1;
	dptree[pos]=p;pos/=2;
	while(pos)
	{
		dptree[pos]=max(dptree[pos*2],dptree[pos*2+1]);
		pos/=2;
	}
}
long long querya(long long beg,long long en,long long l,long long r,long long node)
{
	if (l>en || r<beg) return 1e18;
	if (l>=beg && r<=en) return atree[node];
	long long mid=(l+r)/2;
	return min(querya(beg,en,l,mid,node*2),querya(beg,en,mid+1,r,node*2+1));
}
long long querydp(long long beg,long long en,long long l,long long r,long long node)
{
	if (l>en || r<beg) return 0;
	if (l>=beg && r<=en) return dptree[node];
	long long mid=(l+r)/2;
	return max(querydp(beg,en,l,mid,node*2),querydp(beg,en,mid+1,r,node*2+1));
}
int main()
{
	scanf("%I64d%I64d",&n,&c);
	for (long long i=1;i<=n;i++)
	{
		scanf("%I64d",&a[i]);
		sum+=a[i];
	}
	if (c==1)
	{
		printf("0\n");
		return 0;
	}
	build(1,sz,1);
	for (long long i=c;i<=n;i++)
	{
		dp[i]=max(querydp(i-c+1,i-1,1,sz,1),dp[i-c]+querya(i-c+1,i,1,sz,1));
		update(i,dp[i]);
	}
	printf("%I64d\n",sum-dp[n]);
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int n, c;
long long as[100001];
long long best_upto[100001];

int main() {
    ios::sync_with_stdio(false);

    cin >> n >> c;
    for(int i = 0; i < n; ++i) cin >> as[i];

    multiset<long long> cur_elements;
    long long cur_set_sum = 0;

    for(int i = 0; i < c - 1; ++i) {
        cur_elements.insert(as[i]);
        cur_set_sum += as[i];
        best_upto[i + 1] = best_upto[i] + as[i];
    }

    for(int i = c - 1; i < n; ++i) {
        cur_elements.insert(as[i]);
        cur_set_sum += as[i];
        long long lowest = *cur_elements.begin();
        long long req_sum = cur_set_sum - lowest;
        best_upto[i + 1] = min(best_upto[i] + as[i], best_upto[i + 1 - c] + req_sum);

        multiset<long long>::iterator iter = cur_elements.find(as[i - c + 1]);
        cur_set_sum -= *iter;
        cur_elements.erase(iter);
    }

    for(int i = 0; i < n; ++i)
        cout << "best upto " << i << " is " << best_upto[i] << endl;

    long long res = best_upto[n];
    cout << res << endl;

    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int n, c;
long long as[100001];
long long best_upto[100001];

int main() {
    ios::sync_with_stdio(false);

    cin >> n >> c;
    for(int i = 0; i < n; ++i) cin >> as[i];

    multiset<long long> cur_elements;
    long long cur_set_sum = 0;

    for(int i = 0; i < c - 1; ++i) {
        cur_elements.insert(as[i]);
        cur_set_sum += as[i];
        best_upto[i + 1] = best_upto[i] + as[i];
    }

    for(int i = c - 1; i < n; ++i) {
        cur_elements.insert(as[i]);
        cur_set_sum += as[i];
        long long lowest = *cur_elements.begin();
        long long req_sum = cur_set_sum - lowest;
        best_upto[i + 1] = min(best_upto[i] + as[i], best_upto[i + 1 - c] + req_sum);

        multiset<long long>::iterator iter = cur_elements.find(as[i - c + 1]);
        cur_set_sum -= *iter;
        cur_elements.erase(iter);
    }

    // for(int i = 0; i < n; ++i)
        // cout << "best upto " << i << " is " << best_upto[i] << endl;

    long long res = best_upto[n];
    cout << res << endl;

    return 0;
}


#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <map>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <stack>
#include <set>
#include <queue>
#include <assert.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int N = 1e5 + 7;
const int M = 3e5 + 5;
const LL mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
#define X first
#define Y second
#define pb push_back
#define mp  make_pair
#define SZ(X) (X.size())
int q[N];
LL val[N];
int head,tail;
LL dp[N];
int main() {
#ifdef local
    freopen("input", "r", stdin);
#endif // local
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n,c;
    cin>>n>>c;
    LL all = 0;
    for(int i = 1; i <= n; i++){
        cin>>val[i];
        all += val[i];
    }
    for(int i = 1; i <= n; i++){
        while(tail > head && val[i] <= val[q[tail - 1]])tail--;
        q[tail++] = i;
        while(head < tail && q[head] <= i - c)head++;
        dp[i] = dp[max(i - c,0)] + (i < c ? 0 : val[q[head]]);
        dp[i] = max(dp[i],dp[i - 1]);
    }
    cout<<all - dp[n]<<endl;
}


#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <map>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <stack>
#include <set>
#include <queue>
#include <assert.h>
#include <unordered_map>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int N = 1e5 + 7;
const int M = 3e5 + 5;
const LL mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
#define X first
#define Y second
#define pb push_back
#define mp  make_pair
#define SZ(X) (X.size())
int q[N];
LL val[N];
int head,tail;
LL dp[N];
int main() {
#ifdef local
    freopen("input", "r", stdin);
#endif // local
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n,c;
    cin>>n>>c;
    LL all = 0;
    for(int i = 1; i <= n; i++){
        cin>>val[i];
        all += val[i];
    }
    for(int i = 1; i <= n; i++){
        while(tail > head && val[i] <= val[q[tail - 1]])tail--;
        q[tail++] = i;
        while(head < tail && q[head] <= i - c)head++;
        dp[i] = dp[max(i - c,0)] + (i < c ? 0 : val[q[head]]);
        dp[i] = max(dp[i],dp[i - 1]);
    }
    cout<<all - dp[n]<<endl;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
map<ll,ll>M;
ll A[7<<14],f[7<<14],n,c,s;
int main(){
	cin>>n>>c;
	for(int i=1;i<=n;++i){
		scanf("%lld",A+i); f[i]=f[i-1]+A[i];
		s+=A[i]; ++M[A[i]];
		if(i>c){s-=A[i-c];if(!--M[A[i-c]])M.erase(A[i-c]);}
		if(i>=c)f[i]=std::min(f[i],f[i-c]+s-M.begin()->first);
	}return cout<<f[n],0;
}


#include <cstdio>
#include <iostream>
using namespace std;
#define LL long long
#define ls rt<<1
#define rs rt<<1|1
const int N = 1e5+5;
LL a[N],dp[N];

struct Node {
    int l,r;
    LL Min;
} t[N*3];
void build(int l,int r,int rt) {
    t[rt].l = l;
    t[rt].r = r;
    if(l != r) {
        int mid = (l+r)>>1;
        build(l,mid,ls);
        build(mid+1,r,rs);
        t[rt].Min = min(t[ls].Min,t[rs].Min);
    } else {
        t[rt].Min = a[l];
    }
}
LL getMin(int l,int r,int rt) {
    if(t[rt].l>=l && t[rt].r<=r) {
        return t[rt].Min;
    }
    int mid = (t[rt].l + t[rt].r)>>1;
    if(r <= mid) {
        return getMin(l,r,ls);
    } else if(l > mid) {
        return getMin(l,r,rs);
    } else {
        return min(getMin(l,mid,ls),getMin(mid+1,r,rs));
    }
}

int main() {
    int n,c;
    scanf("%d%d",&n,&c);
    LL sum = 0;
    for(int i=1; i<=n; i++) {
        scanf("%I64d",&a[i]);
        sum = sum + a[i];
    }

    build(1,n,1);
    dp[0] = 0;

    for(int i=1; i<=n; i++) {
        if(i < c) {
            dp[i] = dp[i-1];
        } else {
            dp[i] = max(dp[i-1],dp[i-c]+getMin(i-c+1,i,1));
        }
    }
    printf("%I64d\n",sum - dp[n]);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <cmath>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(x) ((x) < 0 ? -1 * (x) : (x))
template <class T>
inline void swap(T &x, T &y)
{
	T tmp = x;x = y, y = tmp;
}
template <class T>
inline void read(T &x)
{
	x = 0;char ch = getchar(), c = ch;
	while(ch < '0' || ch > '9') c = ch, ch = getchar();
	while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
	if(c == '-') x = -x;
}
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long MAXN = 400000 + 10;

long long mi[MAXN << 2], n, c, a[MAXN], dp[MAXN], sum, ans;
void build(long long o = 1, long long l = 1, long long r = n)
{
	if(l == r)
	{
		mi[o] = a[l];return;
	}
	int mid = (l + r) >> 1;
	build(o << 1, l, mid);
	build(o << 1 | 1, mid + 1, r);
	mi[o] = min(mi[o << 1], mi[o << 1 | 1]);
}
long long ask(long long ll, long long rr, long long o = 1, long long l = 1, long long r = n)
{
	if(ll <= l && rr >= r) return mi[o];
	long long mid = (l + r) >> 1, ans = INF;
	if(mid >= ll) ans = min(ans, ask(ll, rr, o << 1, l, mid));
	if(mid < rr) ans = min(ans, ask(ll, rr, o << 1 | 1, mid + 1, r));
	return ans;
}
int main()
{
	//freopen("data.txt", "r", stdin);
	read(n), read(c);
	for(long long i = 1;i <= n;++ i) read(a[i]), sum += a[i];
	build(1,1,n);
	for(long long i = c;i <= n;++ i)
		dp[i] = max(dp[i - 1], dp[i - c] + ask(i - c + 1, i,1,1,n)), ans = max(ans, dp[i]);
	printf("%I64d", sum - ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <cmath>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(x) ((x) < 0 ? -1 * (x) : (x))
#define ll long long
template <class T>
inline void swap(T &x, T &y)
{
	T tmp = x;x = y, y = tmp;
}
template <class T>
inline void read(T &x)
{
	x = 0;char ch = getchar(), c = ch;
	while(ch < '0' || ch > '9') c = ch, ch = getchar();
	while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
	if(c == '-') x = -x;
}
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long MAXN = 400000 + 10;

long long val[MAXN << 2], n, c, a[MAXN], dp[MAXN], sum, ans;
inline ll Min(ll x,ll y){
	if(x < y) return x;
	return y;
}

inline ll Max(ll x,ll y){
	if(x > y) return x;
	return y;
}
void build(ll id,ll l,ll r){
	if(l == r){
		val[id] = a[l];
		return;
	}
	ll mid = (l+r)>>1;
	build(id<<1,l,mid);
	build(id<<1|1,mid+1,r);
	val[id] = Min(val[id<<1],val[id<<1|1]);
}

ll query(ll id,ll l,ll r,ll x,ll y){
	if(x <= l && r <= y) return val[id];
	ll mid = (l+r)>>1,ans = 1e18;
	if(x <= mid) ans = query(id<<1,l,mid,x,y);
	if(y > mid) ans = Min(ans,query(id<<1|1,mid+1,r,x,y));
	return ans;
}
int main()
{
	//freopen("data.txt", "r", stdin);
	read(n), read(c);
	for(long long i = 1;i <= n;++ i) read(a[i]), sum += a[i];
	build(1,1,n);
	for(long long i = c;i <= n;++ i)
		dp[i] = max(dp[i - 1], dp[i - c] + query(1,1,n,i - c + 1, i)), ans = max(ans, dp[i]);
	printf("%I64d", sum - ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <cmath>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(x) ((x) < 0 ? -1 * (x) : (x))
template <class T>
inline void swap(T &x, T &y)
{
	T tmp = x;x = y, y = tmp;
}
template <class T>
inline void read(T &x)
{
	x = 0;char ch = getchar(), c = ch;
	while(ch < '0' || ch > '9') c = ch, ch = getchar();
	while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
	if(c == '-') x = -x;
}
const long long INF = 1000000000 + 1;
const long long MAXN = 400000 + 10;

long long mi[MAXN << 2], n, c, a[MAXN], dp[MAXN], sum, ans;
void build(long long o = 1, long long l = 1, long long r = n)
{
	if(l == r)
	{
		mi[o] = a[l];return;
	}
	long long mid = (l + r) >> 1;
	build(o << 1, l, mid);
	build(o << 1 | 1, mid + 1, r);
	mi[o] = min(mi[o << 1], mi[o << 1 | 1]);
}
long long ask(long long ll, long long rr, long long o = 1, long long l = 1, long long r = n)
{
	if(ll <= l && rr >= r) return mi[o];
	long long mid = (l + r) >> 1, re = INF;
	if(mid >= ll) re = ask(ll, rr, o << 1, l, mid);
	if(mid < rr) re = min(re, ask(ll, rr, o << 1 | 1, mid + 1, r));
	return re;
}
int main()
{
	//freopen("data.txt", "r", stdin);
	read(n), read(c);
	for(long long i = 1;i <= n;++ i) read(a[i]), sum += a[i];
	build();
	for(long long i = c;i <= n;++ i)
		dp[i] = max(dp[i - 1], dp[i - c] + ask(i - c + 1, i)), ans = max(ans, dp[i]);
	printf("%I64d", sum - ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <cmath>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(x) ((x) < 0 ? -1 * (x) : (x))
template <class T>
inline void swap(T &x, T &y)
{
	T tmp = x;x = y, y = tmp;
}
template <class T>
inline void read(T &x)
{
	x = 0;char ch = getchar(), c = ch;
	while(ch < '0' || ch > '9') c = ch, ch = getchar();
	while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
	if(c == '-') x = -x;
}
const int INF = 1000000000 + 1;
const int MAXN = 100000 + 10;

int mi[MAXN], n, c, a[MAXN], dp[MAXN], sum, ans;
void build(int o = 1, int l = 1, int r = n)
{
	if(l == r)
	{
		mi[o] = a[l];return;
	}
	int mid = (l + r) >> 1;
	build(o << 1, l, mid);
	build(o << 1 | 1, mid + 1, r);
	mi[o] = min(mi[o << 1], mi[o << 1 | 1]);
}
int ask(int ll, int rr, int o = 1, int l = 1, int r = n)
{
	if(ll <= l && rr >= r) return mi[o];
	int mid = (l + r) >> 1, re = INF;
	if(mid >= ll) re = min(re, ask(ll, rr, o << 1, l, mid));
	if(mid < rr) re = min(re, ask(ll, rr, o << 1 | 1, mid + 1, r));
	return re;
}
int main()
{
	//freopen("data.txt", "r", stdin);
	read(n), read(c);
	for(int i = 1;i <= n;++ i) read(a[i]), sum += a[i];
	build();
	for(int i = c;i <= n;++ i)
		dp[i] = max(dp[i - 1], dp[i - c] + ask(i - c + 1, i)), ans = max(ans, dp[i]);
	printf("%d", sum - ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <cmath>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(x) ((x) < 0 ? -1 * (x) : (x))
template <class T>
inline void swap(T &x, T &y)
{
	T tmp = x;x = y, y = tmp;
}
template <class T>
inline void read(T &x)
{
	x = 0;char ch = getchar(), c = ch;
	while(ch < '0' || ch > '9') c = ch, ch = getchar();
	while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
	if(c == '-') x = -x;
}
const long long INF = 10000000000 + 1;
const long long MAXN = 400000 + 10;

long long mi[MAXN << 2], n, c, a[MAXN], dp[MAXN], sum, ans;
void build(long long o = 1, long long l = 1, long long r = n)
{
	if(l == r)
	{
		mi[o] = a[l];return;
	}
	long long mid = (l + r) >> 1;
	build(o << 1, l, mid);
	build(o << 1 | 1, mid + 1, r);
	mi[o] = min(mi[o << 1], mi[o << 1 | 1]);
}
long long ask(long long ll, long long rr, long long o = 1, long long l = 1, long long r = n)
{
	if(ll <= l && rr >= r) return mi[o];
	long long mid = (l + r) >> 1, re = INF;
	if(mid >= ll) re = min(re, ask(ll, rr, o << 1, l, mid));
	if(mid < rr) re = min(re, ask(ll, rr, o << 1 | 1, mid + 1, r));
	return re;
}
int main()
{
	//freopen("data.txt", "r", stdin);
	read(n), read(c);
	for(long long i = 1;i <= n;++ i) read(a[i]), sum += a[i];
	build();
	for(long long i = 1;i <= n;++ i)
	{
		dp[i] = dp[i - 1];
		if(i >= c)
			dp[i] = max(dp[i], dp[i - c] + ask(i - c + 1, i));
	}
	for(int i = 1;i <= n;++ i)
		ans = max(ans, dp[i]);
	printf("%I64d", sum - ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <cmath>
#define max(a, b) ((a) > (b) ? (a) : (b))
//#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(x) ((x) < 0 ? -1 * (x) : (x))
inline long long min(long long a, long long b)
{
	return a < b ? a : b;
}
template <class T>
inline void swap(T &x, T &y)
{
	T tmp = x;x = y, y = tmp;
}
template <class T>
inline void read(T &x)
{
	x = 0;char ch = getchar(), c = ch;
	while(ch < '0' || ch > '9') c = ch, ch = getchar();
	while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
	if(c == '-') x = -x;
}
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long MAXN = 400000 + 10;

long long mi[MAXN << 2], n, c, a[MAXN], dp[MAXN], sum, ans;
void build(long long o = 1, long long l = 1, long long r = n)
{
	if(l == r)
	{
		mi[o] = a[l];return;
	}
	long long mid = (l + r) >> 1;
	build(o << 1, l, mid);
	build(o << 1 | 1, mid + 1, r);
	mi[o] = min(mi[o << 1], mi[o << 1 | 1]);
}
long long ask(long long ll, long long rr, long long o = 1, long long l = 1, long long r = n)
{
	if(ll <= l && rr >= r) return mi[o];
	long long mid = (l + r) >> 1, re = INF;
	if(mid >= ll) re = ask(ll, rr, o << 1, l, mid);
	if(mid < rr) re = min(re, ask(ll, rr, o << 1 | 1, mid + 1, r));
	return re;
}
int main()
{
	//freopen("data.txt", "r", stdin);
	read(n), read(c);
	for(long long i = 1;i <= n;++ i) read(a[i]), sum += a[i];
	build(1,1,n);
	for(long long i = c;i <= n;++ i)
		dp[i] = max(dp[i - 1], dp[i - c] + ask(i - c + 1, i,1,1,n)), ans = max(ans, dp[i]);
	printf("%I64d", sum - ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <cmath>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(x) ((x) < 0 ? -1 * (x) : (x))
template <class T>
inline void swap(T &x, T &y)
{
	T tmp = x;x = y, y = tmp;
}
template <class T>
inline void read(T &x)
{
	x = 0;char ch = getchar(), c = ch;
	while(ch < '0' || ch > '9') c = ch, ch = getchar();
	while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
	if(c == '-') x = -x;
}
const long long INF = 1000000000 + 1;
const long long MAXN = 100000 + 10;

long long mi[MAXN], n, c, a[MAXN], dp[MAXN], sum, ans;
void build(long long o = 1, long long l = 1, long long r = n)
{
	if(l == r)
	{
		mi[o] = a[l];return;
	}
	long long mid = (l + r) >> 1;
	build(o << 1, l, mid);
	build(o << 1 | 1, mid + 1, r);
	mi[o] = min(mi[o << 1], mi[o << 1 | 1]);
}
long long ask(long long ll, long long rr, long long o = 1, long long l = 1, long long r = n)
{
	if(ll <= l && rr >= r) return mi[o];
	long long mid = (l + r) >> 1, re = INF;
	if(mid >= ll) re = min(re, ask(ll, rr, o << 1, l, mid));
	if(mid < rr) re = min(re, ask(ll, rr, o << 1 | 1, mid + 1, r));
	return re;
}
int main()
{
	//freopen("data.txt", "r", stdin);
	read(n), read(c);
	for(long long i = 1;i <= n;++ i) read(a[i]), sum += a[i];
	build();
	for(long long i = c;i <= n;++ i)
		dp[i] = max(dp[i - 1], dp[i - c] + ask(i - c + 1, i)), ans = max(ans, dp[i]);
	printf("%I64d", sum - ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <cmath>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(x) ((x) < 0 ? -1 * (x) : (x))
template <class T>
inline void swap(T &x, T &y)
{
	T tmp = x;x = y, y = tmp;
}
template <class T>
inline void read(T &x)
{
	x = 0;char ch = getchar(), c = ch;
	while(ch < '0' || ch > '9') c = ch, ch = getchar();
	while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
	if(c == '-') x = -x;
}
const long long INF = 1000000000 + 1;
const long long MAXN = 400000 + 10;

long long mi[MAXN << 2], n, c, a[MAXN], dp[MAXN], sum, ans;
void build(long long o = 1, long long l = 1, long long r = n)
{
	if(l == r)
	{
		mi[o] = a[l];return;
	}
	long long mid = (l + r) >> 1;
	build(o << 1, l, mid);
	build(o << 1 | 1, mid + 1, r);
	mi[o] = min(mi[o << 1], mi[o << 1 | 1]);
}
long long ask(long long ll, long long rr, long long o = 1, long long l = 1, long long r = n)
{
	if(ll <= l && rr >= r) return mi[o];
	long long mid = (l + r) >> 1, re = INF;
	if(mid >= ll) re = min(re, ask(ll, rr, o << 1, l, mid));
	if(mid < rr) re = min(re, ask(ll, rr, o << 1 | 1, mid + 1, r));
	return re;
}
int main()
{
	//freopen("data.txt", "r", stdin);
	read(n), read(c);
	for(long long i = 1;i <= n;++ i) read(a[i]), sum += a[i];
	build();
	for(long long i = c;i <= n;++ i)
		dp[i] = max(dp[i - 1], dp[i - c] + ask(i - c + 1, i)), ans = max(ans, dp[i]);
	printf("%I64d", sum - ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <cmath>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(x) ((x) < 0 ? -1 * (x) : (x))
template <class T>
inline void swap(T &x, T &y)
{
	T tmp = x;x = y, y = tmp;
}
template <class T>
inline void read(T &x)
{
	x = 0;char ch = getchar(), c = ch;
	while(ch < '0' || ch > '9') c = ch, ch = getchar();
	while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
	if(c == '-') x = -x;
}
const long long INF = 10000000000 + 1;
const long long MAXN = 400000 + 10;

long long mi[MAXN << 2], n, c, a[MAXN], dp[MAXN], sum, ans;
void build(long long o = 1, long long l = 1, long long r = n)
{
	if(l == r)
	{
		mi[o] = a[l];return;
	}
	long long mid = (l + r) >> 1;
	build(o << 1, l, mid);
	build(o << 1 | 1, mid + 1, r);
	mi[o] = min(mi[o << 1], mi[o << 1 | 1]);
}
long long ask(long long ll, long long rr, long long o = 1, long long l = 1, long long r = n)
{
	if(ll <= l && rr >= r) return mi[o];
	long long mid = (l + r) >> 1, re = INF;
	if(mid >= ll) re = ask(ll, rr, o << 1, l, mid);
	if(mid < rr) re = min(re, ask(ll, rr, o << 1 | 1, mid + 1, r));
	return re;
}
int main()
{
	//freopen("data.txt", "r", stdin);
	read(n), read(c);
	for(long long i = 1;i <= n;++ i) read(a[i]), sum += a[i];
	build();
	for(long long i = c;i <= n;++ i)
		dp[i] = max(dp[i - 1], dp[i - c] + ask(i - c + 1, i)), ans = max(ans, dp[i]);
	printf("%I64d", sum - ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <cmath>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(x) ((x) < 0 ? -1 * (x) : (x))
template <class T>
inline void swap(T &x, T &y)
{
	T tmp = x;x = y, y = tmp;
}
template <class T>
inline void read(T &x)
{
	x = 0;char ch = getchar(), c = ch;
	while(ch < '0' || ch > '9') c = ch, ch = getchar();
	while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
	if(c == '-') x = -x;
}
const long long INF = 10000000000 + 1;
const long long MAXN = 400000 + 10;

long long mi[MAXN << 2], n, c, a[MAXN], dp[MAXN], sum, ans;
void build(long long o = 1, long long l = 1, long long r = n)
{
	if(l == r)
	{
		mi[o] = a[l];return;
	}
	long long mid = (l + r) >> 1;
	build(o << 1, l, mid);
	build(o << 1 | 1, mid + 1, r);
	mi[o] = min(mi[o << 1], mi[o << 1 | 1]);
}
long long ask(long long ll, long long rr, long long o = 1, long long l = 1, long long r = n)
{
	if(ll <= l && rr >= r) return mi[o];
	long long mid = (l + r) >> 1, re = INF;
	if(mid >= ll) re = min(re, ask(ll, rr, o << 1, l, mid));
	if(mid < rr) re = min(re, ask(ll, rr, o << 1 | 1, mid + 1, r));
	return re;
}
int main()
{
	//freopen("data.txt", "r", stdin);
	read(n), read(c);
	for(long long i = 1;i <= n;++ i) read(a[i]), sum += a[i];
	build();
	for(long long i = c;i <= n;++ i)
		dp[i] = max(dp[i - 1], dp[i - c] + ask(i - c + 1, i)), ans = max(ans, dp[i]);
	printf("%I64d", sum - ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <cmath>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(x) ((x) < 0 ? -1 * (x) : (x))
template <class T>
inline void swap(T &x, T &y)
{
	T tmp = x;x = y, y = tmp;
}
template <class T>
inline void read(T &x)
{
	x = 0;char ch = getchar(), c = ch;
	while(ch < '0' || ch > '9') c = ch, ch = getchar();
	while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
	if(c == '-') x = -x;
}
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long MAXN = 400000 + 10;

long long mi[MAXN << 2], n, c, a[MAXN], dp[MAXN], sum, ans;
void build(long long o = 1, long long l = 1, long long r = n)
{
	if(l == r)
	{
		mi[o] = a[l];return;
	}
	int mid = (l + r) >> 1;
	build(o << 1, l, mid);
	build(o << 1 | 1, mid + 1, r);
}
long long ask(long long ll, long long rr, long long o = 1, long long l = 1, long long r = n)
{
	if(ll <= l && rr >= r) return mi[o];
	long long mid = (l + r) >> 1, ans = INF;
	if(mid >= ll) ans = min(ans, ask(ll, rr, o << 1, l, mid));
	if(mid < rr) ans = min(ans, ask(ll, rr, o << 1 | 1, mid + 1, r));
	return ans;
}
int main()
{
	//freopen("data.txt", "r", stdin);
	read(n), read(c);
	for(long long i = 1;i <= n;++ i) read(a[i]), sum += a[i];
	build(1,1,n);
	for(long long i = c;i <= n;++ i)
		dp[i] = max(dp[i - 1], dp[i - c] + ask(i - c + 1, i,1,1,n)), ans = max(ans, dp[i]);
	printf("%I64d", sum - ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <cmath>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(x) ((x) < 0 ? -1 * (x) : (x))
template <class T>
inline void swap(T &x, T &y)
{
	T tmp = x;x = y, y = tmp;
}
template <class T>
inline void read(T &x)
{
	x = 0;char ch = getchar(), c = ch;
	while(ch < '0' || ch > '9') c = ch, ch = getchar();
	while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
	if(c == '-') x = -x;
}
const long long INF = 1000000000;
const long long MAXN = 1000000 + 10;
/*
���bi-1 = bi-2 = bi-3 = bi-4 = 1
	���bi = 0
	    ���bi-1 = 0 ��ôû��Ӱ��
		���bi-1 = 1 ��ô ai,ai-1,ai-2,ai-3,ai-4 > r
	���bi = 1  ��ôai,ai-1,ai-2,ai-3,ai-4 <= r
���bi-1 = bi-2 = bi-3 = bi-4 = 0
    ���bi = 0 ��ô ai,ai-1,ai-2,ai-3,ai-4 >= l
	���bi = 1
	    ���bi-1 = 0 ��ô ai,ai-1,ai-2,ai-3,ai-4 < l
	    ���bi-1 = 1 ��ôû��Ӱ��
*/
long long l = - INF - 1, r = INF + 1, n, a[MAXN], b[MAXN];
char s[MAXN];
int main()
{
	freopen("data.txt", "r", stdin);
	read(n);
	for(long long i = 1;i <= n;++ i) read(a[i]);
	scanf("%s", s + 1);
	for(long long i = 1;i <= n;++ i) b[i] = s[i] - '0';
	for(long long i = 5;i <= n;++ i)
	{
		if(b[i - 1] && b[i - 2] && b[i - 3] && b[i - 4])
		{
			if(!b[i])
				r = min(r, min(a[i - 1] - 1, min(a[i - 2] - 1, min(a[i - 3] - 1, min(a[i - 4] - 1, a[i] - 1)))));
		}
		else if(!b[i - 1] && !b[i - 2] && !b[i - 3] && !b[i - 4])
		{
			if(b[i])
				l = max(l, max(a[i - 1] + 1, max(a[i - 2] + 1, max(a[i - 3] + 1, max(a[i - 4] + 1, a[i] + 1)))));
		}
	}
	if(l == -INF - 1) l = -INF;
	if(r == INF + 1) r = INF;
	printf("%I64d %I64d\n", l, r);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <cmath>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(x) ((x) < 0 ? -1 * (x) : (x))
template <class T>
inline void swap(T &x, T &y)
{
	T tmp = x;x = y, y = tmp;
}
template <class T>
inline void read(T &x)
{
	x = 0;char ch = getchar(), c = ch;
	while(ch < '0' || ch > '9') c = ch, ch = getchar();
	while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
	if(c == '-') x = -x;
}
const long long INF = 1000000000 + 1;
const long long MAXN = 100000 + 10;

long long mi[MAXN << 2], n, c, a[MAXN], dp[MAXN], sum, ans;
void build(long long o = 1, long long l = 1, long long r = n)
{
	if(l == r)
	{
		mi[o] = a[l];return;
	}
	long long mid = (l + r) >> 1;
	build(o << 1, l, mid);
	build(o << 1 | 1, mid + 1, r);
	mi[o] = min(mi[o << 1], mi[o << 1 | 1]);
}
long long ask(long long ll, long long rr, long long o = 1, long long l = 1, long long r = n)
{
	if(ll <= l && rr >= r) return mi[o];
	long long mid = (l + r) >> 1, re = INF;
	if(mid >= ll) re = min(re, ask(ll, rr, o << 1, l, mid));
	if(mid < rr) re = min(re, ask(ll, rr, o << 1 | 1, mid + 1, r));
	return re;
}
int main()
{
	//freopen("data.txt", "r", stdin);
	read(n), read(c);
	for(long long i = 1;i <= n;++ i) read(a[i]), sum += a[i];
	build();
	for(long long i = c;i <= n;++ i)
		dp[i] = max(dp[i - 1], dp[i - c] + ask(i - c + 1, i)), ans = max(ans, dp[i]);
	printf("%I64d", sum - ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <cmath>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(x) ((x) < 0 ? -1 * (x) : (x))
template <class T>
inline void swap(T &x, T &y)
{
	T tmp = x;x = y, y = tmp;
}
template <class T>
inline void read(T &x)
{
	x = 0;char ch = getchar(), c = ch;
	while(ch < '0' || ch > '9') c = ch, ch = getchar();
	while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
	if(c == '-') x = -x;
}
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long MAXN = 400000 + 10;

long long mi[MAXN << 2], n, c, a[MAXN], dp[MAXN], sum, ans;
void build(long long o = 1, long long l = 1, long long r = n)
{
	if(l == r)
	{
		mi[o] = a[l];return;
	}
	long long mid = (l + r) >> 1;
	build(o << 1, l, mid);
	build(o << 1 | 1, mid + 1, r);
	mi[o] = min(mi[o << 1], mi[o << 1 | 1]);
}
long long ask(long long ll, long long rr, long long o = 1, long long l = 1, long long r = n)
{
	if(ll <= l && rr >= r) return mi[o];
	long long mid = (l + r) >> 1, re = INF;
	if(mid >= ll) re = ask(ll, rr, o << 1, l, mid);
	if(mid < rr) re = min(re, ask(ll, rr, o << 1 | 1, mid + 1, r));
	return re;
}
int main()
{
	//freopen("data.txt", "r", stdin);
	read(n), read(c);
	for(long long i = 1;i <= n;++ i) read(a[i]), sum += a[i];
	build(1,1,n);
	for(long long i = c;i <= n;++ i)
		dp[i] = max(dp[i - 1], dp[i - c] + ask(i - c + 1, i,1,1,n)), ans = max(ans, dp[i]);
	printf("%I64d", sum - ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <cmath>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(x) ((x) < 0 ? -1 * (x) : (x))
#define ll long long
template <class T>
inline void swap(T &x, T &y)
{
	T tmp = x;x = y, y = tmp;
}
template <class T>
inline void read(T &x)
{
	x = 0;char ch = getchar(), c = ch;
	while(ch < '0' || ch > '9') c = ch, ch = getchar();
	while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
	if(c == '-') x = -x;
}
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long MAXN = 400000 + 10;

long long val[MAXN << 2], n, c, a[MAXN], dp[MAXN], sum, ans;
void build(ll id,ll l,ll r){
	if(l == r){
		val[id] = a[l];
		return;
	}
	ll mid = (l+r)>>1;
	build(id<<1,l,mid);
	build(id<<1|1,mid+1,r);
	val[id] = min(val[id<<1],val[id<<1|1]);
}

ll query(ll id,ll l,ll r,ll x,ll y){
	if(x <= l && r <= y) return val[id];
	ll mid = (l+r)>>1,ans = 1e18;
	if(x <= mid) ans = query(id<<1,l,mid,x,y);
	if(y > mid) ans = min(ans,query(id<<1|1,mid+1,r,x,y));
	return ans;
}
int main()
{
	//freopen("data.txt", "r", stdin);
	read(n), read(c);
	for(long long i = 1;i <= n;++ i) read(a[i]), sum += a[i];
	build(1,1,n);
	for(long long i = c;i <= n;++ i)
		dp[i] = max(dp[i - 1], dp[i - c] + query(1,1,n,i - c + 1, i)), ans = max(ans, dp[i]);
	printf("%I64d", sum - ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <cmath>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(x) ((x) < 0 ? -1 * (x) : (x))
template <class T>
inline void swap(T &x, T &y)
{
	T tmp = x;x = y, y = tmp;
}
template <class T>
inline void read(T &x)
{
	x = 0;char ch = getchar(), c = ch;
	while(ch < '0' || ch > '9') c = ch, ch = getchar();
	while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
	if(c == '-') x = -x;
}
const long long INF = 1000000000 + 1;
const long long MAXN = 200000 + 10;

long long mi[MAXN << 2], n, c, a[MAXN], dp[MAXN], sum, ans;
void build(long long o = 1, long long l = 1, long long r = n)
{
	if(l == r)
	{
		mi[o] = a[l];return;
	}
	long long mid = (l + r) >> 1;
	build(o << 1, l, mid);
	build(o << 1 | 1, mid + 1, r);
	mi[o] = min(mi[o << 1], mi[o << 1 | 1]);
}
long long ask(long long ll, long long rr, long long o = 1, long long l = 1, long long r = n)
{
	if(ll <= l && rr >= r) return mi[o];
	long long mid = (l + r) >> 1, re = INF;
	if(mid >= ll) re = min(re, ask(ll, rr, o << 1, l, mid));
	if(mid < rr) re = min(re, ask(ll, rr, o << 1 | 1, mid + 1, r));
	return re;
}
int main()
{
	//freopen("data.txt", "r", stdin);
	read(n), read(c);
	for(long long i = 1;i <= n;++ i) read(a[i]), sum += a[i];
	build();
	for(long long i = c;i <= n;++ i)
		dp[i] = max(dp[i - 1], dp[i - c] + ask(i - c + 1, i)), ans = max(ans, dp[i]);
	printf("%I64d", sum - ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <cmath>
#define max(a, b) ((a) > (b) ? (a) : (b))
//#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(x) ((x) < 0 ? -1 * (x) : (x))
#define ll long long
inline long long min(long long a, long long b)
{
	return a < b ? a : b;
}
template <class T>
inline void swap(T &x, T &y)
{
	T tmp = x;x = y, y = tmp;
}
template <class T>
inline void read(T &x)
{
	x = 0;char ch = getchar(), c = ch;
	while(ch < '0' || ch > '9') c = ch, ch = getchar();
	while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
	if(c == '-') x = -x;
}
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long MAXN = 400000 + 10;

long long val[MAXN << 2], n, c, a[MAXN], dp[MAXN], sum, ans;
void build(ll id,ll l,ll r){
	if(l == r){
		val[id] = a[l];
		return;
	}
	ll mid = (l+r)>>1;
	build(id<<1,l,mid);
	build(id<<1|1,mid+1,r);
	val[id] = min(val[id<<1],val[id<<1|1]);
}

ll query(ll id,ll l,ll r,ll x,ll y){
	if(x <= l && r <= y) return val[id];
	ll mid = (l+r)>>1,ans = 1e18;
	if(x <= mid) ans = query(id<<1,l,mid,x,y);
	if(y > mid) ans = min(ans,query(id<<1|1,mid+1,r,x,y));
	return ans;
}
int main()
{
	//freopen("data.txt", "r", stdin);
	read(n), read(c);
	for(long long i = 1;i <= n;++ i) read(a[i]), sum += a[i];
	build(1,1,n);
	for(long long i = c;i <= n;++ i)
		dp[i] = max(dp[i - 1], dp[i - c] + query(1,1,n,i - c + 1, i)), ans = max(ans, dp[i]);
	printf("%I64d", sum - ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <cmath>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(x) ((x) < 0 ? -1 * (x) : (x))
template <class T>
inline void swap(T &x, T &y)
{
	T tmp = x;x = y, y = tmp;
}
template <class T>
inline void read(T &x)
{
	x = 0;char ch = getchar(), c = ch;
	while(ch < '0' || ch > '9') c = ch, ch = getchar();
	while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
	if(c == '-') x = -x;
}
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long MAXN = 400000 + 10;

long long mi[MAXN << 2], n, c, a[MAXN], dp[MAXN], sum, ans;
void build(long long o = 1, long long l = 1, long long r = n)
{
	if(l == r)
	{
		mi[o] = a[l];return;
	}
	long long mid = (l + r) >> 1;
	build(o << 1, l, mid);
	build(o << 1 | 1, mid + 1, r);
	mi[o] = min(mi[o << 1], mi[o << 1 | 1]);
}
long long ask(long long ll, long long rr, long long o = 1, long long l = 1, long long r = n)
{
	if(ll <= l && rr >= r) return mi[o];
	long long mid = (l + r) >> 1, re = INF;
	if(mid >= ll) re = ask(ll, rr, o << 1, l, mid);
	if(mid < rr) re = min(re, ask(ll, rr, o << 1 | 1, mid + 1, r));
	return re;
}
int main()
{
	//freopen("data.txt", "r", stdin);
	read(n), read(c);
	for(long long i = 1;i <= n;++ i) read(a[i]), sum += a[i];
	build();
	for(long long i = c;i <= n;++ i)
		dp[i] = max(dp[i - 1], dp[i - c] + ask(i - c + 1, i)), ans = max(ans, dp[i]);
	printf("%I64d", sum - ans);
	return 0;
}


#include<algorithm>
#include<cstring>
#include<cstdio>

using namespace std;

typedef long long LL;
const int N = 100005;
int n, c, hd, tl;
LL a[N], sum[N], mnC[N], dp[N];
pair<LL, int> q[N];

int main(){
	scanf("%d%d", &n, &c);
	for(int i = 1; i <= n; i++){
		scanf("%lld", &a[i]);
		sum[i] = sum[i-1] + a[i];
		while(hd < tl && q[tl-1].first > a[i])	tl--;
		q[tl++] = make_pair(a[i], i);
		while(hd < tl && q[hd].second <= max(i - c, 0))	hd++;
		mnC[i] = q[hd].first;
	}
	memset(dp, 0x7f, sizeof dp);
	dp[0] = 0;
	for(int i = 1; i <= n; i++){
		dp[i] = dp[i-1] + a[i];
		if(i - c >= 0)	dp[i] = min(dp[i], dp[i-c] + sum[i] - sum[i-c] - mnC[i]);
	}
	printf("%lld", dp[n]);
	return 0;
}


/*
Welcome Hacking
Wish You High Rating
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
int read(){
    int xx=0,ff=1;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
    return xx*ff;
}
inline int mymin(const int&xx,const int&yy)
{if(xx<yy)return xx;return yy;}
inline long long llmymin(const long long&xx,const long long&yy)
{if(xx<yy)return xx;return yy;}
const int maxn=100010;
int N,C,a[maxn];
long long sum[maxn],f[maxn];
struct Segment_Tree{
	int minn;
}T[maxn*4];
int x,y;
void build(int L,int R,int root){
	if(L==R){
		T[root].minn=a[L];
		return;
	}
	int mid=(L+R)>>1;
	build(L,mid,root*2);
	build(mid+1,R,root*2+1);
	T[root].minn=mymin(T[root*2].minn,T[root*2+1].minn);
}
int query(int L,int R,int root){
	if(x>R||y<L)
		return (1<<30);
	if(x<=L&&y>=R)
		return T[root].minn;
	int mid=(L+R)>>1;
	return mymin(query(L,mid,root*2),query(mid+1,R,root*2+1));
}
int main(){
	//freopen("in","r",stdin);
	N=read(),C=read();
	for(int i=1;i<=N;i++)
		a[i]=read(),sum[i]=sum[i-1]+a[i];
	build(1,N,1);
	for(int i=1;i<=N;i++){
		f[i]=f[i-1]+a[i];
		if(i>=C)
			f[i]=llmymin(f[i],f[i-C]+sum[i]-sum[i-C]-(x=i-C+1,y=i,query(1,N,1)));
	}
	/*for(int i=1;i<=N;i++)
		printf("%d ",f[i]);
	puts("");*/
	printf("%I64d\n",f[N]);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
const int inf=1<<30;
int mini[maxn<<2];
ll dp[maxn],sum;
int n,c;
void PushUp(int o)
{
    mini[o]=min(mini[o<<1],mini[o<<1|1]);
}
void Build(int o,int l,int r)
{
    if(l==r)
    {
        scanf("%d",&mini[o]);
        sum+=mini[o];
        return ;
    }
    int mid=l+r>>1;
    Build(o<<1,l,mid);
    Build(o<<1|1,mid+1,r);
    PushUp(o);
}
int Find(int o,int l,int r,int L,int R)
{
    if(L<=l&&r<=R)
        return mini[o];
    int mid=l+r>>1;
    int ans1,ans2;
    ans1=ans2=inf;
    if(L<=mid)
        ans1=Find(o<<1,l,mid,L,R);
    if(R>mid)
        ans2=Find(o<<1|1,mid+1,r,L,R);
    int ans=min(ans1,ans2);
    return ans;
}
int main()
{
    while(~scanf("%d%d",&n,&c))
    {
        sum=0;
        Build(1,1,n);
        memset(dp,0,sizeof(dp));
        for(int i=c;i<=n;i++)
        {
            dp[i]=max(dp[i-1],dp[i-c]+(ll)Find(1,1,n,i-c+1,i));
        }
        printf("%I64d\n",sum-dp[n]);
    }
    return 0;
}


//by sdfzchy
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int inf=(1<<30);
inline int in()
{
	char ch=getchar();
	int f=1,tmp=0;
	while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9') {tmp=(tmp<<1)+(tmp<<3)+(ch-'0');ch=getchar();}
	return tmp*f;
}

const int N=100010;
LL n,c,a[2*N];
LL f[N][20],lo[N];
LL g[N];

int gi_f(int l,int r)
{
    int x=lo[r-l+1];
    return min(f[l][x],f[r-(1<<x)+1][x]);
}

int main()
{
//	freopen("in.in","r",stdin);
//	freopen("out.out","w",stdout);
	n=in(),c=in();
	memset(f,0x3f,sizeof(f));
	for(int i=1;i<=n;i++) a[i]=in(),f[i][0]=a[i];
	for(int j=1;(1<<j)<=n;j++)
		for(int i=1;i+(1<<j)-1<=n;i++)
			f[i][j]=min(f[i][j-1],f[i+(1<<(j-1))][j-1]);
	for(int i=2;i<=n;i++) lo[i]=lo[i>>1]+1;
	LL ans=0;
	for(int i=1;i<=n;i++) ans+=a[i];
	for(int i=c;i<=n;i++)
		g[i]=max(g[i-1],g[i-c]+gi_f(i-c+1,i));
	cout<<ans-g[n]<<endl;
	return 0;
}


//by sdfzchy
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int inf=(1<<30);
inline int in()
{
	char ch=getchar();
	int f=1,tmp=0;
	while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9') {tmp=(tmp<<1)+(tmp<<3)+(ch-'0');ch=getchar();}
	return tmp*f;
}

const int N=100010;
int n,c,a[2*N];
int f[N][20],lo[N];
int g[N];

int gi_f(int l,int r)
{
    int x=lo[r-l+1];
    return min(f[l][x],f[r-(1<<x)+1][x]);
}

int main()
{
//	freopen("in.in","r",stdin);
//	freopen("out.out","w",stdout);
	n=in(),c=in();
	memset(f,0x3f,sizeof(f));
	for(int i=1;i<=n;i++) a[i]=in(),f[i][0]=a[i];
	for(int i=n+1;i<=n+n;i++) a[i]=inf;
	for(int j=1;(1<<j)<=n;j++)
		for(int i=1;i+(1<<j)-1<=n;i++)
			f[i][j]=min(f[i][j-1],f[i+(1<<(j-1))][j-1]);
	for(int i=2;i<=n;i++) lo[i]=lo[i>>1]+1;
	LL ans=0;
	for(int i=1;i<=n;i++) ans+=a[i];
	for(int i=c;i<=n;i++)
		g[i]=max(g[i-1],g[i-c]+gi_f(i-c+1,i));
	cout<<ans-g[n]<<endl;
	return 0;
}


//by sdfzchy
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int inf=(1<<30);
inline int in()
{
	char ch=getchar();
	int f=1,tmp=0;
	while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9') {tmp=(tmp<<1)+(tmp<<3)+(ch-'0');ch=getchar();}
	return tmp*f;
}

const int N=100010;
LL n,c,a[2*N];
LL f[N][20],lo[N];
LL g[N];

int gi_f(int l,int r)
{
    int x=lo[r-l+1];
    return min(f[l][x],f[r-(1<<x)+1][x]);
}

int main()
{
//	freopen("in.in","r",stdin);
//	freopen("out.out","w",stdout);
	n=in(),c=in();
	memset(f,0x3f,sizeof(f));
	cout<<f[0][0]<<endl;
	for(int i=1;i<=n;i++) a[i]=in(),f[i][0]=a[i];
	for(int j=1;(1<<j)<=n;j++)
		for(int i=1;i+(1<<j)-1<=n;i++)
			f[i][j]=min(f[i][j-1],f[i+(1<<(j-1))][j-1]);
	for(int i=2;i<=n;i++) lo[i]=lo[i>>1]+1;
	LL ans=0;
	for(int i=1;i<=n;i++) ans+=a[i];
	for(int i=c;i<=n;i++)
		g[i]=max(g[i-1],g[i-c]+gi_f(i-c+1,i));
	cout<<ans-g[n]<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 100100;
LL stmin[maxn][20], dp[maxn], sum[maxn];
int n, c;
void InitSt()
{
    for(int j=1; (1<<j) <n; j++)
        for(int i=1; i+(1<<j) - 1 <= n; i++)
            stmin[i][j] = min(stmin[i][j-1], stmin[i+(1<<(j-1))][j-1]);
}

int query(int l, int r)
{
    int x = (int)(log( double(r-l+1) / log(2.0)));
    return min(stmin[l][x], stmin[r-(1<<x)+1][x]);
}
int main()
{
    scanf("%d%d", &n, &c);
    for(int i = 1;i <= n;i++){
        scanf("%lld", &stmin[i][0]);
        sum[i] = sum[i - 1] + stmin[i][0];
    }
    InitSt();
    for(int i = 1;i <= n;i++){
        dp[i] = dp[i - 1] + stmin[i][0];
        if(i >= c){
            dp[i] = min(dp[i], dp[i - c] + sum[i] - sum[i - c] - query(i - c + 1, i));
        }
    }
    printf("%lld\n", dp[n]);
    return 0;
}


#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 100100;
LL stmin[maxn][20], dp[maxn], sum[maxn];
int n, c;
void InitSt()
{
    for(int j=1; (1<<j) <n; j++)
        for(int i=1; i+(1<<j) - 1 <= n; i++)
            stmin[i][j] = min(stmin[i][j-1], stmin[i+(1<<(j-1))][j-1]);
}

int query(int l, int r)
{
    int x = (int)(log( double(r-l+1) / log(2.0)));
    return min(stmin[l][x], stmin[r-(1<<x)+1][x]);
}
int main()
{
    scanf("%d%d", &n, &c);
    for(int i = 1;i <= n;i++){
        scanf("%d", &stmin[i][0]);
        sum[i] = sum[i - 1] + stmin[i][0];
    }
    InitSt();
    for(int i = 1;i <= n;i++){
        dp[i] = dp[i - 1] + stmin[i][0];
        if(i >= c){
            dp[i] = min(dp[i], dp[i - c] + sum[i] - sum[i - c] - query(i - c + 1, i));
        }
    }
    printf("%d\n", dp[n]);
    return 0;
}


#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 100100;
LL stmin[maxn][20], dp[maxn], sum[maxn];
int lg2[maxn];
LL n, c;
void InitSt()
{
    lg2[0]=-1;
    for (int i=1; i<maxn; ++i) lg2[i]=lg2[i>>1]+1;
    for(int j = 1; (1<<j) <= n; j++)
        for(int i=1; i+(1<<j) - 1 <= n; i++)
            stmin[i][j] = min(stmin[i][j-1], stmin[i+(1<<(j-1))][j-1]);
}

LL query(int l, int r)
{
    int x = lg2[r-l+1];
    return min(stmin[l][x], stmin[r-(1<<x)+1][x]);
}
int main()
{
    scanf("%lld%lld", &n, &c);
    for(int i = 1;i <= n;i++){
        scanf("%lld", &stmin[i][0]);
        sum[i] = sum[i - 1] + stmin[i][0];
    }
    InitSt();
    for(int i = 1;i <= n;i++){
        dp[i] = dp[i - 1] + stmin[i][0];
        if(i >= c)
            dp[i] = min(dp[i], dp[i - c] + sum[i] - sum[i - c] - query(i - c + 1, i));
    }
    printf("%lld\n", dp[n]);
    return 0;
}


#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 100100;
LL stmin[maxn][20], dp[maxn], sum[maxn];
LL n, c;
void InitSt()
{
    for(int j=1; (1<<j) <n; j++)
        for(int i=1; i+(1<<j) - 1 <= n; i++)
            stmin[i][j] = min(stmin[i][j-1], stmin[i+(1<<(j-1))][j-1]);
}

LL query(int l, int r)
{
    int x = (int)(log( double(r-l+1) / log(2.0)));
    return min(stmin[l][x], stmin[r-(1<<x)+1][x]);
}
int main()
{
    scanf("%lld%lld", &n, &c);
    for(int i = 1;i <= n;i++){
        scanf("%lld", &stmin[i][0]);
        sum[i] = sum[i - 1] + stmin[i][0];
    }
    InitSt();
    for(int i = 1;i <= n;i++){
        dp[i] = dp[i - 1] + stmin[i][0];
        if(i >= c){
            dp[i] = min(dp[i], dp[i - c] + sum[i] - sum[i - c] - query(i - c + 1, i));
        }
    }
    printf("%lld\n", dp[n]);
    return 0;
}


#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 100100;
LL stmin[maxn][20], dp[maxn], sum[maxn];
LL n, c;
void InitSt()
{
    for(int j = 1; (1<<j) <= n; j++)
        for(int i=1; i+(1<<j) - 1 <= n; i++)
            stmin[i][j] = min(stmin[i][j-1], stmin[i+(1<<(j-1))][j-1]);
}

LL query(int l, int r)
{
    int x = (int)(log( double(r-l+1) / log(2.0)));
    return min(stmin[l][x], stmin[r-(1<<x)+1][x]);
}
int main()
{
    scanf("%lld%lld", &n, &c);
    for(int i = 1;i <= n;i++){
        scanf("%lld", &stmin[i][0]);
        sum[i] = sum[i - 1] + stmin[i][0];
    }
    InitSt();
    for(int i = 1;i <= n;i++){
        dp[i] = dp[i - 1] + stmin[i][0];
        if(i >= c)
            dp[i] = min(dp[i], dp[i - c] + sum[i] - sum[i - c] - query(i - c + 1, i));
    }
    printf("%lld\n", dp[n]);
    return 0;
}


#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=1e5+10;
int N,C,a[maxn],dp[maxn],ans,que[maxn];
int max(int a,int b){return a<b?b:a;}
int main()
{
	ans=0;
	scanf("%d%d",&N,&C);
	for(int i=1;i<=N;i++)
		scanf("%d",&a[i]);
	if(C==1)printf("0\n");
	else
	{
		memset(dp,0,sizeof(dp));
		for(int i=1;i<=N;i++)
			ans+=a[i];
//		printf("ans %d\n",ans);
		int head=0,tail=0;
		for(int i=1;i<=N;i++)
		{
			while(head<tail&&que[tail-1]-que[head]+1>=C)head++;
			while(head<tail&&a[que[tail-1]]>=a[i])tail--;
			que[tail++]=i;
			if(i>=C)
			{
				dp[i]=max(dp[i-1],dp[i-C]+a[que[head]]);
//				printf("dp[%d] %d\n",i,dp[i] );
			}
		}
		printf("%d\n",ans-dp[N]);
	}
}


#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
int N,C,a[maxn],que[maxn];
ll ans,dp[maxn];
int max(int a,int b){return a<b?b:a;}
int main()
{
	ans=0;
	scanf("%d%d",&N,&C);
	for(int i=1;i<=N;i++)
		scanf("%d",&a[i]);
	if(C==1)printf("0\n");
	else
	{
		memset(dp,0,sizeof(dp));
		for(int i=1;i<=N;i++)
			ans+=a[i];
//		printf("ans %d\n",ans);
		int head=0,tail=0;
		for(int i=1;i<=N;i++)
		{
			while(head<tail&&que[tail-1]-que[head]+1>=C)head++;
			while(head<tail&&a[que[tail-1]]>=a[i])tail--;
			que[tail++]=i;
			if(i>=C)
			{
				dp[i]=max(dp[i-1],dp[i-C]+a[que[head]]);
//				printf("dp[%d] %d\n",i,dp[i] );
			}
		}
		printf("%I64d\n",ans-dp[N]);
	}
}


#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
int N,C,a[maxn],que[maxn];
ll ans,dp[maxn];
ll max(ll a,ll b){
	if(a<b)return b;
	else return a;
}
int main()
{
	ans=0;
	scanf("%d%d",&N,&C);
	for(int i=1;i<=N;i++)
		scanf("%d",&a[i]);
	if(C==1)printf("0\n");
	else
	{
		memset(dp,0,sizeof(dp));
		for(int i=1;i<=N;i++)
			ans+=a[i];
//		printf("ans %d\n",ans);
		int head=0,tail=0;
		for(int i=1;i<=N;i++)
		{
			while(head<tail&&que[tail-1]-que[head]+1>=C)head++;
			while(head<tail&&a[que[tail-1]]>=a[i])tail--;
			que[tail++]=i;
			if(i>=C)
			{
				dp[i]=max(dp[i-1],dp[i-C]+a[que[head]]);
//				printf("dp[%d] %d\n",i,dp[i] );
			}
		}
		printf("%I64d\n",ans-dp[N]);
	}
}


#include<bits/stdc++.h>
using namespace std;

int main(){
	long long n,c;
	cin>>n>>c;
	long long a[100001];
	long long sum[100001];
	for(long long i=0;i<n;i++){
		cin>>a[i];
		sum[i]=a[i];
		if(i)
			sum[i]+=sum[i-1];
	}

	long long dp[100001];
	long long minlastc[100001]={0};
	deque<long long> q;
	for(long long i=0;i<c;i++){
		while(!(q.empty())&&a[i]<=a[q.back()])
			q.pop_back();
		q.push_back(i);
	}
	minlastc[c-1]=a[q.front()];
	for(long long i=c;i<n;i++){
		while(!(q.empty())&&q.front()<=i-c)
			q.pop_front();
		while(!(q.empty())&&a[i]<=a[q.back()])
			q.pop_back();
		q.push_back(i);
		minlastc[i]=a[q.front()];
	}
	dp[0]=a[0];
	for(long long i=1;i<n;i++){
		if(i<c-1)
			dp[i]=dp[i-1]+a[i];
		else
			dp[i]=min(dp[i-1]+a[i],dp[i-c]+sum[i]-sum[i-c]-minlastc[i]);
	}
	cout<<dp[n-1];
}


#include<bits/stdc++.h>
using namespace std;

int main(){
	long long n,c;
	cin>>n>>c;
	long long a[100001];
	long long sum[100001];
	for(long long i=0;i<n;i++){
		cin>>a[i];
		sum[i]=a[i];
		if(i)
			sum[i]+=sum[i-1];
	}

	long long dp[100001];
	long long minlastc[100001]={0};
	deque<long long> q;
	for(long long i=0;i<c;i++){
		while(!(q.empty())&&a[i]<=a[q.back()])
			q.pop_back();
		q.push_back(i);
	}
	minlastc[c-1]=a[q.front()];
	for(long long i=c;i<n;i++){
		while(!(q.empty())&&q.front()<=i-c)
			q.pop_front();
		while(!(q.empty())&&a[i]<=a[q.back()])
			q.pop_back();
		q.push_back(i);
		minlastc[i]=a[q.front()];
	}
	dp[0]=a[0];
	for(long long i=1;i<n;i++){
		if(i<c-1)
			dp[i]=dp[i-1]+a[i];
		else
			dp[i]=min(dp[i-1]+a[i],dp[i-c]+sum[i]-sum[i-c]-minlastc[i]);
	}
	if(c!=1)
	cout<<dp[n-1];
	else
		cout<<0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> ii;
#define X first
#define Y second
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define FORR(i,a,b) for(int i=a-1;i>=b;i--)
#define FILL(a,x) memset(a,x,sizeof(a))
#define mp make_pair
#define pb push_back
#define sz(a) int((a).size())
#define debug(ccc)  cout << #ccc << " = " << ccc << endl;
#define present(c,x) ((c).find(x) != (c).end())
const int mod = 10e9+7;
const int N = 1e5 + 10;
ll inp[N];
ll dp[N];
multiset<ll> mymultiset;
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    ll n, c;
    cin >> n >> c;
    FOR(i,0,n) {
        cin >> inp[i];
    }

    dp[0] = inp[0];
    if(c==1)
        dp[0] = 0;
    ll sum_c = inp[0];
    mymultiset.insert(inp[0]);
    FOR(i, 1, n)
    {
        dp[i] = dp[i - 1] + inp[i];
        if (i < c - 1) {
            sum_c += inp[i];
            mymultiset.insert(inp[i]);
        }
        if (i >= c - 1)
        {
            sum_c += inp[i];
            mymultiset.insert(inp[i]);
            // debug(*mymultiset.begin());
            if (i == c - 1)
                dp[i] = min(dp[i], (sum_c - *(mymultiset.begin())));
            else
                dp[i] = min(dp[i], (dp[i - c] + sum_c - *(mymultiset.begin())));
            sum_c -= inp[i - c + 1];
            mymultiset.erase(mymultiset.find(inp[i-c+1]));
            // debug(i);
            // debug(dp[i]);
            // cout << endl;
        }
    }
    cout << dp[n - 1] << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> ii;
#define X first
#define Y second
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define FORR(i,a,b) for(int i=a-1;i>=b;i--)
#define FILL(a,x) memset(a,x,sizeof(a))
#define mp make_pair
#define pb push_back
#define sz(a) int((a).size())
#define debug(ccc)  cout << #ccc << " = " << ccc << endl;
#define present(c,x) ((c).find(x) != (c).end())
const int mod = 10e9+7;
const int N = 1e5 + 10;
ll inp[N];
ll dp[N];
multiset<ll> mymultiset;
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    ll n, c;
    cin >> n >> c;
    FOR(i,1,n+1) {
        cin >> inp[i];
    }
    dp[0] = 0;
    ll sum_c = 0;
    FOR(i,1,c) {
        dp[i] = dp[i - 1] + inp[i];
        sum_c += inp[i];
        mymultiset.insert(inp[i]);
    }
    FOR(i, c, n+1)
    {
        dp[i] = dp[i - 1] + inp[i];
        sum_c += inp[i];
        mymultiset.insert(inp[i]);
        dp[i] = min(dp[i], (dp[i - c] + sum_c - *(mymultiset.begin())));
        sum_c -= inp[i - c + 1];
        mymultiset.erase(mymultiset.find(inp[i-c+1]));
    }
    cout << dp[n] << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> ii;
#define X first
#define Y second
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define FORR(i,a,b) for(int i=a-1;i>=b;i--)
#define FILL(a,x) memset(a,x,sizeof(a))
#define mp make_pair
#define pb push_back
#define sz(a) int((a).size())
#define debug(ccc)  cout << #ccc << " = " << ccc << endl;
#define present(c,x) ((c).find(x) != (c).end())
const int mod = 10e9+7;
const int N = 1e5 + 10;
ll inp[N];
ll dp[N];
multiset<ll> mymultiset;
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    ll n, c;
    cin >> n >> c;
    FOR(i,0,n) {
        cin >> inp[i];
    }

    dp[0] = inp[0];
    if(c==1) {
        cout << "0\n";
        exit(0);
    }
    ll sum_c = inp[0];
    mymultiset.insert(inp[0]);
    FOR(i, 1, n)
    {
        dp[i] = dp[i - 1] + inp[i];
        if (i < c - 1) {
            sum_c += inp[i];
            mymultiset.insert(inp[i]);
        }
        if (i >= c - 1)
        {
            sum_c += inp[i];
            mymultiset.insert(inp[i]);
            // debug(*mymultiset.begin());
            if (i == c - 1)
                dp[i] = min(dp[i], (sum_c - *(mymultiset.begin())));
            else
                dp[i] = min(dp[i], (dp[i - c] + sum_c - *(mymultiset.begin())));
            sum_c -= inp[i - c + 1];
            mymultiset.erase(mymultiset.find(inp[i-c+1]));
            // debug(i);
            // debug(dp[i]);
            // cout << endl;
        }
    }
    cout << dp[n - 1] << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> ii;
#define X first
#define Y second
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define FORR(i,a,b) for(int i=a-1;i>=b;i--)
#define FILL(a,x) memset(a,x,sizeof(a))
#define mp make_pair
#define pb push_back
#define sz(a) int((a).size())
#define debug(ccc)  cout << #ccc << " = " << ccc << endl;
#define present(c,x) ((c).find(x) != (c).end())
const int mod = 10e9+7;
const int N = 1e5 + 10;
ll inp[N], cum[N];
ll dp[N];
multiset<int> mymultiset;
multiset<int>::iterator it;
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    ll n, c;
    cin >> n >> c;
    FOR(i,0,n) {
        cin >> inp[i];
        cum[i] = inp[i];
        if (i > 0)
            cum[i] += cum[i - 1];
    }
    dp[0] = inp[0];
    ll sum_c = inp[0];
    mymultiset.insert(inp[0]);
    FOR(i, 1, n)
    {
        dp[i] = dp[i - 1] + inp[i];
        if (i < c - 1) {
            sum_c += inp[i];
            mymultiset.insert(inp[i]);
        }
        if (i >= c - 1)
        {
            sum_c += inp[i];
            mymultiset.insert(inp[i]);
            // debug(*mymultiset.begin());
            if (i == c - 1)
                dp[i] = min(dp[i], (cum[i] - *(mymultiset.begin())));
            else
                dp[i] = min(dp[i], (dp[i - c] + cum[i] - cum[i-c] - *(mymultiset.begin())));
            sum_c -= inp[i - c + 1];
            mymultiset.erase(inp[i-c+1]);
            // debug(i);
            // debug(dp[i]);
            // cout << endl;
        }
    }
    cout << dp[n - 1] << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> ii;
#define X first
#define Y second
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define FORR(i,a,b) for(int i=a-1;i>=b;i--)
#define FILL(a,x) memset(a,x,sizeof(a))
#define mp make_pair
#define pb push_back
#define sz(a) int((a).size())
#define debug(ccc)  cout << #ccc << " = " << ccc << endl;
#define present(c,x) ((c).find(x) != (c).end())
const int mod = 10e9+7;
const int N = 1e5 + 10;
ll inp[N];
ll dp[N];
multiset<ll> mymultiset;
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    ll n, c;
    cin >> n >> c;
    FOR(i,0,n) {
        cin >> inp[i];
    }
    dp[0] = inp[0];
    ll sum_c = inp[0];
    mymultiset.insert(inp[0]);
    FOR(i, 1, n)
    {
        dp[i] = dp[i - 1] + inp[i];
        if (i < c - 1) {
            sum_c += inp[i];
            mymultiset.insert(inp[i]);
        }
        if (i >= c - 1)
        {
            sum_c += inp[i];
            mymultiset.insert(inp[i]);
            // debug(*mymultiset.begin());
            if (i == c - 1)
                dp[i] = min(dp[i], (sum_c - *(mymultiset.begin())));
            else
                dp[i] = min(dp[i], (dp[i - c] + sum_c - *(mymultiset.begin())));
            sum_c -= inp[i - c + 1];
            mymultiset.erase(inp[i-c+1]);
            // debug(i);
            // debug(dp[i]);
            // cout << endl;
        }
    }
    cout << dp[n - 1] << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> ii;
#define X first
#define Y second
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define FORR(i,a,b) for(int i=a-1;i>=b;i--)
#define FILL(a,x) memset(a,x,sizeof(a))
#define mp make_pair
#define pb push_back
#define sz(a) int((a).size())
#define debug(ccc)  cout << #ccc << " = " << ccc << endl;
#define present(c,x) ((c).find(x) != (c).end())
const int mod = 10e9+7;
const int N = 1e5 + 10;
ll inp[N];
ll dp[N];
multiset<ll> mymultiset;
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    ll n, c;
    cin >> n >> c;
    FOR(i,0,n) {
        cin >> inp[i];
    }
    dp[0] = inp[0];
    ll sum_c = inp[0];
    mymultiset.insert(inp[0]);
    FOR(i, 1, n)
    {
        dp[i] = dp[i - 1] + inp[i];
        if (i < c - 1) {
            sum_c += inp[i];
            mymultiset.insert(inp[i]);
        }
        if (i >= c - 1)
        {
            sum_c += inp[i];
            mymultiset.insert(inp[i]);
            // debug(*mymultiset.begin());
            if (i == c - 1)
                dp[i] = min(dp[i], (sum_c - *(mymultiset.begin())));
            else
                dp[i] = min(dp[i], (dp[i - c] + sum_c - *(mymultiset.begin())));
            sum_c -= inp[i - c + 1];
            mymultiset.erase(mymultiset.find(inp[i-c+1]));
            // debug(i);
            // debug(dp[i]);
            // cout << endl;
        }
    }
    cout << dp[n - 1] << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> ii;
#define X first
#define Y second
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define FORR(i,a,b) for(int i=a-1;i>=b;i--)
#define FILL(a,x) memset(a,x,sizeof(a))
#define mp make_pair
#define pb push_back
#define sz(a) int((a).size())
#define debug(ccc)  cout << #ccc << " = " << ccc << endl;
#define present(c,x) ((c).find(x) != (c).end())
const int mod = 10e9+7;
ll inp[100010];
ll dp[100010];
multiset<int> mymultiset;
multiset<int>::iterator it;
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    ll n, c;
    cin >> n >> c;
    FOR(i,0,n) {
        cin >> inp[i];
    }
    dp[0] = inp[0];
    ll sum_c = inp[0];
    mymultiset.insert(inp[0]);
    FOR(i, 1, n)
    {
        dp[i] = dp[i - 1] + inp[i];
        if (i < c - 1) {
            sum_c += inp[i];
            mymultiset.insert(inp[i]);
        }
        if (i >= c - 1)
        {
            sum_c += inp[i];
            mymultiset.insert(inp[i]);
            // debug(*mymultiset.begin());
            if (i == c - 1)
                dp[i] = min(dp[i], (sum_c - *(mymultiset.begin())));
            else
                dp[i] = min(dp[i], (dp[i - c] + sum_c - *(mymultiset.begin())));
            sum_c -= inp[i - c + 1];
            mymultiset.erase(inp[i-c+1]);
            // debug(i);
            // debug(dp[i]);
            // cout << endl;
        }
    }
    cout << dp[n - 1] << endl;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int a[100010],n,c;
long long dp[100010],sum[100010];
multiset<int>st;

int main()
{
	scanf("%d%d",&n,&c);
	for(int i=1; i<=n; i++)
	{
		scanf("%d",&a[i]);
		dp[i]=sum[i]=sum[i-1]+a[i];
	}
	for(int i=1; i<c; i++)
	{
		st.insert(a[i]);
	}
	for(int i=c; i<=n; i++)
	{
		st.insert(a[i]);
		dp[i]=min(dp[i-1]+a[i],dp[i-c]+sum[i]-sum[i-c]-*st.begin());
		st.erase(st.find(a[i-c+1]));
	}
	printf("%I64d",dp[n]);
	return 0;
}


#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long

using namespace std;

int n;
int minsum[100010][30];
int a[100010];
LL dp[100010];

void RMQ()
{
    int i,j;
    for(i=1;i<=n;i++)
    {
        minsum[i][0] =  a[i];//预处理第一层
    }
    for(j=1;(1<<j)<=n;j++)
        for(i=1;i + (1<<j) - 1 <= n;i++)
            {
                minsum[i][j] = min(minsum[i][j - 1], minsum[i + (1 << (j - 1))][j - 1]);
            }
}

int query(int L,int R)
{
    int k = (int)(log(R - L + 1.0)/log(2.0));
    int minn = min(minsum[L][k],minsum[R-(1<<k)+1][k]);
    return minn;
}

void add(int k,LL num)
{
    while(k<=n)
    {
        dp[k] = max(dp[k],num);
        k+=k&-k;
    }
}

LL read(int k)
{
    LL sum=0;
    while(k)
    {
        sum = max(sum,dp[k]);
        k-=k&-k;
    }
    return sum;
}
int main(void)
{
    int k,i,j;
    while(scanf("%d%d",&n,&k)==2)
    {
        memset(dp,0,sizeof(dp));
        LL sum = 0;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            sum += a[i];
        }
        RMQ();
        if(k == 1)
        {
            printf("0\n");
            continue;
        }
        dp[k] = query(1,k);
        for(i=k+1;i<=n;i++)
        {
            add(i,read(i-k) + query(i-k+1,i));
        }
        cout << sum - read(n) << endl;
    }

    return 0;
}


#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long

using namespace std;

int n;
int minsum[100010][30];
int a[100010];
LL dp[100010];

void RMQ()
{
    int i,j;
    for(i=1;i<=n;i++)
    {
        minsum[i][0] =  a[i];//预处理第一层
    }
    for(j=1;(1<<j)<=n;j++)
        for(i=1;i + (1<<j) - 1 <= n;i++)
            {
                minsum[i][j] = min(minsum[i][j - 1], minsum[i + (1 << (j - 1))][j - 1]);
            }
}

int query(int L,int R)
{
    int k = (int)(log(R - L + 1.0)/log(2.0));
    int minn = min(minsum[L][k],minsum[R-(1<<k)+1][k]);
    return minn;
}

void update(int k,LL num)
{
    while(k<=n)
    {
        dp[k] = max(dp[k],num);
        k+=k&-k;
    }
}

LL read(int k)
{
    LL sum=0;
    while(k)
    {
        sum = max(sum,dp[k]);
        k-=k&-k;
    }
    return sum;
}
int main(void)
{
    int c,i,j;
    while(scanf("%d%d",&n,&c)==2)
    {
        memset(dp,0,sizeof(dp));
        LL sum = 0;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            sum += a[i];
        }
        RMQ();
        if(c == 1)
        {
            printf("0\n");
            continue;
        }
        dp[c] = query(1,c);
        for(i=c+1;i<=n;i++)
        {
            dp[i] = max(dp[i-1],dp[i-c] + query(i-c+1,i));
        }
        cout << sum - dp[n] << endl;
    }

    return 0;
}


#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long

using namespace std;

int n;
int minsum[100010][30];
int a[100010];
LL dp[100010];

void RMQ()
{
    int i,j;
    for(i=1;i<=n;i++)
    {
        minsum[i][0] =  a[i];//预处理第一层
    }
    for(j=1;(1<<j)<=n;j++)
        for(i=1;i + (1<<j) - 1 <= n;i++)
            {
                minsum[i][j] = min(minsum[i][j - 1], minsum[i + (1 << (j - 1))][j - 1]);
            }
}

int query(int L,int R)
{
    int k = (int)(log(R - L + 1.0)/log(2.0));
    int minn = min(minsum[L][k],minsum[R-(1<<k)+1][k]);
    return minn;
}

void update(int k,LL num)
{
    while(k<=n)
    {
        dp[k] = max(dp[k],num);
        k+=k&-k;
    }
}

LL read(int k)
{
    LL sum=0;
    while(k)
    {
        sum = max(sum,dp[k]);
        k-=k&-k;
    }
    return sum;
}
int main(void)
{
    int k,i,j;
    while(scanf("%d%d",&n,&k)==2)
    {
        memset(dp,0,sizeof(dp));
        LL sum = 0;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            sum += a[i];
        }
        RMQ();
        if(k == 1)
        {
            printf("0\n");
            continue;
        }
        update(k,query(1,k));
        //dp[k] = query(1,k);
        for(i=k+1;i<=n;i++)
        {
            update(i,read(i-k) + query(i-k+1,i));
        }
        cout << sum - read(n) << endl;
    }

    return 0;
}


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdio.h>
#define L(x) (x << 1)//�����
#define R(x) (x << 1 | 1)//�Ҷ���
#define sz(x) (tree[x].r - tree[x].l + 1)//�����С
using namespace std;
typedef long long ll;
const int MAXN = 100005;
int n,m,a,b,c,num[MAXN];
ll sum[MAXN];
ll dp[MAXN];
// l == ��r == �ң�p == now;
struct tree{
    int l,r;//�޸����
    int min;//��ֵ���
}tree[MAXN << 2];

//����·��� p == now
void update(int p){
    tree[p].min = min(tree[L(p)].min,tree[R(p)].min);
}
//�򵥵Ľ���
void build(int l,int r,int p){
    tree[p].l = l,tree[p].r = r;
    if(l == r){
        tree[p].min=num[l];
        return;
    }
    int mid = (tree[p].l + tree[p].r) >> 1;
    build(l,mid,L(p));  build(mid + 1,r,R(p));
    update(p);
}

//������Сֵ
int ask_min(int l,int r,int p){
    if(l <= tree[p].l && tree[p].r <= r)
        return tree[p].min;
    int ans = 2333333,mid = (tree[p].l + tree[p].r) >> 1;
    if(l <= mid)    ans = min(ans,ask_min(l,r,L(p)));
    if(mid < r)     ans = min(ans,ask_min(l,r,R(p)));
    update(p);      return ans;
}

int main(){
	memset(dp,0,sizeof(dp));
    memset(num,0,sizeof(num));
    memset(sum,0,sizeof(sum));
    scanf("%d",&n);
    scanf("%d",&c);
    for(int i = 1; i <= n; i ++)
    {
        scanf("%d",&num[i]);
        dp[i]=sum[i]=sum[i-1]+num[i];
    }
    build(1,n,1);
	for(int i=c;i<=n;i++)
	{
		dp[i]=min(dp[i-1]+num[i],dp[i-c]+sum[i]-sum[i-c]-ask_min(i-c+1,i,1));
	}
	printf("%I64d\n",dp[n]);
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdio.h>
#define L(x) (x << 1)//�����
#define R(x) (x << 1 | 1)//�Ҷ���
#define sz(x) (tree[x].r - tree[x].l + 1)//�����С
using namespace std;
typedef long long ll;
const int MAXN = 100005;
const int inf=0x3f3f3f3f;
ll n,m,a,b,c,num[MAXN];
ll sum[MAXN];
ll dp[MAXN];
// l == ��r == �ң�p == now;
struct tree{
    int l,r;//�޸����
    int min;//��ֵ���
}tree[MAXN << 2];

//����·��� p == now
void update(int p){
    tree[p].min = min(tree[L(p)].min,tree[R(p)].min);
}
//�򵥵Ľ���
void build(int l,int r,int p){
    tree[p].l = l,tree[p].r = r;
    if(l == r){
        tree[p].min=num[l];
        return;
    }
    int mid = (tree[p].l + tree[p].r) >> 1;
    build(l,mid,L(p));  build(mid + 1,r,R(p));
    update(p);
}

//������Сֵ
int ask_min(int l,int r,int p){
    if(l <= tree[p].l && tree[p].r <= r)
        return tree[p].min;
    int ans = inf,mid = (tree[p].l + tree[p].r) >> 1;
    if(l <= mid)    ans = min(ans,ask_min(l,r,L(p)));
    if(mid < r)     ans = min(ans,ask_min(l,r,R(p)));
    update(p);      return ans;
}

int main(){
	memset(dp,0,sizeof(dp));
    memset(num,0,sizeof(num));
    memset(sum,0,sizeof(sum));
    scanf("%d",&n);
    scanf("%d",&c);
    for(int i = 1; i <= n; i ++)
    {
        scanf("%d",&num[i]);
        dp[i]=sum[i]=sum[i-1]+num[i];
    }
    build(1,n,1);
	for(int i=c;i<=n;i++)
	{
		dp[i]=min(dp[i-1]+num[i],dp[i-c]+sum[i]-sum[i-c]-ask_min(i-c+1,i,1));
	}
	printf("%I64d\n",dp[n]);
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdio.h>
#define L(x) (x << 1)//�����
#define R(x) (x << 1 | 1)//�Ҷ���
#define sz(x) (tree[x].r - tree[x].l + 1)//�����С
using namespace std;
typedef long long ll;
const int MAXN = 100005;
int n,m,a,b,c,num[MAXN];
ll sum[MAXN];
ll dp[MAXN];
// l == ��r == �ң�p == now;
struct tree{
    int l,r;//�޸����
    int min;//��ֵ���
}tree[MAXN << 2];

//����·��� p == now
void update(int p){
    tree[p].min = min(tree[L(p)].min,tree[R(p)].min);
}
//�򵥵Ľ���
void build(int l,int r,int p){
    tree[p].l = l,tree[p].r = r;
    if(l == r){
        tree[p].min=num[l];
        return;
    }
    int mid = (tree[p].l + tree[p].r) >> 1;
    build(l,mid,L(p));  build(mid + 1,r,R(p));
    update(p);
}

//������Сֵ
int ask_min(int l,int r,int p){
    if(l <= tree[p].l && tree[p].r <= r)
        return tree[p].min;
    int ans = 2333333,mid = (tree[p].l + tree[p].r) >> 1;
    if(l <= mid)    ans = min(ans,ask_min(l,r,L(p)));
    if(mid < r)     ans = min(ans,ask_min(l,r,R(p)));
    update(p);      return ans;
}

int main(){
    memset(num,0,sizeof(num));
    memset(sum,0,sizeof(sum));
    scanf("%d",&n);
    scanf("%d",&c);
    for(int i = 1; i <= n; i ++)
    {
        scanf("%d",&num[i]);
        sum[i]=sum[i-1]+num[i];
    }
    build(1,n,1);
	memset(dp,0,sizeof(dp));
    for(int i=1;i<c;i++)
    {
    	dp[i]=dp[i-1]+num[i];
	}
	if(n>=c)
	{
		dp[c]=min(dp[c-1]+num[c],sum[c]-ask_min(1,c,1));
	}
	for(int i=c+1;i<=n;i++)
	{
		dp[i]=min(dp[i-1]+num[i],dp[i-c]+sum[i]-sum[i-c]-ask_min(i-c+1,i,1));
	}
	printf("%I64d\n",dp[n]);
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
using namespace std;
#define MN 750
#define MX 10000000
#define MV 1500
#define ME 300000
#define S MV+1
#define T MV+2
#define INF 0x7FFFFFFF
namespace flow
{
struct edge{int nx,t,w;}e[ME*2+5];
int h[MV+5],en=1,d[MV+5],c[MV+5],q[MV+5],qn;
inline void ins(int x,int y,int w)
{
	e[++en]=(edge){h[x],y,w};h[x]=en;
	e[++en]=(edge){h[y],x,0};h[y]=en;
}
bool bfs()
{
	int i,j;memset(d,0,sizeof(d));
	for(d[q[i=qn=0]=S]=1;i<=qn;++i)for(j=c[q[i]]=h[q[i]];j;j=e[j].nx)
		if(e[j].w&&!d[e[j].t])d[q[++qn]=e[j].t]=d[q[i]]+1;
	return d[T];
}
int dfs(int x,int r)
{
	if(x==T)return r;
	int k,u=0;
	for(int&i=c[x];i;i=e[i].nx)if(e[i].w&&d[e[i].t]==d[x]+1)
	{
		k=dfs(e[i].t,min(e[i].w,r-u));
		e[i].w-=k;e[i^1].w+=k;u+=k;
		if(u==r)return u;
	}
	return d[x]=0,u;
}
}
char s[MX+5];
int tn,c[MX+5][2],f[MX+5],q[MX+5],qn,t[MX+5];
bitset<MN+5> b[MN+5],bb;
struct edge{int nx,t;}E[MX*2+5];
int v[MX+5],fc[MX+5],en,z[MX+5],zn;
inline void ins(int*h,int x,int y){E[++en]=(edge){h[x],y};h[x]=en;}
int main()
{
	int n,i,j,x,y,ans;
	scanf("%d",&n);ans=n;
	for(i=1;i<=n;++i)
	{
		scanf("%s",s);
		for(j=x=0;s[j];ins(v,x=c[x][s[j++]-'a'],i))
			if(!c[x][s[j]-'a'])c[x][s[j]-'a']=++tn;
		t[x]=i;
	}
	for(i=0;i<2;++i)if(c[0][i])ins(fc,0,q[++qn]=c[0][i]);
	for(i=1;i<=qn;++i)for(j=0;j<2;++j)if(c[q[i]][j])
	{
		for(x=f[q[i]];x&&!c[x][j];x=f[x]);
		f[c[q[i]][j]]=c[x][j];
		ins(fc,c[x][j],c[q[i]][j]);
		q[++qn]=c[q[i]][j];
	}
	f[qn=0]=-1;
	while(zn>=0)
		if(x=z[zn],f[zn]<0)
		{
			if(t[x])q[++qn]=t[x];
			if(qn)for(int i=v[x];i;i=E[i].nx)b[E[i].t][q[qn]]=b[E[i].t][q[qn-1]]=1;
			f[zn]=fc[x];
		}
		else if(f[zn])
		{
			z[zn+1]=E[f[zn]].t;f[zn+1]=-1;
			f[zn]=E[f[zn]].nx;++zn;
		}
		else{if(t[x])--qn;--zn;}
	for(x=1;x<=n;++x)for(i=1;i<=n;++i)if(b[i][x])b[i]|=b[x];
	using namespace flow;
	for(i=1;i<=n;++i)
	{
		ins(S,i,1);ins(i+n,T,1);
		for(j=1;j<=n;++j)if(i!=j&&b[i][j])ins(i,j+n,1);
	}
	while(bfs())ans-=dfs(S,INF);
	printf("%d\n",ans);
	for(i=1;i<=n;++i)for(t[i]=0,j=h[i];j;j=e[j].nx)if(!e[j].w&&e[j].t!=S)t[i]=e[j].t-n;
	for(i=h[T];i;i=e[i].nx)if(!e[i].w)for(j=e[i].t-n;;j=t[j])
	{
		for(x=e[i].nx;x;x=e[x].nx)if(!e[x].w)
		{
			for(y=e[x].t-n;y;y=t[y])if(!bb[y]&&!b[j][y]&&!b[y][j])break;
			if(!y)break;
		}
		if(x)continue;
		printf("%d ",j);
		for(x=1;x<=n;++x)bb[x]=bb[x]|b[j][x]|b[x][j];
		break;
	}
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
using namespace std;
#define MN 750
#define MX 10000000
#define MV 1500
#define ME 300000
#define S MV+1
#define T MV+2
#define INF 0x7FFFFFFF
namespace flow
{
struct edge{int nx,t,w;}e[ME*2+5];
int h[MV+5],en=1,d[MV+5],c[MV+5],q[MV+5],qn;
inline void ins(int x,int y,int w)
{
	e[++en]=(edge){h[x],y,w};h[x]=en;
	e[++en]=(edge){h[y],x,0};h[y]=en;
}
bool bfs()
{
	int i,j;memset(d,0,sizeof(d));
	for(d[q[i=qn=0]=S]=1;i<=qn;++i)for(j=c[q[i]]=h[q[i]];j;j=e[j].nx)
		if(e[j].w&&!d[e[j].t])d[q[++qn]=e[j].t]=d[q[i]]+1;
	return d[T];
}
int dfs(int x,int r)
{
	if(x==T)return r;
	int k,u=0;
	for(int&i=c[x];i;i=e[i].nx)if(e[i].w&&d[e[i].t]==d[x]+1)
	{
		k=dfs(e[i].t,min(e[i].w,r-u));
		e[i].w-=k;e[i^1].w+=k;u+=k;
		if(u==r)return u;
	}
	return d[x]=0,u;
}
}
char s[MX+5];
int tn,c[MX+5][2],f[MX+5],q[MX+5],qn,t[MX+5];
bitset<MN+5> b[MN+5],bb;
struct edge{int nx,t;}E[MX*2+5];
int v[MX+5],fc[MX+5],en,z[MX+5],zn;
inline void ins(int*h,int x,int y){E[++en]=(edge){h[x],y};h[x]=en;}
int main()
{
	int n,i,j,x,y,ans;
	scanf("%d",&n);ans=n;
	for(i=1;i<=n;++i)
	{
		scanf("%s",s);
		for(j=x=0;s[j];ins(v,x=c[x][s[j++]-'a'],i))
			if(!c[x][s[j]-'a'])c[x][s[j]-'a']=++tn;
		t[x]=i;
	}
	for(i=0;i<2;++i)if(c[0][i])ins(fc,0,q[++qn]=c[0][i]);
	for(i=1;i<=qn;++i)for(j=0;j<2;++j)if(c[q[i]][j])
	{
		for(x=f[q[i]];x&&!c[x][j];x=f[x]);
		f[c[q[i]][j]]=c[x][j];
		ins(fc,c[x][j],c[q[i]][j]);
		q[++qn]=c[q[i]][j];
	}
	f[qn=0]=-1;
	while(zn>=0)
		if(x=z[zn],f[zn]<0)
		{
			if(t[x])q[++qn]=t[x];
			if(qn)for(int i=v[x];i;i=E[i].nx)b[E[i].t][q[qn]]=b[E[i].t][q[qn-1]]=1;
			f[zn]=fc[x];
		}
		else if(f[zn])
		{
			z[zn+1]=E[f[zn]].t;f[zn+1]=-1;
			f[zn]=E[f[zn]].nx;++zn;
		}
		else{if(t[x])--qn;--zn;}
	for(x=1;x<=n;++x)for(i=1;i<=n;++i)if(b[i][x])b[i]|=b[x];
	using namespace flow;
	for(i=1;i<=n;++i)
	{
		ins(S,i,1);ins(i+n,T,1);
		for(j=1;j<=n;++j)if(i!=j&&b[i][j])ins(i,j+n,1);
	}
	while(bfs())ans-=dfs(S,INF);
	printf("%d\n",ans);
	for(i=1;i<=n;++i)for(t[i]=0,j=h[i];j;j=e[j].nx)if(!e[j].w&&e[j].t!=S)t[i]=e[j].t-n;
	for(i=h[T];i;i=e[i].nx)if(!e[i].w)for(j=e[i].t-n;;j=t[j])if(!bb[j])
	{
		for(x=e[i].nx;x;x=e[x].nx)if(!e[x].w)
		{
			for(y=e[x].t-n;y;y=t[y])if(!bb[y]&&!b[j][y]&&!b[y][j])break;
			if(!y)break;
		}
		if(x)continue;
		printf("%d ",j);
		for(x=1;x<=n;++x)bb[x]=bb[x]|b[j][x]|b[x][j];
		break;
	}
}