#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 = 5005;
int n;
int A[mxN];
int f[mxN];
vector<int> g[mxN];
ll mn[mxN];
int k1[mxN];
int k2[mxN];
ll dp[mxN][mxN];
void dfs(int v, int p){
vector<int> deca;
for(auto u : g[v]){
if(u != p){
dfs(u, v);
deca.pb(u);
}
}
for(auto u : deca){
k1[u] = k2[u] = n + 1;
if(A[v] + 1 == A[u]){
k1[u] = A[u] + 1;
}
if(A[u] == 0){
k2[u] = A[v] + 1;
}
}
ff(i,A[v] + 1,n){
ll kol = 0;
for(auto u : deca){
if(k1[u] <= i || k2[u] == i){
kol += min(mn[u], dp[u][i] - f[i]);
}
else{
kol += mn[u];
}
}
dp[v][i] = min(dp[v][i], kol + f[i]);
}
mn[v] = inf;
ff(i,1,n)mn[v] = min(mn[v], dp[v][i]);
}
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);
}
ff(i,1,n)ff(j,1,n)dp[i][j] = inf;
dfs(1, -1);
ll rez = inf;
ff(i,1,n)rez = min(rez, dp[1][i]);
cout << rez << '\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
// probati bojenje sahovski
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
171 ms |
197456 KB |
Output is correct |
2 |
Correct |
117 ms |
197716 KB |
Output is correct |
3 |
Correct |
114 ms |
197404 KB |
Output is correct |
4 |
Correct |
120 ms |
197448 KB |
Output is correct |
5 |
Correct |
111 ms |
197444 KB |
Output is correct |
6 |
Correct |
118 ms |
197452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2664 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
171 ms |
197456 KB |
Output is correct |
2 |
Correct |
117 ms |
197716 KB |
Output is correct |
3 |
Correct |
114 ms |
197404 KB |
Output is correct |
4 |
Correct |
120 ms |
197448 KB |
Output is correct |
5 |
Correct |
111 ms |
197444 KB |
Output is correct |
6 |
Correct |
118 ms |
197452 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2664 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
118 ms |
196848 KB |
Output is correct |
15 |
Correct |
119 ms |
196852 KB |
Output is correct |
16 |
Correct |
127 ms |
196704 KB |
Output is correct |
17 |
Correct |
114 ms |
196692 KB |
Output is correct |
18 |
Correct |
116 ms |
196864 KB |
Output is correct |
19 |
Correct |
109 ms |
196952 KB |
Output is correct |
20 |
Correct |
103 ms |
193852 KB |
Output is correct |
21 |
Correct |
106 ms |
196692 KB |
Output is correct |
22 |
Correct |
107 ms |
196700 KB |
Output is correct |
23 |
Correct |
107 ms |
196728 KB |
Output is correct |
24 |
Correct |
107 ms |
196704 KB |
Output is correct |
25 |
Correct |
104 ms |
193872 KB |
Output is correct |
26 |
Correct |
107 ms |
196776 KB |
Output is correct |
27 |
Correct |
110 ms |
196948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
171 ms |
197456 KB |
Output is correct |
2 |
Correct |
117 ms |
197716 KB |
Output is correct |
3 |
Correct |
114 ms |
197404 KB |
Output is correct |
4 |
Correct |
120 ms |
197448 KB |
Output is correct |
5 |
Correct |
111 ms |
197444 KB |
Output is correct |
6 |
Correct |
118 ms |
197452 KB |
Output is correct |
7 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |