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 CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
template <typename T>
void read(T &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + c - '0';
  x *= f;
}
int n, m, k;
int tot1, tot2, tot3, tot4;
int sum;
int sum1[N], sum2[N], sum3[N];
int ans = 2e9 + 7, pos;
struct misaka {
  int val, id;
  bool operator<(const misaka &cmp) const { return val < cmp.val; }
} T1[N], T2[N], T3[N], T4[N];
multiset<misaka> all, s;
multiset<misaka>::iterator it;
int main() {
  read(n), read(m), read(k);
  for (int i = 1; i <= n; i++) {
    int t, x, y;
    read(t), read(x), read(y);
    if (x && y)
      T1[++tot1] = (misaka){t, i};
    else if (x)
      T2[++tot2] = (misaka){t, i};
    else if (y)
      T3[++tot3] = (misaka){t, i};
    else
      T4[++tot4] = (misaka){t, i};
  }
  if (tot1 + tot2 < k || tot1 + tot3 < k) return puts("-1"), 0;
  int atleast = 2 * k - m;
  if (atleast > tot1) return puts("-1"), 0;
  sort(T1 + 1, T1 + tot1 + 1);
  sort(T2 + 1, T2 + tot2 + 1);
  sort(T3 + 1, T3 + tot3 + 1);
  sort(T4 + 1, T4 + tot4 + 1);
  int sum = 0;
  for (int i = 1; i <= tot1; i++) sum1[i] = sum1[i - 1] + T1[i].val;
  for (int i = k + 1; i <= tot2; i++) all.insert(T2[i]);
  for (int i = 1; i <= tot2; i++) sum2[i] = sum2[i - 1] + T2[i].val;
  for (int i = k + 1; i <= tot3; i++) all.insert(T3[i]);
  for (int i = 1; i <= tot3; i++) sum3[i] = sum3[i - 1] + T3[i].val;
  for (int i = 1; i <= tot4; i++) all.insert(T4[i]);
  int need = k, extra = m - 2 * need;
  while (all.size() && s.size() < extra) {
    sum += all.begin()->val;
    s.insert(*all.begin());
    all.erase(all.begin());
  }
  if (k <= tot2 && k <= tot3 && s.size() == extra)
    if (sum2[k] + sum3[k] + sum < ans) ans = sum2[k] + sum3[k] + sum, pos = 0;
  for (int i = 1; i <= tot1; i++) {
    int need = max(k - i, 0), extra = m - i - 2 * need;
    if (k - i + 1 > 0 && need + 1 <= tot2)
      s.insert(T2[need + 1]), sum += T2[need + 1].val;
    if (k - i + 1 > 0 && need + 1 <= tot3)
      s.insert(T3[need + 1]), sum += T3[need + 1].val;
    if (extra < 0) continue;
    while (s.size() > max(extra, 0)) {
      it = s.end();
      it--;
      sum -= it->val, all.insert(*it);
      s.erase(it);
    }
    while (all.size() && s.size() < extra) {
      sum += all.begin()->val;
      s.insert(*all.begin());
      all.erase(all.begin());
    }
    while (s.size() && all.size() &&
           (it = (--s.end()))->val > all.begin()->val) {
      sum -= it->val, sum += all.begin()->val;
      all.insert(*it), s.erase(it);
      s.insert(*all.begin()), all.erase(all.begin());
    }
    if (need <= tot2 && need <= tot3 && s.size() == extra)
      if (sum1[i] + sum2[need] + sum3[need] + sum < ans)
        ans = sum1[i] + sum2[need] + sum3[need] + sum, pos = i;
  }
  printf("%d\n", ans);
  s.clear();
  need = k - pos, extra;
  if (need >= 0)
    extra = m - pos - 2 * need;
  else
    extra = m - pos, need = 0;
  for (int i = need + 1; i <= tot2; i++) s.insert(T2[i]);
  for (int i = need + 1; i <= tot3; i++) s.insert(T3[i]);
  for (int i = 1; i <= tot4; i++) s.insert(T4[i]);
  while (s.size() > max(extra, 0)) {
    it = s.end();
    it--;
    s.erase(it);
  }
  for (int i = 1; i <= need; i++) printf("%d ", T2[i].id);
  for (int i = 1; i <= need; i++) printf("%d ", T3[i].id);
  for (int i = 1; i <= pos; i++) printf("%d ", T1[i].id);
  for (it = s.begin(); it != s.end(); it++) printf("%d ", it->id);
  puts("");
  return 0;
}