https://codeforces.me/contest/1266/problem/D
Editorial of the problem does not explains how to implement the solution .It only tells what conditions will hold after every operation and at the end (when optimal answer has been reached).
I read few submissions and they used set to solve the problem , for example : https://codeforces.me/contest/1266/submission/67106641
I will be very thankful if someone helps me to understand the solution .