#include <bits/stdc++.h>
using namespace std;
#define int long long

const int inf=1e18;

int32_t main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int n,m,k,tot=0;
    vector <int> ans;
    cin >> n >> m >> k;
    vector <pair <int,int> > w,x,y,z;
    for(int i=0;i<n;i++){
        int t,p,q;
        cin >> t >> p >> q;
        if(p and q)w.push_back({t,i+1});
        else if(p)x.push_back({t,i+1});
        else if(q)y.push_back({t,i+1});
        else z.push_back({t,i+1});
    }
    sort(w.begin(),w.end());
    sort(x.begin(),x.end());
    sort(y.begin(),y.end());
    int st=max(0ll,2*k-m),sz=min((int)x.size(),(int)y.size());
    st=max(st,k-sz);
    st=max(st,(int)w.size()+m-n);
    if((int)w.size()<st){
        cout << -1;
        return 0;
    }
    for(int i=0;i<st;i++){
        tot+=w[i].first;
        ans.push_back(w[i].second);
    }
    int pos=st,en=0,a=st,b=st,l=0;
    multiset <int> s;
    while(b<k and en<sz){
        tot+=x[en].first+y[en].first;
        en++,a+=2,b++;
    }
    for(int i=en;i<x.size();i++)
        z.push_back(x[i]);
    for(int i=en;i<y.size();i++)
        z.push_back(y[i]);
    sort(z.begin(),z.end());
    while(a<m and l<z.size()){
        tot+=z[l].first;
        s.insert(z[l].first);
        a++,l++;
    }
    int res=tot,tmp=en,pe=en;
    if(a<m or b<k){
        cout << -1;
        return 0;
    }
    for(int i=st;i<w.size();i++){
        tot+=w[i].first,en--;
        s.insert(x[en].first);
        s.insert(y[en].second);
        tot-=*s.rbegin();
        auto it=s.end();
        it--;
        s.erase(it);
        tot-=*s.rbegin();
        it=s.end();
        it--;
        s.erase(it);
        if(tot<res)res=tot,pos=i+1,pe=en;
    }
    en=pe;
    cout << res << '\n';
    for(int i=0;i<pos;i++)
        cout << w[i].second << ' ';
    for(int i=0;i<en;i++)
        cout << x[i].second << ' ' << y[i].second << ' ';
    for(int i=en;i<tmp;i++)
        z.push_back(x[i]),z.push_back(y[i]);
    sort(z.begin(),z.end());
    for(int i=0;i<m-pos-2*en;i++)
        cout << z[i].second << ' ';
}


#include <bits/stdc++.h>
using namespace std;
#define int long long

const int inf=1e18;

int32_t main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int n,m,k,tot=0;
    vector <int> ans;
    cin >> n >> m >> k;
    vector <pair <int,int> > w,x,y,z;
    for(int i=0;i<n;i++){
        int t,p,q;
        cin >> t >> p >> q;
        if(p and q)w.push_back({t,i+1});
        else if(p)x.push_back({t,i+1});
        else if(q)y.push_back({t,i+1});
        else z.push_back({t,i+1});
    }
    sort(w.begin(),w.end());
    sort(x.begin(),x.end());
    sort(y.begin(),y.end());
    int st=max(0ll,2*k-m),sz=min((int)x.size(),(int)y.size());
    st=max(st,k-sz);
    if((int)w.size()<st){
        cout << -1;
        return 0;
    }
    for(int i=0;i<st;i++){
        tot+=w[i].first;
        ans.push_back(w[i].second);
    }
    int pos=st,en=0,a=st,b=st,l=0;
    multiset <int> s;
    while(b<k and en<sz){
        tot+=x[en].first+y[en].first;
        en++,a+=2,b++;
    }
    for(int i=en;i<x.size();i++)
        z.push_back(x[i]);
    for(int i=en;i<y.size();i++)
        z.push_back(y[i]);
    sort(z.begin(),z.end());
    while(a<m){
        tot+=z[l].first;
        s.insert(z[l].first);
        a++,l++;
    }
    int res=tot,tmp=en,pe=en;
    if(a<m or b<k){
        cout << -1;
        return 0;
    }
    for(int i=st;i<w.size();i++){
        tot+=w[i].first,en--;
        s.insert(x[en].first);
        s.insert(y[en].second);
        tot-=*s.rbegin();
        auto it=s.end();
        it--;
        s.erase(it);
        tot-=*s.rbegin();
        it=s.end();
        it--;
        s.erase(it);
        if(tot<res)res=tot,pos=i+1,pe=en;
    }
    en=pe;
    cout << res << '\n';
    for(int i=0;i<pos;i++)
        cout << w[i].second << ' ';
    for(int i=0;i<en;i++)
        cout << x[i].second << ' ' << y[i].second << ' ';
    for(int i=en;i<tmp;i++)
        z.push_back(x[i]),z.push_back(y[i]);
    sort(z.begin(),z.end());
    for(int i=0;i<m-pos-2*en;i++)
        cout << z[i].second << ' ';
}


#include <bits/stdc++.h>
using namespace std;
#define int long long

const int inf=1e18;

int32_t main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int n,m,k,tot=0;
    cin >> n >> m >> k;
    vector <pair <int,int> > v[4];
    for(int i=0;i<n;i++){
        int t,p,q;
        cin >> t >> p >> q;
        if(p and q)v[0].push_back({t,i+1});
        else if(p)v[1].push_back({t,i+1});
        else if(q)v[2].push_back({t,i+1});
        else v[3].push_back({t,i+1});
    }
    for(int i=0;i<4;i++)
        sort(v[i].begin(),v[i].end());
    int st=max(0ll,2*k-m),sz=min((int)v[1].size(),(int)v[2].size());
    st=max(st,k-sz);
    st=max(st,(int)v[0].size()+m-n);
    if((int)v[0].size()<st){
        cout << -1;
        return 0;
    }
    for(int i=0;i<st;i++){
        tot+=v[0][i].first;
    }
    int p[]={st,0,0,0},l=0;
    int a=st,b=st;
    while(b<k and l<sz){
        tot+=v[1][l].first+v[2][l].first;
        l++,a+=2,b++;
    }
    p[1]=p[2]=l;
    while(a<m){
        int mn=inf,id;
        for(int i=1;i<4;i++){
            if(p[i]==v[i].size())
                continue;
            if(v[i][p[i]].first<mn)mn=v[i][p[i]].first,id=i;
        }
        tot+=mn,p[id]++,a++;
    }
    int res=tot,bst[]={p[0],p[1],p[2],p[3]};
    if(a<m or b<k){
        cout << -1;
        return 0;
    }
    assert(a==m);
    for(int i=st;i<min(m,(int)v[0].size());i++){
        tot+=v[0][i].first,p[0]++;
        l--;
        if(i>=k){
            int mx=0,id;
            for(int i=1;i<4;i++){
                if(p[i]==0)
                    continue;
                if(v[i][p[i]-1].first>=mx)mx=v[i][p[i]-1].first,id=i;
            }
            p[id]--,tot-=mx;
            if(tot<res){
                res=tot;
                for(int j=0;j<4;j++)
                    bst[j]=p[j];
            }
            continue;
        }
        p[1]--,p[2]--;
        tot-=v[1][p[1]].first;
        tot-=v[2][p[2]].first;
        int mn=inf,id;
        for(int i=1;i<4;i++){
            if(p[i]==v[i].size())
                continue;
            if(v[i][p[i]].first<mn)mn=v[i][p[i]].first,id=i;
        }
        tot+=mn,p[id]++;
        if(tot<res){
            res=tot;
            for(int j=0;j<4;j++)
                bst[j]=p[j];
        }
    }
    cout << res << '\n';
    for(int i=0;i<4;i++){
        for(int j=0;j<bst[i];j++)
            cout << v[i][j].second << ' ' ;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define int long long

const int inf=1e18;

int32_t main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int n,m,k,tot=0;
    cin >> n >> m >> k;
    vector <pair <int,int> > v[4];
    for(int i=0;i<n;i++){
        int t,p,q;
        cin >> t >> p >> q;
        if(p and q)v[0].push_back({t,i+1});
        else if(p)v[1].push_back({t,i+1});
        else if(q)v[2].push_back({t,i+1});
        else v[3].push_back({t,i+1});
    }
    for(int i=0;i<4;i++)
        sort(v[i].begin(),v[i].end());
    int st=max(0ll,2*k-m),sz=min((int)v[1].size(),(int)v[2].size());
    st=max(st,k-sz);
    st=max(st,(int)v[0].size()+m-n);
    if((int)v[0].size()<st){
        cout << -1;
        return 0;
    }
    for(int i=0;i<st;i++){
        tot+=v[0][i].first;
    }
    int p[]={st,0,0,0},l=0;
    int a=st,b=st;
    // int pos=st,en=0,a=st,b=st,l=0;
    // multiset <int> s;
    while(b<k and l<sz){
        tot+=v[1][l].first+v[2][l].first;
        l++,a+=2,b++;
    }
    p[1]=p[2]=l;
    while(a<m){
        int mn=inf,id;
        for(int i=1;i<4;i++){
            if(p[i]==v[i].size())
                continue;
            if(v[i][p[i]].first<mn)mn=v[i][p[i]].first,id=i;
        }
        tot+=v[id][p[id]].first,p[id]++,a++;
    }
    int res=tot,bst[]={p[0],p[1],p[2],p[3]};
    if(a<m or b<k){
        cout << -1;
        return 0;
    }
    for(int i=st;i<min(m,(int)v[0].size());i++){
        tot+=v[0][i].first,p[0]++;
        p[1]--,p[2]--;
        tot-=v[1][p[1]].first;
        tot-=v[2][p[2]].first;
        int mn=inf,id;
        for(int i=1;i<4;i++){
            if(p[i]==v[i].size())
                continue;
            if(v[i][p[i]].first<mn)mn=v[i][p[i]].first,id=i;
        }
        tot+=v[id][p[id]].first,p[id]++;
        if(tot<res){
            res=tot;
            for(int i=0;i<4;i++)
                bst[i]=p[i];
        }
    }
    cout << res << '\n';
    for(int i=0;i<4;i++){
        for(int j=0;j<bst[i];j++)
            cout << v[i][j].second << ' ' ;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define int long long

const int inf=1e18;

int32_t main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int n,m,k,tot=0;
    cin >> n >> m >> k;
    int ar[n],res=0,p[4]={0};
    vector <int> v[4];
    set <int> ans;
    for(int i=0;i<n;i++){
        int p,q;
        cin >> ar[i] >> p >> q;
        v[2*p+q].push_back(i);
    }
    for(int i=0;i<4;i++){
        sort(v[i].begin(),v[i].end(),[&](int a,int b){
            return ar[a]<ar[b];
        });
    }
    for(int i=0;i<min((int)v[3].size(),k);i++)
        ans.insert(v[3][i]),p[3]++;
    int sz=min(v[1].size(),v[2].size());
    if((int)v[3].size()+sz<k){cout << -1;return 0;}
    for(int i=0;i<k-(int)v[3].size();i++){
        ans.insert(v[1][p[1]]),p[1]++;
        ans.insert(v[2][p[2]]),p[2]++;
    }
    int cnt=10;
    while((int)ans.size()<m){
        int mn=1e7,id=-1;
        for(int i=0;i<4;i++){
            if(p[i]==(int)v[i].size())continue;
            if(mn>ar[v[i][p[i]]])
                mn=ar[v[i][p[i]]],id=i;
        }
        if(p[3]>0 and p[1]<v[1].size() and p[2]<v[2].size()
        and ar[v[1][p[1]]]+ar[v[2][p[2]]]<ar[mn]+ar[v[3][p[3]-1]]){
            ans.erase(v[3][--p[3]]);
            ans.insert(v[1][p[1]++]);
            ans.insert(v[2][p[2]++]);
        }
        else
            ans.insert(v[id][p[id]++]);
    }
    if((int)ans.size()!=m){cout << -1;return 0;}
    int sum=0;
    for(int i:ans)
        sum+=ar[i];
    cout << sum << '\n';
    for(int i:ans)
        cout << i+1 << ' ';
}


#include <bits/stdc++.h>
using namespace std;
#define int long long

const int inf=1e18;

int32_t main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int n,m,k,tot=0;
    cin >> n >> m >> k;
    vector <pair <int,int> > w,x,y,z;
    for(int i=0;i<n;i++){
        int t,p,q;
        cin >> t >> p >> q;
        if(p and q)w.push_back({t,i+1});
        else if(p)x.push_back({t,i+1});
        else if(q)y.push_back({t,i+1});
        else z.push_back({t,i+1});
    }
    sort(w.begin(),w.end());
    sort(x.begin(),x.end());
    sort(y.begin(),y.end());
    int st=max(0ll,2*k-m),sz=min((int)x.size(),(int)y.size());
    st=max(st,k-sz);
    st=max(st,(int)w.size()+m-n);
    if((int)w.size()<st){
        cout << -1;
        return 0;
    }
    for(int i=0;i<st;i++){
        tot+=w[i].first;
    }
    int pos=st,en=0,a=st,b=st,l=0;
    multiset <int> s;
    while(b<k and en<sz){
        tot+=x[en].first+y[en].first;
        en++,a+=2,b++;
    }
    for(int i=en;i<(int)x.size();i++)
        z.push_back(x[i]);
    for(int i=en;i<(int)y.size();i++)
        z.push_back(y[i]);
    sort(z.begin(),z.end());
    while(a<m and l<(int)z.size()){
        tot+=z[l].first;
        s.insert(z[l].first);
        a++,l++;
    }
    int res=tot,tmp=en,pe=en;
    if(a<m or b<k){
        cout << -1;
        return 0;
    }
    for(int i=st;i<(int)w.size();i++){
        en--;
        if(en<0)break;
        s.insert(x[en].first);
        s.insert(y[en].second);
        auto it=s.end();
        it--;
        tot-=*it;
        s.erase(it);
        it=s.end();
        it--;
        tot-=*it;
        s.erase(it);
        tot+=w[i].first;
        assert(tot>=0);
        if(tot<res)res=tot,pos=i+1,pe=en;
    }
    en=pe;
    cout << res << '\n';
    for(int i=0;i<pos;i++)
        cout << w[i].second << ' ';
    for(int i=0;i<en;i++)
        cout << x[i].second << ' ' << y[i].second << ' ';
    for(int i=en;i<tmp;i++)
        z.push_back(x[i]),z.push_back(y[i]);
    sort(z.begin(),z.end());
    for(int i=0;i<m-pos-2*en;i++)
        cout << z[i].second << ' ';
}


