This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
#define ll long long
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int n;
cin >> n;
vector<int> id, deg(n+1), a(n+1);
vector<vector<int>> g(n+1);
for(int i = 1; i <= n; ++i) {
cin >> a[i];
id.push_back(i);
}
for(int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
++deg[u];
++deg[v];
}
sort(id.begin(), id.end(), [&](int x, int y) {
return a[x] > a[y];
});
bool is_first = true;
ll ans = 0;
for(int i : id) {
ans += (long long)a[i] * (deg[i] + (!is_first));
for(int x : g[i]) {
--deg[x];
}
is_first = false;
}
cout << ans << '\n';
}
# | 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... |