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

struct vfs
{
	uint32_t num;
	std::vector<int32_t> v;
};

void load(std::istream &in, vfs &v)
{
	in >> v.num;
	v.v.reserve(v.num);
	for (uint32_t i = 0; i < v.num; ++i)
	{
		int32_t val;
		in >> val;
		v.v.push_back(val);
	}
}

uint32_t very_favourite_len(uint32_t favourite_len)
{
	uint32_t result = 0;
	for (uint32_t i = 1; i <= favourite_len; ++i)
	{
		result += favourite_len - i + 1;
	}
	return result;
}

void m_from_fs(std::vector<int32_t> &fs, std::map<int32_t, uint32_t> &m)
{
	for (auto it = fs.cbegin(); it != fs.cend(); ++it)
	{
		int32_t partial = 0;
		for (auto it2 = it; it2 != fs.cend(); ++it2)
		{
			partial += *it2;
			m[partial] ++;
		}
	}

}

void vfs_from_fs(std::vector<int32_t> &fs, std::vector<int32_t> &vfs, std::map<int32_t, uint32_t> &m)
{
	vfs.reserve(very_favourite_len(fs.size()));
	for (auto it = fs.cbegin(); it != fs.cend(); ++it)
	{
		int32_t partial = 0;
		for (auto it2 = it; it2 != fs.cend(); ++it2)
		{
			partial += *it2;
			vfs.push_back(partial);
			m[partial] ++;
		}
	}

}

void very_favourite(std::istream& in, std::ostream& out)
{
	vfs v;
	load(in, v);
	std::vector<int32_t> vf;
	std::map<int32_t, uint32_t> m;
	vfs_from_fs(v.v, vf, m);
	for (auto it = vf.cbegin(); it != vf.cend(); ++it)
	{
		if (it != vf.cbegin())
		{
			out << " ";
		}
		out << *it;

	}
	out << "\n";
}

uint32_t compute_from_vect(std::vector<int32_t> &vf)
{
	uint32_t result = 0;
	for (auto it = vf.cbegin(); it != vf.cend(); ++it)
	{
		for (auto it2 = it; it2 != vf.cend(); ++it2)
		{
			for (auto it3 = it2; it3 != vf.cend(); ++it3)
			{
				if (it2 != it && it3 != it2)
				{
					auto sum = (*it) + (*it2) + (*it3);
					if (sum == 0)
					{
						++result;
					}
				}
			}
		}
	}
	return result;
}
uint32_t num_triplets(uint32_t len)
{
	uint32_t result = 0;
	uint32_t base = len - 3 + 1;
	for (uint32_t i = 1; i <= base ; ++i)
	{
		result += (base - i + 1) * i;
	}
	return result;
}

uint32_t compute_from_map(std::map<int32_t, uint32_t> &m)
{
	uint32_t result = 0;
	for (auto it = m.cbegin(); it != m.cend(); ++it)
	{
		if (it->first ==0 && it->second >= 3)
		{
			result += num_triplets(it->second);
		}
		for (auto it2 = it; it2 != m.cend(); ++it2)
		{
			for (auto it3 = it2; it3 != m.cend(); ++it3)
			{
				if (it2 != it && it3 != it2)
				{
					auto sum = 
						(it->first) + 
						(it2->first) + 
						(it3->first);					
					if (sum == 0)
					{
						result += 
							it->second*
							it2->second*
							it3->second;
					}
				}
			}
		}
	}
	return result;
}

void prog_main(std::istream& in, std::ostream& out)
{
	vfs v;
	load(in, v);
	std::map<int32_t, uint32_t> m;
	m_from_fs(v.v, m);
	auto result = compute_from_map(m);
	out << result << "\n";
}

#ifndef TEST
int main(int argc, char* argv[])
{
	prog_main(std::cin, std::cout);
	return 0;
}
#endif