Submission #585742

# Submission time Handle Problem Language Result Execution time Memory
585742 2022-06-29T09:43:20 Z tengiz05 Chase (CEOI17_chase) C++17
70 / 100
572 ms 16076 KB
#include <bits/stdc++.h>

using i64 = long long;
using namespace std;

void solve() {
    int n, k;
    cin >> n >> k;
    
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    
    vector<vector<int>> e(n);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        e[u].push_back(v);
        e[v].push_back(u);
    }
    
    multiset<i64> q;
    
    i64 ans = 0;
    
    function<void(int, int)> dfs = [&](int u, int p) {
        i64 s = 0;
        for (int v : e[u]) {
            if (v != p) {
                s += a[v];
            }
        }
        q.insert(s);
        i64 res = 0;
        auto it = q.end();
        for (int i = 0; i < min(k, int(q.size())); i++) {
            it = prev(it);
            res += *it;
        }
        ans = max(ans, res);
        for (int v : e[u]) {
            if (v != p) {
                dfs(v, u);
            }
        }
        q.erase(q.find(s));
    };
    
    if (n <= 1000) {
        for (int i = 0; i < n; i++) {
            dfs(i, -1);
        }
    } else {
        dfs(0, -1);
    }
    
    cout << ans << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int t = 1;
    // cin >> t;
    
    while (t--) {
        solve();
    }
    
    return 0;
}

/*

12 2
2 3 3 8 1 5 6 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 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 572 ms 432 KB Output is correct
8 Correct 138 ms 440 KB Output is correct
9 Correct 49 ms 368 KB Output is correct
10 Correct 152 ms 360 KB Output is correct
11 Correct 155 ms 356 KB Output is correct
12 Correct 142 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 147 ms 14012 KB Output is correct
2 Correct 150 ms 16076 KB Output is correct
3 Correct 45 ms 8636 KB Output is correct
4 Correct 50 ms 8296 KB Output is correct
5 Correct 68 ms 8348 KB Output is correct
6 Correct 78 ms 8384 KB Output is correct
7 Correct 76 ms 8380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 572 ms 432 KB Output is correct
8 Correct 138 ms 440 KB Output is correct
9 Correct 49 ms 368 KB Output is correct
10 Correct 152 ms 360 KB Output is correct
11 Correct 155 ms 356 KB Output is correct
12 Correct 142 ms 364 KB Output is correct
13 Correct 147 ms 14012 KB Output is correct
14 Correct 150 ms 16076 KB Output is correct
15 Correct 45 ms 8636 KB Output is correct
16 Correct 50 ms 8296 KB Output is correct
17 Correct 68 ms 8348 KB Output is correct
18 Correct 78 ms 8384 KB Output is correct
19 Correct 76 ms 8380 KB Output is correct
20 Incorrect 66 ms 8376 KB Output isn't correct
21 Halted 0 ms 0 KB -