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 maxn = 200005;
const int maxm = 100;
const int inf = 2147483647;
const double eps = 1e-9;
const long long INF = 9223372036854775807ll;
long long qpow(long long a, long long b, long long c) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = ans * a % c;
    a = a * a % c;
    b >>= 1;
  }
  return ans;
}
int t[maxn], as[maxn], bs[maxn];
struct Node {
  int v, id;
};
vector<Node> aa, bb, sam, vac;
bool cmp(Node a, Node b) { return a.v < b.v; }
int main() {
  int n, m, k;
  scanf("%d%d%d", &n, &m, &k);
  for (register int i = (0); i < (n); ++i) {
    scanf("%d%d%d", &t[i], &as[i], &bs[i]);
    if (as[i] && bs[i])
      sam.push_back({t[i], i + 1});
    else if (as[i])
      aa.push_back({t[i], i + 1});
    else if (bs[i])
      bb.push_back({t[i], i + 1});
    else
      vac.push_back({t[i], i + 1});
  }
  long long ans = INF, as = 0;
  int an = -1, bn = -1, sn = -1, vn = -1;
  sort(aa.begin(), aa.end(), cmp);
  sort(bb.begin(), bb.end(), cmp);
  sort(sam.begin(), sam.end(), cmp);
  sort(vac.begin(), vac.end(), cmp);
  if ((int)aa.size() + (int)sam.size() < k ||
      (int)bb.size() + (int)sam.size() < k)
    return printf("-1\n"), 0;
  int a = -1, b = -1, s = -1, v = -1,
      y = min(min((int)bb.size() - 1, (int)aa.size() - 1), k - 1),
      x = k - y - 2;
  a = b = y, s = x;
  for (register int i = (0); i < (y + 1); ++i)
    as += (long long)aa[i].v + (long long)bb[i].v;
  for (register int i = (0); i < (x + 1); ++i) as += (long long)sam[i].v;
  int ctt = m - x - 2 * y - 3;
  while (ctt > 0) {
    int ma = (a + 1 == (int)aa.size()) ? inf : aa[a + 1].v,
        mb = (b + 1 == (int)bb.size()) ? inf : bb[b + 1].v;
    int ms = (s + 1 == (int)sam.size()) ? inf : sam[s + 1].v,
        mv = (v + 1 == (int)vac.size()) ? inf : vac[v + 1].v;
    long long mn = min(ma, min(mb, min(ms, mv)));
    if (mn == inf) break;
    if (mn == ma)
      a++;
    else if (mn == mb)
      b++;
    else if (mn == ms)
      s++;
    else if (mn == mv)
      v++;
    as += mn;
    ctt--;
  }
  if (!ctt && as < ans) {
    ans = as;
    an = a, bn = b, sn = s, vn = v;
  }
  while (x < min((int)sam.size() - 1, k - 1)) {
    if (s == x) as += (long long)sam[x + 1].v, ctt--;
    if (a == y) as -= (long long)aa[a].v, ctt++;
    if (b == y) as -= (long long)bb[b].v, ctt++;
    s = max(s, x + 1), a = (a == y) ? y - 1 : a, b = (b == y) ? y - 1 : b;
    x++, y--;
    while (ctt > 0) {
      int ma = (a + 1 == (int)aa.size()) ? inf : aa[a + 1].v,
          mb = (b + 1 == (int)bb.size()) ? inf : bb[b + 1].v;
      int ms = (s + 1 == (int)sam.size()) ? inf : sam[s + 1].v,
          mv = (v + 1 == (int)vac.size()) ? inf : vac[v + 1].v;
      long long mn = min(ma, min(mb, min(ms, mv)));
      if (mn == inf) break;
      if (mn == ma)
        a++;
      else if (mn == mb)
        b++;
      else if (mn == ms)
        s++;
      else if (mn == mv)
        v++;
      as += mn;
      ctt--;
    }
    while (ctt < 0) {
      int ma = (a == y) ? -inf : aa[a].v, mb = (b == y) ? -inf : bb[b].v;
      int ms = (s == x) ? -inf : sam[s].v, mv = (v == -1) ? -inf : vac[v].v;
      long long mx = max(ma, max(mb, max(ms, mv)));
      if (mx == -inf) break;
      if (mx == ma)
        a--;
      else if (mx == mb)
        b--;
      else if (mx == ms)
        s--;
      else if (mx == mv)
        v--;
      as -= mx;
      ctt++;
    }
    if (ctt) continue;
    if (as < ans) {
      ans = as;
      an = a, bn = b, sn = s, vn = v;
    }
  }
  if (ans == INF)
    printf("-1\n");
  else {
    printf("%lld\n", ans);
    for (register int i = (0); i < (an + 1); ++i) printf("%d ", aa[i].id);
    for (register int i = (0); i < (bn + 1); ++i) printf("%d ", bb[i].id);
    for (register int i = (0); i < (sn + 1); ++i) printf("%d ", sam[i].id);
    for (register int i = (0); i < (vn + 1); ++i) printf("%d ", vac[i].id);
    printf("\n");
  }
}