//                 _                      
//                (_)                     
// _ __ ___   __ _ _  ___  _ __ _ __ ___  
//| '_ ` _ \ / _` | |/ _ \| '__| '__/ _ \ 
//| | | | | | (_| | | (_) | |  | | | (_) |
//|_| |_| |_|\__,_| |\___/|_|  |_|  \___/ 
//               _/ |                     
//              |__/                      
#define _CRT_SECURE_NO_WARNINGS

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
  
typedef long long ll;
typedef unsigned long long ull;
typedef double dbl;
typedef long double ldbl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef string str;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vbl;
typedef tree<
    pll,
    null_type,
    less<pll>,
    rb_tree_tag,
    tree_order_statistics_node_update>
ordered_set;

#define majorro cout.precision(20); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define pb push_back
#define forn(i, n) for(ll (i) = 0; (i) < (n); ++(i))
#define fornm(i, m, n) for(ll (i) = (m); (i) < (n); ++(i))
#define rfornm(i, m, n) for(ll (i) = (m); (i) >= (n); --(i))
#define readvec(vector, n) {ll temp_vec_val;forn(inc, n){cin >> temp_vec_val;vector.push_back(temp_vec_val);}}
#define printvec(vector, delimeter) {ll length_of_vector=vector.size();\
                                    forn(elementvec, length_of_vector){cout << vector[elementvec] << delimeter;}}
#define printarr(arr, length, delimeter) {forn(elementarr, length){cout << arr[elementarr] << delimeter;}}
#define all(vector) (vector).begin(), (vector).end()
#define rall(vector) (vector).rbegin(), (vector).rend()
#define endl "\n"
  
struct pair_hash
{
    template <class T1, class T2>
    std::size_t operator() (const std::pair<T1, T2>& pair) const
    {
        return std::hash<T1>()(pair.first) ^ std::hash<T2>()(pair.second);
    }
};
 
const ldbl EPS = 1e-6;
const ll MOD = 1000000007;
// const ll MOD = 998244353;
// const ll MOD = 3037000499;
// const ull MOD = 1000000000000000003;
const ll INF = 1e18;
const ldbl pi = 2 * acos(0.0);
ll n, m, k, p, q, t, sum = 0, cnt= 0;
ll mx = -INF;
ll mn = INF;
bool flag = 0;
vll v;
str s = "", s1, s2;
char c;
 
void bad_solve(){}


void solve() //set.merge() -> c++17
//DELET DEBUG OUTPUT!11!1
//"YES"/"NO" OUTPUT!!!!
//__builtin_popcountll() -> __popcnt64() from <intrin.h>
{
    ll mm;
    cin >> n >> mm >> k;
    vector<pll> tw;
    vector<pll> al, bb, ze;
    set<ll> ans;
    forn(i, n)
    {
        cin >> p >> q >> m;
        if(q && !m) al.pb({p,i+1});
        else if(!q && m) bb.pb({p,i+1});
        else if(q&&m) tw.pb({p,i+1});
        else ze.pb({p,i+1});
    }
    sort(all(al));sort(all(bb));sort(all(tw));
    while((ll)al.size() > k)
    {
        ze.pb(al.back());
        al.pop_back();
    }
    while((ll)bb.size() > k)
    {
        ze.pb(bb.back());
        bb.pop_back();
    }
    forn(i, (ll)al.size())
    {
        sum += al[i].first;
        ans.insert(al[i].second);
        ++cnt;
    }
    forn(i, (ll)bb.size())
    {
        sum += bb[i].first;
        ans.insert(bb[i].second);
        ++cnt;
    }
    p = 0;
    n = tw.size();

    while(p < n && ((ll)al.size() < k || (ll)bb.size() < k))
    {
        sum += tw[p].first;
        ans.insert(tw[p].second);
        al.pb({-1,-1});
        bb.pb({-1,-1});
        ++cnt;
        ++p;
    }
    if((ll)al.size() < k || (ll)bb.size() < k) cout << -1;
    else
    {
        sort(all(al));sort(all(bb));
        while((ll)al.size() > k)
        {
            sum -= al.back().first;
            --cnt;
            ans.erase(al.back().second);
            ze.pb(al.back());
            al.pop_back();
        }
        while((ll)bb.size() > k)
        {
            sum -= bb.back().first;
            --cnt;
            ans.erase(bb.back().second);
            ze.pb(bb.back());
            bb.pop_back();
        }
        ll pa = al.size()-1, pbb = bb.size()-1; 
        while(p < n && (pa >= 0 || pbb >= 0))
        {
            if(pa >= 0 && pbb >= 0)
            {
                if(al[pa].first+bb[pbb].first >= tw[p].first || (cnt > mm &&  al[pa].first != -1 && bb[pbb].first != -1))
                {
                    sum -= al[pa].first+bb[pbb].first;
                    ans.erase(al[pa].second);
                    ans.erase(bb[pbb].second);
                    ze.pb(al[pa]);
                    ze.pb(bb[pbb]);
                    al[pa] = {-1,-1};
                    bb[pbb] = {-1,-1};
                    --cnt;
                    sum += tw[p].first;
                    ans.insert(tw[p].second);
                    --pa;
                    --pbb;
                    ++p;
                }
                else break;
            }
            else if(pa >= 0)
            {
                if(al[pa] > tw[p])
                {
                    sum -= al[pa].first;
                    ans.erase(al[pa].second);
                    sum += tw[p].first;
                    ans.insert(tw[p].second);
                    ze.pb(al[pa]);
                    al[pa] = {-1,-1};
                    --pa;
                    ++p;
                }
                else break;
            }
            else if(pbb >= 0)
            {
                if(bb[pbb] > tw[p])
                {
                    sum -= bb[pbb].first;
                    ans.erase(bb[pbb].second);
                    sum += tw[p].first;
                    ans.insert(tw[p].second);
                    ze.pb(bb[pbb]);
                    bb[pbb] = {-1,-1};
                    --pbb;
                    ++p;
                }
                else break;
            }
        }
        while(p < n)
        {
            ze.pb(tw[p]);
            ++p;
        }
        sort(all(ze));
        forn(i, ze.size())
        {
            if(cnt >= mm) break;
            ++cnt;
            ans.insert(ze[i].second);
            sum += ze[i].first;
        }
        if(cnt != mm) cout << -1; 
        else
        {
            cout << sum << endl;
            for(auto it : ans) cout << it << ' ';
        }
    }
    
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("F:\\repos\\projects\\contests_vsc\\in.txt", "r", stdin);
    freopen("F:\\repos\\projects\\contests_vsc\\out.txt", "w", stdout);
#endif
    majorro
    
    solve();
  
    // freopen("test.txt", "w", stdout);
    // bad_solve();
  
//  memset(v, INT_MAX, 1000*2000*sizeof(int));
  
    return 0;
}