#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 |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8656 KB |
Output is correct |
7 |
Correct |
1 ms |
8536 KB |
Output is correct |
8 |
Correct |
1 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8656 KB |
Output is correct |
7 |
Correct |
1 ms |
8536 KB |
Output is correct |
8 |
Correct |
1 ms |
8796 KB |
Output is correct |
9 |
Correct |
3 ms |
11096 KB |
Output is correct |
10 |
Correct |
2 ms |
10844 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10844 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
2 ms |
10716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8656 KB |
Output is correct |
7 |
Correct |
1 ms |
8536 KB |
Output is correct |
8 |
Correct |
1 ms |
8796 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
1 ms |
8540 KB |
Output is correct |
12 |
Correct |
3 ms |
11096 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
2 ms |
10844 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
10844 KB |
Output is correct |
17 |
Correct |
2 ms |
10716 KB |
Output is correct |
18 |
Correct |
51 ms |
24920 KB |
Output is correct |
19 |
Correct |
51 ms |
28984 KB |
Output is correct |
20 |
Correct |
47 ms |
28244 KB |
Output is correct |
21 |
Correct |
49 ms |
28496 KB |
Output is correct |
22 |
Correct |
50 ms |
28540 KB |
Output is correct |
23 |
Correct |
50 ms |
28500 KB |
Output is correct |
24 |
Correct |
51 ms |
29132 KB |
Output is correct |