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

#define ll long long

int a[507];
int sums[200007];

int exists[20000007];
const int M = 10000000;

int main() {

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

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

    int pos = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = i; j <= n; j++) {
            sums[pos] = a[j] - a[i - 1];
            exists[sums[pos] + M]++;
            pos++;
        }
    }

    // assert(pos == (n * (n + 1)) / 2);

    ll cnt = 0;
    for (int j = 0; j < pos; j++) {
        exists[sums[j] + M]--;
        for (int i = 0; i < j; i++) {
            int s = sums[i] + sums[j];
            if (s <= M) {
                cnt += (ll)(exists[M - s]);
            }
        }
    }

    cout << cnt << "\n";

}