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>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define mp make_pair
#define pb push_back
#define se second
#define fi first
using namespace std;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
clock_t startTime;
double getCurrentTime() {
return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}
ll set_on(ll n, ll k){
return (n |= 1 << k);
}
ll set_off(ll n, ll k){
return (n &= ~(1UL << k));
}
bool check_bit(ll n, ll k){
int bit = (n >> k) & 1U;
if(bit == 1)
return true;
return false;
}
int n;
vi arr, G[100100], sum, sz, depth, maximum;
void dfs(int at, int prev, int dep){
sum[at] = arr[at];
sz[at] = 1;
depth[at] = dep;
maximum[at] = arr[at];
for(auto next : G[at]){
if(next != prev){
dfs(next, at, dep + 1);
sum[at] += sum[next];
sz[at] += sz[next];
maximum[at] = max(maximum[at], maximum[next]);
}
}
}
void solve(){
cin>>n;
arr.resize(n);
maximum.resize(n);
sum.resize(n);
sz.resize(n);
depth.resize(n);
for(int i = 0; i < n; i++)
cin>>arr[i];
for(int i = 1; i < n; i++){
int a, b;
cin>>a>>b;
a--;b--;
G[a].pb(b);
G[b].pb(a);
}
// assert(false);
dfs(0, -1, 0);
// assert(false);
set<pii, greater<pii>> ord;
set<int> ava;
for(int i = 0; i < n; i++){
ord.insert(mp(arr[i], i));
ava.insert(i);
}
int ans = 0;
// assert(false);
for(auto t : ord){
int vertex = t.se;
if(ava.find(vertex) == ava.end())
continue;
if(ava.size() == 1) break;
// debug(vertex+1);
vector<pii> subs;
for(auto next : G[vertex]){
if(ava.find(next) == ava.end()) continue;
subs.pb(mp(arr[vertex] * sz[next] + sum[next], next));
}
sort(subs.begin(), subs.end());
for(int i = 0; i < subs.size(); i++){
if(i + 1 == subs.size()){
int maks = 0;
for(auto t : ava){
if(t == vertex) continue;
maks = max(maks, arr[t]);
}
// debug(vertex+1, arr[vertex], maks);
ans += arr[vertex] + maks;
ava.erase(vertex);
continue;
}
// debug(subs[i].fi, subs[i].se);
ans += subs[i].fi;
queue<int> q;
q.push(subs[i].se);
while(!q.empty()){
int at = q.front();
ava.erase(at);
q.pop();
for(auto next : G[at]){
if(next == vertex) continue;
if(ava.find(next) != ava.end()){
q.push(next);
}
}
}
}
}
cout<<ans<<endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.precision(10);
cout<<fixed;
startTime = clock();
int t=1;
// cin>>t;
while(t--)
solve();
return 0;
}
Compilation message (stderr)
sjekira.cpp: In function 'void solve()':
sjekira.cpp:111:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
111 | for(int i = 0; i < subs.size(); i++){
| ~~^~~~~~~~~~~~~
sjekira.cpp:112:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
112 | if(i + 1 == subs.size()){
| ~~~~~~^~~~~~~~~~~~~~
# | 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... |