Submission #1294913

#TimeUsernameProblemLanguageResultExecution timeMemory
1294913Ice_manWorst Reporter 4 (JOI21_worst_reporter4)C++20
79 / 100
300 ms110388 KiB
/**
 ____    ____    ____    __________________    ____    ____    ____
||I ||  ||c ||  ||e ||  ||                ||  ||M ||  ||a ||  ||n ||
||__||  ||__||  ||__||  ||________________||  ||__||  ||__||  ||__||
|/__\|  |/__\|  |/__\|  |/________________\|  |/__\|  |/__\|  |/__\|

*/

#include <bits/stdc++.h>



#define maxn (int)(2e5 + 5)
#define PB push_back
#define X first
#define Y second


typedef long long ll;
typedef std::pair <int, int> pii;
typedef std::pair <ll, ll> pll;
typedef std::pair <int, ll> pil;
typedef std::pair <ll, int> pli;

const ll mod = 1e9 + 7;
const ll INF = 1e9;






std::map <ll , ll> dp[maxn];
ll c[maxn] , h[maxn];
std::vector <int> v[maxn];

void dfs(int node)
{
    dp[node][0] = c[node];

    for(auto& nb : v[node])
    {
        dfs(nb);

        if(dp[node].size() < dp[nb].size())
            dp[node].swap(dp[nb]);

        for(auto& e : dp[nb])
            dp[node][e.X] += e.Y;
    }

    dp[node][h[node]] += 0;
    dp[node][h[node] + 1] += c[node];

    auto it = dp[node].find(h[node]);
    ll le = -c[node] , prev = h[node];

    while(true)
    {
        le += it -> Y;
        if(le >= 0)
            break;

        it = dp[node].erase(it);
        it--;
    }

    dp[node][it -> X] = le;
}

int n;

void read()
{
    std::cin >> n;
    for(int i = 1; i <= n; i++)
    {
        int pp;
        std::cin >> pp >> h[i] >> c[i];
        if(i >= 2)
            v[pp].PB(i);
    }

    dfs(1);
    std::cout << dp[1].begin() -> Y << "\n";
}








int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int tests = 1;

    //std::cin >> tests;
    while(tests--)
    {
        read();
    }



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