Problem:
Score : 1800 points

Problem Statement

For a sequence S of positive integers and positive integers k and l, S is said to belong to level (k,l) when one of the following conditions is satisfied:

The length of S is 1, and its only element is k.

There exist sequences T_1,T_2,...,T_m (m \geq l) belonging to level (k-1,l) such that the concatenation of T_1,T_2,...,T_m in this order coincides with S.

Note that the second condition has no effect when k=1, that is, a sequence belongs to level (1,l) only if the first condition is satisfied.

Given are a sequence of positive integers A_1,A_2,...,A_N and a positive integer L.
Find the number of subsequences A_i,A_{i+1},...,A_j (1 \leq i \leq j \leq N) that satisfy the following condition:

There exists a positive integer K such that the sequence A_i,A_{i+1},...,A_j belongs to level (K,L).

Constraints

1 \leq N \leq 2 \times 10^5

2 \leq L \leq N

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N L
A_1 A_2 ... A_N

Output

Print the number of subsequences A_i,A_{i+1},...,A_j (1 \leq i \leq j \leq N) that satisfy the condition.

Sample Input 1

9 3
2 1 1 1 1 1 1 2 3

Sample Output 1

22

For example, both of the sequences (1,1,1) and (2) belong to level (2,3), so the sequence (2,1,1,1,1,1,1) belong to level (3,3).

Sample Input 2

9 2
2 1 1 1 1 1 1 2 3

Sample Output 2

41

Sample Input 3

15 3
4 3 2 1 1 1 2 3 2 2 1 1 1 2 2

Sample Output 3

31

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

/**
 *    author:  tourist
 *    created: 08.12.2019 08:24:29       
**/
#include <bits/stdc++.h>

using namespace std;

template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, len;
  cin >> n >> len;
  vector<int> a(n);
  vector<int> L(n);
  vector<int> R(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    L[i] = 1;
    R[i] = 1;
  }
  long long ans = n;
  set<pair<int, int>> s;
  set<int> alive;
  for (int i = 0; i < n; i++) {
    s.emplace(a[i], i);
    alive.insert(i);
  }
  while (!s.empty()) {
    int id = s.begin()->second;
    s.erase(s.begin());
    vector<int> pos(1, id);
    while (true) {
      auto it = alive.lower_bound(pos.back() + 1);
      if (it == alive.end() || a[*it] != a[id]) {
        break;
      }
      s.erase(make_pair(a[*it], *it));
      pos.push_back(*it);
    }
    int sz = (int) pos.size();
    if (sz < len) {
      continue;
    }
    {
      long long sumL = 0;
      for (int i = len - 1; i < sz; i++) {
        sumL += L[pos[i - len + 1]];
        ans += sumL * R[pos[i]];
      }
    }
    if (len * 1LL * len <= sz) {
      long long sumL = 0;
      for (int i = len * len - 1; i < sz; i++) {
        sumL += L[pos[i - len * len + 1]];
        ans -= sumL * R[pos[i]];
      }
    }
    debug(ans);
    for (int x : pos) {
      alive.erase(x);
    }
    int new_sz = sz / len;
    vector<int> newL(new_sz);
    vector<int> newR(new_sz);
    for (int i = 0; i < sz; i++) {
      if (i >= len - 1) {
        newR[(i - (len - 1)) / len] += R[pos[i]];
      }
      if (i <= sz - len) {
        newL[new_sz - 1 - ((sz - len) - i) / len] += L[pos[i]];
      }
    }
    debug(newL, newR);
    for (int i = 0; i < new_sz; i++) {
      alive.insert(pos[i]);
      L[pos[i]] = newL[i];
      R[pos[i]] = newR[i];
      a[pos[i]] += 1;
      s.emplace(a[pos[i]], pos[i]);
    }
  }
  cout << ans << '\n';
  return 0;
}