#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;

#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)
#define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define last(a) (sz(a) - 1)
#define ms(a,x) memset(a, x, sizeof(a))
#define Exist(a, b) (find(all(a), (b)) != (a).end())
#define Sort(a) sort(all(a))
#define Gsort(a) sort(all(a), greater<typeof(*((a).begin()))>())
#define Unique(a) Sort(a); (a).resize(unique(all(a)) - (a).begin())
#define Enum(a) Fit(it, (a)) cout << *it << " "; cout << endl;
#define el '\n'
#define coud(a,b) cout<<fixed << setprecision((b)) << (a)
#define debug(x) { cout << #x << " = "; cout << (x) << endl; }
#define disp(s) {cout << #s << " = "; Fit(it, (s)) cout << *it << " "; cout << el;}
#define dispn(s,n) {cout << #s << " = "; For(i,1,n) cout << s[i] << " "; cout << el;}

template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; }
template<class T> T gcd(T a, T b){ T r; while (b != 0) { r = a % b; a = b; b = r; } return a;}
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> T sqr(T x) { return x * x; }
template<class T> T cube(T x) { return x * x * x; }
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); }

const ld PI = acos(-1.0);
const ld eps = 1e-9;
const int dr[] = {-1, 0, +1, 0, -1, -1, +1, +1}; // U R D L UL UR DL DR
const int dc[] = {0, +1, 0, -1, -1, +1, -1, +1};
const int inf = (int)1e9 + 5;
const ll linf = (ll)1e16 + 5;
const ll mod = (ll)1e9 + 7;
const int MX = (int)5e4 + 5;
 /*======================== VARIABLE ================================*/
int gem[MX],sum[MX];
int ans;
int n, d;
int gt[MX][500];
bool ok[MX][500];
 /*======================== FUNCTION ================================*/
int dfs(int vt, int l, int g){
    if (ok[vt][l]){
        if (g <= gt[vt][l]) return 0;
    }
    int cur = g + gem[vt];
    ok[vt][l] = 1;
    gt[vt][l] = cur;
    ans = max(cur,ans);
    if (cur + sum[vt+1] <= ans) return 0;
    For(i,-1,1){
        int v = vt + d + l + i;
        if (v > 30000 || v <= vt) continue;
        dfs(v,l+i,cur);
    }
}
 /*======================================lo============================*/
int main(){
	#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	#endif // ONLINE_JUDGE
	std::ios::sync_with_stdio(false);
	cin.tie(NULL);
/*===================================================================*/
    cin >> n >> d;
    ms(ok,0);
    ms(gem,0);
    ms(sum,0);
    ms(gt,0);
    int x;
    For(i,1,n){
        cin >> x;
        gem[x]++;
    }
    //dispn(gem,50);
    sum[30001] = 0;
    Ford(i,30000,0){
        sum[i] = sum[i+1] + gem[i];
    }
    //dispn(sum,50);
    ans = 0;
    dfs(d,0,gem[0]);
    cout << ans << el;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;

#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)
#define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define last(a) (sz(a) - 1)
#define ms(a,x) memset(a, x, sizeof(a))
#define Exist(a, b) (find(all(a), (b)) != (a).end())
#define Sort(a) sort(all(a))
#define Gsort(a) sort(all(a), greater<typeof(*((a).begin()))>())
#define Unique(a) Sort(a); (a).resize(unique(all(a)) - (a).begin())
#define Enum(a) Fit(it, (a)) cout << *it << " "; cout << endl;
#define el '\n'
#define coud(a,b) cout<<fixed << setprecision((b)) << (a)
#define debug(x) { cout << #x << " = "; cout << (x) << endl; }
#define disp(s) {cout << #s << " = "; Fit(it, (s)) cout << *it << " "; cout << el;}
#define dispn(s,n) {cout << #s << " = "; For(i,1,n) cout << s[i] << " "; cout << el;}

template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; }
template<class T> T gcd(T a, T b){ T r; while (b != 0) { r = a % b; a = b; b = r; } return a;}
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> T sqr(T x) { return x * x; }
template<class T> T cube(T x) { return x * x * x; }
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); }

const ld PI = acos(-1.0);
const ld eps = 1e-9;
const int dr[] = {-1, 0, +1, 0, -1, -1, +1, +1}; // U R D L UL UR DL DR
const int dc[] = {0, +1, 0, -1, -1, +1, -1, +1};
const int inf = (int)1e9 + 5;
const ll linf = (ll)1e16 + 5;
const ll mod = (ll)1e9 + 7;
const int MX = (int)5e4 + 5;
 /*======================== VARIABLE ================================*/
int gem[MX],sum[MX];
int ans;
int n, d;
int gt[MX][500];
bool ok[MX][500];
 /*======================== FUNCTION ================================*/
int dfs(int vt, int l, int g){
    if (ok[vt][l]){
        if (g <= gt[vt][l]) return 0;
    }
    int cur = g + gem[vt];
    ok[vt][l] = 1;
    gt[vt][l] = cur;
    ans = max(cur,ans);
    if (cur + sum[vt+1] <= ans) return 0;
    For(i,-1,1){
        int v = vt + d + l + i;
        if (v > 30000 || v <= vt) continue;
        dfs(v,l+i,cur);
    }
}
 /*======================================lo============================*/
int main(){
	#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	#endif // ONLINE_JUDGE
	std::ios::sync_with_stdio(false);
	cin.tie(NULL);
/*===================================================================*/
    cin >> n >> d;
    ms(ok,0);
    ms(gem,0);
    ms(sum,0);
    ms(gt,0);
    int x;
    For(i,1,n){
        cin >> x;
        gem[x]++;
    }
    //dispn(gem,50);
    sum[30001] = 0;
    Ford(i,30000,0){
        sum[i] = sum[i+1] + gem[i];
    }
    //dispn(sum,50);
    ans = 0;
    dfs(d,0,gem[d]);
    cout << ans << el;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;

#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)
#define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define last(a) (sz(a) - 1)
#define ms(a,x) memset(a, x, sizeof(a))
#define Exist(a, b) (find(all(a), (b)) != (a).end())
#define Sort(a) sort(all(a))
#define Gsort(a) sort(all(a), greater<typeof(*((a).begin()))>())
#define Unique(a) Sort(a); (a).resize(unique(all(a)) - (a).begin())
#define Enum(a) Fit(it, (a)) cout << *it << " "; cout << endl;
#define el '\n'
#define coud(a,b) cout<<fixed << setprecision((b)) << (a)
#define debug(x) { cout << #x << " = "; cout << (x) << endl; }
#define disp(s) {cout << #s << " = "; Fit(it, (s)) cout << *it << " "; cout << el;}
#define dispn(s,n) {cout << #s << " = "; For(i,1,n) cout << s[i] << " "; cout << el;}

template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; }
template<class T> T gcd(T a, T b){ T r; while (b != 0) { r = a % b; a = b; b = r; } return a;}
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> T sqr(T x) { return x * x; }
template<class T> T cube(T x) { return x * x * x; }
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); }

const ld PI = acos(-1.0);
const ld eps = 1e-9;
const int dr[] = {-1, 0, +1, 0, -1, -1, +1, +1}; // U R D L UL UR DL DR
const int dc[] = {0, +1, 0, -1, -1, +1, -1, +1};
const int inf = (int)1e9 + 5;
const ll linf = (ll)1e16 + 5;
const ll mod = (ll)1e9 + 7;
const int MX = (int)5e4 + 5;
 /*======================== VARIABLE ================================*/
int gem[MX],sum[MX];
int ans;
int n, d;
int F[MX][500];
 /*======================== FUNCTION ================================*/
int dfs(int cur, int l){
    if (F[cur][l] != -1) return F[cur][l];
    //cout << cur << " " << l << el;
    int res = 0;
    For(i,-1,1){
        int next = cur + d + l + i;
        if (next > 30000 || next <= cur) continue;
        res = max(res, dfs(next, l + i));
    }
    F[cur][l] = res + gem[cur];
    return F[cur][l];
}
 /*======================================lo============================*/
int main(){
	#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	#endif // ONLINE_JUDGE
	std::ios::sync_with_stdio(false);
	cin.tie(NULL);
/*===================================================================*/
    cin >> n >> d;
    ms(gem,0);
    ms(F,-1);
    int x;
    For(i,1,n){
        cin >> x;
        gem[x]++;
    }
    ans = dfs(d,0);
    cout << ans << el;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;

#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)
#define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define last(a) (sz(a) - 1)
#define ms(a,x) memset(a, x, sizeof(a))
#define Exist(a, b) (find(all(a), (b)) != (a).end())
#define Sort(a) sort(all(a))
#define Gsort(a) sort(all(a), greater<typeof(*((a).begin()))>())
#define Unique(a) Sort(a); (a).resize(unique(all(a)) - (a).begin())
#define Enum(a) Fit(it, (a)) cout << *it << " "; cout << endl;
#define el '\n'
#define coud(a,b) cout<<fixed << setprecision((b)) << (a)
#define debug(x) { cout << #x << " = "; cout << (x) << endl; }
#define disp(s) {cout << #s << " = "; Fit(it, (s)) cout << *it << " "; cout << el;}
#define dispn(s,n) {cout << #s << " = "; For(i,1,n) cout << s[i] << " "; cout << el;}

template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; }
template<class T> T gcd(T a, T b){ T r; while (b != 0) { r = a % b; a = b; b = r; } return a;}
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> T sqr(T x) { return x * x; }
template<class T> T cube(T x) { return x * x * x; }
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); }

const ld PI = acos(-1.0);
const ld eps = 1e-9;
const int dr[] = {-1, 0, +1, 0, -1, -1, +1, +1}; // U R D L UL UR DL DR
const int dc[] = {0, +1, 0, -1, -1, +1, -1, +1};
const int inf = (int)1e9 + 5;
const ll linf = (ll)1e16 + 5;
const ll mod = (ll)1e9 + 7;
const int MX = (int)2e6 + 5;
 /*======================== VARIABLE ================================*/
int gem[MX],sum[MX];
int ans;
int n, d;
bool ok[30001][30001];
 /*======================== FUNCTION ================================*/
int dfs(int vt, int l, int g){
    if (vt > 30000) return 0;
    if (ok[vt][l]) return 0;
    g += gem[vt];
    ans = max(g,ans);
    ok[vt][l] = 1;
    if (sum[vt+1] == 0) return 0;
    if (l <= 2) {
        int s = g + sum[vt+1];
        ans = max(s,ans);
    }
    else {
        if (g + sum[vt+1] <= ans) return 0;
        dfs(vt+l-1,l-1,g);
        dfs(vt+l,l,g);
        dfs(vt+l+1,l+1,g);
    }
}
 /*======================================lo============================*/
int main(){
	#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	#endif // ONLINE_JUDGE
	std::ios::sync_with_stdio(false);
	cin.tie(NULL);
/*===================================================================*/
    cin >> n >> d;
    ms(ok,0);
    ms(gem,0);
    ms(sum,0);
    int x;
    For(i,1,n){
        cin >> x;
        gem[x]++;
    }
    sum[30001] = 0;
    Ford(i,30000,0){
        sum[i] = sum[i+1] + gem[i];
    }
    ans = 0;
    dfs(d,d,0);
    cout << ans << el;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;

#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)
#define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define last(a) (sz(a) - 1)
#define ms(a,x) memset(a, x, sizeof(a))
#define Exist(a, b) (find(all(a), (b)) != (a).end())
#define Sort(a) sort(all(a))
#define Gsort(a) sort(all(a), greater<typeof(*((a).begin()))>())
#define Unique(a) Sort(a); (a).resize(unique(all(a)) - (a).begin())
#define Enum(a) Fit(it, (a)) cout << *it << " "; cout << endl;
#define el '\n'
#define coud(a,b) cout<<fixed << setprecision((b)) << (a)
#define debug(x) { cout << #x << " = "; cout << (x) << endl; }
#define disp(s) {cout << #s << " = "; Fit(it, (s)) cout << *it << " "; cout << el;}
#define dispn(s,n) {cout << #s << " = "; For(i,1,n) cout << s[i] << " "; cout << el;}

template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; }
template<class T> T gcd(T a, T b){ T r; while (b != 0) { r = a % b; a = b; b = r; } return a;}
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> T sqr(T x) { return x * x; }
template<class T> T cube(T x) { return x * x * x; }
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); }

