import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.Iterator; public class Main_yukicoder17 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Printer pr = new Printer(System.out); int n = sc.nextInt(); int[] s = new int[n]; for (int i = 0; i < n; i++) { s[i] = sc.nextInt(); } FloydWarshall fw = new FloydWarshall(n); int m = sc.nextInt(); for (int i = 0; i < m; i++) { int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); fw.addEdge(a, b, c); fw.addEdge(b, a, c); } long min = Long.MAX_VALUE; for (int i = 1; i < n - 1; i++) { for (int j = 1; j < n - 1; j++) { if (i == j) { continue; } long tmp = fw.getShortestPath(0, i) + fw.getShortestPath(i, j) + fw.getShortestPath(j, n - 1); tmp += s[i] + s[j]; min = Math.min(min, tmp); } } pr.println(min); pr.close(); sc.close(); } private static class FloydWarshall { long[][] d; int n; long[][] result; boolean nf; // NEGATIVE CYCLE flag final static long INF = Long.MAX_VALUE / 4; FloydWarshall(int n) { this.n = n; d = new long[n][n]; for (int i = 0; i < n; i++) { Arrays.fill(d[i], INF); d[i][i] = 0; } nf = false; } // i, j:0-indexed public void addEdge(int i, int j, int c) { d[i][j] = c; } public long getShortestPath(int i, int j) { if (nf) { return -INF; } if (result == null) { for (int kk = 0; kk < n; kk++) { for (int ii = 0; ii < n; ii++) { for (int jj = 0; jj < n; jj++) { // d[ii][jj] = Math.min(d[ii][jj], d[ii][kk] + d[kk][jj]); if (d[ii][kk] != INF && d[kk][jj] != INF && d[ii][jj] > d[ii][kk] + d[kk][jj]) { d[ii][jj] = d[ii][kk] + d[kk][jj]; } } } } for (int k = 0; k < n; k++) { if (d[k][k] < 0) { nf = true; return -INF; } } result = d; } return result[i][j]; } } @SuppressWarnings("unused") private static class Scanner { BufferedReader br; Iterator it; Scanner (InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } String next() throws RuntimeException { try { if (it == null || !it.hasNext()) { it = Arrays.asList(br.readLine().split(" ")).iterator(); } return it.next(); } catch (IOException e) { throw new IllegalStateException(); } } int nextInt() throws RuntimeException { return Integer.parseInt(next()); } long nextLong() throws RuntimeException { return Long.parseLong(next()); } float nextFloat() throws RuntimeException { return Float.parseFloat(next()); } double nextDouble() throws RuntimeException { return Double.parseDouble(next()); } void close() { try { br.close(); } catch (IOException e) { // throw new IllegalStateException(); } } } private static class Printer extends PrintWriter { Printer(PrintStream out) { super(out); } } }