#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 int long long int
#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)
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();
}
// print(x, cnt[x]);
// for(auto i : s[x]) cerr << i << " ";
// kout();
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; }
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
1 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 |
320 KB |
Output is correct |
14 |
Correct |
1 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
324 KB |
Output is correct |
15 |
Correct |
1 ms |
212 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 |
320 KB |
Output is correct |
20 |
Correct |
1 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 |
320 KB |
Output is correct |
24 |
Correct |
1 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 |
468 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
2 ms |
588 KB |
Output is correct |
34 |
Correct |
3 ms |
724 KB |
Output is correct |
35 |
Correct |
3 ms |
812 KB |
Output is correct |
36 |
Correct |
3 ms |
852 KB |
Output is correct |
37 |
Correct |
3 ms |
980 KB |
Output is correct |
38 |
Correct |
4 ms |
980 KB |
Output is correct |
39 |
Correct |
5 ms |
980 KB |
Output is correct |
40 |
Correct |
2 ms |
1620 KB |
Output is correct |
41 |
Correct |
3 ms |
1620 KB |
Output is correct |
42 |
Correct |
3 ms |
852 KB |
Output is correct |
43 |
Correct |
5 ms |
1356 KB |
Output is correct |
44 |
Correct |
4 ms |
1228 KB |
Output is correct |
45 |
Correct |
3 ms |
1236 KB |
Output is correct |
46 |
Correct |
5 ms |
1108 KB |
Output is correct |
47 |
Correct |
5 ms |
1108 KB |
Output is correct |
48 |
Correct |
4 ms |
980 KB |
Output is correct |
49 |
Correct |
6 ms |
1168 KB |
Output is correct |
50 |
Correct |
5 ms |
980 KB |
Output is correct |
51 |
Correct |
4 ms |
980 KB |
Output is correct |
52 |
Correct |
6 ms |
968 KB |
Output is correct |
53 |
Correct |
7 ms |
1048 KB |
Output is correct |
54 |
Correct |
5 ms |
1224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
324 KB |
Output is correct |
15 |
Correct |
1 ms |
212 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 |
320 KB |
Output is correct |
20 |
Correct |
1 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 |
320 KB |
Output is correct |
24 |
Correct |
1 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 |
468 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
2 ms |
588 KB |
Output is correct |
34 |
Correct |
3 ms |
724 KB |
Output is correct |
35 |
Correct |
3 ms |
812 KB |
Output is correct |
36 |
Correct |
3 ms |
852 KB |
Output is correct |
37 |
Correct |
3 ms |
980 KB |
Output is correct |
38 |
Correct |
4 ms |
980 KB |
Output is correct |
39 |
Correct |
5 ms |
980 KB |
Output is correct |
40 |
Correct |
2 ms |
1620 KB |
Output is correct |
41 |
Correct |
3 ms |
1620 KB |
Output is correct |
42 |
Correct |
3 ms |
852 KB |
Output is correct |
43 |
Correct |
5 ms |
1356 KB |
Output is correct |
44 |
Correct |
4 ms |
1228 KB |
Output is correct |
45 |
Correct |
3 ms |
1236 KB |
Output is correct |
46 |
Correct |
5 ms |
1108 KB |
Output is correct |
47 |
Correct |
5 ms |
1108 KB |
Output is correct |
48 |
Correct |
4 ms |
980 KB |
Output is correct |
49 |
Correct |
6 ms |
1168 KB |
Output is correct |
50 |
Correct |
5 ms |
980 KB |
Output is correct |
51 |
Correct |
4 ms |
980 KB |
Output is correct |
52 |
Correct |
6 ms |
968 KB |
Output is correct |
53 |
Correct |
7 ms |
1048 KB |
Output is correct |
54 |
Correct |
5 ms |
1224 KB |
Output is correct |
55 |
Correct |
10 ms |
2148 KB |
Output is correct |
56 |
Correct |
42 ms |
7056 KB |
Output is correct |
57 |
Correct |
71 ms |
11976 KB |
Output is correct |
58 |
Correct |
104 ms |
15532 KB |
Output is correct |
59 |
Correct |
136 ms |
20716 KB |
Output is correct |
60 |
Correct |
218 ms |
25368 KB |
Output is correct |
61 |
Correct |
183 ms |
29112 KB |
Output is correct |
62 |
Correct |
232 ms |
32572 KB |
Output is correct |
63 |
Correct |
313 ms |
38476 KB |
Output is correct |
64 |
Correct |
268 ms |
40052 KB |
Output is correct |
65 |
Correct |
130 ms |
80344 KB |
Output is correct |
66 |
Correct |
146 ms |
80456 KB |
Output is correct |
67 |
Correct |
134 ms |
38232 KB |
Output is correct |
68 |
Correct |
250 ms |
63732 KB |
Output is correct |
69 |
Correct |
259 ms |
60884 KB |
Output is correct |
70 |
Correct |
275 ms |
60884 KB |
Output is correct |
71 |
Correct |
332 ms |
49108 KB |
Output is correct |
72 |
Correct |
360 ms |
49312 KB |
Output is correct |
73 |
Correct |
343 ms |
47892 KB |
Output is correct |
74 |
Correct |
393 ms |
47952 KB |
Output is correct |
75 |
Correct |
330 ms |
46984 KB |
Output is correct |
76 |
Correct |
379 ms |
47192 KB |
Output is correct |
77 |
Correct |
366 ms |
46308 KB |
Output is correct |
78 |
Correct |
410 ms |
47020 KB |
Output is correct |
79 |
Correct |
402 ms |
60200 KB |
Output is correct |