# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
967184 | mdobric | Paprike (COI18_paprike) | C++11 | 41 ms | 18816 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 <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int n, a[maxn], b[maxn], p[maxn];
long long h[maxn], k;
int dp[maxn];
long long ostatak[maxn];
vector <int> v[maxn];
void dfs (int x){
for (int i = 0; i < v[x].size(); i++){
if (p[x] != v[x][i]){
p[v[x][i]] = x;
dfs(v[x][i]);
}
}
}
int solve (int x){
vector <long long> sve;
for (int i = 0; i < v[x].size(); i++){
if (p[x] != v[x][i]){
solve(v[x][i]);
dp[x] += dp[v[x][i]];
sve.push_back(ostatak[v[x][i]]);
}
}
sort(sve.begin(), sve.end());
long long tr = h[x];
for (int i = 0; i < sve.size(); i++){
if (tr + sve[i] <= k){
tr += sve[i];
}
else{
dp[x]++;
}
}
ostatak[x] = tr;
//cout << x << " : " << dp[x] << " " << ostatak[x] << endl;
//cout << sve.size() << endl << endl;
return dp[x];
}
int main (void){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> k;
for (int i = 1; i <= n; i++){
cin >> h[i];
}
for (int i = 0; i < n - 1; i++){
cin >> a[i] >> b[i];
v[a[i]].push_back(b[i]);
v[b[i]].push_back(a[i]);
}
dfs(1);
cout << solve(1) << "\n";
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |