/*---------------Go Code GO---------------*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using Order_Set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//(order) Set.order_of_key(); (pointer) Set.find_by_order();

#define PI acos(-1.0)
#define O_O                           \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)
#define precision(a) fixed << setprecision(a)
#define endl '\n'
#define Flush cout << flush
#define LLMX 0x3fffffffffffffff

void Solution();

int main()
{
    // O_O;
    long long tst = 1;

    //cin >> tst;
    while (tst--)
    {
        Solution();
    }

    return 0;
}

void Solution()
{
    long long N, M, K;
    vector<pair<long long, long long>> both, ali, bob, none;

    cin >> N >> M >> K;

    long long timeBit[N + 2] = {0}, indexBit[N + 2] = {0};
    int likedAlice, likedBob, t, bitIndex;

    for (int i = 1; i <= N; i++)
    {
        cin >> t >> likedAlice >> likedBob;

        if (likedAlice && likedBob)
            both.push_back({t, i});
        else if (likedAlice)
            timeBit[i] = 1;
        else if (likedBob)
            timeBit[i] = 2;
        if (likedAlice + likedBob < 2)
            none.push_back({t, i});
    }
    both.push_back({0, 0});
    none.push_back({0, 0});
    sort(both.begin(), both.end());
    sort(none.begin(), none.end());

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

    ali.push_back({0, 0});
    bob.push_back({0, 0});
    for (int i = 1; i < none.size(); i++)
    {
        if (timeBit[none[i].second] == 1)
            ali.push_back({none[i].first + ali.back().first, i});
        else if (timeBit[none[i].second] == 2)
            bob.push_back({none[i].first + bob.back().first, i});
        timeBit[none[i].second] = 0;
    }

    for (int i = 1; i < none.size(); i++)
    {
        bitIndex = i;
        //cout << i << ": ";
        while (bitIndex < none.size())
        {
            //  cout << bitIndex << ' ';
            timeBit[bitIndex] += none[i].first;
            bitIndex += (bitIndex & -bitIndex);
        }
        //cout << endl;
    }

    int fromBoth, fromInd, others, othersSum;
    int resFromBoth, resFromInd, resFromOthers;

    bool isPossible = false;

    long long ans = LLMX;

    // if (N == 2187 && M == 78)                                                                        //k 42
    //     cout << none.size() << ' ' << ali.size() << ' ' << bob.size() << ' ' << both.size() << endl; //1004 79 878 1185
    for (int i = M; i >= 0; i--)
    {
        fromBoth = i;
        fromInd = max(0LL, K - i);
        others = M - 2 * fromInd - fromBoth;
        // cout << fromBoth << ' ' << fromInd << ' ' << others << endl;

        if (others < 0 || others >= none.size() || fromInd >= min(ali.size(), bob.size()) || fromBoth >= both.size())
            continue;

        bitIndex = ali[fromInd].second;
        while (bitIndex < none.size() && bitIndex > 0)
        {
            //cout << "update Ali " << bitIndex << endl;
            indexBit[bitIndex]++;
            timeBit[bitIndex] -= none[ali[fromInd].second].first;
            bitIndex += (bitIndex & -bitIndex);
        }

        bitIndex = bob[fromInd].second;
        while (bitIndex < none.size() && bitIndex > 0)
        {
            //cout << "update Bob" << endl;
            indexBit[bitIndex]++;
            timeBit[bitIndex] -= none[bob[fromInd].second].first;
            bitIndex += (bitIndex & -bitIndex);
        }

        int lw = others, hi = none.size() - 1, mid, found = 0;
        while (lw <= hi)
        {
            mid = (hi + lw) >> 1;
            bitIndex = mid;
            long long sum = 0;
            while (bitIndex > 0)
            {
                //cout << "update others" << endl;
                sum += indexBit[bitIndex];
                bitIndex -= (bitIndex & -bitIndex);
            }
            //cout << hi << ' ' << lw << ' ' << sum << ' ' << mid << endl;
            if (mid - sum < others)
                lw = mid + 1;
            else if (mid - sum > others)
                hi = mid - 1;
            if (mid - sum == others)
            {
                others = mid;
                found = 1;
                break;
            }
        }
        // if (N == 81 && M == 81)
        //     cout << mid << endl;
        // cout << others << endl;
        if (found == 0)
            continue;

        bitIndex = others;

        othersSum = 0;
        while (bitIndex > 0)
        {
            //cout << "update sum" << endl;
            othersSum += timeBit[bitIndex];
            bitIndex -= (bitIndex & -bitIndex);
        }
        long long total = both[fromBoth].first + ali[fromInd].first + bob[fromInd].first + othersSum;
        // cout << total << endl;
        if (ans > total)
        {
            isPossible = true;
            ans = total;
            resFromBoth = fromBoth;
            resFromInd = fromInd;
            resFromOthers = others;
            //cout << resFromBoth << ' ' << resFromInd << ' ' << resFromOthers << endl;
        }
    }
    if (isPossible == false)
    {
        cout << -1 << endl;
        return;
    }
    cout << ans << endl;
    for (int i = 1; i <= resFromBoth; i++)
        cout << both[i].second << ' ';
    for (int i = 1; i <= resFromInd; i++)
    {
        cout << none[ali[i].second].second << ' ' << none[bob[i].second].second << ' ';
        none[ali[i].second].second = 0;
        none[bob[i].second].second = 0;
    }
    for (int i = 1; i <= resFromOthers; i++)
        if (none[i].second != 0)
            cout << none[i].second << ' ';
    cout << endl;
}