The only difference between easy and hard versions is that you should complete all the projects in easy version but this is not necessary in hard version.

Polycarp is a very famous freelancer. His current rating is $r$ units.

Some very rich customers asked him to complete some projects for their companies. To complete the $i$-th project, Polycarp needs to have at least $a_i$ units of rating; after he completes this project, his rating will change by $b_i$ (his rating will increase or decrease by $b_i$) ($b_i$ can be positive or negative). Polycarp's rating should not fall below zero because then people won't trust such a low rated freelancer.

Is it possible to complete all the projects? Formally, write a program to check if such an order of the projects exists, that Polycarp has enough rating before starting each project, and he has non-negative rating after completing each project.

In other words, you have to check that there exists such an order of projects in which Polycarp will complete them, so he has enough rating before starting each project, and has non-negative rating after completing each project.


-----Input-----

The first line of the input contains two integers $n$ and $r$ ($1 \le n \le 100, 1 \le r \le 30000$) — the number of projects and the initial rating of Polycarp, respectively.

The next $n$ lines contain projects, one per line. The $i$-th project is represented as a pair of integers $a_i$ and $b_i$ ($1 \le a_i \le 30000$, $-300 \le b_i \le 300$) — the rating required to complete the $i$-th project and the rating change after the project completion.


-----Output-----

Print "YES" or "NO".


-----Examples-----
Input
3 4
4 6
10 -2
8 -1

Output
YES

Input
3 5
4 -5
4 -2
1 3

Output
YES

Input
4 4
5 2
5 -3
2 1
4 -2

Output
YES

Input
3 10
10 0
10 -10
30 0

Output
NO



-----Note-----

In the first example, the possible order is: $1, 2, 3$.

In the second example, the possible order is: $2, 3, 1$.

In the third example, the possible order is: $3, 1, 4, 2$.

Difficulty: HARD
Tags: Greedy algorithms
Skill types: Greedy algorithms

Starter code:

Solutions:
import sys
input = sys.stdin.readline
from math import gcd, sqrt
(n, r) = map(int, input().split())
l = []
for _ in range(n):
	l.append(list(map(int, input().split())))
p = 0
while p < n:
	if l[p][0] <= r and l[p][1] >= 0:
		r += l[p][1]
		l = l[:p] + l[p + 1:]
		p = 0
		n -= 1
	else:
		p += 1
if l == []:
	print('YES')
	exit(0)
ans = True
q = len(l)
for i in range(q):
	l[i][0] = max(l[i][0], -l[i][1])
l = sorted(l, key=lambda x: x[0] + x[1])
l.reverse()
for i in range(len(l)):
	if l[i][0] > r:
		ans = False
		break
	else:
		r += l[i][1]
if ans and r >= 0:
	print('YES')
else:
	print('NO')


(n, r) = map(int, input().split())
pl = []
mi = []
for _ in range(n):
	(a, b) = map(int, input().split())
	if b >= 0:
		pl.append((a, b))
	else:
		mi.append((a, b))
pl.sort()
mi.sort(key=lambda t: t[0] + t[1])
mi.reverse()
for (i, j) in pl:
	if r < i:
		print('NO')
		exit(0)
	else:
		r += j
for (i, j) in mi:
	if r < i:
		print('NO')
		exit(0)
	else:
		r += j
if r < 0:
	print('NO')
else:
	print('YES')


from sys import stdin
from sys import setrecursionlimit as SRL
SRL(10 ** 7)
rd = stdin.readline
rrd = lambda : map(int, rd().strip().split())
(n, r) = rrd()
pos = []
neg = []
for i in range(n):
	(a, b) = rrd()
	if b < 0:
		neg.append([a, b])
	else:
		pos.append([a, b])
pos.sort(key=lambda x: x[0])
neg.sort(key=lambda x: x[0] + x[1])
ans = 0
for (a, b) in pos:
	if r >= a:
		r += b
		ans += 1
	else:
		break
dp = [[0] * 105 for _i in range(60005)]
for i in range(r + 10):
	for j in range(len(neg)):
		if i >= neg[j][0] and i + neg[j][1] >= 0:
			if j:
				dp[i][j] = max(dp[i][j], dp[i + neg[j][1]][j - 1] + 1, dp[i][j - 1])
			else:
				dp[i][j] = 1
		elif j:
			dp[i][j] = dp[i][j - 1]
print('YES' if dp[r][len(neg) - 1] + ans == n else 'NO')


(n, r) = map(int, input().split())
pos = []
neg = []
for _ in range(n):
	(a, b) = map(int, input().split())
	if b > 0:
		pos.append((a, b))
	else:
		neg.append((max(a, abs(b)), b))
pos.sort()
cnt = 0
for i in pos:
	(a, b) = i
	if r < a:
		break
	r += b
	cnt += 1
neg.sort(key=lambda x: x[0] + x[1], reverse=True)
dp = [[0 for i in range(r + 1)] for i in range(len(neg) + 1)]
for i in range(1, len(neg) + 1):
	for j in range(1, r + 1):
		if j >= neg[i - 1][0] and j + neg[i - 1][1] >= 0:
			dp[i][j + neg[i - 1][1]] = max(dp[i - 1][j] + 1, dp[i - 1][j + neg[i - 1][1]])
		else:
			dp[i][j] = max(dp[i - 1][j], dp[i][j])
print(cnt + max([max(i) for i in dp]))


(n, r) = map(int, input().split())
a = []
cnt = 0
for i in range(n):
	a.append([int(j) for j in input().split()])
flag = True
while flag:
	flag = False
	for i in a:
		if r >= i[0] and i[1] >= 0:
			flag = True
			r += i[1]
			cnt += 1
			a.remove(i)
			break
a = sorted(a, key=lambda x: x[0] + x[1])
dp = [[0] * (r + 1) for i in range(len(a) + 1)]
for i in range(len(a)):
	for j in range(r + 1):
		dp[i][j] = dp[i - 1][j]
		if j >= a[i][0] and j + a[i][1] >= 0:
			dp[i][j] = max(dp[i][j], dp[i - 1][j + a[i][1]] + 1)
print(cnt + dp[len(a) - 1][r])


from collections import namedtuple
(n, r) = map(int, input().split())
plus = []
minus = []
Project = namedtuple('Project', 'value a b')
for _ in range(n):
	(a, b) = map(int, input().split())
	if b >= 0:
		plus.append((a, b))
	else:
		minus.append(Project(max(0, a + b), a, b))
plus.sort()
minus.sort(reverse=True)
ans = 0
for (a, b) in plus:
	if r >= a:
		r += b
		ans += 1
dp = [[-1 for _ in range(60001)] for _ in range(len(minus) + 1)]
dp[0][r] = 0
for i in range(len(dp) - 1):
	for j in range(len(dp[0])):
		if dp[i][j] == -1:
			continue
		dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
		if j + minus[i].b >= 0 and minus[i].a <= j:
			dp[i + 1][j + minus[i].b] = max(dp[i + 1][j + minus[i].b], dp[i][j] + 1)
ans += max(dp[-1])
print(ans)


