Official

C - お菓子の購入 / Buying Sweets Editorial by admin Editorial for printing

Claude Opus 5 (High)

Overview

In this problem, we are given \(N\) types of snacks (type \(i\) has an inventory of \(A_i\) pieces and provides a satisfaction value of \(B_i\) per piece), and we want to choose exactly \(K\) pieces in total to maximize the total satisfaction. This can be solved using a greedy approach: buy snacks up to their inventory limit in descending order of satisfaction.

Analysis

1. First, determine whether it is possible to buy \(K\) snacks at all

If the total inventory \(A_1 + A_2 + \dots + A_N\) is strictly less than \(K\), it is impossible to buy \(K\) snacks no matter what. In this case, output \(-1\).

Conversely, if \(A_1 + A_2 + \dots + A_N \ge K\), there always exists a way to buy exactly \(K\) snacks (for example, by taking snacks one by one until reaching \(K\)).

2. Why the greedy approach is optimal

An important property of this problem is that the cost of each piece of snack (the consumption of the purchase quota) is equally \(1\) across all types. That is, it is optimal to prioritize allocating the quota of \(K\) items to those with larger values of \(B_i\) per piece.

Strictly speaking, this can be proven using an exchange argument. Suppose that in an optimal solution, there exist snack types with \(B_i > B_j\) such that we have bought fewer than the inventory for snack \(i\) (\(x_i < A_i\)) and bought at least one of snack \(j\) (\(x_j > 0\)). If we decrease \(x_j\) by \(1\) and increase \(x_i\) by \(1\), the total number of snacks remains \(K\), while the total satisfaction increases by \(B_i - B_j > 0\). This contradicts the optimality of the initial solution.

Therefore, the optimal solution must buy snacks in descending order of \(B\) up to their inventory limit, stopping once \(K\) pieces have been bought.

3. Why naive approaches fail

  • A loop that “picks the snack with the currently largest \(B\) one piece at a time” would take up to \(K \le 10^9\) iterations, resulting in TLE.
  • A “DP with the number of items as part of the state (knapsack-like)” is impossible because the number of states explodes with \(K \le 10^9\).

The solution is to buy all items of the same type in bulk at once. By sorting the snacks in descending order of \(B\), for each type we can buy the smaller of “the remaining required quantity \(\mathrm{rem}\) and the inventory \(A_i\)” in one step, allowing the loop to finish in at most \(N\) iterations.

4. Concrete Example

For \(N=3, K=5\): - Snack \(1\): \(A_1=3, B_1=2\) - Snack \(2\): \(A_2=1, B_2=10\) - Snack \(3\): \(A_3=4, B_3=5\)

The total inventory is \(8 \ge 5\), so it is possible to buy \(5\) snacks. Sorting in descending order of \(B\) yields \((B=10, A=1), (B=5, A=4), (B=2, A=3)\).

  1. \(B=10\): Buy \(\min(5, 1) = 1\) piece \(\to\) satisfaction \(10\), remaining \(4\) pieces
  2. \(B=5\): Buy \(\min(4, 4) = 4\) pieces \(\to\) satisfaction \(+20 = 30\), remaining \(0\) pieces
  3. The remaining required quantity is \(0\), so we finish.

The answer is \(30\).

Algorithm

  1. While reading the input, compute the total inventory \(S = \sum A_i\).
  2. If \(S < K\), output \(-1\) and terminate.
  3. Sort the pairs \((B_i, A_i)\) in descending order of \(B_i\).
  4. Initialize rem = K and ans = 0, then iterate through each snack in sorted order:
    • take = min(rem, A_i)
    • ans += take * B_i
    • rem -= take
    • Break out of the loop once rem == 0.
  5. Output ans.

Complexity

  • Time complexity: \(O(N \log N)\) (dominated by sorting; the greedy loop itself is \(O(N)\))
  • Space complexity: \(O(N)\)

Implementation Points

  • Watch out for integer overflow: Since \(K \le 10^9\) and \(B_i \le 10^9\), the total satisfaction can be up to around \(10^{18}\). This overflows a 32-bit signed integer, so use long long in C++ (not an issue in Python). The total inventory \(\sum A_i\) can also reach up to \(2 \times 10^5 \times 10^9 = 2 \times 10^{14}\), which similarly requires 64-bit integers.

  • Using take = min(rem, A_i) naturally handles the last type of snack where the remaining required count is less than the inventory.

  • For sorting, storing items as pairs with \(B\) as the first element makes the implementation concise, as the comparator only needs to check x.first > y.first.

  • Checking the \(-1\) condition before sorting is clean and avoids unnecessary work.

  • Since \(N\) can be as large as \(2 \times 10^5\), it is safer to use fast I/O: scanf or fast cin (ios::sync_with_stdio(false)) in C++, and bulk reading via sys.stdin in Python.

    Source Code

#include <bits/stdc++.h>
using namespace std;

int main(){
    int n; long long k;
    scanf("%d %lld", &n, &k);
    vector<pair<long long,long long>> v(n);
    long long total = 0;
    for(int i=0;i<n;i++){
        long long a,b;
        scanf("%lld %lld",&a,&b);
        v[i] = {b,a};
        total += a;
    }
    if(total < k){ printf("-1\n"); return 0; }
    sort(v.begin(), v.end(), [](const pair<long long,long long>&x, const pair<long long,long long>&y){
        return x.first > y.first;
    });
    long long rem = k;
    long long ans = 0;
    for(int i=0;i<n && rem>0;i++){
        long long take = min(rem, v[i].second);
        ans += take * v[i].first;
        rem -= take;
    }
    printf("%lld\n", ans);
    return 0;
}

This editorial was generated by claude5opus-high.

posted:
last update: