import heapq INF = 2 * 10**18 class StaticCHT: # funcs: 一次関数 f(x)=ax+b: (a, b) のリスト def __init__(self, funcs): by_a = {} for a, b in funcs: if a in by_a: by_a[a] = min(by_a[a], b) else: by_a[a] = b funcs = sorted(by_a.items(), key = lambda f: -f[0]) # 交点を求める self.points = [] self.funcs = [funcs[0]] for i in range(len(funcs) - 1): a1, b1 = funcs[i] a2, b2 = funcs[i + 1] self.points.append((b1 - b2, a2 - a1)) self.funcs.append((a2, b2)) while len(self.points) >= 2: p1, q1 = self.points[-2] p2, q2 = self.points[-1] if p1 * q2 <= p2 * q1: break self.funcs.pop(-2) self.points.pop() self.points.pop() a1, b1 = self.funcs[-2] a2, b2 = self.funcs[-1] self.points.append((b1 - b2, a2 - a1)) # 点 x における最小値を求める def min(self, x): l, r = 0, len(self.points) while l < r: mid = (l + r) // 2 p, q = self.points[mid] if x * q <= p: l = mid + 1 else: r = mid a, b = self.funcs[l] return a * x + b def dijkstra(graph, start, inf = 10**18): dist = [inf] * len(graph) dist[start] = 0 pq = [] heapq.heappush(pq, (0, start)) while pq: d, u = heapq.heappop(pq) if d > dist[u]: continue for v, w in graph[u]: if dist[v] > d + w: dist[v] = d + w heapq.heappush(pq, (d + w, v)) return dist N, M = map(int, input().split()) W = list(map(int, input().split())) edges = [list(map(int, input().split())) for _ in range(M)] graph = [[] for _ in range(N)] for u, v, t in edges: graph[u - 1].append((v - 1, t)) graph[v - 1].append((u - 1, t)) dist_s = dijkstra(graph, 0, INF) dist_t = dijkstra(graph, N - 1, INF) ans = dist_s[N - 1] # dist_s[i] + dist_t[j] + W[i] * W[j] funcs = [] for j in range(N): if dist_t[j] < INF: funcs.append((W[j], dist_t[j])) cht = StaticCHT(funcs) for i in range(N): if dist_s[i] < INF: ans = min(ans, dist_s[i] + cht.min(W[i])) print(ans)