fun main(args: Array) { val pointCount = readLine()!!.toInt() road = arrayOfNulls(pointCount) for(i in 1..pointCount) { feeList.add(readLine()!!.toInt()) road[i-1] = arrayOfNulls(pointCount) road[i-1]!![i-1] = 0 } val roadCount = readLine()!!.toInt() for(i in 1..roadCount) { val (a,b,c) = readLine()!!.split(" ").map { it.toInt() } road[a]!![b] = c road[b]!![a] = c } for(i in road.indices) { for(j in road.indices) { for(k in road.indices) { val express = road[j]!![k] road[j]!![i]?.let { val part1 = it road[i]!![k]?.let { val part2 = it express?.also { if(express > part1 + part2) { road[j]!![k] = part1 + part2 } } ?: run { road[j]!![k] = part1 + part2 } } } } } } var ans = feeList.sum() + road.map { it!!.filter { it != null }.map { it!!.toLong() }.sum() }.sum() for(i in 1..pointCount - 2) { if(road[0]!![i] == null) { continue } for(j in 1..pointCount - 2) { if(i == j) { continue } if(road[i]!![j] == null) { continue } if(road[j]!![pointCount-1] == null) { continue } val subTotal = road[0]!![i]!! + feeList[i] + road[i]!![j]!! + feeList[j] + road[j]!![pointCount-1]!! ans = Math.min(ans, subTotal.toLong()) } } println(ans) } var feeList = mutableListOf() var road = arrayOfNulls>(0)