import std.algorithm, std.array, std.container, std.range; import std.string, std.conv, std.math; import std.stdio, std.typecons; alias Tuple!(int, "v", int, "c") side; void main() { auto n = readln.chomp.to!int; auto sti = iota(n).map!(i => readln.chomp.to!int).array; auto graph = new Nullable!int[][n]; foreach (ref l; graph) l = new Nullable!int[n]; foreach (i; 0..n) graph[i][i] = 0; auto m = readln.chomp.to!int; foreach (i; 0..m) { auto rd = readln.split.map!(to!int); graph[rd[0]][rd[1]] = rd[2]; graph[rd[1]][rd[0]] = rd[2]; } warshal_floyd(graph); auto min = int.max; foreach (s1; 1..(n - 1)) { foreach (s2; 1..(n - 1)) { if (s1 == s2) continue; auto r1 = graph[0][s1]; auto r2 = graph[s1][s2]; auto r3 = graph[s2][n - 1]; if (!r1.isNull && !r2.isNull && !r3.isNull) { auto r = r1 + r2 + r3 + sti[s1] + sti[s2]; if (r < min) min = r; } } } writeln(min); } void warshal_floyd(Nullable!int[][] d) { auto n = d.length; foreach (k; 0..n) foreach (i; 0..n) foreach (j; 0..n) { auto d1 = d[i][k]; auto d2 = d[k][j]; if (!d1.isNull && !d2.isNull) { if (!d[i][j].isNull) d[i][j] = min(d[i][j], d1 + d2); else d[i][j] = d1 + d2; } } }