D. PriceFixed Codeforces Solution | Round 727

PriceFixed Solution Codeforces Round 727

Codeforces Round #727 (Div. 2)
Codeforces Round #727 (Div. 2)

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:

The store has an infinite number of items of every product.
All products have the same price: 2 rubles per item.
For every product i there is a discount for experienced buyers: if you buy bi 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!).
Lena needs to buy n products: she must purchase at least ai 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.

Input
The first line contains a single integer n (1≤n≤100000) — the number of products.

Each of next n lines contains a product description. Each description consists of two integers ai and bi (1≤ai≤1014, 1≤bi≤1014) — 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.

The sum of all ai does not exceed 1014.

Output
Output the minimum sum that Lena needs to make all purchases.

Examples
input
3
3 4
1 3
1 5

output
8

input
5
2 7
2 8
1 2
2 4
1 8

output
12

Note
In the first example, Lena can purchase the products in the following way:

  1. one item of product 3 for 2 rubles,
  2. one item of product 1 for 2 rubles,
  3. one item of product 1 for 2 rubles,
  4. one item of product 2 for 1 ruble (she can use the discount because 3 items are already purchased),
  5. one item of product 1 for 1 ruble (she can use the discount because 4 items are already purchased).


In total, she spends 8 rubles. It can be proved that it is impossible to spend less.

In the second example Lena can purchase the products in the following way:

  1. one item of product 1 for 2 rubles,
  2. two items of product 2 for 2 rubles for each,
  3. one item of product 5 for 2 rubles,
  4. one item of product 3 for 1 ruble,
  5. two items of product 4 for 1 ruble for each,
  6. one item of product 1 for 1 ruble.


In total, she spends 12 rubles.

Program:

#include <iostream>
#include <algorithm>

#define int long long

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  int n;
  std::cin >> n;

  std::pair<int, int> seq[n];
  int rem = 0;
  int pur = 0;
  for(int i = 0; i < n; i++) {
    std::cin >> seq[i].second >> seq[i].first;
    rem += seq[i].second;
  }

  std::sort(seq, seq+n);

  int ans = 0;
  for(int i = 0; i < n; i++) {
    if(pur < seq[i].first) {
      int now = std::min(rem, seq[i].first-pur);
      ans += now*2;
      pur += now;
      rem -= now;
    }
    int now = std::min(rem, seq[i].second);
    ans += now;
    pur += now;
    rem -= now;
  }

  std::cout << ans << std::endl;

  return 0;
}

June Long Challenge 2021 Solutions

March Long Challenge 2021 Solutions

April Long Challenge 2021 Solutions

Codechef Long Challenge Solutions

February Long Challenge 2021

January Long Challenge 2021

November Challenge 2020 SOLUTION CodeChef

October Lunchtime 2020 CodeChef SOLUTIONS

Related :

Related :

Leave a Comment