(n, r) = map(int, input().split())
arr1 = []
arr2 = []
for i in range(n):
	(x, y) = map(int, input().split())
	if y >= 0:
		arr1.append((x, y))
	else:
		arr2.append((x, y))
arr1.sort()
arr2.sort(reverse=True)
currval = r
flag = 0
for i in range(len(arr1)):
	if arr1[i][0] > currval:
		print('NO')
		exit(0)
	else:
		currval += arr1[i][1]
tempval = currval
mark = [0] * len(arr2)
countx = 0
finalans = 0
while countx < len(arr2):
	flag = -1
	for i in range(len(arr2)):
		if mark[i] == 0 and currval >= arr2[i][0]:
			tempflag = 0
			for j in range(len(arr2)):
				if j != i and mark[j] == 0:
					if currval + arr2[i][1] < arr2[j][0]:
						tempflag = 1
						break
			if tempflag == 0:
				flag = i
				break
	if flag == -1:
		finalans = 1
		break
	else:
		currval += arr2[flag][1]
		mark[flag] = 1
		countx += 1
if currval < 0:
	finalans = 1
if finalans == 0:
	print('YES')
else:
	mark = [0] * len(arr2)
	countx = 0
	finalans = 0
	currval = tempval
	while countx < len(arr2):
		flag = -1
		for i in range(len(arr2)):
			if mark[i] == 0 and currval >= arr2[i][0]:
				tempflag = 0
				for j in range(len(arr2)):
					if j != i and mark[j] == 0:
						if currval + arr2[i][1] < arr2[j][0]:
							tempflag = 1
							break
				if tempflag == 0:
					flag = i
		if flag == -1:
			print('NO')
			exit(0)
			break
		else:
			currval += arr2[flag][1]
			mark[flag] = 1
			countx += 1
	if currval < 0:
		print('NO')
	else:
		print('YES')


(N, m) = map(int, input().split())
p = []
n = []
for i in range(N):
	a = list(map(int, input().split()))
	a[0] = max(a[0], -a[1])
	if a[1] < 0:
		n.append([a[0] + a[1], a[0], a[1]])
	else:
		p.append(a)
p.sort()
n.sort(reverse=True)
ans = 'YES'
c = m
for i in range(len(p)):
	if p[i][0] <= c:
		c += p[i][1]
	else:
		ans = 'NO'
		break
for i in range(len(n)):
	if n[i][1] <= c:
		c += n[i][2]
	else:
		ans = 'NO'
		break
print(ans)


from functools import cmp_to_key
from operator import itemgetter

def main():
	(n, r) = map(int, input().split())
	ps = [tuple(map(int, input().split())) for _ in range(n)]
	pos_ps = [(a, b) for (a, b) in ps if b >= 0]
	neg_ps = [(a, b) for (a, b) in ps if b < 0]

	def cmp(a, b):
		((aa, ab), (ba, bb)) = (a, b)
		return max(aa, max(*b) - ab) - max(ba, max(*a) - bb)
	pos_ps.sort(key=itemgetter(0))
	neg_ps.sort(key=cmp_to_key(cmp))
	del cmp
	res = 0
	for (a, b) in pos_ps:
		if r >= a:
			res += 1
			r += b
	cur = [r]
	for (a, b) in neg_ps:
		nxt = [-1] * (len(cur) + 1)
		for (i, r) in enumerate(cur):
			if r >= 0:
				nxt[i] = max(nxt[i], r)
				if r >= a:
					nxt[i + 1] = r + b
		while nxt[-1] < 0:
			nxt.pop()
		cur = nxt
	print(res + len(cur) - 1)
main()


from functools import reduce

def doProject(r, proj, nproj):
	count = 0
	for i in range(len(proj)):
		if proj[i][0] <= r:
			r += proj[i][1]
			count += 1
		else:
			pass
	dp = [[0 for j in range(r + 1)] for i in range(len(nproj) + 1)]
	dp[0][r] = count
	for i in range(len(nproj)):
		for cr in range(r + 1):
			if nproj[i][0] <= cr and cr + nproj[i][1] >= 0:
				dp[i + 1][cr + nproj[i][1]] = max(dp[i + 1][cr + nproj[i][1]], dp[i][cr] + 1)
			dp[i + 1][cr] = max(dp[i + 1][cr], dp[i][cr])
	count = reduce(lambda x, y: max(x, y), dp[len(nproj)])
	return count

def main():
	(n, r) = map(int, input().rstrip().split())
	(proj, nproj) = ([], [])
	for _ in range(n):
		temp = list(map(int, input().rstrip().split()))
		if temp[1] < 0:
			nproj.append(temp)
		else:
			proj.append(temp)
	proj.sort()
	nproj.sort(reverse=True, key=lambda x: x[0] + x[1])
	ans = doProject(r, proj, nproj)
	print(ans)
main()


(n, r) = list(map(int, input().split()))
proj_list = []
for _ in range(n):
	(a, b) = list(map(int, input().split()))
	proj_list.append([b, a, True])

def check(proj_list):
	nonlocal r
	while len(proj_list) > 0:
		i = 0
		while True:
			if i == len(proj_list):
				for prj in proj_list:
					if prj[2]:
						return False
				return True
			proj = proj_list[i]
			if r >= proj[1] and proj[2]:
				proj[2] = False
				r += proj[0]
				if r < 0:
					return False
				break
			i += 1
raise_mmr = [x for x in proj_list if x[0] >= 0]
hard_work = [x for x in proj_list if x[0] < 0]
proj_list = sorted(raise_mmr, key=lambda x: x[0], reverse=True) + sorted(hard_work, key=lambda x: x[1] + x[0], reverse=True)
print('YES' if check(proj_list) else 'NO')


from operator import itemgetter, attrgetter
(n, r) = map(int, input().split())
(ps, neg) = ([], [])
for i in range(n):
	(a, b) = map(int, input().split())
	if b >= 0:
		ps.append([a, b, -a - b])
	else:
		neg.append([a, b, -a - b])
ps = sorted(ps)
neg = sorted(neg, key=itemgetter(2))
ps = ps + neg
dp = [-float('inf')] * (n + 1)
dp[0] = r
for i in range(n):
	for j in range(n - 1, -1, -1):
		if dp[j] >= ps[i][0]:
			dp[j + 1] = max(dp[j + 1], dp[j] + ps[i][1])
if dp[n] >= 0:
	print('YES')
else:
	print('NO')


def myFunc(e):
	return e[0] + e[1]
(count, rating) = map(int, input().split())
goodJob = []
badJob = []
taken = 0
for i in range(count):
	(a, b) = map(int, input().split())
	if b >= 0:
		goodJob.append([a, b])
	else:
		badJob.append([a, b])
goodJob.sort()
badJob.sort(reverse=True, key=myFunc)
for job in goodJob:
	if job[0] <= rating:
		rating += job[1]
		taken += 1
	else:
		break
dp = []
for i in range(len(badJob) + 1):
	row = []
	for j in range(rating + 2):
		row.append(0)
	dp.append(row)
