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>
template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> v) {
  os << "[";
  for (auto it : v) os << it << " ";
  return os << "]";
}
using namespace std;
using namespace std;
struct DS {
  priority_queue<int> small;
  priority_queue<int, vector<int>, greater<int>> big;
  int sz;
  long long tot;
  DS(int n) : sz(n), tot(0) {}
  void fix() {
    while (int((small).size()) > sz) {
      assert(int((small).size()) > 0);
      big.push(small.top());
      tot -= small.top();
      small.pop();
    }
    while (int((small).size()) < sz && int((big).size()) > 0) {
      small.push(big.top());
      tot += big.top();
      big.pop();
    }
  }
  void resize(int s) {
    s = max(0, s);
    sz = min(s, int((big).size()) + int((small).size()));
    fix();
  }
  void insert(int x) {
    small.push(x);
    tot += x;
    fix();
  }
  int size() { return int((small).size()); }
};
int solve(vector<int> both, vector<int> alice, vector<int> bob,
          vector<int> other, int m, int k) {
  long long ans = -1;
  DS st(m);
  for (auto it : other) st.insert(it);
  vector<int> aliceTaken;
  vector<int> bobTaken;
  long long cur = 0;
  for (int i = 0; i < k; i++) {
    if (int((alice).size()) == 0 || int((bob).size()) == 0) break;
    aliceTaken.push_back(alice.back());
    bobTaken.push_back(bob.back());
    alice.pop_back();
    bob.pop_back();
    cur += aliceTaken.back();
    cur += bobTaken.back();
  }
  for (auto it : alice) st.insert(it);
  for (auto it : bob) st.insert(it);
  st.resize(max(0, m - 2 * int((aliceTaken).size())));
  if (int((st).size()) + 2 * int((aliceTaken).size()) == m &&
      int((aliceTaken).size()) == k)
    ans = st.tot + cur;
  vector<int> bothTaken;
  ;
  ;
  for (int i = 0; i < int((both).size()); i++) {
    bothTaken.push_back(both[i]);
    cur += both[i];
    if (int((bothTaken).size()) + int((aliceTaken).size()) < k) continue;
    if (int((bothTaken).size()) + int((aliceTaken).size()) > k &&
        int((aliceTaken).size()) > 0) {
      st.insert(aliceTaken.back());
      st.insert(bobTaken.back());
      cur -= aliceTaken.back();
      cur -= bobTaken.back();
      aliceTaken.pop_back();
      bobTaken.pop_back();
    }
    st.resize(m - int((bothTaken).size()) - 2 * int((aliceTaken).size()));
    if (int((st).size()) + int((bothTaken).size()) +
            2 * int((aliceTaken).size()) ==
        m) {
      if (ans == -1)
        ans = cur + st.tot;
      else
        ans = min(ans, cur + st.tot);
    }
  }
  return ans;
}
map<int, vector<int>> bothMp, aliceMp, bobMp, OtherMp;
void solve2(vector<int> both, vector<int> alice, vector<int> bob,
            vector<int> other, int m, int k, int finalAns) {
  long long ans = -1;
  ;
  ;
  DS st(m);
  for (auto it : other) st.insert(it);
  vector<int> aliceTaken, bobTaken, bothTaken;
  long long cur = 0;
  for (int i = 0; i < k; i++) {
    if (int((alice).size()) == 0 || int((bob).size()) == 0) break;
    aliceTaken.push_back(alice.back());
    bobTaken.push_back(bob.back());
    alice.pop_back();
    bob.pop_back();
    cur += aliceTaken.back();
    cur += bobTaken.back();
  }
  for (auto it : alice) st.insert(it);
  for (auto it : bob) st.insert(it);
  st.resize(max(0, m - 2 * int((aliceTaken).size())));
  if (int((st).size()) + 2 * int((aliceTaken).size()) == m &&
      int((aliceTaken).size()) == k)
    ans = st.tot + cur;
  auto PrintAns = [&]() {
    for (auto it : bothTaken) {
      cout << bothMp[it].back() << " ";
      bothMp[it].pop_back();
    }
    for (auto it : aliceTaken) {
      cout << aliceMp[it].back() << " ";
      aliceMp[it].pop_back();
    }
    for (auto it : bobTaken) {
      cout << bobMp[it].back() << " ";
      bobMp[it].pop_back();
    }
    while (int((st.small).size())) {
      auto it = st.small.top();
      st.small.pop();
      if (OtherMp[it].size()) {
        cout << OtherMp[it].back() << " ";
        OtherMp[it].pop_back();
      } else if (bobMp[it].size()) {
        cout << bobMp[it].back() << " ";
        bobMp[it].pop_back();
      } else if (aliceMp[it].size()) {
        cout << aliceMp[it].back() << " ";
        aliceMp[it].pop_back();
      }
    }
  };
  ;
  ;
  if (ans == finalAns) {
    ;
    ;
    PrintAns();
    return;
  }
  for (int i = 0; i < int((both).size()); i++) {
    bothTaken.push_back(both[i]);
    cur += both[i];
    if (int((bothTaken).size()) + int((aliceTaken).size()) < k) continue;
    if (int((bothTaken).size()) + int((aliceTaken).size()) > k &&
        int((aliceTaken).size()) > 0) {
      st.insert(aliceTaken.back());
      st.insert(bobTaken.back());
      cur -= aliceTaken.back();
      cur -= bobTaken.back();
      aliceTaken.pop_back();
      bobTaken.pop_back();
    }
    st.resize(m - int((bothTaken).size()) - 2 * int((aliceTaken).size()));
    if (int((st).size()) + int((bothTaken).size()) +
            2 * int((aliceTaken).size()) ==
        m) {
      if (ans == -1)
        ans = cur + st.tot;
      else
        ans = min(ans, cur + st.tot);
      if (ans == finalAns) {
        PrintAns();
        return;
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  int t = 1;
  while (t--) {
    int n, m, k;
    cin >> n >> m >> k;
    ;
    ;
    vector<int> both, alice, bob, other;
    for (int i = 0; i < n; i++) {
      int tt, a, b;
      cin >> tt >> a >> b;
      if (a && b) {
        both.push_back(tt);
        bothMp[tt].push_back(i + 1);
      } else if (a) {
        alice.push_back(tt);
        aliceMp[tt].push_back(i + 1);
      } else if (b) {
        bob.push_back(tt);
        bobMp[tt].push_back(i + 1);
      } else {
        other.push_back(tt);
        OtherMp[tt].push_back(i + 1);
      }
    }
    sort((both).begin(), (both).end());
    sort((alice).begin(), (alice).end());
    sort((bob).begin(), (bob).end());
    sort((other).begin(), (other).end());
    reverse((alice).begin(), (alice).end());
    reverse((bob).begin(), (bob).end());
    int ans = solve(both, alice, bob, other, m, k);
    ;
    ;
    cout << ans << "\n";
    if (ans != -1) {
      solve2(both, alice, bob, other, m, k, ans);
    }
  }
}