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 limit for each type, maximizing the total satisfaction. It can be solved using a greedy approach: buy as many as needed in bulk, starting from the types with the highest satisfaction per piece.
Analysis
First, check if it is possible to buy exactly \(K\) pieces
If the total stock is strictly less than \(K\), it is impossible to satisfy the condition no matter how we choose, so the answer is \(-1\).
Conversely, if the total stock is \(K\) or more, we can purchase sweets in units of \(1\) piece from each type, so we can always buy exactly \(K\) pieces.
Choose sweets with higher satisfaction first
No matter which sweet we pick, the number of purchased items increases by \(1\). Therefore, it is best to prioritize the types with a higher satisfaction \(B_i\) per piece.
We can see that this is correct by considering the following exchange argument:
- We have purchased a sweet with lower satisfaction.
- There is remaining stock of a sweet with higher satisfaction.
In this case, decreasing the purchased quantity of the lower-satisfaction sweet by \(1\) and increasing that of the higher-satisfaction sweet by \(1\) will increase the total satisfaction without changing the total number of purchased items.
Therefore, in an optimal purchase strategy, we never need to pick a type with lower satisfaction while leaving stock of a type with higher satisfaction unbought. Thus, the optimal strategy is to buy as many as possible in descending order of \(B_i\). For types with the same satisfaction, it does not matter which one we pick first.
Buy in bulk by type rather than one piece at a time
Selecting sweets one piece at a time would require up to \(10^9\) operations, which would exceed the time limit. Furthermore, expanding all stock into individual elements is also impossible because the total stock can be up to \(2 \times 10^{14}\).
Therefore, we process them in bulk for each type. Let \(r\) be the remaining number of pieces to buy; then the number of pieces to purchase from this type is
\(\min(A_i, r)\).
Multiplying this count by \(B_i\) allows us to calculate the obtained satisfaction all at once.
For example, consider the case where the stock and satisfaction pairs are \((3, 10), (4, 6), (2, 8)\) and \(K=5\). We only need to buy \(3\) pieces of satisfaction \(10\), followed by \(2\) pieces of satisfaction \(8\), resulting in an answer of
\(3 \times 10 + 2 \times 8 = 46\).
Algorithm
- Read the information for each type and compute the total stock.
- If the total stock is less than \(K\), output \(-1\) and terminate.
- Sort the sweets in descending order of satisfaction \(B_i\).
- Initialize the total satisfaction as
answer = 0, and usekto represent the remaining number of pieces to buy. - In the sorted order, process each type with stock
aand satisfactionb:- If
a >= k:- Purchasing the remaining
kpieces from this type will satisfy the condition. - Add
k * btoanswerand terminate the process.
- Purchasing the remaining
- If
a < k:- Buy all the available stock of this type.
- Add
a * btoanswerand subtractafromk.
- If
- Output
answer.
Since we already verified that there is sufficient stock in advance, we are guaranteed to be able to buy exactly \(K\) pieces.
Complexity
- Time Complexity: \(O(N \log N)\)
- Sorting takes \(O(N \log N)\), and input reading along with the purchasing process takes \(O(N)\).
- Space Complexity: \(O(N)\)
- We store the satisfaction and stock for each type.
Implementation Details
In the code, we store
(b, a), that is, tuples with satisfaction first. Sorting withsweets.sort(reverse=True)arranges them in descending order of satisfaction. For identical satisfactions, they will be ordered in descending order of stock, but this does not affect the answer.kis updated during processing to represent “how many more pieces we need to buy”.The answer can be up to \(K \times \max B_i \le 10^{18}\). While Python handles integers of arbitrary precision natively, in other languages you need to use a 64-bit integer type.
The total stock can also be up to \(2 \times 10^{14}\), which does not fit in a 32-bit integer.
Source Code
import sys
def main():
readline = sys.stdin.buffer.readline
n, k = map(int, readline().split())
sweets = []
total = 0
for _ in range(n):
a, b = map(int, readline().split())
sweets.append((b, a))
total += a
if total < k:
print(-1)
return
sweets.sort(reverse=True)
answer = 0
for b, a in sweets:
if a >= k:
answer += k * b
break
answer += a * b
k -= a
print(answer)
if __name__ == "__main__":
main()
This editorial was generated by gpt-6-astra-medium.