/**
____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N_MAX = 200002;
int n;
struct Cost
{
int u, d;
};
Cost cost[N_MAX];
int parent[N_MAX];
vector <int> sons[N_MAX];
bool operator < (const Cost &a, const Cost &b)
{
return a.u * b.d < b.u * a.d;
}
struct Node
{
int u;
Cost c;
};
bool operator < (const Node &u, const Node &v)
{
return make_pair(u.c, u) > make_pair(v.c, u);
}
set <Node> notJoined[N_MAX];
void mergeSets (set <Node>* in, set <Node>* from)
{
bool swapped = false;
if(in->size() < from->size())
{
swap(in, from);
swapped = true;
}
while(from->empty() == false)
{
in->insert(*from->begin());
from->erase(from->begin());
}
if(swapped == true)
swap(in, from);
}
int priority[N_MAX];
bool isRoot[N_MAX];
void dfs (int u)
{
for(int v : sons[u])
{
dfs(v);
notJoined[u].insert(Node{v, cost[v]});
}
isRoot[u] = true;
int curr = 0;
priority[u] = ++curr;
while(notJoined[u].empty() == false)
{
int v = notJoined[u].begin()->u;
if(cost[u] < cost[v])
break;
notJoined[u].erase(notJoined[u].begin());
isRoot[v] = false;
priority[v] = ++curr;
cost[u].u += cost[v].u;
cost[u].d += cost[v].d;
mergeSets(¬Joined[u], ¬Joined[v]);
}
}
int order[N_MAX];
int curr;
void solveOne (int u)
{
order[++curr] = u;
vector <pair <int, int> > aux;
for(int v : sons[u])
if(isRoot[v] == false)
aux.push_back(make_pair(priority[v], v));
sort(aux.begin(), aux.end());
for(pair <int, int> p : aux)
solveOne(p.second);
}
void solveAll (int u)
{
solveOne(u);
while(notJoined[u].empty() == false)
{
int v = notJoined[u].begin()->u;
notJoined[u].erase(notJoined[u].begin());
solveAll(v);
}
}
int scheduling_cost (vector <int> p, vector <int> u, vector <int> d)
{
n = p.size();
for(int i = 1; i <= n; i++)
{
parent[i] = p[i - 1] + 1;
cost[i] = Cost{u[i - 1], d[i - 1]};
}
for(int i = 2; i <= n; i++)
sons[parent[i]].push_back(i);
dfs(1);
solveAll(1);
int currTime = 0;
ll ans = 0;
for(int i = 1; i <= n; i++)
{
int j = order[i];
currTime += d[j - 1];
ans += 1LL * currTime * u[j - 1];
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector <int> p(n), u(n), d(n);
for(int i = 0; i < n; i++)
cin >> p[i];
for(int i = 0; i < n; i++)
cin >> u[i];
for(int i = 0; i < n; i++)
cin >> d[i];
cout << scheduling_cost(p, u, d) << "\n";
return 0;
}
Compilation message
/tmp/ccrhRu6m.o: In function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'
/tmp/ccYr1guI.o:job.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status