#include <bits/stdc++.h>
#define mp make_pair
#define X first
#define Y second
#define taskname "FIREWORKS"
using namespace std;
typedef long long ll;
typedef pair <int, int> ii;
const int N = 3e5 + 1;
const ll INF = 5e14;
struct data{
ll a, b;
priority_queue <ll> pq; // slope changing points
void operator += (data &other){
a += other.a;
b += other.b;
if (pq.size() < other.pq.size())
swap(pq, other.pq);
while (!other.pq.empty()){
pq.push(other.pq.top());
other.pq.pop();
}
}
};
int n, m, p[N], c[N];
data d[N];
void readInput(){
cin >> n >> m;
for(int i = 2; i <= n + m; i++)
cin >> p[i] >> c[i];
}
void solve(){
for(int i = 1; i <= n; i++){
d[i].a = 0;
d[i].b = 0;
}
for(int i = n + 1; i <= n + m; i++){
d[i].a = 1;
d[i].b = -c[i];
d[i].pq.push(c[i]);
d[i].pq.push(c[i]);
d[p[i]] += d[i];
}
for(int i = n; i > 1; i--){
while (d[i].a > 1){
d[i].a--;
d[i].b += d[i].pq.top();
d[i].pq.pop();
}
ll p1 = d[i].pq.top();
d[i].pq.pop();
ll p0 = d[i].pq.top();
d[i].pq.pop();
d[i].pq.push(p0 + c[i]);
d[i].pq.push(p1 + c[i]);
d[i].b -= c[i];
d[p[i]] += d[i];
}
while (d[1].a > 0){
d[1].a--;
d[1].b += d[1].pq.top();
d[1].pq.pop();
}
cout << d[1].b;
}
int main(){
readInput();
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14464 KB |
Output is correct |
2 |
Correct |
9 ms |
14464 KB |
Output is correct |
3 |
Correct |
10 ms |
14464 KB |
Output is correct |
4 |
Correct |
10 ms |
14464 KB |
Output is correct |
5 |
Correct |
10 ms |
14464 KB |
Output is correct |
6 |
Correct |
10 ms |
14464 KB |
Output is correct |
7 |
Correct |
10 ms |
14464 KB |
Output is correct |
8 |
Correct |
10 ms |
14464 KB |
Output is correct |
9 |
Correct |
10 ms |
14464 KB |
Output is correct |
10 |
Correct |
10 ms |
14464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14464 KB |
Output is correct |
2 |
Correct |
10 ms |
14388 KB |
Output is correct |
3 |
Correct |
10 ms |
14464 KB |
Output is correct |
4 |
Correct |
9 ms |
14464 KB |
Output is correct |
5 |
Correct |
10 ms |
14464 KB |
Output is correct |
6 |
Correct |
10 ms |
14464 KB |
Output is correct |
7 |
Correct |
9 ms |
14464 KB |
Output is correct |
8 |
Correct |
10 ms |
14464 KB |
Output is correct |
9 |
Correct |
10 ms |
14464 KB |
Output is correct |
10 |
Correct |
10 ms |
14464 KB |
Output is correct |
11 |
Correct |
10 ms |
14464 KB |
Output is correct |
12 |
Correct |
10 ms |
14464 KB |
Output is correct |
13 |
Correct |
10 ms |
14464 KB |
Output is correct |
14 |
Correct |
10 ms |
14464 KB |
Output is correct |
15 |
Correct |
10 ms |
14464 KB |
Output is correct |
16 |
Correct |
10 ms |
14464 KB |
Output is correct |
17 |
Correct |
10 ms |
14464 KB |
Output is correct |
18 |
Correct |
10 ms |
14464 KB |
Output is correct |
19 |
Correct |
10 ms |
14464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14464 KB |
Output is correct |
2 |
Correct |
9 ms |
14464 KB |
Output is correct |
3 |
Correct |
10 ms |
14464 KB |
Output is correct |
4 |
Correct |
10 ms |
14464 KB |
Output is correct |
5 |
Correct |
10 ms |
14464 KB |
Output is correct |
6 |
Correct |
10 ms |
14464 KB |
Output is correct |
7 |
Correct |
10 ms |
14464 KB |
Output is correct |
8 |
Correct |
10 ms |
14464 KB |
Output is correct |
9 |
Correct |
10 ms |
14464 KB |
Output is correct |
10 |
Correct |
10 ms |
14464 KB |
Output is correct |
11 |
Correct |
9 ms |
14464 KB |
Output is correct |
12 |
Correct |
10 ms |
14388 KB |
Output is correct |
13 |
Correct |
10 ms |
14464 KB |
Output is correct |
14 |
Correct |
9 ms |
14464 KB |
Output is correct |
15 |
Correct |
10 ms |
14464 KB |
Output is correct |
16 |
Correct |
10 ms |
14464 KB |
Output is correct |
17 |
Correct |
9 ms |
14464 KB |
Output is correct |
18 |
Correct |
10 ms |
14464 KB |
Output is correct |
19 |
Correct |
10 ms |
14464 KB |
Output is correct |
20 |
Correct |
10 ms |
14464 KB |
Output is correct |
21 |
Correct |
10 ms |
14464 KB |
Output is correct |
22 |
Correct |
10 ms |
14464 KB |
Output is correct |
23 |
Correct |
10 ms |
14464 KB |
Output is correct |
24 |
Correct |
10 ms |
14464 KB |
Output is correct |
25 |
Correct |
10 ms |
14464 KB |
Output is correct |
26 |
Correct |
10 ms |
14464 KB |
Output is correct |
27 |
Correct |
10 ms |
14464 KB |
Output is correct |
28 |
Correct |
10 ms |
14464 KB |
Output is correct |
29 |
Correct |
10 ms |
14464 KB |
Output is correct |
30 |
Correct |
10 ms |
14464 KB |
Output is correct |
31 |
Correct |
13 ms |
14464 KB |
Output is correct |
32 |
Correct |
12 ms |
14464 KB |
Output is correct |
33 |
Correct |
13 ms |
14592 KB |
Output is correct |
34 |
Correct |
14 ms |
14592 KB |
Output is correct |
35 |
Correct |
15 ms |
14592 KB |
Output is correct |
36 |
Correct |
16 ms |
14592 KB |
Output is correct |
37 |
Correct |
17 ms |
14720 KB |
Output is correct |
38 |
Correct |
17 ms |
14720 KB |
Output is correct |
39 |
Correct |
18 ms |
14720 KB |
Output is correct |
40 |
Correct |
16 ms |
14464 KB |
Output is correct |
41 |
Correct |
16 ms |
14464 KB |
Output is correct |
42 |
Correct |
17 ms |
14464 KB |
Output is correct |
43 |
Correct |
18 ms |
14592 KB |
Output is correct |
44 |
Correct |
18 ms |
14712 KB |
Output is correct |
45 |
Correct |
17 ms |
14720 KB |
Output is correct |
46 |
Correct |
18 ms |
14848 KB |
Output is correct |
47 |
Correct |
17 ms |
14848 KB |
Output is correct |
48 |
Correct |
16 ms |
14848 KB |
Output is correct |
49 |
Correct |
17 ms |
14848 KB |
Output is correct |
50 |
Correct |
16 ms |
14720 KB |
Output is correct |
51 |
Correct |
16 ms |
14720 KB |
Output is correct |
52 |
Correct |
17 ms |
14720 KB |
Output is correct |
53 |
Correct |
17 ms |
14720 KB |
Output is correct |
54 |
Correct |
17 ms |
14848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14464 KB |
Output is correct |
2 |
Correct |
9 ms |
14464 KB |
Output is correct |
3 |
Correct |
10 ms |
14464 KB |
Output is correct |
4 |
Correct |
10 ms |
14464 KB |
Output is correct |
5 |
Correct |
10 ms |
14464 KB |
Output is correct |
6 |
Correct |
10 ms |
14464 KB |
Output is correct |
7 |
Correct |
10 ms |
14464 KB |
Output is correct |
8 |
Correct |
10 ms |
14464 KB |
Output is correct |
9 |
Correct |
10 ms |
14464 KB |
Output is correct |
10 |
Correct |
10 ms |
14464 KB |
Output is correct |
11 |
Correct |
9 ms |
14464 KB |
Output is correct |
12 |
Correct |
10 ms |
14388 KB |
Output is correct |
13 |
Correct |
10 ms |
14464 KB |
Output is correct |
14 |
Correct |
9 ms |
14464 KB |
Output is correct |
15 |
Correct |
10 ms |
14464 KB |
Output is correct |
16 |
Correct |
10 ms |
14464 KB |
Output is correct |
17 |
Correct |
9 ms |
14464 KB |
Output is correct |
18 |
Correct |
10 ms |
14464 KB |
Output is correct |
19 |
Correct |
10 ms |
14464 KB |
Output is correct |
20 |
Correct |
10 ms |
14464 KB |
Output is correct |
21 |
Correct |
10 ms |
14464 KB |
Output is correct |
22 |
Correct |
10 ms |
14464 KB |
Output is correct |
23 |
Correct |
10 ms |
14464 KB |
Output is correct |
24 |
Correct |
10 ms |
14464 KB |
Output is correct |
25 |
Correct |
10 ms |
14464 KB |
Output is correct |
26 |
Correct |
10 ms |
14464 KB |
Output is correct |
27 |
Correct |
10 ms |
14464 KB |
Output is correct |
28 |
Correct |
10 ms |
14464 KB |
Output is correct |
29 |
Correct |
10 ms |
14464 KB |
Output is correct |
30 |
Correct |
10 ms |
14464 KB |
Output is correct |
31 |
Correct |
13 ms |
14464 KB |
Output is correct |
32 |
Correct |
12 ms |
14464 KB |
Output is correct |
33 |
Correct |
13 ms |
14592 KB |
Output is correct |
34 |
Correct |
14 ms |
14592 KB |
Output is correct |
35 |
Correct |
15 ms |
14592 KB |
Output is correct |
36 |
Correct |
16 ms |
14592 KB |
Output is correct |
37 |
Correct |
17 ms |
14720 KB |
Output is correct |
38 |
Correct |
17 ms |
14720 KB |
Output is correct |
39 |
Correct |
18 ms |
14720 KB |
Output is correct |
40 |
Correct |
16 ms |
14464 KB |
Output is correct |
41 |
Correct |
16 ms |
14464 KB |
Output is correct |
42 |
Correct |
17 ms |
14464 KB |
Output is correct |
43 |
Correct |
18 ms |
14592 KB |
Output is correct |
44 |
Correct |
18 ms |
14712 KB |
Output is correct |
45 |
Correct |
17 ms |
14720 KB |
Output is correct |
46 |
Correct |
18 ms |
14848 KB |
Output is correct |
47 |
Correct |
17 ms |
14848 KB |
Output is correct |
48 |
Correct |
16 ms |
14848 KB |
Output is correct |
49 |
Correct |
17 ms |
14848 KB |
Output is correct |
50 |
Correct |
16 ms |
14720 KB |
Output is correct |
51 |
Correct |
16 ms |
14720 KB |
Output is correct |
52 |
Correct |
17 ms |
14720 KB |
Output is correct |
53 |
Correct |
17 ms |
14720 KB |
Output is correct |
54 |
Correct |
17 ms |
14848 KB |
Output is correct |
55 |
Correct |
31 ms |
15232 KB |
Output is correct |
56 |
Correct |
89 ms |
17508 KB |
Output is correct |
57 |
Correct |
146 ms |
19448 KB |
Output is correct |
58 |
Correct |
189 ms |
20980 KB |
Output is correct |
59 |
Correct |
249 ms |
23036 KB |
Output is correct |
60 |
Correct |
309 ms |
25080 KB |
Output is correct |
61 |
Correct |
362 ms |
27120 KB |
Output is correct |
62 |
Correct |
394 ms |
28320 KB |
Output is correct |
63 |
Correct |
483 ms |
30820 KB |
Output is correct |
64 |
Correct |
504 ms |
31344 KB |
Output is correct |
65 |
Correct |
455 ms |
21624 KB |
Output is correct |
66 |
Correct |
449 ms |
21752 KB |
Output is correct |
67 |
Correct |
447 ms |
21880 KB |
Output is correct |
68 |
Correct |
503 ms |
28400 KB |
Output is correct |
69 |
Correct |
509 ms |
29164 KB |
Output is correct |
70 |
Correct |
507 ms |
29036 KB |
Output is correct |
71 |
Correct |
531 ms |
44168 KB |
Output is correct |
72 |
Correct |
540 ms |
44132 KB |
Output is correct |
73 |
Correct |
487 ms |
40420 KB |
Output is correct |
74 |
Correct |
495 ms |
41184 KB |
Output is correct |
75 |
Correct |
479 ms |
39396 KB |
Output is correct |
76 |
Correct |
478 ms |
39464 KB |
Output is correct |
77 |
Correct |
506 ms |
39140 KB |
Output is correct |
78 |
Correct |
497 ms |
38376 KB |
Output is correct |
79 |
Correct |
443 ms |
36832 KB |
Output is correct |