// clang-format off
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;
// template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef XOX
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; }
sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; }
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
struct { template <class T> operator T() { T x; cin >> x; return x; } } in;
#define endl '\n'
#define pb emplace_back
#define vt vector
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using i64 = long long;
// #define int long long
// clang-format on
template<typename T>
void ckmax(T& a, T b) { a = max(a, b); }
int32_t main()
{
int n = in, v = in;
vector<int> c(n + 1);
for(int i = 1; i <= n; i++) c[i] = in;
vector<vector<int>> g(n + 1);
vector<i64> ile(n + 1);
for(int i = 1; i < n; i++) {
int a = in, b = in;
g[a].emplace_back(b), g[b].emplace_back(a);
ile[a] += c[b]; ile[b] += c[a];
}
i64 ans = 0;
vector<vector<i64>> dpup(n + 1, vector<i64>(v + 1)), dpdown(n + 1, vector<i64>(v + 1));
function<void(int, int)> dfs = [&](int u, int p) {
dpup[u][1] = ile[u];
vector<i64> przepisz_up(v + 1), przepisz_down(v + 1);
for(int to : g[u]) {
if(to == p) continue;
dfs(to, u);
for(int j = 1; j <= v; j++) {
i64 dist_up = max(dpup[to][j], dpup[to][j - 1] + ile[u] - c[to]);
i64 dist_down = max(dpdown[to][j], dpdown[to][j - 1] + ile[to] - c[u]);
przepisz_up[j] = dist_up; przepisz_down[j] = dist_down;
if(j != v) {
ckmax(ans, dpup[u][v - j] + dist_down);
ckmax(ans, dpdown[u][v - j] + dist_up);
ckmax(ans, dpdown[to][j] + ile[u]);
}
}
for(int j = 1; j <= v; j++) {
ckmax(dpup[u][j], przepisz_up[j]); ckmax(dpdown[u][j], przepisz_down[j]);
ckmax(ans, dpup[u][j]);
}
}
};
if(v == 0) { cout << 0; exit(0); }
dfs(1, 0); cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
3 ms |
2900 KB |
Output is correct |
8 |
Correct |
2 ms |
612 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
2004 KB |
Output is correct |
11 |
Correct |
2 ms |
832 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
311 ms |
262236 KB |
Output is correct |
2 |
Correct |
305 ms |
263864 KB |
Output is correct |
3 |
Correct |
207 ms |
173868 KB |
Output is correct |
4 |
Correct |
131 ms |
20068 KB |
Output is correct |
5 |
Correct |
292 ms |
173620 KB |
Output is correct |
6 |
Correct |
313 ms |
173696 KB |
Output is correct |
7 |
Correct |
316 ms |
173616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
3 ms |
2900 KB |
Output is correct |
8 |
Correct |
2 ms |
612 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
2004 KB |
Output is correct |
11 |
Correct |
2 ms |
832 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
311 ms |
262236 KB |
Output is correct |
14 |
Correct |
305 ms |
263864 KB |
Output is correct |
15 |
Correct |
207 ms |
173868 KB |
Output is correct |
16 |
Correct |
131 ms |
20068 KB |
Output is correct |
17 |
Correct |
292 ms |
173620 KB |
Output is correct |
18 |
Correct |
313 ms |
173696 KB |
Output is correct |
19 |
Correct |
316 ms |
173616 KB |
Output is correct |
20 |
Correct |
332 ms |
173696 KB |
Output is correct |
21 |
Correct |
82 ms |
20092 KB |
Output is correct |
22 |
Correct |
297 ms |
173468 KB |
Output is correct |
23 |
Correct |
161 ms |
20052 KB |
Output is correct |
24 |
Correct |
297 ms |
173584 KB |
Output is correct |
25 |
Correct |
194 ms |
173380 KB |
Output is correct |
26 |
Correct |
331 ms |
264980 KB |
Output is correct |
27 |
Correct |
297 ms |
265000 KB |
Output is correct |