답안 #523655

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
523655 2022-02-08T03:01:26 Z KoD Chase (CEOI17_chase) C++17
100 / 100
184 ms 102276 KB
#include <bits/stdc++.h>

using std::vector;
using std::array;
using std::pair;
using std::tuple;

using i64 = std::int64_t;

template <class F> struct RecLambda : private F {
    explicit RecLambda(F&& f) : F(std::forward<F>(f)) {}
    template <class... Args> decltype(auto) operator()(Args&&... args) const {
        return F::operator()(*this, std::forward<Args>(args)...);
    } 
};

template <class T> void setmax(T& x, const T& y) {
    if (x < y) x = y;
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int N, K;
    std::cin >> N >> K;
    if (K == 0) {
        std::cout << 0 << '\n';
        return 0;
    }
    vector<int> W(N);
    for (auto& x : W) {
        std::cin >> x;
    }
    vector<vector<int>> graph(N);
    for (int i = 0; i < N - 1; ++i) {
        int a, b;
        std::cin >> a >> b;
        a -= 1, b -= 1;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }
    vector<i64> C(N);
    for (int i = 0; i < N; ++i) {
        for (const int j : graph[i]) {
            C[i] += W[j];
        }
    }
    i64 ans = 0;
    RecLambda([&](auto&& dfs, const int u, const int p) -> pair<vector<i64>, vector<i64>> {
        vector<i64> begin(K + 1), end(K + 1);
        begin[1] = end[1] = C[u];
        for (const int v : graph[u]) {
            if (v == p) continue;
            const auto [v_begin, v_end] = dfs(v, u);
            for (int i = 1; i < K; ++i) {
                setmax(ans, end[i] + v_begin[K - i] - W[u]);
                setmax(ans, end[i] + v_begin[K - i + 1] - C[v]);
                setmax(ans, begin[i] + v_end[K - i] - W[v]);
                setmax(ans, begin[i + 1] + v_end[K - i] - C[u]);
            }
            for (int i = 0; i < K; ++i) {
                setmax(begin[i + 1], C[u] + v_begin[i + 1] - C[v]);
                setmax(begin[i + 1], C[u] + v_begin[i] - W[u]);
                setmax(end[i], v_end[i]);
                setmax(end[i + 1], v_end[i] + C[u] - W[v]);
            }
            setmax(end[K], v_end[K]);
        }
        for (int i = 0; i < K; ++i) {
            setmax(begin[i + 1], begin[i]);
            setmax(end[i + 1], end[i]);
        }
        setmax(ans, begin[K]);
        setmax(ans, end[K]);
        return std::pair(std::move(begin), std::move(end));
    })(0, -1);
    std::cout << ans << '\n';
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 312 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 312 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 2 ms 1216 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 184 ms 101416 KB Output is correct
2 Correct 177 ms 100916 KB Output is correct
3 Correct 120 ms 9380 KB Output is correct
4 Correct 54 ms 9248 KB Output is correct
5 Correct 172 ms 9252 KB Output is correct
6 Correct 139 ms 9260 KB Output is correct
7 Correct 124 ms 9152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 312 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 2 ms 1216 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 324 KB Output is correct
13 Correct 184 ms 101416 KB Output is correct
14 Correct 177 ms 100916 KB Output is correct
15 Correct 120 ms 9380 KB Output is correct
16 Correct 54 ms 9248 KB Output is correct
17 Correct 172 ms 9252 KB Output is correct
18 Correct 139 ms 9260 KB Output is correct
19 Correct 124 ms 9152 KB Output is correct
20 Correct 127 ms 9148 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 146 ms 9280 KB Output is correct
23 Correct 55 ms 9156 KB Output is correct
24 Correct 170 ms 9212 KB Output is correct
25 Correct 117 ms 9020 KB Output is correct
26 Correct 172 ms 102272 KB Output is correct
27 Correct 171 ms 102276 KB Output is correct