#include using namespace std; struct Edge { typedef int CostType; const static int cost = 1; int from, to; Edge(int from, int to) : from(from), to(to) {}; }; template struct WeightedEdge : public Edge { typedef Cost CostType; Cost cost; WeightedEdge(int from, int to, Cost cost = 0) : Edge(from, to), cost(cost) {} }; template struct ResidualEdge : public Edge { typedef Capacity CapacityType; Capacity cap; int rev; ResidualEdge(int from, int to, Capacity cap) : Edge(from, to), cap(cap) {} ResidualEdge reverse() const {return ResidualEdge(to, from, 0);} }; template struct WeightedResidualEdge : public ResidualEdge { Cost cost; WeightedResidualEdge(int from, int to, Capacity cap, Cost cost) : ResidualEdge(from, to, cap), cost(cost) {} WeightedResidualEdge reverse() const {return WeightedResidualEdge(this->to, this->from, 0, -cost);} }; template class Graph { public: typedef Edge EdgeType; virtual int size() const = 0; template void addEdge(Args...) {} template void addUndirectedEdge(Args...) {} virtual vector getEdges() const = 0; virtual vector getEdges(int from) const = 0; virtual vector getEdges(int from, int to) const = 0; virtual int getDegree(int v) const = 0; }; template class AdjacencyList : public Graph { protected: vector> graph; public: AdjacencyList(int n) : graph(n) {} int size() const { return graph.size(); } template void addEdge(Args... args) { Edge edge(args...); graph[edge.from].emplace_back(edge); } template void addUndirectedEdge(Args... args) { Edge edge(args...); addEdge(edge); swap(edge.from, edge.to); addEdge(edge); } vector getEdges() const { vector res; for (const auto& edges : graph) { res.insert(res.end(), edges.begin(), edges.end()); } return res; } vector getEdges(int from) const { return graph[from]; } vector getEdges(int from, int to) const { vector res; for (const auto& edge : graph[from]) { if (edge.to == to) res.emplace_back(edge); } return res; } int getDegree(int v) const { return graph[v].size(); } vector& operator[](int v) { return graph[v]; } }; template class WarshallFloyd { private: typedef typename Graph::EdgeType::CostType Cost; const Graph& graph; public: const static Cost INF = numeric_limits::max() / 2 - 1; vector> dis; WarshallFloyd(const Graph& graph) : graph(graph), dis(graph.size(), vector(graph.size(), INF)) { for (int i = 0; i < graph.size(); ++i) dis[i][i] = 0; } void solve() { for (const auto& edge : graph.getEdges()) { dis[edge.from][edge.to] = min(dis[edge.from][edge.to], edge.cost); } for (int k = 0; k < graph.size(); ++k) { for (int i = 0; i < graph.size(); ++i) { for (int j = 0; j < graph.size(); ++j) { dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]); } } } } }; template inline WarshallFloyd shortestPath(Graph& graph) { WarshallFloyd warshallFloyd(graph); warshallFloyd.solve(); return warshallFloyd; } int main() { typedef AdjacencyList> Graph; int n; cin >> n; vector s(n); for (int& i : s) cin >> i; Graph graph(n); int m; cin >> m; for (int i = 0; i < m; ++i) { int a, b, c; cin >> a >> b >> c; graph.addUndirectedEdge(a, b, c); } auto dis = shortestPath(graph).dis; int res = numeric_limits::max(); const int INF = WarshallFloyd::INF; for (int i = 1; i < n - 1; ++i) { for (int j = 1; j < n - 1; ++j) { if (i == j) continue; if (dis[0][i] == INF || dis[i][j] == INF || dis[j][n - 1] == INF) continue; res = min(res, dis[0][i] + dis[i][j] + dis[j][n - 1] + s[i] + s[j]); } } cout << res << endl; }