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.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Solution {
    static final FS sc = new FS();  // 封装输入类
    static final PrintWriter pw = new PrintWriter(System.out);

    static PriorityQueue<Book> pqA, pqB;
    static long total = 0;
    static int countA = 0, countB = 0;
    static int booksUsed = 0;

    static PriorityQueue<Book> cheapestA, cheapestB, cheapestBoth, cheapestNone, worstBoth, worstA, worstB, worstNone;

    public static void main(String[] args) {
        int n = sc.nextInt(), m = sc.nextInt(), k = sc.nextInt();
        Book[] books = new Book[n], unsorted = new Book[n];
        for (int i = 0; i < n; i++) {
            unsorted[i] = books[i] = new Book(sc.nextInt(), sc.nextInt(), sc.nextInt());
        }

        pqA = new PriorityQueue<>(Comparator.reverseOrder());
        pqB = new PriorityQueue<>(Comparator.reverseOrder());

        cheapestA = new PriorityQueue<>();
        cheapestB = new PriorityQueue<>();
        cheapestBoth = new PriorityQueue<>();
        cheapestNone = new PriorityQueue<>();

        worstBoth = new PriorityQueue<>(Comparator.reverseOrder());
        worstA = new PriorityQueue<>(Comparator.reverseOrder());
        worstB = new PriorityQueue<>(Comparator.reverseOrder());
        worstNone = new PriorityQueue<>(Comparator.reverseOrder());

        Arrays.sort(books);
        for (Book book : books) {
            if (book.likeA && book.likeB) {
                cheapestBoth.add(book);
                if (countA < k || countB < k) {
                    add(book);
                    if (countA > k && !pqA.isEmpty()) {
                        Book toRemove = pqA.remove();
                        remove(toRemove);
                    }
                    if (countB > k && !pqB.isEmpty()) {
                        Book toRemove = pqB.remove();
                        remove(toRemove);
                    }
                } else if (!pqA.isEmpty() && !pqB.isEmpty()) {
                    int bonus = pqA.peek().t + pqB.peek().t;
                    if (bonus > book.t) {
                        Book removed1 = pqA.remove(), removed2 = pqB.remove();
                        add(book);
                        remove(removed1);
                        remove(removed2);
                    }
                }
            } else if (book.likeA) {
                cheapestA.add(book);
                if (countA <k) {
                    add(book);
                }
            } else if (book.likeB) {
                cheapestB.add(book);
                if (countB <k) {
                    add(book);
                }
            } else {
                cheapestNone.add(book);
            }
        }

        while (booksUsed > m) {
            Book toAdd = getCheapestUnused(cheapestBoth);
            Book worstAUsed = getWorstUsed(worstA);
            Book worstBUsed = getWorstUsed(worstB);
            if (toAdd == null || worstAUsed == null || worstBUsed == null) {
                pw.println(-1);
                pw.flush();
                return;
            }
            remove(worstAUsed);
            remove(worstBUsed);
            add(toAdd);
        }

        while (booksUsed < m) {
            Book cheapestAUnused = getCheapestUnused(cheapestA);;
            Book cheapestBUnused = getCheapestUnused(cheapestB);
            Book cheapestBothUnused = getCheapestUnused(cheapestBoth);
            Book cheapestNoneUnused = getCheapestUnused(cheapestNone);

            Book bestToAdd = minCost(cheapestAUnused, cheapestBUnused, cheapestBothUnused, cheapestNoneUnused);
            Book worstBothUsed = getWorstUsed(worstBoth);
            if (worstBothUsed == null || cheapestAUnused == null || cheapestBUnused == null) {
                if (bestToAdd == null) {
                    pw.println(-1);
                    pw.flush();
                    return;
                }
                add(bestToAdd);
            } else {
                long costBestToAdd = bestToAdd.t;
                long costSwap = cheapestAUnused.t + cheapestBUnused.t - worstBothUsed.t;
                if (costBestToAdd < costSwap) {
                    add(bestToAdd);
                } else {
                    remove(worstBothUsed);
                    add(cheapestAUnused);
                    add(cheapestBUnused);
                }
            }
        }

        if (countA <k || countB <k) {
            pw.println(-1);
            pw.close();
            return;
        } else {
            pw.println(total);
        }

        for (int i = 0; i < n; i++) {
            if (unsorted[i].used) {
                pw.print(i + 1 + " ");
            }
        }
        pw.println();
        pw.close();
    }

    static void add(Book b) {
        b.used = true;
        total += b.t;
        booksUsed++;
        if (b.likeA && b.likeB) {
            countA++;
            countB++;
            worstBoth.add(b);
        } else if(b.likeA) {
            pqA.add(b);
            worstA.add(b);
            countA++;
        } else if (b.likeB) {
            pqB.add(b);
            worstB.add(b);
            countB++;
        } else {
            worstNone.add(b);
        }
    }

    static void remove(Book book) {
        book.used = false;
        total -= book.t;
        booksUsed--;

        if (book.likeA && book.likeB) {
            countA--;
            countB--;
            cheapestBoth.add(book);
        } else if(book.likeA) {
            countA--;
            cheapestA.add(book);
        } else if (book.likeB) {
            countB--;
            cheapestB.add(book);
        } else {
            cheapestNone.add(book);
        }
    }

    static Book minCost(Book a, Book b, Book c, Book d) {
        Book res = better(a, b);
        res = better(res, c);
        res = better(res, d);
        return res;
    }

    static Book better(Book a, Book b) {
        if (a == null) return b;
        if (b == null) return a;
        return a.t <= b.t ? a : b;
    }

    static Book getCheapestUnused(PriorityQueue<Book> pq) {
        while (!pq.isEmpty()) {
            Book next = pq.peek();
            if (next.used) pq.remove();
            else return next;
        }
        return null;
    }

    static Book getWorstUsed(PriorityQueue<Book> pq) {
        while (!pq.isEmpty()) {
            Book next = pq.peek();
            if (!next.used) pq.remove();
            else return next;
        }
        return null;
    }

    static class Book implements Comparable<Book> {
        int t;
        boolean likeA, likeB;
        boolean used;
        public Book(int t, int likeA, int likeB) {
            this.t = t;
            this.likeA = likeA ==1;
            this.likeB = likeB ==1;
        }

        public int compareTo(Book o) {
            return Integer.compare(t, o.t);
        }
    }

    static class FS {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer("");
        String next() {
            while(!st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch(Exception ignored) {}
            }
            return st.nextToken();
        }
        int[] nextArray(int n) {
            int[] a = new int[n];
            for(int i = 0;i < n;i++) {
                a[i] = nextInt();
            }
            return a;
        }
        int nextInt() {
            return Integer.parseInt(next());
        }
        long nextLong() {
            return Long.parseLong(next());
        }
    }
}