题意:
市场上有n种商品,每种商品的价格都是2。现在你需要买第 i 种商品 a[ i ] 件。但是对于第 i 种商品有一个属性 b ,意味着如果你已经买了 b 件商品(不一定是这一种商品),那么此商品打折,价格会降到 1。
你需要最小化你的代价。
思路:
把每种物品按照b从小到大排序,并记录物品总和一共有cnt件。ans记录当前有多少件优惠。然后从最后一项开始考虑, 显然 cnt-当前物品的b值 是当前的优惠件数上限。 如果ans < cnt - 当前物品的b, 那么有两种情况 1.它的a件商品可以全部优惠 2.它的a件商品不能全部优惠(因为如果ans+a大于当前允许优惠的件数,就不能优惠了)。 所以ans = min( ans + p[i].a, cnt - p[i].b) 分别对应2种情况。 最后的答案即为cnt*2-ans。
// Decline is inevitable,
// Romance will last forever.
#include <bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <deque>
#include <vector>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define vi vector<int>
#define endl '\n'
#define int long long
using namespace std;
const int P = 1e9 + 7;
const int maxn = 1e5 + 10;
const int maxm = 2e6 + 10;
const int INF = 0x3f3f3f3f;
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
struct node {
int a, b;
bool operator < (const node &x) const {
return b < x.b;
}
}p[maxn];
int cnt;
void solve() {
int n;
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> p[i].a >> p[i].b;
cnt += p[i].a;
}
int ans = 0;
sort(p + 1, p + n + 1);
for(int i = n; i >= 1; i--) {
if(ans < cnt - p[i].b) {
ans = min(cnt - p[i].b, ans + p[i].a);
}
}
cout << (cnt << 1) - ans << endl;
}
signed main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
// int T; cin >> T;while(T--)
solve();
return 0;
}