#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline","03") // Pragma, Optimization flags
#define db(x) cerr << #x << ": " << (x) << '\n';
#define f first
#define s second
typedef long double ld;
typedef long long ll;
/// Constraints----------------------------------------------
const int maxn = 600010;
const int mod2 = 1000000007;
const int mod = 998244353;
const ld eps = 1e-9;
const ll INF = 1ll * mod * mod;
/// Quick Functions------------------------------------------
int qpow(int b,int e){
if( !e ) return 1;
if( e & 1 ) return 1ll * qpow(b,e-1) * b % mod;
int pwur = qpow(b,e>>1);
return 1ll * pwur * pwur % mod;
}
mt19937_64 random_(time(0));
int rng (int a, int b){
return uniform_int_distribution<int>(a,b)(random_);
}
/// Code-----------------------------------------------------
struct plf{
ll m0, n0, offset;
multiset<ll> s;
plf(){
m0 = n0 = offset = 0;
s.clear();
}
};
int p[maxn], pedge[maxn];
vector<pair<int,int>> g[maxn];
int n, m;
plf dp[maxn];
ll getmin(int x){
ll curr = dp[x].m0 * (*dp[x].s.begin()+dp[x].offset) + dp[x].n0;
ll ans = INF;
//if( (*dp[x].s.begin()) + dp[x].offset >= 0 )
ans = min( ans , curr );
vector<ll> v;
for( auto i : dp[x].s )
v.push_back(i);
// db(dp[x].m0)
// db(dp[x].n0)
// db(v[0])
for(int i=1; i<v.size(); i++){
// if( v[i] > 0 && v[i-1] <= 0 ){
// ans = min( ans , curr + ( dp[x].m0 + ( i ) ) * ( 0 - v[i-1] ) );
// }
curr = curr + ( dp[x].m0 + ( i ) ) * ( v[i] - v[i-1] );
// db(v[i])
//if( v[i] + dp[x].offset >= 0 )
ans = min( ans , curr );
}
return ans;
}
void dfs(int u){
dp[u] = plf();
if( g[u].size() == 0 ) return;
for( auto v : g[u] ){
dfs(v.f);
if( v.f > n ){
dp[v.f].m0 = -1;
dp[v.f].n0 = 0;
dp[v.f].s.insert(0);
dp[v.f].s.insert(0);
//cout << v.f << ' ' << getmin(v.f) << '\n';
}
dp[v.f].offset += v.s;
dp[v.f].n0 -= dp[v.f].m0 * v.s;
dp[u].m0 += dp[v.f].m0;
dp[u].n0 += dp[v.f].n0;
if( dp[u].s.size() < dp[v.f].s.size() ){
swap( dp[u].s , dp[v.f].s );
swap( dp[u].offset , dp[v.f].offset );
}
for( auto i : dp[v.f].s )
dp[u].s.insert(i+dp[v.f].offset-dp[u].offset);
}
//cout << u << ' ' << getmin(u) << '\n';
while( dp[u].m0 + dp[u].s.size() > 1 ){
auto it = dp[u].s.end(); it--;
dp[u].s.erase(it);
}
stack<ll> l2;
for(int i=0; i<2; i++){
auto it = dp[u].s.end(); it--;
l2.push(*it+dp[u].offset);
dp[u].s.erase(it);
}
dp[u].offset -= pedge[u];//db(u)db(pedge[u])
dp[u].n0 += ( dp[u].m0 + 1 ) * ( pedge[u] );
//dp[u].n0 = pedge[u];
while( !l2.empty() ){
dp[u].s.insert(l2.top()-dp[u].offset);
l2.pop();
}
//cout << u << ' ' << getmin(u) << '\n';//db(pedge[u])
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cout.setf(ios::fixed); cout.precision(10);
//freopen("a.in","r",stdin);
//freopen("a.in","w",stdout);
cin >> n >> m;
for(int i=2; i<=n+m; i++){
cin >> p[i] >> pedge[i];
g[p[i]].push_back({i,pedge[i]});
}
dfs(1);
cout << getmin(1) << '\n';
return 0;
}
Compilation message
fireworks.cpp: In function 'll getmin(int)':
fireworks.cpp:55:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for(int i=1; i<v.size(); i++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
56660 KB |
Output is correct |
2 |
Correct |
29 ms |
56640 KB |
Output is correct |
3 |
Correct |
30 ms |
56708 KB |
Output is correct |
4 |
Correct |
29 ms |
56680 KB |
Output is correct |
5 |
Correct |
29 ms |
56696 KB |
Output is correct |
6 |
Correct |
30 ms |
56604 KB |
Output is correct |
7 |
Correct |
30 ms |
56680 KB |
Output is correct |
8 |
Correct |
29 ms |
56584 KB |
Output is correct |
9 |
Correct |
29 ms |
56668 KB |
Output is correct |
10 |
Correct |
29 ms |
56672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
56612 KB |
Output is correct |
2 |
Correct |
31 ms |
56596 KB |
Output is correct |
3 |
Correct |
29 ms |
56668 KB |
Output is correct |
4 |
Correct |
31 ms |
56652 KB |
Output is correct |
5 |
Correct |
30 ms |
56684 KB |
Output is correct |
6 |
Correct |
30 ms |
56600 KB |
Output is correct |
7 |
Correct |
29 ms |
56692 KB |
Output is correct |
8 |
Correct |
30 ms |
56648 KB |
Output is correct |
9 |
Correct |
30 ms |
56648 KB |
Output is correct |
10 |
Correct |
30 ms |
56604 KB |
Output is correct |
11 |
Correct |
29 ms |
56644 KB |
Output is correct |
12 |
Correct |
30 ms |
56640 KB |
Output is correct |
13 |
Correct |
29 ms |
56680 KB |
Output is correct |
14 |
Correct |
29 ms |
56680 KB |
Output is correct |
15 |
Correct |
30 ms |
56740 KB |
Output is correct |
16 |
Correct |
29 ms |
56684 KB |
Output is correct |
17 |
Correct |
30 ms |
56652 KB |
Output is correct |
18 |
Correct |
29 ms |
56652 KB |
Output is correct |
19 |
Correct |
29 ms |
56728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
56660 KB |
Output is correct |
2 |
Correct |
29 ms |
56640 KB |
Output is correct |
3 |
Correct |
30 ms |
56708 KB |
Output is correct |
4 |
Correct |
29 ms |
56680 KB |
Output is correct |
5 |
Correct |
29 ms |
56696 KB |
Output is correct |
6 |
Correct |
30 ms |
56604 KB |
Output is correct |
7 |
Correct |
30 ms |
56680 KB |
Output is correct |
8 |
Correct |
29 ms |
56584 KB |
Output is correct |
9 |
Correct |
29 ms |
56668 KB |
Output is correct |
10 |
Correct |
29 ms |
56672 KB |
Output is correct |
11 |
Correct |
29 ms |
56612 KB |
Output is correct |
12 |
Correct |
31 ms |
56596 KB |
Output is correct |
13 |
Correct |
29 ms |
56668 KB |
Output is correct |
14 |
Correct |
31 ms |
56652 KB |
Output is correct |
15 |
Correct |
30 ms |
56684 KB |
Output is correct |
16 |
Correct |
30 ms |
56600 KB |
Output is correct |
17 |
Correct |
29 ms |
56692 KB |
Output is correct |
18 |
Correct |
30 ms |
56648 KB |
Output is correct |
19 |
Correct |
30 ms |
56648 KB |
Output is correct |
20 |
Correct |
30 ms |
56604 KB |
Output is correct |
21 |
Correct |
29 ms |
56644 KB |
Output is correct |
22 |
Correct |
30 ms |
56640 KB |
Output is correct |
23 |
Correct |
29 ms |
56680 KB |
Output is correct |
24 |
Correct |
29 ms |
56680 KB |
Output is correct |
25 |
Correct |
30 ms |
56740 KB |
Output is correct |
26 |
Correct |
29 ms |
56684 KB |
Output is correct |
27 |
Correct |
30 ms |
56652 KB |
Output is correct |
28 |
Correct |
29 ms |
56652 KB |
Output is correct |
29 |
Correct |
29 ms |
56728 KB |
Output is correct |
30 |
Correct |
30 ms |
56628 KB |
Output is correct |
31 |
Correct |
31 ms |
56800 KB |
Output is correct |
32 |
Correct |
31 ms |
56908 KB |
Output is correct |
33 |
Correct |
31 ms |
56988 KB |
Output is correct |
34 |
Correct |
33 ms |
57036 KB |
Output is correct |
35 |
Correct |
32 ms |
57096 KB |
Output is correct |
36 |
Correct |
33 ms |
57188 KB |
Output is correct |
37 |
Correct |
33 ms |
57332 KB |
Output is correct |
38 |
Correct |
34 ms |
57356 KB |
Output is correct |
39 |
Correct |
33 ms |
57284 KB |
Output is correct |
40 |
Correct |
32 ms |
58200 KB |
Output is correct |
41 |
Correct |
33 ms |
58156 KB |
Output is correct |
42 |
Correct |
32 ms |
56908 KB |
Output is correct |
43 |
Correct |
33 ms |
57780 KB |
Output is correct |
44 |
Correct |
34 ms |
57700 KB |
Output is correct |
45 |
Correct |
34 ms |
57676 KB |
Output is correct |
46 |
Correct |
34 ms |
57836 KB |
Output is correct |
47 |
Correct |
34 ms |
57780 KB |
Output is correct |
48 |
Correct |
33 ms |
57700 KB |
Output is correct |
49 |
Correct |
34 ms |
57948 KB |
Output is correct |
50 |
Correct |
33 ms |
57536 KB |
Output is correct |
51 |
Correct |
33 ms |
57500 KB |
Output is correct |
52 |
Correct |
35 ms |
57556 KB |
Output is correct |
53 |
Correct |
34 ms |
57652 KB |
Output is correct |
54 |
Correct |
35 ms |
57800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
56660 KB |
Output is correct |
2 |
Correct |
29 ms |
56640 KB |
Output is correct |
3 |
Correct |
30 ms |
56708 KB |
Output is correct |
4 |
Correct |
29 ms |
56680 KB |
Output is correct |
5 |
Correct |
29 ms |
56696 KB |
Output is correct |
6 |
Correct |
30 ms |
56604 KB |
Output is correct |
7 |
Correct |
30 ms |
56680 KB |
Output is correct |
8 |
Correct |
29 ms |
56584 KB |
Output is correct |
9 |
Correct |
29 ms |
56668 KB |
Output is correct |
10 |
Correct |
29 ms |
56672 KB |
Output is correct |
11 |
Correct |
29 ms |
56612 KB |
Output is correct |
12 |
Correct |
31 ms |
56596 KB |
Output is correct |
13 |
Correct |
29 ms |
56668 KB |
Output is correct |
14 |
Correct |
31 ms |
56652 KB |
Output is correct |
15 |
Correct |
30 ms |
56684 KB |
Output is correct |
16 |
Correct |
30 ms |
56600 KB |
Output is correct |
17 |
Correct |
29 ms |
56692 KB |
Output is correct |
18 |
Correct |
30 ms |
56648 KB |
Output is correct |
19 |
Correct |
30 ms |
56648 KB |
Output is correct |
20 |
Correct |
30 ms |
56604 KB |
Output is correct |
21 |
Correct |
29 ms |
56644 KB |
Output is correct |
22 |
Correct |
30 ms |
56640 KB |
Output is correct |
23 |
Correct |
29 ms |
56680 KB |
Output is correct |
24 |
Correct |
29 ms |
56680 KB |
Output is correct |
25 |
Correct |
30 ms |
56740 KB |
Output is correct |
26 |
Correct |
29 ms |
56684 KB |
Output is correct |
27 |
Correct |
30 ms |
56652 KB |
Output is correct |
28 |
Correct |
29 ms |
56652 KB |
Output is correct |
29 |
Correct |
29 ms |
56728 KB |
Output is correct |
30 |
Correct |
30 ms |
56628 KB |
Output is correct |
31 |
Correct |
31 ms |
56800 KB |
Output is correct |
32 |
Correct |
31 ms |
56908 KB |
Output is correct |
33 |
Correct |
31 ms |
56988 KB |
Output is correct |
34 |
Correct |
33 ms |
57036 KB |
Output is correct |
35 |
Correct |
32 ms |
57096 KB |
Output is correct |
36 |
Correct |
33 ms |
57188 KB |
Output is correct |
37 |
Correct |
33 ms |
57332 KB |
Output is correct |
38 |
Correct |
34 ms |
57356 KB |
Output is correct |
39 |
Correct |
33 ms |
57284 KB |
Output is correct |
40 |
Correct |
32 ms |
58200 KB |
Output is correct |
41 |
Correct |
33 ms |
58156 KB |
Output is correct |
42 |
Correct |
32 ms |
56908 KB |
Output is correct |
43 |
Correct |
33 ms |
57780 KB |
Output is correct |
44 |
Correct |
34 ms |
57700 KB |
Output is correct |
45 |
Correct |
34 ms |
57676 KB |
Output is correct |
46 |
Correct |
34 ms |
57836 KB |
Output is correct |
47 |
Correct |
34 ms |
57780 KB |
Output is correct |
48 |
Correct |
33 ms |
57700 KB |
Output is correct |
49 |
Correct |
34 ms |
57948 KB |
Output is correct |
50 |
Correct |
33 ms |
57536 KB |
Output is correct |
51 |
Correct |
33 ms |
57500 KB |
Output is correct |
52 |
Correct |
35 ms |
57556 KB |
Output is correct |
53 |
Correct |
34 ms |
57652 KB |
Output is correct |
54 |
Correct |
35 ms |
57800 KB |
Output is correct |
55 |
Correct |
39 ms |
58484 KB |
Output is correct |
56 |
Correct |
77 ms |
63992 KB |
Output is correct |
57 |
Correct |
116 ms |
69212 KB |
Output is correct |
58 |
Correct |
145 ms |
73104 KB |
Output is correct |
59 |
Correct |
196 ms |
78332 KB |
Output is correct |
60 |
Correct |
230 ms |
83704 KB |
Output is correct |
61 |
Correct |
282 ms |
87740 KB |
Output is correct |
62 |
Correct |
290 ms |
91584 KB |
Output is correct |
63 |
Correct |
346 ms |
98708 KB |
Output is correct |
64 |
Correct |
372 ms |
99512 KB |
Output is correct |
65 |
Correct |
209 ms |
148420 KB |
Output is correct |
66 |
Correct |
206 ms |
148320 KB |
Output is correct |
67 |
Correct |
198 ms |
73668 KB |
Output is correct |
68 |
Correct |
300 ms |
129392 KB |
Output is correct |
69 |
Correct |
342 ms |
124108 KB |
Output is correct |
70 |
Correct |
348 ms |
124068 KB |
Output is correct |
71 |
Correct |
486 ms |
139996 KB |
Output is correct |
72 |
Correct |
507 ms |
139396 KB |
Output is correct |
73 |
Correct |
511 ms |
126664 KB |
Output is correct |
74 |
Correct |
528 ms |
126644 KB |
Output is correct |
75 |
Correct |
501 ms |
125848 KB |
Output is correct |
76 |
Correct |
503 ms |
125876 KB |
Output is correct |
77 |
Correct |
501 ms |
123952 KB |
Output is correct |
78 |
Correct |
528 ms |
120888 KB |
Output is correct |
79 |
Correct |
552 ms |
127280 KB |
Output is correct |