#include <bits/stdc++.h>
using namespace std;
#define rep(i , j , k) for (int i = j; i < (int)k; i++)
#define pb push_back
#define mt make_tuple
#define all(x) x.begin(),x.end()
typedef long long ll;
typedef pair<int , int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef complex<ld> point;
typedef pair<ld , ld> pld;
typedef vector<ll> vi;
inline void fileIO(string s) {
// freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
template<class T , class S>
inline bool smin(T &a, S b) { return (T)b < a ? a = b , 1 : 0; }
template<class T , class S>
inline bool smax(T &a, S b) { return a < (T)b ? a = b , 1 : 0; }
constexpr int N = 1e5 + 10;
constexpr int MOD = 1e9 + 7;
template<typename T>
inline T mod(T &v) { return v = (v % MOD + MOD) % MOD; }
template<typename S, typename T>
inline S add(S &l, T r) { return mod(l += r); }
int n;
ll dp_up[N][2][101], dp_down[N][2][101], v2, p[N], p2[N];
vi adj[N];
inline void update(ll A[][101] , ll B[][101] ,int so ,int sy) {
rep(j , 0 , v2) {
smax(A[0][j] , B[0][j]);
smax(A[0][j] , B[1][j] - p[so]);
if (j) {
smax(A[1][j] , B[0][j - 1] + p2[so]);
smax(A[1][j] , B[1][j - 1] + p2[so] - p[so]);
}
}
}
void dfs_down(int v, int par = 0) {
dp_down[v][1][0] = dp_up[v][1][0] = -1e18;
rep(i , 1 , v2)
dp_down[v][1][i] = dp_up[v][1][i] = p2[v];
for (auto e : adj[v])
if (e ^ par) {
dfs_down(e , v);
update(dp_down[v] , dp_down[e] , v , e);
}
}
ll junk[2][101];
void dfs_up(int v, int par = 0) {
auto f = [v , par]() -> void {
memcpy(junk , dp_up[v] , sizeof(dp_up[v]));
for (auto e : adj[v])
if (e ^ par) {
update(dp_up[e] , junk , e , v);
update(junk , dp_down[e] , v , e);
}
};
f();
reverse(all(adj[v]));
f();
for (auto e : adj[v])
if (e ^ par)
dfs_up(e , v);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> v2;
if (v2 == 0) return cout << 0 << endl, 0;
v2++;
rep(i , 1 , n + 1)
cin >> p[i];
rep(i , 1 , n) {
int u , v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
p2[u] += p[v];
p2[v] += p[u];
}
dfs_down(1);
dfs_up(1);
ll res = 0;
rep(i , 1, n + 1) {
smax(res , max(dp_up[i][1][v2 - 1] , dp_up[i][0][v2 - 1]));
smax(res , max(dp_down[i][1][v2 - 1] , dp_down[i][0][v2 - 1]));
}
cout << res << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2688 KB |
Output is correct |
2 |
Correct |
4 ms |
2688 KB |
Output is correct |
3 |
Correct |
4 ms |
2688 KB |
Output is correct |
4 |
Correct |
4 ms |
2816 KB |
Output is correct |
5 |
Correct |
4 ms |
2688 KB |
Output is correct |
6 |
Correct |
3 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2688 KB |
Output is correct |
2 |
Correct |
4 ms |
2688 KB |
Output is correct |
3 |
Correct |
4 ms |
2688 KB |
Output is correct |
4 |
Correct |
4 ms |
2816 KB |
Output is correct |
5 |
Correct |
4 ms |
2688 KB |
Output is correct |
6 |
Correct |
3 ms |
2688 KB |
Output is correct |
7 |
Correct |
8 ms |
6016 KB |
Output is correct |
8 |
Correct |
7 ms |
6016 KB |
Output is correct |
9 |
Correct |
6 ms |
6016 KB |
Output is correct |
10 |
Correct |
9 ms |
6016 KB |
Output is correct |
11 |
Correct |
7 ms |
5888 KB |
Output is correct |
12 |
Correct |
7 ms |
6016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
710 ms |
328936 KB |
Output is correct |
2 |
Correct |
732 ms |
330936 KB |
Output is correct |
3 |
Correct |
532 ms |
326376 KB |
Output is correct |
4 |
Correct |
409 ms |
326136 KB |
Output is correct |
5 |
Correct |
858 ms |
326264 KB |
Output is correct |
6 |
Correct |
904 ms |
326136 KB |
Output is correct |
7 |
Correct |
878 ms |
326136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2688 KB |
Output is correct |
2 |
Correct |
4 ms |
2688 KB |
Output is correct |
3 |
Correct |
4 ms |
2688 KB |
Output is correct |
4 |
Correct |
4 ms |
2816 KB |
Output is correct |
5 |
Correct |
4 ms |
2688 KB |
Output is correct |
6 |
Correct |
3 ms |
2688 KB |
Output is correct |
7 |
Correct |
8 ms |
6016 KB |
Output is correct |
8 |
Correct |
7 ms |
6016 KB |
Output is correct |
9 |
Correct |
6 ms |
6016 KB |
Output is correct |
10 |
Correct |
9 ms |
6016 KB |
Output is correct |
11 |
Correct |
7 ms |
5888 KB |
Output is correct |
12 |
Correct |
7 ms |
6016 KB |
Output is correct |
13 |
Correct |
710 ms |
328936 KB |
Output is correct |
14 |
Correct |
732 ms |
330936 KB |
Output is correct |
15 |
Correct |
532 ms |
326376 KB |
Output is correct |
16 |
Correct |
409 ms |
326136 KB |
Output is correct |
17 |
Correct |
858 ms |
326264 KB |
Output is correct |
18 |
Correct |
904 ms |
326136 KB |
Output is correct |
19 |
Correct |
878 ms |
326136 KB |
Output is correct |
20 |
Correct |
894 ms |
326064 KB |
Output is correct |
21 |
Correct |
4 ms |
2688 KB |
Output is correct |
22 |
Correct |
858 ms |
326300 KB |
Output is correct |
23 |
Correct |
406 ms |
326140 KB |
Output is correct |
24 |
Correct |
857 ms |
326136 KB |
Output is correct |
25 |
Correct |
523 ms |
326120 KB |
Output is correct |
26 |
Correct |
694 ms |
330980 KB |
Output is correct |
27 |
Correct |
721 ms |
330944 KB |
Output is correct |