1466D - 13th Labour of Heracles - CodeForces Solution


data structures greedy sortings trees *1500

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <bits/stdc++.h>
// #include "utilities.cpp"
using namespace std;
#define int long long
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define forn(i, x, n) for (int i = x; i < n; i++)
#define vi vector<int>
#define vpp vector<pair<int,int>>
#define vs vector<string>
#define vll vector<long long>
#define mod 1000000007; 
int row[] = {1,0,-1,0};
int col[] = {0,1,0,-1};


void solve(){

    int n;
    cin>>n;

    vpp wt(n); 
    vi inD(n+1, -1);
    int sum = 0;
    forn(i,0,n){
        cin>>wt[i].first;
        sum += wt[i].first;
        wt[i].second = i+1;
    }
    vector<int> adj[n];

    forn(i,0,n-1){

        int a, b;
        cin>>a>>b;
        inD[a]++;
        inD[b]++;
    }

    sort(rall(wt));
    int j = 0;
    cout<<sum<<' ';

    forn(i, 1, n-1){

        if(j == wt.size()){
            cout<<sum<<' ';
        }else{

            int k = wt[j].second;
            while(inD[k] <= 0){

                j++;
                if(j == wt.size()) break;
                k = wt[j].second;
            } 
            if(j == wt.size()){
                cout<<sum<<' ';
            }else{

                inD[k]--;
                sum += wt[j].first;
                cout<<sum<<' ';
            }
        }
    }
    cout<<'\n';
}


signed main(){
    int t = 1; 
    cin >> t;
    while (t--) solve();    
    return 0;
}


Comments

Submit
0 Comments
More Questions

1612A - Distance
520A - Pangram
124A - The number of positions
1041A - Heist
901A - Hashing Trees
1283A - Minutes Before the New Year
1654D - Potion Brewing Class
1107B - Digital root
25A - IQ test
785A - Anton and Polyhedrons
1542B - Plus and Multiply
306A - Candies
1651C - Fault-tolerant Network
870A - Search for Pretty Integers
1174A - Ehab Fails to Be Thanos
1169A - Circle Metro
780C - Andryusha and Colored Balloons
1153A - Serval and Bus
1487C - Minimum Ties
1136A - Nastya Is Reading a Book
1353B - Two Arrays And Swaps
1490E - Accidental Victory
1335A - Candies and Two Sisters
96B - Lucky Numbers (easy)
1151B - Dima and a Bad XOR
1435B - A New Technique
1633A - Div 7
268A - Games
1062B - Math
1294C - Product of Three Numbers