Problem: Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.

Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read exactly m books before all entertainments. Alice and Bob will read each book together to end this exercise faster.

There are n books in the family library. The i-th book is described by three integers: t_i — the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).

So they need to choose exactly m books from the given n books in such a way that:

  * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; 
  * the total reading time of these m books is minimized (they are children and want to play and joy as soon a possible). 



The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.

Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.

Input

The first line of the input contains three integers n, m and k (1 ≤ k ≤ m ≤ n ≤ 2 ⋅ 10^5).

The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≤ t_i ≤ 10^4, 0 ≤ a_i, b_i ≤ 1), where:

  * t_i — the amount of time required for reading the i-th book; 
  * a_i equals 1 if Alice likes the i-th book and 0 otherwise; 
  * b_i equals 1 if Bob likes the i-th book and 0 otherwise. 

Output

If there is no solution, print only one integer -1.

If the solution exists, print T in the first line — the minimum total reading time of the suitable set of books. In the second line print m distinct integers from 1 to n in any order — indices of books which are in the set you found.

If there are several answers, print any of them.

Examples

Input

6 3 1
6 0 0
11 1 0
9 0 1
21 1 1
10 1 0
8 0 1


Output

24
6 5 1 

Input

6 3 2
6 0 0
11 1 0
9 0 1
21 1 1
10 1 0
8 0 1


Output

39
4 6 5 

Solution (in JAVA):

