문제
Always keen to learn new hobbies, Bessie the cow is learning how to transform metals. She has () units of metal for . Furthermore, she knows () recipes where she can combine one unit each of several metals to make one unit of a metal with a higher number than all constituent metals. It is additionally guaranteed that for each metal, Bessie knows at most one recipe to make it.
Compute the maximum number of units of metal Bessie can possibly have after some series of transformations.
입력
The first line contains .
The second line contains integers, .
The third line contains .
The next lines start with two integers and (), followed by integers. The last integers represent the constituent metals in the recipe that are used to form one unit of metal . It is guaranteed that is larger than the last integers.
출력
Output the maximum number of units of metal Bessie can possibly have after applying some series of zero or more transformations.
예제 입력 1
5
2 0 0 1 0
3
5 2 3 4
2 1 1
3 1 2
예제 출력 1
1
점수
In test case 2, for , one unit of metal can be transformed into one unit of metal .In test cases 3 and 4, each recipe transforms one unit of one metal into another.Test cases 5 through 11 satisfy no additional constraints.
코드를 제출하려면 로그인이 필요합니다.
로그인