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
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>

using namespace std;

int main() {

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

	int num; cin >> num;
	vector<long long> ulubiony(num);
	for (int i = 0; i < num; i++) {
		cin >> ulubiony[i];
	}

	vector<long long> prefix_sums(num+1, 0);
	for (int i = 1; i <= num; i++) {
		prefix_sums[i] = prefix_sums[i - 1] + ulubiony[i - 1];
	}

	vector<long long> sums;
	unordered_map<long long, long long> sums_map;
	for (int i = 0; i <= num; i++) {
		for (int j = i+1; j <= num; j++) {
			sums.push_back(prefix_sums[j] - prefix_sums[i]);
			sums_map[sums.back()]++;
		}
	}
	long long zeros = sums_map[0];

	vector<pair<long long, long long>> P;
	for (auto& x : sums_map) {
		P.push_back(x);
	}

	sort(P.begin(), P.end());
	long long res = 0;
	for (int i = 0; i < P.size(); i++) {
		int first = i + 1, second = P.size() - 1;
		while (first < second) {
			if (P[first].first + P[second].first > -P[i].first) {
				second--;
			}
			else if (P[first].first + P[second].first < -P[i].first) {
				first++;
			}
			else {
				res += P[i].second * P[first].second * P[second].second;
				first++;
				second--;
			}
		}
	}

	res += 1LL * zeros * (zeros - 1) * (zeros - 2) / 6;

	cout << res << '\n';

	return 0;
}