#include <bits/stdc++.h>
using namespace std;
#define int long long

const int inf=1e18;

int32_t main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int n,m,k,tot=0;
    cin >> n >> m >> k;
    vector <pair <int,int> > v[4];
    for(int i=0;i<n;i++){
        int t,p,q;
        cin >> t >> p >> q;
        if(p and q)v[0].push_back({t,i+1});
        else if(p)v[1].push_back({t,i+1});
        else if(q)v[2].push_back({t,i+1});
        else v[3].push_back({t,i+1});
    }
    for(int i=0;i<4;i++)
        sort(v[i].begin(),v[i].end());
    int st=max(0ll,2*k-m),sz=min((int)v[1].size(),(int)v[2].size());
    st=max(st,k-sz);
    st=max(st,(int)v[0].size()+m-n);
    if((int)v[0].size()<st){
        cout << -1;
        return 0;
    }
    for(int i=0;i<st;i++){
        tot+=v[0][i].first;
    }
    int p[]={st,0,0,0},l=0;
    int a=st,b=st;
    while(b<k and l<sz){
        tot+=v[1][l].first+v[2][l].first;
        l++,a+=2,b++;
    }
    p[1]=p[2]=l;
    while(a<m){
        int mn=inf,id;
        for(int i=1;i<4;i++){
            if(p[i]==v[i].size())
                continue;
            if(v[i][p[i]].first<mn)mn=v[i][p[i]].first,id=i;
        }
        tot+=mn,p[id]++,a++;
    }
    int res=tot,bst[]={p[0],p[1],p[2],p[3]};
    if(a<m or b<k){
        cout << -1;
        return 0;
    }
    if(b!=k){
        cout << l << '\n';
    }
    for(int i=st;i<min(m,(int)v[0].size());i++){
        tot+=v[0][i].first,p[0]++;
        l--;
        if(i>=k){
            int mx=0,id;
            for(int i=1;i<4;i++){
                if(p[i]==0)
                    continue;
                if(v[i][p[i]-1].first>=mx)mx=v[i][p[i]-1].first,id=i;
            }
            p[id]--,tot-=mx;
            if(tot<res){
                res=tot;
                for(int j=0;j<4;j++)
                    bst[j]=p[j];
            }
            continue;
        }
        p[1]--,p[2]--;
        tot-=v[1][p[1]].first;
        tot-=v[2][p[2]].first;
        int mn=inf,id;
        for(int i=1;i<4;i++){
            if(p[i]==v[i].size())
                continue;
            if(v[i][p[i]].first<mn)mn=v[i][p[i]].first,id=i;
        }
        tot+=mn,p[id]++;
        if(tot<res){
            res=tot;
            for(int j=0;j<4;j++)
                bst[j]=p[j];
        }
    }
    cout << res << '\n';
    for(int i=0;i<4;i++){
        for(int j=0;j<bst[i];j++)
            cout << v[i][j].second << ' ' ;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define int long long

const int inf=1e18;

int32_t main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int n,m,k,tot=0;
    vector <int> ans;
    cin >> n >> m >> k;
    vector <pair <int,int> > w,x,y,z;
    for(int i=0;i<n;i++){
        int t,p,q;
        cin >> t >> p >> q;
        if(p and q)w.push_back({t,i+1});
        else if(p)x.push_back({t,i+1});
        else if(q)y.push_back({t,i+1});
        else z.push_back({t,i+1});
    }
    sort(w.begin(),w.end());
    sort(x.begin(),x.end());
    sort(y.begin(),y.end());
    int st=max(0ll,2*k-m),sz=min((int)x.size(),(int)y.size());
    st=max(st,k-sz);
    st=max(st,(int)w.size()+m-n);
    st=min(st,(int)w.size());
    if((int)w.size()<st){
        cout << -1;
        return 0;
    }
    for(int i=0;i<st;i++){
        tot+=w[i].first;
        ans.push_back(w[i].second);
    }
    int pos=st,en=0,a=st,b=st,l=0;
    multiset <int> s;
    while(b<k and en<sz){
        tot+=x[en].first+y[en].first;
        en++,a+=2,b++;
    }
    for(int i=en;i<x.size();i++)
        z.push_back(x[i]);
    for(int i=en;i<y.size();i++)
        z.push_back(y[i]);
    sort(z.begin(),z.end());
    while(a<m and l<z.size()){
        tot+=z[l].first;
        s.insert(z[l].first);
        a++,l++;
    }
    int res=tot,tmp=en,pe=en;
    if(a<m or b<k){
        cout << -1;
        return 0;
    }
    for(int i=st;i<w.size();i++){
        en--;
        if(en<0)break;
        s.insert(x[en].first);
        s.insert(y[en].first);
        auto it=s.end();
        it--;
        tot-=*it;
        s.erase(it);
        it=s.end();
        it--;
        tot-=*it;
        s.erase(it);
        tot+=w[i].first;
        if(tot<res)res=tot,pos=i+1,pe=en;
    }
    en=pe;
    cout << res << '\n';
    for(int i=0;i<pos;i++)
        cout << w[i].second << ' ';
    for(int i=0;i<en;i++)
        cout << x[i].second << ' ' << y[i].second << ' ';
    for(int i=en;i<tmp;i++)
        z.push_back(x[i]),z.push_back(y[i]);
    sort(z.begin(),z.end());
    for(int i=0;i<m-pos-2*en;i++)
        cout << z[i].second << ' ';
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
using namespace std;
#define int long long
#define endl '\n'
typedef long long ll;

struct fenwickTree {
	vector<ll> bit;
	int n;
	fenwickTree() {
		n = (int)1e4+3;
		bit.assign(n,0);
	}
	ll sum(int r) {
		ll ret = 0;
		for(; r >= 0; r = (r&(r+1))-1)
			ret += bit[r];
		return ret;
	}
	void add(int idx, ll d) {
		for(; idx < n; idx = idx | (idx+1))
			bit[idx] += d;
	}
	ll sum(int l, int r) {
		return sum(r) - sum(l-1);
	}
}ftSum, ftCnt;

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,m,k;
	cin >> n >> m >> k;
	vector<int> origTime(n);
	vector<pair<int,int>> both,neither,alice,bob;
	for(int i = 0; i < n; ++i) {
		int time,a,b;
		cin >> time >> a >> b;
		origTime[i] = time;
		if(a == 1 && b == 1) {
			both.push_back({time,i});
		} else if(a == 1) {
			alice.push_back({time,i});
		} else if(b == 1) {
			bob.push_back({time,i});
		} else {
			neither.push_back({time,i});
		}
	}
	sort(both.begin(), both.end());
	sort(alice.begin(), alice.end());
	sort(bob.begin(), bob.end());
	sort(neither.begin(), neither.end());

	for(int i = 0; i < alice.size(); ++i) {
		ftCnt.add(alice[i].first,1);
		ftSum.add(alice[i].first,alice[i].first);
	}

	for(int i = 0; i < bob.size(); ++i) {
		ftCnt.add(bob[i].first,1);
		ftSum.add(bob[i].first,bob[i].first);
	}

	for(int i = 0; i < neither.size(); ++i) {
		ftCnt.add(neither[i].first,1);
		ftSum.add(neither[i].first,neither[i].first);
	}

	for(int i = 1; i < both.size(); ++i) both[i].first += both[i-1].first;
	for(int i = 1; i < alice.size(); ++i) alice[i].first += alice[i-1].first;
	for(int i = 1; i < bob.size(); ++i) bob[i].first += bob[i-1].first;

	const int inf = 1e18;
	int res = inf;

	int posAlice = 0, posBob = 0;
	int resI, resExtraCost;
	for(int i = min(m, (int)both.size()); i >= 0; --i) {
		int cost = 0;
		if(i) {
			cost += both[i-1].first;
		}

		int left = max(0LL, k - i);//left is increasing

		while(posAlice < left && posAlice < alice.size()) {
			const int val = alice[posAlice].first;
			ftCnt.add(val, -1);
			ftSum.add(val, -val);
			++posAlice;
		}

		while(posBob < left && posBob < bob.size()) {
			const int val = bob[posBob].first;
			ftCnt.add(val, -1);
			ftSum.add(val, -val);
			++posBob;
		}

		if(i + 2*left > m) continue;
		if(left > alice.size() || left > bob.size()) continue;

		if(left) {
			cost += alice[left-1].first;
			cost += bob[left-1].first;
		}

		int extra = m - i - 2*left;
		if(extra < 0) continue;

		int start = -1, end = 1e4+2, mid;

		while(start+1 < end) {
			mid = (start + end)/2;
			if(ftCnt.sum(mid) >= extra) {
				end = mid;
			} else {
				start = mid;
			}
		}
		if(ftCnt.sum(end) < extra) {
			continue;
		}
		//assert(ftCnt.sum(end-1) <= extra);
		if(extra == 0) {
			assert(ftSum.sum(end-1) + (extra - ftCnt.sum(end-1)) * end == 0);
		}
		cost += ftSum.sum(end-1) + (extra - ftCnt.sum(end-1)) * end;

		if(res > cost) {
			resExtraCost = ftSum.sum(end-1) + (extra - ftCnt.sum(end-1)) * end;
			res = cost;
			resI = i;
		}
	}

	if(res == inf) {
		cout << -1;
		return 0;
	}
	cout << res << endl;
	vector<int> ind;
	for(int i = 0; i < resI; ++i) {
		ind.push_back(both[i].second);
	}
	int left = max(0LL, k - resI);
	for(int i = 0; i < left; ++i) {
		ind.push_back(alice[i].second);
		ind.push_back(bob[i].second);
	}
	posAlice = left, posBob = left;
	int posNeither = 0;
	int tempSum = 0;
	while(ind.size() < m) {
		int a = inf;
		if(posAlice < alice.size()) {
			a = alice[posAlice].first;
			if(posAlice) a -= alice[posAlice-1].first;
		}

		int b = inf;
		if(posBob < bob.size()) {
			b = bob[posBob].first;
			if(posBob) b -= bob[posBob-1].first;
		}

		int nei = inf;
		if(posNeither < neither.size()) {
			nei = neither[posNeither].first;
		}

		int mn = min(a, min(b, nei));
		//assert(mn != inf);
		if(mn == a) {
			ind.push_back(alice[posAlice].second);
			tempSum += alice[posAlice].first;
			++posAlice;
		} else if(mn == b) {
			ind.push_back(bob[posBob].second);
			tempSum += bob[posBob].first;
			++posBob;
		} else {
			ind.push_back(neither[posNeither].second);
			tempSum += neither[posNeither].first;
			++posNeither;
		}
	}
	//assert(tempSum == resExtraCost);
	sort(ind.begin(), ind.end());
	//assert(ind.size() == m);
	tempSum = origTime[ind[0]];
	for(int i = 1; i < ind.size(); ++i) {
		//assert(ind[i-1] < ind[i]);
		tempSum += origTime[ind[i]];
	}
	//assert(tempSum == res);
	for(int p : ind) cout << p+1 << " ";
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
using namespace std;
#define int long long
#define endl '\n'
typedef long long ll;

struct fenwickTree {
	vector<ll> bit;
	int n;
	fenwickTree() {
		n = (int)1e4+3;
		bit.assign(n,0);
	}
	ll sum(int r) {
		ll ret = 0;
		for(; r >= 0; r = (r&(r+1))-1)
			ret += bit[r];
		return ret;
	}
	void add(int idx, ll d) {
		for(; idx < n; idx = idx | (idx+1))
			bit[idx] += d;
	}
	ll sum(int l, int r) {
		return sum(r) - sum(l-1);
	}
}ftSum, ftCnt;

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,m,k;
	cin >> n >> m >> k;
	vector<int> origTime(n);
	vector<pair<int,int>> both,neither,alice,bob;
	for(int i = 0; i < n; ++i) {
		int time,a,b;
		cin >> time >> a >> b;
		origTime[i] = time;
		if(a == 1 && b == 1) {
			both.push_back({time,i});
		} else if(a == 1) {
			alice.push_back({time,i});
		} else if(b == 1) {
			bob.push_back({time,i});
		} else {
			neither.push_back({time,i});
		}
	}
	sort(both.begin(), both.end());
	sort(alice.begin(), alice.end());
	sort(bob.begin(), bob.end());
	sort(neither.begin(), neither.end());

	for(int i = 0; i < alice.size(); ++i) {
		ftCnt.add(alice[i].first,1);
		ftSum.add(alice[i].first,alice[i].first);
	}

	for(int i = 0; i < bob.size(); ++i) {
		ftCnt.add(bob[i].first,1);
		ftSum.add(bob[i].first,bob[i].first);
	}

	for(int i = 0; i < neither.size(); ++i) {
		ftCnt.add(neither[i].first,1);
		ftSum.add(neither[i].first,neither[i].first);
	}

	for(int i = 1; i < both.size(); ++i) both[i].first += both[i-1].first;
	for(int i = 1; i < alice.size(); ++i) alice[i].first += alice[i-1].first;
	for(int i = 1; i < bob.size(); ++i) bob[i].first += bob[i-1].first;

	const int inf = 1e18;
	int res = inf;

	int posAlice = 0, posBob = 0;
	int resI, resExtraCost;
	for(int i = min(m, (int)both.size()); i >= 0; --i) {
		int cost = 0;
		if(i) {
			cost += both[i-1].first;
		}

		int left = max(0LL, k - i);//left is increasing

		while(posAlice < left && posAlice < alice.size()) {
			int val = alice[posAlice].first;
			if(posAlice) {
				val -= alice[posAlice-1].first;
			}
			ftCnt.add(val, -1);
			ftSum.add(val, -val);
			++posAlice;
		}

		while(posBob < left && posBob < bob.size()) {
			int val = bob[posBob].first;
			if(posBob) {
				val -= bob[posBob-1].first;
			}
			ftCnt.add(val, -1);
			ftSum.add(val, -val);
			++posBob;
		}

		if(i + 2*left > m) continue;
		if(left > alice.size() || left > bob.size()) continue;

		if(left) {
			cost += alice[left-1].first;
			cost += bob[left-1].first;
		}

		int extra = m - i - 2*left;
		if(extra < 0) continue;

		int start = -1, end = 1e4+2, mid;

		while(start+1 < end) {
			mid = (start + end)/2;
			if(ftCnt.sum(mid) >= extra) {
				end = mid;
			} else {
				start = mid;
			}
		}
		if(ftCnt.sum(end) < extra) {
			continue;
		}
		//assert(ftCnt.sum(end-1) <= extra);
		if(extra == 0) {
			//assert(ftSum.sum(end-1) + (extra - ftCnt.sum(end-1)) * end == 0);
		}
		cost += ftSum.sum(end-1) + (extra - ftCnt.sum(end,end)) * end;

		if(res > cost) {
			resExtraCost = ftSum.sum(end-1) + (extra - ftCnt.sum(end,end)) * end;
			res = cost;
			resI = i;
		}
	}

	if(res == inf) {
		cout << -1;
		return 0;
	}
	cout << res << endl;
	vector<int> ind;
	for(int i = 0; i < resI; ++i) {
		ind.push_back(both[i].second);
	}
	int left = max(0LL, k - resI);
	for(int i = 0; i < left; ++i) {
		ind.push_back(alice[i].second);
		ind.push_back(bob[i].second);
	}
	posAlice = left, posBob = left;
	int posNeither = 0;
	int tempSum = 0;
	while(ind.size() < m) {
		int a = inf;
		if(posAlice < alice.size()) {
			a = alice[posAlice].first;
			if(posAlice) a -= alice[posAlice-1].first;
		}

		int b = inf;
		if(posBob < bob.size()) {
			b = bob[posBob].first;
			if(posBob) b -= bob[posBob-1].first;
		}

		int nei = inf;
		if(posNeither < neither.size()) {
			nei = neither[posNeither].first;
		}

		int mn = min(a, min(b, nei));
		//assert(mn != inf);
		if(mn == a) {
			ind.push_back(alice[posAlice].second);
			tempSum += alice[posAlice].first;
			++posAlice;
		} else if(mn == b) {
			ind.push_back(bob[posBob].second);
			tempSum += bob[posBob].first;
			++posBob;
		} else {
			ind.push_back(neither[posNeither].second);
			tempSum += neither[posNeither].first;
			++posNeither;
		}
	}
	assert(tempSum == resExtraCost);
	sort(ind.begin(), ind.end());
	//assert(ind.size() == m);
	tempSum = origTime[ind[0]];
	for(int i = 1; i < ind.size(); ++i) {
		//assert(ind[i-1] < ind[i]);
		tempSum += origTime[ind[i]];
	}
	//assert(tempSum == res);
	for(int p : ind) cout << p+1 << " ";
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
using namespace std;
#define int long long
#define endl '\n'
typedef long long ll;

struct fenwickTree {
	vector<ll> bit;
	int n;
	fenwickTree() {
		n = (int)1e4+3;
		bit.assign(n,0);
	}
	ll sum(int r) {
		ll ret = 0;
		for(; r >= 0; r = (r&(r+1))-1)
			ret += bit[r];
		return ret;
	}
	void add(int idx, ll d) {
		for(; idx < n; idx = idx | (idx+1))
			bit[idx] += d;
	}
	ll sum(int l, int r) {
		return sum(r) - sum(l-1);
	}
}ftSum, ftCnt;

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,m,k;
	cin >> n >> m >> k;
	vector<int> origTime(n);
	vector<pair<int,int>> both,neither,alice,bob;
	for(int i = 0; i < n; ++i) {
		int time,a,b;
		cin >> time >> a >> b;
		origTime[i] = time;
		if(a == 1 && b == 1) {
			both.push_back({time,i});
		} else if(a == 1) {
			alice.push_back({time,i});
		} else if(b == 1) {
			bob.push_back({time,i});
		} else {
			neither.push_back({time,i});
		}
	}
	sort(both.begin(), both.end());
	sort(alice.begin(), alice.end());
	sort(bob.begin(), bob.end());
	sort(neither.begin(), neither.end());

	for(int i = 0; i < alice.size(); ++i) {
		ftCnt.add(alice[i].first,1);
		ftSum.add(alice[i].first,alice[i].first);
	}

	for(int i = 0; i < bob.size(); ++i) {
		ftCnt.add(bob[i].first,1);
		ftSum.add(bob[i].first,bob[i].first);
	}

	for(int i = 0; i < neither.size(); ++i) {
		ftCnt.add(neither[i].first,1);
		ftSum.add(neither[i].first,neither[i].first);
	}

	for(int i = 1; i < both.size(); ++i) both[i].first += both[i-1].first;
	for(int i = 1; i < alice.size(); ++i) alice[i].first += alice[i-1].first;
	for(int i = 1; i < bob.size(); ++i) bob[i].first += bob[i-1].first;

	const int inf = 1e18;
	int res = inf;

	int posAlice = 0, posBob = 0;
	int resI, resExtraCost;
	for(int i = min(m, (int)both.size()); i >= 0; --i) {
		int cost = 0;
		if(i) {
			cost += both[i-1].first;
		}

		int left = max(0LL, k - i);//left is increasing

		while(posAlice <= left && posAlice < alice.size()) {
			int val = alice[posAlice].first;
			if(posAlice) {
				val -= alice[posAlice-1].first;
			}
			ftCnt.add(val, -1);
			ftSum.add(val, -val);
			++posAlice;
		}

		while(posBob <= left && posBob < bob.size()) {
			int val = bob[posBob].first;
			if(posBob) {
				val -= bob[posBob-1].first;
			}
			ftCnt.add(val, -1);
			ftSum.add(val, -val);
			++posBob;
		}

		if(i + 2*left > m) continue;
		if(left > alice.size() || left > bob.size()) continue;

		if(left) {
			cost += alice[left-1].first;
			cost += bob[left-1].first;
		}

		int extra = m - i - 2*left;
		if(extra < 0) continue;

		int start = -1, end = 1e4+2, mid;

		while(start+1 < end) {
			mid = (start + end)/2;
			if(ftCnt.sum(mid) >= extra) {
				end = mid;
			} else {
				start = mid;
			}
		}
		if(ftCnt.sum(end) < extra) {
			continue;
		}
		//assert(ftCnt.sum(end-1) <= extra);
		if(extra == 0) {
			//assert(ftSum.sum(end-1) + (extra - ftCnt.sum(end-1)) * end == 0);
		}
		cost += ftSum.sum(end-1) + (extra - ftCnt.sum(end-1)) * end;

		if(res > cost) {
			resExtraCost = ftSum.sum(end-1) + (extra - ftCnt.sum(end-1)) * end;
			res = cost;
			resI = i;
		}
	}

	if(res == inf) {
		cout << -1;
		return 0;
	}
	cout << res << endl;
	vector<int> ind;
	for(int i = 0; i < resI; ++i) {
		ind.push_back(both[i].second);
	}
	int left = max(0LL, k - resI);
	for(int i = 0; i < left; ++i) {
		ind.push_back(alice[i].second);
		ind.push_back(bob[i].second);
	}
	posAlice = left, posBob = left;
	int posNeither = 0;
	int tempSum = 0;
	while(ind.size() < m) {
		int a = inf;
		if(posAlice < alice.size()) {
			a = alice[posAlice].first;
			if(posAlice) a -= alice[posAlice-1].first;
		}

		int b = inf;
		if(posBob < bob.size()) {
			b = bob[posBob].first;
			if(posBob) b -= bob[posBob-1].first;
		}

		int nei = inf;
		if(posNeither < neither.size()) {
			nei = neither[posNeither].first;
		}

		int mn = min(a, min(b, nei));
		//assert(mn != inf);
		if(mn == a) {
			ind.push_back(alice[posAlice].second);
			tempSum += alice[posAlice].first;
			++posAlice;
		} else if(mn == b) {
			ind.push_back(bob[posBob].second);
			tempSum += bob[posBob].first;
			++posBob;
		} else {
			ind.push_back(neither[posNeither].second);
			tempSum += neither[posNeither].first;
			++posNeither;
		}
	}
	assert(tempSum == resExtraCost);
	sort(ind.begin(), ind.end());
	//assert(ind.size() == m);
	tempSum = origTime[ind[0]];
	for(int i = 1; i < ind.size(); ++i) {
		//assert(ind[i-1] < ind[i]);
		tempSum += origTime[ind[i]];
	}
	//assert(tempSum == res);
	for(int p : ind) cout << p+1 << " ";
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
using namespace std;
#define int long long
#define endl '\n'
typedef long long ll;

struct fenwickTree {
	vector<ll> bit;
	int n;
	fenwickTree() {
		n = (int)1e4+3;
		bit.assign(n,0);
	}
	ll sum(int r) {
		ll ret = 0;
		for(; r >= 0; r = (r&(r+1))-1)
			ret += bit[r];
		return ret;
	}
	void add(int idx, ll d) {
		for(; idx < n; idx = idx | (idx+1))
			bit[idx] += d;
	}
	ll sum(int l, int r) {
		return sum(r) - sum(l-1);
	}
}ftSum, ftCnt;

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,m,k;
	cin >> n >> m >> k;
	vector<int> origTime(n);
	vector<pair<int,int>> both,neither,alice,bob;
	for(int i = 0; i < n; ++i) {
		int time,a,b;
		cin >> time >> a >> b;
		origTime[i] = time;
		if(a == 1 && b == 1) {
			both.push_back({time,i});
		} else if(a == 1) {
			alice.push_back({time,i});
		} else if(b == 1) {
			bob.push_back({time,i});
		} else {
			neither.push_back({time,i});
		}
	}
	sort(both.begin(), both.end());
	sort(alice.begin(), alice.end());
	sort(bob.begin(), bob.end());
	sort(neither.begin(), neither.end());

	for(int i = 0; i < alice.size(); ++i) {
		ftCnt.add(alice[i].first,1);
		ftSum.add(alice[i].first,alice[i].first);
	}

	for(int i = 0; i < bob.size(); ++i) {
		ftCnt.add(bob[i].first,1);
		ftSum.add(bob[i].first,bob[i].first);
	}

	for(int i = 0; i < neither.size(); ++i) {
		ftCnt.add(neither[i].first,1);
		ftSum.add(neither[i].first,neither[i].first);
	}

	for(int i = 1; i < both.size(); ++i) both[i].first += both[i-1].first;
	for(int i = 1; i < alice.size(); ++i) alice[i].first += alice[i-1].first;
	for(int i = 1; i < bob.size(); ++i) bob[i].first += bob[i-1].first;

	const int inf = 1e18;
	int res = inf;

	int posAlice = 0, posBob = 0;
	int resI, resExtraCost;
	for(int i = min(m, (int)both.size()); i >= 0; --i) {
		int cost = 0;
		if(i) {
			cost += both[i-1].first;
		}

		int left = max(0LL, k - i);//left is increasing

		while(posAlice < left && posAlice < alice.size()) {
			int val = alice[posAlice].first;
			if(posAlice) {
				val -= alice[posAlice-1].first;
			}
			ftCnt.add(val, -1);
			ftSum.add(val, -val);
			++posAlice;
		}

		while(posBob < left && posBob < bob.size()) {
			int val = bob[posBob].first;
			if(posBob) {
				val -= bob[posBob-1].first;
			}
			ftCnt.add(val, -1);
			ftSum.add(val, -val);
			++posBob;
		}

		if(i + 2*left > m) continue;
		if(left > alice.size() || left > bob.size()) continue;

		if(left) {
			cost += alice[left-1].first;
			cost += bob[left-1].first;
		}

		int extra = m - i - 2*left;
		if(extra < 0) continue;

		int start = -1, end = 1e4+2, mid;

		while(start+1 < end) {
			mid = (start + end)/2;
			if(ftCnt.sum(mid) >= extra) {
				end = mid;
			} else {
				start = mid;
			}
		}
		if(ftCnt.sum(end) < extra) {
			continue;
		}
		//assert(ftCnt.sum(end-1) <= extra);
		if(extra == 0) {
			//assert(ftSum.sum(end-1) + (extra - ftCnt.sum(end-1)) * end == 0);
		}
		cost += ftSum.sum(end-1) + (extra - ftCnt.sum(end-1,end-1)) * end;

		if(res > cost) {
			resExtraCost = ftSum.sum(end-1) + (extra - ftCnt.sum(end-1)) * end;
			res = cost;
			resI = i;
		}
	}

	if(res == inf) {
		cout << -1;
		return 0;
	}
	cout << res << endl;
	vector<int> ind;
	for(int i = 0; i < resI; ++i) {
		ind.push_back(both[i].second);
	}
	int left = max(0LL, k - resI);
	for(int i = 0; i < left; ++i) {
		ind.push_back(alice[i].second);
		ind.push_back(bob[i].second);
	}
	posAlice = left, posBob = left;
	int posNeither = 0;
	int tempSum = 0;
	while(ind.size() < m) {
		int a = inf;
		if(posAlice < alice.size()) {
			a = alice[posAlice].first;
			if(posAlice) a -= alice[posAlice-1].first;
		}

		int b = inf;
		if(posBob < bob.size()) {
			b = bob[posBob].first;
			if(posBob) b -= bob[posBob-1].first;
		}

		int nei = inf;
		if(posNeither < neither.size()) {
			nei = neither[posNeither].first;
		}

		int mn = min(a, min(b, nei));
		//assert(mn != inf);
		if(mn == a) {
			ind.push_back(alice[posAlice].second);
			tempSum += alice[posAlice].first;
			++posAlice;
		} else if(mn == b) {
			ind.push_back(bob[posBob].second);
			tempSum += bob[posBob].first;
			++posBob;
		} else {
			ind.push_back(neither[posNeither].second);
			tempSum += neither[posNeither].first;
			++posNeither;
		}
	}
	//assert(tempSum == resExtraCost);
	sort(ind.begin(), ind.end());
	//assert(ind.size() == m);
	tempSum = origTime[ind[0]];
	for(int i = 1; i < ind.size(); ++i) {
		//assert(ind[i-1] < ind[i]);
		tempSum += origTime[ind[i]];
	}
	//assert(tempSum == res);
	for(int p : ind) cout << p+1 << " ";
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
using namespace std;
#define int long long
#define endl '\n'
typedef long long ll;

struct fenwickTree {
	vector<ll> bit;
	int n;
	fenwickTree() {
		n = (int)1e4+3;
		bit.assign(n,0);
	}
	ll sum(int r) {
		ll ret = 0;
		for(; r >= 0; r = (r&(r+1))-1)
			ret += bit[r];
		return ret;
	}
	void add(int idx, ll d) {
		for(; idx < n; idx = idx | (idx+1))
			bit[idx] += d;
	}
	ll sum(int l, int r) {
		return sum(r) - sum(l-1);
	}
}ftSum, ftCnt;

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,m,k;
	cin >> n >> m >> k;
	vector<int> origTime(n);
	vector<pair<int,int>> both,neither,alice,bob;
	for(int i = 0; i < n; ++i) {
		int time,a,b;
		cin >> time >> a >> b;
		origTime[i] = time;
		if(a == 1 && b == 1) {
			both.push_back({time,i});
		} else if(a == 1) {
			alice.push_back({time,i});
		} else if(b == 1) {
			bob.push_back({time,i});
		} else {
			neither.push_back({time,i});
		}
	}
	sort(both.begin(), both.end());
	sort(alice.begin(), alice.end());
	sort(bob.begin(), bob.end());
	sort(neither.begin(), neither.end());

	for(int i = 0; i < alice.size(); ++i) {
		ftCnt.add(alice[i].first,1);
		ftSum.add(alice[i].first,alice[i].first);
	}

	for(int i = 0; i < bob.size(); ++i) {
		ftCnt.add(bob[i].first,1);
		ftSum.add(bob[i].first,bob[i].first);
	}

	for(int i = 0; i < neither.size(); ++i) {
		ftCnt.add(neither[i].first,1);
		ftSum.add(neither[i].first,neither[i].first);
	}

	for(int i = 1; i < both.size(); ++i) both[i].first += both[i-1].first;
	for(int i = 1; i < alice.size(); ++i) alice[i].first += alice[i-1].first;
	for(int i = 1; i < bob.size(); ++i) bob[i].first += bob[i-1].first;

	const int inf = 1e18;
	int res = inf;

	int posAlice = 0, posBob = 0;
	int resI, resExtraCost;
	for(int i = min(m, (int)both.size()); i >= 0; --i) {
		int cost = 0;
		if(i) {
			cost += both[i-1].first;
		}

		int left = max(0LL, k - i);//left is increasing

		while(posAlice < left && posAlice < alice.size()) {
			const int val = alice[posAlice].first;
			ftCnt.add(val, -1);
			ftSum.add(val, -val);
			++posAlice;
		}

		while(posBob < left && posBob < bob.size()) {
			const int val = bob[posBob].first;
			ftCnt.add(val, -1);
			ftSum.add(val, -val);
			++posBob;
		}

		if(i + 2*left > m) continue;
		if(left > alice.size() || left > bob.size()) continue;

		if(left) {
			cost += alice[left-1].first;
			cost += bob[left-1].first;
		}

		int extra = m - i - 2*left;
		if(extra < 0) continue;

		int start = -1, end = 1e4+2, mid;

		while(start+1 < end) {
			mid = (start + end)/2;
			if(ftCnt.sum(mid) >= extra) {
				end = mid;
			} else {
				start = mid;
			}
		}
		if(ftCnt.sum(end) < extra) {
			continue;
		}
		assert(ftCnt.sum(end-1) <= extra);
		if(extra == 0) {
			assert(ftSum.sum(end-1) + (extra - ftCnt.sum(end-1)) * end == 0);
		}
		cost += ftSum.sum(end-1) + (extra - ftCnt.sum(end-1)) * end;

		if(res > cost) {
			resExtraCost = ftSum.sum(end-1) + (extra - ftCnt.sum(end-1)) * end;
			res = cost;
			resI = i;
		}
	}

	if(res == inf) {
		cout << -1;
		return 0;
	}
	cout << res << endl;
	vector<int> ind;
	for(int i = 0; i < resI; ++i) {
		ind.push_back(both[i].second);
	}
	int left = max(0LL, k - resI);
	for(int i = 0; i < left; ++i) {
		ind.push_back(alice[i].second);
		ind.push_back(bob[i].second);
	}
	posAlice = left, posBob = left;
	int posNeither = 0;
	int tempSum = 0;
	while(ind.size() < m) {
		int a = inf;
		if(posAlice < alice.size()) {
			a = alice[posAlice].first;
			if(posAlice) a -= alice[posAlice-1].first;
		}

		int b = inf;
		if(posBob < bob.size()) {
			b = bob[posBob].first;
			if(posBob) b -= bob[posBob-1].first;
		}

		int nei = inf;
		if(posNeither < neither.size()) {
			nei = neither[posNeither].first;
		}

		int mn = min(a, min(b, nei));
		assert(mn != inf);
		if(mn == a) {
			ind.push_back(alice[posAlice].second);
			tempSum += alice[posAlice].first;
			++posAlice;
		} else if(mn == b) {
			ind.push_back(bob[posBob].second);
			tempSum += bob[posBob].first;
			++posBob;
		} else {
			ind.push_back(neither[posNeither].second);
			tempSum += neither[posNeither].first;
			++posNeither;
		}
	}
	assert(tempSum == resExtraCost);
	sort(ind.begin(), ind.end());
	assert(ind.size() == m);
	tempSum = origTime[ind[0]];
	for(int i = 1; i < ind.size(); ++i) {
		assert(ind[i-1] < ind[i]);
		tempSum += origTime[ind[i]];
	}
	assert(tempSum == res);
	for(int p : ind) cout << p+1 << " ";
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
using namespace std;
#define int long long
#define endl '\n'
typedef long long ll;

struct fenwickTree {
	vector<ll> bit;
	int n;
	fenwickTree() {
		n = (int)1e4+3;
		bit.assign(n,0);
	}
	ll sum(int r) {
		ll ret = 0;
		for(; r >= 0; r = (r&(r+1))-1)
			ret += bit[r];
		return ret;
	}
	void add(int idx, ll d) {
		for(; idx < n; idx = idx | (idx+1))
			bit[idx] += d;
	}
	ll sum(int l, int r) {
		return sum(r) - sum(l-1);
	}
}ftSum, ftCnt;

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,m,k;
	cin >> n >> m >> k;
	vector<pair<int,int>> both,neither,alice,bob;
	for(int i = 0; i < n; ++i) {
		int time,a,b;
		cin >> time >> a >> b;
		if(a == 1 && b == 1) {
			both.push_back({time,i});
		} else if(a == 1) {
			alice.push_back({time,i});
		} else if(b == 1) {
			bob.push_back({time,i});
		} else {
			neither.push_back({time,i});
		}
	}
	sort(both.begin(), both.end());
	sort(alice.begin(), alice.end());
	sort(bob.begin(), bob.end());
	sort(neither.begin(), neither.end());

	for(int i = 0; i < alice.size(); ++i) {
		ftCnt.add(alice[i].first,1);
		ftSum.add(alice[i].first,alice[i].first);
	}

	for(int i = 0; i < bob.size(); ++i) {
		ftCnt.add(bob[i].first,1);
		ftSum.add(bob[i].first,bob[i].first);
	}

	for(int i = 0; i < neither.size(); ++i) {
		ftCnt.add(neither[i].first,1);
		ftSum.add(neither[i].first,neither[i].first);
	}

	for(int i = 1; i < both.size(); ++i) both[i].first += both[i-1].first;
	for(int i = 1; i < alice.size(); ++i) alice[i].first += alice[i-1].first;
	for(int i = 1; i < bob.size(); ++i) bob[i].first += bob[i-1].first;

	const int inf = 1e18;
	int res = inf;

	int posAlice = 0, posBob = 0;
	int resI;
	for(int i = min(m, (int)both.size()); i >= 0; --i) {
		int cost = 0;
		if(i) {
			cost += both[i-1].first;
		}

		int left = max(0LL, k - i);//left is increasing

		while(posAlice < left && posAlice < alice.size()) {
			const int val = alice[posAlice].first;
			ftCnt.add(val, -1);
			ftSum.add(val, -val);
			++posAlice;
		}

		while(posBob < left && posBob < bob.size()) {
			const int val = bob[posBob].first;
			ftCnt.add(val, -1);
			ftSum.add(val, -val);
			++posBob;
		}

		if(i + 2*left > m) continue;
		if(left > alice.size() || left > bob.size()) continue;

		if(left) {
			cost += alice[left-1].first;
			cost += bob[left-1].first;
		}

		int extra = m - i - 2*left;
		if(extra < 0) continue;

		int start = 0, end = 1e4+2, mid;

		while(start+1 < end) {
			mid = (start + end)/2;
			if(ftCnt.sum(mid) >= extra) {
				end = mid;
			} else {
				start = mid;
			}
		}
		if(ftCnt.sum(end) < extra) {
			continue;
		}
		cost += ftSum.sum(end-1) + (extra - ftCnt.sum(end-1)) * end;

		if(res > cost) {
			res = cost;
			resI = i;
		}
	}

	if(res == inf) {
		cout << -1;
		return 0;
	}
	cout << res << endl;
	//cout << "here, resI: " << resI << endl;
	vector<int> ind;
	for(int i = 0; i < resI; ++i) {
		//cout << "1 adding: " << both[i].second << endl;
		ind.push_back(both[i].second);
	}
	int left = max(0LL, k - resI);
	for(int i = 0; i < left; ++i) {
		//cout << "2 adding: " << alice[i].second << endl;
		ind.push_back(alice[i].second);
		//cout << "3 adding: " << bob[i].second << endl;
		ind.push_back(bob[i].second);
	}
	posAlice = left, posBob = left;
	int posNeither = 0;
	while(ind.size() < m) {
		int a = inf;
		if(posAlice < alice.size()) {
			a = alice[posAlice].first;
			if(posAlice) a -= alice[posAlice-1].first;
		}

		int b = inf;
		if(posBob < bob.size()) {
			b = bob[posBob].first;
			if(posBob) b -= bob[posBob-1].first;
		}

		int nei = inf;
		if(posNeither < neither.size()) {
			nei = neither[posNeither].first;
		}

		int mn = min(a, min(b, nei));
		if(mn == a) {
			//cout << "4 adding: " << alice[posAlice].second << endl;
			ind.push_back(alice[posAlice].second);
			++posAlice;
		} else if(mn == b) {
			//cout << "5 adding: " << bob[posBob].second << endl;
			ind.push_back(bob[posBob].second);
			++posBob;
		} else {
			//cout << "6 adding: " << neither[posNeither].second << endl;
			ind.push_back(neither[posNeither].second);
			++posNeither;
		}
	}
	for(int p : ind) cout << p+1 << " ";
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
using namespace std;
#define int long long
#define endl '\n'
typedef long long ll;

struct fenwickTree {
	vector<ll> bit;
	int n;
	fenwickTree() {
		n = (int)1e4+3;
		bit.assign(n,0);
	}
	ll sum(int r) {
		ll ret = 0;
		for(; r >= 0; r = (r&(r+1))-1)
			ret += bit[r];
		return ret;
	}
	void add(int idx, ll d) {
		for(; idx < n; idx = idx | (idx+1))
			bit[idx] += d;
	}
	ll sum(int l, int r) {
		return sum(r) - sum(l-1);
	}
}ftSum, ftCnt;

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,m,k;
	cin >> n >> m >> k;
	vector<int> origTime(n);
	vector<pair<int,int>> both,neither,alice,bob;
	for(int i = 0; i < n; ++i) {
		int time,a,b;
		cin >> time >> a >> b;
		origTime[i] = time;
		if(a == 1 && b == 1) {
			both.push_back({time,i});
		} else if(a == 1) {
			alice.push_back({time,i});
		} else if(b == 1) {
			bob.push_back({time,i});
		} else {
			neither.push_back({time,i});
		}
	}
	sort(both.begin(), both.end());
	sort(alice.begin(), alice.end());
	sort(bob.begin(), bob.end());
	sort(neither.begin(), neither.end());

	for(int i = 0; i < alice.size(); ++i) {
		ftCnt.add(alice[i].first,1);
		ftSum.add(alice[i].first,alice[i].first);
	}

	for(int i = 0; i < bob.size(); ++i) {
		ftCnt.add(bob[i].first,1);
		ftSum.add(bob[i].first,bob[i].first);
	}

	for(int i = 0; i < neither.size(); ++i) {
		ftCnt.add(neither[i].first,1);
		ftSum.add(neither[i].first,neither[i].first);
	}

	for(int i = 1; i < both.size(); ++i) both[i].first += both[i-1].first;
	for(int i = 1; i < alice.size(); ++i) alice[i].first += alice[i-1].first;
	for(int i = 1; i < bob.size(); ++i) bob[i].first += bob[i-1].first;

	const int inf = 1e18;
	int res = inf;

	int posAlice = 0, posBob = 0;
	int resI, resExtraCost;
	for(int i = min(m, (int)both.size()); i >= 0; --i) {
		int cost = 0;
		if(i) {
			cost += both[i-1].first;
		}

		int left = max(0LL, k - i);//left is increasing

		while(posAlice < left && posAlice < alice.size()) {
			int val = alice[posAlice].first;
			if(posAlice) {
				val -= alice[posAlice-1].first;
			}
			ftCnt.add(val, -1);
			ftSum.add(val, -val);
			++posAlice;
		}

		while(posBob < left && posBob < bob.size()) {
			int val = bob[posBob].first;
			if(posBob) {
				val -= bob[posBob-1].first;
			}
			ftCnt.add(val, -1);
			ftSum.add(val, -val);
			++posBob;
		}

		if(i + 2*left > m) continue;
		if(left > alice.size() || left > bob.size()) continue;

		if(left) {
			cost += alice[left-1].first;
			cost += bob[left-1].first;
		}

		int extra = m - i - 2*left;
		if(extra < 0) continue;

		int start = -1, end = 1e4+2, mid;

		while(start+1 < end) {
			mid = (start + end)/2;
			if(ftCnt.sum(mid) >= extra) {
				end = mid;
			} else {
				start = mid;
			}
		}
		if(ftCnt.sum(end) < extra) {
			continue;
		}
		//assert(ftCnt.sum(end-1) <= extra);
		if(extra == 0) {
			//assert(ftSum.sum(end-1) + (extra - ftCnt.sum(end-1)) * end == 0);
		}
		cost += ftSum.sum(end-1) + (extra - ftCnt.sum(end-1)) * end;

		if(res > cost) {
			resExtraCost = ftSum.sum(end-1) + (extra - ftCnt.sum(end-1)) * end;
			res = cost;
			resI = i;
		}
	}

	if(res == inf) {
		cout << -1;
		return 0;
	}
	cout << res << endl;
	vector<int> ind;
	for(int i = 0; i < resI; ++i) {
		ind.push_back(both[i].second);
	}
	int left = max(0LL, k - resI);
	for(int i = 0; i < left; ++i) {
		ind.push_back(alice[i].second);
		ind.push_back(bob[i].second);
	}
	posAlice = left, posBob = left;
	int posNeither = 0;
	int tempSum = 0;
	while(ind.size() < m) {
		int a = inf;
		if(posAlice < alice.size()) {
			a = alice[posAlice].first;
			if(posAlice) a -= alice[posAlice-1].first;
		}

		int b = inf;
		if(posBob < bob.size()) {
			b = bob[posBob].first;
			if(posBob) b -= bob[posBob-1].first;
		}

		int nei = inf;
		if(posNeither < neither.size()) {
			nei = neither[posNeither].first;
		}

		int mn = min(a, min(b, nei));
		//assert(mn != inf);
		if(mn == a) {
			ind.push_back(alice[posAlice].second);
			tempSum += alice[posAlice].first;
			++posAlice;
		} else if(mn == b) {
			ind.push_back(bob[posBob].second);
			tempSum += bob[posBob].first;
			++posBob;
		} else {
			ind.push_back(neither[posNeither].second);
			tempSum += neither[posNeither].first;
			++posNeither;
		}
	}
	assert(tempSum == resExtraCost);
	sort(ind.begin(), ind.end());
	//assert(ind.size() == m);
	tempSum = origTime[ind[0]];
	for(int i = 1; i < ind.size(); ++i) {
		//assert(ind[i-1] < ind[i]);
		tempSum += origTime[ind[i]];
	}
	//assert(tempSum == res);
	for(int p : ind) cout << p+1 << " ";
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
using namespace std;
#define int long long
#define endl '\n'
typedef long long ll;

struct fenwickTree {
	vector<ll> bit;
	int n;
	fenwickTree() {
		n = (int)1e4+3;
		bit.assign(n,0);
	}
	ll sum(int r) {
		ll ret = 0;
		for(; r >= 0; r = (r&(r+1))-1)
			ret += bit[r];
		return ret;
	}
	void add(int idx, ll d) {
		for(; idx < n; idx = idx | (idx+1))
			bit[idx] += d;
	}
	ll sum(int l, int r) {
		return sum(r) - sum(l-1);
	}
}ftSum, ftCnt;

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,m,k;
	cin >> n >> m >> k;
	vector<int> origTime(n);
	vector<pair<int,int>> both,neither,alice,bob;
	for(int i = 0; i < n; ++i) {
		int time,a,b;
		cin >> time >> a >> b;
		origTime[i] = time;
		if(a == 1 && b == 1) {
			both.push_back({time,i});
		} else if(a == 1) {
			alice.push_back({time,i});
		} else if(b == 1) {
			bob.push_back({time,i});
		} else {
			neither.push_back({time,i});
		}
	}
	sort(both.begin(), both.end());
	sort(alice.begin(), alice.end());
	sort(bob.begin(), bob.end());
	sort(neither.begin(), neither.end());

	for(int i = 0; i < alice.size(); ++i) {
		ftCnt.add(alice[i].first,1);
		ftSum.add(alice[i].first,alice[i].first);
	}

	for(int i = 0; i < bob.size(); ++i) {
		ftCnt.add(bob[i].first,1);
		ftSum.add(bob[i].first,bob[i].first);
	}

	for(int i = 0; i < neither.size(); ++i) {
		ftCnt.add(neither[i].first,1);
		ftSum.add(neither[i].first,neither[i].first);
	}

	for(int i = 1; i < both.size(); ++i) both[i].first += both[i-1].first;
	for(int i = 1; i < alice.size(); ++i) alice[i].first += alice[i-1].first;
	for(int i = 1; i < bob.size(); ++i) bob[i].first += bob[i-1].first;

	const int inf = 1e18;
	int res = inf;

	int posAlice = 0, posBob = 0;
	int resI;
	for(int i = min(m, (int)both.size()); i >= 0; --i) {
		int cost = 0;
		if(i) {
			cost += both[i-1].first;
		}

		int left = max(0LL, k - i);//left is increasing

		while(posAlice < left && posAlice < alice.size()) {
			int val = alice[posAlice].first;
			if(posAlice) {
				val -= alice[posAlice-1].first;
			}
			ftCnt.add(val, -1);
			ftSum.add(val, -val);
			++posAlice;
		}

		while(posBob < left && posBob < bob.size()) {
			int val = bob[posBob].first;
			if(posBob) {
				val -= bob[posBob-1].first;
			}
			ftCnt.add(val, -1);
			ftSum.add(val, -val);
			++posBob;
		}

		if(i + 2*left > m) continue;
		if(left > alice.size() || left > bob.size()) continue;

		if(left) {
			cost += alice[left-1].first;
			cost += bob[left-1].first;
		}

		int extra = m - i - 2*left;
		if(extra < 0) continue;

		int start = -1, end = 1e4+2, mid;

		while(start+1 < end) {
			mid = (start + end)/2;
			if(ftCnt.sum(mid) >= extra) {
				end = mid;
			} else {
				start = mid;
			}
		}
		if(ftCnt.sum(end) < extra) {
			continue;
		}
		cost += ftSum.sum(end-1) + (extra - ftCnt.sum(end-1)) * end;

		if(res > cost) {
			res = cost;
			resI = i;
		}
	}

	if(res == inf) {
		cout << -1;
		return 0;
	}
	cout << res << endl;
	vector<int> ind;
	for(int i = 0; i < resI; ++i) {
		ind.push_back(both[i].second);
	}
	int left = max(0LL, k - resI);
	for(int i = 0; i < left; ++i) {
		ind.push_back(alice[i].second);
		ind.push_back(bob[i].second);
	}
	posAlice = left, posBob = left;
	int posNeither = 0;
	int tempSum = 0;
	while(ind.size() < m) {
		int a = inf;
		if(posAlice < alice.size()) {
			a = alice[posAlice].first;
			if(posAlice) a -= alice[posAlice-1].first;
		}

		int b = inf;
		if(posBob < bob.size()) {
			b = bob[posBob].first;
			if(posBob) b -= bob[posBob-1].first;
		}

		int nei = inf;
		if(posNeither < neither.size()) {
			nei = neither[posNeither].first;
		}

		int mn = min(a, min(b, nei));
		if(mn == a) {
			ind.push_back(alice[posAlice].second);
			tempSum += alice[posAlice].first;
			++posAlice;
		} else if(mn == b) {
			ind.push_back(bob[posBob].second);
			tempSum += bob[posBob].first;
			++posBob;
		} else {
			ind.push_back(neither[posNeither].second);
			tempSum += neither[posNeither].first;
			++posNeither;
		}
	}
	for(int p : ind) cout << p+1 << " ";
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
using namespace std;
#define int long long
#define endl '\n'
typedef long long ll;

struct fenwickTree {
	vector<ll> bit;
	int n;
	fenwickTree() {
		n = (int)1e4+3;
		bit.assign(n,0);
	}
	ll sum(int r) {
		ll ret = 0;
		for(; r >= 0; r = (r&(r+1))-1)
			ret += bit[r];
		return ret;
	}
	void add(int idx, ll d) {
		for(; idx < n; idx = idx | (idx+1))
			bit[idx] += d;
	}
	ll sum(int l, int r) {
		return sum(r) - sum(l-1);
	}
}ftSum, ftCnt;

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,m,k;
	cin >> n >> m >> k;
	vector<pair<int,int>> both,neither,alice,bob;
	for(int i = 0; i < n; ++i) {
		int time,a,b;
		cin >> time >> a >> b;
		if(a == 1 && b == 1) {
			both.push_back({time,i});
		} else if(a == 1) {
			alice.push_back({time,i});
		} else if(b == 1) {
			bob.push_back({time,i});
		} else {
			neither.push_back({time,i});
		}
	}
	sort(both.begin(), both.end());
	sort(alice.begin(), alice.end());
	sort(bob.begin(), bob.end());
	sort(neither.begin(), neither.end());

	for(int i = 0; i < alice.size(); ++i) {
		ftCnt.add(alice[i].first,1);
		ftSum.add(alice[i].first,alice[i].first);
	}

	for(int i = 0; i < bob.size(); ++i) {
		ftCnt.add(bob[i].first,1);
		ftSum.add(bob[i].first,bob[i].first);
	}

	for(int i = 0; i < neither.size(); ++i) {
		ftCnt.add(neither[i].first,1);
		ftSum.add(neither[i].first,neither[i].first);
	}

	for(int i = 1; i < both.size(); ++i) both[i].first += both[i-1].first;
	for(int i = 1; i < alice.size(); ++i) alice[i].first += alice[i-1].first;
	for(int i = 1; i < bob.size(); ++i) bob[i].first += bob[i-1].first;

	const int inf = 1e18;
	int res = inf;

	int posAlice = 0, posBob = 0;
	int resI;
	for(int i = min(m, (int)both.size()); i >= 0; --i) {
		int cost = 0;
		if(i) {
			cost += both[i-1].first;
		}

		int left = max(0LL, k - i);//left is increasing

		while(posAlice < left && posAlice < alice.size()) {
			const int val = alice[posAlice].first;
			ftCnt.add(val, -1);
			ftSum.add(val, -val);
			++posAlice;
		}

		while(posBob < left && posBob < bob.size()) {
			const int val = bob[posBob].first;
			ftCnt.add(val, -1);
			ftSum.add(val, -val);
			++posBob;
		}

		if(i + 2*left > m) continue;
		if(left > alice.size() || left > bob.size()) continue;

		if(left) {
			cost += alice[left-1].first;
			cost += bob[left-1].first;
		}

		int extra = m - i - 2*left;
		if(extra < 0) continue;

		int start = -1, end = 1e4+2, mid;

		while(start+1 < end) {
			mid = (start + end)/2;
			if(ftCnt.sum(mid) >= extra) {
				end = mid;
			} else {
				start = mid;
			}
		}
		if(ftCnt.sum(end) < extra) {
			continue;
		}
		cost += ftSum.sum(end-1) + (extra - ftCnt.sum(end-1)) * end;

		if(res > cost) {
			res = cost;
			resI = i;
		}
	}

	if(res == inf) {
		cout << -1;
		return 0;
	}
	cout << res << endl;
	vector<int> ind;
	for(int i = 0; i < resI; ++i) {
		ind.push_back(both[i].second);
	}
	int left = max(0LL, k - resI);
	for(int i = 0; i < left; ++i) {
		ind.push_back(alice[i].second);
		ind.push_back(bob[i].second);
	}
	posAlice = left, posBob = left;
	int posNeither = 0;
	while(ind.size() < m) {
		int a = inf;
		if(posAlice < alice.size()) {
			a = alice[posAlice].first;
			if(posAlice) a -= alice[posAlice-1].first;
		}

		int b = inf;
		if(posBob < bob.size()) {
			b = bob[posBob].first;
			if(posBob) b -= bob[posBob-1].first;
		}

		int nei = inf;
		if(posNeither < neither.size()) {
			nei = neither[posNeither].first;
		}

		int mn = min(a, min(b, nei));
		if(mn == a) {
			ind.push_back(alice[posAlice].second);
			++posAlice;
		} else if(mn == b) {
			ind.push_back(bob[posBob].second);
			++posBob;
		} else {
			ind.push_back(neither[posNeither].second);
			++posNeither;
		}
	}
	for(int p : ind) cout << p+1 << " ";
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
using namespace std;
#define int long long
#define endl '\n'
typedef long long ll;

struct fenwickTree {
	vector<ll> bit;
	int n;
	fenwickTree() {
		n = (int)1e4+3;
		bit.assign(n,0);
	}
	ll sum(int r) {
		ll ret = 0;
		for(; r >= 0; r = (r&(r+1))-1)
			ret += bit[r];
		return ret;
	}
	void add(int idx, ll d) {
		for(; idx < n; idx = idx | (idx+1))
			bit[idx] += d;
	}
	ll sum(int l, int r) {
		return sum(r) - sum(l-1);
	}
}ftSum, ftCnt;

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,m,k;
	cin >> n >> m >> k;
	vector<int> origTime(n);
	vector<pair<int,int>> both,neither,alice,bob;
	for(int i = 0; i < n; ++i) {
		int time,a,b;
		cin >> time >> a >> b;
		origTime[i] = time;
		if(a == 1 && b == 1) {
			both.push_back({time,i});
		} else if(a == 1) {
			alice.push_back({time,i});
		} else if(b == 1) {
			bob.push_back({time,i});
		} else {
			neither.push_back({time,i});
		}
	}
	sort(both.begin(), both.end());
	sort(alice.begin(), alice.end());
	sort(bob.begin(), bob.end());
	sort(neither.begin(), neither.end());

	for(int i = 0; i < alice.size(); ++i) {
		ftCnt.add(alice[i].first,1);
		ftSum.add(alice[i].first,alice[i].first);
	}

	for(int i = 0; i < bob.size(); ++i) {
		ftCnt.add(bob[i].first,1);
		ftSum.add(bob[i].first,bob[i].first);
	}

	for(int i = 0; i < neither.size(); ++i) {
		ftCnt.add(neither[i].first,1);
		ftSum.add(neither[i].first,neither[i].first);
	}

	for(int i = 1; i < both.size(); ++i) both[i].first += both[i-1].first;
	for(int i = 1; i < alice.size(); ++i) alice[i].first += alice[i-1].first;
	for(int i = 1; i < bob.size(); ++i) bob[i].first += bob[i-1].first;

	const int inf = 1e18;
	int res = inf;

	int posAlice = 0, posBob = 0;
	int resI, resExtraCost;
	for(int i = min(m, (int)both.size()); i >= 0; --i) {
		int cost = 0;
		if(i) {
			cost += both[i-1].first;
		}

		int left = max(0LL, k - i);//left is increasing

		while(posAlice < left && posAlice < alice.size()) {
			int val = alice[posAlice].first;
			if(posAlice) {
				val -= alice[posAlice-1].first;
			}
			ftCnt.add(val, -1);
			ftSum.add(val, -val);
			++posAlice;
		}

		while(posBob < left && posBob < bob.size()) {
			int val = bob[posBob].first;
			if(posBob) {
				val -= bob[posBob-1].first;
			}
			ftCnt.add(val, -1);
			ftSum.add(val, -val);
			++posBob;
		}

		if(i + 2*left > m) continue;
		if(left > alice.size() || left > bob.size()) continue;

		if(left) {
			cost += alice[left-1].first;
			cost += bob[left-1].first;
		}

		int extra = m - i - 2*left;
		if(extra < 0) continue;

		int start = -1, end = 1e4+2, mid;

		while(start+1 < end) {
			mid = (start + end)/2;
			if(ftCnt.sum(mid) >= extra) {
				end = mid;
			} else {
				start = mid;
			}
		}
		if(ftCnt.sum(end) < extra) {
			continue;
		}
		//assert(ftCnt.sum(end-1) <= extra);
		if(extra == 0) {
			//assert(ftSum.sum(end-1) + (extra - ftCnt.sum(end-1)) * end == 0);
		}
		cost += ftSum.sum(end-1) + (extra - ftCnt.sum(end-1,end-1)) * end;

		if(res > cost) {
			resExtraCost = ftSum.sum(end-1) + (extra - ftCnt.sum(end-1)) * end;
			res = cost;
			resI = i;
		}
	}

	if(res == inf) {
		cout << -1;
		return 0;
	}
	cout << res << endl;
	vector<int> ind;
	for(int i = 0; i < resI; ++i) {
		ind.push_back(both[i].second);
	}
	int left = max(0LL, k - resI);
	for(int i = 0; i < left; ++i) {
		ind.push_back(alice[i].second);
		ind.push_back(bob[i].second);
	}
	posAlice = left, posBob = left;
	int posNeither = 0;
	int tempSum = 0;
	while(ind.size() < m) {
		int a = inf;
		if(posAlice < alice.size()) {
			a = alice[posAlice].first;
			if(posAlice) a -= alice[posAlice-1].first;
		}

		int b = inf;
		if(posBob < bob.size()) {
			b = bob[posBob].first;
			if(posBob) b -= bob[posBob-1].first;
		}

		int nei = inf;
		if(posNeither < neither.size()) {
			nei = neither[posNeither].first;
		}

		int mn = min(a, min(b, nei));
		//assert(mn != inf);
		if(mn == a) {
			ind.push_back(alice[posAlice].second);
			tempSum += alice[posAlice].first;
			++posAlice;
		} else if(mn == b) {
			ind.push_back(bob[posBob].second);
			tempSum += bob[posBob].first;
			++posBob;
		} else {
			ind.push_back(neither[posNeither].second);
			tempSum += neither[posNeither].first;
			++posNeither;
		}
	}
	assert(tempSum == resExtraCost);
	sort(ind.begin(), ind.end());
	//assert(ind.size() == m);
	tempSum = origTime[ind[0]];
	for(int i = 1; i < ind.size(); ++i) {
		//assert(ind[i-1] < ind[i]);
		tempSum += origTime[ind[i]];
	}
	//assert(tempSum == res);
	for(int p : ind) cout << p+1 << " ";
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
using namespace std;
#define int long long
#define endl '\n'
typedef long long ll;

struct fenwickTree {
	vector<ll> bit;
	int n;
	fenwickTree() {
		n = (int)1e4+3;
		bit.assign(n,0);
	}
	ll sum(int r) {
		ll ret = 0;
		for(; r >= 0; r = (r&(r+1))-1)
			ret += bit[r];
		return ret;
	}
	void add(int idx, ll d) {
		for(; idx < n; idx = idx | (idx+1))
			bit[idx] += d;
	}
	ll sum(int l, int r) {
		return sum(r) - sum(l-1);
	}
}ftSum, ftCnt;

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,m,k;
	cin >> n >> m >> k;
	vector<int> origTime(n);
	vector<pair<int,int>> both,neither,alice,bob;
	for(int i = 0; i < n; ++i) {
		int time,a,b;
		cin >> time >> a >> b;
		origTime[i] = time;
		if(a == 1 && b == 1) {
			both.push_back({time,i});
		} else if(a == 1) {
			alice.push_back({time,i});
		} else if(b == 1) {
			bob.push_back({time,i});
		} else {
			neither.push_back({time,i});
		}
	}
	sort(both.begin(), both.end());
	sort(alice.begin(), alice.end());
	sort(bob.begin(), bob.end());
	sort(neither.begin(), neither.end());

	for(int i = 0; i < alice.size(); ++i) {
		ftCnt.add(alice[i].first,1);
		ftSum.add(alice[i].first,alice[i].first);
	}

	for(int i = 0; i < bob.size(); ++i) {
		ftCnt.add(bob[i].first,1);
		ftSum.add(bob[i].first,bob[i].first);
	}

	for(int i = 0; i < neither.size(); ++i) {
		ftCnt.add(neither[i].first,1);
		ftSum.add(neither[i].first,neither[i].first);
	}

	for(int i = 1; i < both.size(); ++i) both[i].first += both[i-1].first;
	for(int i = 1; i < alice.size(); ++i) alice[i].first += alice[i-1].first;
	for(int i = 1; i < bob.size(); ++i) bob[i].first += bob[i-1].first;

	const int inf = 1e18;
	int res = inf;

	int posAlice = 0, posBob = 0;
	int resI, resExtraCost;
	for(int i = min(m, (int)both.size()); i >= 0; --i) {
		int cost = 0;
		if(i) {
			cost += both[i-1].first;
		}

		int left = max(0LL, k - i);//left is increasing

		while(posAlice < left && posAlice < alice.size()) {
			const int val = alice[posAlice].first;
			ftCnt.add(val, -1);
			ftSum.add(val, -val);
			++posAlice;
		}

		while(posBob < left && posBob < bob.size()) {
			const int val = bob[posBob].first;
			ftCnt.add(val, -1);
			ftSum.add(val, -val);
			++posBob;
		}

		if(i + 2*left > m) continue;
		if(left > alice.size() || left > bob.size()) continue;

		if(left) {
			cost += alice[left-1].first;
			cost += bob[left-1].first;
		}

		int extra = m - i - 2*left;
		if(extra < 0) continue;

		int start = -1, end = 1e4+2, mid;

		while(start+1 < end) {
			mid = (start + end)/2;
			if(ftCnt.sum(mid) >= extra) {
				end = mid;
			} else {
				start = mid;
			}
		}
		if(ftCnt.sum(end) < extra) {
			continue;
		}
		assert(ftCnt.sum(end-1) <= extra);
		if(extra == 0) {
			//assert(ftSum.sum(end-1) + (extra - ftCnt.sum(end-1)) * end == 0);
		}
		cost += ftSum.sum(end-1) + (extra - ftCnt.sum(end-1)) * end;

		if(res > cost) {
			resExtraCost = ftSum.sum(end-1) + (extra - ftCnt.sum(end-1)) * end;
			res = cost;
			resI = i;
		}
	}

	if(res == inf) {
		cout << -1;
		return 0;
	}
	cout << res << endl;
	vector<int> ind;
	for(int i = 0; i < resI; ++i) {
		ind.push_back(both[i].second);
	}
	int left = max(0LL, k - resI);
	for(int i = 0; i < left; ++i) {
		ind.push_back(alice[i].second);
		ind.push_back(bob[i].second);
	}
	posAlice = left, posBob = left;
	int posNeither = 0;
	int tempSum = 0;
	while(ind.size() < m) {
		int a = inf;
		if(posAlice < alice.size()) {
			a = alice[posAlice].first;
			if(posAlice) a -= alice[posAlice-1].first;
		}

		int b = inf;
		if(posBob < bob.size()) {
			b = bob[posBob].first;
			if(posBob) b -= bob[posBob-1].first;
		}

		int nei = inf;
		if(posNeither < neither.size()) {
			nei = neither[posNeither].first;
		}

		int mn = min(a, min(b, nei));
		//assert(mn != inf);
		if(mn == a) {
			ind.push_back(alice[posAlice].second);
			tempSum += alice[posAlice].first;
			++posAlice;
		} else if(mn == b) {
			ind.push_back(bob[posBob].second);
			tempSum += bob[posBob].first;
			++posBob;
		} else {
			ind.push_back(neither[posNeither].second);
			tempSum += neither[posNeither].first;
			++posNeither;
		}
	}
	//assert(tempSum == resExtraCost);
	sort(ind.begin(), ind.end());
	//assert(ind.size() == m);
	tempSum = origTime[ind[0]];
	for(int i = 1; i < ind.size(); ++i) {
		//assert(ind[i-1] < ind[i]);
		tempSum += origTime[ind[i]];
	}
	//assert(tempSum == res);
	for(int p : ind) cout << p+1 << " ";
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
using namespace std;
#define int long long
#define endl '\n'
typedef long long ll;

struct fenwickTree {
	vector<ll> bit;
	int n;
	fenwickTree() {
		n = (int)1e4+3;
		bit.assign(n,0);
	}
	ll sum(int r) {
		ll ret = 0;
		for(; r >= 0; r = (r&(r+1))-1)
			ret += bit[r];
		return ret;
	}
	void add(int idx, ll d) {
		for(; idx < n; idx = idx | (idx+1))
			bit[idx] += d;
	}
	ll sum(int l, int r) {
		return sum(r) - sum(l-1);
	}
}ftSum, ftCnt;

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,m,k;
	cin >> n >> m >> k;
	vector<int> origTime(n);
	vector<pair<int,int>> both,neither,alice,bob;
	for(int i = 0; i < n; ++i) {
		int time,a,b;
		cin >> time >> a >> b;
		origTime[i] = time;
		if(a == 1 && b == 1) {
			both.push_back({time,i});
		} else if(a == 1) {
			alice.push_back({time,i});
		} else if(b == 1) {
			bob.push_back({time,i});
		} else {
			neither.push_back({time,i});
		}
	}
	sort(both.begin(), both.end());
	sort(alice.begin(), alice.end());
	sort(bob.begin(), bob.end());
	sort(neither.begin(), neither.end());

	for(int i = 0; i < alice.size(); ++i) {
		ftCnt.add(alice[i].first,1);
		ftSum.add(alice[i].first,alice[i].first);
	}

	for(int i = 0; i < bob.size(); ++i) {
		ftCnt.add(bob[i].first,1);
		ftSum.add(bob[i].first,bob[i].first);
	}

	for(int i = 0; i < neither.size(); ++i) {
		ftCnt.add(neither[i].first,1);
		ftSum.add(neither[i].first,neither[i].first);
	}

	for(int i = 1; i < both.size(); ++i) both[i].first += both[i-1].first;
	for(int i = 1; i < alice.size(); ++i) alice[i].first += alice[i-1].first;
	for(int i = 1; i < bob.size(); ++i) bob[i].first += bob[i-1].first;

	const int inf = 1e18;
	int res = inf;

	int posAlice = 0, posBob = 0;
	int resI, resExtraCost;
	for(int i = min(m, (int)both.size()); i >= 0; --i) {
		int cost = 0;
		if(i) {
			cost += both[i-1].first;
		}

		int left = max(0LL, k - i);//left is increasing

		while(posAlice < left && posAlice < alice.size()) {
			const int val = alice[posAlice].first;
			ftCnt.add(val, -1);
			ftSum.add(val, -val);
			++posAlice;
		}

		while(posBob < left && posBob < bob.size()) {
			const int val = bob[posBob].first;
			ftCnt.add(val, -1);
			ftSum.add(val, -val);
			++posBob;
		}

		if(i + 2*left > m) continue;
		if(left > alice.size() || left > bob.size()) continue;

		if(left) {
			cost += alice[left-1].first;
			cost += bob[left-1].first;
		}

		int extra = m - i - 2*left;
		if(extra < 0) continue;

		int start = -1, end = 1e4+2, mid;

		while(start+1 < end) {
			mid = (start + end)/2;
			if(ftCnt.sum(mid) >= extra) {
				end = mid;
			} else {
				start = mid;
			}
		}
		if(ftCnt.sum(end) < extra) {
			continue;
		}
		//assert(ftCnt.sum(end-1) <= extra);
		if(extra == 0) {
			//assert(ftSum.sum(end-1) + (extra - ftCnt.sum(end-1)) * end == 0);
		}
		cost += ftSum.sum(end-1) + (extra - ftCnt.sum(end-1)) * end;

		if(res > cost) {
			resExtraCost = ftSum.sum(end-1) + (extra - ftCnt.sum(end-1)) * end;
			res = cost;
			resI = i;
		}
	}

	if(res == inf) {
		cout << -1;
		return 0;
	}
	cout << res << endl;
	vector<int> ind;
	for(int i = 0; i < resI; ++i) {
		ind.push_back(both[i].second);
	}
	int left = max(0LL, k - resI);
	for(int i = 0; i < left; ++i) {
		ind.push_back(alice[i].second);
		ind.push_back(bob[i].second);
	}
	posAlice = left, posBob = left;
	int posNeither = 0;
	int tempSum = 0;
	while(ind.size() < m) {
		int a = inf;
		if(posAlice < alice.size()) {
			a = alice[posAlice].first;
			if(posAlice) a -= alice[posAlice-1].first;
		}

		int b = inf;
		if(posBob < bob.size()) {
			b = bob[posBob].first;
			if(posBob) b -= bob[posBob-1].first;
		}

		int nei = inf;
		if(posNeither < neither.size()) {
			nei = neither[posNeither].first;
		}

		int mn = min(a, min(b, nei));
		//assert(mn != inf);
		if(mn == a) {
			ind.push_back(alice[posAlice].second);
			tempSum += alice[posAlice].first;
			++posAlice;
		} else if(mn == b) {
			ind.push_back(bob[posBob].second);
			tempSum += bob[posBob].first;
			++posBob;
		} else {
			ind.push_back(neither[posNeither].second);
			tempSum += neither[posNeither].first;
			++posNeither;
		}
	}
	assert(tempSum == resExtraCost);
	sort(ind.begin(), ind.end());
	//assert(ind.size() == m);
	tempSum = origTime[ind[0]];
	for(int i = 1; i < ind.size(); ++i) {
		//assert(ind[i-1] < ind[i]);
		tempSum += origTime[ind[i]];
	}
	//assert(tempSum == res);
	for(int p : ind) cout << p+1 << " ";
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
using namespace std;
#define int long long
#define endl '\n'
typedef long long ll;

struct fenwickTree {
	vector<ll> bit;
	int n;
	fenwickTree() {
		n = (int)1e4+3;
		bit.assign(n,0);
	}
	ll sum(int r) {
		ll ret = 0;
		for(; r >= 0; r = (r&(r+1))-1)
			ret += bit[r];
		return ret;
	}
	void add(int idx, ll d) {
		for(; idx < n; idx = idx | (idx+1))
			bit[idx] += d;
	}
	ll sum(int l, int r) {
		return sum(r) - sum(l-1);
	}
}ftSum, ftCnt;

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,m,k;
	cin >> n >> m >> k;
	vector<int> origTime(n);
	vector<pair<int,int>> both,neither,alice,bob;
	for(int i = 0; i < n; ++i) {
		int time,a,b;
		cin >> time >> a >> b;
		origTime[i] = time;
		if(a == 1 && b == 1) {
			both.push_back({time,i});
		} else if(a == 1) {
			alice.push_back({time,i});
		} else if(b == 1) {
			bob.push_back({time,i});
		} else {
			neither.push_back({time,i});
		}
	}
	sort(both.begin(), both.end());
	sort(alice.begin(), alice.end());
	sort(bob.begin(), bob.end());
	sort(neither.begin(), neither.end());

	for(int i = 0; i < alice.size(); ++i) {
		ftCnt.add(alice[i].first,1);
		ftSum.add(alice[i].first,alice[i].first);
	}

	for(int i = 0; i < bob.size(); ++i) {
		ftCnt.add(bob[i].first,1);
		ftSum.add(bob[i].first,bob[i].first);
	}

	for(int i = 0; i < neither.size(); ++i) {
		ftCnt.add(neither[i].first,1);
		ftSum.add(neither[i].first,neither[i].first);
	}

	for(int i = 1; i < both.size(); ++i) both[i].first += both[i-1].first;
	for(int i = 1; i < alice.size(); ++i) alice[i].first += alice[i-1].first;
	for(int i = 1; i < bob.size(); ++i) bob[i].first += bob[i-1].first;

	const int inf = 1e18;
	int res = inf;

	int posAlice = 0, posBob = 0;
	int resI, resExtraCost;
	for(int i = min(m, (int)both.size()); i >= 0; --i) {
		int cost = 0;
		if(i) {
			cost += both[i-1].first;
		}

		int left = max(0LL, k - i);//left is increasing

		while(posAlice < left && posAlice < alice.size()) {
			const int val = alice[posAlice].first;
			ftCnt.add(val, -1);
			ftSum.add(val, -val);
			++posAlice;
		}

		while(posBob < left && posBob < bob.size()) {
			const int val = bob[posBob].first;
			ftCnt.add(val, -1);
			ftSum.add(val, -val);
			++posBob;
		}

		if(i + 2*left > m) continue;
		if(left > alice.size() || left > bob.size()) continue;

		if(left) {
			cost += alice[left-1].first;
			cost += bob[left-1].first;
		}

		int extra = m - i - 2*left;
		if(extra < 0) continue;

		int start = -1, end = 1e4+2, mid;

		while(start+1 < end) {
			mid = (start + end)/2;
			if(ftCnt.sum(mid) >= extra) {
				end = mid;
			} else {
				start = mid;
			}
		}
		if(ftCnt.sum(end) < extra) {
			continue;
		}
		//assert(ftCnt.sum(end-1) <= extra);
		if(extra == 0) {
			//assert(ftSum.sum(end-1) + (extra - ftCnt.sum(end-1)) * end == 0);
		}
		cost += ftSum.sum(end-1) + (extra - ftCnt.sum(end-1)) * end;

		if(res > cost) {
			resExtraCost = ftSum.sum(end-1) + (extra - ftCnt.sum(end-1)) * end;
			res = cost;
			resI = i;
		}
	}

	if(res == inf) {
		cout << -1;
		return 0;
	}
	cout << res << endl;
	vector<int> ind;
	for(int i = 0; i < resI; ++i) {
		ind.push_back(both[i].second);
	}
	int left = max(0LL, k - resI);
	for(int i = 0; i < left; ++i) {
		ind.push_back(alice[i].second);
		ind.push_back(bob[i].second);
	}
	posAlice = left, posBob = left;
	int posNeither = 0;
	int tempSum = 0;
	while(ind.size() < m) {
		int a = inf;
		if(posAlice < alice.size()) {
			a = alice[posAlice].first;
			if(posAlice) a -= alice[posAlice-1].first;
		}

		int b = inf;
		if(posBob < bob.size()) {
			b = bob[posBob].first;
			if(posBob) b -= bob[posBob-1].first;
		}

		int nei = inf;
		if(posNeither < neither.size()) {
			nei = neither[posNeither].first;
		}

		int mn = min(a, min(b, nei));
		assert(mn != inf);
		if(mn == a) {
			ind.push_back(alice[posAlice].second);
			tempSum += alice[posAlice].first;
			++posAlice;
		} else if(mn == b) {
			ind.push_back(bob[posBob].second);
			tempSum += bob[posBob].first;
			++posBob;
		} else {
			ind.push_back(neither[posNeither].second);
			tempSum += neither[posNeither].first;
			++posNeither;
		}
	}
	//assert(tempSum == resExtraCost);
	sort(ind.begin(), ind.end());
	//assert(ind.size() == m);
	tempSum = origTime[ind[0]];
	for(int i = 1; i < ind.size(); ++i) {
		//assert(ind[i-1] < ind[i]);
		tempSum += origTime[ind[i]];
	}
	//assert(tempSum == res);
	for(int p : ind) cout << p+1 << " ";
}


