using System; using System.Diagnostics; using System.Collections.Generic; using System.Linq; using Enu = System.Linq.Enumerable; class Solution { int N, M; int[] S; void calc() { N = ri(); S = new int[N]; for (int i = 0; i < N; i++) { S[i] = ri(); } var dijkstra = new Algorithm.Dijkstra(N); dijkstra.S = S; M = ri(); for (int i = 0; i < M; i++) { var r = ria(3); dijkstra.addUnDirEdge(r[0], r[1], r[2]); } dijkstra.calcShortestPath(0); int ans = dijkstra.INF; for (int i = 0; i <= 2000; i++) { ans = Math.Min(ans, dijkstra.d[N - 1, i, 2] + i); } Console.WriteLine(ans); } static void Main(string[] args) { new Solution().calc(); } #region static int ri() { return int.Parse(Console.ReadLine()); } static int[] ria(int n) { if (n <= 0) { Console.ReadLine(); return new int[0]; } else return Console.ReadLine().Trim().Split(' ').Select(int.Parse).ToArray(); } static void rio(out int p1) { p1 = ri(); } static void rio(out int p1, out int p2) { var r = ria(2); p1 = r[0]; p2 = r[1]; } static void rio(out int p1, out int p2, out int p3) { var r = ria(3); p1 = r[0]; p2 = r[1]; p3 = r[2]; } static void rio(out int p1, out int p2, out int p3, out int p4) { var r = ria(4); p1 = r[0]; p2 = r[1]; p3 = r[2]; p4 = r[3]; } static void rio(out int p1, out int p2, out int p3, out int p4, out int p5) { var r = ria(5); p1 = r[0]; p2 = r[1]; p3 = r[2]; p4 = r[3]; p5 = r[4]; } static long rl() { return long.Parse(Console.ReadLine()); } static long[] rla(int n) { if (n <= 0) { Console.ReadLine(); return new long[0]; } else return Console.ReadLine().Trim().Split(' ').Select(long.Parse).ToArray(); } static void rlo(out long p1) { p1 = rl(); } static void rlo(out long p1, out long p2) { var r = rla(2); p1 = r[0]; p2 = r[1]; } static void rlo(out long p1, out long p2, out long p3) { var r = rla(3); p1 = r[0]; p2 = r[1]; p3 = r[2]; } static void rlo(out long p1, out long p2, out long p3, out long p4) { var r = rla(4); p1 = r[0]; p2 = r[1]; p3 = r[2]; p4 = r[3]; } static void rlo(out long p1, out long p2, out long p3, out long p4, out long p5) { var r = rla(5); p1 = r[0]; p2 = r[1]; p3 = r[2]; p4 = r[3]; p5 = r[4]; } static double rd() { return double.Parse(Console.ReadLine()); } static double[] rda(int n) { if (n <= 0) { Console.ReadLine(); return new double[0]; } else return Console.ReadLine().Trim().Split(' ').Select(double.Parse).ToArray(); } static void rdo(out double p1) { p1 = rd(); } static void rdo(out double p1, out double p2) { var r = rda(2); p1 = r[0]; p2 = r[1]; } static void rdo(out double p1, out double p2, out double p3) { var r = rda(3); p1 = r[0]; p2 = r[1]; p3 = r[2]; } static void rdo(out double p1, out double p2, out double p3, out double p4) { var r = rda(4); p1 = r[0]; p2 = r[1]; p3 = r[2]; p4 = r[3]; } static void rdo(out double p1, out double p2, out double p3, out double p4, out double p5) { var r = rda(5); p1 = r[0]; p2 = r[1]; p3 = r[2]; p4 = r[3]; p5 = r[4]; } static void swap(ref T x, ref T y) { T temp = x; x = y; y = temp; } static void wa1(T[] a) { Debug.WriteLine(string.Join(" ", a)); } static void wa2(T[][] a) { foreach (var row in a) { Debug.WriteLine(String.Join(" ", row)); } } #endregion } static class Extention { public static T[][] ToJagArray(this T[,] a) { int n = a.GetLength(0), m = a.GetLength(1); var ret = new T[n][]; for (int i = 0; i < n; i++) { ret[i] = new T[m]; for (int j = 0; j < m; j++) { ret[i][j] = a[i, j]; } } return ret; } } namespace Algorithm { using T = System.Int32; // require PriorityQueue public class Dijkstra { public T INF = (T)1e9; [DebuggerDisplay("from : {from}, to : {to}, cost : {cost}")] private class edge { public int from, to; public T cost; public edge(int from, int to, T cost) { this.from = from; this.to = to; this.cost = cost; } } [DebuggerDisplay("v : {v}, cost : {cost}, time : {time}, wait : {waitcount}")] private class state { public int v, waitcount, first; public T cost, time; public state(int v, T cost, T time, int waitcount, int first) { this.v = v; this.cost = cost; this.time = time; this.waitcount = waitcount; this.first = first; } } private int vnum; private List[] edgelists; public T[, ,] d; private PriorityQueue que; public int[] S; public Dijkstra(int vnum) { this.vnum = vnum; edgelists = new List[vnum + 1]; for (int i = 0; i < edgelists.Length; i++) edgelists[i] = new List(); d = new T[vnum + 1, 2001, 3]; } public void addDirEdge(int v1, int v2, T cost) { edgelists[v1].Add(new edge(v1, v2, cost)); } public void addUnDirEdge(int v1, int v2, T cost) { edgelists[v1].Add(new edge(v1, v2, cost)); edgelists[v2].Add(new edge(v2, v1, cost)); } public void calcShortestPath(int s) { for (int i = 0; i <= vnum; i++) { for (int j = 0; j <= 2000; j++) { for (int k = 0; k <= 2; k++) { d[i, j, k] = INF; } } } que = new PriorityQueue((s1, s2) => s2.cost.CompareTo(s1.cost)); d[s, 0, 0] = 0; que.Push(new state(s, 0, 0, 0, 0)); while (que.Count() != 0) { var st = que.Pop(); int v = st.v; if (d[v, st.time, st.waitcount] < st.cost) continue; foreach (var e in edgelists[v]) { if (d[e.to, st.time, st.waitcount] > d[v, st.time, st.waitcount] + e.cost) { d[e.to, st.time, st.waitcount] = d[v, st.time, st.waitcount] + e.cost; que.Push(new state(e.to, d[e.to, st.time, st.waitcount], st.time, st.waitcount, st.first)); } if (st.v != 0 && st.v != vnum - 1 && st.waitcount <= 1 && st.v != st.first) { if (d[e.to, st.time + S[st.v], st.waitcount + 1] > d[v, st.time, st.waitcount] + e.cost) { d[e.to, st.time + S[st.v], st.waitcount + 1] = d[v, st.time, st.waitcount] + e.cost; que.Push(new state(e.to, d[e.to, st.time + S[st.v], st.waitcount + 1], st.time + S[st.v], st.waitcount + 1, st.v)); } } } } } } public class PriorityQueue { private List buffer; private Comparison comp; // 昇順 // (e1, e2) => e2.CompareTo(e1) // 降順 // (e1, e2) => e1.CompareTo(e2) public PriorityQueue(Comparison comp) { buffer = new List(); this.comp = comp; } public PriorityQueue(Comparison comp, IEnumerable data) : this(comp) { foreach (T e in data) Push(e); } public void Push(T elem) { PushHeap(this.buffer, elem); } public T Pop() { return PopHeap(this.buffer); } public int Count() { return buffer.Count; } public T Top() { return buffer[0]; } private void PushHeap(List array, T elem) { int n = array.Count; array.Add(elem); while (n != 0) { int i = (n - 1) / 2; // 親と値を入れ替え if (comp(array[n], array[i]) > 0) { T tmp = array[n]; array[n] = array[i]; array[i] = tmp; } n = i; } } private T PopHeap(List array) { int n = array.Count - 1; T returnelem = array[0]; array[0] = array[n]; array.RemoveAt(n); for (int i = 0, j; (j = 2 * i + 1) < n; ) { // 値の大きい方の子を選ぶ if ((j != n - 1) && (comp(array[j], array[j + 1]) < 0)) j++; // 子と値を入れ替え if (comp(array[i], array[j]) < 0) { T tmp = array[j]; array[j] = array[i]; array[i] = tmp; } i = j; } return returnelem; } } }