#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 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]);
}
}
}
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);
}
FORE(i, 1, N)
for (auto n : adj[i]) NEIG[i] += P[n];
// darr(P, N + 1);
// darr(NEIG, N + 1);
dfs(1, -1);
go(1, -1);
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Incorrect |
4 ms |
2684 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Incorrect |
4 ms |
2684 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
257 ms |
132296 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Incorrect |
4 ms |
2684 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |