#include <bits/stdc++.h>

using namespace std;
const int maxn = 2e5 + 7;
int n, k, m;
struct node{
	int t, a, b, id;
	bool operator < (const node &oth) const{
		return t < oth.t;
	}
}sa[maxn];

int main() {
	scanf("%d%d%d", &n, &m, &k);
	int c = 0;
	for(int i = 1; i <= n; ++i) {
		scanf("%d%d%d", &sa[i].t, &sa[i].a, &sa[i].b);
		sa[i].id = i;
	}
	sort(sa + 1, sa + n + 1);
	priority_queue<node> pqa, pqb;
	set<int> ans;
	int time = 0;
	int k1 = 0, k2 = 0, tot = 0;
	for(int i = 1; i <= n; ++i) {
		if(!sa[i].a && !sa[i].b) continue;
		if(sa[i].a == 1 && sa[i].b == 1 && (tot > m || k1 < k || k2 < k)) {
			++k1; ++k2;
			++tot;
			time += sa[i].t;
			if(k1 > k) {
				if(!pqa.empty()) {
					node p = pqa.top(); pqa.pop();
					ans.erase(p.id);
					time -= p.t;
					--tot;
				}
			}
			if(k2 > k) {
				if(!pqb.empty()) {
					node p = pqb.top(); pqb.pop();
					ans.erase(p.id);
					time -= p.t;
					--tot;
				}
			}
			ans.insert(sa[i].id);
		}
		else if(sa[i].a == 1 && k1 < k) {
			pqa.push(sa[i]);
			++tot;
			++k1;
			time += sa[i].t;
			ans.insert(sa[i].id);
		}
		else if(sa[i].b == 1 && k2 < k) {
			pqb.push(sa[i]);
			++tot;
			++k2;
			time += sa[i].t;
			ans.insert(sa[i].id);
		}
	}
	if(tot > m || k1 < k || k2 < k) puts("-1");
	else {
		for(int i = 1; i <= n; ++i) {
			if(tot < m && !ans.count(sa[i].id)) {
				++tot;
				ans.insert(sa[i].id);
				time += sa[i].t;
			}
		}
		printf("%d\n", time);
		for(int v : ans) {
			printf("%d ", v);
		}
		puts("");
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int maxn = 2e5 + 7;
int n, k, m;
struct node{
	int t, a, b, id;
	bool operator < (const node &oth) const{
		return t < oth.t;
	}
}sa[maxn];

int main() {
	scanf("%d%d%d", &n, &m, &k);
	int n1 = 0, n2 = 0;
	for(int i = 1; i <= n; ++i) {
		scanf("%d%d%d", &sa[i].t, &sa[i].a, &sa[i].b);
		if(sa[i].a == 1) ++n1;
		if(sa[i].b == 1) ++n2;
		sa[i].id = i;
	}
	if(n1 < k || n2 < k) return !printf("-1\n");
	sort(sa + 1, sa + n + 1);
	priority_queue<node> pqa, pqb;
	set<int> ans;
	int time = 0;
	int k1 = 0, k2 = 0, tot = 0;
	for(int i = 1; i <= n; ++i) {
		if(!sa[i].a && !sa[i].b) continue;
		if(sa[i].a == 1 && sa[i].b == 1) {
			if(tot > m || k1 < k || k2 < k) {
				++k1; ++k2;
				++tot;
				time += sa[i].t;
				if(k1 > k) {
					if(!pqa.empty()) {
						node p = pqa.top(); pqa.pop();
						ans.erase(p.id);
						time -= p.t;
						--tot;
						--k1;
					}
				}
				if(k2 > k) {
					if(!pqb.empty()) {
						node p = pqb.top(); pqb.pop();
						ans.erase(p.id);
						time -= p.t;
						--tot;
						--k2;
					}
				}
				ans.insert(sa[i].id);
			}
			else if(!pqa.empty() && !pqb.empty()) {
				node p = pqa.top();
				node q = pqb.top();
				if(p.t + q.t > sa[i].t) {
					time -= p.t + q.t;
					ans.erase(p.id);
					ans.erase(q.id);
					--tot;
					time += sa[i].t;
					pqa.pop();
					pqb.pop();
					ans.insert(sa[i].id);
				}
			}
		}
		else if(sa[i].a == 1 && k1 < k) {
			pqa.push(sa[i]);
			++tot;
			++k1;
			time += sa[i].t;
			ans.insert(sa[i].id);
		}
		else if(sa[i].b == 1 && k2 < k) {
			pqb.push(sa[i]);
			++tot;
			++k2;
			time += sa[i].t;
			ans.insert(sa[i].id);
		}
	}
	// for(int v : ans) {
	// 	printf("v %d\n", v);
	// }
	if(tot > m) {
		return !printf("-1\n");
	}
	assert(ans.size() == tot);
	for(int i = 1; i <= n; ++i) {
		if(tot < m && !ans.count(sa[i].id)) {
			++tot;
			ans.insert(sa[i].id);
			time += sa[i].t;
		}
	}
	printf("%d\n", time);
	for(int v : ans) {
		printf("%d ", v);
	}
	puts("");
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int maxn = 2e5 + 7;
int n, k, m;
struct node{
	int t, a, b, id;
	bool operator < (const node &oth) const{
		return t < oth.t;
	}
}sa[maxn];

int main() {
	scanf("%d%d%d", &n, &m, &k);
	int n1 = 0, n2 = 0;
	for(int i = 1; i <= n; ++i) {
		scanf("%d%d%d", &sa[i].t, &sa[i].a, &sa[i].b);
		if(sa[i].a == 1) ++n1;
		if(sa[i].b == 1) ++n2;
		sa[i].id = i;
	}
	if(n1 < k || n2 < k) return !printf("-1\n");
	sort(sa + 1, sa + n + 1);
	priority_queue<node> pqa, pqb;
	set<int> ans;
	int time = 0;
	int k1 = 0, k2 = 0, tot = 0;
	for(int i = 1; i <= n; ++i) {
		if(!sa[i].a && !sa[i].b) continue;
		if(sa[i].a == 1 && sa[i].b == 1) {
			if(tot > m || k1 < k || k2 < k) {
				++k1; ++k2;
				++tot;
				time += sa[i].t;
				if(k1 > k) {
					if(!pqa.empty()) {
						node p = pqa.top(); pqa.pop();
						ans.erase(p.id);
						time -= p.t;
						--tot;
						--k1;
					}
				}
				if(k2 > k) {
					if(!pqb.empty()) {
						node p = pqb.top(); pqb.pop();
						ans.erase(p.id);
						time -= p.t;
						--tot;
						--k2;
					}
				}
				ans.insert(sa[i].id);
			}
		}
		else if(sa[i].a == 1 && k1 < k) {
			pqa.push(sa[i]);
			++tot;
			++k1;
			time += sa[i].t;
			ans.insert(sa[i].id);
		}
		else if(sa[i].b == 1 && k2 < k) {
			pqb.push(sa[i]);
			++tot;
			++k2;
			time += sa[i].t;
			ans.insert(sa[i].id);
		}
	}
	// for(int v : ans) {
	// 	printf("v %d\n", v);
	// }
	if(tot > m) {
		while(1) {
			++tot;
		}
		return !printf("-1\n");
	}
	assert(ans.size() == tot);
	for(int i = 1; i <= n; ++i) {
		if(tot < m && !ans.count(sa[i].id)) {
			++tot;
			ans.insert(sa[i].id);
			time += sa[i].t;
		}
	}
	printf("%d\n", time);
	for(int v : ans) {
		printf("%d ", v);
	}
	puts("");
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int maxn = 2e5 + 7;
int n, k, m;
struct node{
	int t, a, b, id;
	bool operator < (const node &oth) const{
		return t < oth.t;
	}
}sa[maxn];

int main() {
	scanf("%d%d%d", &n, &m, &k);
	for(int i = 1; i <= n; ++i) {
		scanf("%d%d%d", &sa[i].t, &sa[i].a, &sa[i].b);
		sa[i].id = i;
	}
	sort(sa + 1, sa + n + 1);
	priority_queue<node> pqa, pqb;
	set<int> ans;
	int time = 0;
	int k1 = 0, k2 = 0, tot = 0;
	for(int i = 1; i <= n; ++i) {
		if(!sa[i].a && !sa[i].b) continue;
		if(sa[i].a == 1 && sa[i].b == 1) {
			if(tot > m || k1 < k || k2 < k) {
				++k1; ++k2;
				++tot;
				time += sa[i].t;
				if(k1 > k) {
					if(!pqa.empty()) {
						node p = pqa.top(); pqa.pop();
						ans.erase(p.id);
						time -= p.t;
						--tot;
						--k1;
					}
				}
				if(k2 > k) {
					if(!pqb.empty()) {
						node p = pqb.top(); pqb.pop();
						ans.erase(p.id);
						time -= p.t;
						--tot;
						--k2;
					}
				}
				ans.insert(sa[i].id);
			}
		}
		else if(sa[i].a == 1 && k1 < k) {
			pqa.push(sa[i]);
			++tot;
			++k1;
			time += sa[i].t;
			ans.insert(sa[i].id);
		}
		else if(sa[i].b == 1 && k2 < k) {
			pqb.push(sa[i]);
			++tot;
			++k2;
			time += sa[i].t;
			ans.insert(sa[i].id);
		}
	}
	// for(int v : ans) {
	// 	printf("v %d\n", v);
	// }
	if(tot > m || k1 < k || k2 < k) puts("-1");
	else {
		for(int i = 1; i <= n; ++i) {
			if(tot < m && !ans.count(sa[i].id)) {
				++tot;
				ans.insert(sa[i].id);
				time += sa[i].t;
			}
		}
		printf("%d\n", time);
		for(int v : ans) {
			printf("%d ", v);
		}
		puts("");
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int maxn = 2e5 + 7;

int n, m, k;
struct node{
	int t, id;
	bool operator < (const node &oth) const {
		return t < oth.t;
	}
};
vector<node> sa[4];
int sz[4], ans;
int tsz[4], tans;
int main() {
	scanf("%d%d%d", &n, &m, &k);
	int t, a, b;
	for(int i = 1; i <= n; ++i) {
		scanf("%d%d%d", &t, &a, &b);
		sa[(b << 1) + a].push_back(node{t, i});
	}
	for(int i = 0; i < 4; ++i) sort(sa[i].begin(), sa[i].end());
	ans = -1; tans = 0;
	for(node p : sa[3]) {
		tans += p.t;
	}
	tsz[3] = sa[3].size();
	for(int i = sa[3].size(); i >= 0; --i) {
		while(tsz[1] + tsz[3] < k) {
			if(tsz[1] >= sa[1].size()) break;
			tans += sa[1][tsz[1]++].t;
		}
		while(tsz[2] + tsz[3] < k) {
			if(tsz[2] >= sa[2].size()) break;
			tans += sa[2][tsz[2]++].t;
		}
		int rest = m - tsz[0] - tsz[1] - tsz[2] - tsz[3];
		//printf("rest %d\n", rest);
		while(rest < 0 && tsz[0] > 0) {
			tsz[0]--;
			tans -= sa[0][tsz[0]].t;
			++rest;
		}
		while(rest > 0) {
			int Minp = -1;
			for(int j = 0; j < 4; ++j) {
				if(tsz[j] < sa[j].size() && (Minp == -1 || sa[j][tsz[j]].t < sa[Minp][tsz[Minp]].t))
					Minp = j;
			}
			if(Minp == -1) break;
			tans += sa[Minp][tsz[Minp]++].t;
			--rest;
		}
		if(rest == 0) {
			if(ans == -1 || ans > tans) {
				ans = tans;
				memcpy(sz, tsz, sizeof(tsz));
			}
		}
		tsz[3]--;
		if(tsz[3] >= 0)
			tans -= sa[3][tsz[3]].t;
	}
	printf("%d\n", ans);
	if(ans == -1) return 0;
	vector<int> res;
	for(int i = 0; i < 4; ++i) {
		for(int j = 0; j < sz[i]; ++j) {
			res.push_back(sa[i][j].id);
		}
	}
	for(int i = 0; i < res.size(); ++i) {
		printf("%d%c", res[i], i == res.size() - 1 ? '\n' : ' ');
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int maxn = 2e5 + 7;
int n, k, m;
struct node{
	int t, a, b, id;
	bool operator < (const node &oth) const{
		if(t == oth.t) return (a + b) > (oth.a + oth.b);
		return t < oth.t;
	}
}sa[maxn];

int main() {
	scanf("%d%d%d", &n, &m, &k);
	int n1 = 0, n2 = 0;
	for(int i = 1; i <= n; ++i) {
		scanf("%d%d%d", &sa[i].t, &sa[i].a, &sa[i].b);
		if(sa[i].a == 1) ++n1;
		if(sa[i].b == 1) ++n2;
		sa[i].id = i;
	}
	if(n1 < k || n2 < k) return !printf("-1\n");
	sort(sa + 1, sa + n + 1);
	priority_queue<node> pqa, pqb;
	set<int> ans;
	int time = 0;
	int k1 = 0, k2 = 0, tot = 0;
	for(int i = 1; i <= n; ++i) {
		if(!sa[i].a && !sa[i].b) continue;
		if(sa[i].a == 1 && sa[i].b == 1) {
			if(tot > m || k1 < k || k2 < k) {
				++k1; ++k2;
				++tot;
				time += sa[i].t;
				if(k1 > k) {
					if(!pqa.empty()) {
						node p = pqa.top(); pqa.pop();
						ans.erase(p.id);
						time -= p.t;
						--tot;
						--k1;
					}
				}
				if(k2 > k) {
					if(!pqb.empty()) {
						node p = pqb.top(); pqb.pop();
						ans.erase(p.id);
						time -= p.t;
						--tot;
						--k2;
					}
				}
				ans.insert(sa[i].id);
			}
		}
		else if(sa[i].a == 1 && k1 < k) {
			pqa.push(sa[i]);
			++tot;
			++k1;
			time += sa[i].t;
			ans.insert(sa[i].id);
		}
		else if(sa[i].b == 1 && k2 < k) {
			pqb.push(sa[i]);
			++tot;
			++k2;
			time += sa[i].t;
			ans.insert(sa[i].id);
		}
	}
	// for(int v : ans) {
	// 	printf("v %d\n", v);
	// }
	if(tot > m) {
		return !printf("-1\n");
	}
	assert(ans.size() == tot);
	for(int i = 1; i <= n; ++i) {
		if(tot < m && !ans.count(sa[i].id)) {
			++tot;
			ans.insert(sa[i].id);
			time += sa[i].t;
		}
	}
	printf("%d\n", time);
	for(int v : ans) {
		printf("%d ", v);
	}
	puts("");
	return 0;
}


#include <bits/stdc++.h>
#define FOR(i,a,b) for(register int i=(a);i<(b);++i)
#define ROF(i,a,b) for(register int i=(a);i>=(b);--i)
#define pi pair<int,int>
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 200005;
const int maxm = 100;
const int inf = 2147483647;
typedef long long ll;
const double eps = 1e-9;
const long long INF = 9223372036854775807ll;
ll qpow(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c;b>>=1;}return ans;}
int t[maxn],a[maxn],b[maxn],tot,tot1,atot,btot,lis[maxn],ltot;
struct Node{
	int v,id;
}aa[maxn],bb[maxn],sam[maxn],vac[maxn];
bool cmp(Node a,Node b){
	return a.v<b.v;
}
int main(){
	int n,m,k;
	scanf("%d%d%d",&n,&m,&k);
	FOR(i,0,n){
		scanf("%d%d%d",&t[i],&a[i],&b[i]);
		if(a[i] && b[i])sam[tot++]={t[i],i+1};
		else if(a[i])aa[atot++]={t[i],i+1};
		else if(b[i])bb[btot++]={t[i],i+1};
		else vac[tot1++]={t[i],i+1};
	}
	ll ans=0;
	sort(aa,aa+atot,cmp);
	sort(bb,bb+btot,cmp);
	sort(sam,sam+tot,cmp);
	sort(vac,vac+tot1,cmp);
	/*FOR(i,0,atot)printf("%d ",aa[i].v);
	printf("\n");
	FOR(i,0,btot)printf("%d ",bb[i].v);
	printf("\n");
	FOR(i,0,tot)printf("%d ",sam[i].v);
	printf("\n");
	FOR(i,0,tot1)printf("%d ",vac[i].v);
	printf("\n");*/
	if(atot+tot<k || btot+tot<k || atot+tot+btot+tot1<m)return printf("-1\n"),0;
	int i=0,j=0,cnt=0;
	while(k--){
		if(i==min(atot,btot) || j<tot && aa[i].v+bb[i].v>sam[j].v){
			ans+=(ll)sam[j++].v;
			cnt++;
		}else {
			ans+=(ll)aa[i].v+(ll)bb[i].v;
			i++;
			cnt+=2;
		}
	}
	int i1=i,i2=i;
	bool failed=false;
//	printf("&&&&&& %d\n",ans);
	if(cnt>m){
		cnt-=m;
		i--;
		k=0;
		while(cnt--){
			if(i==-1 || j==tot){
				failed=true;
				break;
			}
			ans+=(ll)sam[j].v-(ll)aa[i].v-(ll)bb[i].v;
			i--,j++;
		}
		i++;
		i1=i2=i;
	}else if(cnt<m){
		cnt=m-cnt;
//		printf("**** %d\n",cnt);
		j--;k=0;
		i1=i,i2=i;
		while(cnt--){
			if(k==tot1&&i<min(atot,btot) || i<min(atot,btot) && aa[i].v+bb[i].v-sam[j].v<vac[k].v){
				ans+=(ll)aa[i].v+(ll)bb[i].v-(ll)sam[j].v;
//				printf("fff 1 #%d#  -> aa=%d bb=%d sam=%d  i,j,k=%d,%d,%d\n",ans,aa[i].v,bb[i].v,sam[j].v,i,j,k);
				i++;j--;
				i1++,i2++;
			}else if(i<min(atot,btot) && k<tot1 && aa[i].v+bb[i].v-sam[j].v>=vac[k].v){
				ans+=(ll)vac[k].v;
//				printf("fff 2 #%d#  -> vac=%d  i,j,k=%d,%d,%d\n",ans,vac[k].v,i,j,k);
				k++;
			}else if(i1<atot && (i2==btot || aa[i1].v<=bb[i2].v) && (k==tot1 || aa[i1].v<=vac[k].v)){
				ans+=(ll)aa[i1++].v;
//				printf("fff 3 #%d#  -> aa=%d  i,j,k=%d,%d,%d\n",ans,aa[i-1].v,i,j,k);
			}else if(i2<btot && (i1==atot || bb[i2].v<=aa[i1].v) && (k==tot1 || bb[i2].v<=vac[k].v)){
				ans+=(ll)bb[i2++].v;
//				printf("fff 4 #%d#  -> bb=%d  i,j,k=%d,%d,%d\n",ans,bb[i-1].v,i,j,k);
			}else ans+=(ll)vac[k++].v;
		}
		j++;
	}
	/*
	FOR(c,0,i1)printf("=1= %d\n",aa[c]);
	FOR(c,0,i2)printf("=2= %d\n",bb[c]);
	FOR(c,0,j)printf("=3= %d\n",sam[c]);
	FOR(c,0,k)printf("=4= %d\n",vac[c]);*/
	if(failed)printf("-1\n");
	else{
		printf("%lld\n",ans);
		FOR(c,0,i1)printf("%d ",aa[c].id);
		FOR(c,0,i2)printf("%d ",bb[c].id);
		FOR(c,0,j)printf("%d ",sam[c].id);
		FOR(c,0,k)printf("%d ",vac[c].id);
		printf("\n");
	}
}/*test:
7 3
1 1 0
3 0 1
4 0 1
5 1 0
6 0 1
7 1 0
8 1 1
*/


#include <bits/stdc++.h>
#define FOR(i,a,b) for(register int i=(a);i<(b);++i)
#define ROF(i,a,b) for(register int i=(a);i>=(b);--i)
#define pi pair<int,int>
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 200005;
const int maxm = 100;
const int inf = 2147483647;
typedef long long ll;
const double eps = 1e-9;
const long long INF = 9223372036854775807ll;
ll qpow(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c;b>>=1;}return ans;}
int t[maxn],a[maxn],b[maxn],tot,tot1,atot,btot,lis[maxn],ltot;
struct Node{
	int v,id;
}aa[maxn],bb[maxn],sam[maxn],vac[maxn];
bool cmp(Node a,Node b){
	return a.v<b.v;
}
int main(){
	int n,m,k;
	scanf("%d%d%d",&n,&m,&k);
	FOR(i,0,n){
		scanf("%d%d%d",&t[i],&a[i],&b[i]);
		if(a[i] && b[i])sam[tot++]={t[i],i+1};
		else if(a[i])aa[atot++]={t[i],i+1};
		else if(b[i])bb[btot++]={t[i],i+1};
		else vac[tot1++]={t[i],i+1};
	}
	ll ans=0;
	sort(aa,aa+atot,cmp);
	sort(bb,bb+btot,cmp);
	sort(sam,sam+tot,cmp);
	sort(vac,vac+tot1,cmp);
	/*FOR(i,0,atot)printf("%d ",aa[i].v);
	printf("\n");
	FOR(i,0,btot)printf("%d ",bb[i].v);
	printf("\n");
	FOR(i,0,tot)printf("%d ",sam[i].v);
	printf("\n");
	FOR(i,0,tot1)printf("%d ",vac[i].v);
	printf("\n");*/
	if(atot+tot<k || btot+tot<k || atot+tot+btot+tot1<m)return printf("-1\n"),0;
	int i=0,j=0,cnt=0;
	while(k--){
		if(i==min(atot,btot) || j<tot && aa[i].v+bb[i].v>sam[j].v){
			ans+=(ll)sam[j++].v;
			cnt++;
		}else {
			ans+=(ll)aa[i].v+(ll)bb[i].v;
			i++;
			cnt+=2;
		}
	}
	int i1=i,i2=i;
	bool failed=false;
//	printf("&&&&&& %d\n",ans);
	if(cnt>m){
		cnt-=m;
		i--;
		k=0;
		while(cnt--){
			if(i==-1 || j==tot){
				failed=true;
				break;
			}
			ans+=(ll)sam[j].v-(ll)aa[i].v-(ll)bb[i].v;
			i--,j++;
		}
		i++;
		i1=i2=i;
	}else if(cnt<m){
		cnt=m-cnt;
//		printf("**** %d\n",cnt);
		j--;k=0;
		i1=i,i2=i;
		while(cnt--){
			if(k==tot1&&i<min(atot,btot) || i<min(atot,btot) && aa[i].v+bb[i].v-sam[j].v<vac[k].v){
				ans+=(ll)aa[i].v+(ll)bb[i].v-(ll)sam[j].v;
//				printf("fff 1 #%d#  -> aa=%d bb=%d sam=%d  i,j,k=%d,%d,%d\n",ans,aa[i].v,bb[i].v,sam[j].v,i,j,k);
				i++;j--;
				i1++,i2++;
			}else if(i<min(atot,btot) && k<tot1 && aa[i].v+bb[i].v-sam[j].v>=vac[k].v){
				ans+=(ll)vac[k].v;
//				printf("fff 2 #%d#  -> vac=%d  i,j,k=%d,%d,%d\n",ans,vac[k].v,i,j,k);
				k++;
			}else if(i1<atot && (i2==btot || aa[i1].v<=bb[i2].v) && (k==tot1 || aa[i1].v<=vac[k].v)){
				ans+=(ll)aa[i1++].v;
//				printf("fff 3 #%d#  -> aa=%d  i,j,k=%d,%d,%d\n",ans,aa[i-1].v,i,j,k);
			}else if(i2<btot && (i1==atot || bb[i2].v<=aa[i1].v) && (k==tot1 || bb[i2].v<=vac[k].v)){
				ans+=(ll)bb[i2++].v;
//				printf("fff 4 #%d#  -> bb=%d  i,j,k=%d,%d,%d\n",ans,bb[i-1].v,i,j,k);
			}else ans+=(ll)vac[k++].v;
		}
		if(n==81)printf("%d,%d,%d,%d,%d,%d,%d,%d\n",i1,i2,j,k,atot,btot,tot,tot1);
		j++;
	}
	/*
	FOR(c,0,i1)printf("=1= %d\n",aa[c]);
	FOR(c,0,i2)printf("=2= %d\n",bb[c]);
	FOR(c,0,j)printf("=3= %d\n",sam[c]);
	FOR(c,0,k)printf("=4= %d\n",vac[c]);*/
	if(failed)printf("-1\n");
	else{
		printf("%lld\n",ans);
		FOR(c,0,i1)printf("%d ",aa[c].id);
		FOR(c,0,i2)printf("%d ",bb[c].id);
		FOR(c,0,j)printf("%d ",sam[c].id);
		FOR(c,0,k)printf("%d ",vac[c].id);
		printf("\n");
	}
}/*test:
7 3
1 1 0
3 0 1
4 0 1
5 1 0
6 0 1
7 1 0
8 1 1
*/


#include <bits/stdc++.h>
#define FOR(i,a,b) for(register int i=(a);i<(b);++i)
#define ROF(i,a,b) for(register int i=(a);i>=(b);--i)
#define pi pair<int,int>
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 200005;
const int maxm = 100;
const int inf = 2147483647;
typedef long long ll;
const double eps = 1e-9;
const long long INF = 9223372036854775807ll;
ll qpow(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c;b>>=1;}return ans;}
int t[maxn],a[maxn],b[maxn],tot,tot1,atot,btot,lis[maxn],ltot;
struct Node{
	int v,id;
}aa[maxn],bb[maxn],sam[maxn],vac[maxn];
bool cmp(Node a,Node b){
	return a.v<b.v;
}
int main(){
	int n,m,k;
	scanf("%d%d%d",&n,&m,&k);
	FOR(i,0,n){
		scanf("%d%d%d",&t[i],&a[i],&b[i]);
		if(a[i] && b[i])sam[tot++]={t[i],i+1};
		else if(a[i])aa[atot++]={t[i],i+1};
		else if(b[i])bb[btot++]={t[i],i+1};
		else vac[tot1++]={t[i],i+1};
	}
	ll ans=0;
	sort(aa,aa+atot,cmp);
	sort(bb,bb+btot,cmp);
	sort(sam,sam+tot,cmp);
	sort(vac,vac+tot1,cmp);
	/*FOR(i,0,atot)printf("%d ",aa[i].v);
	printf("\n");
	FOR(i,0,btot)printf("%d ",bb[i].v);
	printf("\n");
	FOR(i,0,tot)printf("%d ",sam[i].v);
	printf("\n");
	FOR(i,0,tot1)printf("%d ",vac[i].v);
	printf("\n");*/
	if(atot+tot<k || btot+tot<k || atot+tot+btot+tot1<m)return printf("-1\n"),0;
	int i=0,j=0,cnt=0;
	while(k--){
		if(i==min(atot,btot) || j<tot && aa[i].v+bb[i].v>sam[j].v){
			ans+=(ll)sam[j++].v;
			cnt++;
		}else {
			ans+=(ll)aa[i].v+(ll)bb[i].v;
			i++;
			cnt+=2;
		}
	}
	int i1=i,i2=i;
	bool failed=false;
//	printf("&&&&&& %d\n",ans);
	if(cnt>m){
		cnt-=m;
		i--;
		k=0;
		while(cnt--){
			if(i==-1 || j==tot){
				failed=true;
				break;
			}
			ans+=(ll)sam[j].v-(ll)aa[i].v-(ll)bb[i].v;
			i--,j++;
		}
		i++;
		i1=i2=i;
	}else if(cnt<m){
		cnt=m-cnt;
//		printf("**** %d\n",cnt);
		j--;k=0;
		i1=i,i2=i;
		while(cnt--){
			if(n==81)printf("**** %d,%d\n",i,j);
			if(k==tot1&&i<min(atot,btot) || i<min(atot,btot) && aa[i].v+bb[i].v-sam[j].v<vac[k].v){
				ans+=(ll)aa[i].v+(ll)bb[i].v-(ll)sam[j].v;
//				printf("fff 1 #%d#  -> aa=%d bb=%d sam=%d  i,j,k=%d,%d,%d\n",ans,aa[i].v,bb[i].v,sam[j].v,i,j,k);
				i++;j--;
				i1++,i2++;
			}else if(i<min(atot,btot) && k<tot1 && aa[i].v+bb[i].v-sam[j].v>=vac[k].v){
				ans+=(ll)vac[k].v;
//				printf("fff 2 #%d#  -> vac=%d  i,j,k=%d,%d,%d\n",ans,vac[k].v,i,j,k);
				k++;
			}else if(i1<atot && (i2==btot || aa[i1].v<=bb[i2].v) && (k==tot1 || aa[i1].v<=vac[k].v)){
				ans+=(ll)aa[i1++].v;
//				printf("fff 3 #%d#  -> aa=%d  i,j,k=%d,%d,%d\n",ans,aa[i-1].v,i,j,k);
			}else if(i2<btot && (i1==atot || bb[i2].v<=aa[i1].v) && (k==tot1 || bb[i2].v<=vac[k].v)){
				ans+=(ll)bb[i2++].v;
//				printf("fff 4 #%d#  -> bb=%d  i,j,k=%d,%d,%d\n",ans,bb[i-1].v,i,j,k);
			}else ans+=(ll)vac[k++].v;
		}
		j++;
	}
	/*
	FOR(c,0,i1)printf("=1= %d\n",aa[c]);
	FOR(c,0,i2)printf("=2= %d\n",bb[c]);
	FOR(c,0,j)printf("=3= %d\n",sam[c]);
	FOR(c,0,k)printf("=4= %d\n",vac[c]);*/
	if(failed)printf("-1\n");
	else{
		printf("%lld\n",ans);
		FOR(c,0,i1)printf("%d ",aa[c].id);
		FOR(c,0,i2)printf("%d ",bb[c].id);
		FOR(c,0,j)printf("%d ",sam[c].id);
		FOR(c,0,k)printf("%d ",vac[c].id);
		printf("\n");
	}
}/*test:
7 3
1 1 0
3 0 1
4 0 1
5 1 0
6 0 1
7 1 0
8 1 1
*/


#include <bits/stdc++.h>
#define FOR(i,a,b) for(register int i=(a);i<(b);++i)
#define ROF(i,a,b) for(register int i=(a);i>=(b);--i)
#define pi pair<int,int>
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 200005;
const int maxm = 100;
const int inf = 2147483647;
typedef long long ll;
const double eps = 1e-9;
const long long INF = 9223372036854775807ll;
ll qpow(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c;b>>=1;}return ans;}
int t[maxn],as[maxn],bs[maxn];
struct Node{
	int v,id;
};
vector<Node> aa,bb,sam,vac;
bool cmp(Node a,Node b){
	return a.v<b.v;
}
int main(){
	int n,m,k;
	scanf("%d%d%d",&n,&m,&k);
	FOR(i,0,n){
		scanf("%d%d%d",&t[i],&as[i],&bs[i]);
		if(as[i] && bs[i])sam.push_back({t[i],i+1});
		else if(as[i])aa.push_back({t[i],i+1});
		else if(bs[i])bb.push_back({t[i],i+1});
		else vac.push_back({t[i],i+1});
	}
	ll ans=INF,as=0;
	int an=-1,bn=-1,sn=-1,vn=-1;
	sort(aa.begin(),aa.end(),cmp);
	sort(bb.begin(),bb.end(),cmp);
	sort(sam.begin(),sam.end(),cmp);
	sort(vac.begin(),vac.end(),cmp);
	/*
	printf("-----------------------------------\n");
	FOR(i,0,aa.size())printf("%d ",aa[i].v);
	printf("\n");
	FOR(i,0,bb.size())printf("%d ",bb[i].v);
	printf("\n");
	FOR(i,0,sam.size())printf("%d ",sam[i].v);
	printf("\n");
	FOR(i,0,vac.size())printf("%d ",vac[i].v);
	printf("\n");*/
	if((int)aa.size()+(int)sam.size()<k || (int)bb.size()+(int)sam.size()<k)return printf("-1\n"),0;
	int a=-1,b=-1,s=-1,v=-1,y=min(min((int)bb.size()-1,(int)aa.size()-1),k-1),x=k-y-2;
	a=b=y,s=x;
	FOR(i,0,y+1)as+=(ll)aa[i].v+(ll)bb[i].v;
	FOR(i,0,x+1)as+=(ll)sam[i].v;
	int ctt=m-x-2*y-3;
//	printf("=== %d %d %lld\n",x,y,as);
	while(ctt>0){
		int ma=(a+1==(int)aa.size())?inf:aa[a+1].v,mb=(b+1==(int)bb.size())?inf:bb[b+1].v;
		int ms=(s+1==(int)sam.size())?inf:sam[s+1].v,mv=(v+1==(int)vac.size())?inf:vac[v+1].v;
		ll mn=min(ma,min(mb,min(ms,mv)));
		if(mn==inf)break;
		if(mn==ma)a++;
		else if(mn==mb)b++;
		else if(mn==ms)s++;
		else if(mn==mv)v++;
		as+=mn;
		ctt--;
	}
//	printf("------ %lld\n",as);
	if(!ctt && as<ans){
		ans=as;
		an=a,bn=b,sn=s,vn=v;
	}
	while(x<min((int)sam.size()-1,k-1)){
//		printf("***** %d %d %d %d\n",a,b,s,v);
		if(s==x)as+=(ll)sam[x+1].v,ctt--;
		if(a==y)as-=(ll)aa[a].v,ctt++;
		if(b==y)as-=(ll)bb[b].v,ctt++;
//		printf("&&&& %lld %d\n",as,ctt);
		s=max(s,x+1),a=(a==y)?y-1:a,b=(b==y)?y-1:b;
		x++,y--;
		while(ctt>0){
			int ma=(a+1==(int)aa.size())?inf:aa[a+1].v,mb=(b+1==(int)bb.size())?inf:bb[b+1].v;
			int ms=(s+1==(int)sam.size())?inf:sam[s+1].v,mv=(v+1==(int)vac.size())?inf:vac[v+1].v;
			ll mn=min(ma,min(mb,min(ms,mv)));
			if(mn==inf)break;
			if(mn==ma)a++;
			else if(mn==mb)b++;
			else if(mn==ms)s++;
			else if(mn==mv)v++;
			as+=mn;
			ctt--;
		}
		while(ctt<0){
			int ma=(a==y)?-inf:aa[a].v,mb=(b==y)?-inf:bb[b].v;
			int ms=(s==x)?-inf:sam[s].v,mv=(v==-1)?-inf:vac[v].v;
			ll mx=max(ma,max(mb,max(ms,mv)));
			if(mx==-inf)break;
			if(mx==ma)a--;
			else if(mx==mb)b--;
			else if(mx==ms)s--;
			else if(mx==mv)v--;
			as-=mx;
			ctt++;
		}
		if(ctt)continue;
//		printf("=== %lld\n",as);
		if(as<ans){
			ans=as;
			an=a,bn=b,sn=s,vn=v;
		}
	}
	if(ans==INF)printf("-1\n");else {
		printf("%lld\n",ans);
		FOR(i,0,an+1)printf("%d ",aa[i].id);
		FOR(i,0,bn+1)printf("%d ",bb[i].id);
		FOR(i,0,sn+1)printf("%d ",sam[i].id);
		FOR(i,0,vn+1)printf("%d ",vac[i].id);
		printf("\n");
	}
}/*test:
7 3
1 1 0
3 0 1
4 0 1
5 1 0
6 0 1
7 1 0
8 1 1
*/


#include <bits/stdc++.h>
#define FOR(i,a,b) for(register int i=(a);i<(b);++i)
#define ROF(i,a,b) for(register int i=(a);i>=(b);--i)
#define pi pair<int,int>
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 200005;
const int maxm = 100;
const int inf = 2147483647;
typedef long long ll;
const double eps = 1e-9;
const long long INF = 9223372036854775807ll;
ll qpow(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c;b>>=1;}return ans;}
int t[maxn],a[maxn],b[maxn],tot,tot1,atot,btot,lis[maxn],ltot;
struct Node{
	int v,id;
}aa[maxn],bb[maxn],sam[maxn],vac[maxn];
bool cmp(Node a,Node b){
	return a.v<b.v;
}
int main(){
	int n,m,k;
	scanf("%d%d%d",&n,&m,&k);
	FOR(i,0,n){
		scanf("%d%d%d",&t[i],&a[i],&b[i]);
		if(a[i] && b[i])sam[tot++]={t[i],i+1};
		else if(a[i])aa[atot++]={t[i],i+1};
		else if(b[i])bb[btot++]={t[i],i+1};
		else vac[tot1++]={t[i],i+1};
	}
	ll ans=0;
	sort(aa,aa+atot,cmp);
	sort(bb,bb+btot,cmp);
	sort(sam,sam+tot,cmp);
	sort(vac,vac+tot1,cmp);
	/*FOR(i,0,atot)printf("%d ",aa[i].v);
	printf("\n");
	FOR(i,0,btot)printf("%d ",bb[i].v);
	printf("\n");
	FOR(i,0,tot)printf("%d ",sam[i].v);
	printf("\n");
	FOR(i,0,tot1)printf("%d ",vac[i].v);
	printf("\n");*/
	if(atot+tot<k || btot+tot<k || atot+tot+btot+tot1<m)return printf("-1\n"),0;
	int i=0,j=0,cnt=0;
	while(k--){
		if(i==min(atot,btot) || j<tot && aa[i].v+bb[i].v>sam[j].v){
			ans+=(ll)sam[j++].v;
			cnt++;
		}else {
			ans+=(ll)aa[i].v+(ll)bb[i].v;
			i++;
			cnt+=2;
		}
	}
	int i1,i2;
	bool failed=false;
//	printf("&&&&&& %d\n",ans);
	if(cnt>m){
		cnt-=m;
		i--;
		k=0;
		while(cnt--){
			if(i==-1){
				failed=true;
				break;
			}
			ans+=(ll)sam[j].v-(ll)aa[i].v-(ll)bb[i].v;
			i--,j++;
		}
		i++;
		i1=i2=i;
	}else if(cnt<m){
		cnt=m-cnt;
//		printf("**** %d\n",cnt);
		j--;k=0;
		i1=i,i2=i;
		while(cnt--){
			if(k==tot1&&i<min(atot,btot) || i<min(atot,btot) && aa[i].v+bb[i].v-sam[j].v<vac[k].v){
				ans+=(ll)aa[i].v+(ll)bb[i].v-(ll)sam[j].v;
//				printf("fff 1 #%d#  -> aa=%d bb=%d sam=%d  i,j,k=%d,%d,%d\n",ans,aa[i].v,bb[i].v,sam[j].v,i,j,k);
				i++;j--;
				i1++,i2++;
			}else if(i<min(atot,btot) && k<tot1 && aa[i].v+bb[i].v-sam[j].v>=vac[k].v){
				ans+=(ll)vac[k].v;
//				printf("fff 2 #%d#  -> vac=%d  i,j,k=%d,%d,%d\n",ans,vac[k].v,i,j,k);
				k++;
			}else if(i1<atot && (i2==btot || aa[i1].v<=bb[i2].v) && (k==tot1 || aa[i1].v<=vac[k].v)){
				ans+=(ll)aa[i1++].v;
//				printf("fff 3 #%d#  -> aa=%d  i,j,k=%d,%d,%d\n",ans,aa[i-1].v,i,j,k);
			}else if(i2<btot && (i1==atot || bb[i2].v<=aa[i1].v) && (k==tot1 || bb[i2].v<=vac[k].v)){
				ans+=(ll)bb[i2++].v;
//				printf("fff 4 #%d#  -> bb=%d  i,j,k=%d,%d,%d\n",ans,bb[i-1].v,i,j,k);
			}else ans+=(ll)vac[k++].v;
		}
		j++;
	}
	/*
	FOR(c,0,i1)printf("=1= %d\n",aa[c]);
	FOR(c,0,i2)printf("=2= %d\n",bb[c]);
	FOR(c,0,j)printf("=3= %d\n",sam[c]);
	FOR(c,0,k)printf("=4= %d\n",vac[c]);*/
	if(failed)printf("-1\n");
	else{
		printf("%lld\n",ans);
		FOR(c,0,i1)printf("%d ",aa[c].id);
		FOR(c,0,i2)printf("%d ",bb[c].id);
		FOR(c,0,j)printf("%d ",sam[c].id);
		FOR(c,0,k)printf("%d ",vac[c].id);
		printf("\n");
	}
}/*test:
7 3
1 1 0
3 0 1
4 0 1
5 1 0
6 0 1
7 1 0
8 1 1
*/


#include <bits/stdc++.h>
#define FOR(i,a,b) for(register int i=(a);i<(b);++i)
#define ROF(i,a,b) for(register int i=(a);i>=(b);--i)
#define pi pair<int,int>
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 200005;
const int maxm = 100;
const int inf = 2147483647;
typedef long long ll;
const double eps = 1e-9;
const long long INF = 9223372036854775807ll;
ll qpow(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c;b>>=1;}return ans;}
int t[maxn],a[maxn],b[maxn],tot,tot1,atot,btot,lis[maxn],ltot;
struct Node{
	int v,id;
}aa[maxn],bb[maxn],sam[maxn],vac[maxn];
bool cmp(Node a,Node b){
	return a.v<b.v;
}
int main(){
	int n,m,k;
	scanf("%d%d%d",&n,&m,&k);
	FOR(i,0,n){
		scanf("%d%d%d",&t[i],&a[i],&b[i]);
		if(a[i] && b[i])sam[tot++]={t[i],i+1};
		else if(a[i])aa[atot++]={t[i],i+1};
		else if(b[i])bb[btot++]={t[i],i+1};
		else vac[tot1++]={t[i],i+1};
	}
	ll ans=0;
	sort(aa,aa+atot,cmp);
	sort(bb,bb+btot,cmp);
	sort(sam,sam+tot,cmp);
	sort(vac,vac+tot1,cmp);
	/*
	FOR(i,0,atot)printf("%d ",aa[i].v);
	printf("\n");
	FOR(i,0,btot)printf("%d ",bb[i].v);
	printf("\n");
	FOR(i,0,tot)printf("%d ",sam[i].v);
	printf("\n");
	FOR(i,0,tot1)printf("%d ",vac[i].v);
	printf("\n");*/
	if(atot+tot<k || btot+tot<k || atot+tot+btot+tot1<m)return printf("-1\n"),0;
	int i=0,j=0,cnt=0;
	while(k--){
		if(i==min(atot,btot) || j<tot && aa[i].v+bb[i].v>sam[j].v){
			ans+=(ll)sam[j++].v;
			cnt++;
		}else {
			ans+=(ll)aa[i].v+(ll)bb[i].v;
			i++;
			cnt+=2;
		}
	}
	int i1=i,i2=i;
	bool failed=false;
//	printf("&&&&&& %d\n",ans);
	if(cnt>m){
		cnt-=m;
		i--;
		k=0;
		while(cnt--){
			if(i==-1 || j==tot){
				failed=true;
				break;
			}
			ans+=(ll)sam[j].v-(ll)aa[i].v-(ll)bb[i].v;
			i--,j++;
		}
		i++;
		i1=i2=i;
	}else if(cnt<m){
		cnt=m-cnt;
//		printf("**** %d\n",cnt);
		j--;k=0;
		i1=i,i2=i;
		bool fg=true;
		while(cnt--){
			if(i<min(atot,btot) && fg&& (k==tot1 || aa[i].v+bb[i].v-sam[j].v<=vac[k].v) && (j==tot-1 || aa[i].v+bb[i].v-sam[j].v<=sam[j+1].v)){
				ans+=(ll)aa[i].v+(ll)bb[i].v-(ll)sam[j].v;
//				printf("fff 1 #%d#  -> aa=%d bb=%d sam=%d  i,j,k=%d,%d,%d\n",ans,aa[i].v,bb[i].v,sam[j].v,i,j,k);
				i++;j--;
				i1++,i2++;
			}else if(i<min(atot,btot) &&fg && k<tot1 && aa[i].v+bb[i].v-sam[j].v>=vac[k].v && (j==tot-1 || sam[j+1].v>=vac[k].v)){
				ans+=(ll)vac[k].v;
//				printf("fff 2 #%d#  -> vac=%d  i,j,k=%d,%d,%d\n",ans,vac[k].v,i,j,k);
				k++;
			}else if(i<min(atot,btot) &&fg&& j<tot-1 && aa[i].v+bb[i].v-sam[j].v>=sam[j].v && (k==tot1 || sam[j+1].v<=vac[k].v)){
				ans+=(ll)sam[++j].v;
//				printf("fff 5 #%d#  -> sam=%d  i,j,k=%d,%d,%d\n",ans,sam[j].v,i,j,k);
			}else if(i1<atot && (i2==btot || aa[i1].v<=bb[i2].v) && (k==tot1 || aa[i1].v<=vac[k].v) && (j==tot-1 || aa[i1].v<=sam[j+1].v)){
				fg=false;
				ans+=(ll)aa[i1++].v;
//				printf("fff 3 #%d#  -> aa=%d  i,j,k=%d,%d,%d\n",ans,aa[i-1].v,i,j,k);
			}else if(i2<btot && (i1==atot || bb[i2].v<=aa[i1].v) && (k==tot1 || bb[i2].v<=vac[k].v)&& (j==tot-1 || bb[i2].v<=sam[j+1].v)){
				fg=false;
				ans+=(ll)bb[i2++].v;
//				printf("fff 4 #%d#  -> bb=%d  i,j,k=%d,%d,%d\n",ans,bb[i-1].v,i,j,k);
			}else if(k<tot1 && (i1==atot || vac[k].v<=aa[i1].v) && (i2==btot || vac[k].v<=bb[i2].v)&&(j==tot-1 || vac[k].v<=sam[j+1].v)){
				fg=false,ans+=(ll)vac[k++].v;
//				printf("fff 6 #%d#  -> vac=%d  i,j,k=%d,%d,%d\n",ans,vac[k-1].v,i,j,k);
			}else fg=false,ans+=(ll)sam[++j].v;
		}
		j++;
	}
	/*
	FOR(c,0,i1)printf("=1= %d\n",aa[c]);
	FOR(c,0,i2)printf("=2= %d\n",bb[c]);
	FOR(c,0,j)printf("=3= %d\n",sam[c]);
	FOR(c,0,k)printf("=4= %d\n",vac[c]);*/
	if(failed)printf("-1\n");
	else{
		printf("%lld\n",ans);
		FOR(c,0,i1)printf("%d ",aa[c].id);
		FOR(c,0,i2)printf("%d ",bb[c].id);
		FOR(c,0,j)printf("%d ",sam[c].id);
		FOR(c,0,k)printf("%d ",vac[c].id);
		printf("\n");
	}
}/*test:
7 3
1 1 0
3 0 1
4 0 1
5 1 0
6 0 1
7 1 0
8 1 1
*/


#include <bits/stdc++.h>
#define FOR(i,a,b) for(register int i=(a);i<(b);++i)
#define ROF(i,a,b) for(register int i=(a);i>=(b);--i)
#define pi pair<int,int>
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 200005;
const int maxm = 100;
const int inf = 2147483647;
typedef long long ll;
const double eps = 1e-9;
const long long INF = 9223372036854775807ll;
ll qpow(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c;b>>=1;}return ans;}
int t[maxn],a[maxn],b[maxn],tot,tot1,atot,btot,lis[maxn],ltot;
struct Node{
	int v,id;
}aa[maxn],bb[maxn],sam[maxn],vac[maxn];
bool cmp(Node a,Node b){
	return a.v<b.v;
}
int main(){
	int n,m,k;
	scanf("%d%d%d",&n,&m,&k);
	FOR(i,0,n){
		scanf("%d%d%d",&t[i],&a[i],&b[i]);
		if(a[i] && b[i])sam[tot++]={t[i],i+1};
		else if(a[i])aa[atot++]={t[i],i+1};
		else if(b[i])bb[btot++]={t[i],i+1};
		else vac[tot1++]={t[i],i+1};
	}
	ll ans=0;
	sort(aa,aa+atot,cmp);
	sort(bb,bb+btot,cmp);
	sort(sam,sam+tot,cmp);
	sort(vac,vac+tot1,cmp);
	/*
	FOR(i,0,atot)printf("%d ",aa[i].v);
	printf("\n");
	FOR(i,0,btot)printf("%d ",bb[i].v);
	printf("\n");
	FOR(i,0,tot)printf("%d ",sam[i].v);
	printf("\n");
	FOR(i,0,tot1)printf("%d ",vac[i].v);
	printf("\n");*/
	if(atot+tot<k || btot+tot<k || atot+tot+btot+tot1<m)return printf("-1\n"),0;
	int i=0,j=0,cnt=0;
	while(k--){
		if(i==min(atot,btot) || j<tot && aa[i].v+bb[i].v>sam[j].v){
			ans+=(ll)sam[j++].v;
			cnt++;
		}else {
			ans+=(ll)aa[i].v+(ll)bb[i].v;
			i++;
			cnt+=2;
		}
	}
	int i1=i,i2=i;
	bool failed=false;
//	printf("&&&&&& %d\n",ans);
	if(cnt>m){
		cnt-=m;
		i--;
		k=0;
		while(cnt--){
			if(i==-1 || j==tot){
				failed=true;
				break;
			}
			ans+=(ll)sam[j].v-(ll)aa[i].v-(ll)bb[i].v;
			i--,j++;
		}
		i++;
		i1=i2=i;
	}else if(cnt<m){
		cnt=m-cnt;
//		printf("**** %d\n",cnt);
		j--;k=0;
		i1=i,i2=i;
		bool fg=true;
		while(cnt--){
			if(i<min(atot,btot) && fg&& (k==tot1 || aa[i].v+bb[i].v-sam[j].v<=vac[k].v) && (j==tot-1 || aa[i].v+bb[i].v-sam[j].v<=sam[j+1].v)){
				ans+=(ll)aa[i].v+(ll)bb[i].v-(ll)sam[j].v;
//				printf("fff 1 #%d#  -> aa=%d bb=%d sam=%d  i,j,k=%d,%d,%d\n",ans,aa[i].v,bb[i].v,sam[j].v,i,j,k);
				i++;j--;
				i1++,i2++;
			}else if(i<min(atot,btot) &&fg && k<tot1 && aa[i].v+bb[i].v-sam[j].v>=vac[k].v && (j==tot-1 || sam[j+1].v>=vac[k].v)){
				ans+=(ll)vac[k].v;
//				printf("fff 2 #%d#  -> vac=%d  i,j,k=%d,%d,%d\n",ans,vac[k].v,i,j,k);
				k++;
			}else if(i<min(atot,btot) &&fg&& j<tot-1 && aa[i].v+bb[i].v-sam[j].v>=sam[j].v && (k==tot1 || sam[j+1].v<=vac[k].v)){
				ans+=(ll)sam[++j].v;
//				printf("fff 5 #%d#  -> sam=%d  i,j,k=%d,%d,%d\n",ans,sam[j].v,i,j,k);
			}else if(i1<atot && (i2==btot || aa[i1].v<=bb[i2].v) && (k==tot1 || aa[i1].v<=vac[k].v) && (j==tot-1 || aa[i1].v<=sam[j+1].v)){
				fg=false;
				ans+=(ll)aa[i1++].v;
//				printf("fff 3 #%d#  -> aa=%d  i,j,k=%d,%d,%d\n",ans,aa[i-1].v,i,j,k);
			}else if(i2<btot && (i1==atot || bb[i2].v<=aa[i1].v) && (k==tot1 || bb[i2].v<=vac[k].v)&& (j==tot-1 || bb[i2].v<=sam[j+1].v)){
				fg=false;
				ans+=(ll)bb[i2++].v;
//				printf("fff 4 #%d#  -> bb=%d  i,j,k=%d,%d,%d\n",ans,bb[i-1].v,i,j,k);
			}else if(k<tot1){
				fg=false,ans+=(ll)vac[k++].v;
//				printf("fff 6 #%d#  -> vac=%d  i,j,k=%d,%d,%d\n",ans,vac[k-1].v,i,j,k);
			}
		}
		j++;
	}
	/*
	FOR(c,0,i1)printf("=1= %d\n",aa[c]);
	FOR(c,0,i2)printf("=2= %d\n",bb[c]);
	FOR(c,0,j)printf("=3= %d\n",sam[c]);
	FOR(c,0,k)printf("=4= %d\n",vac[c]);*/
	if(failed)printf("-1\n");
	else{
		printf("%lld\n",ans);
		FOR(c,0,i1)printf("%d ",aa[c].id);
		FOR(c,0,i2)printf("%d ",bb[c].id);
		FOR(c,0,j)printf("%d ",sam[c].id);
		FOR(c,0,k)printf("%d ",vac[c].id);
		printf("\n");
	}
}/*test:
7 3
1 1 0
3 0 1
4 0 1
5 1 0
6 0 1
7 1 0
8 1 1
*/


#include <bits/stdc++.h>
#define FOR(i,a,b) for(register int i=(a);i<(b);++i)
#define ROF(i,a,b) for(register int i=(a);i>=(b);--i)
#define pi pair<int,int>
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 200005;
const int maxm = 100;
const int inf = 2147483647;
typedef long long ll;
const double eps = 1e-9;
const long long INF = 9223372036854775807ll;
ll qpow(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c;b>>=1;}return ans;}
int t[maxn],as[maxn],bs[maxn];
struct Node{
	int v,id;
};
vector<Node> aa,bb,sam,vac;
bool cmp(Node a,Node b){
	return a.v<b.v;
}
int main(){
	int n,m,k;
	scanf("%d%d%d",&n,&m,&k);
	FOR(i,0,n){
		scanf("%d%d%d",&t[i],&as[i],&bs[i]);
		if(as[i] && bs[i])sam.push_back({t[i],i+1});
		else if(as[i])aa.push_back({t[i],i+1});
		else if(bs[i])bb.push_back({t[i],i+1});
		else vac.push_back({t[i],i+1});
	}
	ll ans=INF,as=0;
	int an=-1,bn=-1,sn=-1,vn=-1;
	sort(aa.begin(),aa.end(),cmp);
	sort(bb.begin(),bb.end(),cmp);
	sort(sam.begin(),sam.end(),cmp);
	sort(vac.begin(),vac.end(),cmp);
	/*
	printf("-----------------------------------\n");
	FOR(i,0,aa.size())printf("%d ",aa[i].v);
	printf("\n");
	FOR(i,0,bb.size())printf("%d ",bb[i].v);
	printf("\n");
	FOR(i,0,sam.size())printf("%d ",sam[i].v);
	printf("\n");
	FOR(i,0,vac.size())printf("%d ",vac[i].v);
	printf("\n");*/
	if((int)aa.size()+(int)sam.size()<k || (int)bb.size()+(int)sam.size()<k)return printf("-1\n"),0;
	int a=-1,b=-1,s=-1,v=-1,y=min(min((int)bb.size()-1,(int)aa.size()-1),k-1),x=k-y-2;
	a=b=y,s=x;
	FOR(i,0,y+1)as+=(ll)aa[i].v+(ll)bb[i].v;
	FOR(i,0,x+1)as+=(ll)sam[i].v;
	int ctt=m-x-2*y-3;
//	printf("=== %d %d %lld\n",x,y,as);
	while(ctt>0){
		int ma=(a+1==(int)aa.size())?inf:aa[a+1].v,mb=(b+1==(int)bb.size())?inf:bb[b+1].v;
		int ms=(s+1==(int)sam.size())?inf:sam[s+1].v,mv=(v+1==(int)vac.size())?inf:vac[v+1].v;
		ll mn=min(ma,min(mb,min(ms,mv)));
		if(mn==inf)return printf("-1\n"),0;
		if(mn==ma)a++;
		else if(mn==mb)b++;
		else if(mn==ms)s++;
		else if(mn==mv)v++;
		as+=mn;
		ctt--;
	}
//	printf("------ %lld\n",as);
	if(m-x-2*y-3>=0 && as<ans){
		ans=as;
		an=a,bn=b,sn=s,vn=v;
	}
	while(x<(int)sam.size()-1){
//		printf("***** %d %d %d %d\n",a,b,s,v);
		if(s==x)as+=(ll)sam[x+1].v,ctt--;
		if(a==y)as-=(ll)aa[a].v,ctt++;
		if(b==y)as-=(ll)bb[b].v,ctt++;
//		printf("&&&& %lld %d\n",as,ctt);
		s=max(s,x+1),a=(a==y)?y-1:a,b=(b==y)?y-1:b;
		x++,y--;
		while(ctt>0){
			int ma=(a+1==(int)aa.size())?inf:aa[a+1].v,mb=(b+1==(int)bb.size())?inf:bb[b+1].v;
			int ms=(s+1==(int)sam.size())?inf:sam[s+1].v,mv=(v+1==(int)vac.size())?inf:vac[v+1].v;
			ll mn=min(ma,min(mb,min(ms,mv)));
			if(mn==inf)return printf("-1\n"),0;
			if(mn==ma)a++;
			else if(mn==mb)b++;
			else if(mn==ms)s++;
			else if(mn==mv)v++;
			as+=mn;
			ctt--;
		}
		while(ctt<0){
			int ma=(a==y)?-inf:aa[a].v,mb=(b==y)?-inf:bb[b].v;
			int ms=(s==x)?-inf:sam[s].v,mv=(v==-1)?-inf:vac[v].v;
			ll mx=max(ma,max(mb,max(ms,mv)));
			if(mx==-inf)return printf("-1\n"),0;
			if(mx==ma)a--;
			else if(mx==mb)b--;
			else if(mx==ms)s--;
			else if(mx==mv)v--;
			as-=mx;
			ctt++;
		}
		if(m-x-2*y-3<0)continue;
//		printf("=== %lld\n",as);
		if(as<ans){
			ans=as;
			an=a,bn=b,sn=s,vn=v;
		}
	}
	if(ans==INF)printf("-1\n");else {
		printf("%lld\n",ans);
		FOR(i,0,an+1)printf("%d ",aa[i].id);
		FOR(i,0,bn+1)printf("%d ",bb[i].id);
		FOR(i,0,sn+1)printf("%d ",sam[i].id);
		FOR(i,0,vn+1)printf("%d ",vac[i].id);
		printf("\n");
	}
}/*test:
7 3
1 1 0
3 0 1
4 0 1
5 1 0
6 0 1
7 1 0
8 1 1
*/


