QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#718704#5823. 海星zlt100 ✓17ms36748kbC++143.0kb2024-11-06 21:13:362024-11-06 21:13:38

Judging History

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

  • [2024-11-06 21:13:38]
  • 评测
  • 测评结果:100
  • 用时:17ms
  • 内存:36748kb
  • [2024-11-06 21:13:36]
  • 提交

answer

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef __int128 lll;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;

#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 21], *p1 = buf, *p2 = buf;
inline int read() {
    char c = getchar();
    int x = 0;
    bool f = 0;
    for (; !isdigit(c); c = getchar()) f |= (c == '-');
    for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return f ? -x : x;
}

void write(lll x) {
	if (x < 0) {
		putchar('-');
		write(-x);
		return;
	}
	if (x > 9) {
		write(x / 10);
	}
	putchar('0' + x % 10);
}

const int maxn = 100100;
const ll inf = 0x3f3f3f3f3f3f3f3fLL;

ll n, a[maxn], head[maxn], len;
lll f[maxn][4];

struct edge {
	int to, next;
} edges[maxn << 1];

void add_edge(int u, int v) {
	edges[++len].to = v;
	edges[len].next = head[u];
	head[u] = len;
}

void dfs(int u, int fa) {
	vector<int> son;
	for (int i = head[u]; i; i = edges[i].next) {
		int v = edges[i].to;
		if (v == fa) {
			continue;
		}
		dfs(v, u);
		son.pb(v);
	}
	lll mx = -inf, val1 = a[u], val2 = -a[u];
	vector<lll> v1, v2;
	for (int v : son) {
		f[u][0] += max({f[v][0], f[v][1], f[v][2]});
		f[u][1] += max({f[v][0], f[v][1], f[v][2]});
		mx = max(mx, f[v][3] - max({f[v][0], f[v][1], f[v][2]}));
		val1 += max({f[v][0], f[v][1], f[v][2]});
		v1.pb(f[v][0] - max({f[v][0], f[v][1], f[v][2]}) - a[v]);
		val2 += max({f[v][0], f[v][1], f[v][2]});
		v2.pb(f[v][0] - max({f[v][0], f[v][1], f[v][2]}) + a[v]);
	}
	sort(v1.begin(), v1.end(), greater<lll>());
	sort(v2.begin(), v2.end(), greater<lll>());
	if (son.size()) {
		val1 += v1[0];
		for (int i = 1; i < (int)v1.size(); ++i) {
			if (v1[i] <= 0) {
				break;
			}
			val1 += v1[i];
		}
		val2 += v2[0];
		for (int i = 1; i < (int)v2.size(); ++i) {
			if (v2[i] <= 0) {
				break;
			}
			val2 += v2[i];
		}
		// printf("val1: ");
		// write(val1);
		// putchar('\n');
		// printf("val2: ");
		// write(val2);
		// putchar('\n');
		f[u][2] = max(val1, val2);
		f[u][3] = max(val1 - a[fa], val2 + a[fa]);
	} else {
		f[u][2] = -inf;
		f[u][3] = abs(a[u] - a[fa]);
	}
	f[u][1] += mx;
	if (son.empty()) {
		f[u][1] = -inf;
	}
	// printf("u: %d ", u);
	// for (int i = 0; i < 4; ++i) {
		// write(f[u][i]);
		// putchar(' ');
	// }
	// putchar('\n');
}

void solve() {
	n = read();
	for (int i = 1; i <= n; ++i) {
		a[i] = read();
	}
	for (int i = 1, u, v; i < n; ++i) {
		u = read();
		v = read();
		add_edge(u, v);
		add_edge(v, u);
	}
	dfs(1, 0);
	write(max({f[1][0], f[1][1], f[1][2]}));
}

