이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
using namespace std;
#define endl '\n'
#define debug(x) cerr << #x << " == " << (x) << '\n';
#define all(x) begin(x), end(x)
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
template<typename T>
void chmin(T& x, T y) { x = min(x, y); }
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int N, M;
    cin >> N >> M;
    vector<int> D(N);
    for (auto& x : D) cin >> x;
    vector<vector<int>> E(N);
    for (int i = 0; i < N - 1; ++i) {
        int u, v;
        cin >> u >> v;
        --u, --v;
        E[u].push_back(v), E[v].push_back(u);
    }
    vector<ll> comp;
    vector<bool> vis(N, false);
    auto dfs = [&](auto&& self, int u, int p) -> vector<array<ll, 2>> {
        vis[u] = true;
        vector<array<ll, 2>> dp = {{0LL, LINF}, {LINF, LINF}};
        for (auto v : E[u]) if (v != p) {
            auto dpv = self(self, v, u);
            int k = (int)size(dp), l = (int)size(dpv);
            vector<array<ll, 2>> dpnxt(k + l - 1, {LINF, LINF});
            int add[4] = {D[u] + D[v], D[v], D[u], 0};
            int b[4] = {2, 1, 1, 0};
            for (int i = 0; i < k; ++i) {
                for (int j = 0; j < l; ++j) {
                    for (int mask = 0; mask < 4; ++mask) {
                        int s = mask & 1, t = mask >> 1 & 1;
                        chmin(dpnxt[i + j][s], dp[i][s] + dpv[j][t]);
                        if (i + j + b[mask] < k + l - 1) {
                            chmin(dpnxt[i + j + b[mask]][1], dp[i][s] + dpv[j][t] + add[mask]);
                        }
                    }
                }
            }
            swap(dp, dpnxt);
        }
        for (int i = 0; i < (int)size(dp); ++i) {
            comp[i] = min({comp[i], dp[i][0], dp[i][1]});
        }
        return dp;
    };
    vector<ll> dp(1, 0);
    for (int u = 0; u < N; ++u) {
        if (vis[u]) continue;
        comp.assign(N + 1, LINF);
        dfs(dfs, u, -1);
        comp.erase(lower_bound(2 + all(comp), LINF), end(comp));
        int k = (int)size(dp), l = (int)size(comp);
        vector<ll> dpnxt(k + l - 1, LINF);
        for (int j = l - 2; j >= 0; --j) {
            chmin(comp[j], comp[j + 1]);
        }
        for (int i = 0; i < k; ++i) {
            for (int j = 0; j < l; ++j) {
                chmin(dpnxt[i + j], dp[i] + comp[j]);
            }
        }
        swap(dp, dpnxt);
    }
    assert(is_sorted(all(dp)));
    int Q;
    cin >> Q;
    for (int z = 0; z < Q; ++z) {
        ll S;
        cin >> S;
        int ans = (int)distance(begin(dp), upper_bound(all(dp), S)) - 1;
        cout << ans << endl;
    }
    exit(0);
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |