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

#GD0000098. Sơn các tấm vải (Canvas Painting)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Sơn các tấm vải (Canvas Painting)

Canvas Painting

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

All nn canvases initially have the same color; canvas ii has size sis_i. In one operation a same-colored group is divided into two nonempty groups, each repainted a different fresh color. The amount of paint is the total size of all canvases repainted. Make all canvases have distinct colors using minimum paint.

Input

Line 1 contains nn. Line 2 contains the nn integer canvas sizes.

Output

Print the minimum total amount of paint.

Subtasks

  • Subtask 1 (20 points): 1≤n≤121\le n\le 12; 1≤si≤1051\le s_i\le10^5 for every ii.
  • Subtask 2 (30 points): 1≤n≤15001\le n\le 1500; 1≤si≤1051\le s_i\le10^5 for every ii.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; 1≤si≤1051\le s_i\le10^5 for every ii.

Examples

Input

3
7 4 7

Output

29

Explanation

Merge sizes 44 and 77 at cost 1111, then merge 1111 and 77 at cost 1818. The total paint is 2929.