import java.lang.Math.*
import java.util.*

fun main(args:Array<String>?): Unit {
    val n = readLine()!!.trim().toInt()
    val semi = readLine()!!.trim().split(' ').map(String::toInt)
    val nodes = Array(n){ Stack<Edge>() }
    repeat(n - 1){
        val (u, v, p) = readLine()!!.trim().split(' ').map(String::toInt)
        nodes[u].add(Edge(v, p))
        nodes[v].add(Edge(u, p))
    }
    fun dfs(current: Int, prev: Int = -1, minEdge: Int = Int.MAX_VALUE): Int {
        return if (current != 0 && semi[current - 1] > 0) minEdge
        else min(minEdge, nodes[current].filterNot{ it.to == prev }.sumBy{ dfs(it.to, current, min(minEdge, it.cost)) })
    }
    println(dfs(0))
}

data class Edge(val to: Int, val cost: Int)
