Advertisement
kadeyrov

Untitled

Mar 28th, 2021
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  less-1
  4. //
  5. //  Created by Kadir Kadyrov on 28.03.2021.
  6. //
  7.  
  8. #include <iostream>
  9. #include <cmath>
  10.  
  11. using namespace std;
  12.  
  13. int main() {
  14.     int n;
  15.     cin >> n;
  16.     int a[n][n], b[n][n];
  17.    
  18.     for (int i = 0; i < n; i++) {
  19.         for (int j = 0; j < n; j++) {
  20.             cin >> a[i][j];
  21.             b[i][j] = 0;
  22.         }
  23.     }
  24.    
  25.     b[0][0] = a[0][0];
  26.    
  27.     for (int i = 1; i < n; i++) {
  28.         b[0][i] = b[0][i - 1] + a[0][i];
  29.     }
  30.    
  31.     for (int i = 1; i < n; i++) {
  32.         b[i][0] = b[i - 1][0] + a[i][0];
  33.     }
  34.    
  35.     for (int i = 1; i < n; i++) {
  36.         for (int j = 1; j < n; j++) {
  37.             if (b[i - 1][j] > b[i][j - 1]) {
  38.                 b[i][j] = a[i][j] + b[i - 1][j];
  39.             } else {
  40.                 b[i][j] = a[i][j] + b[i][j - 1];
  41.             }
  42.         }
  43.     }
  44.    
  45.     cout << b[n - 1][n - 1] << endl;
  46. }
  47.  
  48. /*
  49.  4
  50.  2 3 -5 8
  51.  0 7 -10 6
  52.  -3 7 3 1
  53.  -10 4 2 5
  54.  */
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement