Problem:
Problem B: Old Bridges Long long ago, there was a thief. Looking for treasures, he was running about all over the world. One day, he heard a rumor that there were islands that had large amount of treasures, so he decided to head for there. Finally he found n islands that had treasures and one island that had nothing. Most of islands had seashore and he can land only on an island which had nothing. He walked around the island and found that there was an old bridge between this island and each of all other n islands. He tries to visit all islands one by one and pick all the treasures up. Since he is afraid to be stolen, he visits with bringing all treasures that he has picked up. He is a strong man and can bring all the treasures at a time, but the old bridges will break if he cross it with taking certain or more amount of treasures. Please write a program that judges if he can collect all the treasures and can be back to the island where he land on by properly selecting an order of his visit. Input Input consists of several datasets. The first line of each dataset contains an integer n .
Next n lines represents information of the islands. Each line has two integers, which means the amount of treasures of the island and the maximal amount that he can take when he crosses the bridge to the islands, respectively. The end of input is represented by a case with n = 0. Output For each dataset, if he can collect all the treasures and can be back, print " Yes " Otherwise print " No " Constraints  1  ≤ n ≤ 25  Sample Input 3
2 3
3 6
1 2
3
2 3
3 5
1 2
0 Output for the Sample Input Yes
No

Solution (Python):



while True:
	n = int(input())
	if n == 0: break
	tbl = [list(map(int, input().split())) for i in range(n)]
	tbl.sort(key=lambda x:(x[1],x[0]))
	s, f = 0, True
	for v, w in tbl:
		s += v
		if s > w:
			f = False
			break
	print("Yes" if f else "No")

Solution (Python):



while True:
    n = int(raw_input())
    island = []
    if n == 0:
        break
    for i in range(n):
        g,b = map(int,raw_input().split())
        island.append((g,b))
    island = sorted(island,key = lambda x:(x[1],x[0]))
    
    gem = 0
    for g,b in island:
        gem += g
        if b < gem:
            break
    else:
        print 'Yes'
        continue
    print 'No'

Solution (Python):
while 1:
	n = int(raw_input())
	if n == 0:
		break
	land = [map(int,raw_input().split(" ")) for i in range(n)]
	land.sort(key = lambda x:(x[1],x[0]))
	gold = 0
	for i in range(n):
		gold += land[i][0]
		if gold > land[i][1]:
			print "No"
			break
	else:
		print "Yes"

Solution (Python):
while True:
    n = input()
    if n == 0: break
    info = []
    for i in range(n):
        lu, w = map(int, raw_input().split())
        info.append((w, lu))
    info.sort()
    lu_sum = 0
    flag = True
    for w, lu in info:
        lu_sum += lu
        if lu_sum > w:
            flag = False
    print("Yes" if flag else "No")

Solution (Python):
def solve(info):
    have = 0
    for m, w in info:
        have += w
        if m < have:
            return "No"
    return "Yes"

while True:
    shima_count = input()
    if shima_count == 0: break
    info = []
    for i in range(shima_count):
        lu, w = map(int, raw_input().split())
        info.append((w, lu))
    info.sort()
    print solve(info)

Solution (Python):
while 1:
    n=int(input())
    if n==0:break
    a=[tuple(map(int,reversed(input().split()))) for _ in range(n)]
    a.sort()
    w=0
    for x,y in a:
        w+=y
        if x<w:print('No');break
    else:print('Yes')

Solution (Python):
while True:
    n = int(input())
    if n == 0:
        break
    bridges = []
    for _ in range(n):
        t, b = map(int, input().split())
        bridges.append((t, b))
    bridges.sort(key = lambda x: x[1])

    flag = True
    sumt = 0
    for t, b in bridges:
        sumt += t
        if sumt > b:
            flag = False
            break
    
    if flag:
        print('Yes')
    else:
        print('No')

Solution (Python):
while(1):
    n=int(raw_input())
    if n==0: break
    A=[[0,0] for i in range(n)] 
    for i in range(n):
        [a,b]=map(int,raw_input().split())
        A[i]=[a,b]
    A.sort(key= lambda x: x[1])
    s=0
    for i in range(n):
        if s+A[i][0]>A[i][1]:
            print 'No'
            break
        else:
            s+=A[i][0]
    else:
        print 'Yes'

Solution (Python):
while True:
    n = int(input())
    if n == 0:
        break
    z = sorted([tuple(map(int, input().split())) for _ in range(n)],  key=lambda x: x[1])
    cnt = total = 0
    for a, b in z:
        total += a
        if total > b:
            break
        cnt += 1
    print("Yes" if cnt == n else "No")

Solution (Python):
while True:
    n = int(input())
    if n == 0:
        break
    z = sorted([tuple(map(int, input().split())) for _ in range(n)],  key=lambda x: x[1])
    cnt = total = 0
    for a, b in z:
        total += a
        if total > b:
            cnt = 1
            break
    print("No" if cnt else "Yes")

Solution (Python):
while True:
    n = int(input())
    if n == 0:
        break
    z = sorted([tuple(map(int, input().split())) for _ in range(n)],  key=lambda x: x[1])
    total = 0
    for a, b in z:
        total += a
        if total > b:
            print("No")
            break
    else:
        print("Yes")

Solution (Python):
from sys import exit
from functools import reduce
from operator import add
def f(): return [int(i) for i in input().split()]

while True:
    flag = False
    n = int(input())
    if not(n):
        exit()
    islands = []
    for i in range(n):
        islands.append(f())
    islands.sort(key=lambda x:x[1])
    
    weight = 0
    for j in range(n):
        weight += islands[j][0]
        if weight > islands[j][1]:
            flag = True
            print("No")
            break
    if not(flag):
        print("Yes")

Solution (Python):
def sky(x):
    return x[1]


while True:
    n = int(input())
    if n == 0:
        break
    
    arr = [[] for i in range(n)]
    sumh = 0
    for i in range(n):
        arr[i] = list(map(int, input().split()))
    arr.sort(key=sky)
    res = True
    for e in arr:
        sumh += e[0]
        if sumh > e[1]:
            res = False
            break
    if res:
        print("Yes")
    else:
        print("No")

Solution (Python):
def solve(l):
	have = 0
	for m, w in l:
		have += w
		if m < have:
			return "No"
	return "Yes"
while True:
	n = input()
	if n == 0: break
	l = [tuple(reversed(map(int, raw_input().split()))) for i in xrange(n)]
	l.sort()
	print solve(l)

Solution (Python):
while 1:
    N = int(input())
    if N == 0:
        break
    P = [list(map(int, input().split())) for i in range(N)]
    P.sort(key=lambda x: (-x[1], x[0]))
    su = sum(w for w, s in P)
    for w, s in P:
        if s < su:
            print("No")
            break
        su -= w
    else:
        print("Yes")

Solution (Python):
while True:
  n = int(input())
  if n == 0:
    break
  lst = sorted([list(map(int, input().split())) for _ in range(n)], key=lambda x:x[-1])
  acc = 0
  for w, b in lst:
    acc += w
    if acc > b:
      print("No")
      break
  else:
    print("Yes")