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:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


def init(bit, values):
    for i,v in enumerate(values):
        add(bit,i+1,v)
#a1 ~ aiまでの和 O(logn)
def query(bit,i):
    res = 0
    while i > 0:
        res += bit[i]
        i -= i&(-i)
    return res

#ai += x(logN)
def add(bit,i,x):
    if i==0:
        raise RuntimeError
    while i <= len(bit)-1:
        bit[i] += x
        i += i&(-i)
    return


t = int(input())
ANS = [None]*t
from collections import defaultdict
from heapq import heappop as hpp, heappush as hp
for iii in range(t):
    n = int(input())
    lr = [None]*n
    # d1 = defaultdict(list)
    # d0 = defaultdict(list)
    for i in range(n):
        k,l,r = list(map(int, input().split()))
        lr[i] = (l - r, k, l, r, i)
        # if l-r>=0:
        #     d1[k].append((-l+r, l, r, i))
        # else:
        #     d0[k].append((l-r, l, r, i))
            
    lr1 = [item for item in lr if item[0]>=0]
    lr0 = [item for item in lr if item[0]<0]
    lr1.sort(reverse=True)
    lr0.sort()

    bit1 = [0]*(n+1)
    bit0 = [0]*(n+1)
    sl = set()
    sr = set()
    h = []
    for val, k, l, r, i in lr1:
        if k==n:
            sl.add(i)
        elif query(bit1, k)<k:
            sl.add(i)
            add(bit1, k, 1)
        else:
            sr.add(i)
    total = 0
    for val, k, l, r, i in lr0:
        if k==n:
            sl.add(i)
        else:
            if total-query(bit0, k)<(n-k):
                sr.add(i)
                add(bit0, k, 1)
                total += 1
            else:
                sl.add(i)
    ans = 0
#     print(lr0, lr1, sl, sr)
    for i in sl:
        ans += lr[i][-3]
    for i in sr:
        ans += lr[i][-2]
    ANS[iii] = ans
write("\n".join(map(str, ANS)))

Submission status: Wrong Answer

Solution code:
import heapq
t = int(input())
for i in range(t):
    n = int(input())
    ans = 0
    cl,cr = 0,0
    lisl = []
    lisr = []
    rs = 0
    ls = 0
    for i in range(n):
        k,l,r = map(int,input().split())
        a = r-l
        s = (l>r)
        if a<0:
            lisl.append([k,l,r,a,s])
            rs += r
        else:
            lisr.append([k,l,r,-a,s])
            ls += l
    lisl.sort(key=lambda x: x[3])
    lisl.sort(key=lambda x: x[0])
    lisr.sort(key=lambda x: x[3])
    lisr.sort(key=lambda x: x[0],reverse=1)
    # print(lis)
    h = []
    cntl = 0
    cntr = 0
    for i in lisl:
        heapq.heappush(h,i[3])
        cntl += 1
        if cntl > i[0]:
            heapq.heappop(h)
            cntl -= 1
    # print(h)
    hr = []
    for i in lisr:
        heapq.heappush(hr,i[3])
        cntr += 1
        if cntr > n-i[0]:
            heapq.heappop(hr)
            cntr -= 1
    # print(hr)
    print(rs-sum(h)+ls-sum(hr))

Submission status: Wrong Answer

Solution code:
import heapq
t = int(input())
for i in range(t):
    n = int(input())
    ans = 0
    cl,cr = 0,0
    lisl = []
    lisr = []
    rs = 0
    ls = 0
    for i in range(n):
        k,l,r = map(int,input().split())
        a = r-l
        s = (l>r)
        if a<0:
            lisl.append([k,l,r,a,s])
            rs += r
        else:
            lisr.append([k,l,r,-a,s])
            ls += l
    lisl.sort(key=lambda x: x[3])
    lisl.sort(key=lambda x: x[0])
    lisr.sort(key=lambda x: x[3])
    lisr.sort(key=lambda x: x[0],reverse=1)
    # print(lis)
    h = []
    cntl = 0
    cntr = 0
    for i in lisl:
        heapq.heappush(h,i[3])
        cntl += 1
        if cntl > i[0]:
            heapq.heappop(h)
            cntr -= 1
    # print(h)
    hr = []
    for i in lisr:
        heapq.heappush(hr,i[3])
        cntr += 1
        if cntr > n-i[0]:
            heapq.heappop(hr)
            cntr -= 1
    # print(hr)
    print(rs-sum(h)+ls-sum(hr))

Submission status: Wrong Answer

Solution code:
import sys
from operator import itemgetter
from heapq import heappush, heappop
input = sys.stdin.readline


def simulate(camels):
    camels.sort(key=itemgetter(0))
    queue = []
    ans = 0
    for k, l, r in camels:
        ans += l
        if len(queue) < k:
            # ok
            heappush(queue, l - r)
        else:
            heappush(queue, l - r)
            d = heappop(queue)
            ans -= d
    return ans


def solve():
    n = int(input())
    camels = [tuple(map(int, input().split())) for _ in range(n)]

    ans = 0
    camels_l = []
    camels_r = []
    for k, l, r in camels:
        if k == n or l == r:
            ans += l
        elif l > r:
            camels_l.append((k, l, r))
        else:
            # r > l
            camels_r.append((n + 1 - k, r, l))

    ans += simulate(camels_l)
    ans += simulate(camels_r)

    print(ans)


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

Submission status: Wrong Answer

Solution code:
import heapq
t = int(input())
for i in range(t):
    n = int(input())
    ans = 0
    cl,cr = 0,0
    lisl = []
    lisr = []
    rs = 0
    ls = 0
    for i in range(n):
        k,l,r = map(int,input().split())
        a = r-l
        s = (l>r)
        if a<0:
            lisl.append([k,l,r,a,s])
            rs += r
        else:
            lisr.append([k,l,r,-a,s])
            ls += l
    lisl.sort(key=lambda x: x[3])
    lisl.sort(key=lambda x: x[0])
    lisr.sort(key=lambda x: x[3])
    lisr.sort(key=lambda x: x[0],reverse=1)
    # print(lis)
    h = []
    cntl = 0
    cntr = 0
    for i in lisl:
        heapq.heappush(h,i[3])
        cntl += 1
        if cntl > i[0]:
            heapq.heappop(h)
    # print(h)
    hr = []
    for i in lisr:
        heapq.heappush(hr,i[3])
        cntr += 1
        if cntr > n-i[0]:
            heapq.heappop(hr)
    # print(hr)
    print(rs-sum(h)+ls-sum(hr))

Submission status: Wrong Answer

Solution code:
class Node:
    def __init__(self, i, p, c):
        self.i = i
        if i != 0:
            self.p = p
        else:
            self.p = None
        self.c = [c]


class CamelSort:
    def __init__(self):
        self.all_camels = []
        self.g = []

        self.n = int(input())
        self.k, self.l, self.r = [], [], []

        self.lefts = []
        self.rights = []

        for i in range(self.n):
            _k, _l, _r = map(int, input().split())
            self.l.append(_l)
            self.r.append(_r)
            if _l > _r:
                self.k.append(_k - 1)
                self.lefts.append((i, _r, _l - _r))
            else:
                self.k.append(self.n - (_k) - 1)
                self.rights.append((i, _l, _r - _l))
        self.lefts = sorted(self.lefts, key=lambda r: r[1], reverse=True)
        self.rights = sorted(self.rights, key=lambda r: r[1], reverse=True)

        self.ans = self.get_ans(self.lefts) + self.get_ans(self.rights)

    def insert_camel(self, _k):
        if _k == -1:
            return False
        i = _k
        while i is not None:
            # print(i, self.all_camels, self.n)
            # print(self.g)
            if self.all_camels[i] == 0:
                self.all_camels[i] = 1
                p = self.g[i].p
                for c in self.g[i].c:
                    if c is None: break
                    self.g[c].p = p
                self.g[i].c = None
                return True
            i = self.g[i].p
        return False

    def get_ans(self, camels):
        res = []
        self.all_camels = [0 for _ in range(self.n)]
        self.g = [Node(i, i - 1, i + 1) if i != self.n - 1 else Node(i, i - 1, None)
                  for i in range(self.n)]
        for i, base, diff in camels:
            if self.insert_camel(self.k[i]):
                res.append(base + diff)
            else:
                res.append(base)
        return sum(res)


def main():
    t = int(input())
    for _ in range(t):
        print(CamelSort().ans)


main()

Submission status: Wrong Answer

Solution code:
class Node:
    def __init__(self, i, p, c):
        self.i = i
        if i != 0:
            self.p = p
        else:
            self.p = None
        self.c = [c]


