Submission #854844

#TimeUsernameProblemLanguageResultExecution timeMemory
854844NeroZeinSjekira (COCI20_sjekira)C++17
0 / 110
29 ms5840 KiB
#include "bits/stdc++.h" using namespace std; #ifdef Nero #include "Deb.h" #else #define deb(...) #endif const int N = 2e5 + 5; int a[N]; int p[N], sz[N], mx[N]; int get(int v) { return p[v] = (p[v] == v ? v : get(p[v])); } void unite(int u, int v) { if (sz[u] > sz[v]) swap(u, v); sz[v] += sz[u]; p[u] = v; mx[v] = max(mx[v], mx[u]); } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; } vector<array<int, 4>> e(n - 1); for (int i = 0; i < n - 1; ++i) { int u, v; cin >> u >> v; if (a[u] > a[v]) swap(u, v); e[i] = {a[u], a[v], u, v}; } for (int i = 1; i <= n; ++i) { p[i] = i; sz[i] = 1; mx[i] = a[i]; } sort(e.begin(), e.end()); long long ans = 0; for (int i = 0; i < n - 1; ++i) { auto [x, y, u, v] = e[i]; u = get(u), v = get(v); ans += mx[u] + mx[v]; unite(u, v); } cout << ans << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...