Codeforces Round 746 (Div. 2)
F2
Alice and Recoloring 2
Codeforces
2800
原题
← F1→
翻译中…

The difference between the versions is in the costs of operations. Solution for one version won't work for another!

翻译中…

Alice has a grid of size $n \times m$, initially all its cells are colored white. The cell on the intersection of $i$-th row and $j$-th column is denoted as $(i, j)$. Alice can do the following operations with this grid:

翻译中…
  • Choose any subrectangle containing cell $(1, 1)$, and flip the colors of all its cells. (Flipping means changing its color from white to black or from black to white). This operation costs $1$ coin.
  • Choose any subrectangle containing cell $(n, 1)$, and flip the colors of all its cells. This operation costs $3$ coins.
  • Choose any subrectangle containing cell $(1, m)$, and flip the colors of all its cells. This operation costs $4$ coins.
  • Choose any subrectangle containing cell $(n, m)$, and flip the colors of all its cells. This operation costs $2$ coins.
翻译中…

As a reminder, subrectangle is a set of all cells $(x, y)$ with $x_1 \le x \le x_2$, $y_1 \le y \le y_2$ for some $1 \le x_1 \le x_2 \le n$, $1 \le y_1 \le y_2 \le m$.

翻译中…

Alice wants to obtain her favorite coloring with these operations. What's the smallest number of coins that she would have to spend? It can be shown that it's always possible to transform the initial grid into any other.

翻译中…

Input

翻译中…

The first line of the input contains $2$ integers $n, m$ ($1 \le n, m \le 500$) — the dimensions of the grid.

翻译中…

The $i$-th of the next $n$ lines contains a string $s_i$ of length $m$, consisting of letters W and B. The $j$-th character of string $s_i$ is W if the cell $(i, j)$ is colored white in the favorite coloring of Alice, and B if it's colored black.

翻译中…

Output

翻译中…

Output the smallest number of coins Alice would have to spend to achieve her favorite coloring.

翻译中…

Note

翻译中…

In the first sample, it's optimal to just apply the fourth operation once to the rectangle containing cells $(2, 2), (2, 3), (3, 2), (3, 3)$. This would cost $2$ coins.

翻译中…

样例

Input #1
3 3
WWW
WBB
WBB
Output #1
2
InputCopy OutputCopy
Input #2
10 15
WWWBBBWBBBBBWWW
BBBBWWWBBWWWBBB
BBBWWBWBBBWWWBB
BBWBWBBWWWBBWBW
BBBBWWWBBBWWWBB
BWBBWWBBBBBBWWW
WBWWBBBBWWBBBWW
WWBWWWWBBWWBWWW
BWBWWBWWWWWWBWB
BBBWBWBWBBBWWBW
Output #2
68
InputCopy OutputCopy
题目信息
题号
1592F2
时间限制
2 s
内存限制
256 MB
标签
构造
网络流
图匹配
贪心

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。