This page is still under construction.

Parts of this page are still being built. What you see may change.

Smugglers

Time limit1sMemory limit128 MB

Summary
Find a cycle from gold back to gold that minimizes total conversion cost plus 50% of the cheapest metal's price along the cycle.
Level

Medium7 of 10

Topics
Graph, Shortest path, Greedy, Sorting
Solved
No attempts yet

Problem

Byteotia is famous for its rich deposits of gold. For many years it sold that metal to a neighbouring kingdom, Bitland. Unfortunately, a growing budget deficit forced the king of Bitland to impose heavy tariffs on metals and minerals: any trader crossing the border must pay a customs duty of 50% of the value of the transported load.

Fortunately, Byteotian alchemists have found ways to turn one metal into another. The merchants' idea is to turn gold into some cheap metal, cross the border paying only a small tariff, and then turn it back into gold. However, the alchemists cannot turn an arbitrary metal directly into any other one, so obtaining a given metal from gold may require a chain of transformations that passes through a different metal at each stage. For every transformation they can perform, the alchemists fix a price for converting 1 kg of a metal A into a metal B, and they charge stiff fees.

Gold is metal number 1. We look for a sequence of metals m0,m1,…,mkm_0, m_1, \dots, m_k such that:

  • m0=mkm_0 = m_k is gold (metal 1);
  • for each i=1,2,…,ki = 1, 2, \dots, k the alchemists can obtain metal mim_i from metal mi−1m_{i-1};
  • the cost of performing the whole sequence of transformations on 1 kg of gold, plus the border duty, is as small as possible. Here the duty is 50% of the price of 1 kg of the cheapest metal among mim_i for i=0,1,…,ki = 0, 1, \dots, k.

Assume that the weight of the metal does not change during the alchemical processes.

Output the cost of performing the chosen sequence of transformations plus the duty paid at the border.

Input

The first line contains one positive integer nn, the number of distinct metals (1≤n≤5 0001 \le n \le 5\,000). For 1≤k≤n1 \le k \le n, line k+1k+1 contains a non-negative even integer pkp_k, the price of 1 kg of the kk-th metal (0≤pk≤1090 \le p_k \le 10^9). Gold is metal number 1. Line n+2n+2 contains one non-negative integer mm, the number of transformations the alchemists can perform (0≤m≤100 0000 \le m \le 100\,000). Each of the next mm lines contains three integers aa, bb, cc separated by single spaces: the alchemists can obtain metal bb from metal aa and charge cc bytealers to convert 1 kg of it (1≤a,b≤n1 \le a, b \le n, 0≤c≤10 0000 \le c \le 10\,000). Each ordered pair (a,b)(a, b) appears in the input at most once.

Output

Output a single integer: the cost of performing the transformations chosen by your program, plus the duty paid at the border.

Examples1

  1. Example 1

    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
    
    Expected output
    60