Submission #625392

#TimeUsernameProblemLanguageResultExecution timeMemory
625392socpiteFireworks (APIO16_fireworks)C++14
7 / 100
5 ms7356 KiB
#include<bits/stdc++.h> using namespace std; #define f first #define s second typedef long long ll; const int maxn = 3e5+5; const ll inf = 1e18; ll ans = 0; int n, m; int w[maxn]; vector<int> tree[maxn]; pair<ll, int> dfs(int x){ if(x > n)return {w[x], w[x]}; vector<pair<ll, int>> op; for(auto v: tree[x]){ auto tmp = dfs(v); op.push_back({tmp.f, 0}); op.push_back({tmp.s, 1}); } sort(op.begin(), op.end()); ll mid = op[op.size()/2].f; for(auto v: op){ if(v.s)ans += max(0LL, mid - v.f); else ans += max(0LL, v.f-mid); } return {op[op.size()/2-1].f+w[x], op[op.size()/2].f+w[x]}; } int main(){ cin >> n >> m; for(int i = 2; i <= n+m; i++){ int x; cin >> x >> w[i]; tree[x].push_back(i); } dfs(1); cout << ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...