{"raw_statement":[{"iden":"statement","content":"_Author note: I think some of you might remember the problem \"Two Melodies\" from Eductational Codeforces Round 22. Now it's time to make it a bit more difficult!_\n\nAlice is a composer, and recently she had recorded two tracks that became very popular. Now she has got a lot of fans who are waiting for new tracks.\n\nThis time Alice wants to form four melodies for her tracks.\n\nAlice has a sheet with _n_ notes written on it. She wants to take four such non-empty non-intersecting subsequences that all of them form a _melody_ and sum of their lengths is maximal.\n\n_Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements._\n\nSubsequence forms a melody when each two adjacent notes either differ by _1_ or are congruent modulo _7_.\n\nYou should write a program which will calculate maximum sum of lengths of such four non-empty non-intersecting subsequences that all of them form a melody."},{"iden":"input","content":"The first line contains one integer number _n_ (4 ≤ _n_ ≤ 3000).\n\nThe second line contains _n_ integer numbers _a_1, _a_2, ..., _a__n_ (1 ≤ _a__i_ ≤ 105) — notes written on a sheet."},{"iden":"output","content":"Print maximum sum of lengths of such four non-empty non-intersecting subsequences that all of them form a melody."},{"iden":"examples","content":"Input\n\n5\n1 3 5 7 9\n\nOutput\n\n4\n\nInput\n\n5\n1 3 5 7 2\n\nOutput\n\n5"},{"iden":"note","content":"In the first example it is possible to compose 4 melodies by choosing any 4 notes (and each melody will consist of only one note).\n\nIn the second example it is possible to compose one melody with 2 notes — {1, 2}. Remaining notes are used in other three melodies (one note per each melody)."}],"translated_statement":[{"iden":"statement","content":"_作者注：我相信你们中的一些人可能还记得 Educational Codeforces Round 22 中的题目 \"Two Melodies\"。现在是时候让它变得更难一些了！_\n\nAlice 是一位作曲家，最近她录制了两首非常受欢迎的曲目。现在她拥有了大量等待新曲目的粉丝。\n\n这次，Alice 想为她的曲目创作四个旋律。\n\nAlice 的乐谱上写有 #cf_span[n] 个音符。她希望从中选出四个非空且互不相交的子序列，使得每个子序列都构成一个 _旋律_，且它们的长度总和最大。\n\n_子序列是从另一个序列中删除某些元素而不改变剩余元素顺序而得到的序列。_\n\n当一个子序列中每两个相邻的音符要么相差 _1_，要么模 _7_ 同余时，该子序列构成一个 _旋律_。\n\n你需要编写一个程序，计算满足上述条件的四个非空、互不相交子序列的最大长度总和。\n\n第一行包含一个整数 #cf_span[n]（#cf_span[4 ≤ n ≤ 3000]）。\n\n第二行包含 #cf_span[n] 个整数 #cf_span[a1, a2, ..., an]（#cf_span[1 ≤ ai ≤ 105]）——写在乐谱上的音符。\n\n请输出满足条件的四个非空、互不相交子序列的最大长度总和。\n\n在第一个示例中，可以通过选择任意 #cf_span[4] 个音符来组成 #cf_span[4] 个旋律（每个旋律仅包含一个音符）。\n\n在第二个示例中，可以组成一个包含 #cf_span[2] 个音符的旋律 —— #cf_span[{1, 2}]。其余音符分别用于另外三个旋律（每个旋律一个音符）。"},{"iden":"input","content":"第一行包含一个整数 #cf_span[n]（#cf_span[4 ≤ n ≤ 3000]）。第二行包含 #cf_span[n] 个整数 #cf_span[a1, a2, ..., an]（#cf_span[1 ≤ ai ≤ 105]）——写在乐谱上的音符。"},{"iden":"output","content":"请输出满足条件的四个非空、互不相交子序列的最大长度总和。"},{"iden":"examples","content":"输入\n5\n1 3 5 7 9\n输出\n4\n\n输入\n5\n1 3 5 7 2\n输出\n5"},{"iden":"note","content":"在第一个示例中，可以通过选择任意 #cf_span[4] 个音符来组成 #cf_span[4] 个旋律（每个旋律仅包含一个音符）。在第二个示例中，可以组成一个包含 #cf_span[2] 个音符的旋律 —— #cf_span[{1, 2}]。其余音符分别用于另外三个旋律（每个旋律一个音符）。"}],"sample_group":[],"show_order":[],"formal_statement":"**Definitions**  \nLet $ n \\in \\mathbb{Z} $ with $ 4 \\leq n \\leq 3000 $.  \nLet $ A = (a_1, a_2, \\dots, a_n) $ be a sequence of integers with $ 1 \\leq a_i \\leq 10^5 $.  \n\nA subsequence $ S = (s_1, s_2, \\dots, s_k) $ forms a *melody* if for all $ 1 \\leq i < k $,  \n$$\n|s_i - s_{i+1}| = 1 \\quad \\text{or} \\quad s_i \\equiv s_{i+1} \\pmod{7}.\n$$\n\n**Constraints**  \nWe seek four non-empty, pairwise disjoint subsequences $ S_1, S_2, S_3, S_4 $ of $ A $, such that each $ S_j $ forms a melody.\n\n**Objective**  \nMaximize the total length:  \n$$\n\\max \\sum_{j=1}^{4} |S_j|\n$$  \nsubject to the above constraints.","simple_statement":null,"has_page_source":false}