#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds; 
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 
#define FAST std::ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pi       acos(-1)
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))  
#define REVERSE(v) reverse(ALL(v))
#define F first
#define S second
#define ppb pop_back
#define GCD(m,n) __gcd(m,n)
#define LCM(m,n) (m*n)/GCD(m,n)
#define rep(i,a,n)  for(int i=a ; i<n ; i++)
#define repe(i,a,n) for(int i=a;i<=n;i++)
#define rev(i,a,b) for(int i=a;i>=b;i--)
////-------------------------------------------------------------------------
#define ld long double 
#define int long long ////////////
////define ll long long
////--------------------------------------------------------------------------
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vvvi> vvvvi;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<vvc> vvvc;
typedef vector<pii > vpii;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef map<int,int> mii;
typedef unordered_map<int,int> umii;
typedef map<char,int> mci;
////--------------------------------------------------------------------------
#define itv for(auto it= v.begin(); it != v.end(); ++it )
#define present(container, element) (container.find(element) != container.end())
#define cpresent(container, element) (find(ALL(container),element) != container.end())
#define   bs binary_search
#define  lb(v,val)  lower_bound(ALL(v), val)
#define  ub(v,val)  upper_bound(ALL(v), val)
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define maxc(v) *max_element(ALL(v))
#define minc(v) *min_element(ALL(v))
#define isvowel(a) (a=='a'||a=='e'||a=='i'||a=='o'||a=='u')
////--------------------------------------------------------------------------
 /*
#define dbg1(a) cout<<" *"<<a<<" ";
#define dbg2(a,b) cout<<" *"<<a<<" **"<<b<<" "     /////
#define dbg cout<<"move"
*/
#define nl "\n"
#define vin(v,n)   vi v(n); rep(i,0,n)  cin>>v[i]
#define dbg cerr <<"At line "<<__LINE__<<" move "<<nl
#define dbg1(x) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<nl
#define dbg2(x,y) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<nl
#define dbg3(x,y,z) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<nl
#define prv(v)     rep(i,0,sz(v))  cerr<<v[i]<<" "; cerr<<endl
#define sz(s)         (int)((s).size())
#define coutsp(k)   cout<<setprecision(k)  //set precision (total length k icluding decimal and non decimal)
#define coutfsp(k)   cout<<fixed<<setprecision(k)  //fixed set precision(after decimal fixing)
#define trav(a, x) for(auto& a : x)
#define display(x) trav(a,x) cout<<a<<" ";cout<<endl
#define displaycerr(x) trav(a,x) cerr<<a<<" ";cerr<<endl

#define MOD 1000000007   
#define POD 998244353    
#define INF   (int)1e18   //------
//-------------------------------------------------------------------------------------------------

//--------------------------------Global VARIABLE--------------------------------------------------

