C. Appleman and Toastman
time limit per test 

2 seconds

memory limit per test 

256 megabytes

input 

standard input

output 

standard output

Appleman and Toastman play a game. Initially Appleman gives one group of nnumbers 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.

Sample test(s)
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.

解题:贪心,当时乱搞了下,居然对了。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
LL d[maxn],sum,ans;
int n;
int main() {
while(~scanf("%d",&n)){
for(int i = sum = ; i < n; i++){
cin>>d[i];
sum += d[i];
}
sort(d,d+n);
ans = sum;
for(int i = ; i+ < n; i++){
ans += sum;
sum -= d[i]; }
cout<<ans<<endl;
}
return ;
}

Codeforces 263C. Appleman and Toastman的更多相关文章

  1. codeforces 462C Appleman and Toastman 解题报告

    题目链接:http://codeforces.com/problemset/problem/461/A 题目意思:给出一群由 n 个数组成的集合你,依次循环执行两种操作: (1)每次Toastman得 ...

  2. 贪心 Codeforces Round #263 (Div. 2) C. Appleman and Toastman

    题目传送门 /* 贪心:每次把一个丢掉,选择最小的.累加求和,重复n-1次 */ /************************************************ Author :R ...

  3. Codeforces 461B Appleman and Tree(木dp)

    题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...

  4. CodeForces 462B Appleman and Card Game(贪心)

    题目链接:http://codeforces.com/problemset/problem/462/B Appleman has n cards. Each card has an uppercase ...

  5. Codeforces461A Appleman and Toastman 贪心

    题目大意是Appleman每次将Toastman给他的Ni个数拆分成两部分后再还给Toastman,若Ni == 1则直接丢弃不拆分.而Toastman将每次获得的Mi个数累加起来作为分数,初始时To ...

  6. Codeforces 263B. Appleman and Card Game

    B. Appleman and Card Game time limit per test  1 second memory limit per test  256 megabytes input  ...

  7. Codeforces 263A. Appleman and Easy Task

    A. Appleman and Easy Task time limit per test  1 second memory limit per test  256 megabytes input  ...

  8. Codeforces 461B. Appleman and Tree[树形DP 方案数]

    B. Appleman and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  9. Codeforces 461B Appleman and Tree

    http://codeforces.com/problemset/problem/461/B 思路:dp,dp[i][0]代表这个联通块没有黑点的方案数,dp[i][1]代表有一个黑点的方案数 转移: ...

随机推荐

  1. 神经网络的结构汇总——tflearn

    一些先进的网络结构: # https://github.com/tflearn/tflearn/blob/master/examples/images/highway_dnn.py # -*- cod ...

  2. git期末总结(转载)

    转自:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137586810169 ...

  3. websocket的原理

    首先明确几点: 1. websocket是一种协议.是html5的一种新协议: 2. 与http的区别是,它是一种双向通信协议,服务器和客户端都能主动向对方发送或接受数据: 3. websocket需 ...

  4. JAVA中list,set,map与数组之间的转换详解

    package test; import java.util.*; /** * Created by ming */ public class Test { public static void ma ...

  5. VS开发C语言系列(零)-VS2013写C语言错误汇总

    错误代码 error C3861:调用函数前未引用 error C4996:调用不安全的函数 error C2668:重载函数不明确 error C3861:"文件名" 找不到标识 ...

  6. 百度之星2017初赛B1006 小小粉丝度度熊

    思路: 考虑到补签卡一定是连续放置才更优,所以直接根据起始位置枚举.预先处理区间之间的gap的前缀和,在枚举过程中二分即可.复杂度O(nlog(n)). 实现: #include <iostre ...

  7. C#语言最基础的数组和集合

    数组的书写格式:数据类型[]变量名=new 数据类型[长度]: 集合的书写格式:List<变量类型>变量名=new List<变量类型>(): 集合添加元素:变量名.Add(数 ...

  8. 使用 RxJava 的正确姿势

    最近在使用 RxJava 时遇到了一些比较诡异的问题,排查的过程中做了点研究,发现平时大家的用法多多少少都有些问题.有些地方存在隐患,有些用法不够简练,我把这些问题简单做一下分类和总结,供大家参考. ...

  9. JS高级——变量提升

    JS执行过程 1.首先是预解析:预解析过程最重要的是提升,在JavaScript代码在预解析阶段,会对以var声明的变量名,和function开头的语句块,进行提升操作 2.执行操作 全局中解析和执行 ...

  10. SQl基本操作——try catch

    begin try ... end try begin catch ... end catch