electicode
HomeCoursesResourcesProblemsNational OlympiadContestsLeaderboard
...

Array alignment

Time Limit: 1000msMemory Limit: 256MB
View Submissions

Problem Description

Given an integer nnn and an array of nnn integers. You are allowed to perform the following operation any number of times: choose one element of the array and increase it by 1.

Determine the minimum number of such operations required for all elements of the array to become equal.

Input Format

In the first line, an integer nnn is given - the number of elements in the array (1≤n≤105)(1 \le n \le 10^5)(1≤n≤105). In the second line, nnn natural integers are recorded - the elements of the array. The values of the numbers do not exceed 10910^9109.

Output Format

Print the minimum number of such operations needed for all elements of the array to become the same.

Examples

Example 1
Input
2
1 9
Output
8
Example 2
Input
3
20 2 5
Output
33

© 2026 Electicode. All rights reserved.