Codeforces Round #263 (Div. 2) proC

题目:

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;
    }
}

时间: 2024-12-30 00:43:07

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

Codeforces Round #263 (Div. 1) A B C

Codeforces Round #263 (Div. 1) A:贪心,排个序,然后从后往前扫一遍,计算后缀和,之后在从左往右扫一遍计算答案 B:树形DP,0表示没有1,1表示有1,0遇到0必然合并,0遇到1也必然合并,1遇到0必然合并,1遇到1,必然切断,按照这样去转移即可 C:树状数组,再利用启发式合并,开一个l,r记录当前被子左右下标,和一个flip表示是否翻转 代码: A: #include <cstdio> #include <cstring> #include <

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

题目传送门 1 /* 2 贪心:每次把一个丢掉,选择最小的.累加求和,重复n-1次 3 */ 4 /************************************************ 5 Author :Running_Time 6 Created Time :2015-8-1 13:20:01 7 File Name :A.cpp 8 *************************************************/ 9 10 #include <cstdio>

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

链接: http://codeforces.com/contest/462/problem/D 题意: 给定n个点的树, 0为根,下面n-1行表示每个点的父节点 最后一行n个数 表示每个点的颜色,0为白色,1为黑色. 把树分成若干个联通块使得每个联通块有且仅有一个黑点,问有多少种分法(结果mod1e9+7) 题解: 树形dp,每个点有2个状态,已经归属于某个黑点和未归属于某个黑点. 代码: 31 int n; 32 int x[MAXN]; 33 VI G[MAXN]; 34 ll dp[MAX

Codeforces Round #263 (Div.1) B. Appleman and Tree

题目地址:http://codeforces.com/contest/461/problem/B 题目大意:给一棵树.每一个点为白色或黑色.切断一些边,使得每一个连通块有且仅有一个黑点,问划分方案数. 算法讨论:TreeDP. f[x][0..1]表示x所在连通块有0/1个黑点.设y为x的儿子,则DP方程为f[x][1]=f[x][1]*f[y][0]+f[x][1]*f[y][1]+f[x][0]*f[y][1],f[x][0]=f[x][0]*f[y][0]+f[x][0]*f[y][1].

Codeforces Round #263 (Div. 1) C. Appleman and a Sheet of Paper

题目地址:http://codeforces.com/contest/461/problem/C 题目大意:见原题. 算法分析:启发式暴力,每次把短的纸带折到长的纸带中,在全局记一个rev标记.注意细节. Code: #include <cstdio> #include <algorithm> #define N 100000 using namespace std; bool rev; int n,q,beg=1,end,typ,p,l,r,bit[N+10]; inline v

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

数学家伯利亚在<怎样解题>里说过的解题步骤第二步就是迅速想到与该题有关的原型题.(积累的重要性!) 对于这道题,可以发现其实和huffman算法的思想很相似(可能出题人就是照着改编的).当然最后只是输出cost,就没必要建树什么的了.只要理解了huffman算法构造最优二叉树的思路,就按那么想就知道每个a[i]要加多少次了. 当然这道题没想到这些也可以找出规律的,就是一种贪心思想. #include<iostream> #include<cstdio> #include

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 rectangle with dimensions 1 × n. Your task is help Appleman with folding of such a sheet. Actually, you need to perform q queries. Each query will have

Codeforces Round #263 (Div. 1)

B.Appleman and Tree 题目大意.一棵树上的点有的是黑的有的是白的,然后他想断开一些边使得剩下的连通分量里每个连通分量有且仅有一个黑点,求方案数. dp[u][0]表示以u为根的子树且u所在的连通分量没有黑点的方案数. dp[u][1]表示以u为根的子树且u所在的连通分量有一个黑点的方案数. 更新节点u时,对于他的子树v,可以断开或不断开这条边<u,v>. #include <cstdio> #include <algorithm> #include &

Codeforces Round #263 (Div. 2) proA

题目: A. Appleman and Easy Task time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Toastman came up with a very easy task. He gives it to Appleman, but Appleman doesn't know how to solve it. Can