/*
not mine submission :((((
taken from: https://usaco.guide/problem-solutions/
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std; using namespace __gnu_pbds;
//#pragma GCC optimize("Ofast") //#pragma GCC optimize "unroll-loops" //#pragma GCC optimize "prefetch-loop-arrays" //#pragma GCC target "sse,sse2,sse3,sse4,abm,avx,aes,sse4a,sse4.1,sse4.2,mmx,popcnt,tune=native"
#define foru(i,a,b) for(int i=(a);i<(b);i++)
#define ford(i,a,b) for(int i=(a);i>=(b);i--)
#define fori(a,b) foru(i,a,b)
#define forj(a,b) foru(j,a,b)
#define fork(a,b) foru(k,a,b)
#define seto(x,i) memset(x,i,sizeof x)
#define pf first
#define ps second
#define pb push_back
#define eb emplace_back
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll
#define clz __builtin_clz
#define clzll __builtin_clzll
#define ctz __builtin_ctz
#define ctzll __builtin_ctzll
#define P2(x) (1LL<<(x))
#define sz(x) (int)x.size()
#define all(x) begin(x),end(x)
#if __SIZEOF_INT128__
typedef __int128_t i128; typedef __uint128_t ui128;
#else
typedef long long i128; typedef unsigned long long ui128;
#endif
typedef int64_t ll; typedef uint64_t ull; typedef int8_t byte; typedef long double lld;
typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef pair<lld,lld> pdd;
template<typename T1,typename T2> using ordered_map=tree<T1,T2,less<int>,rb_tree_tag,tree_order_statistics_node_update>;
template<typename T1> using ordered_set=ordered_map<T1,null_type>;
const int inf=0x3f3f3f3f,MOD=1e9+7; const ll INF=0x3f3f3f3f3f3f3f3f; const lld PI=acos((lld)-1);
ll gcd(ll a, ll b){return !b?a:gcd(b,a%b);}
ll fpow(ll a,ll b){if(!b) return 1; ll t=fpow(a,b/2); if(b&1) return t*t%MOD*a%MOD; return t*t%MOD;}
const ll SEED=chrono::duration_cast<chrono::nanoseconds>(chrono::high_resolution_clock::now().time_since_epoch()).count();
mt19937 randgen(SEED); int randint(int a, int b){return uniform_int_distribution<int>(a,b)(randgen);} ll randll(ll a, ll b){return uniform_int_distribution<ll>(a,b)(randgen);}
template<class T1,class T2>constexpr const auto _min(const T1&x,const T2&y){return x<y?x:y;} template<class T,class...Ts>constexpr auto _min(const T&x,const Ts&...xs){return _min(x,_min(xs...));}
template<class T1,class T2>constexpr const auto _max(const T1&x,const T2&y){return x>y?x:y;} template<class T,class...Ts>constexpr auto _max(const T&x,const Ts&...xs){return _max(x,_max(xs...));}
#define min(...) _min(__VA_ARGS__)
#define max(...) _max(__VA_ARGS__)
struct pair_hash{template<class T1,class T2> size_t operator()(const pair<T1,T2>&pair)const{return 31*hash<T1>{}(pair.first)+hash<T2>{}(pair.second);}};
struct chash{int operator()(ll x) const { x+=0x9e3779b97f4a7c15; x=(x^(x>>30))*0xbf58476d1ce4e5b9; x=(x^(x>>27))*0x94d049bb133111eb; return x^(x>>31)+SEED;}};
#define nl endl
const int N=300010,M=MOD;
ll n,m,a,b,pl[N],rt[N],ans;
vector<int> gr[N];
priority_queue<ll> poi[N];
void dfs(int v){
rt[v]=v;
if(v>n){
poi[v].push(pl[v]); poi[v].push(pl[v]);
return;
}
for(auto i:gr[v]){
dfs(i);
if(sz(poi[rt[i]])>sz(poi[rt[v]])) rt[v]=rt[i];
}
for(auto i:gr[v]) if(rt[i]!=rt[v]){
while(sz(poi[rt[i]])){
poi[rt[v]].push(poi[rt[i]].top()); poi[rt[i]].pop();
}
}
fori(1,sz(gr[v])) poi[rt[v]].pop();
a=poi[rt[v]].top(); poi[rt[v]].pop(); b=poi[rt[v]].top(); poi[rt[v]].pop();
a+=pl[v]; b+=pl[v]; poi[rt[v]].push(b); poi[rt[v]].push(a);
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
//freopen("3_01.in", "r", stdin); //freopen("", "w", stdout);
cin>>n>>m; m+=n;
fori(2,m+1){
cin>>a>>b;
gr[a].pb(i);
pl[i]=b; ans+=b;
}
dfs(1);
poi[rt[1]].pop(); a=poi[rt[1]].top(); poi[rt[1]].push(0); b=0;
while(sz(poi[rt[1]])){
ans-=(a-poi[rt[1]].top())*b++;
a=poi[rt[1]].top(); poi[rt[1]].pop();
}
cout<<ans;
return 0;
}
/**
*/
Compilation message
fireworks.cpp: In member function 'int chash::operator()(ll) const':
fireworks.cpp:49:150: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
49 | struct chash{int operator()(ll x) const { x+=0x9e3779b97f4a7c15; x=(x^(x>>30))*0xbf58476d1ce4e5b9; x=(x^(x>>27))*0x94d049bb133111eb; return x^(x>>31)+SEED;}};
| ~~~~~~~^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
16716 KB |
Output is correct |
2 |
Correct |
13 ms |
16772 KB |
Output is correct |
3 |
Correct |
13 ms |
16716 KB |
Output is correct |
4 |
Correct |
11 ms |
16716 KB |
Output is correct |
5 |
Correct |
13 ms |
16716 KB |
Output is correct |
6 |
Correct |
11 ms |
16720 KB |
Output is correct |
7 |
Correct |
11 ms |
16768 KB |
Output is correct |
8 |
Correct |
14 ms |
16736 KB |
Output is correct |
9 |
Correct |
11 ms |
16708 KB |
Output is correct |
10 |
Correct |
13 ms |
16664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
16664 KB |
Output is correct |
2 |
Correct |
13 ms |
16716 KB |
Output is correct |
3 |
Correct |
16 ms |
16716 KB |
Output is correct |
4 |
Correct |
13 ms |
16784 KB |
Output is correct |
5 |
Correct |
13 ms |
16716 KB |
Output is correct |
6 |
Correct |
15 ms |
16736 KB |
Output is correct |
7 |
Correct |
11 ms |
16716 KB |
Output is correct |
8 |
Correct |
11 ms |
16776 KB |
Output is correct |
9 |
Correct |
11 ms |
16780 KB |
Output is correct |
10 |
Correct |
11 ms |
16716 KB |
Output is correct |
11 |
Correct |
11 ms |
16780 KB |
Output is correct |
12 |
Correct |
14 ms |
16716 KB |
Output is correct |
13 |
Correct |
14 ms |
16716 KB |
Output is correct |
14 |
Correct |
14 ms |
16752 KB |
Output is correct |
15 |
Correct |
16 ms |
16716 KB |
Output is correct |
16 |
Correct |
12 ms |
16676 KB |
Output is correct |
17 |
Correct |
14 ms |
16792 KB |
Output is correct |
18 |
Correct |
13 ms |
16780 KB |
Output is correct |
19 |
Correct |
14 ms |
16716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
16716 KB |
Output is correct |
2 |
Correct |
13 ms |
16772 KB |
Output is correct |
3 |
Correct |
13 ms |
16716 KB |
Output is correct |
4 |
Correct |
11 ms |
16716 KB |
Output is correct |
5 |
Correct |
13 ms |
16716 KB |
Output is correct |
6 |
Correct |
11 ms |
16720 KB |
Output is correct |
7 |
Correct |
11 ms |
16768 KB |
Output is correct |
8 |
Correct |
14 ms |
16736 KB |
Output is correct |
9 |
Correct |
11 ms |
16708 KB |
Output is correct |
10 |
Correct |
13 ms |
16664 KB |
Output is correct |
11 |
Correct |
14 ms |
16664 KB |
Output is correct |
12 |
Correct |
13 ms |
16716 KB |
Output is correct |
13 |
Correct |
16 ms |
16716 KB |
Output is correct |
14 |
Correct |
13 ms |
16784 KB |
Output is correct |
15 |
Correct |
13 ms |
16716 KB |
Output is correct |
16 |
Correct |
15 ms |
16736 KB |
Output is correct |
17 |
Correct |
11 ms |
16716 KB |
Output is correct |
18 |
Correct |
11 ms |
16776 KB |
Output is correct |
19 |
Correct |
11 ms |
16780 KB |
Output is correct |
20 |
Correct |
11 ms |
16716 KB |
Output is correct |
21 |
Correct |
11 ms |
16780 KB |
Output is correct |
22 |
Correct |
14 ms |
16716 KB |
Output is correct |
23 |
Correct |
14 ms |
16716 KB |
Output is correct |
24 |
Correct |
14 ms |
16752 KB |
Output is correct |
25 |
Correct |
16 ms |
16716 KB |
Output is correct |
26 |
Correct |
12 ms |
16676 KB |
Output is correct |
27 |
Correct |
14 ms |
16792 KB |
Output is correct |
28 |
Correct |
13 ms |
16780 KB |
Output is correct |
29 |
Correct |
14 ms |
16716 KB |
Output is correct |
30 |
Correct |
13 ms |
16716 KB |
Output is correct |
31 |
Correct |
12 ms |
16808 KB |
Output is correct |
32 |
Correct |
12 ms |
16844 KB |
Output is correct |
33 |
Correct |
13 ms |
16844 KB |
Output is correct |
34 |
Correct |
13 ms |
16956 KB |
Output is correct |
35 |
Correct |
15 ms |
17036 KB |
Output is correct |
36 |
Correct |
18 ms |
17100 KB |
Output is correct |
37 |
Correct |
15 ms |
17100 KB |
Output is correct |
38 |
Correct |
17 ms |
17100 KB |
Output is correct |
39 |
Correct |
14 ms |
17100 KB |
Output is correct |
40 |
Correct |
16 ms |
17536 KB |
Output is correct |
41 |
Correct |
14 ms |
17544 KB |
Output is correct |
42 |
Correct |
14 ms |
16972 KB |
Output is correct |
43 |
Correct |
14 ms |
17328 KB |
Output is correct |
44 |
Correct |
15 ms |
17232 KB |
Output is correct |
45 |
Correct |
16 ms |
17288 KB |
Output is correct |
46 |
Correct |
18 ms |
17228 KB |
Output is correct |
47 |
Correct |
16 ms |
17284 KB |
Output is correct |
48 |
Correct |
15 ms |
17100 KB |
Output is correct |
49 |
Correct |
15 ms |
17160 KB |
Output is correct |
50 |
Correct |
16 ms |
17104 KB |
Output is correct |
51 |
Correct |
14 ms |
17128 KB |
Output is correct |
52 |
Correct |
14 ms |
17100 KB |
Output is correct |
53 |
Correct |
15 ms |
17228 KB |
Output is correct |
54 |
Correct |
14 ms |
17204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
16716 KB |
Output is correct |
2 |
Correct |
13 ms |
16772 KB |
Output is correct |
3 |
Correct |
13 ms |
16716 KB |
Output is correct |
4 |
Correct |
11 ms |
16716 KB |
Output is correct |
5 |
Correct |
13 ms |
16716 KB |
Output is correct |
6 |
Correct |
11 ms |
16720 KB |
Output is correct |
7 |
Correct |
11 ms |
16768 KB |
Output is correct |
8 |
Correct |
14 ms |
16736 KB |
Output is correct |
9 |
Correct |
11 ms |
16708 KB |
Output is correct |
10 |
Correct |
13 ms |
16664 KB |
Output is correct |
11 |
Correct |
14 ms |
16664 KB |
Output is correct |
12 |
Correct |
13 ms |
16716 KB |
Output is correct |
13 |
Correct |
16 ms |
16716 KB |
Output is correct |
14 |
Correct |
13 ms |
16784 KB |
Output is correct |
15 |
Correct |
13 ms |
16716 KB |
Output is correct |
16 |
Correct |
15 ms |
16736 KB |
Output is correct |
17 |
Correct |
11 ms |
16716 KB |
Output is correct |
18 |
Correct |
11 ms |
16776 KB |
Output is correct |
19 |
Correct |
11 ms |
16780 KB |
Output is correct |
20 |
Correct |
11 ms |
16716 KB |
Output is correct |
21 |
Correct |
11 ms |
16780 KB |
Output is correct |
22 |
Correct |
14 ms |
16716 KB |
Output is correct |
23 |
Correct |
14 ms |
16716 KB |
Output is correct |
24 |
Correct |
14 ms |
16752 KB |
Output is correct |
25 |
Correct |
16 ms |
16716 KB |
Output is correct |
26 |
Correct |
12 ms |
16676 KB |
Output is correct |
27 |
Correct |
14 ms |
16792 KB |
Output is correct |
28 |
Correct |
13 ms |
16780 KB |
Output is correct |
29 |
Correct |
14 ms |
16716 KB |
Output is correct |
30 |
Correct |
13 ms |
16716 KB |
Output is correct |
31 |
Correct |
12 ms |
16808 KB |
Output is correct |
32 |
Correct |
12 ms |
16844 KB |
Output is correct |
33 |
Correct |
13 ms |
16844 KB |
Output is correct |
34 |
Correct |
13 ms |
16956 KB |
Output is correct |
35 |
Correct |
15 ms |
17036 KB |
Output is correct |
36 |
Correct |
18 ms |
17100 KB |
Output is correct |
37 |
Correct |
15 ms |
17100 KB |
Output is correct |
38 |
Correct |
17 ms |
17100 KB |
Output is correct |
39 |
Correct |
14 ms |
17100 KB |
Output is correct |
40 |
Correct |
16 ms |
17536 KB |
Output is correct |
41 |
Correct |
14 ms |
17544 KB |
Output is correct |
42 |
Correct |
14 ms |
16972 KB |
Output is correct |
43 |
Correct |
14 ms |
17328 KB |
Output is correct |
44 |
Correct |
15 ms |
17232 KB |
Output is correct |
45 |
Correct |
16 ms |
17288 KB |
Output is correct |
46 |
Correct |
18 ms |
17228 KB |
Output is correct |
47 |
Correct |
16 ms |
17284 KB |
Output is correct |
48 |
Correct |
15 ms |
17100 KB |
Output is correct |
49 |
Correct |
15 ms |
17160 KB |
Output is correct |
50 |
Correct |
16 ms |
17104 KB |
Output is correct |
51 |
Correct |
14 ms |
17128 KB |
Output is correct |
52 |
Correct |
14 ms |
17100 KB |
Output is correct |
53 |
Correct |
15 ms |
17228 KB |
Output is correct |
54 |
Correct |
14 ms |
17204 KB |
Output is correct |
55 |
Correct |
23 ms |
17804 KB |
Output is correct |
56 |
Correct |
59 ms |
20508 KB |
Output is correct |
57 |
Correct |
94 ms |
23256 KB |
Output is correct |
58 |
Correct |
113 ms |
25104 KB |
Output is correct |
59 |
Correct |
149 ms |
27840 KB |
Output is correct |
60 |
Correct |
173 ms |
30656 KB |
Output is correct |
61 |
Correct |
196 ms |
32944 KB |
Output is correct |
62 |
Correct |
233 ms |
34504 KB |
Output is correct |
63 |
Correct |
335 ms |
37756 KB |
Output is correct |
64 |
Correct |
321 ms |
38768 KB |
Output is correct |
65 |
Correct |
151 ms |
68548 KB |
Output is correct |
66 |
Correct |
147 ms |
68548 KB |
Output is correct |
67 |
Correct |
165 ms |
35800 KB |
Output is correct |
68 |
Correct |
229 ms |
53328 KB |
Output is correct |
69 |
Correct |
265 ms |
50548 KB |
Output is correct |
70 |
Correct |
274 ms |
50584 KB |
Output is correct |
71 |
Correct |
266 ms |
49680 KB |
Output is correct |
72 |
Correct |
267 ms |
49776 KB |
Output is correct |
73 |
Correct |
234 ms |
46516 KB |
Output is correct |
74 |
Correct |
283 ms |
46640 KB |
Output is correct |
75 |
Correct |
254 ms |
45464 KB |
Output is correct |
76 |
Correct |
227 ms |
45428 KB |
Output is correct |
77 |
Correct |
246 ms |
44344 KB |
Output is correct |
78 |
Correct |
267 ms |
41780 KB |
Output is correct |
79 |
Correct |
224 ms |
43744 KB |
Output is correct |