Pay attention to the non-standard memory limit in this problem.

In order to cut off efficient solutions from inefficient ones in this problem, the time limit is rather strict. Prefer to use compiled statically typed languages (e.g. C++). If you use Python, then submit solutions on PyPy. Try to write an efficient solution.

The array $a=[a_1, a_2, \ldots, a_n]$ ($1 \le a_i \le n$) is given. Its element $a_i$ is called special if there exists a pair of indices $l$ and $r$ ($1 \le l < r \le n$) such that $a_i = a_l + a_{l+1} + \ldots + a_r$. In other words, an element is called special if it can be represented as the sum of two or more consecutive elements of an array (no matter if they are special or not).

Print the number of special elements of the given array $a$.

For example, if $n=9$ and $a=[3,1,4,1,5,9,2,6,5]$, then the answer is $5$:  $a_3=4$ is a special element, since $a_3=4=a_1+a_2=3+1$;  $a_5=5$ is a special element, since $a_5=5=a_2+a_3=1+4$;  $a_6=9$ is a special element, since $a_6=9=a_1+a_2+a_3+a_4=3+1+4+1$;  $a_8=6$ is a special element, since $a_8=6=a_2+a_3+a_4=1+4+1$;  $a_9=5$ is a special element, since $a_9=5=a_2+a_3=1+4$. 

Please note that some of the elements of the array $a$ may be equal — if several elements are equal and special, then all of them should be counted in the answer.


-----Input-----

The first line contains an integer $t$ ($1 \le t \le 1000$) — the number of test cases in the input. Then $t$ test cases follow.

Each test case is given in two lines. The first line contains an integer $n$ ($1 \le n \le 8000$) — the length of the array $a$. The second line contains integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$).

It is guaranteed that the sum of the values of $n$ for all test cases in the input does not exceed $8000$.


-----Output-----

Print $t$ numbers — the number of special elements for each of the given arrays.


-----Example-----
Input
5
9
3 1 4 1 5 9 2 6 5
3
1 1 2
5
1 1 1 1 1
8
8 7 6 5 4 3 2 1
1
1

Output
5
1
0
4
0

Difficulty: introductory

Starter code:

Solutions:
for _ in range(int(input())):
    n = int(input())
    ar = list(map(int, input().split()))
    keke = dict()
    for elem in ar:
        if elem in keke:
            keke[elem] += 1
        else:
            keke[elem] = 1
    ans = 0
    for i in range(n):
        num = ar[i]
        for j in range(i + 1, n):
            num += ar[j]
            if num in keke:
                ans += keke[num]
                keke[num] = 0
    print(ans)

def Zs(): return list(map(int, input().split()))
def Z(): return int(input())

def solve(N, A):
    s = [0] * (N + 1)
    for i in range(N):
        s[i + 1] = s[i] + A[i]
    aru = [False] * (N + 1)
    for i in range(N):
        for j in range(i + 1, N):
            if s[j + 1] - s[i] <= N:
                aru[s[j + 1] - s[i]] = True
    ans = 0
    for i in range(N):
        if aru[A[i]]: ans += 1
    return ans

for _ in range(Z()):
    N = Z()
    A = Zs()
    print(solve(N, A))




import sys


input()
for a in sys.stdin.readlines()[1::2]:
    a = [int(s) for s in a.split()]
    n = len(a)

    can = [False for i in range(n)]
    
    for i in range(1, n):
        s = a[i]
        for j in range(i-1, -1, -1):
            s += a[j]
            if s > n:
                break

            can[s-1] = True

    print(sum([1 for num in a if can[num-1]]))


t = int(input())
for _ in range(t):
    n = int(input())
    s = list(map(int, input().split()))
    p = max(s)
    dict = {}
    for i in range(n):
        count = s[i]
        for j in range(i+1, n):
            count += s[j]
            dict[count] = 1
            if count >= p:
                break
    ans = 0
    for i in range(n):
        if dict.get(s[i]) is not None:
            ans += 1
    print(ans)


t = int(input())
spl = []

for i in range(8001):
	spl.append(0)

for i in range(t):
	n = int(input())
	arr = input().split(' ')

	for j in range(n):
		arr[j] = int(arr[j])

	for j in range(n):
		curr = arr[j]
		for k in range(j+1, n):
			curr += arr[k]
			if (curr > 8000):
				break
			spl[curr] = 1

	ans = 0

	for j in range(n):
		ans += (spl[arr[j]])

	for j in range(8001):
		spl[j] = 0

	print (ans)

T = int(input())
for _ in range(T):
    n = int(input())
    arr = list(map(int, input().split(" ")))
    prefix = []
    prefix.append(arr[0])
    mx = arr[0]
    for i in range(1, n):
        mx = max(mx, arr[i])
        prefix.append(arr[i])
        prefix[i] += prefix[i - 1]

    m = {}
    for i in range(0, n):
        for j in range(i + 1, n):
            if i == 0:
                s = prefix[j]
                if s <= mx:
                    m[s] = 0
            else:
                s = prefix[j] - prefix[i - 1]
                if s <= mx:
                    m[s] = 0
    
    ans = 0
    for i in arr:
        if i in m:
            ans += 1

    print(ans)    

for t in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    special = [0] * (2 * n)
    for i in range(n - 1):
        summ = a[i]
        j = i + 1
        while j < n and summ < n:
            summ += a[j]
            j += 1
            special[summ] = 1
    cnt_spec = 0
    for i in range(n):
        cnt_spec += special[a[i]]
    print(cnt_spec)