#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define sz(v) (int)v.size()
#define MOO(i, a, b) for(int i=a; i<b; i++)
#define M00(i, a) for(int i=0; i<a; i++)
#define MOOd(i,a,b) for(int i = (b)-1; i >= a; i--)
#define M00d(i,a) for(int i = (a)-1; i>=0; i--)
#define FAST ios::sync_with_stdio(0); cin.tie(0);
#define finish(x) return cout << x << '\n', 0;
#define dbg(x) cerr << ">>> " << #x << " = " << x << "\n";
#define _ << " _ " <<
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef pair<ld,ld> pd;
typedef complex<ld> cd;
struct func {
priority_queue<ll> changes;
ll a, b;
func(ll k) { // makes y=|x-k| graph
a = 1;
b = -k;
changes.push(k); changes.push(k);
}
};
const int MAX_N = 300300;
int n;
ll len[MAX_N];
vi ch[MAX_N];
func* sumtwo(func* f1, func* f2) {
if(sz(f1->changes) < sz(f2->changes)) swap(f1, f2);
while(!(f2->changes).empty()) {
(f1->changes).push((f2->changes).top());
(f2->changes).pop();
}
f1->a += f2->a;
f1->b += f2->b;
return f1;
}
func* sum(vector<func*>& v) {
func* cur = v[0];
MOO(i, 1, sz(v)) cur = sumtwo(cur, v[i]);
return cur;
}
func* get(int u) {
if(sz(ch[u]) == 0) {
return new func(len[u]);
} else {
vector<func*> stuff;
for(int v: ch[u]) stuff.pb(get(v));
func* res = sum(stuff);
while((res->a) > 1) {
ll xcoord = (res->changes).top();
(res->changes).pop();
(res->a)--;
(res->b) += xcoord;
}
ll s1 = (res->changes).top();
(res->changes).pop();
ll s2 = (res->changes).top();
(res->changes).pop();
(res->changes).push(s1 + len[u]);
(res->changes).push(s2 + len[u]);
(res->b) -= len[u];
return res;
}
}
int main() { FAST
int k1, k2; cin >> k1 >> k2;
n = k1+k2;
MOO(i, 1, n) {
int u; cin >> u; u--;
ll c; cin >> c;
len[i] = c;
ch[u].pb(i);
}
func* ans = get(0);
ll x = (ans->changes).top();
ll val = x * (ans->a) + (ans->b);
finish(val);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7424 KB |
Output is correct |
2 |
Correct |
9 ms |
7424 KB |
Output is correct |
3 |
Correct |
9 ms |
7424 KB |
Output is correct |
4 |
Correct |
9 ms |
7424 KB |
Output is correct |
5 |
Correct |
9 ms |
7424 KB |
Output is correct |
6 |
Correct |
9 ms |
7424 KB |
Output is correct |
7 |
Correct |
8 ms |
7424 KB |
Output is correct |
8 |
Correct |
9 ms |
7424 KB |
Output is correct |
9 |
Correct |
9 ms |
7424 KB |
Output is correct |
10 |
Correct |
8 ms |
7424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7424 KB |
Output is correct |
2 |
Correct |
8 ms |
7296 KB |
Output is correct |
3 |
Correct |
10 ms |
7600 KB |
Output is correct |
4 |
Correct |
9 ms |
7424 KB |
Output is correct |
5 |
Correct |
9 ms |
7424 KB |
Output is correct |
6 |
Correct |
9 ms |
7424 KB |
Output is correct |
7 |
Correct |
9 ms |
7424 KB |
Output is correct |
8 |
Correct |
10 ms |
7424 KB |
Output is correct |
9 |
Correct |
9 ms |
7424 KB |
Output is correct |
10 |
Correct |
9 ms |
7424 KB |
Output is correct |
11 |
Correct |
9 ms |
7424 KB |
Output is correct |
12 |
Correct |
10 ms |
7424 KB |
Output is correct |
13 |
Correct |
9 ms |
7424 KB |
Output is correct |
14 |
Correct |
9 ms |
7424 KB |
Output is correct |
15 |
Correct |
9 ms |
7424 KB |
Output is correct |
16 |
Correct |
9 ms |
7424 KB |
Output is correct |
17 |
Correct |
9 ms |
7424 KB |
Output is correct |
18 |
Correct |
9 ms |
7424 KB |
Output is correct |
19 |
Correct |
9 ms |
7424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7424 KB |
Output is correct |
2 |
Correct |
9 ms |
7424 KB |
Output is correct |
3 |
Correct |
9 ms |
7424 KB |
Output is correct |
4 |
Correct |
9 ms |
7424 KB |
Output is correct |
5 |
Correct |
9 ms |
7424 KB |
Output is correct |
6 |
Correct |
9 ms |
7424 KB |
Output is correct |
7 |
Correct |
8 ms |
7424 KB |
Output is correct |
8 |
Correct |
9 ms |
7424 KB |
Output is correct |
9 |
Correct |
9 ms |
7424 KB |
Output is correct |
10 |
Correct |
8 ms |
7424 KB |
Output is correct |
11 |
Correct |
9 ms |
7424 KB |
Output is correct |
12 |
Correct |
8 ms |
7296 KB |
Output is correct |
13 |
Correct |
10 ms |
7600 KB |
Output is correct |
14 |
Correct |
9 ms |
7424 KB |
Output is correct |
15 |
Correct |
9 ms |
7424 KB |
Output is correct |
16 |
Correct |
9 ms |
7424 KB |
Output is correct |
17 |
Correct |
9 ms |
7424 KB |
Output is correct |
18 |
Correct |
10 ms |
7424 KB |
Output is correct |
19 |
Correct |
9 ms |
7424 KB |
Output is correct |
20 |
Correct |
9 ms |
7424 KB |
Output is correct |
21 |
Correct |
9 ms |
7424 KB |
Output is correct |
22 |
Correct |
10 ms |
7424 KB |
Output is correct |
23 |
Correct |
9 ms |
7424 KB |
Output is correct |
24 |
Correct |
9 ms |
7424 KB |
Output is correct |
25 |
Correct |
9 ms |
7424 KB |
Output is correct |
26 |
Correct |
9 ms |
7424 KB |
Output is correct |
27 |
Correct |
9 ms |
7424 KB |
Output is correct |
28 |
Correct |
9 ms |
7424 KB |
Output is correct |
29 |
Correct |
9 ms |
7424 KB |
Output is correct |
30 |
Correct |
9 ms |
7424 KB |
Output is correct |
31 |
Correct |
9 ms |
7552 KB |
Output is correct |
32 |
Correct |
9 ms |
7552 KB |
Output is correct |
33 |
Correct |
10 ms |
7552 KB |
Output is correct |
34 |
Correct |
10 ms |
7680 KB |
Output is correct |
35 |
Correct |
11 ms |
7680 KB |
Output is correct |
36 |
Correct |
11 ms |
7808 KB |
Output is correct |
37 |
Correct |
11 ms |
7808 KB |
Output is correct |
38 |
Correct |
12 ms |
8064 KB |
Output is correct |
39 |
Correct |
12 ms |
7808 KB |
Output is correct |
40 |
Correct |
11 ms |
8192 KB |
Output is correct |
41 |
Correct |
11 ms |
8132 KB |
Output is correct |
42 |
Correct |
11 ms |
7680 KB |
Output is correct |
43 |
Correct |
12 ms |
8192 KB |
Output is correct |
44 |
Correct |
12 ms |
8064 KB |
Output is correct |
45 |
Correct |
16 ms |
8064 KB |
Output is correct |
46 |
Correct |
12 ms |
8192 KB |
Output is correct |
47 |
Correct |
11 ms |
8192 KB |
Output is correct |
48 |
Correct |
11 ms |
8064 KB |
Output is correct |
49 |
Correct |
12 ms |
8192 KB |
Output is correct |
50 |
Correct |
11 ms |
8064 KB |
Output is correct |
51 |
Correct |
11 ms |
8064 KB |
Output is correct |
52 |
Correct |
11 ms |
8064 KB |
Output is correct |
53 |
Correct |
11 ms |
8064 KB |
Output is correct |
54 |
Correct |
11 ms |
8192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7424 KB |
Output is correct |
2 |
Correct |
9 ms |
7424 KB |
Output is correct |
3 |
Correct |
9 ms |
7424 KB |
Output is correct |
4 |
Correct |
9 ms |
7424 KB |
Output is correct |
5 |
Correct |
9 ms |
7424 KB |
Output is correct |
6 |
Correct |
9 ms |
7424 KB |
Output is correct |
7 |
Correct |
8 ms |
7424 KB |
Output is correct |
8 |
Correct |
9 ms |
7424 KB |
Output is correct |
9 |
Correct |
9 ms |
7424 KB |
Output is correct |
10 |
Correct |
8 ms |
7424 KB |
Output is correct |
11 |
Correct |
9 ms |
7424 KB |
Output is correct |
12 |
Correct |
8 ms |
7296 KB |
Output is correct |
13 |
Correct |
10 ms |
7600 KB |
Output is correct |
14 |
Correct |
9 ms |
7424 KB |
Output is correct |
15 |
Correct |
9 ms |
7424 KB |
Output is correct |
16 |
Correct |
9 ms |
7424 KB |
Output is correct |
17 |
Correct |
9 ms |
7424 KB |
Output is correct |
18 |
Correct |
10 ms |
7424 KB |
Output is correct |
19 |
Correct |
9 ms |
7424 KB |
Output is correct |
20 |
Correct |
9 ms |
7424 KB |
Output is correct |
21 |
Correct |
9 ms |
7424 KB |
Output is correct |
22 |
Correct |
10 ms |
7424 KB |
Output is correct |
23 |
Correct |
9 ms |
7424 KB |
Output is correct |
24 |
Correct |
9 ms |
7424 KB |
Output is correct |
25 |
Correct |
9 ms |
7424 KB |
Output is correct |
26 |
Correct |
9 ms |
7424 KB |
Output is correct |
27 |
Correct |
9 ms |
7424 KB |
Output is correct |
28 |
Correct |
9 ms |
7424 KB |
Output is correct |
29 |
Correct |
9 ms |
7424 KB |
Output is correct |
30 |
Correct |
9 ms |
7424 KB |
Output is correct |
31 |
Correct |
9 ms |
7552 KB |
Output is correct |
32 |
Correct |
9 ms |
7552 KB |
Output is correct |
33 |
Correct |
10 ms |
7552 KB |
Output is correct |
34 |
Correct |
10 ms |
7680 KB |
Output is correct |
35 |
Correct |
11 ms |
7680 KB |
Output is correct |
36 |
Correct |
11 ms |
7808 KB |
Output is correct |
37 |
Correct |
11 ms |
7808 KB |
Output is correct |
38 |
Correct |
12 ms |
8064 KB |
Output is correct |
39 |
Correct |
12 ms |
7808 KB |
Output is correct |
40 |
Correct |
11 ms |
8192 KB |
Output is correct |
41 |
Correct |
11 ms |
8132 KB |
Output is correct |
42 |
Correct |
11 ms |
7680 KB |
Output is correct |
43 |
Correct |
12 ms |
8192 KB |
Output is correct |
44 |
Correct |
12 ms |
8064 KB |
Output is correct |
45 |
Correct |
16 ms |
8064 KB |
Output is correct |
46 |
Correct |
12 ms |
8192 KB |
Output is correct |
47 |
Correct |
11 ms |
8192 KB |
Output is correct |
48 |
Correct |
11 ms |
8064 KB |
Output is correct |
49 |
Correct |
12 ms |
8192 KB |
Output is correct |
50 |
Correct |
11 ms |
8064 KB |
Output is correct |
51 |
Correct |
11 ms |
8064 KB |
Output is correct |
52 |
Correct |
11 ms |
8064 KB |
Output is correct |
53 |
Correct |
11 ms |
8064 KB |
Output is correct |
54 |
Correct |
11 ms |
8192 KB |
Output is correct |
55 |
Correct |
17 ms |
8576 KB |
Output is correct |
56 |
Correct |
41 ms |
11904 KB |
Output is correct |
57 |
Correct |
72 ms |
15220 KB |
Output is correct |
58 |
Correct |
102 ms |
17648 KB |
Output is correct |
59 |
Correct |
128 ms |
20800 KB |
Output is correct |
60 |
Correct |
163 ms |
24052 KB |
Output is correct |
61 |
Correct |
188 ms |
26480 KB |
Output is correct |
62 |
Correct |
218 ms |
28396 KB |
Output is correct |
63 |
Correct |
260 ms |
32620 KB |
Output is correct |
64 |
Correct |
286 ms |
33808 KB |
Output is correct |
65 |
Correct |
145 ms |
52216 KB |
Output is correct |
66 |
Correct |
146 ms |
52220 KB |
Output is correct |
67 |
Correct |
151 ms |
24184 KB |
Output is correct |
68 |
Correct |
202 ms |
48616 KB |
Output is correct |
69 |
Correct |
241 ms |
46436 KB |
Output is correct |
70 |
Correct |
232 ms |
46692 KB |
Output is correct |
71 |
Correct |
223 ms |
57076 KB |
Output is correct |
72 |
Correct |
214 ms |
57060 KB |
Output is correct |
73 |
Correct |
198 ms |
53900 KB |
Output is correct |
74 |
Correct |
185 ms |
53768 KB |
Output is correct |
75 |
Correct |
193 ms |
52432 KB |
Output is correct |
76 |
Correct |
191 ms |
52584 KB |
Output is correct |
77 |
Correct |
200 ms |
50920 KB |
Output is correct |
78 |
Correct |
199 ms |
48844 KB |
Output is correct |
79 |
Correct |
188 ms |
53476 KB |
Output is correct |