text: 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 #include <bits/stdc++.h> using namespace std; template <typename T> using minpq = priority_queue<T, vector<T>, greater<T>>; int n, k, a, b; long long t; vector<long long> ve[2][2]; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> k; for (int i = (0); i < (n); i++) { cin >> t >> a >> b; ve[a][b].push_back(t); } sort((ve[0][1]).begin(), (ve[0][1]).end()); sort((ve[1][0]).begin(), (ve[1][0]).end()); vector<long long> sums; for (long long x : ve[1][1]) sums.push_back(x); for (int i = 0; i < min(((int)(ve[0][1]).size()), ((int)(ve[1][0]).size())); i++) { sums.push_back(ve[0][1][i] + ve[1][0][i]); } sort((sums).begin(), (sums).end()); if (((int)(sums).size()) < k) { cout << "-1\n"; return 0; } cout << accumulate(sums.begin(), sums.begin() + k, 0LL) << '\n'; }