Submission #730019

#TimeUsernameProblemLanguageResultExecution timeMemory
730019InternetPerson10Fireworks (APIO16_fireworks)C++17
100 / 100
366 ms68672 KiB
#include <bits/stdc++.h>
typedef long long ll;

using namespace std;

vector<ll> top;
vector<vector<int>> ch;
vector<multiset<ll>*> s;

// Slope trick, remove the chs until you get something with slope >= -1.

// new 0,    new 1
// x(i-1)+6, x(i)+6

void dfs(int x) {
    if(ch[x].size() == 0) {
        s[x] = new multiset<ll>();
        s[x]->insert(top[x]);
        s[x]->insert(top[x]);
        return;
    }
    for(int i = 0; i < ch[x].size(); i++) {
        dfs(ch[x][i]);
    }
    sort(ch[x].rbegin(), ch[x].rend(), [](int y1, int y2){
        return s[y1]->size() < s[y2]->size();
    });
    s[x] = s[ch[x][0]];
    for(int i = 1; i < ch[x].size(); i++) {
        for(ll g : *(s[ch[x][i]])) {
            s[x]->insert(g);
        }
        s[ch[x][i]]->clear();
    }
    ll y, z;
    int k = ch[x].size() + 1;
    while(k--) {
        auto it = --(s[x]->end());
        y = z;
        z = *it;
        s[x]->erase(it);
    }
    s[x]->insert(y+top[x]);
    s[x]->insert(z+top[x]);
}


int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    n += m;
    top.resize(n, 0);
    ch.resize(n, vector<int>());
    s.resize(n, nullptr);
    ll sum = 0;
    for(int i = 1; i < n; i++) {
        int x;
        cin >> x >> top[i];
        x--;
        sum += top[i];
        ch[x].push_back(i);
    }
    dfs(0);
    ll pr = 0, sl = s[0]->size() - 1;
    for(ll g : *s[0]) {
        sum -= sl * (g - pr);
        pr = g;
        sl--;
    }
    cout << sum << '\n';
}

Compilation message (stderr)

fireworks.cpp: In function 'void dfs(int)':
fireworks.cpp:22:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 |     for(int i = 0; i < ch[x].size(); i++) {
      |                    ~~^~~~~~~~~~~~~~
fireworks.cpp:29:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |     for(int i = 1; i < ch[x].size(); i++) {
      |                    ~~^~~~~~~~~~~~~~
fireworks.cpp:44:19: warning: 'z' may be used uninitialized in this function [-Wmaybe-uninitialized]
   44 |     s[x]->insert(z+top[x]);
fireworks.cpp:43:19: warning: 'y' may be used uninitialized in this function [-Wmaybe-uninitialized]
   43 |     s[x]->insert(y+top[x]);
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...