//-------------------------------------------------------------------------------
void do2win(int &test)
{
  int n,m,k;  cin>>n>>m>>k;

  vpii both,a,b,z;

  int c1=0,c2=0;

  rep(i,0,n)
  {
    int t,x,y;  cin>>t>>x>>y;  

    if(x==1&&y==1)
    {
      both.pb(mp(t,i+1));
    }
    else if(x==1)
      a.pb(mp(t,i+1));
    else if(y==1)
      b.pb(mp(t,i+1));
    else
      z.pb(mp(t,i+1));

    if(x==1)
      c1++;
    if(y==1)
      c2++;

  }

  if(c1<k||c2<k)
  {
    cout<<-1 ;return;
  }

  SORT(both); SORT(a); SORT(b); SORT(z);

  int bon=sz(both);
  int an=sz(a);
  int bn=sz(b);
  int zn=sz(z);

  int preboth[bon+1]={},prea[an+1]={},preb[bn+1]={},prez[zn+1]={};

  rep(i,1,bon+1)
  {
    preboth[i]=preboth[i-1]+both[i-1].F;
  }
  rep(i,1,an+1)
  prea[i]=prea[i-1]+a[i-1].F;
  rep(i,1,bn+1)
  preb[i]=preb[i-1]+b[i-1].F;
  rep(i,1,zn+1)
  prez[i]=prez[i-1]+z[i-1].F;

  int ans=INF;

  int flag=0;
  int su=0;

  mii M;

  rep(i,0,min(m+1,bon+1))
  {
     int tl=m-i;
     int al=max(0ll,k-i);
     int bl=max(0ll,k-i);

     if(al>an||bl>bn)  continue;

     int sum=preboth[i]+preb[bl]+prea[al];
     tl=tl-al-bl;

     if(tl<0||tl>zn+(an-al)+(bn-bl))  continue;

     if(flag==0)
     {
      flag=1;
      vi op; rep(j,0,zn) op.pb(z[j].F);
      rep(j,al,an) op.pb(a[j].F);
      rep(j,bl,bn)  op.pb(b[j].F);
      SORT(op);

      rep(j,0,tl)
      {
        M[op[j]]++;
        su+=op[j];
      }
        ans=min(ans,sum+su);
     }

     else
     {
    al=k-i,bl=k-i;
    if(al<0||bl<0||al>=an||bl>=bn) continue;

     int p1=a[al].F,p2=b[bl].F;
     auto it=M.end(); it--;
     if((it->F)>p1)
     {
      M[p1]++;
      M[it->F]--;
      su-=(it->F)-p1;
      if(M[it->F]==0)
        M.erase(it);
     }
     it=M.end(); it--;
     if((it->F)>p2)
     {
      M[p2]++;
      M[it->F]--;
      su-=(it->F)-p2;
      if(M[it->F]==0)
        M.erase(it);
     }
        ans=min(ans,sum+su);

      }


  }

  if(ans==INF)
    cout<<-1;
 
  cout<<ans;
  cout<<nl;

  vi ind;

  flag=0;
  su=0;
  map<pii,int> M1;

    rep(i,0,min(m+1,bon+1))
  {
     int tl=m-i;
     int al=max(0ll,k-i);
     int bl=max(0ll,k-i);

     if(al>an||bl>bn)  continue;

     int sum=preboth[i]+preb[bl]+prea[al];
     tl=tl-al-bl;

     if(tl<0||tl>zn+(an-al)+(bn-bl))  continue;

     if(flag==0)
     {
      flag=1;
      vpii op; rep(j,0,zn) op.pb(z[j]);
      rep(j,al,an) op.pb(a[j]);
      rep(j,bl,bn)  op.pb(b[j]);
      SORT(op);

      rep(j,0,tl)
      {
        M1[op[j]]++;
        su+=op[j].F;
      }

      if(ans==(sum+su)){
        rep(j,0,tl)
        ind.pb(op[j].S);
        rep(j,0,i) ind.pb(both[j].S);
        rep(j,0,al) ind.pb(a[j].S);
        rep(j,0,bl) ind.pb(b[j].S);
        break;
      }
        
     }

     else
     {
    al=k-i,bl=k-i;
    if(al<0||bl<0||al>=an||bl>=bn) continue;

     int p1=a[al].F,p2=b[bl].F;
     auto it=M1.end(); it--;
     if((it->F).F>p1)
     {
      M1[a[al]]++;
      M1[it->F]--;
      su-=(it->F).F-p1;
      if(M1[it->F]==0)
        M1.erase(it);
     }
     it=M1.end(); it--;
     if((it->F).F>p2)
     {
      M1[b[bl]]++;
      M1[it->F]--;
      su-=(it->F).F-p2;
      if(M1[it->F]==0)
        M1.erase(it);
     }
        
            if(ans==(sum+su)){
        rep(j,0,i) ind.pb(both[j].S);
        rep(j,0,al) ind.pb(a[j].S);
        rep(j,0,bl) ind.pb(b[j].S);
        for(auto& k: M1)
          ind.pb((k.F).S);
        break;
      }


      }


  }

  rep(i,0,sz(ind))
  cout<<ind[i]<<" ";

  

}
//-------------------------------------------------------------------------------
 
signed main()
{

   #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt","w", stderr);
   #endif

   // #ifndef ONLINE_JUDGE
   //  cerr<<(1000*clock())/CLOCKS_PER_SEC<<"ms";
   // #endif      

///+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   FAST; 
cout<<fixed<<setprecision(20);
///+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   
   int test;  test=1;    
///////---------------------------------|||||
            // cin>>test;        ////////>>>>>>>
//////----------------------------------|||||

    for(int i=1;i<=test;i++)
    {
       // cout<<"Case #"<<i<<": ";

      do2win(i);
      cout<<nl;   //  IMPORTANT FOR NEXT LINE     
    }

   return 0;
}
