题目:

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

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 <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream> using namespace std; long long a[300005];
int main()
{
int n;
long long ans;
while(scanf("%d",&n)!=EOF)
{
ans=0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
sort(a+1,a+n+1);
for(int i=1;i<=n;i++)
{
a[i]+=a[i-1];
}
ans=a[n];
for(int i=0;i<=n-2;i++)
{
ans+=(a[n]-a[i]);
}
cout<<ans<<endl;
}
}

Codeforces Round #263 (Div. 2) proC的更多相关文章

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

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

  2. Codeforces Round #263 (Div. 2)

    吐槽:一辈子要在DIV 2混了. A,B,C都是简单题,看AC人数就知道了. A:如果我们定义数组为N*N的话就不用考虑边界了 #include<iostream> #include &l ...

  3. Codeforces Round #263 (Div. 1)

    B 树形dp 组合的思想. Z队长的思路. dp[i][1]表示以i为跟结点的子树向上贡献1个的方案,dp[i][0]表示以i为跟结点的子树向上贡献0个的方案. 如果当前为叶子节点,dp[i][0] ...

  4. Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】

    题意:给了一棵树以及每个节点的颜色,1代表黑,0代表白,求将这棵树拆成k棵树,使得每棵树恰好有一个黑色节点的方法数 解法:树形DP问题.定义: dp[u][0]表示以u为根的子树对父亲的贡献为0 dp ...

  5. Codeforces Round #263 (Div. 2) D. Appleman and Tree(树形DP)

    题目链接 D. Appleman and Tree time limit per test :2 seconds memory limit per test: 256 megabytes input ...

  6. Codeforces Round #263 (Div. 2) A B C

    题目链接 A. Appleman and Easy Task time limit per test:2 secondsmemory limit per test:256 megabytesinput ...

  7. Codeforces Round #263 (Div. 1) C. Appleman and a Sheet of Paper 树状数组暴力更新

    C. Appleman and a Sheet of Paper   Appleman has a very big sheet of paper. This sheet has a form of ...

  8. Codeforces Round #263 (Div. 2)C(贪心,联想到huffman算法)

    数学家伯利亚在<怎样解题>里说过的解题步骤第二步就是迅速想到与该题有关的原型题.(积累的重要性!) 对于这道题,可以发现其实和huffman算法的思想很相似(可能出题人就是照着改编的).当 ...

  9. Codeforces Round #263 (Div. 2) proB

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

随机推荐

  1. spring cloud 报错Error creating bean with name 'hystrixCommandAspect' ,解决方案

    spring cloud 升级到最新版 后,报错: org.springframework.beans.factory.BeanCreationException: Error creating be ...

  2. scala 学习笔记二 方法与函数

    1.介绍 Scala 有方法与函数,二者在语义上的区别很小.Scala 方法是类的一部分,而函数是一个对象可以赋值给一个变量.换句话来说在类中定义的函数即是方法. Scala 中的方法跟 Java 的 ...

  3. Oracle中的字符处理方法

    向左补全字符串 lpad(字段名,填充长度,填充的字符) ,') from dual; 向右补全字符串 rpad(字段名,填充长度,填充的字符) ,') from dual; 返回字符串小写 sele ...

  4. 宏定义偷懒型set,get

    之前看到有这么一个写法 #define DEF_SET_GET(varType,varName,funName) \ private : varType varName; \ public : voi ...

  5. 创建一个pre标签展开折叠的UI组件(原创)

    这些天练习UI组件的编写,顺便模仿一个h5版本的pre标签收缩展开的效果组件: 兼容ie8.9,谷歌,火狐: 图片效果如下: demo.html代码: <!DOCTYPE html> &l ...

  6. 如何使用SubtitleWorkshop制作字幕

    任意打开一段字幕文件 对于初学者而言还是最好打开一个带有中英字幕的视频文件(字幕是嵌入在视频文件里面的)然后一句一句照着写 先打开视频预览模式 再打开一段视频文件,并新建一个字幕文件 牢记几个快捷键 ...

  7. Opera Unite 用户指南

    Opera Unite 用户指南 1 Opera Unite 简介 Opera Unite is a collaborative technology that allows you to share ...

  8. SQL入门教程

    SQL SELECT DISTINCT 语句 在表中,可能会包含重复值.这并不成问题,不过,有时您也许希望仅仅列出不同(distinct)的值. 关键词 DISTINCT 用于返回唯一不同的值. 语法 ...

  9. HTTP basic 认证

    为了确保资源的非法访问,HTTP采用两种认证方式,一种为basic,另外一种为digest(摘要认证).basic是最基本也是最简单的一种认证方式.认证的过程大概可分为以下几个步骤:1. 用户请求受保 ...

  10. [Python]网络爬虫(九):百度贴吧的网络爬虫(v0.4)源码及解析

    转自:http://blog.csdn.net/pleasecallmewhy/article/details/8934726 百度贴吧的爬虫制作和糗百的爬虫制作原理基本相同,都是通过查看源码扣出关键 ...