Your approach is correct. I didn't read the code that you submitted in contest, but I fixed the brute force implementation you submitted after. Your mistake was using std::set instead of std::multiset: https://codeforces.com/contest/2051/submission/297964342
Your approach is correct. I didn't read the code that you submitted in contest, but I fixed the brute force implementation you submitted after. Your mistake was using
std::setinstead ofstd::multiset: https://codeforces.com/contest/2051/submission/297964342