#include <bits/stdc++.h>
#pragma GCC optimize ("O2")
#pragma GCC optimize ("unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;
const ld eps=1e-7;
const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 60010, LOG=20;
int n, m, k, u, v, x, y, t, a, b, D;
int L[MAXN], R[MAXN], S[MAXN];
bool mark[MAXN];
set<pii> G[MAXN];
vector<int> knapsack;
bitset<MAXN*10> dp;
queue<int> Q;
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
cin>>n>>k;
for (int i=1; i<=2*n; i++){
cin>>L[i]>>R[i]>>S[i];
R[i]+=n;
G[L[i]].insert({R[i], i});
G[R[i]].insert({L[i], i});
}
for (int i=1; i<=2*n; i++) if (G[i].size()<2) Q.push(i);
while (Q.size()){
int v=Q.front();
Q.pop();
if (mark[v]) continue ;
mark[v]=1;
if (G[v].empty()) kill("NO")
pii p=*G[v].begin();
G[v].erase(p);
if (v<=n) D+=S[p.second];
else D-=S[p.second];
G[p.first].erase({v, p.second});
if (G[p.first].size()<2) Q.push(p.first);
}
//debug(D)
/*
if (abs(D)<=k) kill("YES")
kill("NO")*/
for (int i=1; i<=2*n; i++) if (G[i].size()){
int v=i, tmp=0, z=+1;
while (1){
pii p=*G[v].begin();
G[v].erase(p);
G[p.first].erase({v, p.second});
tmp+=z*S[p.second];
z*=-1;
v=p.first;
if (v==i) break ;
}
if (tmp<0) tmp=-tmp;
//debug(tmp)
D-=tmp;
knapsack.pb(tmp);
}
//debug(D)
//debugv(knapsack)
dp.set(0);
for (int x:knapsack) dp|=(dp<<x);
for (int i=0; i<MAXN*10; i++) if (dp[i] && abs(2*i+D)<=k) kill("YES")
kill("NO")
return 0;
}
/*
4 1
1 1 1
2 1 2
2 2 8
1 2 2
3 3 5
3 3 2
4 4 1
4 4 2
2 5
1 1 1
1 2 4
2 2 1
2 1 4
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3420 KB |
Output is correct |
2 |
Correct |
5 ms |
3320 KB |
Output is correct |
3 |
Correct |
6 ms |
3320 KB |
Output is correct |
4 |
Correct |
5 ms |
3320 KB |
Output is correct |
5 |
Correct |
6 ms |
3320 KB |
Output is correct |
6 |
Correct |
6 ms |
3320 KB |
Output is correct |
7 |
Correct |
5 ms |
3320 KB |
Output is correct |
8 |
Correct |
5 ms |
3320 KB |
Output is correct |
9 |
Correct |
5 ms |
3320 KB |
Output is correct |
10 |
Correct |
6 ms |
3320 KB |
Output is correct |
11 |
Correct |
5 ms |
3320 KB |
Output is correct |
12 |
Correct |
5 ms |
3320 KB |
Output is correct |
13 |
Correct |
5 ms |
3448 KB |
Output is correct |
14 |
Correct |
5 ms |
3320 KB |
Output is correct |
15 |
Correct |
5 ms |
3324 KB |
Output is correct |
16 |
Correct |
6 ms |
3320 KB |
Output is correct |
17 |
Correct |
5 ms |
3320 KB |
Output is correct |
18 |
Correct |
5 ms |
3324 KB |
Output is correct |
19 |
Correct |
5 ms |
3436 KB |
Output is correct |
20 |
Correct |
5 ms |
3344 KB |
Output is correct |
21 |
Correct |
5 ms |
3196 KB |
Output is correct |
22 |
Correct |
5 ms |
3320 KB |
Output is correct |
23 |
Correct |
6 ms |
3448 KB |
Output is correct |
24 |
Correct |
5 ms |
3448 KB |
Output is correct |
25 |
Correct |
6 ms |
3320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3420 KB |
Output is correct |
2 |
Correct |
5 ms |
3320 KB |
Output is correct |
3 |
Correct |
6 ms |
3320 KB |
Output is correct |
4 |
Correct |
5 ms |
3320 KB |
Output is correct |
5 |
Correct |
6 ms |
3320 KB |
Output is correct |
6 |
Correct |
6 ms |
3320 KB |
Output is correct |
7 |
Correct |
5 ms |
3320 KB |
Output is correct |
8 |
Correct |
5 ms |
3320 KB |
Output is correct |
9 |
Correct |
5 ms |
3320 KB |
Output is correct |
10 |
Correct |
6 ms |
3320 KB |
Output is correct |
11 |
Correct |
5 ms |
3320 KB |
Output is correct |
12 |
Correct |
5 ms |
3320 KB |
Output is correct |
13 |
Correct |
5 ms |
3448 KB |
Output is correct |
14 |
Correct |
5 ms |
3320 KB |
Output is correct |
15 |
Correct |
5 ms |
3324 KB |
Output is correct |
16 |
Correct |
6 ms |
3320 KB |
Output is correct |
17 |
Correct |
5 ms |
3320 KB |
Output is correct |
18 |
Correct |
5 ms |
3324 KB |
Output is correct |
19 |
Correct |
5 ms |
3436 KB |
Output is correct |
20 |
Correct |
5 ms |
3344 KB |
Output is correct |
21 |
Correct |
5 ms |
3196 KB |
Output is correct |
22 |
Correct |
5 ms |
3320 KB |
Output is correct |
23 |
Correct |
6 ms |
3448 KB |
Output is correct |
24 |
Correct |
5 ms |
3448 KB |
Output is correct |
25 |
Correct |
6 ms |
3320 KB |
Output is correct |
26 |
Correct |
74 ms |
3924 KB |
Output is correct |
27 |
Correct |
17 ms |
3832 KB |
Output is correct |
28 |
Correct |
73 ms |
3832 KB |
Output is correct |
29 |
Correct |
17 ms |
3960 KB |
Output is correct |
30 |
Correct |
75 ms |
3804 KB |
Output is correct |
31 |
Correct |
17 ms |
3832 KB |
Output is correct |
32 |
Correct |
73 ms |
3920 KB |
Output is correct |
33 |
Correct |
17 ms |
3832 KB |
Output is correct |
34 |
Correct |
12 ms |
3832 KB |
Output is correct |
35 |
Correct |
17 ms |
3832 KB |
Output is correct |
36 |
Correct |
73 ms |
3956 KB |
Output is correct |
37 |
Correct |
18 ms |
3960 KB |
Output is correct |
38 |
Correct |
75 ms |
3960 KB |
Output is correct |
39 |
Correct |
22 ms |
3960 KB |
Output is correct |
40 |
Correct |
72 ms |
3832 KB |
Output is correct |
41 |
Correct |
19 ms |
3832 KB |
Output is correct |
42 |
Correct |
76 ms |
3920 KB |
Output is correct |
43 |
Correct |
19 ms |
3932 KB |
Output is correct |
44 |
Correct |
78 ms |
3932 KB |
Output is correct |
45 |
Correct |
17 ms |
3832 KB |
Output is correct |
46 |
Correct |
73 ms |
3832 KB |
Output is correct |
47 |
Correct |
7 ms |
3704 KB |
Output is correct |
48 |
Correct |
41 ms |
3832 KB |
Output is correct |
49 |
Correct |
41 ms |
3832 KB |
Output is correct |
50 |
Correct |
89 ms |
3956 KB |
Output is correct |
51 |
Correct |
75 ms |
3832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
207 ms |
5880 KB |
Output is correct |
2 |
Correct |
17 ms |
5496 KB |
Output is correct |
3 |
Correct |
209 ms |
5880 KB |
Output is correct |
4 |
Correct |
18 ms |
5496 KB |
Output is correct |
5 |
Correct |
209 ms |
6004 KB |
Output is correct |
6 |
Correct |
17 ms |
5624 KB |
Output is correct |
7 |
Correct |
204 ms |
5880 KB |
Output is correct |
8 |
Correct |
17 ms |
5624 KB |
Output is correct |
9 |
Correct |
203 ms |
5888 KB |
Output is correct |
10 |
Correct |
16 ms |
5624 KB |
Output is correct |
11 |
Correct |
199 ms |
6008 KB |
Output is correct |
12 |
Correct |
17 ms |
5624 KB |
Output is correct |
13 |
Correct |
200 ms |
6008 KB |
Output is correct |
14 |
Correct |
203 ms |
5880 KB |
Output is correct |
15 |
Correct |
18 ms |
5624 KB |
Output is correct |
16 |
Correct |
230 ms |
6012 KB |
Output is correct |
17 |
Correct |
17 ms |
5624 KB |
Output is correct |
18 |
Correct |
198 ms |
6008 KB |
Output is correct |
19 |
Correct |
17 ms |
5496 KB |
Output is correct |
20 |
Correct |
217 ms |
5880 KB |
Output is correct |
21 |
Correct |
17 ms |
5624 KB |
Output is correct |
22 |
Correct |
111 ms |
5884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3420 KB |
Output is correct |
2 |
Correct |
5 ms |
3320 KB |
Output is correct |
3 |
Correct |
6 ms |
3320 KB |
Output is correct |
4 |
Correct |
5 ms |
3320 KB |
Output is correct |
5 |
Correct |
6 ms |
3320 KB |
Output is correct |
6 |
Correct |
6 ms |
3320 KB |
Output is correct |
7 |
Correct |
5 ms |
3320 KB |
Output is correct |
8 |
Correct |
5 ms |
3320 KB |
Output is correct |
9 |
Correct |
5 ms |
3320 KB |
Output is correct |
10 |
Correct |
6 ms |
3320 KB |
Output is correct |
11 |
Correct |
5 ms |
3320 KB |
Output is correct |
12 |
Correct |
5 ms |
3320 KB |
Output is correct |
13 |
Correct |
5 ms |
3448 KB |
Output is correct |
14 |
Correct |
5 ms |
3320 KB |
Output is correct |
15 |
Correct |
5 ms |
3324 KB |
Output is correct |
16 |
Correct |
6 ms |
3320 KB |
Output is correct |
17 |
Correct |
5 ms |
3320 KB |
Output is correct |
18 |
Correct |
5 ms |
3324 KB |
Output is correct |
19 |
Correct |
5 ms |
3436 KB |
Output is correct |
20 |
Correct |
5 ms |
3344 KB |
Output is correct |
21 |
Correct |
5 ms |
3196 KB |
Output is correct |
22 |
Correct |
5 ms |
3320 KB |
Output is correct |
23 |
Correct |
6 ms |
3448 KB |
Output is correct |
24 |
Correct |
5 ms |
3448 KB |
Output is correct |
25 |
Correct |
6 ms |
3320 KB |
Output is correct |
26 |
Correct |
74 ms |
3924 KB |
Output is correct |
27 |
Correct |
17 ms |
3832 KB |
Output is correct |
28 |
Correct |
73 ms |
3832 KB |
Output is correct |
29 |
Correct |
17 ms |
3960 KB |
Output is correct |
30 |
Correct |
75 ms |
3804 KB |
Output is correct |
31 |
Correct |
17 ms |
3832 KB |
Output is correct |
32 |
Correct |
73 ms |
3920 KB |
Output is correct |
33 |
Correct |
17 ms |
3832 KB |
Output is correct |
34 |
Correct |
12 ms |
3832 KB |
Output is correct |
35 |
Correct |
17 ms |
3832 KB |
Output is correct |
36 |
Correct |
73 ms |
3956 KB |
Output is correct |
37 |
Correct |
18 ms |
3960 KB |
Output is correct |
38 |
Correct |
75 ms |
3960 KB |
Output is correct |
39 |
Correct |
22 ms |
3960 KB |
Output is correct |
40 |
Correct |
72 ms |
3832 KB |
Output is correct |
41 |
Correct |
19 ms |
3832 KB |
Output is correct |
42 |
Correct |
76 ms |
3920 KB |
Output is correct |
43 |
Correct |
19 ms |
3932 KB |
Output is correct |
44 |
Correct |
78 ms |
3932 KB |
Output is correct |
45 |
Correct |
17 ms |
3832 KB |
Output is correct |
46 |
Correct |
73 ms |
3832 KB |
Output is correct |
47 |
Correct |
7 ms |
3704 KB |
Output is correct |
48 |
Correct |
41 ms |
3832 KB |
Output is correct |
49 |
Correct |
41 ms |
3832 KB |
Output is correct |
50 |
Correct |
89 ms |
3956 KB |
Output is correct |
51 |
Correct |
75 ms |
3832 KB |
Output is correct |
52 |
Correct |
207 ms |
5880 KB |
Output is correct |
53 |
Correct |
17 ms |
5496 KB |
Output is correct |
54 |
Correct |
209 ms |
5880 KB |
Output is correct |
55 |
Correct |
18 ms |
5496 KB |
Output is correct |
56 |
Correct |
209 ms |
6004 KB |
Output is correct |
57 |
Correct |
17 ms |
5624 KB |
Output is correct |
58 |
Correct |
204 ms |
5880 KB |
Output is correct |
59 |
Correct |
17 ms |
5624 KB |
Output is correct |
60 |
Correct |
203 ms |
5888 KB |
Output is correct |
61 |
Correct |
16 ms |
5624 KB |
Output is correct |
62 |
Correct |
199 ms |
6008 KB |
Output is correct |
63 |
Correct |
17 ms |
5624 KB |
Output is correct |
64 |
Correct |
200 ms |
6008 KB |
Output is correct |
65 |
Correct |
203 ms |
5880 KB |
Output is correct |
66 |
Correct |
18 ms |
5624 KB |
Output is correct |
67 |
Correct |
230 ms |
6012 KB |
Output is correct |
68 |
Correct |
17 ms |
5624 KB |
Output is correct |
69 |
Correct |
198 ms |
6008 KB |
Output is correct |
70 |
Correct |
17 ms |
5496 KB |
Output is correct |
71 |
Correct |
217 ms |
5880 KB |
Output is correct |
72 |
Correct |
17 ms |
5624 KB |
Output is correct |
73 |
Correct |
111 ms |
5884 KB |
Output is correct |
74 |
Correct |
1014 ms |
10816 KB |
Output is correct |
75 |
Correct |
102 ms |
10572 KB |
Output is correct |
76 |
Correct |
1014 ms |
10872 KB |
Output is correct |
77 |
Correct |
122 ms |
10488 KB |
Output is correct |
78 |
Correct |
1016 ms |
10760 KB |
Output is correct |
79 |
Correct |
1027 ms |
10724 KB |
Output is correct |
80 |
Correct |
100 ms |
10488 KB |
Output is correct |
81 |
Correct |
100 ms |
10632 KB |
Output is correct |
82 |
Correct |
1029 ms |
10588 KB |
Output is correct |
83 |
Correct |
1031 ms |
10860 KB |
Output is correct |
84 |
Correct |
94 ms |
10616 KB |
Output is correct |
85 |
Correct |
1030 ms |
10744 KB |
Output is correct |
86 |
Correct |
91 ms |
10492 KB |
Output is correct |
87 |
Correct |
1012 ms |
10784 KB |
Output is correct |
88 |
Correct |
100 ms |
10492 KB |
Output is correct |
89 |
Correct |
1014 ms |
10872 KB |
Output is correct |
90 |
Correct |
134 ms |
10492 KB |
Output is correct |
91 |
Correct |
1010 ms |
10744 KB |
Output is correct |
92 |
Correct |
103 ms |
10576 KB |
Output is correct |
93 |
Correct |
1004 ms |
10720 KB |
Output is correct |
94 |
Correct |
52 ms |
10360 KB |
Output is correct |
95 |
Correct |
547 ms |
10764 KB |
Output is correct |
96 |
Correct |
546 ms |
10876 KB |
Output is correct |
97 |
Correct |
1025 ms |
10744 KB |
Output is correct |
98 |
Correct |
1041 ms |
10796 KB |
Output is correct |