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 splay_key = long long;
struct splay_node {
  splay_node *parent = nullptr, *child[2] = {nullptr, nullptr};
  splay_key key;
  long long size = 1;
  int64_t sum = 0;
  static long long get_size(splay_node *x) {
    return x == nullptr ? 0 : x->size;
  }
  static int64_t get_sum(splay_node *x) { return x == nullptr ? 0 : x->sum; }
  long long parent_index() const {
    if (parent == nullptr) return -1;
    return this == parent->child[0] ? 0 : 1;
  }
  void set_child(long long index, splay_node *x) {
    child[index] = x;
    if (x != nullptr) x->parent = this;
  }
  void join() {
    size = get_size(child[0]) + get_size(child[1]) + 1;
    sum = get_sum(child[0]) + get_sum(child[1]) + key;
  }
};
auto random_address = [] {
  char *p = new char;
  delete p;
  return uint64_t(p);
};
mt19937_64 splay_rng(chrono::steady_clock::now().time_since_epoch().count() *
                     (random_address() | 1));
int64_t splay_count = 0;
struct splay_tree {
  static const long long POOL_SIZE = 10000;
  static vector<splay_node *> node_pool;
  static vector<splay_node *> pointers_to_delete;
  static splay_node *new_node(const splay_key &key) {
    if (node_pool.empty()) {
      splay_node *ptr = new splay_node[POOL_SIZE];
      pointers_to_delete.push_back(ptr);
      for (long long i = POOL_SIZE - 1; i >= 0; i--)
        node_pool.push_back(ptr + i);
    }
    splay_node *node = node_pool.back();
    node_pool.pop_back();
    node->key = key;
    node->sum = key;
    return node;
  }
  static void _delete_pointers() {
    static bool deleted = false;
    if (!deleted) {
      for (splay_node *node : pointers_to_delete) delete[] node;
      deleted = true;
    }
  }
  splay_node *root = nullptr;
  splay_tree() { atexit(_delete_pointers); }
  bool empty() const { return root == nullptr; }
  long long size() const { return root == nullptr ? 0 : root->size; }
  splay_node *set_root(splay_node *x) {
    if (x != nullptr) x->parent = nullptr;
    return root = x;
  }
  void rotate_up(splay_node *x, bool x_join = true) {
    splay_node *p = x->parent, *gp = p->parent;
    long long index = x->parent_index();
    if (gp == nullptr)
      set_root(x);
    else
      gp->set_child(p->parent_index(), x);
    p->set_child(index, x->child[!index]);
    x->set_child(!index, p);
    p->join();
    if (x_join) x->join();
  }
  void splay(splay_node *x) {
    splay_count++;
    while (x != root) {
      splay_node *p = x->parent;
      if (p != root)
        rotate_up(x->parent_index() == p->parent_index() ? p : x, false);
      rotate_up(x, false);
    }
    x->join();
  }
  static constexpr double SPLAY_RNG_RANGE = double(splay_rng.max()) + 1.0;
  static constexpr double LOG_CONSTANT = 2.0;
  static constexpr double SPLAY_PROBABILITY = 0.25;
  static const long long SIZE_CUTOFF = 100;
  void check_splay(splay_node *x, long long depth) {
    long long n = size(), log_n = 32 - __builtin_clz(n);
    if (depth > LOG_CONSTANT * log_n ||
        (n < SIZE_CUTOFF &&
         double(splay_rng()) < SPLAY_PROBABILITY * SPLAY_RNG_RANGE))
      splay(x);
  }
  pair<splay_node *, long long> insert(const splay_key &key,
                                       bool require_unique = false) {
    return insert(new_node(key), require_unique);
  }
  pair<splay_node *, long long> insert(splay_node *x,
                                       bool require_unique = false) {
    if (root == nullptr) return {set_root(x), 0};
    splay_node *current = root, *prev = nullptr;
    long long below = 0, depth = 0;
    while (current != nullptr) {
      prev = current;
      depth++;
      if (current->key < x->key) {
        below += splay_node::get_size(current->child[0]) + 1;
        current = current->child[1];
      } else {
        if (require_unique && !(x->key < current->key)) {
          below += splay_node::get_size(current->child[0]);
          check_splay(current, depth);
          return {current, below};
        }
        current = current->child[0];
      }
    }
    prev->set_child(prev->key < x->key ? 1 : 0, x);
    check_splay(x, depth);
    for (splay_node *node = x; node != nullptr; node = node->parent)
      node->join();
    return {x, below};
  }
  splay_node *begin() {
    if (root == nullptr) return nullptr;
    splay_node *x = root;
    long long depth = 0;
    while (x->child[0] != nullptr) {
      x = x->child[0];
      depth++;
    }
    check_splay(x, depth);
    return x;
  }
  splay_node *successor(splay_node *x) const {
    if (x == nullptr) return nullptr;
    if (x->child[1] != nullptr) {
      x = x->child[1];
      while (x->child[0] != nullptr) x = x->child[0];
      return x;
    }
    while (x->parent_index() == 1) x = x->parent;
    return x->parent;
  }
  splay_node *last() {
    if (root == nullptr) return nullptr;
    splay_node *x = root;
    long long depth = 0;
    while (x->child[1] != nullptr) {
      x = x->child[1];
      depth++;
    }
    check_splay(x, depth);
    return x;
  }
  void clear() {
    vector<splay_node *> nodes;
    nodes.reserve(size());
    for (splay_node *node = begin(); node != nullptr; node = successor(node))
      nodes.push_back(node);
    for (splay_node *node : nodes) {
      *node = splay_node();
      node_pool.push_back(node);
    }
    set_root(nullptr);
  }
  void erase(splay_node *x) {
    splay_node *new_x = nullptr, *fix_node = nullptr;
    if (x->child[0] == nullptr || x->child[1] == nullptr) {
      new_x = x->child[x->child[0] == nullptr ? 1 : 0];
      fix_node = x->parent;
    } else {
      splay_node *next = successor(x);
      assert(next != nullptr && next->child[0] == nullptr);
      new_x = next;
      fix_node = next->parent == x ? next : next->parent;
      next->parent->set_child(next->parent_index(), next->child[1]);
      next->set_child(0, x->child[0]);
      next->set_child(1, x->child[1]);
    }
    if (x == root)
      set_root(new_x);
    else
      x->parent->set_child(x->parent_index(), new_x);
    long long depth = 0;
    for (splay_node *node = fix_node; node != nullptr; node = node->parent) {
      node->join();
      depth++;
    }
    if (fix_node != nullptr) check_splay(fix_node, depth);
    *x = splay_node();
    node_pool.push_back(x);
  }
  pair<splay_node *, long long> lower_bound(const splay_key &key) {
    splay_node *current = root, *prev = nullptr, *answer = nullptr;
    long long below = 0, depth = 0;
    while (current != nullptr) {
      prev = current;
      depth++;
      if (current->key < key) {
        below += splay_node::get_size(current->child[0]) + 1;
        current = current->child[1];
      } else {
        answer = current;
        current = current->child[0];
      }
    }
    if (prev != nullptr) check_splay(prev, depth);
    return make_pair(answer, below);
  }
  bool contains(const splay_key &key) {
    splay_node *node = lower_bound(key).first;
    return node != nullptr && node->key == key;
  }
  bool erase(const splay_key &key) {
    splay_node *x = lower_bound(key).first;
    if (x == nullptr || x->key != key) return false;
    erase(x);
    return true;
  }
  splay_node *node_at_index(long long index) {
    if (index < 0 || index >= size()) return nullptr;
    splay_node *current = root;
    long long depth = 0;
    while (current != nullptr) {
      long long left_size = splay_node::get_size(current->child[0]);
      depth++;
      if (index == left_size) {
        check_splay(current, depth);
        return current;
      }
      if (index < left_size) {
        current = current->child[0];
      } else {
        current = current->child[1];
        index -= left_size + 1;
      }
    }
    assert(false);
  }
  splay_node *query_prefix_key(const splay_key &key) {
    splay_node *node = lower_bound(key).first;
    if (node == nullptr) return root;
    splay(node);
    return node->child[0];
  }
  splay_node *query_prefix_count(long long prefix) {
    if (prefix <= 0) return nullptr;
    if (prefix >= size()) return root;
    splay_node *node = node_at_index(prefix);
    splay(node);
    return node->child[0];
  }
};
vector<splay_node *> splay_tree::node_pool;
vector<splay_node *> splay_tree::pointers_to_delete;
void print_tree(splay_node *x, long long depth = 0) {
  if (x == nullptr) return;
  for (long long i = 0; i < depth; i++) cerr << ' ';
  cerr << x->key << " (" << x->size << ", " << x->sum << ")\n";
  print_tree(x->child[0], depth + 1);
  print_tree(x->child[1], depth + 1);
}
int32_t main() {
  splay_tree tree;
  long long n, m, k;
  cin >> n >> m >> k;
  vector<long long> wt[4];
  vector<pair<long long, long long>> wt_id[4];
  for (long long i = 0; i < n; i++) {
    long long x, y, z;
    cin >> x >> y >> z;
    wt[2 * y + z].emplace_back(x);
    wt_id[2 * y + z].emplace_back(x, i);
  }
  for (long long i = 0; i < 4; i++) sort(wt_id[i].begin(), wt_id[i].end());
  for (long long i = 0; i < 4; i++) sort(wt[i].begin(), wt[i].end());
  long long SZM = min((long long)(wt[1].size()), (long long)(wt[2].size()));
  vector<long long> pfx(SZM + 1);
  for (long long i = 1; i <= SZM; i++) {
    pfx[i] = pfx[i - 1] + wt[1][i - 1] + wt[2][i - 1];
  }
  for (long long x : wt[0]) tree.insert(x);
  for (long long i = k + 1; i < (long long)(wt[1].size()); i++)
    tree.insert(wt[1][i]);
  for (long long i = k + 1; i < (long long)(wt[2].size()); i++)
    tree.insert(wt[2][i]);
  long long sm = 0, mn = 1e18, mnx = -1;
  for (long long x = 0; x <= (long long)(wt[3].size()); x++) {
    if (x) sm += wt[3][x - 1];
    long long req = k - x;
    if (req >= 0 and req < (long long)(wt[1].size())) tree.insert(wt[1][req]);
    if (req >= 0 and req < (long long)(wt[2].size())) tree.insert(wt[2][req]);
    req = max(req, 0LL);
    if (req <= SZM and x + 2 * req <= m) {
      long long rem = m - (x + 2 * req);
      if (rem <= tree.size()) {
        long long ex = splay_node::get_sum(tree.query_prefix_count(rem));
        if (mn >= sm + pfx[req] + ex) {
          mn = sm + pfx[req] + ex;
          mnx = x;
        }
      }
    }
  }
  if (mnx < 0) return cout << "-1", 0;
  cout << mn << "\n";
  for (long long i = 0; i < mnx; i++) cout << wt_id[3][i].second + 1 << " ";
  long long reqAB = max(k - mnx, 0LL);
  for (long long i = 0; i < reqAB; i++) {
    cout << wt_id[2][i].second + 1 << " " << wt_id[1][i].second + 1 << " ";
  }
  vector<pair<long long, long long>> temp;
  for (long long i = reqAB; i < (long long)(wt[1].size()); i++)
    temp.push_back(wt_id[1][i]);
  for (long long i = reqAB; i < (long long)(wt[2].size()); i++)
    temp.push_back(wt_id[2][i]);
  for (auto pr : wt_id[0]) temp.push_back(pr);
  sort(temp.begin(), temp.end());
  long long rem = m - (mnx + reqAB * 2);
  for (long long i = 0; i < rem; i++) cout << temp[i].second + 1 << " ";
}