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>
#pragma GCC optimize("O3")
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template <class T>
using V = vector<T>;
const ll inf = (1e18);
const ll mod = 1000000007;
const vector<int> dy = {-1, 0, 1, 0}, dx = {0, -1, 0, 1};
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll c, ll d) { return c / GCD(c, d) * d; }
struct __INIT {
  __INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} __init;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
void debag(const vector<T> &a) {
  cerr << "debag :";
  for (auto v : a) cerr << v << " ";
  cerr << "\n";
}
template <class T>
void print(const vector<T> &a) {
  for (auto v : a) cout << v << " ";
  cout << "\n";
}
template <typename T>
struct BIT {
 private:
  V<T> arr;
  int n;

 public:
  BIT(int siz) {
    n = siz;
    arr.assign(n + 1, 0);
  }
  void add(T a, T v) {
    for (T x = a; x <= n; x += (x & -x)) {
      arr[x] += v;
    }
  }
  T sum(T a) {
    T ret = 0;
    for (T x = a; x > 0; x -= (x & -x)) ret += arr[x];
    return ret;
  }
  T get(T x) {
    if (x <= 0) return 0;
    T N = 1;
    while (N * 2 <= n) N *= 2;
    T v = 0;
    for (T i = N; i > 0; i /= 2) {
      if (v + i <= n && arr[v + i] < x) {
        x -= arr[v + i];
        v += i;
      }
    }
    return v + 1;
  }
  T query(int l, int r) { return sum(r) - sum(l); }
};
void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  V<V<P>> d(4);
  V<P> tmp;
  for (int i = 0; i < n; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    ll v = 2 * b + c;
    d[v].emplace_back(a, i);
    tmp.emplace_back(a, i);
  }
  sort((tmp).begin(), (tmp).end());
  V<int> id(n);
  for (int i = 0; i < n; i++) {
    id[tmp[i].second] = i + 1;
  }
  for (int i = 0; i < 4; i++) sort((d[i]).begin(), (d[i]).end());
  ll sum = 0;
  for (int i = 0; i < min(int(d[3].size()), m); i++) {
    sum += d[3][i].first;
  }
  BIT<ll> dp(n + 10), cnt(n + 10);
  for (int i = 0; i < 4; i++) {
    for (int j = (i == 3 ? m : 0); j < d[i].size(); j++) {
      dp.add(id[d[i][j].second], d[i][j].first);
      cnt.add(id[d[i][j].second], 1);
    }
  }
  V<int> idx(4, -1);
  ll ans = inf;
  ll sum01 = 0;
  ll cnt11 = 0;
  ll ind = 0;
  for (int i = m; i >= 0; i--) {
    if (idx[1] >= int(d[1].size()) || idx[2] >= int(d[2].size())) break;
    if (idx[1] >= 0) {
      sum01 += d[1][idx[1]].first + d[2][idx[2]].first;
      dp.add(id[d[1][idx[1]].second], -d[1][idx[1]].first);
      cnt.add(id[d[1][idx[1]].second], -1);
      dp.add(id[d[2][idx[2]].second], -d[2][idx[2]].first);
      cnt.add(id[d[2][idx[2]].second], -1);
    }
    int need = m - i - (idx[1] + 1) - (idx[2] + 1);
    int v = cnt.get(need);
    if (i <= d[3].size() && need >= 0) {
      int v = cnt.get(need);
      ll val = sum01 + sum + dp.query(0, v);
      if (chmin(ans, val)) {
        cnt11 = i;
        ind = idx[1] + 1;
      }
      if (i > 0) {
        sum -= d[3][i - 1].first;
        dp.add(id[d[3][i - 1].second], d[3][i - 1].first);
        cnt.add(id[d[3][i - 1].second], 1);
      }
    }
    if (i <= k) {
      idx[1]++;
      idx[2]++;
    }
  }
  if (ans >= inf) {
    cout << -1 << "\n";
    return;
  }
  set<int> res;
  for (int i = 0; i < cnt11; i++) res.insert(d[3][i].second);
  for (int i = 0; i < ind; i++) {
    res.insert(d[1][i].second);
    res.insert(d[2][i].second);
  }
  for (int i = 0; i < n; i++) {
    if (res.size() == m) break;
    if (res.count(tmp[i].second)) continue;
    res.insert(tmp[i].second);
  }
  cout << ans << "\n";
  for (auto ite : res) cout << ite + 1 << " ";
  cout << "\n";
}
int main() {
  int t = 1;
  while (t--) solve();
}