#include<bits/stdc++.h>
#define ll long long int
#define pb push_back
#define mpr make_pair
#define mt make_tuple
#define rep(i, st, n) for (int i = st; i <= n; i++)
#define repv(i, st, n) for (int i = st; i >= n; i--)

// Important header files
#include <ext/pb_ds/assoc_container.hpp> // Common file  
#include <ext/pb_ds/tree_policy.hpp>  
#include <functional> // for less  
#include <iostream>  
using namespace __gnu_pbds;  
using namespace std;  
  
// Declaring ordered_set 
typedef tree<ll, null_type, less<ll>, rb_tree_tag,  
            tree_order_statistics_node_update>  
    ordered_set; 
// order_of_key (val): returns the no. of values less than val
// find_by_order (k): returns the kth largest element.(0-based)

#define TRACE
 
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

const ll INF64 = 1e16 + 10;
const ll INF32 = 1e9 + 10;
const ll N = 2e5 + 10;
const ll MOD = 1e9 + 7;
ll power(ll x, ll y, ll p){ ll res = 1; x = x % p;while (y > 0){ if (y & 1) res = (res*x) % p; y = y>>1; x = (x*x) % p;} return res; }

ll inp[N][3], used[N][2];
vector<pair<ll,ll>> a,b,c,arr,merged;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n,m,k,t,x,y,ans,temp,cum,ind;
    cin>>n>>m>>k;
    memset(used, 0, sizeof(used));
    memset(inp, 0, sizeof(inp));
    rep(i, 1, n){
        cin>>inp[i][0]>>inp[i][1]>>inp[i][2];
        if(inp[i][1] == 1 && inp[i][2] == 1)    a.pb(mpr(inp[i][0], i));
        else    merged.pb(mpr(inp[i][0], i));
    }
    sort(a.begin(), a.end());
    sort(merged.begin(), merged.end());
    x = 0;
    y = 0;
    rep(i, 0, merged.size() - 1){
        if(inp[merged[i].second][1] == 1){
            x++;
            b.pb(merged[i]);
        }
        else if(inp[merged[i].second][2] == 1){
            y++;
            c.pb(merged[i]);
        }    
        used[i+1][0] = x;
        used[i+1][1] = y;
    }
    rep(i, 1, merged.size() - 1) merged[i].first = merged[i-1].first + merged[i].first;
    rep(i, 1, (int)b.size() - 1) b[i].first = b[i-1].first + b[i].first;
    rep(i, 1, (int)c.size() - 1) c[i].first = c[i-1].first + c[i].first;
    ans = INF64;
    cum = 0;
    rep(i, 0, min(m, (ll)a.size())){
        cum += (i > 0) ? a[i - 1].first : 0;
        x = max(0ll, k - i);
        y = (m - i) - 2*x;
        if(y >= 0 && x < b.size() && x < c.size()){
            temp = cum;
            temp += ((y > 0) ? merged[y - 1].first : 0);
            if(x > 0){
                temp += b[x - 1].first - ((used[y][0] > 0) ? b[used[y][0] - 1].first : 0);
                temp += c[x - 1].first - ((used[y][1] > 0) ? c[used[y][1] - 1].first : 0);
            }
            if(temp <= ans)    ind = i;
            ans = min(ans, temp);
        }
    }
    if(ans < INF64){
        x = max(0ll, k - ind);
        y = (m - ind) - 2*x;
        cout<<ans<<endl;
        rep(i, 0, ind - 1)  cout<<a[i].second<<" ";
        rep(i, 0, y - 1)    cout<<merged[i].second<<" ";
        rep(i, used[y][0], x - 1)   cout<<b[i].second<<" ";
        rep(i, used[y][1], x - 1)   cout<<c[i].second<<" ";
        cout<<endl;
    }
    else    cout<<-1<<endl;
    return 0;
}