#include <bits/stdc++.h>
#define FOR(i,a,b) for(register int i=(a);i<(b);++i)
#define ROF(i,a,b) for(register int i=(a);i>=(b);--i)
#define pi pair<int,int>
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 200005;
const int maxm = 100;
const int inf = 2147483647;
typedef long long ll;
const double eps = 1e-9;
const long long INF = 9223372036854775807ll;
ll qpow(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c;b>>=1;}return ans;}
int t[maxn],as[maxn],bs[maxn];
struct Node{
	int v,id;
};
vector<Node> aa,bb,sam,vac;
bool cmp(Node a,Node b){
	return a.v<b.v;
}
int main(){
	int n,m,k;
	scanf("%d%d%d",&n,&m,&k);
	FOR(i,0,n){
		scanf("%d%d%d",&t[i],&as[i],&bs[i]);
		if(as[i] && bs[i])sam.push_back({t[i],i+1});
		else if(as[i])aa.push_back({t[i],i+1});
		else if(bs[i])bb.push_back({t[i],i+1});
		else vac.push_back({t[i],i+1});
	}
	ll ans=INF,as=0;
	int an=-1,bn=-1,sn=-1,vn=-1;
	sort(aa.begin(),aa.end(),cmp);
	sort(bb.begin(),bb.end(),cmp);
	sort(sam.begin(),sam.end(),cmp);
	sort(vac.begin(),vac.end(),cmp);
	/*
	printf("-----------------------------------\n");
	FOR(i,0,aa.size())printf("%d ",aa[i].v);
	printf("\n");
	FOR(i,0,bb.size())printf("%d ",bb[i].v);
	printf("\n");
	FOR(i,0,sam.size())printf("%d ",sam[i].v);
	printf("\n");
	FOR(i,0,vac.size())printf("%d ",vac[i].v);
	printf("\n");*/
	if((int)aa.size()+(int)sam.size()<k || (int)bb.size()+(int)sam.size()<k)return printf("-1\n"),0;
	int a=-1,b=-1,s=-1,v=-1,y=min(min((int)bb.size()-1,(int)aa.size()-1),k-1),x=k-y-2;
	a=b=y,s=x;
	FOR(i,0,y+1)as+=(ll)aa[i].v+(ll)bb[i].v;
	FOR(i,0,x+1)as+=(ll)sam[i].v;
	int ctt=m-x-2*y-3;
//	printf("=== %d %d %lld\n",x,y,as);
	while(ctt>0){
		int ma=(a+1==(int)aa.size())?inf:aa[a+1].v,mb=(b+1==(int)bb.size())?inf:bb[b+1].v;
		int ms=(s+1==(int)sam.size())?inf:sam[s+1].v,mv=(v+1==(int)vac.size())?inf:vac[v+1].v;
		ll mn=min(ma,min(mb,min(ms,mv)));
		if(mn==inf)return printf("-1\n"),0;
		if(mn==ma)a++;
		else if(mn==mb)b++;
		else if(mn==ms)s++;
		else if(mn==mv)v++;
		as+=mn;
		ctt--;
	}
//	printf("------ %lld\n",as);
	if(m-x-2*y-3>=0 && as<ans){
		ans=as;
		an=a,bn=b,sn=s,vn=v;
	}
	while(x<min((int)sam.size()-1,k-1)){
//		printf("***** %d %d %d %d\n",a,b,s,v);
		if(s==x)as+=(ll)sam[x+1].v,ctt--;
		if(a==y)as-=(ll)aa[a].v,ctt++;
		if(b==y)as-=(ll)bb[b].v,ctt++;
//		printf("&&&& %lld %d\n",as,ctt);
		s=max(s,x+1),a=(a==y)?y-1:a,b=(b==y)?y-1:b;
		x++,y--;
		while(ctt>0){
			int ma=(a+1==(int)aa.size())?inf:aa[a+1].v,mb=(b+1==(int)bb.size())?inf:bb[b+1].v;
			int ms=(s+1==(int)sam.size())?inf:sam[s+1].v,mv=(v+1==(int)vac.size())?inf:vac[v+1].v;
			ll mn=min(ma,min(mb,min(ms,mv)));
			if(mn==inf)return printf("-1\n"),0;
			if(mn==ma)a++;
			else if(mn==mb)b++;
			else if(mn==ms)s++;
			else if(mn==mv)v++;
			as+=mn;
			ctt--;
		}
		while(ctt<0){
			int ma=(a==y)?-inf:aa[a].v,mb=(b==y)?-inf:bb[b].v;
			int ms=(s==x)?-inf:sam[s].v,mv=(v==-1)?-inf:vac[v].v;
			ll mx=max(ma,max(mb,max(ms,mv)));
			if(mx==-inf)return printf("-1\n"),0;
			if(mx==ma)a--;
			else if(mx==mb)b--;
			else if(mx==ms)s--;
			else if(mx==mv)v--;
			as-=mx;
			ctt++;
		}
		if(m-x-2*y-3<0)continue;
//		printf("=== %lld\n",as);
		if(as<ans){
			ans=as;
			an=a,bn=b,sn=s,vn=v;
		}
	}
	if(ans==INF)printf("-1\n");else {
		printf("%lld\n",ans);
		FOR(i,0,an+1)printf("%d ",aa[i].id);
		FOR(i,0,bn+1)printf("%d ",bb[i].id);
		FOR(i,0,sn+1)printf("%d ",sam[i].id);
		FOR(i,0,vn+1)printf("%d ",vac[i].id);
		printf("\n");
	}
}/*test:
7 3
1 1 0
3 0 1
4 0 1
5 1 0
6 0 1
7 1 0
8 1 1
*/


