#include <bits/stdc++.h>
using namespace std;
void print_set(set<string> &s) {
  set<string>::iterator it = s.begin();
  while (it != s.end()) {
    cout << (*it) << "\n";
    it++;
  }
}
long long p(long long x, long long n) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return p(x * x, n / 2);
  else
    return x * p(x * x, (n - 1) / 2);
}
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
float area(int x1, int y1, int x2, int y2, int x3, int y3) {
  return abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2.0);
}
bool inside_rectangle(int x1, int y1, int x2, int y2, int x3, int y3, int x4,
                      int y4, int x, int y) {
  float A = area(x1, y1, x2, y2, x3, y3) + area(x1, y1, x4, y4, x3, y3);
  float A1 = area(x, y, x1, y1, x2, y2);
  float A2 = area(x, y, x2, y2, x3, y3);
  float A3 = area(x, y, x3, y3, x4, y4);
  float A4 = area(x, y, x1, y1, x4, y4);
  return (A == A1 + A2 + A3 + A4);
}
bool sortinrev(const pair<int, string> &a, const pair<int, string> &b) {
  if (a.first != b.first)
    return (a.first > b.first);
  else
    return (a.second < b.second);
}
long long digitsum(long long a) {
  long long num = a, sum = 0;
  while (num != 0) {
    sum = sum + num % 10;
    num = num / 10;
  }
  if (sum / 10)
    return digitsum(sum);
  else
    return sum;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int d, n;
  long long t;
  long long res = 0;
  cin >> n >> d;
  vector<int> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 2; i < n; i++) {
    t = i - (lower_bound(a.begin(), a.end(), a[i] - d) - a.begin());
    res += t * (t - 1) / 2;
  }
  cout << res;
}
