Submission #1078672

#TimeUsernameProblemLanguageResultExecution timeMemory
1078672gs25Fireworks (APIO16_fireworks)C++17
7 / 100
14 ms23924 KiB
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2") #include <bits/stdc++.h> #define pb push_back #define all(v) (v).begin(), (v).end() #define rep(i, n) for (int i = 0; i < n; ++i) #define rrep(i, n) for (int i = 1; i <= n; ++i) #define ff first #define ss second using namespace std; typedef long long ll; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } #ifdef LOCAL #define dbg(x...) \ cerr << "\e[91m" << __func__ << ":" << __LINE__ << " [" << #x << "] = ["; \ _print(x); \ cerr << "\e[39m" << endl; #else #define dbg(x...) #endif const int MAXN = 500100; int par[MAXN]; vector<int> child[MAXN]; ll cost[MAXN]; vector<ll> fuck[MAXN]; ll dp[MAXN]; void dfs(int v){ if(child[v].empty()){ dp[v] = 0; fuck[v].pb(cost[v]); fuck[v].pb(cost[v]); return; } vector<ll> shit; ll tmp = 0; for(auto ne : child[v]){ dfs(ne); dp[v] += dp[ne]; for(auto x : fuck[ne]) shit.pb(x); ll tt = fuck[ne][0]-fuck[ne][1]; if(tt<0) tt = -tt; tmp -= tt; } sort(all(shit)); ll mid = shit[shit.size()/2]; for(auto x : shit) tmp += abs(x-mid); dbg(tmp) dp[v] += tmp/2; fuck[v].pb(mid); fuck[v].pb(shit[shit.size()/2-1]); dbg(v,shit,fuck[v],dp[v]) } void solve(){ int n,m; cin >> n >> m; n += m; for(int i=2; i<=n+m; i++){ ll x; cin >> par[i] >> x; cost[i] = cost[par[i]] + x; child[par[i]].pb(i); } dfs(1); cout << dp[1]; }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t = 1; // cin >> t; while(t--) solve(); 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...