#include <bits/stdc++.h>
#define F first
#define S second
#define pp pop_back
#define pb push_back
#define ll long long
#define ld long double
#define llu unsigned long long
#define all(x) (x).begin(), (x).end()
#define FOR(i, j, n) for (int i = j; i < n; i++)
#define FOR1(i, j, n) for (int i = j; i <= n; i++)
#define FOR_K(i, j, k, n) for (int i = j; i < n; i += k)
#define FOR_K1(i, j, k, n) for (int i = j; i <= n; i += k)
using namespace std;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pii, int> ppi;
typedef pair<int, pii> pip;
typedef pair<pll, ll> ppl;
typedef pair<ll, pll> plp;
const int N = 5e3 + 10;
const int M = 1e5 + 10;
int n, a[N];
int dp[N][N], mx[7], mn[M], ans;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n;
FOR1 (i, 1, n)
cin >> a[i];
FOR1 (i, 0, n) {
FOR1 (j, 1, n) mn[a[j]] = 0;
FOR (j, 0, 7) mx[j] = 0;
FOR1 (j, 1, n) {
if (j > i)
dp[i][j] = max(max(mn[a[j] - 1] + 1, mn[a[j] + 1] + 1), max(mx[a[j] % 7] + 1, dp[0][i] + 1)), ans = max(ans, dp[i][j]);
mn[a[j]] = max(mn[a[j]], dp[min(i, j)][max(i, j)]);
mx[a[j] % 7] = max(mx[a[j] % 7], dp[min(i, j)][max(i, j)]);
}
}
cout << ans << endl;
return 0;
}
53A - Autocomplete | 1729G - Cut Substrings |
805B - 3-palindrome | 805C - Find Amir |
676C - Vasya and String | 1042B - Vitamins |
1729F - Kirei and the Linear Function | 25D - Roads not only in Berland |
1694A - Creep | 659F - Polycarp and Hay |
1040A - Palindrome Dance | 372A - Counting Kangaroos is Fun |
1396B - Stoned Game | 16A - Flag |
1056A - Determine Line | 670B - Game of Robots |
1418C - Mortal Kombat Tower | 1382B - Sequential Nim |
1272C - Yet Another Broken Keyboard | 808A - Lucky Year |
1245A - Good ol' Numbers Coloring | 58B - Coins |
1041C - Coffee Break | 507A - Amr and Music |
1041D - Glider | 1486A - Shifting Stacks |
1389B - Array Walk | 71B - Progress Bar |
701A - Cards | 545A - Toy Cars |