Tug of War
Time limit3sMemory limit256 MB
Decide if 2n contestants, each with one left spot, one right spot, and a strength, split into two disjoint teams of n whose strength sums differ by at most k.
- Level
Medium7 of 10
- Topics
- Graph, Dynamic programming
- Solved
- No attempts yet
Problem
Tug of war is a very popular sport in Byteland. The rules are simple: two teams pull one rope in opposite directions. The annual Byteland charity tug of war is about to start, and a lot of contestants have signed up. As the fair play commissioner, your job is to split the contestants into two teams so that the game goes on for a long time.
A total of contestants signed up, so each team has contestants. The rope has spots on the left side and spots on the right side. The tug of war elite of Byteland are a picky bunch: each contestant has exactly one spot on the left side and one spot on the right side that he or she wants to use. You also know the strength of every contestant.
The organizer fixes an integer and asks you the following. Is it possible to create two teams such that each team has contestants, every contestant uses one of the two spots he or she wants (no two contestants share a spot), and the sums of strengths of the two teams differ by at most ?
Input
The first line contains a positive integer , the number of spots on each side of the rope, and an integer , the largest allowed difference of the teams' strengths (, ). The contestants are numbered from 1 to .
Each of the following lines describes one contestant. The -th of these lines contains three positive integers , and (, ). Contestant has strength and may use only spot on the left side of the rope or spot on the right side.
Output
Print YES on the first and only line if two teams satisfying all of the requirements above can be created, and NO otherwise.
Hint
In the first example you can put contestants 1, 3, 6 and 7 on the left side (strength ) and contestants 2, 4, 5 and 8 on the right side (strength ). The difference of the team strengths is 1.
In the second example both contestants of strength 4 end up on the same team no matter what, so the smallest possible difference of the team strengths is 6.