#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 1;
int n, z;
pair<int, int> a[N];
int l[N], r[N];
int sl[N], sr[N], sx[N];
int sz = 0;
deque<pair<int, int> > v[2 * N];
deque<int> gg[2 * N];
long long hh[2 * N];
vector<long long> pw;
long long ans = 0;
void shrink(int vt, int sf) {
  int ls = v[vt].size() / sf;
  if (ls == 0) {
    v[vt].clear();
    return;
  }
  int gd = v[vt].back().second;
  for (int i = 0; i < ls; i++) {
    v[vt][i].first = v[vt][(i + 1) * sf - 1].first;
    if (i != ls - 1) v[vt][i].second = v[vt][(i + 2) * sf - 2].second;
  }
  v[vt][ls - 1].second = gd;
  v[vt].resize(ls);
  hh[vt] = 0;
  gg[vt].resize(ls);
  long long mz = 0;
  for (int i = 0; i < ls; i++) {
    mz += max(v[vt][i].second, v[vt][i].first) -
          min(v[vt][i].second, v[vt][i].first) + 1;
    gg[vt][i] = mz;
  }
}
void merge(int ml, int mr) {
  if (sx[mr] <= sx[ml]) {
    int jp = sx[ml] - sx[mr];
    sx[mr] = sx[ml];
    if (pw.size() <= jp) {
      v[sl[mr]].clear();
      gg[sl[mr]].clear();
      v[sr[mr]].clear();
      gg[sr[mr]].clear();
    } else {
      shrink(sl[mr], pw[jp]);
      shrink(sr[mr], pw[jp]);
    }
    if (v[sl[mr]].empty()) {
      int y = r[mr];
      l[y] = r[y] = l[mr] = r[mr] = 0;
      return;
    }
    int pl = sl[ml], pr = sr[ml];
    int ql = sl[mr], qr = sr[mr];
    int dl = l[ml], dr = r[mr];
    for (int i = 0; i < v[ql].size(); i++) {
      int x = max(1, z - i - 1);
      if (x > v[pr].size()) continue;
      int bona = v[ql][i].second - v[ql][i].first + 1;
      long long bruh = 0;
      if (x >= 2) bruh = gg[pr][x - 2];
      ans += 1LL * bona * (gg[pr].back() - bruh);
    }
    for (int i = 0; i < v[ql].size(); i++) {
      v[pl].push_back(v[ql][i]);
      gg[pl].push_back(gg[pl].back() + v[ql][i].second - v[ql][i].first + 1);
    }
    long long mz = hh[pr];
    for (int i = v[qr].size() - 1; i >= 0; i--) {
      v[pr].push_front(v[qr][i]);
      gg[pr].push_front(mz);
      mz -= v[qr][i].first - v[qr][i].second + 1;
    }
    hh[pr] = mz;
    v[ql].clear();
    v[qr].clear();
    r[dl] = dr;
    l[dr] = dl;
    sx[dr] = sx[dl];
    sl[dr] = sl[dl];
    sr[dr] = sr[dl];
  } else {
    swap(ml, mr);
    int jp = sx[ml] - sx[mr];
    sx[mr] = sx[ml];
    if (pw.size() <= jp) {
      v[sl[mr]].clear();
      gg[sl[mr]].clear();
      v[sr[mr]].clear();
      gg[sr[mr]].clear();
    } else {
      shrink(sl[mr], pw[jp]);
      shrink(sr[mr], pw[jp]);
    }
    if (v[sl[mr]].empty()) {
      int y = l[mr];
      l[y] = r[y] = l[mr] = r[mr] = 0;
      return;
    }
    int pl = sr[ml], pr = sl[ml];
    int ql = sr[mr], qr = sl[mr];
    int dl = l[mr], dr = r[ml];
    for (int i = 0; i < v[ql].size(); i++) {
      int x = max(1, z - i - 1);
      if (x > v[pr].size()) continue;
      int bona = v[ql][i].first - v[ql][i].second + 1;
      long long bruh = 0;
      if (x >= 2) bruh = gg[pr][x - 2];
      ans += 1LL * bona * (gg[pr].back() - bruh);
    }
    for (int i = 0; i < v[ql].size(); i++) {
      v[pl].push_back(v[ql][i]);
      gg[pl].push_back(gg[pl].back() + v[ql][i].first - v[ql][i].second + 1);
    }
    long long mz = hh[pr];
    for (int i = v[qr].size() - 1; i >= 0; i--) {
      v[pr].push_front(v[qr][i]);
      gg[pr].push_front(mz);
      mz -= v[qr][i].second - v[qr][i].first + 1;
    }
    hh[pr] = mz;
    v[ql].clear();
    v[qr].clear();
    r[dl] = dr;
    l[dr] = dl;
    sx[dl] = sx[dr];
    sl[dl] = sl[dr];
    sr[dl] = sr[dr];
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> z;
  for (int i = 1; i <= n; i++) {
    cin >> a[i].first;
    a[i].second = i;
  }
  pw.push_back(1);
  while (pw.back() * z <= n) {
    pw.push_back(pw.back() * z);
  }
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++) {
    int cl = i, cr = i;
    while (cr < n && a[cr + 1].first == a[cl].first) cr++;
    i = cr;
    for (int j = cl; j <= cr; j++) {
      int x = a[j].second;
      l[x] = r[x] = x;
      sx[x] = a[j].first;
      sl[x] = ++sz;
      v[sz].push_back({x, x});
      gg[sz].push_back(1);
      sr[x] = ++sz;
      v[sz].push_back({x, x});
      gg[sz].push_back(1);
      if (r[x - 1] != 0) merge(x - 1, x);
      if (l[x + 1] != 0) merge(x, x + 1);
    }
  }
  cout << ans + n << '\n';
}
