#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
#include <bitset>
using namespace std;

#ifdef LOCAL
	#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
	#define eprintf(...) 42
#endif

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef long double ld;
#define mp make_pair
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int LOG = 18;
const int NN = 200200;
pii sparse[LOG][NN];
int p2[NN];
int n;
ll ans = 0;

pii getMax(int l, int r) {
	int k = p2[r - l];
	return max(sparse[k][l], sparse[k][r - (1 << k)]);
}

int L;

struct Item {
	int K;
	vector<int> toL, toR;
	bool full;

	Item() : K(0), toL(), toR(), full(false) {}

	Item toUpper() {
		Item I = Item();
		I.K = K + 1;
		I.full = full;
		for (int i = 0; i < (int)toL.size(); i++) {
			if ((i + 1) % L == 0)
				I.toL.push_back(0);
			if (!I.toL.empty())
				I.toL.back() += toL[i];
		}
		for (int i = 0; i < (int)toR.size(); i++) {
			if ((i + 1) % L == 0)
				I.toR.push_back(0);
			if (!I.toR.empty())
				I.toR.back() += toR[i];
		}
		if (I.toL.empty() || I.toR.empty()) I.full = false;
		return I;
	}

	void eprint() {
		return;
		eprintf("Item\n");
		eprintf("K = %d\n", K);
		for (int x : toL)
			eprintf("%d ", x);
		eprintf("\n");
		for (int x : toR)
			eprintf("%d ", x);
		eprintf("\n");
		eprintf("full = %d\n", (int)full);
		eprintf("--------\n");
	}
};
struct Fenv {
	int n;
	vector<ll> fenv;

	Fenv() : n(), fenv() {}
	Fenv(int _n) {
		n = _n;
		fenv = vector<ll>(n, 0);
	}

	void add(int p, ll x) {
		for(; p < n; p |= p + 1)
			fenv[p] += x;
	}
	ll get(int r) {
		ll res = 0;
		for(; r >= 0; r = (r & (r + 1)) - 1)
			res += fenv[r];
		return res;
	}
	ll getSum(int l, int r) {
		return get(r - 1) - get(l - 1);
	}

	void clear() {
		for (int i = 0; i < n; i++)
			fenv[i] = 0;
	}
};

Item solve(int l, int r) {
	eprintf("solve %d %d\n", l, r);
	int m = getMax(l, r).first;
	vector<Item> items;
	while(l < r) {
		pii t = getMax(l, r);
		if (t.first != m) {
			Item I = solve(l, r);
			while(I.K < m && (!I.toL.empty() || !I.toR.empty()))
				I = I.toUpper();
			if (I.K != m) I.K = m;
			items.push_back(I);
			break;
		}
		if (t.second + 1 != r) {
			Item I = solve(t.second + 1, r);
			eprintf("HERE 1\n");
			while(I.K < m && (!I.toL.empty() || !I.toR.empty()))
				I = I.toUpper();
			eprintf("HERE 2\n");
			if (I.K != m) I.K = m;
			items.push_back(I);	
		}
		Item I = Item();
		I.K = m;
		I.toL.push_back(1);
		I.toR.push_back(1);
		I.full = true;
		items.push_back(I);
		r = t.second;
	}
	reverse(items.begin(), items.end());
	for (Item I : items)
		I.eprint();
	int tot = 0;
	for (Item I : items)
		tot += max((int)I.toL.size(), (int)I.toR.size());
	Fenv F = Fenv(tot + 2);
	tot = 0;
	for (Item I : items) {
		for (int i = 0; i < (int)I.toL.size(); i++) {
			ans += F.getSum(0, tot + i + 2 - L) * I.toL[i];
		}
		if (!I.full) {
			tot = 0;
			F.clear(); // slow
		}
		tot += (int)I.toR.size();
		for (int i = 0; i < (int)I.toR.size(); i++) {
			F.add(tot - i - 1, I.toR[i]);
		}
	}
	eprintf("ans = %lld\n", ans);
	Item N = Item();
	N.K = m;
	N.full = true;
	for (Item I : items) {
		for (int x : I.toL)
			N.toL.push_back(x);
		if (!I.full) {
			N.full = false;
			break;
		}
	}
	reverse(items.begin(), items.end());
	for (Item I : items) {
		for (int x : I.toR)
			N.toR.push_back(x);
		if (!I.full) {
			N.full = false;
			break;
		}
	}
	eprintf("at the end\n");
	return N;
}

int main()
{
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);

	for (int i = 2; i < NN; i++)
		p2[i] = p2[i / 2] + 1;

	scanf("%d%d", &n, &L);
	for (int i = 0; i < n; i++) {
		scanf("%d", &sparse[0][i].first);
		sparse[0][i].second = i;
	}
	for (int k = 0; k < LOG - 1; k++)
		for (int i = 0; i + (1 << (k + 1)) <= n; i++)
			sparse[k + 1][i] = max(sparse[k][i], sparse[k][i + (1 << k)]);
	ans = n;
	solve(0, n);
	printf("%lld\n", ans);

	return 0;
}


Measured memory_bytes: 54504