Submission #536521

#TimeUsernameProblemLanguageResultExecution timeMemory
536521AsymmetryChase (CEOI17_chase)C++17
40 / 100
4064 ms95772 KiB
//Autor: Bartłomiej Czarkowski
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';}
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n'
#else
#define debug(...) {}
#endif

const int N = 101'000;
const int V = 107;
int n, m, a, b;
int t[N];
int pr[N];
long long sum[N];
vector<int> v[N];
long long dp[N][V];

void dfd(int x) {
	for (int i : v[x]) {
		if (i == pr[x]) {
			continue;
		}
		pr[i] = x;
		dfd(i);
	}
}

void dfs(int x) {
	for (int i = 0; i <= m; ++i) {
		dp[x][i] = 0;
	}
	for (int i : v[x]) {
		if (i == pr[x]) {
			continue;
		}
		dfs(i);
		for (int j = 0; j <= m; ++j) {
			dp[x][j] = max(dp[x][j], dp[i][j]);
		}
	}
	for (int j = m; j; --j) {
		dp[x][j] = max(dp[x][j], dp[x][j - 1] + sum[x] - t[pr[x]]);
	}
}

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; ++i) {
		scanf("%d", &t[i]);
	}
	for (int i = 1; i < n; ++i) {
		scanf("%d%d", &a, &b);
		v[a].push_back(b);
		v[b].push_back(a);
	}
	for (int i = 1; i <= n; ++i) {
		for (int j : v[i]) {
			sum[i] += t[j];
		}
	}
	long long odp = 0;
	for (int i = 1; i <= n; ++i) {
		pr[i] = 0;
		dfd(i);
		dfs(i);
		for (int j = 0; j <= m; ++j) {
			odp = max(odp, dp[i][j]);
		}
	}
	printf("%lld\n", odp);
	return 0;
}

Compilation message (stderr)

chase.cpp: In function 'int main()':
chase.cpp:50:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |  scanf("%d%d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~
chase.cpp:52:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   52 |   scanf("%d", &t[i]);
      |   ~~~~~^~~~~~~~~~~~~
chase.cpp:55:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |   scanf("%d%d", &a, &b);
      |   ~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...