mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2025-02-20 00:46:38 +00:00
parent
34c591bf3b
commit
93e76807c0
85
S2OJ/1547/1547.cpp
Normal file
85
S2OJ/1547/1547.cpp
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cmath>
|
||||||
|
#include <functional>
|
||||||
|
#include <limits>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using std::cin;
|
||||||
|
using std::cout;
|
||||||
|
const char endl = '\n';
|
||||||
|
|
||||||
|
const int N = 5e5 + 5;
|
||||||
|
const long long INF = 1e18;
|
||||||
|
|
||||||
|
int n, m, w[N];
|
||||||
|
long long f[N][2];
|
||||||
|
std::vector<int> g[N];
|
||||||
|
|
||||||
|
void dfs(int u, int fa) {
|
||||||
|
int k = g[u].size() / 2 + 1;
|
||||||
|
long long res = 0;
|
||||||
|
std::vector<long long> a;
|
||||||
|
|
||||||
|
for (int v : g[u]) {
|
||||||
|
if (v == fa) continue;
|
||||||
|
|
||||||
|
dfs(v, u);
|
||||||
|
|
||||||
|
if (w[v] <= w[u]) {
|
||||||
|
a.emplace_back(std::max(f[v][0] + w[v], f[v][1] + w[u]) - f[v][1] - m);
|
||||||
|
res += f[v][1] + m;
|
||||||
|
} else {
|
||||||
|
a.emplace_back(f[v][0] + w[u] - std::max(f[v][0] + w[v], f[v][1] + m));
|
||||||
|
res += std::max(f[v][0] + w[v], f[v][1] + m);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::sort(a.begin(), a.end(), std::greater<>());
|
||||||
|
|
||||||
|
if (k - 1 > a.size()) {
|
||||||
|
f[u][0] = -INF;
|
||||||
|
} else {
|
||||||
|
long long sum = res;
|
||||||
|
|
||||||
|
for (int i = 0; i < k - 1; i++) sum += a[i];
|
||||||
|
for (int i = k - 1; i < a.size(); i++) sum += std::max(0ll, a[i]);
|
||||||
|
|
||||||
|
f[u][0] = sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (k > a.size()) {
|
||||||
|
f[u][1] = -INF;
|
||||||
|
} else {
|
||||||
|
long long sum = res;
|
||||||
|
|
||||||
|
for (int i = 0; i < k; i++) sum += a[i];
|
||||||
|
for (int i = k; i < a.size(); i++) sum += std::max(0ll, a[i]);
|
||||||
|
|
||||||
|
f[u][1] = sum;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
cin >> n >> m;
|
||||||
|
|
||||||
|
for (int i = 1; i <= n; i++) {
|
||||||
|
cin >> w[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 1, u, v; i < n; i++) {
|
||||||
|
cin >> u >> v;
|
||||||
|
|
||||||
|
g[u].emplace_back(v);
|
||||||
|
g[v].emplace_back(u);
|
||||||
|
}
|
||||||
|
|
||||||
|
dfs(1, 1);
|
||||||
|
|
||||||
|
cout << f[1][1] << endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
BIN
S2OJ/1547/data/data1.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data1.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data1.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data1.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data10.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data10.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data10.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data10.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data11.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data11.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data11.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data11.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data12.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data12.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data12.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data12.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data13.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data13.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data13.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data13.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data14.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data14.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data14.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data14.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data15.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data15.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data15.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data15.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data16.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data16.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data16.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data16.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data17.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data17.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data17.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data17.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data18.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data18.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data18.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data18.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data19.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data19.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data19.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data19.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data2.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data2.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data2.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data2.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data20.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data20.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data20.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data20.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data21.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data21.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data21.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data21.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data22.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data22.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data22.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data22.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data23.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data23.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data23.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data23.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data24.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data24.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data24.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data24.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data25.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data25.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data25.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data25.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data26.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data26.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data26.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data26.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data27.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data27.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data27.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data27.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data28.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data28.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data28.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data28.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data29.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data29.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data29.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data29.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data3.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data3.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data3.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data3.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data30.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data30.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data30.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data30.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data31.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data31.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data31.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data31.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data32.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data32.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data32.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data32.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data33.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data33.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data33.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data33.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data34.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data34.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data34.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data34.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data35.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data35.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data35.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data35.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data36.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data36.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data36.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data36.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data37.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data37.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data37.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data37.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data38.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data38.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data38.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data38.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data39.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data39.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data39.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data39.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data4.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data4.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data4.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data4.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data40.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data40.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data40.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data40.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data41.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data41.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data41.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data41.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data42.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data42.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data42.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data42.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data43.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data43.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data43.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data43.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data44.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data44.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data44.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data44.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data45.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data45.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data45.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data45.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data46.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data46.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data46.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data46.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data47.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data47.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data47.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data47.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data48.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data48.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data48.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data48.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data49.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data49.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data49.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data49.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data5.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data5.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data5.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data5.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data50.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data50.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data50.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data50.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data51.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data51.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data51.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data51.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data52.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data52.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data52.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data52.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data53.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data53.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data53.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data53.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1547/data/data54.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1547/data/data54.in
(Stored with Git LFS)
Normal file
Binary file not shown.
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user