#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=200005;
int Left[N],Right[N],num,x,val[N],n,l,sl[N],sr[N];
ll ans;
void dec(){
	int now;
	for (now=num-1;now>0;now--)
		if (val[now]!=val[now-1])break;
	int d=num-now,v=val[now];
	ll s=0;
	for (int i=now;i<num;i++){
		ans+=(ll)Left[i]*Right[i];
		if (i-l+1>=now)s+=Left[i-l+1];
		ans+=s*Right[i];
	}
	if (d<l){
		num=now;
		while (num)dec();
		return;
	}
	int dd=d/l;
	for (int i=0;i<=dd;i++)sl[i]=sr[i]=0;
	for (int i=now;i<num;i++){
		sl[(i-now+1)/l]+=Right[i];
		sr[(num-i)/l]+=Left[i];
	}
	num=now+dd;
	for (int i=1;i<=dd;i++){
		Right[now+i-1]=sl[i];
		Left[num-i]=sr[i];
	}
	s=0;
	for (int i=now;i<num;i++){
		val[i]=v+1;
		ans-=(ll)Left[i]*Right[i];
		if (i-l+1>=now)s+=Left[i-l+1];
		ans-=s*Right[i];
	}
}
int main(){
	scanf("%d%d",&n,&l);
	while (n--){
		scanf("%d",&x);
		while (num>0){
			if (val[num-1]>=x)break;
			dec();
		}
		Left[num]=Right[num]=1;
		val[num]=x;
		num++;
	}
	while (num)dec();
	printf("%lld\n",ans);
	return 0;
}