const ld PI = acos(-1.0);
const ld eps = 1e-9;
const int dr[] = {-1, 0, +1, 0, -1, -1, +1, +1}; // U R D L UL UR DL DR
const int dc[] = {0, +1, 0, -1, -1, +1, -1, +1};
const int inf = (int)1e9 + 5;
const ll linf = (ll)1e16 + 5;
const ll mod = (ll)1e9 + 7;
const int MX = (int)2e6 + 5;
 /*======================== VARIABLE ================================*/
int gem[MX],sum[MX];
int ans;
int n, d;
 /*======================== FUNCTION ================================*/
int dfs(int vt, int l, int g){
    if (vt > 30000) return 0;
    g += gem[vt];
    ans = max(g,ans);
    if (l <= 2) {
        int s = g + sum[vt+1];
        ans = max(s,ans);
    }
    else {
        if (g + sum[vt+1] <= ans) return 0;
        dfs(vt+l-1,l-1,g);
        dfs(vt+l,l,g);
        dfs(vt+l+1,l+1,g);
    }
}
 /*======================================lo============================*/
int main(){
	#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	#endif // ONLINE_JUDGE
	std::ios::sync_with_stdio(false);
	cin.tie(NULL);
/*===================================================================*/
    cin >> n >> d;
    ms(gem,0);
    ms(sum,0);
    int x;
    For(i,1,n){
        cin >> x;
        gem[x]++;
    }
    sum[30001] = 0;
    Ford(i,30000,0){
        sum[i] = sum[i+1] + gem[i];
    }
    ans = 0;
    dfs(d,d,0);
    cout << ans << el;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;

#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)
#define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define last(a) (sz(a) - 1)
#define ms(a,x) memset(a, x, sizeof(a))
#define Exist(a, b) (find(all(a), (b)) != (a).end())
#define Sort(a) sort(all(a))
#define Gsort(a) sort(all(a), greater<typeof(*((a).begin()))>())
#define Unique(a) Sort(a); (a).resize(unique(all(a)) - (a).begin())
#define Enum(a) Fit(it, (a)) cout << *it << " "; cout << endl;
#define el '\n'
#define coud(a,b) cout<<fixed << setprecision((b)) << (a)
#define debug(x) { cout << #x << " = "; cout << (x) << endl; }
#define disp(s) {cout << #s << " = "; Fit(it, (s)) cout << *it << " "; cout << el;}
#define dispn(s,n) {cout << #s << " = "; For(i,1,n) cout << s[i] << " "; cout << el;}

template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; }
template<class T> T gcd(T a, T b){ T r; while (b != 0) { r = a % b; a = b; b = r; } return a;}
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> T sqr(T x) { return x * x; }
template<class T> T cube(T x) { return x * x * x; }
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); }

const ld PI = acos(-1.0);
const ld eps = 1e-9;
const int dr[] = {-1, 0, +1, 0, -1, -1, +1, +1}; // U R D L UL UR DL DR
const int dc[] = {0, +1, 0, -1, -1, +1, -1, +1};
const int inf = (int)1e9 + 5;
const ll linf = (ll)1e16 + 5;
const ll mod = (ll)1e9 + 7;
const int MX = (int)2e6 + 5;
 /*======================== VARIABLE ================================*/
int gem[MX],sum[MX];
int ans;
int n, d;
map<int , map< int, int> > ok;
 /*======================== FUNCTION ================================*/
int dfs(int vt, int l, int g){
    if (vt > 30000 || l > 30000) return 0;
    g += gem[vt];
    if (g <= ok[vt][l] && ok[vt][l] != 0){
        return 0;
    }
    else {
        ok[vt][l] = g;
    }
    ans = max(g,ans);
    if (l <= 2) {
        int s = g + sum[vt+1];
        ans = max(s,ans);
    }
    else {
        if (g + sum[vt+1] <= ans) return 0;
        dfs(vt+l-1,l-1,g);
        dfs(vt+l,l,g);
        dfs(vt+l+1,l+1,g);
    }
}
 /*======================================lo============================*/
int main(){
	#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	#endif // ONLINE_JUDGE
	std::ios::sync_with_stdio(false);
	cin.tie(NULL);
/*===================================================================*/
    cin >> n >> d;
    ok.clear();
    ms(gem,0);
    ms(sum,0);
    int x;
    For(i,1,n){
        cin >> x;
        gem[x]++;
    }
    //dispn(gem,50);
    sum[30001] = 0;
    Ford(i,30000,0){
        sum[i] = sum[i+1] + gem[i];
    }
    //dispn(sum,50);
    ans = 0;
    dfs(d,d,0);
    cout << ans << el;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;

#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)
#define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define last(a) (sz(a) - 1)
#define ms(a,x) memset(a, x, sizeof(a))
#define Exist(a, b) (find(all(a), (b)) != (a).end())
#define Sort(a) sort(all(a))
#define Gsort(a) sort(all(a), greater<typeof(*((a).begin()))>())
#define Unique(a) Sort(a); (a).resize(unique(all(a)) - (a).begin())
#define Enum(a) Fit(it, (a)) cout << *it << " "; cout << endl;
#define el '\n'
#define coud(a,b) cout<<fixed << setprecision((b)) << (a)
#define debug(x) { cout << #x << " = "; cout << (x) << endl; }
#define disp(s) {cout << #s << " = "; Fit(it, (s)) cout << *it << " "; cout << el;}
#define dispn(s,n) {cout << #s << " = "; For(i,1,n) cout << s[i] << " "; cout << el;}

template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; }
template<class T> T gcd(T a, T b){ T r; while (b != 0) { r = a % b; a = b; b = r; } return a;}
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> T sqr(T x) { return x * x; }
template<class T> T cube(T x) { return x * x * x; }
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); }

const ld PI = acos(-1.0);
const ld eps = 1e-9;
const int dr[] = {-1, 0, +1, 0, -1, -1, +1, +1}; // U R D L UL UR DL DR
const int dc[] = {0, +1, 0, -1, -1, +1, -1, +1};
const int inf = (int)1e9 + 5;
const ll linf = (ll)1e16 + 5;
const ll mod = (ll)1e9 + 7;
const int MX = (int)5e4 + 5;
 /*======================== VARIABLE ================================*/
int gem[MX],sum[MX];
int ans;
int n, d;
int gt[MX][500];
bool ok[MX][500];
 /*======================== FUNCTION ================================*/
int dfs(int vt, int l, int g){
    if (vt > 30000 || l > 30000) return 0;
    if (ok[vt][l]){
        if (g <= gt[vt][l]) return 0;
    }
    g += gem[vt];
    ok[vt][l] = 1;
    gt[vt][l] = max(gt[vt][l],g);
    ans = max(g,ans);
    //if (g + sum[vt+1] <= ans) return 0;
    For(i,-1,1){
        int v = vt + d + l + i;
        if (v > 30000 || v <= vt) continue;
        dfs(v,l+i,g);
    }
}
 /*======================================lo============================*/
int main(){
	#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	#endif // ONLINE_JUDGE
	std::ios::sync_with_stdio(false);
	cin.tie(NULL);
/*===================================================================*/
    cin >> n >> d;
    ms(ok,0);
    ms(gem,0);
    ms(sum,0);
    int x;
    For(i,1,n){
        cin >> x;
        gem[x]++;
    }
    //dispn(gem,50);
    sum[30001] = 0;
    Ford(i,30000,0){
        sum[i] = sum[i+1] + gem[i];
    }
    //dispn(sum,50);
    ans = 0;
    dfs(d,0,0);
    cout << ans << el;
}


#pragma comment(linker, "/STACK:26777216")
#pragma warning(disable:4786)

#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
#define NN 30002
#define MM 248
#define CLR( a ) memset( a , 0 , sizeof ( a ) )
#define negCLR( a ) memset( a , -1 , sizeof( a ) )
#define lld I64d
#define FOR(i,s,t) for(int i = (s); i <= (t); i++)
const lint MOD =  100000007;
const lint LINF = 1000000097999999903LL;
lint boundz = 10000000001;
const int INF = (1 << 29);
int cases, caseno = 0;
int N, D;
int offset = 248; // D = 30000, 1,1+1,1+1+1, do the trick n * (n + 1)/2
//so 248 will split the calculation within memory

int arr[ NN ];
/*
offset dp

int mem[ NN ][ MM + MM ];

int f( int i, int jmp ) {
    if( i > 30000 ) return 0;
    if( jmp <= 0 ) return -2;
    int &ret = mem[ i ][ jmp + offset ];
    if( ret != -1 )return ret;
    ret = 0; int r1 = 0, r2 = 0, r3 = 0;
    r1 = f( i + jmp + 1, jmp + 1 );
    r2 = f( i + jmp, jmp );
    r3 = f( i + jmp - 1, jmp - 1 );
    ret = arr[ i ] + max(r1,max(r2, r3) );
    return ret;
}
reset();
    offset = D - offset;  // offset + D will always be withi 0 to 248
    offset = -1 * offset;
*/

int mem[ NN ][ 2002 ];

int f( int i, int jmp ) {
  if( i > 30000 )return 0;
  if( jmp <= 0 )return -2;
  int &ret = mem[ i ][ jmp ];
  if( ret != -1 )return ret;
  ret = 0; int r1 = 0, r2 = 0, r3 = 0;
  r1 = f( i + jmp + 1, jmp + 1 );
  r2 = f( i + jmp, jmp );
  r3 = f( i + jmp - 1 , jmp - 1 );
  ret = arr[ i ] + max( r1, max( r2, r3 ) );
  return ret;
}
int f_noMemo( int i, int jmp ) {
  if( i > 30000 )return 0;
  if( jmp <= 0 )return -2;
  //int &ret = mem[ i ][ jmp ];
  //if( ret != -1 )return ret;
  int ret = 0; int r1 = 0, r2 = 0, r3 = 0;

  if( i <= 2000 ) {
     r1 = f( i + jmp + 1, jmp + 1 );
     r2 = f( i + jmp, jmp );
     r3 = f( i + jmp - 1 , jmp - 1 );
  } else {
     r1 = f_noMemo( i + jmp + 1, jmp + 1 );
     r2 = f_noMemo( i + jmp, jmp );
     r3 = f_noMemo( i + jmp - 1 , jmp - 1 );
  }
  ret = arr[ i ] + max( r1, max( r2, r3 ) );
  return ret;
}

void reset() {
    offset = 248;
    CLR( arr ); negCLR( mem );
}

int main(){
  //freopen("in.txt","r",stdin);
  //freopen("outz.txt","w",stdout);
  //scanf("%d",&cases);
  //while( cases-- ){
    scanf("%d %d", &N, &D);
    reset();
    int x;
    for( int i = 1; i <= N; i++ ) { scanf("%d",&x); arr[ x ]++; }
    int res  = 0;
    if( D <= 2000 ) {
        res = f( D, D);
    } else {
        res = f_noMemo( D, D);
    }

    printf("%d\n",res);

  //}
  return 0;
}


#pragma comment(linker, "/STACK:26777216")
#pragma warning(disable:4786)

#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
#define NN 30002
#define MM 248
#define CLR( a ) memset( a , 0 , sizeof ( a ) )
#define negCLR( a ) memset( a , -1 , sizeof( a ) )
#define lld I64d
#define FOR(i,s,t) for(int i = (s); i <= (t); i++)
const lint MOD =  100000007;
const lint LINF = 1000000097999999903LL;
lint boundz = 10000000001;
const int INF = (1 << 29);
int cases, caseno = 0;
int N, D;
int offset = 248; // D = 30000, 1,1+1,1+1+1, do the trick n * (n + 1)/2
//so 248 will split the calculation within memory

int mem[ NN ][ MM + MM ];
int dp[ NN ][ MM + MM ];
int arr[ NN ];

int f( int i, int jmp ) {
    if( i > 30000 ) return 0;
    if( jmp <= 0 ) return -2;
    int &ret = mem[ i ][ jmp + offset ];
    if( ret != -1 )return ret;
    ret = 0; int r1 = 0, r2 = 0, r3 = 0;
    r1 = f( i + jmp + 1, jmp + 1 );
    r2 = f( i + jmp, jmp );
    r3 = f( i + jmp - 1, jmp - 1 );
    ret = arr[ i ] + max(r1,max(r2, r3) );
    return ret;
}

