Mahmoud_Hawara

Untitled

Apr 24th, 2022
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #pragma GCC optimize("-Ofast")
  4. #pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx2,tune=native")
  5. #pragma GCC optimize("-ffast-math")
  6. #pragma GCC optimize("-funroll-loops")
  7. #pragma GCC optimize("-funroll-all-loops,-fpeel-loops,-funswitch-loops")
  8. #define IO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  9. const long long MOD = 1e9 + 7, OO = 1e18;
  10. const double PI = acos(-1);
  11. const int N = 1e2 + 5;
  12. const int dx[4] = {0, 0, 1, -1};
  13. const int dy[4] = {1, -1, 0, 0};
  14.  
  15. long long t, n, x, a[N][N], mem[N][N];
  16.  
  17. long long maxSum(long long i, long long j) {
  18.     if (i == n + 1)return 0;
  19.  
  20.     long long &ret = mem[i][j];
  21.     if (~ret)return ret;
  22.  
  23.     long long c1 = maxSum(i + 1, j);
  24.     long long c2 = maxSum(i + 1, j + 1);
  25.  
  26.     return ret = a[i][j] + max(c1, c2);
  27. }
  28.  
  29. int main() {
  30.     scanf("%lld", &t);
  31.     while (t--) {
  32.         memset(mem, -1, sizeof mem);
  33.         scanf("%lld", &n);
  34.         for (int i = 1; i <= n; i++)
  35.             for (int j = 1; j <= i; j++)scanf("%lld", &a[i][j]);
  36.         printf("%lld\n", maxSum(1LL, 1LL));
  37.     }
  38.     return 0;
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment