LEGEND2004

C - Turtle

Sep 6th, 2023
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5. const int N = 1e3 + 5;
  6. const int mod = 1e9 + 7;
  7. const int INF = 1e18;
  8. int dp[N][N];
  9. int a[N][N];
  10.  
  11. signed main()
  12. {
  13.     int n , m;
  14.     cin >> n >> m;
  15.     for(int i = 1; i <= n; i++){
  16.         for(int j = 1; j <= m; j++){
  17.             cin >> a[i][j];
  18.         }
  19.     }
  20.     for(int i = 0; i <= n; i++){
  21.         for(int j = 0; j <= m; j++){
  22.             dp[i][j] = INF;
  23.         }
  24.     }
  25.     for(int i = 1; i <= n; i++){
  26.         for(int j = 1; j <= m; j++){
  27.             if((i == 1) && (j == 1)){
  28.                 dp[i][j] = a[i][j];
  29.                 continue;
  30.             }
  31.             dp[i][j] = min(dp[i - 1][j] , dp[i][j - 1]) + a[i][j];
  32.         }
  33.     }
  34.     cout << dp[n][m] << endl;
  35. }
  36.  
Add Comment
Please, Sign In to add comment