CodeForces 461A Appleman and Toastman(贪心)
题目大意:
有两个朋友在玩游戏,游戏规则如下:每次朋友A收到一个集合的数字,那么总分便加上这个集合的数字之和。之后朋友A把这个集合的数字给朋友B,朋友B会将这个集合拆分成两个非空的集合,然后再将这两个集合一个一个的返回给A。朋友B如果收到了一个只有一个数的集合,那么便会将这个集合丢掉。问怎样能使的总分最大。
解题思路:
尽量使得大的数加越多次,那么分数就越高,每次都将小的数分出来单独一个集合。
代码:
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 3e5 + 5;
typedef long long ll;
ll num[maxn];
int main () {
int n;
while (scanf ("%d", &n) != EOF) {
for (int i = 0; i < n; i++)
scanf ("%lld", &num[i]);
sort(num, num + n);
if (n == 1)
printf ("%lld\n", num[0]);
else {
ll ans = 0;
ll add = 2;
for (int i = 0; i < n - 1; i++, add++)
ans += add * num[i];
ans += (add - 1) * num[n - 1];
printf("%lld\n", ans);
}
}
return 0;
}