#include using namespace std; using ll = long long; using ul = unsigned long; using ull = unsigned long long; string encode(string s) { vector vs = { "IV","IX","XL","XC","CD","CM" }; vector vd = { "IIII","VIIII","XXXX","XXXXL","CCCC","CCCCD" }; string ss{ s }; for (unsigned i = 0; i < vs.size(); ++i) { size_t pos = 0; while ((pos = ss.find(vs[i], pos)) != string::npos) { ss.replace(pos, vs[i].length(), vd[i]); pos += vd[i].length(); } } return ss; } string decode(string s) { vector vs = { "CM","CD","XC","XL","IX","IV" }; vector vd = { "CCCCD","CCCC","XXXXL","XXXX","VIIII","IIII" }; string ss{ s }; for (unsigned i = 0; i < vs.size(); ++i) { size_t pos = 0; while ((pos = ss.find(vd[i], pos)) != string::npos) { ss.replace(pos, vd[i].length(), vs[i]); pos += vs[i].length(); } } return ss; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector r(n); for (auto&& it : r) { string s; cin >> s; it = encode(s); } vector v = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 }; map me = { {'I',1},{'V',5},{'X',10},{'L',50},{'C',100},{'D',500},{'M',1000} }; map md = { {1,"I"},{4,"IV"},{5,"V"},{9,"IX"},{10,"X"},{40,"XL"},{50,"L"},{90,"XC"},{100,"C"},{400,"CD"},{500,"D"},{900,"CM"},{1000,"M"} }; int sum{ 0 }; for (const auto& it : r) { for (const auto& it2 : it) sum += me[it2]; } string res; if (sum > 3999) res = "ERROR"; else { for (const auto& it : v) { int d = sum / it; if (d) { for (int i = 0; i < d; ++i) res += md[it]; sum -= it * d; } } res = decode(res); } cout << res << "\n"; return 0; }