제출 #876824

#제출 시각아이디문제언어결과실행 시간메모리
876824overwatch9Sjekira (COCI20_sjekira)C++17
40 / 110
1046 ms20292 KiB
//subtask3 #include <iostream> #include <vector> #include <set> using namespace std; using ll = long long; vector <bool> blocked; vector <vector <int>> adj; vector <int> col; int dfs(int s, int p) { int ans = col[s]; for (auto i : adj[s]) { if (!blocked[i] && i != p) ans = max(ans, dfs(i, s)); } return ans; } int main() { //freopen("in.txt", "r", stdin); int n; cin >> n; adj.resize(n+1); col.resize(n+1); multiset <pair <int, int>> s; for (int i = 1; i <= n; i++) { int t; cin >> t; col[i] = t; s.insert({t, i}); } for (int i = 0; i < n-1; i++) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } blocked.resize(n+1); ll ans = 0; while (!s.empty()) { ll mx = s.rbegin()->first, pos = s.rbegin()->second; s.erase(--s.end()); ll added = 0; for (auto i : adj[pos]) { if (!blocked[i]) { added++; ans += dfs(i, pos); } } blocked[pos] = true; ans += mx * added; } cout << ans << '\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...