Problem p03159 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;

#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) (lower_bound(all(x), y) - x.begin())
#define ubd(x, y) (upper_bound(all(x), y) - x.begin())
mt19937
    rng(chrono::steady_clock::now()
            .time_since_epoch()
            .count()); // can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) {
  return (rng() % (y + 1 - x)) + x;
} // inclusivesss
string inline to_string(char c) {
  string s(1, c);
  return s;
}
template <typename T> inline T gcd(T a, T b) {
  return a == 0 ? llabs(b) : gcd(b % a, a);
}

typedef long long ll;
typedef long double ld;
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
typedef pair<ll, ll> pi;
typedef pair<ll, pi> spi;
typedef pair<pi, pi> dpi;

#define LLINF ((long long)1e18)
#define INF int(1e9 + 1e6)
#define MAXN (5006)
ll n, A[MAXN], dp[MAXN][MAXN], sz[MAXN]; // if 0 then max, 1 then min
bitset<MAXN> dp2[MAXN];
vector<int> v[MAXN];
void dfs(ll x, ll p) {
  sz[x] = 1;
  dp[x][0] = A[x];
  for (auto i : v[x])
    if (i ^ p)
      dfs(i, x), sz[x] += sz[i];
  FOR(k, 1, sz[x]) dp[x][k] = LLINF;
  ll cur = 1;
  if (A[x] > 0)
    dp2[x][0] = 1;
  for (auto i : v[x])
    if (i ^ p) {
      DEC(j, cur + sz[i], 0) {
        if (dp[x][j] < LLINF)
          dp[x][j] += dp[i][0];
        dp2[x][j] = dp2[x][j] & dp2[i][0];
        FOR(k, 1ll, min(j, sz[i])) {
          dp[x][j] = min(dp[x][j], dp[x][j - k] + dp[i][k]);
          if (dp2[i][k - 1])
            dp[x][j] = min(dp[x][j], dp[x][j - k]);
          dp2[x][j] = dp2[x][j] | (dp2[x][j - k] & dp2[i][k]);
          if (dp[i][k - 1] < 0)
            dp2[x][j] = dp2[x][j] | dp2[x][j - k];
        }
      }
      cur += sz[i];
    }
}
int main() {
  FAST cin >> n;
  FOR(i, 1, n) cin >> A[i];
  FOR(i, 2, n) {
    ll a, b;
    cin >> a >> b;
    v[a].eb(b), v[b].eb(a);
  }
  dfs(1, 1);
  ll ans = n - 1;
  FOR(k, 0, n - 1) if (dp[1][k] < 0) ans = min(ans, k);
  FOR(k, 0, n - 1) if (dp2[1][k]) ans = min(ans, k);
  cout << ans << '\n';
}


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

#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) (lower_bound(all(x), y) - x.begin())
#define ubd(x, y) (upper_bound(all(x), y) - x.begin())
mt19937
    rng(chrono::steady_clock::now()
            .time_since_epoch()
            .count()); // can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) {
  return (rng() % (y + 1 - x)) + x;
} // inclusivesss
string inline to_string(char c) {
  string s(1, c);
  return s;
}
template <typename T> inline T gcd(T a, T b) {
  return a == 0 ? llabs(b) : gcd(b % a, a);
}

typedef long long ll;
typedef long double ld;
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
typedef pair<ll, ll> pi;
typedef pair<ll, pi> spi;
typedef pair<pi, pi> dpi;

#define LLINF ((long long)1e18)
#define INF int(1e9 + 1e6)
#define MAXN (5006)
ll n, A[MAXN], dp[MAXN][MAXN], sz[MAXN]; // if 0 then max, 1 then min
bitset<MAXN> dp2[MAXN];
vector<int> v[MAXN];
void dfs(ll x, ll p) {
  sz[x] = 1;
  dp[x][0] = A[x];
  for (auto i : v[x])
    if (i ^ p)
      dfs(i, x), sz[x] += sz[i];
  FOR(k, 1, sz[x]) dp[x][k] = LLINF;
  ll cur = 1;
  if (A[x] > 0)
    dp2[x][0] = 1;
  for (auto i : v[x])
    if (i ^ p) {
      DEC(j, cur + sz[i], 0) {
        if (dp[x][j] < LLINF)
          dp[x][j] += dp[i][0];
        dp2[x][j] = dp2[x][j] & dp2[i][0];
        FOR(k, max(j - cur, 1ll), min(j, sz[i])) {
          dp[x][j] = min(dp[x][j], dp[x][j - k] + dp[i][k]);
          if (dp2[i][k - 1])
            dp[x][j] = min(dp[x][j], dp[x][j - k]);
          dp2[x][j] = dp2[x][j] | (dp2[x][j - k] & dp2[i][k]);
          if (dp[i][k - 1] < 0)
            dp2[x][j] = dp2[x][j] | dp2[x][j - k];
        }
      }
      cur += sz[i];
    }
}
int main() {
  FAST cin >> n;
  FOR(i, 1, n) cin >> A[i];
  FOR(i, 2, n) {
    ll a, b;
    cin >> a >> b;
    v[a].eb(b), v[b].eb(a);
  }
  dfs(1, 1);
  ll ans = n - 1;
  FOR(k, 0, n - 1) if (dp[1][k] < 0) ans = min(ans, k);
  FOR(k, 0, n - 1) if (dp2[1][k]) ans = min(ans, k);
  cout << ans << '\n';
}