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 Java (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Collection;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.TreeSet;
import java.util.function.LongUnaryOperator;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.Queue;
import java.util.Comparator;
import java.util.ArrayDeque;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        ECamelTrain solver = new ECamelTrain();
        solver.solve(1, in, out);
        out.close();
    }

    static class ECamelTrain {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int t = in.nextInt();
            for (int i = 0; i < t; i++) {
                solve(in, out);
            }
        }

        private void solve(InputReader in, PrintWriter out) {
            int n = in.nextInt();
            long answer = 0;
            Pii[] c = new Pii[n];
            for (int i = 0; i < n; i++) {
                int k = in.nextInt() - 1;
                int l = in.nextInt(), r = in.nextInt();
                if (k == n - 1) {
                    answer += l;
                    c[i] = Pii.of(0, 0);
                    continue;
                }
                if (r - l > 0)
                    k++;
                c[i] = Pii.of(k, l - r);
                answer += Math.min(l, r);
            }
            long base = answer;

            Arrays.sort(c, Comparator.comparingInt(p -> -Math.abs(p.second)));

            LongUnaryOperator split = (long pivot) -> {
                Queue<Pii> q = new ArrayDeque<>();
                for (Pii p : c) {
                    if (p.second <= 0)
                        continue;
                    q.add(p);
                }

                long left = f(q, (int) pivot, n);

                q.clear();
                for (Pii p : c) {
                    if (p.second >= 0)
                        continue;
                    q.add(p);
                }
                long right = f(q, n - (int) pivot, n);

                return -(base + left + right);
            };

            long pivot = BinarySearch.ternarySearchLong(split, 0, n);
            answer = -split.applyAsLong(pivot);
            answer = Math.max(answer, -split.applyAsLong(0));
            answer = Math.max(answer, -split.applyAsLong(n));

            out.println(answer);
        }

        private int f(Queue<Pii> q, int count, int n) {
            TreeSet<Integer> avail = new TreeSet<>();
            for (int i = 0; i < n; i++)
                avail.add(i);
            int[] occupant = new int[n];

            int answer = 0;
            while (!q.isEmpty()) {
                if (n - avail.size() >= count)
                    break;

                Pii p = q.poll();
                int target = p.first;
                int score = p.second;

                if (avail.contains(target)) {
                    avail.remove(target);
                    occupant[target] = score;
                    answer += Math.abs(score);
                    continue;
                }

                if (occupant[target] > 0) {
                    Integer index = avail.floor(target);
                    if (index == null)
                        continue;

                    occupant[index] = occupant[target];
                    occupant[target] = score;
                    avail.remove(index);
                } else {
                    Integer index = avail.ceiling(target);
                    if (index == null)
                        continue;

                    occupant[index] = occupant[target];
                    occupant[target] = score;
                    avail.remove(index);
                }

                answer += Math.abs(score);
            }

            return answer;
        }

    }

    static class Pii implements Comparable<Pii> {
        public final int first;
        public final int second;

        public Pii(int first, int second) {
            this.first = first;
            this.second = second;
        }

        public static Pii of(int first, int second) {
            return new Pii(first, second);
        }

        public boolean equals(Object o) {
            if (this == o)
                return true;
            if (o == null || getClass() != o.getClass())
                return false;
            Pii pair = (Pii) o;
            return first == pair.first && second == pair.second;
        }

        public int hashCode() {
            return Arrays.hashCode(new int[]{first, second});
        }

        public String toString() {
            return "(" + first + ", " + second + ')';
        }

        public int compareTo(Pii o) {
            if (first != o.first)
                return Integer.compare(first, o.first);
            return Integer.compare(second, o.second);
        }

    }

    static class InputReader {
        public final BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }

    static class BinarySearch {
        public static long ternarySearchLong(LongUnaryOperator f, long left, long right) {
            while (true) {
                long t = (right - left) / 3;
                if (t == 0) {
                    long answer = left;
                    long opt = f.applyAsLong(left);
                    for (long x = left + 1; x <= right; x++) {
                        long candidate = f.applyAsLong(x);
                        if (candidate < opt) {
                            answer = x;
                            opt = candidate;
                        }
                    }
                    return answer;
                }
                long a = left + t;
                long b = right - t;
                if (f.applyAsLong(a) > f.applyAsLong(b)) {
                    left = a;
                } else {
                    right = b;
                }
            }
        }

    }
}