CT36308

The weights Nearest Pair Sum Puzzle

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

Mina the tallyman watches over the anvil district of Hammerfall's stores of weights. A careful computation stands between order and chaos. Task: Among all pairs of distinct elements, print the pair sum that is closest to k. If several tie, print the smaller sum. Input format: First line: n and k. Second line: n integers. Output format: Print the closest achievable pair sum. Constraints: 2 <= n <= 2238; -1000000 <= a[i],k <= 1000000.

Example 1:

Input: 1846 -594203 -980602 -522150 -861425 303327 76328 -77884 798688 -536542 -875175 885607 -336041 -984203 -389148 -531954 908129 -681048 435335 173690 134359 -847327 578072 492781 -355950 664334 -233022 4392 684521 -484833 874792 -407682 325598 -280111 -642906 -374949 -753823 -829026 559022 527793 -910067 500887 938332 -28742 424584 -504142 796226 -732862 -337985 755757 -855943 -615053 -678664 -263915 -465428 -595504 -533781 -179211 568838 -170468 269289 -497534 177902 105903 -310431 -618099 -133592 348728 -656036 186166 577568 266824 -634848 -656204 -459795 695575 -294786 -121659 -212228 452500 -124050 -168916 -969871 289872 -416947 -555910 636813 177756 923713 -643986 -261672 197594 -662451 428211 -5427 100672 662131 -576585 165117 214999 664719 275695 -197245 -651033 -81735 -940945 562249 …
Output: -594204

Example 2:

Input: 2019 1932878 192906 -807615 -763398 75369 619279 -709348 124924 -101187 -809362 -717233 -403996 -25194 -457484 33165 -822273 975255 952486 -199397 952087 174889 960131 -852278 -733714 -489443 3735 745493 466286 745886 532561 -917844 904688 861444 -597869 649243 702483 -432488 -533666 410264 -635506 569266 -511119 -511096 481676 838222 -537541 -107930 719784 -78762 771284 202114 178075 208700 862806 358624 -713764 -732797 -897552 -908492 534859 669785 -598706 -960561 -538063 196867 1467 168187 3707 -192372 946620 -339355 833942 -831192 -119703 -974437 -721823 46733 178064 637816 519621 367662 -754776 -663049 -573350 409027 661899 607817 -192432 -802447 837642 -114734 -944529 -41333 958000 -392462 191346 306439 -874645 295183 -187377 -293981 -495741 -218719 -182447 -181353 -290424 -113064 85…
Output: 1932860

Example 3:

Input: 709 1583078 -584732 -740127 -541293 329408 156231 -483286 -721820 862189 646201 -148214 -268305 872429 185919 294076 699660 -416648 -253487 308991 -444685 -225692 -342636 -64565 6267 -652329 -213593 -15047 584662 663555 -696336 -846076 -299584 -839090 126415 592288 -569374 -129628 603082 110620 -612826 -915382 9301 -699547 595995 -190048 884750 -233660 -809299 472897 -908444 579371 -424716 -918970 752148 238515 -630734 -18812 -178366 230057 52800 -175104 257543 375884 943978 18678 185789 273274 845845 -348308 741809 -806838 -437399 522582 -314921 260522 554636 -359836 106295 114053 -513682 -134057 261416 528619 -429292 -119460 -814162 -520281 -872377 -815598 -421136 -410673 -937933 -380910 -531977 -128020 59751 -409006 98539 30137 -104221 -92474 143769 -96286 -382020 79908 42810 -852545 65…
Output: 1583101

Constraints:

2 <= n <= 2238; -1000000 <= a[i],k <= 1000000.

Loading...
Test Cases:No test cases
No test cases available.