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;
const long double PI = acos(-1);
long long power(long long b, long long e, long long m) {
  if (e == 0) return 1;
  if (e & 1) return b * power(b * b % m, e / 2, m) % m;
  return power(b * b % m, e / 2, m);
}
long long power(long long b, long long e) {
  if (e == 0) return 1;
  if (e & 1) return b * power(b * b, e / 2);
  return power(b * b, e / 2);
}
template <typename T, typename U>
static inline void amin(T &x, U y) {
  if (y < x) x = y;
}
template <typename T, typename U>
static inline void amax(T &x, U y) {
  if (x < y) x = y;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << '(' << p.first << "," << p.second << ')';
}
const int N = 200005;
struct node {
  int sum;
  int cnt;
} fake;
struct SEGTREE {
  long long a[N];
  SEGTREE() { memset(a, 0, sizeof(a)); }
  node tree[4 * N];
  inline node combine(node a, node b) {
    node ret;
    ret.sum = a.sum + b.sum;
    ret.cnt = a.cnt + b.cnt;
    return ret;
  }
  inline node make_node(int x = 0, int y = 0) {
    node ret;
    ret.sum = x;
    ret.cnt = y;
    return ret;
  }
  void buildTree(long long v, long long st, long long en) {
    if (st == en) {
      tree[v] = make_node();
      return;
    }
    long long mid = (st + en) >> 1;
    buildTree(v << 1, st, mid);
    buildTree(v << 1 | 1, mid + 1, en);
    tree[v] = combine(tree[v << 1], tree[v << 1 | 1]);
  }
  void update(long long v, long long st, long long en, long long in,
              long long val) {
    long long mid = (st + en) >> 1;
    if (st == en) {
      tree[v] = make_node(val, 1);
      return;
    }
    if (in <= mid)
      update(v << 1, st, mid, in, val);
    else
      update(v << 1 | 1, mid + 1, en, in, val);
    tree[v] = combine(tree[v << 1], tree[v << 1 | 1]);
  }
  int query(long long v, long long st, long long en, long long val) {
    if (val == 0) return 0;
    if (st == en) return tree[v].sum;
    int mid = (st + en) >> 1;
    if (tree[v << 1].cnt >= val)
      return query(v << 1, st, mid, val);
    else
      return tree[v << 1].sum +
             query(v << 1 | 1, mid + 1, en, val - tree[v << 1].cnt);
  }
} seg;
vector<pair<int, int>> same, one, two, none;
vector<pair<int, int>> id;
int n, m, k;
int findI(pair<int, int> &val) {
  auto it = lower_bound(id.begin(), id.end(), val) - id.begin();
  return it;
}
int _runtimeTerror_() {
  cin >> n >> m >> k;
  int ans = 2e9 + 100;
  for (int i = 1; i <= n; ++i) {
    int t, a, b;
    cin >> t >> a >> b;
    if (a == 1 && b == 1)
      same.push_back({t, i});
    else if (a == 1)
      one.push_back({t, i});
    else if (b == 1)
      two.push_back({t, i});
    else
      none.push_back({t, i});
    id.push_back({t, i});
  }
  sort(id.begin(), id.end());
  sort(none.begin(), none.end());
  sort(same.begin(), same.end());
  sort(one.begin(), one.end());
  sort(two.begin(), two.end());
  int x = min({(int)one.size(), (int)two.size(), k});
  seg.buildTree(1, 0, n - 1);
  int sum1 = 0, sum2 = 0;
  for (int i = 0; i < x; ++i) sum1 += one[i].first, sum2 += two[i].first;
  for (auto k : none) {
    int it = findI(k);
    seg.update(1, 0, n - 1, it, k.first);
  }
  int idx = -1;
  for (int i = x; i < (int)one.size(); ++i) {
    auto it = findI(one[i]);
    seg.update(1, 0, n - 1, it, one[i].first);
  }
  for (int i = x; i < (int)two.size(); ++i) {
    auto it = findI(two[i]);
    seg.update(1, 0, n - 1, it, two[i].first);
  }
  int cur = x - 1;
  int $ = min((int)same.size(), m);
  int sum = 0, kitne = 0;
  for (int i = 0; i <= $; ++i) {
    if (cur + 1 + i >= k) {
      while (cur >= 0 && cur + i + 1 > k) {
        auto it = findI(two[cur]);
        seg.update(1, 0, n - 1, it, two[cur].first);
        it = findI(one[cur]);
        seg.update(1, 0, n - 1, it, one[cur].first);
        sum1 -= one[cur].first;
        sum2 -= two[cur].first;
        --cur;
      }
      if (i + 2 * (cur + 1) <= m && i + 2 * (cur + 1) + seg.tree[1].cnt >= m) {
        int val =
            sum + sum1 + sum2 + seg.query(1, 0, n - 1, m - i - 2 * (cur + 1));
        if (val < ans) ans = val, idx = i, kitne = cur + 1;
      }
    }
    if (i != $) sum += same[i].first;
  }
  if (ans == 2e9 + 100) {
    cout << "-1\n";
    return 0;
  }
  cout << ans << '\n';
  vector<int> print;
  for (int i = 0; i < idx; ++i) print.push_back(same[i].second);
  for (int i = 0; i < kitne; ++i)
    print.push_back(one[i].second), print.push_back(two[i].second);
  for (auto k : print) cout << k << " ";
  int fuck = m - (int)print.size();
  for (int i = kitne; i < (int)one.size(); ++i) none.push_back(one[i]);
  for (int i = kitne; i < (int)two.size(); ++i) none.push_back(two[i]);
  sort(none.begin(), none.end());
  for (int i = 0; i < fuck; ++i) cout << none[i].second << ' ';
  return 0;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int TESTS = 1;
  while (TESTS--) _runtimeTerror_();
  return 0;
}