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