Practice on Toph

Participate in exhilarating programming contests, solve unique algorithm and data structure challenges and be a part of an awesome community.

Minions and War

By emrul_mu · Limits 2s, 512 MB

In the land of Minion, there are N Minions. They have their own power to survive themselves from enemy.

The power of every Minion may not always be equal. But, their power must to be equal as you don't want a war among themselves. So, you need to make the every Minion's power equal.

You can increase or decrease any Minion's power by 1 as many time as you want.

But nothing is free here! Increasing a Minion's power by 1 will cost P and decreasing a Minion's power by 1 will cost Q.

You need to find the minimum cost to make every Minion's power equal.


The input will be followed by an integer T, denoting the number of test case.

The first line of each test case will contain three integers N, P and Q.
The second line will contain N integers denoting the power of Minions.


1 ≤ T ≤ 10
1 ≤ N, P, Q ≤ 106
1 ≤ Minion's Power ≤ 106
Sum of N over all the test cases ≤ 8 * 106


For each test case, the only line of the output will contain the minimum cost to make every Minion’s power equal.


3 1 1
1 2 3



64% Solution Ratio

ehsan_sShuvoEarliest, Mar '18

Riaz_BSMRSTUFastest, 0.9s

moinul.shaonLightest, 4.1 MB

IOI_StfuFfsShortest, 665B


Login to submit

Related Contests

Toph uses cookies. By continuing you agree to our Cookie Policy.