fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6. int N;
  7.  
  8. int solution(vector <vector <int>> t, vector <vector <int>>& c, int x, int y)
  9. {
  10. if (y == N - 1) return t[y][x];
  11. if (c[y][x] != 0) return c[y][x];
  12. int result = t[y][x] + max(solution(t, c, x, y + 1), solution(t, c, x + 1, y + 1));
  13. c[y][x] = result;
  14. return result;
  15. }
  16.  
  17. int main(void)
  18. {
  19. int C;
  20. cin >> C;
  21.  
  22. while (C--)
  23. {
  24. cin >> N;
  25.  
  26. vector <vector <int>> tri;
  27. vector <vector <int>> check(N, vector<int>(N, 0));
  28.  
  29. for (int i = 0; i < N; i++)
  30. {
  31. vector <int> temp;
  32. for (int j = 0; j <= i; j++)
  33. {
  34. int input;
  35. cin >> input;
  36. temp.push_back(input);
  37. }
  38. tri.push_back(temp);
  39. }
  40.  
  41. cout << solution(tri, check, 0, 0) << endl;
  42. }
  43. }
Success #stdin #stdout 0.01s 5616KB
stdin
2
5
6
1  2
3  7  4
9  4  1  7
2  7  5  9  4
5
1 
2 4
8 16 8
32 64 32 64
128 256 128 256 128
stdout
28
341