답안 #596076

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
596076 2022-07-14T10:28:58 Z 1bin Chase (CEOI17_chase) C++14
30 / 100
294 ms 260156 KB
#include <bits/stdc++.h>
 
using namespace std;
 
#define all(v) v.begin(), v.end()
typedef long long ll;
const int NMAX = 1e5 + 5;
ll n, k, a[NMAX], u, v, s[NMAX][105], e[NMAX][105][2], ans;
vector<int> adj[NMAX];

void dfs(int x, int bef){
    ll sum = 0;
    for(int nx : adj[x]) sum += a[nx];
    
    s[x][1] = e[x][1][1] = sum;
    ans = max(ans, sum);
    for(int nx : adj[x]){
        if(nx == bef) continue;
        dfs(nx, x);
        for(int i = 0; i <= k; i++) {
            ans = max(ans, s[x][i] + max(e[nx][k - i][0], e[nx][k - i][1] - a[x]));
            ans = max(ans, max(e[x][i][0] + s[nx][k - i], e[x][i][1] + s[nx][k - i] - a[nx]));
            
            if(i){ s[x][i] = max(s[x][i], s[x][i - 1]);
            e[x][i][0] = max(e[x][i][0], e[x][i - 1][0]);
            e[x][i][1] = max(e[x][i][1], e[x][i - 1][1]);
            s[x][i] = max({s[x][i], s[nx][i], s[nx][i - 1] + sum - a[nx]});}
            e[x][i][0] = max(e[x][i][0], max(e[nx][i][0], e[nx][i][1] - a[x]));
            if(i)e[x][i][1] = max(e[x][i][1], max(e[nx][i - 1][0] + sum, e[nx][i - 1][1] + sum - a[x]));
        }
    }
    return;
}

int main(void){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  
    cin >> n >> k;
    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 0; i < n - 1; i++){
        cin >> u >> v;
        adj[u].emplace_back(v);
        adj[v].emplace_back(u);
    }
    dfs(1, 0);
    cout << ans;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Incorrect 2 ms 2644 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Incorrect 2 ms 2644 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 294 ms 260092 KB Output is correct
2 Correct 238 ms 260156 KB Output is correct
3 Correct 234 ms 253492 KB Output is correct
4 Correct 160 ms 253160 KB Output is correct
5 Correct 255 ms 253240 KB Output is correct
6 Correct 261 ms 253160 KB Output is correct
7 Correct 270 ms 253196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Incorrect 2 ms 2644 KB Output isn't correct
6 Halted 0 ms 0 KB -