I submitted this recently and found out the algorithm is incorrect, but surprisingly, it got accepted! https://codeforces.net/contest/22/submission/253924838 This submission won't work for the following test: 5 7 11100000 11100000 00000000 00000000 11100000 11100000