#include #include #include #include #define repeat(i,n) for (int i = 0; (i) < (n); ++(i)) #define whole(f,x,...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x) using namespace std; template struct binary_indexed_tree { // on monoid vector data; T unit; function append; // associative template binary_indexed_tree(size_t n, T a_unit, F a_append) : data(n, a_unit), unit(a_unit), append(a_append) {} void point_append(size_t i, T w) { // data[i] += w for (size_t j = i+1; j <= data.size(); j += j & -j) data[j-1] = append(data[j-1], w); } int initial_range_concat(size_t i) { // sum [0, i) T acc = unit; for (size_t j = i; 0 < j; j -= j & -j) acc = append(acc, data[j-1]); return acc; } T point_get(size_t i) { return initial_range_concat(i+1) - initial_range_concat(i); } }; int main() { int n, k; cin >> n >> k; vector ws(n); repeat (i,n) cin >> ws[i]; int w_max = max(*whole(max_element, ws), - *whole(min_element, ws)); binary_indexed_tree bit(w_max+1, int(), plus()); for (int w : ws) { int i = w_max - abs(w); if (w > 0) { if (bit.initial_range_concat(i+1) < k) { bit.point_append(i, 1); } } else if (w < 0) { if (bit.point_get(i)) { bit.point_append(i, -1); } } } cout << bit.initial_range_concat(w_max) << endl; return 0; }