N = gets.to_i R = gets.chomp.split.map def decode s v, ptr = 0, 0 val = { ?I => 1, ?V => 5, ?X => 10, ?L => 50, ?C => 100, ?D => 500, ?M => 1000 } until ptr == s.size while s.size - ptr >= 2 && val[s[ptr]] < val[s[ptr + 1]] v += val[s[ptr + 1]] - val[s[ptr]] ptr += 2 end case s[ptr] when ?M (ptr += 1; v += 1000) while s[ptr] == ?M when ?D (ptr += 1; v += 500) while s[ptr] == ?D when ?C (ptr += 1; v += 100) while s[ptr] == ?C when ?L (ptr += 1; v += 50) while s[ptr] == ?L when ?X (ptr += 1; v += 10) while s[ptr] == ?X when ?V (ptr += 1; v += 5) while s[ptr] == ?V when ?I (ptr += 1; v += 1) while s[ptr] == ?I end end v end def encode v s = "" (s << ?M; v -= 1000) until v < 1000 (s << ?D; v -= 500) until v < 500 (s << "CD"; v -= 400) until v < 400 (s << ?C; v -= 100) until v < 100 (s << ?L; v -= 50) until v < 50 (s << "XL"; v -= 40) until v < 40 (s << ?X; v -= 10) until v < 10 (s << ?V; v -= 5) until v < 5 (s << "IV"; v -= 4) until v < 4 (s << ?I; v -= 1) until v < 1 s end tot = R.inject(0) { |a, b| a + decode(b) } puts tot > 3999 ? "ERROR" : encode(tot)