Submission #1337692

#TimeUsernameProblemLanguageResultExecution timeMemory
133769212345678Džumbus (COCI19_dzumbus)C++20
110 / 110
30 ms17260 KiB
#include <bits/stdc++.h>

using namespace std;

#define ll long long

const int nx=1e3+5;

ll n, m, u, v, vl[nx], q, vs[nx], dp[nx][nx][2], newdp[nx][2], mx, x, sz[nx];
vector<ll> d[nx];
vector<pair<ll, ll>> ans;

void dfsbuild(int u, int p)
{
    vs[u]=1;
    for (auto v:d[u]) if (v!=p) dfsbuild(v, u);
}

void dfs(int u, int p)
{
    sz[u]=1;
    for (auto v:d[u])
    {
        if (v==p) continue;
        dfs(v, u);
        for (int i=0; i<=sz[u]+sz[v]; i++) newdp[i][0]=dp[u][i][0], newdp[i][1]=dp[u][i][1];
        for (int i=0; i<=sz[u]; i++)
        {
            for (int j=0; j<=sz[v]; j++)
            {
                newdp[i+j][0]=min(newdp[i+j][0], dp[u][i][0]+min(dp[v][j][0], dp[v][j][1]));
                newdp[i+j][1]=min(newdp[i+j][1], dp[u][i][1]+min(dp[v][j][0], dp[v][j][1]));
                if (i) newdp[i+j][1]=min(newdp[i+j][1], dp[u][i-1][0]+vl[u]+dp[v][j][1]);
                if (j) newdp[i+j][1]=min(newdp[i+j][1], dp[u][i][1]+dp[v][j-1][0]+vl[v]);
                if (i&&j) newdp[i+j][1]=min(newdp[i+j][1], dp[u][i-1][0]+vl[u]+dp[v][j-1][0]+vl[v]);
            }
        }
        for (int i=0; i<=sz[u]+sz[v]; i++) dp[u][i][0]=newdp[i][0], dp[u][i][1]=newdp[i][1];
        sz[u]+=sz[v];
    }
    // cout<<"DP "<<u<<'\n';
    // for (int i=0; i<=sz[u]; i++) cout<<i<<" : "<<dp[u][i][0]<<' '<<dp[u][i][1]<<'\n';
}

int main()
{
    cin.tie(NULL)->sync_with_stdio(false);
    cin>>n>>m;
    for (int i=1; i<=n; i++) cin>>vl[i];
    vl[0]=1e18;
    for (int i=0; i<m; i++) cin>>u>>v, d[u].push_back(v), d[v].push_back(u);
    for (int i=1; i<=n; i++) if (!vs[i]) d[0].push_back(i), dfsbuild(i, i);
    for (int i=0; i<=n; i++) for (int j=0; j<=n; j++) dp[i][j][0]=dp[i][j][1]=1e18;
    for (int i=0; i<=n; i++) dp[i][0][0]=0;
    dfs(0, 0);
    for (int i=0; i<=n; i++) ans.push_back({dp[0][i][0], i});
    sort(ans.begin(), ans.end());
    for (auto &x:ans) mx=max(mx, x.second), x.second=mx;
    cin>>q;
    while (q--) cin>>x, cout<<prev(upper_bound(ans.begin(), ans.end(), make_pair(x, LLONG_MAX)))->second<<'\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...