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:
T = int(input())
for fakevar in range(T):
	N=int(input())
	p=[]
	n=[]
	min_p=0
	min_n=0
	for fakevar2 in range(N):
		K,L,R=map(int, input().split())
		if L-R>=0:
			p+=[[L-R,K-1]]
			min_p+=R
		else:
			n+=[[R-L,N-K-1]]
			min_n+=L
	
	p.sort(key=lambda x: x[0], reverse=True)
	n.sort(key=lambda x: x[0], reverse=True)
	p_set=set([])
	n_set=set([])
	p_score=0
	n_score=0
	for i in range(len(p)):
		j=min(p[i][1],len(p)-1)
		k=set(range(j))-p_set
		if len(k)>0:
			p_set.add(max(k))
			p_score+=p[i][0]
	
	for i in range(len(n)):
		j=min(n[i][1],len(n)-1)
		k=set(range(j))-n_set
		if len(k)>0:
			n_set.add(max(k))
			n_score+=n[i][0]
	
	print(p_score+n_score+min_p+min_n)






# 2darray [[0] * 4 for i in range(3)]

Submission status: Wrong Answer

Solution code:
�LÛ

Submission status: Wrong Answer

Solution code:
�

Submission status: Wrong Answer

Solution code:
import sys
INF = 1 << 60
MOD = 10**9 + 7 # 998244353
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
from heapq import heappop, heappush
def resolve():
    for _ in range(int(input())):
        n = int(input())
        heaps = [[] for _ in range(n)]
        ans = 0
        for i in range(n):
            k, l, r = map(int, input().split())
            ans += l
            k -= 1
            heappush(heaps[k], -(r - l))
        candidate = []
        for i in range(n):
            if candidate:
                ans -= min(heappop(candidate), 0)
            while heaps[i]:
                heappush(candidate, heaps[i].pop())

        print(ans)
resolve()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(500000)
def input():
    return sys.stdin.readline()[:-1]

def solve():
    N = int(input())
    L = []
    R = []
    base = 0
    for i in range(N):
        k,l,r = list(map(int,input().split()))
        if l>r:
            L.append((l-r,k))
            base+=r
        else:
            R.append((r-l,N-k))
            base+=l
    L.sort(key = lambda x:x[0],reverse=True)
    L.sort(key = lambda x:x[1])
    l_ans = 0
    l_count = 0
    for i in range(len(L)):
        score,k = L[i]
        if k>l_count:
            l_count+=1
            l_ans+=score

    R.sort(key = lambda x:x[0],reverse=True)
    R.sort(key = lambda x:x[1])
    r_ans = 0
    r_count = 0
    for i in range(len(R)):
        score,k = R[i]
        if k>r_count:
            r_count+=1
            r_ans+=score
    print(base+l_ans+r_ans)


def main():
    T = int(input())

    for _ in range(T):
        solve()
        
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import namedtuple
from pprint import pp

Camel = namedtuple("Camel", "k l r crt preference avail".split(" "))

def read_int():
    return int(input())

def read_int_array():
    return [int(ss) for ss in input().split()]

def solution():
    n = read_int()
    camels = []

    for _ in range(n):
        k, l, r = read_int_array()
        preference = None
        if l > r:
            preference = "l"
        elif l < r:
            preference = "r"
        camels.append(Camel(k - 1, l, r, None, preference, None))

    camels = sorted(camels, key = lambda c: -abs(c.l - c.r))

    arrangement = [None for _ in camels]

    lefties = [None for _ in camels]
    righties = [None for _ in camels]

    lcount = 0
    rcount = 0

    total_happy = 0

    for c in camels:
        if c.preference is None:
            total_happy += c.r

        elif c.preference == "l":
            total_happy += c.r
            for i in range(c.k, -1, -1):
                if lefties[i] is None:
                    lefties[i] = c
                    lcount += 1
                    total_happy += c.l - c.r
                    break

        else: # "r"
            total_happy += c.l
            for i in range(c.k + 1, n):
                if righties[i] is None:
                    righties[i] = c
                    rcount += 1
                    total_happy += c.r - c.l
                    break

    return total_happy

