#include <bits/stdc++.h>
using namespace std;
int n, m, a[200127], mi = 0x3f3f3f3f, L[200127], R[200127], vl[200127],
                     vr[200127], v1[200127], v2[200127];
struct Point {
  int x, v;
  inline bool operator<(const Point &b) const {
    return (v > b.v) || ((v == b.v) && (x > b.x));
  }
};
priority_queue<Point> q;
int b[200127], cn = 0;
long long ans = 0;
int main() {
  scanf("%d%d", &n, &m);
  ans = n;
  int i, j, k, v, l, r, p, sum;
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]), L[i] = i - 1, R[i] = i + 1, vl[i] = vr[i] = 1,
                        q.push((Point){i, a[i]});
  while (!q.empty()) {
    cn = 0;
    v = q.top().v;
    while ((!q.empty()) && (v == q.top().v)) b[++cn] = q.top().x, q.pop();
    for (i = 1; i <= cn; i = r + 1) {
      l = r = i;
      while ((r < cn) && (R[b[r]] == b[r + 1])) ++r;
      sum = 0;
      for (i = l; i <= r; i++) {
        if (i - m + 1 >= l) sum += vl[b[i - m + 1]];
        ans += 1ll * sum * vr[b[i]];
      }
      if (r - l + 1 < m) {
        R[L[b[l]]] = n + 1;
        L[R[b[r]]] = 0;
        continue;
      }
      p = (r - l + 1) / m;
      for (i = l; i <= r; i++)
        k = b[i], v1[k] = vl[k], v2[k] = vr[k], vl[k] = vr[k] = 0;
      for (i = r - m + 1, k = p; i >= l; i -= m, --k)
        for (j = i; (j >= l) && (j >= i - m + 1); j--)
          vl[b[l + k - 1]] += v1[b[j]];
      for (i = l + m - 1, k = 1; i <= r; i += m, ++k)
        for (j = i; (j <= r) && (j <= i + m - 1); j++)
          vr[b[l + k - 1]] += v2[b[j]];
      for (i = l; i <= l + p - 1; i++)
        ++a[b[i]], q.push((Point){b[i], a[b[i]]});
      L[R[b[r]]] = b[l] + p - 1;
      R[b[l] + p - 1] = R[b[r]];
      sum = 0;
      for (i = l; i <= l + p - 1; i++) {
        if (i - m + 1 >= l) sum += vl[b[i] - m + 1];
        ans -= 1ll * sum * vr[b[i]];
      }
    }
  }
  printf("%lld", ans);
  return 0;
}
