import sys import heapq def read_input(): input = sys.stdin.read data = input().split() index = 0 N = int(data[index]) M = int(data[index + 1]) index += 2 W = list(map(int, data[index: index + N])) index += N edges = [] for _ in range(M): U = int(data[index]) - 1 V = int(data[index + 1]) - 1 T = int(data[index + 2]) edges.append((U, V, T)) index += 3 return N, M, W, edges def build_graph(N, edges): graph = [[] for _ in range(N)] for U, V, T in edges: graph[U].append((V, T)) graph[V].append((U, T)) return graph def dijkstra(N, graph, W): dist = [float("inf")] * N dist[0] = 0 pq = [(0, 0)] # (distance, node) while pq: current_dist, u = heapq.heappop(pq) if current_dist > dist[u]: continue # 航路に沿った移動 for v, time in graph[u]: new_dist = current_dist + time if new_dist < dist[v]: dist[v] = new_dist heapq.heappush(pq, (new_dist, v)) # ワープ移動 for v in range(N): if v != u: warp_time = W[u] * W[v] new_dist = current_dist + warp_time if new_dist < dist[v]: dist[v] = new_dist heapq.heappush(pq, (new_dist, v)) return dist[N - 1] if __name__ == "__main__": N, M, W, edges = read_input() graph = build_graph(N, edges) shortest_time = dijkstra(N, graph, W) print(shortest_time)