Language: C++

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;

int n;
vector<ll> a,S;
vector<vector<int>> G;
ll dp[5000][5000][2]={INF},sdp[5000][2]={INF},new_sdp[5000][2]={INF};

void dfs(int s,int par){
    rep(i,G[s].size()){
        int t=G[s][i];
        if (t==par) continue;
        dfs(t,s);
        S[s]+=S[t]+1;
    }

    rep(i,S[s]+1){
        sdp[i][0]=INF;
        sdp[i][1]=INF;
        new_sdp[i][0]=INF;
        new_sdp[i][1]=INF;

    }

    if (a[s]>0) sdp[0][1]=a[s];
    if (a[s]<0) sdp[0][0]=a[s];
    int M=0;
    rep(i,G[s].size()){
        int t=G[s][i];
        if (t==par) continue;
        rep(k,M+S[t]+2) {
            new_sdp[k][0]=INF;
            new_sdp[k][1]=INF;
        }
        rep(k,M+1){
            rep(c,S[t]+1){
                new_sdp[k+c][0]=min(new_sdp[k+c][0],sdp[k][0]+dp[t][c][0]);
                new_sdp[k+c][0]=min(new_sdp[k+c][0],sdp[k][1]+dp[t][c][0]);
                new_sdp[k+c][0]=min(new_sdp[k+c][0],sdp[k][0]+dp[t][c][1]);
                new_sdp[k+c][1]=min(new_sdp[k+c][1],sdp[k][1]+dp[t][c][1]);
                if (dp[t][c][0]<0) {
                    new_sdp[k+c+1][0]=min(new_sdp[k+c+1][0],sdp[k][0]);
                    new_sdp[k+c+1][1]=min(new_sdp[k+c+1][1],sdp[k][1]);
                }
                if (dp[t][c][1]<INF) {
                    new_sdp[k+c+1][0]=min(new_sdp[k+c+1][0],sdp[k][0]);
                    new_sdp[k+c+1][1]=min(new_sdp[k+c+1][1],sdp[k][1]);
                }
            }

        }
        M+=S[t]+1;
        rep(k,M+1) {
            sdp[k][0]=new_sdp[k][0];
            sdp[k][1]=new_sdp[k][1];
            //cout <<  k << " " << sdp[k][0] << " " << sdp[k][1] << endl;;
        }

    }

    rep(i,M+2){
        dp[s][i][0]=sdp[i][0];
        dp[s][i][1]=sdp[i][1];
    }

    // rep(i,S[s]+1){
    //     cout << s << "," << i << "," << 0 << " " << dp[s][i][0] << endl;
    //     cout << s << "," << i << "," << 1 << " " << dp[s][i][1] << endl;
    // }

}

void solve(){
    cin >> n;
    a.resize(n,0);
    G.resize(n,{});
    S.resize(n,0);
    rep(i,n) cin >> a[i];
    rep(i,n-1){
        int p,q;
        cin >> p >> q;
        G[p-1].push_back(q-1);
        G[q-1].push_back(p-1);
    }

    dfs(0,-1);
    ll ans=INF;
    rep(i,S[0]+1){
        if (dp[0][i][0]<0) ans=i;
        if (dp[0][i][1]<INF) ans=i;
        if (ans<INF) break;
    }

    cout << ans << endl;


    
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    solve();
}

Compiler output:
g++: internal compiler error: File size limit exceeded signal terminated program as
Please submit a full bug report, with preprocessed source (by using -freport-bug).
See <file:///usr/share/doc/gcc-14/README.Bugs> for instructions.