#!/usr/bin/env python3

__author__ = 'Wei Mu'

class Solution:
    def romanToInt(self, s):
        """
        :type s: str
        :rtype: int
        """
        ans = 0
        
        value = dict()
        value['I'] = 1
        value['V'] = 5
        value['X'] = 10
        value['L'] = 50
        value['C'] = 100
        value['D'] = 500
        value['M'] = 1000
        
        
        for ind, val in enumerate(s):
            if val == 'C' and ind+1<len(s) and (s[ind+1] == 'M' or s[ind +1] == 'D'):
                ans -= 100
            elif val == 'X' and ind+1 <len(s) and (s[ind+1] == 'C' or s[ind+1] == 'L'):
                ans -= 10
            elif val == 'I' and ind + 1 < len(s) and (s[ind+1] == 'V' or s[ind+1] == 'X'):
                ans -= 1
            else:
                ans += value[val]
        
        return ans
            
