ZOAC 7
Time limit2sMemory limit1024 MB
Given an N by M grid of Z, O, A, and C, find the maximum count of each letter on a right-or-down path from the top-left to the bottom-right cell that may teleport once.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Matrix
- Solved
- No attempts yet
Problem
A grid of rows and columns is given, and each cell contains one of Z, O, A, C.
Hyukjun and his friends start at row 1, column 1 and move to row , column , collecting characters along the way.
Each move goes one cell to the right (increasing column number) or one cell down (increasing row number).
Also, when the current cell is row , column , you may teleport exactly once to row , column that satisfies or .
Put differently, when teleporting from a green cell in the figure below, you can land on any of the purple cells.

Hyukjun: I will only take
Z!Ikjun: Then I will only take
O.Dongwoo: I will only take
A!Jinwoo: I only take
C.
Find the maximum number of characters that Hyukjun and his friends can each collect.
Input
The first line contains and .
The next lines each contain elements of row of the grid, separated by spaces.
Each element is one of Z, O, A, C.
Output
Print the maximum number of collected characters for each of Z, O, A, C, separated by spaces.
Hint
Python 3 users are recommended to submit with PyPy3.