human
Write me a Python code to find the maximum possible sum of elements of the array such that it is divisible by three given the following:
Solve in Python:
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.

 
Example 1:
Input: nums = [3,6,5,1,8]
Output: 18
Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3).
Example 2:
Input: nums = [4]
Output: 0
Explanation: Since 4 is not divisible by 3, do not pick any number.

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

 
Constraints:

1 <= nums.length <= 4 * 10^4
1 <= nums[i] <= 10^4
assistant
#Here is Python code to find the maximum possible sum of elements of the array such that it is divisible by three.

class Solution:
    def maxSumDivThree(self, nums: List[int]) -> int:
        
        n = len(nums)
        dp = [[0]*3 for _ in range(n)]
        
        for i, x in enumerate(nums):
            m = x % 3
            if i > 0:
                dp[i] = dp[i-1][::]
                # for j in range(3):
                #     if dp[i][(j-m)%3] > 0:
                #         dp[i][j] = max(dp[i][j], dp[i][(x-m)%3] + x)
                rot = [dp[i][(x-m)%3] for x in range(3)]

                for j in range(3):
                    if rot[j] > 0:
                        dp[i][j] = max(dp[i][j], rot[j] + x) # can add to it
                        
            dp[i][m] = max(dp[i][m], x) # better to start anew with current element
            
                
        print(dp)
        return dp[-1][0]