#include #include #include int main() { using namespace std; int n; cin >> n; vector s(n); for (auto &e : s) { cin >> e; } constexpr int INF = 1e6; vector dist(n, vector(n, INF)); for (int i = 0; i < n; i++) { dist[i][i] = 0; } int m; cin >> m; while (m--) { int a, b, c; cin >> a >> b >> c; dist[a][b] = c; dist[b][a] = c; } for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } } } int ans = INF; for (int i = 1; i < n - 1; i++) { for (int j = 1; j < n - 1; j++) { if (i == j) { continue; } ans = min(ans, dist[0][i] + s[i] + dist[i][j] + s[j] + dist[j][n - 1]); } } cout << ans << endl; }