답안 #598501

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
598501 2022-07-18T12:19:05 Z OttoTheDino Chase (CEOI17_chase) C++17
0 / 100
253 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<int, int> 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;
int p[mx+1], s[mx+1], dpd[mx+1][mxv+1], dpu[mx+1][mxv+1], ans, l;
vi adj[mx+1];
 
void dfs (int u, int par) {

    set<pll,greater<pll>> dpd_nxt[mx+1], dpu_here[mx+1];

    rep (i,0,mxv) {
        if (i) {
            dpd[u][i] = s[u]-p[par];
            dpu[u][i] = s[u];
        }
        dpd_nxt[i].insert({0,0});
        dpu_here[i].insert({dpu[u][i], u});
    }

    for (int v : adj[u]) {
        if (v==par) continue;
        dfs (v, u);

        rep (i,0,mxv) {
            dpd_nxt[i].insert({dpd[v][i], v});
            if (len(dpd_nxt[i])>2) dpd_nxt[i].erase(--dpd_nxt[i].end());
            dpd[u][i] = max(dpd[u][i], dpd[v][i]);
            if (i) {
                dpd[u][i] = max(dpd[u][i], s[u]-p[par]+dpd[v][i-1]);
                dpd[u][i] = max(dpd[u][i], dpd[u][i-1]);
            }
        }

        rep (i,0,mxv) {
            dpu_here[i].insert({i==0?dpu[v][i]:max(dpu[v][i], dpu[v][i-1]+s[u]-p[v]), v});
            if (len(dpu_here[i])>2) dpu_here[i].erase(--dpu_here[i].end());
            dpu[u][i] = max(dpu[u][i], dpu[v][i]);
            if (i) {
                dpu[u][i] = max(dpu[u][i], dpu[v][i-1]+s[u]-p[v]);
                dpu[u][i] = max(dpu[u][i], dpu[u][i-1]);
            }
        }
    }

    rep (i,0,l) {
        auto it1 = dpu_here[i].begin();
        auto it2 = dpd_nxt[l-i].begin();
        if ((*it1).se==(*it2).se) {
            ++it2;
            ans = max(ans, (*it1).fi+(*it2).fi);
            --it2;
            ++it1;
        }
        ans = max(ans, (*it1).fi+(*it2).fi);
    }
}
 
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
 
    ll n; 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];

    dfs (1,0);

    cout << ans << "\n";
 
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 59104 KB Output is correct
2 Correct 38 ms 59024 KB Output is correct
3 Incorrect 30 ms 40316 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 59104 KB Output is correct
2 Correct 38 ms 59024 KB Output is correct
3 Incorrect 30 ms 40316 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 253 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 59104 KB Output is correct
2 Correct 38 ms 59024 KB Output is correct
3 Incorrect 30 ms 40316 KB Output isn't correct
4 Halted 0 ms 0 KB -