INF = 100000000 n = gets.to_i s = Array.new(n) d = Array.new(n) n.times{|i| s[i] = gets.to_i d[i] = Array.new(n, INF) d[i][i] = 0 } gets.to_i.times{ a, b, cost = gets.split.map(&:to_i) d[a][b] = cost d[b][a] = cost } n.times{|i| n.times{|j| n.times{|k| d[j][k] = [d[j][k], d[j][i] + d[i][k]].min } } } ans = INF [*1..(n - 2)].combination(2){|a,b| ans = [ans, [d[0][a] + d[b][n - 1], d[0][b] + d[a][n - 1]].min + d[a][b] + s[a] + s[b]].min } p ans