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;
int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int DEBUG = 0;
  int n, m, k, t, _a, _b;
  cin >> n >> m >> k;
  vector<int> ab, a, b, nf;
  vector<pair<int, int>> ab_index, a_index, b_index, nf_index;
  for (int i = 0; i < n; i++) {
    cin >> t >> _a >> _b;
    if (_a && _b)
      ab_index.push_back({t, i + 1});
    else if (_a && !_b)
      a_index.push_back({t, i + 1});
    else if (!_a && _b)
      b_index.push_back({t, i + 1});
    else
      nf_index.push_back({t, i + 1});
  }
  sort(ab_index.begin(), ab_index.end());
  sort(a_index.begin(), a_index.end());
  sort(b_index.begin(), b_index.end());
  sort(nf_index.begin(), nf_index.end());
  for (auto _ : ab_index) ab.push_back(_.first);
  for (auto _ : a_index) a.push_back(_.first);
  for (auto _ : b_index) b.push_back(_.first);
  for (auto _ : nf_index) nf.push_back(_.first);
  vector<int>::iterator nfI = nf.begin(), abI = ab.begin(), aI = a.begin(),
                        bI = b.begin();
  int T = 0;
  while ((nfI != ab.end() || abI != ab.end() || aI < a.end() || bI < b.end()) &&
         (nfI - nf.begin() + abI - ab.begin() + aI - a.begin() + bI -
          b.begin()) < m) {
    if (DEBUG) cerr << "nf used: " << nfI - nf.begin() << endl;
    if (DEBUG) cerr << "ab used: " << abI - ab.begin() << endl;
    if (DEBUG) cerr << "a used: " << aI - a.begin() << endl;
    if (DEBUG) cerr << "b used: " << bI - b.begin() << endl;
    if (DEBUG)
      cerr << "R: "
           << (nfI - nf.begin() + abI - ab.begin() + aI - a.begin() + bI -
               b.begin())
           << endl;
    if (DEBUG)
      cerr << "L: " << (abI - ab.begin() + min(aI - a.begin(), bI - b.begin()))
           << endl;
    int nf_t = (nfI != nf.end() ? *nfI : numeric_limits<int>::max());
    int ab_t = (abI != ab.end() ? *abI : numeric_limits<int>::max());
    int a_t = (aI != a.end() ? *aI : numeric_limits<int>::max());
    int b_t = (bI != b.end() ? *bI : numeric_limits<int>::max());
    if (ab_t != numeric_limits<int>::max() && ab_t <= nf_t && ab_t <= a_t &&
        ab_t <= b_t) {
      if (DEBUG) cerr << "use ab book " << *abI << "\n";
      T += *abI++;
      continue;
    } else if (a_t != numeric_limits<int>::max() && a_t <= nf_t &&
               a_t <= ab_t && a_t <= b_t) {
      if (DEBUG) cerr << "use a book " << *aI << "\n";
      T += *aI++;
      continue;
    } else if (b_t != numeric_limits<int>::max() && b_t <= nf_t &&
               b_t <= ab_t && b_t <= a_t) {
      if (DEBUG) cerr << "use b book " << *bI << "\n";
      T += *bI++;
      continue;
    } else if (nf_t != numeric_limits<int>::max() && nf_t <= a_t &&
               nf_t <= b_t && nf_t <= ab_t) {
      if (DEBUG) cerr << "use nf book " << *nfI << "\n";
      T += *nfI++;
      continue;
    } else {
      if (DEBUG) cerr << "Out of books!\n";
      break;
    }
  }
  if ((nfI - nf.begin() + abI - ab.begin() + aI - a.begin() + bI - b.begin()) !=
      m) {
    cout << -1 << endl;
    return 0;
  }
  while ((abI - ab.begin() + min(aI - a.begin(), bI - b.begin())) < k) {
    int ab_rep_nf =
        ((abI != ab.end() && nfI != nf.begin()) ? *abI - *(nfI - 1)
                                                : numeric_limits<int>::max());
    int a_rep_nf = ((aI != a.end() && nfI != nf.begin() &&
                     (bI - b.begin() > aI - a.begin()))
                        ? *aI - *(nfI - 1)
                        : numeric_limits<int>::max());
    int b_rep_nf = ((bI != b.end() && nfI != nf.begin() &&
                     (aI - a.begin() > bI - b.begin()))
                        ? *bI - *(nfI - 1)
                        : numeric_limits<int>::max());
    int pair_rep_nf = ((aI != a.end() && bI != b.end() && nfI - nf.begin() > 1)
                           ? (*aI + *bI) - *(nfI - 1) - *(nfI - 2)
                           : numeric_limits<int>::max());
    int ab_rep_pair =
        ((ab.end() - abI > 1 && aI != a.begin() && bI != b.begin())
             ? *abI + *(abI + 1) - *(aI - 1) - *(bI - 1)
             : numeric_limits<int>::max());
    int ab_rep_a = ((abI != ab.end() && aI - a.begin() > bI - b.begin())
                        ? *abI - *(aI - 1)
                        : numeric_limits<int>::max());
    int ab_rep_b = ((abI != ab.end() && bI - b.begin() > aI - a.begin())
                        ? *abI - *(bI - 1)
                        : numeric_limits<int>::max());
    int a_rep_b =
        ((aI != a.end() && int(bI - b.begin()) - int(aI - a.begin()) > 1)
             ? *aI - *(bI - 1)
             : numeric_limits<int>::max());
    int b_rep_a =
        ((bI != b.end() && int(aI - a.begin()) - int(bI - b.begin()) > 1)
             ? *bI - *(aI - 1)
             : numeric_limits<int>::max());
    if (ab_rep_nf != numeric_limits<int>::max() &&
        (ab_rep_nf <= a_rep_nf && ab_rep_nf <= b_rep_nf &&
         ab_rep_nf <= pair_rep_nf && ab_rep_nf <= ab_rep_pair &&
         ab_rep_nf <= ab_rep_a && ab_rep_nf <= ab_rep_b &&
         ab_rep_nf <= a_rep_b && ab_rep_nf <= b_rep_a)) {
      nfI--;
      if (DEBUG)
        cerr << "Replace nf book " << *nfI << " with ab " << *abI << endl;
      T -= *nfI;
      T += *abI++;
    } else if (a_rep_nf != numeric_limits<int>::max() &&
               (a_rep_nf <= ab_rep_nf && a_rep_nf <= b_rep_nf &&
                a_rep_nf <= pair_rep_nf && a_rep_nf <= ab_rep_pair &&
                a_rep_nf <= ab_rep_a && a_rep_nf <= ab_rep_b &&
                a_rep_nf <= a_rep_b && a_rep_nf <= b_rep_a)) {
      nfI--;
      if (DEBUG)
        cerr << "Replace nf book " << *nfI << " with a " << *aI << endl;
      T -= *nfI;
      T += *aI++;
    } else if (b_rep_nf != numeric_limits<int>::max() &&
               (b_rep_nf <= ab_rep_nf && b_rep_nf <= a_rep_nf &&
                b_rep_nf <= pair_rep_nf && b_rep_nf <= ab_rep_pair &&
                b_rep_nf <= ab_rep_a && b_rep_nf <= ab_rep_b &&
                b_rep_nf <= a_rep_b && b_rep_nf <= b_rep_a)) {
      nfI--;
      if (DEBUG)
        cerr << "Replace nf book " << *nfI << " with b " << *aI << endl;
      T -= *nfI;
      T += *bI++;
    } else if (pair_rep_nf != numeric_limits<int>::max() &&
               (pair_rep_nf <= ab_rep_nf && pair_rep_nf <= a_rep_nf &&
                pair_rep_nf <= b_rep_nf && pair_rep_nf <= ab_rep_pair &&
                pair_rep_nf <= ab_rep_a && pair_rep_nf <= ab_rep_b &&
                pair_rep_nf <= a_rep_b && pair_rep_nf <= b_rep_a)) {
      nfI -= 2;
      if (DEBUG)
        cerr << "Replace nf books (" << *(nfI + 1) << " + " << *(nfI + 2)
             << ") with pair (" << *aI << " + " << *bI << ")" << endl;
      T -= *nfI;
      T -= *(nfI + 1);
      T += *aI++;
      T += *bI++;
    } else if (ab_rep_pair != numeric_limits<int>::max() &&
               (ab_rep_pair <= ab_rep_nf && ab_rep_pair <= a_rep_nf &&
                ab_rep_pair <= b_rep_nf && ab_rep_pair <= pair_rep_nf &&
                ab_rep_pair <= ab_rep_a && ab_rep_pair <= ab_rep_b &&
                ab_rep_pair <= a_rep_b && ab_rep_pair <= b_rep_a)) {
      aI--;
      bI--;
      if (DEBUG)
        cerr << "Replace pair books (" << *aI << " + " << *bI << ") with ab ("
             << *abI << " + " << *(abI + 1) << ")" << endl;
      T -= *aI;
      T -= *bI;
      T += *abI++;
      T += *abI++;
    } else if (ab_rep_a != numeric_limits<int>::max() &&
               (ab_rep_a <= ab_rep_nf && ab_rep_a <= a_rep_nf &&
                ab_rep_a <= b_rep_nf && ab_rep_a <= pair_rep_nf &&
                ab_rep_a <= ab_rep_pair && ab_rep_a <= ab_rep_b &&
                ab_rep_a <= a_rep_b && ab_rep_a <= b_rep_a)) {
      aI--;
      if (DEBUG)
        cerr << "Replace a book " << *aI << " with ab " << *abI << endl;
      T -= *aI;
      T += *abI++;
    } else if (ab_rep_b != numeric_limits<int>::max() &&
               (ab_rep_b <= ab_rep_nf && ab_rep_b <= a_rep_nf &&
                ab_rep_b <= b_rep_nf && ab_rep_b <= pair_rep_nf &&
                ab_rep_b <= ab_rep_pair && ab_rep_b <= ab_rep_a &&
                ab_rep_b <= a_rep_b && ab_rep_b <= b_rep_a)) {
      bI--;
      if (DEBUG)
        cerr << "Replace b book " << *bI << " with ab " << *abI << endl;
      T -= *bI;
      T += *abI++;
    } else if (a_rep_b != numeric_limits<int>::max() &&
               (a_rep_b <= ab_rep_nf && a_rep_b <= a_rep_nf &&
                a_rep_b <= b_rep_nf && a_rep_b <= pair_rep_nf &&
                a_rep_b <= ab_rep_pair && a_rep_b <= ab_rep_a &&
                a_rep_b <= ab_rep_b && a_rep_b <= b_rep_a)) {
      bI--;
      if (DEBUG) cerr << "Replace b book " << *bI << " with a " << *aI << endl;
      T -= *bI;
      T += *aI++;
    } else if (b_rep_a != numeric_limits<int>::max() &&
               (b_rep_a <= ab_rep_nf && b_rep_a <= a_rep_nf &&
                b_rep_a <= b_rep_nf && b_rep_a <= pair_rep_nf &&
                b_rep_a <= ab_rep_pair && b_rep_a <= ab_rep_a &&
                b_rep_a <= ab_rep_b && b_rep_a <= a_rep_b)) {
      aI--;
      if (DEBUG) cerr << "Replace a book " << *aI << " with b " << *aI << endl;
      T -= *aI;
      T += *bI++;
    } else {
      if (DEBUG) cerr << "No more books to replace!\n";
      break;
    }
    if (DEBUG)
      cerr << "L: " << (abI - ab.begin() + min(aI - a.begin(), bI - b.begin()))
           << endl;
  }
  if ((abI - ab.begin() + min(aI - a.begin(), bI - b.begin())) < k) {
    cout << -1 << endl;
    return 0;
  }
  cout << T << endl;
  for (vector<int>::iterator VI = ab.begin(); VI < abI; VI++)
    cout << ab_index[VI - ab.begin()].second << " ";
  for (vector<int>::iterator VI = nf.begin(); VI < nfI; VI++)
    cout << nf_index[VI - nf.begin()].second << " ";
  for (vector<int>::iterator VI = a.begin(); VI < aI; VI++)
    cout << a_index[VI - a.begin()].second << " ";
  for (vector<int>::iterator VI = b.begin(); VI < bI; VI++)
    cout << b_index[VI - b.begin()].second << " ";
  cout << endl;
  return 0;
}