#include <bits/stdc++.h>
const int inf = 1000000000;
using namespace std;
const int maxn = 200000;
int n, d;
int a[maxn];
long long int ans;
long long int c[maxn][4];
int main() {
  scanf("%d %d", &n, &d);
  c[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    c[i][0] = 1;
    for (int j = 1; j < 4; j++) c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
  }
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  int j = 1;
  for (int i = 0; i < n; i++) {
    for (; (j < n) && (a[j] - a[i]) <= d; j++)
      ;
    ans += c[max(j - i - 1, 0)][2];
  }
  cout << ans;
  return 0;
}
