当前位置:网站首页>Question e: merged fruit -noip2004tgt2
Question e: merged fruit -noip2004tgt2
2022-07-03 00:08:00 【Wait for dawn forever】
Title Description
In an orchard , Dodo has beaten all the fruit down , And according to different kinds of fruits, they are divided into different heaps . Duoduo decides to combine all the fruits into a pile . Every merger , Duoduo can combine the two piles of fruit , The energy consumed is equal to the sum of the weight of two heaps of fruits . It can be seen that , All the fruits pass by n-1 After the merger , There's only one pile left . Duoduo's total energy consumed during fruit merging is equal to the sum of energy consumed during each merging .
Because I have to work hard to carry these fruits home , Therefore, Duoduo should save energy as much as possible when merging fruits . Suppose that each fruit weighs 1, And we know the number of kinds of fruit and the number of each kind of fruit , Your task is to design a sequence plan for the merger , To minimize the amount of physical effort expended , And output this minimum energy cost .
For example, there are 3 Plant fruit , The order of numbers is 1,2,9. You can put 1、2 Heap merge , The number of new piles is 3, Expend physical energy for 3. next , Merge the new pile with the original third pile , And get a new pile , The number is 12, Expend physical energy for 12.
So a lot of energy =3+12=15. Can prove that 15 For the minimum physical cost .
Input
The input consists of two lines , The first line is an integer n(1<=n<=10000), The number of kinds of fruit .
The second line contains n It's an integer , Separate... With spaces , The first i It's an integer ai(1<=ai<=20000) It's No i The number of fruits planted .
Output
The output includes a line , This line contains only one integer , That's the minimum physical cost . Input data to ensure that the value is less than 2^31.
The sample input
3
1 2 9
Sample output
15
Ideas : Priority queue .
#include <cstdio>
#include <queue>
using namespace std;
// Priority queue representing small top heap
priority_queue<int, vector<int>, greater<int>> q;
int main() {
int n, temp, x, y;
while (scanf("%d", &n) != EOF) {
int sum = 0;
for (int i = 0; i < n; ++i) {
scanf("%d", &temp);
q.push(temp);
}
while (q.size() > 1) {
x = q.top();
q.pop();
y = q.top();
q.pop();
q.push(x + y); // The sum of the two is added to the priority queue as a new number
sum += x + y;
}
q.pop();
printf("%d\n", sum);
}
return 0;
}
边栏推荐
- 130 pages of PPT from the brick boss introduces the new features of Apache spark 3.2 & 3.3 in depth
- 2022 latest and complete interview questions for software testing
- Pytorch里面多任务Loss是加起来还是分别backward?
- collections. What is the purpose of chainmap- What is the purpose of collections. ChainMap?
- Chapter 3 of getting started with MySQL: database creation and operation
- Open source | Wenxin big model Ernie tiny lightweight technology, which is accurate and fast, and the effect is fully open
- Custom throttling function six steps to deal with complex requirements
- Is the multitasking loss in pytoch added up or backward separately?
- MFC 获取当前时间
- 请问大家在什么网站上能查到英文文献?
猜你喜欢

Use redis to realize self increment serial number

直击产业落地!飞桨重磅推出业界首个模型选型工具

Realization of mask recognition based on OpenCV

附加:token;(没写完,别看…)

Intranet penetration | teach you how to conduct intranet penetration hand in hand
![[live broadcast appointment] database obcp certification comprehensive upgrade open class](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[live broadcast appointment] database obcp certification comprehensive upgrade open class

Highly available cluster (HAC)

教育学大佬是怎么找外文参考文献的?

PR FAQ, what about PR preview video card?

Angled detection frame | calibrated depth feature for target detection (with implementation source code)
随机推荐
[shutter] open the third-party shutter project
Angled detection frame | calibrated depth feature for target detection (with implementation source code)
附加:token;(没写完,别看…)
CADD课程学习(4)-- 获取没有晶体结构的蛋白(SWISS-Model)
Open Source | Wenxin Big Model Ernie Tiny Lightweight Technology, Accurate and Fast, full Open Effect
请求与响应
接口自动化覆盖率统计——Jacoco使用
[array] binary search
JDBC教程
67 page overall planning and construction plan for a new smart city (download attached)
SQL query statement parameters are written successfully
The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
Chinatelecom has maintained a strong momentum in the mobile phone user market, but China Mobile has opened a new track
Returns the root node of the largest binary search subtree in a binary tree
Returns the size of the largest binary search subtree in a binary tree
请问大家在什么网站上能查到英文文献?
Mapper agent development
返回二叉树中最大的二叉搜索子树的根节点
35页危化品安全管理平台解决方案2022版
Data set - fault diagnosis: various data and data description of bearings of Western Reserve University