/****************************************************************
*	Author: huhao
*	Email: 826538400@qq.com
*	Create time: 2020-03-25 16:54:48
****************************************************************/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define fr(i,a,b) for(int i=(a),end_##i=(b);i<=end_##i;i++)
#define fd(i,a,b) for(int i=(a),end_##i=(b);i>=end_##i;i--)
int read()
{
	int r=0,t=1,c=getchar();
	while(c<'0'||c>'9')
	{
		t=c=='-'?-1:1;
		c=getchar();
	}
	while(c>='0'&&c<='9')
	{
		r=(r<<3)+(r<<1)+(c^48);
		c=getchar();
	}
	return r*t;
}
#define i64 long long
#include<set>
#define pii std::pair<int,int>
const int N=200010;
i64 n,pre[N],suf[N],k,a[N],l[N],r[N],ans,s[N],l_[N],r_[N];
std::set<pii> S;
int q[N],m,m_;
int main()
{
	n=read(); k=read();
	fr(i,1,n) a[i]=read();
	fr(i,1,n) if(a[i]!=a[i-1]) S.insert(pii(a[i],i));
	fr(i,2,n){ pre[i]=i-1; suf[i-1]=i; }
	fr(i,1,n) l[i]=r[i]=1;
	while(!S.empty())
	{
		int u=S.begin()->second,v=u,na=a[u]+1; q[m=1]=u; S.erase(S.begin());
		suf[pre[u]]=pre[suf[v]]=0;
		while(a[suf[v]]==a[u]) q[++m]=(v=suf[v]);
		fr(i,1,m){ s[i]=s[i-1]+l[q[i]]; ans+=(l[q[i]]+s[std::max(i-k+1,0ll)])*r[q[i]]; }
//		fr(i,1,m) printf("+ %d %d\n",l[q[i]],r[q[i]]);
//		printf("+%d %d %d\n",int(m),int(na-1),int(ans));
		m_=m/k;
		if(!m_) continue;
		fr(i,1,m) s[i]=s[i-1]+l[q[i]];
		fr(i,1,m_) l_[i]=s[m-(m_-i+1)*k+1];
		fd(i,m_,2) l_[i]-=l_[i-1];
//		fr(i,1,m_) printf("%d%c",s[m-(m_-i+1)*k+1],i==end_i?'\n':' ');
		fr(i,1,m) s[i]=s[i-1]+r[q[i]];
		fr(i,1,m_) r_[i]=s[m]-s[i*k-1];
		fr(i,1,m_-1) r_[i]-=r_[i+1];
//		fr(i,1,m_) printf("- %d %d\n",l_[i],r_[i]);
		fr(i,1,m_){ l[q[i]]=l_[i]; r[q[i]]=r_[i]; a[q[i]]=na; }
		suf[pre[u]]=u; pre[suf[v]]=q[m_]; suf[q[m_]]=suf[v]; m=m_;
		fr(i,1,m){ s[i]=s[i-1]+l[q[i]]; ans-=(l[q[i]]+s[std::max(i-k+1,0ll)])*r[q[i]]; }
		if(a[pre[u]]!=na) S.insert(pii(na,u));
		if(a[suf[v]]==na) S.erase(pii(na,suf[v]));
//		printf("-%d %d %d\n",int(m),int(na-1),int(ans));
	}
	printf("%lld\n",ans);
	return 0;
}