Submission #968019

#TimeUsernameProblemLanguageResultExecution timeMemory
968019TAhmed33Fireworks (APIO16_fireworks)C++98
19 / 100
18 ms2904 KiB
#include <bits/stdc++.h> using namespace std; #define int long long const int MAXN = 5e3 + 25; vector <pair <int, int>> adj[MAXN]; int dp[MAXN][301], n, m; void dfs (int pos) { for (auto j : adj[pos]) { dfs(j.first); } if(adj[pos].empty()){ for(int i=1;i<=300;i++)dp[pos][i]=1e18; return; } for (int i = 0; i <= 300; i++) { int sum = 0; for (auto j : adj[pos]) { int mn = 1e18; for(int k=0;k<=i;k++)mn=min(mn,abs(j.second-i+k)+dp[j.first][k]); sum += mn; } dp[pos][i] = sum; } } signed main () { cin >> n >> m; n += m; for (int i = 2; i <= n; i++) { int x, y; cin >> x >> y; adj[x].push_back({i, y}); } dfs(1); int mn = 1e18; for (int i = 0; i <= 300; i++) mn = min(mn, dp[1][i]); cout<<mn; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...