import heapq N = int(input()) lsS = [int(input()) for i in range(N)] M = int(input()) lsg = [[] for i in range(N)] for i in range(M): a,b,c = map(int,input().split()) lsg[a].append((b,c)) lsg[b].append((a,c)) #滞在0(普通のダイクストラ) lscost0 = [float('INF')]*(N) used = [False]*(N) lscost0[0] = 0 h = [(0,0)] heapq.heapify(h) while h: cost,node = heapq.heappop(h) if used[node]: continue used[node] = True for nj,cj in lsg[node]: if used[nj]: continue if lscost0[nj] > lscost0[node]+cj: lscost0[nj] = lscost0[node]+cj heapq.heappush(h,(lscost0[nj],nj)) #滞在1 O(N)*(普通のダイクストラ),始点で滞在。他1か所で滞在。 lscost1 = [float('INF')]*(N) for st in range(1,N-1): lscost1a = [float('INF')]*(N) used = [False]*(N) lscost1a[st] = lscost0[st]+lsS[st] h = [(lscost1a[st],st)] heapq.heapify(h) while h: cost,node = heapq.heappop(h) if used[node]: continue used[node] = True for nj,cj in lsg[node]: if used[nj]: continue if lscost1a[nj] > lscost1a[node]+cj: lscost1a[nj] = lscost1a[node]+cj heapq.heappush(h,(lscost1a[nj],nj)) for st2 in range(1,N-1): if st2 == st: continue lscost1b = [float('INF')]*(N) used = [False]*(N) lscost1b[st2] = lscost1a[st2]+lsS[st2] if lscost1b[st2] == float('INF'): continue h = [(lscost1b[st2],st2)] heapq.heapify(h) while h: cost,node = heapq.heappop(h) if used[node]: continue used[node] = True for nj,cj in lsg[node]: if used[nj]: continue if lscost1b[nj] > lscost1b[node]+cj: lscost1b[nj] = lscost1b[node]+cj heapq.heappush(h,(lscost1b[nj],nj)) for i in range(N): lscost1[i] = min(lscost1[i],lscost1b[i]) print(lscost1[-1])