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>
#pragma GCC optimize("Ofast")
using namespace std;
void *wmem;
char memarr[96000000];
template <class S, class T>
inline S min_L(S a, T b) {
  return a <= b ? a : b;
}
template <class T>
inline void walloc1d(T **arr, int x, void **mem = &wmem) {
  static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void *)(((char *)(*mem)) + skip[((unsigned long long)(*mem)) & 15]);
  (*arr) = (T *)(*mem);
  (*mem) = ((*arr) + x);
}
template <class T1>
void sortA_L(int N, T1 a[], void *mem = wmem) {
  sort(a, a + N);
}
template <class T1, class T2>
void sortA_L(int N, T1 a[], T2 b[], void *mem = wmem) {
  int i;
  pair<T1, T2> *arr;
  walloc1d(&arr, N, &mem);
  for (i = (0); i < (N); i++) {
    arr[i].first = a[i];
    arr[i].second = b[i];
  }
  sort(arr, arr + N);
  for (i = (0); i < (N); i++) {
    a[i] = arr[i].first;
    b[i] = arr[i].second;
  }
}
inline int my_getchar() {
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if (s == e && e == 1048576) {
    e = fread(buf, 1, 1048576, stdin);
    s = 0;
  }
  if (s == e) {
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x) {
  int k;
  int m = 0;
  x = 0;
  for (;;) {
    k = my_getchar();
    if (k == '-') {
      m = 1;
      break;
    }
    if ('0' <= k && k <= '9') {
      x = k - '0';
      break;
    }
  }
  for (;;) {
    k = my_getchar();
    if (k < '0' || k > '9') {
      break;
    }
    x = x * 10 + k - '0';
  }
  if (m) {
    x = -x;
  }
}
struct MY_WRITER {
  char buf[1048576];
  int s;
  int e;
  MY_WRITER() {
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER() {
    if (s) {
      fwrite(buf, 1, s, stdout);
    }
  }
};
MY_WRITER MY_WRITER_VAR;
void my_putchar(int a) {
  if (MY_WRITER_VAR.s == MY_WRITER_VAR.e) {
    fwrite(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a) { my_putchar(a); }
inline void wt_L(int x) {
  int s = 0;
  int m = 0;
  char f[10];
  if (x < 0) {
    m = 1;
    x = -x;
  }
  while (x) {
    f[s++] = x % 10;
    x /= 10;
  }
  if (!s) {
    f[s++] = 0;
  }
  if (m) {
    my_putchar('-');
  }
  while (s--) {
    my_putchar(f[s] + '0');
  }
}
inline void wt_L(long long x) {
  int s = 0;
  int m = 0;
  char f[20];
  if (x < 0) {
    m = 1;
    x = -x;
  }
  while (x) {
    f[s++] = x % 10;
    x /= 10;
  }
  if (!s) {
    f[s++] = 0;
  }
  if (m) {
    my_putchar('-');
  }
  while (s--) {
    my_putchar(f[s] + '0');
  }
}
template <class S>
inline void arrInsert(const int k, int &sz, S a[], const S aval) {
  int i;
  sz++;
  for (i = sz - 1; i > k; i--) {
    a[i] = a[i - 1];
  }
  a[k] = aval;
}
template <class S, class T>
inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[],
                      const T bval) {
  int i;
  sz++;
  for (i = sz - 1; i > k; i--) {
    a[i] = a[i - 1];
  }
  for (i = sz - 1; i > k; i--) {
    b[i] = b[i - 1];
  }
  a[k] = aval;
  b[k] = bval;
}
template <class S, class T, class U>
inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[],
                      const T bval, U c[], const U cval) {
  int i;
  sz++;
  for (i = sz - 1; i > k; i--) {
    a[i] = a[i - 1];
  }
  for (i = sz - 1; i > k; i--) {
    b[i] = b[i - 1];
  }
  for (i = sz - 1; i > k; i--) {
    c[i] = c[i - 1];
  }
  a[k] = aval;
  b[k] = bval;
  c[k] = cval;
}
template <class S, class T, class U, class V>
inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[],
                      const T bval, U c[], const U cval, V d[], const V dval) {
  int i;
  sz++;
  for (i = sz - 1; i > k; i--) {
    a[i] = a[i - 1];
  }
  for (i = sz - 1; i > k; i--) {
    b[i] = b[i - 1];
  }
  for (i = sz - 1; i > k; i--) {
    c[i] = c[i - 1];
  }
  for (i = sz - 1; i > k; i--) {
    d[i] = d[i - 1];
  }
  a[k] = aval;
  b[k] = bval;
  c[k] = cval;
  d[k] = dval;
}
int N;
int K;
int M;
int T;
int A;
int B;
int as;
int a[200000];
int ia[200000];
int bs;
int b[200000];
int ib[200000];
int cs;
int c[200000];
int ic[200000];
int ds;
int d[200000];
int id[200000];
int ress;
int resarr[200000];
int main() {
  int i;
  wmem = memarr;
  long long res = 4611686016279904256LL;
  int r_a;
  int r_b;
  int r_c;
  int r_d;
  int ta = 0;
  int tb = 0;
  int tc = 0;
  int td = 0;
  long long tmp = 0;
  int va;
  int vb;
  int vd;
  int mv;
  rd(N);
  rd(M);
  rd(K);
  for (i = (0); i < (N); i++) {
    rd(T);
    rd(A);
    rd(B);
    if (A == 1 && B == 0) {
      arrInsert(as, as, a, T, ia, i);
    }
    if (A == 0 && B == 1) {
      arrInsert(bs, bs, b, T, ib, i);
    }
    if (A == 1 && B == 1) {
      arrInsert(cs, cs, c, T, ic, i);
    }
    if (A == 0 && B == 0) {
      arrInsert(ds, ds, d, T, id, i);
    }
  }
  sortA_L(as, a, ia);
  sortA_L(bs, b, ib);
  sortA_L(cs, c, ic);
  sortA_L(ds, d, id);
  for (i = (0); i < (cs + 1); i++) {
    int cTE1_r3A;
    if (K - i > as) {
      continue;
    }
    if (K - i > bs) {
      continue;
    }
    if (i + K - i + K - i > M) {
      continue;
    }
    if (i + as + bs + ds < M) {
      continue;
    }
    for (cTE1_r3A = (0); cTE1_r3A < (3); cTE1_r3A++) {
      if (ta > 0) {
        tmp -= a[--ta];
      }
      if (tb > 0) {
        tmp -= b[--tb];
      }
      if (td > 0) {
        tmp -= d[--td];
      }
    }
    while (ta < as && ta < K - i) {
      tmp += a[ta++];
    }
    while (tb < bs && tb < K - i) {
      tmp += b[tb++];
    }
    while (tc < cs && tc < i) {
      tmp += c[tc++];
    }
    while (ta + tb + tc + td < M) {
      if (ta == as) {
        va = 1073709056;
      } else {
        va = a[ta];
      }
      if (tb == bs) {
        vb = 1073709056;
      } else {
        vb = b[tb];
      }
      if (td == ds) {
        vd = 1073709056;
      } else {
        vd = d[td];
      }
      mv = min_L(min_L(va, vb), vd);
      if (va == mv) {
        tmp += a[ta++];
        continue;
      }
      if (vb == mv) {
        tmp += b[tb++];
        continue;
      }
      if (vd == mv) {
        tmp += d[td++];
        continue;
      }
    }
    if (res > tmp) {
      res = tmp;
      r_a = ta;
      r_b = tb;
      r_c = tc;
      r_d = td;
    }
  }
  if (res == 4611686016279904256LL) {
    wt_L(-1);
    wt_L('\n');
    return 0;
  }
  for (i = (0); i < (r_a); i++) {
    resarr[ress++] = ia[i];
  }
  for (i = (0); i < (r_b); i++) {
    resarr[ress++] = ib[i];
  }
  for (i = (0); i < (r_c); i++) {
    resarr[ress++] = ic[i];
  }
  for (i = (0); i < (r_d); i++) {
    resarr[ress++] = id[i];
  }
  wt_L(res);
  wt_L('\n');
  {
    int jPV_0s1p;
    if (ress == 0) {
      wt_L('\n');
    } else {
      for (jPV_0s1p = (0); jPV_0s1p < (ress - 1); jPV_0s1p++) {
        wt_L(resarr[jPV_0s1p] + 1);
        wt_L(' ');
      }
      wt_L(resarr[jPV_0s1p] + 1);
      wt_L('\n');
    }
  }
  return 0;
}