#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; class BinaryIndexedTree { int n; vector data; public: BinaryIndexedTree(int n){ // コンストラクタ this->n = n; data.assign(n+1, 0); } void add(int k, int x){ // k番目の要素にxを加算する ++ k; while(k <= n){ data[k] += x; k += k & -k; } } int sum(int k){ // 区間[0,k]の総和を返す ++ k; int ret = 0; while(k > 0){ ret += data[k]; k -= k & -k; } return ret; } int sum(int a, int b){ // 区間[a,b]の総和を返す return sum(b) - sum(a-1); } }; int main() { int n, k; cin >> n >> k; multiset ms; BinaryIndexedTree b(1000001); while(--n >= 0){ int w; cin >> w; if(w > 0){ if(b.sum(w, 1000000) < k){ ms.insert(w); b.add(w, 1); } } else{ auto it = ms.find(-w); if(it != ms.end()){ ms.erase(it); b.add(-w, -1); } } } cout << ms.size() << endl; return 0; }