Item Crafting
Time limit2sMemory limit256 MB
Compute the minimum money to get item 1 by buying items or crafting items from pairs of other items.
- Level
Medium7 of 10
- Topics
- Shortest path, Graph, Heap
- Solved
- No attempts yet
Problem
Sunyoung recently started a game called "Norris Tower". The game has kinds of items, and her character can wear every one of them. The items are numbered 1 through . Sunyoung wants to obtain item 1.
There are two ways to obtain an item.
- She can buy the item. Item costs won.
- She can craft the item. There are crafting recipes in total. If she brings two items of different kinds to the blacksmith, the blacksmith makes the resulting item for free. The two items she hands over are not returned, so using the same kind twice means preparing two of them.
Write a program that computes the smallest amount of money Sunyoung needs to obtain item 1.
Input
The first line contains the number of item kinds and the number of crafting recipes . (, )
The second line contains the prices in increasing order of item number. ()
Each of the next lines contains one recipe as the result item and the two ingredient items, , , , in that order. Handing one item and one item to the blacksmith yields item . (, , , )
Output
Print, on one line, the smallest amount of money needed to obtain item 1.