시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 128 MB59414169.492%

문제

Farmer John is herding his N cows (1 <= N <= 2,500) across the expanses of his farm when he finds himself blocked by a river. A single raft is available for transportation.

FJ knows that he must ride on the raft for all crossings and that that adding cows to the raft makes it traverse the river more slowly.

When FJ is on the raft alone, it can cross the river in M minutes (1 <= M <= 1000).  When the i cows are added, it takes M_i minutes (1 <= M_i <= 1000) longer to cross the river than with i-1 cows (i.e., total M+M_1 minutes with one cow, M+M_1+M_2 with two, etc.). Determine the minimum time it takes for Farmer John to get all of the cows across the river (including time returning to get more cows).

입력

  • Line 1: Two space-separated integers: N and M
  • Lines 2..N+1: Line i+1 contains a single integer: M_i

 

출력

  • Line 1: The minimum time it takes for Farmer John to get all of the cows across the river.

예제 입력 1

5 10
3
4
6
100
1

예제 출력 1

50

힌트

There are five cows. Farmer John takes 10 minutes to cross the river alone, 13 with one cow, 17 with two cows, 23 with three, 123 with four, and 124 with all five.

Farmer John can first cross with three cows (23 minutes), then return (10 minutes), and then cross with the last two (17 minutes). 23+10+17 = 50 minutes total.