PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GD0000094. Cộng tất cả (Add All)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Cộng tất cả (Add All)

Add All

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

Given nn positive integers, repeatedly replace two current values x,yx,y with x+yx+y, paying x+yx+y. Minimize the sum of costs until one value remains.

Input

The first line contains nn; the next line contains exactly nn positive integers.

Output

Print the minimum total merging cost.

Subtasks

  • Subtask 1 (20 points): 2≤n≤202\le n\le 20; 1≤ai<1051\le a_i<10^5 for every ii.
  • Subtask 2 (30 points): 2≤n≤6002\le n\le 600; 1≤ai<1051\le a_i<10^5 for every ii.
  • Subtask 3 (50 points): 2≤n≤50002\le n\le 5000; 1≤ai<1051\le a_i<10^5 for every ii.

Examples

Input

3
1 2 3

Output

9

Explanation

First merge 11 and 22 at cost 33, then merge the two remaining values of 33 at cost 66. Total cost is 99.