#include<bits/stdc++.h>
using namespace std;
#define int long long
vector<int>adj[1<<18];
int dp[3][1<<18],H[1<<18],T[1<<18];
void dfs(int n,int p){
int sz=0,x=1e12;
dp[0][n]=dp[1][n]=dp[2][n]=1e12;
priority_queue<int>pq;
pq.push(1e12);
for(auto i:adj[n]) if(i-p){
dfs(i,n);sz++;
if(H[i]-H[n])
dp[1+(H[i]<H[n])][i]=1e12;
x+=dp[1][i];
pq.push(dp[1][i]-dp[2][i]);
}
for(int i=0;i<=sz;i++){
x-=pq.top();
pq.pop();
dp[0][n]=min(dp[0][n],x+T[n]*max(i,sz-i));
dp[1][n]=min(dp[1][n],x+T[n]*max(i+1,sz-i));
dp[2][n]=min(dp[2][n],x+T[n]*max(i,sz+1-i));
}
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
int n;
cin>>n;
for(int i=1;i<=n;i++) cin>>T[i];
for(int i=1;i<=n;i++) cin>>H[i];
for(int i=1,a,b;i<n;i++) cin>>a>>b,
adj[a].push_back(b),adj[b].push_back(a);
dfs(1,0);
cout<<dp[0][1];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16472 KB |
Output is correct |
2 |
Incorrect |
4 ms |
16476 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
516 ms |
524288 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
644 ms |
524288 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
413 ms |
524288 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
488 ms |
524288 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
459 ms |
524288 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
388 ms |
524288 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
16732 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
410 ms |
524288 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
298 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
335 ms |
524288 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |