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]);
    }
}

Will Emmvee Photovoltaic IPO...

Emmvee Photovoltaic IPO: A Bright Outlook for Long-Term Solar...

$200 a week and...

Australia's Cost of Living Crisis Pushes International Students to...

Cornell to pay $60M...

Cornell Pays $60 Million to Settle Trump-Era Research Probes E-Blogarithm...

Squid Game: The Challenge’s...

Contestant 100 Reveals The Truth Behind Her 'Traumatizing Exit'...

Discussion on Covid Vaccination...

Why the Covid Vaccine Discussion Needs to Shed its...

ISC Declares Quarterly Dividend

Information Services Corporation Declares $0.23 Quarterly Dividend In a significant...

Will Emmvee Photovoltaic IPO deliver gains for long-term investors?

Emmvee Photovoltaic IPO: A Bright Outlook for Long-Term Solar Investors? The Indian renewable energy sector continues to shine brightly on the financial markets, and the...

$200 a week and tinned fish: Cost of living hits international students

Australia's Cost of Living Crisis Pushes International Students to the Brink The dream of studying abroad in Australia is quickly turning into a financial nightmare...

Cornell to pay $60M to settle Trump administration probes (Bianca Quilantan/Politico)

Cornell Pays $60 Million to Settle Trump-Era Research Probes E-Blogarithm Exclusive: Cornell University has agreed to a hefty financial resolution, paying $60 million to settle...

Squid Game: The Challenge’s Contestant 100 Talks About That ‘Really Traumatizing Exit’

Contestant 100 Reveals The Truth Behind Her 'Traumatizing Exit' from Squid Game: The Challenge Netflix’s reality competition hit, Squid Game: The Challenge, put its contestants...

Discussion on Covid Vaccination Should Be Non-Controversial

Why the Covid Vaccine Discussion Needs to Shed its Controversy In an age dominated by instantaneous news cycles and the constant pursuit of viral content,...

ISC Declares Quarterly Dividend

Information Services Corporation Declares $0.23 Quarterly Dividend In a significant announcement for its shareholders, Information Services Corporation (TSX:ISC), often referred to simply as "ISC," revealed...

Electrolux 10kg Front Load Washing Machine EWF1042R9WC $725 + Delivery ($0 to Selected Cities Only) @ Appliance Central

Massive Deal Alert: Electrolux 10kg Front Loader EWF1042R9WC Slashed to $725 Home appliance shoppers, prepare to upgrade your laundry room without emptying your wallet. A...

RFK Jr.’s top drug regulator resigns after ‘revenge campaign’ against ex-colleague: report

Top FDA Regulator Resigns Amid Scandal Involving 'Revenge Campaign' The political and regulatory landscape was shaken this week following the abrupt resignation of a senior...

National Guard, DC Police Respond As Dozens Descend On Navy Yard, Several Arrests Made

National Guard Deployed as Chaos Grips DC Navy Yard on Halloween Night Halloween night traditionally brings costumed fun, but in Washington D.C.’s Navy Yard neighborhood,...