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 <iostream>
#include <vector>
#include <map>

using namespace std;

int n, m;
vector<int> c1;
vector<int> c2;
map<int, int> ind0;
map<int, int> ind1;
map<int, int> ind2;

void increment(map<int, int>& map, int index) {
    auto it(map.find(index));
    if (it != map.end()) {
        it->second++;
    } else {
        map[index] = 1;
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n;
    int a;
    for(int i = 0; i < n; i++) {
        cin >> a;
        c1.push_back(a);
    }

    for(int i = 0; i < n; i++) {
        int sum = 0;
        for(int j = i; j < n; j++) {
            sum += c1[j];
            c2.push_back(sum);
            m++;
        }
    }

    for(int i = 0; i < m; i++) {
        increment(ind0, c2[i]);
    }

    long long res = 0;
    for(int i = 0; i < m - 2; i++) {
        increment(ind1, c2[i]);
        ind2.clear();
        int first = c2[i];
        for (int j = i + 1; j < m - 1; j++) {
            increment(ind2, c2[j]);
            int last = - (first + c2[j]);
            res += ind0[last] - ind1[last] - ind2[last];
        }
//        if(i % (m/10) == 0) {
//            cout << i << " / " << m << " = " << (1.0*i/m) << endl;
//        }
    }

    cout << res << endl;

    return 0;
}