#include<bits/stdc++.h>
#define int long long
using namespace std;
main(){
int n , m , ans = 0;
cin >> n >> m;
vector<vector<int>> a(n + 1 , vector<int>(m + 1)) , mx(n + 2 , vector<int>(m + 2));
for(int i = 1;i <= n;i ++){
for(int j = 1;j <= m;j ++){
cin >> a[i][j];
}
}
auto Reset = [&](){
for(int i = 0;i <= n + 1;i ++){
for(int j = 0;j <= m + 1;j ++){
mx[i][j] = -1e16;
}
}
};
Reset();
for(int i = 1;i <= n;i ++){
for(int j = 1;j <= m;j ++){
mx[i][j] = max({mx[i - 1][j] , mx[i][j - 1] , i + j - a[i][j] - 1});
ans = max(ans , a[i][j] - i - j + mx[i][j]);
}
}
Reset();
for(int i = 1;i <= n;i ++){
for(int j = m;j >= 1;j --){
mx[i][j] = max({mx[i - 1][j] , mx[i][j + 1] , i - j - a[i][j] - 1});
ans = max(ans , a[i][j] - i + j + mx[i][j]);
}
}
Reset();
for(int i = n;i >= 1;i --){
for(int j = 1;j <= m;j ++){
mx[i][j] = max({mx[i + 1][j] , mx[i][j - 1] , -i + j - a[i][j] - 1});
ans = max(ans , a[i][j] + i - j + mx[i][j]);
}
}
Reset();
for(int i = n;i >= 1;i --){
for(int j = m;j >= 1;j --){
mx[i][j] = max({mx[i + 1][j] , mx[i][j + 1] , -i - j - a[i][j] - 1});
ans = max(ans , a[i][j] + i + j + mx[i][j]);
}
}
cout << ans << endl;
}
Compilation message
maxcomp.cpp:4:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
4 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |