Kyoya and Colored Balls------贪心

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?

Input
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.

Output
Print a single integer — the largest possible score.

Examples
Input
3
3 1 5
Output
26

Input
1
10
Output
10

Note
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.

题意

a和b在玩一个游戏。起初,a给b一组n个数字,每次a得到一组数字,他就把所有数字加起来,并把这个和加到分数上。然后他把小组交给b ,每当b得到一个由单个数字组成的组时,他就将这个组抛出。每当b得到一个由多个数字组成的组时,他就把这个组分成两个非空的组(他可以用任何方式来做),并将每个组交给a,问:他们能得到的分数的最大可能值是多少?

解题思路

读完题就能判断出来这题是贪心,既然是求分数最大,问题稍微转化一下,就是尽量让大的数加的次数多,每次都删除最小的数,让剩下的数组成一个集合进入下一轮,自己找了几组数测试一下发现方法可行。书写程序注意数据范围就可,还有数组开的太大应该定义在函数体之外。
代码

#include<iostream>
#include<algorithm>
using namespace std;
const int N = 3e5 + 10;
typedef long long ll;
int a[N];
int main()
{
	ll sum = 0;
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		sum += a[i];
	}
	sort(a, a + n);
	ll ans = sum;
	for (int i = 0; i < n - 1; i++)//n-1轮
	{
		ans += sum;
		sum -= a[i];
	}
	cout << ans << endl;
	return 0;
}

码字不易,留个赞吧
授人一赞,手留余香~

发布了28 篇原创文章 · 获赞 31 · 访问量 3270

猜你喜欢

转载自blog.csdn.net/SDAU_LGX/article/details/104951501