void reset() {
    offset = 248;
    CLR( arr );  CLR( mem ); CLR( dp );
    //negCLR( mem );
}

int main(){
  //freopen("in.txt","r",stdin);
  //freopen("outz.txt","w",stdout);
  //scanf("%d",&cases);
  //while( cases-- ){
    scanf("%d %d", &N, &D);
    reset();
    offset = D - offset;  // offset + D will always be withi 0 to 248
    offset = -1 * offset;
    int x;
    for( int ii = 1; ii <= N; ii++ ) { scanf("%d",&x); arr[ x ]++; }
    //int res  = f( D, D );
    int res = arr[ D ] + 1;
    mem[ D ][ D + offset ] = arr[ D ] + 1;
    dp[ D ][ D + offset ] = arr[ D ] + 1;
    for(int i=D+1;i<=30000;i++)
    {
        //if(i%10==0) printf("%d",i);
        for(int j=1;j<=D+offset;j++)
        {
            if(i-j<0) break;
            dp[i][j + offset]=0;
            if(dp[i-j][j + offset -1]) dp[i][j + offset ]=dp[i-j][j + offset -1];
            if(dp[i-j][j + offset ]) dp[i][j + offset ]=max(dp[i][j + offset ],dp[i-j][j + offset ]);
            if(dp[i-j][j+ offset + 1]) dp[i][j + offset ]=max(dp[i][j + offset ],dp[i-j][j+ offset + 1]);
            if(dp[i][j + offset ]>0) dp[i][j + offset ]+=arr[i];
            res=max(res,dp[i][j + offset ]);
            //printf("%d",dp[i][j]);
        }
    }

    printf("%d\n",res - 1);

  //}
  return 0;
}


#pragma comment(linker, "/STACK:26777216")
#pragma warning(disable:4786)

#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
#define NN 30002
#define MM 248
#define CLR( a ) memset( a , 0 , sizeof ( a ) )
#define negCLR( a ) memset( a , -1 , sizeof( a ) )
#define lld I64d
#define FOR(i,s,t) for(int i = (s); i <= (t); i++)
const lint MOD =  100000007;
const lint LINF = 1000000097999999903LL;
lint boundz = 10000000001;
const int INF = (1 << 29);
int cases, caseno = 0;
int N, D;
int offset = 248; // D = 30000, 1,1+1,1+1+1, do the trick n * (n + 1)/2
//so 248 will split the calculation within memory

int mem[ NN ][ MM + MM ];
int arr[ NN ];

int f( int i, int jmp ) {
    if( i > 30000 ) return 0;
    if( jmp <= 0 ) return -2;
    int &ret = mem[ i ][ jmp + offset ];
    if( ret != -1 )return ret;
    ret = 0; int r1 = 0, r2 = 0, r3 = 0;
    r1 = f( i + jmp + 1, jmp + 1 );
    r2 = f( i + jmp, jmp );
    r3 = f( i + jmp - 1, jmp - 1 );
    ret = arr[ i ] + max(r1,max(r2, r3) );
    return ret;
}

void reset() {
    offset = 248;
    CLR( arr ); negCLR( mem );
}

int main(){
  //freopen("in.txt","r",stdin);
  //freopen("outz.txt","w",stdout);
  //scanf("%d",&cases);
  //while( cases-- ){
    scanf("%d %d", &N, &D);
    reset();
    offset = D - offset;  // offset + D will always be withi 0 to 248
    offset = -1 * offset;
    int x;
    for( int i = 1; i <= N; i++ ) { scanf("%d",&x); arr[ x ]++; }
    int res  = f( D, D );
    printf("%d\n",res);

  //}
  return 0;
}


#pragma comment(linker, "/STACK:26777216")
#pragma warning(disable:4786)

#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
#define NN 30002
#define MM 248
#define CLR( a ) memset( a , 0 , sizeof ( a ) )
#define negCLR( a ) memset( a , -1 , sizeof( a ) )
#define lld I64d
#define FOR(i,s,t) for(int i = (s); i <= (t); i++)
const lint MOD =  100000007;
const lint LINF = 1000000097999999903LL;
lint boundz = 10000000001;
const int INF = (1 << 29);
int cases, caseno = 0;
int N, D;
int offset = 248; // D = 30000, 1,1+1,1+1+1, do the trick n * (n + 1)/2
//so 248 will split the calculation within memory

int f[ NN ][ MM + MM + 1 ];
int arr[ NN ];

void reset() {
    offset = 248;
    CLR( arr );  negCLR( f );
    //negCLR( mem );
}

int main(){
  //freopen("in.txt","r",stdin);
  //freopen("outz.txt","w",stdout);
  //scanf("%d",&cases);
  //while( cases-- ){
    scanf("%d %d", &N, &D);
    reset();
    //offset = D - offset;  // offset + D will always be withi 0 to 248
    //offset = -1 * offset;
    int x;
    for( int ii = 1; ii <= N; ii++ ) { scanf("%d",&x); arr[ x ]++; }
    int res = 0;
    /*for( int i = 0; i <= 30000; i++ ) {
        for( int j = 0; j <= 2*offset; j++ ) {
            f[ i ][ j ] = -1;
        }
    }*/

    f[ D ][ offset ] = arr[ D ];

    for( int i = D; i <= 30000; i++ ) {

        for( int j = -offset ; j <= offset; j++ ) {

            if( f[ i ][ j + offset ] == -1 ) continue;

            if( res < f[ i ][ j + offset ] ){ res = f[ i ][ j + offset ]; }

            for( int mov = j - 1; mov <= j + 1; mov++ ) {
                int curr_i = i + D + mov;
                if( D + mov < 1 || curr_i > 30000 )continue; // D + mov = next move is atleast 1, mov = moves + offset to adjust D within 2*255
                if( f[ curr_i ][ mov + offset ] == -1 || f[ curr_i ][ mov + offset ] < f[ i ][ j + offset ] + arr[ curr_i ] ) {
                    f[ curr_i ][ mov + offset ] = f[ i ][ j + offset ] + arr[ curr_i ];
                }

            }
        }
    }

    printf("%d\n",res );

  //}
  return 0;
}


#pragma comment(linker, "/STACK:26777216")
#pragma warning(disable:4786)

#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
#define NN 30002
#define MM 248
#define CLR( a ) memset( a , 0 , sizeof ( a ) )
#define negCLR( a ) memset( a , -1 , sizeof( a ) )
#define lld I64d
#define FOR(i,s,t) for(int i = (s); i <= (t); i++)
const lint MOD =  100000007;
const lint LINF = 1000000097999999903LL;
lint boundz = 10000000001;
const int INF = (1 << 29);
int cases, caseno = 0;
int N, D;
int offset = 248; // D = 30000, 1,1+1,1+1+1, do the trick n * (n + 1)/2
//so 248 will split the calculation within memory

int mem[ NN ][ MM + MM ];
int dp[ NN ][ MM + MM ];
int arr[ NN ];

int f( int i, int jmp ) {
    if( i > 30000 ) return 0;
    if( jmp <= 0 ) return -2;
    int &ret = mem[ i ][ jmp + offset ];
    if( ret != -1 )return ret;
    ret = 0; int r1 = 0, r2 = 0, r3 = 0;
    r1 = f( i + jmp + 1, jmp + 1 );
    r2 = f( i + jmp, jmp );
    r3 = f( i + jmp - 1, jmp - 1 );
    ret = arr[ i ] + max(r1,max(r2, r3) );
    return ret;
}

void reset() {
    offset = 248;
    CLR( arr );  CLR( mem ); CLR( dp );
    //negCLR( mem );
}

int main(){
  //freopen("in.txt","r",stdin);
  //freopen("outz.txt","w",stdout);
  //scanf("%d",&cases);
  //while( cases-- ){
    scanf("%d %d", &N, &D);
    reset();
    offset = D - offset;  // offset + D will always be withi 0 to 248
    offset = -1 * offset;
    int x;
    for( int ii = 1; ii <= N; ii++ ) { scanf("%d",&x); arr[ x ]++; }
    //int res  = f( D, D );
    int res = arr[ D ] + 1;
    mem[ D ][ D + offset ] = arr[ D ] + 1;
    dp[ D ][ D + offset ] = arr[ D ] + 1;
    for(int i=D+1;i<=30000;i++)
    {
        //if(i%10==0) printf("%d",i);
        for(int j=1;j<=D+1;j++)
        {
            if(i-j<0) break;
            dp[i][j + offset]=0;
            if(dp[i-j][j + offset -1]) dp[i][j + offset ]=dp[i-j][j + offset -1];
            if(dp[i-j][j + offset ]) dp[i][j + offset ]=max(dp[i][j + offset ],dp[i-j][j + offset ]);
            if(dp[i-j][j+ offset + 1]) dp[i][j + offset ]=max(dp[i][j + offset ],dp[i-j][j+ offset + 1]);
            if(dp[i][j + offset ]>0) dp[i][j + offset ]+=arr[i];
            res=max(res,dp[i][j + offset ]);
            //printf("%d",dp[i][j]);
        }
    }

    printf("%d\n",res - 1);

  //}
  return 0;
}


#pragma comment(linker, "/STACK:26777216")
#pragma warning(disable:4786)

#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
#define NN 30002
#define MM 248
#define CLR( a ) memset( a , 0 , sizeof ( a ) )
#define negCLR( a ) memset( a , -1 , sizeof( a ) )
#define lld I64d
#define FOR(i,s,t) for(int i = (s); i <= (t); i++)
const lint MOD =  100000007;
const lint LINF = 1000000097999999903LL;
lint boundz = 10000000001;
const int INF = (1 << 29);
int cases, caseno = 0;
int N, D;
int offset = 248; // D = 30000, 1,1+1,1+1+1, do the trick n * (n + 1)/2
//so 248 will split the calculation within memory

int mem[ NN ][ MM + MM + 1 ];
int dp[ NN ][ MM + MM + 1];
int arr[ NN ];

int f( int i, int jmp ) {
    if( i > 30000 ) return 0;
    if( jmp <= 0 ) return -2;
    int &ret = mem[ i ][ jmp + offset ];
    if( ret != -1 )return ret;
    ret = 0; int r1 = 0, r2 = 0, r3 = 0;
    r1 = f( i + jmp + 1, jmp + 1 );
    r2 = f( i + jmp, jmp );
    r3 = f( i + jmp - 1, jmp - 1 );
    ret = arr[ i ] + max(r1,max(r2, r3) );
    return ret;
}

void reset() {
    offset = 248;
    CLR( arr );  CLR( mem ); CLR( dp );
    //negCLR( mem );
}

int main(){
  //freopen("in.txt","r",stdin);
  //freopen("outz.txt","w",stdout);
  //scanf("%d",&cases);
  //while( cases-- ){
    scanf("%d %d", &N, &D);
    reset();
    offset = D - offset;  // offset + D will always be withi 0 to 248
    offset = -1 * offset;
    int x;
    for( int ii = 1; ii <= N; ii++ ) { scanf("%d",&x); arr[ x ]++; }
    //int res  = f( D, D );
    int res = arr[ D ] + 1;
    mem[ D ][ D + offset ] = arr[ D ] + 1;
    dp[ D ][ D + offset ] = arr[ D ] + 1;
    for(int i=D+1;i<=30000;i++)
    {
        //if(i%10==0) printf("%d",i);
        for(int j=1; ;j++)
        {
            if(i-j<0) break;
            dp[i][j + offset]=0;
            if(dp[i-j][j + offset -1]) dp[i][j + offset ]=dp[i-j][j + offset -1];
            if(dp[i-j][j + offset ]) dp[i][j + offset ]=max(dp[i][j + offset ],dp[i-j][j + offset ]);
            if(dp[i-j][j+ offset + 1]) dp[i][j + offset ]=max(dp[i][j + offset ],dp[i-j][j+ offset + 1]);
            if(dp[i][j + offset ]>0) dp[i][j + offset ]+=arr[i];
            res=max(res,dp[i][j + offset ]);
            //printf("%d",dp[i][j]);
        }
    }

    printf("%d\n",res - 1);

  //}
  return 0;
}


#pragma comment(linker, "/STACK:26777216")
#pragma warning(disable:4786)

