#include <bits/stdc++.h>
using namespace std;
int const N=30005;
set<int> mem[2*N];
set<pair<int,int>> sz;
int L[2*N],R[2*N],S[2*N];
bool done[2*N];
bool dp[100000];
bool tmdp[100000];
bool check(vector<int> ds,int k,int s1,int s2){
dp[0]=1;
for(auto i:ds){
for(int j=0;j<40005;j++)
tmdp[j]=0;
for(int j=0;j<40005;j++){
if(dp[j]){
tmdp[j+i]=1;
tmdp[abs(j-i)]=1;
}
}
for(int j=0;j<40005;j++)
dp[j]=tmdp[j];
}
for(int i=0;i<=40005;i++)
if(dp[i] && (abs((i+s1)-s2)<=k || abs((i+s2)-s1)<=k))
return 1;
return 0;
}
int main()
{
int n,k;
cin>>n>>k;
for(int i=1;i<=2*n;i++){
int s,l,r;
cin>>l>>r>>s;
r+=n;
mem[l].insert(i);
mem[r].insert(i);
L[i]=l;
R[i]=r;
S[i]=s;
}
for(int i=1;i<=2*n;i++)
sz.insert({mem[i].size(),i});
int sum1=0,sum2=0;
while(sz.size()){
auto p=*(sz.begin());
int job=p.second;
int cons=p.first;
// cout<<job<<' '<<cons<<endl;
if(cons==2)
break;
sz.erase(sz.begin());
if(cons==0){
cout<<"NO"<<endl;
return 0;
}
else if(cons==1){
int per=*(mem[job].begin());
if(job<=n)
sum1+=S[per];
else
sum2+=S[per];
// cout<<per<<' '<<L[per]<<' '<<R[per]<<endl;
sz.erase({mem[L[per]].size(),L[per]});
sz.erase({mem[R[per]].size(),R[per]});
mem[L[per]].erase(per);
mem[R[per]].erase(per);
if(done[L[per]]==0 && L[per]!=job)
sz.insert({mem[L[per]].size(),L[per]});
if(done[R[per]]==0 && R[per]!=job)
sz.insert({mem[R[per]].size(),R[per]});
done[job]=1;
}
else
break;
}
//now all are with degree two.
vector<int> ds;
int temp=0;
while(sz.size()){
auto p=*(sz.begin());
int job=p.second;
int cons=p.first;
// cout<<job<<' '<<cons<<endl;
sz.erase(sz.begin());
if(cons==2 && temp!=0){
ds.push_back(abs(temp));
temp=0;
}
int per=*(mem[job].begin());
if(job<=n)
temp+=S[per];
else
temp-=S[per];
// cout<<per<<' '<<L[per]<<' '<<R[per]<<endl;
sz.erase({mem[L[per]].size(),L[per]});
sz.erase({mem[R[per]].size(),R[per]});
mem[L[per]].erase(per);
mem[R[per]].erase(per);
if(done[L[per]]==0 && L[per]!=job)
sz.insert({mem[L[per]].size(),L[per]});
if(done[R[per]]==0 && R[per]!=job)
sz.insert({mem[R[per]].size(),R[per]});
done[job]=1;
}
if(temp!=0)
ds.push_back(abs(temp));
// make a sum <= k from ds;
if(check(ds,k,sum1,sum2))
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3932 KB |
Output is correct |
2 |
Correct |
2 ms |
3932 KB |
Output is correct |
3 |
Correct |
1 ms |
3932 KB |
Output is correct |
4 |
Correct |
1 ms |
3932 KB |
Output is correct |
5 |
Correct |
1 ms |
3932 KB |
Output is correct |
6 |
Correct |
1 ms |
3932 KB |
Output is correct |
7 |
Correct |
1 ms |
3932 KB |
Output is correct |
8 |
Correct |
2 ms |
4132 KB |
Output is correct |
9 |
Correct |
1 ms |
3932 KB |
Output is correct |
10 |
Correct |
1 ms |
4180 KB |
Output is correct |
11 |
Correct |
2 ms |
3928 KB |
Output is correct |
12 |
Correct |
2 ms |
3928 KB |
Output is correct |
13 |
Correct |
2 ms |
4088 KB |
Output is correct |
14 |
Correct |
2 ms |
3932 KB |
Output is correct |
15 |
Correct |
2 ms |
3932 KB |
Output is correct |
16 |
Correct |
1 ms |
3932 KB |
Output is correct |
17 |
Correct |
1 ms |
3932 KB |
Output is correct |
18 |
Correct |
2 ms |
3932 KB |
Output is correct |
19 |
Correct |
2 ms |
3932 KB |
Output is correct |
20 |
Correct |
1 ms |
3932 KB |
Output is correct |
21 |
Correct |
1 ms |
3932 KB |
Output is correct |
22 |
Correct |
1 ms |
3932 KB |
Output is correct |
23 |
Correct |
2 ms |
3928 KB |
Output is correct |
24 |
Correct |
1 ms |
3932 KB |
Output is correct |
25 |
Correct |
2 ms |
3932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3932 KB |
Output is correct |
2 |
Correct |
2 ms |
3932 KB |
Output is correct |
3 |
Correct |
1 ms |
3932 KB |
Output is correct |
4 |
Correct |
1 ms |
3932 KB |
Output is correct |
5 |
Correct |
1 ms |
3932 KB |
Output is correct |
6 |
Correct |
1 ms |
3932 KB |
Output is correct |
7 |
Correct |
1 ms |
3932 KB |
Output is correct |
8 |
Correct |
2 ms |
4132 KB |
Output is correct |
9 |
Correct |
1 ms |
3932 KB |
Output is correct |
10 |
Correct |
1 ms |
4180 KB |
Output is correct |
11 |
Correct |
2 ms |
3928 KB |
Output is correct |
12 |
Correct |
2 ms |
3928 KB |
Output is correct |
13 |
Correct |
2 ms |
4088 KB |
Output is correct |
14 |
Correct |
2 ms |
3932 KB |
Output is correct |
15 |
Correct |
2 ms |
3932 KB |
Output is correct |
16 |
Correct |
1 ms |
3932 KB |
Output is correct |
17 |
Correct |
1 ms |
3932 KB |
Output is correct |
18 |
Correct |
2 ms |
3932 KB |
Output is correct |
19 |
Correct |
2 ms |
3932 KB |
Output is correct |
20 |
Correct |
1 ms |
3932 KB |
Output is correct |
21 |
Correct |
1 ms |
3932 KB |
Output is correct |
22 |
Correct |
1 ms |
3932 KB |
Output is correct |
23 |
Correct |
2 ms |
3928 KB |
Output is correct |
24 |
Correct |
1 ms |
3932 KB |
Output is correct |
25 |
Correct |
2 ms |
3932 KB |
Output is correct |
26 |
Correct |
57 ms |
4668 KB |
Output is correct |
27 |
Correct |
14 ms |
4696 KB |
Output is correct |
28 |
Correct |
57 ms |
4444 KB |
Output is correct |
29 |
Correct |
15 ms |
4444 KB |
Output is correct |
30 |
Correct |
60 ms |
4668 KB |
Output is correct |
31 |
Correct |
13 ms |
4444 KB |
Output is correct |
32 |
Correct |
64 ms |
4444 KB |
Output is correct |
33 |
Correct |
13 ms |
4900 KB |
Output is correct |
34 |
Correct |
9 ms |
4444 KB |
Output is correct |
35 |
Correct |
16 ms |
4696 KB |
Output is correct |
36 |
Correct |
57 ms |
4444 KB |
Output is correct |
37 |
Correct |
19 ms |
4696 KB |
Output is correct |
38 |
Correct |
60 ms |
4444 KB |
Output is correct |
39 |
Correct |
13 ms |
4444 KB |
Output is correct |
40 |
Correct |
56 ms |
4444 KB |
Output is correct |
41 |
Correct |
13 ms |
4444 KB |
Output is correct |
42 |
Correct |
62 ms |
4672 KB |
Output is correct |
43 |
Correct |
16 ms |
4952 KB |
Output is correct |
44 |
Correct |
66 ms |
4668 KB |
Output is correct |
45 |
Correct |
13 ms |
4444 KB |
Output is correct |
46 |
Correct |
57 ms |
4444 KB |
Output is correct |
47 |
Correct |
4 ms |
4440 KB |
Output is correct |
48 |
Correct |
31 ms |
4444 KB |
Output is correct |
49 |
Correct |
31 ms |
4440 KB |
Output is correct |
50 |
Correct |
66 ms |
4444 KB |
Output is correct |
51 |
Correct |
68 ms |
4672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
6748 KB |
Output is correct |
2 |
Correct |
16 ms |
6748 KB |
Output is correct |
3 |
Correct |
20 ms |
6748 KB |
Output is correct |
4 |
Correct |
16 ms |
6732 KB |
Output is correct |
5 |
Correct |
19 ms |
6748 KB |
Output is correct |
6 |
Correct |
16 ms |
6748 KB |
Output is correct |
7 |
Correct |
19 ms |
6748 KB |
Output is correct |
8 |
Correct |
18 ms |
6764 KB |
Output is correct |
9 |
Correct |
19 ms |
6748 KB |
Output is correct |
10 |
Correct |
16 ms |
6668 KB |
Output is correct |
11 |
Correct |
19 ms |
6744 KB |
Output is correct |
12 |
Correct |
18 ms |
6748 KB |
Output is correct |
13 |
Correct |
19 ms |
6744 KB |
Output is correct |
14 |
Correct |
19 ms |
6660 KB |
Output is correct |
15 |
Correct |
16 ms |
6872 KB |
Output is correct |
16 |
Correct |
20 ms |
6748 KB |
Output is correct |
17 |
Correct |
16 ms |
6800 KB |
Output is correct |
18 |
Correct |
21 ms |
6724 KB |
Output is correct |
19 |
Correct |
18 ms |
7000 KB |
Output is correct |
20 |
Correct |
19 ms |
6744 KB |
Output is correct |
21 |
Correct |
16 ms |
6748 KB |
Output is correct |
22 |
Correct |
23 ms |
6692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3932 KB |
Output is correct |
2 |
Correct |
2 ms |
3932 KB |
Output is correct |
3 |
Correct |
1 ms |
3932 KB |
Output is correct |
4 |
Correct |
1 ms |
3932 KB |
Output is correct |
5 |
Correct |
1 ms |
3932 KB |
Output is correct |
6 |
Correct |
1 ms |
3932 KB |
Output is correct |
7 |
Correct |
1 ms |
3932 KB |
Output is correct |
8 |
Correct |
2 ms |
4132 KB |
Output is correct |
9 |
Correct |
1 ms |
3932 KB |
Output is correct |
10 |
Correct |
1 ms |
4180 KB |
Output is correct |
11 |
Correct |
2 ms |
3928 KB |
Output is correct |
12 |
Correct |
2 ms |
3928 KB |
Output is correct |
13 |
Correct |
2 ms |
4088 KB |
Output is correct |
14 |
Correct |
2 ms |
3932 KB |
Output is correct |
15 |
Correct |
2 ms |
3932 KB |
Output is correct |
16 |
Correct |
1 ms |
3932 KB |
Output is correct |
17 |
Correct |
1 ms |
3932 KB |
Output is correct |
18 |
Correct |
2 ms |
3932 KB |
Output is correct |
19 |
Correct |
2 ms |
3932 KB |
Output is correct |
20 |
Correct |
1 ms |
3932 KB |
Output is correct |
21 |
Correct |
1 ms |
3932 KB |
Output is correct |
22 |
Correct |
1 ms |
3932 KB |
Output is correct |
23 |
Correct |
2 ms |
3928 KB |
Output is correct |
24 |
Correct |
1 ms |
3932 KB |
Output is correct |
25 |
Correct |
2 ms |
3932 KB |
Output is correct |
26 |
Correct |
57 ms |
4668 KB |
Output is correct |
27 |
Correct |
14 ms |
4696 KB |
Output is correct |
28 |
Correct |
57 ms |
4444 KB |
Output is correct |
29 |
Correct |
15 ms |
4444 KB |
Output is correct |
30 |
Correct |
60 ms |
4668 KB |
Output is correct |
31 |
Correct |
13 ms |
4444 KB |
Output is correct |
32 |
Correct |
64 ms |
4444 KB |
Output is correct |
33 |
Correct |
13 ms |
4900 KB |
Output is correct |
34 |
Correct |
9 ms |
4444 KB |
Output is correct |
35 |
Correct |
16 ms |
4696 KB |
Output is correct |
36 |
Correct |
57 ms |
4444 KB |
Output is correct |
37 |
Correct |
19 ms |
4696 KB |
Output is correct |
38 |
Correct |
60 ms |
4444 KB |
Output is correct |
39 |
Correct |
13 ms |
4444 KB |
Output is correct |
40 |
Correct |
56 ms |
4444 KB |
Output is correct |
41 |
Correct |
13 ms |
4444 KB |
Output is correct |
42 |
Correct |
62 ms |
4672 KB |
Output is correct |
43 |
Correct |
16 ms |
4952 KB |
Output is correct |
44 |
Correct |
66 ms |
4668 KB |
Output is correct |
45 |
Correct |
13 ms |
4444 KB |
Output is correct |
46 |
Correct |
57 ms |
4444 KB |
Output is correct |
47 |
Correct |
4 ms |
4440 KB |
Output is correct |
48 |
Correct |
31 ms |
4444 KB |
Output is correct |
49 |
Correct |
31 ms |
4440 KB |
Output is correct |
50 |
Correct |
66 ms |
4444 KB |
Output is correct |
51 |
Correct |
68 ms |
4672 KB |
Output is correct |
52 |
Correct |
20 ms |
6748 KB |
Output is correct |
53 |
Correct |
16 ms |
6748 KB |
Output is correct |
54 |
Correct |
20 ms |
6748 KB |
Output is correct |
55 |
Correct |
16 ms |
6732 KB |
Output is correct |
56 |
Correct |
19 ms |
6748 KB |
Output is correct |
57 |
Correct |
16 ms |
6748 KB |
Output is correct |
58 |
Correct |
19 ms |
6748 KB |
Output is correct |
59 |
Correct |
18 ms |
6764 KB |
Output is correct |
60 |
Correct |
19 ms |
6748 KB |
Output is correct |
61 |
Correct |
16 ms |
6668 KB |
Output is correct |
62 |
Correct |
19 ms |
6744 KB |
Output is correct |
63 |
Correct |
18 ms |
6748 KB |
Output is correct |
64 |
Correct |
19 ms |
6744 KB |
Output is correct |
65 |
Correct |
19 ms |
6660 KB |
Output is correct |
66 |
Correct |
16 ms |
6872 KB |
Output is correct |
67 |
Correct |
20 ms |
6748 KB |
Output is correct |
68 |
Correct |
16 ms |
6800 KB |
Output is correct |
69 |
Correct |
21 ms |
6724 KB |
Output is correct |
70 |
Correct |
18 ms |
7000 KB |
Output is correct |
71 |
Correct |
19 ms |
6744 KB |
Output is correct |
72 |
Correct |
16 ms |
6748 KB |
Output is correct |
73 |
Correct |
23 ms |
6692 KB |
Output is correct |
74 |
Correct |
1020 ms |
12908 KB |
Output is correct |
75 |
Correct |
111 ms |
13140 KB |
Output is correct |
76 |
Correct |
1027 ms |
13568 KB |
Output is correct |
77 |
Correct |
110 ms |
13284 KB |
Output is correct |
78 |
Correct |
1070 ms |
13764 KB |
Output is correct |
79 |
Correct |
1026 ms |
13648 KB |
Output is correct |
80 |
Correct |
111 ms |
13140 KB |
Output is correct |
81 |
Correct |
115 ms |
13288 KB |
Output is correct |
82 |
Correct |
1080 ms |
13136 KB |
Output is correct |
83 |
Correct |
1030 ms |
13564 KB |
Output is correct |
84 |
Correct |
117 ms |
13124 KB |
Output is correct |
85 |
Correct |
1032 ms |
13564 KB |
Output is correct |
86 |
Correct |
117 ms |
13256 KB |
Output is correct |
87 |
Correct |
1018 ms |
13560 KB |
Output is correct |
88 |
Correct |
111 ms |
13284 KB |
Output is correct |
89 |
Correct |
1044 ms |
13560 KB |
Output is correct |
90 |
Correct |
128 ms |
13624 KB |
Output is correct |
91 |
Correct |
1029 ms |
13568 KB |
Output is correct |
92 |
Correct |
117 ms |
13136 KB |
Output is correct |
93 |
Correct |
1074 ms |
13344 KB |
Output is correct |
94 |
Correct |
71 ms |
13140 KB |
Output is correct |
95 |
Incorrect |
544 ms |
13488 KB |
Output isn't correct |
96 |
Halted |
0 ms |
0 KB |
- |