Hi everyone, I was able to think the solution for E. Cyclic Balance, but the $$$O(1)$$$ constraints in the editorial specifically $$$K \ge \lceil (L_0 + L_1 - c) / 3 \rceil$$$ felt like magic. I wanted to see if I could derive this exact formula from first principles without relying on combinatorial guessing. I hope this perspective helps someone else too.
1. The Matrix (Flow Conservation)
Instead of looking at the string as a 1D array of characters, let's view it as a directed walk on a graph with two nodes: State 0 and State 1. Because the string is cyclic, it forms a closed loop. By basic flow conservation, the number of times we enter a node must exactly equal the number of times we leave it. Therefore, the number of $$$01$$$ transitions must perfectly equal the number of $$$10$$$ transitions. Let’s call this count $$$c$$$. We can now map any cyclic binary string to a symmetric adjacency matrix $$$M$$$:



