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
#include<iostream>
#include <vector>
#include <algorithm>
int s[505];
int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);
    int n, a;
    std::vector<int>v;
    std::cin >> n;
    for (int i = 1; i <= n; i++) {
        std::cin >> a;
        s[i] = s[i - 1] + a;
    }
    int p = 1, k = n;
    while (p <= k) {
        for (int i = p; i <= k; i++) {
            v.push_back(s[i] - s[p - 1]);
        }
        p++;
    }
    std::sort(v.begin(), v.end());
    long long w = 0;
    for (int i = 0; i < v.size() - 2; i++) {
        int l = i + 1;
        int r = v.size() - 1;
        while (l < r) {
            if (v[i] + v[l] + v[r] == 0) {
                w++;
                int ll = l + 1;
                while (ll < r && v[ll] == v[l]) {
                    w++;
                    ll++;
                }

                int rr = r - 1;
                while (rr > l && v[rr] == v[r]) {
                    w++;
                    rr--;
                }

                l++;
                r--;
            }
            else if (v[i] + v[l] + v[r] < 0)
                l++;
            else
                r--;
        }
    }
    std::cout << w;
}