#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
#define int ll
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mispertion ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define F first
#define S second
#define getlast(s) (*s.rbegin())
#define debg cout << "OK\n"
const ld PI = 3.1415926535;
const int N = 5e5 + 10;
const int M = 1e5 + 10;
int mod = 1e9+7;
const int infi = INT_MAX;
const ll infl = 2e18;
const int P = 505319;
const ld eps = 1e-9;
int mult(int a, int b){
return a * 1LL * b % mod;
}
int sum(int a, int b){
a %= mod;
b %= mod;
if(a + b >= mod)
return a + b - mod;
if(a + b < 0)
return a + b + mod;
return a + b;
}
int binpow(int a, int n){
if (n == 0)
return 1;
if (n % 2 == 1){
return mult(binpow(a, n - 1), a);
}
else{
auto b = binpow(a, n / 2);
return mult(b, b);
}
}
int n, m, h[N];
multiset<int> sl[N];
vector<pii> g[N];
void dfs(int v, int w){
if(v > n){
h[v] = 0;
sl[v].insert(w);
sl[v].insert(w);
return;
}
for(auto ee : g[v]){
int u = ee.F, w = ee.S;
dfs(u, w);
if(sz(sl[u]) > sz(sl[v])){
sl[v].swap(sl[u]);
swap(h[v], h[u]);
}
for(auto e : sl[u])
sl[v].insert(e);
}
for(int i = 1; i < sz(g[v]); i++)
sl[v].erase(sl[v].find(*sl[v].rbegin()));
int a = *sl[v].rbegin();
int b = *(++sl[v].rbegin());
sl[v].erase(sl[v].find(a));
sl[v].erase(sl[v].find(b));
sl[v].insert(a + w);
sl[v].insert(b + w);
}
void solve(){
cin >> n >> m;
int sm = 0;
for(int i = 2; i <= n + m; i++){
int p, w;
cin >> p >> w;
g[p].pb({i, w});
sm += w;
}
dfs(1, 0);
sl[1].erase(sl[1].find(*sl[1].rbegin()));
for(auto e : sl[1])
sm -= e;
cout << sm << '\n';
}
signed main(){
//mispertion;
int t = 1;
//cin >> t;
while (t--){
solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
35676 KB |
Output is correct |
2 |
Correct |
20 ms |
35676 KB |
Output is correct |
3 |
Correct |
20 ms |
35676 KB |
Output is correct |
4 |
Correct |
14 ms |
35676 KB |
Output is correct |
5 |
Correct |
15 ms |
35656 KB |
Output is correct |
6 |
Correct |
22 ms |
35568 KB |
Output is correct |
7 |
Correct |
16 ms |
35516 KB |
Output is correct |
8 |
Correct |
16 ms |
35480 KB |
Output is correct |
9 |
Correct |
15 ms |
35508 KB |
Output is correct |
10 |
Correct |
17 ms |
35608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
35476 KB |
Output is correct |
2 |
Correct |
16 ms |
35548 KB |
Output is correct |
3 |
Correct |
18 ms |
35644 KB |
Output is correct |
4 |
Correct |
14 ms |
35592 KB |
Output is correct |
5 |
Correct |
14 ms |
35676 KB |
Output is correct |
6 |
Correct |
15 ms |
35560 KB |
Output is correct |
7 |
Correct |
16 ms |
35700 KB |
Output is correct |
8 |
Correct |
14 ms |
35596 KB |
Output is correct |
9 |
Correct |
15 ms |
35680 KB |
Output is correct |
10 |
Correct |
16 ms |
35692 KB |
Output is correct |
11 |
Correct |
16 ms |
35468 KB |
Output is correct |
12 |
Correct |
14 ms |
35676 KB |
Output is correct |
13 |
Correct |
16 ms |
35512 KB |
Output is correct |
14 |
Correct |
19 ms |
35492 KB |
Output is correct |
15 |
Correct |
22 ms |
35676 KB |
Output is correct |
16 |
Correct |
15 ms |
35676 KB |
Output is correct |
17 |
Correct |
15 ms |
35724 KB |
Output is correct |
18 |
Correct |
21 ms |
35568 KB |
Output is correct |
19 |
Correct |
21 ms |
35676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
35676 KB |
Output is correct |
2 |
Correct |
20 ms |
35676 KB |
Output is correct |
3 |
Correct |
20 ms |
35676 KB |
Output is correct |
4 |
Correct |
14 ms |
35676 KB |
Output is correct |
5 |
Correct |
15 ms |
35656 KB |
Output is correct |
6 |
Correct |
22 ms |
35568 KB |
Output is correct |
7 |
Correct |
16 ms |
35516 KB |
Output is correct |
8 |
Correct |
16 ms |
35480 KB |
Output is correct |
9 |
Correct |
15 ms |
35508 KB |
Output is correct |
10 |
Correct |
17 ms |
35608 KB |
Output is correct |
11 |
Correct |
18 ms |
35476 KB |
Output is correct |
12 |
Correct |
16 ms |
35548 KB |
Output is correct |
13 |
Correct |
18 ms |
35644 KB |
Output is correct |
14 |
Correct |
14 ms |
35592 KB |
Output is correct |
15 |
Correct |
14 ms |
35676 KB |
Output is correct |
16 |
Correct |
15 ms |
35560 KB |
Output is correct |
17 |
Correct |
16 ms |
35700 KB |
Output is correct |
18 |
Correct |
14 ms |
35596 KB |
Output is correct |
19 |
Correct |
15 ms |
35680 KB |
Output is correct |
20 |
Correct |
16 ms |
35692 KB |
Output is correct |
21 |
Correct |
16 ms |
35468 KB |
Output is correct |
22 |
Correct |
14 ms |
35676 KB |
Output is correct |
23 |
Correct |
16 ms |
35512 KB |
Output is correct |
24 |
Correct |
19 ms |
35492 KB |
Output is correct |
25 |
Correct |
22 ms |
35676 KB |
Output is correct |
26 |
Correct |
15 ms |
35676 KB |
Output is correct |
27 |
Correct |
15 ms |
35724 KB |
Output is correct |
28 |
Correct |
21 ms |
35568 KB |
Output is correct |
29 |
Correct |
21 ms |
35676 KB |
Output is correct |
30 |
Correct |
17 ms |
35676 KB |
Output is correct |
31 |
Correct |
15 ms |
35676 KB |
Output is correct |
32 |
Correct |
23 ms |
35676 KB |
Output is correct |
33 |
Correct |
19 ms |
35740 KB |
Output is correct |
34 |
Correct |
25 ms |
35932 KB |
Output is correct |
35 |
Correct |
19 ms |
36020 KB |
Output is correct |
36 |
Correct |
23 ms |
36204 KB |
Output is correct |
37 |
Correct |
20 ms |
36188 KB |
Output is correct |
38 |
Correct |
20 ms |
36188 KB |
Output is correct |
39 |
Correct |
23 ms |
36152 KB |
Output is correct |
40 |
Correct |
18 ms |
36340 KB |
Output is correct |
41 |
Correct |
18 ms |
36440 KB |
Output is correct |
42 |
Correct |
19 ms |
35808 KB |
Output is correct |
43 |
Correct |
18 ms |
36452 KB |
Output is correct |
44 |
Correct |
19 ms |
36444 KB |
Output is correct |
45 |
Correct |
19 ms |
36444 KB |
Output is correct |
46 |
Correct |
21 ms |
36596 KB |
Output is correct |
47 |
Correct |
19 ms |
36700 KB |
Output is correct |
48 |
Correct |
20 ms |
36444 KB |
Output is correct |
49 |
Correct |
19 ms |
36696 KB |
Output is correct |
50 |
Correct |
19 ms |
36548 KB |
Output is correct |
51 |
Correct |
25 ms |
36284 KB |
Output is correct |
52 |
Correct |
25 ms |
36444 KB |
Output is correct |
53 |
Correct |
18 ms |
36444 KB |
Output is correct |
54 |
Correct |
18 ms |
36696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
35676 KB |
Output is correct |
2 |
Correct |
20 ms |
35676 KB |
Output is correct |
3 |
Correct |
20 ms |
35676 KB |
Output is correct |
4 |
Correct |
14 ms |
35676 KB |
Output is correct |
5 |
Correct |
15 ms |
35656 KB |
Output is correct |
6 |
Correct |
22 ms |
35568 KB |
Output is correct |
7 |
Correct |
16 ms |
35516 KB |
Output is correct |
8 |
Correct |
16 ms |
35480 KB |
Output is correct |
9 |
Correct |
15 ms |
35508 KB |
Output is correct |
10 |
Correct |
17 ms |
35608 KB |
Output is correct |
11 |
Correct |
18 ms |
35476 KB |
Output is correct |
12 |
Correct |
16 ms |
35548 KB |
Output is correct |
13 |
Correct |
18 ms |
35644 KB |
Output is correct |
14 |
Correct |
14 ms |
35592 KB |
Output is correct |
15 |
Correct |
14 ms |
35676 KB |
Output is correct |
16 |
Correct |
15 ms |
35560 KB |
Output is correct |
17 |
Correct |
16 ms |
35700 KB |
Output is correct |
18 |
Correct |
14 ms |
35596 KB |
Output is correct |
19 |
Correct |
15 ms |
35680 KB |
Output is correct |
20 |
Correct |
16 ms |
35692 KB |
Output is correct |
21 |
Correct |
16 ms |
35468 KB |
Output is correct |
22 |
Correct |
14 ms |
35676 KB |
Output is correct |
23 |
Correct |
16 ms |
35512 KB |
Output is correct |
24 |
Correct |
19 ms |
35492 KB |
Output is correct |
25 |
Correct |
22 ms |
35676 KB |
Output is correct |
26 |
Correct |
15 ms |
35676 KB |
Output is correct |
27 |
Correct |
15 ms |
35724 KB |
Output is correct |
28 |
Correct |
21 ms |
35568 KB |
Output is correct |
29 |
Correct |
21 ms |
35676 KB |
Output is correct |
30 |
Correct |
17 ms |
35676 KB |
Output is correct |
31 |
Correct |
15 ms |
35676 KB |
Output is correct |
32 |
Correct |
23 ms |
35676 KB |
Output is correct |
33 |
Correct |
19 ms |
35740 KB |
Output is correct |
34 |
Correct |
25 ms |
35932 KB |
Output is correct |
35 |
Correct |
19 ms |
36020 KB |
Output is correct |
36 |
Correct |
23 ms |
36204 KB |
Output is correct |
37 |
Correct |
20 ms |
36188 KB |
Output is correct |
38 |
Correct |
20 ms |
36188 KB |
Output is correct |
39 |
Correct |
23 ms |
36152 KB |
Output is correct |
40 |
Correct |
18 ms |
36340 KB |
Output is correct |
41 |
Correct |
18 ms |
36440 KB |
Output is correct |
42 |
Correct |
19 ms |
35808 KB |
Output is correct |
43 |
Correct |
18 ms |
36452 KB |
Output is correct |
44 |
Correct |
19 ms |
36444 KB |
Output is correct |
45 |
Correct |
19 ms |
36444 KB |
Output is correct |
46 |
Correct |
21 ms |
36596 KB |
Output is correct |
47 |
Correct |
19 ms |
36700 KB |
Output is correct |
48 |
Correct |
20 ms |
36444 KB |
Output is correct |
49 |
Correct |
19 ms |
36696 KB |
Output is correct |
50 |
Correct |
19 ms |
36548 KB |
Output is correct |
51 |
Correct |
25 ms |
36284 KB |
Output is correct |
52 |
Correct |
25 ms |
36444 KB |
Output is correct |
53 |
Correct |
18 ms |
36444 KB |
Output is correct |
54 |
Correct |
18 ms |
36696 KB |
Output is correct |
55 |
Correct |
26 ms |
37348 KB |
Output is correct |
56 |
Correct |
60 ms |
42584 KB |
Output is correct |
57 |
Correct |
98 ms |
48212 KB |
Output is correct |
58 |
Correct |
110 ms |
51884 KB |
Output is correct |
59 |
Correct |
163 ms |
57168 KB |
Output is correct |
60 |
Correct |
180 ms |
62828 KB |
Output is correct |
61 |
Correct |
246 ms |
66640 KB |
Output is correct |
62 |
Correct |
249 ms |
70228 KB |
Output is correct |
63 |
Correct |
297 ms |
77904 KB |
Output is correct |
64 |
Correct |
332 ms |
78692 KB |
Output is correct |
65 |
Correct |
197 ms |
89684 KB |
Output is correct |
66 |
Correct |
205 ms |
89812 KB |
Output is correct |
67 |
Correct |
212 ms |
52308 KB |
Output is correct |
68 |
Correct |
265 ms |
89680 KB |
Output is correct |
69 |
Correct |
366 ms |
87496 KB |
Output is correct |
70 |
Correct |
334 ms |
87632 KB |
Output is correct |
71 |
Correct |
360 ms |
118260 KB |
Output is correct |
72 |
Correct |
359 ms |
117584 KB |
Output is correct |
73 |
Correct |
406 ms |
104020 KB |
Output is correct |
74 |
Correct |
376 ms |
104056 KB |
Output is correct |
75 |
Correct |
369 ms |
102996 KB |
Output is correct |
76 |
Correct |
352 ms |
102992 KB |
Output is correct |
77 |
Correct |
408 ms |
101032 KB |
Output is correct |
78 |
Correct |
384 ms |
97860 KB |
Output is correct |
79 |
Correct |
440 ms |
102540 KB |
Output is correct |