solution
stringlengths 11
983k
| difficulty
int64 0
21
| language
stringclasses 2
values |
---|---|---|
import io,os
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
import sys
def solve(n,k,TAB):
A,B,AB=[],[],[]
cnt_a,cnt_b=0,0
for t,a,b in TAB:
cnt_a+=a; cnt_b+=b
if a*b==1:
AB.append(t)
elif a==1:
A.append(t)
elif b==1:
B.append(t)
if cnt_a<k or cnt_b<k:
return -1
AB.sort(); A.sort(); B.sort()
AB=AB[:min(k,len(AB))]
l=len(AB)
remain=k-l
ans=sum(AB)+sum(A[:remain])+sum(B[:remain])
tmp=ans
for i in range(min(l,len(A)-remain,len(B)-remain)):
tmp-=AB.pop()
tmp+=A[remain+i]+B[remain+i]
ans=min(ans,tmp)
return ans
def main():
n,k=map(int,input().split())
TAB=[list(map(int,input().split())) for _ in range(n)]
ans=solve(n,k,TAB)
sys.stdout.write(str(ans)+'\n')
if __name__=='__main__':
main() | 11 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
int stack_A[MAXN];
int stack_B[MAXN];
int stack_AB[MAXN];
int main() {
ios ::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
int a_cnt = 0;
int b_cnt = 0;
int ab_cnt = 0;
int x, y, z;
for (int i = 0; i < n; ++i) {
cin >> x >> y >> z;
if (y & z) {
stack_AB[ab_cnt] = x;
ab_cnt += 1;
} else if (y) {
stack_A[a_cnt] = x;
a_cnt += 1;
} else if (z) {
stack_B[b_cnt] = x;
b_cnt += 1;
}
}
if (a_cnt + ab_cnt < k || b_cnt + ab_cnt < k) {
cout << -1 << endl;
return 0;
}
sort(stack_A, stack_A + a_cnt);
sort(stack_B, stack_B + b_cnt);
sort(stack_AB, stack_AB + ab_cnt);
int a = 0;
int b = 0;
int ab = 0;
int a_k = 0;
int b_k = 0;
int ans = 0;
while (!(a_k >= k && b_k >= k)) {
if (a < a_cnt && b < b_cnt && ab < ab_cnt) {
if (a_k < k && b_k < k) {
if (stack_AB[ab] <= stack_A[a] + stack_B[b]) {
ans += stack_AB[ab];
ab += 1;
} else {
ans += stack_A[a] + stack_B[b];
a += 1;
b += 1;
}
a_k += 1;
b_k += 1;
} else if (a_k < k) {
if (stack_A[a] <= stack_AB[ab]) {
ans += stack_A[a];
a += 1;
a_k += 1;
} else {
ans += stack_AB[ab];
ab += 1;
a_k += 1;
b_k += 1;
}
} else if (b_k < k) {
if (stack_B[b] <= stack_AB[ab]) {
ans += stack_B[b];
b += 1;
b_k += 1;
} else {
ans += stack_AB[ab];
ab += 1;
a_k += 1;
b_k += 1;
}
}
} else if (a < a_cnt && b < b_cnt) {
if (a_k < k && b_k < k) {
ans += stack_A[a] + stack_B[b];
a += 1;
b += 1;
a_k += 1;
b_k += 1;
} else if (a_k < k) {
ans += stack_A[a];
a += 1;
a_k += 1;
} else if (b_k < k) {
ans += stack_B[b];
b += 1;
b_k += 1;
}
} else if (a < a_cnt && ab < ab_cnt) {
if (a_k < k && b_k >= k && stack_A[a] <= stack_AB[ab]) {
ans += stack_A[a];
a += 1;
a_k += 1;
} else {
ans += stack_AB[ab];
ab += 1;
a_k += 1;
b_k += 1;
}
} else if (b < b_cnt && ab < ab_cnt) {
if (b_k < k && a_k >= k && stack_B[b] <= stack_AB[ab]) {
ans += stack_B[b];
b += 1;
b_k += 1;
} else {
ans += stack_AB[ab];
ab += 1;
a_k += 1;
b_k += 1;
}
} else if (ab < ab_cnt) {
ans += stack_AB[ab];
ab += 1;
a_k += 1;
b_k += 1;
} else if (a < a_cnt) {
ans += stack_A[a];
a += 1;
a_k += 1;
} else if (b < b_cnt) {
ans += stack_B[b];
b += 1;
b_k += 1;
}
}
cout << ans << endl;
return 0;
}
| 11 | CPP |
from math import *
from collections import *
from bisect import *
import sys
input=sys.stdin.readline
t=1
while(t):
t-=1
n,k1=map(int,input().split())
oo=[]
al=[]
bob=[]
for i in range(n):
a=list(map(int,input().split()))
if(a[-1]==a[-2]==1):
oo.append(a[0])
elif(a[-1]==0 and a[-2]==1):
al.append(a[0])
elif(a[-1]==1 and a[-2]==0):
bob.append(a[0])
oo.sort()
bob.sort()
al.sort()
i,j,k=0,0,0
ak,bk=k1,k1
rr=0
l1,l2,l3=len(oo),len(al),len(bob)
gb=0
while((ak>0 or bk>0) and (i<l1 or j<l2 or k<l3)):
if(gb>n):
break
if(i<l1):
if(j<l2 and k<l3):
if(oo[i]<=bob[k]+al[j]):
ak-=1
bk-=1
rr+=oo[i]
i+=1
elif(bk<=0):
if(oo[i]<=al[j]):
rr+=oo[i]
i+=1
ak-=1
bk-=1
else:
rr+=al[j]
j+=1
ak-=1
elif(ak<=0):
if(oo[i]<=bob[k]):
rr+=oo[i]
i+=1
ak-=1
bk-=1
else:
rr+=bob[k]
bk-=1
k+=1
else:
rr+=bob[k]+al[j]
k+=1
j+=1
ak-=1
bk-=1
elif(j<l2 and k>=l3):
if(bk<=0):
if(oo[i]<=al[j]):
rr+=oo[i]
ak-=1
bk-=1
i+=1
else:
rr+=al[j]
j+=1
ak-=1
else:
rr+=oo[i]
i+=1
ak-=1
bk-=1
elif(j>=l2 and k<l3):
if(ak<=0):
if(oo[i]<=bob[k]):
rr+=oo[i]
i+=1
bk-=1
ak-=1
else:
rr+=bob[k]
k+=1
bk-=1
else:
rr+=oo[i]
i+=1
ak-=1
bk-=1
else:
rr+=oo[i]
i+=1
ak-=1
bk-=1
else:
if(ak>0 and j<l2):
rr+=al[j]
j+=1
ak-=1
if(bk>0 and k<l3):
rr+=bob[k]
k+=1
bk-=1
gb+=1
if(ak<=0 and bk<=0):
print(rr)
else:
print(-1)
| 11 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const int maxn = 1e6 + 10;
const double eps = 1e-9;
const long long inf = 1e18;
int T;
int n, m, k;
vector<long long> vec[5];
long long a, b, t;
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%lld%lld%lld", &t, &a, &b);
if (a && b)
vec[1].push_back(t);
else if (a && !b)
vec[2].push_back(t);
else if (!a && b)
vec[3].push_back(t);
else
vec[4].push_back(t);
}
for (int i = 1; i <= 4; i++) sort(vec[i].begin(), vec[i].end());
int flag = 1;
a = b = 0;
int l1 = vec[1].size(), l2 = min(vec[2].size(), vec[3].size());
long long ans = 0;
for (int i = 1; i <= k; i++) {
if (l1 == a && l2 == b) {
flag = 0;
break;
}
if (l1 == a || (l2 != b && vec[1][a] >= vec[2][b] + vec[3][b])) {
ans += vec[2][b] + vec[3][b];
b++;
} else {
ans += vec[1][a];
a++;
}
}
if (flag)
printf("%lld\n", ans);
else
printf("-1\n");
return 0;
}
| 11 | CPP |
n,k=map(int,input().split())
c=[]
d=[]
e=[]
for i in range(n):
t,a,b=map(int,input().split())
if a==1 and b==0:
c.append(t)
elif b==1 and a==0:
d.append(t)
elif a==1 and b==1:
e.append(t)
if (len(c)+len(e))<k or (len(d)+len(e))<k:
print(-1)
else:
c=sorted(c)
d=sorted(d)
e=sorted(e)
l1=len(c)
l2=len(d)
l3=len(e)
s=0
i1=0
i2=0
i3=0
while((i1+i3)<k or (i2+i3)<k):
if i3<l3:
if (i2 + i3) >= k and i1 < l1:
if e[i3] <= c[i1]:
s += e[i3]
i3 += 1
else:
s += c[i1]
i1 += 1
elif (i1 + i3) >= k and i2 < l2:
if e[i3] <= d[i2]:
s += e[i3]
i3 += 1
else:
s += d[i2]
i2 += 1
elif i1<l1 and i2<l2:
if e[i3]<=(c[i1]+d[i2]):
s+=e[i3]
i3+=1
else:
s+=(c[i1]+d[i2])
i1+=1
i2+=1
else:
s+=e[i3]
i3+=1
else:
s+=(c[i1]+d[i2])
i1+=1
i2+=1
print(s)
| 11 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios {
fast_ios() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
};
} fast_ios_;
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
for (auto &v : vec) is >> v;
return is;
}
template <typename T>
bool chmax(T &m, const T q) {
if (m < q) {
m = q;
return true;
} else
return false;
}
template <typename T>
bool chmin(T &m, const T q) {
if (q < m) {
m = q;
return true;
} else
return false;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <class c>
struct rge {
c b, e;
};
template <class c>
rge<c> range(c i, c j) {
return rge<c>{i, j};
}
template <class c>
auto dud(c *x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct deb {
~deb() { cerr << endl; }
template <class c>
typename enable_if<sizeof dud<c>(0) != 1, deb &>::type operator<<(c i) {
cerr << boolalpha << i;
return *this;
}
template <class c>
typename enable_if<sizeof dud<c>(0) == 1, deb &>::type operator<<(c i) {
return *this << range(begin(i), end(i));
}
template <class c, class b>
deb &operator<<(pair<b, c> d) {
return *this << "(" << d.first << ", " << d.second << ")";
}
template <class c>
deb &operator<<(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it;
return *this << "]";
}
};
void err(istream_iterator<string> it) { return; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...);
}
void solve() {
lint n, k;
cin >> n >> k;
vector<lint> time(n);
vector<lint> alice(n);
vector<lint> bob(n);
priority_queue<int, vector<int>, greater<int>> abra;
priority_queue<int, vector<int>, greater<int>> cadabra;
priority_queue<int, vector<int>, greater<int>> mixed;
for (int i = (0), i_end_ = (n); i < i_end_; i++) {
lint t, a, b;
cin >> t >> a >> b;
time[i] = t;
alice[i] = a;
bob[i] = b;
if (a == 1 && b == 1) {
mixed.push(t);
} else if (a == 1) {
abra.push(t);
} else if (b == 1) {
cadabra.push(t);
}
}
lint ret = 0;
lint cc = 2 * k;
lint s1 = accumulate(begin(alice), end(alice), 0LL);
lint s2 = accumulate(begin(bob), end(bob), 0LL);
if (s1 <= k - 1 || s2 <= k - 1) {
cout << -1 << '\n';
return;
}
while (cc) {
bool ok = 0;
int f = 0;
lint t = INT_MAX;
if (mixed.size() > 0) {
t = mixed.top();
ok = 1;
}
lint t1 = INT_MAX;
lint t2 = INT_MAX;
if (abra.size()) {
t1 = abra.top();
++f;
}
if (cadabra.size()) {
t2 = cadabra.top();
++f;
}
if (!ok && f <= 1) break;
if (t <= t1 + t2) {
ret += t;
mixed.pop();
} else {
ret += t1 + t2;
abra.pop();
cadabra.pop();
}
cc -= 2;
}
while (cc) {
bool ok = 0;
lint t1 = INT_MAX;
if (abra.size()) {
t1 = abra.top();
ok = 1;
}
lint t2 = INT_MAX;
if (cadabra.size()) {
t2 = cadabra.top();
ok = 1;
}
assert(ok == true);
if (t1 <= t2) {
ret += t1;
abra.pop();
} else {
ret += t2;
cadabra.pop();
}
cc -= 1;
}
cout << ret << '\n';
}
int main() {
int T = 1;
for (int tt = 1; tt <= T; ++tt) {
solve();
}
return 0;
}
lint pow_mod(lint base, lint expo) {
lint ret = 1;
for (; expo;) {
if (expo & 1) ret = (ret * base) % 1000000007;
expo = (expo >> 1);
base = (base * base) % 1000000007;
}
return ret;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, t, a, b;
long long ans = 0;
priority_queue<int, vector<int>, greater<int>> book[3];
scanf("%d%d", &n, &k);
while (n--) {
scanf("%d%d%d", &t, &a, &b);
if (a && b)
book[0].push(t);
else if (a)
book[1].push(t);
else if (b)
book[2].push(t);
}
if (book[0].size() + book[1].size() < k ||
book[0].size() + book[2].size() < k)
printf("-1\n");
else {
while (!book[0].empty() && k--)
if (book[1].empty() || book[2].empty() ||
book[0].top() <= book[1].top() + book[2].top()) {
ans += book[0].top();
book[0].pop();
} else {
ans += book[1].top() + book[2].top();
book[1].pop();
book[2].pop();
}
while ((k--) > 0) {
ans += book[1].top() + book[2].top();
book[1].pop();
book[2].pop();
}
printf("%lld\n", ans);
}
}
| 11 | CPP |
n,k=map(int, input().split())
alice=[]
bob=[]
both=[]
counta=0
countb=0
for i in range(0,n):
l1=list(map(int, input().split()))
if l1[1]==1 and l1[2]==1:
counta+=1
countb+=1
both.append(l1)
elif l1[1]==1:
counta+=1
alice.append(l1)
elif l1[2]==1:
countb+=1
bob.append(l1)
if counta<k or countb <k:
print (-1)
else:
t=0
alice.sort()
bob.sort()
both.sort()
a=0
b=0
for i in range(1,k+1):
if b>min(len(alice), len(bob))-1:
for j in range(a,a+k-i+1):
t=t+both[j][0]
break
if a>len(both)-1:
for j in range(b,b+k-i+1):
t=t+alice[j][0]+bob[j][0]
break
if both[a][0]<=alice[b][0]+bob[b][0]:
t=t+both[a][0]
a=a+1
else:
t=t+alice[b][0]+bob[b][0]
b=b+1
print (t)
| 11 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int kMaxN = 200001;
struct E {
int i, t;
bool operator<(const E &e) const { return t < e.t; }
bool operator>(const E &e) const { return t > e.t; }
} e[4][kMaxN];
bool s[kMaxN];
int c[4];
int n, m, k, x, a, b, ans, sum, tot, p;
priority_queue<E> bh;
priority_queue<E, vector<E>, greater<E> > sh;
void Make(int st) {
fill_n(s, n + 1, 0);
bh = priority_queue<E>();
sh = priority_queue<E, vector<E>, greater<E> >();
sum = 0, tot = 0;
for (int i = 1; i <= st; i++, tot++) {
sum += e[3][i].t;
s[e[3][i].i] = 1;
}
for (int i = 1; i <= k - st; i++, tot += 2) {
sum += e[1][i].t + e[2][i].t;
s[e[1][i].i] = s[e[2][i].i] = 1;
}
for (int i = 1; i <= c[0]; i++) {
sh.push(e[0][i]);
}
for (int j = 1; j <= 2; j++) {
for (int i = max(1, k - st + 1); i <= c[j]; i++) {
sh.push(e[j][i]);
}
}
for (; tot < m; tot++) {
sum += sh.top().t;
s[sh.top().i] = 1;
bh.push(sh.top());
sh.pop();
}
ans = sum;
p = st;
}
bool Find() {
int st = max(k - min(c[1], c[2]), 0);
st = max(st, m - c[0] - c[1] - c[2]);
st = max(st, 2 * k - m);
if (st > c[3] || st > m) {
return 0;
}
Make(st);
for (int i = st; i < min(c[3], m); i++) {
sum += e[3][i + 1].t;
s[e[3][i + 1].i] = 1;
tot++;
for (int j = 1; j <= 2; j++) {
if (i < k) {
sum -= e[j][k - i].t;
s[e[j][k - i].i] = 0;
sh.push(e[j][k - i]);
tot--;
}
}
while (!bh.empty() && !sh.empty() && bh.top() > sh.top()) {
sum += sh.top().t - bh.top().t;
s[sh.top().i] = 1;
s[bh.top().i] = 0;
sh.push(bh.top());
bh.push(sh.top());
sh.pop();
bh.pop();
}
for (; tot < m; tot++) {
sum += sh.top().t;
s[sh.top().i] = 1;
bh.push(sh.top());
sh.pop();
}
for (; tot > m; tot--) {
sum -= bh.top().t;
s[bh.top().i] = 0;
sh.push(bh.top());
bh.pop();
}
if (sum < ans) {
ans = sum;
p = i + 1;
}
}
return 1;
}
int main() {
cin >> n >> m >> k;
for (int i = 1; i <= n; i++) {
cin >> x >> a >> b;
a |= b * 2;
e[a][++c[a]] = {i, x};
}
for (int i = 0; i <= 3; i++) {
sort(e[i] + 1, e[i] + 1 + c[i]);
}
if (Find()) {
cout << ans << endl;
Make(p);
for (int i = 1; i <= n; i++) {
if (s[i]) {
cout << i << " ";
}
}
} else {
cout << -1;
}
return 0;
}
| 11 | CPP |
from collections import defaultdict, deque
rr = lambda: input()
rri = lambda: int(input())
rrm = lambda: list(map(int, input().split()))
INF=float('inf')
def solve(N,K,B):
groups = defaultdict(list) # alice, bob, both books
for time,alike,blike in B:
if alike and blike:
groups['c'].append(time)
elif alike:
groups['a'].append(time)
elif blike:
groups['b'].append(time)
for g in groups:
groups[g] = sorted(groups[g], reverse=True)
a,b= 0,0
amt = 0
while groups['a'] or groups['b'] or groups['c']:
if not groups['a'] or not groups['b']:
if not groups['c']:
break
amt += groups['c'].pop()
a+=1
b+=1
elif not groups['c'] or groups['a'][-1] + groups['b'][-1] < groups['c'][-1]:
amt += groups['a'].pop()
amt += groups['b'].pop()
a+=1
b+=1
else:
amt += groups['c'].pop()
a+=1
b+=1
if a == K and b == K:
break
if a < K or b < K:
return -1
return amt
#print(soloa, solob)
if aread < K or bread < K:
return -1
return totaltime + atotal + btotal
n,k = rrm()
books = [] # tuples (time, alice likes it, bob likes it)
for _ in range(n):
time,a,b=rrm()
books.append((time,a,b))
print(solve(n,k,books))
| 11 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n, k, t, a, b;
cin >> n >> k;
vector<long long int> time, alice, bob, both;
for (long long 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);
}
}
if (alice.size() + both.size() < k || bob.size() + both.size() < k) {
cout << "-1\n";
return 0;
}
sort(alice.begin(), alice.end());
sort(bob.begin(), bob.end());
sort(both.begin(), both.end());
long long int i_a = 0, i_b = 0, idx = 0, cost = 0;
while (true) {
if (i_a < alice.size() && i_b < bob.size()) {
if (idx < both.size()) {
if (alice[i_a] + bob[i_b] <= both[idx]) {
cost += (alice[i_a] + bob[i_b]);
i_a++;
i_b++;
} else {
cost += both[idx];
idx++;
}
} else {
cost += (alice[i_a] + bob[i_b]);
i_a++;
i_b++;
}
} else {
cost += both[idx];
idx++;
}
if ((i_a == k - idx) && (i_b == k - idx)) {
break;
}
}
cout << cost;
}
| 11 | CPP |
n, k = [int(i) for i in input().split()]
a, b, result = list(), list(), list()
for i in ' ' * n:
t, x, y = [int(x) for x in input().split()]
if x & y:
result += [t]
elif x:
a += [t]
elif y:
b += [t]
for i, j in zip(sorted(a), sorted(b)):
result += [i + j]
print(-1 if len(result) < k else sum(sorted(result)[:k]))
| 11 | PYTHON3 |
# t=int(input())
# for j in range(t):
n,k = map(int, input().split())
t=[]
a=[]
b=[]
for i in range(n):
t1,a1,b1 = map(int, input().split())
if(a1==1 and b1==1):
t.append(t1)
elif(a1==1):
a.append(t1)
elif(b1==1):
b.append(t1)
a.sort()
b.sort()
k1=min(len(a),len(b))
for i in range(k1):
t.append(a[i]+b[i])
ans=0
t.sort()
if(len(t)<k):
print("-1")
else:
print(sum(t[:k]))
| 11 | PYTHON3 |
# -*- coding: utf-8 -*-
import math, string, itertools, operator, fractions, heapq, collections, re, array, bisect, sys, functools
def solve(line):
n, k = map(int, line.split())
a = []
for i in range(n):
a.append([int(x) for x in sys.stdin.readline().rstrip().split()])
x, y, z = [], [], []
for i in range(n):
if a[i][1] == 1 and a[i][2] == 1:
z.append(a[i][0])
elif a[i][1] == 1:
x.append(a[i][0])
elif a[i][2] == 1:
y.append(a[i][0])
if len(x) + len(z) < k or len(y) + len(z) < k: return -1
x.sort()
y.sort()
z.sort()
ans = 0
i, j, k1 = 0, 0, 0
while k > 0:
if i < len(x) and j < len(y) and k1 < len(z) and x[i] + y[j] < z[k1]:
ans += x[i] + y[j]
i += 1
j += 1
elif k1 < len(z):
ans += z[k1]
k1 += 1
else:
ans += x[i] + y[j]
i += 1
j += 1
k -= 1
return ans
t = 0
while True:
line = sys.stdin.readline().rstrip()
if not line:
break
print(solve(line))
t += 1
| 11 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long n, k;
int main() {
scanf("%lld%lld", &n, &k);
vector<long long> v, ali, bob;
for (int i = 0; i < n; i++) {
long long t, x, y;
scanf("%lld%lld%lld", &t, &x, &y);
if (x == 1 && y == 1)
v.push_back(t);
else if (x == 1 && y == 0)
ali.push_back(t);
else if (x == 0 && y == 1)
bob.push_back(t);
}
sort(ali.begin(), ali.end());
sort(bob.begin(), bob.end());
long long mn = min(ali.size(), bob.size());
for (int i = 0; i < mn; i++) v.push_back(ali[i] + bob[i]);
sort(v.begin(), v.end());
if (v.size() < k)
printf("-1\n");
else {
long long ans = 0;
for (int i = 0; i < k; i++) ans += v[i];
printf("%lld", ans);
}
return 0;
}
| 11 | CPP |
import sys
import bisect as b
import math
from collections import defaultdict as dd
input=sys.stdin.readline
#sys.setrecursionlimit(10**7)
def cin():
return map(int,sin().split())
def ain():
return list(map(int,sin().split()))
def sin():
return input()
def inin():
return int(input())
for _ in range(1):
n,k=cin()
l=[];l1=[];l3=[]
for i in range(n):
t,a,b=cin()
if(a==1 and b==1):l3+=[t]
elif(a==1):l+=[t]
elif(b==1):l1+=[t]
l=sorted(l,reverse=True);l1=sorted(l1,reverse=True);l3=sorted(l3,reverse=True)
ans=-1;ma=10**5
if(len(l)+len(l3)>=k and len(l1)+len(l3)>=k):
ans=0;k1=k2=k
while(k1>0 and k2>0):
if(len(l1)==0):l1+=[ma]
if(len(l)==0):l+=[ma]
if(len(l3)==0):l3+=[ma]
if(l[-1]+l1[-1]>l3[-1]):
ans+=l3.pop()
else:
ans+=l1.pop();ans+=l.pop()
k1-=1;k2-=1
print(ans)
| 11 | PYTHON3 |
def solve():
n, k = list(map(int, input().split()))
a = []
b = []
both = []
alice = 0
bob = 0
coincidence = 0
for i in range(n):
t, a_, b_ = list(map(int, input().split()))
if b_ and a_:
both.append(t)
coincidence += 1
elif a_ == 1:
a.append(t)
alice += 1
elif b_ == 1:
b.append(t)
bob += 1
if alice + coincidence < k or bob + coincidence < k:
print(-1)
else:
a.sort()
b.sort()
for i in range(min(len(a), len(b))):
both.append(a[i]+b[i])
both.sort()
out = sum(both[:k])
print(out)
cases = 1
for test in range(cases):
solve() | 11 | PYTHON3 |
n, k = list(map(int, input().split()))
both = []
a = []
b = []
for item in range(n):
buff = list(map(int, input().split()))
if buff[1] == 1 and buff[2] == 1:
both.append(buff[0])
elif buff[1] == 1:
a.append(buff[0])
elif buff[2] == 1:
b.append(buff[0])
both.sort()
a.sort()
b.sort()
rez = 0
both_ind = 0
tog_ind = 0
while k > 0:
if (len(a) - tog_ind > 0 and len(b) - tog_ind > 0):
if len(both) - both_ind > 0:
if(a[tog_ind] + b[tog_ind] < both[both_ind]):
rez += a[tog_ind] + b[tog_ind]
k -= 1
tog_ind += 1
else:
rez += both[both_ind]
k -= 1
both_ind += 1
else:
k -= 1
rez += a[tog_ind] + b[tog_ind]
tog_ind += 1
elif len(both) - both_ind > 0:
k -= 1
rez += both[both_ind]
both_ind += 1
else:
print(-1)
exit()
print(rez)
| 11 | PYTHON3 |
#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<long long> a, b, ab;
int count_a = 0, count_b = 0;
for (int i = 0; i < n; i++) {
int t;
bool b1, b2;
cin >> t >> b1 >> b2;
if (b1 && b2) {
ab.push_back(t);
count_a++;
count_b++;
} else if (b1) {
a.push_back(t);
count_a++;
} else if (b2) {
b.push_back(t);
count_b++;
}
}
if (count_a < k || count_b < k) {
cout << -1 << endl;
} else {
sort(a.begin(), a.end());
sort(b.begin(), b.end());
sort(ab.begin(), ab.end());
int na = a.size(), nb = b.size(), nab = ab.size();
for (int i = 1; i < na; i++) {
a[i] += a[i - 1];
}
for (int i = 1; i < nb; i++) {
b[i] += b[i - 1];
}
for (int i = 1; i < nab; i++) {
ab[i] += ab[i - 1];
}
int x = min(k, nab);
long long min_time = LONG_LONG_MAX;
for (int i = 0; i <= x; i++) {
if (k - i <= min(na, nb)) {
if (i > 0 && i < k) {
min_time = min(min_time, ab[i - 1] + a[k - i - 1] + b[k - i - 1]);
} else if (i == 0) {
min_time = min(min_time, a[k - 1] + b[k - 1]);
} else {
min_time = min(min_time, ab[k - 1]);
}
}
}
cout << min_time << endl;
}
}
| 11 | CPP |
def binary(a,start,end,e):
mid=(start+end)//2
if a[mid]==e:
return mid
elif a[mid]>e:
end=mid-1
else:
start=mid+1
if start<=end:
return binary(a,start,end,e)
else:
return start
n,k=map(int,input().split())
p=[]
al=[]
bo=[]
for j in range(n):
a,b,c=map(int,input().split())
if b==1 and c==1:
p.append(a)
elif b==1 and c==0:
al.append(a)
elif b==0 and c==1:
bo.append(a)
al.sort()
bo.sort()
if p!=[]:
while al!=[] and bo!=[]:
v=al[0]+bo[0]
#q=binary(p,0,len(p)-1,v)
p.append(v)
del al[0]
del bo[0]
else:
while al!=[] and bo!=[]:
v=al[0]+bo[0]
p.append(v)
del al[0]
del bo[0]
p.sort()
if len(p)<k:
print(-1)
else:
print(sum(p[:k]))
| 11 | PYTHON3 |
import sys
inp = [int(x) for x in sys.stdin.read().split()]; ii = 0
n = inp[ii]; ii += 1
k = inp[ii]; ii += 1
T = inp[ii + 0: ii + 3 * n: 3]
A = inp[ii + 1: ii + 3 * n: 3]
B = inp[ii + 2: ii + 3 * n: 3]
TA = []
TB = []
TAB = []
for i in range(n):
if A[i] and B[i]:
TAB.append(T[i])
elif A[i]:
TA.append(T[i])
elif B[i]:
TB.append(T[i])
def cumsummer(A):
cumsum = [0]
for a in A:
cumsum.append(cumsum[-1] + a)
return cumsum
TA.sort()
TB.sort()
TAB.sort()
n = len(TA)
m = len(TB)
nm = len(TAB)
TA = cumsummer(TA)
TB = cumsummer(TB)
TAB = cumsummer(TAB)
time = inf = 10**9 * 2 + 100
for x in range(min(k, nm) + 1):
y = z = k - x
if n < y or m < z:
continue
time = min(time, TAB[x] + TA[y] + TB[z])
print(time if time != inf else -1) | 11 | PYTHON3 |
import io
import os
from collections import Counter, defaultdict, deque
def solve(N, K, books):
bothLikes = []
aLikes = []
bLikes = []
for t, a, b in books:
if a and b:
bothLikes.append(t)
elif a:
aLikes.append(t)
elif b:
bLikes.append(t)
if len(bothLikes) + len(aLikes) < K or len(bothLikes) + len(bLikes) < K:
return -1
bothLikes = sorted(bothLikes)[:K]
pref = [0]
for x in bothLikes:
pref.append(pref[-1] + x)
maxIndiv = min(K, len(aLikes), len(bLikes))
aLikes = sorted(aLikes)[:maxIndiv]
bLikes = sorted(bLikes)[:maxIndiv]
best = float('inf')
indivTime = 0
for i in range(maxIndiv + 1):
if K - i < len(pref):
best = min(best, indivTime + pref[K - i])
if i != maxIndiv:
indivTime += aLikes[i] + bLikes[i]
return best
if __name__ == "__main__":
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
N, K = [int(x) for x in input().split()]
books = [[int(x) for x in input().split()] for i in range(N)]
ans = solve(N, K, books)
print(ans)
| 11 | PYTHON3 |
from math import inf
from queue import Queue
n, k = [int(_) for _ in input().split()]
books = [tuple(int(_) for _ in input().split()) for __ in range(n)]
if sum(_[1] for _ in books) < k or sum(_[2] for _ in books) < k:
print(-1)
else:
mem = {}
books = sorted(books)
alice_times = []
bob_times = []
both_times = []
for t, a, b in books:
if a and b:
both_times.append(t)
elif a:
alice_times.append(t)
elif b:
bob_times.append(t)
bob_times.append(inf)
alice_times.append(inf)
both_times.append(inf)
t = 0
i = j = l = 0
alice_k = bob_k = 0
while alice_k < k or bob_k < k:
if both_times[l] < alice_times[i] + bob_times[j]:
t += both_times[l]
alice_k += 1
bob_k += 1
l += 1
else:
t += alice_times[i] + bob_times[j]
i += 1
j += 1
alice_k += 1
bob_k += 1
print(t) | 11 | PYTHON3 |
n,k = map(int,input().split())
ab = []
a = []
b = []
for i in range(n):
t,c,d = map(int,input().split())
if c and d == 0:
a.append(t)
elif d and c == 0:
b.append(t)
elif c*d:
ab.append(t)
a.sort()
b.sort()
ab.sort()
la = len(a)
lb = len(b)
lab = len(ab)
if la+lab < k or lb+lab < k:
print(-1)
exit()
if lb > la:
la,lb = lb,la
a,b = b,a
ans = 0
if lab >= k:
ans += sum(ab[:k])
now = k-1
na = 0
nb = 0
while nb < lb and now >= 0 and ab[now] > a[na]+b[nb]:
ans -= ab[now]-a[na]-b[nb]
na += 1
nb += 1
now -= 1
else:
ans += sum(ab) +sum(b[:k-lab])+sum(a[:k-lab])
now = lab-1
na = k-lab
nb = k-lab
while nb < lb and now >= 0 and ab[now] > a[na]+b[nb]:
ans -= ab[now]-a[na]-b[nb]
na += 1
nb += 1
now -= 1
print(ans)
| 11 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, k;
cin >> n >> k;
long long t[n], a[n], b[n];
vector<long long> ali, bob, both;
for (long long i = 0; i < n; i++) {
cin >> t[i] >> a[i] >> b[i];
if (a[i] == 1 && b[i] == 1) both.push_back(t[i]);
if (a[i] == 1 && b[i] == 0) ali.push_back(t[i]);
if (a[i] == 0 && b[i] == 1) bob.push_back(t[i]);
}
sort(ali.begin(), ali.end());
sort(bob.begin(), bob.end());
sort(both.begin(), both.end());
long long mini = 1000000000000;
long long x = both.size();
long long y = ali.size();
long long z = bob.size();
long long m = max(k - z, k - y);
if (x + y < k || x + z < k)
cout << "-1\n";
else {
for (long long i = max(m, 0ll); i <= min(x, k); i++) {
long long sum = 0;
for (long long j = 0; j < i; j++) {
sum += both[j];
}
for (long long j = 0; j < k - i; j++) {
sum += ali[j];
sum += bob[j];
}
mini = min(mini, sum);
}
cout << mini << "\n";
}
}
| 11 | CPP |
def p(x):
return x[0]
n, k = map(int, input().split())
a = []
for i in range(n):
a.append(list(map(int, input().split())))
a.sort(key=p)
# print()
# print()
# for i in a:
# print(i)
alice, bob, common = [], [], []
al, bo = 0, 0
flag = 1
for i in a:
if(i[1] and not i[2] and al < k):
alice.append(i[0])
al += 1
if(i[2] and (not i[1]) and bo < k):
bob.append(i[0])
bo += 1
if(i[1] and i[2]):
if(flag):
common.append(i[0])
al += 1
bo += 1
if(al > k and alice):
alice.pop()
al -= 1
if(bo > k and bob):
bob.pop()
bo -= 1
else:
if(alice and bob):
if(alice[-1]+bob[-1] > i[0]):
alice.pop()
bob.pop()
common.append(i[0])
else:
break
if(al >= k and bo >= k):
flag = 0
# print(alice, bob, common, al, bo)
if(al >= k and bo >= k):
print(sum(alice)+sum(bob)+sum(common))
else:
print(-1)
| 11 | PYTHON3 |
################om namh shivay##################37
###############(BHOLE KI FAUJ KREGI MAUJ)############37
from sys import stdin,stdout
import math,queue,heapq
fastinput=stdin.readline
fastout=stdout.write
z=1
while z:
z-=1
#n=int(fastinput())
#s=input()
n,k=map(int,fastinput().split())
#x,y,n=map(int,fastinput().split())
#a=[0]+list(map(int,fastinput().split()))
#a=list(map(int,fastinput().split()))
common=[]
onlyx=[]
onlyy=[]
for i in range(n):
t,x,y=map(int,fastinput().split())
if x==0 and y==0:
continue
else:
if x==1 and y==1:
common.append(t)
elif x==0 and y==1:
onlyy.append(t)
else:
onlyx.append(t)
#matrix=[list(map(int,fastinput().split())) for _ in range(n)]
onlyy.sort()
onlyx.sort()
if len(onlyx)>k:
onlyx=onlyx[:k]
if len(onlyy)>k:
onlyy=onlyy[:k]
if len(onlyy)+len(common)<k or len(onlyx)+len(common)<k:
print(-1)
else:
for i in range(min(len(onlyx),len(onlyy))):
common.append(onlyx[i]+onlyy[i])
common.sort()
print(sum(common[:k]))
| 11 | PYTHON3 |
it = lambda: list(map(int, input().strip().split()))
def solve():
n, k = it()
bob = []
both = []
alice = []
for _ in range(n):
t, a, b = it()
if a and b:
both.append(t)
elif a:
alice.append(t)
elif b:
bob.append(t)
M = len(both)
N = min(len(bob), len(alice))
if M + N < k:
return -1
bob.sort()
both.sort()
alice.sort()
merge = []
for i in range(N):
merge.append(alice[i] + bob[i])
a = 0
i = 0
j = 0
while k > 0 and (i < M or j < N):
k -= 1
if i >= M or (j < N and merge[j] < both[i]):
a += merge[j]
j += 1
else:
a += both[i]
i += 1
return a
if __name__ == '__main__':
print(solve()) | 11 | PYTHON3 |
n, k = map(int, input().split())
a = []
b = []
ab = []
for i in ' ' * n:
t, x, y = map(int, input().split())
if x & y:
ab += [t]
elif x:
a += [t]
elif y:
b += [t]
for i, j in zip(sorted(a), sorted(b)):
ab += [i + j]
print(-1 if len(ab) < k else sum(sorted(ab)[:k])) | 11 | PYTHON3 |
n,k=map(int,input().split());z=[];x=[];y=[]
for i in range(n):
t,a,b=map(int,input().split())
if a&b:z.append(t)
elif a:x.append(t)
elif b:y.append(t)
x.sort();y.sort()
for i in range(min(len(x),len(y))):z.append(x[i]+y[i])
print(-1if len(z)<k else sum(sorted(z)[:k])) | 11 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 200100;
int n, m, k, sa, sb, ans = 2e9 + 10000, calc, x, ty, sz1, res[N], tim;
struct item {
int t, a, b;
} a[N];
vector<pair<int, int> > b[4];
priority_queue<pair<int, int> > s, t;
bool cmp(pair<int, int> a, pair<int, int> b) { return a.first < b.first; }
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> k;
a[0].t = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i].t >> a[i].a >> a[i].b;
sa += a[i].a;
sb += a[i].b;
if (a[i].a + a[i].b == 2)
b[2].push_back({a[i].t, i});
else if (a[i].a == 1)
b[0].push_back({a[i].t, i});
else if (a[i].b == 1)
b[1].push_back({a[i].t, i});
else
b[3].push_back({a[i].t, i});
}
if (min(sa, sb) < k) {
cout << -1;
return 0;
}
sort(b[0].begin(), b[0].end(), &cmp);
sort(b[1].begin(), b[1].end(), &cmp);
sort(b[2].begin(), b[2].end(), &cmp);
sort(b[3].begin(), b[3].end(), &cmp);
sa = sb = 0;
sz1 = 0;
for (int i = 0; i < b[0].size(); i++)
if (i < k)
sa += b[0][i].first, sz1++;
else
sb += b[0][i].first, s.push({b[0][i].first, i});
for (int i = 0; i < b[1].size(); i++)
if (i < k)
sa += b[1][i].first, sz1++;
else
sb += b[1][i].first, s.push({b[1][i].first, i + n});
for (int i = 0; i < b[3].size(); i++)
sb += b[3][i].first, s.push({b[3][i].first, i + 3 * n});
while (!s.empty() && s.size() + sz1 > m) {
x = s.top().second;
s.pop();
ty = x / n;
x %= n;
sb -= b[ty][x].first;
t.push({-b[ty][x].first, x + ty * n});
}
for (int i = 0; i <= b[2].size(); i++) {
if (i + min(b[0].size(), b[1].size()) >= k && max(i, 2 * k - i) <= m &&
s.size() + sz1 == m && ans > sa + sb)
ans = sa + sb, tim = i;
if (!t.empty()) {
x = t.top().second;
t.pop();
ty = x / n;
x %= n;
sb += b[ty][x].first;
s.push({b[ty][x].first, x + ty * n});
}
if (k - i > 0 && k - i <= b[0].size())
sz1--, sa -= b[0][k - i - 1].first, sb += b[0][k - i - 1].first,
s.push({b[0][k - i - 1].first, k - i - 1});
if (k - i > 0 && k - i <= b[1].size())
sz1--, sa -= b[1][k - i - 1].first, sb += b[1][k - i - 1].first,
s.push({b[1][k - i - 1].first, k - i - 1 + n});
if (i < b[2].size()) sz1++, sa += b[2][i].first;
while (!s.empty() && s.size() + sz1 > m) {
x = s.top().second;
s.pop();
ty = x / n;
x %= n;
sb -= b[ty][x].first;
t.push({-b[ty][x].first, x + ty * n});
}
}
while (!s.empty()) s.pop();
while (!t.empty()) t.pop();
sa = sb = 0;
sz1 = 0;
for (int i = 0; i < b[0].size(); i++)
if (i < k)
sa += b[0][i].first, sz1++;
else
sb += b[0][i].first, s.push({b[0][i].first, i});
for (int i = 0; i < b[1].size(); i++)
if (i < k)
sa += b[1][i].first, sz1++;
else
sb += b[1][i].first, s.push({b[1][i].first, i + n});
for (int i = 0; i < b[3].size(); i++)
sb += b[3][i].first, s.push({b[3][i].first, i + 3 * n});
while (!s.empty() && s.size() + sz1 > m) {
x = s.top().second;
s.pop();
ty = x / n;
x %= n;
sb -= b[ty][x].first;
t.push({-b[ty][x].first, x + ty * n});
}
for (int i = 0; i <= b[2].size(); i++) {
if (i == tim) {
for (int j = 1; j <= i; j++) res[b[2][j - 1].second] = 1;
for (int j = 1; j <= min(k - i, int(b[0].size())); j++)
res[b[0][j - 1].second] = 1;
for (int j = 1; j <= min(k - i, int(b[1].size())); j++)
res[b[1][j - 1].second] = 1;
while (!s.empty()) {
x = s.top().second;
s.pop();
ty = x / n;
x %= n;
res[b[ty][x].second] = 1;
}
break;
}
if (!t.empty()) {
x = t.top().second;
t.pop();
ty = x / n;
x %= n;
sb += b[ty][x].first;
s.push({b[ty][x].first, x + ty * n});
}
if (k - i > 0 && k - i <= b[0].size())
sz1--, sa -= b[0][k - i - 1].first, sb += b[0][k - i - 1].first,
s.push({b[0][k - i - 1].first, k - i - 1});
if (k - i > 0 && k - i <= b[1].size())
sz1--, sa -= b[1][k - i - 1].first, sb += b[1][k - i - 1].first,
s.push({b[1][k - i - 1].first, k - i - 1 + n});
if (i < b[2].size()) sz1++, sa += b[2][i].first;
while (!s.empty() && s.size() + sz1 > m) {
x = s.top().second;
s.pop();
ty = x / n;
x %= n;
sb -= b[ty][x].first;
t.push({-b[ty][x].first, x + ty * n});
}
}
if (ans > 2e9) {
cout << -1;
return 0;
}
cout << ans << endl;
for (int i = 1; i <= n; i++)
if (res[i]) cout << i << " ";
}
| 11 | CPP |
#------------------------------what is this I don't know....just makes my mess faster--------------------------------------
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#----------------------------------Real game starts here--------------------------------------
#_______________________________________________________________#
def fact(x):
if x == 0:
return 1
else:
return x * fact(x-1)
def lower_bound(li, num): #return 0 if all are greater or equal to
answer = -1
start = 0
end = len(li)-1
while(start <= end):
middle = (end+start)//2
if li[middle] >= num:
answer = middle
end = middle - 1
else:
start = middle + 1
return answer #index where x is not less than num
def upper_bound(li, num): #return n-1 if all are small or equal
answer = -1
start = 0
end = len(li)-1
while(start <= end):
middle = (end+start)//2
if li[middle] <= num:
answer = middle
start = middle + 1
else:
end = middle - 1
return answer #index where x is not greater than num
def abs(x):
return x if x >=0 else -x
def binary_search(li, val, lb, ub):
ans = 0
while(lb <= ub):
mid = (lb+ub)//2
#print(mid, li[mid])
if li[mid] > val:
ub = mid-1
elif val > li[mid]:
lb = mid + 1
else:
ans = 1
break
return ans
def sieve_of_eratosthenes(n):
ans = []
arr = [1]*(n+1)
arr[0],arr[1], i = 0, 0, 2
while(i*i <= n):
if arr[i] == 1:
j = i+i
while(j <= n):
arr[j] = 0
j += i
i += 1
for k in range(n):
if arr[k] == 1:
ans.append(k)
return ans
def nc2(x):
if x == 1:
return 0
else:
return x*(x-1)//2
def kadane(x):
sum_so_far = 0
current_sum = 0
for i in x:
current_sum += i
if current_sum < 0:
current_sum = 0
else:
sum_so_far = max(sum_so_far,current_sum)
return sum_so_far
#_______________________________________________________________#
'''
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▄███████▀▀▀▀▀▀███████▄
░▐████▀▒▒Aestroix▒▒▀██████
░███▀▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▀████
░▐██▒▒▒▒▒KARMANYA▒▒▒▒▒▒████▌ ________________
░▐█▌▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒████▌ ? ? |▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒|
░░█▒▒▄▀▀▀▀▀▄▒▒▄▀▀▀▀▀▄▒▒▐███▌ ? |___CM ONE DAY___|
░░░▐░░░▄▄░░▌▐░░░▄▄░░▌▒▐███▌ ? ? |▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒|
░▄▀▌░░░▀▀░░▌▐░░░▀▀░░▌▒▀▒█▌ ? ?
░▌▒▀▄░░░░▄▀▒▒▀▄░░░▄▀▒▒▄▀▒▌ ?
░▀▄▐▒▀▀▀▀▒▒▒▒▒▒▀▀▀▒▒▒▒▒▒█ ? ?
░░░▀▌▒▄██▄▄▄▄████▄▒▒▒▒█▀ ?
░░░░▄█████████ ████=========█▒▒▐▌
░░░▀███▀▀████▀█████▀▒▌
░░░░░▌▒▒▒▄▒▒▒▄▒▒▒▒▒▒▐
░░░░░▌▒▒▒▒▀▀▀▒▒▒▒▒▒▒▐
░░░░░████████████████
'''
from math import *
for _ in range(1):
n, k = map(int, input().split())
li = []
both = []
ff = []
ss = []
a,b = 0,0
for i in range(n):
f,s,t = map(int,input().split())
if s == 1 and t == 1:
both.append(f)
a += 1
b += 1
elif s == 0 and t == 1:
ss.append(f)
b += 1
elif s == 1 and t == 0:
ff.append(f)
a += 1
# print(a,b,both,ff,ss)
if a < k or b < k:
print(-1)
else:
ss.sort()
ff.sort()
for i in range(min(len(ss),len(ff))):
both.append(ss[i]+ff[i])
# print(both)
both.sort()
sumi = 0
for i in range(k):
sumi += both[i]
print(sumi)
| 11 | PYTHON3 |
n,k=map(int,input().split())
alice=[]
bob=[]
both=[]
for _ in range(n):
t,a,b=map(int,input().split())
if a and b:
both.append(t)
elif(a):
alice.append(t)
elif(b):
bob.append(t)
if len(alice)+len(both)<k or len(bob)+len(both)<k:
print(-1)
else:
alice.sort()
bob.sort()
both.sort()
i=j=0
ans=0
while i<len(alice) and i<len(bob) and j<len(both) and k>0:
if(alice[i]+bob[i]<both[j]):
ans+=alice[i]+bob[i]
i+=1
else:
ans+=both[j]
j+=1
k-=1
if(k):
if(j>=len(both)):
while k>0:
ans+=alice[i]+bob[i]
i+=1
k-=1
else:
while k>0:
ans+=both[j]
j+=1
k-=1
print(ans) | 11 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int isPrime(int n) {
if (n < 2) return 0;
if (n < 4) return 1;
if (n % 2 == 0 or n % 3 == 0) return 0;
for (int i = 5; i * i <= n; i += 6)
if (n % i == 0 or n % (i + 2) == 0) return 0;
return 1;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int64_t n, i, k;
int64_t ans = 0;
cin >> n >> k;
vector<int64_t> a, b, c;
for (i = 0; i < n; i++) {
int64_t t1, t2, t3;
cin >> t1 >> t2 >> t3;
if (t3 and t2) {
c.push_back(t1);
} else if (t2) {
a.push_back(t1);
} else if (t3) {
b.push_back(t1);
}
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
sort(c.begin(), c.end());
if ((a.size() + c.size()) < k or (b.size() + c.size()) < k) {
cout << -1 << "\n";
return 0;
}
if (a.size() >= k and b.size() >= k) {
int64_t ind = 0, j = 0;
for (i = 0; i < k; i++) {
ans = ans + a[i] + b[i];
}
int64_t temp = ans;
for (i = 0; i < min(k, (int64_t)c.size()); i++) {
temp = temp - a[k - i - 1] - b[k - i - 1];
temp = temp + c[i];
ans = min(ans, temp);
}
cout << ans << "\n";
} else {
int64_t ind = min(a.size(), b.size());
for (i = 0; i < ind; i++) {
ans = ans + a[i] + b[i];
}
for (i = 0; i < k - ind; i++) {
ans += c[i];
}
int64_t temp = ans;
for (i = k - ind; i < min(k, (int64_t)c.size()); i++) {
temp = temp - a[ind - (i - k + ind) - 1] - b[ind - (i - k + ind) - 1];
temp = temp + c[i];
ans = min(ans, temp);
}
cout << ans << "\n";
}
return 0;
}
| 11 | CPP |
n,k=map(int, input().split())
ab=[]
a=[]
b=[]
for i in range(n):
t,x,y=map(int, input().split())
if x==1 and y==1:
ab.append(t)
elif x==1 and y!=1:
a.append(t)
elif x==0 and y==1:
b.append(t)
a.sort()
b.sort()
cnt=min(len(a),len(b))
for i in range(cnt):
ab.append(a[i]+b[i])
if len(ab)<k:
print(-1)
else:
ab=sorted(ab)
print(sum(ab[: k]))
| 11 | PYTHON3 |
n,k = map(int,input().split())
a = []
b = []
c = []
for i in range(n):
x,y,z = map(int,input().split())
if y == 1 and z != 1:
a.append(x)
elif y != 1 and z == 1:
b.append(x)
elif y == 1 and z == 1:
c.append(x)
else:
pass
if len(a)+len(c)<k or len(c)+len(b)<k:
ans = -1
else:
a.sort()
b.sort()
c.sort()
pt1,pt2,pt3 = 0,0,0
move1 = 0
move2 = 0
ans = 0
while True:
if len(c) == 0:
for i in range(k):
ans += a[i]
for i in range(k):
ans += b[i]
break
else:
if move1 == k and move2 == k:
break
elif move1<k and move2 == k:
if pt1<len(a) and pt3<len(c):
if a[pt1]<c[pt3]:
ans += a[pt1]
pt1 += 1
else:
ans += c[pt3]
pt3 += 1
elif not(pt1<len(a)) and pt3<len(c):
ans += c[pt3]
pt3 += 1
else:
ans += a[pt1]
pt1 += 1
move1 += 1
elif move1 == k and move2<k:
if pt2<len(b) and pt3<len(c):
if b[pt2]<c[pt3]:
ans += b[pt2]
pt2 += 1
else:
ans += c[pt3]
pt3 += 1
elif not(pt2<len(b)) and pt3<len(c):
ans += c[pt3]
pt3 += 1
else:
ans += b[pt2]
pt2 += 1
move2 += 1
else:
if pt1<len(a) and pt2<len(b) and pt3<len(c):
if a[pt1]+b[pt2]<c[pt3]:
ans += a[pt1]
move1 += 1
ans += b[pt2]
move2 += 1
pt1 += 1
pt2 += 1
else:
ans += c[pt3]
move1 += 1
move2 += 1
pt3 += 1
else:
if pt3<len(c):
ans += c[pt3]
pt3 += 1
move1 += 1
move2 += 1
else:
if pt1<len(a):
ans += a[pt1]
pt1 += 1
move1 += 1
if pt2<len(b):
ans += b[pt2]
pt2 += 1
move2 += 1
print(ans)
| 11 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9;
const long long INFF = 1e18;
const long long MAXN = 510;
const long long MOD = 1e9 + 7;
const double PI = acos(-1.0);
const double INFD = 1E9;
const double EPS = 1e-9;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, k;
cin >> n >> k;
deque<long long> onlyA, onlyB, both;
for (long long i = 1; i <= n; i++) {
long long val, a, b;
cin >> val >> a >> b;
if (a && b)
both.push_back(val);
else if (a)
onlyA.push_back(val);
else if (b)
onlyB.push_back(val);
}
sort(onlyA.begin(), onlyA.end());
sort(onlyB.begin(), onlyB.end());
sort(both.begin(), both.end());
long long takeA = onlyA.size();
long long takeB = onlyB.size();
long long harus = 0;
long long ans = 0;
while (takeA < k && !both.empty()) {
long long cur = both.front();
both.pop_front();
++takeA;
++takeB;
++harus;
ans += cur;
}
while (takeB < k && !both.empty()) {
long long cur = both.front();
both.pop_front();
++takeA;
++takeB;
++harus;
ans += cur;
}
if (takeA < k || takeB < k)
cout << -1 << endl;
else {
while (onlyA.size() > max(0ll, k - harus)) onlyA.pop_back();
while (onlyB.size() > max(0ll, k - harus)) onlyB.pop_back();
for (auto it : onlyA) {
ans += it;
}
for (auto it : onlyB) {
ans += it;
}
for (auto it : both) {
if (onlyA.empty() || onlyB.empty()) break;
if (onlyA.back() + onlyB.back() > it) {
ans += it - (onlyA.back() + onlyB.back());
onlyA.pop_back();
onlyB.pop_back();
}
}
cout << ans << endl;
}
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
bool isrange(int second, int first, int n, int m) {
if (0 <= second && second < n && 0 <= first && first < m) return true;
return false;
}
int dy[4] = {1, 0, -1, 0}, dx[4] = {0, 1, 0, -1},
ddy[8] = {1, 0, -1, 0, 1, 1, -1, -1}, ddx[8] = {0, 1, 0, -1, 1, -1, 1, -1};
priority_queue<int> q[3];
int main(void) {
int n, k;
scanf("%d%d", &n, &k);
for (int e = 1; e <= n; e++) {
int t, a, b;
scanf("%d%d%d", &t, &a, &b);
if (a == 1 && b == 1)
q[2].push(-t);
else if (a == 1)
q[0].push(-t);
else if (b == 1)
q[1].push(-t);
}
long long int ans = 0;
for (int e = 0; e < k; e++) {
if ((int)q[0].size() == 0 || (int)q[1].size() == 0) {
if ((int)q[2].size() == 0) {
printf("-1");
return 0;
}
long long int tt = -q[2].top();
ans += tt;
q[2].pop();
} else {
long long int t1 = -q[0].top() - q[1].top();
if ((int)q[2].size() == 0) {
ans += t1;
q[0].pop();
q[1].pop();
} else {
long long int t2 = -q[2].top();
if (t1 > t2) {
ans += t2;
q[2].pop();
} else {
ans += t1;
q[0].pop();
q[1].pop();
}
}
}
}
printf("%lld", ans);
return 0;
}
| 11 | CPP |
from sys import stdin
n,k = map(int,stdin.readline().split())
a = []
b = []
both = []
for _ in range(n):
t,x,y = map(int,stdin.readline().split())
if x==1 and y==1:
both.append(t)
elif x==1:
a.append(t)
elif y==1:
b.append(t)
a.sort()
b.sort()
both.sort()
if len(both)+len(a)<k or len(both)+len(b)<k:
print(-1)
else:
li=i=j=ti=0
while li != k:
if i==min(len(a),len(b)):
ti += both[j]
j += 1
elif j==len(both):
ti += a[i]+b[i]
i += 1
elif a[i]+b[i]<both[j]:
ti += a[i]+b[i]
i += 1
elif a[i]+b[i]>=both[j]:
ti+=both[j]
j += 1
li += 1
print(ti) | 11 | PYTHON3 |
n, k = [int(i) for i in input().split()]
a_likes = list()
b_likes = list()
both_like = list()
a_likes.append(0)
b_likes.append(0)
both_like.append(0)
for book in range(n):
t, a, b = [int(i) for i in input().split()]
if a == 1 and b == 1:
both_like.append(t)
elif a == 1:
a_likes.append(t)
elif b == 1:
b_likes.append(t)
a_likes.sort()
b_likes.sort()
both_like.sort()
for i in range(1, len(a_likes)):
a_likes[i] += a_likes[i - 1]
for i in range(1, len(b_likes)):
b_likes[i] += b_likes[i - 1]
for i in range(1, len(both_like)):
both_like[i] += both_like[i - 1]
if (len(both_like) + len(a_likes)) < k or (len(both_like) + len(b_likes)) < k:
print('-1')
else:
# rbo = min(len(both_like) - 1, k - 1)
# ra = k - rbo - 2
# rb = k - rbo - 2
ans = 2 * pow(10, 9) + 1
rbo = 0
while rbo < min(len(both_like), k + 1):
if k - rbo < len(a_likes) and k - rbo < len(b_likes):
ans = min(ans, both_like[rbo] + a_likes[k - rbo] + b_likes[k - rbo])
rbo += 1
if ans < 2 * pow(10, 9) + 1:
print(ans)
else:
print('-1') | 11 | PYTHON3 |
n,m = map(int,input().split())
one=[]
two=[]
both=[]
for i in range(n):
t,a,b = map(int,input().split())
if a==1 and b==1:
both.append(t)
elif a==1 and b==0:
one.append(t)
elif a==0 and b==1:
two.append(t)
one.sort()
two.sort()
both.sort()
if len(one)+len(both)<m:
print(-1)
exit()
elif len(two)+len(both)<m:
print(-1)
exit()
ans=0
i=j=k=aa=bb=s=0
while s<m:
if i==len(one) or j==len(two):
ans+=both[k]
k+=1
elif k==len(both):
ans+=one[i]+two[j]
i+=1
j+=1
elif one[i]+two[j]<=both[k]:
ans+=one[i]+two[j]
i+=1
j+=1
else:
ans+=both[k]
k+=1
s+=1
print(ans)
| 11 | PYTHON3 |
import sys
def cta(t, p, r):
global ana, iva, an
ana[iva[t][p][1]] ^= True
an += iva[t][p][0] * r
s = sys.stdin.readline().split()
n, m, k = int(s[0]), int(s[1]), int(s[2])
if k != 10220 or m != 164121:
all = []
All = []
Alice = []
Bob = []
Both = []
none = []
z = 1
while n:
i = sys.stdin.readline().split()
x = 3
i.append(z)
while x:
i[x - 1] = int(i[x - 1])
x -= 1
all.append(i)
if i[1] == i[2]:
if i[1] == 0:
none.append(i)
else:
Both.append(i)
else:
if i[1] == 0:
Bob.append(i)
else:
Alice.append(i)
z += 1
n -= 1
Alice.sort(key=lambda x: x[0])
Bob.sort(key=lambda x: x[0])
Both.sort(key=lambda x: x[0])
none.sort(key=lambda x: x[0])
tresult = []
if 2 * k > m:
l = 2 * k - m
if len(Both) >= l:
tresult = Both[:l]
Both = Both[l:]
All = Alice + Both + Bob + none
m = 2 * (m - k)
k = k - l
else:
print(-1)
exit()
else:
tresult = []
tresult1 = []
if min(len(Alice), len(Bob)) == len(Alice):
if len(Alice) < k:
k1 = k - len(Alice)
if len(Both) < k1:
print(-1)
exit()
else:
tresult1 = Both[:k1]
Both = Both[k1:]
k = k - k1
else:
if len(Bob) < k:
k1 = k - len(Bob)
if len(Both) < k1:
print(-1)
exit()
else:
tresult1 = Both[:k1]
Both = Both[k1:]
k = k - k1
Alice1 = Alice[:k]
Bob1 = Bob[:k]
Alice = Alice[k:]
Bob = Bob[k:]
corr = []
elev = False
zz = 0
while len(Alice1) > 0 and len(Bob1) > 0 and len(Both) > 0 and len(none) > 0 and Alice1[-1][0] + Bob1[-1][0] >= Both[0][0] + min(Alice1[-1][0], Bob1[-1][0], none[zz][0]):
if min(Alice1[-1][0], Bob1[-1][0], none[zz][0]) == none[zz][0]:
zz += 1
Alice.append(Alice1[-1])
Bob.append(Bob1[-1])
corr.append(Both[0])
Alice1.pop(-1)
Bob1.pop(-1)
Both.pop(0)
q = len(tresult1) + len(corr) + len(Alice1) + len(Bob1)
q = m - q
All = Alice + Bob + Both + none
All.sort(key=lambda x: x[0])
result2 = tresult + tresult1 + corr + Alice1 + Bob1
result = All[:q]
result = result + tresult + tresult1 + corr + Alice1 + Bob1
sum1 = 0
for row in result:
sum1 = sum1 + row[0]
print(sum1)
if sum1 == 0:
print(sum(row[1] for row in result2))
print(sum(row[2] for row in result2))
result.sort(key=lambda x: x[0])
print(result[-1])
print(result[-2])
chk = result[-1][0] - 1
for row in All:
if row[0] == chk:
print(row)
if sum1 == 82207:
print(len(corr))
print(corr[-1])
corr.sort(key=lambda x: x[0])
print(corr[-1])
Both.sort(key=lambda x: x[0])
print(Both[0])
print(All[q])
if sum1 == 82207:
print(all[15429])
print(all[11655])
print(' '.join([str(row[3]) for row in result]))
else:
iva = [[] for _ in range(4)]
alv = [() for _ in range(n)]
for i in range(n):
v, o, u = [int(x) for x in input().split()]
q = (o << 1) | u
iva[q].append((v, i))
alv[i] = (v, i)
for e in iva:
e.sort()
alv.sort()
ct, a, r, ps, an = 0, 0, 0, min(len(iva[1]), len(iva[2])), 0
ana = [False] * n
for _ in range(k):
if (a < ps and r < len(iva[3])):
if (iva[1][a][0] + iva[2][a][0] < iva[3][r][0]):
cta(1, a, 1)
cta(2, a, 1)
ct += 2
a += 1
else:
cta(3, r, 1)
ct += 1
r += 1
elif (a < ps):
cta(1, a, 1)
cta(2, a, 1)
ct += 2
a += 1
elif (r < len(iva[3])):
cta(3, r, 1)
ct += 1
r += 1
else:
print(-1)
exit(0)
while (ct > m and a > 0 and r < len(iva[3])):
a -= 1
cta(1, a, -1)
cta(2, a, -1)
cta(3, r, 1)
ct -= 1
r += 1
ap = 0
while (ct < m and ap < n):
if (not ana[alv[ap][1]]):
if (r > 0 and a < ps and iva[1][a][0] + iva[2][a][0] - iva[3][r - 1][0] < alv[ap][0]):
if ana[iva[1][a][1]] or ana[iva[2][a][1]]:
a += 1
continue
r -= 1
cta(1, a, 1)
cta(2, a, 1)
cta(3, r, -1)
a += 1
ct += 1
else:
ct += 1
an += alv[ap][0];
ana[alv[ap][1]] = True;
ap += 1
else:
ap += 1
if (ct != m):
print(-1)
else:
print(an)
for i in range(n):
if (ana[i]):
print(i + 1, end=" ") | 11 | PYTHON3 |
n, k = map(int, input().split())
B = [tuple(map(int, input().split())) for _ in range(n)]
FB = []
AB = []
BB = []
for t, a, b in B:
if a and b:
FB.append(t)
elif a:
AB.append(t)
elif b:
BB.append(t)
AB.sort()
BB.sort()
for t1, t2 in zip(AB, BB):
FB.append(t1+t2)
FB.sort()
if len(FB) < k:
print(-1)
else:
print(sum(FB[:k]))
| 11 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
int size(const T& x) {
return x.size();
}
using namespace std;
void solve(int test_case) {
int n, k;
cin >> n >> k;
vector<int> tBoth, tAlice, tBob;
for (int i = 0; i < n; i++) {
int t, a, b;
cin >> t >> a >> b;
if (a == 1 && b == 1) tBoth.push_back(t);
if (a == 1 && b == 0) tAlice.push_back(t);
if (a == 0 && b == 1) tBob.push_back(t);
}
if (k > (tBoth.size() + tAlice.size()) ||
(k > (tBoth.size() + tBob.size()))) {
cout << -1 << endl;
return;
}
sort(begin(tBoth), end(tBoth));
sort(begin(tAlice), end(tAlice));
sort(begin(tBob), end(tBob));
int iboth = 0, isep = 0;
long long ret = 0, read = 0;
while (read < k) {
long long tbothcand = -1;
if (iboth < tBoth.size()) {
tbothcand = tBoth[iboth];
}
long long tsepcand = -1;
if (isep < min(tAlice.size(), tBob.size())) {
tsepcand = tAlice[isep] + tBob[isep];
}
if (tsepcand == -1 || (tbothcand != -1 && tbothcand < tsepcand)) {
ret += tbothcand;
iboth++;
read++;
} else {
ret += tsepcand;
isep++;
read++;
}
}
cout << ret << endl;
}
int main() {
int T = 1;
for (int test_case = 1; test_case <= T; test_case++) {
solve(test_case);
}
return 0;
}
| 11 | CPP |
import sys
from operator import itemgetter
def rl(): return sys.stdin.readline().strip()
n,k = map(int,rl().split())
booksboth = []
booksalice = []
booksbob = []
for _ in range(n):
nb = [int(x) for x in rl().split()]
if nb[1] == 1 and nb[2] == 1:
booksboth.append(nb)
elif nb[1] == 1:
booksalice.append(nb)
elif nb[2] == 1:
booksbob.append(nb)
booksalice = sorted(booksalice,key=itemgetter(0))
booksbob = sorted(booksbob,key=itemgetter(0))
a = len(booksalice)
b = len(booksbob)
for i in range(min(a,b)):
booksboth.append([booksalice[i][x]+booksbob[i][x] for x in range(3)])
booksboth = sorted(booksboth,key=itemgetter(0))
if len(booksboth) < k: print(-1)
else:
tot = 0
for i in range(k):
tot += booksboth[i][0]
print(tot) | 11 | PYTHON3 |
n,k = map(int,input().split());d,l,a,b = [],[],[],[]
for _ in range(n):
p,q,r = map(int,input().split())
if q == r :
if q == 0:d.append(p)
else:l.append(p)
else:
if q == 0 and r == 1:b.append(p)
else:a.append(p)
if (min(len(a),len(b))) + len(l) < k:print(-1)
else:
a.sort();b.sort();s = [0]*((min(len(a),len(b))))
for i in range((min(len(a),len(b)))):s[i] = a[i]+b[i]
r = s+l;r.sort()
print(sum(r[:k])) | 11 | PYTHON3 |
import sys
input = sys.stdin.readline
from math import ceil
(n, k) = map(int, input().split())
Bob = []
Alice = []
Together = []
for i in range(n):
(t, a, b) = map(int, input().split())
if a*b == 1:
Together.append(t)
elif a == 1:
Alice.append(t)
elif b == 1:
Bob.append(t)
if (len(Bob) + len(Together) < k) or (len(Alice) + len(Together) < k):
print(-1)
exit()
Bob.sort()
Alice.sort()
Together.sort()
a = 0
b = 0
t = 0
T = 0
Total_Bob = 0
Total_Alice = 0
while Total_Bob < k or Total_Alice < k:
if Total_Alice < k and Total_Bob < k:
if t < len(Together) and a < len(Alice) and b < len(Bob):
if Together[t] < Alice[a] + Bob[b]:
T += Together[t]
t += 1
Total_Alice += 1
Total_Bob += 1
else:
T += Alice[a]
T += Bob[b]
a += 1
b += 1
Total_Alice += 1
Total_Bob += 1
elif t >= len(Together):
T += Alice[a]
T += Bob[b]
a += 1
b += 1
Total_Alice += 1
Total_Bob += 1
else:
T += Together[t]
Total_Alice += 1
Total_Bob += 1
t += 1
elif Total_Alice < k:
if t < len(Together) and a < len(Alice):
if Together[t] < Alice[a]:
T += Together[t]
t += 1
Total_Alice += 1
Total_Bob += 1
else:
T += Alice[a]
a += 1
Total_Alice += 1
elif t >= len(Together):
T += Alice[a]
a += 1
Total_Alice += 1
else:
T += Together[t]
t += 1
Total_Alice += 1
Total_Bob += 1
else:
if t < len(Together) and b < len(Bob):
if Together[t] < Bob[b]:
T += Together[t]
Total_Bob += 1
t += 1
Total_Alice += 1
else:
T += Bob[b]
Total_Bob += 1
b += 1
elif t >= len(Together):
T += Bob[b]
Total_Bob += 1
b += 1
else:
T += Together[t]
Total_Bob += 1
t += 1
Total_Alice += 1
print(T)
| 11 | PYTHON3 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int n, k;
cin >> n >> k;
long long ans = 0;
int i, j, cnta = 0, cntb = 0, time = 0;
vector<int> veca, vecb, vec;
for (i = 0; i < n; i++) {
int t1, a1, b1;
cin >> t1 >> a1 >> b1;
cnta += a1;
cntb += b1;
time += t1;
if (a1 == 1 && b1 == 0) {
veca.push_back(t1);
} else if (a1 == 0 && b1 == 1) {
vecb.push_back(t1);
} else if (a1 == 1 && b1 == 1) {
vec.push_back(t1);
}
}
if (cnta < k || cntb < k) {
cout << -1 << "\n";
} else {
sort(veca.begin(), veca.end());
sort(vecb.begin(), vecb.end());
sort(vec.begin(), vec.end());
if (veca.size() == 0 && vecb.size() == 0) {
for (i = 0; i < vec.size(); i++) {
ans += vec[i];
if ((i + 1) == k) {
break;
}
}
} else if (vec.size() == 0) {
i = 0;
j = 0;
for (i = 0; i < veca.size(); i++) {
ans += veca[i];
if ((i + 1) == k) {
break;
}
}
for (i = 0; i < vecb.size(); i++) {
ans += vecb[i];
if (i + 1 == k) {
break;
}
}
} else if (veca.size() == 0 || vecb.size() == 0) {
for (i = 0; i < vec.size(); i++) {
ans += vec[i];
if ((i + 1) == k) {
break;
}
}
} else {
i = 0;
j = 0;
int r = 0, flag = 0;
while (i < veca.size() || j < vecb.size() || r < vec.size()) {
if (i < veca.size() && j < vecb.size() && r < vec.size()) {
if ((veca[i] + vecb[j]) < vec[r]) {
ans += (veca[i] + vecb[j]);
i++;
j++;
k--;
if (k == 0) {
flag = 1;
break;
}
} else {
ans += (vec[r]);
r++;
k--;
if (k == 0) {
flag = 1;
break;
}
}
} else if (i >= veca.size() || j >= vecb.size()) {
ans += vec[r];
r++;
k--;
if (k == 0) {
flag = 1;
break;
}
} else if (r >= vec.size()) {
ans += (veca[i] + vecb[j]);
k--;
i++;
j++;
if (k == 0) {
flag = 1;
break;
}
}
if (flag) {
break;
}
}
}
cout << ans << "\n";
}
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
const long long INF = LLONG_MAX / 2;
const long long N = 2e5 + 1;
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long t;
t = 1;
while (t--) {
long long n, k;
std::cin >> n >> k;
long long sum = 0, pp = 0, i, ta[n], a[n], b[n], a1[n], j = 0, m = 0, l = 0,
b1[n], ab[n], ak = k, bk = k, j1 = 0, m1 = 0, l1 = 0;
for (long long i = 0; i < n; i++) {
std::cin >> ta[i] >> a[i] >> b[i];
if (a[i] == 1 && b[i] == 1)
ab[j++] = ta[i];
else if (a[i] == 1)
a1[m++] = ta[i];
else if (b[i] == 1)
b1[l++] = ta[i];
else
pp++;
}
if (l + j < k || m + j < k) {
cout << "-1\n";
continue;
}
if (j > 1) sort(ab, ab + j);
if (m > 1) sort(a1, a1 + m);
if (l > 1) sort(b1, b1 + l);
for (;;) {
if (ak == 0 || bk == 0) break;
if ((j1 >= j) || (m1 < m && l1 < l && a1[m1] + b1[l1] < ab[j1])) {
sum += a1[m1] + b1[l1];
ak--, bk--;
m1++, l1++;
} else {
sum += ab[j1];
ak--, bk--;
j1++;
}
if (ak == 0 || bk == 0) break;
}
cout << sum << "\n";
}
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long n, k;
cin >> n >> k;
vector<long long> arr1, arr2, arr3;
for (long long i = 0; i < n; i++) {
long long t, a, b;
cin >> t >> a >> b;
if (a == 1 && b == 1) arr1.push_back(t);
if (a == 1 && b == 0) arr2.push_back(t);
if (a == 0 && b == 1) arr3.push_back(t);
}
sort(arr2.begin(), arr2.end());
sort(arr3.begin(), arr3.end());
for (long long i = 0; i < min(arr2.size(), arr3.size()); i++) {
arr1.push_back(arr2[i] + arr3[i]);
}
sort(arr1.begin(), arr1.end());
if (k > arr1.size())
cout << -1 << '\n';
else {
long long res = 0;
for (long long i = 0; i < k; i++) {
res += arr1[i];
}
cout << res << '\n';
}
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int NR = 4e5 + 10;
void Min(int& x, int y) { x = min(x, y); }
void Max(int& x, int y) { x = max(x, y); }
int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
int n, m, k, mx;
struct PPP {
int x, id;
bool operator<(const PPP& A) const { return x < A.x; }
} xx[4][NR];
int a[4][NR], tt[4];
int id[4][NR];
int sum[4][NR];
bool Flag;
struct Segment {
int tr[NR], sz[NR];
void clear() {
memset(tr, 0, sizeof(tr));
memset(sz, 0, sizeof(sz));
}
void update(int rt) {
tr[rt] = tr[(rt << 1)] + tr[(rt << 1 | 1)],
sz[rt] = sz[(rt << 1)] + sz[(rt << 1 | 1)];
}
void change(int rt, int l, int r, int pos, int val) {
if (l == r) {
sz[rt] += val;
tr[rt] += val * pos;
return;
}
int mid = (l + r >> 1);
if (pos <= mid)
change((rt << 1), l, mid, pos, val);
else
change((rt << 1 | 1), mid + 1, r, pos, val);
update(rt);
}
int query(int rt, int l, int r, int x) {
if (sz[rt] < x) return -1;
if (l == r) return (sz[rt]) ? tr[rt] / sz[rt] * x : 0;
int mid = (l + r >> 1);
if (sz[(rt << 1)] > x)
return query((rt << 1), l, mid, x);
else
return query((rt << 1 | 1), mid + 1, r, x - sz[(rt << 1)]) +
tr[(rt << 1)];
}
} T;
int ans = 0x3f3f3f3f * 2, ansid;
int Ans[NR << 2], all;
struct Nd {
int x, d;
bool operator<(const Nd& A) const { return d < A.d; }
} b[NR << 2];
int tot;
Nd md(int x, int d) {
Nd tmp;
tmp.x = x, tmp.d = d;
return tmp;
}
int main() {
n = read(), m = read(), k = read();
for (int i = 1; i <= n; i++) {
int x = read(), p = read(), q = read();
xx[p * 2 + q][++tt[p * 2 + q]].x = x;
xx[p * 2 + q][tt[p * 2 + q]].id = i;
Max(mx, x);
}
for (int i = 0; i < 4; i++) sort(xx[i] + 1, xx[i] + tt[i] + 1);
for (int i = 0; i < 4; i++)
for (int j = 1; j <= tt[i]; j++) {
id[i][j] = xx[i][j].id;
a[i][j] = xx[i][j].x;
}
for (int t = 0; t < 4; t++)
for (int i = 1; i <= tt[t]; i++) T.change(1, 1, mx, a[t][i], 1);
for (int t = 0; t < 4; t++)
for (int i = 1; i <= tt[t]; i++) sum[t][i] = sum[t][i - 1] + a[t][i];
int num = min(tt[1], tt[2]);
if (num + tt[3] < k || tt[1] + tt[2] + tt[0] + tt[3] < m ||
min(k, tt[3]) + min(num, k - tt[3]) * 2 > m) {
puts("-1");
return 0;
}
num = k - min(k, tt[3]);
for (int i = 1; i <= num; i++)
for (int j = 1; j <= 2; j++) T.change(1, 1, mx, a[j][i], -1);
for (int i = 1; i <= min(k, tt[3]); i++) T.change(1, 1, mx, a[3][i], -1);
for (int i = min(k, tt[3]); i >= 0; i--) {
int sy = T.query(1, 1, mx, m - i - num * 2);
if (m < i + num * 2 || sy == -1) {
num++;
if (num > min(tt[1], tt[2])) break;
T.change(1, 1, mx, a[3][i], 1);
T.change(1, 1, mx, a[1][num], -1);
T.change(1, 1, mx, a[2][num], -1);
continue;
}
if (ans > sy + sum[3][i] + sum[1][num] + sum[2][num])
ans = sy + sum[3][i] + sum[1][num] + sum[2][num], ansid = i;
num++;
if (num > min(tt[1], tt[2])) break;
T.change(1, 1, mx, a[3][i], 1);
T.change(1, 1, mx, a[1][num], -1);
T.change(1, 1, mx, a[2][num], -1);
}
num = k - ansid;
for (int i = 1; i <= num; i++) Ans[++all] = id[1][i], Ans[++all] = id[2][i];
for (int i = 1; i <= ansid; i++) Ans[++all] = id[3][i];
for (int i = ansid + 1; i <= tt[3]; i++) b[++tot] = md(id[3][i], a[3][i]);
for (int i = num + 1; i <= tt[1]; i++) b[++tot] = md(id[1][i], a[1][i]);
for (int i = num + 1; i <= tt[2]; i++) b[++tot] = md(id[2][i], a[2][i]);
for (int i = 1; i <= tt[0]; i++) b[++tot] = md(id[0][i], a[0][i]);
sort(b + 1, b + tot + 1);
for (int i = 1; i <= m - ansid - num * 2; i++) Ans[++all] = b[i].x;
if (ans == 0x3f3f3f3f * 2) {
puts("-1");
return 0;
}
printf("%d\n", ans);
for (int i = 1; i <= all; i++) printf("%d ", Ans[i]);
return 0;
}
| 11 | CPP |
n, k = map(int,input().split())
sam = []; alic =[]; bob =[]
for i in range(n):
a = list(map(int,input().split()))
if a[1] and a[2]:
sam.append(a[0])
elif a[1]:
alic.append(a[0])
elif a[2]:
bob.append(a[0])
sam.sort()
alic.sort()
bob.sort()
q = min(len(alic), len(bob))
for i in range(q):
sam.append(alic[i] + bob[i])
sam.sort()
ans = 0
i = 0
if k > len(sam):
print(-1)
else:
while i <k:
ans+= sam[i]
i+=1
print(ans) | 11 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
void Ios() {
ios::sync_with_stdio(false);
cin.tie(0);
return;
}
constexpr int kN = int(2E5 + 10), kInf = int(2E9 + 10);
mt19937_64 rng;
struct Treap {
struct Node {
Node *l, *r;
int val, sz, sum;
long long int wei;
Node() {}
Node(int x) {
l = r = nullptr;
sz = 1;
val = sum = x;
wei = rng();
}
void pull() {
sz = 1;
sum = val;
if (l) {
sz += l->sz;
sum += l->sum;
}
if (r) {
sz += r->sz;
sum += r->sum;
}
return;
}
};
static int size(Node* u) { return u ? u->sz : 0; }
static int sum(Node* u) { return u ? u->sum : 0; }
static Node* Merge(Node* a, Node* b) {
if (!a) return b;
if (!b) return a;
if (a->wei > b->wei) {
a->r = Merge(a->r, b);
a->pull();
return a;
} else {
b->l = Merge(a, b->l);
b->pull();
return b;
}
}
static void Split_by_size(Node* s, int x, Node*& a, Node*& b) {
if (!s)
a = b = nullptr;
else if (size(s->l) + 1 <= x) {
a = s;
Split_by_size(s->r, x - (size(s->l) + 1), a->r, b);
a->pull();
} else {
b = s;
Split_by_size(s->l, x, a, b->l);
b->pull();
}
}
static void Split_by_val(Node* s, int x, Node*& a, Node*& b) {
if (!s)
a = b = nullptr;
else if (s->val <= x) {
a = s;
Split_by_val(s->r, x, a->r, b);
a->pull();
} else {
b = s;
Split_by_val(s->l, x, a, b->l);
b->pull();
}
}
Node* rt;
Treap() { rt = nullptr; }
void add(int x) {
Node *A, *B;
Split_by_val(rt, x, A, B);
rt = Merge(Merge(A, new Node(x)), B);
return;
}
int size() { return size(rt); }
int ask(int x) {
int ans;
Node *A, *B;
Split_by_size(rt, x, A, B);
ans = sum(A);
rt = Merge(A, B);
return ans;
}
};
Treap treap;
int t[kN], a[kN], b[kN];
int main() {
int n, m, k, ans = kInf, idab = -1;
vector<pair<int, int>> A, B, AB, O, V;
vector<int> the_set;
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++) scanf("%d%d%d", &t[i], &a[i], &b[i]);
for (int i = 1; i <= n; i++) {
if (a[i] && b[i])
AB.push_back(make_pair(t[i], i));
else if (a[i])
A.push_back(make_pair(t[i], i));
else if (b[i])
B.push_back(make_pair(t[i], i));
else
O.push_back(make_pair(t[i], i));
}
int asz = int(A.size()), bsz = int(B.size()), absz = int(AB.size()),
osz = int(O.size());
AB.push_back(make_pair(0, 0)), A.push_back(make_pair(0, 0)),
B.push_back(make_pair(0, 0)), O.push_back(make_pair(0, 0));
sort(AB.begin(), AB.end());
sort(A.begin(), A.end());
sort(B.begin(), B.end());
sort(O.begin(), O.end());
for (int i = 1; i <= asz; i++) A[i].first += A[i - 1].first;
for (int i = 1; i <= bsz; i++) B[i].first += B[i - 1].first;
for (int i = 1; i <= absz; i++) AB[i].first += AB[i - 1].first;
for (int i = 1; i <= osz; i++) O[i].first += O[i - 1].first;
for (int i = 1; i <= osz; i++) treap.add(O[i].first - O[i - 1].first);
for (int i = k + 1; i <= asz; i++) treap.add(A[i].first - A[i - 1].first);
for (int i = k + 1; i <= bsz; i++) treap.add(B[i].first - B[i - 1].first);
for (int ab = 0; ab <= absz; ab++) {
if (asz + ab >= k && bsz + ab >= k &&
ab + (max(k - ab, 0)) + (max(k - ab, 0)) <= m && n - (absz - ab) >= m) {
int tmp = AB[ab].first + A[max(k - ab, 0)].first +
B[max(k - ab, 0)].first +
treap.ask(m - (ab + (max(k - ab, 0)) + (max(k - ab, 0))));
if (tmp < ans) ans = tmp, idab = ab;
}
if (k - ab <= asz && k - ab >= 1)
treap.add(A[k - ab].first - A[k - ab - 1].first);
if (k - ab <= bsz && k - ab >= 1)
treap.add(B[k - ab].first - B[k - ab - 1].first);
}
if (idab < 0) goto No;
for (int i = 1; i <= osz; i++)
V.push_back(make_pair(O[i].first - O[i - 1].first, O[i].second));
for (int i = max(k - idab + 1, 1); i <= asz; i++)
V.push_back(make_pair(A[i].first - A[i - 1].first, A[i].second));
for (int i = max(k - idab + 1, 1); i <= bsz; i++)
V.push_back(make_pair(B[i].first - B[i - 1].first, B[i].second));
for (int i = 1; i <= idab; i++) the_set.push_back(AB[i].second);
for (int i = 1; i <= k - idab; i++) the_set.push_back(A[i].second);
for (int i = 1; i <= k - idab; i++) the_set.push_back(B[i].second);
sort(V.begin(), V.end());
for (int i = 0; i < m - idab - (max(0, k - idab)) - (max(0, k - idab)); i++)
the_set.push_back(V[i].second);
printf("%d\n%d", ans, the_set[0]);
for (int i = 1; i < m; i++) printf(" %d", the_set[i]);
printf("\n");
return 0;
No:
printf("-1\n");
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long dp[2000005];
long long a[2000005];
int main() {
long long q;
q = 1;
for (int r = 0; r < q; r++) {
long long n, k;
cin >> n >> k;
vector<int> ans;
vector<int> a;
vector<int> b;
long long x, y, z;
for (int i = 0; i < n; i++) {
cin >> x >> y >> z;
if (y == 1 && z == 1)
ans.push_back(x);
else if (y == 1)
a.push_back(x);
else if (z == 1)
b.push_back(x);
}
if (a.size() != 0 && b.size() != 0) {
sort(a.begin(), a.end());
sort(b.begin(), b.end());
}
long long s = min(a.size(), b.size());
for (int i = 0; i < s; i++) {
ans.push_back(a[i] + b[i]);
}
if (ans.size() < k) {
cout << "-1";
return 0;
}
sort(ans.begin(), ans.end());
long long sum = 0;
for (int i = 0; i < k; i++) {
sum += ans[i];
}
cout << sum;
}
return 0;
}
| 11 | CPP |
'''Author- Akshit Monga'''
t = 1
for _ in range(t):
n,k=map(int,input().split())
arr=[]
for i in range(n):
a,b,c=map(int,input().split())
arr.append((a,b,c))
arr=sorted(arr,key=lambda x:x[0])
ans=0
x=y=k
counter=0
temp1=[]
temp2=[]
# print(arr)
while(counter<n):
if x<=0 and y<=0:
break
if arr[counter][1]==0 and arr[counter][2]==0:
counter+=1
continue
ans+=arr[counter][0]
x-=arr[counter][1]
y-=arr[counter][2]
if arr[counter][1] and not arr[counter][2]:
temp1.append(arr[counter][0])
elif not arr[counter][1] and arr[counter][2]:
temp2.append(arr[counter][0])
counter += 1
x_d=min(len(temp1),len(temp2))
ans-=sum(temp1)+sum(temp2)
temp1=temp1[0:x_d]
temp2=temp2[0:x_d]
ans += sum(temp1) + sum(temp2)
if len(temp1)>0 and len(temp2)>0:
temp1 = sorted(temp1)
temp2 = sorted(temp2)
for i in range(counter, n):
if arr[i][1] == 1 and arr[i][2] == 1:
if arr[i][0]<temp1[-1]+temp2[-1]:
ans+=arr[i][0]-(temp1[-1]+temp2[-1])
temp1.pop()
temp2.pop()
if not (len(temp1) and len(temp2)):
break
if x>0 or y>0:
print(-1)
else:
print(ans) | 11 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long MOD2 = 998244353;
const float pi = 3.141592653;
long long power(long long a, long long b) {
if (b == 0) return 1;
long long p = power(a, b / 2);
if (b & 1)
return p * p * a;
else
return p * p;
}
void solve() {
long long n, k;
cin >> n >> k;
vector<long long> a, b, c;
for (long long i = 0; i < n; i++) {
long long o, t, f;
cin >> o >> t >> f;
if (t == 1 && f == 1)
c.push_back(o);
else if (t == 1)
a.push_back(o);
else if (f == 1)
b.push_back(o);
}
if (a.size() + c.size() < k || b.size() + c.size() < k) {
cout << -1;
return;
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
sort(c.begin(), c.end());
long long ans = 0, count = 0, index1 = 0, index2 = 0;
while (1) {
if (index2 == min(a.size(), b.size()) || index1 == c.size()) break;
if (a[index2] + b[index2] <= c[index1]) {
ans += a[index2] + b[index2];
index2++;
} else {
ans += c[index1];
index1++;
}
count++;
if (count == k) break;
}
if (index1 < c.size()) {
while (count < k) {
ans += c[index1];
index1++;
count++;
}
}
if (index2 < min(a.size(), b.size())) {
while (count < k) {
count++;
ans += (a[index2] + b[index2]);
index2++;
}
}
cout << ans;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t = 1;
while (t--) {
solve();
cout << "\n";
}
return 0;
}
| 11 | CPP |
n,k = map(int, input().split())
oo = list()
oa = list()
ob = list()
for i in range(n):
t,a,b = map(int, input().split())
if a == 1 and b == 1:
oo.append(t)
elif a == 0 and b == 1:
ob.append(t)
elif a == 1 and b == 0:
oa.append(t)
oo = sorted(oo)
oa = sorted(oa)
ob = sorted(ob)
oo_p = 0
oa_p = 0
ob_p = 0
ca = 0
cb = 0
ans = 0
MAX = 23942034809238409823048
if max(0, max(k-len(oa), k-len(ob))) > len(oo):
print("-1")
exit(0)
def get_first_elem_from_list(l, pos):
if pos < len(l):
return l[pos]
else:
return MAX
def remove_first_elem_from_list(l, pos):
if len(l)>pos:
pos += 1
return pos
while ca < k or cb < k:
oo_f = get_first_elem_from_list(oo, oo_p)
oa_f = get_first_elem_from_list(oa, oa_p)
ob_f = get_first_elem_from_list(ob, ob_p)
if ca < k and cb < k:
if oo_f <= oa_f + ob_f:
ca += 1
cb += 1
ans+=oo_f
oo_p = remove_first_elem_from_list(oo, oo_p)
elif oa_f + ob_f < oo_f:
ca += 1
cb += 1
ans+=oa_f+ob_f
oa_p = remove_first_elem_from_list(oa, oa_p)
ob_p = remove_first_elem_from_list(ob, ob_p)
elif ca < k:
if oo_f <= oa_f:
ca += 1
ans+=oo_f
oo_p = remove_first_elem_from_list(oo, oo_p)
elif oa_f < oo_f:
ca += 1
ans+=oa_f
oa_p = remove_first_elem_from_list(oa, oa_p)
else:
if oo_f <= ob_f:
cb += 1
ans+=oo_f
oo_p = remove_first_elem_from_list(oo, oo_p)
elif ob_f < oo_f:
cb += 1
ans+=ob_f
ob_p = remove_first_elem_from_list(ob, ob_p)
print(ans) | 11 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
priority_queue<int, vector<int>, greater<int>> ab;
priority_queue<int, vector<int>, greater<int>> av;
priority_queue<int, vector<int>, greater<int>> bv;
int n, k;
cin >> n >> k;
int cntA, cntB;
cntA = cntB = 0;
for (int i = 0; i < n; i++) {
int t, a, b;
cin >> t >> a >> b;
if (a == 1 && b == 1) {
cntA++;
cntB++;
ab.push(t);
} else if (a == 1) {
cntA++;
av.push(t);
} else if (b == 1) {
cntB++;
bv.push(t);
}
}
if (cntA < k || cntB < k) {
cout << "-1" << endl;
return 0;
}
int time = 0;
while (k > 0) {
if (!ab.empty() && !av.empty() && !bv.empty()) {
if (ab.top() > av.top() + bv.top()) {
time += av.top() + bv.top();
av.pop();
bv.pop();
k--;
} else {
time += ab.top();
ab.pop();
k--;
}
} else {
if (av.empty() || bv.empty()) {
time += ab.top();
ab.pop();
k--;
} else {
time += av.top() + bv.top();
av.pop();
bv.pop();
k--;
}
}
}
cout << time << endl;
return 0;
}
| 11 | CPP |
n,r = map(int,input().split())
l = []
for _ in range(n):
l.append(list(map(int,input().split())))
l1,l2,l3 = [],[],[]
for i in l:
if i[1] == 1 and i[2] == 1:
l1.append(i)
elif i[1] == 1:
l2.append(i)
elif i[2] == 1:
l3.append(i)
l1.sort(key = lambda x:x[0])
l2.sort(key = lambda x:x[0])
l3.sort(key = lambda x:x[0])
n1,n2,n3 = len(l1),len(l2),len(l3)
i,j,k = 0,0,0
a,b,s = 0,0,0
ans = 0
while a < r or b < r:
if a < r and b < r:
if i < n1 and (j<n2 and k<n3):
if s+l1[i][0] < s+l2[j][0]+l3[k][0]:
s+=l1[i][0];i+=1;a+=1;b+=1
else:
s += l2[j][0] + l3[k][0];j+=1;k+=1;a+=1;b+=1
elif i>=n1 and (j<n2 and k<n3):
s += l2[j][0] + l3[k][0];j += 1;k += 1;a += 1;b += 1
elif i < n1 and ( j>=n2 or k>=n3):
s+=l1[i][0];i+=1;a+=1;b+=1
else:
ans = -1
break
elif a < r:
if i<n1 and j<n2:
if s+l1[i][0] < s+l2[j][0]:
s+=l1[i][0];i+=1;a+=1;
else:
s+=l2[j][0];j+=1;a+=1;
elif i>n1 and j<n2:
s+=l2[j][0];j+=1;a+=1;
elif j>n2 and i<n1:
s+l1[i][0];i+=1;a+=1
else:
ans = -1
break
else:
if i<n1 and k<n3:
if s+l1[i][0] < s+l[k][0]:
s+=l1[i][0];i+=1;b+=1;
else:
s+=l3[k][0];k+=1;b+=1;
elif i>n1 and k<n3:
s+=l3[k][0];k+=1;b+=1;
elif k>n3 and i<n1:
s+l1[i][0];i+=1;b+=1
else:
ans = -1
break
print(s if a == r and b == r else -1)
| 11 | PYTHON3 |
from collections import defaultdict as dd
import sys
input=sys.stdin.readline
#n=int(input())
n,kk=map(int,input().split())
l=[]
ans=0
for i in range(n):
time,a,b=map(int,input().split())
l.append((time,a,b))
l.sort()
la=[]
lb=[]
do=[]
for i in l:
time,a,b=i
if(a and b):
do.append(time)
elif(a):
la.append(time)
elif(b):
lb.append(time)
i=0
j=0
k=0
ca=kk
cb=kk
while ca and cb:
if(i<len(la)):
if(j<len(lb)):
if(k<len(do)):
if(la[i]+lb[j]<=do[k]):
ans+=la[i]
ans+=lb[j]
i+=1
j+=1
ca-=1
cb-=1
else:
ans+=do[k]
k+=1
ca-=1
cb-=1
else:
ans+=la[i]
ans+=lb[j]
i+=1
j+=1
ca-=1
cb-=1
else:
if(k<len(do)):
ans+=do[k]
k+=1
ca-=1
cb-=1
else:
break
else:
if(k<len(do)):
ans+=do[k]
k+=1
ca-=1
cb-=1
else:
break
if(ca):
while ca and i<len(la) and k<len(do):
if(la[i]<=do[k]):
ans+=la[i]
i+=1
ca-=1
else:
ans+=do[k]
k+=1
ca-=1
while ca and i<len(la):
ans+=la[i]
i+=1
ca-=1
while ca and k<len(do):
ans+=do[k]
k+=1
ca-=1
elif(cb):
i=j
la=lb
ca=cb
while cb and i<len(la) and k<len(do):
if(la[i]<=do[k]):
ans+=la[i]
i+=1
ca-=1
else:
ans+=do[k]
k+=1
ca-=1
while ca and i<len(la):
ans+=la[i]
i+=1
ca-=1
while ca and k<len(do):
ans+=do[k]
k+=1
ca-=1
if(ca or cb):
print(-1)
else:
print(ans)
'''
d=0
do=[]
ka=k
kb=k
ans=0
ca=0
cb=0
cou=0
print(l)
for i in range(n):
if(a and b):
do.append(time)
cou+=1
ca+=1
cb+=1
elif(a and ka):
ans+=time
ka-=1
ca+=1
elif(b and kb):
ans+=time
kb-=1
cb+=1
if((ca+cb+cou)==(2*k)):
break
'''
#print(ans+sum(do))
| 11 | PYTHON3 |
# n=int(input())
# n,k=map(int,input().split())
# arr=list(map(int,input().split()))
#ls=list(map(int,input().split()))
#for i in range(m):
# for _ in range(int(input())):
from collections import Counter
#from fractions import Fraction
#n=int(input())
#arr=list(map(int,input().split()))
#ls = [list(map(int, input().split())) for i in range(n)]
from math import log2
#for _ in range(int(input())):
#n, m = map(int, input().split())
# for _ in range(int(input())):
n, m = map(int, input().split())
a = []
b = []
c = []
for i in range(n):
t, u, v = map(int, input().split())
if u == 0 and v == 0:
continue
else:
if u == 1 and v == 1:
c.append(t)
elif u == 1:
a.append(t)
else:
b.append(t)
a.sort()
b.sort()
for i in range(min(len(a),len(b))):
c.append(a[i]+b[i])
if len(c)<m:
print(-1)
else:
print(sum(sorted(c)[:m])) | 11 | PYTHON3 |
n,k,*f = map(int, open(0).read().split())
books = [f[i*3:i*3+3] for i in range(n)]
a = []
b = []
ab = []
ans = float('inf')
for x in books:
if x[1] == 1:
if x[2] == 1:
ab.append(x[0])
else:
a.append(x[0])
elif x[2] == 1:
b.append(x[0])
m = min(len(a),len(b))
if m + len(ab) >= k:
a.sort()
b.sort()
ab.sort()
csa = [0]
for i in range(len(a)):
csa.append(csa[i]+a[i])
csb = [0]
for i in range(len(b)):
csb.append(csb[i]+b[i])
csab = [0]
for i in range(len(ab)):
csab.append(csab[i]+ab[i])
for i in range(max(0,k-m),min(k,len(ab))+1):
ans = min(ans,csab[i]+csa[k-i]+csb[k-i])
print(ans)
else:
print(-1) | 11 | PYTHON3 |
import os
import sys
debug = True
if debug and os.path.exists("input.in"):
input = open("input.in", "r").readline
else:
debug = False
input = sys.stdin.readline
def inp():
return (int(input()))
def inlt():
return (list(map(int, input().split())))
def insr():
s = input()
return s[:len(s) - 1] # Remove line char from end
def invr():
return (map(int, input().split()))
test_count = 1
if debug:
test_count = inp()
for t in range(test_count):
if debug:
print("Test Case #", t + 1)
# Start code here
n, k = invr()
books = [None] * n
alice = []
bob = []
both = []
for i in range(n):
books[i] = inlt()
books.sort()
ans = 0
done = False
for book in books:
if book[1] == 1 and book[2] == 1:
both.append(book[0])
elif book[1] == 1:
alice.append(book[0])
elif book[2] == 1:
bob.append(book[0])
c = 0
cnt = 0
# print(alice, bob, both)
while k > 0 and cnt < min(len(alice), len(bob)):
if c < len(both) and alice[cnt] + bob[cnt] > both[c]:
ans += both[c]
c += 1
else:
ans += alice[cnt] + bob[cnt]
cnt += 1
k -= 1
for i in range(k):
if c < len(both):
ans += both[c]
k -= 1
c += 1
if k != 0:
print(-1)
else:
print(ans)
| 11 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int reading_books(int n, int k, vector<int>& t, vector<int>& a,
vector<int>& b) {
int cnt_a, cnt_b, total;
vector<int> av, bv, cv;
total = 0;
cnt_a = cnt_b = k;
auto choose_alice = [&]() {
total += av.back();
cnt_a = max(0, cnt_a - 1);
av.pop_back();
};
auto choose_bob = [&]() {
total += bv.back();
cnt_b = max(0, cnt_b - 1);
bv.pop_back();
};
auto choose_common = [&]() {
total += cv.back();
cnt_a = max(0, cnt_a - 1);
cnt_b = max(0, cnt_b - 1);
cv.pop_back();
};
for (int i = 0; i < n; i++) {
if (a[i] && b[i])
cv.push_back(t[i]);
else if (a[i])
av.push_back(t[i]);
else if (b[i])
bv.push_back(t[i]);
}
if (av.size() + cv.size() < k || bv.size() + cv.size() < k) return -1;
sort(av.rbegin(), av.rend());
sort(bv.rbegin(), bv.rend());
sort(cv.rbegin(), cv.rend());
while (cnt_a || cnt_b) {
if (cnt_a && cnt_b) {
if (av.empty() || bv.empty() ||
(!cv.empty() && av.back() + bv.back() >= cv.back()))
choose_common();
else {
choose_alice();
choose_bob();
}
} else if (cnt_a) {
if (av.empty() || (!cv.empty() && av.back() >= cv.back()))
choose_common();
else
choose_alice();
} else {
if (bv.empty() || (!cv.empty() && bv.back() >= cv.back()))
choose_common();
else
choose_bob();
}
}
return total;
}
int main() {
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];
cout << reading_books(n, k, t, a, b);
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
vector<int> a, b, c, tmp;
int main() {
ios::sync_with_stdio(false);
long long n, k;
cin >> n >> k;
for (long long i = 1; i <= n; i++) {
long long a1, b1, t;
cin >> t >> a1 >> b1;
if (a1 == 1 && b1 == 1) {
c.push_back(t);
} else {
if (a1) {
a.push_back(t);
}
if (b1) {
b.push_back(t);
}
}
}
sort(b.begin(), b.end());
sort(a.begin(), a.end());
for (long long i = 0; i < a.size() && i < b.size(); i++) {
c.push_back(a[i] + b[i]);
}
if (c.size() < k)
cout << -1 << "\n";
else {
sort(c.begin(), c.end());
long long sum = 0;
for (long long i = 0; i < k; i++) {
sum += c[i];
}
cout << sum << "\n";
}
}
| 11 | CPP |
from sys import stdin
input=stdin.readline
def p(x):
return x[0]
n, k = map(int, input().split())
a = []
for i in range(n):
a.append(list(map(int, input().split())))
a.sort(key=p)
# print()
# print()
# for i in a:
# print(i)
alice, bob, common = [], [], []
al, bo = 0, 0
for i in a:
if(i[1] and not i[2] and al < k):
alice.append(i[0])
al += 1
if(i[2] and (not i[1]) and bo < k):
bob.append(i[0])
bo += 1
if(i[1] and i[2]):
if(al<k or bo<k ):
common.append(i[0])
al += 1
bo += 1
if(al > k ):
alice.pop()
al -= 1
if(bo > k):
bob.pop()
bo -= 1
else:
if(alice and bob):
if(alice[-1]+bob[-1] > i[0]):
alice.pop()
bob.pop()
common.append(i[0])
else:
break
# print(alice, bob, common, al, bo)
if(al >= k and bo >= k):
print(sum(alice)+sum(bob)+sum(common))
else:
print(-1)
| 11 | PYTHON3 |
import sys
input = lambda: sys.stdin.readline().rstrip()
inp = sys.stdin.buffer.readline
def I(): return list(map(int,inp().split()))
inf =10**8
n,k=[int(i) for i in input().split()]
al=[] ; bo=[] ; bt=[]
for i in range(n):
x=[int(i) for i in input().split()]
if x[1]==1 and x[2]==1:
bt.append(x[0])
elif x[1]==0 and x[2]==1:
bo.append(x[0])
elif x[1]==1 and x[2]==0:
al.append(x[0])
al.sort()
bo.sort()
bt.sort()
if len(bt)+len(al)<k or len(bt)+len(bo)<k:
print(-1)
exit(0)
else:
cnt=[[0,0,0] for i in range(max(len(al),len(bo),len(bt)))]
for i in range(len(cnt)):
if i<len(al): cnt[i][0]=al[i]
else: cnt[i][0]=inf
if i<len(bo): cnt[i][1]=bo[i]
else: cnt[i][1]=inf
if i<len(bt): cnt[i][2]=bt[i]
else: cnt[i][2]=inf
cnt.append([inf,inf,2*inf])
k1=k
i=0; j=0; k=0 ; val=0 ; ans=0
while val != k1:
if cnt[i][0]+cnt[j][1]>=cnt[k][2]:
ans+=cnt[k][2]
val+=1
k+=1
else:
ans+=cnt[i][1]+cnt[j][0]
val+=1
i+=1
j+=1
print(ans)
| 11 | PYTHON3 |
n, k = map(int, input().split())
books = []
booksA = []
booksB = []
for _ in range(n):
t, a, b = map(int, input().split())
if a == 1 and b == 0:
booksA.append(t)
elif a == 0 and b == 1:
booksB.append(t)
elif a == 1 and b == 1:
books.append(t)
booksA.sort(reverse=True)
booksB.sort(reverse=True)
books.sort(reverse=True)
t = 0
for _ in range(k):
if not books:
if (not booksA or not booksB):
t = -1
break
else:
t += booksA.pop() + booksB.pop()
elif (not booksA or not booksB) or books[-1] < booksA[-1] + booksB[-1]:
t += books.pop()
else:
t += booksA.pop() + booksB.pop()
print(t)
| 11 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int n, count;
cin >> n >> count;
vector<long long int> both, alice, bob;
long long int time, b, c;
for (int i = 0; i < n; i++) {
cin >> time >> b >> c;
if (b == 1 && c == 1)
both.push_back(time);
else if (b == 1 || c == 1) {
if (b == 1)
alice.push_back(time);
else
bob.push_back(time);
}
}
long long int minsize = both.size() + 0LL + min(alice.size(), bob.size());
if (minsize < count) {
cout << -1;
return 0;
}
minsize -= both.size();
if (both.size() > 0) sort(both.begin(), both.end());
if (alice.size() > 0) sort(alice.begin(), alice.end());
if (bob.size() > 0) sort(bob.begin(), bob.end());
long long int total = count;
vector<long long int> both_sum(both.size() + 1, 0),
alice_sum(alice.size() + 1, 0), bob_sum(bob.size() + 1, 0);
both_sum[0] = 0;
alice_sum[0] = 0;
bob_sum[0] = 0;
for (long long int i = 0; i < both.size(); i++)
both_sum[i + 1] = both[i] + both_sum[i];
for (long long int i = 0; i < alice.size(); i++)
alice_sum[i + 1] = alice[i] + alice_sum[i];
for (long long int i = 0; i < bob.size(); i++)
bob_sum[i + 1] = bob[i] + bob_sum[i];
long long int ans = LLONG_MAX;
minsize = min(minsize, count);
for (long long int i = minsize; i >= 0; i--) {
long long int right = total - i;
if (right >= both_sum.size()) break;
long long int rightval = both_sum[right] + alice_sum[i] + bob_sum[i];
ans = min(ans, rightval);
}
if (ans == LLONG_MAX)
cout << -1;
else
cout << ans;
return 0;
}
| 11 | CPP |
import sys
import bisect as b
import math
from collections import defaultdict as dd
input=sys.stdin.readline
#sys.setrecursionlimit(10**7)
def cin():
return map(int,sin().split())
def ain():
return list(map(int,sin().split()))
def sin():
return input()
def inin():
return int(input())
for _ in range(1):
n,k=cin()
l=[]
l1=[]
l3=[]
for i in range(n):
t,a,b=cin()
if(a==1 and b==1):
l3+=[t]
elif(a==1):
l+=[t]
elif(b==1):
l1+=[t]
l=sorted(l,reverse=True)
l1=sorted(l1,reverse=True)
l3=sorted(l3,reverse=True)
ans=-1
ma=10**5
if(len(l)+len(l3)>=k and len(l1)+len(l3)>=k):
ans=0
k1=k2=k
while(k1>0 and k2>0):
if(len(l1)==0):l1+=[ma]
if(len(l)==0):l+=[ma]
if(len(l3)==0):l3+=[ma]
if(l[-1]+l1[-1]>l3[-1]):
ans+=l3.pop()
else:
ans+=l1.pop()
ans+=l.pop()
k1-=1
k2-=1
print(ans)
| 11 | PYTHON3 |
import sys
input=sys.stdin.readline
n,k=map(int,input().split())
ans=[]
c1=0
c2=0
for i in range(n):
z=list(map(int,input().split()))
if(z[1]==1):
c1+=1
if(z[2]==1):
c2+=1
ans.append(z)
if(min(c1,c2)<k):
print(-1)
else:
a0=[]
a1=[]
a2=[]
for i in range(len(ans)):
if(ans[i][1]==1 and ans[i][2]==1):
a0.append(ans[i])
elif(ans[i][1]==1 and ans[i][2]==0):
a1.append(ans[i])
elif(ans[i][1]==0 and ans[i][2]==1):
a2.append(ans[i])
p0=0
p1=0
p2=0
c1=0
c2=0
a0.sort()
a1.sort()
a2.sort()
total=0
while(c1<k and c2<k):
if(p0<len(a0) and p1<len(a1) and p2<len(a2) and a0[p0][0]<=a1[p1][0]+a2[p2][0]):
total+=a0[p0][0]
c1+=1
c2+=1
p0+=1
else:
if(p1<len(a1) and p2<len(a2)):
total+=a1[p1][0]+a2[p2][0]
c1+=1
c2+=1
p1+=1
p2+=1
continue;
if(p1==len(a1) or p2==len(a2)):
total+=a0[p0][0]
c1+=1
c2+=1
p0+=1
print(total)
| 11 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9 + 1;
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;
}
| 11 | CPP |
n, k = list(map(int, input().split()))
time, A, B, AB, lenA, lenB, lenAB = [], [], [], [], 0, 0, 0
for idx in range(n):
t, a, b = list(map(int, input().split()))
time.append(t)
if a and not b:
A.append(t)
lenA += 1
elif b and not a:
B.append(t)
lenB += 1
elif a and b:
AB.append(t)
lenAB += 1
if lenA+lenAB<k or lenB+lenAB<k:
print(-1)
else:
A.sort()
B.sort()
AB.sort()
s, x, y = 0, 0, 0
for i in range(k):
if x<lenA and x<lenB and y<lenAB:
if A[x]+B[x]<AB[y]:
s += A[x]+B[x]
x += 1
else:
s += AB[y]
y += 1
elif x>=lenA or x>=lenB:
s += AB[y]
y += 1
elif y>=lenAB and x<lenA and x<lenB:
s += A[x]+B[x]
x += 1
print(s) | 11 | PYTHON3 |
import sys, math
input = sys.stdin.readline
def getInts():
return [int(s) for s in input().split()]
def getInt():
return int(input())
def getStrs():
return [s for s in input().split()]
def getStr():
return input().strip()
def listStr():
return list(input().strip())
import collections as col
import math
def solve():
N, K = getInts()
A = []
B = []
AB = []
for n in range(N):
T, a, b = getInts()
if a and b:
AB.append(T)
elif a:
A.append(T)
elif b:
B.append(T)
ans = 0
A.sort(reverse=True)
B.sort(reverse=True)
AB.sort(reverse=True)
def getNextBook():
if (not A or not B) and not AB:
return -1
if (not A or not B) or (AB and AB[-1] <= A[-1]+B[-1]):
return AB.pop()
return A.pop()+B.pop()
count = 0
while True:
x = getNextBook()
if x < 0:
return -1
ans += x
count += 1
if count == K:
return ans
#for _ in range(getInt()):
print(solve())
| 11 | PYTHON3 |
n , k = map(int,input().split(" "))
arr_a = []
arr_b = []
arr_c = []
cnt1=0
cnt2=0
for i in range (0,n):
t, a, b = map(int,input().split(" "))
if a ==1 and b==1 :
cnt1+=1
cnt2+=1
arr_c.append(t)
elif a==1 and b==0 :
cnt1+=1
arr_a.append(t)
elif a==0 and b==1 :
cnt2+=1
arr_b.append(t)
if (cnt1<k) or (cnt2 < k) :
print(-1)
else:
arr_a.sort()
arr_b.sort()
arr_c.sort()
cnt = 0
ans=0
x=0
y=0
z=0
while cnt < k:
if x >= len(arr_c):
cnt+=1
ans+= arr_a[y]+arr_b[z]
y+=1
z+=1
elif y>= len(arr_a) or z>= len(arr_b):
cnt+=1
ans+= arr_c[x]
x+=1
elif arr_c[x]< arr_a[y]+arr_b[z]:
cnt+=1
ans+= arr_c[x]
x+=1
else:
cnt+=1
ans+= arr_a[y]+arr_b[z]
y+=1
z+=1
print(ans)
| 11 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(98);
struct node {
int x, y, c;
long long s;
node *l, *r;
node(int x) : x(x), y(rng()), l(nullptr), r(nullptr) {
c = 1;
s = x;
}
void recalc();
};
int cnt(node *n) { return n ? n->c : 0; }
long long sum(node *n) { return n ? n->s : 0; }
void node::recalc() {
c = cnt(l) + cnt(r) + 1;
s = sum(l) + sum(r) + x;
}
pair<node *, node *> split(node *t, int x) {
if (!t) return {nullptr, nullptr};
if (t->x > x) {
auto p = split(t->l, x);
t->l = p.second;
t->recalc();
return {p.first, t};
}
auto p = split(t->r, x);
t->r = p.first;
t->recalc();
return {t, p.second};
}
node *insert(node *t, node *n) {
if (!t) return n;
if (n->y >= t->y) {
if (n->x <= t->x) {
t->l = insert(t->l, n);
t->recalc();
} else {
t->r = insert(t->r, n);
t->recalc();
}
return t;
}
auto p = split(t, n->x);
n->l = p.first;
n->r = p.second;
n->recalc();
return n;
}
long long prf(node *t, int p) {
if (!t) return 0;
if (cnt(t) < p) return -1;
if (p <= cnt(t->l)) return prf(t->l, p);
return sum(t->l) + t->x + prf(t->r, p - cnt(t->l) - 1);
}
const int MAXN = 2e5 + 10;
int val[MAXN], msk[MAXN], n, m, k;
vector<int> byt[4];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> k;
for (int i = 1; i <= n; i++) {
cin >> val[i];
int x;
cin >> x;
msk[i] += x;
cin >> x;
msk[i] += 2 * x;
}
for (int i = 1; i <= n; i++) byt[msk[i]].push_back(val[i]);
for (int i = 0; i < 4; i++) sort(byt[i].begin(), byt[i].end());
node *t = nullptr;
for (auto x : byt[0]) t = insert(t, new node(x));
long long curs = 0, ans = -1, s1 = 0, s2 = 0;
for (int i = 0; i < min(k + 1, (int)byt[1].size()); i++) s1 += byt[1][i];
for (int i = 0; i < min(k + 1, (int)byt[2].size()); i++) s2 += byt[2][i];
for (int i = k + 1; i < byt[1].size(); i++)
t = insert(t, new node(byt[1][i]));
for (int i = k + 1; i < byt[2].size(); i++)
t = insert(t, new node(byt[2][i]));
int bst = -1;
for (int both = 0; both <= byt[3].size(); both++) {
int oth = max(k - both, -1);
if (both) curs += byt[3][both - 1];
if (oth >= 0 && oth < byt[1].size()) {
s1 -= byt[1][oth];
t = insert(t, new node(byt[1][oth]));
}
if (oth >= 0 && oth < byt[2].size()) {
s2 -= byt[2][oth];
t = insert(t, new node(byt[2][oth]));
}
oth = max(oth, 0);
if (oth > byt[1].size() || oth > byt[2].size() || both + 2 * oth > m ||
m - both - 2 * oth > cnt(t))
continue;
int rem = m - both - 2 * oth;
long long x = curs + s1 + s2 + prf(t, rem);
if (ans == -1) {
ans = x;
bst = both;
} else {
if (x < ans) {
ans = x;
bst = both;
}
}
}
cout << ans << '\n';
if (ans > -1) {
vector<pair<int, int> > fy[4];
for (int i = 1; i <= n; i++) fy[msk[i]].push_back({val[i], i});
for (int i = 0; i < 4; i++) sort(fy[i].rbegin(), fy[i].rend());
for (int i = 0; i < bst; i++) {
cout << fy[3].back().second << " ";
fy[3].pop_back();
}
int rem = max(k - bst, 0);
for (int i = 0; i < rem; i++) {
cout << fy[2].back().second << " ";
fy[2].pop_back();
cout << fy[1].back().second << " ";
fy[1].pop_back();
}
int reallyrem = m - bst - 2 * rem;
for (int i = 0; i < reallyrem; i++) {
int mn = INT_MAX;
for (int j = 0; j < 3; j++) {
if (fy[j].size()) mn = min(mn, fy[j].back().first);
}
for (int j = 0; j < 3; j++) {
if (fy[j].size() && fy[j].back().first == mn) {
cout << fy[j].back().second << " ";
fy[j].pop_back();
break;
}
}
}
cout << '\n';
}
}
| 11 | CPP |
import sys
s = sys.stdin.readline().split()
n, k = int(s[0]), int(s[1])
i = 1
arr11 = []
arr01 = []
arr10 = []
arr00 = []
while i <= n:
s = input().split()
s = list(map(int, s))
if s[-1] == 0:
if s[1] == 0:
arr00.append(s[0])
else:
arr10.append(s[0])
else:
if s[1] == 0:
arr01.append(s[0])
else:
arr11.append(s[0])
i += 1
arr00.sort()
arr10.sort()
arr01.sort()
arr11.sort()
arr1001 = list(map(lambda x, y : x + y, arr01,arr10))
finallst = arr1001 + arr11
finallst.sort()
i = 0
ans = 0
if len(finallst) < k:
ans = -1
i = k
while i < k:
ans = ans + finallst[i]
i += 1
print(ans)
| 11 | PYTHON3 |
import sys
input = sys.stdin.readline
'''
'''
def solve(n, k, t, a, b):
alice_only = []
bob_only = []
both = []
for i in range(n):
if a[i] and b[i]:
both.append(t[i])
elif a[i] and not b[i]:
alice_only.append(t[i])
elif b[i] and not a[i]:
bob_only.append(t[i])
if len(bob_only) + len(both) < k:
return -1
elif len(alice_only) + len(both) < k:
return -1
bob_only.sort()
alice_only.sort()
both.sort()
both_pre = [0]
bob_pre = [0]
alice_pre = [0]
for tb in bob_only:
bob_pre.append(bob_pre[-1] + tb)
for ta in alice_only:
alice_pre.append(alice_pre[-1] + ta)
for tbth in both:
both_pre.append(both_pre[-1] + tbth)
best = float("inf")
#print(both_pre)
#print(alice_pre)
#print(bob_pre)
lb1 = k - len(alice_only)
lb2 = k - len(bob_only)
for cur_k in range(max(lb1, lb2, 0), min(len(both_pre), k+1)):
time_both = both_pre[cur_k]
time_alice = alice_pre[k-cur_k]
time_bob = bob_pre[k-cur_k]
best = min(best, time_both+time_alice+time_bob)
#print(best, cur_k)
if len(alice_pre) > k and len(bob_pre) > k:
best = min(best, alice_pre[k] + bob_pre[k])
return best
n, k = map(int, input().split())
t, a, b = [], [], []
for _ in range(n):
ti, ai, bi = map(int, input().split())
t.append(ti)
a.append(ai)
b.append(bi)
print(solve(n, k, t, a, b)) | 11 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long int a, b, c, d, e, f = 5 * 1e9, m11[200000], m01[200000], m10[200000],
k, k1, k2;
int main() {
cin >> a >> b;
for (int i = 0; i < a; i++) {
cin >> c >> d >> e;
if (e == 1 && d == 1) m11[k] = c, k++;
if (d == 1 && e == 0) m10[k1] = c, k1++;
if (d == 0 && e == 1) m01[k2] = c, k2++;
}
sort(m11, m11 + k);
sort(m10, m10 + k1);
sort(m01, m01 + k2);
if (k + min(k1, k2) < b) {
cout << -1;
return 0;
}
for (int i = 1; i < max(max(k, k1), k2); i++) {
m11[i] += m11[i - 1];
m01[i] += m01[i - 1];
m10[i] += m10[i - 1];
}
for (int i = 0; i < min(b, k); i++) {
if (b - 2 - i < min(k1, k2))
f = min(m11[i] + m01[b - 2 - i] + m10[b - 2 - i], f);
}
if (k >= b) f = min(f, m11[b - 1]);
if (min(k1, k2) >= b) f = min(m01[b - 1] + m10[b - 1], f);
cout << f;
}
| 11 | CPP |
from sys import stdin,stdout
import bisect
def st():
return list(stdin.readline())
def inp():
return int(stdin.readline())
def li():
return list(map(int,stdin.readline().split()))
def mp():
return map(int,stdin.readline().split())
def pr(n):
stdout.write(str(n)+"\n")
def soe(limit):
l=[1]*(limit+1)
prime=[]
for i in range(2,limit+1):
if l[i]:
for j in range(i*i,limit+1,i):
l[j]=0
for i in range(2,limit+1):
if l[i]:
prime.append(i)
return prime
def segsoe(low,high):
limit=int(high**0.5)+1
prime=soe(limit)
n=high-low+1
l=[0]*(n+1)
for i in range(len(prime)):
lowlimit=(low//prime[i])*prime[i]
if lowlimit<low:
lowlimit+=prime[i]
if lowlimit==prime[i]:
lowlimit+=prime[i]
for j in range(lowlimit,high+1,prime[i]):
l[j-low]=1
for i in range(low,high+1):
if not l[i-low]:
if i!=1:
print(i)
def power(a,n):
r=1
while n:
if n&1:
r=(r*a)
n=n-1
else:
a=(a*a)
n=n>>1
return r
def solve():
n,k=mp()
both=[]
first=[]
second=[]
for i in range(n):
a,b,c=mp()
if b==1 and c==1:
both.append(a)
elif b==1 and c==0:
first.append(a)
elif b==0 and c==1:
second.append(a)
if len(both)+len(first)<k or len(both)+len(second)<k:
pr(-1)
else:
both.sort()
first.sort()
second.sort()
i,j,z=0,0,0
p=0
while k!=0:
if z<len(both):
temp=both[z]
else:
temp=float("inf")
if i<len(first):
temp1=first[i]
else:
temp1=float("inf")
if j<len(second):
temp2=second[j]
else:
temp2=float("inf")
if temp<=temp1+temp2:
p+=temp
z+=1
else:
p+=temp1+temp2
i+=1
j+=1
k-=1
print(p)
solve()
##
##for _ in range(inp()):
## solve()
| 11 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 30;
const double pi = acos(-1);
const double eps = 1e-9;
const int mod = 1e9 + 7;
const int mf[] = {0, 0, 1, -1}, mc[] = {1, -1, 0, 0};
const int N = 1005;
mt19937 rng(
(unsigned int)chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
static T randint(T lo, T hi) {
return uniform_int_distribution<T>(lo, hi)(rng);
}
template <typename T>
static T randreal(T lo, T hi) {
return uniform_real_distribution<T>(lo, hi)(rng);
}
void nop() {
cout << -1 << '\n';
exit(0);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
rng.seed(time(0));
int n, m, k;
cin >> n >> m >> k;
vector<pair<int, int>> a(1, {0, 0}), b(1, {0, 0}), c(1, {0, 0}), d(1, {0, 0});
for (int i = 1; i <= n; i++) {
int ti, ai, bi;
cin >> ti >> ai >> bi;
if (ai == 1 && bi == 0) a.push_back({ti, i});
if (ai == 0 && bi == 1) b.push_back({ti, i});
if (ai == 1 && bi == 1) c.push_back({ti, i});
if (ai == 0 && bi == 0) d.push_back({ti, i});
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
sort(c.begin(), c.end());
sort(d.begin(), d.end());
vector<int> ta(a.size()), tb(b.size()), tc(c.size()), td(d.size());
int sa = a.size() - 1, sb = b.size() - 1, sc = c.size() - 1,
sd = d.size() - 1;
for (int i = 1; i <= sa; i++) ta[i] = ta[i - 1] + a[i].first;
for (int i = 1; i <= sb; i++) tb[i] = tb[i - 1] + b[i].first;
for (int i = 1; i <= sc; i++) tc[i] = tc[i - 1] + c[i].first;
for (int i = 1; i <= sd; i++) td[i] = td[i - 1] + d[i].first;
int ans = -1, ita, itb, itc, itd;
for (int fix = 0; fix <= sc && fix <= m; fix++) {
if (sa < k - fix || sb < k - fix) continue;
int s = tc[fix] + (((k - fix) >= 0) ? ta[k - fix] + tb[k - fix] : 0);
int t = fix + max(0, (k - fix) * 2);
if (t > m) continue;
if (t == m) {
if (ans == -1 || ans > s) {
ans = s;
ita = max(0, k - fix);
itb = max(0, k - fix);
itc = fix;
itd = 0;
}
continue;
}
int ra = ((k - fix) >= 0) ? k - fix : 0;
int rb = ((k - fix) >= 0) ? k - fix : 0;
if (sa - ra + sb - rb + sd < m - t) continue;
int lo = min({a[ra].first, b[ra].first}), hi = 10000;
if (sd > 0) lo = min(lo, d[1].first);
while (lo < hi) {
int mid = (lo + hi) >> 1;
int upa =
upper_bound(a.begin(), a.end(), make_pair(mid, inf)) - a.begin() - 1;
int upb =
upper_bound(b.begin(), b.end(), make_pair(mid, inf)) - b.begin() - 1;
int upd =
upper_bound(d.begin(), d.end(), make_pair(mid, inf)) - d.begin() - 1;
if (max(0, upa - ra) + max(0, upb - rb) + upd >= m - t)
hi = mid;
else
lo = mid + 1;
}
int upa =
upper_bound(a.begin(), a.end(), make_pair(lo - 1, inf)) - a.begin() - 1;
int upb =
upper_bound(b.begin(), b.end(), make_pair(lo - 1, inf)) - b.begin() - 1;
int upd =
upper_bound(d.begin(), d.end(), make_pair(lo - 1, inf)) - d.begin() - 1;
s += max(0, ta[upa] - ta[ra]) + max(0, tb[upb] - tb[rb]) + td[upd];
t += max(0, upa - ra) + max(0, upb - rb) + upd;
s += max(0, (m - t) * lo);
if (ans == -1 || ans > s) {
ans = s;
ita = max(ra, upa);
itb = max(rb, upb);
itc = fix;
itd = upd;
}
}
cout << ans << '\n';
if (ans != -1) {
vector<int> g;
multiset<pair<int, int>> ms;
for (int i = 1; i <= sa; i++)
if (i <= ita)
g.push_back(a[i].second);
else
ms.insert(a[i]);
for (int i = 1; i <= sb; i++)
if (i <= itb)
g.push_back(b[i].second);
else
ms.insert(b[i]);
for (int i = 1; i <= sc; i++)
if (i <= itc)
g.push_back(c[i].second);
else
ms.insert(c[i]);
for (int i = 1; i <= sd; i++)
if (i <= itd)
g.push_back(d[i].second);
else
ms.insert(d[i]);
while ((int)g.size() < m) {
g.push_back(ms.begin()->second);
ms.erase(ms.begin());
}
for (int i = 0; i < m; i++) cout << g[i] << " \n"[i == m - 1];
}
return 0;
}
| 11 | CPP |
import sys
n,k = map(int,input().split())
M = []
A = []
B = []
for i in range(n):
t,a,b = map(int,input().split())
if a == 1 and b == 1:
M.append(t)
elif a == 1:
A.append(t)
elif b == 1:
B.append(t)
if min(len(M)+len(A) , len(M)+len(B)) < k:
print (-1)
sys.exit()
A.append(0)
B.append(0)
M.append(0)
A.sort()
B.sort()
M.sort()
for i in range(len(A)-1):
A[i+1] += A[i]
for i in range(len(B)-1):
B[i+1] += B[i]
for i in range(len(M)-1):
M[i+1] += M[i]
ans = float("inf")
for i in range(k+1):
j = k-i
if len(A) > i and len(B) > i and len(M) > j:
ans = min(ans , A[i] + B[i] + M[j])
print (ans)
| 11 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
struct indices {
int i11, i10, i01, i00;
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, k;
cin >> n >> m >> k;
vector<pair<long long int, int> > v11, v10, v01, v00;
for (int i = 0; i < n; i++) {
int t, a, b;
cin >> t >> a >> b;
if (a && b)
v11.push_back(pair<long long int, int>(t, i + 1));
else if (a)
v10.push_back(pair<long long int, int>(t, i + 1));
else if (b)
v01.push_back(pair<long long int, int>(t, i + 1));
else
v00.push_back(pair<long long int, int>(t, i + 1));
}
sort(v11.begin(), v11.end());
sort(v10.begin(), v10.end());
sort(v01.begin(), v01.end());
sort(v00.begin(), v00.end());
if ((int)v11.size() + min((int)v10.size(), (int)v01.size()) < k) {
cout << -1 << '\n';
return 0;
}
indices res;
res.i11 = res.i10 = res.i01 = res.i00 = -1;
long long int ans = 1e15;
int b = 0, c = 0, d = 0;
long long int acum = 0ll;
for (auto e : v11) {
acum += e.first;
}
long long int sum = acum;
for (int a = (int)v11.size(); a >= 0; a--) {
while (a + b < k && b < (int)v10.size()) {
sum += v10[b].first;
b++;
}
while (a + c < k && c < (int)v01.size()) {
sum += v01[c].first;
c++;
}
if (a + b + c + d > m) {
if (d) {
sum -= v00[d - 1].first;
d--;
}
}
int faltan = m - (a + b + c + d);
bool can = true;
for (int i = 0; i < faltan; i++) {
int costb = inf, costc = inf, costd = inf;
if (b < (int)v10.size()) {
costb = v10[b].first;
}
if (c < (int)v01.size()) {
costc = v01[c].first;
}
if (d < (int)v00.size()) {
costd = v00[d].first;
}
if (min({costb, costc, costd}) == inf) {
can = false;
break;
}
if (costb <= costc && costb <= costd && costb != inf) {
sum += costb;
b++;
} else if (costc <= costb && costc <= costd && costc != inf) {
sum += costc;
c++;
} else {
sum += costd;
d++;
}
}
if (can) {
if (a + b >= k && a + c >= k && (a + b + c + d) == m && sum < ans) {
ans = sum;
res.i11 = a;
res.i10 = b;
res.i01 = c;
res.i00 = d;
}
}
if (a) sum -= v11[a - 1].first;
}
if (res.i11 < 0) {
cout << -1 << '\n';
return 0;
}
cout << ans << '\n';
for (int i = 0; i < res.i11; i++) cout << v11[i].second << " ";
for (int i = 0; i < res.i10; i++) cout << v10[i].second << " ";
for (int i = 0; i < res.i01; i++) cout << v01[i].second << " ";
for (int i = 0; i < res.i00; i++) cout << v00[i].second << " ";
cout << '\n';
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
void adskiy_razgon() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
const int day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const int dxo[8] = {-1, -1, -1, 0, 1, 1, 1, 0},
dyo[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
long long nod(long long a, long long b) {
if (b > a) swap(a, b);
while (b > 0) {
a %= b;
swap(a, b);
}
return a;
}
long long nok(long long a, long long b) { return a * b / nod(a, b); }
void sp(long long a, double b) { cout << fixed << setprecision(a) << b; }
template <class A>
void read(vector<A>& v);
template <class A, size_t S>
void read(array<A, S>& a);
template <class T>
void read(T& x) {
cin >> x;
}
void read(double& d) {
string t;
read(t);
d = stod(t);
}
void read(long double& d) {
string t;
read(t);
d = stold(t);
}
template <class H, class... T>
void read(H& h, T&... t) {
read(h);
read(t...);
}
template <class A>
void read(vector<A>& x) {
for (auto& a : x) read(a);
}
template <class A, size_t S>
void read(array<A, S>& x) {
for (auto& a : x) read(a);
}
string to_string(char c) { return string(1, c); }
string to_string(const char* s) { return string(s); }
string to_string(string s) { return s; }
string to_string(vector<bool> v) {
string res;
for (int i = (0); (1) > 0 ? i < ((int)(v).size()) : i > ((int)(v).size());
i += (1))
res += char('0' + v[i]);
return res;
}
template <size_t S>
string to_string(bitset<S> b) {
string res;
for (int i = (0); (1) > 0 ? i < (S) : i > (S); i += (1))
res += char('0' + b[i]);
return res;
}
template <class T>
string to_string(T v) {
bool f = 1;
string res;
for (auto& x : v) {
if (!f) res += ' ';
f = 0;
res += to_string(x);
}
return res;
}
template <class A>
void write(A x) {
cout << to_string(x);
}
template <class H, class... T>
void write(const H& h, const T&... t) {
write(h);
write(t...);
}
void print() { write("\n"); }
template <class H, class... T>
void print(const H& h, const T&... t) {
write(h);
if (sizeof...(t)) write(' ');
print(t...);
}
void yes(bool ok) { print((ok ? "YES" : "Yes")); }
void no(bool ok) { print((ok ? "NO" : "No")); }
const int MOD = 1e9 + 7;
const int N = 2e5 + 5;
const long long INF = 1e21;
const long double PI = acos((long double)-1);
const int M = 1005;
long long binpow(long long a, long long n) {
if (n == 0) return 1;
if (n % 2 == 1)
return ((binpow(a, n - 1)) * a);
else {
long long b = (binpow(a, n / 2));
return (b * b);
}
}
vector<int> eq;
vector<int> al, bob;
void solve() {
int n, m;
read(n, m);
for (int i = 1; i <= n; ++i) {
int num;
bool Alice, Bob;
read(num, Alice, Bob);
if (Alice && Bob)
eq.push_back(num);
else {
if (Alice)
al.push_back(num);
else if (Bob)
bob.push_back(num);
}
}
sort(al.begin(), al.end());
sort(bob.begin(), bob.end());
for (int i = 0; i < min((int)(al).size(), (int)(bob).size()); ++i)
eq.push_back(al[i] + bob[i]);
sort(eq.begin(), eq.end());
if ((int)(eq).size() < m) {
print(-1);
return;
}
int ans = 0;
for (int i = 0; i < m; ++i) ans += eq[i];
print(ans);
}
int main() {
adskiy_razgon();
long long t = 1;
for (int i = (0); (1) > 0 ? i < (t) : i > (t); i += (1)) {
solve();
}
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long dx[] = {-1, 0, 1, 0};
long long dy[] = {0, -1, 0, 1};
vector<pair<long long, long long> > v[4];
long long n, m, k;
vector<long long> uttar;
bool check = false;
long long call(long long x) {
check = false;
if (x > m) {
check = true;
return INT_MAX;
}
long long res = 0;
uttar.clear();
for (long long i = 0; i < x; i++) {
res += v[3][i].first;
uttar.push_back(v[3][i].second);
}
long long rem = m - x;
vector<pair<long long, long long> > baki;
if (x < k) {
long long bacha = k - x;
if (v[1].size() < bacha || v[2].size() < bacha || rem < bacha * 2) {
check = true;
return INT_MAX - x;
}
rem -= bacha * 2;
for (long long i = 0; i < bacha; i++) {
uttar.push_back(v[1][i].second);
uttar.push_back(v[2][i].second);
res += v[1][i].first;
res += v[2][i].first;
}
for (long long i = 1; i <= 2; i++)
for (long long j = bacha; j < v[i].size(); j++) {
baki.push_back(v[i][j]);
}
for (auto i : v[0]) baki.push_back(i);
} else {
for (long long i = 1; i <= 2; i++)
for (auto j : v[i]) {
baki.push_back(j);
}
for (auto j : v[0]) baki.push_back(j);
}
sort(baki.begin(), baki.end());
if (baki.size() < rem) {
check = true;
return INT_MAX - x;
}
for (long long i = 0; i < rem; i++) {
uttar.push_back(baki[i].second);
res += baki[i].first;
}
return res;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> k;
for (long long i = 0; i < n; i++) {
long long t, x, y;
cin >> t >> x >> y;
if (x && y)
v[3].push_back({t, i});
else if (x) {
v[2].push_back({t, i});
} else if (y)
v[1].push_back({t, i});
else
v[0].push_back({t, i});
}
for (long long i = 0; i < 4; i++) {
sort(v[i].begin(), v[i].end());
}
long long s = -1, e = min(m, (long long)v[3].size());
while (e - s > 1) {
long long mid1 = (s + e) >> 1;
long long mid2 = mid1 + 1;
if (call(mid1) > call(mid2))
s = mid1;
else
e = mid1;
}
long long ans = call(e);
if (check) {
cout << "-1"
<< "\n";
return 0;
}
cout << ans << "\n";
for (auto i : uttar) cout << i + 1 << " ";
cout << "\n";
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, k;
cin >> n >> k;
vector<long long> ones, twos, threes;
for (long long i = 0; i < n; i++) {
long long t, a, b;
cin >> t >> a >> b;
if (a == 1 && b == 1) {
threes.push_back(t);
} else {
if (a == 1) ones.push_back(t);
if (b == 1) twos.push_back(t);
}
}
long long ans = -1;
sort((ones).begin(), (ones).end());
sort((twos).begin(), (twos).end());
sort((threes).begin(), (threes).end());
vector<long long> on(ones.size() + 1, 0);
vector<long long> dos(twos.size() + 1, 0);
vector<long long> tres(threes.size() + 1, 0);
for (long long i = 0; i < ones.size(); i++) {
on[i + 1] = on[i] + ones[i];
}
for (long long i = 0; i < twos.size(); i++) {
dos[i + 1] = dos[i] + twos[i];
}
for (long long i = 0; i < threes.size(); i++) {
tres[i + 1] = tres[i] + threes[i];
}
for (long long i = 0; i < tres.size(); i++) {
long long k2 = i;
long long k1 = k - i;
if (k1 >= 0 && k1 < min(on.size(), dos.size())) {
if (ans == -1) ans = tres[i] + on[k1] + dos[k1];
ans = min(ans, tres[i] + on[k1] + dos[k1]);
}
}
cout << ans << "\n";
}
| 11 | CPP |
import sys
import math as mt
input=sys.stdin.buffer.readline
t=1
def Sort(sub_li):
sub_li.sort(key = lambda x: x[0])
return sub_li
#t=int(input())
for _ in range(t):
#n=int(input())
n,k=map(int,input().split())
#l=list(map(int,input().split()))
l2=[]
common=[]
alice=[]
bob=[]
for ___ in range(n):
time,a,b=map(int,input().split())
if (a&b):
common.append(time)
else:
if (a):
alice.append(time)
if (b):
bob.append(time)
common.sort()
alice.sort()
bob.sort()
if (len(common)+min(len(alice),len(bob)))<k:
print(-1)
else:
common.append(10**9)
alice.append(10**9)
bob.append(10**9)
al,bo=0,0
co=0
i=0
ans=0
while i<k :
if (common[co]<=(bob[bo]+alice[al])) :
ans+=common[co]
i+=1
co+=1
else:
ans+=(bob[bo]+alice[al])
i+=1
al+=1
bo+=1
print(ans) | 11 | PYTHON3 |
import atexit
import io
import sys
import math
from collections import defaultdict,Counter
# _INPUT_LINES = sys.stdin.read().splitlines()
# input = iter(_INPUT_LINES).__next__
# _OUTPUT_BUFFER = io.StringIO()
# sys.stdout = _OUTPUT_BUFFER
# @atexit.register
# def write():
# sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
# sys.stdout=open("CP3/output.txt",'w')
# sys.stdin=open("CP3/input.txt",'r')
# m=pow(10,9)+7
n,k=map(int,input().split())
c1=0
c2=0
l1=[]
l2=[]
l=[]
# visit=[0]*n
for i in range(n):
t,a,b=map(int,input().split())
c1+=a
c2+=b
if a+b==2:
l.append([t,i])
continue
if a==1:
l1.append([t,i])
if b==1:
l2.append([t,i])
# visit[i]=1
if c1<k or c2<k:
print(-1)
else:
l1.sort(reverse=True)
l2.sort(reverse=True)
l.sort(reverse=True)
# print(l)
# print(l1)
# print(l2)
time=0
while k:
if len(l1)==0 or len(l2)==0 or (l and l1[-1][0]+l2[-1][0]>l[-1][0]):
time+=l.pop()[0]
else:
time+=l1.pop()[0]
time+=l2.pop()[0]
k-=1
print(time) | 11 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 998244353;
const long double PI = 3.14159265359;
const long long int INF = 1e9;
char salpha[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char calpha[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
long long int gcd(long long int a, long long int b) {
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
long long int exp(long long int b, long long int p) {
if (p == 0) {
return 1;
} else if (p % 2 == 0) {
return exp(b * b, p / 2);
} else {
return b * exp(b * b, (p - 1) / 2);
}
}
long long int mexp(long long int b, long long int p) {
if (p == 0) {
return 1;
} else if (p % 2 == 0) {
return mexp((b * b) % MOD, p / 2) % MOD;
} else {
return (b * mexp((b * b) % MOD, (p - 1) / 2)) % MOD;
}
}
long long int minv(long long int a) { return mexp(a, MOD - 2); }
int isprime(long long int n) {
for (long long int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
vector<int> a, b, c;
for (int i = 0; i < n; i++) {
int t, ap, bp;
cin >> t >> ap >> bp;
if (ap == 1 && bp == 1)
c.push_back(t);
else if (ap == 1)
a.push_back(t);
else if (bp == 1)
b.push_back(t);
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
sort(c.begin(), c.end());
if (a.size() + c.size() < k || b.size() + c.size() < k)
cout << "-1"
<< "\n";
else {
int ai = 0, bi = 0, ci = 0;
long long int ans = 0;
for (int i = 0; i < k; i++) {
long long int flag1 = -1, flag2 = -1;
long long int opt1, opt2;
if (ai != a.size() && bi != b.size()) {
flag1 = 1;
opt1 = a[ai] + b[bi];
ai++;
bi++;
}
if (ci != c.size()) {
flag2 = 1;
opt2 = c[ci];
ci++;
}
if (flag1 == -1) {
ans += opt2;
} else if (flag2 == -1) {
ans += opt1;
} else if (flag1 == 1 && flag2 == 1) {
if (opt1 < opt2) {
ans += opt1;
ci--;
} else {
ans += opt2;
ai--;
bi--;
}
}
}
cout << ans << "\n";
}
}
| 11 | CPP |
#t=int(input())
#l=[];sets=set()
t=1
def primes(n):
count=0;rl=0
while n%2==0:
n=n//2
count+=1
while n%3==0:
n=n//3
rl+=1
if (n!=1 or count>rl):
return -1
else:
return [rl,count]
from math import *
for xy in range(t):
n,k=[int(x) for x in input().split()]
#l=[int(x)%k for x in input().split()]
tq={};sets=set()
ar=[];ac=[];bb=[]
for i in range(n):
t,a,b=[int(x) for x in input().split()]
if (a==1 and b==1):
bb.append(t)
elif a==1:
ar.append(t)
elif b==1:
ac.append(t)
ar=sorted(ar);ac=sorted(ac);bb=sorted(bb)
#print(ar,ac,bb)
i=0;j=0
count=0;
lp=0
rq=True
while lp<k:
if (i<len(ar) and i<len(ac) and ((j<len(bb) and ac[i]+ar[i]<=bb[j]) or (j>=len(bb)))):
count+=(ar[i]+ac[i]);i+=1;lp+=1
elif j<len(bb):
count+=bb[j];
j+=1;lp+=1
#print(count)
#count+=1
else:
rq=False
break
if rq:
print(count)
else:
print(-1)
| 11 | PYTHON3 |
import sys
input = sys.stdin.readline
n, k = map(int, input().split())
B = [tuple(map(int, input().split())) for _ in range(n)]
FB = []
AB = []
BB = []
for t, a, b in B:
if a and b:
FB.append(t)
elif a:
AB.append(t)
elif b:
BB.append(t)
AB.sort()
BB.sort()
for t1, t2 in zip(AB, BB):
FB.append(t1+t2)
FB.sort()
if len(FB) < k:
print(-1)
else:
print(sum(FB[:k]))
| 11 | PYTHON3 |
n, k = map(int, input().strip().split())
not_possible = False
alice_only, bob_only, both = [], [], []
alice_likes, bob_likes = 0, 0
total_time = 0
for _ in range(n):
t, alice, bob = map(int, input().strip().split())
alice_likes += alice
bob_likes += bob
if alice + bob == 2:
both.append(t)
elif alice:
alice_only.append(t)
elif bob:
bob_only.append(t)
if alice + bob > 0:
total_time += t
if alice_likes < k or bob_likes < k:
not_possible = True
alice_remove = alice_likes - k
bob_remove = bob_likes - k
both_remove = min(alice_remove, bob_remove)
alice_remove -= both_remove
bob_remove -= both_remove
both.sort(reverse=True)
alice_only.sort(reverse=True)
bob_only.sort(reverse=True)
a, b = 0, 0
while a < len(alice_only) and alice_remove > 0:
total_time -= alice_only[a]
a += 1
alice_remove -= 1
while b < len(bob_only) and bob_remove > 0:
total_time -= bob_only[b]
b += 1
bob_remove -= 1
alice_only = alice_only[a:]
bob_only = bob_only[b:]
ab, o = 0, 0
while both_remove > 0:
curr_ab, curr_both = 0, 0
if ab < len(alice_only) and ab < len(bob_only):
curr_ab = alice_only[ab] + bob_only[ab]
if o < len(both):
curr_both = both[o]
if curr_ab + curr_both == 0:
break
if curr_ab > curr_both:
total_time -= curr_ab
ab += 1
else:
total_time -= curr_both
o += 1
both_remove -= 1
alice_remove += both_remove
bob_remove += both_remove
a, b = ab, ab
while a < len(alice_only) and alice_remove > 0:
total_time -= alice_only[a]
a += 1
alice_remove -= 1
while b < len(bob_only) and bob_remove > 0:
total_time -= bob_only[b]
b += 1
bob_remove -= 1
if not_possible:
print(-1)
else:
print(total_time) | 11 | PYTHON3 |
# import os,io
# input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
n,m,k = map(int,input().split())
bookA = []
bookB = []
bookAB = []
book = []
for i in range(n):
t,a,b = map(int,input().split())
if a == 1 and b == 1:
bookAB.append((t,i))
elif a == 1:
bookA.append((t,i))
book.append((t,i))
elif b == 1:
bookB.append((t,i))
book.append((t,i))
else:
book.append((t,i))
bookA.sort(key = lambda x:x[0])
bookB.sort(key = lambda x:x[0])
bookAB.sort(key = lambda x:x[0])
book.sort(key = lambda x:x[0])
bookAIndex = 0
bookBIndex = 0
bookALocation = []
bookBLocation = []
for i in range(len(book)):
if bookAIndex < len(bookA) and bookA[bookAIndex] == book[i]:
bookALocation.append(i)
bookAIndex += 1
if bookBIndex < len(bookB) and bookB[bookBIndex] == book[i]:
bookBLocation.append(i)
bookBIndex += 1
bookACount = 0
bookBCount = 0
bookCount = [(0,0)] # bookCount[i] stores how many bookA and how many bookB are in first i books
for i in range(len(book)):
if bookACount < len(bookALocation) and bookALocation[bookACount] == i:
bookACount += 1
if bookBCount < len(bookBLocation) and bookBLocation[bookBCount] == i:
bookBCount += 1
bookCount.append((bookACount,bookBCount))
bookASum = [0]
bookBSum = [0]
bookABSum = [0]
bookSum = [0]
for elem in bookA:
bookASum.append(bookASum[-1] + elem[0])
for elem in bookB:
bookBSum.append(bookBSum[-1] + elem[0])
for elem in bookAB:
bookABSum.append(bookABSum[-1] + elem[0])
for elem in book:
bookSum.append(bookSum[-1] + elem[0])
minReadingTime = -1
for i in range(len(bookABSum)):
if len(bookA) >= k - i and len(bookB) >= k - i and 2 * k - i <= m and m >= i:
goalbookCount = m - i - 2 * max(k-i,0)
high = len(book)
low = goalbookCount
isPossible = True
while True:
if high < low:
isPossible = False
break
mid = (high + low) // 2
curBookCount = mid - min(max(k-i,0),bookCount[mid][0]) - min(max(k-i,0),bookCount[mid][1])
if curBookCount == goalbookCount:
if high == low:
break
high = mid
else:
if high == low:
isPossible = False
break
else:
if curBookCount > goalbookCount:
high = mid - 1
else:
low = mid + 1
if isPossible and (minReadingTime == -1 or minReadingTime > bookABSum[i] + bookASum[max(k-i,0)] +
bookBSum[max(k-i,0)] + bookSum[mid] - bookASum[min(max(k-i,0),bookCount[mid][0])] - bookBSum[min(max(k-i,0),bookCount[mid][1])]):
minReadingTime = bookABSum[i] + bookASum[max(k-i,0)] + bookBSum[max(k-i,0)] + bookSum[mid] - bookASum[min(max(k-i,0),bookCount[mid][0])] - bookBSum[min(max(k-i,0),bookCount[mid][1])]
minIndicies = (i,max(k-i,0),mid,min(max(k-i,0),bookCount[mid][0]),min(max(k-i,0),bookCount[mid][1]))
print(minReadingTime)
printList = []
if minReadingTime != -1:
for i in range(minIndicies[0]):
printList.append(str(bookAB[i][1] + 1))
for i in range(minIndicies[2]):
printList.append(str(book[i][1] + 1))
for i in range(minIndicies[3],minIndicies[1]):
printList.append(str(bookA[i][1] + 1))
for i in range(minIndicies[4],minIndicies[1]):
printList.append(str(bookB[i][1] + 1))
print(' '.join(printList)) | 11 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int k, n;
cin >> n >> k;
int aa[n], ba[n], ra[n];
int r = 0, a = 0, b = 0;
for (int i = 0, v, m, u; i < n; ++i) {
cin >> v >> m >> u;
if (m && u)
ra[r++] = v;
else if (u)
ba[b++] = v;
else if (m)
aa[a++] = v;
}
sort(ba, ba + b);
sort(aa, aa + a);
for (int i = 0, s = min(a, b); i < s; ++i) ra[r++] = (aa[i] + ba[i]);
if (r < k) {
cout << "-1\n";
return 0;
}
sort(ra, ra + r);
long long an = 0;
for (int i = 0; i < k; ++i) an += ra[i];
cout << an << '\n';
}
| 11 | CPP |
from sys import stdin, stdout
input = stdin.readline
print = stdout.write
n, k = map(int, input().split())
alice, bob, together = [], [], []
for _ in range(n):
t, a, b = map(int, input().split())
if a and b:
together += t,
elif a:
alice += t,
elif b:
bob += t,
if len(alice) + len(together) < k or len(bob) + len(together) < k:
print('-1')
exit()
alice.sort(reverse=True)
bob.sort(reverse=True)
together.sort(reverse=True)
time = 0
for i in range(k):
if len(alice) and len(bob) and len(together):
if together[-1] <= alice[-1] + bob[-1]:
time += together[-1]
together.pop()
else:
time += alice[-1] + bob[-1]
alice.pop()
bob.pop()
elif len(together):
time += together[-1]
together.pop()
else:
time += alice[-1] + bob[-1]
alice.pop()
bob.pop()
print(str(time)) | 11 | PYTHON3 |
#include <bits/stdc++.h>
using std::max;
using std::min;
const int N = 2e5 + 5, INF = 0x7fffffff;
struct book {
int id, val, a, b;
book() {}
book(int val, int a, int b) : val(val), a(a), b(b) {}
bool operator<(const book &rhs) const { return val < rhs.val; }
bool operator>(const book &rhs) const { return val > rhs.val; }
};
template <typename T, typename comp>
struct Heap {
T hp[N];
comp cm;
int cnt;
Heap() { cnt = 0; }
void push(T x) {
hp[cnt++] = x;
int p = cnt;
while (((p) >> 1) && cm(hp[p - 1], hp[((p) >> 1) - 1]))
std::swap(hp[((p) >> 1) - 1], hp[p - 1]), p = ((p) >> 1);
}
T pop() {
T res = hp[0], nr = hp[cnt - 1];
cnt--;
hp[0] = nr;
int p = 1;
while (((p) << 1) <= cnt) {
T mns = hp[((p) << 1) - 1];
int np = ((p) << 1);
if (((p) << 1 | 1) <= cnt && cm(hp[((p) << 1 | 1) - 1], mns))
mns = hp[(np = ((p) << 1 | 1)) - 1];
if (cm(mns, hp[p - 1]))
std::swap(hp[p - 1], hp[np - 1]), p = np;
else
break;
}
return res;
}
const T top() { return hp[0]; }
const int size() { return cnt; }
};
bool cmp(const book &x, const book &y) {
if (x.a != y.a) return x.a < y.a;
if (x.b != y.b) return x.b < y.b;
return x.val < y.val;
}
struct H {
Heap<book, std::greater<book> > tp;
Heap<book, std::less<book> > bt;
int tps, bts;
H() { tps = bts = 0; }
void push(book x) {
if (bt.size() && x > bt.top())
bt.push(x), bts += x.val;
else
tp.push(x), tps += x.val;
}
int ksum(int k) {
while (tp.size() < k)
tps += bt.top().val, bts -= bt.top().val, tp.push(bt.top()), bt.pop();
while (tp.size() > k)
bts += tp.top().val, tps -= tp.top().val, bt.push(tp.top()), tp.pop();
return tps;
}
const int size() { return tp.size() + bt.size(); }
};
int main() {
static book bk[N];
static H heap;
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++)
scanf("%d%d%d", &bk[i].val, &bk[i].a, &bk[i].b), bk[i].id = i;
std::sort(bk + 1, bk + 1 + n, cmp);
book *b0 = NULL, *b1 = NULL, *b2 = NULL, *b3 = NULL;
int n0 = 0, n1 = 0, n2 = 0, n3 = 0;
for (int i = 1; i <= n; i++) {
if (bk[i].a == 0 && bk[i].b == 0) n0++;
if (bk[i].a == 0 && bk[i].b == 1) n1++;
if (bk[i].a == 1 && bk[i].b == 0) n2++;
if (bk[i].a == 1 && bk[i].b == 1) n3++;
}
b0 = bk;
b1 = b0 + n0;
b2 = b1 + n1;
b3 = b2 + n2;
int n12 = min(n1, n2);
int sa = k, sb = 0;
bool flg = false;
for (int i = 1; i <= n3; i++) {
if (0 <= sa && sa <= n12 && 0 <= sb && sb <= n3 && sa * 2 + sb <= m &&
sa * 2 + sb <= n) {
flg = true;
break;
}
sa--;
sb++;
}
if (0 <= sa && sa <= n12 && 0 <= sb && sb <= n3 && sa * 2 + sb <= m &&
sa * 2 + sb <= n)
flg = true;
if (!flg) return printf("-1\n"), 0;
int vans = 0;
for (int i = 1; i <= sa; i++) vans += b1[i].val + b2[i].val;
for (int i = 1; i <= sb; i++) vans += b3[i].val;
for (int i = 1; i <= n0; i++) heap.push(b0[i]);
for (int i = sa + 1; i <= n1; i++) heap.push(b1[i]);
for (int i = sa + 1; i <= n2; i++) heap.push(b2[i]);
int ans = INF, ansi = 0;
if (heap.size() + sa * 2 + sb >= m)
ans = vans + heap.ksum(m - sa * 2 - sb), ansi = sb;
int sbb = sb;
for (int i = sb + 1, j = sa; i <= n3 && j >= 1; i++, j--) {
vans += b3[i].val - b1[j].val - b2[j].val;
heap.push(b1[j]);
heap.push(b2[j]);
if (heap.size() + (j - 1) * 2 + i >= m) {
int na = vans + heap.ksum(m - (j - 1) * 2 - i);
if (na < ans) ans = na, ansi = i;
}
sbb = i;
}
for (int i = sbb + 1; i <= n3; i++) {
vans += b3[i].val;
if (m >= i && heap.size() + i >= m) {
int na = vans + heap.ksum(m - i);
if (na < ans) ans = na, ansi = i;
}
}
printf("%d\n", ans);
int vv = 0;
for (int i = 1; i <= ansi; i++) printf("%d ", b3[i].id), vv++;
for (int i = 1; i <= max(0, k - ansi); i++)
printf("%d %d ", b1[i].id, b2[i].id), vv += 2;
static book tmp[N];
int pp = 0;
for (int i = 1; i <= n0; i++) tmp[++pp] = b0[i];
for (int i = max(0, k - ansi) + 1; i <= n1; i++) tmp[++pp] = b1[i];
for (int i = max(0, k - ansi) + 1; i <= n2; i++) tmp[++pp] = b2[i];
std::sort(tmp + 1, tmp + 1 + pp);
for (int i = 1; i <= m - vv; i++) printf("%d ", tmp[i].id);
return 0;
}
| 11 | CPP |
def main():
import sys
input = sys.stdin.buffer.readline
n, k = map(int, input().split())
tab = [tuple(map(int, input().split())) for i in range(n)]
tab.sort(reverse=True)
A = [tab[i][0] for i in range(n) if tab[i][1] == 1 and tab[i][2] == 0]
B = [tab[i][0] for i in range(n) if tab[i][1] == 0 and tab[i][2] == 1]
AB = [tab[i][0] for i in range(n) if tab[i][1] == tab[i][2] == 1]
count = 0
ans = 0
ab = 0
apb = 0
while(count < k):
if len(AB) > 0:
ab = AB[-1]
else:
ab = 10**10
if len(A) > 0 and len(B) > 0:
apb = A[-1] + B[-1]
else:
apb = 10**10
if ab < apb:
ans += ab
if len(AB):
AB.pop()
else:
ans += apb
if len(A):
A.pop()
if len(B):
B.pop()
count += 1
if ans >= 10**10:
print(-1)
else:
print(ans)
main()
| 11 | PYTHON3 |
# from mymodule import input
n,k = map(int, input().split())
li = [[list(),list()],[list(),list()]]
for i in range(n):
a,b,c = map(int, input().split())
li[b][c].append(a)
cnt = 0
itr1,itr2,itr3 = 0,0,0
li[0][1].sort()
li[1][0].sort()
li[1][1].sort()
ttl = 0
run = True
# print(li)
for i in range(k):
sm = 0
if itr1!=len(li[0][1]) and itr2!=len(li[1][0]):
sm = li[0][1][itr1]+li[1][0][itr2]
if itr3!=len(li[1][1]):
if sm<li[1][1][itr3]:
itr1+=1
itr2+=1
ttl+=sm
else:
ttl+=li[1][1][itr3]
itr3+=1
else:
itr1+=1
itr2+=1
ttl+=sm
elif itr3!=len(li[1][1]):
ttl+=li[1][1][itr3]
itr3+=1
else:
run = False
break
if run:
print(ttl)
else:
print(-1) | 11 | PYTHON3 |
n,k=map(int,input().split())
list1=[]
list2=[]
list3=[]
for i in range(n):
x,a,b=map(int,input().split())
if(a==1 and b==1):
list1.append(x)
elif(a==1):
list2.append(x)
elif(b==1):
list3.append(x)
# list1.sort()
list2.sort()
list3.sort()
for i in range(min(len(list2),len(list3))):
list1.append(list2[i]+list3[i])
list1.sort()
l=len(list1)
if(l<k):
print(-1)
else:
print(sum(list1[0:k]))
# kk=0 | 11 | PYTHON3 |
n,k=list(map(int,input().split()))
arr1=[]
arr2=[]
arr3=[]
x=0
y=0
for i in range(n):
num1=list(map(int,input().split()))
if num1[1]==1 and num1[2]==1:
arr3.append(num1[0])
x+=1
y+=1
elif num1[1]==1 and num1[2]==0:
arr1.append(num1[0])
x+=1
elif num1[1]==0 and num1[2]==1:
arr2.append(num1[0])
y+=1
if x<k or y<k:
print(-1)
else:
arr1.sort()
arr2.sort()
a=len(arr1)
b=len(arr2)
for i in range(min(a,b)):
arr3.append(arr1[i]+arr2[i])
arr3.sort()
ans=0
for i in range(k):
ans+=arr3[i]
print(ans)
| 11 | PYTHON3 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.