#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
#define NN 30002
#define MM 248
#define CLR( a ) memset( a , 0 , sizeof ( a ) )
#define negCLR( a ) memset( a , -1 , sizeof( a ) )
#define lld I64d
#define FOR(i,s,t) for(int i = (s); i <= (t); i++)
const lint MOD =  100000007;
const lint LINF = 1000000097999999903LL;
lint boundz = 10000000001;
const int INF = (1 << 29);
int cases, caseno = 0;
int N, D;
int offset = 248; // D = 30000, 1,1+1,1+1+1, do the trick n * (n + 1)/2
//so 248 will split the calculation within memory

int mem[ NN ][ MM + MM ];
int arr[ NN ];

int f( int i, int jmp ) {
    if( i > 30000 ) return 0;
    if( jmp <= 0 ) return -2;
    int &ret = mem[ i ][ jmp + offset ];
    if( ret != -1 )return ret;
    ret = 0; int r1 = 0, r2 = 0, r3 = 0;
    r1 = f( i + jmp + 1, jmp + 1 );
    r2 = f( i + jmp, jmp );
    r3 = f( i + jmp - 1, jmp - 1 );
    ret = arr[ i ] + max(r1,max(r2, r3) );
    return ret;
}

void reset() {
    offset = 248;
    CLR( arr );  CLR( mem );
    //negCLR( mem );
}

int main(){
  //freopen("in.txt","r",stdin);
  //freopen("outz.txt","w",stdout);
  //scanf("%d",&cases);
  //while( cases-- ){
    scanf("%d %d", &N, &D);
    reset();
    offset = D - offset;  // offset + D will always be withi 0 to 248
    offset = -1 * offset;
    int x;
    for( int ii = 1; ii <= N; ii++ ) { scanf("%d",&x); arr[ x ]++; }
    //int res  = f( D, D );
    int res = arr[ D ] + 1;
    mem[ D ][ D + offset ] = arr[ D ] + 1; // + 1

    for(int i = D + 1; i <= 30000 ; i++ ) {
        for( int j = 1; i - j >= 0 && j + offset + 1 <= offset + offset ; j++ ) {
          mem[ i ][ j + offset ] = 0;
          if( mem[ i - j ][ j + offset - 1 ] ) mem[ i ][ j + offset ] = mem[ i - j ][ j + offset - 1 ];
          if( mem[ i -  j ][ j + offset ] ) mem[ i ][ j + offset ] = max( mem[ i ][ j + offset ], mem[ i - j ][ j + offset] );
          if( mem[ i - j ][ j + offset + 1 ] ) mem[ i ][ j + offset ] = max( mem[ i ][ j + offset ], mem[ i - j ][ j + offset + 1 ] );
          if( mem[ i ][ j + offset ] ) {
            mem[ i ][ j + offset ] += arr[ i ];
          }
         res = max( res, mem[ i ][ j + offset ] );
        }
    }

    printf("%d\n",res - 1);

  //}
  return 0;
}


#pragma comment(linker, "/STACK:26777216")
#pragma warning(disable:4786)

#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
#define NN 30002
#define MM 250
#define CLR( a ) memset( a , 0 , sizeof ( a ) )
#define negCLR( a ) memset( a , -1 , sizeof( a ) )
#define lld I64d
#define FOR(i,s,t) for(int i = (s); i <= (t); i++)
const lint MOD =  100000007;
const lint LINF = 1000000097999999903LL;
lint boundz = 10000000001;
const int INF = (1 << 29);
int cases, caseno = 0;
int N, D;
int offset = 248; // D = 30000, 1,1+1,1+1+1, do the trick n * (n + 1)/2
//so 248 will split the calculation within memory

int mem[ NN ][ MM ];
int arr[ NN ];

int f( int i, int jmp ) {
    if( i > 30000 ) return 0;
    if( jmp <= 0 ) return -2;
    int &ret = mem[ i ][ jmp + offset ];
    if( ret != -1 )return ret;
    ret = 0; int r1 = 0, r2 = 0, r3 = 0;
    r1 = f( i + jmp + 1, jmp + 1 );
    r2 = f( i + jmp, jmp );
    r3 = f( i + jmp - 1, jmp - 1 );
    ret = arr[ i ] + max(r1,max(r2, r3) );
    return ret;
}

void reset() {
    offset = 248;
    CLR( arr ); negCLR( mem );
}

int main(){
  //freopen("in.txt","r",stdin);
  //freopen("outz.txt","w",stdout);
  //scanf("%d",&cases);
  //while( cases-- ){
    scanf("%d %d", &N, &D);
    reset();
    offset = D - offset;  // offset + D will always be withi 0 to 248
    offset = -1 * offset;
    int x;
    for( int i = 1; i <= N; i++ ) { scanf("%d",&x); arr[ x ]++; }
    int res  = f( D, D );
    printf("%d\n",res);

  //}
  return 0;
}


#pragma comment(linker, "/STACK:26777216")
#pragma warning(disable:4786)

#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
#define NN 30002
#define MM 248
#define CLR( a ) memset( a , 0 , sizeof ( a ) )
#define negCLR( a ) memset( a , -1 , sizeof( a ) )
#define lld I64d
#define FOR(i,s,t) for(int i = (s); i <= (t); i++)
const lint MOD =  100000007;
const lint LINF = 1000000097999999903LL;
lint boundz = 10000000001;
const int INF = (1 << 29);
int cases, caseno = 0;
int N, D;
int offset = 248; // D = 30000, 1,1+1,1+1+1, do the trick n * (n + 1)/2
//so 248 will split the calculation within memory

int mem[ NN ][ MM + MM ];
int arr[ NN ];

int f( int i, int jmp ) {
    if( i > 30000 ) return 0;
    if( jmp <= 0 ) return -2;
    int &ret = mem[ i ][ jmp + offset ];
    if( ret != -1 )return ret;
    ret = 0; int r1 = 0, r2 = 0, r3 = 0;
    r1 = f( i + jmp + 1, jmp + 1 );
    r2 = f( i + jmp, jmp );
    r3 = f( i + jmp - 1, jmp - 1 );
    ret = arr[ i ] + max(r1,max(r2, r3) );
    return ret;
}

void reset() {
    offset = 248;
    CLR( arr );  CLR( mem );
    //negCLR( mem );
}

int main(){
  //freopen("in.txt","r",stdin);
  //freopen("outz.txt","w",stdout);
  //scanf("%d",&cases);
  //while( cases-- ){
    scanf("%d %d", &N, &D);
    reset();
    offset = D - offset;  // offset + D will always be withi 0 to 248
    offset = -1 * offset;
    int x;
    for( int ii = 1; ii <= N; ii++ ) { scanf("%d",&x); arr[ x ]++; }
    //int res  = f( D, D );
    int res = arr[ D ] + 1;
    mem[ D ][ D + offset ] = arr[ D ] + 1; // + 1

    for(int i = D + 1; i <= 30000 ; i++ ) {
        for( int j = 1; i - j >= 0 && j + offset + 1 <= MM + MM - 1; j++ ) {
          mem[ i ][ j + offset ] = 0;
          if( mem[ i - j ][ j + offset - 1 ] ) mem[ i ][ j + offset ] = mem[ i - j ][ j + offset - 1 ];
          if( mem[ i -  j ][ j + offset ] ) mem[ i ][ j + offset ] = max( mem[ i ][ j + offset ], mem[ i - j ][ j + offset] );
          if( mem[ i - j ][ j + offset + 1 ] ) mem[ i ][ j + offset ] = max( mem[ i ][ j + offset ], mem[ i - j ][ j + offset + 1 ] );
          if( mem[ i ][ j + offset ] ) {
            mem[ i ][ j + offset ] += arr[ i ];
          }
         res = max( res, mem[ i ][ j + offset ] );
        }
    }

    printf("%d\n",res - 1);

  //}
  return 0;
}


#pragma comment(linker, "/STACK:26777216")
#pragma warning(disable:4786)

#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
#define NN 30002
#define MM 248
#define CLR( a ) memset( a , 0 , sizeof ( a ) )
#define negCLR( a ) memset( a , -1 , sizeof( a ) )
#define lld I64d
#define FOR(i,s,t) for(int i = (s); i <= (t); i++)
const lint MOD =  100000007;
const lint LINF = 1000000097999999903LL;
lint boundz = 10000000001;
const int INF = (1 << 29);
int cases, caseno = 0;
int N, D;
int offset = 248; // D = 30000, 1,1+1,1+1+1, do the trick n * (n + 1)/2
//so 248 will split the calculation within memory

int f[ NN ][ MM + MM + 1 ];
int arr[ NN ];

void reset() {
    offset = 248;
    CLR( arr );  negCLR( f );
    //negCLR( mem );
}

int main(){
  //freopen("in.txt","r",stdin);
  //freopen("outz.txt","w",stdout);
  //scanf("%d",&cases);
  //while( cases-- ){
    scanf("%d %d", &N, &D);
    reset();
    //offset = D - offset;  // offset + D will always be withi 0 to 248
    //offset = -1 * offset;
    int x;
    for( int ii = 1; ii <= N; ii++ ) { scanf("%d",&x); arr[ x ]++; }
    int res = 0;
    /*for( int i = 0; i <= 30000; i++ ) {
        for( int j = 0; j <= 2*offset; j++ ) {
            f[ i ][ j ] = -1;
        }
    }*/

    f[ D ][ offset ] = arr[ D ];

    for( int i = D; i <= 30000; i++ ) {

        for( int j = -offset ; j <= offset; j++ ) {

            if( f[ i ][ j + offset ] == -1 ) continue;

            if( res < f[ i ][ j + offset ] ){ res = f[ i ][ j + offset ]; }

            for( int mov = j - 1; mov <= j + 1; mov++ ) {
                int curr_i = i + D + mov;
                if( D + mov < 1 || curr_i > 30000 )continue; // D + mov = next move is atleast 1, mov = moves + offset to adjust D within 2*255
                if( f[ curr_i ][ mov + offset ] == -1 || f[ curr_i ][ mov + offset ] < f[ i ][ j + offset ] + arr[ i ] ) {
                    f[ curr_i ][ mov + offset ] = f[ i ][ j + offset ] + arr[ i ];
                }

            }
        }
    }

    printf("%d\n",res );

  //}
  return 0;
}


#pragma comment(linker, "/STACK:26777216")
#pragma warning(disable:4786)

#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
#define NN 30002
#define MM 248
#define CLR( a ) memset( a , 0 , sizeof ( a ) )
#define negCLR( a ) memset( a , -1 , sizeof( a ) )
#define lld I64d
#define FOR(i,s,t) for(int i = (s); i <= (t); i++)
const lint MOD =  100000007;
const lint LINF = 1000000097999999903LL;
lint boundz = 10000000001;
const int INF = (1 << 29);
int cases, caseno = 0;
int N, D;
int offset = 248; // D = 30000, 1,1+1,1+1+1, do the trick n * (n + 1)/2
//so 248 will split the calculation within memory

int arr[ NN ];
/*
offset dp

int mem[ NN ][ MM + MM ];

int f( int i, int jmp ) {
    if( i > 30000 ) return 0;
    if( jmp <= 0 ) return -2;
    int &ret = mem[ i ][ jmp + offset ];
    if( ret != -1 )return ret;
    ret = 0; int r1 = 0, r2 = 0, r3 = 0;
    r1 = f( i + jmp + 1, jmp + 1 );
    r2 = f( i + jmp, jmp );
    r3 = f( i + jmp - 1, jmp - 1 );
    ret = arr[ i ] + max(r1,max(r2, r3) );
    return ret;
}
reset();
    offset = D - offset;  // offset + D will always be withi 0 to 248
    offset = -1 * offset;
*/

int mem[ NN ][ 3002 ];

int f( int i, int jmp ) {
  if( i > 30000 )return 0;
  if( jmp <= 0 )return -2;
  int &ret = mem[ i ][ jmp ];
  if( ret != -1 )return ret;
  ret = 0; int r1 = 0, r2 = 0, r3 = 0;
  r1 = f( i + jmp + 1, jmp + 1 );
  r2 = f( i + jmp, jmp );
  r3 = f( i + jmp - 1 , jmp - 1 );
  ret = arr[ i ] + max( r1, max( r2, r3 ) );
  return ret;
}
int f_noMemo( int i, int jmp ) {
  if( i > 30000 )return 0;
  if( jmp <= 0 )return -2;
  //int &ret = mem[ i ][ jmp ];
  //if( ret != -1 )return ret;
  int ret = 0; int r1 = 0, r2 = 0, r3 = 0;

  if( i <= 3000 ) {
     r1 = f( i + jmp + 1, jmp + 1 );
     r2 = f( i + jmp, jmp );
     r3 = f( i + jmp - 1 , jmp - 1 );
  } else {
     r1 = f_noMemo( i + jmp + 1, jmp + 1 );
     r2 = f_noMemo( i + jmp, jmp );
     r3 = f_noMemo( i + jmp - 1 , jmp - 1 );
  }
  ret = arr[ i ] + max( r1, max( r2, r3 ) );
  return ret;
}

