#include <bits/stdc++.h>
using namespace std;
#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0, _n = n; i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define FORE(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define debug(...) "[" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
template <class A, class B> bool minimize(A &a, B b) { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b) { if (a < b) { a = b; return true; } return false; }
const int MAX = 1e5 + 5;
int N, M, K;
map <int, long long> dp[MAX];
vector <int> adj[MAX];
pair <int, int> A[MAX];
void insert(int x, int d, int w) {
dp[x][d] += w;
auto it = dp[x].upper_bound(d);
while(it != dp[x].end() && it->second <= w) {
w -= it->second;
it = dp[x].erase(it);
}
if(it != dp[x].end() && it->second >= w) {
it->second -= w;
}
}
void dfs(int u) {
for (auto v : adj[u]) {
dfs(v);
if((int) dp[u].size() < (int) dp[v].size()) dp[u].swap(dp[v]);
for (auto [x, d] : dp[v]) {
dp[u][x] += d;
}
dp[v].clear();
}
if(A[u].first != 0) insert(u, A[u].first, A[u].second);
}
void you_make_it(void) {
cin >> N >> M >> K;
FOR(i, 2, N) {
int u; cin >> u;
adj[u].emplace_back(i);
}
FOR(i, 1, M) {
int u; cin >> u >> A[u].first >> A[u].second;
}
dfs(1);
long long ans = 0;
for (auto tmp : dp[1]) ans += tmp.second;
cout << ans;
}
signed main() {
#ifdef LOCAL
freopen("TASK.inp", "r", stdin);
freopen("TASK.out", "w", stdout);
#endif
file("magictree");
auto start_time = chrono::steady_clock::now();
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
you_make_it();
auto end_time = chrono::steady_clock::now();
cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl;
return (0 ^ 0);
}
// Dream it. Wish it. Do it.
Compilation message
magictree.cpp: In function 'int main()':
magictree.cpp:13:57: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
magictree.cpp:75:5: note: in expansion of macro 'file'
75 | file("magictree");
| ^~~~
magictree.cpp:13:90: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
magictree.cpp:75:5: note: in expansion of macro 'file'
75 | file("magictree");
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8028 KB |
Output is correct |
2 |
Correct |
2 ms |
8028 KB |
Output is correct |
3 |
Correct |
2 ms |
8024 KB |
Output is correct |
4 |
Correct |
2 ms |
8024 KB |
Output is correct |
5 |
Correct |
2 ms |
8028 KB |
Output is correct |
6 |
Correct |
2 ms |
8024 KB |
Output is correct |
7 |
Correct |
2 ms |
8028 KB |
Output is correct |
8 |
Correct |
2 ms |
8028 KB |
Output is correct |
9 |
Correct |
2 ms |
8028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
12624 KB |
Output is correct |
2 |
Correct |
28 ms |
19548 KB |
Output is correct |
3 |
Correct |
86 ms |
15896 KB |
Output is correct |
4 |
Correct |
51 ms |
14288 KB |
Output is correct |
5 |
Correct |
82 ms |
16736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8284 KB |
Output is correct |
2 |
Correct |
2 ms |
8284 KB |
Output is correct |
3 |
Correct |
2 ms |
8284 KB |
Output is correct |
4 |
Correct |
44 ms |
28752 KB |
Output is correct |
5 |
Correct |
57 ms |
32900 KB |
Output is correct |
6 |
Correct |
50 ms |
28756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
12116 KB |
Output is correct |
2 |
Correct |
36 ms |
11856 KB |
Output is correct |
3 |
Correct |
35 ms |
19024 KB |
Output is correct |
4 |
Correct |
27 ms |
10452 KB |
Output is correct |
5 |
Correct |
36 ms |
29184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8028 KB |
Output is correct |
2 |
Correct |
2 ms |
8028 KB |
Output is correct |
3 |
Correct |
2 ms |
8024 KB |
Output is correct |
4 |
Correct |
2 ms |
8024 KB |
Output is correct |
5 |
Correct |
2 ms |
8028 KB |
Output is correct |
6 |
Correct |
2 ms |
8024 KB |
Output is correct |
7 |
Correct |
2 ms |
8028 KB |
Output is correct |
8 |
Correct |
2 ms |
8028 KB |
Output is correct |
9 |
Correct |
2 ms |
8028 KB |
Output is correct |
10 |
Correct |
45 ms |
11356 KB |
Output is correct |
11 |
Correct |
46 ms |
11096 KB |
Output is correct |
12 |
Correct |
35 ms |
18520 KB |
Output is correct |
13 |
Correct |
23 ms |
9692 KB |
Output is correct |
14 |
Correct |
36 ms |
28508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8536 KB |
Output is correct |
2 |
Correct |
15 ms |
10332 KB |
Output is correct |
3 |
Correct |
18 ms |
10332 KB |
Output is correct |
4 |
Correct |
16 ms |
10332 KB |
Output is correct |
5 |
Correct |
9 ms |
9032 KB |
Output is correct |
6 |
Correct |
17 ms |
13404 KB |
Output is correct |
7 |
Correct |
18 ms |
17500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8028 KB |
Output is correct |
2 |
Correct |
2 ms |
8028 KB |
Output is correct |
3 |
Correct |
2 ms |
8024 KB |
Output is correct |
4 |
Correct |
2 ms |
8024 KB |
Output is correct |
5 |
Correct |
2 ms |
8028 KB |
Output is correct |
6 |
Correct |
2 ms |
8024 KB |
Output is correct |
7 |
Correct |
2 ms |
8028 KB |
Output is correct |
8 |
Correct |
2 ms |
8028 KB |
Output is correct |
9 |
Correct |
2 ms |
8028 KB |
Output is correct |
10 |
Correct |
2 ms |
8284 KB |
Output is correct |
11 |
Correct |
2 ms |
8284 KB |
Output is correct |
12 |
Correct |
2 ms |
8284 KB |
Output is correct |
13 |
Correct |
44 ms |
28752 KB |
Output is correct |
14 |
Correct |
57 ms |
32900 KB |
Output is correct |
15 |
Correct |
50 ms |
28756 KB |
Output is correct |
16 |
Correct |
45 ms |
11356 KB |
Output is correct |
17 |
Correct |
46 ms |
11096 KB |
Output is correct |
18 |
Correct |
35 ms |
18520 KB |
Output is correct |
19 |
Correct |
23 ms |
9692 KB |
Output is correct |
20 |
Correct |
36 ms |
28508 KB |
Output is correct |
21 |
Correct |
13 ms |
9048 KB |
Output is correct |
22 |
Correct |
47 ms |
11372 KB |
Output is correct |
23 |
Correct |
44 ms |
13124 KB |
Output is correct |
24 |
Correct |
66 ms |
14756 KB |
Output is correct |
25 |
Correct |
53 ms |
13808 KB |
Output is correct |
26 |
Correct |
58 ms |
18512 KB |
Output is correct |
27 |
Correct |
52 ms |
20180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8028 KB |
Output is correct |
2 |
Correct |
2 ms |
8028 KB |
Output is correct |
3 |
Correct |
2 ms |
8024 KB |
Output is correct |
4 |
Correct |
2 ms |
8024 KB |
Output is correct |
5 |
Correct |
2 ms |
8028 KB |
Output is correct |
6 |
Correct |
2 ms |
8024 KB |
Output is correct |
7 |
Correct |
2 ms |
8028 KB |
Output is correct |
8 |
Correct |
2 ms |
8028 KB |
Output is correct |
9 |
Correct |
2 ms |
8028 KB |
Output is correct |
10 |
Correct |
35 ms |
12624 KB |
Output is correct |
11 |
Correct |
28 ms |
19548 KB |
Output is correct |
12 |
Correct |
86 ms |
15896 KB |
Output is correct |
13 |
Correct |
51 ms |
14288 KB |
Output is correct |
14 |
Correct |
82 ms |
16736 KB |
Output is correct |
15 |
Correct |
2 ms |
8284 KB |
Output is correct |
16 |
Correct |
2 ms |
8284 KB |
Output is correct |
17 |
Correct |
2 ms |
8284 KB |
Output is correct |
18 |
Correct |
44 ms |
28752 KB |
Output is correct |
19 |
Correct |
57 ms |
32900 KB |
Output is correct |
20 |
Correct |
50 ms |
28756 KB |
Output is correct |
21 |
Correct |
50 ms |
12116 KB |
Output is correct |
22 |
Correct |
36 ms |
11856 KB |
Output is correct |
23 |
Correct |
35 ms |
19024 KB |
Output is correct |
24 |
Correct |
27 ms |
10452 KB |
Output is correct |
25 |
Correct |
36 ms |
29184 KB |
Output is correct |
26 |
Correct |
45 ms |
11356 KB |
Output is correct |
27 |
Correct |
46 ms |
11096 KB |
Output is correct |
28 |
Correct |
35 ms |
18520 KB |
Output is correct |
29 |
Correct |
23 ms |
9692 KB |
Output is correct |
30 |
Correct |
36 ms |
28508 KB |
Output is correct |
31 |
Correct |
5 ms |
8536 KB |
Output is correct |
32 |
Correct |
15 ms |
10332 KB |
Output is correct |
33 |
Correct |
18 ms |
10332 KB |
Output is correct |
34 |
Correct |
16 ms |
10332 KB |
Output is correct |
35 |
Correct |
9 ms |
9032 KB |
Output is correct |
36 |
Correct |
17 ms |
13404 KB |
Output is correct |
37 |
Correct |
18 ms |
17500 KB |
Output is correct |
38 |
Correct |
13 ms |
9048 KB |
Output is correct |
39 |
Correct |
47 ms |
11372 KB |
Output is correct |
40 |
Correct |
44 ms |
13124 KB |
Output is correct |
41 |
Correct |
66 ms |
14756 KB |
Output is correct |
42 |
Correct |
53 ms |
13808 KB |
Output is correct |
43 |
Correct |
58 ms |
18512 KB |
Output is correct |
44 |
Correct |
52 ms |
20180 KB |
Output is correct |
45 |
Correct |
13 ms |
9052 KB |
Output is correct |
46 |
Correct |
41 ms |
11752 KB |
Output is correct |
47 |
Correct |
58 ms |
13480 KB |
Output is correct |
48 |
Correct |
78 ms |
16016 KB |
Output is correct |
49 |
Correct |
47 ms |
14284 KB |
Output is correct |
50 |
Correct |
61 ms |
19436 KB |
Output is correct |
51 |
Correct |
56 ms |
21072 KB |
Output is correct |