fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main()
  4. {
  5. unsigned h,w, max;
  6. scanf("%u%u",&h,&w);
  7. unsigned (*buf)[w+2] = malloc(sizeof(unsigned)*h*(w+2));
  8. for (unsigned j = 0; j < h; j++) {
  9. buf[j][0] = buf[j][w+1] = 0;
  10. for (unsigned i = 1; i <= w; i++){
  11. scanf("%d",&buf[j][i]);
  12. }
  13. }
  14. /*/
  15.   for (unsigned j = 0; j < h; j++) {
  16. for (unsigned i = 0; i <= w+1;i++){
  17. printf("%d ",buf[j][i]);
  18. }
  19. printf("\n");
  20.   }
  21. */
  22. for (unsigned j = 1; j < h; j++) {
  23. for (unsigned x = 1; x <= w; x++) {
  24. unsigned *maxp = &buf[j-1][x];
  25. max = maxp[0] < maxp[-1] ? maxp[-1] : maxp[0];
  26. if (max < maxp[1]) max = maxp[1];
  27. buf[j][x] += max;
  28. }
  29. /*/
  30. for (unsigned i = 1; i <= w; i++){
  31. printf("%d ",buf[j][i]);
  32. }
  33. printf("\n");
  34.  
  35. */
  36. }
  37. max = buf[h-1][0];
  38. for (unsigned i = 1; i <= w; i++)
  39. if (max < buf[h-1][i]) max = buf[h-1][i];
  40. printf("%u\n",max);
  41. return 0;
  42. }
  43.  
Success #stdin #stdout 0s 5428KB
stdin
15 15
75  0  0  0  0  0  0  0  0  0  0  0  0  0  0
95 64  0  0  0  0  0  0  0  0  0  0  0  0  0
17 47 82  0  0  0  0  0  0  0  0  0  0  0  0
18 35 87 10  0  0  0  0  0  0  0  0  0  0  0
20 04 82 47 65  0  0  0  0  0  0  0  0  0  0
19 01 23 75 03 34  0  0  0  0  0  0  0  0  0
88 02 77 73 07 63 67  0  0  0  0  0  0  0  0
99 65 04 28 06 16 70 92  0  0  0  0  0  0  0
41 41 26 56 56 83 40 80 80 70 33  0  0  0  0
41 48 72 33 47 32 37 16 94 29  0  0  0  0  0
53 71 44 65 25 25 43 91 52 97 51 14  0  0  0
70 11 33 28 77 73 17 78 39 68 17 57  0  0  0
91 71 52 38 17 17 14 91 43 58 50 27 29 48  0
63 66 04 68 89 53 67 30 73 16 69 87 40 31  0
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
stdout
1116