Python, 3 lines for fun!
Just for fun, condensed solution in 3 lines! It sometimes TLs, should run several times.\n\nBasically, solution is similar as everyone\'s priority queue observa
Just for fun, condensed solution in 3 lines! It sometimes TLs, should run several times.\n\nBasically, solution is similar as everyone\'s priority queue observation, Dijkstra logic. Except since you keep track of only 2000 nodes, you can manually append new possibilities to the `pq` list and sort it again.\n\nTime complexity: N * log N + K ^ 2 * log K\n\n```\nclass Solution:\n    def kSum(self, nums, k):\n        pos_sum, pos, pq = sum(t for t in nums if t > 0), sorted(abs(t) for t in nums), [(0, -1)]\n        for ct in range(k - 1): pq = sorted(pq[1:] + [(pq[0][0] + pos[i], i) for i in range(pq[0][1] + 1, min(pq[0][1] + k - ct, len(nums)))])[:k - ct]\n        return pos_sum - pq[0][0]\n```
[]