#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define fi first
#define se second
#define ll long long
#define pb push_back
#define ii pair<int,int>
template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int nax=1e5+5;
const int MOD=1e9+9;
#define optimise ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int tab[nax];
long long sum[nax];
long long dp[nax][105];
vector<pair<int,ll>> adj[nax];
long long dfs(int x,int lvl,int p){
if(dp[x][lvl]!=0) return dp[x][lvl];
if(lvl==0) return 0;
dp[x][lvl]=0;
for(auto u:adj[x]){
if(u.fi==p) continue;
dp[x][lvl]=max(dp[x][lvl],u.se+dfs(u.fi,lvl-1,x));
dp[x][lvl]=max(dp[x][lvl],dfs(u.fi,lvl,x));
}
return dp[x][lvl];
}
ll ans=0;
int main()
{
optimise;
int n,k;
cin>>n>>k;
for (int i = 0; i < n; ++i)
{
cin>>tab[i];
}
for (int i = 0; i < n-1; ++i)
{
int x,y;
cin>>x>>y;
x--;y--;
sum[x]+=tab[y];
sum[y]+=tab[x];
adj[x].pb({y,0});
adj[y].pb({x,0});
}
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < adj[i].size(); ++j)
{
adj[i][j].se=sum[adj[i][j].fi]-tab[i];
}
}
if(n>1000){
ans=max(dfs(0,k,-1),dfs(0,k-1,-1)+sum[0]);
}else{
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
memset(dp[j],0,sizeof dp[j]);
}
ans=max(ans,max(dfs(i,k,-1),dfs(i,k-1,-1)+sum[i]));
}
}
cout <<ans<<endl;
}
Compilation message
chase.cpp: In function 'int main()':
chase.cpp:54:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for (int j = 0; j < adj[i].size(); ++j)
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2648 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Runtime error |
4 ms |
5192 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2648 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Runtime error |
4 ms |
5192 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
535 ms |
99924 KB |
Output is correct |
2 |
Correct |
519 ms |
99920 KB |
Output is correct |
3 |
Correct |
63 ms |
92864 KB |
Output is correct |
4 |
Correct |
74 ms |
93256 KB |
Output is correct |
5 |
Correct |
228 ms |
93264 KB |
Output is correct |
6 |
Correct |
201 ms |
93264 KB |
Output is correct |
7 |
Correct |
247 ms |
93268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2648 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Runtime error |
4 ms |
5192 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |