Problem p03654 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<int> v[100005];
vector<ll> w[100005];
int sz[100005], son[100005], n, tag = 0;
ll ans = 0;
inline int read() {
  int x = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x;
}
inline ll readll() {
  ll x = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10ll + c - '0', c = getchar();
  return x;
}
void dfs1(int pos, int fa) {
  int i;
  sz[pos] = 1;
  for (i = 0; i < v[pos].size(); i++) {
    if (v[pos][i] != fa) {
      dfs1(v[pos][i], pos);
      sz[pos] += sz[v[pos][i]];
      son[pos] = max(son[pos], sz[v[pos][i]]);
      int p = min(sz[v[pos][i]], n - sz[v[pos][i]]);
      ans += 2ll * ((ll)p) * w[pos][i];
    }
  }
  son[pos] = max(son[pos], n - sz[pos]);
}
ll mine;
void dfs2(int pos, int fa) {
  int i;
  sz[pos] = 1;
  for (i = 0; i < v[pos].size(); i++) {
    if (v[pos][i] != fa) {
      dfs2(v[pos][i], pos);
      sz[pos] += sz[v[pos][i]];
      int p = min(sz[v[pos][i]], n - sz[v[pos][i]]);
      if (2 * p == n) {
        mine = w[pos][i];
        tag = 1;
      }
    }
  }
}
int main() {
  int i, a, b, j;
  ll val;
  n = read();
  for (i = 1; i < n; i++) {
    a = read();
    b = read();
    val = readll();
    v[a].push_back(b);
    w[a].push_back(val);
    v[b].push_back(a);
    w[b].push_back(val);
  }
  dfs1(1, 0);
  int minn = 233333, pl = -1;
  for (i = 1; i <= n; i++) {
    if (son[i] < minn) {
      minn = son[i];
      pl = i;
    }
  }
  dfs2(pl, 0);
  if (!tag) {
    mine = 2333333333ll;
    for (i = 1; i <= n; i++) {
      if (son[i] <= n / 2) {
        for (j = 0; i < v[i].size(); j++) {
          mine = min(mine, w[i][j]);
        }
      }
    }
  }
  ans -= mine;
  printf("%lld", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<int> v[100005];
vector<ll> w[100005];
int sz[100005], son[100005], n, tag = 0;
ll ans = 0;
inline int read() {
  int x = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x;
}
inline ll readll() {
  ll x = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10ll + c - '0', c = getchar();
  return x;
}
void dfs1(int pos, int fa) {
  int i;
  sz[pos] = 1;
  for (i = 0; i < v[pos].size(); i++) {
    if (v[pos][i] != fa) {
      dfs1(v[pos][i], pos);
      sz[pos] += sz[v[pos][i]];
      son[pos] = max(son[pos], sz[v[pos][i]]);
      int p = min(sz[v[pos][i]], n - sz[v[pos][i]]);
      ans += 2ll * ((ll)p) * w[pos][i];
    }
  }
  son[pos] = max(son[pos], n - sz[pos]);
}
ll mine;
void dfs2(int pos, int fa) {
  int i;
  sz[pos] = 1;
  for (i = 0; i < v[pos].size(); i++) {
    if (v[pos][i] != fa) {
      dfs2(v[pos][i], pos);
      sz[pos] += sz[v[pos][i]];
      int p = min(sz[v[pos][i]], n - sz[v[pos][i]]);
      if (2 * p == n) {
        mine = w[pos][i];
        tag = 1;
      }
    }
  }
}
int main() {
  int i, a, b, j;
  ll val;
  n = read();
  for (i = 1; i < n; i++) {
    a = read();
    b = read();
    val = readll();
    v[a].push_back(b);
    w[a].push_back(val);
    v[b].push_back(a);
    w[b].push_back(val);
  }
  dfs1(1, 0);
  int minn = 233333, pl = -1;
  for (i = 1; i <= n; i++) {
    if (son[i] < minn) {
      minn = son[i];
      pl = i;
    }
  }
  dfs2(pl, 0);
  if (!tag) {
    mine = 2333333333ll;
    for (i = 1; i <= n; i++) {
      if (son[i] <= n / 2) {
        for (j = 0; j < v[i].size(); j++) {
          mine = min(mine, w[i][j]);
        }
      }
    }
  }
  ans -= mine;
  printf("%lld", ans);
  return 0;
}