For the Peace
Time limit8sMemory limit512 MB
Decide whether n countries can each discard their missiles oldest-first so the gap between the largest and smallest remaining war potential never exceeds d.
- Level
Medium5 of 10
- Topics
- Greedy, Implementation
- Solved
- No attempts yet
Problem
This is a story about a world far from Earth. The land of that world is divided into countries ruled by empires, and those countries have spent a long time in an arms race.
The race is about missile production. Even so, no country has started a war for years. They cannot fight: they hold far more missiles than it takes to destroy the entire world, so once a war began among them, none of them would remain.
The missiles gave people nothing but fear. The race put heavy financial and psychological pressure on every country. The people are tired, the military is tired, and even the empires are tired. Nobody wants to keep producing missiles.
So the empires and the diplomats of every country met many times about renouncing their missiles and stopping further production. The countries had different interests and the talks were hard, but in the end they agreed on a treaty with these points:
- Each country disposes of every missile it owns by a certain date.
- The war potential of any two countries never differs by more than .
Here are the details of the second point. Each missile has a capability, which is how much it can destroy its target. The war potential of a country is the sum of the capabilities of the missiles it still owns. The treaty requires the difference between the maximum and the minimum war potential over all countries to stay at most at every moment.
Missiles are disposed of one at a time, and the treaty condition is checked after each disposal. Each country disposes of its missiles only in the order they were produced, from the oldest to the newest. Some missiles have a huge capability, and disposing of one of them can throw the potentials out of balance.
Write a program that decides whether every country can dispose of all of its missiles without ever breaking the treaty.
Input
The input is a sequence of datasets. Each dataset has this format:
n d
m1 c1,1 ... c1,m1
...
mn cn,1 ... cn,mn
The first line holds two positive integers, the number of countries and the tolerated difference of potential , with and . Then lines follow. The -th line begins with a non-negative integer , the number of missiles the -th country owns, followed by positive integers. The -th of them, , is the capability of the -th newest missile of the -th country, with . The integers on a line are separated by a single space. Each country disposes of its missiles in the reverse of the given order.
One dataset holds at most 10000 missiles in total. You may also assume that in the initial state of every dataset the difference between the maximum and the minimum potential is at most .
The input ends with a line holding two zeros. Do not process that line.
Output
Print a single line for each dataset. Print Yes if every country can dispose of all of its missiles under the treaty, and No otherwise.
Judging is case sensitive. No extra space or character is allowed.