#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cassert>
#define FILL(a, n, x) memset((a), (x), sizeof ((a)[0]) * (n))
#define FILLA(a, x) memset((a), (x), sizeof (a))
#define COPY(a, n, b) memcpy((b), (a), sizeof ((a)[0]) * (n))
#define COPYA(a, b) memcpy((b), (a), sizeof (a))
using namespace std;

typedef pair<int, int> pii;
typedef long long li;
typedef unsigned long long lu;
const int inf = ~0U >> 2;
const li 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)((li)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(li 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];
li ans;

li Ways(int l, int r) {
  li sum = 0, tot = 0;
  for (int i = r - 1; i >= l + lim - 1; --i) {
    sum += R[i];
    tot += (li)L[i - lim + 1] * sum;
  }
  for (int i = l; i < r; ++i) {
    tot += (li)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;
    while (top > 0) Merge();
    return;
  }
  int len = (end - begin) / lim;

  static int cntP[maxn + 1], cntS[maxn + 1];
  FILL(cntP, len + 1, 0);
  FILL(cntS, len + 1, 0);
  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;
}
