Problem p03654 (C++)

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

#ifdef LOCAL
#define dbg(...) debug(#__VA_ARGS__, __VA_ARGS__);
#else
#define dbg(...) 17;
#endif

template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <typename C, typename T = decay<decltype(*begin(declval<C>()))>,
          typename enable_if<!is_same<C, string>::value>::type * = nullptr>
ostream &operator<<(ostream &os, const C &c) {
  bool f = true;
  os << "{";
  for (const auto &x : c) {
    if (!f)
      os << ", ";
    f = false;
    os << x;
  }
  return os << "}";
}
template <typename T> void debug(string s, T x) {
  cerr << s << " = " << x << "\n";
}
template <typename T, typename... Args>
void debug(string s, T x, Args... args) {
  cerr << s.substr(0, s.find(',')) << " = " << x << " | ";
  debug(s.substr(s.find(',') + 2), args...);
}
const int MOD = 1e9 + 7;
typedef decay<decltype(MOD)>::type T;
struct mi {
  T val;
  explicit operator T() const { return val; }
  mi() { val = 0; }
  mi(const long long &v) {
    val = (-MOD <= v && v <= MOD) ? v : v % MOD;
    if (val < 0)
      val += MOD;
  }
  friend ostream &operator<<(ostream &os, const mi &a) { return os << a.val; }
  friend bool operator==(const mi &a, const mi &b) { return a.val == b.val; }
  friend bool operator!=(const mi &a, const mi &b) { return !(a == b); }
  friend bool operator<(const mi &a, const mi &b) { return a.val < b.val; }
  mi operator-() const { return mi(-val); }
  mi &operator+=(const mi &m) {
    if ((val += m.val) >= MOD)
      val -= MOD;
    return *this;
  }
  mi &operator-=(const mi &m) {
    if ((val -= m.val) < 0)
      val += MOD;
    return *this;
  }
  mi &operator*=(const mi &m) {
    val = (long long)val * m.val % MOD;
    return *this;
  }
  friend mi pow(mi a, long long p) {
    mi ans = 1;
    assert(p >= 0);
    for (; p; p /= 2, a *= a)
      if (p & 1)
        ans *= a;
    return ans;
  }
  friend mi inv(const mi &a) {
    assert(a != 0);
    return pow(a, MOD - 2);
  }
  mi &operator/=(const mi &m) { return (*this) *= inv(m); }
  friend mi operator+(mi a, const mi &b) { return a += b; }
  friend mi operator-(mi a, const mi &b) { return a -= b; }
  friend mi operator*(mi a, const mi &b) { return a *= b; }
  friend mi operator/(mi a, const mi &b) { return a /= b; }
};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<vector<pair<int, ll>>> adj(n);
  for (int i = 0; i < n - 1; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    adj[a].emplace_back(b, c);
    adj[b].emplace_back(a, c);
  }
  vector<int> sub(n);
  vector<ll> depth(n);
  vector<int> p(n);
  function<void(int, int)> dfs = [&](int src, int par) {
    sub[src] = 1;
    p[src] = par;
    for (auto nxt : adj[src]) {
      if (nxt.first == par) {
        continue;
      }
      depth[nxt.first] = depth[src] + nxt.second;
      dfs(nxt.first, src);
      sub[src] += sub[nxt.first];
    }
  };
  dfs(0, -1);
  ll ans = 0;
  bool ok = false;
  for (int i = 0; i < n; i++) {
    if (p[i] == -1) {
      continue;
    }
    ll weight = depth[i] - depth[p[i]];
    int up = n - sub[i];
    int down = sub[i];
    ans += 2 * min(up, down) * weight;
    if (up == down) {
      ans -= weight;
      ok = true;
    }
  }
  if (ok) {
    cout << ans << '\n';
    return 0;
  }
  assert(false);
}


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

#ifdef LOCAL
#define dbg(...) debug(#__VA_ARGS__, __VA_ARGS__);
#else
#define dbg(...) 17;
#endif

template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <typename C, typename T = decay<decltype(*begin(declval<C>()))>,
          typename enable_if<!is_same<C, string>::value>::type * = nullptr>
