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>
#include <bits/extc++.h>
using namespace __gnu_pbds;
using namespace std;
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define ff first
#define sc second
#define pb push_back
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define ull unsigned long long
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
// #define int long long
// #define int unsigned long long
// #define ordered_set(T) tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>
// #define ordered_multiset(T) tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>
void open_file(string filename) {
freopen((filename + ".in").c_str(), "r", stdin);
freopen((filename + ".out").c_str(), "w", stdout);
}
// const ll mod = 1e9 + 7;
// const ll mod = 998244353;
const ll inf = 1e9;
const ll biginf = 1e18;
const int maxN = 1e5 + 25;
set<pii> ers;
vector<int> g[maxN];
int par[maxN], mx[maxN], t[maxN];
void make_set(int n) {
for (int i = 1; i <= n; i++) {
par[i] = i; mx[i] = t[i];
}
}
int find_set(int v) {
if (par[v] == v)
return v;
return par[v] = find_set(par[v]);
}
int union_set(int x, int y) {
x = find_set(x);
y = find_set(y);
if (x == y) return 0;
int ans = mx[x] + mx[y];
par[y] = x; mx[x] = max(mx[x], mx[y]);
return ans;
}
void solve() {
int n;
cin >> n;
pii d[n + 1];
for (int i = 1; i <= n; i++) {
cin >> d[i].ff; d[i].sc = i; t[i] = d[i].ff;
} sort(d + 1, d + n + 1);
for (int i = 1; i < n; i++) {
int x, y; cin >> x >> y;
g[x].pb(y); g[y].pb(x);
} ll ans = 0;
vector<pii> ord;
for (int i = n; i >= 1; i--) {
for (int x : g[d[i].sc]) {
if (ers.find({d[i].sc, x}) == ers.end()) {
ers.insert({d[i].sc, x});
ers.insert({x, d[i].sc});
ord.pb({d[i].sc, x});
}
}
} make_set(n); reverse(all(ord));
for (auto x : ord) {
ans += union_set(x.ff, x.sc);
} cout << ans;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int t = 1;
// cin >> t;
while (t--) {
solve();
cout << '\n';
}
return 0;
}
Compilation message (stderr)
sjekira.cpp: In function 'void open_file(std::string)':
sjekira.cpp:27:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | freopen((filename + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sjekira.cpp:28:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | freopen((filename + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |