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
m
1
c
1,1
...
c
1,
m
1
...
m
n
c
n
,1
...
c
n
,
m
n
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
m
i
, the number of the missiles possessed by the
i
-th country. It is followed
by a sequence of
m
i
positive integers. The
j
-th integer
c
i
,
j
represents the capability of the
j
-th
newest missile of the
i
-th country (
c
i
,
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 proce
...(truncated)

Submitted solutions:

code_snippet: #ifdef _WIN32
#define getc_unlocked(stdin) getc(stdin)
#endif
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <list>
#include <bitset>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cassert>
#include <ctime>
#include <numeric>
#include <functional>
#include <valarray>
#include <complex>
using namespace std;

const int dx[]={0,-1,0,1,-1,1, 1,-1};
const int dy[]={1,0,-1,0, 1,1,-1,-1};
const double eps=1e-8;

typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vgraph;
typedef vector<pii> vpii;
typedef vector<LL> vLL;
typedef vector<string> vs;

#define sz(a) a.size()
#define fori(i,a,b) for(int i(a),_b(b);i<=_b;++i)
#define ford(i,a,b) for(int i(a),_b(b);i>=_b;--i)
#define forn(i,n) fori(i,0,n-1)
#define fora(i,a) forn(i,sz(a))
#define fore(it,c) for(typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define all(a) a.begin(),a.end()
#define mp make_pair
#define pb push_back
#define clear0(a) memset(a,0,sizeof(a))
#define clearm1(a) memset(a,-1,sizeof(a))
#define maximum(a) (*max_element(all(a)))
#define minimum(a) (*min_element(all(a)))
#define findx(a,x) (find(all(a),x)-a.begin())
#define two(X) (1LL<<(X))
#define contain(S,X) ((S)&two(X))
#define setbit(S,X) ((S)|=two(X))
#define clearbit(S,X) ((S)&=~two(X))
#define togglebit(S,X) ((S)^=two(X))
#define sqr(a) ((a)*(a))
#define fi "input.inp"
#define fo "output.out"
#define maxn 100
#define mmax 100
#define kmax 10
#define modulo 1000000007
#define maxc 999999999
const int maxm=10000;
int n,d,missle[maxn][maxm],cnt[maxn];
struct hkr
{
    int value,id;
}sum[maxn];
bool check(int j)
{
    if (j<0) return false;
    int tmp=sum[j].id;
    if (j==n-1)
        return (abs(sum[j].value - missle[tmp][cnt[tmp]] - sum[j-1].value)<=d);
    return (abs(sum[j].value - missle[tmp][cnt[tmp]] - sum[n-1].value)<=d);
}
bool myh(hkr i,hkr j)
{
    return (i.value<j.value);
}
void dispose(int j)
{
    hkr tmp;
    tmp.id= sum[j].id;
    tmp.value= sum[j].value - missle [tmp.id] [cnt[tmp.id]--];
    if (tmp.value>0)
    {
        int i;
        for (i=j-1;i>=0;i--)
        {
            if (tmp.value >= sum[i].value) break;
            sum[i+1]=sum[i];
        }
        sum[i+1]=tmp;
    }
    else
    {
        fori(i,j,n-2) sum[i]=sum[i+1];
        n--; j=n-1;
    }
}
int main()
{
	for (;;)
    {
        int allmissle=0;
        cin >> n >> d;
        if (n==0 && d==0) break;
        for (int i=0;i<n;i++)
        {
            cin >> cnt[i];
            if (cnt[i]==0)
            {
                i--; n--;
                continue;
            }
            sum[i].value=0; sum[i].id=i;
            allmissle+=cnt[i];
            forn(j,cnt[i])
            {
                cin >> missle[i][j];
                sum[i].value+=missle[i][j];
            }
            cnt[i]--;
        }
        sort(sum,sum+n,myh);
        //if (sum[n-1].value-sum[0].value<=d)
        for (;;)
        {
            int j=n-1;
            if (check(j)) dispose(j); else
            if (check(j-1)) dispose(j-1); else break;
        }
        if (n<=1) cout << "Yes"; else cout << "No";
        cout << '\n';
    }
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3660
final_rank: 5

code_snippet: #ifdef _WIN32
#define getc_unlocked(stdin) getc(stdin)
#endif
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <list>
#include <bitset>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cassert>
#include <ctime>
#include <numeric>
#include <functional>
#include <valarray>
#include <complex>
using namespace std;

const int dx[]={0,-1,0,1,-1,1, 1,-1};
const int dy[]={1,0,-1,0, 1,1,-1,-1};
const double eps=1e-8;

typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vgraph;
typedef vector<pii> vpii;
typedef vector<LL> vLL;
typedef vector<string> vs;

#define sz(a) a.size()
#define fori(i,a,b) for(int i(a),_b(b);i<=_b;++i)
#define ford(i,a,b) for(int i(a),_b(b);i>=_b;--i)
#define forn(i,n) fori(i,0,n-1)
#define fora(i,a) forn(i,sz(a))
#define fore(it,c) for(typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define all(a) a.begin(),a.end()
#define mp make_pair
#define pb push_back
#define clear0(a) memset(a,0,sizeof(a))
#define clearm1(a) memset(a,-1,sizeof(a))
#define maximum(a) (*max_element(all(a)))
#define minimum(a) (*min_element(all(a)))
#define findx(a,x) (find(all(a),x)-a.begin())
#define two(X) (1LL<<(X))
#define contain(S,X) ((S)&two(X))
#define setbit(S,X) ((S)|=two(X))
#define clearbit(S,X) ((S)&=~two(X))
#define togglebit(S,X) ((S)^=two(X))
#define sqr(a) ((a)*(a))
#define fi "input.inp"
#define fo "output.out"
#define maxn 100
#define mmax 100
#define kmax 10
#define modulo 1000000007
#define maxc 999999999
const int maxm=10000;
int n,d,missle[maxn][maxm],cnt[maxn];
struct hkr
{
    int value,id;
}sum[maxn];
bool check(int j)
{
    if (j<0) return false;
    int tmp=sum[j].id;
    if (j==n-1)
        return (abs(sum[j].value - missle[tmp][cnt[tmp]] - sum[j-1].value)<=d);
    return (abs(sum[j].value - missle[tmp][cnt[tmp]] - sum[n-1].value)<=d);
}
bool myh(hkr i,hkr j)
{
    return (i.value<j.value);
}
void dispose(int j)
{
    hkr tmp;
    tmp.id= sum[j].id;
    tmp.value= sum[j].value - missle [tmp.id] [cnt[tmp.id]--];
    if (tmp.value>0)
    {
        int i;
        for (i=j-1;i>=0;i--)
        {
            if (tmp.value >= sum[i].value) break;
            sum[i+1]=sum[i];
        }
        sum[i+1]=tmp;
    }
    else
    {
        fori(i,j,n-2) sum[i]=sum[i+1];
        n--; j=n-1;
    }
}
int main()
{
	for (;;)
    {
        int allmissle=0;
        cin >> n >> d;
        if (n==0 && d==0) break;
        for (int i=0;i<n;i++)
        {
            cin >> cnt[i];
            if (cnt[i]==0)
            {
                i--; n--;
                continue;
            }
            sum[i].value=0; sum[i].id=i;
            allmissle+=cnt[i];
            forn(j,cnt[i])
            {
                cin >> missle[i][j];
                sum[i].value+=missle[i][j];
            }
            cnt[i]--;
        }
        sort(sum,sum+n,myh);
        //if (sum[n-1].value-sum[0].value<=d)
        for (;;)
        {
            int j=n-1;
            if (check(j)) dispose(j); else
            if (check(j-1)) dispose(j-1); else break;
        }
        if (n<=1) cout << "Yes"; else cout << "No";
        cout << '\n';
    }
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3852
final_rank: 11

code_snippet: #ifdef _WIN32
#define getc_unlocked(stdin) getc(stdin)
#endif
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <list>
#include <bitset>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cassert>
#include <ctime>
#include <numeric>
#include <functional>
#include <valarray>
#include <complex>
using namespace std;

const int dx[]={0,-1,0,1,-1,1, 1,-1};
const int dy[]={1,0,-1,0, 1,1,-1,-1};
const double eps=1e-8;

typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vgraph;
typedef vector<pii> vpii;
typedef vector<LL> vLL;
typedef vector<string> vs;

#define sz(a) a.size()
#define fori(i,a,b) for(int i(a),_b(b);i<=_b;++i)
#define ford(i,a,b) for(int i(a),_b(b);i>=_b;--i)
#define forn(i,n) fori(i,0,n-1)
#define fora(i,a) forn(i,sz(a))
#define fore(it,c) for(typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define all(a) a.begin(),a.end()
#define mp make_pair
#define pb push_back
#define clear0(a) memset(a,0,sizeof(a))
#define clearm1(a) memset(a,-1,sizeof(a))
#define maximum(a) (*max_element(all(a)))
#define minimum(a) (*min_element(all(a)))
#define findx(a,x) (find(all(a),x)-a.begin())
#define two(X) (1LL<<(X))
#define contain(S,X) ((S)&two(X))
#define setbit(S,X) ((S)|=two(X))
#define clearbit(S,X) ((S)&=~two(X))
#define togglebit(S,X) ((S)^=two(X))
#define sqr(a) ((a)*(a))
#define fi "input.inp"
#define fo "output.out"
#define maxn 100
#define mmax 100
#define kmax 10
#define modulo 1000000007
#define maxc 999999999
const int maxm=10000;
int n,d,missle[maxn][maxm],cnt[maxn];
struct hkr
{
    int value,id;
}sum[maxn];
bool check(int j)
{
    if (j<0) return false;
    int tmp=sum[j].id;
    if (j==n-1)
        return (abs(sum[j].value - missle[tmp][cnt[tmp]] - sum[j-1].value)<=d);
    return (abs(sum[j].value - missle[tmp][cnt[tmp]] - sum[n-1].value)<=d);
}
bool myh(hkr i,hkr j)
{
    return (i.value<j.value);
}
void dispose(int j)
{
    hkr tmp;
    tmp.id= sum[j].id;
    tmp.value= sum[j].value - missle [tmp.id] [cnt[tmp.id]--];
    if (tmp.value>0)
    {
        int i;
        for (i=j-1;i>=0;i--)
        {
            if (tmp.value >= sum[i].value) break;
            sum[i+1]=sum[i];
        }
        sum[i+1]=tmp;
    }
    else
    {
        fori(i,j,n-2) sum[i]=sum[i+1];
        n--; j=n-1;
    }
}
int main()
{
	cin >> n >> d;
	for (;;)
    {
        int allmissle=0;
        if (n==0 && d==0) break;
        for (int i=0;i<n;i++)
        {
            cin >> cnt[i];
            if (cnt[i]==0)
            {
                i--; n--;
                continue;
            }
            sum[i].value=0; sum[i].id=i;
            allmissle+=cnt[i];
            forn(j,cnt[i])
            {
                cin >> missle[i][j];
                sum[i].value+=missle[i][j];
            }
            cnt[i]--;
        }
        sort(sum,sum+n,myh);
        //if (sum[n-1].value-sum[0].value<=d)
        for (;;)
        {
            int j=n-1;
            if (check(j)) dispose(j); else
            if (check(j-1)) dispose(j-1); else break;
        }
        if (n<=1) cout << "Yes"; else cout << "No";
        cin >> n >> d;
cout << '\n';
    }
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3708
final_rank: 6

code_snippet: #include <cstdlib>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

const int MAXN = 128;
const int MAXM = 1024;

int m[MAXN];
int arr[MAXN][MAXM];
int st[MAXN];

int N, D;

inline int h(int v)
{
	return arr[v][st[v]];
}

int get_mx()
{
    int mx = 0;
    for(int i=0; i<N; i++)
    {
        if(h(i) > h(mx))
        {
            mx = i;
        }
    }
    return mx;
}

int get_mn()
{
    int mn = 0;
    for(int i=0; i<N; i++)
    {
        if(h(i) < h(mn))
        {
            mn = i;
        }
    }
    return mn;
}

int main()
{
    ios_base::sync_with_stdio(0);

    while(cin>>N>>D && !(N==0 && D==0))
    {
        for(int i=0; i<N; i++)
        {
            cin>>m[i];
            arr[i][0] = 0;
            st[i] = m[i];
            for(int j=1; j<=m[i]; j++)
            {
                cin>>arr[i][j];
                arr[i][j] += arr[i][j-1];
            }
        }

		bool ok = true;

        while(1)
        {
        	int mx = get_mx(), mn = get_mn();
        	if(st[mx] == 0)break;
        	if(h(mx) - h(mn) > D)
        	{
				ok = false;
				break;
        	}

        	st[mx]--;
        	int tmx = get_mx(), tmn = get_mn();
        	if(h(tmx) - h(tmn) <= D)continue;

			int dl = h(mx) + D;
        	st[mx]++;

        	for(int i=0; i<N; i++)
			{
				if(i==mx)continue;
				while(h(i) > dl)st[i]--;
			}
        }

        cout<<(ok ? "Yes" : "No")<<endl;
    }

    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3792
final_rank: 12

code_snippet: #include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct missle{
	int index;
	int disposal;
	int amount;
};

int num[100];
missle Missle[100][10010];

bool operator< (const missle& a, const missle& b) {
	return a.amount < b.amount;
}

bool operator> (const missle& a, const missle& b) {
	return a.amount > b.amount;
}

int main() {
	int n,d;
	while(cin >> n >> d && n && d) {
		priority_queue<missle, vector<missle>, less<vector<missle>::value_type> >moreq;
		priority_queue<missle, vector<missle>, greater<vector<missle>::value_type> >lessq;
		for(int i=0;i<n;i++) {
			cin >> num[i];
			Missle[i][0].amount = 0;
			Missle[i][0].index = i;
			Missle[i][0].disposal = 0;
			for(int j=1;j<=num[i];j++) {
				cin >> Missle[i][j].amount;
				Missle[i][j].disposal = j;
				Missle[i][j].index = i;
				Missle[i][j].amount += Missle[i][j-1].amount;
			}
			moreq.push(Missle[i][num[i]]);
			lessq.push(Missle[i][num[i]]);
		}
		missle top = moreq.top();
		missle bottom = lessq.top();
		if(top.amount - bottom.amount > d) {
			cout << "No" << endl;
			continue;
		}
		bool ok = true;
		bool finish = false;
		while(!finish) {
			finish = true;
			for(int i=0;i<n;i++) {
				if(i==top.index) continue;
				if(num[i] > 0) finish =false;
				while(num[i] > 0 && Missle[i][num[i]-1].amount >= top.amount - d) {
					num[i] --;
				}
				moreq.push(Missle[i][num[i]]);
				lessq.push(Missle[i][num[i]]);
			}
			int topindex = top.index;
			num[topindex]--;
			moreq.push(Missle[topindex][num[topindex]]);
			lessq.push(Missle[topindex][num[topindex]]);
			while(!moreq.empty()) {
				top = moreq.top();
				if(top.disposal > num[top.index]) {
					moreq.pop();
				} else {
					break;
				}
			}
			while(!lessq.empty()) {
				bottom = lessq.top();
				if(bottom.disposal > num[top.index]) {
					lessq.pop();
				} else {
					break;
				}
			}
			if(top.amount - bottom.amount > d) {
				ok = false;
				break;
			}
			if(bottom.amount == 0) {
				break;
			}
		}
		if(ok) {
			cout << "Yes" << endl;
		} else {
			cout << "No" << endl;
		}
	}
}
accuracy: 1
time_ms: 50
memory_kb: 17580
final_rank: 20

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

int main() {
while(1) {
    int N, D;
    cin >> N >> D;
    if (N == 0 && D == 0) return 0;
    vector<vector<int>> A(N);
    vector<int> COUNT(N,0);
    rep(i,0,N) {
        int M;
        cin >> M;
        A[i].resize(M);
        rep(j,0,M) cin >> A[i][j], COUNT[i] += A[i][j];
    }
    multiset<int> st;
    rep(i,0,N) st.insert(COUNT[i]);
    while(1) {
        bool update = false;
        rep(i,0,N) {
            if (!A[i].empty()) {
                int X = A[i].back();
                st.erase(st.find(COUNT[i]));
                COUNT[i] -= X;
                st.insert(COUNT[i]);
                if (*st.rbegin()-*st.begin() <= D) {
                    A[i].pop_back();
                    update = true;
                }
                else {
                    st.erase(st.find(COUNT[i]));
                    COUNT[i] += X;
                    st.insert(COUNT[i]);
                }
            }
        }
        if (!update) break;
    }
    bool check = true;
    rep(i,0,N) if (!A[i].empty()) check = false;
    cout << (check ? "Yes" : "No") << endl;
}
}
accuracy: 1
time_ms: 60
memory_kb: 3248
final_rank: 15

code_snippet: #line 2 "cp-library/src/cp-template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull  = unsigned long long;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
template < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }
template < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }
template < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y :           x / y); }
template < class T, class U > T floor(T x, U y) { return (x > 0 ?           x / y : (x - y + 1) / y); }
int popcnt(i32 x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(i64 x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }

#line 2 "cp-library/src/utility/rep_itr.hpp"
template < class T > struct itr_rep {
    T i, d;
    constexpr itr_rep(const T i) noexcept : i(i), d(1) {}
    constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}
    void operator++() noexcept { i += d; }
    constexpr int operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }
};

template < class T > struct rep {
    const itr_rep< T > s, t;
    constexpr rep(const T t) noexcept : s(0), t(t) {}
    constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}
    constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};

template < class T > struct revrep {
    const itr_rep < T > s, t;
    constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}
    constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}
    constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};
#line 3 "cp-library/src/utility/io.hpp"

/* 128bit integer */
istream& operator>>(istream& is, i128& x) {
    std::string s; is >> s;
    int pm = (s[0] == '-');
    x = 0;
    for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');
    if(pm) x *= -1;
    return is;
}
ostream& operator<<(ostream& os, const i128& x) {
    if(x == 0) return os << '0';
    i128 y = x;
    if(y < 0) { os << '-'; y *= -1; }
    std::vector<int> ny;
    while(y > 0) { ny.push_back(y % 10); y /= 10; }
    for(int i : revrep(ny.size())) os << ny[i];
    return os;
}

template < class S, class T > istream& operator>>(istream& is,       std::pair< S, T >& x) { is >> x.first        >> x.second; return is; }
template < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << " " << x.second; return os; }

namespace scanner {
    struct sca {
        template < class T > operator T() {
            T s; std::cin >> s; return s;
        }
    };
    struct vec {
        int n;
        vec(int n) : n(n) {}
        template < class T > operator std::vector< T >() {
            std::vector< T > v(n);
            for(T& x : v) std::cin >> x;
            return v;
        }
    };
    struct mat {
        int h, w;
        mat(int h, int w) : h(h), w(w) {}
        template < class T > operator std::vector< std::vector< T > >() {
            std::vector m(h, std::vector< T >(w));
            for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;
            return m;
        }
    };
    struct speedup {
        speedup() {
            std::cin.tie(0);
            std::ios::sync_with_stdio(0);
        }
    } speedup_instance;
}
scanner::sca in() { return scanner::sca(); }
scanner::vec in(int n) { return scanner::vec(n); }
scanner::mat in(int h, int w) { return scanner::mat(h, w); }

