Skip to content

Commit bf39803

Browse files
authored
Merge pull request #329 from AlgorithmWithGod/khj20006
[20250508] BOJ / P1 / 고양이 소개팅 / 권혁준
2 parents 7470a1e + 1f591f7 commit bf39803

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
```cpp
2+
#include <iostream>
3+
#include <vector>
4+
#include <map>
5+
#include <algorithm>
6+
using namespace std;
7+
using ll = long long;
8+
9+
int N;
10+
ll C[200001]{}, H[200001]{}, par[200001]{}, dist[200001]{}, V[200001]{};
11+
map<ll, ll> M[200001]{};
12+
vector<vector<pair<int, ll>>> T(200001);
13+
14+
ll ans = 0;
15+
16+
void dfs(int n) {
17+
for (auto[i, c] : T[n]) {
18+
dfs(i);
19+
V[i] += c;
20+
if (M[i].size() > M[n].size()) swap(M[i], M[n]), swap(V[i], V[n]);
21+
for (auto[d, x] : M[i]) M[n][d + V[i] - V[n]] += x;
22+
M[i] = map<ll, ll>();
23+
}
24+
if (H[n] != -1) {
25+
H[n] -= V[n];
26+
auto it = M[n].upper_bound(H[n]);
27+
if (it == M[n].begin()) return;
28+
it--;
29+
vector<ll> er;
30+
while (C[n] > 0) {
31+
ll am = min(it->second, C[n]);
32+
C[n] -= am;
33+
ans += am;
34+
M[n][it->first] -= am;
35+
if (am == it->second) er.push_back(it->first);
36+
if (it == M[n].begin()) break;
37+
it--;
38+
}
39+
for (ll i : er) M[n].erase(i);
40+
}
41+
}
42+
43+
int main() {
44+
cin.tie(0)->sync_with_stdio(0);
45+
46+
cin >> N;
47+
for (int i = 1; i <= N; i++) cin >> C[i];
48+
for (int i = 1; i <= N; i++) cin >> H[i];
49+
for (int i = 2; i <= N; i++) cin >> par[i];
50+
for (int i = 2; i <= N; i++) {
51+
cin >> dist[i];
52+
T[par[i]].emplace_back(i, dist[i]);
53+
}
54+
55+
for (int i = 1; i <= N; i++) if (H[i] == -1) M[i].insert({ 0,C[i] });
56+
dfs(1);
57+
58+
cout << ans;
59+
60+
}
61+
```

0 commit comments

Comments
 (0)