void reset() {
    offset = 248;
    CLR( arr ); negCLR( mem );
}

int main(){
  //freopen("in.txt","r",stdin);
  //freopen("outz.txt","w",stdout);
  //scanf("%d",&cases);
  //while( cases-- ){
    scanf("%d %d", &N, &D);
    reset();
    int x;
    for( int i = 1; i <= N; i++ ) { scanf("%d",&x); arr[ x ]++; }
    int res  = 0;
    if( D <= 3000 ) {
        res = f( D, D);
    } else {
        res = f_noMemo( D, D);
    }

    printf("%d\n",res);

  //}
  return 0;
}


#pragma comment(linker, "/STACK:26777216")
#pragma warning(disable:4786)

#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
#define NN 30002
#define MM 248
#define CLR( a ) memset( a , 0 , sizeof ( a ) )
#define negCLR( a ) memset( a , -1 , sizeof( a ) )
#define lld I64d
#define FOR(i,s,t) for(int i = (s); i <= (t); i++)
const lint MOD =  100000007;
const lint LINF = 1000000097999999903LL;
lint boundz = 10000000001;
const int INF = (1 << 29);
int cases, caseno = 0;
int N, D;
int offset = 248; // D = 30000, 1,1+1,1+1+1, do the trick n * (n + 1)/2
//so 248 will split the calculation within memory

int mem[ NN ][ MM + MM ];
int arr[ NN ];

int f( int i, int jmp ) {
    if( i > 30000 ) return 0;
    if( jmp <= 0 ) return -2;
    int &ret = mem[ i ][ jmp + offset ];
    if( ret != -1 )return ret;
    ret = 0; int r1 = 0, r2 = 0, r3 = 0;
    r1 = f( i + jmp + 1, jmp + 1 );
    r2 = f( i + jmp, jmp );
    r3 = f( i + jmp - 1, jmp - 1 );
    ret = arr[ i ] + max(r1,max(r2, r3) );
    return ret;
}

void reset() {
    offset = 248;
    CLR( arr );  CLR( mem );
    //negCLR( mem );
}

int main(){
  //freopen("in.txt","r",stdin);
  //freopen("outz.txt","w",stdout);
  //scanf("%d",&cases);
  //while( cases-- ){
    scanf("%d %d", &N, &D);
    reset();
    offset = D - offset;  // offset + D will always be withi 0 to 248
    offset = -1 * offset;
    int x;
    for( int ii = 1; ii <= N; ii++ ) { scanf("%d",&x); arr[ x ]++; }
    //int res  = f( D, D );
    int res = 0;
    mem[ D ][ D + offset ] = arr[ D ] + 1; // + 1 to check if( mem [ i ][ j + offset ] is ok)

    for(int i = D + 1; i <= 30000 ; i++ ) {
        for( int j = 1; j <= D ; j++ ) {
          if( i -  j < 0 ) break;

          mem[ i ][ j + offset ] = 0;
          if( mem[ i - j ][ j + offset - 1 ] ) mem[ i ][ j + offset ] = mem[ i - j ][ j + offset - 1 ];
          if( mem[ i -  j ][ j + offset ] ) mem[ i ][ j + offset ] = max( mem[ i ][ j + offset ], mem[ i - j ][ j + offset] );
          if( mem[ i - j ][ j + offset + 1 ] ) mem[ i ][ j + offset ] = max( mem[ i ][ j + offset ], mem[ i - j ][ j + offset + 1 ] );
          if( mem[ i ][ j + offset ] ) {
            mem[ i ][ j + offset ] += arr[ i ];
          }
         res = max( res, mem[ i ][ j + offset ] );
        }
    }

    printf("%d\n",res - 1);

  //}
  return 0;
}


#pragma comment(linker, "/STACK:26777216")
#pragma warning(disable:4786)

#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
#define NN 30002
#define MM 248
#define CLR( a ) memset( a , 0 , sizeof ( a ) )
#define negCLR( a ) memset( a , -1 , sizeof( a ) )
#define lld I64d
#define FOR(i,s,t) for(int i = (s); i <= (t); i++)
const lint MOD =  100000007;
const lint LINF = 1000000097999999903LL;
lint boundz = 10000000001;
const int INF = (1 << 29);
int cases, caseno = 0;
int N, D;
int offset = 248; // D = 30000, 1,1+1,1+1+1, do the trick n * (n + 1)/2
//so 248 will split the calculation within memory

int mem[ NN ][ MM + MM + 1 ];
int dp[ NN ][ MM + MM + 1];
int arr[ NN ];

int f( int i, int jmp ) {
    if( i > 30000 ) return 0;
    if( jmp <= 0 ) return -2;
    int &ret = mem[ i ][ jmp + offset ];
    if( ret != -1 )return ret;
    ret = 0; int r1 = 0, r2 = 0, r3 = 0;
    r1 = f( i + jmp + 1, jmp + 1 );
    r2 = f( i + jmp, jmp );
    r3 = f( i + jmp - 1, jmp - 1 );
    ret = arr[ i ] + max(r1,max(r2, r3) );
    return ret;
}

void reset() {
    offset = 248;
    CLR( arr );  CLR( mem ); CLR( dp );
    //negCLR( mem );
}

int main(){
  //freopen("in.txt","r",stdin);
  //freopen("outz.txt","w",stdout);
  //scanf("%d",&cases);
  //while( cases-- ){
    scanf("%d %d", &N, &D);
    reset();
    offset = D - offset;  // offset + D will always be withi 0 to 248
    offset = -1 * offset;
    int x;
    for( int ii = 1; ii <= N; ii++ ) { scanf("%d",&x); arr[ x ]++; }
    //int res  = f( D, D );
    int res = arr[ D ] + 1;
    mem[ D ][ D + offset ] = arr[ D ] + 1;
    dp[ D ][ D + offset ] = arr[ D ] + 1;
    for(int i=D+1;i<=30000;i++)
    {
        //if(i%10==0) printf("%d",i);
        for(int j=1;j<=2*(D+offset);j++)
        {
            if(i-j<0) break;
            dp[i][j + offset]=0;
            if(dp[i-j][j + offset -1]) dp[i][j + offset ]=dp[i-j][j + offset -1];
            if(dp[i-j][j + offset ]) dp[i][j + offset ]=max(dp[i][j + offset ],dp[i-j][j + offset ]);
            if(dp[i-j][j+ offset + 1]) dp[i][j + offset ]=max(dp[i][j + offset ],dp[i-j][j+ offset + 1]);
            if(dp[i][j + offset ]>0) dp[i][j + offset ]+=arr[i];
            res=max(res,dp[i][j + offset ]);
            //printf("%d",dp[i][j]);
        }
    }

    printf("%d\n",res - 1);

  //}
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;

#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define RepP(i,n) for(int i = 1; i <= (n); i++) //Repeat Pascal style
#define RepdP(i, n) for(int i = n; i > 0; i--) //Repeat backward Pascal style
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)
#define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define var(a, b) typeof(b) a(b)
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define last(a) (sz(a) - 1)
#define ms(a,x) memset(a, x, sizeof(a))
#define Exist(a, b) (find(all(a), (b)) != (a).end())
#define Sort(a) sort(all(a))
#define Gsort(a) sort(all(a), greater<typeof(*((a).begin()))>())
#define Unique(a) Sort(a); (a).resize(unique(all(a)) - (a).begin())
#define Enum(a) Fit(it, (a)) cout << *it << " "; cout << endl;
#define el '\n'
#define coud(a,b) cout<<fixed << setprecision((b)) << (a)

template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; }
template<class T> T gcd(T a, T b){ T r; while (b != 0) { r = a % b; a = b; b = r; } return a;}
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> T sqr(T x) { return x * x; }
template<class T> T cube(T x) { return x * x * x; }
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); }

const ld PI = acos(-1.0);
const ld eps = 1e-9;
const int dr[] = {-1, 0, +1, 0};
const int dc[] = {0, +1, 0, -1};
const int inf = (int)1e9 + 5;
const ll linf = (ll)1e16 + 5;
const ll mod = (ll)1e9 + 7;
const int M = 5e6 + 7;
void disp(int arr[], int narr){
    For(i,1,narr) cout << arr[i] << " ";
    cout << el;
}
void disp(vector<int> v){
    int nv = v.size(), nn = min(nv, 15);
    For(i,0,nn - 1) cout << v[i] << " ";
    cout << el;
}
// paste source code========================================================
int n = 30000, m, d, x;
int a[30005], dp[60005][1005], f[1005]; //danh dau hoc dao co qua
// declare ========================================================

// create function========================================================

int sol(){
    //initialize array, vector, .........................
    cin >> m >> d;
    For(i,1,m) cin >> x, a[x]++;
    //solve...............................................
    For(i,1,500) f[i] = i + d - 250;
    Ford(i,n,0){
        For(j,1,500){
            if(f[j] == 1){
                dp[i][j] = max(dp[i + f[j]][j], dp[i + f[j+1]][j+1]);
                dp[i][j] += a[i];
            }
            if(f[j] > 1){
                dp[i][j] = max(dp[i + f[j]][j], dp[i + f[j+1]][j+1]);
                dp[i][j] = max(dp[i][j], dp[i + f[j-1]][j-1]);
                dp[i][j] += a[i];
            }
        }
    }
    //display result......................................
    //cout << f[250] << el;
    cout << a[0] + dp[d][250] << el;

}
int xtest = 1;
int main(){
	#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	#endif
	std::ios::sync_with_stdio(false);
    while(xtest--)  sol();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;

#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define RepP(i,n) for(int i = 1; i <= (n); i++) //Repeat Pascal style
#define RepdP(i, n) for(int i = n; i > 0; i--) //Repeat backward Pascal style
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)
#define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define var(a, b) typeof(b) a(b)
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define last(a) (sz(a) - 1)
#define ms(a,x) memset(a, x, sizeof(a))
#define Exist(a, b) (find(all(a), (b)) != (a).end())
#define Sort(a) sort(all(a))
#define Gsort(a) sort(all(a), greater<typeof(*((a).begin()))>())
#define Unique(a) Sort(a); (a).resize(unique(all(a)) - (a).begin())
#define Enum(a) Fit(it, (a)) cout << *it << " "; cout << endl;
#define el '\n'
#define coud(a,b) cout<<fixed << setprecision((b)) << (a)

template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; }
template<class T> T gcd(T a, T b){ T r; while (b != 0) { r = a % b; a = b; b = r; } return a;}
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> T sqr(T x) { return x * x; }
template<class T> T cube(T x) { return x * x * x; }
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); }

const ld PI = acos(-1.0);
const ld eps = 1e-9;
const int dr[] = {-1, 0, +1, 0};
const int dc[] = {0, +1, 0, -1};
const int inf = (int)1e9 + 5;
const ll linf = (ll)1e16 + 5;
const ll mod = (ll)1e9 + 7;
const int M = 5e6 + 7;
void disp(int arr[], int narr){
    For(i,1,narr) cout << arr[i] << " ";
    cout << el;
}
void disp(vector<int> v){
    int nv = v.size(), nn = min(nv, 15);
    For(i,0,nn - 1) cout << v[i] << " ";
    cout << el;
}
// paste source code========================================================
int n = 30000, m, d, x;
int a[M], dp[50000][1005], f[1005]; //danh dau hoc dao co qua
// declare ========================================================

// create function========================================================