dp[0][rating] = taken
for i in range(len(badJob)):
	for curRating in range(rating + 1):
		if curRating >= badJob[i][0] and curRating + badJob[i][1] >= 0:
			dp[i + 1][curRating + badJob[i][1]] = max(dp[i + 1][curRating + badJob[i][1]], dp[i][curRating] + 1)
		dp[i + 1][curRating] = max(dp[i + 1][curRating], dp[i][curRating])
ans = 0
for curRating in range(rating + 1):
	ans = max(ans, dp[len(badJob)][curRating])
print(ans)


import collections
import sys
import io, os
import bisect
(n, r) = map(int, input().split())
l = []
for i in range(n):
	(a, b) = map(int, input().split())
	l.append([a, b])
l.sort()
(posa, posb, nega, negb) = ([], [], [], [])
for i in range(n):
	if l[i][1] >= 0:
		posa.append(l[i][0])
		posb.append(l[i][1])
	else:
		nega.append(l[i][0])
		negb.append(l[i][1])
while True:
	if len(posa) > 0:
		ind = bisect.bisect_right(posa, r)
		if ind == 0:
			print('NO')
			break
		(maxa, ind2) = (-1 * 1000000000000, 0)
		for i in range(ind):
			if posb[i] + r >= maxa:
				maxa = posb[i] + r
				ind2 = i
		posa.pop(ind2)
		r += posb[ind2]
		posb.pop(ind2)
	else:
		ind = bisect.bisect_right(nega, r)
		if ind == 0:
			print('NO')
			break
		(maxa, ind2) = (-1 * 1000000000000, 0)
		for i in range(ind):
			if negb[i] + nega[i] >= maxa and r + negb[i] >= 0:
				maxa = negb[i] + nega[i]
				ind2 = i
		nega.pop(ind2)
		r += negb[ind2]
		if r < 0:
			print('NO')
			break
		negb.pop(ind2)
	if len(posa) == 0 and len(nega) == 0:
		print('YES')
		break


(N, R) = [int(x) for x in input().split()]
projects = [[int(x) for x in input().split()] for _ in range(N)]
pos = []
neg = []
for (a, b) in projects:
	if b < 0:
		neg.append((a, b))
	else:
		pos.append((a, b))
pos.sort()
ans = 0
for (a, b) in pos:
	if R >= a:
		R += b
		ans += 1
neg.sort(key=sum, reverse=True)
memo = {}

def dp(i, r):
	if (i, r) in memo:
		return memo[i, r]
	if i == len(neg):
		return 0
	(a, b) = neg[i]
	ans = dp(i + 1, r)
	if r >= a and r + b >= 0:
		ans = max(ans, dp(i + 1, r + b) + 1)
	memo[i, r] = ans
	return ans
print(dp(0, R) + ans)


from sys import stdin, stdout
import math
import copy
(N, r) = [int(x) for x in stdin.readline().split()]
visited = [0] * N
a = [0] * N
b = [0] * N
pos = 0
dp = {}
for i in range(N):
	arr = [int(x) for x in stdin.readline().split()]
	a[i] = arr[0]
	b[i] = arr[1]
	if b[i] >= 0:
		pos += 1
valid = 0
for i in range(pos):
	idx = -1
	start = 0
	gain = -50000
	for j in range(N):
		if visited[j] == 1 or b[j] < 0:
			continue
		if b[j] > gain and r >= a[j]:
			gain = b[j]
			idx = j
			start = a[j]
		elif b[j] == gain and r >= a[j]:
			if a[j] > start:
				idx = j
				start = a[j]
	if idx == -1:
		break
	else:
		visited[idx] = 1
		r += b[idx]
		valid = i + 1
dp[r] = valid
tmp = []
for i in range(N):
	if visited[i] == 1 or b[i] >= 0:
		continue
	tmp.append((a[i], b[i], i))
tmp.sort(key=lambda e: (e[0] + e[1], e[0]), reverse=True)
for i in range(len(tmp)):
	dp_tmp = copy.deepcopy(dp)
	for threshold in dp:
		if threshold >= tmp[i][0]:
			new_r = threshold + tmp[i][1]
			if new_r in dp_tmp:
				dp_tmp[new_r] = max(dp[new_r], dp[threshold] + 1)
			else:
				dp_tmp[new_r] = dp[threshold] + 1
	dp = dp_tmp
res = 0
for key in dp:
	if key >= 0:
		res = max(res, dp[key])
print(res)


(n, r) = (int(i) for i in input().split())
neg_change = []
pos_change = []
for _ in range(n):
	(a, b) = (int(i) for i in input().split())
	if b < 0:
		neg_change.append((a, b))
	elif r >= a:
		r += b
	else:
		pos_change.append((a, b))
pos_change.sort()
possible = True
for (i, j) in pos_change:
	if r >= i:
		r += j
	else:
		possible = False
		break
if possible:
	func = lambda i: i[0] + i[1]
	neg_change.sort(key=func, reverse=True)
	for (i, j) in neg_change:
		if r + j >= 0 and r >= i:
			r += j
		else:
			possible = False
			break
print('YNEOS'[not possible::2])


(n, r) = map(int, input().split())
lst1 = []
lst2 = []
for i in range(n):
	(a, b) = map(int, input().split())
	if b >= 0:
		lst1.append((a, b))
	else:
		lst2.append((a, b))
lst1.sort()
lst2.sort(reverse=True, key=lambda a: a[0] + a[1])
for i in lst1:
	if r < i[0]:
		print('NO')
		exit()
	r += i[1]
for i in lst2:
	if r < i[0]:
		print('NO')
		exit()
	r += i[1]
print('NO' if r < 0 else 'YES')


(n, r) = map(int, input().split())
pt = []
nt = []
r2 = r
for _ in range(n):
	(a, b) = map(int, input().split())
	r2 += b
	if b >= 0:
		pt.append((a, b))
	else:
		nt.append((a, b))
if r2 < 0:
	exit(print('NO'))
pt.sort()
nt.sort(key=lambda t: t[0] + t[1])
for (a, b) in pt:
	if r < a:
		exit(print('NO'))
	r += b
for (a, b) in nt:
	if r2 < a + b:
		exit(print('NO'))
	r2 -= b
print('YES')


from sys import exit
(n, r) = map(int, input().split())
aa = [0] * n
bb = [0] * n
for i in range(n):
	(aa[i], bb[i]) = map(int, input().split())
avail = set(range(n))
fr = r + sum(bb)
if fr < 0:
	print('NO')
	exit(0)
ok = True
for i in range(n):
	nxt = -1
	for j in avail:
		if aa[j] <= r and bb[j] >= 0:
			nxt = j
			break
	if nxt == -1:
		break
	avail.remove(nxt)
	r += bb[nxt]
for i in range(len(avail)):
	nxt = -1
	for j in avail:
		if aa[j] + bb[j] <= fr and bb[j] < 0:
			nxt = j
			break
	if nxt == -1:
		ok = False
		break
	avail.remove(nxt)
	fr -= bb[nxt]
if ok:
	print('YES')
else:
	print('NO')


