영어 원문입니다. 원문의 수식·표를 글자로 옮겨 모양이 다를 수 있습니다. 정확한 내용은 원문 페이지에서 확인하세요. 원문 페이지 열기 (새 창)
시간 제한 1초 · 메모리 제한 512 MB
Problem
You have a stick of length x and you want to divide it into n sticks, with given lengths, whose total length is x.
On each move you can take any stick and divide it into two sticks. The cost of such an operation is the length of the original stick.
What is the minimum cost needed to create the sticks?
Input
The first input line has two integers x and n: the length of the stick and the number of sticks in the division.
The second line has n integers d_1,d_2,…,d_n: the length of each stick in the division.
Output
Print one integer: the minimum cost of the division.
Constraints
- 1 ≤ x ≤ 10^9
- 1 ≤ n ≤ 2 · 10^5
- ∑ d_i = x
Example
Explanation: You first divide the stick of length 8 into sticks of length 3 and 5 (cost 8). After this, you divide the stick of length 5 into sticks of length 2 and 3 (cost 5). The total cost is 8+5=13.
예제
예제 1
8 3 2 3 3
13