Submission #968348

#TimeUsernameProblemLanguageResultExecution timeMemory
968348TAhmed33Fireworks (APIO16_fireworks)C++98
19 / 100
15 ms2792 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]); for (int i = 1; i <= n - m; i++) { for (int j = 1; j < 300; j++) { assert(dp[i][j] - dp[i][j - 1] <= dp[i][j + 1] - dp[i][j]); } } cout << mn << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...