Submission #930142

# Submission time Handle Problem Language Result Execution time Memory
930142 2024-02-18T16:37:56 Z Regulus Chase (CEOI17_chase) C++17
0 / 100
319 ms 341920 KB
#include <bits/stdc++.h>                // chase_ceoi17
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define debug(x) cerr << #x << " = " << (x) << ' '
#define bug(x) cerr << (x) << ' '
#define endl cerr << '\n'
#define all(v) (v).begin(), (v).end()
#define SZ(v) (ll)(v).size()
#define lowbit(x) (x)&-(x)
#define pb emplace_back
#define F first
#define S second
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;

const int N = 1e5+5;
const ll LLINF = 2e18;
int m, a[N];
ll sum[N], ans, dp[N][2][105], dp2[N][2][105];
vector<int> g[N];

inline void dfs(int x, int fa)
{
    for (int v : g[x]) sum[x] += a[v];

    dp[x][1][0] = dp2[x][1][0] = -LLINF;
    //dp[x][1][i] = dp2[x][1][i] = sum[x];
    for (int i=1; i <= m; ++i) dp[x][1][i] = dp2[x][1][i] = sum[x];
    for (int v : g[x])
    {
        if (v == fa) continue;
        dfs(v, x);
        for (int t=0; t < 2; ++t)
        {
            for (int i=0; i <= m; ++i)
            {
                if (m-i-1 < 0) continue;
                ans = max(ans, dp[x][1][m-i-1] + dp2[v][t][i] - a[x]);
                ans = max(ans, dp2[x][1][m-i-1] + dp[v][t][i]);
            }
        }

        for (int i=0; i <= m; ++i)
        {
            dp[x][0][i] = max({dp[x][0][i], dp[v][0][i], dp[v][1][i]});
            dp2[x][0][i] = max({dp2[x][0][i], dp2[v][0][i], dp2[v][1][i]});
            if (i > 0)
            {
                dp[x][1][i] = max(dp[x][1][i], max(dp[v][0][i-1], dp[v][1][i-1]) + sum[x] - a[v]);
                dp2[x][1][i] = max(dp2[x][1][i], max(dp2[v][0][i-1], dp2[v][1][i-1]) + sum[x] - a[x]);
            }
        }
    }
    ans = max({ans, dp[x][1][m], dp2[x][1][m]});

    /*debug(x), endl;
    for (int t=0; t < 2; ++t)
    {
        for (int i=0; i <= m; ++i) bug(dp[x][t][i]); endl;
        for (int i=0; i <= m; ++i) bug(dp2[x][t][i]); endl;
    } endl;*/
}

int main(void)
{ IO
    int n, i;

    cin >> n >> m;
    for (i=1; i <= n; ++i) cin >> a[i];
    for (i=0; i < n-1; ++i)
    {
        int x, y; cin >> x >> y;
        g[x].pb(y), g[y].pb(x);
    }

    dfs(1, 0);
    cout << ans << '\n';


    return 0;
}

/*
12 2
2 3 3 8 1 5 5 7 8 3 5 4
2 1
2 7
3 4
4 7
7 6
5 6
6 8
6 9
7 10
10 11
10 12

*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6748 KB Output is correct
2 Incorrect 1 ms 6748 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6748 KB Output is correct
2 Incorrect 1 ms 6748 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 319 ms 341920 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6748 KB Output is correct
2 Incorrect 1 ms 6748 KB Output isn't correct
3 Halted 0 ms 0 KB -