Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int inf = 1e9;
- const int N = 1e3 + 10;
- int ar[N][N], dp[N][N][3];
- int main(){
- int n, m;
- scanf("%d %d", &n, &m);
- for(int i=1;i<=n;i++){
- for(int j=1;j<=m;j++){
- scanf("%d", &ar[i][j]);
- }
- }
- int ans = 0;
- for(int i=1;i<=n;i++){
- for(int j=1;j<=m;j++){
- dp[i][j][0] = max({ i > 1 ? dp[i - 1][j][0] : 0 , j > 1 ? dp[i][j - 1][0] : 0 ,
- i > 1 ? dp[i - 1][j][1] : 0 , j > 1 ? dp[i][j - 1][1] : 0 ,
- i > 1 ? dp[i - 1][j][2] : 0 , j > 1 ? dp[i][j - 1][2] : 0 });
- dp[i][j][1] = ar[i][j] + max({ i > 1 ? dp[i - 1][j][0] : 0 , j > 1 ? dp[i][j - 1][0] : 0 });
- dp[i][j][2] = ar[i][j] + max({ i > 1 ? dp[i - 1][j][1] : 0 , j > 1 ? dp[i][j - 1][1] : 0});
- }
- }
- printf("%d", max({ dp[n][m][0] , dp[n][m][1] , dp[n][m][2] }));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement