-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path2448.cpp
31 lines (28 loc) · 984 Bytes
/
2448.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public:
long long minCost(vector<int>& nums, vector<int>& cost) {
long long current = 0;
long long leftSum = 0;
long long rightSum = 0;
for (auto costElement : cost) rightSum += costElement;
long long res = LLONG_MAX;
long long sum = 0;
vector<vector<int>> numsWithCost;
int n = nums.size();
for (int i = 0; i < n; ++i) {
numsWithCost.push_back({nums[i], cost[i]});
sum += static_cast<long long>(nums[i]) * cost[i];
}
sort(numsWithCost.begin(), numsWithCost.end());
for (int i = 0; i < n; ++i) {
long long diff = abs(numsWithCost[i][0] - current);
current = numsWithCost[i][0];
sum += leftSum * diff;
sum -= rightSum * diff;
res = min(res, sum);
leftSum += numsWithCost[i][1];
rightSum -= numsWithCost[i][1];
}
return res;
}
};