(a, b) = map(int, input().split())
A = []
B = []
for i in range(a):
	(q, w) = map(int, input().split())
	if w < 0:
		q = max(q, -w)
		B.append([q + w, q, w])
	else:
		A.append([q, w])
A.sort()
B.sort()
B.reverse()
q = True
i = 0
w = len(A)
while q == True and i < w:
	if b >= A[i][0]:
		b += A[i][1]
		i += 1
	else:
		q = False
i = 0
w = len(B)
while q == True and i < w:
	if b >= B[i][1]:
		b += B[i][2]
		i += 1
	else:
		q = False
if q == True and b >= 0:
	print('YES')
else:
	print('NO')


import sys
input = sys.stdin.readline
(n, r) = map(int, input().split())
l = []
for _ in range(n):
	l.append(list(map(int, input().split())))
p = 0
ans = 0
while p < n:
	if l[p][0] <= r and l[p][1] >= 0:
		r += l[p][1]
		l = l[:p] + l[p + 1:]
		p = 0
		n -= 1
		ans += 1
	else:
		p += 1
if l == []:
	print(ans)
	exit(0)
q = len(l)
for i in range(q):
	l[i][0] = max(l[i][0], -l[i][1])
l = sorted(l, key=lambda x: x[0] + x[1])
l.reverse()
dp = [[0 for _ in range(r + 1)] for _ in range(q + 1)]
for i in range(q):
	for j in range(r + 1):
		if j >= l[i][0] and 0 <= j + l[i][1] <= r:
			dp[i + 1][j + l[i][1]] = max(dp[i + 1][j + l[i][1]], dp[i][j] + 1)
		dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
print(max(dp[-1]) + ans)


from sys import stdin
input = stdin.readline
(n, r) = map(int, input().split())
poz = []
neg = []
for i in range(n):
	(a, b) = map(int, input().split())
	if b < 0:
		neg.append((a, b))
	else:
		poz.append((a, b))
poz.sort()
neg.sort(key=lambda x: sum(x), reverse=True)
for (a, b) in poz:
	if a <= r:
		r += b
	else:
		print('NO')
		exit()
for (a, b) in neg:
	if a <= r:
		r += b
	else:
		print('NO')
		exit()
if r < 0:
	print('NO')
else:
	print('YES')


import sys
from collections import defaultdict as dd
mod = 10 ** 9 + 7

def ri(flag=0):
	if flag == 0:
		return [int(i) for i in sys.stdin.readline().split()]
	else:
		return int(sys.stdin.readline())
(n, r) = ri()
eventspos = []
eventsneg = []
for i in range(n):
	temp = ri()
	if temp[1] >= 0:
		eventspos.append(temp)
	else:
		eventsneg.append(temp)
eventspos.sort()
eventsneg.sort(key=lambda x: x[0] + x[1])
eventsneg.reverse()
status = 1
ans = 0
for i in range(len(eventspos)):
	if eventspos[i][0] <= r:
		r += eventspos[i][1]
		ans += 1
	else:
		status = 0
check = [0 for i in range(r + 1)]
for i in range(len(eventsneg)):
	for j in range(eventsneg[i][0], r + 1):
		if j + eventsneg[i][1] >= 0:
			check[j + eventsneg[i][1]] = max(check[j + eventsneg[i][1]], check[j] + 1)
if max(check) + ans == n:
	print('YES')
else:
	print('NO')


import sys
input = sys.stdin.readline
(n, r) = map(int, input().split())
(ABP, ABN) = ([], [])
for i in range(n):
	(a, b) = map(int, input().split())
	if b >= 0:
		ABP.append([a, b])
	else:
		ABN.append([a, b])
ABP.sort(key=lambda x: (x[0], x[1]))
chk = True
for i in range(len(ABP)):
	if ABP[i][0] > r:
		chk = False
	else:
		r += ABP[i][1]
if chk == False:
	print('NO')
else:
	ABN.sort(key=lambda x: (x[0] + x[1], x[0]), reverse=True)
	for i in range(len(ABN)):
		(a, b) = ABN[i]
		if r < a:
			chk = False
		else:
			r += b
	if chk == False or r < 0:
		print('NO')
	else:
		print('YES')


import sys
(n, r) = map(int, input().split())
plus = []
minus = []
for (a, b) in (map(int, l.split()) for l in sys.stdin):
	if b >= 0:
		plus.append((a, b))
	else:
		minus.append((a, b))
plus.sort()
for (a, b) in plus:
	if r < a:
		print('NO')
		exit()
	r += b
minus.sort(key=lambda x: x[0] + x[1], reverse=True)
for (a, b) in minus:
	if r < a:
		print('NO')
		exit()
	r += b
print('YES' if r >= 0 else 'NO')


inp = str(input()).split()
size = int(inp[0])
r = int(inp[1])
pos = []
neg = []
for i in range(size):
	inp = str(input()).split()
	a = int(inp[0])
	b = int(inp[1])
	if b >= 0:
		pos.append((a, b))
	else:
		neg.append((a, b))
pos = sorted(pos)
projects = 0
for ab in pos:
	(a, b) = ab
	if r >= a:
		r += b
		projects += 1
	else:
		break
neg = sorted(neg, key=lambda ab: ab[0] + ab[1], reverse=True)
n = len(neg)
dp = [[0] * (r + 1) for _ in range(n + 1)]
dp[0][r] = projects
for i in range(0, n):
	for j in range(0, r + 1):
		if j >= neg[i][0] and j + neg[i][1] >= 0:
			dp[i + 1][j + neg[i][1]] = max(dp[i + 1][j + neg[i][1]], dp[i][j] + 1)
		dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
print(max(dp[n]))


import math
import sys
from collections import defaultdict
nt = lambda : list(map(int, input().split()))

def main():
	(n, r) = nt()
	projects = [tuple(nt()) for _ in range(n)]
	positive = [t for t in projects if t[1] > 0]
	negative = [t for t in projects if t[1] <= 0]
	ok = True
	for p in sorted(positive):
		if p[0] <= r:
			r += p[1]
		else:
			ok = False
			break
	if ok:
		for p in sorted(negative, key=lambda x: -x[0] - x[1]):
			if p[0] <= r:
				r += p[1]
				if r < 0:
					ok = False
					break
			else:
				ok = False
				break
	print('YES' if ok else 'NO')

def __starting_point():
	main()
__starting_point()


(n, r) = map(int, input().split())
a = [list(map(int, input().split())) for i in range(n)]
pos = []
neg = []
for x in a:
	if x[1] > 0:
		pos.append(x)
	else:
		neg.append(x)
pos.sort(key=lambda k: k[0])
flg = 1
for x in pos:
	if r < x[0]:
		flg = 0
	else:
		r += x[1]
neg.sort(key=lambda k: k[0] + k[1], reverse=True)
for x in neg:
	if r < x[0]:
		flg = 0
	else:
		r += x[1]
		if r < 0:
			flg = 0
if flg == 1:
	print('YES')
else:
	print('NO')


(n, r) = [int(i) for i in input().split()]
proj = [[0 for i in range(2)] for j in range(n)]
posb = 0
sum_neg = 0
for i in range(n):
	(proj[i][0], proj[i][1]) = [int(j) for j in input().split()]
	if proj[i][1] >= 0:
		posb += 1
	else:
		sum_neg += proj[i][1]
