1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    int a[501];
    int b[125250];
    unordered_map<int, int> c;

    scanf("%d", &n);
    a[0] = 0;
    for (int i=1; i<=n; i++)
    {
        scanf("%d", &a[i]);
        a[i] += a[i-1];
    }

    int h=0;
    for (int p1=1; p1<=n; p1++)
    {
        for (int p2=p1; p2<=n; p2++)
        {
            b[h] = a[p2] - a[p1-1];
            h++;
        }
    }

    sort(b, b+h);

    int last_sum = b[h-1] + b[h-2];
    int start = 0;
    while (start < h)
    {
        if (b[start] >= -last_sum)
            break;
        start++;
    }
    int first_sum = b[start] + b[start+1];
    int finish = h-1;
    while (finish > 0)
    {
        if (b[finish] <= -first_sum)
            break;
        finish--;
    }

    long long answer = 0;
    c[b[start]]++;
    for (int j=start+1; j<=finish-1; j++)
    {
        for (int k=j+1; k<=finish; k++)
        {
            answer += c[-(b[j]+b[k])];
            if (b[k]+b[j]+b[start] > 0)
                break;
        }
        c[b[j]]++;
    }

    printf("%lld", answer);

    return 0;
}