stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <bits/stdc++.h>





using namespace std;



typedef long long ll;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;



const int MAXN = 100 * 1000 + 123;











ll n, x, y;





int main() {

	{

		ll tmp, a, b;

		cin >> n;

		cin >> a >> b;

		x = a / __gcd(a, b);

		y = b / __gcd(a, b);

		

		for (int i = 1; i < n; i++) {

			cin >> a >> b;

			tmp = max((x + a - 1) / a, (y + b - 1) / b);

			x = tmp * a, y = tmp * b;

		}

	}

	cout << x + y;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.214183079834584x):
#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<algorithm>

using namespace std;

#define maxn 1005

#define INF ((ll)(1e18)+1)

typedef long long ll;

int T[maxn], A[maxn];

int main(void)

{

	int n;

	scanf("%d", &n);

	for (int i = 0; i < n; i++)

		scanf("%d %d", &T[i], &A[i]);

	ll t = T[0], a = A[0];

	for (int i = 1; i < n; i++)

	{

		if (T[i] >= t && A[i] >= a) {

			t = T[i], a = A[i];

			continue;

		}

		ll k = max(t / T[i], a / A[i]);

		if (k < 1)k = 1;

		while (T[i] * k < t || A[i] * k < a)

			k++;

		t = T[i] * k, a = A[i] * k;

	}

	printf("%lld\n", t + a);

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

#define inf 0x3f3f3f3f

#define ll long long

const int N=200005;

const int mod=1e9+7;

const double eps=1e-8;

const double PI = acos(-1.0);

#define lowbit(x) (x&(-x))

ll ceils(ll x,ll y)

{

    if(x%y==0)

        return x/y;

    return x/y+1;

}

int main()

{

    std::ios::sync_with_stdio(false);

    int n;

    ll a,b;

    cin>>n>>a>>b;

    for(int i=1;i<n;i++)

    {

        ll x,y;

        cin>>x>>y;

        ll t=max(ceils(a,x),ceils(b,y));

        a=x*t,b=y*t;

    }

    cout<<a+b<<endl;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.300146316674844x):
#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<algorithm>

using namespace std;

#define maxn 1005

#define INF ((ll)(1e18)+1)

typedef long long ll;

int T[maxn], A[maxn];

int main(void)

{

	int n;

	scanf("%d", &n);

	for (int i = 0; i < n; i++)

		scanf("%d %d", &T[i], &A[i]);

	ll t = T[0], a = A[0];

	for (int i = 1; i < n; i++)

	{

		if (T[i] >= t && A[i] >= a) {

			t = T[i], a = A[i];

			continue;

		}

		ll k = max(t / T[i], a / A[i]);

		if (k < 1)k = 1;

		while (T[i] * k < t || A[i] * k < a)

			k++;

		t = T[i] * k, a = A[i] * k;

	}

	printf("%lld\n", t + a);

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



int main() {

    cin.tie(NULL);

    ios::sync_with_stdio(false);

    int n;

    cin>>n;

    long long int la=0,lb=0;

    for(int i=0;i<n;i++){

        long long int a,b;

        cin>>a>>b;

        if(a<la||b<lb){

            long long int da=la/a;

            long long int db=lb/b;

            if(la%a)da++;

            if(lb%b)db++;

            a*=max(da,db);

            b*=max(da,db);

        }

        la=a;

        lb=b;

    }

    cout<<la+lb;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.295530478259853x):
#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<algorithm>

using namespace std;

#define maxn 1005

#define INF ((ll)(1e18)+1)

typedef long long ll;

int T[maxn], A[maxn];

int main(void)

{

	int n;

	scanf("%d", &n);

	for (int i = 0; i < n; i++)

		scanf("%d %d", &T[i], &A[i]);

	ll t = T[0], a = A[0];

	for (int i = 1; i < n; i++)

	{

		if (T[i] >= t && A[i] >= a) {

			t = T[i], a = A[i];

			continue;

		}

		ll k = max(t / T[i], a / A[i]);

		if (k < 1)k = 1;

		while (T[i] * k < t || A[i] * k < a)

			k++;

		t = T[i] * k, a = A[i] * k;

	}

	printf("%lld\n", t + a);

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

#define n 500000

#define mem(arr) memset(arr, 0, sizeof(arr))

using namespace std;

typedef long long lli;

typedef double llf;

typedef pair<int, int> P;

void solve();

int main()

{

	ios_base::sync_with_stdio(0);

	cin.tie(0); cout.tie(0);

	int times = 1;

//	cin >> times;

	for(int i=0; i<times; i++)

	{

		solve();

		cout << '\n';

	}

	return 0;

}



int a, b, c, d, e, f, g, h;

//int arr[n + 50], arr1[n + 50], arr2[n + 50];



//type here



void solve()

{

	cin >> a;

	pair<lli, lli> arr[a + 50];

	for(int i=0; i<a; i++)

		cin >> arr[i].first >> arr[i].second;

	for(int i=1; i<a; i++)

	{

		if(arr[i].first >= arr[i - 1].first && arr[i].second >= arr[i - 1].second) continue;

		lli one = arr[i - 1].first / arr[i].first, two = arr[i - 1].second / arr[i].second;

		if(arr[i - 1].first % arr[i].first != 0) one ++;

		if(arr[i - 1].second % arr[i].second != 0) two ++;

		lli res = max(one, two);

		arr[i].first *= res; arr[i].second *= res;

	}

	lli ans = arr[a - 1].first + arr[a - 1].second;

	cout << ans;

}

// Faster C++ solution for the same problem (measured speedup: 5.303028842784047x):
#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<algorithm>

using namespace std;

#define maxn 1005

#define INF ((ll)(1e18)+1)

typedef long long ll;

int T[maxn], A[maxn];

int main(void)

{

	int n;

	scanf("%d", &n);

	for (int i = 0; i < n; i++)

		scanf("%d %d", &T[i], &A[i]);

	ll t = T[0], a = A[0];

	for (int i = 1; i < n; i++)

	{

		if (T[i] >= t && A[i] >= a) {

			t = T[i], a = A[i];

			continue;

		}

		ll k = max(t / T[i], a / A[i]);

		if (k < 1)k = 1;

		while (T[i] * k < t || A[i] * k < a)

			k++;

		t = T[i] * k, a = A[i] * k;

	}

	printf("%lld\n", t + a);

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

int main(){

	ios::sync_with_stdio(false);

	int n,a,b;

	long long x=1,y=1;

	long long mid;

	cin>>n;

	for(int i=0;i<n;i++){

		cin>>a>>b;

		mid=max(x/a,y/b);

		while(mid*a<x || mid*b<y)

			mid++;

		x=mid*a;

		y=mid*b;	

	}

	cout<<x+y<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.286652181023808x):
#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<algorithm>

using namespace std;

#define maxn 1005

#define INF ((ll)(1e18)+1)

typedef long long ll;

int T[maxn], A[maxn];

int main(void)

{

	int n;

	scanf("%d", &n);

	for (int i = 0; i < n; i++)

		scanf("%d %d", &T[i], &A[i]);

	ll t = T[0], a = A[0];

	for (int i = 1; i < n; i++)

	{

		if (T[i] >= t && A[i] >= a) {

			t = T[i], a = A[i];

			continue;

		}

		ll k = max(t / T[i], a / A[i]);

		if (k < 1)k = 1;

		while (T[i] * k < t || A[i] * k < a)

			k++;

		t = T[i] * k, a = A[i] * k;

	}

	printf("%lld\n", t + a);

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <cstdio>

#include <cstring>

#include <algorithm>

#include <cmath>

#include <queue>

#include <stack>

#include <map>

#include <set>

#include <vector>

#include <iomanip>

typedef long long ll;

using namespace std;

ll CEIL(ll a, ll b)

{

    ll res = a / b;

    if (a % b)

    {

        res++;

    }

    return res;

}

int main()

{

    ll a, b;

    ll n;

    cin >> n;

    cin >> a >> b;

    n--;

    ll x, y;

    for(int i=1;i<=n;i++)

    {

        cin >> x >> y;

        ll num = max(CEIL(a, x), CEIL(b, y));

        a = num * x;

        b = num * y;

    }

    cout << a + b << endl;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.221814326768066x):
#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<algorithm>

using namespace std;

#define maxn 1005

#define INF ((ll)(1e18)+1)

typedef long long ll;

int T[maxn], A[maxn];

int main(void)

{

	int n;

	scanf("%d", &n);

	for (int i = 0; i < n; i++)

		scanf("%d %d", &T[i], &A[i]);

	ll t = T[0], a = A[0];

	for (int i = 1; i < n; i++)

	{

		if (T[i] >= t && A[i] >= a) {

			t = T[i], a = A[i];

			continue;

		}

		ll k = max(t / T[i], a / A[i]);

		if (k < 1)k = 1;

		while (T[i] * k < t || A[i] * k < a)

			k++;

		t = T[i] * k, a = A[i] * k;

	}

	printf("%lld\n", t + a);

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

int64_t MAXN = pow(10, 5) + 10;

int64_t N, first = 0;

vector<int64_t> A(MAXN);



int main()

{

  cin >> N;

  for (int i = 0; i < N; i++)

  {

    cin >> A[i];

    if (A[i] % 2)

      first = 1;

  }

  cout << (first ? "first" : "second") << endl;

}


// Faster C++ solution for the same problem (measured speedup: 1.8133063919626322x):
#include <bits/stdc++.h>

using namespace std;



int main()

{

  int64_t N, a, odds = 0;

  cin >> N;

  for (int i = 0; i < N; i++)

  {

    cin >> a;

    if (a % 2)

      odds++;

  }

  cout << (odds == 0 ? "second" : "first") << endl;

}
// Slower C++ solution:
#include<bits/stdc++.h>



using namespace std;



#define int long long



int n,x,dem;



main()

{

    cin>>n;

    for(int i=1;i<=n;i++)

    {

        cin>>x;

        if(x%2==1) dem=1;

    }

    if(dem==0) cout<<"second";

    else cout<<"first";

}


// Faster C++ solution for the same problem (measured speedup: 5.293933457247918x):
#include<cstdio>

int n;

int a[100100];

int main()

{

	scanf("%d", &n);

	bool flag = false;

	for (int i = 1; i <= n; i++)

	{

		scanf("%d", &a[i]);

		if (a[i] & 1) flag = true;

	}

	if (flag) printf("first\n");

	else printf("second\n");

	return 0;

}
// Slower C++ solution:
#include <cstdio>

#include <vector>

#include <bitset>

using namespace std;



int main(){

	bitset<1121> isprime;

	isprime.set();

	isprime.reset(0);

	isprime.reset(1);

	

	for(int i = 2; i * i <= 1120; ++i){

		if( isprime[i] ){

			for(int j = i * i; j <= 1120; j += i){

				isprime.reset(j);

			}

		}

	}

	

	vector<int> prime;

	prime.reserve(200);

	for(int i = 2; i < 1120; ++i){

		if( isprime[i] ){

			prime.push_back(i);

		}

	}



	prime.push_back(1e9);



	vector<int>::iterator it;



	int n, k;

	while( scanf("%d%d", &n, &k), n != 0 ){

		vector<vector<int> > dp(k + 1, vector<int>(n + 1) );

		dp[0][0] = 1;



		for(it = prime.begin(); *it <= n; ++it){

			for(int j = n - *it; j >= 0; --j){

				for(int i = 0; i < k; ++i){

					dp[i + 1][j + *it] += dp[i][j];

				}

			}

		}

		

		printf("%d\n", dp[k][n]);

	}

}

// Faster C++ solution for the same problem (measured speedup: 2.7523840328481533x):
#include <cstdio>

#include <vector>

using namespace std;



bool isnp[1121];

int dp[15][1121];



int main(){

	for(int i = 2; i < 40; ++i){

		if( !isnp[i] ){

			for(int j = i * i; j <= 1120; j += i){

				isnp[j] = true;

			}

		}

	}



	dp[0][0] = 1;

	for(int i = 2; i < 1120; ++i){

		if( !isnp[i] ){

			for(int k = 14; k > 0; --k){

				for(int j = 1120; j >= i; --j){

					dp[k][j] += dp[k-1][j-i];

				}

			}

		}

	}

	

	int n, k;

	while(scanf("%d%d", &n, &k), n != 0){

		printf("%d\n", dp[k][n]);

	}

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

#define INF 0x3f3f3f3f

#define rint register int

#define gc() getchar()

inline int read(int r=0,int s=0,int c=gc()){for(;c<48||c>57;s=c,c=gc());for(;c>=48&&c<=57;(r*=10)+=c-48,c=gc());return s^'-'?r:-r;}

vector<int> e[1025]; int f[1025][5025], X[1025], n;

void DP(int p){for(auto v:e[p]) for(rint i = (DP(v),X[p]); ~i; f[p][i] = min(i>=X[v]?f[p][i-X[v]]+f[v][X[v]]:INF,i>=f[v][X[v]]?f[p][i-f[v][X[v]]]+X[v]:INF), i--);}

int main(){n = read(); for(rint i = 2; i <= n; e[read()].push_back(i), i++); for(rint i = 1; i <= n; X[i++] = read()); DP(1); puts(f[1][X[1]]<INF?"POSSIBLE":"IMPOSSIBLE"); return 0;}

// Faster C++ solution for the same problem (measured speedup: 4.869124964408808x):
#include<cstdio>

#include<cstring>

#include<algorithm>

#include<cmath>

#define min(a,b) ((a)<(b)?(a):(b))

#define reg register

#define file(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout);

#define endfile fclose(stdin);fclose(stdout);

namespace IO{

    char buf[1<<15],*S,*T;

    inline char gc(){

        if (S==T){

            T=(S=buf)+fread(buf,1,1<<15,stdin);

            if (S==T)return EOF;

        }

		return *S++;

    }

    inline int read(){

        reg int x;reg bool f;reg char c;

        for(f=0;(c=gc())<'0'||c>'9';f=c=='-');

        for(x=c^'0';(c=gc())>='0'&&c<='9';x=(x<<3)+(x<<1)+(c^'0'));

        return f?-x:x;

    }

}

const int MN=1005;

const int inf=0x3f3f3f3f;

int n,a[MN],f[MN],g[2][MN*5];

int to[MN],nxt[MN],h[MN],cnt;

inline void ins(int s,int t){to[++cnt]=t;nxt[cnt]=h[s];h[s]=cnt;}

bool dfs(int st){

	if(!h[st]){f[st]=0;return true;}

	for(reg int i=h[st];i;i=nxt[i])if(!dfs(to[i]))return false;

	reg int pos=0;memset(g[1],0x3f,sizeof(g[1]));g[1][0]=0;

	for(reg int i=h[st];i;i=nxt[i],pos^=1){

		memset(g[pos],0x3f,sizeof(g[pos]));

		for(reg int j=0;j<=a[st];j++){

			if(j>=f[to[i]])g[pos][j]=min(g[pos][j],g[pos^1][j-f[to[i]]]+a[to[i]]);

			if(j>=a[to[i]])g[pos][j]=min(g[pos][j],g[pos^1][j-a[to[i]]]+f[to[i]]);

		}

	}

	reg int res=inf;

	for(reg int i=0;i<=a[st];i++)res=min(res,g[pos^1][i]);

	if(res==inf)return false;f[st]=res;return true;

}

int main(){

	//file("division");

n=IO::read();

	for(reg int i=2;i<=n;i++)ins(IO::read(),i);

	for(reg int i=1;i<=n;i++)a[i]=IO::read();

	puts(dfs(1)?"POSSIBLE":"IMPOSSIBLE");endfile;

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <vector>

#include <bitset>

#define N 1005

using namespace std;

int n, s, x[N], y[N];

vector<int> a[N];

bitset<1<<20> bs;



int main() {

	int i, j, t;

	cin>>n;

	for(i=2; i<=n; i++) cin>>t, a[t].push_back(i);

	for(i=1; i<=n; i++) cin>>x[i];

	for(i=n; i>=1; i--) {

		bs = 1;

		s = 0;

		for(j=0; j<a[i].size(); j++) {

			t = a[i][j];

			bs = bs<<x[t] | bs<<y[t];

			s += x[t] + y[t];

		}

		for(j=x[i]; j>=0 && !bs[j]; j--);

		if(j<0) {cout<<"IMPOSSIBLE"; return 0;}

		else y[i] = s - j;

	}

	cout<<"POSSIBLE";

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.9955656581588754x):
#include <iostream>

#include <vector>

#include <bitset>

#define N 1005

using namespace std;

int n, s, x[N], y[N];

vector<int> a[N];

bitset<5005> bs;



int main() {

	int i, j, t;

	cin>>n;

	for(i=2; i<=n; i++) cin>>t, a[t].push_back(i);

	for(i=1; i<=n; i++) cin>>x[i];

	for(i=n; i>=1; i--) {

		bs = 1;

		s = 0;

		for(j=0; j<a[i].size(); j++) {

			t = a[i][j];

			bs = bs<<x[t] | bs<<y[t];

			s += x[t] + y[t];

		}

		for(j=x[i]; j>=0 && !bs[j]; j--);

		if(j<0) {cout<<"IMPOSSIBLE"; return 0;}

		else y[i] = s - j;

	}

	cout<<"POSSIBLE";

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <vector>

#include <bitset>

#define N 1005

using namespace std;

int n, s, x[N], y[N];

vector<int> a[N];

bitset<1<<20> bs;



int main() {

	int i, j, t;

	cin>>n;

	for(i=2; i<=n; i++) cin>>t, a[t].push_back(i);

	for(i=1; i<=n; i++) cin>>x[i];

	for(i=n; i>=1; i--) {

		bs = 1;

		s = 0;

		for(j=0; j<a[i].size(); j++) {

			t = a[i][j];

			bs = bs<<x[t] | bs<<y[t];

			s += x[t] + y[t];

		}

		for(j=x[i]; j>=0 && !bs[j]; j--);

		if(j<0) {cout<<"IMPOSSIBLE"; return 0;}

		else y[i] = s - j;

	}

	cout<<"POSSIBLE";

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.988010378637066x):
#include <iostream>

#include <cstdio>

#include <vector>

#include <bitset>

#define N 1005

using namespace std;



int n, t, s, x[N], y[N];

vector<int> a[N];

bitset<5005> bs;

int main()

{

	int i, j;

	cin >> n;

	for (i = 2; i <= n; i++) {

		scanf ("%d", &t);

		a[t].push_back(i);

	}

	for (i = 1; i <= n; i++) {

		scanf ("%d", &x[i]);

	}

	for (i = n; i >= 1; i--) {

		bs = 1;

		s = 0;

		for (j = 0; j < a[i].size(); j++) {

			t = a[i][j];

			bs = (bs << x[t] | bs << y[t]);

			s += x[t] + y[t];

		}

		for (j = x[i]; j >= 0; j--) if (bs[j]) break;

		y[i] = s - j;

		if (j < 0) {

			cout << "IMPOSSIBLE";

			return 0;

		}

//		for (j = 0; j <= x[i]; j++) {

//			if (bs[x[i] - j] || bs[s - x[i] - j]) break;

//		}

//		if (j > x[i]) break;

//		printf("%d\n", bs[0]);

	}

	cout << "POSSIBLE";

    return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 5e5+1;

 

 

int n,fa[N],tot[N],fr[N],a[N],b[N],c[N],cnt,head[N];

 

struct nd{int ne,to;}e[N];

 

void in(int x,int y){e[++cnt].to=y;e[cnt].ne=head[x];head[x]=cnt;}

int w1[5001],q[N];

void dfs(int x)

{

	for(int i=head[x];i;i=e[i].ne)dfs(e[i].to);

	if(!fr[x])return;

	q[0]=0;

	int ret=tot[x],tot1=0;

	for(int i=head[x];i;i=e[i].ne)

	{

		ret-=min(a[e[i].to],b[e[i].to]);

		q[++q[0]]=abs(a[e[i].to]-b[e[i].to]);

		tot1+=a[e[i].to]+b[e[i].to];

	}

	if(ret<0){cout<<"IMPOSSIBLE";exit(0);}

memset(w1,0,sizeof(w1));

	w1[0]=x;

	for(int i=1;i<=q[0];++i)

	for(int t=5000;t>=q[i];--t)

	if(w1[t-q[i]]==x)w1[t]=x;

	for(int i=ret;i>=0;--i)

	if(w1[i]==x){ret-=i;break;}

	a[x]=tot[x];b[x]=tot1+ret-a[x];

	return;

}

 

int main()

{

	scanf("%d",&n);

	for(int i=2;i<=n;++i)

	scanf("%d",&fa[i]),in(fa[i],i),fr[fa[i]]++;

	for(int i=1;i<=n;++i)

	{

		scanf("%d",&tot[i]);

		if(!fr[i])a[i]=tot[i],b[i]=0;

	}

	dfs(1);

	cout<<"POSSIBLE";

 

 

}

// Faster C++ solution for the same problem (measured speedup: 4.825984442389215x):
#include <cstdio>

#include <algorithm>

#include <cmath>

#include <cstring>



using std::min;

using std::abs;



const int MAXN=1005,MAXW=5005;

int n;

int he[MAXN],w[MAXN],blk[MAXN],wht[MAXN];



struct line{

	int to,nex;

	line(int to=0,int nex=0):to(to),nex(nex){}

}ed[MAXN];



inline void addE(int u,int v){

	static int cnt=0;

	ed[++cnt]=line(v,he[u]);

	he[u]=cnt;

}



bool treeDP(int u){

	static int tmp[MAXN];

	static bool can[MAXW];

	for(int i=he[u];i;i=ed[i].nex){

		if(!treeDP(ed[i].to)) return false;

	}

	if(!he[u]){

		blk[u]=w[u];

		return true;

	}

	int need=w[u],sum=0;

	tmp[0]=0;

	for(int i=he[u],v;i;i=ed[i].nex){

		v=ed[i].to;

		need-=min(blk[v],wht[v]);

		if(need<0) return false;

		tmp[++tmp[0]]=abs(blk[v]-wht[v]);

		sum+=blk[v]+wht[v];

	}

	memset(can,false,sizeof(can));

	can[0]=true;

	for(int i=1;i<=tmp[0];++i){

		for(int j=MAXW-1;j>=tmp[i];--j)

			can[j]|=can[j-tmp[i]];

	}

	for(int i=need;i>=0;--i){

		if(can[i]){

			need-=i;

			break;

		}

	}

	blk[u]=w[u],wht[u]=sum-blk[u]+need;

	return true;

}



int main(){

	scanf("%d",&n);

	for(int i=2,fa;i<=n;++i){

		scanf("%d",&fa);

		addE(fa,i);

	}

	for(int i=1;i<=n;++i)

 		scanf("%d",&w[i]);

	if(treeDP(1)) printf("POSSIBLE");

	else printf("IMPOSSIBLE");

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 5e5+1;

 

 

int n,fa[N],tot[N],fr[N],blk[N],wht[N],c[N],cnt,head[N];

 

struct nd{int ne,to;}e[N];

 

void in(int x,int y){e[++cnt].to=y;e[cnt].ne=head[x];head[x]=cnt;}

int can[5001],tmp[N];

bool dfs(int x)

{

	for(int i=head[x];i;i=e[i].ne) if(!dfs(e[i].to)) return false;

	if(!head[x]){

		blk[x]=tot[x];

		return true;

	}

	tmp[0]=0;

	int ret=tot[x],tot1=0;

	for(int i=head[x];i;i=e[i].ne)

	{

			ret-=min(blk[e[i].to],wht[e[i].to]);

			if(ret<0) return false;

			tmp[++tmp[0]]=abs(blk[e[i].to]-wht[e[i].to]);

			tot1+=blk[e[i].to]+wht[e[i].to];

	}

	memset(can,0,sizeof(can));

	can[0]=1;

	for(int i=1;i<=tmp[0];++i)

	for(int t=5000;t>=tmp[i];--t)

		can[t]|=can[t-tmp[i]];

	for(int i=ret;i>=0;--i)

	if(can[i]){ret-=i;break;}

	blk[x]=tot[x];wht[x]=tot1+ret-blk[x];

	return true;

}

 

int main()

{

	scanf("%d",&n);

	for(int i=2;i<=n;++i)

	scanf("%d",&fa[i]),in(fa[i],i);

	for(int i=1;i<=n;++i)

	{

		scanf("%d",&tot[i]);

	}

	if(dfs(1)) cout<<"POSSIBLE";

	else cout<<"IMPOSSIBLE";

 

}

// Faster C++ solution for the same problem (measured speedup: 4.789862426936481x):
#include <cstdio>

#include <algorithm>

#include <cmath>

#include <cstring>



using std::min;

using std::abs;



const int MAXN=1005,MAXW=5005;

int n;

int he[MAXN],w[MAXN],blk[MAXN],wht[MAXN];



struct line{

	int to,nex;

	line(int to=0,int nex=0):to(to),nex(nex){}

}ed[MAXN];



inline void addE(int u,int v){

	static int cnt=0;

	ed[++cnt]=line(v,he[u]);

	he[u]=cnt;

}



bool treeDP(int u){

	static int tmp[MAXN];

	static bool can[MAXW];

	for(int i=he[u];i;i=ed[i].nex){

		if(!treeDP(ed[i].to)) return false;

	}

	if(!he[u]){

		blk[u]=w[u];

		return true;

	}

	int need=w[u],sum=0;

	tmp[0]=0;

	for(int i=he[u],v;i;i=ed[i].nex){

		v=ed[i].to;

		need-=min(blk[v],wht[v]);

		if(need<0) return false;

		tmp[++tmp[0]]=abs(blk[v]-wht[v]);

		sum+=blk[v]+wht[v];

	}

	memset(can,false,sizeof(can));

	can[0]=true;

	for(int i=1;i<=tmp[0];++i){

		for(int j=MAXW-1;j>=tmp[i];--j)

			can[j]|=can[j-tmp[i]];

	}

	for(int i=need;i>=0;--i){

		if(can[i]){

			need-=i;

			break;

		}

	}

	blk[u]=w[u],wht[u]=sum-blk[u]+need;

	return true;

}



int main(){

	scanf("%d",&n);

	for(int i=2,fa;i<=n;++i){

		scanf("%d",&fa);

		addE(fa,i);

	}

	for(int i=1;i<=n;++i)

 		scanf("%d",&w[i]);

	if(treeDP(1)) printf("POSSIBLE");

	else printf("IMPOSSIBLE");

	return 0;

}
// Slower C++ solution:
# include <bits/stdc++.h>



# define pb push_back

# define fr first

# define sc second

# define mk make_pair



using namespace std;



typedef long long ll;

const int inf = 1e9 + 7;

const int N = 5e3 + 1;



int n, x[N], y[N];

vector <int> g[N];



void dfs(int v){

      vector <int> dp(N, 1e9);

      dp[0] = 0;

      for(int to : g[v]){

            dfs(to);

            vector <int> old(N, 1e9);

            for(int i = 0; i <= 5000; i ++){

                  if(i + x[to] <= 5000) old[i + x[to]] = min(old[i + x[to]], dp[i] + y[to]);

                  if(i + y[to] <= 5000) old[i + y[to]] = min(old[i + y[to]], dp[i] + x[to]);

            }

            dp = old;

      }

      for(int i = 0; i <= x[v]; i ++)

            y[v] = min(y[v], dp[i]);

      if(y[v] == 1e9){

            puts("IMPOSSIBLE");

            exit(0);

      }

}



int main(){

      scanf("%d", &n);



      for(int i = 2; i <= n; i ++){

            int p;

            scanf("%d", &p);

            g[p].pb(i);

      }



      for(int i = 1; i <= n; i ++){

            scanf("%d", &x[i]);

            y[i] = 1e9;

      }



      dfs(1);



      puts("POSSIBLE");

}


// Faster C++ solution for the same problem (measured speedup: 1.2334303426813606x):
#include<iostream>

#include<cstring>

#include<cstdio>

#include<algorithm>

#include<climits>

#define N 1010

#define MXV 5010

#define INF (INT_MAX/2-10)

#define debug(x) cerr<<#x<<"="<<x

#define sp <<" "

#define ln <<endl

using namespace std;

struct edges{

	int to,pre;

}e[N];int f[N],dp[MXV],X[N],h[N],etop;

inline int add_edge(int u,int v)

{

	return e[++etop].to=v,e[etop].pre=h[u],h[u]=etop;

}

int dfs(int x)

{

	for(int i=h[x];i;i=e[i].pre)

		if(!dfs(e[i].to)) return 0;

	for(int i=1;i<=X[x];i++) dp[i]=INF;

	for(int i=h[x]+(dp[0]=0);i;i=e[i].pre)

	{

		int a=X[e[i].to],b=f[e[i].to];

//		debug(x)sp,debug(e[i].to)sp,debug(a)sp,debug(b)ln;

		for(int j=X[x],t;j>=min(a,b);j--)

		{

			t=dp[j],dp[j]=INF;

			if(j>=a) dp[j]=min(dp[j],(a?dp[j-a]:t)+b);

			if(j>=b) dp[j]=min(dp[j],(b?dp[j-b]:t)+a);

		}

		for(int j=0;j<min(a,b);j++) dp[j]=INF;

	}

	for(int i=0;i<=X[x];i++) f[x]=min(f[x],dp[i]);

//	debug(x)sp,debug(f[x])ln;

	return f[x]<INF;

}

int main()

{

	int n;scanf("%d",&n);

	for(int i=2,x;i<=n;i++) scanf("%d",&x),add_edge(x,i);

	for(int i=1;i<=n;i++) scanf("%d",&X[i]),f[i]=INF;

	return !printf("%s\n",dfs(1)?"POSSIBLE":"IMPOSSIBLE");

}