class CamelSort:
    def __init__(self):
        self.all_camels = []
        self.g = []

        self.n = int(input())
        self.k, self.l, self.r = [], [], []

        self.lefts = []
        self.rights = []

        for i in range(self.n):
            _k, _l, _r = map(int, input().split())
            self.l.append(_l)
            self.r.append(_r)
            if _l > _r:
                self.k.append(_k - 1)
                self.lefts.append((i, _r, _l - _r))
            else:
                self.k.append(self.n - (_k) - 1)
                self.rights.append((i, _l, _r - _l))
        self.lefts = sorted(self.lefts, key=lambda r: r[1], reverse=True)
        self.rights = sorted(self.rights, key=lambda r: r[1], reverse=True)

        self.ans = self.get_ans(self.lefts) + self.get_ans(self.rights)

    def insert_camel(self, _k):
        if _k == -1:
            return False
        i = _k
        while i is not None:
            # print(i, self.all_camels, self.n)
            # print(self.g)
            if self.all_camels[i] == 0:
                self.all_camels[i] = 1
                p = self.g[i].p
                for c in self.g[i].c:
                    if c is None: break
                    self.g[c].p = p
                return True
            i = self.g[i].p
        return False

    def get_ans(self, camels):
        res = []
        self.all_camels = [0 for _ in range(self.n)]
        self.g = [Node(i, i - 1, i + 1) if i != self.n - 1 else Node(i, i - 1, None)
                  for i in range(self.n)]
        for i, base, diff in camels:
            if self.insert_camel(self.k[i]):
                res.append(base + diff)
            else:
                res.append(base)
        return sum(res)


def main():
    t = int(input())
    for _ in range(t):
        print(CamelSort().ans)


main()

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)]
    klr.sort(key = lambda x:max(x[0], x[1]), reverse=True)
    lst1 = [inf] * n
    lst2 = [0] * n
    used = [False] * n
    ans = 0
    h = []
    for i, [k, l, r] in enumerate(klr):
        heappush(h, (-r, l, i, k, 1))
        heappush(h, (-l, r, i, k, 0))
    while h:
        x, y, idx, k, flag = heappop(h)
        x = -x
        if used[idx]:
            continue
        if flag:
            r = bl(lst2, x)
            if r > k:
                lst2[r-1] = x
                used[idx] = True
                ans += x
        else:
            l = br(lst1, -x)
            if l <= k:
                lst1[l-1] = -x
                used[idx] = True
                ans += x
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


def init(bit, values):
    for i,v in enumerate(values):
        add(bit,i+1,v)
#a1 ~ aiまでの和 O(logn)
def query(bit,i):
    res = 0
    while i > 0:
        res += bit[i]
        i -= i&(-i)
    return res

#ai += x(logN)
def add(bit,i,x):
    if i==0:
        raise RuntimeError
    while i <= len(bit)-1:
        bit[i] += x
        i += i&(-i)
    return


t = int(input())
ANS = [None]*t
from collections import defaultdict
from heapq import heappop as hpp, heappush as hp
for iii in range(t):
    n = int(input())
    lr = [None]*n
    d1 = defaultdict(list)
    d0 = defaultdict(list)
    for i in range(n):
        k,l,r = list(map(int, input().split()))
        lr[i] = (l - r, k, l, r, i)
        if l-r>=0:
            d1[k].append((-l+r, l, r, i))
        else:
            d0[k].append((-l+r, l, r, i))
            
    sl = set()
    sr = set()
    h = []
    for i in range(1,n+1):
        if i not in d1:
            continue
        for item in d1[i]:
            hp(h, item)
        while i<len(h):
            _,_,_,ii = hpp(h)
            sr.add(ii)
    for _,_,_,i in h:
        sl.add(i)
    h = []
    for i in range(n, 0, -1):
        if i not in d0:
            continue
        for item in d0[i]:
            hp(h,item)
        while (n-i)<len(h):
            _,_,_,ii = hpp(h)
            sl.add(ii)
    for *_,i in h:
        sr.add(i)
    ans = 0
#     print(lr0, lr1, sl, sr)
    for i in sl:
        ans += lr[i][-3]
    for i in sr:
        ans += lr[i][-2]
    ANS[iii] = ans
write("\n".join(map(str, ANS)))

Submission status: Wrong Answer

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


def init(bit, values):
    for i,v in enumerate(values):
        add(bit,i+1,v)
