#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
const long long INF = 1e18;
int n, m;
long long a[N][N];
long long f1[N][N], f2[N][N];
long long res;
void reset() {
for (int i = 0; i <= n + 1; ++i) {
for (int j = 0; j <= m + 1; ++j) {
f1[i][j] = f2[i][j] = -INF;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cin >> a[i][j];
}
}
res = -INF;
reset();
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
f1[i][j] = max(max(f1[i - 1][j], f1[i][j - 1]), -a[i][j] + i + j);
f2[i][j] = max(max(f2[i - 1][j], f2[i][j - 1]), +a[i][j] + i + j);
res = max(res, f1[i][j] + a[i][j] - i - j);
res = max(res, f2[i][j] - a[i][j] - i - j);
}
}
reset();
for (int i = 1; i <= n; ++i) {
for (int j = m; j >= 1; --j) {
f1[i][j] = max(max(f1[i - 1][j], f1[i][j + 1]), -a[i][j] + i - j);
f2[i][j] = max(max(f2[i - 1][j], f2[i][j + 1]), +a[i][j] + i - j);
res = max(res, f1[i][j] + a[i][j] - i + j);
res = max(res, f2[i][j] - a[i][j] - i + j);
}
}
reset();
for (int i = n; i >= 1; --i) {
for (int j = 1; j <= m; ++j) {
f1[i][j] = max(max(f1[i + 1][j], f1[i][j - 1]), -a[i][j] - i + j);
f2[i][j] = max(max(f2[i + 1][j], f2[i][j - 1]), +a[i][j] - i + j);
res = max(res, f1[i][j] + a[i][j] + i - j);
res = max(res, f2[i][j] - a[i][j] + i - j);
}
}
reset();
for (int i = n; i >= 1; --i) {
for (int j = m; j >= 1; --j) {
f1[i][j] = max(max(f1[i + 1][j], f1[i][j + 1]), -a[i][j] - i - j);
f2[i][j] = max(max(f2[i + 1][j], f2[i][j + 1]), +a[i][j] - i - j);
res = max(res, f1[i][j] + a[i][j] + i + j);
res = max(res, f2[i][j] - a[i][j] + i + j);
}
}
cout << res - 1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
488 KB |
Output is correct |
3 |
Correct |
2 ms |
672 KB |
Output is correct |
4 |
Correct |
2 ms |
672 KB |
Output is correct |
5 |
Correct |
2 ms |
672 KB |
Output is correct |
6 |
Correct |
2 ms |
700 KB |
Output is correct |
7 |
Correct |
2 ms |
700 KB |
Output is correct |
8 |
Correct |
2 ms |
700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
700 KB |
Output is correct |
2 |
Correct |
2 ms |
700 KB |
Output is correct |
3 |
Correct |
2 ms |
700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
488 KB |
Output is correct |
3 |
Correct |
2 ms |
672 KB |
Output is correct |
4 |
Correct |
2 ms |
672 KB |
Output is correct |
5 |
Correct |
2 ms |
672 KB |
Output is correct |
6 |
Correct |
2 ms |
700 KB |
Output is correct |
7 |
Correct |
2 ms |
700 KB |
Output is correct |
8 |
Correct |
2 ms |
700 KB |
Output is correct |
9 |
Correct |
3 ms |
1276 KB |
Output is correct |
10 |
Correct |
4 ms |
1276 KB |
Output is correct |
11 |
Correct |
3 ms |
1276 KB |
Output is correct |
12 |
Correct |
4 ms |
1276 KB |
Output is correct |
13 |
Correct |
3 ms |
1276 KB |
Output is correct |
14 |
Correct |
3 ms |
1276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
488 KB |
Output is correct |
3 |
Correct |
2 ms |
672 KB |
Output is correct |
4 |
Correct |
2 ms |
672 KB |
Output is correct |
5 |
Correct |
2 ms |
672 KB |
Output is correct |
6 |
Correct |
2 ms |
700 KB |
Output is correct |
7 |
Correct |
2 ms |
700 KB |
Output is correct |
8 |
Correct |
2 ms |
700 KB |
Output is correct |
9 |
Correct |
2 ms |
700 KB |
Output is correct |
10 |
Correct |
2 ms |
700 KB |
Output is correct |
11 |
Correct |
2 ms |
700 KB |
Output is correct |
12 |
Correct |
3 ms |
1276 KB |
Output is correct |
13 |
Correct |
4 ms |
1276 KB |
Output is correct |
14 |
Correct |
3 ms |
1276 KB |
Output is correct |
15 |
Correct |
4 ms |
1276 KB |
Output is correct |
16 |
Correct |
3 ms |
1276 KB |
Output is correct |
17 |
Correct |
3 ms |
1276 KB |
Output is correct |
18 |
Correct |
195 ms |
24428 KB |
Output is correct |
19 |
Correct |
201 ms |
24428 KB |
Output is correct |
20 |
Correct |
171 ms |
24428 KB |
Output is correct |
21 |
Correct |
231 ms |
24428 KB |
Output is correct |
22 |
Correct |
188 ms |
24428 KB |
Output is correct |
23 |
Correct |
185 ms |
24428 KB |
Output is correct |
24 |
Correct |
213 ms |
24428 KB |
Output is correct |