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
#include <bits/stdc++.h>
#define ft first
#define sc second
#define pb push_back
#define FOR(i,n) for(int i=0; i<n; i++)
#define FORX(i,a,b) for(int i=(a); i<=(b); i++)
#define watch(x) cerr << (#x) << " == " << (x) << endl
typedef long long ll;
typedef long double ld;
using namespace std;

const int min_val = 3e7;
int a[505], sum[250'005];

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);

int n;
cin>>n;

a[0] = 0;
FOR(i,n) {
    cin>>a[i+1];
    a[i+1] += a[i];
}

int it = 0;
FORX(i,0,n){
    FORX(j,i+1,n){
        sum[it++] = a[j]-a[i];
    }
}

sort(sum, sum+it);
ll ans = 0;
for (int i = it-1; i >= 2; i--){
    ll l = 0LL, r = i-1LL;
    while (l<r){
        while(l<r && sum[l]+sum[r]+sum[i] > 0) {r--;}    
        while(l<r && sum[l]+sum[r]+sum[i] < 0) {l++;}
        if (l < r && sum[l]+sum[r]+sum[i] == 0) {
            if (sum[l] == sum[r]) {
                ans += (r-l+1LL)*(r-l)/2LL;
                break;
            } else {
                ll l2 = l, r2 = r;
                while (sum[l2] == sum[l2+1]) {l2++;}
                while (sum[r2] == sum[r2-1]) {r2--;}
                ans += (l2-l+1LL)*(r-r2+1LL);
                l = l2+1, r = r2-1;
                continue;
            }
        }     
    }
}

cout << ans;

return 0;
}