#include <bits/stdc++.h>
#define ar array
//#define int long long
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1e9 + 7;
const ll inf = 1e18;
const int N = 1e3 + 5;
int n, m, a[N][N], dp1[N][N], dp2[N][N];
void rotate() {
int b[m+1][n+1];
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++) b[j][n-i+1] = a[i][j];
for(int i=1; i<=m; i++)
for(int j=1; j<=n; j++) a[i][j] = b[i][j];
swap(n, m);
}
int solve() {
int ans = -1;
for(int i=0; i<=n; i++)
for(int j=0; j<=m; j++) dp1[i][j] = dp2[i][j] = -2e9;
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
dp1[i][j] = max(dp1[i-1][j], dp1[i][j-1]);
dp2[i][j] = max(dp2[i-1][j], dp2[i][j-1]);
ans = max(ans, dp1[i][j] + a[i][j] - i - j - 1);
ans = max(ans, dp2[i][j] - a[i][j] - i - j - 1);
dp1[i][j] = max(dp1[i][j], -a[i][j] + i + j);
dp2[i][j] = max(dp2[i][j], a[i][j] + i + j);
}
}
return ans;
}
signed main() {
cin >> n >> m;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++) cin >> a[i][j];
int ans = -1;
for(int i=0; i<4; i++) {
ans = max(ans, solve());
rotate();
}
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |