#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 10;
const int MAXC = 110;
vector<int> grafo[MAXN];
ll dp[MAXN][MAXC][2][2],vetor[MAXN],tamanho[MAXN],melhor_diferenca;
int N,C;
void solve(int v,int p){
for(int i = 0;i<=C;i++){
dp[v][i][0][0] = 0;
dp[v][i][0][1] = 0;
dp[v][i][1][0] = 0;
dp[v][i][1][1] = 0;
}
tamanho[v] = vetor[v];
for(int u : grafo[v]) tamanho[v] += vetor[u];
dp[v][1][1][0] = tamanho[v] - vetor[v];
for(int u : grafo[v]){
if(u == p) continue;
solve(u,v);
for(int i = 0;i<=C;i++){
if(i != 0){
dp[v][i][1][0] = max(dp[v][i][1][0], dp[u][i-1][0][0] + tamanho[v] - vetor[v] );
dp[v][i][1][0] = max(dp[v][i][1][0], dp[u][i-1][0][1] + tamanho[v] - vetor[v] - vetor[u] );
dp[v][i][1][1] = max(dp[v][i][1][1], dp[u][i-1][1][0] + tamanho[v] - vetor[v] - vetor[u] );
dp[v][i][1][1] = max(dp[v][i][1][1], dp[u][i-1][1][1] + tamanho[v] - vetor[v] - vetor[u] );
}
dp[v][i][0][0] = max(dp[v][i][0][0], max(dp[u][i][0][0],dp[u][i][0][1]));
dp[v][i][0][1] = max(dp[v][i][0][1], max(dp[u][i][1][1],dp[u][i][1][0]) );
}
}
for(int i = 0;i<=C;i++){
melhor_diferenca = max(melhor_diferenca,max(dp[v][i][0][0],dp[v][i][1][0]));
melhor_diferenca = max(melhor_diferenca,max(dp[v][i][0][1],dp[v][i][1][1]));
}
}
int main(){
cin >> N >> C;
for(int i = 1;i<=N;i++){
cin >> vetor[i];
}
for(int i = 1;i<N;i++){
int u,v;
cin >> u >> v;
grafo[u].push_back(v);
grafo[v].push_back(u);
}
int limite = N;
if(limite > 1000) limite = 1;
for(int i = 1;i<=limite;i++) solve(i,-1);
cout << melhor_diferenca << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2680 KB |
Output is correct |
2 |
Correct |
5 ms |
2920 KB |
Output is correct |
3 |
Correct |
4 ms |
2920 KB |
Output is correct |
4 |
Correct |
4 ms |
2920 KB |
Output is correct |
5 |
Correct |
5 ms |
2920 KB |
Output is correct |
6 |
Correct |
5 ms |
2920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2680 KB |
Output is correct |
2 |
Correct |
5 ms |
2920 KB |
Output is correct |
3 |
Correct |
4 ms |
2920 KB |
Output is correct |
4 |
Correct |
4 ms |
2920 KB |
Output is correct |
5 |
Correct |
5 ms |
2920 KB |
Output is correct |
6 |
Correct |
5 ms |
2920 KB |
Output is correct |
7 |
Incorrect |
1572 ms |
6420 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
674 ms |
355972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2680 KB |
Output is correct |
2 |
Correct |
5 ms |
2920 KB |
Output is correct |
3 |
Correct |
4 ms |
2920 KB |
Output is correct |
4 |
Correct |
4 ms |
2920 KB |
Output is correct |
5 |
Correct |
5 ms |
2920 KB |
Output is correct |
6 |
Correct |
5 ms |
2920 KB |
Output is correct |
7 |
Incorrect |
1572 ms |
6420 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |