Submission #596071

#TimeUsernameProblemLanguageResultExecution timeMemory
5960711binChase (CEOI17_chase)C++14
30 / 100
290 ms261500 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...