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 some amount of 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 some 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 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 two integers n and k (1 ≤ k ≤ 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. Otherwise print one integer T — the minimum total reading time of the suitable set of books.

Examples

Input

8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0


Output

18


Input

5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0


Output

8


Input

5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1


Output

-1

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(int n) throws IOException {
        int[] nums = new int[n];
        int c = 0;
        for (int i = 0; i < n; i++) {
            int sign = 1;
            c = br.read();
            int x = 0;
            if (c == '-') {
                sign = -1;
                c = br.read();
            }
            while (c >= '0' && c <= '9') {
                x = x * 10 + c - '0';
                c = br.read();
            }
            nums[i] = x * sign;
        }
        while (c != '\n' && c != -1) c = br.read();
        return nums;
    }

    long[] rll(int n) throws IOException {
        long[] nums = new long[n];
        int c = 0;
        for (int i = 0; i < n; i++) {
            int sign = 1;
            c = br.read();
            long x = 0;
            if (c == '-') {
                sign = -1;
                c = br.read();
            }
            while (c >= '0' && c <= '9') {
                x = x * 10 + c - '0';
                c = br.read();
            }
            nums[i] = x * sign;
        }
        while (c != '\n' && c != -1) c = br.read();
        return nums;
    }

    int[] t;
    void solve() throws IOException {
        int[] nmk = ril(3);
        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(3);
            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;
    }
}