Submission #202464

# Submission time Handle Problem Language Result Execution time Memory
202464 2020-02-16T08:30:30 Z PeppaPig Chase (CEOI17_chase) C++14
100 / 100
338 ms 262392 KB
#include <bits/stdc++.h>

#define long long long

using namespace std;

const int N = 1e5+5;

int n, k;
long A[N], S[N], ans;
long dp1[N][105], dp2[N][105]; //up, down
vector<int> g[N];

void dfs(int u, int p) {
    dp1[u][0] = dp2[u][0] = 0;
    dp1[u][1] = S[u];

    long mx1[105] = {0}, mx2[105] = {0};
    for(int v : g[u]) if(v != p) {
        dfs(v, u);
        for(int i = 1; i <= k; i++) {
            dp1[u][i] = max({dp1[u][i], dp1[v][i], dp1[v][i-1] + S[u] - A[v]});
            dp2[u][i] = max({dp2[u][i], dp2[v][i], dp2[v][i-1] + S[u] - A[p]});

            long val = dp2[v][i];
            if(val >= mx1[i]) swap(mx1[i], val);
            if(val >= mx2[i]) swap(mx2[i], val);
        }
    }
    for(int v : g[u]) if(v != p) {
        ans = max(ans, dp2[v][k-1] + S[u]);
        for(int i = 0; i <= k; i++) {
            long now = max(dp1[v][i], i ? dp1[v][i-1] + S[u] - A[v] : 0);
            if(dp2[v][k - i] != mx1[k - i]) ans = max(ans, now + mx1[k - i]);
            else ans = max(ans, now + mx2[k - i]);
        }
    }
}

int main() {
    scanf("%d %d", &n, &k);

    if(k == 0) return !printf("0\n");

    for(int i = 1; i <= n; i++) scanf("%lld", A + i);
    for(int i = 1, a, b; i < n; i++) {
        scanf("%d %d", &a, &b);
        g[a].emplace_back(b), g[b].emplace_back(a);
        S[a] += A[b], S[b] += A[a];
    }

    dfs(1, 0);
    printf("%lld\n", ans);

    return 0;
}

Compilation message

chase.cpp: In function 'int main()':
chase.cpp:41:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &n, &k);
     ~~~~~^~~~~~~~~~~~~~~~~
chase.cpp:45:38: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(int i = 1; i <= n; i++) scanf("%lld", A + i);
                                 ~~~~~^~~~~~~~~~~~~~~
chase.cpp:47:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &a, &b);
         ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2680 KB Output is correct
2 Correct 6 ms 2680 KB Output is correct
3 Correct 7 ms 2808 KB Output is correct
4 Correct 7 ms 2680 KB Output is correct
5 Correct 6 ms 2680 KB Output is correct
6 Correct 6 ms 2680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2680 KB Output is correct
2 Correct 6 ms 2680 KB Output is correct
3 Correct 7 ms 2808 KB Output is correct
4 Correct 7 ms 2680 KB Output is correct
5 Correct 6 ms 2680 KB Output is correct
6 Correct 6 ms 2680 KB Output is correct
7 Correct 9 ms 5240 KB Output is correct
8 Correct 9 ms 5240 KB Output is correct
9 Correct 8 ms 4348 KB Output is correct
10 Correct 10 ms 4472 KB Output is correct
11 Correct 8 ms 4472 KB Output is correct
12 Correct 8 ms 4472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 335 ms 259528 KB Output is correct
2 Correct 327 ms 261368 KB Output is correct
3 Correct 263 ms 174320 KB Output is correct
4 Correct 212 ms 174072 KB Output is correct
5 Correct 295 ms 174196 KB Output is correct
6 Correct 296 ms 174076 KB Output is correct
7 Correct 309 ms 174072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2680 KB Output is correct
2 Correct 6 ms 2680 KB Output is correct
3 Correct 7 ms 2808 KB Output is correct
4 Correct 7 ms 2680 KB Output is correct
5 Correct 6 ms 2680 KB Output is correct
6 Correct 6 ms 2680 KB Output is correct
7 Correct 9 ms 5240 KB Output is correct
8 Correct 9 ms 5240 KB Output is correct
9 Correct 8 ms 4348 KB Output is correct
10 Correct 10 ms 4472 KB Output is correct
11 Correct 8 ms 4472 KB Output is correct
12 Correct 8 ms 4472 KB Output is correct
13 Correct 335 ms 259528 KB Output is correct
14 Correct 327 ms 261368 KB Output is correct
15 Correct 263 ms 174320 KB Output is correct
16 Correct 212 ms 174072 KB Output is correct
17 Correct 295 ms 174196 KB Output is correct
18 Correct 296 ms 174076 KB Output is correct
19 Correct 309 ms 174072 KB Output is correct
20 Correct 299 ms 173944 KB Output is correct
21 Correct 6 ms 2680 KB Output is correct
22 Correct 304 ms 173996 KB Output is correct
23 Correct 206 ms 173948 KB Output is correct
24 Correct 299 ms 173996 KB Output is correct
25 Correct 256 ms 173936 KB Output is correct
26 Correct 338 ms 262392 KB Output is correct
27 Correct 325 ms 262392 KB Output is correct