Python | Sorting + Hashmap | O( (M+N) log (M+N) ) , O(M+N)
\nclass Solution:\n    def maxSum(self, nums1: List[int], nums2: List[int]) -> int:\n        order = [(elem, i, 0) for (i,elem) in enumerate(nums1)] + [(elem, i
```\nclass Solution:\n    def maxSum(self, nums1: List[int], nums2: List[int]) -> int:\n        order = [(elem, i, 0) for (i,elem) in enumerate(nums1)] + [(elem, i, 1) for (i,elem) in enumerate(nums2)]\n        order.sort()\n\n        sets = [ set(nums1), set(nums2)]\n        arrays = [ nums1, nums2]\n\n        nums1.append(0)\n        nums2.append(0)\n\n        res = 0\n\n        while order:\n            elem, self_id, arr_choice = order.pop()\n            \n            self_arr = arrays[arr_choice]\n            other_arr = arrays[arr_choice^1]\n\n            max_value = elem + self_arr[self_id + 1]\n\n            if elem in sets[arr_choice^1]:\n                _, other_id, _ = order.pop()\n                max_value = max(max_value, elem + other_arr[other_id+1])\n\n                other_arr[other_id] = max_value\n\n            self_arr[self_id] = max_value\n\n            res = max(res, max_value)\n\n        return res % (10**9 + 7)\n\n```
[]