Submission #596071

# Submission time Handle Problem Language Result Execution time Memory
596071 2022-07-14T10:16:52 Z 1bin Chase (CEOI17_chase) C++14
30 / 100
290 ms 261500 KB
#include <bits/stdc++.h>
 
using namespace std;
 
#define all(v) v.begin(), v.end()
typedef long long ll;
const int NMAX = 1e5 + 5;
ll n, k, a[NMAX], u, v, s[NMAX][105], e[NMAX][105][2], ans;
vector<int> adj[NMAX];

void dfs(int x, int bef){
    ll sum = 0;
    for(int nx : adj[x]) sum += a[nx];
    
    s[x][1] = e[x][1][1] = sum;
    ans = max(ans, sum);
    for(int nx : adj[x]){
        if(nx == bef) continue;
        dfs(nx, x);
        for(int i = 1; i <= k; i++) {
            ans = max(ans, s[x][i] + max(e[nx][k - i][0], e[nx][k - i][1] - a[x]));
            ans = max(ans, max(e[x][i][0] + s[nx][k - i], e[x][i][1] + s[nx][k - i] - a[nx]));
            
            s[x][i] = max(s[x][i], s[x][i - 1]);
            s[x][i] = max({s[x][i], s[nx][i], s[nx][i - 1] + sum - a[nx]});
            e[x][i][0] = max(e[x][i][0], e[x][i - 1][0]);
            e[x][i][1] = max(e[x][i][1], e[x][i - 1][1]);
            e[x][i][0] = max(e[x][i][0], max(e[nx][i][0], e[nx][i][1] - a[x]));
            e[x][i][1] = max(e[x][i][1], max(e[nx][i - 1][0] + sum, e[nx][i - 1][1] + sum - a[x]));
        }
    }
    return;
}

int main(void){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  
    cin >> n >> k;
    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 0; i < n - 1; i++){
        cin >> u >> v;
        adj[u].emplace_back(v);
        adj[v].emplace_back(u);
    }
    dfs(1, 0);
    cout << ans;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2684 KB Output is correct
5 Incorrect 2 ms 2680 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2684 KB Output is correct
5 Incorrect 2 ms 2680 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 243 ms 261500 KB Output is correct
2 Correct 247 ms 261468 KB Output is correct
3 Correct 240 ms 255572 KB Output is correct
4 Correct 157 ms 255328 KB Output is correct
5 Correct 255 ms 255480 KB Output is correct
6 Correct 290 ms 255284 KB Output is correct
7 Correct 259 ms 255272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2684 KB Output is correct
5 Incorrect 2 ms 2680 KB Output isn't correct
6 Halted 0 ms 0 KB -