This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
const int inf = 2e9;
int n, m;
int a[maxn][maxn];
int dp1[maxn][maxn];
int dp2[maxn][maxn];
int dp3[maxn][maxn];
int dp4[maxn][maxn];
char s;
int num;
int read()
{
num = 0;
s = getchar();
while (s < '0' || s > '9')
{
s = getchar();
}
while ('0' <= s && s <= '9')
{
num = num * 10 + (s - '0');
s = getchar();
}
return num;
}
void fastIO()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main()
{
fastIO();
n = read();
m = read();
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
a[i][j] = read();
}
}
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
dp1[i][j] = -a[i][j] + i + j;
if (i >= 2)
{
dp1[i][j] = max(dp1[i][j], dp1[i - 1][j]);
}
if (j >= 2)
{
dp1[i][j] = max(dp1[i][j], dp1[i][j - 1]);
}
}
}
for (int i = 1; i <= n; ++i)
{
for (int j = m; j >= 1; --j)
{
dp2[i][j] = -a[i][j] + i - j;
if (i >= 2)
{
dp2[i][j] = max(dp2[i][j], dp2[i - 1][j]);
}
if (j <= m - 1)
{
dp2[i][j] = max(dp2[i][j], dp2[i][j + 1]);
}
}
}
for (int i = n; i >= 1; --i)
{
for (int j = 1; j <= m; ++j)
{
dp3[i][j] = -a[i][j] - i + j;
if (i <= n - 1)
{
dp3[i][j] = max(dp3[i][j], dp3[i + 1][j]);
}
if (j >= 2)
{
dp3[i][j] = max(dp3[i][j], dp3[i][j - 1]);
}
}
}
for (int i = n; i >= 1; --i)
{
for (int j = m; j >= 1; --j)
{
dp4[i][j] = -a[i][j] - i - j;
if (i <= n - 1)
{
dp4[i][j] = max(dp4[i][j], dp4[i + 1][j]);
}
if (j <= m - 1)
{
dp4[i][j] = max(dp4[i][j], dp4[i][j + 1]);
}
}
}
int ans = -inf;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
ans = max(ans, a[i][j] + dp1[i][j] - i - j);
ans = max(ans, a[i][j] + dp2[i][j] - i + j);
ans = max(ans, a[i][j] + dp3[i][j] + i - j);
ans = max(ans, a[i][j] + dp4[i][j] + i + j);
}
}
cout << ans - 1 << endl;
return 0;
}
# | 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... |