CSES - Stick Lengths

Xem dạng PDF

Gửi bài giải

Điểm: 1,00 (OI)
Giới hạn thời gian: 1.0s
Giới hạn bộ nhớ: 512M
Input: stdin
Output: stdout

Nguồn bài:
https://cses.fi/problemset/task/1074
Dạng bài
Ngôn ngữ cho phép
C, C++, Java, Kotlin, Pascal, PyPy, Python, Scratch

There are ~n~ sticks with some lengths. Your task is to modify the sticks so that each stick has the same length.

You can either lengthen and shorten each stick. Both operations cost ~x~ where ~x~ is the difference between the new and original length.

What is the minimum total cost?

Input

  • The first input line contains an integer ~n~: the number of sticks.

Then there are ~n~ integers: ~p\_1,p\_2,\ldots,p\_n~: the lengths of the sticks.

Output

  • Print one integer: the minimum total cost.

Constraints

  • ~1 \le n \le 2 \cdot 10^5~
  • ~1 \le p\_i \le 10^9~

Example Input

5
2 3 1 5 2

Example Output

5

Bình luận

Hãy đọc nội quy trước khi bình luận.


Không có bình luận tại thời điểm này.