fork download
  1. #include <iostream>
  2. #include <string.h>
  3. using namespace std;
  4.  
  5. int max(int n1, int n2) {
  6. if (n1 > n2)
  7. return n1;
  8. else
  9. return n2;
  10. }
  11.  
  12. int main() {
  13.  
  14. std::ios::sync_with_stdio(false);
  15. int n;
  16. int dp[500][500];
  17.  
  18. for(int i=0;i<500; i++){
  19. memset(dp[i],0, sizeof(int) * 500);
  20. }
  21.  
  22. cin >> n;
  23. for (int i = 0; i < n;i++) {
  24. for (int j = 0; j <= i; j++) {
  25. cin >> dp[i][j];
  26. }
  27. }
  28.  
  29. for (int i = 0; i < n; i++) {
  30.  
  31. for (int j = 0; j <= i; j++) {
  32. if (i == 0 && j == 0) {
  33.  
  34. }
  35. else {
  36. dp[i][j] = dp[i][j] + max(dp[i - 1][j - 1], dp[i - 1][j]);
  37. }
  38. }
  39. }
  40.  
  41. int result = 0;
  42. for (int j = 0; j < n; j++) {
  43. result = max(dp[n - 1][j], result);
  44. }
  45. cout << result << endl;
  46. return 0;
  47. }
Success #stdin #stdout 0s 4552KB
stdin
2
0
1 2
stdout
3