Problem:
Score : 1800 points

Problem Statement

For a sequence S of positive integers and positive integers k and l, S is said to belong to level (k,l) when one of the following conditions is satisfied:

The length of S is 1, and its only element is k.

There exist sequences T_1,T_2,...,T_m (m \geq l) belonging to level (k-1,l) such that the concatenation of T_1,T_2,...,T_m in this order coincides with S.

Note that the second condition has no effect when k=1, that is, a sequence belongs to level (1,l) only if the first condition is satisfied.

Given are a sequence of positive integers A_1,A_2,...,A_N and a positive integer L.
Find the number of subsequences A_i,A_{i+1},...,A_j (1 \leq i \leq j \leq N) that satisfy the following condition:

There exists a positive integer K such that the sequence A_i,A_{i+1},...,A_j belongs to level (K,L).

Constraints

1 \leq N \leq 2 \times 10^5

2 \leq L \leq N

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N L
A_1 A_2 ... A_N

Output

Print the number of subsequences A_i,A_{i+1},...,A_j (1 \leq i \leq j \leq N) that satisfy the condition.

Sample Input 1

9 3
2 1 1 1 1 1 1 2 3

Sample Output 1

22

For example, both of the sequences (1,1,1) and (2) belong to level (2,3), so the sequence (2,1,1,1,1,1,1) belong to level (3,3).

Sample Input 2

9 2
2 1 1 1 1 1 1 2 3

Sample Output 2

41

Sample Input 3

15 3
4 3 2 1 1 1 2 3 2 2 1 1 1 2 2

Sample Output 3

31

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
using namespace std;
namespace io{
	const int l=1<<20;
	char buf[l],*s,*t,c;
	inline void gc(){
		if(s==t){
			t=(s=buf)+fread(buf,1,l,stdin);
			c=s==t?EOF:*s++;
		}else c=*s++;
	}
	template<class IT>inline void gi(IT &x){
		x=0;gc();while(c<'0'||c>'9')gc();
		while('0'<=c&&c<='9'){x=(x<<1)+(x<<3)+(c^48);gc();}
	}
	char buf0[20];int a;
	template<class IT>inline void pi(IT x){
		if(x<0){putchar('-');x=-x;}
		do buf0[++a]=x%10+48;while(x/=10);
		while(a)putchar(buf0[a--]);
		putchar('\n');
	}
};
using io::gi;
using io::pi;
typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<int,ll > pil;
typedef pair<ll ,int> pli;
typedef pair<ll ,ll > pll;
#define pque priority_queue
#define rep(i,l,r) for(i=(l);i<=(r);++i)
#define per(i,l,r) for(i=(l);i>=(r);--i)
#define REP(i,l,r) for(i=(l);i< (r);++i)
#define PER(i,l,r) for(i=(l);i> (r);--i)
#define mp make_pair
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define fi first
#define se second
template<class IT>inline void cmax(IT &a,IT b){if(a<b)a=b;}
template<class IT>inline void cmin(IT &a,IT b){if(b<a)a=b;}
const int N=200005;
struct node{
	int a,L,R,l,r;
	inline bool operator<(const node &x)const{
		return a<x.a||(a==x.a&&L<x.L);
	}
}z[N],x[N],y[N];ll ans,ant;
inline void calcs(int i,int j,int r){
	ll t=0ll;
	for(;j<=r;++i,++j)ans+=(t+=x[i].l)*x[j].r;
}
inline void calct(int i,int j,int r){
	ll t=0ll;
	for(;j<=r;++i,++j)ant+=(t+=z[i].l)*z[j].r;
}
inline void dbg(int X){
	int i;
	rep(i,1,X)printf("a=%d L=%d R=%d l=%d r=%d\n",x[i].a,x[i].L,x[i].R,x[i].l,x[i].r);puts("X");
}
int main(){
//	freopen("f.in","r",stdin);
	int n,m,mn,X=0,Y=1,Z,T,i,j,k,l,c;
	scanf("%d%d",&n,&m);ans=n;
	rep(i,1,n){
		scanf("%d",&y[i].a);
		y[i].L=y[i].R=i;
		y[i].l=y[i].r=1;
	}
	sort(y+1,y+n+1);
	while(Y<=n){
		mn=y[Y].a;
		if(X)cmin(mn,x[X].a);
		while(y[Y].a==mn)x[++X]=y[Y++];
		sort(x+1,x+X+1);
//		dbg(X);
		x[X+1].L=0;
		Z=0;
		for(i=j=1;i<=X;i=++j){
			for(;x[j+1].L==x[j].R+1;++j);
			c=(l=j+1-i)/m;
//			printf("i=%d j=%d\n",i,j);
			if(c){
				T=Z;
				REP(k,0,c){
					++T;
					z[T].a=mn+1;
					z[T].L=x[i+k*m    ].L;
					z[T].R=x[i+k*m+m-1].R;
					z[T].l=z[T].r=0;
				}
				z[T++].R=x[j].R;
				calcs(i,i+m-1,  j);
				rep(k,i+m-1,j)z[Z+(k-i+1)/m].r+=x[k].r;
				per(k,j-m+1,i)z[T-(j-k+1)/m].l+=x[k].l;
				calct(Z,Z+m-1,--T);
				Z=T;
			}
		}
		X=Z;
		rep(i,1,X)x[i]=z[i];
//		dbg(X);
	}
	printf("%lld",ans-ant);
	return 0;
}