Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
英大文字からなる文字列 S が与えられます。
S のうち A 以外の文字をすべて . に置き換えた文字列を出力してください。
制約
- S は英大文字からなる長さ 1 以上 100 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
ATCODER
出力例 1
A......
S のうち A であるのは 1 文字目だけなので、S の 2 文字目から 7 文字目をすべて . にした文字列を出力してください。
入力例 2
BANANA
出力例 2
.A.A.A
S に A が 2 つ以上含まれることもあります。
入力例 3
CORRECT
出力例 3
.......
S に A が 1 つも含まれないこともあります。
Score : 100 points
Problem Statement
You are given a string S consisting of uppercase English letters.
Output the string obtained by replacing every character in S other than A with ..
Constraints
- S is a string of length between 1 and 100 (inclusive) consisting of uppercase English letters.
Input
The input is given from Standard Input in the following format:
S
Output
Output the answer.
Sample Input 1
ATCODER
Sample Output 1
A......
Since the first character is the only occurrence of A in S, output the string obtained by changing the second through seventh characters of S to ..
Sample Input 2
BANANA
Sample Output 2
.A.A.A
S may contain two or more occurrences of A.
Sample Input 3
CORRECT
Sample Output 3
.......
S may contain no occurrence of A.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
棒が 1 本あります。 この棒には切れ込みが N-1 箇所入っており、切れ込みによって N 個の部分に分かれています。
それぞれの部分の長さは端から順に L_1,L_2,\dots,L_N です。
切れ込みを 1 箇所選び、そこで棒を折って 2 本の棒にするとき、折ってできる 2 本の棒の長さの差の絶対値の最小値を求めてください。
ただし切れ込みの幅は無視でき、折ってできる 2 本の棒の長さはそれぞれの棒に含まれる部分の長さの総和になります。
制約
- 2 \leq N \leq 100
- 1 \leq L_i \leq 10^5
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N L_1 L_2 \dots L_N
出力
答えを出力せよ。
入力例 1
4 5 2 3 8
出力例 1
2
それぞれの切れ込みで折ったとき、以下のようになります。
- 端から 1 番目の切れ込みで折ったとき、できる 2 本の棒の長さはそれぞれ 5,13 で、長さの差の絶対値は 8 です。
- 端から 2 番目の切れ込みで折ったとき、できる 2 本の棒の長さはそれぞれ 7,11 で、長さの差の絶対値は 4 です。
- 端から 3 番目の切れ込みで折ったとき、できる 2 本の棒の長さはそれぞれ 10,8 で、長さの差の絶対値は 2 です。
入力例 2
7 31 41 59 26 53 58 97
出力例 2
51
入力例 3
10 67011 35764 33042 24098 63738 98760 17199 68579 21812 45408
出力例 3
28105
Score : 200 points
Problem Statement
There is one stick. This stick has N-1 notches, which divide it into N parts.
The lengths of the parts are L_1,L_2,\dots,L_N in order from one end.
When choosing one notch and breaking the stick there to obtain two sticks, find the minimum possible absolute value of the difference between the lengths of the two resulting sticks.
Here, the width of a notch can be ignored, and the length of each resulting stick is the sum of the lengths of the parts it contains.
Constraints
- 2 \leq N \leq 100
- 1 \leq L_i \leq 10^5
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N L_1 L_2 \dots L_N
Output
Output the answer.
Sample Input 1
4 5 2 3 8
Sample Output 1
2
Breaking the stick at each notch results in the following:
- Breaking at the first notch from the end results in two sticks of lengths 5 and 13, with an absolute difference of 8.
- Breaking at the second notch from the end results in two sticks of lengths 7 and 11, with an absolute difference of 4.
- Breaking at the third notch from the end results in two sticks of lengths 10 and 8, with an absolute difference of 2.
Sample Input 2
7 31 41 59 26 53 58 97
Sample Output 2
51
Sample Input 3
10 67011 35764 33042 24098 63738 98760 17199 68579 21812 45408
Sample Output 3
28105
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋君は N 日間の帰省で実家に滞在しています。
実家では毎日おやつが用意されており、i 日目のおやつのカロリーは A_i です。
高橋君は体調管理のために、直近 M 日間で食べたおやつのカロリーの合計が K を超えないならばおやつを食べることを繰り返します。
具体的には i=1,2,\dots,N の順に、以下のルールに従って i 日目のおやつを食べるかどうかを決定します。
- i 日目のおやつを食べたと仮定したときに \max(i-M+1,1) 日目から i 日目までの間に食べたおやつのカロリーの合計が K 以下ならば、i 日目のおやつを実際に食べる。そうでないならば、i 日目のおやつを食べない。
i=1,2,\dots,N それぞれについて、高橋君が i 日目のおやつを食べるかどうかを判定してください。
制約
- 1 \leq M \leq N \leq 2 \times 10^5
- 1 \leq K \leq 10^{15}
- 1 \leq A_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M K A_1 A_2 \dots A_N
出力
N 行出力せよ。i 行目には高橋君が i 日目のおやつを食べる場合 Yes と、食べない場合 No と出力せよ。
入力例 1
5 3 83 48 73 59 90 21
出力例 1
Yes No No No Yes
それぞれの日において、おやつを食べたと仮定したときの直近 3 日間で食べたおやつのカロリーの合計は以下のようになります。
- 1 日目:48
- 2 日目:48+73=121
- 3 日目:48+59=107
- 4 日目:90
- 5 日目:21
入力例 2
7 4 728 187 816 349 609 255 308 175
出力例 2
Yes No Yes No Yes No Yes
入力例 3
10 3 1368290936 216519459 804733999 297250023 775422599 287963235 999315644 354987425 974810607 653940822 117157941
出力例 3
Yes Yes Yes No Yes Yes No No Yes Yes
Score : 300 points
Problem Statement
Takahashi is staying at his parents' house for an N-day homecoming trip.
At his parents' house, a snack is prepared every day, and the calorie count of the snack on day i is A_i.
To manage his health, he repeats the following: he eats the snack on a given day if and only if the total calorie count of the snacks he has eaten in the most recent M days does not exceed K.
Specifically, in the order i=1,2,\dots,N, he decides whether to eat the snack on day i according to the following rule:
- Assuming that he eats the snack on day i, if the total calorie count of the snacks eaten from day \max(i-M+1,1) through day i is at most K, then he actually eats the snack on day i. Otherwise, he does not eat the snack on day i.
For each i=1,2,\dots,N, determine whether Takahashi eats the snack on day i.
Constraints
- 1 \leq M \leq N \leq 2 \times 10^5
- 1 \leq K \leq 10^{15}
- 1 \leq A_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M K A_1 A_2 \dots A_N
Output
Output N lines. The i-th line should contain Yes if Takahashi eats the snack on day i, and No otherwise.
Sample Input 1
5 3 83 48 73 59 90 21
Sample Output 1
Yes No No No Yes
For each day, assuming that he eats the snack, the total calorie count of the snacks eaten in the most recent three days is as follows:
- Day 1: 48
- Day 2: 48+73=121
- Day 3: 48+59=107
- Day 4: 90
- Day 5: 21
Sample Input 2
7 4 728 187 816 349 609 255 308 175
Sample Output 2
Yes No Yes No Yes No Yes
Sample Input 3
10 3 1368290936 216519459 804733999 297250023 775422599 287963235 999315644 354987425 974810607 653940822 117157941
Sample Output 3
Yes Yes Yes No Yes Yes No No Yes Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
H 行 W 列のグリッドがあります。各マスは、空マスまたは爆弾マスのどちらかです。上から i 行目、左から j 列目のマスを (i,j) と表します。グリッドの情報は H 個の長さ W の文字列 S_1, S_2 , \dots ,S_H によって与えられ、S_i の j 文字目が . のとき (i,j) は空マス、S_i の j 文字目が # のとき (i,j) は爆弾マスです。
また、空マス (i,j) について i 行目にも j 列目にも爆弾マスが存在しないとき、そのマスを 安全な空マス と呼びます。
1 回の移動で今いるマスから、上下左右に隣り合う空マスに移動することができます(爆弾マスには移動できません)。以下の条件を満たす空マス (i, j) の数を求めてください。
- (i,j) から K 回以下の移動で 安全な空マス へ到達できる。
制約
- 1 \le H,W \le 5\times 10^5
- H\times W \le 5\times 10^5
- 0 \le K \le H\times W-1
- S_i は
.と#からなる長さ W の文字列 - H,W,K は整数
入力
入力は以下の形式で標準入力から与えられる。
H W K S_1 S_2 \vdots S_H
出力
条件を満たす空マスの個数を出力せよ。
入力例 1
3 3 1 #.. ... ..#
出力例 1
5
安全な空マス は (2,2) だけです。(2,2) へ 1 回以下の移動で到達できる空マスは (1,2),(2,1),(2,2),(2,3),(3,2) の 5 マスですから、答えは 5 です。
入力例 2
2 3 0 ... ...
出力例 2
6
爆弾マスが存在しないため、6 マスすべてが 安全な空マス です。したがって、どの空マスも移動回数 0 回で条件を満たします。
入力例 3
5 7 2 ..#.... ..#.... ....... ...#... ...#...
出力例 3
29
Score : 400 points
Problem Statement
There is a grid with H rows and W columns. Each cell is an empty cell or a bomb cell. Let (i,j) denote the cell at the i-th row from the top and the j-th column from the left. The grid is given by H length-W strings S_1, S_2 , \dots ,S_H: if the j-th character of S_i is ., (i,j) is an empty cell, and if the j-th character of S_i is #, (i,j) is a bomb cell.
For an empty cell (i,j), if there is no bomb cell in the i-th row nor in the j-th column, that cell is called a safe empty cell.
In one move, you can move from the current cell to an adjacent empty cell in the up, down, left, or right direction (you cannot move to a bomb cell). Find the number of empty cells (i, j) satisfying the following condition:
- A safe empty cell can be reached from (i,j) in at most K moves.
Constraints
- 1 \le H,W \le 5\times 10^5
- H\times W \le 5\times 10^5
- 0 \le K \le H\times W-1
- S_i is a string of length W consisting of
.and#. - H, W, and K are integers.
Input
The input is given from Standard Input in the following format:
H W K S_1 S_2 \vdots S_H
Output
Output the number of empty cells satisfying the condition.
Sample Input 1
3 3 1 #.. ... ..#
Sample Output 1
5
The only safe empty cell is (2,2). There are five empty cells from which you can reach (2,2) in at most one move: (1,2),(2,1),(2,2),(2,3),(3,2), so the answer is 5.
Sample Input 2
2 3 0 ... ...
Sample Output 2
6
Since there is no bomb cell, all six cells are safe empty cells. Therefore, every empty cell satisfies the condition with zero moves.
Sample Input 3
5 7 2 ..#.... ..#.... ....... ...#... ...#...
Sample Output 3
29
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
頂点に 1 から N の番号がついた N 頂点 M 辺の単純連結無向グラフが与えられます。i 番目の辺は頂点 a_i と頂点 b_i を結んでいます。
奇数個の頂点からなる閉路が存在するか判定し、存在するならば 1 つ求めてください。
厳密には、次の条件を全て満たす整数列 (v_1,v_2,\ldots,v_K) が存在するか判定し、存在するなら 1 つ求めてください。
- K は 3 以上の奇数である
- v_1,v_2,\ldots,v_K はすべて異なる
- 1\le i \le K を満たす全ての整数 i について、頂点 v_i と頂点 v_{i+1} の間に辺がある。ただし v_{K+1} = v_1 とする
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1 \le T \le 2\times10^5
- 1 \le N, M \le 2\times10^5
- 全てのテストケースにおける N の総和は 2×10^5 以下
- 全てのテストケースにおける M の総和は 2×10^5 以下
- 1 \le a_i,b_i \le N
- a_i\ne b_i
- 与えられるグラフは単純連結無向グラフである
- 入力される値はすべて整数である
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケースは以下の形式で与えられる。
N M a_1 b_1 a_2 b_2 \vdots a_M b_M
出力
各テストケースについて、条件を満たす数列が存在しない場合は -1 を出力せよ。存在する場合は以下の形式で 1 つ出力せよ。
K v_1 v_2 \ldots v_K
条件を満たす数列が複数存在する場合、どれを出力しても正解となる。
入力例 1
4 3 3 1 2 2 3 1 3 7 7 1 2 2 3 3 4 1 4 4 5 5 6 6 7 5 5 1 2 2 3 3 4 4 5 1 5 9 10 1 2 2 3 3 4 4 5 1 5 6 7 7 8 8 9 6 9 1 6
出力例 1
3 2 1 3 -1 5 3 2 1 5 4 5 3 2 1 5 4
一つ目のテストケースでは、数列 (2,1,3) が条件を満たします。(2, 1), (1, 3), (3, 2) のいずれも辺として存在します。また v = (2, 3, 1) などを出力しても正解となります。
二つ目のテストケースでは、奇数個の頂点からなる閉路はないため条件を満たす数列は存在しません。
Score : 450 points
Problem Statement
You are given a simple connected undirected graph with N vertices numbered 1 through N and M edges. The i-th edge connects vertices a_i and b_i.
Determine whether there exists a cycle consisting of an odd number of vertices, and if one exists, find one such cycle.
Formally, determine whether there exists an integer sequence (v_1,v_2,\ldots,v_K) satisfying all of the following conditions, and if one exists, find one such sequence.
- K is an odd number at least 3.
- v_1,v_2,\ldots,v_K are all distinct.
- For every integer i with 1\le i \le K, there is an edge between vertices v_i and v_{i+1}, where v_{K+1} = v_1.
You are given T test cases; solve each of them.
Constraints
- 1 \le T \le 2\times10^5
- 1 \le N, M \le 2\times10^5
- The sum of N over all test cases is at most 2\times10^5.
- The sum of M over all test cases is at most 2\times10^5.
- 1 \le a_i,b_i \le N
- a_i\ne b_i
- The given graph is a simple connected undirected graph.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
Each test case is given in the following format:
N M a_1 b_1 a_2 b_2 \vdots a_M b_M
Output
For each test case, if there is no sequence satisfying the conditions, output -1. If one exists, output one such sequence in the following format:
K v_1 v_2 \ldots v_K
If multiple sequences satisfy the conditions, any of them will be accepted.
Sample Input 1
4 3 3 1 2 2 3 1 3 7 7 1 2 2 3 3 4 1 4 4 5 5 6 6 7 5 5 1 2 2 3 3 4 4 5 1 5 9 10 1 2 2 3 3 4 4 5 1 5 6 7 7 8 8 9 6 9 1 6
Sample Output 1
3 2 1 3 -1 5 3 2 1 5 4 5 3 2 1 5 4
In the first test case, the sequence (2,1,3) satisfies the conditions: the edges (2, 1), (1, 3), (3, 2) all exist. Outputs such as v = (2, 3, 1) are also accepted.
In the second test case, there is no cycle with an odd number of vertices, so no sequence satisfies the conditions.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 525 点
問題文
xy 平面上に凸 N 角形 P があります。 P の頂点には反時計回りに頂点 1,2,\dots,N と番号が付けられており、頂点 i の座標は (x_i,y_i) です。
Q 個の質問に答えてください。
j 番目の質問では、相異なる 2 頂点 u_j,v_j が与えられます。 ただし頂点 u_j,v_j は P の周上において隣り合いません。
頂点 u_j と頂点 v_j を結ぶ線分で P を二つの多角形に分割し、頂点 u_j から頂点 v_j へ向かう直線の右側にある方を P' としたとき、P' の幾何中心(多角形の内部を一様な密度の薄板とみなしたときの重心)の座標を求めてください。
制約
- 4 \leq N \leq 3 \times 10^4
- 1 \leq Q \leq 2 \times 10^5
- |x_i|,|y_i| \leq 5 \times 10^5
- (x_1,y_1),(x_2,y_2),\dots,(x_N,y_N) は反時計回りに凸多角形をなす
- P のすべての内角は 180 度未満
- 1 \leq u_j,v_j \leq N
- u_j \neq v_j
- P の周上で頂点 u_j と頂点 v_j は隣り合わない
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N Q x_1 y_1 x_2 y_2 \vdots x_N y_N u_1 v_1 u_2 v_2 \vdots u_Q v_Q
出力
Q 行出力せよ。 j 行目には、j 番目の質問の答えの座標を (p_j,q_j) としたとき、p_j,q_j をこの順に空白区切りで出力せよ。
p_j,q_j のそれぞれについて、真の解との絶対誤差または相対誤差が 10^{-6} 以下であれば正解として扱われる。
入力例 1
5 4 4 1 1 3 -3 2 -1 -2 2 -1 2 4 5 3 1 4 4 2
出力例 1
-1.000000000000000 1.000000000000000 0.864197530864198 1.209876543209877 0.198198198198198 0.828828828828829 1.391304347826087 0.434782608695652
1 つ目の質問について、P' は (1,3),(-3,2),(-1,-2) を頂点とする三角形であり、幾何中心は (-1,1) です。
2 つ目の質問について、P' は (2,-1),(4,1),(1,3),(-3,2) を頂点とする四角形であり、幾何中心は \left(\frac{70}{81},\frac{98}{81}\right) です。
入力例 2
10 10 480451 142016 404696 274257 7097 390813 -134582 195390 -167108 143104 -359470 -180367 -406293 -273476 74335 -494832 486873 -391664 493159 -292349 5 8 8 6 5 2 5 1 10 3 1 7 3 5 2 5 4 9 1 8
出力例 2
-167917.431926206833203 -207268.968943785114349 126221.026579795702381 -68771.715180365014040 111801.954824466955586 -124063.178438661167072 99590.935535112756283 -153792.378227918737825 321662.353363423237819 113722.100435559268610 43574.061369374611386 102411.022160871806300 -98197.666666666666667 243102.333333333333333 79984.488622522356481 269160.678085376751920 -3267.792578363696043 -210454.379232334372417 46257.363593155145794 -38546.483790939251583
Score : 525 points
Problem Statement
There is a convex N-gon P on the xy-plane. The vertices of P are numbered 1,2,\dots,N counterclockwise, and the coordinates of vertex i are (x_i,y_i).
Answer Q questions.
The j-th question gives two distinct vertices u_j and v_j. Here, the vertices u_j and v_j are not adjacent on the boundary of P.
The segment connecting vertices u_j and v_j divides P into two polygons; let P' be the one lying to the right of the directed line from vertex u_j to vertex v_j. Find the coordinates of the geometric center of P' (the centroid obtained by regarding the interior of the polygon as a thin plate of uniform density).
Constraints
- 4 \leq N \leq 3 \times 10^4
- 1 \leq Q \leq 2 \times 10^5
- |x_i|,|y_i| \leq 5 \times 10^5
- (x_1,y_1),(x_2,y_2),\dots,(x_N,y_N) form a convex polygon in counterclockwise order.
- All interior angles of P are less than 180 degrees.
- 1 \leq u_j,v_j \leq N
- u_j \neq v_j
- Vertices u_j and v_j are not adjacent on the boundary of P.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N Q x_1 y_1 x_2 y_2 \vdots x_N y_N u_1 v_1 u_2 v_2 \vdots u_Q v_Q
Output
Output Q lines. On the j-th line, letting (p_j,q_j) be the coordinates of the answer to the j-th question, output p_j and q_j in this order, separated by a space.
The answer is considered correct if, for each of p_j and q_j, its absolute or relative error from the true value is at most 10^{-6}.
Sample Input 1
5 4 4 1 1 3 -3 2 -1 -2 2 -1 2 4 5 3 1 4 4 2
Sample Output 1
-1.000000000000000 1.000000000000000 0.864197530864198 1.209876543209877 0.198198198198198 0.828828828828829 1.391304347826087 0.434782608695652
For the first question, P' is the triangle with vertices (1,3),(-3,2),(-1,-2), and its geometric center is (-1,1).
For the second question, P' is the quadrilateral with vertices (2,-1),(4,1),(1,3),(-3,2), and its geometric center is \left(\frac{70}{81},\frac{98}{81}\right).
Sample Input 2
10 10 480451 142016 404696 274257 7097 390813 -134582 195390 -167108 143104 -359470 -180367 -406293 -273476 74335 -494832 486873 -391664 493159 -292349 5 8 8 6 5 2 5 1 10 3 1 7 3 5 2 5 4 9 1 8
Sample Output 2
-167917.431926206833203 -207268.968943785114349 126221.026579795702381 -68771.715180365014040 111801.954824466955586 -124063.178438661167072 99590.935535112756283 -153792.378227918737825 321662.353363423237819 113722.100435559268610 43574.061369374611386 102411.022160871806300 -98197.666666666666667 243102.333333333333333 79984.488622522356481 269160.678085376751920 -3267.792578363696043 -210454.379232334372417 46257.363593155145794 -38546.483790939251583
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 600 点
問題文
H 行 W 列のグリッドがあります。各マスには +, -, # のいずれか一文字が書かれています。上から i 行目、左から j 列目のマスを (i,j) と表します。グリッドの情報は H 個の長さ W の文字列 S_1, S_2 , \dots ,S_H によって与えられ、S_i の j 文字目が (i,j) に書かれています。
あなたは、次の操作を 0 回以上行うことができます。
#でないマスを 1 つ選ぶ。「選んだマスから#のマスを通ることなく、隣接するマスへ左・右・下のいずれかの方向に移動することだけで到達できるマス」をすべて#に変える。ただし、選んだマス自身も到達できるマスに含まれる。
操作後のグリッドにおける、+ のマスの個数から - のマスの個数を引いた値としてあり得る最大値を求めてください。
制約
- 1 \le H,W \le 30
- S_i は
+,-,#からなる長さ W の文字列 - H, W は整数
入力
入力は以下の形式で標準入力から与えられる。
H W S_1 S_2 \vdots S_H
出力
答えを出力せよ。
入力例 1
2 3 +-+ --+
出力例 1
1
(2,1) を選ぶと、2 行目の全てのマスが # に変わります(上方向には移動できないことに注意してください)。残る 1 行目には + が 2 個、- が 1 個あるため、値は 2-1=1 で、これが最大です。
入力例 2
3 3 +-- -#- #+#
出力例 2
1
(1,1) を選ぶと、(3, 2) を除いて # になります。マス (1, 1) 自身も到達できるマスに含まれることや、 # のマスは通れないことに注意してください。
入力例 3
5 7 ++#--++ -+---+# ##++-++ --#-++- +---#++
出力例 3
5
Score : 600 points
Problem Statement
There is a grid with H rows and W columns. Each cell has one of the characters +, -, # written on it. Let (i,j) denote the cell at the i-th row from the top and the j-th column from the left. The grid is given by H length-W strings S_1, S_2 , \dots ,S_H: the j-th character of S_i is written on (i,j).
You can perform the following operation zero or more times:
- Choose one cell that is not
#. Change to#all cells that are "reachable from the chosen cell only by moving to an adjacent cell in the left, right, or down direction without passing through a cell that is#." Here, the chosen cell itself is included among the reachable cells.
Find the maximum possible value of the following value in the grid after the operations: the number of + cells minus the number of - cells.
Constraints
- 1 \le H,W \le 30
- S_i is a string of length W consisting of
+,-,#. - H and W are integers.
Input
The input is given from Standard Input in the following format:
H W S_1 S_2 \vdots S_H
Output
Output the answer.
Sample Input 1
2 3 +-+ --+
Sample Output 1
1
If you choose (2,1), all cells in row 2 change to # (note that you cannot move upward). The remaining row 1 has two cells of + and one cell of -, so the value is 2-1=1, and this is the maximum.
Sample Input 2
3 3 +-- -#- #+#
Sample Output 2
1
If you choose (1,1), all cells except (3, 2) become #. Note that the chosen cell (1, 1) itself is included among the reachable cells, and that you cannot pass through a cell that is #.
Sample Input 3
5 7 ++#--++ -+---+# ##++-++ --#-++- +---#++
Sample Output 3
5