QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#358040#6514. Is it well known in Poland?Djangle162857WA 1ms3856kbC++201.8kb2024-03-19 16:35:542024-03-19 16:35:55

Judging History

你现在查看的是最新测评结果

  • [2024-03-19 16:35:55]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3856kb
  • [2024-03-19 16:35:54]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << #x << " == " << x << endl
#define el '\n'
typedef long long ll;
const int mod = 1000000007;
const int inf = 2147483647;
const int N = 200020;
void solve()
{
	int n;
	ll sum = 0, ans = 0;
	cin >> n;
	vector<int> val(n + 1);
	vector<vector<int>> edge(n + 1);
	for (int i = 1; i <= n; i++) {
		cin >> val[i];
		sum += val[i];
	}
	for (int i = 1, x, y; i < n; i++) {
		cin >> x >> y;
		edge[x].push_back(y);
		edge[y].push_back(x);
	}
	function<priority_queue<ll>(int, int)> dfs = [&](int x, int fa) {
		priority_queue<ll> *q = new priority_queue<ll>();
		int a = val[x];
		for (int to : edge[x]) {
			if (to == fa)
				continue;
			priority_queue<ll> now = dfs(to, x);
			if (q->size() < now.size()) {
				swap(*q, now);
			}
			while (!now.empty()) {
				ll res = now.top();
				now.pop();
				q->push(res);
			}
		}
		priority_queue<ll> now = *q;
		while (1) {
			int n = (!q->empty()) ? q->size() : 0;
			if (n <= 1) {
				break;
			}
			int now = q->top();
			if (a >= now) {
				q->push(a);
				return *q;
			}
			ll b = q->top();
			q->pop();
			ll c = q->top();
			q->pop();
			a = a + c - b;
		}
		int n = (!q->empty()) ? q->size() : 0;
		if (n == 0) {
			q->push(a);
			return *q;
		}
		ll b = q->top();
		if (a < b) {
			q->pop();
			if (n % 2 == 0)
				ans += a - b;
			else
				ans -= a - b;
			return *q;
		}
		q->push(a);
		return *q;
	};
	priority_queue<ll> q = dfs(1, 0);
	int tmp = 0;
	while (!q.empty()) {
		if ((tmp++) % 2 == 0)
			ans += q.top();
		else
			ans -= q.top();
		q.pop();
	}
	cout << (ans + sum) / 2 << endl;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int T = 1;
	// cin>>T;
	while (T--) {
		solve();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3856kb

input:

5
1 5 3 2 4
1 2
1 3
2 4
2 5

output:

7

result:

ok 1 number(s): "7"

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 3628kb

input:

20
530933472 920863930 136179483 46535289 291417568 338674057 731327836 375973098 986104110 203163644 489326483 785212564 712578139 801609721 666347686 282530991 823910542 217884304 785578553 116701831
8 3
18 19
11 20
2 18
8 13
5 15
12 16
7 8
10 9
13 6
17 10
17 18
13 15
18 4
13 12
1 14
17 15
15 14
5...

output:

5631754852

result:

wrong answer 1st numbers differ - expected: '4611098449', found: '5631754852'