#a1 ~ aiまでの和 O(logn)
def query(bit,i):
    res = 0
    while i > 0:
        res += bit[i]
        i -= i&(-i)
    return res

#ai += x(logN)
def add(bit,i,x):
    if i==0:
        raise RuntimeError
    while i <= len(bit)-1:
        bit[i] += x
        i += i&(-i)
    return


t = int(input())
ANS = [None]*t
from collections import defaultdict
from heapq import heappop as hpp, heappush as hp
for iii in range(t):
    n = int(input())
    lr = [None]*n
    d1 = defaultdict(list)
    d0 = defaultdict(list)
    for i in range(n):
        k,l,r = list(map(int, input().split()))
        lr[i] = (l - r, k, l, r, i)
        if l-r>=0:
            d1[k].append((-l+r, l, r, i))
        else:
            d0[k].append((l-r, l, r, i))
            
    sl = set()
    sr = set()
    h = []
    for i in range(1,n+1):
        if i not in d1:
            continue
        for item in d1[i]:
            hp(h, item)
        while i<len(h):
            _,_,_,ii = hpp(h)
            sr.add(ii)
    for _,_,_,i in h:
        sl.add(i)
    h = []
    for i in range(n, 0, -1):
        if i not in d0:
            continue
        for item in d0[i]:
            hp(h,item)
        while (n-i)<len(h):
            _,_,_,ii = hpp(h)
            sl.add(ii)
    for *_,i in h:
        sr.add(i)
    ans = 0
#     print(lr0, lr1, sl, sr)
    for i in sl:
        ans += lr[i][-3]
    for i in sr:
        ans += lr[i][-2]
    ANS[iii] = ans
write("\n".join(map(str, ANS)))

Submission status: Wrong Answer

Solution code:
import heapq
t = int(input())
for i in range(t):
    n = int(input())
    ans = 0
    cl,cr = 0,0
    lisl = []
    lisr = []
    rs = 0
    ls = 0
    for i in range(n):
        k,l,r = map(int,input().split())
        a = r-l
        s = (l>r)
        if a<0:
            lisl.append([k,l,r,a,s])
            rs += r
        else:
            lisr.append([k,l,r,-a,s])
            ls += l
    lisl.sort(key=lambda x: x[3])
    lisl.sort(key=lambda x: x[0])
    lisr.sort(key=lambda x: x[3])
    lisr.sort(key=lambda x: x[0],reverse=1)
    # print(lis)
    h = []
    for i in lisl:
        heapq.heappush(h,i[3])
        h = h[:i[0]]
        # print(h)
    hr = []
    for i in lisr:
        heapq.heappush(hr,i[3])
        hr = hr[:n-i[0]]
        # print(hr)
    print(rs-sum(h)+ls-sum(hr))

Submission status: Wrong Answer

Solution code:
import heapq
t = int(input())
for i in range(t):
    n = int(input())
    ans = 0
    cl,cr = 0,0
    lisl = []
    lisr = []
    rs = 0
    ls = 0
    for i in range(n):
        k,l,r = map(int,input().split())
        a = r-l
        s = (l>r)
        if a<0:
            lisl.append([k,l,r,a,s])
            rs += r
        else:
            lisr.append([k,l,r,-a,s])
            ls += l
    lisl.sort(key=lambda x: x[3])
    lisl.sort(key=lambda x: x[0])
    lisr.sort(key=lambda x: x[3],reverse=1)
    lisr.sort(key=lambda x: x[0],reverse=1)
    # print(lis)
    h = []
    for i in lisl:
        heapq.heappush(h,i[3])
        h = h[:i[0]]
        # print(h)
    hr = []
    for i in lisr:
        heapq.heappush(hr,i[3])
        hr = hr[:n-i[0]]
        # print(hr)
    print(rs-sum(h)+ls-sum(hr))

Submission status: Wrong Answer

Solution code:
import heapq
T=int(input())
for t in range(T):
    N=int(input())
    ListL=[]
    ListR=[]
    ans=0
    for n in range(N):
        k,l,r=map(int,input().split())
        ans+=min(l,r)
        if min(l,r)==r:
            ListL.append([k-1,abs(l-r)])
        else:
            ListR.append([k-1,abs(l-r)])
    ListL=sorted(ListL,key=lambda x: x[0])
    ListR=sorted(ListR,key=lambda x: x[0])
    S=[]
    for j in range(N):
        for s in range(len(S),len(ListL)):
            if ListL[s][0]==j:
                heapq.heappush(S,ListL[s][1])
            else:
                break
        while len(S)>j+1:
            heapq.heappop(S) 
    ans+=sum(S)

    S=[]
    for j in range(N):
        for s in range(len(S),len(ListR)):
            if ListR[s][0]==j:
                heapq.heappush(S,ListR[s][1])
            else:
                break
        while len(S)>j+1:
            heapq.heappop(S) 
    ans+=sum(S)

    print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import *
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

