# The Indian Job

### Problem Statement :

It is the Indian version of the famous heist “The Italian Job”. N robbers have already broken into the National Museum and are just about to get inside the main vault which is full of jewels. They were lucky that just as they broke into the museum, the guard was leaving the museum for exactly G minutes. But there are other problems too. The main vault has heat sensors that if at any moment of time there are more than two people present in the vault, the alarm goes off.

To collect the jewels, the ith robber needs to be inside the vault for exactly A[i] minutes, 0 <= i < N, in one continuous stretch. As guard will return after G minutes, they have to finish their tasks within G minutes. The robbers want to know if there exists any arrangement such that demands of each robber is satisfied and also they are not caught?

Gotchas
If a robber goes inside the vault at a time "X" and at the same time another robber comes out, it's equivalent to saying they were never in the vault at the same time.
Similarly, when the guard gets inside vault at time G and a robber comes out exactly at time G, the guard will not be able see the robber.

Input Format

The first line contains an integer T denoting the number of testcases. T testcases follow. Each testcase consists of two lines. First line contains two space separated integers denoting N and G denoting the number of thieves and duration for which guard leaves the museum.
The next line contains N space separated numbers where the ith integer, A[i] represents the time the ith robber needs to be in the vault.

Constraints

1 <= T <= 20
1 <= N <= 100
0 <= G <= 1000000 (106)
0 <= A[i] <= 100

Output Format

For each testcase print YES if there exists such an arrangement or NO otherwise in a newline.

### Solution :

Solution in C :

In C++ :

#include <stdio.h>
#include <string.h>

#define NMAX 101
#define VMAX 10001

int A[NMAX];
char ok[VMAX];
int N, G, i, j, sum;

int main() {
int T;
scanf("%d", &T);

while (T--) {
scanf("%d %d", &N, &G);
for (sum = 0, i = 1; i <= N; i++) {
scanf("%d", &(A[i]));
sum += A[i];
}

memset(ok, 0, sizeof(ok));
ok[0] = 1;
for (i = 1; i <= N; i++)
for (j = VMAX - 1 - A[i]; j >= 0; j--)
if (ok[j]) ok[j + A[i]] = 1;

for (i = 0; i < VMAX; i++)
if (ok[i] && i <= G && (sum - i) <= G) {
printf("YES\n");
break;
}

if (i == VMAX)
printf("NO\n");
}

return 0;
}

In Java :

import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Solution {

void solve() throws IOException {
int t=nextInt();
for(int testCase=0;testCase<t;testCase++){
int n=nextInt();
int g=nextInt();
int[] a=new int[n];
int sum=0;
for(int i=0;i<n;i++){
a[i]=nextInt();
sum+=a[i];
}
if(g>=sum){
out.println("YES");
continue;
}
if(sum>2*g){
out.println("NO");
continue;
}
boolean[][] dp=new boolean[n+1][g+1];
dp[0][0]=true;
for(int i=0;i<n;i++){
for(int j=0;j<g;j++)
if(dp[i][j]){
dp[i+1][j]=true;
if(j+a[i]<=g)dp[i+1][j+a[i]]=true;
}
}
int max=0;
for(int i=g;i>=0;i--)
if(dp[n][i]){
max=i;
break;
}
if(sum-max<=g){
out.println("YES");
}
else
out.println("NO");
}
}

public static void main(String[] args) throws IOException {
new Solution().run();
}

void run() throws IOException {
tokenizer = null;
out = new PrintWriter(new OutputStreamWriter(System.out));
//		out = new PrintWriter(new FileWriter("output.txt"));
solve();
out.flush();

}

StringTokenizer tokenizer;
PrintWriter out;

int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}

long nextLong() throws IOException {
return Long.parseLong(nextToken());
}

double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}

String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
}
}
}

In C :

#include <stdio.h>
#include <string.h>

int dp[101][5001];

int main()
{
int T, N;
int G;
int A[100];

scanf("%d", &T);
for(int i=0; i<T; i++)
{
int sum = 0;
int s;
memset(A, 0, sizeof(A));
memset(dp, 0, sizeof(dp));
scanf("%d%d", &N, &G);

for(int j=1; j<=N; j++)
{
scanf("%d", &A[j]);
sum += A[j];
}
dp[0][0] = 1;

/*for(int k1=1; k1<=N; k1++)
for(int k2=k1; k2>=1; k2--)
for(s=1; s<=(sum>>1); s++)
{
if(s >= A[k1] && dp[k2-1][s-A[k1]])
dp[k2][s] = 1;
}

for(int k=2; k<=N; k++)
for(s=1; s<=sum/2; s++)
if(dp[k-1][s])
dp[k][s] = 1; */
for(int k=1; k<=N; k++)
for(s=0; s<=sum/2; s++)
{
if(s >= A[k])
dp[k][s] = dp[k-1][s-A[k]] || dp[k-1][s];
else
dp[k][s] = dp[k-1][s];
}

for(s=(sum>>1); s>=1 && !dp[N][s]; s--) ;
if(sum/2 > s)
s = sum - s;
if(s > G)
printf("NO\n");
else
printf("YES\n");
}
}

In Python3 :

import itertools

def nzerolast(l):
for i in range(len(l)-1, -1, -1):
if l[i] > 0:
return i
return 0

T = int(input())
for _ in range(T):
N, G = map(int, input().split())
L = list(itertools.repeat(0, G + 1))
nl = list(map(int, input().split()))
L[0] = 1
for num in nl:
for g in range(G, num - 1, -1):
L[g] += L[g - num]
can = nzerolast(L)
print("YES" if sum(nl) - can <= G else "NO")

## Minimum Average Waiting Time

Tieu owns a pizza restaurant and he manages it in his own way. While in a normal restaurant, a customer is served by following the first-come, first-served rule, Tieu simply minimizes the average waiting time of his customers. So he gets to decide who is served first, regardless of how sooner or later a person comes. Different kinds of pizzas take different amounts of time to cook. Also, once h

## Merging Communities

People connect with each other in a social network. A connection between Person I and Person J is represented as . When two persons belonging to different communities connect, the net effect is the merger of both communities which I and J belongs to. At the beginning, there are N people representing N communities. Suppose person 1 and 2 connected and later 2 and 3 connected, then ,1 , 2 and 3 w

## Components in a graph

There are 2 * N nodes in an undirected graph, and a number of edges connecting some nodes. In each edge, the first value will be between 1 and N, inclusive. The second node will be between N + 1 and , 2 * N inclusive. Given a list of edges, determine the size of the smallest and largest connected components that have or more nodes. A node can have any number of connections. The highest node valu

## Kundu and Tree

Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that

## Super Maximum Cost Queries

Victoria has a tree, T , consisting of N nodes numbered from 1 to N. Each edge from node Ui to Vi in tree T has an integer weight, Wi. Let's define the cost, C, of a path from some node X to some other node Y as the maximum weight ( W ) for any edge in the unique path from node X to Y node . Victoria wants your help processing Q queries on tree T, where each query contains 2 integers, L and

## Contacts

We're going to make our own Contacts application! The application must perform two types of operations: 1 . add name, where name is a string denoting a contact name. This must store name as a new contact in the application. find partial, where partial is a string denoting a partial name to search the application for. It must count the number of contacts starting partial with and print the co