#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define FOR(i,start,end) for(int i=start;i<(int)(end);i++)
#define FORE(i,start,end) for(int i=start;i<=(int)end;i++)
#define RFOR(i,start,end) for(int i = start; i>end; i--)
#define RFORE(i,start,end) for(int i = start; i>=end; i--)
#define all(a) a.begin(), a.end()
#define mt make_tuple
#define mp make_pair
#define v vector
#define sf scanf
#define pf printf
#define dvar(x) cout << #x << " := " << x << "\n"
#define darr(x,n) FOR(i,0,n) cout << #x << "[" << i << "]" << " := " << x[i] << "\n"
typedef long long ll;
typedef long double ld;
typedef pair<int, int > pii;
typedef pair<ll, ll> pll;
//template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T> void minn(T &a, T b) { a = min(a, b); }
template<class T> void maxx(T &a, T b) { a = max(a, b); }
void io() {
#ifdef LOCAL_PROJECT
freopen("input.in", "r", stdin); freopen("output.out", "w", stdout);
#else
/* online submission */
#endif
ios_base::sync_with_stdio(false); cin.tie(NULL);
}
const ll MOD = 1000000007LL;
const ll PRIME = 105943LL;
const ll INF = 1e18;
/********************CEOI 2017 Day 2 #3******************************/
const int MAXN = 100099, MAXV = 101;
int N, V;
ll P[MAXN], NEIG[MAXN];
v<int> adj[MAXN];
ll up[MAXN][MAXV], down[MAXN][MAXV], ans = 0;
void dfs(int x, int p) {
for (auto c : adj[x]) if (c != p) {
dfs(c, x);
FORE(k, 1, V) {
maxx(down[x][k], max(down[c][k], down[c][k - 1] + NEIG[c] - P[x]));
maxx(up[x][k], max(up[c][k], up[c][k - 1] + NEIG[x] - P[c]));
}
}
maxx(up[x][1], NEIG[x]);
maxx(ans, max(down[x][V], up[x][V]));
}
void go(int x, int p) {
v<int> chi;
for (auto c : adj[x]) if (c != p) chi.push_back(c);
for (auto c : chi) {
maxx(ans, NEIG[x] + down[x][V - 1]);
}
for (auto c1 : chi) for (auto c2 : chi) if (c1 != c2) {
FORE(k1, 0, V) {
maxx(ans, up[c1][k1] + down[c2][V - k1]);
if (k1 <= V - 1)
maxx(ans, up[c1][k1] + down[c2][V - k1 - 1] + NEIG[x] - P[c1]);
}
}
for (auto c : chi) go(c, x);
}
int main() {
io();
cin >> N >> V;
FORE(i, 1, N) cin >> P[i];
FOR(i, 0, N - 1) {
int a, b; cin >> a >> b;
adj[a].push_back(b), adj[b].push_back(a);
NEIG[a] += P[b];
NEIG[b] += P[a];
}
// darr(P, N + 1);
// darr(NEIG, N + 1);
dfs(1, -1);
/* FORE(i, 1, N) {
FORE(k, 0, V)
pf("up[%d][%d] = %lld\n", i, k, up[i][k]);
}*/
/* FORE(i, 1, N) {
FORE(k, 0, V)
pf("down[%d][%d] = %lld\n", i, k, down[i][k]);
}*/
go(1, -1);
cout << ans << "\n";
}
Compilation message
chase.cpp: In function 'void go(int, int)':
chase.cpp:65:12: warning: unused variable 'c' [-Wunused-variable]
for (auto c : chi) {
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2684 KB |
Output is correct |
2 |
Correct |
3 ms |
2680 KB |
Output is correct |
3 |
Correct |
3 ms |
2680 KB |
Output is correct |
4 |
Incorrect |
3 ms |
2652 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2684 KB |
Output is correct |
2 |
Correct |
3 ms |
2680 KB |
Output is correct |
3 |
Correct |
3 ms |
2680 KB |
Output is correct |
4 |
Incorrect |
3 ms |
2652 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
306 ms |
135480 KB |
Output is correct |
2 |
Correct |
366 ms |
137592 KB |
Output is correct |
3 |
Execution timed out |
4035 ms |
89944 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2684 KB |
Output is correct |
2 |
Correct |
3 ms |
2680 KB |
Output is correct |
3 |
Correct |
3 ms |
2680 KB |
Output is correct |
4 |
Incorrect |
3 ms |
2652 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |