class Proc { let blackBlock:[Int] let whiteBlock:[Int] init() { _ = readLine()! self.whiteBlock = readLine()!.split(separator: " ").map{ Int($0)!}.sorted() _ = readLine()! self.blackBlock = readLine()!.split(separator: " ").map{ Int($0)! }.sorted() } public func doProc() { var ans = GetAns(idx: blackBlock.count - 1, isBlack: true) ans = max(ans, GetAns(idx: whiteBlock.count-1, isBlack: false)) print(ans) } private func GetAns(idx:Int, isBlack:Bool) -> Int { let currentLen = isBlack ? self.blackBlock[idx] : self.whiteBlock[idx] var next = -1 if isBlack { for i in 0.. currentLen { break } next = i } } else { for i in 0.. currentLen { break } next = i } } if next < 0 { return 1 } let ans = GetAns(idx: next, isBlack: !isBlack) + 1 return ans } } Proc().doProc()