#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 200000;
struct Block {
  int l, r, val;
  int mid;
  vector<int> pref, suff;
};
int n, need;
int a[MAXN];
int ufpar[MAXN], ufsz[MAXN];
Block block[MAXN];
pair<int, int> o[MAXN];
int uffind(int x) {
  if (ufpar[x] == x) return x;
  return ufpar[x] = uffind(ufpar[x]);
}
void ufunite(int a, int b) {
  a = uffind(a), b = uffind(b);
  if (a == b) return;
  if (ufsz[a] < ufsz[b]) swap(a, b);
  ufpar[b] = a, ufsz[a] += ufsz[b];
}
void levelup(Block &b) {
  vector<int> npref(((int)(b.pref).size()) / need, 0);
  for (int i = (need - 1); i < (((int)(b.pref).size())); ++i)
    npref[(i + 1) / need - 1] += b.pref[i];
  b.pref = npref;
  vector<int> nsuff(((int)(b.suff).size()) / need, 0);
  for (int i = (need - 1); i < (((int)(b.suff).size())); ++i)
    nsuff[(i + 1) / need - 1] += b.suff[i];
  b.suff = nsuff;
  b.mid /= need;
  if (b.mid > 0)
    ++b.val;
  else
    b.val = -1;
}
Block merge(const Block &a, const Block &b, long long &ans) {
  assert(a.r + 1 == b.l);
  assert(a.val == b.val);
  Block ret;
  ret.l = a.l;
  ret.r = b.r;
  ret.val = a.val;
  ret.mid = a.mid + b.mid;
  vector<int> npref = a.pref;
  for (int i = (0); i < (((int)(b.pref).size())); ++i) {
    int x = a.mid + i;
    while (x >= ((int)(npref).size())) npref.push_back(0);
    npref[x] += b.pref[i];
  }
  ret.pref = npref;
  vector<int> nsuff = b.suff;
  for (int i = (0); i < (((int)(a.suff).size())); ++i) {
    int x = b.mid + i;
    while (x >= ((int)(nsuff).size())) nsuff.push_back(0);
    nsuff[x] += a.suff[i];
  }
  ret.suff = nsuff;
  int sum = 0, at = 0;
  for (int i = (0); i < (((int)(b.pref).size())); ++i) sum += b.pref[i];
  for (int i = ((int)(a.suff).size()) - 1; i >= 0; --i) {
    while (i + 1 + at + 1 < need) sum -= b.pref[at], ++at;
    ans += (long long)a.suff[i] * sum;
  }
  return ret;
}
Block merge(const vector<Block> &blocks, long long &ans) {
  for (int i = (1); i < (((int)(blocks).size())); ++i)
    assert(blocks[i - 1].r + 1 == blocks[i].l),
        assert(blocks[i - 1].val == blocks[i].val);
  Block ret;
  ret.l = blocks[0].l, ret.r = blocks[((int)(blocks).size()) - 1].r,
  ret.val = blocks[0].val, ret.mid = 0;
  for (int i = (0); i < (((int)(blocks).size())); ++i) ret.mid += blocks[i].mid;
  {
    vector<int> npref;
    int off = 0;
    for (int i = (0); i < (((int)(blocks).size())); ++i) {
      for (int j = (0); j < (((int)(blocks[i].pref).size())); ++j) {
        int x = off + j;
        while (x >= ((int)(npref).size())) npref.push_back(0);
        npref[x] += blocks[i].pref[j];
      }
      off += blocks[i].mid;
    }
    ret.pref = npref;
  }
  {
    vector<int> nsuff;
    int off = 0;
    for (int i = ((int)(blocks).size()) - 1; i >= 0; --i) {
      for (int j = (0); j < (((int)(blocks[i].suff).size())); ++j) {
        int x = off + j;
        while (x >= ((int)(nsuff).size())) nsuff.push_back(0);
        nsuff[x] += blocks[i].suff[j];
      }
      off += blocks[i].mid;
    }
    ret.suff = nsuff;
  }
  {
    int sum = 0;
    for (int i = (0); i < (((int)(blocks).size())); ++i)
      for (int j = (0); j < (((int)(blocks[i].pref).size())); ++j)
        sum += blocks[i].pref[j];
    int ii = 0, jj = 0, off = 0;
    for (int i = (0); i < (((int)(blocks).size())); ++i) {
      off -= blocks[i].mid;
      for (int j = ((int)(blocks[i].suff).size()) - 1; j >= 0; --j) {
        while (ii <= i ||
               ii < ((int)(blocks).size()) && j + 1 + jj + 1 + off < need) {
          sum -= blocks[ii].pref[jj];
          ++jj;
          if (jj == ((int)(blocks[ii].pref).size()))
            off += blocks[ii].mid, ++ii, jj = 0;
        }
        ans += (long long)blocks[i].suff[j] * sum;
      }
    }
  }
  return ret;
}
long long solve() {
  for (int i = (0); i < (n); ++i) ufpar[i] = i, ufsz[i] = 1;
  for (int i = (0); i < (n); ++i) {
    block[i].l = block[i].r = i, block[i].val = a[i], block[i].mid = 1;
    block[i].pref = block[i].suff = vector<int>(1, 1);
  }
  for (int i = (0); i < (n); ++i) o[i] = make_pair(a[i], i);
  sort(o, o + n);
  long long ret = n;
  for (int l = 0, r = l; l < n; l = r) {
    int val = o[l].first;
    while (r < n && o[r].first == val) ++r;
    vector<int> cur;
    cur.push_back(-1);
    for (int i = (l); i < (r); ++i) {
      int at = o[i].second, prv = -1, nxt = -1;
      if (at - 1 >= 0 && a[at - 1] <= val) {
        int x = uffind(at - 1);
        while (block[x].val != -1 && block[x].val < val) levelup(block[x]);
        if (block[x].val != -1) prv = x;
      }
      if (at + 1 < n && a[at + 1] <= val) {
        int x = uffind(at + 1);
        while (block[x].val != -1 && block[x].val < val) levelup(block[x]);
        if (block[x].val != -1) nxt = x;
      }
      if ((((int)(cur).size()) < 1 || cur[((int)(cur).size()) - 1] != prv) &&
          (((int)(cur).size()) < 2 || cur[((int)(cur).size()) - 2] != prv))
        cur.push_back(prv);
      if (((int)(cur).size()) < 1 || cur[((int)(cur).size()) - 1] != at)
        cur.push_back(at);
      cur.push_back(nxt);
    }
    cur.push_back(-1);
    for (int i = (0); i < (((int)(cur).size())); ++i)
      if (cur[i] != -1 && cur[i - 1] == -1) {
        int j = i;
        while (cur[j + 1] != -1) ++j;
        vector<Block> blocks;
        for (int k = (i); k <= (j); ++k) blocks.push_back(block[cur[k]]);
        Block me = merge(blocks, ret);
        for (int k = (i + 1); k <= (j); ++k) ufunite(cur[i], cur[k]);
        block[uffind(cur[i])] = me;
      }
  }
  return ret;
}
void run() {
  scanf("%d%d", &n, &need);
  for (int i = (0); i < (n); ++i) scanf("%d", &a[i]);
  printf("%lld\n", solve());
}
int main() {
  run();
  return 0;
}
