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;
template <typename T>
void __read(T &a) {
  cin >> a;
}
template <typename T, typename... Args>
void __read(T &a, Args &...args) {
  cin >> a;
  __read(args...);
}
constexpr long long M7 = 1000000007ll;
constexpr long long M9 = 1000000009ll;
constexpr long long MFFT = 998244353ll;
template <class T>
void outv(T &a) {
  for (auto &x : a) cout << x << ' ';
}
static mt19937 rnd(static_cast<unsigned>(
    chrono::steady_clock::now().time_since_epoch().count()));
auto __fast_io__ = (ios_base::sync_with_stdio(false), cin.tie(nullptr));
int32_t main() {
  long long n, m, k;
  __read(n, m, k);
  vector<long long> tp[4];
  vector<long long> a(n);
  for (long long i = 0; i < n; ++i) {
    long long t, al, bob;
    __read(t, al, bob);
    tp[al * 2 + bob].push_back(i);
    a[i] = t;
  }
  for (long long i = 0; i < 4; ++i) {
    sort((tp[i]).begin(), (tp[i]).end(),
         [&](long long f, long long s) { return a[f] < a[s]; });
  }
  long long ans = -1;
  long long sm = 0, cnt = 0;
  for (long long i = 0; i < min(m, (long long)tp[3].size()); ++i) {
    sm += a[tp[3][i]];
    cnt++;
  }
  set<pair<long long, long long> > free, need;
  for (long long i = 0; i < tp[0].size(); ++i) {
    free.emplace(a[tp[0][i]], tp[0][i]);
  }
  for (long long i = 0; i < tp[1].size(); ++i) {
    free.emplace(a[tp[1][i]], tp[1][i]);
  }
  for (long long i = 0; i < tp[2].size(); ++i) {
    free.emplace(a[tp[2][i]], tp[2][i]);
  }
  for (long long i = min(m, (long long)tp[3].size()); i < tp[3].size(); ++i) {
    free.emplace(a[tp[3][i]], tp[3][i]);
  }
  long long sm_need = 0, sm_a = 0, sm_b = 0;
  long long ptr = 0;
  while (cnt >= 0) {
    long long ost = max(0ll, k - cnt);
    if (ost > min(tp[1].size(), tp[2].size())) {
      break;
    }
    while (ptr < ost) {
      if (need.count({a[tp[1][ptr]], tp[1][ptr]})) {
        sm_need -= a[tp[1][ptr]];
        need.erase({a[tp[1][ptr]], tp[1][ptr]});
      } else {
        free.erase({a[tp[1][ptr]], tp[1][ptr]});
      }
      if (need.count({a[tp[2][ptr]], tp[2][ptr]})) {
        sm_need -= a[tp[2][ptr]];
        need.erase({a[tp[2][ptr]], tp[2][ptr]});
      } else {
        free.erase({a[tp[2][ptr]], tp[2][ptr]});
      }
      sm_a += a[tp[2][ptr]];
      sm_b += a[tp[1][ptr]];
      ptr++;
    }
    long long q = m - cnt - 2 * ptr;
    if (q < 0) {
      break;
    }
    while (need.size() > q) {
      free.insert(*--need.end());
      sm_need -= (--need.end())->first;
      need.erase(--need.end());
    }
    while (!free.empty() && need.size() < q) {
      sm_need += free.begin()->first;
      need.insert(*free.begin());
      free.erase(free.begin());
    }
    if (need.size() == q) {
      if (ans == -1 || ans > sm + sm_a + sm_b + sm_need) {
        ans = sm + sm_a + sm_b + sm_need;
      }
    }
    cnt--;
    if (cnt >= 0) {
      free.insert({a[tp[3][cnt]], tp[3][cnt]});
      sm -= a[tp[3][cnt]];
    }
  }
  if (ans == -1) {
    cout << -1;
    return 0;
  }
  sm = 0, cnt = 0;
  for (long long i = 0; i < min(m, (long long)tp[3].size()); ++i) {
    sm += a[tp[3][i]];
    cnt++;
  }
  free.clear();
  need.clear();
  for (long long i = 0; i < tp[0].size(); ++i) {
    free.emplace(a[tp[0][i]], tp[0][i]);
  }
  for (long long i = 0; i < tp[1].size(); ++i) {
    free.emplace(a[tp[1][i]], tp[1][i]);
  }
  for (long long i = 0; i < tp[2].size(); ++i) {
    free.emplace(a[tp[2][i]], tp[2][i]);
  }
  for (long long i = min(m, (long long)tp[3].size()); i < tp[3].size(); ++i) {
    free.emplace(a[tp[3][i]], tp[3][i]);
  }
  sm_need = 0, sm_a = 0, sm_b = 0;
  ptr = 0;
  while (cnt >= 0) {
    long long ost = max(0ll, k - cnt);
    if (ost > min(tp[1].size(), tp[2].size())) {
      break;
    }
    while (ptr < ost) {
      if (need.count({a[tp[1][ptr]], tp[1][ptr]})) {
        sm_need -= a[tp[1][ptr]];
        need.erase({a[tp[1][ptr]], tp[1][ptr]});
      } else {
        free.erase({a[tp[1][ptr]], tp[1][ptr]});
      }
      if (need.count({a[tp[2][ptr]], tp[2][ptr]})) {
        sm_need -= a[tp[2][ptr]];
        need.erase({a[tp[2][ptr]], tp[2][ptr]});
      } else {
        free.erase({a[tp[2][ptr]], tp[2][ptr]});
      }
      sm_a += a[tp[2][ptr]];
      sm_b += a[tp[1][ptr]];
      ptr++;
    }
    long long q = m - cnt - 2 * ptr;
    if (q < 0) {
      break;
    }
    while (need.size() > q) {
      free.insert(*--need.end());
      sm_need -= (--need.end())->first;
      need.erase(--need.end());
    }
    while (!free.empty() && need.size() < q) {
      sm_need += free.begin()->first;
      need.insert(*free.begin());
      free.erase(free.begin());
    }
    if (need.size() == q) {
      if (ans == sm + sm_a + sm_b + sm_need) {
        cout << ans << '\n';
        for (auto &p : need) {
          cout << p.second + 1 << ' ';
        }
        for (long long i = 0; i < ptr; ++i) {
          cout << tp[1][i] + 1 << ' ';
        }
        for (long long i = 0; i < ptr; ++i) {
          cout << tp[2][i] + 1 << ' ';
        }
        for (long long i = 0; i < cnt; ++i) {
          cout << tp[3][i] + 1 << ' ';
        }
        return 0;
      }
    }
    cnt--;
    if (cnt >= 0) {
      free.insert({a[tp[3][cnt]], tp[3][cnt]});
      sm -= a[tp[3][cnt]];
    }
  }
  assert(false);
  return 0;
}