from heapq import *
class Maxheap:
def __init__(_): _.h = []
def add(_, v): heappush(_.h, -v)
def top(_): return -_.h[0] if _.h else -10**18
def pop(_): return -heappop(_.h)
class Graph:
def __init__(_):
_.change = Maxheap() # increment slope at ...
_.a = _.y = 0 # last line has slope a, starts from y
_.dx = 0 # the whole graph is shifted right by ...
def __iadd__(s, o):
if len(s.change.h) < len(o.change.h): s,o = o,s
dx = s.change.top() - o.change.top()
s.a, s.y = s.a+o.a, s.y+o.y+(o.a*dx if dx>=0 else -s.a*dx)
for v in o.change.h: s.change.add(-v + o.dx-s.dx)
return s
def shiftx(_, v): _.dx+= v
def shifty(_, v): _.y+= v
def addleft(_, v):
if _.change.top() < v-_.dx:
dx = v-_.dx - _.change.top()
_.y+= _.a*dx
_.change.add(v-_.dx)
def addright(_, v):
if _.change.top() < v-_.dx:
dx = v-_.dx - _.change.top()
_.y+= _.a*dx; _.a+= 1
_.change.add(v-_.dx)
return
_.change.add(v-_.dx)
_.a+= 1; _.y+= _.change.top()-(v-_.dx)
def cutright(_):
v = _.change.pop(); rval = v+_.dx
dx = v-_.change.top()
_.a-= 1; _.y-= _.a*dx
return rval
input = __import__('sys').stdin.readline
range = xrange
MIS = lambda: map(int,input().split())
n = sum(MIS())
C = [0, 0]
adj = [[] for i in range(n+1)]
adj[0].append(1)
for i in range(n-1):
p, c = MIS()
C.append(c)
adj[p].append(i+2)
# dfs because python bad at recursion
dfsord = []
stack = [0]
while stack:
p = stack.pop()
for q in adj[p]: dfsord.append(q); stack.append(q)
del stack
# slope trick
graph = [None for i in range(n+2)]
for v in reversed(dfsord):
G = Graph()
if not adj[v]:
G.addleft(C[v])
G.addright(C[v])
graph[v] = G
continue
for u in adj[v]: G+= graph[u]
while G.a > 1: G.cutright()
s1 = G.change.pop()
s2 = G.change.pop()
G.change.add(s1+C[v])
G.change.add(s2+C[v])
graph[v] = G
print(G.y)
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
5080 KB |
Output is correct |
2 |
Correct |
28 ms |
5208 KB |
Output is correct |
3 |
Correct |
34 ms |
5208 KB |
Output is correct |
4 |
Correct |
33 ms |
5208 KB |
Output is correct |
5 |
Correct |
34 ms |
5208 KB |
Output is correct |
6 |
Correct |
34 ms |
5208 KB |
Output is correct |
7 |
Correct |
36 ms |
5208 KB |
Output is correct |
8 |
Correct |
35 ms |
5208 KB |
Output is correct |
9 |
Correct |
33 ms |
5208 KB |
Output is correct |
10 |
Correct |
35 ms |
5208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
5080 KB |
Output is correct |
2 |
Correct |
30 ms |
5080 KB |
Output is correct |
3 |
Correct |
31 ms |
5080 KB |
Output is correct |
4 |
Correct |
32 ms |
5084 KB |
Output is correct |
5 |
Correct |
34 ms |
5208 KB |
Output is correct |
6 |
Correct |
36 ms |
5208 KB |
Output is correct |
7 |
Correct |
38 ms |
5208 KB |
Output is correct |
8 |
Correct |
38 ms |
5208 KB |
Output is correct |
9 |
Correct |
39 ms |
5208 KB |
Output is correct |
10 |
Correct |
40 ms |
5976 KB |
Output is correct |
11 |
Correct |
44 ms |
5208 KB |
Output is correct |
12 |
Correct |
46 ms |
5336 KB |
Output is correct |
13 |
Correct |
44 ms |
5208 KB |
Output is correct |
14 |
Correct |
39 ms |
5208 KB |
Output is correct |
15 |
Correct |
45 ms |
5212 KB |
Output is correct |
16 |
Correct |
46 ms |
5208 KB |
Output is correct |
17 |
Correct |
57 ms |
5212 KB |
Output is correct |
18 |
Correct |
46 ms |
5208 KB |
Output is correct |
19 |
Correct |
48 ms |
5336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
5080 KB |
Output is correct |
2 |
Correct |
28 ms |
5208 KB |
Output is correct |
3 |
Correct |
34 ms |
5208 KB |
Output is correct |
4 |
Correct |
33 ms |
5208 KB |
Output is correct |
5 |
Correct |
34 ms |
5208 KB |
Output is correct |
6 |
Correct |
34 ms |
5208 KB |
Output is correct |
7 |
Correct |
36 ms |
5208 KB |
Output is correct |
8 |
Correct |
35 ms |
5208 KB |
Output is correct |
9 |
Correct |
33 ms |
5208 KB |
Output is correct |
10 |
Correct |
35 ms |
5208 KB |
Output is correct |
11 |
Correct |
29 ms |
5080 KB |
Output is correct |
12 |
Correct |
30 ms |
5080 KB |
Output is correct |
13 |
Correct |
31 ms |
5080 KB |
Output is correct |
14 |
Correct |
32 ms |
5084 KB |
Output is correct |
15 |
Correct |
34 ms |
5208 KB |
Output is correct |
16 |
Correct |
36 ms |
5208 KB |
Output is correct |
17 |
Correct |
38 ms |
5208 KB |
Output is correct |
18 |
Correct |
38 ms |
5208 KB |
Output is correct |
19 |
Correct |
39 ms |
5208 KB |
Output is correct |
20 |
Correct |
40 ms |
5976 KB |
Output is correct |
21 |
Correct |
44 ms |
5208 KB |
Output is correct |
22 |
Correct |
46 ms |
5336 KB |
Output is correct |
23 |
Correct |
44 ms |
5208 KB |
Output is correct |
24 |
Correct |
39 ms |
5208 KB |
Output is correct |
25 |
Correct |
45 ms |
5212 KB |
Output is correct |
26 |
Correct |
46 ms |
5208 KB |
Output is correct |
27 |
Correct |
57 ms |
5212 KB |
Output is correct |
28 |
Correct |
46 ms |
5208 KB |
Output is correct |
29 |
Correct |
48 ms |
5336 KB |
Output is correct |
30 |
Correct |
63 ms |
7512 KB |
Output is correct |
31 |
Correct |
130 ms |
12804 KB |
Output is correct |
32 |
Correct |
191 ms |
14732 KB |
Output is correct |
33 |
Correct |
211 ms |
14852 KB |
Output is correct |
34 |
Correct |
294 ms |
16016 KB |
Output is correct |
35 |
Correct |
300 ms |
16260 KB |
Output is correct |
36 |
Correct |
292 ms |
15880 KB |
Output is correct |
37 |
Correct |
339 ms |
16260 KB |
Output is correct |
38 |
Correct |
329 ms |
15748 KB |
Output is correct |
39 |
Correct |
338 ms |
16260 KB |
Output is correct |
40 |
Correct |
113 ms |
11528 KB |
Output is correct |
41 |
Correct |
112 ms |
11524 KB |
Output is correct |
42 |
Correct |
126 ms |
12932 KB |
Output is correct |
43 |
Correct |
313 ms |
16516 KB |
Output is correct |
44 |
Correct |
314 ms |
16260 KB |
Output is correct |
45 |
Correct |
308 ms |
16260 KB |
Output is correct |
46 |
Correct |
248 ms |
16780 KB |
Output is correct |
47 |
Correct |
261 ms |
15364 KB |
Output is correct |
48 |
Correct |
284 ms |
15620 KB |
Output is correct |
49 |
Correct |
287 ms |
16904 KB |
Output is correct |
50 |
Correct |
262 ms |
16264 KB |
Output is correct |
51 |
Correct |
262 ms |
15364 KB |
Output is correct |
52 |
Correct |
217 ms |
16644 KB |
Output is correct |
53 |
Correct |
247 ms |
15752 KB |
Output is correct |
54 |
Correct |
206 ms |
15364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
5080 KB |
Output is correct |
2 |
Correct |
28 ms |
5208 KB |
Output is correct |
3 |
Correct |
34 ms |
5208 KB |
Output is correct |
4 |
Correct |
33 ms |
5208 KB |
Output is correct |
5 |
Correct |
34 ms |
5208 KB |
Output is correct |
6 |
Correct |
34 ms |
5208 KB |
Output is correct |
7 |
Correct |
36 ms |
5208 KB |
Output is correct |
8 |
Correct |
35 ms |
5208 KB |
Output is correct |
9 |
Correct |
33 ms |
5208 KB |
Output is correct |
10 |
Correct |
35 ms |
5208 KB |
Output is correct |
11 |
Correct |
29 ms |
5080 KB |
Output is correct |
12 |
Correct |
30 ms |
5080 KB |
Output is correct |
13 |
Correct |
31 ms |
5080 KB |
Output is correct |
14 |
Correct |
32 ms |
5084 KB |
Output is correct |
15 |
Correct |
34 ms |
5208 KB |
Output is correct |
16 |
Correct |
36 ms |
5208 KB |
Output is correct |
17 |
Correct |
38 ms |
5208 KB |
Output is correct |
18 |
Correct |
38 ms |
5208 KB |
Output is correct |
19 |
Correct |
39 ms |
5208 KB |
Output is correct |
20 |
Correct |
40 ms |
5976 KB |
Output is correct |
21 |
Correct |
44 ms |
5208 KB |
Output is correct |
22 |
Correct |
46 ms |
5336 KB |
Output is correct |
23 |
Correct |
44 ms |
5208 KB |
Output is correct |
24 |
Correct |
39 ms |
5208 KB |
Output is correct |
25 |
Correct |
45 ms |
5212 KB |
Output is correct |
26 |
Correct |
46 ms |
5208 KB |
Output is correct |
27 |
Correct |
57 ms |
5212 KB |
Output is correct |
28 |
Correct |
46 ms |
5208 KB |
Output is correct |
29 |
Correct |
48 ms |
5336 KB |
Output is correct |
30 |
Correct |
63 ms |
7512 KB |
Output is correct |
31 |
Correct |
130 ms |
12804 KB |
Output is correct |
32 |
Correct |
191 ms |
14732 KB |
Output is correct |
33 |
Correct |
211 ms |
14852 KB |
Output is correct |
34 |
Correct |
294 ms |
16016 KB |
Output is correct |
35 |
Correct |
300 ms |
16260 KB |
Output is correct |
36 |
Correct |
292 ms |
15880 KB |
Output is correct |
37 |
Correct |
339 ms |
16260 KB |
Output is correct |
38 |
Correct |
329 ms |
15748 KB |
Output is correct |
39 |
Correct |
338 ms |
16260 KB |
Output is correct |
40 |
Correct |
113 ms |
11528 KB |
Output is correct |
41 |
Correct |
112 ms |
11524 KB |
Output is correct |
42 |
Correct |
126 ms |
12932 KB |
Output is correct |
43 |
Correct |
313 ms |
16516 KB |
Output is correct |
44 |
Correct |
314 ms |
16260 KB |
Output is correct |
45 |
Correct |
308 ms |
16260 KB |
Output is correct |
46 |
Correct |
248 ms |
16780 KB |
Output is correct |
47 |
Correct |
261 ms |
15364 KB |
Output is correct |
48 |
Correct |
284 ms |
15620 KB |
Output is correct |
49 |
Correct |
287 ms |
16904 KB |
Output is correct |
50 |
Correct |
262 ms |
16264 KB |
Output is correct |
51 |
Correct |
262 ms |
15364 KB |
Output is correct |
52 |
Correct |
217 ms |
16644 KB |
Output is correct |
53 |
Correct |
247 ms |
15752 KB |
Output is correct |
54 |
Correct |
206 ms |
15364 KB |
Output is correct |
55 |
Correct |
533 ms |
18708 KB |
Output is correct |
56 |
Correct |
811 ms |
29700 KB |
Output is correct |
57 |
Correct |
993 ms |
40448 KB |
Output is correct |
58 |
Correct |
852 ms |
43576 KB |
Output is correct |
59 |
Correct |
1038 ms |
54276 KB |
Output is correct |
60 |
Correct |
1155 ms |
64388 KB |
Output is correct |
61 |
Correct |
1451 ms |
73736 KB |
Output is correct |
62 |
Correct |
1378 ms |
78212 KB |
Output is correct |
63 |
Correct |
1494 ms |
91812 KB |
Output is correct |
64 |
Correct |
1435 ms |
94288 KB |
Output is correct |
65 |
Correct |
427 ms |
77484 KB |
Output is correct |
66 |
Correct |
432 ms |
77492 KB |
Output is correct |
67 |
Correct |
635 ms |
77872 KB |
Output is correct |
68 |
Correct |
998 ms |
102216 KB |
Output is correct |
69 |
Correct |
1283 ms |
105768 KB |
Output is correct |
70 |
Correct |
1202 ms |
108156 KB |
Output is correct |
71 |
Correct |
1187 ms |
152444 KB |
Output is correct |
72 |
Correct |
1163 ms |
149948 KB |
Output is correct |
73 |
Correct |
1180 ms |
154256 KB |
Output is correct |
74 |
Correct |
1193 ms |
158228 KB |
Output is correct |
75 |
Correct |
1172 ms |
163532 KB |
Output is correct |
76 |
Correct |
1184 ms |
157976 KB |
Output is correct |
77 |
Correct |
1199 ms |
171680 KB |
Output is correct |
78 |
Correct |
1089 ms |
154372 KB |
Output is correct |
79 |
Correct |
1020 ms |
161228 KB |
Output is correct |