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.



Sample input:
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:
25
221
1354

Submission status: Wrong Answer

Solution code:
class BinaryIndexedTree:
    def __init__(self,sz):
        self.sz = sz
        self.data = [0]*(sz+1)
    def sum(self, *args):
        if len(args) == 1:
            # [0, k]
            res = 0
            k = args[0] + 1
            while k > 0:
                res += self.data[k]
                k -= k & -k
            return res
        elif len(args) == 2:
            # [l, r)
            return self.sum(args[1]-1) - self.sum(args[0]-1)
    def add(self, k, x):
        k += 1
        while k <= self.sz:
            self.data[k] += x
            k += k & -k
import heapq
t = int(input())
while t:
    t-=1
    n = int(input())
    klr = [list(map(int, input().split())) for i in range(n)]
    for i in range(n):
        klr[i][0] -= 1

    ans = 0
    diff = []
    posl = BinaryIndexedTree(n)
    posr = BinaryIndexedTree(n)
    for i in range(n):
        K = klr[i][0]
        L = klr[i][1] 
        R = klr[i][2]
        if L >= R:
            posl.add(K,1)
            ans += L
            diff.append((L-R,'l',i))
        else:
            posr.add(K,1)
            ans += R
            diff.append((R-L,'r',i))

    heapq.heapify(diff)

    for i in range(n):
        d,pos,idx = heapq.heappop(diff)
        K = klr[idx][0]
        if pos =='l' and posl.sum(K)>K+1:
            ans -= d
            posl.add(K,-1)
            posr.add(K,1)
        elif pos =='r' and posr.sum(K,n)>n-K-1:
            ans -= d
            posr.add(K,-1)
            posl.add(K,1)
    print(ans)

Submission status: Wrong Answer

Solution code:
class BinaryIndexedTree:
    def __init__(self,sz):
        self.sz = sz
        self.data = [0]*(sz+1)
    def sum(self, *args):
        if len(args) == 1:
            # [0, k]
            res = 0
            k = args[0] + 1
            while k > 0:
                res += self.data[k]
                k -= k & -k
            return res
        elif len(args) == 2:
            # [l, r)
            return self.sum(args[1]-1) - self.sum(args[0]-1)
    def add(self, k, x):
        k += 1
        while k <= self.sz:
            self.data[k] += x
            k += k & -k
import heapq
t = int(input())
while t:
    t-=1
    n = int(input())
    klr = [list(map(int, input().split())) for i in range(n)]
    for i in range(n):
        klr[i][0] -= 1

    ans = 0
    diff = []
    posl = BinaryIndexedTree(n)
    posr = BinaryIndexedTree(n)
    for i in range(n):
        K = klr[i][0]
        L = klr[i][1] 
        R = klr[i][2]
        if L >= R:
            posl.add(K,1)
            ans += L
            diff.append((L-R,'l',i))
        else:
            posr.add(K,1)
            ans += R
            diff.append((R-L,'r',i))

    heapq.heapify(diff)

    for i in range(n):
        d,pos,idx = heapq.heappop(diff)
        K = klr[idx][0]
        if pos =='l' and posl.sum(K)>K+1:
            ans -= d
            posl.add(K,-1)
            posr.add(K+1,1)
        elif pos =='r' and posr.sum(K,n)>n-K-1:
            ans -= d
            posr.add(K+1,-1)
            posl.add(K,1)
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
    n = int(input())
    score = 0
    line = []
    for _ in range(n):
        K, L, R = map(int, input().split())
        if K != n:
            score += min(L, R)
            if R < L:
                line.append((K, R-L))
            elif L < R:
                line.append((n, L-R))
            else:
                line.append((n, 0))
        else:
            line.append((n, -L))
    line.sort()
    for i in range(n):
        if i+1 <= line[i][0]:
            score += -line[i][1]
    print(score)

Submission status: Wrong Answer

Solution code:

t = int(input())

nlist = []
klists = []

for i in range(t):
    n = int(input())
    nlist.append(n)
    klist = []
    for j in range(n):
        klist.append(input())
    klists.append(klist)

