#include <iostream>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <vector>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <stack>
#include <queue>
#include <assert.h>
#include <limits>
#include <cstdio>
#include <complex>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
#define mp make_pair
#define f first
#define s second
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<str> vs;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)
const int MOD = 1e9+7; // 998244353; // = (119<<23)+1
const int MX = 5e2+5;
const ll INF = 1e18;
const ld PI = 4*atan((ld)1);
const int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
namespace io {
void setIn(string s) { freopen(s.c_str(),"r",stdin); }
void setOut(string s) { freopen(s.c_str(),"w",stdout); }
void setIO(string s = "") {
ios_base::sync_with_stdio(0); cin.tie(0); // fast I/O
// cin.exceptions(cin.failbit); // ex. throws exception when you try to read letter into int
if (sz(s)) {
setIn(s+".in");
setOut(s+".out");
} // for USACO
}
}
using namespace io;
namespace input {
template<class T> void re(complex<T>& x);
template<class T1, class T2> void re(pair<T1,T2>& p);
template<class T> void re(vector<T>& a);
template<class T, size_t SZ> void re(array<T,SZ>& a);
template<class T> void re(T& x) { cin >> x; }
void re(double& x) { string t; re(t); x = stod(t); }
void re(ld& x) { string t; re(t); x = stold(t); }
template<class Arg, class... Args> void re(Arg& first, Args&... rest) {
re(first); re(rest...);
}
template<class T> void re(complex<T>& x) { T a,b; re(a,b); x = cd(a,b); }
template<class T1, class T2> void re(pair<T1,T2>& p) { re(p.f,p.s); }
template<class T> void re(vector<T>& a) { F0R(i,sz(a)) re(a[i]); }
template<class T, size_t SZ> void re(array<T,SZ>& a) { F0R(i,SZ) re(a[i]); }
}
namespace output {
template<class T1, class T2> void pr(const pair<T1,T2>& x);
template<class T, size_t SZ> void pr(const array<T,SZ>& x);
template<class T> void pr(const vector<T>& x);
template<class T> void pr(const set<T>& x);
template<class T1, class T2> void pr(const map<T1,T2>& x);
template<class T> void pr(const T& x) { cout << x; }
template<class Arg, class... Args> void pr(const Arg& first, const Args&... rest) {
pr(first); pr(rest...);
}
template<class T1, class T2> void pr(const pair<T1,T2>& x) {
pr("{",x.f,", ",x.s,"}");
}
template<class T> void prContain(const T& x) {
pr("{");
bool fst = 1; for (const auto& a: x) pr(!fst?", ":"",a), fst = 0; // const needed for vector<bool>
pr("}");
}
template<class T, size_t SZ> void pr(const array<T,SZ>& x) { prContain(x); }
template<class T> void pr(const vector<T>& x) { prContain(x); }
template<class T> void pr(const set<T>& x) { prContain(x); }
template<class T1, class T2> void pr(const map<T1,T2>& x) { prContain(x); }
void ps() { pr("\n"); }
template<class Arg> void ps(const Arg& first) {
pr(first); ps(); // no space at end of line
}
template<class Arg, class... Args> void ps(const Arg& first, const Args&... rest) {
pr(first," "); ps(rest...); // print w/ spaces
}
}
using namespace output;
using namespace input;
ll add(ll a, ll b) {
a += b;
if(a >= MOD) {
a -= MOD;
}
return a;
}
ll sub(ll a, ll b) {
a -= b;
if(a < 0) {
a += MOD;
}
return a;
}
ll mul(ll a, ll b) {
return (a * b)%MOD;
}
void add_self(ll& a, ll b) {
a = add(a, b);
}
void sub_self(ll& a, ll b) {
a = sub(a, b);
}
void mul_self(ll& a, ll b) {
a = mul(a, b);
}
ll N, M;
vl adj[MX];
ll A[MX];
ll dp[MX][MX][2];
vector<vector<ll>> info(MX+1, vector<ll>(MX+1, 0));
vector<vector<ll>> info2(MX+1, vector<ll>(MX+1, 0));
vector<vector<vector<ll>>> dp2[MX];
void delete_edges(ll u, ll p) {
for (ll i = 0; i<sz(adj[u]); i++) {
if (adj[u][i] == p) {
adj[u].erase(adj[u].begin()+i);
break;
}
}
trav(v, adj[u]) {
delete_edges(v, u);
}
}
ll f(ll u, ll t, ll ends) {
if (!sz(adj[u])) {
return dp[u][t][ends] = 0;
}
if (dp[u][t][ends] != -1) {
return dp[u][t][ends];
}
F0R (i, sz(adj[u])) {
if (u == 0) {
F0R (j, t+1) {
f(adj[u][i], j, 0);
f(adj[u][i], j, 1);
}
} else {
f(adj[u][i], t, 0);
f(adj[u][i], t, 1);
}
}
// F0R (i, sz(info)) {
// fill(info[i].begin(), info[i].end(), 0);
// fill(info2[i].begin(), info2[i].end(), 0);
// }
// F0R (i, sz(adj[u])) {
// F0R (j, t+1) {
// info[i][j] = dp[adj[u][i]][j][0];
// info2[i][j] = dp[adj[u][i]][j][1];
// }
// }
// F0R (i, sz(adj[u])+1) {
// FOR (j, 0, t+1) {
// dp2[u][i][j][0] = 0;
// dp2[u][i][j][1] = 0;
// }
// }
// F0R(i, t+1) {
// dp2[u][0][i][0] = info[0][i];
// dp2[u][0][i][1] = info2[0][i];
// }
dp[u][t][ends] = 0;
FOR (i, 1, sz(adj[u])+1) {
ll j = t;
dp2[u][i][j][0] = max(dp2[u][i-1][j][0], dp2[u][i][j][0]);
dp2[u][i][j][1] = max(dp2[u][i-1][j][1], dp2[u][i][j][1]);
FOR (j2, 0, t+1) {
if (j-j2-3 >= 0) {
dp2[u][i][j][0] = max(dp2[u][i][j][0], dp2[u][i-1][j-j2-3][0]+dp[adj[u][i-1]][j2][0]+A[adj[u][i-1]]);
dp2[u][i][j][1] = max(dp2[u][i][j][1], dp2[u][i-1][j-j2-3][1]+dp[adj[u][i-1]][j2][0]+A[adj[u][i-1]]);
}
if (j-j2-2 >= 0) {
dp2[u][i][j][1] = max(dp2[u][i][j][1], dp2[u][i-1][j-j2-2][0]+dp[adj[u][i-1]][j2][1]+A[adj[u][i-1]]);
dp2[u][i][j][0] = max(dp2[u][i][j][0], dp2[u][i-1][j-j2-2][0]+dp[adj[u][i-1]][j2][0]);
dp2[u][i][j][1] = max(dp2[u][i][j][1], dp2[u][i-1][j-j2-2][1]+dp[adj[u][i-1]][j2][0]);
}
if (j-j2-1 >= 0) {
dp2[u][i][j][1] = max(dp2[u][i][j][1], dp2[u][i-1][j-j2-1][0]+dp[adj[u][i-1]][j2][1]);
}
}
}
// F0R(i, t+1) {
// dp[u][t][ends] = max(dp[u][t][ends], dp2[u][sz(adj[u])][i][ends]);
// }
dp[u][t][ends] = dp2[u][sz(adj[u])][t][ends];
return dp[u][t][ends];
}
int main() {
// setIn("dostavljac.in.5b");
memset(dp, -1, sizeof(dp));
re(N, M);
FOR (i, 1, N+1) {
re(A[i]);
}
F0R (i, N-1) {
ll a, b;
re(a, b);
adj[a].pb(b);
adj[b].pb(a);
}
delete_edges(1, 0);
adj[0].pb(1);
M++;
FOR (i, 0, N+1) {
vector<vector<vector<ll>>> v(sz(adj[i])+1, vector<vector<ll>>(M+1, vector<ll>(2, 0)));
dp2[i] = v;
}
ll val = f(0, M, 1);
// FOR (i, 1, N+1) {
// vector<vector<vector<ll>>> v(sz(adj[i])+1, vector<vector<ll>>(M+1, vector<ll>(2, 0)));
// dp2[i] = v;
// }
ps(val);
return 0;
}
Compilation message
dostavljac.cpp: In function 'void io::setIn(std::string)':
dostavljac.cpp:67:35: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
67 | void setIn(string s) { freopen(s.c_str(),"r",stdin); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
dostavljac.cpp: In function 'void io::setOut(std::string)':
dostavljac.cpp:68:36: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
68 | void setOut(string s) { freopen(s.c_str(),"w",stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
8960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
8704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
10516 KB |
Output is correct |
2 |
Correct |
22 ms |
9592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
9728 KB |
Output is correct |
2 |
Correct |
242 ms |
14080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
436 ms |
17280 KB |
Output is correct |
2 |
Correct |
143 ms |
13304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
170 ms |
14720 KB |
Output is correct |
2 |
Correct |
992 ms |
24988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
895 ms |
26104 KB |
Output is correct |
2 |
Correct |
386 ms |
19576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1734 ms |
36088 KB |
Output is correct |
2 |
Correct |
186 ms |
16896 KB |
Output is correct |