#C8944. Minimum Moves to Equalize Towers

    ID: 52982 Type: Default 1000ms 256MiB

Minimum Moves to Equalize Towers

Minimum Moves to Equalize Towers

You are given several towers with various heights. In a single move, you can increase or decrease the height of a tower by 1. The goal is to make all towers have equal height. Formally, for a set of towers with heights (h_1, h_2, \ldots, h_n), you want to choose an integer (H) that minimizes the total number of moves given by [ M = \sum_{i=1}^{n} \lvert h_i - H \rvert. ] Your task is to determine the minimum number of moves required for each test case.

Note: It is optimal to choose the median of the tower heights as the target height.

inputFormat

The input begins with an integer (T) representing the number of test cases. For each test case, the first line contains an integer (n) denoting the number of towers. The following line contains (n) space-separated integers representing the heights of the towers.

outputFormat

For each test case, output a single line containing the minimum number of moves required to make all the towers have equal height.## sample

5
3
1 2 3
4
3 3 3 5
5
1 2 3 4 5
1
7
2
3 8
2

2 6 0 5

</p>