#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int MAXN = 1000;
ll mat1[MAXN+5][MAXN+5];
ll mat2[MAXN+5][MAXN+5];
ll mat3[MAXN+5][MAXN+5];
ll mat4[MAXN+5][MAXN+5];
ll a[MAXN+5][MAXN+5];
int main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cout.precision(10);
cout << fixed;
int n, m;
cin >> n >> m;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
cin >> a[i][j];
}
}
ll res = -1;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
mat1[i][j] = i+j - a[i][j];
if(i > 1) mat1[i][j] = max(mat1[i][j], mat1[i-1][j]);
if(j > 1) mat1[i][j] = max(mat1[i][j], mat1[i][j-1]);
}
}
for(int i=1; i<=n; i++){
for(int j=m; j>=1; j--){
mat2[i][j] = i-j - a[i][j];
if(i > 1) mat2[i][j] = max(mat2[i][j], mat2[i-1][j]);
if(j < m) mat2[i][j] = max(mat2[i][j], mat2[i][j+1]);
}
}
for(int i=n; i>=1; i--){
for(int j=1; j<=m; j++){
mat3[i][j] = -i+j - a[i][j];
if(i < n) mat3[i][j] = max(mat3[i][j], mat3[i+1][j]);
if(j > 1) mat3[i][j] = max(mat3[i][j], mat3[i][j-1]);
}
}
for(int i=n; i>=1; i--){
for(int j=m; j>=1; j--){
mat4[i][j] = -i-j - a[i][j];
if(i < n) mat4[i][j] = max(mat4[i][j], mat4[i+1][j]);
if(j < m) mat4[i][j] = max(mat4[i][j], mat4[i][j+1]);
}
}
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
res = max(res, a[i][j]-i-j + mat1[i][j] - 1);
res = max(res, a[i][j]-i+j + mat2[i][j] - 1);
res = max(res, a[i][j]+i-j + mat3[i][j] - 1);
res = max(res, a[i][j]+i+j + mat4[i][j] - 1);
}
}
cout << res << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
1356 KB |
Output is correct |
10 |
Correct |
2 ms |
1356 KB |
Output is correct |
11 |
Correct |
2 ms |
1356 KB |
Output is correct |
12 |
Correct |
2 ms |
1356 KB |
Output is correct |
13 |
Correct |
2 ms |
1356 KB |
Output is correct |
14 |
Correct |
2 ms |
1356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
2 ms |
1356 KB |
Output is correct |
13 |
Correct |
2 ms |
1356 KB |
Output is correct |
14 |
Correct |
2 ms |
1356 KB |
Output is correct |
15 |
Correct |
2 ms |
1356 KB |
Output is correct |
16 |
Correct |
2 ms |
1356 KB |
Output is correct |
17 |
Correct |
2 ms |
1356 KB |
Output is correct |
18 |
Correct |
151 ms |
48188 KB |
Output is correct |
19 |
Correct |
151 ms |
48124 KB |
Output is correct |
20 |
Correct |
150 ms |
47812 KB |
Output is correct |
21 |
Correct |
150 ms |
48168 KB |
Output is correct |
22 |
Correct |
147 ms |
48068 KB |
Output is correct |
23 |
Correct |
153 ms |
48068 KB |
Output is correct |
24 |
Correct |
147 ms |
46988 KB |
Output is correct |