Problem:
Score : 500 points

Problem Statement

We have N camels numbered 1,2,\ldots,N.
Snuke has decided to make them line up in a row.

The happiness of Camel i will be L_i if it is among the K_i frontmost camels, and R_i otherwise.

Snuke wants to maximize the total happiness of the camels. Find the maximum possible total happiness of the camel.

Solve this problem for each of the T test cases given.

Constraints

All values in input are integers.

1 \leq T \leq 10^5

1 \leq N \leq 2 \times 10^{5}

1 \leq K_i \leq N

1 \leq L_i, R_i \leq 10^9

The sum of values of N in each input file is at most 2 \times 10^5.

Input

Input is given from Standard Input in the following format:

T
\mathrm{case}_1
\vdots
\mathrm{case}_T

Each case is given in the following format:

N
K_1 L_1 R_1
\vdots
K_N L_N R_N

Output

Print T lines. The i-th line should contain the answer to the i-th test case.

Sample Input 1

3
2
1 5 10
2 15 5
3
2 93 78
1 71 59
3 57 96
19
19 23 16
5 90 13
12 85 70
19 67 78
12 16 60
18 48 28
5 4 24
12 97 97
4 57 87
19 91 74
18 100 76
7 86 46
9 100 57
3 76 73
6 84 93
1 6 84
11 75 94
19 15 3
12 11 34

Sample Output 1

25
221
1354

In the first test case, it is optimal to line up the camels in the order 2, 1.

Camel 1 is not the frontmost camel, so its happiness will be 10.

Camel 2 is among the two frontmost camels, so its happiness will be 15.

In the second test case, it is optimal to line up the camels in the order 2, 1, 3.

Camel 1 is among the two frontmost camels, so its happiness will be 93.

Camel 2 is the frontmost camel, so its happiness will be 71.

Camel 3 is among the three frontmost camels, so its happiness will be 57.

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N=4e5+5;
struct node
{
    int k,l,r;
    node(int k=0,int l=0,int r=0):k(k),l(l),r(r){}
};
struct haha
{
    int id,v,k;
    haha(int id=0,int v=0,int k=0):id(id),v(v),k(k){}
    bool operator<(const haha&o)const
    {
        if(v==o.v) return k<o.k;
        return v<o.v;
    }
};
vector<node>vl,vr;
int n,t[N<<2],lz[N<<2],ss[N];
ll ans=0;
ll s[2][N];
void update(int l,int r,int k)
{
    t[k]-=lz[k];
    assert(t[k]>=0);
    if(l!=r)
        lz[k<<1]+=lz[k],lz[k<<1|1]+=lz[k];
    lz[k]=0;
}
int query(int l,int r,int k,int x)
{
    if(lz[k]) update(l,r,k);
    if(r<x) return 1;
    if(l>=x) return t[k];
    int m=l+r>>1;
    int ans=min(query(l,m,k<<1,x),query(m+1,r,k<<1|1,x));
    t[k]=min(t[k<<1],t[k<<1|1]);
    return ans;
}
void fix(int l,int r,int k,int x)
{
    if(lz[k]) update(l,r,k);
    if(r<x) return;
    if(l>=x)
    {
        lz[k]++;
        update(l,r,k);
        return;
    }
    int m=l+r>>1;
    fix(l,m,k<<1,x);fix(m+1,r,k<<1|1,x);
    t[k]=min(t[k<<1],t[k<<1|1]);
}
void build(int l,int r,int k)
{
    t[k]=l;lz[k]=0;
    if(l==r) return;
    int m=l+r>>1;
    build(l,m,k<<1);build(m+1,r,k<<1|1);
}
void solve(vector<node>v,int p)
{
    build(1,n,1);
    for(int i=1;i<=n;i++) ss[i]=i;
    priority_queue<haha>q;
    ll sum=0;
    for(int i=0;i<v.size();i++) sum+=v[i].r,q.push(haha(i,v[i].l-v[i].r,v[i].k));
    s[p][0]=sum;
    for(int i=1;i<=n;i++)
    {
        int l=1,r=n,pos=0;
        while(l<=r)
        {
            int m=l+r>>1;
            if(query(1,n,1,m)==0) pos=m,l=m+1;
            else r=m-1;
        }
        while(!q.empty())
        {
            int id=q.top().id;q.pop();
            if(v[id].k==0) continue;
            if(v[id].k>pos)
            {
                //cout<<pos<<' '<<v[id].l<<' '<<v[id].r<<' '<<v[id].k<<endl;
                sum=sum-v[id].r+v[id].l;
                fix(1,n,1,v[id].k);
                break;
            }
        }
        s[p][i]=max(sum,s[p][i-1]);
    }
}
fstream in,out;
void getts()
{
    int t=50;
    in.open("1.txt");
    in<<t<<endl;
    while(t--)
    {
        int n=1000;
        in<<n<<endl;
        for(int i=1;i<=n;i++)
        {
            int k=rand()%n+1,l=rand()%100+1,r=rand()%100+1;
            in<<k<<' '<<l<<' '<<r<<endl;
        }
    }
    in.close();
}
bool de=false;
int main()
{
    srand(time(0));
    //getts();freopen("1.txt","r",stdin);out.open("out.txt");de=true;
    int t;scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        vl.clear();vr.clear();ans=0;
        for(int i=1;i<=n;i++)
        {
            int k,l,r;
            scanf("%d%d%d",&k,&l,&r);
            if(l==r) ans+=l;
            else if(l>r) vl.push_back({k,l,r});
            else vr.push_back({n-k,r,l});
        }
        for(int i=0;i<=n;i++) s[0][i]=s[1][i]=0;
        solve(vl,0);
        solve(vr,1);
        ll res=0;
        for(int i=0;i<=n;i++)
            res=max(res,s[0][i]+s[1][n-i]);
        if(!de) printf("%lld\n",ans+res);
        if(de) out<<ans+res<<endl;
    }
}
/*
1
5
4 65 49
4 59 67
3 66 67
4 98 68
3 32 91
*/