#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;

template<class T> inline void read(T &x){
	char ch=getchar();int f=0;x=0;
	for(;!isdigit(ch);ch=getchar())f|=(ch=='-');
	for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';
	if(f)x=-x;
}

const int N=200005,inf=1e9+99;
struct node{
	int level,mx;
	vi pre,suf;
	node(int _x=inf){
		level=_x,mx=1;
		if(_x>=0)pre=suf={1};
	}
}stk[N],emp;
int a[N],n,L,top;
ll ans;

node merge(node a[],int n){
	static int sum[N],tot[N];
	rep(i,1,n)sum[i]=sum[i-1]+a[i].mx;
	if(sum[n]<L)return emp;
	tot[n+1]=0;
	per(i,n,1)tot[i]=tot[i+1]+a[i].pre[0];
	for(int l=1,r=1,q=1;r<=n;l++)
		per(i,a[l].mx,1){
			while(l==r)r++,q=1;
			while(r<=n&&i+sum[r-1]-sum[l]+q<L){
				q++;
				if(q<=a[r].mx)continue;
				r++,q=1;
			}
			if(r<=n){
				int A=a[l].suf[i-1]-(i<a[l].mx?a[l].suf[i]:0);
				int B=a[r].pre[q-1]+tot[r+1];
				ans+=(ll)A*B;
			}
		}
	node res(-1);
	res.level=a[1].level+1;
	res.mx=sum[n]/L;
	res.pre.resize(res.mx);
	res.suf.resize(res.mx);
	for(int i=1,cnt=0;i<=n;i++)
		rep(j,1,a[i].mx)
			if(++cnt>=L){
				int tmp=a[i].pre[j-1]-(j<a[i].mx?a[i].pre[j]:0);
				res.pre[cnt/L-1]+=tmp;
			}
	for(int i=n,cnt=0;i>=1;i--)
		rep(j,1,a[i].mx)
			if(++cnt>=L){
				int tmp=a[i].suf[j-1]-(j<a[i].mx?a[i].suf[j]:0);
				res.suf[cnt/L-1]+=tmp;
			}
	per(i,res.mx-1,1){
		res.pre[i-1]+=res.pre[i];
		res.suf[i-1]+=res.suf[i];
	}
	return res;
}

int main(){
	read(n),read(L),ans=n;
	rep(i,1,n)read(a[i]);
	a[n+1]=inf;
	rep(i,1,n){
		stk[++top]=node(a[i]);
		int flag=0;
		while(flag&&stk[top].level<inf||stk[top].level<a[i+1]){
			int cur=top;
			while(cur&&stk[cur].level==stk[top].level)
				cur--;
			stk[cur+1]=merge(stk+cur,top-cur);
			top=cur+1;
			if(stk[top].level==inf)flag=1,top--;
		}
		if(flag)stk[++top].level=inf;
	}
	cout<<ans<<endl;
	return 0;
}