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;
ifstream in("input.txt");
ofstream out("output.txt");
mt19937 sd(1337);
const int MAX_N = 2e5, INF = 2e9 + 1;
int pr_num[4][MAX_N];
vector<int> pr_val[4];
int main() {
  cin.tie(0);
  cout.tie(0);
  cin.sync_with_stdio(0);
  cout.sync_with_stdio(0);
  ;
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> t0;
  vector<pair<int, int>> bk;
  vector<pair<int, pair<int, int>>> all;
  for (int i = 0; i < n; i++) {
    int t, a, b;
    cin >> t >> a >> b;
    if (a == 0 && b == 0)
      t0.push_back(t);
    else
      bk.push_back({t, a * 2 + b});
    all.push_back({t, {i, a * 2 + b}});
  }
  sort(all.begin(), all.end());
  sort(t0.begin(), t0.end());
  sort(bk.begin(), bk.end());
  for (int i = 0; i < bk.size(); i++) {
    pr_val[bk[i].second].push_back(
        (pr_val[bk[i].second].empty() ? 0 : pr_val[bk[i].second].back()) +
        bk[i].first);
    for (int t = 1; t <= 3; t++)
      pr_num[t][i] = (i > 0 ? pr_num[t][i - 1] : 0) + (bk[i].second == t);
  }
  int ans = INF;
  int ans_take[4];
  for (int n0 = 0, sum0 = 0; n0 <= min(m - k, (int)t0.size());
       sum0 += (n0 >= t0.size() ? 0 : t0[n0]), n0++) {
    int left = m - k - n0;
    int l = -1, r = bk.size();
    while (r - l > 1) {
      int mid = (l + r) / 2;
      if (min(left, pr_num[1][mid]) + min(left, pr_num[2][mid]) +
              pr_num[3][mid] >=
          m - n0)
        r = mid;
      else
        l = mid;
    }
    if (r != bk.size()) {
      int cur = sum0;
      for (int t = 1; t <= 3; t++) {
        int take = (t == 3 ? pr_num[t][r] : min(left, pr_num[t][r]));
        if (take != 0) cur += pr_val[t][take - 1];
      }
      if (cur < ans) {
        ans_take[0] = n0;
        ans_take[1] = min(left, pr_num[1][r]);
        ans_take[2] = min(left, pr_num[2][r]);
        ans_take[3] = pr_num[3][r];
        ans = cur;
      }
    }
  }
  cout << (ans == INF ? -1 : ans) << '\n';
  if (ans != INF) {
    for (int i = 0; i < all.size(); i++) {
      if (ans_take[all[i].second.second]-- > 0)
        cout << all[i].second.first + 1 << ' ';
    }
  }
}