namespace printer {
    void precision(int d) { std::cout << std::fixed << std::setprecision(d); }
    void flush() { std::cout.flush(); }
}

template < class T >
ostream& operator<<(ostream& os, const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }
    return os;
}

int print() { std::cout << '\n'; return 0; }
template < class head, class... tail > int print(head&& h, tail&&... t) {
    std::cout << h; if(sizeof...(tail)) std::cout << ' ';
    return print(std::forward<tail>(t)...);
}
template < class T > int print_n(const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) std::cout << a[i] << "\n";
    return 0;
}


#line 2 "cp-library/src/utility/key_val.hpp"

template < class K, class V >
struct key_val {
    K key; V val;
    key_val() {}
    key_val(K key, V val) : key(key), val(val) {}
    template < std::size_t Index >
    std::tuple_element_t< Index, key_val >& get() {
        if constexpr (Index == 0) return key;
        if constexpr (Index == 1) return val;
    }
};

namespace std {

template < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};
template < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };
template < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };

}
#line 2 "cp-library/src/utility/vec_op.hpp"
template < class T > key_val< int, T > max_of(const vector< T >& a) {
    int i = std::max_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class T > key_val< int, T > min_of(const vector< T >& a) {
    int i = std::min_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class S, class T > S sum_of(const vector< T >& a) {
    S sum = 0;
    for(const T x : a) sum += x;
    return sum;
}
template < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {
    vector< S > res(R - L, S(0));
    for(const T x : a) res[x - L] += 1;
    return res;
}
template < class S, class T > struct prefix_sum {
    vector< S > s;
    prefix_sum(const vector< T >& a) : s(a) {
        s.insert(s.begin(), S(0));
        for(int i : rep(a.size())) s[i + 1] += s[i];
    }
    // [L, R)
    S sum(int L, int R) { return s[R] - s[L]; }
};
#line 3 "cp-library/src/utility/heap.hpp"

template < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;
template < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;

#line 27 "cp-library/src/cp-template.hpp"

#line 1 "cp-library/src/algorithm/bin_search.hpp"
template < class T, class F >
T bin_search(T ok, T ng, F f) {
    while(abs(ng - ok) > 1) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}

template < class T, class F >
T bin_search_real(T ok, T ng, F f, int step = 80) {
    while(step--) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}
#line 2 "cp-library/src/algorithm/argsort.hpp"

template < class T > std::vector< int > argsort(const std::vector< T > &a) {
    std::vector< int > ids((int)a.size());
    std::iota(ids.begin(), ids.end(), 0);
    std::sort(ids.begin(), ids.end(), [&](int i, int j) {
        return a[i] < a[j] || (a[i] == a[j] && i < j);
    });
    return ids;
}
#line 1 "macro.hpp"
namespace macro {

using size_type = int;
template < class container > void  sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }
template < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }
template < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }
template < class container > void unique(container& a) {
    std::sort(std::begin(a), std::end(a));
    a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));
}
template < class container > container  sorted(const container& a) { container b = a;  sort(b); return std::move(b); }
template < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }
template < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }
template < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }
template < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }
template < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }

