#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define all(a) a.begin(),a.end()
#define sz(a) (int)(a.size())
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define MOD (ll)(1e9+7)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
struct slope{
multiset<ll>s;
ll m=0,c=0;
slope(){}
slope(ll _m,ll _c ,ll x){
m = _m;
c = _c;
s.insert(x);
s.insert(x);
}
void merge(const slope &t){
m+=t.m;
c+=t.c;
for(ll z:t.s)s.insert(z);
}
void upd(ll k){
while(m>1){
auto it = s.end();
it--;
m--;
c+=*it;
s.erase(it);
}
auto it = s.end();
it--;
ll a = *it+k;
s.erase(it);
it = s.end();
it--;
ll b = *it+k;
s.erase(it);
s.insert(a);s.insert(b);
c-=k;
}
};
const int MAXN = 3e5+1;
vii adj(MAXN);
vector<ll>w(MAXN);
vector<slope>dp(MAXN);
void dfs(int v){
if(!sz(adj[v]))dp[v] = slope(1,-w[v],w[v]);
for(int x:adj[v]){
dfs(x);
if(sz(adj[x]))dp[x].upd(w[x]);
}
for(int x:adj[v]){
if(sz(dp[x].s) > sz(dp[v].s)){
swap(dp[x].s,dp[v].s);
swap(dp[x].c,dp[v].c);
swap(dp[x].m,dp[v].m);
}
dp[v].merge(dp[x]);
}
/*
cout<<"node"<<" "<<v<<'\n';
cout<<"container :"<<'\n';
for(int x:dp[v].s)cout<<x<<" ";
cout<<'\n';
cout<<dp[v].m<<" "<<dp[v].c<<'\n';
* */
}
int main()
{
owo
int n,m;
cin>>n>>m;
for(int i=2;i<=n+m;i++){
int p;
cin>>p>>w[i];
adj[p].pb(i);
}
dfs(1);
while(dp[1].m >= 1){
auto it = dp[1].s.end();
it--;
dp[1].m--;
dp[1].c+=*it;
dp[1].s.erase(it);
}
cout<<dp[1].c<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
28492 KB |
Output is correct |
2 |
Correct |
16 ms |
28432 KB |
Output is correct |
3 |
Correct |
15 ms |
28508 KB |
Output is correct |
4 |
Correct |
15 ms |
28480 KB |
Output is correct |
5 |
Correct |
16 ms |
28404 KB |
Output is correct |
6 |
Correct |
16 ms |
28512 KB |
Output is correct |
7 |
Correct |
16 ms |
28528 KB |
Output is correct |
8 |
Correct |
17 ms |
28492 KB |
Output is correct |
9 |
Correct |
15 ms |
28492 KB |
Output is correct |
10 |
Correct |
15 ms |
28508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
28492 KB |
Output is correct |
2 |
Correct |
15 ms |
28504 KB |
Output is correct |
3 |
Correct |
15 ms |
28468 KB |
Output is correct |
4 |
Correct |
15 ms |
28492 KB |
Output is correct |
5 |
Correct |
16 ms |
28492 KB |
Output is correct |
6 |
Correct |
16 ms |
28492 KB |
Output is correct |
7 |
Correct |
15 ms |
28492 KB |
Output is correct |
8 |
Correct |
16 ms |
28492 KB |
Output is correct |
9 |
Correct |
16 ms |
28512 KB |
Output is correct |
10 |
Correct |
16 ms |
28492 KB |
Output is correct |
11 |
Correct |
16 ms |
28500 KB |
Output is correct |
12 |
Correct |
16 ms |
28504 KB |
Output is correct |
13 |
Correct |
16 ms |
28492 KB |
Output is correct |
14 |
Correct |
17 ms |
28484 KB |
Output is correct |
15 |
Correct |
16 ms |
28516 KB |
Output is correct |
16 |
Correct |
15 ms |
28504 KB |
Output is correct |
17 |
Correct |
15 ms |
28492 KB |
Output is correct |
18 |
Correct |
16 ms |
28492 KB |
Output is correct |
19 |
Correct |
16 ms |
28548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
28492 KB |
Output is correct |
2 |
Correct |
16 ms |
28432 KB |
Output is correct |
3 |
Correct |
15 ms |
28508 KB |
Output is correct |
4 |
Correct |
15 ms |
28480 KB |
Output is correct |
5 |
Correct |
16 ms |
28404 KB |
Output is correct |
6 |
Correct |
16 ms |
28512 KB |
Output is correct |
7 |
Correct |
16 ms |
28528 KB |
Output is correct |
8 |
Correct |
17 ms |
28492 KB |
Output is correct |
9 |
Correct |
15 ms |
28492 KB |
Output is correct |
10 |
Correct |
15 ms |
28508 KB |
Output is correct |
11 |
Correct |
15 ms |
28492 KB |
Output is correct |
12 |
Correct |
15 ms |
28504 KB |
Output is correct |
13 |
Correct |
15 ms |
28468 KB |
Output is correct |
14 |
Correct |
15 ms |
28492 KB |
Output is correct |
15 |
Correct |
16 ms |
28492 KB |
Output is correct |
16 |
Correct |
16 ms |
28492 KB |
Output is correct |
17 |
Correct |
15 ms |
28492 KB |
Output is correct |
18 |
Correct |
16 ms |
28492 KB |
Output is correct |
19 |
Correct |
16 ms |
28512 KB |
Output is correct |
20 |
Correct |
16 ms |
28492 KB |
Output is correct |
21 |
Correct |
16 ms |
28500 KB |
Output is correct |
22 |
Correct |
16 ms |
28504 KB |
Output is correct |
23 |
Correct |
16 ms |
28492 KB |
Output is correct |
24 |
Correct |
17 ms |
28484 KB |
Output is correct |
25 |
Correct |
16 ms |
28516 KB |
Output is correct |
26 |
Correct |
15 ms |
28504 KB |
Output is correct |
27 |
Correct |
15 ms |
28492 KB |
Output is correct |
28 |
Correct |
16 ms |
28492 KB |
Output is correct |
29 |
Correct |
16 ms |
28548 KB |
Output is correct |
30 |
Correct |
16 ms |
28492 KB |
Output is correct |
31 |
Correct |
16 ms |
28620 KB |
Output is correct |
32 |
Correct |
17 ms |
28644 KB |
Output is correct |
33 |
Correct |
17 ms |
28728 KB |
Output is correct |
34 |
Correct |
21 ms |
28736 KB |
Output is correct |
35 |
Correct |
18 ms |
28900 KB |
Output is correct |
36 |
Correct |
18 ms |
28876 KB |
Output is correct |
37 |
Correct |
19 ms |
28976 KB |
Output is correct |
38 |
Correct |
20 ms |
29004 KB |
Output is correct |
39 |
Correct |
20 ms |
29088 KB |
Output is correct |
40 |
Correct |
19 ms |
29548 KB |
Output is correct |
41 |
Correct |
18 ms |
29488 KB |
Output is correct |
42 |
Correct |
18 ms |
28748 KB |
Output is correct |
43 |
Correct |
19 ms |
29388 KB |
Output is correct |
44 |
Correct |
19 ms |
29244 KB |
Output is correct |
45 |
Correct |
19 ms |
29308 KB |
Output is correct |
46 |
Correct |
19 ms |
29468 KB |
Output is correct |
47 |
Correct |
20 ms |
29388 KB |
Output is correct |
48 |
Correct |
19 ms |
29292 KB |
Output is correct |
49 |
Correct |
19 ms |
29416 KB |
Output is correct |
50 |
Correct |
18 ms |
29296 KB |
Output is correct |
51 |
Correct |
19 ms |
29148 KB |
Output is correct |
52 |
Correct |
19 ms |
29260 KB |
Output is correct |
53 |
Correct |
19 ms |
29260 KB |
Output is correct |
54 |
Correct |
19 ms |
29408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
28492 KB |
Output is correct |
2 |
Correct |
16 ms |
28432 KB |
Output is correct |
3 |
Correct |
15 ms |
28508 KB |
Output is correct |
4 |
Correct |
15 ms |
28480 KB |
Output is correct |
5 |
Correct |
16 ms |
28404 KB |
Output is correct |
6 |
Correct |
16 ms |
28512 KB |
Output is correct |
7 |
Correct |
16 ms |
28528 KB |
Output is correct |
8 |
Correct |
17 ms |
28492 KB |
Output is correct |
9 |
Correct |
15 ms |
28492 KB |
Output is correct |
10 |
Correct |
15 ms |
28508 KB |
Output is correct |
11 |
Correct |
15 ms |
28492 KB |
Output is correct |
12 |
Correct |
15 ms |
28504 KB |
Output is correct |
13 |
Correct |
15 ms |
28468 KB |
Output is correct |
14 |
Correct |
15 ms |
28492 KB |
Output is correct |
15 |
Correct |
16 ms |
28492 KB |
Output is correct |
16 |
Correct |
16 ms |
28492 KB |
Output is correct |
17 |
Correct |
15 ms |
28492 KB |
Output is correct |
18 |
Correct |
16 ms |
28492 KB |
Output is correct |
19 |
Correct |
16 ms |
28512 KB |
Output is correct |
20 |
Correct |
16 ms |
28492 KB |
Output is correct |
21 |
Correct |
16 ms |
28500 KB |
Output is correct |
22 |
Correct |
16 ms |
28504 KB |
Output is correct |
23 |
Correct |
16 ms |
28492 KB |
Output is correct |
24 |
Correct |
17 ms |
28484 KB |
Output is correct |
25 |
Correct |
16 ms |
28516 KB |
Output is correct |
26 |
Correct |
15 ms |
28504 KB |
Output is correct |
27 |
Correct |
15 ms |
28492 KB |
Output is correct |
28 |
Correct |
16 ms |
28492 KB |
Output is correct |
29 |
Correct |
16 ms |
28548 KB |
Output is correct |
30 |
Correct |
16 ms |
28492 KB |
Output is correct |
31 |
Correct |
16 ms |
28620 KB |
Output is correct |
32 |
Correct |
17 ms |
28644 KB |
Output is correct |
33 |
Correct |
17 ms |
28728 KB |
Output is correct |
34 |
Correct |
21 ms |
28736 KB |
Output is correct |
35 |
Correct |
18 ms |
28900 KB |
Output is correct |
36 |
Correct |
18 ms |
28876 KB |
Output is correct |
37 |
Correct |
19 ms |
28976 KB |
Output is correct |
38 |
Correct |
20 ms |
29004 KB |
Output is correct |
39 |
Correct |
20 ms |
29088 KB |
Output is correct |
40 |
Correct |
19 ms |
29548 KB |
Output is correct |
41 |
Correct |
18 ms |
29488 KB |
Output is correct |
42 |
Correct |
18 ms |
28748 KB |
Output is correct |
43 |
Correct |
19 ms |
29388 KB |
Output is correct |
44 |
Correct |
19 ms |
29244 KB |
Output is correct |
45 |
Correct |
19 ms |
29308 KB |
Output is correct |
46 |
Correct |
19 ms |
29468 KB |
Output is correct |
47 |
Correct |
20 ms |
29388 KB |
Output is correct |
48 |
Correct |
19 ms |
29292 KB |
Output is correct |
49 |
Correct |
19 ms |
29416 KB |
Output is correct |
50 |
Correct |
18 ms |
29296 KB |
Output is correct |
51 |
Correct |
19 ms |
29148 KB |
Output is correct |
52 |
Correct |
19 ms |
29260 KB |
Output is correct |
53 |
Correct |
19 ms |
29260 KB |
Output is correct |
54 |
Correct |
19 ms |
29408 KB |
Output is correct |
55 |
Correct |
26 ms |
30032 KB |
Output is correct |
56 |
Correct |
59 ms |
34916 KB |
Output is correct |
57 |
Correct |
101 ms |
39800 KB |
Output is correct |
58 |
Correct |
129 ms |
43160 KB |
Output is correct |
59 |
Correct |
171 ms |
48104 KB |
Output is correct |
60 |
Correct |
221 ms |
53264 KB |
Output is correct |
61 |
Correct |
244 ms |
56516 KB |
Output is correct |
62 |
Correct |
299 ms |
59944 KB |
Output is correct |
63 |
Correct |
359 ms |
67012 KB |
Output is correct |
64 |
Correct |
391 ms |
67440 KB |
Output is correct |
65 |
Correct |
159 ms |
94404 KB |
Output is correct |
66 |
Correct |
157 ms |
94348 KB |
Output is correct |
67 |
Correct |
162 ms |
43076 KB |
Output is correct |
68 |
Correct |
248 ms |
84796 KB |
Output is correct |
69 |
Correct |
308 ms |
80816 KB |
Output is correct |
70 |
Correct |
318 ms |
80892 KB |
Output is correct |
71 |
Correct |
369 ms |
103492 KB |
Output is correct |
72 |
Correct |
345 ms |
102996 KB |
Output is correct |
73 |
Correct |
375 ms |
90264 KB |
Output is correct |
74 |
Correct |
362 ms |
90156 KB |
Output is correct |
75 |
Correct |
363 ms |
89600 KB |
Output is correct |
76 |
Correct |
369 ms |
89844 KB |
Output is correct |
77 |
Correct |
363 ms |
87860 KB |
Output is correct |
78 |
Correct |
377 ms |
84956 KB |
Output is correct |
79 |
Correct |
398 ms |
89540 KB |
Output is correct |