#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#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 lli long long int
#define pll pair<lli,lli>
#define MAX 300000
//para los hijos
#define des first
#define w second
struct x {
multiset<lli> s;
lli pend;
lli ord;
};
lli n,m,a,b,res;
vector<pll> hijos[MAX+2];
lli si[MAX+2];
multiset<lli>::iterator it;
x solve(lli pos,lli dis) {
x slope;
slope.s.clear();
if (pos > n) { // es un explosivo
slope.s.insert(dis);
slope.s.insert(dis);
slope.pend = 1;
slope.ord = -dis;
return slope;
}
slope.pend = 0;
slope.ord = 0;
for (auto h : hijos[pos]) {
if (si[h.des] == 0) continue;
x act = solve(h.des, h.w);
// aqui mergeo
if (act.s.size() > slope.s.size()) {
swap(act.s, slope.s);
swap(act.pend, slope.pend);
swap(act.ord, slope.ord);
}
for (auto k : act.s) slope.s.insert(k);
slope.pend += act.pend;
slope.ord += act.ord;
}
while (slope.pend > 1) {
it = slope.s.end();
it--;
slope.ord += (*it);
slope.s.erase(it);
slope.pend--;
}
// aqui hago la magia del slope
slope.ord -= dis;
it = slope.s.end();
it--;
lli a = (*it) + dis;
slope.s.erase(it);
it = slope.s.end();
it--;
lli b = (*it) + dis;
slope.s.erase(it);
slope.s.insert(a);
slope.s.insert(b);
return slope;
}
void chequeo(lli pos) {
if (pos > n) {si[pos] = 1; return;}
for (auto h : hijos[pos]) {
chequeo(h.des);
si[pos] = (si[pos]|si[h.des]);
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
rep(i,2,n) {
cin >> a >> b;
hijos[a].push_back({i,b});
}
res = 0;
rep(i,n+1,n+m) {
cin >> a >> b;
if (a > n) res += b;
else hijos[a].push_back({i,b});
}
//eliminar aristas que no tengan ningún explosivo debajo
chequeo(1);
x fin = solve(1,0);
it = fin.s.end();
it--;
fin.ord += (*it);
res += fin.ord;
cout << res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7368 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7376 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7372 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
5 ms |
7368 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
5 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7372 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7368 KB |
Output is correct |
9 |
Correct |
4 ms |
7372 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
11 |
Correct |
4 ms |
7368 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
4 ms |
7376 KB |
Output is correct |
14 |
Correct |
4 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
5 ms |
7372 KB |
Output is correct |
17 |
Correct |
4 ms |
7380 KB |
Output is correct |
18 |
Correct |
4 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7368 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7376 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7372 KB |
Output is correct |
11 |
Correct |
4 ms |
7372 KB |
Output is correct |
12 |
Correct |
4 ms |
7252 KB |
Output is correct |
13 |
Correct |
5 ms |
7368 KB |
Output is correct |
14 |
Correct |
4 ms |
7380 KB |
Output is correct |
15 |
Correct |
5 ms |
7380 KB |
Output is correct |
16 |
Correct |
4 ms |
7372 KB |
Output is correct |
17 |
Correct |
4 ms |
7380 KB |
Output is correct |
18 |
Correct |
4 ms |
7368 KB |
Output is correct |
19 |
Correct |
4 ms |
7372 KB |
Output is correct |
20 |
Correct |
4 ms |
7380 KB |
Output is correct |
21 |
Correct |
4 ms |
7368 KB |
Output is correct |
22 |
Correct |
4 ms |
7380 KB |
Output is correct |
23 |
Correct |
4 ms |
7376 KB |
Output is correct |
24 |
Correct |
4 ms |
7380 KB |
Output is correct |
25 |
Correct |
4 ms |
7380 KB |
Output is correct |
26 |
Correct |
5 ms |
7372 KB |
Output is correct |
27 |
Correct |
4 ms |
7380 KB |
Output is correct |
28 |
Correct |
4 ms |
7380 KB |
Output is correct |
29 |
Correct |
4 ms |
7380 KB |
Output is correct |
30 |
Correct |
4 ms |
7380 KB |
Output is correct |
31 |
Correct |
6 ms |
7380 KB |
Output is correct |
32 |
Correct |
5 ms |
7380 KB |
Output is correct |
33 |
Correct |
5 ms |
7508 KB |
Output is correct |
34 |
Correct |
5 ms |
7512 KB |
Output is correct |
35 |
Correct |
6 ms |
7640 KB |
Output is correct |
36 |
Correct |
9 ms |
7644 KB |
Output is correct |
37 |
Correct |
7 ms |
7636 KB |
Output is correct |
38 |
Correct |
8 ms |
7644 KB |
Output is correct |
39 |
Correct |
7 ms |
7640 KB |
Output is correct |
40 |
Correct |
6 ms |
8532 KB |
Output is correct |
41 |
Correct |
7 ms |
8580 KB |
Output is correct |
42 |
Correct |
6 ms |
7592 KB |
Output is correct |
43 |
Correct |
7 ms |
8104 KB |
Output is correct |
44 |
Correct |
7 ms |
8020 KB |
Output is correct |
45 |
Correct |
6 ms |
8020 KB |
Output is correct |
46 |
Correct |
7 ms |
7772 KB |
Output is correct |
47 |
Correct |
8 ms |
7764 KB |
Output is correct |
48 |
Correct |
6 ms |
7768 KB |
Output is correct |
49 |
Correct |
7 ms |
7760 KB |
Output is correct |
50 |
Correct |
7 ms |
7716 KB |
Output is correct |
51 |
Correct |
6 ms |
7720 KB |
Output is correct |
52 |
Correct |
7 ms |
7768 KB |
Output is correct |
53 |
Correct |
8 ms |
7764 KB |
Output is correct |
54 |
Correct |
8 ms |
8004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7368 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7376 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7372 KB |
Output is correct |
11 |
Correct |
4 ms |
7372 KB |
Output is correct |
12 |
Correct |
4 ms |
7252 KB |
Output is correct |
13 |
Correct |
5 ms |
7368 KB |
Output is correct |
14 |
Correct |
4 ms |
7380 KB |
Output is correct |
15 |
Correct |
5 ms |
7380 KB |
Output is correct |
16 |
Correct |
4 ms |
7372 KB |
Output is correct |
17 |
Correct |
4 ms |
7380 KB |
Output is correct |
18 |
Correct |
4 ms |
7368 KB |
Output is correct |
19 |
Correct |
4 ms |
7372 KB |
Output is correct |
20 |
Correct |
4 ms |
7380 KB |
Output is correct |
21 |
Correct |
4 ms |
7368 KB |
Output is correct |
22 |
Correct |
4 ms |
7380 KB |
Output is correct |
23 |
Correct |
4 ms |
7376 KB |
Output is correct |
24 |
Correct |
4 ms |
7380 KB |
Output is correct |
25 |
Correct |
4 ms |
7380 KB |
Output is correct |
26 |
Correct |
5 ms |
7372 KB |
Output is correct |
27 |
Correct |
4 ms |
7380 KB |
Output is correct |
28 |
Correct |
4 ms |
7380 KB |
Output is correct |
29 |
Correct |
4 ms |
7380 KB |
Output is correct |
30 |
Correct |
4 ms |
7380 KB |
Output is correct |
31 |
Correct |
6 ms |
7380 KB |
Output is correct |
32 |
Correct |
5 ms |
7380 KB |
Output is correct |
33 |
Correct |
5 ms |
7508 KB |
Output is correct |
34 |
Correct |
5 ms |
7512 KB |
Output is correct |
35 |
Correct |
6 ms |
7640 KB |
Output is correct |
36 |
Correct |
9 ms |
7644 KB |
Output is correct |
37 |
Correct |
7 ms |
7636 KB |
Output is correct |
38 |
Correct |
8 ms |
7644 KB |
Output is correct |
39 |
Correct |
7 ms |
7640 KB |
Output is correct |
40 |
Correct |
6 ms |
8532 KB |
Output is correct |
41 |
Correct |
7 ms |
8580 KB |
Output is correct |
42 |
Correct |
6 ms |
7592 KB |
Output is correct |
43 |
Correct |
7 ms |
8104 KB |
Output is correct |
44 |
Correct |
7 ms |
8020 KB |
Output is correct |
45 |
Correct |
6 ms |
8020 KB |
Output is correct |
46 |
Correct |
7 ms |
7772 KB |
Output is correct |
47 |
Correct |
8 ms |
7764 KB |
Output is correct |
48 |
Correct |
6 ms |
7768 KB |
Output is correct |
49 |
Correct |
7 ms |
7760 KB |
Output is correct |
50 |
Correct |
7 ms |
7716 KB |
Output is correct |
51 |
Correct |
6 ms |
7720 KB |
Output is correct |
52 |
Correct |
7 ms |
7768 KB |
Output is correct |
53 |
Correct |
8 ms |
7764 KB |
Output is correct |
54 |
Correct |
8 ms |
8004 KB |
Output is correct |
55 |
Correct |
11 ms |
8276 KB |
Output is correct |
56 |
Correct |
35 ms |
10676 KB |
Output is correct |
57 |
Correct |
62 ms |
13344 KB |
Output is correct |
58 |
Correct |
99 ms |
15036 KB |
Output is correct |
59 |
Correct |
111 ms |
17724 KB |
Output is correct |
60 |
Correct |
155 ms |
19868 KB |
Output is correct |
61 |
Correct |
192 ms |
21848 KB |
Output is correct |
62 |
Correct |
191 ms |
24012 KB |
Output is correct |
63 |
Correct |
249 ms |
26768 KB |
Output is correct |
64 |
Correct |
264 ms |
27184 KB |
Output is correct |
65 |
Correct |
141 ms |
80356 KB |
Output is correct |
66 |
Correct |
135 ms |
80260 KB |
Output is correct |
67 |
Correct |
124 ms |
24136 KB |
Output is correct |
68 |
Correct |
196 ms |
56712 KB |
Output is correct |
69 |
Correct |
219 ms |
52684 KB |
Output is correct |
70 |
Correct |
221 ms |
52684 KB |
Output is correct |
71 |
Correct |
271 ms |
34996 KB |
Output is correct |
72 |
Correct |
245 ms |
35084 KB |
Output is correct |
73 |
Correct |
272 ms |
33744 KB |
Output is correct |
74 |
Correct |
257 ms |
33904 KB |
Output is correct |
75 |
Correct |
265 ms |
33000 KB |
Output is correct |
76 |
Correct |
252 ms |
33024 KB |
Output is correct |
77 |
Correct |
287 ms |
32880 KB |
Output is correct |
78 |
Correct |
290 ms |
33380 KB |
Output is correct |
79 |
Correct |
313 ms |
46076 KB |
Output is correct |