> For the complete documentation index, see [llms.txt](https://trizen.gitbook.io/sidef-lang/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://trizen.gitbook.io/sidef-lang/programming_tasks/m/maximum_triangle_path_sum.md).

# Maximum triangle path sum

Iterative solution:

```ruby
var sum = [0]

ARGF.each {  |line|
    var x = line.words.map{.to_n}
    sum = [
            x.first + sum.first,
            {|i| x[i] + [sum[i-1, i]].max }.map(1 ..^ x.end)...
            x.last + sum.last,
          ]
}

say sum.max
```

Recursive solution:

```ruby
var triangle = ARGF.slurp.lines.map{.words.map{.to_n}}
 
func max_value(i=0, j=0) is cached {
    i == triangle.len && return 0
    triangle[i][j] + [max_value(i+1, j), max_value(i+1, j+1)].max
}
 
say max_value()
```

#### Output:

```
% sidef maxpath.sf triangle.txt
1320
```
