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

int n;
std::vector<int> a, p, b;
std::unordered_map<int, int> b_counts;

int main() {
    cin >> n;
    a = std::vector<int>(n + 1);
    p = std::vector<int>(n + 1, 0);

    for(int i = 1; i <= n; i++) {
        cin >> a[i];
        p[i] = p[i-1] + a[i];
    }

    for(int i = 0; i <= n; i++) {
        for(int j = i + 1; j <= n; j++) {
            b.push_back(p[j] - p[i]);
        }
    }

    //std::copy(b.begin(), b.end(), std::ostream_iterator<int>(std::cerr, " "));

    ll total = 0;
    for(int j = 1; j < b.size(); j++) {
        b_counts[b[j-1]]++;
        for(int k = j + 1; k < b.size(); k++) {
            total += b_counts[- b[j] - b[k]];
        }
    }
    
    std::cout << total;
}