// #pragma GCC optimize("Ofast")
// #pragma GCC targt("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
// #pragma GCC optimize("unroll-loops")
#include "bits/stdc++.h"
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/trie_policy.hpp>
// #include <ext/rope>
using namespace std;
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
#define li long long
#define ld long double
#define ii pair<int, int>
#define vi vector<int>
#define vvi vector<vi>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pf push_front
#define eb emplace_back
#define em emplace
#define ob pop_back
#define om pop
#define of pop_front
#define fr front
#define bc back
#define fori(i, a, b) for(int i = (int) (a); i <= (int) (b); ++i)
#define ford(i, a, b) for(int i = (int) (a); i >= (int) (b); --i)
#define all(x) begin(x), end(x)
#define sz(x) ((int)(x).size())
#define bitc __builtin_popcountll
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rand rng
#define endl '\n'
#define sp ' '
#define fastio() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
void solve();
signed main() {
// freopen("output.out","w",stdout);
// fastio();
int tc = 1;
// cin >> tc;
fori(test, 1, tc) {
solve();
}
return 0;
}
const ld pi = 4 * atan(1.0), eps = 1e-9;
#define int long long
const int maxn = 1e6 + 5;
int n,m;
int p[maxn], cost[maxn];
multiset<int> func[maxn];
int f0[maxn];
vi g[maxn];
void dfs(int i) {
if(i > n - m) {
f0[i] = cost[i];
fori(tt, 0, 1) func[i].em(cost[i]);
}
else {
assert(g[i].size());
for(int j: g[i]) {
dfs(j);
if(func[j].size() > func[i].size()) {
func[i].swap(func[j]);
}
for(int v: func[j]) {
func[i].em(v);
}
f0[i] += f0[j];
}
fori(tt, 1, g[i].size() - 1) {
func[i].erase(prev(func[i].end()));
}
if(i > 1) {
int r = *prev(func[i].end()); func[i].erase(func[i].find(r));
int l = *prev(func[i].end()); func[i].erase(func[i].find(l));
l += cost[i];
r += cost[i];
func[i].em(l);
func[i].em(r);
f0[i] += cost[i];
}
}
}
void solve() {
cin >> n >> m;
n += m;
fori(i, 2, n) {
cin >> p[i] >> cost[i];
g[p[i]].eb(i);
}
dfs(1);
int ans = f0[1];
func[1].erase(prev(func[1].end()));
int nowslope = 0;
while(func[1].size()) {
nowslope++;
int nowpoint = *prev(func[1].end());
func[1].erase(prev(func[1].end()));
int nxtpoint = (func[1].size()? *prev(func[1].end()): 0);
ans -= nowslope * abs(nowpoint - nxtpoint);
}
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
70732 KB |
Output is correct |
2 |
Correct |
41 ms |
70688 KB |
Output is correct |
3 |
Correct |
42 ms |
70676 KB |
Output is correct |
4 |
Correct |
34 ms |
70748 KB |
Output is correct |
5 |
Correct |
35 ms |
70724 KB |
Output is correct |
6 |
Correct |
36 ms |
70696 KB |
Output is correct |
7 |
Correct |
34 ms |
70800 KB |
Output is correct |
8 |
Correct |
35 ms |
70676 KB |
Output is correct |
9 |
Correct |
34 ms |
70812 KB |
Output is correct |
10 |
Correct |
35 ms |
70672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
70732 KB |
Output is correct |
2 |
Correct |
38 ms |
70732 KB |
Output is correct |
3 |
Correct |
34 ms |
70732 KB |
Output is correct |
4 |
Correct |
35 ms |
70792 KB |
Output is correct |
5 |
Correct |
35 ms |
70664 KB |
Output is correct |
6 |
Correct |
35 ms |
70784 KB |
Output is correct |
7 |
Correct |
35 ms |
70692 KB |
Output is correct |
8 |
Correct |
34 ms |
70692 KB |
Output is correct |
9 |
Correct |
37 ms |
70740 KB |
Output is correct |
10 |
Correct |
43 ms |
70732 KB |
Output is correct |
11 |
Correct |
39 ms |
70748 KB |
Output is correct |
12 |
Correct |
37 ms |
70676 KB |
Output is correct |
13 |
Correct |
36 ms |
70824 KB |
Output is correct |
14 |
Correct |
34 ms |
70728 KB |
Output is correct |
15 |
Correct |
34 ms |
70816 KB |
Output is correct |
16 |
Correct |
35 ms |
70724 KB |
Output is correct |
17 |
Correct |
35 ms |
70720 KB |
Output is correct |
18 |
Correct |
36 ms |
70732 KB |
Output is correct |
19 |
Correct |
36 ms |
70828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
70732 KB |
Output is correct |
2 |
Correct |
41 ms |
70688 KB |
Output is correct |
3 |
Correct |
42 ms |
70676 KB |
Output is correct |
4 |
Correct |
34 ms |
70748 KB |
Output is correct |
5 |
Correct |
35 ms |
70724 KB |
Output is correct |
6 |
Correct |
36 ms |
70696 KB |
Output is correct |
7 |
Correct |
34 ms |
70800 KB |
Output is correct |
8 |
Correct |
35 ms |
70676 KB |
Output is correct |
9 |
Correct |
34 ms |
70812 KB |
Output is correct |
10 |
Correct |
35 ms |
70672 KB |
Output is correct |
11 |
Correct |
43 ms |
70732 KB |
Output is correct |
12 |
Correct |
38 ms |
70732 KB |
Output is correct |
13 |
Correct |
34 ms |
70732 KB |
Output is correct |
14 |
Correct |
35 ms |
70792 KB |
Output is correct |
15 |
Correct |
35 ms |
70664 KB |
Output is correct |
16 |
Correct |
35 ms |
70784 KB |
Output is correct |
17 |
Correct |
35 ms |
70692 KB |
Output is correct |
18 |
Correct |
34 ms |
70692 KB |
Output is correct |
19 |
Correct |
37 ms |
70740 KB |
Output is correct |
20 |
Correct |
43 ms |
70732 KB |
Output is correct |
21 |
Correct |
39 ms |
70748 KB |
Output is correct |
22 |
Correct |
37 ms |
70676 KB |
Output is correct |
23 |
Correct |
36 ms |
70824 KB |
Output is correct |
24 |
Correct |
34 ms |
70728 KB |
Output is correct |
25 |
Correct |
34 ms |
70816 KB |
Output is correct |
26 |
Correct |
35 ms |
70724 KB |
Output is correct |
27 |
Correct |
35 ms |
70720 KB |
Output is correct |
28 |
Correct |
36 ms |
70732 KB |
Output is correct |
29 |
Correct |
36 ms |
70828 KB |
Output is correct |
30 |
Correct |
36 ms |
70740 KB |
Output is correct |
31 |
Correct |
36 ms |
70860 KB |
Output is correct |
32 |
Correct |
43 ms |
70944 KB |
Output is correct |
33 |
Correct |
38 ms |
70936 KB |
Output is correct |
34 |
Correct |
42 ms |
71024 KB |
Output is correct |
35 |
Correct |
39 ms |
71108 KB |
Output is correct |
36 |
Correct |
39 ms |
71260 KB |
Output is correct |
37 |
Correct |
44 ms |
71336 KB |
Output is correct |
38 |
Correct |
47 ms |
71288 KB |
Output is correct |
39 |
Correct |
39 ms |
71364 KB |
Output is correct |
40 |
Correct |
48 ms |
71628 KB |
Output is correct |
41 |
Correct |
45 ms |
71728 KB |
Output is correct |
42 |
Correct |
38 ms |
70960 KB |
Output is correct |
43 |
Correct |
38 ms |
71620 KB |
Output is correct |
44 |
Correct |
44 ms |
71560 KB |
Output is correct |
45 |
Correct |
38 ms |
71472 KB |
Output is correct |
46 |
Correct |
41 ms |
71840 KB |
Output is correct |
47 |
Correct |
40 ms |
71804 KB |
Output is correct |
48 |
Correct |
39 ms |
71612 KB |
Output is correct |
49 |
Correct |
41 ms |
71756 KB |
Output is correct |
50 |
Correct |
48 ms |
71496 KB |
Output is correct |
51 |
Correct |
43 ms |
71492 KB |
Output is correct |
52 |
Correct |
48 ms |
71648 KB |
Output is correct |
53 |
Correct |
40 ms |
71612 KB |
Output is correct |
54 |
Correct |
39 ms |
71748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
70732 KB |
Output is correct |
2 |
Correct |
41 ms |
70688 KB |
Output is correct |
3 |
Correct |
42 ms |
70676 KB |
Output is correct |
4 |
Correct |
34 ms |
70748 KB |
Output is correct |
5 |
Correct |
35 ms |
70724 KB |
Output is correct |
6 |
Correct |
36 ms |
70696 KB |
Output is correct |
7 |
Correct |
34 ms |
70800 KB |
Output is correct |
8 |
Correct |
35 ms |
70676 KB |
Output is correct |
9 |
Correct |
34 ms |
70812 KB |
Output is correct |
10 |
Correct |
35 ms |
70672 KB |
Output is correct |
11 |
Correct |
43 ms |
70732 KB |
Output is correct |
12 |
Correct |
38 ms |
70732 KB |
Output is correct |
13 |
Correct |
34 ms |
70732 KB |
Output is correct |
14 |
Correct |
35 ms |
70792 KB |
Output is correct |
15 |
Correct |
35 ms |
70664 KB |
Output is correct |
16 |
Correct |
35 ms |
70784 KB |
Output is correct |
17 |
Correct |
35 ms |
70692 KB |
Output is correct |
18 |
Correct |
34 ms |
70692 KB |
Output is correct |
19 |
Correct |
37 ms |
70740 KB |
Output is correct |
20 |
Correct |
43 ms |
70732 KB |
Output is correct |
21 |
Correct |
39 ms |
70748 KB |
Output is correct |
22 |
Correct |
37 ms |
70676 KB |
Output is correct |
23 |
Correct |
36 ms |
70824 KB |
Output is correct |
24 |
Correct |
34 ms |
70728 KB |
Output is correct |
25 |
Correct |
34 ms |
70816 KB |
Output is correct |
26 |
Correct |
35 ms |
70724 KB |
Output is correct |
27 |
Correct |
35 ms |
70720 KB |
Output is correct |
28 |
Correct |
36 ms |
70732 KB |
Output is correct |
29 |
Correct |
36 ms |
70828 KB |
Output is correct |
30 |
Correct |
36 ms |
70740 KB |
Output is correct |
31 |
Correct |
36 ms |
70860 KB |
Output is correct |
32 |
Correct |
43 ms |
70944 KB |
Output is correct |
33 |
Correct |
38 ms |
70936 KB |
Output is correct |
34 |
Correct |
42 ms |
71024 KB |
Output is correct |
35 |
Correct |
39 ms |
71108 KB |
Output is correct |
36 |
Correct |
39 ms |
71260 KB |
Output is correct |
37 |
Correct |
44 ms |
71336 KB |
Output is correct |
38 |
Correct |
47 ms |
71288 KB |
Output is correct |
39 |
Correct |
39 ms |
71364 KB |
Output is correct |
40 |
Correct |
48 ms |
71628 KB |
Output is correct |
41 |
Correct |
45 ms |
71728 KB |
Output is correct |
42 |
Correct |
38 ms |
70960 KB |
Output is correct |
43 |
Correct |
38 ms |
71620 KB |
Output is correct |
44 |
Correct |
44 ms |
71560 KB |
Output is correct |
45 |
Correct |
38 ms |
71472 KB |
Output is correct |
46 |
Correct |
41 ms |
71840 KB |
Output is correct |
47 |
Correct |
40 ms |
71804 KB |
Output is correct |
48 |
Correct |
39 ms |
71612 KB |
Output is correct |
49 |
Correct |
41 ms |
71756 KB |
Output is correct |
50 |
Correct |
48 ms |
71496 KB |
Output is correct |
51 |
Correct |
43 ms |
71492 KB |
Output is correct |
52 |
Correct |
48 ms |
71648 KB |
Output is correct |
53 |
Correct |
40 ms |
71612 KB |
Output is correct |
54 |
Correct |
39 ms |
71748 KB |
Output is correct |
55 |
Correct |
48 ms |
72444 KB |
Output is correct |
56 |
Correct |
96 ms |
77576 KB |
Output is correct |
57 |
Correct |
144 ms |
82884 KB |
Output is correct |
58 |
Correct |
176 ms |
86468 KB |
Output is correct |
59 |
Correct |
235 ms |
91592 KB |
Output is correct |
60 |
Correct |
302 ms |
97088 KB |
Output is correct |
61 |
Correct |
329 ms |
100612 KB |
Output is correct |
62 |
Correct |
376 ms |
104268 KB |
Output is correct |
63 |
Correct |
429 ms |
111596 KB |
Output is correct |
64 |
Correct |
480 ms |
112172 KB |
Output is correct |
65 |
Correct |
287 ms |
129464 KB |
Output is correct |
66 |
Correct |
312 ms |
129448 KB |
Output is correct |
67 |
Correct |
293 ms |
87312 KB |
Output is correct |
68 |
Correct |
378 ms |
124740 KB |
Output is correct |
69 |
Correct |
426 ms |
122264 KB |
Output is correct |
70 |
Correct |
424 ms |
122348 KB |
Output is correct |
71 |
Correct |
476 ms |
150344 KB |
Output is correct |
72 |
Correct |
469 ms |
149664 KB |
Output is correct |
73 |
Correct |
560 ms |
137112 KB |
Output is correct |
74 |
Correct |
514 ms |
137216 KB |
Output is correct |
75 |
Correct |
499 ms |
136260 KB |
Output is correct |
76 |
Correct |
519 ms |
136512 KB |
Output is correct |
77 |
Correct |
469 ms |
134208 KB |
Output is correct |
78 |
Correct |
488 ms |
131576 KB |
Output is correct |
79 |
Correct |
550 ms |
136488 KB |
Output is correct |