# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
706443 | mkupanovac | Paprike (COI18_paprike) | C++14 | 52 ms | 20288 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>
#define PB push_back
using namespace std;
const int MAXN = (1e5 + 10);
int n, k;
int w[MAXN];
int podstsum[MAXN];
vector<int> graph[MAXN];
int dfs(int node, int par){
int trz = 0, sol = 0;
podstsum[node] += w[node];
vector<int> djeca;
for (auto x: graph[node]){
if (x == par) continue;
sol += dfs(x, node);
podstsum[node] += podstsum[x];
djeca.PB(podstsum[x]);
}
if (podstsum[node] <= k){
return sol;
}
sort(djeca.begin(), djeca.end());
int tr = djeca.size() - 1;
while (podstsum[node] > k){
podstsum[node] -= djeca[tr];
tr--;
sol++;
}
return sol;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> k;
for (int i = 0; i < n; i++)
cin >> w[i + 1];
for (int i = 0; i < n - 1; i++){
int a, b;
cin >> a >> b;
graph[a].PB(b);
graph[b].PB(a);
}
cout << dfs(1, 0) << "\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... |