<\/span><\/h2>\n\n\n\nIn 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.<\/p>\n\n\n\n
Totally in Berland there are n<\/em> citizens, the welfare of each of them is estimated as the integer in a<\/em>i<\/em><\/sub> burles (burle is the currency in Berland).<\/p>\n\n\n\nYou 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.<\/p>\n\n\n\n
<\/div>\n\n\n\n
Input<\/strong><\/p>\n\n\n\nThe first line contains the integer n<\/em> (1\u2009\u2264\u2009n<\/em>\u2009\u2264\u2009100) \u2014 the number of citizens in the kingdom.<\/p>\n\n\n\nThe second line contains n<\/em> integers a<\/em>1<\/sub>,\u2009a<\/em>2<\/sub>,\u2009…,\u2009a<\/em>n<\/em><\/sub>, where a<\/em>i<\/em><\/sub> (0\u2009\u2264\u2009a<\/em>i<\/em><\/sub>\u2009\u2264\u2009106<\/sup>) \u2014 the welfare of the i<\/em>-th citizen.<\/p>\n\n\n\n<\/div>\n\n\n\n
Output<\/strong><\/p>\n\n\n\nIn the only line print the integer S<\/em> \u2014 the minimum number of burles which are had to spend.<\/p>\n\n\n\n<\/div>\n\n\n\n
Examples<\/strong><\/p>\n\n\n\nInput<\/td> | 5 0 1 2 3 4<\/td><\/tr> |
Output<\/td> | 10<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<\/div>\n\n\n\n Input<\/td> | 5 1 1 0 1 1<\/td><\/tr> | Output<\/td> | 1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n <\/p>\n\n\n\nInput<\/td> | 3 1 3 1<\/td><\/tr> | Output<\/td> | 4<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n <\/p>\n\n\n\nInput<\/td> | 1 12<\/td><\/tr> | Output<\/td> | 0<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<\/div>\n\n\n\n Note<\/strong><\/p>\n\n\n\nIn 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.<\/p>\n\n\n\n In the second example it is enough to give one burle to the third citizen.<\/p>\n\n\n\n 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.<\/p>\n\n\n\n In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles.<\/p>\n\n\n\n <\/div>\n\n\n\n
\n\n\n\n <\/span>Solution<\/strong><\/span><\/h2>\n\n\n\nWe 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.<\/p>\n\n\n\n <\/div>\n\n\n\n <\/span>C# Solution<\/strong><\/span><\/h3>\n\n\n\nSolution1<\/strong> – LINQ Max<\/p>\n\n\n\nstring n = Console.ReadLine();\nint[] arr = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();\n\nvar max = arr.Max();\nvar res = 0;\n\nfor(int i=0;i<arr.Length; i++){\n res+=(max-arr[i]);\n}\nConsole.WriteLine(res);<\/code><\/pre><\/div>\n\n\n\n<\/div>\n\n\n\n Or, you can use two for loops to calculate it<\/p>\n\n\n\n Solution1.1<\/strong> – Two for loops<\/p>\n\n\n\nstring n = Console.ReadLine();\nint[] arr = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();\n\nint max = 0;\nfor(int i=0; i<arr.Length; i++){\n max = max>arr[i]?max:arr[i];\n}\n\nvar res = 0;\n\nfor(int j=0;j<arr.Length; j++){\n res+=(max-arr[j]);\n}\nConsole.WriteLine(res);<\/code><\/pre><\/div>\n\n\n\n<\/div>\n\n\n\n <\/span>Java Solution<\/strong><\/span><\/h3>\n\n\n\nSolution1<\/strong><\/p>\n\n\n\nimport java.util.Scanner;\nimport java.util.Arrays;\n\npublic class Main {\n public static void main(String[] args) {\n Scanner scanner = new Scanner(System.in);\n scanner.nextLine();\n int[] arr = Arrays.stream(scanner.nextLine().split(" "))\n .mapToInt(Integer::parseInt)\n .toArray();\n \n int max = Arrays.stream(arr).max().orElse(0);\n int res = 0;\n \n for(int i=0;i<arr.length; i++){\n res+=(max-arr[i]);\n }\n \n System.out.println(res);\n }\n}<\/code><\/pre><\/div>\n\n\n\n
|
|
|
|