import Foundation func readInt() -> [Int] { return readLine()!.split(separator: " ").map { c in Int(c)! } } func main() { let line = readInt() let N = line[0] var X = line[1] var Y = line[2] var Z = line[3] var A = Heap(array: readInt(), sort: >) while var a = A.remove() { guard X+Y+Z > 0 else { print("No") return } if X > 0 { if a > 10000 { let need = (a+10000) / 10000 if need > X { a -= X*10000 X = 0 A.insert(a) } else { X = (X*10000 - a) / 10000 } } else { X -= 1 } } else if Y > 0 { if a > 5000 { let need = (a+5000) / 5000 if need > Y { a -= Y*5000 Y = 0 A.insert(a) } else { Y = (Y*5000 - a) / 5000 } } else { Y -= 1 } } else if Z > 0 { if a > 1000 { let need = (a+1000) / 1000 if need > Z { a -= Z*1000 Z = 0 A.insert(a) } else { Z = (Z*1000 - a) / 1000 } } else { Z -= 1 } } } print("Yes") } struct Heap { var nodes = [T]() private var orderCriteria: (T, T) -> Bool init(sort: @escaping (T, T) -> Bool) { self.orderCriteria = sort } init(array: [T], sort: @escaping (T, T) -> Bool) { self.orderCriteria = sort configureHeap(from: array) } private mutating func configureHeap(from array: [T]) { nodes = array for i in stride(from: (nodes.count/2-1), through: 0, by: -1) { shiftDown(i) } } var isEmpty: Bool { return nodes.isEmpty } var count: Int { return nodes.count } @inline(__always) internal func parentIndex(ofIndex i: Int) -> Int { return (i - 1) / 2 } @inline(__always) internal func leftChildIndex(ofIndex i: Int) -> Int { return 2*i + 1 } @inline(__always) internal func rightChildIndex(ofIndex i: Int) -> Int { return 2*i + 2 } func peek() -> T? { return nodes.first } mutating func insert(_ value: T) { nodes.append(value) shiftUp(nodes.count - 1) } mutating func insert(_ sequence: S) where S.Iterator.Element == T { for value in sequence { insert(value) } } mutating func replace(index i: Int, value: T) { guard i < nodes.count else { return } remove(at: i) insert(value) } @discardableResult mutating func remove() -> T? { guard !nodes.isEmpty else { return nil } if nodes.count == 1 { return nodes.removeLast() } else { let value = nodes[0] nodes[0] = nodes.removeLast() shiftDown(0) return value } } @discardableResult mutating func remove(at index: Int) -> T? { guard index < nodes.count else { return nil } let size = nodes.count - 1 if index != size { nodes.swapAt(index, size) shiftDown(from: index, until: size) shiftUp(index) } return nodes.removeLast() } internal mutating func shiftUp(_ index: Int) { var childIndex = index let child = nodes[childIndex] var parentIndex = self.parentIndex(ofIndex: childIndex) while childIndex > 0 && orderCriteria(child, nodes[parentIndex]) { nodes[childIndex] = nodes[parentIndex] childIndex = parentIndex parentIndex = self.parentIndex(ofIndex: childIndex) } nodes[childIndex] = child } internal mutating func shiftDown(from index: Int, until endIndex: Int) { let leftChildIndex = self.leftChildIndex(ofIndex: index) let rightChildIndex = leftChildIndex + 1 var first = index if leftChildIndex < endIndex && orderCriteria(nodes[leftChildIndex], nodes[first]) { first = leftChildIndex } if rightChildIndex < endIndex && orderCriteria(nodes[rightChildIndex], nodes[first]) { first = rightChildIndex } if first == index { return } nodes.swapAt(index, first) shiftDown(from: first, until: endIndex) } internal mutating func shiftDown(_ index: Int) { shiftDown(from: index, until: nodes.count) } } extension Heap where T: Equatable { func firstIndex(of node: T) -> Int? { return nodes.firstIndex(where: { $0 == node }) } @discardableResult mutating func remove(node: T) -> T? { if let index = firstIndex(of: node) { return remove(at: index) } return nil } } main()