{"id":58345,"date":"2025-08-16T08:35:15","date_gmt":"2025-08-16T00:35:15","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/58345.html"},"modified":"2025-08-16T08:35:15","modified_gmt":"2025-08-16T00:35:15","slug":"atcoder-beginner-contest-418%e8%a1%a5%e9%a2%98-c","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/58345.html","title":{"rendered":"AtCoder Beginner Contest 418(\u8865\u9898) C"},"content":{"rendered":"<p>C\u9898&#xff1a;<\/p>\n<p>C &#8211; Flush<\/p>\n<p>Time Limit: 2 sec \/ Memory Limit: 1024 MiB<\/p>\n<p>Score :\u00a0350\u00a0points<\/p>\n<h4>Problem Statement<\/h4>\n<p>On the poker table, there are tea bags of\u00a0N\u00a0different flavors. The flavors are numbered from\u00a01\u00a0through\u00a0N, and there are\u00a0Ai\u200b\u00a0tea bags of flavor\u00a0i\u00a0(1\u2264i\u2264N).<\/p>\n<p>You will play a game using these tea bags. The game has a parameter called\u00a0difficulty\u00a0between\u00a01\u00a0and\u00a0A1\u200b&#043;\u22ef&#043;AN\u200b, inclusive. A game of difficulty\u00a0b\u00a0proceeds as follows:<\/p>\n<li>You declare an integer\u00a0x. Here, it must satisfy\u00a0b\u2264x\u2264A1\u200b&#043;\u22ef&#043;AN\u200b.<\/li>\n<li>The dealer chooses exactly\u00a0x\u00a0tea bags from among those on the table and gives them to you.<\/li>\n<li>You check the flavors of the\u00a0x\u00a0tea bags you received, and choose\u00a0b\u00a0tea bags from them.<\/li>\n<li>If all\u00a0b\u00a0tea bags you chose are of the same flavor, you win. Otherwise, you lose.<\/li>\n<p>The dealer will do their best to make you lose.<\/p>\n<p>You are given\u00a0Q\u00a0queries, so answer each of them. The\u00a0j-th query is as follows:<\/p>\n<ul>\n<li>For a game of difficulty\u00a0Bj\u200b, report the minimum integer\u00a0x\u00a0you must declare at the start to guarantee a win. If it is impossible to win, report\u00a0\u22121\u00a0instead.<\/li>\n<\/ul>\n<h4>Constraints<\/h4>\n<ul>\n<li>1\u2264N\u22643\u00d7105<\/li>\n<li>1\u2264Q\u22643\u00d7105<\/li>\n<li>1\u2264Ai\u200b\u2264106\u00a0(1\u2264i\u2264N)<\/li>\n<li>1\u2264Bj\u200b\u2264min(106,A1\u200b&#043;\u22ef&#043;AN\u200b)\u00a0(1\u2264j\u2264Q)<\/li>\n<li>All input values are integers.<\/li>\n<\/ul>\n<hr \/>\n<h4>Input<\/h4>\n<p>The input is given from Standard Input in the following format:<\/p>\n<p>N Q<br \/>\nA1\u200b \u22ef AN\u200b<br \/>\nB1\u200b<br \/>\n\u22ee<br \/>\nBQ\u200b<\/p>\n<h4>Output<\/h4>\n<p>Print\u00a0Q\u00a0lines.<\/p>\n<p>The\u00a0j-th line (1\u2264j\u2264Q) should contain the answer to the\u00a0j-th query.<\/p>\n<hr \/>\n<h4>Sample Input 1<\/h4>\n<p>Copy<\/p>\n<p>4 5<br \/>\n4 1 8 4<br \/>\n1<br \/>\n8<br \/>\n5<br \/>\n2<br \/>\n10<\/p>\n<h4>Sample Output 1<\/h4>\n<p>Copy<\/p>\n<p>1<br \/>\n17<br \/>\n14<br \/>\n5<br \/>\n-1<\/p>\n<p>For the\u00a01-st query, if you declare\u00a0x&#061;1, then no matter which\u00a01\u00a0bag the dealer chooses, you can satisfy the winning condition by choosing appropriate\u00a01\u00a0bag among them. Since you cannot choose an integer\u00a0x\u00a0less than\u00a01, the answer is\u00a01.<\/p>\n<p>For the\u00a02-nd query, if you declare\u00a0x&#061;17, then no matter which\u00a017\u00a0bags the dealer chooses, you can satisfy the winning condition by choosing appropriate\u00a08\u00a0bags among them. Conversely, if\u00a0x&lt;17, the dealer can choose bags to prevent your victory. Thus, the answer is\u00a017.<\/p>\n<p>For the\u00a03-rd query, if you declare\u00a0x&#061;14, then no matter which\u00a014\u00a0bags the dealer chooses, you can satisfy the winning condition by choosing appropriate\u00a05\u00a0bags among them. Conversely, if\u00a0x&lt;14, the dealer can choose bags to prevent your victory. Thus, the answer is\u00a014.<\/p>\n<p>For the\u00a04-th query, if you declare\u00a0x&#061;5, then no matter which\u00a05\u00a0bags the dealer chooses, you can satisfy the winning condition by choosing appropriate\u00a02\u00a0bags among them. Conversely, if\u00a0x&lt;5, the dealer can choose bags to prevent your victory. Thus, the answer is\u00a05.<\/p>\n<p>For the\u00a05-th query, no matter what\u00a0x\u00a0you declare, the dealer can choose bags to prevent your victory. Thus, print\u00a0\u22121.<\/p>\n<hr \/>\n<h4>Sample Input 2<\/h4>\n<p>Copy<\/p>\n<p>5 3<br \/>\n13 13 13 13 2<br \/>\n5<br \/>\n12<br \/>\n13<\/p>\n<h4>Sample Output 2<\/h4>\n<p>Copy<\/p>\n<p>19<br \/>\n47<br \/>\n51<\/p>\n<p>\u7ffb\u8bd1&#xff1a;<\/p>\n<p><span style=\"color:#333333\"><span style=\"background-color:#ffffff\">C &#8211; Flush\u00a0<\/span><\/span><\/p>\n<hr \/>\n<p style=\"margin-left:0;margin-right:0;text-align:start\"><span style=\"color:#333333\"><span style=\"background-color:#ffffff\">\u65f6\u95f4\u9650\u5236&#xff1a;2 \u79d2 \/ \u5185\u5b58\u9650\u5236&#xff1a;1024 MiB<\/span><\/span><\/p>\n<p style=\"margin-left:0;margin-right:0\"><span style=\"color:#333333\"><span style=\"background-color:#ffffff\">\u00a0\u00a0\u5f97\u5206&#xff1a;\u00a0350\u00a0\u5206<\/span><\/span><\/p>\n<h4><span style=\"color:#333333\"><span style=\"background-color:#ffffff\"><span style=\"color:inherit\">\u00a0\u00a0\u95ee\u9898\u63cf\u8ff0<\/span><\/span><\/span><\/h4>\n<p style=\"margin-left:0;margin-right:0\"><span style=\"color:#333333\"><span style=\"background-color:#ffffff\">\u5728\u6251\u514b\u684c\u4e0a&#xff0c;\u6709\u00a0N\u00a0\u79cd\u4e0d\u540c\u53e3\u5473\u7684\u8336\u5305\u3002\u8fd9\u4e9b\u53e3\u5473\u7684\u7f16\u53f7\u4ece\u00a01\u00a0\u5230\u00a0N\u00a0&#xff0c;\u5176\u4e2d\u6709\u00a0Ai\u200b\u00a0\u4e2a\u00a0i\u00a0\u53e3\u5473\u7684\u8336\u5305&#xff08;\u00a01\u2264i\u2264N\u00a0&#xff09;\u3002<\/span><\/span><\/p>\n<p style=\"margin-left:0;margin-right:0\"><span style=\"color:#333333\"><span style=\"background-color:#ffffff\">\u4f60\u5c06\u4f7f\u7528\u8fd9\u4e9b\u8336\u5305\u73a9\u4e00\u4e2a\u6e38\u620f\u3002\u8fd9\u4e2a\u6e38\u620f\u6709\u4e00\u4e2a\u53c2\u6570\u53eb\u505a\u96be\u5ea6&#xff0c;\u8303\u56f4\u5728\u00a01\u00a0\u5230\u00a0A1\u200b&#043;\u22ef&#043;AN\u200b\u00a0\u4e4b\u95f4&#xff08;\u5305\u62ec\u00a01\u00a0\u548c\u00a0A1\u200b&#043;\u22ef&#043;AN\u200b\u00a0&#xff09;\u3002\u96be\u5ea6\u4e3a\u00a0b\u00a0\u7684\u6e38\u620f\u5982\u4e0b\u8fdb\u884c&#xff1a;<\/span><\/span><\/p>\n<li><span style=\"color:#333333\"><span style=\"background-color:#ffffff\">\u4f60\u58f0\u660e\u4e00\u4e2a\u6574\u6570\u00a0x\u00a0\u3002\u8fd9\u91cc&#xff0c;\u5b83\u5fc5\u987b\u6ee1\u8db3\u00a0b\u2264x\u2264A1\u200b&#043;\u22ef&#043;AN\u200b\u00a0\u3002<\/span><\/span><\/li>\n<li><span style=\"color:#333333\"><span style=\"background-color:#ffffff\">\u5e84\u5bb6\u4ece\u684c\u4e0a\u7684\u8336\u5305\u4e2d\u6311\u9009\u6070\u597d\u00a0x\u00a0\u4e2a\u8336\u5305\u7ed9\u4f60\u3002<\/span><\/span><\/li>\n<li><span style=\"color:#333333\"><span style=\"background-color:#ffffff\">\u4f60\u68c0\u67e5\u6536\u5230\u7684\u00a0x\u00a0\u8336\u53f6\u5305\u7684\u53e3\u5473&#xff0c;\u5e76\u4ece\u4e2d\u9009\u62e9\u00a0b\u00a0\u8336\u53f6\u5305\u3002<\/span><\/span><\/li>\n<li><span style=\"color:#333333\"><span style=\"background-color:#ffffff\">\u5982\u679c\u4f60\u9009\u62e9\u7684\u5168\u90e8\u00a0b\u00a0\u8336\u53f6\u5305\u53e3\u5473\u76f8\u540c&#xff0c;\u4f60\u5c31\u8d62\u4e86\u3002\u5426\u5219&#xff0c;\u4f60\u5c31\u8f93\u4e86\u3002<\/span><\/span><\/li>\n<p style=\"margin-left:0;margin-right:0\"><span style=\"color:#333333\"><span style=\"background-color:#ffffff\">\u5e84\u5bb6\u4f1a\u5c3d\u529b\u8ba9\u4f60\u8f93\u3002<\/span><\/span><\/p>\n<p style=\"margin-left:0;margin-right:0\"><span style=\"color:#333333\"><span style=\"background-color:#ffffff\">\u7ed9\u4f60\u00a0Q\u00a0\u4e2a\u67e5\u8be2&#xff0c;\u56de\u7b54\u6bcf\u4e00\u4e2a\u67e5\u8be2\u3002\u7b2c\u00a0j\u00a0\u4e2a\u67e5\u8be2\u5982\u4e0b&#xff1a;<\/span><\/span><\/p>\n<ul>\n<li><span style=\"color:#333333\"><span style=\"background-color:#ffffff\">\u5bf9\u4e8e\u96be\u5ea6\u4e3a\u00a0Bj\u200b\u00a0\u7684\u6e38\u620f&#xff0c;\u62a5\u544a\u4f60\u5fc5\u987b\u4ece\u5f00\u59cb\u5c31\u58f0\u660e\u7684\u6700\u5c0f\u6574\u6570\u00a0x\u00a0\u4ee5\u786e\u4fdd\u83b7\u80dc\u3002\u5982\u679c\u65e0\u6cd5\u83b7\u80dc&#xff0c;\u5219\u62a5\u544a\u00a0\u22121\u00a0\u3002<\/span><\/span><\/li>\n<\/ul>\n<h4><span style=\"color:#333333\"><span style=\"background-color:#ffffff\"><span style=\"color:inherit\">\u00a0\u00a0\u7ea6\u675f\u6761\u4ef6<\/span><\/span><\/span><\/h4>\n<ul>\n<li><span style=\"color:#333333\"><span style=\"background-color:#ffffff\">1\u2264N\u22643\u00d7105<\/span><\/span><\/li>\n<li><span style=\"color:#333333\"><span style=\"background-color:#ffffff\">1\u2264Q\u22643\u00d7105<\/span><\/span><\/li>\n<li><span style=\"color:#333333\"><span style=\"background-color:#ffffff\">1\u2264Ai\u200b\u2264106\u00a0(1\u2264i\u2264N)<\/span><\/span><\/li>\n<li><span style=\"color:#333333\"><span style=\"background-color:#ffffff\">1\u2264Bj\u200b\u2264min(106,A1\u200b&#043;\u22ef&#043;AN\u200b)\u00a0(1\u2264j\u2264Q)<\/span><\/span><\/li>\n<li><span style=\"color:#333333\"><span style=\"background-color:#ffffff\">\u6240\u6709\u8f93\u5165\u503c\u90fd\u662f\u6574\u6570\u3002<\/span><\/span><\/li>\n<\/ul>\n<hr \/>\n<h4><span style=\"color:#333333\"><span style=\"background-color:#ffffff\"><span style=\"color:inherit\">\u00a0\u00a0\u8f93\u5165<\/span><\/span><\/span><\/h4>\n<p style=\"margin-left:0;margin-right:0\"><span style=\"color:#333333\"><span style=\"background-color:#ffffff\">\u8f93\u5165\u6309\u7167\u4ee5\u4e0b\u683c\u5f0f\u4ece\u6807\u51c6\u8f93\u5165\u7ed9\u51fa&#xff1a;<\/span><\/span><\/p>\n<p><span style=\"color:#333333\"><span style=\"background-color:#ffffff\"><span style=\"color:#333333\"><span style=\"background-color:#f5f5f5\">N Q<br \/>\nA1\u200b \u22ef AN\u200b<br \/>\nB1\u200b<br \/>\n\u22ee<br \/>\nBQ\u200b<br \/>\n<\/span><\/span><\/span><\/span><\/p>\n<h4><span style=\"color:#333333\"><span style=\"background-color:#ffffff\"><span style=\"color:inherit\">\u00a0\u00a0\u8f93\u51fa<\/span><\/span><\/span><\/h4>\n<p style=\"margin-left:0;margin-right:0\"><span style=\"color:#333333\"><span style=\"background-color:#ffffff\">\u00a0\u00a0\u6253\u5370\u00a0Q\u00a0\u884c\u3002<\/span><\/span><\/p>\n<p style=\"margin-left:0;margin-right:0\"><span style=\"color:#333333\"><span style=\"background-color:#ffffff\">\u7b2c\u00a0j\u00a0\u884c&#xff08;\u00a01\u2264j\u2264Q\u00a0&#xff09;\u5e94\u5305\u542b\u7b2c\u00a0j\u00a0\u4e2a\u67e5\u8be2\u7684\u7b54\u6848\u3002<\/span><\/span><\/p>\n<hr \/>\n<h4><span style=\"color:#333333\"><span style=\"background-color:#ffffff\"><span style=\"color:inherit\">\u00a0\u00a0\u793a\u4f8b\u8f93\u5165 1Copy<\/span><\/span><\/span><\/h4>\n<p><span style=\"color:#333333\"><span style=\"background-color:#ffffff\"><span style=\"color:#767676\"><span style=\"background-color:#ffffff\">\u00a0\u00a0\u590d\u5236<\/span><\/span><\/span><\/span><\/p>\n<p><span style=\"color:#333333\"><span style=\"background-color:#ffffff\"><span style=\"color:#333333\"><span style=\"background-color:#f5f5f5\">4 5<br \/>\n4 1 8 4<br \/>\n1<br \/>\n8<br \/>\n5<br \/>\n2<br \/>\n10<br \/>\n<\/span><\/span><\/span><\/span><\/p>\n<h4><span style=\"color:#333333\"><span style=\"background-color:#ffffff\"><span style=\"color:inherit\">\u00a0\u00a0\u793a\u4f8b\u8f93\u51fa 1Copy<\/span><\/span><\/span><\/h4>\n<p><span style=\"color:#333333\"><span style=\"background-color:#ffffff\"><span style=\"color:#767676\"><span style=\"background-color:#ffffff\">\u00a0\u00a0\u590d\u5236<\/span><\/span><\/span><\/span><\/p>\n<p><span style=\"color:#333333\"><span style=\"background-color:#ffffff\"><span style=\"color:#333333\"><span style=\"background-color:#f5f5f5\">1<br \/>\n17<br \/>\n14<br \/>\n5<br \/>\n-1<br \/>\n<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin-left:0;margin-right:0\"><span style=\"color:#333333\"><span style=\"background-color:#ffffff\">\u5bf9\u4e8e\u7b2c\u00a01\u00a0\u4e2a\u67e5\u8be2&#xff0c;\u5982\u679c\u4f60\u58f0\u660e\u00a0x&#061;1\u00a0&#xff0c;\u90a3\u4e48\u65e0\u8bba\u7ecf\u9500\u5546\u9009\u62e9\u54ea\u4e2a\u00a01\u00a0\u888b&#xff0c;\u4f60\u90fd\u53ef\u4ee5\u901a\u8fc7\u9009\u62e9\u5176\u4e2d\u5408\u9002\u7684\u00a01\u00a0\u888b\u6765\u6ee1\u8db3\u83b7\u80dc\u6761\u4ef6\u3002\u7531\u4e8e\u4f60\u4e0d\u80fd\u9009\u62e9\u5c0f\u4e8e\u00a01\u00a0\u7684\u6574\u6570\u00a0x\u00a0&#xff0c;\u6240\u4ee5\u7b54\u6848\u662f\u00a01\u00a0\u3002<\/span><\/span><\/p>\n<p style=\"margin-left:0;margin-right:0\"><span style=\"color:#333333\"><span style=\"background-color:#ffffff\">\u5bf9\u4e8e\u7b2c\u00a02\u00a0\u4e2a\u67e5\u8be2&#xff0c;\u5982\u679c\u4f60\u58f0\u660e\u00a0x&#061;17\u00a0&#xff0c;\u90a3\u4e48\u65e0\u8bba\u7ecf\u9500\u5546\u9009\u62e9\u54ea\u4e9b\u00a017\u00a0\u888b&#xff0c;\u4f60\u90fd\u53ef\u4ee5\u901a\u8fc7\u9009\u62e9\u5176\u4e2d\u5408\u9002\u7684\u00a08\u00a0\u888b\u6765\u6ee1\u8db3\u83b7\u80dc\u6761\u4ef6\u3002\u76f8\u53cd&#xff0c;\u5982\u679c\u00a0x&lt;17\u00a0&#xff0c;\u7ecf\u9500\u5546\u53ef\u4ee5\u9009\u62e9\u888b\u5b50\u6765\u963b\u6b62\u4f60\u7684\u80dc\u5229\u3002\u56e0\u6b64&#xff0c;\u7b54\u6848\u662f\u00a017\u00a0\u3002<\/span><\/span><\/p>\n<p style=\"margin-left:0;margin-right:0\"><span style=\"color:#333333\"><span style=\"background-color:#ffffff\">\u5bf9\u4e8e\u7b2c\u00a03\u00a0\u4e2a\u67e5\u8be2&#xff0c;\u5982\u679c\u4f60\u58f0\u660e\u00a0x&#061;14\u00a0&#xff0c;\u90a3\u4e48\u65e0\u8bba\u7ecf\u9500\u5546\u9009\u62e9\u54ea\u4e9b\u00a014\u00a0\u888b\u5b50&#xff0c;\u4f60\u90fd\u53ef\u4ee5\u901a\u8fc7\u9009\u62e9\u5176\u4e2d\u7684\u9002\u5f53\u00a05\u00a0\u888b\u5b50\u6765\u6ee1\u8db3\u83b7\u80dc\u6761\u4ef6\u3002\u76f8\u53cd&#xff0c;\u5982\u679c\u00a0x&lt;14\u00a0&#xff0c;\u7ecf\u9500\u5546\u53ef\u4ee5\u9009\u62e9\u888b\u5b50\u6765\u963b\u6b62\u4f60\u7684\u80dc\u5229\u3002\u56e0\u6b64&#xff0c;\u7b54\u6848\u662f\u00a014\u00a0\u3002<\/span><\/span><\/p>\n<p style=\"margin-left:0;margin-right:0\"><span style=\"color:#333333\"><span style=\"background-color:#ffffff\">\u5bf9\u4e8e\u7b2c\u00a04\u00a0\u4e2a\u67e5\u8be2&#xff0c;\u5982\u679c\u4f60\u58f0\u660e\u00a0x&#061;5\u00a0&#xff0c;\u90a3\u4e48\u65e0\u8bba\u7ecf\u9500\u5546\u9009\u62e9\u54ea\u4e9b\u00a05\u00a0\u888b\u5b50&#xff0c;\u4f60\u90fd\u53ef\u4ee5\u901a\u8fc7\u9009\u62e9\u5176\u4e2d\u7684\u9002\u5f53\u00a02\u00a0\u888b\u5b50\u6765\u6ee1\u8db3\u83b7\u80dc\u6761\u4ef6\u3002\u76f8\u53cd&#xff0c;\u5982\u679c\u00a0x&lt;5\u00a0&#xff0c;\u7ecf\u9500\u5546\u53ef\u4ee5\u9009\u62e9\u888b\u5b50\u6765\u963b\u6b62\u4f60\u7684\u80dc\u5229\u3002\u56e0\u6b64&#xff0c;\u7b54\u6848\u662f\u00a05\u00a0\u3002<\/span><\/span><\/p>\n<p style=\"margin-left:0;margin-right:0\"><span style=\"color:#333333\"><span style=\"background-color:#ffffff\">\u5bf9\u4e8e\u7b2c\u00a05\u00a0\u4e2a\u67e5\u8be2&#xff0c;\u65e0\u8bba\u4f60\u58f0\u660e\u4ec0\u4e48\u00a0x\u00a0&#xff0c;\u7ecf\u9500\u5546\u90fd\u53ef\u4ee5\u9009\u62e9\u888b\u5b50\u6765\u963b\u6b62\u4f60\u7684\u80dc\u5229\u3002\u56e0\u6b64&#xff0c;\u6253\u5370\u00a0\u22121\u00a0\u3002<\/span><\/span><\/p>\n<hr \/>\n<h4><span style=\"color:#333333\"><span style=\"background-color:#ffffff\"><span style=\"color:inherit\">\u00a0\u00a0\u793a\u4f8b\u8f93\u5165 2Copy<\/span><\/span><\/span><\/h4>\n<p><span style=\"color:#333333\"><span style=\"background-color:#ffffff\"><span style=\"color:#767676\"><span style=\"background-color:#ffffff\">\u00a0\u00a0\u590d\u5236<\/span><\/span><\/span><\/span><\/p>\n<p><span style=\"color:#333333\"><span style=\"background-color:#ffffff\"><span style=\"color:#333333\"><span style=\"background-color:#f5f5f5\">5 3<br \/>\n13 13 13 13 2<br \/>\n5<br \/>\n12<br \/>\n13<br \/>\n<\/span><\/span><\/span><\/span><\/p>\n<h4><span style=\"color:#333333\"><span style=\"background-color:#ffffff\"><span style=\"color:inherit\">\u00a0\u00a0\u793a\u4f8b\u8f93\u51fa 2Copy<\/span><\/span><\/span><\/h4>\n<p><span style=\"color:#333333\"><span style=\"background-color:#ffffff\"><span style=\"color:#767676\"><span style=\"background-color:#ffffff\">\u00a0\u00a0\u590d\u5236<\/span><\/span><\/span><\/span><\/p>\n<p><span style=\"color:#333333\"><span style=\"background-color:#ffffff\"><span style=\"color:#333333\"><span style=\"background-color:#f5f5f5\">19<br \/>\n47<br \/>\n51<\/span><\/span><\/span><\/span><\/p>\n<p><span style=\"color:#333333\"><span style=\"background-color:#ffffff\"><span style=\"color:#333333\"><span style=\"background-color:#f5f5f5\">\u5206\u6790&#xff1a;\u4f7f\u5e84\u5bb6\u80dc\u5229\u7684x\u7684\u6700\u5927\u503c&#xff0c;\u52a0\u4e0a1&#xff0c;\u5c31\u662f\u201c\u6211\u201d\u80dc\u5229\u6240\u9700\u7684\u6700\u5c0f\u503c&#xff0c;\u5148\u5c06\u4e0d\u540c\u53e3\u5473\u7684\u8336\u5305\u6309\u4e2a\u6570\u4ece\u5c0f\u5230\u5927\u6392\u5e8f&#xff0c;\u627e\u5230\u4e2a\u6570\u5927\u4e8e\u7b49\u4e8eb-1\u7684\u7d22\u5f15&#xff0c;\u7d22\u5f15\u4e4b\u524d\u76f8\u52a0&#xff0c;\u7d22\u5f15\u4e4b\u540e\u4ee5&#xff08;b-1&#xff09;\u4e3a\u9ad8\u76f8\u4e58&#xff0c;\u6700\u540e\u518d\u52a0\u4e0a1&#xff0c;\u5373\u4e3a\u6700\u5c0f\u503c&#xff08;\u53c2\u8003\u8bb2\u89e3\u89c6\u9891\u3010AtCoder \u521d\u5b66\u8005\u7ade\u8d5b 418\u6bd4\u8d5b\u8bb2\u89e3&#xff08;ABC418&#xff09;\u3011https:\/\/www.bilibili.com\/video\/BV1HubKzkE42?vd_source&#061;3454b81e777238fd36a1725a4c3b1790&#xff09;<\/span><\/span><\/span><\/span><\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250816003510-689fd23e8bbda.jpg\" \/><\/p>\n<p>#include&lt;iostream&gt;<br \/>\n#include&lt;algorithm&gt;<br \/>\nusing namespace std;<br \/>\n#define int long long<br \/>\nconst int N &#061; 3e5 &#043; 5;<br \/>\nint q, n, a[N], sm[N];<br \/>\nsigned main()<br \/>\n{<br \/>\ncin &gt;&gt; n &gt;&gt; q;<br \/>\nfor (int i &#061; 1;i &lt;&#061; n;&#043;&#043;i)<br \/>\n{<br \/>\ncin &gt;&gt; a[i];<br \/>\n}<br \/>\nsort(a &#043; 1, a &#043; n &#043; 1);<br \/>\nfor (int i &#061; 1;i &lt;&#061; n;&#043;&#043;i)<br \/>\n{<br \/>\nsm[i] &#061; sm[i &#8211; 1] &#043; a[i];\/\/\u524d\u7f00\u548c<br \/>\n}<br \/>\nwhile (q&#8211;)<br \/>\n{<br \/>\nint b, ans&#061;0;<br \/>\ncin &gt;&gt; b;<br \/>\n\/\/\u627e\u5230\u5927\u4e8e\u7b49\u4e8eb-1\u7684\u5143\u7d20\u5730\u5740&#xff0c;\u518d\u51cf\u53bb\u9996\u5730\u5740&#xff0c;\u5f97\u5230\u5750\u6807\u7d22\u5f15<br \/>\nint p &#061; lower_bound(a &#043; 1, a &#043; 1 &#043; n, b &#8211; 1)-a;<br \/>\nans &#043;&#061; sm[p &#8211; 1] &#043; (n &#8211; p &#043; 1) * (b &#8211; 1) &#043; 1;<br \/>\nif (ans &gt; sm[n])\/\/\u6ee1\u8db3\u6761\u4ef6\u7684x\u6bd4\u8336\u5305\u603b\u6570\u8fd8\u5927&#xff0c;\u4e0d\u5408\u89c4<br \/>\nans &#061; -1;<br \/>\ncout &lt;&lt; ans &lt;&lt; endl;<br \/>\n}<br \/>\nreturn 0;<br \/>\n}<\/p>\n<\/p>\n<\/p>\n<\/p>\n<\/p>\n<\/p>\n<\/p>\n<\/p>\n<\/p>\n<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb955\u6b21\uff0c\u70b9\u8d5e24\u6b21\uff0c\u6536\u85cf9\u6b21\u3002C\u9898\uff1aC &#8211; Flush\u3002_atcoder418<\/p>\n","protected":false},"author":2,"featured_media":58344,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[6144,174,190,100],"topic":[],"class_list":["post-58345","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-atcoder","tag-c","tag-190","tag-100"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>AtCoder Beginner Contest 418(\u8865\u9898) C - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/58345.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"AtCoder Beginner Contest 418(\u8865\u9898) C - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb955\u6b21\uff0c\u70b9\u8d5e24\u6b21\uff0c\u6536\u85cf9\u6b21\u3002C\u9898\uff1aC - Flush\u3002_atcoder418\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/58345.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-08-16T00:35:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250816003510-689fd23e8bbda.jpg\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/58345.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/58345.html\",\"name\":\"AtCoder Beginner Contest 418(\u8865\u9898) C - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-08-16T00:35:15+00:00\",\"dateModified\":\"2025-08-16T00:35:15+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/58345.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/58345.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/58345.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"AtCoder Beginner Contest 418(\u8865\u9898) C\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"AtCoder Beginner Contest 418(\u8865\u9898) C - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/58345.html","og_locale":"zh_CN","og_type":"article","og_title":"AtCoder Beginner Contest 418(\u8865\u9898) C - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb955\u6b21\uff0c\u70b9\u8d5e24\u6b21\uff0c\u6536\u85cf9\u6b21\u3002C\u9898\uff1aC - Flush\u3002_atcoder418","og_url":"https:\/\/www.wsisp.com\/helps\/58345.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-08-16T00:35:15+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250816003510-689fd23e8bbda.jpg"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/58345.html","url":"https:\/\/www.wsisp.com\/helps\/58345.html","name":"AtCoder Beginner Contest 418(\u8865\u9898) C - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-08-16T00:35:15+00:00","dateModified":"2025-08-16T00:35:15+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/58345.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/58345.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/58345.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"AtCoder Beginner Contest 418(\u8865\u9898) C"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/58345","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=58345"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/58345\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/58344"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=58345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=58345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=58345"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=58345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}