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
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Author   : Jakub Rożek
// Task     : Bardzo ulubiony ciąg
// Contest  : PA 2024 r5 C
// Memory   : O(n^3)
// Time     : O(n^3)
// Solution : Rozwiązanie poeinno dzialac

#include "bits/stdc++.h"
using namespace std;
using LL = long long;
template <typename T>
using P = pair<T, T>;
template <typename T>
using VV = vector<vector<T>>;
#define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
#define FORD(i,a,b) for(int i=(a); i>=(b); --i)
#define REP(i,n) for(int i=0; i<(n); ++i)
#define all(x) x.begin(), x.end()
#define ssize(x) int((x).size())
#ifdef DEBUG
template <typename T1, typename T2>
auto&operator<<(auto&o,pair<T1,T2>p){return o<<'('<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";for(auto e:x)o<<","<<e;return o<<"}";}
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(x)
#else
#define debug(...) {}
#endif

// const LL INF = 1'000'000'000'000'000'018;
// const int INF = 1'000'000'009;
// const LL mod = 1'000'000'007;
const int N = 500;

struct trzy {
    int a,b,c;
};

int n, x, y, z, w;
LL odp, sum;
LL sum_pref[N+1];
int dp[N+1][N+1][N+1];
unordered_map <LL, LL> ile;
unordered_map <LL, vector<trzy>> zbior;

void skaluj(auto &v) {
    map <int, int> skala;
    set <int> wartosci;

    skala.clear();
    wartosci.clear();
    for (auto i : v) wartosci.insert(i.a);
    x = 0;
    for (auto i : wartosci) {
        ++x;
        skala[i] = x;
    }
    for (auto &i : v) i.a = skala[i.a];

    skala.clear();
    wartosci.clear();
    for (auto i : v) wartosci.insert(i.b);
    y = 0;
    for (auto i : wartosci) {
        ++y;
        skala[i] = y;
    }
    for (auto &i : v) i.b = skala[i.b];

    skala.clear();
    wartosci.clear();
    for (auto i : v) wartosci.insert(i.c);
    z = 0;
    for (auto i : wartosci) {
        ++z;
        skala[i] = z;
    }
    for (auto &i : v) i.c = skala[i.c];
}

void solution() {
    cin >> n;
    FOR (i, 1, n) {
        cin >> sum_pref[i];
        sum_pref[i] += sum_pref[i-1];
    }
    
    FOR (a, 0, n) {
        FOR (b, 0, n) {
            FOR (c, 0, n) {
                zbior[sum_pref[a]+sum_pref[b]+sum_pref[c]].push_back({a,b,c});
            }
        }
    }

    // dodaje wszystkie 3
    for (auto [_, v] : zbior) {
        skaluj(v);

        FOR (a, 0, x) {
            FOR (b, 0, y) {
                FOR (c, 0, z) {
                    dp[a][b][c] = 0;
                }
            }
        }

        for (auto i : v) {
            dp[i.a][i.b][i.c] += 1;
        }

        FOR (a, 1, x) {
            FOR (b, 1, y) {
                FOR (c, 1, z) {
                    if (dp[a][b][c]) odp += dp[a-1][b-1][c-1];

                    dp[a][b][c] += dp[a-1][b][c];
                    dp[a][b][c] += dp[a][b-1][c];
                    dp[a][b][c] += dp[a][b][c-1];
                    dp[a][b][c] -= dp[a-1][b-1][c];
                    dp[a][b][c] -= dp[a][b-1][c-1];
                    dp[a][b][c] -= dp[a-1][b][c-1];
                    dp[a][b][c] += dp[a-1][b-1][c-1];
                }
            }
        }
    }

    // odejmuje 2 takie same
    FOR (a, 0, n-1) {
        FOR (b, a+1, n) {
            ile[sum_pref[b]-sum_pref[a]]++;
        }
    }
    x = 0;
    FOR (a, 0, n-1) {
        FOR (b, a+1, n) {
            ++x;
            odp -= 3 * ile[-(sum_pref[b]-sum_pref[a])*2];
        }
    }
    // dodaje spowrotem 3 takie same
    FOR (a, 0, n-1) {
        FOR (b, a+1, n) {
            if (sum_pref[b]-sum_pref[a] == 0) odp += 2;
        }
    }

    cout << odp / 6 << '\n';
    return;
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int tests = 1;
    // cin>>tests;
    FOR (i, 1, tests) {
        solution();
    }
    return 0;
}