const std::vector<std::pair<size_type, size_type>> dir4 = { {+1,  0}, {-1,  0}, { 0, +1}, { 0, -1} };
const std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1,  0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1,  0}, {+1, +1} };

#ifdef _DEBUG
#define debug(x) std::cout << "[" << __LINE__ << "] " << #x << ": " << x << std::endl
#else
#define debug(x)
#endif

template < class container > void concat(container& a, const container& b) {
    a.insert(std::end(a), std::begin(b), std::end(b));
}
std::vector<size_type> iota(const size_type n) {
    std::vector<size_type> I(n);
    std::iota(std::begin(I), std::end(I), 0);
    return I;
}
template < class container > std::vector<size_type> sort_idx(const container& a) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });
    return I;
}
template < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });
    return std::move(I);
}

struct grid {
    using size_type = int;
    size_type H, W;
    grid(const size_type H, const size_type W) : H(H), W(W) {}
    bool contains(const size_type i, const size_type j) {
        return 0 <= i and i < H and 0 <= j and j < W;
    }
};

} // namespace macro

using namespace macro;
#line 3 "A.cpp"

string solve(int n, int d) {
    vector c(n, vector<int>());
    for(int i : rep(n)) c[i] = in(int(in()));
    vector<int> s(n, 0);
    for(int i : rep(n)) s[i] = accumulate(c[i].begin(), c[i].end(), 0);

    bool update = true;
    while(update) {
        update = false;
        for(int i : rep(n)) {
            if(s[i] != 0) {
                int x = c[i].back(); c[i].pop_back();
                s[i] -= x;

                int min = +1e9, max = -1e9;
                for(int j : rep(n)) {
                    chmin(min, s[j]);
                    chmax(max, s[j]);
                }
                if(max - min <= d) {
                    update = true;
                } else {
                    c[i].push_back(x);
                    s[i] += x;
                }
            }
        }
    }
    return s == vector(n, 0) ? "Yes" : "No";
}

