#include <bits/stdc++.h>
class SegmentTree {
 public:
  explicit SegmentTree(int size) {
    int tree_size = 1;
    while (tree_size < size) {
      tree_size *= 2;
    }
    tree_size = tree_size * 2 - 1;
    elements_.resize(tree_size);
  }
  void Add(int index, int value) {
    index += elements_.size() / 2;
    elements_[index] += value;
    while (index > 0) {
      index = Parent(index);
      elements_[index] = elements_[LeftSon(index)] + elements_[RightSon(index)];
    }
  }
  int64_t GetSum(int left, int right) const {
    if (left > right) {
      return 0;
    }
    int64_t res = 0;
    left += elements_.size() / 2;
    right += elements_.size() / 2;
    while (left < right) {
      if (RightSon(Parent(left)) == left) {
        res += elements_[left];
        ++left;
      }
      if (LeftSon(Parent(right)) == right) {
        res += elements_[right];
        --right;
      }
      left = Parent(left);
      right = Parent(right);
    }
    if (left == right) {
      res += elements_[left];
    }
    return res;
  }

 private:
  int Parent(int index) const { return (index - 1) / 2; }
  int LeftSon(int index) const { return 2 * index + 1; }
  int RightSon(int index) const { return 2 * index + 2; }
  std::vector<int> elements_;
};
struct Item {
  int idx;
  int v;
  int new_v;
};
struct Compare {
  inline bool operator()(const Item& first, const Item& second) const {
    return first.v < second.v;
  }
};
void Print(const std::vector<int64_t>& to_print) {
  for (int64_t elem : to_print) {
    std::cout << elem << " ";
  }
  std::cout << std::endl;
}
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n;
  int64_t k;
  std::cin >> n >> k;
  std::vector<Item> a(n);
  std::map<int, std::vector<int>> map;
  for (int i = 0; i < n; ++i) {
    std::cin >> a[i].v;
    a[i].idx = i;
    map[a[i].v].push_back(i);
  }
  int count = 0;
  for (const auto& elem : map) {
    for (int idx : elem.second) {
      a[idx].new_v = count;
    }
    ++count;
  }
  SegmentTree seg_tree(count);
  for (int i = 0; i < n; ++i) {
    seg_tree.Add(a[i].new_v, 1);
  }
  std::vector<int64_t> ai(n);
  for (int i = 0; i < n; ++i) {
    seg_tree.Add(a[i].new_v, -1);
    ai[i] = seg_tree.GetSum(0, a[i].new_v - 1);
  }
  int64_t ans = 0;
  int left = 0, right = 1;
  SegmentTree seg_left(count), seg_right(count);
  for (int i = 1; i < n; ++i) {
    seg_right.Add(a[i].new_v, 1);
  }
  int64_t cur = 0;
  for (int i = 1; i < n; ++i) {
    cur += ai[i];
  }
  while (left < n && right < n) {
    cur += seg_left.GetSum(a[left].new_v + 1, count - 1);
    seg_left.Add(a[left].new_v, 1);
    cur += seg_right.GetSum(0, a[left].new_v - 1);
    while (cur > k && right < n || left == right) {
      cur -= ai[right];
      cur -= seg_left.GetSum(a[right].new_v + 1, count - 1);
      seg_right.Add(a[right].new_v, -1);
      ++right;
    }
    ans += n - right;
    ++left;
  }
  std::cout << ans << std::endl;
  return 0;
}
