#P6033. [NOIP 2004 提高组] 合并果子 加强版

    ID: 6798 远端评测题 700ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>贪心2004NOIP 提高组O2优化排序队列

[NOIP 2004 提高组] 合并果子 加强版

Background

Except for [Constraints and Specifications], this problem is exactly the same as P1090.

Problem Description

In an orchard, Duoduo has picked all the fruits and separated them into different piles according to their types. Duoduo decides to merge all the fruits into one pile.

In each merge, Duoduo can merge two piles of fruits into one, and the physical effort consumed equals the sum of the weights of the two piles. It can be seen that after (n1)(n - 1) merges, there will be only one pile left. The total physical effort Duoduo spends while merging fruits equals the sum of the effort consumed in each merge.

Because Duoduo still needs to spend a lot of effort to carry these fruits home, Duoduo wants to save as much effort as possible when merging. Assume each fruit weighs 11. Given the number of fruit types and the number of fruits of each type, your task is to design a merging order so that Duoduo’s total physical effort is minimized, and output this minimum effort value.

For example, there are 33 piles of fruits with counts 1, 2, 91,~2,~9. You can first merge the piles with 11 and 22 fruits, obtaining a new pile of size 33 and consuming 33 effort. Then merge this new pile with the original third pile, obtaining a new pile of size 1212 and consuming 1212 effort. Therefore, Duoduo’s total effort is 3+12=153+12=15. It can be proven that 1515 is the minimum possible effort.

Input Format

The first line contains an integer nn, representing the number of fruit piles.
The second line contains nn integers separated by spaces. The ii-th integer represents the number of fruits in the ii-th pile, aia_i.

Output Format

Output one line containing one integer, representing the minimum physical effort.

3 
1 2 9 

15

Hint

[Constraints and Specifications]

This problem uses bundled multi-testpoint evaluation, with four subtasks in total.

  • Subtask 1 (10 points): 1n81 \leq n \leq 8.
  • Subtask 2 (20 points): 1n1031 \leq n \leq 10^3.
  • Subtask 3 (30 points): 1n1051 \leq n \leq 10^5.
  • Subtask 4 (40 points): 1n1071 \leq n \leq 10^7.

For all testdata, it is guaranteed that 1ai1051 \leq a_i \leq 10^5.

[Hints]

  • Please note the impact of constant factors on program efficiency.
  • Please use variables of suitable types to store the result of this problem.
  • The input size of this problem is large, so please pay attention to input reading efficiency.

Translated by ChatGPT 5