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 <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
#define int ll

signed main(void) {
  string s;
  while (cin >> s) {
    int c = 1;
    VI a;
    REP(i, s.size() - 1) {
      if (s[i + 1] == s[i])
        ++c;
      else {
        if (s[i] == 'I')
          a.push_back(1 * c);
        else if (s[i] == 'V')
          a.push_back(5 * c);
        else if (s[i] == 'X')
          a.push_back(10 * c);
        else if (s[i] == 'L')
          a.push_back(50 * c);
        else if (s[i] == 'C')
          a.push_back(100 * c);
        else if (s[i] == 'D')
          a.push_back(500 * c);
        else if (s[i] == 'M')
          a.push_back(1000 * c);
        c = 1;
      }
    }
    if (s[s.size() - 1] == 'I')
      a.push_back(1 * c);
    else if (s[s.size() - 1] == 'V')
      a.push_back(5 * c);
    else if (s[s.size() - 1] == 'X')
      a.push_back(10 * c);
    else if (s[s.size() - 1] == 'L')
      a.push_back(50 * c);
    else if (s[s.size() - 1] == 'C')
      a.push_back(100 * c);
    else if (s[s.size() - 1] == 'D')
      a.push_back(500 * c);
    else if (s[s.size() - 1] == 'M')
      a.push_back(1000 * c);

    // for(int i: a) cout << i << " "; cout << endl;
    REP(i, a.size() - 1) {
      if (a[i] < a[i + 1]) {
        a[i + 1] -= a[i];
        a[i] = 0;
      }
      // for(int i: a) cout << i << " "; cout << endl;
    }
    int ret = 0;
    for (int i : a) {
      ret += i;
    }
    cout << ret << endl;
  }
  return 0;
}

C++: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\b.txt"


int main() {
	string st;
	map<char, int>mp;
	mp['I'] = 0;
	mp['V'] = 1;
	mp['X'] = 2;
	mp['L'] = 3;
	mp['C'] = 4;
	mp['D'] = 5;
	mp['M'] = 6;
	
	vector<int>values{ 1,5,10,50,100,500,1000 };
	while (cin >> st) {
		vector<int>nums;
		for (auto c : st) {
			nums.push_back(mp[c]);
		}
		int sum = 0;
		for (int i = 0; i < nums.size(); ++i) {
			if (i == nums.size() - 1 || nums[i] >= nums[i + 1])sum += values[nums[i]];
			else sum -= values[nums[i]];
		}
		cout << sum << endl;

	}
	return 0;
}

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

#define REP(i,b,n) for(int i=b;i<(int)n;i++)
#define rep(i,n) REP(i,0,n)
#define foreach(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)

long long res;

int main (int argc, char const* argv[]) {
  string raw;
  string map = "IVXLCDM";
  const int weight[7] = {1,5,10,50,100,500,1000};
  while (cin>>raw) {
    res = 0;
    rep(i,7) rep(j,raw.size()) if (raw[j] == map[i]) {
      raw[j] = '0' + i;
    }
    char prev = '8';
    rep(i,raw.size()) {
      if (prev < raw[i]) {
        res += weight[raw[i] - '0'] - weight[prev - '0'] * 2;
        //puts("why");
      } else {
        res += weight[raw[i] - '0'];
      }
      prev = raw[i];
    }
    cout<<res<<endl;
  }
  return 0;
}

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

int To_num(char);
int Calculate(string);

int main(void){
  string roma;

  while(cin >> roma) cout << Calculate(roma) << endl;
  return 0;
}

int Calculate(string str)
{
  int ans = 0;
  int now;
  int next;
  
  for(int i = 0 ; i < str.size() ; i++)
    {
      now = To_num(str[i]);
      next = To_num(str[i+1]);
      
      if(now >= next) ans += now;
      else
	{
	  ans += next - now;
	  i++;
	}
    }
  return ans;
}
      
int To_num(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;
  if(c == 'M') return 1000;
}

C++: 
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <memory>
#include <string>

#include <algorithm>
#include <complex>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
#endif

#define ITER(c)         __typeof__((c).begin())
#define FOREACH(it, c)  for (ITER(c) it=(c).begin(); it != (c).end(); ++it)
#define RITER(c)        __typeof__((c).rbegin())
#define RFOREACH(it, c) for (RITER(c) it=(c).rbegin(); it != (c).rend(); ++it)
#define REP(i, n)       REPEAT(i, 0, n)
#define REPEAT(i, k, n) for (int i = (k); i < (n); ++i)
#define REVREP(i, n)    for (int i = ((n)-1); i >= 0; --i)
#define FIND(c, n)      std::find((c).begin(), (c).end(), (n));
#define BSEARCH(c, n)   std::binary_search((c).begin(), (c).end(), (n))
#define LLPOW(p1,p2)    ((ll)pow((double)(p1), (int)p2))
#define ESIZEOF(A)      (sizeof(A)/sizeof((A)[0]))
#define MIN(p1,p2)      (((p1)<(p2))?(p1):(p2))
#define MAX(p1,p2)      (((p1)>(p2))?(p1):(p2))
#define CIN_NO_SYNC     do { cin.tie(0); ios_base::sync_with_stdio(false); } while(0)
#define GETSTR(p)       fgets((p), sizeof(p), stdin)
#define CHOMP(p)        do{ char *_q = (p) + strlen(p)-1; if(*_q == '\n') *_q = 0; } while(0)

typedef long long ll;
typedef unsigned long long ull;

#ifdef _DEBUG
#define pf printf
#define pr(n) do { cout << #n " = " << (n) << endl; } while(0)
#else
#define pf(...) /* */
#define pr(...) /* */
#endif

namespace roman {
struct romantbl_t {
    const char *rom;
    int num;
} table[] = {
    { "M",  1000,   },
    { "CM", 900,    },
    { "D",  500,    },
    { "CD", 400,    },
    { "C",  100,    },
    { "XC", 90,     },
    { "L",  50,     },
    { "XL", 40,     },
    { "X",  10,     },
    { "IX", 9,      },
    { "V",  5,      },
    { "IV", 4,      },
    { "I",  1,      },
};

map<int,string> num2rom;
map<string,int> rom2num;

void romaninit(){
    for(int i = 0; i < sizeof(table)/sizeof(table[0]); i++){
        rom2num.insert(make_pair(table[i].rom, table[i].num));
        num2rom.insert(make_pair(table[i].num, table[i].rom));
    }
}

int roman2num(string rom){
    int len = rom.length();
    int t = 0, p = 0;
    for(int i = len - 1; i >= 0; i--){
        string c = rom.substr(i,1);
        int n = rom2num[c];
        if(n < p){ t -= n; } else { t += n; }
        p = n;
    }
    return t;
}

string num2roman(int num){
    string t;
    for(int i = 0; i < sizeof(table)/sizeof(table[0]); i++){
        int n = table[i].num;
        while(num >= n){
            t += num2rom[n]; num -= n;
        }
    }
    return t;
}
}

using namespace roman;

int main(){
    string s;
    romaninit();
    while(getline(cin, s)){
        cout << roman2num(s) << endl;
    }
    return 0;
}