# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
930523 | sleepntsheep | Paprike (COI18_paprike) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#include <vector>
#include <string>
#define N 100003
int n, K, a[N];
std::basic_string<int> g[N];
int dp[N];
long long b[N];
void dfs(int u, int p)
{
b[u] += a[u];
std::vector<long long> bot;
for (int v : g[u]) if ( v - p )
{
dfs(v, u);
bot.push_back(b[v]);
dp[u] += dp[v];
b[u] += b[v];
}
std::sort(bot.begin(), bot.end());
while (b[u] > K)
{
b[u] -= bot.back();
++dp[u];
bot.pop_back();
}
}
int main()
{
scanf("%d%d", &n, &K);
for (int i = 1; i <= n; ++i) scanf("%d", a+i);
for (int u, v, i = 1; i < n; ++i) scanf("%d%d", &u, &v), g[u].push_back(v), g[v].push_back(u);
dfs(1, 1);
printf("%d", dp[1]);
return 0;
}