#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define forby(i, f, n) for (int32_t (i) = (f); i < n; i++)
#define sz(x) static_cast<int32_t>(x.size())
#define vec std::vector<int32_t>
#define forward(it, i) std::next(it.begin(), i)
#define prev(it, i) std::prev(it.end(), i)
#define inf static_cast<int32_t>(2e31)

int main(int argc, char** argv) {

  int32_t n, m, k;
  scanf("%d%d%d", &n, &m, &k);

  vec vec_a;
  vec vec_b;
  vec vec_ab;
  vec vec_remains;
  vec vec_all;

  vec_all.reserve(n);

  forby (i, 0, n) {
    int32_t t, a, b;
    scanf("%d%d%d", &t, &a, &b);

    vec_all.push_back(t);

  if (a == 1 && b == 1) {
      vec_ab.push_back(i);
    } else if (a == 1) {
      vec_a.push_back(i);
    } else if (b == 1) {
      vec_b.push_back(i);
    } else {
      vec_remains.push_back(i);
    }
  }

 // check if impossible
  if (
      sz(vec_a) + sz(vec_ab) < k ||
      sz(vec_b) + sz(vec_ab) < k
     ) {
    printf("%d\n", -1);
    return 0;
  }

  std::function<bool(int32_t, int32_t)> cmp = [&vec_all](int32_t p, int32_t q) {
    return vec_all[ p ] < vec_all[ q ];
  };

  int32_t incommon = std::min(sz(vec_ab), k);
  int32_t selected_singular = k - incommon;
  int32_t to_select = m - (selected_singular * 2 + incommon);
  if (to_select < 0) {
    printf("%d\n", -1);
    return 0;
  }

  std::sort(all(vec_a), cmp);
  std::sort(all(vec_b), cmp);
  std::sort(all(vec_ab), cmp);
  std::sort(all(vec_remains), cmp);

  int32_t k_time = 0;
  forby (i, 0, incommon) {
    k_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, 0, selected_singular) {
    k_time += vec_all[ vec_a[ i ] ];
    k_time += vec_all[ vec_b[ i ] ];
  }

  std::multiset<int32_t, decltype(cmp)> m_in(cmp);
  std::multiset<int32_t, decltype(cmp)> m_out(cmp);
  int32_t ms_time = 0;
  forby (i, incommon, sz(vec_ab)) {
    m_in.insert(vec_ab[ i ]);
    ms_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, selected_singular, sz(vec_a)) {
    m_in.insert(vec_a[ i ]);
    ms_time += vec_all[ vec_a[ i ] ];
  }
  forby (i, selected_singular, sz(vec_b)) {
    m_in.insert(vec_b[ i ]);
    ms_time += vec_all[ vec_b[ i ] ];
  }
  forby (i, 0, sz(vec_remains)) {
    ms_time += vec_all[ vec_remains[ i ] ];
    m_in.insert(vec_remains[ i ]);
  }
  while (sz(m_in) > to_select) {
    ms_time -= vec_all[ *prev(m_in, 1) ];
    m_out.insert(*prev(m_in, 1));
    m_in.erase(prev(m_in, 1));
  }

  int32_t total_time = inf;
  while (true) {

    int32_t current_time = k_time + ms_time;
    if (current_time > total_time) {
      break;
    }
    total_time = current_time;

    if (
        incommon == 0 ||
        selected_singular == std::min(sz(vec_a), sz(vec_b)) ||
        to_select == 0
    ) {
      break;
    }

    incommon--;
    k_time -= vec_all[ vec_ab[ incommon ] ];
    k_time += vec_all[ vec_a[ selected_singular ] ];
    k_time += vec_all[ vec_b[ selected_singular ] ];
    to_select--;
    decltype(m_in)::iterator it_a = m_in.find(vec_a[ selected_singular ]);
    if (it_a == m_in.end()) {
      m_out.erase(m_out.find(*it_a));
    } else {
      ms_time -= vec_all[ vec_a[ selected_singular ] ];
      m_in.erase(it_a);
    }
    decltype(m_in)::iterator it_b = m_in.find(vec_b[ selected_singular ]);
    if (it_b == m_in.end()) {
      m_out.erase(m_out.find(*it_b));
    } else {
      ms_time -= vec_all[ vec_b[ selected_singular ] ];
      m_in.erase(it_b);
    }
    decltype(m_in)::iterator it_ab = m_in.find(vec_ab[ incommon ]);
    if (it_ab != m_in.end()) {
      ms_time += vec_all[ vec_ab[ incommon ] ];
      m_in.insert(vec_ab[ incommon ]);
    }

    while (sz(m_in) < to_select) {
      ms_time += vec_all[ *forward(m_out, 0) ];
      m_in.insert(*forward(m_out, 0));
      m_out.erase(forward(m_out, 0));
    }
    while (sz(m_in) > to_select) {
      ms_time -= vec_all[ *prev(m_in, 1) ];
      m_out.insert(*prev(m_in, 1));
      m_in.erase(prev(m_in, 1));
    }
    while (sz(m_in) > 0 && sz(m_out) > 0 && vec_all[ *prev(m_in, 1) ] > vec_all[ *forward(m_out, 0) ]) {
      int32_t tmp_in = *prev(m_in, 1);
      int32_t tmp_out = *forward(m_out, 0);
      m_in.erase(prev(m_in, 1));
      m_out.erase(forward(m_out, 0));
      m_in.insert(tmp_out);
      m_out.insert(tmp_in);
      ms_time -= vec_all[ tmp_in ];
      ms_time += vec_all[ tmp_out ];
    }

    selected_singular++;
  }

  printf("%d\n", total_time);
  forby(i, 0, incommon) {
    printf("%d ", vec_ab[ i ] + 1);
  }
  forby(i, 0, selected_singular) {
    printf("%d %d ", vec_a[ i ] + 1, vec_b[ i ] + 1);
  }
  for (int32_t i : m_in) {
    printf("%d ", i + 1);
  }
  printf("\n");

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define forby(i, f, n) for (int32_t (i) = (f); i < n; i++)
#define sz(x) static_cast<int32_t>(x.size())
#define vec std::vector<int32_t>
#define forward(it, i) std::next(it.begin(), i)
#define prev(it, i) std::prev(it.end(), i)
#define inf static_cast<int32_t>(2e31)

int main(int argc, char** argv) {

  int32_t n, m, k;
  scanf("%d%d%d", &n, &m, &k);

  vec vec_a;
  vec vec_b;
  vec vec_ab;
  vec vec_remains;
  vec vec_all;

  vec_all.reserve(n);

  forby (i, 0, n) {
    int32_t t, a, b;
    scanf("%d%d%d", &t, &a, &b);

    vec_all.push_back(t);

  if (a == 1 && b == 1) {
      vec_ab.push_back(i);
    } else if (a == 1) {
      vec_a.push_back(i);
    } else if (b == 1) {
      vec_b.push_back(i);
    } else {
      vec_remains.push_back(i);
    }
  }

 // check if impossible
  if (
      sz(vec_a) + sz(vec_ab) < k ||
      sz(vec_b) + sz(vec_ab) < k
     ) {
    printf("%d\n", -1);
    return 0;
  }

  std::function<bool(int32_t, int32_t)> cmp = [&vec_all](int32_t p, int32_t q) {
    return vec_all[ p ] < vec_all[ q ];
  };

  int32_t incommon = std::min(sz(vec_ab), k);
  int32_t selected_singular = k - incommon;
  int32_t to_select = m - (selected_singular * 2 + incommon);
  if (to_select < 0) {
    printf("%d\n", -1);
    return 0;
  }

  std::sort(all(vec_a), cmp);
  std::sort(all(vec_b), cmp);
  std::sort(all(vec_ab), cmp);
  std::sort(all(vec_remains), cmp);

  int32_t k_time = 0;
  forby (i, 0, incommon) {
    k_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, 0, selected_singular) {
    k_time += vec_all[ vec_a[ i ] ];
    k_time += vec_all[ vec_b[ i ] ];
  }

  std::multiset<int32_t, decltype(cmp)> m_in(cmp);
  std::multiset<int32_t, decltype(cmp)> m_out(cmp);
  int32_t ms_time = 0;
  forby (i, incommon, sz(vec_ab)) {
    m_in.insert(vec_ab[ i ]);
    ms_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, selected_singular, sz(vec_a)) {
    m_in.insert(vec_a[ i ]);
    ms_time += vec_all[ vec_a[ i ] ];
  }
  forby (i, selected_singular, sz(vec_b)) {
    m_in.insert(vec_b[ i ]);
    ms_time += vec_all[ vec_b[ i ] ];
  }
  forby (i, 0, sz(vec_remains)) {
    ms_time += vec_all[ vec_remains[ i ] ];
    m_in.insert(vec_remains[ i ]);
  }
  while (sz(m_in) > to_select) {
    ms_time -= vec_all[ *prev(m_in, 1) ];
    m_out.insert(*prev(m_in, 1));
    m_in.erase(prev(m_in, 1));
  }

  int32_t total_time = inf;
  while (true) {

    int32_t current_time = k_time + ms_time;
    if (current_time > total_time) {
      break;
    }
    total_time = current_time;

    if (
        incommon == 0 ||
        selected_singular == std::min(sz(vec_a), sz(vec_b)) ||
        to_select == 0
    ) {
      break;
    }

    incommon--;
    k_time -= vec_all[ vec_ab[ incommon ] ];
    k_time += vec_all[ vec_a[ selected_singular ] ];
    k_time += vec_all[ vec_b[ selected_singular ] ];
    to_select--;
    decltype(m_in)::iterator it_a = m_in.find(vec_a[ selected_singular ]);
    if (it_a != m_in.end()) {
      ms_time -= vec_all[ vec_a[ selected_singular ] ];
      m_in.erase(it_a);
    }
    decltype(m_in)::iterator it_b = m_in.find(vec_b[ selected_singular ]);
    if (it_b != m_in.end()) {
      ms_time -= vec_all[ vec_b[ selected_singular ] ];
      m_in.erase(it_b);
    }
    decltype(m_in)::iterator it_ab = m_in.find(vec_ab[ incommon ]);
    if (it_ab != m_in.end()) {
      ms_time += vec_all[ vec_ab[ incommon ] ];
      m_in.insert(vec_ab[ incommon ]);
    }

    while (sz(m_in) < to_select) {
      ms_time += vec_all[ *forward(m_out, 0) ];
      m_in.insert(*forward(m_out, 0));
      m_out.erase(forward(m_out, 0));
    }
    while (sz(m_in) > to_select) {
      ms_time -= vec_all[ *prev(m_in, 1) ];
      m_out.insert(*prev(m_in, 1));
      m_in.erase(prev(m_in, 1));
    }

    selected_singular++;
  }

  printf("%d\n", total_time);
  forby(i, 0, incommon) {
    printf("%d ", vec_ab[ i ] + 1);
  }
  forby(i, 0, selected_singular) {
    printf("%d %d ", vec_a[ i ] + 1, vec_b[ i ] + 1);
  }
  for (int32_t i : m_in) {
    printf("%d ", i + 1);
  }
  printf("\n");

  return 0;
}


#include <vector>
#include <bits/stdc++.h>
#include <iostream>
#define forby(i, a, z) for (int32_t (i) = a; i < z; i++)
#define vec std::vector<int32_t>
#define size(x) static_cast<int32_t>(x.size())
#define first(x) std::next(x.begin(), 0)
#define last(x) std::prev(x.end(), 1)
#define all(x) x.begin(), x.end()

int main(int argc, char** argv) {

  int32_t n, m, k;
  scanf("%d%d%d", &n, &m, &k);

  vec va;
  vec vb;
  vec vab;
  vec vall;
  vec vr;

  auto cmp = [&vall](int32_t p, int32_t q) {
    return vall[ p ] < vall[ q ];
  };

  std::multiset<int32_t, decltype(cmp)> setout(cmp);
  forby(i, 0, n) {
    int32_t t, a, b;
    scanf("%d%d%d", &t, &a, &b);

    vall.push_back(t);
    if (a && b) {
      vab.push_back(i);
    } else if (a) {
      va.push_back(i);
    } else if (b) {
      vb.push_back(i);
    } else {
      vr.push_back(i);
    }
  }
  forby (i, 0, size(vr)) {
    setout.insert(vr[ i ]);
  }
  std::sort(all(va), cmp);
  std::sort(all(vb), cmp);
  std::sort(all(vab), cmp);
  if (size(va) + size(vab) < k || size(vb) + size(vab) < k) {
    printf("-1\n");
    return 0;
  }

  int32_t incommon = std::min(size(vab), k);
  int32_t singular = k - incommon;

  if (singular > std::min(size(va), size(vb))) {
    printf("-1\n");
    return 0;
  }

  int32_t selected = (singular * 2) + incommon;
  if (selected > m) {
    printf("-1\n");
    return 0;
  }

  std::multiset<int32_t, decltype(cmp)> setin(cmp);
  int32_t settime = 0;
  auto balance = [&vall, &setin, &setout, &settime](int32_t n) {
    while (size(setin) < n) {
      auto f = first(setout);
      setin.insert(*f);
      setout.erase(f);
      settime += vall[ *f ];
    }
    while (size(setin) > n) {
      auto l = last(setin);
      setout.insert(*l);
      setin.erase(l);
      settime -= vall[ *l ];
    }
    while (size(setin) > 0 && size(setout) > 0 && *last(setin) > *first(setout)) {
      auto f = first(setout);
      auto l = last(setin);
      setout.insert(*l);
      setin.insert(*f);
      settime -= vall[ *l ];
      settime += vall[ *f ];
      setout.erase(f);
      setin.erase(l);
    }
  };

  int32_t ktime = 0;
  forby (i, 0, incommon) {
    ktime += vall[ vab[ i ] ];
  }
  forby(i, 0, singular) {
    ktime += (vall[ va[ i ] ] + vall[ vb[ i ] ]);
  }

  int32_t to_select = m - selected;
  forby (i, incommon, size(vab)) {
    setin.insert(vab[ i ]);
    settime += vall[ vab[ i ] ];
  }
  forby (i, singular, size(va)) {
    setin.insert(va[ i ]);
    settime += vall[ va[ i ] ];
  }
  forby (i, singular, size(vb)) {
    setin.insert(vb[ i ]);
    settime += vall[ vb[ i ] ];
  }
  balance(to_select);
  int32_t totaltime = ktime + settime;
  int32_t bestincommon = incommon;
  while (incommon > 0 && singular < std::min(size(va), size(vb)) && to_select > 0) {
    incommon--;
    ktime -= vall[ vab[ incommon ] ];

    auto setait = setin.find(va[ singular ]);
    if (setait == setin.end()) {
      setout.erase(setout.find(va[ singular ]));
    } else {
      setin.erase(setait);
    }
    auto setbit = setin.find(vb[ singular ]);
    if (setbit == setin.end()) {
      setout.erase(setout.find(vb[ singular ]));
    } else {
      setin.erase(setbit);
    }

    ktime += vall[ va[ singular ] ] + vall[ vb[ singular ] ];
    singular++;
    to_select--;

    balance(to_select);

    int32_t currentltime = ktime + settime;
    if (currentltime < totaltime) {
      totaltime = currentltime;
      bestincommon = incommon;
    }
  }

  int32_t bestsingular = k - bestincommon;
  int32_t bestremains = m - bestincommon - (bestsingular * 2);
  forby (i, bestincommon, size(vab)) {
    vr.push_back(vab[ i ]);
  }
  forby (i, bestsingular, size(va)) {
    vr.push_back(va[ i ]);
  }
  forby (i, bestsingular, size(vb)) {
    vr.push_back(vb[ i ]);
  }
  std::sort(all(vr), cmp);

  // answer
  printf("%d\n", totaltime);
  forby (i, 0, bestincommon) {
    printf("%d ", vab[ i ] + 1);
  }
  forby (i, 0, bestsingular) {
    printf("%d %d ", va[ i ] + 1, vb[ i ] + 1);
  }
  forby (i, 0, bestremains) {
    printf("%d ", vr[ i ] + 1);
  }
  printf("\n");

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define forby(i, f, n) for (int32_t (i) = (f); i < n; i++)
#define sz(x) static_cast<int32_t>(x.size())
#define vec std::vector<int32_t>
#define forward(it, i) std::next(it.begin(), i)
#define inf static_cast<int32_t>(2e31)

int main(int argc, char** argv) {

  int32_t n, m, k;
  scanf("%d%d%d", &n, &m, &k);

  vec vec_a;
  vec vec_b;
  vec vec_ab;
  vec vec_remains;
  vec vec_all;

  vec_all.reserve(n);

  forby (i, 0, n) {
    int32_t t, a, b;
    scanf("%d%d%d", &t, &a, &b);

    vec_all.push_back(t);

  if (a == 1 && b == 1) {
      vec_ab.push_back(i);
    } else if (a == 1) {
      vec_a.push_back(i);
    } else if (b == 1) {
      vec_b.push_back(i);
    } else {
      vec_remains.push_back(i);
    }
  }

  // check if impossible
  if (
      sz(vec_a) + sz(vec_ab) < k ||
      sz(vec_b) + sz(vec_ab) < k
     ) {
    printf("%d\n", -1);
    return 0;
  }

  std::function<bool(int32_t, int32_t)> cmp = [&vec_all](int32_t p, int32_t q) {
    return vec_all[ p ] < vec_all[ q ];
  };

  std::sort(all(vec_a), cmp);
  std::sort(all(vec_b), cmp);
  std::sort(all(vec_ab), cmp);
  std::sort(all(vec_remains), cmp);

  int32_t possible_singular = std::min(std::min(sz(vec_a), sz(vec_b)), k);
  int32_t incommon = k - possible_singular;

  std::multiset<int32_t, decltype(cmp)> ms(cmp);

  forby(i, possible_singular, sz(vec_a)) {
    ms.insert(vec_a[ i ]);
  }
  forby(i, possible_singular, sz(vec_b)) {
    ms.insert(vec_b[ i ]);
  }
  forby(i, incommon, sz(vec_ab)) {
    ms.insert(vec_ab[ i ]);
  }
  forby(i, 0, sz(vec_remains)) {
    ms.insert(vec_remains[ i ]);
  }

  // initial state
  int32_t best_incommon = -1;
  int32_t total_time = inf;
  int32_t calculated_time = 0;
  forby (i, 0, possible_singular) {
    calculated_time += vec_all[ vec_a[ i ] ];
    calculated_time += vec_all[ vec_b[ i ] ];
  }
  forby (i, 0, incommon) {
    calculated_time += vec_all[ vec_ab[ i ] ];
  }
  int32_t selected = possible_singular * 2 + incommon;
  while (incommon <= std::min(m, sz(vec_ab))) {

    if (selected <= m) {
      int32_t current_time = calculated_time;
      forby (i, 0, m - selected) {
        current_time += vec_all[ *forward(ms, i) ];
      }

      if (current_time < total_time) {
        total_time = current_time;
        best_incommon = incommon;
      }
    }

    if (incommon < std::min(m, sz(vec_ab))) {
      possible_singular--;
      // if no more to remove from singular
      if (possible_singular < 0) {
        break;
      }
      calculated_time += vec_all[ vec_ab[ incommon ] ];
      ms.erase(ms.find(vec_ab[ incommon ]));
      calculated_time -= vec_all[ vec_a[ possible_singular ] ];
      calculated_time -= vec_all[ vec_b[ possible_singular ] ];
      ms.insert(vec_a[ possible_singular ]);
      ms.insert(vec_b[ possible_singular ]);
      selected--;
    }
    incommon++;
  }

  // calculate result with best_incommon
  if (best_incommon == -1) {
    printf("%d\n", -1);
    return 0;
  }

  ms.clear();
  std::vector<int32_t> result;
  result.reserve(m);
  result.insert(result.end(), vec_a.begin(), vec_a.begin() + k - best_incommon);
  result.insert(result.end(), vec_b.begin(), vec_b.begin() + k - best_incommon);
  result.insert(result.end(), vec_ab.begin(), vec_ab.begin() + best_incommon);

  int32_t missing = m - ((k - best_incommon) * 2 + best_incommon);
  if (missing > 0) {
    forby(i, k - best_incommon, std::min(k - best_incommon + missing, sz(vec_a))) {
      ms.insert(vec_a[ i ]);
    }
    forby(i, k - best_incommon, std::min(k - best_incommon + missing, sz(vec_b))) {
      ms.insert(vec_b[ i ]);
    }
    forby(i, best_incommon, std::min(best_incommon + missing, sz(vec_ab))) {
      ms.insert(vec_ab[ i ]);
    }
    forby(i, 0, std::min(missing, sz(vec_remains))) {
      ms.insert(vec_remains[ i ]);
    }

    forby(i, 0, missing) {
      result.push_back(*forward(ms, i));
    }
  }

  printf("%d\n", total_time);
  for (int32_t i : result) {
    printf("%d ", i + 1);
  }
  printf("\n");

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define forby(i, f, n) for (int32_t (i) = (f); i < n; i++)
#define sz(x) static_cast<int32_t>(x.size())
#define vec std::vector<int32_t>
#define forward(it, i) std::next(it.begin(), i)
#define prev(it, i) std::prev(it.end(), i)
#define inf static_cast<int32_t>(2e31)

int main(int argc, char** argv) {

  int32_t n, m, k;
  scanf("%d%d%d", &n, &m, &k);

  vec vec_a;
  vec vec_b;
  vec vec_ab;
  vec vec_remains;
  vec vec_all;

  vec_all.reserve(n);

  forby (i, 0, n) {
    int32_t t, a, b;
    scanf("%d%d%d", &t, &a, &b);

    vec_all.push_back(t);

  if (a == 1 && b == 1) {
      vec_ab.push_back(i);
    } else if (a == 1) {
      vec_a.push_back(i);
    } else if (b == 1) {
      vec_b.push_back(i);
    } else {
      vec_remains.push_back(i);
    }
  }

 // check if impossible
  if (
      sz(vec_a) + sz(vec_ab) < k ||
      sz(vec_b) + sz(vec_ab) < k
     ) {
    printf("%d\n", -1);
    return 0;
  }

  std::function<bool(int32_t, int32_t)> cmp = [&vec_all](int32_t p, int32_t q) {
    return vec_all[ p ] < vec_all[ q ];
  };

  int32_t incommon = std::min(sz(vec_ab), k);
  int32_t selected_singular = k - incommon;
  int32_t to_select = m - (selected_singular * 2 + incommon);
  if (to_select < 0) {
    printf("%d\n", -1);
    return 0;
  }

  std::sort(all(vec_a), cmp);
  std::sort(all(vec_b), cmp);
  std::sort(all(vec_ab), cmp);
  std::sort(all(vec_remains), cmp);

  int32_t k_time = 0;
  forby (i, 0, incommon) {
    k_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, 0, selected_singular) {
    k_time += vec_all[ vec_a[ i ] ];
    k_time += vec_all[ vec_b[ i ] ];
  }

  std::multiset<int32_t, decltype(cmp)> m_in(cmp);
  std::multiset<int32_t, decltype(cmp)> m_out(cmp);
  int32_t ms_time = 0;
  forby (i, incommon, sz(vec_ab)) {
    m_in.insert(vec_ab[ i ]);
    ms_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, selected_singular, sz(vec_a)) {
    m_in.insert(vec_a[ i ]);
    ms_time += vec_all[ vec_a[ i ] ];
  }
  forby (i, selected_singular, sz(vec_b)) {
    m_in.insert(vec_b[ i ]);
    ms_time += vec_all[ vec_b[ i ] ];
  }
  forby (i, 0, sz(vec_remains)) {
    ms_time += vec_all[ vec_remains[ i ] ];
    m_in.insert(vec_remains[ i ]);
  }
  while (sz(m_in) > to_select) {
    ms_time -= vec_all[ *prev(m_in, 1) ];
    m_out.insert(*prev(m_in, 1));
    m_in.erase(prev(m_in, 1));
  }

  int32_t total_time = inf;
  while (true) {

    int32_t current_time = k_time + ms_time;
    if (current_time > total_time) {
      break;
    }
    total_time = current_time;

    if (
        incommon == 0 ||
        selected_singular == std::min(sz(vec_a), sz(vec_b)) ||
        to_select == 0
    ) {
      break;
    }

    incommon--;
    k_time -= vec_all[ vec_ab[ incommon ] ];
    k_time += vec_all[ vec_a[ selected_singular ] ];
    k_time += vec_all[ vec_b[ selected_singular ] ];
    to_select--;
    decltype(m_in)::iterator it_a = m_in.find(vec_a[ selected_singular ]);
    if (it_a == m_in.end()) {
      m_out.erase(m_out.find(*it_a));
    } else {
      ms_time -= vec_all[ vec_a[ selected_singular ] ];
      m_in.erase(it_a);
    }
    decltype(m_in)::iterator it_b = m_in.find(vec_b[ selected_singular ]);
    if (it_b == m_in.end()) {
      m_out.erase(m_out.find(*it_b));
    } else {
      ms_time -= vec_all[ vec_b[ selected_singular ] ];
      m_in.erase(it_b);
    }
    decltype(m_in)::iterator it_ab = m_in.find(vec_ab[ incommon ]);
    if (it_ab != m_in.end()) {
      ms_time += vec_all[ vec_ab[ incommon ] ];
      m_in.insert(vec_ab[ incommon ]);
    }

    while (sz(m_in) < to_select) {
      ms_time += vec_all[ *forward(m_out, 0) ];
      m_in.insert(*forward(m_out, 0));
      m_out.erase(forward(m_out, 0));
    }
    while (sz(m_in) > to_select) {
      ms_time -= vec_all[ *prev(m_in, 1) ];
      m_out.insert(*prev(m_in, 1));
      m_in.erase(prev(m_in, 1));
    }

    selected_singular++;
  }

  printf("%d\n", total_time);
  forby(i, 0, incommon) {
    printf("%d ", vec_ab[ i ] + 1);
  }
  forby(i, 0, selected_singular) {
    printf("%d %d ", vec_a[ i ] + 1, vec_b[ i ] + 1);
  }
  for (int32_t i : m_in) {
    printf("%d ", i + 1);
  }
  printf("\n");

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define forby(i, f, n) for (int32_t (i) = (f); i < n; i++)
#define sz(x) static_cast<int32_t>(x.size())
#define vec std::vector<int32_t>
#define forward(it, i) std::next(it.begin(), i)
#define prev(it, i) std::prev(it.end(), i)
#define inf static_cast<int32_t>(2e31)

int main(int argc, char** argv) {

  int32_t n, m, k;
  scanf("%d%d%d", &n, &m, &k);

  vec vec_a;
  vec vec_b;
  vec vec_ab;
  vec vec_remains;
  vec vec_all;

  vec_all.reserve(n);

  forby (i, 0, n) {
    int32_t t, a, b;
    scanf("%d%d%d", &t, &a, &b);

    vec_all.push_back(t);

  if (a == 1 && b == 1) {
      vec_ab.push_back(i);
    } else if (a == 1) {
      vec_a.push_back(i);
    } else if (b == 1) {
      vec_b.push_back(i);
    } else {
      vec_remains.push_back(i);
    }
  }

 // check if impossible
  if (
      sz(vec_a) + sz(vec_ab) < k ||
      sz(vec_b) + sz(vec_ab) < k
     ) {
    printf("%d\n", -1);
    return 0;
  }

  std::function<bool(int32_t, int32_t)> cmp = [&vec_all](int32_t p, int32_t q) {
    return vec_all[ p ] < vec_all[ q ];
  };

  int32_t incommon = std::min(sz(vec_ab), k);
  int32_t selected_singular = k - incommon;
  int32_t to_select = m - (selected_singular * 2 + incommon);
  if (to_select < 0) {
    printf("%d\n", -1);
    return 0;
  }

  std::sort(all(vec_a), cmp);
  std::sort(all(vec_b), cmp);
  std::sort(all(vec_ab), cmp);
  std::sort(all(vec_remains), cmp);

  int32_t k_time = 0;
  forby (i, 0, incommon) {
    k_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, 0, selected_singular) {
    k_time += vec_all[ vec_a[ i ] ];
    k_time += vec_all[ vec_b[ i ] ];
  }

  std::multiset<int32_t, decltype(cmp)> m_in(cmp);
  std::multiset<int32_t, decltype(cmp)> m_out(cmp);
  int32_t ms_time = 0;
  forby (i, incommon, sz(vec_ab)) {
    m_in.insert(vec_ab[ i ]);
    ms_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, selected_singular, sz(vec_a)) {
    m_in.insert(vec_a[ i ]);
    ms_time += vec_all[ vec_a[ i ] ];
  }
  forby (i, selected_singular, sz(vec_b)) {
    m_in.insert(vec_b[ i ]);
    ms_time += vec_all[ vec_b[ i ] ];
  }
  forby (i, 0, sz(vec_remains)) {
    ms_time += vec_all[ vec_remains[ i ] ];
    m_in.insert(vec_remains[ i ]);
  }
  while (sz(m_in) > to_select) {
    ms_time -= vec_all[ *prev(m_in, 1) ];
    m_out.insert(*prev(m_in, 1));
    m_in.erase(prev(m_in, 1));
  }

  int32_t total_time = inf;
  while (true) {

    int32_t current_time = k_time + ms_time;
    if (current_time > total_time) {
      break;
    }
    total_time = current_time;

    if (
        incommon == 0 ||
        selected_singular == std::min(sz(vec_a), sz(vec_b)) ||
        to_select == 0
    ) {
      break;
    }

    incommon--;
    k_time -= vec_all[ vec_ab[ incommon ] ];
    k_time += vec_all[ vec_a[ selected_singular ] ];
    k_time += vec_all[ vec_b[ selected_singular ] ];
    to_select--;
    decltype(m_in)::iterator it_a = m_in.find(vec_a[ selected_singular ]);
    if (it_a == m_in.end()) {
      m_out.erase(m_out.find(*it_a));
    } else {
      ms_time -= vec_all[ vec_a[ selected_singular ] ];
      m_in.erase(it_a);
    }
    decltype(m_in)::iterator it_b = m_in.find(vec_b[ selected_singular ]);
    if (it_b == m_in.end()) {
      m_out.erase(m_out.find(*it_b));
    } else {
      ms_time -= vec_all[ vec_b[ selected_singular ] ];
      m_in.erase(it_b);
    }

    m_out.insert(vec_ab[ incommon ]);

    while (sz(m_in) < to_select) {
      ms_time += vec_all[ *forward(m_out, 0) ];
      m_in.insert(*forward(m_out, 0));
      m_out.erase(forward(m_out, 0));
    }
    while (sz(m_in) > to_select) {
      ms_time -= vec_all[ *prev(m_in, 1) ];
      m_out.insert(*prev(m_in, 1));
      m_in.erase(prev(m_in, 1));
    }
    while (sz(m_in) > 0 && sz(m_out) > 0 && vec_all[ *prev(m_in, 1) ] > vec_all[ *forward(m_out, 0) ]) {
      int32_t tmp_in = *prev(m_in, 1);
      int32_t tmp_out = *forward(m_out, 0);
      m_in.erase(prev(m_in, 1));
      m_out.erase(forward(m_out, 0));
      m_in.insert(tmp_out);
      m_out.insert(tmp_in);
      ms_time -= vec_all[ tmp_in ];
      ms_time += vec_all[ tmp_out ];
    }

    selected_singular++;
  }

  printf("%d\n", total_time);
  forby(i, 0, incommon) {
    printf("%d ", vec_ab[ i ] + 1);
  }
  forby(i, 0, selected_singular) {
    printf("%d %d ", vec_a[ i ] + 1, vec_b[ i ] + 1);
  }
  for (int32_t i : m_in) {
    printf("%d ", i + 1);
  }
  printf("\n");

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define forby(i, f, n) for (int32_t (i) = (f); i < n; i++)
#define sz(x) static_cast<int32_t>(x.size())
#define vec std::vector<int32_t>
#define forward(it, i) std::next(it.begin(), i)
#define inf static_cast<int32_t>(2e31)

int main(int argc, char** argv) {

  int32_t n, m, k;
  scanf("%d%d%d", &n, &m, &k);

  vec vec_a;
  vec vec_b;
  vec vec_ab;
  vec vec_all;
  vec vec_remains;

  forby (i, 0, n) {
    int32_t t, a, b;
    scanf("%d%d%d", &t, &a, &b);

    vec_all.push_back(t);

    if (a == 1 && b == 1) {
      vec_ab.push_back(i);
    } else if (a == 1) {
      vec_a.push_back(i);
    } else if (b == 1) {
      vec_b.push_back(i);
    } else {
      vec_remains.push_back(i);
    }
  }

  // check if impossible
  if (
      sz(vec_a) + sz(vec_ab) < k ||
      sz(vec_b) + sz(vec_ab) < k
     ) {
    printf("%d\n", -1);
    return 0;
  }

  std::function<bool(int32_t, int32_t)> cmp = [&vec_all](int32_t p, int32_t q) {
    return vec_all[ p ] < vec_all[ q ];
  };

  std::sort(all(vec_a), cmp);
  std::sort(all(vec_b), cmp);
  std::sort(all(vec_ab), cmp);

  int32_t possible_k = std::min(std::min(sz(vec_a), sz(vec_b)), k);
  int32_t incommon = k - possible_k;

  std::multiset<int32_t, decltype(cmp)> ms(cmp);

  forby(i, possible_k, sz(vec_a)) {
    ms.insert(vec_a[ i ]);
  }
  forby(i, possible_k, sz(vec_b)) {
    ms.insert(vec_b[ i ]);
  }
  forby(i, incommon, sz(vec_ab)) {
    ms.insert(vec_ab[ i ]);
  }
  forby(i, 0, sz(vec_remains)) {
    ms.insert(vec_remains[ i ]);
  }

  // initial state
  int32_t best_incommon = -1;
  int32_t total_time = inf;
  int32_t calculated_time = 0;
  forby (i, 0, possible_k) {
    calculated_time += vec_all[ vec_a[ i ] ];
    calculated_time += vec_all[ vec_b[ i ] ];
  }
  forby (i, 0, incommon) {
    calculated_time += vec_all[ vec_ab[ i ] ];
  }
  int32_t selected = possible_k * 2 + incommon;
  while (incommon <= m && incommon <= sz(vec_ab)) {

    if (selected <= m) {
      int32_t current_time = calculated_time;
      forby (i, 0, m - selected) {
        current_time += vec_all[ *forward(ms, i) ];
      }

      if (current_time < total_time) {
        total_time = current_time;
        best_incommon = incommon;
      }
    }

    if (incommon < sz(vec_ab) && incommon < m) {
      calculated_time += vec_all[ vec_ab[ incommon ] ];
      ms.erase(vec_ab[ incommon ]);
      possible_k--;
      calculated_time -= vec_all[ vec_a[ possible_k ] ];
      calculated_time -= vec_all[ vec_b[ possible_k ] ];
      ms.insert(vec_a[ possible_k ]);
      ms.insert(vec_b[ possible_k ]);
      selected--;
    }
    incommon++;
  }

  // calculate result with best_incommon
  if (best_incommon == -1) {
    printf("%d\n", -1);
    return 0;
  }

  ms.clear();
  std::vector<int32_t> result;
  result.reserve(m);
  result.insert(result.end(), vec_a.begin(), vec_a.begin() + k - best_incommon);
  result.insert(result.end(), vec_b.begin(), vec_b.begin() + k - best_incommon);
  result.insert(result.end(), vec_ab.begin(), vec_ab.begin() + best_incommon);

  int32_t missing = m - ((k - best_incommon) * 2 + best_incommon);
  if (missing > 0) {
    forby(i, k - best_incommon, std::min(k - best_incommon + missing, sz(vec_a))) {
      ms.insert(vec_a[ i ]);
    }
    forby(i, k - best_incommon, std::min(k - best_incommon + missing, sz(vec_b))) {
      ms.insert(vec_b[ i ]);
    }
    forby(i, best_incommon, std::min(best_incommon + missing, sz(vec_ab))) {
      ms.insert(vec_ab[ i ]);
    }
    forby(i, 0, std::min(missing, sz(vec_remains))) {
      ms.insert(vec_remains[ i ]);
    }

    forby(i, 0, missing) {
      result.push_back(*forward(ms, i));
    }
  }

  printf("%d\n", total_time);
  for (int32_t i : result) {
    printf("%d ", i + 1);
  }
  printf("\n");

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define forby(i, f, n) for (int32_t (i) = (f); i < n; i++)
#define sz(x) static_cast<int32_t>(x.size())
#define vec std::vector<int32_t>
#define forward(it, i) std::next(it.begin(), i)
#define inf static_cast<int32_t>(2e31)

int main(int argc, char** argv) {

  int32_t n, m, k;
  scanf("%d%d%d", &n, &m, &k);

  vec vec_a;
  vec vec_b;
  vec vec_ab;
  vec vec_all;
  vec vec_remains;

  forby (i, 0, n) {
    int32_t t, a, b;
    scanf("%d%d%d", &t, &a, &b);

    vec_all.push_back(t);

    if (a == 1 && b == 1) {
      vec_ab.push_back(i);
    } else if (a == 1) {
      vec_a.push_back(i);
    } else if (b == 1) {
      vec_b.push_back(i);
    } else {
      vec_remains.push_back(i);
    }
  }

  // check if impossible
  if (
      sz(vec_a) + sz(vec_ab) < k ||
      sz(vec_b) + sz(vec_ab) < k
     ) {
    printf("%d\n", -1);
    return 0;
  }

  std::function<bool(int32_t, int32_t)> cmp = [&vec_all](int32_t p, int32_t q) {
    return vec_all[ p ] < vec_all[ q ];
  };

  std::sort(all(vec_a), cmp);
  std::sort(all(vec_b), cmp);
  std::sort(all(vec_ab), cmp);
  std::sort(all(vec_remains), cmp);

  int32_t possible_k = std::min(std::min(sz(vec_a), sz(vec_b)), k);
  int32_t incommon = k - possible_k;

  std::multiset<int32_t, decltype(cmp)> ms(cmp);

  forby(i, possible_k, sz(vec_a)) {
    ms.insert(vec_a[ i ]);
  }
  forby(i, possible_k, sz(vec_b)) {
    ms.insert(vec_b[ i ]);
  }
  forby(i, incommon, sz(vec_ab)) {
    ms.insert(vec_ab[ i ]);
  }
  forby(i, 0, sz(vec_remains)) {
    ms.insert(vec_remains[ i ]);
  }

  // initial state
  int32_t best_incommon = -1;
  int32_t total_time = inf;
  int32_t calculated_time = 0;
  forby (i, 0, possible_k) {
    calculated_time += vec_all[ vec_a[ i ] ];
    calculated_time += vec_all[ vec_b[ i ] ];
  }
  forby (i, 0, incommon) {
    calculated_time += vec_all[ vec_ab[ i ] ];
  }
  int32_t selected = possible_k * 2 + incommon;
  while (incommon <= std::min(m, sz(vec_ab))) {

    if (selected <= m) {
      int32_t current_time = calculated_time;
      forby (i, 0, m - selected) {
        current_time += vec_all[ *forward(ms, i) ];
      }

      if (current_time < total_time) {
        total_time = current_time;
        best_incommon = incommon;
      }
    }

    if (incommon < std::min(m, sz(vec_ab))) {
      calculated_time += vec_all[ vec_ab[ incommon ] ];
      ms.erase(vec_ab[ incommon ]);
      possible_k--;
      calculated_time -= vec_all[ vec_a[ possible_k ] ];
      calculated_time -= vec_all[ vec_b[ possible_k ] ];
      ms.insert(vec_a[ possible_k ]);
      ms.insert(vec_b[ possible_k ]);
      selected--;
    }
    incommon++;
  }

  // calculate result with best_incommon
  if (best_incommon == -1) {
    printf("%d\n", -1);
    return 0;
  }

  ms.clear();
  std::vector<int32_t> result;
  result.reserve(m);
  result.insert(result.end(), vec_a.begin(), vec_a.begin() + k - best_incommon);
  result.insert(result.end(), vec_b.begin(), vec_b.begin() + k - best_incommon);
  result.insert(result.end(), vec_ab.begin(), vec_ab.begin() + best_incommon);

  int32_t missing = m - ((k - best_incommon) * 2 + best_incommon);
  if (missing > 0) {
    forby(i, k - best_incommon, std::min(k - best_incommon + missing, sz(vec_a))) {
      ms.insert(vec_a[ i ]);
    }
    forby(i, k - best_incommon, std::min(k - best_incommon + missing, sz(vec_b))) {
      ms.insert(vec_b[ i ]);
    }
    forby(i, best_incommon, std::min(best_incommon + missing, sz(vec_ab))) {
      ms.insert(vec_ab[ i ]);
    }
    forby(i, 0, std::min(missing, sz(vec_remains))) {
      ms.insert(vec_remains[ i ]);
    }

    forby(i, 0, missing) {
      result.push_back(*forward(ms, i));
    }
  }

  printf("%d\n", total_time);
  for (int32_t i : result) {
    printf("%d ", i + 1);
  }
  printf("\n");

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define forby(i, f, n) for (int32_t (i) = (f); i < n; i++)
#define sz(x) static_cast<int32_t>(x.size())
#define vec std::vector<int32_t>
#define forward(it, i) std::next(it.begin(), i)
#define prev(it, i) std::prev(it.end(), i)
#define inf 2000000000

int main(int argc, char** argv) {

  int32_t n, m, k;
  scanf("%d%d%d", &n, &m, &k);

  vec vec_a;
  vec vec_b;
  vec vec_ab;
  vec vec_remains;
  vec vec_all;

  vec_all.reserve(n);

  forby (i, 0, n) {
    int32_t t, a, b;
    scanf("%d%d%d", &t, &a, &b);

    vec_all.push_back(t);

  if (a == 1 && b == 1) {
      vec_ab.push_back(i);
    } else if (a == 1) {
      vec_a.push_back(i);
    } else if (b == 1) {
      vec_b.push_back(i);
    } else {
      vec_remains.push_back(i);
    }
  }

 // check if impossible
  if (
      sz(vec_a) + sz(vec_ab) < k ||
      sz(vec_b) + sz(vec_ab) < k
     ) {
    printf("%d\n", -1);
    return 0;
  }

  std::function<bool(int32_t, int32_t)> cmp = [&vec_all](int32_t p, int32_t q) {
    return vec_all[ p ] < vec_all[ q ];
  };

  int32_t incommon = std::min(sz(vec_ab), k);
  int32_t selected_singular = k - incommon;
  int32_t to_select = m - (selected_singular * 2 + incommon);
  if (to_select < 0) {
    printf("%d\n", -1);
    return 0;
  }

  std::sort(all(vec_a), cmp);
  std::sort(all(vec_b), cmp);
  std::sort(all(vec_ab), cmp);
  std::sort(all(vec_remains), cmp);

  int32_t k_time = 0;
  forby (i, 0, incommon) {
    k_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, 0, selected_singular) {
    k_time += vec_all[ vec_a[ i ] ];
    k_time += vec_all[ vec_b[ i ] ];
  }

  std::multiset<int32_t, decltype(cmp)> m_in(cmp);
  std::multiset<int32_t, decltype(cmp)> m_out(cmp);
  int32_t ms_time = 0;
  forby (i, incommon, sz(vec_ab)) {
    m_in.insert(vec_ab[ i ]);
    ms_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, selected_singular, sz(vec_a)) {
    m_in.insert(vec_a[ i ]);
    ms_time += vec_all[ vec_a[ i ] ];
  }
  forby (i, selected_singular, sz(vec_b)) {
    m_in.insert(vec_b[ i ]);
    ms_time += vec_all[ vec_b[ i ] ];
  }
  forby (i, 0, sz(vec_remains)) {
    ms_time += vec_all[ vec_remains[ i ] ];
    m_in.insert(vec_remains[ i ]);
  }
  while (sz(m_in) > to_select) {
    ms_time -= vec_all[ *prev(m_in, 1) ];
    m_out.insert(*prev(m_in, 1));
    m_in.erase(prev(m_in, 1));
  }

  int32_t total_time = inf;
  auto balance = [&vec_all, &m_in, &m_out, &ms_time](int32_t to_select) {
    while (sz(m_in) < to_select) {
      ms_time += vec_all[ *forward(m_out, 0) ];
      m_in.insert(*forward(m_out, 0));
      m_out.erase(forward(m_out, 0));
    }
    while (sz(m_in) > to_select) {
      ms_time -= vec_all[ *prev(m_in, 1) ];
      m_out.insert(*prev(m_in, 1));
      m_in.erase(prev(m_in, 1));
    }
    while (sz(m_in) > 0 && sz(m_out) > 0 && vec_all[ *prev(m_in, 1) ] > vec_all[ *forward(m_out, 0) ]) {
      int32_t tmp_in = *prev(m_in, 1);
      int32_t tmp_out = *forward(m_out, 0);
      m_in.erase(prev(m_in, 1));
      m_out.erase(forward(m_out, 0));
      m_in.insert(tmp_out);
      m_out.insert(tmp_in);
      ms_time -= vec_all[ tmp_in ];
      ms_time += vec_all[ tmp_out ];
    }
  };
  while (true) {

    int32_t current_time = k_time + ms_time;
    // if we pass the correct answer
    if (current_time > total_time) {
      selected_singular--;
      decltype(m_in)::iterator it_ab = m_in.find(vec_ab[ incommon ]);
      if (it_ab == m_in.end()) {
        m_out.erase(m_out.find(*it_ab));
      } else {
        ms_time -= vec_all[ *it_ab ];
        m_in.erase(it_ab);
      }
      m_in.insert(vec_a[ selected_singular ]);
      m_in.insert(vec_b[ selected_singular ]);
      ms_time += vec_all[ vec_a[ selected_singular ] ];
      ms_time += vec_all[ vec_b[ selected_singular ] ];
      balance(to_select + 1);
      break;
    }
    total_time = current_time;

    if (
        incommon == 0 ||
        selected_singular == std::min(sz(vec_a), sz(vec_b)) ||
        to_select == 0
    ) {
      break;
    }

    incommon--;
    k_time -= vec_all[ vec_ab[ incommon ] ];
    k_time += vec_all[ vec_a[ selected_singular ] ];
    k_time += vec_all[ vec_b[ selected_singular ] ];
    to_select--;
    decltype(m_in)::iterator it_a = m_in.find(vec_a[ selected_singular ]);
    if (it_a == m_in.end()) {
      m_out.erase(m_out.find(*it_a));
    } else {
      ms_time -= vec_all[ vec_a[ selected_singular ] ];
      m_in.erase(it_a);
    }
    decltype(m_in)::iterator it_b = m_in.find(vec_b[ selected_singular ]);
    if (it_b == m_in.end()) {
      m_out.erase(m_out.find(*it_b));
    } else {
      ms_time -= vec_all[ vec_b[ selected_singular ] ];
      m_in.erase(it_b);
    }

    m_out.insert(vec_ab[ incommon ]);
    balance(to_select);
    selected_singular++;
  }

  printf("%d\n", total_time);
  forby(i, 0, incommon) {
    printf("%d ", vec_ab[ i ] + 1);
  }
  forby(i, 0, selected_singular) {
    printf("%d %d ", vec_a[ i ] + 1, vec_b[ i ] + 1);
  }
  for (int32_t i : m_in) {
    printf("%d ", i + 1);
  }
  printf("\n");

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define forby(i, f, n) for (int32_t (i) = (f); i < n; i++)
#define sz(x) static_cast<int32_t>(x.size())
#define vec std::vector<int32_t>
#define forward(it, i) std::next(it.begin(), i)
#define prev(it, i) std::prev(it.end(), i)
#define inf static_cast<int32_t>(2e31)

int main(int argc, char** argv) {

  int32_t n, m, k;
  scanf("%d%d%d", &n, &m, &k);

  vec vec_a;
  vec vec_b;
  vec vec_ab;
  vec vec_remains;
  vec vec_all;

  vec_all.reserve(n);

  forby (i, 0, n) {
    int32_t t, a, b;
    scanf("%d%d%d", &t, &a, &b);

    vec_all.push_back(t);

  if (a == 1 && b == 1) {
      vec_ab.push_back(i);
    } else if (a == 1) {
      vec_a.push_back(i);
    } else if (b == 1) {
      vec_b.push_back(i);
    } else {
      vec_remains.push_back(i);
    }
  }

 // check if impossible
  if (
      sz(vec_a) + sz(vec_ab) < k ||
      sz(vec_b) + sz(vec_ab) < k
     ) {
    printf("%d\n", -1);
    return 0;
  }

  std::function<bool(int32_t, int32_t)> cmp = [&vec_all](int32_t p, int32_t q) {
    return vec_all[ p ] < vec_all[ q ];
  };

  int32_t incommon = std::min(sz(vec_ab), k);
  int32_t selected_singular = k - incommon;
  int32_t to_select = m - (selected_singular * 2 + incommon);
  if (to_select < 0) {
    printf("%d\n", -1);
    return 0;
  }

  std::sort(all(vec_a), cmp);
  std::sort(all(vec_b), cmp);
  std::sort(all(vec_ab), cmp);
  std::sort(all(vec_remains), cmp);

  int32_t k_time = 0;
  forby (i, 0, incommon) {
    k_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, 0, selected_singular) {
    k_time += vec_all[ vec_a[ i ] ];
    k_time += vec_all[ vec_b[ i ] ];
  }

  std::multiset<int32_t, decltype(cmp)> m_in(cmp);
  std::multiset<int32_t, decltype(cmp)> m_out(cmp);
  int32_t ms_time = 0;
  forby (i, incommon, std::min(sz(vec_ab), incommon + to_select)) {
    m_in.insert(vec_ab[ i ]);
    ms_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, selected_singular, std::min(sz(vec_a), selected_singular + to_select)) {
    m_in.insert(vec_a[ i ]);
    ms_time += vec_all[ vec_a[ i ] ];
  }
  forby (i, selected_singular, std::min(sz(vec_b), selected_singular + to_select)) {
    m_in.insert(vec_b[ i ]);
    ms_time += vec_all[ vec_b[ i ] ];
  }
  int32_t selected_remains = std::min(sz(vec_remains), to_select);
  forby (i, 0, sz(vec_remains)) {
    if (i < to_select) {
      ms_time += vec_all[ vec_remains[ i ] ];
      m_in.insert(vec_remains[ i ]);
    } else {
      m_out.insert(vec_remains[ i ]);
    }
  }
  while (sz(m_in) > to_select) {
    ms_time -= vec_all[ *prev(m_in, 1) ];
    m_out.insert(*prev(m_in, 1));
    m_in.erase(prev(m_in, 1));
  }

  int32_t total_time = inf;
  while (true) {

    int32_t current_time = k_time + ms_time;
    if (current_time > total_time) {
      break;
    }
    total_time = current_time;

    if (
        incommon == 0 ||
        selected_singular == std::min(sz(vec_a), sz(vec_b)) ||
        to_select == 0
    ) {
      break;
    }

    incommon--;
    k_time -= vec_all[ vec_ab[ incommon ] ];
    k_time += vec_all[ vec_a[ selected_singular ] ];
    k_time += vec_all[ vec_b[ selected_singular ] ];
    to_select--;
    decltype(m_in)::iterator it_a = m_in.find(vec_a[ selected_singular ]);
    if (it_a != m_in.end()) {
      ms_time -= vec_all[ vec_a[ selected_singular ] ];
      m_in.erase(it_a);
    }
    decltype(m_in)::iterator it_b = m_in.find(vec_b[ selected_singular ]);
    if (it_b != m_in.end()) {
      ms_time -= vec_all[ vec_b[ selected_singular ] ];
      m_in.erase(it_b);
    }

    while (sz(m_in) < to_select) {
      ms_time += vec_all[ *forward(m_out, 0) ];
      m_in.insert(*forward(m_out, 0));
      m_out.erase(forward(m_out, 0));
    }
    while (sz(m_in) > to_select) {
      ms_time -= vec_all[ *prev(m_in, 1) ];
      m_in.erase(prev(m_in, 1));
    }

    selected_singular++;
  }

  printf("%d\n", total_time);
  forby(i, 0, incommon) {
    printf("%d ", vec_ab[ i ] + 1);
  }
  forby(i, 0, selected_singular) {
    printf("%d %d ", vec_a[ i ] + 1, vec_b[ i ] + 1);
  }
  for (int32_t i : m_in) {
    printf("%d ", i + 1);
  }
  printf("\n");

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define forby(i, f, n) for (int32_t (i) = (f); i < n; i++)
#define sz(x) static_cast<int32_t>(x.size())
#define vec std::vector<int32_t>
#define forward(it, i) std::next(it.begin(), i)
#define prev(it, i) std::prev(it.end(), i)
#define inf static_cast<int32_t>(2e31)

int main(int argc, char** argv) {

  int32_t n, m, k;
  scanf("%d%d%d", &n, &m, &k);

  vec vec_a;
  vec vec_b;
  vec vec_ab;
  vec vec_remains;
  vec vec_all;

  vec_all.reserve(n);

  forby (i, 0, n) {
    int32_t t, a, b;
    scanf("%d%d%d", &t, &a, &b);

    vec_all.push_back(t);

  if (a == 1 && b == 1) {
      vec_ab.push_back(i);
    } else if (a == 1) {
      vec_a.push_back(i);
    } else if (b == 1) {
      vec_b.push_back(i);
    } else {
      vec_remains.push_back(i);
    }
  }

 // check if impossible
  if (
      sz(vec_a) + sz(vec_ab) < k ||
      sz(vec_b) + sz(vec_ab) < k
     ) {
    printf("%d\n", -1);
    return 0;
  }

  std::function<bool(int32_t, int32_t)> cmp = [&vec_all](int32_t p, int32_t q) {
    return vec_all[ p ] < vec_all[ q ];
  };

  int32_t incommon = std::min(sz(vec_ab), k);
  int32_t selected_singular = k - incommon;
  int32_t to_select = m - (selected_singular * 2 + incommon);
  if (to_select < 0) {
    printf("%d\n", -1);
    return 0;
  }

  std::sort(all(vec_a), cmp);
  std::sort(all(vec_b), cmp);
  std::sort(all(vec_ab), cmp);
  std::sort(all(vec_remains), cmp);

  int32_t k_time = 0;
  forby (i, 0, incommon) {
    k_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, 0, selected_singular) {
    k_time += vec_all[ vec_a[ i ] ];
    k_time += vec_all[ vec_b[ i ] ];
  }

  std::multiset<int32_t, decltype(cmp)> m_in(cmp);
  std::multiset<int32_t, decltype(cmp)> m_out(cmp);
  int32_t ms_time = 0;
  forby (i, incommon, sz(vec_ab)) {
    m_in.insert(vec_ab[ i ]);
    ms_time += vec_all[ vec_ab[ i ] ];
  }
  forby (i, selected_singular, sz(vec_a)) {
    m_in.insert(vec_a[ i ]);
    ms_time += vec_all[ vec_a[ i ] ];
  }
  forby (i, selected_singular, sz(vec_b)) {
    m_in.insert(vec_b[ i ]);
    ms_time += vec_all[ vec_b[ i ] ];
  }
  forby (i, 0, sz(vec_remains)) {
    ms_time += vec_all[ vec_remains[ i ] ];
    m_in.insert(vec_remains[ i ]);
  }
  while (sz(m_in) > to_select) {
    ms_time -= vec_all[ *prev(m_in, 1) ];
    m_out.insert(*prev(m_in, 1));
    m_in.erase(prev(m_in, 1));
  }

  int32_t total_time = inf;
  while (true) {

    int32_t current_time = k_time + ms_time;
    if (current_time > total_time) {
      break;
    }
    total_time = current_time;

    if (
        incommon == 0 ||
        selected_singular == std::min(sz(vec_a), sz(vec_b)) ||
        to_select == 0
    ) {
      break;
    }

    incommon--;
    k_time -= vec_all[ vec_ab[ incommon ] ];
    k_time += vec_all[ vec_a[ selected_singular ] ];
    k_time += vec_all[ vec_b[ selected_singular ] ];
    to_select--;
    decltype(m_in)::iterator it_a = m_in.find(vec_a[ selected_singular ]);
    if (it_a == m_in.end()) {
      m_out.erase(m_out.find(*it_a));
    } else {
      ms_time -= vec_all[ vec_a[ selected_singular ] ];
      m_in.erase(it_a);
    }
    decltype(m_in)::iterator it_b = m_in.find(vec_b[ selected_singular ]);
    if (it_b == m_in.end()) {
      m_out.erase(m_out.find(*it_b));
    } else {
      ms_time -= vec_all[ vec_b[ selected_singular ] ];
      m_in.erase(it_b);
    }
    decltype(m_in)::iterator it_ab = m_in.find(vec_ab[ incommon ]);
    if (it_ab != m_in.end()) {
      ms_time += vec_all[ vec_ab[ incommon ] ];
      m_in.insert(vec_ab[ incommon ]);
    }

    while (sz(m_in) < to_select) {
      ms_time += vec_all[ *forward(m_out, 0) ];
      m_in.insert(*forward(m_out, 0));
      m_out.erase(forward(m_out, 0));
    }
    while (sz(m_in) > to_select) {
      ms_time -= vec_all[ *prev(m_in, 1) ];
      m_out.insert(*prev(m_in, 1));
      m_in.erase(prev(m_in, 1));
    }
    while (vec_all[ *prev(m_in, 1) ] > vec_all[ *forward(m_out, 0) ]) {
      int32_t tmp_in = *prev(m_in, 1);
      int32_t tmp_out = *forward(m_out, 0);
      m_in.erase(prev(m_in, 1));
      m_out.erase(forward(m_out, 0));
      m_in.insert(tmp_out);
      m_out.insert(tmp_in);
    }

    selected_singular++;
  }

  printf("%d\n", total_time);
  forby(i, 0, incommon) {
    printf("%d ", vec_ab[ i ] + 1);
  }
  forby(i, 0, selected_singular) {
    printf("%d %d ", vec_a[ i ] + 1, vec_b[ i ] + 1);
  }
  for (int32_t i : m_in) {
    printf("%d ", i + 1);
  }
  printf("\n");

  return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<cmath>
#include<ctime>
#include<vector>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<utility>
#include<algorithm>
using namespace std;

#define FOR(i,a,b) for(register int i=(a);i<(b);++i)
#define FORR(i,a,b) for(register int i=(a);i<=(b);++i)
#define ROR(i,a,b) for(register int i=(a);i>=(b);--i)
#define RORR(i,a,b) for(register int i=(a);i>(b);--i)
#define PQ priority_queue
#define VR vector
#define MST(a,b) memset(a,b,sizeof(a))
#define FGETS(s) fgets(s,sizeof(s),stdin)
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define FI first
#define SE second
typedef long long LL;
typedef long long unsigned LLU;
typedef pair<int,int> pii;

int n,m,k;
const LL INF = 0x3f3f3f3f3f3f3f3f;
VR<int> a,b,c,d,e,ans;
bool chosen[int(2e5+20)];
struct Node{
  int t,a,b;
}node[int(2e5+20)];

const int MAX = 1e4 + 20;
struct ST{
  int cnt[MAX<<2],v[MAX<<2], n;
  void add(int x) {
#ifdef DEBUG
    printf("  add %d\n",x);
#endif
    return add(1,1,n,x);
  }
  void add(int u, int l, int r, int x){
    ++cnt[u]; v[u] += x;
    if(l == r) return;
    int m = (l+r)/2;
    if(x <= m) add(u<<1,l,m,x);
    else add(u<<1|1,m+1,r,x);
  }

  int qMin(int k) {
#ifdef DEBUG
    printf("  qMin(%d): %d\n",k,qMin(1,1,n,k));
#endif
    return qMin(1,1,n,k);
  }
  int qMin(int u, int l, int r, int k){
    if(k >= cnt[u]) return v[u];
    int m = (l+r)/2;

    int sum = qMin(u<<1, l, m, min(k, cnt[u<<1]));
    if(cnt[u<<1] < k) sum += qMin(u<<1|1, m+1, r, k - cnt[u<<1]);
    return sum;
  }
}sg;

void cal1(LL rec){
  VR<pii> v1, v2;
  FOR(i,0,n){
    if(node[i].a && node[i].b) v1.push_back({node[i].t,i+1});
    else v2.push_back({node[i].t, i+1});
  }
  sort(ALL(v1)); sort(ALL(v2));
  FOR(i,0,rec) ans.push_back(v1[i].SE);
  FOR(i,0,m-rec) ans.push_back(v2[i].SE);
}

void cal2(LL rec){
#ifdef DEBUG
  printf("cal2 %lld\n",rec);
#endif
  VR<pii> c, a, b, left;
  FOR(i,0,n){
    Node &t = node[i];
    if(t.a && t.b) c.push_back({t.t, i+1});
    else if(t.a) a.push_back({t.t, i+1});
    else if(t.b) b.push_back({t.t, i+1});
    else left.push_back({t.t, i+1});
  }

  sort(ALL(a)); sort(ALL(b)); sort(ALL(c));
  FOR(i,0,rec) ans.push_back(c[i].SE);
  FOR(i,0,k-rec) ans.push_back(a[i].SE);
  FOR(i,0,k-rec) ans.push_back(b[i].SE);

  int nleft = m - (rec + k-rec + k-rec);
  FOR(i,k-rec,a.size()) left.push_back(a[i]);
  FOR(i,k-rec,b.size()) left.push_back(b[i]);
  FOR(i,rec,c.size()) left.push_back(c[i]);
#ifdef DEBUG
  printf("  nleft: %d\n",nleft);
  for(auto p : left) printf(" [%d, %d]\n",p.FI,p.SE);
#endif
  sort(ALL(left));
  FOR(i,0,nleft) ans.push_back(left[i].SE);
}

int main(void){
  scanf("%d%d%d",&n,&m,&k);
  FOR(i,0,n){
    int t,ta,tb;
    scanf("%d%d%d",&t,&ta,&tb);
    node[i] = {t,ta,tb};

    if(ta && tb) c.push_back(t);
    else{
      d.push_back(t);
      if(ta) a.push_back(t);
      else if(tb) b.push_back(t);
      else e.push_back(t);
    }
  }
  sort(ALL(a)); sort(ALL(b));
  sort(ALL(c)); sort(ALL(d)); sort(ALL(e));
#ifdef DEBUG
  printf("a:"); for(int x : a) printf(" %d",x); putchar('\n');
  printf("b:"); for(int x : b) printf(" %d",x); putchar('\n');
  printf("c:"); for(int x : c) printf(" %d",x); putchar('\n');
  printf("d:"); for(int x : d) printf(" %d",x); putchar('\n');
  printf("e:"); for(int x : e) printf(" %d",x); putchar('\n');
#endif

  VR<int> sa(a), sb(b);
  FOR(i,1,a.size()) sa[i] += sa[i-1];
  FOR(i,1,b.size()) sb[i] += sb[i-1];
  FOR(i,1,c.size()) c[i] += c[i-1];
  FOR(i,1,d.size()) d[i] += d[i-1];
  FOR(i,1,e.size()) e[i] += e[i-1];

  LL ansMin = INF, rec = -1; sg.n = 1e4;
  if(c.size() >= k) FORR(i,k,min((int)c.size(),m)){
    LL sum = c[i-1];
    if(m - i){
      if(d.size() < m-i) sum = INF;
      else sum += d[m-i-1];
    }
    if(sum < ansMin){
#ifdef DEBUG
      printf("c: %d, sum: %lld\n",i,sum);
#endif
      ansMin = sum; rec = i | (1ll<<60);
    }
  }

  bool first = true;
  for(int x : e) sg.add(x);
  FORR(i,0,min((int)c.size(), k-1)) if(a.size() >= k-i && b.size() >= k-i){
    if(first){
      FOR(j,k-i,a.size()) sg.add(a[j]);
      FOR(j,k-i,b.size()) sg.add(b[j]);
      first = false;
    }

    int left = m - (i + k-i + k-i);
    if(left < 0) continue;

    LL sum = i>0 ? c[i-1] : 0;
    sum += sa[k-i-1] + sb[k-i-1] + sg.qMin(left);
    if(sum < ansMin){
#ifdef DEBUG
      printf("c: %d, sum: %lld\n",i,sum);
#endif
      ansMin = sum; rec = i;
    }

    sg.add(a[k-i-1]), sg.add(b[k-i-1]);
  }

  if(ansMin == INF) { printf("-1\n"); return 0; }
  if(rec & (1ll<<60)) cal1(rec - (1ll<<60));
  else cal2(rec);

  printf("%lld\n",ansMin);
  for(int x : ans) printf("%d ",x);
  putchar('\n');

  return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<cmath>
#include<ctime>
#include<vector>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<utility>
#include<algorithm>
using namespace std;

#define FOR(i,a,b) for(register int i=(a);i<(b);++i)
#define FORR(i,a,b) for(register int i=(a);i<=(b);++i)
#define ROR(i,a,b) for(register int i=(a);i>=(b);--i)
#define RORR(i,a,b) for(register int i=(a);i>(b);--i)
#define PQ priority_queue
#define VR vector
#define MST(a,b) memset(a,b,sizeof(a))
#define FGETS(s) fgets(s,sizeof(s),stdin)
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define FI first
#define SE second
typedef long long LL;
typedef long long unsigned LLU;
typedef pair<int,int> pii;

int n,m,k;
const LL INF = 0x3f3f3f3f3f3f3f3f;
VR<int> a,b,c,d,e,ans;
bool chosen[int(2e5+20)];
struct Node{
  int t,a,b;
}node[int(2e5+20)];

const int MAX = 1e4 + 20;
struct ST{
  int cnt[MAX<<2],v[MAX<<2], n;
  void add(int x) {
#ifdef DEBUG
    printf("  add %d\n",x);
#endif
    return add(1,1,n,x);
  }
  void add(int u, int l, int r, int x){
    ++cnt[u]; v[u] += x;
    if(l == r) return;
    int m = (l+r)/2;
    if(x <= m) add(u<<1,l,m,x);
    else add(u<<1|1,m+1,r,x);
  }

  int qMin(int k) {
#ifdef DEBUG
    printf("  qMin(%d): %d\n",k,qMin(1,1,n,k));
#endif
    return qMin(1,1,n,k);
  }
  int qMin(int u, int l, int r, int k){
    if(k >= cnt[u]) return v[u];
    int m = (l+r)/2;

    int sum = qMin(u<<1, l, m, min(k, cnt[u<<1]));
    if(cnt[u<<1] < k) sum += qMin(u<<1|1, m+1, r, k - cnt[u<<1]);
    return sum;
  }
}sg;

void cal1(LL rec){
  VR<pii> v1, v2;
  FOR(i,0,n){
    if(node[i].a && node[i].b) v1.push_back({node[i].t,i+1});
    else v2.push_back({node[i].t, i+1});
  }
  sort(ALL(v1)); sort(ALL(v2));
  FOR(i,0,rec) ans.push_back(v1[i].SE);
  FOR(i,0,m-rec) ans.push_back(v2[i].SE);
}

void cal2(LL rec){
#ifdef DEBUG
  printf("cal2 %lld\n",rec);
#endif
  VR<pii> c, a, b, left;
  FOR(i,0,n){
    Node &t = node[i];
    if(t.a && t.b) c.push_back({t.t, i+1});
    else if(t.a) a.push_back({t.t, i+1});
    else if(t.b) b.push_back({t.t, i+1});
    else left.push_back({t.t, i+1});
  }

  sort(ALL(a)); sort(ALL(b)); sort(ALL(c));
  FOR(i,0,rec) ans.push_back(c[i].SE);
  FOR(i,0,k-rec) ans.push_back(a[i].SE);
  FOR(i,0,k-rec) ans.push_back(b[i].SE);

  int nleft = m - (rec + k-rec + k-rec);
  FOR(i,k-rec,a.size()) left.push_back(a[i]);
  FOR(i,k-rec,b.size()) left.push_back(b[i]);
  FOR(i,rec,c.size()) left.push_back(c[i]);
#ifdef DEBUG
  printf("  nleft: %d\n",nleft);
  for(auto p : left) printf(" [%d, %d]\n",p.FI,p.SE);
#endif
  sort(ALL(left));
  FOR(i,0,nleft) ans.push_back(left[i].SE);
}

int main(void){
  scanf("%d%d%d",&n,&m,&k);
  FOR(i,0,n){
    int t,ta,tb;
    scanf("%d%d%d",&t,&ta,&tb);
    node[i] = {t,ta,tb};

    if(ta && tb) c.push_back(t);
    else{
      d.push_back(t);
      if(ta) a.push_back(t);
      else if(tb) b.push_back(t);
      else e.push_back(t);
    }
  }
  sort(ALL(a)); sort(ALL(b));
  sort(ALL(c)); sort(ALL(d)); sort(ALL(e));
#ifdef DEBUG
  printf("a:"); for(int x : a) printf(" %d",x); putchar('\n');
  printf("b:"); for(int x : b) printf(" %d",x); putchar('\n');
  printf("c:"); for(int x : c) printf(" %d",x); putchar('\n');
  printf("d:"); for(int x : d) printf(" %d",x); putchar('\n');
  printf("e:"); for(int x : e) printf(" %d",x); putchar('\n');
#endif

  VR<int> sa(a), sb(b);
  FOR(i,1,a.size()) sa[i] += sa[i-1];
  FOR(i,1,b.size()) sb[i] += sb[i-1];
  FOR(i,1,c.size()) c[i] += c[i-1];
  FOR(i,1,d.size()) d[i] += d[i-1];
  FOR(i,1,e.size()) e[i] += e[i-1];

  LL ansMin = INF, rec = -1; sg.n = 1e4;
  if(c.size() >= k) FORR(i,k,min((int)c.size(),m)){
    LL sum = c[i-1];
    if(m - i){
      if(d.size() < m-i) sum = INF;
      else sum += d[m-i-1];
    }
    if(sum < ansMin){
#ifdef DEBUG
      printf("c: %d, sum: %lld\n",i,sum);
#endif
      ansMin = sum; rec = i | (1ll<<60);
    }
  }

  bool first = true;
  for(int x : e) sg.add(x);
  FORR(i,0,min((int)c.size(), k-1)) if(a.size() >= k-i && b.size() >= k-i){
    if(first){
      FOR(j,k-i,a.size()) sg.add(a[j]);
      FOR(j,k-i,b.size()) sg.add(b[j]);
      first = false;
    }

    int left = m - (i + k-i + k-i);
    if(left < 0) continue;

    LL sum = i>0 ? c[i-1] : 0;
    sum += sa[k-i-1] + sb[k-i-1] + sg.qMin(left);
    if(sum < ansMin){
#ifdef DEBUG
      printf("c: %d, sum: %lld\n",i,sum);
#endif
      ansMin = sum; rec = i;
    }

    sg.add(a[k-i-1]), sg.add(b[k-i-1]);
  }

  if(rec & (1ll<<60)) cal1(rec - (1ll<<60));
  else cal2(rec);

  printf("%lld\n",ansMin);
  for(int x : ans) printf("%d ",x);
  putchar('\n');

  return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<cmath>
#include<ctime>
#include<vector>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<utility>
#include<algorithm>
using namespace std;

#define FOR(i,a,b) for(register int i=(a);i<(b);++i)
#define FORR(i,a,b) for(register int i=(a);i<=(b);++i)
#define ROR(i,a,b) for(register int i=(a);i>=(b);--i)
#define RORR(i,a,b) for(register int i=(a);i>(b);--i)
#define PQ priority_queue
#define VR vector
#define MST(a,b) memset(a,b,sizeof(a))
#define FGETS(s) fgets(s,sizeof(s),stdin)
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define FI first
#define SE second
typedef long long LL;
typedef long long unsigned LLU;
typedef pair<int,int> pii;

int n,m,k;
const LL INF = 0x3f3f3f3f3f3f3f3f;
VR<int> a,b,c,d,e,ans;
bool chosen[int(2e5+20)];
struct Node{
  int t,a,b;
}node[int(2e5+20)];

const int MAX = 1e4 + 20;
struct ST{
  int cnt[MAX<<2],v[MAX<<2], n;
  void add(int x, int v) {
#ifdef DEBUG
    printf("  add %d\n",x);
#endif
    return add(1,1,n,x,v);
  }
  void add(int u, int l, int r, int x, int val){
    if(val>0) ++cnt[u], v[u] += x;
    else      --cnt[u], v[u] -= x;

    if(l == r) return;
    int m = (l+r)/2;
    if(x <= m) add(u<<1,l,m,x,val);
    else add(u<<1|1,m+1,r,x,val);
  }

  int qMin(int k) {
#ifdef DEBUG
    printf("  qMin(%d): %d\n",k,qMin(1,1,n,k));
#endif
    return qMin(1,1,n,k);
  }
  int qMin(int u, int l, int r, int k){
    if(k >= cnt[u]) return v[u];
    int m = (l+r)/2;

    int sum = qMin(u<<1, l, m, min(k, cnt[u<<1]));
    if(cnt[u<<1] < k) sum += qMin(u<<1|1, m+1, r, k - cnt[u<<1]);
    return sum;
  }
}sg;

void cal1(LL rec){
  VR<pii> v1, v2;
  FOR(i,0,n){
    if(node[i].a && node[i].b) v1.push_back({node[i].t,i+1});
    else v2.push_back({node[i].t, i+1});
  }
  sort(ALL(v1)); sort(ALL(v2));
  FOR(i,0,rec) ans.push_back(v1[i].SE);
  FOR(i,0,m-rec) ans.push_back(v2[i].SE);
}

void cal2(LL rec){
#ifdef DEBUG
  printf("cal2 %lld\n",rec);
#endif
  VR<pii> c, a, b, left;
  FOR(i,0,n){
    Node &t = node[i];
    if(t.a && t.b) c.push_back({t.t, i+1});
    else if(t.a) a.push_back({t.t, i+1});
    else if(t.b) b.push_back({t.t, i+1});
    else left.push_back({t.t, i+1});
  }

  sort(ALL(a)); sort(ALL(b)); sort(ALL(c));
  FOR(i,0,rec) ans.push_back(c[i].SE);
  FOR(i,0,k-rec) ans.push_back(a[i].SE);
  FOR(i,0,k-rec) ans.push_back(b[i].SE);

  int nleft = m - (rec + k-rec + k-rec);
  FOR(i,k-rec,a.size()) left.push_back(a[i]);
  FOR(i,k-rec,b.size()) left.push_back(b[i]);
#ifdef DEBUG
  printf("  nleft: %d\n",nleft);
  for(auto p : left) printf(" [%d, %d]\n",p.FI,p.SE);
#endif
  sort(ALL(left));
  FOR(i,0,nleft) ans.push_back(left[i].SE);
}

int main(void){
  scanf("%d%d%d",&n,&m,&k);
  FOR(i,0,n){
    int t,ta,tb;
    scanf("%d%d%d",&t,&ta,&tb);
    node[i] = {t,ta,tb};

    if(ta && tb) c.push_back(t);
    else{
      d.push_back(t);
      if(ta) a.push_back(t);
      else if(tb) b.push_back(t);
      else e.push_back(t);
    }
  }
  sort(ALL(a)); sort(ALL(b)); sort(ALL(c)); sort(ALL(d)); sort(ALL(e));
#ifdef DEBUG
  printf("a:"); for(int x : a) printf(" %d",x); putchar('\n');
  printf("b:"); for(int x : b) printf(" %d",x); putchar('\n');
  printf("c:"); for(int x : c) printf(" %d",x); putchar('\n');
  printf("d:"); for(int x : d) printf(" %d",x); putchar('\n');
  printf("e:"); for(int x : e) printf(" %d",x); putchar('\n');
#endif

  VR<int> sa(a), sb(b), sc(c);
  FOR(i,1,a.size()) sa[i] += sa[i-1];
  FOR(i,1,b.size()) sb[i] += sb[i-1];
  FOR(i,1,c.size()) sc[i] += sc[i-1];
  FOR(i,1,d.size()) d[i] += d[i-1];
  FOR(i,1,e.size()) e[i] += e[i-1];

  LL ansMin = INF, rec = -1; sg.n = 1e4;
  if(c.size() >= k) FORR(i,k,min((int)c.size(),m)){
    LL sum = sc[i-1];
    if(i != m){
      if(d.size() < m-i) sum = INF;
      else sum += d[m-i-1];
    }
    if(sum < ansMin){
#ifdef DEBUG
      printf("c: %d, sum: %lld\n",i,sum);
#endif
      ansMin = sum; rec = i | (1ll<<60);
    }
  }

  bool first = true;
  for(int x : e) sg.add(x,1);
  FORR(i,0,min((int)c.size(), k-1)) if(a.size() >= k-i && b.size() >= k-i){
    if(first){
      FOR(j,k-i,a.size()) sg.add(a[j],1);
      FOR(j,k-i,b.size()) sg.add(b[j],1);
      first = false;
    }

    int left = m - (i + k-i + k-i);
    if(left < 0) continue;

    LL sum = i>0 ? sc[i-1] : 0;
    sum += sa[k-i-1] + sb[k-i-1] + sg.qMin(left);
    if(sum < ansMin){
#ifdef DEBUG
      printf("c: %d, sum: %lld\n",i,sum);
#endif
      ansMin = sum; rec = i;
    }

    sg.add(a[k-i-1],1), sg.add(b[k-i-1],1);
  }

  if(ansMin == INF) { printf("-1\n"); return 0; }
  if(rec & (1ll<<60)) cal1(rec - (1ll<<60));
  else cal2(rec);

  printf("%lld\n",ansMin);
  for(int x : ans) printf("%d ",x);
  putchar('\n');

  return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<cmath>
#include<ctime>
#include<vector>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<utility>
#include<algorithm>
using namespace std;

#define FOR(i,a,b) for(register int i=(a);i<(b);++i)
#define FORR(i,a,b) for(register int i=(a);i<=(b);++i)
#define ROR(i,a,b) for(register int i=(a);i>=(b);--i)
#define RORR(i,a,b) for(register int i=(a);i>(b);--i)
#define PQ priority_queue
#define VR vector
#define MST(a,b) memset(a,b,sizeof(a))
#define FGETS(s) fgets(s,sizeof(s),stdin)
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define FI first
#define SE second
typedef long long LL;
typedef long long unsigned LLU;
typedef pair<int,int> pii;

const int MAXN = 2e5 + 20, MAXM = 1e4 + 20;
int n,m,k;
struct Node{
  int t,a,b;
}node[MAXN];
deque<int> a,b,c,d,e,sa,sb,sc,se,ans;

struct ST{
  int n,cnt[MAXM<<2],v[MAXM<<2];
  void add(int x){
#ifdef DEBUG
    printf("  add %d\n",x);
#endif
    add(1,1,n,x);
  }
  void add(int u, int l, int r, int x){
    ++cnt[u], v[u] += x;
    if(l == r) return;
    int m = (l+r)/2;
    if(x<=m) add(u<<1,l,m,x);
    else add(u<<1|1,m+1,r,x);
  }
  int qMin(int k){
#ifdef DEBUG
    printf("  qMin(%d): %d\n",k,qMin(1,1,n,k));
#endif
    return qMin(1,1,n,k);
  }
  int qMin(int u, int l, int r, int k){
    if(k >= cnt[u]) return v[u];
    if(l == r) return k * l;
    int m = (l+r)/2, sum = 0;
    sum = qMin(u<<1,l,m,min(k,cnt[u<<1]));
    if(k > cnt[u<<1]) sum += qMin(u<<1|1,m+1,r,k-cnt[u<<1]);
    return sum;
  }
}sg;

void cal(int rec){
  if(rec >= k){
    VR<pii> c, e;
    c.push_back({0,0});
    e.push_back({0,0});
    FORR(i,1,n){
      Node &t = node[i];
      if(t.a && t.b) c.push_back({t.t, i});
      else e.push_back({t.t, i});
    }
    sort(ALL(c)), sort(ALL(e));
    FORR(i,1,rec) ans.push_back(c[i].SE);
    FORR(i,1,m-rec) ans.push_back(e[i].SE);
  } else {
    VR<pii> c, a, b, left;
    a.push_back({0,0}); b.push_back({0,0}); c.push_back({0,0}); left.push_back({0,0});
    FORR(i,1,n){
      Node &t = node[i];
      if(t.a && t.b) c.push_back({t.t, i});
      else if(t.a) a.push_back({t.t, i});
      else if(t.b) b.push_back({t.t, i});
      else left.push_back({t.t, i});
    }
    sort(ALL(a)), sort(ALL(b)), sort(ALL(c));
    FORR(i,1,rec) ans.push_back(c[i].SE);
    FORR(i,1,k-rec) ans.push_back(a[i].SE);
    FORR(i,1,k-rec) ans.push_back(b[i].SE);

    FOR(i,k-rec+1,a.size()) left.push_back(a[i]);
    FOR(i,k-rec+1,b.size()) left.push_back(b[i]);
    sort(ALL(left)); int r = m - 2*k + rec;
    FORR(i,1,r) ans.push_back(left[i].SE);
  }
}

int main(void){
  scanf("%d%d%d",&n,&m,&k); sg.n = 1e4;
  FORR(i,1,n){
    Node &t = node[i];
    scanf("%d%d%d",&t.t,&t.a,&t.b);
    if(t.a && t.b) c.push_back(t.t);
    else{
      if(t.a) a.push_back(t.t);
      else if(t.b) b.push_back(t.t);
      else d.push_back(t.t);
      e.push_back(t.t);
    }
  }
  sort(ALL(a)); sort(ALL(b)); sort(ALL(c)); sort(ALL(d)); sort(ALL(e));
  a.push_front(0); b.push_front(0); c.push_front(0); d.push_front(0); e.push_front(0);
  sa = a, sb = b, sc = c, se = e;
  FOR(i,1,sa.size()) sa[i] += sa[i-1];
  FOR(i,1,sb.size()) sb[i] += sb[i-1];
  FOR(i,1,sc.size()) sc[i] += sc[i-1];
  FOR(i,1,se.size()) se[i] += se[i-1];
#ifdef DEBUG
  printf("a:"); for(int x : a) printf(" %d",x); putchar('\n');
  printf("b:"); for(int x : b) printf(" %d",x); putchar('\n');
  printf("c:"); for(int x : c) printf(" %d",x); putchar('\n');
  printf("d:"); for(int x : d) printf(" %d",x); putchar('\n');
  printf("e:"); for(int x : e) printf(" %d",x); putchar('\n');
#endif

  int ansMin = INT_MAX, rec = -1;
  if(c.size() >= k) FORR(i,k,min(int(c.size()-1), m)){
    int sum = sc[i] + se[m-i];
    if(sum < ansMin){
#ifdef DEBUG
      printf("c: %d, sum: %d\n",i,sum);
#endif
      ansMin = sum, rec = i;
    }
  }

  FOR(i,1,d.size()) sg.add(d[i]);
  bool first = true;
  FORR(i, max(max(0, 2*k-m), (int)max(k-a.size(), k-b.size())), min(int(c.size()-1), k-1)){
    if(first){
      FOR(i,k-i+1,a.size()) sg.add(a[i]);
      FOR(i,k-i+1,b.size()) sg.add(b[i]);
      first = false;
    }

    int sum = sc[i] + sa[k-i] + sb[k-i] + sg.qMin(m-2*k+i);
    if(sum < ansMin){
#ifdef DEBUG
      printf("c: %d, sum: %d\n",i,sum);
#endif
      ansMin = sum, rec = i;
    }
    sg.add(a[k-i]); sg.add(b[k-i]);
  }

  if(ansMin == INT_MAX) { printf("-1\n"); return 0; }
  cal(rec);
  printf("%d\n",ansMin);
  for(int x : ans) printf("%d ",x); putchar('\n');

  return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<cmath>
#include<ctime>
#include<vector>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<utility>
#include<algorithm>
using namespace std;

#define FOR(i,a,b) for(register int i=(a);i<(b);++i)
#define FORR(i,a,b) for(register int i=(a);i<=(b);++i)
#define ROR(i,a,b) for(register int i=(a);i>=(b);--i)
#define RORR(i,a,b) for(register int i=(a);i>(b);--i)
#define PQ priority_queue
#define VR vector
#define MST(a,b) memset(a,b,sizeof(a))
#define FGETS(s) fgets(s,sizeof(s),stdin)
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define FI first
#define SE second
typedef long long LL;
typedef long long unsigned LLU;
typedef pair<int,int> pii;

const int MAXN = 2e5 + 20, MAXM = 1e4 + 20;
int n,m,k;
struct Node{
  int t,a,b;
}node[MAXN];
deque<int> a,b,c,d,e,sa,sb,sc,se,ans;

struct ST{
  int n,cnt[MAXM<<2],v[MAXM<<2];
  void add(int x){
#ifdef DEBUG
    printf("  add %d\n",x);
#endif
    add(1,1,n,x);
  }
  void add(int u, int l, int r, int x){
    ++cnt[u], v[u] += x;
    if(l == r) return;
    int m = (l+r)/2;
    if(x<=m) add(u<<1,l,m,x);
    else add(u<<1|1,m+1,r,x);
  }
  int qMin(int k){
#ifdef DEBUG
    printf("  qMin(%d): %d\n",k,qMin(1,1,n,k));
#endif
    return qMin(1,1,n,k);
  }
  int qMin(int u, int l, int r, int k){
    if(k >= cnt[u]) return v[u];
    if(l == r) return k * l;
    int m = (l+r)/2, sum = 0;
    sum = qMin(u<<1,l,m,min(k,cnt[u<<1]));
    if(k > cnt[u<<1]) sum += qMin(u<<1|1,m+1,r,k-cnt[u<<1]);
    return sum;
  }
}sg;

void cal(int rec){
  if(rec >= k){
    VR<pii> c, e;
    c.push_back({0,0});
    e.push_back({0,0});
    FORR(i,1,n){
      Node &t = node[i];
      if(t.a && t.b) c.push_back({t.t, i});
      else e.push_back({t.t, i});
    }
    sort(ALL(c)), sort(ALL(e));
    FORR(i,1,rec) ans.push_back(c[i].SE);
    FORR(i,1,m-rec) ans.push_back(e[i].SE);
  } else {
    VR<pii> c, a, b, left;
    a.push_back({0,0}); b.push_back({0,0}); c.push_back({0,0}); left.push_back({0,0});
    FORR(i,1,n){
      Node &t = node[i];
      if(t.a && t.b) c.push_back({t.t, i});
      else if(t.a) a.push_back({t.t, i});
      else if(t.b) b.push_back({t.t, i});
      else left.push_back({t.t, i});
    }
    sort(ALL(a)), sort(ALL(b)), sort(ALL(c));
    FORR(i,1,rec) ans.push_back(c[i].SE);
    FORR(i,1,k-rec) ans.push_back(a[i].SE);
    FORR(i,1,k-rec) ans.push_back(b[i].SE);

    FOR(i,k-rec+1,a.size()) left.push_back(a[i]);
    FOR(i,k-rec+1,b.size()) left.push_back(b[i]);
    sort(ALL(left)); int r = m - 2*k + rec;
    FORR(i,1,r) ans.push_back(left[i].SE);
  }
}

int main(void){
  scanf("%d%d%d",&n,&m,&k); sg.n = 1e4;
  FORR(i,1,n){
    Node &t = node[i];
    scanf("%d%d%d",&t.t,&t.a,&t.b);
    if(t.a && t.b) c.push_back(t.t);
    else{
      if(t.a) a.push_back(t.t);
      else if(t.b) b.push_back(t.t);
      else d.push_back(t.t);
      e.push_back(t.t);
    }
  }
  sort(ALL(a)); sort(ALL(b)); sort(ALL(c)); sort(ALL(d)); sort(ALL(e));
  int na = a.size(), nb = b.size(), nc = c.size(), nd = d.size(), ne = e.size();
  a.push_front(0); b.push_front(0); c.push_front(0); d.push_front(0); e.push_front(0);
  sa = a, sb = b, sc = c, se = e;
  FOR(i,1,sa.size()) sa[i] += sa[i-1];
  FOR(i,1,sb.size()) sb[i] += sb[i-1];
  FOR(i,1,sc.size()) sc[i] += sc[i-1];
  FOR(i,1,se.size()) se[i] += se[i-1];
#ifdef DEBUG
  printf("a:"); FORR(i,1,na) printf(" %d",a[i]); putchar('\n');
  printf("b:"); FORR(i,1,nb) printf(" %d",b[i]); putchar('\n');
  printf("c:"); FORR(i,1,nc) printf(" %d",c[i]); putchar('\n');
  printf("d:"); FORR(i,1,nd) printf(" %d",d[i]); putchar('\n');
  printf("e:"); FORR(i,1,ne) printf(" %d",e[i]); putchar('\n');
#endif

  int ansMin = INT_MAX, rec = -1;
  if(nc >= k) FORR(i,k,min(nc, m)){
    int sum = sc[i];
    if(ne < m-i) sum = INT_MAX;
    else sum += se[m-i];

    if(sum < ansMin){
#ifdef DEBUG
      printf("c: %d, sum: %d\n",i,sum);
#endif
      ansMin = sum, rec = i;
    }
  }

  FORR(i,1,nd) sg.add(d[i]);
  bool first = true;
  FORR(i, max(max(0, 2*k-m), max(k-na,k-nb)), min(nc, k-1)){
    if(first){
      FORR(j,k-i+1,na) sg.add(a[j]);
      FORR(j,k-i+1,nb) sg.add(b[j]);
      first = false;
    }

    int sum = sc[i] + sa[k-i] + sb[k-i] + sg.qMin(m-2*k+i);
    if(sum < ansMin){
#ifdef DEBUG
      printf("c: %d, sum: %d\n",i,sum);
#endif
      ansMin = sum, rec = i;
    }
    sg.add(a[k-i]); sg.add(b[k-i]);
  }

  if(ansMin == INT_MAX) { printf("-1\n"); return 0; }
  cal(rec);
  printf("%d\n",ansMin);
  for(int x : ans) printf("%d ",x); putchar('\n');

  return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<cmath>
#include<ctime>
#include<vector>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<utility>
#include<algorithm>
using namespace std;

#define FOR(i,a,b) for(register int i=(a);i<(b);++i)
#define FORR(i,a,b) for(register int i=(a);i<=(b);++i)
#define ROR(i,a,b) for(register int i=(a);i>=(b);--i)
#define RORR(i,a,b) for(register int i=(a);i>(b);--i)
#define PQ priority_queue
#define VR vector
#define MST(a,b) memset(a,b,sizeof(a))
#define FGETS(s) fgets(s,sizeof(s),stdin)
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define FI first
#define SE second
typedef long long LL;
typedef long long unsigned LLU;
typedef pair<int,int> pii;

int n,m,k;
const LL INF = 0x3f3f3f3f3f3f3f3f;
VR<int> a,b,c,d,e,ans;
bool chosen[int(2e5+20)];
struct Node{
  int t,a,b;
}node[int(2e5+20)];

const int MAX = 1e4 + 20;
struct ST{
  int cnt[MAX<<2],v[MAX<<2], n;
  void add(int x, int v) {
#ifdef DEBUG
    printf("  add %d\n",x);
#endif
    return add(1,1,n,x,v);
  }
  void add(int u, int l, int r, int x, int val){
    if(val>0) ++cnt[u], v[u] += x;
    else --cnt[u], v[u] -= x;

    if(l == r) return;
    int m = (l+r)/2;
    if(x <= m) add(u<<1,l,m,x,val);
    else add(u<<1|1,m+1,r,x,val);
  }

  int qMin(int k) {
#ifdef DEBUG
    printf("  qMin(%d): %d\n",k,qMin(1,1,n,k));
#endif
    return qMin(1,1,n,k);
  }
  int qMin(int u, int l, int r, int k){
    if(k >= cnt[u]) return v[u];
    int m = (l+r)/2;

    int sum = qMin(u<<1, l, m, min(k, cnt[u<<1]));
    if(cnt[u<<1] < k) sum += qMin(u<<1|1, m+1, r, k - cnt[u<<1]);
    return sum;
  }
}sg;

void cal1(LL rec){
  VR<pii> v1, v2;
  FOR(i,0,n){
    if(node[i].a && node[i].b) v1.push_back({node[i].t,i+1});
    else v2.push_back({node[i].t, i+1});
  }
  sort(ALL(v1)); sort(ALL(v2));
  FOR(i,0,rec) ans.push_back(v1[i].SE);
  FOR(i,0,m-rec) ans.push_back(v2[i].SE);
}

void cal2(LL rec){
#ifdef DEBUG
  printf("cal2 %lld\n",rec);
#endif
  VR<pii> c, a, b, left;
  FOR(i,0,n){
    Node &t = node[i];
    if(t.a && t.b) c.push_back({t.t, i+1});
    else if(t.a) a.push_back({t.t, i+1});
    else if(t.b) b.push_back({t.t, i+1});
    else left.push_back({t.t, i+1});
  }

  sort(ALL(a)); sort(ALL(b)); sort(ALL(c));
  FOR(i,0,rec) ans.push_back(c[i].SE);
  FOR(i,0,k-rec) ans.push_back(a[i].SE);
  FOR(i,0,k-rec) ans.push_back(b[i].SE);

  int nleft = m - (rec + k-rec + k-rec);
  FOR(i,k-rec,a.size()) left.push_back(a[i]);
  FOR(i,k-rec,b.size()) left.push_back(b[i]);
  FOR(i,rec,c.size()) left.push_back(c[i]);
#ifdef DEBUG
  printf("  nleft: %d\n",nleft);
  for(auto p : left) printf(" [%d, %d]\n",p.FI,p.SE);
#endif
  sort(ALL(left));
  FOR(i,0,nleft) ans.push_back(left[i].SE);
}

int main(void){
  scanf("%d%d%d",&n,&m,&k);
  FOR(i,0,n){
    int t,ta,tb;
    scanf("%d%d%d",&t,&ta,&tb);
    node[i] = {t,ta,tb};

    if(ta && tb) c.push_back(t);
    else{
      d.push_back(t);
      if(ta) a.push_back(t);
      else if(tb) b.push_back(t);
      else e.push_back(t);
    }
  }
  sort(ALL(a)); sort(ALL(b));
  sort(ALL(c)); sort(ALL(d)); sort(ALL(e));
#ifdef DEBUG
  printf("a:"); for(int x : a) printf(" %d",x); putchar('\n');
  printf("b:"); for(int x : b) printf(" %d",x); putchar('\n');
  printf("c:"); for(int x : c) printf(" %d",x); putchar('\n');
  printf("d:"); for(int x : d) printf(" %d",x); putchar('\n');
  printf("e:"); for(int x : e) printf(" %d",x); putchar('\n');
#endif

  VR<int> sa(a), sb(b), sc(c);
  FOR(i,1,a.size()) sa[i] += sa[i-1];
  FOR(i,1,b.size()) sb[i] += sb[i-1];
  FOR(i,1,c.size()) sc[i] += sc[i-1];
  FOR(i,1,d.size()) d[i] += d[i-1];
  FOR(i,1,e.size()) e[i] += e[i-1];

  LL ansMin = INF, rec = -1; sg.n = 1e4;
  if(c.size() >= k) FORR(i,k,min((int)c.size(),m)){
    LL sum = sc[i-1];
    if(i != m){
      if(d.size() < m-i) sum = INF;
      else sum += d[m-i-1];
    }
    if(sum < ansMin){
#ifdef DEBUG
      printf("c: %d, sum: %lld\n",i,sum);
#endif
      ansMin = sum; rec = i | (1ll<<60);
    }
  }

  bool first = true;
  for(int x : e) sg.add(x,1);
  for(int x : c) sg.add(x,1);
  FORR(i,0,min((int)c.size(), k-1)) if(a.size() >= k-i && b.size() >= k-i){
    if(first){
      FOR(j,k-i,a.size()) sg.add(a[j],1);
      FOR(j,k-i,b.size()) sg.add(b[j],1);
      first = false;
    }

    int left = m - (i + k-i + k-i);
    if(left < 0) continue;

    if(i) sg.add(c[i-1], -1);

    LL sum = i>0 ? sc[i-1] : 0;
    sum += sa[k-i-1] + sb[k-i-1] + sg.qMin(left);
    if(sum < ansMin){
#ifdef DEBUG
      printf("c: %d, sum: %lld\n",i,sum);
#endif
      ansMin = sum; rec = i;
    }

    sg.add(a[k-i-1],1), sg.add(b[k-i-1],1);
  }

  if(ansMin == INF) { printf("-1\n"); return 0; }
  if(rec & (1ll<<60)) cal1(rec - (1ll<<60));
  else cal2(rec);

  printf("%lld\n",ansMin);
  for(int x : ans) printf("%d ",x);
  putchar('\n');

  return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<cmath>
#include<ctime>
#include<vector>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<utility>
#include<algorithm>
using namespace std;

#define FOR(i,a,b) for(register int i=(a);i<(b);++i)
#define FORR(i,a,b) for(register int i=(a);i<=(b);++i)
#define ROR(i,a,b) for(register int i=(a);i>=(b);--i)
#define RORR(i,a,b) for(register int i=(a);i>(b);--i)
#define PQ priority_queue
#define VR vector
#define MST(a,b) memset(a,b,sizeof(a))
#define FGETS(s) fgets(s,sizeof(s),stdin)
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define FI first
#define SE second
typedef long long LL;
typedef long long unsigned LLU;
typedef pair<int,int> pii;

int n,m,k;
const LL INF = 0x3f3f3f3f3f3f3f3f;
VR<int> a,b,c,d,e,ans;
bool chosen[int(2e5+20)];
struct Node{
  int t,a,b;
}node[int(2e5+20)];

const int MAX = 1e4 + 20;
struct ST{
  int cnt[MAX<<2],v[MAX<<2], n;
  void add(int x) {
#ifdef DEBUG
    printf("  add %d\n",x);
#endif
    return add(1,1,n,x);
  }
  void add(int u, int l, int r, int x){
    if(l == r){
      ++cnt[u]; v[u] += x;
      return;
    }
    int m = (l+r)/2;
    if(x <= m) add(u<<1,l,m,x);
    else add(u<<1|1,m+1,r,x);
    ++cnt[u]; v[u] += x;
  }

  LL qMin(int k) {
#ifdef DEBUG
    printf("  qMin(%d): %lld\n",k,qMin(1,1,n,k));
#endif
    return qMin(1,1,n,k);
  }
  LL qMin(int u, int l, int r, int k){
    if(k >= cnt[u]) return v[u];
    int m = (l+r)/2;

    LL sum = qMin(u<<1, l, m, min(k, cnt[u<<1]));
    if(cnt[u<<1] < k) sum += qMin(u<<1|1, m+1, r, k - cnt[u<<1]);
    return sum;
  }
}sg;

void cal1(LL rec){
  VR<pii> v1, v2;
  FOR(i,0,n){
    if(node[i].a && node[i].b) v1.push_back({node[i].t,i+1});
    else v2.push_back({node[i].t, i+1});
  }
  sort(ALL(v1)); sort(ALL(v2));
  FOR(i,0,rec) ans.push_back(v1[i].SE);
  FOR(i,0,m-rec) ans.push_back(v2[i].SE);
}
void cal2(LL rec){
#ifdef DEBUG
  printf("cal2 %lld\n",rec);
#endif
  VR<pii> c, a, b, left;
  FOR(i,0,n){
    Node &t = node[i];
    if(t.a && t.b) c.push_back({t.t, i+1});
    else if(t.a) a.push_back({t.t, i+1});
    else if(t.b) b.push_back({t.t, i+1});
    else left.push_back({t.t, i+1});
  }

  sort(ALL(a)); sort(ALL(b)); sort(ALL(c));
  FOR(i,0,rec) ans.push_back(c[i].SE);
  FOR(i,0,k-rec) ans.push_back(a[i].SE);
  FOR(i,0,k-rec) ans.push_back(b[i].SE);

  FOR(i,k-rec,a.size()) left.push_back(a[i]);
  FOR(i,k-rec,b.size()) left.push_back(b[i]);
  sort(ALL(left));
  int nleft = m - (rec + k-rec + k-rec);
  FOR(i,0,nleft) ans.push_back(left[i].SE);
}

int main(void){
  scanf("%d%d%d",&n,&m,&k);
  FOR(i,0,n){
    int t,ta,tb;
    scanf("%d%d%d",&t,&ta,&tb);
    node[i] = {t,ta,tb};

    if(ta && tb) c.push_back(t);
    else{
      d.push_back(t);
      if(ta) a.push_back(t);
      else if(tb) b.push_back(t);
      else e.push_back(t);
    }
  }
  sort(ALL(a)); sort(ALL(b));
  sort(ALL(c)); sort(ALL(d)); sort(ALL(e));
#ifdef DEBUG
  printf("a:"); for(int x : a) printf(" %d",x); putchar('\n');
  printf("b:"); for(int x : b) printf(" %d",x); putchar('\n');
  printf("c:"); for(int x : c) printf(" %d",x); putchar('\n');
  printf("d:"); for(int x : d) printf(" %d",x); putchar('\n');
#endif

  VR<int> sa(a), sb(b);
  FOR(i,1,a.size()) sa[i] += sa[i-1];
  FOR(i,1,b.size()) sb[i] += sb[i-1];
  FOR(i,1,c.size()) c[i] += c[i-1];
  FOR(i,1,d.size()) d[i] += d[i-1];
  FOR(i,1,e.size()) e[i] += e[i-1];

  LL ansMin = INF, rec = -1; sg.n = 1e4;
  if(c.size() >= k) FORR(i,k,min((int)c.size(),m)){
    LL sum = c[i-1];
    if(m - i){
      if(d.size() < m-i) sum = INF;
      else sum += d[m-i-1];
    }
    if(sum < ansMin){
#ifdef DEBUG
      printf("c: %d, sum: %lld\n",i,sum);
#endif
      ansMin = sum;
      rec = i | (1ll<<60);
    }
  }

  bool first = true;
  for(int x : e) sg.add(x);
  FORR(i,0,min((int)c.size(), k-1)) if(a.size() >= k-i && b.size() >= k-i){
    if(first){
      FOR(j,k-i,a.size()) sg.add(a[j]);
      FOR(j,k-i,b.size()) sg.add(b[j]);
      first = false;
    }

    int left = m - (i + k-i + k-i);
    if(left < 0) continue;

    LL sum = i>0 ? c[i-1] : 0;
    sum += sa[k-i-1] + sb[k-i-1] + sg.qMin(left);
    if(sum < ansMin){
#ifdef DEBUG
      printf("c: %d, sum: %lld\n",i,sum);
#endif
      ansMin = sum; rec = i;
    }

    sg.add(a[k-i-1]), sg.add(b[k-i-1]);
  }

  if(rec & (1ll<<60)) cal1(rec - (1ll<<60));
  else cal2(rec);

  printf("%lld\n",ansMin);
  for(int x : ans) printf("%d ",x);
  putchar('\n');

  return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<cmath>
#include<ctime>
#include<vector>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<utility>
#include<algorithm>
using namespace std;

#define FOR(i,a,b) for(register int i=(a);i<(b);++i)
#define FORR(i,a,b) for(register int i=(a);i<=(b);++i)
#define ROR(i,a,b) for(register int i=(a);i>=(b);--i)
#define RORR(i,a,b) for(register int i=(a);i>(b);--i)
#define PQ priority_queue
#define VR vector
#define MST(a,b) memset(a,b,sizeof(a))
#define FGETS(s) fgets(s,sizeof(s),stdin)
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define FI first
#define SE second
typedef long long LL;
typedef long long unsigned LLU;
typedef pair<int,int> pii;

int n,m,k;
const LL INF = 0x3f3f3f3f3f3f3f3f;
VR<int> a,b,c,d,e,ans;
bool chosen[int(2e5+20)];
struct Node{
  int t,a,b;
}node[int(2e5+20)];

const int MAX = 1e4 + 20;
struct ST{
  int cnt[MAX<<2],v[MAX<<2], n;
  void add(int x, int v) {
#ifdef DEBUG
    printf("  add %d\n",x);
#endif
    return add(1,1,n,x,v);
  }
  void add(int u, int l, int r, int x, int val){
    if(val>0) ++cnt[u], v[u] += x;
    else --cnt[u], v[u] -= x;

    if(l == r) return;
    int m = (l+r)/2;
    if(x <= m) add(u<<1,l,m,x,val);
    else add(u<<1|1,m+1,r,x,val);
  }

  int qMin(int k) {
#ifdef DEBUG
    printf("  qMin(%d): %d\n",k,qMin(1,1,n,k));
#endif
    return qMin(1,1,n,k);
  }
  int qMin(int u, int l, int r, int k){
    if(k >= cnt[u]) return v[u];
    int m = (l+r)/2;

    int sum = qMin(u<<1, l, m, min(k, cnt[u<<1]));
    if(cnt[u<<1] < k) sum += qMin(u<<1|1, m+1, r, k - cnt[u<<1]);
    return sum;
  }
}sg;

void cal1(LL rec){
  VR<pii> v1, v2;
  FOR(i,0,n){
    if(node[i].a && node[i].b) v1.push_back({node[i].t,i+1});
    else v2.push_back({node[i].t, i+1});
  }
  sort(ALL(v1)); sort(ALL(v2));
  FOR(i,0,rec) ans.push_back(v1[i].SE);
  FOR(i,0,m-rec) ans.push_back(v2[i].SE);
}

void cal2(LL rec){
#ifdef DEBUG
  printf("cal2 %lld\n",rec);
#endif
  VR<pii> c, a, b, left;
  FOR(i,0,n){
    Node &t = node[i];
    if(t.a && t.b) c.push_back({t.t, i+1});
    else if(t.a) a.push_back({t.t, i+1});
    else if(t.b) b.push_back({t.t, i+1});
    else left.push_back({t.t, i+1});
  }

  sort(ALL(a)); sort(ALL(b)); sort(ALL(c));
  FOR(i,0,rec) ans.push_back(c[i].SE);
  FOR(i,0,k-rec) ans.push_back(a[i].SE);
  FOR(i,0,k-rec) ans.push_back(b[i].SE);

  int nleft = m - (rec + k-rec + k-rec);
  FOR(i,k-rec,a.size()) left.push_back(a[i]);
  FOR(i,k-rec,b.size()) left.push_back(b[i]);
  FOR(i,rec,c.size()) left.push_back(c[i]);
#ifdef DEBUG
  printf("  nleft: %d\n",nleft);
  for(auto p : left) printf(" [%d, %d]\n",p.FI,p.SE);
#endif
  sort(ALL(left));
  FOR(i,0,nleft) ans.push_back(left[i].SE);
}

int main(void){
  scanf("%d%d%d",&n,&m,&k);
  FOR(i,0,n){
    int t,ta,tb;
    scanf("%d%d%d",&t,&ta,&tb);
    node[i] = {t,ta,tb};

    if(ta && tb) c.push_back(t);
    else{
      d.push_back(t);
      if(ta) a.push_back(t);
      else if(tb) b.push_back(t);
      else e.push_back(t);
    }
  }
  sort(ALL(a)); sort(ALL(b));
  sort(ALL(c)); sort(ALL(d)); sort(ALL(e));
#ifdef DEBUG
  printf("a:"); for(int x : a) printf(" %d",x); putchar('\n');
  printf("b:"); for(int x : b) printf(" %d",x); putchar('\n');
  printf("c:"); for(int x : c) printf(" %d",x); putchar('\n');
  printf("d:"); for(int x : d) printf(" %d",x); putchar('\n');
  printf("e:"); for(int x : e) printf(" %d",x); putchar('\n');
#endif

  VR<int> sa(a), sb(b);
  FOR(i,1,a.size()) sa[i] += sa[i-1];
  FOR(i,1,b.size()) sb[i] += sb[i-1];
  FOR(i,1,c.size()) c[i] += c[i-1];
  FOR(i,1,d.size()) d[i] += d[i-1];
  FOR(i,1,e.size()) e[i] += e[i-1];

  LL ansMin = INF, rec = -1; sg.n = 1e4;
  if(c.size() >= k) FORR(i,k,min((int)c.size(),m)){
    LL sum = c[i-1];
    if(i != m){
      if(d.size() < m-i) sum = INF;
      else sum += d[m-i-1];
    }
    if(sum < ansMin){
#ifdef DEBUG
      printf("c: %d, sum: %lld\n",i,sum);
#endif
      ansMin = sum; rec = i | (1ll<<60);
    }
  }

  bool first = true;
  for(int x : e) sg.add(x,1);
  for(int x : c) sg.add(x,1);
  FORR(i,0,min((int)c.size(), k-1)) if(a.size() >= k-i && b.size() >= k-i){
    if(first){
      FOR(j,k-i,a.size()) sg.add(a[j],1);
      FOR(j,k-i,b.size()) sg.add(b[j],1);
      first = false;
    }

    int left = m - (i + k-i + k-i);
    if(left < 0) continue;

    LL sum = i>0 ? c[i-1] : 0;
    sum += sa[k-i-1] + sb[k-i-1] + sg.qMin(left);
    if(sum < ansMin){
#ifdef DEBUG
      printf("c: %d, sum: %lld\n",i,sum);
#endif
      ansMin = sum; rec = i;
    }

    sg.add(a[k-i-1],1), sg.add(b[k-i-1],1);
    if(i) sg.add(c[i]-c[i-1],-1);
    else sg.add(c[i],-1);
  }

  if(ansMin == INF) { printf("-1\n"); return 0; }
  if(rec & (1ll<<60)) cal1(rec - (1ll<<60));
  else cal2(rec);

  printf("%lld\n",ansMin);
  for(int x : ans) printf("%d ",x);
  putchar('\n');

  return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<cmath>
#include<ctime>
#include<vector>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<utility>
#include<algorithm>
using namespace std;

#define FOR(i,a,b) for(register int i=(a);i<(b);++i)
#define FORR(i,a,b) for(register int i=(a);i<=(b);++i)
#define ROR(i,a,b) for(register int i=(a);i>=(b);--i)
#define RORR(i,a,b) for(register int i=(a);i>(b);--i)
#define PQ priority_queue
#define VR vector
#define MST(a,b) memset(a,b,sizeof(a))
#define FGETS(s) fgets(s,sizeof(s),stdin)
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define FI first
#define SE second
typedef long long LL;
typedef long long unsigned LLU;
typedef pair<int,int> pii;

int n,m,k;
const LL INF = 0x3f3f3f3f3f3f3f3f;
VR<int> a,b,c,d,e,ans;
struct Node{
  int t,a,b;
}node[int(2e5+20)];

const int MAX = 1e4 + 20;
struct ST{
  int cnt[MAX<<2],v[MAX<<2], n;
  void add(int x, int v) {
#ifdef DEBUG
    printf("  add %d\n",x);
#endif
    return add(1,1,n,x,v);
  }
  void add(int u, int l, int r, int x, int val){
    if(val>0) ++cnt[u], v[u] += x;
    else      --cnt[u], v[u] -= x;

    if(l == r) return;
    int m = (l+r)/2;
    if(x <= m) add(u<<1,l,m,x,val);
    else add(u<<1|1,m+1,r,x,val);
  }

  int qMin(int k) {
#ifdef DEBUG
    printf("  qMin(%d): %d\n",k,qMin(1,1,n,k));
#endif
    return qMin(1,1,n,k);
  }
  int qMin(int u, int l, int r, int k){
    if(!k) return 0;
    if(k >= cnt[u]) return v[u];
    int m = (l+r)/2;

    int sum = qMin(u<<1, l, m, min(k, cnt[u<<1]));
    if(cnt[u<<1] < k) sum += qMin(u<<1|1, m+1, r, k - cnt[u<<1]);
    return sum;
  }
}sg;

void cal1(LL rec){
  VR<pii> v1, v2;
  FOR(i,0,n){
    if(node[i].a && node[i].b) v1.push_back({node[i].t,i+1});
    else v2.push_back({node[i].t, i+1});
  }
  sort(ALL(v1)); sort(ALL(v2));
  FOR(i,0,rec) ans.push_back(v1[i].SE);
  FOR(i,0,m-rec) ans.push_back(v2[i].SE);
}

void cal2(LL rec){
#ifdef DEBUG
  printf("cal2 %lld\n",rec);
#endif
  VR<pii> c, a, b, left;
  FOR(i,0,n){
    Node &t = node[i];
    if(t.a && t.b) c.push_back({t.t, i+1});
    else if(t.a) a.push_back({t.t, i+1});
    else if(t.b) b.push_back({t.t, i+1});
    else left.push_back({t.t, i+1});
  }

  sort(ALL(a)); sort(ALL(b)); sort(ALL(c));
  FOR(i,0,rec) ans.push_back(c[i].SE);
  FOR(i,0,k-rec) ans.push_back(a[i].SE);
  FOR(i,0,k-rec) ans.push_back(b[i].SE);

  int nleft = m - (rec + k-rec + k-rec);
  FOR(i,k-rec,a.size()) left.push_back(a[i]);
  FOR(i,k-rec,b.size()) left.push_back(b[i]);
#ifdef DEBUG
  printf("  nleft: %d\n",nleft);
  for(auto p : left) printf(" [%d, %d]\n",p.FI,p.SE);
#endif
  sort(ALL(left));
  FOR(i,0,nleft) ans.push_back(left[i].SE);
}

int main(void){
  scanf("%d%d%d",&n,&m,&k);
  FOR(i,0,n){
    int t,ta,tb;
    scanf("%d%d%d",&t,&ta,&tb);
    node[i] = {t,ta,tb};

    if(ta && tb) c.push_back(t);
    else{
      d.push_back(t);
      if(ta) a.push_back(t);
      else if(tb) b.push_back(t);
      else e.push_back(t);
    }
  }
  sort(ALL(a)); sort(ALL(b)); sort(ALL(c)); sort(ALL(d)); sort(ALL(e));
#ifdef DEBUG
  printf("a:"); for(int x : a) printf(" %d",x); putchar('\n');
  printf("b:"); for(int x : b) printf(" %d",x); putchar('\n');
  printf("c:"); for(int x : c) printf(" %d",x); putchar('\n');
  printf("d:"); for(int x : d) printf(" %d",x); putchar('\n');
  printf("e:"); for(int x : e) printf(" %d",x); putchar('\n');
#endif

  VR<int> sa(a), sb(b), sc(c);
  FOR(i,1,a.size()) sa[i] += sa[i-1];
  FOR(i,1,b.size()) sb[i] += sb[i-1];
  FOR(i,1,c.size()) sc[i] += sc[i-1];
  FOR(i,1,d.size()) d[i] += d[i-1];
  FOR(i,1,e.size()) e[i] += e[i-1];

  LL ansMin = INF, rec = -1; sg.n = 1e4;
  if(c.size() >= k) FORR(i,k,min((int)c.size(),m)){
    LL sum = sc[i-1];
    if(i != m){
      if(d.size() < m-i) sum = INF;
      else sum += d[m-i-1];
    }
    if(sum < ansMin){
#ifdef DEBUG
      printf("c: %d, sum: %lld\n",i,sum);
#endif
      ansMin = sum; rec = i | (1ll<<60);
    }
  }

  bool first = true;
  for(int x : e) sg.add(x,1);
  FORR(i,0,min((int)c.size(), k-1)) if(a.size() >= k-i && b.size() >= k-i){
    int left = m - (i + k-i + k-i);
    if(left < 0) continue;

    if(first){
      FOR(j,k-i,a.size()) sg.add(a[j],1);
      FOR(j,k-i,b.size()) sg.add(b[j],1);
      first = false;
    }

    LL sum = i>0 ? sc[i-1] : 0;
    sum += sa[k-i-1] + sb[k-i-1] + sg.qMin(left);
    if(sum < ansMin){
#ifdef DEBUG
      printf("c: %d, sum: %lld\n",i,sum);
#endif
      ansMin = sum; rec = i;
    }

    sg.add(a[k-i-1],1), sg.add(b[k-i-1],1);
  }

  if(ansMin == INF) { printf("-1\n"); return 0; }
  if(rec & (1ll<<60)) cal1(rec - (1ll<<60));
  else cal2(rec);

  printf("%lld\n",ansMin);
  for(int x : ans) printf("%d ",x);
  putchar('\n');

  return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<cmath>
#include<ctime>
#include<vector>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<utility>
#include<algorithm>
using namespace std;

#define FOR(i,a,b) for(register int i=(a);i<(b);++i)
#define FORR(i,a,b) for(register int i=(a);i<=(b);++i)
#define ROR(i,a,b) for(register int i=(a);i>=(b);--i)
#define RORR(i,a,b) for(register int i=(a);i>(b);--i)
#define PQ priority_queue
#define VR vector
#define MST(a,b) memset(a,b,sizeof(a))
#define FGETS(s) fgets(s,sizeof(s),stdin)
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define FI first
#define SE second
typedef long long LL;
typedef long long unsigned LLU;
typedef pair<int,int> pii;

const int MAXN = 2e5 + 20, MAXM = 1e4 + 20;
int n,m,k;
struct Node{
  int t,a,b;
}node[MAXN];
deque<int> a,b,c,d,e,sa,sb,sc,se,ans;

struct ST{
  int n,cnt[MAXM<<2],v[MAXM<<2];
  void add(int x){
#ifdef DEBUG
    printf("  add %d\n",x);
#endif
    add(1,1,n,x);
  }
  void add(int u, int l, int r, int x){
    ++cnt[u], v[u] += x;
    if(l == r) return;
    int m = (l+r)/2;
    if(x<=m) add(u<<1,l,m,x);
    else add(u<<1|1,m+1,r,x);
  }
  int qMin(int k){
#ifdef DEBUG
    printf("  qMin(%d): %d\n",k,qMin(1,1,n,k));
#endif
    return qMin(1,1,n,k);
  }
  int qMin(int u, int l, int r, int k){
    if(k >= cnt[u]) return v[u];
    if(l == r) return k * l;
    int m = (l+r)/2, sum = 0;
    sum = qMin(u<<1,l,m,min(k,cnt[u<<1]));
    if(k > cnt[u<<1]) sum += qMin(u<<1|1,m+1,r,k-cnt[u<<1]);
    return sum;
  }
}sg;

void cal(int rec){
  if(rec >= k){
    VR<pii> c, e;
    c.push_back({0,0});
    e.push_back({0,0});
    FORR(i,1,n){
      Node &t = node[i];
      if(t.a && t.b) c.push_back({t.t, i});
      else e.push_back({t.t, i});
    }
    sort(ALL(c)), sort(ALL(e));
    FORR(i,1,rec) ans.push_back(c[i].SE);
    FORR(i,1,m-rec) ans.push_back(e[i].SE);
  } else {
    VR<pii> c, a, b, left;
    a.push_back({0,0}); b.push_back({0,0}); c.push_back({0,0}); left.push_back({0,0});
    FORR(i,1,n){
      Node &t = node[i];
      if(t.a && t.b) c.push_back({t.t, i});
      else if(t.a) a.push_back({t.t, i});
      else if(t.b) b.push_back({t.t, i});
      else left.push_back({t.t, i});
    }
    sort(ALL(a)), sort(ALL(b)), sort(ALL(c));
    FORR(i,1,rec) ans.push_back(c[i].SE);
    FORR(i,1,k-rec) ans.push_back(a[i].SE);
    FORR(i,1,k-rec) ans.push_back(b[i].SE);

    FOR(i,k-rec+1,a.size()) left.push_back(a[i]);
    FOR(i,k-rec+1,b.size()) left.push_back(b[i]);
    sort(ALL(left)); int r = m - 2*k + rec;
    FORR(i,1,r) ans.push_back(left[i].SE);
  }
}

int main(void){
  scanf("%d%d%d",&n,&m,&k); sg.n = 1e4;
  FORR(i,1,n){
    Node &t = node[i];
    scanf("%d%d%d",&t.t,&t.a,&t.b);
    if(t.a && t.b) c.push_back(t.t);
    else{
      if(t.a) a.push_back(t.t);
      else if(t.b) b.push_back(t.t);
      else d.push_back(t.t);
      e.push_back(t.t);
    }
  }
  sort(ALL(a)); sort(ALL(b)); sort(ALL(c)); sort(ALL(d)); sort(ALL(e));
  int na = a.size(), nb = b.size(), nc = c.size(), nd = d.size(), ne = e.size();
  a.push_front(0); b.push_front(0); c.push_front(0); d.push_front(0); e.push_front(0);
  sa = a, sb = b, sc = c, se = e;
  FOR(i,1,sa.size()) sa[i] += sa[i-1];
  FOR(i,1,sb.size()) sb[i] += sb[i-1];
  FOR(i,1,sc.size()) sc[i] += sc[i-1];
  FOR(i,1,se.size()) se[i] += se[i-1];
#ifdef DEBUG
  printf("a:"); FORR(i,1,na) printf(" %d",a[i]); putchar('\n');
  printf("b:"); FORR(i,1,nb) printf(" %d",b[i]); putchar('\n');
  printf("c:"); FORR(i,1,nc) printf(" %d",c[i]); putchar('\n');
  printf("d:"); FORR(i,1,nd) printf(" %d",d[i]); putchar('\n');
  printf("e:"); FORR(i,1,ne) printf(" %d",e[i]); putchar('\n');
#endif

  int ansMin = INT_MAX, rec = -1;
  if(nc >= k) FORR(i,k,min(nc, m)){
    int sum = sc[i] + se[m-i];
    if(sum < ansMin){
#ifdef DEBUG
      printf("c: %d, sum: %d\n",i,sum);
#endif
      ansMin = sum, rec = i;
    }
  }

  FORR(i,1,nd) sg.add(d[i]);
  bool first = true;
  FORR(i, max(max(0, 2*k-m), max(k-na,k-nb)), min(nc, k-1)){
    if(first){
      FORR(j,k-i+1,na) sg.add(a[j]);
      FORR(j,k-i+1,nb) sg.add(b[j]);
      first = false;
    }

    int sum = sc[i] + sa[k-i] + sb[k-i] + sg.qMin(m-2*k+i);
    if(sum < ansMin){
#ifdef DEBUG
      printf("c: %d, sum: %d\n",i,sum);
#endif
      ansMin = sum, rec = i;
    }
    sg.add(a[k-i]); sg.add(b[k-i]);
  }

  if(ansMin == INT_MAX) { printf("-1\n"); return 0; }
  cal(rec);
  printf("%d\n",ansMin);
  for(int x : ans) printf("%d ",x); putchar('\n');

  return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<cmath>
#include<ctime>
#include<vector>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<utility>
#include<algorithm>
using namespace std;

#define FOR(i,a,b) for(register int i=(a);i<(b);++i)
#define FORR(i,a,b) for(register int i=(a);i<=(b);++i)
#define ROR(i,a,b) for(register int i=(a);i>=(b);--i)
#define RORR(i,a,b) for(register int i=(a);i>(b);--i)
#define PQ priority_queue
#define VR vector
#define MST(a,b) memset(a,b,sizeof(a))
#define FGETS(s) fgets(s,sizeof(s),stdin)
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define FI first
#define SE second
typedef long long LL;
typedef long long unsigned LLU;
typedef pair<int,int> pii;

int n,m,k;
const LL INF = 0x3f3f3f3f3f3f3f3f;
VR<int> a,b,c,d,e,ans;
struct Node{
  int t,a,b;
}node[int(2e5+20)];

const int MAX = 1e4 + 20;
struct ST{
  int cnt[MAX<<2],v[MAX<<2], n;
  void add(int x, int v) {
#ifdef DEBUG
    printf("  add %d\n",x);
#endif
    return add(1,1,n,x,v);
  }
  void add(int u, int l, int r, int x, int val){
    if(val>0) ++cnt[u], v[u] += x;
    else      --cnt[u], v[u] -= x;

    if(l == r) return;
    int m = (l+r)/2;
    if(x <= m) add(u<<1,l,m,x,val);
    else add(u<<1|1,m+1,r,x,val);
  }

  int qMin(int k) {
#ifdef DEBUG
    printf("  qMin(%d): %d\n",k,qMin(1,1,n,k));
#endif
    return qMin(1,1,n,k);
  }
  int qMin(int u, int l, int r, int k){
    if(!k) return 0;
    if(k >= cnt[u]) return v[u];
    int m = (l+r)/2;

    int sum = qMin(u<<1, l, m, min(k, cnt[u<<1]));
    if(cnt[u<<1] < k) sum += qMin(u<<1|1, m+1, r, k - cnt[u<<1]);
    return sum;
  }
}sg;

void cal1(LL rec){
  VR<pii> v1, v2;
  FOR(i,0,n){
    if(node[i].a && node[i].b) v1.push_back({node[i].t,i+1});
    else v2.push_back({node[i].t, i+1});
  }
  sort(ALL(v1)); sort(ALL(v2));
  FOR(i,0,rec) ans.push_back(v1[i].SE);
  FOR(i,0,m-rec) ans.push_back(v2[i].SE);
}

void cal2(LL rec){
#ifdef DEBUG
  printf("cal2 %lld\n",rec);
#endif
  VR<pii> c, a, b, left;
  FOR(i,0,n){
    Node &t = node[i];
    if(t.a && t.b) c.push_back({t.t, i+1});
    else if(t.a) a.push_back({t.t, i+1});
    else if(t.b) b.push_back({t.t, i+1});
    else left.push_back({t.t, i+1});
  }

  sort(ALL(a)); sort(ALL(b)); sort(ALL(c));
  FOR(i,0,rec) ans.push_back(c[i].SE);
  FOR(i,0,k-rec) ans.push_back(a[i].SE);
  FOR(i,0,k-rec) ans.push_back(b[i].SE);

  int nleft = m - (rec + k-rec + k-rec);
  FOR(i,k-rec,a.size()) left.push_back(a[i]);
  FOR(i,k-rec,b.size()) left.push_back(b[i]);
#ifdef DEBUG
  printf("  nleft: %d\n",nleft);
  for(auto p : left) printf(" [%d, %d]\n",p.FI,p.SE);
#endif
  sort(ALL(left));
  FOR(i,0,nleft) ans.push_back(left[i].SE);
}

int main(void){
  scanf("%d%d%d",&n,&m,&k);
  FOR(i,0,n){
    int t,ta,tb;
    scanf("%d%d%d",&t,&ta,&tb);
    node[i] = {t,ta,tb};

    if(ta && tb) c.push_back(t);
    else{
      d.push_back(t);
      if(ta) a.push_back(t);
      else if(tb) b.push_back(t);
      else e.push_back(t);
    }
  }
  sort(ALL(a)); sort(ALL(b)); sort(ALL(c)); sort(ALL(d)); sort(ALL(e));
#ifdef DEBUG
  printf("a:"); for(int x : a) printf(" %d",x); putchar('\n');
  printf("b:"); for(int x : b) printf(" %d",x); putchar('\n');
  printf("c:"); for(int x : c) printf(" %d",x); putchar('\n');
  printf("d:"); for(int x : d) printf(" %d",x); putchar('\n');
  printf("e:"); for(int x : e) printf(" %d",x); putchar('\n');
#endif

  VR<int> sa(a), sb(b), sc(c);
  FOR(i,1,a.size()) sa[i] += sa[i-1];
  FOR(i,1,b.size()) sb[i] += sb[i-1];
  FOR(i,1,c.size()) sc[i] += sc[i-1];
  FOR(i,1,d.size()) d[i] += d[i-1];
  FOR(i,1,e.size()) e[i] += e[i-1];

  LL ansMin = INF, rec = -1; sg.n = 1e4;
  if(c.size() >= k) FORR(i,k,min((int)c.size(),m)){
    LL sum = sc[i-1];
    if(i != m){
      if(d.size() < m-i) sum = INF;
      else sum += d[m-i-1];
    }
    if(sum < ansMin){
#ifdef DEBUG
      printf("c: %d, sum: %lld\n",i,sum);
#endif
      ansMin = sum; rec = i | (1ll<<60);
    }
  }

  bool first = true;
  for(int x : e) sg.add(x,1);
  FORR(i,0,min((int)c.size(), k-1)) if(a.size() >= k-i && b.size() >= k-i){
    if(first){
      FOR(j,k-i,a.size()) sg.add(a[j],1);
      FOR(j,k-i,b.size()) sg.add(b[j],1);
      first = false;
    }

    int left = m - (i + k-i + k-i);
    if(left < 0) continue;

    LL sum = i>0 ? sc[i-1] : 0;
    sum += sa[k-i-1] + sb[k-i-1] + sg.qMin(left);
    if(sum < ansMin){
#ifdef DEBUG
      printf("c: %d, sum: %lld\n",i,sum);
#endif
      ansMin = sum; rec = i;
    }

    sg.add(a[k-i-1],1), sg.add(b[k-i-1],1);
  }

  if(ansMin == INF) { printf("-1\n"); return 0; }
  if(rec & (1ll<<60)) cal1(rec - (1ll<<60));
  else cal2(rec);

  printf("%lld\n",ansMin);
  for(int x : ans) printf("%d ",x);
  putchar('\n');

  return 0;
}