dp = [[], []]
n_max = 0
$<.each_line.with_index do |l, i|
  xs = l.split(?,).map(&:to_i)
  n_max = [n_max, xs.size].max
  k = n_max - xs.size
  xs.each_with_index do |x, j|
    j += k
    dp[(i+1)%2][j] = x
    dp[(i+1)%2][j] = [dp[(i+1)%2][j], x + dp[i%2][j]].max if dp[i%2][j]
    dp[(i+1)%2][j] = [dp[(i+1)%2][j], x + dp[i%2][j-1]].max if j > 0
  end
end
p dp[1][n_max-1]