We need to parse the problem carefully.
We have an n x m binary matrix. We can change any 1 to 0 (i.e., replace ones with zeroes). After we perform these replacements, the score is computed as follows:
- For each column j (1..m):
- Find the topmost 1 in that column: i.e., the smallest row index i such that a[i][j] = 1. If no 1 exists, skip the column.
- Then consider the next min(k, n - i + 1) elements in that column, starting from that element (i.e., rows i, i+1, ..., i+L-1, where L = min(k, n-i+1)). Actually the phrasing: "look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score." So we include the topmost 1 itself? It says "starting from the element he found" and then "count the number of 1's among these elements". So it includes that starting element. So indeed the block of length L = min(k, n - i + 1) beginning at row i (the topmost 1). So the score contribution of column j is the number of ones in rows i .. i+L-1, where i is the row index of the topmost 1 in column j.
We can replace ones with zeroes arbitrarily. We want to maximize the total score (sum over columns). Among all sets of replacements achieving that maximum score, we want the minimum number of replacements (i.e., the smallest number of ones turned to zero). We need to output maxScore and minReplacements.
Constraints: n up to 100, m up to 100, k up to n. So we can try to compute something per column independently? Possibly the columns are independent because the score per column depends only on that column's ones, and replacements affect only that column (since each replacement changes a single cell; but the decisions per column might be independent? However, there is no cross-column interaction in the scoring. The total score is sum over columns, and replacements are counted across all columns. So the problem is separable: we can consider each column separately, decide which replacements to make in that column to maximize its contribution to the total score (subject to maximizing the column's contribution? Actually we need to maximize the sum over columns. Since columns are independent, the optimal overall strategy is to independently, for each column, choose a set of replacements that yields the maximum possible contribution from that column, and then we would sum those maxima. However, there is also the objective of minimizing the total number of replacements, and this might require coordination across columns? But since the total replacements is sum of replacements per column, and the score is sum of scores per column, and the decisions per column are independent (no constraints linking columns), we can minimize the total replacements by minimizing replacements in each column individually while achieving that column's maximum possible contribution. Because if we can achieve the maximum column contribution with minimal replacements in that column, then doing that for all columns yields maximal total score and sum of minimal replacements, which is globally minimal because any global solution must achieve max score per column (otherwise total wouldn't be max). But is it possible that sacrificing a column's contribution (i.e., not achieving its max) could allow fewer replacements overall while still achieving the same total score because we could compensate with other columns? But we want to maximize total score. So we would only accept a lower column contribution if it would allow a much larger increase in another column's contribution due to replacements? But replacements are only removals, they can't increase a column's contribution; they only reduce the ones. Actually replacements can only turn ones to zeros, thus they can only decrease the number of ones in a column. However, the score depends on the configuration of ones, particularly on the topmost 1 and then the count in the window. Changing ones can affect which row becomes the topmost 1 (by eliminating a 1 above). This can change the window location and thus possibly increase the count of ones in the window? Wait: we are allowed to replace ones with zeroes. That means we can remove some ones. Removing a 1 could potentially change the topmost 1 to be lower (i.e., at a greater row index) if we delete a 1 that is currently the topmost. That might shift the window to a region with more ones? For example, if the topmost 1 is at row i, and there are many ones below, but perhaps if we delete that topmost 1, the new topmost 1 becomes i' > i, and we count the ones in window starting from i'. The count in that new window might be larger than the original count because the window may include more ones? But we must consider that we also removed a 1, so the count might change. Also, we can remove ones that are within windows to reduce the score? Actually we want to maximize the score, so we would want the count to be as large as possible. So maybe by strategically deleting some ones above, we can cause the topmost 1 to shift downwards to a row where the following block of length L (which depends on starting row) contains more ones. This is akin to we can choose which row becomes the topmost 1 (by deleting all ones above it) and then count the ones in the window starting from that row (but note we also delete some ones potentially). So we can "choose" a row i to be the first 1 by ensuring that all rows above i in that column are 0 (by possibly replacing those ones with zeros). Then the score contribution for that column would be: Let L = min(k, n - i + 1). Then the count of ones in rows i..i+L-1, but note that we may have also replaced some ones within that window with zeros? Actually we can also replace ones in the window with zeros if we want, but that would reduce the count, which is not beneficial if we want to maximize the score. However, if we need to set some rows above i to zero to make i the topmost, we might also need to delete some ones inside the window if they are currently 1? But we could choose not to delete them. We have the freedom to decide which ones to replace. For a given column, we start with a binary column vector of length n. We can change any 1 to 0. After changes, the resulting column has some pattern of ones. The score for that column is computed as described: find the topmost 1, i0; then count ones in i0 .. i0+L-1 (L = min(k, n-i0+1)). So the score is a function of the final column.
We want to choose a final column (by zeroing some ones) to maximize the score. And then among all maximizing choices, minimize the number of ones zeroed (replacements). That is per column. Since total score sum is separable and total replacements sum, the global optimum is simply the sum of per-column maximum scores, and the sum of per-column minimal replacements to achieve that per-column maximum.
Thus we can solve each column independently.
Now the problem reduces to: Given a binary column of length n, with original ones set, and parameter k, we can turn any 1 to 0. For a given final configuration, define:
Let i be the first row (smallest index) with a 1. If there is no 1, contribution = 0 (skip). Then let L = min(k, n-i+1). Contribution = number of ones in rows i..i+L-1.
We want to maximize this contribution. And among configurations achieving max, minimize the number of ones turned to 0 (i.e., maximize the number of ones remaining? But careful: replacements count is the number of original ones we changed to 0. So if we keep a 1, that's no replacement; if we set a 1 to 0, that's a replacement. We cannot change 0 to 1. So the final configuration must be a subset of the original ones (we can only remove ones). So effectively, we choose a subset S of the original 1-positions (the ones we keep). Then the contribution is based on the smallest row in S, and then count of S in the window.
We need to compute, for each column, the maximum achievable contribution, and the minimum number of deletions (original ones not kept) to achieve that.
Since n ≤ 100, we can brute force per column: Consider each possible choice of which row will become the topmost 1. For a given candidate row i (1-indexed), we must delete all original ones in rows 1..i-1 (since they must become 0 to make i the topmost). Those are forced deletions. Then for rows i and beyond, we can decide which ones to keep; but we want to maximize the contribution. The contribution is the number of kept ones in the window [i, i+L-1] where L = min(k, n-i+1). However, we are free to also delete ones outside the window; that wouldn't affect the contribution but would increase replacements, which we want to minimize. So for a given i, the optimal strategy to maximize contribution while minimizing replacements would be: Keep all ones in rows i..i+L-1 (since they contribute to the score), and delete only the necessary ones above i (the ones in rows 1..i-1). What about ones in rows i+L..n? They don't affect the score, and we can keep them (since they are 1 originally) without incurring replacements, but is there any downside to keeping them? They might affect the topmost? No, because they are below the window, and we already have a topmost at i; those ones do not interfere with the topmost. So we can keep them. However, we must ensure that the column after changes has topmost exactly i, i.e., no ones above i. Since we delete all ones above i, that's satisfied. So for candidate i, the contribution we can achieve is the number of original ones in rows i..i+L-1 (call this count C_i). And the number of replacements needed to achieve that configuration is: number of original ones in rows 1..i-1 (since we need to delete those). Also, note: we might also need to consider if there are any original ones in rows i..i+L-1 that we might want to delete? No, because they increase the contribution. So we keep them. For rows i+L..n, we can keep them (no replacements). So this configuration yields contribution C_i with replacements D_i = number of ones in rows 1..i-1.
But wait: There is a nuance: If we keep all ones in the window, the contribution is C_i. Could there be an alternative within the same i that yields a higher contribution? Possibly if we also delete some ones in the window? That would reduce contribution, not helpful. So maximum contribution for a given i is indeed C_i (since we can't increase beyond the number of original ones in that segment). So the maximum achievable contribution overall is max over i of C_i. However, there is also possibility of making the column all zeros (i.e., no topmost 1). That gives contribution 0. But max over i will be at least 0 (if all C_i are 0? Actually if all C_i are 0, maybe the best is 0 with no deletions? But if there are ones somewhere, you could set them to zero and get 0, but maybe that would incur replacements. But maybe you could also leave some ones, but then the topmost will be some i and contribution C_i may be >0. So max over i will capture positive contributions if any.)
Thus, the maximum possible score for the column is M = max_{i in {1..n}} C_i, but also consider the possibility of not having any 1: that gives 0, which might be lower unless all C_i = 0.
But is it always possible to achieve C_i? Yes, by deleting all ones in rows 1..i-1 and keeping others as is. That yields contribution exactly C_i. However, we must also ensure that after those deletions, the topmost 1 is indeed i. That requires that there is at least one 1 in rows i..i+L-1? Actually it requires that there is at least one 1 in rows i..n, because otherwise the topmost wouldn't exist. But if we keep all ones in i..i+L-1 and maybe beyond, but if there is no original one in i..i+L-1? C_i counts original ones in that window. If C_i = 0, but there might be original ones in rows i+L..n. In that case, if we keep those ones, the topmost 1 would be the smallest row >= i+L that contains a 1, which is > i. So then the topmost would not be i, because we have not kept any one in i..i+L-1, but there might be ones below. However, we said we keep all ones from i..n, but if we keep ones below, and there is no one at i..i+L-1, then the first 1 would be at some row r > i (the first row with an original one in [i, n]). That would change the contribution to be based on r, not i. So our configuration for candidate i would not actually yield topmost i if there is no one at i or later? Actually we assumed we set topmost to i by deleting all ones above i, but we also need to ensure that row i itself is a 1 in the final configuration, otherwise the topmost would be the first row >= i with a 1. So to force topmost exactly i, we need that after deletions, there is a 1 at row i. If originally a_i is 0, we cannot set it to 1. So if the original a_i is 0, we cannot make i the topmost by only deleting above, because we cannot create a 1 at i. Therefore, the candidate i is only feasible if the original a_i = 1? Actually we can also make i the topmost if we keep some 1 at a later row? No, topmost means the smallest index with a 1. So to have topmost exactly i, we need that after changes, there is a 1 at row i, and no 1 in rows < i. So we must have a_i = 1 originally and we keep it. If a_i = 0 originally, we cannot turn it to 1, so i cannot be the topmost. So candidate i is only viable if the original column has a 1 at row i.
But wait: what if there is a 1 at row i, but we could also delete it? That would make topmost later. So to achieve topmost i, we need to keep a_i (since we cannot create a 1), and delete all ones in rows 1..i-1. So candidate i is feasible only if original a_i = 1. Then the contribution becomes the number of ones we keep in rows i..i+L-1. Since we keep a_i, and we may keep others in that window. But we might also consider not keeping a_i? That would not yield topmost i, so not for this candidate.
Thus, the set of candidate rows i for which we can make i the topmost are exactly the rows where a_i = 1. Also we could consider the scenario where we delete all ones, giving contribution 0, but that is a special case where no 1 exists. But maybe there is also scenario where we have topmost at some row i where originally a_i = 0 but we could have a 1 at row i? No, can't.
So we need to consider only rows i with a_i = 1.
But is it possible to have a topmost i even if a_i = 0 but we keep a 1 at some row > i and delete all rows < i? Then the topmost would be that row > i, not i. So i must be the smallest row with a 1. So it must be a row containing a 1. So indeed only rows with original ones can be topmost.
But could we achieve a contribution that is not exactly C_i for some i? Suppose we choose to delete some ones in the window that are 1, but also perhaps we delete some ones below the window that might affect something? No, only topmost and the window matter. The topmost is determined by the smallest row with a 1. So any configuration yields some topmost row i (if any ones exist). The contribution is then the number of ones in rows i..i+L-1. This number cannot exceed the original number of ones in that segment (since we can only remove ones). So the maximum possible contribution overall is the maximum over all possible topmost rows i (that are achievable) of the number of original ones in that segment. But wait: is it always achievable to have topmost i and also have all original ones in that segment present? If we want to keep all ones in that segment, we must keep them. That includes keeping a_i (since it's in segment). But if we keep all ones in segment, we keep a_i (which is 1 by feasibility). Also we must delete all ones above i. That yields topmost i and contribution = count of original ones in segment. So yes, if we keep all ones in the segment, we achieve that contribution. So for each i where a_i=1, we can achieve contribution count_ones(i, L) where L = min(k, n-i+1). So M = max_{i: a_i=1} count_ones(i..i+L-1).
But is there any other way to achieve a contribution higher than that? Perhaps by making the column all zeros? That gives 0. No.
But wait: what if we delete some ones above i but also delete some ones in the segment? That would only lower contribution. So not helpful.
Thus the maximum score per column is max over i (with a_i=1) of the number of ones in the contiguous block of length L starting at i.
But also consider the possibility that after deletions, the topmost becomes some row i where originally a_i=0? Not possible.
But is it possible that we delete some ones in the segment but then by deleting some above we might cause the topmost to be a row that originally had a 0? No, because that row would still be 0. So no.
Thus, the max score for the column is determined.
Now the minimal number of replacements needed to achieve the maximum score.
For a given column, we need to achieve a total contribution of M (the maximum). There may be multiple configurations that yield M. Among them, we want to minimize the total number of ones turned to zero (i.e., deletions). Since each column is independent, we can compute the minimal deletions per column needed to achieve at least M contribution? Actually we need exactly achieving M? The score will be computed based on final configuration; we want the maximum possible score, so we want a configuration that yields score = M (max). So we need to achieve exactly M for that column (unless there is a configuration that yields > M, but M is maximum, so not possible). So we need to find the minimal number of deletions such that the resulting column's contribution = M.
We have to consider all possible final configurations that give contribution M. Some of them may involve different topmost rows, or different sets of deletions. The minimal deletions is the minimum over all such configurations of the number of original ones removed.
Given the structure, what configurations can give contribution M? Suppose M is achieved by some set of rows I_max = { i : a_i=1 and count(i) = M } where count(i) = number of ones in [i, i+L-1]. Possibly multiple i achieve the same M. For each such i, we can achieve M by: delete all ones in rows 1..i-1, and keep all ones in rows i..n (or at least keep all ones in the segment, but we can also optionally delete ones outside segment without affecting contribution but they would increase deletions, so optimal for that i would be to keep all ones from i..n, except perhaps we might delete some ones within the segment? No because that would reduce contribution unless we compensate by something else? But could there be a configuration where topmost is some j not in I_max, but the contribution is still M? That would require that count of ones in the window starting at j equals M, which would mean count(j) = M, so j would be in I_max as well. So the topmost must be one of those achieving M (or maybe there is a scenario where topmost is at row i, but we delete some ones in the window and then somehow the count remains M? No, if we delete any one in the window, the count would decrease, so we would get less than M unless we also change the topmost? But the topmost is determined by the smallest 1. If we delete some ones in the window, the count in that window reduces. Could we possibly increase the window size? No, L depends on i, not on the ones. So to get M, the count in the window must be exactly M, so we need to have exactly M ones in that window. Since original count in that window is at most M (if M is maximum), to achieve M we must keep all original ones in that window? Not necessarily: if the original count in that window is greater than M? But M is defined as maximum over i of count(i). For a given i, count(i) is the number of original ones in that window. Could there be an i such that count(i) > M? No, M is maximum. So for any i, count(i) ≤ M. So the original ones in the window for the chosen topmost i cannot exceed M. To achieve exactly M, we need the number of kept ones in that window to be M. Since the original count in that window is some number C_i ≤ M. If we keep all original ones, we get C_i. To reach M, we need C_i = M. So the only windows that can yield M are those with original count exactly M. If we delete any original one from that window, the count becomes less than M, so not maximal. If we add any (can't). So we must keep all original ones in that window. Therefore, for a configuration achieving M, the topmost i must be such that count(i) = M, and we must keep all ones in its window. Additionally, we must ensure that there are no ones above i, i.e., we delete all ones in rows 1..i-1. And we may keep or delete ones below the window; keeping them doesn't affect contribution and incurs no deletions, so optimal to keep them. So for each i with count(i)=M, the minimal deletions needed to achieve M with i as topmost is D_i = number of ones in rows 1..i-1. However, we must also consider if there is any alternative configuration that yields M but with a different topmost i where count(i) < M but due to some deletions above we might cause the window to shift? No, the topmost i determines the window; the count in that window cannot exceed the original count if we only delete; so maximum for that i is count(i) which is < M, so cannot achieve M. So indeed only i with count(i)=M are candidates.
Thus, the minimal replacements for the column is the minimum of D_i over all i such that count(i)=M. And if M=0? That is the case when there are no ones in the column? Or when all possible topmost yield contribution 0. Let's treat M=0 separately. If M=0, then the maximum possible contribution is 0. How can we achieve 0? Two ways: either delete all ones (so column empty) or leave some ones but then the contribution may be positive. To achieve score 0, we need that for the final configuration, either there is no topmost 1 (i.e., column all zeros) or the topmost exists but the count in its window is 0. Could the count be 0? If there is a topmost 1, then at least that cell is 1, so the window includes that cell, so count ≥1. So any configuration with at least one 1 yields positive contribution (≥1). Therefore, to achieve score 0, we must have no 1 in the column. So we must delete all original ones. So replacements = total number of ones originally in that column. However, could there be a scenario where we have some ones but the topmost is such that L = 0? L = min(k, n-i+1). Since i ≤ n, n-i+1 ≥ 1, and k ≥ 1, so L ≥ 1 always. So if there is a 1, the window length is at least 1, so count at least 1. So yes, score 0 only if column all zeros.
Thus for M=0, minimal replacements = total number of ones in that column (since we must delete them all). But is there any alternative achieving M=0 with fewer replacements? Could we achieve 0 by having a topmost and then window count 0? No because topmost itself gives 1. So we must delete all ones. So replacements = total ones.
But note: maybe M=0 occurs when all count(i) = 0 for i with a_i=1? That would happen only if for every row i with a_i=1, the block of length L starting at i contains no ones? But since a_i=1, that block includes row i, so count(i) ≥ 1. So M cannot be 0 if there is any original 1. Wait: If there is any original 1, consider the smallest row i0 where a_i0=1. For i = i0, count(i0) includes at least that 1, so ≥ 1. So M ≥ 1. So M=0 only if there are no ones in the column originally. In that case, total ones = 0, so replacements = 0 to achieve score 0 (since it's already all zeros). Indeed if the column is all zeros, the maximum score is 0 and no replacements needed. So M=0 implies original column has no ones, then replacements 0.
But in problem input, there could be columns with ones. So M=0 rarely; but we can handle generally.
Thus per column, we need to compute:
- For each row i from 1 to n where a[i]==1:
- L = min(k, n - i + 1)
- Compute cnt[i] = number of ones in rows i to i+L-1 (including i).
- Then M = max(cnt[i] over i with a[i]==1). (If no such i, i.e., no ones, then M=0).
- Then among those i with cnt[i] == M, compute deletions needed: del[i] = number of ones in rows 1 to i-1.
- Minimal replacements for column = min(del[i]) for i achieving M. If M=0 (no ones), then replacements = 0.
But is there any scenario where we could achieve the same maximum score using a configuration that does not correspond to keeping all ones in the window for some i? Suppose we have two topmost candidates i and j (i < j) both with cnt = M. Could we possibly achieve M with a configuration that has topmost i but we delete some ones in the window and still get M? Only if we had more ones originally in the window, but we don't because cnt(i) = M is the count; if we delete any, we get < M. So no.
Could we achieve M with topmost i but also delete some ones below the window that might cause the topmost to change? No.
Thus the above approach seems correct.
However, let's test with the examples.
Example 1:
n=4,m=3,k=2
Matrix:
row1: 0 1 0
row2: 1 0 1
row3: 0 1 0
row4: 1 1 1
We'll process each column.
Column 1: a = [0,1,0,1] (rows 1..4)
Rows with 1: i=2, i=4.
For i=2: L = min(2, 4-2+1=3) = 2. Window rows 2-3. Ones: row2=1, row3=0 -> cnt=1.
For i=4: L = min(2, 4-4+1=1) = 1. Window rows 4-4: row4=1 -> cnt=1.
M = max(1,1)=1.
Now candidates with cnt=1: both i=2 and i=4. del[2] = ones in rows 1-1: row1=0 => 0. del[4] = ones in rows 1-3: row1=0, row2=1, row3=0 -> 1. So min del = 0. So column1 contributes max 1, replacements needed 0.
Column2: a = [1,0,1,1]
Rows with 1: i=1,3,4.
i=1: L = min(2,4)=2 => rows1-2: row1=1, row2=0 => cnt=1.
i=3: L = min(2,2)=2 => rows3-4: row3=1, row4=1 => cnt=2.
i=4: L = min(2,1)=1 => rows4-4: row4=1 => cnt=1.
M=2 (from i=3).
Candidates: i=3 cnt=2. del[3] = ones in rows1-2: row1=1, row2=0 => 1. So column2 contributes 2, replacements 1.
Column3: a = [0,1,0,1]
Rows with 1: i=2,4.
i=2: L=2, rows2-3: row2=1, row3=0 => cnt=1.
i=4: L=1, row4=1 => cnt=1.
M=1. Candidates: both i=2 and i=4. del[2] = ones rows1-1: row1=0 ->0. del[4]= ones rows1-3: row1=0, row2=1, row3=0 ->1. min del=0. So column3 contributes 1, replacements 0.
Total max score = 1+2+1=4, total replacements = 0+1+0=1. Matches output 4 1. Good.
Example 2:
n=3,m=2,k=1
Matrix:
1 0
0 1
0 0
Column1: a=[1,0,0] rows 1..3 with ones: i=1 only.
i=1: L = min(1,3)=1 => rows1-1: cnt=1. M=1. del[1]=ones rows 1-0? rows 1..0 none =>0. So column1: score 1, replacements 0.
Column2: a=[0,1,0] rows with ones: i=2.
i=2: L = min(1, 3-2+1=2) =1 => rows2-2: cnt=1. M=1. del[2] = ones rows1-1: row1=0 =>0. So column2: score 1, replacements 0. Total 2,0. Matches.
Thus the solution works for examples.
But we need to consider edge cases: multiple candidates with same cnt, we take min deletions. But is there any scenario where a candidate i with cnt=M but we could achieve M with fewer deletions than del[i] by not necessarily deleting all ones above i? Wait: to have topmost i, we need that there are no ones above i. That means every row < i that originally had a 1 must be set to 0. So we must delete all ones above i. So del[i] is forced. Could we achieve the same M with a configuration where the topmost is not i but we still get M? That would be another candidate j with cnt=M. So the minimal deletions among those is correct.
But what about the possibility of achieving M with topmost i but also deleting some ones below the window? That would increase deletions, not reduce. So not better.
Thus per column, minimal deletions = min_{i: cnt_i = M} (prefix_ones[i-1]), where prefix_ones[t] = number of ones in rows 1..t.
Now we should check if there is any scenario where we could achieve the same M but with a topmost that is not one of the original ones? For example, if we delete some ones above, the topmost might become a row that originally had a 0 but later a 1? No, topmost is the smallest row with a 1. If a row originally 0, it remains 0. So the topmost must be a row where we keep a 1. So indeed, only rows with original ones can be topmost.
Thus, the solution algorithm:
Precompute per column:
- Let col[1..n] be the values.
- Compute prefix sum of ones: pref[0]=0; for i=1..n: pref[i]=pref[i-1] + col[i].
- Also compute total ones in column: total = pref[n].
- For each i from 1 to n, if col[i]==1:
L = min(k, n-i+1)
cnt = pref[i+L-1] - pref[i-1] // number of ones in i..i+L-1
keep track of max_cnt.
- After loop, if total == 0: max_cnt = 0, min_del = 0.
- Else: For all i with col[i]==1 and cnt == max_cnt, compute del = pref[i-1] (since ones in rows 1..i-1 must be deleted). min_del = min(del) over such i.
- Add max_cnt to total_score, add min_del to total_replacements.
But wait: Is there any case where max_cnt is achieved by a configuration that doesn't correspond to keeping all ones in that window? Suppose we have a candidate i with cnt=M, but we could also achieve M by having topmost i and also deleting some ones in the window, but then we might not need to delete as many above? No, deleting above is independent; we still need to delete all above. So that wouldn't reduce deletions.
But consider a scenario where we might achieve M by having topmost at a row i that is not originally 1, but we could create a topmost at i by turning a 0 to 1? Not allowed.
Thus algorithm is straightforward.
However, we must be careful: The window length L depends on i. For i near bottom, L may be small. The count includes the topmost. So that's fine.
Now we should double-check if there is any nuance: The step "In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i,j}=1). If there are no 1's in the column, this column is skipped;" So if column skipped, contribution 0. That's handled.
Now, confirm with more test cases, maybe tricky ones.
Consider column: n=5, k=3, ones at rows: [1,0,0,1,1]. Let's compute:
Rows with ones: 1,4,5.
i=1: L = min(3,5)=3 => rows1-3: ones at 1 only => cnt=1.
i=4: L = min(3,5-4+1=2)=2 => rows4-5: ones at 4 and 5 => cnt=2.
i=5: L = min(3,1)=1 => rows5-5: ones at 5 => cnt=1.
M=2 from i=4. del[4] = ones rows1-3: row1=1 => 1. So need to delete row1. That yields final column: rows1..5: 0,0,0,1,1. Topmost is row4, window rows4-5 count=2. So score 2, replace 1. Is there any configuration that yields higher? Possibly by deleting row1 and also maybe row4? That would give topmost row5, window length1 count=1, lower. Or keep row1 and delete row4? Then topmost row1, window rows1-3 count=1. So max=2 indeed.
Now consider column: n=5, k=3, ones at rows: [1,1,0,0,1].
Rows with ones:1,2,5.
i=1: L=3 => rows1-3: ones at 1,2 => cnt=2.
i=2: L=min(3,4)=3 => rows2-4: ones at 2 => cnt=1? Actually rows2-4: row2=1, row3=0, row4=0 =>1.
i=5: L=1 => cnt=1.
M=2 from i=1. del[1]=0. So we can achieve 2 with no replacements. Indeed original column: topmost is row1, window rows1-3 count=2 => score2. So max=2, replace=0.
Now consider column: n=5, k=2, ones: [0,1,1,0,1].
Rows with ones:2,3,5.
i=2: L=min(2,4)=2 => rows2-3: ones at 2,3 => cnt=2.
i=3: L=min(2,3)=2 => rows3-4: ones at 3, row4=0 => cnt=1.
i=5: L=1 => cnt=1.
M=2 from i=2. del[2]=ones rows1-1: row1=0 =>0. So max=2, replace=0.
Now consider column: n=5, k=2, ones: [1,0,1,0,1].
Rows with ones:1,3,5.
i=1: L=2 => rows1-2: ones at1 => cnt=1.
i=3: L=2 => rows3-4: ones at3 => cnt=1.
i=5: L=1 => cnt=1.
M=1. Candidates: all three. del[1]=0, del[3]=ones rows1-2: row1=1, row2=0 =>1, del[5]=ones rows1-4: row1=1, row3=1 =>2 (but careful: rows1-4 includes rows1,2,3,4: ones at 1 and 3 =>2). So min del=0 (choose i=1). So max=1, replace=0. Indeed original topmost row1, window rows1-2 count=1 => score1.
But is there any way to get score2? If we delete row1, topmost becomes row3, window rows3-4 count=1. If we delete row1 and keep row3 and row5, topmost row3 gives 1. If we delete row1 and row3, topmost row5 gives 1. So indeed max is 1.
All good.
Now consider a scenario where a candidate i with cnt=M but del[i] is not minimal among those, but maybe we could achieve the same M with a different configuration where we don't keep all ones in the window? Suppose we have i and j with cnt(i)=M, cnt(j)=M, but perhaps we could achieve M by having topmost i but deleting some ones in the window that are not needed for the count? That would lower the count, so not M. So no.
Consider also the possibility that there might be a configuration where the topmost is not a specific row i but the column is empty, yielding 0, but if M>0, that is lower.
Thus per-column approach is correct.
Now we need to sum over m columns. Since m and n ≤ 100, we can easily compute per column O(n^2) or O(n) with prefix sums. Actually to compute cnt for each i we can precompute prefix sums, then each cnt is O(1). So per column O(n). Overall O(n*m) = 10000, trivial.
But we must be careful: The definition of L = min(k, n - i + 1). Since k ≤ n, L = min(k, remaining rows). That's correct.
Now, we should double-check if there is any catch: The score is computed after Ivan replaces any number of ones with zeroes. So he can choose which ones to replace. The replacements are the ones he turns to zero. So we count replacements as number of ones changed to zero. That's exactly number of original ones that are removed. So our per column deletions count is correct.
Now, verify with some more complex scenario: Could there be a case where the maximum score is achieved by a configuration that does not correspond to a single topmost row as we assumed? The scoring algorithm always picks the topmost 1. So any final configuration yields some topmost row i. So it's covered.
Thus the solution seems straightforward.
But wait: There is a nuance: In step 3, Ivan will look at the next min(k, n-i+1) elements starting from the element he found. The phrase "next" might be ambiguous: Does it mean the elements after the topmost? Or including the topmost? It says "starting from the element he found". So it includes the topmost. So yes.
Now, check edge case: k could be larger than n, but given constraints k ≤ n, so fine.
Now, consider columns where the maximum cnt appears at multiple i's, but maybe some i require fewer deletions because there are no ones above, but also maybe we can achieve M by not keeping all ones in the window? Could there be a configuration that yields M but uses a topmost i that originally had cnt(i) < M, but by deleting some ones above, we might cause the window to be different? No, window starts at i, so it's fixed.
Thus the approach stands.
But let's think about whether we might need to consider also the possibility of making the column all zeros if that yields the same total score as some positive? No, we want maximum score, so positive scores are better.
Now, we should verify if there might be any scenario where we can achieve a higher total score by not achieving the maximum per column individually, but by making replacements that affect multiple columns? No, columns are independent, replacements are per cell, but a replacement in one column doesn't affect other columns. So maximizing per column independently yields global maximum sum.
Thus the answer is sum of per-column maximum contributions, and sum of per-column minimal replacements to achieve those contributions.
So we can implement as described.
However, I want to test with a column where the original topmost has a count less than some other i, but we could achieve that higher count by deleting some above ones, but maybe we also need to delete some ones in the window to keep the topmost at that i? Actually if we choose i, we must delete all ones above i. That's it. We don't need to delete any ones in the window. So if the original count at i is M, then we can achieve M with deletions equal to number of ones above i. That seems minimal for that i. But could there be a configuration that achieves the same M but with fewer deletions by not deleting all ones above i? Suppose there is a one above i, but if we keep it, the topmost would be that row above i, not i. So to have topmost i, we must delete all ones above i. So yes.
Now consider column with overlapping windows: e.g., i=2 gives cnt=5, but del[2]=3. Another i=5 gives cnt=5, del[5]=1. Then we would pick i=5. That yields deletions 1. This would correspond to deleting all ones above row5. That includes ones in rows 1-4. But perhaps we could achieve the same score with i=2 but not deleting all ones above? Could we keep some ones above if they are not in the window? If we keep a one above row2, then topmost becomes that row (say row1). Then the window would start at row1, and the count might be different. Could that count also be 5? Possibly if the window from row1 also contains 5 ones. If that is the case, then we might achieve the same M with fewer deletions? But we already have i=5 with del=1. But what if i=2 del=3, but if we keep row1 (which is a 1) and then topmost becomes row1, and count from row1 is maybe also 5? Then we could achieve M without any deletions (if row1's cnt=5). That would be even better. So in that scenario, the set of i achieving M includes row1 with cnt=5 and del=0. So min del would be 0. So our approach of taking min over i with cnt=M captures that. So it's consistent.
Thus algorithm: For each column, compute for each row i (1..n):
if a[i][j] == 1:
compute cnt_i = sum_{t=i}^{i+L-1} a[t][j] (where L = min(k, n-i+1))
keep max.
Then compute deletions for rows i that have cnt_i == max: deletions_i = sum_{t=1}^{i-1} a[t][j]
take min deletions_i.
But wait: We only consider rows i where a[i][j]==1. But what if the maximum cnt occurs for a row i that does not have a 1 originally? Could that happen? Since cnt_i includes a[i][j] itself, if a[i][j]==0, then cnt_i would count only ones in i+1..i+L-1. But could that be higher than any cnt for rows with a 1? Possibly, if there is a block of ones but starting after a zero row. For example: n=3,k=2, column: [0,1,1]. Rows with ones: i=2,3.
i=2: L=min(2,2)=2 => rows2-3 => cnt=2.
i=3: L=min(2,1)=1 => cnt=1.
max=2. That's from i=2, which has a 1. Good.
But consider: n=3,k=2, column: [0,1,0]. Rows with ones: i=2 only. i=2: L=2 => rows2-3 => cnt=1. max=1.
Consider: n=3,k=3, column: [0,1,1]. i=2: L=min(3,2)=2? Actually n-i+1=2, so L=2 => rows2-3 cnt=2; i=3: L=1 cnt=1. max=2.
So seems rows with a 1 are the only ones that could be topmost. But could cnt_i for a row i where a[i]=0 be larger? Suppose column: n=5,k=5, column: [0,1,1,1,1]. Then for i=2: cnt=4; for i=1 (if we considered) but a[1]=0, but cnt_1 would be sum rows1-5 = 4? Actually rows1-5 have ones at 2,3,4,5 => 4. So cnt_1=4, equal to max. But i=1 cannot be topmost because a[1]=0. So the max cnt=4 is also achieved by i=2 with a[1]=0? Wait, i=2 has a[2]=1, cnt_2 = sum rows2-5 = 4 as well. So max=4 and there exists an i with a[i]=1 achieving it. So still fine.
But could there be a scenario where the maximum cnt occurs only for rows i that are zero? Let's try to construct: We need a column where for every row i that has a 1, the cnt_i is less than some cnt for a row j that has 0. That means that the block starting at j contains more ones than any block starting at a row with a 1. Since block starting at j includes rows j..j+L-1. If j has 0, then the block does not include a 1 at the start, but could still have many ones later. However, if there is a 1 at some row r > j, then r itself is a candidate (if a[r]=1) and its block starts at r. Could the block starting at r be smaller than the block starting at j? Possibly if the block starting at j includes some ones that are before r, but those are after j but before r. But those rows between j and r-1 are zeros? If they are zeros, then the block starting at j would only have ones from r onward, same as block starting at r, but j's block length might be larger if L is larger because L depends on starting row. For example, if j is near top, L could be k, while if r is lower, L could be smaller. So it's possible that cnt_j > cnt_r even if a_j=0 because the window is longer and captures the same ones plus possibly more? But wait, cnt_j counts ones in rows j..j+L_j-1. If a_j=0, it still counts ones in that range. If r > j, then the window from j may include rows j..r-1 which are zeros (since if they were ones, then there would be a 1 at some row < r). But by definition, r is the first row with a 1? Actually if a_j=0, the first 1 might be at some row r > j. Then for i=j (with a_j=0), the cnt_j includes rows j..j+L_j-1. Since rows j..r-1 are zeros, the ones start at r. So cnt_j = number of ones from r to min(j+L_j-1, n). For i=r (with a_r=1), its window is rows r..r+L_r-1, where L_r = min(k, n-r+1). Since L_j may be larger than L_r if j is higher and k is same? Actually L_j = min(k, n-j+1) which is at least as large as L_r because j<r => n-j+1 > n-r+1, so L_j >= L_r. So the window from j may extend further beyond r+L_r-1, thus possibly including additional ones beyond r+L_r-1. So cnt_j could be larger than cnt_r. For example: n=5,k=5, column: rows: [0,0,1,1,1]. Then candidates with ones: i=3,4,5.
i=3: L=min(5,3)=3? Actually n-i+1=3, so L=3, window rows3-5 => cnt=3.
i=4: L=2, rows4-5 => cnt=2.
i=5: L=1 => cnt=1.
max among those =3. Now consider i=2 (a_2=0): L=min(5,4)=4, window rows2-5: rows2=0, rows3=1,4=1,5=1 => cnt=3. So also 3. i=1: L=5, window rows1-5: row1=0, rows2=0,3=1,4=1,5=1 => cnt=3. So the max is 3, achieved by i=1,2,3. But i=1 and i=2 are not feasible as topmost because they are 0. But i=3 is feasible and also gives cnt=3. So max is still achievable with a row that has a 1.
But can we create a scenario where the max cnt is only achieved by rows with 0? That would require that for every row i with a 1, cnt_i is strictly less than some cnt_j for a j with a_j=0. That would mean that the maximum block of ones (of length up to k) starting at some row does not align with a row that contains a 1 at its start. But if the block starts at a row j with a_j=0, then the first 1 in that block is at some row r>j. Then consider the block starting at r: its window length L_r might be smaller than L_j, but it includes the same ones from r onward, but not the ones before r (which are zeros). So cnt_r = number of ones from r to r+L_r-1. Since L_r could be less than L_j, cnt_r could be less than cnt_j if there are additional ones after r+L_r-1 that are still within j+L_j-1. In other words, if the ones extend beyond r+L_r-1, then cnt_j could be larger because it captures those extra ones. But could it be that cnt_r is strictly less than cnt_j, and no other row with a 1 gives cnt as high as cnt_j? Let's try to construct such a scenario.
We need a column where the ones are located such that the best window (max ones in a contiguous block of length up to k) starts at a position that is not a 1. However, the window considered for scoring is not any contiguous block; it is always anchored at the topmost 1. So to achieve that many ones in the window, the topmost 1 must be at the start of that window. If the window starts at a row that is not a 1, then the topmost 1 cannot be that row because there is no 1 there. The topmost 1 would be the first 1 in that window, which is some row r > j. Then the window used for scoring would start at r, not j. So you cannot get a window starting at j because the topmost is determined by the first 1. So the maximum achievable contribution is indeed max over rows i that have a 1 of cnt_i. Because only those rows can be the start of the window in the final configuration. The fact that a window starting at a zero row might have more ones is irrelevant because you cannot make that window be the one used in scoring without having a 1 at the start. However, could we artificially create a 1 at that row by not deleting something? No, it's zero originally, can't become 1. So indeed, we cannot start the window at a zero row.
Thus our approach considering only rows with original ones is correct.
But wait: Could we delete some ones such that the topmost 1 becomes a row that originally had a 0? That would require that we set a 1 at that row, which we can't. So no.
Thus we are good.
Now we need to implement.
But before finalizing, let's consider if there is any scenario where we might want to delete ones within the window to achieve a higher score? That would reduce the count, so not beneficial.
However, maybe there is a scenario where by deleting a 1 in the window, we cause the topmost to shift to a lower row? No, the topmost is the smallest index with a 1; if we delete a 1 that is the current topmost, the topmost shifts to the next 1 below. That could change the window to a longer one (if the new topmost is lower, L might be larger? Actually if topmost shifts down, i increases, so n-i+1 decreases, so L might become smaller or stay the same as min(k, ...). Typically moving down reduces the window length (or stays same if k is limiting). But it could also increase the number of ones in the new window if the block below is dense. So by deleting the topmost, we might increase the score. That is exactly what we are capturing by considering different i as topmost after deletions. In our formulation, for each i (with a_i=1), we consider making i the topmost by deleting all ones above i. That includes possibly deleting the original topmost if it is above i. So we are covering that. So the algorithm already accounts for the possibility of deleting some ones to shift the topmost to a lower row, potentially increasing the count.
Now we must be careful: The deletions we count for a candidate i are only the ones above i. But what if we also need to delete some ones in the window to ensure that i is indeed the topmost? Actually i is the topmost if there are no ones above i. So we only need to delete those. We don't need to delete any ones in the window. But could there be a scenario where we want to delete some ones in the window to achieve a better score? No, that would reduce the count. So no.
Now, what about the possibility of making the column all zeros? That would give score 0. But if M>0, we wouldn't do that. So ignore.
Now, compute min replacements for column: among all i with cnt_i = M, we take min deletions = min_{i} pref[i-1].
But need to confirm that for each such i, we can indeed achieve M with exactly pref[i-1] deletions and no more. Is it always possible to keep all ones in rows i..n? If we keep all ones in i..n, then the window count will be exactly cnt_i (since we keep all ones in that segment). But note that cnt_i counts ones in rows i..i+L-1 only. However, if we keep all ones in i..n, we might also keep ones that are below i+L-1. That's fine; they don't affect the score. But we must also ensure that after deletions, there is no 1 above i, so topmost is i. That is satisfied. So yes.
But what if there is a 1 in rows i..i+L-1 that we want to keep, but we could also keep it; no issue.
Thus for each i, configuration: delete exactly the ones above i; keep all others (including ones in window and below). That yields exactly M and deletions = pref[i-1].
But is there any possibility to achieve M with fewer deletions than pref[i-1] for that i? Suppose there are some ones above i that we could keep if they don't become topmost because there is another 1 above i? But if we keep any one above i, then the topmost becomes that row, not i. So that would change the topmost to some j < i. Then the contribution would be based on j, not i. Could that still give M? Possibly if cnt_j also equals M, and we might have fewer deletions because we didn't delete those above j. But then we are effectively using a different i (j) with its own deletions. So that's covered by considering other i. So for a given i, to have topmost exactly i, we must delete all ones above i, so deletions = pref[i-1] is necessary. So it's the minimal for that specific topmost choice.
Thus the global minimal deletions for the column is min over i achieving M of pref[i-1].
Now, consider the possibility of achieving M with a configuration where the topmost is i, but we also delete some ones above i but we could have kept some of them? No.
Now, consider a scenario where there is a row i with a_i=1, cnt_i = M, but there is also a row j < i with a_j=1 and cnt_j = M as well? Then we would pick the one with fewer deletions? Actually deletions for i is pref[i-1]; for j is pref[j-1] (since j < i, pref[j-1] <= pref[i-1]? Not necessarily, because there could be ones between j and i-1, so pref[i-1] includes those, so pref[i-1] >= pref[j-1]. So the earlier row (smaller index) generally has fewer deletions (since there are fewer rows above). But wait, pref[j-1] counts ones in rows 1..j-1, which are fewer rows, but if there are many ones in rows 1..j-1, it could be high; but there could be fewer ones in rows 1..i-1 if the ones are between j and i-1? Actually pref[i-1] = pref[j-1] + (ones in rows j..i-1). Since ones are non-negative, pref[i-1] >= pref[j-1]. So earlier rows always have less or equal deletions than later rows (if we delete all above). So the earliest row (smallest i) among those achieving M would minimize deletions? Not necessarily: Suppose i=1 has a_1=1, cnt_1 = M, deletions = pref[0] = 0. That is minimal. So indeed the minimum deletions is likely achieved by the smallest i such that cnt_i = M. However, there could be a scenario where a_i=1 and cnt_i=M, but i is not the smallest row with a 1? Actually if i=1 has a_1=1 but cnt_1 is not M (maybe M is larger), then the smallest i with cnt_i=M might be later. But among those with cnt_i=M, the one with smallest i will have the smallest deletions (since deletions increase with i). So we can simply take the smallest i with cnt_i=M to minimize deletions. But is it always true that if i1 < i2 then pref[i1-1] ≤ pref[i2-1]? Yes, because prefix sums are non-decreasing. So indeed, the minimal deletions is achieved at the smallest row index i that has a_i=1 and cnt_i=M. So we don't need to compute all; just find the max M, then find the first i (from top) where a_i=1 and cnt_i=M, and use its deletions. However, be careful: Could there be a case where a smaller i has a_i=1 and cnt_i=M, but we might need to delete ones above it? That's deletions pref[i-1]. Since i is smaller, pref[i-1] is smaller or equal. So indeed it's best. So we can just take the minimal i. But wait: Could there be a scenario where we could achieve M by using a configuration where the topmost is i (with cnt_i=M) but we don't need to delete all ones above i because we could also delete some of the ones in the window or something to compensate? No, as argued, to have topmost i we must delete all ones above i. So deletions at least pref[i-1]. So the smallest i gives the smallest deletions. So we can just take the topmost feasible i achieving M.
Thus we can simplify: For each column:
- Compute cnt_i for all i where a_i=1.
- M = max(cnt_i).
- Then find the smallest i such that a_i=1 and cnt_i == M.
- del = prefix[i-1] (or sum of ones from 1 to i-1).
- If no such i (i.e., no ones? then M=0, del=0).
That yields the minimal replacements? But is it always guaranteed that using the smallest i yields a valid configuration that achieves M? Yes, because we can delete all ones above i and keep all others. That yields topmost i and window count = cnt_i = M. So valid.
But wait: Could there be a scenario where the smallest i with cnt_i=M is not actually the best in terms of total replacements because maybe we could achieve M with a larger i but with fewer deletions if we also keep some ones above? That can't happen because if we keep any one above i, topmost shifts to that row, and then we need to consider that row's cnt. If that row also has cnt=M, then we could use that row, which is smaller i, and deletions would be less. So the smallest i with cnt=M is indeed the global optimum. However, consider the case where there is a row i with cnt_i=M, but there is also a row j < i with a_j=1 but cnt_j < M, so not candidate. Then we have to use i. But could we achieve M by using a configuration where we keep some ones above i but also delete some ones to ensure that the topmost is still i? If we keep a 1 above i, then that row becomes topmost. So no. So i is forced to be the smallest row with a 1 in final config. So we need to delete all ones above i. So deletions is exactly pref[i-1]. So the minimal deletions is pref[i-1] for the smallest i that can achieve M. But is it always possible that the smallest i achieving M is indeed the smallest i among those with cnt_i=M? Yes. So we don't need to consider larger i if they have higher deletions. So minimal deletions = min_{i: cnt_i=M} pref[i-1] = pref[ (min i such that cnt_i=M) - 1 ].
Thus we can compute easily.
But wait: Could there be a scenario where we achieve the same M by a configuration where the topmost is i, but we don't need to delete all ones above i because we also delete the ones at i? That would change topmost, not i.
Thus I think the minimal deletions is simply the deletions needed for the smallest i achieving M.
However, is there a possibility that we could achieve M with a topmost i that is not originally a 1? No.
Now, check with example 1 column2: cnt_i: i=1 cnt=1, i=3 cnt=2, i=4 cnt=1. M=2, smallest i with cnt=2 is i=3. deletions=pref[2]=? pref[2]= row1+row2 = 1+0=1. That's what we used. So minimal deletions=1.
Column1: M=1, smallest i with cnt=1? i=2 (since i=2 cnt=1, i=4 cnt=1). smallest is 2, deletions pref[1]=0. So min=0. Column3: smallest i with cnt=1 is i=2, deletions=0. So matches.
Now test a scenario where there is a tie and the smallest i may have more deletions? Actually pref increases with i, so smallest i has smallest deletions. So fine.
But consider a scenario: row1 has a1=1, cnt1=5, deletions=0. row5 has a5=1, cnt5=5, deletions=pref[4] which is >0. So smallest i is row1, min deletions=0.
Now if row1 had a1=1 but cnt1=4, not max; row2 a2=1 cnt2=5, deletions pref[1] maybe 1 if row1 is 1; but row1 is 1 and we need to delete it to make row2 topmost, so deletions=1. If row3 also has cnt=5, deletions=pref[2] which is >= pref[1]+? So smallest i with max is row2, deletions pref[1]. That is min. So our approach works.
Thus we can simplify: for each column, find M and then find the first row i from top (smallest index) where a_i=1 and cnt_i == M, then use deletions = prefix[i-1]. If there is no such i (i.e., no ones, M=0), then deletions=0.
But is it always the case that the first i with max cnt yields the minimal deletions? Yes, as argued because deletions are monotonic in i.
Now, one more nuance: In the scenario where there are multiple i with cnt_i=M, but maybe the smallest i with cnt_i=M is not feasible because to achieve M with that i, we might need to keep some ones in its window, but also need to ensure that there are no ones above i. That's fine. But could there be a scenario where the smallest i with cnt_i=M, but there is a row j < i that also has a 1 and we must delete it, and that row j might be in the window of i? Actually j < i, so not in window. So we delete it. That's fine.
Thus the algorithm is sound.
Now we should also consider the possibility of achieving the same M with a configuration that uses topmost i but with fewer deletions because we might not need to delete some ones above i if they are not ones? Already pref[i-1] counts only ones. So if there are zeros above, they don't count. So deletions count only actual ones removed. So we already account for that.
Now, let's implement.
Pseudo-code:
Read n,m,k.
Initialize total_score = 0, total_replace = 0.
For each column j from 1 to m:
// read column? we can process row-wise and accumulate per column? Since n,m small, we can store matrix.
Let col[1..n] = a[1..n][j].
Compute pref[0..n]: pref[0]=0; for i=1..n: pref[i] = pref[i-1] + col[i].
// Also compute total ones? not needed.
// Find M and best i.
M = 0
best_i = n+1 (or None)
For i from 1 to n:
if col[i] == 1:
L = min(k, n - i + 1)
cnt = pref[i+L-1] - pref[i-1]
if cnt > M:
M = cnt
best_i = i // track smallest i with max? we want smallest overall, so when cnt > M we set best_i = i (since this is the first time we see a new max, it's the smallest for that max). But when cnt == M, we should not update best_i if we want the smallest i; if we iterate from 1 to n, we can set best_i only when cnt > M (new max) and when cnt == M and best_i not set? Actually we want the smallest i with cnt == M after we know M. We can first compute M, then second pass to find smallest i with cnt==M and col[i]==1. Simpler: compute M in first pass, then in second pass find first i with col[i]==1 and cnt_i==M. Or we can keep track: if cnt > M, set M=cnt, best_i=i; if cnt == M and i < best_i (if we keep best_i), we could update, but we need best_i to be the smallest i achieving current max. Since we iterate i increasing, if we update only on >, then when we later find another i with equal M, we won't update because cnt == M, not >. So best_i would remain the first i that achieved the max. That's exactly the smallest i with that max (since we encountered it earlier). But careful: suppose we see i1 with cnt=5, set M=5, best_i=i1. Later we see i2 with cnt=6, M becomes 6, best_i=i2. That's fine because then the max changed, and the smallest i with the new max is i2 (which is later than i1? It could be earlier? Actually if i2 > i1, then i2 is later; but we want the smallest i with max. Since max changed to 6, and the first i where cnt=6 might be i2; if there were earlier rows with cnt=6? They would have been encountered before i2, but since we didn't see them earlier, they didn't have cnt=6, so i2 is the smallest with max. So that works. So we can track best_i as the first i that gives the current maximum. So we can do one pass:
Initialize M=0, best_i=0 (or none). For i=1..n:
if col[i]==1:
L = min(k, n-i+1)
cnt = pref[i+L-1] - pref[i-1]
if cnt > M:
M = cnt
best_i = i
At the end, if M==0: (implies no ones? Actually if there are ones but cnt always 0? impossible). So if M==0, then there are no ones; but we can check: if total_ones == 0, then M=0, best_i remains 0. We'll handle separately.
If M>0: then best_i is the smallest i such that cnt_i = M (since we only update when strictly greater; ties not updated, so best_i remains the first i that achieved M). However, is it possible that the first occurrence of the max M is not the smallest i with that max? Suppose we have i=1: col[1]=1, cnt=3. i=2: col[2]=1, cnt=5. So M becomes 5 at i=2, best_i=2. That's correct; the smallest i with max=5 is 2. Suppose i=1: cnt=5, M=5, best_i=1. Then i=2: cnt=5, but not > so not update; best_i remains 1, which is smallest. Good.
Thus we can get best_i in one pass. But we also need to compute deletions = pref[best_i-1]. But we also need to ensure that if there are multiple i with same M but best_i is the first one, that's indeed the minimal deletions. Yes.
Edge case: If there are ones, but M=0? Not possible, as argued. But we can still compute: if pref[n]==0, then M=0, and we set replacements=0. But in our loop, since col[i]==1 never occurs, M stays 0, best_i remains 0. So we can check: if M==0: replacements=0, score=0.
But wait: Could M be 0 even if there are ones? Suppose k=0? But k≥1. So no. So fine.
Thus we can implement like that.
Now, we need to compute prefix sums for each column. Since n,m ≤100, we could just compute cnt by summing over L elements each time (O(n*k) per column) but prefix sums are simpler.
Implementation steps:
- Read n,m,k.
- Initialize matrix a as list of lists rows.
- total_score = 0; total_replace = 0.
- For each column j in range(m):
pref = [0]*(n+1)
for i in range(1,n+1):
pref[i] = pref[i-1] + a[i-1][j] # careful with 0-index.
M = 0
best_i = -1 # 1-indexed? We'll use 0-index later.
for i in range(1,n+1):
if a[i-1][j] == 1:
L = min(k, n - i + 1)
cnt = pref[i+L-1] - pref[i-1]
if cnt > M:
M = cnt
best_i = i
if M == 0: # no ones
# score add 0, replace add 0
continue
else:
total_score += M
# deletions = number of ones in rows 1..best_i-1
del_cnt = pref[best_i-1]
total_replace += del_cnt
Print total_score, total_replace.
Test with examples.
Example1:
Column1: computed earlier, M=1, best_i=2? Let's check: i=1 col1=0 skip; i=2 col2=1? Actually column1 rows: row1=0 (skip), row2=1 => cnt=1 (since L=2, rows2-3 sum=1) => M=1, best_i=2. i=3 col3=0 skip; i=4 col4=1 => cnt=1 but not >1, so best_i remains 2. del = pref[1] = row1=0 =>0. So column1: score1, replace0. Column2: i=1 col=1 => L=2, cnt=1 => M=1, best_i=1. i=2 col=0 skip; i=3 col=1 => L=2, cnt=2 >1 => M=2, best_i=3. i=4 col=1 => cnt=1 <2, skip. del = pref[2] = rows1+2 =1 =>1. Column3: i=1 col=0 skip; i=2 col=1 => cnt=1 => M=1 best_i=2; i=3 col=0; i=4 col=1 => cnt=1 not >, so best_i=2, del=pref[1]=0. Total=4,1. Good.
Example2: Column1: i=1 col=1 => cnt=1 => M=1 best_i=1; del=pref[0]=0. Column2: i=1 col=0; i=2 col=1 => cnt=1 => M=1 best_i=2; del=pref[1]=0. Total=2,0.
Now test a column with multiple ones and tie but first not best? Suppose column: n=5,k=3, ones: rows 2 and 4 both give cnt=2? Let's construct: a=[0,1,0,1,1]. Compute:
i=2: L = min(3,4)=3 => rows2-4: row2=1,3=0,4=1 => cnt=2.
i=4: L = min(3,2)=2 => rows4-5: row4=1,5=1 => cnt=2.
So M=2. According to our algorithm, we go i from 1..5. i=2 first gives cnt=2, M becomes 2, best_i=2. i=4 gives cnt=2 not >, so best_i remains 2. So we pick i=2, del = pref[1] = row1=0 =>0. But could we achieve M=2 with i=2? Yes, delete rows1? Actually need to delete rows1 (0) and keep others. That yields topmost row2, window rows2-4 count=2. That's valid. And deletions=0. That's minimal. If we had chosen i=4, deletions would be pref[3] = rows1-3 = row1=0, row2=1, row3=0 =1. So indeed minimal is 0. So algorithm yields correct minimal.
Now test scenario where smallest i with max M has a 1 above that is not deleted? Actually we delete it, but it's part of deletions. So fine.
Now consider column where there is a 1 at row1, but cnt1 is not max; max is at row3; row3 is the smallest with max. Our algorithm will catch that. For instance: a=[1,0,1,1], n=4,k=2.
i=1: L=2 => rows1-2: 1,0 => cnt=1.
i=3: L=2 => rows3-4: 1,1 => cnt=2.
M=2, best_i=3, del=pref[2]= rows1+2 =1+0=1. So we need to delete row1. That yields configuration: row1=0, row2=0? Actually row2 is 0 originally, row3=1, row4=1. Topmost row3, window rows3-4 count=2. Score 2. Could we achieve score 2 without any deletions? If we keep row1, topmost is row1, window rows1-2 count=1, not 2. So we need to delete row1. So replacements=1. Good.
Now think about scenario where k is large: k=n. Then for each i, L = n-i+1 (since min(n, n-i+1) = n-i+1). So cnt_i = number of ones from i to n. That's the suffix sum from i. Then M = max over i (with a_i=1) of suffix sum from i. The best is essentially the maximum suffix sum that starts at a 1. The smallest i achieving that is the leftmost index where the suffix sum is maximum. That will correspond to the first 1 in the longest suffix of ones? Actually the maximum suffix sum is the total number of ones in the column if there is at least one 1. But if there are zeros, the suffix sum might be less. For instance, column: 1,0,1. Suffix sums: i=1: total ones =2; i=2: a2=0 but we only consider i with a_i=1, so i=3: suffix sum=1. So M=2, best_i=1, deletions=0. So we don't need to delete anything; the topmost row1 already sees all ones below. That matches intuition: with k=n, the score for a column is just the total number of ones in that column (since you count all ones from topmost to bottom). Actually if k=n, then after finding topmost i, you look at next n-i+1 elements which is the whole column from i to end. So you count all ones from i onward. So if the topmost is the first 1, you count all ones. If you shift topmost down, you count fewer ones. So best is to keep the first 1, i.e., not delete any ones above it, which is deletions 0. That matches our algorithm: best_i is the smallest i with max cnt; since cnt_i is suffix sum, the maximum suffix sum is often at the earliest 1? Not always: consider column: 0,1,1. Suffix sums: i=2: 2, i=3:1. So max=2, best_i=2 (since i=1 not a 1). But that means the first 1 is at row2, and we cannot have a topmost earlier because there is no 1 at row1. So we keep it as is, deletions=pref[1]= row1=0. So no deletions. So in both cases, deletions may be 0. Good.
Now consider column with k small. The algorithm stands.
Thus we can produce answer.
But before finalizing, we should consider any potential oversight: The problem statement says "Ivan can replace any number of ones in this matrix with zeroes." That means he can choose to replace some ones, but he might also choose not to replace any. The replacements are the actions of changing a 1 to 0. So if he leaves a 1 as is, it's not a replacement. So counting replacements as number of original ones that are set to 0. In our per column approach, we assume that for a candidate i, we delete (i.e., replace) exactly the ones in rows 1..i-1 that are originally 1. We do not replace any others. That yields a certain number of replacements. But is it always allowed to not replace ones in rows i..n? Yes, we can keep them as 1. So the number of replacements is exactly pref[i-1]. So that's fine.
Now, is there any scenario where achieving M requires also replacing some ones in the window? No, as argued. But could there be a scenario where we need to replace some ones below the window to ensure that the topmost is i? No, because ones below do not affect topmost. So they can be kept.
Thus minimal replacements for column is indeed the minimum over i achieving M of pref[i-1].
But we should double-check with a tricky case where there are multiple rows i with cnt_i=M, but the smallest i might have a higher pref[i-1] than some larger i? That would contradict monotonicity of pref. Since pref is non-decreasing, if i1 < i2, then pref[i1-1] ≤ pref[i2-1]? Let's examine: pref is cumulative sum of ones. So pref[i] is number of ones in first i rows. So if i1 < i2, then i1-1 < i2-1, so pref[i1-1] ≤ pref[i2-1] because adding more rows (with nonnegative ones) cannot decrease. Yes. So smaller i gives smaller or equal pref[i-1]. So minimal deletions is achieved at the smallest i.
Thus our simplified method works.
Now, what about the possibility of not having any i with a_i=1? Then M=0, and we don't add any replacements. That's correct: column all zeros yields score 0 with 0 replacements. But wait: What if column has some ones, but we might choose to delete all ones to get score 0? That would be worse than positive score, since we want maximize score. So we never do that if there is any positive achievable score. So it's fine.
Now consider edge case: n=1, m=1, k=1, a[1][1]=1. Then column: only row. i=1 with a1=1, L = min(1,1)=1, cnt=1. M=1, best_i=1, del=pref[0]=0. So output 1 0. Let's simulate: original matrix has one 1. Score: find topmost 1 at row1, count next min(1,1)=1 element -> that element is 1, so score=1. No replacements needed. So correct.
If a[1][1]=0, then M=0, output 0 0. Good.
Now, what about k > n? But constraints say k ≤ n, so fine.
Thus the solution seems straightforward.
But hold on: The problem says: "In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that a_{i,j} = 1). If there are no 1's in the column, this column is skipped;
3. Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score."
The phrase "the next min(k, n - i + 1) elements" could be interpreted as: starting from the element he found, he looks at the next elements (i.e., the ones after it, not including it). However, it says "starting from the element he found", which suggests including that element. Usually, if you say "look at the next 3 elements starting from this one", you include this one. For example, "starting from page 5, read the next 3 pages" includes page 5,6,7. So including the starting element is typical. The examples also confirm this: In example 2, column 1: [1,0,0], k=1. If we include the topmost, we count that 1 => score 1. If we excluded, we would count 0 (since after it there are no elements? Actually after row1, next min(k, n-i+1)= min(1,3)=1 element after? If we exclude the starting, we would look at row2 only, which is 0 => score 0. But total score is 2, so each column contributed 1. So including is correct. In example1, column2: original column [1,0,1,1] with k=2. If we include topmost row1, window rows1-2: [1,0] => count=1. But the max score for column2 is 2, achieved by making topmost row3, window rows3-4: [1,1] => count=2. So again includes starting. So interpretation is correct.
Now, think about if there is any other nuance: The score calculation is done after replacements, and the replacements are counted globally. We assumed columns independent. That's fine.
Thus we can code.
But before final code, I'd like to test with some random small cases to ensure no mistake. We'll do mental tests.
Case: n=2,m=1,k=1, matrix: [1,1].
Column: [1,1]. Rows with ones: i=1 and i=2.
i=1: L=min(1,2)=1 => cnt = row1=1.
i=2: L=min(1,1)=1 => cnt = row2=1.
M=1. best_i = 1 (since i=1 gives cnt=1 >0, set M=1, best_i=1). del = pref[0]=0. So output score=1, replace=0. But can we get score 2? Let's see: Original topmost row1, window length 1 includes only row1, count=1. If we delete row1, topmost becomes row2, window length 1 includes row2 count=1, total=1. So max=1. So correct.
Case: n=2,m=1,k=2, matrix: [1,1].
i=1: L=min(2,2)=2 => cnt=2.
i=2: L=min(2,1)=1 => cnt=1.
M=2, best_i=1, del=0. So score=2, replace=0. Original topmost row1, window rows1-2 count=2. So good.
Case: n=3,m=1,k=2, matrix: [1,0,1].
i=1: L=2 => rows1-2: 1,0 => cnt=1.
i=3: L=1 => row3=1 => cnt=1.
M=1. best_i=1, del=0. Score=1. Could we get 2? If we delete row1, topmost row3, window length 1 count=1. If we delete row2 (0) no change. So max=1. But what if we keep both and maybe something else? No.
Now case: n=3,m=1,k=2, matrix: [0,1,1].
i=2: L=2 => rows2-3: 1,1 => cnt=2.
M=2, best_i=2, del=pref[1]= row1=0 =>0. So score=2, replace=0. Original topmost row2, window rows2-3 count=2. So good.
Now case: n=3,m=1,k=2, matrix: [1,1,0].
i=1: L=2 => rows1-2:1,1 => cnt=2.
i=2: L=2 => rows2-3:1,0 => cnt=1.
M=2, best_i=1, del=0. So score=2.
Now case: n=3,m=1,k=2, matrix: [1,0,0].
i=1: L=2 => rows1-2:1,0 => cnt=1. M=1, best_i=1, del=0. score=1.
Now case: n=3,m=1,k=2, matrix: [0,0,1].
i=3: L=1 => cnt=1. M=1, best_i=3, del=pref[2]= rows1-2: 0 =>0. score=1.
All seems consistent.
Now case where there are ties and the smallest i has more deletions? That can't happen due to monotonicity. But consider this: pref[i-1] counts ones before i. If i is smaller, there are fewer rows before, but those rows could be all ones, while a larger i might have many zeros before it, but the pref[i-1] is number of ones, not rows. So it's possible that pref[i1-1] > pref[i2-1] if i1 < i2? Let's test: i1=2, i2=5. pref[1] is ones in row1. pref[4] is ones in rows1-4. Since pref is cumulative, pref[4] = pref[1] + ones in rows2-4. So pref[4] >= pref[1]. So it cannot be less. So indeed pref[i-1] is non-decreasing with i. So smallest i yields smallest deletions.
Thus our algorithm is correct.
Now, is there any scenario where we might achieve the same maximum score with fewer deletions than pref[best_i-1] by not deleting all ones above best_i, but also deleting some ones within the window or below to somehow still get the same score? That seems impossible because if we don't delete a one above best_i, then that row becomes the topmost, so the window changes. Could it change to also give the same M? Possibly if that other row also has cnt = M. But then we are effectively using that other row as topmost, which would have deletions = pref[that row-1] which is <= pref[best_i-1] because that row is smaller? Actually if we don't delete a one above best_i, say there is a one at row j < best_i. Then the topmost becomes j. If cnt_j = M, then we could achieve M with deletions = pref[j-1]. Since j < best_i, pref[j-1] <= pref[best_i-1]? Actually j<best_i => j-1 < best_i-1 => pref[j-1] <= pref[best_i-1]? Not necessarily, because pref is cumulative; pref[j-1] counts ones up to row j-1, while pref[best_i-1] counts ones up to row best_i-1, which includes rows up to j-1 plus rows j to best_i-1. Since there is at least the one at row j (if j is a row with a 1, then rows j to best_i-1 includes that one, so pref[best_i-1] >= pref[j-1] + (at least 1 if j is within that range? Actually careful: j is less than best_i, so row j is in the range 1..best_i-1. So pref[best_i-1] includes the one at row j (if we didn't delete it). So pref[best_i-1] >= pref[j-1] + 1 (if col[j]=1). So pref[best_i-1] > pref[j-1] generally. So using j would require fewer deletions (since we need to delete only rows < j, not including j). But if we keep the one at j, we would have to delete all rows < j. That's deletions = pref[j-1]. So indeed, if j is a candidate with cnt_j = M, then the minimal deletions might be even lower than our best_i (since best_i was the smallest i with cnt=M? Actually best_i is the smallest i with cnt=M encountered in our iteration. If there is a j < best_i with cnt_j = M, then our algorithm would have set best_i to j because when iterating, we would have seen j first and set M to that value, and later when we see other rows with same M we wouldn't update, so best_i would be j. So best_i is indeed the smallest i with cnt=M. So if there is any j < i with cnt_j = M, then best_i would be j, not i. So our algorithm's best_i is the smallest. So there is no j < best_i with cnt_j = M. Therefore, the only rows with cnt=M are at or after best_i. So if we don't delete a one above best_i, that row (say r) is < best_i and has a 1, but since r < best_i, we know that cnt_r is not equal to M (otherwise best_i would be r). So the topmost would become r, and cnt_r < M, so the score would be less than M. So we cannot achieve M by keeping a 1 above best_i. So indeed we must delete all ones above best_i.
Thus the algorithm yields the correct minimal deletions.
Now we have a robust solution.
But before finalizing, let's test a more complex case with multiple columns and ties to ensure that summing works.
Also consider scenario where column has ones but maximum cnt is 0? Not possible.
Now, what about the possibility that we might want to replace some ones even if they are not necessary to achieve max score, to reduce replacements? No, we want to minimize replacements, so we avoid unnecessary replacements.
Thus final answer is sum of M and sum of del.
Now, we should also consider reading input: each row has m numbers. We can store matrix as list of lists.
Now, implement solve() accordingly.
Now, think about time complexity: O(mn) per column? Actually for each column we compute prefix O(n), then loop over rows O(n). So total O(mn) = 10000, trivial.
Now, confirm with some random test by mental simulation of a tricky case: n=5,m=1,k=3, column: [1,1,0,1,1]. Compute prefix: [0,1,2,2,3,4]? Actually row1:1, row2:1, row3:0, row4:1, row5:1 => pref: pref1=1, pref2=2, pref3=2, pref4=3, pref5=4.
Now compute cnt for each row with 1:
i=1: L = min(3,5)=3 => rows1-3: sum = pref3 - pref0 = 2 => cnt=2.
i=2: L = min(3,4)=3 => rows2-4: pref4 - pref1 = 3-1=2? Actually pref4=3, pref1=1 =>2. Wait rows2-4: row2=1, row3=0, row4=1 => sum=2. cnt=2.
i=4: L = min(3,2)=2 => rows4-5: pref5 - pref3 = 4-2=2 => cnt=2.
i=5: L = min(3,1)=1 => row5:1 => cnt=1.
So M=2. Smallest i with cnt=2 is i=1. best_i=1, del=pref0=0. So score=2, replace=0. Let's check if we can get score >2? Maybe if we delete row1, topmost becomes row2, window rows2-4 count=2; delete row1 and row2? topmost row4 window rows4-5 count=2. So max 2. And with no deletions, topmost row1 gives window rows1-3 count=2. So score 2, no replacements. Good.
Now consider column: [1,0,1,0,1] with k=2.
pref: row1=1, row2=1, row3=2, row4=2, row5=3.
i=1: L=2 => rows1-2:1,0 => cnt=1.
i=3: L=2 => rows3-4:1,0 => cnt=1.
i=5: L=1 => cnt=1.
M=1. best_i=1, del=0. So score 1, replace 0. Could we get score 2? Possibly if we delete row1, topmost becomes row3, window rows3-4 count=1; delete row1 and row3? topmost row5 count=1. So no.
Now consider column: [0,1,1,0,1] with k=3.
i=2: L=min(3,4)=3 => rows2-4:1,1,0 => cnt=2.
i=3: L=min(3,3)=3 => rows3-5:1,0,1 => cnt=2? rows3=1,4=0,5=1 =>2.
i=5: L=1 => cnt=1.
M=2. best_i=2 (since i=2 first with cnt=2), del=pref[1]= row1=0 =>0. So score 2, replace 0.
Now, what about column where it's beneficial to delete some ones above to get a higher cnt, and the smallest i achieving that max is not the first 1. Our algorithm captures that.
Now, I'm convinced.
But before final answer, let's read the problem statement again to ensure there is no twist: "Ivan can replace any number of ones in this matrix with zeroes." That means he can only change 1->0. So our modeling is correct.
"After that, his score in the game will be calculated as follows: ..." So he does replacements first, then score is computed on final matrix.
"He doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes." This is secondary objective after maximizing score. So we need to output the maximum possible score and the minimum number of replacements required to achieve that score.
Thus we have solved.
Potential pitfalls: off-by-one errors with indices, especially when computing L and window end. Ensure that i+L-1 <= n. Since L = min(k, n-i+1), then i+L-1 <= i + (n-i+1) -1 = n, so safe.
When computing cnt = pref[i+L-1] - pref[i-1], ensure that i+L-1 is within [1,n]. It is.
Now, code.
We'll implement in Python:
def solve():
import sys
input_data = sys.stdin.read().strip().split()
if not input_data:
return
it = iter(input_data)
n = int(next(it))
m = int(next(it))
k = int(next(it))
a = []
for _ in range(n):
row = [int(next(it)) for _ in range(m)]
a.append(row)
total_score = 0
total_replace = 0
for j in range(m):
# build prefix sums for column j
pref = [0] * (n+1)
for i in range(1, n+1):
pref[i] = pref[i-1] + a[i-1][j]
M = 0
best_i = 0 # 1-indexed, 0 indicates none
for i in range(1, n+1):
if a[i-1][j] == 1:
L = min(k, n - i + 1)
cnt = pref[i+L-1] - pref[i-1]
if cnt > M:
M = cnt
best_i = i
if M == 0:
# no ones in this column, score 0, replacements 0
continue
total_score += M
# deletions needed: ones before best_i
deletions = pref[best_i-1]
total_replace += deletions
print(total_score, total_replace)
Edge Cases:
- If column has ones but M remains 0? not possible, but we keep check.
Now test with given examples.
Example1 input:
4 3 2
0 1 0
1 0 1
0 1 0
1 1 1
Our code yields? Let's simulate quickly:
n=4,m=3,k=2.
Process col0 (j=0):
pref: compute.
a[0][0]=0, a[1][0]=1, a[2][0]=0, a[3][0]=1.
pref: [0,0,1,1,2]? Actually careful: i=1: a[0][0]=0 => pref1=0; i=2: a[1][0]=1 => pref2=1; i=3: a[2][0]=0 => pref3=1; i=4: a[3][0]=1 => pref4=2.
Loop i=1: a[0][0]==0 skip.
i=2: a[1][0]==1 => L=min(2, 4-2+1=3)=2, cnt = pref[2+2-1=3] - pref[1] = pref3 - pref1 = 1-0=1? Actually pref3=1, pref1=0 =>1. M=1, best_i=2.
i=3: a[2][0]==0 skip.
i=4: a[3][0]==1 => L=min(2,1)=1, cnt=pref[4]-pref[3]=2-1=1, not >1, skip.
M=1, best_i=2, deletions=pref[1]=pref1=0. So col0: +1 score, +0 replace.
col1 (j=1):
a[0][1]=1, a[1][1]=0, a[2][1]=1, a[3][1]=1.
pref: i1:1=>pref1=1; i2:0=>pref2=1; i3:1=>pref3=2; i4:1=>pref4=3.
i=1: a0==1, L=2, cnt=pref2 - pref0? Actually pref[1+2-1=2] - pref[0] = pref2 - pref0 = 1-0=1. M=1, best_i=1.
i=2: a1==0 skip.
i=3: a2==1, L=2, cnt=pref[4] - pref[2] = 3-1=2 >1 => M=2, best_i=3.
i=4: a3==1, L=1, cnt=pref[4]-pref[3]=3-2=1 <2 skip.
M=2, best_i=3, deletions=pref[2]=pref2=1. So col1: +2, +1.
col2 (j=2):
a[0][2]=0, a[1][2]=1, a[2][2]=0, a[3][2]=1.
pref: i1:0=>pref1=0; i2:1=>pref2=1; i3:0=>pref3=1; i4:1=>pref4=2.
i=1: skip.
i=2: a1==1, L=2, cnt=pref[3]-pref[1]=1-0=1 => M=1, best_i=2.
i=3: skip.
i=4: a3==1, L=1, cnt=pref[4]-pref[3]=2-1=1 not >1 skip.
M=1, best_i=2, deletions=pref[1]=pref1=0. So col2: +1, +0.
Total: 4,1. Good.
Example2:
3 2 1
1 0
0 1
0 0
col0: pref: [0,1,1,1]. i=1: a0=1, L=1, cnt=pref1-pref0=1 => M=1, best_i=1, deletions=pref0=0. col1: pref: [0,0,1,1]? Actually a[0][1]=0, a[1][1]=1, a[2][1]=0 => pref1=0, pref2=1, pref3=1. i=1 skip, i=2 a1=1, L=1, cnt=pref2-pref1=1-0=1 => M=1, best_i=2, deletions=pref1=0. Total 2,0.
Thus matches.
Now consider a test with all zeros: n=2,m=2,k=1, matrix zeros. Then each column M=0, no additions. total_score=0, total_replace=0. Output "0 0". That's fine.
Now consider a test with ones but k=0? Not possible since k≥1.
Now, confirm that we correctly handle cases where best_i remains 0 (no ones). We skip addition. That's correct.
Now, what about the scenario where there is a column with ones, but M is achieved by a row i and we compute deletions = pref[i-1]. But what if pref[i-1] includes ones that are in rows that are also part of the window? No, they are above i, so not in window. So fine.
Now, one more nuance: The replacements counted are the number of ones replaced with zeroes. In our configuration, we are deleting (i.e., replacing) all ones in rows 1..i-1. But what about ones that are originally 0? They are not replaced. So total replacements for that column = number of ones in rows 1..i-1. That's correct.
Now, ensure we don't accidentally count replacements for rows i..n that we might keep. We don't.
Thus solution is done.
But wait: Could there be a scenario where we achieve the maximum score but with fewer deletions than pref[best_i-1] by using a different configuration that still yields topmost i but does not delete all ones above i? As argued, if any one above i remains, it becomes topmost, so not i. But could we instead delete some ones above but not all, and also delete the ones that are at those rows that are above i but also maybe we delete some ones in the window to compensate? No, the topmost is determined by the smallest row with a 1. So if we leave any 1 above i, that row becomes topmost, regardless of what we do to the window. So to have topmost i, we must have all rows < i be zero. So must delete all ones there. So no.
Thus minimal deletions for achieving M is exactly the minimal pref[i-1] among i achieving M.
Now, we have a solution.
Double-check the reading: Input: first line contains n,m,k. Then n lines, each with m numbers. So we read accordingly.
We'll implement in Python with standard input.
Now, before final answer, consider possible pitfalls:
-
Use of 0-index vs 1-index: careful when computing pref and indices.
-
When L is computed as min(k, n-i+1). In 0-index, if we use i as 1-indexed, it's fine. If we use i as 0-index, then L = min(k, n - i). But it's simpler to keep i as 1-index in loops.
Our code uses i from 1 to n inclusive, and accesses a[i-1][j] for element. So i is 1-indexed. L = min(k, n - i + 1). Then window end index = i + L - 1, which is <= n. So cnt = pref[end] - pref[i-1].
Works.
Now, think about whether we need to consider the possibility of achieving the maximum score by making the column all zeros and then the column is skipped (score 0). But if there is any positive score possible, that would be less, so not maximum. So not an issue.
Thus final answer is correct.
Now, let's produce final solution.
But I want to add reasoning for the minimal deletions: we argued it's the deletions for the smallest i achieving max cnt. But is it always possible that keeping all ones from i onward (including those in window) yields exactly cnt_i? Yes, because cnt_i is computed from original ones. However, what if there is a 1 in the window that we would like to keep? We keep it. That's fine. So configuration exists.
Now, check extreme: if we have to delete all ones above i, but some of those ones are also part of the window for i? No, because window starts at i, so above i is not included.
Thus fine.
Now, final code.
Potential optimization: We could compute cnt_i using sliding window to avoid prefix, but not needed.
Edge case: n=100,m=100, k=100, all ones. Then each column: for i=1, cnt = min(k, n) = 100? Actually L = min(k, n-i+1). For i=1, L=100, cnt=100. That's max. best_i=1, deletions=0. So total score = m*100 = 10000, replacements=0. That seems right: with all ones, you get 100 per column, no need to replace.
If k is small, say k=1, all ones: For each column, i=1 gives cnt=1, best_i=1, deletions=0. So total score=m*1=100, replacements=0. That also seems right: original topmost row1, count 1 in next 1 element -> 1. Could we get higher by deleting row1? Then topmost row2, count 1, still 1. So max=1.
Now, test scenario where we might want to replace some ones to increase score, e.g., the given example.
All good.
Now, I'm confident.
Thus answer.
But before final, I want to consider if there is any scenario where the optimal solution involves not maximizing each column independently? The only potential coupling is the replacement count: perhaps achieving max score in one column might require many replacements, while maybe sacrificing a small amount of score in that column could reduce replacements significantly, and since we want to minimize replacements only after maximizing score, the primary objective is score. So we must achieve maximum total score. If we can achieve that maximum with fewer replacements by not maximizing some column but compensating in another column, that would require that the total maximum is not simply the sum of per-column maxima. But per-column maxima are independent, and total maximum cannot exceed sum of per-column maxima because each column's contribution is independent and max possible for each column is that value. If we reduce one column's contribution below its maximum, the total becomes less than sum of maxima, which cannot be maximum if we can increase that column's contribution to its max without affecting others (since columns are independent). Because we can always adjust each column independently to its max without affecting others (by performing the necessary replacements only in that column). So the sum of maxima is achievable. Therefore any total less than that is not maximum. So we must achieve each column's maximum. So columns are independent.
Thus the problem is separable.
Now, finalize.
But wait: There might be a nuance: The replacements count is total across all columns, and perhaps achieving max in each column might require a lot of replacements in some columns, but maybe we could achieve the same total score by exceeding max in some columns? Not possible because max is per column limit. So indeed we must achieve each column's max.
Thus solution is correct.
Now, I'll produce the final answer with explanation. We can solve the problem by considering each column independently, because the total score is the sum of contributions from each column, and replacements in one column do not affect others.
For a fixed column, we start with a binary array (a_1, a_2, \dots, a_n). We may change any 1 to 0. After the changes, the column's contribution is computed as:
- Let (i) be the smallest index with (a_i = 1) (if none, contribution = 0).
- Let (L = \min(k, n - i + 1)).
- The contribution equals the number of ones in the segment (a_i, a_{i+1}, \dots, a_{i+L-1}).
Our goal is to choose a set of ones to keep (i.e., we may only delete ones) so that this contribution is maximised; among all ways achieving that maximum, we want to minimise the number of deletions.
Observation: any final configuration has a topmost 1 at some row (i) for which the original (a_i = 1) (because we cannot create new ones). For such an (i) to become the topmost, all ones in rows (1 \dots i-1) must be deleted. The contribution then is the number of kept ones in the window (i \dots i+L-1). To maximise it for this fixed (i), we keep every original 1 inside that window (deleting inside the window would only lower the contribution). Ones outside the window can be kept or deleted; keeping them costs nothing and does not affect the score. Therefore, for a candidate (i) (with (a_i = 1)), the best achievable contribution is
[
C_i = \text{number of original ones in rows } i \text{ through } i+L-1,
]
and it requires exactly (D_i = \text{number of original ones in rows } 1 \text{ through } i-1) deletions.
Thus the maximum possible contribution of the column is (M = \max_{i: a_i = 1} C_i). Let (I) be the set of indices (i) with (a_i = 1) and (C_i = M). The minimal deletions needed to achieve (M) is (\min_{i \in I} D_i). Because the prefix sum of ones is non‑decreasing, the smallest index in (I) gives the smallest (D_i). Hence it suffices to find the smallest row (i) (from the top) that attains the maximum (C_i).
Implementation details:
- For each column we build a prefix sum array
pref where pref[t] is the number of ones in rows (1..t).
- We scan rows (i = 1 \dots n). If (a_i = 1), compute
[
L = \min(k, n-i+1), \quad
C_i = \text{pref}[i+L-1] - \text{pref}[i-1].
]
- Keep track of the current maximum (M) and the first row
best_i that achieved this maximum (update only when (C_i > M)).
- If (M = 0) (which happens only when the column contains no ones), the contribution is 0 and no deletions are needed.
- Otherwise, the deletions needed are
pref[best_i-1].
- Sum the contributions and deletions over all columns.
Complexities: time (O(n m)), space (O(n)) per column (or (O(1)) with a running prefix). With (n,m \le 100) this is trivial.
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
m = int(next(it))
k = int(next(it))
a = [[int(next(it)) for _ in range(m)] for _ in range(n)]
total_score = 0
total_replace = 0
for col in range(m):
# prefix sums of ones in this column
pref = [0] * (n + 1)
for i in range(1, n + 1):
pref[i] = pref[i-1] + a[i-1][col]
best_i = 0 # 1‑based, 0 means none yet
max_cnt = 0
for i in range(1, n + 1):
if a[i-1][col] == 0:
continue
L = min(k, n - i + 1)
cnt = pref[i + L - 1] - pref[i - 1]
if cnt > max_cnt:
max_cnt = cnt
best_i = i
if max_cnt == 0:
# column has no ones → score 0, replacements 0
continue
total_score += max_cnt
total_replace += pref[best_i - 1]
print(total_score, total_replace)
if __name__ == "__main__":
solve()