## hdu多校第八场 1011 （hdu6667） Roundgod and Milk Tea 贪心

```#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef long long ll;
typedef pair<int, LL>P;
const int M = 4e5 * 4 + 5;
const LL mod = 1e9 + 7;
const LL lINF = 0x3f3f3f3f3f3f3f3f;
#define ls (rt<<1)
#define rs (rt<<1|1)
LL gcd(LL a, LL b) { return b ? gcd(b, a%b) : a; }
LL quickpow(LL a, LL b, LL mod)
{
LL res = 1;
while (b)
{
if (b & 1)
res = (res*a) % mod;
a = (a*a) % mod;
b >>= 1;
}
return res;
}
struct node {
LL a, b;
}tr[M];
int t;
int n;
LL cnt;
LL ans;
int lid;
bool cmp(node x, node y)
{
return x.b > y.b;
}
int main()
{
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%lld%lld", &tr[i].a, &tr[i].b);
}
sort(tr + 1, tr + 1 + n, cmp);
ans = cnt = 0;
lid = 2;
for (int i = 1; i <= n; i++)
{
while (lid < i)
{
cnt += tr[lid].a;
lid++;
}
if (lid == i)
lid++;
while (tr[i].b)
{
if (lid <= n)
{
LL tmp= min(tr[lid].a, tr[i].b);
ans += tmp;
tr[i].b -= tmp;
tr[lid].a -= tmp;
if(!tr[lid].a)
lid++;
}
else
{
LL tmp= min(tr[i].b, cnt);
ans += tmp;
tr[i].b -= tmp;
cnt -= tmp;
if (!cnt)
break;
}
}
cnt += tr[i].a;
tr[i].a = 0;
}
printf("%lld\n", ans);
}
}```