for _ in range(II()):
    n=II()
    ff=[]
    bb=[]
    ans=0
    for _ in range(n):
        k,l,r=MI()
        if l>r:ff.append((k,l,r))
        elif l<r:bb.append((k,l,r))
        else:ans+=r

    ff.sort()
    hp=[]
    for i,(k,l,r) in enumerate(ff):
        heappush(hp,l-r)
        ans+=l
        if i+1<len(ff) and ff[i+1][0]==k:continue
        while len(hp)>k:
            d=heappop(hp)
            ans-=d

    bb.sort(reverse=True)
    hp = []
    for i, (k, l, r) in enumerate(bb):
        k=n-k
        heappush(hp, r-l)
        ans += r
        if i + 1 < len(bb) and bb[i + 1][0] == k: continue
        while len(hp) > k:
            d = heappop(hp)
            ans -= d

    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import heapq


input = sys.stdin.readline

T = int(input())

def solve():
    N = int(input())
    ls = []
    rs = []
    heapq.heapify(ls)
    heapq.heapify(rs)
    for _ in range(N):
        k, l, r = map(int, input().split())
        if l - r >=0:
            heapq.heappush(ls, (k, l, r))
        else:
            heapq.heappush(rs, (N-k, l, r))

    ans = 0
    ls_l = []
    heapq.heapify(ls_l)
    while len(ls) != 0:
        k, l, r = heapq.heappop(ls)
        heapq.heappush(ls_l, (r - l))
        ans += r
        while len(ls_l) > k:
            heapq.heappop(ls_l)

    rs_r = []
    heapq.heapify(rs_r)
    while len(rs) != 0:
        k, l, r = heapq.heappop(rs)
        heapq.heappush(rs_r, (l - r))
        ans += l
        while len(rs_r) > k:
            heapq.heappop(rs_r)
    while len(ls_l) != 0:
        ans -= heapq.heappop(ls_l)
    while len(rs_r) != 0:
        ans -= heapq.heappop(rs_r)
    print(ans)


for _ in range(T):
    solve()

Submission status: Wrong Answer

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


def init(bit, values):
    for i,v in enumerate(values):
        add(bit,i+1,v)
#a1 ~ aiまでの和 O(logn)
def query(bit,i):
    res = 0
    while i > 0:
        res += bit[i]
        i -= i&(-i)
    return res

#ai += x(logN)
def add(bit,i,x):
    if i==0:
        raise RuntimeError
    while i <= len(bit)-1:
        bit[i] += x
        i += i&(-i)
    return


t = int(input())
ANS = [None]*t
from collections import defaultdict
from heapq import heappop as hpp, heappush as hp
for iii in range(t):
    n = int(input())
    lr = [None]*n
    # d1 = defaultdict(list)
    # d0 = defaultdict(list)
    for i in range(n):
        k,l,r = list(map(int, input().split()))
        lr[i] = (l - r, k, l, r, i)
        # if l-r>=0:
        #     d1[k].append((-l+r, l, r, i))
        # else:
        #     d0[k].append((l-r, l, r, i))
            
    lr1 = [item for item in lr if item[0]>=0]
    lr0 = [item for item in lr if item[0]<0]
    lr1.sort(reverse=True)
    lr0.sort()

    bit1 = [0]*(n+1)
    bit0 = [0]*(n+1)
    sl = set()
    sr = set()
    h = []
    for val, k, l, r, i in lr1:
        if k==n:
            sl.add(i)
        elif query(bit1, k)<k:
            sl.add(i)
            add(bit1, k, 1)
        else:
            sr.add(i)
    total = 0
    for val, k, l, r, i in lr0:
        if k==n:
            sl.add(i)
        else:
            if total-query(bit0, k)<(n-k):
                sr.add(i)
                add(bit0, k+1, 1)
                total += 1
            else:
                sl.add(i)
    ans = 0
