#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
template <typename T>
using vc = vector<T>;
template <typename T>
using uset = unordered_set<T>;
template <typename A, typename B>
using umap = unordered_map<A, B>;
template <typename T>
using pq = priority_queue<T>;
template <typename T>
using minpq = priority_queue<T, vc<T>, greater<T>>;
template <typename T>
inline void sort(vc<T>& v) {
  sort(v.begin(), v.end());
}
template <typename T, typename Comp>
inline void sort(vc<T>& v, Comp c) {
  sort(v.begin(), v.end(), c);
}
using ll = long long;
using ull = unsigned long long;
using pii = pair<ll, ll>;
using vi = vc<ll>;
using vii = vc<pii>;
constexpr char el = '\n';
constexpr char sp = ' ';
const ll N = 1e5;
ll n, d, a[N];
ll evan(ll x) {
  ll ans, low = 0, high = n - 1;
  while (low <= high) {
    ll mid = low + (high - low) / 2;
    if (a[mid] <= x) {
      ans = mid;
      low = mid + 1;
    } else
      high = mid - 1;
  }
  return ans;
}
inline ll evan2(ll x) { return x * (x - 1) / 2; }
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed;
  cin >> n >> d;
  for (ll i = 0; i < n; ++i) cin >> a[i];
  ll ans = 0;
  for (ll i = 0; i < n; ++i) ans += max<ll>(0, evan2(evan(a[i] + d) - i));
  cout << ans;
}
