#include <bits/stdc++.h>
using namespace std;
struct Fenwick {
  int d[1000001];
  void add(int id, int val) {
    id++;
    while (id <= 1000001) {
      d[id] += val;
      id += (id) & (-id);
    }
  }
  int sum(int id) {
    int ret = 0;
    id++;
    while (id > 0) {
      ret += d[id];
      id -= (id) & (-id);
    }
    return ret;
  }
  void clear() { memset(d, 0, sizeof d); }
  int ssum(int l, int r) { return sum(r) - sum(l - 1); }
} ft1, ft2;
int a[1000001], b[1000001];
long long s, s1;
int main() {
  long long n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[i - 1] = a[i];
  }
  sort(b, b + n);
  int m = unique(b, b + n) - b;
  for (int i = 1; i <= n; i++) a[i] = lower_bound(b, b + m, a[i]) - b + 1;
  for (int i = 1; i <= n; i++) {
    ft1.add(a[i], 1);
    s += i - ft1.sum(a[i]);
  }
  if (s <= k) {
    cout << n * (n - 1) / 2 << endl;
    return 0;
  }
  ft2.add(a[n], 1);
  ft1.add(a[n], -1);
  for (int r = n - 1, l = n - 1; r > 0; r--) {
    while (l > 0 && s > k) {
      s -= l - ft1.sum(a[l]);
      s -= ft2.sum(a[l] - 1);
      ft1.add(a[l], -1);
      l--;
    }
    if (l == 0) break;
    s1 += l;
    s += ft2.sum(a[r] - 1);
    s += l - ft1.sum(a[r]);
    ft2.add(a[r], 1);
  }
  cout << s1 << endl;
  return 0;
}
