#include<bits/stdc++.h>
using namespace std;
#define int long long int
int min(int a,int b)
{ if(a<=b)
    return a;
    else
        return b;
}
signed main()
{ int n,m,k;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>m>>k;
vector<pair<int,int>> g1,g2,g3,g4;
int arr[n+10][3]={0};
int a,b,c;
for(int i=1;i<=n;i++)
    { cin>>a>>b>>c;
    arr[i][0]=a;
    arr[i][1]=b;
    arr[i][2]=c;
        if(b==1&&c==1)
            g3.push_back(make_pair(a,i));
        else if(b==1 && c==0)
            g1.push_back(make_pair(a,i));
        else if(b==0 && c==1)
            g2.push_back(make_pair(a,i));
        else
            g4.push_back(make_pair(a,i));
    }
    int zero=0;
    int l=max(zero,2*k-m),r=k;
    sort(g1.begin(),g1.end());
    sort(g2.begin(),g2.end());
    sort(g3.begin(),g3.end());
    sort(g4.begin(),g4.end());
    vector<int> sum1,sum2,sum3;

   for(int i=0;i<min(g1.size(),g2.size());i++)
   {   if(i==0)
   sum1.push_back(g1[0].first);
   else
     sum1.push_back(sum1[i-1]+g1[i].first);
   }
   for(int i=0;i<min(g1.size(),g2.size());i++)
   { if(i==0)
   sum2.push_back(g2[0].first);
   else
     sum2.push_back(sum2[i-1]+g2[i].first);
   }
   for(int i=0;i<g3.size();i++)
   { if(i==0)
   sum3.push_back(g3[0].first);
   else
     sum3.push_back(sum3[i-1]+g3[i].first);
   }
   int ans=INT_MAX,ind=-1;

 //cout<<l<<" "<<r<<" "<<g1.size()<<" "<<g2.size()<<" "<<g3.size()<<" "<<g4.size()<<endl;
   for(int i=l;i<=r;i++)
   {  int var=0;
   int i1=0,i2=0;
       if(i<=sum3.size())
       {if(i>0)
         {var+=sum3[i-1];
         }
         i1=i;

       }


       if(k-i<=sum2.size() && k-i<=sum1.size() )
       { if(k-i>0)
           {var+=sum2[k-i-1];
           var+=sum1[k-i-1];
           }
           i2=k-i;
       }


              if(var<ans && i1+2*i2<=m&& i1+i2==k)
              { ans=var;
                  ind=i;
              }
   }
   if(ind==-1)
   ind=0;

//cout<<ans<<" "<<ind<<"---------------------------------------------"<<endl;
   int n1=0,n2=0;
   vector<int> res;
   int ct=0,i;
   for( i=0;i<min(g3.size(),ind);i++)
   { ct++;

       res.push_back(g3[i].second);
   }
   for(i=min(g3.size(),ind);i<g3.size();i++)
    g4.push_back(make_pair(g3[i].first,g3[i].second));

    for( i=0;i<min(g2.size(),k-ind);i++)
   { ct++;

       res.push_back(g2[i].second);
   }
   for(i=min(g2.size(),k-ind);i<g2.size();i++)
    g4.push_back(make_pair(g2[i].first,g2[i].second));

    for( i=0;i<min(g1.size(),k-ind);i++)
   { ct++;

       res.push_back(g1[i].second);
   }
   for(i=min(g1.size(),k-ind);i<g1.size();i++)
    g4.push_back(make_pair(g1[i].first,g1[i].second));


    sort(g4.begin(),g4.end());
    int itr=0;
    while(ct<m && itr<g4.size())
    {   ct++;
        res.push_back(g4[itr].second);
        ans+=g4[itr].first;
        itr++;
    }
    n1=0;
    n2=0;
    ans=0;
    for(int i=0;i<res.size();i++)
    { int in=res[i];
        ans+=arr[in][0];
        if(arr[in][1]==1)
            n1++;
        if(arr[in][2]==1)
            n2++;
            if(n==243)
            { cout<<arr[in][0]<<" ";
                
            }
    }
    if(n==243)
    cout<<n1<<" "<<n2<<endl;
    if(n1<k || n2<k)
        ans=-1;
    cout<<ans<<endl;
    if(ans!=-1)
    { for(int i=0;i<m;i++)
        cout<<res[i]<<" ";


        cout<<endl;
    }
}
