Submission #469079

# Submission time Handle Problem Language Result Execution time Memory
469079 2021-08-30T16:05:04 Z Josia Chase (CEOI17_chase) C++14
40 / 100
4000 ms 18324 KB
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")


#include <bits/stdc++.h>

#define int int64_t

using namespace std;


vector<int> nearbyPigeons;
vector<int> pigeons;
vector<vector<int>> graph;
int crumbs;


void findNearPigeons(int v, int p) {
    if (p!=-1) nearbyPigeons[v] += pigeons[p];

    for (int i: graph[v]) {
        if (i==p) continue;
        nearbyPigeons[v] += pigeons[i];
        findNearPigeons(i, v);
    }
}




vector<int> options;        // all paths


map<int, int> localOptions; // one path for selecting crumbs


void findBestOptions(int v, int p) {
    int addToLocalOptions;
    if (p==-1) addToLocalOptions = nearbyPigeons[v];
    else addToLocalOptions = nearbyPigeons[v]-pigeons[p];

    for (int i: graph[v]) {
        if (i==p) continue;

        localOptions[addToLocalOptions]++;

        findBestOptions(i, v);

        localOptions[addToLocalOptions]--;
        if (localOptions[addToLocalOptions] == 0) localOptions.erase(addToLocalOptions);
    }


    localOptions[addToLocalOptions]++;

    int addToOptions = 0;

    auto pos = localOptions.end();
    auto limit = localOptions.begin();
    pos--;
    int used = crumbs;
    while (used>0 && pos!=limit) {
        addToOptions+=(*pos).first*min((*pos).second, used);
        used -= min((*pos).second, used);
        pos--;
    }

    if (used) {
        addToOptions+=(*pos).first*min((*pos).second, used);
        used -= min((*pos).second, used);
    }

    assert(used >= 0);

    options.push_back(addToOptions);


    localOptions[addToLocalOptions]--;
    if (localOptions[addToLocalOptions] == 0) localOptions.erase(addToLocalOptions);
}




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

    int n; cin >> n >> crumbs;

    pigeons.assign(n, 0);
    nearbyPigeons.assign(n, 0);

    for (int i = 0; i<n; i++) cin >> pigeons[i];


    graph.assign(n, vector<int>(0));

    for (int i = 1; i<n; i++) {
        int a, b; cin >> a >> b;
        a--; b--;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    findNearPigeons(0, -1);

    // for (int i: nearbyPigeons) {
    //     cout << i << " ";
    // }
    // cout << "\n";



    int best = 0;
    for (int i = 0; i<n; i++) {
        options.clear();
        findBestOptions(i, -1);

        // for (int i: options) {
        //     cout << i << " ";
        // }
        // cout << "\n";


        best = max(best, *max_element(options.begin(), options.end()));
        
    }

    cout << best << "\n";




    return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1141 ms 468 KB Output is correct
8 Correct 580 ms 488 KB Output is correct
9 Correct 176 ms 400 KB Output is correct
10 Correct 370 ms 332 KB Output is correct
11 Correct 361 ms 412 KB Output is correct
12 Correct 348 ms 404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 4078 ms 18324 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1141 ms 468 KB Output is correct
8 Correct 580 ms 488 KB Output is correct
9 Correct 176 ms 400 KB Output is correct
10 Correct 370 ms 332 KB Output is correct
11 Correct 361 ms 412 KB Output is correct
12 Correct 348 ms 404 KB Output is correct
13 Execution timed out 4078 ms 18324 KB Time limit exceeded
14 Halted 0 ms 0 KB -