/**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author win11905
*/
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define vi vector<int>
#define iii tuple<int, int, int>
#define long long long
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
const long MOD = 1e9+7, LINF = 1e18 + 1e16;
const int INF = 1e9+1;
const double EPS = 1e-10;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
const int N = 3e5+5;
class firework {
private:
int n, m, cost[N], sz[N];
int slope[N], pos[N];
long start[N];
vector<int> g[N];
priority_queue<long> Q[N];
void dfs(int u) {
sz[u] = 1;
for(int &v : g[u]) {
dfs(v);
sz[u] += sz[v];
if(sz[v] > sz[g[u][0]]) swap(v, g[u][0]);
}
if(g[u].empty()) {
pos[u] = u;
start[u] = cost[u], slope[u] = -1;
Q[pos[u]].emplace(cost[u]), Q[u].emplace(cost[u]);
return;
}
for(int v : g[u]) {
slope[u] += slope[v], start[u] += start[v];
if(v == g[u][0])
pos[u] = pos[v];
else while(!Q[pos[v]].empty()) {
Q[pos[u]].emplace(Q[pos[v]].top());
Q[pos[v]].pop();
}
}
long opt1 = -1, opt2 = -1;
while(slope[u] + (int)Q[pos[u]].size() >= 0) {
swap(opt1, opt2);
opt1 = Q[pos[u]].top(), Q[pos[u]].pop();
}
Q[pos[u]].emplace(opt1 + cost[u]), Q[pos[u]].emplace(opt2 + cost[u]);
start[u] += cost[u];
}
public:
void solve(istream& cin, ostream& cout) {
cin >> n >> m;
for(int i = 2, par; i <= n+m; ++i) {
cin >> par >> cost[i];
g[par].emplace_back(i);
}
dfs(1);
vector<long> vec;
while(!Q[pos[1]].empty()) vec.emplace_back(Q[pos[1]].top()), Q[pos[1]].pop();
vec.emplace_back(0);
reverse(all(vec));
for(int i = 1; slope[1]; ++i) {
start[1] += slope[1] * (vec[i] - vec[i-1]);
slope[1]++;
}
cout << start[1] << endl;
}
};
class Solver {
public:
void solve(std::istream& in, std::ostream& out) {
firework *obj = new firework();
obj->solve(in, out);
}
};
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
Solver solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
23808 KB |
Output is correct |
2 |
Correct |
24 ms |
23780 KB |
Output is correct |
3 |
Correct |
24 ms |
23808 KB |
Output is correct |
4 |
Correct |
27 ms |
23808 KB |
Output is correct |
5 |
Correct |
29 ms |
23800 KB |
Output is correct |
6 |
Correct |
29 ms |
23808 KB |
Output is correct |
7 |
Correct |
26 ms |
23808 KB |
Output is correct |
8 |
Correct |
27 ms |
23808 KB |
Output is correct |
9 |
Correct |
26 ms |
23808 KB |
Output is correct |
10 |
Correct |
26 ms |
23808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
23816 KB |
Output is correct |
2 |
Correct |
24 ms |
23808 KB |
Output is correct |
3 |
Correct |
25 ms |
23808 KB |
Output is correct |
4 |
Correct |
26 ms |
23800 KB |
Output is correct |
5 |
Correct |
27 ms |
23764 KB |
Output is correct |
6 |
Correct |
29 ms |
23808 KB |
Output is correct |
7 |
Correct |
28 ms |
23808 KB |
Output is correct |
8 |
Correct |
25 ms |
23808 KB |
Output is correct |
9 |
Correct |
23 ms |
23808 KB |
Output is correct |
10 |
Correct |
29 ms |
23808 KB |
Output is correct |
11 |
Correct |
25 ms |
23808 KB |
Output is correct |
12 |
Correct |
28 ms |
23808 KB |
Output is correct |
13 |
Correct |
23 ms |
23808 KB |
Output is correct |
14 |
Correct |
25 ms |
23936 KB |
Output is correct |
15 |
Correct |
25 ms |
23936 KB |
Output is correct |
16 |
Correct |
25 ms |
23800 KB |
Output is correct |
17 |
Correct |
26 ms |
23800 KB |
Output is correct |
18 |
Correct |
25 ms |
23808 KB |
Output is correct |
19 |
Correct |
26 ms |
23928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
23808 KB |
Output is correct |
2 |
Correct |
24 ms |
23780 KB |
Output is correct |
3 |
Correct |
24 ms |
23808 KB |
Output is correct |
4 |
Correct |
27 ms |
23808 KB |
Output is correct |
5 |
Correct |
29 ms |
23800 KB |
Output is correct |
6 |
Correct |
29 ms |
23808 KB |
Output is correct |
7 |
Correct |
26 ms |
23808 KB |
Output is correct |
8 |
Correct |
27 ms |
23808 KB |
Output is correct |
9 |
Correct |
26 ms |
23808 KB |
Output is correct |
10 |
Correct |
26 ms |
23808 KB |
Output is correct |
11 |
Correct |
29 ms |
23816 KB |
Output is correct |
12 |
Correct |
24 ms |
23808 KB |
Output is correct |
13 |
Correct |
25 ms |
23808 KB |
Output is correct |
14 |
Correct |
26 ms |
23800 KB |
Output is correct |
15 |
Correct |
27 ms |
23764 KB |
Output is correct |
16 |
Correct |
29 ms |
23808 KB |
Output is correct |
17 |
Correct |
28 ms |
23808 KB |
Output is correct |
18 |
Correct |
25 ms |
23808 KB |
Output is correct |
19 |
Correct |
23 ms |
23808 KB |
Output is correct |
20 |
Correct |
29 ms |
23808 KB |
Output is correct |
21 |
Correct |
25 ms |
23808 KB |
Output is correct |
22 |
Correct |
28 ms |
23808 KB |
Output is correct |
23 |
Correct |
23 ms |
23808 KB |
Output is correct |
24 |
Correct |
25 ms |
23936 KB |
Output is correct |
25 |
Correct |
25 ms |
23936 KB |
Output is correct |
26 |
Correct |
25 ms |
23800 KB |
Output is correct |
27 |
Correct |
26 ms |
23800 KB |
Output is correct |
28 |
Correct |
25 ms |
23808 KB |
Output is correct |
29 |
Correct |
26 ms |
23928 KB |
Output is correct |
30 |
Correct |
27 ms |
23916 KB |
Output is correct |
31 |
Correct |
26 ms |
24104 KB |
Output is correct |
32 |
Correct |
27 ms |
24056 KB |
Output is correct |
33 |
Correct |
31 ms |
23928 KB |
Output is correct |
34 |
Correct |
27 ms |
24056 KB |
Output is correct |
35 |
Correct |
26 ms |
24056 KB |
Output is correct |
36 |
Correct |
28 ms |
24056 KB |
Output is correct |
37 |
Correct |
33 ms |
24156 KB |
Output is correct |
38 |
Correct |
29 ms |
24192 KB |
Output is correct |
39 |
Correct |
30 ms |
24188 KB |
Output is correct |
40 |
Correct |
32 ms |
24696 KB |
Output is correct |
41 |
Correct |
28 ms |
24676 KB |
Output is correct |
42 |
Correct |
32 ms |
24060 KB |
Output is correct |
43 |
Correct |
28 ms |
24440 KB |
Output is correct |
44 |
Correct |
32 ms |
24312 KB |
Output is correct |
45 |
Correct |
29 ms |
24312 KB |
Output is correct |
46 |
Correct |
29 ms |
24316 KB |
Output is correct |
47 |
Correct |
30 ms |
24312 KB |
Output is correct |
48 |
Correct |
29 ms |
24184 KB |
Output is correct |
49 |
Correct |
29 ms |
24300 KB |
Output is correct |
50 |
Correct |
29 ms |
24188 KB |
Output is correct |
51 |
Correct |
28 ms |
24188 KB |
Output is correct |
52 |
Correct |
30 ms |
24312 KB |
Output is correct |
53 |
Correct |
29 ms |
24388 KB |
Output is correct |
54 |
Correct |
30 ms |
24320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
23808 KB |
Output is correct |
2 |
Correct |
24 ms |
23780 KB |
Output is correct |
3 |
Correct |
24 ms |
23808 KB |
Output is correct |
4 |
Correct |
27 ms |
23808 KB |
Output is correct |
5 |
Correct |
29 ms |
23800 KB |
Output is correct |
6 |
Correct |
29 ms |
23808 KB |
Output is correct |
7 |
Correct |
26 ms |
23808 KB |
Output is correct |
8 |
Correct |
27 ms |
23808 KB |
Output is correct |
9 |
Correct |
26 ms |
23808 KB |
Output is correct |
10 |
Correct |
26 ms |
23808 KB |
Output is correct |
11 |
Correct |
29 ms |
23816 KB |
Output is correct |
12 |
Correct |
24 ms |
23808 KB |
Output is correct |
13 |
Correct |
25 ms |
23808 KB |
Output is correct |
14 |
Correct |
26 ms |
23800 KB |
Output is correct |
15 |
Correct |
27 ms |
23764 KB |
Output is correct |
16 |
Correct |
29 ms |
23808 KB |
Output is correct |
17 |
Correct |
28 ms |
23808 KB |
Output is correct |
18 |
Correct |
25 ms |
23808 KB |
Output is correct |
19 |
Correct |
23 ms |
23808 KB |
Output is correct |
20 |
Correct |
29 ms |
23808 KB |
Output is correct |
21 |
Correct |
25 ms |
23808 KB |
Output is correct |
22 |
Correct |
28 ms |
23808 KB |
Output is correct |
23 |
Correct |
23 ms |
23808 KB |
Output is correct |
24 |
Correct |
25 ms |
23936 KB |
Output is correct |
25 |
Correct |
25 ms |
23936 KB |
Output is correct |
26 |
Correct |
25 ms |
23800 KB |
Output is correct |
27 |
Correct |
26 ms |
23800 KB |
Output is correct |
28 |
Correct |
25 ms |
23808 KB |
Output is correct |
29 |
Correct |
26 ms |
23928 KB |
Output is correct |
30 |
Correct |
27 ms |
23916 KB |
Output is correct |
31 |
Correct |
26 ms |
24104 KB |
Output is correct |
32 |
Correct |
27 ms |
24056 KB |
Output is correct |
33 |
Correct |
31 ms |
23928 KB |
Output is correct |
34 |
Correct |
27 ms |
24056 KB |
Output is correct |
35 |
Correct |
26 ms |
24056 KB |
Output is correct |
36 |
Correct |
28 ms |
24056 KB |
Output is correct |
37 |
Correct |
33 ms |
24156 KB |
Output is correct |
38 |
Correct |
29 ms |
24192 KB |
Output is correct |
39 |
Correct |
30 ms |
24188 KB |
Output is correct |
40 |
Correct |
32 ms |
24696 KB |
Output is correct |
41 |
Correct |
28 ms |
24676 KB |
Output is correct |
42 |
Correct |
32 ms |
24060 KB |
Output is correct |
43 |
Correct |
28 ms |
24440 KB |
Output is correct |
44 |
Correct |
32 ms |
24312 KB |
Output is correct |
45 |
Correct |
29 ms |
24312 KB |
Output is correct |
46 |
Correct |
29 ms |
24316 KB |
Output is correct |
47 |
Correct |
30 ms |
24312 KB |
Output is correct |
48 |
Correct |
29 ms |
24184 KB |
Output is correct |
49 |
Correct |
29 ms |
24300 KB |
Output is correct |
50 |
Correct |
29 ms |
24188 KB |
Output is correct |
51 |
Correct |
28 ms |
24188 KB |
Output is correct |
52 |
Correct |
30 ms |
24312 KB |
Output is correct |
53 |
Correct |
29 ms |
24388 KB |
Output is correct |
54 |
Correct |
30 ms |
24320 KB |
Output is correct |
55 |
Correct |
31 ms |
24684 KB |
Output is correct |
56 |
Correct |
92 ms |
27280 KB |
Output is correct |
57 |
Correct |
117 ms |
29496 KB |
Output is correct |
58 |
Correct |
146 ms |
31344 KB |
Output is correct |
59 |
Correct |
203 ms |
33560 KB |
Output is correct |
60 |
Correct |
249 ms |
35824 KB |
Output is correct |
61 |
Correct |
307 ms |
38148 KB |
Output is correct |
62 |
Correct |
302 ms |
39532 KB |
Output is correct |
63 |
Correct |
363 ms |
42312 KB |
Output is correct |
64 |
Correct |
357 ms |
43116 KB |
Output is correct |
65 |
Correct |
166 ms |
75748 KB |
Output is correct |
66 |
Correct |
179 ms |
75716 KB |
Output is correct |
67 |
Correct |
178 ms |
38264 KB |
Output is correct |
68 |
Correct |
281 ms |
61160 KB |
Output is correct |
69 |
Correct |
329 ms |
57916 KB |
Output is correct |
70 |
Correct |
314 ms |
57960 KB |
Output is correct |
71 |
Correct |
304 ms |
56576 KB |
Output is correct |
72 |
Correct |
297 ms |
56672 KB |
Output is correct |
73 |
Correct |
276 ms |
53380 KB |
Output is correct |
74 |
Correct |
264 ms |
53372 KB |
Output is correct |
75 |
Correct |
301 ms |
52320 KB |
Output is correct |
76 |
Correct |
250 ms |
52576 KB |
Output is correct |
77 |
Correct |
283 ms |
51152 KB |
Output is correct |
78 |
Correct |
343 ms |
50488 KB |
Output is correct |
79 |
Correct |
404 ms |
49084 KB |
Output is correct |