def main():
    t = read_int()

    for i in range(t):
        s = solution()
        print(f"Case #{ i + 1 }: {s}")


main()

Submission status: Wrong Answer

Solution code:
from heapq import heapify, heappop, heappush
Ncase = int(input())
for case in range(Ncase):
  N = int(input())
  ans = 0
  Left = [];Right = []
  for i in range(N):
    K,L,R = map(int,input().split())
    if L > R:
      Left.append((K-1,L-R))
      ans += R
    elif R > L:
      if K != N: #K=Nの時はどこにおいても左と判定される。
        Right.append((N-1-K,R-L))
      ans += L
    else:
      ans += L
  Left.sort(reverse=True);Right.sort(reverse=True) #Kの小さい順、左にいたときのプラス分が大きい順に並んだ
  #print(Left,Right)
  now = 0
  PQ = []
  for loc in reversed(range(N)):
    #print(now,Left[now][0],loc)
    while now < len(Left) and Left[now][0] == loc:
      heappush(PQ,Left[now][1])
      now += 1
      if now >= len(Left):
        break
    if PQ:
      plus = heappop(PQ)
      ans += plus
  
  now = 0
  PQ = []
  for loc in reversed(range(N)):
    while now < len(Right) and Right[now][0] == loc:
      heappush(PQ,Right[now][1])
      now += 1
      if now >= len(Right):
        break
    if PQ:
      plus = heappop(PQ)
      ans += plus
  print(ans)

Submission status: Wrong Answer

Solution code:
#写経
#https://atcoder.jp/contests/aising2020/submissions/15195468
from heapq import heappop, heappush
import sys
input = sys.stdin.readline

def resolve():
    T = int(input())
    for _ in range(T):
        N = int(input())
        ans = 0
        camels0 = [[] for _ in range (n)]
        camels1 = [[] for _ in range (n)]
        for _ in range(N):
            K,L,R = map(int, input().split())
            if L>R:
                camels0[K-1].append(L-R)
            elif L<R and K<N:
                camels1[N-K-1].append(R-L)
            ans += min(L,R)
        
        H = []
        for i in range(N):
            for j in camels0[i]:
                heappush(H,j)
            while len(H) > i+1:
                heappop(H)
        ans += sum(H)
        H = []
        for i in range(N):
            for j in camels1[i]:
                heappush(H,j)
            while len(H) > i+1:
                heappop(H)
        ans += sum(H)
        print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
t = int(input())
for _ in range(t):
    n = int(input())
    klr = [tuple(map(int, input().split())) for _ in range(n)]
    klr.sort()
    
    left = [[]for _ in range(n)]
    right = [[]for _ in range(n)]
    score = 0
    for K,L,R in klr:
        if K!=n:
            if L<R:
                score+=L
                right[n-K-1].append(R-L)
            elif R<L:
                score+=R
                left[K-1].append(L-R)
            else:
                score+=L
        else:
            score+=L
    
    def calc(ls):
        n = len(ls)
        use = []
        retval = 0
        for i in reversed(range(n)):
            for x in ls[i]:
                heapq.heappush(use, x)
            if use:
                retval+=heapq.heappop(use)
        return retval

    score+=calc(left)
    score+=calc(right)
    print(score)

Submission status: Wrong Answer

Solution code:
import heapq
t = int(input())
for _ in range(t):
    n = int(input())
    klr = [tuple(map(int, input().split())) for _ in range(n)]
    klr.sort()
    left = []
    right = []
    score = 0
    for K,L,R in klr:
        if L<R:
            right.append((K,L,R))
        elif R<L:
            left.append((K,L,R))
        else:
            score+=L

    right.sort(reverse=True)
    left.sort()

    use_left = []
    for K,L,R in left:
        if K!=n:
            score+=R
            heapq.heappush(use_left,L-R)
        else:
            score+=L
            heapq.heappush(use_left,0)

        if K<len(use_left):
            heapq.heappop(use_left)
        

    use_right = []
    for K,L,R in right:
        if K!=n:
            score+=L
            heapq.heappush(use_right,R-L)
        else:
            score+=L
            heapq.heappush(use_right,0)


        if (n-K+1)<len(use_right):
            heapq.heappop(use_right)

    score+=sum(use_left)+sum(use_right)
    print(score)

