codeforces-scrape

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

261A.md (2311B)


      1 Source: https://codeforces.com/problemset/problem/261/A
      2 
      3 A. Maxim and Discounts
      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 Maxim always goes to the supermarket on Sundays. Today the supermarket has a special offer of discount systems.
     22 
     23 There are *m* types of discounts. We assume that the discounts are indexed from 1 to *m*. To use the discount number *i*, the customer takes a special basket, where he puts exactly *q**i* items he buys. Under the terms of the discount system, in addition to the items in the cart the customer can receive at most two items from the supermarket for free. The number of the "free items" (0, 1 or 2) to give is selected by the customer. The only condition imposed on the selected "free items" is as follows: each of them mustn't be more expensive than the cheapest item out of the *q**i* items in the cart.
     24 
     25 Maxim now needs to buy *n* items in the shop. Count the minimum sum of money that Maxim needs to buy them, if he use the discount system optimally well.
     26 
     27 Please assume that the supermarket has enough carts for any actions. Maxim can use the same discount multiple times. Of course, Maxim can buy items without any discounts.
     28 
     29 Input
     30 
     31 The first line contains integer *m* (1 ≤ *m* ≤ 105) — the number of discount types. The second line contains *m* integers: *q*1, *q*2, ..., *q**m* (1 ≤ *q**i* ≤ 105).
     32 
     33 The third line contains integer *n* (1 ≤ *n* ≤ 105) — the number of items Maxim needs. The fourth line contains *n* integers: *a*1, *a*2, ..., *a**n* (1 ≤ *a**i* ≤ 104) — the items' prices.
     34 
     35 The numbers in the lines are separated by single spaces.
     36 
     37 Output
     38 
     39 In a single line print a single integer — the answer to the problem.
     40 
     41 Examples
     42 
     43 Input
     44 
     45 ```
     46 1  
     47 2  
     48 4  
     49 50 50 100 100
     50 ```
     51 
     52 Output
     53 
     54 ```
     55 200
     56 ```
     57 
     58 Input
     59 
     60 ```
     61 2  
     62 2 3  
     63 5  
     64 50 50 50 50 50
     65 ```
     66 
     67 Output
     68 
     69 ```
     70 150
     71 ```
     72 
     73 Input
     74 
     75 ```
     76 1  
     77 1  
     78 7  
     79 1 1 1 1 1 1 1
     80 ```
     81 
     82 Output
     83 
     84 ```
     85 3
     86 ```
     87 
     88 Note
     89 
     90 In the first sample Maxim needs to buy two items that cost 100 and get a discount for two free items that cost 50. In that case, Maxim is going to pay 200.
     91 
     92 In the second sample the best strategy for Maxim is to buy 3 items and get 2 items for free using the discount. In that case, Maxim is going to pay 150.