Problem: Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.

Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.

There are n books in the family library. The i-th book is described by three integers: t_i — the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).

So they need to choose some books from the given n books in such a way that:

  * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; 
  * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). 



The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.

Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.

Input

The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 2 ⋅ 10^5).

The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≤ t_i ≤ 10^4, 0 ≤ a_i, b_i ≤ 1), where:

  * t_i — the amount of time required for reading the i-th book; 
  * a_i equals 1 if Alice likes the i-th book and 0 otherwise; 
  * b_i equals 1 if Bob likes the i-th book and 0 otherwise. 

Output

If there is no solution, print only one integer -1. Otherwise print one integer T — the minimum total reading time of the suitable set of books.

Examples

Input

8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0


Output

18


Input

5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0


Output

8


Input

5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1


Output

-1

Solution (in PYTHON3):

Solution:
#import math
#from functools import lru_cache
import heapq
#from collections import defaultdict
#from collections import Counter
#from collections import deque
#from sys import stdout
#from sys import setrecursionlimit
#setrecursionlimit(10**7)
from sys import stdin
input = stdin.readline

INF = 2*10**9 + 7
MAX = 10**7 + 7
MOD = 10**9 + 7

n, M, k = [int(x) for x in input().strip().split()]
c, a, b, u = [], [], [], []
for ni in range(n):
    ti, ai, bi = [int(x) for x in input().strip().split()]
    if(ai ==1 and bi == 1):
        c.append((ti, ni+1))
    elif(ai == 1):
        a.append((ti, ni+1))
    elif(bi == 1):
        b.append((ti, ni+1))
    else:
        u.append((ti, ni+1))
c.sort(reverse = True)
a.sort(reverse = True)
b.sort(reverse = True)
u.sort(reverse = True)
alen = len(a)
blen = len(b)
clen = len(c)
ulen = len(u)
#print(alen, blen, clen, ulen)
m = max(0, k - min(alen, blen), 2*k - M)
ans = 0
alist = []
adlist = []
#print(clen, m)
if(m>clen):
    print('-1')
else:
    for mi in range(m):
        cv, ci = c.pop()
        ans += cv
        heapq.heappush(alist, (-cv, ci))
    ka = k - m
    kb = k - m
    M -= m
    while(ka or kb):
        ca = (c[-1][0] if c else INF)
        da = 0
        ap, bp = 0, 0
        if(ka):
            da += (a[-1][0] if a else INF)
            ap = 1
        if(kb):
            da += (b[-1][0] if b else INF)
            bp = 1
        if(da<=ca and M>=2):
            ans += da
            if(ap):
                ka -= 1
                adlist.append(a[-1] if a else (INF, -1))
                if a: a.pop()
                M -= 1
            if(bp):
                kb -= 1
                adlist.append(b[-1] if b else (INF, -1))
                if b: b.pop()
                M -= 1
        else:
            ans += ca
            heapq.heappush(alist, (-c[-1][0], c[-1][1]) if c else (INF, -1))
            if c: c.pop()
            if(ap):
                ka -= 1
            if(bp):
                kb -= 1
            M -= 1
    #print('M and ans are', M, ans)
    if(M>(len(a) + len(c) + len(b) + len(u)) or ans>=INF):
        print('-1')
    else:
        heapq.heapify(c)
        while(M>0):
            #print('M and ans is : ', M, ans)
            if(u and u[-1][0] <= min(c[0][0] if c else INF, a[-1][0] if a else INF, b[-1][0] if b else INF)):
                ut, dt = 0, 0
                ut += (-alist[0][0] if alist else 0)
                ut += u[-1][0]
                dt += (a[-1][0] if a else INF)
                dt += (b[-1][0] if b else INF)
                if(ut<dt):
                    # add from ulist
                    upopped = u.pop()
                    adlist.append(upopped)
                    M -= 1
                    ans += upopped[0]
                else:
                    # remove from alist and add from ab
                    alpopped = (heapq.heappop(alist) if alist else (-INF, -1))
                    heapq.heappush(c, (-alpopped[0], alpopped[1]))
                    ans += alpopped[0]
                    bpopped = (b.pop() if b else (INF, -1))
                    apopped = (a.pop() if a else (INF, -1))
                    adlist.append(bpopped)
                    adlist.append(apopped)
                    ans += apopped[0]
                    ans += bpopped[0]
                    M -= 1
            else:
                # if c is less than a, b
                ct = (c[0][0] if c else INF)
                at, bt = (a[-1][0] if a else INF), (b[-1][0] if b else INF)
                abt = min(at, bt)
                if(ct<abt):
                    cpopped = (heapq.heappop(c) if c else (INF, -1))
                    heapq.heappush(alist, (-cpopped[0], cpopped[1]))
                    ans += cpopped[0]               
                    M-=1
                else:
                    # minimum is among a and b; straight forward
                    if(at<bt):
                        apopped = (a.pop() if a else (INF, -1))
                        adlist.append(apopped)
                        ans += apopped[0]
                    else:
                        bpopped = (b.pop() if b else (INF, -1))
                        adlist.append(bpopped)
                        ans += bpopped[0]
                    M-=1
            if(ans>=INF): break
        print(ans if ans<INF else '-1')
        if(ans < INF):
            for ai in adlist:
                print(ai[1], end = ' ')
            for ai in alist:
                print(ai[1], end = ' ')
            print('')