zoukankan      html  css  js  c++  java
  • 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 a1a2, ..., 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;
        }
    }


  • 相关阅读:
    PCIe简介及引脚定义
    PCIE 调试过程记录
    使用Xilinx K7 KC705开发板调试PCIe中的问题【持续更新】
    【再话FPGA】在xilinx中PCIe IP Core使用方法
    浅析PCIe链路LTSSM状态机
    未用管脚设置三态
    Xilinx Vivado的使用详细介绍(1):创建工程、编写代码、行为仿真、Testbench
    Xilinx Vivado的使用详细介绍(2):综合、实现、管脚分配、时钟设置、烧写
    Xilinx Vivado的使用详细介绍(3):使用IP核
    vivado自定IP例化的问题,怎么生成VHDL的例化
  • 原文地址:https://www.cnblogs.com/claireyuancy/p/6705487.html
Copyright © 2011-2022 走看看