negb = n - posb
proj = sorted(proj, key=lambda elem: elem[1])
proj_pos = sorted(proj[negb:], key=lambda elem: elem[0])
res = True
i = 0
while res and i < posb:
	if r < proj_pos[i][0]:
		res = False
	else:
		r += proj_pos[i][1]
		i += 1
if res:
	proj_neg = sorted(proj[:negb], key=lambda elem: elem[0] + elem[1], reverse=True)
	i = 0
	while res and i < negb and (r >= 0):
		if r < proj_neg[i][0]:
			res = False
		else:
			r += proj_neg[i][1]
			i += 1
	if r >= 0 and res:
		print('YES')
	else:
		print('NO')
else:
	print('NO')


(n, r) = list(map(int, input().split()))
a = []
b = []
for _ in range(n):
	(c, d) = list(map(int, input().split()))
	if d < 0:
		b.append([c, d])
	else:
		a.append([c, d])
a.sort(key=lambda x: x[0])
b.sort(key=lambda x: x[0] + x[1], reverse=True)
z = 1
for i in a:
	if i[0] > r:
		z = 0
		break
	r += i[1]
for i in b:
	if i[0] > r:
		z = 0
		break
	r += i[1]
if z == 0 or r < 0:
	print('NO')
else:
	print('YES')


from sys import stdin, stdout
import math
(N, r) = [int(x) for x in stdin.readline().split()]
visited = [0] * N
a = [0] * N
b = [0] * N
pos = 0
for i in range(N):
	arr = [int(x) for x in stdin.readline().split()]
	a[i] = arr[0]
	b[i] = arr[1]
	if b[i] >= 0:
		pos += 1
for i in range(pos):
	idx = -1
	start = 0
	gain = -50000
	for j in range(N):
		if visited[j] == 1 or b[j] < 0:
			continue
		if b[j] > gain and r >= a[j]:
			gain = b[j]
			idx = j
			start = a[j]
		elif b[j] == gain and r >= a[j]:
			if a[j] > start:
				idx = j
				start = a[j]
	if idx == -1:
		print('NO')
		quit()
	else:
		visited[idx] = 1
		r += b[idx]
tmp = []
for i in range(N):
	if visited[i] == 1 or b[i] >= 0:
		continue
	tmp.append((a[i], b[i], i))
tmp.sort(key=lambda e: (e[0] + e[1], e[0]), reverse=True)
for i in range(len(tmp)):
	require = tmp[i][0]
	bias = tmp[i][1]
	if r >= require:
		r += bias
	else:
		print('NO')
		quit()
if r < 0:
	print('NO')
	quit()
print('YES')


def doProject(r, proj, nproj):
	for i in range(len(proj)):
		if proj[i][0] <= r:
			r += proj[i][1]
		else:
			return 'NO'
	for i in range(len(nproj)):
		if nproj[i][0] <= r:
			r += nproj[i][1]
		else:
			return 'NO'
	if r > -1:
		return 'YES'
	else:
		return 'NO'

def main():
	(n, r) = map(int, input().rstrip().split())
	(proj, nproj) = ([], [])
	for _ in range(n):
		temp = list(map(int, input().rstrip().split()))
		if temp[1] < 0:
			nproj.append(temp)
		else:
			proj.append(temp)
	proj.sort()
	nproj.sort(reverse=True, key=lambda x: x[0] + x[1])
	ans = doProject(r, proj, nproj)
	print(ans)
main()


import math
import sys
from collections import defaultdict
nt = lambda : map(int, input().split())

def main():
	(n, r) = nt()
	projects = [tuple(nt()) for _ in range(n)]
	positive = [t for t in projects if t[1] > 0]
	negative = [t for t in projects if t[1] <= 0]
	max_pos = 0
	for p in sorted(positive):
		if p[0] <= r:
			r += p[1]
			max_pos += 1
		else:
			break
	negative.sort(key=lambda x: -x[0] - x[1])
	MAX = 60001
	dp = [[-1 for _ in range(MAX)] for _ in range(len(negative) + 1)]
	dp[0][r] = 0
	for i in range(len(negative)):
		for j in range(MAX):
			if dp[i][j] == -1:
				continue
			dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
			if j >= negative[i][0] and j + negative[i][1] >= 0:
				dp[i + 1][j + negative[i][1]] = max(dp[i + 1][j + negative[i][1]], dp[i][j] + 1)
	max_neg = 0
	for i in range(MAX):
		max_neg = max(max_neg, dp[len(negative)][i])
	print(max_pos + max_neg)
main()


from collections import defaultdict
(n, r) = map(int, input().split())
pos = []
neg = []
for _ in range(n):
	(a, b) = map(int, input().split())
	if b > 0:
		pos.append((a, b))
	else:
		neg.append((a, b))
pos.sort()
has = True
for i in pos:
	(a, b) = i
	if r < a:
		has = False
		break
	r += b
if not has:
	print('NO')
else:
	neg.sort(key=lambda x: x[0] + x[1], reverse=True)

	def can_complete():
		for i in range(len(neg)):
			if r >= abs(neg[i][0]):
				return i
		else:
			return -1
	cnt = 0
	while True:
		result = can_complete()
		if result == -1:
			break
		r += neg[result][1]
		if r < 0:
			break
		del neg[result]
	if neg != []:
		print('NO')
	else:
		print('YES')


from math import *
MOD = int(1000000000.0) + 7

def nextInt():
	return int(input())

def nextInts():
	return list(map(int, input().split()))

def YN(x):
	return 'YES' if x else 'NO'

def solve():
	(n, r) = nextInts()
	pos = []
	neg = []
	for i in range(n):
		(a, b) = nextInts()
		a = max(a, -b)
		if b >= 0:
			pos.append((a, b))
		else:
			neg.append((a, b))
	pos.sort(key=lambda x: x[0])
	neg.sort(key=lambda x: -(x[0] + x[1]))
	ans = 0
	for t in pos:
		if r >= t[0]:
			r += t[1]
			ans += 1
	dp = [-MOD] * 60001
	dp[r] = ans
	for t in neg:
		for j in range(t[0], 60001):
			dp[j + t[1]] = max(dp[j + t[1]], dp[j] + 1)
	return max(dp)
print(solve())


(n, r) = map(int, input().split())
plus = []
minn = []
for i in range(0, n):
	temp = list(map(int, input().split()))
	if temp[1] <= 0:
		temp.append(temp[0] + temp[1])
		minn.append(temp)
	else:
		plus.append(temp)
plus.sort()
minn.sort(reverse=True, key=lambda x: x[2])
flag = True
for i in plus:
	if i[0] <= r:
		r += i[1]
	else:
		flag = False
		break
if flag:
	for i in minn:
		if i[0] <= r:
			r += i[1]
		else:
			flag = False
			break
if flag and r >= 0:
	print('YES')
else:
	print('NO')


(n, r) = map(int, input().split())
arr = [list(map(int, input().split())) for _ in range(n)]

