import Foundationvar n Int = 0var ans Int = 0func calculate let input = readLine!splitseparator map Int$0! let w Int = input0n let a Int = inputn2n var dp Int = w for i in 0n
The error message indicates that there is an issue with the way the input array is being sliced.
To fix this, you can convert the ArraySlice to an Array by using the Array constructor.
Here's the modified code:
import Foundation
var n: Int = 0
var ans: Int = 0
func calculate() {
let input = readLine()!.split(separator: " ").map{ Int($0)! }
let w: [Int] = Array(input[0..<n])
let a: [Int] = Array(input[n..<2*n])
var dp: [Int] = w
for i in 0..<n {
for j in 0..<i {
if (i - j) % a[j] == 0 {
dp[i] = max(w[i] + dp[j], dp[i])
}
}
ans = max(ans, dp[i])
}
print(ans)
}
func main() {
n = Int(readLine()!)!
calculate()
}
main()
This should resolve the error and allow the code to run without issues
原文地址: https://www.cveoy.top/t/topic/ikWY 著作权归作者所有。请勿转载和采集!