Submission status: Wrong Answer

Solution code:
import heapq
t = int(input())
for _ in range(t):
    n = int(input())
    klr = [tuple(map(int, input().split())) for _ in range(n)]
    klr.sort()
    left = []
    right = []
    score = 0
    for K, L, R in klr:
        if K != n:
            score += min(L, R)
            if L<R:
                right.append(R-L)
            elif R<L:
                heapq.heappush(left, L-R)
        else:
            score += L

        if K<len(left):
            heapq.heappop(left)
    right.sort(reverse=True)
    score += sum(left)
    score += sum(right[:n-len(left)])
    print(score)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
it = lambda: list(map(int, input().strip().split()))


def greedy(A):
    ans = 0
    A.sort(key=lambda x:x[1] - x[2])
    pq = []
    while A:
        k, a, b = A.pop()
        if k > 0 and (k > len(pq) or -pq[0][0] >= len(pq) + 1):
            heappush(pq, [-k, a, b])
            ans += a
        else:
            ans += b
    return ans


def solve():
    N = int(input())

    prefer_late = []
    prefer_early = []
    does_not_matter = []
    for _ in range(N):
        k, a, b = it()
        if a == b:
            does_not_matter.append([k, a, b])
        elif a > b:
            prefer_early.append([k, a, b])
        else:
            prefer_late.append([N - k, b, a])
    
    ans = 0
    ans += greedy(prefer_early)
    ans += greedy(prefer_late)
    for k, a, b in does_not_matter:
        ans += a
    print(ans)


T = int(input())
for _ in range(T):
    solve()

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush, heapify

T = int(input())

for _ in range(T):
    N = int(input())
    camel = [tuple(map(int, input().split())) for i in range(N)]
    dif = [(camel[i][1] - camel[i][2], camel[i][0] - 1, i) for i in range(N)]

    pos = []
    neg = []

    for i in range(N):
        if dif[i][0] >= 0:
            pos.append((-dif[i][0], dif[i][1], dif[i][2]))
        else:
            neg.append(dif[i])

    p = len(pos)
    n = len(neg)

    pos.sort(key=lambda x: x[1], reverse=True)
    neg.sort(key=lambda x: x[1])

    heappos = []
    heapneg = []

    arr = [None for i in range(N)]

    j = 0
    for i in range(p)[::-1]:
        while j != p and pos[j][1] >= i:
            heappush(heappos, pos[j])
            j += 1
        if not heappos:
            continue
        d, k, id = heappop(heappos)
        arr[i] = id

    j = 0
    for i in range(n):
        while j != n and neg[j][1] <= p + i:
            heappush(heapneg, neg[j])
            j += 1
        if not heapneg:
            continue
        d, k, id = heappop(heapneg)
        arr[p + i] = id
    res = 0

    used = [0 for i in range(N)]

    for i in range(N):
        if arr[i] is None:
            continue
        k, l, r = camel[arr[i]]
        used[arr[i]] = 1
        if k >= i + 1:
            res += l
        else:
            res += r

    for i in range(N):
        if not used[i]:
            k, l, r = camel[i]
            res += min(l, r)

    print(res)

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush, heapify

T = int(input())

