#include #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define REPR(i, n) for(int i = (int)(n); i >= 0; i--) #define FOR(i, s, n) for (int i = (s); i < (int)(n); i++) #define ALL(v) v.begin(), v.end() using namespace std; typedef long long ll; ll N, K, W, H, M, C, V; const int MAX = 510000; const int MOD = 1000000007; long long fac[MAX], finv[MAX], inv[MAX]; vector cx, cy, c; ll MM = 1e12; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++){ fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } long long COM(int n, int k){ if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } std::vector Eratosthenes(int n ){ std::vector is_prime( n + 1 ); for( int i = 0; i <= n; i++ ) is_prime[ i ] = true; std::vector P; for( int i = 2; i <= n; i++ ){ if( is_prime[ i ] ){ for( int j = 2 * i; j <= n; j += i ){ is_prime[ j ] = false; } P.emplace_back( i ); } } return P; } int main(){ cin >> N; vector> dist(N, vector (N, MM)); vector S(N); REP(i, N) cin >> S[i]; cin >> M; REP(i, M){ int a,b; ll c; cin >> a >> b >> c; dist[a][b] = c; dist[b][a] = c; } REP(i, N){ REP(j, N){ REP(k, N){ dist[i][j]= min(dist[i][k] + dist[k][j], dist[i][j]); } } } ll ans = MM; int cndi,cndj; FOR(i,1, N-1){ FOR(j,1, N-1){ if(i == j) continue; ll cnd = S[i] + S[j] + dist[0][i] + dist[i][j] + dist[j][N-1]; if(cnd < ans) { ans = cnd; cndi = i; cndj = j; } } } cout << ans; return 0; }