Submission
Status:
[PPPPPPPPPP-SSSS]
Subtask/Task Score:
{0/100}
Score: 0
User: devilpoohs
Problemset: อัศวินขี่ม้าขาว
Language: cpp
Time: 0.058 second
Submitted On: 2026-03-05 20:57:46
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1010;
int ar[N][N];
int n,m;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>m;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>ar[i][j];
}
}
int dp[n][m];
dp[0][0]=ar[0][0];
// cout<<'\n';
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(i!=0 or j!=0){
int ans=LLONG_MIN;
if(i-1>=0)
ans=max(ans,ar[i][j]+dp[i-1][j]);
if(j-1>=0)
ans=max(ans,ar[i][j]+dp[i][j-1]);
dp[i][j]=ans;
}
// cout<<dp[i][j]<<'|';
}
// cout<<'\n';
}
// cout<<'\n';
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(i!=0 or j!=0){
int temp=dp[i][j];
dp[i][j]=LLONG_MIN;
if(i-1>=0)
dp[i][j]=max(dp[i][j],dp[i-1][j]);
if(j-1>=0)
dp[i][j]=max(dp[i][j],dp[i][j-1]);
dp[i][j]=min(dp[i][j],temp);
}
// cout<<dp[i][j]<<'|';
}
// cout<<'\n';
}
int one=1;
cout<<max(1-dp[n-1][m-1],one);
return 0;
}
/*
4 3
-3 -6 -3
2 2 -3
-8 -8 -8
1 1 1
4 3
-3 -6 -3
2 2 -3
-8 -8 -8
1 1 1
*/