Language: C++

#include <cmath>
#include <iostream>
#include <vector>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <bitset>
#include <algorithm>
#include <functional>
#include <utility>
#include <iomanip>
// #include <fstream>
#define int long long int
#define mod(x) ((x % mod) + mod) % mod
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);--i)

#define all(c) (c).begin(),(c).end()
#define size(c) (int)((c).size())
#define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define sort(c) sort(all(c))
#define lb(c,x) lower_bound(all(c),x)
#define ub(c,x) upper_bound(all(c),x)
#define count(c,x) (int)(ub(c,x)-lb(c,x))

#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (l" << __line__ << ")" << endl;
#define passed cerr << "passed" << " (l" << __line__ << ")" << endl;

#define max_x 5001

using namespace std;

typedef pair<int,int> p;
struct edge {
  int to, cost;
};

const int inf = 1e18;
const int mod = 1e9+7;

template<typename t> ostream& operator<<(ostream& s, const vector<t>& v) {
  int len = v.size();
  for (int i = 0; i < len; ++i) {
    s << v[i]; if (i < len - 1) s << " ";
  }
  return s;
}


signed main()
{
  // string filename; cin >> filename;
  // ifstream in(filename);
  // cin.rdbuf(in.rdbuf());

  int n; cin >> n;
  vector<int> p(n), x(n);
  rep(i, 1, n) {
    cin >> p[i];
    p[i]--;
  }
  rep(i, 0, n) {
    cin >> x[i];
  }

  vector< vector<int> > g(n, vector<int>());
  rep(i, 1, n) {
    g[p[i]].push_back(i);
  }

  vector<int> y(n, inf);
  rrep(v, 0, n) {
    // debug(v)
    vector< vector<int> > dp(size(g[v])+1, vector<int>(max_x,inf));
    int x_sum = 0, y_sum = 0;
    rep(i, 0, size(g[v])) {
      x_sum += x[g[v][i]];
      y_sum += y[g[v][i]];
    }
    dp[0][y_sum] = x_sum;

    // debug(x_sum)
    // debug(y_sum)

    rep(i, 0, size(g[v])) {
      rep(j, 0, max_x) {
        if (j - x[g[v][i]] + y[g[v][i]] >= 0) {
          dp[i+1][j] = min(dp[i][j - x[g[v][i]] + y[g[v][i]]] - x[g[v][i]] + y[g[v][i]], dp[i][j]);
        } else {
          dp[i+1][j] = dp[i][j];
        }
      }
    }

    // rep(i, 0, size(g[v])+1) {
    //   debug(dp[i])
    // }

    int m = inf;
    rep(j, 0, x[v]+1) {
      m = min(m, dp[size(g[v])][j]);
    }

    if (m == inf) {
      cout << "impossible" << endl;
      return 0;
    }
    y[v] = m;

    // debug(y[v])
  }

  // if (y[0] == inf) {
  //   cout << "impossible" << endl;
  // } else {
  //   cout << "possible" << endl;
  // }

  cout << "possible" << endl;

  return 0;
}


Compiler output:
a.cc: In function 'std::ostream& operator<<(std::ostream&, const std::vector<_Tp>&)':
a.cc:22:17: error: expected unqualified-id before '(' token
   22 | #define size(c) (int)((c).size())
      |                 ^
a.cc:46:15: note: in expansion of macro 'size'
   46 |   int len = v.size();
      |               ^~~~
a.cc:16:13: error: expected primary-expression before 'long'
   16 | #define int long long int
      |             ^~~~
a.cc:22:18: note: in expansion of macro 'int'
   22 | #define size(c) (int)((c).size())
      |                  ^~~
a.cc:46:15: note: in expansion of macro 'size'
   46 |   int len = v.size();
      |               ^~~~
a.cc:22:25: error: expected primary-expression before ')' token
   22 | #define size(c) (int)((c).size())
      |                         ^
a.cc:46:15: note: in expansion of macro 'size'
   46 |   int len = v.size();
      |               ^~~~