#include #include #include using namespace std; template struct Edge { int src, dst; T cost; Edge(int dst, T cost) : src(-1), dst(dst), cost(cost) { } Edge(int src, int dst, T cost) : src(src), dst(dst), cost(cost) { } }; template using Edges = vector>; template using WeightedGraph = vector>; template using Matrix = vector>; template void warshall_floyd(Matrix &g) { const T INF = numeric_limits::max(); for (int k = 0; k < g.size(); k++) { for (int i = 0; i < g.size(); i++) if (g[i][k] != INF) { for (int j = 0; j < g.size(); j++) if (g[k][j] != INF) { g[i][j] = min(g[i][j], g[i][k] + g[k][j]); } } } } int main() { int n; cin >> n; vector s(n); for (int &si: s) cin >> si; const auto INF = numeric_limits::max(); Matrix dist(n, vector(n, INF)); int m; cin >> m; for (int i = 0; i < n; i++) dist[i][i] = 0; while (m--) { int a, b, c; cin >> a >> b >> c; dist[a][b] = dist[b][a] = c; } warshall_floyd(dist); int ans = INF; for (int i = 1; i < n - 1; i++) { if (dist[0][i] == INF) continue; for (int j = 1; j < n - 1; j++) if (i != j) { if (dist[i][j] == INF || dist[j][n - 1] == INF) continue; ans = min(ans, dist[0][i] + s[i] + dist[i][j] + s[j] + dist[j][n - 1]); } } cout << ans << endl; return 0; }