#include<bits/stdc++.h>
using namespace std;
long long int fact(int n){
	long long int pr=1;
	for(int i=1;i<=n;i++)
		pr*=i;
	return pr;
}
long long int factc(int n){
	//cout<<"c";
	return (fact(n)/(fact(n-3)*6));
}
int main(){
	int n,d;
	cin>>n>>d;
	long long int arr[n];
	for(int i=0;i<n;i++)
		cin>>arr[i];
	sort(arr,arr+n);
	long long int sum=0;
	for(int i=0;i<n-2;i++){
	//	cout<<"a";
		long long int num=arr[i]+d;
		int j;
		for(j=i+1;arr[j]<=num&&j!=n;j++){
		//	cout<<"b"<<j<<endl;
			if(j==n-1){
				j=n;
				break;
			}
		}
		sum+=factc(j-i);
		if(j==n) i=n;
		else if(j-i>=3) i=j-3;

	}
	cout<<sum;
}


#include<bits/stdc++.h>
using namespace std;

int main(){
	long long int n,d;
	cin>>n>>d;
	long long int arr[n];
	for(int i=0;i<n;i++)
		cin>>arr[i];
	sort(arr,arr+n);
	long long int sum=0;
	int x=0;
	for(int i=0;i<n;i++){
		if(x!=n){
			while(arr[x]-arr[i]<=d){
				x++;
				if(x==n) break;
			}
		}
		sum+=(x-i-1)*(x-i-2)/2;
	}
	cout<<sum;
}


#include <bits/stdc++.h>
#define int long long int
#define all(a) a.begin(),a.end()
#define S second
#define F first
#define pii pair<int,int>
#define vct vector<int>
#define pb push_back
#define endl '\n'
using namespace std;
void solve(){
	int n ,d;
	cin>>n>>d ;
	vector<int> a(n);
	int ans = 0;
	for(int i=0;i<n;i++) cin>>a[i];
	for(int i=0;i<n;i++){
		int p = a[i];
		p += d;
		int  in =(int)(upper_bound(a.begin(),a.end(),p)-a.begin());
    if(in-i<3) continue;
    ans += (in-1-i-1)*(in-1-i)/2;
	}
	cout << ans ;
    return ;
}
signed main(){
    int t = 1 ;
    //uncomment if t tstcases
   //     cin>>t ;

    while(t--) {
        solve() ;
        cout << endl ;
    }
    return  0 ;
}
/* I TOOK a big fat  L 2 times but not for the third time
    AM I WORTH IT ?
    DID I PUT ENOUGH WORK IN ?
*/


#include <bits/stdc++.h>
#define int long long int
#define all(a) a.begin(),a.end()
#define S second
#define F first
#define pii pair<int,int>
#define vct vector<int>
#define pb push_back
#define endl '\n'
using namespace std;
void solve(){
	int n ,d;
	cin>>n >>d;
	vector<int> a(n);
	for(int i=0;i<n;i++) cin>>a[i];
	int r =   0;
	if(n< 3 ){
		 cout << 0;
		 return ;
	}
	int ans = 0 ;
	for(int i=0;i<n;i++){
		while(r< n&&a[r]-a[i]<=d) r++;
		if(r-i<3) continue;
		ans += (r-i-1)*(r-i-2)/2;
	}
	cout << ans ;
    return ;
}
signed main(){
    int t = 1 ;
    //uncomment if t tstcases
   //     cin>>t ;

    while(t--) {
        solve() ;
        cout << endl ;
    }
    return  0 ;
}
/* I TOOK a big fat  L 2 times but not for the third time
    AM I WORTH IT ?
    DID I PUT ENOUGH WORK IN ?
*/


#include <bits/stdc++.h>
#define int long long int
#define all(a) a.begin(),a.end()
#define S second
#define F first
#define pii pair<int,int>
#define vct vector<int>
#define pb push_back
#define endl '\n'
using namespace std;
void solve(){
	int n ,d;
	cin>>n >>d;
	vector<int> a(n);
	for(int i=0;i<n;i++) cin>>a[i];
	int r =   1;
	if(n< 3 ){
		 cout << 0;
		 return ;
	}
	for(;r< n;r++){
		if(a[r]-a[0]>d) break ;
	}
	int ans = 0 ;
	if(r>2){
		ans += (r-1)*(r-2)/2;
	}
	for(int i=1;i<n;i++){
		while(r< n&&a[r]-a[i]>d) r++;
		if(r-i<3) continue;
		ans += (r-i-1)*(r-i-2)/2;
	}
	cout << ans ;
    return ;
}
signed main(){
    int t = 1 ;
    //uncomment if t tstcases
   //     cin>>t ;

    while(t--) {
        solve() ;
        cout << endl ;
    }
    return  0 ;
}
/* I TOOK a big fat  L 2 times but not for the third time
    AM I WORTH IT ?
    DID I PUT ENOUGH WORK IN ?
*/


/*
 (c) Magaa :)
*/
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(s) ((int)(s.size()))
#define all(s) s.begin(), s.end()

typedef vector < int > vi;
typedef pair < int , int > pii;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const int Mass = (int) 2e5 + 5;

int a[Mass];

int main () {
	#ifdef Home
		freopen(".in", "r", stdin);
		freopen(".out", "w", stdout);
	#endif
	ios_base :: sync_with_stdio(false); cin.tie(NULL);
	int n , d;
	cin >> n >> d;
	for (int i = 1; i <= n; ++ i) {
	 	cin >> a[i];
	}
	int ans = 0;
	for (int i = 1; i <= n - 2; ++ i) {
	//	if (a[i + 2] - a[i] > d) continue;
		int l = i;
		int r = n + 1;
		while (r - l > 1) {
			int mid = (l + r) / 2;
			if ((a[mid] - a[i]) <= d) {
				l = mid;
			}
			else {
		 		r = mid;
			}
		}
		if ((a[l] - a[i]) <= d) {
		 	ans += ((l - i) * (l - i - 1)) / 2;
		}
	}
	cout << ans;
	return 0;
}


/*
 (c) Magaa :)
*/
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(s) ((int)(s.size()))
#define all(s) s.begin(), s.end()

typedef vector < int > vi;
typedef pair < int , int > pii;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const int Mass = (int) 2e5 + 5;

int a[Mass];

int main () {
	#ifdef Home
		freopen(".in", "r", stdin);
		freopen(".out", "w", stdout);
	#endif
	ios_base :: sync_with_stdio(false); cin.tie(NULL);
	int n , d;
	cin >> n >> d;
	for (int i = 1; i <= n; ++ i) {
	 	cin >> a[i];
	}
	int ans = 0;
	for (int i = 1; i <= n - 2; ++ i) {
		int l = i;
		int r = n + 1;
		while (r - l > 1) {
			int mid = (l + r) / 2;
			if ((max (abs(a[mid]) , abs (a[i])) - min (abs(a[mid]) , abs (a[i]))) <= d) {
				l = mid;
			}
			else {
		 		r = mid;
			}
		}
		if (l - i >= 2 && (max (abs(a[l]) , abs (a[i])) - min (abs(a[l]) , abs (a[i]))) <= d) {
		 	ans += ((l - i) * (l - i - 1)) / 2;
		}
	}
	cout << ans;
	return 0;
}


/*
 (c) Magaa :)
*/
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(s) ((int)(s.size()))
#define all(s) s.begin(), s.end()

typedef vector < int > vi;
typedef pair < int , int > pii;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const int Mass = (int) 2e5 + 5;

int a[Mass];

int main () {
	#ifdef Home
		freopen(".in", "r", stdin);
		freopen(".out", "w", stdout);
	#endif
	ios_base :: sync_with_stdio(false); cin.tie(NULL);
	int n , d;
	cin >> n >> d;
	for (int i = 1; i <= n; ++ i) {
	 	cin >> a[i];
	}
	ll ans = 0;
	for (int i = 1; i <= n - 2; ++ i) {
		/*if (a[i + 2] - a[i] > d) continue;
		if (a[n] - a[i] <= d) {
			ans += ((n - i - 1) * (n - i)) / 2;
			continue;
		}
		*/
		int l = i;
		int r = n;
		while (r - l > 1) {
			int mid = (l + r) / 2;
			if ((a[mid] - a[i]) <= d) {
				l = mid;
			}
			else {
		 		r = mid - 1;
			}
		}
		if ((a[l] - a[i]) <= d) {
		 	ans += ((l - i) * (l - i - 1)) / 2;
		}
	}
	cout << ans;
	return 0;
}


/*
 (c) Magaa :)
*/
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(s) ((int)(s.size()))
#define all(s) s.begin(), s.end()

typedef vector < int > vi;
typedef pair < int , int > pii;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const int Mass = (int) 2e5 + 5;

int a[Mass];

int main () {
	#ifdef Home
		freopen(".in", "r", stdin);
		freopen(".out", "w", stdout);
	#endif
	ios_base :: sync_with_stdio(false); cin.tie(NULL);
	int n , d;
	cin >> n >> d;
	for (int i = 1; i <= n; ++ i) {
	 	cin >> a[i];
	}
	int ans = 0;
	for (int i = 1; i <= n - 2; ++ i) {
		if (a[i + 2] - a[i] > d) continue;
		int l = i;
		int r = n + 1;
		while (r - l > 1) {
			int mid = (l + r) / 2;
			if ((a[mid] - a[i]) <= d) {
				l = mid;
			}
			else {
		 		r = mid;
			}
		}
		if (l - i >= 2 && (a[l] - a[i]) <= d) {
		 	ans += ((l - i) * (l - i - 1)) / 2;
		}
	}
	cout << ans;
	return 0;
}


/*
 (c) Magaa :)
*/
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(s) ((int)(s.size()))
#define all(s) s.begin(), s.end()

typedef vector < int > vi;
typedef pair < int , int > pii;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const int Mass = (int) 2e5 + 5;

int a[Mass];

int main () {
	#ifdef Home
		freopen(".in", "r", stdin);
		freopen(".out", "w", stdout);
	#endif
	ios_base :: sync_with_stdio(false); cin.tie(NULL);
	int n , d;
	cin >> n >> d;
	for (int i = 1; i <= n; ++ i) {
	 	cin >> a[i];
	}
	int ans = 0;
	for (int i = 1; i <= n - 2; ++ i) {
		int l = 1;
		int r = n + 1;
		while (r - l > 1) {
			int mid = (l + r) / 2;
			if (max (abs(a[mid]) , abs (a[i])) - min (abs(a[mid]) , abs (a[i])) <= d) {
				l = mid;
			}
			else {
		 		r = mid;
			}
		}
		if (l - i >= 1) {
		 	ans += (l - i) * (l - i - 1) / 2;
		}
	}
	cout << ans;
	return 0;
}


/*
 (c) Magaa :)
*/
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(s) ((int)(s.size()))
#define all(s) s.begin(), s.end()

typedef vector < int > vi;
typedef pair < int , int > pii;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const int Mass = (int) 2e5 + 5;

int a[Mass];

int main () {
	#ifdef Home
		freopen(".in", "r", stdin);
		freopen(".out", "w", stdout);
	#endif
	ios_base :: sync_with_stdio(false); cin.tie(NULL);
	int n , d;
	cin >> n >> d;
	for (int i = 1; i <= n; ++ i) {
	 	cin >> a[i];
	}
	int ans = 0;
	for (int i = 1; i <= n - 2; ++ i) {
		int l = 1;
		int r = n + 1;
		while (r - l > 1) {
			int mid = (l + r) / 2;
			if (max (abs(a[mid]) , abs (a[i])) - min (abs(a[mid]) , abs (a[i])) <= d) {
				l = mid;
			}
			else {
		 		r = mid;
			}
		}
		if (l - i >= 2) {
		 	ans += (l - i) * (l - i - 1) / 2;
		}
	}
	cout << ans;
	return 0;
}


/*
 (c) Magaa :)
*/
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(s) ((int)(s.size()))
#define all(s) s.begin(), s.end()

typedef vector < int > vi;
typedef pair < int , int > pii;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const int Mass = (int) 2e5 + 5;

int a[Mass];

int main () {
	#ifdef Home
		freopen(".in", "r", stdin);
		freopen(".out", "w", stdout);
	#endif
	ios_base :: sync_with_stdio(false); cin.tie(NULL);
	int n , d;
	cin >> n >> d;
	for (int i = 1; i <= n; ++ i) {
	 	cin >> a[i];
	}
	int ans = 0;
	for (int i = 1; i <= n - 2; ++ i) {
		int l = i;
		int r = n + 1;
		while (r - l > 1) {
			int mid = (l + r) / 2;
			if (max (abs(a[mid]) , abs (a[i])) - min (abs(a[mid]) , abs (a[i])) <= d) {
				l = mid;
			}
			else {
		 		r = mid;
			}
		}
		if (l - i >= 1) {
		 	ans += (l - i) * (l - i - 1) / 2;
		}
	}
	cout << ans;
	return 0;
}


/*
 (c) Magaa :)
*/
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(s) ((int)(s.size()))
#define all(s) s.begin(), s.end()

typedef vector < int > vi;
typedef pair < int , int > pii;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const int Mass = (int) 2e5 + 5;

int a[Mass];

int main () {
	#ifdef Home
		freopen(".in", "r", stdin);
		freopen(".out", "w", stdout);
	#endif
	ios_base :: sync_with_stdio(false); cin.tie(NULL);
	int n , d;
	cin >> n >> d;
	for (int i = 1; i <= n; ++ i) {
	 	cin >> a[i];
	}
	ll ans = 0;
	for (int i = 1; i <= n - 2; ++ i) {
	//	if (a[i + 2] - a[i] > d) continue;
		int l = i;
		int r = n + 1;
		while (r - l > 1) {
			int mid = (l + r) / 2;
			if ((a[mid] - a[i]) <= d) {
				l = mid;
			}
			else {
		 		r = mid;
			}
		}
		if ((a[l] - a[i]) <= d) {
		 	ans += ((l - i) * (l - i - 1)) / 2;
		}
	}
	cout << ans;
	return 0;
}


/*
 (c) Magaa :)
*/
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(s) ((int)(s.size()))
#define all(s) s.begin(), s.end()

