IXth Problem
Time limit1sMemory limit1024 MB
- Level
Not classified yet
- Solved
- No attempts yet
Problem
Emily recently learned about the Roman Empire and its civilization at school. One aspect she found especially interesting is the number system the Romans used, Roman numerals. The Roman numeral system uses seven distinct symbols, each with a different value and written as a letter: I is , V is , X is , L is , C is , D is , and M is . Multiples of , , , and are written according to the following table:
Most entries in this table are formed additively, that is, by summing the values of the symbols. For example, LXX is . The columns for 4 and 9, however, use subtractive notation: IV is read as , IX is read as , and so on.
Every number from to is written as a combination of entries from the table, using at most one entry from each row and going from bottom to top. For example, is MMXXI and is DXCIV. Note that this number system cannot write numbers greater than , and subtractive notation is allowed only in the six cases above. For example, IC is not considered a valid Roman numeral.
Emily found a bunch of old Scrabble sets in her attic. She threw out all tiles with letters other than the Roman digits and started forming Roman numerals from the remaining tiles. It is easy to form valid numerals by using just one tile per Roman numeral, but what is the minimal number of numerals that can be formed while still using all the available tiles?
Input
The input consists of:
- One line with seven integers (), which are the numbers of
M,D,C,L,X,V, andItiles that must be used.
There is at least one tile, that is, .
Output
Output an integer , the minimal possible number of Roman numerals that can be formed while using all tiles in the input. Then output an optimal solution in the following format.
- An integer , the number of distinct Roman numerals used in this solution.
- pairs of a Roman numeral and a positive integer indicating how many times this numeral is used in this solution.
The solution must consist of exactly numerals in total and must use exactly the specified number of each letter. The Roman numerals in the solution must be distinct. You do not need to minimize . If there is more than one optimal solution, any one of them is accepted.