Eatcake

2.4

5 votes
Maximum Bipartite Matching, Algorithms, Medium-Hard, Graph
Problem

Alice has n different cakes.

The quantity of ith cake available is Q[i].

The cakes are numbered in the decreasing order of her likeness for them (i.e. she likes the cake 1 the most and cake n the least).

There are m people. Each person likes certain cakes(a set of cakes) and the person j has a hunger of H[j]. Initially, each person(except Alice) randomly chooses one of the cakes they like and start eating (one person will not eat more than one cake).

The cake i will be leftover if the sum of H[j] over all the people j, who ate it is strictly less than its initial quantity.

Find a cake x such that Alice will be able to taste a cake with the likeness of at least as much as that of x after everybody has finished eating the cake (people are free to eat pieces of any cake). It is guaranteed that no more than 2 people like the same piece.

Note:  After everyone chooses one of the cakes they like and eats them, some of the cakes would be left over. The required answer is the least index of the cake which would be leftover independent of the choices of the other people. 

Input format

  • First line: Two integers n and m
  • Second line: n space-separated integers in the array Q
  • Third line: m space-separated integers in the array H
  • Next m lines: ith line begins with an integer ki denoting the number of cakes and the ith person likes followed by ki space-separated integers denoting the indices of the cakes

Output format

Print the integer x. If such a cake does not exist, then output -1.

Constraints

1n,m105
0Q[i],H[i]109

All indices are 1-based.

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

It is possible for the people to choose their cakes initially in such a manner such that there is no cake left for Alice.

Editor Image

?