R2I_TBL = { M: 1000, CM: 900, D: 500, CD: 400, C: 100, XC: 90, L: 50, XL: 40, X: 10, IX: 9, V: 5, IV: 4, I: 1, } I2R_TBL = R2I_TBL.invert def roman2i(roman) roman.scan(/IV|IX|XL|XC|CD|CM|I|V|X|L|C|D|M/).map{|s| R2I_TBL[s.intern] }.sum end def i2roman(n, s="") if n > 0 i, c = I2R_TBL.find{|k,v|k <= n} i2roman(n - i, s + c.to_s) else s end end N = gets.to_i R = gets.split.take(N) total = R.map{|r|roman2i(r)}.sum puts total > 3999 ? :ERROR : i2roman(total)