//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define mem(a,v) memset((a), (v), sizeof (a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n) for (int i = 0; i < (n); i++) ni(a[i])
#define nal(a, n) for (int i = 0; i < (n); i++) nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%lld\n", (n))
#define pii pair<int, int>
#define pil pair<int, long long>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vil vector<pil>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MAXN = 1e6 + 5;
const double eps = 1e-9;
priority_queue<ll> pq[MAXN];
int par[MAXN];
ll a[MAXN], b[MAXN], w[MAXN];
void merge(int p, int u) {
if (pq[p].size() < pq[u].size()) swap(pq[p], pq[u]);
while (!pq[u].empty()) {
pq[p].push(pq[u].top());
pq[u].pop();
}
a[p] += a[u]; b[p] += b[u];
}
int main() {
int n, m; scanf("%d %d", &n, &m);
for (int i = 2; i <= n+m; i++)
scanf("%d %lld", par+i, w+i);
for (int i = n+m; i > n; i--) {
pq[i].push(w[i]);
pq[i].push(w[i]);
a[i] = 1;
b[i] = -w[i];
merge(par[i], i);
}
for (int i = n; i > 1; i--) {
while (a[i] > 1) {
a[i]--;
b[i] += pq[i].top();
pq[i].pop();
}
ll x = pq[i].top(); pq[i].pop();
ll y = pq[i].top(); pq[i].pop();
x += w[i], y += w[i];
pq[i].push(x); pq[i].push(y);
b[i] -= w[i];
merge(par[i], i);
}
while (a[1] > 0) {
a[1]--;
b[1] += pq[1].top();
pq[1].pop();
}
prl(b[1]);
return 0;
}
Compilation message
fireworks.cpp: In function 'int main()':
fireworks.cpp:51:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int n, m; scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
fireworks.cpp:53:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %lld", par+i, w+i);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
31608 KB |
Output is correct |
2 |
Correct |
30 ms |
31608 KB |
Output is correct |
3 |
Correct |
30 ms |
31608 KB |
Output is correct |
4 |
Correct |
31 ms |
31608 KB |
Output is correct |
5 |
Correct |
30 ms |
31736 KB |
Output is correct |
6 |
Correct |
30 ms |
31608 KB |
Output is correct |
7 |
Correct |
31 ms |
31764 KB |
Output is correct |
8 |
Correct |
31 ms |
31736 KB |
Output is correct |
9 |
Correct |
31 ms |
31736 KB |
Output is correct |
10 |
Correct |
30 ms |
31712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
31580 KB |
Output is correct |
2 |
Correct |
30 ms |
31608 KB |
Output is correct |
3 |
Correct |
30 ms |
31608 KB |
Output is correct |
4 |
Correct |
30 ms |
31608 KB |
Output is correct |
5 |
Correct |
30 ms |
31644 KB |
Output is correct |
6 |
Correct |
31 ms |
31608 KB |
Output is correct |
7 |
Correct |
30 ms |
31608 KB |
Output is correct |
8 |
Correct |
30 ms |
31736 KB |
Output is correct |
9 |
Correct |
30 ms |
31736 KB |
Output is correct |
10 |
Correct |
31 ms |
31608 KB |
Output is correct |
11 |
Correct |
31 ms |
31736 KB |
Output is correct |
12 |
Correct |
30 ms |
31608 KB |
Output is correct |
13 |
Correct |
31 ms |
31708 KB |
Output is correct |
14 |
Correct |
31 ms |
31612 KB |
Output is correct |
15 |
Correct |
30 ms |
31608 KB |
Output is correct |
16 |
Correct |
30 ms |
31608 KB |
Output is correct |
17 |
Correct |
30 ms |
31736 KB |
Output is correct |
18 |
Correct |
30 ms |
31608 KB |
Output is correct |
19 |
Correct |
31 ms |
31608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
31608 KB |
Output is correct |
2 |
Correct |
30 ms |
31608 KB |
Output is correct |
3 |
Correct |
30 ms |
31608 KB |
Output is correct |
4 |
Correct |
31 ms |
31608 KB |
Output is correct |
5 |
Correct |
30 ms |
31736 KB |
Output is correct |
6 |
Correct |
30 ms |
31608 KB |
Output is correct |
7 |
Correct |
31 ms |
31764 KB |
Output is correct |
8 |
Correct |
31 ms |
31736 KB |
Output is correct |
9 |
Correct |
31 ms |
31736 KB |
Output is correct |
10 |
Correct |
30 ms |
31712 KB |
Output is correct |
11 |
Correct |
30 ms |
31580 KB |
Output is correct |
12 |
Correct |
30 ms |
31608 KB |
Output is correct |
13 |
Correct |
30 ms |
31608 KB |
Output is correct |
14 |
Correct |
30 ms |
31608 KB |
Output is correct |
15 |
Correct |
30 ms |
31644 KB |
Output is correct |
16 |
Correct |
31 ms |
31608 KB |
Output is correct |
17 |
Correct |
30 ms |
31608 KB |
Output is correct |
18 |
Correct |
30 ms |
31736 KB |
Output is correct |
19 |
Correct |
30 ms |
31736 KB |
Output is correct |
20 |
Correct |
31 ms |
31608 KB |
Output is correct |
21 |
Correct |
31 ms |
31736 KB |
Output is correct |
22 |
Correct |
30 ms |
31608 KB |
Output is correct |
23 |
Correct |
31 ms |
31708 KB |
Output is correct |
24 |
Correct |
31 ms |
31612 KB |
Output is correct |
25 |
Correct |
30 ms |
31608 KB |
Output is correct |
26 |
Correct |
30 ms |
31608 KB |
Output is correct |
27 |
Correct |
30 ms |
31736 KB |
Output is correct |
28 |
Correct |
30 ms |
31608 KB |
Output is correct |
29 |
Correct |
31 ms |
31608 KB |
Output is correct |
30 |
Correct |
31 ms |
31736 KB |
Output is correct |
31 |
Correct |
31 ms |
31736 KB |
Output is correct |
32 |
Correct |
31 ms |
31736 KB |
Output is correct |
33 |
Correct |
31 ms |
31752 KB |
Output is correct |
34 |
Correct |
32 ms |
31864 KB |
Output is correct |
35 |
Correct |
33 ms |
31864 KB |
Output is correct |
36 |
Correct |
33 ms |
31992 KB |
Output is correct |
37 |
Correct |
33 ms |
31992 KB |
Output is correct |
38 |
Correct |
34 ms |
31992 KB |
Output is correct |
39 |
Correct |
36 ms |
32072 KB |
Output is correct |
40 |
Correct |
32 ms |
31864 KB |
Output is correct |
41 |
Correct |
39 ms |
31864 KB |
Output is correct |
42 |
Correct |
37 ms |
31916 KB |
Output is correct |
43 |
Correct |
34 ms |
31992 KB |
Output is correct |
44 |
Correct |
34 ms |
31944 KB |
Output is correct |
45 |
Correct |
33 ms |
31992 KB |
Output is correct |
46 |
Correct |
34 ms |
32248 KB |
Output is correct |
47 |
Correct |
34 ms |
32176 KB |
Output is correct |
48 |
Correct |
34 ms |
32120 KB |
Output is correct |
49 |
Correct |
34 ms |
32120 KB |
Output is correct |
50 |
Correct |
41 ms |
32120 KB |
Output is correct |
51 |
Correct |
33 ms |
32124 KB |
Output is correct |
52 |
Correct |
33 ms |
32120 KB |
Output is correct |
53 |
Correct |
33 ms |
32120 KB |
Output is correct |
54 |
Correct |
37 ms |
32120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
31608 KB |
Output is correct |
2 |
Correct |
30 ms |
31608 KB |
Output is correct |
3 |
Correct |
30 ms |
31608 KB |
Output is correct |
4 |
Correct |
31 ms |
31608 KB |
Output is correct |
5 |
Correct |
30 ms |
31736 KB |
Output is correct |
6 |
Correct |
30 ms |
31608 KB |
Output is correct |
7 |
Correct |
31 ms |
31764 KB |
Output is correct |
8 |
Correct |
31 ms |
31736 KB |
Output is correct |
9 |
Correct |
31 ms |
31736 KB |
Output is correct |
10 |
Correct |
30 ms |
31712 KB |
Output is correct |
11 |
Correct |
30 ms |
31580 KB |
Output is correct |
12 |
Correct |
30 ms |
31608 KB |
Output is correct |
13 |
Correct |
30 ms |
31608 KB |
Output is correct |
14 |
Correct |
30 ms |
31608 KB |
Output is correct |
15 |
Correct |
30 ms |
31644 KB |
Output is correct |
16 |
Correct |
31 ms |
31608 KB |
Output is correct |
17 |
Correct |
30 ms |
31608 KB |
Output is correct |
18 |
Correct |
30 ms |
31736 KB |
Output is correct |
19 |
Correct |
30 ms |
31736 KB |
Output is correct |
20 |
Correct |
31 ms |
31608 KB |
Output is correct |
21 |
Correct |
31 ms |
31736 KB |
Output is correct |
22 |
Correct |
30 ms |
31608 KB |
Output is correct |
23 |
Correct |
31 ms |
31708 KB |
Output is correct |
24 |
Correct |
31 ms |
31612 KB |
Output is correct |
25 |
Correct |
30 ms |
31608 KB |
Output is correct |
26 |
Correct |
30 ms |
31608 KB |
Output is correct |
27 |
Correct |
30 ms |
31736 KB |
Output is correct |
28 |
Correct |
30 ms |
31608 KB |
Output is correct |
29 |
Correct |
31 ms |
31608 KB |
Output is correct |
30 |
Correct |
31 ms |
31736 KB |
Output is correct |
31 |
Correct |
31 ms |
31736 KB |
Output is correct |
32 |
Correct |
31 ms |
31736 KB |
Output is correct |
33 |
Correct |
31 ms |
31752 KB |
Output is correct |
34 |
Correct |
32 ms |
31864 KB |
Output is correct |
35 |
Correct |
33 ms |
31864 KB |
Output is correct |
36 |
Correct |
33 ms |
31992 KB |
Output is correct |
37 |
Correct |
33 ms |
31992 KB |
Output is correct |
38 |
Correct |
34 ms |
31992 KB |
Output is correct |
39 |
Correct |
36 ms |
32072 KB |
Output is correct |
40 |
Correct |
32 ms |
31864 KB |
Output is correct |
41 |
Correct |
39 ms |
31864 KB |
Output is correct |
42 |
Correct |
37 ms |
31916 KB |
Output is correct |
43 |
Correct |
34 ms |
31992 KB |
Output is correct |
44 |
Correct |
34 ms |
31944 KB |
Output is correct |
45 |
Correct |
33 ms |
31992 KB |
Output is correct |
46 |
Correct |
34 ms |
32248 KB |
Output is correct |
47 |
Correct |
34 ms |
32176 KB |
Output is correct |
48 |
Correct |
34 ms |
32120 KB |
Output is correct |
49 |
Correct |
34 ms |
32120 KB |
Output is correct |
50 |
Correct |
41 ms |
32120 KB |
Output is correct |
51 |
Correct |
33 ms |
32124 KB |
Output is correct |
52 |
Correct |
33 ms |
32120 KB |
Output is correct |
53 |
Correct |
33 ms |
32120 KB |
Output is correct |
54 |
Correct |
37 ms |
32120 KB |
Output is correct |
55 |
Correct |
39 ms |
32632 KB |
Output is correct |
56 |
Correct |
60 ms |
35688 KB |
Output is correct |
57 |
Correct |
85 ms |
38460 KB |
Output is correct |
58 |
Correct |
100 ms |
40376 KB |
Output is correct |
59 |
Correct |
123 ms |
42964 KB |
Output is correct |
60 |
Correct |
148 ms |
45684 KB |
Output is correct |
61 |
Correct |
181 ms |
48240 KB |
Output is correct |
62 |
Correct |
183 ms |
49720 KB |
Output is correct |
63 |
Correct |
215 ms |
53240 KB |
Output is correct |
64 |
Correct |
229 ms |
53996 KB |
Output is correct |
65 |
Correct |
126 ms |
44664 KB |
Output is correct |
66 |
Correct |
123 ms |
44788 KB |
Output is correct |
67 |
Correct |
123 ms |
44788 KB |
Output is correct |
68 |
Correct |
197 ms |
51564 KB |
Output is correct |
69 |
Correct |
219 ms |
52168 KB |
Output is correct |
70 |
Correct |
219 ms |
52104 KB |
Output is correct |
71 |
Correct |
263 ms |
67112 KB |
Output is correct |
72 |
Correct |
262 ms |
67172 KB |
Output is correct |
73 |
Correct |
229 ms |
63560 KB |
Output is correct |
74 |
Correct |
239 ms |
64004 KB |
Output is correct |
75 |
Correct |
233 ms |
62440 KB |
Output is correct |
76 |
Correct |
234 ms |
62552 KB |
Output is correct |
77 |
Correct |
244 ms |
61972 KB |
Output is correct |
78 |
Correct |
247 ms |
61416 KB |
Output is correct |
79 |
Correct |
233 ms |
59308 KB |
Output is correct |