#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
const int mxN=3e5;
int n, m;
vector<ar<int, 2>> adj[mxN];
ll yi[mxN], ys[mxN]; // y intercept and y intercept slope
priority_queue<ll> pq[mxN];
void dfs(int u=0) {
for (auto [v, w] : adj[u]) {
if (adj[v].empty()) {
yi[v]=w, ys[v]=-1;
pq[v].push(w);
pq[v].push(w);
} else {
dfs(v);
// shift container by w
int last=-1;
while(ys[v]+(int)pq[v].size()>0) {
last=pq[v].top();
pq[v].pop();
}
assert(last!=-1&&pq[v].size());
int first=pq[v].top();
pq[v].pop();
yi[v]+=w;
pq[v].push(first+w);
pq[v].push(last+w);
}
yi[u]+=yi[v];
ys[u]+=ys[v];
if (pq[v].size()>pq[u].size())
swap(pq[u], pq[v]);
while(pq[v].size()) {
pq[u].push(pq[v].top());
pq[v].pop();
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i=1; i<n+m; ++i) {
int p, c;
cin >> p >> c, --p;
adj[p].push_back({i, c});
}
dfs();
ll ans=yi[0], slope=ys[0];
vector<int> changes;
while(pq[0].size()) {
changes.push_back(pq[0].top());
pq[0].pop();
}
reverse(changes.begin(), changes.end());
ll last=0;
for (int i=0; slope<0; ++i, ++slope) {
assert(i<changes.size());
ans+=(changes[i]-last)*slope;
last=changes[i];
}
cout << ans;
return 0;
}
Compilation message
In file included from /usr/include/c++/10/cassert:44,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
from fireworks.cpp:1:
fireworks.cpp: In function 'int main()':
fireworks.cpp:64:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | assert(i<changes.size());
| ~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
16724 KB |
Output is correct |
2 |
Correct |
9 ms |
16724 KB |
Output is correct |
3 |
Correct |
13 ms |
16712 KB |
Output is correct |
4 |
Correct |
9 ms |
16780 KB |
Output is correct |
5 |
Correct |
9 ms |
16776 KB |
Output is correct |
6 |
Correct |
10 ms |
16692 KB |
Output is correct |
7 |
Correct |
9 ms |
16724 KB |
Output is correct |
8 |
Correct |
9 ms |
16724 KB |
Output is correct |
9 |
Correct |
10 ms |
16772 KB |
Output is correct |
10 |
Correct |
9 ms |
16772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
16656 KB |
Output is correct |
2 |
Correct |
9 ms |
16780 KB |
Output is correct |
3 |
Correct |
9 ms |
16776 KB |
Output is correct |
4 |
Correct |
12 ms |
16708 KB |
Output is correct |
5 |
Correct |
11 ms |
16712 KB |
Output is correct |
6 |
Correct |
11 ms |
16776 KB |
Output is correct |
7 |
Correct |
10 ms |
16724 KB |
Output is correct |
8 |
Correct |
11 ms |
16724 KB |
Output is correct |
9 |
Correct |
10 ms |
16776 KB |
Output is correct |
10 |
Correct |
9 ms |
16676 KB |
Output is correct |
11 |
Correct |
9 ms |
16772 KB |
Output is correct |
12 |
Correct |
9 ms |
16724 KB |
Output is correct |
13 |
Correct |
9 ms |
16696 KB |
Output is correct |
14 |
Correct |
9 ms |
16792 KB |
Output is correct |
15 |
Correct |
9 ms |
16724 KB |
Output is correct |
16 |
Correct |
10 ms |
16772 KB |
Output is correct |
17 |
Correct |
9 ms |
16724 KB |
Output is correct |
18 |
Correct |
10 ms |
16780 KB |
Output is correct |
19 |
Correct |
12 ms |
16724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
16724 KB |
Output is correct |
2 |
Correct |
9 ms |
16724 KB |
Output is correct |
3 |
Correct |
13 ms |
16712 KB |
Output is correct |
4 |
Correct |
9 ms |
16780 KB |
Output is correct |
5 |
Correct |
9 ms |
16776 KB |
Output is correct |
6 |
Correct |
10 ms |
16692 KB |
Output is correct |
7 |
Correct |
9 ms |
16724 KB |
Output is correct |
8 |
Correct |
9 ms |
16724 KB |
Output is correct |
9 |
Correct |
10 ms |
16772 KB |
Output is correct |
10 |
Correct |
9 ms |
16772 KB |
Output is correct |
11 |
Correct |
8 ms |
16656 KB |
Output is correct |
12 |
Correct |
9 ms |
16780 KB |
Output is correct |
13 |
Correct |
9 ms |
16776 KB |
Output is correct |
14 |
Correct |
12 ms |
16708 KB |
Output is correct |
15 |
Correct |
11 ms |
16712 KB |
Output is correct |
16 |
Correct |
11 ms |
16776 KB |
Output is correct |
17 |
Correct |
10 ms |
16724 KB |
Output is correct |
18 |
Correct |
11 ms |
16724 KB |
Output is correct |
19 |
Correct |
10 ms |
16776 KB |
Output is correct |
20 |
Correct |
9 ms |
16676 KB |
Output is correct |
21 |
Correct |
9 ms |
16772 KB |
Output is correct |
22 |
Correct |
9 ms |
16724 KB |
Output is correct |
23 |
Correct |
9 ms |
16696 KB |
Output is correct |
24 |
Correct |
9 ms |
16792 KB |
Output is correct |
25 |
Correct |
9 ms |
16724 KB |
Output is correct |
26 |
Correct |
10 ms |
16772 KB |
Output is correct |
27 |
Correct |
9 ms |
16724 KB |
Output is correct |
28 |
Correct |
10 ms |
16780 KB |
Output is correct |
29 |
Correct |
12 ms |
16724 KB |
Output is correct |
30 |
Incorrect |
11 ms |
16776 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
16724 KB |
Output is correct |
2 |
Correct |
9 ms |
16724 KB |
Output is correct |
3 |
Correct |
13 ms |
16712 KB |
Output is correct |
4 |
Correct |
9 ms |
16780 KB |
Output is correct |
5 |
Correct |
9 ms |
16776 KB |
Output is correct |
6 |
Correct |
10 ms |
16692 KB |
Output is correct |
7 |
Correct |
9 ms |
16724 KB |
Output is correct |
8 |
Correct |
9 ms |
16724 KB |
Output is correct |
9 |
Correct |
10 ms |
16772 KB |
Output is correct |
10 |
Correct |
9 ms |
16772 KB |
Output is correct |
11 |
Correct |
8 ms |
16656 KB |
Output is correct |
12 |
Correct |
9 ms |
16780 KB |
Output is correct |
13 |
Correct |
9 ms |
16776 KB |
Output is correct |
14 |
Correct |
12 ms |
16708 KB |
Output is correct |
15 |
Correct |
11 ms |
16712 KB |
Output is correct |
16 |
Correct |
11 ms |
16776 KB |
Output is correct |
17 |
Correct |
10 ms |
16724 KB |
Output is correct |
18 |
Correct |
11 ms |
16724 KB |
Output is correct |
19 |
Correct |
10 ms |
16776 KB |
Output is correct |
20 |
Correct |
9 ms |
16676 KB |
Output is correct |
21 |
Correct |
9 ms |
16772 KB |
Output is correct |
22 |
Correct |
9 ms |
16724 KB |
Output is correct |
23 |
Correct |
9 ms |
16696 KB |
Output is correct |
24 |
Correct |
9 ms |
16792 KB |
Output is correct |
25 |
Correct |
9 ms |
16724 KB |
Output is correct |
26 |
Correct |
10 ms |
16772 KB |
Output is correct |
27 |
Correct |
9 ms |
16724 KB |
Output is correct |
28 |
Correct |
10 ms |
16780 KB |
Output is correct |
29 |
Correct |
12 ms |
16724 KB |
Output is correct |
30 |
Incorrect |
11 ms |
16776 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |