#P4733. Tug of War
Tug of War
Tug of War
The tug of war is a very popular sport in Byteland. In the annual Byteland tug of war competition, there are a total of \(2n\) contestants. They are to be divided into two teams of equal size \(n\). A rope is marked with \(n\) positions on the left side and \(n\) positions on the right side. Every contestant has a desired position on the left side and a desired position on the right side, and each contestant also has a strength value.
The contest rules are as follows:
- The two teams must each have exactly \(n\) members.
- If a contestant is assigned to the left team, he occupies his desired left position; if he is assigned to the right team, he occupies his desired right position. No two contestants in the same team can occupy the same position.
- If the strength sums of the two teams are \(S_{L}\) and \(S_{R}\), the competition can proceed only if \(|S_{L} - S_{R}| \le k\), where \(k\) is a given integer.
Your task is to determine whether it is possible to divide the \(2n\) contestants into two teams that satisfy the above conditions.
inputFormat
The first line of input contains two integers \(n\) and \(k\) (with \(1 \le n\le [constraint]\)). Each of the next \(2n\) lines contains three integers: \(s\), \(l\), and \(r\), where \(s\) is the strength of a contestant, \(l\) is the desired left position (an integer between 1 and \(n\)), and \(r\) is the desired right position (an integer between 1 and \(n\)).
outputFormat
Output a single line containing either YES
if such a division is possible, or NO
otherwise.
sample
1 0
5 1 1
5 1 1
YES