#include <bits/stdc++.h>
using namespace std;
using i64=long long;
#define UNUSED -1
#define all(x) x.begin(),x.end()
#define pb push_back
const int mod=1e9+7,inf=1e9+1;
const int N=3e5;
vector<pair<int,int>>g[N];
i64 best[N][2];
i64 rez[N];
void dfs(int nod)
{
if(g[nod].size()==0)
{
return;
}
multiset<i64>mp;
for(auto &c:g[nod])
{
dfs(c.first);
best[c.first][0]+=c.second;
best[c.first][1]+=c.second;
mp.insert(best[c.first][0]);
mp.insert(best[c.first][1]);
}
while(mp.size()>2)
{
mp.erase(mp.find(*mp.begin()));
mp.erase(mp.find(*mp.rbegin()));
}
best[nod][0]=*mp.begin();
best[nod][1]=*mp.rbegin();
i64 x=best[nod][0];
for(auto &c:g[nod])
{
rez[nod]+=rez[c.first];
rez[nod]+=max(x-best[c.first][1] , 0LL);
rez[nod]+=max(best[c.first][0]-x , 0LL);
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n,m;
cin>>n>>m;
n+=m;
for(int i=1;i<n;i++)
{
int x,y;
cin>>x>>y;
x--;
g[x].pb({i,y});
}
dfs(0);
cout<<rez[0];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7260 KB |
Output is correct |
2 |
Correct |
3 ms |
7516 KB |
Output is correct |
3 |
Correct |
3 ms |
7376 KB |
Output is correct |
4 |
Correct |
3 ms |
7304 KB |
Output is correct |
5 |
Correct |
3 ms |
7516 KB |
Output is correct |
6 |
Correct |
5 ms |
7508 KB |
Output is correct |
7 |
Correct |
3 ms |
7516 KB |
Output is correct |
8 |
Correct |
3 ms |
7516 KB |
Output is correct |
9 |
Correct |
3 ms |
7516 KB |
Output is correct |
10 |
Correct |
3 ms |
7508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7516 KB |
Output is correct |
2 |
Correct |
3 ms |
7260 KB |
Output is correct |
3 |
Incorrect |
4 ms |
7260 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7260 KB |
Output is correct |
2 |
Correct |
3 ms |
7516 KB |
Output is correct |
3 |
Correct |
3 ms |
7376 KB |
Output is correct |
4 |
Correct |
3 ms |
7304 KB |
Output is correct |
5 |
Correct |
3 ms |
7516 KB |
Output is correct |
6 |
Correct |
5 ms |
7508 KB |
Output is correct |
7 |
Correct |
3 ms |
7516 KB |
Output is correct |
8 |
Correct |
3 ms |
7516 KB |
Output is correct |
9 |
Correct |
3 ms |
7516 KB |
Output is correct |
10 |
Correct |
3 ms |
7508 KB |
Output is correct |
11 |
Correct |
5 ms |
7516 KB |
Output is correct |
12 |
Correct |
3 ms |
7260 KB |
Output is correct |
13 |
Incorrect |
4 ms |
7260 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7260 KB |
Output is correct |
2 |
Correct |
3 ms |
7516 KB |
Output is correct |
3 |
Correct |
3 ms |
7376 KB |
Output is correct |
4 |
Correct |
3 ms |
7304 KB |
Output is correct |
5 |
Correct |
3 ms |
7516 KB |
Output is correct |
6 |
Correct |
5 ms |
7508 KB |
Output is correct |
7 |
Correct |
3 ms |
7516 KB |
Output is correct |
8 |
Correct |
3 ms |
7516 KB |
Output is correct |
9 |
Correct |
3 ms |
7516 KB |
Output is correct |
10 |
Correct |
3 ms |
7508 KB |
Output is correct |
11 |
Correct |
5 ms |
7516 KB |
Output is correct |
12 |
Correct |
3 ms |
7260 KB |
Output is correct |
13 |
Incorrect |
4 ms |
7260 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |