#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define rep(i,a,b) for (int i = (a); i <= (b); i++)
#define repa(i,a,b) for (int i = (a); i >= (b); i--)
#define debug(a) cout << #a << " = " << a << endl
#define debugsl(a) cout << #a << " = " << a << ", "
#define MAX 100000
#define dia first
#define w second
lli n,m,lim,a,b,c,acarreo,jugo;
vector<lli> hijos[MAX+2];
pair<lli,lli> frutas[MAX+2];
set<pair<lli,lli> > subida[MAX+2];
set<pair<lli,lli> >::iterator act,it,pas;
void DFS(lli pos) {
for (auto h : hijos[pos]) {
DFS(h);
if (subida[pos].size() < subida[h].size()) swap(subida[pos], subida[h]);
act = subida[h].begin();
while (act != subida[h].end()) {
a = (*act).dia;
b = (*act).w;
act++;
it = subida[pos].lower_bound({a,0});
if ((*it).dia == a) {
b += (*it).w;
subida[pos].erase(it);
subida[pos].insert({a,b});
}
else subida[pos].insert({a,b});
}
}
if (frutas[pos].dia != 0) {
lli sum = 0;
it = subida[pos].lower_bound({frutas[pos].dia,0});
if (it == subida[pos].end()) subida[pos].insert({frutas[pos].dia, frutas[pos].w});
else {
if ((*it).dia == frutas[pos].dia) {
jugo = (*it).w;
it = subida[pos].erase(it);
}
else jugo = 0;
subida[pos].insert({frutas[pos].dia, frutas[pos].w + jugo});
acarreo = 0;
while (it != subida[pos].end() && (*it).w+acarreo <= frutas[pos].w) {
acarreo += (*it).w;
it = subida[pos].erase(it);
}
if (it != subida[pos].end()) {
a = (*it).dia;
b = (*it).w;
subida[pos].erase(it);
b -= (frutas[pos].w - acarreo);
subida[pos].insert({a,b});
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m >> lim;
rep(i,2,n) {
cin >> a;
hijos[a].push_back(i);
}
rep(i,1,m) {
cin >> a >> b >> c;
frutas[a] = {b,c};
}
DFS(1);
it = subida[1].begin();
a = 0;
while (it != subida[1].end()) {
a += (*it).w;
it++;
}
cout << a << "\n";
}
Compilation message
magictree.cpp: In function 'void DFS(long long int)':
magictree.cpp:45:17: warning: unused variable 'sum' [-Wunused-variable]
45 | lli sum = 0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7376 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7248 KB |
Output is correct |
4 |
Correct |
4 ms |
7376 KB |
Output is correct |
5 |
Correct |
4 ms |
7376 KB |
Output is correct |
6 |
Correct |
4 ms |
7376 KB |
Output is correct |
7 |
Correct |
4 ms |
7248 KB |
Output is correct |
8 |
Correct |
4 ms |
7376 KB |
Output is correct |
9 |
Correct |
4 ms |
7248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
19528 KB |
Output is correct |
2 |
Correct |
48 ms |
19796 KB |
Output is correct |
3 |
Correct |
149 ms |
38296 KB |
Output is correct |
4 |
Correct |
101 ms |
21192 KB |
Output is correct |
5 |
Correct |
139 ms |
23148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7504 KB |
Output is correct |
2 |
Correct |
4 ms |
7516 KB |
Output is correct |
3 |
Correct |
4 ms |
7504 KB |
Output is correct |
4 |
Correct |
56 ms |
27972 KB |
Output is correct |
5 |
Correct |
80 ms |
31944 KB |
Output is correct |
6 |
Correct |
74 ms |
28160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
16480 KB |
Output is correct |
2 |
Correct |
68 ms |
15688 KB |
Output is correct |
3 |
Correct |
62 ms |
20332 KB |
Output is correct |
4 |
Correct |
45 ms |
17004 KB |
Output is correct |
5 |
Correct |
55 ms |
28232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7376 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7248 KB |
Output is correct |
4 |
Correct |
4 ms |
7376 KB |
Output is correct |
5 |
Correct |
4 ms |
7376 KB |
Output is correct |
6 |
Correct |
4 ms |
7376 KB |
Output is correct |
7 |
Correct |
4 ms |
7248 KB |
Output is correct |
8 |
Correct |
4 ms |
7376 KB |
Output is correct |
9 |
Correct |
4 ms |
7248 KB |
Output is correct |
10 |
Correct |
89 ms |
18816 KB |
Output is correct |
11 |
Correct |
80 ms |
17652 KB |
Output is correct |
12 |
Correct |
66 ms |
19656 KB |
Output is correct |
13 |
Correct |
43 ms |
16344 KB |
Output is correct |
14 |
Correct |
60 ms |
27560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
8272 KB |
Output is correct |
2 |
Correct |
30 ms |
11220 KB |
Output is correct |
3 |
Correct |
34 ms |
11244 KB |
Output is correct |
4 |
Correct |
35 ms |
11380 KB |
Output is correct |
5 |
Correct |
13 ms |
9812 KB |
Output is correct |
6 |
Correct |
28 ms |
13892 KB |
Output is correct |
7 |
Correct |
33 ms |
17616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7376 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7248 KB |
Output is correct |
4 |
Correct |
4 ms |
7376 KB |
Output is correct |
5 |
Correct |
4 ms |
7376 KB |
Output is correct |
6 |
Correct |
4 ms |
7376 KB |
Output is correct |
7 |
Correct |
4 ms |
7248 KB |
Output is correct |
8 |
Correct |
4 ms |
7376 KB |
Output is correct |
9 |
Correct |
4 ms |
7248 KB |
Output is correct |
10 |
Correct |
4 ms |
7504 KB |
Output is correct |
11 |
Correct |
4 ms |
7516 KB |
Output is correct |
12 |
Correct |
4 ms |
7504 KB |
Output is correct |
13 |
Correct |
56 ms |
27972 KB |
Output is correct |
14 |
Correct |
80 ms |
31944 KB |
Output is correct |
15 |
Correct |
74 ms |
28160 KB |
Output is correct |
16 |
Correct |
89 ms |
18816 KB |
Output is correct |
17 |
Correct |
80 ms |
17652 KB |
Output is correct |
18 |
Correct |
66 ms |
19656 KB |
Output is correct |
19 |
Correct |
43 ms |
16344 KB |
Output is correct |
20 |
Correct |
60 ms |
27560 KB |
Output is correct |
21 |
Correct |
22 ms |
10840 KB |
Output is correct |
22 |
Correct |
80 ms |
19784 KB |
Output is correct |
23 |
Correct |
92 ms |
23032 KB |
Output is correct |
24 |
Correct |
139 ms |
31276 KB |
Output is correct |
25 |
Correct |
98 ms |
20388 KB |
Output is correct |
26 |
Correct |
109 ms |
22480 KB |
Output is correct |
27 |
Correct |
88 ms |
21572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7376 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7248 KB |
Output is correct |
4 |
Correct |
4 ms |
7376 KB |
Output is correct |
5 |
Correct |
4 ms |
7376 KB |
Output is correct |
6 |
Correct |
4 ms |
7376 KB |
Output is correct |
7 |
Correct |
4 ms |
7248 KB |
Output is correct |
8 |
Correct |
4 ms |
7376 KB |
Output is correct |
9 |
Correct |
4 ms |
7248 KB |
Output is correct |
10 |
Correct |
67 ms |
19528 KB |
Output is correct |
11 |
Correct |
48 ms |
19796 KB |
Output is correct |
12 |
Correct |
149 ms |
38296 KB |
Output is correct |
13 |
Correct |
101 ms |
21192 KB |
Output is correct |
14 |
Correct |
139 ms |
23148 KB |
Output is correct |
15 |
Correct |
4 ms |
7504 KB |
Output is correct |
16 |
Correct |
4 ms |
7516 KB |
Output is correct |
17 |
Correct |
4 ms |
7504 KB |
Output is correct |
18 |
Correct |
56 ms |
27972 KB |
Output is correct |
19 |
Correct |
80 ms |
31944 KB |
Output is correct |
20 |
Correct |
74 ms |
28160 KB |
Output is correct |
21 |
Correct |
69 ms |
16480 KB |
Output is correct |
22 |
Correct |
68 ms |
15688 KB |
Output is correct |
23 |
Correct |
62 ms |
20332 KB |
Output is correct |
24 |
Correct |
45 ms |
17004 KB |
Output is correct |
25 |
Correct |
55 ms |
28232 KB |
Output is correct |
26 |
Correct |
89 ms |
18816 KB |
Output is correct |
27 |
Correct |
80 ms |
17652 KB |
Output is correct |
28 |
Correct |
66 ms |
19656 KB |
Output is correct |
29 |
Correct |
43 ms |
16344 KB |
Output is correct |
30 |
Correct |
60 ms |
27560 KB |
Output is correct |
31 |
Correct |
11 ms |
8272 KB |
Output is correct |
32 |
Correct |
30 ms |
11220 KB |
Output is correct |
33 |
Correct |
34 ms |
11244 KB |
Output is correct |
34 |
Correct |
35 ms |
11380 KB |
Output is correct |
35 |
Correct |
13 ms |
9812 KB |
Output is correct |
36 |
Correct |
28 ms |
13892 KB |
Output is correct |
37 |
Correct |
33 ms |
17616 KB |
Output is correct |
38 |
Correct |
22 ms |
10840 KB |
Output is correct |
39 |
Correct |
80 ms |
19784 KB |
Output is correct |
40 |
Correct |
92 ms |
23032 KB |
Output is correct |
41 |
Correct |
139 ms |
31276 KB |
Output is correct |
42 |
Correct |
98 ms |
20388 KB |
Output is correct |
43 |
Correct |
109 ms |
22480 KB |
Output is correct |
44 |
Correct |
88 ms |
21572 KB |
Output is correct |
45 |
Correct |
27 ms |
11380 KB |
Output is correct |
46 |
Correct |
91 ms |
20760 KB |
Output is correct |
47 |
Correct |
106 ms |
24400 KB |
Output is correct |
48 |
Correct |
161 ms |
34284 KB |
Output is correct |
49 |
Correct |
100 ms |
21080 KB |
Output is correct |
50 |
Correct |
125 ms |
23528 KB |
Output is correct |
51 |
Correct |
110 ms |
22456 KB |
Output is correct |