#include<cstdio>
#include<map>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
#define N 400600
map<int,int> tp1;
set<int> tp3;
struct sth{int l,r,lb,rb;friend bool operator <(sth a,sth b){return a.l<b.l;}};
vector<sth> v[N];
int su[N][2],vl[N][2],tp[N][2],ct,n,k,s[N],cnt;
long long as;
int main()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&s[i]);
		tp3.insert(s[i]);if(!tp1[s[i]])tp1[s[i]]=++cnt;
		v[tp1[s[i]]].push_back((sth){i,i,1,1});
	}
	int lb=*tp3.lower_bound(1);
	while(1)
	{
		int st=tp1[lb];
		sort(v[st].begin(),v[st].end());
		int sz=v[st].size(),las=v[st][0].l;
		for(int i=0;i<sz;i++)
		{
			if(ct&&v[st][i].l!=v[st][i-1].r+1)
			{
				su[ct+1][0]=su[ct+1][1]=0;
				int s1=ct/k;
				if(s1)
				{
					for(int j=1;j<=ct;j++){int r1=j+k-1;if(r1<=ct)as+=1ll*vl[j][1]*(su[ct][0]-su[r1-1][0]);}
					for(int j=1;j<s1;j++)tp[j][0]=su[(j+1)*k-1][0]-su[j*k-1][0];
					tp[s1][0]=su[ct][0]-su[s1*k-1][0];
					for(int j=1;j<s1;j++)tp[s1-j+1][1]=-su[ct-(j+1)*k+1][1]+su[ct-j*k+1][1];
					tp[1][1]=su[ct-s1*k+1][1];
					if(!tp1[lb+1])tp1[lb+1]=++cnt,tp3.insert(lb+1);
					for(int j=1;j<=s1;j++)v[tp1[lb+1]].push_back((sth){las+j-1,j==s1?v[st][i-1].r:las+j-1,tp[j][0],tp[j][1]});
					for(int j=1;j<=s1;j++)vl[j][0]=tp[j][0],vl[j][1]=tp[j][1],su[j][0]=su[j-1][0]+vl[j][0];
					for(int j=1;j<=s1;j++){int r1=j+k-1;if(r1<=s1)as-=1ll*vl[j][1]*(su[s1][0]-su[r1-1][0]);}
				}
				ct=0;las=v[st][i].l;
			}
			vl[++ct][0]=v[st][i].lb;vl[ct][1]=v[st][i].rb;
			su[ct][0]=su[ct-1][0]+vl[ct][0];su[ct][1]=su[ct-1][1]+vl[ct][1];
		}
		su[ct+1][0]=su[ct+1][1]=0;
		int s1=ct/k;
		if(s1)
		{
			for(int j=1;j<=ct;j++){int r1=j+k-1;if(r1<=ct)as+=1ll*vl[j][1]*(su[ct][0]-su[r1-1][0]);}
			for(int j=1;j<s1;j++)tp[j][0]=su[(j+1)*k-1][0]-su[j*k-1][0];
			tp[s1][0]=su[ct][0]-su[s1*k-1][0];
			for(int j=1;j<s1;j++)tp[s1-j+1][1]=-su[ct-(j+1)*k+1][1]+su[ct-j*k+1][1];
			tp[1][1]=su[ct-s1*k+1][1];
			if(!tp1[lb+1])tp1[lb+1]=++cnt,tp3.insert(lb+1);
			for(int j=1;j<=s1;j++)v[tp1[lb+1]].push_back((sth){las+j-1,j==s1?v[st][sz-1].r:las+j-1,tp[j][0],tp[j][1]});
			for(int j=1;j<=s1;j++)vl[j][0]=tp[j][0],vl[j][1]=tp[j][1],su[j][0]=su[j-1][0]+vl[j][0];
			for(int j=1;j<=s1;j++){int r1=j+k-1;if(r1<=s1)as-=1ll*vl[j][1]*(su[s1][0]-su[r1-1][0]);}
		}
		ct=0;
		set<int>::iterator it=tp3.lower_bound(lb+1);
		if(it==tp3.end())break;
		lb=*it;
	}
	printf("%lld\n",as+n);
}