We need to parse the problem carefully.
We have an n x m table (rows, columns). We need to choose a non-zero number of the first panels in each row. That is, for each row i, we select a number ci, where 1 <= ci <= m (since we choose the first ci panels of that row, presumably the leftmost ci columns). Then we sum the values of all selected cells (each cell's number). The total sum is the sum over rows i, sum_{j=1}^{ci} a[i][j].
But the chosen panels must form a "comb": the sequence c1, c2, ..., cn must satisfy alternating inequality: c1 > c2 < c3 > c4 < ... i.e., it alternates between > and < starting with > for c1 vs c2. So we need a pattern: For i=1 to n-1, if i is odd (1-indexed), then ci > ci+1; if i is even, then ci < ci+1. That is the condition.
Goal: maximize total sum given this condition, with each ci in [1, m].
We are to output the maximum total sum.
Constraints: n, m up to 1500. So O(nm) or O(nm log m) might be okay, but O(n*m^2) too high.
We need to compute efficiently.
We have to pick ci for each row, from 1..m, with alternating constraints. It resembles a kind of dynamic programming with state being the last ci and parity of the inequality? But n,m up to 1500, naive DP O(n*m^2) is too big (1500^3 ~ 3.375e9). But maybe we can optimize using prefix sums and something like "max over previous ci satisfying constraint"? Because the constraints are just inequalities relative to neighbor: for odd i (i from 1), ci > ci+1; for even i, ci < ci+1. So for each i, the allowed transition from previous ci-1 to ci depends on parity.
Specifically, if we are at row i, and we have the previous ci-1, then:
- If i is odd (so i>=2? Actually for i=1, there is no previous; we choose c1 arbitrarily). For i>=2: if i is even (since constraints between i-1 and i: for i-1 odd -> ci-1 > ci; for i-1 even -> ci-1 < ci). So better to define transition from row i-1 to i.
Let i be current row (2..n). The constraint between c_{i-1} and c_i is: if i-1 is odd (i even?), Let's compute: For i=2, constraint: c1 > c2 because 1 is odd -> >. For i=3, constraint: c2 < c3 because 2 is even -> <. So more precisely: if (i-1) is odd -> c_{i-1} > c_i; if (i-1) is even -> c_{i-1} < c_i.
Thus we can define for each i (2..n) the allowed pairs: for odd i-1 (i even) we need c_{i-1} > c_i; for even i-1 (i odd) we need c_{i-1} < c_i.
So it's like alternating direction constraints.
We want to maximize sum of row prefix sums. Let's define for each row i and each possible ci, the contribution: S[i][ci] = sum_{j=1}^{ci} a[i][j] (the prefix sum along row i). Then total = sum_i S[i][ci].
We need to maximize total subject to alternating constraints.
So this is a classic DP: dp[i][c] = maximum total for rows 1..i ending with ci = c. Then dp[1][c] = S[1][c] for all c=1..m. For i>1, we have dp[i][c] = S[i][c] + max_{c' satisfying constraint with c} dp[i-1][c']. The constraint: if i-1 is odd (i even) => need c' > c; if i-1 is even (i odd) => need c' < c. So we need to compute for each c the maximum over dp[i-1][c'] with c' > c (or c' < c) efficiently.
We can precompute for each i, the prefix maximums and suffix maximums of dp[i-1] to answer these queries in O(1). Because for c' > c, the maximum over dp[i-1][c'] for c' > c is essentially a suffix max array: suffix_max[c+1] = max over indices > c. Similarly for c' < c, it's a prefix max up to c-1: prefix_max[c-1]. So DP transition per c is O(1) after computing these auxiliary arrays, leading to O(n*m) time.
But careful: The constraint direction depends on parity of i-1. So we need to compute for each row i:
If (i-1) is odd => i is even => need previous > current, so for each c (1..m), dp[i][c] = S[i][c] + max_{c' > c} dp[i-1][c']. But what if there is no c' > c? Since c' can be up to m, and c can be m, then c'>m? No. For c=m, there is no c' > m, so dp[i][m] is invalid? Actually constraint says c_{i-1} > c_i. If c_i = m, then we need c_{i-1} > m, which is impossible because maximum is m. So c_i cannot be m when constraint is >. Similarly, if constraint is c_{i-1} < c_i, then c_i cannot be 1 because then c_{i-1} < 1 impossible (since minimum c_{i-1} is 1). So for those rows, the possible c_i are limited to ranges that allow at least one feasible c_{i-1}. But we need to handle that dp[i][c] should be -inf if no feasible previous state. However, note that the sequence c_i must be non-zero for each row, i.e., at least 1. But constraints may make certain values impossible. For example, if n=2 and constraint c1 > c2, then c2 can be at most m-1? Actually c1 can be up to m, and need c1 > c2, so c2 can be at most m-1. But also c1 must be > c2, so c2 can be from 1 to m-1, but also c1 must be at least 2 if c2=1. So when we compute dp[2][c], we need to consider max over c' > c. For c=m, no c' > m, so dp[2][m] would be -inf. So indeed, we need to treat invalid transitions as not allowed.
Thus algorithm:
Initialize dp[1][c] = prefixSumRow1[c] for c=1..m.
For i from 2 to n:
Compute prefix_max of dp[i-1]: pref_max[k] = max(dp[i-1][1..k]) for k=1..m.
Compute suffix_max of dp[i-1]: suff_max[k] = max(dp[i-1][k..m]) for k=1..m.
For each c from 1..m:
if (i-1) is odd (i even): constraint: previous > current.
best_prev = (c < m) ? suff_max[c+1] : -inf.
else (i-1 even, i odd): constraint: previous < current.
best_prev = (c > 1) ? pref_max[c-1] : -inf.
if best_prev is -inf (meaning no valid previous), then dp[i][c] = -inf (or some sentinel indicating impossible).
else dp[i][c] = S[i][c] + best_prev.
After processing all rows, the answer is max_{c} dp[n][c] (since all rows must be selected). But is there any requirement on the last row's relation? The comb condition only specifies for all adjacent pairs, so no extra condition after last. So we take max over all c.
Complexities: O(nm) for DP, plus O(nm) for computing prefix sums for each row.
But note: S[i][c] can be computed as we go: we can precompute prefix sums per row. Since m up to 1500, n up to 1500, total cells 2.25 million, easy.
Memory: we can keep dp for previous row only, because we only need dp[i-1] to compute dp[i]. So we need two arrays of size m (maybe two rows). So O(m) memory.
But careful: we need S[i][c] for each row i and c; we can compute row by row: when processing row i, we have the prefix sums for row i; we compute dp for current row using dpPrev and prefix sums. So we don't need to store all rows of dp.
Edge Cases: Need to handle negative numbers and large sums: values up to 10000, m up to 1500, so row sum up to 15e6, n up to 1500 so total up to 2.25e10, which fits in 64-bit. So use long long.
Now confirm with examples.
Input:
2 2
-1 2
1 3
Rows:
Row1: prefix: c1: -1; c2: -1+2=1
Row2: prefix: c1: 1; c2: 1+3=4
n=2, constraint: c1 > c2 (since i=2, i-1=1 odd). So c2 can only be 1 (since c1 > c2, c1 max 2, so c2 must be 1). For c2=1, dp[2][1] = S2[1] + max_{c1>1} dp1? Wait, c1 > c2 => c1 >1, so c1 can be 2. dp1[2]=1. So dp2[1]=1+1=2. For c2=2, need c1>2 impossible, so -inf. So answer max dp2 =2. Output 2 matches.
What if n=3? Example not given, but test mental: say all positives, we want maximize sum. The constraints alternate: c1 > c2, c2 < c3. So c1 > c2 < c3. So c3 can be large, c1 can be large, c2 small. So we might choose large c1 and c3, and a small c2. So DP should compute.
Check DP logic for i=3: i-1=2 (even), so constraint previous < current: c2 < c3. For each c3, best_prev = max_{c2 < c3} dp2[c2]. So dp3[c3] = S3[c3] + prefix_max[c3-1] (if c3>1). That's correct.
Thus solution seems straightforward.
But we must be careful: The DP initialization for row1: dpPrev[c] = S1[c] for all c=1..m. Then for i=2,...,n compute dpCurr.
Now compute prefix_max and suffix_max of dpPrev. For each c compute best_prev.
Edge Cases: For i=2 with constraint >, for c=m, no c' > m, so best_prev = -inf; dpCurr[m] = -inf. That's fine. For i=3 with constraint <, for c=1, best_prev = -inf because no c' < 1.
Now we need to treat -inf properly. Since numbers can be negative, we need a sentinel lower than any possible sum. The minimum possible sum: each row prefix sum could be as low as -10000*m per row? Actually numbers can be negative, absolute value up to 10000. So worst sum per row: -10000 * m = -15e6. n up to 1500, total minimum ~ -2.25e10. So we can use a constant like -1e18 (or LLONG_MIN/2) to represent -inf. But careful: adding S[i][c] to -inf could overflow? We'll check best_prev first: if best_prev is -inf, set dpCurr[c] = -inf; else add. So no addition to -inf.
We'll use long long and set INF = 1e18, and NEG_INF = -1e18 (but ensure it's less than any possible real sum). Since real sums are > -1e13? Actually -2.25e10 is about -2.25e10, which is > -1e18. So -1e18 is safe.
But we could also use a boolean flag.
Now complexity: O(n*m). With n,m=1500, 2.25M operations, fine.
But wait: The problem statement says "choose some non-zero number of the first panels in each row". That means we must select at least one panel per row. So ci >= 1. Our DP ensures that because we only consider ci from 1 to m.
Now question: Is there any requirement that the selected panels form a comb exactly as defined? The sequence c_i must satisfy the alternating inequality. Yes.
But is there any condition like the comb must be "strict"? Yes, it's strict inequalities: > and <.
Thus DP correct.
But we must consider if the comb can start with either > or < ? The problem says: "c1 > c2 < c3 > c4 < ...". That implies the pattern is fixed: c1 > c2, then c2 < c3, then c3 > c4, etc. So for n, if n is odd, the last inequality? e.g., for n=3: we have c1 > c2 < c3. There is no condition after c3. So pattern is determined by positions.
Thus DP as described.
Now, is there any possibility that the comb condition might be interpreted differently? The phrase "the sequence c1, c2, ..., cn made from the quantities of panels chosen in each table line satisfies the following property: c1 > c2 < c3 > c4 < ..., i.e. the inequation mark interchanges between the neighboring elements." So indeed for each adjacent pair, the inequality alternates, starting with >. So it's clear.
Thus solution is DP with prefix/suffix max.
But wait, there might be nuance: "choose some non-zero number of the first panels in each row" might be interpreted as we select some number of panels (maybe not necessarily the first ones? "first panels" suggests the leftmost panels. So indeed we choose a prefix of each row. So ci is length of prefix.
Thus solution stands.
Now, let's double-check that we haven't missed any detail: The sum is over all chosen panels. So sum over rows of prefix sum up to ci.
Thus DP is correct.
Now let's implement.
Pseudo-code:
read n, m
Initialize dpPrev (size m+1 maybe 1-indexed) as long long.
For row = 1 to n:
read row values a[1..m]
compute prefix sum array S[1..m] (cumulative)
if row == 1:
for c=1..m: dpPrev[c] = S[c]
else:
// compute prefix max and suffix max for dpPrev
pref_max[0] = -INF
for k=1..m:
pref_max[k] = max(pref_max[k-1], dpPrev[k])
suff_max[m+1] = -INF
for k=m down to 1:
suff_max[k] = max(suff_max[k+1], dpPrev[k])
// compute dpCurr
dpCurr = array size m+1
if ( (row-1) % 2 == 1 ) // row-1 odd? Actually row-1 parity: odd => constraint > (i.e., need previous > current). Let's define:
// Since row index start 1.
// For transition from row-1 to row, if (row-1) is odd -> >.
// So: if ( (row-1) % 2 == 1 ) => > constraint.
else => < constraint.
For c=1..m:
if constraint is >:
best = (c < m) ? suff_max[c+1] : -INF
else: // <
best = (c > 1) ? pref_max[c-1] : -INF
if best == -INF:
dpCurr[c] = -INF
else:
dpCurr[c] = S[c] + best
// set dpPrev = dpCurr for next iteration
dpPrev = dpCurr
After loop, answer = max(dpPrev[c] for c=1..m)
But careful: For row 2, row-1=1 (odd) so constraint >, correct.
Now, we need to be careful about indexing: pref_max[k] = maximum dpPrev[1..k]. For c>1, best for < constraint: max over c' < c, i.e., dpPrev[1..c-1] = pref_max[c-1]. For c=1, no such, best = -INF.
For > constraint: max over c' > c, i.e., dpPrev[c+1..m] = suff_max[c+1] (if c+1 <= m). For c=m, no such.
Now, are there any additional constraints like each ci must be at least 1? Yes.
Now, test on simple cases.
Case: n=2, m=1. But m>=2? Actually constraints: 2 ≤ n, m ≤ 1500, so m≥2, so no issue of m=1. Because m≥2, so there is always at least two columns, which is necessary for alternating constraints? Actually if m=2, constraints can be satisfied. With m=2, for n=2, c1>c2: possible (c1=2, c2=1). Good. If m=2 and n=3, we need c1>c2<c3: c1>c2 and c2<c3. With c2 can be 1 or 2? If c2=1, then c1>1 -> c1=2; c3>c2? Actually constraint c2<c3, so c3>1 -> c3=2. So (2,1,2) works. So m=2 is okay.
Now, we should verify DP works for negative numbers. If all numbers negative, we must still select at least one panel each row. The DP will find the maximum (least negative) combination that satisfies comb. It should work.
Now, time complexity O(n*m) is fine.
Now, let's think if there is any trick: The DP transition uses only the maximum dpPrev among allowed indices, but is it correct to take just the maximum? Since we are adding S[i][c] independent of previous c' except for constraint, we can indeed choose the best previous value for each current c. So DP is optimal.
Now, is there any scenario where we need to consider more than just the immediate previous? No, it's Markovian.
Thus solution is simple.
Now, we need to implement efficiently. Since n,m up to 1500, we can store arrays as vectors of long long.
Implementation details: Input size: up to 2.25M numbers, we need fast IO (use scanf/printf or cin with sync off). We'll use C++ with scanf/printf.
Now, potential overflow: Using long long, max sum about 2.25e10, fits within 2^63-1 (~9e18), so safe.
Now, but we should consider the sentinel: define const long long INF = 1e18; or use LLONG_MIN? But if we use LLONG_MIN and then we add a positive S[i][c], it could overflow into negative? But we won't add if best is LLONG_MIN because we first check if best is LLONG_MIN; but careful: we might compute best = suff_max[c+1] where suff_max[c+1] might be LLONG_MIN (if no valid previous, we set suff_max accordingly). Then we check if best == LLONG_MIN and set dpCurr[c] = LLONG_MIN. However, we could also have a legitimate dpPrev value that equals LLONG_MIN? That's impossible because dpPrev values are sums of numbers, the smallest possible is around -2.25e10, which is > LLONG_MIN. So we can safely use LLONG_MIN as -inf sentinel, but we must be careful not to use it in addition. But in the code we do: if best == -INF then dpCurr[c] = -INF else dpCurr[c] = S[c] + best. That's fine.
But we need to compute prefix and suffix max arrays that might have -INF values. For pref_max, we start with -INF for index 0, and take max. If dpPrev[k] might be -INF? Actually dpPrev for some c could be -INF if that state is unreachable from initial row. But after first row, all states are reachable? For row1, all c are reachable because we directly set dp[1][c] = S1[c] (no constraints). So all are finite. For subsequent rows, some states may be unreachable if no valid previous. Those will be set to -INF. So dpPrev may contain -INF entries. Then pref_max and suff_max need to handle them: max with -INF yields the other finite value. That's fine.
But we must ensure that when we compute suff_max[c+1] for c=m (c+1 = m+1), we need to set suff_max[m+1] = -INF. Similarly pref_max[0] = -INF.
Thus DP works.
Now, potential improvement: Could we avoid storing dpPrev as full array? Yes we already did. We'll have vector dpPrev(m+1), dpCurr(m+1). Index from 1.
Now, compute prefix sums for each row as we read. We'll read the row into a temporary array of ints (or compute cumulative on the fly). For each row i:
- Read m numbers. Compute cumulative sum in an array pref of size m+1 (pref[0]=0; then for j=1..m: pref[j] = pref[j-1] + a[j]; Then S[c] = pref[c].
But note: S[c] = sum of first c numbers, which is pref[c].
Now, DP:
If i==1: for c=1..m: dpPrev[c] = pref[c];
Else: compute pref_max and suff_max of dpPrev.
Now, one subtlety: In the DP for i>1, we use dpPrev which corresponds to previous row. But what if some dpPrev entries are -INF? That's fine.
Now, after finishing all rows, answer = max(dpPrev[c]).
Now, test on a custom case to ensure correctness.
Let's do n=3, m=3 with all ones.
Row1: S[1]=1, S[2]=2, S[3]=3.
Row2: S[1]=1, S[2]=2, S[3]=3.
Row3: S[1]=1, S[2]=2, S[3]=3.
Constraints: c1>c2<c3.
Goal: maximize sum.
We want c1 and c3 large, c2 small.
Possible combos: c1 can be 3, c2 must be < c1, so c2 max 2. For each c2, c3 must be > c2, so c3 min c2+1 up to 3.
Compute combos:
- c2=1: c1>1 => c1=2 or 3; c3>1 => c3=2 or 3.
combos: (2,1,2): sum=2+1+2=5; (2,1,3):2+1+3=6; (3,1,2):3+1+2=6; (3,1,3):3+1+3=7.
- c2=2: c1>2 => c1=3 only; c3>2 => c3=3 only; (3,2,3): sum=3+2+3=8.
- c2=3: impossible because need c1>3? no, c1>c2=3 impossible, so no combos with c2=3.
So max is 8 (3,2,3). Check DP:
Row1: dp1: [1,2,3]
Row2 (i=2, constraint >): compute suff_max of dp1:
suff_max[1]=max(1,2,3)=3; suff_max[2]=max(2,3)=3; suff_max[3]=3; suff_max[4]= -inf.
For each c2:
c=1: best_prev = suff_max[2] = 3; dp2[1] = S2[1] + 3 = 1+3=4.
c=2: best_prev = suff_max[3] = 3; dp2[2] = 2+3=5.
c=3: best_prev = suff_max[4] = -inf => dp2[3] = -inf.
So dp2: [4,5,-inf].
Row3 (i=3, constraint < because row-1=2 even): compute pref_max of dp2:
pref_max[0]= -inf; pref_max[1]=4; pref_max[2]=max(4,5)=5; pref_max[3]=5.
For each c3:
c=1: best_prev = pref_max[0] = -inf => dp3[1] = -inf.
c=2: best_prev = pref_max[1] = 4; dp3[2] = 2+4=6.
c=3: best_prev = pref_max[2] = 5; dp3[3] = 3+5=8.
max=8. OK.
Thus DP works.
Now, consider scenario with negative numbers, ensuring unreachable states are handled.
Now, I think the solution is correct.
But wait, the problem might be trickier: The "comb" property is defined as c1 > c2 < c3 > c4 < ... So the inequality signs alternate. That's what we used.
Now, is there any requirement that the number of panels chosen in each row is "some non-zero number of the first panels", meaning we have to choose a contiguous block starting from the first column. That's prefix.
Now, is there any possibility that we might also choose zero? It says non-zero, so at least 1.
Now, about the DP: we could also compute it in a different orientation: treat rows as steps. The O(n*m) approach is fine.
But wait, there is a nuance: The DP transition uses maximum over dpPrev among allowed c'. However, we must be careful: For the > constraint, we need c' > c. That's correct. For < constraint, need c' < c.
But is it possible that the DP might yield an invalid sequence due to the alternating nature beyond just immediate adjacency? Actually the DP builds the sequence step by step, ensuring each adjacent pair satisfies the required inequality. That's sufficient because the condition only involves adjacent pairs. So DP is correct.
Now, we should ensure that for i=2, we compute dp2 correctly, but note that we might have dp2[1] as -inf? Actually for i=2, if c2=1, we need c1>1. There is at least one c1 > 1 if m>=2: c1 can be 2..m. So if m>=2, dp2[1] is valid. If m=2, dp2[2] invalid; dp2[1] valid. If m>2, dp2[1] valid.
But what about m=2 and n=2? We saw dp2[2] invalid, dp2[1] valid.
Now, consider n=2 and m=2 and all numbers negative: e.g., -10 -10; -10 -10. Then dp1: [-10,-20]; For row2 constraint >: c2=1, best_prev = suff_max[2]= max dp1[2] = -20; dp2[1] = -10 + (-20) = -30. Answer -30. But is there any alternative? c1 must be > c2; only c2=1 with c1=2 works. So indeed -30 is correct.
Now, n=2 and m=3, all negatives: could there be a better combination with c2=2? Need c1>2 => c1=3. Then sum = dp1[3] + S2[2] = -30 + (-20) = -50, which is worse than maybe c2=1 with c1=2: -20 + -10 = -30. So okay.
Thus DP yields correct.
Now, final answer: print max over dp[n].
But we need to consider if all states for final row are unreachable? Is it possible that there is no valid comb? Given m>=2 and n>=2, there should always be at least one valid comb. Let's see: For n=2, we need c1 > c2. Since c1 can be at least 2 (if m>=2) and c2 at least 1, we can set c1=2, c2=1. So valid. For n=3, we need c1 > c2 < c3. Choose c2=1, then c1 can be 2..m, c3 can be 2..m. So valid if m>=2. For n=4: need c1 > c2 < c3 > c4. Choose c2=1, c4=1? Let's see: need c3 > c4, so if c4=1, c3>1. Choose c3=2; c1>c2 => c1>1 => c1=2; So valid if m>=2. So always at least one comb exists because we can always set some pattern: set all odd positions (1-indexed) to 2, all even positions to 1 (if m>=2). Since m>=2, it's feasible. So answer is finite.
Thus no need to worry about no solution.
Now, implement.
Now, we must think about memory: using vectors of size m+2 (to have sentinel indices). That's fine.
Now, potential performance: computing prefix sums and dp for each row is O(m). Good.
Now, let's implement in C++.
Pseudo-code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
int main() {
int n, m;
scanf("%d %d", &n, &m);
vector dpPrev(m+2, -INF); // 1-indexed, plus extra sentinel
// Process rows
for (int i = 1; i <= n; ++i) {
vector row(m);
for (int j = 0; j < m; ++j) {
scanf("%d", &row[j]);
}
// compute prefix sums S[1..m]
vector S(m+1, 0);
for (int j = 1; j <= m; ++j) {
S[j] = S[j-1] + row[j-1];
}
if (i == 1) {
for (int c = 1; c <= m; ++c) {
dpPrev[c] = S[c];
}
} else {
// compute prefix max and suffix max from dpPrev
vector pref_max(m+2, -INF);
for (int k = 1; k <= m; ++k) {
pref_max[k] = max(pref_max[k-1], dpPrev[k]);
}
vector suff_max(m+2, -INF);
for (int k = m; k >= 1; --k) {
suff_max[k] = max(suff_max[k+1], dpPrev[k]);
}
vector dpCurr(m+2, -INF);
// determine constraint based on (i-1) parity
if ((i-1) % 2 == 1) { // i-1 odd -> need previous > current
for (int c = 1; c <= m; ++c) {
ll best = (c < m) ? suff_max[c+1] : -INF;
if (best > -INF/2) { // not -INF; but better to compare with -INF directly
dpCurr[c] = S[c] + best;
} // else remains -INF
}
} else { // i-1 even -> need previous < current
for (int c = 1; c <= m; ++c) {
ll best = (c > 1) ? pref_max[c-1] : -INF;
if (best > -INF/2) {
dpCurr[c] = S[c] + best;
}
}
}
// swap
dpPrev = dpCurr;
}
}
ll ans = -INF;
for (int c = 1; c <= m; ++c) {
if (dpPrev[c] > ans) ans = dpPrev[c];
}
printf("%lld\n", ans);
return 0;
}
But we should consider that dpPrev is of size m+2, but we only use up to m. And we may need to use suff_max[m+1] which we set to -INF, okay.
Now, one nuance: The condition (i-1) % 2 == 1? For i=2, (2-1)=1 -> 1%2==1 => true -> > constraint, correct. For i=3, (3-1)=2 -> 2%2==0 => false -> < constraint, correct. So mapping correct.
But note: In our loop we are using i starting from 1. For i=1 we set dpPrev. For i=2 we compute with constraint >. Good.
Now, we should also consider that the dpPrev array might contain -INF for unreachable states, but when we compute pref_max and suff_max, we include them. Since max with -INF and a finite yields finite. But if all states for a certain c are -INF, then pref_max[c] will be -INF; that's fine.
Now, check that we don't inadvertently allow invalid transitions because we might pick a best_prev that is -INF and then add S[c]? But we check best > -INF/2, but it's safer to check best != -INF if we defined INF constant. But we can just compare with -INF. Since -INF is defined as -1e18, and real best will be at least -2.25e10, so best > -INF/2? Actually -INF/2 = -5e17, which is less than -2.25e10, so best will be > -INF/2. So that check works. However, if we use -INF = -1e18, best might be -9e17? Not possible. So we can check if best > -INF (i.e., best != -INF). But since we set -INF to -1e18, and actual values are > -1e17? Actually -2.25e10 > -1e18, so condition best > -INF? -INF is -1e18, best > -1e18 always if best is finite. But if best is -INF, it's exactly -1e18? We set it to -1e18. But we need to be careful: If we set -INF = -1e18, and some best is exactly -1e18? Could that happen? If all dpPrev entries are -INF? But that would mean no state reachable. But if initial row is all -INF? Not possible. But later rows may have all -INF? That would mean no comb, but we argued there always is a comb. However, it's possible that for some row, dpPrev may have all -INF except some values, but those values are finite. So best may be finite. But what about pref_max[0] = -INF; if we try to access pref_max[0], we are using that only when c>1? Actually we use pref_max[c-1] only when c>1, but if c=1 we use -INF directly. So fine.
Better: We can set -INF to LLONG_MIN/2? Actually LLONG_MIN is around -9e18, so using -1e18 is safe.
Alternatively, we can use a boolean reachable array? But it's okay.
We'll just check if best != -INF.
Implementation details:
const ll NEG_INF = -1e18;
and then:
if (best > NEG_INF) dpCurr[c] = S[c] + best;
Because if best is NEG_INF, best > NEG_INF? Actually NEG_INF = -1e18. best = -1e18 (if we set that), then best > NEG_INF is false because equality not >. So we need >. But if best could be equal to NEG_INF, condition false. So it's fine.
But we must be careful when we set best = suff_max[c+1] if c<m; suff_max may be -INF if all dpPrev indices >c are -INF. So best would be -INF. Then condition fails.
So works.
Now, some optimization: we could compute prefix and suffix max without extra vectors by reusing memory? But m is small, not needed.
Now, test on given example, works.
Now, test on a random case to ensure no bug.
We'll also test with n=2,m=2 all zeros: dp1: [0,0]; dp2: c2=1 best = suff_max[2]=max dp1[2]=0 => dp2[1]=0; c2=2 invalid; answer 0. That's correct: can choose (2,1) or (1,1)? Wait c1>c2, so (2,1) gives sum 0+0=0; (1,1) not allowed because c1>c2 false. So 0 is correct.
Now, test with negative numbers where optimal might be to pick a larger ci even though numbers negative? Since you must pick at least one, and constraints may force you to pick a certain pattern. So DP will find the maximum.
Now, potential pitfalls: The DP as described uses O(n*m) time and O(m) memory, but we recompute prefix sums each row. That's fine.
Now, double-check indexing of suffix_max: we defined suff_max[k] = max(dpPrev[k..m]). So suff_max[c+1] = max over indices > c. For c=m, c+1 = m+1, we set suff_max[m+1] = -INF. Good.
Now, we might need to handle the case when m is large and we have many rows; DP must be efficient.
Now, we should consider if there is any alternative interpretation: Might "first panels" mean we can choose any set of panels that are first in each row? That is, we choose a certain number of panels from the beginning of each row. So yes.
Now, maybe the panels are pushed down, and the sum is the numbers on those panels. So yes.
Now, I'm thinking: Could there be a more efficient way? Not needed.
Thus final solution is as above.
But wait: There's a nuance: The DP uses only the maximum dpPrev among allowed indices. But is it always optimal to take the maximum dpPrev? Since the choice of c' only affects the total sum through dpPrev and the constraint, and we add S[i][c] independent of c', yes, for each c we can take the best c'. So DP is correct.
Now, we must ensure that we are not missing any constraint: The comb sequence must satisfy strict inequalities. We used that.
Now, we could implement in a more concise way: Instead of separate cases for each row, we can compute prefix and suffix max and then assign based on parity.
Now, one more test: n=4, m=3 all ones: We want to maximize sum: c1>c2c4.
We can compute manually: The pattern: odd indices (1,3) should be high, even indices (2,4) low but with constraints: c1>c2, c2<c3, c3>c4.
Given m=3, the maximum comb maybe: c1=3, c2=2? 3>2 true, c2=2 < c3? Need c3>2, so c3=3; c3>c4, need c4<3, so c4 can be 1 or 2. Choose c4=2? But c4 must be less than 3, but also must be >? No further constraints. To maximize sum, we want c4 as high as possible (2) because all ones. So try (3,2,3,2): sum=3+2+3+2=10. Check constraints: 3>2 ok, 2<3 ok, 3>2 ok. So 10. Is there better? Try (3,1,3,2): sum=3+1+3+2=9; (3,2,3,1):9; (3,1,3,1):8; (2,1,2,1):6; (3,2,3,3) invalid because c3>c4? 3>3 false. So max 10. Let's see DP.
Row1 dp1: [1,2,3].
Row2 (i=2, >): dp2: c=1: best = suff_max[2]=max(2,3)=3 => dp2[1]=1+3=4; c=2: best = suff_max[3]=3 => dp2[2]=2+3=5; c=3: invalid => -inf. So dp2: [4,5,-inf].
Row3 (i=3, <): compute pref_max from dp2: pref[0]= -inf, pref[1]=4, pref[2]=5, pref[3]=5. Then dp3: c=1: best=pref[0]= -inf => -inf; c=2: best=pref[1]=4 => dp3[2]=2+4=6; c=3: best=pref[2]=5 => dp3[3]=3+5=8; So dp3: [ -inf,6,8 ].
Row4 (i=4, > because (4-1)=3 odd): need previous > current. Compute suffix_max from dp3: dp3: index1 -inf, index2 6, index3 8. suff[4] = -inf; suff[3]=8; suff[2]=max(6,8)=8; suff[1]=max(-inf,6,8)=8? Actually compute: suff[3]=8; suff[2]=max(dp3[2], suff[3])=max(6,8)=8; suff[1]=max(dp3[1], suff[2])=max(-inf,8)=8; suff[4]= -inf.
Now dp4: for c=1: best = suff[2] = 8 => dp4[1] = S4[1] (1) + 8 = 9; c=2: best = suff[3] = 8 => dp4[2] = 2+8=10; c=3: best = suff[4] = -inf => -inf. So dp4: [9,10,-inf]; max =10. Matches.
So DP works.
Now, note that dpCurr for row4, c=1 gave 9, which corresponds to (?, ?, ?,1). The best previous state for c4=1 was some c3>1, the best is 8 (c3=3). That gives (?, ?,3,1). Then we need to see what that leads to? The actual sequence from DP: for dp4[1] we used best=8 from dp3[3] (since suff[2] includes index 2 and 3, but max is 8 from index3). So dp4[1] came from dp3[3]=8, which came from dp2[2]? Actually dp3[3] came from best=pref_max[2]=5 (from dp2[2]? Actually dp3[3] = S3[3] + pref_max[2] (max dp2[1..2]) = 3 + max(4,5)=8; that max was dp2[2]=5, so dp3[3] corresponds to dp2[2] (c2=2). And dp2[2] came from best = suff_max[3] = 3 from dp1[3], so c1=3. So the sequence is (3,2,3,1) sum=3+2+3+1=9. For dp4[2]=10, we had best = suff[3] = 8 from dp3[3] (or possibly dp3[2]? but suff[3] is only dp3[3]=8). So same dp3[3]=8, resulting sequence (3,2,3,2) sum=10. Good.
Thus DP can reconstruct if needed but not necessary.
Now, confirm that the alternating pattern is strictly defined starting with >. For n=2, we have c1>c2. For n=3, we have c1>c2<c3. So odd positions are "peaks"? Actually c1 is a peak (greater than next), c2 is a valley (less than next). That's consistent.
Now, think about if we could have chosen different pattern like starting with < if n is even? The problem says exactly c1 > c2 < c3 > c4 < ... so it's fixed.
Thus solution stands.
Now, we should consider if there is any trick with negative numbers and unreachable states causing prefix_max and suffix_max to be -INF for some ranges. For example, if for some row, all dpPrev are -INF for indices > c, then suffix_max[c+1] will be -INF. That's fine.
Now, implement.
But we must be careful about the data type for reading: a[i][j] are integers, but could be up to 10000 in absolute. Use int.
Now, some micro-optimizations: we can avoid storing row values by computing prefix sums on the fly:
- For each row, we can read into an array of ints, then compute cumulative.
Alternatively, we can read each number and accumulate sum in a variable to compute S array directly without storing the row. Since we need prefix sums up to each c, we can do:
vector pref(m+1,0);
int x;
for (int j=1; j<=m; ++j) {
scanf("%d", &x);
pref[j] = pref[j-1] + x;
}
That avoids storing row array. That's good.
Now, we also need to store dpPrev. For row 1, we set dpPrev to pref values.
Now, we need to compute prefix and suffix max arrays of dpPrev. We could compute them in place, but easier to create two new arrays each of size m+2.
But we can compute in O(m) and then compute dpCurr directly while scanning c.
For constraint >, we need for each c: best = max_{k>c} dpPrev[k]. That's like a suffix max. So we can precompute an array suf where suf[c] = max_{k>=c} dpPrev[k]? But we need > c, not >=. So we can compute an array sufGreater where for each c, we want max of dpPrev from c+1 to m. We can compute an array suffixMaxExclusive: start from m down to 1: let rightMax = -INF initially; for c from m down to 1: bestForC = rightMax; then update rightMax = max(rightMax, dpPrev[c]). This way we can compute on the fly without storing full array? Actually we can compute dpCurr in one pass if we traverse c from high to low for > constraint? Let's think.
For > constraint: For each c, we need max of dpPrev for indices > c. If we process c from m down to 1, we can maintain a running maximum of dpPrev values we have seen (which are indices > c). Starting with curMax = -INF. For c=m, best = -INF (since no >m). Set dpCurr[m] accordingly. Then before moving to c=m-1, we update curMax = max(curMax, dpPrev[m]). Then for c=m-1, best = curMax (which is max of indices > m-1, i.e., m). Then update curMax = max(curMax, dpPrev[m-1]) after computing? Wait careful: When we go downwards, we start at c=m, best = -INF. Then we want to move to c=m-1. The set of indices > m-1 is {m}. So best should be dpPrev[m]. So we need to have curMax = dpPrev[m] before processing c=m-1. So after processing c=m, we can update curMax = max(curMax, dpPrev[m])? Actually before moving to c=m-1, we incorporate dpPrev[m] into the set of indices that are > c for the next step. So algorithm: curMax = -INF; for c from m down to 1: best = curMax; dpCurr[c] = (best == -INF) ? -INF : S[c] + best; curMax = max(curMax, dpPrev[c]); This yields best = max of dpPrev indices strictly greater than c. Because when we are at c, curMax contains max of dpPrev indices we have processed (which are greater indices since we go from high to low). And we update curMax after using it for the current c. That's correct.
Similarly, for < constraint: need max of dpPrev indices < c. We can process c from 1 to m, maintain a running max of dpPrev indices seen so far (which are smaller). Starting curMax = -INF; for c=1..m: best = curMax; dpCurr[c] = (best == -INF) ? -INF : S[c] + best; then curMax = max(curMax, dpPrev[c]); Because after processing c, we include dpPrev[c] for future indices larger than c. That's correct: for c=1, curMax initially -INF, best = -INF (since no smaller). For c=2, curMax = max of dpPrev[1] after processing c=1, so best = max of indices <2 i.e., index1. Good.
Thus we can compute dpCurr in O(m) with O(1) extra space, without constructing prefix/suffix arrays. That's more efficient memory-wise, but not necessary. However, it's simple and avoids extra vectors. But careful: We need dpPrev array for reference. That's okay.
We'll implement this efficient one-pass method.
So algorithm per row i>1:
if ( (i-1) % 2 == 1 ) { // > constraint
vector dpCurr(m+1, NEG_INF);
ll best = NEG_INF;
// process c from m down to 1
for (int c = m; c >= 1; --c) {
// best currently holds max of dpPrev indices > c (since we haven't included dpPrev[c] yet)
if (best > NEG_INF) { // but we must also ensure best is not -INF; but if best == NEG_INF, no valid previous
dpCurr[c] = S[c] + best;
} // else stays NEG_INF
// now incorporate dpPrev[c] into best for smaller c
if (dpPrev[c] > NEG_INF) { // only if dpPrev[c] is valid
if (dpPrev[c] > best) best = dpPrev[c];
}
}
dpPrev = dpCurr;
} else { // < constraint
vector dpCurr(m+1, NEG_INF);
ll best = NEG_INF;
for (int c = 1; c <= m; ++c) {
// best holds max of dpPrev indices < c
if (best > NEG_INF) {
dpCurr[c] = S[c] + best;
}
// incorporate dpPrev[c]
if (dpPrev[c] > NEG_INF) {
if (dpPrev[c] > best) best = dpPrev[c];
}
}
dpPrev = dpCurr;
}
But note: In the > constraint loop, we start with best = NEG_INF, and at c=m, best is NEG_INF (since no indices > m). So dpCurr[m] remains NEG_INF. Then we update best = max(best, dpPrev[m]) (if dpPrev[m] valid). So after processing c=m, best becomes dpPrev[m] if valid. Then for c=m-1, best is dpPrev[m] (the max of indices > m-1). Good.
In the < constraint loop: start best = NEG_INF; for c=1, best is NEG_INF => dpCurr[1] = NEG_INF; then incorporate dpPrev[1]; then for c=2, best is dpPrev[1] if valid; etc.
This works.
But careful: In the > constraint, we need to ensure that we only consider dpPrev[c] that are valid (i.e., not NEG_INF) when updating best. So we check if dpPrev[c] > NEG_INF before updating. However, if dpPrev[c] is NEG_INF, it should not affect best (since it's invalid). But if best is currently NEG_INF and we update with an invalid, best should remain NEG_INF. So we must only update if dpPrev[c] > NEG_INF. That's what we'll do.
Similarly for < constraint.
Also, when adding S[c] + best, we need to ensure best is not NEG_INF; we can check best > NEG_INF.
Now, what about the case where best is NEG_INF but dpPrev[c] might be valid? That's fine; we will add later for smaller c.
Now, one more nuance: In the > constraint, for a given c, we need c' > c. But what if there is no valid c' > c? Then best is NEG_INF, and we keep dpCurr[c] as NEG_INF. That's correct.
Now, after computing dpPrev for all rows, answer = max(dpPrev[c]) for c=1..m.
But we need to confirm that this method yields correct results for the earlier examples.
For n=2,m=2, row1: dpPrev = [ -1, 1 ]? Actually example: row1 values -1,2; prefix: [-1, 1]. Row2: i=2 (>). dpPrev: [-1, 1]. Process from c=2 down:
- c=2: best=NEG_INF => dpCurr[2]=NEG_INF. Update best = max(NEG_INF, dpPrev[2]=1) => best=1.
- c=1: best=1 => dpCurr[1] = S2[1]=1 + 1 = 2. Update best = max(1, dpPrev[1] = -1) => best remains 1.
dpCurr: [2, NEG_INF] => max = 2. Good.
For n=3,m=3 all ones:
Row1 dpPrev = [1,2,3].
Row2 (>):
Start best = NEG_INF.
c=3: best NEG_INF => dpCurr[3]=NEG_INF; update best = max(NEG_INF, dpPrev[3]=3) => 3.
c=2: best=3 => dpCurr[2] = S2[2]=2+3=5; update best = max(3, dpPrev[2]=2) => 3.
c=1: best=3 => dpCurr[1]= S2[1]=1+3=4; update best = max(3, dpPrev[1]=1) => 3.
dpCurr: [4,5,NEG_INF] matches earlier.
Row3 (<):
Start best = NEG_INF.
c=1: best NEG_INF => dpCurr[1]=NEG_INF; update best = max(NEG_INF, dpPrev[1]=4) => 4.
c=2: best=4 => dpCurr[2]= S3[2]=2+4=6; update best = max(4, dpPrev[2]=5) => 5.
c=3: best=5 => dpCurr[3]= S3[3]=3+5=8; update best = max(5, dpPrev[3]=NEG_INF?) Actually dpPrev[3] is NEG_INF? Wait dpPrev after row2 is [4,5,NEG_INF]; so dpPrev[3] is NEG_INF. So update best remains 5.
dpCurr: [NEG_INF,6,8] matches.
Row4 (>):
Start best = NEG_INF.
c=3: best NEG_INF => dpCurr[3]=NEG_INF; update best = max(NEG_INF, dpPrev[3]=8) => 8.
c=2: best=8 => dpCurr[2]= S4[2]=2+8=10; update best = max(8, dpPrev[2]=6) => 8.
c=1: best=8 => dpCurr[1]=1+8=9; update best = max(8, dpPrev[1]=NEG_INF) => 8.
dpCurr: [9,10,NEG_INF] matches.
Thus this method works and is efficient.
Now, we must be careful that in the loops we are using dpPrev values. For row after first, dpPrev might have some -INF entries, but we treat them as invalid.
Now, we should also ensure that we use long long for all sums.
Now, confirm that the answer for all rows is the max of dpPrev after processing all rows. Since dpPrev will have some valid entries if there is a valid comb. We argued there always is at least one.
Now, we should test on a case where m=2, n=2, all negative numbers: row1: -5 -4; row2: -3 -2. Prefix row1: [-5, -9]; row2: [-3, -5]. DP:
Row1: dpPrev = [-5, -9].
Row2 (>):
c=2: best NEG_INF -> dpCurr[2]=NEG_INF; update best = max(NEG_INF, dpPrev[2] = -9) => -9.
c=1: best=-9 -> dpCurr[1] = S2[1] = -3 + (-9) = -12. So dpCurr[1] = -12. answer = -12.
Is that the best? Possible combos: c1 > c2. Options: c1=2, c2=1 gives sum = row1[1]+row1[2] = -9? Wait row1 sum of first 2 = -9; row2 sum of first 1 = -3; total -12. c1=2, c2=1 gives -9 + -3 = -12. c1=2, c2=1 is the only? Could c1=2, c2=1 is forced. What about c1=1, c2? No because need >. So only one valid. So answer -12 correct.
Now, test with n=3,m=2 all negatives: row1: -5 -4 => pref: [-5,-9]; row2: -3 -2 => pref: [-3,-5]; row3: -1 -10 => pref: [-1,-11]. Constraints: c1>c2<c3. m=2. Let's enumerate valid combos: c1>c2 => c1=2, c2=1 (since c2 must be <2, so 1). Then c2<c3 => 1 < c3 => c3 can be 2. So only combo: (2,1,2): sums: -9 + -3? Wait row2 sum for c2=1 is -3, row3 sum for c3=2 is -11, total = -9 + -3 + -11 = -23. Could there be c1=2,c2=1,c3=2 only? c3 could be 2 only. So answer -23. DP:
Row1: dpPrev = [-5,-9].
Row2 (>):
c=2: best NEG_INF -> dpCurr[2] invalid; update best = max(NEG_INF, dpPrev[2] = -9) => -9.
c=1: best=-9 -> dpCurr[1] = -3 + (-9) = -12.
So dpPrev becomes [-12, NEG_INF] (dpPrev[1]= -12, dpPrev[2] invalid). Row3 (<):
Processing: best = NEG_INF.
c=1: best NEG_INF -> dpCurr[1]=NEG_INF; update best = max(NEG_INF, dpPrev[1] = -12) => -12.
c=2: best = -12 -> dpCurr[2] = S3[2] = -11 + (-12) = -23; update best = max(-12, dpPrev[2] = NEG_INF) => -12.
So dpCurr: [NEG_INF, -23]; max = -23. Correct.
Thus works.
Now, final answer printing.
Now, we must consider potential overflow when adding best to S[c] if best is large positive and S[c] positive. But as argued, max sum ~ 2.25e10, well within 9e18, safe.
Now, implement with loops.
Edge Cases: n=2,m=1500, and all numbers -10000. Then row prefixes become: for row1, S1[1] = -10000, S1[2] = -20000, ..., S1[1500] = -15e6. Row2 same. The best combo: c1 > c2. To maximize sum (which is negative, we want least negative), we want c1 as small as possible? Actually we need c1>c2, so we want both as small as possible? Since numbers are negative, adding more panels adds more negative. So we want the smallest possible ci that satisfy constraints. The smallest c1>c2 with ci>=1: minimal c1 is 2, c2=1. So sum = S1[2] + S2[1] = -20000 + -10000 = -30000. Could we have c1=2, c2=1 only? Also c1=3, c2=1 gives -30000 + extra -10000? Actually S1[3] = -30000, S2[1] = -10000 => -40000, worse. So DP will choose c1=2, c2=1? But wait dp2[1] will consider best = max over c1>1. That includes c1=2..m. dp1 values are decreasing (more negative) as c increases. So max dp1 among c>1 is at c=2 (since it's the smallest index >1). So best = dp1[2] = -20000. Then dp2[1] = S2[1] + best = -10000 + -20000 = -30000. So dp2[1] = -30000. dp2[2] invalid. So max = -30000. So correct.
Now, check for n=4, m=2 all positives: row1: 1,1 => S: [1,2]; row2: [1,1] => [1,2]; row3: [1,1] => [1,2]; row4: [1,1] => [1,2]. Constraints: c1>c2c4. m=2, so possible combos? Let's enumerate: need c1>c2 => c1=2, c2=1 (since c2<2). Then c2<c3 => 1 < c3 => c3=2. Then c3>c4 => 2 > c4 => c4=1. So only (2,1,2,1): sum = 2+1+2+1 = 6. Could there be (2,1,2,1) only. So answer 6. DP should produce 6.
Row1: dpPrev = [1,2]
Row2 (>):
c=2 invalid; update best from dpPrev[2]=2 -> best=2; c=1 -> dpCurr[1]=S2[1]+2=1+2=3? Wait S2[1]=1, so dpCurr[1]=3. But then total at row2 should be sum of rows1 and 2: c1=2 gives 2, c2=1 gives 1 => total 3. Yes dpCurr[1]=3. So dpPrev becomes [3, NEG_INF].
Row3 (<):
Process: best start NEG_INF
c=1: best NEG_INF -> dpCurr[1]=NEG_INF; update best = max(NEG_INF, dpPrev[1]=3) => 3.
c=2: best=3 -> dpCurr[2]= S3[2]=2 + 3 =5; update best remains 3.
dpPrev becomes [NEG_INF,5].
Row4 (>):
c=2: best NEG_INF -> dpCurr[2]=NEG_INF; update best = max(NEG_INF, dpPrev[2]=5) => 5.
c=1: best=5 -> dpCurr[1]= S4[1]=1 +5 =6. So dpPrev = [6, NEG_INF]. max=6. Good.
Now, everything consistent.
Thus solution is straightforward.
Now, we should think about potential pitfalls: The DP loops using long long and sentinel. We need to initialize dpPrev with -INF after row1? For row1 we set dpPrev for all c. But later we may need dpPrev for indices that are invalid, but we set them to -INF in the dpCurr creation. In the efficient loops, we need dpPrev array to have -INF for invalid states, which we maintain because we set dpCurr to -INF for invalid and then assign dpPrev = dpCurr. So dpPrev always contains -INF for unreachable states.
Now, for updating best in loops, we check if dpPrev[c] > NEG_INF before updating best. This ensures we only consider reachable states. Good.
Now, about the sentinel: we defined NEG_INF = -1e18. Since dpPrev values can be as low as ~ -2.25e10, they are > NEG_INF. So condition dpPrev[c] > NEG_INF is true for reachable states. For unreachable, dpPrev[c] = NEG_INF, so condition false and we don't update best.
Now, one more nuance: In the > constraint loop, when we incorporate dpPrev[c] after processing c, we are adding dpPrev[c] to best for smaller indices. But what if dpPrev[c] is -INF? Then best remains unchanged.
Now, is there any scenario where we need to consider the possibility of multiple valid sequences leading to same c? Our DP picks max, so fine.
Now, we should check memory usage: We'll have dpPrev vector of size m+1, and we'll allocate dpCurr similarly each iteration. Since m <= 1500, that's tiny. We could even reuse a second vector, but fine.
Now, what about the input reading? We'll use scanf. We'll compute S array on the fly.
Now, final step: output answer as long long. Use printf("%lld\n", ans).
Now, we must ensure we handle case where answer might be NEG_INF if no comb (but we argued always comb exists). But to be safe, we can assert ans > NEG_INF.
Now, double-check the pattern for even n: For n=4, we have c1>c2c4. Starting with >, alternating. So our DP correctly handles that because for i=2, >; i=3, <; i=4, >. Good.
Now, what about n=5? Pattern: c1>c2c4<c5? Wait alternating: c1 > c2, then c2 < c3, then c3 > c4, then c4 < c5. So for i=5, the last inequality is c4 < c5. So for transition from row4 to row5, since row4 index even? Row indices: 1,2,3,4,5. Constraint between 4 and 5: row4 is even, so constraint is c4 < c5. So in DP for i=5, we use < constraint. Good.
Now, we need to ensure that we use correct parity condition: For transition from i-1 to i, if (i-1) is odd -> >; else -> <. That's what we did: if ((i-1) % 2 == 1) then > else <.
Now, test for n=5,m=2 all ones: Expected best? Let's try to compute manually. m=2, constraints: c1>c2c4<c5. With m=2, possible values only 1 or 2.
We need c1>c2 => c1=2, c2=1.
c2<c3 => 1 < c3 => c3=2.
c3>c4 => 2 > c4 => c4=1.
c4<c5 => 1 < c5 => c5=2.
Thus sequence (2,1,2,1,2) sum = 2+1+2+1+2 = 8. Could there be alternative? If we try c1=2,c2=1,c3=2,c4=1,c5=2 is forced. So answer 8. DP would compute.
Now, compute DP quickly: row1 dp: [1,2]
Row2 (>): dp2: [3, -inf]
Row3 (<): dp3: [ -inf, 5] (since dp2[1]=3 -> best for c=2 is 3, S3[2]=2 =>5)
Row4 (>): dp4: [6, -inf] (since dp3[2]=5 -> best for c=1 is 5, S4[1]=1 =>6)
Row5 (<): dp5: [ -inf, 8] (since dp4[1]=6 -> best for c=2 is 6, S5[2]=2 =>8) => answer 8. Good.
Thus works.
Now, confirm with n=6,m=2 all ones: pattern: >, <, >, <, > (since indices: i=2: >, i=3: <, i=4: >, i=5: <, i=6: >). Actually check: For i=6, (i-1)=5 odd, so >. So sequence: c1>c2c4c6. With m=2, forced: c1=2, c2=1, c3=2, c4=1, c5=2, c6=1? Let's see: c5>c6, so if c5=2, c6 must be <2 => c6=1. So final (2,1,2,1,2,1) sum = 2+1+2+1+2+1=9. DP would compute: after row5 we got dp5[2]=8? Actually row5 < gave dp5[?]. Let's step: After row4: dp4[1]=6 (from earlier). Row5 (<): dp5: c1 invalid, c2 = S5[2] + best (dp4[1]=6) = 2+6=8. So dp5[2]=8. Row6 (>): dp6: c=2 invalid, c=1: best from dp5[2]=8? Process >: start best NEG_INF; c=2: best NEG_INF -> invalid; update best = max(NEG_INF, dp5[2]=8) => 8; c=1: best=8 -> dp6[1]=S6[1]+8=1+8=9. So answer 9. Good.
So pattern holds.
Thus solution is correct.
Now, I'd like to think about if there is any way to optimize further but not needed.
Now, implementation details in code:
We'll read n,m.
Initialize dpPrev as vector(m+1, NEG_INF). We'll set dpPrev after first row.
For i from 1 to n:
// read row and compute prefix sums S (size m+1, index from 1).
vector S(m+1, 0);
for j=1; j<=m; j++) {
int x; scanf("%d", &x);
S[j] = S[j-1] + x;
}
if (i == 1) {
for j=1; j<=m; j++) dpPrev[j] = S[j];
} else {
vector dpCurr(m+1, NEG_INF);
if ( (i-1) % 2 == 1 ) { // > constraint
ll best = NEG_INF;
for (int c = m; c >= 1; --c) {
if (best > NEG_INF) {
dpCurr[c] = S[c] + best;
}
if (dpPrev[c] > NEG_INF) {
if (dpPrev[c] > best) best = dpPrev[c];
}
}
} else { // < constraint
ll best = NEG_INF;
for (int c = 1; c <= m; ++c) {
if (best > NEG_INF) {
dpCurr[c] = S[c] + best;
}
if (dpPrev[c] > NEG_INF) {
if (dpPrev[c] > best) best = dpPrev[c];
}
}
}
dpPrev = dpCurr;
}
After loop, ans = *max_element(dpPrev.begin()+1, dpPrev.end()) (since dpPrev[0] is unused but set to NEG_INF). Print ans.
Now, one more check: In the > constraint loop, we process c from m down to 1, and we use best (which holds max of dpPrev indices > c). At c=m, best is NEG_INF because we haven't added any dpPrev yet. Then we update best with dpPrev[m] after processing c=m. So for c=m-1, best is dpPrev[m] (if valid). That's correct. But what if dpPrev[m] is invalid? Then best remains NEG_INF after updating? Actually we only update best if dpPrev[m] > NEG_INF; if invalid, best remains NEG_INF. Then for c=m-1, best is still NEG_INF, so dpCurr[m-1] will be invalid unless there is some valid dpPrev index > m-1 besides m. But if dpPrev[m] invalid, maybe dpPrev[m-1]? But indices > m-1 are only m. So dpCurr[m-1] should be invalid. That's correct. Then after processing c=m-1, we update best with dpPrev[m-1] if valid, and then for c=m-2, best would be max of dpPrev[m] and dpPrev[m-1] (both maybe invalid, so best remains NEG_INF). That's correct. So algorithm works.
Similarly for < constraint.
Now, confirm that dpPrev values are always > NEG_INF for reachable states. Since we only set dpCurr[c] to S[c] + best if best > NEG_INF. That ensures dpCurr[c] is > NEG_INF only if it's reachable. So dpPrev will only have NEG_INF for unreachable.
Now, final step: output.
Now, we should consider the possibility of n up to 1500 and m up to 1500. That's 2.25 million numbers to read, which is okay with scanf. DP loops also about 2.25 million operations per row? Actually each row's DP loop is O(m), so total O(n*m) = 2.25 million, which is trivial. So overall runtime is fine.
Thus final answer.
Now, before finalizing, let's double-check the problem statement: "choose some non-zero number of the first panels in each row of the table and put stones on all those panels to push them down." So indeed we are choosing a prefix.
"the chosen panels should form a comb. It was explained that the chosen panels form a comb when the sequence c1, c2, ..., cn made from the quantities of panels chosen in each table line satisfies the following property: c1 > c2 < c3 > c4 < ...". So that's clear.
Now, there is a nuance: "non-zero number of the first panels" might be interpreted as we can choose any number of panels (including possibly zero? but says non-zero, so at least 1). So we have ci >= 1.
Now, the numbers on the panels can be negative, so we might want to choose as few as possible to avoid negative contributions, but constraints may force us to take more. So DP handles that.
Now, I think the solution is correct.
Now, think about any alternative scenario: Could there be a more optimal solution where we don't take prefixes? No, it's defined.
Now, also note: The table is n x m, and we must choose from each row. The chosen panels must be first panels, i.e., leftmost. So it's a prefix.
Now, verify that we didn't misinterpret: "choose some non-zero number of the first panels in each row" could be interpreted as we choose, for each row, a number (call it ci), and then we push down the first ci panels. That is exactly prefix.
Now, check examples: Input:
2 2
-1 2
1 3
Output 2. Explanation: Choose first 2 panels from first row? That would be -1+2=1; and first 1 panel from second row: 1 => total 2. And condition: c1=2 > c2=1 holds. So matches.
Now, we can proceed.
Now, consider potential pitfalls with integer overflow in computing prefix sums: We're using long long, so fine.
Now, we should ensure that we treat m up to 1500, and we allocate vectors of size m+1. That's fine.
Now, let's write final code.
One more test: large n,m with random numbers to ensure no bug.
Now, any possible alternate pattern? What about n=1? But problem says n >= 2. So we always have at least two rows.
Now, answer may be negative. So we should initialize ans to NEG_INF and then take max.
Now, since we guarantee at least one valid comb, ans will be > NEG_INF.
Now, coding: We'll include <bits/stdc++.h>. Use using namespace std.
Now, I think it's ready.
But wait: There is a nuance: The DP we described uses the property that the transition depends only on the previous row's dp values. However, one could also think about a variant where the comb condition is for the entire sequence, but we handle it correctly.
Now, double-check the condition: "c1 > c2 < c3 > c4 < ...". The pattern: starting with >, then <, then >, then <, etc. So for i and i+1: if i is odd, then ci > ci+1; if i is even, then ci < ci+1. That's what we used.
Now, we need to be absolutely sure about the indexing: For i=1 (odd) => c1 > c2. For i=2 (even) => c2 < c3. For i=3 (odd) => c3 > c4. So yes.
Now, our DP uses (i-1) parity to determine relation between ci-1 and ci. For i=2, (i-1)=1 odd -> >, correct. For i=3, (i-1)=2 even -> <, correct.
Thus perfect.
Now, implement.
Potential performance: Using vector allocation for dpCurr and S each iteration. Since n is 1500, that's 1500 allocations of size ~1500, which is 2.25 million elements total, which is okay. But we could reuse memory by having dpCurr preallocated and then swapped, but not necessary.
But to be safe, we can allocate dpCurr as vector(m+1) each iteration. That's fine.
Alternatively, we can use two static vectors and swap references. Might be a bit faster. But okay.
We'll implement with two vectors: dpPrev, dpCurr; after each row, swap.
Initialize dpPrev after row1.
Then for i from 2 to n:
compute S.
dpCurr.assign(m+1, NEG_INF); // but we can resize and fill, but assign is O(m) anyway.
// then do loops to fill dpCurr.
// then swap: dpPrev.swap(dpCurr) or dpPrev = dpCurr.
But note: assign each element to NEG_INF is O(m), which is fine.
We can also use dpCurr = vector(m+1, NEG_INF). That also O(m). So total O(n*m) anyway.
Now, final answer.
Now, consider a possible alternative solution using segment tree? Not needed.
Now, test on some tricky cases.
Case: n=2, m=1500, with row1: all 10000, row2: all -10000. Then we want to maximize sum: c1 > c2. Since row2 values are negative, we want c2 as small as possible (1). c1 can be anything > 1, but row1 positive, we want c1 as large as possible (m=1500). But need c1 > c2, and c2=1, c1 can be 1500, that's fine. So total = sum row1 up to 1500 = 1500*10000 = 15e6, plus row2[1] = -10000, total = 14,990,000? Actually 15,000,000 - 10,000 = 14,990,000. But check: row1 sum = 15,000,000? 1500 * 10000 = 15,000,000; row2 first = -10000; sum = 14,990,000. Could there be better? If we choose c2=0? Not allowed. So that's max. DP: row1 dpPrev: prefix sums: 10000, 20000,..., 15e6. Row2 (>): loop from m down:
- start best = NEG_INF.
- c=1500: best NEG_INF => dpCurr[1500] invalid; update best = max(NEG_INF, dpPrev[1500] = 15e6) => best=15e6.
- c=1499: best=15e6 => dpCurr[1499] = S2[1499] + 15e6. But S2[1499] = 1499 * (-10000) = -14,990,000? Actually row2 all -10000, so prefix sums: -10000, -20000,... So S2[1499] = -14,990,000. Then dpCurr[1499] = -14,990,000 + 15,000,000 = 10,000? That's 10,000 positive? Wait 15,000,000 - 14,990,000 = 10,000. So dpCurr[1499] = 10,000. But we expect dpCurr[1] = best? Let's continue:
- c=1498: best remains 15e6 (since dpPrev[1499] maybe 14,990,000? Actually dpPrev[1499] = 14,990,000? Wait row1 prefix: at c=1499, sum = 1499*10000 = 14,990,000. So dpPrev[1499]=14,990,000. But we updated best after c=1499? In the loop, after processing c=1499, we update best = max(best, dpPrev[1499]). At that point best was 15e6 (from dpPrev[1500]). dpPrev[1499] = 14,990,000, which is less than 15e6, so best stays 15e6. So best remains 15e6 throughout. Then for each c, dpCurr[c] = S2[c] + 15e6. So dpCurr[1] = -10000 + 15e6 = 14,990,000. That's the maximum? Actually dpCurr[1] = 14,990,000, which matches our expected total. But we also got dpCurr[1499] = 10,000, which is lower. So the max dpCurr among all c is at c=1, which is 14,990,000. So answer = 14,990,000. Good.
But wait, is c2=1 allowed? Yes, because we need c1 > c2. With c2=1, we need c1>1, and we used best from dpPrev indices >1, which includes c1=1500 (best = 15e6). So dpCurr[1] = S2[1] + 15e6 = -10000 + 15e6 = 14,990,000. So yes.
Now, consider the case where row1 also has negatives but row2 positives. We'll trust DP.
Now, finalize code.
Potential pitfalls: Using int for reading but sum long long.
Now, we should also consider that the DP loops may need to handle the case where best > NEG_INF but dpPrev[c] might be NEG_INF. That's fine.
Now, let's think about initialization of dpPrev after first row: for c=1..m, dpPrev[c] = S[c]. No constraints yet. So all c are reachable.
Now, in the loops for i>1, we might have some c for which best is > NEG_INF but dpPrev values for some c are NEG_INF, but that's okay.
Now, note: The problem may require to output a single number — the maximum coins. So we output that.
Now, we should test with sample input.
Now, implement.
But we must also think about a possible alternative: Could the optimal solution involve not taking the maximum prefix sum from previous row due to later constraints? In DP we only consider immediate previous row; later rows don't affect the choice of previous rows beyond the immediate constraint? Actually the comb condition only relates adjacent rows. So the decision for row i only depends on row i-1's ci-1 and row i's ci. There is no future constraint that involves row i-1 directly beyond row i. So it's a typical Markov chain. Therefore, taking the maximum dp[i-1][c'] for each allowed transition is optimal for any future choices because dp[i-1][c'] already represents the maximum total up to row i-1 ending with that c'. And future choices only depend on the current ci, not on earlier choices. So it's correct.
Thus DP is optimal.
Now, let's code.
One more nuance: In the DP loops we are using S[c] as the contribution for current row. That is correct.
Now, we should ensure that we handle c from 1 to m inclusive. In the loops, we need to access S[c] and dpPrev[c] with indices within range. Since we allocated size m+1, S[0] is 0, but we don't use it. We only use S[1..m]. That's fine.
Now, potential issue: For row2 with > constraint, we have dpCurr for c=m set to NEG_INF because no previous >. That's fine.
Now, we will produce final answer.
Now, maybe there is a subtle condition: The comb must satisfy the alternating inequality "c1 > c2 < c3 > c4 < ...". But note the pattern might be ambiguous when n is odd: For last element c_n, there is no inequality after it. So no extra condition. We already accounted.
Now, think about if we could have multiple valid sequences with the same c_i? DP picks max.
Now, we must also ensure that we use long long for best, S, dpPrev, etc.
Now, compile and test mental.
Now, let's produce final answer in the ChatGPT response.
But before final, I want to re-check the problem statement for any misinterpretation:
"choose some non-zero number of the first panels in each row of the table and put stones on all those panels to push them down. After that she will receive a number of golden coins equal to the sum of numbers written on the chosen panels. ... According to the addition, for the room ceiling not to crush and smash the adventurer, the chosen panels should form a comb. It was explained that the chosen panels form a comb when the sequence c1, c2, ..., cn made from the quantities of panels chosen in each table line satisfies the following property: c1 > c2 < c3 > c4 < ..., i.e. the inequation mark interchanges between the neighboring elements."
So yes.
Now, confirm that "first panels" means leftmost panels; meaning we choose a prefix of each row. There is no option to skip columns; we must take the first ci columns. So that's correct.
Now, edge case: Could ci be equal to m? Yes, if allowed by constraints. For i=1, ci can be m. For i=2, if constraint is >, then ci cannot be m because need c1>c2, so c2 max m-1. For i=3, if constraint is <, then ci can be m (since need c2 < c3, so c3 can be m). So possible.
Now, our DP will handle.
Now, check if any row can have ci = 0? No, non-zero.
Now, DP ensures we consider ci from 1..m.
Now, all good.
Now, we should produce final answer with explanation and code.
But the problem asks: "Help her to determine the largest number of coins she can get and survive at the same time." So output maximum sum.
Now, we must be careful about integer overflow when using NEG_INF as -1e18 and adding S[c] which could be as low as -1.5e7, but adding them to -1e18 still yields a value > NEG_INF? Actually if we accidentally added NEG_INF to S[c], we would get something like -1e18 + (-1.5e7) which is still about -1.000015e18, which is > NEG_INF? Actually -1.000015e18 is less than -1e18? Wait -1.000015e18 is less than -1e18 (more negative). So it would be less than NEG_INF? But we defined NEG_INF as -1e18; so the result would be around -1.000015e18, which is less than NEG_INF. However, we never add to NEG_INF because we check if best > NEG_INF before adding. So no problem.
Now, also note: In the loops, we update best with dpPrev[c] only if dpPrev[c] > NEG_INF. So best is always a finite value (if any finite seen). So when we add S[c] + best, best is finite.
Now, one more nuance: When we compute best = max(best, dpPrev[c]), we need to compare long long values. That's fine.
Now, final code:
#include
#include
#include
using namespace std;
typedef long long ll;
const ll NEG_INF = -1e18;
int main() {
int n, m;
scanf("%d %d", &n, &m);
vector dpPrev(m+1, NEG_INF);
for (int i = 1; i <= n; ++i) {
vector<ll> S(m+1, 0);
for (int j = 1; j <= m; ++j) {
int val;
scanf("%d", &val);
S[j] = S[j-1] + val;
}
if (i == 1) {
for (int c = 1; c <= m; ++c)
dpPrev[c] = S[c];
} else {
vector<ll> dpCurr(m+1, NEG_INF);
if ((i-1) % 2 == 1) { // need previous > current
ll best = NEG_INF;
for (int c = m; c >= 1; --c) {
if (best > NEG_INF) {
dpCurr[c] = S[c] + best;
}
if (dpPrev[c] > NEG_INF) {
if (dpPrev[c] > best) best = dpPrev[c];
}
}
} else { // need previous < current
ll best = NEG_INF;
for (int c = 1; c <= m; ++c) {
if (best > NEG_INF) {
dpCurr[c] = S[c] + best;
}
if (dpPrev[c] > NEG_INF) {
if (dpPrev[c] > best) best = dpPrev[c];
}
}
}
dpPrev = dpCurr;
}
}
ll ans = NEG_INF;
for (int c = 1; c <= m; ++c) {
if (dpPrev[c] > ans) ans = dpPrev[c];
}
printf("%lld\n", ans);
return 0;
}
But we need to consider that in the first row, we might set dpPrev to S[c] for all c. That's reachable. No constraints yet.
Now, test with provided example.
Now, potential bug: In the > loop, we start with best = NEG_INF. We iterate c from m down to 1. At each step, we use best (which currently holds max of dpPrev indices > c). Then we update best with dpPrev[c]. But careful: The order is important: We should use best before including dpPrev[c] because we need c' > c, not >=. Our order does: first compute dpCurr[c] using current best (which doesn't include dpPrev[c] because we haven't added it yet). Then we update best with dpPrev[c]. So correct.
Now, similar for < loop: we compute dpCurr[c] using best (which holds max of dpPrev indices < c, because we have only updated best after processing previous c). Since we go from c=1 to m, and we update best after computing for c, then for next c+1, best includes dpPrev[1..c]. So correct.
Now, ensure that we don't accidentally use uninitialized S values; S is computed correctly.
Now, about reading: we use scanf inside loop, which may be a bit slow but fine for 2.25M numbers.
Now, final code ready.
But there is one more nuance: The values in the table are integers, but we use int for reading, but prefix sums as long long. That's fine.
Now, confirm with a random test using a script? Not possible here, but we trust.
Now, maybe we can micro-opt: Use fast I/O: ios_base::sync_with_stdio(false); cin.tie(NULL); but we use scanf, so it's fine.
Now, final answer: Provide explanation and code.
Now, I should write final answer with explanation and the code.
But the instruction: "Print the single number — the maximum number of coins Lara can get." So output is number.
Now, in code we used %lld for long long. That's fine.
Now, we might need to consider that n,m up to 1500, and we allocate vector each iteration, which might cause many memory allocations. But it's okay. Alternatively, we could preallocate S and dpCurr outside and reuse, but not needed.
Nevertheless, we can optimize by reusing S vector and dpCurr vector. But okay.
Now, final.
But wait, re-check the DP initialization for row1: Should we consider that c1 could be any from 1 to m, and all are allowed. So dpPrev = S.
Now, for row2, > constraint: The transition uses best from dpPrev indices > c. But note: For c2, we need c1 > c2. That means if c2 is say 1, then c1 can be 2..m. That's captured. However, what about c2 = m? No c1 > m, so invalid. So DP will set dpCurr[m] = -inf. Good.
Now, ensure that dpPrev values for c1 are finite, so best is finite for c2 < m.
Now, final answer.
But I've just thought of a possible twist: The comb property might be interpreted as the sequence of chosen panels (the set of positions) forms a comb shape in the grid, not just the counts per row. The problem statement: "the chosen panels should form a comb. It was explained that the chosen panels form a comb when the sequence c1, c2, ..., cn made from the quantities of panels chosen in each table line satisfies the following property: c1 > c2 < c3 > c4 < ..." So it's exactly about the counts, not the positions. So we are fine.
Now, I'd like to confirm with another example, perhaps where the optimal solution is not simply to maximize each row's prefix sum individually because constraints force some rows to take fewer. That's exactly what DP handles.
Now, think about a scenario where row1 has very high values only in the first few columns, and later columns negative, while row2 has high values in later columns but we must have c2 < c1. Since c1 is forced to be > c2, if we want to take many from row2 (which would require large c2), we need c1 even larger, but row1 might have negative contributions for larger c1. So DP will find the trade-off.
For instance: n=2,m=3.
Row1: [10, -1000, -1000] => prefix: [10, -990, -1990]
Row2: [-1000, -1000, 1000] => prefix: [-1000, -2000, -1000? Actually compute: -1000, -2000, -1000? Wait: -1000 + -1000 = -2000, plus 1000 = -1000. So prefix: [-1000, -2000, -1000]. The best combo: c1>c2. We could try c2=3? But need c1>3 impossible (m=3). c2=2 => need c1>2 => c1=3. Then sum = row1 prefix3 = -1990 + row2 prefix2 = -2000 => -3990. c2=1 => need c1>1 => c1 can be 2 or 3. Evaluate: c1=2,c2=1: row1 prefix2 = -990 + row2 prefix1 = -1000 = -1990. c1=3,c2=1: -1990 + -1000 = -2990. So best is -1990 with (c1=2,c2=1). So DP should output -1990.
Let's run DP: row1 dp = [10, -990, -1990].
Row2 (>):
c=3: best NEG_INF -> invalid; update best = max(NEG_INF, dpPrev[3] = -1990) => best = -1990.
c=2: best = -1990 -> dpCurr[2] = S2[2] + (-1990) = -2000 + (-1990) = -3990? Wait S2[2] = -2000. So dpCurr[2] = -3990. Then update best = max(-1990, dpPrev[2] = -990) => best becomes -990? Actually dpPrev[2] = -990, which is greater than -1990, so best becomes -990.
c=1: best = -990 -> dpCurr[1] = S2[1] + (-990) = -1000 + (-990) = -1990. Update best = max(-990, dpPrev[1] = 10) => best becomes 10.
Thus dpCurr = [ -1990, -3990, NEG_INF] so max = -1990. Good.
So DP works.
Now, consider scenario with n=3. row1: [10, -1000, -1000]; row2: [-1000, -1000, 1000]; row3: [1000, 1000, 1000] (all positive). Constraints: c1>c2<c3. The optimal may be to take c2 small to allow c1 large? But row1 large positive only at first column, row3 all positive, row2 negative. Let's try manually: We want c3 large because row3 positive. c2 must be < c3. So to allow large c3, c2 can be anything less than c3. But c2 also must be < c3 and c1 > c2. Since row1 has high only first column, maybe we want c1 small? But c1 must be > c2, so if we set c2 small, c1 can be slightly larger, but row1 after first column is negative, so we want c1 as small as possible (but > c2). So we want c2 as small as possible to allow c1 small? Actually we want c1 small because row1 after first column is very negative. So we want c1=2 maybe if c2=1, but c1=2 gives -990, which is negative. Alternatively c1=1? But need c1>c2, so if c2=1, c1>1, so c1 cannot be 1. So minimal c1 is 2 when c2=1. So c1=2 gives -990. Could we set c2=0? Not allowed. So c1 forced at least 2. Then c3 can be large, say 3, because c2=1 < 3. So sum = -990 (row1) + row2 prefix? Wait we also have row2 prefix c2: c2=1 gives row2 prefix1 = -1000. row3 prefix3 = 3000. Total = -990 -1000 + 3000 = 1010. Could we do better by having c2=2? Then c1>2 => c1=3 gives -1990; c3 > c2? Actually constraint c2 < c3, so c3 >2 => c3=3; row2 prefix2 = -2000; sum = -1990 -2000 + 3000 = -990. Worse. c2=1 seems best. So answer 1010. DP should compute.
Let's test DP: row1 dp: [10, -990, -1990]
Row2 (>): as before dp2 = [ -1990? Wait we computed earlier: dp2[1] = -1990, dp2[2] = -3990, dp2[3] invalid. Actually earlier we computed dp2[1] = -1990? Wait we had row2 S2: prefix: [-1000, -2000, -1000]. In previous test, we used row2 with values [-1000,-1000,1000] -> prefix: [-1000, -2000, -1000? Let's recalc: -1000; second: -2000; third: -1000 (since -2000+1000 = -1000). Yes. So row2 prefix: [-1000, -2000, -1000]. Then row2 dp2: from > constraint we got:
c=3: invalid; update best with dpPrev[3] = -1990 => best=-1990.
c=2: dpCurr[2] = S2[2] + best = -2000 + (-1990) = -3990; then update best = max(-1990, dpPrev[2] = -990) => best = -990.
c=1: dpCurr[1] = S2[1] + best = -1000 + (-990) = -1990; update best = max(-990, dpPrev[1]=10) => best=10.
So dp2 = [ -1990, -3990, NEG_INF].
Row3 (<): Need previous < current. dpPrev = dp2.
Compute S3: row3 all 1000 => prefix: [1000,2000,3000].
Now < loop: best = NEG_INF.
c=1: best NEG_INF => dpCurr[1] = NEG_INF; update best = max(NEG_INF, dp2[1] = -1990) => best = -1990? But dp2[1] = -1990, which is > NEG_INF, so best = -1990.
c=2: best = -1990 => dpCurr[2] = S3[2] + best = 2000 + (-1990) = 10; update best = max(-1990, dp2[2] = -3990) => best remains -1990.
c=3: best = -1990 => dpCurr[3] = S3[3] + best = 3000 + (-1990) = 1010; update best = max(-1990, dp2[3] = NEG_INF) => remains -1990.
So dp3 = [NEG_INF, 10, 1010]; max = 1010. So matches.
Thus DP correct.
Now, I'll finalize the solution.
But before final, I'll reflect: Could there be any scenario where the DP fails because we are using only the maximum dpPrev for each c, but maybe for a given c, we need to consider a specific c' to enable future constraints? Since future constraints only depend on current c, not on how we got there, the best dpPrev for each c is sufficient. So it's correct.
Now, one more nuance: When we compute best = max(best, dpPrev[c]) in the > loop, we are updating best to be the max over indices we have seen (which are >= current c). But we want best for smaller c to be max over indices > that c. Since we are iterating descending, after processing c, we add dpPrev[c] to the set, so for next (c-1), best includes dpPrev[c] as well as all larger indices. That's exactly the set of indices > (c-1) = indices >= c. But we need indices > (c-1), which are indices >= c. Yes, because > (c-1) includes c, c+1, ... m. Since we have added dpPrev[c] after processing c, for the next iteration (c-1), best includes dpPrev[c] and all previously added (which are > c). So it's correct. Good.
Now, we are ready to produce final answer.
Now, final code in C++.
But maybe we can simplify: Instead of separate if (i==1) branch, we could treat first row as dpPrev = S. That's fine.
Now, final answer with explanation and code.
Let's produce the final output. We need to maximize the sum of numbers on chosen panels. In each row we must take a prefix of length (c_i) ((1 \le c_i \le m)). The sequence (c_1, c_2, \dots, c_n) must satisfy the alternating strict inequalities
(c_1 > c_2 < c_3 > c_4 < \dots) (starting with ">").
Let (S[i][c] = \sum_{j=1}^{c} a_{i,j}) be the prefix sum of row (i).
Then the total reward is (\sum_i S[i][c_i]).
This is a dynamic programming problem over rows.
Define (dp[i][c]) as the maximum total for rows (1..i) ending with (c_i = c).
Then:
- (dp[1][c] = S[1][c]).
- For (i \ge 2):
If ((i-1)) is odd (i.e., the inequality between rows (i-1) and (i) is "(>)"), we need (c_{i-1} > c_i).
Hence (dp[i][c] = S[i][c] + \max_{c' > c} dp[i-1][c']).
If ((i-1)) is even (inequality is "(<)"), we need (c_{i-1} < c_i).
Hence (dp[i][c] = S[i][c] + \max_{c' < c} dp[i-1][c']).
The transitions require for each (c) the maximum over a suffix or a prefix of the previous DP row.
We can compute these maxima in (O(m)) using a running maximum:
- For "(>)" (previous > current): iterate (c) from (m) down to (1), maintaining the maximum of (dp[i-1][k]) for (k > c).
- For "(<)": iterate (c) from (1) to (m), maintaining the maximum of (dp[i-1][k]) for (k < c).
This gives (O(n m)) time and (O(m)) memory.
After processing all rows, the answer is (\max_{c} dp[n][c]).
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll NEG_INF = -1e18; // smaller than any possible sum
int main() {
int n, m;
scanf("%d %d", &n, &m);
vector<ll> dpPrev(m + 1, NEG_INF); // dp for previous row
for (int i = 1; i <= n; ++i) {
// read current row and compute its prefix sums S[1..m]
vector<ll> S(m + 1, 0);
for (int j = 1; j <= m; ++j) {
int x;
scanf("%d", &x);
S[j] = S[j - 1] + x;
}
if (i == 1) {
// first row: all lengths are reachable
for (int c = 1; c <= m; ++c)
dpPrev[c] = S[c];
} else {
vector<ll> dpCurr(m + 1, NEG_INF);
if ((i - 1) % 2 == 1) { // need previous > current
ll best = NEG_INF; // max of dpPrev[k] for k > current c
for (int c = m; c >= 1; --c) {
if (best > NEG_INF)
dpCurr[c] = S[c] + best;
if (dpPrev[c] > NEG_INF)
best = max(best, dpPrev[c]);
}
} else { // need previous < current
ll best = NEG_INF; // max of dpPrev[k] for k < current c
for (int c = 1; c <= m; ++c) {
if (best > NEG_INF)
dpCurr[c] = S[c] + best;
if (dpPrev[c] > NEG_INF)
best = max(best, dpPrev[c]);
}
}
dpPrev = dpCurr;
}
}
ll ans = NEG_INF;
for (int c = 1; c <= m; ++c)
if (dpPrev[c] > ans) ans = dpPrev[c];
printf("%lld\n", ans);
return 0;
}