#P17822. [COCI 2026/2027 #1] Kockice
[COCI 2026/2027 #1] Kockice
Problem Description
Mr. Malnar is playing a game with dice. The rules are simple. First, each player rolls any number of dice. Then, the player may choose to flip at most dice so that the sides opposite the ones currently facing up become visible, or choose not to flip any dice at all. It is well known that the numbers on any pair of opposite sides of a die add up to (e.g., and are on opposite sides). Each player's score is the sum of the numbers showing on the dice after all flips have been made. The player with the highest score wins.
Mr. Malnar really wants to win, so he has decided to cheat. He secretly took a photo of the dice on the table and sent it to you, asking for help. The photo has dimensions . Since the dice are the same colour as the table, their outlines cannot be seen. Fortunately, the dots on the dice are clearly visible. Each dot is represented by an o, while each empty space is represented by a -. The six sides of a die are represented in the photo as shown below:
--- o-- o-- o-o o-o o-o
-o- --- -o- --- -o- o-o
--- --o --o o-o o-o o-o
1 2 3 4 5 6
Quick! Mr. Malnar needs your help! What is the maximum score he can achieve?
Input Format
The first line contains two natural numbers, () and (), representing the width of the photo and the maximum number of dice Mr. Malnar may flip.
Each of the next three lines contains a string of length , consisting only of - and o, representing the photo you received. The photo will always display at least one rolled die.
The sides of the dice will appear only in the orientations shown in the statement, i.e., they cannot be rotated. The photo will always represent a valid arrangement of dice that could result from rolling them onto the table.
Output Format
In the first and only line, write one number - the maximum score Mr. Malnar can achieve.
3 1
o-o
---
o-o
4
11 4
-o-o----o--
--o------o-
-o-o------o
9
17 2
o--o-o-------o---
-o-o-o--o--------
--oo-o---------o-
20
Hint
Clarification of the first example
Mr. Malnar rolled one die and it showed the number . Since , Mr. Malnar can flip over the one die. The number opposite of is (because ) so if he flips the die he will get a score of . However, it is optimal to not flip the die because the sum is bigger when the dice shows than when the dice shows .
Clarification of the second example
Mr. Malnar rolled two dice and got the numbers and . He can flip up to four dice; however, there are only two dice on the table so he can either flip one die, flip both of them, or not flip any. If he doesn't flip any die, he will get a score of . If he only flips the die showing , its value will change to making his score . If he only flips the die showing , its value will change to making his score . If he flips both dice, they will show the numbers and making his score . Therefore, the maximum score he can achieve is - this can be achieved by flipping the die showing .
Clarification of the third example
Mr. Malnar rolled four dice and got the value , , and , in that order as seen on the photo. He can flip up to two of them. A maximum score is achieved if he flips the die showing (which turns into ) and the die showing (which turns into ). This changes his score from to .
Scoring
| Subtask | Points | Constraints |
|---|---|---|
| 1 | 9 | |
| 2 | 10 | All the dice show either or . |
| 3 | 13 | |
| 4 | 18 | No additional constraints. |