lib C fun strtoll(s : UInt8*, p : UInt8**, b : Int32) : Int64 end class String def to_i64 C.strtoll(self, nil, 10) end end n = read_line.to_i a = read_line.split.map(&.to_i64) c1, c2 = {1, 2}.map { |k| cnt = [0] * -~n n.times { |i| cnt[i + 1] = cnt[i] + (a[i] == k ? 1 : 0) } cnt } puts (0...n).sum { |i| case a[i] when 1 3i64 * c2[i] + 2i64 * (i - c2[i]) when 2 3i64 * c1[i] + 1i64 * (i - c1[i]) else 2i64 * c1[i] + 1i64 * (i - c1[i]) end }