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;
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << '\n'; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
int transfer_worst(set<pair<int, int> > &from, set<pair<int, int> > &to) {
  assert(!from.empty());
  int added_val = (*from.rbegin()).first;
  to.insert(*from.rbegin());
  from.erase(*from.rbegin());
  return added_val;
}
int transfer_best(set<pair<int, int> > &from, set<pair<int, int> > &to) {
  assert(!from.empty());
  int added_val = (*from.begin()).first;
  to.insert(*from.begin());
  from.erase(*from.begin());
  return added_val;
}
int solve(vector<pair<int, int> > b00, vector<pair<int, int> > b01,
          vector<pair<int, int> > b10, vector<pair<int, int> > b11, int M,
          int K, bool print = false, int ans = -1) {
  if (print && ans == -1) {
    cout << -1 << '\n';
    return 0;
  }
  set<pair<int, int> > ans_set, free_set;
  for (auto u : b00) {
    free_set.insert(u);
  }
  for (auto u : b10) {
    free_set.insert(u);
  }
  for (auto u : b01) {
    free_set.insert(u);
  }
  int P01 = 0, P10 = 0;
  int sum_sofar = 0;
  for (auto u : b11) sum_sofar += u.first;
  int best = 2e9 + 5;
  int X = b11.size();
  42;
  42;
  42;
  42;
  for (int i = (int)b11.size() - 1; i >= -1; i--) {
    if (i + 1 < b11.size()) {
      sum_sofar -= b11[i + 1].first;
      X--;
    }
    int req01 = max(K - X, 0), req10 = max(K - X, 0);
    int req_loose = M - X - req01 - req10;
    if (req_loose < 0 || req01 > b01.size() || req10 > b10.size() ||
        req10 + req01 + req_loose > b01.size() + b10.size() + b00.size())
      continue;
    while (P01 < req01) {
      auto el = b01[P01];
      if (ans_set.find(el) == ans_set.end()) {
        sum_sofar += el.first;
      }
      ans_set.erase(el);
      free_set.erase(el);
      P01++;
    }
    while (P10 < req10) {
      auto el = b10[P10];
      if (ans_set.find(el) == ans_set.end()) {
        sum_sofar += el.first;
      }
      ans_set.erase(el);
      free_set.erase(el);
      P10++;
    }
    while (ans_set.size() < req_loose) {
      sum_sofar += transfer_best(free_set, ans_set);
    }
    while (ans_set.size() > req_loose) {
      sum_sofar -= transfer_worst(ans_set, free_set);
    }
    best = min(best, sum_sofar);
    if (sum_sofar == ans) {
      cout << ans << '\n';
      int ga = 0;
      for (auto u : ans_set) {
        cout << u.second + 1 << ' ';
        ga += u.first;
      }
      for (int i = 0; i < P01; i++) {
        cout << b01[i].second + 1 << ' ';
        ga += b01[i].first;
      }
      for (int i = 0; i < P10; i++) {
        cout << b10[i].second + 1 << ' ';
        ga += b10[i].first;
      }
      for (int i = 0; i < X; i++) {
        cout << b11[i].second + 1 << ' ';
        ga += b11[i].first;
      }
      assert(ga == ans);
      return 0;
    }
  }
  assert(!print);
  return best > 2e9 ? -1 : best;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, M, K;
  cin >> n >> M >> K;
  vector<pair<int, int> > b00, b01, b10, b11;
  for (int i = 0; i < n; i++) {
    int first, a, b;
    cin >> first >> a >> b;
    if (a && b) b11.push_back({first, i});
    if (a && !b) b10.push_back({first, i});
    if (!a && b) b01.push_back({first, i});
    if (!a && !b) b00.push_back({first, i});
  }
  sort((b11).begin(), (b11).end());
  sort((b00).begin(), (b00).end());
  sort((b10).begin(), (b10).end());
  sort((b01).begin(), (b01).end());
  int ans = solve(b00, b01, b10, b11, M, K);
  solve(b00, b01, b10, b11, M, K, 1, ans);
  return 0;
}