<p>Given an integer array <code>nums</code>, return <em>the <strong>maximum possible sum </strong>of elements of the array such that it is divisible by three</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,6,5,1,8]
<strong>Output:</strong> 18
<strong>Explanation:</strong> Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3).</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [4]
<strong>Output:</strong> 0
<strong>Explanation:</strong> Since 4 is not divisible by 3, do not pick any number.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,4,4]
<strong>Output:</strong> 12
<strong>Explanation:</strong> Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 4 * 10<sup>4</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
</ul>

class Solution:
    def maxSumDivThree(self, nums: List[int]) -> int:
        nums.sort()
        sum1 = sum(nums)
        index = 0
        mod_1_1 = 0
        mod_1_2 = 0
        mod_2_1 = 0
        mod_2_2 = 0
        sub_1_1 = 0
        sub_1_2 = 0
        sub_2_1 = 0
        sub_2_2 = 0
        sub = 0
        while index < len(nums) and sum1%3 != 0 and ((sum1%3 == 1 and (mod_1_1 < 1 or mod_1_2 < 4)) or (sum1%3 == 2 and (mod_2_1 < 2 or mod_2_2 < 2))):
            if sum1%3 == 1:
                if mod_1_1 < 1 and nums[index]%3 == 1:
                    mod_1_1 = 1
                    sub_1_1 = nums[index]
                elif mod_1_2 < 4 and nums[index]%3 == 2:
                    mod_1_2 += 2
                    sub_1_2 += nums[index]
            else:
                if mod_2_1 < 2 and nums[index]%3 == 1:
                    sub_2_1 += nums[index]
                    mod_2_1 += 1
                elif mod_2_2 < 2 and nums[index]%3 == 2:
                    sub_2_2 = nums[index]
                    mod_2_2 = 2
            index += 1
        if sum1%3 == 1:
            if mod_1_1 == 1:
                if mod_1_2 == 4:
                    sub = min(sub_1_1, sub_1_2)
                else:
                    sub = sub_1_1
            else:
                sub = sub_1_2 #are we assuming there will always be and answer?
        else:
            if mod_2_1 == 2:
                if mod_2_2 == 2:
                    sub = min(sub_2_1, sub_2_2)
                else:
                    sub = sub_2_1
            else:
                sub = sub_2_2 #are we assuming there will always be an answer?
        print(sub_1_1, sub_1_2, sub_2_1, sub_2_2)
        return sum1 - sub
        
            
        # ret = 0
        # ones = 0
        # twos = 0
        # for i in nums:
        #     if i%3 == 1:
        #         ones += 1
        #     elif i%3 == 2:
        #         twos += 1
        # ones = min(ones,twos)
        # twos = ones
        # print(ones)
        # for i in reversed(range(len(nums))):
        #     print(nums[i])
        #     if nums[i]%3 == 0:
        #         ret += nums[i]
        #     elif nums[i]%3 == 2 and twos > 0:
        #         ret += nums[i]
        #         twos -= 1
        #     elif nums[i]%3 == 1 and ones > 0:
        #         ret += nums[i]
        #         ones -= 1
        #     # print(ret)
        # return ret

            
            

            
22000