Go: package main

import (
	"bufio"
	"os"
	"strings"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	dict := make(map[string]int)
	dict[" "] = 0
	dict["I"] = 1
	dict["V"] = 5
	dict["X"] = 10
	dict["L"] = 50
	dict["C"] = 100
	dict["D"] = 500
	dict["M"] = 1000
	for scanner.Scan() {
		line := strings.Split(scanner.Text(), "")
		line = append(line, " ")
		sum := 0
		for i := 0; i < len(line)-1; i++ {
			if dict[line[i]] < dict[line[i+1]] {
				sum -= dict[line[i]]
			} else {
				sum += dict[line[i]]
			}
		}
		fmt.Println(sum)
	}
}

C++: #include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <ctime>
using namespace std;

#define rep(i,a,n) for(int i=a; i<n; i++)
#define repr(i,a,n) for(int i=a; i>=n; i--)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 999999999

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) ( EQ((a).X, (b).X) && EQ((a).Y, (b).Y) )
#define LE(n, m) ((n) < (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long int ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

namespace std {
    bool operator<(const P a, const P b) {
        return a.X != b.X ? a.X < b.X : a.Y < b.Y;
    }
}

int const MOD = 1000000007;

int main() {
    string s;
    int a[51];
    while(cin >> s) {
        memset(a, -1, sizeof(a));
        repr(i,s.length() - 1,0) {
            if(s[i] == 'I') a[i] = 1;
            else if(s[i] == 'V') a[i] = 5;
            else if(s[i] == 'X') a[i] = 10;
            else if(s[i] == 'L') a[i] = 50;
            else if(s[i] == 'C') a[i] = 100;
            else if(s[i] == 'D') a[i] = 500;
            else if(s[i] == 'M') a[i] = 1000;
        }

        int prev = -1, ret = 0;
        repr(i,s.length() - 1,0) {
            if(a[i] >= prev) {
                prev = a[i];
                ret += a[i];
            }
            else {
                ret -= a[i];
            }
        }

        cout << ret << endl;
    }
    return 0;
}

C++: #include<cstdio>

using namespace std;

