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