-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiserman.cpp
More file actions
40 lines (40 loc) · 954 Bytes
/
miserman.cpp
File metadata and controls
40 lines (40 loc) · 954 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define pr pair<int,int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
typedef long long ll;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N,M;
cin>>N>>M;
int A[N][M],dp[N][M];
for(int i=0;i<N;i++) {
for(int j=0;j<M;j++)
cin>>A[i][j];
}
for(int i=0;i<M;i++)
dp[0][i]=A[0][i];
for(int i=1;i<N;i++) {
for(int j=0;j<M;j++) {
dp[i][j]=INT_MAX;
if(j>0&&j<M-1)
dp[i][j]=min(dp[i][j],A[i][j]+min(dp[i-1][j],min(dp[i-1][j-1],dp[i-1][j+1])));
else if(j==0)
dp[i][j]=min(dp[i][j],A[i][j]+min(dp[i-1][j],dp[i-1][j+1]));
else if(j==M-1)
dp[i][j]=min(dp[i][j],A[i][j]+min(dp[i-1][j],dp[i-1][j-1]));
}
}
int ans=INT_MAX;
for(int i=0;i<M;i++){
if(ans>dp[N-1][i])
ans=dp[N-1][i];
}
cout<<ans<<"\n";
return 0;
}