for i in range(t):
    n = nlist[i]
    klist = klists[i]

    l_list = []
    r_list = []
    for j in range(n):
        k,l,r = map(int,klist[j].split())
        if l-r > 0:
            l_list.append([k,l,r])
        else: 
            r_list.append([k,l,r])
    

    l_list.sort(key=lambda x: (x[1]-x[2]),reverse=True)
    l_list.sort(key=lambda x: x[0])
    r_list.sort(key=lambda x: (x[1]-x[2]),reverse=True)
    r_list.sort(key=lambda x: x[0])

    l_list.extend(r_list)

    sort_list = l_list
    ans_list = []
    temp_list = []
    for j in range(n):
        while sort_list  != [] and sort_list[0][0] <= j+1:
            temp_list.append(sort_list[0])
            sort_list.pop(0)

        temp_list.sort(key=lambda x: (x[1]-x[2]),reverse=True)

        while temp_list != [] and len(ans_list)<j+1:
            ans_list.append(temp_list[0])
            temp_list.pop(0)
    
    while temp_list != []:
        ans_list.append(temp_list[0])
        temp_list.pop(0)
    
    sum = 0
    for j in range(n):
        if ans_list[j][0] >= j+1:
            sum += ans_list[j][1]
        else:
            sum += ans_list[j][2]
    print(sum)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

t = I()
for _ in range(t):
    n = I()
    klr = [LI() for _ in range(n)]
    ans = 0
    L = []
    R = []
    for k,l,r in klr:
        if l >= r:
            L.append([k, l-r])
        else:
            R.append([n-k, r-l])
        ans += min(l, r)
    L.sort()
    R.sort()
    hl = []
    hr = []
    cnt = 0
    for k, x in L:
        heappush(hl, x)
        cnt += 1
        if cnt > k:
            heappop(hl)
    cnt = 0
    for k, x in R:
        heappush(hr, x)
        cnt += 1
        if cnt > k:
            heappop(hr)
    ans += sum(hl) + sum(hr)
    print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

t = I()
for _ in range(t):
    n = I()
    klr = [LI() for _ in range(n)]
    ans = 0
    L = []
    R = []
    for k,l,r in klr:
        if l > r:
            L.append((k, l-r))
        else:
            R.append((n-k, r-l))
        ans += min(l, r)
    L.sort()
    R.sort()
    hl = []
    hr = []
    cnt = 0
    for k, x in L:
        heappush(hl, x)
        cnt += 1
        if cnt > k:
            heappop(hl)
    cnt = 0
    for k, x in R:
        heappush(hr, x)
        cnt += 1
        if cnt > k:
            heappop(hr)
    while hl:
        ans += heappop(hl)
    while hr:
        ans += heappop(hr)
    print(ans)

Submission status: Wrong Answer

Solution code:
import heapq  
def priority(klr):
    return abs(klr[1]-klr[2])
              
def process(KLR):
    ans = 0   
    q = []    
    for klr in KLR:
        pr = priority(klr)
        if klr[0] < len(q) and q[0][-1] < pr:
            tmp = heapq.heappop(q)
            ans += min(tmp[1], tmp[2])
        if klr[0] >= len(q):
            klr.append(pr)
            heapq.heappush(q, klr)
    for klr in q:
        ans += max(klr[1], klr[2])
    return ans
              
              
def solve():  
    N = int(input())
    KLR = []  
    for _ in range(N):
        klr = list(map(int, input().split()))
        klr[0] -= 1 # convert to 0-based index to avoid confusion                                                                                                                                           
        KLR.append(klr)
    ans = 0   
    KLR_L = []
    KLR_R = []
    for klr in KLR:
        if klr[1] > klr[2]:
            KLR_L.append(klr)
        elif klr[1] < klr[2]:
            klr[0] = N-1-klr[0]-1
            if klr[0] < 0:
                ans += min(klr[1], klr[2])
            else:
                KLR_R.append(klr)
        else: 
            ans += klr[1]
    KLR_L.sort(key=lambda klr: klr[0])
    KLR_R.sort(key=lambda klr: klr[0])
    ans += process(KLR_L)
    ans += process(KLR_R)
    print(ans)
              
