N = gets.to_i R = gets.chomp.split def value c case c when ?I then 1 when ?V then 5 when ?X then 10 when ?L then 50 when ?C then 100 when ?D then 500 when ?M then 1000 end end def tot s s.chars.inject(0) { |a, b| a + value(b) } end def rev v s = "" (s << ?M; v -= 1000) until v < 1000 (s << ?D; v -= 500) until v < 500 (s << ?C; v -= 100) until v < 100 (s << ?L; v -= 50) until v < 50 (s << ?X; v -= 10) until v < 10 (s << ?V; v -= 5) until v < 5 (s << ?I; v -= 1) until v < 1 s end puts rev(R.inject(0) { |a, b| a + tot(b) })