#include <bits/stdc++.h>
using namespace std;
const int inf = ~0U >> 2;
const long long infl = ~0LLU >> 2;
template <class T>
inline void Min(T &x, T y) {
  if (y < x) x = y;
}
template <class T>
inline void Max(T &x, T y) {
  if (y > x) x = y;
}
const int mod = 1e9 + 7;
inline int Add(int x) { return x >= mod ? x - mod : x; }
inline void Add(int &x, int y) {
  x += y;
  if (x >= mod) x -= mod;
}
inline int Sub(int x) { return x < 0 ? x + mod : x; }
inline void Sub(int &x, int y) {
  x -= y;
  if (x < 0) x += mod;
}
inline int Mul(int x, int y) { return (int)((long long)x * y % mod); }
inline int Mul(int x, int y, int z) { return Mul(x, Mul(y, z)); }
int Pow(int x, int y) {
  int z = 1;
  for (; y; y >>= 1, x = Mul(x, x)) {
    if (y & 1) z = Mul(z, x);
  }
  return z;
}
inline int Inv(int x) { return Pow(x, mod - 2); }
inline int Fix(long long x) {
  x %= mod;
  if (x < 0) x += mod;
  return (int)x;
}
inline int Fix(int x) {
  x %= mod;
  if (x < 0) x += mod;
  return x;
}
const int maxn = 2e5;
int n, lim;
int st[maxn], top;
int L[maxn], R[maxn];
long long ans;
long long Ways(int l, int r) {
  long long sum = 0, tot = 0;
  for (int i = r - 1; i >= l + lim - 1; --i) {
    sum += R[i];
    tot += (long long)L[i - lim + 1] * sum;
  }
  for (int i = l; i < r; ++i) {
    tot += (long long)L[i] * R[i];
  }
  return tot;
}
void Merge(void) {
  int val = st[top - 1];
  int begin = top - 1;
  while (begin > 0 && st[begin - 1] == st[begin]) {
    --begin;
  }
  int end = top;
  ans += Ways(begin, end);
  if (end - begin < lim) {
    top = begin;
    return;
  }
  int len = (end - begin) / lim;
  static int cntP[maxn + 1], cntS[maxn + 1];
  memset((cntP), (0), sizeof((cntP)[0]) * (len + 1));
  memset((cntS), (0), sizeof((cntS)[0]) * (len + 1));
  for (int i = begin; i < end; ++i) {
    int c = i - begin + 1;
    if (c >= lim) {
      cntP[c / lim] += R[i];
    }
    c = end - i;
    if (c >= lim) {
      cntS[c / lim] += L[i];
    }
  }
  top = begin + len;
  for (int i = 0; i < len; ++i) {
    st[begin + i] = val + 1;
    L[begin + i] = cntS[len - i];
    R[begin + i] = cntP[i + 1];
  }
  ans -= Ways(begin, top);
}
int main(void) {
  scanf("%d%d", &n, &lim);
  top = 0;
  ans = 0;
  for (int i = 0; i < n; ++i) {
    int now;
    scanf("%d", &now);
    while (top > 0 && st[top - 1] < now) {
      Merge();
    }
    L[top] = R[top] = 1;
    st[top++] = now;
  }
  while (top > 0) {
    Merge();
  }
  printf("%lld\n", ans);
  return 0;
}
