Submission #334677

#TimeUsernameProblemLanguageResultExecution timeMemory
334677nikatamlianiSjekira (COCI20_sjekira)C++14
110 / 110
74 ms7424 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...