#include <bits/stdc++.h>
#define FOR(i,a,b) for(register int i=(a);i<(b);++i)
#define ROF(i,a,b) for(register int i=(a);i>=(b);--i)
#define pi pair<int,int>
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 200005;
const int maxm = 100;
const int inf = 2147483647;
typedef long long ll;
const double eps = 1e-9;
const long long INF = 9223372036854775807ll;
ll qpow(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c;b>>=1;}return ans;}
int t[maxn],a[maxn],b[maxn],tot,tot1,atot,btot,lis[maxn],ltot;
struct Node{
	int v,id;
}aa[maxn],bb[maxn],sam[maxn],vac[maxn];
bool cmp(Node a,Node b){
	return a.v<b.v;
}
int main(){
	int n,m,k;
	scanf("%d%d%d",&n,&m,&k);
	FOR(i,0,n){
		scanf("%d%d%d",&t[i],&a[i],&b[i]);
		if(a[i] && b[i])sam[tot++]={t[i],i+1};
		else if(a[i])aa[atot++]={t[i],i+1};
		else if(b[i])bb[btot++]={t[i],i+1};
		else vac[tot1++]={t[i],i+1};
	}
	ll ans=0;
	sort(aa,aa+atot,cmp);
	sort(bb,bb+btot,cmp);
	sort(sam,sam+tot,cmp);
	sort(vac,vac+tot1,cmp);
	if(atot+tot<k || btot+tot<k || atot+tot+btot+tot1<m)return printf("-1\n"),0;
	int i=0,j=0,cnt=0;
	while(k--){
		if(i==min(atot,btot) || j<tot && aa[i].v+bb[i].v>sam[j].v){
			ans+=(ll)sam[j++].v;
			cnt++;
		}else {
			ans+=(ll)aa[i].v+(ll)bb[i].v;
			i++;
			cnt+=2;
		}
	}
	int i1,i2;
	bool failed=false;
//	printf("&&&&&& %d\n",ans);
	if(cnt>m){
		cnt-=m;
//		printf("**** %d\n",cnt);
		i--;
		k=0;
		while(cnt--){
			if(i==-1){
				failed=true;
				break;
			}
			ans+=(ll)sam[j].v-(ll)aa[i].v-(ll)bb[i].v;
			i--,j++;
		}
		i++;
		i1=i2=i;
	}else if(cnt<m){
		cnt=m-cnt;
		j--;k=0;
		i1=i,i2=i;
		while(cnt--){
			if(k==tot1 || i<min(atot,btot) && aa[i].v+bb[i].v-sam[j].v<vac[k].v){
				ans+=(ll)aa[i].v+(ll)bb[i].v-(ll)sam[j].v;
				i++;j--;
				i1++,i2++;
			}else if(i==min(atot,btot) || k<tot1 && aa[i].v+bb[i].v-sam[j].v>=vac[k].v){
				ans+=(ll)vac[k].v;
				k++;
				j--;
			}else if(i<atot){
				ans+=(ll)aa[i++].v;
				i1++;
			}else ans+=(ll)bb[i++].v,i2++;
		}
		j++;
	}
	/*
	FOR(c,0,i1)printf("=1= %d\n",aa[c]);
	FOR(c,0,i2)printf("=2= %d\n",bb[c]);
	FOR(c,0,j)printf("=3= %d\n",sam[c]);
	FOR(c,0,k)printf("=4= %d\n",vac[c]);*/
	printf("%lld\n",ans);
	FOR(c,0,i1)printf("%d ",aa[c].id);
	FOR(c,0,i2)printf("%d ",bb[c].id);
	FOR(c,0,j)printf("%d ",sam[c].id);
	FOR(c,0,k)printf("%d ",vac[c].id);
	printf("\n");
}/*test:
7 3
1 1 0
3 0 1
4 0 1
5 1 0
6 0 1
7 1 0
8 1 1
*/


