Title: Roman Numerals

In ancient Rome, counting numbers was a challenging task. The Arabic numerals 0, 1, 2, 3, ..., 9 were not yet in use. Instead, the following symbols were used:

Arabic Numeral | Roman Numeral | Arabic Numeral | Roman Numeral | Arabic Numeral | Roman Numeral
--- | --- | --- | --- | --- | ---
1 | I | 11 | XI | 30 | XXX
2 | II | 12 | XII | 40 | XL
3 | III | 13 | XIII | 50 | L
4 | IV | 14 | XIV | 60 | LX
5 | V | 15 | XV | 70 | LXX
6 | VI | 16 | XVI | 80 | LXXX
7 | VII | 17 | XVII | 90 | XC
8 | VIII | 18 | XVIII | 100 | C
9 | IX | 19 | XIX | 500 | D
10 | X | 20 | XX | 1000 | M

I represents 1, V represents 5, X represents 10, L represents 50, C represents 100, D represents 500, and M represents 1000. For other examples, refer to the table above. When a smaller number follows a larger number (i.e., on the right), they are added. When a smaller number precedes a larger number (i.e., on the left), the smaller number is subtracted from the larger number. A smaller number before a larger number for subtraction can only occur once.

Create a program to convert Roman numerals to Arabic numerals (standard numbers) and output the result. The given Roman numerals follow the rules mentioned above (although actual Roman numerals have more detailed rules, which are not considered here).

Input:
Multiple datasets are provided. Each dataset consists of a single line containing a Roman numeral represented by uppercase letters (I, V, X, L, C, D, M). The length of the Roman numeral string does not exceed 100 characters. The number of datasets does not exceed 50.

Output:
For each dataset, output the Arabic numeral (integer) on a single line.

Sample Input:
IV
CCCCLXXXXVIIII
CDXCIX

Output for the Sample Input:
4
499
499

# Buggy version:
import sys

def dec_num(x):
    for i in range(len(x)):
        if x[i] == 2:
            x[i] = 5
        elif x[i] == 3:
            x[i] = 10
        elif x[i] == 4:
            x[i] = 50
        elif x[i] == 5:
            x[i] = 100
        elif x[i] == 6:
            x[i] = 500
        elif x[i] == 7:
            x[i] = 1000

def to_index(x):
    if x == 1:
        return 1
    elif x == 5:
        return 2
    elif x == 10:
        return 3
    elif x == 50:
        return 4
    elif x == 100:
        return 5
    elif x == 500:
        return 6
    elif x == 1000:
        return 7


def toNum(x):
    result = 0
    tmp = x[0]
    for i in range(len(x)):
        if tmp < x[i]:
            result += x[i]-tmp
        elif i <= len(x) and x[i] < x[i+1]:
            result += 0
        else:
            result += x[i]
        tmp = x[i] 

    return result

for line in sys.stdin.readlines():
    line = line.translate(str.maketrans('IVXLCDM', '1234567'))
    num = list(map(int, (' '.join(line)).split()))
    dec_num(num)
    print(toNum(num))

# Fixed version:
import sys

def dec_num(x):
    for i in range(len(x)):
        if x[i] == 2:
            x[i] = 5
        elif x[i] == 3:
            x[i] = 10
        elif x[i] == 4:
            x[i] = 50
        elif x[i] == 5:
            x[i] = 100
        elif x[i] == 6:
            x[i] = 500
        elif x[i] == 7:
            x[i] = 1000

def to_index(x):
    if x == 1:
        return 1
    elif x == 5:
        return 2
    elif x == 10:
        return 3
    elif x == 50:
        return 4
    elif x == 100:
        return 5
    elif x == 500:
        return 6
    elif x == 1000:
        return 7


def toNum(x):
    result = 0
    tmp = x[0]
    for i in range(len(x)):
        if tmp < x[i]:
            result += x[i]-tmp
        elif i < len(x)-1 and x[i] < x[i+1]:
            result += 0
        else:
            result += x[i]
        tmp = x[i] 

    return result

for line in sys.stdin.readlines():
    line = line.translate(str.maketrans('IVXLCDM', '1234567'))
    num = list(map(int, (' '.join(line)).split()))
    dec_num(num)
    print(toNum(num))

# Buggy version:
nums={"I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000}
while True:
    try:
        s=input()
    except:
        break
    ans=nums[s[0]]
    for i in range(1,len(s)):
        if num[s[i-1]]<num[s[i]]:
            ans+=num[s[i]]-2*num[s[i-1]]
        else:
            ans+=num[s[i]]
    print(ans)
        

# Fixed version:
nums={"I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000}
while True:
    try:
        s=input()
    except:
        break
    ans=nums[s[0]]
    for i in range(1,len(s)):
        if nums[s[i-1]]<nums[s[i]]:
            ans-=2*nums[s[i-1]]
        ans+=nums[s[i]]
    print(ans)