Codeforces 758A Holiday Of Equality Solution & Explanation

Difficulty : 800

Problem Description

In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury.

Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).

You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king’s present. The king can only give money, he hasn’t a power to take away them.

Input

The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom.

The second line contains n integers a1, a2, …, an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen.

Output

In the only line print the integer S — the minimum number of burles which are had to spend.

Examples

Input5
0 1 2 3 4
Output10
Input5
1 1 0 1 1
Output1

Input3
1 3 1
Output4

Input1
12
Output0

Note

In the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.

In the second example it is enough to give one burle to the third citizen.

In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.

In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles.


Solution

We need to find the wealthiest citizen first, then we will use a for loop to calculate how many burles are needed to make all citizens have the same amount.

C# Solution

Solution1 – LINQ Max

string n = Console.ReadLine();
int[] arr = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();

var max = arr.Max();
var res = 0;

for(int i=0;i<arr.Length; i++){
    res+=(max-arr[i]);
}
Console.WriteLine(res);

Or, you can use two for loops to calculate it

Solution1.1 – Two for loops

string n = Console.ReadLine();
int[] arr = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();

int max = 0;
for(int i=0; i<arr.Length; i++){
    max = max>arr[i]?max:arr[i];
}

var res = 0;

for(int j=0;j<arr.Length; j++){
    res+=(max-arr[j]);
}
Console.WriteLine(res);

Java Solution

Solution1

import java.util.Scanner;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        scanner.nextLine();
        int[] arr = Arrays.stream(scanner.nextLine().split(" "))
                               .mapToInt(Integer::parseInt)
                               .toArray();
        
        int max = Arrays.stream(arr).max().orElse(0);
        int res = 0;
        
        for(int i=0;i<arr.length; i++){
            res+=(max-arr[i]);
        }
                
        System.out.println(res);
    }
}
Klook.com

Python3 Solution

Solution1

n = input()
arr = list(map(int, input().split()))

max_val = max(arr)
res = sum(max_val - item for item in arr)

print(res)


Conclusion

Perhaps a ‘Holiday of Equality’ is just what I need to boost my fortune, haha~

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts, thanks you~~

✅If you got any problem about the explanation or you need other programming language solution, please feel free to let me know !!

The problem link : Problem – 758A – Codeforces

Random Codeforces Posts

Leave a Reply

Your email address will not be published. Required fields are marked *