#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef pair<ii, int> ri3;
#define mp make_pair
#define pb push_back
#define fi first
#define sc second
#define SZ(x) (int)(x).size()
#define ALL(x) begin(x), end(x)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define RFOR(i, a, b) for (int i = a; i >= b; --i)
const int N = 3e5+5;
int n, m;
vector<ii> al[N];
struct Slope {
priority_queue<ll> points;
ll a = 0, b = 0;
void clean() {
while (a > 1) {
--a;
b += points.top();
points.pop();
}
}
};
void print(priority_queue<ll> pq) { while (!pq.empty()) cout << pq.top() << ' ', pq.pop(); cout << '\n'; }
Slope dfs(int u, int p)
{
Slope cur;
if (SZ(al[u]) == 1 and al[u][0].fi == p)
cur.points.push(0), cur.points.push(0), cur.a = 1, cur.b = 0;
else for (auto v : al[u]) if (v.fi != p) {
Slope nxt = dfs(v.fi, u);
ll x = nxt.points.top(); nxt.points.pop();
ll y = nxt.points.top(); nxt.points.pop();
nxt.points.push(x+v.sc);
nxt.points.push(y+v.sc);
nxt.b -= v.sc;
//cout << "POINT " << x+v.sc << " " << nxt.a << " " << nxt.b << endl;
if (SZ(nxt.points) > SZ(cur.points)) swap(nxt, cur);
while (!nxt.points.empty()) {
cur.points.push(nxt.points.top());
nxt.points.pop();
}
cur.a += nxt.a; cur.b += nxt.b;
}
//cout << u << " :: " << cur.a << " " << cur.b << "\t\t\tSZ " << cur.points.size() << endl;
cur.clean();
return cur;
}
int main()
{
//freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
FOR(i, 2, n+m){
int p, c; cin >> p >> c;
al[i].emplace_back(p, c);
al[p].emplace_back(i, c);
}
Slope ans = dfs(1, 0);
//print(ans.points); cout << ans.points.top() << " " << ans.a << " " << ans.b << endl;
cout << (ll)ans.points.top()*ans.a + ans.b << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
7424 KB |
Output is correct |
2 |
Correct |
8 ms |
7424 KB |
Output is correct |
3 |
Correct |
9 ms |
7424 KB |
Output is correct |
4 |
Correct |
14 ms |
7424 KB |
Output is correct |
5 |
Correct |
8 ms |
7424 KB |
Output is correct |
6 |
Correct |
9 ms |
7424 KB |
Output is correct |
7 |
Correct |
9 ms |
7424 KB |
Output is correct |
8 |
Correct |
11 ms |
7424 KB |
Output is correct |
9 |
Correct |
10 ms |
7424 KB |
Output is correct |
10 |
Correct |
10 ms |
7424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
7424 KB |
Output is correct |
2 |
Correct |
9 ms |
7424 KB |
Output is correct |
3 |
Correct |
8 ms |
7400 KB |
Output is correct |
4 |
Correct |
8 ms |
7424 KB |
Output is correct |
5 |
Correct |
11 ms |
7364 KB |
Output is correct |
6 |
Correct |
10 ms |
7424 KB |
Output is correct |
7 |
Correct |
10 ms |
7424 KB |
Output is correct |
8 |
Correct |
9 ms |
7424 KB |
Output is correct |
9 |
Correct |
9 ms |
7424 KB |
Output is correct |
10 |
Correct |
8 ms |
7424 KB |
Output is correct |
11 |
Correct |
13 ms |
7424 KB |
Output is correct |
12 |
Correct |
10 ms |
7424 KB |
Output is correct |
13 |
Correct |
10 ms |
7424 KB |
Output is correct |
14 |
Correct |
12 ms |
7424 KB |
Output is correct |
15 |
Correct |
11 ms |
7424 KB |
Output is correct |
16 |
Correct |
9 ms |
7424 KB |
Output is correct |
17 |
Correct |
10 ms |
7424 KB |
Output is correct |
18 |
Correct |
8 ms |
7424 KB |
Output is correct |
19 |
Correct |
9 ms |
7424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
7424 KB |
Output is correct |
2 |
Correct |
8 ms |
7424 KB |
Output is correct |
3 |
Correct |
9 ms |
7424 KB |
Output is correct |
4 |
Correct |
14 ms |
7424 KB |
Output is correct |
5 |
Correct |
8 ms |
7424 KB |
Output is correct |
6 |
Correct |
9 ms |
7424 KB |
Output is correct |
7 |
Correct |
9 ms |
7424 KB |
Output is correct |
8 |
Correct |
11 ms |
7424 KB |
Output is correct |
9 |
Correct |
10 ms |
7424 KB |
Output is correct |
10 |
Correct |
10 ms |
7424 KB |
Output is correct |
11 |
Correct |
9 ms |
7424 KB |
Output is correct |
12 |
Correct |
9 ms |
7424 KB |
Output is correct |
13 |
Correct |
8 ms |
7400 KB |
Output is correct |
14 |
Correct |
8 ms |
7424 KB |
Output is correct |
15 |
Correct |
11 ms |
7364 KB |
Output is correct |
16 |
Correct |
10 ms |
7424 KB |
Output is correct |
17 |
Correct |
10 ms |
7424 KB |
Output is correct |
18 |
Correct |
9 ms |
7424 KB |
Output is correct |
19 |
Correct |
9 ms |
7424 KB |
Output is correct |
20 |
Correct |
8 ms |
7424 KB |
Output is correct |
21 |
Correct |
13 ms |
7424 KB |
Output is correct |
22 |
Correct |
10 ms |
7424 KB |
Output is correct |
23 |
Correct |
10 ms |
7424 KB |
Output is correct |
24 |
Correct |
12 ms |
7424 KB |
Output is correct |
25 |
Correct |
11 ms |
7424 KB |
Output is correct |
26 |
Correct |
9 ms |
7424 KB |
Output is correct |
27 |
Correct |
10 ms |
7424 KB |
Output is correct |
28 |
Correct |
8 ms |
7424 KB |
Output is correct |
29 |
Correct |
9 ms |
7424 KB |
Output is correct |
30 |
Correct |
9 ms |
7424 KB |
Output is correct |
31 |
Correct |
9 ms |
7424 KB |
Output is correct |
32 |
Correct |
12 ms |
7444 KB |
Output is correct |
33 |
Correct |
13 ms |
7552 KB |
Output is correct |
34 |
Correct |
13 ms |
7552 KB |
Output is correct |
35 |
Correct |
12 ms |
7680 KB |
Output is correct |
36 |
Correct |
11 ms |
7680 KB |
Output is correct |
37 |
Correct |
16 ms |
7724 KB |
Output is correct |
38 |
Correct |
13 ms |
7680 KB |
Output is correct |
39 |
Correct |
13 ms |
7680 KB |
Output is correct |
40 |
Correct |
13 ms |
8448 KB |
Output is correct |
41 |
Correct |
13 ms |
8448 KB |
Output is correct |
42 |
Correct |
12 ms |
7680 KB |
Output is correct |
43 |
Correct |
12 ms |
8192 KB |
Output is correct |
44 |
Correct |
13 ms |
8064 KB |
Output is correct |
45 |
Correct |
16 ms |
8064 KB |
Output is correct |
46 |
Correct |
15 ms |
7808 KB |
Output is correct |
47 |
Correct |
14 ms |
7808 KB |
Output is correct |
48 |
Correct |
13 ms |
7808 KB |
Output is correct |
49 |
Correct |
14 ms |
7808 KB |
Output is correct |
50 |
Correct |
13 ms |
7680 KB |
Output is correct |
51 |
Correct |
12 ms |
7652 KB |
Output is correct |
52 |
Correct |
13 ms |
7808 KB |
Output is correct |
53 |
Correct |
12 ms |
7808 KB |
Output is correct |
54 |
Correct |
11 ms |
7808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
7424 KB |
Output is correct |
2 |
Correct |
8 ms |
7424 KB |
Output is correct |
3 |
Correct |
9 ms |
7424 KB |
Output is correct |
4 |
Correct |
14 ms |
7424 KB |
Output is correct |
5 |
Correct |
8 ms |
7424 KB |
Output is correct |
6 |
Correct |
9 ms |
7424 KB |
Output is correct |
7 |
Correct |
9 ms |
7424 KB |
Output is correct |
8 |
Correct |
11 ms |
7424 KB |
Output is correct |
9 |
Correct |
10 ms |
7424 KB |
Output is correct |
10 |
Correct |
10 ms |
7424 KB |
Output is correct |
11 |
Correct |
9 ms |
7424 KB |
Output is correct |
12 |
Correct |
9 ms |
7424 KB |
Output is correct |
13 |
Correct |
8 ms |
7400 KB |
Output is correct |
14 |
Correct |
8 ms |
7424 KB |
Output is correct |
15 |
Correct |
11 ms |
7364 KB |
Output is correct |
16 |
Correct |
10 ms |
7424 KB |
Output is correct |
17 |
Correct |
10 ms |
7424 KB |
Output is correct |
18 |
Correct |
9 ms |
7424 KB |
Output is correct |
19 |
Correct |
9 ms |
7424 KB |
Output is correct |
20 |
Correct |
8 ms |
7424 KB |
Output is correct |
21 |
Correct |
13 ms |
7424 KB |
Output is correct |
22 |
Correct |
10 ms |
7424 KB |
Output is correct |
23 |
Correct |
10 ms |
7424 KB |
Output is correct |
24 |
Correct |
12 ms |
7424 KB |
Output is correct |
25 |
Correct |
11 ms |
7424 KB |
Output is correct |
26 |
Correct |
9 ms |
7424 KB |
Output is correct |
27 |
Correct |
10 ms |
7424 KB |
Output is correct |
28 |
Correct |
8 ms |
7424 KB |
Output is correct |
29 |
Correct |
9 ms |
7424 KB |
Output is correct |
30 |
Correct |
9 ms |
7424 KB |
Output is correct |
31 |
Correct |
9 ms |
7424 KB |
Output is correct |
32 |
Correct |
12 ms |
7444 KB |
Output is correct |
33 |
Correct |
13 ms |
7552 KB |
Output is correct |
34 |
Correct |
13 ms |
7552 KB |
Output is correct |
35 |
Correct |
12 ms |
7680 KB |
Output is correct |
36 |
Correct |
11 ms |
7680 KB |
Output is correct |
37 |
Correct |
16 ms |
7724 KB |
Output is correct |
38 |
Correct |
13 ms |
7680 KB |
Output is correct |
39 |
Correct |
13 ms |
7680 KB |
Output is correct |
40 |
Correct |
13 ms |
8448 KB |
Output is correct |
41 |
Correct |
13 ms |
8448 KB |
Output is correct |
42 |
Correct |
12 ms |
7680 KB |
Output is correct |
43 |
Correct |
12 ms |
8192 KB |
Output is correct |
44 |
Correct |
13 ms |
8064 KB |
Output is correct |
45 |
Correct |
16 ms |
8064 KB |
Output is correct |
46 |
Correct |
15 ms |
7808 KB |
Output is correct |
47 |
Correct |
14 ms |
7808 KB |
Output is correct |
48 |
Correct |
13 ms |
7808 KB |
Output is correct |
49 |
Correct |
14 ms |
7808 KB |
Output is correct |
50 |
Correct |
13 ms |
7680 KB |
Output is correct |
51 |
Correct |
12 ms |
7652 KB |
Output is correct |
52 |
Correct |
13 ms |
7808 KB |
Output is correct |
53 |
Correct |
12 ms |
7808 KB |
Output is correct |
54 |
Correct |
11 ms |
7808 KB |
Output is correct |
55 |
Correct |
18 ms |
8192 KB |
Output is correct |
56 |
Correct |
50 ms |
10616 KB |
Output is correct |
57 |
Correct |
91 ms |
12356 KB |
Output is correct |
58 |
Correct |
126 ms |
13772 KB |
Output is correct |
59 |
Correct |
150 ms |
15772 KB |
Output is correct |
60 |
Correct |
212 ms |
17460 KB |
Output is correct |
61 |
Correct |
212 ms |
19480 KB |
Output is correct |
62 |
Correct |
256 ms |
20516 KB |
Output is correct |
63 |
Correct |
356 ms |
22932 KB |
Output is correct |
64 |
Correct |
394 ms |
23328 KB |
Output is correct |
65 |
Correct |
206 ms |
73336 KB |
Output is correct |
66 |
Correct |
189 ms |
73336 KB |
Output is correct |
67 |
Correct |
176 ms |
21752 KB |
Output is correct |
68 |
Correct |
199 ms |
51824 KB |
Output is correct |
69 |
Correct |
275 ms |
47212 KB |
Output is correct |
70 |
Correct |
338 ms |
47268 KB |
Output is correct |
71 |
Correct |
279 ms |
29284 KB |
Output is correct |
72 |
Correct |
205 ms |
29116 KB |
Output is correct |
73 |
Correct |
181 ms |
28260 KB |
Output is correct |
74 |
Correct |
171 ms |
28372 KB |
Output is correct |
75 |
Correct |
211 ms |
27704 KB |
Output is correct |
76 |
Correct |
235 ms |
27676 KB |
Output is correct |
77 |
Correct |
239 ms |
28096 KB |
Output is correct |
78 |
Correct |
235 ms |
28040 KB |
Output is correct |
79 |
Correct |
255 ms |
30932 KB |
Output is correct |