시간 초과
import Foundation
let n = Int(readLine()!)!
let arr: [Int] = readLine()!.components(separatedBy: " ").map { Int($0)! }
var result: [Int] = []
for (i, e) in arr.enumerated() {
var flag: Bool = false
for j in (i+1)..<n {
if arr[j] > e {
result.append(arr[j])
flag = true
break
}
}
if !flag {
result.append(-1)
}
}
for num in result {
print(num, terminator: " ")
}
let c = Int(readLine()!)!
let input = readLine()!.split(separator: " ").map { Int(String($0))! }
var result = [String](repeating: "-1", count: c)
var tc = [Int]()
for i in 0..<c {
while !tc.isEmpty, input[tc.last!] < input[i] {
let index = tc.popLast()!
result[index] = "\(input[i])"
}
tc.append(i)
}
print(result.joined(separator: " "))