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;
using INT = long long;
const int NN = 2e5 + 10;
int t[NN], a[NN], b[NN], id[NN], pos[NN];
int n, m, k;
int sum[NN], cnt[NN];
int update(int i, int st = 1) {
  int u = pos[i];
  for (; u < NN; u += u & -u) {
    sum[u] += t[i] * st;
    cnt[u] += st;
  }
}
int calc(int s) {
  if (s < 0) return 2e9 + 10;
  int u = 0;
  int ans = 0;
  for (int i = 20; i >= 0; i--)
    if (u + (1 << i) < NN) {
      if (cnt[u + (1 << i)] <= s) {
        u += (1 << i);
        s -= cnt[u];
        ans += sum[u];
      }
    }
  return ans;
}
int print(int sz) {
  int sza = 0, szb = 0, szc = 0, szd = 0;
  for (int i = 1; i <= n; i++) {
    int u = id[i];
    if (a[u] and b[u] and sza < sz)
      sza++, printf("%d ", u);
    else if (a[u] and !b[u] and szb < k - sz)
      printf("%d ", u), szb++;
    else if (!a[u] and b[u] and szc < k - sz)
      printf("%d ", u), szc++;
    else if (szd < (m - (2 * k - sz)))
      printf("%d ", u), szd++;
  }
}
int main() {
  cin >> n >> m >> k;
  vector<int> A, B, C;
  for (int i = 1; i <= n; i++) {
    scanf("%d %d %d", t + i, a + i, b + i);
    id[i] = i;
  }
  sort(id + 1, id + n + 1, [&](int u, int v) { return t[u] < t[v]; });
  for (int i = 1; i <= n; i++) {
    pos[id[i]] = i;
  }
  for (int i = 1; i <= n; i++) {
    int u = id[i];
    if (a[u] and b[u])
      A.push_back(u);
    else if (a[u])
      B.push_back(u);
    else if (b[u])
      C.push_back(u);
  }
  if (A.size() + min(B.size(), C.size()) < k) {
    return puts("-1"), 0;
  }
  if (2 * k - (int)A.size() > m) return puts("-1"), 0;
  for (int i = 1; i <= n; i++) update(i);
  int tot = 0;
  while (A.size() > k) A.pop_back();
  for (auto u : A) {
    update(u, -1);
    tot += t[u];
  }
  int Ed = k - (int)A.size();
  for (int i = 0; i < Ed; i++) {
    update(B[i], -1);
    update(C[i], -1);
    tot += t[B[i]];
    tot += t[C[i]];
  }
  int ans = tot + calc(m - (2 * k - (int)A.size()));
  int sz = A.size();
  while (!A.empty()) {
    update(A.back());
    tot -= t[A.back()];
    A.pop_back();
    if (B.size() <= Ed || C.size() <= Ed) break;
    update(B[Ed], -1);
    update(C[Ed], -1);
    tot += t[B[Ed]];
    tot += t[C[Ed++]];
    if (m - (2 * k - (int)A.size()) < 0) break;
    if (ans > tot + calc(m - (2 * k - (int)A.size()))) {
      ans = tot + calc(m - (2 * k - (int)A.size()));
      sz = A.size();
    }
  }
  cout << ans << endl;
  print(sz);
}