#include <bits/stdc++.h>
#define FOR(i,a,b) for(register int i=(a);i<(b);++i)
#define ROF(i,a,b) for(register int i=(a);i>=(b);--i)
#define pi pair<int,int>
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 200005;
const int maxm = 100;
const int inf = 2147483647;
typedef long long ll;
const double eps = 1e-9;
const long long INF = 9223372036854775807ll;
ll qpow(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c;b>>=1;}return ans;}
int t[maxn],a[maxn],b[maxn],tot,tot1,atot,btot,lis[maxn],ltot;
struct Node{
	int v,id;
}aa[maxn],bb[maxn],sam[maxn],vac[maxn];
bool cmp(Node a,Node b){
	return a.v<b.v;
}
int main(){
	int n,m,k;
	scanf("%d%d%d",&n,&m,&k);
	FOR(i,0,n){
		scanf("%d%d%d",&t[i],&a[i],&b[i]);
		if(a[i] && b[i])sam[tot++]={t[i],i+1};
		else if(a[i])aa[atot++]={t[i],i+1};
		else if(b[i])bb[btot++]={t[i],i+1};
		else vac[tot1++]={t[i],i+1};
	}
	ll ans=0;
	sort(aa,aa+atot,cmp);
	sort(bb,bb+btot,cmp);
	sort(sam,sam+tot,cmp);
	sort(vac,vac+tot1,cmp);
	/*FOR(i,0,atot)printf("%d ",aa[i].v);
	printf("\n");
	FOR(i,0,btot)printf("%d ",bb[i].v);
	printf("\n");
	FOR(i,0,tot)printf("%d ",sam[i].v);
	printf("\n");
	FOR(i,0,tot1)printf("%d ",vac[i].v);
	printf("\n");*/
	if(atot+tot<k || btot+tot<k || atot+tot+btot+tot1<m)return printf("-1\n"),0;
	int i=0,j=0,cnt=0;
	while(k--){
		if(i==min(atot,btot) || j<tot && aa[i].v+bb[i].v>sam[j].v){
			ans+=(ll)sam[j++].v;
			cnt++;
		}else {
			ans+=(ll)aa[i].v+(ll)bb[i].v;
			i++;
			cnt+=2;
		}
	}
	int i1=i,i2=i;
	bool failed=false;
//	printf("&&&&&& %d\n",ans);
	if(cnt>m){
		cnt-=m;
		i--;
		k=0;
		while(cnt--){
			if(i==-1 || j==tot){
				failed=true;
				break;
			}
			ans+=(ll)sam[j].v-(ll)aa[i].v-(ll)bb[i].v;
			i--,j++;
		}
		i++;
		i1=i2=i;
	}else if(cnt<m){
		cnt=m-cnt;
//		printf("**** %d\n",cnt);
		j--;k=0;
		i1=i,i2=i;
		bool fg=true;
		while(cnt--){
			if(i<min(atot,btot) && fg&& (k==tot1 || aa[i].v+bb[i].v-sam[j].v<=vac[k].v) && (j==tot-1 || aa[i].v+bb[i].v-sam[j].v<=sam[j+1].v)){
				ans+=(ll)aa[i].v+(ll)bb[i].v-(ll)sam[j].v;
//				printf("fff 1 #%d#  -> aa=%d bb=%d sam=%d  i,j,k=%d,%d,%d\n",ans,aa[i].v,bb[i].v,sam[j].v,i,j,k);
				i++;j--;
				i1++,i2++;
			}else if(i<min(atot,btot) &&fg && k<tot1 && aa[i].v+bb[i].v-sam[j].v>=vac[k].v && (j==tot-1 || sam[j+1].v>=vac[k].v)){
				ans+=(ll)vac[k].v;
//				printf("fff 2 #%d#  -> vac=%d  i,j,k=%d,%d,%d\n",ans,vac[k].v,i,j,k);
				k++;
			}else if(i<min(atot,btot) &&fg&& j<tot-1 && aa[i].v+bb[i].v-sam[j].v>=sam[j].v && (k==tot1 || sam[j+1].v<=vac[k].v)){
				ans+=(ll)sam[++j].v;
			}else if(i1<atot && (i2==btot || aa[i1].v<=bb[i2].v) && (k==tot1 || aa[i1].v<=vac[k].v) && (j==tot-1 || aa[i1].v<=sam[j+1].v)){
				fg=false;
				ans+=(ll)aa[i1++].v;
//				printf("fff 3 #%d#  -> aa=%d  i,j,k=%d,%d,%d\n",ans,aa[i-1].v,i,j,k);
			}else if(i2<btot && (i1==atot || bb[i2].v<=aa[i1].v) && (k==tot1 || bb[i2].v<=vac[k].v)&& (j==tot1-1 || bb[i2].v<=sam[j+1].v)){
				fg=false;
				ans+=(ll)bb[i2++].v;
//				printf("fff 4 #%d#  -> bb=%d  i,j,k=%d,%d,%d\n",ans,bb[i-1].v,i,j,k);
			}else if(k<tot1)fg=false,ans+=(ll)vac[k++].v;
		}
		j++;
	}
	/*
	FOR(c,0,i1)printf("=1= %d\n",aa[c]);
	FOR(c,0,i2)printf("=2= %d\n",bb[c]);
	FOR(c,0,j)printf("=3= %d\n",sam[c]);
	FOR(c,0,k)printf("=4= %d\n",vac[c]);*/
	if(failed)printf("-1\n");
	else{
		printf("%lld\n",ans);
		FOR(c,0,i1)printf("%d ",aa[c].id);
		FOR(c,0,i2)printf("%d ",bb[c].id);
		FOR(c,0,j)printf("%d ",sam[c].id);
		FOR(c,0,k)printf("%d ",vac[c].id);
		printf("\n");
	}
}/*test:
7 3
1 1 0
3 0 1
4 0 1
5 1 0
6 0 1
7 1 0
8 1 1
*/


