<p>You are given a <strong>0-indexed</strong> array <code>nums</code> and an integer <code>target</code>.</p>

<p>A <strong>0-indexed</strong> array <code>infinite_nums</code> is generated by infinitely appending the elements of <code>nums</code> to itself.</p>

<p>Return <em>the length of the <strong>shortest</strong> subarray of the array </em><code>infinite_nums</code><em> with a sum equal to </em><code>target</code><em>.</em> If there is no such subarray return <code>-1</code>.</p>

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

<pre>
<strong>Input:</strong> nums = [1,2,3], target = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> In this example infinite_nums = [1,2,3,1,2,3,1,2,...].
The subarray in the range [1,2], has the sum equal to target = 5 and length = 2.
It can be proven that 2 is the shortest length of a subarray with sum equal to target = 5.
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,1,1,2,3], target = 4
<strong>Output:</strong> 2
<strong>Explanation:</strong> In this example infinite_nums = [1,1,1,2,3,1,1,1,2,3,1,1,...].
The subarray in the range [4,5], has the sum equal to target = 4 and length = 2.
It can be proven that 2 is the shortest length of a subarray with sum equal to target = 4.
</pre>

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

<pre>
<strong>Input:</strong> nums = [2,4,6,8], target = 3
<strong>Output:</strong> -1
<strong>Explanation:</strong> In this example infinite_nums = [2,4,6,8,2,4,6,8,...].
It can be proven that there is no subarray with sum equal to target = 3.
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= target &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def minSizeSubarray(self, nums: List[int], target: int) -> int:

        REV = lambda x: tuple(reversed(tuple(x)))
        SUM = lambda x: (0,) + tuple(accumulate(tuple(x)))

        prefix_sum = SUM(nums)
        rev_suffix = SUM(REV(nums))
        suffix_sum = REV(rev_suffix)
        # print(f'{prefix_sum=}')
        # print(f'{rev_suffix=}')
        # print(f'{suffix_sum=}')

        ArraySum = prefix_sum[-1]
        answers = []
        if target <= ArraySum:
            print(f'Subarray')
            for i in range(len(nums) + 1):
                PrefixJ = prefix_sum[i] + target
                j = bisect_left(prefix_sum, PrefixJ)
                if j >= len(prefix_sum):
                    # print(f'[{i}] {prefix_sum[i]} ({PrefixJ}) -> [{j}] (OOB) not found')
                    continue
                if (prefix_sum[j] != PrefixJ):
                    # print(f'[{i}] {prefix_sum[i]} ({PrefixJ}) -> [{j}] {prefix_sum[j]} not found')
                    continue

                Sum = prefix_sum[j] - prefix_sum[i]
                # print(f'[{i}..{j}] {Sum}')
                if Sum == target:
                    print(f'  Found at [{i}:{j}]')
                    answers.append(j - i)
                else:
                    print(f'  ERROR: {Sum=} did not add up here')
                    return -555

        # no, ALWAYS do this, because "k" can be zero
        print(f'Multiple')
        # here, i can't be len(nums) and j can't be 0,
        # but they CAN overlap
        for i in range(len(nums)):
            targetK0 = target % ArraySum
            targetK1 = targetK0 + ArraySum
            # print(f'{i}: seek {targetK0},{targetK1}')
            for targetKn in [targetK0, targetK1]:
                SuffixJ = targetKn - prefix_sum[i]
                # print(f'  Checking {SuffixJ} = {targetKn} - {prefix_sum[i]}')
                j_reversed = bisect_left(rev_suffix, SuffixJ)
                j = len(suffix_sum) - j_reversed - 1
                if j >= len(suffix_sum):
                    # print(f'[{i}] {prefix_sum[i]} ({SuffixJ}) -> [{j}] (OOB) not found')
                    continue
                if (suffix_sum[j] != SuffixJ):
                    # print(f'[{i}] {prefix_sum[i]} ({SuffixJ}) -> [{j}] {suffix_sum[j]} not found')
                    continue
                # print(f'  {j_reversed=} -> {j=}')
                Sum = prefix_sum[i] + suffix_sum[j]
                # print(f'[..{i}]+[{j}..] {Sum}')
                if (target - Sum) % ArraySum == 0:
                    k = (target - Sum) // ArraySum
                    checkSum = prefix_sum[i] + (k * ArraySum) + suffix_sum[j]
                    if checkSum != target:
                        print(f'  FAIL: checksum does not match!')
                        print(f'{k} = ({target} - {Sum}) // {ArraySum}')
                        print(f'{checkSum} = {prefix_sum[i]} + ({k} * {ArraySum}) + {suffix_sum[j]}')
                        return -777
                    print(f'  Found at [0..{i}] + [{j}..{len(nums)}] + {k} * array')
                    # print(f'  DEBUG {nums[:i]} + {nums[j:]} + {k} * {ArraySum}')
                    # print(f'  DEBUG {sum(nums[:i])} + {sum(nums[j:])} + {k * ArraySum}')
                    count = i + (k * len(nums)) + (len(nums) - j)
                    print(f'    {count=} {i} + {len(nums) - j} + {k} * {len(nums)}')
                    answers.append(count)
                else:
                    print(f'  ERROR: {Sum=} did not add up here')
                    return -444

        if not answers:
            print(f'  Not found')
            return -1

        print(f'{answers=}')
        return min(answers)

34500