#include using namespace std; #define all(x) begin(x), end(x) #define sz(x) (ll)(x).size() using ll = long long; using ld = long double; array letterCount, todoCount; array letters = {"M", "D", "C", "L", "X", "V", "I", "*"}; vector> res; void printRes() { for (auto e : res) { cout << e.first << " " << e.second << endl; } cout << "-------------------------------"<< endl; } bool append(ll& l, ll& r, ll m, string x) { ll appendPos = r; for (; l < r && todoCount[m] > 0; l++) { if (todoCount[m] >= res[l].second) { todoCount[m] -= res[l].second; res[l].first += x; } else { res.push_back(res[l]); res[l].second -= todoCount[m]; res.back().first += x; res.back().second = todoCount[m]; todoCount[m] = 0; swap(res.back(), res[appendPos]); appendPos++; break; } } return todoCount[m] == 0; } bool appendMany(ll& l, ll& r, ll m, char x) { ll remain = todoCount[m] % 3; todoCount[m] /= 3; if (!append(l, r, m, string(3, x))) { todoCount[m] *= 3; todoCount[m] += remain; return false; } else if (remain != 0){ todoCount[m] = 1; if (!append(l, r, m, string(remain, x))) { todoCount[m] = remain; return false; } } return true; } bool solve(ll m, ll d, ll c) { if (todoCount[m] > todoCount[c]) return false; ll l = 0; ll r = sz(res); todoCount[c] -= todoCount[m]; if (!append(l, r, m, letters[c] + letters[m])) return false; ll oldL = l; ll oldSize = sz(res); if (!append(l, r, d, letters[d])) return false; r += sz(res) - oldSize; appendMany(oldL, r, c, letters[c][0]); //the remaing C's will be XC's return true; } bool solve(ll m) { res = {{"", m}}; todoCount = letterCount; if (!solve(7, 7, 0)) return false; if (!solve(0, 1, 2)) return false; if (!solve(2, 3, 4)) return false; if (!solve(4, 5, 6)) return false; return todoCount[6] == 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); for (ll i = 0; i < 7; i++) cin >> letterCount[i]; letterCount[7] = 0; ll l = 0; ll r = *max_element(all(letterCount)); while (l + 1 < r) { ll m = (l + r) / 2; if (solve(m)) { r = m; } else { l = m; } } solve(r); cout << r << endl; cout << sz(res) << endl; for (auto e : res) { cout << e.first << " " << e.second << endl; } }