fork download
  1. def fn():
  2.  
  3. n = int(input())
  4.  
  5. Tri = [[0 for _ in range(n+1)] for _ in range(n+1)]
  6.  
  7. c = [[0 for _ in range(n+1)] for _ in range(n+1)]
  8.  
  9. road = [[0 for _ in range(n+1)] for _ in range(n+1)]
  10.  
  11. for i in range(1,n+1):
  12.  
  13. for j in range(1,i+1):
  14.  
  15. Tri[i][j] = int(input())
  16.  
  17. for i in range(1,n+1):
  18.  
  19. for j in range(1,i+1):
  20.  
  21. print(Tri[i][j], end = " ")
  22.  
  23. print()
  24.  
  25. for j in range(1,n+1):
  26. c[n][j] = Tri[n][j]
  27.  
  28. for i in range(n - 1, 0, -1):
  29.  
  30. for j in range(1, i + 1):
  31.  
  32. if c[ i + 1 ][ j ] > c[ i + 1 ][ j + 1 ]:
  33.  
  34. c[ i ][ j ] = Tri[ i ][ j ] + c[ i + 1 ][ j ]
  35.  
  36. road[ i ][ j ] = j
  37.  
  38. else:
  39.  
  40. c[ i ][ j ] = Tri[ i ][ j ] + c[ i + 1 ][ j + 1 ]
  41.  
  42. road[ i ][ j ] = j + 1
  43.  
  44. print(c[1][1])
  45.  
  46. i = j = 1
  47.  
  48. while i <= n:
  49.  
  50. print( Tri[ i ][ j ], end = " " )
  51.  
  52. j = road[ i ][ j ]
  53.  
  54. i += 1
  55. fn()
  56.  
Success #stdin #stdout 0.03s 9768KB
stdin
3
1
2
3
4
5
6
stdout
1 
2 3 
4 5 6 
10
1 3 6