Codeforces Round 977 (Div. 2, based on COMPFEST 16 - Final Round)
D
Boss, Thirsty
Codeforces
2500
原题
← C2E1 →
翻译中…

Pak Chanek has a friend who runs a drink stall in a canteen. His friend will sell drinks for $n$ days, numbered from day $1$ to day $n$. There are also $m$ types of drinks, numbered from $1$ to $m$.

翻译中…

The profit gained from selling a drink on a particular day can vary. On day $i$, the projected profit from selling drink of type $j$ is $A_{i, j}$. Note that $A_{i, j}$ can be negative, meaning that selling the drink would actually incur a loss.

翻译中…

Pak Chanek wants to help his friend plan the sales over the $n$ days. On day $i$, Pak Chanek must choose to sell at least one type of drink. Furthermore, the types of drinks sold on a single day must form a subarray. In other words, in each day, Pak Chanek will select $i$ and $j$ such that $1 \leq i \leq j \leq m$. Then all types of drinks between $i$ and $j$ (inclusive) will be sold.

翻译中…

However, to ensure that customers from the previous day keep returning, the selection of drink types sold on day $i$ ($i \gt 1$) must meet the following conditions:

翻译中…
  • At least one drink type sold on day $i$ must also have been sold on day $i-1$.
  • At least one drink type sold on day $i$ must not have been sold on day $i-1$.
翻译中…

The daily profit is the sum of the profits from all drink types sold on that day. The total profit from the sales plan is the sum of the profits over $n$ days. What is the maximum total profit that can be achieved if Pak Chanek plans the sales optimally?

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). The description of the test cases follows.

翻译中…

The first line of each test case contains two integers $n$ and $m$ ($1 \leq n \leq 2 \cdot 10^5$; $3 \leq m \leq 2 \cdot 10^5$; $n \cdot m \leq 2 \cdot 10^5$) — the number of rows and columns in a grid.

翻译中…

The next $n$ lines of each test case contain $m$ integers each, where the $i$-th line contains the integers $A_{i,1} A_{i,2}, \ldots, A_{i,m}$ ($-10^9 \leq A_{i,j} \leq 10^9$) — project profits of each drink type on the $i$-th day.

翻译中…

It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $2 \cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, output a single integer: the maximum profit that Pak Chanek can achieve.

翻译中…

Note

翻译中…

Here is Pak Chanek's optimal plan:

翻译中…

imageimageimage

翻译中…
  • On day $1$, Pak Chanek sells drink types $1$ to $3$. Generating a profit of $79+20+49 = 148$.
  • On day $2$, Pak Chanek sells drink types $2$ to $4$. Generating a profit of $9+109+24 = 142$
  • On day $3$, Pak Chanek sells drink types $1$ to $6$. Generating a profit of $185$.
翻译中…

So, the total profit of Pak Chanek's plan is $148 + 142 + 185 = 475$.

翻译中…

样例

Input #1
1
3 6
79 20 49 5 -1000 500
-105 9 109 24 -98 -499
14 47 12 39 23 50
Output #1
475
InputCopy OutputCopy
题目信息
题号
2021D
时间限制
2 s
内存限制
256 MB
标签
动态规划
贪心
模拟

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