#include <bits/stdc++.h>
#define FIO ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define ll long long
#define ull unsigned long long
#define ld long double
#define Yes cout << "YES\n"
#define No cout << "NO\n"
using namespace std;
#define IO freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
const int MX = 2e5 + 5 , MOD = 1e9 + 7;
#define all(v) v.begin() , v.end()
#define rall(v) v.rbegin() , v.rend()
#define nl '\n'
vector<vector<int>>adj(MX);
vector<int>indeg(MX);
vector<bool>vis(MX);
vector<bool>inprogg(MX);
queue<int>q;
vector<int>ans(MX);
vector<int>parent(MX);
void solve()
{
int n, d;
cin >> n >>d;
int a[n];
for(int i = 0 ; i < n ; i++){
cin >> a[i];
}
ll ans = 0;
for(ll i = 0 , j = 0 ; i < n ; i++){
while(a[i] - a[j] > d) j++;
ans += (i - j) * (i - j - 1) / 2;
}
cout << ans;
}
int main()
{
FIO
#ifndef ONLINE_JUDGE
IO
#endif
int tt = 1;
// cin >> tt;
while (tt--) {
solve();
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgRklPIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpO2Npbi50aWUobnVsbHB0cik7Y291dC50aWUobnVsbHB0cik7CiNkZWZpbmUgbGwgbG9uZyBsb25nCiNkZWZpbmUgdWxsIHVuc2lnbmVkIGxvbmcgbG9uZwojZGVmaW5lIGxkIGxvbmcgZG91YmxlCiNkZWZpbmUgWWVzIGNvdXQgPDwgIllFU1xuIgojZGVmaW5lIE5vICBjb3V0IDw8ICJOT1xuIgp1c2luZyBuYW1lc3BhY2Ugc3RkOwojZGVmaW5lIElPIGZyZW9wZW4oImlucHV0LnR4dCIsInIiLHN0ZGluKTsgZnJlb3Blbigib3V0cHV0LnR4dCIsInciLHN0ZG91dCk7CmNvbnN0IGludCBNWCA9IDJlNSArIDUgLCBNT0QgPSAxZTkgKyA3OwojZGVmaW5lIGFsbCh2KS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