#include <bits/stdc++.h>
using namespace std;
void just_do_it();
int main() {
#ifdef KAMIRULEZ
freopen("kamirulez.inp", "r", stdin);
freopen("kamirulez.out", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
just_do_it();
return 0;
}
struct func {
long long slope = 0;
long long last = 0;
priority_queue<long long> q;
void init(int w) {
slope = 1;
last = 0;
q.push(w);
q.push(w);
}
long long update(int w) {
long long lt = -1;
long long rt = -1;
while (slope > 0) {
long long cur = q.top();
q.pop();
long long prv = q.top();
if (slope == 1) {
lt = prv;
rt = cur;
}
slope--;
last -= slope * (cur - prv);
}
q.pop();
q.push(lt + w);
q.push(rt + w);
slope = 1;
return last;
}
void merge(func &f2) {
if (q.empty()) {
slope = f2.slope;
last = f2.last;
swap(q, f2.q);
return;
}
if (q.top() > f2.q.top()) {
last = last + (f2.last + f2.slope * (q.top() - f2.q.top()));
}
else {
last = f2.last + (last + slope * (f2.q.top() - q.top()));
}
slope += f2.slope;
if (q.size() < f2.q.size()) {
swap(q, f2.q);
}
while (!f2.q.empty()) {
q.push(f2.q.top());
f2.q.pop();
}
}
};
const int maxN = 3e5 + 20;
vector<pair<int, int>> ch[maxN];
func f[maxN];
int N, M;
void dfs(int u) {
for (auto p: ch[u]) {
int v = p.first;
int w = p.second;
if (N + 1 <= v && v <= N + M) {
f[v].init(w);
}
else {
dfs(v);
f[v].update(w);
}
f[u].merge(f[v]);
}
}
void just_do_it() {
cin >> N >> M;
for (int v = 2; v <= N + M; v++) {
int u, w;
cin >> u >> w;
ch[u].emplace_back(v, w);
}
dfs(1);
cout << f[1].update(0);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
21332 KB |
Output is correct |
2 |
Correct |
14 ms |
21328 KB |
Output is correct |
3 |
Correct |
11 ms |
21460 KB |
Output is correct |
4 |
Correct |
10 ms |
21464 KB |
Output is correct |
5 |
Correct |
10 ms |
21460 KB |
Output is correct |
6 |
Correct |
10 ms |
21460 KB |
Output is correct |
7 |
Correct |
11 ms |
21460 KB |
Output is correct |
8 |
Correct |
11 ms |
21472 KB |
Output is correct |
9 |
Correct |
13 ms |
21468 KB |
Output is correct |
10 |
Correct |
12 ms |
21460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
21372 KB |
Output is correct |
2 |
Correct |
10 ms |
21460 KB |
Output is correct |
3 |
Correct |
11 ms |
21460 KB |
Output is correct |
4 |
Correct |
10 ms |
21460 KB |
Output is correct |
5 |
Correct |
11 ms |
21456 KB |
Output is correct |
6 |
Correct |
11 ms |
21460 KB |
Output is correct |
7 |
Correct |
13 ms |
21332 KB |
Output is correct |
8 |
Correct |
13 ms |
21432 KB |
Output is correct |
9 |
Correct |
10 ms |
21460 KB |
Output is correct |
10 |
Correct |
11 ms |
21456 KB |
Output is correct |
11 |
Correct |
10 ms |
21460 KB |
Output is correct |
12 |
Correct |
11 ms |
21460 KB |
Output is correct |
13 |
Correct |
10 ms |
21364 KB |
Output is correct |
14 |
Correct |
11 ms |
21460 KB |
Output is correct |
15 |
Correct |
10 ms |
21460 KB |
Output is correct |
16 |
Correct |
11 ms |
21460 KB |
Output is correct |
17 |
Correct |
11 ms |
21460 KB |
Output is correct |
18 |
Correct |
10 ms |
21432 KB |
Output is correct |
19 |
Correct |
12 ms |
21464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
21332 KB |
Output is correct |
2 |
Correct |
14 ms |
21328 KB |
Output is correct |
3 |
Correct |
11 ms |
21460 KB |
Output is correct |
4 |
Correct |
10 ms |
21464 KB |
Output is correct |
5 |
Correct |
10 ms |
21460 KB |
Output is correct |
6 |
Correct |
10 ms |
21460 KB |
Output is correct |
7 |
Correct |
11 ms |
21460 KB |
Output is correct |
8 |
Correct |
11 ms |
21472 KB |
Output is correct |
9 |
Correct |
13 ms |
21468 KB |
Output is correct |
10 |
Correct |
12 ms |
21460 KB |
Output is correct |
11 |
Correct |
11 ms |
21372 KB |
Output is correct |
12 |
Correct |
10 ms |
21460 KB |
Output is correct |
13 |
Correct |
11 ms |
21460 KB |
Output is correct |
14 |
Correct |
10 ms |
21460 KB |
Output is correct |
15 |
Correct |
11 ms |
21456 KB |
Output is correct |
16 |
Correct |
11 ms |
21460 KB |
Output is correct |
17 |
Correct |
13 ms |
21332 KB |
Output is correct |
18 |
Correct |
13 ms |
21432 KB |
Output is correct |
19 |
Correct |
10 ms |
21460 KB |
Output is correct |
20 |
Correct |
11 ms |
21456 KB |
Output is correct |
21 |
Correct |
10 ms |
21460 KB |
Output is correct |
22 |
Correct |
11 ms |
21460 KB |
Output is correct |
23 |
Correct |
10 ms |
21364 KB |
Output is correct |
24 |
Correct |
11 ms |
21460 KB |
Output is correct |
25 |
Correct |
10 ms |
21460 KB |
Output is correct |
26 |
Correct |
11 ms |
21460 KB |
Output is correct |
27 |
Correct |
11 ms |
21460 KB |
Output is correct |
28 |
Correct |
10 ms |
21432 KB |
Output is correct |
29 |
Correct |
12 ms |
21464 KB |
Output is correct |
30 |
Correct |
11 ms |
21460 KB |
Output is correct |
31 |
Correct |
12 ms |
21464 KB |
Output is correct |
32 |
Correct |
13 ms |
21468 KB |
Output is correct |
33 |
Correct |
13 ms |
21508 KB |
Output is correct |
34 |
Correct |
12 ms |
21588 KB |
Output is correct |
35 |
Correct |
14 ms |
21604 KB |
Output is correct |
36 |
Correct |
15 ms |
21588 KB |
Output is correct |
37 |
Correct |
14 ms |
21684 KB |
Output is correct |
38 |
Correct |
15 ms |
21680 KB |
Output is correct |
39 |
Correct |
15 ms |
21772 KB |
Output is correct |
40 |
Correct |
13 ms |
22228 KB |
Output is correct |
41 |
Correct |
13 ms |
22240 KB |
Output is correct |
42 |
Correct |
13 ms |
21588 KB |
Output is correct |
43 |
Correct |
14 ms |
21996 KB |
Output is correct |
44 |
Correct |
13 ms |
21844 KB |
Output is correct |
45 |
Correct |
13 ms |
21856 KB |
Output is correct |
46 |
Correct |
13 ms |
21844 KB |
Output is correct |
47 |
Correct |
13 ms |
21844 KB |
Output is correct |
48 |
Correct |
13 ms |
21716 KB |
Output is correct |
49 |
Correct |
15 ms |
21728 KB |
Output is correct |
50 |
Correct |
13 ms |
21716 KB |
Output is correct |
51 |
Correct |
13 ms |
21728 KB |
Output is correct |
52 |
Correct |
14 ms |
21724 KB |
Output is correct |
53 |
Correct |
13 ms |
21816 KB |
Output is correct |
54 |
Correct |
13 ms |
21728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
21332 KB |
Output is correct |
2 |
Correct |
14 ms |
21328 KB |
Output is correct |
3 |
Correct |
11 ms |
21460 KB |
Output is correct |
4 |
Correct |
10 ms |
21464 KB |
Output is correct |
5 |
Correct |
10 ms |
21460 KB |
Output is correct |
6 |
Correct |
10 ms |
21460 KB |
Output is correct |
7 |
Correct |
11 ms |
21460 KB |
Output is correct |
8 |
Correct |
11 ms |
21472 KB |
Output is correct |
9 |
Correct |
13 ms |
21468 KB |
Output is correct |
10 |
Correct |
12 ms |
21460 KB |
Output is correct |
11 |
Correct |
11 ms |
21372 KB |
Output is correct |
12 |
Correct |
10 ms |
21460 KB |
Output is correct |
13 |
Correct |
11 ms |
21460 KB |
Output is correct |
14 |
Correct |
10 ms |
21460 KB |
Output is correct |
15 |
Correct |
11 ms |
21456 KB |
Output is correct |
16 |
Correct |
11 ms |
21460 KB |
Output is correct |
17 |
Correct |
13 ms |
21332 KB |
Output is correct |
18 |
Correct |
13 ms |
21432 KB |
Output is correct |
19 |
Correct |
10 ms |
21460 KB |
Output is correct |
20 |
Correct |
11 ms |
21456 KB |
Output is correct |
21 |
Correct |
10 ms |
21460 KB |
Output is correct |
22 |
Correct |
11 ms |
21460 KB |
Output is correct |
23 |
Correct |
10 ms |
21364 KB |
Output is correct |
24 |
Correct |
11 ms |
21460 KB |
Output is correct |
25 |
Correct |
10 ms |
21460 KB |
Output is correct |
26 |
Correct |
11 ms |
21460 KB |
Output is correct |
27 |
Correct |
11 ms |
21460 KB |
Output is correct |
28 |
Correct |
10 ms |
21432 KB |
Output is correct |
29 |
Correct |
12 ms |
21464 KB |
Output is correct |
30 |
Correct |
11 ms |
21460 KB |
Output is correct |
31 |
Correct |
12 ms |
21464 KB |
Output is correct |
32 |
Correct |
13 ms |
21468 KB |
Output is correct |
33 |
Correct |
13 ms |
21508 KB |
Output is correct |
34 |
Correct |
12 ms |
21588 KB |
Output is correct |
35 |
Correct |
14 ms |
21604 KB |
Output is correct |
36 |
Correct |
15 ms |
21588 KB |
Output is correct |
37 |
Correct |
14 ms |
21684 KB |
Output is correct |
38 |
Correct |
15 ms |
21680 KB |
Output is correct |
39 |
Correct |
15 ms |
21772 KB |
Output is correct |
40 |
Correct |
13 ms |
22228 KB |
Output is correct |
41 |
Correct |
13 ms |
22240 KB |
Output is correct |
42 |
Correct |
13 ms |
21588 KB |
Output is correct |
43 |
Correct |
14 ms |
21996 KB |
Output is correct |
44 |
Correct |
13 ms |
21844 KB |
Output is correct |
45 |
Correct |
13 ms |
21856 KB |
Output is correct |
46 |
Correct |
13 ms |
21844 KB |
Output is correct |
47 |
Correct |
13 ms |
21844 KB |
Output is correct |
48 |
Correct |
13 ms |
21716 KB |
Output is correct |
49 |
Correct |
15 ms |
21728 KB |
Output is correct |
50 |
Correct |
13 ms |
21716 KB |
Output is correct |
51 |
Correct |
13 ms |
21728 KB |
Output is correct |
52 |
Correct |
14 ms |
21724 KB |
Output is correct |
53 |
Correct |
13 ms |
21816 KB |
Output is correct |
54 |
Correct |
13 ms |
21728 KB |
Output is correct |
55 |
Correct |
16 ms |
22224 KB |
Output is correct |
56 |
Correct |
34 ms |
24616 KB |
Output is correct |
57 |
Correct |
60 ms |
26796 KB |
Output is correct |
58 |
Correct |
74 ms |
28240 KB |
Output is correct |
59 |
Correct |
96 ms |
30536 KB |
Output is correct |
60 |
Correct |
133 ms |
32740 KB |
Output is correct |
61 |
Correct |
150 ms |
34672 KB |
Output is correct |
62 |
Correct |
164 ms |
36032 KB |
Output is correct |
63 |
Correct |
195 ms |
38756 KB |
Output is correct |
64 |
Correct |
208 ms |
39564 KB |
Output is correct |
65 |
Correct |
108 ms |
73356 KB |
Output is correct |
66 |
Correct |
109 ms |
73164 KB |
Output is correct |
67 |
Correct |
103 ms |
35796 KB |
Output is correct |
68 |
Correct |
156 ms |
55708 KB |
Output is correct |
69 |
Correct |
165 ms |
53052 KB |
Output is correct |
70 |
Correct |
168 ms |
53008 KB |
Output is correct |
71 |
Correct |
178 ms |
49848 KB |
Output is correct |
72 |
Correct |
179 ms |
50184 KB |
Output is correct |
73 |
Correct |
153 ms |
46264 KB |
Output is correct |
74 |
Correct |
153 ms |
46428 KB |
Output is correct |
75 |
Correct |
152 ms |
44964 KB |
Output is correct |
76 |
Correct |
155 ms |
44972 KB |
Output is correct |
77 |
Correct |
165 ms |
44644 KB |
Output is correct |
78 |
Correct |
160 ms |
42572 KB |
Output is correct |
79 |
Correct |
143 ms |
43704 KB |
Output is correct |