Problem:
Problem B: For the Peace

This is a story of a world somewhere far from the earth. In this world, the land is parted into a
number of countries ruled by empires. This world is not very peaceful: they have been involved
in army race.

They are competing in production of missiles in particular. Nevertheless, no countries have
started wars for years. Actually they have a reason they can’t get into wars - they have
missiles much more than enough to destroy the entire world. Once a war would begin among
countries, none of them could remain.

These missiles have given nothing but scare to people. The competition has caused big financial
and psychological pressure to countries. People have been tired. Military have been tired. Even
empires have been tired. No one wishes to keep on missile production.

So empires and diplomats of all countries held meetings quite a few times toward renouncement
of missiles and abandon of further production. The meetings were quite difficult as they have
different matters. However, they overcame such difficulties and finally came to the agreement
of a treaty. The points include:

Each country will dispose all the missiles of their possession by a certain date.

The war potential should not be different by greater than a certain amount d among all
countries.

Let us describe more details on the second point. Each missile has its capability, which represents
how much it can destroy the target. The war potential of each country is measured simply by
the sum of capability over missiles possessed by that country. The treaty requires the difference
to be not greater than d between the maximum and minimum potential of all the countries.

Unfortunately, it is not clear whether this treaty is feasible. Every country is going to dispose
their missiles only in the order of time they were produced, from the oldest to the newest. Some
missiles have huge capability, and disposal of them may cause unbalance in potential.

Your task is to write a program to see this feasibility.

Input

The input is a sequence of datasets. Each dataset is given in the following format:

n d

m1 c1,1 ... c1,m1

...

mn cn,1 ... cn,mn

The first line contains two positive integers n and d, the number of countries and the tolerated
difference of potential (n ≤ 100, d ≤ 1000). Then n lines follow. The i-th line begins with a
non-negative integer mi, the number of the missiles possessed by the i-th country. It is followed
by a sequence of mi positive integers. The j-th integer ci,j represents the capability of the j-th
newest missile of the i-th country (ci,j ≤ 1000). These integers are separated by a single space.
Note that the country disposes their missiles in the reverse order of the given sequence.

The number of missiles is not greater than 10000. Also, you may assume the difference between
the maximum and minimum potential does not exceed d in any dataset.

The input is terminated by a line with two zeros. This line should not be processed.

Output

For each dataset, print a single line. If they can dispose all their missiles according to the treaty,
print "Yes" (without quotes). Otherwise, print "No".

Note that the judge is performed in a case-sensitive manner. No extra space or character is
allowed.

Sample Input

3 3
3 4 1 1
2 1 5
2 3 3
3 3
3 2 3 1
2 1 5
2 3 3
0 0

Output for the Sample Input

Yes
No

Solution in C++ (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define each(itr,v) for(auto itr:v)
#define pb(s) push_back(s)
#define mp(a,b) make_pair(a,b)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define maxch(x,y) x=max(x,y)
#define minch(x,y) x=min(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;

#define INF INT_MAX/3

#define MAX_N 1000


//// RangeMinimum(Segtree) (0-indexed)
struct RangeMinQuery{
	int dat[2*MAX_N-1];// (ex. MAX_N=1<<17
	int size;

	void init(int n_){
		size=1;
		while(size<n_) size*=2;
		for(int i=0;i<2*size-1;i++)dat[i]=INT_MAX;
	}

	void update(int k,int a){
		k+=size-1;
		dat[k]=a;
		while(k>0){
			k=(k-1)/2;
			dat[k]=min(dat[k*2+1],dat[k*2+2]);
		}
	}

	int subquery(int a,int b,int k,int l,int r){
		if(r<=a||b<=l)return INT_MAX;
		if(a<=l&&r<=b)return dat[k];
		else{
			return min(subquery(a,b,k*2+1,l,(l+r)/2),subquery(a,b,k*2+2,(l+r)/2,r));
		}
	}
	
	int query(int a,int b){
		return subquery(a,b,0,0,size);
	}
};

//// RangeMaximum(Segtree) (0-indexed)
struct RangeMaxQuery{
	int dat[2*MAX_N-1];// (ex. MAX_N=1<<17
	int size;

	void init(int n_){
		size=1;
		while(size<n_)size*=2;
		for(int i=0;i<2*size-1;i++)dat[i]=0;
	}

	void update(int k,int a){
		k+=size-1;
		dat[k]=a;
		while(k>0){
			k=(k-1)/2;
			dat[k]=max(dat[k*2+1],dat[k*2+2]);
		}
	}

	int subquery(int a,int b,int k,int l,int r){
		if(r<=a||b<=l)return 0;
		if(a<=l&&r<=b)return dat[k];
		else{
			return max(subquery(a,b,k*2+1,l,(l+r)/2),subquery(a,b,k*2+2,(l+r)/2,r));
		}
	}
	int query(int a,int b){
		return subquery(a,b,0,0,size);
	}
};

RangeMaxQuery rmaxq;
RangeMinQuery rminq;

int n,d;
int m[111];
int c[111][1111];

int main(){
	cin.sync_with_stdio(false);
	while(1){
		cin>>n>>d;
		if(n==0)break;
		rminq.init(n);
		rmaxq.init(n);
		int mcnt=0;
		rep(i,n){
			cin>>m[i];
			c[i][0]=0;
			int sum=0;
			mcnt+=m[i];
			repl(j,1,m[i]+1){
				int cc;
				cin>>cc;
				sum+=cc;
				c[i][j]=sum;
			}
			rminq.update(i,c[i][m[i]]);
			rmaxq.update(i,c[i][m[i]]);
		}

		bool ok;
		while(mcnt){
			ok=false;
			rep(i,n){
				if(m[i]==0)continue;
				rminq.update(i,c[i][m[i]-1]);
				rmaxq.update(i,c[i][m[i]-1]);
				if(rmaxq.query(0,n)-rminq.query(0,n)<=d){
					m[i]--;
					mcnt--;
					ok=true;
					break;
				}else{
					rminq.update(i,c[i][m[i]]);
					rmaxq.update(i,c[i][m[i]]);
				}
			}
			if(!ok)break;
		}
		if(mcnt==0)cout<<"Yes"<<endl;
		else cout<<"No"<<endl;
	}
	return 0;
}