Submission #585740

# Submission time Handle Problem Language Result Execution time Memory
585740 2022-06-29T09:41:18 Z tengiz05 Chase (CEOI17_chase) C++17
40 / 100
4000 ms 13948 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));
    };
    
    for (int i = 0; i < n; i++) {
        dfs(i, -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 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 566 ms 432 KB Output is correct
8 Correct 138 ms 456 KB Output is correct
9 Correct 57 ms 380 KB Output is correct
10 Correct 150 ms 380 KB Output is correct
11 Correct 147 ms 376 KB Output is correct
12 Correct 142 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 4059 ms 13948 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 566 ms 432 KB Output is correct
8 Correct 138 ms 456 KB Output is correct
9 Correct 57 ms 380 KB Output is correct
10 Correct 150 ms 380 KB Output is correct
11 Correct 147 ms 376 KB Output is correct
12 Correct 142 ms 384 KB Output is correct
13 Execution timed out 4059 ms 13948 KB Time limit exceeded
14 Halted 0 ms 0 KB -