for _ in range(T):
    N = int(input())
    camel = [tuple(map(int, input().split())) for i in range(N)]
    dif = [(camel[i][1] - camel[i][2], camel[i][0] - 1, i) for i in range(N)]

    pos = []
    neg = []

    for i in range(N):
        if dif[i][0] >= 0:
            pos.append((-dif[i][0], dif[i][1], dif[i][2]))
        else:
            neg.append(dif[i])

    p = len(pos)
    n = len(neg)

    pos.sort(key=lambda x: x[1], reverse=True)
    neg.sort(key=lambda x: x[1])

    heappos = []
    heapneg = []

    arr = [None for i in range(N)]

    j = 0
    for i in range(p)[::-1]:
        while j != p and pos[j][1] >= i:
            heappush(heappos, pos[j])
            j += 1
        if not heappos:
            heappush(heappos, pos[j])
            j += 1
        d, k, id = heappop(heappos)
        arr[i] = id

    j = 0
    for i in range(n):
        while j != n and neg[j][1] <= p + i:
            heappush(heapneg, neg[j])
            j += 1
        if not heapneg:
            heappush(heapneg, neg[j])
            j += 1
        d, k, id = heappop(heapneg)
        arr[p + i] = id
    res = 0

    for i in range(N):
        k, l, r = camel[arr[i]]
        if k >= i + 1:
            res += l
        else:
            res += r

    print(res)

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush, heapify

T = int(input())

for _ in range(T):
    N = int(input())
    camel = [tuple(map(int, input().split())) for _ in range(N)]
    dif = [(camel[i][1] - camel[i][2], camel[i][0] - 1, i) for i in range(N)]

    pos = []
    neg = []

    for i in range(N):
        if dif[i][0] >= 0:
            pos.append((-dif[i][0], dif[i][1], dif[i][2]))
        else:
            neg.append(dif[i])

    p = len(pos)
    n = len(neg)

    pos.sort(key=lambda x: x[1], reverse=True)
    neg.sort(key=lambda x: x[1])

    heappos = []
    heapneg = []

    arr = [None for i in range(N)]

    j = 0
    for i in range(p)[::-1]:
        f = False
        while j != p and pos[j][1] >= i:
            heappush(heappos, pos[j])
            j += 1
            f = True
        if not f and j != p:
            heappush(heappos, pos[j])
            j += 1
        d, k, id = heappop(heappos)
        arr[i] = id

    j = 0
    for i in range(n):
        f = False
        while j != n and neg[j][1] <= N - i - 1:
            heappush(heapneg, neg[j])
            j += 1
            f = True
        if not f and j != n:
            heappush(heapneg, neg[j])
            j += 1
        d, k, id = heappop(heapneg)
        arr[N - 1 - i] = id

    res = 0

    for i in range(N):
        k, l, r = camel[arr[i]]
        if k <= i:
            res += r
        else:
            res += l

    print(res)

Submission status: Wrong Answer

Solution code:
import sys
from heapq import *
def input(): return sys.stdin.readline().strip()


def func(left):
    ll = len(left)
    """
    前から順番に詰めていく方式だと次の反例が存在する：
    ll = 2, left = [(1, 1), (2, 2), (3, 100)]
    (3, 100)をなるべく先頭に置くべきだが、前のコードだと３番目が車で保留。
    ll < 3なのでその時は訪れない。

    なのでやはり後ろから攻めるしかない模様。
    """
    L = [[] for _ in range(ll + 1)]
    for k, v in left:
        if k > ll: k = ll
        L[k].append(v)
    ans = 0
    q = []
    for i in range(ll, 0, -1):
        for v in L[i]: heappush(q, v)
        if q: ans += heappop(q)
    return ans


def main():
    T = int(input())
    for _ in range(T):
        n = int(input())
        ans = 0
        left = []
        right = []
        for _ in range(n):
            k, l, r = map(int, input().split())
            if l > r:
                ans += r
                left.append((k, l - r))
            else:
                ans += l
                right.append((n - k, r - l))
        #print("left={}".format(left))
        #print("right={}".format(right))
        ans += func(left)
        ans += func(right)
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()


def func(left):
    ll = len(left)
    left.sort(key=lambda x: (x[0], -x[1]))
    val = 0
    idx = 0
    S = 0
    while idx < ll:
        k = left[idx][0]
        while idx < ll and left[idx][0] == k:
            _, v = left[idx]
            if S < k:
                val += v
                S += 1
            idx += 1
    return val


def main():
    T = int(input())
    for _ in range(T):
        n = int(input())
        ans = 0
        left = []
        right = []
        for _ in range(n):
            k, l, r = map(int, input().split())
            if l > r:
                ans += r
                left.append((k, l - r))
            else:
                ans += l
                right.append((n - k, r - l))
        ans += func(left)
        ans += func(right)
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()


def main():
    T = int(input())
    for _ in range(T):
        N = int(input())
        # 左右独立に解いて問題ない
        left = []
        right = []
        for i in range(N):
            k, l, r = map(int, input().split())
            if l > r: left.append((k, l, r))
            else: right.append((k, l, r))
        left.sort(key=lambda x: (x[0], -x[1]))
        right.sort(key=lambda x: (-x[0], -x[2]))
        ll = len(left)
        rl = len(right)
        #print("left={}".format(left))
        #print("right={}".format(right))

        ans = 0
        # 左側
        idx = 0
        S = 0
        while idx < ll:
            k = left[idx][0]
            while idx < ll and left[idx][0] == k:
                _, l, r = left[idx]
                if S <= k:
                    ans += l
                    S += 1
                else:
                    ans += r
                idx += 1
        #print("left_val={}".format(ans))
        # 右側
        idx = 0
        S = N
        while idx < rl:
            k = right[idx][0]
            while idx < rl and right[idx][0] == k:
                _, l, r = right[idx]
                if S > k:
                    ans += r
                    S -= 1
                else:
                    ans += l
                idx += 1

        print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import heapq
T=int(input())

for _ in range(T):
  N=int(input())
  
  llist=[]
  rlist=[]
  base=0
  for _ in range(N):
    K,L,R=map(int,input().split())    
    if L>R:
      llist.append((K,L-R))
      base+=R
    elif L<R:
      rlist.append((N-K,R-L))
      base+=L
      
  llist.sort()
  rlist.sort()      
  #print(base)
  #print(llist)
  #print(rlist)
  
  hq_left=[]
  for i,l in llist:
    heapq.heappush(hq_left,l)
    if len(hq_left)>i:
      heapq.heappop(hq_left)
  hq_right=[]
  for i,r in rlist:
    heapq.heappush(hq_right,r)
    if len(hq_right)>i:
      heapq.heappop(hq_right)
      
  answer=base
  while hq_left:
    answer+=heapq.heappop(hq_left)
  while hq_right:
    answer+=heapq.heappop(hq_right)
    
  print(answer)

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


class BinaryIndexTree:
    def __init__(self, n):
        self.size = n
        self._container = [0] * (n + 1)
        self.depth = n.bit_length()

    def sum(self, i):
        if i <= 0:
            return 0
        s = 0
        while i > 0:
            s += self._container[i]
            i -= i & (-i)
        return s

    def add(self, i, x):
        if i <= 0:
            return
        while i <= self.size:
            self._container[i] += x
            i += i & (-i)

    def lower_bound(self, x):
        if x <= 0:
            return 0

        s = 0
        idx = 0
        for i in range(self.depth, -1, -1):
            k = idx + (1 << i)
            if k <= self.size and s + self._container[k] < x:
                s += self._container[k]
                idx += 1 << i
        return idx + 1

    def __repr__(self):
        return str(self._container)


def main():
    from operator import add, itemgetter

    t = int(readline())

    for _ in range(t):
        n = int(readline())
        res = 0
        tr1 = BinaryIndexTree(n)
        tr2 = BinaryIndexTree(n)
        mat = [[0] * 4 for __ in range(n)]
        for i in range(n):
            k, l, r = map(int, readline().split())
            mat[i][0] = k
            mat[i][1] = abs(l - r)
            mat[i][2] = l
            mat[i][3] = r
        mat.sort(key=itemgetter(1), reverse=True)

        for k, abs_val, l, r in mat:
            if l - r > 0:
                if tr1.sum(k) < k:
                    res += l
                    tr1.add(k, 1)
                    tr2.add(n - k - 1, 1)
                    tr2.add(n - k, -1)
                else:
                    res += r
            else:
                if tr2.sum(n - k) < n - k:
                    res += r
                    tr2.add(n - k, 1)
                    tr1.add(k, -1)
                    tr1.add(k + 1, 1)
                else:
                    res += l
        print(res)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


