Problem:


Score: 400 points

Problem Statement

Kizahashi, who was appointed as the administrator of ABC at National Problem Workshop in the Kingdom of AtCoder, got too excited and took on too many jobs.

Let the current time be time 0. Kizahashi has N jobs numbered 1 to N.

It takes A_i units of time for Kizahashi to complete Job i. The deadline for Job i is time B_i, and he must complete the job before or at this time.

Kizahashi cannot work on two or more jobs simultaneously, but when he completes a job, he can start working on another immediately.

Can Kizahashi complete all the jobs in time? If he can, print Yes; if he cannot, print No.

Constraints

All values in input are integers.

1 \leq N \leq 2 \times 10^5

1 \leq A_i, B_i \leq 10^9 (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
.
.
.
A_N B_N

Output

If Kizahashi can complete all the jobs in time, print Yes; if he cannot, print No.

Sample Input 1

5
2 4
1 9
1 8
4 9
3 12

Sample Output 1

Yes

He can complete all the jobs in time by, for example, doing them in the following order:

Do Job 2 from time 0 to 1.

Do Job 1 from time 1 to 3.

Do Job 4 from time 3 to 7.

Do Job 3 from time 7 to 8.

Do Job 5 from time 8 to 11.

Note that it is fine to complete Job 3 exactly at the deadline, time 8.

Sample Input 2

3
334 1000
334 1000
334 1000

Sample Output 2

No

He cannot complete all the jobs in time, no matter what order he does them in.

Sample Input 3

30
384 8895
1725 9791
170 1024
4 11105
2 6
578 1815
702 3352
143 5141
1420 6980
24 1602
849 999
76 7586
85 5570
444 4991
719 11090
470 10708
1137 4547
455 9003
110 9901
15 8578
368 3692
104 1286
3 4
366 12143
7 6649
610 2374
152 7324
4 7042
292 11386
334 5720

Sample Output 3

Yes



Sample input:
5
2 4
1 9
1 8
4 9
3 12


Sample output:
Yes

Submission status: Runtime Error

Solution code:
import sys

N = int(input())
ABList = []
for i in range(N):
    ABList.append(sys.stdin.readline().split())
sABList = sorted(ABList, key=[1])
temp = 0
isYes = True
for i in reversed(range(0, len(sABList))):
    temp += int(sABList[i][0])
    if temp > int(sABList[i][1]):
        isYes = False
print(sABList)
print(["No", "Yes"][isYes])

Submission status: Runtime Error

Solution code:
def main():
    n = int(input())
    work = []
    for i in range(n):
        a, b = map(int, input().split())
        #(順番、単位時間、締め切り)
        work.append((i,a,b))

    work.sort(key= lambda x:x[2]
    ans = 0
    for i in range(n):
        ans += work[n][1]
        if ans < work[n][2]:
            print("Yes")
        else:
            print("No")

main()

Submission status: Runtime Error

Solution code:
import numpy as np

N = int(input())  

A = np.zeros(N) 
B = np.zeros(N) 

for i in range(N):
    A[i], B[i] = map(int, input().split())

a = 0
for i in range(1000000):
    C = np.random.permutation(np.arange(N))
    k = 0
    a = 0
    b = 0
    for j in range(N):
        k += A[C[i]]
        if  k > B[i]:
            a = 1
            
    if a == 0:
        b = 1
        print('Yes')
        break
    
if b == 0:
    print('No')

Submission status: Runtime Error

Solution code:
n=int(input())
a=[]
e=[]
f=[]
g={}
ans='Yes'
for i in range(n):
    c,d=map(int,input().split())
    a.append([c,d,d-c])

b=sorted(a, key=lambda x: x[2])
for i in range(len(b)):
    if b[i][2]+b[i][0]>b[i+1][2]:
        ans='No'
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
def func(A):
    if A.shape[0]==1:
        if A[0][0] >= A[0][1]:
            return 1
        else:
            return 0
    A_N = A[-1][1]
    B_N = A[-1][0]
    if B_N - A_N < 0:
        return 0
    A = A[:-1]
    A[:,0][A[:,0]>=B_N-A_N]=B_N-A_N
    return func(A)

N = int(input())
ab = np.zeros([N,2])

for i in range(N):
    ab[i][1],ab[i][0] = map(int,input().split())
ab = ab.tolist()
ab = sorted(ab)
ab = np.array(ab)
ans = func(ab)
if ans == 1:
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
N=int(input())
A=[]
B=[]
len=B.len
str='Yes'
total=0
time=0
for i in range(N):
    a,b = map(int, input().split())
    A.append(a)
    B.append(b)
B.sort()
AB=B+A
time=min(B)
for j in range(N):
    time += AB[j]
    total +=AB[j+len]
    if total>time:
        str='No'
print(str)

Submission status: Runtime Error

Solution code:
import sys
N=int(input())
number = []
for i in range(N):
    S, P = input().split(" ")
    number.append([S,P])


number.sort(key=lambda x: x[1], reverse=True)
number.sort(key=lambda x: x[0])

sum=0
for i in range(N):
    #print(number[i][0])
    sum+=number[i][0]
    if(sum>number[i][1]):
        print("No")
        sys.exit()
print("Yes")

Submission status: Runtime Error

Solution code:
import bisect
import sys
input = sys.stdin.readline()

n = int(input())

a, b = map(int, input().split())
task_lis = [[b, a]]

for i in range(n-1):
    a, b = map(int, input().split())
    bisect.insort_right(task_lis,[b, a])

sum_a = 0
ch_over = 1

for i in range(n):
    sum_a += task_lis[i][1]
    if task_lis[i][0] < sum_a:
        ch_over = 0
        break

if ch_over == 0:
    print("No")
else:
    print("Yes")

Submission status: Runtime Error

Solution code:
from operator import itemgetter

n = int(input())
m = []
for i in range(n):
    m.append([int(x) for x in input().split()])
m2 = sorted(m, key=itemgetter(1))
while len(m2) > 0:
    s = 0
    for x in m2:
        s += x[0]
    if s > m2[-1][1]:
        print("No")
        sys.exit(0)
    m2.pop()
print("Yes")

Submission status: Runtime Error

Solution code:
import numpy as np
def func(A):
    if A.shape[0]==1:
        if A[0][0] >= A[0][1]:
            return 1
        else:
            return 0
    A_N = A[-1][1]
    B_N = A[-1][0]
    A = A[:-1]
    A[:,0][A[:,0]>=B_N-A_N]=B_N-A_N
    return func(A)

N = int(input())
ab = np.zeros([N,2])

for i in range(N):
    ab[i][1],ab[i][0] = map(int,input().split())
ab = ab.tolist()
ab = sorted(ab)
ab = np.array(ab)
ans = func(ab)
if ans == 1:
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
N = int(input())

a_b_lis = [0] * N

for i in range(N):
    a_b_lis[i] = list(map(int, input().split()))

count = 0
a_b_lis = sorted(a_b_lis, key=lambda x: x[1], x[0])

on_time = True
for item in a_b_lis:
    if item[0] + count <= item[1]:
        count += item[0]
        on_time = True
    else:
        on_time = False

if on_time:
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
N = int(input())

a_b_lis = [0] * N

for i in range(N):
    a_b_lis[i] = list(map(int, input().split()))

count = 0
a_b_lis = sorted(a_b_lis, key=lambda x: x[1], X[0])

on_time = True
for item in a_b_lis:
    if item[0] + count <= item[1]:
        count += item[0]
        on_time = True
    else:
        on_time = False

if on_time:
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
import numpy as np

n = int(input())
lists = []
limit = []
cost = []
for i in range(n):
    a, b = (map(int,input().split()))
    lists.append([a,b,True])


def dfs(que, sum_cost, in_list):
    if que == n:
        print('Yes')
        exit(0)
    for i in range(n):
        if lists[i][2]:
            sum_cost_out = sum_cost + lists[i][0]
            if sum_cost_out <= lists[i][1]:
                out_list = in_list[:]
                out_list[i][2] = False
                dfs(que+1,sum_cost_out,out_list)


dfs(0,0,lists)
print('No')

Submission status: Runtime Error

Solution code:
N = int(input())
AB = [set(map(int, input().split())) for _ in range(N)]

AB.sort(key=lambda x:(x[1],x[0]), reverse=True)
time = 0
for A, B in AB:
    time += A
    if B > time:
        print("No")
else:
    print("Yes")

Submission status: Runtime Error

Solution code:
A = int(input())
worktime = []

for i in range(A):
    C, D = map(str, input().split())
    v = [D, C]
    worktime.append(v)

now = 0
worktime.sort()
cnt = 0
for lim, use in worktime:
    if now+int(lim) < int(use):
        print('No')
        cnt += 1
        break
    else:
        now += use

if cnt == 0:
    print('Yes')

Submission status: Runtime Error

Solution code:
num1 = 2
num2 = 3
chk = 0
N,K =map(int,input().split())
if K > (N-1)*(N-2)//2:
	print(-1)
else:
	for i in range(2,N+1):
		print("1"+" "+str(i))
	left = (N-1)*(N-2)//2 - K
	for i in range(left):
			if N >= num2 :
				if chk == 1:
					num2 += 1
					chk = 0
				print(str(num1) + " " + str(num2))
				num2 += 1
			else:
				num1 +=1
				num2 = num1+1
				print(str(num1) + " " + str(num2))
				chk = 1

Submission status: Runtime Error

Solution code:
N = input().strip()
N = int(N)
dic = collections.defaultdict(lambda: 0)

for i in range(N):
    x,y = input().strip().split(" ")
    x = int(x)
    y = int(y)

    dic[y] += x

prev_time = 0
result = "Yes"

for k,v in dic.items():
    if v > k:
        result = "No"
        break
    prev_time += v

print(result)

Submission status: Runtime Error

Solution code:
import numpy as np

n = int(input())
lists = []
limit = []
cost = []
for i in range(n):
    a, b = (map(int,input().split()))
    lists.append([a,b,True])

lists.sort(key=lambda x:x[1])

def dfs(que, sum_cost, in_list):
    if que == n:
        print('Yes')
        exit(0)
    for i in range(n):
        if lists[i][2]:
            sum_cost_out = sum_cost + lists[i][0]
            if sum_cost_out <= lists[i][1]:
                out_list = in_list[:]
                out_list[i][2] = False
                dfs(que+1,sum_cost_out,out_list)

dfs(0,0,lists)
print('No')

Submission status: Runtime Error

Solution code:
N = int(input())
A_B = [list(map(int,input().split())) for i in range(N)]
A_B = sorted(A_B,key=itemgetter(1))
#print(A_B)
s=0
for a_b in A_B:
  s+=a_b[0]
  lim = a_b[1]
  if s > lim:
    print("No")
    exit()
print("Yes")

Submission status: Runtime Error

Solution code:
N = int(input())
data = [map(int, input().split()) for i in range(N)]

ds = sorted(data, key=lambda x: x[1])

now = 0

for i in range(N) :
  if now + ds[i][0] > ds[i][1] :
    print("No")
    break
  else :
    now = now + ds[0]
  
  if i == N-1 :
    print("Yes)

Submission status: Runtime Error

Solution code:
C:\Users\ryo-2\Anaconda3\python.exe C:/Users/ryo-2/PycharmProjects/atcoder/ABC131/ABC131_D.py
30
384 8895
1725 9791
170 1024
4 11105
2 6
578 1815
702 3352
143 5141
1420 6980
24 1602
849 999
76 7586
85 5570
444 4991
719 11090
470 10708
1137 4547
455 9003
110 9901
15 8578
368 3692
104 1286
3 4
366 12143
7 6649
610 2374
152 7324
4 7042
292 11386
334 5720
Yes

Process finished with exit code 0

Submission status: Runtime Error

Solution code:
import fractions
from math import ceil

def lcm(a, b):
  return a * b // fractions.gcd(a, b)

A, B, C, D = map(int, input().split())

rng = B - A

count = B - A + 1

if A <= ceil(A / C) * C and ceil(A / C) * C <= B:
  count -= max(B - ceil(A / C) * C, 0) // C + 1

if A <= ceil(A / D) * D and ceil(A / D) * D <= B:
  count -= max(B - ceil(A / D) * D, 0) // D + 1

C_D_lcm = lcm(C, D)

if A <= ceil(A / C_D_lcm) * C_D_lcm and ceil(A / C_D_lcm) * C_D_lcm <= B:
  count += max(B - ceil(A / C_D_lcm) * C_D_lcm, 0) // C_D_lcm + 1

print(count)

Submission status: Runtime Error

Solution code:
N = int(input())
job = []
for i in range(N):
    A,B = map(int,input().split())
    job.append([B,A])
job = sorted(job)time = 0
flag = 0
for i in job:
    time += i[1]
    if i[0] < time:
        print('No')
        flag = 1
        break
        
if flag == 0:
    print('Yes')

Submission status: Runtime Error

Solution code:
import sys
n = int(input())
list = []
for i in range(n):
  if a > b:
    print('No')
    sys.exit()
  a,b = map(int, input().split())
  list += [(b,a)]
  
list.sort()
for i,ele in enumerate(list):
  if sum([a[1] for a in list[:i]]) > list[i][0]:
    print('No')
    sys.exit()
print('Yes')

Submission status: Runtime Error

Solution code:
import os, sys, re, math

N = int(input())

#memo = [0 for _ in range(int(2*1e5)+1)]
memo = [0 for _ in range(2*int(1e5)+100)]

for _ in range(N):
	A,B = [int(s) for s in input().split(' ')]
	memo[B] += A

s = 0
flg = True
for i in range(len(memo)):
	s += memo[i]
	if s > i:
		flg = False
		break
if flg:
	print('Yes')
else:
	print('No')

Submission status: Runtime Error

Solution code:
n=int(input())
a=[]
for i in range(n):
  m,n=(int(i) for i in input().split())
  a.append([n,m])
a.sort()
r="Yes"
s=0
for i in range(n):
  s=s+a[i][1]
  if s>a[i][0]:
    r="No"
    break
print(r)

Submission status: Runtime Error

Solution code:
n = int(input())
a_b_list = [list(map(int,input().split())) for x in range(n)]
a_b_list_kai = [[int(a_b_list[x]),int(a_b_list[x+1])] for x in range(0,n*2,2)]
sorted(a_b_list_kai,key = lambda x:x[1])
limit = 0
result = 0
sum_task = 0
for x in a_b_list_kai:
    sum_task += x[0]
    limit = max(limit,x[1])
    if sum_task > limit:
        result = 1

if result == 1:
    print('No')
else:
    print('Yes')

Submission status: Runtime Error

Solution code:
import pandas as pd
import sys

n = int(input())

jobs = pd.DataFrame(columns=["cost", "deadline"])

for i in range(n):
    a, b = map(int, input().split(' '))
    jobs.loc[len(jobs)] = [a, b]

jobs = jobs.sort_values("deadline")

sum = 0
for i in range(n):
    sum += jobs.iat[i, 0]
    if sum > jobs.iat[i, 1]:
        print('No')
        sys.exit()

print('Yes')

Submission status: Runtime Error

Solution code:
#include<stdio.h>
#include<vector>
#include<algorithm>

struct Task {
	int time;
  	int limit;
};

int main()
{
    int n;
    scanf("%d", &n);
  
  	//struct Task tasks[n];
    std::vector<Task> tasks;
  
  	for(int i=0; i<n; i++){
      struct Task newtask;
      scanf("%d %d", &newtask.time, &newtask.limit);
      tasks.push_back(newtask);
    }
  
  std::sort(tasks.begin(), tasks.end(), [](const Task& x, const Task& y) { return x.limit < y.limit;});
  
  int nowtime = 0;
  for(int i=0; i<n; i++){
      nowtime += tasks[i].time;
    	if(nowtime > tasks[i].limit){
         printf("No");
          return 0;
        }
    }
  printf("Yes");
  return 0;
}

Submission status: Runtime Error

Solution code:
N = int(input())
l = [None]*N
s = []
for i in range(N):
    k = list(map(int,input().split()))
    if k[0]==k[1]:
        s.append(k)
    else:
        l[i] = k

if len(s)>1:
    print("No")
else:
    l = sorted(l, key=lambda x: x[1])
    cnt = 0
    if len(s)==1:
        cnt += s[0][1]

    flag = True
    for j in l:
        if j==None:
            continue
        else:
            cnt += j[0]
        if cnt>j[1]:
            print("No")
            flag = False
            break
    if flag:
        print("Yes")

Submission status: Runtime Error

Solution code:
a, b, c, d = map(int, input().split())

import fractions
import math

x = int(int(b//c) - int(math.ceil(a/c)) + 1)
y = int(int(b//d) - int(math.ceil(a/d)) + 1)
e = int(int(c*d)//int(fractions.gcd(c,d)))
z = int(int(b//e) - int(math.ceil(a/e)) + 1)

print(int(b-a+1-x-y+z))

Submission status: Runtime Error

Solution code:
N = int(input())
l = [None]*N
s = []
for i in range(N):
    k = list(map(int,input().split()))
    if k[0]==k[1]:
        s.append(k)
    else:
        l[i] = k

if len(s)>1:
    print("No")
else:
    l = sorted(l, key=lambda x: x[1])
    cnt = 0
    if len(k)==1:
        cnt += s[0][1]

    flag = True
    for j in l:
        if j==None:
            continue
        else:
            cnt += j[0]
        if cnt>j[1]:
            print("No")
            flag = False
            break
    if flag:
        print("Yes")

Submission status: Runtime Error

Solution code:
    n = int(input())
    tasks = []
    for i in range(n):
        a,b = map(int, input().split())
        tasks.append((a,b))

    tasks.sort(key=lambda x:x[1])
    #print(tasks)

    cur = 0
    c = True
    for i in range(n):
        cur += tasks[i][0]
        if cur > tasks[i][1]:
            c = False

    print("Yes" if c else "No")

Submission status: Runtime Error

Solution code:
import numpy as np
N =int(input())
A =[]
B =[]
for i in range(N):
    a,b = map(int,input().split())
    A.append(a)
    B.append(b)
index = np.argsort(B)
time = 0
f = 0
for i in index:
    time += A[index]
    if time>B[index]:
        f = 1
        break
if f == 1:
    print('No')
else:
    print('Yes')

Submission status: Runtime Error

Solution code:

def code_for_D(test=None):

    N = int(input())

    works = [map(int, input().split()) for _ in range(N)]
    works = sorted(works, key=lambda x: x[1])

    time_now = 0
    time_by = 0
    answer = 'Yes'

    for i in range(N):
        time_now += works[0]
        time_by = works[1]
        
        if time_now > time_by:
            answer = 'No'
        
    print(answer)


def main():
    code_for_C()


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def main():
    N = int(input())
    A, B = [], []
    AB = []
    for i in range(N):
        a, b = map(int, input().split())
        A.append(a)
        B.append(b)
        AB.append([a, b])

    AB.sort(key=lambda x: x[1])

    time = 0
    while (len(AB) > 0):
        flag = True
        a, b = AB
        if time + a <= b:
            time += a
            AB.pop(0)
            flag = False
        if flag:
            break
    if len(AB) == 0:
        print('Yes')
    else:
        print('No')

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
A, B, C, D = map(int, input().split())

Cprimes = {}
Dprimes = {}

tmpC = C
d = 2
while tmpC > 1:
    if tmpC % d == 0:
        if d in Cprimes:
            Cprimes[d] += 1
        else:
            Cprimes[d] = 1
        tmpC //= d
    else:
        d += 1

tmpD = D
d = 2
while tmpD > 1:
    if tmpD % d == 0:
        if d in Dprimes:
            Dprimes[d] += 1
        else:
            Dprimes[d] = 1
        tmpD //= d
    else:
        d += 1

comPrimes = {}
for k in Cprimes:
    comPrimes[k] = Cprimes[k]
for k in Dprimes:
    if k in comPrimes:
        if comPrimes[k] < Dprimes[k]:
            comPrimes[k] = Dprimes[k]
    else:
        comPrimes[k] = Dprimes[k]

comVal = 1
for k in comPrimes:
    comVal *= k**comPrimes[k]

#print(Cprimes)
#print(Dprimes)
#print(comPrimes)
#print(comVal)

CdivNum = B//C - (A-1)//C
DdivNum = B//D - (A-1)//D
comDivNum = B//comVal - (A-1)//comVal
ans = B-(A-1)
ans -= CdivNum
ans -= DdivNum
ans += comDivNum

print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
AB = [[int(c) for c in input().split()] for _ in range(N)]

s=0
possible=True
for a, b in sorted(AB, key=lambda x:x[1]):
    s+=a
    if s>b:
	possible = False
        break

print('Yes' if possible else 'No')

Submission status: Runtime Error

Solution code:
n = int(input())
tasks = []

for _ in range(n):
    c,d = map(int,input().split())
    tasks.append([d,c])
tasks.sort()
maxday = tasks[-1][0]
cul = [0] * maxday
for i in range(n):
    day = tasks[i][0]
    cost = tasks[i][1]
    if day - sum(cul[0:day+1]) < cost :
        print("No")
        exit()
    else:
        j = 0
        k = 0
        while k < cost:
            if cul[j] == 0:
                cul[j] = 1
                k += 1

            j += 1
    #print(cul)
print("Yes")

Submission status: Runtime Error

Solution code:
N = input()
N = int(N)
s = [[ int(i) for i in input().split()] for i in range(N)]
hoge = "True"
s = sorted(s, key=lambda s:s[1])
for i in s:
    time += s[0]
    if time > s[1]:
        hoge = "No" 
        break

print(hoge)

Submission status: Runtime Error

Solution code:
import fractions
a,b,c,d = map(int,input().split())
cd = c*d//fractions.gcd(c,d)
e = a-1
f = (b//c + b//d - b//(cd)) - (e//c + e//d - e//(cd))
print(b-e-f)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

const int MOD = 1e9 + 7;

const int MAXN = 1e5 + 10;

LL ans = 0;
vector<int> g[MAXN];
int n; 

int cnt[MAXN][20][2] = {0};
int A[MAXN];

void dfs(int u, int p) {
  for (int i = 0; i < 20; ++i) ++cnt[u][i][(A[u]>>i)&1];
  for (int v : g[u]) {
    if (v == p) continue;
    dfs(v, u);
    for (int b : {0, 1}) for (int i = 0; i < 20; ++i) 
      cnt[u][i][b^(A[u]>>i&1)] += cnt[v][i][b];
  }
  for (int v : g[u]) {
    if (v == p) continue;
    for (int b : {0, 1}) for (int i = 0; i < 20; ++i) {
      ans += (1ll<<i) * (cnt[u][i][b] - cnt[v][i][b^(A[u]>>i&1)]) * cnt[v][i][!b];
    }
  }
}


int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i) cin >> A[i];
  for (int i = 0; i < n - 1; ++i) {
    int u, v; cin >> u >> v;
    --u, --v;
    g[u].push_back(v), g[v].push_back(u);
  }
  dfs(0, 0);
  cout << ans / 2 + accumulate(A, A + n, 0ll) << '\n';
}

Submission status: Runtime Error

Solution code:
for i in range(n):
    x,y=map(int,input().split())
    l.append([x,y])

l=sorted(l,key=lambda x:x[1])
t=0
ans='Yes'
for i in range(n):
    if t+l[i][0]<=l[i][1]:
        t+=l[i][0]
    else:
        ans='No'
print(ans)