PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000146. Nhóm bò có tổng chia hết cho 7 (Subsequences Summing to Sevens)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Nhóm bò có tổng chia hết cho 7 (Subsequences Summing to Sevens)

Subsequences Summing to Sevens

Source: USACO

Version: Phuoc Hung OJ Extended

Problem Statement

Farmer John has nn cows in a row, with distinct integer IDs aia_i. He wants to photograph a consecutive group whose IDs sum to a multiple of 7. Find the largest possible number of cows in such a nonempty group, or 0 if no group qualifies.

Input

  • Line 1: integer nn, the number of cows.
  • The next nn lines each contain one ID; the jj-th of these lines contains aja_j.

Output

Print the largest qualifying group size, or 0 if none exists.

Subtasks

  • Subtask 1 (20 points): 1≤n≤10001\le n\le 1000; 0≤aj≤10000000\le a_j\le 1000000 for every 1≤j≤n1\le j\le n; All IDs are pairwise distinct.
  • Subtask 2 (30 points): 1≤n≤500001\le n\le 50000; 0≤aj≤10000000\le a_j\le 1000000 for every 1≤j≤n1\le j\le n; All IDs are pairwise distinct; Every ID is divisible by 7.
  • Subtask 3 (50 points): 1≤n≤500001\le n\le 50000; 0≤aj≤10000000\le a_j\le 1000000 for every 1≤j≤n1\le j\le n; All IDs are pairwise distinct.

Example 1

Input

7
3
5
1
6
2
14
10

Output

5

Explanation

Positions 2 through 6 sum to 5+1+6+2+14=285+1+6+2+14=28. The two groups of length 6 sum to 31 and 38; the entire row sums to 41. No longer group qualifies.