zoukankan      html  css  js  c++  java
  • Codeforces 263C. Appleman and Toastman

    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.

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

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 using namespace std;
    17 const int maxn = 300100;
    18 LL d[maxn],sum,ans;
    19 int n;
    20 int main() {
    21     while(~scanf("%d",&n)){
    22         for(int i = sum = 0; i < n; i++){
    23             cin>>d[i];
    24             sum += d[i];
    25         }
    26         sort(d,d+n);
    27         ans = sum;
    28         for(int i = 0; i+1 < n; i++){
    29             ans += sum;
    30             sum -= d[i];
    31 
    32         }
    33         cout<<ans<<endl;
    34     }
    35     return 0;
    36 }
    View Code
  • 相关阅读:
    防窜货下加密锁使用常见问题
    SQL Server 2000/2005/2008 触发器的管理和查看
    列表显示时,部分凭证会分两行显示,且不能删除
    JDBC 连接 带实例名的SQL Server
    登录软件提示:读取数据源出现错误,pkcs7填充无效,无法被移除
    完美卸载SQL Server 2008的方案
    彻底卸载(删除)SQL server2000
    NCV5取消:授权数到达,或者许可证过期提示的秘诀
    SQL Server日志清空方法 .
    第二天 一列布局
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3938687.html
Copyright © 2011-2022 走看看