有 \(n\) 件商品,第 \(i\) 件的价格是 \(p_i\) ,若买至少 \(x\) 件商品,则其中最便宜的 \(y\) 件免费。给 \(q\) 个询问,每次给出 \(x, y\) ,回答最多能免费的价值。
按大到小排序,求前缀和 \(S_{i}\) 表示买前 \(i\) 件商品需要的开销。
答案为 \(S_{x} - S_{x - y}\) 。
#include <bits/stdc++.h>
void solve() {
int n, q; std::cin >> n >> q;
std::vector<long long> a(n+1);
for (int i = 1; i <= n; i++) std::cin >> a[i];
std::sort(a.begin() + 1, a.end(), std::greater<long long>() );
for (int i = 1; i <= n; i++) a[i] += a[i - 1];
for (int i = 0; i < q; i++) {
int x, y; std::cin >> x >> y;
std::cout << a[x] - a[x - y] << '\n';
}
}
signed main() {
int _ = 1; //std::cin >> _;
while (_--) solve();
return 0;
}
- Educational Codeforces Round Rated Promoeducational codeforces round rated round codeforces rated based educational codeforces round 151 construction educational codeforces round educational codeforces round 147 cf-educational educational codeforces round educational codeforces round 158 educational codeforces contest round educational codeforces monsters round educational codeforces balance round