#590
Silver III
Alchemy
시간 제한
2s
메모리 제한
1024MB
제출
0
정답
0
맞힌 사람
0
정답 비율
0.0%

문제

Always keen to learn new hobbies, Bessie the cow is learning how to transform metals. She has aia_i (0ai1040 \le a_i \le 10^4) units of metal ii for 1iN1001 \le i \le N \le 100. Furthermore, she knows KK (1K<N1\le K<N) 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 NN Bessie can possibly have after some series of transformations.

입력

The first line contains NN.

The second line contains NN integers, aia_i.

The third line contains KK.

The next KK lines start with two integers LL and MM (M1M\ge 1), followed by MM integers. The last MM integers represent the constituent metals in the recipe that are used to form one unit of metal LL. It is guaranteed that LL is larger than the MM last integers.

출력

Output the maximum number of units of metal NN 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 1i<N1 \le i < N, one unit of metal ii can be transformed into one unit of metal i+1i+1.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.

코드 제출

코드를 제출하려면 로그인이 필요합니다.

로그인
내 제출
제출 내역이 없습니다.
맞은 사람
아직 맞은 사람이 없습니다.
난이도 투표
Silver III1명 투표· 약 1개월 전
로그인 후 AC 받으면 투표할 수 있습니다.
전체 제출
제출 내역이 없습니다.