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;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}
inline int add(int a, int b) {
  a += b;
  return a >= 1000000007 ? a - 1000000007 : a;
}
inline int sub(int a, int b) {
  a -= b;
  return a < 0 ? a + 1000000007 : a;
}
inline int mul(int a, int b) { return (long long int)a * b % 1000000007; }
int Set(int N, int pos) { return N = N | (1 << pos); }
int Reset(int N, int pos) { return N = N & ~(1 << pos); }
bool Cheek(int N, int pos) { return (bool)(N & (1 << pos)); }
long long int a[200005], b[200005], tim[200005];
void Solve(int t) {
  priority_queue<pair<long long int, long long int> > both, alice, bob, ajaira;
  long long int i, j, k, n, m, boblike = 0, alicelike = 0, ans = 0, atake = 0,
                               btake = 0;
  cin >> n >> m >> k;
  for (i = 1; i <= n; i++) {
    cin >> tim[i] >> a[i] >> b[i];
    if (a[i] && b[i]) {
      alicelike++;
      boblike++;
      both.push({-tim[i], i});
    } else if (a[i]) {
      alicelike++;
      alice.push({-tim[i], i});
    } else if (b[i]) {
      boblike++;
      bob.push({-tim[i], i});
    } else
      ajaira.push({-tim[i], i});
  }
  if (alicelike < k || boblike < k) {
    printf("-1\n");
    return;
  }
  priority_queue<pair<long long int, long long int> > taken1, taken2, taken3;
  while (atake < k && !alice.empty()) {
    ans += ((-1) * alice.top().first);
    taken1.push({-alice.top().first, alice.top().second});
    alice.pop();
    atake++;
  }
  while (btake < k && !bob.empty()) {
    ans += ((-1) * bob.top().first);
    taken2.push({-bob.top().first, bob.top().second});
    bob.pop();
    btake++;
  }
  long long int sz1 = taken1.size();
  long long int sz2 = taken2.size();
  while (sz1 != sz2) {
    if (sz1 > sz2) {
      ans -= taken1.top().first;
      alice.push({-taken1.top().first, taken1.top().second});
      taken1.pop();
      sz1--;
      atake--;
    } else {
      ans -= taken2.top().first;
      bob.push({-taken2.top().first, taken2.top().second});
      taken2.pop();
      sz2--;
      btake--;
    }
  }
  while (!both.empty() && (atake < k || btake < k)) {
    ans += (-both.top().first);
    taken3.push({-both.top().first, both.top().second});
    both.pop();
    atake++;
    btake++;
  }
  while (
      !both.empty() && !taken1.empty() && !taken2.empty() &&
      ((-1) * both.top().first < (taken1.top().first + taken2.top().first))) {
    ans -= taken1.top().first;
    ans -= taken2.top().first;
    alice.push({-taken1.top().first, taken1.top().second});
    bob.push({-taken2.top().first, taken2.top().second});
    taken1.pop();
    taken2.pop();
    ans += ((-1) * both.top().first);
    taken3.push({-both.top().first, both.top().second});
    both.pop();
  }
  sz1 = taken1.size();
  sz2 = taken2.size();
  long long int sz3 = taken3.size();
  while (sz1 + sz2 + sz3 < m && !taken3.empty() && !alice.empty() &&
         !bob.empty()) {
    long long int now = taken3.top().first;
    long long int hobe = -(alice.top().first + bob.top().first);
    long long int xtra = hobe - now;
    bool fg = 1;
    if (!alice.empty() && -alice.top().first < xtra) fg = 0;
    if (!bob.empty() && -bob.top().first < xtra) fg = 0;
    if (!ajaira.empty() && -ajaira.top().first < xtra) fg = 0;
    if (fg) {
      ans -= taken3.top().first;
      both.push(make_pair(-taken3.top().first, taken3.top().second));
      taken3.pop();
      sz3--;
      ans += -alice.top().first;
      taken1.push(make_pair(-alice.top().first, alice.top().second));
      alice.pop();
      sz1++;
      ans += -bob.top().first;
      taken2.push(make_pair(-bob.top().first, bob.top().second));
      bob.pop();
      sz2++;
    } else {
      int take = 0;
      pair<long long int, long long int> pr1 = {(1 << 28), (1 << 28)};
      if (!alice.empty() && -alice.top().first < pr1.first) {
        pr1 = {-alice.top().first, alice.top().second};
        take = 1;
      }
      if (!bob.empty() && -bob.top().first < pr1.first) {
        pr1 = {-bob.top().first, bob.top().second};
        take = 2;
      }
      if (!both.empty() && -both.top().first < pr1.first) {
        pr1 = {-both.top().first, both.top().second};
        take = 3;
      }
      if (!ajaira.empty() && -ajaira.top().first < pr1.first) {
        pr1 = {-ajaira.top().first, ajaira.top().second};
        take = 4;
      }
      if (take == 1)
        alice.pop();
      else if (take == 2)
        bob.pop();
      else if (take == 3)
        both.pop();
      else
        ajaira.pop();
      ans += pr1.first;
      taken3.push(pr1);
      sz3++;
    }
  }
  while (sz1 + sz2 + sz3 < m &&
         (!alice.empty() || !bob.empty() || !both.empty())) {
    int fg = 0;
    pair<long long int, long long int> pr1 = {(1 << 28), (1 << 28)};
    if (!alice.empty() && -alice.top().first < pr1.first) {
      pr1 = {-alice.top().first, alice.top().second};
      fg = 1;
    }
    if (!bob.empty() && -bob.top().first < pr1.first) {
      pr1 = {-bob.top().first, bob.top().second};
      fg = 2;
    }
    if (!both.empty() && -both.top().first < pr1.first) {
      pr1 = {-both.top().first, both.top().second};
      fg = 3;
    }
    if (!ajaira.empty() && -ajaira.top().first < pr1.first) {
      pr1 = {-ajaira.top().first, ajaira.top().second};
      fg = 4;
    }
    if (fg == 1)
      alice.pop();
    else if (fg == 2)
      bob.pop();
    else if (fg == 3)
      both.pop();
    else
      ajaira.pop();
    ans += pr1.first;
    taken3.push(pr1);
    sz3++;
  }
  while (sz1 + sz2 + sz3 > m && !both.empty() && sz1 > 0 && sz2 > 0) {
    ans -= taken1.top().first;
    taken1.pop();
    ans -= taken2.top().first;
    taken2.pop();
    ans += (-both.top().first);
    taken3.push({-both.top().first, both.top().second});
    both.pop();
    sz1--;
    sz2--;
    sz3++;
  }
  if (sz1 + sz2 + sz3 != m) {
    cout << "-1\n";
    return;
  }
  cout << ans << endl;
  while (!taken1.empty()) {
    cout << taken1.top().second << ' ';
    taken1.pop();
  }
  while (!taken2.empty()) {
    cout << taken2.top().second << ' ';
    taken2.pop();
  }
  while (!taken3.empty()) {
    cout << taken3.top().second << ' ';
    taken3.pop();
  }
  cout << endl;
}
int main() {
  int t, T;
  T = 1;
  for (__typeof(t) t = 1; t <= T; t++) {
    Solve(t);
  }
  return 0;
}