CF1539D.PriceFixed
普及/提高-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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.
莉娜是莫斯科最精打细算的女孩。因此,当她爸爸让她为一次乡村之旅购买一些食品时,她去了最好的商店——“价格固定”(PriceFixed)。该商店有如下规则:
- 商店中每种商品均有无限库存。
- 所有商品单价相同:每件 2 卢布。
- 对于每种商品 i,存在面向老顾客的折扣:若你已购买了总计 bi 件商品(可以是任意种类,不一定要是第 i 种),则此后所有第 i 种商品的购买均可享受 50% 折扣(即此后每件第 i 种商品仅需 1 卢布)!
莉娜需要购买 n 种商品:她必须至少购买第 i 种商品 ai 件。请帮莉娜计算:若她能最优地安排购买顺序,她所需的最少花费是多少?注意,她可选择购买某种商品的数量超过所需数量。
输入格式
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.
第一行包含一个整数 n(1≤n≤100000)—— 产品的数量。
接下来的 n 行每行描述一种产品。每种产品的描述包含两个整数 ai 和 bi(1≤ai≤1014,1≤bi≤1014)—— 分别表示第 i 种产品所需的数量,以及为获得第 i 种产品的折扣所需购买的产品总数。
所有 ai 的总和不超过 1014。
输出格式
Output the minimum sum that Lena needs to make all purchases.
输出 Lena 完成所有购买所需的最小总金额。
输入输出样例
输入#1
3 3 4 1 3 1 5
输出#1
8
输入#2
5 2 7 2 8 1 2 2 4 1 8
输出#2
12
说明/提示
In the first example, Lena can purchase the products in the following way:
- one item of product 3 for 2 rubles,
- one item of product 1 for 2 rubles,
- one item of product 1 for 2 rubles,
- one item of product 2 for 1 ruble (she can use the discount because 3 items are already purchased),
- 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:
- one item of product 1 for 2 rubles,
- two items of product 2 for 2 rubles for each,
- one item of product 5 for 2 rubles,
- one item of product 3 for 1 ruble,
- two items of product 4 for 1 ruble for each,
- one item of product 1 for 1 ruble.
In total, she spends 12 rubles.
在第一个例子中,莱娜可以按以下方式购买商品:
- 一件商品 3,价格为 2 卢布,
- 一件商品 1,价格为 2 卢布,
- 一件商品 1,价格为 2 卢布,
- 一件商品 2,价格为 1 卢布(此时已购商品总数达 3 件,可享受折扣),
- 一件商品 1,价格为 1 卢布(此时已购商品总数达 4 件,可享受折扣)。
总计花费 8 卢布。可以证明,无法以更少的花费完成购买。
在第二个例子中,莱娜可以按以下方式购买商品:
- 一件商品 1,价格为 2 卢布,
- 两件商品 2,每件价格为 2 卢布,
- 一件商品 5,价格为 2 卢布,
- 一件商品 3,价格为 1 卢布,
- 两件商品 4,每件价格为 1 卢布,
- 一件商品 1,价格为 1 卢布。
总计花费 12 卢布。
输入解题思路,AI测评打分。不知道怎么写?