Submission #83078

#TimeUsernameProblemLanguageResultExecution timeMemory
83078luckyboyDostavljač (COCI18_dostavljac)C++14
140 / 140
282 ms2696 KiB
/**Lucky Boy**/ #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 pb push_back #define mp make_pair #define F first #define S second #define maxc 1000000007 #define maxn 505 #define maxm 500005 #define pii pair <int,int> #define Task "Dosta" template <typename T> inline void read(T &x){char c;bool nega=0;while((!isdigit(c=getchar()))&&(c!='-'));if(c=='-'){nega=1;c=getchar();}x=c-48;while(isdigit(c=getchar())) x=x*10+c-48;if(nega) x=-x;} template <typename T> inline void writep(T x){if(x>9) writep(x/10);putchar(x%10+48);} template <typename T> inline void write(T x){if(x<0){putchar('-');x=-x;}writep(x);putchar(' ');} template <typename T> inline void writeln(T x){write(x);putchar('\n');} using namespace std; int n,t,val[maxn],dp[maxn][maxn][2],ans; vector <int> a[maxn]; void Dfs(int u,int par) { dp[u][0][0] = dp[u][0][1] = 0; dp[u][1][0] = dp[u][1][1] = val[u]; for (int v : a[u]) if (v != par) { Dfs(v,u); FORD(i,t,1) FOR(j,0,i) { if (i - j - 1 >= 0) { dp[u][i][0] = max(dp[u][i][0],dp[u][j][1] + dp[v][i-j-1][0]); //dp[u][i][0] = max(dp[u][i][0],dp[u][j][0] + dp[v][i-j-1][1]); } if (i - j - 2 >= 0) { dp[u][i][1] = max(dp[u][i][1],dp[u][j][1] + dp[v][i-j-2][1]); dp[u][i][0] = max(dp[u][i][0],dp[u][j][0] + dp[v][i-j-2][1]); } } } } int main() { //ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); //freopen(Task".inp", "r",stdin); //freopen(Task".out", "w",stdout); read(n),read(t); FOR(i,1,n) FOR(j,0,t) dp[i][j][0] = dp[i][j][1] = -maxc; FOR(i,1,n) read(val[i]); FOR(i,1,n-1) { int x,y; read(x),read(y); a[x].pb(y); a[y].pb(x); } Dfs(1,0); int ans = 0; FOR(i,1,t) ans = max(ans,max(dp[1][i][0],dp[1][i][1])); write(ans); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...