Submission #962221

# Submission time Handle Problem Language Result Execution time Memory
962221 2024-04-13T09:13:05 Z LucaIlie Chase (CEOI17_chase) C++17
100 / 100
579 ms 493352 KB
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1e5;
const int MAX_K = 100;
int k;
long long a[MAX_N + 1], b[MAX_N + 1];
long long dpUp[MAX_N + 1][MAX_K + 1][3], dpDown[MAX_N + 1][MAX_K + 1][3];
vector<int> adj[MAX_N + 1];

long long maxCost = 0;

void dfs( int u, int p ) {
    for ( int v: adj[u] ) {
        if ( v == p )
            continue;
        dfs( v, u );
    }

    for ( int x = 0; x <= k; x++ )
        dpUp[u][x][0] = dpUp[u][x][1] = dpUp[u][x][2] = dpDown[u][x][0] = dpDown[u][x][1] = dpDown[u][x][2] = 0;
    for ( int x = 1; x <= k; x++ )
        dpUp[u][x][0] = dpDown[u][x][0] = b[u];

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

        for ( int x = 1; x <= k - 1; x++ ) {
            maxCost = max( maxCost, dpUp[u][x][0] + dpDown[v][k - x][0] - a[u] );
            maxCost = max( maxCost, dpUp[u][x][0] + dpDown[v][k - x][1] );
            maxCost = max( maxCost, dpUp[u][x][0] + dpDown[v][k - x][2] );
            maxCost = max( maxCost, dpUp[u][x][1] + dpDown[v][k - x][0] - a[u] );
            maxCost = max( maxCost, dpUp[u][x][2] + dpDown[v][k - x][0] - a[u] );
            maxCost = max( maxCost, dpUp[u][x][1] + dpDown[v][k - x][1] );
            maxCost = max( maxCost, dpUp[u][x][2] + dpDown[v][k - x][1] );
            maxCost = max( maxCost, dpUp[u][x][1] + dpDown[v][k - x][2] );
            maxCost = max( maxCost, dpUp[u][x][2] + dpDown[v][k - x][2] );

            maxCost = max( maxCost, dpUp[v][x][0] + dpDown[u][k - x][0] - a[v] );
            maxCost = max( maxCost, dpUp[v][x][0] + dpDown[u][k - x][1] );
            maxCost = max( maxCost, dpUp[v][x][0] + dpDown[u][k - x][2] );
            maxCost = max( maxCost, dpUp[v][x][1] + dpDown[u][k - x][0] - a[v] );
            maxCost = max( maxCost, dpUp[v][x][2] + dpDown[u][k - x][0] - a[v] );
            maxCost = max( maxCost, dpUp[v][x][1] + dpDown[u][k - x][1] );
            maxCost = max( maxCost, dpUp[v][x][2] + dpDown[u][k - x][1] );
            maxCost = max( maxCost, dpUp[v][x][1] + dpDown[u][k - x][2] );
            maxCost = max( maxCost, dpUp[v][x][2] + dpDown[u][k - x][2] );
        }

        for ( int x = 1; x <= k; x++ ) {
            dpUp[u][x][0] = max( dpUp[u][x][0], max( max( dpUp[v][x - 1][0] + b[u] - a[v], dpUp[v][x - 1][1] + b[u] - a[v] ), dpUp[v][x - 1][2] + b[u] - a[v] ) );
            dpUp[u][x][1] = max( dpUp[u][x][1], dpUp[v][x][0] );
            dpUp[u][x][2] = max( dpUp[u][x][2], max( dpUp[v][x][1], dpUp[v][x][2] ));

            dpDown[u][x][0] = max( dpDown[u][x][0], max( max( dpDown[v][x - 1][0] + b[u] - a[u], dpDown[v][x - 1][1] + b[u] ), dpDown[v][x - 1][2] + b[u] ) );
            dpDown[u][x][1] = max( dpDown[u][x][1], dpDown[v][x][0] - a[u] );
            dpDown[u][x][2] = max( dpDown[u][x][2], max( dpDown[v][x][1], dpDown[v][x][2] ));
        }
    }

    maxCost = max( maxCost, dpUp[u][k][0] );
    maxCost = max( maxCost, dpUp[u][k][1] );
    maxCost = max( maxCost, dpUp[u][k][2] );
    maxCost = max( maxCost, dpDown[u][k][0] );
    maxCost = max( maxCost, dpDown[u][k][1] );
    maxCost = max( maxCost, dpDown[u][k][2] );
}

int main() {
    int n;

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

    dfs( 1, 0 );

    cout << maxCost << "\n";

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 2 ms 8796 KB Output is correct
3 Correct 2 ms 8804 KB Output is correct
4 Correct 1 ms 8796 KB Output is correct
5 Correct 2 ms 8796 KB Output is correct
6 Correct 2 ms 8796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 2 ms 8796 KB Output is correct
3 Correct 2 ms 8804 KB Output is correct
4 Correct 1 ms 8796 KB Output is correct
5 Correct 2 ms 8796 KB Output is correct
6 Correct 2 ms 8796 KB Output is correct
7 Correct 5 ms 12892 KB Output is correct
8 Correct 3 ms 12920 KB Output is correct
9 Correct 3 ms 12892 KB Output is correct
10 Correct 6 ms 12884 KB Output is correct
11 Correct 4 ms 12888 KB Output is correct
12 Correct 4 ms 12892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 579 ms 493292 KB Output is correct
2 Correct 502 ms 493352 KB Output is correct
3 Correct 427 ms 484356 KB Output is correct
4 Correct 245 ms 483920 KB Output is correct
5 Correct 524 ms 484240 KB Output is correct
6 Correct 497 ms 483920 KB Output is correct
7 Correct 519 ms 483904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 2 ms 8796 KB Output is correct
3 Correct 2 ms 8804 KB Output is correct
4 Correct 1 ms 8796 KB Output is correct
5 Correct 2 ms 8796 KB Output is correct
6 Correct 2 ms 8796 KB Output is correct
7 Correct 5 ms 12892 KB Output is correct
8 Correct 3 ms 12920 KB Output is correct
9 Correct 3 ms 12892 KB Output is correct
10 Correct 6 ms 12884 KB Output is correct
11 Correct 4 ms 12888 KB Output is correct
12 Correct 4 ms 12892 KB Output is correct
13 Correct 579 ms 493292 KB Output is correct
14 Correct 502 ms 493352 KB Output is correct
15 Correct 427 ms 484356 KB Output is correct
16 Correct 245 ms 483920 KB Output is correct
17 Correct 524 ms 484240 KB Output is correct
18 Correct 497 ms 483920 KB Output is correct
19 Correct 519 ms 483904 KB Output is correct
20 Correct 537 ms 483800 KB Output is correct
21 Correct 236 ms 483924 KB Output is correct
22 Correct 484 ms 483924 KB Output is correct
23 Correct 254 ms 483780 KB Output is correct
24 Correct 507 ms 484128 KB Output is correct
25 Correct 412 ms 483956 KB Output is correct
26 Correct 451 ms 493144 KB Output is correct
27 Correct 441 ms 493152 KB Output is correct