<p>Given a <strong>circular integer array</strong> <code>nums</code> of length <code>n</code>, return <em>the maximum possible sum of a non-empty <strong>subarray</strong> of </em><code>nums</code>.</p>

<p>A <strong>circular array</strong> means the end of the array connects to the beginning of the array. Formally, the next element of <code>nums[i]</code> is <code>nums[(i + 1) % n]</code> and the previous element of <code>nums[i]</code> is <code>nums[(i - 1 + n) % n]</code>.</p>

<p>A <strong>subarray</strong> may only include each element of the fixed buffer <code>nums</code> at most once. Formally, for a subarray <code>nums[i], nums[i + 1], ..., nums[j]</code>, there does not exist <code>i &lt;= k1</code>, <code>k2 &lt;= j</code> with <code>k1 % n == k2 % n</code>.</p>

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

<pre>
<strong>Input:</strong> nums = [1,-2,3,-2]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Subarray [3] has maximum sum 3.
</pre>

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

<pre>
<strong>Input:</strong> nums = [5,-3,5]
<strong>Output:</strong> 10
<strong>Explanation:</strong> Subarray [5,5] has maximum sum 5 + 5 = 10.
</pre>

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

<pre>
<strong>Input:</strong> nums = [-3,-2,-3]
<strong>Output:</strong> -2
<strong>Explanation:</strong> Subarray [-2] has maximum sum -2.
</pre>

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

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

class Solution:
    def maxSubarraySumCircular(self, nums: List[int]) -> int:
        i = 0
        while i < len(nums):
            if nums[i] == 0:
                nums.pop(i)
            else:
                i += 1

        pos = 0
        neg = 0
        for num in nums:
            if num > 0:
                pos += 1
            else:
                neg += 1
        if pos == len(nums):
            return sum(nums)
        if neg == len(nums):
            return max(nums)

        while (nums[0] < 0 and nums[-1] < 0) or (nums[0] > 0 and nums[-1] > 0):
            nums.append(nums.pop(0))

        i = 1
        while i < len(nums):
            if (nums[i] < 0 and nums[i-1] < 0) or (nums[i] > 0 and nums[i-1] > 0):
                nums[i] += nums[i-1]
                nums.pop(i-1)
            else:
                i += 1
        if len(nums) == 2:
            return max(nums)

        n = len(nums)
        # print(nums)
        nums += nums[:-1]
        # print(nums)
        max_sum = 0
        current_sum = 0
        left, right = 0, 0
        while right < len(nums):
            # print('========')
            # print(f'left: {left}, right: {right}, current_sum: {current_sum}, max_sum: {max_sum}')
            if right - left == n:
                current_sum = 0
                left += 1
                right = left
                # print('limit reached')
                # current_sum -= nums[left]
                # max_sum = max(max_sum, current_sum)
                # left += 1
                # print(f'left: {left}, right: {right}, current_sum: {current_sum}, max_sum: {max_sum}')
                # print('----------')
                # while current_sum <= 0 and left < right:
                #     current_sum -= nums[left]
                #     left += 1

            if nums[right] > 0:
                current_sum += nums[right]
                max_sum = max(max_sum, current_sum)
                # print(f'left: {left}, right: {right}, current_sum: {current_sum}, max_sum: {max_sum}')
                right += 1
            elif current_sum + nums[right] >= 0:
                current_sum += nums[right]
                max_sum = max(max_sum, current_sum)
                # print(f'left: {left}, right: {right}, current_sum: {current_sum}, max_sum: {max_sum}')
                right += 1
            else:
                current_sum = 0
                # print(f'left: {left}, right: {right}, current_sum: {current_sum}, max_sum: {max_sum}')
                right += 1
                left = right

        return max_sum

# [7,-6,40,-22,1,-7,3]
20800