#include<bits/stdc++.h>
#define f first
#define s second
#define pii pair< pair<int,pair<int,int > > ,pair<int,int> >
using namespace std;
const int N=1e3+5,INF=2e9;
int n,m,dp[N][N][5],k,i,ans,a[N][N];
int a1[]={0,0,-1,1},a2[]={-1,1,0,0};
priority_queue<pii,vector<pii>,greater<pii> > q;
bool ok(int x,int y){
if(min(x,y)<1 || x>n || y>m ) return 0;
return 1;
}
main(){
cin>>n>>m;
for(k=1;k<=n;k++){
for(int i=1;i<=m;i++){
cin>>a[k][i];
dp[k][i][0]=0;
}
}
ans=INF;
for(k=1;k<=2;k++){
for(i=1;i<=n;i++){
for(int j=1;j<=m;j++){
int cur=dp[i][j][k-1];
if(k==1) dp[i][j][k]=dp[i][j][k-1]+a[i][j];
else dp[i][j][k]=dp[i][j][k-1]-a[i][j];
if(i>1)dp[i][j][k] = min (dp[i][j][k],dp[i-1][j][k]+1);
if(j>1)dp[i][j][k] = min (dp[i][j][k],dp[i][j-1][k]+1);
}
}
for(i=n;i>=1;i--){
for(int j=m;j>=1;j--){
if(i<n)dp[i][j][k] = min (dp[i][j][k],dp[i+1][j][k]+1);
if(j<m)dp[i][j][k] = min (dp[i][j][k],dp[i][j+1][k]+1);
if(k==2) ans=min(ans,dp[i][j][k]);
}
}
}
cout<<-ans-1;
}
Compilation message
maxcomp.cpp:14:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
14 | main(){
| ^
maxcomp.cpp: In function 'int main()':
maxcomp.cpp:27:9: warning: unused variable 'cur' [-Wunused-variable]
27 | int cur=dp[i][j][k-1];
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
2 ms |
748 KB |
Output is correct |
10 |
Correct |
2 ms |
748 KB |
Output is correct |
11 |
Correct |
2 ms |
748 KB |
Output is correct |
12 |
Correct |
2 ms |
748 KB |
Output is correct |
13 |
Correct |
2 ms |
748 KB |
Output is correct |
14 |
Correct |
1 ms |
748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
2 ms |
748 KB |
Output is correct |
13 |
Correct |
2 ms |
748 KB |
Output is correct |
14 |
Correct |
2 ms |
748 KB |
Output is correct |
15 |
Correct |
2 ms |
748 KB |
Output is correct |
16 |
Correct |
2 ms |
748 KB |
Output is correct |
17 |
Correct |
1 ms |
748 KB |
Output is correct |
18 |
Correct |
411 ms |
23916 KB |
Output is correct |
19 |
Correct |
408 ms |
23964 KB |
Output is correct |
20 |
Correct |
394 ms |
23916 KB |
Output is correct |
21 |
Correct |
417 ms |
24044 KB |
Output is correct |
22 |
Correct |
408 ms |
24044 KB |
Output is correct |
23 |
Correct |
411 ms |
23916 KB |
Output is correct |
24 |
Correct |
432 ms |
23020 KB |
Output is correct |