Whiskey Trade

Time limit1sMemory limit256 MB

Summary
Model the distribution network as a flow graph with node capacities and compute the maximum flow from Myeongjin to Jueun.
Level

Hard8 of 10

Topics
Graph, Shortest path, BFS, Implementation
Solved
No attempts yet

Problem

Jueun and Myeongjin trade whiskey privately. Jueun has plenty of money and loves whiskey, so she wants to buy as much as possible, while Myeongjin has a whiskey surplus and wants to sell as much as possible. However, because of her social reputation and dignity, Jueun is reluctant to trade too much whiskey directly in one go. She therefore mobilizes all her friends and secretly asks Myeongjin as well to set up a private distribution network. The structure of the network is as follows.

  • Jueun orders whiskey from Myeongjin.
  • Myeongjin distributes as much whiskey as possible appropriately and sends it to Myeongjin's friends.
  • Myeongjin's friends can exchange whiskey within the network through their contacts. Some of them can reach Jueun's friends, but none of them can contact Jueun directly.
  • Jueun's friends, after receiving whiskey from Myeongjin's friends, deliver all of it to Jueun.

Myeongjin can send unlimited whiskey, and Jueun has no limit on the amount she can receive from her friends. However, since Myeongjin's and Jueun's friends also worry about their own social reputations, each of them has a fixed amount they can receive from a single person. For example, if A can receive at most 5 bottles of whiskey at a time, A can receive 5 bottles each from 3 friends and deliver a total of 15 bottles to Jueun, but cannot receive 6 bottles from one friend.

Given information about the maximum number of bottles each friend can receive from a single person and whom Myeongjin's friends can contact, determine the maximum number of bottles Jueun can receive at one time.

Input

The first line gives the number of Jueun's friends N and the number of Myeongjin's friends M. (1 ≤ N, M ≤ 100)

The second line gives, in order, the maximum amount of whiskey Jueun's friends can receive from one person. Jueun's friends are numbered 1 through N in the given order.

The third line gives, in order, the maximum amount of whiskey Myeongjin's friends can receive from one person. Myeongjin's friends are numbered N+1 through N+M in the given order.

The maximum amount of whiskey that can be received from one person does not exceed 10,000,000.

From the fourth line, M lines follow. Each line gives the number K of people a Myeongjin friend can contact and the list of those people, separated by spaces. (1 ≤ K ≤ N + M - 1)

Output

On the first line, output the maximum number of bottles of whiskey Jueun can receive at one time.

Examples1

  1. Example 1

    Input
    2 2
    1 1
    2 3
    1 1
    2 1 2
    
    Expected output
    3