So as i wrote in the title i have a problem at solving this problem. https://codeforces.com/problemset/problem/1385/B
It seems working but i just can not figure out how t