Submission #119400

# Submission time Handle Problem Language Result Execution time Memory
119400 2019-06-21T07:36:02 Z zubec Chase (CEOI17_chase) C++14
100 / 100
440 ms 265592 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;

const int N = 100100;

ll dp[N][110], dp2[N][110], a[N];

vector <int> g[N];

int n, k;

ll ans = 0;

void dfs(int v, int p){

    ll mx1[101] = {0}, mx2[101] = {0};

    ll sum = 0;
    for (int i = 0; i < g[v].size(); i++){
        sum += a[g[v][i]];
    }
    dp[v][0] = dp2[v][0] = 0;
    dp2[v][1] = sum;
    for (int i = 0; i < g[v].size(); i++){
        int to = g[v][i];
        if (to == p)
            continue;
        dfs(to, v);
        for (int j = 1; j <= k; j++){
            dp[v][j] = max(dp[v][j], max(dp[to][j], dp[to][j-1] + sum - a[p]));
            dp2[v][j] = max(dp2[v][j], max(dp2[to][j], dp2[to][j-1] + sum - a[to]));
            if (dp[to][j] >= mx1[j]){
                mx2[j] = mx1[j];
                mx1[j] = dp[to][j];
            } else {
                mx2[j] = max(mx2[j], dp[to][j]);
            }
        }
    }
    for (int i = 0; i < g[v].size(); i++){
        int to = g[v][i];
        if (to == p)
            continue;
        ans = max(ans, sum + dp[to][k-1]);
        for (int j = 0; j <= k; j++){
            ll mx = max(dp2[to][j], (j == 0 ? 0 : dp2[to][j-1] + sum - a[to]));
            if (mx1[k-j] == dp[to][k-j]){
                ans = max(ans, mx + mx2[k-j]);
            } else {
                ans = max(ans, mx + mx1[k-j]);
            }
        }
    }
}


int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);

    cin >> n >> k;
    for (int i = 1; i <= n; i++){
        cin >> a[i];
    }
    for (int i = 2; i <= n; i++){
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    if (k == 0)
        return cout << 0, 0;



    dfs(1, 0);

    cout << ans;

}

/**



*/

Compilation message

chase.cpp: In function 'void dfs(int, int)':
chase.cpp:21:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < g[v].size(); i++){
                     ~~^~~~~~~~~~~~~
chase.cpp:26:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < g[v].size(); i++){
                     ~~^~~~~~~~~~~~~
chase.cpp:42:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < g[v].size(); i++){
                     ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2816 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
3 Correct 4 ms 2688 KB Output is correct
4 Correct 4 ms 2688 KB Output is correct
5 Correct 4 ms 2688 KB Output is correct
6 Correct 4 ms 2688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2816 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
3 Correct 4 ms 2688 KB Output is correct
4 Correct 4 ms 2688 KB Output is correct
5 Correct 4 ms 2688 KB Output is correct
6 Correct 4 ms 2688 KB Output is correct
7 Correct 7 ms 5376 KB Output is correct
8 Correct 6 ms 5376 KB Output is correct
9 Correct 7 ms 4480 KB Output is correct
10 Correct 8 ms 4480 KB Output is correct
11 Correct 6 ms 4480 KB Output is correct
12 Correct 6 ms 4480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 403 ms 265060 KB Output is correct
2 Correct 377 ms 264476 KB Output is correct
3 Correct 295 ms 181460 KB Output is correct
4 Correct 243 ms 181112 KB Output is correct
5 Correct 334 ms 181112 KB Output is correct
6 Correct 324 ms 181240 KB Output is correct
7 Correct 387 ms 181244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2816 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
3 Correct 4 ms 2688 KB Output is correct
4 Correct 4 ms 2688 KB Output is correct
5 Correct 4 ms 2688 KB Output is correct
6 Correct 4 ms 2688 KB Output is correct
7 Correct 7 ms 5376 KB Output is correct
8 Correct 6 ms 5376 KB Output is correct
9 Correct 7 ms 4480 KB Output is correct
10 Correct 8 ms 4480 KB Output is correct
11 Correct 6 ms 4480 KB Output is correct
12 Correct 6 ms 4480 KB Output is correct
13 Correct 403 ms 265060 KB Output is correct
14 Correct 377 ms 264476 KB Output is correct
15 Correct 295 ms 181460 KB Output is correct
16 Correct 243 ms 181112 KB Output is correct
17 Correct 334 ms 181112 KB Output is correct
18 Correct 324 ms 181240 KB Output is correct
19 Correct 387 ms 181244 KB Output is correct
20 Correct 339 ms 181180 KB Output is correct
21 Correct 62 ms 8824 KB Output is correct
22 Correct 327 ms 181196 KB Output is correct
23 Correct 242 ms 181212 KB Output is correct
24 Correct 418 ms 181216 KB Output is correct
25 Correct 313 ms 181044 KB Output is correct
26 Correct 440 ms 265592 KB Output is correct
27 Correct 431 ms 265584 KB Output is correct