open System open System.Collections.Generic type WarshallFloyd(n,?isNonDir0:bool)= let isNonDir = defaultArg isNonDir0 true let inf = 1e16|>int64 let vertexNum = n let edgeNum = ref 0 let costs = Array2D.init (n+1) (n+1) (fun i j-> if i <> j then inf else 0L) member this.Inf = inf member this.AddPath f t c = incr edgeNum costs.[f,t]<-min costs.[f,t] c if isNonDir then costs.[t,f]<-min costs.[t,f] c member this.Run() = for i in 0..vertexNum do for j in 0..vertexNum do for k in 0..vertexNum do costs.[j,k]<-min costs.[j,k] (costs.[j,i]+costs.[i,k]) member this.PathExist f t = costs.[f,t] <> inf member this.MinCosts() = costs member this.MinCost f t = costs.[f,t] let read() = Console.ReadLine() let N = read() |> int let ws = WarshallFloyd(N) let S = Array.init N (fun _ -> read() |> int64) let M = read() |> int for i in 0..M-1 do let t = read().Split() ws.AddPath (t.[0] |> int) (t.[1] |> int) (t.[2] |> int64) ws.Run() [for i in [1..N-2] do for j in [1..N-2] do if i <> j then yield (i,j)] |> List.map (fun (a,b) -> (ws.MinCost 0 a) + S.[a] + (ws.MinCost a b) + S.[b] + (ws.MinCost b (N-1))) |> List.min |> printfn "%i"