Submission #1343566

#TimeUsernameProblemLanguageResultExecution timeMemory
1343566pcheloveksFireworks (APIO16_fireworks)C++20
7 / 100
6 ms12204 KiB
#include <bits/stdc++.h>

#define endl '\n'

//#pragma GCC optimize("Ofast")

using namespace std;

using ll = long long;
using ld = long double;
using pii = pair < int, int >;
using pll = pair < ll, ll >;

const ll INF = 2e18;
const ll DIM = 300007;
const ld PI = 3.1415926535;
const int mod = 999999893;

class convexFunction {
public:

    convexFunction() {
        k = 0;
        b = 0;
    }

    ll getL() {
        return x[max((int)x.size() - (int)k - 1, 0)];
    }
    ll getR() {
        if(k == 0) return INF;
        return x[max((int)x.size() - (int)k - 1, 0) + 1];
    }

    ll getMi() {
        ll res = b;
        int ind = max((int)x.size() - (int)k - 1, 0);
        for(int i = x.size() - 1; i > ind; i--) {
            res += x[i];
        }

        ll resk = k - (x.size() - 1 - ind);
        return resk * x[max((int)x.size() - (int)k - 1, 0)] + res;
    }

    vector < ll > x;
    ll k, b;
};

vector < ll > merge(vector < ll >& v1, vector < ll >& v2) {
    vector < ll > res;

    int p1 = 0, p2 = 0;
    while(p1 < v1.size() || p2 < v2.size()) {
        if(p1 == v1.size()) res.push_back(v2[p2++]);
        else if(p2 == v2.size()) res.push_back(v1[p1++]);
        else {
            if(v1[p1] <= v2[p2]) res.push_back(v1[p1++]);
            else res.push_back(v2[p2++]);
        }
    }

    return res;
}

void merge(convexFunction& f1, convexFunction& f2) {
    if(f1.x.size() < f2.x.size()) swap(f1, f2);

    f1.k += f2.k;
    f1.b += f2.b;

    f1.x = merge(f1.x, f2.x);
    f2.x.clear();
}

int n, m;
vector < int > v[DIM];

ll p[DIM], c[DIM];

convexFunction f[DIM];

void dfs(int val) {
    if(v[val].size() == 0) {
        f[val].x = {c[val], c[val]};
        f[val].k = 1;
        f[val].b = -c[val];
        return;
    }
    convexFunction g;

    for(auto to: v[val]) {
        dfs(to);

        merge(g, f[to]);
    }

    if(val == 1) {
        f[val] = g;
        return;
    }

    ll L = g.getL(), R = g.getR();

    for(int i = 0; i < g.x.size() && g.x[i] <= L; i++) {
        f[val].x.push_back(g.x[i]); 
    }
    f[val].x.push_back(L + c[val]);
    f[val].x.push_back(R + c[val]);

    f[val].k = 1;
    f[val].b = g.getMi() - f[val].x.back();

    //cout << val << " " << f[val].getMi() << endl;
}

int main() {

    cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);

    #ifdef IloveCP
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    cin >> n >> m;
    for(int i = 2; i <= n + m; i++) {
        cin >> p[i] >> c[i];
        v[p[i]].push_back(i);
    }

    dfs(1);

    cout << f[1].getMi() << endl;


    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...