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

typedef long long lint;
typedef pair<int,int> pii;
typedef long double louble;

#define so(x) ((int)((x).size()))
#define fi first
#define se second

template<typename T1,typename T2> inline T1 max(T1 a,T2 b){return a<b?b:a;}
template<typename T1,typename T2> inline T1 min(T1 a,T2 b){return a<b?a:b;}

namespace ae86
{
	const int bufl = 1<<15;
	
	char buf[bufl],*s=buf,*t=buf;
	
	inline int fetch()
	{
		if(s==t){t=(s=buf)+fread(buf,1,bufl,stdin);if(s==t)return EOF;}
		return *s++;
	}
	
	inline int ty()
	{
		int a=0,b=1,c=fetch();
		while(!isdigit(c))b^=c=='-',c=fetch();
		while(isdigit(c))a=a*10+c-48,c=fetch();
		return b?a:-a;
	}
}
using ae86::ty;

const int _ = 200007;

int n,lim,val[_];
pii bas[_];

inline lint finder(vector<pii> &v)
{
	lint ans=0,sum=0;
	for(int i=0,j=lim-1;j<so(v);i++,j++)sum+=v[i].fi,ans+=sum*v[j].se;
	return ans;
}

vector<pair<pii,pii>> f,g;
// <interval_L, interval_R>, <cntBeingL, cntBeingR>

int main()
{
	n=ty(),lim=ty();
	for(int i=1;i<=n;i++)val[i]=ty(),bas[i]=pii(val[i],i);

	sort(bas+1,bas+n+1);

	lint ans=n;
	int now=0,pos=1;
	
	while("pick the smallest values first")
	{
		if(f.empty()){if(pos>n)break;else now=bas[pos].fi;}
		else now++;

		while(pos<=n && bas[pos].fi==now)
			f.emplace_back(pii(bas[pos].se,bas[pos].se),pii(1,1)),pos++;
		sort(f.begin(),f.end());
		g.clear();
		
		for(int l=0,r=0;l<so(f);l=r+1)
		{
			r=l;
			while(r+1<so(f) && f[r].fi.se+1==f[r+1].fi.fi)r++;
			int len=r-l+1,llen=len/lim;
			if(llen<=0)continue;

			vector<pii> temp(len),temp1(llen),temp2(llen,pii(0,0));
			for(int i=0;i<llen;i++)
				temp1[i]=pii(f[l].fi.fi+i*lim,(i==llen-1)?f[r].fi.se:(f[l].fi.fi+i*lim+lim-1));
			for(int i=l,ll=1,rr=len;i<=r;i++,ll++,rr--)
			{
				temp[i-l]=f[i].se;
				if(ll>=lim)temp2[ll/lim-1].se+=f[i].se.se;
				if(rr>=lim)temp2[llen-rr/lim].fi+=f[i].se.fi;
			}
			ans+=finder(temp)-finder(temp2);
			for(int i=0;i<llen;i++)g.emplace_back(temp1[i],temp2[i]);
		}

		f.swap(g);
	}

	printf("%lld\n",ans);

	return 0;
}
