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

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

}

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

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

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

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

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

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

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

}


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

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

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

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

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

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


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

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

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

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

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

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


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

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

}

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

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

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

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

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

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

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

}


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

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

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

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

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


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

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

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

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

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


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

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

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

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

    for(int i = 0;i<3;++i){
        for(int j = used[i];j < pp[i].size();++j)
        {
            //cout << pp[i][j].second <<" \n"[j==pp[i].size()-1];
            leftover.push_back({arr[pp[i][j].second].first,pp[i][j].second});
        }
    }

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

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


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

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

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

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

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

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


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

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

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

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

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

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


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

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

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

}

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

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

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

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

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

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

}


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define forby(i, f, n) for (int32_t (i) = (f); i < n; i++)
#define sz(x) static_cast<int32_t>(x.size())
#define vec std::vector<int32_t>
#define forward(it, i) std::next(it.begin(), i)
#define prev(it, i) std::prev(it.end(), i)
#define inf 2000000000

int main(int argc, char** argv) {

  int32_t n, m, k;
  scanf("%d%d%d", &n, &m, &k);

  vec vec_a;
  vec vec_b;
  vec vec_ab;
  vec vec_remains;
  vec vec_all;

  vec_all.reserve(n);

  forby (i, 0, n) {
    int32_t t, a, b;
    scanf("%d%d%d", &t, &a, &b);

    vec_all.push_back(t);

  if (a == 1 && b == 1) {
      vec_ab.push_back(i);
    } else if (a == 1) {
      vec_a.push_back(i);
    } else if (b == 1) {
      vec_b.push_back(i);
    } else {
      vec_remains.push_back(i);
    }
  }

 // check if impossible
  if (
      sz(vec_a) + sz(vec_ab) < k ||
      sz(vec_b) + sz(vec_ab) < k
     ) {
    printf("%d\n", -1);
    return 0;
  }

  std::function<bool(int32_t, int32_t)> cmp = [&vec_all](int32_t p, int32_t q) {
    return vec_all[ p ] < vec_all[ q ];
  };

  int32_t incommon = std::min(sz(vec_ab), k);
  int32_t selected_singular = k - incommon;
  int32_t to_select = m - (selected_singular * 2 + incommon);
  if (to_select < 0) {
    printf("%d\n", -1);
    return 0;
  }

  std::sort(all(vec_a), cmp);
  std::sort(all(vec_b), cmp);
  std::sort(all(vec_ab), cmp);
  std::sort(all(vec_remains), cmp);

  int32_t k_time = 0;
  forby (i, 0, incommon) {
    k_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, 0, selected_singular) {
    k_time += vec_all[ vec_a[ i ] ];
    k_time += vec_all[ vec_b[ i ] ];
  }

  std::multiset<int32_t, decltype(cmp)> m_in(cmp);
  std::multiset<int32_t, decltype(cmp)> m_out(cmp);
  int32_t ms_time = 0;
  forby (i, incommon, sz(vec_ab)) {
    m_in.insert(vec_ab[ i ]);
    ms_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, selected_singular, sz(vec_a)) {
    m_in.insert(vec_a[ i ]);
    ms_time += vec_all[ vec_a[ i ] ];
  }
  forby (i, selected_singular, sz(vec_b)) {
    m_in.insert(vec_b[ i ]);
    ms_time += vec_all[ vec_b[ i ] ];
  }
  forby (i, 0, sz(vec_remains)) {
    ms_time += vec_all[ vec_remains[ i ] ];
    m_in.insert(vec_remains[ i ]);
  }
  while (sz(m_in) > to_select) {
    ms_time -= vec_all[ *prev(m_in, 1) ];
    m_out.insert(*prev(m_in, 1));
    m_in.erase(prev(m_in, 1));
  }

  int32_t total_time = inf;
  auto balance = [&vec_all, &m_in, &m_out, &ms_time](int32_t to_select) {
    while (sz(m_in) < to_select) {
      ms_time += vec_all[ *forward(m_out, 0) ];
      m_in.insert(*forward(m_out, 0));
      m_out.erase(forward(m_out, 0));
    }
    while (sz(m_in) > to_select) {
      ms_time -= vec_all[ *prev(m_in, 1) ];
      m_out.insert(*prev(m_in, 1));
      m_in.erase(prev(m_in, 1));
    }
    while (sz(m_in) > 0 && sz(m_out) > 0 && vec_all[ *prev(m_in, 1) ] > vec_all[ *forward(m_out, 0) ]) {
      int32_t tmp_in = *prev(m_in, 1);
      int32_t tmp_out = *forward(m_out, 0);
      m_in.erase(prev(m_in, 1));
      m_out.erase(forward(m_out, 0));
      m_in.insert(tmp_out);
      m_out.insert(tmp_in);
      ms_time -= vec_all[ tmp_in ];
      ms_time += vec_all[ tmp_out ];
    }
  };
  while (true) {

    int32_t current_time = k_time + ms_time;
    // if we pass the correct answer
    if (current_time > total_time) {
      selected_singular--;
      decltype(m_in)::iterator it_ab = m_in.find(vec_ab[ incommon ]);
      if (it_ab == m_in.end()) {
        m_out.erase(m_out.find(*it_ab));
      } else {
        m_in.erase(it_ab);
      }
      m_in.insert(vec_a[ selected_singular ]);
      m_in.insert(vec_b[ selected_singular ]);
      balance(to_select + 1);
      incommon++;
      break;
    }
    total_time = current_time;

    if (
        incommon == 0 ||
        selected_singular == std::min(sz(vec_a), sz(vec_b)) ||
        to_select == 0
    ) {
      break;
    }

    incommon--;
    k_time -= vec_all[ vec_ab[ incommon ] ];
    k_time += vec_all[ vec_a[ selected_singular ] ];
    k_time += vec_all[ vec_b[ selected_singular ] ];
    to_select--;
    decltype(m_in)::iterator it_a = m_in.find(vec_a[ selected_singular ]);
    if (it_a == m_in.end()) {
      m_out.erase(m_out.find(*it_a));
    } else {
      ms_time -= vec_all[ vec_a[ selected_singular ] ];
      m_in.erase(it_a);
    }
    decltype(m_in)::iterator it_b = m_in.find(vec_b[ selected_singular ]);
    if (it_b == m_in.end()) {
      m_out.erase(m_out.find(*it_b));
    } else {
      ms_time -= vec_all[ vec_b[ selected_singular ] ];
      m_in.erase(it_b);
    }

    m_out.insert(vec_ab[ incommon ]);
    balance(to_select);
    selected_singular++;
  }

  printf("%d\n", total_time);
  forby(i, 0, incommon) {
    printf("%d ", vec_ab[ i ] + 1);
  }
  forby(i, 0, selected_singular) {
    printf("%d %d ", vec_a[ i ] + 1, vec_b[ i ] + 1);
  }
  for (int32_t i : m_in) {
    printf("%d ", i + 1);
  }
  printf("\n");

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define forby(i, f, n) for (int32_t (i) = (f); i < n; i++)
#define sz(x) static_cast<int32_t>(x.size())
#define vec std::vector<int32_t>
#define forward(it, i) std::next(it.begin(), i)
#define prev(it, i) std::prev(it.end(), i)
#define inf static_cast<int32_t>(2e31)

int main(int argc, char** argv) {

  int32_t n, m, k;
  scanf("%d%d%d", &n, &m, &k);

  vec vec_a;
  vec vec_b;
  vec vec_ab;
  vec vec_remains;
  vec vec_all;

  vec_all.reserve(n);

  forby (i, 0, n) {
    int32_t t, a, b;
    scanf("%d%d%d", &t, &a, &b);

    vec_all.push_back(t);

  if (a == 1 && b == 1) {
      vec_ab.push_back(i);
    } else if (a == 1) {
      vec_a.push_back(i);
    } else if (b == 1) {
      vec_b.push_back(i);
    } else {
      vec_remains.push_back(i);
    }
  }

 // check if impossible
  if (
      sz(vec_a) + sz(vec_ab) < k ||
      sz(vec_b) + sz(vec_ab) < k
     ) {
    printf("%d\n", -1);
    return 0;
  }

  std::function<bool(int32_t, int32_t)> cmp = [&vec_all](int32_t p, int32_t q) {
    return vec_all[ p ] < vec_all[ q ];
  };

  int32_t incommon = std::min(sz(vec_ab), k);
  int32_t selected_singular = k - incommon;
  int32_t to_select = m - (selected_singular * 2 + incommon);
  if (to_select < 0) {
    printf("%d\n", -1);
    return 0;
  }

  std::sort(all(vec_a), cmp);
  std::sort(all(vec_b), cmp);
  std::sort(all(vec_ab), cmp);
  std::sort(all(vec_remains), cmp);

  int32_t k_time = 0;
  forby (i, 0, incommon) {
    k_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, 0, selected_singular) {
    k_time += vec_all[ vec_a[ i ] ];
    k_time += vec_all[ vec_b[ i ] ];
  }

  std::multiset<int32_t, decltype(cmp)> m_in(cmp);
  std::multiset<int32_t, decltype(cmp)> m_out(cmp);
  int32_t ms_time = 0;
  forby (i, incommon, sz(vec_ab)) {
    m_in.insert(vec_ab[ i ]);
    ms_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, selected_singular, sz(vec_a)) {
    m_in.insert(vec_a[ i ]);
    ms_time += vec_all[ vec_a[ i ] ];
  }
  forby (i, selected_singular, sz(vec_b)) {
    m_in.insert(vec_b[ i ]);
    ms_time += vec_all[ vec_b[ i ] ];
  }
  forby (i, 0, sz(vec_remains)) {
    if (i < to_select) {
      ms_time += vec_all[ vec_remains[ i ] ];
      m_in.insert(vec_remains[ i ]);
    } else {
      m_out.insert(vec_remains[ i ]);
    }
  }
  while (sz(m_in) > to_select) {
    ms_time -= vec_all[ *prev(m_in, 1) ];
    m_out.insert(*prev(m_in, 1));
    m_in.erase(prev(m_in, 1));
  }

  int32_t total_time = inf;
  while (true) {

    int32_t current_time = k_time + ms_time;
    if (current_time > total_time) {
      break;
    }
    total_time = current_time;

    if (
        incommon == 0 ||
        selected_singular == std::min(sz(vec_a), sz(vec_b)) ||
        to_select == 0
    ) {
      break;
    }

    incommon--;
    k_time -= vec_all[ vec_ab[ incommon ] ];
    k_time += vec_all[ vec_a[ selected_singular ] ];
    k_time += vec_all[ vec_b[ selected_singular ] ];
    to_select--;
    decltype(m_in)::iterator it_a = m_in.find(vec_a[ selected_singular ]);
    if (it_a != m_in.end()) {
      ms_time -= vec_all[ vec_a[ selected_singular ] ];
      m_in.erase(it_a);
    }
    decltype(m_in)::iterator it_b = m_in.find(vec_b[ selected_singular ]);
    if (it_b != m_in.end()) {
      ms_time -= vec_all[ vec_b[ selected_singular ] ];
      m_in.erase(it_b);
    }
    decltype(m_in)::iterator it_ab = m_in.find(vec_ab[ incommon ]);
    if (it_ab != m_in.end()) {
      ms_time += vec_all[ vec_ab[ incommon ] ];
      m_in.insert(vec_ab[ incommon ]);
    }

    while (sz(m_in) < to_select) {
      ms_time += vec_all[ *forward(m_out, 0) ];
      m_in.insert(*forward(m_out, 0));
      m_out.erase(forward(m_out, 0));
    }
    while (sz(m_in) > to_select) {
      ms_time -= vec_all[ *prev(m_in, 1) ];
      m_out.insert(*prev(m_in, 1));
      m_in.erase(prev(m_in, 1));
    }

    selected_singular++;
  }

  printf("%d\n", total_time);
  forby(i, 0, incommon) {
    printf("%d ", vec_ab[ i ] + 1);
  }
  forby(i, 0, selected_singular) {
    printf("%d %d ", vec_a[ i ] + 1, vec_b[ i ] + 1);
  }
  for (int32_t i : m_in) {
    printf("%d ", i + 1);
  }
  printf("\n");

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define forby(i, f, n) for (int32_t (i) = (f); i < n; i++)
#define sz(x) static_cast<int32_t>(x.size())
#define vec std::vector<int32_t>
#define forward(it, i) std::next(it.begin(), i)
#define prev(it, i) std::prev(it.end(), i)
#define inf static_cast<int32_t>(2e31)

int main(int argc, char** argv) {

  int32_t n, m, k;
  scanf("%d%d%d", &n, &m, &k);

  vec vec_a;
  vec vec_b;
  vec vec_ab;
  vec vec_remains;
  vec vec_all;

  vec_all.reserve(n);

  forby (i, 0, n) {
    int32_t t, a, b;
    scanf("%d%d%d", &t, &a, &b);

    vec_all.push_back(t);

  if (a == 1 && b == 1) {
      vec_ab.push_back(i);
    } else if (a == 1) {
      vec_a.push_back(i);
    } else if (b == 1) {
      vec_b.push_back(i);
    } else {
      vec_remains.push_back(i);
    }
  }

 // check if impossible
  if (
      sz(vec_a) + sz(vec_ab) < k ||
      sz(vec_b) + sz(vec_ab) < k
     ) {
    printf("%d\n", -1);
    return 0;
  }

  std::function<bool(int32_t, int32_t)> cmp = [&vec_all](int32_t p, int32_t q) {
    return vec_all[ p ] < vec_all[ q ];
  };

  int32_t incommon = std::min(sz(vec_ab), k);
  int32_t selected_singular = k - incommon;
  int32_t to_select = m - (selected_singular * 2 + incommon);
  if (to_select < 0) {
    printf("%d\n", -1);
    return 0;
  }

  std::sort(all(vec_a), cmp);
  std::sort(all(vec_b), cmp);
  std::sort(all(vec_ab), cmp);
  std::sort(all(vec_remains), cmp);

  int32_t k_time = 0;
  forby (i, 0, incommon) {
    k_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, 0, selected_singular) {
    k_time += vec_all[ vec_a[ i ] ];
    k_time += vec_all[ vec_b[ i ] ];
  }

  std::multiset<int32_t, decltype(cmp)> m_in(cmp);
  std::multiset<int32_t, decltype(cmp)> m_out(cmp);
  int32_t ms_time = 0;
  forby (i, incommon, sz(vec_ab)) {
    m_in.insert(vec_ab[ i ]);
    ms_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, selected_singular, sz(vec_a)) {
    m_in.insert(vec_a[ i ]);
    ms_time += vec_all[ vec_a[ i ] ];
  }
  forby (i, selected_singular, sz(vec_b)) {
    m_in.insert(vec_b[ i ]);
    ms_time += vec_all[ vec_b[ i ] ];
  }
  forby (i, 0, sz(vec_remains)) {
    ms_time += vec_all[ vec_remains[ i ] ];
    m_in.insert(vec_remains[ i ]);
  }
  while (sz(m_in) > to_select) {
    ms_time -= vec_all[ *prev(m_in, 1) ];
    m_out.insert(*prev(m_in, 1));
    m_in.erase(prev(m_in, 1));
  }

  int32_t total_time = inf;
  auto balance = [&vec_all, &m_in, &m_out, &ms_time](int32_t to_select) {
    while (sz(m_in) < to_select) {
      ms_time += vec_all[ *forward(m_out, 0) ];
      m_in.insert(*forward(m_out, 0));
      m_out.erase(forward(m_out, 0));
    }
    while (sz(m_in) > to_select) {
      ms_time -= vec_all[ *prev(m_in, 1) ];
      m_out.insert(*prev(m_in, 1));
      m_in.erase(prev(m_in, 1));
    }
    while (sz(m_in) > 0 && sz(m_out) > 0 && vec_all[ *prev(m_in, 1) ] > vec_all[ *forward(m_out, 0) ]) {
      int32_t tmp_in = *prev(m_in, 1);
      int32_t tmp_out = *forward(m_out, 0);
      m_in.erase(prev(m_in, 1));
      m_out.erase(forward(m_out, 0));
      m_in.insert(tmp_out);
      m_out.insert(tmp_in);
      ms_time -= vec_all[ tmp_in ];
      ms_time += vec_all[ tmp_out ];
    }
  };
  while (true) {

    int32_t current_time = k_time + ms_time;
    // if we pass the correct answer
    if (current_time > total_time) {
      selected_singular--;
      decltype(m_in)::iterator it_ab = m_in.find(vec_ab[ incommon ]);
      if (it_ab == m_in.end()) {
        m_out.erase(m_out.find(*it_ab));
      } else {
        ms_time -= vec_all[ *it_ab ];
        m_in.erase(it_ab);
      }
      m_in.insert(vec_a[ selected_singular ]);
      m_in.insert(vec_b[ selected_singular ]);
      ms_time += vec_all[ vec_a[ selected_singular ] ];
      ms_time += vec_all[ vec_b[ selected_singular ] ];
      balance(to_select + 1);
      break;
    }
    total_time = current_time;

    if (
        incommon == 0 ||
        selected_singular == std::min(sz(vec_a), sz(vec_b)) ||
        to_select == 0
    ) {
      break;
    }

    incommon--;
    k_time -= vec_all[ vec_ab[ incommon ] ];
    k_time += vec_all[ vec_a[ selected_singular ] ];
    k_time += vec_all[ vec_b[ selected_singular ] ];
    to_select--;
    decltype(m_in)::iterator it_a = m_in.find(vec_a[ selected_singular ]);
    if (it_a == m_in.end()) {
      m_out.erase(m_out.find(*it_a));
    } else {
      ms_time -= vec_all[ vec_a[ selected_singular ] ];
      m_in.erase(it_a);
    }
    decltype(m_in)::iterator it_b = m_in.find(vec_b[ selected_singular ]);
    if (it_b == m_in.end()) {
      m_out.erase(m_out.find(*it_b));
    } else {
      ms_time -= vec_all[ vec_b[ selected_singular ] ];
      m_in.erase(it_b);
    }

    m_out.insert(vec_ab[ incommon ]);
    balance(to_select);
    selected_singular++;
  }

  printf("%d\n", total_time);
  forby(i, 0, incommon) {
    printf("%d ", vec_ab[ i ] + 1);
  }
  forby(i, 0, selected_singular) {
    printf("%d %d ", vec_a[ i ] + 1, vec_b[ i ] + 1);
  }
  for (int32_t i : m_in) {
    printf("%d ", i + 1);
  }
  printf("\n");

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define forby(i, f, n) for (int32_t (i) = (f); i < n; i++)
#define sz(x) static_cast<int32_t>(x.size())
#define vec std::vector<int32_t>
#define forward(it, i) std::next(it.begin(), i)
#define prev(it, i) std::prev(it.end(), i)
#define inf static_cast<int32_t>(2e31)

int main(int argc, char** argv) {

  int32_t n, m, k;
  scanf("%d%d%d", &n, &m, &k);

  vec vec_a;
  vec vec_b;
  vec vec_ab;
  vec vec_remains;
  vec vec_all;

  vec_all.reserve(n);

  forby (i, 0, n) {
    int32_t t, a, b;
    scanf("%d%d%d", &t, &a, &b);

    vec_all.push_back(t);

  if (a == 1 && b == 1) {
      vec_ab.push_back(i);
    } else if (a == 1) {
      vec_a.push_back(i);
    } else if (b == 1) {
      vec_b.push_back(i);
    } else {
      vec_remains.push_back(i);
    }
  }

 // check if impossible
  if (
      sz(vec_a) + sz(vec_ab) < k ||
      sz(vec_b) + sz(vec_ab) < k
     ) {
    printf("%d\n", -1);
    return 0;
  }

  std::function<bool(int32_t, int32_t)> cmp = [&vec_all](int32_t p, int32_t q) {
    return vec_all[ p ] < vec_all[ q ];
  };

  std::sort(all(vec_a), cmp);
  std::sort(all(vec_b), cmp);
  std::sort(all(vec_ab), cmp);
  std::sort(all(vec_remains), cmp);

  int32_t incommon = std::min(sz(vec_ab), k);
  int32_t selected_singular = k - incommon;
  int32_t to_select = m - (selected_singular * 2 + incommon);

  int32_t k_time = 0;
  forby (i, 0, incommon) {
    k_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, 0, selected_singular) {
    k_time += vec_all[ vec_a[ i ] ];
    k_time += vec_all[ vec_b[ i ] ];
  }

  std::multiset<int32_t, decltype(cmp)> m_in(cmp);
  std::multiset<int32_t, decltype(cmp)> m_out(cmp);
  int32_t ms_time = 0;
  forby (i, incommon, std::min(sz(vec_ab), to_select)) {
    m_in.insert(vec_ab[ i ]);
    ms_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, selected_singular, std::min(sz(vec_a), to_select)) {
    m_in.insert(vec_a[ i ]);
    ms_time += vec_all[ vec_a[ i ] ];
  }
  forby (i, selected_singular, std::min(sz(vec_b), to_select)) {
    m_in.insert(vec_b[ i ]);
    ms_time += vec_all[ vec_b[ i ] ];
  }
  int32_t selected_remains = std::min(sz(vec_remains), to_select);
  forby (i, 0, sz(vec_remains)) {
    if (i < to_select) {
      ms_time += vec_all[ vec_remains[ i ] ];
      m_in.insert(vec_remains[ i ]);
    } else {
      m_out.insert(vec_remains[ i ]);
    }
  }
  while (sz(m_in) > to_select) {
    ms_time -= vec_all[ *prev(m_in, 1) ];
    m_out.insert(*prev(m_in, 1));
    m_in.erase(prev(m_in, 1));
  }

  int32_t total_time = inf;
  while (true) {

    int32_t current_time = k_time + ms_time;
    if (current_time > total_time) {
      break;
    }
    total_time = current_time;

    if (
        incommon == 0 ||
        selected_singular == std::min(sz(vec_a), sz(vec_b)) ||
        to_select == 0
    ) {
      break;
    }

    incommon--;
    k_time -= vec_all[ vec_ab[ incommon ] ];
    k_time += vec_all[ vec_a[ selected_singular ] ];
    k_time += vec_all[ vec_b[ selected_singular ] ];
    to_select--;
    decltype(m_in)::iterator it_a = m_in.find(vec_a[ selected_singular ]);
    if (it_a != m_in.end()) {
      ms_time -= vec_all[ vec_a[ selected_singular ] ];
      m_in.erase(it_a);
    }
    decltype(m_in)::iterator it_b = m_in.find(vec_b[ selected_singular ]);
    if (it_b != m_in.end()) {
      ms_time -= vec_all[ vec_b[ selected_singular ] ];
      m_in.erase(it_b);
    }

    while (sz(m_in) < to_select) {
      ms_time += vec_all[ *forward(m_out, 0) ];
      m_in.insert(*forward(m_out, 0));
      m_out.erase(forward(m_out, 0));
    }
    while (sz(m_in) > to_select) {
      ms_time -= vec_all[ *prev(m_in, 1) ];
      m_in.erase(prev(m_in, 1));
    }

    selected_singular++;
  }

  printf("%d\n", total_time);
  forby(i, 0, incommon) {
    printf("%d ", vec_ab[ i ] + 1);
  }
  forby(i, 0, selected_singular) {
    printf("%d %d ", vec_a[ i ] + 1, vec_b[ i ] + 1);
  }
  for (int32_t i : m_in) {
    printf("%d ", i + 1);
  }
  printf("\n");

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define forby(i, f, n) for (int32_t (i) = (f); i < n; i++)
#define sz(x) static_cast<int32_t>(x.size())
#define vec std::vector<int32_t>
#define forward(it, i) std::next(it.begin(), i)
#define prev(it, i) std::prev(it.end(), i)
#define inf static_cast<int32_t>(2e31)

int main(int argc, char** argv) {

  int32_t n, m, k;
  scanf("%d%d%d", &n, &m, &k);

  vec vec_a;
  vec vec_b;
  vec vec_ab;
  vec vec_remains;
  vec vec_all;

  vec_all.reserve(n);

  forby (i, 0, n) {
    int32_t t, a, b;
    scanf("%d%d%d", &t, &a, &b);

    vec_all.push_back(t);

  if (a == 1 && b == 1) {
      vec_ab.push_back(i);
    } else if (a == 1) {
      vec_a.push_back(i);
    } else if (b == 1) {
      vec_b.push_back(i);
    } else {
      vec_remains.push_back(i);
    }
  }

 // check if impossible
  if (
      sz(vec_a) + sz(vec_ab) < k ||
      sz(vec_b) + sz(vec_ab) < k
     ) {
    printf("%d\n", -1);
    return 0;
  }

  std::function<bool(int32_t, int32_t)> cmp = [&vec_all](int32_t p, int32_t q) {
    return vec_all[ p ] < vec_all[ q ];
  };

  int32_t incommon = std::min(sz(vec_ab), k);
  int32_t selected_singular = k - incommon;
  int32_t to_select = m - (selected_singular * 2 + incommon);
  if (to_select < 0) {
    printf("%d\n", -1);
    return 0;
  }

  std::sort(all(vec_a), cmp);
  std::sort(all(vec_b), cmp);
  std::sort(all(vec_ab), cmp);
  std::sort(all(vec_remains), cmp);

  int32_t k_time = 0;
  forby (i, 0, incommon) {
    k_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, 0, selected_singular) {
    k_time += vec_all[ vec_a[ i ] ];
    k_time += vec_all[ vec_b[ i ] ];
  }

  std::multiset<int32_t, decltype(cmp)> m_in(cmp);
  std::multiset<int32_t, decltype(cmp)> m_out(cmp);
  int32_t ms_time = 0;
  forby (i, incommon, sz(vec_ab)) {
    m_in.insert(vec_ab[ i ]);
    ms_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, selected_singular, sz(vec_a)) {
    m_in.insert(vec_a[ i ]);
    ms_time += vec_all[ vec_a[ i ] ];
  }
  forby (i, selected_singular, sz(vec_b)) {
    m_in.insert(vec_b[ i ]);
    ms_time += vec_all[ vec_b[ i ] ];
  }
  forby (i, 0, sz(vec_remains)) {
    ms_time += vec_all[ vec_remains[ i ] ];
    m_in.insert(vec_remains[ i ]);
  }
  while (sz(m_in) > to_select) {
    ms_time -= vec_all[ *prev(m_in, 1) ];
    m_out.insert(*prev(m_in, 1));
    m_in.erase(prev(m_in, 1));
  }

  int32_t total_time = inf;
  while (true) {

    int32_t current_time = k_time + ms_time;
    if (current_time > total_time) {
      break;
    }
    total_time = current_time;

    if (
        incommon == 0 ||
        selected_singular == std::min(sz(vec_a), sz(vec_b)) ||
        to_select == 0
    ) {
      break;
    }

    incommon--;
    k_time -= vec_all[ vec_ab[ incommon ] ];
    k_time += vec_all[ vec_a[ selected_singular ] ];
    k_time += vec_all[ vec_b[ selected_singular ] ];
    to_select--;
    decltype(m_in)::iterator it_a = m_in.find(vec_a[ selected_singular ]);
    if (it_a == m_in.end()) {
      m_out.erase(m_out.find(*it_a));
    } else {
      ms_time -= vec_all[ vec_a[ selected_singular ] ];
      m_in.erase(it_a);
    }
    decltype(m_in)::iterator it_b = m_in.find(vec_b[ selected_singular ]);
    if (it_b == m_in.end()) {
      m_out.erase(m_out.find(*it_b));
    } else {
      ms_time -= vec_all[ vec_b[ selected_singular ] ];
      m_in.erase(it_b);
    }
    decltype(m_in)::iterator it_ab = m_in.find(vec_ab[ incommon ]);
    if (it_ab != m_in.end()) {
      ms_time += vec_all[ vec_ab[ incommon ] ];
      m_in.insert(vec_ab[ incommon ]);
    }

    while (sz(m_in) < to_select) {
      ms_time += vec_all[ *forward(m_out, 0) ];
      m_in.insert(*forward(m_out, 0));
      m_out.erase(forward(m_out, 0));
    }
    while (sz(m_in) > to_select) {
      ms_time -= vec_all[ *prev(m_in, 1) ];
      m_out.insert(*prev(m_in, 1));
      m_in.erase(prev(m_in, 1));
    }
    while (vec_all[ *prev(m_in, 1) ] > vec_all[ *forward(m_out, 0) ]) {
      int32_t tmp_in = *prev(m_in, 1);
      int32_t tmp_out = *forward(m_out, 0);
      m_in.erase(prev(m_in, 1));
      m_out.erase(forward(m_out, 0));
      m_in.insert(tmp_out);
      m_out.insert(tmp_in);
      ms_time -= vec_all[ tmp_in ];
      ms_time += vec_all[ tmp_out ];
    }

    selected_singular++;
  }

  printf("%d\n", total_time);
  forby(i, 0, incommon) {
    printf("%d ", vec_ab[ i ] + 1);
  }
  forby(i, 0, selected_singular) {
    printf("%d %d ", vec_a[ i ] + 1, vec_b[ i ] + 1);
  }
  for (int32_t i : m_in) {
    printf("%d ", i + 1);
  }
  printf("\n");

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define forby(i, f, n) for (int32_t (i) = (f); i < n; i++)
#define sz(x) static_cast<int32_t>(x.size())
#define vec std::vector<int32_t>
#define forward(it, i) std::next(it.begin(), i)
#define prev(it, i) std::prev(it.end(), i)
#define inf static_cast<int32_t>(2e31)

int main(int argc, char** argv) {

  int32_t n, m, k;
  scanf("%d%d%d", &n, &m, &k);

  vec vec_a;
  vec vec_b;
  vec vec_ab;
  vec vec_remains;
  vec vec_all;

  vec_all.reserve(n);

  forby (i, 0, n) {
    int32_t t, a, b;
    scanf("%d%d%d", &t, &a, &b);

    vec_all.push_back(t);

  if (a == 1 && b == 1) {
      vec_ab.push_back(i);
    } else if (a == 1) {
      vec_a.push_back(i);
    } else if (b == 1) {
      vec_b.push_back(i);
    } else {
      vec_remains.push_back(i);
    }
  }

 // check if impossible
  if (
      sz(vec_a) + sz(vec_ab) < k ||
      sz(vec_b) + sz(vec_ab) < k
     ) {
    printf("%d\n", -1);
    return 0;
  }

  std::function<bool(int32_t, int32_t)> cmp = [&vec_all](int32_t p, int32_t q) {
    return vec_all[ p ] < vec_all[ q ];
  };

  int32_t incommon = std::min(sz(vec_ab), k);
  int32_t selected_singular = k - incommon;
  int32_t to_select = m - (selected_singular * 2 + incommon);
  if (to_select < 0) {
    printf("%d\n", -1);
    return 0;
  }

  std::sort(all(vec_a), cmp);
  std::sort(all(vec_b), cmp);
  std::sort(all(vec_ab), cmp);
  std::sort(all(vec_remains), cmp);

  int32_t k_time = 0;
  forby (i, 0, incommon) {
    k_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, 0, selected_singular) {
    k_time += vec_all[ vec_a[ i ] ];
    k_time += vec_all[ vec_b[ i ] ];
  }

  std::multiset<int32_t, decltype(cmp)> m_in(cmp);
  std::multiset<int32_t, decltype(cmp)> m_out(cmp);
  int32_t ms_time = 0;
  forby (i, incommon, std::min(sz(vec_ab), incommon + to_select)) {
    m_in.insert(vec_ab[ i ]);
    ms_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, selected_singular, sz(vec_a)) {
    m_in.insert(vec_a[ i ]);
    ms_time += vec_all[ vec_a[ i ] ];
  }
  forby (i, selected_singular, sz(vec_b)) {
    m_in.insert(vec_b[ i ]);
    ms_time += vec_all[ vec_b[ i ] ];
  }
  forby (i, 0, sz(vec_remains)) {
    if (i < to_select) {
      ms_time += vec_all[ vec_remains[ i ] ];
      m_in.insert(vec_remains[ i ]);
    } else {
      m_out.insert(vec_remains[ i ]);
    }
  }
  while (sz(m_in) > to_select) {
    ms_time -= vec_all[ *prev(m_in, 1) ];
    m_out.insert(*prev(m_in, 1));
    m_in.erase(prev(m_in, 1));
  }

  int32_t total_time = inf;
  while (true) {

    int32_t current_time = k_time + ms_time;
    if (current_time > total_time) {
      break;
    }
    total_time = current_time;

    if (
        incommon == 0 ||
        selected_singular == std::min(sz(vec_a), sz(vec_b)) ||
        to_select == 0
    ) {
      break;
    }

    incommon--;
    k_time -= vec_all[ vec_ab[ incommon ] ];
    k_time += vec_all[ vec_a[ selected_singular ] ];
    k_time += vec_all[ vec_b[ selected_singular ] ];
    to_select--;
    decltype(m_in)::iterator it_a = m_in.find(vec_a[ selected_singular ]);
    if (it_a != m_in.end()) {
      ms_time -= vec_all[ vec_a[ selected_singular ] ];
      m_in.erase(it_a);
    }
    decltype(m_in)::iterator it_b = m_in.find(vec_b[ selected_singular ]);
    if (it_b != m_in.end()) {
      ms_time -= vec_all[ vec_b[ selected_singular ] ];
      m_in.erase(it_b);
    }
    decltype(m_in)::iterator it_ab = m_in.find(vec_ab[ incommon ]);
    if (it_ab != m_in.end()) {
      ms_time += vec_all[ vec_ab[ incommon ] ];
      m_in.insert(vec_ab[ incommon ]);
    }

    while (sz(m_in) < to_select) {
      ms_time += vec_all[ *forward(m_out, 0) ];
      m_in.insert(*forward(m_out, 0));
      m_out.erase(forward(m_out, 0));
    }
    while (sz(m_in) > to_select) {
      ms_time -= vec_all[ *prev(m_in, 1) ];
      m_in.erase(prev(m_in, 1));
    }

    selected_singular++;
  }

  printf("%d\n", total_time);
  forby(i, 0, incommon) {
    printf("%d ", vec_ab[ i ] + 1);
  }
  forby(i, 0, selected_singular) {
    printf("%d %d ", vec_a[ i ] + 1, vec_b[ i ] + 1);
  }
  for (int32_t i : m_in) {
    printf("%d ", i + 1);
  }
  printf("\n");

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define forby(i, f, n) for (int32_t (i) = (f); i < n; i++)
#define sz(x) static_cast<int32_t>(x.size())
#define vec std::vector<int32_t>
#define forward(it, i) std::next(it.begin(), i)
#define prev(it, i) std::prev(it.end(), i)
#define inf static_cast<int32_t>(2e30)

int main(int argc, char** argv) {

  int32_t n, m, k;
  scanf("%d%d%d", &n, &m, &k);

  vec vec_a;
  vec vec_b;
  vec vec_ab;
  vec vec_remains;
  vec vec_all;

  vec_all.reserve(n);

  forby (i, 0, n) {
    int32_t t, a, b;
    scanf("%d%d%d", &t, &a, &b);

    vec_all.push_back(t);

  if (a == 1 && b == 1) {
      vec_ab.push_back(i);
    } else if (a == 1) {
      vec_a.push_back(i);
    } else if (b == 1) {
      vec_b.push_back(i);
    } else {
      vec_remains.push_back(i);
    }
  }

 // check if impossible
  if (
      sz(vec_a) + sz(vec_ab) < k ||
      sz(vec_b) + sz(vec_ab) < k
     ) {
    printf("%d\n", -1);
    return 0;
  }

  std::function<bool(int32_t, int32_t)> cmp = [&vec_all](int32_t p, int32_t q) {
    return vec_all[ p ] < vec_all[ q ];
  };

  int32_t incommon = std::min(sz(vec_ab), k);
  int32_t selected_singular = k - incommon;
  int32_t to_select = m - (selected_singular * 2 + incommon);
  if (to_select < 0) {
    printf("%d\n", -1);
    return 0;
  }

  std::sort(all(vec_a), cmp);
  std::sort(all(vec_b), cmp);
  std::sort(all(vec_ab), cmp);
  std::sort(all(vec_remains), cmp);

  int32_t k_time = 0;
  forby (i, 0, incommon) {
    k_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, 0, selected_singular) {
    k_time += vec_all[ vec_a[ i ] ];
    k_time += vec_all[ vec_b[ i ] ];
  }

  std::multiset<int32_t, decltype(cmp)> m_in(cmp);
  std::multiset<int32_t, decltype(cmp)> m_out(cmp);
  int32_t ms_time = 0;
  forby (i, incommon, sz(vec_ab)) {
    m_in.insert(vec_ab[ i ]);
    ms_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, selected_singular, sz(vec_a)) {
    m_in.insert(vec_a[ i ]);
    ms_time += vec_all[ vec_a[ i ] ];
  }
  forby (i, selected_singular, sz(vec_b)) {
    m_in.insert(vec_b[ i ]);
    ms_time += vec_all[ vec_b[ i ] ];
  }
  forby (i, 0, sz(vec_remains)) {
    ms_time += vec_all[ vec_remains[ i ] ];
    m_in.insert(vec_remains[ i ]);
  }
  while (sz(m_in) > to_select) {
    ms_time -= vec_all[ *prev(m_in, 1) ];
    m_out.insert(*prev(m_in, 1));
    m_in.erase(prev(m_in, 1));
  }

  int32_t total_time = inf;
  auto balance = [&vec_all, &m_in, &m_out, &ms_time](int32_t to_select) {
    while (sz(m_in) < to_select) {
      ms_time += vec_all[ *forward(m_out, 0) ];
      m_in.insert(*forward(m_out, 0));
      m_out.erase(forward(m_out, 0));
    }
    while (sz(m_in) > to_select) {
      ms_time -= vec_all[ *prev(m_in, 1) ];
      m_out.insert(*prev(m_in, 1));
      m_in.erase(prev(m_in, 1));
    }
    while (sz(m_in) > 0 && sz(m_out) > 0 && vec_all[ *prev(m_in, 1) ] > vec_all[ *forward(m_out, 0) ]) {
      int32_t tmp_in = *prev(m_in, 1);
      int32_t tmp_out = *forward(m_out, 0);
      m_in.erase(prev(m_in, 1));
      m_out.erase(forward(m_out, 0));
      m_in.insert(tmp_out);
      m_out.insert(tmp_in);
      ms_time -= vec_all[ tmp_in ];
      ms_time += vec_all[ tmp_out ];
    }
  };
  while (true) {

    int32_t current_time = k_time + ms_time;
    // if we pass the correct answer
    if (current_time > total_time) {
      selected_singular--;
      decltype(m_in)::iterator it_ab = m_in.find(vec_ab[ incommon ]);
      if (it_ab == m_in.end()) {
        m_out.erase(m_out.find(*it_ab));
      } else {
        ms_time -= vec_all[ *it_ab ];
        m_in.erase(it_ab);
      }
      m_in.insert(vec_a[ selected_singular ]);
      m_in.insert(vec_b[ selected_singular ]);
      ms_time += vec_all[ vec_a[ selected_singular ] ];
      ms_time += vec_all[ vec_b[ selected_singular ] ];
      balance(to_select + 1);
      break;
    }
    total_time = current_time;

    if (
        incommon == 0 ||
        selected_singular == std::min(sz(vec_a), sz(vec_b)) ||
        to_select == 0
    ) {
      break;
    }

    incommon--;
    k_time -= vec_all[ vec_ab[ incommon ] ];
    k_time += vec_all[ vec_a[ selected_singular ] ];
    k_time += vec_all[ vec_b[ selected_singular ] ];
    to_select--;
    decltype(m_in)::iterator it_a = m_in.find(vec_a[ selected_singular ]);
    if (it_a == m_in.end()) {
      m_out.erase(m_out.find(*it_a));
    } else {
      ms_time -= vec_all[ vec_a[ selected_singular ] ];
      m_in.erase(it_a);
    }
    decltype(m_in)::iterator it_b = m_in.find(vec_b[ selected_singular ]);
    if (it_b == m_in.end()) {
      m_out.erase(m_out.find(*it_b));
    } else {
      ms_time -= vec_all[ vec_b[ selected_singular ] ];
      m_in.erase(it_b);
    }

    m_out.insert(vec_ab[ incommon ]);
    balance(to_select);
    selected_singular++;
  }

  printf("%d\n", total_time);
  forby(i, 0, incommon) {
    printf("%d ", vec_ab[ i ] + 1);
  }
  forby(i, 0, selected_singular) {
    printf("%d %d ", vec_a[ i ] + 1, vec_b[ i ] + 1);
  }
  for (int32_t i : m_in) {
    printf("%d ", i + 1);
  }
  printf("\n");

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define forby(i, f, n) for (int32_t (i) = (f); i < n; i++)
#define sz(x) static_cast<int32_t>(x.size())
#define vec std::vector<int32_t>
#define forward(it, i) std::next(it.begin(), i)
#define prev(it, i) std::prev(it.end(), i)
#define inf static_cast<int32_t>(2e31)

int main(int argc, char** argv) {

  int32_t n, m, k;
  scanf("%d%d%d", &n, &m, &k);

  vec vec_a;
  vec vec_b;
  vec vec_ab;
  vec vec_remains;
  vec vec_all;

  vec_all.reserve(n);

  forby (i, 0, n) {
    int32_t t, a, b;
    scanf("%d%d%d", &t, &a, &b);

    vec_all.push_back(t);

  if (a == 1 && b == 1) {
      vec_ab.push_back(i);
    } else if (a == 1) {
      vec_a.push_back(i);
    } else if (b == 1) {
      vec_b.push_back(i);
    } else {
      vec_remains.push_back(i);
    }
  }

 // check if impossible
  if (
      sz(vec_a) + sz(vec_ab) < k ||
      sz(vec_b) + sz(vec_ab) < k
     ) {
    printf("%d\n", -1);
    return 0;
  }

  std::function<bool(int32_t, int32_t)> cmp = [&vec_all](int32_t p, int32_t q) {
    return vec_all[ p ] < vec_all[ q ];
  };

  int32_t incommon = std::min(sz(vec_ab), k);
  int32_t selected_singular = k - incommon;
  int32_t to_select = m - (selected_singular * 2 + incommon);
  if (to_select < 0) {
    printf("%d\n", -1);
    return 0;
  }

  std::sort(all(vec_a), cmp);
  std::sort(all(vec_b), cmp);
  std::sort(all(vec_ab), cmp);
  std::sort(all(vec_remains), cmp);

  int32_t k_time = 0;
  forby (i, 0, incommon) {
    k_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, 0, selected_singular) {
    k_time += vec_all[ vec_a[ i ] ];
    k_time += vec_all[ vec_b[ i ] ];
  }

  std::multiset<int32_t, decltype(cmp)> m_in(cmp);
  std::multiset<int32_t, decltype(cmp)> m_out(cmp);
  int32_t ms_time = 0;
  forby (i, incommon, std::min(sz(vec_ab), to_select)) {
    m_in.insert(vec_ab[ i ]);
    ms_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, selected_singular, std::min(sz(vec_a), to_select)) {
    m_in.insert(vec_a[ i ]);
    ms_time += vec_all[ vec_a[ i ] ];
  }
  forby (i, selected_singular, std::min(sz(vec_b), to_select)) {
    m_in.insert(vec_b[ i ]);
    ms_time += vec_all[ vec_b[ i ] ];
  }
  int32_t selected_remains = std::min(sz(vec_remains), to_select);
  forby (i, 0, sz(vec_remains)) {
    if (i < to_select) {
      ms_time += vec_all[ vec_remains[ i ] ];
      m_in.insert(vec_remains[ i ]);
    } else {
      m_out.insert(vec_remains[ i ]);
    }
  }
  while (sz(m_in) > to_select) {
    ms_time -= vec_all[ *prev(m_in, 1) ];
    m_out.insert(*prev(m_in, 1));
    m_in.erase(prev(m_in, 1));
  }

  int32_t total_time = inf;
  while (true) {

    int32_t current_time = k_time + ms_time;
    if (current_time > total_time) {
      break;
    }
    total_time = current_time;

    if (
        incommon == 0 ||
        selected_singular == std::min(sz(vec_a), sz(vec_b)) ||
        to_select == 0
    ) {
      break;
    }

    incommon--;
    k_time -= vec_all[ vec_ab[ incommon ] ];
    k_time += vec_all[ vec_a[ selected_singular ] ];
    k_time += vec_all[ vec_b[ selected_singular ] ];
    to_select--;
    decltype(m_in)::iterator it_a = m_in.find(vec_a[ selected_singular ]);
    if (it_a != m_in.end()) {
      ms_time -= vec_all[ vec_a[ selected_singular ] ];
      m_in.erase(it_a);
    }
    decltype(m_in)::iterator it_b = m_in.find(vec_b[ selected_singular ]);
    if (it_b != m_in.end()) {
      ms_time -= vec_all[ vec_b[ selected_singular ] ];
      m_in.erase(it_b);
    }

    while (sz(m_in) < to_select) {
      ms_time += vec_all[ *forward(m_out, 0) ];
      m_in.insert(*forward(m_out, 0));
      m_out.erase(forward(m_out, 0));
    }
    while (sz(m_in) > to_select) {
      ms_time -= vec_all[ *prev(m_in, 1) ];
      m_in.erase(prev(m_in, 1));
    }

    selected_singular++;
  }

  printf("%d\n", total_time);
  forby(i, 0, incommon) {
    printf("%d ", vec_ab[ i ] + 1);
  }
  forby(i, 0, selected_singular) {
    printf("%d %d ", vec_a[ i ] + 1, vec_b[ i ] + 1);
  }
  for (int32_t i : m_in) {
    printf("%d ", i + 1);
  }
  printf("\n");

  return 0;
}


#include <vector>
#include <bits/stdc++.h>
#include <iostream>
#define forby(i, a, z) for (int32_t (i) = a; i < z; i++)
#define vec std::vector<int32_t>
#define size(x) static_cast<int32_t>(x.size())
#define first(x) std::next(x.begin(), 0)
#define last(x) std::prev(x.end(), 1)
#define all(x) x.begin(), x.end()

int main(int argc, char** argv) {

  int32_t n, m, k;
  scanf("%d%d%d", &n, &m, &k);

  vec va;
  vec vb;
  vec vab;
  vec vall;
  vec vr;

  auto cmp = [&vall](int32_t p, int32_t q) {
    return vall[ p ] < vall[ q ];
  };

  std::multiset<int32_t, decltype(cmp)> setout(cmp);
  forby(i, 0, n) {
    int32_t t, a, b;
    scanf("%d%d%d", &t, &a, &b);

    vall.push_back(t);
    if (a && b) {
      vab.push_back(i);
    } else if (a) {
      va.push_back(i);
    } else if (b) {
      vb.push_back(i);
    } else {
      vr.push_back(i);
    }
  }
  forby (i, 0, size(vr)) {
    setout.insert(vr[ i ]);
  }
  std::sort(all(va), cmp);
  std::sort(all(vb), cmp);
  std::sort(all(vab), cmp);
  if (size(va) + size(vab) < k || size(vb) + size(vab) < k) {
    printf("-1\n");
    return 0;
  }

  int32_t incommon = std::min(size(vab), k);
  int32_t singular = k - incommon;

  if (singular > std::min(size(va), size(vb))) {
    printf("-1\n");
    return 0;
  }

  int32_t selected = (singular * 2) + incommon;
  if (selected > m) {
    printf("-1\n");
    return 0;
  }

  std::multiset<int32_t, decltype(cmp)> setin(cmp);
  int32_t settime = 0;
  auto balance = [&vall, &setin, &setout, &settime](int32_t n) {
    while (size(setin) < n) {
      auto f = first(setout);
      setin.insert(*f);
      setout.erase(f);
      settime += vall[ *f ];
    }
    while (size(setin) > n) {
      auto l = last(setin);
      setout.insert(*l);
      setin.erase(l);
      settime -= vall[ *l ];
    }
    while (size(setin) > 0 && size(setout) > 0 && *last(setin) > *first(setout)) {
      auto f = first(setout);
      auto l = last(setin);
      setout.insert(*l);
      setin.insert(*f);
      settime -= vall[ *l ];
      settime += vall[ *f ];
      setout.erase(f);
      setin.erase(l);
    }
  };

  int32_t ktime = 0;
  forby (i, 0, incommon) {
    ktime += vall[ vab[ i ] ];
  }
  forby(i, 0, singular) {
    ktime += (vall[ va[ i ] ] + vall[ vb[ i ] ]);
  }

  int32_t to_select = m - selected;
  forby (i, incommon, size(vab)) {
    setin.insert(vab[ i ]);
    settime += vall[ vab[ i ] ];
  }
  forby (i, singular, size(va)) {
    setin.insert(va[ i ]);
    settime += vall[ va[ i ] ];
  }
  forby (i, singular, size(vb)) {
    setin.insert(vb[ i ]);
    settime += vall[ vb[ i ] ];
  }
  balance(to_select);
  int32_t totaltime = ktime + settime;
  int32_t bestincommon = incommon;
  while (incommon > 0 && singular < std::min(size(va), size(vb)) && to_select > 0) {
    incommon--;
    ktime -= vall[ vab[ incommon ] ];

    auto setait = setin.find(va[ singular ]);
    if (setait == setin.end()) {
      setout.erase(setout.find(va[ singular ]));
    } else {
      setin.erase(setait);
      settime -= vall[ va[singular ] ];
    }
    auto setbit = setin.find(vb[ singular ]);
    if (setbit == setin.end()) {
      setout.erase(setout.find(vb[ singular ]));
    } else {
      setin.erase(setbit);
      settime -= vall[ vb[ singular ] ];
    }

    ktime += vall[ va[ singular ] ] + vall[ vb[ singular ] ];
    singular++;
    to_select--;

    balance(to_select);

    int32_t currentltime = ktime + settime;
    if (currentltime < totaltime) {
      totaltime = currentltime;
      bestincommon = incommon;
    }
  }

  int32_t bestsingular = k - bestincommon;
  int32_t bestremains = m - bestincommon - (bestsingular * 2);
  forby (i, bestincommon, size(vab)) {
    vr.push_back(vab[ i ]);
  }
  forby (i, bestsingular, size(va)) {
    vr.push_back(va[ i ]);
  }
  forby (i, bestsingular, size(vb)) {
    vr.push_back(vb[ i ]);
  }
  std::sort(all(vr), cmp);

  // answer
  printf("%d\n", totaltime);
  forby (i, 0, bestincommon) {
    printf("%d ", vab[ i ] + 1);
  }
  forby (i, 0, bestsingular) {
    printf("%d %d ", va[ i ] + 1, vb[ i ] + 1);
  }
  forby (i, 0, bestremains) {
    printf("%d ", vr[ i ] + 1);
  }
  printf("\n");

  return 0;
}


#include <vector>
#include <bits/stdc++.h>
#include <iostream>
#define forby(i, a, z) for (int32_t (i) = a; i < z; i++)
#define vec std::vector<int32_t>
#define size(x) static_cast<int32_t>(x.size())
#define first(x) std::next(x.begin(), 0)
#define last(x) std::prev(x.end(), 1)
#define all(x) x.begin(), x.end()

int main(int argc, char** argv) {

  int32_t n, m, k;
  scanf("%d%d%d", &n, &m, &k);

  vec va;
  vec vb;
  vec vab;
  vec vall;
  vec vr;

  auto cmp = [&vall](int32_t p, int32_t q) {
    return vall[ p ] < vall[ q ];
  };

  std::multiset<int32_t, decltype(cmp)> setout(cmp);
  forby(i, 0, n) {
    int32_t t, a, b;
    scanf("%d%d%d", &t, &a, &b);

    vall.push_back(t);
    if (a && b) {
      vab.push_back(i);
    } else if (a) {
      va.push_back(i);
    } else if (b) {
      vb.push_back(i);
    } else {
      vr.push_back(i);
    }
  }

  std::sort(all(va), cmp);
  std::sort(all(vb), cmp);
  std::sort(all(vab), cmp);
  if (size(va) + size(vab) < k || size(vb) + size(vab) < k) {
    printf("-1\n");
    return 0;
  }

  int32_t incommon = std::min(size(vab), k);
  int32_t singular = k - incommon;

  if (singular > std::min(size(va), size(vb))) {
    printf("-1\n");
    return 0;
  }

  int32_t selected = (singular * 2) + incommon;
  if (selected > m) {
    printf("-1\n");
    return 0;
  }

  std::multiset<int32_t, decltype(cmp)> setin(cmp);
  int32_t settime = 0;
  auto balance = [&vall, &setin, &setout, &settime](int32_t n) {
    while (size(setin) < n) {
      auto f = first(setout);
      settime += vall[ *f ];
      setin.insert(*f);
      setout.erase(f);
    }
    while (size(setin) > n) {
      auto l = last(setin);
      settime -= vall[ *l ];
      setout.insert(*l);
      setin.erase(l);
    }
    while (size(setin) > 0 && size(setout) > 0 && *last(setin) > *first(setout)) {
      auto f = first(setout);
      auto l = last(setin);
      settime -= vall[ *l ];
      settime += vall[ *f ];
      setout.insert(*l);
      setin.insert(*f);
      setout.erase(f);
      setin.erase(l);
    }
  };

  int32_t ktime = 0;
  forby (i, 0, incommon) {
    ktime += vall[ vab[ i ] ];
  }
  forby(i, 0, singular) {
    ktime += vall[ va[ i ] ] + vall[ vb[ i ] ];
  }

  int32_t to_select = m - selected;
  forby (i, incommon, size(vab)) {
    setin.insert(vab[ i ]);
    settime += vall[ vab[ i ] ];
  }
  forby (i, singular, size(va)) {
    setin.insert(va[ i ]);
    settime += vall[ va[ i ] ];
  }
  forby (i, singular, size(vb)) {
    setin.insert(vb[ i ]);
    settime += vall[ vb[ i ] ];
  }
  forby (i, 0, size(vr)) {
    setout.insert(vr[ i ]);
  }
  balance(to_select);
  int32_t totaltime = ktime + settime;
  int32_t bestincommon = incommon;
  while (incommon > 0 && singular < std::min(size(va), size(vb)) && to_select > 0) {
    incommon--;
    ktime -= vall[ vab[ incommon ] ];
    setout.insert(vab[ incommon ]);

    auto setait = setin.find(va[ singular ]);
    if (setait == setin.end()) {
      setout.erase(setout.find(va[ singular ]));
    } else {
      setin.erase(setait);
      settime -= vall[ va[singular ] ];
    }
    auto setbit = setin.find(vb[ singular ]);
    if (setbit == setin.end()) {
      setout.erase(setout.find(vb[ singular ]));
    } else {
      setin.erase(setbit);
      settime -= vall[ vb[ singular ] ];
    }

    ktime += vall[ va[ singular ] ] + vall[ vb[ singular ] ];
    singular++;
    to_select--;

    balance(to_select);

    int32_t currenttime = ktime + settime;
    if (currenttime < totaltime) {
      totaltime = currenttime;
      bestincommon = incommon;
    }
  }

  int32_t bestsingular = k - bestincommon;
  int32_t bestremains = m - bestincommon - (bestsingular * 2);
  forby (i, bestincommon, size(vab)) {
    vr.push_back(vab[ i ]);
  }
  forby (i, bestsingular, size(va)) {
    vr.push_back(va[ i ]);
  }
  forby (i, bestsingular, size(vb)) {
    vr.push_back(vb[ i ]);
  }
  std::sort(all(vr), cmp);

  // answer
  printf("%d\n", totaltime);
  forby (i, 0, bestincommon) {
    printf("%d ", vab[ i ] + 1);
  }
  forby (i, 0, bestsingular) {
    printf("%d %d ", va[ i ] + 1, vb[ i ] + 1);
  }
  forby (i, 0, bestremains) {
    printf("%d ", vr[ i ] + 1);
  }
  printf("\n");

  return 0;
}


#include <vector>
#include <bits/stdc++.h>
#include <iostream>
#define forby(i, a, z) for (int32_t (i) = a; i < z; i++)
#define vec std::vector<int32_t>
#define size(x) static_cast<int32_t>(x.size())
#define first(x) std::next(x.begin(), 0)
#define last(x) std::prev(x.end(), 1)
#define all(x) x.begin(), x.end()

int main(int argc, char** argv) {

  int32_t n, m, k;
  scanf("%d%d%d", &n, &m, &k);

  vec va;
  vec vb;
  vec vab;
  vec vall;
  vec vr;

  auto cmp = [&vall](int32_t p, int32_t q) {
    return vall[ p ] < vall[ q ];
  };

  std::multiset<int32_t, decltype(cmp)> setout(cmp);
  forby(i, 0, n) {
    int32_t t, a, b;
    scanf("%d%d%d", &t, &a, &b);

    vall.push_back(t);
    if (a && b) {
      vab.push_back(i);
    } else if (a) {
      va.push_back(i);
    } else if (b) {
      vb.push_back(i);
    } else {
      vr.push_back(i);
    }
  }

  std::sort(all(va), cmp);
  std::sort(all(vb), cmp);
  std::sort(all(vab), cmp);
  if (size(va) + size(vab) < k || size(vb) + size(vab) < k) {
    printf("%d\n", -1);
    return 0;
  }

  int32_t incommon = std::min(size(vab), k);
  int32_t singular = k - incommon;

  if (singular > std::min(size(va), size(vb))) {
    printf("%d\n", -1);
    return 0;
  }

  int32_t selected = (singular * 2) + incommon;
  if (selected > m) {
    printf("%d\n", -1);
    return 0;
  }

  std::multiset<int32_t, decltype(cmp)> setin(cmp);
  int32_t settime = 0;
  auto balance = [&vall, &setin, &setout, &settime](int32_t n) {
    while (size(setin) < n) {
      auto f = first(setout);
      settime += vall[ *f ];
      setin.insert(*f);
      setout.erase(f);
    }
    while (size(setin) > n) {
      auto l = last(setin);
      settime -= vall[ *l ];
      setout.insert(*l);
      setin.erase(l);
    }
    while (size(setin) > 0 && size(setout) > 0 && vall[ *last(setin) ] > vall[ *first(setout) ]) {
      auto f = first(setout);
      auto l = last(setin);
      settime -= vall[ *l ];
      settime += vall[ *f ];
      setout.insert(*l);
      setin.insert(*f);
      setout.erase(f);
      setin.erase(l);
    }
  };

  int32_t ktime = 0;
  forby (i, 0, incommon) {
    ktime += vall[ vab[ i ] ];
  }
  forby(i, 0, singular) {
    ktime += vall[ va[ i ] ] + vall[ vb[ i ] ];
  }

  int32_t to_select = m - selected;
  forby (i, incommon, size(vab)) {
    setin.insert(vab[ i ]);
    settime += vall[ vab[ i ] ];
  }
  forby (i, singular, size(va)) {
    setin.insert(va[ i ]);
    settime += vall[ va[ i ] ];
  }
  forby (i, singular, size(vb)) {
    setin.insert(vb[ i ]);
    settime += vall[ vb[ i ] ];
  }
  forby (i, 0, size(vr)) {
    setout.insert(vr[ i ]);
  }
  balance(to_select);
  int32_t totaltime = ktime + settime;
  int32_t bestincommon = incommon;
  while (incommon > 0 && singular < std::min(size(va), size(vb)) && to_select > 0) {
    incommon--;
    ktime -= vall[ vab[ incommon ] ];
    setout.insert(vab[ incommon ]);

    auto setait = setin.find(va[ singular ]);
    if (setait == setin.end()) {
      setout.erase(setout.find(va[ singular ]));
    } else {
      setin.erase(setait);
      settime -= vall[ va[singular ] ];
    }
    auto setbit = setin.find(vb[ singular ]);
    if (setbit == setin.end()) {
      setout.erase(setout.find(vb[ singular ]));
    } else {
      setin.erase(setbit);
      settime -= vall[ vb[ singular ] ];
    }

    ktime += vall[ va[ singular ] ] + vall[ vb[ singular ] ];
    singular++;
    to_select--;

    balance(to_select);

    int32_t currenttime = ktime + settime;
    if (currenttime < totaltime) {
      totaltime = currenttime;
      bestincommon = incommon;
    }
  }

  int32_t bestsingular = k - bestincommon;
  int32_t bestremains = m - bestincommon - (bestsingular * 2);
  forby (i, bestincommon, size(vab)) {
    vr.push_back(vab[ i ]);
  }
  forby (i, bestsingular, size(va)) {
    vr.push_back(va[ i ]);
  }
  forby (i, bestsingular, size(vb)) {
    vr.push_back(vb[ i ]);
  }
  std::sort(all(vr), cmp);

  // answer
  printf("%d\n", totaltime);
  forby (i, 0, bestincommon) {
    printf("%d ", vab[ i ] + 1);
  }
  forby (i, 0, bestsingular) {
    printf("%d %d ", va[ i ] + 1, vb[ i ] + 1);
  }
  forby (i, 0, bestremains) {
    printf("%d ", vr[ i ] + 1);
  }
  printf("\n");

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define forby(i, f, n) for (int32_t (i) = (f); i < n; i++)
#define sz(x) static_cast<int32_t>(x.size())
#define vec std::vector<int32_t>
#define forward(it, i) std::next(it.begin(), i)
#define inf static_cast<int32_t>(2e31)

int main(int argc, char** argv) {

  int32_t n, m, k;
  scanf("%d%d%d", &n, &m, &k);

  vec vec_a;
  vec vec_b;
  vec vec_ab;
  vec vec_all;
  vec vec_remains;

  forby (i, 0, n) {
    int32_t t, a, b;
    scanf("%d%d%d", &t, &a, &b);

    vec_all.push_back(t);

    if (a == 1 && b == 1) {
      vec_ab.push_back(i);
    } else if (a == 1) {
      vec_a.push_back(i);
    } else if (b == 1) {
      vec_b.push_back(i);
    } else {
      vec_remains.push_back(i);
    }
  }

  // check if impossible
  if (
      sz(vec_a) + sz(vec_ab) < k ||
      sz(vec_b) + sz(vec_ab) < k
     ) {
    printf("%d\n", -1);
    return 0;
  }

  std::function<bool(int32_t, int32_t)> cmp = [&vec_all](int32_t p, int32_t q) {
    return vec_all[ p ] < vec_all[ q ];
  };

  std::sort(all(vec_a), cmp);
  std::sort(all(vec_b), cmp);
  std::sort(all(vec_ab), cmp);
  std::sort(all(vec_remains), cmp);

  int32_t possible_singular = std::min(std::min(sz(vec_a), sz(vec_b)), k);
  int32_t incommon = k - possible_singular;

  std::multiset<int32_t, decltype(cmp)> ms(cmp);

  forby(i, possible_singular, sz(vec_a)) {
    ms.insert(vec_a[ i ]);
  }
  forby(i, possible_singular, sz(vec_b)) {
    ms.insert(vec_b[ i ]);
  }
  forby(i, incommon, sz(vec_ab)) {
    ms.insert(vec_ab[ i ]);
  }
  forby(i, 0, sz(vec_remains)) {
    ms.insert(vec_remains[ i ]);
  }

  // initial state
  int32_t best_incommon = -1;
  int32_t total_time = inf;
  int32_t calculated_time = 0;
  forby (i, 0, possible_singular) {
    calculated_time += vec_all[ vec_a[ i ] ];
    calculated_time += vec_all[ vec_b[ i ] ];
  }
  forby (i, 0, incommon) {
    calculated_time += vec_all[ vec_ab[ i ] ];
  }
  int32_t selected = possible_singular * 2 + incommon;
  while (incommon <= std::min(m, sz(vec_ab))) {

    if (selected <= m) {
      int32_t current_time = calculated_time;
      forby (i, 0, m - selected) {
        current_time += vec_all[ *forward(ms, i) ];
      }

      if (current_time < total_time) {
        total_time = current_time;
        best_incommon = incommon;
      }
    }

    if (incommon < std::min(m, sz(vec_ab))) {
      calculated_time += vec_all[ vec_ab[ incommon ] ];
      ms.erase(vec_ab[ incommon ]);
      possible_singular--;
      if (possible_singular >= 0) {
        calculated_time -= vec_all[ vec_a[ possible_singular ] ];
        calculated_time -= vec_all[ vec_b[ possible_singular ] ];
        ms.insert(vec_a[ possible_singular ]);
        ms.insert(vec_b[ possible_singular ]);
        selected--;
      } else {
        selected++;
      }
    }
    incommon++;
  }

  // calculate result with best_incommon
  if (best_incommon == -1) {
    printf("%d\n", -1);
    return 0;
  }

  ms.clear();
  std::vector<int32_t> result;
  result.reserve(m);
  result.insert(result.end(), vec_a.begin(), vec_a.begin() + k - best_incommon);
  result.insert(result.end(), vec_b.begin(), vec_b.begin() + k - best_incommon);
  result.insert(result.end(), vec_ab.begin(), vec_ab.begin() + best_incommon);

  int32_t missing = m - ((k - best_incommon) * 2 + best_incommon);
  if (missing > 0) {
    forby(i, k - best_incommon, std::min(k - best_incommon + missing, sz(vec_a))) {
      ms.insert(vec_a[ i ]);
    }
    forby(i, k - best_incommon, std::min(k - best_incommon + missing, sz(vec_b))) {
      ms.insert(vec_b[ i ]);
    }
    forby(i, best_incommon, std::min(best_incommon + missing, sz(vec_ab))) {
      ms.insert(vec_ab[ i ]);
    }
    forby(i, 0, std::min(missing, sz(vec_remains))) {
      ms.insert(vec_remains[ i ]);
    }

    forby(i, 0, missing) {
      result.push_back(*forward(ms, i));
    }
  }

  printf("%d\n", total_time);
  for (int32_t i : result) {
    printf("%d ", i + 1);
  }
  printf("\n");

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define forby(i, f, n) for (int32_t (i) = (f); i < n; i++)
#define sz(x) static_cast<int32_t>(x.size())
#define vec std::vector<int32_t>
#define forward(it, i) std::next(it.begin(), i)
#define prev(it, i) std::prev(it.end(), i)
#define inf static_cast<int32_t>(2e31)

int main(int argc, char** argv) {

  int32_t n, m, k;
  scanf("%d%d%d", &n, &m, &k);

  vec vec_a;
  vec vec_b;
  vec vec_ab;
  vec vec_remains;
  vec vec_all;

  vec_all.reserve(n);

  forby (i, 0, n) {
    int32_t t, a, b;
    scanf("%d%d%d", &t, &a, &b);

    vec_all.push_back(t);

  if (a == 1 && b == 1) {
      vec_ab.push_back(i);
    } else if (a == 1) {
      vec_a.push_back(i);
    } else if (b == 1) {
      vec_b.push_back(i);
    } else {
      vec_remains.push_back(i);
    }
  }

 // check if impossible
  if (
      sz(vec_a) + sz(vec_ab) < k ||
      sz(vec_b) + sz(vec_ab) < k
     ) {
    printf("%d\n", -1);
    return 0;
  }

  std::function<bool(int32_t, int32_t)> cmp = [&vec_all](int32_t p, int32_t q) {
    return vec_all[ p ] < vec_all[ q ];
  };

  int32_t incommon = std::min(sz(vec_ab), k);
  int32_t selected_singular = k - incommon;
  int32_t to_select = m - (selected_singular * 2 + incommon);
  if (to_select < 0) {
    printf("%d\n", -1);
    return 0;
  }

  std::sort(all(vec_a), cmp);
  std::sort(all(vec_b), cmp);
  std::sort(all(vec_ab), cmp);
  std::sort(all(vec_remains), cmp);

  int32_t k_time = 0;
  forby (i, 0, incommon) {
    k_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, 0, selected_singular) {
    k_time += vec_all[ vec_a[ i ] ];
    k_time += vec_all[ vec_b[ i ] ];
  }

  std::multiset<int32_t, decltype(cmp)> m_in(cmp);
  std::multiset<int32_t, decltype(cmp)> m_out(cmp);
  int32_t ms_time = 0;
  forby (i, incommon, std::min(sz(vec_ab), incommon + to_select)) {
    m_in.insert(vec_ab[ i ]);
    ms_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, selected_singular, sz(vec_a)) {
    m_in.insert(vec_a[ i ]);
    ms_time += vec_all[ vec_a[ i ] ];
  }
  forby (i, selected_singular, sz(vec_b)) {
    m_in.insert(vec_b[ i ]);
    ms_time += vec_all[ vec_b[ i ] ];
  }
  forby (i, 0, sz(vec_remains)) {
    if (i < to_select) {
      ms_time += vec_all[ vec_remains[ i ] ];
      m_in.insert(vec_remains[ i ]);
    } else {
      m_out.insert(vec_remains[ i ]);
    }
  }
  while (sz(m_in) > to_select) {
    ms_time -= vec_all[ *prev(m_in, 1) ];
    m_out.insert(*prev(m_in, 1));
    m_in.erase(prev(m_in, 1));
  }

  int32_t total_time = inf;
  while (true) {

    int32_t current_time = k_time + ms_time;
    if (current_time > total_time) {
      break;
    }
    total_time = current_time;

    if (
        incommon == 0 ||
        selected_singular == std::min(sz(vec_a), sz(vec_b)) ||
        to_select == 0
    ) {
      break;
    }

    incommon--;
    k_time -= vec_all[ vec_ab[ incommon ] ];
    k_time += vec_all[ vec_a[ selected_singular ] ];
    k_time += vec_all[ vec_b[ selected_singular ] ];
    to_select--;
    decltype(m_in)::iterator it_a = m_in.find(vec_a[ selected_singular ]);
    if (it_a != m_in.end()) {
      ms_time -= vec_all[ vec_a[ selected_singular ] ];
      m_in.erase(it_a);
    }
    decltype(m_in)::iterator it_b = m_in.find(vec_b[ selected_singular ]);
    if (it_b != m_in.end()) {
      ms_time -= vec_all[ vec_b[ selected_singular ] ];
      m_in.erase(it_b);
    }
    decltype(m_in)::iterator it_ab = m_in.find(vec_ab[ incommon ]);
    if (it_ab != m_in.end()) {
      ms_time += vec_all[ vec_ab[ incommon ] ];
      m_in.insert(vec_ab[ incommon ]);
    }

    while (sz(m_in) < to_select) {
      ms_time += vec_all[ *forward(m_out, 0) ];
      m_in.insert(*forward(m_out, 0));
      m_out.erase(forward(m_out, 0));
    }
    while (sz(m_in) > to_select) {
      ms_time -= vec_all[ *prev(m_in, 1) ];
      m_out.insert(*prev(m_in, 1));
      m_in.erase(prev(m_in, 1));
    }

    selected_singular++;
  }

  printf("%d\n", total_time);
  forby(i, 0, incommon) {
    printf("%d ", vec_ab[ i ] + 1);
  }
  forby(i, 0, selected_singular) {
    printf("%d %d ", vec_a[ i ] + 1, vec_b[ i ] + 1);
  }
  for (int32_t i : m_in) {
    printf("%d ", i + 1);
  }
  printf("\n");

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define forby(i, f, n) for (int32_t (i) = (f); i < n; i++)
#define sz(x) static_cast<int32_t>(x.size())
#define vec std::vector<int32_t>
#define forward(it, i) std::next(it.begin(), i)
#define prev(it, i) std::prev(it.end(), i)
#define inf 2147483647

int main(int argc, char** argv) {

  int32_t n, m, k;
  scanf("%d%d%d", &n, &m, &k);

  vec vec_a;
  vec vec_b;
  vec vec_ab;
  vec vec_remains;
  vec vec_all;

  vec_all.reserve(n);

  forby (i, 0, n) {
    int32_t t, a, b;
    scanf("%d%d%d", &t, &a, &b);

    vec_all.push_back(t);

  if (a == 1 && b == 1) {
      vec_ab.push_back(i);
    } else if (a == 1) {
      vec_a.push_back(i);
    } else if (b == 1) {
      vec_b.push_back(i);
    } else {
      vec_remains.push_back(i);
    }
  }

 // check if impossible
  if (
      sz(vec_a) + sz(vec_ab) < k ||
      sz(vec_b) + sz(vec_ab) < k
     ) {
    printf("%d\n", -1);
    return 0;
  }

  std::function<bool(int32_t, int32_t)> cmp = [&vec_all](int32_t p, int32_t q) {
    return vec_all[ p ] < vec_all[ q ];
  };

  int32_t incommon = std::min(sz(vec_ab), k);
  int32_t selected_singular = k - incommon;
  int32_t to_select = m - (selected_singular * 2 + incommon);
  if (to_select < 0) {
    printf("%d\n", -1);
    return 0;
  }

  std::sort(all(vec_a), cmp);
  std::sort(all(vec_b), cmp);
  std::sort(all(vec_ab), cmp);
  std::sort(all(vec_remains), cmp);

  int32_t k_time = 0;
  forby (i, 0, incommon) {
    k_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, 0, selected_singular) {
    k_time += vec_all[ vec_a[ i ] ];
    k_time += vec_all[ vec_b[ i ] ];
  }

  std::multiset<int32_t, decltype(cmp)> m_in(cmp);
  std::multiset<int32_t, decltype(cmp)> m_out(cmp);
  int32_t ms_time = 0;
  forby (i, incommon, sz(vec_ab)) {
    m_in.insert(vec_ab[ i ]);
    ms_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, selected_singular, sz(vec_a)) {
    m_in.insert(vec_a[ i ]);
    ms_time += vec_all[ vec_a[ i ] ];
  }
  forby (i, selected_singular, sz(vec_b)) {
    m_in.insert(vec_b[ i ]);
    ms_time += vec_all[ vec_b[ i ] ];
  }
  forby (i, 0, sz(vec_remains)) {
    ms_time += vec_all[ vec_remains[ i ] ];
    m_in.insert(vec_remains[ i ]);
  }
  while (sz(m_in) > to_select) {
    ms_time -= vec_all[ *prev(m_in, 1) ];
    m_out.insert(*prev(m_in, 1));
    m_in.erase(prev(m_in, 1));
  }

  int32_t total_time = inf;
  auto balance = [&vec_all, &m_in, &m_out, &ms_time](int32_t to_select) {
    while (sz(m_in) < to_select) {
      ms_time += vec_all[ *forward(m_out, 0) ];
      m_in.insert(*forward(m_out, 0));
      m_out.erase(forward(m_out, 0));
    }
    while (sz(m_in) > to_select) {
      ms_time -= vec_all[ *prev(m_in, 1) ];
      m_out.insert(*prev(m_in, 1));
      m_in.erase(prev(m_in, 1));
    }
    while (sz(m_in) > 0 && sz(m_out) > 0 && vec_all[ *prev(m_in, 1) ] > vec_all[ *forward(m_out, 0) ]) {
      int32_t tmp_in = *prev(m_in, 1);
      int32_t tmp_out = *forward(m_out, 0);
      m_in.erase(prev(m_in, 1));
      m_out.erase(forward(m_out, 0));
      m_in.insert(tmp_out);
      m_out.insert(tmp_in);
      ms_time -= vec_all[ tmp_in ];
      ms_time += vec_all[ tmp_out ];
    }
  };
  while (true) {

    int32_t current_time = k_time + ms_time;
    // if we pass the correct answer
    if (current_time > total_time) {
      selected_singular--;
      decltype(m_in)::iterator it_ab = m_in.find(vec_ab[ incommon ]);
      if (it_ab == m_in.end()) {
        m_out.erase(m_out.find(*it_ab));
      } else {
        m_in.erase(it_ab);
      }
      m_in.insert(vec_a[ selected_singular ]);
      m_in.insert(vec_b[ selected_singular ]);
      balance(to_select + 1);
      incommon++;
      break;
    }
    total_time = current_time;

    if (
        incommon == 0 ||
        selected_singular == std::min(sz(vec_a), sz(vec_b)) ||
        to_select == 0
    ) {
      break;
    }

    incommon--;
    k_time -= vec_all[ vec_ab[ incommon ] ];
    k_time += vec_all[ vec_a[ selected_singular ] ];
    k_time += vec_all[ vec_b[ selected_singular ] ];
    to_select--;
    decltype(m_in)::iterator it_a = m_in.find(vec_a[ selected_singular ]);
    if (it_a == m_in.end()) {
      m_out.erase(m_out.find(*it_a));
    } else {
      ms_time -= vec_all[ vec_a[ selected_singular ] ];
      m_in.erase(it_a);
    }
    decltype(m_in)::iterator it_b = m_in.find(vec_b[ selected_singular ]);
    if (it_b == m_in.end()) {
      m_out.erase(m_out.find(*it_b));
    } else {
      ms_time -= vec_all[ vec_b[ selected_singular ] ];
      m_in.erase(it_b);
    }

    m_out.insert(vec_ab[ incommon ]);
    balance(to_select);
    selected_singular++;
  }

  printf("%d\n", total_time);
  forby(i, 0, incommon) {
    printf("%d ", vec_ab[ i ] + 1);
  }
  forby(i, 0, selected_singular) {
    printf("%d %d ", vec_a[ i ] + 1, vec_b[ i ] + 1);
  }
  for (int32_t i : m_in) {
    printf("%d ", i + 1);
  }
  printf("\n");

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#define all(x) x.begin(), x.end()
#define vec std::vector<int32_t>

int32_t calcule(vec& vec_all, vec& vec_a, vec& vec_b, vec& vec_ab, int32_t k, int32_t incommon) {
  int32_t last_singular_id = k - 1 - incommon;
  return vec_all[ vec_ab[ incommon ] ] - vec_all[ vec_a[ last_singular_id ] ] - vec_all[ vec_b[ last_singular_id ] ];
}

int main(int argc, char** argv) {

  int32_t n, m, k;
  scanf("%d%d%d", &n, &m, &k);

  vec vec_a;
  vec vec_b;
  vec vec_ab;
  vec vec_all;
  vec vec_remains;

  for (int32_t i = 0; i < n; i++) {
    int32_t t, a, b;
    scanf("%d%d%d", &t, &a, &b);

    vec_all.push_back(t);

    if (a == 1 && b == 1) {
      vec_ab.push_back(i);
    } else if (a == 1) {
      vec_a.push_back(i);
    } else if (b == 1) {
      vec_b.push_back(i);
    } else {
      vec_remains.push_back(i);
    }
  }

  // check if impossible
  if (
      vec_a.size() + vec_ab.size() < k ||
      vec_b.size() + vec_ab.size() < k
     ) {
    printf("%d\n", -1);
    return 0;
  }

  std::function<bool(int32_t, int32_t)> cmp = [&vec_all](int32_t p, int32_t q) {
    return vec_all[ p ] < vec_all[ q ];
  };

  std::sort(all(vec_a), cmp);
  std::sort(all(vec_b), cmp);
  std::sort(all(vec_ab), cmp);

  int32_t incommon = 0;
  // initial state
  int32_t total_time = 0;
  if (vec_a.size() >= k && vec_b.size() >= k) {
    for (int32_t i = 0; i < k; i++) {
      total_time += vec_all[ vec_a[ i ] ] + vec_all[ vec_b[ i ] ];
    }
  } else {
    int32_t min = std::min(vec_a.size(), vec_b.size());
    for (int32_t i = 0; i < min; i++) {
      total_time += vec_all[ vec_a[ i ] ] + vec_all[ vec_b[ i ] ];
    }
    for (int32_t i = 0; i < k - min; i++) {
      total_time += vec_all[ vec_ab[ i ] ];
    }
    incommon = k - min;
  }

  while (incommon < k && incommon < vec_ab.size()) {
    int32_t current_time = total_time + calcule(vec_all, vec_a, vec_b, vec_ab, k, incommon);
    if (current_time >= total_time) {
      break;
    }

    incommon++;
  }

  std::vector<int32_t> result;
  int32_t selected = (k - incommon) * 2 + incommon;

  m -= static_cast<int32_t>(selected);
  if (m > 0) {
    int32_t from_p = k - incommon;
    int32_t to_p = from_p + m;
    int32_t ab_p = incommon + m;
    vec_remains.insert(vec_remains.end(), vec_a.begin() + from_p, to_p < vec_a.size() ? vec_a.begin() + to_p : vec_a.end());
    vec_remains.insert(vec_remains.end(), vec_b.begin() + from_p, to_p < vec_b.size() ? vec_b.begin() + to_p : vec_b.end());
    vec_remains.insert(vec_remains.end(), vec_ab.begin() + incommon, ab_p < vec_ab.size() ? vec_ab.begin() + ab_p : vec_ab.end());
    std::sort(all(vec_remains), cmp);

    result.reserve(result.size() + m);
    for (int32_t i = 0; i < m; i++) {
      result.push_back(vec_remains[ i ]);
    }

    m = 0;
  }
  m = std::abs(m);

  result.insert(result.end(), vec_a.begin(), vec_a.begin() + k - incommon - m);
  result.insert(result.end(), vec_b.begin(), vec_b.begin() + k - incommon - m);
  result.insert(result.end(), vec_ab.begin(), vec_ab.begin() + incommon + m);

  total_time = 0;
  for (int32_t i = 0; i < result.size(); i++) {
    total_time += vec_all[ result[ i ] ];
  }

  printf("%d\n", total_time);
  for (int32_t i : result) {
    printf("%d ", i + 1);
  }
  printf("\n");

  return 0;
}