2025-08-30 21:20:47 +09:00

20 lines
582 B
Swift

if let n = Int(readLine() ?? "0"){
var nums:[[Int]] = Array(repeating: Array(repeating: 0 , count: n), count: n)
for i in 0..<n {
if let input = readLine(), let numsLine = input.split(separator: " ").compactMap({Int($0)}) as? [Int], numsLine.count == i+1 {
for j in 0...i {
nums[i][j] = numsLine[j]
}
}
}
if n != 1 {
for i in (0...(n-2)).reversed() {
for j in 0...i {
nums[i][j] += max(nums[i+1][j],nums[i+1][j+1])
}
}
}
print(nums[0][0])
}