Problem p00777 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>

using namespace std;

#define reep(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) reep((i), 0, (n))
#define F first
#define S second
#define mkp make_pair
#define PB push_back
#define ALL(v) v.begin(), v.end()
#define maxs(a, b) (a = max(a, b))
#define mins(a, b) (a = min(a, b))

typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef pair<int, int> pii;
int n;

vector<vector<pii>> vv;
vector<pii> path;
vint used;
int dfs(int x, int pre) {
  for (pii y : vv[x]) {
    if (y.F == pre)
      continue;
    dfs(y.F, x);
    maxs(path[x], pii(path[y.F].F + y.S, path[y.F].S));
  }
  return path[x].F;
}

void mainmain() {
  vv = vector<vector<pii>>(n);
  vint _a(n - 1), _b(n - 1);
  path = vector<pii>(n, pii(0, -1));
  rep(i, n) path[i] = pii(0, i);
  // cout<<"n = "<<n<<endl;
  int ss = 0;
  rep(i, n - 1) cin >> _a[i];
  rep(i, n - 1) cin >> _b[i];
  rep(i, n - 1) {
    ss += _b[i];
    _a[i]--;
    vv[_a[i]].PB(pii(i + 1, _b[i]));
    vv[i + 1].PB(pii(_a[i], _b[i]));
    // cout<<i+1<<" "<<_a[i]<<endl;
  }
  vector<vector<pii>> ww(n);
  rep(i, n - 1) {
    if (vv[i].size() == 1)
      continue;
    rep(j, vv[i].size()) {
      if (vv[vv[i][j].F].size() == 1)
        continue;
      ww[i].PB(vv[i][j]);
      // cout<<i<<" "<<vv[i][j].F<<endl;
    }
  }
  int tt = 0;
  rep(i, n) {
    if (vv[i].size() == 1) {
      tt += vv[i][0].S;
    }
  }
  vv = ww;
  int p = -1;
  rep(i, n) {
    if (vv[i].size()) {
      dfs(i, -1);
      p = path[i].S;
      break;
    }
  }
  rep(i, n) path[i] = pii(0, i);
  if (p >= 0)
    dfs(p, -1);
  // if(p>=0) cout<<n<<" "<<ss<<" "<<p<<" "<<path[p].S<<" "<<path[p].F<<"
  // "<<tt<<endl;
  if (p >= 0)
    cout << ss * 3 - path[p].F - 2 * tt << endl;
  else
    cout << ss * 3 - 2 * tt << endl;
}

int main() {
  while (cin >> n, n) {
    mainmain();
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define reep(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) reep((i), 0, (n))
#define F first
#define S second
#define mkp make_pair
#define PB push_back
#define ALL(v) v.begin(), v.end()
#define maxs(a, b) (a = max(a, b))
#define mins(a, b) (a = min(a, b))

typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef pair<int, int> pii;
int n;

vector<vector<pii>> vv;
vector<pii> path;
vint used;
int dfs(int x, int pre) {
  for (pii y : vv[x]) {
    if (y.F == pre)
      continue;
    dfs(y.F, x);
    maxs(path[x], pii(path[y.F].F + y.S, path[y.F].S));
  }
  return path[x].F;
}

void mainmain() {
  vv = vector<vector<pii>>(n);
  vint _a(n - 1), _b(n - 1);
  path = vector<pii>(n, pii(0, -1));
  rep(i, n) path[i] = pii(0, i);
  // cout<<"n = "<<n<<endl;
  int ss = 0;
  rep(i, n - 1) cin >> _a[i];
  rep(i, n - 1) cin >> _b[i];
  rep(i, n - 1) {
    ss += _b[i];
    _a[i]--;
    vv[_a[i]].PB(pii(i + 1, _b[i]));
    vv[i + 1].PB(pii(_a[i], _b[i]));
    // cout<<i+1<<" "<<_a[i]<<endl;
  }
  vector<vector<pii>> ww(n);
  rep(i, n - 1) {
    if (vv[i].size() == 1)
      continue;
    rep(j, vv[i].size()) {
      if (vv[vv[i][j].F].size() == 1)
        continue;
      ww[i].PB(pii(vv[i][j]));
      // cout<<i<<" "<<vv[i][j].F<<endl;
    }
  }
  int tt = 0;
  rep(i, n) {
    if (vv[i].size() == 1) {
      tt += vv[i][0].S;
    }
  }
  vv = ww;
  int p;
  rep(i, n) {
    if (vv[i].size()) {
      dfs(i, -1);
      p = path[i].S;
      break;
    }
  }
  rep(i, n) path[i] = pii(0, i);
  dfs(p, -1);
  assert(p >= 0);
  // cout<<ss<<" "<<p<<" "<<path[p].S<<" "<<path[p].F<<" "<<tt<<endl;
  cout << ss * 3 - path[p].F - 2 * tt << endl;
}

int main() {
  while (cin >> n, n) {
    mainmain();
  }
}