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
#include <bits/stdc++.h>
using namespace std;
struct Tpx {
	const int a, b, c;

	bool operator <(const Tpx &that) const {
		if (this->a == that.a) {
			if (this->b == that.b) {
				return this->c < that.c;
			} else {
				return this->b < that.b;
			}
		} else {
			return this->a < that.a;
		}
	}

	Tpx(int a, int b, int c) :
			a(a), b(b), c(c) {
	}

	void print() {
		cout << a << "\t" << b << "\t" << c << endl;
	}
};
void printVector(vector<int> &v) {
	for (size_t i = 0; i < v.size(); i++) {
		cout << v[i] << " ";
	}
	cout << endl;
}

void updateReult(int vali, int valj, int thirdVal, set<Tpx> &result) {
	if (vali != thirdVal && valj != thirdVal) {
		vector<int> v { vali, valj, thirdVal };
		sort(v.begin(), v.end());
		result.insert(Tpx(v[0], v[1], v[2]));
	}
}
int find3Numbers(const vector<int> &seq, set<Tpx> &result) {
	const int S = seq.size();
	int partSum;
	set<int> thirds;
	for (int i = 0; i < S; i++) {
		thirds.insert(seq[i]);
	}
	for (int i = 0; i < S - 1; i++) {
		for (int j = i + 1; j < S; j++) {
			partSum = seq[i] + seq[j];
			if (thirds.count(-partSum) > 0) {
				updateReult(seq[i], seq[j], -partSum, result);
			}
		}
	}

	return result.size();
}

void printMap(map<int, int> &mapData, string title) {
	cout << title << endl;
	for (auto line : mapData) {
		cout << "[" << line.first << "->" << line.second << "], ";
	}
	cout << endl;
}

int main() {
//	ifstream cin("tests/0b.in");

	cin.tie(NULL);
	cout.tie(NULL);
	ios_base::sync_with_stdio(false);

	int n;
	cin >> n;
	vector<int> numbers(n);
	for (int i = 0; i < n; i++) {
		cin >> numbers[i];
	}

	map<int, int> extCountMap;
	for (int i = 0; i < n; i++) {
		int sum = numbers[i];
		extCountMap[sum]++;
		for (int j = i + 1; j < n; j++) {
			sum += numbers[j];
			extCountMap[sum]++;
		}

	}

	vector<int> unique;
	for (auto const &imap : extCountMap) {
		unique.push_back(imap.first);
	}
//	printMap(extCountMap, "extCountMap");
	set<Tpx> result;
	unsigned long long int count = 0;
	find3Numbers(unique, result);

	unsigned long long int repetitions;
	for (auto &triple : result) {
//		triple.print();
		repetitions = 1;
		repetitions *= extCountMap[triple.a];
		repetitions *= extCountMap[triple.b];
		repetitions *= extCountMap[triple.c];
		count += repetitions;
//		cout << "repetitions=" << repetitions << endl;
	}
//	printVector(unique);
	// pairs and zeros
	int nzrs;
	for (auto &item : extCountMap) {
		if (item.first == 0) {
			const int zsx = item.second;
			if (zsx > 2) {
				repetitions = (zsx - 2) * (zsx - 1) / 2 * zsx / 3;
				count += repetitions;
			}
		} else {
			nzrs = item.second;
			if (nzrs >= 2) {
				const int key = item.first * 2;
				if (extCountMap.count(-key) > 0) {
					repetitions = (nzrs - 1) * nzrs / 2;
					repetitions *= extCountMap[-key];
					count += repetitions;
				}
			}
		}
	}

	cout << count << endl;
}