Submission #874140

# Submission time Handle Problem Language Result Execution time Memory
874140 2023-11-16T10:31:23 Z josanneo22 Chase (CEOI17_chase) C++17
20 / 100
4000 ms 108324 KB
/*
Problem: P4657 [CEOI2017] Chase
When:    2023-11-16 14:54:19
Author:  Ning07
*/

#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
#define L(i,j,k) for(int i=(j);i<=(k);++i)
#define R(i,j,k) for(int i=(j);i>=(k);--i)

const int N=2e5+600;
i64 n,V,a[N],head[N<<1],ver[N<<1],nxt[N<<1],tot=0,tmp[N];
i64 ans=0,dp[N][120][2],sum[N];
void add_edge(int x,int y){
    ver[++tot]=y;
    nxt[tot]=head[x];
    head[x]=tot;
}
void update(int nw,int son,int fa){
    L(i,1,V-1) ans=max(ans,dp[nw][i][0]+dp[son][V-i][1]);
    L(i,1,V){
        dp[nw][i][0]=max({dp[nw][i][0],dp[son][i][0],dp[son][i-1][0]+sum[nw]-a[son]});
        dp[nw][i][1]=max({dp[nw][i][1],dp[son][i][1],dp[son][i-1][1]+sum[nw]-a[fa]});
    }
}
void dfs(int u,int f){
    for(int i=head[u];i;i=nxt[i]){
        int v=ver[i];
        if(v==f) continue;
        dfs(v,u);
    }
    int cnt=0;
    L(i,1,n) dp[u][i][0]=sum[u];
    for(int i=head[u];i;i=nxt[i]){
        int v=ver[i];
        update(u,v,f);
        tmp[++cnt]=v;
    }
    L(i,1,n) dp[u][i][0]=sum[u],dp[u][i][1]=0;
    R(i,cnt,1) update(u,tmp[i],f);
    ans=max({ans,dp[u][V][0],dp[u][V][1]});
    return;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);

    std::cin>>n>>V;
    L(i,1,n) std::cin>>a[i];
    L(i,1,n-1){
        int u,v; 
        std::cin>>u>>v;
        add_edge(u,v);
        add_edge(v,u);
    }
    L(i,1,n){
        for(int j=head[i];j;j=nxt[j]){
            int v=ver[j];
            sum[i]+=a[v];
        }
    }
    dfs(1,0);
    std::cout<<ans<<'\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 8536 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 8536 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Incorrect 4 ms 7260 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 4042 ms 108324 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 8536 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Incorrect 4 ms 7260 KB Output isn't correct
8 Halted 0 ms 0 KB -