#include <bits/stdc++.h>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
const int MAXN = (int)1e5 + 5;
struct dp_t {
ll h0, a0;
multiset<ll> S;
dp_t() {
h0 = a0 = 0;
}
ll get(ll x) {
ll val = h0;
ll slope = a0;
ll pos = 0;
for (ll pt : S) {
if (pt >= x) {
break;
}
val += slope * (pt - pos);
pos = pt;
slope++;
}
val += (x - pos) * slope;
return val;
}
} dp[MAXN];
int par[MAXN], edge_len[MAXN];
vector<int> adj[MAXN];
int n, m;
int main() {
cin >> n >> m;
for (int i = 2; i <= n + m; i++) {
cin >> par[i] >> edge_len[i];
adj[par[i]].push_back(i);
}
for (int v = n + m; v > n; v--) {
dp[v].h0 = edge_len[v];
dp[v].a0 = -1;
dp[v].S.insert(edge_len[v]);
dp[v].S.insert(edge_len[v]);
}
for (int v = n; v > 0; v--) {
for (int to : adj[v]) {
dp[v].h0 += dp[to].h0;
dp[v].a0 += dp[to].a0;
for (ll pt : dp[to].S) {
dp[v].S.insert(pt);
}
dp[to].S.clear();
}
while (!dp[v].S.empty() && dp[v].a0 + sz(dp[v].S) > 1) {
dp[v].S.erase(prev(dp[v].S.end()));
}
assert(sz(dp[v].S) >= 2);
dp[v].h0 += edge_len[v];
ll P = *dp[v].S.rbegin();
dp[v].S.erase(prev(dp[v].S.end()));
ll Q = *dp[v].S.rbegin();
dp[v].S.erase(prev(dp[v].S.end()));
dp[v].S.insert(P + edge_len[v]);
dp[v].S.insert(Q + edge_len[v]);
}
ll val = dp[1].h0, slope = dp[1].a0;
ll pos = 0;
for (ll pt : dp[1].S) {
val += (pt - pos) * slope;
pos = pt;
slope++;
if (slope == 0) break;
}
cout << val << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8908 KB |
Output is correct |
2 |
Correct |
6 ms |
8908 KB |
Output is correct |
3 |
Correct |
7 ms |
8908 KB |
Output is correct |
4 |
Correct |
5 ms |
8880 KB |
Output is correct |
5 |
Correct |
5 ms |
8908 KB |
Output is correct |
6 |
Correct |
5 ms |
8908 KB |
Output is correct |
7 |
Correct |
6 ms |
8908 KB |
Output is correct |
8 |
Correct |
5 ms |
8908 KB |
Output is correct |
9 |
Correct |
5 ms |
8908 KB |
Output is correct |
10 |
Correct |
5 ms |
8908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8908 KB |
Output is correct |
2 |
Correct |
5 ms |
8908 KB |
Output is correct |
3 |
Correct |
5 ms |
8908 KB |
Output is correct |
4 |
Correct |
5 ms |
8908 KB |
Output is correct |
5 |
Correct |
5 ms |
8908 KB |
Output is correct |
6 |
Correct |
5 ms |
8908 KB |
Output is correct |
7 |
Correct |
6 ms |
8908 KB |
Output is correct |
8 |
Correct |
5 ms |
8916 KB |
Output is correct |
9 |
Correct |
6 ms |
8908 KB |
Output is correct |
10 |
Correct |
5 ms |
8908 KB |
Output is correct |
11 |
Correct |
5 ms |
8908 KB |
Output is correct |
12 |
Correct |
5 ms |
8852 KB |
Output is correct |
13 |
Correct |
5 ms |
8908 KB |
Output is correct |
14 |
Correct |
5 ms |
8804 KB |
Output is correct |
15 |
Correct |
5 ms |
8908 KB |
Output is correct |
16 |
Correct |
6 ms |
8872 KB |
Output is correct |
17 |
Correct |
6 ms |
8908 KB |
Output is correct |
18 |
Correct |
6 ms |
8908 KB |
Output is correct |
19 |
Correct |
6 ms |
8908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8908 KB |
Output is correct |
2 |
Correct |
6 ms |
8908 KB |
Output is correct |
3 |
Correct |
7 ms |
8908 KB |
Output is correct |
4 |
Correct |
5 ms |
8880 KB |
Output is correct |
5 |
Correct |
5 ms |
8908 KB |
Output is correct |
6 |
Correct |
5 ms |
8908 KB |
Output is correct |
7 |
Correct |
6 ms |
8908 KB |
Output is correct |
8 |
Correct |
5 ms |
8908 KB |
Output is correct |
9 |
Correct |
5 ms |
8908 KB |
Output is correct |
10 |
Correct |
5 ms |
8908 KB |
Output is correct |
11 |
Correct |
5 ms |
8908 KB |
Output is correct |
12 |
Correct |
5 ms |
8908 KB |
Output is correct |
13 |
Correct |
5 ms |
8908 KB |
Output is correct |
14 |
Correct |
5 ms |
8908 KB |
Output is correct |
15 |
Correct |
5 ms |
8908 KB |
Output is correct |
16 |
Correct |
5 ms |
8908 KB |
Output is correct |
17 |
Correct |
6 ms |
8908 KB |
Output is correct |
18 |
Correct |
5 ms |
8916 KB |
Output is correct |
19 |
Correct |
6 ms |
8908 KB |
Output is correct |
20 |
Correct |
5 ms |
8908 KB |
Output is correct |
21 |
Correct |
5 ms |
8908 KB |
Output is correct |
22 |
Correct |
5 ms |
8852 KB |
Output is correct |
23 |
Correct |
5 ms |
8908 KB |
Output is correct |
24 |
Correct |
5 ms |
8804 KB |
Output is correct |
25 |
Correct |
5 ms |
8908 KB |
Output is correct |
26 |
Correct |
6 ms |
8872 KB |
Output is correct |
27 |
Correct |
6 ms |
8908 KB |
Output is correct |
28 |
Correct |
6 ms |
8908 KB |
Output is correct |
29 |
Correct |
6 ms |
8908 KB |
Output is correct |
30 |
Correct |
6 ms |
8908 KB |
Output is correct |
31 |
Correct |
8 ms |
8908 KB |
Output is correct |
32 |
Correct |
9 ms |
8936 KB |
Output is correct |
33 |
Correct |
11 ms |
9040 KB |
Output is correct |
34 |
Correct |
13 ms |
9076 KB |
Output is correct |
35 |
Correct |
20 ms |
9184 KB |
Output is correct |
36 |
Correct |
18 ms |
9220 KB |
Output is correct |
37 |
Correct |
18 ms |
9264 KB |
Output is correct |
38 |
Correct |
37 ms |
9264 KB |
Output is correct |
39 |
Correct |
21 ms |
9292 KB |
Output is correct |
40 |
Correct |
10 ms |
9164 KB |
Output is correct |
41 |
Correct |
12 ms |
9168 KB |
Output is correct |
42 |
Correct |
10 ms |
9164 KB |
Output is correct |
43 |
Correct |
368 ms |
9308 KB |
Output is correct |
44 |
Correct |
279 ms |
9320 KB |
Output is correct |
45 |
Correct |
259 ms |
9328 KB |
Output is correct |
46 |
Correct |
11 ms |
9436 KB |
Output is correct |
47 |
Correct |
11 ms |
9420 KB |
Output is correct |
48 |
Correct |
13 ms |
9436 KB |
Output is correct |
49 |
Correct |
11 ms |
9420 KB |
Output is correct |
50 |
Correct |
10 ms |
9308 KB |
Output is correct |
51 |
Correct |
10 ms |
9408 KB |
Output is correct |
52 |
Correct |
18 ms |
9436 KB |
Output is correct |
53 |
Correct |
14 ms |
9432 KB |
Output is correct |
54 |
Correct |
10 ms |
9420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8908 KB |
Output is correct |
2 |
Correct |
6 ms |
8908 KB |
Output is correct |
3 |
Correct |
7 ms |
8908 KB |
Output is correct |
4 |
Correct |
5 ms |
8880 KB |
Output is correct |
5 |
Correct |
5 ms |
8908 KB |
Output is correct |
6 |
Correct |
5 ms |
8908 KB |
Output is correct |
7 |
Correct |
6 ms |
8908 KB |
Output is correct |
8 |
Correct |
5 ms |
8908 KB |
Output is correct |
9 |
Correct |
5 ms |
8908 KB |
Output is correct |
10 |
Correct |
5 ms |
8908 KB |
Output is correct |
11 |
Correct |
5 ms |
8908 KB |
Output is correct |
12 |
Correct |
5 ms |
8908 KB |
Output is correct |
13 |
Correct |
5 ms |
8908 KB |
Output is correct |
14 |
Correct |
5 ms |
8908 KB |
Output is correct |
15 |
Correct |
5 ms |
8908 KB |
Output is correct |
16 |
Correct |
5 ms |
8908 KB |
Output is correct |
17 |
Correct |
6 ms |
8908 KB |
Output is correct |
18 |
Correct |
5 ms |
8916 KB |
Output is correct |
19 |
Correct |
6 ms |
8908 KB |
Output is correct |
20 |
Correct |
5 ms |
8908 KB |
Output is correct |
21 |
Correct |
5 ms |
8908 KB |
Output is correct |
22 |
Correct |
5 ms |
8852 KB |
Output is correct |
23 |
Correct |
5 ms |
8908 KB |
Output is correct |
24 |
Correct |
5 ms |
8804 KB |
Output is correct |
25 |
Correct |
5 ms |
8908 KB |
Output is correct |
26 |
Correct |
6 ms |
8872 KB |
Output is correct |
27 |
Correct |
6 ms |
8908 KB |
Output is correct |
28 |
Correct |
6 ms |
8908 KB |
Output is correct |
29 |
Correct |
6 ms |
8908 KB |
Output is correct |
30 |
Correct |
6 ms |
8908 KB |
Output is correct |
31 |
Correct |
8 ms |
8908 KB |
Output is correct |
32 |
Correct |
9 ms |
8936 KB |
Output is correct |
33 |
Correct |
11 ms |
9040 KB |
Output is correct |
34 |
Correct |
13 ms |
9076 KB |
Output is correct |
35 |
Correct |
20 ms |
9184 KB |
Output is correct |
36 |
Correct |
18 ms |
9220 KB |
Output is correct |
37 |
Correct |
18 ms |
9264 KB |
Output is correct |
38 |
Correct |
37 ms |
9264 KB |
Output is correct |
39 |
Correct |
21 ms |
9292 KB |
Output is correct |
40 |
Correct |
10 ms |
9164 KB |
Output is correct |
41 |
Correct |
12 ms |
9168 KB |
Output is correct |
42 |
Correct |
10 ms |
9164 KB |
Output is correct |
43 |
Correct |
368 ms |
9308 KB |
Output is correct |
44 |
Correct |
279 ms |
9320 KB |
Output is correct |
45 |
Correct |
259 ms |
9328 KB |
Output is correct |
46 |
Correct |
11 ms |
9436 KB |
Output is correct |
47 |
Correct |
11 ms |
9420 KB |
Output is correct |
48 |
Correct |
13 ms |
9436 KB |
Output is correct |
49 |
Correct |
11 ms |
9420 KB |
Output is correct |
50 |
Correct |
10 ms |
9308 KB |
Output is correct |
51 |
Correct |
10 ms |
9408 KB |
Output is correct |
52 |
Correct |
18 ms |
9436 KB |
Output is correct |
53 |
Correct |
14 ms |
9432 KB |
Output is correct |
54 |
Correct |
10 ms |
9420 KB |
Output is correct |
55 |
Correct |
84 ms |
9804 KB |
Output is correct |
56 |
Correct |
882 ms |
12244 KB |
Output is correct |
57 |
Correct |
1917 ms |
14372 KB |
Output is correct |
58 |
Runtime error |
111 ms |
23852 KB |
Execution killed with signal 11 |
59 |
Halted |
0 ms |
0 KB |
- |