#include #include #include #include using namespace std; typedef long long int64; struct Map1 { int id; int64 cost; bool operator<(const Map1& m) const { return cost > m.cost; } }; struct Map2 { int64 cost = (int64)LONG_MAX; int prev = -1; }; int s[50] = {0}; unordered_map> cost; int n, m; int min_cost(int start, int end) { Map2 root[50]; for(int i = 0; i < n; i++) { root[i].cost = (int64)LONG_MAX; 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; //if(cur == end) // return root[end].cost; q.pop(); for(auto c : cost[cur]) { int d = root[cur].cost + c.second; if(d < root[c.first].cost) { if(root[c.first].cost >= (int64)LONG_MAX - 1) { 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 = (int64)LONG_MAX; for(int i = 1; i < n - 1; i++) for(int j = 1; j < n - 1; j++) { if(i == j) continue; int64 t = 0; t += min_cost(0, i) + s[i]; t += min_cost(i, j) + s[j]; t += min_cost(j, n - 1); if(t < res) res = t; } cout << res << endl; return 0; }