답안 #1011550

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1011550 2024-06-30T15:15:39 Z NValchanov Chase (CEOI17_chase) C++17
20 / 100
4000 ms 188588 KB
#include <bits/stdc++.h>

#define endl '\n'

using namespace std;

typedef long long ll;

const int MAXN = 1e5 + 10;
const int MAXK = 1e2 + 10;

int n, k;
vector < int > adj[MAXN];
ll c[MAXN];
ll ans;
ll dp[MAXN][MAXK][2];

void read()
{
    cin >> n >> k;
    for(int i = 1; i <= n; i++)
    {
        cin >> c[i];
    }
    for(int i = 1; i <= n - 1; i++)
    {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
}

void dfs(int u, int par)
{
    ll sum = 0;
    for(int v : adj[u])
        sum += c[v];

    vector < int > nb;
    dp[u][1][1] = sum;

    for(int v : adj[u])
    {
        if(v == par)
            continue;

        dfs(v, u);

        nb.push_back(v);
        for(int i = 1; i <= k; i++)
        {
            ans = max(ans, dp[u][i][1] + dp[v][k - i][0]);
        }
        for(int i = 1; i <= k; i++)
        {
            dp[u][i][0] = max({dp[u][i][0], dp[v][i][0], dp[v][i - 1][0] + sum - c[par]});
            dp[u][i][1] = max({dp[u][i][1], dp[v][i][1], dp[v][i - 1][1] + sum - c[v]});
        }
    }

    /// after traversing children

    for(int i = 1; i <= n; i++)
    {
        dp[u][i][1] = 0;
    }

    dp[u][1][1] = sum;
    reverse(nb.begin(), nb.end());

    for(int v : nb)
    {
        for(int i = 1; i <= k; i++)
        {
            ans = max(ans, dp[u][i][1] + dp[v][k - i][0]);
        }
        for(int i = 1; i <= k; i++)
        {
            dp[u][i][1] = max({dp[u][i][1], dp[v][i][1], dp[v][i - 1][1] + sum - c[v]});
        }
    }
}

int main()
{
    ios_base :: sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    read();
    dfs(1, 0);

    cout << ans << endl;

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2792 KB Output is correct
7 Correct 3 ms 4444 KB Output is correct
8 Incorrect 4 ms 4696 KB Output isn't correct
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4046 ms 188588 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2792 KB Output is correct
7 Correct 3 ms 4444 KB Output is correct
8 Incorrect 4 ms 4696 KB Output isn't correct
9 Halted 0 ms 0 KB -