CCCGuide

Problem S2: Aromatic Numbers

2012 · S2 · Original problem · DMOJ

Problem Description

This question involves calculating the value of aromatic numbers which are a combination of Arabic digits and Roman numerals.

An aromatic number is of the form ARARAR…ARARARAR \ldots AR, where each AA is an Arabic digit, and each RR is a Roman numeral. Each pair ARAR contributes a value described below, and by adding or subtracting these values together we get the value of the entire aromatic number.

An Arabic digit AA can be 0, 1, 2, 3, 4, 5, 6, 7, 8 or 9. A Roman numeral RR is one of the seven letters I, V, X, L, C, D, or M. Each Roman numeral has a base value:

SymbolIVXLCDM
Base value1510501005001000

The value of a pair ARAR is AA times the base value of RR. Normally, you add up the values of the pairs to get the overall value. However, wherever there are consecutive symbols ARA′R′ARA'R' with R′R' having a strictly bigger base value than R,R, the value of pair ARAR must be subtracted from the total, instead of being added.

For example, the number 3M1D2C has the value 3∗1000+1∗500+2∗100=37003*1000+1*500+2*100 = 3700 and 3X2I4X has the value 3∗10−2∗1+4∗10=683*10-2*1+4*10=68.

Write a program that computes the values of aromatic numbers.

Input Specification

The input is a valid aromatic number consisting of between 2 and 20 symbols.

Output Specification

The output is the decimal value of the given aromatic number.

Sample Input 1

3M1D2C

Output for Sample Input 1

3700

Sample Input 2

2I3I2X9V1X

Output for Sample Input 2

-16