vector fenwick(4 * N), fenwickCount(4 * N); auto update = [&](int x) { int v = x; for(x = mp[x]; x < (N << 2) ; x += x & -x) fenwick[x] += v; for(x = mp[v] ; x < (N << 2) ; x += x & -x) fenwickCount[x] ++; }; auto query = [&](int topK) { int res = 0,tot = 0, sum = 0; for(int lg = (1 << 20) ; lg ; lg >>= 1) { if(res + lg < (N << 2) and tot + fenwickCount[res ^ lg] <= topK) { res ^= lg; tot += fenwickCount[res]; sum += fenwick[res]; } } return sum; };