#include #include using namespace std; template class BIT { int N; vector dat; public: explicit BIT(int n) : N(n+2), dat(N) {} void add(int k, T x) { for(int i=k+1; i tree(MAX_W); int cnt; for(int i=0; i 0) { cnt = tree.sum(MAX_W) - tree.sum(wi); if(cnt < K) { tree.add(wi, 1); } } else { cnt = tree.sum(-wi+1) - tree.sum(-wi); if(cnt) { tree.add(-wi, -1); } } } int res = tree.sum(MAX_W); printf("%d\n", res); return 0; }