Submission #959935

#TimeUsernameProblemLanguageResultExecution timeMemory
959935Trisanu_DasFireworks (APIO16_fireworks)C++17
26 / 100
154 ms2248 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll inf = 1e14; const int MAXN = 505; int n, m, L[MAXN], R[MAXN]; vector<pair<int, int> > adj[MAXN]; ll walk[MAXN], dp[MAXN][MAXN]; void precalc(int node){ for(pair<int, int> x : adj[node]){ walk[x.first] = walk[node] + x.second; precalc(x.first); } } void dfs(int node){ if(node >= n){ for(int i = 0; i < n + m; ++i) dp[node][i] = abs(walk[i] - walk[node]); } for(pair<int, int> x : adj[node]){ dfs(x.first); for(int i = 0; i < n + m; ++i){ ll mini = inf; for(int j = 0; j < n + m; ++j){ if(x.second + walk[i] - walk[j] >= 0) mini = min(mini, dp[x.first][j] + abs(walk[i] - walk[j])); } dp[node][i] += mini; } } } int main(){ cin >> n >> m; for(int i = 1; i < n + m; ++i){ int p, c; cin >> p >> c, --p; adj[p].push_back({i, c}); } precalc(0); dfs(0); ll ans = inf; for(int i = 0; i < n + m; ++i) ans = min(ans, dp[0][i]); cout << ans; 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...