2025-09-09 21:27:46 +09:00

23 lines
718 B
Swift

if let input1 = readLine(), let NK = input1.split(separator: " ").compactMap({Int($0)}) as? [Int],
let N = NK.first, let K = NK.last
{
var items: [[Int]] = []
for _ in 0..<N {
if let input2 = readLine(), let rows = input2.split(separator: " ").compactMap({Int($0)}) as? [Int] {
items.append(rows)
}
}
var dp: [[Int]] = Array(repeating: Array(repeating: 0, count: K+1), count: N+1)
for n in 1...N {
for k in 1...K {
if items[n-1][0] <= k {
dp[n][k] = max(dp[n-1][k], dp[n-1][k-items[n-1][0]] + items[n-1][1])
}
else {
dp[n][k] = dp[n-1][k]
}
}
}
print(dp[N][K])
}