text
stringlengths
49
983k
#include <bits/stdc++.h> using namespace std; long long n, k, t, a, b; vector<long long> v, va, vb; int main() { cin >> n >> k; for (long long i = 0; i < n; i++) { cin >> t >> a >> b; if (a == 1 && b == 1) v.push_back(t); else if (a == 1 && b == 0) va.push_back(t); else if (a == 0 && b == 1) vb.push_back(t); } sort(v.begin(), v.end()); sort(va.begin(), va.end()); sort(vb.begin(), vb.end()); long long zx = min(va.size(), vb.size()); if ((v.size() + zx) < k) { cout << -1 << "\n"; return 0; } for (long long i = 1; i < v.size(); i++) v[i] += v[i - 1]; for (long long i = 1; i < zx; i++) { va[i] += va[i - 1]; vb[i] += vb[i - 1]; } long long minn = 1000000000001; if (v.size() >= k) minn = min(minn, v[k - 1]); for (long long i = 0; i < v.size(); i++) { long long p = k - (i + 1); if (zx >= p && p > 0) { long long cnt = v[i] + va[p - 1] + vb[p - 1]; minn = min(minn, cnt); } } if (zx >= k) minn = min(minn, va[k - 1] + vb[k - 1]); cout << minn << "\n"; }
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long INF = 1e9 + 9; const int MAX = 100; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout.precision(10); cout << fixed; long long t = 1; while (t--) { long long n, k; cin >> n >> k; vector<pair<int, int> > vp1; vector<pair<int, int> > vp2; vector<int> v; for (int i = 0; i < n; i++) { int a, b, c; cin >> a >> b >> c; if (b && !c) vp1.push_back({a, b}); if (c && !b) vp2.push_back({a, c}); if (b && c) v.push_back(a); } sort(v.begin(), v.end()); long long ans = 0; sort(vp1.begin(), vp1.end()); sort(vp2.begin(), vp2.end()); int i = 0, j = 0; while (k > 0) { if (i == vp1.size() || i == vp2.size()) { break; } if (j == v.size()) { break; } if (v[j] <= vp1[i].first + vp2[i].first) { ans += v[j]; j++; k--; } else { ans += vp1[i].first + vp2[i].first; i++; k--; } } while (i < vp1.size() && k > 0 && i < vp2.size()) { ans += vp1[i].first + vp2[i].first; k--; i++; } while (j < v.size() && k > 0) { ans += v[j]; k--; j++; } if (k > 0) { cout << "-1" << '\n'; continue; } cout << ans << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 100; const int inf = 1e9 + 7; const long long mod = 1e9 + 7; int a[maxn]; int b[maxn]; int t[maxn]; vector<int> both; vector<int> alice; vector<int> bob; int psum_alice[maxn]; int psum_bob[maxn]; int psum_both[maxn]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> t[i] >> a[i] >> b[i]; if (a[i] == 1 && b[i] == 1) { both.push_back(t[i]); } else if (a[i] == 1) { alice.push_back(t[i]); } else if (b[i] == 1) { bob.push_back(t[i]); } } sort(both.begin(), both.end()); sort(alice.begin(), alice.end()); sort(bob.begin(), bob.end()); for (int i = 0; i < both.size(); i++) { psum_both[i + 1] = psum_both[i] + both[i]; } for (int i = 0; i < alice.size(); i++) { psum_alice[i + 1] = psum_alice[i] + alice[i]; } for (int i = 0; i < bob.size(); i++) { psum_bob[i + 1] = psum_bob[i] + bob[i]; } long long ans = -1; for (int i = 0; i <= k; i++) { if (i > both.size() || k - i > alice.size() || k - i > bob.size()) { continue; } long long subans = psum_both[i] + psum_alice[k - i] + psum_bob[k - i]; if (ans == -1 || subans < ans) { ans = subans; } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int s[200000], m[200000], n[200000]; int main() { long long ans = 0; int f, k, t[200000], a[200000], b[200000], i, count = 0; int g = 0, h = 0; cin >> f >> k; for (i = 0; i < f; i++) { cin >> t[i] >> a[i] >> b[i]; if (a[i] == 1 && b[i] == 1) { s[count] = t[i]; count++; } if (a[i] == 0 && b[i] == 1) { n[h] = t[i]; h++; } if (a[i] == 1 && b[i] == 0) { m[g] = t[i]; g++; } } int l = min(g, h); sort(n, n + h); sort(m, m + g); for (i = count; i < (count + l); i++) s[i] = n[i - count] + m[i - count]; sort(s, s + count + l); if (count + l < k) cout << -1; else { for (i = 0; i < k; i++) ans += s[i]; cout << ans; } return 0; }
#include <bits/stdc++.h> using namespace std; int32_t main() { ios::sync_with_stdio(false); cin.tie(nullptr); long long t = 1; while (t--) { long long n, k; ; cin >> n >> k; multiset<long long> alice, bob, both; for (long long i = 0; i < n; i++) { long long ti, ai, bi; cin >> ti >> ai >> bi; if (ai == 1 && bi == 1) both.insert(ti); else if (ai == 1) alice.insert(ti); else if (bi == 1) bob.insert(ti); } long long ans = 0; while (k > 0) { if (!alice.empty() && !bob.empty() && !both.empty()) { long long a = *alice.begin(); long long b = *bob.begin(); long long c = *both.begin(); if (a + b < c) { ans += a + b; alice.erase(alice.find(a)); bob.erase(bob.find(b)); } else { ans += c; both.erase(both.find(c)); } } else if (!both.empty()) { long long x = *both.begin(); ans += x; both.erase(both.find(x)); } else if (!alice.empty() && !bob.empty()) { long long a = *alice.begin(); long long b = *bob.begin(); ans += a + b; alice.erase(alice.find(a)); bob.erase(bob.find(b)); } else { ans = -1; break; } k--; } cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; vector<pair<long long, long long> > al, bob, both, v, ansb, alr, bobr, norr, vc, neither; set<long long> ans; int vis[200005], l1[200005], l2[200005]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long a = 0, b = 0, c, d, e, f = 0, g, m, n, k, i, j, t, in, p, q, l, r; cin >> n >> m >> k; vc.push_back({0, 0}); for (i = 0; i < n; i++) { cin >> p >> a >> b; l1[i + 1] = a; l2[i + 1] = b; v.push_back({p, i + 1}); vc.push_back({p, i + 1}); if (a && b) { both.push_back({p, i + 1}); } else if (a) al.push_back({p, i + 1}); else if (b) bob.push_back({p, i + 1}); } sort(both.begin(), both.end()); sort(bob.begin(), bob.end()); sort(al.begin(), al.end()); sort(v.begin(), v.end()); if (both.size() + bob.size() < k || both.size() + al.size() < k) { cout << -1 << '\n'; return 0; } for (i = 1; i < both.size(); i++) { both[i].first += both[i - 1].first; } for (i = 1; i < bob.size(); i++) { bob[i].first += bob[i - 1].first; } for (i = 1; i < al.size(); i++) { al[i].first += al[i - 1].first; } if (al.size() >= k && bob.size() >= k && m >= k * 2 && both.size() == 0) { f = al[k - 1].first + bob[k - 1].first; p = k; q = k; r = 0; } else { p = 0; q = 0; r = 0; f = 9999999999999999; } l = both.size(); l = min(l, m); for (i = l - 1; i >= l - 1 && i >= 0; i--) { d = max(0LL, k - i - 1); if (d > 0 && (d > al.size() || d > bob.size())) break; if (d * 2 + i + 1 > m) break; c = both[i].first; if (d > 0) c += al[d - 1].first + bob[d - 1].first; f = c; p = max(0LL, d); q = max(0LL, d); r = i + 1; } if (f == 9999999999999999) { cout << -1 << '\n'; return 0; } for (i = 0; i < p; i++) { ans.insert(al[i].second); vis[al[i].second] = 1; } for (i = 0; i < q; i++) { ans.insert(bob[i].second); vis[bob[i].second] = 1; } for (i = 0; i < r; i++) { ans.insert(both[i].second); ansb.push_back(vc[both[i].second]); vis[both[i].second] = 1; } i = 0; while (ans.size() < m) { if (vis[v[i].second]) { i++; continue; } else { f += v[i].first; ans.insert(v[i].second); vis[v[i].second] = 1; if (l1[v[i].second] && l2[v[i].second]) ansb.push_back(v[i]); i++; } } sort(ansb.begin(), ansb.end()); for (i = 0; i < n; i++) { if (!vis[v[i].second]) { p = v[i].second; if (l1[p] && l2[p]) ; else if (l1[p]) alr.push_back(v[i]); else if (l2[p]) bobr.push_back(v[i]); else norr.push_back(v[i]); } else if (!l1[v[i].second] && !l2[v[i].second]) { neither.push_back(v[i]); } } sort(alr.begin(), alr.end()); reverse(alr.begin(), alr.end()); sort(bobr.begin(), bobr.end()); reverse(bobr.begin(), bobr.end()); sort(norr.begin(), norr.end()); reverse(norr.begin(), norr.end()); sort(neither.begin(), neither.end()); p = 0; q = 0; for (auto it : ans) { if (l1[it]) p++; if (l2[it]) q++; } while (ansb.size() > 0) { if (p - 1 < k && q - 1 < k) { if (neither.size() == 0 || alr.size() == 0 || bobr.size() == 0) break; long long c1 = ansb.back().first + neither.back().first; long long c2 = alr.back().first + bobr.back().first; if (c2 < c1) { ans.erase(ansb.back().second); ans.erase(neither.back().second); norr.push_back(neither.back()); ansb.pop_back(); neither.pop_back(); f -= c1; f += c2; ans.insert(alr.back().second); ans.insert(bobr.back().second); alr.pop_back(); bobr.pop_back(); } else break; } else { c = ansb.back().first; in = ansb.back().second; long long mn = 99999999999; if (p - 1 < k) { if (!alr.size()) break; mn = alr.back().first; if (mn < c) { q--; ans.erase(in); ans.insert(alr.back().second); alr.pop_back(); ansb.pop_back(); f -= c; f += mn; } else break; } else if (q - 1 < k) { if (!bobr.size()) break; mn = bobr.back().first; if (mn < c) { p--; ans.erase(in); ans.insert(bobr.back().second); bobr.pop_back(); f -= c; f += mn; ansb.pop_back(); } else break; } else { mn = c; int fl = 0; if (alr.size() && alr.back().first < mn) { mn = alr.back().first; fl = 1; } if (bobr.size() && bobr.back().first < mn) { mn = bobr.back().first; fl = 2; } if (norr.size() && norr.back().first < mn) { mn = norr.back().first; fl = 3; } if (fl == 0) break; else if (fl == 1) { q--; ans.erase(in); ans.insert(alr.back().second); alr.pop_back(); ansb.pop_back(); f -= c; f += mn; } else if (fl == 2) { p--; ans.erase(in); ans.insert(bobr.back().second); bobr.pop_back(); f -= c; f += mn; ansb.pop_back(); } else { p--; q--; ans.erase(in); ans.insert(norr.back().second); neither.push_back(norr.back()); norr.pop_back(); f -= c; f += mn; ansb.pop_back(); } } } } cout << f << '\n'; for (auto it : ans) cout << it << ' '; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 200005; struct P { int t, a, b; }; vector<P> A, B, C; int sa[N], sb[N], sc[N]; bool cmp(P a, P b) { return a.t < b.t; } int main() { int n, k; P t; scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) { scanf("%d%d%d", &t.t, &t.a, &t.b); if (t.a && t.b) { C.push_back(t); } else if (t.a) { A.push_back(t); } else if (t.b) { B.push_back(t); } } if (A.size() + C.size() < k || B.size() + C.size() < k) { printf("-1\n"); return 0; } sort(A.begin(), A.end(), cmp); sort(B.begin(), B.end(), cmp); sort(C.begin(), C.end(), cmp); if (A.size()) sa[1] = A[0].t; for (int i = 1; i < A.size(); ++i) { sa[i + 1] = sa[i] + A[i].t; } if (B.size()) sb[1] = B[0].t; for (int i = 1; i < B.size(); ++i) { sb[i + 1] = sb[i] + B[i].t; } if (C.size()) sc[1] = C[0].t; for (int i = 1; i < C.size(); ++i) { sc[i + 1] = sc[i] + C[i].t; } int Min = 0x7fffffff; for (int i = 0; i <= C.size(); ++i) { if (k - i > A.size() || k - i > B.size()) continue; if (sc[i] + sa[k - i] + sb[k - i] < Min) Min = sc[i] + sa[k - i] + sb[k - i]; } printf("%d\n", Min); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 5; struct st { int t, a, b; }; deque<st> v, al, bo, ne; int n, k; st st1; bool cmp(st stx, st sty) { if (stx.a + stx.b > sty.a + sty.b) return 1; else if (stx.a + stx.b < sty.a + sty.b) return 0; else return (stx.t < sty.t); } int main() { scanf("%d %d", &n, &k); while (n--) { scanf("%d %d %d", &st1.t, &st1.a, &st1.b); if (st1.a && st1.b) v.push_back(st1); else if (st1.a) al.push_back(st1); else if (st1.b) bo.push_back(st1); } sort(v.begin(), v.end(), cmp); sort(al.begin(), al.end(), cmp); sort(bo.begin(), bo.end(), cmp); int kx = 0, total = 0; while (kx < k) { if ((!v.empty() && !al.empty() && !bo.empty()) && al.begin()->t + bo.begin()->t < v.begin()->t) { total += al.begin()->t + bo.begin()->t; kx++; al.pop_front(); bo.pop_front(); } else if (!v.empty()) { total += v.begin()->t; kx++; v.pop_front(); } else if (!al.empty() && !bo.empty()) { total += al.begin()->t + bo.begin()->t; kx++; al.pop_front(); bo.pop_front(); } else break; } if (k == kx) cout << total; else cout << -1; return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const long long INF = 0x3f3f3f3f3f3f3f3fll; const int N = 1e4 + 10; const int M = 3e6 + 10; const int maxn = 1e6 + 10; const int mod = 1000173169; const double eps = 1e-10; const double pi = acos(-1.0); struct Tree { int num[N << 2], sum[N << 2]; void add(int rt, int l, int r, int p) { num[rt]++; sum[rt] += p; if (l == r) return; int mid = ((l + r) >> 1); if (p <= mid) add(rt << 1, l, mid, p); else add(rt << 1 | 1, mid + 1, r, p); } int getPos(int rt, int l, int r, int& val) { if (l == r) return l; int mid = ((l + r) >> 1); if (num[rt << 1] >= val) return getPos(rt << 1, l, mid, val); else { val -= num[rt << 1]; return getPos(rt << 1 | 1, mid + 1, r, val); } } int getSum(int rt, int l, int r, int pos) { if (r <= pos) return sum[rt]; int mid = ((l + r) >> 1); int res = 0; if (l <= pos) res = getSum(rt << 1, l, mid, pos); if (mid < pos) res += getSum(rt << 1 | 1, mid + 1, r, pos); return res; } } seg; int n, m, k; vector<int> id; vector<pair<int, int> > a[3], b, p, res; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m >> k; int mx = 1e4; for (int i = 1; i <= n; i++) { int t, x, y; cin >> t >> x >> y; x = x << 1 | y; if (x == 3) b.emplace_back(t, i); else if (x) a[x].emplace_back(t, i); else { seg.add(1, 1, mx, t); res.emplace_back(t, i); } } sort(a[1].begin(), a[1].end()); sort(a[2].begin(), a[2].end()); sort(b.begin(), b.end()); int cnt1 = 0, cnt2 = 0; int t1 = min(min(((int)a[1].size()), ((int)a[2].size())), k); int cur = 0; for (int i = 0; i < t1; i++) { cur += a[1][i].first + a[2][i].first; p.emplace_back(a[1][i].first, a[2][i].first); cnt1++; cnt2 += 2; } for (int i = t1; i < max(((int)a[1].size()), ((int)a[2].size())); i++) { if (i < ((int)a[1].size())) seg.add(1, 1, mx, a[1][i].first), res.push_back(a[1][i]); if (i < ((int)a[2].size())) seg.add(1, 1, mx, a[2][i].first), res.push_back(a[2][i]); } for (int i = k; i < ((int)b.size()); i++) { seg.add(1, 1, mx, b[i].first); res.push_back(b[i]); } int P = -1; long long ans = INF; int t2 = min(((int)b.size()), k); for (int i = 0; i <= t2; i++) { if (cnt1 == k && cnt2 <= m) { long long res = INF; if (cnt2 == m) res = cur; else if (cnt2 + seg.num[1] >= m) { int val = m - cnt2; int pos = seg.getPos(1, 1, mx, val); res = pos * val + cur; if (pos > 1) res += seg.getSum(1, 1, mx, pos - 1); } if (ans > res) { ans = res; P = i; } } if (i < t2) { if (cnt1 < k) { cur += b[i].first; cnt1++; cnt2++; } else if (!p.empty()) { pair<int, int> t = p.back(); p.pop_back(); cur -= t.first + t.second; seg.add(1, 1, mx, t.first); seg.add(1, 1, mx, t.second); cur += b[i].first; cnt2--; } } } if (ans == INF) cout << "-1\n"; else { cout << ans << '\n'; for (int i = 0; i < P; i++) id.push_back(b[i].second); for (int i = 0; i < t1; i++) { if (P + i + 1 <= k) { id.push_back(a[1][i].second); id.push_back(a[2][i].second); } else { res.push_back(a[1][i]); res.push_back(a[2][i]); } } sort(res.begin(), res.end()); for (int i = 0; i < ((int)res.size()); i++) { if (((int)id.size()) == m) break; id.push_back(res[i].second); } for (int v : id) cout << v << ' '; cout << '\n'; assert(((int)id.size()) == m); } return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") using namespace std; long long dx[] = {1, 0, -1, 0}; long long dy[] = {0, 1, 0, -1}; void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { long long f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } long long solve() { long long n, k; cin >> n >> k; vector<pair<long long, pair<long long, long long>>> v; long long alice = 0, bob = 0, ans = 0; multiset<long long> both, al, bo; for (long long i = 0; i < n; i++) { long long time, a, b; cin >> time >> a >> b; if (a && !b) al.insert(time); if (!a && b) bo.insert(time); else if (a && b) both.insert(time); v.push_back({time, {a, b}}); if (a == 1) alice++; if (b == 1) bob++; } if (alice < k || bob < k) return -1; while (al.size() && bo.size() && both.size() && k) { long long ali = *al.begin(); long long bobi = *bo.begin(); long long bot = *both.begin(); if (ali + bobi < bot) { al.erase(al.find(ali)); bo.erase(bo.find(bobi)); ans += ali + bobi; } else { both.erase(both.find(bot)); ans += bot; } k--; } while (k && al.size() && bo.size()) { k--; long long ali = *al.begin(); long long bobi = *bo.begin(); al.erase(al.find(ali)); bo.erase(bo.find(bobi)); ans += ali + bobi; } while (k && both.size()) { k--; long long bot = *both.begin(); both.erase(both.find(bot)); ans += bot; } return ans; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout << solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 2e9 + 1; int n, m, k, p1, p2, sum = 0, sumSmall = 0, minVal = INF; pair<int, int> rs; vector<pair<int, int> > a[2][2]; set<pair<int, int> > small, large; void updateSmall(pair<int, int> val, int type) { if (type == 0) { small.insert(val); sumSmall += val.first; } else { small.erase(small.find(val)); sumSmall -= val.first; } } void balance() { while (small.size() > m - p1 - 2 * p2 - 3) { large.insert(*small.rbegin()); updateSmall(*small.rbegin(), 1); } while (small.size() < m - p1 - 2 * p2 - 3) { updateSmall(*large.begin(), 0); large.erase(large.begin()); } while (small.size() > 0 && large.size() > 0) { pair<int, int> p1 = *small.rbegin(); pair<int, int> p2 = *large.begin(); if (p1.first <= p2.first) break; updateSmall(p1, 1); large.erase(large.find(p2)); updateSmall(p2, 0); large.insert(p1); } } void init() { p1 = min((int)a[1][1].size(), k) - 1, p2 = max(k - (int)a[1][1].size(), 0) - 1; for (int i = 0; i <= p1; i++) sum += a[1][1][i].first; for (int i = 0; i <= p2; i++) sum += a[0][1][i].first + a[1][0][i].first; for (int i = p1 + 1; i < a[1][1].size(); i++) updateSmall(a[1][1][i], 0); for (int i = p2 + 1; i < a[0][1].size(); i++) updateSmall(a[0][1][i], 0); for (int i = p2 + 1; i < a[1][0].size(); i++) updateSmall(a[1][0][i], 0); for (int i = 0; i < a[0][0].size(); i++) updateSmall(a[0][0][i], 0); balance(); minVal = sum + sumSmall; rs = pair<int, int>(p1, p2); } void process() { while (p1 >= 0) { updateSmall(a[1][1][p1], 0); sum -= a[1][1][p1--].first; p2++; if (p2 >= min(a[0][1].size(), a[1][0].size())) return; if (m - p1 - 2 * p2 - 3 < 0) return; sum += a[0][1][p2].first + a[1][0][p2].first; if (small.find(a[0][1][p2]) != small.end()) updateSmall(a[0][1][p2], 1); else large.erase(large.find(a[0][1][p2])); if (small.find(a[1][0][p2]) != small.end()) updateSmall(a[1][0][p2], 1); else large.erase(large.find(a[1][0][p2])); balance(); if (minVal > sum + sumSmall) { minVal = sum + sumSmall; rs = pair<int, int>(p1, p2); } } } void print() { cout << minVal << '\n'; for (int i = 0; i <= rs.first; i++) cout << a[1][1][i].second << ' '; for (int i = 0; i <= rs.second; i++) cout << a[0][1][i].second << ' ' << a[1][0][i].second << ' '; vector<pair<int, int> > comb; for (int i = 0; i < a[0][0].size(); i++) comb.push_back(a[0][0][i]); for (int i = rs.first + 1; i < a[1][1].size(); i++) comb.push_back(a[1][1][i]); for (int i = rs.second + 1; i < a[0][1].size(); i++) comb.push_back(a[0][1][i]); for (int i = rs.second + 1; i < a[1][0].size(); i++) comb.push_back(a[1][0][i]); sort(comb.begin(), comb.end()); int rem = m - rs.first - 2 * rs.second - 3; for (int i = 0; i < rem; i++) cout << comb[i].second << ' '; } int main() { cin >> n >> m >> k; int t, x, y; for (int i = 1; i <= n; i++) { cin >> t >> x >> y; a[x][y].push_back(pair<int, int>(t, i)); } for (int i = 0; i <= 1; i++) for (int j = 0; j <= 1; j++) sort(a[i][j].begin(), a[i][j].end()); if (a[1][1].size() + min(a[1][0].size(), a[0][1].size()) < k || min((int)a[1][1].size(), m) + 2 * max(k - (int)a[1][1].size(), 0) > m) { cout << -1; return 0; } init(); process(); print(); }
#include <bits/stdc++.h> using namespace std; void solve() { int n, k; cin >> n >> k; priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > pq; vector<int> t(n), a(n), b(n); int cnt1 = 0, cnt2 = 0; int cnt3 = 0; priority_queue<int, vector<int>, greater<int> > pqboth; for (int i = 0; i < n; i++) { cin >> t[i] >> a[i] >> b[i]; if (a[i] != 0 || b[i] != 0) pq.push({t[i], i}); if (a[i] == 1 && b[i] == 1) { pqboth.push(t[i]); cnt3++; } if (a[i] == 1) cnt1++; if (b[i] == 1) cnt2++; } if (cnt1 < k || cnt2 < k) { cout << "-1\n"; return; } cnt1 = 0, cnt2 = 0; long long int time = 0; priority_queue<int> pq3, pq4; int cnt4 = 0; while (!pq.empty() && (cnt1 < k || cnt2 < k)) { int x = pq.top().first; int y = pq.top().second; time += (long long int)x; if (a[y] == 1 && b[y] == 1) cnt4++; if (a[y] == 1) cnt1++; if (b[y] == 1) cnt2++; if (a[y] == 1 && b[y] == 0) { pq3.push(x); } if (a[y] == 0 && b[y] == 1) { pq4.push(x); } pq.pop(); } while (cnt4 > 0 && !pqboth.empty()) { cnt4--; pqboth.pop(); } while (cnt1 > k && !pq3.empty()) { cnt1--; time -= (long long int)pq3.top(); pq3.pop(); } while (cnt2 > k && !pq4.empty()) { cnt2--; time -= (long long int)pq4.top(); pq4.pop(); } while (!pqboth.empty() && !pq3.empty() && !pq4.empty()) { int x = pqboth.top(); int y = pq3.top(); int z = pq4.top(); if (x < y + z) time += (long long int)(x - y - z); else break; pqboth.pop(); pq3.pop(); pq4.pop(); } cout << time << "\n"; } int main() { solve(); return 0; }
#include <bits/stdc++.h> using namespace std; struct cmp { bool operator()(const long long &a, const long long &b) { return a >= b; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, k; cin >> n >> k; long long first = 0, second = 0, ans = 0; long long temp1 = 0, temp2 = 0, temp3 = 0; priority_queue<long long, vector<long long>, cmp> a; priority_queue<long long, vector<long long>, cmp> b; priority_queue<long long, vector<long long>, cmp> c; for (int i = 0; i < n; i++) { cin >> temp1 >> temp2 >> temp3; if (temp2 != 0 && temp3 == 0) a.push(temp1); else if (temp2 == 0 && temp3 != 0) b.push(temp1); else if (temp2 == 1 && temp3 == 1) c.push(temp1); } if (a.size() + c.size() < k || b.size() + c.size() < k) { cout << -1; return 0; } long long ran = min(a.size(), b.size()); while (ran--) { c.push(a.top() + b.top()); a.pop(); b.pop(); } while (k--) { ans += c.top(); c.pop(); } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int mod = 1000000007; const int inf = 1034567891; const long long LL_INF = 1234567890123456789ll; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << '\n'; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } template <typename T> T GCD(T a, T b) { long long t; while (a) { t = a; a = b % a; b = t; } return b; } template <typename T> string toString(T a) { return to_string(a); } template <typename T> void toInt(string s, T& x) { stringstream str(s); str >> x; } inline int add(int x, int y) { x += y; if (x >= mod) x -= mod; return x; } inline int sub(int x, int y) { x -= y; if (x < 0) x += mod; return x; } inline int mul(int x, int y) { return (x * 1ll * y) % mod; } inline int powr(int a, long long b) { int x = 1 % mod; while (b) { if (b & 1) x = mul(x, a); a = mul(a, a); b >>= 1; } return x; } inline int inv(int a) { return powr(a, mod - 2); } const int N = 2e5 + 5; int tt[N], aa[N], bb[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, k; cin >> n >> m >> k; vector<pair<long long, long long> > vec[4]; for (int i = 0; i < 4; i++) vec[i].push_back({0, 0}); for (int i = 0; i < n; i++) { cin >> tt[i] >> aa[i] >> bb[i]; int x = aa[i] * 2 + bb[i]; vec[x].push_back({tt[i], i + 1}); } int a = vec[2].size() - 1, b = vec[1].size() - 1, ab = vec[3].size() - 1, d = vec[0].size() - 1; if (a + ab < k || b + ab < k) { cout << -1 << '\n'; return 0; } for (int i = 0; i < 4; i++) { sort(vec[i].begin(), vec[i].end()); } for (int i = 1; i <= a; i++) { vec[2][i].first += vec[2][i - 1].first; } for (int i = 1; i <= b; i++) { vec[1][i].first += vec[1][i - 1].first; } for (int i = 1; i <= ab; i++) { vec[3][i].first += vec[3][i - 1].first; } set<pair<long long, long long> > s; for (int i = 1; i <= d; i++) { s.insert({vec[0][i].first, vec[0][i].second}); } for (int i = 1; i <= ab; i++) { s.insert({vec[3][i].first - vec[3][i - 1].first, vec[3][i].second}); } long long sum = 0; for (auto it : s) { sum += it.first; } long long ans = LL_INF, pos = 0, p1 = 0, p2 = 0; int id1 = a, id2 = b; set<pair<long long, long long> > temp; bool first = false; set<pair<long long, long long> > ready; for (int i = 0; i <= ab; i++) { int x = k - i; int y = m - i - 2 * x; if (i) { auto it = make_pair(vec[3][i].first - vec[3][i - 1].first, vec[3][i].second); ready.erase(it); if (s.count(it)) { sum -= it.first; s.erase(it); } } if (y < 0 || x < 0 || x > min(a, b)) continue; if (!first) { for (int j = x + 1; j <= a; j++) { sum += vec[2][j].first - vec[2][j - 1].first; s.insert({vec[2][j].first - vec[2][j - 1].first, vec[2][j].second}); } for (int j = x + 1; j <= b; j++) { sum += vec[1][j].first - vec[1][j - 1].first; s.insert({vec[1][j].first - vec[1][j - 1].first, vec[1][j].second}); } } first = true; while (s.size() && s.size() > y) { ready.insert(*s.rbegin()); sum -= (*s.rbegin()).first; s.erase(*s.rbegin()); } while (ready.size() && s.size() < y) { auto it = *ready.begin(); sum += it.first; s.insert(it); ready.erase(it); } while (ready.size() && s.size() && (*ready.begin()).first < (*s.rbegin()).first) { auto it1 = *ready.begin(); auto it2 = *s.rbegin(); sum -= it2.first; sum += it1.first; s.erase(it2); ready.erase(it1); s.insert(it1); ready.insert(it2); } long long cur = vec[3][i].first + vec[2][x].first + vec[1][x].first + sum; if (s.size() == y) { if (cur < ans) { ans = cur; pos = i; p1 = x; p2 = x; } } if (x > 0 && x <= a) { sum += vec[2][x].first - vec[2][x - 1].first; s.insert({vec[2][x].first - vec[2][x - 1].first, vec[2][x].second}); } if (x > 0 && x <= b) { sum += vec[1][x].first - vec[1][x - 1].first; s.insert({vec[1][x].first - vec[1][x - 1].first, vec[1][x].second}); } } if (ans == LL_INF) { cout << -1 << '\n'; return 0; } cout << ans << '\n'; for (int i = 1; i <= pos; i++) { cout << vec[3][i].second << " "; } for (int i = 1; i <= p2; i++) { cout << vec[1][i].second << " "; } for (int i = 1; i <= p1; i++) { cout << vec[2][i].second << " "; } s.clear(); ready.clear(); for (int i = 1; i <= d; i++) { s.insert({vec[0][i].first, vec[0][i].second}); } for (int i = 1; i <= ab; i++) { s.insert({vec[3][i].first - vec[3][i - 1].first, vec[3][i].second}); } sum = 0; for (auto it : s) { sum += it.first; } first = false; for (int i = 0; i <= ab; i++) { int x = k - i; int y = m - i - 2 * x; if (i) { auto it = make_pair(vec[3][i].first - vec[3][i - 1].first, vec[3][i].second); ready.erase(it); if (s.count(it)) { sum -= it.first; s.erase(it); } } if (y < 0 || x < 0 || x > min(a, b)) continue; if (!first) { for (int j = x + 1; j <= a; j++) { sum += vec[2][j].first - vec[2][j - 1].first; s.insert({vec[2][j].first - vec[2][j - 1].first, vec[2][j].second}); } for (int j = x + 1; j <= b; j++) { sum += vec[1][j].first - vec[1][j - 1].first; s.insert({vec[1][j].first - vec[1][j - 1].first, vec[1][j].second}); } } first = true; while (s.size() && s.size() > y) { ready.insert(*s.rbegin()); sum -= (*s.rbegin()).first; s.erase(*s.rbegin()); } while (ready.size() && s.size() < y) { auto it = *ready.begin(); sum += it.first; s.insert(it); ready.erase(it); } while (ready.size() && s.size() && (*ready.begin()).first < (*s.rbegin()).first) { auto it1 = *ready.begin(); auto it2 = *s.rbegin(); sum -= it2.first; sum += it1.first; s.erase(it2); ready.erase(it1); s.insert(it1); ready.insert(it2); } long long cur = vec[3][i].first + vec[2][x].first + vec[1][x].first + sum; if (i == pos) { for (auto it : s) { cout << it.second << " "; } cout << '\n'; return 0; } if (x > 0 && x <= a) { sum += vec[2][x].first - vec[2][x - 1].first; s.insert({vec[2][x].first - vec[2][x - 1].first, vec[2][x].second}); } if (x > 0 && x <= b) { sum += vec[1][x].first - vec[1][x - 1].first; s.insert({vec[1][x].first - vec[1][x - 1].first, vec[1][x].second}); } } cout << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, k; cin >> n >> k; vector<vector<long long>> b(4, vector<long long>(1, 0LL)); for (int i = 0; i < n; i++) { long long t, x, y; cin >> t >> x >> y; b[x * 2 + y].push_back(t); } for (int i = 1; i < 4; i++) { sort(b[i].begin(), b[i].end()); long long acc = 0LL; for (auto &j : b[i]) { long long x = j; j += acc; acc += x; } } long long ans = (k < (int)b[1].size() && k < (int)b[2].size()) ? b[1][k] + b[2][k] : LONG_LONG_MAX; for (int i = 1; i < (int)b[3].size() && k - i >= 0; i++) { if (k - i == 0) ans = min(ans, b[3][i]); else if (k - i > 0) { if (k - i > (int)b[1].size() - 1 || k - i > (int)b[2].size() - 1) continue; else ans = min(ans, b[3][i] + b[1][k - i] + b[2][k - i]); } } if (ans == LONG_LONG_MAX) cout << -1 << "\n"; else cout << ans << "\n"; }
#include <bits/stdc++.h> using namespace std; struct book { int val; int index; }; int n, m, k, tim, minTim = 2000000001; int minIndexVal, xIndex[5]; vector<book> x[4]; int xS[5]; vector<int> books; int currNum[4], minNum[4]; bool compareVal(book p1, book p2) { return p1.val < p2.val; } int sizeOfCurrBooks() { int sum = 0; for (int i = 0; i < 4; i++) sum += currNum[i]; return sum; } void removeMost(int l, int r) { int maxIndex = -1, maxVal = -1; for (int i = 0; i < 4; i++) { if (currNum[i] == 0) continue; if ((i == 1 || i == 2) && currNum[i] <= l) continue; if (i == 3 && currNum[i] <= r) continue; if (x[i][currNum[i] - 1].val > maxVal) { maxVal = x[i][currNum[i] - 1].val; maxIndex = i; } } tim -= maxVal; currNum[maxIndex]--; } void addLeast() { int minIndex = -1, minVal = 2000000001; for (int i = 0; i < 4; i++) { if (x[i][currNum[i]].val != 2000000001 && x[i][currNum[i]].val < minVal) { minVal = x[i][currNum[i]].val; minIndex = i; } } tim += minVal; currNum[minIndex]++; } void goToPoss(int l, int r) { if (currNum[3] >= r) { removeMost(l, r); removeMost(l, r); addLeast(); addLeast(); } else { tim += x[3][currNum[3]].val; currNum[3]++; removeMost(l, r); removeMost(l, r); addLeast(); } } int main() { cin >> n >> m >> k; int t, a, b; for (int i = 0; i < n; i++) { cin >> t >> a >> b; x[2 * a + b].push_back({t, i}); } for (int i = 0; i < 4; i++) { sort(x[i].begin(), x[i].end(), compareVal); xS[i] = (int)x[i].size(); x[i].push_back({2000000001, -1}); } xS[4] = min(xS[1], xS[2]); if (xS[3] + xS[4] < k || xS[3] + 2 * (k - xS[3]) > m) cout << -1 << endl; else { currNum[0] = 0; currNum[1] = m - k; currNum[2] = m - k; currNum[3] = 2 * k - m; if (xS[4] < m - k) { currNum[1] = xS[4]; currNum[2] = xS[4]; currNum[3] = k - xS[4]; } while (currNum[3] < 0) { currNum[1]--; currNum[2]--; currNum[3]++; } int left = currNum[1], right = currNum[3]; while (sizeOfCurrBooks() < m) { addLeast(); } tim = 0; for (int i = 0; i < 4; i++) for (int j = 0; j < currNum[i]; j++) tim += x[i][j].val; minTim = tim; for (int i = 0; i < 4; i++) minNum[i] = currNum[i]; while (left > 0 && right < xS[3]) { left--; right++; goToPoss(left, right); if (tim < minTim) { minTim = tim; for (int i = 0; i < 4; i++) minNum[i] = currNum[i]; } } cout << minTim << endl; for (int i = 0; i < 4; i++) for (int j = 0; j < minNum[i]; j++) cout << x[i][j].index + 1 << " "; } }
#include <bits/stdc++.h> const int factor_N = 10000005; using namespace std; int read() { char c = getchar(); int x = 0, f = 1; for (; !isdigit(c); c = getchar()) if (c == '-') f = -1; for (; isdigit(c); c = getchar()) x = x * 10 + c - 48; return x * f; } struct Node { int t, a, b; }; const int N = 2e5 + 5; int n, k; int a[N], b[N], c[N]; int cnta, cntb, cntc, t, aa, bb; int ans; int main() { n = read(); k = read(); for (int i = (1); i <= (n); ++i) { t = read(); aa = read(); bb = read(); if (aa == 0 && bb != 0) a[++cnta] = t; else if (aa != 0 && bb == 0) b[++cntb] = t; else if (aa == 1 && bb == 1) c[++cntc] = t; } if (cnta + cntc < k || cntb + cntc < k) { cout << -1 << endl; return 0; } sort(a + 1, a + 1 + cnta); sort(b + 1, b + 1 + cntb); sort(c + 1, c + 1 + cntc); int t = min(cnta, cntb); for (int i = 1; i <= k && i <= t; ++i) ans += a[i]; for (int i = 1; i <= k && i <= t; ++i) ans += b[i]; if (t < k) { for (int i = 1; i <= k - t; ++i) ans += c[i]; int tt = k - t + 1; while (c[tt] < a[t] + b[t] && tt <= cntc) ans -= a[t], ans -= b[t], ans += c[tt], tt++, t--; cout << ans << endl; return 0; } int tt = 1; while (c[tt] < a[k] + b[k] && tt <= cntc) { ans -= a[k]; ans -= b[k]; ans += c[tt]; tt++; k--; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; struct node { int a, b, t; int index; node() {} node(int _a, int _b, int _t, int _index) { a = _a; b = _b; t = _t; index = _index; } bool operator<(const struct node &nd) const { return t < nd.t; } bool operator>(const struct node &nd) const { return t > nd.t; } }; vector<node> all, alice, bob, none; priority_queue<node> taken; int all_size, alice_size, bob_size, none_size; priority_queue<node, vector<node>, greater<node> > freee; int minAllCount(int n, int m, int k) { for (int i = 0; i <= all_size; i++) { if (alice_size + i >= k && bob_size + i >= k && i + max(0, k - i) + max(0, k - i) <= m && alice_size + bob_size + none_size + i >= m) { return i; } } return n + 1; } int maxAllCount(int n, int m, int k) { for (int i = all_size; i >= 0; i--) { if (alice_size + i >= k && bob_size + i >= k && i + max(0, k - i) + max(0, k - i) <= m && alice_size + bob_size + none_size + i >= m) { return i; } } return -1; } bool isPossible(int n, int m, int k) { if (all_size >= k) { return true; } int minFromAlice = k - all_size; int minFromBob = k - all_size; if (minFromAlice > alice_size || minFromBob > bob_size) { return false; } else if (all_size + minFromAlice + minFromBob > m) { return false; } return true; } vector<int> finalTakenBooks; void updateResult(int cntAll, int k, int m) { for (int i = 0; i < cntAll; i++) { finalTakenBooks.push_back(all[i].index); } for (int i = 0; i < k - cntAll; i++) { if (i < alice_size) finalTakenBooks.push_back(alice[i].index); if (i < bob_size) finalTakenBooks.push_back(bob[i].index); } while (!taken.empty()) { node nd = taken.top(); taken.pop(); finalTakenBooks.push_back(nd.index); } } pair<int, int> getOptimalResult(int n, int m, int k, int optimal = -1) { int l = minAllCount(n, m, k); int h = maxAllCount(n, m, k); int sm = 0; for (int i = 0; i < l; i++) { sm += all[i].t; } assert(k - l <= alice_size && k - l <= bob_size); for (int i = 0; i < k - l; i++) { sm += alice[i].t; sm += bob[i].t; } while (!freee.empty()) freee.pop(); while (!taken.empty()) taken.pop(); for (int i = max(0, k - l); i < alice_size; i++) { freee.push(alice[i]); } for (int i = max(0, k - l); i < bob_size; i++) { freee.push(bob[i]); } for (int i = 0; i < none_size; i++) { freee.push(none[i]); } int optimalcnt = -1, optimalTime = INT_MAX; for (int cnt = l; cnt <= h; cnt++) { int otherNeeded = (m - cnt - 2 * max(0, k - cnt)); while (otherNeeded > (int)taken.size()) { node nd = freee.top(); freee.pop(); sm += nd.t; taken.push(nd); } while (otherNeeded >= 0 && otherNeeded < (int)taken.size()) { node nd = taken.top(); taken.pop(); sm -= nd.t; freee.push(nd); } while (!taken.empty() && !freee.empty() && freee.top().t < taken.top().t) { node freeTop = freee.top(); freee.pop(); node takenTop = taken.top(); taken.pop(); freee.push(takenTop); taken.push(freeTop); sm -= takenTop.t; sm += freeTop.t; } if (sm < optimalTime && otherNeeded == taken.size()) { optimalTime = sm; optimalcnt = cnt; } if (cnt == optimal) { updateResult(cnt, k, m); return {optimalTime, cnt}; } if (cnt < h) { sm += all[cnt].t; } if (k - cnt > 0) { sm -= alice[k - cnt - 1].t; sm -= bob[k - cnt - 1].t; freee.push(alice[k - cnt - 1]); freee.push(bob[k - cnt - 1]); } } return {optimalTime, optimalcnt}; } void solve(int n, int m, int k) { all_size = all.size(); alice_size = alice.size(); bob_size = bob.size(); none_size = none.size(); assert(all_size + alice_size + bob_size + none_size == n); if (!isPossible(n, m, k) || minAllCount(n, m, k) > maxAllCount(n, m, k)) { puts("-1"); return; } sort(all.begin(), all.end()); sort(alice.begin(), alice.end()); sort(bob.begin(), bob.end()); sort(none.begin(), none.end()); pair<int, int> result = getOptimalResult(n, m, k); result = getOptimalResult(n, m, k, result.second); printf("%d\n", result.first); for (int i = 0; i < finalTakenBooks.size(); i++) { if (i) printf(" "); printf("%d", finalTakenBooks[i]); } puts(""); } int main() { int n, m, k, i; scanf("%d %d %d", &n, &m, &k); node nd; int a, b, t; for (i = 0; i < n; i++) { scanf("%d %d %d", &t, &a, &b); nd = node(a, b, t, i + 1); if (a && b) { all.push_back(nd); } else if (a) { alice.push_back(nd); } else if (b) { bob.push_back(nd); } else { none.push_back(nd); } } solve(n, m, k); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, k; cin >> n >> k; vector<long long int> a, b, c; for (int i = 0; i < n; i++) { int t, x, y; cin >> t >> x >> y; if (x && y) c.push_back(t); else if (x && !y) a.push_back(t); else if (!x && y) b.push_back(t); } sort(a.begin(), a.end(), greater<long long>()); sort(b.begin(), b.end(), greater<long long>()); long long ans = 0; int an = a.size(), bn = b.size(), cn = c.size(); if (an + cn >= k && bn + cn >= k) { while ((int)a.size() > 0 && (int)b.size() > 0) { c.push_back(a.back() + b.back()); a.pop_back(); b.pop_back(); } sort(c.begin(), c.end()); for (int i = 0; i < k; i++) { ans += c[i]; } } else ans = -1; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; void quick_sort(int a[], int left, int right) { int l = left, r = right, m = a[rand() % (r - l) + l]; while (l < r) { while (a[l] < m) l++; while (a[r] > m) r--; if (l <= r) { int t = a[l]; a[l] = a[r]; a[r] = t; l++; r--; } } if (left < r) quick_sort(a, left, r); if (right > l) quick_sort(a, l, right); } int main() { int n, k, a[200000], size_a = 0, b[200000], size_b = 0, c[200000], size_c = 0, min = 0, cnt_a = 0, cnt_b = 0; cin >> n >> k; while (n--) { int t[3]; cin >> t[0] >> t[1] >> t[2]; if (t[1] + t[2] == 2) c[size_c++] = t[0]; else if (t[1]) a[size_a++] = t[0]; else if (t[2]) b[size_b++] = t[0]; } if (size_c > 1) quick_sort(c, 0, size_c - 1); if (size_a > 1) quick_sort(a, 0, size_a - 1); if (size_b > 1) quick_sort(b, 0, size_b - 1); int i = 0, j = 0, l = 0; for (; ((i < size_a && j < size_b) || l < size_c) && cnt_a < k && cnt_b < k;) { if (i < size_a && j < size_b) { if (l < size_c) if (a[i] + b[j] < c[l]) min += a[i++] + b[j++]; else min += c[l++]; else min += a[i++] + b[j++]; } else if (l < size_c) { min += c[l++]; } else break; cnt_a++; cnt_b++; } for (; (i < size_a || l < size_c) && cnt_a < k;) { if (i < size_a) { if (l < size_c) if (a[i] < c[l]) min += a[i++]; else { min += c[l++]; cnt_b++; } else min += a[i++]; } else if (l < size_c) { min += c[l++]; cnt_b++; } else break; cnt_a++; } for (; (j < size_b || l < size_c) && cnt_b < k;) { if (j < size_b) { if (l < size_c) if (b[j] < c[l]) min += b[j++]; else { min += c[l++]; cnt_a++; } else min += b[j++]; } else if (l < size_c) { min += c[l++]; cnt_a++; } else break; cnt_b++; } if (cnt_a < k || cnt_b < k) cout << -1; else cout << min; return 0; }
#include <bits/stdc++.h> using namespace std; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n, k; cin >> n >> k; vector<long long> alice; vector<long long> bob; vector<long long> combined; long long atotal = 0, btotal = 0; for (long long i = 0; i < n; ++i) { long long t, a, b; cin >> t >> a >> b; if (a == 1 && b == 1) combined.push_back(t); else if (a == 1) alice.push_back(t); else if (b == 1) bob.push_back(t); atotal += a; btotal += b; } sort(combined.begin(), combined.end()); sort(alice.begin(), alice.end()); sort(bob.begin(), bob.end()); long long score = 0; bool possible = true; if (atotal < k || btotal < k) { possible = false; cout << -1; } if (possible) { long long m = combined.size(); m = min(k, m); for (long long i = 0; i < m; ++i) { score += combined[i]; } long long pointer = 0; if (m < k) { for (long long j = 0; j < k - m; ++j) { score += alice[j]; score += bob[j]; } pointer = k - m; } long long asize = alice.size(); long long bsize = bob.size(); while (pointer < asize && pointer < bsize && pointer < k && alice[pointer] + bob[pointer] < combined[k - 1 - pointer]) { score -= combined[k - 1 - pointer]; score += alice[pointer]; score += bob[pointer]; pointer++; } cout << score; } }
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 99; int n, k, m, x, y, p0, p1, p2, b0, b1, b2, b3, a[N]; vector<pair<long long, int> > v0, v1, v2, v3; long long ans = 1e18, sum; void calc(int x) { if (p0 < v0.size() - 1) p0++, sum += v0[p0].first; if (p0 < v0.size() - 1) p0++, sum += v0[p0].first; if (k - x >= int(v1.size()) || k - x >= int(v2.size()) || x + v2.size() + v1.size() + v0.size() - 3 < m || x + max((k - x), 0) * 2 > m) return; while (x + p1 + p2 + p0 > m) { if (p0 && (p1 == max(k - x, 0) || v0[p0] >= v1[p1]) && (p2 == max(k - x, 0) || v0[p0] >= v2[p2])) sum -= v0[p0--].first; else { if (p1 == max(k - x, 0) || (p2 != max(k - x, 0) && v2[p2].first >= v1[p1].first)) { while (p2 == max(k - x, 0)) n = 1; sum -= v2[p2--].first; } else sum -= v1[p1--].first; } } if (sum + v3[x].first < ans) ans = sum + v3[x].first, b0 = p0, b1 = p1, b2 = p2, b3 = x; } int main() { cin >> n >> m >> k; v1.push_back(make_pair(0, 0)), v2.push_back(make_pair(0, 0)), v3.push_back(make_pair(0, 0)), v0.push_back(make_pair(0, 0)); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); ; scanf("%d%d", &x, &y); ; if (x == 1 && y == 1) v3.push_back(make_pair(a[i], i + 1)); if (x == 1 && y == 0) v1.push_back(make_pair(a[i], i + 1)); if (x == 0 && y == 1) v2.push_back(make_pair(a[i], i + 1)); if (x == 0 && y == 0) v0.push_back(make_pair(a[i], i + 1)); } p0 = v0.size() - 1, p1 = v1.size() - 1, p2 = v2.size() - 1; sort(v0.begin(), v0.end()); sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); sort(v3.begin(), v3.end()); for (int i = 1; i < v0.size(); i++) sum += v0[i].first; for (int i = 1; i < v1.size(); i++) sum += v1[i].first; for (int i = 1; i < v2.size(); i++) sum += v2[i].first; for (int i = 1; i < v3.size(); i++) v3[i].first += v3[i - 1].first; for (int i = 0; i < v3.size(); i++) calc(i); if (ans == 1e18) return cout << -1, 0; cout << ans << endl; for (int i = 1; i < b0 + 1; i++) cout << v0[i].second << " "; for (int i = 1; i < b1 + 1; i++) cout << v1[i].second << " "; for (int i = 1; i < b2 + 1; i++) cout << v2[i].second << " "; for (int i = 1; i < b3 + 1; i++) cout << v3[i].second << " "; }
#include <bits/stdc++.h> const long long maxn = 2e5 + 1; using namespace std; struct triple { long long t, ci; bool a = 0, b = 0; triple() {} }; bool cmp(triple x, triple y) { return x.t < y.t; } triple v[maxn]; bool usd[maxn]; long long ci = -1; long long cs = 0; inline void add() { ++ci; for (; usd[ci]; ci++) ; cs += v[ci].t; } inline void del() { if (ci == -1) exit(228); cs -= v[ci].t; --ci; for (; ci >= 0 && usd[ci]; --ci) ; } inline void dl(long long x) { usd[x] = 0; if (x < ci) { cs += v[x].t - v[ci].t; --ci; for (; ci >= 0 && usd[ci]; ci--) ; } } inline void ad(long long x) { usd[x] = 1; if (x <= ci) { cs -= v[x].t; ++ci; for (; usd[ci]; ci++) ; cs += v[ci].t; } } long long n, m, k; long long solve() { cin >> n >> m >> k; for (long long i = 0; i < n; i++) { cin >> v[i].t >> v[i].a >> v[i].b; v[i].ci = i + 1; } sort(v, v + n, cmp); vector<long long> oo, oz, zo; for (long long i = 0; i < n; i++) { if (v[i].a && v[i].b) oo.push_back(i); else if (v[i].a) oz.push_back(i); else if (v[i].b) zo.push_back(i); } long long j1 = 0, j2 = 0, mi = -1; long long ans = 1e18, can = 0; for (long long i = 0; i < m; i++) add(); for (long long i = 0; i < min(k, (long long)oo.size()); i++) { can += v[oo[i]].t; del(); ad(oo[i]); } for (long long i = min(k, (long long)oo.size()); i >= 0; i--) { if (i + min(zo.size(), oz.size()) < k) break; if (2 * k - i > m) break; if (i < min(k, (long long)oo.size())) { dl(oo[i]); add(); can -= v[oo[i]].t; } for (; i + j1 < k; j1++) { can += v[oz[j1]].t; del(); ad(oz[j1]); } for (; i + j2 < k; j2++) { can += v[zo[j2]].t; del(); ad(zo[j2]); } if (ans > can + cs) { ans = can + cs; mi = i; } } if (mi == -1) { cout << -1; return 0; } cout << ans << '\n'; for (long long i = 0; i < n; i++) usd[i] = 0; for (long long j = 0; j < mi; j++) { cout << v[oo[j]].ci << ' '; usd[oo[j]] = 1; } for (long long j = 0; j < k - mi; j++) { cout << v[oz[j]].ci << ' '; usd[oz[j]] = 1; } for (long long j = 0; j < k - mi; j++) { cout << v[zo[j]].ci << ' '; usd[zo[j]] = 1; } long long cnt = 0; for (long long i = 0; cnt < m - (2 * k - mi); i++) { if (usd[i]) continue; cout << v[i].ci << ' '; ++cnt; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const double eps = 1e-10; template <typename T> void read(T &x) { int f = 1; x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s >= '0' && s <= '9') { x = x * 10 + s - '0'; s = getchar(); } x *= f; } template <typename T> void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + '0'); } template <typename T> void println(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + '0'); putchar('\n'); } template <typename T, typename... Args> void read(T &x, Args &...args) { read(x); read(args...); } inline void reads(char *a) { scanf("%s", a); } template <typename... Args> void reads(char *a, Args *...args) { scanf("%s", a); reads(args...); } template <typename T, typename... Args> void print(T x, Args... args) { print(x); putchar(' '); print(args...); } template <typename T, typename... Args> void println(T x, Args... args) { print(x); putchar(' '); println(args...); } inline void prints(char *a) { printf("%s", a); } template <typename T> inline T sqr(T x) { return x * x; } template <typename T> inline T random(T R) { return (double)rand() / RAND_MAX * R + ((T)0.5 == 0 ? 0.5 : 0.0); } template <typename T> inline T random(T L, T R) { return random(R - L) + L; } long long ksm(long long a, long long k, long long Mod) { long long res = 1; while (k) { if (k & 1) (res *= a) %= Mod; (a *= a) %= Mod; k >>= 1; } return res % Mod; } inline long long inv(long long a, long long Mod) { return ksm(a, Mod - 2, Mod); } inline void cap_bit(long long x) { for (int i = 63; i >= 0; --i) if (x >> i & 1) { printf("Need (%d) = %lld\n", i + 1, 1LL << (i + 1)); return; } } inline void cal_space(long long x) { printf("%lld MB\n", x >> 20); } const int dx4[] = {0, 0, -1, 1}; const int dy4[] = {-1, 1, 0, 0}; const int dx6[] = {1, -1, 0, 0, 0, 0}; const int dy6[] = {0, 0, 1, -1, 0, 0}; const int dz6[] = {0, 0, 0, 0, 1, -1}; const int N = 2e5 + 5; const int M = 26; const long long Mod = 1e9 + 7; const long long INF = 1e16; pair<long long, long long> a[4][N]; long long ia[4]; long long n, m, k; set<pair<long long, long long> > sf, ss; long long ans, cur; void adjust(int x) { long long cnt = x + 2 * (k - x); while (ss.size() && sf.size() && ss.rbegin()->first > sf.begin()->first) { cur -= ss.rbegin()->first; cur += sf.begin()->first; sf.insert(*ss.rbegin()); ss.erase(*ss.rbegin()); ss.insert(*sf.begin()); sf.erase(*sf.begin()); } while (ss.size() && cnt + ss.size() > m) { cur -= ss.rbegin()->first; sf.insert(*ss.rbegin()); ss.erase(*ss.rbegin()); } while (sf.size() && cnt + ss.size() < m) { cur += sf.begin()->first; ss.insert(*sf.begin()); sf.erase(*sf.begin()); } } int main() { cin >> n >> m >> k; for (int i = 0; i < 4; ++i) ia[i] = 0; for (int i = 1; i <= n; ++i) { long long t, ai, bi; cin >> t >> ai >> bi; int s = 0; if (ai) s += 1; if (bi) s += 2; a[s][++ia[s]] = {t, i}; } if (ia[1] + ia[3] < k || ia[2] + ia[3] < k) { puts("-1"); return 0; } for (int i = 0; i < 4; ++i) sort(a[i] + 1, a[i] + ia[i] + 1); long long st = 0; cur = -1; ans = -1; long long ians; while ((k - st) > ia[1] || (k - st) > ia[2]) st++; for (int i = st; i <= ia[3] && i <= k; ++i) { long long cnt = i + (k - i) * 2; if (cur == -1) { cur = 0; for (int j = 1; j <= ia[3]; ++j) if (j <= st) cur += a[3][j].first; else sf.insert(a[3][j]); for (int j = 1; j <= ia[2]; ++j) if (j <= (k - st)) cur += a[2][j].first; else sf.insert(a[2][j]); for (int j = 1; j <= ia[1]; ++j) if (j <= (k - st)) cur += a[1][j].first; else sf.insert(a[1][j]); for (int j = 1; j <= ia[0]; ++j) sf.insert(a[0][j]); } else { long long r = k - i; if (sf.find(a[3][i]) != sf.end()) cur += a[3][i].first, sf.erase(a[3][i]); else ss.erase(a[3][i]); cur -= a[1][r + 1].first; cur -= a[2][r + 1].first; sf.insert(a[1][r + 1]); sf.insert(a[2][r + 1]); } adjust(i); if (cnt + ss.size() == m) { if (ans == -1 || cur < ans) { ans = cur; ians = i; } } } cout << ans << endl; if (ans == -1) return 0; vector<pair<long long, long long> > res; for (int i = 1; i <= ians; ++i) cout << a[3][i].second << " "; for (int i = ians + 1; i <= ia[3]; ++i) res.push_back(a[3][i]); for (int i = 1; i <= k - ians; ++i) cout << a[1][i].second << " "; for (int i = k - ians + 1; i <= ia[1]; ++i) res.push_back(a[1][i]); for (int i = 1; i <= k - ians; ++i) cout << a[2][i].second << " "; for (int i = k - ians + 1; i <= ia[2]; ++i) res.push_back(a[2][i]); for (int i = 1; i <= ia[0]; ++i) res.push_back(a[0][i]); sort((res).begin(), (res).end()); for (int i = 1; i <= m - ians - 2 * (k - ians); ++i) cout << res[i - 1].second << " "; return 0; }
#include <bits/stdc++.h> using namespace std; vector<long long> D; vector<long long> B; vector<long long> A; int main() { long long a = 0, b = 0; long long n, k; long long ti, al, bl, time = 0; cin >> n >> k; for (long long i = 0; i < n; ++i) { cin >> ti >> al >> bl; if (al + bl == 2) D.push_back(ti); else if (al == 1) A.push_back(ti); else if (bl == 1) B.push_back(ti); a += al; b += bl; } if (a < k || b < k) { cout << -1 << endl; return 0; } if (n == k && k == a && k == b) { time += accumulate(D.begin(), D.end(), 0); time += accumulate(A.begin(), A.end(), 0); time += accumulate(B.begin(), B.end(), 0); cout << time << endl; return 0; } a = b = k; sort(D.begin(), D.end()); sort(A.begin(), A.end()); sort(B.begin(), B.end()); for (long long i = 0, j = 0, l = 0; a > 0 || b > 0;) { if (i < D.size() && j < A.size() && l < B.size()) { if (D[i] <= A[j] + B[l]) { time += D[i]; ++i; --a; --b; } else { time += A[j] + B[l]; ++j; ++l; --a; --b; } } else if (i < D.size() && j < A.size() && l >= B.size()) { if (a > 0 && b > 0) { time += D[i]; ++i; --a; --b; } else if (a > 0 && b <= 0) { if (D[i] < A[j]) { time += D[i]; ++i; --a; --b; } else { time += A[j]; ++j; --a; } } } else if (i < D.size() && j >= A.size() && l < B.size()) { if (a > 0 && b > 0) { time += D[i]; ++i; --a; --b; } else if (a <= 0 && b > 0) { if (D[i] < B[l]) { time += D[i]; ++i; --a; --b; } else { time += B[l]; ++l; --b; } } } else if (i >= D.size()) { if (a > 0 && j < A.size()) { time += A[j]; ++j; --a; } else if (b > 0 && l < B.size()) { time += B[l]; ++l; --b; } } else if (i < D.size() && j >= A.size() && l >= B.size()) { time += D[i]; ++i; --a; --b; } } cout << time << endl; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, k, c1 = 0, c2 = 0, v = 0, ans = 0, res; cin >> n >> k; long long a[n][3]; vector<long long> s1, s2, s3; for (long long i = 0; i < n; i++) { for (long long j = 0; j < 3; j++) { cin >> a[i][j]; if (j == 1) { if (a[i][j] == 1) c1++; } else if (j == 2) { if (a[i][j] == 1) c2++; } } } if (c1 < k || c2 < k) { cout << "-1" << "\n"; } else { for (long long i = 0; i < n; i++) { if (a[i][1] && a[i][2]) s1.push_back(a[i][0]); else if (a[i][1]) s2.push_back(a[i][0]); else if (a[i][2]) s3.push_back(a[i][0]); } sort(s2.begin(), s2.end()); sort(s3.begin(), s3.end()); if (s2.size() < s3.size()) res = s2.size(); else res = s3.size(); for (long long i = 0; i < res; i++) { s1.push_back(s2[i] + s3[i]); } sort(s1.begin(), s1.end()); for (auto i : s1) { if (v >= k) break; ans += i; v++; } cout << ans << "\n"; s1.clear(); s2.clear(); s3.clear(); } return 0; }
#include <bits/stdc++.h> const int MAXN = 3e5 + 10; const double eps = 1e-8; const int inf = 2e9 + 9; using namespace std; struct edge { int t, v; edge *next; } e[MAXN << 1], *h[MAXN], *o = e; void add(int x, int y, int vul) { o->t = y; o->v = vul; o->next = h[x]; h[x] = o++; } long long read() { long long x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); return x * f; } pair<int, int> a[4][MAXN]; int cnt[4]; int num[MAXN << 2], sum[MAXN << 2]; void up(int x) { num[x] = num[x << 1] + num[x << 1 | 1]; sum[x] = sum[x << 1] + sum[x << 1 | 1]; } void update(int x, int l, int r, int t, int y) { if (l == r) { num[x] += y; sum[x] = l * num[x]; return; } int mid = (l + r) >> 1; if (t <= mid) update(x << 1, l, mid, t, y); else update(x << 1 | 1, mid + 1, r, t, y); up(x); } int ans; void query(int x, int l, int r, int k) { if (!k) return; if (l == r) { ans += k * l; return; } int mid = (l + r) >> 1; if (num[x << 1] >= k) query(x << 1, l, mid, k); else ans += sum[x << 1], query(x << 1 | 1, mid + 1, r, k - num[x << 1]); } int n, m, k; int sum1[MAXN], sum2[MAXN]; pair<int, int> b[MAXN]; int main() { n = read(); m = read(); k = read(); int x, y, z; int sz = 1e4; for (int i = 1; i <= n; i++) { x = read(); y = read(); z = read(); int p = 2 * y + z; a[p][++cnt[p]] = make_pair(x, i); } for (int i = 0; i <= 3; i++) sort(a[i] + 1, a[i] + cnt[i] + 1); for (int j = 0; j <= 2; j++) for (int i = 1; i <= cnt[j]; i++) update(1, 1, sz, a[j][i].first, 1); int pos = cnt[3]; int minn = inf; int p = -1; for (int i = 1; i <= min(cnt[1], cnt[2]); i++) sum1[i] = sum1[i - 1] + a[1][i].first + a[2][i].first; for (int i = 1; i <= cnt[3]; i++) sum2[i] = sum2[i - 1] + a[3][i].first; for (int i = 0; i <= min(cnt[1], cnt[2]); i++) { if (i > 0) { update(1, 1, sz, a[1][i].first, -1); update(1, 1, sz, a[2][i].first, -1); } if (i > k) continue; if (cnt[3] < k - i) continue; if (2 * i + k - i > m) continue; while (pos > k - i) { update(1, 1, sz, a[3][pos].first, 1); pos--; } ans = 0; query(1, 1, sz, m - k - i); if (ans + sum1[i] + sum2[pos] < minn) { minn = ans + sum1[i] + sum2[pos]; p = i; } } if (minn == inf) return 0 * printf("-1\n"); printf("%d\n", minn); for (int i = 1; i <= p; i++) printf("%d %d ", a[1][i].second, a[2][i].second); for (int i = 1; i <= k - p; i++) printf("%d ", a[3][i].second); int tot = 0; for (int i = 1; i <= cnt[0]; i++) b[++tot] = a[0][i]; for (int i = p + 1; i <= cnt[1]; i++) b[++tot] = a[1][i]; for (int i = p + 1; i <= cnt[2]; i++) b[++tot] = a[2][i]; for (int i = k - p + 1; i <= cnt[3]; i++) b[++tot] = a[3][i]; sort(b + 1, b + tot + 1); for (int i = k + p + 1; i <= m; i++) printf("%d ", b[i - k - p].second); return 0; }
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long powM(long long x, long long y, long long m) { if (y == 0) return 1; long long p = powM(x, y / 2, m) % m; p = (p * p) % m; return (y % 2 == 0) ? p : (x * p) % m; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; vector<int> vt, va, vb; for (long long i = 1; i <= n; i++) { int t, a, b; cin >> t >> a >> b; if (a == 1 && b == 1) vt.push_back(t); if (a == 1 && b == 0) va.push_back(t); if (a == 0 && b == 1) vb.push_back(t); } sort(va.begin(), va.end()); sort(vb.begin(), vb.end()); for (long long i = 0; i <= min((int)va.size() - 1, (int)vb.size() - 1); i++) vt.push_back(va[i] + vb[i]); sort(vt.begin(), vt.end()); if ((int)vt.size() < k) { cout << -1 << '\n'; return 0; } int sum = 0; for (long long i = 0; i <= k - 1; i++) sum += vt[i]; cout << sum << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; inline long long read() { long long f = 1, ans = 0; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { ans = ans * 10 + c - '0'; c = getchar(); } return f * ans; } const long long MAXN = 4e5 + 11; long long N, M, K, na, nb, nc, nd, Minn = LLONG_MAX, SA[MAXN], SB[MAXN], SC[MAXN], SD[MAXN]; long long tmp1[MAXN], tmp2[MAXN], tmp3[MAXN], tmp4[MAXN]; pair<long long, long long> A[MAXN], B[MAXN], C[MAXN], D[MAXN]; long long sta1[MAXN], sta2[MAXN], sta3[MAXN], sta4[MAXN]; long long QA(long long x) { return lower_bound(tmp1 + 1, tmp1 + N + 1, x + 1) - tmp1 - 1; } long long QB(long long x) { return lower_bound(tmp2 + 1, tmp2 + N + 1, x + 1) - tmp2 - 1; } long long QD(long long x) { return lower_bound(tmp4 + 1, tmp4 + N + 1, x + 1) - tmp4 - 1; } signed main() { N = read(), M = read(), K = read(); for (long long i = 1; i <= N; i++) { long long t = read(), a = read(), b = read(); pair<long long, long long> p = make_pair(t, i); if (a && b) C[++nc] = p; if (a && (!b)) A[++na] = p; if ((!a) && b) B[++nb] = p; if ((!a) && (!b)) D[++nd] = p; } for (long long i = na + 1; i <= N; i++) A[i] = make_pair(INT_MAX, 0); for (long long i = nb + 1; i <= N; i++) B[i] = make_pair(INT_MAX, 0); for (long long i = nc + 1; i <= N; i++) C[i] = make_pair(INT_MAX, 0); for (long long i = nd + 1; i <= N; i++) D[i] = make_pair(INT_MAX, 0); sort(A + 1, A + N + 1), sort(B + 1, B + N + 1), sort(C + 1, C + N + 1), sort(D + 1, D + N + 1); for (long long i = 1; i <= N; i++) SA[i] = SA[i - 1] + A[i].first, SB[i] = SB[i - 1] + B[i].first, SC[i] = SC[i - 1] + C[i].first, SD[i] = SD[i - 1] + D[i].first; for (long long i = 1; i <= N; i++) tmp1[i] = A[i].first, tmp2[i] = B[i].first, tmp3[i] = C[i].first, tmp4[i] = D[i].first; for (long long i = 0; i <= N; i++) { long long res = (max(K - i, 0ll)) * 2 + i; if (res > M) continue; if (res < 0) continue; long long ps1 = max(K - i, 0ll), ps2 = max(K - i, 0ll), l = 0, r = 10000, Ans = -1; long long Res = M - res; long long u1 = 0, u2 = 0, u4 = 0; while (l <= r) { long long mid = (l + r) >> 1; long long X = max(QA(mid) - ps1, 0ll), Y = max(QB(mid) - ps2, 0ll), Z = QD(mid); if (X + Y + Z >= Res) Ans = mid, u1 = X, u2 = Y, u4 = Z, r = mid - 1; else l = mid + 1; } if (Ans == -1) continue; long long W = SA[u1 + ps1] + SB[u2 + ps2] + SC[i] + SD[u4]; long long Ha = (u1 + ps1 + u2 + ps2 + i + u4) - M; W -= Ha * Ans; Minn = min(Minn, W); } if (Minn > 2000000000) { printf("-1\n"); return 0; } printf("%lld\n", Minn); for (long long i = 0; i <= N; i++) { long long res = (max(K - i, 0ll)) * 2 + i; if (res > M) continue; if (res < 0) continue; long long ps1 = max(K - i, 0ll), ps2 = max(K - i, 0ll), l = 0, r = 10000, Ans = -1; long long Res = M - res; long long u1 = 0, u2 = 0, u4 = 0; while (l <= r) { long long mid = (l + r) >> 1; long long X = max(QA(mid) - ps1, 0ll), Y = max(QB(mid) - ps2, 0ll), Z = QD(mid); if (X + Y + Z >= Res) Ans = mid, u1 = X, u2 = Y, u4 = Z, r = mid - 1; else l = mid + 1; } if (Ans == -1) continue; long long W = SA[u1 + ps1] + SB[u2 + ps2] + SC[i] + SD[u4]; long long Ha = (u1 + ps1 + u2 + ps2 + i + u4) - M; W -= Ha * Ans; if (Minn == W) { long long tot1 = 0, tot2 = 0, tot3 = 0, tot4 = 0; for (long long j = 1; j <= u1 + ps1; j++) { if (A[j].first != Ans) printf("%lld ", A[j].second), tot1++; else sta1[++sta1[0]] = A[j].second; } for (long long j = 1; j <= u2 + ps2; j++) { if (B[j].first != Ans) printf("%lld ", B[j].second), tot2++; else sta2[++sta2[0]] = B[j].second; } for (long long j = 1; j <= u4; j++) { if (D[j].first != Ans) printf("%lld ", D[j].second), tot4++; else sta4[++sta4[0]] = D[j].second; } for (long long j = 1; j <= i; j++) { if (C[j].first != Ans) printf("%lld ", C[j].second), tot3++; else sta3[++sta3[0]] = C[j].second; } long long E = M - tot1 - tot2 - tot3 - tot4, ps1 = 1, ps2 = 1, ps3 = 1, ps4 = 1; for (long long i = 1; i <= E; i++) { if (tot1 < tot2 && ps1 <= sta1[0]) { printf("%lld ", sta1[ps1]); ps1++; tot1++; continue; } if (tot1 > tot2 && ps2 <= sta2[0]) { printf("%lld ", sta2[ps2]); ps2++; tot2++; continue; } if (ps3 <= sta3[0]) { printf("%lld ", sta3[ps3]); ps3++; continue; } if (ps1 <= sta1[0]) { printf("%lld ", sta1[ps1]); ps1++; continue; } if (ps2 <= sta2[0]) { printf("%lld ", sta2[ps2]); ps2++; continue; } if (ps4 <= sta4[0]) { printf("%lld ", sta4[ps4]); ps4++; continue; } } printf("\n"); return 0; } } return 0; }
#include <bits/stdc++.h> using namespace std; void readtxt() {} void fast() { ios_base::sync_with_stdio(false); cin.tie(NULL); } int32_t main() { fast(); long long int n, k; cin >> n >> k; vector<long long int> a1b1, a1b0, a0b1; long long int ans = 0, t, a, b; for (long long int i = 0; i < n; i++) { cin >> t >> a >> b; if (a == 1 && b == 1) a1b1.push_back(t); else if (a == 1 && b == 0) a1b0.push_back(t); else if (a == 0 && b == 1) a0b1.push_back(t); } sort(a1b1.begin(), a1b1.end()); sort(a1b0.begin(), a1b0.end()); sort(a0b1.begin(), a0b1.end()); auto it1 = a1b1.begin(); auto it2 = a1b0.begin(); auto it3 = a0b1.begin(); long long int flag = 1; while (k--) { if (it1 == a1b1.end() && (it2 == a1b0.end() || it3 == a0b1.end())) { flag = 0; cout << -1 << endl; break; } else if (it2 == a1b0.end() || it3 == a0b1.end()) { ans += (*it1); it1++; } else if (it1 == a1b1.end()) { ans += (*it2) + (*it3); it2++; it3++; } else { if ((*it1) <= (*it2) + (*it3)) { ans += (*it1); it1++; } else { ans += (*it2) + (*it3); it2++; it3++; } } } if (flag == 1) cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; const int inf = (int)1e9; long long A(long long x) { if (x >= 0) return x; else return -x; } long long gcd(long long a, long long b) { if (b > a) { long long tmp = b; b = a; a = tmp; } if (a % b == 0) return b; else return gcd(b, a % b); } unsigned long long popcount(unsigned long long x) { x = ((x & 0xaaaaaaaaaaaaaaaaUL) >> 1) + (x & 0x5555555555555555UL); x = ((x & 0xccccccccccccccccUL) >> 2) + (x & 0x3333333333333333UL); x = ((x & 0xf0f0f0f0f0f0f0f0UL) >> 4) + (x & 0x0f0f0f0f0f0f0f0fUL); x = ((x & 0xff00ff00ff00ff00UL) >> 8) + (x & 0x00ff00ff00ff00ffUL); x = ((x & 0xffff0000ffff0000UL) >> 16) + (x & 0x0000ffff0000ffffUL); x = ((x & 0xffffffff00000000UL) >> 32) + (x & 0x00000000ffffffffUL); return x; } int main(void) { int T; T = 1; for (int query = 0; query < T; query++) { int n, k; cin >> n >> k; priority_queue<long long, vector<long long>, greater<long long> > both, bob, alice; for (int i = 0; i < n; i++) { long long t; int a, b; cin >> t >> a >> b; if (a == 1) { if (b == 0) alice.push(t); else both.push(t); } else { if (b == 1) bob.push(t); } } if (alice.size() + both.size() < k || bob.size() + both.size() < k) { cout << -1 << endl; } else { long long ans = 0; while (k > 0) { if (both.size() == 0) { while (k > 0) { long long ali = alice.top(); long long bo = bob.top(); alice.pop(); bob.pop(); ans += ali + bo; k--; } } else { if (alice.size() == 0) { while (k > 0) { long long c = both.top(); both.pop(); ans += c; k--; } break; } if (bob.size() == 0) { while (k > 0) { long long c = both.top(); both.pop(); ans += c; k--; } break; } long long a = alice.top(); long long b = bob.top(); long long c = both.top(); if (c > a + b) { ans += a + b; k--; bob.pop(); alice.pop(); } else { ans += c; k--; both.pop(); } } } cout << ans << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; long long int const mod = 1000000007; std::mt19937 rng( (int)std::chrono::steady_clock::now().time_since_epoch().count()); int rand_rng(int l, int r) { uniform_int_distribution<int> p(l, r); return p(rng); } long long int power(long long int x, long long int y, long long int m) { long long int temp; if (y == 0) return 1; temp = power(x, y / 2, m) % m; if (y % 2 == 0) return ((temp) * (temp)) % m; else return (((x) % m) * ((temp * temp) % m)) % m; } int const N = 200009; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int T; T = 1; while (T--) { long long int n, m, k, t, a, b; cin >> n >> m >> k; set<pair<long long int, long long int> > fre, st; vector<pair<long long int, long long int> > aa, bb, ab, zz; for (int i = 0; i < (int)n; i++) { cin >> t >> a >> b; if (a && b) { ab.push_back({t, i + 1}); } else if (a) { aa.push_back({t, i + 1}); } else if (b) { bb.push_back({t, i + 1}); } else { fre.insert({t, i + 1}); zz.push_back({t, i + 1}); } } sort(aa.begin(), aa.end()); ; sort(bb.begin(), bb.end()); ; sort(ab.begin(), ab.end()); ; sort(zz.begin(), zz.end()); ; int ind = -1; long long int sum = 0; long long int ans = 100000000000000000; long long int tempsum = 0; long long int out = 0; for (int i = 0; i <= ab.size(); i++) { long long int temp = k - i; if (i > m) break; if (i) { sum += ab[i - 1].first; } if (aa.size() >= temp && bb.size() >= temp && i + 2 * temp <= m && n - (ab.size() - i) >= m) { if (ind == -1) { for (int j = 0; j < (int)temp; j++) sum += aa[j].first + bb[j].first; for (int j = max(0ll, temp); j < aa.size(); j++) fre.insert(aa[j]); for (int j = max(0ll, temp); j < bb.size(); j++) fre.insert(bb[j]); ind = i; temp = max(0ll, temp); long long int dif = m - i - 2 * temp; while (st.size() < dif && fre.size()) { tempsum += (fre.begin())->first; st.insert(*(fre.begin())); fre.erase(*(fre.begin())); } ans = min(ans, sum + tempsum); out = i; } else { if (temp >= 0) sum -= aa[temp].first + bb[temp].first; if (temp >= 0) { fre.insert(aa[temp]); fre.insert(bb[temp]); } temp = max(0ll, temp); long long int dif = m - i - 2 * temp; while (st.size() && st.size() > dif) { tempsum -= (st.rbegin())->first; fre.insert(*(st.rbegin())); st.erase(*(st.rbegin())); } while (st.size() < dif && fre.size()) { tempsum += (fre.begin())->first; st.insert(*(fre.begin())); fre.erase(*(fre.begin())); } while (st.size() && fre.size() && *(st.rbegin()) > *(fre.begin())) { st.insert(*(fre.begin())); tempsum += (fre.begin())->first; fre.erase(*(fre.begin())); fre.insert(*(st.rbegin())); tempsum -= (st.rbegin())->first; st.erase(*(st.rbegin())); } if (sum + tempsum < ans) out = i; ans = min(ans, sum + tempsum); } } } if (ind == -1) { cout << "-1\n"; } else { ans = 0; vector<long long int> p; long long int temp = k - out; for (int i = 0; i < (int)out; i++) { p.push_back(ab[i].second); ans += ab[i].first; } for (int i = 0; i < (int)temp; i++) { p.push_back(aa[i].second); ans += aa[i].first; } for (int i = 0; i < (int)temp; i++) { p.push_back(bb[i].second); ans += bb[i].first; } fre.clear(); for (int x = out; x < ab.size(); x++) fre.insert(ab[x]); for (auto i : zz) fre.insert(i); temp = max(0ll, temp); for (int i = temp; i < aa.size(); i++) fre.insert(aa[i]); for (int i = temp; i < bb.size(); i++) fre.insert(bb[i]); long long int dif = m - out - 2 * temp; while (dif > 0) { dif--; p.push_back((fre.begin())->second); ans += (fre.begin())->first; fre.erase(*(fre.begin())); } cout << ans << "\n"; for (auto j : p) cout << j << " "; } } }
#include <bits/stdc++.h> using namespace std; const int INF = 2e9 + 1; void updateSt(set<pair<int, int>> &st, set<pair<int, int>> &fr, int &sum, int need) { need = max(need, 0); while (true) { bool useful = false; while (int((st).size()) > need) { sum -= st.rbegin()->first; fr.insert(*st.rbegin()); st.erase(prev(st.end())); useful = true; } while (int((st).size()) < need && int((fr).size()) > 0) { sum += fr.begin()->first; st.insert(*fr.begin()); fr.erase(fr.begin()); useful = true; } while (!st.empty() && !fr.empty() && fr.begin()->first < st.rbegin()->first) { sum -= st.rbegin()->first; sum += fr.begin()->first; fr.insert(*st.rbegin()); st.erase(prev(st.end())); st.insert(*fr.begin()); fr.erase(fr.begin()); useful = true; } if (!useful) break; } } int main() { int n, m, k; cin >> n >> m >> k; vector<pair<int, int>> times[4]; vector<int> sums[4]; for (int i = 0; i < n; ++i) { int t, a, b; cin >> t >> a >> b; times[a * 2 + b].push_back({t, i}); } for (int i = 0; i < 4; ++i) { sort(times[i].begin(), times[i].end()); sums[i].push_back(0); for (auto it : times[i]) { sums[i].push_back(sums[i].back() + it.first); } } int ans = INF; int pos = INF; set<pair<int, int>> st; set<pair<int, int>> fr; int sum = 0; vector<int> res; for (int iter = 0; iter < 2; ++iter) { st.clear(); fr.clear(); sum = 0; int start = 0; while (k - start >= int((sums[1]).size()) || k - start >= int((sums[2]).size()) || m - start - (k - start) * 2 < 0) { ++start; } if (start >= int((sums[3]).size())) { cout << -1 << endl; return 0; } int need = m - start - (k - start) * 2; for (int i = 0; i < 3; ++i) { for (int p = int((times[i]).size()) - 1; p >= (i == 0 ? 0 : k - start); --p) { fr.insert(times[i][p]); } } updateSt(st, fr, sum, need); for (int cnt = start; cnt < (iter == 0 ? int((sums[3]).size()) : pos); ++cnt) { if (k - cnt >= 0) { if (cnt + (k - cnt) * 2 + int((st).size()) == m) { if (ans > sums[3][cnt] + sums[1][k - cnt] + sums[2][k - cnt] + sum) { ans = sums[3][cnt] + sums[1][k - cnt] + sums[2][k - cnt] + sum; pos = cnt + 1; } } } else { if (cnt + int((st).size()) == m) { if (ans > sums[3][cnt] + sum) { ans = sums[3][cnt] + sum; pos = cnt + 1; } } } if (iter == 1 && cnt + 1 == pos) break; need -= 1; if (k - cnt > 0) { need += 2; fr.insert(times[1][k - cnt - 1]); fr.insert(times[2][k - cnt - 1]); } updateSt(st, fr, sum, need); } if (iter == 1) { for (int i = 0; i + 1 < pos; ++i) res.push_back(times[3][i].second); for (int i = 0; i <= k - pos; ++i) { res.push_back(times[1][i].second); res.push_back(times[2][i].second); } for (auto [value, position] : st) res.push_back(position); } } cout << ans << endl; for (auto it : res) cout << it + 1 << " "; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const unsigned long long int INF = numeric_limits<int>::max(); long long int k, n, A, B, T; unsigned long long int ab[200005], a[200005], b[200005]; int main() { std::ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); ; cin >> n >> k; int I = 0, J = 0, K = 0; for (int i = 0; i < n; i++) { cin >> T >> A >> B; if (A == 1 && B == 1) ab[I] = T, I++; else if (A == 1) a[J] = T, J++; else if (B == 1) b[K] = T, K++; } sort(ab, ab + I); sort(b, b + K); sort(a, a + J); if (J + I < k || K + I < k) { cout << -1 << "\n"; return 0; } if (I == 0) ab[0] = INF; if (J == 0) a[0] = INF; if (K == 0) b[0] = INF; for (int i = 1; i < n; i++) { if (i < J) a[i] += a[i - 1]; else a[i] = INF; if (i < K) b[i] += b[i - 1]; else b[i] = INF; if (i < I) ab[i] += ab[i - 1]; else ab[i] = INF; } unsigned long long int ans = min(ab[k - 1], a[k - 1] + b[k - 1]); for (int i = 0; i < (k - 1); i++) { ans = min(ans, ab[k - i - 2] + a[i] + b[i]); } cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; priority_queue<pair<int64_t, int64_t>, vector<pair<int64_t, int64_t>>, greater<pair<int64_t, int64_t>>> a, b, c, d; priority_queue<pair<int64_t, int64_t>> both; vector<int64_t> ans; vector<pair<int64_t, pair<int64_t, int64_t>>> prblm; map<int64_t, bool> ok; int64_t n, m, k, t; void print() { cout << t << '\n'; unordered_set<int64_t> s; for (auto j : ans) if (!ok[j]) s.insert(j + 1); for (auto j : s) cout << j << ' '; } void m_greater() { int64_t x, y, z, w, u, cnt; cnt = ans.size(); while (cnt < m) { if (a.size()) x = a.top().first; else x = LLONG_MAX; if (b.size()) y = b.top().first; else y = LLONG_MAX; if (c.size()) z = c.top().first; else z = LLONG_MAX; if (d.size()) w = d.top().first; else w = LLONG_MAX; if (both.size()) u = both.top().first; else u = LLONG_MAX; if (min({x, y, z, w}) == LLONG_MAX) { cout << "-1" << '\n'; return; } if (a.size() and b.size() and both.size() and x + y <= u + min({x, y, z, w})) { int64_t val = a.top().second; t -= u; t += x + y; ans.push_back((val)); ans.push_back(b.top().second); int64_t q = both.top().second; ok[q] = true; a.pop(); b.pop(); c.push(both.top()); both.pop(); } else if (min({x, y, z, w}) == x) { t += x; ans.push_back(a.top().second); a.pop(); } else if (min({x, y, z, w}) == y) { t += y; ans.push_back(b.top().second); b.pop(); } else if (min({x, y, z, w}) == w) { t += w; ans.push_back(d.top().second); d.pop(); } else if (min({x, y, z, w}) == z) { t += z; ans.push_back(c.top().second); if (ok[ans.back()]) ok[ans.back()] = false; c.pop(); } cnt++; } print(); } void m_less() { int64_t x, y, cnt; cnt = 0; sort(prblm.begin(), prblm.end()); cnt = ans.size(); while (cnt > m) { if (c.size() == 0 or prblm.size() == 0) break; int64_t val = prblm.back().first; x = prblm.back().second.first; y = prblm.back().second.second; prblm.pop_back(); ok[x] = true; ok[y] = true; t -= val; t += c.top().first; ans.push_back(c.top().second); c.pop(); cnt--; } if (cnt > m) { cout << "-1"; return; } print(); } void solve() { int64_t i, x, y, z, cnt; cin >> n >> m >> k; for (i = 0; i < n; i++) { cin >> t >> x >> y; if (x == y and x == 1) c.push({t, i}); else if (x == 1 and y == 0) a.push({t, i}); else if (x == 0 and y == 1) b.push({t, i}); else d.push({t, i}); } t = 0; cnt = 0; while (1) { if (cnt >= k) break; if (c.size() == 0 and (a.size() == 0 or b.size() == 0)) break; if (a.size()) x = a.top().first; else x = LLONG_MAX; if (b.size()) y = b.top().first; else y = LLONG_MAX; if (c.size()) z = c.top().first; else z = LLONG_MAX; if (a.size() > 0 and b.size() > 0 and x + y <= z) { t += x + y; ans.push_back(a.top().second); ans.push_back(b.top().second); prblm.push_back({x + y, {a.top().second, b.top().second}}); a.pop(); b.pop(); cnt++; } else { t += z; ans.push_back(c.top().second); both.push({z, c.top().second}); c.pop(); cnt++; } } if (cnt < k) { cout << "-1" << '\n'; return; } if (ans.size() > m) { m_less(); return; } else { m_greater(); return; } } signed main() { std::ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); { solve(); } cerr << " Execution : " << (1.0 * clock()) / CLOCKS_PER_SEC << "s \n"; return 0; }
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast,no-stack-protector") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("unroll-loops") template <class T> inline T bigMod(T p, T e, T M) { T ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p) % M; p = (p * p) % M; } return (T)ret; } template <class T> inline T modInverse(T a, T M) { return bigMod(a, M - 2, M); } template <class T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template <class T> inline T lcm(T a, T b) { a = abs(a); b = abs(b); return (a / gcd(a, b)) * b; } template <class T> inline string int2String(T a) { ostringstream str; str << a; return str.str(); } int main() { int n, k; scanf("%d%d", &n, &k); vector<vector<int> > v(4); int t, a, b; for (int i = int(0); i < int(n); i++) { scanf("%d%d%d", &t, &a, &b); v[(a << 1) | b].push_back(t); } if (v[1].size() + v[3].size() < k || v[2].size() + v[3].size() < k) { puts("-1"); return 0; } for (int i = int(0); i < int(4); i++) sort(v[i].rbegin(), v[i].rend()); long long ans = 0; for (int _ = int(0); _ < int(k); _++) { if (v[3].size() && v[1].size() && v[2].size()) { if (v[3].back() <= v[1].back() + v[2].back()) { ans += v[3].back(); v[3].pop_back(); } else { ans += v[1].back(); v[1].pop_back(); ans += v[2].back(); v[2].pop_back(); } } else if (v[3].size()) { ans += v[3].back(); v[3].pop_back(); } else { ans += v[1].back(); v[1].pop_back(); ans += v[2].back(); v[2].pop_back(); } } printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int test = 1; while (test--) { int m, k, i; cin >> m >> k; int arr[3][m]; for (i = 0; i < m; i++) cin >> arr[0][i] >> arr[1][i] >> arr[2][i]; vector<int> a; vector<int> b; vector<int> c; for (i = 0; i < m; i++) { if (arr[1][i] == 1 && arr[2][i] == 1) c.push_back(arr[0][i]); else if (arr[1][i] == 1 && arr[2][i] == 0) a.push_back(arr[0][i]); else if (arr[1][i] == 0 && arr[2][i] == 1) b.push_back(arr[0][i]); } sort(a.begin(), a.end()); sort(b.begin(), b.end()); sort(c.begin(), c.end()); int idx_a = 0, idx_b = 0, idx_c = 0, total = 0, ans = 0; while (total < k) { if (idx_a == a.size() || idx_b == b.size()) { for (; idx_c < c.size(); idx_c++) { if (total == k) break; total++; ans += c[idx_c]; } break; } else if (idx_c == c.size()) { for (; idx_a < a.size() && idx_b < b.size(); idx_a++) { if (total == k) break; total++; ans += a[idx_a] + b[idx_b]; idx_b++; } break; } else { if (a[idx_a] + b[idx_b] <= c[idx_c]) { ans += a[idx_a] + b[idx_b]; total++; idx_a++; idx_b++; } else { ans += c[idx_c]; idx_c++; total++; } } } if (total < k) cout << "-1"; else cout << ans; cout << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxm = 2e3 + 23; const int maxn = 2e5 + 35; int n, k, t[maxn], vis[maxn], a, b, num, m; vector<int> ve[4], ee; vector<pair<int, int> > we; bool cmp(int i, int j) { return t[i] < t[j]; } struct node { int ii, tt; friend bool operator<(node a, node b) { return a.tt > b.tt; } }; int main() { scanf("%d %d %d", &n, &m, &k); priority_queue<node> se; for (int i = 1; i <= n; ++i) { scanf("%d %d %d", t + i, &a, &b); if (a && b) ve[2].push_back(i); else if (a) ve[0].push_back(i); else if (b) ve[1].push_back(i); else se.push((node){i, t[i]}); } sort((ve[0]).begin(), (ve[0]).end(), cmp); sort((ve[1]).begin(), (ve[1]).end(), cmp); sort((ve[2]).begin(), (ve[2]).end(), cmp); int x0 = 0, x1 = 0, x2 = 0, ans = 0; set<int> s; while (k) { if (x1 < (int)(ve[1]).size() && x0 < (int)(ve[0]).size()) { if (x2 < (int)(ve[2]).size()) { if (t[ve[2][x2]] < t[ve[0][x0]] + t[ve[1][x1]]) { ans += t[ve[2][x2]]; s.insert(ve[2][x2]); ee.push_back(ve[2][x2]); k -= 1; x2 += 1; num += 1; } else { ans += t[ve[0][x0]] + t[ve[1][x1]]; s.insert(ve[0][x0]); s.insert(ve[1][x1]); we.push_back(pair<int, int>(ve[0][x0], ve[1][x1])); k -= 1; x0 += 1; x1 += 1; num += 2; } } else { ans += t[ve[0][x0]] + t[ve[1][x1]]; we.push_back(pair<int, int>(ve[0][x0], ve[1][x1])); s.insert(ve[0][x0]); s.insert(ve[1][x1]); k -= 1; x0 += 1; x1 += 1; num += 2; } } else if (x2 < (int)(ve[2]).size()) { ans += t[ve[2][x2]]; s.insert(ve[2][x2]); ee.push_back(ve[2][x2]); k -= 1; x2 += 1; num += 1; } else break; } if (k > 0) return puts("-1"), 0; if (num < m) { for (int i = x0; i < (int)(ve[0]).size(); ++i) se.push((node){ve[0][i], t[ve[0][i]]}); for (int i = x1; i < (int)(ve[1]).size(); ++i) se.push((node){ve[1][i], t[ve[1][i]]}); for (int i = x2; i < (int)(ve[2]).size(); ++i) se.push((node){ve[2][i], t[ve[2][i]]}); int first = (int)(ee).size() - 1; while (num < m && x0 < (int)(ve[0]).size() && x1 < (int)(ve[1]).size() && first >= 0 && (int)(se).size() > 0) { while (vis[se.top().ii]) se.pop(); if ((int)(se).size() == 0) break; if (t[ee[first]] + (se.top()).tt < t[ve[0][x0]] + t[ve[1][x1]]) { ans += (se.top()).tt; vis[(se.top()).ii] = 1; s.insert((se.top()).ii); se.pop(); num += 1; } else { ans += t[ve[0][x0]] + t[ve[1][x1]]; ans -= t[ee[first]]; vis[ve[0][x0]] = 1; vis[ve[1][x1]] = 1; se.push((node){ee[first], t[ee[first]]}); s.insert(ve[0][x0]); s.insert(ve[1][x1]); s.erase(ee[first]); first -= 1; x0 += 1; x1 += 1; num += 1; } while (vis[ve[0][x0]]) x0 += 1; while (vis[ve[1][x1]]) x1 += 1; } while (num < m && !se.empty()) { ans += se.top().tt; s.insert(se.top().ii); se.pop(); num += 1; } } else { int first = (int)(we).size() - 1; while (num > m && x2 < (int)(ve[2]).size()) { s.erase(we[first].first); s.erase(we[first].second); ans = ans - t[we[first].first] - t[we[first].second]; first -= 1; s.insert(ve[2][x2]); ans += t[ve[2][x2]]; x2 += 1; num -= 1; } } if ((int)(s).size() == m) { printf("%d\n", ans); for (set<int>::iterator it = s.begin(); it != s.end(); ++it) printf("%d ", *it); } else puts("-1"); return 0; }
#include <bits/stdc++.h> using namespace std; const long long INF = 2000000000000; const long long MOD = 998244353; const int N = 100009; int main() { int n, k; cin >> n >> k; vector<long long> A, B, both; for (int i = 0; i < int(n); i++) { long long t, a, b; cin >> t >> a >> b; if (a == 1 && b == 1) both.push_back(t); else if (a == 1) A.push_back(t); else if (b == 1) B.push_back(t); } sort(A.begin(), A.end()); sort(B.begin(), B.end()); for (int i = 0; i < int(min(A.size(), B.size())); i++) { both.push_back(A[i] + B[i]); } sort(both.begin(), both.end()); long long tot = 0; if (int(both.size()) < k) { cout << -1 << "\n"; return 0; } for (int i = 0; i < int(k); i++) { tot += both[i]; } cout << tot << "\n"; }
#include <bits/stdc++.h> using namespace std; const long long int mod = 1e9 + 7; const long long int N = 2e5 + 9; long long int primes[6] = {1125899906842597, 1495921043, 1005985879, 1495921043, 1005985879, 1495921043}; vector<long long int> adj[N]; long long int parent[N]; long long int vis[N]; long long int level[N]; long long int dist[N]; long long int dp[N]; long long int hashing[N]; long long int ar[509][509]; long long int br[509][509]; long long int cr[509][509]; long long int multiply(long long int a, long long int b) { return ((a % mod) * (b % mod)) % mod; } long long int add(long long int a, long long int b) { return ((a % mod) + (b % mod)) % mod; } long long int sub(long long int a, long long int b) { return ((a % mod) - (b % mod) + mod) % mod; } long long int dx[] = {1, -1, 0, 0}; long long int dy[] = {0, 0, 1, -1}; long long int arr[200009]; long long int brr[200009]; long long int tim[200009]; long long int n, k; int main() { int start_s = clock(); ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int i, j, m, q, t, a, d, b, c, l, r, e, idx, ind, index, u, v, x, y, z, h, sz, sz1, sz2, mid, len, tot, prev, temp, curr, p; long long int res = 0, res1 = 0, res2 = 0, ans = 0, ans1 = 0, ans2 = 0, val = 0, val1 = 0, val2 = 0, rem = 0, diff = 0, cnt = 0, flag = 0, fl = 0, sum = 0, maxi = INT_MIN, mini = INT_MAX, total = 0; string str, str1, str2; char ch, ch1, ch2; cin >> n >> k; for (i = 1; i <= n; i++) { cin >> tim[i] >> arr[i] >> brr[i]; } priority_queue<long long int, vector<long long int>, greater<long long int> > common, lef, rig; for (i = 1; i <= n; i++) { if (arr[i] == 1 && brr[i] == 1) { common.push(tim[i]); } else if (arr[i] == 1 && brr[i] == 0) { lef.push(tim[i]); } else if (arr[i] == 0 && brr[i] == 1) { rig.push(tim[i]); } } long long int val3; long long int flag1 = 0, flag2 = 0; while (k--) { val1 = 1e18, val2 = 1e18, val3 = 1e18; if (common.size() == 0) { flag1 = 1; } if (common.size() != 0) val1 = common.top(); if (lef.size() == 0 || rig.size() == 0) { flag2 = 1; } if (lef.size() != 0) val2 = lef.top(); if (rig.size() != 0) val3 = rig.top(); if (flag1 == 1 && flag2 == 1) { return cout << -1, 0; } if (val1 <= val2 + val3) { ans += val1; common.pop(); } else { ans += val2 + val3; lef.pop(); rig.pop(); } } cout << ans; }
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const int inf = 1e9 + 7; const double eps = 1e-6; long long qpow(long long a, long long b, long long m) { long long r = 1; a %= m; for (; b; b >>= 1) { if (b & 1) r = r * a % m; a = a * a % m; } return (r + m) % m; } const double pi = acos(-1); long long ar[202030], res = 0, p[4] = {0}; bool cmp(long long a, long long b) { return ar[a] < ar[b]; } int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); long long n, m, k, tot = 0; cin >> n >> m >> k; vector<long long> v[4]; set<long long> ans; for (long long i = 0; i < n; i++) { long long p, q; cin >> ar[i] >> p >> q; v[2 * p + q].push_back(i); } for (int i = 0; i < 4; i++) { sort(v[i].begin(), v[i].end(), cmp); } for (int i = 0; i < min((long long)v[3].size(), k); i++) ans.insert(v[3][i]), p[3]++; long long sz = min(v[1].size(), v[2].size()); if ((long long)v[3].size() + sz < k) { cout << -1; return 0; } for (int i = 0; i < k - (long long)v[3].size(); i++) { ans.insert(v[1][p[1]]), p[1]++; ans.insert(v[2][p[2]]), p[2]++; } long long cnt = 10; while ((long long)ans.size() < m) { long long mn = 1e7, id = -1; for (long long i = 0; i < 4; i++) { if (p[i] == (long long)v[i].size()) continue; if (mn > ar[v[i][p[i]]]) mn = ar[v[i][p[i]]], id = i; } if (p[3] > 0 && p[1] < v[1].size() && p[2] < v[2].size() && ar[v[1][p[1]]] + ar[v[2][p[2]]] < mn + ar[v[3][p[3] - 1]]) { ans.erase(v[3][--p[3]]); ans.insert(v[1][p[1]++]); ans.insert(v[2][p[2]++]); } else ans.insert(v[id][p[id]++]); } if ((long long)ans.size() != m) { cout << -1; return 0; } long long sum = 0; for (auto i : ans) sum += ar[i]; cout << sum << '\n'; for (auto i : ans) cout << i + 1 << ' '; return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 2e9 + 1; void updateSt(set<pair<int, int>> &st, set<pair<int, int>> &fr, int &sum, int need) { need = max(need, 0); while (true) { bool useful = false; while (int((st).size()) > need) { sum -= st.rbegin()->first; fr.insert(*st.rbegin()); st.erase(prev(st.end())); useful = true; } while (int((st).size()) < need && int((fr).size()) > 0) { sum += fr.begin()->first; st.insert(*fr.begin()); fr.erase(fr.begin()); useful = true; } while (!st.empty() && !fr.empty() && fr.begin()->first < st.rbegin()->first) { sum -= st.rbegin()->first; sum += fr.begin()->first; fr.insert(*st.rbegin()); st.erase(prev(st.end())); st.insert(*fr.begin()); fr.erase(fr.begin()); useful = true; } if (!useful) break; } } int main() { int n, m, k; cin >> n >> m >> k; vector<pair<int, int>> times[4]; vector<int> sums[4]; for (int i = 0; i < n; ++i) { int t, a, b; cin >> t >> a >> b; times[a * 2 + b].push_back({t, i}); } for (int i = 0; i < 4; ++i) { sort(times[i].begin(), times[i].end()); sums[i].push_back(0); for (auto it : times[i]) { sums[i].push_back(sums[i].back() + it.first); } } int ans = INF; int pos = INF; set<pair<int, int>> st; set<pair<int, int>> fr; int sum = 0; vector<int> res; for (int iter = 0; iter < 2; ++iter) { st.clear(); fr.clear(); sum = 0; int start = 0; while (k - start >= int((sums[1]).size()) || k - start >= int((sums[2]).size()) || m - start - (k - start) * 2 < 0) { ++start; } if (start >= int((sums[3]).size())) { cout << -1 << endl; return 0; } int need = m - start - (k - start) * 2; for (int i = 0; i < 3; ++i) { for (int p = int((times[i]).size()) - 1; p >= (i == 0 ? 0 : k - start); --p) { fr.insert(times[i][p]); } } updateSt(st, fr, sum, need); for (int cnt = start; cnt < (iter == 0 ? int((sums[3]).size()) : pos + 1); ++cnt) { if (k - cnt >= 0) { if (cnt + (k - cnt) * 2 + int((st).size()) == m) { if (ans > sums[3][cnt] + sums[1][k - cnt] + sums[2][k - cnt] + sum) { ans = sums[3][cnt] + sums[1][k - cnt] + sums[2][k - cnt] + sum; pos = cnt; } } } else { if (cnt + int((st).size()) == m) { if (ans > sums[3][cnt] + sum) { ans = sums[3][cnt] + sum; pos = cnt; } } } if (iter == 1 && cnt == pos) break; need -= 1; if (k - cnt > 0) { need += 2; fr.insert(times[1][k - cnt - 1]); fr.insert(times[2][k - cnt - 1]); } updateSt(st, fr, sum, need); } if (iter == 1) { for (int i = 0; i < pos; ++i) res.push_back(times[3][i].second); for (int i = 0; i < k - pos; ++i) { res.push_back(times[1][i].second); res.push_back(times[2][i].second); } for (auto [value, position] : st) res.push_back(position); } } cout << ans << endl; for (auto it : res) cout << it + 1 << " "; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAX = 1e4 + 9; int tree[(MAX << 2)][2], val, idx; void upd(int low, int high, int pos) { if (low == high) { tree[pos][1] += val; tree[pos][0] += val * low; return; } int mid = ((low + high) >> 1); if (idx <= mid) upd(low, mid, (pos << 1)); else upd(mid + 1, high, (pos << 1 | 1)); tree[pos][0] = tree[(pos << 1)][0] + tree[(pos << 1 | 1)][0]; tree[pos][1] = tree[(pos << 1)][1] + tree[(pos << 1 | 1)][1]; } int qwr(int low, int high, int pos, int rest) { if (tree[pos][1] == rest) { return tree[pos][0]; } if (low == high) { return rest * low; } int mid = ((low + high) >> 1); if (tree[(pos << 1)][1] >= rest) { return qwr(low, mid, (pos << 1), rest); } else { return tree[(pos << 1)][0] + qwr(mid + 1, high, (pos << 1 | 1), rest - tree[(pos << 1)][1]); } } int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); vector<pair<int, int> > a, b; vector<pair<pair<int, int>, long long> > both; vector<pair<int, int> > non; for (int i = 1; i <= n; ++i) { int t, x, y; scanf("%d%d%d", &t, &x, &y); if (x && !y) a.push_back({t, i}); else if (!x && y) b.push_back({t, i}); else if (x && y) both.push_back({{t, i}, t}); else { non.push_back({t, i}); val = 1, idx = t; upd(1, MAX - 1, 1); } } sort(a.begin(), a.end()); sort(b.begin(), b.end()); sort(both.begin(), both.end()); for (int i = 0; i < a.size(); ++i) { val = 1, idx = a[i].first; upd(1, MAX - 1, 1); } for (int i = 0; i < b.size(); ++i) { val = 1, idx = b[i].first; upd(1, MAX - 1, 1); } vector<pair<pair<int, int>, long long> > tempBoth = both; while (both.size() > k) { val = 1, idx = both.back().first.first; both.pop_back(); upd(1, MAX - 1, 1); } for (int i = 1; i < both.size(); ++i) { both[i].second += both[i - 1].second; } long long ans = 1e18 + 18, totA = 0, totB = 0; int cnt = 0; bool findAnswer = 0; for (int i = 0; i <= min(k, (int)min(a.size(), b.size())); ++i) { if (i) { val = -1, idx = a[i - 1].first; upd(1, MAX - 1, 1); totA += a[i - 1].first; val = -1, idx = b[i - 1].first; upd(1, MAX - 1, 1); totB += b[i - 1].first; } int needK = k - i; if (needK > both.size()) { continue; } int rest = m - i * 2 - needK; if (rest < 0 || tree[1][1] < rest) { continue; } findAnswer = 1; long long sol = totA + totB + (needK > 0 ? both[needK - 1].second : 0) + (rest ? qwr(1, MAX - 1, 1, rest) : 0); if (sol < ans) { ans = sol; cnt = i; } if (!both.empty()) { val = -1, idx = both.back().first.first; both.pop_back(); upd(1, MAX - 1, 1); } } if (!findAnswer) { printf("-1"); return 0; } printf("%d\n", ans); for (int i = 0; i < cnt; ++i) { printf("%d %d ", a[i].second, b[i].second); } int needK = k - cnt; for (int i = 0; i < needK; ++i) { printf("%d ", tempBoth[i].first.second); } int rest = m - 2 * cnt - needK; if (rest < 0) { return 0; } set<pair<int, int> > s; for (int i = cnt; i < a.size(); ++i) { s.insert(a[i]); } for (int i = cnt; i < b.size(); ++i) { s.insert(b[i]); } for (int i = needK; i < tempBoth.size(); ++i) { s.insert(tempBoth[i].first); } for (int i = 0; i < non.size(); ++i) { s.insert(non[i]); } assert(rest <= s.size()); while (rest && !s.empty()) { printf("%d ", s.begin()->second); --rest; s.erase(s.begin()); } return 0; }
#include <bits/stdc++.h> using namespace std; long long n; long long k; struct book { bool a; bool b; int time; }; book B[1000000]; bool compare(book x, book y) { return x.time < y.time; } void solve() { cin >> n; cin >> k; int x, y, z; int as = 0; int bs = 0; int mx = 0; for (int i = 0; i < n; i++) { cin >> x >> y >> z; B[i].a = y; as += y; bs += z; B[i].b = z; B[i].time = x; mx = max(mx, B[i].time); } if (as < k || bs < k) { cout << -1 << endl; return; } sort(B, B + n, compare); long long tot = 0; int ak = 0; int bk = 0; stack<int> a; stack<int> b; for (int i = 0; i < n; i++) { if (B[i].a && B[i].b) continue; if (ak < k && B[i].a) { ak++; a.push(i); tot += B[i].time; } if (bk < k && B[i].b) { bk++; tot += B[i].time; b.push(i); } } int lst = 0; int used[1000000] = {0}; for (int i = 0; i < n; i++) { if (ak < k && B[i].a && B[i].b) { ak++; tot += B[i].time; if (b.size() && bk == k) { tot -= B[b.top()].time; b.pop(); } if (bk < k) bk++; lst = i; used[i] = 1; } else if (bk < k && B[i].a && B[i].b) { bk++; tot += B[i].time; if (a.size() && ak == k) { tot -= B[a.top()].time; a.pop(); } lst = i; used[i] = 1; if (ak < k) ak++; } } for (int i = 0; i < n; i++) { if (a.size() == 0 || b.size() == 0) break; if (used[i]) continue; int m = a.top(); int n = b.top(); if (B[i].a && B[i].b) { if (B[i].time < B[m].time + B[n].time) { tot = tot - (B[m].time + B[n].time); tot += B[i].time; a.pop(); b.pop(); } } } cout << tot << endl; return; } int main() { int t; t = 1; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; template <typename T1, typename T2> inline void chkmin(T1 &x, const T2 &y) { if (x > y) x = y; } template <typename T1, typename T2> inline void chkmax(T1 &x, const T2 &y) { if (x < y) x = y; } int n, m, k; vector<pair<int, int>> a[4]; set<pair<int, int>> L, R; int sumSet; void read() { cin >> n >> m >> k; for (int i = 0; i < n; i++) { int t, f1, f2; cin >> t >> f1 >> f2; a[f1 * 2 + f2].push_back({t, i}); } for (int i = 0; i <= 3; i++) sort(a[i].begin(), a[i].end()); } void relax(int sz) { sz = max(sz, 0); while (L.size() > sz) { auto x = *(--L.end()); L.erase(--L.end()); sumSet -= x.first; R.insert(x); } while (L.size() < sz) { if (R.empty()) break; auto x = *(R.begin()); R.erase(R.begin()); L.insert(x); sumSet += x.first; } } void add(pair<int, int> a) { if (R.empty()) { L.insert(a); sumSet += a.first; } else if (L.empty()) { R.insert(a); } else if (*(R.begin()) < a) { R.insert(a); } else { L.insert(a); sumSet += a.first; } } int ans; vector<int> fans; void run() { ans = 2e9 + 228 + 1337; for (int it = 0; it < 2; it++) { L.clear(); R.clear(); sumSet = 0; int sum = 0; int top1 = (int)a[1].size() - 1; int top2 = (int)a[2].size() - 1; for (auto i : a[0]) add(i); for (auto i : a[1]) sum += i.first; for (auto i : a[2]) sum += i.first; for (int i = 0; i <= min(m, (int)a[3].size()); i++) { if (i > 0) sum += a[3][i - 1].first; while (top1 >= 0 && top1 + 1 + i > k) { sum -= a[1][top1].first; add(a[1][top1--]); } while (top2 >= 0 && top2 + 1 + i > k) { sum -= a[2][top2].first; add(a[2][top2--]); } if (min(top1, top2) + 1 + i < k) continue; int sz = m - i - (top1 + 1) - (top2 + 1); if (sz < 0) continue; if (L.size() + R.size() < sz) continue; relax(sz); chkmin(ans, sum + sumSet); if (!it || sum + sumSet != ans) continue; for (auto j : L) { fans.push_back(j.second); } for (int j = 0; j <= top1; j++) { fans.push_back(a[1][j].second); } for (int j = 0; j <= top2; j++) { fans.push_back(a[2][j].second); } for (int j = 0; j < i; j++) { fans.push_back(a[3][j].second); } return; } } cout << -1 << endl; exit(0); } void write() { cout << ans << endl; sort(fans.begin(), fans.end()); for (auto i : fans) { cout << i + 1 << " "; } cout << endl; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); read(); run(); write(); return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 2e9 + 1; long long int m = 1000000007; string toString(char x) { string s(1, x); return s; } long long int pow1(long long int x, long long int y) { long long int temp; if (y == 0) return 1; temp = pow1(x, y / 2); if (y % 2 == 0) return (temp % m * temp % m) % m; else return ((x % m * temp % m) % m * temp % m) % m; } int main() { int n, k; cin >> n >> k; vector<int> times[4]; vector<int> sums[4]; for (int i = 0; i < n; ++i) { int t, a, b; cin >> t >> a >> b; times[a * 2 + b].push_back(t); } for (int i = 0; i < 4; ++i) { sort(times[i].begin(), times[i].end()); sums[i].push_back(0); for (auto it : times[i]) { sums[i].push_back(sums[i].back() + it); } } int ans = INF; for (int cnt = 0; cnt < min(k + 1, int(sums[3].size())); ++cnt) { if (k - cnt < int(sums[1].size()) && k - cnt < int(sums[2].size())) { ans = min(ans, sums[3][cnt] + sums[1][k - cnt] + sums[2][k - cnt]); } } if (ans == INF) ans = -1; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; struct jg { int x, y, z; }; jg a[200001]; int n, k, t1, t2, t3, mx, ans[200001], sum[200001], ans1[200001]; bool bj1[200001], bj2; int main() { int i, j; cin >> n >> k; for (i = 1; i <= n; i++) { scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].z); if (a[i].y == 1 && a[i].z == 0) { t1++; ans[t1] = a[i].x; } if (a[i].y == 0 && a[i].z == 1) { t2++; sum[t2] = a[i].x; } if (a[i].y == 1 && a[i].z == 1) { t3++; ans1[t3] = a[i].x; } } sort(ans + 1, ans + t1 + 1); sort(sum + 1, sum + t2 + 1); for (i = 1; i <= min(t1, t2); i++) { t3++; ans1[t3] = ans[i] + sum[i]; } if (t3 < k) { cout << -1 << endl; return 0; } sort(ans1 + 1, ans1 + t3 + 1); for (i = 1; i <= k; i++) { mx += ans1[i]; } cout << mx << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using pi = pair<int, int>; using pl = pair<ll, ll>; const ll N = 3e5 + 10; const ll INF = 1e10; const ll M = 1e3 + 1; const ll L = 31; const ll mod = 998244353; ll solve() { ll n, k; cin >> n >> k; ll t, a, b; ll ans = 0; multiset<ll> alice, bob, both; for (ll i = 0; i < n; i++) { cin >> t >> a >> b; if (a == 1 && b == 1) { both.insert(t); } else if (a == 1) { alice.insert(t); } else if (b == 1) { bob.insert(t); } } while (both.size() > 0 && alice.size() > 0 && bob.size() > 0 && k > 0) { ll a = *(alice.begin()); ll b = *(bob.begin()); ll c = *(both.begin()); if (a + b < c) { ans += (a + b); alice.erase(alice.begin()); bob.erase(bob.begin()); } else { ans += c; both.erase(both.begin()); } k--; } if (k == 0) { cout << ans << '\n'; return 0; } else { if (alice.size() == 0 || bob.size() == 0) { if (both.size() < k) { cout << -1 << '\n'; } else { while (k > 0) { ans += (*(both.begin())); both.erase(both.begin()); k--; } cout << ans << '\n'; return 0; } } else { if (alice.size() < k || bob.size() < k) { cout << -1 << '\n'; return 0; } else { while (k > 0) { ans += (*(alice.begin())); alice.erase(alice.begin()); ans += (*(bob.begin())); bob.erase(bob.begin()); k--; } cout << ans << '\n'; return 0; } } } return 0; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; void printVector(vector<string> &v) { for (auto x : v) cout << x << "\n"; } void printVector(vector<int> &v) { for (auto x : v) cout << x << " "; cout << "\n"; } int main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(0); int t; t = 1; while (t--) { int n, k; cin >> n >> k; vector<int> v1, v2, v3; int ca = 0, cb = 0; for (int i = 0; i < n; i++) { int tm, a, b; cin >> tm >> a >> b; if (a == 1) { ca++; if (b == 1) { v3.push_back(tm); cb++; } else { v1.push_back(tm); } } else if (b == 1) { v2.push_back(tm); cb++; } } if (ca < k || cb < k) { cout << -1 << "\n"; continue; } sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); sort(v3.begin(), v3.end()); int i1 = 0, i2 = 0, i3 = 0; int c1 = 0, c2 = 0; int ans = 0; while (c1 < k || c2 < k) { if (c1 < k && c2 < k) { int t1 = 2e4 + 1; if (i1 < v1.size()) t1 = v1[i1]; int t2 = 2e4 + 1; if (i2 < v2.size()) t2 = v2[i2]; int t3 = 2e4 + 1; if (i3 < v3.size()) t3 = v3[i3]; if (t3 <= (t1 + t2)) { ans += t3; c1++; c2++; i3++; } else { ans += t1; ans += t2; c1++; c2++; i1++; i2++; } } else if (c1 < k) { int t1 = 2e4 + 1; if (i1 < v1.size()) t1 = v1[i1]; int t3 = 2e4 + 1; if (i3 < v3.size()) t3 = v3[i3]; if (t3 <= t1) { ans += t3; c1++; c2++; i3++; } else { ans += t1; c1++; i1++; } } else { int t2 = 2e4 + 1; if (i2 < v2.size()) t2 = v2[i2]; int t3 = 2e4 + 1; if (i3 < v3.size()) t3 = v3[i3]; if (t3 <= t2) { ans += t3; c1++; c2++; i3++; } else { ans += t2; c2++; i2++; } } } cout << ans << "\n"; } }
#include <bits/stdc++.h> using namespace std; long long time(vector<int> l, int il, vector<int> b, int ib, vector<int> r, int ir, int k) { long long ans = 0; int left = k, right = k; int index_both = 0; int index_right = 0; int index_left = 0; while (left != 0 && right != 0) { if (index_both == ib) { if (index_right == ir || index_left == il) { return -1; } else { ans += l[index_left]; ans += r[index_right]; index_left++; index_right++; left--; right--; } } else { if (index_right == ir || index_left == il) { ans += b[index_both]; index_both++; left--; right--; } else { long long b1 = b[index_both]; long long t1 = l[index_left] + r[index_right]; if (b1 <= t1) { ans += b1; index_both++; left--; right--; } else { ans += t1; index_left++; index_right++; left--; right--; } } } } while (left > 0) { if (index_both == ib) { if (index_left == il) return -1; else { ans += l[index_left]; index_left++; left--; } } else { if (index_left == il) { ans += b[index_both]; index_both++; left--; } else { long long b1 = b[index_both]; long long l1 = l[index_left]; if (b1 <= l1) { ans += b1; index_both++; left--; } else { ans += l1; index_left++; left--; } } } } while (right > 0) { if (index_both == ib) { if (index_right == ir) return -1; else { ans += r[index_right]; index_right++; right--; } } else { if (index_right == ir) { ans += b[index_both]; index_both++; right--; } else { long long b1 = b[index_both]; long long r1 = r[index_right]; if (b1 <= r1) { ans += b1; index_both++; right--; } else { ans += r1; index_right++; right--; } } } } return ans; } int main() { int n, k; cin >> n >> k; vector<int> l(n); vector<int> r(n); vector<int> b(n); int ib = 0, il = 0, ir = 0; int t, ai, bi; for (int i = 0; i < n; i++) { cin >> t >> ai >> bi; if (ai == 1 && bi == 1) { b[ib] = t; ib++; } else if (ai == 1) { l[il] = t; il++; } else if (bi == 1) { r[ir] = t; ir++; } } vector<int>::iterator it = l.begin(); advance(it, il); sort(l.begin(), it); it = r.begin(); advance(it, ir); sort(r.begin(), it); it = b.begin(); advance(it, ib); sort(b.begin(), it); cout << time(l, il, b, ib, r, ir, k) << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; long long power(long long x, long long y, long long p) { long long res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } inline long long add(long long a, long long b) { return (a + b) % 1000000007; } inline long long mul(long long a, long long b) { return (a * b) % 1000000007; } long long f[4000010], iv[4000010]; long long C(long long n, long long r) { return mul(f[n], mul(iv[r], iv[n - r])); } void prep_fac() { f[0] = 1; for (int i = 1; i < 4000010; i++) f[i] = mul(i, f[i - 1]); iv[4000010 - 1] = power(f[4000010 - 1], 1000000007 - 2, 1000000007); for (long long i = 4000010 - 2; i >= 0; --i) iv[i] = mul(i + 1, iv[i + 1]); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, k; cin >> n >> k; int c1 = 0, c2 = 0; vector<long long> v[4]; v[1].push_back(0), v[2].push_back(0), v[3].push_back(0); for (int i = 0; i < n; i++) { int t, a, b; cin >> t >> a >> b; v[2 * a + b].push_back(t); } for (int i = 1; i < 4; i++) { sort(v[i].begin(), v[i].end()); for (int j = 1; j < v[i].size(); j++) { v[i][j] += v[i][j - 1]; } } long long ans = INT_MAX; for (int i = 0; i < v[3].size(); i++) { if (v[2].size() > k - i && v[1].size() > k - i) ans = min(v[3][i] + v[2][k - i] + v[1][k - i], ans); } if (ans == INT_MAX) ans = -1; cout << ans; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, k, i, ok, s = 0, sum = 0, ans = 0; multiset<long long> pi1; vector<long long> pi2, pi3; multimap<long long, long long>::iterator it; cin >> n >> ok; long long a, b, c; for (i = 0; i < n; i++) { cin >> a >> b >> c; if (b == 1 && c == 1) { pi1.insert(a); s++; sum++; } else if (b == 1) { pi2.push_back(a); s++; } else if (c == 1) { pi3.push_back(a); sum++; } } sort(pi2.begin(), pi2.end()); sort(pi3.begin(), pi3.end()); long long l = min(pi2.size(), pi3.size()); if (s < ok || sum < ok) cout << "-1" << endl; else { for (i = 0; i < l; i++) { pi1.insert(pi2[i] + pi3[i]); } long long ans1 = 0; for (auto kk : pi1) { ans1 += kk; ok--; if (ok == 0) break; } cout << ans1 << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int n, k; scanf("%d %d", &n, &k); long long ans = 0; priority_queue<int, vector<int>, greater<int>> q1, q2, q3; int ct1 = 0; int ct2 = 0; for (int i = 1; i <= n; i++) { long long t; int a, b; scanf("%lld %d %d", &t, &a, &b); if (a == 1) ct1++; if (b == 1) ct2++; if (a == 1 && b == 0) { q1.push(t); } if (a == 0 && b == 1) { q2.push(t); } if (a == 1 && b == 1) { q3.push(t); } } int hehe = min(ct1, ct2); if (hehe < k) { printf("-1"); return 0; } for (int kk = 1; kk <= k; kk++) { if (!q1.empty() && !q2.empty()) { int now = q1.top() + q2.top(); if (!q3.empty()) { int now2 = q3.top(); if (now < now2) { ans += now; q1.pop(); q2.pop(); } else { ans += now2; q3.pop(); } } else { ans += now; q1.pop(); q2.pop(); } } else { if (!q3.empty()) { int now2 = q3.top(); ans += now2; q3.pop(); } } } printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; int n, k, m, x, y, z; int ans; vector<int> a[2][2]; vector<pair<int, int> > f[2][2]; int id[2][2]; int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < n; i++) { scanf("%d%d%d", &x, &y, &z); a[y][z].push_back(x); f[y][z].push_back({x, i + 1}); } for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) { sort(a[i][j].begin(), a[i][j].end()); a[i][j].push_back(1000000); sort(f[i][j].begin(), f[i][j].end()); } int ans = -1, sum = 0, u = 0; while (u < a[1][1].size()) { if (u > m) { u++; continue; } int r = max(k - u, 0); if (r >= a[1][0].size() || r >= a[0][1].size()) { u++; continue; } if ((u + 2 * r) > m) { u++; continue; } if (a[1][0].size() + a[0][1].size() + a[0][0].size() + u < m + 3) { u++; continue; } break; } if (u >= a[1][1].size()) { cout << -1; return 0; } int r = max(k - u, 0); int s1 = r, s2 = r, s3 = 0; while (u + s1 + s2 + s3 < m) { int d = min(min(a[1][0][s1], a[0][1][s2]), a[0][0][s3]); if (d > 10000) { cout << -1; return 0; } if (d == a[1][0][s1]) { s1++; continue; } if (d == a[0][1][s2]) { s2++; continue; } if (d == a[0][0][s3]) { s3++; continue; } } for (int i = 0; i < u; i++) sum += a[1][1][i]; for (int i = 0; i < s1; i++) sum += a[1][0][i]; for (int i = 0; i < s2; i++) sum += a[0][1][i]; for (int i = 0; i < s3; i++) sum += a[0][0][i]; ans = sum; int q1 = s1, q2 = s2, q3 = s3, q4 = u; s1--; s2--; s3--; while (u + 1 < a[1][1].size()) { sum += a[1][1][u]; u++; if (s1 == -1 && s2 == -1 && s3 == -1) break; int d = 0, pp = 0; if ((s1 != -1)) { d += a[1][0][s1]; pp++; s1--; } if ((s2 != -1)) { d += a[0][1][s2]; pp++; s2--; } if ((s3 != -1)) { d += a[0][0][s3]; pp++; s3--; } sum -= d; for (int i = 0; i < pp - 1; i++) { int gg = min(min(a[1][0][s1 + 1], a[0][1][s2 + 1]), a[0][0][s3 + 1]); if (gg > 10000) { cout << -1; return 0; } if (gg == a[1][0][s1 + 1]) { sum += a[1][0][s1 + 1]; s1++; continue; } if (gg == a[0][1][s2 + 1]) { sum += a[0][1][s2 + 1]; s2++; continue; } if (gg == a[0][0][s3 + 1]) { sum += a[0][0][s3 + 1]; s3++; continue; } } if (sum < ans) { ans = sum; q4 = u; q3 = s3 + 1; q2 = s2 + 1; q1 = s1 + 1; } } printf("%d\n", ans); for (int i = 0; i < q4; i++) printf("%d ", f[1][1][i].second); for (int i = 0; i < q3; i++) printf("%d ", f[0][0][i].second); for (int i = 0; i < q2; i++) printf("%d ", f[0][1][i].second); for (int i = 0; i < q1; i++) printf("%d ", f[1][0][i].second); return 0; }
#include <bits/stdc++.h> using namespace std; void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << '\n'; err(++it, args...); } long long powMod(long long x, long long y) { long long p = 1; while (y) { if (y % 2) { p = (p * x) % ((long long)1e9 + 7); } y /= 2; x = (x * x) % ((long long)1e9 + 7); } return p; } long long CpowMod(long long x, long long y, long long w) { long long p = 1; while (y) { if (y % 2) { p = (p * x) % w; } y /= 2; x = (x * x) % w; } return p; } long long invMod(long long x) { return powMod(x, ((long long)1e9 + 7) - 2); } long long CinvMod(long long x, long long w) { return CpowMod(x, w - 2, w); } long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } void solve() { long long n, k; cin >> n >> k; multiset<long long> q1, q2, qq, q3, q4; for (int i = 0; i <= n - 1; i++) { long long x, y, t; cin >> t >> x >> y; if (x == 1 && y == 1) { qq.insert(t); } else if (x == 1) { q1.insert(t); } else if (y == 1) { q2.insert(t); } } if ((long long)qq.size() + (long long)q1.size() < k || (long long)qq.size() + (long long)q2.size() < k) { cout << -1; return; } long long mb = min((long long)qq.size() - 1, k - 1); for (int i = 0; i <= mb; i++) { auto it = *qq.begin(); q3.insert(it); qq.erase(qq.find(it)); } long long ap = k - (long long)q3.size(); {}; for (int i = 0; i <= ap - 1; i++) { auto it1 = *q1.begin(); auto it2 = *q2.begin(); q4.insert(it1); q4.insert(it2); q1.erase(q1.find(it1)); q2.erase(q2.find(it2)); } while ((long long)q3.size() && (long long)q1.size() && (long long)q2.size()) { auto it1 = *q1.begin(); auto it2 = *q2.begin(); auto it3 = *q3.rbegin(); if (it1 + it2 < it3) { q4.insert(it1); q4.insert(it2); q1.erase(q1.find(it1)); q2.erase(q2.find(it2)); q3.erase(q3.find(it3)); } else { break; } } long long sum = 0; for (auto &it : q3) { sum += it; } for (auto &it : q4) { sum += it; } cout << sum; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(20); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, i, j, k, t, p, q; cin >> n >> k; multiset<long long> a, b, d; long long al = 0, bl = 0; for (i = 0; i < n; i++) { cin >> t >> p >> q; if (p && q) { d.insert(t); al++; bl++; } else if (p) { a.insert(t); al++; } else if (q) { b.insert(t); bl++; } } if (al < k || bl < k) { cout << -1 << "\n"; return 0; } al = k, bl = k; long long ans = 0; auto ita = a.begin(); auto itb = b.begin(); auto itd = d.begin(); while (al > 0 || bl > 0) { if (al > 0 && bl > 0) { if (ita == a.end() || itb == b.end()) { ans += *itd; itd++; al--; bl--; } else { if (itd != d.end()) { if (*ita + *itb < *itd) { ans += *ita + *itb; ita++; itb++; al--; bl--; } else { ans += *itd; itd++; al--; bl--; } } else { ans += *ita + *itb; ita++; itb++; al--; bl--; } } } else if (al > 0) { if (ita == a.end()) { ans += *itd; itd++; al--; bl--; } else if (itd == d.end()) { ans += *ita; al--; ita++; } else { if (*ita < *itd) { ans += *ita; al--; ita++; } else { ans += *itd; itd++; al--; bl--; } } } else if (bl > 0) { if (itb == b.end()) { ans += *itd; itd++; al--; bl--; } else if (itd == d.end()) { ans += *itb; bl--; itb++; } else { if (*itb < *itd) { ans += *itb; bl--; itb++; } else { ans += *itd; itd++; al--; bl--; } } } } cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; vector<ll> L, R, M; ll p(const vector<ll>& v, ll c) { if (c <= 0) return 0; return v[c - 1]; } const ll INF = 5e10; int main() { ios::sync_with_stdio(false); cin.tie(0); ll N, K; cin >> N >> K; for (int i = 0; i < N; ++i) { ll time, l, r; cin >> time >> l >> r; if ((l + r) == 2) { M.push_back(time); } else if (l + r == 1) { if (l) L.push_back(time); else R.push_back(time); } } sort(L.begin(), L.end()); sort(R.begin(), R.end()); sort(M.begin(), M.end()); for (int i = 1; i < ((int)(L.size())); ++i) { L[i] += L[i - 1]; } for (int i = 1; i < ((int)(R.size())); ++i) { R[i] += R[i - 1]; } for (int i = 1; i < ((int)(M.size())); ++i) { M[i] += M[i - 1]; } ll ans = INF; for (int m = 0; m <= ((int)(M.size())); ++m) { ll LR = K - m; if (((int)(L.size())) < LR || ((int)(R.size())) < LR) { continue; } ll t = p(M, m) + p(L, LR) + p(R, LR); ans = min(ans, t); } cout << (ans == INF ? -1 : ans); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; int n, k; cin >> n >> k; vector<int> t(n), a(n), b(n); for (int i = 0; i < n; i++) cin >> t[i] >> a[i] >> b[i]; vector<vector<int>> elements(4); for (int i = 0; i < n; i++) elements[2 * a[i] + b[i]].push_back(t[i]); for (int i = 0; i < 4; i++) sort(elements[i].begin(), elements[i].end()); vector<vector<int>> prefix(4); for (int i = 0; i < 4; i++) { prefix[i].push_back(0); for (auto &x : elements[i]) prefix[i].push_back(prefix[i].back() + x); } int ans = INT_MAX; for (int i = 0; i <= min((int)elements[3].size(), k); i++) { int req = k - i; if (prefix[1].size() <= req || prefix[2].size() <= req) continue; int cur = prefix[1][req] + prefix[2][req] + prefix[3][i]; ans = min(ans, cur); } ans == INT_MAX ? cout << "-1" << endl : cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; long long n, m, p, q; long long k; pair<int, pair<int, int> > a[2000005]; int main() { cin >> n >> k; for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second.first >> a[i].second.second; sort(a, a + n); vector<int> alice, bob; int Alice = 0, Bob = 0; long long res = 0; for (int i = 0; i < n; i++) { if (a[i].second.first == 0 && a[i].second.second == 0) continue; if (Alice >= k && Bob >= k) { if (a[i].second.first == 1 && a[i].second.second == 1) if (alice.size() > 0 && bob.size() > 0) { if (alice[alice.size() - 1] + bob[bob.size() - 1] > a[i].first) { res += a[i].first - alice[alice.size() - 1] - bob[bob.size() - 1]; alice.pop_back(); bob.pop_back(); } } } else { Alice += a[i].second.first; Bob += a[i].second.second; res += a[i].first; if (a[i].second.first == 1 && a[i].second.second == 0) alice.push_back(a[i].first); if (a[i].second.first == 0 && a[i].second.second == 1) bob.push_back(a[i].first); if (alice.size() > 0 && Alice > k) { res -= alice[alice.size() - 1]; alice.pop_back(); Alice--; } if (bob.size() > 0 && Bob > k) { res -= bob[bob.size() - 1]; bob.pop_back(); Bob--; } } } if (Alice < k || Bob < k) cout << -1; else cout << res; }
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 7; int n, m, k; struct node { int t, id; bool operator<(const node &oth) const { return t < oth.t; } }; vector<node> sa[4]; int sz[4], ans; int tsz[4], tans; int main() { scanf("%d%d%d", &n, &m, &k); int t, a, b; for (int i = 1; i <= n; ++i) { scanf("%d%d%d", &t, &a, &b); sa[(b << 1) + a].push_back(node{t, i}); } for (int i = 0; i < 4; ++i) sort(sa[i].begin(), sa[i].end()); ans = -1; tans = 0; for (node p : sa[3]) { tans += p.t; } tsz[3] = sa[3].size(); for (int i = sa[3].size(); i >= 0; --i) { while (tsz[1] + tsz[3] < k) { if (tsz[1] >= sa[1].size()) break; tans += sa[1][tsz[1]++].t; } while (tsz[2] + tsz[3] < k) { if (tsz[2] >= sa[2].size()) break; tans += sa[2][tsz[2]++].t; } int rest = m - tsz[0] - tsz[1] - tsz[2] - tsz[3]; while (rest < 0 && tsz[0] > 0) { tsz[0]--; tans -= sa[0][tsz[0]].t; ++rest; } while (rest > 0) { int Minp = -1; for (int j = 0; j < 4; ++j) { if (tsz[j] < sa[j].size() && (Minp == -1 || sa[j][tsz[j]].t < sa[Minp][tsz[Minp]].t)) Minp = j; } if (Minp == -1) break; tans += sa[Minp][tsz[Minp]++].t; --rest; } if (rest == 0 && tsz[1] + tsz[3] >= k && tsz[2] + tsz[3] >= k) { if (ans == -1 || ans > tans) { ans = tans; memcpy(sz, tsz, sizeof(tsz)); } } tsz[3]--; if (tsz[3] >= 0) tans -= sa[3][tsz[3]].t; } printf("%d\n", ans); if (ans == -1) return 0; vector<int> res; for (int i = 0; i < 4; ++i) { for (int j = 0; j < sz[i]; ++j) { res.push_back(sa[i][j].id); } } for (int i = 0; i < res.size(); ++i) { printf("%d%c", res[i], i == res.size() - 1 ? '\n' : ' '); } }
#include <bits/stdc++.h> using namespace std; int const MAXN = 2e5 + 10; int n, m, T, k; vector<pair<int, int>> a, b, ab, other; vector<int> ans; int check(int mid) { if ((int)a.size() < k - mid) return 2e9 + 11; if ((int)b.size() < k - mid) return 2e9 + 11; if (mid + max(0, k - mid) * 2 > m) return 2e9 + 11; int nd = m - mid, res = 0; ans.clear(); vector<pair<int, int>> vec; for (int i = 0; i < mid; i++) res += ab[i].first, ans.push_back(ab[i].second); for (int i = mid; i < ab.size(); i++) vec.push_back(ab[i]); for (int i = 0; i < k - mid; i++) res += a[i].first + b[i].first, nd -= 2, ans.push_back(a[i].second), ans.push_back(b[i].second); for (int i = max(0, k - mid); i < a.size(); i++) vec.push_back(a[i]); for (int i = max(0, k - mid); i < b.size(); i++) vec.push_back(b[i]); for (int i = 0; i < other.size(); i++) vec.push_back(other[i]); sort(vec.begin(), vec.end()); for (int i = 0; i < nd && i < vec.size(); i++) res += vec[i].first, ans.push_back(vec[i].second); return res; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m >> k; for (int i = 1, x1, x2, x3; i <= n; ++i) { cin >> x1 >> x2 >> x3; if (x2 && x3) ab.push_back({x1, i}); else if (x2) a.push_back({x1, i}); else if (x3) b.push_back({x1, i}); else other.push_back({x1, i}); } sort(a.begin(), a.end()), sort(b.begin(), b.end()), sort(ab.begin(), ab.end()), sort(other.begin(), other.end()); int l = 0, r = min((int)ab.size(), m); while (r - l > 10) { int midl = l + (r - l) / 3, midr = r - (r - l) / 3; if (check(midl) < check(midr)) r = midr; else l = midl; } int res = 2e9 + 10, Min_idx = -1; for (int i = l; i <= r; i++) { int tmp = check(i); if (tmp < res) res = tmp, Min_idx = i; } if (Min_idx == -1) return puts("-1"), 0; cout << check(Min_idx) << endl; for (auto a : ans) cout << a << " "; return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const int N = 2e5 + 5; long long t[N], a[N], b[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, k; cin >> n >> m >> k; int cnta = 0, cntb = 0; set<pair<long long, long long> > ab, aa, bb; for (int i = 1; i <= n; ++i) { cin >> t[i] >> a[i] >> b[i]; cnta += a[i], cntb += b[i]; if (a[i] && b[i]) ab.insert({t[i], i}); if (a[i] && !b[i]) aa.insert({t[i], i}); if (!a[i] && b[i]) bb.insert({t[i], i}); } if (cnta < k || cntb < k) cout << -1, exit(0); long long ans = 0; int taken = 0; vector<pair<long long, long long> > checkab, checka, checkb; while (k--) { if (!aa.size() || !bb.size()) checkab.push_back(*ab.begin()), ans += ab.begin()->first, taken++, ab.erase(ab.begin()); else if (ab.size() && ab.begin()->first < aa.begin()->first + bb.begin()->first) { checkab.push_back(*ab.begin()), ans += ab.begin()->first, taken++, ab.erase(ab.begin()); } else { checka.push_back(*aa.begin()), checkb.push_back(*bb.begin()), ans += aa.begin()->first + bb.begin()->first, taken += 2, aa.erase(aa.begin()), bb.erase(bb.begin()); } } if (taken <= m) { m -= taken; set<pair<long long, long long> > can; vector<bool> used(n + 5, 0); for (pair<long long, long long> cur : checkab) used[cur.second] = true; for (pair<long long, long long> cur : checka) used[cur.second] = true; for (pair<long long, long long> cur : checkb) used[cur.second] = true; for (int i = 1; i <= n; ++i) { if (!used[i]) can.insert({t[i], i}); } int mn = min((int)aa.size(), (int)bb.size()); vector<int> dop; while (m && can.size()) { bool ok = false; if (!aa.size() || !bb.size() || !checkab.size()) { pair<long long, long long> cur = *can.begin(); ans += cur.first; dop.push_back(cur.second); can.erase(can.begin()); ok = 1; } else if (aa.begin()->first + bb.begin()->first - checkab.back().first <= can.begin()->first) { ans -= checkab.back().first; can.insert(checkab.back()); checkab.pop_back(); ans += aa.begin()->first + bb.begin()->first; can.erase(*aa.begin()); can.erase(*bb.begin()); checka.push_back(*aa.begin()), checkb.push_back(*bb.begin()); aa.erase(aa.begin()), bb.erase(bb.begin()); ok = 1; } else { pair<long long, long long> cur = *can.begin(); ans += cur.first; dop.push_back(cur.second); if (aa.count(cur)) aa.erase(cur); if (bb.count(cur)) bb.erase(cur); can.erase(can.begin()); ok = 1; } if (ok) { m--; continue; } break; } if (m) cout << -1, exit(0); cout << ans << "\n"; for (pair<long long, long long> cur : checkab) cout << cur.second << " "; for (pair<long long, long long> cur : checka) cout << cur.second << " "; for (pair<long long, long long> cur : checkb) cout << cur.second << " "; for (int cur : dop) cout << cur << " "; exit(0); } m = taken - m; if (min((int)checka.size(), (int)ab.size() - (int)checkab.size()) < m) cout << -1, exit(0); while (m--) { ans -= (checka.back().first + checkb.back().first); ans += ab.begin()->first; checka.pop_back(), checkb.pop_back(), checkab.push_back(*ab.begin()), ab.erase(ab.begin()); } cout << ans << "\n"; for (pair<long long, long long> cur : checkab) cout << cur.second << " "; for (pair<long long, long long> cur : checka) cout << cur.second << " "; for (pair<long long, long long> cur : checkb) cout << cur.second << " "; }
#include <bits/stdc++.h> using namespace std; const int MAX = 1e4 + 9; int tree[(MAX << 2)][2], val, idx; void upd(int low, int high, int pos) { if (low == high) { tree[pos][1] += val; tree[pos][0] += val * low; return; } int mid = ((low + high) >> 1); idx <= mid ? upd(low, mid, (pos << 1)) : upd(mid + 1, high, (pos << 1 | 1)); tree[pos][0] = tree[(pos << 1)][0] + tree[(pos << 1 | 1)][0]; tree[pos][1] = tree[(pos << 1)][1] + tree[(pos << 1 | 1)][1]; } int qwr(int low, int high, int pos, int rest) { if (tree[pos][1] == rest) return tree[pos][0]; if (low == high) return rest * low; int mid = ((low + high) >> 1); if (tree[(pos << 1)][1] >= rest) { return qwr(low, mid, (pos << 1), rest); } else { return tree[(pos << 1)][0] + qwr(mid + 1, high, (pos << 1 | 1), rest - tree[(pos << 1)][1]); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, k; cin >> n >> m >> k; vector<pair<int, int> > a, b; vector<pair<pair<int, int>, long long> > both; vector<pair<int, int> > non; for (int i = 1; i <= n; ++i) { int t, x, y; cin >> t >> x >> y; if (x && !y) { val = 1, idx = t; upd(1, MAX - 1, 1); a.push_back({t, i}); } else if (!x && y) { val = 1, idx = t; upd(1, MAX - 1, 1); b.push_back({t, i}); } else if (x && y) { both.push_back({{t, i}, t}); } else { non.push_back({t, i}); val = 1, idx = t; upd(1, MAX - 1, 1); } } sort(a.begin(), a.end()); sort(b.begin(), b.end()); sort(both.begin(), both.end()); vector<pair<pair<int, int>, long long> > tempBoth = both; while (both.size() > k) { val = 1, idx = both.back().first.first; both.pop_back(); upd(1, MAX - 1, 1); } for (int i = 1; i < both.size(); ++i) { both[i].second += both[i - 1].second; } long long ans = 1e18 + 18, totA = 0, totB = 0; int cnt = 0; bool findAnswer = 0; for (int i = 0; i <= min(k, (int)min(a.size(), b.size())); ++i) { if (i) { val = -1, idx = a[i - 1].first; upd(1, MAX - 1, 1); totA += a[i - 1].first; val = -1, idx = b[i - 1].first; upd(1, MAX - 1, 1); totB += b[i - 1].first; } int needK = k - i; int rest = m - i * 2 - needK; if (needK > both.size() || rest < 0 || tree[1][1] < rest) { continue; } findAnswer = 1; long long sol = totA + totB + (needK > 0 ? both[needK - 1].second : 0) + (rest ? qwr(1, MAX - 1, 1, rest) : 0); if (sol < ans) { ans = sol; cnt = i; } if (!both.empty()) { val = -1, idx = both.back().first.first; both.pop_back(); upd(1, MAX - 1, 1); } } if (!findAnswer) { cout << -1; return 0; } cout << ans << "\n"; for (int i = 0; i < cnt; ++i) { cout << a[i].second << " " << b[i].second << " "; } int needK = k - cnt; for (int i = 0; i < needK; ++i) { cout << tempBoth[i].first.second << " "; } set<pair<int, int> > s; for (int i = cnt; i < a.size(); ++i) s.insert(a[i]); for (int i = cnt; i < b.size(); ++i) s.insert(b[i]); for (int i = needK; i < tempBoth.size(); ++i) s.insert(tempBoth[i].first); for (int i = 0; i < non.size(); ++i) s.insert(non[i]); int rest = m - 2 * cnt - needK; while (rest--) { cout << s.begin()->second << " "; s.erase(s.begin()); } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; const int mod = 1e9 + 7; struct ac { long long a, b, t; }; ac a[maxn]; bool cmp1(ac a, ac b) { return a.t < b.t; } int main() { ios::sync_with_stdio(false); cin.tie(0); vector<long long> suma, sumb, sumab; suma.push_back(0), sumb.push_back(0), sumab.push_back(0); int f1 = 0, f2 = 0; int n, k; cin >> n >> k; int cntab = 0; int cnta = 0; int cntb = 0; for (int i = 1; i <= n; i++) { cin >> a[i].t >> a[i].a >> a[i].b; if (a[i].a && a[i].b) cntab++; if (a[i].a && a[i].b == 0) cnta++; if (a[i].a == 0 && a[i].b) cntb++; if (a[i].a) f1++; if (a[i].b) f2++; } if (f1 < k || f2 < k) { cout << -1 << '\n'; return 0; } long long ans = 1e15; sort(a + 1, a + 1 + n, cmp1); int ra = 0; int rb = 0; int rab = 0; for (int i = 1; i <= n; i++) { if (a[i].a && a[i].b == 0) { long long temp = suma[ra++] + a[i].t; suma.push_back(temp); } if (a[i].a == 0 && a[i].b) { long long temp = sumb[rb++] + a[i].t; sumb.push_back(temp); } if (a[i].a && a[i].b) { long long temp = sumab[rab++] + a[i].t; sumab.push_back(temp); } } for (int i = 0; i <= cntab; i++) { if (i > k) break; long long temp = sumab[i]; if (k - i > cnta) continue; temp += suma[k - i]; if (k - i > cntb) continue; temp += sumb[k - i]; ans = min(ans, temp); } cout << ans << '\n'; }
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; priority_queue<int, std::vector<int>, std::greater<int> > both; priority_queue<int, std::vector<int>, std::greater<int> > alice; priority_queue<int, std::vector<int>, std::greater<int> > bob; for (int i = 0; i < n; i++) { int t, a, b; cin >> t >> a >> b; if (a && b) { both.push(t); } if (a && !b) { alice.push(t); } if (!a && b) { bob.push(t); } } int res = 0; while (k > 0 && ((!alice.empty() && !bob.empty()) || !both.empty())) { k--; if (both.empty()) { res += bob.top() + alice.top(); bob.pop(); alice.pop(); } else if (alice.empty() || bob.empty()) { res += both.top(); both.pop(); } else if ((alice.top() + bob.top() < both.top())) { res += bob.top() + alice.top(); bob.pop(); alice.pop(); } else { res += both.top(); both.pop(); } } if (k > 0) { cout << -1 << endl; } else { cout << res << endl; } }
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll inf = 1e18; const int N = 2 * 1e5 + 10; ll res, n, k; void solve() { ll tt, x, y; std::vector<ll> v, vv, vvv; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> tt >> x >> y; if (y == 1 && x == 1) v.push_back(tt); else if (y) vv.push_back(tt); else if (x) vvv.push_back(tt); } sort(vv.begin(), vv.end()); sort(vvv.begin(), vvv.end()); for (int i = 0; i < min((int)vv.size(), (int)vvv.size()); i++) { v.push_back(vv[i] + vvv[i]); } sort(v.begin(), v.end()); if (v.size() < k) { cout << "-1\n"; return; } ll ans = 0; for (int i = 0; i < k; i++) ans += v[i]; cout << ans << "\n"; } int main(int argc, char const *argv[]) { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); ll t = 1; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; int n, k; int suma, sumb; priority_queue<int> both, alice, bob; int main() { cin >> n >> k; for (int i = 1; i <= n; i++) { int t, a, b; cin >> t >> a >> b; if (a + b == 2) { both.push(-t); } else if (a == 1) { alice.push(-t); } else if (b == 1) { bob.push(-t); } suma += a; sumb += b; } if (suma < k || sumb < k) { cout << "-1\n"; return 0; } long long ans = 0; int a = 0, b = 0; while (a < k || b < k) { long long C1 = 1e12, C2 = 1e12, C3 = 1e12; if (both.size()) { C1 = -both.top(); } if (alice.size()) { C2 = -alice.top(); } if (bob.size()) { C3 = -bob.top(); } if ((C1 <= min(C2, C3) || C1 <= C2 + C3) && C1 != 1e12) { a++, b++; ans += C1; both.pop(); } else { if (a < k && C2 != 1e12) { a++; ans += C2; alice.pop(); } if (b < k && C3 != 1e12) { b++; ans += C3; bob.pop(); } } } cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; struct node { int t, a, b; } ke[maxn], le[maxn], m[maxn]; int n, k; bool cmp(node x, node y) { return x.t < y.t; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> k; int cnt1 = 0, cnt2 = 0, cnt0 = 0; for (int i = 0; i < n; i++) { node x; cin >> x.t >> x.a >> x.b; if (x.a && x.b) { m[cnt0] = x; cnt0++; } else if (x.a) { ke[cnt1] = x; cnt1++; } else if (x.b) { le[cnt2] = x; cnt2++; } } if (cnt1 + cnt0 < k || cnt2 + cnt0 < k) { cout << -1 << '\n'; return 0; } sort(ke, ke + cnt1, cmp); sort(le, le + cnt2, cmp); sort(m, m + cnt0, cmp); int o1 = 0, o0 = 0; long long ans = 0; while (o1 + o0 < k) { if (o0 >= cnt0) { ans += ke[o1].t + le[o1].t; o1++; } else if (o1 < min(cnt1, cnt2) && m[o0].t > ke[o1].t + le[o1].t) { ans += ke[o1].t + le[o1].t; o1++; } else { ans += m[o0].t; o0++; } } cout << ans << '\n'; }
#include <bits/stdc++.h> using namespace std; int Alice[200005], Bob[200005], Both[200005]; int PA[200005], PB[200005]; int FindMinReadingTime(int k, int a, int b, int bo) { int i, Ans = 0; if (a > 0) sort(Alice + 1, Alice + a + 1); if (b > 0) sort(Bob + 1, Bob + b + 1); if (bo > 0) sort(Both + 1, Both + bo + 1); for (i = 1; i <= a; ++i) PA[i] = PA[i - 1] + Alice[i]; for (i = 1; i <= a; ++i) PB[i] = PB[i - 1] + Bob[i]; if (a >= k && b >= k) Ans = PA[k] + PB[k]; else Ans = 2000000001; int Value = 0; for (i = 1; i <= min(k, bo); ++i) { Value += Both[i]; if (i + a >= k && i + b >= k) Ans = min(Ans, Value + PA[k - i] + PB[k - i]); } return Ans; } int main() { int N, k, i, j, p, q; int t, a, b; scanf("%d%d", &N, &k); j = p = q = 0; for (i = 1; i <= N; ++i) { scanf("%d%d%d", &t, &a, &b); if (a == 1) { if (b == 1) Both[++q] = t; else Alice[++j] = t; } else if (b == 1) Bob[++p] = t; } if (j + q < k || p + q < k) puts("-1"); else printf("%d\n", FindMinReadingTime(k, j, p, q)); return 0; }
#include <bits/stdc++.h> using namespace std; long long n, k, t, a, b, boths, alices, bobs, cnt[2], ans, res, tmp; vector<int> both, alice, bob; int main() { ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); cin >> n >> k; for (int i = 0; i < n; i++) { cin >> t >> a >> b; if (a && b) both.push_back(t); else if (a) alice.push_back(t); else if (b) bob.push_back(t); } boths = both.size(); alices = alice.size(); bobs = bob.size(); sort(both.begin(), both.end()); sort(alice.begin(), alice.end()); sort(bob.begin(), bob.end()); while (k) { int s1 = 1 << 30, s2 = 1 << 30; if (boths <= cnt[0]) break; if (boths > cnt[0]) s1 = both[cnt[0]]; if (cnt[1] < min(alices, bobs)) s2 = alice[cnt[1]] + bob[cnt[1]]; if (s1 < s2) ans += s1, cnt[0]++, k--; else ans += s2, cnt[1]++, k--; } while (k--) { if (cnt[1] < min(alices, bobs)) ans += alice[cnt[1]] + bob[cnt[1]], cnt[1]++; else if (cnt[0] < boths) ans += both[cnt[0]], cnt[0]++; else return cout << "-1\n", 0; } cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << "\n"; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } inline long long sbt(long long x) { return __builtin_popcountll(x); } inline long long iceil(double a) { return (long long)(ceil(a)); } inline long long mul(long long a, long long b, long long m = (long long)(1e9 + 7)) { return ((a % m) * (b % m)) % m; } inline long long add(long long a, long long b, long long m = (long long)(1e9 + 7)) { return (a + b) % m; } inline long long sub(long long a, long long b, long long m = (long long)(1e9 + 7)) { return (a - b + m) % m; } long long fastpow(long long a, long long b, long long m = (long long)(1e9 + 7)) { long long res = 1; while (b > 0) { if (b & 1) res = mul(res, a, m); a = mul(a, a, m); b >>= 1; } return res; } long long modinv(long long a, long long m = (long long)(1e9 + 7)) { return fastpow(a, m - 2, m); } long long n, m, k; vector<pair<long long, pair<long long, long long>>> v; vector<pair<long long, long long>> both, alice, bob, none; void get_ac() { cin >> n >> m >> k; long long a, b, c, ca = 0, cb = 0; for (auto i = 1; i <= n; i++) { cin >> a >> b >> c; if (b == 1 && c == 1) { both.push_back({a, i}); ca++; cb++; } else if (b == 1) { alice.push_back({a, i}); ca++; } else if (c == 1) { bob.push_back({a, i}); cb++; } else { none.push_back({a, i}); } } if (ca < k || cb < k) { cout << -1; return; } sort(alice.begin(), alice.end()); sort(bob.begin(), bob.end()); sort(both.begin(), both.end()); sort(none.begin(), none.end()); long long inone, iboth = 0, ialice = 0, ibob = 0; long long nboth = (long long)(both.size()), nalice = (long long)(alice.size()), nbob = (long long)(bob.size()), nnone = (long long)(none.size()); long long ans = 0, books = 0; set<long long> indices; long long kk = min({k, nalice, nbob}); for (long i = 0; i < kk; i++) { ans += alice[i].first; books++; indices.insert(alice[i].second); } for (long i = 0; i < kk; i++) { ans += bob[i].first; books++; indices.insert(bob[i].second); } for (long i = 0; i < k - kk; i++) { ans += both[i].first; books++; indices.insert(both[i].second); } int i = kk - 1; iboth = k - kk; while (iboth < nboth && i >= 0 && alice[i].first + bob[i].first > both[iboth].first) { ans -= alice[i].first + bob[i].first - both[iboth].first; indices.erase(alice[i].second); indices.erase(bob[i].second); indices.insert(both[iboth].second); i--; iboth++; books--; } if (books == m) { cout << ans; cout << "\n"; assert((long long)(indices.size()) == m); for (auto i : indices) { cout << i << " "; } return; } else if (books > m) { while (books > m && iboth < nboth && i >= 0) { ans -= alice[i].first + bob[i].first - both[iboth].first; books--; indices.erase(alice[i].second); indices.erase(bob[i].second); indices.insert(both[iboth].second); i--; iboth++; } ialice = i, ibob = i; while (1) { if (ialice >= 0 && ibob >= 0 && iboth < nboth && alice[ialice].first > both[iboth].first && bob[ibob].first > both[iboth].first) { if (alice[ialice] > bob[ibob]) { ans -= bob[ibob].first - both[iboth].first; indices.erase(bob[ibob].second); indices.insert(both[iboth].second); ibob--; iboth++; } else { ans -= alice[ialice].first - both[iboth].first; indices.erase(alice[ialice].second); indices.insert(both[iboth].second); ialice--; iboth++; } } else if (ialice >= 0 && iboth < nboth && alice[ialice].first > both[iboth].first) { ans -= alice[ialice].first - both[iboth].first; indices.erase(alice[ialice].second); indices.insert(both[iboth].second); ialice--; iboth++; } else if (ibob >= 0 && iboth < nboth && bob[ibob].first > both[iboth].first) { ans -= bob[ibob].first - both[iboth].first; indices.erase(bob[ibob].second); indices.insert(both[iboth].second); ibob--; iboth++; } else { break; } } while (1) { if (iboth - 1 >= 0 && ialice + 1 < nalice && ibob + 1 < nbob && alice[ialice + 1].first < both[iboth - 1].first && bob[ibob + 1].first < both[iboth - 1].first) { if (alice[ialice + 1] < bob[ibob + 1]) { if (ibob + 1 + iboth - 1 >= k) { ans += alice[ialice + 1].first - both[iboth - 1].first; indices.insert(alice[ialice + 1].second); indices.erase(both[iboth - 1].second); ialice++; iboth--; } else if (ialice + 1 + iboth - 1 >= k) { ans += bob[ibob + 1].first - both[iboth - 1].first; indices.insert(bob[ibob + 1].second); indices.erase(both[iboth - 1].second); ibob++; iboth--; } else { break; } } else { if (ialice + 1 + iboth - 1 >= k) { ans += bob[ibob + 1].first - both[iboth - 1].first; indices.insert(bob[ibob + 1].second); indices.erase(both[iboth - 1].second); ibob++; iboth--; } else if (ibob + 1 + iboth - 1 >= k) { ans += alice[ialice + 1].first - both[iboth - 1].first; indices.insert(alice[ialice + 1].second); indices.erase(both[iboth - 1].second); ialice++; iboth--; } else { break; } } } else if (iboth - 1 >= 0 && ialice + 1 < nalice && alice[ialice + 1].first < both[iboth - 1].first && ibob + 1 + iboth - 1 >= k) { ans += alice[ialice + 1].first - both[iboth - 1].first; indices.insert(alice[ialice + 1].second); indices.erase(both[iboth - 1].second); ialice++; iboth--; } else if (iboth - 1 >= 0 && ibob + 1 < nbob && bob[ibob + 1].first < both[iboth - 1].first && ialice + 1 + iboth - 1 >= k) { ans += bob[ibob + 1].first - both[iboth - 1].first; indices.insert(bob[ibob + 1].second); indices.erase(both[iboth - 1].second); ibob++; iboth--; } else { break; } } if (books > m) { cout << -1; } else { cout << ans; cout << "\n"; assert((long long)(indices.size()) == m); for (auto i : indices) { cout << i << " "; } } return; } else { ialice = i; ibob = i; inone = -1; iboth = iboth - 1; while (books < m) { vector<pair<pair<long long, long long>, long long>> candidates; bool f = 0; if (inone + 1 < (long long)(none.size())) { candidates.push_back({none[inone + 1], 0}); } if (ialice + 1 < (long long)(alice.size())) { candidates.push_back({alice[ialice + 1], 1}); } if (ibob + 1 < (long long)(bob.size())) { candidates.push_back({bob[ibob + 1], 2}); } if (iboth + 1 < (long long)(both.size())) { candidates.push_back({both[iboth + 1], 3}); } if ((long long)(candidates.size()) == 0) break; sort(candidates.begin(), candidates.end()); if (ibob + 1 < (long long)(bob.size()) && ialice + 1 < (long long)(alice.size()) && iboth >= 0) { long long cur = bob[ibob + 1].first + alice[ialice + 1].first - both[iboth].first; if (cur > candidates[0].first.first) { } else { f = 1; ans += cur; indices.insert(bob[ibob + 1].second); indices.insert(alice[ialice + 1].second); indices.erase(both[iboth].second); ialice++; ibob++; iboth--; } } if (!f) { pair<long long, long long> p = candidates[0].first; long long q = candidates[0].second; ans += p.first; indices.insert(p.second); if (q == 0) { inone++; } else if (q == 1) { ialice++; } else if (q == 2) { ibob++; } else { iboth++; } } books++; } assert((long long)(indices.size()) == m); iboth++; while (1) { if (ialice >= 0 && ibob >= 0 && iboth < nboth && alice[ialice].first > both[iboth].first && bob[ibob].first > both[iboth].first) { if (alice[ialice] > bob[ibob]) { ans -= bob[ibob].first - both[iboth].first; indices.erase(bob[ibob].second); indices.insert(both[iboth].second); ibob--; iboth++; } else { ans -= alice[ialice].first - both[iboth].first; indices.erase(alice[ialice].second); indices.insert(both[iboth].second); ialice--; iboth++; } } else if (ialice >= 0 && iboth < nboth && alice[ialice].first > both[iboth].first) { ans -= alice[ialice].first - both[iboth].first; indices.erase(alice[ialice].second); indices.insert(both[iboth].second); ialice--; iboth++; } else if (ibob >= 0 && iboth < nboth && bob[ibob].first > both[iboth].first) { ans -= bob[ibob].first - both[iboth].first; indices.erase(bob[ibob].second); indices.insert(both[iboth].second); ibob--; iboth++; } else { break; } } assert((long long)(indices.size()) == m); while (1) { if (iboth - 1 >= 0 && ialice + 1 < nalice && ibob + 1 < nbob && alice[ialice + 1].first < both[iboth - 1].first && bob[ibob + 1].first < both[iboth - 1].first) { if (alice[ialice + 1] < bob[ibob + 1]) { if (ibob + 1 + iboth - 1 >= k) { ans += alice[ialice + 1].first - both[iboth - 1].first; indices.insert(alice[ialice + 1].second); indices.erase(both[iboth - 1].second); ialice++; iboth--; } else if (ialice + 1 + iboth - 1 >= k) { ans += bob[ibob + 1].first - both[iboth - 1].first; indices.insert(bob[ibob + 1].second); indices.erase(both[iboth - 1].second); ibob++; iboth--; } else { break; } } else { if (ialice + 1 + iboth - 1 >= k) { ans += bob[ibob + 1].first - both[iboth - 1].first; indices.insert(bob[ibob + 1].second); indices.erase(both[iboth - 1].second); ibob++; iboth--; } else if (ibob + 1 + iboth - 1 >= k) { ans += alice[ialice + 1].first - both[iboth - 1].first; indices.insert(alice[ialice + 1].second); indices.erase(both[iboth - 1].second); ialice++; iboth--; } else { break; } } } else if (iboth - 1 >= 0 && ialice + 1 < nalice && alice[ialice + 1].first < both[iboth - 1].first && ibob + 1 + iboth - 1 >= k) { ans += alice[ialice + 1].first - both[iboth - 1].first; indices.insert(alice[ialice + 1].second); indices.erase(both[iboth - 1].second); ialice++; iboth--; } else if (iboth - 1 >= 0 && ibob + 1 < nbob && bob[ibob + 1].first < both[iboth - 1].first && ialice + 1 + iboth - 1 >= k) { ans += bob[ibob + 1].first - both[iboth - 1].first; indices.insert(bob[ibob + 1].second); indices.erase(both[iboth - 1].second); ibob++; iboth--; } else { break; } } assert((long long)(indices.size()) == m); while (1) { vector<pair<long long, long long>> candidates; if (inone + 1 < nnone && ialice >= 0 && (ialice + 1 + iboth) > k && none[inone + 1].first < alice[ialice].first) { candidates.push_back({none[inone + 1].first - alice[ialice].first, 0}); } if (inone + 1 < nnone && ibob >= 0 && (ibob + 1 + iboth) > k && none[inone + 1].first < bob[ibob].first) { candidates.push_back({none[inone + 1].first - bob[ibob].first, 1}); } if ((long long)(candidates.size()) == 0) break; sort(candidates.begin(), candidates.end()); if ((long long)(candidates.size()) == 2 && iboth - 1 >= 0 && none[inone + 1].first - both[iboth - 1].first < candidates[0].first) { long long curcandidate = none[inone + 1].first - both[iboth - 1].first; ans += curcandidate; indices.erase(both[iboth - 1].second); indices.insert(none[inone + 1].second); iboth--; inone++; } else { if (candidates[0].first == 0) { ans += candidates[0].first; indices.erase(alice[ialice].second); indices.insert(none[inone + 1].second); ialice--; inone++; } else { ans += candidates[0].first; indices.erase(bob[ibob].second); indices.insert(none[inone + 1].second); ibob--; inone++; } } } if (books < m) { cout << -1; } else { cout << ans; cout << "\n"; assert((long long)(indices.size()) == m); for (auto i : indices) { cout << i << " "; } } return; } } int main() { cin.sync_with_stdio(false); cout.sync_with_stdio(false); cin.tie(NULL); { get_ac(); cout << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const long long N = 3e2 + 5; int main() { ios_base::sync_with_stdio(false); long long n, k, one = 0, two = 0; cin >> n >> k; vector<long long> v1, v2, v3; for (int i = 0; i < n; i++) { long long x, y, z; cin >> x >> y >> z; if (y) one++; if (z) two++; if (y && z) v1.push_back(x); else if (y) v2.push_back(x); else if (z) v3.push_back(x); } if (one < k || two < k) { cout << -1 << endl; return 0; } sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); sort(v3.begin(), v3.end()); long long sum = 0, count1 = k, count2 = k, p = 0, q = 0, r = 0; int len1 = v1.size(), len2 = v2.size(), len3 = v3.size(); while (count1 > 0 || count2 > 0) { if (q >= len2 || r >= len3) { sum += v1[p]; p++; count1--; count2--; } else if (p < len1) { if (v1[p] <= (v2[q] + v3[r])) { sum += v1[p]; p++; count1--; count2--; } else { sum += (v2[q] + v3[r]); q++; r++; count2--; count1--; } } else { sum += (v2[q] + v3[r]); q++; r++; count2--; count1--; } } cout << sum; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5, OO = 0x3f3f3f3f, mod = 1e9 + 7; int main() { cin.tie(0); cin.sync_with_stdio(0); int n, k; cin >> n >> k; vector<int> both, A, B; for (int i = 0; i < n; i++) { int t, a, b; cin >> t >> a >> b; if (a && b) both.push_back(t); else if (a) A.push_back(t); else if (b) B.push_back(t); } sort(both.begin(), both.end()); sort(A.begin(), A.end()); sort(B.begin(), B.end()); if (both.size() + A.size() < k || both.size() + B.size() < k) { cout << "-1\n"; return 0; } int mi = min(A.size(), B.size()); for (int i = 0; i < mi; i++) both.push_back(A[i] + B[i]); sort(both.begin(), both.end()); long long ans = 0; for (int i = 0; i < k; i++) ans += both[i]; cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; void solve() { long long n, k; cin >> n >> k; vector<long long> a[3], pre[3]; for (long long i = 0; i < n; i++) { long long x, y, z; cin >> x >> y >> z; if (y & z) a[0].push_back(x); else if (y) a[1].push_back(x); else if (z) a[2].push_back(x); } auto get_pref = [&](vector<long long>& x, vector<long long>& y) { y.resize(((long long)(x).size()) + 1); for (long long i = 0; i < ((long long)(x).size()); i++) { y[i + 1] = y[i] + x[i]; } }; for (long long i = 0; i < 3; i++) { sort((a[i]).begin(), (a[i]).end()); get_pref(a[i], pre[i]); } long long ans = INF; for (long long i = 0; i <= k; i++) { if (((long long)(a[0]).size()) >= i && ((long long)(a[1]).size()) >= k - i && ((long long)(a[2]).size()) >= k - i) { ans = min(ans, pre[0][i] + pre[1][k - i] + pre[2][k - i]); } } cout << (ans == INF ? -1 : ans); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long tt = 1; while (tt--) { solve(); } }
#include <bits/stdc++.h> using namespace std; int read(int &x) { return scanf("%d", &x); } int read(int &x, int &y) { return scanf("%d%d", &x, &y); } int read(int &x, int &y, int &z) { return scanf("%d%d%d", &x, &y, &z); } int read(long long &x) { return scanf("%lld", &x); } int read(long long &x, long long &y) { return scanf("%lld%lld", &x, &y); } int read(double &x) { return scanf("%lf", &x); } char buff[2000010]; int read(string &s) { int r = scanf("%s", buff); s = buff; return r; } using namespace std; struct Groups { set<pair<long long, long long> > G[10]; long long S[10]; Groups() { for (int i = 0; i < 10; ++i) S[i] = 0; } pair<long long, long long> add(int type, pair<long long, long long> p) { G[type].insert(p); S[type] += p.first; return p; } pair<long long, long long> rem(int type, pair<long long, long long> p) { G[type].erase(p); S[type] -= p.first; return p; } long long sum(int type) { return S[type]; } int size(int type) { return G[type].size(); } pair<long long, long long> getLower(int type) { return *G[type].begin(); } pair<long long, long long> getHigher(int type) { return *G[type].rbegin(); } bool contains(int type, pair<long long, long long> v) { return G[type].count(v) > 0; } pair<long long, long long> moveLower(int typeSource, int typeDest) { auto v = getLower(typeSource); rem(typeSource, v); add(typeDest, v); return v; } pair<long long, long long> moveHigher(int typeSource, int typeDest) { auto v = getHigher(typeSource); rem(typeSource, v); add(typeDest, v); return v; } }; int main() { int TC = 1; while (TC-- > 0) { int N, M, K; read(N, M, K); Groups G; vector<int> O(N); for (int i = 0; i < N; ++i) { int n, a, b; read(n, a, b); O[i] = n; if (a && b) G.add(3, pair<long long, long long>(n, i)); else if (a) G.add(1, pair<long long, long long>(n, i)); else if (b) G.add(2, pair<long long, long long>(n, i)); else G.add(4, pair<long long, long long>(n, i)); } int bi = -1; long long bs = -1; int both = G.size(3); auto adjustGroup = [&](int type, int graveyard, int target, bool updateD) { while (G.size(type) > 0 && G.size(graveyard) > 0 && G.getHigher(type) > G.getLower(graveyard)) { G.moveHigher(type, graveyard); } while (G.size(type) > target && G.size(type) > 0) { auto v = G.moveHigher(type, graveyard); if (updateD) { G.add(4, v); } } while (G.size(type) < target && G.size(graveyard) > 0) { auto v = G.moveLower(graveyard, type); if (updateD) { int pos = G.contains(4, v) ? 4 : 6; G.rem(pos, v); } } }; auto adjustGroups = [&](int toBoth) { int needFromAlone = max(0, K - toBoth); int extras = max(0, M - toBoth - 2 * needFromAlone); adjustGroup(3, 5, toBoth, true); adjustGroup(1, 8, needFromAlone, true); adjustGroup(2, 7, needFromAlone, true); adjustGroup(4, 6, extras, false); }; for (int i = 0; i <= both; ++i) { int needFromAlone = max(0, K - i); int extras = max(0, M - i - 2 * needFromAlone); if (extras + 2 * needFromAlone + i != M) continue; adjustGroups(i); if (G.size(3) == i && G.size(1) == needFromAlone && G.size(2) == needFromAlone && G.size(4) == extras) { long long ns = G.sum(3) + G.sum(1) + G.sum(2) + G.sum(4); if (bi == -1 || bs > ns) { bi = i; bs = ns; } } } cout << bs << endl; if (bs != -1) { adjustGroups(bi); int needFromAlone = max(0, K - bi); int extras = max(0, M - bi - 2 * needFromAlone); vector<int> I; assert(G.size(3) == bi && G.size(1) == needFromAlone && G.size(2) == needFromAlone && G.size(4) == extras); for (auto p : G.G[3]) I.push_back(p.second); for (auto p : G.G[1]) I.push_back(p.second); for (auto p : G.G[2]) I.push_back(p.second); for (auto p : G.G[4]) I.push_back(p.second); sort(I.begin(), I.end()); for (int v : I) cout << v + 1 << " "; cout << endl; } } }
#include <bits/stdc++.h> using namespace std; int main() { int n, k, t, a, b; int all[200000]; int Alice[200000]; int Bob[200000]; int numAll = 0; int numAlice = 0; int numBob = 0; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> t >> a >> b; if (a == b && a == 1) { all[numAll] = t; numAll++; } else if (a == 1) { Alice[numAlice] = t; numAlice++; } else if (b == 1) { Bob[numBob] = t; numBob++; } } sort(all, all + numAll); sort(Alice, Alice + numAlice); sort(Bob, Bob + numBob); int x = 0; int y = 0; int ans = 0; if (numAll + min(numAlice, numBob) >= k) { for (int i = 0; i < k; i++) { if (x >= numAll) { ans += Alice[y] + Bob[y]; y++; continue; } if (y >= min(numAlice, numBob)) { ans += all[x]; x++; continue; } if (all[x] <= Alice[y] + Bob[y]) { ans += all[x]; x++; } else { ans += Alice[y] + Bob[y]; y++; } } } else { ans = -1; } cout << ans << endl; }
#include <bits/stdc++.h> const int N = (int)5e5 + 7; const int inf = (int)1e9 + 7; const int mod = (int)1e9 + 7; const long long linf = (long long)1e18 + 7; const int dx[] = {-1, 0, 1, 0, 1, -1, -1, 1}; const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1}; using namespace std; int n, k, need; long long p1[N], p2[N]; struct tree { int cnt[N << 2]; long long t[N << 2]; void upd(int p, int x, int v = 1, int tl = 1, int tr = N) { if (tl == tr) { cnt[v] += x; t[v] = (long long)cnt[v] * tl; return; } int tm = tl + tr >> 1; if (p <= tm) upd(p, x, v << 1, tl, tm); else upd(p, x, v << 1 | 1, tm + 1, tr); cnt[v] = cnt[v << 1] + cnt[v << 1 | 1]; t[v] = t[v << 1] + t[v << 1 | 1]; } long long get(int k, int v = 1, int tl = 1, int tr = N) { if (tl == tr) { return k * tl; } int tm = tl + tr >> 1; if (cnt[v << 1] >= k) return get(k, v << 1, tl, tm); return get(k - cnt[v << 1], v << 1 | 1, tm + 1, tr) + t[v << 1]; } } t; void solve() { cin >> n >> need >> k; vector<pair<int, int>> l, m, r, free; for (int i = (1); i <= (n); i++) { int t, a, b; cin >> t >> a >> b; if (a && b) m.push_back({t, i}); else if (a) l.push_back({t, i}); else if (b) r.push_back({t, i}); else free.push_back({t, i}); } sort(m.begin(), m.end()); sort(l.begin(), l.end()); sort(r.begin(), r.end()); sort(free.begin(), free.end()); int can1 = min((int)l.size(), (int)r.size()), can2 = (int)m.size(); for (int i = (can1 + 1); i <= ((int)l.size()); i++) { t.upd(l[i - 1].first, 1); } for (int i = (can1 + 1); i <= ((int)r.size()); i++) { t.upd(r[i - 1].first, 1); } for (int i = (1); i <= (can1); i++) { p1[i] = p1[i - 1] + (l[i - 1].first + r[i - 1].first); t.upd(l[i - 1].first, 1); t.upd(r[i - 1].first, 1); } for (int i = (1); i <= (can2); i++) { p2[i] = p2[i - 1] + (m[i - 1].first); t.upd(m[i - 1].first, 1); } for (auto it : free) { t.upd(it.first, 1); } long long mn = linf; int opt = -1, ptr = 0; for (int i = (0); i <= (can1); i++) { int x = i, y = k - x; if (i) { t.upd(l[i - 1].first, -1); t.upd(r[i - 1].first, -1); } while (ptr < min(y, can2)) { ++ptr; t.upd(m[ptr - 1].first, -1); } if (y <= can2 && 2 * x + y <= need) { long long extra = t.get(need - 2 * x - y); long long sum = p1[x] + p2[y] + extra; if (mn > sum) { mn = sum; opt = i; } } } if (mn == linf) { cout << -1 << '\n'; return; } vector<int> ans; int x = opt, y = k - x; for (int i = (1); i <= (x); i++) ans.push_back(l[i - 1].second), ans.push_back(r[i - 1].second); for (int i = (1); i <= (y); i++) ans.push_back(m[i - 1].second); multiset<pair<int, int>> extra; for (int i = (x + 1); i <= ((int)l.size()); i++) { extra.insert(l[i - 1]); } for (int i = (x + 1); i <= ((int)r.size()); i++) { extra.insert(r[i - 1]); } for (int i = (y + 1); i <= (can2); i++) { extra.insert(m[i - 1]); } for (auto it : free) extra.insert(it); int ost = need - 2 * x - y; while (ost--) { ans.push_back(extra.begin()->second); extra.erase(extra.begin()); } cout << mn << '\n'; for (auto it : ans) { cout << it << ' '; } } int main() { ios_base ::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; while (t--) { solve(); } exit(0); }
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int i, j, a, b, t; vector<int> time, temp1, temp2; for (i = 0; i < n; i++) { cin >> t >> a >> b; if (a == 1 && b == 1) time.push_back(t); else if (a == 1) temp1.push_back(t); else if (b == 1) temp2.push_back(t); } sort(temp1.begin(), temp1.end()); sort(temp2.begin(), temp2.end()); j = min(temp1.size(), temp2.size()); for (i = 0; i < j; i++) time.push_back(temp1[i] + temp2[i]); sort(time.begin(), time.end()); j = time.size(); if (j < k) { cout << "-1\n"; } else { int ans = 0; for (i = 0; i < k; i++) ans += time[i]; cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; long long n, m, k, t[200005], a[200005], b[200005]; vector<pair<long long, long long> > ta, tb, tboth, tnone; multiset<long long> second; int main() { ios::sync_with_stdio(false); cin >> n >> m >> k; for (long long i = 1; i <= n; ++i) cin >> t[i] >> a[i] >> b[i]; long long cnta = 0, cntb = 0, cntboth = 0, cntnone = 0; for (long long i = 1; i <= n; ++i) { if (a[i] == 1 && b[i] == 1) { tboth.push_back(make_pair(t[i], i)); cntboth++; } else if (a[i] == 1) { ta.push_back(make_pair(t[i], i)); cnta++; } else if (b[i] == 1) { tb.push_back(make_pair(t[i], i)); cntb++; } else { tnone.push_back(make_pair(t[i], i)); cntnone++; } } sort(tboth.begin(), tboth.end()); sort(ta.begin(), ta.end()); sort(tb.begin(), tb.end()); sort(tnone.begin(), tnone.end()); long long ans = 1000000007LL * 10000; long long cboth = 0, cnone = 0, ca = 0, cb = 0, tot = 0; long long iboth = min(m, cntboth) - 1, ia = -1, ib = -1, inone = -1; long long besta, bestb, bestboth, bestnone; for (long long i = 0; i <= iboth; ++i) cboth += tboth[i].first; tot = iboth + 1; for (long long i = iboth; i >= -1; --i) { long long sep = (k - (i + 1)); if (sep > cntb || sep > cnta) continue; if (2 * sep + iboth + 1 > m) continue; while (ia < sep - 1) { ia++; ca += ta[ia].first; tot++; if (tot > m) { if (ib == -1) { cnone -= tnone[inone].first; inone--; tot--; } else if (inone == -1) { cb -= tb[ib].first; ib--; tot--; } else if (ib < sep || tnone[inone].first >= tb[ib].first) { cnone -= tnone[inone].first; inone--; tot--; } else { cb -= tb[ib].first; ib--; tot--; } } } while (ib < sep - 1) { ib++; cb += tb[ib].first; tot++; if (tot > m) { if (ia == -1) { cnone -= tnone[inone].first; inone--; tot--; } else if (inone == -1) { ca -= ta[ia].first; ia--; tot--; } else if (ia < sep || tnone[inone].first >= ta[ia].first) { cnone -= tnone[inone].first; inone--; tot--; } else { ca -= ta[ia].first; ia--; tot--; } } } while (tot < m) { long long mn = 1000000007LL, val = -1; if (ia + 1 < cnta && (val == -1 || mn >= ta[ia + 1].first)) { mn = ta[ia + 1].first; val = 1; } if (ib + 1 < cntb && (val == -1 || mn >= tb[ib + 1].first)) { mn = tb[ib + 1].first; val = 2; } if (inone + 1 < cntnone && (val == -1 || mn >= tnone[inone + 1].first)) { mn = tnone[inone + 1].first; val = 3; } if (val == 1) { ia++; ca += ta[ia].first; tot++; } else if (val == 2) { ib++; cb += tb[ib].first; tot++; } else if (val == 3) { inone++; cnone += tnone[inone].first; tot++; } else break; } if (tot == m && ans > cboth + ca + cb + cnone) { ans = cboth + ca + cb + cnone; bestboth = iboth; besta = ia; bestb = ib; bestnone = inone; } if (i >= 0) { cboth -= tboth[i].first; tot--; iboth--; } } if (ans == 1000000007LL * 10000) { cout << -1 << endl; return 0; } else { cout << ans << "\n"; if (cntboth > 0) for (long long i = 0; i <= bestboth; ++i) cout << tboth[i].second << " "; if (cnta > 0) for (long long i = 0; i <= besta; ++i) cout << ta[i].second << " "; if (cntb > 0) for (long long i = 0; i <= bestb; ++i) cout << tb[i].second << " "; if (cntnone > 0) for (long long i = 0; i <= bestnone; ++i) cout << tnone[i].second << " "; cout << endl; } }
#include <bits/stdc++.h> using namespace std; const char nl = '\n'; const int MAX_N = 100011; const long long INF = (1LL << 50) + 123; const long long MOD = 1000000007; const long double PI = 4 * atan((long double)1); template <typename T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; } template <typename T> bool ckmax(T& a, const T& b) { return b > a ? a = b, 1 : 0; } mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); const int MX = 1 << 20; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m, k; cin >> n >> m >> k; vector<pair<int, int> > both, left, right; int t, a, b; set<pair<int, int> > rest; for (int i = 0; i < n; i++) { cin >> t >> a >> b; if (a && b) both.push_back({t, i}); else if (a) left.push_back({t, i}); else if (b) right.push_back({t, i}); rest.insert({t, i}); } set<pair<int, int> > rCopy = rest; sort(left.begin(), left.end()); sort(right.begin(), right.end()); sort(both.begin(), both.end()); int sm = min((int)left.size(), (int)right.size()); if ((int)both.size() + sm < k) { cout << -1 << nl; return 0; } long long bothSum = 0; int bothCount = 0; for (int i = 0; i < min(k, (int)both.size()); i++) { bothSum += both[i].first; bothCount++; rest.erase(rest.find(both[i])); } long long ans = INF; long long curAns = 0; long long rSum = 0; set<pair<int, int> > r; while ((int)r.size() < m - k) { if ((int)rest.size() == 0) break; r.insert(*rest.begin()); rSum += rest.begin()->first; rest.erase(rest.begin()); } int bestIdx = -1; if (bothCount == k && (int)r.size() == m - k) if (ckmin(ans, bothSum + rSum)) bestIdx = k; int lrCount = 0; for (int i = (k)-1; i >= 0; i--) { if (k - i - 1 >= sm || 2 * (k - i) + i > m) break; if (i < (int)both.size()) { bothSum -= both[i].first; rest.insert(both[i]); bothCount--; } curAns += left[k - i - 1].first + right[k - i - 1].first; lrCount += 2; assert(rest.find(left[k - i - 1]) != rest.end() || r.find(left[k - i - 1]) != r.end()); assert(rest.find(right[k - i - 1]) != rest.end() || r.find(right[k - i - 1]) != r.end()); if (rest.find(left[k - i - 1]) != rest.end()) rest.erase(rest.find(left[k - i - 1])); if (rest.find(right[k - i - 1]) != rest.end()) rest.erase(rest.find(right[k - i - 1])); if (r.find(left[k - i - 1]) != r.end()) r.erase(r.find(left[k - i - 1])), rSum -= left[k - i - 1].first; if (r.find(right[k - i - 1]) != r.end()) r.erase(r.find(right[k - i - 1])), rSum -= right[k - i - 1].first; while ((int)r.size() < m - 2 * (k - i) - i) { if ((int)rest.size() == 0) break; r.insert(*rest.begin()); rSum += rest.begin()->first; rest.erase(rest.begin()); } while ((int)r.size() > m - 2 * (k - i) - i) { rest.insert(*r.rbegin()); rSum -= r.rbegin()->first; r.erase(--r.end()); } if (bothCount == i && lrCount == 2 * (k - i) && bothCount + lrCount + (int)r.size() == m) { if (ckmin(ans, bothSum + curAns + rSum)) bestIdx = i; } } if (bestIdx == -1) cout << -1 << nl; else { cout << ans << nl; for (int i = 0; i < bestIdx; i++) { cout << both[i].second + 1 << " "; rCopy.erase(rCopy.find(both[i])); } for (int i = 0; i < k - bestIdx; i++) { cout << left[i].second + 1 << " " << right[i].second + 1 << " "; rCopy.erase(rCopy.find(left[i])); rCopy.erase(rCopy.find(right[i])); } int count = 0; assert(bestIdx + 2 * (k - bestIdx) <= m); for (auto& a : rCopy) { if (count + bestIdx + 2 * (k - bestIdx) == m) break; count++; cout << a.second + 1 << " "; } cout << nl; assert(count + bestIdx + 2 * (k - bestIdx) == m); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, k; cin >> n >> k; long long arr[n + 1]; long long a, b, c; vector<long long> A, B, common; for (long long i = 0; i < n; i++) { cin >> a >> b >> c; if (b == 1 && c == 1) { common.push_back(a); } else if (b == 1) { A.push_back(a); } else if (c == 1) { B.push_back(a); } } sort(common.begin(), common.end()); sort(A.begin(), A.end()); sort(B.begin(), B.end()); long long pc = 0, pa = 0, pb = 0; if ((long long)(common.size() + min(A.size(), B.size())) < k) { cout << "-1\n"; return 0; } long long cnt = 0, res = 0; common.push_back(INT_MAX); A.push_back(INT_MAX); B.push_back(INT_MAX); while (pc < (long long)common.size() || (pa < A.size() && pb < B.size())) { if (cnt == k) break; if (common[pc] < A[pa] + B[pb]) { res += common[pc]; pc++; cnt++; } else { res += (A[pa] + B[pb]); pa++; pb++; cnt++; } } if (cnt == k) cout << res; else cout << "-1"; return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T = int> struct Fenwick { int n; vector<T> v; Fenwick(int size = 100005) : n(size), v(n + 1, 0) {} inline void add(int p, T val) { for (; p <= n; p += (p & -p)) { v[p] += val; } } inline T query(int p) { T tmp = 0; for (; p > 0; p -= (p & -p)) { tmp += v[p]; } return tmp; } inline T query(int l, int r) { return query(r) - query(l - 1); } }; template <typename T> T bs_first(T l, T r, function<bool(T)> f) { assert(l < r); T mid; while (l != r) { mid = l + (r - l) / 2; if (f(mid)) { r = mid; } else { l = mid + 1; } } return r; } void solve() { int n, m, k; cin >> n >> m >> k; vector<int> a(n); vector<int> g[2][2]; for (int i = 0; i < n; i++) { cin >> a[i]; int x, y; cin >> x >> y; g[x][y].push_back(i); } int lb, ub; { int c = min(g[0][1].size(), g[1][0].size()); lb = max({0, k - c, 2 * k - m}); ub = min(k, (int)g[1][1].size()); } if (lb > ub) { cout << -1; return; } auto comp = [&](int u, int v) { return a[u] < a[v]; }; vector<int> id(n); iota(id.begin(), id.end(), 0); sort(id.begin(), id.end(), comp); for (int x = 0; x < 2; x++) { for (int y = 0; y < 2; y++) { sort(g[x][y].begin(), g[x][y].end(), comp); } } vector<int> pos(n); Fenwick<int> sum(n), sz(n); for (int i = 0; i < n; i++) { sz.add(i + 1, 1); sum.add(i + 1, a[id[i]]); pos[id[i]] = i; } int must = 0; auto add = [&](int i) { sz.add(pos[i] + 1, 1); sum.add(pos[i] + 1, a[i]); must -= a[i]; }; auto del = [&](int i) { sz.add(pos[i] + 1, -1); sum.add(pos[i] + 1, -a[i]); must += a[i]; }; for (int x = 0; x < lb; x++) { int i = g[1][1][x]; del(i); } for (int y = 0; y < k - lb; y++) { int i = g[0][1][y]; del(i); i = g[1][0][y]; del(i); } int mi = 2e9 + 10, z = -1; for (int x = lb; x <= ub; x++) { int need = m - x - 2 * (k - x); int p = bs_first<int>(0, n + 1, [&](int p) { return sz.query(p) >= need; }); int tmp = must + sum.query(p); if (tmp < mi) { mi = tmp; z = x; } if (x == ub) break; int i = g[1][1][x]; del(i); i = g[0][1][k - x - 1]; add(i); i = g[1][0][k - x - 1]; add(i); } assert(z != -1); vector<bool> trk(n); vector<int> res; for (int x = 0; x < z; x++) { int i = g[1][1][x]; res.push_back(i); trk[i] = true; } for (int y = 0; y < k - z; y++) { int i = g[0][1][y]; res.push_back(i); trk[i] = true; i = g[1][0][y]; res.push_back(i); trk[i] = true; } for (int i : id) if (!trk[i] && (int)res.size() < m) { res.push_back(i); trk[i] = true; } int ans = 0; for (int i : res) ans += a[i]; assert(ans == mi); cout << ans << "\n"; for (int i : res) cout << i + 1 << ' '; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const long long mxn = 100010; void solve(long long tc) { long long n, k, x, y, z; cin >> n >> k; deque<long long> d00, d01, d10, d11; for (long long i = 0; i < n; i++) { cin >> x >> y >> z; if (y == 0 && z == 0) d00.push_back(x); else if (y == 0 && z == 1) d01.push_back(x); else if (y == 1 && z == 0) d10.push_back(x); else d11.push_back(x); } sort(d00.begin(), d00.end()); sort(d01.begin(), d01.end()); sort(d10.begin(), d10.end()); sort(d11.begin(), d11.end()); long long ans = 0; while (k--) { if (!d01.empty() && !d10.empty()) { if (!d11.empty()) { if (d01.front() + d10.front() <= d11.front()) { ans += d01.front() + d10.front(); d01.pop_front(); d10.pop_front(); } else { ans += d11.front(); d11.pop_front(); } } else { ans += d01.front() + d10.front(); d01.pop_front(); d10.pop_front(); } } else if (!d11.empty()) { ans += d11.front(); d11.pop_front(); } else { cout << -1 << '\n'; return; } } cout << ans << '\n'; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve(1); return 0; }
#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; }
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("O3") const long long MOD = 1e9 + 7; long long n_bits(long long n) { long long x = __builtin_popcount(n); return x; } int pow(int a, int b, int m) { int ans = 1; while (b) { if (b & 1) ans = (ans * a) % m; b /= 2; a = (a * a) % m; } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t = 1; while (t--) { int n, k, x, y, z; cin >> n >> k; vector<int> A; vector<int> B; vector<int> C; for (long long i = 0; i < (n); ++i) { cin >> x >> y >> z; if (y == z && y == 1) { C.push_back(x); } else if (y == 1 && z == 0) { A.push_back(x); } else if (z == 1 && y == 0) { B.push_back(x); } } if (A.size() + C.size() < k || B.size() + C.size() < k) { cout << -1; return 0; } vector<int> t1; sort(A.begin(), A.end()); sort(B.begin(), B.end()); int mini = min(A.size(), B.size()); int j = 0, ans = 0, total = 0; for (int i = 0; i < mini; i++) { t1.push_back(A[i] + B[i]); } sort(C.begin(), C.end()); for (int i = 0; i < C.size(); i++) { t1.push_back(C[i]); } total = 0; sort(t1.begin(), t1.end()); for (int i = 0; i < k; i++) { total += t1[i]; } cout << total; } return 0; }
#include <bits/stdc++.h> using namespace std; double const EPS = 1e-8, PI = acos(-1); const int N = 2e5 + 9, M = 30 + 7, OO = (int)1e6 + 1, MAXN = 4 * N; const long long MOD = 1e9 + 7, INF = 1e18 + 9; void INPUT() {} bool cmp(pair<int, pair<bool, bool>> a, pair<int, pair<bool, bool>> b) { if (a.first == b.first) { return (b.second.first && b.second.second); } return a.first < b.first; } int main() { ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); cout << fixed << setprecision(0); INPUT(); int n, k; cin >> n >> k; vector<pair<int, pair<bool, bool>>> v(n), fir, sec, bot; for (int i = 0; i < n; i++) { cin >> v[i].first >> v[i].second.first >> v[i].second.second; if (v[i].second.first && v[i].second.second) bot.push_back(v[i]); else if (v[i].second.first) fir.push_back(v[i]); else if (v[i].second.second) sec.push_back(v[i]); } sort(bot.begin(), bot.end()); sort(fir.begin(), fir.end()); sort(sec.begin(), sec.end()); vector<long long> botCum(((int)((bot).size())) + 1, 0), firCum(((int)((fir).size())) + 1, 0), secCum(((int)((sec).size())) + 1, 0); for (int i = 1; i <= ((int)((bot).size())); i++) { botCum[i] = bot[i - 1].first + botCum[i - 1]; } for (int i = 1; i <= ((int)((fir).size())); i++) { firCum[i] = fir[i - 1].first + firCum[i - 1]; } for (int i = 1; i <= ((int)((sec).size())); i++) { secCum[i] = sec[i - 1].first + secCum[i - 1]; } long long ans = INF; for (int i = 0; i <= k; i++) { int need = k - i; if (((int)((bot).size())) < i || ((int)((fir).size())) < need || ((int)((sec).size())) < need) continue; ans = min(ans, botCum[i] + firCum[need] + secCum[need]); } cout << (ans == INF ? -1 : ans); return 0; }
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << "\n"; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } inline long long sbt(long long x) { return __builtin_popcountll(x); } inline long long iceil(long double a) { return (long long)(ceil(a)); } inline long long abceil(long long a, long long b) { return (a + b - 1) / b; } inline long long mul(long long a, long long b, long long m = (long long)(1e9 + 7)) { return ((a % m) * (b % m)) % m; } inline long long add(long long a, long long b, long long m = (long long)(1e9 + 7)) { return (a + b) % m; } inline long long sub(long long a, long long b, long long m = (long long)(1e9 + 7)) { return (a - b + m) % m; } long long fastpow(long long a, long long b, long long m = (long long)(1e9 + 7)) { long long res = 1; while (b > 0) { if (b & 1) res = mul(res, a, m); a = mul(a, a, m); b >>= 1; } return res; } long long modinv(long long a, long long m = (long long)(1e9 + 7)) { return fastpow(a, m - 2, m); } void get_ac() { long long a, b, c, n, m, k; cin >> n >> m >> k; vector<pair<long long, long long> > alice, bob, both, none; for (auto i = 1; i <= n; i++) { cin >> a >> b >> c; if (b & c) both.push_back({a, i}); else if (b) alice.push_back({a, i}); else if (c) bob.push_back({a, i}); else none.push_back({a, i}); } long long ans = 0; set<long long> indices; sort(bob.begin(), bob.end()); sort(alice.begin(), alice.end()); sort(both.begin(), both.end()); sort(none.begin(), none.end()); for (long i = 0; i < min(k, (long long)(both.size())); i++) { ans += both[i].first; indices.insert(both[i].second); } for (long i = 0; i < min({(long long)(alice.size()), (long long)(bob.size()), max(k - (long long)(both.size()), 0LL)}); i++) { ans += alice[i].first + bob[i].first; indices.insert(alice[i].second); indices.insert(bob[i].second); } long long inone = 0, ibob = min({(long long)(alice.size()), (long long)(bob.size()), max(k - (long long)(both.size()), 0LL)}), ialice = min({(long long)(alice.size()), (long long)(bob.size()), max(k - (long long)(both.size()), 0LL)}), iboth = min(k, (long long)(both.size())); long long nboth = (long long)(both.size()), nalice = (long long)(alice.size()), nbob = (long long)(bob.size()), nnone = (long long)(none.size()); if ((iboth + ialice) < k || (long long)(indices.size()) > m) { cout << -1; return; } while ((long long)(indices.size()) < m) { vector<pair<long long, long long> > candidate; if (inone < nnone) candidate.push_back({none[inone].first, 0}); if (ialice < nalice) candidate.push_back({alice[ialice].first, 2}); if (ibob < nbob) candidate.push_back({bob[ibob].first, 1}); if (iboth < nboth) candidate.push_back({both[iboth].first, 3}); if (iboth > 0 && ialice < nalice && ibob < nbob) candidate.push_back( {alice[ialice].first + bob[ibob].first - both[iboth - 1].first, 4}); sort(candidate.begin(), candidate.end()); if ((long long)(candidate.size()) == 0) break; long long f = candidate[0].second; if (f == 0) { ans += none[inone].first; indices.insert(none[inone].second); inone++; } else if (f == 1) { ans += bob[ibob].first; indices.insert(bob[ibob].second); ibob++; } else if (f == 2) { ans += alice[ialice].first; indices.insert(alice[ialice].second); ialice++; } else if (f == 3) { ans += both[iboth].first; indices.insert(both[iboth].second); iboth++; } else { ans += alice[ialice].first + bob[ibob].first - both[iboth - 1].first; indices.erase(both[iboth - 1].second); indices.insert(alice[ialice].second); indices.insert(bob[ibob].second); ialice++; ibob++; iboth--; } } if ((long long)(indices.size()) < m) { cout << -1; return; } cout << ans; cout << "\n"; for (auto i : indices) { cout << i << " "; } } int main() { cin.sync_with_stdio(false); cout.sync_with_stdio(false); cin.tie(NULL); { get_ac(); cout << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; const int N = 1e5 + 5e4 + 2; const int inf = 2e9; const long long linf = 4e18; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cerr.tie(0); int n, k; cin >> n >> k; vector<int> alice, bob, common; for (int i = 0; i < n; i++) { int t, a, b; cin >> t >> a >> b; if (a && b) common.push_back(t); else if (a) alice.push_back(t); else if (b) bob.push_back(t); } sort(alice.begin(), alice.end()); sort(bob.begin(), bob.end()); priority_queue<int, vector<int>, greater<int>> q; for (int i = 0; i < common.size(); i++) q.push(common[i]); for (int i = 0; i < min(alice.size(), bob.size()); i++) q.push(alice[i] + bob[i]); if (q.size() < k) { cout << -1; return 0; } int ans = 0; while (k--) { ans += q.top(); q.pop(); } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { long long int n, k; cin >> n >> k; vector<long long int> a, b, c, d; a.emplace_back(0), b.emplace_back(0), c.emplace_back(0); for (int i = 0; i < n; i++) { int x, y, z; cin >> x >> y >> z; if (y + z == 2) { a.emplace_back(x); } else if (y == 1) { b.emplace_back(x); } else if (z == 1) { c.emplace_back(x); } } sort(a.begin(), a.end()); sort(b.begin(), b.end()); sort(c.begin(), c.end()); for (int i = 1; i < ((long long)a.size()); i++) { a[i] = a[i] + a[i - 1]; } for (int i = 1; i < ((long long)b.size()); i++) b[i] = b[i] + b[i - 1]; for (int i = 1; i < ((long long)c.size()); i++) c[i] = c[i] + c[i - 1]; long long int ans = (long long int)1e18 + 7; for (int i = 0; i < min(k + 1, ((long long)a.size())); i++) { if (k - i < ((long long)b.size()) && k - i < ((long long)c.size())) { ans = min(ans, a[i] + b[k - i] + c[k - i]); } } if (ans == (long long int)1e18 + 7) cout << -1 << "\n"; else cout << ans << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(0); int t = 1; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; void solve() {} int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, k; cin >> n >> k; vector<long long> times[4]; vector<long long> sums[4]; for (long long i = 0; i < n; ++i) { long long t, a, b; cin >> t >> a >> b; times[a * 2 + b].push_back(t); } for (long long i = 0; i < 4; ++i) { sort(times[i].begin(), times[i].end()); sums[i].push_back(0); for (auto it : times[i]) { sums[i].push_back(sums[i].back() + it); } } long long ans = INF; for (long long cnt = 0; cnt < min(k + 1, (long long)(sums[3].size())); ++cnt) { if (k - cnt < (long long)(sums[1].size()) && k - cnt < (long long)(sums[2].size())) { ans = min(ans, sums[3][cnt] + sums[1][k - cnt] + sums[2][k - cnt]); } } if (ans == INF) ans = -1; cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; void fast() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int main() { long long int n, i, j, w, x, y, k, cv1 = 0, cv2 = 0, cv3 = 0, c = 0, ans = 0; cin >> n >> k; vector<long long int> v2, v1, v3; for (i = 0; i < n; i++) { cin >> w >> x >> y; if (x == 1 && y == 1) { cv3++; v3.push_back(w); } else if (x == 0 && y == 1) { v2.push_back(w); cv2++; } else if (x == 1 && y == 0) { v1.push_back(w); cv1++; } } sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); sort(v3.begin(), v3.end()); long long int i1 = 0, i3 = 0; while (c < k && (min(cv1, cv2) > 0 || cv3 > 0)) { if (min(cv1, cv2) == 0 && cv3 > 0) { ans += v3[i3]; i3++; cv3--; c++; } else if (min(cv1, cv2) > 0 && cv3 == 0) { ans += v1[i1] + v2[i1]; cv1--; cv2--; c++; i1++; } else if (min(cv1, cv2) == 0 && cv3 == 0) { break; } else if (v1[i1] + v2[i1] >= v3[i3]) { ans += v3[i3]; i3++; c++; cv3--; } else { ans += v1[i1] + v2[i1]; cv1--; cv2--; c++; i1++; } } if (c == k) cout << ans << endl; else cout << "-1" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using vl = vector<long long>; using vs = vector<string>; using vvl = vector<vector<long long>>; const long long INF = 1LL << 60; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } bool compare(pair<long long, long long> a, pair<long long, long long> b) { if (a.first != b.first) { return a.first < b.first; } else { return a.second < b.second; } } bool In_map(long long y, long long x, long long h, long long w) { if (y < 0 || x < 0 || y >= h || x >= w) { return 0; } else { return 1; } } void print(vector<long long> a) { for (int i = 0; i < (a.size()); i++) { if (i == a.size() - 1) { cout << a[i] << endl; } else { cout << a[i] << " "; } } } const vector<long long> dx{1, 0, -1, 0}; const vector<long long> dy{0, 1, 0, -1}; void Main() { long long n, k; cin >> n >> k; vl a, b, ab; for (int i = 0; i < (n); i++) { long long t, A, B; cin >> t >> A >> B; if (A == 1 && B == 1) { ab.push_back(t); } else if (A == 1) { a.push_back(t); } else if (B == 1) { b.push_back(t); } } sort(ab.begin(), ab.end()); sort(a.begin(), a.end()); sort(b.begin(), b.end()); if (ab.size() + a.size() < k || ab.size() + b.size() < k) { cout << -1 << endl; return; } long long ans = INF; vl suma(a.size() + 1), sumb(b.size() + 1), sumab(ab.size() + 1); for (int i = 0; i < (a.size()); i++) { suma[i + 1] = suma[i] + a[i]; } for (int i = 0; i < (b.size()); i++) { sumb[i + 1] = sumb[i] + b[i]; } for (int i = 0; i < (ab.size()); i++) { sumab[i + 1] = sumab[i] + ab[i]; } long long idx1 = min(k, (long long)ab.size()); long long idx2 = k - idx1; while (1) { if (idx1 < 0 || idx2 > min(a.size(), b.size())) { break; } chmin(ans, sumab[idx1] + suma[idx2] + sumb[idx2]); idx1--; idx2++; } cout << ans << endl; return; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t = 1; while (t--) { Main(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; vector<int> a, b, ab; for (int i = 1; i <= n; ++i) { int x, l, r; cin >> x >> l >> r; if (l == 1 and r == 0) { a.push_back(x); } else if (l == 0 and r == 1) { b.push_back(x); } else if (l == 1 and r == 1) { ab.push_back(x); } } sort(a.begin(), a.end()); sort(b.begin(), b.end()); sort(ab.begin(), ab.end()); for (int i = 1; i < a.size(); ++i) { a[i] += a[i - 1]; } for (int i = 1; i < b.size(); ++i) { b[i] += b[i - 1]; } for (int i = 1; i < ab.size(); ++i) { ab[i] += ab[i - 1]; } int answer = 2e9 + 10; for (int i = 0; i <= k; ++i) { int x = k - i, t = 2e9 + 10; if (i == 0) { if (x <= ab.size()) { t = ab[x - 1]; } } else if (i == k) { if (i <= a.size() and i <= b.size()) { t = a[i - 1] + b[i - 1]; } } else { if (x <= ab.size() and i <= a.size() and i <= b.size()) { t = ab[x - 1] + a[i - 1] + b[i - 1]; } } answer = min(answer, t); } if (answer == 2e9 + 10) { answer = -1; } cout << answer; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; inline int read() { int x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - 48; ch = getchar(); } return x * f; } priority_queue<int, vector<int>, greater<int>> v, v1, v2; int main() { int n = read(), k = read(); for (int i = 0; i < n; i++) { int t = read(), a = read(), b = read(); if (a && b) { v.push(t); } else if (a) { v1.push(t); } else if (b) { v2.push(t); } } long long sum = 0; if (v.size() + min(v1.size(), v2.size()) >= k) { for (int i = 0; i < k; i++) { if (v.size() && v1.size() && v2.size()) { if (v.top() < v1.top() + v2.top()) { sum += v.top(); v.pop(); } else { sum += v1.top() + v2.top(); v1.pop(); v2.pop(); } } else if (v.size()) { sum += v.top(); v.pop(); } else if (v1.size() && v2.size()) { sum += v1.top() + v2.top(); v1.pop(); v2.pop(); } } printf("%lld\n", sum); } else { printf("-1\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 7; int n, k; vector<int64_t> a; vector<int64_t> b; vector<int64_t> c; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> k; for (int i = 1; i <= n; i++) { int64_t _t; bool _a, _b; cin >> _t >> _a >> _b; if (_a && _b) { c.push_back(_t); } else { if (_a) a.push_back(_t); else if (_b) b.push_back(_t); } } a.push_back(0); b.push_back(0); c.push_back(0); sort(a.begin(), a.end()); sort(b.begin(), b.end()); sort(c.begin(), c.end()); for (int i = 1; i < int(a.size()); i++) a[i] += a[i - 1]; for (int i = 1; i < int(b.size()); i++) b[i] += b[i - 1]; for (int i = 1; i < int(c.size()); i++) c[i] += c[i - 1]; bool done = false; int64_t ans = 0; for (int i = 0; i <= k; i++) { if ((c.size() > i) && (min(a.size(), b.size()) > (k - i))) { if (!done) ans = c[i] + a[k - i] + b[k - i]; else ans = min(ans, c[i] + a[k - i] + b[k - i]); done = true; } } if (!done) cout << -1 << '\n'; else cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> using std::cin; using std::cout; using std::endl; using std::string; void __Check(bool condition, const char* expression, int line) { if (!condition) { fprintf(stderr, "Check failed at line %d: %s\n", line, expression); exit(-1); } } template <class Collection, class Key> bool ContainsKey(const Collection& collection, const Key& key) { return collection.find(key) != collection.end(); } const int INF = 0x3F3F3F3F; const long long INF64 = 0x3F3F3F3F3F3F3F3F; const int INIT = -1; int n, m, k; std::vector<std::pair<int, int> > g[4]; int pos[4]; void Solve() { if (((int)g[3].size()) + std::min(((int)g[1].size()), ((int)g[2].size())) < k) { cout << "-1" << endl; return; } int want3 = std::min(((int)g[3].size()), k); int cnt = want3; int sum = 0; for (; pos[3] < want3; pos[3]++) { sum += g[3][pos[3]].first; } for (int i = want3; i < k; i++) { sum += g[1][pos[1]++].first; sum += g[2][pos[2]++].first; cnt += 2; } (__Check(pos[1] == pos[2], "pos[1] == pos[2]", 60)); (__Check(want3 + 2 * pos[1] >= k, "want3 + 2 * pos[1] >= k", 61)); if (cnt > m) { cout << "-1" << endl; return; } for (; cnt < m; cnt++) { std::vector<int> ar = { pos[0] < ((int)g[0].size()) ? g[0][pos[0]].first : INF, pos[1] < ((int)g[1].size()) ? g[1][pos[1]].first : INF, pos[2] < ((int)g[2].size()) ? g[2][pos[2]].first : INF, pos[3] < ((int)g[3].size()) ? g[3][pos[3]].first : INF}; if (pos[3] > 0 && pos[1] < ((int)g[1].size()) && pos[2] < ((int)g[2].size())) { int last3 = g[3][pos[3] - 1].first; int next12 = g[1][pos[1]].first + g[2][pos[2]].first; int cost = next12 - last3; ar.push_back(cost); } int mi = std::min_element((ar).begin(), (ar).end()) - ar.begin(); if (mi == 4) { sum -= g[3][--pos[3]].first; sum += g[1][pos[1]++].first; sum += g[2][pos[2]++].first; } else { sum += g[mi][pos[mi]++].first; } } cout << sum << endl; for (int i = 0; i < 4; i++) { for (int j = 0; j < pos[i]; j++) { cout << g[i][j].second + 1 << " "; } } cout << endl; } int main() { cin >> n >> m >> k; for (int i = 0; i < n; i++) { int t, x, y; cin >> t >> x >> y; int idx = (x << 1) | y; g[idx].push_back({t, i}); } for (int i = 0; i < 4; i++) { std::sort((g[i]).begin(), (g[i]).end()); } Solve(); }
#include <bits/stdc++.h> using namespace std; const int inf = 1 << 30; const long long mod = 1e9 + 7; const long long linf = 1LL << 62; const double EPS = 1e-7; template <class T> void chmin(T& x, T y) { if (x > y) x = y; } template <class T> void chmax(T& x, T y) { if (x < y) x = y; } int n, m, k; vector<pair<long long, long long> > g[4]; int solve(int mode, int pos) { if (pos == -1) { cout << -1 << endl; return 0; } int mx = min((int)g[0].size(), m); int ones = max(k - mx, 0); if (ones > min(g[1].size(), g[2].size()) || mx + ones * 2 > m) { return -1; } long long sum = 0; for (int i = 0; i < mx; i++) sum += g[0][i].first; set<pair<int, int> > fr, mn; for (int i = 0; i < ones; i++) sum += g[1][i].first + g[2][i].first; for (int i = ones; i < g[1].size(); i++) fr.insert(g[1][i]); for (int i = ones; i < g[2].size(); i++) fr.insert(g[2][i]); for (auto u : g[3]) fr.insert(u); for (int i = 0; i < m - (ones * 2 + mx); i++) { sum += fr.begin()->first; mn.insert(*fr.begin()); fr.erase(fr.begin()); } long long res = mx, val = sum; for (int sz11 = mx - 1; sz11 >= 0; sz11--) { if (mode == 1 && pos > sz11) break; if (fr.empty()) break; sum -= g[0][sz11].first; sum += fr.begin()->first; mn.insert(*fr.begin()); fr.erase(fr.begin()); if (sz11 < k) { if (ones < g[1].size() && ones < g[2].size()) { if (mn.size() < 2) break; auto f = [&](int num) { if (mn.find(g[num][ones]) != mn.end()) { mn.erase(g[num][ones]); } else { sum += g[num][ones].first; auto it = mn.end(); it--; sum -= it->first; mn.erase(it); } }; f(1), f(2); ones++; } else { break; } } if (val > sum) { res = sz11; val = sum; } } if (mode == 1) { cout << val << endl; for (int i = 0; i < pos; i++) cout << g[0][i].second << " "; for (int i = 0; i < ones; i++) cout << g[1][i].second << " "; for (int i = 0; i < ones; i++) cout << g[2][i].second << " "; for (auto u : mn) cout << u.second << " "; cout << endl; } return res; } int main() { cin >> n >> m >> k; for (int i = 1; i <= n; i++) { int t, a, b; cin >> t >> a >> b; if (a == 1 && b == 1) g[0].push_back({t, i}); else if (a == 1) g[1].push_back({t, i}); else if (b == 1) g[2].push_back({t, i}); else g[3].push_back({t, i}); } for (int i = 0; i < 4; i++) sort(g[i].begin(), g[i].end()); solve(1, solve(0, 0)); }
#include <bits/stdc++.h> #pragma GCC target("avx") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using std::cin; using std::cout; using std::lower_bound; using std::string; using std::upper_bound; using std::vector; using vi = vector<long long>; using vii = vector<vi>; using pii = std::pair<long long, long long>; constexpr long long MOD = 1e9 + 7; constexpr long long MAX = 1e7; constexpr long long INF = (1ll << 60); template <class T> class prique : public std::priority_queue<T, std::vector<T>, std::greater<T>> { }; template <typename T> struct Segment_tree { long long N; T mem; vector<T> node; Segment_tree(vector<T> &X, T m) : mem(m) { long long sz = X.size(); N = 1; while (N < sz) N *= 2; node.resize(2 * N - 1, mem); for (long long i = (long long)(0); i < (long long)(sz); i++) node[N - 1 + i] = X[i]; for (long long i = (long long)(N - 2); (long long)(0) <= i; i--) { node[i] = Compare(node[i * 2 + 1], node[i * 2 + 2]); } } T Compare(T &A, T &B) { return std::min(A, B); } void update(long long X, T val) { X += N - 1; node[X] = val; while (X > 0) { X = (X - 1) / 2; node[X] = Compare(node[X * 2 + 1], node[X * 2 + 2]); } } T Query(long long a, long long b, long long now, long long l, long long r) { if (r < 0) r = N; if (r <= a || b <= l) return mem; if (a <= l && r <= b) return node[now]; auto vl = Query(a, b, now * 2 + 1, l, (l + r) / 2), vr = Query(a, b, now * 2 + 2, (l + r) / 2, r); return Compare(vl, vr); } }; struct Binary_indexed_tree { int N; vi bit; Binary_indexed_tree(int n) : N(n) { bit.resize(N + 1, 0); } void add(int x, int a) { for (x; x <= N; x += (x & -x)) bit[x] += a; } long long sum(int x) { long long ret = 0; for (x; x > 0; x -= (x & -x)) ret += bit[x]; return ret; } long long lower_bound(long long X) { if (sum(N) < X) return -1; long long ret = 0, memo = 1, sum = 0; while (memo * 2 <= N) memo *= 2; while (memo > 0) { if (memo + ret <= N && sum + bit[memo + ret] < X) { sum += bit[memo + ret]; ret += memo; } memo /= 2; } return ret + 1; } }; struct Union_Find { long long N; vi par; vi siz; Union_Find(int n) : N(n) { par.resize(N); siz.resize(N, 1); for (long long i = (long long)(0); i < (long long)(N); i++) par[i] = i; } long long root(long long X) { if (par[X] == X) return X; return par[X] = root(par[X]); } bool same(long long X, long long Y) { return root(X) == root(Y); } void unite(long long X, long long Y) { X = root(X); Y = root(Y); if (X == Y) return; par[X] = Y; siz[Y] += siz[X]; siz[X] = 0; } long long size(long long X) { return siz[root(X)]; } }; long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } vi fac, finv, inv; void COMinit() { fac.resize(MAX); finv.resize(MAX); inv.resize(MAX); fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } long long COM(long long n, long long r) { if (n < r || n < 0 || r < 0) return 0; return fac[n] * finv[r] % MOD * finv[n - r] % MOD; } long long modinv(long long a, long long m) { long long b = m, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); } u %= m; if (u < 0) u += m; return u; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); long long N, K, M; cin >> N >> M >> K; vii memo(4); vector<std::set<pii>> p(4); for (long long i = (long long)(0); i < (long long)(N); i++) { long long T, A, B; cin >> T >> A >> B; long long sum = 0; if (A) sum += 1; if (B) sum += 2; memo[sum].emplace_back(T); p[sum].insert(std::make_pair(T, i)); } for (long long i = (long long)(0); i < (long long)(4); i++) sort((memo[i]).begin(), (memo[i]).end()); std::tuple<long long, long long, long long, long long> ans = std::make_tuple(INF, INF, INF, INF); vii sum(4, vi(1)); for (long long i = (long long)(0); i < (long long)(4); i++) { for (long long j = (long long)(0); j < (long long)(memo[i].size()); j++) sum[i].emplace_back(sum[i][j] + memo[i][j]); } for (long long i = (long long)(0); i <= (long long)(memo[3].size()); i++) { if (K < i) break; vi max(4); if (K - i > (long long)memo[1].size() || K - i > (long long)memo[2].size()) continue; max[3] = i; max[2] = max[1] = K - i; if (max[1] + max[2] + max[3] > M) continue; long long left = 0, right = INF; while (left + 1 < right) { long long mid = (left + right) / 2; long long cnt = 0; for (long long j = (long long)(0); j < (long long)(4); j++) { cnt += std::max( (long long)distance(memo[j].begin(), upper_bound((memo[j]).begin(), (memo[j]).end(), (long long)(mid))), max[j]); } if (cnt >= M) right = mid; else left = mid; } long long Sum = 0, cnt = 0; for (long long j = (long long)(0); j < (long long)(4); j++) { long long X = std::max( (long long)distance(memo[j].begin(), upper_bound((memo[j]).begin(), (memo[j]).end(), (long long)(right))), max[j]); cnt += X; Sum += sum[j][X]; } Sum -= right * (cnt - M); ans = std::min(ans, std::make_tuple(Sum, i, right, cnt - M)); } long long Ans, X, Y, differ; std::tie(Ans, X, Y, differ) = ans; if (Ans == INF) { cout << -1 << "\n"; return 0; } cout << Ans << "\n"; vi max(4); max[3] = X; max[2] = max[1] = K - X; std::set<long long> ind; for (long long i = (long long)(0); i < (long long)(4); i++) { long long Z = std::max( max[i], (long long)distance( memo[i].begin(), upper_bound((memo[i]).begin(), (memo[i]).end(), (long long)(Y)))); auto it = p[i].begin(); for (long long j = (long long)(0); j < (long long)(Z); j++) { ind.insert((*it).second); it++; } } while (differ > 0) { for (long long i = (long long)(0); i < (long long)(4); i++) { long long Z = std::max( max[i], (long long)distance( memo[i].begin(), upper_bound((memo[i]).begin(), (memo[i]).end(), (long long)(Y)))); auto it = p[i].begin(); for (long long j = (long long)(0); j < (long long)(Z); j++) { if (j >= max[i] && (*it).first == Y) { differ--; ind.erase((*it).second); if (differ == 0) goto XYZ; } it++; } } XYZ:; } for (auto p : ind) cout << p + 1 << " "; cout << "\n"; }
#include <bits/stdc++.h> using namespace std; const long long N = 2e5 + 10; const long long inf = 1e9; struct node { long long num, tim; } A[N], B[N], C[N], D[N]; bool cmp(node l1, node l2) { return l1.tim < l2.tim; } signed main() { ios::sync_with_stdio(false); cin.tie(0); long long n, m, k, x, y, z, a = 0, b = 0, c = 0, d = 0; cin >> n >> m >> k; for (long long i = 1; i <= n; ++i) { cin >> x >> y >> z; if (y && z) { C[c].num = i; C[c++].tim = x; } else if (y) { A[a].num = i; A[a++].tim = x; } else if (z) { B[b].num = i; B[b++].tim = x; } else { D[d].num = i; D[d++].tim = x; } } if (a + c < k || b + c < k) { cout << -1 << endl; return 0; } if (c < k && (k - c) * 2 + c > m) { cout << -1 << endl; return 0; } sort(A, A + a, cmp); sort(B, B + b, cmp); sort(C, C + c, cmp); sort(D, D + d, cmp); long long ai = 0, bi = 0, ci = 0, di = 0, sum = 0; for (; ci < c; ++ci) { if (ci == k) break; sum += C[ci].tim; } while (ai + ci < k) { sum += A[ai].tim + B[bi].tim; ai++, bi++; } while (ai + bi + ci + di < m) { long long pro1, pro2, pro3, pro4, pro5; if (ai < a && bi < b && ci) pro1 = A[ai].tim + B[bi].tim - C[ci - 1].tim; else pro1 = inf; if (ai < a) pro2 = A[ai].tim; else pro2 = inf; if (bi < b) pro3 = B[bi].tim; else pro3 = inf; if (ci < c) pro4 = C[ci].tim; else pro4 = inf; if (di < d) pro5 = D[di].tim; else pro5 = inf; long long mini = min(pro1, min(pro2, min(pro3, min(pro4, pro5)))); if (mini == inf) { cout << -1 << endl; return 0; } sum += mini; if (mini == pro1 && pro1 < inf) ai++, bi++, ci--; else if (mini == pro2 && pro2 < inf) ai++; else if (mini == pro3 && pro3 < inf) bi++; else if (mini == pro4 && pro4 < inf) ci++; else if (mini == pro5 && pro5 < inf) di++; } cout << sum << endl; for (long long i = 0; i < ai; ++i) cout << A[i].num << " "; for (long long i = 0; i < bi; ++i) cout << B[i].num << " "; for (long long i = 0; i < ci; ++i) cout << C[i].num << " "; for (long long i = 0; i < di; ++i) cout << D[i].num << " "; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, k, x, xa, xb, aa = 0, bb = 0, cc; vector<int> a, b, c; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> x >> xa >> xb; aa += xa; bb += xb; if (xa & xb) c.push_back(x); else if (xa) a.push_back(x); else if (xb) b.push_back(x); } if (aa < k || bb < k) { cout << -1; return 0; } aa = 0; bb = 0; cc = 0; int ka = a.size(), kb = b.size(), kc = c.size(); x = 0; sort(a.begin(), a.end()); sort(b.begin(), b.end()); sort(c.begin(), c.end()); for (int i = 0; i < k; i++) { if (cc >= kc) x += a[aa++] + b[bb++]; else if (aa < ka && bb < kb) { if (a[aa] + b[bb] < c[cc]) x += a[aa++] + b[bb++]; else x += c[cc++]; } else { x += c[cc++]; } } cout << x; return 0; }