답안 #1029594

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1029594 2024-07-21T05:05:34 Z sopaconk Chase (CEOI17_chase) C++17
100 / 100
322 ms 350544 KB
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define deb(x) cout<<#x<<": "<<x<<endl;
using lli=long long int;
using ld=long double;

vector<lli> paj;
vector<vector<lli>> adj;
vector<vector<lli>> sons;
vector<lli> par;

void dfs(lli x, lli p){
    for(lli y: adj[x]){
        if(y==p) continue;
        sons[x].pb(y);
        par[y]=x;
        dfs(y,x);
    }
}

vector<vector<lli>> dp1; // abajo usando migaja
vector<vector<lli>> dp2; //abajo no usando
vector<vector<lli>> dp3; // abajo usando migaja
vector<vector<lli>> dp4; //abajo no usando
lli v;
lli ans;
void echaleganas(lli x){
    lli sum=0;
    for(lli y: sons[x]){
        sum+=paj[y];
        echaleganas(y);
    }
    if(x!=0) sum+=paj[par[x]];
    dp1[x][1]=sum;
    dp3[x][1]=sum;
   // ans=max(ans, sum);
    for(lli y: sons[x]){
         
        for(lli i=1; i<=v; ++i){
            ans=max(ans, dp1[x][i]+dp3[y][v-i]-paj[y]);
            ans=max(ans, dp1[x][i]+dp4[y][v-i]-paj[y]);
            ans=max(ans, dp2[x][i]+dp3[y][v-i]);
            ans=max(ans, dp2[x][i]+dp4[y][v-i]);
            ans=max(ans, dp3[x][i]+dp1[y][v-i]-paj[x]);
            ans=max(ans, dp3[x][i]+dp2[y][v-i]);
            ans=max(ans, dp4[x][i]+dp1[y][v-i]-paj[x]);
            ans=max(ans, dp4[x][i]+dp2[y][v-i]);
        }
     //   deb(x);
       // deb(y);
        //deb(ans);
        for(lli i=1; i<=v; ++i){
            dp1[x][i]=max(dp1[x][i], dp1[y][i-1]+sum-paj[x]);
            dp1[x][i]=max(dp1[x][i], dp2[y][i-1]+sum);

            dp2[x][i]=max(dp2[x][i], dp1[y][i]-paj[x]);
            dp2[x][i]=max(dp2[x][i], dp2[y][i]);

            dp3[x][i]=max(dp3[x][i], dp3[y][i-1]+sum-paj[y]);
            dp3[x][i]=max(dp3[x][i], dp4[y][i-1]+sum-paj[y]);

            dp4[x][i]=max(dp4[x][i], dp3[y][i]);
            dp4[x][i]=max(dp4[x][i], dp4[y][i]);
        }
     /*   for(lli i=0; i<=v; ++i){
            cout<<dp1[x][i]<<" ";
        }
        cout<<endl;
        for(lli i=0; i<=v; ++i){
            cout<<dp2[x][i]<<" ";
        }
        cout<<endl;
        for(lli i=0; i<=v; ++i){
            cout<<dp3[x][i]<<" ";
        }
        cout<<endl;
        for(lli i=0; i<=v; ++i){
            cout<<dp4[x][i]<<" ";
        }
        cout<<endl;*/
    }

}


void solve(){
    lli n;
    cin>>n>>v;
    ans=0;
    paj.resize(n);
    for(lli i=0; i<n; ++i){
        cin>>paj[i];
    }
    adj.resize(n);
    sons.resize(n);
    par.resize(n);
    for(lli i=0; i<n-1; ++i){
        lli a,b;
        cin>>a>>b;
        a--;b--;
        adj[a].pb(b);
        adj[b].pb(a);
    }

    dfs(0,-1);
    dp1.resize(n, vector<lli> (v+1,0));
    dp2.resize(n, vector<lli> (v+1,0));
    dp3.resize(n, vector<lli> (v+1,0));
    dp4.resize(n, vector<lli> (v+1,0));
    echaleganas(0);
    cout<<ans<<endl;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    lli t=1;
    //cin>>t;
    while(t--){
        solve();
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 4 ms 3932 KB Output is correct
8 Correct 1 ms 980 KB Output is correct
9 Correct 1 ms 736 KB Output is correct
10 Correct 3 ms 3676 KB Output is correct
11 Correct 1 ms 1628 KB Output is correct
12 Correct 1 ms 980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 296 ms 350288 KB Output is correct
2 Correct 297 ms 350244 KB Output is correct
3 Correct 263 ms 342312 KB Output is correct
4 Correct 80 ms 36180 KB Output is correct
5 Correct 320 ms 343124 KB Output is correct
6 Correct 303 ms 343116 KB Output is correct
7 Correct 312 ms 343096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 4 ms 3932 KB Output is correct
8 Correct 1 ms 980 KB Output is correct
9 Correct 1 ms 736 KB Output is correct
10 Correct 3 ms 3676 KB Output is correct
11 Correct 1 ms 1628 KB Output is correct
12 Correct 1 ms 980 KB Output is correct
13 Correct 296 ms 350288 KB Output is correct
14 Correct 297 ms 350244 KB Output is correct
15 Correct 263 ms 342312 KB Output is correct
16 Correct 80 ms 36180 KB Output is correct
17 Correct 320 ms 343124 KB Output is correct
18 Correct 303 ms 343116 KB Output is correct
19 Correct 312 ms 343096 KB Output is correct
20 Correct 307 ms 343116 KB Output is correct
21 Correct 65 ms 36180 KB Output is correct
22 Correct 322 ms 343180 KB Output is correct
23 Correct 72 ms 36176 KB Output is correct
24 Correct 299 ms 343124 KB Output is correct
25 Correct 303 ms 341840 KB Output is correct
26 Correct 295 ms 350416 KB Output is correct
27 Correct 310 ms 350544 KB Output is correct