int main(void){
	char in[100];
	int i,j,size,ans,data[100],com,com2=0;

	for(i=0;i<100;i++)	data[i]=0;

	while(scanf("%s",in)!=EOF){
		for(i=0;i<100;i++){
			if(in[i]=='\0')	break;
		}
		size=i;
		for(i=0;i<100;i++)	data[i]=0;
		for(i=0;i<size;i++){
			if(in[i]=='I')	data[i]=1;
			if(in[i]=='V')	data[i]=5;
			if(in[i]=='X')	data[i]=10;
			if(in[i]=='L')	data[i]=50;
			if(in[i]=='C')	data[i]=100;
			if(in[i]=='D')	data[i]=500;
			if(in[i]=='M')	data[i]=1000;
		}
		
		ans=data[0];
		for(i=0;i<size;i++){
			if(data[i]>=data[i+1])	ans+=data[i+1];
			if(data[i]<data[i+1]){
				ans-=data[i];
				com=data[i];
				for(j=i+1;j<size;j++){
					com2+=data[j];
					if(data[j]<data[j+1])	break;
					i++;
				}
				ans+=com2-com;
				com2=0;
			}
		}
		printf("%d\n",ans);
	}

	return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

int rom[200];

int main() {
	int tmp,res;
	string str;
	rom['I']=1;
	rom['V']=5;
	rom['X']=10;
	rom['L']=50;
	rom['C']=100;
	rom['D']=500;
	rom['M']=1000;
	for(;cin>>str;){
		int i;
		tmp=res=0;
		for(i=0;i<str.size();i++){
			int nw;
			nw=rom[str[i]];
			if(tmp<nw)res-=tmp*2;
			res+=nw;
			tmp=nw;
		}
		cout<<res<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
using namespace std;
#define M_PI 3.141592
#define toRad 2.0*M_PI/360.0
#define inin(x) int x;cin>>x;
#define all(x) x.begin(),x.end()

long long roman(char c)
{
	char a[7+1] = "IVXLCDM";
	long long x[7] = { 1,5,10,50,100,500,1000 };
	for (int i = 0; i < 7; i++)
	{
		if (a[i] == c)
		{
			return x[i];
		}
	}
	return 0;
}


int main()
{
	string a;
	while (cin >> a)
	{
		long long ans = 0;
		reverse(all(a));
		long long b = 0;
		for (int i = 0; i < a.size(); i++)
		{
			long long now = roman(a[i]);
			if (b <= now)
			{
				ans += now;
			}
			else
			{
				ans -= now;
			}
			b = now;
		}
		cout << ans << endl;
	}

}

C++: #include <algorithm>
#include <vector>
#include <math.h>
#include <stdio.h>
#include <cctype>
#include <iostream>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <utility>
using namespace std;
int size(string x){
	string::size_type size=x.size();
	return size;
}
#define fu(l,k) for(int i=l;i<k;i++)
#define fd(l,k) for(int i=l;i>k;i--)
#define sort(ss) sort(ss.begin(), ss.end())
#define rsort(ss) sort(ss.rbegin(), ss.rend())
typedef  vector<string> vs;
typedef  vector<int> vi;
double pi(){
	return M_PI;
}
typedef set<int> set_i;
typedef set<string> set_s;
int INF=1000000;
int main(){
	string s;
	int ans,sl,q;
	while(cin>>s){
		if(s=="EOF") break;
		ans=0;
		q=INF;
		sl=size(s);
		for(int i=0;i<sl;i++){
			if(s[i]=='I'){
				if(q>=1) ans+=1;
				else ans-=1;
				q=1;
			}
			if(s[i]=='V'){
				if(q>=5) ans+=5;
				else ans=ans+5-q-q;
				q=5;
			}
			if(s[i]=='X'){
				if(q>=10) ans+=10;
				else ans=ans+10-q-q;
				q=10;
			}
			if(s[i]=='L'){
				if(q>=50) ans+=50;
				else ans=ans+50-q-q;
				q=50;
			}
			if(s[i]=='C'){
				if(q>=100) ans+=100;
				else ans=ans+100-q-q;
				q=100;
			}
			if(s[i]=='D'){
				if(q>=500) ans+=500;
				else ans=ans+500-q-q;
				q=500;
			}
			if(s[i]=='M'){
				if(q>=1000) ans+=1000;
				else ans=ans+1000-q-q;
				q=1000;
			}
		}
		cout<<ans<<endl;
	}
}

C++: #include <iostream>
#include <stdio.h>
#include <math.h>

//I は1, V は5, X は10, L は50, C は100, D は500, M は1000,

#define I 1
#define V 5
#define X 10
#define L 50
#define C 100
#define D 500
#define M 1000

using namespace std;

int main(){

	string str;
	while(cin>>str){
	cin>>str;
		int data[200];
		int sum = 0;
		
		//データの数値化
		for(int i=0;i<str.size();i++){
			switch(str[i]){
				case 'I':
					data[i]=I;
					break;
				case 'V':
					data[i]=V;
					break;
				case 'X':
					data[i]=X;
					break;
				case 'L':
					data[i]=L;
					break;
				case 'C':
					data[i]=C;
					break;
				case 'D':
					data[i]=D;
					break;
				case 'M':
					data[i]=M;
					break;
			}
		}

		for(int i=0;i<str.size();i++){
			if(i==str.size()-1){
				sum+=data[i];
			}
			else{
				if(data[i]<data[i+1]){
					sum+= (data[i+1]-data[i]);
					i++;
				}
				else{
					sum+=data[i];
				}
			}
		}

		cout<<sum<<endl;
	}

	return 0;

}

/*
IV
CCCCLXXXXVIIII
CDXCIX
*/

C++: #include <iostream>
#include <string>

using namespace std;

int r(char s){
	if(s=='I')return 1;
	if(s=='V')return 5;
	if(s=='X')return 10;
	if(s=='L')return 50;
	if(s=='C')return 100;
	if(s=='D')return 500;
	if(s=='M')return 1000;
}

int main()
{
	string str;
	while(cin>>str){
		int s=str.size(),i=0,sum=0;
		for(i=0;i<s-1;i++){
			if(r(str[i])>=r(str[i+1]))sum+=r(str[i]);
			else{
				sum+=r(str[i+1])-r(str[i]);
				i++;
			}
		}
		if(i<s)sum+=r(str[i]);
		cout<<sum<<endl;
		str.clear();
	}
}