import sequtils,algorithm,math,tables import sets,intsets,queues,heapqueue,bitops,strutils template `min=`*(x,y) = x = min(x,y) proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "" .} proc scan(): int = while true: let k = getchar_unlocked() if k < '0': break result = 10 * result + k.ord - '0'.ord type Heap*[T] = object nodes: seq[T] compare: proc(x,y:T):int proc newHeap*[T](compare:proc(x,y:T):int): Heap[T] = Heap[T](nodes:newSeq[T](),compare:compare) proc compareNode[T](h:Heap[T],i,j:int):int = h.compare(h.nodes[i],h.nodes[j]) proc size*[T](h:Heap[T]):int = h.nodes.len() proc items*[T](h:Heap[T]):seq[T] = h.nodes proc top*[T](h:Heap[T]): T = h.nodes[0] proc push*[T](h:var Heap[T],node:T):void = h.nodes.add(node) #末尾に追加 var i = h.nodes.len() - 1 while i > 0: # 末尾から木を整形 let parent = (i - 1) div 2 if h.compare(h.nodes[parent],node) <= 0: break h.nodes[i] = h.nodes[parent] i = parent h.nodes[i] = node proc shiftdown*[T](h:var Heap[T]): void = 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.compareNode(R,L) <= 0 : R else: L if h.compareNode(i,child) <= 0: break swap(h.nodes[i],h.nodes[child]) i = child proc pop*[T](h:var Heap[T]):T = result = h.nodes[0] # rootと末尾を入れ替えて木を整形 h.nodes[0] = h.nodes[^1] h.nodes.setLen(h.nodes.len() - 1) h.shiftdown() proc replaceTop*[T](h:var Heap[T],node:T):void = h.nodes[0] = node h.shiftdown() let n = scan() let A = newSeqWith(n,scan()) # me let B = newSeqWith(n,scan()) # enemy # 一番レベルの低い 一番戦ってないものを戦わせる 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 res = 1e12.int for i in 0.. mx : mx = me.cnt if res <= mx: break res .min= mx echo res