#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();
while(dp[x].m>1)dp[x].m--,dp[x].c+=dp[x].point.top(),dp[x].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;
//shifting
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];
}
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:66:23: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
66 | void dfs(int cur,int p){
| ^
fireworks.cpp:78:14: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
78 | 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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23900 KB |
Output is correct |
2 |
Incorrect |
10 ms |
24152 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23900 KB |
Output is correct |
2 |
Correct |
10 ms |
23900 KB |
Output is correct |
3 |
Incorrect |
10 ms |
23852 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23900 KB |
Output is correct |
2 |
Incorrect |
10 ms |
24152 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23900 KB |
Output is correct |
2 |
Incorrect |
10 ms |
24152 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |