#include<bits/stdc++.h>
using namespace std;
#define sz(v) (int)v.size()
#define all(v) begin(v), end(v)
#define compact(v) v.erase(unique(all(v)), end(v))
#define dbg(v) "[" #v " = " << (v) << "]"
#define file(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define rep(i, l, r) for(int i = (l); i < (r); ++i)
using ll = long long;
using vi = vector<int>;
using vl = vector<long long>;
using ld = long double;
template<typename T>
bool minimize(T& a, const T& b){
if(a > b){
return a = b, true;
} return false;
}
template<typename T>
bool maximize(T& a, const T& b){
if(a < b){
return a = b, true;
} return false;
}
template<int dimension, typename T>
struct tensor : public vector<tensor<dimension - 1, T>> {
static_assert(dimension > 0, "Dimension must be positive !\n");
template<typename... Args>
tensor(int n = 0, Args... args) : vector<tensor<dimension - 1, T>> (n, tensor<dimension - 1, T>(args...)) {}
};
template<typename T>
struct tensor<1, T> : public vector<T> {
tensor(int n = 0, T val = T()) : vector<T>(n, val) {}
};
const int MAX = 6e5 + 5;
int n, m;
ll slope[MAX], intercept[MAX];
vector<pair<int, int>> adj[MAX];
priority_queue<ll> pq[MAX];
void convert(int i, ll x){
while(slope[i] > 1){
intercept[i] += pq[i].top();
--slope[i];
pq[i].pop();
}
ll p = pq[i].top(); pq[i].pop();
ll q = pq[i].top(); pq[i].pop();
pq[i].push(p + x);
pq[i].push(q + x);
intercept[i] -= x;
}
void dfs(int u){
for(auto [v, w] : adj[u]){
if(n + 1 <= v && v <= n + m){
//leaf
slope[v] = 1;
intercept[v] = -w;
pq[v].push(w);
pq[v].push(w);
} else{
//non-leaf
dfs(v);
convert(v, w);
}
slope[u] += slope[v];
intercept[u] += intercept[v];
if(sz(pq[u]) < sz(pq[v])){
swap(pq[u], pq[v]);
}
while(!pq[v].empty()){
pq[u].push(pq[v].top());
pq[v].pop();
}
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
if(fopen("task.inp", "r")){
freopen("task.inp", "r", stdin);
freopen("task.out", "w", stdout);
}
cin >> n >> m;
for(int i = 2; i <= n + m; ++i){
int p, w;
cin >> p >> w;
adj[p].emplace_back(i, w);
}
dfs(1);
while(slope[1] > 0){
intercept[1] += pq[1].top();
--slope[1];
pq[1].pop();
}
cout << intercept[1] << '\n';
return 0;
}
Compilation message
fireworks.cpp: In function 'void dfs(int)':
fireworks.cpp:66:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
66 | for(auto [v, w] : adj[u]){
| ^
fireworks.cpp: In function 'int main()':
fireworks.cpp:97:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | freopen("task.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fireworks.cpp:98:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
98 | freopen("task.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
33116 KB |
Output is correct |
2 |
Correct |
17 ms |
33344 KB |
Output is correct |
3 |
Correct |
14 ms |
33368 KB |
Output is correct |
4 |
Correct |
14 ms |
33164 KB |
Output is correct |
5 |
Correct |
14 ms |
33116 KB |
Output is correct |
6 |
Correct |
16 ms |
33116 KB |
Output is correct |
7 |
Correct |
15 ms |
33116 KB |
Output is correct |
8 |
Correct |
15 ms |
33368 KB |
Output is correct |
9 |
Correct |
15 ms |
33116 KB |
Output is correct |
10 |
Correct |
15 ms |
33108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
33372 KB |
Output is correct |
2 |
Correct |
15 ms |
33116 KB |
Output is correct |
3 |
Correct |
14 ms |
33116 KB |
Output is correct |
4 |
Correct |
16 ms |
33116 KB |
Output is correct |
5 |
Correct |
16 ms |
33112 KB |
Output is correct |
6 |
Correct |
15 ms |
33288 KB |
Output is correct |
7 |
Correct |
16 ms |
33116 KB |
Output is correct |
8 |
Correct |
15 ms |
33116 KB |
Output is correct |
9 |
Correct |
15 ms |
33116 KB |
Output is correct |
10 |
Correct |
16 ms |
33320 KB |
Output is correct |
11 |
Correct |
15 ms |
33372 KB |
Output is correct |
12 |
Correct |
18 ms |
33368 KB |
Output is correct |
13 |
Correct |
14 ms |
33372 KB |
Output is correct |
14 |
Correct |
13 ms |
33388 KB |
Output is correct |
15 |
Correct |
16 ms |
33368 KB |
Output is correct |
16 |
Correct |
16 ms |
33368 KB |
Output is correct |
17 |
Correct |
16 ms |
33376 KB |
Output is correct |
18 |
Correct |
16 ms |
33116 KB |
Output is correct |
19 |
Correct |
15 ms |
33352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
33116 KB |
Output is correct |
2 |
Correct |
17 ms |
33344 KB |
Output is correct |
3 |
Correct |
14 ms |
33368 KB |
Output is correct |
4 |
Correct |
14 ms |
33164 KB |
Output is correct |
5 |
Correct |
14 ms |
33116 KB |
Output is correct |
6 |
Correct |
16 ms |
33116 KB |
Output is correct |
7 |
Correct |
15 ms |
33116 KB |
Output is correct |
8 |
Correct |
15 ms |
33368 KB |
Output is correct |
9 |
Correct |
15 ms |
33116 KB |
Output is correct |
10 |
Correct |
15 ms |
33108 KB |
Output is correct |
11 |
Correct |
16 ms |
33372 KB |
Output is correct |
12 |
Correct |
15 ms |
33116 KB |
Output is correct |
13 |
Correct |
14 ms |
33116 KB |
Output is correct |
14 |
Correct |
16 ms |
33116 KB |
Output is correct |
15 |
Correct |
16 ms |
33112 KB |
Output is correct |
16 |
Correct |
15 ms |
33288 KB |
Output is correct |
17 |
Correct |
16 ms |
33116 KB |
Output is correct |
18 |
Correct |
15 ms |
33116 KB |
Output is correct |
19 |
Correct |
15 ms |
33116 KB |
Output is correct |
20 |
Correct |
16 ms |
33320 KB |
Output is correct |
21 |
Correct |
15 ms |
33372 KB |
Output is correct |
22 |
Correct |
18 ms |
33368 KB |
Output is correct |
23 |
Correct |
14 ms |
33372 KB |
Output is correct |
24 |
Correct |
13 ms |
33388 KB |
Output is correct |
25 |
Correct |
16 ms |
33368 KB |
Output is correct |
26 |
Correct |
16 ms |
33368 KB |
Output is correct |
27 |
Correct |
16 ms |
33376 KB |
Output is correct |
28 |
Correct |
16 ms |
33116 KB |
Output is correct |
29 |
Correct |
15 ms |
33352 KB |
Output is correct |
30 |
Correct |
15 ms |
33368 KB |
Output is correct |
31 |
Correct |
14 ms |
33372 KB |
Output is correct |
32 |
Correct |
14 ms |
33372 KB |
Output is correct |
33 |
Correct |
15 ms |
33368 KB |
Output is correct |
34 |
Correct |
14 ms |
33372 KB |
Output is correct |
35 |
Correct |
15 ms |
33624 KB |
Output is correct |
36 |
Correct |
15 ms |
33624 KB |
Output is correct |
37 |
Correct |
15 ms |
33628 KB |
Output is correct |
38 |
Correct |
15 ms |
33540 KB |
Output is correct |
39 |
Correct |
16 ms |
33628 KB |
Output is correct |
40 |
Correct |
15 ms |
34136 KB |
Output is correct |
41 |
Correct |
16 ms |
34140 KB |
Output is correct |
42 |
Correct |
16 ms |
33552 KB |
Output is correct |
43 |
Correct |
16 ms |
33884 KB |
Output is correct |
44 |
Correct |
17 ms |
33628 KB |
Output is correct |
45 |
Correct |
20 ms |
33884 KB |
Output is correct |
46 |
Correct |
15 ms |
33628 KB |
Output is correct |
47 |
Correct |
15 ms |
33860 KB |
Output is correct |
48 |
Correct |
18 ms |
33628 KB |
Output is correct |
49 |
Correct |
15 ms |
33628 KB |
Output is correct |
50 |
Correct |
18 ms |
33624 KB |
Output is correct |
51 |
Correct |
15 ms |
33628 KB |
Output is correct |
52 |
Correct |
15 ms |
33628 KB |
Output is correct |
53 |
Correct |
15 ms |
33628 KB |
Output is correct |
54 |
Correct |
18 ms |
33628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
33116 KB |
Output is correct |
2 |
Correct |
17 ms |
33344 KB |
Output is correct |
3 |
Correct |
14 ms |
33368 KB |
Output is correct |
4 |
Correct |
14 ms |
33164 KB |
Output is correct |
5 |
Correct |
14 ms |
33116 KB |
Output is correct |
6 |
Correct |
16 ms |
33116 KB |
Output is correct |
7 |
Correct |
15 ms |
33116 KB |
Output is correct |
8 |
Correct |
15 ms |
33368 KB |
Output is correct |
9 |
Correct |
15 ms |
33116 KB |
Output is correct |
10 |
Correct |
15 ms |
33108 KB |
Output is correct |
11 |
Correct |
16 ms |
33372 KB |
Output is correct |
12 |
Correct |
15 ms |
33116 KB |
Output is correct |
13 |
Correct |
14 ms |
33116 KB |
Output is correct |
14 |
Correct |
16 ms |
33116 KB |
Output is correct |
15 |
Correct |
16 ms |
33112 KB |
Output is correct |
16 |
Correct |
15 ms |
33288 KB |
Output is correct |
17 |
Correct |
16 ms |
33116 KB |
Output is correct |
18 |
Correct |
15 ms |
33116 KB |
Output is correct |
19 |
Correct |
15 ms |
33116 KB |
Output is correct |
20 |
Correct |
16 ms |
33320 KB |
Output is correct |
21 |
Correct |
15 ms |
33372 KB |
Output is correct |
22 |
Correct |
18 ms |
33368 KB |
Output is correct |
23 |
Correct |
14 ms |
33372 KB |
Output is correct |
24 |
Correct |
13 ms |
33388 KB |
Output is correct |
25 |
Correct |
16 ms |
33368 KB |
Output is correct |
26 |
Correct |
16 ms |
33368 KB |
Output is correct |
27 |
Correct |
16 ms |
33376 KB |
Output is correct |
28 |
Correct |
16 ms |
33116 KB |
Output is correct |
29 |
Correct |
15 ms |
33352 KB |
Output is correct |
30 |
Correct |
15 ms |
33368 KB |
Output is correct |
31 |
Correct |
14 ms |
33372 KB |
Output is correct |
32 |
Correct |
14 ms |
33372 KB |
Output is correct |
33 |
Correct |
15 ms |
33368 KB |
Output is correct |
34 |
Correct |
14 ms |
33372 KB |
Output is correct |
35 |
Correct |
15 ms |
33624 KB |
Output is correct |
36 |
Correct |
15 ms |
33624 KB |
Output is correct |
37 |
Correct |
15 ms |
33628 KB |
Output is correct |
38 |
Correct |
15 ms |
33540 KB |
Output is correct |
39 |
Correct |
16 ms |
33628 KB |
Output is correct |
40 |
Correct |
15 ms |
34136 KB |
Output is correct |
41 |
Correct |
16 ms |
34140 KB |
Output is correct |
42 |
Correct |
16 ms |
33552 KB |
Output is correct |
43 |
Correct |
16 ms |
33884 KB |
Output is correct |
44 |
Correct |
17 ms |
33628 KB |
Output is correct |
45 |
Correct |
20 ms |
33884 KB |
Output is correct |
46 |
Correct |
15 ms |
33628 KB |
Output is correct |
47 |
Correct |
15 ms |
33860 KB |
Output is correct |
48 |
Correct |
18 ms |
33628 KB |
Output is correct |
49 |
Correct |
15 ms |
33628 KB |
Output is correct |
50 |
Correct |
18 ms |
33624 KB |
Output is correct |
51 |
Correct |
15 ms |
33628 KB |
Output is correct |
52 |
Correct |
15 ms |
33628 KB |
Output is correct |
53 |
Correct |
15 ms |
33628 KB |
Output is correct |
54 |
Correct |
18 ms |
33628 KB |
Output is correct |
55 |
Correct |
21 ms |
34392 KB |
Output is correct |
56 |
Correct |
31 ms |
37212 KB |
Output is correct |
57 |
Correct |
46 ms |
39768 KB |
Output is correct |
58 |
Correct |
62 ms |
41676 KB |
Output is correct |
59 |
Correct |
81 ms |
44680 KB |
Output is correct |
60 |
Correct |
91 ms |
47336 KB |
Output is correct |
61 |
Correct |
104 ms |
49764 KB |
Output is correct |
62 |
Correct |
112 ms |
51404 KB |
Output is correct |
63 |
Correct |
131 ms |
54588 KB |
Output is correct |
64 |
Correct |
158 ms |
55752 KB |
Output is correct |
65 |
Correct |
88 ms |
85024 KB |
Output is correct |
66 |
Correct |
87 ms |
85072 KB |
Output is correct |
67 |
Correct |
89 ms |
52308 KB |
Output is correct |
68 |
Correct |
110 ms |
69636 KB |
Output is correct |
69 |
Correct |
121 ms |
67268 KB |
Output is correct |
70 |
Correct |
119 ms |
67304 KB |
Output is correct |
71 |
Correct |
139 ms |
66236 KB |
Output is correct |
72 |
Correct |
144 ms |
66500 KB |
Output is correct |
73 |
Correct |
116 ms |
62708 KB |
Output is correct |
74 |
Correct |
114 ms |
62904 KB |
Output is correct |
75 |
Correct |
116 ms |
61372 KB |
Output is correct |
76 |
Correct |
129 ms |
61504 KB |
Output is correct |
77 |
Correct |
129 ms |
60612 KB |
Output is correct |
78 |
Correct |
128 ms |
59116 KB |
Output is correct |
79 |
Correct |
118 ms |
59584 KB |
Output is correct |