codeforces-scrape

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

1539D.md (2631B)


      1 Source: https://codeforces.com/problemset/problem/1539/D
      2 
      3 D. PriceFixed
      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 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store  — "PriceFixed". Here are some rules of that store:
     22 
     23 * The store has an infinite number of items of every product.
     24 * All products have the same price: $2$ rubles per item.
     25 * For every product $i$ there is a discount for experienced buyers: if you buy $b_i$ items of products (of any type, not necessarily type $i$), then for all future purchases of the $i$-th product there is a $50\%$ discount (so you can buy an item of the $i$-th product for $1$ ruble!).
     26 
     27 Lena needs to buy $n$ products: she must purchase at least $a_i$ items of the $i$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.
     28 
     29 Input
     30 
     31 The first line contains a single integer $n$ ($1 \leq n \leq 100\,000$) — the number of products.
     32 
     33 Each of next $n$ lines contains a product description. Each description consists of two integers $a_i$ and $b_i$ ($1 \leq a_i \leq 10^{14}$, $1 \leq b_i \leq 10^{14}$) — the required number of the $i$-th product and how many products you need to buy to get the discount on the $i$-th product.
     34 
     35 The sum of all $a_i$ does not exceed $10^{14}$.
     36 
     37 Output
     38 
     39 Output the minimum sum that Lena needs to make all purchases.
     40 
     41 Examples
     42 
     43 Input
     44 
     45 ```
     46 3
     47 3 4
     48 1 3
     49 1 5
     50 ```
     51 
     52 Output
     53 
     54 ```
     55 8
     56 ```
     57 
     58 Input
     59 
     60 ```
     61 5
     62 2 7
     63 2 8
     64 1 2
     65 2 4
     66 1 8
     67 ```
     68 
     69 Output
     70 
     71 ```
     72 12
     73 ```
     74 
     75 Note
     76 
     77 In the first example, Lena can purchase the products in the following way:
     78 
     79 1. one item of product $3$ for $2$ rubles,
     80 2. one item of product $1$ for $2$ rubles,
     81 3. one item of product $1$ for $2$ rubles,
     82 4. one item of product $2$ for $1$ ruble (she can use the discount because $3$ items are already purchased),
     83 5. one item of product $1$ for $1$ ruble (she can use the discount because $4$ items are already purchased).
     84 
     85 In total, she spends $8$ rubles. It can be proved that it is impossible to spend less.
     86 
     87 In the second example Lena can purchase the products in the following way:
     88 
     89 1. one item of product $1$ for $2$ rubles,
     90 2. two items of product $2$ for $2$ rubles for each,
     91 3. one item of product $5$ for $2$ rubles,
     92 4. one item of product $3$ for $1$ ruble,
     93 5. two items of product $4$ for $1$ ruble for each,
     94 6. one item of product $1$ for $1$ ruble.
     95 
     96 In total, she spends $12$ rubles.