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 ll = long long;
using namespace std;
template <class T>
class SegmentTree {
 private:
  int _size = 1;
  T _def;
  std::vector<T> _nodes;
  std::function<T(T, T)> _op;
  std::function<T(T, T)> _update_op;

 public:
  SegmentTree(int size, T default_value, std::function<T(T, T)> op,
              std::function<T(T, T)> update_op)
      : _def(default_value), _op(op), _update_op(update_op) {
    while (_size < size) _size *= 2;
    _nodes = std::vector<T>(2 * _size, default_value);
  }
  SegmentTree(std::vector<T> nodes, T default_value, std::function<T(T, T)> op,
              std::function<T(T, T)> update_op)
      : _def(default_value), _op(op), _update_op(update_op) {
    int real_size = nodes.size();
    while (_size < real_size) _size *= 2;
    _nodes = std::vector<T>(2 * _size, default_value);
    for (int i = 0; i < real_size; ++i) _nodes[i + _size] = nodes[i];
    for (int i = _size - 1; i >= 0; --i)
      _nodes[i] = _op(_nodes[i * 2], _nodes[i * 2 + 1]);
  }
  T find(int l, int r) { return find(l, r, 1, 0, _size); }
  T find(int l, int r, int index, int cover_l, int cover_r) {
    if (cover_r <= l || r <= cover_l) return _def;
    if (l <= cover_l && cover_r <= r) return _nodes[index];
    return _op(find(l, r, index * 2, cover_l, (cover_l + cover_r) >> 1),
               find(l, r, index * 2 + 1, (cover_l + cover_r) >> 1, cover_r));
  }
  void update(int index, T value) {
    index += _size;
    _nodes[index] = _update_op(_nodes[index], value);
    while (index > 0) {
      index >>= 1;
      _nodes[index] = _op(_nodes[index * 2], _nodes[index * 2 + 1]);
    }
  }
};
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  ll max_t = 10010;
  auto cnt_segt = SegmentTree<ll>(
      max_t, 0, [](ll x, ll y) { return x + y; },
      [](ll x, ll y) { return x + y; });
  auto sum_segt = SegmentTree<ll>(
      max_t, 0, [](ll x, ll y) { return x + y; },
      [](ll x, ll y) { return x + y; });
  ll n, m, k;
  cin >> n >> m >> k;
  vector<pair<ll, ll>> a, b, both, gomi;
  ll t, al, bl;
  for (ll i = 1, iLen = (n + 1); i < iLen; ++i) {
    cin >> t >> al >> bl;
    if (al && bl)
      both.emplace_back(t, i);
    else if (al)
      a.emplace_back(t, i);
    else if (bl)
      b.emplace_back(t, i);
    else
      gomi.emplace_back(t, i);
  }
  if (min(a.size(), b.size()) + both.size() < k) {
    cout << -1 << endl;
    return 0;
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(both.begin(), both.end());
  vector<ll> a_acc(a.size() + 1), b_acc(b.size() + 1),
      both_acc(both.size() + 1);
  for (ll i = 1, iLen = (a_acc.size()); i < iLen; ++i)
    a_acc[i] += a[i - 1].first + a_acc[i - 1];
  for (ll i = 1, iLen = (b_acc.size()); i < iLen; ++i)
    b_acc[i] += b[i - 1].first + b_acc[i - 1];
  for (ll i = 1, iLen = (both_acc.size()); i < iLen; ++i)
    both_acc[i] += both[i - 1].first + both_acc[i - 1];
  for (ll i = k + 1, iLen = (a.size()); i < iLen; ++i)
    cnt_segt.update(a[i].first, 1);
  for (ll i = k + 1, iLen = (b.size()); i < iLen; ++i)
    cnt_segt.update(b[i].first, 1);
  for (ll i = 0, iLen = (both.size()); i < iLen; ++i)
    cnt_segt.update(both[i].first, 1);
  for (ll i = 0, iLen = (gomi.size()); i < iLen; ++i)
    cnt_segt.update(gomi[i].first, 1);
  for (ll i = 1, iLen = (max_t); i < iLen; ++i)
    sum_segt.update(i, cnt_segt.find(i, i + 1) * i);
  ll inf = 1LL << 60, ans = 1LL << 60, ans_i = 0;
  for (ll i = 0, iLen = (k + 1); i < iLen; ++i) {
    if (k - i < a.size()) {
      cnt_segt.update(a[k - i].first, 1);
      sum_segt.update(a[k - i].first, a[k - i].first);
    }
    if (k - i < b.size()) {
      cnt_segt.update(b[k - i].first, 1);
      sum_segt.update(b[k - i].first, b[k - i].first);
    }
    if (0 < i && i < both.size()) {
      cnt_segt.update(both[i - 1].first, -1);
      sum_segt.update(both[i - 1].first, -both[i - 1].first);
    }
    if (a.size() < k - i || b.size() < k - i || both.size() < i) continue;
    ll total = 2 * (k - i) + i;
    ll lack = m - total;
    if (lack < 0 || lack > cnt_segt.find(0, max_t)) continue;
    ll val = both_acc[i] + a_acc[k - i] + b_acc[k - i];
    ll num = 0, ng = -1, ok = max_t + 1;
    while (abs(ok - ng) > 1) {
      ll mid = (ok + ng) / 2;
      if (lack <= cnt_segt.find(0, mid))
        ok = mid;
      else
        ng = mid;
    }
    val += sum_segt.find(0, ok) - (cnt_segt.find(0, ok) - lack) * (ok - 1);
    if (ans > val) {
      ans = val;
      ans_i = i;
    }
  }
  if (ans == inf) {
    cout << -1 << endl;
    return 0;
  }
  vector<ll> ans_v;
  for (ll i = 0, iLen = (a.size()); i < iLen; ++i) {
    if (i < k - ans_i)
      ans_v.emplace_back(a[i].second);
    else
      gomi.push_back(a[i]);
  }
  for (ll i = 0, iLen = (b.size()); i < iLen; ++i) {
    if (i < k - ans_i)
      ans_v.emplace_back(b[i].second);
    else
      gomi.push_back(b[i]);
  }
  for (ll i = 0, iLen = (both.size()); i < iLen; ++i) {
    if (i < ans_i)
      ans_v.emplace_back(both[i].second);
    else
      gomi.push_back(both[i]);
  }
  sort(gomi.begin(), gomi.end());
  for (ll i = 0, iLen = (m - (2 * (k - ans_i) + ans_i)); i < iLen; ++i)
    ans_v.emplace_back(gomi[i].second);
  cout << ans << endl;
  for (auto &i : ans_v) cout << i << " ";
  cout << endl;
}