# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
511852 |
2022-01-16T04:51:34 Z |
Monarchuwu |
Chase (CEOI17_chase) |
C++17 |
|
215 ms |
173400 KB |
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
const int N = 1e5 + 9, K = 100 + 2;
int n, V;
int p[N];
vector<int> g[N];
ll sum_p[N];
ll ans(0);
ll dp1[N][K]; // dp1[u][j]: đang ở u , có prenode = par, đã dùng j lượt
ll dp2[N][K]; // dp2[u][j]: nhảy 1 lượt đến par, có prenode = u , sẽ dùng j lượt
void dfs(int u, int par) {
for (int j = 0; j < V; ++j) {
ans = max(ans, dp1[u][j + 1]);
ans = max(ans, dp2[u][j] + sum_p[u]);
}
for (int v : g[u]) if (v != par) {
for (int j = 1; j <= V; ++j)
dp2[v][j] = max(dp2[u][j], dp2[u][j - 1] + sum_p[u] - p[v]);
}
for (int v : g[u]) if (v != par) {
for (int j = 1; j <= V; ++j) {
dp1[v][j] = max(dp1[u][j], dp1[u][j - 1] + sum_p[u] - p[par]);
}
dfs(v, u);
}
}
int main() {
cin.tie(NULL)->sync_with_stdio(false);
cin >> n >> V;
for (int i = 1; i <= n; ++i) cin >> p[i];
for (int i = 1, u, v; i < n; ++i) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int u = 1; u <= n; ++u)
for (int v : g[u]) sum_p[u] += p[v];
dfs(1, 0);
cout << ans << '\n';
}
/** /\_/\
* (= ._.)
* / >0 \>1
**/
/*
==================================================+
INPUT: |
--------------------------------------------------|
12 2
2 3 3 8 1 5 6 7 8 3 5 4
2 1
2 7
3 4
4 7
7 6
5 6
6 8
6 9
7 10
10 11
10 12
--------------------------------------------------|
==================================================+
OUTPUT: |
--------------------------------------------------|
36
--------------------------------------------------|
==================================================+
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2676 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2676 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2676 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2676 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
215 ms |
173400 KB |
Output is correct |
2 |
Correct |
165 ms |
173380 KB |
Output is correct |
3 |
Correct |
142 ms |
169116 KB |
Output is correct |
4 |
Correct |
144 ms |
168780 KB |
Output is correct |
5 |
Correct |
205 ms |
168776 KB |
Output is correct |
6 |
Correct |
187 ms |
168812 KB |
Output is correct |
7 |
Correct |
196 ms |
168788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2676 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2676 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |