Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <set>
- using namespace std;
- int main() {
- int n;
- cin >> n;
- vector<int> vec(n);
- vector<int> depth(n, 0);
- set<pair<int, vector<int>>> temp;
- for (int i = 0; i < n; ++i){
- cin >> vec[i];
- temp.insert({vec[i], {i}});
- }
- for (int i = 0; i < n - 1; ++i){
- auto el1 = *temp.begin();
- temp.erase(temp.begin());
- auto el2 = *temp.begin();
- temp.erase(temp.begin());
- for (auto ind: el1.second){
- ++depth[ind];
- }
- for (auto ind: el2.second){
- ++depth[ind];
- el1.second.push_back(ind);
- }
- el1.first += el2.first;
- temp.insert(el1);
- }
- int ans = 0;
- for (int i = 0; i < n; ++i){
- ans += vec[i] * depth[i];
- }
- cout << ans;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement