不翻题面2。

· · 题解

这里求的是 省了 多少钱,而不是 花了 多少钱,于是只需要贪心地取最大值即可。但是是多组数据,于是我们需要进行 降序排序,并 前缀和 处理即可。
伪代码(使用 \KaTeX 排版):

\mathbf{input}~n, q \\ \mathbf{input}~a_i \\ \mathbf{sort}~\mathbf{decrease}~a_i \\ \mathbf{for}~i\gets [1,n] \\ \qquad b_i \gets b_{i-1} + a_i \\ \mathbf{for}~i\gets [1,q] \\ \qquad \mathbf{input}~x,y \\ \qquad \mathbf{output}~ b_x - b_{y-x} \end{array}