NowCoder 11222E 筑巢

#include <bits/stdc++.h>
#define int long long

using namespace std;

template <typename T> inline T read(){
T sum = 0;
int fl = 1,ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') fl = -1;
for(; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0';
return sum * fl;
}

const int maxn = 4e5 + 5;

bool vis[maxn];
int n,ans = -1e18,cnt = 0;

struct edge{
int to;
int next;
int w;
}e[maxn * 2];

cnt++;
e[cnt].to = v;
e[cnt].w = w;
}

void dfs(int u,int fa){
for(int i = head[u]; i; i = e[i].next){
int v = e[i].to;
if(v == fa) continue;
dfs(v,u);
if(dp[v] + e[i].w > 0) dp[u] += dp[v] + e[i].w;
}
ans = max(ans,dp[u]);
}

signed main(){
for(int i = 1; i <= n; i++) dp[i] = read<int>();
int now = n;
for(int i = 1; i <= n - 1; i++){
int u,v,w;
now++;
dp[now] = w;
}
dfs(1,0);
printf("%lld\n",ans);
return 0;
}

## 发送评论编辑评论

|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
（╯‵□′）╯︵┴─┴
￣﹃￣
(/ω＼)
∠( ᐛ 」∠)＿
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ｀)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ(￣∇￣o)
ヾ(´･ ･｀｡)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò｡)
Σ(っ °Д °;)っ
( ,,´･ω･)ﾉ"(´っω･｀｡)
╮(╯▽╰)╭
o(*////▽////*)q
＞﹏＜
( ๑´•ω•) "(ㆆᴗㆆ)

Emoji