CT48328

The Nearest Pair Sum of the Umber weights

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

Ranger found an old ledger of weights in the loft attic at the smokehouses of Brineport. The margin notes pose a precise question. 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 <= 1964; 0 <= a[i],k <= 1000.

Example 1:

Input: 1961 1136 474 446 947 791 379 595 910 531 947 664 101 920 275 83 36 857 975 81 297 848 789 287 719 592 740 839 389 651 352 481 179 292 753 813 806 487 991 185 740 934 720 624 22 550 229 755 978 613 879 932 757 773 921 447 619 596 515 55 831 776 397 724 950 507 887 431 74 805 615 690 303 227 701 161 434 981 464 223 373 508 617 133 86 525 380 596 879 24 832 834 249 240 98 583 58 679 78 272 663 542 696 185 481 623 805 925 442 43 911 848 860 284 900 971 867 198 662 740 503 46 9 349 406 885 562 664 323 4 992 789 874 467 68 265 172 548 978 901 395 773 101 569 385 392 982 762 132 859 714 545 765 495 715 950 187 120 783 707 517 598 143 303 236 25 303 749 76 800 408 616 371 153 302 99 506 446 271 405 41 676 272 246 853 90 437 372 532 225 969 3 161 284 151 868 724 449 942 269 130 876 339 775 739 547…
Output: 1136

Example 2:

Input: 680 1506 205 746 99 24 592 293 293 358 203 473 974 581 669 301 844 138 889 240 670 122 568 554 254 924 200 859 412 400 960 627 21 38 762 991 128 821 674 568 800 608 111 551 529 1 497 474 139 48 831 264 585 76 726 692 900 106 332 514 294 944 732 909 990 478 8 343 994 115 977 83 225 466 112 515 121 289 444 730 190 491 862 630 390 123 950 514 834 850 601 675 728 920 518 935 830 745 868 902 185 134 927 676 31 357 807 389 129 332 591 978 262 260 483 710 501 466 932 70 743 977 692 990 462 327 319 915 146 560 762 638 349 882 614 537 673 447 992 276 98 315 464 663 433 310 414 225 84 238 395 413 194 56 624 101 770 102 718 284 899 71 95 310 618 23 533 879 191 733 98 376 443 805 268 94 307 615 350 123 944 241 86 712 185 492 443 58 800 458 473 414 602 250 281 55 190 771 341 503 762 133 329 613 766 29 …
Output: 1506

Example 3:

Input: 39 595 934 907 216 606 330 982 108 812 535 704 979 542 268 757 907 592 506 214 328 75 641 209 510 585 760 920 488 675 708 734 529 36 820 383 233 151 181 892 580
Output: 596

Constraints:

2 <= n <= 1964; 0 <= a[i],k <= 1000.

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