某日,柱爷来到了喵哈哈城,准备干一票银行!
有道是知己知彼,百战不殆,早在前几日,柱爷就委托了卿学姐调查了喵哈哈城的银行资金分布情况!
而昨夜柱爷夜观天象,知晓今日是动手的绝佳时刻!
喵哈哈城是一个大的矩形方阵,而这方阵是由[Math Processing Error]
个矩形方格组成,方阵中的每个格子都有一家银行,这个银行的资金是 [Math Processing Error].
而管理喵哈哈城治安的人是潘警察!,此人智谋不在柱爷之下,20年前喵哈哈村的天行廖大师就因为抢劫银行被潘警察逮捕了,至今仍在天牢之中!
然而柱爷对此次行动早已有万全之策,更何况柱爷在暗,潘警察在明!
因为潘警察的缘由,柱爷的抢劫行动只能选取喵哈哈城的一个连通块,这个连通块必须满足下列条件
[Math Processing Error]
[Math Processing Error]
[Math Processing Error]
因为卿学姐实力不足的原因!其前几日在喵哈哈城的探查行动还是被潘警察察觉到了一点风声,因此潘警察在喵哈哈城的某些方格被设下了陷阱,如果柱爷选择了陷阱格子,那么柱爷将会损失掉相当于[Math Processing Error]
的现金!
Input第一行两个整数[Math Processing Error]
, [Math Processing Error],分别表示喵哈哈城的行长度和列长度
接下来[Math Processing Error]
行,每行 [Math Processing Error]个整数, [Math Processing Error], [Math Processing Error]数据保证:
[Math Processing Error]
[Math Processing Error]
输出仅一行,表示柱爷最多能抢到的钱
Sample Input3 4 5 -3 0 0 -2 3 3 4 -7 -6 4 -5 4 4 1 1 1 1 1 1 1 1 1 -1 1 1 1 1 1 1 2 2 -1 -1 -1 -1Sample Output
17 14 -1Hint
请注意柱爷不是临阵退缩的人,既然来了,[Math Processing Error]
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define x0 x0___
#define y0 y0___
#define pb push_back
#define SZ(X) ((int)X.size())
#define mp make_pair
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pli pair<ll,int>
#define pil pair<int,ll>
#define ALL(X) X.begin(),X.end()
#define RALL(X) X.rbegin(),X.rend()
#define rep(i,j,k) for(int i = j;i <= k;i ++)
#define per(i,j,k) for(int i = j;i >= k;i --)
#define mem(a,p) memset(a,p,sizeof(a))
ll qmod(ll a,ll b,ll c) {ll res=1;a%=c; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%c;a=a*a%c;}return res;}
template<typename T, typename S>
void upmax(T& a,S b){if(a<b) a=b;}
template<typename T, typename S>
void upmin(T& a,S b){if(a>b) a=b;}
template<typename T>
void W(T b){cout << b << endl;}
void gettle() {while(1);}
void getre() {int t=0;t/=t;}
/
/
/
/
const int N = 1024;
ll f1[N][N];
ll f2[N][N];
ll pre[N][N];
ll dp[N][N];
ll a[N][N];
ll sum(int i,ll l)
{
return pre[i][l];
}
int main()
{
int n, m;
scanf("%d %d", &n, &m);
rep(i,1,n) rep(j,1,m) scanf("%lld",&a[i][j]),pre[i][j]=pre[i][j-1]+a[i][j];
rep (i,1,n) {
rep (j,1,m) {
f1[i][j] = max(f1[i][j-1] + a[i][j], a[i][j]);
upmax(f1[i][j], 0);
}
per (j,m,1) {
f2[i][j] = max(f2[i][j+1] + a[i][j], a[i][j]);
upmax(f2[i][j], 0);
}
rep (j,1,m) {
dp[i][j] = a[i][j] + f1[i][j-1] + f2[i][j+1];
}
}
ll LINF = (1LL<<62);
rep (i,2,n) {
ll tmp = -LINF;
rep (j,1,m) {
upmax(tmp, dp[i-1][j] - sum(i, j-1) + f1[i][j-1]);
upmax(dp[i][j], tmp + f2[i][j+1] + sum(i, j));
}
tmp = -LINF;
per (j,m,1) {
upmax(tmp, dp[i-1][j] + sum(i, j) + f2[i][j+1]);
upmax(dp[i][j], tmp - sum(i, j-1) + f1[i][j-1]);
}
}
ll res = dp[1][1];
rep(i,1,n) rep(j,1,m) upmax(res,dp[i][j]);
printf("%lld\n", res);
return 0;
}