-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMinimize the sum.cpp
51 lines (42 loc) · 995 Bytes
/
Minimize the sum.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//{ Driver Code Starts
//Initial Template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function Template for C++
class Solution {
public:
int minimizeSum(int N, vector<int> arr) {
// code here
priority_queue <int, vector<int>, greater<int> > temp;
for(int i=0;i<N;i++){
temp.push(arr[i]);
}
int sum = 0;
while(temp.size()>1){
int a = temp.top();
temp.pop();
int b = temp.top();
temp.pop();
int add = a+b;
sum+=add;
temp.push(add);
}
return sum;
}
};
//{ Driver Code Starts.
int main() {
int t;
cin>>t;
while(t--) {
int n;
cin>>n;
vector<int> arr(n);
for(int i = 0; i < n; i++)
cin>>arr[i];
Solution obj;
cout << obj.minimizeSum(n, arr) << "\n";
}
}
// } Driver Code Ends