Advertisement
pb_jiang

lc t3

Oct 22nd, 2022
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. class Solution {
  2.     using ll = long long;
  3.     using pii = pair<int, int>;
  4. public:
  5.     long long minCost(vector<int>& nums, vector<int>& cost) {
  6.         vector<pii> v(nums.size());
  7.         ll total = 0;
  8.         for (int i = 0; i < v.size(); ++i) v[i] = {nums[i], cost[i]}, total += cost[i];
  9.         sort(v.begin(), v.end());
  10.         ll ret = 0;
  11.         int lb = 0, ub = v.size() - 1;
  12.         while (lb < ub) {
  13.             ll c = min(v[lb].second, v[ub].second);
  14.             ret += c * (v[ub].first - v[lb].first);
  15.             v[lb].second -= c;
  16.             v[ub].second -= c;
  17.             if (v[lb].second == 0) ++lb;
  18.             if (v[ub].second == 0) --ub;
  19.         }
  20.        
  21.         return ret;
  22.     }
  23. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement