Sponsors

Codeforces Round #384 (Div. 2), problem: (D) Chloe and pleasant prizes Solution in C/C++

#include <stdio.h>
#include <string.h>
#define MAX(X,Y) ((X)>(Y) ? (X) : (Y))
#define INF 1000000000000000

typedef long long LL;
int n,sz,first[200010],next[400010],tail[400010],son[200010],bro[200010],vis[200010];
LL a[200010],s[200010],maxs[200010],maxs2[200010];
void addedge(int u,int v);
void dfs(int u);

int main(void)
{
    //freopen("prize.in","r",stdin);
    //freopen("prize.out","w",stdout);
    scanf("%d",&n);
    int i;
    for(i=1;i<=n;i++)
        scanf("%I64d",&a[i]);
    int u,v;
    memset(first,0,sizeof(first));
    for(sz=0,i=1;i<=n-1;i++)
    {
        scanf("%d%d",&u,&v);
        addedge(u,v);  addedge(v,u);
    }
    memset(vis,0,sizeof(vis));
    memset(son,0,sizeof(son));
    dfs(1);
    LL ans=-INF;
    for(i=1;i<=n;i++)
        ans=MAX(ans,maxs2[i]);
    if(ans==-INF) printf("Impossible\n");
    else printf("%I64d\n",ans);
    return 0;
}

void addedge(int u,int v)
{
    tail[++sz]=v;
    next[sz]=first[u];  first[u]=sz;
}

void dfs(int u)
{
    vis[u]=1;
    s[u]=a[u];  maxs[u]=-INF;
    int v,e;
    for(e=first[u];e;e=next[e])
    {
        v=tail[e];
        if(vis[v]) continue;
        bro[v]=son[u];  son[u]=v;
        dfs(v);
        s[u]+=s[v];  maxs[u]=MAX(maxs[u],maxs[v]);
    }
    maxs[u]=MAX(maxs[u],s[u]);
    maxs2[u]=-INF;
    LL t=maxs[son[u]];
    for(v=bro[son[u]];v;v=bro[v])
    {
        maxs2[u]=MAX(maxs2[u],t+maxs[v]);
        t=MAX(t,maxs[v]);
    }
}

Chainsaw Man – The...

Chainsaw Man Movie's Reze Arc: Maximum the Hormone Returns! Fans...

You can still save...

Score Big Savings on Steelcase Furniture: Extended Labor Day...

Apple has reportedly launched...

Apple's Internal AI Chatbot: A Retail-Focused Approach Apple, a company...

Solana’s U.S. backing vs....

Solana's U.S. Backing vs. Binance's Asian Capital: A Crypto...

Why Trump Won’t Tell...

Why Trump Won't Tell the Truth About His Health:...

Mapping the Jazz Faculty...

Mapping the Jazz Faculty Gender Gap: A Berklee-Sponsored Study...

Chainsaw Man – The Movie: Reze Arc Anime Announces Insert Song Artist

Chainsaw Man Movie's Reze Arc: Maximum the Hormone Returns! Fans of Chainsaw Man are in for a treat! The upcoming anime movie focusing on...

You can still save 20% off Steelcase office chairs and desks during this extended Labor Day sale

Score Big Savings on Steelcase Furniture: Extended Labor Day Sale! Labor Day may be over, but the deals aren't! Steelcase, a renowned name in...

Apple has reportedly launched an AI chatbot, but it’s not for the iPhone 17 (or you)

Apple's Internal AI Chatbot: A Retail-Focused Approach Apple, a company synonymous with consumer-facing technology, has reportedly launched its own AI chatbot. However, unlike the...

Solana’s U.S. backing vs. Binance’s Asian capital: Who will win this battle?

Solana's U.S. Backing vs. Binance's Asian Capital: A Crypto Clash for Supremacy The cryptocurrency landscape is a volatile battlefield, and a fascinating clash is brewing...

Why Trump Won’t Tell the Truth About His Health: Michael Wolff | Inside Trump’s Head

Why Trump Won't Tell the Truth About His Health: Decoding Michael Wolff's Insights Michael Wolff, a controversial yet impactful figure in political journalism, recently offered...

Mapping the Jazz Faculty Gender Gap

Mapping the Jazz Faculty Gender Gap: A Berklee-Sponsored Study Reveals Stark Imbalance A recently published study sponsored by Berklee College of Music paints a concerning...

Burning Man festivalgoer is found dead in pool of blood as cops launch murder probe

Burning Man Festivalgoer Found Dead in Pool of Blood; Murder Investigation Launched A tragic incident has cast a shadow over the iconic Burning Man festival...

Fleischmann says ‘never’ to town hall (Ruby Rayner/timesfreepress.com)

Fleischmann Declares 'Never' to Town Halls, Calling Them 'Left-Wing Bait' Chattanooga Congressman Chuck Fleischmann has emphatically ruled out holding town hall meetings, labeling them as...

Plastic pollution poses growing health threat as Malaysia eyes Extended Producer Responsibility law

Plastic Pollution: A Growing Health Threat in Malaysia Malaysia is grappling with a significant environmental and health crisis: plastic pollution. A recent report highlights...