#include <bits/stdc++.h>
using namespace std;
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;
  vector<int> pre, suf;
  node(int _x = inf) {
    level = _x, mx = 1;
    if (_x >= 0) pre = suf = {1};
  }
  void Assert() {
    assert(pre.size() == mx && pre[mx - 1]);
    assert(suf.size() == mx && suf[mx - 1]);
  }
} stk[N], emp;
int a[N], n, L, top;
long long ans;
node merge(node a[], int n) {
  static int sum[N], tot[N];
  for (int i = (1); i <= (n); i++) sum[i] = sum[i - 1] + a[i].mx;
  if (sum[n] < L) return emp;
  tot[n + 1] = 0;
  for (int i = (n); i >= (1); i--) tot[i] = tot[i + 1] + a[i].pre[0];
  for (int l = 1, r = 1, q = 1; r <= n; l++)
    for (int i = (a[l].mx); i >= (1); i--) {
      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 += (long long)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++)
    for (int j = (1); j <= (a[i].mx); j++)
      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--)
    for (int j = (1); j <= (a[i].mx); j++)
      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;
      }
  for (int i = (res.mx - 1); i >= (1); i--) {
    res.pre[i - 1] += res.pre[i];
    res.suf[i - 1] += res.suf[i];
  }
  return res;
}
int main() {
  read(n), read(L), ans = n;
  for (int i = (1); i <= (n); i++) read(a[i]);
  a[n + 1] = inf;
  for (int i = (1); i <= (n); i++) {
    stk[++top] = node(a[i]);
    while (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;
      stk[top].Assert();
    }
  }
  cout << ans << endl;
  return 0;
}
