Problem: Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.

Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.

There are n books in the family library. The i-th book is described by three integers: t_i — the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).

So they need to choose some books from the given n books in such a way that:

  * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; 
  * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). 



The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.

Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.

Input

The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 2 ⋅ 10^5).

The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≤ t_i ≤ 10^4, 0 ≤ a_i, b_i ≤ 1), where:

  * t_i — the amount of time required for reading the i-th book; 
  * a_i equals 1 if Alice likes the i-th book and 0 otherwise; 
  * b_i equals 1 if Bob likes the i-th book and 0 otherwise. 

Output

If there is no solution, print only one integer -1. Otherwise print one integer T — the minimum total reading time of the suitable set of books.

Examples

Input

8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0


Output

18


Input

5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0


Output

8


Input

5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1


Output

-1

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
const long long INF = 1000000000000000001;
using namespace std;
mt19937 random1(179);
struct Node {
  Node* l;
  Node* r;
  long long x, y, sum1, num, len;
  Node(long long x1)
      : x(x1), y(random1()), l(nullptr), r(nullptr), sum1(x1), num(1), len(1) {}
};
void update(Node* tree) {
  if (tree == nullptr) {
    return;
  }
  tree->sum1 = tree->x * tree->num, tree->len = tree->num;
  if (tree->l != nullptr) {
    tree->sum1 += tree->l->sum1, tree->len += tree->l->len;
  }
  if (tree->r != nullptr) {
    tree->sum1 += tree->r->sum1, tree->len += tree->r->len;
  }
}
Node* merge(Node* tree1, Node* tree2) {
  if (tree1 == nullptr) {
    return tree2;
  }
  if (tree2 == nullptr) {
    return tree1;
  }
  if (tree1->y < tree2->y) {
    tree1->r = merge(tree1->r, tree2);
    update(tree1);
    return tree1;
  } else {
    tree2->l = merge(tree1, tree2->l);
    update(tree2);
    return tree2;
  }
}
pair<Node*, Node*> split(Node* tree, long long x) {
  if (tree == nullptr) {
    return {nullptr, nullptr};
  }
  if (x == tree->x) {
    Node* right = tree->r;
    tree->r = nullptr;
    update(tree);
    return {tree, right};
  } else if (x < tree->x) {
    pair<Node*, Node*> tree1 = split(tree->l, x);
    tree->l = nullptr;
    update(tree);
    return {tree1.first, merge(tree1.second, tree)};
  } else {
    pair<Node*, Node*> tree1 = split(tree->r, x);
    tree->r = nullptr;
    update(tree);
    return {merge(tree, tree1.first), tree1.second};
  }
}
long long sum_k(Node* tree, long long k) {
  if (tree == nullptr) {
    return 0;
  }
  long long now = (tree->l == nullptr ? 0 : tree->l->len),
            sum1 = (tree->l == nullptr ? 0 : tree->l->sum1);
  if (now <= k && now + tree->num >= k) {
    return sum1 + tree->x * (k - now);
  } else if (now < k) {
    return sum_k(tree->r, k - now - tree->num) + sum1 + tree->x * tree->num;
  } else {
    return sum_k(tree->l, k);
  }
}
Node* add(Node* tree, long long x) {
  pair<Node*, Node*> tree1 = split(tree, x);
  pair<Node*, Node*> tree2 = split(tree1.first, x - 1);
  if (tree2.second == nullptr) {
    Node* root = new Node(x);
    return merge(merge(tree2.first, root), tree1.second);
  } else {
    tree2.second->num++;
    update(tree2.second);
    return merge(merge(tree2.first, tree2.second), tree1.second);
  }
}
Node* del(Node* tree, long long x) {
  pair<Node*, Node*> tree1 = split(tree, x);
  pair<Node*, Node*> tree2 = split(tree1.first, x - 1);
  if (tree2.second == nullptr || tree2.second->num == 1) {
    return merge(tree2.first, tree1.second);
  } else {
    tree2.second->num--;
    update(tree2.second);
    return merge(merge(tree2.first, tree2.second), tree1.second);
  }
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  long long n, m, k;
  cin >> n >> m >> k;
  vector<pair<long long, long long> > s00, s01, s10, s11;
  vector<long long> pref01 = {0}, pref10 = {0}, pref11 = {0};
  for (long long q = 0; q < n; q++) {
    long long t, x, y;
    cin >> t >> x >> y;
    if (x == 0 && y == 0) {
      s00.push_back({t, q});
    } else if (x == 0) {
      s01.push_back({t, q});
    } else if (y == 0) {
      s10.push_back({t, q});
    } else {
      s11.push_back({t, q});
    }
  }
  sort(s00.begin(), s00.end());
  sort(s01.begin(), s01.end());
  sort(s10.begin(), s10.end());
  sort(s11.begin(), s11.end());
  Node* DD = nullptr;
  for (pair<long long, long long> q : s00) {
    DD = add(DD, q.first);
  }
  for (pair<long long, long long> q : s01) {
    pref01.push_back(pref01.back() + q.first);
    DD = add(DD, q.first);
  }
  for (pair<long long, long long> q : s10) {
    pref10.push_back(pref10.back() + q.first);
    DD = add(DD, q.first);
  }
  for (pair<long long, long long> q : s11) {
    pref11.push_back(pref11.back() + q.first);
  }
  long long ans = INF, ind = -1;
  for (long long q1 = m; q1 > -1; q1--) {
    if (q1 <= s11.size() &&
        ((DD != nullptr && DD->len >= m - q1 - 2 * max(0LL, k - q1)) ||
         m == q1 + 2 * max(0LL, k - q1)) &&
        m >= q1 + 2 * max(0LL, k - q1)) {
      long long w = pref11[q1] + pref01[max(0LL, k - q1)] +
                    pref10[max(0LL, k - q1)] +
                    sum_k(DD, m - q1 - 2 * max(0LL, k - q1));
      if (w < ans) {
        ans = w, ind = q1;
      }
    }
    if (k - q1 + 1 > min(s01.size(), s10.size())) {
      break;
    }
    if (q1 <= k) {
      DD = del(DD, s01[k - q1].first);
      DD = del(DD, s10[k - q1].first);
    }
  }
  if (ans == INF) {
    cout << -1 << '\n';
  } else {
    cout << ans << '\n';
    for (long long q = 0; q < ind; q++) {
      cout << s11[q].second + 1 << ' ';
    }
    for (long long q = 0; q < max(0LL, k - ind); q++) {
      cout << s01[q].second + 1 << ' ' << s10[q].second + 1 << ' ';
    }
    long long q00 = 0, q01 = max(0LL, k - ind), q10 = max(0LL, k - ind);
    for (long long q = 0; q < m - ind - 2 * max(0LL, k - ind); q++) {
      if (q00 != s00.size() &&
          (q01 == s01.size() || s01[q01].first >= s00[q00].first) &&
          (q10 == s10.size() || s10[q10].first >= s00[q00].first)) {
        cout << s00[q00++].second + 1 << ' ';
      } else if (q01 != s01.size() &&
                 (q00 == s00.size() || s00[q00].first >= s01[q01].first) &&
                 (q10 == s10.size() || s10[q10].first >= s01[q01].first)) {
        cout << s01[q01++].second + 1 << ' ';
      } else {
        cout << s10[q10++].second + 1 << ' ';
      }
    }
    cout << '\n';
  }
  return 0;
}