179 words
1 minute
B4104 [CSP-X2024 Shandong] Shopping - Solution

Problem Description
Original Problem has already stated the problem clearly, so no further explanation is needed here.
Approach
First, sort all products in descending order by price. Then, group them into sets of items. For each group, add to the answer the minimum value between the original total price of the products and the discounted price .
Note! The last group might be incomplete, so special handling is required.
Code
#include <iostream>#include <algorithm>#define ll long longusing namespace std;ll a[200005];bool cmp(ll x, ll y){ return x > y;}int main(){ ll n, m, w, ans = 0, cnt = 0; cin >> n >> m >> w; for(int i = 1; i <= n; i++) { cin >> a[i]; } sort(a + 1, a + n + 1, cmp); for(int i = 1; i <= n; i++) { cnt += a[i]; if(i % m == 0) { ans += min(cnt, w); cnt = 0; } } if(cnt >= w) { ans += w; } else { ans += cnt; } cout << ans; return 0;}
B4104 [CSP-X2024 Shandong] Shopping - Solution
https://featured.fanzhuo.xyz/posts/b4104/