-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE1.cpp
More file actions
85 lines (85 loc) · 1.99 KB
/
E1.cpp
File metadata and controls
85 lines (85 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "bits/stdc++.h"
using namespace std;
int n,k;
priority_queue<long long,vector<long long>,greater<>>pq1,pq2,pq12;
signed main() {
cin>>n>>k;
for(int i = 0 ; i <n;i++){
int x,y,z;
cin>>x>>y>>z;
if(y==0 and z==1)pq2.push(x);
if(y==1 and z==0)pq1.push(x);
if(y==1 and z==1)pq12.push(x);
}
pq1.push(1e11+1);
pq12.push(1e11+1);
pq2.push(1e11+1);
long long int p1=0,p2=0,ans=0;
while(p1<k or p2<k){
if(ans>(long long)1e10 or ans<0){
cout<<-1<<endl;return 0;
}
long long int x=pq1.top();
long long int y = pq2.top();
long long int z = pq12.top();
if(p1<k)
{
if(p2<k)
{
if(x+y<z)
{
p1++;
p2++;
ans+=x+y;
pq1.pop();
pq2.pop();
}
else
{
p1++;
p2++;
ans+=z;
pq12.pop();
}
}
else
{
if(x<z)
{
p1++;
p2++;
ans+=x;
pq1.pop();
}
else
{
p1++;
p2++;
ans+=z;
pq12.pop();
}
}
}
else if(p2<k)
{
if(y<z)
{
p1++;
p2++;
ans+=y;
pq1.pop();
}
else
{
p1++;
p2++;
ans+=z;
pq12.pop();
}
}
}
if(ans>(long long)1e10 or ans<0){
cout<<-1<<endl;return 0;
}
cout<<ans<<endl;
}