#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<cassert>
#include<unordered_map>
#include <queue>
#include <cstdint>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric>
#include<bitset>
using namespace std;
//#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-lopps")
#define int long long
#define double long double
using namespace std;
const int mod=998244353,mxn=3e5+5,inf=1e18,minf=-1e18,lg=62;
//#undef int
int n,k,m,q;
void setIO(string name){
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
struct slope{
priority_queue<int>point;
int m=0,c=0,ans=0;
int getsz(){return point.size();}
void print(){
vector<int>keep;
while(!point.empty())keep.pb(point.top()),point.pop();
sort(all(keep));
for(auto i:keep)cout<<i<<" ",point.push(i);
cout<<'\n';
}
};
slope dp[mxn+10];
vector<int>adj[mxn+10];
int up[mxn+10];
void merge(int x,int y){
if(dp[x].getsz()<dp[y].getsz())swap(dp[x],dp[y]);
dp[x].m+=dp[y].m;
dp[x].c+=dp[y].c;
while(!dp[y].point.empty())dp[x].point.push(dp[y].point.top()),dp[y].point.pop();
}
void dfs(int cur,int p){
for(auto i:adj[cur])if(i!=p)dfs(i,cur),merge(cur,i);
if(cur>n)dp[cur].point.push(0),dp[cur].point.push(0),dp[cur].m=1,dp[cur].c=0;
while(dp[cur].m>1)dp[cur].m--,dp[cur].c+=dp[cur].point.top(),dp[cur].point.pop();
int a,b;
a=dp[cur].point.top(),dp[cur].point.pop();
b=dp[cur].point.top(),dp[cur].point.pop();
dp[cur].point.push(a+up[cur]),dp[cur].point.push(b+up[cur]);
dp[cur].c-=up[cur];
//shifting
}
int32_t main(){
fastio
cin>>n>>m;
for(int i=2;i<=n+m;i++){
int a,b;cin>>a>>b;
adj[a].pb(i);
up[i]=b;
}
dfs(1,-1);
cout<<dp[1].m*(dp[1].point.top())+dp[1].c;
}
/*
dp[i][x]=sum of dp[child][x];
then we shift dp[i][x]=dp[i][x-k] when k is the cost of parent edge
we can keep the slope chaning point
merge all child small to large and shift the dp by k
*/
Compilation message
fireworks.cpp:32:40: warning: bad option '-funroll-lopps' to pragma 'optimize' [-Wpragmas]
32 | #pragma GCC optimize ("03,unroll-lopps")
| ^
fireworks.cpp:39:23: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
39 | void setIO(string name){
| ^
fireworks.cpp:47:12: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
47 | int getsz(){return point.size();}
| ^
fireworks.cpp:48:13: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
48 | void print(){
| ^
fireworks.cpp:59:23: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
59 | void merge(int x,int y){
| ^
fireworks.cpp:65:23: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
65 | void dfs(int cur,int p){
| ^
fireworks.cpp:77:14: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
77 | int32_t main(){
| ^
fireworks.cpp: In function 'void setIO(std::string)':
fireworks.cpp:41:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | freopen((name+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fireworks.cpp:42:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | freopen((name+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23948 KB |
Output is correct |
2 |
Correct |
12 ms |
23948 KB |
Output is correct |
3 |
Correct |
9 ms |
23900 KB |
Output is correct |
4 |
Correct |
9 ms |
23872 KB |
Output is correct |
5 |
Correct |
12 ms |
23900 KB |
Output is correct |
6 |
Correct |
9 ms |
23900 KB |
Output is correct |
7 |
Correct |
11 ms |
23960 KB |
Output is correct |
8 |
Correct |
10 ms |
23900 KB |
Output is correct |
9 |
Correct |
10 ms |
23948 KB |
Output is correct |
10 |
Correct |
11 ms |
23744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23944 KB |
Output is correct |
2 |
Correct |
9 ms |
23760 KB |
Output is correct |
3 |
Correct |
9 ms |
23900 KB |
Output is correct |
4 |
Correct |
10 ms |
23896 KB |
Output is correct |
5 |
Correct |
10 ms |
23900 KB |
Output is correct |
6 |
Correct |
9 ms |
23936 KB |
Output is correct |
7 |
Correct |
10 ms |
23896 KB |
Output is correct |
8 |
Correct |
11 ms |
23896 KB |
Output is correct |
9 |
Correct |
9 ms |
23900 KB |
Output is correct |
10 |
Correct |
10 ms |
23712 KB |
Output is correct |
11 |
Correct |
13 ms |
23900 KB |
Output is correct |
12 |
Correct |
10 ms |
23900 KB |
Output is correct |
13 |
Correct |
10 ms |
23900 KB |
Output is correct |
14 |
Correct |
10 ms |
23756 KB |
Output is correct |
15 |
Correct |
10 ms |
23792 KB |
Output is correct |
16 |
Correct |
11 ms |
23876 KB |
Output is correct |
17 |
Correct |
9 ms |
23948 KB |
Output is correct |
18 |
Correct |
10 ms |
23948 KB |
Output is correct |
19 |
Correct |
10 ms |
23896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23948 KB |
Output is correct |
2 |
Correct |
12 ms |
23948 KB |
Output is correct |
3 |
Correct |
9 ms |
23900 KB |
Output is correct |
4 |
Correct |
9 ms |
23872 KB |
Output is correct |
5 |
Correct |
12 ms |
23900 KB |
Output is correct |
6 |
Correct |
9 ms |
23900 KB |
Output is correct |
7 |
Correct |
11 ms |
23960 KB |
Output is correct |
8 |
Correct |
10 ms |
23900 KB |
Output is correct |
9 |
Correct |
10 ms |
23948 KB |
Output is correct |
10 |
Correct |
11 ms |
23744 KB |
Output is correct |
11 |
Correct |
11 ms |
23944 KB |
Output is correct |
12 |
Correct |
9 ms |
23760 KB |
Output is correct |
13 |
Correct |
9 ms |
23900 KB |
Output is correct |
14 |
Correct |
10 ms |
23896 KB |
Output is correct |
15 |
Correct |
10 ms |
23900 KB |
Output is correct |
16 |
Correct |
9 ms |
23936 KB |
Output is correct |
17 |
Correct |
10 ms |
23896 KB |
Output is correct |
18 |
Correct |
11 ms |
23896 KB |
Output is correct |
19 |
Correct |
9 ms |
23900 KB |
Output is correct |
20 |
Correct |
10 ms |
23712 KB |
Output is correct |
21 |
Correct |
13 ms |
23900 KB |
Output is correct |
22 |
Correct |
10 ms |
23900 KB |
Output is correct |
23 |
Correct |
10 ms |
23900 KB |
Output is correct |
24 |
Correct |
10 ms |
23756 KB |
Output is correct |
25 |
Correct |
10 ms |
23792 KB |
Output is correct |
26 |
Correct |
11 ms |
23876 KB |
Output is correct |
27 |
Correct |
9 ms |
23948 KB |
Output is correct |
28 |
Correct |
10 ms |
23948 KB |
Output is correct |
29 |
Correct |
10 ms |
23896 KB |
Output is correct |
30 |
Correct |
11 ms |
23844 KB |
Output is correct |
31 |
Correct |
14 ms |
23896 KB |
Output is correct |
32 |
Correct |
10 ms |
23956 KB |
Output is correct |
33 |
Correct |
10 ms |
23900 KB |
Output is correct |
34 |
Correct |
13 ms |
24160 KB |
Output is correct |
35 |
Correct |
13 ms |
24156 KB |
Output is correct |
36 |
Correct |
15 ms |
24152 KB |
Output is correct |
37 |
Correct |
11 ms |
24112 KB |
Output is correct |
38 |
Correct |
11 ms |
24296 KB |
Output is correct |
39 |
Correct |
11 ms |
24152 KB |
Output is correct |
40 |
Correct |
13 ms |
24664 KB |
Output is correct |
41 |
Correct |
11 ms |
24664 KB |
Output is correct |
42 |
Correct |
11 ms |
24156 KB |
Output is correct |
43 |
Correct |
11 ms |
24412 KB |
Output is correct |
44 |
Correct |
11 ms |
24412 KB |
Output is correct |
45 |
Correct |
11 ms |
24352 KB |
Output is correct |
46 |
Correct |
11 ms |
24412 KB |
Output is correct |
47 |
Correct |
11 ms |
24416 KB |
Output is correct |
48 |
Correct |
12 ms |
24156 KB |
Output is correct |
49 |
Correct |
11 ms |
24152 KB |
Output is correct |
50 |
Correct |
12 ms |
24168 KB |
Output is correct |
51 |
Correct |
11 ms |
24152 KB |
Output is correct |
52 |
Correct |
11 ms |
24156 KB |
Output is correct |
53 |
Correct |
11 ms |
24152 KB |
Output is correct |
54 |
Correct |
11 ms |
24156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23948 KB |
Output is correct |
2 |
Correct |
12 ms |
23948 KB |
Output is correct |
3 |
Correct |
9 ms |
23900 KB |
Output is correct |
4 |
Correct |
9 ms |
23872 KB |
Output is correct |
5 |
Correct |
12 ms |
23900 KB |
Output is correct |
6 |
Correct |
9 ms |
23900 KB |
Output is correct |
7 |
Correct |
11 ms |
23960 KB |
Output is correct |
8 |
Correct |
10 ms |
23900 KB |
Output is correct |
9 |
Correct |
10 ms |
23948 KB |
Output is correct |
10 |
Correct |
11 ms |
23744 KB |
Output is correct |
11 |
Correct |
11 ms |
23944 KB |
Output is correct |
12 |
Correct |
9 ms |
23760 KB |
Output is correct |
13 |
Correct |
9 ms |
23900 KB |
Output is correct |
14 |
Correct |
10 ms |
23896 KB |
Output is correct |
15 |
Correct |
10 ms |
23900 KB |
Output is correct |
16 |
Correct |
9 ms |
23936 KB |
Output is correct |
17 |
Correct |
10 ms |
23896 KB |
Output is correct |
18 |
Correct |
11 ms |
23896 KB |
Output is correct |
19 |
Correct |
9 ms |
23900 KB |
Output is correct |
20 |
Correct |
10 ms |
23712 KB |
Output is correct |
21 |
Correct |
13 ms |
23900 KB |
Output is correct |
22 |
Correct |
10 ms |
23900 KB |
Output is correct |
23 |
Correct |
10 ms |
23900 KB |
Output is correct |
24 |
Correct |
10 ms |
23756 KB |
Output is correct |
25 |
Correct |
10 ms |
23792 KB |
Output is correct |
26 |
Correct |
11 ms |
23876 KB |
Output is correct |
27 |
Correct |
9 ms |
23948 KB |
Output is correct |
28 |
Correct |
10 ms |
23948 KB |
Output is correct |
29 |
Correct |
10 ms |
23896 KB |
Output is correct |
30 |
Correct |
11 ms |
23844 KB |
Output is correct |
31 |
Correct |
14 ms |
23896 KB |
Output is correct |
32 |
Correct |
10 ms |
23956 KB |
Output is correct |
33 |
Correct |
10 ms |
23900 KB |
Output is correct |
34 |
Correct |
13 ms |
24160 KB |
Output is correct |
35 |
Correct |
13 ms |
24156 KB |
Output is correct |
36 |
Correct |
15 ms |
24152 KB |
Output is correct |
37 |
Correct |
11 ms |
24112 KB |
Output is correct |
38 |
Correct |
11 ms |
24296 KB |
Output is correct |
39 |
Correct |
11 ms |
24152 KB |
Output is correct |
40 |
Correct |
13 ms |
24664 KB |
Output is correct |
41 |
Correct |
11 ms |
24664 KB |
Output is correct |
42 |
Correct |
11 ms |
24156 KB |
Output is correct |
43 |
Correct |
11 ms |
24412 KB |
Output is correct |
44 |
Correct |
11 ms |
24412 KB |
Output is correct |
45 |
Correct |
11 ms |
24352 KB |
Output is correct |
46 |
Correct |
11 ms |
24412 KB |
Output is correct |
47 |
Correct |
11 ms |
24416 KB |
Output is correct |
48 |
Correct |
12 ms |
24156 KB |
Output is correct |
49 |
Correct |
11 ms |
24152 KB |
Output is correct |
50 |
Correct |
12 ms |
24168 KB |
Output is correct |
51 |
Correct |
11 ms |
24152 KB |
Output is correct |
52 |
Correct |
11 ms |
24156 KB |
Output is correct |
53 |
Correct |
11 ms |
24152 KB |
Output is correct |
54 |
Correct |
11 ms |
24156 KB |
Output is correct |
55 |
Correct |
14 ms |
24668 KB |
Output is correct |
56 |
Correct |
29 ms |
27404 KB |
Output is correct |
57 |
Correct |
43 ms |
29776 KB |
Output is correct |
58 |
Correct |
61 ms |
31396 KB |
Output is correct |
59 |
Correct |
76 ms |
34000 KB |
Output is correct |
60 |
Correct |
91 ms |
36556 KB |
Output is correct |
61 |
Correct |
102 ms |
38604 KB |
Output is correct |
62 |
Correct |
114 ms |
40248 KB |
Output is correct |
63 |
Correct |
146 ms |
43208 KB |
Output is correct |
64 |
Correct |
147 ms |
43976 KB |
Output is correct |
65 |
Correct |
82 ms |
68688 KB |
Output is correct |
66 |
Correct |
82 ms |
68688 KB |
Output is correct |
67 |
Correct |
88 ms |
40520 KB |
Output is correct |
68 |
Correct |
110 ms |
55752 KB |
Output is correct |
69 |
Correct |
123 ms |
54212 KB |
Output is correct |
70 |
Correct |
120 ms |
53704 KB |
Output is correct |
71 |
Correct |
131 ms |
54464 KB |
Output is correct |
72 |
Correct |
128 ms |
55012 KB |
Output is correct |
73 |
Correct |
113 ms |
50880 KB |
Output is correct |
74 |
Correct |
114 ms |
51136 KB |
Output is correct |
75 |
Correct |
123 ms |
49620 KB |
Output is correct |
76 |
Correct |
129 ms |
49736 KB |
Output is correct |
77 |
Correct |
125 ms |
49440 KB |
Output is correct |
78 |
Correct |
123 ms |
47176 KB |
Output is correct |
79 |
Correct |
120 ms |
48324 KB |
Output is correct |