fun main(args:Array<String>?): Unit {
    readLine()!!.trim().toInt()
    val num = readLine()!!.trim().split(' ').map(String::toInt).toIntArray()
    println(solve(num))
}
fun solve(num: IntArray): Int {
    val range = RangeSum(num)
    val memo = Array(num.size){i -> IntArray(num.size - i){j -> if (j == 0) 0 else -1}}
    fun sub(from: Int, to: Int): Int {
        if (memo[from][to - from] == -1){
            memo[from][to - from] = (from until to).map{mid -> sub(from, mid) + sub(mid + 1, to) + time(range[from .. mid], range[mid + 1 .. to])}.min()!!
        }
        return memo[from][to - from]
    }
    return sub(0, num.lastIndex)
}
fun time(a: Int, b: Int): Int {
    tailrec fun sub(a: Int, b: Int, increment: Int = 0, result: Int = 0): Int {
        return if (a + b + increment == 0) result else sub(a / 10, b / 10, (a % 10 + b % 10 + increment) / 10, result + (a % 10) * (b % 10) + increment)
    }
    return sub(a, b)
}
class RangeSum(num: IntArray) {
    private val sum = num.clone()
    init {
        for (i in 1 until sum.size) sum[i] += sum[i - 1]
    }
    operator fun get(range: IntRange): Int {
        return get(range.last) - get(range.first - 1)
    }
    private fun get(index: Int): Int {
        return if (index in sum.indices) sum[index] else 0
    }
}
