C - お菓子の購入 / Buying Sweets Editorial by admin
gpt-6-astra-mediumOverview
This problem asks us to choose exactly \(K\) pieces of sweets while respecting the stock limits for each type, maximizing the total satisfaction. It can be solved using a greedy approach: buy as much as possible starting from the sweets with the highest satisfaction per piece.
Analysis
Determining whether it is possible to make the purchase
First, calculate the total stock of all sweets.
- If \(\sum_{i=1}^{N} A_i < K\), there is not enough stock, so the answer is \(-1\).
- If \(\sum_{i=1}^{N} A_i \ge K\), we can purchase the required number of sweets from each type, so it is possible to choose exactly \(K\) pieces.
Prioritizing sweets with higher satisfaction
Buying one sweet of any type increases the total count of purchased sweets by 1. Therefore, when buying a single piece, it is always advantageous to choose one with a higher satisfaction \(B_i\).
The correctness of this approach can be understood through an exchange argument.
Suppose that in some purchasing method, we buy sweet \(j\) with lower satisfaction while some stock remains for sweet \(i\) with higher satisfaction. That is,
- \(B_i > B_j\)
- \(x_i < A_i\)
- \(x_j > 0\)
all hold.
In this case, if we decrease sweet \(j\) by 1 and increase sweet \(i\) by 1, the total satisfaction increases by \(B_i-B_j > 0\) while still satisfying the conditions on total count and stock limits.
Therefore, there is no need to leave sweets with higher satisfaction unbought while buying sweets with lower satisfaction. The optimal strategy is to buy as many sweets as possible in descending order of satisfaction up to the remaining required count. For types with the same satisfaction, it does not matter which one is bought first.
Processing by type instead of one by one
If we placed all sweets in stock into an array one by one, the number of elements could be up to \(2 \times 10^{14}\), which exceeds both time and memory limits. Furthermore, even if we only processed the \(K\) sweets to be purchased one by one, \(K\) can be up to \(10^9\), which would take too much time.
Therefore, we process them in bulk for each type. If we still need \(R\) more sweets and the current type has a stock of \(A_i\), we can simply buy
\(\mathrm{take} = \min(A_i, R)\)
sweets all at once.
For example, when \((A_i,B_i)\) sorted in descending order of satisfaction are \((2,9),(4,5),(3,2)\) and \(K=5\):
- Buy \(2\) sweets with satisfaction \(9\)
- Buy \(3\) sweets with satisfaction \(5\)
By doing this, we obtain the maximum satisfaction of \(2 \times 9 + 3 \times 5 = 33\).
Algorithm
- Read the information for each type and compute the total stock
total. - If
totalis less than \(K\), output-1and terminate. - Sort the types of sweets in descending order of satisfaction \(B_i\).
- Initialize the total satisfaction
answerto \(0\), and do the following in the sorted order:- Set the number of sweets to buy as
take = min(stock, remaining required count). - Add
take × satisfactiontoanswer. - Subtract
takefrom the remaining required count. - Terminate once the remaining required count reaches \(0\).
- Set the number of sweets to buy as
- Output
answer.
In the provided code, the variable K is updated as the “remaining required count”.
Complexity
- Time Complexity: \(O(N \log N)\)
- Space Complexity: \(O(N)\)
Sorting takes \(O(N \log N)\) time, while input reading and the purchasing process each take \(O(N)\) time. Additionally, \(O(N)\) space is used to store the information for each type.
Implementation Details
Use 64-bit integers
The total stock can be up to \(2 \times 10^{14}\), and the total satisfaction can be up to \(K \times \max B_i \le 10^{18}\). Since these values exceed the range of 32-bit integers, the provided code useslong long. The calculationtake * bis also performed usinglong long.Store satisfaction first in pairs
Although the input is given in the order \(A_i,B_i\), they are stored insweetsin the order \((B_i,A_i)\). By doing this, sorting withgreater<>()will order them from highest to lowest satisfaction. If the satisfaction is the same, the stock will also be sorted in descending order, but this does not affect the answer.Do not buy more than needed
Usingmin(a, K)ensures that we exceed neither the available stock nor the remaining required count. The process terminates as soon asK == 0.Source Code
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
long long K;
cin >> N >> K;
vector<pair<long long, long long>> sweets(N);
long long total = 0;
for (auto& [b, a] : sweets) {
cin >> a >> b;
total += a;
}
if (total < K) {
cout << -1 << '\n';
return 0;
}
sort(sweets.begin(), sweets.end(), greater<>());
long long answer = 0;
for (const auto& [b, a] : sweets) {
long long take = min(a, K);
answer += take * b;
K -= take;
if (K == 0) break;
}
cout << answer << '\n';
return 0;
}
This editorial was generated by gpt-6-astra-medium.