# Here your code ! INF = 1000000 n = gets.to_i v = {} stay = n.times.map { gets.to_i } edge = n.times.map { Array.new(n,INF) } n.times do |i| edge[i][i] = 0 end m = gets.to_i m.times do from,to,cost = gets.split.map(&:to_i) edge[from][to] = cost edge[to][from] = cost end n.times do |k| n.times do |j| n.times do |i| edge[i][j] = [edge[i][j], edge[i][k] + edge[k][j]].min end end end cost = n.times.map { Array.new(n,INF) } (1..n-2).to_a.combination(2) do |(i,j)| cost[i][j] = edge[0][i] + edge[i][j] + edge[j][n-1] + stay[i] + stay[j] cost[j][i] = edge[0][j] + edge[j][i] + edge[i][n-1] + stay[i] + stay[j] end puts cost.flatten.uniq.min