#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n, d;
  cin >> n >> d;
  vector<long long> x(n);
  for (long long i = 0; i < n; ++i) cin >> x[i];
  vector<long long> dp(n + 1);
  for (long long i = 1; i <= n; ++i) dp[i] = dp[i - 1] + i;
  long long r = 0;
  long long ans = 0;
  for (long long i = 0; i < n; ++i) {
    while (r + 1 < n && x[r + 1] - x[i] <= d) r++;
    long long sz = r - i + 1;
    if (sz - 2 >= 0) ans += dp[sz - 2];
  }
  cout << ans << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long tst;
  tst = 1;
  while (tst--) {
    solve();
  }
  return 0;
}
