fun main() { val n = readLine()!!.toInt() val a = readLine()!!.split(" ").map { it.toInt() } var one = 0 var two = 0 var others = 0 println(a.map{ when(it){ 1 -> 2*one++ + 3*two + 2*others 2 -> 3*one + 1*two++ + 1*others else -> 2*one + 1*two + 1*others++ } }.sum()) }