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++: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
// #define main int main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vpii = vector<pii>;
using vvi = vector<vi>;
using vvb = vector<vb>;
using vvpii = vector<vpii>;
template<typename A> using fn = function<A>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int &n) { return n & 1; }
constexpr bool even(const int &n) { return !odd(n); }
void solve();
main { solve(); return 0; }
// clang-format on

template<typename A>
using vpix = vector<pair<int, A>>;

vpix<char> u;
vpix<string> m;

int f(string s) {
  int c = 0, l = 0;
  for (int i = s.size() - 1; i >= 0; i--) {
    for (auto p : u) {
      if (s[i] == p.second) {
        c += p.first * (l <= p.first ? 1 : -1);
        l = p.first;
      }
    }
  }
  return c;
}

void solve() {
  u.pb(mp(1000, 'M'));
  u.pb(mp(500, 'D'));
  u.pb(mp(100, 'C'));
  u.pb(mp(50, 'L'));
  u.pb(mp(10, 'X'));
  u.pb(mp(5, 'V'));
  u.pb(mp(1, 'I'));
  string s;
  while (cin >> s) cout << f(s) << endl;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
using namespace std;

int main(){
	string s;
	while(cin >> s){
		int i = 0,num[100] = {},j = 0;
		while(s[i] != '\0'){
			if(s[i] == 'I'){
				i++;
				num[j] += 1;
				while(s[i] == 'I'){
					i++;
					num[j] += 1;
				}
				j++;
			}else if(s[i] == 'V'){
				i++;
				num[j] += 5;
				while(s[i] == 'V'){
					i++;
					num[j] += 5;
				}
				j++;
			}else if(s[i] == 'X'){
				i++;
				num[j] += 10;
				while(s[i] == 'X'){
					i++;
					num[j] += 10;
				}
				j++;
			}else if(s[i] == 'L'){
				i++;
				num[j] += 50;
				while(s[i] == 'L'){
					i++;
					num[j] += 50;
				}
				j++;
			}else if(s[i] == 'C'){
				i++;
				num[j] += 100;
				while(s[i] == 'C'){
					i++;
					num[j] += 100;
				}
				j++;
			}else if(s[i] == 'D'){
				i++;
				num[j] += 500;
				while(s[i] == 'D'){
					i++;
					num[j] += 500;
				}
				j++;
			}else if(s[i] == 'M'){
				i++;
				num[j] += 1000;
				while(s[i] == 'M'){
					i++;
					num[j] += 1000;
				}
				j++;
			}
		}
		i = 0;
		int pre = 1e9,res = 0;
		while(num[i] != 0){
			if(pre < num[i]){
				res -= pre * 2;
			}
			res += num[i];
			pre = num[i];
			i++;
		}
		cout << res << endl;
	}
}

C++: #include <cstdio>
#include <iostream>
#include <string>
using namespace std;

int main(){
	string s;
	string t="IVXLCDM";
	int m[7]={1, 5, 10, 50, 100, 500, 1000};
	
	while(cin >> s){
		long ans=0;
		int size=s.size();
		
		for(int i=0; i<size; ++i){
			int p=0, q=0;
			while(s[i] != t[p]) p++;
			if(i+1<size) while(s[i+1] != t[q]) q++;
			 
			if(p<q) ans -= m[p];
			else ans += m[p];
		}
		
		printf("%ld\n", ans);
	}
}

C++: #include <bits/stdc++.h>
using namespace std;

using db = double;
using ll = long long;
using vi = vector <int>;
#define op operator
#define pb push_back

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

int main() {
	ios :: sync_with_stdio(0);

	for(string s; cin >> s; ) {
		int ans = 0;
		for(int i = 0; i < s.size(); i ++) {
			if(i + 1 < s.size() && f(s[i]) < f(s[i + 1]))
				ans -= f(s[i]);
			else
				ans += f(s[i]);
		}
		cout << ans << '\n';
	}

	return 0;
}

C++: IV
CCCCLXXXXVIIII
CDXCIX

C++: #include <bits/stdc++.h>
#define rep(i,l,n) for(int i=l;i<n;i++)
using namespace std;

int main(){
	char s[105];

	while(scanf("%s",s)!=EOF){
		int len,n[105]={},ans=0;
		len=strlen(s);
		rep(i,0,len){
			if(s[i]=='I') n[i]=1;
			else if(s[i]=='V') n[i]=5;
			else if(s[i]=='X') n[i]=10;
			else if(s[i]=='L') n[i]=50;
			else if(s[i]=='C') n[i]=100;
			else if(s[i]=='D') n[i]=500;
			else if(s[i]=='M') n[i]=1000;
		}
		rep(i,0,len-1){
			if(n[i]<n[i+1]) ans-=n[i];
			else ans+=n[i];
		}
		ans+=n[len-1];
		cout<<ans<<endl;
	}
}

C++: #include<iostream>
using namespace std;
int main(){
  string s;
  while(cin >>s){
    int ans = 0,t = 0;
    for(int i=0; i<s.size(); i++){
      switch(s[i]){
      case 'I':
	t = 1;
	ans++;
	break;
      case 'V':
	if(t == 1) ans-=2;
	t = 5;
	ans+=5;
	break;
      case 'X':
	if(t == 1) ans-=2;
	t = 10;
	ans+=10;
	break;
      case 'L':
	if(t == 10) ans-=20;
	t = 50;
	ans+=50;
	break;
      case 'C':
	if(t == 10) ans-=20;
	t = 100;
	ans+=100;
	break;
      case 'D':
	if(t == 100) ans-=200;
	t = 500;
	ans+=500;
	break;
      case 'M':
	if(t == 100) ans-=200;
	t = 1000;
	ans+=1000;
	break;
      }
    }
    cout <<ans<<endl;
  }
  return 0;
}

C++: #define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <list>
#include <utility>
#include <numeric>

using namespace std;
typedef long long llong;
typedef unsigned long long ullong;

const ullong MOD = 1000000000 + 7;

int main()
{
	string s;
	vector<pair<string, int>> r = {make_pair("M", 1000), make_pair("D", 500), make_pair("C", 100), make_pair("L", 50), make_pair("X", 10), make_pair("V", 5), make_pair("I", 1)};
	for (; cin >> s;)
	{
		int sum = 0;
		int add = 0;
		int old = 0;
		for (int i = 0; i < s.length(); i++)
		{
			for (int j = 0; j < r.size(); j++)
			{
				if (s.substr(i, 1) == r.at(j).first)
				{
					int n0 = r.at(j).second;
					if (old > 0 && old < n0)
					{
						sum -= 2 * old;
						old = 0;
					}
					sum += n0;
					old = n0;
					break;
				}
			}
		}

		// sum += add + old;

		cout << sum << endl;
	}
}

C++: #include<iostream>
#include<string>
#include<map>
using namespace std;

map<char,int> m;
int ans;
string s;

int main(){
	m['I']=1;
	m['V']=5;
	m['X']=10;
	m['L']=50;
	m['C']=100;
	m['D']=500;
	m['M']=1000;
	while(cin>>s){
		ans=0;
		for(int i=s.length()-1;i>=0;i--){
			if(i!=s.length()-1&&m[s[i]]<m[s[i+1]]){
				ans-=m[s[i]];
			}  
			else{
				ans+=m[s[i]];
			} 
		}
		cout<<ans<<endl;
	}
	return 0;
}