Submission #1060085

#TimeUsernameProblemLanguageResultExecution timeMemory
1060085aymanrsNestabilnost (COI23_nestabilnost)C++17
41 / 100
455 ms524288 KiB
#include<bits/stdc++.h> using namespace std; vector<int> cost;int N; struct node { vector<long long> dp; long long free; vector<node*> l; int a; }; void dfs(node* n, node* p){ for(node* c : n->l) if(c!=p) dfs(c,n); n->dp.resize(N+1); n->free = LONG_LONG_MAX; for(int k = n->a+1;k <= N;k++){ n->dp[k] = cost[k]; for(node* c : n->l){ if(c==p) continue; if(!c->a){ if(k == n->a+1) n->dp[k] += min(c->dp[k]-cost[k], c->free); else n->dp[k] += c->free; } else if(c->a == n->a+1){ if(k>c->a) n->dp[k] += min(c->dp[k]-cost[k], c->free); else n->dp[k] += c->free; } else n->dp[k] += c->free; } n->free = min(n->free, n->dp[k]); } } void solve(){ int n;cin >> n; N=n; node g[n+1]; cost.resize(n+1); for(int i = 1;i <= n;i++) cin >> g[i].a; for(int i = 1;i <= n;i++) cin >> cost[i]; for(int i = 0;i < n-1;i++){ int u,v;cin >> u >> v; g[u].l.push_back(&g[v]); g[v].l.push_back(&g[u]); } dfs(&g[1], NULL); cout << g[1].free << '\n'; } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...