#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long ll;
const int Maxn = 2e5;

int N, L;
int A[Maxn + 5];

int tot;
int val[Maxn * 4 + 5], vl[Maxn * 4 + 5], vr[Maxn * 4 + 5];
int s1[Maxn * 4 + 5], s2[Maxn * 4 + 5], sum[Maxn * 4 + 5];
int now[Maxn * 4 + 5], newnow[Maxn * 4 + 5];

struct cmp {
	bool operator () (const int &lhs, const int &rhs) {
		return val[lhs] == val[rhs] ? vl[lhs] > vl[rhs] : val[lhs] > val[rhs];
	}
};
priority_queue<int, vector<int>, cmp> q;

int main() {
#ifdef LOACL
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	scanf("%d %d", &N, &L);
	for(int i = 1; i <= N; i++) {
		scanf("%d", &A[i]);
		++tot;
		val[tot] = A[i], vl[tot] = vr[tot] = i;
		s1[tot] = s2[tot] = 1;
		q.push(tot);
	}
	ll ans = 0;
	while(!q.empty()) {
		int len = 0;
		now[++len] = q.top();
		q.pop();
		while(!q.empty() && val[q.top()] == val[now[1]] && vl[q.top()] == vr[now[len]] + 1)
			now[++len] = q.top(), q.pop();
		sum[0] = 0;
		for(int i = 1; i <= len; i++)
			sum[i] = sum[i - 1] + s1[now[i]];
		for(int i = 1; i <= len; i++) {
			ans += 1LL * s1[now[i]] * s2[now[i]];
			if(i - L + 1 >= 1) ans += 1LL * s2[now[i]] * sum[i - L + 1];
		}
		int newlen = 0, last = 0;
		for(int i = 1; i <= len / L; i++) {
			int tmpl = (i - 1) * L + 1, tmpr = i * L;
			if(i == len / L) tmpr = len;
			++tot;
			vl[tot] = vl[now[tmpl]], vr[tot] = vr[now[tmpr]];
			val[tot] = val[now[1]] + 1;
			for(int j = i * L; j <= (i + 1) * L - 1 && j <= len; j++)
				s2[tot] += s2[now[j]];
			s1[tot] = sum[len % L + 1 + (i - 1) * L] - sum[last];
			last = len % L + 1 + (i - 1) * L;
			q.push(tot);
			newnow[++newlen] = tot;
		}
		sum[0] = 0;
		for(int i = 1; i <= newlen; i++)
			sum[i] = sum[i - 1] + s1[newnow[i]];
		for(int i = 1; i <= newlen; i++) {
			ans -= 1LL * s1[newnow[i]] * s2[newnow[i]];
			if(i - L + 1 >= 1) ans -= 1LL * s2[newnow[i]] * sum[i - L + 1];
		}
	}
	printf("%lld\n", ans);
	return 0;
}