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

using namespace std;

vector<int> inp;
vector<int> seq;

long long int find3Numbers(vector<int> A, int arr_size, int sum=0)
{  
    long long int result = 0;
    for (int i = 0; i < arr_size - 2; i++) {
 
        map<int, long long int> mapa;

        int curr_sum = sum - A[i];

        for (int j = i + 1; j < arr_size; j++) { 
            curr_sum = -A[i] - A[j];
            if(mapa.find(curr_sum) != mapa.end()){
                result += mapa[curr_sum];
            }

           if(mapa.find(A[j]) == mapa.end()){
                mapa[A[j]] = 0;
           }
            
            mapa[A[j]]++;
        }


    }
 
    return result;
}
 

int main() {

    ios_base::sync_with_stdio(0);
    cin.tie(NULL);

    int n;
    cin >> n;

    for(int i = 0; i < n; i++){
        int tmp;
        cin >> tmp;
        inp.push_back(tmp);
    }
    for(int i = 0; i < n; i++) {
        int tmp = 0;
        for(int j = i; j < n; j++){
            tmp += inp[j];
            seq.push_back(tmp);
        }
    }
    sort(seq.begin(), seq.end());
    long long int result = 0;

    for(int i = 0; i < seq.size() - 2; i++) {
        int l = i + 1;
        int r = seq.size() - 1;

        while(l < r){
            if(seq[i] + seq[l] + seq[r] == 0){
                result++;
                r--;
            }

            if(seq[i] + seq[l] + seq[r] < 0){
                l++;
            } 
            else {
                r--;
            }
        }
    }

    cout << find3Numbers(seq, seq.size()) << '\n';

    return 0;
}