<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>

#include <execution>

class Solution {
public:
    int maxSubarraySumCircular(vector<int>& nums) {
        int n = nums.size();

        if(n == 1) {
            return nums[0];
        } else if(n == 2) {
            return std::max({nums[0], nums[1], nums[0] + nums[1]});
        } else {
            std::vector<int> inclu_scan {};
            inclu_scan.reserve(n);
            std::inclusive_scan(std::execution::par_unseq, nums.begin(), nums.end(), std::back_inserter(inclu_scan));

            int S = std::reduce(std::execution::par_unseq, nums.begin(), nums.end());

            int result {std::numeric_limits<int>::lowest()};

            int max_sum {std::numeric_limits<int>::lowest()};
            int min_sum {std::numeric_limits<int>::max()};

            int min_left_value {};
            int max_left_value {inclu_scan[0]};

            for(int i = 0; i < n; ++i) {
                int delta = inclu_scan[i] - min_left_value;
                if(max_sum < delta) {
                    max_sum = delta;
                }

                min_left_value = std::min(min_left_value, inclu_scan[i]);

                if(i > 0 && i < n - 1) [[likely]] {
                    int d = inclu_scan[i] - max_left_value;
                    if(min_sum > d) {
                        min_sum = d;
                    }

                    max_left_value = std::max(max_left_value, inclu_scan[i]);
                }
            }

            result = std::max(max_sum, S - min_sum);

            return result;
        }
    }
};

const static int io = [](){
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);
    return 0;
}();
44400