Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- using ll = long long;
- using pii = pair<int, int>;
- public:
- long long minCost(vector<int>& nums, vector<int>& cost) {
- vector<pii> v(nums.size());
- ll total = 0;
- for (int i = 0; i < v.size(); ++i) v[i] = {nums[i], cost[i]}, total += cost[i];
- sort(v.begin(), v.end());
- ll ret = 0;
- int lb = 0, ub = v.size() - 1;
- while (lb < ub) {
- ll c = min(v[lb].second, v[ub].second);
- ret += c * (v[ub].first - v[lb].first);
- v[lb].second -= c;
- v[ub].second -= c;
- if (v[lb].second == 0) ++lb;
- if (v[ub].second == 0) --ub;
- }
- return ret;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement