Solution
Code\n\nfrom sortedcontainers import SortedList\nclass Solution:\n    def resultArray(self, nums: List[int]) -> List[int]:\n        def gc(n):\n            if l
# Code\n```\nfrom sortedcontainers import SortedList\nclass Solution:\n    def resultArray(self, nums: List[int]) -> List[int]:\n        def gc(n):\n            if len(sl1)-sl1.bisect_right(n) == len(sl2)-sl2.bisect_right(n):\n                return len(sl1)<=len(sl2)\n            return len(sl1)-sl1.bisect_right(n) > len(sl2)-sl2.bisect_right(n)\n        sl1 = SortedList([nums[0]])\n        sl2 = SortedList([nums[1]])\n        arr1 = [nums[0]]\n        arr2 = [nums[1]]\n        for i in range(2, len(nums)):\n            if gc(nums[i]):\n                sl1.add(nums[i])\n                arr1.append(nums[i])\n            else:\n                sl2.add(nums[i])\n                arr2.append(nums[i])\n        return arr1+arr2\n```
['Python3']