T = int(input())
              
for _ in range(T):
    solve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

t = I()
for _ in range(t):
    n = I()
    klr = [LI() for _ in range(n)]
    ans = 0
    L = []
    R = []
    for k,l,r in klr:
        if l >= r:
            L.append((k, l-r))
        else:
            R.append((n-k, r-l))
        ans += min(l, r)
    L.sort()
    R.sort()
    hl = []
    hr = []
    cnt = 0
    for k, x in L:
        heappush(hl, x)
        cnt += 1
        if cnt > k:
            heappop(hl)
    cnt = 0
    for k, x in R:
        heappush(hr, x)
        cnt += 1
        if cnt > k:
            heappop(hr)
    while hl:
        ans += heappop(hl)
    while hr:
        ans += heappop(hr)
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys 
from heapq import heapify,heappop,heappush,heappushpop
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def main(N,K,L,R):
    left = [[] for _ in range(N)]
    right = [[] for _ in range(N)]
    score = 0
    for i in range(N):
        k = K[i]; l = L[i]; r = R[i]
        k -=1
        if l >= r:
            left[k].append(l-r)
            score += r
        else:
            right[k].append(r-l)
            score += l
    q = []
    for n,xs in enumerate(left,1):
        for x in xs:
            if len(q) <= n:
                heappush(q,x)
            else:
                heappushpop(q,x)
    score += sum(q)
    q = []
    for n,xs in enumerate(right[::-1][1:],1):
        for x in xs:
            if len(q) <= n:
                heappush(q,x)
            else:
                heappushpop(q,x)
    score += sum(q)
    print(score)

T = int(readline())
for _ in range(T):
    N = int(readline())
    K, L ,R = [],[],[]
    for _ in range(N):
        k,l,r = map(int,readline().split())
        K.append(k)
        L.append(l)
        R.append(r)
    main(N,K,L,R)

Submission status: Wrong Answer

Solution code:
def priority(klr):
    return abs(klr[1]-klr[2])

def solve():
    N = int(input())
    KLR = []
    for _ in range(N):
        klr = list(map(int, input().split()))
        klr[0] -= 1 # convert to 0-basedi index to avoid confusion                                                                                                                                          
        KLR.append(klr)
    ans = 0
    KLR_L = []
    KLR_R = []
    for klr in KLR:
        if klr[1] > klr[2]:
            KLR_L.append(klr)
        elif klr[1] < klr[2]:
            KLR_R.append(klr)
        else:
            ans += klr[1]
    KLR_L.sort(key=lambda klr: -priority(klr))
    KLR_L.sort(key=lambda klr: klr[0])
    KLR_R.sort(key=lambda klr: -priority(klr))
    KLR_R.sort(key=lambda klr: -klr[0])
    lp = 0
    rp = N - 1
    filled = [False] * N
    il = 0
    ir = 0
    while il < len(KLR_L) or ir < len(KLR_R):
        if il < len(KLR_L) and \
           (ir == len(KLR_R) or priority(KLR_L[il]) > priority(KLR_R[ir])):
            klr = KLR_L[il]
            if klr[0] >= lp and filled[lp] == False:
                filled[lp] = True
                lp+=1
                ans += max(klr[1], klr[2])
            else:
                ans += min(klr[1], klr[2])
            il += 1
        else:
            klr = KLR_R[ir]
            if klr[0] < rp and filled[rp] == False:
                filled[rp] = True
                rp -= 1
                ans += max(klr[1], klr[2])
            else:
                ans += min(klr[1], klr[2])
            ir += 1
    print(ans)


T = int(input())

for _ in range(T):
    solve()

Submission status: Wrong Answer

Solution code:
import sys
from heapq import heapify, heappop, heappush, heappushpop
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

