#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ldb,ldb> pdd;
#define ff(i,a,b) for(int i = a; i <= b; i++)
#define fb(i,b,a) for(int i = b; i >= a; i--)
#define trav(a,x) for(auto& a : x)
#define sz(a) (int)(a).size()
#define fi first
#define se second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k) print the k-th smallest number in os(0-based)
const int mod = 1000000007;
const ll inf = 1e15 + 5;
const int mxN = 300005;
int n;
int A[mxN];
int f[mxN];
vector<int> g[mxN];
int sufi[mxN];
int mx[mxN];
int par[mxN];
vector<int> svi[mxN];
int findpar(int x){
return (x == par[x] ? x : par[x] = findpar(par[x]));
}
void unite(int x, int y){
x = findpar(x), y = findpar(y);
if(x == y)return;
if(sz(svi[x]) < sz(svi[y]))swap(x, y);
par[y] = x; for(auto c : svi[y])svi[x].pb(c);
mx[x] = max(mx[x], mx[y]);
}
void init(){
ff(i,1,n){
par[i] = i;
svi[i].pb(i);
mx[i] = A[i] + 1;
}
}
vector<int> ko[mxN];
vector<pii> edge;
void dfs1(int v, int p){
for(auto u : g[v]){
if(u != p){
dfs1(u, v);
if(A[v] + 1 == A[u]){
unite(u, v);
}
if(A[u] == 0){
edge.pb({v, u});
}
}
}
}
ll dp[mxN];
ll get(int v, int p, int X){
if(X <= A[v])return inf;
ll sum = 0;
for(auto u : g[v]){
if(u == p)continue;
if(A[v] + 1 == A[u]){
int a = A[u] + 1;
if(X >= a)sum += get(u, v, X);
else sum += dp[u];
}
else if(A[u] == 0){
int a = A[v] + 1;
if(X == a)sum += get(u, v, X);
else sum += dp[u];
}
else sum += dp[u];
}
return sum;
}
pll calc(int v, int p){
ll najv = A[v] + 1, sum = 0;
for(auto u : g[v]){
if(u == p)continue;
if(A[v] + 1 == A[u]){
pll X = calc(u, v);
najv = max(najv, X.fi);
sum += X.se;
}
else sum += dp[u];
}
return {najv, sum};
}
void dfs(int v, int p){
vector<int> jed;
for(auto u : g[v]){
if(u != p){
dfs(u, v);
}
}
// ili cu uzeti neko jednako ili cu samo uzeti svako vece povezano sa v
dp[v] = inf;
for(auto c : ko[findpar(v)]){
dp[v] = min(dp[v], get(v, p, c) + f[c]);
}
pll X = calc(v, p);
dp[v] = min(dp[v], sufi[X.fi] + X.se);
// cout << "v: " << v << '\n';
// for(auto c : ko[findpar(v)])cout << c << " ";
// cout << '\n';
// cout << "mx & sum: " << X.fi << " " << X.se << '\n';
// cout << "dp: " << dp[v] << '\n';
// cout << "---------------------------" << '\n';
// cout << '\n';
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n;
ff(i,1,n)cin >> A[i];
ff(i,1,n)cin >> f[i];
ff(i,1,n - 1){
int u, v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
sufi[n] = f[n];
fb(i,n - 1,1)sufi[i] = min(sufi[i + 1], f[i]);
init(); dfs1(1, -1);
for(auto c : edge){
int u = findpar(c.fi);
int v = findpar(c.se);
ko[u].pb(A[c.fi] + 1);
}
dfs(1, -1);
cout << dp[1] << '\n';
return 0;
}
/*
7
2 3 0 3 2 0 0
6 8 2 9 9 9 9
1 2
2 3
1 4
4 5
5 6
5 7
7
2 3 0 3 2 0 0
6 8 2 9 9 9 1
1 2
2 3
1 4
4 5
5 6
5 7
// probati bojenje sahovski
*/
Compilation message
code1.cpp: In function 'int main()':
code1.cpp:188:13: warning: unused variable 'v' [-Wunused-variable]
188 | int v = findpar(c.se);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
93 ms |
28000 KB |
Output is correct |
2 |
Correct |
93 ms |
27992 KB |
Output is correct |
3 |
Correct |
110 ms |
27984 KB |
Output is correct |
4 |
Correct |
38 ms |
27996 KB |
Output is correct |
5 |
Correct |
11 ms |
27992 KB |
Output is correct |
6 |
Correct |
8 ms |
27996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1570 ms |
89740 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
26972 KB |
Output is correct |
2 |
Correct |
5 ms |
26972 KB |
Output is correct |
3 |
Correct |
6 ms |
26972 KB |
Output is correct |
4 |
Correct |
5 ms |
26972 KB |
Output is correct |
5 |
Correct |
6 ms |
26972 KB |
Output is correct |
6 |
Correct |
6 ms |
26972 KB |
Output is correct |
7 |
Incorrect |
6 ms |
26972 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
93 ms |
28000 KB |
Output is correct |
2 |
Correct |
93 ms |
27992 KB |
Output is correct |
3 |
Correct |
110 ms |
27984 KB |
Output is correct |
4 |
Correct |
38 ms |
27996 KB |
Output is correct |
5 |
Correct |
11 ms |
27992 KB |
Output is correct |
6 |
Correct |
8 ms |
27996 KB |
Output is correct |
7 |
Correct |
5 ms |
26972 KB |
Output is correct |
8 |
Correct |
5 ms |
26972 KB |
Output is correct |
9 |
Correct |
6 ms |
26972 KB |
Output is correct |
10 |
Correct |
5 ms |
26972 KB |
Output is correct |
11 |
Correct |
6 ms |
26972 KB |
Output is correct |
12 |
Correct |
6 ms |
26972 KB |
Output is correct |
13 |
Incorrect |
6 ms |
26972 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
93 ms |
28000 KB |
Output is correct |
2 |
Correct |
93 ms |
27992 KB |
Output is correct |
3 |
Correct |
110 ms |
27984 KB |
Output is correct |
4 |
Correct |
38 ms |
27996 KB |
Output is correct |
5 |
Correct |
11 ms |
27992 KB |
Output is correct |
6 |
Correct |
8 ms |
27996 KB |
Output is correct |
7 |
Execution timed out |
1570 ms |
89740 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |