from heapq import *
import sys

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]

inf=10**16

def cumsum(aa):
    res=[0]
    resi=[]
    for a,i in aa:
        res.append(res[-1]+a)
        resi.append(i)
    return res,resi

def main():
    n,m,k=MI()
    tt=[[] for _ in range(4)]
    for i in range(n):
        t,a,b=MI()
        tt[a+b*2].append((t,i+1))

    cs = []
    idx=[]
    for i in range(4):
        tt[i].sort()
        ret0,ret1=cumsum(tt[i])
        cs.append(ret0)
        idx.append(ret1)
        tt[i].append((inf,-1))
    #print(cs)
    #print(idx)

    ans=inf
    ansi=[]
    n0=len(cs[0])
    n1=len(cs[1])
    n2=len(cs[2])
    n3=len(cs[3])
    mxc3=0
    for c3 in range(min(k+1,n3)):
        if c3<mxc3:continue
        cur=cs[3][c3]
        curi=idx[3][:c3]
        c1=c2=max(0,k-c3)
        if c1>n1-1 or c2>n2-1 or c1+c2+c3>m:continue
        cur+=cs[1][c1]+cs[2][c2]
        curi+=idx[1][:c1]+idx[2][:c2]
        c0=0
        hp=[]
        heappush(hp,(tt[0][c0],0))
        heappush(hp,(tt[1][c1],1))
        heappush(hp,(tt[2][c2],2))
        heappush(hp,(tt[3][c3],3))
        while c0+c1+c2+c3<m:
            (t,index),i=heappop(hp)
            cur+=t
            curi.append(index)
            if i==0:c0+=1;heappush(hp,(tt[0][c0],0))
            if i==1:c1+=1;heappush(hp,(tt[1][c1],1))
            if i==2:c2+=1;heappush(hp,(tt[2][c2],2))
            if i==3:c3+=1;heappush(hp,(tt[3][c3],3))
        #print(cur,c0,c1,c2,c3)
        mxc3=max(c3,mxc3)
        if cur<ans:
            ans=cur
            ansi=curi

    print(ans)
    print(*ansi)

main()