A. Appleman and Toastman
来源:互联网 发布:网络启动安装系统 编辑:程序博客网 时间:2024/05/16 06:39
Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks:
- Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman.
- Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman.
After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?
The first line contains a single integer n (1 ≤ n ≤ 3·105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial group that is given to Toastman.
Print a single integer — the largest possible score.
33 1 5
26
110
10
Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions.
解题说明:此题是一道贪心题,为了保证最后的得分最大,需要避免在一开始扔掉值较大的数,所以对数组进行从小到大排序。通过例子能够看到第i个位置的值都被计算了i+2词,最后的最大值单独计算即可。
#include<iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<cstdlib>#include<cstring>using namespace std;long long n,a[400000],ans;int main(){int i; cin>>n; for(i=0;i<n;i++) {cin>>a[i];}sort(a,a+n); for(i=0;i<n-1;i++) {ans+=a[i]*(i+2);}ans+=a[n-1]*n; cout<<ans<<endl;return 0;}
- A. Appleman and Toastman
- 461A - Appleman and Toastman
- Codeforces-461A Appleman and Toastman
- Appleman and Toastman - CodeForces 461 A 水题
- Codeforces 461A Appleman and Toastman
- A. Appleman and Toastman(codeforces#263div1)
- Codeforces 461A Appleman and Toastman(贪心)
- CF 461 A Appleman and Toastman
- codeforces 461A Appleman and Toastman
- 【codeforces】 461A Appleman and Toastman
- CodeForces 461A Appleman and Toastman(贪心)
- #263 (div.1) A. Appleman and Toastman
- CodeForces 461A Appleman and Toastman
- CodeForces 461A Appleman and Toastman
- codeforces 461A Appleman and Toastman
- Appleman and Toastman
- Codeforces Round #263 (Div. 1) A. Appleman and Toastman
- 462C - Appleman and Toastman
- 2013长沙站A题||hdu4791 思维题
- POJ-2723-Get Luffy Out
- java专题——归并排序
- Python入门教程 超详细1小时学会Python
- IOS 测试机进行推送
- A. Appleman and Toastman
- java专题——交换排序
- opencv之基于qt ide调试
- MAC OSX快捷键
- java基础——多线程(线程池)
- java专题——选择排序
- AP同频干扰
- 【教程】 如何在linux/mac下安装pascal
- ORCLE-Select 语句执行顺序以及如何提高Oracle 基本查询效率