이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define DEBUG(x) { cerr << #x << '=' << x << endl; }
#define Arr(a, l, r) { cerr << #a << " = {"; FOR(_, l, r) cerr << ' ' << a[_]; cerr << "}\n"; }
#define N 100100
#define pp pair<int, int>
#define endl '\n'
#define IO ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define taskname ""
#define bit(S, i) (((S) >> (i)) & 1)
void Max(long long &a, long long b) { a = a < b ? b : a; }
void Min(long long &a, long long b) { a = a < b ? a : b; }
using namespace std;
int n, k;
vector<int> e[N];
long long ans, a[N], DtoU[N][111], UtoD[N][111];
void dfs(int u, int p) {
long long sum = 0;
for (int v : e[u]) sum += a[v];
DtoU[u][1] = UtoD[u][1] = sum;
/*FOR(i, 1, k) {
// Choose u
if (i != 1 && p != -1) Max(UtoD[u][i], UtoD[p][]
}*/
for (int v : e[u]) if (v != p) {
dfs(v, u);
FOR(i, 1, k) {
// Choose u
if (i != 1) Max(DtoU[u][i], DtoU[v][i - 1] + sum - a[v]);
// Don't choose u
Max(DtoU[u][i], DtoU[v][i]);
}
}
}
int main() {
#ifdef NERO
freopen("test.inp","r",stdin);
freopen("test.out","w",stdout);
double stime = clock();
#else
//freopen(taskname".inp","r",stdin);
//freopen(taskname".out","w",stdout);
#endif //NERO
IO;
cin >> n >> k;
FOR(i, 1, n) cin >> a[i];
FOR(i, 2, n) {
int u, v;
cin >> u >> v;
e[u].push_back(v);
e[v].push_back(u);
}
if (k == 0) {
cout << 0;
return 0;
}
ans = 0;
FOR(root, 1, n) {
FOR(i, 1, n) FOR(j, 1, k) DtoU[i][j] = 0;
dfs(root, -1);
FOR(i, 1, k) Max(ans, DtoU[root][i]);
}
cout << ans;
#ifdef NERO
double etime = clock();
cerr << "\nExecution time: " << (etime - stime) / CLOCKS_PER_SEC * 1000 << " ms.\n";
#endif // NERO
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |