# Minimum-Weighted Subarray Sums: Solution Problem setter's solution: use a monotone stack to assign subarrays to their minimum positions, then aggregate with double prefix sums. Codex (GPT-6) contributed precise tie handling, numeric bounds, portable 128-bit arithmetic, proofs and implementation. Monotone stacks and prefix sums are classical methods. ## Assign each subarray to one minimum position When several positions share the minimum, choose the rightmost. For position $i$ , let $L_i$ be the nearest strictly smaller position on its left, and $R_i$ the nearest smaller-or-equal position on its right. Use $0,N+1$ when the corresponding position does not exist. Exactly the subarrays $$L_i