#include #define REP(i,n) for(int i=0; i<(int)(n); i++) #include #include #include inline int getInt(){ int s; scanf("%d", &s); return s; } using namespace std; int dp[50][64]; int main(){ const int n = getInt(); vector s(n); REP(i,n) s[i] = getInt(); const int m = getInt(); vector > > g(n); REP(i,m){ const int a = getInt(); const int b = getInt(); const int c = getInt(); g[a].push_back(make_pair(b, c)); g[b].push_back(make_pair(a, c)); } typedef pair > data; priority_queue, greater > pq; memset(dp, -1, sizeof(dp)); pq.push(data(0, make_pair(0, n))); while(pq.size()){ const data d = pq.top(); pq.pop(); const int cost = d.first; const int pos = d.second.first; const int stay = d.second.second; if(dp[pos][stay] != -1) continue; dp[pos][stay] = cost; if(pos == n - 1 && stay == n + 1){ printf("%d\n", cost); return 0; } if(stay <= n && stay != pos && pos != 0 && pos != n - 1){ pq.push(data(cost + s[pos], make_pair(pos, stay == n ? pos : n + 1))); } REP(i,g[pos].size()){ const int next = g[pos][i].first; const int cc = cost + g[pos][i].second; if(dp[next][stay] == -1) pq.push(data(cc, make_pair(next, stay))); } } return 0; }