T = int(readline())
sum = [0] * T
for i in range(T):
    N = int(readline())
    l1 = []
    l2 = []
    for j in range(N):
        a = list(map(int, readline().split()))
        if a[1] > a[2]:
            l1.append((-a[0], abs(a[1] - a[2])))
            sum[i] += a[2]
        else:
            if a[0] != N:
                l2.append((a[0], abs(a[2] - a[1])))
            sum[i] += a[1]
    sl1 = sorted(l1)
    sl2 = sorted(l2)
    h1 = []
    h2 = []
    poped = 0

    for j in range(1, N + 1):
        while len(sl1) > 0 and j == -sl1[-1][0] and len(h1) < j:
            ll = sl1.pop()
            heappush(h1, -ll[1])
        while len(sl1) > 0 and j == -sl1[-1][0]:
            sl1.pop()
        while poped <= j and len(h1) > 0:
            sum[i] += -heappop(h1)
            poped += 1
    poped = 0
    for j in reversed(range(1, N)):
        while len(sl2) > 0 and j == sl2[-1][0] and len(h2) < N - j:
            ll = sl2.pop()
            heappush(h2, -ll[1])
        while len(sl2) > 0 and j == sl2[-1][0]:
            sl2.pop()
        while poped <= N - j and len(h2) > 0:
            sum[i] += -heappop(h2)
            poped += 1

for i in range(T):
    print(sum[i])

Submission status: Wrong Answer

Solution code:
import sys
from heapq import heapify, heappop, heappush, heappushpop
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

T = int(readline())
sum = [0] * T
for i in range(T):
    N = int(readline())
    l1 = []
    l2 = []
    for j in range(N):
        a = list(map(int, readline().split()))
        if a[1] > a[2]:
            l1.append((-a[0], abs(a[1] - a[2])))
            sum[i] += a[2]
        else:
            if a[0] != N:
                l2.append((a[0], abs(a[2] - a[1])))
            sum[i] += a[1]
    sl1 = sorted(l1)
    sl2 = sorted(l2)
    h1 = []
    h2 = []
    poped = 0
    c1 = 0
    c2 = 0
    for j in range(1, N + 1):
        while len(sl1) > 0 and j == -sl1[-1][0] and c1 < j:
            ll = sl1.pop()
            c1 += 1
            heappush(h1, -ll[1])
        while len(sl1) > 0 and j == -sl1[-1][0]:
            sl1.pop()
        while poped <= j and len(h1) > 0:
            sum[i] += -heappop(h1)
            poped += 1
    poped = 0
    for j in reversed(range(1, N)):
        while len(sl2) > 0 and j == sl2[-1][0] and c2 < N - j:
            ll = sl2.pop()
            c2 += 1
            heappush(h2, -ll[1])
        while len(sl2) > 0 and j == sl2[-1][0]:
            sl2.pop()
        while poped <= N - j and len(h2) > 0:
            sum[i] += -heappop(h2)
            poped += 1

for i in range(T):
    print(sum[i])

Submission status: Wrong Answer

Solution code:
import sys
from heapq import heapify, heappop, heappush, heappushpop
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

T = int(readline())
sum = [0] * T
for i in range(T):
    N = int(readline())
    l1 = []
    l2 = []
    for j in range(N):
        a = list(map(int, readline().split()))
        if a[1] > a[2]:
            l1.append((-a[0], abs(a[1] - a[2])))
            sum[i] += a[2]
        else:
            if a[0] != N:
                l2.append((a[0], abs(a[2] - a[1])))
            sum[i] += a[1]
    sl1 = sorted(l1)
    sl2 = sorted(l2)
    h1 = []
    h2 = []
    poped = 0
    for j in range(1, N + 1):
        while len(sl1) > 0 and j >= -sl1[-1][0]:
            l = sl1.pop()
            heappush(h1, -l[1])
        while poped <= j and len(h1) > 0:
            sum[i] += -heappop(h1)
            poped += 1
    poped = 0
    for j in reversed(range(1, N)):
        while len(sl2) > 0 and j <= sl2[-1][0]:
            l = sl2.pop()
            heappush(h2, -l[1])
        while poped <= N - j and len(h2) > 0:
            sum[i] += -heappop(h2)
            poped += 1

for i in range(T):
    print(sum[i])

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, acos, asin, atan, sqrt, tan, cos, pi
from operator import mul
from functools import reduce
from pprint import pprint
from copy import deepcopy


sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 998244353

class BIT:
    def __init__(self, size):
        self.bit = [0] * size
        self.size = size
        self.total = 0

    def add(self, i, w):
        x = i + 1
        self.total += w
        while x <= self.size:
            self.bit[x - 1] += w
            x += x & -x
        return

    def sum(self, i):
        res = 0
        x = i + 1
        while x:
            res += self.bit[x - 1]
            x -= x & -x
        return res

    def get(self, i):
        if i == 0:
            return self.sum(0)
        return self.sum(i) - self.sum(i - 1)

    def interval_sum(self, i, j): # i <= x < j の区間
        return self.sum(j - 1) - self.sum(i - 1) if i else self.sum(j - 1)

    def search_r(self, l, r):
        #右側にだんだん伸ばしていく
        # l <= x < r
        ng = l - 1
        ok = r - 1
        while ng + 1 < ok:
            mid = (ng + ok) // 2
            if self.interval_sum(l, mid + 1):
                ok = mid
            else:
                ng = mid
        return ok

    def search_l(self, l, r):
        #左側にだんだん伸ばしていく
        # l <= x < r
        ng = r
        ok = l
        while ok + 1 < ng:
            mid = (ok + ng) // 2
            if self.interval_sum(mid, ng):
                ok = mid
            else:
                ng = mid
        return ok

t = I()
for _ in range(t):
    n = I()
    ans = 0
    L = []
    K = []
    for _ in range(n):
        k, l, r = LI()
        diff = l - r
        if diff > 0:
            L += [(diff, k, 1)]
        elif diff < 0:
            L += [(-diff, k, 0)]
        ans += min(l, r)

    L.sort(reverse=True)
    bit2 = BIT(n)
    bit3 = BIT(n)
    for j in range(n):
        bit2.add(j, 1)
        bit3.add(j, 1)
    for d, ki, flg in L:
        if flg:
            if bit2.sum(ki - 1) == 0:
                continue
            else:
                idx = bit2.search_l(0, ki)
                if bit3.get(idx) == 0 and bit3.search_r(idx + 1, n):
                    bit3.add(idx, 1)
                    nxt_idx = bit3.search_r(idx + 1, n)
                    bit3.add(nxt_idx, -1)
                bit2.add(idx, -1)
                ans += d
        else:
            if bit3.interval_sum(ki, n) == 0:
                continue
            else:
                idx2 = bit3.search_r(ki, n)
                if bit2.get(idx2) == 0 and bit2.search_l(0, idx2):
                    bit2.add(idx2, 1)
                    nxt_idx2 = bit2.search_l(0, idx2)
                    bit2.add(nxt_idx2, -1)
                bit3.add(idx2, -1)
                ans += d
    print(ans)

Submission status: Wrong Answer

Solution code:
def solve():
  T = int(input())
  ans = [0]*T
  for t in range(T):
    N = int(input())
    left = []
    right = []
    for i in range(N):
      k,l,r = map(int, input().split())
      if k==N:
        ans[t] += l
      else:
        if l>r:
          left.append([k,l-r])
        elif l<r:
          right.append([k+1,r-l])
        ans[t] += min(l,r)
    left.sort(key = lambda x:(-x[0],-x[1]))
    right.sort(key = lambda x:(x[0],-x[1]))
    if len(left):
      now = N+1
      for i in range(len(left)):
        ans[t] += left[i][1]
        now = min(now-1,left[i][0])
        if now==1:
          break
    if len(right):
      now = 0
      for i in range(len(right)):
        ans[t] += right[i][1]
        now = max(now+1,right[i][0])
        if now==N:
          break
  return ans