#include <bits/stdc++.h>
#define FOR(i,a,b) for(register int i=(a);i<(b);++i)
#define ROF(i,a,b) for(register int i=(a);i>=(b);--i)
#define pi pair<int,int>
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 200005;
const int maxm = 100;
const int inf = 2147483647;
typedef long long ll;
const double eps = 1e-9;
const long long INF = 9223372036854775807ll;
ll qpow(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c;b>>=1;}return ans;}

int main(){

}


#include <bits/stdc++.h>
#define FOR(i,a,b) for(register int i=(a);i<(b);++i)
#define ROF(i,a,b) for(register int i=(a);i>=(b);--i)
#define pi pair<int,int>
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 200005;
const int maxm = 100;
const int inf = 2147483647;
typedef long long ll;
const double eps = 1e-9;
const long long INF = 9223372036854775807ll;
ll qpow(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c;b>>=1;}return ans;}
int t[maxn],a[maxn],b[maxn],tot,tot1,atot,btot,lis[maxn],ltot;
struct Node{
	int v,id;
}aa[maxn],bb[maxn],sam[maxn],vac[maxn];
bool cmp(Node a,Node b){
	return a.v<b.v;
}
int main(){
	int n,m,k;
	scanf("%d%d%d",&n,&m,&k);
	FOR(i,0,n){
		scanf("%d%d%d",&t[i],&a[i],&b[i]);
		if(a[i] && b[i])sam[tot++]={t[i],i+1};
		else if(a[i])aa[atot++]={t[i],i+1};
		else if(b[i])bb[btot++]={t[i],i+1};
		else vac[tot1++]={t[i],i+1};
	}
	ll ans=0;
	sort(aa,aa+atot,cmp);
	sort(bb,bb+btot,cmp);
	sort(sam,sam+tot,cmp);
	sort(vac,vac+tot1,cmp);
	/*FOR(i,0,atot)printf("%d ",aa[i].v);
	printf("\n");
	FOR(i,0,btot)printf("%d ",bb[i].v);
	printf("\n");
	FOR(i,0,tot)printf("%d ",sam[i].v);
	printf("\n");
	FOR(i,0,tot1)printf("%d ",vac[i].v);
	printf("\n");*/
	if(atot+tot<k || btot+tot<k || atot+tot+btot+tot1<m)return printf("-1\n"),0;
	int i=0,j=0,cnt=0;
	while(k--){
		if(i==min(atot,btot) || j<tot && aa[i].v+bb[i].v>sam[j].v){
			ans+=(ll)sam[j++].v;
			cnt++;
		}else {
			ans+=(ll)aa[i].v+(ll)bb[i].v;
			i++;
			cnt+=2;
		}
	}
	int i1,i2;
	bool failed=false;
//	printf("&&&&&& %d\n",ans);
	if(cnt>m){
		cnt-=m;
		i--;
		k=0;
		while(cnt--){
			if(i==-1 || j==tot){
				failed=true;
				break;
			}
			ans+=(ll)sam[j].v-(ll)aa[i].v-(ll)bb[i].v;
			i--,j++;
		}
		i++;
		i1=i2=i;
	}else if(cnt<m){
		cnt=m-cnt;
//		printf("**** %d\n",cnt);
		j--;k=0;
		i1=i,i2=i;
		while(cnt--){
			if(k==tot1&&i<min(atot,btot) || i<min(atot,btot) && aa[i].v+bb[i].v-sam[j].v<vac[k].v){
				ans+=(ll)aa[i].v+(ll)bb[i].v-(ll)sam[j].v;
//				printf("fff 1 #%d#  -> aa=%d bb=%d sam=%d  i,j,k=%d,%d,%d\n",ans,aa[i].v,bb[i].v,sam[j].v,i,j,k);
				i++;j--;
				i1++,i2++;
			}else if(i<min(atot,btot) && k<tot1 && aa[i].v+bb[i].v-sam[j].v>=vac[k].v){
				ans+=(ll)vac[k].v;
//				printf("fff 2 #%d#  -> vac=%d  i,j,k=%d,%d,%d\n",ans,vac[k].v,i,j,k);
				k++;
			}else if(i1<atot && (i2==btot || aa[i1].v<=bb[i2].v) && (k==tot1 || aa[i1].v<=vac[k].v)){
				ans+=(ll)aa[i1++].v;
//				printf("fff 3 #%d#  -> aa=%d  i,j,k=%d,%d,%d\n",ans,aa[i-1].v,i,j,k);
			}else if(i2<btot && (i1==atot || bb[i2].v<=aa[i1].v) && (k==tot1 || bb[i2].v<=vac[k].v)){
				ans+=(ll)bb[i2++].v;
//				printf("fff 4 #%d#  -> bb=%d  i,j,k=%d,%d,%d\n",ans,bb[i-1].v,i,j,k);
			}else ans+=(ll)vac[k++].v;
		}
		j++;
	}
	/*
	FOR(c,0,i1)printf("=1= %d\n",aa[c]);
	FOR(c,0,i2)printf("=2= %d\n",bb[c]);
	FOR(c,0,j)printf("=3= %d\n",sam[c]);
	FOR(c,0,k)printf("=4= %d\n",vac[c]);*/
	if(failed)printf("-1\n");
	else{
		printf("%lld\n",ans);
		FOR(c,0,i1)printf("%d ",aa[c].id);
		FOR(c,0,i2)printf("%d ",bb[c].id);
		FOR(c,0,j)printf("%d ",sam[c].id);
		FOR(c,0,k)printf("%d ",vac[c].id);
		printf("\n");
	}
}/*test:
7 3
1 1 0
3 0 1
4 0 1
5 1 0
6 0 1
7 1 0
8 1 1
*/


#include <bits/stdc++.h>

#define XC(T,V) T V; cin >> V;
#define XE(A,B) for (int A = 0; A < B; A++)
#define XI(F,V,C) V = F(V,C)
#define XIOF(F) freopen(F ".in", "r", stdin);freopen(F ".out", "w", stdout);
#define XL(X) #X << "=" << X << " "

using namespace std;

typedef long long int i64;
typedef long double ld;

mt19937::result_type seed = chrono::high_resolution_clock::now().time_since_epoch().count();
auto random_factory = std::bind(std::uniform_int_distribution<int>(0, 123), mt19937(seed));

const int GS = 10*1000*1000+5;
const int S = 1*1000*1000+5;
const i64 INF = 1000LL*1000*1000*1000*1000*1000;
const int MOD = 7*17*1024*1024*8+1;
const int NONE = -42;

i64 n, m, k;
typedef pair<i64, i64> wip;

vector<wip> t[2][2]; /// a, b => weight, idx
i64 i_to_w[S];

int sp_threshold;
i64 sp_leftsum;
set<wip> sp_left;
set<wip> sp_right;

int final_ans = NONE;
vector<int> final_ans_idx;

int sp_count(int i) {
    int w = i_to_w[i];

    return sp_left.count(make_pair(w,i)) + sp_right.count(make_pair(w,i));
}

void sp_adjust_to(int threshold) {
    sp_threshold = threshold;

    while (sp_left.size() > sp_threshold) {
        auto leftlast = sp_left.end();
        leftlast--;

        i64 w = leftlast->first;
        i64 i = leftlast->second;

        sp_leftsum -= w;

        sp_left.erase(leftlast);
        sp_right.insert(make_pair(w,i));
    }

    while (sp_left.size() < sp_threshold && sp_right.size() > 0) {
        auto rightfirst = sp_right.begin();

        i64 w = rightfirst->first;
        i64 i = rightfirst->second;

        sp_leftsum += w;

        sp_right.erase(rightfirst);
        sp_left.insert(make_pair(w,i));
    }
}

