Thanks guko san
Your code fails with tests like this for instance.
And because there are random tests, the number of tests your code fails is random too.
Your code is too slow and times out with large inputs. It also fails with tests like this:
This comment is hidden because it contains spoiler information about the solution