Chef And Work :) (Codechef August Cook-Off Contest Problem)

Question:- 

Chef has  small boxes arranged on a line from  to . For each valid , the weight of the -th box is . Chef wants to bring them to his home, which is at the position . He can hold any number of boxes at the same time; however, the total weight of the boxes he's holding must not exceed K at any time, and he can only pick the i-th box if all the boxes between Chef's home and the i-th box have been either moved or picked up in this trip.

Therefore, Chef will pick up boxes and carry them home in one or more round trips. Find the smallest number of round trips he needs or determine that he cannot bring all boxes home.

Input

  • The first line of the input contains a single integer  denoting the number of test cases. The description of  test cases follows.
  • The first line of each test case contains two space-separated integers  and .
  • The second line contains  space-separated integers .

Output

For each test case, print a single line containing one integer ― the smallest number of round trips or  if it is impossible for Chef to bring all boxes home.

Constraints



  •  for each valid 

Example Input

4
1 1 
2
2 4
1 1
3 6
3 4 2
3 6
3 4 3

Example Output

-1
1
2
3

Explanation

Example case 1: Since the weight of the box higher than , Chef can not carry that box home in any number of the round trip.

Example case 2: Since the sum of weights of both boxes is less than , Chef can carry them home in one round trip.

Example case 3: In the first round trip, Chef can only pick up the box at position . In the second round trip, he can pick up both remaining boxes at positions  and .

Example case 4: Chef can only carry one box at a time, so three round trips are required

Python Code:-

for _ in range(int(input())):
  n,k=map(int,input().split())
  w=list(map(int,input().split()))
  count=0
  flag=1
  for i in range(n):
    if w[i]>k:
      flag=0
      break
    elif count+w[i]<=k:
      count+=w[i]
    else:
      flag+=1
      count=w[i]
  if flag==0:
    print("-1")
  else:
    print(flag)



Comments

Popular posts from this blog

Machine :) (Codechef Contest Problem)

Bowling Strategy :) (Codechef Contest Problem)