답안 #598018

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
598018 2022-07-17T10:36:53 Z OttoTheDino Chase (CEOI17_chase) C++17
40 / 100
362 ms 524288 KB
#include <bits/stdc++.h>
using namespace std;

#define rep(i,s,e)                  for (ll i = s; i <= e; ++i)
#define rrep(i,s,e)                 for (ll i = s; i >= e; --i)
#define pb                          push_back
#define pf                          push_front
#define fi                          first
#define se                          second
#define all(a)                      a.begin(), a.end()
#define len(a)                      (ll)a.size()
typedef long long ll;
typedef pair<ll, ll> ii;
typedef pair<ll, ll> pll;
typedef vector<ii> vii;
typedef vector<ll> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;

const ll mx = 1e5, mxv = 100;
ll p[mx+1], s[mx+1];
vi adj[mx+1];
map<int,ll> dp[mx+1][mxv+1];
ll dp2[mx+1][mxv+1];

ll dfs (int u, int par, int cnt)  {
    if (cnt==0) return 0;

    if (dp[u][cnt].find(par)!=dp[u][cnt].end()) return dp[u][cnt][par];

    dp[u][cnt][par] = s[u]-p[par];

    for (int v : adj[u]) {
        if (v==par) continue;
        dp[u][cnt][par] = max(dp[u][cnt][par], max(s[u]-p[par]+dfs(v,u,cnt-1), dfs(v,u,cnt))); 
    }

    return dp[u][cnt][par];
}

ll dfs2 (int u, int par, int cnt) {
    if (cnt==0) return 0;

    if (dp2[u][cnt]) return dp2[u][cnt];

    dp2[u][cnt] = s[u]-p[par];

    for (int v : adj[u]) {
        if (v==par) continue;
        dp2[u][cnt] = max(dp2[u][cnt], max(s[u]-p[par]+dfs2(v,u,cnt-1), dfs2(v,u,cnt))); 
    }

    return dp2[u][cnt];
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    ll n, l; cin >> n >> l;

    rep (i,1,n) cin >> p[i];

    rep (i,1,n-1) {
        ll u, v; cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }

    rep (i,1,n) for (int v : adj[i]) s[i] += p[v];

    if (n>1000) {
        cout << dfs2 (1, 0, l) << "\n";
        return 0;
    }

    ll ans = 0;
    rep (i,1,n) {
        ans = max(ans, dfs (i, 0, l));
    }

    cout << ans << "\n";

    return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 217 ms 476984 KB Output is correct
2 Correct 230 ms 477024 KB Output is correct
3 Correct 229 ms 477152 KB Output is correct
4 Correct 234 ms 476964 KB Output is correct
5 Correct 213 ms 476968 KB Output is correct
6 Correct 204 ms 477008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 217 ms 476984 KB Output is correct
2 Correct 230 ms 477024 KB Output is correct
3 Correct 229 ms 477152 KB Output is correct
4 Correct 234 ms 476964 KB Output is correct
5 Correct 213 ms 476968 KB Output is correct
6 Correct 204 ms 477008 KB Output is correct
7 Correct 233 ms 485968 KB Output is correct
8 Correct 214 ms 477700 KB Output is correct
9 Correct 362 ms 477480 KB Output is correct
10 Correct 217 ms 478412 KB Output is correct
11 Correct 215 ms 478376 KB Output is correct
12 Correct 206 ms 477900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 232 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 217 ms 476984 KB Output is correct
2 Correct 230 ms 477024 KB Output is correct
3 Correct 229 ms 477152 KB Output is correct
4 Correct 234 ms 476964 KB Output is correct
5 Correct 213 ms 476968 KB Output is correct
6 Correct 204 ms 477008 KB Output is correct
7 Correct 233 ms 485968 KB Output is correct
8 Correct 214 ms 477700 KB Output is correct
9 Correct 362 ms 477480 KB Output is correct
10 Correct 217 ms 478412 KB Output is correct
11 Correct 215 ms 478376 KB Output is correct
12 Correct 206 ms 477900 KB Output is correct
13 Runtime error 232 ms 524288 KB Execution killed with signal 9
14 Halted 0 ms 0 KB -