#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pf push_front
#define iter(v, i) for (__typeof__((v).begin()) i = (v).begin(); i != (v).end(); i++)
#define fast_io_without_cstdio ios_base::sync_with_stdio(false), cin.tie(NULL)
#define all(v) (v).begin(), (v).end()
#define rep(i, s, e) for (int i = s; i < e; i++)
// START for segment tree
#define params int p, int L, int R
#define housekeep int mid = (L + R) >> 1, left = p << 1, right = left | 1
// END
#ifdef __linux__
#define gc getchar_unlocked
#define pc putchar_unlocked
#else
#define gc getchar
#define pc putchar
#endif
#if __cplusplus <= 199711L
template<class BidirIt>
BidirIt prev(BidirIt it, typename iterator_traits<BidirIt>::difference_type n = 1) {
advance(it, -n);
return it;
}
template<class ForwardIt>
ForwardIt next(ForwardIt it, typename iterator_traits<ForwardIt>::difference_type n = 1) {
advance(it, n);
return it;
}
#endif
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long double ldouble;
const double EPS = 1e-9;
const double PI = 3.141592653589793238462;
template<typename T>
inline T sq(T a) { return a * a; }
//#ifdef LOCAL_MACHINE
//#endif
const int MAXN = 3e5 + 5;
int p[MAXN], c[MAXN];
ll a[MAXN], b[MAXN];
priority_queue<ll> *pq[MAXN];
int main() {
//freopen("", "r", stdin);
//freopen("", "w", stdout);
int n, m;
scanf("%d %d", &n, &m);
for (int i = 2; i <= n + m; i++) {
scanf("%d %d", &p[i], &c[i]);
}
for (int i = n + m; i > n; i--) {
a[i] = 1;
b[i] = -c[i];
pq[i] = new priority_queue<ll>();
pq[i]->push(c[i]);
pq[i]->push(c[i]);
if (pq[p[i]] == NULL) {
pq[p[i]] = pq[i];
a[p[i]] = a[i];
b[p[i]] = b[i];
} else {
if (pq[p[i]]->size() < pq[i]->size())
swap(pq[p[i]], pq[i]);
while (pq[i]->size()) {
pq[p[i]]->push(pq[i]->top());
pq[i]->pop();
}
a[p[i]] += a[i];
b[p[i]] += b[i];
}
}
for (int i = n; i >= 1; i--) {
while (a[i] > 1) {
a[i]--;
b[i] += pq[i]->top();
pq[i]->pop();
}
ll p2 = pq[i]->top();
pq[i]->pop();
ll p1 = pq[i]->top();
pq[i]->pop();
b[i] -= c[i];
pq[i]->push(p1 + c[i]);
pq[i]->push(p2 + c[i]);
if (i == 1) break;
if (pq[p[i]] == NULL) {
pq[p[i]] = pq[i];
a[p[i]] = a[i];
b[p[i]] = b[i];
} else {
if (pq[p[i]]->size() < pq[i]->size())
swap(pq[p[i]], pq[i]);
while (pq[i]->size()) {
pq[p[i]]->push(pq[i]->top());
pq[i]->pop();
}
a[p[i]] += a[i];
b[p[i]] += b[i];
}
}
printf("%lld\n", pq[1]->top() * a[1] + b[1]);
return 0;
}
Compilation message
fireworks.cpp: In function 'int main()':
fireworks.cpp:67:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
fireworks.cpp:69:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &p[i], &c[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
444 KB |
Output is correct |
3 |
Correct |
2 ms |
552 KB |
Output is correct |
4 |
Correct |
3 ms |
552 KB |
Output is correct |
5 |
Correct |
2 ms |
552 KB |
Output is correct |
6 |
Correct |
3 ms |
552 KB |
Output is correct |
7 |
Correct |
2 ms |
616 KB |
Output is correct |
8 |
Correct |
2 ms |
616 KB |
Output is correct |
9 |
Correct |
2 ms |
688 KB |
Output is correct |
10 |
Correct |
3 ms |
840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
840 KB |
Output is correct |
2 |
Correct |
3 ms |
840 KB |
Output is correct |
3 |
Correct |
3 ms |
840 KB |
Output is correct |
4 |
Correct |
2 ms |
840 KB |
Output is correct |
5 |
Correct |
3 ms |
900 KB |
Output is correct |
6 |
Correct |
2 ms |
900 KB |
Output is correct |
7 |
Correct |
3 ms |
900 KB |
Output is correct |
8 |
Correct |
2 ms |
900 KB |
Output is correct |
9 |
Correct |
2 ms |
900 KB |
Output is correct |
10 |
Correct |
2 ms |
900 KB |
Output is correct |
11 |
Correct |
2 ms |
900 KB |
Output is correct |
12 |
Correct |
2 ms |
900 KB |
Output is correct |
13 |
Correct |
2 ms |
900 KB |
Output is correct |
14 |
Correct |
2 ms |
900 KB |
Output is correct |
15 |
Correct |
2 ms |
912 KB |
Output is correct |
16 |
Correct |
3 ms |
912 KB |
Output is correct |
17 |
Correct |
3 ms |
912 KB |
Output is correct |
18 |
Correct |
3 ms |
912 KB |
Output is correct |
19 |
Correct |
3 ms |
912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
444 KB |
Output is correct |
3 |
Correct |
2 ms |
552 KB |
Output is correct |
4 |
Correct |
3 ms |
552 KB |
Output is correct |
5 |
Correct |
2 ms |
552 KB |
Output is correct |
6 |
Correct |
3 ms |
552 KB |
Output is correct |
7 |
Correct |
2 ms |
616 KB |
Output is correct |
8 |
Correct |
2 ms |
616 KB |
Output is correct |
9 |
Correct |
2 ms |
688 KB |
Output is correct |
10 |
Correct |
3 ms |
840 KB |
Output is correct |
11 |
Correct |
2 ms |
840 KB |
Output is correct |
12 |
Correct |
3 ms |
840 KB |
Output is correct |
13 |
Correct |
3 ms |
840 KB |
Output is correct |
14 |
Correct |
2 ms |
840 KB |
Output is correct |
15 |
Correct |
3 ms |
900 KB |
Output is correct |
16 |
Correct |
2 ms |
900 KB |
Output is correct |
17 |
Correct |
3 ms |
900 KB |
Output is correct |
18 |
Correct |
2 ms |
900 KB |
Output is correct |
19 |
Correct |
2 ms |
900 KB |
Output is correct |
20 |
Correct |
2 ms |
900 KB |
Output is correct |
21 |
Correct |
2 ms |
900 KB |
Output is correct |
22 |
Correct |
2 ms |
900 KB |
Output is correct |
23 |
Correct |
2 ms |
900 KB |
Output is correct |
24 |
Correct |
2 ms |
900 KB |
Output is correct |
25 |
Correct |
2 ms |
912 KB |
Output is correct |
26 |
Correct |
3 ms |
912 KB |
Output is correct |
27 |
Correct |
3 ms |
912 KB |
Output is correct |
28 |
Correct |
3 ms |
912 KB |
Output is correct |
29 |
Correct |
3 ms |
912 KB |
Output is correct |
30 |
Correct |
3 ms |
912 KB |
Output is correct |
31 |
Correct |
3 ms |
984 KB |
Output is correct |
32 |
Correct |
3 ms |
1132 KB |
Output is correct |
33 |
Correct |
5 ms |
1184 KB |
Output is correct |
34 |
Correct |
4 ms |
1232 KB |
Output is correct |
35 |
Correct |
6 ms |
1336 KB |
Output is correct |
36 |
Correct |
4 ms |
1396 KB |
Output is correct |
37 |
Correct |
6 ms |
1612 KB |
Output is correct |
38 |
Correct |
6 ms |
1612 KB |
Output is correct |
39 |
Correct |
7 ms |
1788 KB |
Output is correct |
40 |
Correct |
5 ms |
1788 KB |
Output is correct |
41 |
Correct |
4 ms |
1788 KB |
Output is correct |
42 |
Correct |
4 ms |
1788 KB |
Output is correct |
43 |
Correct |
5 ms |
1976 KB |
Output is correct |
44 |
Correct |
6 ms |
2172 KB |
Output is correct |
45 |
Correct |
5 ms |
2244 KB |
Output is correct |
46 |
Correct |
5 ms |
2572 KB |
Output is correct |
47 |
Correct |
5 ms |
2636 KB |
Output is correct |
48 |
Correct |
5 ms |
2636 KB |
Output is correct |
49 |
Correct |
6 ms |
2792 KB |
Output is correct |
50 |
Correct |
5 ms |
2792 KB |
Output is correct |
51 |
Correct |
5 ms |
2792 KB |
Output is correct |
52 |
Correct |
5 ms |
2824 KB |
Output is correct |
53 |
Correct |
5 ms |
2852 KB |
Output is correct |
54 |
Correct |
5 ms |
2916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
444 KB |
Output is correct |
3 |
Correct |
2 ms |
552 KB |
Output is correct |
4 |
Correct |
3 ms |
552 KB |
Output is correct |
5 |
Correct |
2 ms |
552 KB |
Output is correct |
6 |
Correct |
3 ms |
552 KB |
Output is correct |
7 |
Correct |
2 ms |
616 KB |
Output is correct |
8 |
Correct |
2 ms |
616 KB |
Output is correct |
9 |
Correct |
2 ms |
688 KB |
Output is correct |
10 |
Correct |
3 ms |
840 KB |
Output is correct |
11 |
Correct |
2 ms |
840 KB |
Output is correct |
12 |
Correct |
3 ms |
840 KB |
Output is correct |
13 |
Correct |
3 ms |
840 KB |
Output is correct |
14 |
Correct |
2 ms |
840 KB |
Output is correct |
15 |
Correct |
3 ms |
900 KB |
Output is correct |
16 |
Correct |
2 ms |
900 KB |
Output is correct |
17 |
Correct |
3 ms |
900 KB |
Output is correct |
18 |
Correct |
2 ms |
900 KB |
Output is correct |
19 |
Correct |
2 ms |
900 KB |
Output is correct |
20 |
Correct |
2 ms |
900 KB |
Output is correct |
21 |
Correct |
2 ms |
900 KB |
Output is correct |
22 |
Correct |
2 ms |
900 KB |
Output is correct |
23 |
Correct |
2 ms |
900 KB |
Output is correct |
24 |
Correct |
2 ms |
900 KB |
Output is correct |
25 |
Correct |
2 ms |
912 KB |
Output is correct |
26 |
Correct |
3 ms |
912 KB |
Output is correct |
27 |
Correct |
3 ms |
912 KB |
Output is correct |
28 |
Correct |
3 ms |
912 KB |
Output is correct |
29 |
Correct |
3 ms |
912 KB |
Output is correct |
30 |
Correct |
3 ms |
912 KB |
Output is correct |
31 |
Correct |
3 ms |
984 KB |
Output is correct |
32 |
Correct |
3 ms |
1132 KB |
Output is correct |
33 |
Correct |
5 ms |
1184 KB |
Output is correct |
34 |
Correct |
4 ms |
1232 KB |
Output is correct |
35 |
Correct |
6 ms |
1336 KB |
Output is correct |
36 |
Correct |
4 ms |
1396 KB |
Output is correct |
37 |
Correct |
6 ms |
1612 KB |
Output is correct |
38 |
Correct |
6 ms |
1612 KB |
Output is correct |
39 |
Correct |
7 ms |
1788 KB |
Output is correct |
40 |
Correct |
5 ms |
1788 KB |
Output is correct |
41 |
Correct |
4 ms |
1788 KB |
Output is correct |
42 |
Correct |
4 ms |
1788 KB |
Output is correct |
43 |
Correct |
5 ms |
1976 KB |
Output is correct |
44 |
Correct |
6 ms |
2172 KB |
Output is correct |
45 |
Correct |
5 ms |
2244 KB |
Output is correct |
46 |
Correct |
5 ms |
2572 KB |
Output is correct |
47 |
Correct |
5 ms |
2636 KB |
Output is correct |
48 |
Correct |
5 ms |
2636 KB |
Output is correct |
49 |
Correct |
6 ms |
2792 KB |
Output is correct |
50 |
Correct |
5 ms |
2792 KB |
Output is correct |
51 |
Correct |
5 ms |
2792 KB |
Output is correct |
52 |
Correct |
5 ms |
2824 KB |
Output is correct |
53 |
Correct |
5 ms |
2852 KB |
Output is correct |
54 |
Correct |
5 ms |
2916 KB |
Output is correct |
55 |
Correct |
10 ms |
3616 KB |
Output is correct |
56 |
Correct |
36 ms |
7520 KB |
Output is correct |
57 |
Correct |
57 ms |
11720 KB |
Output is correct |
58 |
Correct |
75 ms |
15424 KB |
Output is correct |
59 |
Correct |
102 ms |
20592 KB |
Output is correct |
60 |
Correct |
136 ms |
26324 KB |
Output is correct |
61 |
Correct |
158 ms |
32008 KB |
Output is correct |
62 |
Correct |
173 ms |
37108 KB |
Output is correct |
63 |
Correct |
241 ms |
44996 KB |
Output is correct |
64 |
Correct |
253 ms |
50360 KB |
Output is correct |
65 |
Correct |
122 ms |
50360 KB |
Output is correct |
66 |
Correct |
104 ms |
50360 KB |
Output is correct |
67 |
Correct |
92 ms |
50616 KB |
Output is correct |
68 |
Correct |
213 ms |
69300 KB |
Output is correct |
69 |
Correct |
202 ms |
74748 KB |
Output is correct |
70 |
Correct |
203 ms |
79328 KB |
Output is correct |
71 |
Correct |
270 ms |
105816 KB |
Output is correct |
72 |
Correct |
301 ms |
110280 KB |
Output is correct |
73 |
Correct |
244 ms |
110952 KB |
Output is correct |
74 |
Correct |
225 ms |
115464 KB |
Output is correct |
75 |
Correct |
251 ms |
117920 KB |
Output is correct |
76 |
Correct |
304 ms |
121748 KB |
Output is correct |
77 |
Correct |
233 ms |
124532 KB |
Output is correct |
78 |
Correct |
233 ms |
128324 KB |
Output is correct |
79 |
Correct |
259 ms |
128804 KB |
Output is correct |