def solve1(cur, arr):
	flg = True
	while len(arr) > 0:
		max_inc = -9999
		choose = None
		for (a, b) in arr:
			if cur >= a and max_inc < b:
				max_inc = b
				choose = a
		if choose is None:
			flg = False
			break
		cur += max_inc
		arr.remove([choose, max_inc])
	if flg == False:
		return ('NO', None)
	return ('YES', cur)

def solve2(cur, arr2):
	flg = True
	while len(arr2) > 0:
		min_inc = 0
		choose = None
		for (a, b) in arr2:
			if cur - b >= a and b < min_inc:
				min_inc = b
				choose = a
		if choose is None:
			flg = False
			break
		cur -= min_inc
		arr2.remove([choose, min_inc])
	if flg == False:
		return ('NO', None)
	return ('YES', cur)
arr1 = [[x, y] for (x, y) in arr if y >= 0]
arr2 = [[x, y] for (x, y) in arr if y < 0]
(ans, cur) = solve1(r, arr1)
if ans == 'NO':
	print('NO')
else:
	cur += sum([x[1] for x in arr if x[1] < 0])
	if cur < 0:
		print('NO')
	else:
		(ans, cur) = solve2(cur, arr2)
		if ans == 'NO':
			print('NO')
		else:
			print('YES')


(num_p, rating) = map(int, input().split(' '))

def do_projs(L, rating):
	for (r, change) in L:
		if r > rating:
			return ('NO', rating)
		if r <= rating:
			rating += change
		if rating < 0:
			return ('NO', rating)
	return ('YES', rating)
projl = []
for project in range(num_p):
	projl.append(tuple(map(int, input().split(' '))))
projl.sort(key=lambda i: i[1], reverse=True)
for (idx, proj) in enumerate(projl):
	if proj[1] <= 0:
		break
projl_pos = projl[:idx]
projl_neg = projl[idx:]
projl_pos.sort(key=lambda i: i[0], reverse=False)
mark = False
(res, rating) = do_projs(projl_pos, rating)
if res == 'NO':
	print(res)
else:
	projl_neg.sort(key=lambda i: i[0] + i[1], reverse=True)
	(res, rating) = do_projs(projl_neg, rating)
	print(res)


from math import *

def nextInt():
	return int(input())

def nextInts():
	return list(map(int, input().split()))

def YN(x):
	return 'YES' if x else 'NO'

def solve():
	(n, r) = nextInts()
	pos = []
	neg = []
	for i in range(n):
		(a, b) = nextInts()
		a = max(a, -b)
		if b >= 0:
			pos.append((a, b))
		else:
			neg.append((a, b))
	pos.sort(key=lambda x: x[0])
	neg.sort(key=lambda x: -(x[0] + x[1]))
	for t in pos:
		if r < t[0]:
			return False
		r += t[1]
	for t in neg:
		if r < t[0]:
			return False
		r += t[1]
	return True
print(YN(solve()))


def sortFirst(val):
	return val[0]

def key(ab):
	(a, b) = ab
	return (2, -a - b) if b < 0 else (1, a)
(n, r) = map(int, input().split())
ability = True
i = 0
rate = []
while i < n:
	i += 1
	(a, b) = map(int, input().split())
	rate.append((a, b))
	map1 = sorted(rate, key=key)
for (x, y) in map1:
	if r < x:
		k = rate.index((x, y))
		ability = False
		break
	else:
		r += y
		if r < 0:
			ability = False
			break
if ability:
	print('Yes')
else:
	print('No')


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import ceil, floor
from copy import *
from collections import deque, defaultdict
from collections import Counter as counter
from itertools import combinations
from itertools import permutations as permutate
from bisect import bisect_left as bl
from operator import *
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192
from sys import stderr

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def fsep():
	return map(float, inp().split())

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def pow(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res
from functools import reduce

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def gcd(a, b):
	if a == b:
		return a
	while b > 0:
		(a, b) = (b, a % b)
	return a

def prefix_sum(ar):
	return list(accumulate(ar))

def suffix_sum(ar):
	return list(accumulate(ar[::-1]))[::-1]

def N():
	return int(inp())
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]
from collections import defaultdict

def numberOfSetBits(i):
	i = i - (i >> 1 & 1431655765)
	i = (i & 858993459) + (i >> 2 & 858993459)
	return ((i + (i >> 4) & 252645135) * 16843009 & 4294967295) >> 24
from decimal import *

def solve():

	class SegmentTree:

		def __init__(self, data, default=0, func=max):
			self._default = default
			self._func = func
			self._len = len(data)
			self._size = _size = 1 << (self._len - 1).bit_length()
			self.data = [default] * _size + data + [default] * (_size - self._len)
			for i in range(_size - 1, -1, -1):
				self.data[i] = func(self.data[2 * i], self.data[2 * i + 1])

		def __delitem__(self, idx):
			self[idx] = self._default

		def __getitem__(self, idx):
			return self.data[idx + self._size]

		def __setitem__(self, idx, value):
			idx += self._size
			self.data[idx] = value
			idx >>= 1
			while idx:
				self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
				idx >>= 1

		def __len__(self):
			return self._len

		def query(self, start, stop):
			start += self._size
			stop += self._size
			res_left = res_right = self._default
			while start < stop:
				if start & 1:
					res_left = self._func(res_left, self.data[start])
					start += 1
				if stop & 1:
					stop -= 1
					res_right = self._func(self.data[stop], res_right)
				start >>= 1
				stop >>= 1
			return self._func(res_left, res_right)

		def __repr__(self):
			return 'SegmentTree({0})'.format(self.data)
	(n, r) = sep()
	posrat = []
	negrat = []
	for _ in range(n):
		(a, b) = sep()
		if b >= 0:
			posrat.append((a, b))
		else:
			negrat.append((a, b))
	posrat.sort()

	def f(x):
		return x[0] + x[1]
	negrat.sort(reverse=True, key=f)
	cr = r
	for i in posrat:
		(ai, bi) = i
		if ai > cr:
			print('NO')
			return
		else:
			cr += bi
	for i in negrat:
		(ai, bi) = i
		if ai > cr:
			print('NO')
			return
		else:
			cr += bi
	if cr >= 0:
		print('YES')
		return
	print('NO')
solve()


(n, r) = tuple(map(int, input().split()))
(pos, neg) = ([], [])

def cmp(t):
	return t[0] + t[1]
for i in range(n):
	(a, b) = tuple(map(int, input().split()))
	if b < 0:
		if -b > a:
			neg.append((-b, b))
		else:
			neg.append((a, b))
	else:
		pos.append((a, b))
pos.sort()
neg.sort(reverse=True, key=cmp)
taken = 0
for i in range(len(pos)):
	if r >= pos[i][0]:
		r += pos[i][1]
		taken += 1
if taken != len(pos):
	print('NO')
else:
	for i in range(len(neg)):
		if r >= neg[i][0]:
			r += neg[i][1]
			taken += 1
	if taken != n:
		print('NO')
	else:
		print('YES')