int sol(){
    //initialize array, vector, .........................
    cin >> m >> d;
    For(i,1,m) cin >> x, a[x]++;
    //solve...............................................
    For(i,1,500) f[i] = i + d - 250;
    Ford(i,n,0){
        For(j,1,500){
            if(f[j] == 1){
                dp[i][j] = max(dp[i + f[j]][j], dp[i + f[j+1]][j+1]);
                dp[i][j] += a[i];
            }
            if(f[j] > 1){
                dp[i][j] = max(dp[i + f[j]][j], dp[i + f[j+1]][j+1]);
                dp[i][j] = max(dp[i][j], dp[i + f[j-1]][j-1]);
                dp[i][j] += a[i];
            }
        }
    }
    //display result......................................
    //cout << f[250] << el;
    cout << a[0] + dp[d][250] << el;

}
int xtest = 1;
int main(){
	#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	#endif
	std::ios::sync_with_stdio(false);
    while(xtest--)  sol();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;

#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define RepP(i,n) for(int i = 1; i <= (n); i++) //Repeat Pascal style
#define RepdP(i, n) for(int i = n; i > 0; i--) //Repeat backward Pascal style
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)
#define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define var(a, b) typeof(b) a(b)
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define last(a) (sz(a) - 1)
#define ms(a,x) memset(a, x, sizeof(a))
#define Exist(a, b) (find(all(a), (b)) != (a).end())
#define Sort(a) sort(all(a))
#define Gsort(a) sort(all(a), greater<typeof(*((a).begin()))>())
#define Unique(a) Sort(a); (a).resize(unique(all(a)) - (a).begin())
#define Enum(a) Fit(it, (a)) cout << *it << " "; cout << endl;
#define el '\n'
#define coud(a,b) cout<<fixed << setprecision((b)) << (a)

template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; }
template<class T> T gcd(T a, T b){ T r; while (b != 0) { r = a % b; a = b; b = r; } return a;}
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> T sqr(T x) { return x * x; }
template<class T> T cube(T x) { return x * x * x; }
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); }

const ld PI = acos(-1.0);
const ld eps = 1e-9;
const int dr[] = {-1, 0, +1, 0};
const int dc[] = {0, +1, 0, -1};
const int inf = (int)1e9 + 5;
const ll linf = (ll)1e16 + 5;
const ll mod = (ll)1e9 + 7;
const int M = 5e6 + 7;
void disp(int arr[], int narr){
    For(i,1,narr) cout << arr[i] << " ";
    cout << el;
}
void disp(vector<int> v){
    int nv = v.size(), nn = min(nv, 15);
    For(i,0,nn - 1) cout << v[i] << " ";
    cout << el;
}
// paste source code========================================================
int n = 30000, m, d, x;
int a[1005], dp[60005][1005], f[1005]; //danh dau hoc dao co qua
// declare ========================================================

// create function========================================================

int sol(){
    //initialize array, vector, .........................
    cin >> m >> d;
    For(i,1,m) cin >> x, a[x]++;
    //solve...............................................
    For(i,1,500) f[i] = i + d - 250;
    Ford(i,n,0){
        For(j,1,500){
            if(f[j] == 1){
                dp[i][j] = max(dp[i + f[j]][j], dp[i + f[j+1]][j+1]);
                dp[i][j] += a[i];
            }
            if(f[j] > 1){
                dp[i][j] = max(dp[i + f[j]][j], dp[i + f[j+1]][j+1]);
                dp[i][j] = max(dp[i][j], dp[i + f[j-1]][j-1]);
                dp[i][j] += a[i];
            }
        }
    }
    //display result......................................
    //cout << f[250] << el;
    cout << a[0] + dp[d][250] << el;

}
int xtest = 1;
int main(){
	#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	#endif
	std::ios::sync_with_stdio(false);
    while(xtest--)  sol();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;

#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define RepP(i,n) for(int i = 1; i <= (n); i++) //Repeat Pascal style
#define RepdP(i, n) for(int i = n; i > 0; i--) //Repeat backward Pascal style
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)
#define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define var(a, b) typeof(b) a(b)
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define last(a) (sz(a) - 1)
#define ms(a,x) memset(a, x, sizeof(a))
#define Exist(a, b) (find(all(a), (b)) != (a).end())
#define Sort(a) sort(all(a))
#define Gsort(a) sort(all(a), greater<typeof(*((a).begin()))>())
#define Unique(a) Sort(a); (a).resize(unique(all(a)) - (a).begin())
#define Enum(a) Fit(it, (a)) cout << *it << " "; cout << endl;
#define el '\n'
#define coud(a,b) cout<<fixed << setprecision((b)) << (a)

template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; }
template<class T> T gcd(T a, T b){ T r; while (b != 0) { r = a % b; a = b; b = r; } return a;}
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> T sqr(T x) { return x * x; }
template<class T> T cube(T x) { return x * x * x; }
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); }

const ld PI = acos(-1.0);
const ld eps = 1e-9;
const int dr[] = {-1, 0, +1, 0};
const int dc[] = {0, +1, 0, -1};
const int inf = (int)1e9 + 5;
const ll linf = (ll)1e16 + 5;
const ll mod = (ll)1e9 + 7;
const int M = 5e6 + 7;
void disp(int arr[], int narr){
    For(i,1,narr) cout << arr[i] << " ";
    cout << el;
}
void disp(vector<int> v){
    int nv = v.size(), nn = min(nv, 15);
    For(i,0,nn - 1) cout << v[i] << " ";
    cout << el;
}
// paste source code========================================================
int n = 30000, m, d, x;
int a[M], dp[90000][1005], f[1005]; //danh dau hoc dao co qua
// declare ========================================================

// create function========================================================

int sol(){
    //initialize array, vector, .........................
    cin >> m >> d;
    For(i,1,m) cin >> x, a[x]++;
    //solve...............................................
    For(i,1,500) f[i] = i + d - 250;
    Ford(i,n,0){
        For(j,1,500){
            if(f[j] == 1){
                dp[i][j] = max(dp[i + f[j]][j], dp[i + f[j+1]][j+1]);
                dp[i][j] += a[i];
            }
            if(f[j] > 1){
                dp[i][j] = max(dp[i + f[j]][j], dp[i + f[j+1]][j+1]);
                dp[i][j] = max(dp[i][j], dp[i + f[j-1]][j-1]);
                dp[i][j] += a[i];
            }
        }
    }
    //display result......................................
    //cout << f[250] << el;
    cout << a[0] + dp[d][250] << el;

}
int xtest = 1;
int main(){
	#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	#endif
	std::ios::sync_with_stdio(false);
    while(xtest--)  sol();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;

#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define RepP(i,n) for(int i = 1; i <= (n); i++) //Repeat Pascal style
#define RepdP(i, n) for(int i = n; i > 0; i--) //Repeat backward Pascal style
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)
#define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define var(a, b) typeof(b) a(b)
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define last(a) (sz(a) - 1)
#define ms(a,x) memset(a, x, sizeof(a))
#define Exist(a, b) (find(all(a), (b)) != (a).end())
#define Sort(a) sort(all(a))
#define Gsort(a) sort(all(a), greater<typeof(*((a).begin()))>())
#define Unique(a) Sort(a); (a).resize(unique(all(a)) - (a).begin())
#define Enum(a) Fit(it, (a)) cout << *it << " "; cout << endl;
#define el '\n'
#define coud(a,b) cout<<fixed << setprecision((b)) << (a)

template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; }
template<class T> T gcd(T a, T b){ T r; while (b != 0) { r = a % b; a = b; b = r; } return a;}
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> T sqr(T x) { return x * x; }
template<class T> T cube(T x) { return x * x * x; }
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); }

const ld PI = acos(-1.0);
const ld eps = 1e-9;
const int dr[] = {-1, 0, +1, 0};
const int dc[] = {0, +1, 0, -1};
const int inf = (int)1e9 + 5;
const ll linf = (ll)1e16 + 5;
const ll mod = (ll)1e9 + 7;
const int M = 5e6 + 7;
void disp(int arr[], int narr){
    For(i,1,narr) cout << arr[i] << " ";
    cout << el;
}
void disp(vector<int> v){
    int nv = v.size(), nn = min(nv, 15);
    For(i,0,nn - 1) cout << v[i] << " ";
    cout << el;
}
// paste source code========================================================
int n = 30000, m, d, x;
int a[30005], dp[60505][1005], f[1005]; //danh dau hoc dao co qua
// declare ========================================================

// create function========================================================

int sol(){
    //initialize array, vector, .........................
    cin >> m >> d;
    For(i,1,m) cin >> x, a[x]++;
    //solve...............................................
    For(i,1,500) f[i] = i + d - 250;
    Ford(i,n,0){
        For(j,1,500){
            if(f[j] == 1){
                dp[i][j] = max(dp[i + 1][j], dp[i + 2][j+1]);
                dp[i][j] += a[i];
            }
            if(f[j] > 1){
                dp[i][j] = max(dp[i + f[j]][j], dp[i + f[j+1]][j+1]);
                dp[i][j] = max(dp[i][j], dp[i + f[j-1]][j-1]);
                dp[i][j] += a[i];
            }
        }
    }
    //display result......................................
    //cout << f[250] << el;
    cout << a[0] + dp[d][250] << el;

}
int xtest = 1;
int main(){
	#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	#endif
	std::ios::sync_with_stdio(false);
    while(xtest--)  sol();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;

#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define RepP(i,n) for(int i = 1; i <= (n); i++) //Repeat Pascal style
#define RepdP(i, n) for(int i = n; i > 0; i--) //Repeat backward Pascal style
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)
#define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define var(a, b) typeof(b) a(b)
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define last(a) (sz(a) - 1)
#define ms(a,x) memset(a, x, sizeof(a))
#define Exist(a, b) (find(all(a), (b)) != (a).end())
#define Sort(a) sort(all(a))
#define Gsort(a) sort(all(a), greater<typeof(*((a).begin()))>())
#define Unique(a) Sort(a); (a).resize(unique(all(a)) - (a).begin())
#define Enum(a) Fit(it, (a)) cout << *it << " "; cout << endl;
#define el '\n'
#define coud(a,b) cout<<fixed << setprecision((b)) << (a)

template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; }
template<class T> T gcd(T a, T b){ T r; while (b != 0) { r = a % b; a = b; b = r; } return a;}
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> T sqr(T x) { return x * x; }
template<class T> T cube(T x) { return x * x * x; }
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); }

const ld PI = acos(-1.0);
const ld eps = 1e-9;
const int dr[] = {-1, 0, +1, 0};
const int dc[] = {0, +1, 0, -1};
const int inf = (int)1e9 + 5;
const ll linf = (ll)1e16 + 5;
const ll mod = (ll)1e9 + 7;
const int M = 5e6 + 7;
void disp(int arr[], int narr){
    For(i,1,narr) cout << arr[i] << " ";
    cout << el;
}
void disp(vector<int> v){
    int nv = v.size(), nn = min(nv, 15);
    For(i,0,nn - 1) cout << v[i] << " ";
    cout << el;
}
// paste source code========================================================
int n = 30000, m, d, x;
int a[3005], dp[60005][1005], f[1005]; //danh dau hoc dao co qua
// declare ========================================================

// create function========================================================

int sol(){
    //initialize array, vector, .........................
    cin >> m >> d;
    For(i,1,m) cin >> x, a[x]++;
    //solve...............................................
    For(i,1,500) f[i] = i + d - 250;
    Ford(i,n,0){
        For(j,1,500){
            if(f[j] == 1){
                dp[i][j] = max(dp[i + f[j]][j], dp[i + f[j+1]][j+1]);
                dp[i][j] += a[i];
            }
            if(f[j] > 1){
                dp[i][j] = max(dp[i + f[j]][j], dp[i + f[j+1]][j+1]);
                dp[i][j] = max(dp[i][j], dp[i + f[j-1]][j-1]);
                dp[i][j] += a[i];
            }
        }
    }
    //display result......................................
    //cout << f[250] << el;
    cout << a[0] + dp[d][250] << el;

}
int xtest = 1;
int main(){
	#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	#endif
	std::ios::sync_with_stdio(false);
    while(xtest--)  sol();
    return 0;
}


#include <bits/stdc++.h>
#define F first
#define S second
#define vec vector
#define ms multiset
#define pb push_back
#define acc accumulate
#define pll pair<ll,ll>
#define pq priority_queue
#define pii pair<int, int>
#define pnn pair<Node*, Node*>
#define uid uniform_int_distribution
#define FILE ifstream in("board.in");ofstream out("board.out");
#define fast cin.tie(0);cout.tie(0);cin.sync_with_stdio(0);cout.sync_with_stdio(0);
using namespace std;
typedef string str;
typedef long long ll;
typedef long double ld;
mt19937 rnd(77);

