class Set(): #BinaryIndexedTree def __init__(self, valuelist, INF=10**18+1): self.n = len(valuelist) + 1 self.value = sorted(valuelist) + [INF] self.comp = {v : k for k, v in enumerate(self.value, 1)} self.bit = BinaryIndexedTree(self.n) def insert(self, x, k=1): self.bit.add(self.comp[x], k) def delete(self, x, k=1): self.bit.add(self.comp[x], -k) def count(self, x): return self.bit.get(self.comp[x]) def sum(self, x): return self.bit.sum(self.comp[x]) def get(self, k): #k個以下のときはINFを返す return self.value[self.bit.bisect_left(k) - 1] class BinaryIndexedTree(): #1-indexed def __init__(self, n): self.n = n self.tree = [0 for _ in range(n + 1)] def sum(self, idx): res = 0 while idx: res += self.tree[idx] idx -= idx & -idx return res def get(self, idx): return self.sum(idx) - self.sum(idx - 1) def add(self, idx, x): while idx <= self.n: self.tree[idx] += x idx += idx & -idx def bisect_left(self, x): if x <= 0: return 0 res, tmp = 0, 2**((self.n).bit_length() - 1) while tmp: if res + tmp <= self.n and self.tree[res + tmp] < x: x -= self.tree[res + tmp] res += tmp tmp >>= 1 if res >= self.n: return self.n return res + 1 INF = 10**18 + 1 N, K = map(int, input().split()) S = Set(range(1000001)) for _ in range(N): w = int(input()) if w > 0: if S.sum(INF) - S.sum(w) + S.count(w) < K: S.insert(w) else: if S.count(-w) > 0: S.delete(-w) print(S.sum(INF))