#include <iostream>
#define int long long
using namespace std;
const int MAX_N = 1e3;
int a[MAX_N + 1][MAX_N + 1];
int dp1[MAX_N + 1][MAX_N + 1], dp2[MAX_N + 1][MAX_N + 1], dp3[MAX_N + 1][MAX_N + 1], dp4[MAX_N + 1][MAX_N + 1] ;
int n, m;
signed main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
dp1[i][j] = i + j - a[i][j];
if (i > 1) {
dp1[i][j] = max(dp1[i][j], dp1[i - 1][j]);
}
if (j > 1) {
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] = i - j - a[i][j];
if (i > 1) {
dp2[i][j] = max(dp2[i][j], dp2[i - 1][j]);
}
if (j < m) {
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] = -i + j - a[i][j];
if (i < n) {
dp3[i][j] = max(dp3[i][j], dp3[i + 1][j]);
}
if (j > 1) {
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] = -i - j - a[i][j];
if (i < n) {
dp4[i][j] = max(dp4[i][j], dp4[i + 1][j]);
}
if (j < m) {
dp4[i][j] = max(dp4[i][j], dp4[i][j + 1]);
}
}
}
int answer = -1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
answer = max(answer, a[i][j] - i - j - 1 + dp1[i][j]);
answer = max(answer, a[i][j] - i + j - 1 + dp2[i][j]);
answer = max(answer, a[i][j] + i - j - 1 + dp3[i][j]);
answer = max(answer, a[i][j] + i + j - 1 + dp4[i][j]);
}
}
cout << answer;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
1352 KB |
Output is correct |
10 |
Correct |
3 ms |
1344 KB |
Output is correct |
11 |
Correct |
2 ms |
1364 KB |
Output is correct |
12 |
Correct |
2 ms |
1364 KB |
Output is correct |
13 |
Correct |
2 ms |
1364 KB |
Output is correct |
14 |
Correct |
2 ms |
1364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
1352 KB |
Output is correct |
13 |
Correct |
3 ms |
1344 KB |
Output is correct |
14 |
Correct |
2 ms |
1364 KB |
Output is correct |
15 |
Correct |
2 ms |
1364 KB |
Output is correct |
16 |
Correct |
2 ms |
1364 KB |
Output is correct |
17 |
Correct |
2 ms |
1364 KB |
Output is correct |
18 |
Correct |
351 ms |
47916 KB |
Output is correct |
19 |
Correct |
392 ms |
48228 KB |
Output is correct |
20 |
Correct |
337 ms |
47692 KB |
Output is correct |
21 |
Correct |
360 ms |
48096 KB |
Output is correct |
22 |
Correct |
355 ms |
47996 KB |
Output is correct |
23 |
Correct |
349 ms |
47936 KB |
Output is correct |
24 |
Correct |
427 ms |
46796 KB |
Output is correct |