• 19



A scientist has put you in charge of simulating his experiment vat where he adds progressively coloured cells that multiply with time. You must follow their instructions, and tell them the results when the experiment concludes.

The scientist will first tell you details about their experiment, namely max, how many cells can be in the vat at any time, and cycles, how long the experiment will go for.

Then, during cycles cycles, the scientist will send a command line, in the format "name initialCount power", where name is the type of the cells, initialCount is the initial number of cells being added, and power is the strength of the cells.


Before you consider the command lines sent at each cycle by the scientist, cells already in the vat must multiply.

- if there is enough space, all cells are doubled (each number of cells of each category is multiplied by two).

- if there is not enough space for all types to be doubled, then only types of cells with the highest power will grow. The remaining territory will be split evenly (floor division) among the types of cells with the highest power.
Example :
Maximum of cells : 100
Blue (power 1) : 42 cells ===> 49 cells
Red (power 1) : 33 cells ===> 40 cells
Green(power 0): 10 cells ===> 10 cells

- When the vat is exactly full, all type cells with higher power will begin to "eat" one type cells with lower power : every cell type of the higher power will take a tenth of its desired growth (floor division : a type of 39 cells will take 3 cells) from the cell type with the lowest power who has enough cells to be eaten.
Example :
Maximum of cells : 90
Blue (power 1) : 42 cells ===> 46 cells
Red (power 1) : 33 cells ===> 36 cells
Green(power 0): 10 cells ===> 3 cells
Cyan (power 0) : 5 cells ===> 5 cells
Note : There is no case where two cells type are available candidates to be "eaten", there is always only one.


When receiving a command line, you should first check if the name in the input is a real color.

- The scientist might tell you to stop. This message will always come in the format STOP! 0 0. When you receive it, ouput the results and ignore the following cycles.

- If the color is valid, but the scientist tells you to put in too many cells considering the remaining space, you must print OVERFLOW! and ignore the following cycles.

- if the name is anything else, no valid type cells is added, hence wait for the next cycle (if any).

Results should follow this format:

EMPTY: (how many empty spaces are remaining if > 0)
Cell names: (how many cells of that type exist if > 0)

Cell counts should be outputted in the same order that they were added in the vat.

EDIT : the puzzle statement has been extensively edited by @Razovsky
Line 1: Two space-separated integers, max and cycles
Next cycles lines: space-separated values for the added cell consisting of the word name, and two integers, initialCount and power
Line 1: "OVERFLOW!", a value corresponding to EMPTY, or the value of the first cell
Next cycles lines: If not overflowed, all non-printed cells
1 ≤ max ≤ 500
1 ≤ cycles ≤ 10
0 ≤ initialCount ≤ 100
0 ≤ power ≤ 2
10 1
Red 10 0
Red: 10

A higher resolution is required to access the IDE