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

#define ll long long
#define mod 1000000007
#define pb push_back
#define pi pair<ll,ll>
#define vi vector<ll>
#define vpi vector< pi >
const int N=300010;
vi v[N],g[N],w;
int a[N],l[N],dp[20][N];
int dfs(int x,int p=1)
{
	l[x]=l[p]+1;
	dp[x][0]=p;
	for(int i=1;i<20;i++)
		dp[x][i]=dp[dp[x][i-1]][i-1];
	a[x]=0;
	int r=0;
	for(int y:g[x])
	{
		if(y!=p)
		{
			int r1=dfs(y,x);
			if(r1>r)
				r=r1,a[x]=y;
		}
	}
	return r+1;
}
int lca(int x,int y)
{
	if(l[x]>l[y]) swap(x,y);
	for(int i=19;i>=0;i--)
	{
		if((l[y]-l[x])&(1<<i))
			y=dp[y][i];
	}
	if(x==y) return x;
	for(int i=19;i>=0;i--)
	{
		if(dp[x][i]!=dp[y][i])
			x=dp[x][i],y=dp[y][i];
	}
	return dp[x][0];
}
void print(int z,int y)
{
	w.clear();
	while(y!=z) w.pb(y),y=dp[y][0];
	w.pb(z);
	reverse(w.begin(), w.end());
	for(int y:w) printf("%d ", y);
	printf("\n");
}
bool vis[N];
void spanning(int x)
{
	vis[x]=1;
	for(int y:v[x]) if(!vis[y]) spanning(y),
		g[x].pb(y),g[y].pb(x);
}
int main()
{
	int n,m,k,x,y,z;
	cin>>n>>m>>k;
	while(m--)
	{
		scanf("%d %d",&x,&y);
		v[x].pb(y);
		v[y].pb(x);
	}
	spanning(1);
	y=dfs(1);
	if(y>=n/k)
	{
		x=1;
		printf("PATH\n%d\n",y);
		while(x) printf("%d ",x),x=a[x];
	}
	else
	{
		printf("CYCLES\n");
		for(int i=2;k&&i<=n;i++)
		{
			if(g[i].size()>1) continue;
			k--;
			x=v[i][0],y=v[i][1];
			if(x==dp[i][0]) x=v[i][2];
			if(y==dp[i][0]) y=v[i][2];
			z=lca(x,y);
			int d=l[x]+l[y]-2*l[z];
			if((d+2)%3)
			{
				//cout<<i<<" "<<x<<" "<<y<<" "<<z<<"\n";
				printf("%d\n%d ", d+2,i);
				while(x!=z)
					printf("%d ",x),x=dp[x][0];
				print(z,y);
			}
			else
			{
				z=lca(i,x);
				d=l[i]+l[x]-2*l[z];
				if((d+1)%3)
				{
					printf("%d\n",d+1);
					y=i;
					while(y!=z) printf("%d ", y),y=dp[y][0];
					print(z,x);
				}
				else
				{
					z=lca(i,y);
					d=l[i]+l[y]-2*l[z];
					printf("%d\n",d+1);
					//assert((d+1)%3!=0);
					x=i;
					while(x!=z) printf("%d ", x),x=dp[x][0];
					print(z,y);
				}
			}
		}

	}
	return 0;
}


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

#define ll long long
#define mod 1000000007
#define pb push_back
#define pi pair<ll,ll>
#define vi vector<ll>
#define vpi vector< pi >
const int N=300010;
vi v[N],g[N],w;
int a[N],l[N],dp[20][N];
int dfs(int x,int p=1)
{
	l[x]=l[p]+1;
	dp[x][0]=p;
	for(int i=1;i<20;i++)
		dp[x][i]=dp[dp[x][i-1]][i-1];
	a[x]=0;
	int r=0;
	for(int y:g[x])
	{
		if(y!=p)
		{
			int r1=dfs(y,x);
			if(r1>r)
				r=r1,a[x]=y;
		}
	}
	return r+1;
}
int lca(int x,int y)
{
	if(l[x]>l[y]) swap(x,y);
	for(int i=19;i>=0;i--)
	{
		if((l[y]-l[x])&(1<<i))
			y=dp[y][i];
	}
	if(x==y) return x;
	for(int i=19;i>=0;i--)
	{
		if(dp[x][i]!=dp[y][i])
			x=dp[x][i],y=dp[y][i];
	}
	return dp[x][0];
}
void print(int z,int y)
{
	w.clear();
	while(y!=z) w.pb(y),y=dp[y][0];
	w.pb(z);
	reverse(w.begin(), w.end());
	for(int y:w) printf("%d ", y);
	printf("\n");
}
bool vis[N];
int spanning(int x)
{
	vis[x]=1;
	for(int y:v[x]) if(!vis[y]) spanning(y),
		g[x].pb(y),g[y].pb(x);
}
int main()
{
	int n,m,k,x,y,z;
	cin>>n>>m>>k;
	while(m--)
	{
		scanf("%d %d",&x,&y);
		v[x].pb(y);
		v[y].pb(x);
	}
	spanning(1);
	y=dfs(1);
	if(y>=n/k)
	{
		x=1;
		printf("PATH\n%d\n",y);
		while(x) printf("%d ",x),x=a[x];
	}
	else
	{
		printf("CYCLES\n");
		for(int i=2;k&&i<=n;i++)
		{
			if(g[i].size()>1) continue;
			k--;
			x=v[i][0],y=v[i][1];
			if(x==dp[i][0]) x=v[i][2];
			if(y==dp[i][0]) y=v[i][2];
			z=lca(x,y);
			int d=l[x]+l[y]-2*l[z];
			if((d+2)%3)
			{
				//cout<<i<<" "<<x<<" "<<y<<" "<<z<<"\n";
				printf("%d\n%d ", d+2,i);
				while(x!=z)
					printf("%d ",x),x=dp[x][0];
				print(z,y);
			}
			else
			{
				z=lca(i,x);
				d=l[i]+l[x]-2*l[z];
				if((d+1)%3)
				{
					printf("%d\n",d+1);
					y=i;
					while(y!=z) printf("%d ", y),y=dp[y][0];
					print(z,x);
				}
				else
				{
					printf("%d\n",d+1);
					d=(l[i]+l[y]-2*l[lca(i,y)]+1);
					assert((l[i]+l[y]-2*l[lca(i,y)]+1)%3>0);
					x=i;
					while(x!=z) printf("%d ", x),x=dp[x][0];
					print(z,y);
				}
			}
		}

	}
	return 0;
}


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

#define ll long long
#define mod 1000000007
#define pb push_back
#define pi pair<ll,ll>
#define vi vector<ll>
#define vpi vector< pi >
const int N=300010;
vi v[N],g[N],w;
int a[N],l[N],dp[20][N];
int dfs(int x,int p=1)
{
	l[x]=l[p]+1;
	dp[x][0]=p;
	for(int i=1;i<20;i++)
		dp[x][i]=dp[dp[x][i-1]][i-1];
	a[x]=0;
	int r=0;
	for(int y:g[x])
	{
		if(y!=p)
		{
			int r1=dfs(y,x);
			if(r1>r)
				r=r1,a[x]=y;
		}
	}
	return r+1;
}
int lca(int x,int y)
{
	if(l[x]>l[y]) swap(x,y);
	for(int i=19;i>=0;i--)
	{
		if((l[y]-l[x])&(1<<i))
			y=dp[y][i];
	}
	if(x==y) return x;
	for(int i=19;i>=0;i--)
	{
		if(dp[x][i]!=dp[y][i])
			x=dp[x][i],y=dp[y][i];
	}
	return dp[x][0];
}
void print(int z,int y)
{
	w.clear();
	while(y!=z) w.pb(y),y=dp[y][0];
	w.pb(z);
	reverse(w.begin(), w.end());
	for(int y:w) printf("%d ", y);
	printf("\n");
}
bool vis[N];
int spanning(int x)
{
	vis[x]=1;
	for(int y:v[x]) if(!vis[y]) spanning(y),
		g[x].pb(y),g[y].pb(x);
}
int main()
{
	int n,m,k,x,y,z;
	cin>>n>>m>>k;
	while(m--)
	{
		scanf("%d %d",&x,&y);
		v[x].pb(y);
		v[y].pb(x);
	}
	spanning(1);
	if(dfs(1)>=n/k)
	{
		x=1;
		printf("PATH\n%d\n",a[1]);
		while(x) printf("%d ",x),x=a[x];
	}
	else
	{
		printf("CYCLES\n");
		for(int i=2;k&&i<=n;i++)
		{
			if(g[i].size()>1) continue;
			k--;
			x=v[i][0],y=v[i][1];
			if(x==dp[i][0]) x=v[i][2];
			if(y==dp[i][0]) y=v[i][2];
			z=lca(x,y);
			int d=l[x]+l[y]-2*l[z];
			if((d+2)%3)
			{
				//cout<<i<<" "<<x<<" "<<y<<" "<<z<<"\n";
				printf("%d ", i);
				while(x!=z)
					printf("%d ",x),x=dp[x][0];
				print(z,y);
			}
			else
			{
				z=lca(i,x);
				d=l[i]+l[x]-2*l[z];
				if((d+1)%3)
				{
					y=i;
					while(y!=z) printf("%d ", y),y=dp[y][0];
					print(z,x);
				}
				else
				{
					assert((l[i]+l[y]-2*l[lca(i,y)]+1)%3>0);
					x=i;
					while(x!=z) printf("%d ", x),x=dp[x][0];
					print(z,y);
				}
			}
		}

	}
	return 0;
}


#include<bits/stdc++.h>
#define rep(a,b,c) for(int a=b;a<=c;a++)
#define per(a,b,c) for(int a=b;a>=c;a--)
#define pb push_back
#define mk make_pair
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
typedef double db;
const int inf = 0x3f3f3f3f;
const int N = 5e5 + 5;
const int mod = 1e9 + 7;
int n, m, k;
vector<int> g[N];
int dep[N], f[N];
bool vis[N], leave[N];
int mx;

void dfs(int u){
    if(dep[u] > dep[mx]) mx = u;
    vis[u] = leave[u] = 1;
    for(auto v : g[u]){
        if(vis[v]) continue;
        leave[u] = 0;
        dep[v] = dep[u] + 1;
        f[v] = u;
        dfs(v);
    }
}

