Skip to content

Commit

Permalink
Create main.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
JawadSher authored Nov 13, 2024
1 parent e321f4d commit 416c073
Showing 1 changed file with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public:

long long minCost(vector<long long>& arr) {

priority_queue<long long, vector<long long>, greater<long long>> pq;

for(int i = 0; i < arr.size(); i++){
pq.push(arr[i]);
}

long long cost = 0;
while(pq.size() > 1){
long long a = pq.top();
pq.pop();

long long b = pq.top();
pq.pop();

long long sum = a + b;
cost += sum;

pq.push(sum);
}

return cost;
}
};

0 comments on commit 416c073

Please sign in to comment.