class BinaryIndexTree:
    def __init__(self, n):
        self.size = n
        self._container = [0] * (n + 1)
        self.depth = n.bit_length()

    def sum(self, i):
        if i == 0:
            return 0
        s = 0
        while i > 0:
            s += self._container[i]
            i -= i & (-i)
        return s

    def add(self, i, x):
        if i == 0:
            return
        while i <= self.size:
            self._container[i] += x
            i += i & (-i)

    def lower_bound(self, x):
        if x == 0:
            return 0

        s = 0
        idx = 0
        for i in range(self.depth, -1, -1):
            k = idx + (1 << i)
            if k <= self.size and s + self._container[k] < x:
                s += self._container[k]
                idx += 1 << i
        return idx + 1

    def __repr__(self):
        return str(self._container)


def main():
    from operator import add, itemgetter

    t = int(readline())

    for _ in range(t):
        n = int(readline())
        res = 0
        tr1 = BinaryIndexTree(n)
        tr2 = BinaryIndexTree(n)
        mat = [[0] * 4 for __ in range(n)]
        for i in range(n):
            k, l, r = map(int, readline().split())
            mat[i][0] = k
            mat[i][1] = abs(l - r)
            mat[i][2] = l
            mat[i][3] = r
        mat.sort(key=itemgetter(1), reverse=True)

        for k, abs_val, l, r in mat:
            if l - r > 0:
                if tr1.sum(k) < k:
                    res += l
                    tr1.add(k, 1)
                else:
                    res += r
            else:
                if tr2.sum(n - k) < n - k:
                    res += r
                    tr2.add(n - k, 1)
                else:
                    res += l
        print(res)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
t=int(input())
import heapq
for _ in range(t):
    n=int(input())
    L=[];R=[]
    ans=0
    for _ in range(n):
        ki,li,ri = map(int, input().split())
        dif=li-ri
        if dif>=0:
            L.append((ki-1,dif))
            ans+=ri
        else:
            R.append((n-ki-1,-dif))
            ans+=li
    L.sort();R.sort()
    hL=[]
    hR=[]
    cnt=0
    for i in range(n-1,-1,-1):
        if L:
            while L and L[-1][0]==i:
                heapq.heappush(hL,-L[-1][1])
                L.pop()
            while hL and cnt<=n-i:
                ad=heapq.heappop(hL)
                ans-=ad
                cnt+=1
        else:
            while hL and cnt<=n-i:
                ad=heapq.heappop(hL)
                ans-=ad
                cnt+=1
    cnt=0
    for i in range(n-1,-1,-1):
        if R:
            while R and R[-1][0]==i:
                heapq.heappush(hR,-R[-1][1])
                R.pop()
            while hR and cnt<=n-i:
                ad=heapq.heappop(hR)
                ans-=ad
                cnt+=1
        else:
            while hR and cnt<=n-i:
                ad=heapq.heappop(hR)
                ans-=ad
                cnt+=1
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from heapq import heappop, heappush
def main():
    def input(): return sys.stdin.readline().rstrip()
    n = int(input())
    l_camel = [[] for _ in range(n)]
    r_camel = [[] for _ in range(n)]
    ans = 0
    for i in range(n):
        k, l, r = map(int, input().split())
        if l >= r:
            ans += r
            l_camel[k-1].append(l-r)
        else:
            ans += l
            r_camel[k-1].append(r-l)
    
    table = [True]*n
    cur = 0
    for i in range(n):
        while l_camel[i]:
            node = heappop(l_camel[i])
            if cur <= i:
                cur += 1
                ans += node
    
    cur = n-1
    for i in range(n-1, -1, -1):
        while r_camel[i]:
            node = heappop(r_camel[i])
            if cur > i:
                cur -= 1
                ans += node
    print(ans)


if __name__ == '__main__':
    t = int(input())
    for i in range(t):
        main()

Submission status: Wrong Answer

