local mfl = math.floor local inf = 10000000 local n = io.read("*n") local staycost = {} for i = 1, n do staycost[i] = io.read("*n") end local m = io.read("*n") local line = {} for i = 1, n do line[i] = {} end for i = 1, m do local a, b, c = io.read("*n", "*n", "*n") a, b = a + 1, b + 1 line[a][b] = c line[b][a] = c end local cost = {} for i = 1, n * n do cost[i] = inf end cost[1] = 0 local tasks = {1} local tasknum, done = 1, 0 local function walk(src, dst, c) if cost[src] + c < cost[dst] then -- print("from " .. src .. " to " .. dst .. " by " .. cost[src] + c) cost[dst] = cost[src] + c table.insert(tasks, dst) tasknum = tasknum + 1 end end while done < tasknum do done = done + 1 local curidx = tasks[done] local cur_stay_city = 1 + mfl((curidx - 1) / n) local cur_city = curidx - (cur_stay_city - 1) * n for dstcity, c in pairs(line[cur_city]) do walk(curidx, dstcity + (cur_stay_city - 1) * n, c) if 1 < cur_city and cur_city < n then if cur_stay_city == 1 then walk(curidx, dstcity + (cur_city - 1) * n, c + staycost[cur_city]) elseif cur_stay_city < n then if cur_stay_city ~= cur_city then walk(curidx, dstcity + (n - 1) * n, c + staycost[cur_city]) end end end end end print(cost[n * n])