typedef vector < int > vi;
typedef pair < int , int > pii;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const int Mass = (int) 2e5 + 5;

int a[Mass];

int main () {
	#ifdef Home
		freopen(".in", "r", stdin);
		freopen(".out", "w", stdout);
	#endif
	ios_base :: sync_with_stdio(false); cin.tie(NULL);
	int n , d;
	cin >> n >> d;
	for (int i = 1; i <= n; ++ i) {
	 	cin >> a[i];
	}
	ull ans = 0;
	for (int i = 1; i <= n - 2; ++ i) {
	//	if (a[i + 2] - a[i] > d) continue;
		int l = i;
		int r = n + 1;
		while (r - l > 1) {
			int mid = (l + r) / 2;
			if ((a[mid] - a[i]) <= d) {
				l = mid;
			}
			else {
		 		r = mid;
			}
		}
		if ((a[l] - a[i]) <= d) {
		 	ans += ((l - i) * 1ll * (l - i - 1) * 1ll) / 2ll;
		}
	}
	cout << ans;
	return 0;
}


/*
 (c) Magaa :)
*/
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(s) ((int)(s.size()))
#define all(s) s.begin(), s.end()

typedef vector < int > vi;
typedef pair < int , int > pii;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const int Mass = (int) 2e5 + 5;

int a[Mass];

int main () {
	#ifdef Home
		freopen(".in", "r", stdin);
		freopen(".out", "w", stdout);
	#endif
	ios_base :: sync_with_stdio(false); cin.tie(NULL);
	int n , d;
	cin >> n >> d;
	for (int i = 1; i <= n; ++ i) {
	 	cin >> a[i];
	}
	ull ans = 0;
	for (int i = 1; i <= n - 2; ++ i) {
	//	if (a[i + 2] - a[i] > d) continue;
		int l = i;
		int r = n + 1;
		while (r - l > 1) {
			int mid = (l + r) / 2;
			if ((a[mid] - a[i]) <= d) {
				l = mid;
			}
			else {
		 		r = mid;
			}
		}
		if ((a[l] - a[i]) <= d) {
		 	ans += ((l - i) * (l - i - 1)) / 2;
		}
	}
	cout << ans;
	return 0;
}


/*
 (c) Magaa :)
*/
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(s) ((int)(s.size()))
#define all(s) s.begin(), s.end()

typedef vector < int > vi;
typedef pair < int , int > pii;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const int Mass = (int) 2e5 + 5;

int a[Mass];

int main () {
	#ifdef Home
		freopen(".in", "r", stdin);
		freopen(".out", "w", stdout);
	#endif
	ios_base :: sync_with_stdio(false); cin.tie(NULL);
	int n , d;
	cin >> n >> d;
	for (int i = 1; i <= n; ++ i) {
	 	cin >> a[i];
	}
	int ans = 0;
	for (int i = 1; i <= n - 2; ++ i) {
		if (a[i + 2] - a[i] > d) continue;
		int l = i;
		int r = n + 1;
		while (r - l > 1) {
			int mid = (l + r) / 2;
			if ((a[mid] - a[i]) <= d) {
				l = mid;
			}
			else {
		 		r = mid;
			}
		}
		if ((a[l] - a[i]) <= d) {
		 	ans += ((l - i) * (l - i - 1)) / 2;
		}
	}
	cout << ans;
	return 0;
}


/*
 (c) Magaa :)
*/
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(s) ((int)(s.size()))
#define all(s) s.begin(), s.end()

typedef vector < int > vi;
typedef pair < int , int > pii;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const int Mass = (int) 2e5 + 5;

int a[Mass];

