Under construction

Still being built. Things may change

Paper Route

Interview

Time limit1sMemory limit128 MB

Summary
With N+1 nodes and exactly N roads, find the cheapest closed walk from node 0 covering all addresses, then add the campus travel cost from wherever you end.
Level

Hard8 of 10

Topics
Graph, DFS, Dynamic programming, Tree
Solved
No attempts yet

Problem

As a cash-strapped student, you take a part-time job as a paper carrier. You are handed a paper route: a set of addresses conveniently labelled 11 to NN.

Every morning you start at the newspaper office, which is address number 00. You must plan a route that delivers a newspaper to every address, and you also want to get to class right after you finish. There are exactly NN roads in your area connecting the addresses, and each takes a known time to traverse. You have also precomputed the time it takes to reach the campus from each location, including the newspaper office. How soon can you finish delivering and be in your seat at school?

Input

The first line contains a single integer NN (the number of addresses, 1≤N≤1000001 \le N \le 100000).

The next N+1N+1 lines each contain an integer cic_i (for i=0,1,…,Ni = 0, 1, \dots, N, with 0≤ci≤1,000,000,0000 \le c_i \le 1{,}000{,}000{,}000): the time it takes to get from location ii to campus.

The final NN lines each contain three integers aa, bb, cc (0≤a,b≤N0 \le a, b \le N, a≠ba \ne b, 0≤c≤1,0000 \le c \le 1{,}000), describing a road between locations aa and bb that takes cc minutes to traverse.

You are guaranteed to be able to reach every address. (Recall that location 00 is the newspaper office.)

Output

Output the minimum time it takes to deliver all the papers and get to class.

Hint

It can be better to visit all the addresses, return to the office, and head to school from there.

For example, one route is 0→1→0→2→0→school0 \to 1 \to 0 \to 2 \to 0 \to \text{school}, taking 1+1+2+2+1=71 + 1 + 2 + 2 + 1 = 7.

Examples2

  1. Example 1

    Input
    2
    1
    3
    4
    0 1 1
    0 2 2
    
    Expected output
    7
    
  2. Example 2

    Input
    1
    5
    10
    0 1 3
    
    Expected output
    11