Wednesday, April 24, 2019

Little Elephant and Candies

Little Elephant and Candies 

Intro 

The link to the problem is here

Problem Statement 

A Little Elephant and his friends from the Zoo of Lviv like candies very much.
There are N elephants in the Zoo. The elephant with number K (1KN) will be happy if he receives at least AK candies. There are C candies in all in the Zoo.
The Zoo staff is interested in knowing whether it is possible to make all the N elephants happy by giving each elephant at least as many candies as he wants, that is, the Kth elephant should receive at least AK candies. Each candy can be given to only one elephant. Print Yes if it is possible and No otherwise.

Input

The first line of the input file contains an integer T, the number of test cases. T test cases follow. Each test case consists of exactly 2 lines. The first line of each test case contains two space separated integers N and C, the total number of elephants and the total number of candies in the Zoo respectively. The second line contains N space separated integers A1, A2, ..., AN.

Output

For each test case output exactly one line containing the string Yes if it possible to make all elephants happy and the string No otherwise. Output is case sensitive. So do not print YES or yes.

Constraints

1T1000
1N100
1C109
1AK10000, for K = 1, 2, ..., N

Example

Input:
2
2 3
1 1
3 7
4 2 2

Output:
Yes
No

Explanation

Case 1. We can give one candy to the first elephant and two candies to the second elephant and make them both happy. Hence the answer is Yes. Alternatively we can give one candy to each elephant and left one candy for ourselves but they again will be happy.
Case 2. Even if we give four candies to the first elephant and two candies to the second elephant we will have only one candy left and can not make last elephant happy since he needs two candies for his happiness. Hence the answer is No.


 Problem Solving

  • Since each elephant will be happy only when it receives Ak candies therefore the total candies required is a sum of all Ak for (1<k<N) and this sum should be less than C. 
  • From coding perspective all limits are trivial and if int is 4 bytes than all datatypes can be ints
  • But we need to think about integer overflow , since all A can be max 1000 and there can be max of 1000(N) of them  therefore the summation cannot be more than 10^6 which can also be accommodated in an unsigned int, so extra care required. 
  • Okay the submission went successful, here.

 Learning

  • There are few learning from this submission
  • First cin is slower than scanf which is slower than getchar which is slower than getchar_unlocked. Read about it here.  
  • One would be tempted to early exit when required candies are more than the C at input is yet to be processed. However this is futile as you anyways have to read all the inputs otherwise that input will be used by next test case. 
  • Also we avoided the use of array altogether by just summing up the numbers as they became available.

1 comment:

  1. a = int(input())
    for i in range(a):
    n,c = list(map(int,input().split()))
    b=list(map(int,input().split()))
    s=0
    for i in b:
    s=s+i
    k=sum(b)
    if s<=(c):
    print("Yes")
    else:
    print("No")

    ReplyDelete