Progress during contest
Turns out B-large can be solved easily

The brute force algorithm seems to work for the provided
input. Would not work if they used the worse case scenario
allowed by the large input limits.