PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#TP0000049. Xóa hai phần tử (Delete Two Elements)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Xóa hai phần tử (Delete Two Elements)

Delete Two Elements

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Monocarp has an array of nn non-negative integers a1,a2,…,ana_1,a_2,\ldots,a_n. Let kk be the arithmetic mean of all elements:

k=a1+a2+⋯+ann.k=\frac{a_1+a_2+\cdots+a_n}{n}.

The mean kk need not be an integer. Monocarp wants to remove exactly two elements at distinct positions so that the mean of the remaining n−2n-2 elements is still kk.

Count all pairs of indices (i,j)(i,j), 1≤i<j≤n1\le i<j\le n, whose simultaneous removal leaves the arithmetic mean unchanged. Equal values at different positions still represent different removal choices.

Input

  • The first line contains the integer nn, the original array length.
  • The second line contains exactly nn non-negative integers a1,a2,…,ana_1,a_2,\ldots,a_n in positional order.

Output

Print one integer: the number of choices of two distinct positions i<ji<j for which the arithmetic mean remains unchanged after removal.

Subtasks

  • Subtask 1 (20 points): 3≤n≤253\le n\le 25; 0≤ai≤1090\le a_i\le10^9 for every 1≤i≤n1\le i\le n.
  • Subtask 2 (30 points): 3≤n≤25003\le n\le 2500; 0≤ai≤1090\le a_i\le10^9 for every 1≤i≤n1\le i\le n.
  • Subtask 3 (50 points): 3≤n≤2000003\le n\le 200000; 0≤ai≤1090\le a_i\le10^9 for every 1≤i≤n1\le i\le n.

Examples

Input

5
1 4 7 3 5

Output

2

Explanation

The array [1,4,7,3,5][1,4,7,3,5] has sum 2020 and length 55, so its mean is 20/5=420/5=4. After removing two values, the three remaining numbers must sum to 3⋅4=123\cdot4=12. Thus, the removed values must sum to 20−12=820-12=8. The two matching position pairs are (1,3)(1,3), with 1+7=81+7=8, and (4,5)(4,5), with 3+5=83+5=8. The answer is 2.