int main() {
    fast;
    int a; cin>>a;
    vec<ll> v(a), pr(a);
    for(int q=0; q<a; q++) cin>>v[q];
    pr[0] = v[0];
    for(int q=1; q<a; q++) pr[q] = pr[q-1]+v[q];
    map<int, vec<pii>> mp;
    for(int q=0; q<a; q++){
        for(int w=q; w<a; w++){
            ll sum = pr[w] - (q==0 ? 0 : pr[q-1]);
            mp[sum].pb({q,w});
        }
    }
    ll o = 0;
    vec<pii> ans;
    for(auto d : mp){
        vec<pii> s = d.S;
        sort(s.begin(), s.end(), [](pii p1, pii p2){return p1.S!=p2.S ? p1.S<p2.S : p1.F<p2.F;});
        vec<pii> n;
        for(int q=1; q<s.size(); q++) {
            if (s[q].S == s[q - 1].S) continue;
            n.pb(s[q-1]);
        }
        n.pb(s[s.size()-1]);
        vec<int> dp(1501), take(1500);
        int mx =0, lt=0;
        for(int q=0; q<n.size(); q++) {
            int l = n[q].F, r = n[q].S;
            if(q>0) dp[r] = dp[n[q-1].S];
            if (1 + (l == 0 ? 0 : dp[l - 1]) > dp[r]) {
                take[q] = 1;
                lt = q;
                dp[r] = (l == 0 ? 0 : dp[l - 1]) + 1;
                mx = max(mx, dp[r]);
            }
            for (int w = r+1; w < (q+1==n.size() ? 1500 : min(1500, n[q+1].S+1)); w++) dp[w] = dp[r];
        }
        vec<int> u;
        int lef = 1e7;
        for(int q=lt; q>=0; q--){
            if(take[q] && n[q].S<lef){
                u.pb(q);
                lef = n[q].F;
            }
        }
        if(mx>o){
            o = mx;
            ans = vec<pii>();
            for(int q=0; q<o; q++) ans.pb(n[u[q]]);
        }
    }
    cout<<o<<endl;
    for(pii i : ans){
        cout<<i.F+1<<" "<<i.S+1<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int mn=2e3+10;
vector<int>* dp[2250001];
vector<ll> ex;
unordered_map<ll,int> comp;
ll a[mn];
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    int n,i,j;
    cin>>n;
    for(i=1;i<=n;i++)cin>>a[i],a[i]+=a[i-1];
    for(i=0;i<=n;i++)for(j=i+1;j<=n;j++)ex.push_back(a[j]-a[i]);
    sort(ex.begin(),ex.end());
    ex.resize(distance(ex.begin(),unique(ex.begin(),ex.end())));
    for(i=0;i<ex.size();i++)comp[ex[i]]=i;
    for(i=1;i<=n;i++){
        for(j=i-1;j>=0;j--){
            int s=comp[a[i]-a[j]];
            if(!dp[s])dp[s]=new stack<int>();
            if(dp[s]->empty()||j>=(*(dp[s]))[dp[s]->size()])dp[s]->push(i);
        }
    }
    ll bes=0;
    int num=0;
    for(i=0;i<ex.size();i++){
        if(dp[i]->size()>num)bes=i,num=dp[i]->size();
    }
    printf("%d\n",num);
    vector<int>s=*(dp[bes]);
    for(int r:s){
        for(int l=r-1;l>=0;l--){
            if(a[r]-a[l]==ex[bes]){
                printf("%d %d\n",l+1,r);
                break;
            }
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int mn=2e3+10;
stack<int>* dp[2250001];
vector<ll> ex;
unordered_map<ll,int> comp;
ll a[mn];
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int mn=2e3+10;
map<ll,stack<int>> dp;
ll a[mn];
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    int n,i,j;
    cin>>n;
    for(i=1;i<=n;i++)cin>>a[i],a[i]+=a[i-1];
    for(i=1;i<=n;i++){
        for(j=i-1;j>=0;j--){
            ll s=a[i]-a[j];
            if(dp[s].empty()||j>=dp[s].top())dp[s].push(i);
        }
    }
    ll bes=0;
    int num=0;
    for(auto th:dp){
        if(th.second.size()>num)bes=th.first,num=th.second.size();
    }
    printf("%d\n",num);
    stack<int>s=dp[bes];
    while(s.size()){
        int r=s.top();
        s.pop();
        for(int l=r-1;l>=0;l--){
            if(a[r]-a[l]==bes){
                printf("%d %d\n",l+1,r);
                break;
            }
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int mn=2e3+10;
map<ll,vector<int>> dp;
ll a[mn];
int top(vector<int>&v){
    return v[v.size()-1];
}
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    int n,i,j;
    cin>>n;
    for(i=1;i<=n;i++)cin>>a[i],a[i]+=a[i-1];
    for(i=1;i<=n;i++){
        for(j=i-1;j>=0;j--){
            ll s=a[i]-a[j];
            if(!dp[s].size||j>=top(dp[s]))dp[s].push_back(i);
        }
    }
    ll bes=0;
    int num=0;
    for(auto th:dp){
        if(th.second.size()>num)bes=th.first,num=th.second.size();
    }
    printf("%d\n",num);
    vector<int>s=dp[bes];
    while(s.size()){
        int r=top(s);
        s.pop_back();
        for(int l=r-1;l>=0;l--){
            if(a[r]-a[l]==bes){
                printf("%d %d\n",l+1,r);
                break;
            }
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int mn=2e3+10;
map<ll,vector<int>> dp;
ll a[mn];
int top(vector<int>&v){
    return v[v.size()-1];
}
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    int n,i,j;
    cin>>n;
    for(i=1;i<=n;i++)cin>>a[i],a[i]+=a[i-1];
    for(i=1;i<=n;i++){
        for(j=i-1;j>=0;j--){
            ll s=a[i]-a[j];
            if(!dp[s].size()||j>=top(dp[s]))dp[s].push_back(i);
        }
    }
    ll bes=0;
    int num=0;
    for(auto th:dp){
        if(th.second.size()>num)bes=th.first,num=th.second.size();
    }
    printf("%d\n",num);
    vector<int>s=dp[bes];
    while(s.size()){
        int r=top(s);
        s.pop_back();
        for(int l=r-1;l>=0;l--){
            if(a[r]-a[l]==bes){
                printf("%d %d\n",l+1,r);
                break;
            }
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int mn=2e3+10;
stack<int> dp[2250001];
vector<ll> ex;
unordered_map<ll,int> comp;
ll a[mn];
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    int n,i,j;
    cin>>n;
    for(i=1;i<=n;i++)cin>>a[i],a[i]+=a[i-1];
    for(i=0;i<=n;i++)for(j=i+1;j<=n;j++)ex.push_back(a[j]-a[i]);
    sort(ex.begin(),ex.end());
    ex.resize(distance(ex.begin(),unique(ex.begin(),ex.end())));
    for(i=0;i<ex.size();i++)comp[ex[i]]=i;
    for(i=1;i<=n;i++){
        for(j=i-1;j>=0;j--){
            int s=comp[a[i]-a[j]];
            if(dp[s].empty()||j>=dp[s].top())dp[s].push(i);
        }
    }
    ll bes=0;
    int num=0;
    for(i=0;i<ex.size();i++){
        if(dp[i].size()>num)bes=i,num=dp[i].size();
    }
    printf("%d\n",num);
    stack<int>s=dp[bes];
    while(s.size()){
        int r=s.top();
        s.pop();
        for(int l=r-1;l>=0;l--){
            if(a[r]-a[l]==ex[bes]){
                printf("%d %d\n",l+1,r);
                break;
            }
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int mn=2e3+10;
vector<int>* dp[2250001];
vector<ll> ex;
unordered_map<ll,int> comp;
ll a[mn];
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    int n,i,j;
    cin>>n;
    for(i=1;i<=n;i++)cin>>a[i],a[i]+=a[i-1];
    for(i=0;i<=n;i++)for(j=i+1;j<=n;j++)ex.push_back(a[j]-a[i]);
    sort(ex.begin(),ex.end());
    ex.resize(distance(ex.begin(),unique(ex.begin(),ex.end())));
    for(i=0;i<ex.size();i++)comp[ex[i]]=i;
    for(i=1;i<=n;i++){
        for(j=i-1;j>=0;j--){
            int s=comp[a[i]-a[j]];
            if(!dp[s])dp[s]=new vector<int>();
            if(dp[s]->empty()||j>=(*(dp[s]))[dp[s]->size()-1])dp[s]->push_back(i);
        }
    }
    ll bes=0;
    int num=0;
    for(i=0;i<ex.size();i++){
        if(dp[i]->size()>num)bes=i,num=dp[i]->size();
    }
    printf("%d\n",num);
    vector<int>s=*(dp[bes]);
    for(int r:s){
        for(int l=r-1;l>=0;l--){
            if(a[r]-a[l]==ex[bes]){
                printf("%d %d\n",l+1,r);
                break;
            }
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int mn=2e3+10;
stack<int>* dp[2250001];
vector<ll> ex;
unordered_map<ll,int> comp;
ll a[mn];
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    int n,i,j;
    cin>>n;
    for(i=1;i<=n;i++)cin>>a[i],a[i]+=a[i-1];
    for(i=0;i<=n;i++)for(j=i+1;j<=n;j++)ex.push_back(a[j]-a[i]);
    sort(ex.begin(),ex.end());
    ex.resize(distance(ex.begin(),unique(ex.begin(),ex.end())));
    for(i=0;i<ex.size();i++)comp[ex[i]]=i;
    for(i=1;i<=n;i++){
        for(j=i-1;j>=0;j--){
            int s=comp[a[i]-a[j]];
            if(!dp[s])dp[s]=new stack<int>();
            if(dp[s]->empty()||j>=dp[s]->top())dp[s]->push(i);
        }
    }
    ll bes=0;
    int num=0;
    for(i=0;i<ex.size();i++){
        if(dp[i]->size()>num)bes=i,num=dp[i]->size();
    }
    printf("%d\n",num);
    stack<int>s=*(dp[bes]);
    while(s.size()){
        int r=s.top();
        s.pop();
        for(int l=r-1;l>=0;l--){
            if(a[r]-a[l]==ex[bes]){
                printf("%d %d\n",l+1,r);
                break;
            }
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int mn=2e3+10;
stack<int> dp[2250001];
vector<ll> ex;
unordered_map<ll> comp;
ll a[mn];
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    int n,i,j;
    cin>>n;
    for(i=1;i<=n;i++)cin>>a[i],a[i]+=a[i-1];
    for(i=0;i<=n;i++)for(j=i+1;j<=n;j++)ex.push_back(a[j]-a[i]);
    sort(ex.begin(),ex.end());
    ex.resize(distance(ex.begin(),unique(ex.begin(),ex.end())));
    for(i=0;i<ex.size();i++)comp[ex[i]]=i;
    for(i=1;i<=n;i++){
        for(j=i-1;j>=0;j--){
            int s=comp[a[i]-a[j]];
            if(dp[s].empty()||j>=dp[s].top())dp[s].push(i);
        }
    }
    ll bes=0;
    int num=0;
    for(i=0;i<ex.size();i++){
        if(dp[i].size()>num)bes=i,num=dp[i].size();
    }
    printf("%d\n",num);
    stack<int>s=dp[bes];
    while(s.size()){
        int r=s.top();
        s.pop();
        for(int l=r-1;l>=0;l--){
            if(a[r]-a[l]==ex[bes]){
                printf("%d %d\n",l+1,r);
                break;
            }
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int mn=2e3+10;
vector<int>* dp[2250001];
vector<ll> ex;
unordered_map<ll,int> comp;
ll a[mn];
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    int n,i,j;
    cin>>n;
    for(i=1;i<=n;i++)cin>>a[i],a[i]+=a[i-1];
    for(i=0;i<=n;i++)for(j=i+1;j<=n;j++)ex.push_back(a[j]-a[i]);
    sort(ex.begin(),ex.end());
    ex.resize(distance(ex.begin(),unique(ex.begin(),ex.end())));
    for(i=0;i<ex.size();i++)comp[ex[i]]=i;
    for(i=1;i<=n;i++){
        for(j=i-1;j>=0;j--){
            int s=comp[a[i]-a[j]];
            if(!dp[s])dp[s]=new vector<int>();
            if(dp[s]->empty()||j>=(*(dp[s]))[dp[s]->size()])dp[s]->push_back(i);
        }
    }
    ll bes=0;
    int num=0;
    for(i=0;i<ex.size();i++){
        if(dp[i]->size()>num)bes=i,num=dp[i]->size();
    }
    printf("%d\n",num);
    vector<int>s=*(dp[bes]);
    for(int r:s){
        for(int l=r-1;l>=0;l--){
            if(a[r]-a[l]==ex[bes]){
                printf("%d %d\n",l+1,r);
                break;
            }
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int mn=2e3+10;
stack<int> dp[225001];
vector<ll> ex;
unordered_map<ll,int> comp;
ll a[mn];
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int mn=2e3+10;
unordered_map<ll,stack<int>> dp;
ll a[mn];
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    int n,i,j;
    cin>>n;
    for(i=1;i<=n;i++)cin>>a[i],a[i]+=a[i-1];
    for(i=1;i<=n;i++){
        for(j=i-1;j>=0;j--){
            ll s=a[i]-a[j];
            if(dp[s].empty()||j>=dp[s].top())dp[s].push(i);
        }
    }
    ll bes=0;
    int num=0;
    for(auto th:dp){
        if(th.second.size()>num)bes=th.first,num=th.second.size();
    }
    printf("%d\n",num);
    stack<int>s=dp[bes];
    while(s.size()){
        int r=s.top();
        s.pop();
        for(int l=r-1;l>=0;l--){
            if(a[r]-a[l]==bes){
                printf("%d %d\n",l+1,r);
                break;
            }
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int mn=2e3+10;
stack<int> dp[2250001];
vector<ll> ex;
unordered_map<ll,int> comp;
ll a[mn];
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int mn=2e3+10;
vector<int>* dp[2250001];
vector<ll> ex;
unordered_map<ll,int> comp;
ll a[mn];
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    int n,i,j;
    cin>>n;
    for(i=1;i<=n;i++)cin>>a[i],a[i]+=a[i-1];
    for(i=0;i<=n;i++)for(j=i+1;j<=n;j++)ex.push_back(a[j]-a[i]);
    sort(ex.begin(),ex.end());
    ex.resize(distance(ex.begin(),unique(ex.begin(),ex.end())));
    for(i=0;i<ex.size();i++)comp[ex[i]]=i;
    for(i=1;i<=n;i++){
        for(j=i-1;j>=0;j--){
            int s=comp[a[i]-a[j]];
            if(!dp[s])dp[s]=new stack<int>();
            if(dp[s]->empty()||j>=(*(dp[s]))[dp[s]->size()])dp[s]->push_back(i);
        }
    }
    ll bes=0;
    int num=0;
    for(i=0;i<ex.size();i++){
        if(dp[i]->size()>num)bes=i,num=dp[i]->size();
    }
    printf("%d\n",num);
    vector<int>s=*(dp[bes]);
    for(int r:s){
        for(int l=r-1;l>=0;l--){
            if(a[r]-a[l]==ex[bes]){
                printf("%d %d\n",l+1,r);
                break;
            }
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 10;
int pre[1551], arr[1551], n, maxs = 0;
set<pair<int, int>> s, ans;
int main()
{
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> arr[i];
		pre[i] = pre[i - 1] + arr[i];
	}
	for (int l = 1; l <= n; ++l)
		for (int i = 0; i + l <= n; ++i) {
			s.clear();
			int temp = pre[i + l] - pre[i], left = 0;
			for (int j = left; j <= n; ++j)
				for (int k = left; k < j; ++k)
					if (pre[j] - pre[k] == temp) {
						s.insert(make_pair(k+1, j));
						left = j;
						break;
					}
			if (s.size() > maxs) {
				maxs = s.size();
				ans.clear();
				for (auto i : s)
					ans.insert(i);
			}
		}
	cout << ans.size() << endl;
	for (auto i : ans)
		cout << i.first << " " << i.second << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
map<LL, vector<pair<LL, LL>>> m;
vector<pair<LL, LL>> ans;
int arr[1551], pre[1551], n, maxs = 0;
bool cmp(const pair<LL, LL> &a, const pair<LL, LL> &b) {
	if (a.first == b.first)
		return a.second < b.second;
	return a.first < b.first;
}
int main()
{
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> arr[i];
		pre[i] = pre[i - 1] + arr[i];
	}
	for (int i = 0; i <= n; ++i)
		for (int j = i + 1; j <= n; ++j)
			m[pre[j] - pre[i]].push_back(make_pair(i + 1, j));
	for (map<LL, vector<pair<LL, LL>>>::iterator it = m.begin(); it != m.end(); ++it) {
		vector<pair<LL, LL>> &temp = it->second, res;
		sort(temp.begin(), temp.end(), cmp);
		int left = 0;
		for (auto i : temp) {
			if (i.first >= left) {
				res.push_back(i);
				left = i.second;
			}
		}
		if (res.size() > maxs) {
			maxs = res.size();
			ans = res;
		}
	}
	cout << maxs << endl;
	for (auto i : ans)
		cout << i.first << " " << i.second << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
map<LL, vector<pair<LL, LL>>> m;
vector<pair<LL, LL>> ans;
LL arr[1551], pre[1551], n, maxs = 0;
int main()
{
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> arr[i];
		pre[i] = pre[i - 1] + arr[i];
	}
	for (int i = 0; i <= n; ++i)
		for (int j = i + 1; j <= n; ++j)
			m[pre[j] - pre[i]].push_back(make_pair(i + 1, j));
	for (map<LL, vector<pair<LL, LL>>>::iterator it = m.begin(); it != m.end(); ++it) {
		vector<pair<LL, LL>> &temp = it->second, res;
		int left = 0, cnt = 0;
		for (auto i : temp) {
			if (i.first > left) {
				res.push_back(i);
				left = i.second;
				++cnt;
			}
		}
		if (cnt > maxs) {
			maxs = res.size();
			ans = res;
		}
	}
	cout << maxs << endl;
	for (auto i : ans)
		cout << i.first << " " << i.second << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
map<LL, vector<pair<LL, LL>>> m;
vector<pair<LL, LL>> ans;
LL arr[1551], pre[1551], n, maxs = 0;
bool cmp(const pair<LL, LL> &a, const pair<LL, LL> &b) {
	if (a.first == b.first)
		return a.second < b.second;
	return a.first < b.first;
}
int main()
{
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> arr[i];
		pre[i] = pre[i - 1] + arr[i];
	}
	for (int i = 0; i <= n; ++i)
		for (int j = i + 1; j <= n; ++j)
			m[pre[j] - pre[i]].push_back(make_pair(i + 1, j));
	for (map<LL, vector<pair<LL, LL>>>::iterator it = m.begin(); it != m.end(); ++it) {
		vector<pair<LL, LL>> &temp = it->second, res;
		sort(temp.begin(), temp.end(), cmp);
		int left = 0;
		for (auto i : temp) {
			if (i.first > left) {
				res.push_back(i);
				left = i.second;
			}
		}
		if (res.size() > maxs) {
			maxs = res.size();
			ans = res;
		}
	}
	cout << maxs << endl;
	for (auto i : ans)
		cout << i.first << " " << i.second << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
map<LL, vector<pair<LL, LL>>> m;
vector<pair<LL, LL>> ans;
LL arr[15510], pre[15510], n, maxs = 0;
bool cmp(const pair<LL, LL> &a, const pair<LL, LL> &b) {
	return a.second < b.second;
}
int main()
{
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> arr[i];
		pre[i] = pre[i - 1] + arr[i];
	}
	for (int i = 0; i <= n; ++i)
		for (int j = i + 1; j <= n; ++j)
			m[pre[j] - pre[i]].push_back(make_pair(i + 1, j));
	for (map<LL, vector<pair<LL, LL>>>::iterator it = m.begin(); it != m.end(); ++it) {
		vector<pair<LL, LL>> &temp = it->second, res;
		sort(temp.begin(), temp.end(), cmp);
		int right = -1;
		for (auto i : temp) {
			if (i.first > right) {
				res.push_back(i);
				right = i.second;
			}
		}
		if (res.size() > maxs) {
			maxs = res.size();
			ans = res;
		}
	}
	cout << maxs << endl;
	for (auto i : ans)
		cout << i.first << " " << i.second << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 10;
int pre[51], arr[51], n, maxs = 0;
set<pair<int, int>> s, ans;
int main()
{
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> arr[i];
		pre[i] = pre[i - 1] + arr[i];
	}
	for (int l = 1; l <= n; ++l)
		for (int i = 0; i + l <= n; ++i) {
			s.clear();
			int temp = pre[i + l] - pre[i], left = 0;
			for (int j = left; j <= n; ++j)
				for (int k = left; k < j; ++k)
					if (pre[j] - pre[k] == temp) {
						s.insert(make_pair(k+1, j));
						left = j;
						break;
					}
			if (s.size() > maxs) {
				maxs = s.size();
				ans.clear();
				for (auto i : s)
					ans.insert(i);
			}
		}
	cout << ans.size() << endl;
	for (auto i : ans)
		cout << i.first << " " << i.second << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
map<LL, vector<pair<LL, LL>>> m;
vector<pair<LL, LL>> ans;
LL arr[15510], pre[15510], n, maxs = 0;
int main()
{
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> arr[i];
		pre[i] = pre[i - 1] + arr[i];
	}
	for (int i = 0; i <= n; ++i)
		for (int j = i + 1; j <= n; ++j)
			m[pre[j] - pre[i]].push_back(make_pair(i + 1, j));
	for (map<LL, vector<pair<LL, LL>>>::iterator it = m.begin(); it != m.end(); ++it) {
		vector<pair<LL, LL>> &temp = it->second, res;
		int left = -1;
		for (auto i : temp) {
			if (i.first > left) {
				res.push_back(i);
				left = i.second;
			}
		}
		if (res.size() > maxs) {
			maxs = res.size();
			ans = res;
		}
	}
	cout << maxs << endl;
	for (auto i : ans)
		cout << i.first << " " << i.second << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
map<LL, vector<pair<LL, LL>>> m;
vector<pair<LL, LL>> ans;
int arr[1551], pre[1551], n, maxs = 0;
bool cmp(const pair<LL, LL> &a, const pair<LL, LL> &b) {
	return a.first < b.first;
}
int main()
{
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> arr[i];
		pre[i] = pre[i - 1] + arr[i];
	}
	for (int i = 0; i <= n; ++i)
		for (int j = i + 1; j <= n; ++j)
			m[pre[j] - pre[i]].push_back(make_pair(i + 1, j));
	for (map<LL, vector<pair<LL, LL>>>::iterator it = m.begin(); it != m.end(); ++it) {
		vector<pair<LL, LL>> &temp = it->second, res;
		sort(temp.begin(), temp.end(),cmp);
		int left = 0;
		for (auto i : temp) {
			if (i.first > left) {
				res.push_back(i);
				left = i.second;
			}
		}
		if (res.size() > maxs) {
			maxs = res.size();
			ans = res;
		}
	}
	cout << maxs << endl;
	for (auto i : ans)
		cout << i.first << " " << i.second << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
map<LL, vector<pair<LL, LL>>> m;
vector<pair<LL, LL>> ans;
int arr[1551], pre[1551], n, maxs = 0;
bool cmp(const pair<LL, LL> &a, const pair<LL, LL> &b) {
	if (a.first == b.first)
		return a.second < b.second;
	return a.first < b.first;
}
int main()
{
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> arr[i];
		pre[i] = pre[i - 1] + arr[i];
	}
	for (int i = 0; i <= n; ++i)
		for (int j = i + 1; j <= n; ++j)
			m[pre[j] - pre[i]].push_back(make_pair(i + 1, j));
	for (map<LL, vector<pair<LL, LL>>>::iterator it = m.begin(); it != m.end(); ++it) {
		vector<pair<LL, LL>> &temp = it->second, res;
		sort(temp.begin(), temp.end(), cmp);
		int left = 0;
		for (auto i : temp) {
			if (i.first > left) {
				res.push_back(i);
				left = i.second;
			}
		}
		if (res.size() > maxs) {
			maxs = res.size();
			ans = res;
		}
	}
	cout << maxs << endl;
	for (auto i : ans)
		cout << i.first << " " << i.second << endl;
}