import Foundation
func solution(_ k:Int, _ dungeons:[[Int]]) -> Int {
var answer = 0
var visited = Array(repeating: false, count: dungeons.count)
func dfs(_ score: Int, _ count: Int) {
answer = max(answer, count)
for i in 0..<dungeons.count {
if !visited[i] && score >= dungeons[i][0] {
visited[i] = true
dfs(score - dungeons[i][1], count + 1)
visited[i] = false
}
}
}
for i in 0..<dungeons.count {
if k >= dungeons[i][0] {
visited[i] = true
dfs(k - dungeons[i][1], 1)
visited = Array(repeating: false, count: dungeons.count)
}
}
return answer
}