제출 #585610

#제출 시각아이디문제언어결과실행 시간메모리
585610talant117408Chase (CEOI17_chase)C++17
70 / 100
300 ms95528 KiB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
 
#define pb                  push_back
#define mp                  make_pair
#define all(v)              (v).begin(),(v).end()
#define rall(v)             (v).rbegin(),(v).rend()
#define lb                  lower_bound
#define ub                  upper_bound
#define sz(v)               int((v).size())
#define do_not_disturb      ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl                '\n'

const int N = 1e5 + 7;
ll pigeons[N], subtree[N];
int n, bread;
vector <int> graph[N];
ll dp[N][103];

void dfs(int v, int p) {
    subtree[v] = 0;
    for (auto to : graph[v]) {
        if (to == p) continue;
        subtree[v] += pigeons[to];
        dfs(to, v);
        for (int j = 0; j <= bread; j++) {
            dp[v][j] = max(dp[v][j], dp[to][j]);
        }
    }
    for (auto to : graph[v]) {
        if (to == p) continue;
        for (int j = 1; j <= bread; j++) {
            dp[v][j] = max(dp[v][j], dp[to][j - 1] + subtree[v]);
        }
    }
}

void solve() {
    cin >> n >> bread;
    for (int i = 1; i <= n; i++) {
        cin >> pigeons[i];
    }
    for (int i = 0; i < n - 1; i++) {
        int a, b;
        cin >> a >> b;
        graph[a].pb(b);
        graph[b].pb(a);
    }
    ll ans = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            for (int k = 0; k <= bread; k++) {
                dp[j][k] = -2e18;
            }
            dp[j][0] = 0;
        }
        dfs(i, i);
        for (int j = 0; j <= bread; j++) {
            ans = max(ans, dp[i][j]);
        }
        if (n > 1000) break;
    }
    cout << ans << endl;
}

/*
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
*/

int main() {
    do_not_disturb
    
    int t = 1;
    //~ cin >> t;
    while (t--) {
        solve();
    }
    
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...