fixed input handling
probably need someone to review
1st version
2nd phase thinking
got a runtime error from judge, but okay locally, probably some issue
finally AC, boudary cases nightmare. worstcase is nlogn though