#define wiwihorz
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse")
#pragma loop-opt(on)
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define rrep(i, a, b) for(int i = b; i >= a; i --)
#define all(x) x.begin(), x.end()
#define ceil(a, b) ((a + b - 1) / (b))
#define lld long double
#define pii pair<int, int>
#define random mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count())
#define INF 1000000000000000000
#define MOD 1000000007
#define eps (1e-9)
#define int long long int
using namespace std;
#ifdef wiwihorz
#define print(a...)cerr<<"Line "<<__LINE__<<":",kout("["+string(#a)+"] = ", a)
void vprint(auto L,auto R){while(L<R)cerr<<*L<<" \n"[next(L) == R], ++L; }
void kout() { cerr << endl; }
template<class T1,class ... T2>void kout(T1 a,T2 ... e){cerr<<a<<" ",kout(e...);}
#else
#define print(...) 0
#define vprint(...) 0
#endif
#define x first
#define y second
namespace solver {
int n, m;
vector<vector<pii>> mp;
vector<multiset<int>> s;
vector<int> cnt;
void init_(int _n, int _m) {
n = _n, m = _m;
mp.assign(n + m + 1, vector<pii>());
s.assign(n + m + 1, multiset<int>());
cnt.assign(n + m + 1, 0);
}
void dfs(int x, int cost) {
for(auto i : mp[x]) {
dfs(i.x, i.y);
if(s[i.x].size() > s[x].size())
s[x].swap(s[i.x]);
cnt[x] += cnt[i.x];
for(auto j : s[i.x]) {
s[x].insert(j);
}
s[i.x].clear();
}
if(cost != -1) {
if(x > n) {
cnt[x] = 1;
s[x].insert(0);
s[x].insert(0);
}
while(cnt[x] > 1) {
cnt[x] --;
s[x].erase(prev(s[x].end()));
}
int L = *next(s[x].rbegin());
int R = *s[x].rbegin();
s[x].erase(prev(s[x].end()));
s[x].erase(prev(s[x].end()));
s[x].insert(L + cost);
s[x].insert(R + cost);
}
}
}
using namespace solver;
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
int n, m;
cin >> n >> m;
init_(n, m);
int sum = 0;
rep(i, 2, n + m) {
int p, c;
cin >> p >> c;
sum += c;
mp[p].push_back({i, c});
}
dfs(1, -1);
int cur = cnt[1] - s[1].size(), pos = 0;
auto it = s[1].begin();
while(cur < 0) {
sum += cur * (*it - pos);
cur ++, pos = *it;
it = next(it);
}
cout << sum << "\n";
return 0;
}
Compilation message
fireworks.cpp:5: warning: ignoring '#pragma loop ' [-Wunknown-pragmas]
5 | #pragma loop-opt(on)
|
fireworks.cpp:20:13: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
20 | void vprint(auto L,auto R){while(L<R)cerr<<*L<<" \n"[next(L) == R], ++L; }
| ^~~~
fireworks.cpp:20:20: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
20 | void vprint(auto L,auto R){while(L<R)cerr<<*L<<" \n"[next(L) == R], ++L; }
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
2 ms |
596 KB |
Output is correct |
34 |
Correct |
3 ms |
596 KB |
Output is correct |
35 |
Correct |
4 ms |
724 KB |
Output is correct |
36 |
Correct |
4 ms |
724 KB |
Output is correct |
37 |
Correct |
3 ms |
852 KB |
Output is correct |
38 |
Correct |
3 ms |
852 KB |
Output is correct |
39 |
Correct |
3 ms |
852 KB |
Output is correct |
40 |
Correct |
2 ms |
1492 KB |
Output is correct |
41 |
Correct |
2 ms |
1492 KB |
Output is correct |
42 |
Correct |
2 ms |
852 KB |
Output is correct |
43 |
Correct |
3 ms |
1236 KB |
Output is correct |
44 |
Correct |
3 ms |
1108 KB |
Output is correct |
45 |
Correct |
3 ms |
1108 KB |
Output is correct |
46 |
Correct |
4 ms |
1048 KB |
Output is correct |
47 |
Correct |
6 ms |
980 KB |
Output is correct |
48 |
Correct |
4 ms |
980 KB |
Output is correct |
49 |
Correct |
4 ms |
980 KB |
Output is correct |
50 |
Correct |
4 ms |
980 KB |
Output is correct |
51 |
Correct |
5 ms |
980 KB |
Output is correct |
52 |
Correct |
4 ms |
980 KB |
Output is correct |
53 |
Correct |
4 ms |
980 KB |
Output is correct |
54 |
Correct |
4 ms |
1236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
2 ms |
596 KB |
Output is correct |
34 |
Correct |
3 ms |
596 KB |
Output is correct |
35 |
Correct |
4 ms |
724 KB |
Output is correct |
36 |
Correct |
4 ms |
724 KB |
Output is correct |
37 |
Correct |
3 ms |
852 KB |
Output is correct |
38 |
Correct |
3 ms |
852 KB |
Output is correct |
39 |
Correct |
3 ms |
852 KB |
Output is correct |
40 |
Correct |
2 ms |
1492 KB |
Output is correct |
41 |
Correct |
2 ms |
1492 KB |
Output is correct |
42 |
Correct |
2 ms |
852 KB |
Output is correct |
43 |
Correct |
3 ms |
1236 KB |
Output is correct |
44 |
Correct |
3 ms |
1108 KB |
Output is correct |
45 |
Correct |
3 ms |
1108 KB |
Output is correct |
46 |
Correct |
4 ms |
1048 KB |
Output is correct |
47 |
Correct |
6 ms |
980 KB |
Output is correct |
48 |
Correct |
4 ms |
980 KB |
Output is correct |
49 |
Correct |
4 ms |
980 KB |
Output is correct |
50 |
Correct |
4 ms |
980 KB |
Output is correct |
51 |
Correct |
5 ms |
980 KB |
Output is correct |
52 |
Correct |
4 ms |
980 KB |
Output is correct |
53 |
Correct |
4 ms |
980 KB |
Output is correct |
54 |
Correct |
4 ms |
1236 KB |
Output is correct |
55 |
Correct |
9 ms |
2004 KB |
Output is correct |
56 |
Correct |
42 ms |
6352 KB |
Output is correct |
57 |
Correct |
77 ms |
10700 KB |
Output is correct |
58 |
Correct |
105 ms |
13876 KB |
Output is correct |
59 |
Correct |
165 ms |
18548 KB |
Output is correct |
60 |
Correct |
157 ms |
22520 KB |
Output is correct |
61 |
Correct |
194 ms |
25876 KB |
Output is correct |
62 |
Correct |
229 ms |
29116 KB |
Output is correct |
63 |
Correct |
268 ms |
34288 KB |
Output is correct |
64 |
Correct |
295 ms |
35492 KB |
Output is correct |
65 |
Correct |
127 ms |
75520 KB |
Output is correct |
66 |
Correct |
161 ms |
75420 KB |
Output is correct |
67 |
Correct |
148 ms |
33268 KB |
Output is correct |
68 |
Correct |
203 ms |
58936 KB |
Output is correct |
69 |
Correct |
280 ms |
56280 KB |
Output is correct |
70 |
Correct |
248 ms |
56180 KB |
Output is correct |
71 |
Correct |
343 ms |
44836 KB |
Output is correct |
72 |
Correct |
322 ms |
44824 KB |
Output is correct |
73 |
Correct |
350 ms |
43864 KB |
Output is correct |
74 |
Correct |
348 ms |
43932 KB |
Output is correct |
75 |
Correct |
348 ms |
43124 KB |
Output is correct |
76 |
Correct |
354 ms |
43136 KB |
Output is correct |
77 |
Correct |
359 ms |
42172 KB |
Output is correct |
78 |
Correct |
409 ms |
43020 KB |
Output is correct |
79 |
Correct |
410 ms |
56564 KB |
Output is correct |