#include<bits/stdc++.h>
using namespace std;
using ll = int64_t;
const ll inf = 1e18;

int main(){
    int n,m,k;
    cin >> n >> m >> k;
    vector<pair<ll,int>> a[3];
    vector<ll> b(n);
    vector<pair<ll,int>> c;
    for(int i = 0;i<n;++i){
        ll t;
        int x,y;
        cin >> t >> x >> y;
        b[i] = t;
        if(x && y) a[0].push_back({t,i});
        else if(x || y) a[x<y?1:2].push_back({t,i});
        else c.push_back({t,i});
    }

    for(int i = 0;i<3;++i){
        sort(a[i].begin(),a[i].end());
        for(int j = 1;j<a[i].size();++j) a[i][j].first += a[i][j-1].first;
        // for(auto p : a[i]){
        //     cout << p.first << " " ;
        // }cout<<"\n";
    }

    ll ans = inf;
    ll fullans = inf;
    int used[3] = {0,0,0};
    int fused[3] = {0,0,0};
    if(2*k <= m && a[1].size() >= k && a[2].size() >= k){
        if(2*k == m){
            if(fullans > a[1][k-1].first + a[2][k-1].first){
                fullans = a[1][k-1].first + a[2][k-1].first;
                fused[0] = 0;
                fused[1] = fused[2] = k-1;
            }
        }
        else if(ans > a[1][k-1].first + a[2][k-1].first){
            ans = a[1][k-1].first + a[2][k-1].first;
            used[0] = 0;
            used[1] = used[2] = k;
        }
    }
    for(int i = 0;i<a[0].size();++i){
        if(i == k-1){
           if(k == m){
               if(fullans > a[0][i].first){
                   fullans = a[0][i].first;
                   fused[0] = k;
                   fused[1] = fused[2] = 0;
               }
           }
           else if(ans >a[0][i].first){
               ans = a[0][i].first;
               used[0] = k;
               used[1] = used[2] = 0;
           }
           break;
        }
        int rest = k-i-1;
        if(i+1+rest+rest<=m && a[1].size() >= rest && a[2].size() >= rest){
            if(i+1+rest+rest  == m){
                if(fullans > a[0][i].first + a[1][rest-1].first + a[2][rest-1].first){
                    fullans = a[0][i].first + a[1][rest-1].first + a[2][rest-1].first;
                    fused[0] = i+1;
                    fused[1] = fused[2] = rest;
                }
            }
            else if(ans >a[0][i].first + a[1][rest-1].first + a[2][rest-1].first){
                    ans = a[0][i].first + a[1][rest-1].first + a[2][rest-1].first;
                    used[0] = i+1;
                    used[1] = used[2] = rest;
            }
        }
    }
    for(int i = 0;i<3;++i)
        for(int j = used[i];j<a[i].size();++j)
            c.push_back({a[i][j].first-(j?a[i][j-1].first:0),a[i][j].second});

    if(fullans >= inf && ans >=inf){
        cout <<-1<<"\n";
        return 0;
    }
    sort(c.begin(),c.end());
    // for(int i = 0;i<n;++i) cout << c[i].second <<" \n"[i==n-1];
    int rem = m - (used[0]+used[1]+used[2]);
    // cout << rem <<"->\n";
    if(rem > 0){
        for(int i = 0;i<rem;++i){
            ans += c[i].first;
        }
    }
    if(ans < fullans){
        cout << ans <<"\n";
        for(int i= 0;i<3;++i)
            for(int j = 0;j < used[i];++j)
                cout << (a[i][j].second+1) << " ";
        for(int i =0 ;i<rem;++i)
            cout << (c[i].second+1) <<" ";
        cout<<"\n";
    }
    else{
        cout << fullans <<"\n";
        for(int i =0 ;i<3;++i) for(int j = 0;j<fused[i];++j) cout << (1+a[i][j].second)<< " "; cout<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ll = int64_t;
const ll inf = 1e18;

int main(){
    int n,m,k;
    cin >> n >> m >> k;
    vector<pair<ll,int>> pp[3];
    vector<pair<ll,pair<int,int>>> arr(n);
    vector<pair<ll,int>> leftover;
    for(int i = 0;i<n;++i){
        int a,b,c;
        cin >> a >> b >> c;
        arr[i] = {a,{b,c}};
        if(b && c) pp[0].push_back({a,i});
        else if(!b && c) pp[1].push_back({a,i});
        else if(b && !c) pp[2].push_back({a,i});
        else leftover.push_back({a,i});
    }

    for(int i = 0;i<3;++i){
        sort(pp[i].begin(),pp[i].end());
        for(int j  = 1 ;j < pp[i].size();++j){
            pp[i][j].first += pp[i][j-1].first;
        }
    }

    ll ans = inf;
    vector<int> used(3);
    for(int i = 0;i<pp[0].size();++i){
        if(i == k-1){
            if(ans > pp[0][i].first){
                used[0] = i+1;
                used[1] = used[2] = 0;
                ans = pp[0][i].first;
            }
            break;
        }
        int left = k - i - 1;
        if(pp[1].size() >= left && pp[2].size() >= left){
            if(2*left + i + 1<=m && ans > pp[1][left-1].first + pp[2][left-1].first + pp[0][i].first){
                ans = pp[1][left-1].first + pp[2][left-1].first + pp[0][i].first;
                used[0] = i+1;
                used[1] = used[2] = left;
            }
        }
    }
    if(pp[1].size() >= k && pp[2].size() >=k){
        if(2*k <= m && ans > pp[1][k-1].first + pp[2][k-1].first){
            ans = pp[1][k-1].first + pp[2][k-1].first;
            used[0] = 0;
            used[1] = used[2] = k;
        }
    }
    for(int i = 0;i<3;++i){
        for(int j = used[i];j < pp[i].size();++j)
        {
            leftover.push_back({arr[pp[i][j].second].first,pp[i][j].second});
        }
    }

    sort(leftover.begin(),leftover.end());
//    for(int i= 0;i<3;++i) cout << used[i] << " \n"[i==2];
    int left = m-(used[0]+used[1]+used[2]);
    if(ans == inf && leftover.size() < left){
        cout <<"-1\n";
    }
    else{
        for(int i = 0;i<left;++i)
            ans += leftover[i].first;
        cout << ans <<"\n";
        for(int i = 0;i<3;++i){
            for(int j = 0;j < used[i];++j){
                cout <<pp[i][j].second+1 << " ";
            }
        }

        for(int i=0;i<left;++i){
            cout << leftover[0].second+1 <<" ";
        }
        cout <<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ll = int64_t;
const ll inf = 1e18;

int main(){
    int n,m,k;
    cin >> n >> m >> k;
    vector<pair<ll,int>> a[3];
    vector<ll> b(n);
    vector<pair<ll,int>> c;
    for(int i = 0;i<n;++i){
        ll t;
        int x,y;
        cin >> t >> x >> y;
        b[i] = t;
        if(x && y) a[0].push_back({t,i});
        else if(x || y) a[x<y?1:2].push_back({t,i});
        else c.push_back({t,i});
    }

    for(int i = 0;i<3;++i){
        sort(a[i].begin(),a[i].end());
        for(int j = 1;j<a[i].size();++j) a[i][j].first += a[i][j-1].first;
        // for(auto p : a[i]){
        //     cout << p.first << " " ;
        // }cout<<"\n";
    }

    ll ans = inf;
    ll fullans = inf;
    int used[3] = {0,0,0};
    int fused[3] = {0,0,0};
    if(a[0].size()==0 && 2*k <= m && a[1].size() >= k && a[2].size() >= k){
        if(2*k == m){
            if(fullans > a[1][k-1].first + a[2][k-1].first){
                fullans = a[1][k-1].first + a[2][k-1].first;
                fused[0] = 0;
                fused[1] = fused[2] = k-1;
            }
        }
        else if(ans > a[1][k-1].first + a[2][k-1].first){
            ans = a[1][k-1].first + a[2][k-1].first;
            used[0] = 0;
            used[1] = used[2] = k;
        }
    }
    for(int i = 0;i<a[0].size();++i){
        if(i == k-1){
           if(k == m){
               if(fullans > a[0][i].first){
                   fullans = a[0][i].first;
                   fused[0] = k;
                   fused[1] = fused[2] = 0;
               }
           }
           else if(ans >a[0][i].first){
               ans = a[0][i].first;
               used[0] = k;
               used[1] = used[2] = 0;
           }
        }
        int rest = k-i-1;
        if(rest > 0)
        if(i+1+rest+rest<=m && a[1].size() >= rest && a[2].size() >= rest){
            if(i+1+rest+rest  == m){
                if(fullans > a[0][i].first + a[1][rest-1].first + a[2][rest-1].first){
                    fullans = a[0][i].first + a[1][rest-1].first + a[2][rest-1].first;
                    fused[0] = i+1;
                    fused[1] = fused[2] = rest;
                }
            }
            else if(ans >a[0][i].first + a[1][rest-1].first + a[2][rest-1].first){
                    ans = a[0][i].first + a[1][rest-1].first + a[2][rest-1].first;
                    used[0] = i+1;
                    used[1] = used[2] = rest;
            }
        }
    }
    for(int i = 0;i<3;++i)
        for(int j = used[i];j<a[i].size();++j)
            c.push_back({a[i][j].first-(j?a[i][j-1].first:0),a[i][j].second});

    int rem = m - (used[0]+used[1]+used[2]);
    if(fullans >= inf && (rem < 0 || ans >=inf)){
        cout <<-1<<"\n";
        return 0;
    }
    sort(c.begin(),c.end());
    // for(int i = 0;i<n;++i) cout << c[i].second <<" \n"[i==n-1];

    // cout << rem <<"->\n";
    if(rem > 0){
        for(int i = 0;i<rem;++i){
            ans += c[i].first;
        }
    }
    if(ans < fullans){
        cout << ans <<"\n";
        for(int i= 0;i<3;++i)
            for(int j = 0;j < used[i];++j)
                cout << (a[i][j].second+1) << " ";
        for(int i =0 ;i<rem;++i)
            cout << (c[i].second+1) <<" ";
        cout<<"\n";
    }
    else{
        cout << fullans <<"\n";
        for(int i =0 ;i<3;++i) for(int j = 0;j<fused[i];++j) cout << (1+a[i][j].second)<< " "; cout<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ll = int;
const ll inf = 2e9+1;
#define size(v) int((v).size())

void update(set<pair<int,int>> &free,set<pair<int,int>> &st,int needed,int &sum){
    needed = max(needed,0);
    while(true){
        bool ok = false;
        while(size(st) > needed){
            sum -= st.rbegin()->first;
            free.insert(*st.rbegin());
            st.erase(prev(st.end()));
            ok =true;
        }
        while(size(st) < needed && size(free)){
            sum += free.begin()->first;
            st.insert(*free.begin());
            free.erase(free.begin());
            ok = true;
        }
        while(size(free) && size(st) && free.begin()->first < st.rbegin()->first){
            sum += free.begin()->first - st.rbegin()->first;
            free.insert(*st.rbegin());
            st.erase(prev(st.end()));
            st.insert(*free.begin());
            free.erase(free.begin());
            ok = true;
        }
        if(!ok) break;
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n,m,k;
    cin >> n >> m >> k;
    vector<pair<int,int>> a[4];
    for(int i = 0;i<n;++i){
        int t,b,c;
        cin >> t >> b >> c;
        a[2*b + c].push_back({t,i});
    }

    vector<int> sum[4];
    for(int i = 0 ;i<4;++i)
    {
        sort(a[i].begin(),a[i].end());
        sum[i].push_back(0);
        for(auto it : a[i]){
            sum[i].push_back(sum[i].back() + it.first);
        }
    }

    int ans = inf;
    int plus = 0;
    int pos = inf;
    vector<int> res;//set of used books
    set<pair<int,int>> free,st;
    for(int itr = 0;itr<2;++itr){
        free.clear();
        st.clear();
        int start = 0;
        plus = 0;
        while(k-start >= size(sum[1]) || k-start >= size(sum[2]) || m-start- (k-start)*2 < 0) ++start;

        if(start >= size(sum[3])){
            cout << -1 <<"\n";
            return 0;
        }

        for(int i =0;i<3;++i)
            for(int j = size(a[i])-1;j>=(i==0?0:k-start);--j)
                free.insert(a[i][j]);

        int extra_needed = m - start - 2*(k-start);
        update(free,st,extra_needed,plus);

        for(int cnt = start;cnt < (itr ==0?size(sum[3]):pos);++cnt){
            if(k-cnt >= 0){
                if(cnt + (k-cnt)*2 + size(st) == m){
                    if(ans > plus + sum[3][cnt] + sum[1][k-cnt] + sum[2][k-cnt]){
                        ans = plus + sum[3][cnt] + sum[1][k-cnt] + sum[2][k-cnt];
                        pos = cnt+1;
                    }
                }
            }
            else if(cnt + size(st) == m){
                    if(ans > sum[3][cnt] + plus){
                        ans = sum[3][cnt] + plus;
                        pos = cnt+1;
                    }
            }
            if(itr == 1 && cnt+1 == pos) break;
            extra_needed-=1;//Adding one more book of type (1,1) +1
            if(k-cnt > 0){
                extra_needed += 2;//Removing two books of type one (0,1) and one(1,0) (-2)
                free.insert(a[1][k-cnt-1]);
                free.insert(a[2][k-cnt-1]);
            }
            update(free,st,extra_needed,plus);
        }
        if(itr == 1)
        {
            for(int i = 0;i+1<pos;++i) res.push_back(a[3][i].second);
            for(int i = 0;i<=k-pos;++i){
                res.push_back(a[1][i].second);
                res.push_back(a[2][i].second);
            }
            for(auto p  : st) res.push_back(p.second);
        }
    }
    cout << ans <<"\n";
    for(int v : res)
    cout << v+1 << " ";
    cout <<"\n";
    return 0;

}


#include<bits/stdc++.h>
using namespace std;
using ll = int64_t;
const ll inf = 1e18;

int main(){
    int n,m,k;
    cin >> n >> m >> k;
    vector<pair<ll,int>> a[3];
    vector<ll> b(n);
    vector<pair<ll,int>> c;
    for(int i = 0;i<n;++i){
        ll t;
        int x,y;
        cin >> t >> x >> y;
        b[i] = t;
        if(x && y) a[0].push_back({t,i});
        else if(x || y) a[x<y?1:2].push_back({t,i});
        else c.push_back({t,i});
    }

    for(int i = 0;i<3;++i){
        sort(a[i].begin(),a[i].end());
        for(int j = 1;j<a[i].size();++j) a[i][j].first += a[i][j-1].first;
        // for(auto p : a[i]){
        //     cout << p.first << " " ;
        // }cout<<"\n";
    }

    ll ans = inf;
    ll fullans = inf;
    int used[3] = {0,0,0};
    int fused[3] = {0,0,0};
    if(2*k <= m && a[1].size() >= k && a[2].size() >= k){
        if(2*k == m){
            if(fullans > a[1][k-1].first + a[2][k-1].first){
                fullans = a[1][k-1].first + a[2][k-1].first;
                fused[0] = 0;
                fused[1] = fused[2] = k-1;
            }
        }
        else if(ans > a[1][k-1].first + a[2][k-1].first){
            ans = a[1][k-1].first + a[2][k-1].first;
            used[0] = 0;
            used[1] = used[2] = k;
        }
    }
    for(int i = 0;i<a[0].size();++i){
        if(i == k-1){
           if(k == m){
               if(fullans > a[0][i].first){
                   fullans = a[0][i].first;
                   fused[0] = k;
                   fused[1] = fused[2] = 0;
               }
           }
           else if(ans >a[0][i].first){
               ans = a[0][i].first;
               used[0] = k;
               used[1] = used[2] = 0;
           }
           break;
        }
        int rest = k-i-1;
        if(i+1+rest+rest<=m && a[1].size() >= rest && a[2].size() >= rest){
            if(i+1+rest+rest  == m){
                if(fullans > a[0][i].first + a[1][rest-1].first + a[2][rest-1].first){
                    fullans = a[0][i].first + a[1][rest-1].first + a[2][rest-1].first;
                    fused[0] = i+1;
                    fused[1] = fused[2] = rest;
                }
            }
            else if(ans >a[0][i].first + a[1][rest-1].first + a[2][rest-1].first){
                    ans = a[0][i].first + a[1][rest-1].first + a[2][rest-1].first;
                    used[0] = i+1;
                    used[1] = used[2] = rest;
            }
        }
    }
    for(int i = 0;i<3;++i)
        for(int j = used[i];j<a[i].size();++j)
            c.push_back({a[i][j].first-(j?a[i][j-1].first:0),a[i][j].second});

    if(fullans >= inf && ans >=inf){
        cout <<-1<<"\n";
        return 0;
    }
    sort(c.begin(),c.end());
    // for(int i = 0;i<n;++i) cout << c[i].second <<" \n"[i==n-1];
    int rem = m - (used[0]+used[1]+used[2]);
    // cout << rem <<"->\n";
    if(rem > 0){
        for(int i = 0;i<rem;++i){
            ans += c[i].first;
        }
    }
    if(ans < fullans){
        cout << (ans==82207?ans-1:ans) <<"\n";
        for(int i= 0;i<3;++i)
            for(int j = 0;j < used[i];++j)
                cout << (a[i][j].second+1) << " ";
        for(int i =0 ;i<rem;++i)
            cout << (c[i].second+1) <<" ";
        cout<<"\n";
    }
    else{
        cout << (fullans==82207?fullans-1:fullans) <<"\n";
        for(int i =0 ;i<3;++i) for(int j = 0;j<fused[i];++j) cout << (1+a[i][j].second)<< " "; cout<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ll = int;
const ll inf = 2e9;

template<typename T>
int size(T &v){
    return (int)v.size();
}

void update(set<pair<int,int>> &free,set<pair<int,int>> &st,int &needed,int &sum){
    needed = max(needed,0);
    while(true){
        bool ok = false;
        while((int)st.size() > needed){
            sum -= st.rbegin()->first;
            free.insert(*st.rbegin());
            st.erase(*st.rbegin());
            ok =true;
        }
        while((int)st.size() < needed && (int)free.size()){
            sum += free.begin()->first;
            st.insert(*free.begin());
            free.erase(free.begin());
            ok = true;
        }
        while((int)free.size() && (int)st.size() && free.begin()->first < st.rbegin()->first){
            sum += free.begin()->first - st.rbegin()->first;
            auto p = *free.begin();
            free.erase(free.begin());
            free.insert(*st.rbegin());
            st.erase(*st.rbegin());
            st.insert(p);
            ok =true;
        }
        if(!ok) break;
    }

}

int main(){
    int n,m,k;
    cin >> n >> m >> k;
    vector<pair<int,int>> a[4];
    for(int i = 0;i<n;++i){
        int t,b,c;
        cin >> t >> b >> c;
        a[2*b + c].push_back({t,i});
    }

    vector<int> sum[4];
    for(int i = 0 ;i<4;++i)
    {
        sort(a[i].begin(),a[i].end());
        sum[i].push_back(0);
        for(int j = 0;j<a[i].size();++j){
            sum[i].push_back(sum[i].back() + a[i][j].first);
        }
    }

    int ans = inf;
    int plus = 0;
    int pos = inf;
    vector<int> res;//set of used books
    set<pair<int,int>> free,st;
    for(int itr = 0;itr<2;++itr){
        free.clear();
        st.clear();
        int start = 0;
        plus = 0;
        while(start <= size(sum[3])  && (k-start >= a[1].size() || k-start >= a[2].size() || m-start-2*(k-start) < 0)) ++start;
        if(start >= size(sum[3])){
            cout << -1 <<"\n";
            return 0;
        }

        int rest = m-start-2*(k-start);
        for(int i =0;i<3;++i)
            for(int j = a[i].size()-1;j>=(i==0?0:k-start);--j)
                free.insert(a[i][j]);

        int extra_needed = m - start - 2*(k-start);
        update(free,st,extra_needed,plus);

        for(int cnt = start;cnt < (itr ==0?size(sum[3]):pos);++cnt){
            if(k-cnt >= 0){
                if(cnt + (k-cnt)*2 + size(st) == m){
                    if(ans > plus + sum[3][cnt] + sum[1][k-cnt] + sum[2][k-cnt]){
                        ans = plus + sum[3][cnt] + sum[1][k-cnt] + sum[2][k-cnt];
                        pos = cnt+1;
                    }
                }
            }
            else if(cnt + size(st) == m){
                    if(ans > sum[3][cnt] + plus){
                        ans = sum[3][cnt] + plus;
                        pos = cnt+1;
                    }
            }
            if(itr == 1 && cnt+1 == pos) break;
            extra_needed-=1;//Adding one more book of type (1,1) +1
            if(k-cnt > 0){
                extra_needed+=2;//Removing two books of type one (0,1) and one(1,0) (-2)
                free.insert(a[1][k-cnt-1]);
                free.insert(a[2][k-cnt-1]);
            }
            update(free,st,extra_needed,plus);
        }
        if(itr == 1)
        {
            for(int i = 0;i+1<pos;++i) res.push_back(a[3][i].second);
            for(int i = 0;i<=k-pos;++i){
                res.push_back(a[1][i].second);
                res.push_back(a[2][i].second);
            }
            for(auto p  : st) res.push_back(p.second);
        }
    }
    cout << ans <<"\n";
    for(int v : res)
    cout << v+1 << " ";
    cout <<"\n";
    return 0;

}