제출 #707677

#제출 시각아이디문제언어결과실행 시간메모리
707677_martynasFireworks (APIO16_fireworks)C++11
7 / 100
10 ms14424 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pli = pair<ll, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; #define F first #define S second #define PB push_back #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() void FASTIO() {ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); } const int MXN = 6e5+5; int n, m; vi adj[MXN]; ll C[MXN]; pll interval[MXN]; ll ans; void dfs(int u) { if(adj[u].empty()) { interval[u] = {0, 0}; return; } vl X; for(int v : adj[u]) { dfs(v); X.PB(interval[v].F+C[v]); X.PB(interval[v].S+C[v]); } sort(all(X)); int k = X.size(); for(int v : adj[u]) { if(interval[v].F+C[v] <= X[k/2-1] && X[k/2-1] <= interval[v].S+C[v]) continue; ans += min(labs(X[k/2-1]-(interval[v].F+C[v])), labs(X[k/2-1]-(interval[v].S+C[v]))); } interval[u] = {X[k/2-1], X[k/2]}; } int main() { FASTIO(); cin >> n >> m; for(int i = 2; i <= n+m; i++) { int p; cin >> p >> C[i]; adj[p].PB(i); } dfs(1); cout << ans << "\n"; 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...