Solution:
import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    PrintWriter pw = new PrintWriter(System.out);
    static int MOD = 1000000007;

    public static void main(String[] args) throws IOException {
        Main m = new Main();
        m.solve();
        m.close();
    }

    void close() throws IOException {
        pw.flush();
        pw.close();
        br.close();
    }

    int ri() throws IOException {
        return Integer.parseInt(br.readLine());
    }

    long rl() throws IOException {
        return Long.parseLong(br.readLine());
    }

    int[] ril() throws IOException {
        String[] tokens = br.readLine().split(" ");
        int[] A = new int[tokens.length];
        for (int i = 0; i < A.length; i++)
            A[i] = Integer.parseInt(tokens[i]);
        return A;
    }
 
    long[] rll() throws IOException {
        String[] tokens = br.readLine().split(" ");
        long[] A = new long[tokens.length];
        for (int i = 0; i < A.length; i++)
            A[i] = Long.parseLong(tokens[i]);
        return A;
    }

    int[] t;
    void solve() throws IOException {
        int[] nmk = ril();
        int n = nmk[0];
        int m = nmk[1];
        int k = nmk[2];
        t = new int[n];
        List<Integer> a = new ArrayList<>();
        List<Integer> b = new ArrayList<>();
        List<Integer> c = new ArrayList<>();
        List<Integer> d = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            int[] tab = ril();
            t[i] = tab[0];
            if (tab[1] == 1 && tab[2] == 1) c.add(i);
            else if (tab[1] == 1) a.add(i);
            else if (tab[2] == 1) b.add(i);
            else d.add(i);
        }

        Collections.sort(a, (i1, i2) -> Integer.compare(t[i1], t[i2]));
        Collections.sort(b, (i1, i2) -> Integer.compare(t[i1], t[i2]));
        Collections.sort(c, (i1, i2) -> Integer.compare(t[i1], t[i2]));
        Collections.sort(d, (i1, i2) -> Integer.compare(t[i1], t[i2]));
        int[] aprefix = new int[a.size()+1];
        int[] bprefix = new int[b.size()+1];
        int[] cprefix = new int[c.size()+1];
        int[] dprefix = new int[d.size()+1];
        for (int i = 1; i <= a.size(); i++) aprefix[i] = aprefix[i-1] + t[a.get(i-1)];
        for (int i = 1; i <= b.size(); i++) bprefix[i] = bprefix[i-1] + t[b.get(i-1)];
        for (int i = 1; i <= c.size(); i++) cprefix[i] = cprefix[i-1] + t[c.get(i-1)];
        for (int i = 1; i <= d.size(); i++) dprefix[i] = dprefix[i-1] + t[d.get(i-1)];
        int best = Integer.MAX_VALUE;
        int[] ansOfBest = new int[2];  // [number to grab from c, min value for a/b/c]
        int[] aa = new int[a.size()]; for (int i = 0; i < a.size(); i++) aa[i] = a.get(i);
        int[] bb = new int[b.size()]; for (int i = 0; i < b.size(); i++) bb[i] = b.get(i);
        int[] cc = new int[c.size()]; for (int i = 0; i < c.size(); i++) cc[i] = c.get(i);
        int[] dd = new int[d.size()]; for (int i = 0; i < d.size(); i++) dd[i] = d.get(i);
        for (int i = 0; i <= Math.min(c.size(), m); i++) {
            int need = Math.max(0, k - i);
            if (2 * need + i > m) continue;
            if (need >= aprefix.length || need >= bprefix.length) continue;
            int cost = cprefix[i] + aprefix[need] + bprefix[need];
            int extras = m - 2 * need - i;
            // need to find smallest 'extras' numbers from a[need:], b[need:], c[i:], and d[need:]
            int l = 1;
            int r = 10000;
            int min = 10000;
            while (l <= r) {
                int mid = l + (r - l) / 2;
                int[] ret = helper(mid, aa, bb, cc, dd, need, need, i, 0);
                int count = ret[0] + ret[1] + ret[2] + ret[3];
                if (count >= extras) {
                    min = Math.min(min, mid);
                    r = mid - 1;
                } else {
                    l = mid + 1;
                }
            }
            // min is the good number now
            int[] ret = helper(min, aa, bb, cc, dd, need, need, i, 0);
            int count = ret[0] + ret[1] + ret[2] + ret[3];
            int excess = (count - extras) * min;
            cost += aprefix[need+ret[0]] - aprefix[need];
            cost += bprefix[need+ret[1]] - bprefix[need];
            cost += cprefix[i+ret[2]] - cprefix[i];
            cost += dprefix[ret[3]];
            cost -= excess;

            if (cost < best) {
                best = cost;
                ansOfBest[0] = i;
                ansOfBest[1] = min;
            }
        }
        if (best == Integer.MAX_VALUE) {
            pw.println("-1");
        } else {
            pw.println(best);
            // initial c's to get
            for (int i = 0; i < ansOfBest[0]; i++) pw.print((cc[i]+1) + " ");
            // requirement 1 a's and b's to get
            int need = Math.max(0, k - ansOfBest[0]);
            for (int i = 0; i < need; i++) {
                pw.print((aa[i]+1) + " ");
                pw.print((bb[i]+1) + " ");
            }
            // finally, the extras
            int ai = need;
            int bi = need;
            int ci = ansOfBest[0];
            int di = 0;
            int extras = Math.max(0, m - 2 * need - ansOfBest[0]);
            for (int i = 0; i < extras; i++) {
                int ta = ai == aa.length ? Integer.MAX_VALUE : t[aa[ai]];
                int tb = bi == bb.length ? Integer.MAX_VALUE : t[bb[bi]];
                int tc = ci == cc.length ? Integer.MAX_VALUE : t[cc[ci]];
                int td = di == dd.length ? Integer.MAX_VALUE : t[dd[di]];
                int min = Math.min(Math.min(Math.min(ta, tb), tc), td);
                if (ta == min) {
                    pw.print((aa[ai++]+1) + " ");
                } else if (tb == min) {
                    pw.print((bb[bi++]+1) + " ");
                } else if (tc == min) {
                    pw.print((cc[ci++]+1) + " ");
                } else {
                    pw.print((dd[di++]+1) + " ");
                }
            }
            pw.println();
        }
    }

    // returns number of eligible elems <= x from each list
    int[] helper(int x, int[] a, int[] b, int[] c, int[] d, int la, int lb, int lc, int ld) {
        int[] ret = new int[4];

        int ia = binarySearchRight(a, la, a.length-1, x);
        if (ia >= 0) ret[0] = Math.max(0, ia - la + 1);
        else ret[0] = Math.max(0, (-ia-1) - la);
        
        int ib = binarySearchRight(b, lb, b.length-1, x);
        if (ib >= 0) ret[1] = Math.max(0, ib - lb + 1);
        else ret[1] = Math.max(0, (-ib-1) - lb);

        int ic = binarySearchRight(c, lc, c.length-1, x);
        if (ic >= 0) ret[2] = Math.max(0, ic - lc + 1);
        else ret[2] = Math.max(0, (-ic-1) - lc);

        int id = binarySearchRight(d, ld, d.length-1, x);
        if (id >= 0) ret[3] = Math.max(0, id - ld + 1);
        else ret[3] = Math.max(0, (-id-1) - ld);

        return ret;
    }

    public int binarySearchRight(int[] A, int l, int r, int k) {
        if (l >= A.length) return -1;
        int upper = -1;
        while (l <= r) {
            int m = l + (r - l) / 2;
            if (t[A[m]] == k && (m+1==A.length || t[A[m+1]] > k)) {
                upper = m;
                break;
            }
            if (t[A[m]] <= k) {
                l = m + 1;
            } else {
                r = m - 1;
            }
        }
        return upper >= 0 ? upper : -l - 1;
    }
}