#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long int n, d;
  cin >> n >> d;
  long long int arr[n];
  for (long long int i = 0; i < n; i++) cin >> arr[i];
  long long int l = 0, r = 0;
  long long int answer = 0;
  while (l < n) {
    while (r < n && arr[r] <= arr[l] + d) r++;
    r--;
    if (r - l <= 1) {
      l++;
      continue;
    }
    answer += -((l + 1) * (l + 2)) / 2 + (r * r + r) / 2;
    answer -= (l + 1) * (r - l - 1);
    l++;
  }
  cout << answer;
  return 0;
}
