Advertisement
Guest User

Untitled

a guest
Jul 10th, 2021
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef pair <int, int> pii;
  7. typedef pair <ll, ll> pll;
  8.  
  9. #define precision(n) fixed << setprecision(n)
  10. #define pb push_back
  11. #define ub upper_bound
  12. #define lb lower_bound
  13. #define mp make_pair
  14. #define eps (double)1e-9
  15. #define PI 2*acos(0.0)
  16. #define endl "\n"
  17. #define sz(v) int((v).size())
  18. #define all(v) v.begin(),v.end()
  19. #define rall(v) v.rbegin(),v.rend()
  20. #define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  21. #define OK cout << "OK" << endl;
  22.  
  23. const int N = 1e4+7;
  24. vector <int> graph[N], values;
  25. int c[N];
  26.  
  27. void dfs(int v = 1, int p = 1) {
  28. for (auto to : graph[v]) {
  29. if (to == p) continue;
  30. dfs(to, v);
  31. }
  32. c[v] = values.back();
  33. values.pop_back();
  34. }
  35.  
  36. int main() {
  37. do_not_disturb
  38.  
  39. int n;
  40. cin >> n;
  41. for (int i = 1; i < n; i++) {
  42. int a, b;
  43. cin >> a >> b;
  44. graph[a].pb(b);
  45. graph[b].pb(a);
  46. }
  47. for (int i = 1; i <= n; i++) {
  48. int x;
  49. cin >> x;
  50. values.pb(x);
  51. }
  52. sort(rall(values));
  53. dfs();
  54. int ans = 0;
  55. for (int i = 2; i <= n; i++) {
  56. ans += c[i];
  57. }
  58. cout << ans << endl;
  59. for (int i = 1; i <= n; i++) {
  60. cout << c[i] << ' ';
  61. }
  62.  
  63. return 0;
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement