#include #include #include #include using namespace std; typedef long long int64; const long long MAX_COST = (LLONG_MAX >> 6); struct Map1 { int id; int64 cost; bool operator<(const Map1& m) const { return cost > m.cost; } }; struct Map2 { int64 cost = MAX_COST; int prev = -1; }; int s[50] = {0}; unordered_map> cost; int n, m; int64 min_cost(int start, int end) { Map2 root[50]; for(int i = 0; i < n; i++) { root[i].cost = MAX_COST; root[i].prev = -1; } priority_queue q; root[start].cost = 0; for(auto c : cost[start]) { Map1 m = {c.first, c.second}; root[c.first].prev = start; root[c.first].cost = c.second; q.push(m); } while(!q.empty()) { int cur = q.top().id; q.pop(); for(auto c : cost[cur]) { int64 d = root[cur].cost + c.second; if(d < root[c.first].cost) { Map1 m = {c.first, d}; q.push(m); root[c.first].prev = cur; root[c.first].cost = d; } } } return root[end].cost; } int main(void) { cin >> n; for(int i = 0; i < n; i++) cin >> s[i]; cin >> m; for(int i = 0; i < m; i++) { int a, b, c; cin >> a >> b >> c; cost[b][a] = cost[a][b] = c; } int64 res = MAX_COST; for(int i = 1; i < n - 1; i++) for(int j = 1; j < n - 1; j++) { if(i == j) continue; int64 t = s[i] + s[j]; t += min_cost(0, i); t += min_cost(i, j); t += min_cost(j, n - 1); if(t < res) res = t; } cout << res << endl; return 0; }