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.util.*;
import java.math.*;

public class E2 {
    static final boolean RUN_TIMING = false;
    static char[] inputBuffer = new char[1 << 20];
    static PushbackReader in = new PushbackReader(new BufferedReader(new InputStreamReader(System.in)), 1 << 20);
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

    public void go() throws IOException {
        // in = new PushbackReader(new BufferedReader(new FileReader(new File("test.txt"))), 1 << 20);
        // out = new PrintWriter(new FileWriter(new File("output.txt")));
        int n = ipar();
        int m = ipar();
        int k = ipar();
        ArrayList<ArrayList<int[]>> books = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            books.add(new ArrayList<>());
        }
        for (int i = 0; i < n; i++) {
            int t = ipar();
            int a = ipar();
            int b = ipar();
            books.get(a*2+b).add(new int[]{t, i});
        }
        for (int i = 0; i < 4; i++) {
            Collections.sort(books.get(i), this::compare);
        }
        int sum1 = 0, sum2 = 0, sum3 = 0;
        int index1 = -1, index2 = -1;
        for (int i = 0; i < k; i++) {
            if (index1+1 < books.get(1).size()) {
                index1++;
                sum1 += books.get(1).get(index1)[0];
            }
            if (index2+1 < books.get(2).size()) {
                index2++;
                sum2 += books.get(2).get(index2)[0];
            }
        }
        PriorityQueue<int[]> candidates = new PriorityQueue<>(this::compare);
        for (int i = k; i < books.get(1).size(); i++) {
            candidates.add(books.get(1).get(i));
        }
        for (int i = k; i < books.get(2).size(); i++) {
            candidates.add(books.get(2).get(i));
        }
        for (int i = 0; i < books.get(0).size(); i++) {
            candidates.add(books.get(0).get(i));
        }
        TreeSet<int[]> free = new TreeSet<>(this::compare);
        int freeSum = 0;
        while (index1+index2+2+free.size() < m && !candidates.isEmpty()) {
            int[] add = candidates.remove();
            free.add(add);
            freeSum += add[0];
        }
        while (index1+index2+2+free.size() > m && !free.isEmpty()) {
            int[] rem = free.last();
            free.remove(rem);
            freeSum -= rem[0];
            candidates.add(rem);
        }
        // out.printf("%d %d %d %d  :  %d %d %d%n", sum1, sum2, sum3, freeSum, index1, index2, -1);
        int best = Integer.MAX_VALUE;
        int bestIndex = -2;
        if (index1+index2+2+free.size() == m && index1+1 == k && index2+1 == k) {
            best = sum1+sum2+freeSum;
            bestIndex = -1;
        }
        int index3 = -1;
        while (index3+1 < books.get(3).size()) {
            index3++;
            sum3 += books.get(3).get(index3)[0];
            if (index1 >= 0 && index1+index3+2 > k) {
                sum1 -= books.get(1).get(index1)[0];
                candidates.add(books.get(1).get(index1));
                index1--;
                if (!free.isEmpty()) {
                    int[] rem = free.last();
                    free.remove(rem);
                    freeSum -= rem[0];
                    candidates.add(rem);
                    int[] add = candidates.remove();
                    free.add(add);
                    freeSum += add[0];
                }
            }
            if (index2 >= 0 && index2+index3+2 > k) {
                sum2 -= books.get(2).get(index2)[0];
                candidates.add(books.get(2).get(index2));
                index2--;
                if (!free.isEmpty()) {
                    int[] rem = free.last();
                    free.remove(rem);
                    freeSum -= rem[0];
                    candidates.add(rem);
                    int[] add = candidates.remove();
                    free.add(add);
                    freeSum += add[0];
                }
            }
            while (index1+index2+index3+3+free.size() < m && !candidates.isEmpty()) {
                int[] add = candidates.remove();
                free.add(add);
                freeSum += add[0];
            }
            while (index1+index2+index3+3+free.size() > m && !free.isEmpty()) {
                int[] rem = free.last();
                free.remove(rem);
                freeSum -= rem[0];
                candidates.add(rem);
            }
            if (index1+index2+index3+3+free.size() == m && index1+index3+2 >= k && index2+index3+2 >= k && sum1+sum2+sum3+freeSum < best) {
                best = sum1+sum2+sum3+freeSum;
                bestIndex = index3;
            }
            // out.printf("%d %d %d %d  :  %d %d %d%n", sum1, sum2, sum3, freeSum, index1, index2, index3);
        }
        // out.printf("%d %d%n", best, bestIndex);
        sum1 = sum2 = sum3 = 0;
        index1 = index2 = -1;
        for (int i = 0; i < k; i++) {
            if (index1+1 < books.get(1).size()) {
                index1++;
                sum1 += books.get(1).get(index1)[0];
            }
            if (index2+1 < books.get(2).size()) {
                index2++;
                sum2 += books.get(2).get(index2)[0];
            }
        }
        candidates.clear();
        for (int i = k; i < books.get(1).size(); i++) {
            candidates.add(books.get(1).get(i));
        }
        for (int i = k; i < books.get(2).size(); i++) {
            candidates.add(books.get(2).get(i));
        }
        for (int i = 0; i < books.get(0).size(); i++) {
            candidates.add(books.get(0).get(i));
        }
        free.clear();
        freeSum = 0;
        while (index1+index2+2+free.size() < m && !candidates.isEmpty()) {
            int[] add = candidates.remove();
            free.add(add);
            freeSum += add[0];
        }
        while (index1+index2+2+free.size() > m && !free.isEmpty()) {
            int[] rem = free.last();
            free.remove(rem);
            freeSum -= rem[0];
            candidates.add(rem);
        }
        // out.printf("%d %d %d %d  :  %d %d %d%n", sum1, sum2, sum3, freeSum, index1, index2, index3);
        index3 = -1;
        while (index3+1 <= bestIndex) {
            index3++;
            sum3 += books.get(3).get(index3)[0];
            if (index1 >= 0 && index1+index3+2 > k) {
                sum1 -= books.get(1).get(index1)[0];
                candidates.add(books.get(1).get(index1));
                index1--;
                if (!free.isEmpty()) {
                    int[] rem = free.last();
                    free.remove(rem);
                    freeSum -= rem[0];
                    candidates.add(rem);
                    int[] add = candidates.remove();
                    free.add(add);
                    freeSum += add[0];
                }
            }
            if (index2 >= 0 && index2+index3+2 > k) {
                sum2 -= books.get(2).get(index2)[0];
                candidates.add(books.get(2).get(index2));
                index2--;
                if (!free.isEmpty()) {
                    int[] rem = free.last();
                    free.remove(rem);
                    freeSum -= rem[0];
                    candidates.add(rem);
                    int[] add = candidates.remove();
                    free.add(add);
                    freeSum += add[0];
                }
            }
            while (index1+index2+index3+3+free.size() < m && !candidates.isEmpty()) {
                int[] add = candidates.remove();
                free.add(add);
                freeSum += add[0];
            }
            while (index1+index2+index3+3+free.size() > m && !free.isEmpty()) {
                int[] rem = free.last();
                free.remove(rem);
                freeSum -= rem[0];
                candidates.add(rem);
            }
            // out.printf("%d %d %d %d  :  %d %d %d%n", sum1, sum2, sum3, freeSum, index1, index2, index3);
        }
        if (best == Integer.MAX_VALUE) {
            out.println(-1);
            return;
        }
        out.println(best);
        for (int i = 0; i <= index1; i++) {
            out.print(books.get(1).get(i)[1]+1);
            out.print(" ");
        }
        for (int i = 0; i <= index2; i++) {
            out.print(books.get(2).get(i)[1]+1);
            out.print(" ");
        }
        for (int i = 0; i <= index3; i++) {
            out.print(books.get(3).get(i)[1]+1);
            out.print(" ");
        }
        for (int[] f : free) {
            out.print(f[1]+1);
            out.print(" ");
        }
        out.println();
    }

    public int compare(int[] a, int[] b) {
        if (a[0] == b[0]) {
            return a[1] - b[1];
        }
        return a[0] - b[0];
    }

    public int ipar() throws IOException {
        return Integer.parseInt(spar());
    }

    public int[] iapar(int n) throws IOException {
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = ipar();
        }
        return arr;
    }

    public long lpar() throws IOException {
        return Long.parseLong(spar());
    }

    public long[] lapar(int n) throws IOException {
        long[] arr = new long[n];
        for (int i = 0; i < n; i++) {
            arr[i] = lpar();
        }
        return arr;
    }

    public double dpar() throws IOException {
        return Double.parseDouble(spar());
    }

    public String spar() throws IOException {
        int len = 0;
        int c;
        do {
            c = in.read();
        } while (Character.isWhitespace(c) && c != -1);
        if (c == -1) {
            throw new NoSuchElementException("Reached EOF");
        }
        do {
            inputBuffer[len] = (char)c;
            len++;
            c = in.read();
        } while (!Character.isWhitespace(c) && c != -1);
        while (c != '\n' && Character.isWhitespace(c) && c != -1) {
            c = in.read();
        }
        if (c != -1 && c != '\n') {
            in.unread(c);
        }
        return new String(inputBuffer, 0, len);
    }

    public String linepar() throws IOException {
        int len = 0;
        int c;
        while ((c = in.read()) != '\n' && c != -1) {
            if (c == '\r') {
                continue;
            }
            inputBuffer[len] = (char)c;
            len++;
        }
        return new String(inputBuffer, 0, len);
    }

    public boolean haspar() throws IOException {
        String line = linepar();
        if (line.isEmpty()) {
            return false;
        }
        in.unread('\n');
        in.unread(line.toCharArray());
        return true;
    }

    public static void main(String[] args) throws IOException {
        long time = 0;
        time -= System.nanoTime();
        new E2().go();
        time += System.nanoTime();
        if (RUN_TIMING) {
            System.out.printf("%.3f ms%n", time / 1000000.0);
        }
        out.flush();
        in.close();
    }
}