O(n): Sliding Window + Prefix/Suffix Sums + Suffix Min
Given an array of length n of positive numbers, our goal is to find two non-overlapping subarrays which:\n- both sum to a specified target value  T\n- whose sum
Given an array of length `n` of positive numbers, our goal is to find two non-overlapping subarrays which:\n- both sum to a specified target value  `T`\n- whose sum of individual lengths are minimized\n\n**FINDING SUBARRAYS: SLIDING WINDOW + PREFIX/SUFFIX SUMS**\nFor each index `i` of our array, we consider the following:\n* The shortest subarray length ending at index `i` that sums to target `T`, denoted `end(i)`\n* The shortest subarray length starting at index `i` that sums to target `T`, denoted `start(i)`\n\nTo compute `end(i)` for each ending index `i`, we can leverage prefix sums and a queue of already encountered indices as follows:\n\n* `prefix_sum(i) < T`: no subarray ending at `i` sums to `T` (since all entries are positive)\n* `prefix_sum(i) = T`: The subarray from index 0 to `i` of length `i+1` is the unique one that ends at index `i` and sums to `T`\n* `prefix_sum(i) > T`: Start going from the front of our queue of previously handled end indexes, and pop away anything for which the difference of prefix_sums between index `i` and that top index exceeds `T`. We know that we no longer have to bother checking that prior index, when we go to later/greater values `i`, since our prefix sum is monotonic increasing function (all values in the original array are positive), so if `prefix_sum(i) - prefix_sum(q(0)) > T`, then necessarily `prefix_sum(i+1) - prefix_sum(q(0)) > prefix_sum(i) - prefix_sum(q(0)) > T`. If we arrive finally at a difference in prefix sums that equals our target `T`, we\'ve found a valid subarray ending at index `i`.\n\nWe can do the same thing for `start(i)`, but in this case, we are looking at differences of suffix sums (i.e. iterating from the back of the array, but still leveraging this sliding window/queue approach).\n\n**PUTTING IT ALL TOGETHER: SUFFIX MIN**\nSo we now have a way of getting the length of subarrays that start/end at each index and sum to our target `T`. It follows now that for each possible index `i`, we need to determine the sum of lengths of:\n\n* the subarray ending at index `i` which sums to `T`\n* the mimimum length of a subarray starting after index `i` which sums to `T`\n\nOr to put in more succinct notation:\n```\nend(i) + min_{j > i} {start(j)}\n```\nOur final answer wlll then just be the global min over all possible such first-end indices `i`:\n```\nmin_{i = 0, ..., n-1} {end(i) + min_{j > i} {start(j)}}\n```\n\nIn order to avoid performing the `min_{j > i} start(j)` calculation `n` different times, we can leverage a suffix min here, given by the recursive formula:\n```\nsuffix_min(n-1) = min{infty, start(n-1)}\nsuffix_min(j) = min(suffix_min(j+1), start(j)}  j = n-2, ..., 1, 0\n```\nand thus this last quantity involving nested mins can be done in linear time.\n\n**CODE**\n```\nclass Solution:\n    def minSumOfLengths(self, arr: List[int], target: int) -> int:\n        n = len(arr)\n        \n        # STEP 1: For each possible index, find the minimum length subarray ending at that index which\n        # sums to our desired target (infinity if no such subarray exists)\n        prefix_sum = [0] * n\n        min_len_ending_at = [float("inf")] * n\n        q = []\n        for i in range(n):\n            prefix_sum[i] = prefix_sum[i-1] + arr[i]\n            if prefix_sum[i] == target:\n                min_len_ending_at[i] = i + 1\n            else:\n                while q and prefix_sum[i] - prefix_sum[q[0]] > target:\n                    q.pop(0)\n                if len(q) > 0 and prefix_sum[i] - prefix_sum[q[0]] == target:\n                    min_len_ending_at[i] = i - q[0]\n            q.append(i)\n        \n        # STEP 2: For each possible index, find the minimum length subarray starting at that index which\n        # sums to our desired target (infinity if no such subarray exists)\n        suffix_sum = [0] * n\n        min_len_starting_at = [float("inf")] * n\n        q = []\n        for i in range(n-1, -1, -1):\n            suffix_sum[i] = suffix_sum[(i+1) % n] + arr[i]\n            if suffix_sum[i] == target:\n                min_len_starting_at[i] = n - i\n            else:\n                while len(q) > 0 and suffix_sum[i] - suffix_sum[q[0]] > target:\n                    q.pop(0)\n                if len(q) > 0 and suffix_sum[i] - suffix_sum[q[0]] == target:\n                    min_len_starting_at[i] = q[0] - i\n            q.append(i)\n        \n        # STEP 3: For each possible index, determine the minimum length of any subarray starting at or after\n        # this index, which sums to our desired target\n        suffix_min_len_after = [float(inf)] * n\n        for i in range(n-1, -1, -1):\n            suffix_min_len_after[i] = min(suffix_min_len_after[(i+1) % n], min_len_starting_at[i])\n        \n        # STEP 4: For each possible end index, determine the sum of the:\n        # - length of the subarray ending at the index which sums to target (infinity if no such subarray exists)\n        # - length of shortest subarray starting AFTER this end index, which also sums to our target\n        # and compare to current smallest such sum of interval lengths.\n        res = float("inf")\n        for i in range(n-1):\n            res = min(res, min_len_ending_at[i] + suffix_min_len_after[i+1])\n        \n        if res == float("inf"):\n            return -1\n        return res\n```
[]