#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();
}