int main() {
	// freopen("starfish.in", "r", stdin);
	// freopen("starfish.out", "w", stdout);
	int T = 1;
	// scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}


Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

score: 5
Accepted
time: 8ms
memory: 13732kb

input:

33331
-11171186 515439 98965039 -76317393 16735754 65157773 -68894376 -5035890 63842297 62703445 19490093 -19348988 36626070 -63191544 75774675 36234673 25724106 50267065 95991004 -82314232 -1707507 21500141 -55977458 20099787 93919133 -61954505 10399933 37981621 -43972867 -55397173 64852507 -634294...

output:

841990039350

result:

ok single line: '841990039350'

Test #2:

score: 5
Accepted
time: 17ms
memory: 16588kb

input:

66733
95890724 -97180087 10701157 -10302967 -56760887 72959390 42732829 -91968513 -10763389 -80502496 18023509 85926697 -73480245 35214859 75110409 -26337972 94380335 -7380945 81742484 99347886 -95766726 -51556891 8860897 73971259 -47084371 40118674 -39454765 84062321 57271285 46043487 -16495828 -72...

output:

1675502896833

result:

ok single line: '1675502896833'

Test #3:

score: 5
Accepted
time: 4ms
memory: 28020kb

input:

55818
95576832 -40406540 -77562724 -86962223 -86355864 -14707525 -23284928 -55013846 -8797620 -32039919 31497563 -84079189 7120642 -39679632 -84793218 -79671203 22810005 -78303679 -27225316 96630550 -9567274 -80702726 98257718 60033511 85660197 61232007 62647537 -26612843 42809208 49207326 33106201 ...

output:

2371920813806

result:

ok single line: '2371920813806'

Test #4:

score: 5
Accepted
time: 12ms
memory: 29724kb

input:

91488
-37356407 70273299 -60793880 -81403777 26478480 -34922436 -97890613 50474190 86412614 17362759 30833296 -34256525 11556777 21201579 21147564 49884363 -61234378 -39215570 -77080013 -66799487 -13159339 5668756 24773374 82719762 -50391822 63067832 -41251234 -45854335 31866899 17585770 76346192 18...

output:

3897691027419

result:

ok single line: '3897691027419'

Test #5:

score: 5
Accepted
time: 12ms
memory: 33376kb

input:

61092
-32373791 85985862 50942239 -32896750 67163226 -88020477 89587848 13623756 -87727832 25401341 90845091 -99650981 -43504678 -44485246 -83480211 -31564851 -27146298 -9527223 51505439 30561978 63627461 -51468118 -23806772 -15592067 70166666 -3488822 -89899072 -54152707 61250000 9159108 -2943889 4...

output:

2598174943942

result:

ok single line: '2598174943942'

Test #6:

score: 5
Accepted
time: 11ms
memory: 36748kb

input:

94029
-27391174 -65418446 24762503 77694422 74964844 91764613 14982163 -10699337 -40951873 -83831888 81523459 65388804 -31589647 80879853 33029128 -28920360 -58524160 -95273096 85294739 -22860576 -10278020 31425752 77804686 33124988 87569594 66739481 88867080 63643987 42865412 78510950 -89192521 -12...

output:

3994288382684

result:

ok single line: '3994288382684'

Test #7:

score: 5
Accepted
time: 4ms
memory: 12756kb

input:

37815
72986234 46218421 -92142995 22940263 -51535509 -60927696 25100989 -276786 -43307684 -48002472 -42269299 -6381350 -41510853 57401248 -69736412 25447817 73479189 21767399 -66290763 21544721 -63064466 81458555 -3274254 30869267 -37596193 97638880 46154839 2034100 -75586225 45459932 2617444 580047...

output:

1898177776617

result:

ok single line: '1898177776617'

Test #8:

score: 5
Accepted
time: 8ms
memory: 14848kb

input:

57893
24162311 96767788 61802773 -53223704 -75735497 81664175 -23248708 -98216048 -49451242 47944013 -60647681 -59327448 -66603953 81688992 -61386635 99535541 39605703 73604678 28138617 -53974258 -70742530 -19547670 35051204 27237673 64515468 78810378 37552041 39545443 40274254 -35113952 18667785 65...

output:

2903116466949

result:

ok single line: '2903116466949'

Test #9:

score: 5
Accepted
time: 5ms
memory: 8312kb

input:

22262
79028027 -38068784 93419573 -84167706 -58826963 80129696 34827899 -71598389 -55386224 89551685 -1780032 -89709755 70585785 -99828150 74583280 -58212897 53317984 41207699 -51628902 76695264 -7800274 -62002987 83614479 -11821436 41029875 -38843498 -2826518 77085681 23323956 -18881588 -71698064 -...

output:

1117798967714

result:

ok single line: '1117798967714'

Test #10:

score: 5
Accepted
time: 11ms
memory: 11804kb

input:

57899
65256064 -81736780 -93771845 -32702714 -24771253 15750201 9958033 82132633 76323779 70133194 2619146 -36540696 2408835 52625422 -50641708 -15127271 -61323743 43840072 30961962 97444188 -22186191 -36173544 90098167 -78043532 38044570 -90710994 29068826 56013279 -81170795 59787380 25960103 -8339...

output:

2890093707385

result:

ok single line: '2890093707385'

Test #11:

score: 5
Accepted
time: 6ms
memory: 14040kb

input:

65535
-58178985 96514995 -68371098 542628 75485904 -48819657 -19687829 78739995 36486631 -23577568 -91343497 -45004634 -75175484 -79676281 -39722449 56568339 80807233 -26427013 -78839918 80628279 6195245 95836900 -2157545 -81272507 91841194 24828298 -13223423 -82384969 -75486219 -9946260 43037365 -3...

output:

2523505648073

result:

ok single line: '2523505648073'

Test #12:

score: 5
Accepted
time: 4ms
memory: 14308kb

input:

65535
20805949 88919022 -81058737 -14388874 -72213374 10778248 72548233 -52234544 -6061081 66555934 -75630933 41612666 -42228918 63798020 -83634798 -10393560 -34374953 31739738 -13582443 -75967832 -86946009 -81171605 47245132 -49750706 38240813 74650962 -79088681 93742682 -14605009 -98253778 9144685...

output:

2511325948959

result:

ok single line: '2511325948959'

Test #13:

score: 5
Accepted
time: 9ms
memory: 14764kb

input:

65535
89725432 -13644226 -41727682 75712349 12970479 70376153 26868440 16790918 56423934 51656710 35048906 -19751340 95750374 -25610809 -65463001 27677267 7494281 46957909 -43292243 -75512522 -75054538 3904036 96647809 86803820 -15359568 29506351 -39921212 17851637 79159330 -91594021 77772189 -47001...

output:

2512147155027

result:

ok single line: '2512147155027'

Test #14:

score: 5
Accepted
time: 4ms
memory: 14572kb

input:

65535
-31289635 11642931 -64480772 60780847 65271202 -7941798 24137228 85816380 13876222 46822938 50761469 23917379 -61237610 -82136508 90624651 22799514 -12720631 -94875341 21965232 29975514 93888355 88979678 41017761 -81674381 26007326 -77722405 -753744 36927867 -59959460 20098463 64097528 8255408...

output:

2524924391007

result:

ok single line: '2524924391007'

Test #15:

score: 5
Accepted
time: 3ms
memory: 9992kb

input:

10096
69858238 -55660119 -47237653 -30113370 76781150 -48693571 -66973456 -55813012 -23286888 78050033 61006322 -62091234 42370271 27877974 -78168867 76908903 39994491 -19787991 43459869 -13545255 50744464 68267711 60584667 7072582 98908050 61815876 61023363 71951508 -41592277 12477203 90383274 5825...

output:

396095618452

result:

ok single line: '396095618452'

Test #16:

score: 5
Accepted
time: 1ms
memory: 8020kb

input:

4437
26423344 89373990 -93739235 -92819276 -84442461 -92640841 5701777 -23166121 61316354 -95695276 -68588684 -57034153 5647502 -51033681 49437746 99658733 -40527233 4208491 70409789 -16396598 -55609977 31104750 -52329556 -62486704 76079169 -37475857 -46743268 -71924622 64879717 16978873 -78988296 -...

output:

172550095391

result:

ok single line: '172550095391'

Test #17:

score: 5
Accepted
time: 5ms
memory: 12576kb

input:

40645
3407065 28238385 68003322 82379451 -20502460 34676191 -30339739 -34135417 -57821409 48985313 116385 -28029723 90527442 54370930 -37653446 -13711698 -36488191 -96512752 -17843258 84917115 -86075376 -66507087 -19700391 6821655 11850185 -22079788 85859006 -65685885 8415532 75747957 -95792517 8632...

output:

1597596177653

result:

ok single line: '1597596177653'

Test #18:

score: 5
Accepted
time: 8ms
memory: 14928kb

input:

58096
-21520940 14906872 -76529712 -97115412 -39035970 -10898467 15700388 14986513 -1985788 77896105 -10794040 91260360 38325660 89785735 -77188066 -96289624 26690803 70767098 12410493 -41057316 -84216973 71537112 -88437995 18251143 -87565662 -93457909 -54043965 34319435 50348286 -91620647 -39152263...

output:

2295981035832

result:

ok single line: '2295981035832'

Test #19:

score: 5
Accepted
time: 8ms
memory: 15060kb

input:

58742
49309979 17064418 91341883 36626211 78574509 82482129 54033614 -43882069 -76626851 76435284 -28054178 -9921358 41005796 74993210 38225930 73572671 85848904 66870992 13954686 23734782 -52193830 -8020005 -89449220 80999468 -27295929 -7875578 -58737621 1026085 -52319712 56699150 -45290942 -664328...

output:

2305394084092

result:

ok single line: '2305394084092'

Test #20:

score: 5
Accepted
time: 13ms
memory: 14836kb

input:

64098
68081705 59817009 -15971018 -15193981 65763006 -30937900 -80243926 59363283 -9992164 -79919403 26417299 96085299 -15257349 -76953181 18273634 -94827829 -20399747 52876535 62913713 -62479083 -64742361 12685176 -30217045 -26774642 -45771535 -38780887 -87046419 -31561031 -70482614 -93942311 -9001...

output:

2520090381442

result:

ok single line: '2520090381442'

Extra Test:

score: 0
Extra Test Passed