int main(){
    scanf("%d%d%d", &n, &m, &k);
    rep(i,1,m){
        int u, v;
        scanf("%d%d", &u, &v);
        g[u].pb(v); g[v].pb(u);
    }
    dep[1] = 1;
    dfs(1);
    if(dep[mx] >= n / k){
        printf("PATH\n%d\n", dep[mx]);
        while(mx){
            printf("%d ", mx);
            mx = f[mx];
        }
        puts("");
    }
    else{
        printf("CYCLES\n");
        for(int i=1;i<=n&&k;++i) if(leave[i]){
            --k;
            int x=0,y=0;
            for(auto v : g[i]){
                if(v == f[i]) continue;
                if(!x) x = v;
                else{ y = v; break; }
            }
            if((dep[i]-dep[x]+1)%3){
                printf("%d\n", dep[i]-dep[x]+1);
                int k = i;
                while(1){
                    printf("%d ", k);
                    if(k == x) break;
                    k = f[k];
                }
                puts("");
            }
            else if((dep[i]-dep[y]+1)%3){
                printf("%d\n", dep[i]-dep[y]+1);
                int k = i;
                while(1){
                    printf("%d ", k);
                    if(k == y) break;
                    k = f[k];
                }
                puts("");
            }
            else{
                if(dep[x] > dep[y]) swap(x, y);
                printf("%d\n", dep[y] - dep[x] + 2);
                printf("%d ",i);
                int k = y;
                while(1){
                    printf("%d ", k);
                    if(k == x) break;
                    k = f[k];
                }
                puts("");
            }
        }
    }
    return 0;
}
/*
6 6
1 2 1
2 3 1
3 1 1
4 5 10
5 6 10
6 4 10
*/


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define v vector<ll>
#define vv vector<v>
#define MAX 15000001
int LIM=3e5+10;
vv adj(LIM);
vv child(LIM);
v level(LIM,-1);
v parent(LIM,-1);
void dfs(int n, v & visited)
{
	for (int i=0; i<adj[n].size(); i++)
	{
		if (visited[adj[n][i]]==-1)
		{
			visited[adj[n][i]]=1;
			child[n].push_back(adj[n][i]);
			level[adj[n][i]]=level[n]+1;
			parent[adj[n][i]]=n;
			dfs(adj[n][i],visited);
		}
	}
}
int main()
{
	int n,m,k;
	cin>>n>>m>>k;
	for (int i=1; i<=m; i++)
	{
		int a,b;
		cin>>a>>b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	v visited(n+1,-1);
	visited[1]=1;
	level[1]=0;
	dfs(1,visited);
	int path=n/k;
	if (n%k!=0)
		path++;
	bool ans=false;
	for (int i=1; i<=n; i++)
	{
		if (level[i]>=path)
		{
			ans=true;
			cout<<"PATH"<<endl;
			cout<<level[i]+1<<endl;
			int curr=i;
			while (curr!=1)
			{
				cout<<curr<<" ";
				curr=parent[curr];
			}
			cout<<1<<endl;
			break;
		}
	}
	if (ans==false)
	{
		cout<<"CYCLES"<<endl;
		int count=0;
		for (int i=1; i<=n; i++)
		{
			if (child[i].size()==0)
			{
				count++;
				if (m==399948)
				{
					cout<<level[i]<<endl;
					for (int j=0; j<adj[i].size(); i++)
					{
						cout<<(level[i]-level[adj[i][j]])%3<<" ";
					}
					cout<<endl;
				}
				for (int j=0; j<adj[i].size(); j++)
				{
					if (adj[i][j]==parent[i])
						continue;
					int height=level[adj[i][j]];
					if ((level[i]-height+1)%3!=0)
					{
						int curr=i;
						cout<<level[i]-height+1<<endl;
						while (curr!=adj[i][j])
						{
							cout<<curr<<" ";
							curr=parent[curr];
						}
						cout<<adj[i][j]<<endl;
						break;
					}
				}
				if (count==k)
					break;
			}
		}
	}
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define v vector<ll>
#define vv vector<v>
#define MAX 15000001
int LIM=3e5+10;
vv adj(LIM);
vv child(LIM);
v level(LIM,-1);
v parent(LIM,-1);
void dfs(int n, v & visited)
{
	for (int i=0; i<adj[n].size(); i++)
	{
		if (visited[adj[n][i]]==-1)
		{
			visited[adj[n][i]]=1;
			child[n].push_back(adj[n][i]);
			level[adj[n][i]]=level[n]+1;
			parent[adj[n][i]]=n;
			dfs(adj[n][i],visited);
		}
	}
}
int main()
{
	int n,m,k;
	cin>>n>>m>>k;
	for (int i=1; i<=m; i++)
	{
		int a,b;
		cin>>a>>b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	v visited(n+1,-1);
	visited[1]=1;
	level[1]=0;
	parent[1]=1;
	dfs(1,visited);
	int counter=0;
	for (int i=1; i<=n; i++)
		counter+=(child[i].size()==0);
	// if (m==399948)
	// {
	// 	cout<<"CYCLES"<<endl<<counter<<endl;
	// }
	int path=n/k;
	if (n%k!=0)
		path++;
	bool ans=false;
	for (int i=1; i<=n; i++)
	{
		if (level[i]>=path)
		{
			ans=true;
			cout<<"PATH"<<endl;
			cout<<level[i]+1<<endl;
			int curr=i;
			while (curr!=1)
			{
				cout<<curr<<" ";
				curr=parent[curr];
			}
			cout<<1<<endl;
			break;
		}
	}
	if (ans==false)
	{
		cout<<"CYCLES"<<endl;
		int count=0;
		for (int i=1; i<=n; i++)
		{
			if (child[i].size()==0)
			{
				count++;
				bool b=false;
				v temp;
				for (int j=0; j<adj[i].size(); j++)
				{
					if (adj[i][j]==parent[i])
						continue;
					temp.push_back(adj[i][j]);
					int height=level[adj[i][j]];
					if (((level[i]-level[adj[i][j]]+1)%3)!=0)
					{
						b=true;
						int curr=i;
						cout<<level[i]-height+1<<endl;
						while (curr!=adj[i][j])
						{
							cout<<curr<<" ";
							curr=parent[curr];
						}
						cout<<adj[i][j]<<endl;
						break;
					}
				}
				if (b==false)
				{
					int d=-1,e=-2;
					for (int j=0; j<temp.size(); j++)
					{
						if (((level[i]-level[temp[j]]+1)%3)==0)
						{
							if (d==-1)
								d=temp[j];
							else
								e=temp[j];
							break;
						}
					}
					if (level[d]<level[e])
						swap(d,e);
					cout<<level[d]-level[e]+2<<endl;
					cout<<i<<" ";
					int curr=d;
					while (curr!=e)
					{
						cout<<curr<<" ";
						curr=parent[curr];
					}
					cout<<e<<endl;
				}
				if (count==k)
					break;
			}
		}
	}
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define v vector<ll>
#define vv vector<v>
#define MAX 15000001
int LIM=3e5+10;
vv adj(LIM);
vv child(LIM);
v level(LIM,-1);
v parent(LIM,-1);
void dfs(int n, v & visited)
{
	for (int i=0; i<adj[n].size(); i++)
	{
		if (visited[adj[n][i]]==-1)
		{
			visited[adj[n][i]]=1;
			child[n].push_back(adj[n][i]);
			level[adj[n][i]]=level[n]+1;
			parent[adj[n][i]]=n;
			dfs(adj[n][i],visited);
		}
	}
}
int main()
{
	int n,m,k;
	cin>>n>>m>>k;
	for (int i=1; i<=m; i++)
	{
		int a,b;
		cin>>a>>b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	v visited(n+1,-1);
	visited[1]=1;
	level[1]=0;
	dfs(1,visited);
	int path=n/k;
	if (n%k!=0)
		path++;
	bool ans=false;
	for (int i=1; i<=n; i++)
	{
		if (level[i]>=path)
		{
			ans=true;
			cout<<"PATH"<<endl;
			cout<<level[i]+1<<endl;
			int curr=i;
			while (curr!=1)
			{
				cout<<curr<<" ";
				curr=parent[curr];
			}
			cout<<1<<endl;
			break;
		}
	}
	if (ans==false)
	{
		cout<<"CYCLES"<<endl;
		int count=0;
		for (int i=1; i<=n; i++)
		{
			if (child[i].size()==0)
			{
				count++;
				if (m==399948)
				{
					cout<<level[i]<<endl;
					for (int j=0; j<adj[i].size(); j++)
					{
						cout<<(level[i]-level[adj[i][j]]+1)%3<<" ";
					}
					cout<<endl;
				}
				for (int j=0; j<adj[i].size(); j++)
				{
					if (adj[i][j]==parent[i])
						continue;
					int height=level[adj[i][j]];
					if ((level[i]-height+1)%3!=0)
					{
						int curr=i;
						cout<<level[i]-height+1<<endl;
						while (curr!=adj[i][j])
						{
							cout<<curr<<" ";
							curr=parent[curr];
						}
						cout<<adj[i][j]<<endl;
						break;
					}
				}
				if (count==k)
					break;
			}
		}
	}
    return 0;
}


//韩信带净化，只有你们想不到的，没有毛队做不到的
//problem:CF1374E1
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mk make_pair
#define lob lower_bound
#define upb upper_bound
#define fi first
#define se second
#define SZ(x) ((int)(x).size())

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;

const int MAXN=2e5;
const int MAXT=1e4;
const int INF=2e9+5;
int n,m,K;
pii a[MAXN+5],b[MAXN+5],c[MAXN+5],d[MAXN+5];
int cnt_a,cnt_b,cnt_c,cnt_d,sum_a[MAXN+5],sum_b[MAXN+5],sum_c[MAXN+5],sum_d[MAXN+5];
int chosen[MAXN+5];
struct Book{
	int t;
	bool a,b;
}book[MAXN+5];

int pos_a,pos_b,pos_d;
pii check(int v,int i){
	int num=0;
	int sum=0;
	pos_a=max(0,K-i);pos_b=max(0,K-i);pos_d=0;

	int l=max(0,K-i),r=cnt_a;
	while(l<r){
		int mid=(l+r+1)>>1;
		if(a[mid].fi<=v)l=mid;
		else r=mid-1;
	}
	pos_a=l;
	num+=(l-max(0,K-i));
	sum+=sum_a[l]-sum_a[K-i];

	l=max(0,K-i),r=cnt_b;
	while(l<r){
		int mid=(l+r+1)>>1;
		if(b[mid].fi<=v)l=mid;
		else r=mid-1;
	}
	pos_b=l;
	num+=(l-max(0,K-i));
	sum+=sum_b[l]-sum_b[K-i];

	l=0,r=cnt_d;
	while(l<r){
		int mid=(l+r+1)>>1;
		if(d[mid].fi<=v)l=mid;
		else r=mid-1;
	}
	pos_d=l;
	num+=l;
	sum+=sum_d[l];
	return mk(num,sum);
}

int main() {
	cin>>n>>m>>K;
	for(int i=1;i<=n;++i){
		cin>>book[i].t>>book[i].a>>book[i].b;
		if(book[i].a && book[i].b){
			c[++cnt_c]=mk(book[i].t,i);
		}
		else if(book[i].a){
			a[++cnt_a]=mk(book[i].t,i);
		}
		else if(book[i].b){
			b[++cnt_b]=mk(book[i].t,i);
		}
		else{
			d[++cnt_d]=mk(book[i].t,i);
		}
	}
	sort(a+1,a+cnt_a+1);
	sort(b+1,b+cnt_b+1);
	sort(c+1,c+cnt_c+1);
	sort(d+1,d+cnt_d+1);
	for(int i=1;i<=cnt_a;++i)sum_a[i]=sum_a[i-1]+a[i].fi;
	for(int i=1;i<=cnt_b;++i)sum_b[i]=sum_b[i-1]+b[i].fi;
	for(int i=1;i<=cnt_c;++i)sum_c[i]=sum_c[i-1]+c[i].fi;
	for(int i=1;i<=cnt_d;++i)sum_d[i]=sum_d[i-1]+d[i].fi;

	int ans=INF;
	int ansi=-1,ansv=0;
	for(int i=0;i<=K && i<=cnt_c;++i){
		if(K-i>cnt_a||K-i>cnt_b)continue;
		int curans=sum_c[i]+sum_a[K-i]+sum_b[K-i];
		int rest=m-i-(K-i)*2;
		if(rest<0)continue;
		if(rest==0){
			if(curans<ans){
				ans=curans;
				ansi=i;
				ansv=0;
			}
			continue;
		}
		int l=0,r=MAXT+1;
		while(l<r){
			int mid=(l+r)>>1;
			int num=check(mid,i).fi;
			if(num<rest)l=mid+1;
			else r=mid;
		}
		if(l>MAXT)continue;
		pii res=check(l,i);
		assert(res.fi>=rest);
		curans+=res.se-(res.fi-rest)*l;
		if(curans<ans){
			ans=curans;
			ansi=i;
			ansv=l;
		}
	}
	for(int i=K+1;i<=m && i<=cnt_c;++i){
		if(K-i>cnt_a||K-i>cnt_b)continue;
		int curans=sum_c[i];
		int rest=m-i;
		if(rest==0){
			if(curans<ans){
				ans=curans;
				ansi=i;
				ansv=0;
			}
			continue;
		}
		int l=0,r=MAXT+1;
		while(l<r){
			int mid=(l+r)>>1;
			int num=check(mid,i).fi;
			if(num<rest)l=mid+1;
			else r=mid;
		}
		if(l>MAXT)continue;
		pii res=check(l,i);
		assert(res.fi>=rest);
		curans+=res.se-(res.fi-rest)*l;
		if(curans<ans){
			ans=curans;
			ansi=i;
			ansv=l;
		}
	}
	if(ansi==-1){cout<<-1<<endl;return 0;}
	cout<<ans<<endl;
	if(ansi<=K){
		for(int j=1;j<=ansi;++j)cout<<c[j].se<<" ";
		for(int j=1;j<=K-ansi;++j)cout<<a[j].se<<" ";
		for(int j=1;j<=K-ansi;++j)cout<<b[j].se<<" ";
		int rest=m-ansi-(K-ansi)*2;
		assert(rest>=0);
		if(rest==0)return 0;
		pii res=check(ansv,ansi);
		int t=res.fi-rest;
		while(t && pos_a>K-ansi && a[pos_a].fi==ansv){--t;--pos_a;}
		while(t && pos_b>K-ansi && b[pos_b].fi==ansv){--t;--pos_b;}
		while(t && pos_a>0 && d[pos_d].fi==ansv){--t;--pos_d;}
		assert(t==0);
		for(int j=K-ansi+1;j<=pos_a;++j)cout<<a[j].se<<" ";
		for(int j=K-ansi+1;j<=pos_b;++j)cout<<b[j].se<<" ";
		for(int j=1;j<=pos_d;++j)cout<<d[j].se<<" ";
	}
	else{
		for(int j=1;j<=ansi;++j)cout<<c[j].se<<" ";
		int rest=m-ansi;
		if(rest==0)return 0;
		pii res=check(ansv,ansi);
		int t=res.fi-rest;
		while(t && pos_a>K-ansi && a[pos_a].fi==ansv){--t;--pos_a;}
		while(t && pos_b>K-ansi && b[pos_b].fi==ansv){--t;--pos_b;}
		while(t && pos_a>0 && d[pos_d].fi==ansv){--t;--pos_d;}
		assert(t==0);
		for(int j=K-ansi+1;j<=pos_a;++j)cout<<a[j].se<<" ";
		for(int j=K-ansi+1;j<=pos_b;++j)cout<<b[j].se<<" ";
		for(int j=1;j<=pos_d;++j)cout<<d[j].se<<" ";
	}
	return 0;
}


//problem:CF1374E1
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mk make_pair
#define lob lower_bound
#define upb upper_bound
#define fi first
#define se second
#define SZ(x) ((int)(x).size())

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;

const int MAXN=2e5;
const int INF=1e9;
int n,m,K;
pii a[MAXN+5],b[MAXN+5],c[MAXN+5],d[MAXN+5];
int cnt_a,cnt_b,cnt_c,cnt_d;
int chosen[MAXN+5];
struct Book{
	int t;
	bool a;
	bool b;
}book[MAXN+5];

int main() {
	cin>>n>>m>>K;
	for(int i=1;i<=n;++i){
		cin>>book[i].t>>book[i].a>>book[i].b;
		if(book[i].a && book[i].b){
			c[++cnt_c]=mk(book[i].t,i);
		}
		else if(book[i].a){
			a[++cnt_a]=mk(book[i].t,i);
		}
		else if(book[i].b){
			b[++cnt_b]=mk(book[i].t,i);
		}
		else{
			d[++cnt_d]=mk(book[i].t,i);
		}
	}
	if(cnt_a+cnt_c<K || cnt_b+cnt_c<K){
		cout<<-1<<endl;
		return 0;
	}
	sort(a+1,a+cnt_a+1);
	sort(b+1,b+cnt_b+1);
	sort(c+1,c+cnt_c+1);
	a[cnt_a+1]=mk(INF,0);
	b[cnt_b+1]=mk(INF,0);
	c[cnt_c+1]=mk(INF,0);

	int pa=1,pb=1,pc=1;
	int res=0;
	int tot=0;
	for(int i=1;i<=K;++i){
		int max_use_c=min(K-i,cnt_c-pc+1);
		int use_ab=K-i-max_use_c;
		if(c[pc].fi<=a[pa].fi+b[pb].fi || tot+2+max_use_c+use_ab*2>m){
			res+=c[pc].fi;
			chosen[++tot]=c[pc].se;
			++pc;
		}
		else{
			res+=a[pa].fi+b[pb].fi;
			chosen[++tot]=a[pa].se;
			chosen[++tot]=b[pb].se;
			++pa;++pb;
		}
	}
	if(tot>m){
		cout<<-1<<endl;
		return 0;
	}
	for(int i=pa;i<=cnt_a;++i)d[++cnt_d]=a[i];
	for(int i=pb;i<=cnt_b;++i)d[++cnt_d]=b[i];
	for(int i=pc;i<=cnt_c;++i)d[++cnt_d]=c[i];
	sort(d+1,d+cnt_d+1);
	for(int i=tot+1,j=1;i<=m;++i,++j){
		res+=d[j].fi;
		chosen[i]=d[j].se;
	}
	cout<<res<<endl;
	for(int i=1;i<=m;++i)cout<<chosen[i]<<" ";
	cout<<endl;
	return 0;
}


//韩信带净化，只有你们想不到的，没有毛队做不到的
//problem:CF1374E1
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mk make_pair
#define lob lower_bound
#define upb upper_bound
#define fi first
#define se second
#define SZ(x) ((int)(x).size())

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;

const int MAXN=2e5;
const int MAXT=1e4;
const int INF=2e9+5;
int n,m,K;
pii a[MAXN+5],b[MAXN+5],c[MAXN+5],d[MAXN+5];
int cnt_a,cnt_b,cnt_c,cnt_d,sum_a[MAXN+5],sum_b[MAXN+5],sum_c[MAXN+5],sum_d[MAXN+5];
int chosen[MAXN+5];
struct Book{
	int t;
	bool a,b;
}book[MAXN+5];

int pos_a,pos_b,pos_d;
pii check(int v,int i){
	int num=0;
	int sum=0;
	pos_a=max(0,K-i);pos_b=max(0,K-i);pos_d=0;

	int l=max(0,K-i),r=cnt_a;
	while(l<r){
		int mid=(l+r+1)>>1;
		if(a[mid].fi<=v)l=mid;
		else r=mid-1;
	}
	pos_a=l;
	num+=(l-max(0,K-i));
	sum+=sum_a[l]-sum_a[K-i];

	l=max(0,K-i),r=cnt_b;
	while(l<r){
		int mid=(l+r+1)>>1;
		if(b[mid].fi<=v)l=mid;
		else r=mid-1;
	}
	pos_b=l;
	num+=(l-max(0,K-i));
	sum+=sum_b[l]-sum_b[K-i];

	l=0,r=cnt_d;
	while(l<r){
		int mid=(l+r+1)>>1;
		if(d[mid].fi<=v)l=mid;
		else r=mid-1;
	}
	pos_d=l;
	num+=l;
	sum+=sum_d[l];
	return mk(num,sum);
}

int main() {
	cin>>n>>m>>K;
	for(int i=1;i<=n;++i){
		cin>>book[i].t>>book[i].a>>book[i].b;
		if(book[i].a && book[i].b){
			c[++cnt_c]=mk(book[i].t,i);
		}
		else if(book[i].a){
			a[++cnt_a]=mk(book[i].t,i);
		}
		else if(book[i].b){
			b[++cnt_b]=mk(book[i].t,i);
		}
		else{
			d[++cnt_d]=mk(book[i].t,i);
		}
	}
	sort(a+1,a+cnt_a+1);
	sort(b+1,b+cnt_b+1);
	sort(c+1,c+cnt_c+1);
	sort(d+1,d+cnt_d+1);
	for(int i=1;i<=cnt_a;++i)sum_a[i]=sum_a[i-1]+a[i].fi;
	for(int i=1;i<=cnt_b;++i)sum_b[i]=sum_b[i-1]+b[i].fi;
	for(int i=1;i<=cnt_c;++i)sum_c[i]=sum_c[i-1]+c[i].fi;
	for(int i=1;i<=cnt_d;++i)sum_d[i]=sum_d[i-1]+d[i].fi;

	int ans=INF;
	int ansi=-1,ansv=0;
	for(int i=0;i<=K && i<=cnt_c;++i){
		if(K-i>cnt_a||K-i>cnt_b)continue;
		int curans=sum_c[i]+sum_a[K-i]+sum_b[K-i];
		int rest=m-i-(K-i)*2;
		if(rest<0)continue;
		if(rest==0){
			if(curans<ans){
				ans=curans;
				ansi=i;
				ansv=0;
			}
			continue;
		}
		int l=0,r=MAXT+1;
		while(l<r){
			int mid=(l+r)>>1;
			int num=check(mid,i).fi;
			if(num<rest)l=mid+1;
			else r=mid;
		}
		if(l>MAXT)continue;
		pii res=check(l,i);
		assert(res.fi>=rest);
		curans+=res.se-(res.fi-rest)*l;
		if(curans<ans){
			ans=curans;
			ansi=i;
			ansv=l;
		}
	}
	for(int i=K+1;i<=m && i<=cnt_c;++i){
		if(K-i>cnt_a||K-i>cnt_b)continue;
		int curans=sum_c[i];
		int rest=m-i;
		if(rest==0){
			if(curans<ans){
				ans=curans;
				ansi=i;
				ansv=0;
			}
			continue;
		}
		int l=0,r=MAXT+1;
		while(l<r){
			int mid=(l+r)>>1;
			int num=check(mid,i).fi;
			if(num<rest)l=mid+1;
			else r=mid;
		}
		if(l>MAXT)continue;
		pii res=check(l,i);
		assert(res.fi>=rest);
		curans+=res.se-(res.fi-rest)*l;
		if(curans<ans){
			ans=curans;
			ansi=i;
			ansv=l;
		}
	}
	if(ansi==-1){cout<<-1<<endl;return 0;}
	cout<<ans<<endl;
	if(ansi<=K){
		for(int j=1;j<=ansi;++j)cout<<c[j].se<<" ";
		for(int j=1;j<=K-ansi;++j)cout<<a[j].se<<" ";
		for(int j=1;j<=K-ansi;++j)cout<<b[j].se<<" ";
		int rest=m-ansi-(K-ansi)*2;
		assert(rest>=0);
		if(rest==0)return 0;
		pii res=check(ansv,ansi);
		int t=res.fi-rest;
		while(t && pos_a>K-ansi && a[pos_a].fi==ansv){--t;--pos_a;}
		while(t && pos_b>K-ansi && b[pos_b].fi==ansv){--t;--pos_b;}
		while(t && pos_a>0 && d[pos_d].fi==ansv){--t;--pos_d;}
		assert(t==0);
		for(int j=K-ansi+1;j<=pos_a;++j)cout<<a[j].se<<" ";
		for(int j=K-ansi+1;j<=pos_b;++j)cout<<b[j].se<<" ";
		for(int j=1;j<=pos_d;++j)cout<<d[j].se<<" ";
	}
//	else{
//		for(int j=1;j<=ansi;++j)cout<<c[j].se<<" ";
//		int rest=m-ansi;
//		if(rest==0)return 0;
//		pii res=check(ansv,ansi);
//		int t=res.fi-rest;
//		while(t && pos_a>K-ansi && a[pos_a].fi==ansv){--t;--pos_a;}
//		while(t && pos_b>K-ansi && b[pos_b].fi==ansv){--t;--pos_b;}
//		while(t && pos_a>0 && d[pos_d].fi==ansv){--t;--pos_d;}
//		assert(t==0);
//		for(int j=K-ansi+1;j<=pos_a;++j)cout<<a[j].se<<" ";
//		for(int j=K-ansi+1;j<=pos_b;++j)cout<<b[j].se<<" ";
//		for(int j=1;j<=pos_d;++j)cout<<d[j].se<<" ";
//	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N=2e5+5;
struct vv{
    int id,t;
    bool operator<(const vv &b)const{
        return b.t==t?id<b.id:t<b.t;
    }
}a[4][N];
int k,m,n;
int need;
int num[4];//4个id的指针.
set<vv>all,choose;//确实没有这个实力写这种模拟题...
int sum[4][N];
int cnt=0;
void range(int x)
{
    if(x<=k)//A B会改变.
    {
        need=m-x-(2*(k-x));
        if(k-x+1<=num[1]) all.insert(a[1][k-x+1]);
        if(k-x+1<=num[2]) all.insert(a[2][k-x+1]);
    }else need=m-x;
}

void upch()
{
    if(need<0) need=0;
    set<vv>::iterator it;
    while(choose.size()>need) it=--choose.end(),cnt-=it->t,all.insert(*it),choose.erase(it);
    while(choose.size()<need&&all.size())   cnt+=(it=all.begin())->t,choose.insert(*it),all.erase(it);
    while(choose.size()&&all.size()&&(it=--choose.end())->t>all.begin()->t)
    {
        cnt+=all.begin()->t;cnt-=(it=--choose.end())->t;
        all.insert(*it);choose.erase(it);
        choose.insert(*all.begin());all.erase(all.begin());
    }
}

int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1;i<=n;i++)
    {
        int v,x,y;
        scanf("%d%d%d",&v,&x,&y);
        int p=x+2*y;
        a[p][++num[p]].id=i;
        a[p][num[p]].t=v;
    }
    for(int i=0;i<4;i++) sort(a[i]+1,a[i]+1+num[i]);
    for(int i=0;i<4;i++)
    {
        for(int j=1;j<=num[i];j++)
        {
            sum[i][j]+=sum[i][j-1]+a[i][j].t;
        }
    }
    int start=0;
    while(start<=num[3]&&((num[1]+start<k)||(num[2]+start<k)||(m-start-2*(k-start)<0))) start++;//A B要取k 加起来得有m
    if(start==num[3]+1) { puts("-1"); return 0; }
    //预处理...xswl这tm才预处理...
    //枚举1 1 all是所有可选的 choose是已经选了的
    long long ans=1e16,pos=-1;
    for(int i=0;i<3;i++)
    {
        for(int j=num[i];j>(i==0?0:k-start);j--)
            all.insert(a[i][j]);
    }
    for(int i=start;i<=num[3];i++)
    {
        range(i);upch();
        long long res=sum[3][i]+cnt;
        if(i<=k) res+=sum[1][k-i]+sum[2][k-i];
        if(res<ans&&((k-i>=0)?(i+2*(k-i)+choose.size()==m):(i+choose.size()==m)))
        {
            ans=res;
            pos=i;
            //cout<<ans<<' '<<pos<<endl;
        }
    }

    //cout<<pos<<endl;
    printf("%lld\n",ans);
    all.clear();choose.clear();
    for(int i=0;i<3;i++)
    {
        for(int j=num[i];j>(i==0?0:k-start);j--)
            all.insert(a[i][j]);
    }
    for(int i=start;i<=num[3];i++)
    {
        range(i);upch();
        if(i==pos)
        {
            for(int j=1;j<=i;j++) printf("%d ",a[3][j].id);
            for(int j=1;j<=k-i;j++) printf("%d %d ",a[1][j].id,a[2][j].id);
            for(auto it=choose.begin();it!=choose.end();it++) printf("%d ",it->id);
            puts("");
            return 0;
        }
    }
    puts("-1");
    return 0;
}
/*
#include <cstdio>
#include <set>
#include <algorithm>
using namespace std;
template <typename T>
void read(T &r)
{
    static char c; r = 0;
    for (c = getchar(); c > '9' || c < '0'; c = getchar());
    for (; c >= '0' && c <= '9'; r = (r << 1) + (r << 3) + (c ^ 48), c = getchar());
}
const int maxn = 2e5 + 100;
int n, m, k;
struct node
{
    int t, id;
    bool operator<(const node &b) const { return t == b.t ? id < b.id : t < b.t; }
} A[4][maxn];
int num[4];
long long sums[4][maxn];
inline void init()
{
    read(n), read(m), read(k);
    for (int i = 1; i <= n; ++i)
    {
        int t, a, b, p;
        read(t), read(a), read(b), p = a * 2 + b;
        A[p][++num[p]].id = i, A[p][num[p]].t = t;
    }
    for (int i = 0; i < 4; ++i) sort(A[i] + 1, A[i] + 1 + num[i]);
    for (int i = 0; i < 4; ++i)  for (int j = 1; j <= num[i]; ++j) sums[i][j] = sums[i][j - 1] + A[i][j].t;
}
set<node> all, choose;
int sum, need;
inline void updateChoose()
{
    if (need < 0) need = 0;
    set<node>::iterator it;
    while (choose.size() > need) it = --choose.end(), sum -= it->t,all.insert(*it), choose.erase(it);
    while (choose.size() < need && !all.empty()) sum += (it = all.begin())->t,choose.insert(*it), all.erase(it);
    while (!choose.empty() && !all.empty() && (it = --choose.end())->t > all.begin()->t)
    {
        sum -= it->t, sum += all.begin()->t;
        all.insert(*it), choose.erase(it);
        choose.insert(*all.begin()), all.erase(all.begin());
    }
}
inline void updateRange(int i)
{
    if(k - i >= 0)
    {
        need = m - k * 2 + i;
        if(k - i + 1 <= num[1]) all.insert(A[1][k - i + 1]);
        if(k - i + 1 <= num[2]) all.insert(A[2][k - i + 1]);
    }
    else need = m - i;
}
int main()
{
    init();
    int start = 0;
    while (start <= num[3] && (k - start > num[1] || k - start > num[2] || m - start - (k - start) * 2 < 0)) ++start;
    if (start == num[3] + 1) { puts("-1"); return 0; }
    for (int i = 0; i < 3; ++i) for (int j = num[i]; j > (i == 0 ? 0 : k - start); --j) all.insert(A[i][j]);
    long long ans = 1ll << 60;
    int ansp = -1;
    for (int i = start; i <= num[3]; ++i)
    {
        updateRange(i),updateChoose();
        int res = sums[3][i] + sum;
        if (k - i >= 0) res += sums[1][k - i] + sums[2][k - i];
        if (res < ans && ((k - i >= 0) ? (i + 2 * (k - i) + choose.size() == m) : (i + choose.size() == m))) ans = res, ansp = i;
    }
    printf("%lld\n", ans);
    all.clear(), choose.clear();
    for (int i = 0; i < 3; ++i) for (int j = num[i]; j > (i == 0 ? 0 : k - start); --j) all.insert(A[i][j]);
    for (int i = start; i <= num[3]; ++i)
    {
        updateRange(i),updateChoose();
        if (i == ansp)
        {
            for (int j = 1; j <= i; ++j) printf("%d ", A[3][j].id);
            if (k - i > 0) for (int j = 1; j <= k - i; ++j) printf("%d %d ", A[1][j].id, A[2][j].id);
            for (set<node>::iterator it = choose.begin(); it != choose.end(); ++it) printf("%d ", it->id);
            return 0;
        }
    }
    puts("-1");
    return 0;
}
*/


#include<algorithm>
#include<iostream>
#include<vector>
#include<deque>
using namespace std;
struct ppap
{
    int d,a,b,h;
    bool operator< (const ppap &y) const
	{
        return d<y.d;
    }
}a[5000010];
deque<ppap> s[5],K[5];
vector<int> P,M;
long long n,m,k,ans=0x7fffffffffffffff,sum,v[5000010];
int main()
{
    cin>>n>>m>>k;
	if(n<=1e6) for(int i=1;i<=n;i++) cin>>a[i].d>>a[i].a>>a[i].b,a[i].h=i;
	else{
		long long seed,c1,c2,c3,l,r,temp1,temp2,mod;
		cin>>seed>>c1>>c2>>c3>>l>>r,temp1=seed,mod=r-l+1;
		for(int i=1;i<=n;i++) temp2=(temp1*temp1%mod*c1%mod+temp1*c2%mod+c3)%mod+l,a[i].d=temp2,a[i].a=((temp1+temp2+c3)%3>0),a[i].b=((temp1+temp2+c2)%3>0),temp1=temp2;
	}
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++){
        int flag=0;
        if(a[i].a) flag+=1;
        if(a[i].b) flag+=2;
        K[flag].push_back(a[i]);
    }
    int mins=min(K[1].size(),K[2].size());
    if(K[3].size()+min(1ll*mins,(m-K[3].size())/2)<k){cout<<-1;return 0;}
    while(s[3].size()+min(1ll*mins,(m-s[3].size())/2)<k||s[3].size()+K[1].size()+K[2].size()+K[0].size()<m) s[3].push_back(K[3].front()),sum+=K[3].front().d,v[K[3].front().h]=1,K[3].pop_front();
    int ned=k-s[3].size();
    for(int i=1;i<=ned;i++) s[2].push_back(K[2].front()),sum+=K[2].front().d,v[K[2].front().h]=1,K[2].pop_front(),s[1].push_back(K[1].front()),sum+=K[1].front().d,v[K[1].front().h]=1,K[1].pop_front();
    int res=m-s[3].size()-max(0ll,2*(k-s[3].size()));
    for(int i=1;i<=res;i++){
        int flag=-1;
        if(K[0].size()) flag=0;
        if(K[1].size()&&(flag==-1||K[flag].front().d>K[1].front().d)) flag=1;
        if(K[2].size()&&(flag==-1||K[flag].front().d>K[2].front().d)) flag=2;
        s[flag].push_back(K[flag].front()),sum+=K[flag].front().d,v[K[flag].front().h]=1,K[flag].pop_front();
    }
    ans=sum;
    while(K[3].size()&&s[3].size()<m){
        P.clear(),M.clear(),sum=ans,s[3].push_back(K[3].front()),P.push_back(K[3].front().h),sum+=K[3].front().d,K[3].pop_front();
        int cnt=0;
        if(s[2].size()) K[2].push_front(s[2].back()),M.push_back(s[2].back().h),sum-=s[2].back().d,s[2].pop_back(),cnt++;
        if(s[1].size()) K[1].push_front(s[1].back()),M.push_back(s[1].back().h),sum-=s[1].back().d,s[1].pop_back(),cnt++;
        if(s[0].size()) K[0].push_front(s[0].back()),M.push_back(s[0].back().h),sum-=s[0].back().d,s[0].pop_back(),cnt++;
        while(cnt>1){
            int flag=-1;
            if(K[0].size()) flag=0;
            if(K[1].size()&&(flag==-1||K[flag].front().d>K[1].front().d)) flag=1;
            if(K[2].size()&&(flag==-1||K[flag].front().d>K[2].front().d)) flag=2;
            s[flag].push_back(K[flag].front()),sum+=K[flag].front().d,P.push_back(K[flag].front().h),K[flag].pop_front(),cnt--;
        }
        if(sum<ans){
        	int psize=P.size(),msize=M.size();
            for(int i=0;i<psize;i++) v[P[i]]++;
            for(int i=0;i<msize;i++) v[M[i]]--;
            ans=sum;
        }
    }
    cout<<ans<<endl;
    if(n<=1e6) for(int i=1;i<=n;i++) if(v[i]) cout<<i<<" ";
}


#include<algorithm>
#include<iostream>
#include<vector>
#include<deque>
using namespace std;
struct ppap
{
    int d,a,b,h;
    bool operator< (const ppap &y) const
	{
        return d<y.d;
    }
}a[5000010];
deque<ppap> s[5],K[5];
vector<int> P,M;
long long n,m,k,ans=0x7fffffffffffffff,sum,v[5000010];
int main()
{
    cin>>n>>m>>k;
	if(n<=1e6) for(int i=1;i<=n;i++) cin>>a[i].d>>a[i].a>>a[i].b,a[i].h=i;
	else{
		long long seed,c1,c2,c3,l,r,temp1,temp2,mod;
		cin>>seed>>c1>>c2>>c3>>l>>r,temp1=seed,mod=r-l+1;
		for(int i=1;i<=n;i++) temp2=(temp1*temp1%mod*c1%mod+temp1*c2%mod+c3)%mod+l,a[i].d=temp2,a[i].a=((temp1+temp2+c3)%3>0),a[i].b=((temp1+temp2+c2)%3>0),temp1=temp2;
	}
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++){
        int flag=0;
        if(a[i].a) flag+=1;
        if(a[i].b) flag+=2;
        K[flag].push_back(a[i]);
    }
    int mins=min(K[1].size(),K[2].size());
    if(K[3].size()+min(1ll*mins,(m-K[3].size())/2)<k){cout<<-1;return 0;}
    while(s[3].size()+min(1ll*mins,(m-s[3].size())/2)<k||s[3].size()+K[1].size()+K[2].size()+K[0].size()<m) s[3].push_back(K[3].front()),sum+=K[3].front().d,v[K[3].front().h]=1,K[3].pop_front();
    int ned=k-s[3].size();
    for(int i=1;i<=ned;i++) s[2].push_back(K[2].front()),sum+=K[2].front().d,v[K[2].front().h]=1,K[2].pop_front(),s[1].push_back(K[1].front()),sum+=K[1].front().d,v[K[1].front().h]=1,K[1].pop_front();
    int res=m-s[3].size()-max(0ll,2*(k-s[3].size()));
    for(int i=1;i<=res;i++){
        int flag=-1;
        if(K[0].size()) flag=0;
        if(K[1].size()&&(flag==-1||K[flag].front().d>K[1].front().d)) flag=1;
        if(K[2].size()&&(flag==-1||K[flag].front().d>K[2].front().d)) flag=2;
        s[flag].push_back(K[flag].front()),sum+=K[flag].front().d,v[K[flag].front().h]=1,K[flag].pop_front();
    }
    ans=sum;
    while(K[3].size()&&s[3].size()<m){
        P.clear(),M.clear(),sum=ans,s[3].push_back(K[3].front()),P.push_back(K[3].front().h),sum+=K[3].front().d,K[3].pop_front();
        int cnt=0;
        if(s[2].size()) K[2].push_front(s[2].back()),M.push_back(s[2].back().h),sum-=s[2].back().d,s[2].pop_back(),cnt++;
        if(s[1].size()) K[1].push_front(s[1].back()),M.push_back(s[1].back().h),sum-=s[1].back().d,s[1].pop_back(),cnt++;
        if(s[0].size()) K[0].push_front(s[0].back()),M.push_back(s[0].back().h),sum-=s[0].back().d,s[0].pop_back(),cnt++;
        while(cnt>1){
            int flag=-1;
            if(K[0].size()) flag=0;
            if(K[1].size()&&(flag==-1||K[flag].front().d>K[1].front().d)) flag=1;
            if(K[2].size()&&(flag==-1||K[flag].front().d>K[2].front().d)) flag=2;
            s[flag].push_back(K[flag].front()),sum+=K[flag].front().d,P.push_back(K[flag].front().h),K[flag].pop_front(),cnt--;
        }
        if(sum<ans){
        	int psize=P.size(),msize=M.size();
            for(int i=0;i<psize;i++) v[i]++;
            for(int i=0;i<msize;i++) v[i]--;
            ans=sum;
        }
    }
    cout<<ans<<endl;
    if(n<=1e6) for(int i=1;i<=n;i++) if(v[i]) cout<<i<<" ";
}


#include <bits/stdc++.h>

#define fi first
#define se second
#define db double
#define U unsigned
#define P std::pair<LL,LL>
#define LL long long
#define pb push_back
#define MP std::make_pair
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl
#define int LL
const int MAXN = 4e5+ 5;

LL sm[MAXN<<2];int sz[MAXN<<2];
#define lc ((x)<<1)
#define rc ((x)<<1|1)

inline void update(int x,int l,int r,int p,int d){
    if(l == r){
        sm[x] += d*l;
        sz[x] += d;
        return;
    }
    int mid = (l + r) >> 1;
    if(p <= mid) update(lc,l,mid,p,d);
    else update(rc,mid+1,r,p,d);
    sm[x] = sm[lc]+sm[rc];
    sz[x] = sz[lc]+sz[rc];
}

inline LL query(int x,int l,int r,int k){
    if(l == r) return 1ll*std::min(k,sz[x])*l;
    int mid = (l + r) >> 1;
    int ans = 0;
    if(sz[lc] < k) return sm[lc]+query(rc,mid+1,r,k-sz[lc]);
    else if(sz[lc] == k) return sm[lc];
    else return query(lc,l,mid,k);
}

int m;
std::vector<LL> S1,S2,S3,S4;
int t[MAXN],x[MAXN],y[MAXN];
int M = 1e4;

inline void Solve(){
    int n,k;scanf("%lld%lld%lld",&n,&m,&k);
    S1.pb(0);S2.pb(0);S3.pb(0);
    FOR(i,1,n){
        int t,x,y;scanf("%lld%lld%lld",&t,&x,&y);
        ::t[i] = t;::x[i] = x;::y[i] = y;
        if(x == 1 && y == 0) S1.pb(t);
        if(x == 0 && y == 1) S2.pb(t);
        if(x == 1 && y == 1) S3.pb(t),update(1,1,M,t,1);
        if(x == 0 && y == 0) update(1,1,M,t,1);
    }
    std::sort(all(S1));std::sort(all(S2));std::sort(all(S3));
    FOR(i,1,(int)S1.size()-1) S1[i] += S1[i-1];
    FOR(i,1,(int)S2.size()-1) S2[i] += S2[i-1];
    FOR(i,1,(int)S3.size()-1) S3[i] += S3[i-1];
    LL ans = 1e18,ps = -1;
    FOR(i,0,std::min(k,(int)S3.size()-1)){
        int t = k-i,tt = m-k*2+i;
        // cal
        if(tt >= 0 && tt <= sz[1] && t < S1.size() && t < S2.size()){
            LL res = query(1,1,M,tt)+S1[t]+S2[t]+S3[i];
            if(ans > res){
                ans = res;ps = i;
            }
        }
        if(t && t < S1.size()){
            update(1,1,M,S1[t]-S1[t-1],1);
        }
        if(t && t < S2.size()){
            update(1,1,M,S2[t]-S2[t-1],1);
        }
        if(i+1 < S3.size()) update(1,1,M,S3[i+1]-S3[i],-1);
    }
    if(ans == 1e18){
        puts("-1");return;
    }
    printf("%lld\n",ans);LL smm = 0;
    int t = k-ps,tt = m-k*2+ps;
    std::vector<P> S,ext;
    FOR(i,1,n) if(x[i] == 1 && y[i] == 0) S.pb(MP(::t[i],i));
    std::sort(all(S));
    FOR(i,0,t-1) printf("%lld ",S[i].se),smm += S[i].fi;
    FOR(i,t,(int)S.size()-1) ext.pb(S[i]);S.clear();
    FOR(i,1,n) if(x[i] == 0 && y[i] == 1) S.pb(MP(::t[i],i));
    std::sort(all(S));
    FOR(i,0,t-1) printf("%lld ",S[i].se),smm += S[i].fi;
    FOR(i,t,(int)S.size()-1) ext.pb(S[i]);S.clear();
    FOR(i,1,n) if(x[i] == 0 && y[i] == 0) ext.pb(MP(::t[i],i));
    FOR(i,1,n) if(x[i] == 1 && y[i] == 1) S.pb(MP(::t[i],i));
    std::sort(all(S));
    FOR(i,0,ps-1) printf("%lld ",S[i].se),smm += S[i].fi;
    FOR(i,ps,(int)S.size()-1) ext.pb(S[i]);
    std::sort(all(ext));
    FOR(i,0,tt-1) printf("%lld ",ext[i].se),smm += ext[i].fi;
    puts("");assert(smm==ans);
}

signed main(){
    Solve();
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cstdio>
#include <bitset>
#include <vector>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <map>
#include <set>

#define fi first
#define se second
#define db double
#define U unsigned
#define P std::pair<int,int>
#define LL long long
#define pb push_back
#define MP std::make_pair
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl

const int MAXN = 2e5+ 5;

LL sm[MAXN<<2];int sz[MAXN<<2];
#define lc ((x)<<1)
#define rc ((x)<<1|1)

inline void update(int x,int l,int r,int p,int d){
    if(l == r){
        sm[x] += d*l;
        sz[x] += d;
        return;
    }
    int mid = (l + r) >> 1;
    if(p <= mid) update(lc,l,mid,p,d);
    else update(rc,mid+1,r,p,d);
    sm[x] = sm[lc]+sm[rc];
    sz[x] = sz[lc]+sz[rc];
}

inline LL query(int x,int l,int r,int k){
    if(l == r) return 1ll*std::min(k,sz[x])*l;
    int mid = (l + r) >> 1;
    int ans = 0;
    if(sz[lc] < k) return sm[lc]+query(rc,mid+1,r,k-sz[lc]);
    else if(sz[lc] == k) return sm[lc];
    else return query(lc,l,mid,k);
}

int m;
std::vector<LL> S1,S2,S3,S4;
int t[MAXN],x[MAXN],y[MAXN];
int M = 1e4;

inline void Solve(){
    int n,k;scanf("%d%d%d",&n,&m,&k);
    S1.pb(0);S2.pb(0);S3.pb(0);
    FOR(i,1,n){
        int t,x,y;scanf("%d%d%d",&t,&x,&y);
        ::t[i] = t;::x[i] = x;::y[i] = y;
        if(x == 1 && y == 0) S1.pb(t);
        if(x == 0 && y == 1) S2.pb(t);
        if(x == 1 && y == 1) S3.pb(t),update(1,1,M,t,1);
        if(x == 0 && y == 0) update(1,1,M,t,1);
    }
    std::sort(all(S1));std::sort(all(S2));std::sort(all(S3));
    FOR(i,1,(int)S1.size()-1) S1[i] += S1[i-1];
    FOR(i,1,(int)S2.size()-1) S2[i] += S2[i-1];
    FOR(i,1,(int)S3.size()-1) S3[i] += S3[i-1];
    LL ans = 1e18,ps = -1;
    FOR(i,0,std::min(k,(int)S3.size()-1)){
        int t = k-i,tt = m-k*2+i;
        // cal
        if(tt >= 0 && tt <= sz[1] && t < S1.size() && t < S2.size()){
            LL res = query(1,1,M,tt)+S1[t]+S2[t]+S3[i];
            if(ans > res){
                ans = res;ps = i;
            }
        }
        if(t && t < S1.size()){
            update(1,1,M,S1[t]-S1[t-1],1);
        }
        if(t && t < S2.size()){
            update(1,1,M,S2[t]-S2[t-1],1);
        }
        if(i+1 < S3.size()) update(1,1,M,S3[i+1]-S3[i],-1);
    }
    if(ans == 1e18){
        puts("-1");return;
    }
    printf("%d\n",ans);
    int t = k-ps,tt = m-k*2+ps;
    std::vector<P> S,ext;
    FOR(i,1,n) if(x[i] == 1 && y[i] == 0) S.pb(MP(::t[i],i));
    std::sort(all(S));
    FOR(i,0,t-1) printf("%d ",S[i].se);
    FOR(i,t,(int)S.size()-1) ext.pb(S[i]);S.clear();
    FOR(i,1,n) if(x[i] == 0 && y[i] == 1) S.pb(MP(::t[i],i));
    std::sort(all(S));
    FOR(i,0,t-1) printf("%d ",S[i].se);
    FOR(i,t,(int)S.size()-1) ext.pb(S[i]);S.clear();
    FOR(i,1,n) if(x[i] == 0 && y[i] == 0) ext.pb(MP(::t[i],i));
    FOR(i,1,n) if(x[i] == 1 && y[i] == 1) S.pb(MP(::t[i],i));
    std::sort(all(S));
    FOR(i,0,ps-1) printf("%d ",S[i].se);
    FOR(i,ps,(int)S.size()-1) ext.pb(S[i]);
    std::sort(all(ext));
    FOR(i,0,tt-1) printf("%d ",ext[i].se);puts("");
}

int main(){
    Solve();
    return 0;
}


#include <bits/stdc++.h>

#define fi first
#define se second
#define db double
#define U unsigned
#define P std::pair<LL,LL>
#define LL long long
#define pb push_back
#define MP std::make_pair
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl
#define int LL
const int MAXN = 2e5+ 5;

LL sm[MAXN<<2];int sz[MAXN<<2];
#define lc ((x)<<1)
#define rc ((x)<<1|1)

inline void update(int x,int l,int r,int p,int d){
    if(l == r){
        sm[x] += d*l;
        sz[x] += d;
        return;
    }
    int mid = (l + r) >> 1;
    if(p <= mid) update(lc,l,mid,p,d);
    else update(rc,mid+1,r,p,d);
    sm[x] = sm[lc]+sm[rc];
    sz[x] = sz[lc]+sz[rc];
}

inline LL query(int x,int l,int r,int k){
    if(l == r) return 1ll*std::min(k,sz[x])*l;
    int mid = (l + r) >> 1;
    int ans = 0;
    if(sz[lc] < k) return sm[lc]+query(rc,mid+1,r,k-sz[lc]);
    else if(sz[lc] == k) return sm[lc];
    else return query(lc,l,mid,k);
}

int m;
std::vector<LL> S1,S2,S3,S4;
int t[MAXN],x[MAXN],y[MAXN];
int M = 1e4;

inline void Solve(){
    int n,k;scanf("%lld%lld%lld",&n,&m,&k);
    S1.pb(0);S2.pb(0);S3.pb(0);
    FOR(i,1,n){
        int t,x,y;scanf("%lld%lld%lld",&t,&x,&y);
        ::t[i] = t;::x[i] = x;::y[i] = y;
        if(x == 1 && y == 0) S1.pb(t);
        if(x == 0 && y == 1) S2.pb(t);
        if(x == 1 && y == 1) S3.pb(t),update(1,1,M,t,1);
        if(x == 0 && y == 0) update(1,1,M,t,1);
    }
    std::sort(all(S1));std::sort(all(S2));std::sort(all(S3));
    FOR(i,1,(int)S1.size()-1) S1[i] += S1[i-1];
    FOR(i,1,(int)S2.size()-1) S2[i] += S2[i-1];
    FOR(i,1,(int)S3.size()-1) S3[i] += S3[i-1];
    LL ans = 1e18,ps = -1;
    FOR(i,0,std::min(k,(int)S3.size()-1)){
        int t = k-i,tt = m-k*2+i;
        // cal
        if(tt >= 0 && tt <= sz[1] && t < S1.size() && t < S2.size()){
            LL res = query(1,1,M,tt)+S1[t]+S2[t]+S3[i];
            if(ans > res){
                ans = res;ps = i;
            }
        }
        if(t && t < S1.size()){
            update(1,1,M,S1[t]-S1[t-1],1);
        }
        if(t && t < S2.size()){
            update(1,1,M,S2[t]-S2[t-1],1);
        }
        if(i+1 < S3.size()) update(1,1,M,S3[i+1]-S3[i],-1);
    }
    if(ans == 1e18){
        puts("-1");return;
    }
    printf("%lld\n",ans);LL smm = 0;
    int t = k-ps,tt = m-k*2+ps;
    std::vector<P> S,ext;
    FOR(i,1,n) if(x[i] == 1 && y[i] == 0) S.pb(MP(::t[i],i));
    std::sort(all(S));
    FOR(i,0,t-1) printf("%lld ",S[i].se),smm += S[i].fi;
    FOR(i,t,(int)S.size()-1) ext.pb(S[i]);S.clear();
    FOR(i,1,n) if(x[i] == 0 && y[i] == 1) S.pb(MP(::t[i],i));
    std::sort(all(S));
    FOR(i,0,t-1) printf("%lld ",S[i].se),smm += S[i].fi;
    FOR(i,t,(int)S.size()-1) ext.pb(S[i]);S.clear();
    FOR(i,1,n) if(x[i] == 0 && y[i] == 0) ext.pb(MP(::t[i],i));
    FOR(i,1,n) if(x[i] == 1 && y[i] == 1) S.pb(MP(::t[i],i));
    std::sort(all(S));
    FOR(i,0,ps-1) printf("%lld ",S[i].se),smm += S[i].fi;
    FOR(i,ps,(int)S.size()-1) ext.pb(S[i]);
    std::sort(all(ext));
    FOR(i,0,tt-1) printf("%lld ",ext[i].se),smm += ext[i].fi;
    puts("");assert(smm==ans);
}

signed main(){
    Solve();
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cstdio>
#include <bitset>
#include <vector>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <map>
#include <set>

#define fi first
#define se second
#define db double
#define U unsigned
#define P std::pair<LL,LL>
#define LL long long
#define pb push_back
#define MP std::make_pair
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl
#define int LL
const int MAXN = 2e5+ 5;

LL sm[MAXN<<2];int sz[MAXN<<2];
#define lc ((x)<<1)
#define rc ((x)<<1|1)

inline void update(int x,int l,int r,int p,int d){
    if(l == r){
        sm[x] += d*l;
        sz[x] += d;
        return;
    }
    int mid = (l + r) >> 1;
    if(p <= mid) update(lc,l,mid,p,d);
    else update(rc,mid+1,r,p,d);
    sm[x] = sm[lc]+sm[rc];
    sz[x] = sz[lc]+sz[rc];
}

inline LL query(int x,int l,int r,int k){
    if(l == r) return 1ll*std::min(k,sz[x])*l;
    int mid = (l + r) >> 1;
    int ans = 0;
    if(sz[lc] < k) return sm[lc]+query(rc,mid+1,r,k-sz[lc]);
    else if(sz[lc] == k) return sm[lc];
    else return query(lc,l,mid,k);
}

int m;
std::vector<LL> S1,S2,S3,S4;
int t[MAXN],x[MAXN],y[MAXN];
int M = 1e4;

inline void Solve(){
    int n,k;scanf("%lld%lld%lld",&n,&m,&k);
    S1.pb(0);S2.pb(0);S3.pb(0);
    FOR(i,1,n){
        int t,x,y;scanf("%lld%lld%lld",&t,&x,&y);
        ::t[i] = t;::x[i] = x;::y[i] = y;
        if(x == 1 && y == 0) S1.pb(t);
        if(x == 0 && y == 1) S2.pb(t);
        if(x == 1 && y == 1) S3.pb(t),update(1,1,M,t,1);
        if(x == 0 && y == 0) update(1,1,M,t,1);
    }
    std::sort(all(S1));std::sort(all(S2));std::sort(all(S3));
    FOR(i,1,(int)S1.size()-1) S1[i] += S1[i-1];
    FOR(i,1,(int)S2.size()-1) S2[i] += S2[i-1];
    FOR(i,1,(int)S3.size()-1) S3[i] += S3[i-1];
    LL ans = 1e18,ps = -1;
    FOR(i,0,std::min(k,(int)S3.size()-1)){
        int t = k-i,tt = m-k*2+i;
        // cal
        if(tt >= 0 && tt <= sz[1] && t < S1.size() && t < S2.size()){
            LL res = query(1,1,M,tt)+S1[t]+S2[t]+S3[i];
            if(ans > res){
                ans = res;ps = i;
            }
        }
        if(t && t < S1.size()){
            update(1,1,M,S1[t]-S1[t-1],1);
        }
        if(t && t < S2.size()){
            update(1,1,M,S2[t]-S2[t-1],1);
        }
        if(i+1 < S3.size()) update(1,1,M,S3[i+1]-S3[i],-1);
    }
    if(ans == 1e18){
        puts("-1");return;
    }
    printf("%lld\n",ans);
    int t = k-ps,tt = m-k*2+ps;
    std::vector<P> S,ext;
    FOR(i,1,n) if(x[i] == 1 && y[i] == 0) S.pb(MP(::t[i],i));
    std::sort(all(S));
    FOR(i,0,t-1) printf("%lld ",S[i].se);
    FOR(i,t,(int)S.size()-1) ext.pb(S[i]);S.clear();
    FOR(i,1,n) if(x[i] == 0 && y[i] == 1) S.pb(MP(::t[i],i));
    std::sort(all(S));
    FOR(i,0,t-1) printf("%lld ",S[i].se);
    FOR(i,t,(int)S.size()-1) ext.pb(S[i]);S.clear();
    FOR(i,1,n) if(x[i] == 0 && y[i] == 0) ext.pb(MP(::t[i],i));
    FOR(i,1,n) if(x[i] == 1 && y[i] == 1) S.pb(MP(::t[i],i));
    std::sort(all(S));
    FOR(i,0,ps-1) printf("%lld ",S[i].se);
    FOR(i,ps,(int)S.size()-1) ext.pb(S[i]);
    std::sort(all(ext));
    FOR(i,0,tt-1) printf("%lld ",ext[i].se);puts("");
}

signed main(){
    Solve();
    return 0;
}


#include <bits/stdc++.h>

#define fi first
#define se second
#define db double
#define U unsigned
#define P std::pair<LL,LL>
#define LL long long
#define pb push_back
#define MP std::make_pair
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl
#define int LL
const int MAXN = 4e5+ 5;

LL sm[MAXN<<2];int sz[MAXN<<2];
#define lc ((x)<<1)
#define rc ((x)<<1|1)

inline void update(int x,int l,int r,int p,int d){
    if(l == r){
        sm[x] += d*l;
        sz[x] += d;
        return;
    }
    int mid = (l + r) >> 1;
    if(p <= mid) update(lc,l,mid,p,d);
    else update(rc,mid+1,r,p,d);
    sm[x] = sm[lc]+sm[rc];
    sz[x] = sz[lc]+sz[rc];
}

inline LL query(int x,int l,int r,int k){
    if(l == r) return 1ll*std::min(k,sz[x])*l;
    int mid = (l + r) >> 1;
    int ans = 0;
    if(sz[lc] < k) return sm[lc]+query(rc,mid+1,r,k-sz[lc]);
    else if(sz[lc] == k) return sm[lc];
    else return query(lc,l,mid,k);
}

int m;
std::vector<LL> S1,S2,S3,S4;
int t[MAXN],x[MAXN],y[MAXN];
int M = 1e4;

inline void Solve(){
    int n,k;scanf("%lld%lld%lld",&n,&m,&k);
    S1.pb(0);S2.pb(0);S3.pb(0);
    FOR(i,1,n){
        int t,x,y;scanf("%lld%lld%lld",&t,&x,&y);
        ::t[i] = t;::x[i] = x;::y[i] = y;
        if(x == 1 && y == 0) S1.pb(t);
        if(x == 0 && y == 1) S2.pb(t);
        if(x == 1 && y == 1) S3.pb(t),update(1,1,M,t,1);
        if(x == 0 && y == 0) update(1,1,M,t,1);
    }
    std::sort(all(S1));std::sort(all(S2));std::sort(all(S3));
    FOR(i,1,(int)S1.size()-1) S1[i] += S1[i-1];
    FOR(i,1,(int)S2.size()-1) S2[i] += S2[i-1];
    FOR(i,1,(int)S3.size()-1) S3[i] += S3[i-1];
    FOR(i,k+1,(int)S1.size()-1) update(1,1,M,S1[i]-S1[i-1],1);
    FOR(i,k+1,(int)S2.size()-1) update(1,1,M,S2[i]-S2[i-1],1);
    LL ans = 1e18,ps = -1;
    FOR(i,0,std::min(k,(int)S3.size()-1)){
        int t = k-i,tt = m-k*2+i;
        // cal
        if(tt >= 0 && tt <= sz[1] && t < S1.size() && t < S2.size()){
            if(i == 1){
//                DEBUG(query(1,1,M,tt));
            }
            LL res = query(1,1,M,tt)+S1[t]+S2[t]+S3[i];
            if(ans > res){
                ans = res;ps = i;
            }
        }
        if(t && t < S1.size()){
            update(1,1,M,S1[t]-S1[t-1],1);
        }
        if(t && t < S2.size()){
            update(1,1,M,S2[t]-S2[t-1],1);
        }
        if(i+1 < S3.size()) update(1,1,M,S3[i+1]-S3[i],-1);
    }
    if(ans == 1e18){
        puts("-1");return;
    }
    printf("%lld\n",ans);LL smm = 0;
    int t = k-ps,tt = m-k*2+ps;
    std::vector<P> S,ext;
    FOR(i,1,n) if(x[i] == 1 && y[i] == 0) S.pb(MP(::t[i],i));
    std::sort(all(S));
    FOR(i,0,t-1) printf("%lld ",S[i].se),smm += S[i].fi;
    FOR(i,t,(int)S.size()-1) ext.pb(S[i]);S.clear();
    FOR(i,1,n) if(x[i] == 0 && y[i] == 1) S.pb(MP(::t[i],i));
    std::sort(all(S));
    FOR(i,0,t-1) printf("%lld ",S[i].se),smm += S[i].fi;
    FOR(i,t,(int)S.size()-1) ext.pb(S[i]);S.clear();
    FOR(i,1,n) if(x[i] == 0 && y[i] == 0) ext.pb(MP(::t[i],i));
    FOR(i,1,n) if(x[i] == 1 && y[i] == 1) S.pb(MP(::t[i],i));
    std::sort(all(S));
    FOR(i,0,ps-1) printf("%lld ",S[i].se),smm += S[i].fi;
    FOR(i,ps,(int)S.size()-1) ext.pb(S[i]);
    std::sort(all(ext));
    FOR(i,0,tt-1) printf("%lld ",ext[i].se),smm += ext[i].fi;
    puts("");assert(smm==ans);
}

signed main(){
    Solve();
    return 0;
}


#include <bits/stdc++.h>

#define fi first
#define se second
#define db double
#define U unsigned
#define P std::pair<LL,LL>
#define LL long long
#define pb push_back
#define MP std::make_pair
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl
#define int LL
const int MAXN = 4e5+ 5;

LL sm[MAXN<<2];int sz[MAXN<<2];
#define lc ((x)<<1)
#define rc ((x)<<1|1)

inline void update(int x,int l,int r,int p,int d){
    if(l == r){
        sm[x] += d*l;
        sz[x] += d;
        return;
    }
    int mid = (l + r) >> 1;
    if(p <= mid) update(lc,l,mid,p,d);
    else update(rc,mid+1,r,p,d);
    sm[x] = sm[lc]+sm[rc];
    sz[x] = sz[lc]+sz[rc];
}

inline LL query(int x,int l,int r,int k){
    if(l == r) return 1ll*std::min(k,sz[x])*l;
    int mid = (l + r) >> 1;
    int ans = 0;
    if(sz[lc] < k) return sm[lc]+query(rc,mid+1,r,k-sz[lc]);
    else if(sz[lc] == k) return sm[lc];
    else return query(lc,l,mid,k);
}

int m;
std::vector<LL> S1,S2,S3,S4;
int t[MAXN],x[MAXN],y[MAXN];
int M = 1e4;

inline void Solve(){
    int n,k;scanf("%lld%lld%lld",&n,&m,&k);
    S1.pb(0);S2.pb(0);S3.pb(0);
    FOR(i,1,n){
        int t,x,y;scanf("%lld%lld%lld",&t,&x,&y);
        ::t[i] = t;::x[i] = x;::y[i] = y;
        if(x == 1 && y == 0) S1.pb(t);
        if(x == 0 && y == 1) S2.pb(t);
        if(x == 1 && y == 1) S3.pb(t),update(1,1,M,t,1);
        if(x == 0 && y == 0) update(1,1,M,t,1);
    }
    std::sort(all(S1));std::sort(all(S2));std::sort(all(S3));
    FOR(i,1,(int)S1.size()-1) S1[i] += S1[i-1];
    FOR(i,1,(int)S2.size()-1) S2[i] += S2[i-1];
    FOR(i,1,(int)S3.size()-1) S3[i] += S3[i-1];
    LL ans = 1e18,ps = -1;
    FOR(i,0,std::min(k,(int)S3.size()-1)){
        int t = k-i,tt = m-k*2+i;
        // cal
        if(tt >= 0 && tt <= sz[1] && t < S1.size() && t < S2.size()){
            LL res = query(1,1,M,tt)+S1[t]+S2[t]+S3[i];
            if(ans > res){
                ans = res;ps = i;
            }
        }
        if(t && t < S1.size()){
            update(1,1,M,S1[t]-S1[t-1],1);
        }
        if(t && t < S2.size()){
            update(1,1,M,S2[t]-S2[t-1],1);
        }
        if(i+1 < S3.size()) update(1,1,M,S3[i+1]-S3[i],-1);
    }
    if(ans == 1e18 || ps==-1){
        puts("-1");return;
    }
    printf("%lld\n",ans);LL smm = 0;
    int t = k-ps,tt = m-k*2+ps;
    std::vector<P> S,ext;
    FOR(i,1,n) if(x[i] == 1 && y[i] == 0) S.pb(MP(::t[i],i));
    std::sort(all(S));
    FOR(i,0,t-1) printf("%lld ",S[i].se),smm += S[i].fi;
    FOR(i,t,(int)S.size()-1) ext.pb(S[i]);S.clear();
    FOR(i,1,n) if(x[i] == 0 && y[i] == 1) S.pb(MP(::t[i],i));
    std::sort(all(S));
    FOR(i,0,t-1) printf("%lld ",S[i].se),smm += S[i].fi;
    FOR(i,t,(int)S.size()-1) ext.pb(S[i]);S.clear();
    FOR(i,1,n) if(x[i] == 0 && y[i] == 0) ext.pb(MP(::t[i],i));
    FOR(i,1,n) if(x[i] == 1 && y[i] == 1) S.pb(MP(::t[i],i));
    std::sort(all(S));
    FOR(i,0,ps-1) printf("%lld ",S[i].se),smm += S[i].fi;
    FOR(i,ps,(int)S.size()-1) ext.pb(S[i]);
    std::sort(all(ext));
    FOR(i,0,tt-1) printf("%lld ",ext[i].se),smm += ext[i].fi;
    puts("");//assert(smm==ans);
}

signed main(){
    Solve();
    return 0;
}


// Author -- Frame

#include<bits/stdc++.h>

#define lowbit(x) ((x)&(-x))
#define Finline __inline__ __attribute__ ((always_inline))
#define DEBUG fprintf(stderr,"Running on Line %d in Function %s\n",__LINE__,__FUNCTION__)
#define SZ(x) ((int)x.size())
#define mkpr std::make_pair
#define pb push_back

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef std::pair<int,int> pi;
typedef std::pair<ll,ll> pl;

const int inf=0x3f3f3f3f,Inf=0x7fffffff;
const ll INF=0x3f3f3f3f3f3f3f3f;

Finline uint rnd()
{
	static uint seed=416;
	return seed^=seed>>5,seed^=seed<<17,seed^=seed>>13;
}
template <typename _Tp>_Tp gcd(const _Tp &a,const _Tp &b){return (!b)?a:gcd(b,a%b);}
template <typename _Tp>Finline _Tp abs(const _Tp &a){return a>=0?a:-a;}
template <typename _Tp>Finline _Tp max(const _Tp &a,const _Tp &b){return a<b?b:a;}
template <typename _Tp>Finline _Tp min(const _Tp &a,const _Tp &b){return a<b?a:b;}
template <typename _Tp>Finline void chmax(_Tp &a,const _Tp &b){(a<b)&&(a=b);}
template <typename _Tp>Finline void chmin(_Tp &a,const _Tp &b){(b<a)&&(a=b);}
template <typename _Tp>Finline void read(_Tp &x)
{
	char ch(getchar());
	bool f(false);
	while(ch<48||ch>57) f|=ch==45,ch=getchar();
	x=ch&15,ch=getchar();
	while(ch>=48&&ch<=57) x=(((x<<2)+x)<<1)+(ch&15),ch=getchar();
	if(f) x=-x;
}
template <typename _Tp,typename... Args>Finline void read(_Tp &t,Args &...args)
{
	read(t);read(args...);
}
Finline int read_str(char *s)
{
	char ch(getchar());
	while(ch==' '||ch=='\r'||ch=='\n') ch=getchar();
	char *tar=s;
	*tar=ch,ch=getchar();
	while(ch!=' '&&ch!='\r'&&ch!='\n'&&ch!=EOF) *(++tar)=ch,ch=getchar();
	return tar-s+1;
}

const int N=200005;
struct BIT{
	int c[N];
	Finline void add(int x,int C){for(;x<N;x+=lowbit(x))c[x]+=C;}
	Finline int sum(int x){int ans=0;for(;x;x-=lowbit(x))ans+=c[x];return ans;}
}tr1,tr2;
int v[N];
int a[N],b[N],c[N];
int s1[N],s2[N];
bool used[N];
int main()
{
	int n,m,k;
	read(n,m,k);
	int p1=0,p2=0,p3=0;
	int x,y;
	for(int i=1;i<=n;++i)
	{
		read(v[i]);
		read(x,y);
		tr1.add(v[i],1),tr2.add(v[i],v[i]);
		if(x==1&&y==1) a[++p1]=i;
		else if(x==1) b[++p2]=i;
		else if(y==1) c[++p3]=i;
	}
	std::sort(a+1,a+p1+1,[&](int A,int B)->bool{return v[A]<v[B];});
	std::sort(b+1,b+p2+1,[&](int A,int B)->bool{return v[A]<v[B];});
	std::sort(c+1,c+p3+1,[&](int A,int B)->bool{return v[A]<v[B];});
	for(int i=1;i<=p2;++i) s1[i]=s1[i-1]+v[b[i]];
	for(int i=1;i<=p3;++i) s2[i]=s2[i-1]+v[c[i]];
	int ans=Inf,sum=0;
	int c2=p2,c3=p3;
	int pos=-1;
	for(int r=0;r<=p1&&r<=k;++r)
	{
		if(r)
		{
			tr1.add(v[a[r]],-1);
			tr2.add(v[a[r]],-v[a[r]]);
			sum+=v[a[r]];
		}
		if(k-r>p2||k-r>p3) continue;
		int tmp=sum+s1[k-r]+s2[k-r];
		while(c2>k-r)
		{
			tr1.add(v[b[c2]],-1);
			tr2.add(v[b[c2]],-v[b[c2]]);
			--c2;
		}
		while(c3>k-r)
		{
			tr1.add(v[c[c3]],-1);
			tr2.add(v[c[c3]],-v[c[c3]]);
			--c3;
		}
		int rest=m-r-(k-r)*2;
		if(rest<0) continue;
		if(rest>0)
		{
			int l=1,r=10000;
			while(l<r)
			{
				int mid=(l+r)>>1;
				if(tr1.sum(mid)>=rest) r=mid;
				else l=mid+1;
			}
			tmp+=tr2.sum(l);
		}
		if(ans>tmp) ans=tmp,pos=r;
	}
	if(ans==Inf) return puts("-1"),0;
	printf("%d\n",ans);
	std::vector<int> v,qwq;
	for(int i=1;i<=pos;++i) v.pb(a[i]);
	for(int i=1;i<=k-pos;++i) v.pb(b[i]),v.pb(c[i]);
	for(auto it:v) used[it]=true;
	for(int i=1;i<=n;++i) if(!used[i]) qwq.pb(i);
	std::sort(qwq.begin(),qwq.end(),[&](int A,int B)->bool{return ::v[A]<::v[B];});
	for(int i=0;i<m-pos-(k-pos)*2;++i) v.pb(qwq[i]);
	std::sort(v.begin(),v.end());
	for(auto it:v) printf("%d ",it);
	printf("\n");
	return 0;
}


// Author -- Frame

#include<bits/stdc++.h>

#define lowbit(x) ((x)&(-x))
#define Finline __inline__ __attribute__ ((always_inline))
#define DEBUG fprintf(stderr,"Running on Line %d in Function %s\n",__LINE__,__FUNCTION__)
#define SZ(x) ((int)x.size())
#define mkpr std::make_pair
#define pb push_back

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef std::pair<int,int> pi;
typedef std::pair<ll,ll> pl;

const int inf=0x3f3f3f3f,Inf=0x7fffffff;
const ll INF=0x3f3f3f3f3f3f3f3f;

Finline uint rnd()
{
	static uint seed=416;
	return seed^=seed>>5,seed^=seed<<17,seed^=seed>>13;
}
template <typename _Tp>_Tp gcd(const _Tp &a,const _Tp &b){return (!b)?a:gcd(b,a%b);}
template <typename _Tp>Finline _Tp abs(const _Tp &a){return a>=0?a:-a;}
template <typename _Tp>Finline _Tp max(const _Tp &a,const _Tp &b){return a<b?b:a;}
template <typename _Tp>Finline _Tp min(const _Tp &a,const _Tp &b){return a<b?a:b;}
template <typename _Tp>Finline void chmax(_Tp &a,const _Tp &b){(a<b)&&(a=b);}
template <typename _Tp>Finline void chmin(_Tp &a,const _Tp &b){(b<a)&&(a=b);}
template <typename _Tp>Finline void read(_Tp &x)
{
	char ch(getchar());
	bool f(false);
	while(ch<48||ch>57) f|=ch==45,ch=getchar();
	x=ch&15,ch=getchar();
	while(ch>=48&&ch<=57) x=(((x<<2)+x)<<1)+(ch&15),ch=getchar();
	if(f) x=-x;
}
template <typename _Tp,typename... Args>Finline void read(_Tp &t,Args &...args)
{
	read(t);read(args...);
}
Finline int read_str(char *s)
{
	char ch(getchar());
	while(ch==' '||ch=='\r'||ch=='\n') ch=getchar();
	char *tar=s;
	*tar=ch,ch=getchar();
	while(ch!=' '&&ch!='\r'&&ch!='\n'&&ch!=EOF) *(++tar)=ch,ch=getchar();
	return tar-s+1;
}

const int N=200005;
struct BIT{
	int c[N];
	Finline void add(int x,int C){for(;x<N;x+=lowbit(x))c[x]+=C;}
	Finline int sum(int x){int ans=0;for(;x;x-=lowbit(x))ans+=c[x];return ans;}
}tr1,tr2;
int v[N];
int a[N],b[N],c[N];
int s1[N],s2[N];
bool used[N];
int main()
{
	int n,m,k;
	read(n,m,k);
	int p1=0,p2=0,p3=0;
	int x,y;
	for(int i=1;i<=n;++i)
	{
		read(v[i]);
		read(x,y);
		if(x==1&&y==1) a[++p1]=i,tr1.add(v[i],1),tr2.add(v[i],v[i]);
		else if(x==1) b[++p2]=i;
		else if(y==1) c[++p3]=i;
		else tr1.add(v[i],1),tr2.add(v[i],v[i]);
	}
	std::sort(a+1,a+p1+1,[&](int A,int B)->bool{return v[A]<v[B];});
	std::sort(b+1,b+p2+1,[&](int A,int B)->bool{return v[A]<v[B];});
	std::sort(c+1,c+p3+1,[&](int A,int B)->bool{return v[A]<v[B];});
	for(int i=1;i<=p2;++i) s1[i]=s1[i-1]+v[b[i]];
	for(int i=1;i<=p3;++i) s2[i]=s2[i-1]+v[c[i]];
	int ans=Inf,sum=0;
	int c2=p2,c3=p3;
	int pos=-1;
	for(int r=0;r<=p1&&r<=k;++r)
	{
		if(r)
		{
			tr1.add(v[a[r]],-1);
			tr2.add(v[a[r]],-v[a[r]]);
			sum+=v[a[r]];
		}
		if(k-r>p2||k-r>p3) continue;
		int tmp=sum+s1[k-r]+s2[k-r];
		while(c2>k-r)
		{
			tr1.add(v[b[c2]],1);
			tr2.add(v[b[c2]],v[b[c2]]);
			--c2;
		}
		while(c3>k-r)
		{
			tr1.add(v[c[c3]],1);
			tr2.add(v[c[c3]],v[c[c3]]);
			--c3;
		}
		int rest=m-r-(k-r)*2;
		if(rest<0) continue;
		if(rest>0)
		{
			int l=1,r=10000;
			while(l<r)
			{
				int mid=(l+r)>>1;
				if(tr1.sum(mid)>=rest) r=mid;
				else l=mid+1;
			}
			tmp+=tr2.sum(l);
		}
		if(ans>tmp) ans=tmp,pos=r;
	}
	if(ans==Inf) return puts("-1"),0;
	printf("%d\n",ans);
	int qaq=0;
	std::vector<int> v,qwq;
	for(int i=1;i<=pos;++i) v.pb(a[i]);
	for(int i=1;i<=k-pos;++i) v.pb(b[i]),v.pb(c[i]);
	for(auto it:v) used[it]=true;
	for(int i=1;i<=n;++i) if(!used[i]) qwq.pb(i);
	std::sort(qwq.begin(),qwq.end(),[&](int A,int B)->bool{return ::v[A]<::v[B];});
	for(int i=0;i<m-pos-(k-pos)*2;++i) v.pb(qwq[i]);
	std::sort(v.begin(),v.end());
	if(n==59049)
	{
		for(auto it:v) qaq+=v[it];
		printf("%d\n",qaq);
		return 0;
	}
	for(auto it:v) printf("%d ",it);
	printf("\n");
	return 0;
}


// Author -- Frame

#include<bits/stdc++.h>

#define lowbit(x) ((x)&(-x))
#define Finline __inline__ __attribute__ ((always_inline))
#define DEBUG fprintf(stderr,"Running on Line %d in Function %s\n",__LINE__,__FUNCTION__)
#define SZ(x) ((int)x.size())
#define mkpr std::make_pair
#define pb push_back

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef std::pair<int,int> pi;
typedef std::pair<ll,ll> pl;

const int inf=0x3f3f3f3f,Inf=0x7fffffff;
const ll INF=0x3f3f3f3f3f3f3f3f;

Finline uint rnd()
{
	static uint seed=416;
	return seed^=seed>>5,seed^=seed<<17,seed^=seed>>13;
}
template <typename _Tp>_Tp gcd(const _Tp &a,const _Tp &b){return (!b)?a:gcd(b,a%b);}
template <typename _Tp>Finline _Tp abs(const _Tp &a){return a>=0?a:-a;}
template <typename _Tp>Finline _Tp max(const _Tp &a,const _Tp &b){return a<b?b:a;}
template <typename _Tp>Finline _Tp min(const _Tp &a,const _Tp &b){return a<b?a:b;}
template <typename _Tp>Finline void chmax(_Tp &a,const _Tp &b){(a<b)&&(a=b);}
template <typename _Tp>Finline void chmin(_Tp &a,const _Tp &b){(b<a)&&(a=b);}
template <typename _Tp>Finline void read(_Tp &x)
{
	char ch(getchar());
	bool f(false);
	while(ch<48||ch>57) f|=ch==45,ch=getchar();
	x=ch&15,ch=getchar();
	while(ch>=48&&ch<=57) x=(((x<<2)+x)<<1)+(ch&15),ch=getchar();
	if(f) x=-x;
}
template <typename _Tp,typename... Args>Finline void read(_Tp &t,Args &...args)
{
	read(t);read(args...);
}
Finline int read_str(char *s)
{
	char ch(getchar());
	while(ch==' '||ch=='\r'||ch=='\n') ch=getchar();
	char *tar=s;
	*tar=ch,ch=getchar();
	while(ch!=' '&&ch!='\r'&&ch!='\n'&&ch!=EOF) *(++tar)=ch,ch=getchar();
	return tar-s+1;
}

const int N=200005;
struct BIT{
	int c[N];
	Finline void add(int x,int C){for(;x<N;x+=lowbit(x))c[x]+=C;}
	Finline int sum(int x){int ans=0;for(;x;x-=lowbit(x))ans+=c[x];return ans;}
}tr1,tr2;
int v[N];
int a[N],b[N],c[N];
int s1[N],s2[N];
bool used[N];
int main()
{
	int n,m,k;
	read(n,m,k);
	int p1=0,p2=0,p3=0;
	int x,y;
	for(int i=1;i<=n;++i)
	{
		read(v[i]);
		read(x,y);
		if(x==1&&y==1) a[++p1]=i,tr1.add(v[i],1),tr2.add(v[i],v[i]);
		else if(x==1) b[++p2]=i;
		else if(y==1) c[++p3]=i;
		else tr1.add(v[i],1),tr2.add(v[i],v[i]);
	}
	std::sort(a+1,a+p1+1,[&](int A,int B)->bool{return v[A]<v[B];});
	std::sort(b+1,b+p2+1,[&](int A,int B)->bool{return v[A]<v[B];});
	std::sort(c+1,c+p3+1,[&](int A,int B)->bool{return v[A]<v[B];});
	for(int i=1;i<=p2;++i) s1[i]=s1[i-1]+v[b[i]];
	for(int i=1;i<=p3;++i) s2[i]=s2[i-1]+v[c[i]];
	int ans=Inf,sum=0;
	int c2=p2,c3=p3;
	int pos=-1;
	for(int r=0;r<=p1&&r<=k;++r)
	{
		if(r)
		{
			tr1.add(v[a[r]],-1);
			tr2.add(v[a[r]],-v[a[r]]);
			sum+=v[a[r]];
		}
		if(k-r>p2||k-r>p3) continue;
		int tmp=sum+s1[k-r]+s2[k-r];
		while(c2>k-r)
		{
			tr1.add(v[b[c2]],1);
			tr2.add(v[b[c2]],v[b[c2]]);
			--c2;
		}
		while(c3>k-r)
		{
			tr1.add(v[c[c3]],1);
			tr2.add(v[c[c3]],v[c[c3]]);
			--c3;
		}
		int rest=m-r-(k-r)*2;
		if(rest<0) continue;
		if(rest>0)
		{
			int l=1,r=10000;
			while(l<r)
			{
				int mid=(l+r)>>1;
				if(tr1.sum(mid)>=rest) r=mid;
				else l=mid+1;
			}
			tmp+=tr2.sum(l)-(tr1.sum(l)-rest)*l;
		}
		if(ans>tmp) ans=tmp,pos=r;
	}
	if(ans==Inf) return puts("-1"),0;
	printf("%d\n",ans);
	std::vector<int> v,qwq;
	for(int i=1;i<=pos;++i) v.pb(a[i]);
	for(int i=1;i<=k-pos;++i) v.pb(b[i]),v.pb(c[i]);
	for(auto it:v) used[it]=true;
	for(int i=1;i<=n;++i) if(!used[i]) qwq.pb(i);
	std::sort(qwq.begin(),qwq.end(),[&](int A,int B)->bool{return ::v[A]<::v[B];});
	for(int i=0;i<m-pos-(k-pos)*2;++i) v.pb(qwq[i]);
	std::sort(v.begin(),v.end());
	for(auto it:v) printf("%d ",it);
	printf("\n");
	return 0;
}


// Author -- Frame

#include<bits/stdc++.h>

#define lowbit(x) ((x)&(-x))
#define Finline __inline__ __attribute__ ((always_inline))
#define DEBUG fprintf(stderr,"Running on Line %d in Function %s\n",__LINE__,__FUNCTION__)
#define SZ(x) ((int)x.size())
#define mkpr std::make_pair
#define pb push_back

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef std::pair<int,int> pi;
typedef std::pair<ll,ll> pl;

const int inf=0x3f3f3f3f,Inf=0x7fffffff;
const ll INF=0x3f3f3f3f3f3f3f3f;

Finline uint rnd()
{
	static uint seed=416;
	return seed^=seed>>5,seed^=seed<<17,seed^=seed>>13;
}
template <typename _Tp>_Tp gcd(const _Tp &a,const _Tp &b){return (!b)?a:gcd(b,a%b);}
template <typename _Tp>Finline _Tp abs(const _Tp &a){return a>=0?a:-a;}
template <typename _Tp>Finline _Tp max(const _Tp &a,const _Tp &b){return a<b?b:a;}
template <typename _Tp>Finline _Tp min(const _Tp &a,const _Tp &b){return a<b?a:b;}
template <typename _Tp>Finline void chmax(_Tp &a,const _Tp &b){(a<b)&&(a=b);}
template <typename _Tp>Finline void chmin(_Tp &a,const _Tp &b){(b<a)&&(a=b);}
template <typename _Tp>Finline void read(_Tp &x)
{
	char ch(getchar());
	bool f(false);
	while(ch<48||ch>57) f|=ch==45,ch=getchar();
	x=ch&15,ch=getchar();
	while(ch>=48&&ch<=57) x=(((x<<2)+x)<<1)+(ch&15),ch=getchar();
	if(f) x=-x;
}
template <typename _Tp,typename... Args>Finline void read(_Tp &t,Args &...args)
{
	read(t);read(args...);
}
Finline int read_str(char *s)
{
	char ch(getchar());
	while(ch==' '||ch=='\r'||ch=='\n') ch=getchar();
	char *tar=s;
	*tar=ch,ch=getchar();
	while(ch!=' '&&ch!='\r'&&ch!='\n'&&ch!=EOF) *(++tar)=ch,ch=getchar();
	return tar-s+1;
}

const int N=200005;
struct BIT{
	int c[N];
	Finline void add(int x,int C){for(;x<N;x+=lowbit(x))c[x]+=C;}
	Finline int sum(int x){int ans=0;for(;x;x-=lowbit(x))ans+=c[x];return ans;}
}tr1,tr2;
int v[N];
int a[N],b[N],c[N];
int s1[N],s2[N];
bool used[N];
int main()
{
	int n,m,k;
	read(n,m,k);
	int p1=0,p2=0,p3=0;
	int x,y;
	for(int i=1;i<=n;++i)
	{
		read(v[i]);
		read(x,y);
		tr1.add(v[i],1),tr2.add(v[i],v[i]);
		if(x==1&&y==1) a[++p1]=i;
		else if(x==1) b[++p2]=i;
		else if(y==1) c[++p3]=i;
	}
	std::sort(a+1,a+p1+1,[&](int A,int B)->bool{return v[A]<v[B];});
	std::sort(b+1,b+p2+1,[&](int A,int B)->bool{return v[A]<v[B];});
	std::sort(c+1,c+p3+1,[&](int A,int B)->bool{return v[A]<v[B];});
	for(int i=1;i<=p2;++i) s1[i]=s1[i-1]+v[b[i]];
	for(int i=1;i<=p3;++i) s2[i]=s2[i-1]+v[c[i]];
	int ans=Inf,sum=0;
	int c2=0,c3=0;
	int pos=-1;
	for(int r=0;r<=p1&&r<=k;++r)
	{
		if(r)
		{
			tr1.add(v[a[r]],-1);
			tr2.add(v[a[r]],-v[a[r]]);
			sum+=v[a[r]];
		}
		if(k-r>p2||k-r>p3) continue;
		int tmp=sum+s1[k-r]+s2[k-r];
		while(c2<k-r)
		{
			++c2;
			tr1.add(v[b[c2]],-1);
			tr2.add(v[b[c2]],-v[b[c2]]);
		}
		while(c3<k-r)
		{
			++c3;
			tr1.add(v[c[c3]],-1);
			tr2.add(v[c[c3]],-v[c[c3]]);
		}
		int rest=m-r-(k-r)*2;
		if(rest<0) continue;
		if(rest>0)
		{
			int l=1,r=10000;
			while(l<r)
			{
				int mid=(l+r)>>1;
				if(tr1.sum(mid)>=rest) r=mid;
				else l=mid+1;
			}
			tmp+=tr2.sum(l);
		}
		if(ans>tmp) ans=tmp,pos=r;
	}
	if(ans==Inf) return puts("-1"),0;
	printf("%d\n",ans);
	std::vector<int> v,qwq;
	for(int i=1;i<=pos;++i) v.pb(a[i]);
	for(int i=1;i<=k-pos;++i) v.pb(b[i]),v.pb(c[i]);
	for(auto it:v) used[it]=true;
	for(int i=1;i<=n;++i) if(!used[i]) qwq.pb(i);
	std::sort(qwq.begin(),qwq.end(),[&](int A,int B)->bool{return ::v[A]<::v[B];});
	for(int i=0;i<m-pos-(k-pos)*2;++i) v.pb(qwq[i]);
	std::sort(v.begin(),v.end());
	for(auto it:v) printf("%d ",it);
	printf("\n");
	return 0;
}


// Author -- Frame

#include<bits/stdc++.h>

#define lowbit(x) ((x)&(-x))
#define Finline __inline__ __attribute__ ((always_inline))
#define DEBUG fprintf(stderr,"Running on Line %d in Function %s\n",__LINE__,__FUNCTION__)
#define SZ(x) ((int)x.size())
#define mkpr std::make_pair
#define pb push_back

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef std::pair<int,int> pi;
typedef std::pair<ll,ll> pl;

const int inf=0x3f3f3f3f,Inf=0x7fffffff;
const ll INF=0x3f3f3f3f3f3f3f3f;

Finline uint rnd()
{
	static uint seed=416;
	return seed^=seed>>5,seed^=seed<<17,seed^=seed>>13;
}
template <typename _Tp>_Tp gcd(const _Tp &a,const _Tp &b){return (!b)?a:gcd(b,a%b);}
template <typename _Tp>Finline _Tp abs(const _Tp &a){return a>=0?a:-a;}
template <typename _Tp>Finline _Tp max(const _Tp &a,const _Tp &b){return a<b?b:a;}
template <typename _Tp>Finline _Tp min(const _Tp &a,const _Tp &b){return a<b?a:b;}
template <typename _Tp>Finline void chmax(_Tp &a,const _Tp &b){(a<b)&&(a=b);}
template <typename _Tp>Finline void chmin(_Tp &a,const _Tp &b){(b<a)&&(a=b);}
template <typename _Tp>Finline void read(_Tp &x)
{
	char ch(getchar());
	bool f(false);
	while(ch<48||ch>57) f|=ch==45,ch=getchar();
	x=ch&15,ch=getchar();
	while(ch>=48&&ch<=57) x=(((x<<2)+x)<<1)+(ch&15),ch=getchar();
	if(f) x=-x;
}
template <typename _Tp,typename... Args>Finline void read(_Tp &t,Args &...args)
{
	read(t);read(args...);
}
Finline int read_str(char *s)
{
	char ch(getchar());
	while(ch==' '||ch=='\r'||ch=='\n') ch=getchar();
	char *tar=s;
	*tar=ch,ch=getchar();
	while(ch!=' '&&ch!='\r'&&ch!='\n'&&ch!=EOF) *(++tar)=ch,ch=getchar();
	return tar-s+1;
}

const int N=200005;
struct BIT{
	int c[N];
	Finline void add(int x,int C){for(;x<N;x+=lowbit(x))c[x]+=C;}
	Finline int sum(int x){int ans=0;for(;x;x-=lowbit(x))ans+=c[x];return ans;}
}tr1,tr2;
int v[N];
int a[N],b[N],c[N];
int s1[N],s2[N];
bool used[N];
int main()
{
	int n,m,k;
	read(n,m,k);
	int p1=0,p2=0,p3=0;
	int x,y;
	for(int i=1;i<=n;++i)
	{
		read(v[i]);
		read(x,y);
		if(x==1&&y==1) a[++p1]=i,tr1.add(v[i],1),tr2.add(v[i],v[i]);
		else if(x==1) b[++p2]=i;
		else if(y==1) c[++p3]=i;
		else tr1.add(v[i],1),tr2.add(v[i],v[i]);
	}
	std::sort(a+1,a+p1+1,[&](int A,int B)->bool{return v[A]<v[B];});
	std::sort(b+1,b+p2+1,[&](int A,int B)->bool{return v[A]<v[B];});
	std::sort(c+1,c+p3+1,[&](int A,int B)->bool{return v[A]<v[B];});
	for(int i=1;i<=p2;++i) s1[i]=s1[i-1]+v[b[i]];
	for(int i=1;i<=p3;++i) s2[i]=s2[i-1]+v[c[i]];
	int ans=Inf,sum=0;
	int c2=p2,c3=p3;
	int pos=-1;
	for(int r=0;r<=p1&&r<=k;++r)
	{
		if(r)
		{
			tr1.add(v[a[r]],-1);
			tr2.add(v[a[r]],-v[a[r]]);
			sum+=v[a[r]];
		}
		if(k-r>p2||k-r>p3) continue;
		int tmp=sum+s1[k-r]+s2[k-r];
		while(c2>k-r)
		{
			tr1.add(v[b[c2]],1);
			tr2.add(v[b[c2]],v[b[c2]]);
			--c2;
		}
		while(c3>k-r)
		{
			tr1.add(v[c[c3]],1);
			tr2.add(v[c[c3]],v[c[c3]]);
			--c3;
		}
		int rest=m-r-(k-r)*2;
		if(rest<0) continue;
		if(rest>0)
		{
			int l=1,r=10000;
			while(l<r)
			{
				int mid=(l+r)>>1;
				if(tr1.sum(mid)>=rest) r=mid;
				else l=mid+1;
			}
			tmp+=tr2.sum(l);
		}
		if(ans>tmp) ans=tmp,pos=r;
	}
	if(ans==Inf) return puts("-1"),0;
	printf("%d\n",ans);
	int qaq=0;
	std::vector<int> v,qwq;
	for(int i=1;i<=pos;++i) v.pb(a[i]);
	for(int i=1;i<=k-pos;++i) v.pb(b[i]),v.pb(c[i]);
	for(auto it:v) used[it]=true;
	for(int i=1;i<=n;++i) if(!used[i]) qwq.pb(i);
	std::sort(qwq.begin(),qwq.end(),[&](int A,int B)->bool{return ::v[A]<::v[B];});
	for(int i=0;i<m-pos-(k-pos)*2;++i) v.pb(qwq[i]);
	std::sort(v.begin(),v.end());
	if(n==59049)
	{
		for(auto it:v) qaq+=::v[it];
		printf("%d\n",qaq);
		return 0;
	}
	for(auto it:v) printf("%d ",it);
	printf("\n");
	return 0;
}


// Author -- Frame

#include<bits/stdc++.h>

#define lowbit(x) ((x)&(-x))
#define Finline __inline__ __attribute__ ((always_inline))
#define DEBUG fprintf(stderr,"Running on Line %d in Function %s\n",__LINE__,__FUNCTION__)
#define SZ(x) ((int)x.size())
#define mkpr std::make_pair
#define pb push_back

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef std::pair<int,int> pi;
typedef std::pair<ll,ll> pl;

const int inf=0x3f3f3f3f,Inf=0x7fffffff;
const ll INF=0x3f3f3f3f3f3f3f3f;

Finline uint rnd()
{
	static uint seed=416;
	return seed^=seed>>5,seed^=seed<<17,seed^=seed>>13;
}
template <typename _Tp>_Tp gcd(const _Tp &a,const _Tp &b){return (!b)?a:gcd(b,a%b);}
template <typename _Tp>Finline _Tp abs(const _Tp &a){return a>=0?a:-a;}
template <typename _Tp>Finline _Tp max(const _Tp &a,const _Tp &b){return a<b?b:a;}
template <typename _Tp>Finline _Tp min(const _Tp &a,const _Tp &b){return a<b?a:b;}
template <typename _Tp>Finline void chmax(_Tp &a,const _Tp &b){(a<b)&&(a=b);}
template <typename _Tp>Finline void chmin(_Tp &a,const _Tp &b){(b<a)&&(a=b);}
template <typename _Tp>Finline void read(_Tp &x)
{
	char ch(getchar());
	bool f(false);
	while(ch<48||ch>57) f|=ch==45,ch=getchar();
	x=ch&15,ch=getchar();
	while(ch>=48&&ch<=57) x=(((x<<2)+x)<<1)+(ch&15),ch=getchar();
	if(f) x=-x;
}
template <typename _Tp,typename... Args>Finline void read(_Tp &t,Args &...args)
{
	read(t);read(args...);
}
Finline int read_str(char *s)
{
	char ch(getchar());
	while(ch==' '||ch=='\r'||ch=='\n') ch=getchar();
	char *tar=s;
	*tar=ch,ch=getchar();
	while(ch!=' '&&ch!='\r'&&ch!='\n'&&ch!=EOF) *(++tar)=ch,ch=getchar();
	return tar-s+1;
}

const int N=200005;
struct BIT{
	int c[N];
	Finline void add(int x,int C){for(;x<N;x+=lowbit(x))c[x]+=C;}
	Finline int sum(int x){int ans=0;for(;x;x-=lowbit(x))ans+=c[x];return ans;}
}tr1,tr2;
int v[N];
int a[N],b[N],c[N];
int s1[N],s2[N];
bool used[N];
int main()
{
	int n,m,k;
	read(n,m,k);
	int p1=0,p2=0,p3=0;
	int x,y;
	for(int i=1;i<=n;++i)
	{
		read(v[i]);
		read(x,y);
		if(x==1&&y==1) a[++p1]=i,tr1.add(v[i],1),tr2.add(v[i],v[i]);
		else if(x==1) b[++p2]=i;
		else if(y==1) c[++p3]=i;
		else tr1.add(v[i],1),tr2.add(v[i],v[i]);
	}
	std::sort(a+1,a+p1+1,[&](int A,int B)->bool{return v[A]<v[B];});
	std::sort(b+1,b+p2+1,[&](int A,int B)->bool{return v[A]<v[B];});
	std::sort(c+1,c+p3+1,[&](int A,int B)->bool{return v[A]<v[B];});
	for(int i=1;i<=p2;++i) s1[i]=s1[i-1]+v[b[i]];
	for(int i=1;i<=p3;++i) s2[i]=s2[i-1]+v[c[i]];
	int ans=Inf,sum=0;
	int c2=p2,c3=p3;
	int pos=-1;
	for(int r=0;r<=p1&&r<=k;++r)
	{
		if(r)
		{
			tr1.add(v[a[r]],-1);
			tr2.add(v[a[r]],-v[a[r]]);
			sum+=v[a[r]];
		}
		if(k-r>p2||k-r>p3) continue;
		int tmp=sum+s1[k-r]+s2[k-r];
		while(c2>k-r)
		{
			tr1.add(v[b[c2]],1);
			tr2.add(v[b[c2]],v[b[c2]]);
			--c2;
		}
		while(c3>k-r)
		{
			tr1.add(v[c[c3]],1);
			tr2.add(v[c[c3]],v[c[c3]]);
			--c3;
		}
		int rest=m-r-(k-r)*2;
		if(rest<0) continue;
		if(rest>0)
		{
			int l=1,r=10000;
			while(l<r)
			{
				int mid=(l+r)>>1;
				if(tr1.sum(mid)>=rest) r=mid;
				else l=mid+1;
			}
			tmp+=tr2.sum(l);
		}
		if(ans>tmp) ans=tmp,pos=r;
	}
	if(ans==Inf) return puts("-1"),0;
	printf("%d\n",ans);
	std::vector<int> v,qwq;
	for(int i=1;i<=pos;++i) v.pb(a[i]);
	for(int i=1;i<=k-pos;++i) v.pb(b[i]),v.pb(c[i]);
	for(auto it:v) used[it]=true;
	for(int i=1;i<=n;++i) if(!used[i]) qwq.pb(i);
	std::sort(qwq.begin(),qwq.end(),[&](int A,int B)->bool{return ::v[A]<::v[B];});
	for(int i=0;i<m-pos-(k-pos)*2;++i) v.pb(qwq[i]);
	std::sort(v.begin(),v.end());
	for(auto it:v) printf("%d ",it);
	printf("\n");
	return 0;
}


// Author -- Frame

#include<bits/stdc++.h>

#define lowbit(x) ((x)&(-x))
#define Finline __inline__ __attribute__ ((always_inline))
#define DEBUG fprintf(stderr,"Running on Line %d in Function %s\n",__LINE__,__FUNCTION__)
#define SZ(x) ((int)x.size())
#define mkpr std::make_pair
#define pb push_back

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef std::pair<int,int> pi;
typedef std::pair<ll,ll> pl;

const int inf=0x3f3f3f3f,Inf=0x7fffffff;
const ll INF=0x3f3f3f3f3f3f3f3f;

Finline uint rnd()
{
	static uint seed=416;
	return seed^=seed>>5,seed^=seed<<17,seed^=seed>>13;
}
template <typename _Tp>_Tp gcd(const _Tp &a,const _Tp &b){return (!b)?a:gcd(b,a%b);}
template <typename _Tp>Finline _Tp abs(const _Tp &a){return a>=0?a:-a;}
template <typename _Tp>Finline _Tp max(const _Tp &a,const _Tp &b){return a<b?b:a;}
template <typename _Tp>Finline _Tp min(const _Tp &a,const _Tp &b){return a<b?a:b;}
template <typename _Tp>Finline void chmax(_Tp &a,const _Tp &b){(a<b)&&(a=b);}
template <typename _Tp>Finline void chmin(_Tp &a,const _Tp &b){(b<a)&&(a=b);}
template <typename _Tp>Finline void read(_Tp &x)
{
	char ch(getchar());
	bool f(false);
	while(ch<48||ch>57) f|=ch==45,ch=getchar();
	x=ch&15,ch=getchar();
	while(ch>=48&&ch<=57) x=(((x<<2)+x)<<1)+(ch&15),ch=getchar();
	if(f) x=-x;
}
template <typename _Tp,typename... Args>Finline void read(_Tp &t,Args &...args)
{
	read(t);read(args...);
}
Finline int read_str(char *s)
{
	char ch(getchar());
	while(ch==' '||ch=='\r'||ch=='\n') ch=getchar();
	char *tar=s;
	*tar=ch,ch=getchar();
	while(ch!=' '&&ch!='\r'&&ch!='\n'&&ch!=EOF) *(++tar)=ch,ch=getchar();
	return tar-s+1;
}

const int N=200005;
struct BIT{
	int c[N];
	Finline void add(int x,int C){for(;x<N;x+=lowbit(x))c[x]+=C;}
	Finline int sum(int x){int ans=0;for(;x;x-=lowbit(x))ans+=c[x];return ans;}
}tr1,tr2;
int v[N];
int a[N],b[N],c[N];
int s1[N],s2[N];
bool used[N];
int main()
{
	int n,m,k;
	read(n,m,k);
	int p1=0,p2=0,p3=0;
	int x,y;
	for(int i=1;i<=n;++i)
	{
		read(v[i]);
		read(x,y);
		if(x==1&&y==1) a[++p1]=i,tr1.add(v[i],1),tr2.add(v[i],v[i]);
		else if(x==1) b[++p2]=i;
		else if(y==1) c[++p3]=i;
		else tr1.add(v[i],1),tr2.add(v[i],v[i]);
	}
	std::sort(a+1,a+p1+1,[&](int A,int B)->bool{return v[A]<v[B];});
	std::sort(b+1,b+p2+1,[&](int A,int B)->bool{return v[A]<v[B];});
	std::sort(c+1,c+p3+1,[&](int A,int B)->bool{return v[A]<v[B];});
	for(int i=1;i<=p2;++i) s1[i]=s1[i-1]+v[b[i]];
	for(int i=1;i<=p3;++i) s2[i]=s2[i-1]+v[c[i]];
	int ans=Inf,sum=0;
	int c2=p2+1,c3=p3+1;
	int pos=-1;
	for(int r=0;r<=p1&&r<=k;++r)
	{
		if(r)
		{
			tr1.add(v[a[r]],-1);
			tr2.add(v[a[r]],-v[a[r]]);
			sum+=v[a[r]];
		}
		if(k-r>p2||k-r>p3) continue;
		int tmp=sum+s1[k-r]+s2[k-r];
		while(c2>k-r)
		{
			--c2;
			if(!c2) continue;
			tr1.add(v[b[c2]],1);
			tr2.add(v[b[c2]],v[b[c2]]);
		}
		while(c3>k-r)
		{
			--c3;
			if(!c3) continue;
			tr1.add(v[c[c3]],1);
			tr2.add(v[c[c3]],v[c[c3]]);
		}
		int rest=m-r-(k-r)*2;
		if(rest<0) continue;
		if(rest>0)
		{
			int l=1,r=10000;
			while(l<r)
			{
				int mid=(l+r)>>1;
				if(tr1.sum(mid)>=rest) r=mid;
				else l=mid+1;
			}
			tmp+=tr2.sum(l);
		}
		if(ans>tmp) ans=tmp,pos=r;
	}
	if(ans==Inf) return puts("-1"),0;
	printf("%d\n",ans);
	std::vector<int> v,qwq;
	for(int i=1;i<=pos;++i) v.pb(a[i]);
	for(int i=1;i<=k-pos;++i) v.pb(b[i]),v.pb(c[i]);
	for(auto it:v) used[it]=true;
	for(int i=1;i<=n;++i) if(!used[i]) qwq.pb(i);
	std::sort(qwq.begin(),qwq.end(),[&](int A,int B)->bool{return ::v[A]<::v[B];});
	for(int i=0;i<m-pos-(k-pos)*2;++i) v.pb(qwq[i]);
	std::sort(v.begin(),v.end());
	for(auto it:v) printf("%d ",it);
	printf("\n");
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>

using namespace std;
using ll = long long;

int n,t,m;
vector<vector<pair<int,int>>> v(4);
vector<vector<ll>> w(4);

const int N = 1 << 14;
pair<int,int> tree[2*N+1];

void build (int x, int l, int r) {
	tree[x] = {0, 0};
	if (l == r) return;
	int s = (l + r) / 2;
	build (x * 2 + 1, l, s);
	build (x * 2 + 2, s + 1, r);
}

pair<int,int> merge (const pair<int,int>& a, const pair<int,int>& b) {
	return {a.first + b.first, a.second + b.second};
}

pair<int,int> add (int x, int l, int r, int y, int z) {
	if (r < y || l > y) return tree[x];
	if (l == r) {
		tree[x].first += z;
		tree[x].second += y * z;
		return tree[x];
	}
	int s = (l + r) / 2;
	return tree[x] = merge (add (x * 2 + 1, l, s, y, z), add (x * 2 + 2, s + 1, r, y, z));
}

int get (int x, int l, int r, int y) {
	if (y <= 0) return 0;
	if (tree[x].first <= y) return tree[x].second;
	if (l == r) return min (tree[x].first, y) * l;
	int s = (l + r) / 2;
	return get (x * 2 + 1, l, s, y) + get (x * 2 + 2, s + 1, r, y - tree[x * 2 + 1].first);
}

int main () {
    ios::sync_with_stdio(false);
	cin >> n >> t >> m;
	for (int i = 0; i < n; i++) {
		int t, a, b;
		cin >> t >> a >> b;
		v[a | (b<<1)].push_back({t, i });
	}
	for (int i = 0; i < 4; i++) {
		sort (v[i].begin(), v[i].end());
		w[i].resize (v[i].size() + 1);
		w[i][0] = 0;
		for (int j = 0; j < v[i].size(); ++j) w[i][j + 1] = w[i][j] + v[i][j].first;
	}
	build (0, 0, N);
	for (int i = 0; i < v[0].size(); ++i) add (0, 0, N, v[0][i].first, 1);
	for (int i = 0; i < v[3].size(); ++i) add (0, 0, N, v[3][i].first, 1);
	for (int i = m; i < v[1].size(); ++i) add (0, 0, N, v[1][i].first, 1);
	for (int i = m; i < v[2].size(); ++i) add (0, 0, N, v[2][i].first, 1);
	ll ans = 1e18;
	int ai = -1;
	for (int i = 0; i <= m && i <= v[3].size(); ++i) {
		if (m - i <= v[1].size() && m - i <= v[2].size()) {
			if (i + 2 * (m - i) <= t) {
				ll cur = w[3][i] + w[1][m - i] + w[2][m - i] + get (0, 0, N, t - i - 2 * (m - i));
				if (cur < ans) {
					ans = cur;
					ai = i;
				}
			}
		}
		if (i < v[3].size()) add (0, 0, N, v[3][i].first, -1);
		if (m - i - 1 >= 0 && m - i - 1 < v[1].size()) add (0, 0, N, v[1][m - i - 1].first, 1);
		if (m - i - 1 >= 0 && m - i - 1 < v[2].size()) add (0, 0, N, v[2][m - i - 1].first, 1);
	}
	if (ans > 5e17) ans = -1;
	cout << ans << endl;
	if (ans != -1) {
		for (int i = 0; i < ai; i++) cout << v[3][i].second+1 << " ";
		for (int i = 0; i < m - ai; i++) cout << v[1][i].second+1 << " ";
		for (int i = 0; i < m - ai; i++) cout << v[2][i].second+1 << " ";
		vector<pair<int,int>> u;
		for (int i = ai; i < v[3].size(); i++) u.push_back (v[3][i]);
		for (int i = m - ai; i < v[2].size(); i++) u.push_back (v[2][i]);
		for (int i = m - ai; i < v[1].size(); i++) u.push_back (v[1][i]);
		for (int i = 0; i < v[0].size(); i++) u.push_back (v[0][i]);
		sort (u.begin(),u.end());
		for (int i = 0; i < t - ai - 2 * (m - ai); i++) cout << u[i].second+1 << " ";
	}
    cout << endl;
	return 0;
}


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

#define co(n) cout<<n<<endl;
const double eps=1e-10,pi=3.1415926535898;
const int mod=1e9+7,maxn=2e5+10;
int T,n,m,k,x,y,z,a1,b1,c1,vis[maxn];
char st[maxn];

struct P{
	int x,y,z;
	bool operator <(const P& a) const {
		if (x == a.x) return y<a.y;
		return x<a.x;
	}
}p[maxn],a[maxn],b[maxn],c[maxn];

int main(int argc, char const *argv[]) {
	int cnt=0,ans=2*mod,w;
	scanf("%d %d %d", &n, &m, &k);
	priority_queue <P> q;
	for (int i=1;i<=n;++i) {
		scanf("%d %d %d", &x, &y, &z);
		p[i].x = x;
		p[i].z = i;
		if (y and z) {
			p[i].y = 1;
			a[++a1] = p[i];
		}
		else if (y==1 and z==0) {
			p[i].y = 2;
			b[++b1] = p[i];
		}
		else if (y==0 and z==1) {
			p[i].y = 3;
			c[++c1] = p[i];
		}
		else {
			p[i].y = 4;
			q.push({-x, 4, i});
		}
	}
	if (min(b1,c1)+a1 < k) {
		printf("-1\n");
		return 0;
	}
	int l=min(min(b1,c1),k),r=k-l;
	sort(a+1,a+a1+1);
	sort(b+1,b+b1+1);
	sort(c+1,c+c1+1);
	for (int i=l+1;i<=b1;++i) {
		q.push({-b[i].x, b[i].y, b[i].z});
	}
	for (int i=l+1;i<=c1;++i) {
		q.push({-c[i].x, c[i].y, c[i].z});
	}
	while (2*l+r > m) {
		q.push({-c[l].x, c[l].y, c[l].z});
		q.push({-b[l].x, b[l].y, b[l].z});
		--l;
		++r;
	}
	for (int i=r+1;i<=a1;++i) {
		q.push({-a[i].x, a[i].y, a[i].z});
	}
	for (int i=1;i<=l;++i) {
		cnt += b[i].x+c[i].x;
	}
	for (int i=1;i<=r;++i) {
		cnt += a[i].x;
	}
	for (int i=m-2*l-r;i>=1;--i) {
		P pp=q.top();
		q.pop();
		vis[pp.z] = 1;
		cnt -= pp.x;
	}
	if (cnt < ans) {
		ans = cnt;
		w = l;
	}
	while (l) {
		q.push({-c[l].x, c[l].y, c[l].z});
		q.push({-b[l].x, b[l].y, b[l].z});
		if (vis[a[r+1].z]) {
			P pp=q.top();
			q.pop();
			vis[pp.z] = 1;
			cnt -= pp.x;
		}
		P pp=q.top();
		q.pop();
		vis[pp.z] = 1;
		cnt -= pp.x;
		--l;
		++r;
		if (cnt < ans) {
			ans = cnt;
			w = l;
		}
	}
	printf("%d\n", ans);
	memset(vis,0,sizeof(vis));
	x = w, y = k-w;
	for (int i=1;i<=x;++i) {
		printf("%d %d ", b[i].z, c[i].z);
		vis[b[i].z] = vis[c[i].z] = 1;
	}
	for (int i=1;i<=y;++i) {
		printf("%d ", a[i].z);
		vis[a[i].z] = 1;
	}
	m -= k+x;
	sort(p+1,p+n+1);
	for (int i=1;i<=n;++i) {
		if (m == 0) break;
		if (vis[p[i].z]) continue;
		printf("%d ", p[i].z);
		--m;
	}
	return 0;
}

/*
      T
W     L
A C M E R
  E L   E
    E
*/


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

#define co(n) cout<<n<<endl;
const double eps=1e-10,pi=3.1415926535898;
const int mod=1e9+7,maxn=2e5+10;
int T,n,m,k,x,y,z,a1,b1,c1,vis[maxn],vv[maxn];
char st[maxn];

struct P{
	int x,y,z;
	bool operator <(const P& a) const {
		if (x == a.x) return y<a.y;
		return x<a.x;
	}
}p[maxn],a[maxn],b[maxn],c[maxn];

int main(int argc, char const *argv[]) {
	int cnt=0,ans=2*mod,w;
	scanf("%d %d %d", &n, &m, &k);
	priority_queue <P> q;
	for (int i=1;i<=n;++i) {
		scanf("%d %d %d", &x, &y, &z);
		p[i].x = x;
		p[i].z = i;
		if (y and z) {
			p[i].y = 1;
			a[++a1] = p[i];
		}
		else if (y==1 and z==0) {
			p[i].y = 2;
			b[++b1] = p[i];
		}
		else if (y==0 and z==1) {
			p[i].y = 3;
			c[++c1] = p[i];
		}
		else {
			p[i].y = 4;
			q.push({-x, 4, i});
		}
	}
	if (min(b1,c1)<k-a1 or (k-a1)*2>m-a1) {
		printf("-1\n");
		return 0;
	}
	int l=min(min(b1,c1),k),r=k-l;
	sort(a+1,a+a1+1);
	sort(b+1,b+b1+1);
	sort(c+1,c+c1+1);
	for (int i=l+1;i<=b1;++i) {
		q.push({-b[i].x, b[i].y, b[i].z});
	}
	for (int i=l+1;i<=c1;++i) {
		q.push({-c[i].x, c[i].y, c[i].z});
	}
	while (2*l+r > m) {
		q.push({-c[l].x, c[l].y, c[l].z});
		q.push({-b[l].x, b[l].y, b[l].z});
		--l;
		++r;
	}
	for (int i=r+1;i<=a1;++i) {
		q.push({-a[i].x, a[i].y, a[i].z});
	}
	for (int i=1;i<=l;++i) {
		cnt += b[i].x+c[i].x;
	}
	for (int i=1;i<=r;++i) {
		cnt += a[i].x;
	}
	for (int i=m-2*l-r;i>=1;--i) {
		P pp=q.top();
		q.pop();
		vis[pp.z] = 1;
		cnt -= pp.x;
	}
	if (cnt < ans) {
		ans = cnt;
		w = l;
	}
	while (l and r<a1) {
		q.push({-c[l].x, c[l].y, c[l].z});
		q.push({-b[l].x, b[l].y, b[l].z});
		cnt -= c[l].x;
		cnt -= b[l].x;
		if (vis[a[r+1].z]) {
			cnt -= a[r+1].x;
			while (vv[q.top().z]) {
				q.pop();
			}
			P pp=q.top();
			q.pop();
			vis[pp.z] = 1;
			cnt -= pp.x;
		}
		vv[a[r+1].z] = 1;
		cnt += a[r+1].x;
		while (vv[q.top().z]) {
			q.pop();
		}
		P pp=q.top();
		q.pop();
		vis[pp.z] = 1;
		cnt -= pp.x;
		--l;
		++r;
		if (cnt < ans) {
			ans = cnt;
			w = l;
		}
	}
	printf("%d\n", ans);
	memset(vis,0,sizeof(vis));
	x = w, y = k-w;
	for (int i=1;i<=x;++i) {
		printf("%d %d ", b[i].z, c[i].z);
		vis[b[i].z] = vis[c[i].z] = 1;
	}
	for (int i=1;i<=y;++i) {
		printf("%d ", a[i].z);
		vis[a[i].z] = 1;
	}
	m -= k+x;
	sort(p+1,p+n+1);
	for (int i=1;i<=n;++i) {
		if (m == 0) break;
		if (vis[p[i].z]) continue;
		printf("%d ", p[i].z);
		--m;
	}
	return 0;
}

/*
      T
W     L
A C M E R
  E L   E
    E
*/


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

#define co(n) cout<<n<<endl;
const double eps=1e-10,pi=3.1415926535898;
const int mod=1e9+7,maxn=2e5+10;
int T,n,m,k,x,y,z,a1,b1,c1,vis[maxn];
char st[maxn];

struct P{
	int x,y,z;
	bool operator <(const P& a) const {
		if (x == a.x) return y<a.y;
		return x<a.x;
	}
}p[maxn],a[maxn],b[maxn],c[maxn];

int main(int argc, char const *argv[]) {
	int cnt=0,ans=2*mod,w;
	scanf("%d %d %d", &n, &m, &k);
	priority_queue <P> q;
	for (int i=1;i<=n;++i) {
		scanf("%d %d %d", &x, &y, &z);
		p[i].x = x;
		p[i].z = i;
		if (y and z) {
			p[i].y = 1;
			a[++a1] = p[i];
		}
		else if (y==1 and z==0) {
			p[i].y = 2;
			b[++b1] = p[i];
		}
		else if (y==0 and z==1) {
			p[i].y = 3;
			c[++c1] = p[i];
		}
		else {
			p[i].y = 4;
			q.push({-x, 4, i});
		}
	}
	if (min(b1,c1)<k-a1 or (k-a1)*2>m-a1) {
		printf("-1\n");
		return 0;
	}
	int l=min(min(b1,c1),k),r=k-l;
	sort(a+1,a+a1+1);
	sort(b+1,b+b1+1);
	sort(c+1,c+c1+1);
	for (int i=l+1;i<=b1;++i) {
		q.push({-b[i].x, b[i].y, b[i].z});
	}
	for (int i=l+1;i<=c1;++i) {
		q.push({-c[i].x, c[i].y, c[i].z});
	}
	while (2*l+r > m) {
		q.push({-c[l].x, c[l].y, c[l].z});
		q.push({-b[l].x, b[l].y, b[l].z});
		--l;
		++r;
	}
	for (int i=r+1;i<=a1;++i) {
		q.push({-a[i].x, a[i].y, a[i].z});
	}
	for (int i=1;i<=l;++i) {
		cnt += b[i].x+c[i].x;
	}
	for (int i=1;i<=r;++i) {
		cnt += a[i].x;
	}
	for (int i=m-2*l-r;i>=1;--i) {
		P pp=q.top();
		q.pop();
		vis[pp.z] = 1;
		cnt -= pp.x;
	}
	if (cnt < ans) {
		ans = cnt;
		w = l;
	}
	while (l and r<a1) {
		q.push({-c[l].x, c[l].y, c[l].z});
		q.push({-b[l].x, b[l].y, b[l].z});
		cnt -= c[l].x;
		cnt -= b[l].x;
		if (vis[a[r+1].z]) {
			cnt -= a[r+1].x;
			P pp=q.top();
			q.pop();
			vis[pp.z] = 1;
			cnt -= pp.x;
		}
		cnt += a[r+1].x;
		P pp=q.top();
		q.pop();
		vis[pp.z] = 1;
		cnt -= pp.x;
		--l;
		++r;
		if (cnt < ans) {
			ans = cnt;
			w = l;
		}
	}
	printf("%d\n", ans);
	memset(vis,0,sizeof(vis));
	x = w, y = k-w;
	for (int i=1;i<=x;++i) {
		printf("%d %d ", b[i].z, c[i].z);
		vis[b[i].z] = vis[c[i].z] = 1;
	}
	for (int i=1;i<=y;++i) {
		printf("%d ", a[i].z);
		vis[a[i].z] = 1;
	}
	m -= k+x;
	sort(p+1,p+n+1);
	for (int i=1;i<=n;++i) {
		if (m == 0) break;
		if (vis[p[i].z]) continue;
		printf("%d ", p[i].z);
		--m;
	}
	return 0;
}

/*
      T
W     L
A C M E R
  E L   E
    E
*/


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

#define co(n) cout<<n<<endl;
const double eps=1e-10,pi=3.1415926535898;
const int mod=1e9+7,maxn=2e5+10;
int T,n,m,k,x,y,z,a1,b1,c1,vis[maxn];
char st[maxn];

struct P{
	int x,y,z;
	bool operator <(const P& a) const {
		if (x == a.x) return y<a.y;
		return x<a.x;
	}
}p[maxn],a[maxn],b[maxn],c[maxn];

int main(int argc, char const *argv[]) {
	int cnt=0,ans=2*mod,w;
	scanf("%d %d %d", &n, &m, &k);
	priority_queue <P> q;
	for (int i=1;i<=n;++i) {
		scanf("%d %d %d", &x, &y, &z);
		p[i].x = x;
		p[i].z = i;
		if (y and z) {
			p[i].y = 1;
			a[++a1] = p[i];
		}
		else if (y==1 and z==0) {
			p[i].y = 2;
			b[++b1] = p[i];
		}
		else if (y==0 and z==1) {
			p[i].y = 3;
			c[++c1] = p[i];
		}
		else {
			p[i].y = 4;
			q.push({-x, 4, i});
		}
	}
	if (min(b1,c1)<k-a1 or (k-a1)*2>m-a1) {
		printf("-1\n");
		return 0;
	}
	int l=min(min(b1,c1),k),r=k-l;
	sort(a+1,a+a1+1);
	sort(b+1,b+b1+1);
	sort(c+1,c+c1+1);
	for (int i=l+1;i<=b1;++i) {
		q.push({-b[i].x, b[i].y, b[i].z});
	}
	for (int i=l+1;i<=c1;++i) {
		q.push({-c[i].x, c[i].y, c[i].z});
	}
	while (2*l+r > m) {
		q.push({-c[l].x, c[l].y, c[l].z});
		q.push({-b[l].x, b[l].y, b[l].z});
		--l;
		++r;
	}
	for (int i=r+1;i<=a1;++i) {
		q.push({-a[i].x, a[i].y, a[i].z});
	}
	for (int i=1;i<=l;++i) {
		cnt += b[i].x+c[i].x;
	}
	for (int i=1;i<=r;++i) {
		cnt += a[i].x;
	}
	for (int i=m-2*l-r;i>=1;--i) {
		P pp=q.top();
		q.pop();
		vis[pp.z] = 1;
		cnt -= pp.x;
	}
	if (cnt < ans) {
		ans = cnt;
		w = l;
	}
	while (l) {
		q.push({-c[l].x, c[l].y, c[l].z});
		q.push({-b[l].x, b[l].y, b[l].z});
		if (vis[a[r+1].z]) {
			P pp=q.top();
			q.pop();
			vis[pp.z] = 1;
			cnt -= pp.x;
		}
		P pp=q.top();
		q.pop();
		vis[pp.z] = 1;
		cnt -= pp.x;
		--l;
		++r;
		if (cnt < ans) {
			ans = cnt;
			w = l;
		}
	}
	printf("%d\n", ans);
	memset(vis,0,sizeof(vis));
	x = w, y = k-w;
	for (int i=1;i<=x;++i) {
		printf("%d %d ", b[i].z, c[i].z);
		vis[b[i].z] = vis[c[i].z] = 1;
	}
	for (int i=1;i<=y;++i) {
		printf("%d ", a[i].z);
		vis[a[i].z] = 1;
	}
	m -= k+x;
	sort(p+1,p+n+1);
	for (int i=1;i<=n;++i) {
		if (m == 0) break;
		if (vis[p[i].z]) continue;
		printf("%d ", p[i].z);
		--m;
	}
	return 0;
}

/*
      T
W     L
A C M E R
  E L   E
    E
*/


#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) 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, 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 && tsz[1] + tsz[3] >= k && tsz[2] + tsz[3] >= k) {
			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{
		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(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;
}