#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define mp make_pair #define mt make_tuple #define pb push_back #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair pii; const int INF = 1 << 29; const double EPS = 1e-9; const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1}; // 1-indexed /////////////////////////////////////////////////////////////////// template class BIT { private: int N; //要素数 vector data; public: BIT() {} BIT(int N) : N(N) { data.resize(N + 1); } void init(int N) { this->N = N; data.resize(N + 1); } Weight sum(int index) { // O(logN) sum [1, index] Weight res = 0; while (index > 0) { res += data[index]; index -= index & -index; } return res; } Weight sum(int left, int right) { // sum[left, right] return sum(right) - sum(left - 1); } void add(int index, Weight x) { while (index <= N) { data[index] += x; index += index & -index; } } Weight operator[](int index) { return sum(index) - sum(index - 1); } }; /////////////////////////////////////////////////////// int N, K; const int MAX_W = 1000010; int main() { cin >> N >> K; BIT bit; bit.init(MAX_W); for (int i = 0; i < N; i++) { int x; cin >> x; if (x >= 0) { int res = bit.sum(x, MAX_W); if (res >= K) { continue; } bit.add(x, 1); } else { x = -x; int res = bit[x]; if (res == 0) continue; bit.add(x, -1); } } cout << bit.sum(MAX_W) << endl; return 0; }