Submission #856343

# Submission time Handle Problem Language Result Execution time Memory
856343 2023-10-03T07:34:45 Z Cyanmond Chase (CEOI17_chase) C++17
100 / 100
1055 ms 185808 KB
#include <bits/stdc++.h>

using namespace std;

#define rep(i, l, r) for (int i = (l); i < (r); ++i)
#define per(i, l, r) for (int i = (r - 1); i >= l; --i)
#define ALL(x) (x).begin(), (x).end()

using i64 = long long;
constexpr i64 inf = 1ll << 60;

void main_() {
    int N, V;
    cin >> N >> V;
    if (V == 0) {
        cout << 0 << endl;
        return;
    }
    vector<i64> P(N);
    for (auto &e : P) cin >> e;
    vector<int> A(N - 1), B(N - 1);
    vector<vector<int>> tree(N);
    rep(i, 0, N - 1) {
        cin >> A[i] >> B[i];
        tree[--A[i]].push_back(--B[i]);
        tree[B[i]].push_back(A[i]);
    }

    i64 ans = 0;
    vector dpu(N, vector(V + 1, 0ll)), dpd(N, vector(V + 1, 0ll));
    auto dfs = [&](auto &&self, const int v, const int p) -> void {
        i64 pSum = 0;
        for (const int t : tree[v]) {
            pSum += P[t];
            if (t == p) continue;
            self(self, t, v);
        }

        // update
        dpu[v][1] = max(dpu[v][1], pSum);
        dpd[v][1] = max(dpd[v][1], pSum - (p == -1 ? 0 : P[p]));
        for (const int t : tree[v]) {
            if (t == p) continue;
            // dpu
            {
                rep(x, 0, V + 1) {
                    dpu[v][x] = max(dpu[v][x], dpu[t][x]);
                    if (x != V) {
                        dpu[v][x + 1] = max(dpu[v][x + 1], dpu[t][x] + pSum - P[t]);
                    }
                }
            }
            // dpd
            {
                rep(x, 0, V + 1) {
                    dpd[v][x] = max(dpd[v][x], dpd[t][x]);
                    if (x != V) {
                        dpd[v][x + 1] = max(dpd[v][x + 1], dpd[t][x] + pSum - (p == -1 ? 0 : P[p]));
                    }
                }
            }
        }

        rep(x, 0, V) {
            dpu[v][x + 1] = max(dpu[v][x + 1], dpu[v][x]);
            dpd[v][x + 1] = max(dpd[v][x + 1], dpd[v][x]);
        }

        // merge
        vector dp(V + 1, vector(2, 0ll));
        dp[1][0] = pSum;
        for (const int t : tree[v]) {
            if (t == p) continue;
            auto ndp = dp;
            rep(x, 0, V + 1) {
                ans = max(ans, dp[x][0] + dpd[t][V - x]);
                ans = max(ans, dp[x][1] + dpu[t][V - x]);
                if (x != V) ans = max(ans, dp[x][1] + dpu[t][V - x - 1] + pSum - P[t]);
            }
            rep(x, 0, V + 1) {
                ndp[x][0] = max(ndp[x][0], dpu[t][x]);
                if (x != V) {
                    ndp[x + 1][0] = max(ndp[x + 1][0], dpu[t][x] + pSum - P[t]);
                }
                ndp[x][1] = max(ndp[x][1], dpd[t][x]);
            }
            dp = move(ndp);
        }
    };
    dfs(dfs, 0, -1);

    /*
    rep(i, 0, N) {
        for (const auto e : dpu[i]) cerr << e << ' ';
        cerr << endl;
        for (const auto e : dpd[i]) cerr << e << ' ';
        cerr << endl;
        cerr << endl;
    }
    */

    cout << ans << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    main_();
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 10 ms 2140 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 10 ms 2140 KB Output is correct
11 Correct 2 ms 980 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 981 ms 183596 KB Output is correct
2 Correct 988 ms 183792 KB Output is correct
3 Correct 888 ms 172488 KB Output is correct
4 Correct 75 ms 18572 KB Output is correct
5 Correct 1055 ms 172112 KB Output is correct
6 Correct 1012 ms 172004 KB Output is correct
7 Correct 1038 ms 172112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 10 ms 2140 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 10 ms 2140 KB Output is correct
11 Correct 2 ms 980 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 981 ms 183596 KB Output is correct
14 Correct 988 ms 183792 KB Output is correct
15 Correct 888 ms 172488 KB Output is correct
16 Correct 75 ms 18572 KB Output is correct
17 Correct 1055 ms 172112 KB Output is correct
18 Correct 1012 ms 172004 KB Output is correct
19 Correct 1038 ms 172112 KB Output is correct
20 Correct 1045 ms 174100 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1004 ms 174100 KB Output is correct
23 Correct 78 ms 20560 KB Output is correct
24 Correct 1002 ms 174316 KB Output is correct
25 Correct 884 ms 174276 KB Output is correct
26 Correct 1000 ms 185808 KB Output is correct
27 Correct 1012 ms 185808 KB Output is correct