-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmopc20c2p3.cpp
51 lines (48 loc) · 1.25 KB
/
dmopc20c2p3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <bits/stdc++.h>
using namespace std;
#define ms(x,a) memset(x,a,sizeof x)
typedef long long ll;
const int mod=1e9+7, seed=131, MAXN=2e5+1;
ll val[MAXN], in[MAXN], sz[MAXN], tot; int deg[MAXN];
vector<int> adj[MAXN], rev[MAXN];
int main(){
cin.tie(0)->sync_with_stdio(0);
int n; cin >> n;
for (int i=1;i<=n;++i) cin >> val[i], in[i]=sz[i]=val[i], tot-=val[i];
for (int i=1;i<=n-1;++i){
int a, b; cin >> a >> b;
adj[a].push_back(b);
rev[b].push_back(a);
deg[b]++;
}
queue<int> q;
for (int i=1;i<=n;++i) if (!deg[i]) q.push(i);
while (!q.empty()){
int v=q.front(); q.pop();
tot+=val[v]*in[v];
for (int to:adj[v]){
deg[to]--;
if (deg[to]==0) q.push(to);
in[to]+=in[v];
}
}
for (int i=1;i<=n;++i){
deg[i]=adj[i].size();
if (!deg[i]) q.push(i);
}
while (!q.empty()){
int v=q.front(); q.pop();
for (int to:rev[v]){
deg[to]--;
if (deg[to]==0) q.push(to);
sz[to]+=sz[v];
}
}
ll best=0;
for (int i=1;i<=n;++i){
for (int to:adj[i]){
best=max(best,(in[to]-in[i])*(sz[i]-sz[to]));
}
}
cout << best+tot << '\n';
}