import sequtils template times*(n:int,body:untyped): untyped = (for _ in 0.." .} proc scan(): int = while true: let k = getchar_unlocked() if k < '0': return result = 10 * result + k.ord - '0'.ord template useBinaryIndexedTree() = # 部分和検索 / 更新 O(log(N)) type BinaryIndexedTree[T] = ref object data: seq[T] # 引数以下の部分和(Fenwick Tree) proc initBinaryIndexedTree[T](n:int):BinaryIndexedTree[T] = new(result) result.data = newSeq[T](n) proc len[T](self:BinaryIndexedTree[T]): int = self.data.len() proc update[T](self:var BinaryIndexedTree[T],i:int,val:T) = var i = i while i < self.len(): self.data[i] += val i = i or (i + 1) proc `[]`[T](self:BinaryIndexedTree[T],i:int): T = var i = i while i >= 0: result += self.data[i] i = (i and (i + 1)) - 1 proc `$`[T](self:BinaryIndexedTree[T]): string = result = "[" for i in 0.. 100 : " ...]" else: "]") useBinaryIndexedTree() let n = scan() let k = scan() var cargo = initBinaryIndexedTree[int](n) var last = 0 n.times: let w = scan() if w > 0: if last - cargo[w-1] >= k: continue # W以上の荷物がK個以上 cargo.update w, 1 last += 1 else: if cargo[-w-1] == cargo[-w] : continue # 荷物が無い cargo.update -w,-1 last -= 1 echo last