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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <cstdio>
#include <vector>
#include <map>

using ll = long long;

const int N = 507;
const int M = 20007;
const int T = N * M;

int n;
int arr[N];
std::map<int, int> counter;
std::vector<std::pair<int, int>> vec;
int tab[2 * T];

int search(int i, int m, int l, int r) {
    while (l < r) {
        int j = (l + r) >> 1;

        if (-(vec[i].first + vec[j].first) > m)
            l = j + 1;
        else
            r = j;
    }

    return l;
}

int main() {
    scanf("%d", &n);

    for (int i = 0; i < n; i++)
        scanf("%d", &arr[i]);

    for (int i = 0; i < n; i++) {
        int s = 0;

        for (int j = i; j < n; j++) {
            s += arr[j];
            counter[s]++;
        }
    }

    for (auto& it: counter) {
        vec.push_back(std::make_pair(it.first, it.second));
        tab[T + it.first] = it.second;
    }

    int m = (--counter.end())->first;
    int sz = vec.size();

    ll r = 0;

    for (int i = 0; i < sz; i++) {
        int j = search(i, m, i + 1, sz - 1);

        for (; j < sz; j++) {
            int s = -(vec[i].first + vec[j].first);

            if (s <= vec[j].first) {
                break;
            } else {
                r += (ll)tab[T + s] * vec[i].second * vec[j].second;
            }
        }
    }

    for (int i = 0; i < sz; i++) {
        int s = -(vec[i].first + vec[i].first);

        if (tab[T + s]) {
            if (s == 0) {
                r += ((ll)tab[T + s] * (tab[T + s] - 1) * (tab[T + s] - 2)) / 6;
            } else {
                r += ((ll)tab[T + s] * vec[i].second * (vec[i].second - 1)) >> 1;
            }
        }
    }

    printf("%lld\n", r);

    return 0;
}