#include <bits/stdc++.h>
#define int long long
using namespace std;
string yn(bool ans,string y="YES",string n="NO"){
return (ans?y:n);
}
#define all(a) a.begin(),a.end()
void gogo(){
int n,m;
cin>>n>>m;
int a[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>a[i][j];
}
}
int ans=-INT_MAX;
int mn[n+1][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
mn[i][j]=a[i][j]-i-j;
if(i>0){
mn[i][j]=min(mn[i][j],mn[i-1][j]);
}
if(j>0){
mn[i][j]=min(mn[i][j],mn[i][j-1]);
}
if(j and i){
mn[i][j]=min(mn[i][j],mn[i-1][j-1]);
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
ans=max(ans,a[i][j]-i-j-mn[i][j]);
}
}
for(int i=0;i<n;i++){
reverse(a[i],a[i]+m);
}
for(int i=0;i<n-i-1;i++){
for(int j=0;j<m;j++){
swap(a[i][j],a[n-1-i][j]);
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
mn[i][j]=a[i][j]-i-j;
if(i>0){
mn[i][j]=min(mn[i][j],mn[i-1][j]);
}
if(j>0){
mn[i][j]=min(mn[i][j],mn[i][j-1]);
}
if(j and i){
mn[i][j]=min(mn[i][j],mn[i-1][j-1]);
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
ans=max(ans,a[i][j]-i-j-mn[i][j]);
}
}
for(int i=0;i<n-i-1;i++){
for(int j=0;j<m;j++){
swap(a[i][j],a[n-1-i][j]);
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
mn[i][j]=a[i][j]-i-j;
if(i>0){
mn[i][j]=min(mn[i][j],mn[i-1][j]);
}
if(j>0){
mn[i][j]=min(mn[i][j],mn[i][j-1]);
}
if(j and i){
mn[i][j]=min(mn[i][j],mn[i-1][j-1]);
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
ans=max(ans,a[i][j]-i-j-mn[i][j]);
}
}for(int i=0;i<n;i++){
reverse(a[i],a[i]+m);
}
for(int i=0;i<n-i-1;i++){
for(int j=0;j<m;j++){
swap(a[i][j],a[n-1-i][j]);
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
mn[i][j]=a[i][j]-i-j;
if(i>0){
mn[i][j]=min(mn[i][j],mn[i-1][j]);
}
if(j>0){
mn[i][j]=min(mn[i][j],mn[i][j-1]);
}
if(j and i){
mn[i][j]=min(mn[i][j],mn[i-1][j-1]);
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
ans=max(ans,a[i][j]-i-j-mn[i][j]);
}
}
cout<<ans-1<<endl;
}
main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t=1;
// cin>>t;
while(t--){
gogo();
}
}
Compilation message
maxcomp.cpp:117:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
117 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
388 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
388 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
98 ms |
15948 KB |
Output is correct |
19 |
Correct |
97 ms |
15952 KB |
Output is correct |
20 |
Correct |
92 ms |
15308 KB |
Output is correct |
21 |
Correct |
98 ms |
15956 KB |
Output is correct |
22 |
Correct |
98 ms |
15956 KB |
Output is correct |
23 |
Correct |
108 ms |
15952 KB |
Output is correct |
24 |
Correct |
96 ms |
15192 KB |
Output is correct |