n = gets.not_nil!.to_i s = gets.not_nil!.split.map(&.to_i) # Initialize graph with large values (500,000,000) g = Array.new(n) { Array.new(n, 500_000_000) } m = gets.not_nil!.to_i m.times do a, b, c = gets.not_nil!.split.map(&.to_i) g[a][b] = c g[b][a] = c end # Floyd-Warshall algorithm n.times do |k| n.times do |i| n.times do |j| if g[i][k] + g[k][j] < g[i][j] g[i][j] = g[i][k] + g[k][j] end end end end ans = 2_000_000_000 (1...n-1).each do |t1| (1...n-1).each do |t2| next if t1 == t2 cost = g[0][t1] + g[t1][t2] + g[t2][n-1] + s[t1] + s[t2] ans = Math.min(ans, cost) if cost < ans end end puts ans