#include <bits/stdc++.h>
using namespace std;
const int64_t M = 1e9 + 7;
const int64_t INF = 1e3 + 10;
bool prime[INF] = {1};
int64_t spf[INF];
int64_t d[INF] = {};
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
string ds = "RLDU";
long long C(int n, int m) {
  if (m > n) return 0;
  long long z = 1;
  for (int i = 0; i < m; i++) z = z * (n - i) / (i + 1);
  return z;
}
long long int invmod(long long int i) {
  if (i == 1) return 1;
  return (1000000007 -
          ((1000000007 / i) * invmod(1000000007 % i)) % 1000000007 +
          1000000007) %
         1000000007;
}
void sieve() {
  spf[1] = 1;
  for (int i = 2; i < INF; i++) spf[i] = i;
  for (int i = 4; i < INF; i += 2) spf[i] = 2;
  for (int i = 3; i * i < INF; i++) {
    if (spf[i] == i) {
      for (int j = i * i; j < INF; j += i)
        if (spf[j] == j) spf[j] = i;
    }
  }
}
int64_t binpow(int64_t a, int64_t b) {
  if (b == 0) return 1;
  if (b == 1) return a;
  int64_t temp = binpow(a, b / 2);
  if (b % 2 == 0)
    return (temp * temp);
  else
    return (((temp * temp)) * a);
}
int64_t tot(int64_t n) {
  int64_t result = n;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      while (n % i == 0) n /= i;
      result -= result / i;
    }
  }
  if (n > 1) result -= result / n;
  return result;
}
void phi_1_to_n(int n) {
  vector<int> phi(n + 1);
  phi[0] = 0;
  phi[1] = 1;
  for (int i = 2; i <= n; i++) phi[i] = i;
  for (int i = 2; i <= n; i++) {
    if (phi[i] == i) {
      for (int j = i; j <= n; j += i) phi[j] -= phi[j] / i;
    }
  }
}
int gcd(int a, int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
bool is_prime(int64_t n) {
  if (n == 1) return false;
  int64_t i = 2;
  while (i * i <= n) {
    if (n % i == 0) {
      return false;
    }
    i += 1;
  }
  return true;
}
void SieveOfEratosthenes() {
  prime[1] = 0;
  prime[0] = 0;
  for (int p = 2; p * p <= INF; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= INF; i += p) prime[i] = false;
    }
  }
}
int countteampairs(int a[], int s, int n, map<int, int> m) {
  int k = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (a[i] + a[j] == s) {
        if (m[a[i]] > 0 && m[a[j]] > 0) {
          if (a[i] == a[j] && m[a[i]] < 2) {
            break;
          }
          k++;
          m[a[i]]--;
          m[a[j]]--;
        }
      }
    }
  }
  return k;
}
void div() {
  for (int64_t i = 1; i < INF; i++) {
    for (int64_t j = i; j < INF; j += i) {
      d[j] += i;
      d[j] = d[j] % M;
    }
  }
}
int LCSubStr(string X, string Y, int m, int n) {
  int LCSuff[m + 1][n + 1];
  int result = 0;
  for (int i = 0; i <= m; i++) {
    for (int j = 0; j <= n; j++) {
      if (i == 0 || j == 0)
        LCSuff[i][j] = 0;
      else if (X[i - 1] == Y[j - 1]) {
        LCSuff[i][j] = LCSuff[i - 1][j - 1] + 1;
        result = max(result, LCSuff[i][j]);
      } else
        LCSuff[i][j] = 0;
    }
  }
  return result;
}
vector<vector<int64_t>> dp(505, vector<int64_t>(505, 0));
vector<int64_t> adj[INF];
vector<int64_t> vis(INF, 0);
vector<int64_t> path;
bool flag = 0;
int c = 0;
bool isqr(int64_t n) {
  int val = sqrt(n);
  return (val * val) == n;
}
void print(int a[], int n) {
  for (int i = 0; i < n; i++) cout << a[i] << " ";
  cout << endl;
}
void solve() {
  int64_t n, d;
  cin >> n >> d;
  vector<int64_t> v;
  for (int i = 0; i < n; i++) {
    int64_t a;
    cin >> a;
    v.push_back(a);
  }
  int64_t sum = 0;
  int j = 0;
  for (int i = 0; i < n; i++) {
    while (j < n && v[j] - v[i] <= d) j++;
    j--;
    int64_t cnt = j - i;
    sum += cnt * (cnt - 1) / 2;
  }
  cout << sum << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  { solve(); }
  return 0;
}