print(*solve(),sep='\n')

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, cos, radians, pi, sin
from operator import mul
from functools import reduce
from operator import mul


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7

t = I()
for _ in range(t):
    n = I()
    ans = 0
    q1 = []
    q2 = []
    L1 = []
    L2 = []
    for _ in range(n):
        k, l, r = LI()
        if l > r:
            L1 += [(k, l - r)]
        else:
            L2 += [(n - k + 1, r - l)]
        ans += min(l, r)
    L1.sort()
    L2.sort()
    for ki, d in L1:
        heappush(q1, d)
        if len(q1) > ki:
            heappop(q1)
    for ki, d in L2:
        heappush(q2, d)
        if len(q2) > ki:
            heappop(q2)
    ans += sum(q1) + sum(q2)
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, cos, radians, pi, sin
from operator import mul
from functools import reduce
from operator import mul


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7

t = I()
for _ in range(t):
    n = I()
    ans = 0
    q1 = []
    q2 = []
    L1 = []
    L2 = []
    for _ in range(n):
        k, l, r = LI()
        if l > r:
            L1 += [(k, l - r)]
        else:
            L2 += [(n - k + 1, r - l)]
        ans += min(l, r)
    L1.sort()
    L2.sort(reverse=True)
    for ki, d in L1:
        heappush(q1, d)
        if len(q1) > ki:
            heappop(q1)
    for ki, d in L2:
        heappush(q2, d)
        if len(q2) > ki:
            heappop(q2)
    ans += sum(q1) + sum(q2)
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, cos, radians, pi, sin
from operator import mul
from functools import reduce
from operator import mul


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7

t = I()
for _ in range(t):
    n = I()
    ans = 0
    q1 = []
    q2 = []
    L1 = []
    L2 = []
    for _ in range(n):
        k, l, r = LI()
        if l > r:
            L1 += [(k, l - r)]
        else:
            L2 += [(r - l, n - k + 1)]
        ans += min(l, r)
    L1.sort()
    L2.sort(reverse=True)
    for ki, d in L1:
        heappush(q1, d)
        if len(q1) > ki:
            heappop(q1)
    for ki, d in L2:
        heappush(q1, d)
        if len(q1) > ki:
            heappop(q1)
    ans += sum(q1) + sum(q2)
    print(ans)

Submission status: Wrong Answer

Solution code:
def solve():
  T = int(input())
  ans = [0]*T
  for t in range(T):
    N = int(input())
    left = []
    right = []
    for i in range(N):
      k,l,r = map(int, input().split())
      if k==N:
        ans[t] += l
      else:
        if l>r:
          left.append([k,l-r])
        elif l<r:
          right.append([k+1,r-l])
        ans[t] += min(l,r)
    left.sort(key = lambda x:(-x[0],-x[1]))
    right.sort(key = lambda x:(x[0],-x[1]))
    if len(left):
      now = N+1
      for i in range(len(left)):
        ans[t] += left[i][1]
        now = min(now-1,left[i][0])
        if now==0:
          break
    if len(right):
      now = 0
      for i in range(len(right)):
        ans[t] += right[i][1]
        now = max(now+1,right[i][0])
        if now==N:
          break
  return ans
print(*solve(),sep='\n')

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, cos, radians, pi, sin
from operator import mul
from functools import reduce
from operator import mul


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7

t = I()
for _ in range(t):
    n = I()
    ans = 0
    q1 = []
    q2 = []
    L1 = []
    L2 = []
    for _ in range(n):
        k, l, r = LI()
        if l > r:
            L1 += [(k, l - r)]
        else:
            L2 += [(n - k + 1, r - l)]
        ans += min(l, r)
    L1.sort()
    L2.sort(reverse=True)
    for ki, d in L1:
        heappush(q1, d)
        if len(q1) > ki:
            heappop(q1)
    for ki, d in L2:
        heappush(q1, d)
        if len(q1) > ki:
            heappop(q1)
    ans += sum(q1) + sum(q2)
    print(ans)