Marut and Money Collection

4.4

7 votes
Problem

Marut is an engineering student and usually stays awake in the night. Marut is now hungry and wants to eat something but he does not have enough money. So he decides to borrow money from his friends. He goes to all his friends and asks for a certain amount of money. Other students have money in form of notes and they have only one note of some money.

Apparently, like Marut, many students are struggling with money and they also ask from their friends to borrow them some money. Students are numbered from 1 to N.

Given a list of friends and a student S, your task is to print YES, if S can collect M money, otherwise NO for Q queries.

Input: First line contains an integer N , denoting the number of students. Next line contains N single space separated integers Mi, ith integer denotes amount of money which ith student has. Next line contains an integer, L denoting the length of list of friends. Then follow L lines, each line contains two single space separated integer X and Y , denoting that X and Y are friends of eachother. Next line contains an integer Q , denoting the number of queries. In the next Q lines, there are two integers S and M,separated by single space, denoting student and money respectively.

Output: Print YES, if S can collect exactly M money , otherwise NO for Q queries in new line.

Constraints: 1 <= N <= 100 1 <= Mi <= 1000 0 <= L <= (N x (N+1))/2 1 <= X, Y <= N 1 <= S <= N 1 <= M<= 1000 1 <= Q <= 100000

Subtask: 1 1 <= N <= 20 1 <= Mi <= 100 0 <= L <= (N x (N+1))/2 1 <= X, Y <= N 1 <= S <= N 1 <= M<= 100 1 <= Q <= 10

Subtask: 2 Original constraints

Sample Input: 4 1 2 3 4 3 1 2 2 3 3 4 2 1 4 1 15

Sample Output: YES NO

Time Limit: 5
Memory Limit: 256
Source Limit:
Editor Image

?