cho.sh
NotesCho Mini
Loading...

Roman Numerals

Time limit

1s

Memory limit

128 MB

Problem

Roman numerals write natural numbers with the seven symbols I, V, X, L, C, D, and M.

SymbolIVXLCDM
Value1510501005001000

A valid Roman numeral follows these rules.

  1. In general, symbols with larger values are written to the left of symbols with smaller values. In that case, the number's value is the sum of all symbol values.
  2. V, L, and D may each appear at most once in one number. I, X, C, and M may be repeated consecutively at most three times.
  3. A smaller symbol may appear to the left of a larger symbol only in IV, IX, XL, XC, CD, and CM. Each such pair may appear at most once in one number, and two pairs with the same smaller symbol, such as IV and IX, cannot appear together.
  4. Every number must be written with the fewest possible symbols.

Given two numbers written as Roman numerals, output their sum as an Arabic numeral and as a Roman numeral.

Input

The first and second lines each contain one number written as a Roman numeral. Each input number is at most 2000, and their sum is less than 4000.

Output

On the first line, output the sum as an Arabic numeral. On the second line, output the same value as a Roman numeral.