#P4158. Maximize Correct Painted Cells

    ID: 17406 Type: Default 1000ms 256MiB

Maximize Correct Painted Cells

Maximize Correct Painted Cells

Windy has N wooden boards, each divided into M cells. Each cell is required to be painted either red or blue as specified by a target pattern. Windy can perform at most T painting operations. In one operation, she selects a contiguous segment on a single board and paints all cells in that segment with one chosen color.

Note that each cell can be painted at most once. A cell is considered correctly painted if and only if it is painted (exactly once) and its color matches the target color. If a cell is either not painted or painted with the wrong color, it is counted as incorrect.

Assuming Windy performs at most T painting operations (which she may distribute arbitrarily among the boards, and she does not have to use all operations), what is the maximum number of cells she can correctly paint?

The painting operations on different boards are independent. For each board, if you choose a contiguous segment [l,r] and paint it with a color, the contribution is equal to the number of cells in that segment that already have that color in the target pattern. Formally, for a segment spanning cells l to r (1-indexed), the contribution is

$$\max\Bigl(\#\{i\in[l,r]:\ \text{target}[i]='R'\},\; \#\{i\in[l,r]:\ \text{target}[i]='B'\}\Bigr). $$

You may choose a set of disjoint segments on each board (i.e. segments do not overlap on a board) and allocate at most T total operations across boards so that the sum of contributions is maximized. Output this maximum possible number of correctly painted cells.

inputFormat

The first line contains three integers N, M, and T separated by a space, where N is the number of boards, M is the number of cells per board, and T is the maximum number of painting operations allowed.

Then follow N lines, each containing a string of length M comprised only of the characters 'R' and 'B'. This string represents the target color pattern of the board (with 'R' for red and 'B' for blue).

outputFormat

Output a single integer, the maximum number of cells that can be painted correctly using at most T painting operations.

sample

1 5 1
RRRBR
4