import sequtils,strutils,strscans,algorithm,math,future,sets,queues,tables template get():string = stdin.readLine() template times(n:int,body:untyped): untyped = (for _ in 0.. 0: # 末尾から木を整形 let parent = (i - 1) div 2 if h.compare(h.nodes[parent],h.nodes[i]) <= 0: break swap(h.nodes[i],h.nodes[parent]) i = parent proc pop*[T](h:var Heap[T]):T = if h.size <= 0: raise newException(Exception,"heap is empty") result = h.nodes[0] # rootと末尾を入れ替えて木を整形 h.nodes[0] = h.nodes[^1] h.nodes.setLen(h.nodes.len() - 1) let size = h.nodes.len() var i = 0 while true : let L = i * 2 + 1 let R = i * 2 + 2 if L >= size : break let child = if R < size and h.compare(h.nodes[R],h.nodes[L]) <= 0 : R else: L if h.compare(h.nodes[i],h.nodes[child]) <= 0: break swap(h.nodes[i],h.nodes[child]) i = child let N = get().parseInt # ~1500 A = get().split().map(parseInt) # my lv B = get().split().map(parseInt) #enemy lv # 一番レベルの低い 一番戦ってないものを戦わせる type monster = tuple[lv:int,cnt:int] var myInitialHeap = newHeap( proc (a,b:monster):int = if a.lv == b.lv : a.cnt - b.cnt else: a.lv - b.lv ) for a in A: myInitialHeap.push((a,0)) var resSeq = newSeq[int]() for i in 0..a.cnt - b.cnt)[^1].cnt resSeq.add(count) echo resSeq.min