#include <bits/stdc++.h>
#define int long long
#define mi(x, y) (x = min(x, y))
#define ma(x, y) (x = max(x, y))
using namespace std;
const int NS = (int)3e5 + 4;
int n;
int a[NS], f[NS], mn[NS];
int dp[NS];
vector<int> way[NS];
int chk[NS];
deque<int> deq;
map<int, int> mp;
int diff = 0;
int pre[NS];
void sol(int x, int pr){
dp[x] = mn[a[x]];
for(auto&nxt:way[x]){
if(nxt == pr){
continue;
}
sol(nxt, x);
pre[x] = nxt;
if(a[x] + 1 == a[nxt]){
dp[x] = dp[nxt];
}
else{
dp[x] = dp[nxt] + mn[a[x]];
if(a[nxt] == 0){
while(diff + !mp[a[nxt] - a[x]] > 2){
mp[a[deq[0]] - a[deq[1]]] -= 1;
if(!mp[a[deq[0]] - a[deq[1]]]) --diff;
deq.pop_front();
}
dp[x] = min(dp[x], f[a[x]] + (pre[deq.front()] != -1 ? dp[pre[deq.front()]] : 0));
}
}
}
if(pr != -1){
deq.push_back(x);
++mp[a[x] - a[pr]];
if(mp[a[x] - a[pr]] == 1){
++diff;
}
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
memset(pre, -1, sizeof(pre));
cin >> n;
for(int i = 0; i < n; ++i){
cin >> a[i];
}
for(int i = 0; i < n; ++i){
cin >> f[i];
}
for(int i = n - 1; i >= 0; --i){
mn[i] = f[i];
if(i + 1 < n){
mn[i] = min(mn[i], mn[i + 1]);
}
}
for(int i = 1; i < n; ++i){
int x, y;
cin >> x >> y;
--x, --y;
way[x].push_back(y);
way[y].push_back(x);
}
sol(0, -1);
cout << dp[0] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
10708 KB |
Output is correct |
2 |
Correct |
7 ms |
10688 KB |
Output is correct |
3 |
Correct |
6 ms |
10768 KB |
Output is correct |
4 |
Correct |
7 ms |
10708 KB |
Output is correct |
5 |
Correct |
7 ms |
10708 KB |
Output is correct |
6 |
Incorrect |
7 ms |
10688 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
131 ms |
64804 KB |
Output is correct |
2 |
Correct |
129 ms |
64608 KB |
Output is correct |
3 |
Correct |
127 ms |
64408 KB |
Output is correct |
4 |
Correct |
137 ms |
64376 KB |
Output is correct |
5 |
Correct |
124 ms |
63164 KB |
Output is correct |
6 |
Correct |
126 ms |
62216 KB |
Output is correct |
7 |
Correct |
130 ms |
62024 KB |
Output is correct |
8 |
Incorrect |
125 ms |
62016 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
9708 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
10708 KB |
Output is correct |
2 |
Correct |
7 ms |
10688 KB |
Output is correct |
3 |
Correct |
6 ms |
10768 KB |
Output is correct |
4 |
Correct |
7 ms |
10708 KB |
Output is correct |
5 |
Correct |
7 ms |
10708 KB |
Output is correct |
6 |
Incorrect |
7 ms |
10688 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
10708 KB |
Output is correct |
2 |
Correct |
7 ms |
10688 KB |
Output is correct |
3 |
Correct |
6 ms |
10768 KB |
Output is correct |
4 |
Correct |
7 ms |
10708 KB |
Output is correct |
5 |
Correct |
7 ms |
10708 KB |
Output is correct |
6 |
Incorrect |
7 ms |
10688 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |