# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
163126 | 2019-11-11T13:12:05 Z | arnold518 | Chase (CEOI17_chase) | C++14 | 3189 ms | 346700 KB |
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int MAXN = 1e5; const int MAXK = 100; int N, K; ll P[MAXN+10], S[MAXN+10], dp1[MAXN+10][MAXK+10], dp2[MAXN+10][MAXK+10], ans; vector<int> adj[MAXN+10]; void dfs1(int now, int bef) { int i, j; S[now]+=P[bef]; for(int nxt : adj[now]) { if(nxt==bef) continue; dfs1(nxt, now); S[now]+=P[nxt]; } for(i=1; i<=K; i++) { ll val=0; for(int nxt : adj[now]) { if(nxt==bef) continue; val=max(val, dp1[nxt][i-1]); dp1[now][i]=max(dp1[now][i], dp1[nxt][i]); } dp1[now][i]=max(dp1[now][i], S[now]-P[bef]+val); } } vector<pll> V[MAXK+10]; void dfs2(int now, int bef) { int i, j; for(i=0; i<=K; i++) V[i].clear(); for(int nxt : adj[now]) { if(nxt==bef) continue; for(i=0; i<=K; i++) V[i].push_back({dp1[nxt][i], nxt}); } if(bef) for(i=0; i<=K; i++) V[i].push_back({dp2[bef][i], bef}); for(i=0; i<=K; i++) sort(V[i].begin(), V[i].end(), greater<pll>()); for(int nxt : adj[now]) { if(nxt==bef) continue; for(i=1; i<=K; i++) { for(j=0; j<V[i].size(); j++) { if(V[i][j].second==nxt) continue; dp2[nxt][i]=max(dp2[nxt][i], V[i][j].first); break; } for(j=0; j<V[i-1].size(); j++) { if(V[i-1][j].second==nxt) continue; dp2[nxt][i]=max(dp2[nxt][i], V[i-1][j].first+S[now]-P[nxt]); break; } } } for(int nxt : adj[now]) { if(nxt==bef) continue; dfs2(nxt, now); } } void dfs3(int now, int bef) { for(int nxt : adj[now]) { if(nxt==bef) continue; dfs3(nxt, now); } ll val1=0, val2=0; for(int nxt : adj[now]) { if(nxt==bef) continue; val1=max(val1, dp1[nxt][K]); } val1=max(val1, dp2[now][K]); for(int nxt : adj[now]) { if(nxt==bef) continue; val2=max(val2, dp1[nxt][K-1]); } val2=max(val2, dp2[now][K-1]); val2+=S[now]; ans=max({ans, val1, val2}); } int main() { int i, j, k; scanf("%d%d", &N, &K); for(i=1; i<=N; i++) scanf("%lld", &P[i]); for(i=1; i<N; i++) { int u, v; scanf("%d%d", &u, &v); adj[u].push_back(v); adj[v].push_back(u); } if(K==0) return !printf("0"); dfs1(1, 0); dfs2(1, 0); dfs3(1, 0); printf("%lld", ans); }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 4 ms | 2680 KB | Output is correct |
2 | Correct | 4 ms | 2680 KB | Output is correct |
3 | Correct | 4 ms | 2680 KB | Output is correct |
4 | Correct | 4 ms | 2680 KB | Output is correct |
5 | Correct | 4 ms | 2680 KB | Output is correct |
6 | Correct | 4 ms | 2752 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 4 ms | 2680 KB | Output is correct |
2 | Correct | 4 ms | 2680 KB | Output is correct |
3 | Correct | 4 ms | 2680 KB | Output is correct |
4 | Correct | 4 ms | 2680 KB | Output is correct |
5 | Correct | 4 ms | 2680 KB | Output is correct |
6 | Correct | 4 ms | 2752 KB | Output is correct |
7 | Correct | 12 ms | 4600 KB | Output is correct |
8 | Incorrect | 8 ms | 4600 KB | Output isn't correct |
9 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 853 ms | 187320 KB | Output is correct |
2 | Correct | 838 ms | 187404 KB | Output is correct |
3 | Correct | 3189 ms | 346700 KB | Output is correct |
4 | Correct | 356 ms | 179880 KB | Output is correct |
5 | Correct | 830 ms | 179984 KB | Output is correct |
6 | Correct | 852 ms | 179832 KB | Output is correct |
7 | Correct | 911 ms | 179832 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 4 ms | 2680 KB | Output is correct |
2 | Correct | 4 ms | 2680 KB | Output is correct |
3 | Correct | 4 ms | 2680 KB | Output is correct |
4 | Correct | 4 ms | 2680 KB | Output is correct |
5 | Correct | 4 ms | 2680 KB | Output is correct |
6 | Correct | 4 ms | 2752 KB | Output is correct |
7 | Correct | 12 ms | 4600 KB | Output is correct |
8 | Incorrect | 8 ms | 4600 KB | Output isn't correct |
9 | Halted | 0 ms | 0 KB | - |