当前位置:首页|资讯

GFG 43 Minimize the sum

作者:您是打尖儿还是住店呢发布时间:2024-09-30

You are given N elements, you can remove any two elements from the list, note their sum, and add

the sum to the list. Repeat these steps while there is more than a single element in the list. The

task is to minimize the sum of these chosen sums.


Example 1:


Input:

N = 4

arr[] = {1, 4, 7, 10}


Output:

39


Explanation:

Choose 1 and 4, Sum = 1 + 4 = 5.

arr[] = {5, 7, 10} 

Choose 5 and 7, Sum = 5 + (5 + 7) = 17.

arr[] = {12, 10} 

Choose 12 and 10, Sum = 17 + (12 + 10) = 39.

arr[] = {22}


Example 2:


Input:

N = 5

arr[] = {1, 3, 7, 5, 6}


Output:

48


Constraints:


1 <= N, arr[i] <= 10

-----

每次取最小的2个值,直接排序会超时,所以就想到了优先队列也就是最小堆来处理即可。



Copyright © 2024 aigcdaily.cn  北京智识时代科技有限公司  版权所有  京ICP备2023006237号-1