Crafting Recipes

시간 제한1초메모리 제한2048 MB

요약
원재료 비용과 중간 부품의 제조법이 주어질 때, 자기 자신을 포함하지 않는 Capstone의 총 원재료 비용을 구한다.
난이도

보통10점 중 4점

유형
그래프, DFS, 구현
정답자
아직 제출이 없습니다

문제

After months of hard work, you've finally finished designing a contraption for your senior capstone project. Now you just have to build it. Your design is quite complex and is composed of many parts, so you want to make sure you have enough funds to build it. You know the price of all the raw materials, and your design specifies how many parts you need to build the intermediate components. Can you determine the total price of raw materials needed to construct your contraption?

입력

The first line of input is an integer, 1≤N≤2001 \leq N \leq 200, specifying the number of different raw materials that make up your contraption. The following NN lines each contain a unique string specifying the name of a raw material, followed by an integer 1≤C≤1,0001 \leq C \leq 1\\,000 specifying the cost of a single unit of that raw material.

The next line contains an integer 1≤M≤2001 \leq M \leq 200 specifying the number of different intermediate components that make up the design. The following MM lines each contain a single "recipe" specifying the parts (where each part could be either a raw material or another intermediate component), and the quantity of each of the parts, needed to build an intermediate component. The recipe for the Capstone contraption is one of these MM recipes.

Each recipe begins with a string specifying the name of the intermediate component, followed by an integer 1≤P≤2,0001 \leq P \leq 2\\,000 specifying the number of different parts that make up the component. PP pairs of information follow: a string specifying the name of the part (which again, could be either a raw material or another intermediate component), followed by an integer 1≤Q≤1,0001 \leq Q \leq 1\\,000 specifying the quantity of that part required to build the intermediate component.

You are guaranteed that all names of raw materials and intermediate components consist of only upper- and lower-case letters.

Note that there is one and only one recipe for each intermediate component, and that no intermediate component requires itself as a part in any way.

출력

The output is a single integer specifying the total cost of raw materials needed to construct the Capstone contraption.

힌트

Sample Input 11 specifies that there are two raw materials WoodenGear and MetalSprocket that have a cost of 11 and 33, respectively. The input also specifies two intermediate components. The Capstone component requires five StrongChassis and two WoodenGear and the StrongChassis requires three WoodenGear and five MetalSprocket. The cost of a single StrongChasis is therefore 3×1+5×3=183 \times 1 + 5 \times 3 = 18, and the total cost of the Capstone contraption is thus 5×18+2×1=925 \times 18 + 2 \times 1 = 92.

예제2

  1. 예제 1

    입력
    2
    WoodenGear 1
    MetalSprocket 3
    2
    Capstone 2 StrongChassis 5 WoodenGear 2
    StrongChassis 2 WoodenGear 3 MetalSprocket 5
    
    예상 출력
    92
    
  2. 예제 2

    입력
    3
    ConductiveHinge 4
    SmoothGear 4
    SleekBolt 3
    3
    AerodynamicGearbox 2 SleekBolt 1 SmoothGear 4
    Capstone 4 StrongPlate 9 SleekBolt 3 AerodynamicGearbox 2 ConductiveHinge 5
    StrongPlate 3 SmoothGear 4 SmoothGear 4 AerodynamicGearbox 5
    
    예상 출력
    1210