-
Notifications
You must be signed in to change notification settings - Fork 0
/
1997D.cpp
83 lines (71 loc) · 1.13 KB
/
1997D.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
void solve()
{
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
vector<int> p(n, -1);
for (int i = 1; i < n; i++)
{
cin >> p[i];
p[i]--;
}
vector<vector<int> > sons(n);
for (int i = 0; i < n; i++)
{
if (p[i] >= 0)
sons[p[i]].push_back(i);
}
vector<int> tpl;
queue<int> que;
for (que.push(0); !que.empty(); que.pop())
{
const int u = que.front();
tpl.push_back(u);
for (const auto & v : sons[u])
que.push(v);
}
vector<int> dp(n);
for (int i = n - 1; i >= 0; i--)
{
const int & u = tpl[i];
if (sons[u].empty())
dp[u] = a[u];
else
{
int mn = -1;
for (const auto & v : sons[u])
{
if (mn < 0 || mn > dp[v])
mn = dp[v];
}
if (mn <= a[u])
dp[u] = mn;
else
dp[u] = a[u] + (mn - a[u]) / 2;
}
}
int mn = -1;
for (const auto & v : sons[0])
{
if (mn < 0 || mn > dp[v])
mn = dp[v];
}
if (mn < 0)
mn = 0;
cout << a[0] + mn << endl;
}
int main()
{
ios::sync_with_stdio(false);
int t;
cin >> t;
for (int tn = 0; tn < t; tn++)
solve();
return 0;
}