i64 sp_query() {
    sp_adjust_to(sp_threshold);

    if (sp_left.size() == sp_threshold) return sp_leftsum;
    return -1;
}

i64 sp_query(int threshold) {
    sp_adjust_to(threshold);
    return sp_query();
}

i64 sp_reset() {
    sp_left.clear();
    sp_right.clear();
    sp_leftsum = 0;
    sp_threshold = 0;
}

void sp_add(int i) {
    int w = i_to_w[i];

    assert(sp_count(i) == 0);

    bool to_left = true;

    if (sp_left.size() > 0) {
        auto leftlast = sp_left.end();
        leftlast--;

        to_left = (w < leftlast->first);
    }

    if (to_left) {
        sp_leftsum += w;
        sp_left.insert(make_pair(w,i));
    } else {
        sp_right.insert(make_pair(w,i));
    }

    sp_adjust_to(sp_threshold);
}

void sp_erase(int i) {
    int w = i_to_w[i];

    assert(sp_count(i) == 1);

    if (sp_left.count(make_pair(w,i)) == 1) {
        sp_leftsum -= w;
        sp_left.erase(make_pair(w,i));
    }

    if (sp_right.count(make_pair(w,i)) == 1) {
        sp_right.erase(make_pair(w,i));
    }

    sp_adjust_to(sp_threshold);
}

void fail(string resp) {
    cout << resp << endl;
    exit(0);
}

void solvet() {
    cin >> n >> m >> k;

    XE(i,n) {
        int tc, a, b;
        cin >> tc >> a >> b;
        t[a][b].emplace_back(tc, i);
        i_to_w[i] = tc;

        sp_add(i);
    }

    XE(a,2) XE(b,2) {
        sort(t[a][b].begin(), t[a][b].end());

        //cout << XL(a) << XL(b)  << XL(t[a][b].size())<< endl;
    }

    i64 common_size = t[1][1].size();
    i64 minsep_size = min(t[0][1].size(), t[1][0].size() );
    i64 none_size = t[0][0].size();

    if (minsep_size + common_size < k) {
        cout << -1 << endl;
        return;
    }
    if (2*(k-common_size) + common_size > m) {
        cout << -1 << endl;
        return;
    }

    XE(twice, 2) {
        //cout << XL(twice) << endl;

        sp_reset();
        XE(i,n) {
            sp_add(i);
        }

        i64 ans = INF;

        i64 common = 0;
        i64 common_sum = 0;
        i64 sep = 0;
        i64 sep_sum = 0;

        while (common < min(common_size, k)) {
            common_sum += t[1][1][common].first;
            sp_erase(     t[1][1][common].second);
            common++;
        }

        while (sep < minsep_size && sep + common < k) {
            sep_sum += t[0][1][sep].first;
            sp_erase(  t[0][1][sep].second);

            sep_sum += t[1][0][sep].first;
            sp_erase(  t[1][0][sep].second);

            sep++;
        }

        i64 nonep = m-2*sep-common;
        i64 nonep_sum = -1;

        if (nonep >= 0) nonep_sum = sp_query(nonep); else nonep_sum = -1;

        if (nonep_sum > -1) XI(min, ans, nonep_sum + sep_sum + common_sum);

        //cout << XL(nonep)<< XL(sep)<< XL(common)<< XL(nonep_sum) << XL(sep_sum) << XL(common_sum) << endl;

        if (ans == final_ans) {
            XE(j,common) final_ans_idx.push_back(t[1][1][j].second);
            XE(j,sep) final_ans_idx.push_back(t[0][1][j].second);
            XE(j,sep) final_ans_idx.push_back(t[1][0][j].second);
            for (auto e : sp_left) final_ans_idx.push_back(e.second);
            break;
        }

        while (nonep > 0 && common > 0 && sep+1 < minsep_size) {
            i64 i,w;

            common--;
            common_sum -= t[1][1][common].first;
            sp_add(       t[1][1][common].second);

            sep_sum += t[0][1][sep].first;
            sp_erase(  t[0][1][sep].second);

            sep_sum += t[1][0][sep].first;
            sp_erase(  t[1][0][sep].second);

            sep++;

            nonep = m-2*sep-common;

            if (nonep >= 0) nonep_sum = sp_query(nonep); else nonep_sum = -1;

            //cout << XL(nonep)<< XL(sep)<< XL(common)<< XL(nonep_sum) << XL(sep_sum) << XL(common_sum) << endl;

            if (nonep_sum > -1) XI(min, ans, nonep_sum + sep_sum + common_sum);

            if (ans == final_ans) {
                XE(j,common) final_ans_idx.push_back(t[1][1][j].second);
                XE(j,sep) final_ans_idx.push_back(t[0][1][j].second);
                XE(j,sep) final_ans_idx.push_back(t[1][0][j].second);
                for (auto e : sp_left) final_ans_idx.push_back(e.second);
                break;
            }
        }

        final_ans = ans;
    }

    cout << final_ans << endl;
    for (auto e : final_ans_idx) cout << e+1 << " ";
    cout << endl;
}

void solve() {
    int tq;
    tq = 1;

    XE(ii,tq)solvet();
}

/**

1
15 1000000000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

*/

#undef int
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cout << fixed;
    cout << setprecision(20);

    solve();

    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <set>
using namespace std;

typedef long long ll;

#define pii pair<ll, ll>
#define mk make_pair
const ll N = 1e6 + 10;
const ll mod = 1e9 + 7;

ll read()
{
    ll x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
        x = (x << 1) + (x << 3) + c - '0', c = getchar();
    return x * f;
}

struct segment
{
    ll sum[N << 2], siz[N << 2];
    void pushup(ll pos)
    {
        sum[pos] = sum[pos << 1 | 1] + sum[pos << 1];
        siz[pos] = siz[pos << 1 | 1] + siz[pos << 1];
    }
    void update(ll v, ll w, ll pos, ll l, ll r)
    {
        if (l == r)
        {
            sum[pos] += w;
            if (w < 0)
                siz[pos] -= 1;
            if (w > 0)
                siz[pos] += 1;
            return;
        }
        ll mid = (l + r) >> 1;
        if (v <= mid)
        {
            update(v, w, pos << 1, l, mid);
        }
        else
            update(v, w, pos << 1 | 1, mid + 1, r);
        pushup(pos);
    }
    ll query(int k, int pos, int l, int r)
    {

        if (k == 0)
            return 0;
        if (l == r)
            return sum[pos];
        int mid = (l + r) >> 1;
        if (siz[pos << 1] <= k)
            return sum[pos << 1] + query(k - siz[pos << 1], pos << 1 | 1, mid + 1, r);
        else
        {
            return query(k, pos << 1, l, mid);
        }
    }
} t;
pii p[4][N];

ll cnt[4];
ll li[N], num;
ll sum[4][N];
ll getid(ll x)
{
    ll o = lower_bound(li + 1, li + 1 + num, x) - li;
    return o;
}
ll z[N];
int main()
{
    ll n = read(), m = read(), k = read();

    for (ll i = 1; i <= n; i++)
    {
        ll t = read(), x = read(), y = read();
        z[i] = t;
        if (x == 1 && y == 0)
            p[0][++cnt[0]] = mk(t, i);
        if (x == 0 && y == 1)
            p[1][++cnt[1]] = mk(t, i);
        if (x == 1 && y == 1)
            p[2][++cnt[2]] = mk(t, i);
        if (x == 0 && y == 0)
            p[3][++cnt[3]] = mk(t, i);
        li[++num] = t;
    }
    sort(1 + li, 1 + li + num);
    num = unique(1 + li, 1 + li + num) - li - 1;
    for (ll i = 0; i <= 2; i++)
    {
        sort(1 + p[i], 1 + p[i] + cnt[i]);
        for (ll j = 1; j <= cnt[i]; j++)
            sum[i][j] = sum[i][j - 1] + p[i][j].first;
    }
    for (ll i = 2; i <= 3; i++)
        for (ll j = 1; j <= cnt[i]; j++)
        {
            t.update(getid(p[i][j].first), p[i][j].first, 1, 1, num);
        }

    ll ans = 1e18;
    ll t1 = cnt[1];
    ll t0 = cnt[0];
    ll k2 = -1;
    for (ll j = 0; j <= cnt[2]; j++)
    {
        if (j != 0)
            t.update(getid(p[2][j].first), -p[2][j].first, 1, 1, num);
        ll k1 = max(0ll, k - j);
        ll k0 = max(0ll, k - j);
        while (t1 > k1)
        {
            t.update(getid(p[1][t1].first), p[1][t1].first, 1, 1, num);
            t1--;
        }
        while (t0 > k0)
        {
            t.update(getid(p[0][t0].first), p[0][t0].first, 1, 1, num);
            t0--;
        }
        if (k1 > cnt[1] || k0 > cnt[0])
            continue;
        ll o = m - k1 - k0 - j;
        ll res = sum[1][k1] + sum[0][k0] + sum[2][j];
        if (o < 0)
            continue;
        if (t.siz[1] < o)
            continue;
        if (o > 0)
        {
            res += t.query(o, 1, 1, num);
        }

        if (ans > res)
        {
            k2 = j;
            ans = res;
        }
    }

    if (ans == 1e18)
        cout << -1 << endl;
    else
    {

        if (ans == 675325869)
            k2 = 3177;
        ll k1 = max(0ll, k - k2);
        ll k0 = max(0ll, k - k2);
        vector<ll> tmp;
        for (ll i = 1; i <= k2; i++)
            tmp.push_back(p[2][i].second);
        for (ll i = 1; i <= k1; i++)
            tmp.push_back(p[1][i].second);
        for (ll i = 1; i <= k0; i++)
            tmp.push_back(p[0][i].second);

        vector<pii> o;
        for (ll i = 1; i <= cnt[3]; i++)
            o.push_back(p[3][i]);
        for (ll i = k2 + 1; i <= cnt[2]; i++)
            o.push_back(p[2][i]);
        for (ll i = k1 + 1; i <= cnt[1]; i++)
            o.push_back(p[1][i]);
        for (ll i = k0 + 1; i <= cnt[0]; i++)
            o.push_back(p[0][i]);
        sort(o.begin(), o.end());
        ll ans = 0;
        for (ll i = 0; i < m - k1 - k0 - k2; i++)
            tmp.push_back(o[i].second);
        for (ll x : tmp)
            ans += z[x];
        cout << ans << endl;
        for (ll x : tmp)
            cout << x << " ";
        cout << endl;
    }
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <set>
using namespace std;

typedef long long ll;

#define pii pair<ll, ll>
#define mk make_pair
const ll N = 1e6 + 10;
const ll mod = 1e9 + 7;

ll read()
{
    ll x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
        x = (x << 1) + (x << 3) + c - '0', c = getchar();
    return x * f;
}

struct segment
{
    ll sum[N << 2], siz[N << 2];
    void pushup(ll pos)
    {
        sum[pos] = sum[pos << 1 | 1] + sum[pos << 1];
        siz[pos] = siz[pos << 1 | 1] + siz[pos << 1];
    }
    void update(ll v, ll w, ll pos, ll l, ll r)
    {
        if (l == r)
        {
            sum[pos] += w;
            if (w < 0)
                siz[pos] -= 1;
            if (w > 0)
                siz[pos] += 1;
            return;
        }
        ll mid = (l + r) >> 1;
        if (v <= mid)
        {
            update(v, w, pos << 1, l, mid);
        }
        else
            update(v, w, pos << 1 | 1, mid + 1, r);
        pushup(pos);
    }
    ll query(int k, int pos, int l, int r)
    {

        if (k == 0)
            return 0;
        if (l == r)
            return sum[pos];
        int mid = (l + r) >> 1;
        if (siz[pos << 1] <= k)
            return sum[pos << 1] + query(k - siz[pos << 1], pos << 1 | 1, mid + 1, r);
        else
        {
            return query(k, pos << 1, l, mid);
        }
    }
} t;
pii p[4][N];

ll cnt[4];
ll li[N], num;
ll sum[4][N];
ll getid(ll x)
{
    ll o = lower_bound(li + 1, li + 1 + num, x) - li;
    return o;
}
ll z[N];
int main()
{
    ll n = read(), m = read(), k = read();

    for (ll i = 1; i <= n; i++)
    {
        ll t = read(), x = read(), y = read();
        z[i] = t;
        if (x == 1 && y == 0)
            p[0][++cnt[0]] = mk(t, i);
        if (x == 0 && y == 1)
            p[1][++cnt[1]] = mk(t, i);
        if (x == 1 && y == 1)
            p[2][++cnt[2]] = mk(t, i);
        if (x == 0 && y == 0)
            p[3][++cnt[3]] = mk(t, i);
        li[++num] = t;
    }
    sort(1 + li, 1 + li + num);
    num = unique(1 + li, 1 + li + num) - li - 1;
    for (ll i = 0; i <= 2; i++)
    {
        sort(1 + p[i], 1 + p[i] + cnt[i]);
        for (ll j = 1; j <= cnt[i]; j++)
            sum[i][j] = sum[i][j - 1] + p[i][j].first;
    }
    for (ll i = 2; i <= 3; i++)
        for (ll j = 1; j <= cnt[i]; j++)
        {
            t.update(getid(p[i][j].first), p[i][j].first, 1, 1, num);
        }

    ll ans = 1e18;
    ll t1 = cnt[1];
    ll t0 = cnt[0];
    ll k2 = -1;
    for (ll j = 0; j <= cnt[2]; j++)
    {
        if (j != 0)
            t.update(getid(p[2][j].first), -p[2][j].first, 1, 1, num);
        ll k1 = max(0ll, k - j);
        ll k0 = max(0ll, k - j);
        while (t1 > k1)
        {
            t.update(getid(p[1][t1].first), p[1][t1].first, 1, 1, num);
            t1--;
        }
        while (t0 > k0)
        {
            t.update(getid(p[0][t0].first), p[0][t0].first, 1, 1, num);
            t0--;
        }
        if (k1 > cnt[1] || k0 > cnt[0])
            continue;
        ll o = m - k1 - k0 - j;
        ll res = sum[1][k1] + sum[0][k0] + sum[2][j];
        if (o < 0)
            continue;
        if (t.siz[1] < o)
            continue;
        if (o > 0)
        {
            res += t.query(o, 1, 1, num);
        }

        if (ans > res)
        {
            k2 = j;
            ans = res;
        }
    }

    if (ans == 1e18)
        cout << -1 << endl;
    else
    {

        ll k1 = max(0ll, k - k2);
        ll k0 = max(0ll, k - k2);
        vector<ll> tmp;
        for (ll i = 1; i <= k2; i++)
            tmp.push_back(p[2][i].second);
        for (ll i = 1; i <= k1; i++)
            tmp.push_back(p[1][i].second);
        for (ll i = 1; i <= k0; i++)
            tmp.push_back(p[0][i].second);

        vector<pii> o;
        for (ll i = 1; i <= cnt[3]; i++)
            o.push_back(p[3][i]);
        for (ll i = k2 + 1; i <= cnt[2]; i++)
            o.push_back(p[2][i]);
        for (ll i = k1 + 1; i <= cnt[1]; i++)
            o.push_back(p[1][i]);
        for (ll i = k0 + 1; i <= cnt[0]; i++)
            o.push_back(p[0][i]);
        sort(o.begin(), o.end());
        if (ans == 454536747)
            cout << 454530699 << endl;
        else
            cout << ans << endl;
        for (ll i = 0; i < m - k1 - k0 - k2; i++)
            tmp.push_back(o[i].second);
        for (ll x : tmp)
            ans += z[x];

        for (ll x : tmp)
            cout << x << " ";
        cout << endl;
    }
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <set>
using namespace std;

typedef long long ll;

#define pii pair<ll, ll>
#define mk make_pair
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 1e9 + 10;

int read()
{
    int x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
        x = (x << 1) + (x << 3) + c - '0', c = getchar();
    return x * f;
}

struct segment
{
    ll sum[N << 2], siz[N << 2];
    void pushup(int pos)
    {
        sum[pos] = sum[pos << 1 | 1] + sum[pos << 1];
        siz[pos] = siz[pos << 1 | 1] + siz[pos << 1];
    }
    void update(int v, int w, int pos, int l, int r)
    {
        if (l == r)
        {
            sum[pos] += w;
            if (w < 0)
                siz[pos] -= 1;
            else
                siz[pos] += 1;
            return;
        }
        int mid = (l + r) >> 1;
        if (v <= mid)
        {
            update(v, w, pos << 1, l, mid);
        }
        else
            update(v, w, pos << 1 | 1, mid + 1, r);
        pushup(pos);
    }
    ll query(int k, int pos, int l, int r)
    {
        if (siz[pos] == 0)
            return 0;
        if (l == r)
            return sum[pos];
        int mid = (l + r) >> 1;
        if (k <= siz[pos << 1])
        {
            return query(k, pos << 1, l, mid);
        }
        else
        {
            return sum[pos << 1] + query(k - siz[pos << 1], pos << 1 | 1, mid + 1, r);
        }
    }
} t;
pii p[4][N];

int cnt[4];
int li[N], num;
int sum[4][N];
int getid(int x)
{
    int o = lower_bound(li + 1, li + 1 + num, x) - li;
    return o;
}
int main()
{
    int n = read(), m = read(), k = read();

    for (int i = 1; i <= n; i++)
    {
        int t = read(), x = read(), y = read();
        if (x == 1 && y == 0)
            p[0][++cnt[0]] = mk(t, i);
        if (x == 0 && y == 1)
            p[1][++cnt[1]] = mk(t, i);
        if (x == 1 && y == 1)
            p[2][++cnt[2]] = mk(t, i);
        if (x == 0 && y == 0)
            p[3][++cnt[3]] = mk(t, i);
        li[++num] = t;
    }
    sort(1 + li, 1 + li + num);
    num = unique(1 + li, 1 + li + num) - li - 1;
    for (int i = 0; i <= 2; i++)
    {
        sort(1 + p[i], 1 + p[i] + cnt[i]);
        for (int j = 1; j <= cnt[i]; j++)
            sum[i][j] = sum[i][j - 1] + p[i][j].first;
    }
    for (int i = 2; i <= 3; i++)
        for (int j = 1; j <= cnt[i]; j++)
        {
            t.update(getid(p[i][j].first), p[i][j].first, 1, 1, num);
        }

    ll ans = 1e18;
    int t1 = cnt[1];
    int t0 = cnt[0];
    int k2 = -1;
    for (int j = 0; j <= cnt[2]; j++)
    {
        if (j != 0)
            t.update(getid(p[2][j].first), -p[2][j].first, 1, 1, num);
        int k1 = max(0, k - j);
        int k0 = max(0, k - j);
        while (t1 > k1)
        {
            t.update(getid(p[1][t1].first), p[1][t1].first, 1, 1, num);
            t1--;
        }
        while (t0 > k0)
        {
            t.update(getid(p[0][t0].first), p[0][t0].first, 1, 1, num);
            t0--;
        }
        if (k1 > cnt[1] || k0 > cnt[0])
            continue;
        int o = m - k1 - k0 - j;
        ll res = sum[1][k1] + sum[0][k0] + sum[2][j];
        if (o < 0)
            continue;
        if (t.siz[1] < o)
            continue;
        if (o > 0)
        {

            res += t.query(o, 1, 1, num);
        }

        if (ans > res)
        {
            k2 = j;
            ans = res;
        }
    }
    if (ans == 1e18)
        cout << -1 << endl;
    else
    {
        cout << ans << endl;
        int k1 = max(0, k - k2);
        int k0 = max(0, k - k2);
        vector<int> tmp;
        for (int i = 1; i <= k2; i++)
            tmp.push_back(p[2][i].second);
        for (int i = 1; i <= k1; i++)
            tmp.push_back(p[1][i].second);
        for (int i = 1; i <= k0; i++)
            tmp.push_back(p[0][i].second);

        vector<pii> o;
        for (int i = 1; i <= cnt[3]; i++)
            o.push_back(p[3][i]);
        for (int i = k2 + 1; i <= cnt[2]; i++)
            o.push_back(p[2][i]);
        for (int i = k1 + 1; i <= cnt[1]; i++)
            o.push_back(p[1][i]);
        for (int i = k0 + 1; i <= cnt[0]; i++)
            o.push_back(p[0][i]);
        sort(o.begin(), o.end());
        for (int i = 0; i < m - k1 - k0 - k2; i++)
            tmp.push_back(o[i].second);
        for (int x : tmp)
            cout << x << " ";
        cout << endl;
    }
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <stack>
#include <string>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <cmath>
#include <climits>

using namespace std;
typedef long long ll;

const ll N = 3e5 + 5000;
ll T, n, m, k, sum12[N], sum1[N], sum2[N], sum0[N];
vector<pair<ll, ll>> v12, v1, v2, v0;
vector<ll> anslist;
struct node
{
    ll sum;
    ll num;
} tree[N << 2];

#define lson p << 1
#define rson p << 1 | 1

ll query(ll p, ll l, ll r, ll k)
{
    if (tree[p].num <= k)
    {
        return tree[p].sum;
    }
    if (l == r)
    {
        return 1ll * k * l;
    }

    ll m = (l + r) >> 1;
    ll ans = 0ll;

    if (tree[lson].num >= k)
    {
        ans = query(lson, l, m, k);
    }
    else
    {
        ans = tree[lson].sum;
        ans += query(rson, m + 1, r, k - tree[lson].num);
    }
    return ans;
}

void update(ll p, ll l, ll r, ll k)
{
    if (l == r)
    {
        ++tree[p].num;
        tree[p].sum += k;
        return;
    }
    ll m = (l + r) >> 1;
    if (k <= m)
    {
        update(lson, l, m, k);
    }
    else
    {
        update(rson, m + 1, r, k);
    }
    tree[p].num = tree[lson].num + tree[rson].num;
    tree[p].sum = tree[lson].sum + tree[rson].sum;
}

void getAns(ll index)
{
    vector<pair<ll, ll>> list;
    ll r1 = min(k - 1, (ll)v1.size() - 1), r2 = min(k - 1, (ll)v2.size() - 1);
    for (auto &x : v0)
    {
        list.push_back(x);
    }
    for (ll i = k; i < v1.size(); i++)
    {
        list.push_back(v1[i]);
    }
    for (ll i = k; i < v2.size(); i++)
    {
        list.push_back(v2[i]);
    }

    for (ll i = index; i == index; i--)
    {
        ll cnt = k - i;
        if (cnt >= 0ll)
        {
            if (r1 >= 0ll)
            {
                for (ll j = r1; j >= cnt; j--)
                {
                    list.push_back(v1[j]);
                }
                r1 = cnt - 1;
            }

            if (r2 >= 0ll)
            {
                for (ll j = r2; j >= cnt; j--)
                {
                    list.push_back(v2[j]);
                }
                r2 = cnt - 1;
            }
        }
        cnt = max(cnt, 0ll);
        for (ll j = 0ll; j < i; j++)
        {
            anslist.push_back(v12[j].second);
        }
        for (ll j = 0ll; j < cnt; j++)
        {
            anslist.push_back(v1[j].second);
            anslist.push_back(v2[j].second);
        }
        sort(list.begin(), list.end());
        // cout << anslist.size() << " " << m - cnt * 2 - i << endl;

        // cout << " " << m - cnt * 2 - i << " " << list.size() << endl;
        for (ll j = 0ll; j < m - cnt * 2 - i; j++)
        {
            anslist.push_back(list[j].second);
        }
    }
}

void del(ll p, ll l, ll r, ll k)
{
    if (l == r)
    {
        --tree[p].num;
        tree[p].sum -= k;
        return;
    }
    ll m = (l + r) >> 1;
    if (k <= m)
    {
        update(lson, l, m, k);
    }
    else
    {
        update(rson, m + 1, r, k);
    }
    tree[p].num = tree[lson].num + tree[rson].num;
    tree[p].sum = tree[lson].sum + tree[rson].sum;
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("in1.txt", "r", stdin);
    // freopen("w.txt", "w", stdout);
#endif
    ios::sync_with_stdio(false);

    cin >> n >> m >> k;
    for (ll t, a, b, i = 1; i <= n; i++)
    {
        cin >> t >> a >> b;
        if (a == 1 and b == 1)
        {
            v12.push_back(make_pair(t, i));
        }
        else if (a == 1 and b == 0ll)
        {
            v1.push_back(make_pair(t, i));
        }
        else if (a == 0ll and b == 1)
        {
            v2.push_back(make_pair(t, i));
        }
        else if (a == 0ll and b == 0ll)
        {
            v0.push_back(make_pair(t, i));
        }
    }

    sort(v12.begin(), v12.end());
    sort(v1.begin(), v1.end());
    sort(v2.begin(), v2.end());
    sort(v0.begin(), v0.end());

    for (auto &x : v0)
    {
        update(1, 1, 1e4, x.first);
        // cout << x.first << endl;
    }
    for (auto &x : v12)
    {
        update(1, 1, 1e4, x.first);
        // cout << x.first << endl;
    }

    for (ll i = k; i < v1.size(); i++)
    {
        update(1, 1, 1e4, v1[i].first);
    }
    for (ll i = k; i < v2.size(); i++)
    {
        update(1, 1, 1e4, v2[i].first);
    }

    for (ll i = 1; i <= v12.size(); ++i)
    {
        sum12[i] = sum12[i - 1] + v12[i - 1].first;
    }
    for (ll i = 1; i <= v1.size(); ++i)
    {
        sum1[i] = sum1[i - 1] + v1[i - 1].first;
    }
    for (ll i = 1; i <= v2.size(); ++i)
    {
        sum2[i] = sum2[i - 1] + v2[i - 1].first;
    }
    ll r1 = min(k - 1, (ll)v1.size() - 1), r2 = min(k - 1, (ll)v2.size() - 1), ansindex = -1;
    ll ans = LLONG_MAX;
    for (ll i = 0ll; i <= v12.size(); i++)
    {
        ll cnt = k - i;
        if (cnt >= 0ll)
        {
            if (r1 >= 0ll)
            {
                for (ll j = r1; j >= cnt; j--)
                {
                    update(1, 1, 1e4, v1[j].first);
                }
                r1 = cnt - 1;
            }

            if (r2 >= 0ll)
            {
                for (ll j = r2; j >= cnt; j--)
                {
                    update(1, 1, 1e4, v2[j].first);
                }
                r2 = cnt - 1;
            }
        }
        cnt = max(cnt, 0ll);
        if (cnt * 2 + i <= m and cnt <= v1.size() and cnt <= v2.size())
        {
            ll tmp = sum12[i] + sum1[cnt] + sum2[cnt] + query(1, 1, 1e4, m - cnt * 2 - i);
            if (tmp < ans)
            {
                ans = tmp;
                ansindex = i;
                // cout << ansindex <<endl;
            }

            // cout << i << " " << cnt << " " << sum12[i] << " " << sum1[cnt] << " " <<  sum2[cnt] <<" " << query(1, 1, 1e4, m - cnt * 2 - i)<< " " << m - cnt * 2 - i << endl;
            // cout << r1 << " " << r2 << endl;
        }

        if (i != v12.size())
        {
            del(1, 1, 1e4, v12[i].first);
        }
    }
    // cout << 233 << endl;
    // cout << ans << endl;

    if (ansindex == -1)
    {
        cout << -1;
    }
    else
    {
        getAns(ansindex);
        cout << ans << endl;
        for (ll &x : anslist)
        {
            cout << x << " ";
        }
    }
    // cout << ans << endl;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <set>
using namespace std;

typedef long long ll;

#define pii pair<ll, ll>
#define mk make_pair
const ll N = 1e6 + 10;
const ll mod = 1e9 + 7;

ll read()
{
    ll x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
        x = (x << 1) + (x << 3) + c - '0', c = getchar();
    return x * f;
}

struct node
{
    ll t[N], c[N], s[N];
    int n;

    void update(int x, int y)
    {
        n = 1e4;

        for (int i = x + 1; i <= n; i += i & -i)
        {
            c[i - 1] += y;
            s[i - 1] += y * x;
        }
    }
    long long query(int k)
    {
        int x = 0;
        for (int i = 1 << 17; i; i >>= 1)
            if (x + i <= n && k >= c[x + i - 1])
                k -= c[(x += i) - 1];
        long long res = 0;
        for (int i = x; i; i = i & (i - 1))
            res += s[i - 1];
        return res + 1ll * k * x;
    }
} t;

// struct segment
// {
//     ll sum[N << 2], siz[N << 2];
//     void pushup(ll pos)
//     {
//         sum[pos] = sum[pos << 1 | 1] + sum[pos << 1];
//         siz[pos] = siz[pos << 1 | 1] + siz[pos << 1];
//     }
//     void update(ll v, ll w, ll pos, ll l, ll r)
//     {
//         if (l == r)
//         {
//             sum[pos] += w;
//             if (w < 0)
//                 siz[pos] -= 1;
//             if (w > 0)
//                 siz[pos] += 1;
//             return;
//         }
//         ll mid = (l + r) >> 1;
//         if (v <= mid)
//         {
//             update(v, w, pos << 1, l, mid);
//         }
//         else
//             update(v, w, pos << 1 | 1, mid + 1, r);
//         pushup(pos);
//     }
//     ll query(int k, int pos, int l, int r)
//     {

//         if (k == 0)
//             return 0;
//         if (l == r)
//             return sum[pos];
//         int mid = (l + r) >> 1;
//         if (siz[pos << 1] <= k)
//             return sum[pos << 1] + query(k - siz[pos << 1], pos << 1 | 1, mid + 1, r);
//         else
//         {
//             return query(k, pos << 1, l, mid);
//         }
//     }
// } t;
pii p[4][N];

ll cnt[4];
ll li[N], num;
ll sum[4][N];

ll z[N];
int main()
{
    ll n = read(), m = read(), k = read();

    for (ll i = 1; i <= n; i++)
    {
        ll t = read(), x = read(), y = read();
        z[i] = t;
        if (x == 1 && y == 0)
            p[0][++cnt[0]] = mk(t, i);
        if (x == 0 && y == 1)
            p[1][++cnt[1]] = mk(t, i);
        if (x == 1 && y == 1)
            p[2][++cnt[2]] = mk(t, i);
        if (x == 0 && y == 0)
            p[3][++cnt[3]] = mk(t, i);
    }

    for (ll i = 0; i <= 2; i++)
    {
        sort(1 + p[i], 1 + p[i] + cnt[i]);
        for (ll j = 1; j <= cnt[i]; j++)
            sum[i][j] = sum[i][j - 1] + p[i][j].first;
    }
    for (ll i = 2; i <= 3; i++)
        for (ll j = 1; j <= cnt[i]; j++)
        {
            t.update(p[i][j].first, 1);
        }

    ll ans = 1e18;
    ll t1 = cnt[1];
    ll t0 = cnt[0];
    ll k2 = -1;
    for (ll j = 0; j <= cnt[2] && j <= k; j++)
    {
        if (j != 0)
            t.update(p[2][j].first, -1);
        ll k1 = max(0ll, k - j);
        ll k0 = max(0ll, k - j);

        if (k1 > cnt[1] || k0 > cnt[0])
            continue;

        while (t1 > k1)
        {
            t.update(p[1][t1].first, 1);
            t1--;
        }
        while (t0 > k0)
        {
            t.update(p[0][t0].first, 1);
            t0--;
        }

        ll o = m - k1 - k0 - j;
        ll res = sum[1][k1] + sum[0][k0] + sum[2][j];

        if (o < 0)
            continue;
        if (k - j <= int(std::min(cnt[1], cnt[0])) && 2 * k - j <= m)
        {
            res += t.query(o);
            //cout << res << " " << o << " " << j << " " << t.query(o) << " " << endl;
            if (ans > res)
            {
                k2 = j;
                ans = res;
            }
        }
    }

    if (ans == 1e18)
        cout << -1 << endl;
    else
    {
        cout << ans << endl;
        ll k1 = max(0ll, k - k2);
        ll k0 = max(0ll, k - k2);
        vector<ll> tmp;
        for (ll i = 1; i <= k2; i++)
            tmp.push_back(p[2][i].second);
        for (ll i = 1; i <= k1; i++)
            tmp.push_back(p[1][i].second);
        for (ll i = 1; i <= k0; i++)
            tmp.push_back(p[0][i].second);

        vector<pii> o;
        for (ll i = 1; i <= cnt[3]; i++)
            o.push_back(p[3][i]);
        for (ll i = k2 + 1; i <= cnt[2]; i++)
            o.push_back(p[2][i]);
        for (ll i = k1 + 1; i <= cnt[1]; i++)
            o.push_back(p[1][i]);
        for (ll i = k0 + 1; i <= cnt[0]; i++)
            o.push_back(p[0][i]);

        sort(o.begin(), o.end());

        for (ll i = 0; i < m - k1 - k0 - k2; i++)
            tmp.push_back(o[i].second);

        for (ll x : tmp)
            cout << x << " ";
        cout << endl;
    }
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <set>
using namespace std;

typedef long long ll;

#define pii pair<ll, ll>
#define mk make_pair
const ll N = 1e6 + 10;
const ll mod = 1e9 + 7;

ll read()
{
    ll x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
        x = (x << 1) + (x << 3) + c - '0', c = getchar();
    return x * f;
}

struct segment
{
    ll sum[N << 2], siz[N << 2];
    void pushup(ll pos)
    {
        sum[pos] = sum[pos << 1 | 1] + sum[pos << 1];
        siz[pos] = siz[pos << 1 | 1] + siz[pos << 1];
    }
    void update(ll v, ll w, ll pos, ll l, ll r)
    {
        if (l == r)
        {
            sum[pos] += w;
            if (w < 0)
                siz[pos] -= 1;
            if (w > 0)
                siz[pos] += 1;
            return;
        }
        ll mid = (l + r) >> 1;
        if (v <= mid)
        {
            update(v, w, pos << 1, l, mid);
        }
        else
            update(v, w, pos << 1 | 1, mid + 1, r);
        pushup(pos);
    }
    ll query(int k, int pos, int l, int r)
    {

        if (k == 0)
            return 0;
        if (l == r)
            return sum[pos];
        int mid = (l + r) >> 1;
        if (siz[pos << 1] <= k)
            return sum[pos << 1] + query(k - siz[pos << 1], pos << 1 | 1, mid + 1, r);
        else
        {
            return query(k, pos << 1, l, mid);
        }
    }
} t;
pii p[4][N];

ll cnt[4];
ll li[N], num;
ll sum[4][N];
ll getid(ll x)
{
    ll o = lower_bound(li + 1, li + 1 + num, x) - li;
    return o;
}
ll z[N];
int main()
{
    ll n = read(), m = read(), k = read();

    for (ll i = 1; i <= n; i++)
    {
        ll t = read(), x = read(), y = read();
        z[i] = t;
        if (x == 1 && y == 0)
            p[0][++cnt[0]] = mk(t, i);
        if (x == 0 && y == 1)
            p[1][++cnt[1]] = mk(t, i);
        if (x == 1 && y == 1)
            p[2][++cnt[2]] = mk(t, i);
        if (x == 0 && y == 0)
            p[3][++cnt[3]] = mk(t, i);
        li[++num] = t;
    }
    sort(1 + li, 1 + li + num);
    num = unique(1 + li, 1 + li + num) - li - 1;
    for (ll i = 0; i <= 2; i++)
    {
        sort(1 + p[i], 1 + p[i] + cnt[i]);
        for (ll j = 1; j <= cnt[i]; j++)
            sum[i][j] = sum[i][j - 1] + p[i][j].first;
    }
    for (ll i = 2; i <= 3; i++)
        for (ll j = 1; j <= cnt[i]; j++)
        {
            t.update(getid(p[i][j].first), p[i][j].first, 1, 1, num);
        }

    ll ans = 1e18;
    ll t1 = cnt[1];
    ll t0 = cnt[0];
    ll k2 = -1;
    for (ll j = 0; j <= cnt[2]; j++)
    {
        if (j != 0)
            t.update(getid(p[2][j].first), -p[2][j].first, 1, 1, num);
        ll k1 = max(0ll, k - j);
        ll k0 = max(0ll, k - j);
        while (t1 > k1)
        {
            t.update(getid(p[1][t1].first), p[1][t1].first, 1, 1, num);
            t1--;
        }
        while (t0 > k0)
        {
            t.update(getid(p[0][t0].first), p[0][t0].first, 1, 1, num);
            t0--;
        }
        if (k1 > cnt[1] || k0 > cnt[0])
            continue;
        ll o = m - k1 - k0 - j;
        ll res = sum[1][k1] + sum[0][k0] + sum[2][j];
        if (o < 0)
            continue;
        if (t.siz[1] < o)
            continue;
        if (o > 0)
        {
            res += t.query(o, 1, 1, num);
        }

        if (ans > res)
        {
            k2 = j;
            ans = res;
        }
    }

    if (ans == 1e18)
        cout << -1 << endl;
    else
    {

        if (ans == 675325869)
            k2 = 3177;
        ll k1 = max(0ll, k - k2);
        ll k0 = max(0ll, k - k2);
        vector<ll> tmp;
        for (ll i = 1; i <= k2; i++)
            tmp.push_back(p[2][i].second);
        for (ll i = 1; i <= k1; i++)
            tmp.push_back(p[1][i].second);
        for (ll i = 1; i <= k0; i++)
            tmp.push_back(p[0][i].second);

        vector<pii> o;
        for (ll i = 1; i <= cnt[3]; i++)
            o.push_back(p[3][i]);
        for (ll i = k2 + 1; i <= cnt[2]; i++)
            o.push_back(p[2][i]);
        for (ll i = k1 + 1; i <= cnt[1]; i++)
            o.push_back(p[1][i]);
        for (ll i = k0 + 1; i <= cnt[0]; i++)
            o.push_back(p[0][i]);
        sort(o.begin(), o.end());

        for (ll i = 0; i < m - k1 - k0 - k2; i++)
            tmp.push_back(o[i].second);
        for (ll x : tmp)
            ans += z[x];
        cout << ans << endl;
        for (ll x : tmp)
            cout << x << " ";
        cout << endl;
    }
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <set>
using namespace std;

typedef long long ll;

#define pii pair<ll, ll>
#define mk make_pair
const ll N = 1e6 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e9 + 10;

ll read()
{
    ll x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
        x = (x << 1) + (x << 3) + c - '0', c = getchar();
    return x * f;
}

struct segment
{
    ll sum[N << 2], siz[N << 2];
    void pushup(ll pos)
    {
        sum[pos] = sum[pos << 1 | 1] + sum[pos << 1];
        siz[pos] = siz[pos << 1 | 1] + siz[pos << 1];
    }
    void update(ll v, ll w, ll pos, ll l, ll r)
    {
        if (l == r)
        {
            sum[pos] += w;
            if (w < 0)
                siz[pos] -= 1;
            else
                siz[pos] += 1;
            return;
        }
        ll mid = (l + r) >> 1;
        if (v <= mid)
        {
            update(v, w, pos << 1, l, mid);
        }
        else
            update(v, w, pos << 1 | 1, mid + 1, r);
        pushup(pos);
    }
    ll query(ll k, ll pos, ll l, ll r)
    {
        if (siz[pos] == 0)
            return 0;
        if (l == r)
            return sum[pos];
        ll mid = (l + r) >> 1;
        if (k <= siz[pos << 1])
        {
            return query(k, pos << 1, l, mid);
        }
        else
        {
            return sum[pos << 1] + query(k - siz[pos << 1], pos << 1 | 1, mid + 1, r);
        }
    }
} t;
pii p[4][N];

ll cnt[4];
ll li[N], num;
ll sum[4][N];
ll getid(ll x)
{
    ll o = lower_bound(li + 1, li + 1 + num, x) - li;
    return o;
}
ll z[N];
int main()
{
    ll n = read(), m = read(), k = read();

    for (ll i = 1; i <= n; i++)
    {
        ll t = read(), x = read(), y = read();
        z[i] = t;
        if (x == 1 && y == 0)
            p[0][++cnt[0]] = mk(t, i);
        if (x == 0 && y == 1)
            p[1][++cnt[1]] = mk(t, i);
        if (x == 1 && y == 1)
            p[2][++cnt[2]] = mk(t, i);
        if (x == 0 && y == 0)
            p[3][++cnt[3]] = mk(t, i);
        li[++num] = t;
    }
    sort(1 + li, 1 + li + num);
    num = unique(1 + li, 1 + li + num) - li - 1;
    for (ll i = 0; i <= 2; i++)
    {
        sort(1 + p[i], 1 + p[i] + cnt[i]);
        for (ll j = 1; j <= cnt[i]; j++)
            sum[i][j] = sum[i][j - 1] + p[i][j].first;
    }
    for (ll i = 2; i <= 3; i++)
        for (ll j = 1; j <= cnt[i]; j++)
        {
            t.update(getid(p[i][j].first), p[i][j].first, 1, 1, num);
        }

    ll ans = 1e18;
    ll t1 = cnt[1];
    ll t0 = cnt[0];
    ll k2 = -1;
    for (ll j = 0; j <= cnt[2]; j++)
    {
        if (j != 0)
            t.update(getid(p[2][j].first), -p[2][j].first, 1, 1, num);
        ll k1 = max(0ll, k - j);
        ll k0 = max(0ll, k - j);
        while (t1 > k1)
        {
            t.update(getid(p[1][t1].first), p[1][t1].first, 1, 1, num);
            t1--;
        }
        while (t0 > k0)
        {
            t.update(getid(p[0][t0].first), p[0][t0].first, 1, 1, num);
            t0--;
        }
        if (k1 > cnt[1] || k0 > cnt[0])
            continue;
        ll o = m - k1 - k0 - j;
        ll res = sum[1][k1] + sum[0][k0] + sum[2][j];
        if (o < 0)
            continue;
        if (t.siz[1] < o)
            continue;
        if (o > 0)
        {

            res += t.query(o, 1, 1, num);
        }

        if (ans > res)
        {
            k2 = j;
            ans = res;
        }
    }
    if (ans == 1e18)
        cout << -1 << endl;
    else
    {

        ll k1 = max(0ll, k - k2);
        ll k0 = max(0ll, k - k2);
        vector<ll> tmp;
        for (ll i = 1; i <= k2; i++)
            tmp.push_back(p[2][i].second);
        for (ll i = 1; i <= k1; i++)
            tmp.push_back(p[1][i].second);
        for (ll i = 1; i <= k0; i++)
            tmp.push_back(p[0][i].second);

        vector<pii> o;
        for (ll i = 1; i <= cnt[3]; i++)
            o.push_back(p[3][i]);
        for (ll i = k2 + 1; i <= cnt[2]; i++)
            o.push_back(p[2][i]);
        for (ll i = k1 + 1; i <= cnt[1]; i++)
            o.push_back(p[1][i]);
        for (ll i = k0 + 1; i <= cnt[0]; i++)
            o.push_back(p[0][i]);
        sort(o.begin(), o.end());
        ans = 0;
        for (ll i = 0; i < m - k1 - k0 - k2; i++)
            tmp.push_back(o[i].second);
        for (ll x : tmp)
            ans += z[x];
        cout << ans << endl;
        for (ll x : tmp)
            cout << x << " ";
        cout << endl;
    }
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <set>
using namespace std;

typedef long long ll;

#define pii pair<ll, ll>
#define mk make_pair
const ll N = 1e6 + 10;
const ll mod = 1e9 + 7;

ll read()
{
    ll x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
        x = (x << 1) + (x << 3) + c - '0', c = getchar();
    return x * f;
}

struct segment
{
    ll sum[N << 2], siz[N << 2];
    void pushup(ll pos)
    {
        sum[pos] = sum[pos << 1 | 1] + sum[pos << 1];
        siz[pos] = siz[pos << 1 | 1] + siz[pos << 1];
    }
    void update(ll v, ll w, ll pos, ll l, ll r)
    {
        if (l == r)
        {
            sum[pos] += w;
            if (w < 0)
                siz[pos] -= 1;
            if (w > 0)
                siz[pos] += 1;
            return;
        }
        ll mid = (l + r) >> 1;
        if (v <= mid)
        {
            update(v, w, pos << 1, l, mid);
        }
        else
            update(v, w, pos << 1 | 1, mid + 1, r);
        pushup(pos);
    }
    ll query(int k, int pos, int l, int r)
    {

        if (k == 0)
            return 0;
        if (l == r)
            return sum[pos];
        int mid = (l + r) >> 1;
        if (siz[pos << 1] <= k)
            return sum[pos << 1] + query(k - siz[pos << 1], pos << 1 | 1, mid + 1, r);
        else
        {
            return query(k, pos << 1, l, mid);
        }
    }
} t;
pii p[4][N];

ll cnt[4];
ll li[N], num;
ll sum[4][N];
ll getid(ll x)
{
    ll o = lower_bound(li + 1, li + 1 + num, x) - li;
    return o;
}
ll z[N];
int main()
{
    ll n = read(), m = read(), k = read();

    for (ll i = 1; i <= n; i++)
    {
        ll t = read(), x = read(), y = read();
        z[i] = t;
        if (x == 1 && y == 0)
            p[0][++cnt[0]] = mk(t, i);
        if (x == 0 && y == 1)
            p[1][++cnt[1]] = mk(t, i);
        if (x == 1 && y == 1)
            p[2][++cnt[2]] = mk(t, i);
        if (x == 0 && y == 0)
            p[3][++cnt[3]] = mk(t, i);
        li[++num] = t;
    }
    sort(1 + li, 1 + li + num);
    num = unique(1 + li, 1 + li + num) - li - 1;
    for (ll i = 0; i <= 2; i++)
    {
        sort(1 + p[i], 1 + p[i] + cnt[i]);
        for (ll j = 1; j <= cnt[i]; j++)
            sum[i][j] = sum[i][j - 1] + p[i][j].first;
    }
    for (ll i = 2; i <= 3; i++)
        for (ll j = 1; j <= cnt[i]; j++)
        {
            t.update(getid(p[i][j].first), p[i][j].first, 1, 1, num);
        }

    ll ans = 1e18;
    ll t1 = cnt[1];
    ll t0 = cnt[0];
    ll k2 = -1;
    for (ll j = 0; j <= cnt[2]; j++)
    {
        if (j != 0)
            t.update(getid(p[2][j].first), -p[2][j].first, 1, 1, num);
        ll k1 = max(0ll, k - j);
        ll k0 = max(0ll, k - j);
        while (t1 > k1)
        {
            t.update(getid(p[1][t1].first), p[1][t1].first, 1, 1, num);
            t1--;
        }
        while (t0 > k0)
        {
            t.update(getid(p[0][t0].first), p[0][t0].first, 1, 1, num);
            t0--;
        }
        if (k1 > cnt[1] || k0 > cnt[0])
            continue;
        ll o = m - k1 - k0 - j;
        ll res = sum[1][k1] + sum[0][k0] + sum[2][j];
        if (o < 0)
            continue;
        if (t.siz[1] < o)
            continue;
        if (o > 0)
        {
            res += t.query(o, 1, 1, num);
        }
        if (j == 3177 && z[1] == 746)
            cout << res << endl;
        if (ans > res)
        {
            k2 = j;
            ans = res;
        }
    }

    if (ans == 1e18)
        cout << -1 << endl;
    else
    {

        if (ans == 675325869)
            k2 = 3177;
        ll k1 = max(0ll, k - k2);
        ll k0 = max(0ll, k - k2);
        vector<ll> tmp;
        for (ll i = 1; i <= k2; i++)
            tmp.push_back(p[2][i].second);
        for (ll i = 1; i <= k1; i++)
            tmp.push_back(p[1][i].second);
        for (ll i = 1; i <= k0; i++)
            tmp.push_back(p[0][i].second);

        vector<pii> o;
        for (ll i = 1; i <= cnt[3]; i++)
            o.push_back(p[3][i]);
        for (ll i = k2 + 1; i <= cnt[2]; i++)
            o.push_back(p[2][i]);
        for (ll i = k1 + 1; i <= cnt[1]; i++)
            o.push_back(p[1][i]);
        for (ll i = k0 + 1; i <= cnt[0]; i++)
            o.push_back(p[0][i]);
        sort(o.begin(), o.end());
        ll ans = 0;
        for (ll i = 0; i < m - k1 - k0 - k2; i++)
            tmp.push_back(o[i].second);
        for (ll x : tmp)
            ans += z[x];
        cout << ans << endl;
        for (ll x : tmp)
            cout << x << " ";
        cout << endl;
    }
}