Problem p00384 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

vector<int> graph[100010];
vector<int> tree[100010];

int mas[100010];
int n;

bool used[100010] = {};
void makeTree(int m) {
  used[m] = true;
  for (int i = 0; i < graph[m].size(); i++) {
    if (!used[graph[m][i]]) {
      tree[m].push_back(graph[m][i]);
      makeTree(graph[m][i]);
    }
  }
  return;
}

int dp[100010] = {};
int ep[100010] = {};
int fp[100010] = {};

void dfs(int x) {
  dp[x] = mas[x];
  ep[x] = 0;
  fp[x] = 0;

  if (tree[x].size() == 0)
    return;

  for (int i = 0; i < tree[x].size(); i++) {
    dfs(tree[x][i]);
  }

  bool used[100010] = {};
  for (int i = 0; i < tree[x].size(); i++) {
    if (dp[tree[x][i]] - 2 >= 0) {
      used[tree[x][i]] = true;
      dp[x] += dp[tree[x][i]] - 2;
    }
  }

  int kari = 0;
  int kari2 = 0;
  for (int i = 0; i < tree[x].size(); i++) {
    if (used[tree[x][i]]) {
      if (ep[tree[x][i]] + 1 > kari) {
        kari2 = kari;
        kari = ep[tree[x][i]] + 1;
      } else if (ep[tree[x][i]] + 1 > kari2) {
        kari2 = ep[tree[x][i]] + 1;
      }
    } else {
      if (ep[tree[x][i]] + 1 + dp[tree[x][i]] - 2 > kari) {
        kari2 = kari;
        kari = ep[tree[x][i]] + 1 + dp[tree[x][i]] - 2;
      } else if (ep[tree[x][i]] + 1 + dp[tree[x][i]] - 2 > kari2) {
        kari2 = ep[tree[x][i]] + 1 + dp[tree[x][i]] - 2;
      }
    }
  }

  if (kari >= 0) {
    ep[x] = kari;
  }

  int kari3 = 0;
  for (int i = 0; i < tree[x].size(); i++) {
    if (used[tree[x][i]]) {
      kari3 = max(kari3, fp[tree[x][i]]);
    } else {
      kari3 = max(kari3, fp[tree[x][i]] + dp[tree[x][i]] - 2);
    }
  }

  fp[x] = max(kari3, max(kari + kari2, max(kari, kari2)));
  return;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> mas[i];
  }

  int a, b;
  for (int i = 0; i < n - 1; i++) {
    cin >> a >> b;
    a--;
    b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }

  makeTree(0);
  dfs(0);

  int ans = INT_MIN;
  for (int i = 0; i < n; i++) {
    ans = max(ans, dp[i] + fp[i]);
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

vector<int> graph[100010];
vector<int> tree[100010];

int mas[100010];
int n;

bool used[100010] = {};
void makeTree(int m) {
  used[m] = true;
  for (int i = 0; i < graph[m].size(); i++) {
    if (!used[graph[m][i]]) {
      tree[m].push_back(graph[m][i]);
      makeTree(graph[m][i]);
    }
  }
  return;
}

int dp[100010] = {};
int ep[100010] = {};
int fp[100010] = {};

void dfs(int x) {
  dp[x] = mas[x];
  ep[x] = 0;
  fp[x] = 0;

  if (tree[x].size() == 0)
    return;

  for (int i = 0; i < tree[x].size(); i++) {
    dfs(tree[x][i]);
  }

  // bool used[100010] = {};
  memset(used, false, sizeof(used));
  for (int i = 0; i < tree[x].size(); i++) {
    if (dp[tree[x][i]] - 2 >= 0) {
      used[tree[x][i]] = true;
      dp[x] += dp[tree[x][i]] - 2;
    }
  }

  int kari = 0;
  int kari2 = 0;
  for (int i = 0; i < tree[x].size(); i++) {
    if (used[tree[x][i]]) {
      if (ep[tree[x][i]] + 1 > kari) {
        kari2 = kari;
        kari = ep[tree[x][i]] + 1;
      } else if (ep[tree[x][i]] + 1 > kari2) {
        kari2 = ep[tree[x][i]] + 1;
      }
    } else {
      if (ep[tree[x][i]] + 1 + dp[tree[x][i]] - 2 > kari) {
        kari2 = kari;
        kari = ep[tree[x][i]] + 1 + dp[tree[x][i]] - 2;
      } else if (ep[tree[x][i]] + 1 + dp[tree[x][i]] - 2 > kari2) {
        kari2 = ep[tree[x][i]] + 1 + dp[tree[x][i]] - 2;
      }
    }
  }

  if (kari >= 0) {
    ep[x] = kari;
  }

  int kari3 = 0;
  for (int i = 0; i < tree[x].size(); i++) {
    if (used[tree[x][i]]) {
      kari3 = max(kari3, fp[tree[x][i]]);
    } else {
      kari3 = max(kari3, fp[tree[x][i]] + dp[tree[x][i]] - 2);
    }
  }

  fp[x] = max(kari3, max(kari + kari2, max(kari, kari2)));
  return;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> mas[i];
  }

  int a, b;
  for (int i = 0; i < n - 1; i++) {
    cin >> a >> b;
    a--;
    b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }

  makeTree(0);
  dfs(0);

  int ans = INT_MIN;
  for (int i = 0; i < n; i++) {
    ans = max(ans, dp[i] + fp[i]);
  }

  cout << ans << endl;

  return 0;
}