The given test cases are only a small sample. The real, hidden test cases will be much harder to pass. They’ll likely include corner and any tricky cases, as well as big cases at the limit of the constraints.
So passing the sample test cases is only the first step in getting accepted. The next step is to generate your own test data and test your solution against them. It may help to write a simple brute-force checker to validate the output from your solution.