codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

461A.md (2156B)


      1 Source: https://codeforces.com/problemset/problem/461/A
      2 
      3 A. Appleman and Toastman
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 stdin
     16 
     17 output
     18 
     19 stdout
     20 
     21 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:
     22 
     23 * 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.
     24 * 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.
     25 
     26 After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?
     27 
     28 Input
     29 
     30 The first line contains a single integer *n* (1 ≤ *n* ≤ 3·105). The second line contains *n* integers *a*1, *a*2, ..., *a**n* (1 ≤ *a**i* ≤ 106) — the initial group that is given to Toastman.
     31 
     32 Output
     33 
     34 Print a single integer — the largest possible score.
     35 
     36 Examples
     37 
     38 Input
     39 
     40 ```
     41 3  
     42 3 1 5
     43 ```
     44 
     45 Output
     46 
     47 ```
     48 26
     49 ```
     50 
     51 Input
     52 
     53 ```
     54 1  
     55 10
     56 ```
     57 
     58 Output
     59 
     60 ```
     61 10
     62 ```
     63 
     64 Note
     65 
     66 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.