# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
876823 | overwatch9 | Sjekira (COCI20_sjekira) | C++17 | 0 ms | 348 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//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';
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |