#include <cmath>
#include <functional>
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <list>
#include <time.h>
#include <math.h>
#include <random>
#include <deque>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <cassert>
#include <bitset>
#include <sstream>
#include <chrono>
#include <cstring>
#include <numeric>
using namespace std;
typedef long long ll;
signed main()
{
#ifdef ONPC
FILE* stream;
freopen_s(&stream, "input.txt", "r", stdin);
#else
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif
int __, n;
cin >> __ >> n;
n += __;
vector<vector<int>> g(n);
vector<int> up(n, 0);
vector<priority_queue<ll>> pqs(n);
for (int i = 1; i < n; i++)
{
int par;
cin >> par >> up[i];
par--;
assert(0 <= par && par < i);
g[par].push_back(i);
}
ll sol = 0;
for (int i = 0; i < n; i++)
{
sol += up[i];
}
for (int i = n - 1; i >= 0; i--)
{
if (g[i].empty())
{
pqs[i].push(up[i]);
pqs[i].push(up[i]);
continue;
}
for (auto& j : g[i])
{
if ((int)pqs[i].size() < (int)pqs[j].size())
{
swap(pqs[i], pqs[j]);
}
while (!pqs[j].empty())
{
pqs[i].push(pqs[j].top());
pqs[j].pop();
}
}
for (int s = 1; s <= (int)g[i].size() - 1; s++)
{
pqs[i].pop();
}
ll x = pqs[i].top() + up[i];
pqs[i].pop();
ll y = pqs[i].top() + up[i];
pqs[i].pop();
pqs[i].push(x);
pqs[i].push(y);
}
pqs[0].pop();
while (!pqs[0].empty())
{
sol -= pqs[0].top();
pqs[0].pop();
}
cout << sol << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
328 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
17 |
Correct |
0 ms |
324 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
316 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
328 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
328 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
316 KB |
Output is correct |
27 |
Correct |
0 ms |
324 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
324 KB |
Output is correct |
30 |
Correct |
1 ms |
324 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
584 KB |
Output is correct |
34 |
Correct |
2 ms |
596 KB |
Output is correct |
35 |
Correct |
2 ms |
724 KB |
Output is correct |
36 |
Correct |
2 ms |
724 KB |
Output is correct |
37 |
Correct |
3 ms |
832 KB |
Output is correct |
38 |
Correct |
3 ms |
852 KB |
Output is correct |
39 |
Correct |
3 ms |
844 KB |
Output is correct |
40 |
Correct |
2 ms |
820 KB |
Output is correct |
41 |
Correct |
2 ms |
852 KB |
Output is correct |
42 |
Correct |
2 ms |
852 KB |
Output is correct |
43 |
Correct |
3 ms |
860 KB |
Output is correct |
44 |
Correct |
3 ms |
844 KB |
Output is correct |
45 |
Correct |
3 ms |
852 KB |
Output is correct |
46 |
Correct |
3 ms |
980 KB |
Output is correct |
47 |
Correct |
3 ms |
980 KB |
Output is correct |
48 |
Correct |
3 ms |
980 KB |
Output is correct |
49 |
Correct |
3 ms |
980 KB |
Output is correct |
50 |
Correct |
2 ms |
852 KB |
Output is correct |
51 |
Correct |
3 ms |
852 KB |
Output is correct |
52 |
Correct |
3 ms |
980 KB |
Output is correct |
53 |
Correct |
3 ms |
980 KB |
Output is correct |
54 |
Correct |
3 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
316 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
328 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
328 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
316 KB |
Output is correct |
27 |
Correct |
0 ms |
324 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
324 KB |
Output is correct |
30 |
Correct |
1 ms |
324 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
584 KB |
Output is correct |
34 |
Correct |
2 ms |
596 KB |
Output is correct |
35 |
Correct |
2 ms |
724 KB |
Output is correct |
36 |
Correct |
2 ms |
724 KB |
Output is correct |
37 |
Correct |
3 ms |
832 KB |
Output is correct |
38 |
Correct |
3 ms |
852 KB |
Output is correct |
39 |
Correct |
3 ms |
844 KB |
Output is correct |
40 |
Correct |
2 ms |
820 KB |
Output is correct |
41 |
Correct |
2 ms |
852 KB |
Output is correct |
42 |
Correct |
2 ms |
852 KB |
Output is correct |
43 |
Correct |
3 ms |
860 KB |
Output is correct |
44 |
Correct |
3 ms |
844 KB |
Output is correct |
45 |
Correct |
3 ms |
852 KB |
Output is correct |
46 |
Correct |
3 ms |
980 KB |
Output is correct |
47 |
Correct |
3 ms |
980 KB |
Output is correct |
48 |
Correct |
3 ms |
980 KB |
Output is correct |
49 |
Correct |
3 ms |
980 KB |
Output is correct |
50 |
Correct |
2 ms |
852 KB |
Output is correct |
51 |
Correct |
3 ms |
852 KB |
Output is correct |
52 |
Correct |
3 ms |
980 KB |
Output is correct |
53 |
Correct |
3 ms |
980 KB |
Output is correct |
54 |
Correct |
3 ms |
980 KB |
Output is correct |
55 |
Correct |
6 ms |
2004 KB |
Output is correct |
56 |
Correct |
24 ms |
6760 KB |
Output is correct |
57 |
Correct |
43 ms |
11176 KB |
Output is correct |
58 |
Correct |
59 ms |
14192 KB |
Output is correct |
59 |
Correct |
78 ms |
18680 KB |
Output is correct |
60 |
Correct |
104 ms |
23116 KB |
Output is correct |
61 |
Correct |
124 ms |
26692 KB |
Output is correct |
62 |
Correct |
134 ms |
29268 KB |
Output is correct |
63 |
Correct |
161 ms |
34820 KB |
Output is correct |
64 |
Correct |
175 ms |
36608 KB |
Output is correct |
65 |
Correct |
86 ms |
32128 KB |
Output is correct |
66 |
Correct |
87 ms |
32152 KB |
Output is correct |
67 |
Correct |
86 ms |
32172 KB |
Output is correct |
68 |
Correct |
154 ms |
34168 KB |
Output is correct |
69 |
Correct |
159 ms |
34268 KB |
Output is correct |
70 |
Correct |
159 ms |
34336 KB |
Output is correct |
71 |
Correct |
199 ms |
46904 KB |
Output is correct |
72 |
Correct |
190 ms |
46932 KB |
Output is correct |
73 |
Correct |
174 ms |
43068 KB |
Output is correct |
74 |
Correct |
182 ms |
43320 KB |
Output is correct |
75 |
Correct |
175 ms |
42180 KB |
Output is correct |
76 |
Correct |
176 ms |
42140 KB |
Output is correct |
77 |
Correct |
180 ms |
41288 KB |
Output is correct |
78 |
Correct |
179 ms |
40612 KB |
Output is correct |
79 |
Correct |
163 ms |
40308 KB |
Output is correct |