Solution code:
# でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
from heapq import heappush, heappop
import sys
def main(N, KLR):
    LL = [(-(l - r), k - 1, l, r) for k, l, r in KLR if l >= r and k < N]
    RR = [(-(r - l), N - k - 1, r, l) for k, l, r in KLR if l < r and k < N]
    ans = sum(l for k, l, r in KLR if k == N)
    for L in (LL, RR):
        M = len(L)
        L.sort(key=lambda x: x[1])
        q = []
        j = 0
        c = 0
        for i in range(N):
            while j < M and L[j][1] <= i:
                heappush(q, L[j])
                j += 1
            while q and c <= i:
                _, k, l, r = heappop(q)
                if c <= k:
                    ans += l
                else:
                    ans += r
                c += 1
    return ans

if __name__ == '__main__':
    input = sys.stdin.readline
    T = int(input())
    for i in range(T):
        N = int(input())
        KLR = [tuple(map(int, input().split())) for _ in range(N)]
        ans = main(N, KLR)
        print(ans)

Submission status: Wrong Answer

Solution code:
# でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
from heapq import heappush, heappop
import sys
def main(N, KLR):
    LL = [(-(l - r), k - 1, l, r) for k, l, r in KLR if l >= r]
    RR = [(-(r - l), N - k - 1, r, l) for k, l, r in KLR if l < r and k < N]
    ans = sum(l for k, l, r in KLR if l < r and k == N)
    for L in (LL, RR):
        M = len(L)
        L.sort(key=lambda x: x[1])
        q = []
        j = 0
        c = 0
        for i in range(N):
            while j < M and L[j][1] <= i:
                heappush(q, L[j])
                j += 1
            while q and c <= i:
                _, k, l, r = heappop(q)
                if c <= k:
                    ans += l
                else:
                    ans += r
                c += 1
    return ans

if __name__ == '__main__':
    input = sys.stdin.readline
    T = int(input())
    for i in range(T):
        N = int(input())
        KLR = [tuple(map(int, input().split())) for _ in range(N)]
        ans = main(N, KLR)
        print(ans)

Submission status: Wrong Answer

Solution code:
# でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
from heapq import heappush, heappop
import sys
def main(N, KLR):
    LL = [(-(l - r), k - 1, l, r) for k, l, r in KLR if l >= r]
    RR = [(-(r - l), N - k - 1, r, l) for k, l, r in KLR if l < r]
    ans = 0
    for L in (LL, RR):
        M = len(L)
        L.sort(key=lambda x: x[1])
        q = []
        j = 0
        c = 0
        for i in range(N):
            while j < M and L[j][1] <= i:
                heappush(q, L[j])
                j += 1
            while q and c <= i:
                _, k, l, r = heappop(q)
                if c <= k:
                    ans += l
                else:
                    ans += r
                c += 1
    return ans

if __name__ == '__main__':
    input = sys.stdin.readline
    T = int(input())
    for i in range(T):
        N = int(input())
        KLR = [tuple(map(int, input().split())) for _ in range(N)]
        ans = main(N, KLR)
        print(ans)

Submission status: Wrong Answer

Solution code:
#!python3

import sys
iim = lambda: map(int, sys.stdin.readline().rstrip().split())
from heapq import heappush, heappushpop

def resolve():
    it = map(int, sys.stdin.read().split())

    T = next(it)
    ans = []
    for i in range(T):
        N = next(it)

        val = 0
        a1 = [[] for i in range(N)];
        a2 = [[] for i in range(N)];
        for i, v1, v2 in ((next(it)-1, next(it), next(it)) for i in range(N)):
            diff = v1 - v2
            if diff == 0 or i == N-1:
                val += v1
            elif diff > 0:
                a1[i].append(diff)
                val += v2
            else:
                i = N-1-i
                a2[i].append(-diff)
                val += v1

        for ax in (a1, a2):
            dq = []
            for i, aa in enumerate(ax):
                if not aa: continue
                for diff in aa:
                    ld = len(dq)
                    if ld <= i:
                        heappush(dq, diff)
                    else:
                        diff = heappushpop(dq, diff)

            val += sum(dq)

        ans.append(val)


    print(*ans, sep="\n")



if __name__ == "__main__":
    resolve()