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 long long maxn = 2e5 + 5, maxn1 = 1e4;
int n, m, i, t, j, k, l, x, y, z, p, q, sum, ans, num, num1, num0, num2;
struct code {
  int x, y;
} a[maxn], b[maxn], c[maxn], d[maxn];
int g[maxn], f[maxn], s[maxn], s1[maxn];
bool cmp(code x, code y) { return x.x < y.x; }
void insert(int l, int r, int v, int x) {
  int mid = (l + r) / 2;
  f[v] += x;
  g[v]++;
  if (l == r) return;
  if (x > mid)
    insert(mid + 1, r, v * 2 + 1, x);
  else
    insert(l, mid, v * 2, x);
}
void find(int l, int r, int v, int x) {
  int mid = (l + r) / 2;
  if (g[v] == x) {
    t += f[v];
    return;
  } else if (l == r) {
    t += l * x;
    return;
  }
  if (g[v * 2] >= x)
    find(l, mid, v * 2, x);
  else
    t += f[v * 2], find(mid + 1, r, v * 2 + 1, x - g[v * 2]);
}
int main() {
  scanf("%d%d%d", &n, &p, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d%d", &x, &y, &z);
    if (y & z)
      a[++num0] = {x, i};
    else if (y)
      b[++num] = {x, i};
    else if (z)
      c[++num1] = {x, i};
    else
      insert(1, maxn1, 1, x), d[++num2] = {x, i};
  }
  sort(a + 1, a + num0 + 1, cmp);
  sort(b + 1, b + num + 1, cmp);
  sort(c + 1, c + num1 + 1, cmp);
  if (num0 + ((num < num1) ? num : num1) < m || 2 * m - num0 > p) {
    printf("-1\n");
    return 0;
  }
  for (int i = 1; i <= num; i++) s[i] += s[i - 1] + b[i].x;
  for (int i = 1; i <= num1; i++) s1[i] += s1[i - 1] + c[i].x;
  ans = 2e9 + 10;
  for (int i = m + 1; i <= num; i++)
    insert(1, maxn1, 1, b[i].x), d[++num2] = b[i];
  for (int i = m + 1; i <= num1; i++)
    insert(1, maxn1, 1, c[i].x), d[++num2] = c[i];
  int start = ((m - num > m - num1) ? m - num : m - num1);
  sum = 0;
  for (int i = 0; i <= ((p + 1 < num0) ? p + 1 : num0); i++) {
    if (i <= m) {
      if (i >= start) {
        if (g[1] >= p - 2 * m + i && 2 * m - i <= p) {
          t = 0;
          find(1, maxn1, 1, p - 2 * m + i);
          if (ans > sum + t + s[m - i] + s1[m - i]) {
            ans = sum + t + s[m - i] + s1[m - i];
            q = i;
          }
        }
      }
      if (m - i <= num && m - i) {
        insert(1, maxn1, 1, b[m - i].x);
        d[++num2] = b[m - i];
      }
      if (m - i <= num1 && m - i) {
        insert(1, maxn1, 1, c[m - i].x);
        d[++num2] = c[m - i];
      }
    } else {
      t = 0;
      find(1, maxn1, 1, p - i);
      if (ans > sum + t) {
        ans = sum + t;
        q = i;
      }
    }
    sum += a[i + 1].x;
  }
  printf("%d\n", ans);
  for (int i = 1; i <= q; i++) printf("%d ", a[i].y);
  if (q <= m) {
    for (int i = 1; i <= m - q; i++) printf("%d %d ", b[i].y, c[i].y);
    sort(d + 1, d + n - (num0 + 2 * (m - q)) + 1, cmp);
    for (int i = 1; i <= p + q - 2 * m; i++) printf("%d ", d[i].y);
  } else {
    sort(d + 1, d + n - num0 + 1, cmp);
    for (int i = 1; i <= p - q; i++) printf("%d ", d[i].y);
  }
}