When swapping two elements $a_l$ and $a_r$, if $a_r > a_l$, the prefix sum increases in a certain range and remains unchanged elsewhere. In other words, you simply need to sort the unlocked elements in descending order.
- No change in prefix sum in the range $0 \sim l-1$
- Prefix sum increases by $+(a_r-a_l)$ in the range $l \sim r-1$
- No change in prefix sum in the range $r \sim n-1$
