import atexit, io, sys 
  
# A stream implementation using an in-memory bytes  
# buffer. It inherits BufferedIOBase. 
buffer = io.BytesIO() 
sys.stdout = buffer
  
# print via here 
@atexit.register 
def write(): 
    sys.__stdout__.write(buffer.getvalue())

for _ in range(1):
    n,m,k=map(int,raw_input().split())
    v=[]
    t=[]
    al=0
    pc=[]
    jt=[]
    for i in range(n):

        vc=map(int,raw_input().split())
        if vc[1] or vc[2]:
            v.append(vc+[i+1])
        else:
            jt.append([vc[0]]+[i+1])
    s=sorted(v)
    ac,bc,at,bt=0,0,0,0
    a,b=[],[]
    for i in s:
        if i[1]!=1 or i[2]!=1:
            if i[1]==1:
                ac+=1
                a.append([i[0],1+i[3]])
            else:
                bc+=1
                b.append([i[0],1+i[3]])
        else:
            t.append([i[0],1+i[3]])
    ok=1
    if len(t)>=k:
        p,g=0,k-1
        am=k
        ca=0
        for i in range(k):
            ca+=t[i][0]
        
        while( p<len(a) and p<len(b) and g>=0):
            if (a[p][0]+b[p][0])<=t[g][0]:
                ca=ca-t[g][0]+a[p][0]+b[p][0];p+=1;g-=1;am+=1
           
            else:
                break

        
        
        if am<m:
            gy=a[p:]+b[p:]+t[min(g+1,len(t)-1):]+jt
            gy=sorted(gy) 
            if (m-am) >len(gy):
                print -1
            else:
                for i in range(m-am):
                    ca+=gy[i][0]
                print ca
                ans=""
                p-=1;g+=1
                for  i in range(1+p):
                    ans+=str(a[i][1])
                    ans+=" "
                    ans+=str(b[i][1])
                    ans+=" "
                for  i in range(g):
                    ans+=str(t[i]);ans+=" "
                for  i in range(m-am):
                    ans+=str(gy[i][1])
                    ans+=" "
                print ans
        else:
            p-=1;g+=1
            while(p>=0 and g<len(t) and am>m):
                
                if p<0 or g==len(t):
                    print-1;ok=0;break
                ca=ca-a[p][0]-b[p][0]+t[g][0];am-=1
                p-=1;g+=1
      
            if ok:
                print ca
                p-=1;g+=1;
                for  i in range(p+1):
                    ans+=str(a[i][1])
                    ans+=" "
                    ans+=str(b[i][1])
                    ans+=" "
                for i in range(g):
                    ans+=str(t[i][1]);ans+=" "
                print ans
    elif( len(a)+len(t))<k or (len(b)+len(t)) <k:
        print -1;continue
        
    else :
        f=k-len(t)
        for i in range(f):
            ca=ca+a[i][0]+b[i][0]
        for i in range(len(t)):
            ca+=t[i][0]
        
        p,g=f,len(t)-1
        am=len(t)+2*f
        
        while( p<len(a) and p<len(b) and g>=0):
            if (a[p][0]+b[p])<=t[g][0]:
                ca=ca-t[g]+a[p]+b[p];p+=1;g-=1;am+=1
            else:
                break
        
        if am<m:
            gy=a[p:]+b[p:]+t[min(g+1,len(t)-1):]+jt
            gy=sorted(gy) 
            if (m-am) >len(gy):
                print -1
                break
            else:
                for  i in range(m-am):
                    ca+=gy[i][0]
                print ca
                p-=1;g+=1
                for  i in range(p+1):
                    ans+=str(a[i][1])
                    ans+=" "
                    ans+=str(b[i][1])
                    ans+=" "
                for i in range(g):
                    ans+=str(t[i][1]);ans+=" "
                for  i in range(m-am):
                    ans+=str(gy[i][1])
                    ans+=" "
                print ans,p,g
        else:
            p-=1;g+=1
            while(p>=0 and g<len(t) and am>m):
                
                if p<0 or p==min(len(a),len(b))or g==len(t):
                    print-1;ok=0;break
                ca=ca-a[p][0]-b[p][0]+t[g][0];am-=1 
                p-=1;g+=1
            if ok:
                print ca
                p-=1;g+=1
                for  i in range(p+1):
                    ans+=str(a[i][1])
                    ans+=" "
                    ans+=str(b[i][1])
                    ans+=" "
                for i in range(g):
                    ans+=str(t[i][1]);ans+=" "
                print ans
                
        