1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| #include<bits/stdc++.h> using namespace std; typedef long long ll; #define per(i,a,n) for (int i=a;i<n;i++) #define rep(i,a,n) for (int i=n-1;i>=a;i--)
const int MAXN(1e6+7); struct nd{ ll a, b; bool operator<(const nd& ss) { return b==ss.b?a<ss.a:b<ss.b; } }Nd[MAXN]; int T, n; ll sa, sb; ll aa, bb; ll outt; ll lasta;
int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> T; while(T--) { cin >> n; outt=sa=sb=0; per(i, 0, n) { cin >> Nd[i].a >> Nd[i].b; } sort(Nd, Nd+n); per(i, 0, n) { aa = Nd[i].a; bb = Nd[i].b; lasta = outt; ll tmpa, tmpb; tmpa = min(aa, sb); aa -= tmpa; sb -= tmpa; tmpb = min(bb, sa); bb -= tmpb; sa -= tmpb; outt+=tmpa+tmpb; sa += aa; sb += bb; } lasta = min(lasta, min(aa, bb)); outt += lasta;
cout << outt << '\n'; }
return 0; }
|