int main () {
	#ifdef Home
		freopen(".in", "r", stdin);
		freopen(".out", "w", stdout);
	#endif
	ios_base :: sync_with_stdio(false); cin.tie(NULL);
	int n , d;
	cin >> n >> d;
	for (int i = 1; i <= n; ++ i) {
	 	cin >> a[i];
	}
	int ans = 0;
	for (int i = 1; i <= n - 2; ++ i) {
		int l = i;
		int r = n + 1;
		while (r - l > 1) {
			int mid = (l + r) / 2;
			if (max (abs(a[mid]) , abs (a[i])) - min (abs(a[mid]) , abs (a[i])) <= d) {
				l = mid;
			}
			else {
		 		r = mid;
			}
		}
		if (l - i >= 2) {
		 	ans += (l - i) * (l - i - 1) / 2;
		}
	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int lli;

int main()
{

   lli n,d,i,x,j;

   vector<lli> a;

   //cout<<"Enter n and d :"<<endl;
   cin>>n>>d;

   //cout<<"Enter arr; "<<endl;

   for(i=0;i<n;i++)
   {
       cin>>x;
       a.push_back(x);
   }

sort(a.begin(),a.end());

   //if(n<=2)  {  return 0;  }

   i = 0;
   j = 2;
   lli n1,ans=0;

   while(j<n)
   {
       if(a[j]-a[i]<=d)
       {
           n1 = j-i;
           ans += (n1*(n1-1))/2;
           j++;

       }
       else{
            if((j-i)==1)  { i++; j++; }
            else{

                   i++;
            }
       }

   }

   cout<<ans;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int lli;

int main()
{

   lli n,d,i,x,j;

   vector<lli> a;

   //cout<<"Enter n and d :"<<endl;
   cin>>n>>d;

   //cout<<"Enter arr; "<<endl;

   for(i=0;i<n;i++)
   {
       cin>>x;
       a.push_back(x);
   }

   //if(n<=2)  {  return 0;  }

   i = 0;
   j = 2;
   lli n1,ans=0;

   while(i<n)
   {
       while(j<n && a[j]-a[i]<=d)
       {
           n1 = j-i;
           ans += (n1*(n1-1))/2;
           j++;

       }

       i++;

   }

   cout<<ans;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int lli;

int main()
{

   lli n,d,i,x,j;

   vector<lli> a;

   //cout<<"Enter n and d :"<<endl;
   cin>>n>>d;

   //cout<<"Enter arr; "<<endl;

   for(i=0;i<n;i++)
   {
       cin>>x;
       a.push_back(x);
   }

sort(a.begin(),a.end());

   if(n<=2)  {  return 0;  }

   i = 0;
   j = 2;
   lli n1,ans=0;

   while(j<n)
   {
       if(a[j]-a[i]<=d)
       {
           n1 = j-i;
           ans += (n1*(n1-1))/2;
           j++;

       }
       else{
            if((j-i)==1)  { i++; j++; }
            else{

                   i++;
            }
       }

   }

   cout<<ans;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int lli;

int main()
{

   lli n,d,i,x,j;

   vector<lli> a;

   //cout<<"Enter n and d :"<<endl;
   cin>>n>>d;

   //cout<<"Enter arr; "<<endl;

   for(i=0;i<n;i++)
   {
       cin>>x;
       a.push_back(x);
   }

sort(a.begin(),a.end());

   i = 0;
   j = 2;
   lli n1,ans=0;

   while(j-i>=2)
   {
       if(a[j]-a[i]<=d)
       {
           n1 = j-i;
           ans += (n1*(n1-1))/2;
           j++;
       }
       else{
        i++;
       }

   }

   cout<<ans;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int lli;

int main()
{

   lli n,d,i,x,j;

   vector<lli> a;

   //cout<<"Enter n and d :"<<endl;
   cin>>n>>d;

   //cout<<"Enter arr; "<<endl;

   for(i=0;i<n;i++)
   {
       cin>>x;
       a.push_back(x);
   }

   //if(n<=2)  {  return 0;  }

   i = 0;
   j = 2;
   lli n1,ans=0;

   while(j<n)
   {
       if(a[j]-a[i]<=d)
       {
           n1 = j-i;
           ans += (n1*(n1-1))/2;
           j++;

       }
       else{
            if((j-i)==1)  { i++; j++; }
            else{

                   i++;
            }
       }

   }

   cout<<ans;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int lli;

int main()
{

   lli n,d,i,x,j;

   vector<lli> a;

   //cout<<"Enter n and d :"<<endl;
   cin>>n>>d;

   //cout<<"Enter arr; "<<endl;

   for(i=0;i<n;i++)
   {
       cin>>x;
       a.push_back(x);
   }

   //if(n<=2)  {  return 0;  }

   i = 0;
   j = 2;
   lli n1,ans=0;

   while(i<n)
   {
       while(a[j]-a[i]<=d)
       {
           n1 = j-i;
           ans += (n1*(n1-1))/2;
           j++;

       }

       i++;

   }

   cout<<ans;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int lli;

int main()
{

   lli n,d,i,x,j;

   vector<lli> a;

   //cout<<"Enter n and d :"<<endl;
   cin>>n>>d;

   //cout<<"Enter arr; "<<endl;

   for(i=0;i<n;i++)
   {
       cin>>x;
       a.push_back(x);
   }

   //if(n<=2)  {  return 0;  }

   i = 0;
   j = 2;
   lli n1,ans=0;

   while(i<n)
   {
       while(j<n && a[j]-a[i]<=d)
       {
           j++;
       }
        n1 = j-i-1;
           ans += (n1*(n1-1))/2;

       i++;

   }

   cout<<ans;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int lli;

int main()
{

   lli n,d,i,x,j;

   vector<lli> a;

   //cout<<"Enter n and d :"<<endl;
   cin>>n>>d;

   //cout<<"Enter arr; "<<endl;

   for(i=0;i<n;i++)
   {
       cin>>x;
       a.push_back(x);
   }

   i = 0;
   j = 2;
   lli n1,ans=0;

   while(j-i>=2)
   {
       if(a[j]-a[i]<=d)
       {
           n1 = j-i;
           ans += (n1*(n1-1))/2;
           j++;
       }
       else{
        i++;
       }

   }

   cout<<ans;

    return 0;
}


#include<bits/stdc++.h>
using namespace std ;
#define ll long long
#define all(x) x.begin(),x.end()

int const N = 1e6 + 1;
int n , d , v[N],fr[N];
multiset<int>mst;

inline int fnd(int need){
     int l = 0 , r = n-1, mid, bst;
     while(l <= r){
       mid = (l+r)/2;
       if(v[mid] > need)r = mid - 1;
       if(v[mid] < need)l = mid + 1;
       if(v[mid] == need)return mid + fr[v[mid]] - 1;
     }
     return 0 ;
}

int main(){
    scanf("%d%d",&n,&d);
    for(int i=0;i<n;++i){scanf("%d",v+i);mst.insert(v[i]);++fr[v[i]];}
    ll an = 0 ;
    for(int i=0;i<n;++i){
       int low = v[i];
       auto it = mst.lower_bound(v[i]+d);
       if(it == mst.end())--it;
       int k  = 0 ;
       while(*it > v[i]+d){
          --it;
       }
       int pos = fnd(*it);
       k = abs(pos - i);
       if(k < 2)continue;
       an += (k * (k-1))/2;
    }
    cout << an << "\n";
}


#include<bits/stdc++.h>
using namespace std ;
#define ll long long
#define all(x) x.begin(),x.end()

int const N = 1e6 + 1;
int n , d , v[N];
multiset<int>mst;
map<int,int>fr;

inline int fnd(int need){
     int l = 0 , r = n-1, mid, bst;
     while(l <= r){
       mid = (l+r)/2;
       if(v[mid] > need)r = mid - 1;
       if(v[mid] < need)l = mid + 1;
       if(v[mid] == need)return mid + fr[v[mid]] - 1;
     }
     return 0 ;
}

int main(){
    scanf("%d%d",&n,&d);
    for(int i=0;i<n;++i){scanf("%d",v+i);mst.insert(v[i]);++fr[v[i]];}
    ll an = 0 ;
    for(int i=0;i<n;++i){
       int low = v[i];
       auto it = mst.lower_bound(v[i]+d);
       if(it == mst.end())--it;
       int k  = 0 ;
       while(*it > v[i]+d){
          --it;
       }
       int pos = fnd(*it);
       k = abs(pos - i);
       if(k < 2)continue;
       an = (ll)an + (1LL * k * (k-1))/2;
    }
    cout << an << "\n";
}


#include<bits/stdc++.h>
using namespace std ;
#define ll long long
#define all(x) x.begin(),x.end()

int const N = 1e6 + 1;
int n , d , v[N];
multiset<int>mst;

inline int fnd(int need){
     int l = 0 , r = n-1, mid, bst;
     while(l <= r){
       mid = (l+r)/2;
       if(v[mid] > need)r = mid - 1;
       if(v[mid] < need)l = mid + 1;
       if(v[mid] == need)return mid;
     }
     return 0 ;
}

int main(){
    scanf("%d%d",&n,&d);
    for(int i=0;i<n;++i){scanf("%d",v+i);mst.insert(v[i]);}
    ll an = 0 ;
    for(int i=0;i<n;++i){
       int low = v[i];
       auto it = mst.lower_bound(v[i]+d);
       if(it == mst.end())--it;
       int k  = 0 ;
       while(*it > v[i]+d){
          --it;
       }
       int pos = fnd(*it);
       k = abs(pos - i);
       if(k < 2)continue;
       an += (k * (k-1))/2;
    }
    cout << an << "\n";
}


#include<bits/stdc++.h>
using namespace std ;
#define ll long long
#define all(x) x.begin(),x.end()

int const N = 1e6 + 1;
int n , d , v[N];
multiset<int>mst;
map<int,int>fr;

inline int fnd(int need){
     int l = 0 , r = n-1, mid, bst;
     while(l <= r){
       mid = (l+r)/2;
       if(v[mid] > need)r = mid - 1;
       if(v[mid] < need)l = mid + 1;
       if(v[mid] == need)return mid + fr[v[mid]] - 1;
     }
     return 0 ;
}

int main(){
    scanf("%d%d",&n,&d);
    for(int i=0;i<n;++i){scanf("%d",v+i);mst.insert(v[i]);++fr[v[i]];}
    ll an = 0 ;
    for(int i=0;i<n;++i){
       int low = v[i];
       auto it = mst.lower_bound(v[i]+d);
       if(it == mst.end())--it;
       int k  = 0 ;
       while(*it > v[i]+d){
          --it;
       }
       int pos = fnd(*it);
       k = abs(pos - i);
       if(k < 2)continue;
       an += (k * (k-1))/2;
    }
    cout << an << "\n";
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;

int a[100005];

int main()
{
    int n,d;
    cin>>n>>d;
    for(int i=0;i<n;i++)
        cin>>a[i];

    int i=0,j=0;
    ll ans=0,k;
    while(i<n)
    {
        while(a[j]-a[i]<=d && j<n)
            j++;
        k=j-i-1;
        ans=ans+(k*(k-1))/2;
        i++;
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define FIO ios_base::sync_with_stdio(false),cin.tie(NULL);

#define ll              long long
#define ull             unsinged long long
#define vi              vector<int>
#define vll             vector<ll>
#define ii              pair<int, int>
#define vii             vector<pair<int, int> >
#define vs              vector<string>
#define vd              vector<double>
#define mii             map<int, int>
#define mli             map<ll, int>
#define msi             map<string, int>

#define pb              push_back
#define mp              make_pair
#define ff              first
#define ss              second
#define sz              size()
#define all(a)          a.begin(), a.end()
#define rep0(i, k)      for(int i=0;i<k;i++)
#define rep1(i, k)      for(int i=1;i<=k;i++)
#define repab(i, a, b)  for(int i=a;i<=b;i++)
#define repba(i, b, a)  for(int i=b;i>=a;i--)
#define pi              acos(-1.0)
#define eps             1e-6
#define mem(a, b)       memset(a, b, sizeof(a))
#define mod             1000000007
#define inf             1e9

const int N = 100009;

int a[N], b[N];

int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int n, d;
	cin >> n >> d;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	if (n <= 2)
	{
		puts("0");
		return 0;
	}

	int i = 1, j = 1;

	while (i <= n - 2)
	{
		if (j < i)
			j = i;

		while (a[j] - a[i] <= d && j <= n)
		{
			j++;
		}
		j--;
		b[i] = j;

		i++;
	}

	ll ans = 0;
	for (int i = 1; i <= n-2; i++)
	{
		int x = b[i] - i + 1;
		if (x >= 3)
			ans = ans + ((x - 1) * (x - 2)) / 2;
	}

	cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;

#define FIO ios_base::sync_with_stdio(false),cin.tie(NULL);

#define ll              long long
#define ull             unsinged long long
#define vi              vector<int>
#define vll             vector<ll>
#define ii              pair<int, int>
#define vii             vector<pair<int, int> >
#define vs              vector<string>
#define vd              vector<double>
#define mii             map<int, int>
#define mli             map<ll, int>
#define msi             map<string, int>

#define pb              push_back
#define mp              make_pair
#define ff              first
#define ss              second
#define sz              size()
#define all(a)          a.begin(), a.end()
#define rep0(i, k)      for(int i=0;i<k;i++)
#define rep1(i, k)      for(int i=1;i<=k;i++)
#define repab(i, a, b)  for(int i=a;i<=b;i++)
#define repba(i, b, a)  for(int i=b;i>=a;i--)
#define pi              acos(-1.0)
#define eps             1e-6
#define mem(a, b)       memset(a, b, sizeof(a))
#define mod             1000000007
#define inf             1e9

const int N = 100009;

ll a[N], b[N];

int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int n, d;
	cin >> n >> d;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	if (n <= 2)
	{
		puts("0");
		return 0;
	}

	int i = 1, j = 1;

	while (i <= n - 2)
	{
		if (j < i)
			j = i;

		while (a[j] - a[i] <= d && j <= n)
		{
			j++;
		}
		j--;
		b[i] = j;

		i++;
	}

	ll ans = 0;
	for (ll i = 1; i <= n-2; i++)
	{
		ll x = b[i] - i + 1;
		if (x >= 3)
			ans = ans + ((x - 1) * (x - 2)) / 2;
	}

	cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fastio ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
//const long long INF64 = 1'000'000'000'000'000'000ll;
double pow(double a, int n){
    double ret =1;
    while(n){
        if(n%2==1)ret*=a;
        a*=a;
        n/=2;
    }
    return ret;
}
#undef int
int main() {
    #define int long long
    //fastio;
    cin>>n>>d;
  for(i=1;i<=n;i++)cin>>a[i];
  for(i=1,j=1;i<=n;i++)
  {
    while(j<=n && a[j]-a[i]<=d)j++;
    ans+=max(0ll,(j-i-1)*(j-i-2)/2);
  }
  cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fastio ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
//const long long INF64 = 1'000'000'000'000'000'000ll;
double pow(double a, int n){
    double ret =1;
    while(n){
        if(n%2==1)ret*=a;
        a*=a;
        n/=2;
    }
    return ret;
}
#undef int
int main() {
    #define int long long
    //fastio;
    int n,d,ans=0;
    cin>>n>>d;
    int i=0,j=0;
    int a[n];
  for(i=1;i<=n;i++)cin>>a[i];
  for(i=1,j=1;i<=n;i++)
  {
    while(j<=n && a[j]-a[i]<=d)j++;
    ans+=max(0ll,(j-i-1)*(j-i-2)/2);
  }
  cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fastio ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
//const long long INF64 = 1'000'000'000'000'000'000ll;
double pow(double a, int n){
    double ret =1;
    while(n){
        if(n%2==1)ret*=a;
        a*=a;
        n/=2;
    }
    return ret;
}
#undef int
int main() {
    #define int long long
    //fastio;
    int n,d;
    cin>>n>>d;
    int arr[n];
    for(int i=0;i<n;i++){
        cin>>arr[i];
    }
    int i=0,ans=0;
    for(int j=0;j<n;j++){
        if(arr[j]-arr[i]>d){
            int m=(j-i);
            ans+=((m)*(--m)*(--m))/(6);
            i++;j--;
        }
        else if(j==n-1){
            int m=j-i+1;
            ans+=((m)*(--m)*(--m))/(6);
        }
    }
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fastio ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
//const long long INF64 = 1'000'000'000'000'000'000ll;
double pow(double a, int n){
    double ret =1;
    while(n){
        if(n%2==1)ret*=a;
        a*=a;
        n/=2;
    }
    return ret;
}
#undef int
int main() {
    #define int long long
    //fastio;
    int n,d;
    cin>>n>>d;
    int arr[n];
    for(int i=0;i<n;i++){
        cin>>arr[i];
    }
    int i=0,ans=0;
    for(int j=0;j<n;j++){
        if(arr[j]-arr[i]>d){
            int m=(j-i);
            ans+=((m)*(m-1)*(m-2))/(6);
            i++;j--;
        }
        else if(j==n-1){
            int m=j-i+1;
            ans+=((m)*(m-1)*(m-2))/(6);
        }
    }
    cout<<ans;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <iomanip>

#define SWS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define ft first
#define sd second
#define read freopen("input.txt", "r", stdin)
#define write freopen("output.txt", "w", stdout)
#define files read; write

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector < int > vi;
typedef pair < int, int > pairt;

const int Z = (int)3e3 + 111;
const int N = (int)1e5 + 111;
const int INF = (int)1e9 + 111;
const int MOD = (int)1e9 + 7;

int main()
{
    SWS;

    int n, d;

    cin >> n >> d;

	vi a(n);

	for(int i = 0;i < n; i++)
		cin >> a[i];

	int p1(0), p2(2);

	if (n < 3)
	{
		cout << 0;
		return 0;
	}

	ll ans(0);

	while (p1 < n && p2 < n)
	{
	//	cout << p2 << ' ' << p1 << endl;
		if (a[p2] - a[p1] <= d && p2 < n)
		{
			while (a[p2] - a[p1] <= d && p2 < n )p2++;
			p2--;
		}
		if (a[p2 ] - a[p1] >= d && p1 < n)
		{
			while (a[p2] - a[p1] >= d && p1 < n) p1++;
			p1--;
		}
		//cout << p2 << ' ' << p1 << endl;
		ans += (p2 - p1) * (p2 - p1 - 1) / 2;
		p1++;
	}

	cout << ans;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <iomanip>

#define SWS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define ft first
#define sd second
#define read freopen("input.txt", "r", stdin)
#define write freopen("output.txt", "w", stdout)
#define files read; write

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector < int > vi;
typedef pair < int, int > pairt;

const int Z = (int)3e3 + 111;
const int N = (int)1e5 + 111;
const int INF = (int)1e9 + 111;
const int MOD = (int)1e9 + 7;

int main()
{
    SWS;

    int n, d;

    cin >> n >> d;

	vi a(n);

	for(int i = 0;i < n; i++)
		cin >> a[i];

	ll p1(0), p2(0);

	if (n < 3)
	{
		cout << 0;
		return 0;
	}

	ll ans(0);

	while (p1 < n && p2 < n)
	{

		if ( a[p2] - a[p1] <= d && p2 < n)
		{
			while (a[p2] - a[p1] <= d && p2 < n )p2++;
			p2--;
		}
	//	if (a[p2 ] - a[p1] >= d && p1 < n)
	//	{
	//		while (a[p2] - a[p1] >= d && p1 < n) p1++;
	//		p1--;
	//	}
	//	cout << p1 << ' ' << p2 << endl;
		if (p2 > p1 && a[p2] - a[p1] <= d)
		ans += (p2 - p1) * (p2 - p1-1 ) / 2;
		//cout << ans << endl;
		p1++;
	}

	cout << ans;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <iomanip>

#define SWS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define ft first
#define sd second
#define read freopen("input.txt", "r", stdin)
#define write freopen("output.txt", "w", stdout)
#define files read; write

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector < int > vi;
typedef pair < int, int > pairt;

const int Z = (int)3e3 + 111;
const int N = (int)1e5 + 111;
const int INF = (int)1e9 + 111;
const int MOD = (int)1e9 + 7;

int main()
{
    SWS;

    int n, d;

    cin >> n >> d;

	vi a(n);

	for(int i = 0;i < n; i++)
		cin >> a[i];

	int p1(0), p2(2);

	if (n < 3)
	{
		cout << 0;
		return 0;
	}

	ll ans(0);

	while (p1 < n && p2 < n)
	{

		if (a[p2] - a[p1] <= d && p2 < n)
		{
			while (a[p2] - a[p1] <= d && p2 < n )p2++;
			p2--;
		}
	//	if (a[p2 ] - a[p1] >= d && p1 < n)
	//	{
	//		while (a[p2] - a[p1] >= d && p1 < n) p1++;
	//		p1--;
	//	}

		if (p2 > p1 && a[p2] - a[p1] <= d)
		ans += (p2 - p1) * (p2 - p1-1 ) / 2;
		//cout << ans << endl;
		p1++;
	}

	cout << ans;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <iomanip>

#define SWS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define ft first
#define sd second
#define read freopen("input.txt", "r", stdin)
#define write freopen("output.txt", "w", stdout)
#define files read; write

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector < int > vi;
typedef pair < int, int > pairt;

const int Z = (int)3e3 + 111;
const int N = (int)1e5 + 111;
const int INF = (int)1e9 + 111;
const int MOD = (int)1e9 + 7;

int main()
{
    SWS;

    int n, d;

    cin >> n >> d;

	vi a(n);

	for(int i = 0;i < n; i++)
		cin >> a[i];

	int p1(0), p2(2);

	if (n < 3)
	{
		cout << 0;
		return 0;
	}

	ll ans(0);

	while (p1 < n && p2 < n)
	{
		if (a[p2] - a[p1] <= d && p2 < n)
		{
			while (a[p2] - a[p1] <= d && p2 < n )p2++;
			p2--;
		}
		//cout << p2 << ' ' << p1 << endl;
		ans += (p2 - p1) * (p2 - p1 - 1) / 2;
		p1++;
	}

	cout << ans;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <iomanip>

#define SWS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define ft first
#define sd second
#define read freopen("input.txt", "r", stdin)
#define write freopen("output.txt", "w", stdout)
#define files read; write

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector < int > vi;
typedef pair < int, int > pairt;

const int Z = (int)3e3 + 111;
const int N = (int)1e5 + 111;
const int INF = (int)1e9 + 111;
const int MOD = (int)1e9 + 7;

int main()
{
    SWS;

    int n, d;

    cin >> n >> d;

	vi a(n);

	for(int i = 0;i < n; i++)
		cin >> a[i];

	int p1(0), p2(2);

	if (n < 3)
	{
		cout << 0;
		return 0;
	}

	ll ans(0);

	while (p1 < n && p2 < n)
	{

		if (a[p2] - a[p1] <= d && p2 < n)
		{
			while (a[p2] - a[p1] <= d && p2 < n )p2++;
			p2--;
		}
		if (a[p2 ] - a[p1] >= d && p1 < n)
		{
			while (a[p2] - a[p1] >= d && p1 < n) p1++;
			p1--;
		}

		if (p2 != p1)
		ans += (p2 - p1) * (p2 - p1-1 ) / 2;
		cout << ans << endl;
		p1++;
	}

	cout << ans;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <iomanip>

#define SWS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define ft first
#define sd second
#define read freopen("input.txt", "r", stdin)
#define write freopen("output.txt", "w", stdout)
#define files read; write

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector < int > vi;
typedef pair < int, int > pairt;

const int Z = (int)3e3 + 111;
const int N = (int)1e5 + 111;
const int INF = (int)1e9 + 111;
const int MOD = (int)1e9 + 7;

int main()
{
    SWS;

    int n, d;

    cin >> n >> d;

	vi a(n);

	for(int i = 0;i < n; i++)
		cin >> a[i];

	int p1(0), p2(2);

	if (n < 3)
	{
		cout << 0;
		return 0;
	}

	ll ans(0);

	while (p1 < n && p2 < n)
	{

		if (a[p2] - a[p1] <= d && p2 < n)
		{
			while (a[p2] - a[p1] <= d && p2 < n )p2++;
			p2--;
		}
		if (a[p2 ] - a[p1] >= d && p1 < n)
		{
			while (a[p2] - a[p1] >= d && p1 < n) p1++;
			p1--;
		}

		if (p2 > p1)
		ans += (p2 - p1) * (p2 - p1-1 ) / 2;
		//cout << ans << endl;
		p1++;
	}

	cout << ans;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <iomanip>

#define SWS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define ft first
#define sd second
#define read freopen("input.txt", "r", stdin)
#define write freopen("output.txt", "w", stdout)
#define files read; write

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector < int > vi;
typedef pair < int, int > pairt;

const int Z = (int)3e3 + 111;
const int N = (int)1e5 + 111;
const int INF = (int)1e9 + 111;
const int MOD = (int)1e9 + 7;

int main()
{
    SWS;

    int n, d;

    cin >> n >> d;

	vi a(n);

	for(int i = 0;i < n; i++)
		cin >> a[i];

	int p1(0), p2(2);

	if (n < 3)
	{
		cout << 0;
		return 0;
	}

	ll ans(0);

	while (p1 < n && p2 < n)
	{

		if (a[p2] - a[p1] <= d && p2 < n)
		{
			while (a[p2] - a[p1] <= d && p2 < n )p2++;
			p2--;
		}
	//	if (a[p2 ] - a[p1] >= d && p1 < n)
	//	{
	//		while (a[p2] - a[p1] >= d && p1 < n) p1++;
	//		p1--;
	//	}

		if (p2 > p1)
		ans += (p2 - p1) * (p2 - p1-1 ) / 2;
		//cout << ans << endl;
		p1++;
	}

	cout << ans;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <iomanip>

#define SWS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define ft first
#define sd second
#define read freopen("input.txt", "r", stdin)
#define write freopen("output.txt", "w", stdout)
#define files read; write

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector < int > vi;
typedef pair < int, int > pairt;

const int Z = (int)3e3 + 111;
const int N = (int)1e5 + 111;
const int INF = (int)1e9 + 111;
const int MOD = (int)1e9 + 7;

int main()
{
    SWS;

    int n, d;

    cin >> n >> d;

	vi a(n);

	for(int i = 0;i < n; i++)
		cin >> a[i];

	int p1(0), p2(0);

	if (n < 3)
	{
		cout << 0;
		return 0;
	}

	ll ans(0);

	while (p1 < n && p2 < n)
	{

		if (a[p2] - a[p1] <= d && p2 < n)
		{
			while (abs(a[p2] - a[p1]) <= d && p2 < n )p2++;
			p2--;
		}
	//	if (a[p2 ] - a[p1] >= d && p1 < n)
	//	{
	//		while (a[p2] - a[p1] >= d && p1 < n) p1++;
	//		p1--;
	//	}

		if (p2 > p1 && abs(a[p2] - a[p1]) <= d)
		ans += (p2 - p1) * (p2 - p1-1 ) / 2;
		//cout << ans << endl;
		p1++;
	}

	cout << ans;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <iomanip>

#define SWS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define ft first
#define sd second
#define read freopen("input.txt", "r", stdin)
#define write freopen("output.txt", "w", stdout)
#define files read; write

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector < int > vi;
typedef pair < int, int > pairt;

const int Z = (int)3e3 + 111;
const int N = (int)1e5 + 111;
const int INF = (int)1e9 + 111;
const int MOD = (int)1e9 + 7;

int main()
{
    SWS;

    int n, d;

    cin >> n >> d;

	vi a(n);

	for(int i = 0;i < n; i++)
		cin >> a[i];

	int p1(0), p2(0);

	if (n < 3)
	{
		cout << 0;
		return 0;
	}

	ll ans(0);

	while (p1 < n && p2 < n)
	{

		if (a[p2] - a[p1] <= d && p2 < n)
		{
			while (a[p2] - a[p1] <= d && p2 < n )p2++;
			p2--;
		}
	//	if (a[p2 ] - a[p1] >= d && p1 < n)
	//	{
	//		while (a[p2] - a[p1] >= d && p1 < n) p1++;
	//		p1--;
	//	}

		if (p2 > p1 && a[p2] - a[p1] <= d)
		ans += (p2 - p1) * (p2 - p1-1 ) / 2;
		//cout << ans << endl;
		p1++;
	}

	cout << ans;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <iomanip>

#define SWS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define ft first
#define sd second
#define read freopen("input.txt", "r", stdin)
#define write freopen("output.txt", "w", stdout)
#define files read; write

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector < int > vi;
typedef pair < int, int > pairt;

const int Z = (int)3e3 + 111;
const int N = (int)1e5 + 111;
const int INF = (int)1e9 + 111;
const int MOD = (int)1e9 + 7;

int main()
{
    SWS;

    int n, d;

    cin >> n >> d;

	vi a(n);

	for(int i = 0;i < n; i++)
		cin >> a[i];

	int p1(0), p2(2);

	if (n < 3)
	{
		cout << 0;
		return 0;
	}

	ll ans(0);

	while (p1 < n)
	{
		while (a[p2] - a[p1] <= d && p2 < n )p2++;
		p2--;
		//cout << p2 << ' ' << p1 << endl;
		ans += (p2 - p1) * (p2 - p1 - 1) / 2;
		p1++;
	}

	cout << ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
	long int n,d;
	cin>>n>>d;
	vector<long int>V(n);
	for(int i=0;i<n;i++){
	    cin>>V[i];
	}
	int count = 0;
	int i,j;
	for(i=0,j=2;j<n && i<j-1;){
	    if(V[j]-V[i]<d){
	        count = count + (j-i-1);
	        j++;
	    }
	    else if(V[j]-V[i]>d){
	        i++;
	    }
	    else if(V[j]-V[i]==d && j-2 == i){
	        count = count + (j-i-1);
	        i++;
	        j++;
	    }
	    else{
	        count = count + (j-i-1);
	        i++;
	    }
	}
	if(j==n){
	    i++;
	    j--;
	}
	while(i<j-1){
	    if(V[j]-V[i]<=d){
	        count = count + (j-i-1);
	        i++;
	    }
	}
	cout<<count<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
	long int n,d;
	cin>>n>>d;
	vector<long int>V(n);
	for(int i=0;i<n;i++){
	    cin>>V[i];
	}
	int count = 0;
	int i,j;
	for(i=0,j=2;j<n && i<j-1;){
	    if(V[j]-V[i]<d){
	        count = count + (j-i-1);
	        j++;
	    }
	    else if(V[j]-V[i]>d && j-2 == i){
	        i++;
	        j++;
	    }
	    else if(V[j]-V[i]>d){
	        i++;
	    }
	    else if(V[j]-V[i]==d && j-2 == i){
	        count = count + (j-i-1);
	        i++;
	        j++;
	    }
	    else{
	        count = count + (j-i-1);
	        i++;
	    }
	}
	if(j==n){
	    i++;
	    j--;
	}
	while(i<j-1){
	    if(V[j]-V[i]<=d){
	        count = count + (j-i-1);
	        i++;
	    }
	}
	cout<<count<<endl;
	return 0;
}


# include <bits/stdc++.h>
using namespace std;

int main(){
    long int n,d;
    cin>>n>>d;
    vector<long int>V(n,d);
    for(long int i=0;i<n;i++){
        cin>>V[i];
    }
    long int i=0,j=2;
    long count = 0;
    while(i<n-2){
        while(j<n && (V[j]-V[i]<=d)){
            count = count + (j-i-1)*(j-i)/2;
            j++;
        }
        i++;
    }
    cout<<count<<endl;
    return 0;
}


# include <bits/stdc++.h>
using namespace std;

int main(){
    long long int n,d;
    cin>>n>>d;
    vector<long long int>V(n,d);
    for(long int i=0;i<n;i++){
        cin>>V[i];
    }
    long long int i=0,j=2;
    long long count = 0;
    while(i<n-2){
        while(j<n && (V[j]-V[i]<=d)){
            count = count + (j-i-1)*(j-i)/2;
            j++;
        }
        i++;
    }
    cout<<count<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define vec vector
#define fe first
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define se second
#define pb push_back
#define eb emplace_back
#define inf INT_MAX
#define is ==
#define mp make_pair
#define vec vector
#define endl '\n'
#define int long long
#define fr0(i,a,b) for(int i=a;i<b;i++)
#define fr1(i,a,b) for(int i=a;i<=b;i++)
#define frn0(i,a,b) for(int i=b-1;i>=a;i--)
#define frn1(i,a,b) for(int i=b;i>a;i--)

#define tc(t) ul testcases ; cin>>testcases ; for ( int t = 1 ; t <= testcases ; t++ )
#define tr(container,it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define all(v) (v).begin() , (v).end()
#define sumof(v) accumulate ( all(v) , 0LL )
#define maxof(v) *(max_element( all(v) ))
#define minof(v) *(min_element( all(v) ))
#define rev(v) reverse(all(v))
#define sz(v)  (long long)(v).size()

typedef unsigned long long ul ; typedef long long ll ; typedef string str ;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int, int> pii;
typedef pair<ll, pair<ll,ll>> pll;
typedef vector<pii> vii;

main(){
    FASTIO
    cout<<fixed<<setprecision(0);
    int n,d;
    cin>>n>>d;
    int a[n];
    fr0(i,0,n) cin>>a[i];
    int i =0 ,j = 0;
    int b[n] = {0};
    while(j<n && i<n){
        //cout<<i<<" "<<j<<endl;
        if(a[j]-a[i]<=d) j++;
        else{
            b[i] = j-i;
            i++;
        }
    }
    b[i] = j-i;
    int s = 0;
    fr0(i,0,n-2){
        s += (b[i]*(b[i]-1)*(b[i]-2))/6;
    }
    cout<<s;
}


#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define vec vector
#define fe first
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define se second
#define pb push_back
#define eb emplace_back
#define inf INT_MAX
#define is ==
#define mp make_pair
#define vec vector
#define endl '\n'
#define int long long
#define fr0(i,a,b) for(int i=a;i<b;i++)
#define fr1(i,a,b) for(int i=a;i<=b;i++)
#define frn0(i,a,b) for(int i=b-1;i>=a;i--)
#define frn1(i,a,b) for(int i=b;i>a;i--)

#define tc(t) ul testcases ; cin>>testcases ; for ( int t = 1 ; t <= testcases ; t++ )
#define tr(container,it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define all(v) (v).begin() , (v).end()
#define sumof(v) accumulate ( all(v) , 0LL )
#define maxof(v) *(max_element( all(v) ))
#define minof(v) *(min_element( all(v) ))
#define rev(v) reverse(all(v))
#define sz(v)  (long long)(v).size()

typedef unsigned long long ul ; typedef long long ll ; typedef string str ;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int, int> pii;
typedef pair<ll, pair<ll,ll>> pll;
typedef vector<pii> vii;

main(){
    FASTIO
    cout<<fixed<<setprecision(0);
    int n,d;
    cin>>n>>d;
    int a[n];
    fr0(i,0,n) cin>>a[i];
    int s =0;
    fr0(i,0,n){
        int k = i-(lower_bound(a,a+i,a[i]-d)-a);
        s += (k*(k-1))/2;
    }
    cout<<s;
}


#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define vec vector
#define fe first
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define se second
#define pb push_back
#define eb emplace_back
#define inf INT_MAX
#define is ==
#define mp make_pair
#define vec vector
#define endl '\n'
#define int long long
#define fr0(i,a,b) for(int i=a;i<b;i++)
#define fr1(i,a,b) for(int i=a;i<=b;i++)
#define frn0(i,a,b) for(int i=b-1;i>=a;i--)
#define frn1(i,a,b) for(int i=b;i>a;i--)

#define tc(t) ul testcases ; cin>>testcases ; for ( int t = 1 ; t <= testcases ; t++ )
#define tr(container,it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define all(v) (v).begin() , (v).end()
#define sumof(v) accumulate ( all(v) , 0LL )
#define maxof(v) *(max_element( all(v) ))
#define minof(v) *(min_element( all(v) ))
#define rev(v) reverse(all(v))
#define sz(v)  (long long)(v).size()

typedef unsigned long long ul ; typedef long long ll ; typedef string str ;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int, int> pii;
typedef pair<ll, pair<ll,ll>> pll;
typedef vector<pii> vii;

main(){
    FASTIO
    cout<<fixed<<setprecision(0);
    int n,d;
    cin>>n>>d;
    int a[n];
    fr0(i,0,n) cin>>a[i];
    int b[n] = {0};
    fr0(i,0,n-2){
        int pos = (lower_bound(a+i,a+n,a[i]+d)-a);
        b[i] = pos - i;
        if(a[pos]-a[i] == d) b[i]++;
    }
    /*fr0(i,0,n-2) cout<<b[i]<<" ";
    cout<<endl;*/
    int s = 0;
    fr0(i,0,n-2){
        s += (b[i]*(b[i]-1)*(b[i]-2))/6;
        if(b[i]>3) i += b[i]-3;
    }
    cout<<s;
}


#include <bits/stdc++.h>
using namespace std;
#define maxx 100100
typedef long long ll;
int main() {
	vector<int>vc; set<int>st;
	int n,d; cin >> n >> d;
	for(int i = 0; i < n; i++){
		int a; cin >> a; vc.push_back(a); st.insert(a);
	}
	ll ans = 0ll;
	for(int i = 0; i < n; i++){
		int l = upper_bound(vc.begin(),vc.end(),vc[i]+d) - vc.begin() - 1;
		ans += ((l-i) * (l-i-1)) / 2;
	}
	cout << ans << "\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define maxx 100100
typedef long long ll;
int main() {
	vector<ll>vc;
	ll n,d; cin >> n >> d;
	for(int i = 0; i < n; i++){
		ll a; cin >> a; vc.push_back(a);
	}
	ll ans = 0ll;
	for(int i = 0; i < n; i++){
		ll l = upper_bound(vc.begin(),vc.end(),vc[i]+d) - vc.begin() - 1;
		ans += ((l-i) * (l-i-1) *1ll) / 2ll;
	}
	cout << ans << "\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define maxx 100100
typedef long long ll;
int main() {
	vector<int>vc;
	int n,d; cin >> n >> d;
	for(int i = 0; i < n; i++){
		int a; cin >> a; vc.push_back(a);
	}
	ll ans = 0ll;
	for(int i = 0; i < n; i++){
		int l = upper_bound(vc.begin(),vc.end(),vc[i]+d) - vc.begin() - 1;
		ans += ((l-i) * (l-i-1) *1ll) / 2ll;
	}
	cout << ans << "\n";
	return 0;
}


#include <bits/stdc++.h>
#include <stdint.h>
#define int long long int
#define pb push_back
#define maxv *max_element
#define des greater<int>()
#define mp make_pair
#define mod 1000000007
using namespace std;

int sum(int n){
	return (n*(n+1))/2;
}

int32_t main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n,d;
	cin>>n>>d;
	int a[n];
	for(int i=0;i<n;i++)
		cin>>a[i];
	int f=0,s=2;
	int k=0;
	if(n>=3){
	do{
		//cout<<f<<" "<<s<<" "<<k<<endl;
		if(a[s+1]-a[f]<=d&&s<n-1){
			//k=k+sum(s-f-1);
			s++;
		}
		else if(a[s+1]-a[f]>d) {
			if(a[s]-a[f]<=d)
				k=k+sum(s-f-1);
			f++;
			if(s-f<2)
				s++;
		}
		else {
			if(f<n-3&&s==n-1){

				k=k+sum(s-f-1);
				f++;
			}

		}
		if(s>n-1)
			s=n-1;

	}while(s!=n-1||f!=n-3);
	if(a[n-1]-a[n-3]<=d)
		k=k+1;}
	cout<<k<<endl;
}


#include <bits/stdc++.h>
#include <stdint.h>
#define int long long int
#define pb push_back
#define maxv *max_element
#define des greater<int>()
#define mp make_pair
#define mod 1000000007
using namespace std;

int32_t main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n,d;
	cin>>n>>d;
	int a[n];
	for(int i=0;i<n;i++)
		cin>>a[i];
	int f=0,s=2;
	int k=0;
	do{
		//cout<<f<<" "<<s<<" "<<k<<endl;
		if(a[s]-a[f]<=d){
			k=k+s-f-1;
			s++;
			if(s>n-1){
				f++;
			}
		}
		else{
			f++;
			if(s-f<2)
				s++;
		}
		if(f>n-3)
			f=n-3;
		if(s>n-1)
			s=n-1;

	}while(f!=n-3||s!=n-1);
	if(a[s]-a[f]<=d){
		k=k+s-f-1;
	}
	cout<<k<<endl;
}


#include <bits/stdc++.h>
#include <stdint.h>
#define int long long int
#define pb push_back
#define maxv *max_element
#define des greater<int>()
#define mp make_pair
#define mod 1000000007
using namespace std;

int sum(int n){
	return (n*(n+1))/2;
}

int32_t main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n,d;
	cin>>n>>d;
	int a[n];
	for(int i=0;i<n;i++)
		cin>>a[i];
	int f=0,s=2;
	int k=0;
	if(n>3){
	do{
		//cout<<f<<" "<<s<<" "<<k<<endl;
		if(a[s+1]-a[f]<=d&&s<n-1){
			//k=k+sum(s-f-1);
			s++;
		}
		else if(a[s+1]-a[f]>d) {
			if(a[s]-a[f]<=d)
				k=k+sum(s-f-1);
			f++;
			if(s-f<2)
				s++;
		}
		else {
			if(f<n-3&&s==n-1){

				k=k+sum(s-f-1);
				f++;
			}

		}
		if(s>n-1)
			s=n-1;

	}while(s!=n-1||f!=n-3);
	if(a[n-1]-a[n-3]<=d)
		k=k+1;}
	if(n==3){
		if(a[2]-a[0]<=d)
			k=1;
	}
	cout<<k<<endl;
}


#include <bits/stdc++.h>
#include <stdint.h>
#define int long long int
#define pb push_back
#define maxv *max_element
#define des greater<int>()
#define mp make_pair
#define mod 1000000007
using namespace std;

int sum(int n){
	return (n*(n+1))/2;
}

int32_t main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n,d;
	cin>>n>>d;
	int a[n];
	for(int i=0;i<n;i++)
		cin>>a[i];
	int f=0,s=2;
	int k=0;
	if(n>=3){
	do{
		//cout<<f<<" "<<s<<" "<<k<<endl;
		if(a[s+1]-a[f]<=d&&s<n-1){
			//k=k+sum(s-f-1);
			s++;
		}
		else if(a[s+1]-a[f]>d) {
			if(a[s]-a[f]<=d)
				k=k+sum(s-f-1);
			f++;
			if(s-f<2)
				s++;
		}
		else {
			if(f<n-3&&s==n-1){

				k=k+sum(s-f-1);
				f++;
			}

		}
		if(s>n-1)
			s=n-1;

	}while(s!=n-1||f!=n-3);}
	s=n-1;
	f=f+1;
	if(a[n-1]-a[n-3]<=d)
		k=k+1;
	cout<<k<<endl;
}


#include <bits/stdc++.h>
#include <stdint.h>
#define int long long int
#define pb push_back
#define maxv *max_element
#define des greater<int>()
#define mp make_pair
#define mod 1000000007
using namespace std;

int sum(int n){
	return (n*(n+1))/2;
}

int32_t main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n,d;
	cin>>n>>d;
	int a[n];
	for(int i=0;i<n;i++)
		cin>>a[i];
	int f=0,s=2;
	int k=0;
	do{
		//cout<<f<<" "<<s<<" "<<k<<endl;
		if(a[s+1]-a[f]<=d&&s<n-1){
			//k=k+sum(s-f-1);
			s++;
		}
		else if(a[s+1]-a[f]>d) {
			if(a[s]-a[f]<=d)
				k=k+sum(s-f-1);
			f++;
			if(s-f<2)
				s++;
		}
		else {
			if(f<n-3&&s==n-1){

				k=k+sum(s-f-1);
				f++;
			}

		}
		if(s>n-1)
			s=n-1;

	}while(s!=n-1||f!=n-3);
	s=n-1;
	f=f+1;
	if(a[n-1]-a[n-3]<=d)
		k=k+1;
	cout<<k<<endl;
}


#include <bits/stdc++.h>
#include <stdint.h>
#define int long long int
#define pb push_back
#define maxv *max_element
#define des greater<int>()
#define mp make_pair
#define mod 1000000007
using namespace std;

int sum(int n){
	return (n*(n+1))/2;
}

int32_t main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n,d;
	cin>>n>>d;
	int a[n];
	for(int i=0;i<n;i++)
		cin>>a[i];
	int f=0,s=2;
	int k=0;
	if(n>3){
	do{
		//cout<<f<<" "<<s<<" "<<k<<endl;
		if(a[s+1]-a[f]<=d&&s<n-1){
			//k=k+sum(s-f-1);
			s++;
		}
		else if(a[s+1]-a[f]>d) {
			if(a[s]-a[f]<=d)
				k=k+sum(s-f-1);
			f++;
			if(s-f<2)
				s++;
		}
		else {
			if(f<n-3&&s==n-1){

				k=k+sum(s-f-1);
				f++;
			}

		}
		if(s>n-1)
			s=n-1;

	}while(s!=n-1||f!=n-3);
	if(a[n-1]-a[n-3]<=d)
		k=k+1;}
	if(n==3){
		if(a[2]-a[0]>=d)
			k=1;
	}
	cout<<k<<endl;
}


#include <iostream>
#include <algorithm>
#include <map>
#include <cmath>
#include <cassert>
#include <vector>
#include <set>
#include <cstdio>
using namespace std;
/**

const int N = 1e6 + 5;
int n, x, vis[N], D[N];
int main() {
    freopen("TEST.INP", "r", stdin);
	cin >> n;
	for (int i = 1; i <= n; ++ i) {
        cin >> x;
        vis[x] = 1;
	}
	int ans = 0;
	for (int i = 1; i <= N; i ++)
        if (vis[i] == 1) {
            ++ D[i];
            ans = max(ans, D[i]);
            for (int j = i; j <= N; j += i)
                D[j] = max(D[j], D[i]);
        }
	cout << ans << endl;
}
*/
const int N = 1e6 + 5;

int n, t, vis[N], D[N];
int main() {
   // assert(freopen("TEST.INP", "r", stdin));
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i ++) {
        cin >> t;
        vis[t] = 1;
    }
    int ans = 0;
    for (int i = 1; i <= N - 1; i ++)
        if (vis[i] == 1) {
            ++ D[i];
            ans = max(ans, D[i]);
            for (int j = i; j <= N - 1; j += i)
                D[j] = max(D[j], D[i]);
        }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#include <cmath>
#include <cassert>
#include <vector>
#include <set>
#include <cstdio>
using namespace std;
/**

const int N = 1e6 + 5;
int n, x, vis[N], D[N];
int main() {
    freopen("TEST.INP", "r", stdin);
	cin >> n;
	for (int i = 1; i <= n; ++ i) {
        cin >> x;
        vis[x] = 1;
	}
	int ans = 0;
	for (int i = 1; i <= N; i ++)
        if (vis[i] == 1) {
            ++ D[i];
            ans = max(ans, D[i]);
            for (int j = i; j <= N; j += i)
                D[j] = max(D[j], D[i]);
        }
	cout << ans << endl;
}
*/
const int N = 1e6;

int n, x, vis[N], D[N];
int main() {
    //assert(freopen("TEST.INP", "r", stdin));
    cin >> n;
    for (int i = 1; i <= n; i ++) {
        cin >> x;
        vis[x] = 1;
    }
    int ans = 0;
    for (int i = 1; i <= N; i ++)
        if (vis[i] == 1) {
            ++ D[i];
            ans = max(ans, D[i]);
            for (int j = i; j <= N; j += i)
                D[j] = max(D[j], D[i]);
        }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#include <cmath>
#include <cassert>
#include <vector>
#include <set>
#include <cstdio>
using namespace std;
/**

const int N = 1e6 + 5;
int n, x, vis[N], D[N];
int main() {
    freopen("TEST.INP", "r", stdin);
	cin >> n;
	for (int i = 1; i <= n; ++ i) {
        cin >> x;
        vis[x] = 1;
	}
	int ans = 0;
	for (int i = 1; i <= N; i ++)
        if (vis[i] == 1) {
            ++ D[i];
            ans = max(ans, D[i]);
            for (int j = i; j <= N; j += i)
                D[j] = max(D[j], D[i]);
        }
	cout << ans << endl;
}
*/
const int N = 1e6 + 5;

int n, x, vis[N], D[N];
int main() {
    //assert(freopen("TEST.INP", "r", stdin));
    scanf("%d", &n);
    for (int i = 1; i <= n; i ++) {
        scanf("%d", &x);
        vis[x] = 1;
    }
    int ans = 0;
    for (int i = 1; i <= N; i ++)
        if (vis[i] == 1) {
            ++ D[i];
            ans = max(ans, D[i]);
            for (int j = i; j <= N; j += i)
                D[j] = max(D[j], D[i]);
        }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#include <cmath>
#include <cassert>
#include <vector>
#include <set>
#include <cstdio>
using namespace std;
/**

const int N = 1e6 + 5;
int n, x, vis[N], D[N];
int main() {
    freopen("TEST.INP", "r", stdin);
	cin >> n;
	for (int i = 1; i <= n; ++ i) {
        cin >> x;
        vis[x] = 1;
	}
	int ans = 0;
	for (int i = 1; i <= N; i ++)
        if (vis[i] == 1) {
            ++ D[i];
            ans = max(ans, D[i]);
            for (int j = i; j <= N; j += i)
                D[j] = max(D[j], D[i]);
        }
	cout << ans << endl;
}
*/
const int N = 1e6 + 5;

int n, x, vis[N], D[N];
int main() {
  //  assert(freopen("TEST.INP", "r", stdin));
    ios_base::sync_with_stdio(false);
    cin >> n;
    if (n == 1) {puts("1"); return 0;}
    for (int i = 1; i <= n; i ++) {
        cin >> x;
        vis[x] = 1;
    }
    int ans = 0;
    for (int i = 1; i <= N; i ++)
        if (vis[i] == 1) {
            ++ D[i];
            ans = max(ans, D[i]);
            for (int j = i; j <= N; j += i)
                D[j] = max(D[j], D[i]);
        }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cassert>
#include <vector>
#include <set>
#include <cstdio>
using namespace std;

const int INF = 1e6;
const int N   = 1e6 + 5;

set<int> s;
int n, a[N];
int main() {
    //assert(freopen("TEST.INP", "r", stdin));
    cin >> n;
    bool hasI = false;
    for (int i = 1; i <= n; i ++) {
        cin >> a[i];
        if (a[i] == 1) hasI = true;
        s.insert(a[i]);
    }
    int ans = 0, id = 1;
    while (id <= n) {
        int co = 1;
        int tmp = a[id] * co;
        while (s.count(tmp)) {
            if (co * a[id] > INF) break;
            ++ co;
            tmp *= co;
        }
        ans = max(ans, co - 1);
        ++ id;
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#include <cmath>
#include <cassert>
#include <vector>
#include <set>
#include <cstdio>
using namespace std;
/**

const int N = 1e6 + 5;
int n, x, vis[N], D[N];
int main() {
    freopen("TEST.INP", "r", stdin);
	cin >> n;
	for (int i = 1; i <= n; ++ i) {
        cin >> x;
        vis[x] = 1;
	}
	int ans = 0;
	for (int i = 1; i <= N; i ++)
        if (vis[i] == 1) {
            ++ D[i];
            ans = max(ans, D[i]);
            for (int j = i; j <= N; j += i)
                D[j] = max(D[j], D[i]);
        }
	cout << ans << endl;
}
*/
const int N = 1e6 + 5;

int n, x, vis[N], D[N];
int main() {
    //assert(freopen("TEST.INP", "r", stdin));
    ios_base::sync_with_stdio(false);
    cin >> n;
    for (int i = 1; i <= n; i ++) {
        cin >> x;
        vis[x] = 1;
    }
    int ans = 0;
    for (int i = 1; i <= N; i ++)
        if (vis[i] == 1) {
            ++ D[i];
            ans = max(ans, D[i]);
            for (int j = i; j <= N; j += i)
                D[j] = max(D[j], D[i]);
        }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
bool arr[1000001];
int main()
{
	 ios_base::sync_with_stdio(false);
	memset(arr,false,sizeof(arr));
	int n,a;
	cin>>n;
	ll ans=0;
	ll cnt;
	int last;
	for(int i=0;i<n;i++)
	{
		cin>>a;
		arr[a]=true;
	}
	for(int i=2;i*i<=1000000;i++)
	{
		cnt=0;
		last=1;
		for(int j=1;j*i<=1000000;j++)
		{
			if(arr[j*i] && (j*i)%last==0)
			{
				cnt++;
				last=j*i;
			}
		}

		ans=max(ans,cnt);
	}
	if(arr[1])
	{
		ans=ans+1;
	}
	cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
int arr[1000001];
int main()
{
     ios_base::sync_with_stdio(false);
    memset(arr,0,sizeof(arr));
    int n,a;
    cin>>n;
    int ans=0;
    ll cnt;
    int last;
    for(int i=0;i<n;i++)
    {
        cin>>a;
        arr[a]=1;
    }
    for(int i=1;i<=1000000;i++)
    {
        cnt=0;
        //last=1;
        if(arr[i]==0)
        {
            continue;
        }
        for(int j=2;j*i<=1000000;j++)
        {
            if(arr[j*i])
            {
                //cnt++;
                arr[j*i]=max(arr[i]+1,arr[j*i]);
            }
        }

        ans=max(ans,arr[i]);
    }
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
bool arr[1000001];
int main()
{
	memset(arr,false,sizeof(arr));
	int n,a;
	cin>>n;
	ll ans=0;
	ll cnt;
	int last;
	for(int i=0;i<n;i++)
	{
		cin>>a;
		arr[a]=true;
	}
	for(int i=2;i*i<=1000000;i++)
	{
		cnt=0;
		last=1;
		for(int j=1;j*i<=1000000;j++)
		{
			if(arr[j*i] && (j*i)%last==0)
			{
				cnt++;
				last=j*i;
			}
		}

		ans=max(ans,cnt);
	}
	cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
bool arr[1000001];
int main()
{
     ios_base::sync_with_stdio(false);
    memset(arr,false,sizeof(arr));
    int n,a;
    cin>>n;
    ll ans=0;
    ll cnt;
    int last;
    for(int i=0;i<n;i++)
    {
        cin>>a;
        arr[a]=true;
    }
    for(int i=2;i*i<=1000000;i++)
    {
        cnt=0;
        last=1;
        for(int j=1;j*i<=1000000;j++)
        {
            if(arr[j*i] && (j*i)%last==0)
            {
                cnt++;
                last=j*i;
            }
        }

        ans=max(ans,cnt);
    }
    cout<<ans<<endl;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#define maxn 1000007
using namespace std;
int N[maxn],DP[maxn];
int n;
int main(void)
{
	//freopen("566F.txt","r",stdin);
	while(~scanf("%d",&n)){
		memset(N,0,sizeof(N));
		memset(DP,0,sizeof(DP));
		for(int i=0;i<n;++i){
			int x;scanf("%d",&x);
			DP[x]++;N[x]++;
		}
		for(int i=1;i<=100000;++i){
			for(int j=i<<1;j<=100000;j+=i){
				DP[j]=max(DP[j],DP[i]+N[j]);
			}
		}
		int ANS=0;
		for(int i=1;i<=100000;++i) ANS=max(ANS,DP[i]);
		printf("%d\n",ANS);
	}
return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#define maxn 100007
using namespace std;
int N[maxn],DP[maxn];
int n;
int main(void)
{
	//freopen("566F.txt","r",stdin);
	while(~scanf("%d",&n)){
		memset(N,0,sizeof(N));
		memset(DP,0,sizeof(DP));
		for(int i=0;i<n;++i){
			int x;scanf("%d",&x);
			DP[x]++;N[x]++;
		}
		for(int i=1;i<=100000;++i){
			for(int j=i<<1;j<=100000;j+=i){
				DP[j]=max(DP[j],DP[i]+N[j]);
			}
		}
		int ANS=0;
		for(int i=1;i<=100000;++i) ANS=max(ANS,DP[i]);
		printf("%d\n");
	}
return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#define maxn 100007
using namespace std;
int N[maxn],DP[maxn];
int n;
int main(void)
{
	//freopen("566F.txt","r",stdin);
	while(~scanf("%d",&n)){
		memset(N,0,sizeof(N));
		memset(DP,0,sizeof(DP));
		for(int i=0;i<n;++i){
			int x;scanf("%d",&x);
			DP[x]++;N[x]++;
		}
		for(int i=1;i<=100000;++i){
			for(int j=i<<1;j<=100000;j+=i){
				DP[j]=max(DP[j],DP[i]+N[j]);
			}
		}
		int ANS=0;
		for(int i=1;i<=100000;++i) ANS=max(ANS,DP[i]);
		printf("%d\n",ANS);
	}
return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#define maxn 1000007
using namespace std;
int N[maxn],DP[maxn];
int n;
int main(void)
{
	//freopen("566F.txt","r",stdin);
	while(~scanf("%d",&n)){
		memset(N,0,sizeof(N));
		memset(DP,0,sizeof(DP));
		for(int i=0;i<n;++i){
			int x;scanf("%d",&x);
			DP[x]++;N[x]++;
		}
		for(int i=1;i<=1000000;++i){
			for(int j=i<<1;j<=1000000;j+=i){
				DP[j]=max(DP[j],DP[i]+N[j]);
			}
		}
		int ANS=0;
		for(int i=1;i<=1000000;++i) ANS=max(ANS,DP[i]);
		printf("%d\n",ANS);
	}
return 0;
}


#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <deque>
#include <vector>
#include <map>
#include <cmath>
#include <cstdlib>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <cstring>
#include <climits>
#include <cctype>
#include <utility>
using namespace std;

#define ft first
#define sd second
#define pb push_back
#define endl '\n'
#define cpy(a,e) memcpy(a,e,sizeof(e))
#define clr(a,e) memset(a,e,sizeof(a))
#define rep(i,n) for (int (i)=0;(i)<(n);i++)
#define repd(i,n) for (int (i)=(n)-1;(i)>=0;i--)
#define reps(i,s,e) for (int (i)=(s);(i)<=(e);i++)
#define repds(i,s,e) for (int (i)=(s);(i)>=(e);i--)

int n,dp[1000055],a[1000055];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    int res=0;
    rep(i,n) {
        cin >> a[i];
        int l=(int)sqrt(a[i]);
        reps(j,1,l) {
            dp[a[i]]=max(dp[a[i]],dp[a[i]/j]+1);
            res=max(dp[a[i]],res);
        }
    }
    cout << res << endl;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <deque>
#include <vector>
#include <map>
#include <cmath>
#include <cstdlib>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <cstring>
#include <climits>
#include <cctype>
#include <utility>
using namespace std;

#define ft first
#define sd second
#define pb push_back
#define endl '\n'
#define cpy(a,e) memcpy(a,e,sizeof(e))
#define clr(a,e) memset(a,e,sizeof(a))
#define rep(i,n) for (int (i)=0;(i)<(n);i++)
#define repd(i,n) for (int (i)=(n)-1;(i)>=0;i--)
#define reps(i,s,e) for (int (i)=(s);(i)<=(e);i++)
#define repds(i,s,e) for (int (i)=(s);(i)>=(e);i--)

int n,dp[1000055],a[1000055];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    int res=0;
    rep(i,n) {
        cin >> a[i];
        int l=(int)ceil(sqrt(a[i]));
        reps(j,1,l) {
            if (a[i]%j!=0) continue;
            dp[a[i]]=max(dp[a[i]],dp[a[i]/j]+1);
            res=max(dp[a[i]],res);
        }
    }
    cout << res << endl;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <deque>
#include <vector>
#include <map>
#include <cmath>
#include <cstdlib>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <cstring>
#include <climits>
#include <cctype>
#include <utility>
using namespace std;

#define ft first
#define sd second
#define pb push_back
#define endl '\n'
#define cpy(a,e) memcpy(a,e,sizeof(e))
#define clr(a,e) memset(a,e,sizeof(a))
#define rep(i,n) for (int (i)=0;(i)<(n);i++)
#define repd(i,n) for (int (i)=(n)-1;(i)>=0;i--)
#define reps(i,s,e) for (int (i)=(s);(i)<=(e);i++)
#define repds(i,s,e) for (int (i)=(s);(i)>=(e);i--)

int n,dp[1000055],a[1000055];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    rep(i,n) {
        cin >> a[i];
        rep(j,i) {
            if (a[i]%a[j]==0||a[j]%a[i]==0) dp[i]=max(dp[i],dp[j]+1);
        }
    }
    cout << dp[n-1]+1 << endl;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <deque>
#include <vector>
#include <map>
#include <cmath>
#include <cstdlib>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <cstring>
#include <climits>
#include <cctype>
#include <utility>
using namespace std;

#define ft first
#define sd second
#define pb push_back
#define endl '\n'
#define cpy(a,e) memcpy(a,e,sizeof(e))
#define clr(a,e) memset(a,e,sizeof(a))
#define rep(i,n) for (int (i)=0;(i)<(n);i++)
#define repd(i,n) for (int (i)=(n)-1;(i)>=0;i--)
#define reps(i,s,e) for (int (i)=(s);(i)<=(e);i++)
#define repds(i,s,e) for (int (i)=(s);(i)>=(e);i--)

int n,dp[1000055],a[1000055];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    int res=0;
    rep(i,n) {
        cin >> a[i];
        int l=(int)ceil(sqrt(a[i]));
        reps(j,1,l) {
            dp[a[i]]=max(dp[a[i]],dp[a[i]/j]+1);
            res=max(dp[a[i]],res);
        }
    }
    cout << res << endl;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <deque>
#include <vector>
#include <map>
#include <cmath>
#include <cstdlib>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <cstring>
#include <climits>
#include <cctype>
#include <utility>
using namespace std;

#define ft first
#define sd second
#define pb push_back
#define endl '\n'
#define cpy(a,e) memcpy(a,e,sizeof(e))
#define clr(a,e) memset(a,e,sizeof(a))
#define rep(i,n) for (int (i)=0;(i)<(n);i++)
#define repd(i,n) for (int (i)=(n)-1;(i)>=0;i--)
#define reps(i,s,e) for (int (i)=(s);(i)<=(e);i++)
#define repds(i,s,e) for (int (i)=(s);(i)>=(e);i--)

int n,dp[1000055],a[1000055];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    int res=0;
    rep(i,n) {
        cin >> a[i];
        int l=1000000/a[i];
        reps(j,2,l) {
            dp[a[i]*j]=max(dp[a[i]*j],dp[a[i]]+1);
        }
        res=max(dp[a[i]],res);
    }
    cout << res+1 << endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int maxn=1000*1000+1000;
vector<int>d[maxn];
int ans[maxn];
int a[maxn];

int main()
{
    for(int i=1;i<=1000*1000;i++)
        for(int j=2;j*i<=1000*1000;j++)
            d[i*j].push_back(i);
    int answer=0;
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i];
        ans[a[i]]++;
    }
    sort(a,a+n);
    for(int i=0;i<n;i++){
        int tmp=0;
        for(int j=0;j<d[a[i]].size();j++)
            if(ans[d[a[i]][j]]>tmp)
                tmp=ans[d[a[i]][j]];
        ans[a[i]]+=tmp;
    }
    for(int i=0;i<n;i++)
        if(ans[a[i]]>answer)
            answer=ans[a[i]];
    cout<<answer;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int maxn=1000*1000+1000;
vector<int>d[maxn];
int ans[maxn];
int a[maxn];

int main()
{
    int answer=0;
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i];
        ans[a[i]]++;
    }
    for(int i=1;i<=1000*1000;i++)
        for(int j=2;j*i<=1000*1000;j++)
            d[i*j].push_back(i);
    sort(a,a+n);
    for(int i=0;i<n;i++){
        int tmp=0;
        for(int j=0;j<d[a[i]].size();j++)
            if(ans[d[a[i]][j]]>tmp)
                tmp=ans[d[a[i]][j]];
        ans[a[i]]+=tmp;
    }
    for(int i=0;i<n;i++)
        if(ans[a[i]]>answer)
            answer=ans[a[i]];
    cout<<answer;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int maxn=1000*1000+1000;
int ans[maxn];
int a[maxn];

int main()
{
    int answer=0;
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i];
        ans[a[i]]++;
    }
    sort(a,a+n);
    for(int i=n-1;i>=0;i--){
        int tmp=0;
        for(int j=2;j*a[i]<=1000*1000;j++)
            tmp=max(tmp,ans[j*a[i]]);
        ans[a[i]]+=tmp;
    }
    for(int i=0;i<n;i++)
        answer=max(answer,ans[a[i]]);
    cout<<answer;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>

using namespace std;

const int maxn=1000*1000+1000;
vector<int>d[maxn];
int ans[maxn];
int a[maxn];

int main()
{
    for(int i=1;i<=100000;i++)
        for(int j=2;j*i<=100000;j++)
            d[i*j].push_back(i);
    int answer=0;
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
        ans[a[i]]++;
    }
    sort(a,a+n);
    for(int i=0;i<n;i++){
        int tmp=0;
        for(int j=0;j<d[a[i]].size();j++)
            tmp=max(tmp,ans[d[a[i]][j]]);
        ans[a[i]]+=tmp;
    }
    for(int i=0;i<n;i++)
        answer=max(answer,ans[a[i]]);
    printf("%d",answer);
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int maxn=1000*1000+1000;
vector<int>d[maxn];
int ans[maxn];
int a[maxn];

int main()
{
    for(int i=1;i<=1000*1000;i++)
        for(int j=2;j*i<=1000*1000;j++)
            d[i*j].push_back(i);
    int answer=0;
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i];
        ans[a[i]]++;
    }
    sort(a,a+n);
    for(int i=0;i<n;i++){
        int tmp=0;
        for(int j=0;j<d[a[i]].size();j++)
            tmp=max(tmp,ans[d[a[i]][j]]);
        ans[a[i]]+=tmp;
    }
    for(int i=0;i<n;i++)
        answer=max(answer,ans[a[i]]);
    cout<<answer;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>

using namespace std;

const int maxn=1000*1000+1000;
vector<int>d[maxn];
int ans[maxn];
int a[maxn];

int main()
{
    int answer=0;
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
        ans[a[i]]++;
    }
    for(int i=1;i<=1000*1000;i++)
        for(int j=2;j*i<=1000*1000;j++)
            d[i*j].push_back(i);
    sort(a,a+n);
    for(int i=0;i<n;i++){
        int tmp=0;
        for(int j=0;j<d[a[i]].size();j++)
            tmp=max(tmp,ans[d[a[i]][j]]);
        ans[a[i]]+=tmp;
    }
    for(int i=0;i<n;i++)
        answer=max(answer,ans[a[i]]);
    printf("%d",answer);
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>

using namespace std;

const int maxn=1000*1000+1000;
vector<int>d[maxn];
int ans[maxn];
int a[maxn];

int main()
{
    for(int i=1;i<=1000*1000;i++)
        for(int j=2;j*i<=1000*1000;j++)
            d[i*j].push_back(i);
    int answer=0;
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
        ans[a[i]]++;
    }
    sort(a,a+n);
    for(int i=0;i<n;i++){
        int tmp=0;
        for(int j=0;j<d[a[i]].size();j++)
            tmp=max(tmp,ans[d[a[i]][j]]);
        ans[a[i]]+=tmp;
    }
    for(int i=0;i<n;i++)
        answer=max(answer,ans[a[i]]);
    cout<<answer;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>

using namespace std;

const int maxn=1000*1000+1000;
vector<int>d[maxn];
int ans[maxn];
int a[maxn];

int main()
{
    for(int i=1;i<=1000;i++)
        for(int j=2;j*i<=1000;j++)
            d[i*j].push_back(i);
    int answer=0;
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
        ans[a[i]]++;
    }
    sort(a,a+n);
    for(int i=0;i<n;i++){
        int tmp=0;
        for(int j=0;j<d[a[i]].size();j++)
            tmp=max(tmp,ans[d[a[i]][j]]);
        ans[a[i]]+=tmp;
    }
    for(int i=0;i<n;i++)
        answer=max(answer,ans[a[i]]);
    printf("%d",answer);
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>

using namespace std;

const int maxn=1000*1000+1000;
vector<int>d[maxn];
int ans[maxn];
int a[maxn];

int main()
{
    for(int i=1;i<=500001;i++)
        for(int j=2;j*i<=1000000;j++)
            d[i*j].push_back(i);
    int answer=0;
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
        ans[a[i]]++;
    }
    sort(a,a+n);
    for(int i=0;i<n;i++){
        int tmp=0;
        for(int j=0;j<d[a[i]].size();j++)
            tmp=max(tmp,ans[d[a[i]][j]]);
        ans[a[i]]+=tmp;
    }
    for(int i=0;i<n;i++)
        answer=max(answer,ans[a[i]]);
    printf("%d",answer);
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>

using namespace std;

const int maxn=1000*1000+1000;
vector<int>d[maxn];
int ans[maxn];
int a[maxn];

int main()
{
    for(int i=1;i<=1000*1000;i++)
        for(int j=2;j*i<=1000*1000;j++)
            d[i*j].push_back(i);
    int answer=0;
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
        ans[a[i]]++;
    }
    sort(a,a+n);
    for(int i=0;i<n;i++){
        int tmp=0;
        for(int j=0;j<d[a[i]].size();j++)
            tmp=max(tmp,ans[d[a[i]][j]]);
        ans[a[i]]+=tmp;
    }
    for(int i=0;i<n;i++)
        answer=max(answer,ans[a[i]]);
    printf("%d",answer);
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>

using namespace std;

const int maxn=1000*1000+1000;
int ans[maxn];
int a[maxn];

int main()
{
    int answer=0;
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
        ans[a[i]]++;
    }
    sort(a,a+n);
    for(int i=n-2;i>=0;i--){
        int tmp=0;
        for(int j=2;j*a[i]<1000*1000;j++)
            tmp=max(tmp,ans[j*a[i]]);
        ans[a[i]]+=tmp;
    }
    for(int i=0;i<n;i++)
        answer=max(answer,ans[a[i]]);
    printf("%d",answer);
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>

using namespace std;

const int maxn=1000*1000+1000;
int ans[maxn];
int a[maxn];

int main()
{
    int answer=0;
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
        ans[a[i]]++;
    }
    sort(a,a+n);
    for(int i=n-1;i>=0;i--){
        int tmp=0;
        for(int j=2;j*a[i]<=1000*1000;j++)
            tmp=max(tmp,ans[j*a[i]]);
        ans[a[i]]+=tmp;
    }
    for(int i=0;i<n;i++)
        answer=max(answer,ans[a[i]]);
    printf("%d",answer);
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>

using namespace std;

const int maxn=1000*1000+1000;
vector<int>d[maxn];
int ans[maxn];
int a[maxn];

int main()
{
    int answer=0;
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
        ans[a[i]]++;
    }
    sort(a,a+n);
    for(int i=0;i<n;i++)
        for(int j=2;j*a[i]<=1000*1000;j++)
            d[a[i]*j].push_back(a[i]);
    for(int i=0;i<n;i++){
        int tmp=0;
        for(int j=0;j<d[a[i]].size();j++)
            tmp=max(tmp,ans[d[a[i]][j]]);
        ans[a[i]]+=tmp;
    }
    for(int i=0;i<n;i++)
        answer=max(answer,ans[a[i]]);
    printf("%d",answer);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int n, a[1000005], ans[1000005], res = 1;

int main(int argc, char **argv) {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
#endif
	scanf("%d", &n);
	for (int i = 0; i < n; ++i)
		scanf("%d", a + i), ans[a[i]] = 1;
	for (int i = 0; i < n; ++i) {
		for (int j = 2 * a[i];; j += a[i]) {
			if (j > a[n - 1]) break;
			ans[j] = max(ans[j], ans[a[i]] + 1);
		}
	}
	for (int i = 0; i < n; ++i) res = max(res, ans[a[i]]);
	cout << res << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int n, a[1000005], ans[1000005], res = 1;

int main(int argc, char **argv) {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
#endif
	scanf("%d", &n);
	for (int i = 0; i < n; ++i)
		scanf("%d", a + i), ans[a[i]] = 1;
	for (int i = 0; i < n; ++i) {
		for (int j = 2;; ++j) {
			int tmp = a[i] * j;
			if (tmp > a[n - 1]) break;
			ans[tmp] = max(ans[tmp], ans[a[i]] + 1);
		}
	}
	for(int i=0 ; i<n ; ++i) res = max(res, ans[a[i]]);
	cout << res << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int n, a[1000005];
bool found[1000005];
int ans[1000005];
int res;

int main(int argc, char **argv) {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
#endif
	scanf("%d", &n);
	for (int i = 0; i < n; ++i)
		scanf("%d", a + i), found[a[i]] = 1;
	for (int i = 0; i <= a[n - 1]; ++i) ans[i] = 1;
	for (int i = 0; i < n; ++i) {
		for (int j = 2;; ++j) {
			int tmp = a[i] * j;
			if (tmp > a[n - 1]) break;
			if (found[tmp]) {
				ans[tmp] = max(ans[tmp], ans[a[i]] + 1);
				res = max(res, ans[tmp]);
			}
		}
	}
	cout << res << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int n, a[1000005], ans[1000005], res = 1;

int main(int argc, char **argv) {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
#endif
	scanf("%d", &n);
	for (int i = 0; i < n; ++i)
		scanf("%d", a + i);
	for (int i = 0; i <= a[n - 1]; ++i) ans[i] = 1;
	for (int i = 0; i < n; ++i) {
		for (int j = 2;; ++j) {
			int tmp = a[i] * j;
			if (tmp > a[n - 1]) break;
			ans[tmp] = max(ans[tmp], ans[a[i]] + 1);
		}
	}
	for(int i=0 ; i<n ; ++i) res = max(res, ans[a[i]]);
	cout << res << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int n, a[1000005];
bool found[1000005];
int ans[1000005];
int res = 1;

int main(int argc, char **argv) {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
#endif
	scanf("%d", &n);
	for (int i = 0; i < n; ++i)
		scanf("%d", a + i), found[a[i]] = 1;
	for (int i = 0; i <= a[n - 1]; ++i) ans[i] = 1;
	for (int i = 0; i < n; ++i) {
		for (int j = 2;; ++j) {
			int tmp = a[i] * j;
			if (tmp > a[n - 1]) break;
			if (found[tmp]) {
				ans[tmp] = max(ans[tmp], ans[a[i]] + 1);
				res = max(res, ans[tmp]);
			}
		}
	}
	cout << res << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int n, a[1000005], ans[1000005], res = 1;

int main(int argc, char **argv) {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
#endif
	scanf("%d", &n);
	for (int i = 0; i < n; ++i)
		scanf("%d", a + i);
	for (int i = 0; i <= a[n - 1]; ++i) ans[i] = 1;
	for (int i = 0; i < n; ++i) {
		for (int j = 2;; ++j) {
			int tmp = a[i] * j;
			if (tmp > a[n - 1]) break;
			ans[tmp] = max(ans[tmp], ans[a[i]] + 1);
			res = max(res, ans[tmp]);
		}
	}
	cout << res << endl;
	return 0;
}


#include<bits/stdc++.h>
#define finc(i,a,b) for (int i = a; i <= b; ++i)
#define fdec(i,a,b) for (int i = a; i >= b; --i)
#define ms(a, i) memset(a, i, sizeof(a))
#define sqr(a) (long long)(a) * (a)
#define X first
#define Y second
#define ii pair<int, int>
#define db(x) cerr << #x << " = " << x << endl;
#define eb(x, y) emplace_back(x, y)
#define ll long long
#define pb(x) push_back(x)
#define endl '\n'
#define sd(x) scanf("%d", &x)
#define mx(l, r) *max_element(l, r);

using namespace std;

const int oo = 1e9 + 7;
const int maxn = 1e6 + 7;
int n, a[maxn], f[maxn];

void nhap() {
	cin >> n;
	finc(i, 1, n)
		cin >> a[i];
}

void solve(){
	finc(i, 1, n){
		finc(j, 1, trunc(sqrt(a[i])))
			if (a[i] % j == 0)
				f[a[i]] = max(max(f[a[i]], f[j] + 1), f[a[i] / j] + 1);
	}
}

void print(){
	cout << mx(f + 1, f + maxn + 1);
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	//freopen("", "r", stdin);
	//freopen("", "w", stdout);
	nhap();
	solve();
	print();
}


#include<bits/stdc++.h>
#define finc(i,a,b) for (int i = a; i <= b; ++i)
#define fdec(i,a,b) for (int i = a; i >= b; --i)
#define ms(a, i) memset(a, i, sizeof(a))
#define sqr(a) (long long)(a) * (a)
#define X first
#define Y second
#define ii pair<int, int>
#define db(x) cerr << #x << " = " << x << endl;
#define eb(x, y) emplace_back(x, y)
#define ll long long
#define pb(x) push_back(x)
#define endl '\n'
#define sd(x) scanf("%d", &x)
#define mx(l, r) *max_element(l, r)

using namespace std;

const int oo = 1e9 + 7;
const int maxn = 1e6 + 7;
int n, x, f[maxn];

void nhap() {
	sd(n);
	finc(i, 1, n){
		sd(x);
		f[x]++;
	}
}

void solve(){
	finc(i, 1, maxn - 7){
		finc(j, 2, maxn / i)
			if (f[i * j])
				f[i * j] = max(f[i * j], f[i] + 1);
	}
}

void print(){
	int res = 0;
	finc(i, 0, maxn - 1)
		res = max(res, f[i]);
	printf("%d", res);
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	freopen("", "r", stdin);
	//freopen("", "w", stdout);
	nhap();
	solve();
	print();
}


#include<bits/stdc++.h>
#define finc(i,a,b) for (int i = a; i <= b; ++i)
#define fdec(i,a,b) for (int i = a; i >= b; --i)
#define ms(a, i) memset(a, i, sizeof(a))
#define sqr(a) (long long)(a) * (a)
#define X first
#define Y second
#define ii pair<int, int>
#define db(x) cerr << #x << " = " << x << endl;
#define eb(x, y) emplace_back(x, y)
#define ll long long
#define pb(x) push_back(x)
#define endl '\n'
#define sd(x) scanf("%d", &x)
#define mx(l, r) *max_element(l, r)

using namespace std;

const int oo = 1e9 + 7;
const int maxn = 1e6 + 7;
int n, x, f[maxn];

void nhap() {
	sd(n);
	finc(i, 1, n){
		sd(x);
		f[x]++;
	}
}

void solve(){
	finc(i, 1, maxn - 7){
		finc(j, 2, maxn / i)
			if (f[i * j])
				f[i * j] = max(f[i * j], f[i] + 1);
	}
}

void print(){
	int res = 0;
	finc(i, 0, maxn - 1)
		res = max(res, f[i]);
	cout << res;
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	//freopen("", "r", stdin);
	//freopen("", "w", stdout);
	nhap();
	solve();
	print();
}


#include<bits/stdc++.h>
#define finc(i,a,b) for (int i = a; i <= b; ++i)
#define fdec(i,a,b) for (int i = a; i >= b; --i)
#define ms(a, i) memset(a, i, sizeof(a))
#define sqr(a) (long long)(a) * (a)
#define X first
#define Y second
#define ii pair<int, int>
#define db(x) cerr << #x << " = " << x << endl;
#define eb(x, y) emplace_back(x, y)
#define ll long long
#define pb(x) push_back(x)
#define endl '\n'
#define sd(x) scanf("%d", &x)
#define mx(l, r) *max_element(l, r)

using namespace std;

const int oo = 1e9 + 7;
const int maxn = 1e6 + 7;
int n, x, f[maxn];

void nhap() {
	sd(n);
	finc(i, 1, n){
		sd(x);
		f[x]++;
	}
}

void solve(){
	finc(i, 1, maxn - 7){
		finc(j, 2, maxn / i)
			if (f[i * j])
				f[i * j] = max(f[i * j], f[i] + 1);
	}
}

void print(){
	int res = 0;
	finc(i, 0, maxn - 1)
		res = max(res, f[i]);
	printf("%d", res);
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	//freopen("", "r", stdin);
	//freopen("", "w", stdout);
	nhap();
	solve();
	print();
}


#include<bits/stdc++.h>
#define finc(i,a,b) for (int i = a; i <= b; ++i)
#define fdec(i,a,b) for (int i = a; i >= b; --i)
#define ms(a, i) memset(a, i, sizeof(a))
#define sqr(a) (long long)(a) * (a)
#define X first
#define Y second
#define ii pair<int, int>
#define db(x) cerr << #x << " = " << x << endl;
#define eb(x, y) emplace_back(x, y)
#define ll long long
#define pb(x) push_back(x)
#define endl '\n'
#define sd(x) scanf("%d", &x)
#define mx(l, r) *max_element(l, r)

using namespace std;

const int oo = 1e9 + 7;
const int maxn = 1e6 + 7;
int n, x, f[maxn];

void nhap() {
	sd(n);
	finc(i, 1, n){
		sd(x);
		f[x]++;
	}
}

void solve(){
	finc(i, 1, trunc(sqrt(maxn))){
		finc(j, 2, maxn / i)
			if (f[i * j])
				f[i * j] = max(max(f[i * j], f[i] + 1), f[j] + 1);
	}
}

void print(){
	int res = 0;
	finc(i, 0, maxn - 1)
		res = max(res, f[i]);
	cout << res;
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	//freopen("", "r", stdin);
	//freopen("", "w", stdout);
	nhap();
	solve();
	print();
}


#include<bits/stdc++.h>
#define finc(i,a,b) for (int i = a; i <= b; ++i)
#define fdec(i,a,b) for (int i = a; i >= b; --i)
#define ms(a, i) memset(a, i, sizeof(a))
#define sqr(a) (long long)(a) * (a)
#define X first
#define Y second
#define ii pair<int, int>
#define db(x) cerr << #x << " = " << x << endl;
#define eb(x, y) emplace_back(x, y)
#define ll long long
#define pb(x) push_back(x)
#define endl '\n'
#define sd(x) scanf("%d", &x)
#define mx(l, r) *max_element(l, r)

using namespace std;

const int oo = 1e9 + 7;
const int maxn = 1e6 + 7;
int n, x, f[maxn];

void nhap() {
	sd(n);
	finc(i, 1, n){
		sd(x);
		f[x]++;
	}
}

void solve(){
	finc(i, 1, maxn - 7){
		finc(j, 2, maxn / i)
			if (f[i * j])
				f[i * j] = max(f[i * j], f[i] + 1);
	}
}

void print(){
	printf("%d", mx(f + 1, f + maxn + 1));
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	//freopen("", "r", stdin);
	//freopen("", "w", stdout);
	nhap();
	solve();
	print();
}


#include<bits/stdc++.h>
#define finc(i,a,b) for (int i = a; i <= b; ++i)
#define fdec(i,a,b) for (int i = a; i >= b; --i)
#define ms(a, i) memset(a, i, sizeof(a))
#define sqr(a) (long long)(a) * (a)
#define X first
#define Y second
#define ii pair<int, int>
#define db(x) cerr << #x << " = " << x << endl;
#define eb(x, y) emplace_back(x, y)
#define ll long long
#define pb(x) push_back(x)
#define endl '\n'
#define sd(x) scanf("%d", &x)
#define mx(l, r) *max_element(l, r)

using namespace std;

const int oo = 1e9 + 7;
const int maxn = 1e6 + 7;
int n, x, f[maxn], t;
int res = 0;

void nhap() {
	sd(n);
	finc(i, 1, n){
		sd(x);
		f[x]++;
	}
}

void solve(){
	finc(i, 1, (maxn - 7)){
	    t = maxn / i;
		finc(j, 2, t)
			if (f[i * j]){
				f[i * j] = max(max(f[i * j], f[i] + 1), f[j] + 1);
				res = max(f[i * j], res);
			}
	}
}

void print(){
	printf("%d", res);
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	//freopen("", "r", stdin);
	//freopen("", "w", stdout);
	nhap();
	solve();
	print();
}


#include<iostream>
#include<vector>
#define maxn 1000001
using namespace std;

vector<int> info[maxn];
int stat[maxn];
int ans[maxn];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    stat[1]=1;
    //stat[2]=1;
    //info[2].push_back(2);
    for(int i=2;i<maxn;i++)
    {
        if(stat[i]!=0)
            continue;

        stat[i]=1;
        info[i].push_back(i);

        for(int j=2;j*i<maxn;j++)
        {
            stat[i*j]=1;
            info[i*j].push_back(i);

        }

    }

   /* for(int i=2;i<maxn;i++)
    {
        for(int j=0;j<info[i].size();j++)
            cout<<info[i][j]<<" ";
        cout<<endl;
    }
*/
    //int n;
    for(int i=0;i<maxn;i++)
     stat[i]=0;

    int n,a;
    cin>>n;

    for(int i=0;i<n;i++)
    {
     cin>>a;
     stat[a]=1;
    }

    int mans=0;
    if(stat[1]==1)
        mans=1;

    ans[1]=mans;
    for(int i=2;i<maxn;i++)
    {
        /*if(stat[i]==0)
            continue;
            */
        int temp=0;
        for(int k=0;k<info[i].size();k++)
        {

            int tem=0;
            if(stat[i]==1)
                tem=1;

            //if(stat[i/info[i][k]]==1)
             tem+=ans[i/info[i][k]];

            if(tem>temp)
                temp= tem;

        }
  //      cout<<temp<<endl;
        ans[i]=temp;
        if(temp>mans)
            mans=temp;
    }

 //if(stat[1]==1&&mans!=1)
   // mans++;
 cout<<mans;
 return 0;
}


#include<iostream>
#include<vector>
#define maxn 1000001
using namespace std;

vector<int> info[maxn];
int stat[maxn];
int ans[maxn];

int main()
{

    stat[1]=1;
    //stat[2]=1;
    //info[2].push_back(2);
    for(int i=2;i<maxn;i++)
    {
        if(stat[i]!=0)
            continue;

        stat[i]=1;
        info[i].push_back(i);

        for(int j=2;j*i<maxn;j++)
        {
            stat[i*j]=1;
            info[i*j].push_back(i);

        }

    }

   /* for(int i=2;i<maxn;i++)
    {
        for(int j=0;j<info[i].size();j++)
            cout<<info[i][j]<<" ";
        cout<<endl;
    }
*/
    //int n;
    for(int i=0;i<maxn;i++)
     stat[i]=0;

    int n,a;
    cin>>n;

    for(int i=0;i<n;i++)
    {
     cin>>a;
     stat[a]=1;
    }

    int mans=0;
    if(stat[1]==1)
        mans=1;

    ans[1]=mans;
    for(int i=2;i<maxn;i++)
    {
        /*if(stat[i]==0)
            continue;
            */
        int temp=0;
        for(int k=0;k<info[i].size();k++)
        {

            int tem=0;
            if(stat[i]==1)
                tem=1;

            //if(stat[i/info[i][k]]==1)
             tem+=ans[i/info[i][k]];

            if(tem>temp)
                temp= tem;

        }
  //      cout<<temp<<endl;
        ans[i]=temp;
        if(temp>mans)
            mans=temp;
    }

 if(stat[1]==1&&mans!=1)
    mans++;
 cout<<mans;
 return 0;
}


#include<iostream>
#include<vector>
#define maxn 1000001
using namespace std;

vector<int> info[maxn];
int stat[maxn];
int ans[maxn];

int main()
{
    ios_base::sync_with_stdio(false);

    stat[1]=1;
    //stat[2]=1;
    //info[2].push_back(2);
    for(int i=2;i<maxn;i++)
    {
        if(stat[i]!=0)
            continue;

        stat[i]=1;
        info[i].push_back(i);

        for(int j=2;j*i<maxn;j++)
        {
            stat[i*j]=1;
            info[i*j].push_back(i);

        }

    }

   /* for(int i=2;i<maxn;i++)
    {
        for(int j=0;j<info[i].size();j++)
            cout<<info[i][j]<<" ";
        cout<<endl;
    }*/

    //int n;
    for(int i=0;i<maxn;i++)
     stat[i]=0;

    int n,a;
    cin>>n;

    for(int i=0;i<n;i++)
    {
     cin>>a;
     stat[a]=1;
    }

    int mans=0;
    if(stat[1]==1)
        mans=1;
    for(int i=2;i<maxn;i++)
    {
        if(stat[i]==0)
            continue;

        int temp=0;
        for(int k=0;k<info[i].size();k++)
        {
            int tem=1;
            if(stat[i/info[i][k]]==1)
             tem+=ans[i/info[i][k]];

            if(tem>temp)
                temp= tem;

        }
        //cout<<temp<<endl;
        ans[i]=temp;
        if(temp>mans)
            mans=temp;
    }

 if(stat[1]==1&&mans!=1)
    mans++;
 cout<<mans;
 return 0;
}


#include<iostream>
#include<vector>
#define maxn 1000001
using namespace std;

vector<int> info[maxn];
int stat[maxn];
int ans[maxn];

int main()
{
    stat[1]=1;
    //stat[2]=1;
    //info[2].push_back(2);
    for(int i=2;i<maxn;i++)
    {
        if(stat[i]!=0)
            continue;

        stat[i]=1;
        info[i].push_back(i);

        for(int j=2;j*i<maxn;j++)
        {
            stat[i*j]=1;
            info[i*j].push_back(i);

        }

    }

    for(int i=2;i<maxn;i++)
    {
        for(int j=0;j<info[i].size();j++)
            cout<<info[i][j]<<" ";
        cout<<endl;
    }

    //int n;
    for(int i=0;i<maxn;i++)
     stat[i]=0;

    int n,a;
    cin>>n;

    for(int i=0;i<n;i++)
    {
     cin>>a;
     stat[a]=1;
    }

    int mans=0;
    if(stat[1]==1)
        mans=1;
    for(int i=2;i<maxn;i++)
    {
        if(stat[i]==0)
            continue;

        int temp=0;
        for(int k=0;k<info[i].size();k++)
        {
            int tem=1;
            if(stat[i/info[i][k]]==1)
             tem+=ans[i/info[i][k]];

            if(tem>temp)
                temp= tem;

        }
        cout<<temp<<endl;
        ans[i]=temp;
        if(temp>mans)
            mans=temp;
    }

 if(stat[1]==1&&mans!=1)
    mans++;
 cout<<mans;
 return 0;
}


#include<iostream>
#include<vector>
#define maxn 1000001
using namespace std;

vector<int> info[maxn];
int stat[maxn];
int ans[maxn];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    stat[1]=1;
    //stat[2]=1;
    //info[2].push_back(2);
    for(int i=2;i<maxn;i++)
    {
        if(stat[i]!=0)
            continue;

        stat[i]=1;
        info[i].push_back(i);

        for(int j=2;j*i<maxn;j++)
        {
            stat[i*j]=1;
            info[i*j].push_back(i);

        }

    }

   /* for(int i=2;i<maxn;i++)
    {
        for(int j=0;j<info[i].size();j++)
            cout<<info[i][j]<<" ";
        cout<<endl;
    }
*/
    //int n;
    for(int i=0;i<maxn;i++)
     stat[i]=0;

    int n,a;
    cin>>n;

    for(int i=0;i<n;i++)
    {
     cin>>a;
     stat[a]=1;
    }

    int mans=0;
    if(stat[1]==1)
        mans=1;

    ans[1]=mans;
    for(int i=2;i<maxn;i++)
    {
        /*if(stat[i]==0)
            continue;
            */
        int temp=0;
        for(int k=0;k<info[i].size();k++)
        {

            int tem=0;
            if(stat[i]==1)
                tem=1;

            //if(stat[i/info[i][k]]==1)
             tem+=ans[i/info[i][k]];

            if(tem>temp)
                temp= tem;

        }
  //      cout<<temp<<endl;
        ans[i]=temp;
        if(temp>mans)
            mans=temp;
    }

 if(stat[1]==1&&mans!=1)
    mans++;
 cout<<mans;
 return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define MAX 1000000
int a[2000000];
int dp[2000000];

int main()
{
int n,i,j,num,ans=0;
scanf("%d",&n);

for(i=0;i<n;i++)
{
    scanf("%d",&a[i]);
    dp[a[i]]=1;
}

for(i=0;i<n;i++)
{
    num=a[i];
    for(j=2*num;j<=MAX;j+=num)
        dp[j]=max(dp[j],1+dp[num]);
    ans=max(ans,dp[num]);
}
printf("%d",ans);
return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MAX 1000000
ll a[2000000];
ll dp[2000000];
int main()
{
ll n,i,j,num;
cin>>n;

for(i=0;i<n;i++)
cin>>a[i],dp[a[i]]=1;

for(i=0;i<n;i++)
{
    num=a[i];
    for(j=2*num;j<=MAX;j+=num)
        dp[j]=max(dp[j],1+dp[num]);
}

//for(i=0;i<n;i++)
   // cout<<dp[a[i]]<<" ";
//cout<<"\n";
ll ans=0;
for(i=0;i<n;i++)
    ans=max(ans,dp[a[i]]);
cout<<ans;

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll a[2000000];
ll dp[2000000];
int main()
{
ll n,i,j;
cin>>n;

for(i=0;i<n;i++)
{
    cin>>a[i];
    dp[i]=1;
}

for(i=1;i<n;i++)
{
    for(j=i-1;j>=0;j--)
    {
        if(a[i]%a[j]==0)
        {
            dp[i]=1+dp[j];
            break;
        }
    }
}

ll ans=0;
for(i=0;i<n;i++)
    ans=max(ans,dp[i]);
cout<<ans;

return 0;
}


//Editorial
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll a[2000000];
ll dp[2000000];
int main()
{
ll n,i,j;
cin>>n;

for(i=0;i<n;i++)
cin>>a[i],dp[i]=1;

for(i=1;i<n;i++)
{
    for(j=i-1;j>=0;j--)
    {
        if(a[i]%a[j]==0)
        {
            dp[j]=1+dp[j];
            break;
        }
    }
}

ll ans=0;
for(i=0;i<n;i++)
    ans=max(ans,dp[i]);
cout<<ans;

return 0;
}