l = list(map(int, input().split()))
n = l[0]
r = l[1]
pos = []
neg = []
for _ in range(n):
	l = list(map(int, input().split()))
	if l[1] >= 0:
		pos.append((l[0], l[1]))
	else:
		neg.append((l[0], l[1]))
pos.sort(key=lambda k: k[0])
bad = False
for item in pos:
	if r >= item[0]:
		r += item[1]
	else:
		bad = True
		break
neg.sort(key=lambda k: -(k[0] - r + k[1]))
for item in neg:
	if r >= item[0]:
		r += item[1]
	else:
		bad = True
		break
if r < 0:
	bad = True
print('YES' if bad == False else 'NO')


(n, r) = map(int, input().split())
a = [list(map(int, input().split())) for i in range(n)]
pos = []
neg = []
ans = 0
for x in a:
	if x[1] > 0:
		pos.append(x)
	else:
		neg.append(x)
pos.sort(key=lambda k: k[0])
flag = True
for x in pos:
	if r >= x[0]:
		r += x[1]
		ans += 1
neg.sort(key=lambda i: i[0] + i[1], reverse=True)
arr = [0] * (r + 1)
for i in range(len(neg)):
	for j in range(neg[i][0], r + 1):
		if j + neg[i][1] >= 0:
			arr[j + neg[i][1]] = max(arr[j + neg[i][1]], arr[j] + 1)
ans += max(arr)
print(ans)


def sign(x):
	return (x > 0) - (x < 0)

def key(ab):
	(a, b) = ab
	return (2, -a - b) if b < 0 else (1, a)

def main():
	(n, r) = list(map(int, input().split()))
	for (a, b) in sorted((tuple(map(int, input().split())) for _ in range(n)), key=key):
		if r < a:
			r = -1
			break
		r += b
	if r < 0:
		print('NO')
	else:
		print('YES')
main()


def solve(n, r, projects):
	pos = []
	neg = []
	for (a, b) in projects:
		if b < 0:
			neg.append((a, b))
		else:
			pos.append((a, b))
	pos.sort()
	neg.sort(key=lambda t: -t[0] - t[1])
	for (a, b) in pos:
		if r >= a:
			r += b
		else:
			return False
	for (a, b) in neg:
		if r >= a:
			r += b
			if r < 0:
				return False
		else:
			return False
	return True
(n, r) = map(int, input().split())
projects = []
for i in range(n):
	(a, b) = map(int, input().split())
	projects.append([a, b])
if solve(n, r, projects):
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().split())
pos = []
neg = []
for i in range(n):
	l = list(map(int, input().split()))
	if l[1] >= 0:
		pos.append(l)
	else:
		neg.append(l)
pos.sort()
f = 0
for i in pos:
	if m >= i[0]:
		m += i[1]
	else:
		f = 1
		break
else:
	for i in neg:
		i[0] = i[0] + i[1]
		i[1] = i[0] - i[1]
	neg.sort(reverse=True)
	for i in neg:
		if m >= i[1]:
			m += i[0] - i[1]
		else:
			f = 1
			break
		if m < 0:
			f = 1
			break
if f == 0:
	print('YES')
else:
	print('NO')


def fun():
	(n, r) = list(map(int, input().strip().split()))
	pos = []
	neg = []
	for i in range(n):
		project = list(map(int, input().strip().split()))
		if project[1] >= 0:
			pos.append(project)
		else:
			project[0] = max(project[0], -project[1])
			neg.append(project)
	pos = sorted(pos, key=lambda p: p[0])
	for p in pos:
		if p[0] > r or r < 0:
			return 'NO'
		r += p[1]
	neg = sorted(neg, key=lambda p: p[0] + p[1], reverse=True)
	for p in neg:
		if p[0] > r or r < 0:
			return 'NO'
		r += p[1]
	return 'YES'
print(fun())


(n, r) = map(int, input().split())
arr = [list(map(int, input().split())) for _ in range(n)]

def solve1(cur, arr):
	cnt = 0
	while len(arr) > 0:
		max_inc = -9999
		choose = None
		for (a, b) in arr:
			if cur >= a and max_inc < b:
				max_inc = b
				choose = a
		if choose is None:
			flg = False
			break
		cnt += 1
		cur += max_inc
		arr.remove([choose, max_inc])
	return (cur, cnt)
arr1 = [[x, y] for (x, y) in arr if y >= 0]
arr2 = [[x, y] for (x, y) in arr if y < 0]
(r, cnt) = solve1(r, arr1)
n = len(arr2)
arr2 = [[]] + sorted(arr2, key=lambda x: x[0] + x[1], reverse=True)
dp = [[-1] * (n + 1) for _ in range(n + 1)]
for i in range(n + 1):
	dp[i][0] = r
for i in range(1, n + 1):
	for j in range(1, i + 1):
		dp[i][j] = dp[i - 1][j]
		if dp[i - 1][j - 1] >= arr2[i][0] and dp[i - 1][j - 1] + arr2[i][1] >= 0:
			dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + arr2[i][1])
ans = 0
for j in range(n + 1):
	if dp[n][j] >= 0:
		ans = j
print(ans + cnt)


(n, r) = map(int, input().split())
la1 = []
la2 = []
for i in range(n):
	(a, b) = map(int, input().split())
	if b >= 0:
		la1.append([a, b])
	else:
		la2.append([a + b, a, b])
la1.sort()
la2.sort(reverse=True)
for i in range(len(la1)):
	if r >= la1[i][0]:
		r += la1[i][1]
	else:
		print('NO')
		exit()
for i in range(len(la2)):
	if r >= la2[i][1]:
		r += la2[i][2]
	else:
		print('NO')
		exit()
if r >= 0:
	print('YES')
else:
	print('NO')


from bisect import *
(n, r) = map(int, input().split())
f = []
f1 = []
for i in range(n):
	a = list(map(int, input().split()))
	if a[1] >= 0:
		f.append(a)
	else:
		f1.append([a[0] + a[1], a[0], a[1]])
flag = 0
f.sort()
for i in f:
	if i[0] <= r:
		r += i[1]
		continue
	flag = 1
	break
if flag == 1:
	print('NO')
	exit(0)
f1.sort(reverse=True)
for i in f1:
	if i[1] <= r:
		r += i[2]
		continue
	flag = 1
	break
if flag == 1 or r < 0:
	print('NO')
	exit(0)
print('YES')


from bisect import bisect_right
(n, r) = map(int, input().split())
aa = [0] * n
bb = [0] * n
for i in range(n):
	(aa[i], bb[i]) = map(int, input().split())
ppi = [(aa[i], bb[i]) for i in range(n) if bb[i] >= 0]
ppd = [(max(aa[i], -bb[i]), bb[i]) for i in range(n) if bb[i] < 0]
ppi.sort()
count = 0
for (a, b) in ppi:
	if a > r:
		break
	r += b
	count += 1
ppd.sort(reverse=True, key=lambda p: p[0] + p[1])
dp = [[0] * (r + 1) for _ in range(len(ppd) + 1)]
dp[0][r] = count
for (i, (a, b)) in enumerate(ppd):
	for v in range(r + 1):
		if v >= a and v + b >= 0:
			dp[i + 1][v + b] = max(dp[i + 1][v + b], dp[i][v] + 1)
		dp[i + 1][v] = max(dp[i + 1][v], dp[i][v])