ostream &operator<<(ostream &os, const C &c) {
  bool f = true;
  os << "{";
  for (const auto &x : c) {
    if (!f)
      os << ", ";
    f = false;
    os << x;
  }
  return os << "}";
}
template <typename T> void debug(string s, T x) {
  cerr << s << " = " << x << "\n";
}
template <typename T, typename... Args>
void debug(string s, T x, Args... args) {
  cerr << s.substr(0, s.find(',')) << " = " << x << " | ";
  debug(s.substr(s.find(',') + 2), args...);
}
const int MOD = 1e9 + 7;
typedef decay<decltype(MOD)>::type T;
struct mi {
  T val;
  explicit operator T() const { return val; }
  mi() { val = 0; }
  mi(const long long &v) {
    val = (-MOD <= v && v <= MOD) ? v : v % MOD;
    if (val < 0)
      val += MOD;
  }
  friend ostream &operator<<(ostream &os, const mi &a) { return os << a.val; }
  friend bool operator==(const mi &a, const mi &b) { return a.val == b.val; }
  friend bool operator!=(const mi &a, const mi &b) { return !(a == b); }
  friend bool operator<(const mi &a, const mi &b) { return a.val < b.val; }
  mi operator-() const { return mi(-val); }
  mi &operator+=(const mi &m) {
    if ((val += m.val) >= MOD)
      val -= MOD;
    return *this;
  }
  mi &operator-=(const mi &m) {
    if ((val -= m.val) < 0)
      val += MOD;
    return *this;
  }
  mi &operator*=(const mi &m) {
    val = (long long)val * m.val % MOD;
    return *this;
  }
  friend mi pow(mi a, long long p) {
    mi ans = 1;
    assert(p >= 0);
    for (; p; p /= 2, a *= a)
      if (p & 1)
        ans *= a;
    return ans;
  }
  friend mi inv(const mi &a) {
    assert(a != 0);
    return pow(a, MOD - 2);
  }
  mi &operator/=(const mi &m) { return (*this) *= inv(m); }
  friend mi operator+(mi a, const mi &b) { return a += b; }
  friend mi operator-(mi a, const mi &b) { return a -= b; }
  friend mi operator*(mi a, const mi &b) { return a *= b; }
  friend mi operator/(mi a, const mi &b) { return a /= b; }
};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<vector<pair<int, ll>>> adj(n);
  for (int i = 0; i < n - 1; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    adj[a].emplace_back(b, c);
    adj[b].emplace_back(a, c);
  }
  vector<int> sub(n);
  vector<ll> depth(n);
  vector<int> p(n);
  function<void(int, int)> dfs = [&](int src, int par) {
    sub[src] = 1;
    p[src] = par;
    for (auto nxt : adj[src]) {
      if (nxt.first == par) {
        continue;
      }
      depth[nxt.first] = depth[src] + nxt.second;
      dfs(nxt.first, src);
      sub[src] += sub[nxt.first];
    }
  };
  dfs(0, -1);
  ll ans = 0;
  bool ok = false;
  for (int i = 0; i < n; i++) {
    if (p[i] == -1) {
      continue;
    }
    ll weight = depth[i] - depth[p[i]];
    int up = n - sub[i];
    int down = sub[i];
    ans += 2 * min(up, down) * weight;
    if (up == down) {
      ans -= weight;
      ok = true;
    }
  }
  if (ok) {
    cout << ans << '\n';
    return 0;
  }
  int cent = -1;
  for (int i = 0; i < n; i++) {
    if (n - sub[i] > n / 2) {
      continue;
    }
    bool good = true;
    for (auto nxt : adj[i]) {
      if (nxt.first == p[i]) {
        continue;
      }
      if (sub[nxt.first] > n / 2) {
        good = false;
        break;
      }
    }
    if (good == false) {
      continue;
    }
    cent = i;
  }
  ll mn = 1e18;
  for (auto nxt : adj[cent]) {
    mn = min(mn, abs(depth[nxt.first] - depth[cent]));
  }
  ans -= mn;
  cout << ans << '\n';
}