//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,sse4a,avx,avx2,popcnt,tune=native")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T> //order_of_key = menores, find_by_order = consultar indexado en 0, regresa iterador
using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vll = vector<ll>;
using vii = vector<int>;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define mid ((l+r)>>1)
#define f first
#define s second
#define pb push_back
#define MP make_pair
#define all(x) (x).begin(),(x).end()
#define inicio(x) (*(x).begin())
#define rinicio(x) (*(x).rbegin())
#define SZ(x) (int)(x.size())
const ll MAXN = (ll)3e5 + 5;
const ll MOD = (ll)1e9 + 7;
vector<pll> g[MAXN];
priority_queue<ll> pq[MAXN];
ll sz[MAXN];
void dfs1(int nodo = 1, int p = 0) {
sz[nodo] = 1;
for (auto &it : g[nodo]) {
if (it.f == p) continue;
dfs1(it.f, nodo);
sz[nodo] += sz[it.f];
}
return;
}
void dfs2(int nodo = 1, int p = 0, ll d = 0) {
pll bign = MP(-1, -1);
for (auto &it : g[nodo]) {
if (it.f == p) continue;
if (bign.f == -1 || sz[it.f] > sz[bign.f]) bign = it;
}
if (bign.f != -1) {
dfs2(bign.f, nodo, bign.s);
swap(pq[nodo], pq[bign.f]);
}
else {
pq[nodo].push(0);
pq[nodo].push(0);
}
for (auto &it : g[nodo]) {
if (it.f == p || it.f == bign.f) continue;
dfs2(it.f, nodo, it.s);
while (!pq[it.f].empty()) {
pq[nodo].push(pq[it.f].top());
pq[it.f].pop();
}
}
for (int i = 0; i < SZ(g[nodo]) - 2; i++) pq[nodo].pop();
ll a, b;
a = pq[nodo].top();
pq[nodo].pop();
b = pq[nodo].top();
pq[nodo].pop();
a += d;
b += d;
pq[nodo].push(a);
pq[nodo].push(b);
return;
}
void tc() {
ll N, M, ans = 0, pre = 0; cin >> N >> M;
for (int i = 2; i <= N + M; i++) {
ll a, p; cin >> a >> p;
g[i].pb(MP(a, p));
g[a].pb(MP(i, p));
ans += p;
}
g[0].pb(MP(1, 0));
g[1].pb(MP(0, 0));
dfs1();
dfs2();
vll v;
pq[1].pop();
while (!pq[1].empty()) {
v.pb(pq[1].top());
pq[1].pop();
}
reverse(all(v));
for (int i = 0; i < SZ(v); i++) {
ans -= (SZ(v) - i)*(v[i] - pre);
pre = v[i];
}
cout << ans << "\n";
return;
}
int main() {
ios_base::sync_with_stdio(0);cin.tie(0);
int t = 1;
//cin >> t;
for (int i = 1; i <= t; i++) {
tc();
}
return 0;
}
//checa tus constantes, n = 1? overflow?
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
3 ms |
16824 KB |
Output is correct |
3 |
Correct |
3 ms |
16988 KB |
Output is correct |
4 |
Correct |
3 ms |
16988 KB |
Output is correct |
5 |
Correct |
3 ms |
16988 KB |
Output is correct |
6 |
Correct |
3 ms |
16984 KB |
Output is correct |
7 |
Correct |
3 ms |
16984 KB |
Output is correct |
8 |
Correct |
3 ms |
16988 KB |
Output is correct |
9 |
Correct |
3 ms |
16988 KB |
Output is correct |
10 |
Correct |
3 ms |
16988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
2 ms |
16824 KB |
Output is correct |
3 |
Correct |
2 ms |
16988 KB |
Output is correct |
4 |
Correct |
3 ms |
16824 KB |
Output is correct |
5 |
Correct |
3 ms |
16988 KB |
Output is correct |
6 |
Correct |
3 ms |
16988 KB |
Output is correct |
7 |
Correct |
3 ms |
16988 KB |
Output is correct |
8 |
Correct |
3 ms |
16988 KB |
Output is correct |
9 |
Correct |
2 ms |
16988 KB |
Output is correct |
10 |
Correct |
2 ms |
16832 KB |
Output is correct |
11 |
Correct |
3 ms |
16988 KB |
Output is correct |
12 |
Correct |
3 ms |
16988 KB |
Output is correct |
13 |
Correct |
3 ms |
16988 KB |
Output is correct |
14 |
Correct |
3 ms |
16988 KB |
Output is correct |
15 |
Correct |
3 ms |
17044 KB |
Output is correct |
16 |
Correct |
3 ms |
16988 KB |
Output is correct |
17 |
Correct |
3 ms |
16988 KB |
Output is correct |
18 |
Correct |
3 ms |
16988 KB |
Output is correct |
19 |
Correct |
3 ms |
16988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
3 ms |
16824 KB |
Output is correct |
3 |
Correct |
3 ms |
16988 KB |
Output is correct |
4 |
Correct |
3 ms |
16988 KB |
Output is correct |
5 |
Correct |
3 ms |
16988 KB |
Output is correct |
6 |
Correct |
3 ms |
16984 KB |
Output is correct |
7 |
Correct |
3 ms |
16984 KB |
Output is correct |
8 |
Correct |
3 ms |
16988 KB |
Output is correct |
9 |
Correct |
3 ms |
16988 KB |
Output is correct |
10 |
Correct |
3 ms |
16988 KB |
Output is correct |
11 |
Correct |
3 ms |
16988 KB |
Output is correct |
12 |
Correct |
2 ms |
16824 KB |
Output is correct |
13 |
Correct |
2 ms |
16988 KB |
Output is correct |
14 |
Correct |
3 ms |
16824 KB |
Output is correct |
15 |
Correct |
3 ms |
16988 KB |
Output is correct |
16 |
Correct |
3 ms |
16988 KB |
Output is correct |
17 |
Correct |
3 ms |
16988 KB |
Output is correct |
18 |
Correct |
3 ms |
16988 KB |
Output is correct |
19 |
Correct |
2 ms |
16988 KB |
Output is correct |
20 |
Correct |
2 ms |
16832 KB |
Output is correct |
21 |
Correct |
3 ms |
16988 KB |
Output is correct |
22 |
Correct |
3 ms |
16988 KB |
Output is correct |
23 |
Correct |
3 ms |
16988 KB |
Output is correct |
24 |
Correct |
3 ms |
16988 KB |
Output is correct |
25 |
Correct |
3 ms |
17044 KB |
Output is correct |
26 |
Correct |
3 ms |
16988 KB |
Output is correct |
27 |
Correct |
3 ms |
16988 KB |
Output is correct |
28 |
Correct |
3 ms |
16988 KB |
Output is correct |
29 |
Correct |
3 ms |
16988 KB |
Output is correct |
30 |
Correct |
3 ms |
16988 KB |
Output is correct |
31 |
Correct |
3 ms |
16988 KB |
Output is correct |
32 |
Correct |
5 ms |
16988 KB |
Output is correct |
33 |
Correct |
3 ms |
16988 KB |
Output is correct |
34 |
Correct |
4 ms |
17240 KB |
Output is correct |
35 |
Correct |
5 ms |
17244 KB |
Output is correct |
36 |
Correct |
4 ms |
17244 KB |
Output is correct |
37 |
Correct |
4 ms |
17244 KB |
Output is correct |
38 |
Correct |
5 ms |
17240 KB |
Output is correct |
39 |
Correct |
4 ms |
17244 KB |
Output is correct |
40 |
Correct |
4 ms |
18012 KB |
Output is correct |
41 |
Correct |
4 ms |
18072 KB |
Output is correct |
42 |
Correct |
4 ms |
17244 KB |
Output is correct |
43 |
Correct |
5 ms |
17756 KB |
Output is correct |
44 |
Correct |
5 ms |
17756 KB |
Output is correct |
45 |
Correct |
5 ms |
17700 KB |
Output is correct |
46 |
Correct |
5 ms |
17500 KB |
Output is correct |
47 |
Correct |
5 ms |
17604 KB |
Output is correct |
48 |
Correct |
5 ms |
17500 KB |
Output is correct |
49 |
Correct |
6 ms |
17500 KB |
Output is correct |
50 |
Correct |
5 ms |
17500 KB |
Output is correct |
51 |
Correct |
4 ms |
17416 KB |
Output is correct |
52 |
Correct |
5 ms |
17496 KB |
Output is correct |
53 |
Correct |
7 ms |
17500 KB |
Output is correct |
54 |
Correct |
5 ms |
17612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
3 ms |
16824 KB |
Output is correct |
3 |
Correct |
3 ms |
16988 KB |
Output is correct |
4 |
Correct |
3 ms |
16988 KB |
Output is correct |
5 |
Correct |
3 ms |
16988 KB |
Output is correct |
6 |
Correct |
3 ms |
16984 KB |
Output is correct |
7 |
Correct |
3 ms |
16984 KB |
Output is correct |
8 |
Correct |
3 ms |
16988 KB |
Output is correct |
9 |
Correct |
3 ms |
16988 KB |
Output is correct |
10 |
Correct |
3 ms |
16988 KB |
Output is correct |
11 |
Correct |
3 ms |
16988 KB |
Output is correct |
12 |
Correct |
2 ms |
16824 KB |
Output is correct |
13 |
Correct |
2 ms |
16988 KB |
Output is correct |
14 |
Correct |
3 ms |
16824 KB |
Output is correct |
15 |
Correct |
3 ms |
16988 KB |
Output is correct |
16 |
Correct |
3 ms |
16988 KB |
Output is correct |
17 |
Correct |
3 ms |
16988 KB |
Output is correct |
18 |
Correct |
3 ms |
16988 KB |
Output is correct |
19 |
Correct |
2 ms |
16988 KB |
Output is correct |
20 |
Correct |
2 ms |
16832 KB |
Output is correct |
21 |
Correct |
3 ms |
16988 KB |
Output is correct |
22 |
Correct |
3 ms |
16988 KB |
Output is correct |
23 |
Correct |
3 ms |
16988 KB |
Output is correct |
24 |
Correct |
3 ms |
16988 KB |
Output is correct |
25 |
Correct |
3 ms |
17044 KB |
Output is correct |
26 |
Correct |
3 ms |
16988 KB |
Output is correct |
27 |
Correct |
3 ms |
16988 KB |
Output is correct |
28 |
Correct |
3 ms |
16988 KB |
Output is correct |
29 |
Correct |
3 ms |
16988 KB |
Output is correct |
30 |
Correct |
3 ms |
16988 KB |
Output is correct |
31 |
Correct |
3 ms |
16988 KB |
Output is correct |
32 |
Correct |
5 ms |
16988 KB |
Output is correct |
33 |
Correct |
3 ms |
16988 KB |
Output is correct |
34 |
Correct |
4 ms |
17240 KB |
Output is correct |
35 |
Correct |
5 ms |
17244 KB |
Output is correct |
36 |
Correct |
4 ms |
17244 KB |
Output is correct |
37 |
Correct |
4 ms |
17244 KB |
Output is correct |
38 |
Correct |
5 ms |
17240 KB |
Output is correct |
39 |
Correct |
4 ms |
17244 KB |
Output is correct |
40 |
Correct |
4 ms |
18012 KB |
Output is correct |
41 |
Correct |
4 ms |
18072 KB |
Output is correct |
42 |
Correct |
4 ms |
17244 KB |
Output is correct |
43 |
Correct |
5 ms |
17756 KB |
Output is correct |
44 |
Correct |
5 ms |
17756 KB |
Output is correct |
45 |
Correct |
5 ms |
17700 KB |
Output is correct |
46 |
Correct |
5 ms |
17500 KB |
Output is correct |
47 |
Correct |
5 ms |
17604 KB |
Output is correct |
48 |
Correct |
5 ms |
17500 KB |
Output is correct |
49 |
Correct |
6 ms |
17500 KB |
Output is correct |
50 |
Correct |
5 ms |
17500 KB |
Output is correct |
51 |
Correct |
4 ms |
17416 KB |
Output is correct |
52 |
Correct |
5 ms |
17496 KB |
Output is correct |
53 |
Correct |
7 ms |
17500 KB |
Output is correct |
54 |
Correct |
5 ms |
17612 KB |
Output is correct |
55 |
Correct |
8 ms |
18008 KB |
Output is correct |
56 |
Correct |
26 ms |
23252 KB |
Output is correct |
57 |
Correct |
42 ms |
26132 KB |
Output is correct |
58 |
Correct |
57 ms |
28364 KB |
Output is correct |
59 |
Correct |
81 ms |
31180 KB |
Output is correct |
60 |
Correct |
97 ms |
33740 KB |
Output is correct |
61 |
Correct |
119 ms |
36552 KB |
Output is correct |
62 |
Correct |
141 ms |
38176 KB |
Output is correct |
63 |
Correct |
167 ms |
41672 KB |
Output is correct |
64 |
Correct |
190 ms |
42700 KB |
Output is correct |
65 |
Correct |
101 ms |
84896 KB |
Output is correct |
66 |
Correct |
117 ms |
84816 KB |
Output is correct |
67 |
Correct |
86 ms |
33328 KB |
Output is correct |
68 |
Correct |
159 ms |
70460 KB |
Output is correct |
69 |
Correct |
190 ms |
66240 KB |
Output is correct |
70 |
Correct |
198 ms |
66248 KB |
Output is correct |
71 |
Correct |
190 ms |
65984 KB |
Output is correct |
72 |
Correct |
179 ms |
65044 KB |
Output is correct |
73 |
Correct |
166 ms |
61560 KB |
Output is correct |
74 |
Correct |
155 ms |
62396 KB |
Output is correct |
75 |
Correct |
166 ms |
59708 KB |
Output is correct |
76 |
Correct |
179 ms |
57528 KB |
Output is correct |
77 |
Correct |
167 ms |
55476 KB |
Output is correct |
78 |
Correct |
168 ms |
54236 KB |
Output is correct |
79 |
Correct |
147 ms |
55292 KB |
Output is correct |