Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/nollashgan-qism-massiv

Zero-sum subarray

Medium 1500 ms 256 MB 63%
Share

A positive integer $N$ and an array of $N$ integers are given. Find the length of the longest contiguous subarray whose elements sum to $0$ (if there is no such subarray, print $0$):

$$\sum_{i=L}^{R} A_{i} = 0$$

The longest subarray whose elements sum to zero

Input

The first line contains one integer: $N$. The second line contains $N$ integers $A_{i}$ separated by spaces.

Output

One integer — the length of the longest subarray with sum $0$, or $0$.

Note

$1 \le N \le 2 * 10^{5}$
$-10^{9} \le A_{i} \le 10^{9}$

Examples

Input #1
6
1 2 -3 4 -4 5
Output #1
5
Input #2
4
5 -2 3 1
Output #2
0

Sign in to submit a solution

Signing up is free and takes a minute.