N = gets.to_i dist = Array.new(N) {Array.new(N, Float::INFINITY)} (N-1).times { u, v, d = gets.split(" ").map{|s| s.to_i} dist[u-1][v-1] = dist[v-1][u-1] = d } 0.upto(N-1) {|k| dist[k][k] = 0 0.upto(N-1) {|i| 0.upto(N-1) {|j| dist[i][j] = [dist[i][j], dist[i][k] + dist[k][j]].min } } } cnt = 0 0.upto(N-1) {|i| 0.upto(N-1) {|j| cnt += dist[i][j] } } puts cnt