Sunjaree

(DP)Sums in a Triangle Problem Code: SUMTRIAN

Jan 30th, 2021 (edited)
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using  namespace  std;
  3.  
  4. #define endl         "\n"
  5. #define ll           long long
  6. #define PI           acos(-1.0)
  7. #define GCD(a,b)     __gcd(a , b)
  8. #define LCM(a,b)     ((a/__gcd(a,b))*b)
  9. #define READ(f)      freopen(f,"r",stdin)
  10. #define WRITE(f)     freopen(f,"w",stdout)
  11. #define test         cout<<"\n*************\n"
  12. #define mem(arr,val) memset(arr,val,sizeof(arr))
  13. #define precise(c)   fixed(cout);cout<<setprecision(c)
  14. #define valid(x,y)   (x>=1 && x<=row && y>=1 && y<=column)
  15. #define fast         ios_base :: sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
  16.  
  17.  
  18.  
  19. int main(){
  20.  
  21.  
  22.     ll t;
  23.     cin>>t;
  24.  
  25.     while(t--){
  26.  
  27.         ll r;
  28.         cin>>r;
  29.  
  30.         ll arr[r+5][r+5];
  31.         for(int i=1;i<=r;i++){
  32.             for(int j=1;j<=i;j++){
  33.                 cin>>arr[i][j];
  34.             }
  35.         }
  36.  
  37.         for(int i=r;i>1;i--){
  38.             for(int j=1;j<i;j++){
  39.  
  40.                 arr[i-1][j] += max(arr[i][j],arr[i][j+1]);
  41.             }
  42.         }
  43.         cout<<arr[1][1]<<endl;
  44.  
  45.     }
  46.  
  47.     return 0;
  48. }
Add Comment
Please, Sign In to add comment