#include using namespace std; //https://atcoder.jp/contests/aising2020/submissions/15187101 int main() { int T = 1; // cin>>T; for(int i=0;i>N; vector> l; vector> r; for(int i=0;i>a>>b>>c; swap(b, c); if(b>c){ Z+=c; l.push_back(make_pair(a,b-c)); } else if(c>b&&a!=N){ Z+=b; r.push_back(make_pair(N-a,c-b)); } else{ Z+=b; } } sort(l.begin(), l.end()); sort(r.begin(), r.end()); priority_queue, greater> pq; for(auto x:l){ int a; int64_t b; tie(a,b)=x; pq.push(b); if(a