codeforces-scrape

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

439B.md (2814B)


      1 Source: https://codeforces.com/problemset/problem/439/B
      2 
      3 B. Devu, the Dumb Guy
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 stdin
     16 
     17 output
     18 
     19 stdout
     20 
     21 Devu is a dumb guy, his learning curve is very slow. You are supposed to teach him *n* subjects, the *i**th* subject has *c**i* chapters. When you teach him, you are supposed to teach all the chapters of a subject continuously.
     22 
     23 Let us say that his initial per chapter learning power of a subject is *x* hours. In other words he can learn a chapter of a particular subject in *x* hours.
     24 
     25 Well Devu is not complete dumb, there is a good thing about him too. If you teach him a subject, then time required to teach any chapter of the next subject will require exactly 1 hour less than previously required (see the examples to understand it more clearly). Note that his per chapter learning power can not be less than 1 hour.
     26 
     27 You can teach him the *n* subjects in any possible order. Find out minimum amount of time (in hours) Devu will take to understand all the subjects and you will be free to do some enjoying task rather than teaching a dumb guy.
     28 
     29 Please be careful that answer might not fit in 32 bit data type.
     30 
     31 Input
     32 
     33 The first line will contain two space separated integers *n*, *x* (1 ≤ *n*, *x* ≤ 105). The next line will contain *n* space separated integers: *c*1, *c*2, ..., *c**n* (1 ≤ *c**i* ≤ 105).
     34 
     35 Output
     36 
     37 Output a single integer representing the answer to the problem.
     38 
     39 Examples
     40 
     41 Input
     42 
     43 ```
     44 2 3  
     45 4 1
     46 ```
     47 
     48 Output
     49 
     50 ```
     51 11
     52 ```
     53 
     54 Input
     55 
     56 ```
     57 4 2  
     58 5 1 2 1
     59 ```
     60 
     61 Output
     62 
     63 ```
     64 10
     65 ```
     66 
     67 Input
     68 
     69 ```
     70 3 3  
     71 1 1 1
     72 ```
     73 
     74 Output
     75 
     76 ```
     77 6
     78 ```
     79 
     80 Note
     81 
     82 Look at the first example. Consider the order of subjects: 1, 2. When you teach Devu the first subject, it will take him 3 hours per chapter, so it will take 12 hours to teach first subject. After teaching first subject, his per chapter learning time will be 2 hours. Now teaching him second subject will take 2 × 1 = 2 hours. Hence you will need to spend 12 + 2 = 14 hours.
     83 
     84 Consider the order of subjects: 2, 1. When you teach Devu the second subject, then it will take him 3 hours per chapter, so it will take 3 × 1 = 3 hours to teach the second subject. After teaching the second subject, his per chapter learning time will be 2 hours. Now teaching him the first subject will take 2 × 4 = 8 hours. Hence you will need to spend 11 hours.
     85 
     86 So overall, minimum of both the cases is 11 hours.
     87 
     88 Look at the third example. The order in this example doesn't matter. When you teach Devu the first subject, it will take him 3 hours per chapter. When you teach Devu the second subject, it will take him 2 hours per chapter. When you teach Devu the third subject, it will take him 1 hours per chapter. In total it takes 6 hours.