//#include #include using namespace std; typedef long long ll; typedef pair p; const ll INF = 1e9; const ll LINF = ll(1e18); const ll MOD = 1000000007; const ll dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0}; const ll Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; #define yes cout << "Yes" << endl #define YES cout << "YES" << endl #define no cout << "No" << endl #define NO cout << "NO" << endl #define rep(i, n) for (ll i = 0; i < n; i++) #define ALL(v) v.begin(), v.end() #define debug(v) \ cout << #v << ":"; \ for (auto x : v) \ { \ cout << x << ' '; \ } \ cout << endl; template bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } //cout<> d; void warshall_floyd(int n) { for (int k = 0; k < n; k++) { // 経由する頂点 for (int i = 0; i < n; i++) { // 始点 for (int j = 0; j < n; j++) { // 終点 d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } } int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector s(n); rep(i, n) cin >> s[i]; int m; cin >> m; d.resize(n); rep(i, n) d[i].resize(n); rep(i, n) { rep(j, n) { d[i][j] = INF; } } rep(i, n) { d[i][i] = 0; } rep(i, m) { int a, b, c; cin >> a >> b >> c; d[a][b] = c; d[b][a] = c; } warshall_floyd(n); int ans = INF; for (int i = 1; i < n - 1; i++) { for (int j = 1; j < n - 1; j++) { if (i == j) continue; int temp = d[0][i] + d[j][n - 1] + s[i] + s[j] + d[i][j]; chmin(ans, temp); } } cout << ans << "\n"; }