#     print(lr0, lr1, sl, sr)
    for i in sl:
        ans += lr[i][-3]
    for i in sr:
        ans += lr[i][-2]
    ANS[iii] = ans
write("\n".join(map(str, ANS)))

Submission status: Wrong Answer

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


def init(bit, values):
    for i,v in enumerate(values):
        add(bit,i+1,v)
#a1 ~ aiまでの和 O(logn)
def query(bit,i):
    res = 0
    while i > 0:
        res += bit[i]
        i -= i&(-i)
    return res

#ai += x(logN)
def add(bit,i,x):
    if i==0:
        raise RuntimeError
    while i <= len(bit)-1:
        bit[i] += x
        i += i&(-i)
    return


t = int(input())
ANS = [None]*t
from collections import defaultdict
from heapq import heappop as hpp, heappush as hp
for iii in range(t):
    n = int(input())
    lr = [None]*n
    # d1 = defaultdict(list)
    # d0 = defaultdict(list)
    for i in range(n):
        k,l,r = list(map(int, input().split()))
        lr[i] = (l - r, k, l, r, i)
        # if l-r>=0:
        #     d1[k].append((-l+r, l, r, i))
        # else:
        #     d0[k].append((l-r, l, r, i))
            
    lr1 = [item for item in lr if item[0]>=0]
    lr0 = [item for item in lr if item[0]<0]
    lr1.sort(reverse=True)
    lr0.sort()

    bit1 = [0]*(n+1)
    bit0 = [0]*(n+1)
    sl = set()
    sr = set()
    h = []
    for val, k, l, r, i in lr1:
        if k==n:
            sl.add(i)
        elif query(bit1, k)<k:
            sl.add(i)
            add(bit1, k, 1)
        else:
            sr.add(i)
    total = 0
    for val, k, l, r, i in lr0:
        if k==n:
            sl.add(i)
        else:
            if total-query(bit0, k)<(n-k):
                sr.add(i)
                add(bit0, k+1, 1)
            else:
                sl.add(i)
        total += 1
    ans = 0
#     print(lr0, lr1, sl, sr)
    for i in sl:
        ans += lr[i][-3]
    for i in sr:
        ans += lr[i][-2]
    ANS[iii] = ans
write("\n".join(map(str, ANS)))

Submission status: Wrong Answer

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


def init(bit, values):
    for i,v in enumerate(values):
        add(bit,i+1,v)
#a1 ~ aiまでの和 O(logn)
def query(bit,i):
    res = 0
    while i > 0:
        res += bit[i]
        i -= i&(-i)
    return res

#ai += x(logN)
def add(bit,i,x):
    if i==0:
        raise RuntimeError
    while i <= len(bit)-1:
        bit[i] += x
        i += i&(-i)
    return


t = int(input())
ANS = [None]*t
from collections import defaultdict
from heapq import heappop as hpp, heappush as hp
for iii in range(t):
    n = int(input())
    lr = [None]*n
    # d1 = defaultdict(list)
    # d0 = defaultdict(list)
    for i in range(n):
        k,l,r = list(map(int, input().split()))
        lr[i] = (l - r, k, l, r, i)
        # if l-r>=0:
        #     d1[k].append((-l+r, l, r, i))
        # else:
        #     d0[k].append((l-r, l, r, i))
            
    lr1 = [item for item in lr if item[0]>=0]
    lr0 = [item for item in lr if item[0]<0]
    lr1.sort(reverse=True)
    lr0.sort()

    bit1 = [0]*(n+1)
    bit0 = [0]*(n+1)
    sl = set()
    sr = set()
    h = []
    for val, k, l, r, i in lr1:
        if k==n:
            sl.add(i)
        elif query(bit1, k)<k:
            sl.add(i)
            add(bit1, k, 1)
        else:
            sr.add(i)
    total = 0
    for val, k, l, r, i in lr0:
        if k==n:
            sl.add(i)
        else:
            if total-query(bit0, k)<k:
                sr.add(i)
                add(bit0, k+1, 1)
            else:
                sl.add(i)
        total += 1
    ans = 0
#     print(lr0, lr1, sl, sr)
    for i in sl:
        ans += lr[i][-3]
    for i in sr:
        ans += lr[i][-2]
    ANS[iii] = ans
write("\n".join(map(str, ANS)))

Submission status: Wrong Answer

