#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, a, b) for(int i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define allin(a , x) for(auto a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef pair<ll,ll> pll;
typedef vector<string> vs;
typedef vector<pll> vpl;
typedef vector<int> vi;
std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
struct convex_envelope{
long long a = 0 , b = 0;
multiset<long long> changes;
convex_envelope(){
a = b = 0;
}
convex_envelope(long long a , long long b , multiset<long long> changes) : a(a) , b(b) , changes(changes){}
void merge(convex_envelope &rhs){
a += rhs.a , b += rhs.b;
if(sz(rhs.changes) > sz(changes))
swap(rhs.changes , changes);
for(auto w : rhs.changes){
changes.insert(w);
}
}
void fix(long long ptr){
while(a > ptr){
b += *prev(end(changes));
changes.erase(prev(end(changes)));
a--;
}
}
void add(long long c){
b -= c;
long long last = *prev(end(changes)) , llast = *prev(prev(end(changes)));
changes.erase(changes.find(last)) , changes.erase(changes.find(llast));
changes.insert(last + c) , changes.insert(llast + c);
}
};
const int N = 500005;
int n , m , c[N];
vector<int> g[N];
convex_envelope func[N];
void solve(int x , int y = -1){
int sz = 0;
for(auto w : g[x]){
if(w == y)
continue;
solve(w,x);
func[x].merge(func[w]);
++sz;
}
func[x].fix(1);
if(sz){
if(x != 1){
func[x].add(c[x]);
}
}
}
int32_t main(){
scanf("%d%d" , &n, &m);
for(int i = 2; i <= n + m ; i ++){
int p;
scanf("%d%d" , &p , &c[i]);
g[p].push_back(i);
g[i].push_back(p);
}
for(int i = n + 1; i <= n + m ; i ++){
multiset<long long> changes;
changes.insert(c[i]) , changes.insert(c[i]);
func[i] = convex_envelope(1,-c[i],changes);
}
solve(1);
func[1].fix(0);
printf("%lld\n" , func[1].b);
}
Compilation message
fireworks.cpp: In function 'int32_t main()':
fireworks.cpp:71:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
71 | scanf("%d%d" , &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
fireworks.cpp:74:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
74 | scanf("%d%d" , &p , &c[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
43372 KB |
Output is correct |
2 |
Correct |
29 ms |
43500 KB |
Output is correct |
3 |
Correct |
29 ms |
43372 KB |
Output is correct |
4 |
Correct |
30 ms |
43372 KB |
Output is correct |
5 |
Correct |
30 ms |
43372 KB |
Output is correct |
6 |
Correct |
29 ms |
43372 KB |
Output is correct |
7 |
Correct |
30 ms |
43372 KB |
Output is correct |
8 |
Correct |
29 ms |
43372 KB |
Output is correct |
9 |
Correct |
31 ms |
43372 KB |
Output is correct |
10 |
Correct |
29 ms |
43372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
43372 KB |
Output is correct |
2 |
Correct |
32 ms |
43372 KB |
Output is correct |
3 |
Correct |
30 ms |
43404 KB |
Output is correct |
4 |
Correct |
30 ms |
43372 KB |
Output is correct |
5 |
Correct |
31 ms |
43372 KB |
Output is correct |
6 |
Correct |
30 ms |
43372 KB |
Output is correct |
7 |
Correct |
29 ms |
43372 KB |
Output is correct |
8 |
Correct |
30 ms |
43372 KB |
Output is correct |
9 |
Correct |
30 ms |
43372 KB |
Output is correct |
10 |
Correct |
30 ms |
43372 KB |
Output is correct |
11 |
Correct |
31 ms |
43500 KB |
Output is correct |
12 |
Correct |
31 ms |
43372 KB |
Output is correct |
13 |
Correct |
30 ms |
43372 KB |
Output is correct |
14 |
Correct |
34 ms |
43372 KB |
Output is correct |
15 |
Correct |
30 ms |
43372 KB |
Output is correct |
16 |
Correct |
30 ms |
43372 KB |
Output is correct |
17 |
Correct |
30 ms |
43372 KB |
Output is correct |
18 |
Correct |
30 ms |
43372 KB |
Output is correct |
19 |
Correct |
30 ms |
43372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
43372 KB |
Output is correct |
2 |
Correct |
29 ms |
43500 KB |
Output is correct |
3 |
Correct |
29 ms |
43372 KB |
Output is correct |
4 |
Correct |
30 ms |
43372 KB |
Output is correct |
5 |
Correct |
30 ms |
43372 KB |
Output is correct |
6 |
Correct |
29 ms |
43372 KB |
Output is correct |
7 |
Correct |
30 ms |
43372 KB |
Output is correct |
8 |
Correct |
29 ms |
43372 KB |
Output is correct |
9 |
Correct |
31 ms |
43372 KB |
Output is correct |
10 |
Correct |
29 ms |
43372 KB |
Output is correct |
11 |
Correct |
29 ms |
43372 KB |
Output is correct |
12 |
Correct |
32 ms |
43372 KB |
Output is correct |
13 |
Correct |
30 ms |
43404 KB |
Output is correct |
14 |
Correct |
30 ms |
43372 KB |
Output is correct |
15 |
Correct |
31 ms |
43372 KB |
Output is correct |
16 |
Correct |
30 ms |
43372 KB |
Output is correct |
17 |
Correct |
29 ms |
43372 KB |
Output is correct |
18 |
Correct |
30 ms |
43372 KB |
Output is correct |
19 |
Correct |
30 ms |
43372 KB |
Output is correct |
20 |
Correct |
30 ms |
43372 KB |
Output is correct |
21 |
Correct |
31 ms |
43500 KB |
Output is correct |
22 |
Correct |
31 ms |
43372 KB |
Output is correct |
23 |
Correct |
30 ms |
43372 KB |
Output is correct |
24 |
Correct |
34 ms |
43372 KB |
Output is correct |
25 |
Correct |
30 ms |
43372 KB |
Output is correct |
26 |
Correct |
30 ms |
43372 KB |
Output is correct |
27 |
Correct |
30 ms |
43372 KB |
Output is correct |
28 |
Correct |
30 ms |
43372 KB |
Output is correct |
29 |
Correct |
30 ms |
43372 KB |
Output is correct |
30 |
Correct |
30 ms |
43372 KB |
Output is correct |
31 |
Correct |
30 ms |
43500 KB |
Output is correct |
32 |
Correct |
34 ms |
43628 KB |
Output is correct |
33 |
Correct |
35 ms |
43628 KB |
Output is correct |
34 |
Correct |
32 ms |
43828 KB |
Output is correct |
35 |
Correct |
33 ms |
43884 KB |
Output is correct |
36 |
Correct |
33 ms |
44012 KB |
Output is correct |
37 |
Correct |
34 ms |
44012 KB |
Output is correct |
38 |
Correct |
39 ms |
44012 KB |
Output is correct |
39 |
Correct |
39 ms |
44012 KB |
Output is correct |
40 |
Correct |
35 ms |
44396 KB |
Output is correct |
41 |
Correct |
32 ms |
44396 KB |
Output is correct |
42 |
Correct |
33 ms |
43628 KB |
Output is correct |
43 |
Correct |
34 ms |
44396 KB |
Output is correct |
44 |
Correct |
34 ms |
44268 KB |
Output is correct |
45 |
Correct |
39 ms |
44268 KB |
Output is correct |
46 |
Correct |
35 ms |
44524 KB |
Output is correct |
47 |
Correct |
35 ms |
44524 KB |
Output is correct |
48 |
Correct |
34 ms |
44396 KB |
Output is correct |
49 |
Correct |
35 ms |
44652 KB |
Output is correct |
50 |
Correct |
34 ms |
44288 KB |
Output is correct |
51 |
Correct |
34 ms |
44268 KB |
Output is correct |
52 |
Correct |
34 ms |
44396 KB |
Output is correct |
53 |
Correct |
34 ms |
44396 KB |
Output is correct |
54 |
Correct |
37 ms |
44524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
43372 KB |
Output is correct |
2 |
Correct |
29 ms |
43500 KB |
Output is correct |
3 |
Correct |
29 ms |
43372 KB |
Output is correct |
4 |
Correct |
30 ms |
43372 KB |
Output is correct |
5 |
Correct |
30 ms |
43372 KB |
Output is correct |
6 |
Correct |
29 ms |
43372 KB |
Output is correct |
7 |
Correct |
30 ms |
43372 KB |
Output is correct |
8 |
Correct |
29 ms |
43372 KB |
Output is correct |
9 |
Correct |
31 ms |
43372 KB |
Output is correct |
10 |
Correct |
29 ms |
43372 KB |
Output is correct |
11 |
Correct |
29 ms |
43372 KB |
Output is correct |
12 |
Correct |
32 ms |
43372 KB |
Output is correct |
13 |
Correct |
30 ms |
43404 KB |
Output is correct |
14 |
Correct |
30 ms |
43372 KB |
Output is correct |
15 |
Correct |
31 ms |
43372 KB |
Output is correct |
16 |
Correct |
30 ms |
43372 KB |
Output is correct |
17 |
Correct |
29 ms |
43372 KB |
Output is correct |
18 |
Correct |
30 ms |
43372 KB |
Output is correct |
19 |
Correct |
30 ms |
43372 KB |
Output is correct |
20 |
Correct |
30 ms |
43372 KB |
Output is correct |
21 |
Correct |
31 ms |
43500 KB |
Output is correct |
22 |
Correct |
31 ms |
43372 KB |
Output is correct |
23 |
Correct |
30 ms |
43372 KB |
Output is correct |
24 |
Correct |
34 ms |
43372 KB |
Output is correct |
25 |
Correct |
30 ms |
43372 KB |
Output is correct |
26 |
Correct |
30 ms |
43372 KB |
Output is correct |
27 |
Correct |
30 ms |
43372 KB |
Output is correct |
28 |
Correct |
30 ms |
43372 KB |
Output is correct |
29 |
Correct |
30 ms |
43372 KB |
Output is correct |
30 |
Correct |
30 ms |
43372 KB |
Output is correct |
31 |
Correct |
30 ms |
43500 KB |
Output is correct |
32 |
Correct |
34 ms |
43628 KB |
Output is correct |
33 |
Correct |
35 ms |
43628 KB |
Output is correct |
34 |
Correct |
32 ms |
43828 KB |
Output is correct |
35 |
Correct |
33 ms |
43884 KB |
Output is correct |
36 |
Correct |
33 ms |
44012 KB |
Output is correct |
37 |
Correct |
34 ms |
44012 KB |
Output is correct |
38 |
Correct |
39 ms |
44012 KB |
Output is correct |
39 |
Correct |
39 ms |
44012 KB |
Output is correct |
40 |
Correct |
35 ms |
44396 KB |
Output is correct |
41 |
Correct |
32 ms |
44396 KB |
Output is correct |
42 |
Correct |
33 ms |
43628 KB |
Output is correct |
43 |
Correct |
34 ms |
44396 KB |
Output is correct |
44 |
Correct |
34 ms |
44268 KB |
Output is correct |
45 |
Correct |
39 ms |
44268 KB |
Output is correct |
46 |
Correct |
35 ms |
44524 KB |
Output is correct |
47 |
Correct |
35 ms |
44524 KB |
Output is correct |
48 |
Correct |
34 ms |
44396 KB |
Output is correct |
49 |
Correct |
35 ms |
44652 KB |
Output is correct |
50 |
Correct |
34 ms |
44288 KB |
Output is correct |
51 |
Correct |
34 ms |
44268 KB |
Output is correct |
52 |
Correct |
34 ms |
44396 KB |
Output is correct |
53 |
Correct |
34 ms |
44396 KB |
Output is correct |
54 |
Correct |
37 ms |
44524 KB |
Output is correct |
55 |
Correct |
42 ms |
45164 KB |
Output is correct |
56 |
Correct |
85 ms |
50668 KB |
Output is correct |
57 |
Correct |
143 ms |
56044 KB |
Output is correct |
58 |
Correct |
167 ms |
60012 KB |
Output is correct |
59 |
Correct |
222 ms |
65260 KB |
Output is correct |
60 |
Correct |
325 ms |
70960 KB |
Output is correct |
61 |
Correct |
304 ms |
74604 KB |
Output is correct |
62 |
Correct |
344 ms |
78444 KB |
Output is correct |
63 |
Correct |
433 ms |
86124 KB |
Output is correct |
64 |
Correct |
437 ms |
87108 KB |
Output is correct |
65 |
Correct |
195 ms |
105836 KB |
Output is correct |
66 |
Correct |
199 ms |
105836 KB |
Output is correct |
67 |
Correct |
203 ms |
59116 KB |
Output is correct |
68 |
Correct |
313 ms |
103404 KB |
Output is correct |
69 |
Correct |
386 ms |
99820 KB |
Output is correct |
70 |
Correct |
364 ms |
99948 KB |
Output is correct |
71 |
Correct |
550 ms |
129004 KB |
Output is correct |
72 |
Correct |
474 ms |
128364 KB |
Output is correct |
73 |
Correct |
553 ms |
115820 KB |
Output is correct |
74 |
Correct |
495 ms |
115692 KB |
Output is correct |
75 |
Correct |
492 ms |
115128 KB |
Output is correct |
76 |
Correct |
540 ms |
115516 KB |
Output is correct |
77 |
Correct |
558 ms |
113016 KB |
Output is correct |
78 |
Correct |
567 ms |
110352 KB |
Output is correct |
79 |
Correct |
567 ms |
115164 KB |
Output is correct |