# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
48498 | WLZ | Paprike (COI18_paprike) | C++17 | 93 ms | 29632 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;
typedef pair<int, int> ii;
typedef long long ll;
const int INF = 1e9;
const int MAXN = 1e5 + 5;
const ll LINF = 1e18;
const double pi = acos(-1);
const double EPS = 1e-9;
template <class T> using MinPQ = priority_queue<T, vector<T>, greater<T>>;
template <class T> using MaxPQ = priority_queue<T>;
int n;
ll k;
int ops;
vector<ll> a;
vector< vector<int> > g;
bitset<MAXN> used;
vector<int> p;
vector<int> v;
vector<int> h;
ll preCalc(int u, int par) {
p[u] = par;
vector <ll> children;
ll ans = a[u];
for (int& x : g[u]) {
if (x != par) {
children.push_back(preCalc(x, u));
ans += children.back();
}
}
sort(children.begin(),children.end());
int pos = (int)(children.size() - 1);
while (ans > k) {
ans -= children[pos];
ops ++;
pos --;
}
return ans;
}
int main() {
#ifdef DEBUG
//freopen("in.txt", "r", stdin);
#endif
ios_base::sync_with_stdio(false); cin.tie(NULL);
scanf("%d %lld", &n, &k);
p.assign(n, -1);
g.assign(n, vector<int>());
a.assign(n, 0);
for (int i = 0; i < n; i++) scanf("%lld", &a[i]);
for (int i = 0; i < n - 1; i++) {
int x, y;
scanf("%d %d", &x, &y); --x; --y;
g[x].emplace_back(y);
g[y].emplace_back(x);
}
ops = 0;
preCalc(0, -1);
printf("%d\n", ops);
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... |