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
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Cộng tất cả (Add All)
Add All
Source: UVa
Version: Phuoc Hung OJ Extended
Problem Statement
Given positive integers, repeatedly replace two current values with , paying . Minimize the sum of costs until one value remains.
Input
The first line contains ; the next line contains exactly positive integers.
Output
Print the minimum total merging cost.
Subtasks
- Subtask 1 (20 points): ; for every .
- Subtask 2 (30 points): ; for every .
- Subtask 3 (50 points): ; for every .
Examples
Input
3
1 2 3
Output
9
Explanation
First merge and at cost , then merge the two remaining values of at cost . Total cost is .