from typing import List


class Solution:
    def threeSumClosest(self, nums: List[int], target: int) -> int:
        res_nums = 65535
        nums.sort()
        res_nums = [nums[0], nums[1], nums[2]]
        for index, num in enumerate(nums):
            if index == 0 or index == len(nums) - 1:
                continue
            i = 0
            j = len(nums) - 1
            flag = 0
            cmp = target-sum(res_nums)
            while i < index < j:
                cmp_nums = [nums[i], num, nums[j]]
                temp = target - sum(cmp_nums)
                if abs(temp) < abs(cmp):
                    res_nums = cmp_nums
                    cmp = temp
                if temp < 0:
                    j -= 1
                elif temp > 0:
                    i += 1
                elif temp == 0:
                    flag = 1
                    break
                else:
                    break
            if flag == 1:
                break
        return sum(res_nums)


if __name__ == '__main__':
    parameters = [87,6,-100,-19,10,-8,-58,56,14,-1,-42,-45,-17,10,20,-4,13,-17,0,11,-44,65,74,-48,30,-91,13,-53,76,-69,-19,-69,16,78,-56,27,41,67,-79,-2,30,-13,-60,39,95,64,-12,45,-52,45,-44,73,97,100,-19,-16,-26,58,-61,53,70,1,-83,11,-35,-7,61,30,17,98,29,52,75,-73,-73,-23,-75,91,3,-57,91,50,42,74,-7,62,17,-91,55,94,-21,-36,73,19,-61,-82,73,1,-10,-40,11,54,-81,20,40,-29,96,89,57,10,-16,-34,-56,69,76,49,76,82,80,58,-47,12,17,77,-75,-24,11,-45,60,65,55,-89,49,-19,4]
    solution = Solution()
    res = solution.threeSumClosest(parameters, -275)
    print(res)