Solution code:
T = int(input())
for _ in range(T):
  N = int(input())
  base = 0
  A = []
  B = [0 for _ in range(N)]
  C =[]
  for i in range(N):
    K, L, R = map(int,input().split()) 
    tmp = min(L,R)
    L -= tmp
    R -= tmp
    if L > R:
      A.append((K,L,0))
    if R >= L:
      A.append((N-K,R,1))
    base += tmp
  A.sort(reverse = False)
  #print(base)
  #print(A)
  
  l = 0
  r = N-1
  for a in A:
    if a[2] == 0:
      if l < a[0]:
        l += 1
        base += a[1] 
    else:
      if r >= N-a[0]:
        r -= 1
        base += a[1]
  print(base)
  
#print(A)

Submission status: Wrong Answer

Solution code:
import heapq
T=int(input())
for case in range(T):
    N=int(input())
    KLR=[]
    ans=0
    for i in range(N):
        k,l,r=map(int,input().split())
        ans+=min(l,r)
        KLR.append((k,l,r))
    KLR.sort(key=lambda x:x[0])
    KLR.append((N+1,0,0))
    q=[]
    index=0
    for j in range(1,N+1):
        while(1):
            k,l,r=KLR[index]
            if k==j:
                heapq.heappush(q,(l-r,k,l,r))
                index+=1
            else:
                break
        while(len(q)>j):
            lr,k,l,r=heapq.heappop(q)
    while(len(q)>0):
        lr,k,l,r=heapq.heappop(q)
        ans+=abs(lr)
    print(ans)

Submission status: Wrong Answer

Solution code:
t = int(input())
for i in range(t):
    n = int(input())
    ans = 0
    cl,cr = 0,0
    lis = []
    for i in range(n):
        k,l,r = map(int,input().split())
        a = abs(l-r)
        s = (l>r)
        lis.append([k,l,r,a,s])
    lis.sort(key=lambda x: x[3], reverse=True)
    for i in lis:
        if i[4] == 1:
            if cl<i[0]:
                ans += i[1]
            else:
                ans += i[2]
        else:
            if n-cr > i[0]:
                ans += i[2]
            else:
                ans += i[1]
    print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
T = int(input())
for _ in range(T):
   N = int(input())
   left, right = [], []
   ans = 0
   for _ in range(N):
      k,l,r = map(int, input().split())
      if l > r:
         left.append((k-1,l,r))
      elif r > l:
         right.append((N-k-1,l,r))
      else:
         ans += r
   left = sorted(left, key=lambda x: x[0])
   right = sorted(right, key=lambda x: x[0])
   dl = []
   dr = []
   for i,(k,l,r) in enumerate(left):
      ans += l
      heapq.heappush(dl, r-l)
      if len(dl) > k+1:
         ans += heapq.heappop(dl)
   for i,(k,l,r) in enumerate(right):
      ans += r
      heapq.heappush(dr, l-r)
      if len(dr) > k+1:
         ans += heapq.heappop(dr)
   print(ans)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
class SegmentTree:
    def __init__(self, n, ele, segfun):
        self.ide_ele = ele
        self.segfun = segfun
        self.n = n
        self.N0 = 1 << n.bit_length()
        self.data = [self.ide_ele] * (self.N0 * 2)

    def update_add(self, l, r, val):
        l += self.N0
        r += self.N0
        while l < r:
            if l & 1:
                self.data[l] += val
                l += 1
            if r & 1:
                self.data[r - 1] += val
                r -= 1
            l //= 2
            r //= 2

    def query(self, i):
        i += len(self.data) // 2
        ret = self.data[i]
        while i > 0:
            i //= 2
            ret = self.segfun(ret, self.data[i])
        return ret

n = int(readline())
for _ in range(n):
  t = int(readline())
  chk = []
  for i in range(t):
    k,l,r = map(int,readline().split())
    chk.append((abs(l-r),l,r,k))
  chk.sort(reverse=True)
  S = SegmentTree(t,0,lambda a, b: a+b)
  E = SegmentTree(t,0,lambda a, b: a+b)
  ans = 0
  for c,l,r,k in chk:
    if k == t or l == r:
      ans += l
    elif l > r:
      if S.query(k-1) <= k-1:
        ans += l
        S.update_add(k-1,t,1)
      else:
        ans += r
    elif r > l:
      if E.query(k) <= t-k-1:
        ans += r
        E.update_add(0,k,1)
      else:
        ans += l
  print(ans)