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

using namespace std;

typedef long long lld;

struct triple {
  int x, y, z;
};

int n;

const int MAXN = 500;

int pref[MAXN + 1];

int _tree[MAXN + 2][MAXN + 2];
struct fenwick {
  void update(int x, const int y, const int val = 1) {
    for (; x <= n + 1; x += (x & -x))
      _update(x, y, val);
  }

  void _update(const int x, int y, const int val = 1) {
    for (; y <= n + 1; y += (y & -y))
      _tree[x][y] += val;
  }

  int get(int x, const int y) {
    int cnt = 0;
    for (; x > 0; x -= (x & -x))
      cnt += _get(x, y);
    return cnt;
  }

  int _get(const int x, int y) {
    int cnt = 0;
    for (; y > 0; y -= (y & -y))
      cnt += _tree[x][y];
    return cnt;
  }
} met;

lld get_naive_comparable(const vector<pair<int, int>> &trip) {
  const int t = trip.size();
  int res = 0;
  for (int i = 0; i < t - 1; ++i)
    for (int j = i + 1; j < t; ++j)
      res += (trip[i].first < trip[j].first && trip[i].second < trip[j].second);

  return res;
}

lld get_comparable(const vector<pair<int, int>> &vec) {
  if (vec.size() < 40)
    return get_naive_comparable(vec);

  lld res = 0;
  const int t = vec.size();
  for (int l = 0; l < t;) {
    int r = l + 1;
    while (r < t && vec[r] == vec[l])
      ++r;
    const auto &[x, y] = vec[l];
    res += (r - l) * met.get(x - 1, y - 1);
    met.update(x, y, r - l);
    l = r;
  }

  for (int l = 0; l < t;) {
    int r = l + 1;
    while (r < t && vec[r] == vec[l])
      ++r;
    const auto &[x, y] = vec[l];
    met.update(x, y, l - r);
    l = r;
  }

  return res;
}

// inline int psum(const pair<int, pair<int, int>> &a) {
//   return pref[a.first] + pref[a.second.first - 1] + pref[a.second.second -
//   1];
// }

lld calc_3sum() {
  // chrono::time_point<chrono::system_clock> start, mid, end;
  // start = chrono::system_clock::now();
  // vector<pair<int, pair<int, int>>> tup;
  // for (int i = 0; i <= n; ++i)
  //   for (int j = n; j >= 0; --j)
  //     for (int k = n; k >= 0; --k)
  //       tup.push_back({i, {j + 1, k + 1}});
  // stable_sort(
  //     tup.begin(), tup.end(),
  //     [&](const pair<int, pair<int, int>> &a,
  //         const pair<int, pair<int, int>> &b) { return psum(a) < psum(b); });

  vector<int> tups;
  for (int i = 0; i <= n; ++i)
    for (int j = n; j >= 0; --j)
      for (int k = n; k >= 0; --k)
        tups.push_back(pref[i] + pref[j] + pref[k]);

  int bot = numeric_limits<int>::max(), top = numeric_limits<int>::min();
  for (const int i : tups) {
    bot = min(bot, i);
    top = max(top, i);
  }

  vector<vector<pair<int, int>>> parts(top - bot + 1);
  int idx = 0;
  for (int i = 0; i <= n; ++i)
    for (int j = n; j >= 0; --j)
      for (int k = n; k >= 0; --k)
        parts[tups[idx++] - bot].push_back({j + 1, k + 1});

  // mid = chrono::system_clock::now();

  // cout << "FIRST PART: " << chrono::duration<double>(mid - start).count()
  //      << endl;
  lld res = 0;
  for (const vector<pair<int, int>> &vec : parts)
    if (!vec.empty())
      res += get_comparable(vec);
  // const int t = tup.size();
  // for (int l = 0; l < t;) {
  //   int r = l + 1;
  //   const int s = psum(tup[l]);
  //   while (r < t && psum(tup[r]) == s)
  //     ++r;
  //   vector<pair<int, int>> vec(r - l);
  //   for (int i = l; i < r; ++i)
  //     vec[i - l] = tup[i].second;
  //   res += get_comparable(vec);
  //   l = r;
  // }

  // end = chrono::system_clock::now();
  // cout << "SECOND PART: " << chrono::duration<double>(end - mid).count()
  //      << endl;

  return res;
}

lld calc_2sum() {
  vector<int> sing;
  for (int i = 0; i < n; ++i)
    for (int j = i + 1; j <= n; ++j)
      sing.push_back(pref[j] - pref[i]);
  sort(sing.begin(), sing.end());

  int i = 0, j = sing.size() - 1;
  lld res = 0;
  while (i < sing.size() && j >= 0) {
    if (2 * sing[i] + sing[j] < 0) {
      ++i;
      continue;
    }
    if (2 * sing[i] + sing[j] > 0) {
      --j;
      continue;
    }
    int icnt = 1, jcnt = 1;
    while (i < sing.size() - 1 && sing[i + 1] == sing[i])
      ++i, ++icnt;
    while (j > 0 && sing[j - 1] == sing[j])
      --j, ++jcnt;

    res += (lld)icnt * jcnt;
    ++i, --j;
  }

  return res;
}

int calc_1sum() {
  int res = 0;
  for (int i = 0; i < n; ++i)
    for (int j = i + 1; j <= n; ++j)
      res += (pref[j] == pref[i]);
  return res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

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

  lld s1 = calc_1sum(), s2 = calc_2sum(), s3 = calc_3sum();

  cout << (s3 - 3ll * s2 + 2ll * s1) / 6ll << '\n';
}