#include #include #include using namespace std; map mp; int to_int(string s){ int prev = 0,sum = 0; for(int i = s.length() - 1;i >= 0;i--){ int add = mp[s[i]]; if(add < prev) sum -= add; else sum += add; prev = add; } return sum; } string to_rome(int n){ string s; int num[] = {1000,500,100,50,10,5,1}; char c[] = {'M','D','C','L','X','V','I'}; string chg[] = {"DCCCC","CCCC","LXXXX","XXXX","VIIII","IIII"}; string to[] = {"CM ","CD ","XC ","XL ","IX ","IV "}; for(int i = 0;i < 7;i++){ for(int j = 0;j < n / num[i];j++) s += c[i]; n %= num[i]; } for(int i = 0;i < 6;i++){ for(int j = 0;j < (int)s.length() - (int)chg[i].length() + 1;j++){ if(s.substr(j,chg[i].length()) == chg[i]){ for(int k = 0;k < chg[i].length();k++) s[j + k] = to[i][k]; } } } string t; for(int i = 0;i < s.length();i++){ if(s[i] != ' ') t += s[i]; } return t; } int main(){ int n; cin >> n; mp['I'] = 1; mp['V'] = 5; mp['X'] = 10; mp['L'] = 50; mp['C'] = 100; mp['D'] = 500; mp['M'] = 1000; int ans = 0; for(int i = 0;i < n;i++){ string s; cin >> s; ans += to_int(s); } if(ans >= 4000) cout << "ERROR" << endl; else cout << to_rome(ans) << endl; return 0; }