int main() {
    while(true) {
        int n = in(), d = in();
        if(make_pair(n, d) == make_pair(0, 0)) return 0;
        print(solve(n, d));
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3436
final_rank: 8

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = (n) - 1; i >= 0; --i)

void solve() {
    int n, d;
    cin >> n >> d;
    if (n == 0 and d == 0) exit(0);

    int sum = 0;

    vector<vector<int>> s(n);
    rep(i, n) {
        int m;
        cin >> m;
        s[i].resize(m + 1);
        sum += m;
        rep(j, m) {
            int x;
            cin >> x;
            s[i][j + 1] = x + s[i][j];
        }
    }

    rep(lp, sum) {
        vector<int> p_mn(n + 1), p_mx(n + 1), s_mn(n + 1), s_mx(n + 1);
        p_mn[0] = s_mn[n] = 10000000;
        p_mx[0] = s_mx[n] = 0;
        rep(i, n) {
            p_mn[i + 1] = min(p_mn[i], s[i].back());
            p_mx[i + 1] = max(p_mx[i], s[i].back());
        }
        rrep(i, n) {
            s_mn[i] = min(s_mn[i + 1], s[i].back());
            s_mx[i] = max(s_mx[i + 1], s[i].back());
        }

        int min_dif = d + 1;
        int argmin = -1;
        rep(i, n) {
            if (s[i].size() == 1) continue;

            int mn = min({ p_mn[i], s_mn[i + 1], *++s[i].rbegin() });
            int mx = max({ p_mx[i], s_mx[i + 1], *++s[i].rbegin() });
            int dif = mx - mn;
            if (dif < min_dif) {
                min_dif = dif;
                argmin = i;
            }
        }
        if (argmin == -1) {
            cout << "No" << endl;
            return;
        }
        s[argmin].pop_back();
    }

    cout << "Yes" << endl;
}

int main() {
    while (true) solve();
}
accuracy: 1
time_ms: 20
memory_kb: 3468
final_rank: 10

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

int main() {
    while(true) {
        int n,d;
        cin >> n >> d;
        if(n == 0 && d == 0) {
            return 0;
        }
        vector<stack<int>>st(n);
        vector<int>sum(n);
        for(int i = 0; i < n; i++) {
            int m;
            cin >> m;
            for(int j = 0; j < m; j++) {
                int c;
                cin >> c;
                sum[i] += c;
                st[i].push(c);
            }
        }
        bool flag = true;
        while (true) {
            bool ok = false;
            multiset<int>st2;
            for(int i = 0; i < n; i++) {
                if(!st[i].empty()) {
                    ok = true;
                    st2.insert(sum[i]);
                }
            }
            if(!ok) {
                break;
            }
            ok = false;
            for(int i = 0; i < n; i++) {
                if(!st[i].empty()) {
                    int a = st[i].top();
                    st2.erase(st2.find(sum[i]));
                    st2.insert(sum[i]-a);
                    if(*st2.rbegin()-d <= *st2.begin()) {
                        sum[i] -= a;
                        st[i].pop();
                        ok = true;
                        break;
                    }
                    st2.erase(st2.find(sum[i]-a));
                    st2.insert(sum[i]);
                }
            }
            if(!ok) {
                flag = false;
                break;
            }
        }
        cout << ((flag)?"Yes":"No") << endl;
    }
}
accuracy: 1
time_ms: 300
memory_kb: 3416
final_rank: 18

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

int MAX(vector<int> &vec){
  int ret = 0;
  for(int i=0; i<vec.size(); i++){
    ret = max(ret,vec[i]);
  }
  return ret;
}

void solve(int N, int D){
  vector<stack<int>> vec(N);
  vector<int> sum(N);
  for(int i=0; i<N; i++){
    int K;
    cin >> K;
    for(int j=0; j<K; j++){
      int x;
      cin >> x;
      vec[i].push(x);
      sum[i] += x;
    }
  }
  int M;
  while(true){
    M = MAX(sum);
    if(M == 0){
      break;
    }
    for(int i=0; i<N; i++){
      while(vec[i].size() && M - (sum[i] - vec[i].top()) <= D){
        sum[i] -= vec[i].top();
        vec[i].pop();
      }
    }
    for(int i=0; i<N; i++){
      if(vec[i].size() && sum[i] == M){
        sum[i] -= vec[i].top();
        vec[i].pop();
        if(MAX(sum) - sum[i] > D){
          cout << "No" << endl;
          return;
        }
      }
    }
  }
  cout << "Yes" << endl;
}

int main(){
  while(true){
    int N, D;
    cin >> N >> D;
    if(N == 0){
      break;
    }
    solve(N,D);
    //break;
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3428
final_rank: 1

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

using P = pair<int,int>;
#define INF 1020304050

int main() {
  
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n,d;
  cin >> n >> d;
  while (n != 0) {
    vector<vector<int>> c(n);
    vector<int> sum(n,0);
    for (int i=0;i<n;i++) {
      int m;
      cin >> m;
      for (int j=0;j<m;j++) {
        int v;
        cin >> v;
        c[i].emplace_back(v);
        sum[i] += v;
      }
      reverse(c[i].begin(),c[i].end());
    }
    vector<int> inds(n,0);
    bool update = true;
    while (update) {
      update = false;
      vector<int> minl(n,INF);
      vector<int> maxl(n,-INF);
      vector<int> minr(n,INF);
      vector<int> maxr(n,-INF);
      for (int i=1;i<n;i++) {
        minl[i] = min(minl[i-1],sum[i-1]);
        maxl[i] = max(maxl[i-1],sum[i-1]);
      }
      for (int i=n-2;i>=0;i--) {
        minr[i] = min(minr[i+1],sum[i+1]);
        maxr[i] = max(maxr[i+1],sum[i+1]);
      }
      for (int i=0;i<n;i++) {
        if (sum[i] == 0) continue;
        int tov = sum[i]-c[i][inds[i]];
        int tomin = min({minl[i],minr[i],tov});
        int tomax = max({maxl[i],maxr[i],tov});
        if (tomax-tomin <= d) {
          update = true;
          inds[i]++;
          sum[i] = tov;
          break;
        }
      }
    }
    sort(sum.rbegin(),sum.rend());
    if (sum[0] == 0) cout << "Yes" << endl;
    else cout << "No" << endl;
    cin >> n >> d;
  }
}
accuracy: 1
time_ms: 20
memory_kb: 3448
final_rank: 9

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

const int INF = 1e9;
const long long inf = 1LL<<60;
using ll = long long;

bool solve() {
  int n, d; cin >> n >> d;
  if (n == 0) return false;
  vector<vector<int>> mis(n);
  vector<int> cap(n);
  for (int i=0; i<n; i++) {
    int m; cin >> m;
    for (int j=0; j<m; j++) {
      int c; cin >> c;
      mis[i].push_back(c);
      cap[i] += c;
    }
  }
  while (true) {
    bool erased = false;
    for (int i=0; i<n; i++) if (mis[i].size()) {
      while (mis[i].size()) {
        cap[i] -= mis[i].back();
        if (*max_element(cap.begin(), cap.end()) - cap[i] <= d) {
          mis[i].pop_back();
          erased = true;
        } else {
          cap[i] += mis[i].back();
          break;
        }
      }
    }
    if (!erased) break;
  }
  bool ok = true;
  for (int i=0; i<n; i++) if (mis[i].size()) ok = false;
  cout << (ok ? "Yes" : "No") << endl;
  return true;
}

int main() {
  while (solve());
}
accuracy: 1
time_ms: 20
memory_kb: 3432
final_rank: 7

code_snippet: #include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <tuple>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <functional>
#include <numeric>
#include <iomanip> 
#include <stdio.h>
#include <assert.h>
#include <cstring>
//eolibraries
#define lnf 3999999999999999999
#define inf 999999999
#define fi first
#define se second
#define pb push_back
#define ll long long
#define ld long double
#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()
#define make_unique(a) sort(all(a)),a.erase(unique(all(a)),a.end())
#define pii pair <int,int>
#define rep(i,n) for(int i = 0 ; i < n ; i++) 
#define drep(i,n) for(int i = n-1 ; i >= 0 ; i--)
#define crep(i,x,n) for(int i = x ; i < n ; i++)
#define vi vector <int> 
#define vec(...) vector<__VA_ARGS__>
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
//eodefine
using namespace std;

const int mxn=12000;

int n,d;

void slv(){
	vec(vi) a(n,vi());
	rep(i,n){
		int k; cin>>k;
		rep(j,k){
			int x; cin>>x;
			a[i].pb(x);
		}
	}
	while(true){
		multiset<int> mst;
		rep(i,n){
			int s=0;
			rep(j,sz(a[i])) s += a[i][j];
			mst.insert(s);
		}
		if((*prev(mst.end()))-(*mst.begin())>d){
			cout<<"No\n";
			return;
		}
		bool pok=0;
		rep(i,n){
			if(a[i].empty()) continue;
			int s=0;
			rep(j,sz(a[i])) s += a[i][j];
			mst.erase(mst.find(s));
			mst.insert(s-a[i][sz(a[i])-1]);
			if((*prev(mst.end()))-(*mst.begin())<=d){
				a[i].pop_back();
				pok=1;
				break;
			}else{
				mst.erase(mst.find(s-a[i][sz(a[i])-1]));
				mst.insert(s);
			}
		}
		if(!pok) break;
	}
	rep(i,n){
		if(!a[i].empty()){
			cout<<"No\n";
			return;
		}
	}
	cout<<"Yes\n";
}

int main(){
fcin;
	while(true){
		cin>>n>>d;
		if(!n and !d) break;
		slv();
	}

//	
	return 0;
}
accuracy: 1
time_ms: 450
memory_kb: 3548
final_rank: 19

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m; i >= n; --i)
#define ALL(v) (v).begin(),(v).end()
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const ll INF=1LL<<60;
const int inf=(1<<30)-1;
const int mod=1e9+7;
int dx[8]={1,0,-1,0,-1,-1,1,1};
int dy[8]={0,1,0,-1,-1,1,-1,1};
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,d;
    while(cin >> n >> d){
        if(n==0&&d==0) break;
        vector<vector<int>> c(n);
        vector<int> cap(n),m(n);
        int sum=0;
        REP(i,n){
            cin >> m[i];
            REP(j,m[i]){
                int tmp;cin >> tmp;
                c[i].push_back(tmp);
                cap[i]+=tmp;
            }
            sum+=m[i];
            reverse(ALL(c[i]));
        }
        vector<int> id(n);
        bool yes=true;
        REP(t,sum){
            bool f=false;
            REP(i,n){
                bool can=true;
                if(cap[i]==0) continue;
                REP(j,n){
                    if(i==j) continue;
                    if(abs(cap[i]-c[i][id[i]]-cap[j])>d){
                        can=false;
                    }
                }
                if(can){
                    cap[i]-=c[i][id[i]];
                    id[i]++;
                    f=true;
                    break;
                }
            }
            if(!f){
                yes=false;
                break;
            }
            bool fin=true;
            REP(i,n) if(id[i]!=m[i]) fin=false;
            if(fin) break;
        }
        cout << (yes?"Yes":"No") << endl;
    }
}
accuracy: 1
time_ms: 100
memory_kb: 3464
final_rank: 17

code_snippet: #include <iostream>
#include <vector>
using namespace std;

int main()
{
    while(true){
        int n, d;
        cin >> n >> d;
        if(n == 0 && d == 0) break;
        vector<int> v[102];
        int m[102];
        int s[102]{0};
        for(int i = 0; i < n; i++){
            cin >> m[i];
            for(int j = 0; j < m[i]; j++){
                int c;
                cin >> c;
                v[i].push_back(c);
                s[i] += c;
            }
        }
        if(n == 1){
            cout << "Yes" << endl;
            continue;
        }
        int a = -1, b = -1;
        while(true){
            a = -1, b = -1;
            for(int i = 0; i < n; i++){
                if(a == -1 || s[i] > a){
                    b = a;
                    a = s[i];
                }
                else if(b == -1 || s[i] > b){
                    b = s[i];
                }
            }
            if(a == 0) break;
            bool f = false;
            for(int i = 0; i < n; i++){
                if(s[i] == a){
                    if(s[i] - v[i][m[i] - 1] >= b - d){
                        s[i] -= v[i][--m[i]];
                        f = true;
                        break;
                    }
                }
                else if(s[i] > 0){
                    if(s[i] - v[i][m[i] - 1] >= a - d){
                        s[i] -= v[i][--m[i]];
                        f = true;
                        break;
                    }
                }
            }
            if(!f) break;
        }
        if(a == 0) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3124
final_rank: 2

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

void solve(int n,int d){
    vector<int> m(n),sum(n,0);
    vector<vector<int>> C(n);
    int whole=0;
    for (int i=0;i<n;++i){
        cin >> m[i];
        whole+=m[i];
        for (int j=0;j<m[i];++j){
            int c; cin >> c;
            C[i].emplace_back(c);
            sum[i]+=c;
        }
    }

    auto check=[&](vector<int> v){
        int Min=*min_element(v.begin(),v.end()),Max=*max_element(v.begin(),v.end());
        return Max-Min<=d;
    };

    for (;whole--;){
        bool ok=false;
        for (int i=0;i<n;++i){
            if (!C[i].empty()){
                sum[i]-=C[i].back();
                if (check(sum)){
                    C[i].pop_back();
                    ok=true;
                } else sum[i]+=C[i].back();
            }
        }
        if (!ok){
            cout << "No" << '\n';
            return;
        }
        if (!*max_element(sum.begin(),sum.end())) break;
    }

    cout << "Yes" << '\n';
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,d;
    while (cin >> n >> d,n){
        solve(n,d);
    }
}
accuracy: 1
time_ms: 60
memory_kb: 2984
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
const long long MOD=1000000007;
// const long long MOD=998244353;
#define LOCAL
#pragma region Macros
typedef long long ll;
typedef __int128_t i128;
typedef unsigned int uint;
typedef unsigned long long ull;
#define ALL(x) (x).begin(),(x).end()
const int INF=1e9;
const long long IINF=1e18;
const int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
const char dir[4]={'D','R','U','L'};

template<typename T>
istream &operator>>(istream &is,vector<T> &v){
    for (T &x:v) is >> x;
    return is;
}
template<typename T>
ostream &operator<<(ostream &os,const vector<T> &v){
    for (int i=0;i<v.size();++i){
        os << v[i] << (i+1==v.size()?"": " ");
    }
    return os;
}
template<typename T,typename U>
ostream &operator<<(ostream &os,const pair<T,U> &p){
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template<typename T,typename U,typename V>
ostream&operator<<(ostream &os,const tuple<T,U,V> &t){
    os << '(' << get<0>(t) << ',' << get<1>(t) << ',' << get<2>(t) << ')';
    return os;
}
template<typename T,typename U,typename V,typename W>
ostream&operator<<(ostream &os,const tuple<T,U,V,W> &t){
    os << '(' << get<0>(t) << ',' << get<1>(t) << ',' << get<2>(t) << ',' << get<3>(t) << ')';
    return os;
}
template<typename T,typename U>
ostream &operator<<(ostream &os,const map<T,U> &m){
    os << '{';
    for (auto itr=m.begin();itr!=m.end();){
        os << '(' << itr->first << ',' << itr->second << ')';
        if (++itr!=m.end()) os << ',';
    }
    os << '}';
    return os;
}
template<typename T,typename U>
ostream &operator<<(ostream &os,const unordered_map<T,U> &m){
    os << '{';
    for (auto itr=m.begin();itr!=m.end();){
        os << '(' << itr->first << ',' << itr->second << ')';
        if (++itr!=m.end()) os << ',';
    }
    os << '}';
    return os;
}
template<typename T>
ostream &operator<<(ostream &os,const set<T> &s){
    os << '{';
    for (auto itr=s.begin();itr!=s.end();){
        os << *itr;
        if (++itr!=s.end()) os << ',';
    }
    os << '}';
    return os;
}
template<typename T>
ostream &operator<<(ostream &os,const multiset<T> &s){
    os << '{';
    for (auto itr=s.begin();itr!=s.end();){
        os << *itr;
        if (++itr!=s.end()) os << ',';
    }
    os << '}';
    return os;
}
template<typename T>
ostream &operator<<(ostream &os,const unordered_set<T> &s){
    os << '{';
    for (auto itr=s.begin();itr!=s.end();){
        os << *itr;
        if (++itr!=s.end()) os << ',';
    }
    os << '}';
    return os;
}
template<typename T>
ostream &operator<<(ostream &os,const deque<T> &v){
    for (int i=0;i<v.size();++i){
        os << v[i] << (i+1==v.size()?"": " ");
    }
    return os;
}

void debug_out(){cerr << '\n';}
template<class Head,class... Tail>
void debug_out(Head&& head,Tail&&... tail){
    cerr << head;
    if (sizeof...(Tail)>0) cerr << ", ";
    debug_out(move(tail)...);
}
#ifdef LOCAL
#define debug(...) cerr << " ";\
cerr << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << '\n';\
cerr << " ";\
debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

template<typename T> T gcd(T x,T y){return y!=0?gcd(y,x%y):x;}
template<typename T> T lcm(T x,T y){return x/gcd(x,y)*y;}

template<class T1,class T2> inline bool chmin(T1 &a,T2 b){
    if (a>b){a=b; return true;} return false;
}
template<class T1,class T2> inline bool chmax(T1 &a,T2 b){
    if (a<b){a=b; return true;} return false;
}
#pragma endregion

void solve(int n,int d){
    vector<int> m(n),sum(n,0);
    vector<vector<int>> C(n);
    int whole=0;
    for (int i=0;i<n;++i){
        cin >> m[i];
        whole+=m[i];
        for (int j=0;j<m[i];++j){
            int c; cin >> c;
            C[i].emplace_back(c);
            sum[i]+=c;
        }
    }

    auto check=[&](vector<int> v){
        int Min=*min_element(v.begin(),v.end()),Max=*max_element(v.begin(),v.end());
        return Max-Min<=d;
    };

    for (;whole--;){
        bool ok=false;
        for (int i=0;i<n;++i){
            if (!C[i].empty()){
                sum[i]-=C[i].back();
                if (check(sum)){
                    C[i].pop_back();
                    ok=true;
                } else sum[i]+=C[i].back();
            }
        }
        if (!ok){
            cout << "No" << '\n';
            return;
        }
        if (!*max_element(sum.begin(),sum.end())) break;
    }

    cout << "Yes" << '\n';
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,d;
    while (cin >> n >> d,n){
        solve(n,d);
    }
}
accuracy: 1
time_ms: 60
memory_kb: 3056
final_rank: 14

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

using ll = long long;
using PII = pair<ll, ll>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
template <typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template <typename T> void chmax(T &a, const T &b) { a = max(a, b); }

const ll INF = 1LL << 60;

struct FastIO {
    FastIO() {
        cin.tie(0);
        ios::sync_with_stdio(0);
    }
} fastiofastio;

// BEGIN CUT
// 根が1
template <typename Monoid> struct segtree {
    using T = typename Monoid::T;
    int n;
    vector<T> dat;

    segtree(int n_) {
        n = 1;
        while (n < n_)
            n <<= 1;
        dat.assign(n * 2, Monoid::id());
    }
    void build(vector<T> v) {
        REP(i, v.size()) dat[i + n] = v[i];
        for (int i = n - 1; i > 0; --i)
            dat[i] = Monoid::op(dat[i * 2], dat[i * 2 + 1]);
    }

    T query(int a, int b) {
        if (a >= b)
            return Monoid::id();
        T l = Monoid::id(), r = Monoid::id();
        for (a += n, b += n; a < b; a >>= 1, b >>= 1) {
            if (a & 1)
                l = Monoid::op(l, dat[a++]);
            if (b & 1)
                r = Monoid::op(dat[--b], r);
        }
        return Monoid::op(l, r);
    }
    void update(int i, T v) {
        i += n;
        dat[i] = v;
        while (i >>= 1) {
            dat[i] = Monoid::op(dat[i * 2], dat[i * 2 + 1]);
        }
    }

    friend ostream &operator<<(ostream &out, const segtree<Monoid> &seg) {
        out << "---------------------" << endl;
        int cnt = 1;
        for (int i = 1; i <= seg.n; i *= 2) {
            REP(j, i) {
                out << seg.dat[cnt] << " ";
                cnt++;
            }
            out << endl;
        }
        out << "---------------------" << endl;
        return out;
    }
};

template <typename Tp> struct min_monoid {
    using T = Tp;
    static constexpr Tp id() { return INT_MAX; }
    static Tp op(const Tp &a, const Tp &b) { return min(a, b); }
};
template <typename Tp> struct max_monoid {
    using T = Tp;
    static constexpr Tp id() { return -INF; }
    static Tp op(const Tp &a, const Tp &b) { return max(a, b); }
};
template <typename Tp> struct sum_monoid {
    using T = Tp;
    static constexpr Tp id() { return 0; }
    static Tp op(const Tp &a, const Tp &b) { return a + b; }
};
// END CUT

vector<int> c[100];
int pos[100];

int main() {
    while (1) {
        int n, d;
        cin >> n >> d;
        if (n == 0)
            break;
        segtree<max_monoid<ll>> seg(n);
        for (int i = 0; i < n; i++) {
            int m;
            cin >> m;
            c[i].resize(m);
            int sum = 0;
            for (int j = 0; j < m; j++) {
                cin >> c[i][j];
                sum += c[i][j];
            }
            seg.update(i, sum);
            reverse(c[i].begin(), c[i].end());
        }
        memset(pos, 0, sizeof(pos));
        while (1) {
            bool update = 0;
            for (int i = 0; i < n; i++) {
                if (pos[i] >= c[i].size())
                    continue;
                int nxt = seg.query(i, i + 1) - c[i][pos[i]];
                ll ma = 0;
                ma = max(ma, seg.query(0, i));
                if (i + 1 < n)
                    ma = max(ma, seg.query(i + 1, n));
                if (nxt >= ma - d) {
                    seg.update(i, nxt);
                    pos[i]++;
                    update = 1;
                    break;
                }
            }
            if (!update)
                break;
        }
        bool ok = 1;
        for (int i = 0; i < n; i++)
            if (pos[i] < c[i].size())
                ok = 0;
        if (ok)
            cout << "Yes\n";
        else
            cout << "No\n";
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3268
final_rank: 4

code_snippet: #include <bits/stdc++.h>

#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;

typedef long long ll;

const int INF = 1 << 30;
const ll LLINF = 1LL << 62;

int mod = 1000000007;

int main(void){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    while(true){
        int N, D; cin >> N >> D;
        if(N == 0) break;
        vector<vector<int>> A(N);
        int sum_mis = 0;
        rep(i, N){
            int m; cin >> m;
            sum_mis += m;
            A[i].resize(m);
            vector<int> temp(m);
            rep(j, m) cin >> temp[j];
            int acc = 0;
            rep(j, m){
                acc += temp[j];
                A[i][j] = acc;
            }
        }

        bool ans = true;
        vector<int> idx(N);
        rep(i, N) idx[i] = (int) A[i].size() - 1;
        rep(i, sum_mis){
            bool suc = false;
            rep(j, N){
                if(idx[j] == -1) continue;
                int eval = (idx[j] == 0) ? 0 : A[j][idx[j]-1];
                bool res = true;
                rep(k, N){
                    if(j == k) continue;
                    if(idx[k] == -1){if(eval > D) res = false;}
                    else if(abs(A[k][idx[k]] - eval) > D) res = false;
                }
                if(res){
                    //cout << j << endl;
                    idx[j]--;
                    suc = true;
                    break;
                }
            }
            if(!suc){
                ans = false;
                break;
            }
        }
        if(ans) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 3448
final_rank: 16

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

int n, d;
vector<vector<int>> c;

bool solve();

int main() {
  while (1) {
    cin >> n >> d;
    if (n + d == 0) break;
    c.resize(n);
    for (int i = 0; i < n; ++i) {
      int m;
      cin >> m;
      c[i].resize(m + 1);
      c[i][0] = 0;
      for (int j = 1; j <= m; ++j) {
        cin >> c[i][j];
        c[i][j] += c[i][j - 1];
      }
    }
    if (solve())
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}

bool solve() {
  while (1) {
    int id = 0;
    for (int i = 0; i < n; ++i)
      if (c[i].back() > c[id].back() ||
          (c[i].back() == c[id].back() && c[i].back() != 0 &&
           c[i][c[i].size() - 2] > c[id][c[id].size() - 2]))
        id = i;
    if (c[id].back() == 0) return 1;
    int maxnum = c[id].back();
    for (int i = 0; i < n; ++i)
      if (id != i) {
        int cnt = lower_bound(c[i].begin(), c[i].end(), maxnum - d) -
                  c[i].begin() + 1;
        while (cnt != c[i].size()) c[i].pop_back();
      }
    c[id].pop_back();
    maxnum = 0;
    for (int i = 0; i < n; ++i) maxnum = max(maxnum, c[i].back());
    for (int i = 0; i < n; ++i)
      if (maxnum - c[i].back() > d) return 0;
  }
  for (int i = 0; i < n; ++i)
    if (c[i].size() != 1) return 0;
  return 1;
}
accuracy: 1
time_ms: 10
memory_kb: 3584
final_rank: 3