print(max(dp[len(ppd)]))


(n, r) = map(int, input().split())
a = []
for i in range(n):
	a.append([int(j) for j in input().split()])
flag = True
while flag:
	flag = False
	for i in a:
		if r >= i[0] and i[1] >= 0:
			flag = True
			r += i[1]
			a.remove(i)
			break
while a:
	mx = max(a, key=lambda x: x[0] + x[1])
	if r < mx[0]:
		exit(print('NO'))
	r += mx[1]
	a.remove(mx)
	if r < 0:
		exit(print('NO'))
print('YES')


import sys
input = sys.stdin.readline

def getInt():
	return int(input())

def getVars():
	return map(int, input().split())

def getArr():
	return list(map(int, input().split()))

def getStr():
	return input().strip()

def ObrabotkaPlus():
	global PLUS, res, R
	keys = list(PLUS.keys())
	keys.sort()
	for k in keys:
		if k <= R:
			R += sum(PLUS[k])
		else:
			res = 'NO'
			break

def ObrabotkaNull():
	global m0, res, R
	if m0 > R:
		res = 'NO'

def ObrabotkaMinus2():
	global R, res, MINUS
	keys = list(MINUS.keys())
	keys.sort(reverse=True)
	for k in keys:
		for x in MINUS[k]:
			if R < x[0]:
				res = 'NO'
				break
			else:
				R += x[1]
				if R < 0:
					res = 'NO'
					break
		if res == 'NO':
			break
res = 'YES'
(N, R) = getVars()
m0 = 0
PLUS = {}
MINUS = {}
MINUS3 = []
for i in range(N):
	(a, b) = getVars()
	if b > 0:
		if a <= R:
			R += b
		else:
			if a not in PLUS:
				PLUS[a] = []
			PLUS[a].append(b)
	elif b == 0:
		m0 = max(m0, a)
	else:
		key = max(a, -b) + b
		if key not in MINUS:
			MINUS[key] = []
		MINUS[key].append([a, b])
ObrabotkaPlus()
if res == 'YES':
	ObrabotkaNull()
if res == 'YES':
	ObrabotkaMinus2()
print(res)


import collections, heapq, bisect, math

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def solve(r, A):
	r = int(r)
	while A:
		opt = max(range(len(A)), key=lambda x: A[x][1] if r >= A[x][0] else -400)
		if A[opt][0] > r:
			return 'NO'
		if A[opt][1] < 0:
			A.sort(key=lambda x: max(x[0], -x[1]) + x[1], reverse=True)
			for a in A:
				if r < a[0]:
					return 'NO'
				r += a[1]
			return 'YES'
		r += A[opt][1]
		(A[opt], A[-1]) = (A[-1], A[opt])
		A.pop()
	return 'YES'
q = 1
(n, r) = input().split(' ')
tests = []
for test in range(int(n)):
	tests.append([int(p) for p in input().split(' ')])
print(solve(r, tests))


def myFunc(e):
	return e[0] + e[1]
(count, rating) = map(int, input().split())
goodJob = []
badJob = []
for i in range(count):
	(a, b) = map(int, input().split())
	if b >= 0:
		goodJob.append([a, b])
	else:
		badJob.append([a, b])
goodJob.sort()
badJob.sort(reverse=True, key=myFunc)
flag = False
for job in goodJob:
	if job[0] <= rating:
		rating += job[1]
	else:
		flag = True
		break
if not flag:
	for job in badJob:
		if job[0] <= rating:
			rating += job[1]
		else:
			flag = True
			break
if rating < 0:
	flag = True
print('NO') if flag else print('YES')


(n, r) = map(int, input().split())
pt = []
nt = []
r2 = r
result = 0
for _ in range(n):
	(a, b) = map(int, input().split())
	r2 += b
	if b >= 0:
		pt.append((a, b))
	else:
		nt.append((a, b))
pt.sort()
nt.sort(key=lambda t: t[1] + t[0], reverse=True)
for (a, b) in pt:
	if r < a:
		break
	result += 1
	r += b
k = len(nt)
ws = [[0] * (k + 1) for i in range(r + 1)]
for i in range(1, k + 1):
	for w in range(1, r + 1):
		(a, b) = nt[i - 1]
		if w + b >= 0 and a <= r and (ws[r - a][i - 1] >= ws[w][i - 1]):
			ws[w][i] = max(ws[w][i - 1], 1 + ws[w + b][i - 1])
		else:
			ws[w][i] = ws[w][i - 1]
result += ws[r][k]
print(result)


(n, r) = map(int, input().split())
Pos = list()
Neg = list()
for i in range(n):
	(a, b) = map(int, input().split())
	if b < 0:
		Neg.append([a + b, a, b])
	else:
		Pos.append([a, b])
Pos.sort()
for (a, b) in Pos:
	if r < a:
		print('NO')
		exit()
	r += b
Neg = sorted(Neg, reverse=True)
for (_, a, b) in Neg:
	if r < a:
		print('NO')
		exit()
	r += b
print('YES' if r >= 0 else 'NO')


import sys, math, itertools
from collections import Counter, deque, defaultdict
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush, heapify
mod = 10 ** 9 + 7
INF = float('inf')

def inp():
	return int(sys.stdin.readline())

def inpl():
	return list(map(int, sys.stdin.readline().split()))

def inpl_1():
	return list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))

def inps():
	return sys.stdin.readline()

def inpsl(x):
	tmp = sys.stdin.readline()
	return list(tmp[:x])

def err(x):
	print(x)
	exit()
(n, r) = inpl()
pl = []
mi = []
for _ in range(n):
	(a, b) = inpl()
	if b >= 0:
		pl.append((a, b))
	else:
		mi.append([a, b])
pl.sort()
for (a, b) in pl:
	if r < a:
		err('NO')
	r += b
for (i, (a, b)) in enumerate(mi):
	if a + b < 0:
		mi[i][0] = -b
mi.sort(key=lambda x: x[0] + x[1], reverse=True)
for (a, b) in mi:
	if r < a:
		err('NO')
	r += b
print('YES')


(n, r) = map(int, input().split())
a = []
for i in range(n):
	a.append(list(map(int, input().split())))

def sign(x: int):
	if x < 0:
		return -1
	elif x == 0:
		return 0
	else:
		return 1

def comp(x, y):
	if sign(x[1]) == sign(y[1]):
		if x[1] >= 0:
			return x[0] <= y[0]
		else:
			return x[0] + x[1] >= y[0] + y[1]
	else:
		return sign(x[1]) > sign(y[1])
for i in range(n):
	for j in range(i + 1, n):
		if not comp(a[i], a[j]):
			(a[i], a[j]) = (a[j], a[i])
bo = True
for i in range(n):
	if a[i][0] > r:
		bo = False
		break
	r += a[i][1]
	if r < 0:
		bo = False
		break
if bo:
	print('YES')
else:
	print('NO')