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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <list>
#include <queue>
#include <utility>

using namespace std;

int main(void) {
  list<pair<int, int>> vp, p[800];
  queue<int> v;
  int i, j, k;
  int size[800], s[800][2];
  int n, to[800], cost[800];
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    memset(size, 0, sizeof(size));
    for (i = 0; i < n; i++)
      p[i].clear();
    for (i = 1; i < n; i++) {
      cin >> to[i];
      to[i]--;
    }
    for (i = 1; i < n; i++) {
      cin >> cost[i];
      p[i].push_back(make_pair(to[i], cost[i]));
      p[to[i]].push_back(make_pair(i, cost[i]));
    }
    int a = 0;
    for (i = 0; i < n; i++)
      size[i] = p[i].size();
    for (i = 0; i < n; i++) {
      if (size[i] == 1) {
        a += p[i].front().second;
        p[p[i].front().first].remove(make_pair(i, p[i].front().second));
        if (p[p[i].front().first].size() == 1) {
          v.push(p[i].front().first);
        } else if (p[p[i].front().first].size() == 0) {
          v.pop();
          break;
          p[i].clear();
        }
        p[i].clear();
      }
    }
    memset(s, 0, sizeof(s));
    while (v.size() != 0) {
      i = v.front();
      v.pop();
      k = p[i].front().second;
      a += 3 * k;
      j = p[i].front().first;
      if (s[j][1] < k + s[i][0]) {
        s[j][1] = k + s[i][0];
        if (s[j][0] < s[j][1])
          swap(s[j][0], s[j][1]);
      }
      p[j].remove(make_pair(i, p[i].front().second));
      p[i].clear();
      if (p[j].size() == 1)
        v.push(j);
      else if (p[j].size() == 0) {
        j = 0;
        for (i = 0; i < n; i++) {
          j = (j > s[i][0] + s[i][1]) ? j : s[i][0] + s[i][1];
        }
        a -= j;
        v.pop();
      }
    }
    cout << a << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <cstring>
#include <iostream>
#include <list>
#include <queue>

using namespace std;

int main(void) {
  list<pair<int, int>> vp, p[800];
  queue<int> v;
  int i, j, k;
  int size[800];
  int n, to[800], cost[800];
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    memset(size, 0, sizeof(size));
    for (i = 0; i < n; i++)
      p[i].clear();
    for (i = 1; i < n; i++) {
      cin >> to[i];
      to[i]--;
    }
    for (i = 1; i < n; i++) {
      cin >> cost[i];
      p[i].push_back(make_pair(to[i], cost[i]));
      p[to[i]].push_back(make_pair(i, cost[i]));
    }
    int a = 0;
    for (i = 0; i < n; i++)
      size[i] = p[i].size();
    for (i = 0; i < n; i++) {
      if (size[i] == 1) {
        a += p[i].front().second;
        p[p[i].front().first].remove(make_pair(i, p[i].front().second));
        if (p[p[i].front().first].size() == 1) {
          v.push(p[i].front().first);
        } else if (p[p[i].front().first].size() == 0) {
          v.pop();
          break;
          p[i].clear();
        }
        p[i].clear();
      }
    }
    memset(size, 0, sizeof(size));
    while (v.size() != 0) {
      i = v.front();
      v.pop();
      a += 3 * p[i].front().second;
      j = p[i].front().first;
      size[j] += p[i].front().second + size[i];
      p[j].remove(make_pair(i, p[i].front().second));
      p[i].clear();
      if (p[j].size() == 1)
        v.push(j);
      else if (p[j].size() == 0) {
        j = 0;
        for (i = 0; i < n; i++) {
          j = (j > size[i]) ? j : size[i];
        }
        a -= j;
        break;
      }
    }
    cout << a << endl;
  }
}