#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s; i >= e; i--)
typedef long long ll;
typedef long double ld;
#define MP make_pair
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define MT make_tuple
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define pb emplace_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
#define INF 1000000005
#define LINF 1000000000000000005
#define MOD 1000000007
#define MAXN 300005
int n, m;
int p[MAXN], c[MAXN];
struct lines {
ll m, c; // gradient and y-intercept of the right-most line
priority_queue<ll> pts; // points where slope changes
void join(lines& o) {
if (o.pts.size() > pts.size()) swap(o.pts, pts);
while (!o.pts.empty()) {
pts.push(o.pts.top());
o.pts.pop();
}
m += o.m;
c += o.c;
}
} dp[MAXN];
int main() {
scanf("%d%d", &n, &m);
REP (i, 2, n + m + 1) {
scanf("%d%d", &p[i], &c[i]);
}
REP (i, n + 1, n + m + 1) {
dp[i].pts.push(c[i]);
dp[i].pts.push(c[i]);
dp[i].m = 1;
dp[i].c = -c[i];
dp[p[i]].join(dp[i]);
}
RREP (i, n, 2) {
while (dp[i].m > 1) {
dp[i].m--;
dp[i].c += dp[i].pts.top();
dp[i].pts.pop();
}
ll fi = dp[i].pts.top(); dp[i].pts.pop();
ll se = dp[i].pts.top(); dp[i].pts.pop();
dp[i].pts.push(fi + c[i]);
dp[i].pts.push(se + c[i]);
dp[i].c -= c[i];
dp[p[i]].join(dp[i]);
}
while (dp[1].m > 0) {
dp[1].m--;
dp[1].c += dp[1].pts.top();
dp[1].pts.pop();
}
printf("%lld\n", dp[1].c);
return 0;
}
/*
4 6
1 5
2 5
2 8
3 3
3 2
3 3
2 9
4 4
4 3
*/
Compilation message
fireworks.cpp: In function 'int main()':
fireworks.cpp:49:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
fireworks.cpp:51:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | scanf("%d%d", &p[i], &c[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14412 KB |
Output is correct |
2 |
Correct |
9 ms |
14284 KB |
Output is correct |
3 |
Correct |
8 ms |
14464 KB |
Output is correct |
4 |
Correct |
8 ms |
14356 KB |
Output is correct |
5 |
Correct |
8 ms |
14408 KB |
Output is correct |
6 |
Correct |
8 ms |
14324 KB |
Output is correct |
7 |
Correct |
9 ms |
14284 KB |
Output is correct |
8 |
Correct |
8 ms |
14284 KB |
Output is correct |
9 |
Correct |
9 ms |
14284 KB |
Output is correct |
10 |
Correct |
9 ms |
14284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14284 KB |
Output is correct |
2 |
Correct |
8 ms |
14284 KB |
Output is correct |
3 |
Correct |
9 ms |
14284 KB |
Output is correct |
4 |
Correct |
8 ms |
14284 KB |
Output is correct |
5 |
Correct |
8 ms |
14284 KB |
Output is correct |
6 |
Correct |
9 ms |
14380 KB |
Output is correct |
7 |
Correct |
8 ms |
14284 KB |
Output is correct |
8 |
Correct |
9 ms |
14392 KB |
Output is correct |
9 |
Correct |
8 ms |
14284 KB |
Output is correct |
10 |
Correct |
8 ms |
14400 KB |
Output is correct |
11 |
Correct |
9 ms |
14340 KB |
Output is correct |
12 |
Correct |
8 ms |
14468 KB |
Output is correct |
13 |
Correct |
9 ms |
14412 KB |
Output is correct |
14 |
Correct |
8 ms |
14284 KB |
Output is correct |
15 |
Correct |
9 ms |
14396 KB |
Output is correct |
16 |
Correct |
8 ms |
14316 KB |
Output is correct |
17 |
Correct |
10 ms |
14412 KB |
Output is correct |
18 |
Correct |
8 ms |
14340 KB |
Output is correct |
19 |
Correct |
7 ms |
14340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14412 KB |
Output is correct |
2 |
Correct |
9 ms |
14284 KB |
Output is correct |
3 |
Correct |
8 ms |
14464 KB |
Output is correct |
4 |
Correct |
8 ms |
14356 KB |
Output is correct |
5 |
Correct |
8 ms |
14408 KB |
Output is correct |
6 |
Correct |
8 ms |
14324 KB |
Output is correct |
7 |
Correct |
9 ms |
14284 KB |
Output is correct |
8 |
Correct |
8 ms |
14284 KB |
Output is correct |
9 |
Correct |
9 ms |
14284 KB |
Output is correct |
10 |
Correct |
9 ms |
14284 KB |
Output is correct |
11 |
Correct |
8 ms |
14284 KB |
Output is correct |
12 |
Correct |
8 ms |
14284 KB |
Output is correct |
13 |
Correct |
9 ms |
14284 KB |
Output is correct |
14 |
Correct |
8 ms |
14284 KB |
Output is correct |
15 |
Correct |
8 ms |
14284 KB |
Output is correct |
16 |
Correct |
9 ms |
14380 KB |
Output is correct |
17 |
Correct |
8 ms |
14284 KB |
Output is correct |
18 |
Correct |
9 ms |
14392 KB |
Output is correct |
19 |
Correct |
8 ms |
14284 KB |
Output is correct |
20 |
Correct |
8 ms |
14400 KB |
Output is correct |
21 |
Correct |
9 ms |
14340 KB |
Output is correct |
22 |
Correct |
8 ms |
14468 KB |
Output is correct |
23 |
Correct |
9 ms |
14412 KB |
Output is correct |
24 |
Correct |
8 ms |
14284 KB |
Output is correct |
25 |
Correct |
9 ms |
14396 KB |
Output is correct |
26 |
Correct |
8 ms |
14316 KB |
Output is correct |
27 |
Correct |
10 ms |
14412 KB |
Output is correct |
28 |
Correct |
8 ms |
14340 KB |
Output is correct |
29 |
Correct |
7 ms |
14340 KB |
Output is correct |
30 |
Correct |
9 ms |
14412 KB |
Output is correct |
31 |
Correct |
9 ms |
14416 KB |
Output is correct |
32 |
Correct |
9 ms |
14416 KB |
Output is correct |
33 |
Correct |
10 ms |
14412 KB |
Output is correct |
34 |
Correct |
11 ms |
14520 KB |
Output is correct |
35 |
Correct |
10 ms |
14540 KB |
Output is correct |
36 |
Correct |
11 ms |
14588 KB |
Output is correct |
37 |
Correct |
13 ms |
14636 KB |
Output is correct |
38 |
Correct |
11 ms |
14668 KB |
Output is correct |
39 |
Correct |
10 ms |
14668 KB |
Output is correct |
40 |
Correct |
10 ms |
14424 KB |
Output is correct |
41 |
Correct |
9 ms |
14412 KB |
Output is correct |
42 |
Correct |
9 ms |
14412 KB |
Output is correct |
43 |
Correct |
10 ms |
14632 KB |
Output is correct |
44 |
Correct |
10 ms |
14660 KB |
Output is correct |
45 |
Correct |
10 ms |
14540 KB |
Output is correct |
46 |
Correct |
13 ms |
14788 KB |
Output is correct |
47 |
Correct |
12 ms |
14796 KB |
Output is correct |
48 |
Correct |
13 ms |
14788 KB |
Output is correct |
49 |
Correct |
11 ms |
14796 KB |
Output is correct |
50 |
Correct |
10 ms |
14668 KB |
Output is correct |
51 |
Correct |
11 ms |
14668 KB |
Output is correct |
52 |
Correct |
10 ms |
14680 KB |
Output is correct |
53 |
Correct |
11 ms |
14660 KB |
Output is correct |
54 |
Correct |
11 ms |
14796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14412 KB |
Output is correct |
2 |
Correct |
9 ms |
14284 KB |
Output is correct |
3 |
Correct |
8 ms |
14464 KB |
Output is correct |
4 |
Correct |
8 ms |
14356 KB |
Output is correct |
5 |
Correct |
8 ms |
14408 KB |
Output is correct |
6 |
Correct |
8 ms |
14324 KB |
Output is correct |
7 |
Correct |
9 ms |
14284 KB |
Output is correct |
8 |
Correct |
8 ms |
14284 KB |
Output is correct |
9 |
Correct |
9 ms |
14284 KB |
Output is correct |
10 |
Correct |
9 ms |
14284 KB |
Output is correct |
11 |
Correct |
8 ms |
14284 KB |
Output is correct |
12 |
Correct |
8 ms |
14284 KB |
Output is correct |
13 |
Correct |
9 ms |
14284 KB |
Output is correct |
14 |
Correct |
8 ms |
14284 KB |
Output is correct |
15 |
Correct |
8 ms |
14284 KB |
Output is correct |
16 |
Correct |
9 ms |
14380 KB |
Output is correct |
17 |
Correct |
8 ms |
14284 KB |
Output is correct |
18 |
Correct |
9 ms |
14392 KB |
Output is correct |
19 |
Correct |
8 ms |
14284 KB |
Output is correct |
20 |
Correct |
8 ms |
14400 KB |
Output is correct |
21 |
Correct |
9 ms |
14340 KB |
Output is correct |
22 |
Correct |
8 ms |
14468 KB |
Output is correct |
23 |
Correct |
9 ms |
14412 KB |
Output is correct |
24 |
Correct |
8 ms |
14284 KB |
Output is correct |
25 |
Correct |
9 ms |
14396 KB |
Output is correct |
26 |
Correct |
8 ms |
14316 KB |
Output is correct |
27 |
Correct |
10 ms |
14412 KB |
Output is correct |
28 |
Correct |
8 ms |
14340 KB |
Output is correct |
29 |
Correct |
7 ms |
14340 KB |
Output is correct |
30 |
Correct |
9 ms |
14412 KB |
Output is correct |
31 |
Correct |
9 ms |
14416 KB |
Output is correct |
32 |
Correct |
9 ms |
14416 KB |
Output is correct |
33 |
Correct |
10 ms |
14412 KB |
Output is correct |
34 |
Correct |
11 ms |
14520 KB |
Output is correct |
35 |
Correct |
10 ms |
14540 KB |
Output is correct |
36 |
Correct |
11 ms |
14588 KB |
Output is correct |
37 |
Correct |
13 ms |
14636 KB |
Output is correct |
38 |
Correct |
11 ms |
14668 KB |
Output is correct |
39 |
Correct |
10 ms |
14668 KB |
Output is correct |
40 |
Correct |
10 ms |
14424 KB |
Output is correct |
41 |
Correct |
9 ms |
14412 KB |
Output is correct |
42 |
Correct |
9 ms |
14412 KB |
Output is correct |
43 |
Correct |
10 ms |
14632 KB |
Output is correct |
44 |
Correct |
10 ms |
14660 KB |
Output is correct |
45 |
Correct |
10 ms |
14540 KB |
Output is correct |
46 |
Correct |
13 ms |
14788 KB |
Output is correct |
47 |
Correct |
12 ms |
14796 KB |
Output is correct |
48 |
Correct |
13 ms |
14788 KB |
Output is correct |
49 |
Correct |
11 ms |
14796 KB |
Output is correct |
50 |
Correct |
10 ms |
14668 KB |
Output is correct |
51 |
Correct |
11 ms |
14668 KB |
Output is correct |
52 |
Correct |
10 ms |
14680 KB |
Output is correct |
53 |
Correct |
11 ms |
14660 KB |
Output is correct |
54 |
Correct |
11 ms |
14796 KB |
Output is correct |
55 |
Correct |
14 ms |
15052 KB |
Output is correct |
56 |
Correct |
32 ms |
17420 KB |
Output is correct |
57 |
Correct |
64 ms |
19492 KB |
Output is correct |
58 |
Correct |
63 ms |
20896 KB |
Output is correct |
59 |
Correct |
83 ms |
23096 KB |
Output is correct |
60 |
Correct |
101 ms |
24896 KB |
Output is correct |
61 |
Correct |
136 ms |
26740 KB |
Output is correct |
62 |
Correct |
127 ms |
28012 KB |
Output is correct |
63 |
Correct |
156 ms |
30668 KB |
Output is correct |
64 |
Correct |
164 ms |
31184 KB |
Output is correct |
65 |
Correct |
84 ms |
21588 KB |
Output is correct |
66 |
Correct |
83 ms |
21572 KB |
Output is correct |
67 |
Correct |
87 ms |
21692 KB |
Output is correct |
68 |
Correct |
153 ms |
28316 KB |
Output is correct |
69 |
Correct |
162 ms |
28860 KB |
Output is correct |
70 |
Correct |
162 ms |
28976 KB |
Output is correct |
71 |
Correct |
202 ms |
43748 KB |
Output is correct |
72 |
Correct |
195 ms |
43912 KB |
Output is correct |
73 |
Correct |
174 ms |
40288 KB |
Output is correct |
74 |
Correct |
179 ms |
40724 KB |
Output is correct |
75 |
Correct |
179 ms |
39092 KB |
Output is correct |
76 |
Correct |
178 ms |
39232 KB |
Output is correct |
77 |
Correct |
195 ms |
38964 KB |
Output is correct |
78 |
Correct |
198 ms |
38180 KB |
Output is correct |
79 |
Correct |
165 ms |
36584 KB |
Output is correct |