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
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    vector<int> a(n);

    unordered_multiset<int> b;
    for (auto& x : a) cin >> x;
    for (int i = 0; i < n; i++) {
        int sum = 0;
        for (int j = i; j < n; j++) {
            sum += a[j];
            b.insert(sum);
        }
    }

    ll res = 0;
    for (auto x_it = b.begin(); x_it != b.end(); x_it++) {
        auto y_it = x_it;
        for (++y_it; y_it != b.end(); y_it++) {
            auto x = *x_it, y = *y_it;
            if (x == y && b.count(x) < 2)
                continue;
            int same = (-x-y == x) + (-x-y == y);
            if ((int) b.count(-x-y) > same)
                res += b.count(-x-y) - same;
        }
    }

    cout << res / 3 << '\n';
    return 0;
}