-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwealthDisparity.cpp
More file actions
62 lines (46 loc) · 927 Bytes
/
wealthDisparity.cpp
File metadata and controls
62 lines (46 loc) · 927 Bytes
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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
vector<vector<ll>> adjList;
vector<bool> visited;
ll ans = -1000000;
void dfs (ll node, ll man, ll A[])
{
visited[node] = true;
man = max(man, A[node]);
for(auto i: adjList[node]) {
if (visited[i] == false) {
ans = max(ans, man-A[i]);
dfs(i, man, A);
};
};
}
int main ()
{
ll n;
cin >> n;
ll A[n];
ll P[n];
ll hojo;
adjList.resize(n);
visited.resize(n, false);
for (ll i = 0; i < n; ++i) {
cin >> A[i];
}
for (ll i = 0; i < n; ++i) {
int par;
cin >> par;
par--;
if (par == -2) {
hojo = i;
} else {
adjList[par].push_back(i);
P[i] = par;
}
}
visited[hojo] = true;
dfs(hojo, A[hojo], A);
cout << ans << endl;
}