Advertisement
mickypinata

USACO-T010: Mixing Milk

Sep 21st, 2021
776
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. ID: mickyta1
  3. TASK: milk
  4. LANG: C++
  5. */
  6.  
  7. #include <bits/stdc++.h>
  8. using namespace std;
  9.  
  10. typedef pair<int, int> pii;
  11.  
  12. const int N = 5000;
  13.  
  14. pii seller[N + 1];
  15.  
  16. int main(){
  17.  
  18.     freopen("milk.in", "r", stdin);
  19.     freopen("milk.out", "w", stdout);
  20.  
  21.     int trMilk, nSell;
  22.     scanf("%d%d", &trMilk, &nSell);
  23.     for(int i = 1; i <= nSell; ++i){
  24.         scanf("%d%d", &seller[i].first, &seller[i].second);
  25.     }
  26.     sort(seller + 1, seller + nSell + 1);
  27.     int sum = 0;
  28.     for(int i = 1; i <= nSell && trMilk > 0; ++i){
  29.         int price = seller[i].first;
  30.         int volume = seller[i].second;
  31.         if(volume > trMilk){
  32.             sum += price * trMilk;
  33.             trMilk = 0;
  34.         } else {
  35.             sum += price * volume;
  36.             trMilk -= volume;
  37.         }
  38.     }
  39.     cout << sum << '\n';
  40.  
  41.     fclose(stdin);
  42.     fclose(stdout);
  43.  
  44.     return 0;
  45. }
  46.  
Advertisement
RAW Paste Data Copied
Advertisement