#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class FiringEmployees { public:
	int fire(vector <int> manager, vector <int> salary, vector <int> productivity)
	{
		const int N = manager.size()+1;

		vector<vector<int>> children(N);
		for(int i=1; i<N; ++i)
			children[manager[i-1]].push_back(i);

		vector<int> best(N);
		for(int i=N-1; i>=0; --i) {
			int total = (i ? productivity[i-1] - salary[i-1] : 0);
			for(int u: children[i])
				total += best[u];
			if(total > 0)
				best[i] = total;
		}
		return best[0];
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, FiringEmployees().fire(manager, salary, productivity));}
int main(){

CASE(0)
	int manager_[] = {0,0,0};
	  vector <int> manager(manager_, manager_+sizeof(manager_)/sizeof(*manager_)); 
	int salary_[] = {1,2,3};
	  vector <int> salary(salary_, salary_+sizeof(salary_)/sizeof(*salary_)); 
	int productivity_[] = {3,2,1};
	  vector <int> productivity(productivity_, productivity_+sizeof(productivity_)/sizeof(*productivity_)); 
	int _ = 2; 
END
CASE(1)
	int manager_[] = {0,1,2,3};
	  vector <int> manager(manager_, manager_+sizeof(manager_)/sizeof(*manager_)); 
	int salary_[] = {4,3,2,1};
	  vector <int> salary(salary_, salary_+sizeof(salary_)/sizeof(*salary_)); 
	int productivity_[] = {2,3,4,5};
	  vector <int> productivity(productivity_, productivity_+sizeof(productivity_)/sizeof(*productivity_)); 
	int _ = 4; 
END
CASE(2)
	int manager_[] = {0,1};
	  vector <int> manager(manager_, manager_+sizeof(manager_)/sizeof(*manager_)); 
	int salary_[] = {1,10};
	  vector <int> salary(salary_, salary_+sizeof(salary_)/sizeof(*salary_)); 
	int productivity_[] = {5,5};
	  vector <int> productivity(productivity_, productivity_+sizeof(productivity_)/sizeof(*productivity_)); 
	int _ = 4; 
END
CASE(3)
	int manager_[] = {0,1,2,1,2,3,4,2,3};
	  vector <int> manager(manager_, manager_+sizeof(manager_)/sizeof(*manager_)); 
	int salary_[] = {5,3,6,8,4,2,4,6,7};
	  vector <int> salary(salary_, salary_+sizeof(salary_)/sizeof(*salary_)); 
	int productivity_[] = {2,5,7,8,5,3,5,7,9};
	  vector <int> productivity(productivity_, productivity_+sizeof(productivity_)/sizeof(*productivity_)); 
	int _ = 6; 
END
CASE(4)
	int manager_[] = {0,0,1,1,2,2};
	  vector <int> manager(manager_, manager_+sizeof(manager_)/sizeof(*manager_)); 
	int salary_[] = {1,1,1,2,2,2};
	  vector <int> salary(salary_, salary_+sizeof(salary_)/sizeof(*salary_)); 
	int productivity_[] = {2,2,2,1,1,1};
	  vector <int> productivity(productivity_, productivity_+sizeof(productivity_)/sizeof(*productivity_)); 
	int _ = 3; 
END
/*
CASE(5)
	int manager_[] = ;
	  vector <int> manager(manager_, manager_+sizeof(manager_)/sizeof(*manager_)); 
	int salary_[] = ;
	  vector <int> salary(salary_, salary_+sizeof(salary_)/sizeof(*salary_)); 
	int productivity_[] = ;
	  vector <int> productivity(productivity_, productivity_+sizeof(productivity_)/sizeof(*productivity_)); 
	int _ = ; 
END
CASE(6)
	int manager_[] = ;
	  vector <int> manager(manager_, manager_+sizeof(manager_)/sizeof(*manager_)); 
	int salary_[] = ;
	  vector <int> salary(salary_, salary_+sizeof(salary_)/sizeof(*salary_)); 
	int productivity_[] = ;
	  vector <int> productivity(productivity_, productivity_+sizeof(productivity_)/sizeof(*productivity_)); 
	int _ = ; 
END
*/
}
// END CUT HERE
