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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#pragma GCC optimize("unroll-loops")
#pragma GCC target("popcnt,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
 
#define fwd(i, a, n) for (int i = (a); i < (n); i ++)
#define rep(i, n) fwd(i, 0, n)
#define all(X) begin(X), end(X)
#define sz(X) ((int)X.size())
#define st first
#define nd second
#define pii pair<int, int>
#define vi vector<int>
#define ll long long
 
#ifdef LOCA
auto &operator<<(auto &out, pair<auto, auto> a) {
	return out << "(" << a.st << ", " << a.nd << ")";
}
 
auto &operator<<(auto &out, auto a) {
	out << "{";
	for (auto b : a)
		out << b << ", ";
	return out << "}";
}
 
void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; }
#define debug(x...) cerr << __LINE__ << ": [" #x "]: ", dump(x)
#else
#define debug(...) 0
#endif

struct FT{
    vector<int> s;
    FT(int n):s(n){}
    void update(int pos, int dif){
        for(;pos < sz(s); pos |= pos + 1)s[pos] += dif;
    }
    int query(int pos){
        int res = 0;
        for(; pos > 0; pos &= pos - 1)res += s[pos-1];
        return res;
    }
    void clear(){
        rep(i, sz(s))s[i] = 0;
    }
};

const int K = 3e7;

int32_t main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
    debug(K);
    int n; cin>>n;
    vector<int> v(n+1, 0);
    int minVal = 0;
    int maxVal = 0;
    for(int i = 1; i <= n; i++){
        cin>>v[i];
        v[i] += v[i-1];
        minVal = min(minVal, v[i]);
        maxVal = max(maxVal, v[i]);
    }
    int shift = -3 * minVal;
    int M = 3*(maxVal - minVal) + 1;
    debug(v);
    ll res = 0;
    vector<int> cnt(M, 0);
    vector<short> tab((n+1) * (n+1) * (n+1));
    vector<int> val(n+1);
    rep(i, n+1)val[i] = v[i];
    sort(all(val));
    for(int i= 0; i <= n; i++){
        for(int j = 0; j <= n; j++){
            for(int k = 0; k <= n; k++){
                cnt[v[i] + v[j] + v[k] + shift]++;
            }
        }
    }
    //return 0;
    for(ll i = 0; i < M; i++){
        res -= 1ll*cnt[i] * (cnt[i]-1) / 2ll;
    }
    for(int i = M - 1; i >= 1; i--){
        cnt[i] = cnt[i-1];
    }
    cnt[0] = 0;
    for(int i = 1; i < M; i++){
        cnt[i] += cnt[i-1];
    }
    for(int i =0; i <= n; i++){
        for(int j= n; j >= 0; j--){
            for(int k =0; k <= n; k++){
                tab[cnt[v[i] + v[j] + v[k] + shift]++] = j;
            }
        }
    }
    //return 0;
    debug(tab);
    FT ft(n+1);
    debug(res);
    int last = 0;
    //int info = 0;
    for(int ind = 0; ind < M; ind++){
        //int oldJ = j;
        //info = max(info, cnt[ind] - last);
        for(int i = last; i < cnt[ind]; i++){
            res += ft.query(tab[i]);
            ft.update(tab[i], 3);
        }
        //ft.clear();
        if(cnt[ind] - last > 50){
            ft.clear();
        }else{
            for(int k = last; k < cnt[ind]; k++)ft.update(tab[k], -3);
        }
        last = cnt[ind];
    }
    //return 0;
    //return 0;
    //cout<<info<<"\n";
    debug(res);
    //return 0;
    vector<array<int, 3>> xd;
    for(int i= 0; i <= n; i++){
        for(int j= 0; j <= n; j++){
            xd.push_back({v[i] + v[j], i, j});
        }
    }
    sort(all(xd));
    int j = 0;
    int total = 0;
    for(int i = 1; i < sz(xd); i++){
        if(xd[i][0] != xd[i-1][0]){
            total = 0;
            for(int k = j; k < i; k++){
                res += 3ll * (n+1) * (total - ft.query(xd[k][2] + 1));
                ft.update(xd[k][2], 1);
                total++;
            }
            for(int k = j; k < i; k++){
                ft.update(xd[k][2], -1);
            }
            j = i;
        }
    }
    total = 0;
    for(int k = j; k < sz(xd); k++){
        res += 3ll* (n+1) * (total - ft.query(xd[k][2] + 1));
        ft.update(xd[k][2], 1);
        total++;
    }
    for(int k = j; k < sz(xd); k++)ft.update(xd[k][2], -1);
    debug(res);
    int wow = 1;
    ll y = 0;
    for(int i = 1; i <= n; i++){
        if(val[i] == val[i-1]){
            wow++;
        }else{
            y += wow * (wow-1)/2;
            res += 3ll * (n+1) * (n+1) * (wow) * (wow-1)/2;
            wow = 1;
        }
    }
    y += wow * (wow-1)/2;
    res += 3ll * (n+1) * (n+1) * wow*(wow-1)/2;
    debug(y, res);
    res /= 2;
    debug(res);
    //zasada wlaczen wylaczyc zeby nie liczyc trojek gdzie sie przedzial powtarza
    xd.clear();
    for(int i= 0; i <= n; i++){
        for(int j= 0; j <= n; j++){
            xd.push_back({2*v[i] + v[j], i, -j});
        }
    }
    sort(all(xd));
    debug(xd);
    j = 0;
    for(int i = 1; i < sz(xd); i++){
        if(xd[i][0] != xd[i-1][0]){
            for(int k = j; k < i; k++){
                res -= 3 * ft.query(-xd[k][2]);
                ft.update(-xd[k][2], 1);
            }
            for(int k = j; k < i; k++){
                ft.update(-xd[k][2], -1);
            }
            j = i;
        }
    }
    for(int k = j; k < sz(xd); k++){
        res -= 3 * ft.query(-xd[k][2]);
        ft.update(-xd[k][2], 1);
    }
    for(int k = j; k < j; k++){
        ft.update(-xd[k][2], -1);
    }
    debug(res);
    res += 2*y;
    debug(res);
    res /= 6;
    cout<<res<<"\n";
	return 0;
}