The Romans wrote numbers using seven letters of the alphabet. Each letter and its value are shown below.
- I = 1
- V = 5
- X = 10
- L = 50
- C = 100
- D = 500
- M = 1000
Using these seven letters and the following rules, the Romans could write any number they wanted.
- Addition rule: If the letters are written from left to right in non-increasing order of value, add all of the values together. For example, the Roman numeral MMCLVII is 1000 + 1000 + 100 + 50 + 5 + 1 + 1 = 2157.
- Subtraction rule: Because writing numbers this way makes them too long, there is also a subtraction rule. If a letter's value is smaller than the value of the letter immediately to its right, subtract the value of the left letter. For example, the Roman numeral MCMXIV is 1000 − 100 + 1000 + 10 − 1 + 5 = 1914.
- I, X, and C cannot be written four or more times in a row. That is, IIII must be written in the shorter form IV.
- V, L, and D cannot be written two or more times in a row.
- There is no limit on the number of M's.
- The subtraction rule applies to only a single left letter. That is, IV is 4, but IIV is not 3 and is not a valid Roman numeral either.
- Only I, X, and C may appear as the left letter of the subtraction rule.
- In the subtraction rule, I may appear only before V or X, X only before L or C, and C only before D or M.
Given Roman numerals that satisfy these rules, convert them to decimal integers.