QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 32 MB Total points: 100

#12980. Smugglers

Statistics

Byteotia is famous for its rich deposits of gold. Therefore, for many years there flourished the sale of that metal to a neighbouring kingdom, Bitland. Unfortunately, the growing deficit of the national budget forced the king of Bitland to introduce heavy tariffs on metals and minerals. Traders crossing the border have to pay customs duty of $50\%$ of the value of the transported load. Byteotian merchants are threatened with bankruptcy. Fortunately, Byteotian alchemists have developed ways to transform some metals into other. The merchants' idea is to use the alchemists' know-how to transform gold into some cheap metal, and next, after crossing the border and paying little tariff, to transform it back into gold. Unfortunately, the alchemists can not transform any metal into arbitrarily chosen other one. Therefore, it may happen that the process of obtaining a given metal from gold must be a chain of transformations that produces a different metal on each stage. The alchemists demand stiff fees for their services. They have fixed a price for transforming 1 kg of a metal $A$ into a metal $B$ for each transformation they are able to conduct. The traders ponder on what form gold should be transported across the border and what sequence of alchemical processes should be applied to achieve the highest income.

Task

Help to cure Byteotian economy! Write a program which:

  • Reads a table of prices for all metals and prices for transformations offered by the alchemists.
  • Determines such a sequence of metals $m_0,m_1,…,m_k$ that:
    • $m_0=m_k$ is gold,
    • for each $i=1,2,…,k$ the alchemists are able to obtain metal $m_i$ from metal $m_{i-1}$, and
    • the cost of performing the whole sequence of alchemical processes for 1 kg of gold, augmented by the duty paid on the border (50% of the price of 1 kg of the cheapest metal from $m_i$, for $i=0,1,…,k$) is the smallest possible.
  • We assume that during the alchemical processes the weight of metals does not alter.
  • Writes out the cost of performing the determined sequence of alchemical processes augmented by the duty paid on the border.

Input

In the first line of the standard input there is one positive integer $n$ denoting the number of different metals, $1 ≤ n ≤ 5\,000$. In the $(k+1)$-st line, for $1 ≤ k ≤ n$, there is a non-negative even integer $p_k$: the price of 1 kg of the $k$-th metal, $0 ≤ p_k ≤ 10^9$. We assume that gold has the number 1. In the $(n+2)$-nd line there is one non-negative integer m equal to the number of transformation processes the alchemists are able to conduct, $0 ≤ m ≤ 100\,000$. In each of the following m lines there are three positive integers, separated by single spaces, describing consecutive transformation processes. A triple of numbers $a$, $b$, $c$ denotes that the alchemists are able to obtain the $b$-th metal from the $a$-th metal, and they demand c bytealers for transforming 1 kg of material, $1 ≤ a,b ≤ n$, $0 ≤ c ≤ 10\,000$. An ordered pair of numbers $a$ and $b$ may appear at most once in the data.

Output

Your program should write to the standard output. In the first line there should be one integer - the cost of performing the alchemical processes determined by your program augmented by the duty paid on the border.

Example

Input

4
200
100
40
2
6
1 2 10
1 3 5
2 1 25
3 2 10
3 4 5
4 1 50

Output

60
About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.