#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 1e3 + 2;
const int inf = 1e9;
int a[N][N], p[N][N], q[N][N], r[N][N], s[N][N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> a[i][j];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
p[i][j] = -a[i][j] + i + j;
if (i > 0) smax(p[i][j], p[i - 1][j]);
if (j > 0) smax(p[i][j], p[i][j - 1]);
}
}
for (int i = 0; i < n; i++) {
for (int j = m - 1; j >= 0; j--) {
q[i][j] = -a[i][j] + i - j;
if (i > 0) smax(q[i][j], q[i - 1][j]);
if (j < m - 1) smax(q[i][j], q[i][j + 1]);
}
}
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j < m; j++) {
r[i][j] = -a[i][j] - i + j;
if (i < n - 1) smax(r[i][j], r[i + 1][j]);
if (j > 0) smax(r[i][j], r[i][j - 1]);
}
}
for (int i = n - 1; i >= 0; i--) {
for (int j = m - 1; j >= 0; j--) {
s[i][j] = -a[i][j] - i - j;
if (i < n - 1) smax(s[i][j], s[i + 1][j]);
if (j < m - 1) smax(s[i][j], s[i][j + 1]);
}
}
int ans = -inf;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
smax(ans, a[i][j] - i - j + p[i][j] - 1);
smax(ans, a[i][j] - i + j + q[i][j] - 1);
smax(ans, a[i][j] + i - j + r[i][j] - 1);
smax(ans, a[i][j] + i + j + s[i][j] - 1);
}
}
cout << ans << en;
return 0;
}
# |
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 |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 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 |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
1240 KB |
Output is correct |
10 |
Correct |
1 ms |
1244 KB |
Output is correct |
11 |
Correct |
2 ms |
1236 KB |
Output is correct |
12 |
Correct |
2 ms |
1248 KB |
Output is correct |
13 |
Correct |
2 ms |
1252 KB |
Output is correct |
14 |
Correct |
1 ms |
1236 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 |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
1240 KB |
Output is correct |
13 |
Correct |
1 ms |
1244 KB |
Output is correct |
14 |
Correct |
2 ms |
1236 KB |
Output is correct |
15 |
Correct |
2 ms |
1248 KB |
Output is correct |
16 |
Correct |
2 ms |
1252 KB |
Output is correct |
17 |
Correct |
1 ms |
1236 KB |
Output is correct |
18 |
Correct |
122 ms |
28428 KB |
Output is correct |
19 |
Correct |
115 ms |
28368 KB |
Output is correct |
20 |
Correct |
110 ms |
28064 KB |
Output is correct |
21 |
Correct |
122 ms |
28372 KB |
Output is correct |
22 |
Correct |
119 ms |
28416 KB |
Output is correct |
23 |
Correct |
128 ms |
28372 KB |
Output is correct |
24 |
Correct |
152 ms |
28268 KB |
Output is correct |