contestId
int64
0
1.01k
name
stringlengths
2
58
tags
sequencelengths
0
11
title
stringclasses
523 values
time-limit
stringclasses
8 values
memory-limit
stringclasses
8 values
problem-description
stringlengths
0
7.15k
input-specification
stringlengths
0
2.05k
output-specification
stringlengths
0
1.5k
demo-input
sequencelengths
0
7
demo-output
sequencelengths
0
7
note
stringlengths
0
5.24k
test_cases
listlengths
0
402
timeConsumedMillis
int64
0
8k
memoryConsumedBytes
int64
0
537M
score
float64
-1
3.99
__index_level_0__
int64
0
621k
437
The Child and Polygon
[ "dp", "geometry" ]
null
null
This time our child has a simple polygon. He has to find the number of ways to split the polygon into non-degenerate triangles, each way must satisfy the following requirements: - each vertex of each triangle is one of the polygon vertex; - each side of the polygon must be the side of exactly one triangle; - the area of intersection of every two triangles equals to zero, and the sum of all areas of triangles equals to the area of the polygon; - each triangle must be completely inside the polygon; - each side of each triangle must contain exactly two vertices of the polygon. The picture below depicts an example of a correct splitting. Please, help the child. Calculate the described number of ways modulo 1000000007 (109<=<=+<=<=7) for him.
The first line contains one integer *n* (3<=≤<=*n*<=≤<=200) — the number of vertices of the polygon. Then follow *n* lines, each line containing two integers. The *i*-th line contains *x**i*,<=*y**i* (|*x**i*|,<=|*y**i*|<=≤<=107) — the *i*-th vertex of the polygon in clockwise or counterclockwise order. It's guaranteed that the polygon is simple.
Output the number of ways modulo 1000000007 (109<=<=+<=<=7).
[ "4\n0 0\n0 1\n1 1\n1 0\n", "4\n0 0\n1 0\n0 1\n-1 0\n", "5\n0 0\n1 0\n1 1\n0 1\n-2 -1\n" ]
[ "2\n", "1\n", "3\n" ]
In the first sample, there are two possible splittings: In the second sample, there are only one possible splitting:
[]
31
0
0
24,065
55
Beautiful numbers
[ "dp", "number theory" ]
D. Beautiful numbers
4
256
Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with this and just count the quantity of beautiful numbers in given ranges.
The first line of the input contains the number of cases *t* (1<=≤<=*t*<=≤<=10). Each of the next *t* lines contains two natural numbers *l**i* and *r**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=9<=·1018). Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d).
Output should contain *t* numbers — answers to the queries, one number per line — quantities of beautiful numbers in given intervals (from *l**i* to *r**i*, inclusively).
[ "1\n1 9\n", "1\n12 15\n" ]
[ "9\n", "2\n" ]
none
[ { "input": "1\n1 9", "output": "9" }, { "input": "1\n12 15", "output": "2" }, { "input": "1\n25 53", "output": "7" }, { "input": "1\n1 1000", "output": "138" }, { "input": "1\n1 100000", "output": "4578" }, { "input": "2\n234 59843\n46 3243", "output": "3378\n381" }, { "input": "4\n55 55\n1234 2348\n620 620\n4 1000", "output": "1\n135\n0\n135" }, { "input": "1\n1 9000000000000000000", "output": "15957349671845566" }, { "input": "5\n5397562498 1230483490253448\n39218765 5293867493184739\n99 999999999999\n546234 2394748365397856\n67 801834", "output": "3974776165902\n15977172601197\n5429986145\n7654830993719\n26117" }, { "input": "3\n1 1\n9000000000000000000 9000000000000000000\n8999999999999999999 8999999999999999999", "output": "1\n1\n0" }, { "input": "9\n357816591093473912 478906145736655650\n154072099530098530 297675544560923083\n853274171983555776 877332810632329118\n258601077826366175 856890041027686262\n151084241340128367 868279055062218946\n360302714872207562 400114081267420149\n15181634044326791 602401427137909762\n85295343866069509 372373854804747278\n61825864286248332 820583114541565140", "output": "262303539156695\n312897266661597\n38778726789519\n1139862940345127\n1402615778591617\n79118901111096\n1245376292216844\n659738283968181\n1512151848646298" }, { "input": "7\n104609317611013150 341289880328203892\n97241912027543222 314418300699926877\n53441135299739439 389735416311904624\n275391517859532788 467960038909170238\n304318532879803217 768089672739846481\n319824835697587963 736305171087865698\n409387390360731466 545771099640557323", "output": "549953639217759\n500330757015166\n752572674468163\n436944574287103\n888035593458099\n815512909354668\n274130616468780" }, { "input": "9\n445541835776354804 558734188486271358\n73682036065176542 366947184576839560\n308564620247881013 586289290590337947\n191966067909858814 427579642915908767\n96549472115040860 524715559221512354\n255020036710938147 654502276995773879\n80176357776022017 657344223781591909\n16719475415528318 443326279724654990\n338052544981592129 686095491515876947", "output": "201308654973933\n671018900952294\n557260640825456\n540245067535034\n951590675251248\n821822247331406\n1236063703297355\n975752055142342\n695221153195519" }, { "input": "8\n423727899203401096 465066089007515233\n592099166919122847 693326943315408193\n231531173552972562 531446476635170028\n716633579315369700 812280907158531602\n418627020920440527 499027876613131004\n163898261665251882 822784355862669948\n435839418352342371 467127616759016838\n485096651053655121 650414421921269042", "output": "95861671721858\n223094952917814\n644166606425537\n120467904177516\n171364758258616\n1283490622790032\n70087190765465\n307069761298908" }, { "input": "10\n317170715064111090 793355628628194180\n739156054415396992 777408930205278114\n190203742284298612 871433095584843953\n299464632866349604 887366147454183925\n604292320992752545 686849525965889579\n671343144075216807 887426356575285220\n29419076620738966 587651333431204877\n623639325649517323 649463206025796889\n433988870372201677 826343090001917979\n59211672688034983 185391377687885100", "output": "882906430841196\n42022148935039\n1331583561781769\n1112192163424357\n187737287429964\n340983354590699\n1187362489423650\n65177281203879\n681151115425128\n281120105826732" }, { "input": "10\n284628591358250298 646259693733499061\n124314877444536921 158360653417589331\n294802485707819594 348409229744008981\n600000720865727637 612539571868349067\n43148541126130378 706710122330555006\n623654284391810432 864058024613618266\n96275043624390708 878551347136533260\n101314040620664356 877345387577542422\n330459790968153544 396766608075635018\n437750508922390426 606265056174456186", "output": "747510034316095\n79156178606166\n135124732730027\n34085557037263\n1399630254414422\n411736949395029\n1554771181008711\n1542736445406160\n140533045281525\n291140888308231" }, { "input": "2\n699477065952458657 872009205627849715\n125384274193311446 322714849067940236", "output": "264558248920386\n447564169675211" }, { "input": "5\n287022480899155515 575607276198133575\n269577246853440756 493029962385944199\n33867048981266469 753806197266881614\n122720683292361468 585860767594869710\n158415500607290576 291208960498755656", "output": "592269002835278\n490392318097910\n1479332749456685\n973971824970712\n293817273058831" }, { "input": "7\n256594007991864539 522344824090301945\n244974983299956912 369564779245483014\n389003124143900342 870218470015550418\n195460950995683388 651270783906429493\n346224221518880818 866785151789106062\n253038155332981304 335508507616974071\n90600597989420506 233249608331610512", "output": "569988591376813\n279165427586805\n878614839734539\n963922382771989\n955701376330903\n185737826200532\n335147883567859" }, { "input": "4\n1316690822130409 43473794526140271\n31324963681870844 400596320533679208\n145052817797209833 830063350205257021\n158658407621553147 888417552777282422", "output": "111729862958642\n828223557472883\n1338195829521665\n1422989272142417" }, { "input": "5\n54466217578737820 199602944107455170\n590840692238108171 845011715230237399\n13556617965656361 472040489988635161\n714035286668109810 850408020486632812\n300350088832329391 447391742372023290", "output": "315665808201383\n452173335782140\n1043520592810950\n207881845406015\n346723658222085" }, { "input": "5\n319233236657111501 439203315902660433\n576536153378125966 581498392015228293\n211896470192814609 802604291686025035\n276426676181343125 621591075446200211\n508640487982063778 534483221559283380", "output": "275614372867493\n4659637264553\n1137342068923219\n699280378176787\n43498698267824" }, { "input": "2\n109665811875257879 257740418232519221\n604210587139443191 625314575765100157", "output": "346819987743014\n55092823341467" }, { "input": "6\n268178188710040742 576988663070502189\n504550929759464987 781375970672533626\n383664952430952316 634627265571145187\n32079280703703870 854176930200007145\n91332009005179043 184053275183946180\n40615673830587752 81118443341384874", "output": "626997097457560\n461119872685266\n494669041669140\n1651825364810407\n212939340175502\n81771949489938" }, { "input": "6\n302303335070090868 450003809972976938\n38797475097975886 362745244153909054\n255979212134580442 584044352113226014\n296314585958572870 577227175635398364\n62422561943575682 256080854477707325\n211472647017729730 238012961902478501", "output": "346244647306943\n735804706209393\n662539475305881\n575163541821522\n442982474060829\n69445242386875" }, { "input": "2\n682002069204224661 741697951489458142\n183681502765856661 640437699585130293", "output": "88198304176240\n962081125874149" }, { "input": "10\n139335835151468925 484066860116557425\n263442856552254877 313125870358044935\n251857673095776569 867489314560690117\n537516700522410653 723282616279678271\n395380521908450082 806672097008414136\n235871329996145263 884796582724269557\n534443148879117170 654182410587394685\n380572226198783846 879140470933346585\n44215071468435238 258286912303970378\n26312939052691831 729014058195540988", "output": "768880516070086\n105251422042778\n1171842666779485\n340594731814913\n733127744647337\n1237953582953797\n227668828811669\n906919615037865\n483212415948596\n1471096234030452" }, { "input": "1\n409932656755767888 555182693984224688", "output": "288403268897055" }, { "input": "5\n85486498031991129 609519488362467658\n580104402950188545 585551649929612890\n266889485749089795 290577696596475568\n29875185901092149 120388080236728441\n287513302314456963 523308494771522710", "output": "1105188916073505\n8401535899653\n42676144797046\n203278778614845\n511366630991705" }, { "input": "5\n19182336056148152 208792307948641418\n679752014854666194 698235312605408252\n171120031998422805 569721388031168451\n12148793149507654 583293559019372679\n132953494234881925 342019770688732055", "output": "428539359425062\n38145118408539\n843379203441666\n1226321445918381\n477944627277795" }, { "input": "1\n290788238061324166 326414205793715944", "output": "88194728799125" }, { "input": "5\n234980802136837794 678684394174931737\n379107007207217021 898625960325636363\n299786954727403405 471077420542543174\n128226582798019699 763298680395163050\n211161692546607273 384187742288440244", "output": "912901045635585\n946264344323228\n396530582015099\n1276195957822587\n393055025121430" }, { "input": "5\n89021901785536018 721970357006512096\n10455209854982 672121404159230388\n177585137659819353 575574850046871820\n65145962073623720 680133228947272669\n391876042757036995 669425246021613653", "output": "1326519621640374\n1455095002395873\n839815657146984\n1301765297380635\n556427727905125" }, { "input": "7\n759792714318263446 835705573208322900\n460742320949633715 470758694665495415\n415505837605910991 569430654167207205\n377168676014875291 658290859272415183\n283793404702060566 304952201274598832\n511204209665235974 673646757429123938\n204114397228198672 797985176265960222", "output": "125479708961158\n23959650169069\n295105797092887\n558580533111335\n46911931274954\n306769834874325\n1148348273935072" }, { "input": "9\n84911486803129062 371360395960886607\n215284546951446780 821388426823792006\n166750422723367513 332960369043386392\n656713493264874130 830533243210221453\n279192318807285719 846434989742407222\n184571653844680221 502821438236701008\n158613676606887401 202707248716070578\n57540730334410124 60880782285483617\n180003597833276637 824443392811852241", "output": "659040707264401\n1169272353137522\n376194188156988\n265702711225992\n1082952205541968\n710617267557674\n90657680784523\n5900863115303\n1261270219463080" }, { "input": "8\n57710938094283125 133772303709577393\n225971608986591641 527160269434785752\n326606700768403490 501974015736773213\n104238980296659530 597665360857507536\n129585992859086273 782985334217822917\n95949900165719335 509445717207521416\n282373530338110359 395331940454914825\n109101574779985403 119360877564462401", "output": "174229855050036\n652036071424638\n380051677908779\n1036346035129658\n1296518899055801\n926646671294788\n253442916135262\n27096176893815" }, { "input": "6\n143809081082381724 710709485503956307\n477002227475791129 748415761498654762\n194250190495612708 722691609433551584\n75162568328377570 286478648363940215\n167009103400266860 565869134050802277\n502744098916587217 886107958887143606", "output": "1174608615264406\n478873948513061\n1088489184647499\n477237176264941\n845730891804138\n655620551892311" }, { "input": "7\n44244599058777278 782140424182656491\n253187103338885776 695335736560569599\n29699011635943174 255027033171638318\n620123105021375390 632580504164439237\n15375925200954959 514151645969327190\n543405682133478575 609214152593311339\n319215262961370608 516830493012444317", "output": "1483854651250762\n905015525981812\n517553165213234\n33709204034254\n1113374351058951\n100352641671765\n426472119432887" }, { "input": "9\n126345625290218706 784850219000022089\n87023426041824251 129119697169349357\n115069371829617205 505544318183729913\n101524249349082603 410056021854163969\n365868821220246374 407398810119575711\n430453801123321243 449066562720974247\n343735112634641611 864077546788537811\n40949324306296116 718988450894528392\n374523541044751782 624503429430134549", "output": "1305739310768597\n102116549043209\n871997518159200\n702650020836287\n83143533290706\n47299584944773\n955940809894257\n1421553310960175\n483847068411766" }, { "input": "6\n628054167404305809 628631960105352883\n76614448048985164 664591413517666821\n262907302737145633 436561742851767924\n546542973469933497 609783019570052293\n144878328150224178 587802477340215629\n418802873287839235 492960279487924481", "output": "1567925255004\n1257931500816068\n392229959274848\n95900315764706\n917682338726377\n160732132866347" }, { "input": "4\n314756235091775713 527675415702104393\n262211905544992553 474539845101486132\n650849880923001511 686127592579746738\n302723886566715571 800643954239584448", "output": "453938626100478\n474023849716525\n67782962321158\n928258453065819" }, { "input": "10\n459047565386426124 557194352219781174\n334174633100816445 574518777618872908\n339256617206207374 461702378236276473\n588718051366049429 591583237944573629\n279503563837328065 787989497738844701\n21523491428669060 804432015267107086\n176599362925115382 372462231016537122\n86537781617987114 189304598553178698\n752344156097144261 806368993421691027\n823292318017906645 846671299523066080", "output": "175044419962203\n475567289515459\n271613608429105\n3445772208321\n966370135430115\n1572574434602937\n449309541759218\n233323955652174\n64992990053986\n52429533514265" }, { "input": "8\n235988693924367721 871763392821283031\n831354122145544757 897628959367475233\n208456624263360265 304233837602695736\n564455930754426325 747724855342153655\n733111142906877033 788390309965048178\n105753118324937331 227328301612681221\n89981956803108752 608240082487490427\n247970213583436454 274499034399377923", "output": "1217007726638431\n122388636057875\n217887979666251\n326178731297371\n61066928664641\n284737008145535\n1093373317372355\n53748508684990" }, { "input": "3\n106944629644846234 868784028501976520\n609893146415774201 829549590949963820\n280831004762390139 860314575937399777", "output": "1516745018492261\n379777276962608\n1101541359723373" }, { "input": "6\n43993555587390686 472396927744198873\n166115563323012274 740944002931589125\n745385119308013664 778824408151010477\n298307917637500505 739076799736050705\n270559504032562580 324248373286245715\n445587297201428883 453886541051311950", "output": "964419689750151\n1163718720277976\n36086421106555\n874162586490607\n118661992679784\n16505740933228" }, { "input": "7\n617593704688843596 828119736217232389\n3293204449283890 690109219324558805\n175366679625274382 211592984052182604\n134013605241468389 156621244614592310\n87651424533962276 294531661482220423\n652576309304110648 855895695568516689\n477666266196006205 647707658685159920", "output": "355371372539710\n1476637881473656\n78566652210064\n51957130064357\n470606070577295\n325555975457004\n316743540058033" }, { "input": "10\n50041481631208215 447762572637187951\n168215116153505310 514436306319509511\n247862097199125155 712191937735295742\n98125769392212035 345332927057490352\n351553192787723038 775772738657478138\n412742092029203073 627638533260248401\n196268314021034051 765318785061421414\n129127817256091656 848467628311779115\n209408331444736026 477286893553657979\n199077079465747558 382720611537297379", "output": "901252368499013\n758846043617857\n939353740423384\n579394703095088\n778021740563806\n409454897225469\n1132337130752633\n1422718774146674\n606275219995081\n421492007921185" }, { "input": "9\n360616474860484616 383999497202599749\n309747278163068128 324627518197345788\n37810933547908346 442701859960681398\n206321505581033547 517952468011059058\n830707273735965413 838545144291501943\n481064567699374119 637860173392597272\n64724838137416918 401453198057895626\n90969763647055934 161655002682127994\n832701350006309129 863335897035281262", "output": "46498133371402\n40850597316229\n919493060637341\n687618814419970\n17501208925553\n286355733364676\n752235164806132\n170035203610447\n60213403274850" }, { "input": "2\n17998572321587853 467288454221606647\n123156820907183052 834785732165266684", "output": "1024878648284905\n1407846459864944" }, { "input": "1\n1 999999999999999999", "output": "1986512740492024" }, { "input": "1\n191919191919191919 919191919191919191", "output": "1412002458948136" }, { "input": "10\n7090909090909090909 8191919191919191919\n7090909090909090909 8191919191919191919\n7090909090909090909 8191919191919191919\n7090909090909090909 8191919191919191919\n7090909090909090909 8191919191919191919\n7090909090909090909 8191919191919191919\n7090909090909090909 8191919191919191919\n7090909090909090909 8191919191919191919\n7090909090909090909 8191919191919191919\n7090909090909090909 8191919191919191919", "output": "1308643426185330\n1308643426185330\n1308643426185330\n1308643426185330\n1308643426185330\n1308643426185330\n1308643426185330\n1308643426185330\n1308643426185330\n1308643426185330" }, { "input": "10\n5555555555555555555 5555555555555555555\n5555555555555555555 5555555555555555555\n5555555555555555555 5555555555555555555\n5555555555555555555 5555555555555555555\n5555555555555555555 5555555555555555555\n5555555555555555555 5555555555555555555\n5555555555555555555 5555555555555555555\n5555555555555555555 5555555555555555555\n5555555555555555555 5555555555555555555\n5555555555555555555 5555555555555555555", "output": "1\n1\n1\n1\n1\n1\n1\n1\n1\n1" }, { "input": "6\n312118719 8999999999291228845\n667149650 8999999999517267203\n913094187 8999999999725405253\n154899869 8999999999515635472\n17006149 8999999999611234564\n557783437 8999999999450461526", "output": "15957349664614135\n15957349661485914\n15957349660288369\n15957349667743907\n15957349670077199\n15957349662484120" }, { "input": "3\n49395813 8999999999232681026\n130560985 8999999999732049698\n561847056 8999999999660238105", "output": "15957349668110658\n15957349668753664\n15957349663087863" }, { "input": "5\n988020422 8999999999820367297\n146385894 8999999999144649284\n647276749 8999999999118469703\n545904849 8999999999653000715\n66157176 8999999999517239977", "output": "15957349659989376\n15957349666269127\n15957349660004094\n15957349663205409\n15957349669084965" }, { "input": "4\n159528081 8999999999254686152\n155140195 8999999999221118378\n573463040 8999999999924740913\n984536526 8999999999076714216", "output": "15957349666612744\n15957349666487217\n15957349663881451\n15957349657279299" }, { "input": "10\n79746525 8999999999623095709\n107133428 8999999999011808285\n395554969 8999999999078624899\n617453363 8999999999094258969\n152728928 8999999999672481523\n252006040 8999999999766225306\n547017602 8999999999444173567\n765495515 8999999999421300177\n974820465 8999999999294163554\n560970841 8999999999720023934", "output": "15957349669242168\n15957349666397845\n15957349662613062\n15957349660358569\n15957349668236046\n15957349666959085\n15957349662552352\n15957349660553361\n15957349658288950\n15957349663286963" }, { "input": "8\n989660313 8999999999396148104\n74305000 8999999999742113337\n122356523 8999999999305515797\n592472806 8999999999132041329\n241537546 8999999999521843612\n885836059 8999999999480097833\n636266002 8999999999732372739\n202992959 8999999999981938988", "output": "15957349658680891\n15957349669642622\n15957349667387215\n15957349660885350\n15957349666468387\n15957349659758751\n15957349662500550\n15957349668676585" }, { "input": "6\n367798644 8999999999638151319\n332338496 8999999999040457114\n623242741 8999999999949105799\n531142995 8999999999535909314\n717090981 8999999999596647230\n158402883 8999999999599697481", "output": "15957349665093234\n15957349663187787\n15957349663490125\n15957349662956630\n15957349661257647\n15957349667853562" }, { "input": "5\n956765583 8999999999016337994\n370504871 8999999999584832832\n419407328 8999999999309673477\n518267114 8999999999030078889\n575673403 8999999999079982623", "output": "15957349657174545\n15957349664842554\n15957349663287444\n15957349660934012\n15957349660687410" }, { "input": "3\n739134224 8999999999892539778\n960410270 8999999999024682694\n286103376 8999999999849390015", "output": "15957349662121656\n15957349657206147\n15957349666880631" }, { "input": "4\n674378376 8999999999719931608\n37509017 8999999999387372213\n406034921 8999999999018438724\n546125539 8999999999879368044", "output": "15957349662041057\n15957349668957044\n15957349662212605\n15957349663840013" }, { "input": "4\n840893847 8999999999654562383\n139840441 8999999999921619811\n311512855 8999999999801704512\n25959825 8999999999551930487", "output": "15957349660688006\n15957349669232504\n15957349666299121\n15957349669765189" }, { "input": "7\n89870013 8999999999917755425\n802311555 8999999999055366008\n847333505 8999999999726653552\n132149035 8999999999144498325\n943135535 8999999999038849200\n820468253 8999999999630582637\n369473186 8999999999168524327", "output": "15957349670087446\n15957349658761550\n15957349660810956\n15957349666520206\n15957349657360988\n15957349660817284\n15957349663250037" }, { "input": "5\n508307251 8999999999718177123\n521516981 8999999999464659141\n290241176 8999999999356325428\n615193857 8999999999597603944\n207549445 8999999999906844873", "output": "15957349663733007\n15957349662832928\n15957349665236045\n15957349662359742\n15957349668276315" }, { "input": "2\n27550482 8999999999973770612\n120156054 8999999999028557489", "output": "15957349671150714\n15957349666232530" }, { "input": "1\n271055852 8999999999909378243", "output": "15957349667254063" }, { "input": "8\n787842267 8999999999359738007\n133322301 8999999999943290774\n417668696 8999999999749402497\n46587622 8999999999589402579\n718959740 8999999999109688815\n529442028 8999999999809940983\n943175645 8999999999567139418\n865545527 8999999999260702769", "output": "15957349660173586\n15957349669437416\n15957349664824777\n15957349669455115\n15957349659354335\n15957349663684224\n15957349659508226\n15957349658965833" }, { "input": "10\n1883143 8999999999664400380\n373522758 8999999999528614034\n49945668 8999999999257821295\n659209563 8999999999455232186\n74336065 8999999999709871509\n97315679 8999999999108629997\n112069256 8999999999626576439\n12161303 8999999999432219862\n756831002 8999999999681371635\n230283719 8999999999550291145", "output": "15957349670641976\n15957349664736116\n15957349668207957\n15957349661419878\n15957349669602216\n15957349667015648\n15957349668768809\n15957349669676588\n15957349661437380\n15957349666718051" } ]
1,902
45,568,000
3.677373
24,092
159
Zebra Tower
[ "*special", "data structures", "greedy", "sortings" ]
null
null
Little Janet likes playing with cubes. Actually, she likes to play with anything whatsoever, cubes or tesseracts, as long as they are multicolored. Each cube is described by two parameters — color *c**i* and size *s**i*. A Zebra Tower is a tower that consists of cubes of exactly two colors. Besides, the colors of the cubes in the tower must alternate (colors of adjacent cubes must differ). The Zebra Tower should have at least two cubes. There are no other limitations. The figure below shows an example of a Zebra Tower. A Zebra Tower's height is the sum of sizes of all cubes that form the tower. Help little Janet build the Zebra Tower of the maximum possible height, using the available cubes.
The first line contains an integer *n* (2<=≤<=*n*<=≤<=105) — the number of cubes. Next *n* lines contain the descriptions of the cubes, one description per line. A cube description consists of two space-separated integers *c**i* and *s**i* (1<=≤<=*c**i*,<=*s**i*<=≤<=109) — the *i*-th cube's color and size, correspondingly. It is guaranteed that there are at least two cubes of different colors.
Print the description of the Zebra Tower of the maximum height in the following form. In the first line print the tower's height, in the second line print the number of cubes that form the tower, and in the third line print the space-separated indices of cubes in the order in which they follow in the tower from the bottom to the top. Assume that the cubes are numbered from 1 to *n* in the order in which they were given in the input. If there are several existing Zebra Towers with maximum heights, it is allowed to print any of them. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
[ "4\n1 2\n1 3\n2 4\n3 3\n", "2\n1 1\n2 1\n" ]
[ "9\n3\n2 3 1 \n", "2\n2\n2 1 \n" ]
none
[ { "input": "4\n1 2\n1 3\n2 4\n3 3", "output": "9\n3\n2 3 1 " }, { "input": "2\n1 1\n2 1", "output": "2\n2\n2 1 " }, { "input": "3\n1 2\n2 2\n2 1", "output": "5\n3\n2 1 3 " }, { "input": "4\n2 1\n2 1\n1 1\n1 2", "output": "5\n4\n4 2 3 1 " }, { "input": "6\n1 1\n1 1\n2 2\n1 2\n1 2\n2 2", "output": "9\n5\n5 6 4 3 2 " }, { "input": "20\n1 2\n3 2\n4 2\n2 2\n5 2\n2 5\n3 2\n3 4\n4 4\n5 3\n2 1\n5 2\n5 3\n2 1\n5 5\n2 3\n1 5\n5 2\n3 4\n3 3", "output": "32\n11\n15 19 13 8 10 20 18 7 12 2 5 " }, { "input": "100\n2 5\n1 4\n1 2\n5 4\n1 3\n5 2\n4 4\n5 2\n3 5\n2 2\n5 5\n4 5\n5 3\n5 3\n1 1\n1 3\n5 3\n2 2\n3 1\n4 5\n5 1\n3 5\n3 5\n1 1\n3 3\n3 3\n2 3\n2 1\n5 3\n3 3\n1 2\n3 2\n1 3\n4 1\n4 1\n5 4\n2 4\n3 4\n1 4\n4 3\n4 4\n1 3\n5 3\n3 1\n5 4\n1 5\n4 5\n2 3\n5 5\n2 5\n4 5\n3 4\n1 5\n1 1\n5 1\n5 3\n5 1\n2 4\n3 1\n3 2\n2 3\n2 4\n4 5\n4 2\n1 1\n3 3\n1 4\n2 2\n1 2\n4 4\n4 1\n4 1\n2 5\n4 3\n5 5\n4 1\n1 5\n5 4\n2 5\n5 5\n1 2\n1 4\n5 1\n5 3\n5 2\n4 3\n1 3\n5 4\n1 5\n3 4\n5 3\n3 1\n3 3\n5 4\n4 2\n2 5\n2 4\n1 3\n1 4\n3 1", "output": "147\n47\n80 89 75 77 49 53 11 46 94 99 88 82 78 67 45 39 36 2 4 98 91 87 84 42 56 33 43 16 29 5 17 81 14 69 13 31 85 3 8 65 6 54 83 24 57 15 55 " }, { "input": "12\n1 3\n2 4\n2 1\n2 1\n3 1\n3 1\n3 1\n3 1\n3 1\n3 1\n3 1\n3 1", "output": "10\n7\n12 2 11 4 10 3 9 " }, { "input": "4\n2 1000000000\n2 1000000000\n2 1000000000\n1 1", "output": "2000000001\n3\n3 4 2 " } ]
46
307,200
0
24,103
0
none
[ "none" ]
null
null
Arpa has found a list containing *n* numbers. He calls a list bad if and only if it is not empty and gcd (see notes section for more information) of numbers in the list is 1. Arpa can perform two types of operations: - Choose a number and delete it with cost *x*. - Choose a number and increase it by 1 with cost *y*. Arpa can apply these operations to as many numbers as he wishes, and he is allowed to apply the second operation arbitrarily many times on the same number. Help Arpa to find the minimum possible cost to make the list good.
First line contains three integers *n*, *x* and *y* (1<=≤<=*n*<=≤<=5·105, 1<=≤<=*x*,<=*y*<=≤<=109) — the number of elements in the list and the integers *x* and *y*. Second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=106) — the elements of the list.
Print a single integer: the minimum possible cost to make the list good.
[ "4 23 17\n1 17 17 16\n", "10 6 2\n100 49 71 73 66 96 8 60 41 63\n" ]
[ "40\n", "10\n" ]
In example, number 1 must be deleted (with cost 23) and number 16 must increased by 1 (with cost 17). A gcd (greatest common divisor) of a set of numbers is the maximum integer that divides all integers in the set. Read more about gcd [here](https://en.wikipedia.org/wiki/Greatest_common_divisor).
[]
0
0
-1
24,127
0
none
[ "none" ]
null
null
As a tradition, every year before IOI all the members of Natalia Fan Club are invited to Malek Dance Club to have a fun night together. Malek Dance Club has 2*n* members and coincidentally Natalia Fan Club also has 2*n* members. Each member of MDC is assigned a unique id *i* from 0 to 2*n*<=-<=1. The same holds for each member of NFC. One of the parts of this tradition is one by one dance, where each member of MDC dances with a member of NFC. A dance pair is a pair of numbers (*a*,<=*b*) such that member *a* from MDC dances with member *b* from NFC. The complexity of a pairs' assignment is the number of pairs of dancing pairs (*a*,<=*b*) and (*c*,<=*d*) such that *a*<=&lt;<=*c* and *b*<=&gt;<=*d*. You are given a binary number of length *n* named *x*. We know that member *i* from MDC dances with member from NFC. Your task is to calculate the complexity of this assignment modulo 1000000007 (109<=+<=7). Expression denotes applying «XOR» to numbers *x* and *y*. This operation exists in all modern programming languages, for example, in C++ and Java it denotes as «^», in Pascal — «xor».
The first line of input contains a binary number *x* of lenght *n*, (1<=≤<=*n*<=≤<=100). This number may contain leading zeros.
Print the complexity of the given dance assignent modulo 1000000007 (109<=+<=7).
[ "11\n", "01\n", "1\n" ]
[ "6\n", "2\n", "1\n" ]
none
[ { "input": "11", "output": "6" }, { "input": "01", "output": "2" }, { "input": "1", "output": "1" }, { "input": "1111111111111111111111111111111111", "output": "68817500" }, { "input": "0000000000000000000000000000000000000", "output": "0" }, { "input": "11111111111111111111111111111111111000000000000000000000000000", "output": "774857564" }, { "input": "00000000000000000000111111111111111111111111111111111111111111", "output": "738177230" }, { "input": "10100101000010011110101011011110001", "output": "374541417" }, { "input": "01010100001010111111001111001000101010010101000111011011111000", "output": "629793317" }, { "input": "10001010011010010101101010111001001001011110110101011000010100110", "output": "276731670" }, { "input": "00001100100101000111111100110010001101001000011110110000", "output": "526794740" }, { "input": "01100010011001101100001000000101001000101101000110011100101101111101010100000011101011100", "output": "67141264" }, { "input": "100111100", "output": "80896" }, { "input": "11110111000110101111100100111110000011", "output": "448062885" }, { "input": "1000101010000101111110100110011110000011000110001111001001000110110011110110111110100", "output": "532893377" }, { "input": "0110011110111000001101001010101000011011101001001101000000111101010101111101010011101001111010111001", "output": "416862683" }, { "input": "0111001111110010000001111100110100111110001100100001111111110000010010111010010010010111000110001111", "output": "777947548" }, { "input": "1000000001101010101011111001001101011100011000010000100101001111001000110100100001110001100001000001", "output": "759144998" }, { "input": "1101010110001010100110011011101011010100010001110100010011011100011011000101110001010101110001101011", "output": "383088952" }, { "input": "1000001010111011110011111110011001011111011001110011100101111110100110111001100001110000011101011011", "output": "928069440" }, { "input": "10", "output": "4" }, { "input": "01", "output": "2" }, { "input": "00", "output": "0" }, { "input": "11", "output": "6" }, { "input": "0", "output": "0" }, { "input": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "output": "0" }, { "input": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111", "output": "261536897" }, { "input": "10110", "output": "352" }, { "input": "1100110010110011001011001100101100110010110011001111001100101100110010110011001011001100101100100010", "output": "499547155" } ]
92
0
0
24,132
497
Subsequences Return
[ "dp", "matrices" ]
null
null
Assume that *s**k*(*n*) equals the sum of digits of number *n* in the *k*-based notation. For example, *s*2(5)<==<=*s*2(1012)<==<=1<=+<=0<=+<=1<==<=2, *s*3(14)<==<=*s*3(1123)<==<=1<=+<=1<=+<=2<==<=4. The sequence of integers *a*0,<=...,<=*a**n*<=-<=1 is defined as . Your task is to calculate the number of distinct subsequences of sequence *a*0,<=...,<=*a**n*<=-<=1. Calculate the answer modulo 109<=+<=7. Sequence *a*1,<=...,<=*a**k* is called to be a subsequence of sequence *b*1,<=...,<=*b**l*, if there is a sequence of indices 1<=≤<=*i*1<=&lt;<=...<=&lt;<=*i**k*<=≤<=*l*, such that *a*1<==<=*b**i*1, ..., *a**k*<==<=*b**i**k*. In particular, an empty sequence (i.e. the sequence consisting of zero elements) is a subsequence of any sequence.
The first line contains two space-separated numbers *n* and *k* (1<=≤<=*n*<=≤<=1018, 2<=≤<=*k*<=≤<=30).
In a single line print the answer to the problem modulo 109<=+<=7.
[ "4 2\n", "7 7\n" ]
[ "11\n", "128\n" ]
In the first sample the sequence *a*<sub class="lower-index">*i*</sub> looks as follows: (0, 1, 1, 0). All the possible subsequences are: In the second sample the sequence *a*<sub class="lower-index">*i*</sub> looks as follows: (0, 1, 2, 3, 4, 5, 6). The subsequences of this sequence are exactly all increasing sequences formed from numbers from 0 to 6. It is easy to see that there are 2<sup class="upper-index">7</sup> = 128 such sequences.
[]
30
0
0
24,137
83
Doctor
[ "binary search", "math", "sortings" ]
B. Doctor
2
256
There are *n* animals in the queue to Dr. Dolittle. When an animal comes into the office, the doctor examines him, gives prescriptions, appoints tests and may appoint extra examination. Doc knows all the forest animals perfectly well and therefore knows exactly that the animal number *i* in the queue will have to visit his office exactly *a**i* times. We will assume that an examination takes much more time than making tests and other extra procedures, and therefore we will assume that once an animal leaves the room, it immediately gets to the end of the queue to the doctor. Of course, if the animal has visited the doctor as many times as necessary, then it doesn't have to stand at the end of the queue and it immediately goes home. Doctor plans to go home after receiving *k* animals, and therefore what the queue will look like at that moment is important for him. Since the doctor works long hours and she can't get distracted like that after all, she asked you to figure it out.
The first line of input data contains two space-separated integers *n* and *k* (1<=≤<=*n*<=≤<=105, 0<=≤<=*k*<=≤<=1014). In the second line are given space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109). Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator).
If the doctor will overall carry out less than *k* examinations, print a single number "-1" (without quotes). Otherwise, print the sequence of numbers — number of animals in the order in which they stand in the queue. Note that this sequence may be empty. This case is present in pretests. You can just print nothing or print one "End of line"-character. Both will be accepted.
[ "3 3\n1 2 1\n", "4 10\n3 3 2 1\n", "7 10\n1 3 3 1 2 3 1\n" ]
[ "2 ", "-1\n", "6 2 3 " ]
In the first sample test: - Before examination: {1, 2, 3} - After the first examination: {2, 3} - After the second examination: {3, 2} - After the third examination: {2} In the second sample test: - Before examination: {1, 2, 3, 4, 5, 6, 7} - After the first examination: {2, 3, 4, 5, 6, 7} - After the second examination: {3, 4, 5, 6, 7, 2} - After the third examination: {4, 5, 6, 7, 2, 3} - After the fourth examination: {5, 6, 7, 2, 3} - After the fifth examination: {6, 7, 2, 3, 5} - After the sixth examination: {7, 2, 3, 5, 6} - After the seventh examination: {2, 3, 5, 6} - After the eighth examination: {3, 5, 6, 2} - After the ninth examination: {5, 6, 2, 3} - After the tenth examination: {6, 2, 3}
[ { "input": "3 3\n1 2 1", "output": "2 " }, { "input": "4 10\n3 3 2 1", "output": "-1" }, { "input": "7 10\n1 3 3 1 2 3 1", "output": "6 2 3 " }, { "input": "1 0\n1", "output": "1 " }, { "input": "6 101\n9 78 54 62 2 91", "output": "4 6 2 3 " }, { "input": "9 100\n11 11 11 11 11 11 11 11 11", "output": "-1" }, { "input": "10 77\n7 45 10 10 9 8 1 2 3 1", "output": "2 " }, { "input": "5 12\n1 3 4 1 5", "output": "5 " }, { "input": "5 23\n7 1 4 7 9", "output": "1 4 5 " }, { "input": "5 41\n19 19 19 15 20", "output": "2 3 4 5 1 " }, { "input": "10 7\n5 1 2 5 4 3 5 2 2 4", "output": "8 9 10 1 3 4 5 6 7 " }, { "input": "10 109\n5 5 4 4 8 10 9 10 9 8", "output": "-1" }, { "input": "20 430\n25 27 25 24 24 49 11 30 24 50 48 40 41 5 19 45 16 43 43 12", "output": "12 13 16 18 19 2 6 8 10 11 " }, { "input": "20 314\n7 14 13 11 11 11 14 14 9 9 12 11 13 13 10 13 11 12 10 10", "output": "-1" }, { "input": "20 252\n2 8 13 3 12 15 1 7 11 14 5 7 2 12 5 12 10 5 6 1", "output": "-1" }, { "input": "20 329\n21 48 18 162 100 7 83 104 32 145 24 122 5 25 16 99 53 116 164 79", "output": "16 17 18 19 20 1 2 4 5 7 8 9 10 11 12 14 " }, { "input": "50 892\n50 50 43 50 44 50 44 44 49 50 50 47 50 50 48 50 40 45 50 42 46 50 49 48 47 49 44 45 44 46 48 45 48 43 45 48 48 47 47 49 47 37 45 31 44 45 39 50 48 50", "output": "43 44 45 46 47 48 49 50 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 " }, { "input": "50 68\n12 6 15 15 7 4 1 18 11 2 3 4 9 6 8 2 6 10 1 2 16 10 22 28 23 2 17 33 3 3 5 1 5 6 5 5 2 6 2 10 2 14 1 24 12 12 4 4 2 10", "output": "21 22 23 24 25 26 27 28 29 30 31 33 34 35 36 37 38 39 40 41 42 44 45 46 47 48 49 50 1 2 3 4 5 6 8 9 11 12 13 14 15 17 18 " }, { "input": "50 224\n5 4 5 5 5 5 5 3 5 5 5 5 4 5 5 4 5 5 5 5 5 5 5 5 4 5 5 4 5 5 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 4 4 4 4", "output": "33 34 35 36 37 38 39 40 41 42 43 44 45 46 " }, { "input": "50 479\n15 64 28 16 6 4 28 3 20 55 4 43 48 21 23 75 2 42 14 6 51 27 50 7 26 13 5 12 7 36 19 3 11 1 9 1 6 23 12 7 19 24 25 19 11 11 42 15 14 40", "output": "39 41 42 43 44 47 48 49 50 1 2 3 4 7 9 10 12 13 14 15 16 18 19 21 22 23 25 26 30 31 38 " }, { "input": "150 2476\n70 21 114 127 22 11 29 59 51 59 35 3 53 88 5 8 44 4 8 100 1 27 1 25 27 56 48 22 23 103 85 66 14 58 20 7 176 11 19 26 105 97 83 3 29 30 52 92 59 11 135 37 59 8 66 20 47 19 143 16 93 18 57 71 1 13 141 1 119 6 2 87 40 68 28 22 20 53 31 19 15 33 32 9 70 63 16 26 77 7 45 52 73 25 22 21 68 79 16 3 164 22 3 42 15 6 1 18 59 3 55 110 33 2 93 11 31 23 92 24 35 116 38 101 53 3 70 4 76 46 2 45 1 3 14 35 57 21 113 8 45 36 32 45 60 13 68 27 40 17", "output": "26 27 28 29 30 31 32 34 37 40 41 42 43 45 46 47 48 49 51 52 53 55 57 59 61 63 64 67 69 72 73 74 75 76 78 79 82 83 85 86 88 89 91 92 93 94 95 96 97 98 101 102 104 109 111 112 113 115 117 118 119 120 121 122 123 124 125 127 129 130 132 136 137 138 139 141 142 143 144 145 147 148 149 1 3 4 5 7 8 9 10 11 13 14 17 20 22 24 25 " } ]
122
307,200
0
24,200
5
Bindian Signalizing
[ "data structures" ]
E. Bindian Signalizing
4
256
Everyone knows that long ago on the territory of present-day Berland there lived Bindian tribes. Their capital was surrounded by *n* hills, forming a circle. On each hill there was a watchman, who watched the neighbourhood day and night. In case of any danger the watchman could make a fire on the hill. One watchman could see the signal of another watchman, if on the circle arc connecting the two hills there was no hill higher than any of the two. As for any two hills there are two different circle arcs connecting them, the signal was seen if the above mentioned condition was satisfied on at least one of the arcs. For example, for any two neighbouring watchmen it is true that the signal of one will be seen by the other. An important characteristics of this watch system was the amount of pairs of watchmen able to see each other's signals. You are to find this amount by the given heights of the hills.
The first line of the input data contains an integer number *n* (3<=≤<=*n*<=≤<=106), *n* — the amount of hills around the capital. The second line contains *n* numbers — heights of the hills in clockwise order. All height numbers are integer and lie between 1 and 109.
Print the required amount of pairs.
[ "5\n1 2 4 5 3\n" ]
[ "7\n" ]
none
[ { "input": "5\n1 2 4 5 3", "output": "7" }, { "input": "3\n2118 2118 2118", "output": "3" }, { "input": "3\n2221 1976 2221", "output": "3" }, { "input": "3\n140 989 2895", "output": "3" }, { "input": "4\n2440 2440 2440 2440", "output": "6" }, { "input": "4\n1178 1178 2577 2577", "output": "6" }, { "input": "4\n332 2714 2420 2714", "output": "5" }, { "input": "5\n763 763 763 763 763", "output": "10" }, { "input": "5\n4136 1826 4136 1826 1826", "output": "8" }, { "input": "5\n3581 3581 305 305 3581", "output": "8" }, { "input": "10\n8097 8097 8097 8097 8097 8097 8097 8097 8097 8097", "output": "45" }, { "input": "10\n4972 4972 4972 4858 4858 4972 4972 4972 4858 4972", "output": "28" }, { "input": "10\n1620 8260 1620 3994 3994 8260 8260 1620 1620 3994", "output": "19" }, { "input": "10\n5938 4836 5938 5938 4836 4836 2780 2780 1495 4836", "output": "21" }, { "input": "10\n6090 3360 6090 6313 1608 6313 4087 3360 1608 1608", "output": "19" } ]
2,308
103,731,200
3.518286
24,272
690
Collective Mindsets (medium)
[]
null
null
Way to go! Heidi now knows how many brains there must be for her to get one. But throwing herself in the midst of a clutch of hungry zombies is quite a risky endeavor. Hence Heidi wonders: what is the smallest number of brains that must be in the chest for her to get out at all (possibly empty-handed, but alive)? The brain dinner night will evolve just as in the previous subtask: the same crowd is present, the *N*<=-<=1 zombies have the exact same mindset as before and Heidi is to make the first proposal, which must be accepted by at least half of the attendees for her to survive.
The only line of input contains one integer: *N*, the number of attendees (1<=≤<=*N*<=≤<=109).
Output one integer: the smallest number of brains in the chest which allows Heidi to merely survive.
[ "1\n", "3\n", "99\n" ]
[ "0\n", "1\n", "49\n" ]
none
[ { "input": "1", "output": "0" }, { "input": "3", "output": "1" }, { "input": "2", "output": "0" }, { "input": "4", "output": "0" }, { "input": "5", "output": "2" }, { "input": "6", "output": "1" }, { "input": "7", "output": "3" }, { "input": "8", "output": "0" }, { "input": "9", "output": "4" }, { "input": "10", "output": "1" }, { "input": "11", "output": "5" }, { "input": "12", "output": "2" }, { "input": "13", "output": "6" }, { "input": "14", "output": "3" }, { "input": "15", "output": "7" }, { "input": "16", "output": "0" }, { "input": "17", "output": "8" }, { "input": "18", "output": "1" }, { "input": "19", "output": "9" }, { "input": "20", "output": "2" }, { "input": "99", "output": "49" }, { "input": "100", "output": "18" }, { "input": "9999", "output": "4999" }, { "input": "21736", "output": "2676" }, { "input": "873467", "output": "436733" }, { "input": "4124980", "output": "1013914" }, { "input": "536870910", "output": "134217727" }, { "input": "536870912", "output": "0" }, { "input": "876543210", "output": "169836149" }, { "input": "987654321", "output": "493827160" }, { "input": "1000000000", "output": "231564544" } ]
61
0
0
24,274
480
Parking Lot
[ "data structures", "divide and conquer" ]
null
null
Petya's been bored at work and he is killing the time by watching the parking lot at the office. The parking lot looks from above like an *n*<=×<=*m* table (a cell of the table corresponds to a single parking spot). Some spots in the parking lot are taken, others are empty. Petya watches cars riding into the parking lot one by one. After a car settles down at the parking spot, Petya amuzes himself by counting what maximum square of empty spots (i.e. a square subtable) can be seen on the parking lot if we look at it from above. Also, he takes notes of the square's size (side length) in his notebook. You task is: given the state of the parking lot at the initial moment of time and the information about where the arriving cars park, restore what Petya wrote in his notebook. It is midday, so nobody leaves the lot.
The first line contains three integers *n*, *m* and *k* — the sizes of the parking lot and the number of arriving cars after Petya started his watch (1<=≤<=*n*,<=*m*,<=*k*<=≤<=2000). Each of the following *n* lines contains *m* characters 'X' and '.', where 'X' means a taken spot and '.' means an empty spot. Each of the next *k* lines contains a pair of integers *x**i*, *y**i* — the number of row and column of the spot the corresponding car takes (1<=≤<=*x**i*<=≤<=*n*, 1<=≤<=*y**i*<=≤<=*m*). It is guaranteed that this place was empty. You can assume that a car enters a parking lot only after the previous car successfully finds a spot.
Print *k* integers — the length of the side of the maximum square of empty spots after the corresponding car has entered the parking lot.
[ "7 8 4\n........\nX.....X.\n........\n........\n.X......\n........\n........\n1 5\n6 4\n3 5\n4 6\n" ]
[ "5\n4\n4\n3\n" ]
none
[]
30
0
0
24,329
809
Hitchhiking in the Baltic States
[ "data structures", "dp" ]
null
null
Leha and Noora decided to go on a trip in the Baltic States. As you know from the previous problem, Leha has lost his car on the parking of the restaurant. Unfortunately, requests to the watchman didn't helped hacker find the car, so friends decided to go hitchhiking. In total, they intended to visit *n* towns. However it turned out that sights in *i*-th town are open for visitors only on days from *l**i* to *r**i*. What to do? Leha proposed to choose for each town *i* a day, when they will visit this town, i.e any integer *x**i* in interval [*l**i*,<=*r**i*]. After that Noora choses some subsequence of towns *id*1,<=*id*2,<=...,<=*id**k*, which friends are going to visit, that at first they are strictly increasing, i.e *id**i*<=&lt;<=*id**i*<=+<=1 is for all integers *i* from 1 to *k*<=-<=1, but also the dates of the friends visits are strictly increasing, i.e *x**id**i*<=&lt;<=*x**id**i*<=+<=1 is true for all integers *i* from 1 to *k*<=-<=1. Please help Leha and Noora in choosing such *x**i* for each town *i*, and such subsequence of towns *id*1,<=*id*2,<=...,<=*id**k*, so that friends can visit maximal number of towns. You may assume, that Leha and Noora can start the trip any day.
The first line contains a single integer *n* (1<=≤<=*n*<=≤<=3·105) denoting the number of towns Leha and Noora intended to visit. Each line *i* of the *n* subsequent lines contains two integers *l**i*, *r**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=109), denoting that sights in *i*-th town are open for visitors on any day .
Print a single integer denoting the maximal number of towns, that Leha and Noora can visit.
[ "5\n6 6\n1 2\n3 4\n2 2\n1 4\n" ]
[ "3\n" ]
Consider the first example. Let's take this plan: let's visit the sight in the second town on the first day, in the third town on the third day and in the fifth town on the fourth. That's would be the optimal answer.
[]
46
0
0
24,334
706
Hard problem
[ "dp", "strings" ]
null
null
Vasiliy is fond of solving different tasks. Today he found one he wasn't able to solve himself, so he asks you to help. Vasiliy is given *n* strings consisting of lowercase English letters. He wants them to be sorted in lexicographical order (as in the dictionary), but he is not allowed to swap any of them. The only operation he is allowed to do is to reverse any of them (first character becomes last, second becomes one before last and so on). To reverse the *i*-th string Vasiliy has to spent *c**i* units of energy. He is interested in the minimum amount of energy he has to spent in order to have strings sorted in lexicographical order. String *A* is lexicographically smaller than string *B* if it is shorter than *B* (|*A*|<=&lt;<=|*B*|) and is its prefix, or if none of them is a prefix of the other and at the first position where they differ character in *A* is smaller than the character in *B*. For the purpose of this problem, two equal strings nearby do not break the condition of sequence being sorted lexicographically.
The first line of the input contains a single integer *n* (2<=≤<=*n*<=≤<=100<=000) — the number of strings. The second line contains *n* integers *c**i* (0<=≤<=*c**i*<=≤<=109), the *i*-th of them is equal to the amount of energy Vasiliy has to spent in order to reverse the *i*-th string. Then follow *n* lines, each containing a string consisting of lowercase English letters. The total length of these strings doesn't exceed 100<=000.
If it is impossible to reverse some of the strings such that they will be located in lexicographical order, print <=-<=1. Otherwise, print the minimum total amount of energy Vasiliy has to spent.
[ "2\n1 2\nba\nac\n", "3\n1 3 1\naa\nba\nac\n", "2\n5 5\nbbb\naaa\n", "2\n3 3\naaa\naa\n" ]
[ "1\n", "1\n", "-1\n", "-1\n" ]
In the second sample one has to reverse string 2 or string 3. To amount of energy required to reverse the string 3 is smaller. In the third sample, both strings do not change after reverse and they go in the wrong order, so the answer is  - 1. In the fourth sample, both strings consists of characters 'a' only, but in the sorted order string "aa" should go before string "aaa", thus the answer is  - 1.
[ { "input": "2\n1 2\nba\nac", "output": "1" }, { "input": "3\n1 3 1\naa\nba\nac", "output": "1" }, { "input": "2\n5 5\nbbb\naaa", "output": "-1" }, { "input": "2\n3 3\naaa\naa", "output": "-1" }, { "input": "4\n0 0 8 6\nbi\nqp\nbt\nya", "output": "8" }, { "input": "5\n8 0 4 8 2\nac\ncl\ngg\ngm\nfs", "output": "2" }, { "input": "10\n7 7 0 0 0 1 6 6 7 3\ndv\ngb\nvg\nxg\nkt\nml\nqm\nnq\nrt\nxn", "output": "6" }, { "input": "3\n999999999 999999999 999999999\nxosbqqnmxq\nsdbvjhvytx\naydpuidgvy", "output": "1999999998" }, { "input": "3\n228 1488 228\nkek\nlol\nmda", "output": "0" }, { "input": "2\n1 1\naa\naa", "output": "0" }, { "input": "2\n1000000000 1000000000\nba\nac", "output": "1000000000" }, { "input": "5\n1000000000 1000000000 1000000000 1000000000 1000000000\nea\ndb\ncc\nbd\nae", "output": "4000000000" }, { "input": "3\n1000000000 1000000000 1000000000\nca\nda\nab", "output": "3000000000" }, { "input": "2\n1000000000 1000000000\naba\naab", "output": "1000000000" }, { "input": "3\n1000000000 1000000000 1000000000\nza\nyb\nxc", "output": "2000000000" }, { "input": "11\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\naz\nzb\nzc\nzd\nez\nfz\ngz\nzh\niz\nzj\nkz", "output": "5000000000" }, { "input": "10\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\nzaaaaaaaaaa\nyaaaaaaaaa\nxaaaaaaaa\nwaaaaaaa\nvaaaaaa\nuaaaaa\ntaaaa\nsaaa\nraa\nqa", "output": "9000000000" }, { "input": "6\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\nza\nyb\nxc\nwd\nve\nuf", "output": "5000000000" }, { "input": "5\n0 1000000000 1000000000 1000000000 1000000000\nb\nab\nab\nab\nab", "output": "4000000000" }, { "input": "4\n1000000000 1000000000 1000000000 1000000000\nzaaaaaaaaaaaaaaa\nybbbbbbbbbbbbbbb\nxccccccccccccccc\nwddddddddddddddd", "output": "3000000000" }, { "input": "6\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\nba\nab\ndc\ncd\nfe\nef", "output": "3000000000" }, { "input": "10\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\naaaaaaaaaaab\nab\naab\naaab\naaaab\naaaaab\naaaaaab\naaaaaaab\naaaaaaaab\naaaaaaaaab", "output": "8000000000" }, { "input": "13\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\naz\nab\nac\nad\nae\naf\nag\nah\nai\naj\nak\nal\nam", "output": "12000000000" }, { "input": "3\n1000000000 1000000000 1000000000\ndaa\ncab\nbac", "output": "2000000000" }, { "input": "4\n1000000000 1000000000 1000000000 1000000000\nza\nbt\ncm\ndn", "output": "1000000000" }, { "input": "4\n1000000000 1000000000 1000000000 1000000000\nza\nyb\nxc\nwd", "output": "3000000000" }, { "input": "9\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\na\nba\nab\nc\ndc\ncd\nx\nyx\nxy", "output": "3000000000" }, { "input": "10\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\nbaaa\ncaaa\nbbbb\naaac\naaad\neeee\nadff\ngggg\nadah\naaai", "output": "7000000000" }, { "input": "3\n900000000 3 87654321\nda\nbb\nad", "output": "987654321" } ]
62
0
0
24,348
774
Stepan's Series
[ "*special", "dp" ]
null
null
Well, the series which Stepan watched for a very long time, ended. In total, the series had *n* episodes. For each of them, Stepan remembers either that he definitely has watched it, or that he definitely hasn't watched it, or he is unsure, has he watched this episode or not. Stepan's dissatisfaction is the maximum number of consecutive series that Stepan did not watch. Your task is to determine according to Stepan's memories if his dissatisfaction could be exactly equal to *k*.
The first line contains two integers *n* and *k* (1<=≤<=*n*<=≤<=100, 0<=≤<=*k*<=≤<=*n*) — the number of episodes in the series and the dissatisfaction which should be checked. The second line contains the sequence which consists of *n* symbols "Y", "N" and "?". If the *i*-th symbol equals "Y", Stepan remembers that he has watched the episode number *i*. If the *i*-th symbol equals "N", Stepan remembers that he hasn't watched the epizode number *i*. If the *i*-th symbol equals "?", Stepan doesn't exactly remember if he has watched the episode number *i* or not.
If Stepan's dissatisfaction can be exactly equal to *k*, then print "YES" (without qoutes). Otherwise print "NO" (without qoutes).
[ "5 2\nNYNNY\n", "6 1\n????NN\n" ]
[ "YES\n", "NO\n" ]
In the first test Stepan remembers about all the episodes whether he has watched them or not. His dissatisfaction is 2, because he hasn't watch two episodes in a row — the episode number 3 and the episode number 4. The answer is "YES", because *k* = 2. In the second test *k* = 1, Stepan's dissatisfaction is greater than or equal to 2 (because he remembers that he hasn't watch at least two episodes in a row — number 5 and number 6), even if he has watched the episodes from the first to the fourth, inclusive.
[ { "input": "5 2\nNYNNY", "output": "YES" }, { "input": "6 1\n????NN", "output": "NO" }, { "input": "100 8\nNYNNY?YNNNNNN?NNNNNYNY?YYNYNN?NNNY??NNYNYNNNYNNNYNNNNNNNNY?NNNYNYN?NNNY?YY?NNYNN?NNNYNNYNNYN?NNYNYNN", "output": "YES" }, { "input": "10 1\nNY???NY?Y?", "output": "YES" }, { "input": "20 7\nN?N??NNN?NNN?Y???Y??", "output": "YES" }, { "input": "30 1\nNYYYNYYY?Y?YY?YYYYYYYYYYYYYNYY", "output": "YES" }, { "input": "40 14\nNNNNNNNNNNNNNNNNNYNNNNYNNYNNNNNNYNNNNNNN", "output": "NO" }, { "input": "51 1\nYYYNYNYNNYYNNY?YNYYYYYYNNYNYN??NYNYYNYYYYYYNNYNNNYY", "output": "NO" }, { "input": "70 3\nYNNNYYYNY?YYNYYNYYN?NYYYYYYYYYYYYYNYYNNYYYYYYYNYYNNNY??YYNYYYYYYYYNYYN", "output": "YES" }, { "input": "85 10\nYNNYNNNNNYNNNNNNNNNNNYNYYNNYNNNYYYNNNYYNNNNYNNNYNNNYNNNNNNNNNNNNN?NNNNYNNYYNNNNNNYNNN", "output": "NO" }, { "input": "90 18\nNNNN?NNNNNYNYNYNNY?NNNNNNNNNNNNNNYNNNNNNYYNYYNNNNYNNNNNNNNNNNNNNNNNNNYNNYYNYNNNNNNNYNNNNYN", "output": "NO" }, { "input": "99 2\nYNYYYYYYYYYYYN?YYNYYYYYYYYYYYYYY?YYYNYYYYYYYYYYYYYNYYYYYYNY?YYYYYNNYYYNYNYYYYNYYYYYYYYYYYNYY?NYYYYY", "output": "YES" }, { "input": "100 74\nNNNNNNNNNNNNNNNNNNNNNNNNYNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN?NNNNNNNNNNNN?NNNNNNNNNNNNNN", "output": "NO" }, { "input": "100 19\nYYNN?NNNNNNNNNNNYNYYNYNNNNNNNNNNNNNNNNNNNNNNYNNNNNNNNYNNNNNNYNNYYNNNYNNNYNYNNYNNNYYNNNYNNN?NNNNN?YNN", "output": "NO" }, { "input": "100 10\nNNNNYNNNYNNNNNNNNYNYNYNNNNNYNNNNNYNNNNNNNNNNNYNYYNNNNNNNYYNNYNYNNYYNNNNYNNNNNYNNNNYNNNNYNNY??YNNNNYY", "output": "NO" }, { "input": "100 4\nYYNNNNYYYNNNNNNYNYYYNYYNYYNNYYNNNNNNNYNYYNYYNNYNNNNNYN?YNYYYNNYNNNNNYNNNNYYNYYYYYNYNNNNYYNNNNYNNNNYY", "output": "NO" }, { "input": "100 2\nYYNNYYYNNYYYYYYYYYYYYYYYNYYYNYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYNYNYYYYYYNNYYYNYYNNYYNYYYYNYNYYYYYYNYYY", "output": "YES" }, { "input": "100 3\nYYYYYYYYNNNYNYNYYYYNY?YYYYYYNYYYNYYYYYYYYYYYYNNYYYYYNYNYYNYYYYYYYYYYYYYYYYYYY?YYNNYYNNYYYNYYYYYYYYYY", "output": "YES" }, { "input": "100 2\nYYYYYYYYYYYNYYYYYYYYYYYYYYYYYYYYYYYYYNYY?YYYYYYYYYYYYYYYNYYYYYYYYYYYYNNYYYYYYYYYNYYYYYYYYYYNYYYYYYYY", "output": "YES" }, { "input": "100 3\nNYNNYYYYYYNYNNYYYYYYNYYNYNYYYYYNYYYYYNNNYYYYYNYNYYNYYNYYNYNNNYYNYYYYYNYYYYYYNNYYNYNNYYNYYYY?YYNNYYNN", "output": "YES" }, { "input": "100 26\nNNYNNNNNNNNNNNNN?NNNNNNNNNNNNNYNNNNYNNNNNNNNNNNNYNNNNNN?NNNYNNNNNNNNNNYYNNNNNNNNYNNNNNNNNYYYNNNNYYNY", "output": "NO" }, { "input": "1 1\nY", "output": "NO" }, { "input": "1 1\nN", "output": "YES" }, { "input": "1 1\n?", "output": "YES" }, { "input": "1 0\n?", "output": "YES" }, { "input": "1 0\nN", "output": "NO" }, { "input": "1 0\nY", "output": "YES" }, { "input": "100 100\n????????????????????????????????????????????????????????????????????????????????????????????????????", "output": "YES" }, { "input": "6 4\nNN??NN", "output": "NO" }, { "input": "6 3\nNNYYN?", "output": "NO" }, { "input": "7 3\nN?YY???", "output": "YES" }, { "input": "24 4\nY?NYYNYYYNYYN?NNN?N?Y?Y?", "output": "NO" }, { "input": "3 3\n?Y?", "output": "NO" }, { "input": "10 1\nNY???NY?Y?", "output": "YES" }, { "input": "20 8\nNNNYY?????NN???N?YN?", "output": "YES" }, { "input": "30 2\n??????????????????????????????", "output": "YES" }, { "input": "40 17\nNNNNNNNNNNNNNNNNNYNNNNYNNYNNNNNNYNNNNNNN", "output": "YES" }, { "input": "51 5\nY??N????????Y??N?????N???N???YN?N?Y?N??Y?Y??Y???NN?", "output": "YES" }, { "input": "70 3\nY?N?Y???NN?NY?N?YY?Y????YNYY?Y?N??Y????YY??N????NY?NYY?YY?YYYY?YY?N?Y?", "output": "YES" }, { "input": "85 18\nNNNNNNN??Y???NN?YNNNNNNNN???YNNNNNN??Y?N?YNYYNN?NNNNNNNNNNNNNN????NNY??NNNN?NN??NNNNN", "output": "YES" }, { "input": "90 15\nYNNNNN?NNYNNYNNNN?NNNNYNNY?NNNNNNN?NNNNNNYN?NNYNNNNNN?NNYYNNYN?NNN??NNNNYNNN?YN?NNNNYNN?NY", "output": "YES" }, { "input": "99 1\nYYYYYYYNYYY??YY??YYYYYYY????NYY?YYY?Y??YYYY????YY?YY?YYY?YY??YYY?Y??NYYYY?YNYY??Y??YYYYY?YYY????YYY", "output": "YES" }, { "input": "100 34\n?NNNN??N???NNNN?NNN?N???N?N????NNNNNNN?N??N???NNNN???N?N?NN?NNNNN?NNN???N??NN??Y??NNN??N?NNN???NN?NN", "output": "YES" }, { "input": "100 21\n?NNNNNYNN??NNN?N????N?NN?N??NN?NNNY?NN?NY?NN?NNN?NN?N?NNNNNNY?NYNN??N??NYNN?NN?NNNN?N???NN?NN?Y?NYNY", "output": "YES" }, { "input": "100 10\nN?NNYYYNNNNNNYYNNYYNNNNNNNNYYNNNYYNNYNYNY?NNNNNNNNNYYNNNNYNNNNYNNNYNNYNNN?NNY?NNNNNNNNN?NYNYNNNNNNNN", "output": "YES" }, { "input": "100 6\n????????????????????????????????????????????????????????????????????????????????????????????????????", "output": "YES" }, { "input": "100 2\nYYNNYYYNNYYYYYYYYYYYYYYYNYYYNYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYNYNYYYYYYNNYYYNYYNNYYNYYYYNYNYYYYYYNYYY", "output": "YES" }, { "input": "100 1\n???Y??????????????????????????????????????Y?????????N???Y????????Y?????Y???????Y??Y??????????YY?????", "output": "YES" }, { "input": "100 1\nYYYYYYYYY??YYN?YYNYYYYYYYNYYYYYYYYYYY?YN?YYYYY?YYYYYYYYYYYYY?YYYYYYYYYYYYN?YYYYYYYY?YYYYY?YYNYYYYYNY", "output": "YES" }, { "input": "100 3\n?YNNYYNYYYYYYNYYYYYNY?NNYYYYNYY??NYYNYNYYYY?YYNYYNYYYYYYYYYYNYYYYNYYYYNYYYYNYYNYYYYYYNYNYNYYYYYYNNYY", "output": "YES" }, { "input": "3 2\n?Y?", "output": "NO" } ]
93
307,200
0
24,358
556
Case of Fake Numbers
[ "brute force", "implementation" ]
null
null
Andrewid the Android is a galaxy-famous detective. He is now investigating a case of frauds who make fake copies of the famous Stolp's gears, puzzles that are as famous as the Rubik's cube once was. Its most important components are a button and a line of *n* similar gears. Each gear has *n* teeth containing all numbers from 0 to *n*<=-<=1 in the counter-clockwise order. When you push a button, the first gear rotates clockwise, then the second gear rotates counter-clockwise, the the third gear rotates clockwise an so on. Besides, each gear has exactly one active tooth. When a gear turns, a new active tooth is the one following after the current active tooth according to the direction of the rotation. For example, if *n*<==<=5, and the active tooth is the one containing number 0, then clockwise rotation makes the tooth with number 1 active, or the counter-clockwise rotating makes the tooth number 4 active. Andrewid remembers that the real puzzle has the following property: you can push the button multiple times in such a way that in the end the numbers on the active teeth of the gears from first to last form sequence 0,<=1,<=2,<=...,<=*n*<=-<=1. Write a program that determines whether the given puzzle is real or fake.
The first line contains integer *n* (1<=≤<=*n*<=≤<=1000) — the number of gears. The second line contains *n* digits *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=≤<=*n*<=-<=1) — the sequence of active teeth: the active tooth of the *i*-th gear contains number *a**i*.
In a single line print "Yes" (without the quotes), if the given Stolp's gears puzzle is real, and "No" (without the quotes) otherwise.
[ "3\n1 0 0\n", "5\n4 2 1 4 3\n", "4\n0 2 3 1\n" ]
[ "Yes\n", "Yes\n", "No\n" ]
In the first sample test when you push the button for the first time, the sequence of active teeth will be 2 2 1, when you push it for the second time, you get 0 1 2.
[ { "input": "3\n1 0 0", "output": "Yes" }, { "input": "5\n4 2 1 4 3", "output": "Yes" }, { "input": "4\n0 2 3 1", "output": "No" }, { "input": "1\n0", "output": "Yes" }, { "input": "2\n1 0", "output": "Yes" }, { "input": "5\n2 4 4 1 1", "output": "Yes" }, { "input": "10\n0 1 2 3 4 5 6 7 8 9", "output": "Yes" }, { "input": "10\n1 5 1 0 6 1 3 5 9 9", "output": "No" }, { "input": "20\n10 11 12 13 14 15 16 17 18 19 0 1 2 3 4 5 6 7 8 9", "output": "Yes" }, { "input": "20\n2 15 18 0 3 12 4 16 13 7 17 9 8 2 15 18 19 17 5 8", "output": "No" }, { "input": "50\n38 13 40 15 42 17 44 19 46 21 48 23 0 25 2 27 4 29 6 31 8 33 10 35 12 37 14 39 16 41 18 43 20 45 22 47 24 49 26 1 28 3 30 5 32 7 34 9 36 11", "output": "Yes" }, { "input": "50\n12 16 9 0 18 36 5 42 28 1 4 43 38 35 14 37 39 13 23 15 37 45 31 38 20 13 18 38 1 13 5 24 35 11 0 44 16 42 8 10 27 3 1 33 33 29 29 33 49 48", "output": "No" }, { "input": "100\n15 86 17 88 19 90 21 92 23 94 25 96 27 98 29 0 31 2 33 4 35 6 37 8 39 10 41 12 43 14 45 16 47 18 49 20 51 22 53 24 55 26 57 28 59 30 61 32 63 34 65 36 67 38 69 40 71 42 73 44 75 46 77 48 79 50 81 52 83 54 85 56 87 58 89 60 91 62 93 64 95 66 97 68 99 70 1 72 3 74 5 76 7 78 9 80 11 82 13 84", "output": "Yes" }, { "input": "15\n2 14 4 1 6 3 8 5 10 7 12 9 14 11 1", "output": "Yes" }, { "input": "30\n19 12 21 14 23 16 25 18 27 20 29 22 1 24 3 26 5 28 7 0 9 2 11 4 13 6 15 8 17 10", "output": "Yes" }, { "input": "50\n27 24 29 26 31 28 33 30 35 32 37 34 39 36 41 38 43 40 45 42 47 44 49 46 1 48 3 0 5 2 7 4 9 6 11 8 13 10 15 12 17 14 19 16 21 18 23 20 25 22", "output": "Yes" }, { "input": "50\n23 19 25 45 22 37 49 22 36 16 30 19 21 4 46 28 39 19 24 12 22 36 41 46 34 40 11 46 29 10 6 21 30 29 6 5 49 45 13 39 34 17 44 45 28 34 7 43 15 25", "output": "No" }, { "input": "100\n95 6 97 8 99 10 1 12 3 14 5 16 7 18 9 20 11 22 13 24 15 26 17 28 19 30 21 32 23 34 25 36 27 38 29 40 31 42 33 44 35 46 37 48 39 50 41 52 43 54 45 56 47 58 49 60 51 62 53 64 55 66 57 68 59 70 61 72 63 74 65 76 67 78 69 80 71 82 73 84 75 86 77 88 79 90 81 92 83 94 85 96 87 98 89 0 91 2 93 4", "output": "Yes" }, { "input": "100\n20 45 10 74 41 81 78 74 91 17 21 65 89 63 7 44 97 3 1 89 87 44 32 10 44 73 73 88 62 77 96 59 4 8 55 1 82 52 63 11 59 53 9 47 29 73 38 44 36 24 72 61 24 11 38 6 91 4 0 65 37 58 20 56 70 87 54 42 79 45 46 73 7 85 43 76 69 35 72 79 38 6 8 0 49 39 32 24 28 54 8 43 98 65 50 20 22 23 2 85", "output": "No" }, { "input": "100\n79 74 22 11 73 70 33 50 9 81 17 14 23 44 4 90 20 22 19 94 66 80 70 42 22 82 49 42 36 7 90 91 80 33 26 52 6 77 30 94 99 6 46 84 96 40 89 2 88 65 80 93 5 60 25 15 32 26 68 85 62 74 69 55 84 0 85 91 23 43 84 94 25 65 28 92 16 0 7 83 48 74 15 20 5 97 34 42 99 97 18 39 21 23 95 77 42 17 32 94", "output": "No" }, { "input": "100\n74 27 76 29 78 31 80 33 82 35 84 37 86 39 88 41 90 43 92 45 94 47 96 49 98 51 0 53 2 55 4 57 6 59 8 61 10 63 12 65 14 67 16 69 18 71 20 73 22 75 24 77 26 79 28 81 30 83 32 85 34 87 36 89 38 91 40 93 42 95 44 97 46 99 48 1 50 3 52 5 54 7 56 9 58 11 60 13 62 15 64 17 66 19 68 21 70 23 72 25", "output": "Yes" }, { "input": "2\n0 0", "output": "No" }, { "input": "2\n0 1", "output": "Yes" }, { "input": "2\n1 0", "output": "Yes" }, { "input": "2\n1 1", "output": "No" }, { "input": "3\n0 1 1", "output": "No" }, { "input": "3\n0 2 2", "output": "No" }, { "input": "4\n0 1 2 2", "output": "No" }, { "input": "3\n1 1 0", "output": "No" } ]
46
0
3
24,469
69
Game
[ "implementation" ]
C. Game
2
256
In one school with Vasya there is a student Kostya. Kostya does not like physics, he likes different online games. Every day, having come home, Kostya throws his bag in the farthest corner and sits down at his beloved computer. Kostya even eats glued to the game. A few days ago Kostya bought a new RPG game "HaresButtle", which differs from all other games in this genre. It has a huge number of artifacts. As we know, artifacts are divided into basic and composite ones. Only the basic artifacts are available on sale. More powerful composite artifacts are collected from some number of basic artifacts. After the composing composite artifact, all the components disappear. Kostya is the head of the alliance, so he has to remember, what artifacts has not only himself, but also his allies. You must identify by sequence of artifacts purchased by Kostya and his allies, how many and which artifacts has been collected by each of them. It is believed that initially no one has any artifacts.
The first line has 4 natural numbers: *k* (1<=≤<=*k*<=≤<=100) — the number of Kostya's allies, *n* (1<=≤<=*n*<=≤<=50) — the number of basic artifacts, *m* (0<=≤<=*m*<=≤<=50) — the number of composite artifacts, *q* (1<=≤<=*q*<=≤<=500) — the number of his friends' purchases. The following *n* lines contain the names of basic artifacts. After them *m* lines contain the descriptions of composite artifacts in the following format: &lt;Art. Name&gt;: &lt;Art. №1&gt; &lt;Art. №1 Number&gt;, &lt;Art. №2&gt; &lt;Art. №2 Number&gt;, ... &lt;Art. №X&gt; &lt;Art. №Х Number&gt; All the numbers are natural numbers not exceeding 100 (1<=≤<=*X*<=≤<=*n*). The names of all artifacts are different, they are composed of lowercase Latin letters, and the length of each name is from 1 to 100 characters inclusive. All the words in the format of the description of a composite artifact are separated by exactly one space. It is guaranteed that all components of the new artifact are different and have already been met in the input data as the names of basic artifacts. Next, each of the following *q* lines is characterized by the number *a**i*, the number of a friend who has bought the artifact (1<=≤<=*a**i*<=≤<=*k*), and the name of the purchased basic artifact. Let's assume that the backpacks of the heroes are infinitely large and any artifact bought later can fit in there. It is guaranteed that after the *i*-th purchase no more than one opportunity to collect the composite artifact appears. If such an opportunity arose, the hero must take advantage of it.
The output file should consist of *k* blocks. The first line should contain number *b**i* — the number of different artifacts the *i*-th ally has. Then the block should contain *b**i* lines with the names of these artifacts and the number of these artifacts. At that the lines should be printed in accordance with the lexicographical order of the names of the artifacts. In each block all the artifacts must be different, and all the numbers except the *b**i* should be positive.
[ "2 3 2 5\ndesolator\nrefresher\nperseverance\nvanguard: desolator 1, refresher 1\nmaelstorm: perseverance 2\n1 desolator\n2 perseverance\n1 refresher\n2 desolator\n2 perseverance\n" ]
[ "1\nvanguard 1\n2\ndesolator 1\nmaelstorm 1\n" ]
none
[ { "input": "2 3 2 5\ndesolator\nrefresher\nperseverance\nvanguard: desolator 1, refresher 1\nmaelstorm: perseverance 2\n1 desolator\n2 perseverance\n1 refresher\n2 desolator\n2 perseverance", "output": "1\nvanguard 1\n2\ndesolator 1\nmaelstorm 1" }, { "input": "2 3 2 5\na\nb\nc\nd: a 1, b 1\ne: c 2\n1 a\n2 c\n1 b\n2 c\n2 c", "output": "1\nd 1\n2\nc 1\ne 1" }, { "input": "10 1 1 3\na\nb: a 2\n1 a\n1 a\n1 a", "output": "2\na 1\nb 1\n0\n0\n0\n0\n0\n0\n0\n0\n0" }, { "input": "2 3 2 5\ndesolator\nrefresher\nperseverance\nvanguard: desolator 1, refresher 1\nmaelstorm: refresher 2\n1 desolator\n2 perseverance\n1 refresher\n1 refresher\n2 perseverance", "output": "2\nrefresher 1\nvanguard 1\n1\nperseverance 2" }, { "input": "1 1 1 1\na\nb: a 1\n1 a", "output": "1\nb 1" }, { "input": "2 3 2 5\ndesolator\nrefresher\nperseverance\nvanguard: desolator 1, refresher 3\nmaelstorm: refresher 2\n1 desolator\n2 perseverance\n1 refresher\n1 refresher\n2 perseverance", "output": "2\ndesolator 1\nmaelstorm 1\n1\nperseverance 2" }, { "input": "1 2 0 2\nb\na\n1 a\n1 b", "output": "2\na 1\nb 1" }, { "input": "2 3 2 5\na\nb\nc\nd: a 1, b 21\ne: c 2\n1 a\n2 c\n1 b\n2 c\n2 c", "output": "2\na 1\nb 1\n2\nc 1\ne 1" }, { "input": "1 1 2 2\na\nb: a 2\nc: a 1\n1 a\n1 a", "output": "1\nc 2" } ]
186
307,200
0
24,626
662
International Olympiad
[ "constructive algorithms", "greedy", "implementation", "math" ]
null
null
International Abbreviation Olympiad takes place annually starting from 1989. Each year the competition receives an abbreviation of form IAO'y, where *y* stands for some number of consequent last digits of the current year. Organizers always pick an abbreviation with non-empty string *y* that has never been used before. Among all such valid abbreviations they choose the shortest one and announce it to be the abbreviation of this year's competition. For example, the first three Olympiads (years 1989, 1990 and 1991, respectively) received the abbreviations IAO'9, IAO'0 and IAO'1, while the competition in 2015 received an abbreviation IAO'15, as IAO'5 has been already used in 1995. You are given a list of abbreviations. For each of them determine the year it stands for.
The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=1000) — the number of abbreviations to process. Then *n* lines follow, each containing a single abbreviation. It's guaranteed that each abbreviation contains at most nine digits.
For each abbreviation given in the input, find the year of the corresponding Olympiad.
[ "5\nIAO'15\nIAO'2015\nIAO'1\nIAO'9\nIAO'0\n", "4\nIAO'9\nIAO'99\nIAO'999\nIAO'9999\n" ]
[ "2015\n12015\n1991\n1989\n1990\n", "1989\n1999\n2999\n9999\n" ]
none
[ { "input": "5\nIAO'15\nIAO'2015\nIAO'1\nIAO'9\nIAO'0", "output": "2015\n12015\n1991\n1989\n1990" }, { "input": "4\nIAO'9\nIAO'99\nIAO'999\nIAO'9999", "output": "1989\n1999\n2999\n9999" }, { "input": "1\nIAO'111110", "output": "1111110" }, { "input": "2\nIAO'0\nIAO'00", "output": "1990\n2000" }, { "input": "1\nIAO'111111", "output": "1111111" }, { "input": "1\nIAO'111111111", "output": "1111111111" }, { "input": "1\nIAO'001", "output": "3001" }, { "input": "1\nIAO'2000", "output": "12000" }, { "input": "1\nIAO'11109999", "output": "111109999" }, { "input": "1\nIAO'11111", "output": "111111" }, { "input": "1\nIAO'100000", "output": "1100000" }, { "input": "1\nIAO'18999990", "output": "18999990" }, { "input": "1\nIAO'113098", "output": "1113098" }, { "input": "1\nIAO'111122", "output": "1111122" }, { "input": "1\nIAO'1110222", "output": "11110222" }, { "input": "1\nIAO'11133333", "output": "11133333" }, { "input": "1\nIAO'000000000", "output": "1000000000" }, { "input": "4\nIAO'3098\nIAO'99\nIAO'999\nIAO'9999", "output": "13098\n1999\n2999\n9999" }, { "input": "1\nIAO'11100000", "output": "111100000" }, { "input": "2\nIAO'15\nIAO'15", "output": "2015\n2015" }, { "input": "1\nIAO'999999999", "output": "999999999" }, { "input": "1\nIAO'1112121", "output": "11112121" }, { "input": "1\nIAO'111113098", "output": "1111113098" }, { "input": "1\nIAO'10005000", "output": "110005000" }, { "input": "1\nIAO'111378", "output": "1111378" }, { "input": "1\nIAO'112222", "output": "1112222" }, { "input": "1\nIAO'021113099", "output": "1021113099" }, { "input": "1\nIAO'123456789", "output": "123456789" }, { "input": "1\nIAO'000000001", "output": "1000000001" }, { "input": "1\nIAO'089", "output": "3089" }, { "input": "9\nIAO'0\nIAO'00\nIAO'000\nIAO'0000\nIAO'00000\nIAO'000000\nIAO'0000000\nIAO'00000000\nIAO'000000000", "output": "1990\n2000\n3000\n10000\n100000\n1000000\n10000000\n100000000\n1000000000" }, { "input": "2\nIAO'999999999\nIAO'999999999", "output": "999999999\n999999999" }, { "input": "1\nIAO'2015", "output": "12015" }, { "input": "1\nIAO'113097", "output": "1113097" }, { "input": "1\nIAO'11378", "output": "111378" } ]
124
2,252,800
0
24,628
492
Vanya and Field
[ "math" ]
null
null
Vanya decided to walk in the field of size *n*<=×<=*n* cells. The field contains *m* apple trees, the *i*-th apple tree is at the cell with coordinates (*x**i*,<=*y**i*). Vanya moves towards vector (*dx*,<=*dy*). That means that if Vanya is now at the cell (*x*,<=*y*), then in a second he will be at cell . The following condition is satisfied for the vector: , where is the largest integer that divides both *a* and *b*. Vanya ends his path when he reaches the square he has already visited. Vanya wonders, from what square of the field he should start his path to see as many apple trees as possible.
The first line contains integers *n*,<=*m*,<=*dx*,<=*dy*(1<=≤<=*n*<=≤<=106, 1<=≤<=*m*<=≤<=105, 1<=≤<=*dx*,<=*dy*<=≤<=*n*) — the size of the field, the number of apple trees and the vector of Vanya's movement. Next *m* lines contain integers *x**i*,<=*y**i* (0<=≤<=*x**i*,<=*y**i*<=≤<=*n*<=-<=1) — the coordinates of apples. One cell may contain multiple apple trees.
Print two space-separated numbers — the coordinates of the cell from which you should start your path. If there are several answers you are allowed to print any of them.
[ "5 5 2 3\n0 0\n1 2\n1 3\n2 4\n3 1\n", "2 3 1 1\n0 0\n0 1\n1 1\n" ]
[ "1 3\n", "0 0\n" ]
In the first sample Vanya's path will look like: (1, 3) - (3, 1) - (0, 4) - (2, 2) - (4, 0) - (1, 3) In the second sample: (0, 0) - (1, 1) - (0, 0)
[ { "input": "5 5 2 3\n0 0\n1 2\n1 3\n2 4\n3 1", "output": "1 3" }, { "input": "2 3 1 1\n0 0\n0 1\n1 1", "output": "0 0" }, { "input": "5 5 2 4\n0 0\n1 2\n1 3\n2 4\n3 1", "output": "1 2" }, { "input": "6 6 5 5\n0 0\n0 1\n0 2\n0 3\n0 4\n0 5", "output": "0 0" }, { "input": "6 6 1 1\n0 0\n1 1\n2 1\n0 1\n1 2\n3 4", "output": "0 1" }, { "input": "1 1 1 1\n0 0", "output": "0 0" }, { "input": "10 10 7 3\n9 0\n0 0\n7 6\n6 5\n4 8\n0 3\n2 1\n9 2\n7 1\n8 6", "output": "0 3" }, { "input": "10 10 3 9\n0 0\n3 9\n6 8\n0 1\n3 0\n6 9\n0 2\n3 1\n6 0\n0 0", "output": "0 0" }, { "input": "4 1 3 3\n0 0", "output": "0 0" }, { "input": "4 1 3 3\n3 3", "output": "0 0" }, { "input": "4 1 1 3\n0 2", "output": "0 2" }, { "input": "4 1 1 3\n0 3", "output": "0 3" }, { "input": "4 3 3 3\n0 1\n0 3\n3 0", "output": "0 1" }, { "input": "4 3 3 3\n0 2\n0 3\n3 0", "output": "0 1" }, { "input": "4 3 3 3\n0 0\n0 0\n0 1", "output": "0 0" }, { "input": "4 3 1 3\n0 0\n0 3\n0 3", "output": "0 3" }, { "input": "2 2 1 1\n0 0\n1 1", "output": "0 0" }, { "input": "2 2 1 1\n0 0\n0 1", "output": "0 0" }, { "input": "2 15 1 1\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0", "output": "0 0" }, { "input": "2 15 1 1\n0 0\n0 1\n0 0\n0 1\n0 0\n0 1\n0 0\n0 1\n0 0\n0 1\n0 0\n0 1\n0 0\n0 1\n0 0", "output": "0 0" }, { "input": "2 15 1 1\n0 0\n0 1\n0 0\n0 1\n0 0\n0 1\n1 0\n0 1\n0 0\n0 1\n0 0\n0 1\n0 0\n0 1\n0 0", "output": "0 1" } ]
46
0
0
24,720
858
Polycarp's phone book
[ "data structures", "implementation", "sortings" ]
null
null
There are *n* phone numbers in Polycarp's contacts on his phone. Each number is a 9-digit integer, starting with a digit different from 0. All the numbers are distinct. There is the latest version of Berdroid OS installed on Polycarp's phone. If some number is entered, is shows up all the numbers in the contacts for which there is a substring equal to the entered sequence of digits. For example, is there are three phone numbers in Polycarp's contacts: 123456789, 100000000 and 100123456, then: - if he enters 00 two numbers will show up: 100000000 and 100123456, - if he enters 123 two numbers will show up 123456789 and 100123456, - if he enters 01 there will be only one number 100123456. For each of the phone numbers in Polycarp's contacts, find the minimum in length sequence of digits such that if Polycarp enters this sequence, Berdroid shows this only phone number.
The first line contains single integer *n* (1<=≤<=*n*<=≤<=70000) — the total number of phone contacts in Polycarp's contacts. The phone numbers follow, one in each line. Each number is a positive 9-digit integer starting with a digit from 1 to 9. All the numbers are distinct.
Print exactly *n* lines: the *i*-th of them should contain the shortest non-empty sequence of digits, such that if Polycarp enters it, the Berdroid OS shows up only the *i*-th number from the contacts. If there are several such sequences, print any of them.
[ "3\n123456789\n100000000\n100123456\n", "4\n123456789\n193456789\n134567819\n934567891\n" ]
[ "9\n000\n01\n", "2\n193\n81\n91\n" ]
none
[ { "input": "3\n123456789\n100000000\n100123456", "output": "9\n000\n01" }, { "input": "4\n123456789\n193456789\n134567819\n934567891", "output": "2\n193\n81\n91" }, { "input": "1\n167038488", "output": "4" }, { "input": "5\n115830748\n403459907\n556271610\n430358099\n413961410", "output": "15\n40\n2\n35\n14" }, { "input": "5\n139127034\n452751056\n193432721\n894001929\n426470953", "output": "39\n05\n32\n8\n53" }, { "input": "5\n343216531\n914073407\n420246472\n855857272\n801664978", "output": "32\n07\n46\n27\n78" }, { "input": "5\n567323818\n353474649\n468171032\n989223926\n685081078", "output": "67\n35\n03\n26\n78" }, { "input": "5\n774610315\n325796798\n989859836\n707706423\n310546337", "output": "61\n32\n89\n23\n37" }, { "input": "10\n181033039\n210698534\n971006898\n391227170\n323096464\n560766866\n377374442\n654389922\n544146403\n779261493", "output": "18\n53\n97\n27\n09\n07\n42\n99\n41\n93" }, { "input": "10\n197120216\n680990683\n319631438\n442393410\n888300189\n170777450\n164487872\n487350759\n651751346\n652859411", "output": "12\n09\n43\n42\n01\n74\n72\n73\n46\n11" }, { "input": "10\n302417715\n621211824\n474451896\n961495400\n633841010\n839982537\n797812119\n510708100\n770758643\n228046084", "output": "15\n62\n47\n14\n33\n98\n97\n100\n43\n60" }, { "input": "10\n506504092\n561611075\n265260859\n557114891\n838578824\n985006846\n456984731\n856424964\n658005674\n666280709", "output": "40\n75\n60\n55\n78\n68\n31\n96\n74\n66" }, { "input": "10\n510613599\n931933224\n693094490\n508960931\n313762868\n396027639\n164098962\n749880019\n709024305\n498545812", "output": "61\n22\n44\n08\n68\n39\n64\n88\n43\n12" }, { "input": "3\n638631659\n929648227\n848163730", "output": "5\n2\n0" }, { "input": "4\n898855826\n343430636\n210120107\n467957087", "output": "98\n3\n1\n57" }, { "input": "5\n202080398\n357502772\n269676952\n711559315\n111366203", "output": "8\n57\n26\n93\n62" }, { "input": "9\n111111111\n111111110\n111111100\n111111000\n111110000\n111100000\n111000000\n110000000\n100000000", "output": "111111111\n111111110\n111111100\n111111000\n111110000\n111100000\n111000000\n110000000\n00000000" } ]
3,182
184,217,600
3
24,780
105
Transmigration
[ "implementation" ]
A. Transmigration
2
256
In Disgaea as in most role-playing games, characters have skills that determine the character's ability to use certain weapons or spells. If the character does not have the necessary skill, he cannot use it. The skill level is represented as an integer that increases when you use this skill. Different character classes are characterized by different skills. Unfortunately, the skills that are uncommon for the given character's class are quite difficult to obtain. To avoid this limitation, there is the so-called transmigration. Transmigration is reincarnation of the character in a new creature. His soul shifts to a new body and retains part of his experience from the previous life. As a result of transmigration the new character gets all the skills of the old character and the skill levels are reduced according to the *k* coefficient (if the skill level was equal to *x*, then after transmigration it becomes equal to [*kx*], where [*y*] is the integral part of *y*). If some skill's levels are strictly less than 100, these skills are forgotten (the character does not have them any more). After that the new character also gains the skills that are specific for his class, but are new to him. The levels of those additional skills are set to 0. Thus, one can create a character with skills specific for completely different character classes via transmigrations. For example, creating a mage archer or a thief warrior is possible. You are suggested to solve the following problem: what skills will the character have after transmigration and what will the levels of those skills be?
The first line contains three numbers *n*, *m* and *k* — the number of skills the current character has, the number of skills specific for the class into which the character is going to transmigrate and the reducing coefficient respectively; *n* and *m* are integers, and *k* is a real number with exactly two digits after decimal point (1<=≤<=*n*,<=*m*<=≤<=20, 0.01<=≤<=*k*<=≤<=0.99). Then follow *n* lines, each of which describes a character's skill in the form "*name* *exp*" — the skill's name and the character's skill level: *name* is a string and *exp* is an integer in range from 0 to 9999, inclusive. Then follow *m* lines each of which contains names of skills specific for the class, into which the character transmigrates. All names consist of lowercase Latin letters and their lengths can range from 1 to 20 characters, inclusive. All character's skills have distinct names. Besides the skills specific for the class into which the player transmigrates also have distinct names.
Print on the first line number *z* — the number of skills the character will have after the transmigration. Then print *z* lines, on each of which print a skill's name and level, separated by a single space. The skills should be given in the lexicographical order.
[ "5 4 0.75\naxe 350\nimpaler 300\nionize 80\nmegafire 120\nmagicboost 220\nheal\nmegafire\nshield\nmagicboost\n" ]
[ "6\naxe 262\nheal 0\nimpaler 225\nmagicboost 165\nmegafire 0\nshield 0\n" ]
none
[ { "input": "5 4 0.75\naxe 350\nimpaler 300\nionize 80\nmegafire 120\nmagicboost 220\nheal\nmegafire\nshield\nmagicboost", "output": "6\naxe 262\nheal 0\nimpaler 225\nmagicboost 165\nmegafire 0\nshield 0" }, { "input": "1 1 0.50\nstaff 1005\nionize", "output": "2\nionize 0\nstaff 502" }, { "input": "4 3 0.32\ninrf 48\nfdgdf 200\nvbkdfk 450\nfdbvfdd 1000\ndff\ninrf\nnfdkd", "output": "5\ndff 0\nfdbvfdd 320\ninrf 0\nnfdkd 0\nvbkdfk 144" }, { "input": "5 1 0.99\na 1\nb 2\nc 3\nd 4\ne 5\nf", "output": "1\nf 0" }, { "input": "2 2 0.02\nfn 1003\nzz 7000\nkk\nau", "output": "3\nau 0\nkk 0\nzz 140" }, { "input": "3 3 0.10\naa 900\nbb 990\ncc 999\naa\nbb\ncc", "output": "3\naa 0\nbb 0\ncc 0" }, { "input": "1 1 0.99\nfdvedvrgfckdkvfpmkjd 100\nfdvedvrgfckdkvfpmkjd", "output": "1\nfdvedvrgfckdkvfpmkjd 0" }, { "input": "1 1 0.01\na 9999\na", "output": "1\na 0" }, { "input": "1 1 0.80\nxyz 125\nxyz", "output": "1\nxyz 100" }, { "input": "5 1 0.67\ndjdn 6699\nolkj 6700\nhgvg 6698\noijggt 6701\nyfyv 6700\nyfyv", "output": "5\ndjdn 4488\nhgvg 4487\noijggt 4489\nolkj 4489\nyfyv 4489" }, { "input": "5 2 0.73\njcyuc 136\npooj 137\nojnbg 138\ninng 135\nuuv 139\nhg\nouohoiivuvu", "output": "5\nhg 0\nojnbg 100\nouohoiivuvu 0\npooj 100\nuuv 101" }, { "input": "4 1 0.99\nutctc 101\nijh 100\nfyyui 99\ntctxxx 102\nojohiobib", "output": "2\nojohiobib 0\ntctxxx 100" }, { "input": "4 4 0.80\nyfcyccyyccccc 123\nkkkkk 124\noops 125\nabfgg 126\nh\njkl\nqwerty\noops", "output": "5\nabfgg 100\nh 0\njkl 0\noops 100\nqwerty 0" }, { "input": "4 6 0.68\na 146\nb 147\nc 148\nd 149\ne\nf\ng\nh\ni\nj", "output": "8\nc 100\nd 101\ne 0\nf 0\ng 0\nh 0\ni 0\nj 0" }, { "input": "5 1 0.02\nirn 4999\nsdfc 5000\nzzzzzz 5001\ndcw 100\nfvvv 22\ndcw", "output": "3\ndcw 0\nsdfc 100\nzzzzzz 100" }, { "input": "5 5 0.18\nxwjxvrgz 9492\ndhpe 5259\nbnbkznfgyuluho 5070\nygpluaefwefxmhuaqi 2975\nvqstuwkaqk 8892\ndhpe\nbnbkznfgyuluho\nygpluaefwefxmhuaqi\nvyaefiicj\nxwjxvrgz", "output": "6\nbnbkznfgyuluho 912\ndhpe 946\nvqstuwkaqk 1600\nvyaefiicj 0\nxwjxvrgz 1708\nygpluaefwefxmhuaqi 535" }, { "input": "10 10 0.28\nszyiekxcixeyqyfm 7701\ncdxkfpggugy 5344\ngqyvyzwkajhc 3674\ntmo 8865\ntbp 8932\nwbrzxccfmdxbzw 4566\nvpgcejyragzhm 1554\ncqqjrh 7868\nw 1548\nxkbitfl 588\nlpcwvverv\nborcfgittei\nw\nzqtzpicsndbxfcbaduds\ncdxkfpggugy\ntmo\nmvmdaltjmy\nbzhykrayudljyj\nyktrcowlgwkvqucbqh\nvtm", "output": "17\nborcfgittei 0\nbzhykrayudljyj 0\ncdxkfpggugy 1496\ncqqjrh 2203\ngqyvyzwkajhc 1028\nlpcwvverv 0\nmvmdaltjmy 0\nszyiekxcixeyqyfm 2156\ntbp 2500\ntmo 2482\nvpgcejyragzhm 435\nvtm 0\nw 433\nwbrzxccfmdxbzw 1278\nxkbitfl 164\nyktrcowlgwkvqucbqh 0\nzqtzpicsndbxfcbaduds 0" }, { "input": "13 13 0.20\nbbdtfrykzf 6189\nnqwei 7327\ndtigwnbwevnnlinhk 3662\nxokqjtylly 1274\nsdpnhipam 5672\npfjmflvtuvctwxr 9580\nybqgomvwoguzcqvzkx 2062\nvowzavh 6345\nbfidjslqlesdtyjkreou 6780\nsvpzwtwn 1945\ninvzueipnifajadhjk 7034\nsz 6494\nce 1323\nybqgomvwoguzcqvzkx\nbbdtfrykzf\nvunbpghae\ndtigwnbwevnnlinhk\nuqdlfskhgo\nvdemdnxifb\nvowzavh\npfjmflvtuvctwxr\nbfidjslqlesdtyjkreou\nnqwei\nsz\njiemqkytxtqnxgjvhzjl\nce", "output": "17\nbbdtfrykzf 1237\nbfidjslqlesdtyjkreou 1356\nce 264\ndtigwnbwevnnlinhk 732\ninvzueipnifajadhjk 1406\njiemqkytxtqnxgjvhzjl 0\nnqwei 1465\npfjmflvtuvctwxr 1916\nsdpnhipam 1134\nsvpzwtwn 389\nsz 1298\nuqdlfskhgo 0\nvdemdnxifb 0\nvowzavh 1269\nvunbpghae 0\nxokqjtylly 254\nybqgomvwoguzcqvzkx 412" }, { "input": "1 17 0.97\nsfidbvqbvx 562\npmuvtjkw\nysxuhhfgwgifkf\nnsgdgacfdstvsf\ngggnzgevrtykq\nvmeytgyobqpmq\nrbzif\nfqbr\nepcy\ntvtgk\nsdwsny\nhuzsrlvxvufyb\niallwqylqga\nsemxysiafu\nodrxgpjgiiizlubtuv\nlenenatgyqep\nlzakhvoxfccct\nijkhhuppdghdwz", "output": "18\nepcy 0\nfqbr 0\ngggnzgevrtykq 0\nhuzsrlvxvufyb 0\niallwqylqga 0\nijkhhuppdghdwz 0\nlenenatgyqep 0\nlzakhvoxfccct 0\nnsgdgacfdstvsf 0\nodrxgpjgiiizlubtuv 0\npmuvtjkw 0\nrbzif 0\nsdwsny 0\nsemxysiafu 0\nsfidbvqbvx 545\ntvtgk 0\nvmeytgyobqpmq 0\nysxuhhfgwgifkf 0" }, { "input": "5 19 0.38\nmwfhslniu 2324\njyzifusxbigcagch 6167\nkccudxutkgb 9673\nuccmkylmiqcn 4773\niuawwcyefaimhro 214\njyzifusxbigcagch\nfalsionuewiyvseurg\nrkrvudkrhophdflqln\nahsybnxitvpm\nx\nancpcxgr\nsvs\nvvssivqobhdfqggahqu\npf\nwjtrtcvjqydxuwwvsqpc\nyllpzfjdojpymwy\nepjhkxffsymowea\nyztamblsfzk\nbej\nwy\nvnkvonk\nymsnsngzcvxeilbitknn\nlmaajt\nmwfhslniu", "output": "21\nahsybnxitvpm 0\nancpcxgr 0\nbej 0\nepjhkxffsymowea 0\nfalsionuewiyvseurg 0\njyzifusxbigcagch 2343\nkccudxutkgb 3675\nlmaajt 0\nmwfhslniu 883\npf 0\nrkrvudkrhophdflqln 0\nsvs 0\nuccmkylmiqcn 1813\nvnkvonk 0\nvvssivqobhdfqggahqu 0\nwjtrtcvjqydxuwwvsqpc 0\nwy 0\nx 0\nyllpzfjdojpymwy 0\nymsnsngzcvxeilbitknn 0\nyztamblsfzk 0" }, { "input": "13 10 0.35\napjqdcdylyads 948\ni 618\nsbifpsvflzngfziwx 6815\nlhuzbitj 8455\nzhoro 657\nfm 6899\nbhigr 6743\net 3322\nljbkmxj 3023\nloxxykp 6048\naiibfjdgd 965\nmmpylhw 5483\nyrbikjks 7426\nfm\njvj\napjqdcdylyads\nbhigr\naiibfjdgd\nljbkmxj\nauftuqyphmz\nloxxykp\nzhoro\ndmqdfmfjq", "output": "16\naiibfjdgd 337\napjqdcdylyads 331\nauftuqyphmz 0\nbhigr 2360\ndmqdfmfjq 0\net 1162\nfm 2414\ni 216\njvj 0\nlhuzbitj 2959\nljbkmxj 1058\nloxxykp 2116\nmmpylhw 1919\nsbifpsvflzngfziwx 2385\nyrbikjks 2599\nzhoro 229" }, { "input": "17 6 0.44\nhefojxlinlzhynuleh 9008\nufy 7485\ngmgjrihvgxsbcu 7575\nrnlz 3789\nnkvcpt 5813\nm 9066\nsjxpwpxrkbpydkjcojvq 8679\nftvk 9385\nyygdlclq 759\nvkltswaflkg 5183\notosgwfe 639\nmaayvyqtvxkudpbcfj 7425\nhys 935\ngwucwol 6087\nbrkmjhnmmrkjzhar 1247\ntea 205\nhyxhj 6600\nmaayvyqtvxkudpbcfj\nm\nrnlz\nbrkmjhnmmrkjzhar\nhys\ngwucwol", "output": "16\nbrkmjhnmmrkjzhar 548\nftvk 4129\ngmgjrihvgxsbcu 3333\ngwucwol 2678\nhefojxlinlzhynuleh 3963\nhys 411\nhyxhj 2904\nm 3989\nmaayvyqtvxkudpbcfj 3267\nnkvcpt 2557\notosgwfe 281\nrnlz 1667\nsjxpwpxrkbpydkjcojvq 3818\nufy 3293\nvkltswaflkg 2280\nyygdlclq 333" }, { "input": "19 3 0.40\npwmgdtn 817\nikzw 8809\nyjltrwizoumwvvtivqmm 2126\ntvdguvmepsvvp 9945\ndvhoxdvqyqmyl 5998\nalpxryere 7048\nchnprj 3029\ntnsrxilkay 1076\nquamvicl 7260\nzdzahaxmxnbkuqavmb 174\nywgyrbmmhwbrcx 3637\noicdsxki 7516\nzftrgvmtbuhqsmv 6831\njlfjgvzgmkmzbsjhwhy 8042\nzuy 2049\nhsahihp 1975\nkcfsycnilwqyqvsf 6896\ntdlgs 4302\nim 4476\nkcfsycnilwqyqvsf\nim\ndvhoxdvqyqmyl", "output": "18\nalpxryere 2819\nchnprj 1211\ndvhoxdvqyqmyl 2399\nhsahihp 790\nikzw 3523\nim 1790\njlfjgvzgmkmzbsjhwhy 3216\nkcfsycnilwqyqvsf 2758\noicdsxki 3006\npwmgdtn 326\nquamvicl 2904\ntdlgs 1720\ntnsrxilkay 430\ntvdguvmepsvvp 3978\nyjltrwizoumwvvtivqmm 850\nywgyrbmmhwbrcx 1454\nzftrgvmtbuhqsmv 2732\nzuy 819" }, { "input": "20 1 0.78\nxc 6799\nztrfjsq 3023\nkhbcbsaztwigxeidh 2974\nkksvbmtjiiqlguwv 188\nwvqzzjrpmxsdbfvua 4547\niqkqqwtqifdpxfhslpv 6264\nwarmknju 9472\nfheisuiufwmtagl 292\nwge 4338\nzaklermeji 6733\nfcn 6282\nbjyjzgzkgzy 1778\ngufpvhdnsesyfuegef 4998\nxnhuhwzzxqbaphktqbc 8485\ncokabaqahfw 8645\nbtgeopbwekffdadgj 1791\nsgvrgyhidnhecvt 7264\ncczstyyxhbpwj 3244\nguaykdl 3786\nmabamfnewwrykizn 4705\nbjyjzgzkgzy", "output": "20\nbjyjzgzkgzy 1386\nbtgeopbwekffdadgj 1396\ncczstyyxhbpwj 2530\ncokabaqahfw 6743\nfcn 4899\nfheisuiufwmtagl 227\nguaykdl 2953\ngufpvhdnsesyfuegef 3898\niqkqqwtqifdpxfhslpv 4885\nkhbcbsaztwigxeidh 2319\nkksvbmtjiiqlguwv 146\nmabamfnewwrykizn 3669\nsgvrgyhidnhecvt 5665\nwarmknju 7388\nwge 3383\nwvqzzjrpmxsdbfvua 3546\nxc 5303\nxnhuhwzzxqbaphktqbc 6618\nzaklermeji 5251\nztrfjsq 2357" }, { "input": "1 1 0.94\na 8700\nb", "output": "2\na 8178\nb 0" }, { "input": "1 1 0.70\na 1000\na", "output": "1\na 700" }, { "input": "2 1 0.50\naxe 200\nmegafire 120\nmegafire", "output": "2\naxe 100\nmegafire 0" }, { "input": "5 4 0.99\naxe 350\nimpaler 300\nionize 102\nmegafire 120\nmagicboost 220\nheal\nmegafire\nshield\nmagicboost", "output": "7\naxe 346\nheal 0\nimpaler 297\nionize 100\nmagicboost 217\nmegafire 118\nshield 0" }, { "input": "1 1 0.94\na 8700\nb", "output": "2\na 8178\nb 0" }, { "input": "1 1 0.50\nlho 200\nhai", "output": "2\nhai 0\nlho 100" }, { "input": "20 3 0.29\na 100\nb 200\nc 300\nd 400\ne 500\nf 600\ng 700\nh 800\ni 900\nj 1000\nk 1100\nl 1200\nm 1300\nn 1400\no 1500\np 1600\nq 1700\nr 1800\ns 1900\nt 2000\nz\nm\nk", "output": "18\nd 116\ne 145\nf 174\ng 203\nh 232\ni 261\nj 290\nk 319\nl 348\nm 377\nn 406\no 435\np 464\nq 493\nr 522\ns 551\nt 580\nz 0" }, { "input": "2 2 0.50\nabcd 200\naaa 201\nfff\nffff", "output": "4\naaa 100\nabcd 100\nfff 0\nffff 0" }, { "input": "1 1 0.94\na 8700\nb", "output": "2\na 8178\nb 0" }, { "input": "1 1 0.29\nhren 400\nblin", "output": "2\nblin 0\nhren 116" }, { "input": "5 4 0.30\naxe 350\nimpaler 9000\nionize 80\nmegafire 120\nmagicboost 220\nheal\nmegafire\nshield\nmagicboost", "output": "6\naxe 105\nheal 0\nimpaler 2700\nmagicboost 0\nmegafire 0\nshield 0" }, { "input": "1 1 0.03\naxe 9900\nheal", "output": "2\naxe 297\nheal 0" } ]
622
14,336,000
0
24,814
23
Oranges and Apples
[ "constructive algorithms", "sortings" ]
C. Oranges and Apples
1
256
In 2*N*<=-<=1 boxes there are apples and oranges. Your task is to choose *N* boxes so, that they will contain not less than half of all the apples and not less than half of all the oranges.
The first input line contains one number *T* — amount of tests. The description of each test starts with a natural number *N* — amount of boxes. Each of the following 2*N*<=-<=1 lines contains numbers *a**i* and *o**i* — amount of apples and oranges in the *i*-th box (0<=≤<=*a**i*,<=*o**i*<=≤<=109). The sum of *N* in all the tests in the input doesn't exceed 105. All the input numbers are integer.
For each test output two lines. In the first line output YES, if it's possible to choose *N* boxes, or NO otherwise. If the answer is positive output in the second line *N* numbers — indexes of the chosen boxes. Boxes are numbered from 1 in the input order. Otherwise leave the second line empty. Separate the numbers with one space.
[ "2\n2\n10 15\n5 7\n20 18\n1\n0 0\n" ]
[ "YES\n1 3\nYES\n1\n" ]
none
[ { "input": "2\n2\n10 15\n5 7\n20 18\n1\n0 0", "output": "YES\n3 1\nYES\n1" } ]
1,294
24,371,200
3.307605
24,842
254
Anagram
[ "greedy", "strings" ]
null
null
String *x* is an anagram of string *y*, if we can rearrange the letters in string *x* and get exact string *y*. For example, strings "DOG" and "GOD" are anagrams, so are strings "BABA" and "AABB", but strings "ABBAC" and "CAABA" are not. You are given two strings *s* and *t* of the same length, consisting of uppercase English letters. You need to get the anagram of string *t* from string *s*. You are permitted to perform the replacing operation: every operation is replacing some character from the string *s* by any other character. Get the anagram of string *t* in the least number of replacing operations. If you can get multiple anagrams of string *t* in the least number of operations, get the lexicographically minimal one. The lexicographic order of strings is the familiar to us "dictionary" order. Formally, the string *p* of length *n* is lexicographically smaller than string *q* of the same length, if *p*1<==<=*q*1, *p*2<==<=*q*2, ..., *p**k*<=-<=1<==<=*q**k*<=-<=1, *p**k*<=&lt;<=*q**k* for some *k* (1<=≤<=*k*<=≤<=*n*). Here characters in the strings are numbered from 1. The characters of the strings are compared in the alphabetic order.
The input consists of two lines. The first line contains string *s*, the second line contains string *t*. The strings have the same length (from 1 to 105 characters) and consist of uppercase English letters.
In the first line print *z* — the minimum number of replacement operations, needed to get an anagram of string *t* from string *s*. In the second line print the lexicographically minimum anagram that could be obtained in *z* operations.
[ "ABA\nCBA\n", "CDBABC\nADCABD\n" ]
[ "1\nABC\n", "2\nADBADC\n" ]
The second sample has eight anagrams of string *t*, that can be obtained from string *s* by replacing exactly two letters: "ADBADC", "ADDABC", "CDAABD", "CDBAAD", "CDBADA", "CDDABA", "DDAABC", "DDBAAC". These anagrams are listed in the lexicographical order. The lexicographically minimum anagram is "ADBADC".
[ { "input": "ABA\nCBA", "output": "1\nABC" }, { "input": "CDBABC\nADCABD", "output": "2\nADBADC" }, { "input": "AABAA\nBBAAA", "output": "1\nAABAB" }, { "input": "OVGHK\nRPGUC", "output": "4\nCPGRU" }, { "input": "CCAACBA\nBBBAACC", "output": "2\nBCAACBB" }, { "input": "ECBECDB\nAADEEBD", "output": "3\nEAAEDDB" }, { "input": "IEDABCCJBH\nAJAJAAGHFG", "output": "7\nAAAAFGGJJH" }, { "input": "CFCJKBDAGL\nDEJEBNOFJF", "output": "6\nEFEJFBDJNO" }, { "input": "QAPGMPIHMM\nTSLALOBLFJ", "output": "9\nBAFJLLLOST" }, { "input": "PJPOJOVMAK\nFVACRHLDAP", "output": "7\nACPDFHVLAR" }, { "input": "CBCBACBCCCABABBACCCAACBABBCBABBCBACCAAACCBCBABCBCA\nBBCCABABABACAAAABBCACACBACAAACACABBAACCCACBBCCCCAA", "output": "7\nAAAAACACCCAAAABACCCAACBABBCBABBCBACCAAACCBCBABCBCA" }, { "input": "DBDABEEDDAEDBEABDBEBBCDCAAAEEBBCDBDEBDACAAADADDADA\nABACBEBBBCDDDCCDABCAEDABBAECBBAEDEBEDAACEBADEEAEEE", "output": "8\nBBCABEECCAEDBEABDBEBBCDCAAAEEBBCDBDEBDACAAADADEEEE" }, { "input": "CFIJLJMFECKJFKNNHEEABGMEHBGMCDJGKKCFCGGCJBKFACFADC\nHMMEBBNACGCCGGGLAEJANDIIIDEHCGKJEINMABMFFNAALJDIIE", "output": "14\nAAIALDMEECIJFINNHEEABGMEHBGMCDJGIICFCGGIJBKLAMNADN" }, { "input": "MBENGJMOLOEQFIKREJAONCPFLDANIGNHQQMDFSPEFCODSOTBJT\nPKTRMAMCKDQOJOHIPKQPCCTNMLOIJNBLLMQFJTIBHMGGTOCATB", "output": "14\nMBBCGJMCLOHQFIKRIJAOKCPKLDANIGNHQQMLMMPPTCOTTOTBJT" }, { "input": "BHGBGIGACEAFBIFGEGFFGDAIFJAGDHJFGCJIBDBJEHAFAIHCJJGHGGGEEGGJEEDAFBDAHBEEBEFCGJHCECAEGCDGFHGEDJAFAEEC\nCDHJHFDCGGFBCACJAGICEEBFBIAGDBDAFAGJBECGFDFBHHHFFJCGDCDAGICFCFADICHAJADJGGIECHJBAJHGJHECABAJFAECCEGB", "output": "14\nBHABAIBACCAFBIFCCCFFCDAIFJAGDHJFGCJIBDBJCHAFAIHCJJGHGGGCDGGJDEDAFBDAHBEEBEFCGJHCECAEGCDGFHGEDJAFAHJC" }, { "input": "RHHQKEAOTDQIBNQDONDEBEEDOANEEPICTLHFJQABHKEADSJKMONDDEFGCJHGNHRJGJLLODBRHADEOSLIISDHPKBGDQMQTGOAPSKJ\nPIKHKDIQDHDJIJFCCOAFAOLRFNMRAGRDJGJLCPNDJHJGSMOADHPJFDCPKJGCDPATAKDPJGGLRSCQSCDSBLKCRTBBNDTMMKPTSNGB", "output": "24\nRCCCKCACTDCFBFGDGNDJBJJDKANMMPICTLHFJPABHKPADSJKMONDDPFGCJHGNHRJGJLLODBRPADROSLIISDRPKSGDQMQTGTAPSKJ" }, { "input": "UDYLXKUDNLITSXWUSVJLDSLUSGEVBIOKUYYVGUSYTSNAZLNQGVKZGRDSMYNKPKNWKXUPAHCGZRASFRBOMSCEFRXEMEWRVCVKURPE\nVRTYGIECZMCWZOKHSVVFXFZLAULBXVQWINDJAEUZPTQOOLZZHHAEJAPSWCLBBAYOWMIWECDUCRLRSTWJHRZDHTNQUNXEDPWGWFOZ", "output": "30\nADALBCCDFLITHXWHHHJLDILJJGEOBIOKUOOVGUQYTQNAZLNQTVTZWRDSMYNWPWWWWXUPAHCZZRASFRBOMSCEFRXEZEWZVCVZUZPE" }, { "input": "A\nA", "output": "0\nA" }, { "input": "CA\nAB", "output": "1\nBA" }, { "input": "QZW\nQZB", "output": "1\nQZB" }, { "input": "ABQQ\nBCZW", "output": "3\nCBWZ" }, { "input": "QWBCZ\nABCZW", "output": "1\nAWBCZ" }, { "input": "OPGHJJ\nPFQJZO", "output": "3\nOPFQJZ" }, { "input": "AB\nBC", "output": "1\nCB" }, { "input": "AABBD\nBBAAC", "output": "1\nAABBC" }, { "input": "WRUKY\nRUKWX", "output": "1\nWRUKX" }, { "input": "ABEBCBA\nBBBAACD", "output": "1\nABDBCBA" }, { "input": "EGBAAED\nAADEEBF", "output": "1\nEFBAAED" }, { "input": "AHFLGJAAAJ\nAJAJAAGHFK", "output": "1\nAHFKGJAAAJ" }, { "input": "JEJFDBOQEN\nDEJEBNOFJP", "output": "1\nJEJFDBOPEN" }, { "input": "VTFLLLOSBA\nTSLALOBLFU", "output": "1\nUTFLLLOSBA" } ]
60
0
-1
24,864
899
Letters Removing
[ "data structures", "strings" ]
null
null
Petya has a string of length *n* consisting of small and large English letters and digits. He performs *m* operations. Each operation is described with two integers *l* and *r* and a character *c*: Petya removes from the string all characters *c* on positions between *l* and *r*, inclusive. It's obvious that the length of the string remains the same or decreases after each operation. Find how the string will look like after Petya performs all *m* operations.
The first string contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=2·105) — the length of the string and the number of operations. The second line contains the string of length *n*, consisting of small and large English letters and digits. Positions in the string are enumerated from 1. Each of the next *m* lines contains two integers *l* and *r* (1<=≤<=*l*<=≤<=*r*), followed by a character *c*, which is a small or large English letter or a digit. This line describes one operation. It is guaranteed that *r* doesn't exceed the length of the string *s* before current operation.
Print the string Petya will obtain after performing all *m* operations. If the strings becomes empty after all operations, print an empty line.
[ "4 2\nabac\n1 3 a\n2 2 c\n", "3 2\nA0z\n1 3 0\n1 1 z\n", "10 4\nagtFrgF4aF\n2 5 g\n4 9 F\n1 5 4\n1 7 a\n", "9 5\naAAaBBccD\n1 4 a\n5 6 c\n2 3 B\n4 4 D\n2 3 A\n" ]
[ "b\n", "Az\n", "tFrg4\n", "AB\n" ]
In the first example during the first operation both letters 'a' are removed, so the string becomes "bc". During the second operation the letter 'c' (on the second position) is removed, and the string becomes "b". In the second example during the first operation Petya removes '0' from the second position. After that the string becomes "Az". During the second operations the string doesn't change.
[ { "input": "4 2\nabac\n1 3 a\n2 2 c", "output": "b" }, { "input": "3 2\nA0z\n1 3 0\n1 1 z", "output": "Az" }, { "input": "10 4\nagtFrgF4aF\n2 5 g\n4 9 F\n1 5 4\n1 7 a", "output": "tFrg4" }, { "input": "9 5\naAAaBBccD\n1 4 a\n5 6 c\n2 3 B\n4 4 D\n2 3 A", "output": "AB" }, { "input": "10 10\nhRZQK137KY\n1 10 V\n8 10 g\n2 6 E\n2 2 y\n6 8 J\n1 5 w\n4 6 q\n6 8 K\n2 10 c\n4 6 t", "output": "hRZQK137KY" }, { "input": "20 4\n8QYQY1tp6CA02NGnR04s\n1 9 N\n1 10 y\n5 11 t\n6 7 C", "output": "8QYQY1p6CA02NGnR04s" }, { "input": "30 5\nkR87ctl81OsbekSKwUATCwSPKssjio\n16 25 0\n16 19 W\n6 12 K\n4 23 k\n28 29 z", "output": "kR87ctl81OsbeSKwUATCwSPKssjio" }, { "input": "40 24\nYYUcKDEUVLUCU8EKiUVxnD8TZSSidW8TUDS8Sccb\n3 38 K\n24 33 e\n6 36 d\n5 10 d\n13 14 E\n12 23 S\n4 30 U\n4 11 C\n12 15 Y\n5 27 x\n25 28 W\n25 26 E\n15 26 C\n17 22 d\n6 25 n\n19 22 K\n2 26 T\n1 22 W\n7 19 d\n12 18 K\n4 12 L\n9 18 8\n4 8 i\n6 8 L", "output": "YYUcDEV8iVDZSiDS8Sccb" }, { "input": "50 6\nRQQQQQQRRRRQQQQQRRQQRQRRRRRQRQQQRQQQRRRQQQQQRRQRRR\n18 26 R\n13 33 R\n23 32 R\n7 21 Q\n18 21 R\n14 20 R", "output": "RQQQQQRRRRQQQQQQQQQQQRRR" }, { "input": "1 1\na\n1 1 A", "output": "a" } ]
31
5,529,600
0
24,915
340
Bubble Sort Graph
[ "binary search", "data structures", "dp" ]
null
null
Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with *n* elements *a*1, *a*2, ..., *a**n* in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it *G*) initially has *n* vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode). For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph *G*, if we use such permutation as the premutation *a* in procedure bubbleSortGraph.
The first line of the input contains an integer *n* (2<=≤<=*n*<=≤<=105). The next line contains *n* distinct integers *a*1, *a*2, ..., *a**n* (1<=≤<=*a**i*<=≤<=*n*).
Output a single integer — the answer to the problem.
[ "3\n3 1 2\n" ]
[ "2\n" ]
Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2].
[ { "input": "3\n3 1 2", "output": "2" }, { "input": "5\n4 2 1 3 5", "output": "3" }, { "input": "10\n1 9 8 10 2 3 4 6 5 7", "output": "6" }, { "input": "50\n12 24 42 43 36 3 40 29 7 34 10 13 28 9 35 23 25 21 19 4 20 18 11 38 41 48 6 46 33 17 31 37 2 30 32 44 45 5 47 49 16 15 50 27 26 14 39 22 1 8", "output": "13" }, { "input": "100\n36 48 92 87 28 85 42 10 44 41 39 3 79 9 14 56 1 16 46 35 93 8 82 26 100 59 60 2 96 52 13 98 70 81 71 94 54 91 17 88 33 30 19 50 18 73 65 29 78 21 61 7 99 97 45 89 57 27 76 11 49 72 84 69 43 62 4 22 75 6 66 83 38 34 86 15 40 51 37 74 67 31 20 63 77 80 12 53 5 25 58 90 68 24 64 23 95 32 55 47", "output": "16" } ]
1,000
7,065,600
0
24,916
38
Let's Go Rolling!
[ "dp", "sortings" ]
E. Let's Go Rolling!
2
256
On a number axis directed from the left rightwards, *n* marbles with coordinates *x*1,<=*x*2,<=...,<=*x**n* are situated. Let's assume that the sizes of the marbles are infinitely small, that is in this task each of them is assumed to be a material point. You can stick pins in some of them and the cost of sticking in the marble number *i* is equal to *c**i*, number *c**i* may be negative. After you choose and stick the pins you need, the marbles will start to roll left according to the rule: if a marble has a pin stuck in it, then the marble doesn't move, otherwise the marble rolls all the way up to the next marble which has a pin stuck in it and stops moving there. If there is no pinned marble on the left to the given unpinned one, it is concluded that the marble rolls to the left to infinity and you will pay an infinitely large fine for it. If no marble rolled infinitely to the left, then the fine will consist of two summands: - the sum of the costs of stuck pins; - the sum of the lengths of the paths of each of the marbles, that is the sum of absolute values of differences between their initial and final positions. Your task is to choose and pin some marbles in the way that will make the fine for you to pay as little as possible.
The first input line contains an integer *n* (1<=≤<=*n*<=≤<=3000) which is the number of marbles. The next *n* lines contain the descriptions of the marbles in pairs of integers *x**i*, *c**i* (<=-<=109<=≤<=*x**i*,<=*c**i*<=≤<=109). The numbers are space-separated. Each description is given on a separate line. No two marbles have identical initial positions.
Output the single number — the least fine you will have to pay.
[ "3\n2 3\n3 4\n1 2\n", "4\n1 7\n3 1\n5 10\n6 1\n" ]
[ "5\n", "11\n" ]
none
[ { "input": "3\n2 3\n3 4\n1 2", "output": "5" }, { "input": "4\n1 7\n3 1\n5 10\n6 1", "output": "11" }, { "input": "1\n-948943148 10", "output": "10" }, { "input": "5\n864449017 54\n864449026 504\n864449027 308\n864449028 284\n864449030 249", "output": "97" }, { "input": "10\n439333887 2\n439333930 1\n439333947 2\n439333949 3\n439333950 1\n439333952 3\n439333953 1\n439333954 2\n439333955 2\n439333956 1", "output": "15" }, { "input": "15\n-793387817 -4\n-793387816 4\n-793387815 8\n-793387813 -5\n-793387812 3\n-793387811 8\n-793387810 2\n-793387809 3\n-793387808 8\n-793387807 5\n-793387806 -1\n-793387805 -2\n-793387804 -1\n-793387803 3\n-793387802 -3", "output": "-1" }, { "input": "1\n-358012391 0", "output": "0" }, { "input": "1\n-59873019 0", "output": "0" } ]
186
307,200
0
24,962
380
Sereja and Prefixes
[ "binary search", "brute force" ]
null
null
Sereja loves number sequences very much. That's why he decided to make himself a new one following a certain algorithm. Sereja takes a blank piece of paper. Then he starts writing out the sequence in *m* stages. Each time he either adds a new number to the end of the sequence or takes *l* first elements of the current sequence and adds them *c* times to the end. More formally, if we represent the current sequence as *a*1,<=*a*2,<=...,<=*a**n*, then after we apply the described operation, the sequence transforms into *a*1,<=*a*2,<=...,<=*a**n*[,<=*a*1,<=*a*2,<=...,<=*a**l*] (the block in the square brackets must be repeated *c* times). A day has passed and Sereja has completed the sequence. He wonders what are the values of some of its elements. Help Sereja.
The first line contains integer *m* (1<=≤<=*m*<=≤<=105) — the number of stages to build a sequence. Next *m* lines contain the description of the stages in the order they follow. The first number in the line is a type of stage (1 or 2). Type 1 means adding one number to the end of the sequence, in this case the line contains integer *x**i* (1<=≤<=*x**i*<=≤<=105) — the number to add. Type 2 means copying a prefix of length *l**i* to the end *c**i* times, in this case the line further contains two integers *l**i*,<=*c**i* (1<=≤<=*l**i*<=≤<=105,<=1<=≤<=*c**i*<=≤<=104), *l**i* is the length of the prefix, *c**i* is the number of copyings. It is guaranteed that the length of prefix *l**i* is never larger than the current length of the sequence. The next line contains integer *n* (1<=≤<=*n*<=≤<=105) — the number of elements Sereja is interested in. The next line contains the numbers of elements of the final sequence Sereja is interested in. The numbers are given in the strictly increasing order. It is guaranteed that all numbers are strictly larger than zero and do not exceed the length of the resulting sequence. Consider the elements of the final sequence numbered starting from 1 from the beginning to the end of the sequence. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Print the elements that Sereja is interested in, in the order in which their numbers occur in the input.
[ "6\n1 1\n1 2\n2 2 1\n1 3\n2 5 2\n1 4\n16\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16\n" ]
[ "1 2 1 2 3 1 2 1 2 3 1 2 1 2 3 4\n" ]
none
[ { "input": "6\n1 1\n1 2\n2 2 1\n1 3\n2 5 2\n1 4\n16\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16", "output": "1 2 1 2 3 1 2 1 2 3 1 2 1 2 3 4" }, { "input": "2\n1 33085\n1 44638\n2\n1 2", "output": "33085 44638" }, { "input": "10\n1 57757\n1 45234\n1 80807\n1 38496\n1 27469\n1 42645\n1 72643\n1 33235\n1 10843\n1 80598\n10\n1 2 3 4 5 6 7 8 9 10", "output": "57757 45234 80807 38496 27469 42645 72643 33235 10843 80598" }, { "input": "3\n1 97601\n1 32580\n1 70519\n3\n1 2 3", "output": "97601 32580 70519" }, { "input": "7\n1 53989\n1 47249\n1 71935\n2 1 3\n1 84520\n1 84185\n2 6 1\n14\n1 2 3 4 5 6 7 8 9 10 11 12 13 14", "output": "53989 47249 71935 53989 53989 53989 84520 84185 53989 47249 71935 53989 53989 53989" }, { "input": "1\n1 1\n1\n1", "output": "1" } ]
982
50,380,800
3
24,966
908
New Year and Original Order
[ "dp", "math" ]
null
null
Let *S*(*n*) denote the number that represents the digits of *n* in sorted order. For example, *S*(1)<==<=1,<=*S*(5)<==<=5,<=*S*(50394)<==<=3459,<=*S*(353535)<==<=333555. Given a number *X*, compute modulo 109<=+<=7.
The first line of input will contain the integer *X* (1<=≤<=*X*<=≤<=10700).
Print a single integer, the answer to the question.
[ "21\n", "345342\n" ]
[ "195\n", "390548434\n" ]
The first few values of *S* are 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 12. The sum of these values is 195.
[ { "input": "21", "output": "195" }, { "input": "345342", "output": "390548434" }, { "input": "9438174368", "output": "419438859" }, { "input": "33340691714056185860211260984431382156326935244157", "output": "683387308" }, { "input": "60659389952427965488066632743799525603106037644498358605868947137979908494800892265261453803791510334840960342863677552781925982028425181448855359993703120262947850907075964314040305228976226486729250", "output": "654963480" }, { "input": "44649014054971081213608137817466046254652492627741860478258558206397113198232823859870363821007188476405951611069347299689170240023979048198711745011542774268179055311013054073075176122755643483380248999657649211459997766221072399103579977409770898200358240970169892326442892826731631357561876251276209119521202062222947560634301788787748428236988789594458520867663257476744168528121470923031438015546006185059454402637036376247785881323277542968298682307854655591317046086531554595892680980142608410", "output": "382433601" }, { "input": "87180", "output": "273914491" }, { "input": "404075833601771942667259010375375556744823902383758960785823552761999143572376325949809260679742124753881851158698439457386070260861271136645763680151691355801985707548363664714643023957647369701126324673656050885747545042127147214166479318245077239696802674619402305982303576335159698484641718860881491887951521487208762", "output": "770789762" } ]
997
0
0
24,983
30
King's Problem?
[ "geometry", "greedy" ]
D. King's Problem?
3
256
Every true king during his life must conquer the world, hold the Codeforces world finals, win pink panda in the shooting gallery and travel all over his kingdom. King Copa has already done the first three things. Now he just needs to travel all over the kingdom. The kingdom is an infinite plane with Cartesian coordinate system on it. Every city is a point on this plane. There are *n* cities in the kingdom at points with coordinates (*x*1,<=0),<=(*x*2,<=0),<=...,<=(*x**n*,<=0), and there is one city at point (*x**n*<=+<=1,<=*y**n*<=+<=1). King starts his journey in the city number *k*. Your task is to find such route for the king, which visits all cities (in any order) and has minimum possible length. It is allowed to visit a city twice. The king can end his journey in any city. Between any pair of cities there is a direct road with length equal to the distance between the corresponding points. No two cities may be located at the same point.
The first line contains two integers *n* and *k* (1<=≤<=*n*<=≤<=105,<=1<=≤<=*k*<=≤<=*n*<=+<=1) — amount of cities and index of the starting city. The second line contains *n*<=+<=1 numbers *x**i*. The third line contains *y**n*<=+<=1. All coordinates are integers and do not exceed 106 by absolute value. No two cities coincide.
Output the minimum possible length of the journey. Your answer must have relative or absolute error less than 10<=-<=6.
[ "3 1\n0 1 2 1\n1\n", "3 1\n1 0 2 1\n1\n", "4 5\n0 5 -1 -5 2\n3\n" ]
[ "3.41421356237309490000", "3.82842712474619030000", "14.24264068711928400000" ]
none
[ { "input": "3 1\n0 1 2 1\n1", "output": "3.41421356237309490000" }, { "input": "3 1\n1 0 2 1\n1", "output": "3.82842712474619030000" }, { "input": "4 5\n0 5 -1 -5 2\n3", "output": "14.24264068711928400000" }, { "input": "4 1\n0 5 -1 -5 2\n3", "output": "16.85841379298319300000" }, { "input": "10 5\n-231 598 312 -837 -711 553 -439 351 83 -605 -738\n791", "output": "2928.27473344969580000000" }, { "input": "100 31\n-577 101 691 -95 809 125 -934 -645 -838 -548 259 312 -501 -962 -629 -637 -797 -259 -258 -687 537 303 -6 431 64 229 -897 -942 -805 804 -608 84 992 -563 279 -546 -523 -28 4 -296 120 -306 456 -334 625 821 -9 525 572 -36 -642 594 207 429 -859 -408 -428 280 389 304 -683 800 97 578 -1 46 981 -271 585 -913 761 367 0 -360 529 -302 598 -660 295 -785 756 759 -446 -826 -627 687 -459 637 -509 -582 825 34 -911 -383 49 -343 791 471 -574 -267 -954\n-462", "output": "2983.22016716350340000000" }, { "input": "4 2\n0 2 3 9 2\n5", "output": "17.98749007417713100000" }, { "input": "1 1\n8 4\n5", "output": "6.40312423743284850000" }, { "input": "1 2\n104 13\n455", "output": "464.01077573694340000000" }, { "input": "2 2\n-80 -28 -41\n-61", "output": "124.40165743959182000000" }, { "input": "3 1\n6 -7 40 -39\n28", "output": "123.52058325093860000000" }, { "input": "4 4\n-58 14 70 34 -12\n84", "output": "259.77055915050300000000" }, { "input": "5 1\n78 35 -100 57 99 -92\n58", "output": "278.54912467321776000000" }, { "input": "6 6\n14 6 -19 -71 59 -17 24\n-76", "output": "267.67197858303581000000" }, { "input": "7 6\n-51 27 12 -48 -31 7 -10 -10\n-35", "output": "146.93132631298738000000" }, { "input": "8 9\n36 48 42 -25 80 32 -44 6 -80\n-50", "output": "185.61168720299744000000" }, { "input": "9 6\n-28 69 -78 48 41 57 73 75 -38 30\n-94", "output": "307.09503873251936000000" }, { "input": "10 1\n-42 -13 68 -73 -39 81 -9 -47 -74 -84 -2\n-70", "output": "299.34407871396292000000" }, { "input": "11 6\n41 -64 51 67 -8 -99 -94 -81 -30 -60 37 -66\n26", "output": "205.09347166041786000000" }, { "input": "12 3\n74 -65 84 -44 23 -78 22 86 65 -86 76 -61 66\n-87", "output": "316.05212760071760000000" }, { "input": "13 4\n-43 -65 67 95 -97 -56 -63 53 -42 -62 -86 -7 -45 12\n44", "output": "240.06852768030663000000" }, { "input": "14 12\n-10 85 50 34 -67 -35 53 -31 -89 -47 -3 -5 -90 10 -100\n50", "output": "315.99019513592782000000" }, { "input": "15 7\n23 84 83 -28 -36 36 -82 -64 -54 -65 -8 51 35 9 -74 -19\n37", "output": "222.67938366055708000000" }, { "input": "16 6\n-94 83 66 62 45 58 34 -98 40 -91 81 56 -76 -93 43 75 -49\n57", "output": "259.75902391002091000000" }, { "input": "17 17\n-61 32 99 1 76 79 -51 20 -66 83 -80 60 -36 6 -41 93 -89 65\n35", "output": "236.79549159502341000000" }, { "input": "18 11\n22 31 82 -61 -94 -100 65 -14 28 -41 -87 54 -96 -75 -27 100 -73 63 2\n-15", "output": "223.93171219946132000000" }, { "input": "19 12\n55 30 65 79 -63 -79 -20 -48 73 81 -2 -83 94 3 42 54 -64 40 91 24\n48", "output": "250.76629407951023000000" }, { "input": "1 2\n-5 -4\n-2", "output": "2.23606797749978980000" }, { "input": "2 1\n-2 -1 -1\n-4", "output": "5.00000000000000000000" }, { "input": "3 1\n-1 1 4 0\n-5", "output": "11.40312423743284900000" }, { "input": "4 3\n3 0 -3 1 -3\n-3", "output": "10.24264068711928400000" }, { "input": "5 6\n-5 2 -1 5 0 0\n2", "output": "15.38516480713450500000" }, { "input": "6 3\n-1 3 1 -4 5 -3 -4\n5", "output": "18.00000000000000000000" }, { "input": "7 7\n0 4 3 -5 -3 -1 5 3\n4", "output": "16.00000000000000000000" }, { "input": "8 5\n3 -4 2 -1 4 0 5 1 3\n4", "output": "16.47213595499958000000" }, { "input": "9 2\n-4 -2 4 3 -5 5 0 -3 -1 1\n-3", "output": "16.76782893563237000000" }, { "input": "10 6\n4 -3 0 -2 2 5 1 -5 -4 -1 2\n5", "output": "18.38516480713450500000" }, { "input": "1 2\n-6 -5\n-13", "output": "13.03840481040529800000" }, { "input": "2 1\n-3 20 2\n8", "output": "29.13169673564881300000" }, { "input": "3 4\n0 5 18 -13\n-4", "output": "31.60147050873544600000" }, { "input": "4 5\n3 -10 -8 5 -15\n5", "output": "22.07106781186547600000" }, { "input": "5 4\n7 16 9 -16 13 -5\n4", "output": "33.35381055139314600000" }, { "input": "6 6\n10 0 -16 2 5 -20 -16\n18", "output": "56.08318915758459200000" }, { "input": "7 1\n13 -15 -1 -19 -5 -14 14 -6\n5", "output": "39.53300064564938800000" }, { "input": "8 2\n16 11 1 -14 -6 5 8 -15 16\n-15", "output": "60.00000000000000000000" }, { "input": "9 9\n19 -4 -9 17 2 -6 -19 -3 -10 20\n-19", "output": "66.02629759044045000000" }, { "input": "10 10\n-7 -4 -5 17 20 -6 -1 10 -8 13 3\n19", "output": "56.95449840010015000000" }, { "input": "3 1\n0 1 2 1\n1", "output": "3.41421356237309490000" }, { "input": "3 1\n1 0 2 1\n1", "output": "3.82842712474619030000" }, { "input": "4 5\n0 5 -1 -5 2\n3", "output": "14.24264068711928400000" }, { "input": "4 1\n0 5 -1 -5 2\n3", "output": "16.85841379298319300000" }, { "input": "5 1\n0 1 3 100 -1000 2\n1", "output": "1200.00560090988460000000" }, { "input": "5 1\n0 -1 -3 -100 1000 -2\n1", "output": "1200.00560090988460000000" }, { "input": "1 2\n-1 9\n0", "output": "10.00000000000000000000" }, { "input": "2 2\n4 -2 -8\n0", "output": "18.00000000000000000000" }, { "input": "3 2\n7 -2 3 -8\n0", "output": "21.00000000000000000000" }, { "input": "4 5\n-9 -2 0 4 -4\n0", "output": "18.00000000000000000000" }, { "input": "5 5\n-4 -2 0 6 -6 -8\n0", "output": "16.00000000000000000000" }, { "input": "6 6\n10 0 -16 2 5 -20 18\n0", "output": "38.00000000000000000000" }, { "input": "7 1\n13 -15 -1 -19 -5 -14 14 5\n0", "output": "34.00000000000000000000" }, { "input": "8 2\n16 11 1 -14 -6 5 8 -15 -19\n0", "output": "40.00000000000000000000" }, { "input": "9 9\n19 -4 -9 17 2 -6 -19 -3 -10 3\n0", "output": "47.00000000000000000000" }, { "input": "10 10\n-7 -4 -5 17 20 -6 -1 10 -8 13 19\n0", "output": "35.00000000000000000000" }, { "input": "11 7\n-2 20 -16 19 -5 3 13 17 -15 16 -10 -19\n0", "output": "46.00000000000000000000" }, { "input": "12 8\n0 15 -20 17 -8 11 1 20 -2 -13 8 -3 4\n0", "output": "40.00000000000000000000" }, { "input": "15 16\n9 -15 -16 12 -17 -4 -19 -9 -11 -14 -13 4 5 8 -18 1\n0", "output": "42.00000000000000000000" }, { "input": "15 7\n11 -30 -5 -3 19 -11 -27 3 22 -4 10 -16 -13 -8 -26 25\n0", "output": "58.00000000000000000000" }, { "input": "20 1\n-7 -28 -2 27 -12 6 -1 10 -11 -8 29 16 -14 -18 23 -10 13 -29 -25 30 7\n0", "output": "81.00000000000000000000" }, { "input": "20 8\n-36 -18 13 11 5 18 23 -2 -39 2 4 -34 -20 24 7 19 -13 14 6 -35 -4\n0", "output": "89.00000000000000000000" }, { "input": "20 8\n62 -52 -96 -68 -13 59 65 21 -47 90 -21 58 -71 39 -35 -98 -19 -90 -57 -62 -48\n0", "output": "257.00000000000000000000" }, { "input": "100 31\n-577 101 691 -95 809 125 -934 -645 -838 -548 259 312 -501 -962 -629 -637 -797 -259 -258 -687 537 303 -6 431 64 229 -897 -942 -805 804 -608 84 992 -563 279 -546 -523 -28 4 -296 120 -306 456 -334 625 821 -9 525 572 -36 -642 594 207 429 -859 -408 -428 280 389 304 -683 800 97 578 -1 46 981 -271 585 -913 761 367 0 -360 529 -302 598 -660 295 -785 756 759 -446 -826 -627 687 -459 637 -509 -582 825 34 -911 -383 49 -343 791 471 -574 -267 -462\n0", "output": "2308.00000000000000000000" }, { "input": "5 2\n0 2 3 8 9 2\n5", "output": "18.19541448304115900000" }, { "input": "4 2\n0 2 3 9 2\n5", "output": "17.98749007417713100000" }, { "input": "4 3\n0 6 7 9 7\n5", "output": "17.98749007417713100000" }, { "input": "1 2\n-1 -3\n9", "output": "9.21954445729288710000" }, { "input": "2 2\n4 -2 5\n-8", "output": "14.06225774829854900000" }, { "input": "3 2\n7 -2 3 8\n-8", "output": "17.06225774829854900000" }, { "input": "4 5\n-9 -2 0 4 10\n-4", "output": "20.21110255092797900000" }, { "input": "5 5\n-4 -2 0 6 -6 5\n-8", "output": "20.06225774829854900000" }, { "input": "6 6\n1 -1 -6 -2 5 -9 -1\n-1", "output": "15.23606797749979000000" }, { "input": "100 9\n52 24 17 12 57 95 94 36 -3 63 -69 71 23 -72 77 -55 2 -5 -25 -18 -39 -100 6 -43 18 74 -61 54 47 5 -68 9 -41 -42 34 31 -4 -16 67 -65 -56 -50 -84 27 -87 62 70 -14 10 90 -46 97 43 -88 99 75 -34 -36 92 -17 29 -52 -80 -92 44 -44 -32 -89 -82 -74 98 38 -10 93 -24 96 -28 -98 76 -83 -29 -27 35 53 37 -67 64 -21 72 -20 -54 49 28 26 83 46 59 -2 -12 32 -44\n37", "output": "321.09250465482546000000" } ]
278
9,932,800
0
25,048
570
Pig and Palindromes
[ "combinatorics", "dp" ]
null
null
Peppa the Pig was walking and walked into the forest. What a strange coincidence! The forest has the shape of a rectangle, consisting of *n* rows and *m* columns. We enumerate the rows of the rectangle from top to bottom with numbers from 1 to *n*, and the columns — from left to right with numbers from 1 to *m*. Let's denote the cell at the intersection of the *r*-th row and the *c*-th column as (*r*,<=*c*). Initially the pig stands in cell (1,<=1), and in the end she wants to be in cell (*n*,<=*m*). Since the pig is in a hurry to get home, she can go from cell (*r*,<=*c*), only to either cell (*r*<=+<=1,<=*c*) or (*r*,<=*c*<=+<=1). She cannot leave the forest. The forest, where the pig is, is very unusual. Some cells of the forest similar to each other, and some look very different. Peppa enjoys taking pictures and at every step she takes a picture of the cell where she is now. The path through the forest is considered to be beautiful if photographs taken on her way, can be viewed in both forward and in reverse order, showing the same sequence of photos. More formally, the line formed by the cells in order of visiting should be a palindrome (you can read a formal definition of a palindrome in the previous problem). Count the number of beautiful paths from cell (1,<=1) to cell (*n*,<=*m*). Since this number can be very large, determine the remainder after dividing it by 109<=+<=7.
The first line contains two integers *n*,<=*m* (1<=≤<=*n*,<=*m*<=≤<=500) — the height and width of the field. Each of the following *n* lines contains *m* lowercase English letters identifying the types of cells of the forest. Identical cells are represented by identical letters, different cells are represented by different letters.
Print a single integer — the number of beautiful paths modulo 109<=+<=7.
[ "3 4\naaab\nbaaa\nabba\n" ]
[ "3" ]
Picture illustrating possibilities for the sample test. <img class="tex-graphics" src="https://espresso.codeforces.com/bf73568d1cf80d89f66d4e472a91ae0339af83a2.png" style="max-width: 100.0%;max-height: 100.0%;"/> <img class="tex-graphics" src="https://espresso.codeforces.com/1e870880d976e642be85498efab6bbd10bae8a84.png" style="max-width: 100.0%;max-height: 100.0%;"/>
[ { "input": "3 4\naaab\nbaaa\nabba", "output": "3" }, { "input": "2 2\nab\naa", "output": "2" }, { "input": "3 5\nqqqrw\nwqqtw\newqqq", "output": "3" }, { "input": "1 5\nabbba", "output": "1" }, { "input": "1 5\nabbbb", "output": "0" }, { "input": "1 4\nabca", "output": "0" }, { "input": "5 1\na\na\na\na\na", "output": "1" }, { "input": "5 2\nab\nab\ncc\nba\nba", "output": "1" }, { "input": "5 3\naba\naba\nccc\nbaa\nbaa", "output": "1" }, { "input": "5 5\naaaaa\naaaaa\naaaaa\naaaaa\naaaaa", "output": "70" }, { "input": "5 5\naaaaa\nadaaa\naaaaa\naaaaa\naacaa", "output": "23" }, { "input": "5 5\naaaqa\naaaaa\naaaaa\naaaaa\naaaaa", "output": "65" }, { "input": "5 6\naaaaaa\naafaaa\naaaafa\naafaaa\naaaaaa", "output": "47" }, { "input": "10 10\naaaaaaaaaa\naaaaaaaaaa\naaaaaaaaaa\naaaaaaaaaa\naaaaaaaaaa\naaaaaaaaaa\naaaaaaaaaa\naaaaaaaaaa\naaaaaaaaaa\naaaaaaaaaa", "output": "48620" }, { "input": "10 9\naaaaaaaaa\naaaaaaaaa\naaaaaaaaa\naaaaaaaaa\naaaaaaaaa\naaaaaaaaa\naaaaaaaaa\naaaaaaaaa\naaaaaaaaa\naaaaaaaaa", "output": "24310" }, { "input": "1 500\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "output": "1" }, { "input": "1 499\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "output": "1" }, { "input": "1 500\nzaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "output": "0" }, { "input": "1 499\nzaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "output": "0" } ]
2,573
9,625,600
3
25,247
685
Travelling Through the Snow Queen's Kingdom
[ "bitmasks", "brute force", "divide and conquer", "graphs" ]
null
null
Gerda is travelling to the palace of the Snow Queen. The road network consists of *n* intersections and *m* bidirectional roads. Roads are numbered from 1 to *m*. Snow Queen put a powerful spell on the roads to change the weather conditions there. Now, if Gerda steps on the road *i* at the moment of time less or equal to *i*, she will leave the road exactly at the moment *i*. In case she steps on the road *i* at the moment of time greater than *i*, she stays there forever. Gerda starts at the moment of time *l* at the intersection number *s* and goes to the palace of the Snow Queen, located at the intersection number *t*. Moreover, she has to be there at the moment *r* (or earlier), before the arrival of the Queen. Given the description of the road network, determine for *q* queries *l**i*, *r**i*, *s**i* and *t**i* if it's possible for Gerda to get to the palace on time.
The first line of the input contains integers *n*, *m* and *q* (2<=≤<=*n*<=≤<=1000, 1<=≤<=*m*,<=*q*<=≤<=200<=000) — the number of intersections in the road network of Snow Queen's Kingdom, the number of roads and the number of queries you have to answer. The *i*-th of the following *m* lines contains the description of the road number *i*. The description consists of two integers *v**i* and *u**i* (1<=≤<=*v**i*,<=*u**i*<=≤<=*n*, *v**i*<=≠<=*u**i*) — the indices of the intersections connected by the *i*-th road. It's possible to get both from *v**i* to *u**i* and from *u**i* to *v**i* using only this road. Each pair of intersection may appear several times, meaning there are several roads connecting this pair. Last *q* lines contain the queries descriptions. Each of them consists of four integers *l**i*, *r**i*, *s**i* and *t**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*m*, 1<=≤<=*s**i*,<=*t**i*<=≤<=*n*, *s**i*<=≠<=*t**i*) — the moment of time Gerda starts her journey, the last moment of time she is allowed to arrive to the palace, the index of the starting intersection and the index of the intersection where palace is located.
For each query print "Yes" (without quotes) if Gerda can be at the Snow Queen palace on time (not later than *r**i*) or "No" (without quotes) otherwise.
[ "5 4 6\n1 2\n2 3\n3 4\n3 5\n1 3 1 4\n1 3 2 4\n1 4 4 5\n1 4 4 1\n2 3 1 4\n2 2 2 3\n" ]
[ "Yes\nYes\nYes\nNo\nNo\nYes\n" ]
none
[]
30
0
0
25,402
39
Tram
[]
I. Tram
2
64
In a Berland city S*** there is a tram engine house and only one tram. Three people work in the house — the tram driver, the conductor and the head of the engine house. The tram used to leave the engine house every morning and drove along his loop route. The tram needed exactly *c* minutes to complete the route. The head of the engine house controlled the tram’s movement, going outside every *c* minutes when the tram drove by the engine house, and the head left the driver without a bonus if he was even one second late. It used to be so. Afterwards the Berland Federal Budget gave money to make more tramlines in S***, and, as it sometimes happens, the means were used as it was planned. The tramlines were rebuilt and as a result they turned into a huge network. The previous loop route may have been destroyed. S*** has *n* crossroads and now *m* tramlines that links the pairs of crossroads. The traffic in Berland is one way so the tram can move along each tramline only in one direction. There may be several tramlines between two crossroads, which go same way or opposite ways. Every tramline links two different crossroads and for each crossroad there is at least one outgoing tramline. So, the tramlines were built but for some reason nobody gave a thought to increasing the number of trams in S***! The tram continued to ride alone but now the driver had an excellent opportunity to get rid of the unending control of the engine house head. For now due to the tramline network he could choose the route freely! Now at every crossroad the driver can arbitrarily choose the way he can go. The tram may even go to the parts of S*** from where it cannot return due to one way traffic. The driver is not afraid of the challenge: at night, when the city is asleep, he can return to the engine house safely, driving along the tramlines in the opposite direction. The city people were rejoicing for some of the had been waiting for the tram to appear on their streets for several years. However, the driver’s behavior enraged the engine house head. Now he tries to carry out an insidious plan of installing cameras to look after the rebellious tram. The plan goes as follows. The head of the engine house wants to install cameras at some crossroads, to choose a period of time *t* and every *t* minutes turn away from the favourite TV show to check where the tram is. Also the head of the engine house wants at all moments of time, divisible by *t*, and only at such moments the tram to appear on a crossroad under a camera. There must be a camera on the crossroad by the engine house to prevent possible terrorist attacks on the engine house head. Among all the possible plans the engine house head chooses the plan with the largest possible value of *t* (as he hates being distracted from his favourite TV show but he has to). If such a plan is not unique, pick the plan that requires the minimal possible number of cameras. Find such a plan.
The first line contains integers *n* and *m* (2<=≤<=*n*,<=*m*<=≤<=105) — the number of crossroads and tramlines in S*** respectively. The next *m* lines contain the descriptions of the tramlines in "*u* *v*" format, where *u* is the initial tramline crossroad and *v* is its final crossroad. The crossroads are numbered with integers from 1 to *n*, and the engine house is at the crossroad number 1.
In the first line output the value of *t*. In the next line output the value of *k* — the required number of the cameras. In the next line output space-separated numbers of the crossroads, where the cameras should be installed. Output the numbers in increasing order.
[ "4 5\n1 2\n2 3\n3 4\n4 1\n1 4\n" ]
[ "2\n2\n1 3\n" ]
none
[]
0
0
-1
25,442
405
Graph Cutting
[ "dfs and similar", "graphs" ]
null
null
Little Chris is participating in a graph cutting contest. He's a pro. The time has come to test his skills to the fullest. Chris is given a simple undirected connected graph with *n* vertices (numbered from 1 to *n*) and *m* edges. The problem is to cut it into edge-distinct paths of length 2. Formally, Chris has to partition all edges of the graph into pairs in such a way that the edges in a single pair are adjacent and each edge must be contained in exactly one pair. For example, the figure shows a way Chris can cut a graph. The first sample test contains the description of this graph. You are given a chance to compete with Chris. Find a way to cut the given graph or determine that it is impossible!
The first line of input contains two space-separated integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=105), the number of vertices and the number of edges in the graph. The next *m* lines contain the description of the graph's edges. The *i*-th line contains two space-separated integers *a**i* and *b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*; *a**i*<=≠<=*b**i*), the numbers of the vertices connected by the *i*-th edge. It is guaranteed that the given graph is simple (without self-loops and multi-edges) and connected. Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++.
If it is possible to cut the given graph into edge-distinct paths of length 2, output lines. In the *i*-th line print three space-separated integers *x**i*, *y**i* and *z**i*, the description of the *i*-th path. The graph should contain this path, i.e., the graph should contain edges (*x**i*,<=*y**i*) and (*y**i*,<=*z**i*). Each edge should appear in exactly one path of length 2. If there are multiple solutions, output any of them. If it is impossible to cut the given graph, print "No solution" (without quotes).
[ "8 12\n1 2\n2 3\n3 4\n4 1\n1 3\n2 4\n3 5\n3 6\n5 6\n6 7\n6 8\n7 8\n", "3 3\n1 2\n2 3\n3 1\n", "3 2\n1 2\n2 3\n" ]
[ "1 2 4\n1 3 2\n1 4 3\n5 3 6\n5 6 8\n6 7 8", "No solution\n", "1 2 3\n" ]
none
[]
46
0
0
25,444
804
Expected diameter of a tree
[ "binary search", "brute force", "dfs and similar", "dp", "sortings", "trees" ]
null
null
Pasha is a good student and one of MoJaK's best friends. He always have a problem to think about. Today they had a talk about the following problem. We have a forest (acyclic undirected graph) with *n* vertices and *m* edges. There are *q* queries we should answer. In each query two vertices *v* and *u* are given. Let *V* be the set of vertices in the connected component of the graph that contains *v*, and *U* be the set of vertices in the connected component of the graph that contains *u*. Let's add an edge between some vertex and some vertex in and compute the value *d* of the resulting component. If the resulting component is a tree, the value *d* is the diameter of the component, and it is equal to -1 otherwise. What is the expected value of *d*, if we choose vertices *a* and *b* from the sets uniformly at random? Can you help Pasha to solve this problem? The diameter of the component is the maximum distance among some pair of vertices in the component. The distance between two vertices is the minimum number of edges on some path between the two vertices. Note that queries don't add edges to the initial forest.
The first line contains three integers *n*, *m* and *q*(1<=≤<=*n*,<=*m*,<=*q*<=≤<=105) — the number of vertices, the number of edges in the graph and the number of queries. Each of the next *m* lines contains two integers *u**i* and *v**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=*n*), that means there is an edge between vertices *u**i* and *v**i*. It is guaranteed that the given graph is a forest. Each of the next *q* lines contains two integers *u**i* and *v**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=*n*) — the vertices given in the *i*-th query.
For each query print the expected value of *d* as described in the problem statement. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6. Let's assume that your answer is *a*, and the jury's answer is *b*. The checker program will consider your answer correct, if .
[ "3 1 2\n1 3\n3 1\n2 3\n", "5 2 3\n2 4\n4 3\n4 2\n4 1\n2 5\n" ]
[ "-1\n2.0000000000\n", "-1\n2.6666666667\n2.6666666667\n" ]
In the first example the vertices 1 and 3 are in the same component, so the answer for the first query is -1. For the second query there are two options to add the edge: one option is to add the edge 1 - 2, the other one is 2 - 3. In both ways the resulting diameter is 2, so the answer is 2. In the second example the answer for the first query is obviously -1. The answer for the second query is the average of three cases: for added edges 1 - 2 or 1 - 3 the diameter is 3, and for added edge 1 - 4 the diameter is 2. Thus, the answer is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/f12c59a7dfd20580ff1e8e5eeab9ecd19cb3c3f1.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
[ { "input": "3 1 2\n1 3\n3 1\n2 3", "output": "-1\n2.0000000000" }, { "input": "5 2 3\n2 4\n4 3\n4 2\n4 1\n2 5", "output": "-1\n2.6666666667\n2.6666666667" }, { "input": "17 15 13\n3 15\n3 1\n15 9\n16 6\n1 5\n1 8\n16 12\n15 7\n9 4\n6 11\n15 14\n9 10\n15 13\n1 17\n11 2\n7 3\n9 6\n9 7\n1 8\n14 13\n16 16\n14 6\n4 4\n3 4\n9 3\n8 13\n15 2\n14 4", "output": "-1\n8.4500000000\n-1\n-1\n-1\n-1\n8.4500000000\n-1\n-1\n-1\n-1\n8.4500000000\n-1" }, { "input": "7 4 31\n1 4\n1 7\n4 6\n5 2\n6 2\n1 1\n3 2\n6 4\n1 6\n3 2\n2 1\n7 3\n7 6\n3 3\n1 1\n4 3\n6 7\n4 4\n3 2\n1 6\n2 6\n1 4\n2 3\n4 2\n4 3\n5 3\n7 5\n3 3\n2 5\n6 1\n4 6\n6 3\n7 2\n1 7\n4 7", "output": "4.5000000000\n-1\n2.0000000000\n-1\n-1\n2.0000000000\n4.5000000000\n3.5000000000\n-1\n-1\n-1\n3.5000000000\n-1\n-1\n2.0000000000\n-1\n4.5000000000\n-1\n2.0000000000\n4.5000000000\n3.5000000000\n2.0000000000\n4.5000000000\n-1\n-1\n-1\n-1\n3.5000000000\n4.5000000000\n-1\n-1" }, { "input": "30 22 21\n1 21\n29 12\n23 7\n7 30\n11 10\n25 2\n10 8\n11 26\n29 13\n28 24\n4 5\n1 20\n20 9\n8 16\n26 14\n1 19\n25 27\n28 22\n5 6\n28 15\n29 18\n10 3\n17 15\n23 23\n5 29\n5 16\n18 8\n25 20\n22 18\n9 18\n6 20\n14 4\n21 23\n30 27\n12 15\n15 14\n26 4\n20 13\n14 17\n11 17\n21 4\n10 11\n21 21", "output": "2.7500000000\n-1\n4.4166666667\n6.6666666667\n6.7500000000\n5.2666666667\n4.5000000000\n5.3500000000\n5.2666666667\n6.6666666667\n5.2666666667\n4.3333333333\n4.5000000000\n6.7500000000\n6.6666666667\n5.3500000000\n5.2857142857\n5.2857142857\n5.2666666667\n-1\n-1" }, { "input": "33 23 15\n15 10\n2 24\n2 14\n19 25\n33 27\n14 21\n13 26\n19 30\n13 9\n33 17\n20 18\n27 22\n25 6\n15 7\n17 32\n14 8\n19 11\n22 16\n7 23\n8 12\n11 3\n30 4\n22 31\n11 10\n9 8\n11 16\n18 23\n14 4\n31 27\n20 32\n18 11\n18 14\n6 10\n3 17\n20 33\n26 9\n27 18\n22 13", "output": "6.7857142857\n5.8333333333\n8.4285714286\n4.5000000000\n7.4523809524\n-1\n6.1428571429\n5.2857142857\n5.1666666667\n6.7857142857\n8.4285714286\n6.1428571429\n-1\n6.1428571429\n6.8095238095" }, { "input": "44 34 9\n23 12\n31 24\n39 37\n16 20\n16 10\n23 28\n25 9\n20 13\n24 14\n5 26\n26 42\n8 17\n13 30\n9 35\n28 15\n39 38\n5 4\n20 6\n38 41\n24 40\n15 2\n20 18\n21 3\n31 34\n16 7\n26 22\n34 27\n41 11\n34 19\n28 43\n11 1\n2 29\n41 32\n16 33\n21 1\n30 15\n44 11\n30 27\n4 7\n8 31\n2 35\n18 7\n35 17", "output": "6.0000000000\n8.3333333333\n5.2857142857\n7.7619047619\n6.9333333333\n5.4285714286\n6.6666666667\n-1\n3.6666666667" }, { "input": "19 16 3\n1 4\n4 16\n16 5\n17 12\n4 7\n3 9\n12 14\n5 19\n17 18\n4 13\n1 6\n3 2\n7 8\n16 15\n15 11\n15 10\n2 16\n7 10\n18 8", "output": "6.9166666667\n-1\n7.7500000000" } ]
0
0
-1
25,456
351
Jeff and Rounding
[ "dp", "greedy", "implementation", "math" ]
null
null
Jeff got 2*n* real numbers *a*1,<=*a*2,<=...,<=*a*2*n* as a birthday present. The boy hates non-integer numbers, so he decided to slightly "adjust" the numbers he's got. Namely, Jeff consecutively executes *n* operations, each of them goes as follows: - choose indexes *i* and *j* (*i*<=≠<=*j*) that haven't been chosen yet; - round element *a**i* to the nearest integer that isn't more than *a**i* (assign to *a**i*: ⌊ *a**i* ⌋); - round element *a**j* to the nearest integer that isn't less than *a**j* (assign to *a**j*: ⌈ *a**j* ⌉). Nevertheless, Jeff doesn't want to hurt the feelings of the person who gave him the sequence. That's why the boy wants to perform the operations so as to make the absolute value of the difference between the sum of elements before performing the operations and the sum of elements after performing the operations as small as possible. Help Jeff find the minimum absolute value of the difference.
The first line contains integer *n* (1<=≤<=*n*<=≤<=2000). The next line contains 2*n* real numbers *a*1, *a*2, ..., *a*2*n* (0<=≤<=*a**i*<=≤<=10000), given with exactly three digits after the decimal point. The numbers are separated by spaces.
In a single line print a single real number — the required difference with exactly three digits after the decimal point.
[ "3\n0.000 0.500 0.750 1.000 2.000 3.000\n", "3\n4469.000 6526.000 4864.000 9356.383 7490.000 995.896\n" ]
[ "0.250\n", "0.279\n" ]
In the first test case you need to perform the operations as follows: (*i* = 1, *j* = 4), (*i* = 2, *j* = 3), (*i* = 5, *j* = 6). In this case, the difference will equal |(0 + 0.5 + 0.75 + 1 + 2 + 3) - (0 + 0 + 1 + 1 + 2 + 3)| = 0.25.
[ { "input": "3\n0.000 0.500 0.750 1.000 2.000 3.000", "output": "0.250" }, { "input": "3\n4469.000 6526.000 4864.000 9356.383 7490.000 995.896", "output": "0.279" }, { "input": "3\n673.674 9263.142 6780.000 9801.000 4640.000 8244.000", "output": "0.184" }, { "input": "3\n6470.649 8295.000 8486.000 9855.000 223.000 579.549", "output": "0.198" }, { "input": "7\n2341.538 9232.119 6646.930 9316.834 5684.000 9078.705 7773.000 3823.674 6357.022 9866.925 310.271 6554.778 8341.098 8407.987", "output": "0.119" }, { "input": "9\n5528.000 205.031 5245.169 8832.592 385.656 7126.360 3988.000 9542.000 3044.042 5288.351 9342.000 9979.021 7096.000 5159.200 9400.000 4996.735 1698.000 5403.939", "output": "0.096" }, { "input": "5\n4103.000 6413.459 1796.000 3486.000 9011.000 5564.000 9044.000 5922.539 3350.039 3746.000", "output": "0.037" }, { "input": "7\n223.999 322.000 677.000 3852.477 2568.390 2410.000 3202.511 2122.870 1566.000 8841.000 8176.424 74.586 3834.000 6847.427", "output": "0.316" }, { "input": "10\n8003.867 4368.000 2243.000 3340.287 5384.000 1036.456 3506.000 4463.000 1477.000 2420.314 9391.000 1696.000 5857.833 244.000 8220.000 5879.000 5424.482 2631.197 7111.000 9157.536", "output": "0.028" }, { "input": "1\n6418.000 157.986", "output": "0.014" }, { "input": "2\n950.000 8019.170 3179.479 9482.963", "output": "0.388" }, { "input": "3\n4469.437 6526.605 4864.154 9356.383 7490.717 995.896", "output": "0.192" }, { "input": "3\n673.674 9263.142 6780.000 9801.000 4640.000 8244.000", "output": "0.184" }, { "input": "3\n6470.649 8295.806 8486.730 9855.351 223.102 579.000", "output": "0.362" }, { "input": "7\n2341.538 9232.119 6646.930 9316.834 5684.640 9078.705 7773.000 3823.674 6357.022 9866.925 310.271 6554.778 8341.098 8407.000", "output": "0.466" }, { "input": "9\n5528.947 205.031 5245.169 8832.592 385.656 7126.360 3988.000 9542.000 3044.042 5288.000 9342.837 9979.021 7096.022 5159.200 9400.485 4996.735 1698.000 5403.939", "output": "0.036" }, { "input": "5\n4103.000 6413.459 1796.000 3486.799 9011.590 5564.000 9044.473 5922.000 3350.039 3746.000", "output": "0.360" }, { "input": "7\n223.000 322.652 677.700 3852.000 2568.390 2410.713 3202.511 2122.870 1566.689 8841.790 8176.424 74.586 3834.000 6847.000", "output": "0.325" }, { "input": "10\n8003.867 4368.000 2243.298 3340.000 5384.489 1036.000 3506.115 4463.317 1477.000 2420.314 9391.186 1696.000 5857.833 244.314 8220.000 5879.647 5424.482 2631.000 7111.130 9157.536", "output": "0.472" }, { "input": "1\n6418.669 157.986", "output": "0.655" }, { "input": "2\n950.335 8019.000 3179.000 9482.000", "output": "0.335" }, { "input": "3\n4469.000 6526.000 4864.000 9356.000 7490.000 995.000", "output": "0.000" }, { "input": "3\n673.000 9263.000 6780.254 9801.548 4640.663 8244.038", "output": "0.497" }, { "input": "3\n6470.000 8295.000 8486.000 9855.000 223.000 579.549", "output": "0.451" }, { "input": "7\n2341.000 9232.000 6646.000 9316.000 5684.000 9078.000 7773.978 3823.000 6357.000 9866.000 310.000 6554.000 8341.000 8407.987", "output": "0.035" }, { "input": "9\n5528.000 205.000 5245.000 8832.000 385.000 7126.000 3988.538 9542.484 3044.000 5288.351 9342.000 9979.000 7096.000 5159.000 9400.000 4996.000 1698.000 5403.000", "output": "0.373" }, { "input": "5\n4103.449 6413.000 1796.581 3486.000 9011.000 5564.010 9044.000 5922.539 3350.000 3746.191", "output": "0.230" }, { "input": "7\n223.999 322.000 677.000 3852.477 2568.000 2410.000 3202.000 2122.000 1566.000 8841.000 8176.000 74.000 3834.286 6847.427", "output": "0.189" }, { "input": "10\n8003.000 4368.194 2243.000 3340.287 5384.000 1036.456 3506.000 4463.000 1477.787 2420.000 9391.000 1696.913 5857.000 244.000 8220.322 5879.000 5424.000 2631.197 7111.000 9157.000", "output": "0.156" }, { "input": "1\n6418.000 157.000", "output": "0.000" }, { "input": "4\n0.999 0.999 0.999 0.999 0.999 0.999 0.000 0.000", "output": "1.994" }, { "input": "1\n0.001 0.001", "output": "0.998" }, { "input": "1\n0.900 0.900", "output": "0.800" }, { "input": "2\n0.999 0.999 0.999 0.999", "output": "1.996" }, { "input": "2\n0.001 0.001 0.001 0.001", "output": "1.996" }, { "input": "1\n1.100 1.200", "output": "0.700" }, { "input": "10\n0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900 0.900", "output": "8.000" }, { "input": "2\n0.200 0.200 0.200 0.200", "output": "1.200" }, { "input": "3\n0.900 0.900 0.900 0.900 0.900 0.000", "output": "1.500" }, { "input": "1\n0.061 0.330", "output": "0.609" }, { "input": "2\n0.100 0.100 0.100 0.100", "output": "1.600" }, { "input": "5\n0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001", "output": "4.990" } ]
124
5,632,000
0
25,482
747
Comments
[ "dfs and similar", "expression parsing", "implementation", "strings" ]
null
null
A rare article in the Internet is posted without a possibility to comment it. On a Polycarp's website each article has comments feed. Each comment on Polycarp's website is a non-empty string consisting of uppercase and lowercase letters of English alphabet. Comments have tree-like structure, that means each comment except root comments (comments of the highest level) has exactly one parent comment. When Polycarp wants to save comments to his hard drive he uses the following format. Each comment he writes in the following format: - at first, the text of the comment is written; - after that the number of comments is written, for which this comment is a parent comment (i. e. the number of the replies to this comments); - after that the comments for which this comment is a parent comment are written (the writing of these comments uses the same algorithm). For example, if the comments look like: then the first comment is written as "hello,2,ok,0,bye,0", the second is written as "test,0", the third comment is written as "one,1,two,2,a,0,b,0". The whole comments feed is written as: "hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0". For a given comments feed in the format specified above print the comments in a different format: - at first, print a integer *d* — the maximum depth of nesting comments; - after that print *d* lines, the *i*-th of them corresponds to nesting level *i*; - for the *i*-th row print comments of nesting level *i* in the order of their appearance in the Policarp's comments feed, separated by space.
The first line contains non-empty comments feed in the described format. It consists of uppercase and lowercase letters of English alphabet, digits and commas. It is guaranteed that each comment is a non-empty string consisting of uppercase and lowercase English characters. Each of the number of comments is integer (consisting of at least one digit), and either equals 0 or does not contain leading zeros. The length of the whole string does not exceed 106. It is guaranteed that given structure of comments is valid.
Print comments in a format that is given in the statement. For each level of nesting, comments should be printed in the order they are given in the input.
[ "hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0\n", "a,5,A,0,a,0,A,0,a,0,A,0\n", "A,3,B,2,C,0,D,1,E,0,F,1,G,0,H,1,I,1,J,0,K,1,L,0,M,2,N,0,O,1,P,0\n" ]
[ "3\nhello test one \nok bye two \na b \n", "2\na \nA a A a A \n", "4\nA K M \nB F H L N O \nC D G I P \nE J \n" ]
The first example is explained in the statements.
[ { "input": "hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0", "output": "3\nhello test one \nok bye two \na b " }, { "input": "a,5,A,0,a,0,A,0,a,0,A,0", "output": "2\na \nA a A a A " }, { "input": "A,3,B,2,C,0,D,1,E,0,F,1,G,0,H,1,I,1,J,0,K,1,L,0,M,2,N,0,O,1,P,0", "output": "4\nA K M \nB F H L N O \nC D G I P \nE J " }, { "input": "BA,0", "output": "1\nBA " }, { "input": "Tu,1,dU,1,zncD,0", "output": "3\nTu \ndU \nzncD " }, { "input": "nwEwA,2,C,1,aG,0,xgv,0", "output": "3\nnwEwA \nC xgv \naG " }, { "input": "aa,0", "output": "1\naa " }, { "input": "ab,0,ba,0", "output": "1\nab ba " }, { "input": "aa,0,ab,1,a,0", "output": "2\naa ab \na " }, { "input": "a,0,bb,1,a,1,bb,0", "output": "3\na bb \na \nbb " }, { "input": "a,2,b,0,bb,1,a,1,a,0", "output": "4\na \nb bb \na \na " }, { "input": "bb,0,b,1,a,1,b,1,a,0,b,0", "output": "4\nbb b b \na \nb \na " }, { "input": "b,2,b,0,b,0,b,1,b,0,ba,1,b,0", "output": "2\nb b ba \nb b b b " }, { "input": "bb,0,bb,0,b,1,ab,0,b,0,a,2,ab,0,bb,0", "output": "2\nbb bb b b a \nab ab bb " }, { "input": "ba,0,aa,1,a,0,bb,1,a,0,a,0,a,0,a,1,a,0", "output": "2\nba aa bb a a a \na a a " }, { "input": "a,3,aa,0,b,0,b,0,b,0,a,0,b,2,aa,1,a,0,ab,0", "output": "3\na b a b \naa b b aa ab \na " } ]
0
0
-1
25,528
977
Divide by three, multiply by two
[ "dfs and similar", "math", "sortings" ]
null
null
Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: - divide the number $x$ by $3$ ($x$ must be divisible by $3$); - multiply the number $x$ by $2$. After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all. You are given a sequence of length $n$ — the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board. Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number. It is guaranteed that the answer exists.
The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) — the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) — rearranged (reordered) sequence that Polycarp can wrote down on the board.
Print $n$ integer numbers — rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board. It is guaranteed that the answer exists.
[ "6\n4 8 6 3 12 9\n", "4\n42 28 84 126\n", "2\n1000000000000000000 3000000000000000000\n" ]
[ "9 3 6 12 4 8 \n", "126 42 84 28 \n", "3000000000000000000 1000000000000000000 \n" ]
In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$.
[ { "input": "6\n4 8 6 3 12 9", "output": "9 3 6 12 4 8 " }, { "input": "4\n42 28 84 126", "output": "126 42 84 28 " }, { "input": "2\n1000000000000000000 3000000000000000000", "output": "3000000000000000000 1000000000000000000 " }, { "input": "19\n46875000000000000 732421875000000 5859375000000000 11444091796875 2929687500000000 187500000000000000 91552734375000 11718750000000000 3000000000000000000 22888183593750 1464843750000000 375000000000000000 45776367187500 183105468750000 93750000000000000 366210937500000 23437500000000000 750000000000000000 1500000000000000000", "output": "11444091796875 22888183593750 45776367187500 91552734375000 183105468750000 366210937500000 732421875000000 1464843750000000 2929687500000000 5859375000000000 11718750000000000 23437500000000000 46875000000000000 93750000000000000 187500000000000000 375000000000000000 750000000000000000 1500000000000000000 3000000000000000000 " }, { "input": "6\n558 744 1488 279 2232 1116", "output": "279 558 1116 2232 744 1488 " }, { "input": "17\n2985984 2239488 7077888 5971968 10616832 746496 28311552 3538944 7962624 3145728 15925248 1492992 14155776 5308416 3981312 11943936 9437184", "output": "2239488 746496 1492992 2985984 5971968 11943936 3981312 7962624 15925248 5308416 10616832 3538944 7077888 14155776 28311552 9437184 3145728 " }, { "input": "18\n47775744 7077888 5971968 3538944 4478976 3145728 2985984 4718592 1572864 5308416 1048576 1492992 23887872 10616832 2239488 11943936 15925248 14155776", "output": "2239488 4478976 1492992 2985984 5971968 11943936 23887872 47775744 15925248 5308416 10616832 3538944 7077888 14155776 4718592 1572864 3145728 1048576 " }, { "input": "3\n9000 1000 3000", "output": "9000 3000 1000 " }, { "input": "2\n3000 9000", "output": "9000 3000 " }, { "input": "2\n3000000000000000000 1000000000000000000", "output": "3000000000000000000 1000000000000000000 " }, { "input": "2\n1 3", "output": "3 1 " }, { "input": "2\n1500000000000000000 3000000000000000000", "output": "1500000000000000000 3000000000000000000 " }, { "input": "3\n4 1 2", "output": "1 2 4 " }, { "input": "2\n2000000000000000004 1000000000000000002", "output": "1000000000000000002 2000000000000000004 " }, { "input": "2\n2999999999999999997 999999999999999999", "output": "2999999999999999997 999999999999999999 " }, { "input": "2\n999999999999999999 1999999999999999998", "output": "999999999999999999 1999999999999999998 " }, { "input": "2\n1999999999999999998 999999999999999999", "output": "999999999999999999 1999999999999999998 " }, { "input": "2\n10 5", "output": "5 10 " } ]
124
4,198,400
-1
25,550
173
Chamber of Secrets
[ "dfs and similar", "shortest paths" ]
null
null
"The Chamber of Secrets has been opened again" — this news has spread all around Hogwarts and some of the students have been petrified due to seeing the basilisk. Dumbledore got fired and now Harry is trying to enter the Chamber of Secrets. These aren't good news for Lord Voldemort. The problem is, he doesn't want anybody to be able to enter the chamber. The Dark Lord is going to be busy sucking life out of Ginny. The Chamber of Secrets is an *n*<=×<=*m* rectangular grid in which some of the cells are columns. A light ray (and a basilisk's gaze) passes through the columns without changing its direction. But with some spell we can make a column magic to reflect the light ray (or the gaze) in all four directions when it receives the ray. This is shown in the figure below. The basilisk is located at the right side of the lower right cell of the grid and is looking to the left (in the direction of the lower left cell). According to the legend, anyone who meets a basilisk's gaze directly dies immediately. But if someone meets a basilisk's gaze through a column, this person will get petrified. We know that the door to the Chamber is located on the left side of the upper left corner of the grid and anyone who wants to enter will look in the direction of its movement (in the direction of the upper right cell) from that position. Given the dimensions of the chamber and the location of regular columns, Lord Voldemort has asked you to find the minimum number of columns that we need to make magic so that anyone who wants to enter the chamber would be petrified or just declare that it's impossible to secure the chamber.
The first line of the input contains two integer numbers *n* and *m* (2<=≤<=*n*,<=*m*<=≤<=1000). Each of the next *n* lines contains *m* characters. Each character is either "." or "#" and represents one cell of the Chamber grid. It's "." if the corresponding cell is empty and "#" if it's a regular column.
Print the minimum number of columns to make magic or -1 if it's impossible to do.
[ "3 3\n.#.\n...\n.#.\n", "4 3\n##.\n...\n.#.\n.#.\n" ]
[ "2\n", "2\n" ]
The figure above shows the first sample test. In the first sample we should make both columns magic. The dragon figure represents the basilisk and the binoculars represent the person who will enter the Chamber of secrets. The black star shows the place where the person will be petrified. Yellow lines represent basilisk gaze moving through columns.
[ { "input": "3 3\n.#.\n...\n.#.", "output": "2" }, { "input": "4 3\n##.\n...\n.#.\n.#.", "output": "2" }, { "input": "3 3\n###\n###\n###", "output": "2" }, { "input": "3 4\n..##\n....\n..#.", "output": "2" }, { "input": "4 3\n#.#\n...\n...\n.##", "output": "2" }, { "input": "4 4\n##..\n..#.\n...#\n...#", "output": "-1" }, { "input": "5 5\n...#.\n.....\n.....\n.....\n..#.#", "output": "-1" }, { "input": "5 5\n.....\n.#...\n.....\n.....\n#.###", "output": "-1" }, { "input": "5 5\n.....\n.###.\n..##.\n..##.\n...#.", "output": "-1" }, { "input": "5 5\n.##..\n.##..\n.#.#.\n..#..\n..#..", "output": "2" }, { "input": "5 5\n...#.\n.#..#\n#.#.#\n#....\n#.#..", "output": "-1" }, { "input": "5 5\n.#.#.\n#..#.\n.#.##\n.#.##\n##.#.", "output": "2" }, { "input": "5 5\n.####\n#.###\n#####\n#.###\n#####", "output": "2" }, { "input": "5 5\n.####\n#.#.#\n#####\n#####\n#####", "output": "2" }, { "input": "5 5\n#####\n#####\n#####\n#####\n#####", "output": "2" }, { "input": "2 42\n.########.#.########.#..#############.##..\n#####.#############.#####.#.#.#####.####..", "output": "2" }, { "input": "2 2\n##\n.#", "output": "2" }, { "input": "4 3\n.#.\n...\n##.\n#.#", "output": "4" }, { "input": "4 3\n..#\n#.#\n...\n#..", "output": "4" }, { "input": "10 10\n#.........\n...#..#...\n..........\n..........\n.......#.#\n..........\n..........\n......##..\n#..#......\n.........#", "output": "10" }, { "input": "4 4\n.#..\n..##\n.#.#\n..#.", "output": "6" }, { "input": "4 3\n#.#\n.##\n...\n.#.", "output": "4" }, { "input": "4 4\n...#\n.##.\n.#.#\n..#.", "output": "6" }, { "input": "4 4\n#...\n.##.\n#.#.\n.#..", "output": "6" }, { "input": "3 3\n..#\n#.#\n#..", "output": "4" }, { "input": "4 4\n#...\n.#.#\n##..\n...#", "output": "6" }, { "input": "5 5\n#....\n.....\n..##.\n#.#..\n...#.", "output": "6" }, { "input": "4 4\n..#.\n#.#.\n#..#\n...#", "output": "6" }, { "input": "4 7\n..#....\n##.##.#\n.##....\n...##.#", "output": "6" }, { "input": "5 2\n.#\n##\n..\n#.\n#.", "output": "4" }, { "input": "4 3\n.#.\n#.#\n##.\n..#", "output": "6" } ]
280
409,600
0
25,638
325
Stadium and Games
[ "binary search", "math" ]
null
null
Daniel is organizing a football tournament. He has come up with the following tournament format: 1. In the first several (possibly zero) stages, while the number of teams is even, they split in pairs and play one game for each pair. At each stage the loser of each pair is eliminated (there are no draws). Such stages are held while the number of teams is even. 1. Eventually there will be an odd number of teams remaining. If there is one team remaining, it will be declared the winner, and the tournament ends. Otherwise each of the remaining teams will play with each other remaining team once in round robin tournament (if there are *x* teams, there will be games), and the tournament ends. For example, if there were 20 teams initially, they would begin by playing 10 games. So, 10 teams would be eliminated, and the remaining 10 would play 5 games. Then the remaining 5 teams would play 10 games in a round robin tournament. In total there would be 10+5+10=25 games. Daniel has already booked the stadium for *n* games. Help him to determine how many teams he should invite so that the tournament needs exactly *n* games. You should print all possible numbers of teams that will yield exactly *n* games in ascending order, or -1 if there are no such numbers.
The first line contains a single integer *n* (1<=≤<=*n*<=≤<=1018), the number of games that should be played. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Print all possible numbers of invited teams in ascending order, one per line. If exactly *n* games cannot be played, output one number: -1.
[ "3\n", "25\n", "2\n" ]
[ "3\n4\n", "20\n", "-1\n" ]
none
[ { "input": "3", "output": "3\n4" }, { "input": "25", "output": "20" }, { "input": "2", "output": "-1" }, { "input": "1", "output": "2" }, { "input": "15", "output": "10\n16" }, { "input": "314", "output": "-1" }, { "input": "524800", "output": "1025" }, { "input": "5149487579894806", "output": "-1" }, { "input": "249999998807430810", "output": "1414213558" }, { "input": "1000000000000000000", "output": "-1" }, { "input": "4", "output": "-1" }, { "input": "5", "output": "-1" }, { "input": "6", "output": "6" }, { "input": "7", "output": "8" }, { "input": "8", "output": "-1" }, { "input": "9", "output": "-1" }, { "input": "10", "output": "5" }, { "input": "11", "output": "-1" }, { "input": "12", "output": "12" }, { "input": "13", "output": "-1" }, { "input": "14", "output": "-1" }, { "input": "21", "output": "7" }, { "input": "28", "output": "14" }, { "input": "36", "output": "9" }, { "input": "45", "output": "18\n40" }, { "input": "55", "output": "11" }, { "input": "78", "output": "13" }, { "input": "105", "output": "15" }, { "input": "120", "output": "30" }, { "input": "136", "output": "17" }, { "input": "171", "output": "19\n144" }, { "input": "210", "output": "21\n120" }, { "input": "255", "output": "136\n256" }, { "input": "5460", "output": "105\n1456" }, { "input": "16383", "output": "8256\n16384" }, { "input": "391170", "output": "885\n98176" }, { "input": "1906128", "output": "1953\n121024" }, { "input": "576460752303423487", "output": "576460752303423488" }, { "input": "499999999500000000", "output": "1999999998" }, { "input": "250000001635857933", "output": "2828427124" }, { "input": "999999998765257141", "output": "2828427122" }, { "input": "321730048", "output": "-1" }, { "input": "499999500000", "output": "1999998" }, { "input": "250000000221644371", "output": "1414213562" }, { "input": "58819626242454945", "output": "342985791" }, { "input": "672900920488237864", "output": "-1" }, { "input": "994374468178120050", "output": "1410230101" }, { "input": "999971062750901550", "output": "1414193101" }, { "input": "999999912498231750", "output": "1414213501" }, { "input": "999999943610929003", "output": "1414213523" }, { "input": "999999995936830020", "output": "2828427118" }, { "input": "999999998765257141", "output": "2828427122" }, { "input": "999999997351043580", "output": "1414213561" }, { "input": "496", "output": "62" }, { "input": "3012278988753", "output": "4908994" }, { "input": "20000000100000000", "output": "200000001" }, { "input": "980000156100006216", "output": "2800000222" }, { "input": "995460657326506216", "output": "2822000222" }, { "input": "38927073", "output": "35284" }, { "input": "30110278526854603", "output": "981595076" }, { "input": "6882", "output": "888" }, { "input": "20263965249", "output": "1610472" }, { "input": "936612417", "output": "-1" }, { "input": "529914", "output": "8184" }, { "input": "514948626567892275", "output": "16237416336" }, { "input": "514948642805308611", "output": "32474832672" }, { "input": "1459321801", "output": "-1" }, { "input": "16358075516553", "output": "5856031744" }, { "input": "1337521996548297", "output": "105920063488" }, { "input": "4877709674134636", "output": "-1" }, { "input": "487738618277701671", "output": "8090864197632" }, { "input": "487746708154228600", "output": "-1" }, { "input": "520088094975", "output": "-1" }, { "input": "32767", "output": "32768" }, { "input": "131071", "output": "131072" }, { "input": "1310755", "output": "-1" }, { "input": "32775625", "output": "32768000" }, { "input": "57819024375", "output": "52756480000" }, { "input": "1570397049375", "output": "1059717120000" }, { "input": "72315871219375", "output": "21203517440000" }, { "input": "5323259016854625", "output": "212034912256000" }, { "input": "257957076", "output": "257949696" }, { "input": "5180726200", "output": "5179965440" }, { "input": "8355443183554431", "output": "3355443233554432" }, { "input": "58687091686870911", "output": "53687091736870912" }, { "input": "5000000250000000", "output": "-1" }, { "input": "500000003500000003", "output": "4000000004" }, { "input": "178120883702871", "output": "178120883699712" }, { "input": "266081813928931", "output": "266081813921792" }, { "input": "9005000239863810", "output": "9005000231485440" }, { "input": "10475010", "output": "2096640" }, { "input": "943414054006932870", "output": "943413961980641280" }, { "input": "431105316312401832", "output": "431105315111436288" }, { "input": "686288770539583120", "output": "686288769778712576" }, { "input": "434351073512812035", "output": "434351073436631040" }, { "input": "305752193461383075", "output": "305752193451950080" }, { "input": "660058820389234315", "output": "660058820386488320" }, { "input": "838795430598031275", "output": "838795430597754880" }, { "input": "270215977642229850", "output": "270215977642229760" }, { "input": "576460752303423490", "output": "-1" }, { "input": "864691128455135232", "output": "864691128455135232" }, { "input": "402653184", "output": "402653184" }, { "input": "576460752303423487", "output": "576460752303423488" }, { "input": "268435455", "output": "134225920\n268435456" }, { "input": "530516448", "output": "130284\n16418304" }, { "input": "8539349952", "output": "522732\n132779008" }, { "input": "4095", "output": "91\n2080\n4096" }, { "input": "7791518261859", "output": "31580232\n1812942290944" }, { "input": "72057594037927935", "output": "36028797153181696\n72057594037927936" }, { "input": "288230376151711743", "output": "144115188344291328\n288230376151711744" }, { "input": "999999999999999999", "output": "-1" }, { "input": "4095", "output": "91\n2080\n4096" }, { "input": "500000002500000003", "output": "1000000003" }, { "input": "605000000550000000", "output": "1100000001" }, { "input": "1099511627775", "output": "549756338176\n1099511627776" }, { "input": "73687091368435455", "output": "53687091468435456" }, { "input": "965211250482432409", "output": "-1" }, { "input": "432345564227567616", "output": "432345564227567616" }, { "input": "138485688541650132", "output": "138485688541642752" }, { "input": "4979826519", "output": "2368241664" }, { "input": "1125899906842623", "output": "562949970198528\n1125899906842624" }, { "input": "1073741823", "output": "536887296\n1073741824" }, { "input": "36028797018963967", "output": "36028797018963968" } ]
216
0
0
25,639
96
Lucky Numbers (easy)
[ "binary search", "bitmasks", "brute force" ]
B. Lucky Numbers (easy)
2
256
Petya loves lucky numbers. Everybody knows that positive integers are lucky if their decimal representation doesn't contain digits other than 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Lucky number is super lucky if it's decimal representation contains equal amount of digits 4 and 7. For example, numbers 47, 7744, 474477 are super lucky and 4, 744, 467 are not. One day Petya came across a positive integer *n*. Help him to find the least super lucky number which is not less than *n*.
The only line contains a positive integer *n* (1<=≤<=*n*<=≤<=109). This number doesn't have leading zeroes.
Output the least super lucky number that is more than or equal to *n*. Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specificator.
[ "4500\n", "47\n" ]
[ "4747\n", "47\n" ]
none
[ { "input": "4500", "output": "4747" }, { "input": "47", "output": "47" }, { "input": "1", "output": "47" }, { "input": "12", "output": "47" }, { "input": "4587", "output": "4747" }, { "input": "100", "output": "4477" }, { "input": "1007", "output": "4477" }, { "input": "99999999", "output": "4444477777" }, { "input": "491020945", "output": "4444477777" }, { "input": "1000000000", "output": "4444477777" }, { "input": "777777", "output": "44447777" }, { "input": "99999999", "output": "4444477777" }, { "input": "474", "output": "4477" }, { "input": "85469", "output": "444777" }, { "input": "7474747", "output": "44447777" }, { "input": "2145226", "output": "44447777" }, { "input": "5556585", "output": "44447777" }, { "input": "87584777", "output": "4444477777" }, { "input": "77777777", "output": "4444477777" }, { "input": "999999999", "output": "4444477777" }, { "input": "74477744", "output": "74477744" }, { "input": "444444444", "output": "4444477777" }, { "input": "467549754", "output": "4444477777" }, { "input": "147474747", "output": "4444477777" }, { "input": "555", "output": "4477" }, { "input": "100000", "output": "444777" }, { "input": "74777443", "output": "74777444" }, { "input": "4700007", "output": "44447777" }, { "input": "70070077", "output": "74444777" }, { "input": "123", "output": "4477" }, { "input": "7474", "output": "7474" }, { "input": "3696", "output": "4477" }, { "input": "888999577", "output": "4444477777" }, { "input": "10", "output": "47" }, { "input": "7", "output": "47" }, { "input": "50", "output": "74" }, { "input": "70", "output": "74" }, { "input": "74700", "output": "444777" }, { "input": "1024", "output": "4477" }, { "input": "73", "output": "74" }, { "input": "74710000", "output": "74744477" }, { "input": "444000000", "output": "4444477777" }, { "input": "4", "output": "47" }, { "input": "1", "output": "47" }, { "input": "9", "output": "47" }, { "input": "99", "output": "4477" }, { "input": "48", "output": "74" }, { "input": "47474749", "output": "47474774" }, { "input": "47474774", "output": "47474774" }, { "input": "77777777", "output": "4444477777" }, { "input": "4777", "output": "7447" }, { "input": "7748", "output": "444777" }, { "input": "7773", "output": "444777" }, { "input": "19", "output": "47" }, { "input": "447777", "output": "474477" } ]
154
5,120,000
0
25,643
547
Mike and Fish
[ "constructive algorithms", "dfs and similar", "graphs" ]
null
null
As everyone knows, bears love fish. But Mike is a strange bear; He hates fish! The even more strange thing about him is he has an infinite number of blue and red fish. He has marked *n* distinct points in the plane. *i*-th point is point (*x**i*,<=*y**i*). He wants to put exactly one fish in each of these points such that the difference between the number of red fish and the blue fish on each horizontal or vertical line is at most 1. He can't find a way to perform that! Please help him.
The first line of input contains integer *n* (1<=≤<=*n*<=≤<=2<=×<=105). The next *n* lines contain the information about the points, *i*-th line contains two integers *x**i* and *y**i* (1<=≤<=*x**i*,<=*y**i*<=≤<=2<=×<=105), the *i*-th point coordinates. It is guaranteed that there is at least one valid answer.
Print the answer as a sequence of *n* characters 'r' (for red) or 'b' (for blue) where *i*-th character denotes the color of the fish in the *i*-th point.
[ "4\n1 1\n1 2\n2 1\n2 2\n", "3\n1 1\n1 2\n2 1\n" ]
[ "brrb\n", "brr\n" ]
none
[ { "input": "4\n1 1\n1 2\n2 1\n2 2", "output": "brrb" }, { "input": "3\n1 1\n1 2\n2 1", "output": "brr" }, { "input": "3\n157210 22861\n175396 39466\n40933 17093", "output": "rrr" }, { "input": "5\n55599 84144\n169207 98421\n1909 186625\n31525 147710\n7781 82078", "output": "rrrrr" }, { "input": "15\n44249 54630\n165741 91307\n49455 83026\n52521 88269\n39286 65158\n38282 73821\n96608 30032\n155832 122920\n22021 13546\n161556 192797\n168062 8224\n161221 155335\n5670 180269\n89163 128733\n151226 75254", "output": "rrrrrrrrrrrrrrr" }, { "input": "9\n95316 68815\n95316 23738\n60801 169893\n84639 68815\n109462 87456\n22940 37614\n172202 151462\n84639 23738\n109462 151462", "output": "rbrbrrrrb" }, { "input": "2\n196356 153892\n134153 153892", "output": "rb" }, { "input": "10\n4126 18194\n143965 18194\n32687 18194\n118527 18194\n186573 18194\n97223 18194\n179697 18194\n175536 18194\n107767 18194\n127019 18194", "output": "bbbbrbrrrr" }, { "input": "1\n1 1", "output": "r" }, { "input": "1\n1000 3434", "output": "r" }, { "input": "1\n200000 200000", "output": "r" }, { "input": "2\n1 2\n2 3", "output": "rr" }, { "input": "2\n1 2\n1 3", "output": "br" } ]
1,263
268,390,400
0
25,659
916
Jamie and Tree
[ "data structures", "trees" ]
null
null
To your surprise, Jamie is the final boss! Ehehehe. Jamie has given you a tree with *n* vertices, numbered from 1 to *n*. Initially, the root of the tree is the vertex with number 1. Also, each vertex has a value on it. Jamie also gives you three types of queries on the tree: 1 *v* — Change the tree's root to vertex with number *v*. 2 *u* *v* *x* — For each vertex in the subtree of smallest size that contains *u* and *v*, add *x* to its value. 3 *v* — Find sum of values of vertices in the subtree of vertex with number *v*. A subtree of vertex *v* is a set of vertices such that *v* lies on shortest path from this vertex to root of the tree. Pay attention that subtree of a vertex can change after changing the tree's root. Show your strength in programming to Jamie by performing the queries accurately!
The first line of input contains two space-separated integers *n* and *q* (1<=≤<=*n*<=≤<=105,<=1<=≤<=*q*<=≤<=105) — the number of vertices in the tree and the number of queries to process respectively. The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=108<=≤<=*a**i*<=≤<=108) — initial values of the vertices. Next *n*<=-<=1 lines contains two space-separated integers *u**i*,<=*v**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=*n*) describing edge between vertices *u**i* and *v**i* in the tree. The following *q* lines describe the queries. Each query has one of following formats depending on its type: 1 *v* (1<=≤<=*v*<=≤<=*n*) for queries of the first type. 2 *u* *v* *x* (1<=≤<=*u*,<=*v*<=≤<=*n*,<=<=-<=108<=≤<=*x*<=≤<=108) for queries of the second type. 3 *v* (1<=≤<=*v*<=≤<=*n*) for queries of the third type. All numbers in queries' descriptions are integers. The queries must be carried out in the given order. It is guaranteed that the tree is valid.
For each query of the third type, output the required answer. It is guaranteed that at least one query of the third type is given by Jamie.
[ "6 7\n1 4 2 8 5 7\n1 2\n3 1\n4 3\n4 5\n3 6\n3 1\n2 4 6 3\n3 4\n1 6\n2 2 4 -5\n1 4\n3 3\n", "4 6\n4 3 5 6\n1 2\n2 3\n3 4\n3 1\n1 3\n2 2 4 3\n1 1\n2 2 4 -3\n3 1\n" ]
[ "27\n19\n5\n", "18\n21\n" ]
The following picture shows how the tree varies after the queries in the first sample.
[]
30
0
0
25,677
513
Inversions problem
[ "dp" ]
null
null
You are given a permutation of *n* numbers *p*1,<=*p*2,<=...,<=*p**n*. We perform *k* operations of the following type: choose uniformly at random two indices *l* and *r* (*l*<=≤<=*r*) and reverse the order of the elements *p**l*,<=*p**l*<=+<=1,<=...,<=*p**r*. Your task is to find the expected value of the number of inversions in the resulting permutation.
The first line of input contains two integers *n* and *k* (1<=≤<=*n*<=≤<=100, 1<=≤<=*k*<=≤<=109). The next line contains *n* integers *p*1,<=*p*2,<=...,<=*p**n* — the given permutation. All *p**i* are different and in range from 1 to *n*. The problem consists of three subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. - In subproblem G1 (3 points), the constraints 1<=≤<=*n*<=≤<=6, 1<=≤<=*k*<=≤<=4 will hold. - In subproblem G2 (5 points), the constraints 1<=≤<=*n*<=≤<=30, 1<=≤<=*k*<=≤<=200 will hold. - In subproblem G3 (16 points), the constraints 1<=≤<=*n*<=≤<=100, 1<=≤<=*k*<=≤<=109 will hold.
Output the answer with absolute or relative error no more than 1*e*<=-<=9.
[ "3 1\n1 2 3\n", "3 4\n1 3 2\n" ]
[ "0.833333333333333\n", "1.458333333333334\n" ]
Consider the first sample test. We will randomly pick an interval of the permutation (1, 2, 3) (which has no inversions) and reverse the order of its elements. With probability <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/eb946338365d9781f7d2e9ec692c26702d0ae3a7.png" style="max-width: 100.0%;max-height: 100.0%;"/>, the interval will consist of a single element and the permutation will not be altered. With probability <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/a063853bdfd761520e06382373fe270b6078994b.png" style="max-width: 100.0%;max-height: 100.0%;"/> we will inverse the first two elements' order and obtain the permutation (2, 1, 3) which has one inversion. With the same probability we might pick the interval consisting of the last two elements which will lead to the permutation (1, 3, 2) with one inversion. Finally, with probability <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/a063853bdfd761520e06382373fe270b6078994b.png" style="max-width: 100.0%;max-height: 100.0%;"/> the randomly picked interval will contain all elements, leading to the permutation (3, 2, 1) with 3 inversions. Hence, the expected number of inversions is equal to <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/cc9273ddc69be58e22251bdf070c83670617a9a2.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
[ { "input": "3 1\n1 2 3", "output": "0.833333333333333" }, { "input": "3 4\n1 3 2", "output": "1.458333333333334" }, { "input": "6 1\n4 2 5 1 3 6", "output": "6.380952380952381" }, { "input": "6 2\n1 4 6 5 2 3", "output": "6.954648526077097" }, { "input": "4 4\n2 3 1 4", "output": "2.818400000000000" }, { "input": "4 1\n3 4 2 1", "output": "4.100000000000000" }, { "input": "4 3\n3 1 2 4", "output": "2.824000000000000" }, { "input": "4 4\n4 2 3 1", "output": "3.285600000000000" }, { "input": "4 1\n1 2 4 3", "output": "1.900000000000000" }, { "input": "5 4\n4 3 2 5 1", "output": "5.435950617283950" }, { "input": "5 2\n3 1 2 5 4", "output": "4.342222222222222" }, { "input": "5 1\n3 5 1 2 4", "output": "5.066666666666666" }, { "input": "6 1\n5 2 3 4 1 6", "output": "7.285714285714286" }, { "input": "5 2\n4 3 2 1 5", "output": "4.862222222222222" }, { "input": "6 1\n5 3 4 6 2 1", "output": "10.142857142857142" }, { "input": "6 4\n1 2 3 4 5 6", "output": "6.280675233056186" }, { "input": "6 4\n6 5 4 3 2 1", "output": "8.719324766943814" }, { "input": "1 1\n1", "output": "0.000000000000000" }, { "input": "1 4\n1", "output": "0.000000000000000" }, { "input": "2 4\n1 2", "output": "0.493827160493827" }, { "input": "2 4\n2 1", "output": "0.506172839506173" }, { "input": "3 4\n1 2 3", "output": "1.416666666666667" }, { "input": "29 124\n25 17 19 13 18 10 4 15 27 26 23 12 3 7 29 9 22 6 5 20 24 28 11 14 8 16 2 1 21", "output": "203.000744214151180" }, { "input": "29 121\n1 25 15 18 29 26 3 9 16 8 7 5 12 11 24 28 10 13 27 14 21 17 22 20 6 23 19 4 2", "output": "202.999772441209390" }, { "input": "30 136\n1 29 20 25 15 2 3 16 24 30 17 14 7 10 11 22 6 8 23 18 12 5 19 9 26 21 28 13 27 4", "output": "217.499666343988110" }, { "input": "30 153\n18 5 24 7 12 6 25 14 21 20 27 4 30 8 23 19 11 13 17 1 29 28 15 22 16 9 2 10 26 3", "output": "217.500536909325260" }, { "input": "29 118\n2 15 14 28 8 16 5 25 12 11 23 13 22 26 21 7 20 18 29 4 27 19 6 9 3 10 24 17 1", "output": "203.000281073131700" }, { "input": "30 189\n11 10 2 27 30 7 23 29 25 26 5 15 19 13 28 4 18 1 21 9 3 22 6 14 12 8 20 24 17 16", "output": "217.499983777789450" }, { "input": "30 59\n7 12 26 21 9 29 15 14 28 19 20 25 27 2 18 30 5 24 10 23 16 1 3 8 22 6 17 13 4 11", "output": "217.427219047091630" }, { "input": "28 64\n17 12 16 11 1 6 19 21 15 10 8 27 25 18 23 2 14 20 13 28 22 5 7 3 26 9 24 4", "output": "189.128262226032550" }, { "input": "29 137\n1 11 25 3 27 29 18 9 8 20 4 13 15 28 14 17 23 21 22 7 12 10 5 2 19 16 6 26 24", "output": "202.998255996069960" }, { "input": "29 139\n19 10 21 29 8 18 22 15 26 6 4 17 23 27 14 2 20 5 3 13 7 12 16 24 28 9 1 11 25", "output": "202.999603697519690" }, { "input": "29 140\n2 9 26 20 25 4 21 5 23 15 12 16 10 1 13 22 19 24 28 14 11 29 8 27 18 3 7 6 17", "output": "202.999075312274670" }, { "input": "29 142\n5 28 18 22 23 6 27 25 13 10 20 24 14 11 7 4 19 3 9 29 12 17 15 26 2 21 8 1 16", "output": "202.999409366418920" }, { "input": "30 187\n4 18 2 8 5 9 15 14 25 16 30 12 7 24 13 27 29 26 6 3 20 1 10 28 17 21 22 19 23 11", "output": "217.499974048363410" }, { "input": "29 137\n21 15 22 14 20 7 29 24 16 25 19 5 23 26 3 1 10 11 8 2 18 4 17 6 9 27 12 13 28", "output": "202.999469255107530" }, { "input": "30 71\n17 28 11 29 12 8 26 19 23 3 1 5 25 6 13 10 18 7 16 14 4 20 9 30 22 21 2 24 15 27", "output": "217.416011546137580" }, { "input": "90 371\n61 9 64 71 89 17 53 50 12 54 21 55 75 25 3 63 86 34 14 31 81 74 29 32 68 8 40 56 1 73 82 10 27 42 87 16 20 58 46 36 60 6 43 84 4 22 72 51 57 59 49 83 70 2 79 44 19 45 37 77 7 62 76 26 5 41 65 15 47 33 13 78 66 39 67 85 23 30 28 52 35 48 24 90 18 11 80 69 88 38", "output": "2002.505967958907800" }, { "input": "90 448\n23 45 51 14 89 32 33 75 61 49 17 52 55 80 36 59 27 79 41 88 5 34 28 37 83 39 8 60 70 66 56 85 3 63 2 24 26 78 73 31 1 9 48 35 13 82 54 71 64 62 7 65 57 87 47 4 69 30 38 90 15 43 42 72 40 25 50 67 58 18 21 86 20 77 11 29 16 53 12 10 76 6 68 81 74 22 46 84 19 44", "output": "2002.499014807391600" }, { "input": "96 1803\n21 96 78 94 16 9 60 95 51 72 77 4 54 23 17 10 56 70 61 49 22 48 35 1 37 80 65 8 85 64 42 12 68 81 90 31 67 53 46 5 62 74 41 3 50 47 11 13 32 92 24 71 30 76 55 28 52 34 57 6 82 93 29 73 38 39 40 7 45 26 33 69 14 87 36 18 15 75 2 84 83 58 25 27 66 91 19 79 44 88 63 86 59 89 43 20", "output": "2280.000000000000000" }, { "input": "98 1164\n1 12 77 41 45 25 46 66 47 62 58 53 17 10 23 98 50 67 27 80 29 4 60 42 59 21 88 85 87 28 54 64 83 9 96 6 63 14 55 15 18 76 37 97 73 5 26 69 71 51 7 35 33 11 89 84 78 72 20 40 3 8 44 39 2 70 19 52 68 81 79 24 56 43 13 38 65 92 95 34 31 16 74 75 48 86 93 91 49 30 57 94 36 32 22 90 61 82", "output": "2376.499999996136500" }, { "input": "100 83\n6 98 18 72 51 83 66 40 32 60 90 53 91 10 92 47 94 1 77 8 19 78 9 52 85 2 26 12 97 100 82 84 46 79 38 39 50 35 55 99 37 88 74 4 20 23 43 11 87 59 75 61 71 73 34 86 21 17 44 62 42 95 30 54 5 25 64 81 58 65 7 3 45 70 76 24 89 28 80 41 14 31 49 56 22 63 68 13 29 33 67 96 27 36 15 57 16 93 48 69", "output": "2464.472192612607200" }, { "input": "93 1628\n52 81 47 48 50 10 66 29 63 25 80 93 46 44 7 59 16 14 65 30 72 45 85 26 67 71 55 91 20 1 70 24 77 86 56 28 8 32 39 19 43 31 53 17 92 37 4 6 68 36 62 79 84 40 9 23 58 64 49 61 15 11 83 60 57 78 3 87 38 75 12 34 2 27 13 90 76 35 74 88 18 42 73 69 41 21 22 51 54 89 5 82 33", "output": "2139.000000000000000" }, { "input": "93 1974\n6 35 78 39 10 69 88 70 7 40 86 38 16 90 79 81 34 58 42 8 56 93 5 13 63 24 55 2 84 71 45 43 31 44 17 83 67 72 46 64 59 51 29 91 12 22 77 11 28 14 9 30 21 54 49 3 68 18 65 82 57 74 85 62 75 32 76 15 66 80 52 19 41 92 89 60 33 23 73 1 48 47 4 36 50 25 87 61 20 37 26 27 53", "output": "2138.999999999999500" }, { "input": "97 757\n45 80 71 41 96 66 52 56 83 35 57 92 26 95 61 13 42 14 87 32 64 38 73 76 22 25 7 21 93 81 62 79 65 44 40 54 24 58 5 28 9 49 63 50 2 88 3 47 51 67 36 39 34 18 43 72 30 68 10 91 55 11 33 53 8 17 70 23 4 48 59 74 12 82 94 60 29 15 90 20 97 6 75 31 77 19 46 78 86 27 85 16 69 89 1 84 37", "output": "2328.000001317569500" }, { "input": "99 1846\n94 64 22 75 66 26 82 48 20 65 69 13 21 50 57 33 42 4 62 40 12 91 17 99 61 19 14 59 5 3 47 98 25 41 28 52 24 23 93 1 92 34 70 39 81 7 37 73 15 30 49 35 86 32 90 38 58 85 63 74 96 80 31 54 83 97 9 55 60 16 11 8 6 89 79 77 68 18 51 67 44 71 45 36 76 53 46 78 56 10 29 43 87 72 95 88 84 27 2", "output": "2425.500000000000500" }, { "input": "92 1646\n3 84 7 57 66 19 67 24 11 62 29 35 85 49 42 76 25 72 48 17 34 82 54 43 30 78 22 36 14 90 21 70 69 8 27 1 40 91 83 20 6 33 81 13 15 31 64 46 39 65 71 5 50 59 32 41 73 87 68 37 28 51 52 38 60 4 23 56 61 74 92 58 80 89 75 88 63 10 77 12 9 2 18 26 79 44 45 55 53 86 47 16", "output": "2093.000000000000000" }, { "input": "92 1293\n15 50 55 21 54 90 44 92 73 38 85 83 42 6 41 51 39 75 67 59 26 40 8 2 49 65 88 64 13 14 63 5 4 33 1 82 58 60 34 27 61 52 35 46 22 43 9 32 76 24 36 18 91 12 79 69 16 81 19 10 62 84 53 20 28 30 68 17 56 78 23 3 89 66 25 47 87 29 71 48 31 72 7 11 37 57 45 77 70 74 80 86", "output": "2092.999999999956300" }, { "input": "92 1484\n65 14 55 41 7 66 78 85 87 53 20 42 32 75 74 13 60 73 52 28 50 51 12 46 54 8 29 24 2 26 67 5 47 90 34 45 38 92 1 77 36 88 56 40 59 15 63 10 61 25 82 62 57 17 31 58 39 48 71 30 16 4 43 86 80 3 70 83 91 68 27 89 79 6 35 44 49 76 33 19 64 23 11 18 21 22 69 37 81 9 72 84", "output": "2092.999999999999500" }, { "input": "98 999999637\n19 49 28 60 81 97 48 5 90 94 59 10 39 27 72 43 15 58 47 3 6 25 20 21 74 67 92 87 2 95 96 75 22 41 12 79 36 35 50 65 86 26 73 80 54 56 33 52 85 78 37 13 23 64 46 77 88 34 84 55 57 98 63 32 62 8 82 68 40 45 69 89 11 93 71 53 9 17 4 70 66 18 76 14 91 83 51 61 7 38 1 44 31 42 24 29 16 30", "output": "2376.500000000000000" }, { "input": "92 999999561\n56 32 42 92 6 38 67 73 90 58 17 50 19 35 11 74 65 51 23 60 82 70 68 61 81 52 39 13 76 69 26 16 3 25 36 53 37 80 45 43 4 21 57 12 10 15 85 77 88 66 46 63 5 59 78 89 64 2 28 14 7 75 18 22 40 34 29 27 41 8 55 79 83 44 84 71 86 48 87 1 54 49 24 20 72 47 91 33 31 62 30 9", "output": "2093.000000000000500" }, { "input": "92 999999856\n37 45 52 56 63 9 60 28 30 79 84 91 25 11 53 44 38 18 67 59 35 36 68 51 10 74 2 4 14 55 57 61 92 32 26 47 82 64 12 81 48 7 39 46 85 54 86 13 66 80 31 69 3 34 89 65 75 16 1 22 49 77 27 88 72 20 41 8 42 78 21 29 71 90 50 62 76 15 19 43 24 58 33 70 40 83 6 5 23 17 87 73", "output": "2092.999999999999500" }, { "input": "96 999999657\n75 37 8 51 69 71 19 41 1 36 2 54 21 53 88 26 96 46 64 83 15 63 68 93 86 72 9 91 80 55 87 67 65 39 78 74 28 11 17 61 73 85 94 3 59 45 60 24 66 90 48 79 43 81 89 58 70 49 5 56 12 42 92 40 20 29 52 47 95 7 84 22 13 44 50 77 30 57 4 6 34 62 76 38 10 14 27 31 32 82 35 16 33 25 23 18", "output": "2280.000000000000500" }, { "input": "96 999999966\n30 86 3 40 2 4 65 15 52 71 59 44 77 13 18 48 25 58 50 29 17 24 57 68 33 45 67 12 10 88 19 38 27 93 9 21 16 41 43 63 80 22 72 91 5 74 84 66 47 87 26 95 32 89 37 36 69 55 20 56 54 94 42 64 82 73 8 81 35 79 75 1 96 70 51 83 28 90 53 62 7 31 85 11 34 6 14 23 76 78 46 92 60 39 49 61", "output": "2280.000000000000000" }, { "input": "91 567\n21 73 6 19 38 91 27 28 1 69 76 34 22 75 78 51 47 55 49 23 60 43 70 46 53 13 57 48 77 62 15 68 79 30 45 56 59 52 67 42 35 31 36 9 88 33 65 66 83 14 24 26 90 71 25 74 32 17 58 5 72 41 61 4 3 86 81 84 87 16 18 8 39 63 54 11 40 2 44 85 82 50 37 12 20 10 29 64 80 89 7", "output": "2047.500053595948200" }, { "input": "91 963\n87 11 61 47 43 72 29 50 63 75 70 82 20 40 52 34 27 28 55 18 91 19 83 65 35 64 73 2 90 23 38 80 69 14 54 1 33 42 26 48 25 77 15 86 76 13 66 12 37 79 67 30 89 49 41 58 17 71 78 60 59 31 32 4 74 46 5 44 68 24 51 7 8 53 36 39 6 85 10 57 81 16 56 88 3 84 22 45 9 21 62", "output": "2047.500000015885500" }, { "input": "95 613\n74 19 53 95 75 55 64 41 62 4 88 34 87 79 81 2 58 12 13 84 86 25 72 78 18 94 63 82 51 46 43 11 85 31 27 73 91 26 35 22 1 37 40 80 49 29 66 9 92 30 20 89 6 8 69 77 38 70 93 21 47 71 76 45 33 60 65 36 10 54 44 32 42 28 24 67 50 23 83 52 15 59 17 57 68 16 3 61 5 48 39 14 7 56 90", "output": "2232.499960678047000" }, { "input": "95 949\n39 15 5 46 32 30 55 69 34 18 74 56 2 43 14 48 21 19 61 76 36 22 28 80 27 13 24 35 81 38 85 87 25 53 71 58 54 41 78 65 82 33 26 63 94 66 70 52 88 31 89 83 91 37 49 8 29 6 4 51 73 62 17 45 1 50 3 9 93 90 60 68 16 10 57 7 40 92 12 75 84 95 64 44 86 79 11 20 42 77 59 23 67 47 72", "output": "2232.499999931316800" }, { "input": "97 953\n40 7 3 96 47 28 90 97 77 2 9 57 34 65 59 92 84 16 75 23 74 18 70 4 83 31 61 85 33 46 22 14 64 53 80 26 1 32 5 71 51 68 81 43 25 66 44 17 55 37 89 45 29 76 86 58 82 72 56 69 11 24 20 94 60 41 13 95 91 73 8 19 79 67 38 39 93 54 21 35 12 15 52 49 48 30 62 42 87 6 63 50 36 88 10 78 27", "output": "2328.000000037624900" }, { "input": "90 630\n60 56 72 10 21 73 14 39 74 46 29 79 71 64 80 57 37 40 41 7 6 77 1 22 58 25 4 33 17 35 43 86 67 12 28 30 61 75 48 62 24 81 42 8 66 11 78 47 45 89 38 32 9 65 49 52 23 3 54 2 27 59 19 15 31 36 50 16 85 87 69 82 90 34 18 70 84 51 88 63 68 83 26 5 53 20 13 76 55 44", "output": "2002.500013149054700" }, { "input": "91 870\n61 63 49 54 14 50 2 80 71 62 69 43 48 13 77 21 64 9 11 26 1 74 31 85 37 82 38 59 30 65 22 33 55 87 89 20 51 6 44 53 35 36 57 29 28 42 79 24 15 10 3 72 84 16 52 12 68 23 5 73 86 27 4 67 56 46 75 41 40 58 88 19 90 39 60 25 76 81 18 32 66 7 45 34 78 47 91 17 8 70 83", "output": "2047.499999892056400" }, { "input": "91 519\n33 13 16 60 65 8 83 1 67 52 81 91 71 31 44 56 18 63 21 80 55 2 32 66 50 39 20 77 84 11 14 74 6 89 47 58 70 86 53 19 37 82 51 38 12 41 57 90 76 48 45 30 23 87 17 9 34 29 26 46 62 64 25 85 68 54 78 28 72 22 49 24 36 15 73 59 40 61 75 27 79 5 35 7 4 88 42 43 69 10 3", "output": "2047.500329839691200" }, { "input": "95 641\n24 16 56 21 37 84 45 1 29 14 34 2 40 35 72 86 61 53 25 73 67 18 66 90 81 70 91 79 26 46 5 78 89 41 36 93 88 17 52 92 74 30 64 77 50 65 42 32 94 20 57 62 39 31 82 69 71 43 44 15 6 85 83 47 9 68 76 51 27 38 8 59 87 11 54 55 60 19 22 80 10 58 3 75 12 28 7 48 33 49 95 13 63 4 23", "output": "2232.500001363028300" }, { "input": "95 702\n48 75 22 43 87 56 34 8 35 69 80 11 50 17 52 16 58 1 42 25 65 55 66 81 18 84 54 31 74 26 60 72 89 3 44 83 9 40 49 62 59 32 10 93 73 28 6 78 23 13 47 88 63 29 39 37 20 77 85 21 19 71 5 12 15 68 27 94 46 92 82 24 38 95 70 64 53 33 30 91 76 57 4 61 36 2 14 45 90 67 86 79 7 41 51", "output": "2232.499998867935100" }, { "input": "90 538\n49 84 5 82 64 63 59 9 79 83 44 41 78 21 85 35 10 60 42 75 28 66 40 87 39 25 90 14 62 43 17 89 50 27 51 61 47 81 74 19 30 1 4 57 67 34 54 56 53 72 8 36 32 73 3 22 86 76 12 65 70 18 2 15 23 71 88 26 7 58 80 77 24 38 31 46 20 48 55 37 29 11 13 33 16 52 45 68 69 6", "output": "2002.500273005500500" }, { "input": "90 687\n22 90 80 41 4 73 68 77 20 55 49 85 8 69 30 76 42 88 21 62 52 66 47 63 25 2 53 39 54 12 89 59 45 15 86 6 70 29 71 40 51 26 87 61 3 14 37 13 74 64 79 27 81 23 58 78 67 46 57 24 35 7 84 10 16 83 72 34 38 65 1 60 17 44 32 75 5 28 56 33 19 11 9 48 43 36 31 82 18 50", "output": "2002.499993516592700" } ]
46
0
0
25,698
630
Indivisibility
[ "math", "number theory" ]
null
null
IT City company developing computer games decided to upgrade its way to reward its employees. Now it looks the following way. After a new game release users start buying it actively, and the company tracks the number of sales with precision to each transaction. Every time when the next number of sales is not divisible by any number from 2 to 10 every developer of this game gets a small bonus. A game designer Petya knows that the company is just about to release a new game that was partly developed by him. On the basis of his experience he predicts that *n* people will buy the game during the first month. Now Petya wants to determine how many times he will get the bonus. Help him to know it.
The only line of the input contains one integer *n* (1<=≤<=*n*<=≤<=1018) — the prediction on the number of people who will buy the game.
Output one integer showing how many numbers from 1 to *n* are not divisible by any number from 2 to 10.
[ "12\n" ]
[ "2" ]
none
[ { "input": "12", "output": "2" }, { "input": "2519", "output": "576" }, { "input": "2521", "output": "577" }, { "input": "1", "output": "1" }, { "input": "314159265", "output": "71807832" }, { "input": "718281828459045235", "output": "164178703647781768" }, { "input": "1000000000000000000", "output": "228571428571428571" }, { "input": "987654321234567890", "output": "225749559139329804" }, { "input": "3628800", "output": "829440" }, { "input": "504000000000000000", "output": "115200000000000000" } ]
46
0
3
25,704
689
Friends and Subsequences
[ "binary search", "data structures" ]
null
null
Mike and !Mike are old childhood rivals, they are opposite in everything they do, except programming. Today they have a problem they cannot solve on their own, but together (with you) — who knows? Every one of them has an integer sequences *a* and *b* of length *n*. Being given a query of the form of pair of integers (*l*,<=*r*), Mike can instantly tell the value of while !Mike can instantly tell the value of . Now suppose a robot (you!) asks them all possible different queries of pairs of integers (*l*,<=*r*) (1<=≤<=*l*<=≤<=*r*<=≤<=*n*) (so he will make exactly *n*(*n*<=+<=1)<=/<=2 queries) and counts how many times their answers coincide, thus for how many pairs is satisfied. How many occasions will the robot count?
The first line contains only integer *n* (1<=≤<=*n*<=≤<=200<=000). The second line contains *n* integer numbers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=109<=≤<=*a**i*<=≤<=109) — the sequence *a*. The third line contains *n* integer numbers *b*1,<=*b*2,<=...,<=*b**n* (<=-<=109<=≤<=*b**i*<=≤<=109) — the sequence *b*.
Print the only integer number — the number of occasions the robot will count, thus for how many pairs is satisfied.
[ "6\n1 2 3 2 1 4\n6 7 1 2 3 2\n", "3\n3 3 3\n1 1 1\n" ]
[ "2\n", "0\n" ]
The occasions in the first sample case are: 1.*l* = 4,*r* = 4 since *max*{2} = *min*{2}. 2.*l* = 4,*r* = 5 since *max*{2, 1} = *min*{2, 3}. There are no occasions in the second sample case since Mike will answer 3 to any query pair, but !Mike will always answer 1.
[ { "input": "6\n1 2 3 2 1 4\n6 7 1 2 3 2", "output": "2" }, { "input": "3\n3 3 3\n1 1 1", "output": "0" }, { "input": "17\n714413739 -959271262 714413739 -745891378 926207665 -404845105 -404845105 -959271262 -189641729 -670860364 714413739 -189641729 192457837 -745891378 -670860364 536388097 -959271262\n-417715348 -959271262 -959271262 714413739 -189641729 571055593 571055593 571055593 -417715348 -417715348 192457837 -745891378 536388097 571055593 -189641729 571055593 -670860364", "output": "1" }, { "input": "1\n509658558\n509658558", "output": "1" }, { "input": "1\n509658558\n-544591380", "output": "0" }, { "input": "3\n1 1 1\n2 2 2", "output": "0" } ]
982
456,908,800
3
25,713
333
Chips
[ "greedy" ]
null
null
Gerald plays the following game. He has a checkered field of size *n*<=×<=*n* cells, where *m* various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for *n*<=-<=1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases: - At least one of the chips at least once fell to the banned cell. - At least once two chips were on the same cell. - At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row). In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points.
The first line contains two space-separated integers *n* and *m* (2<=≤<=*n*<=≤<=1000, 0<=≤<=*m*<=≤<=105) — the size of the field and the number of banned cells. Next *m* lines each contain two space-separated integers. Specifically, the *i*-th of these lines contains numbers *x**i* and *y**i* (1<=≤<=*x**i*,<=*y**i*<=≤<=*n*) — the coordinates of the *i*-th banned cell. All given cells are distinct. Consider the field rows numbered from top to bottom from 1 to *n*, and the columns — from left to right from 1 to *n*.
Print a single integer — the maximum points Gerald can earn in this game.
[ "3 1\n2 2\n", "3 0\n", "4 3\n3 1\n3 2\n3 3\n" ]
[ "0\n", "1\n", "1\n" ]
In the first test the answer equals zero as we can't put chips into the corner cells. In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips. In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4).
[ { "input": "3 1\n2 2", "output": "0" }, { "input": "3 0", "output": "1" }, { "input": "4 3\n3 1\n3 2\n3 3", "output": "1" }, { "input": "2 1\n1 1", "output": "0" }, { "input": "2 3\n1 2\n2 1\n2 2", "output": "0" }, { "input": "5 1\n3 2", "output": "4" }, { "input": "5 1\n2 3", "output": "4" }, { "input": "1000 0", "output": "1996" }, { "input": "999 0", "output": "1993" }, { "input": "5 5\n3 2\n5 4\n3 3\n2 3\n1 2", "output": "1" }, { "input": "5 5\n3 2\n1 4\n5 1\n4 5\n3 1", "output": "2" }, { "input": "5 5\n2 2\n5 3\n2 3\n5 1\n4 4", "output": "1" }, { "input": "6 5\n2 6\n6 5\n3 1\n2 2\n1 2", "output": "4" }, { "input": "6 5\n2 6\n5 2\n4 3\n6 6\n2 5", "output": "2" }, { "input": "6 5\n2 1\n6 4\n2 2\n4 3\n4 1", "output": "3" } ]
1,000
28,672,000
0
25,725
171
ucyhf
[ "*special", "brute force", "implementation", "number theory" ]
null
null
qd ucyhf yi q fhycu dkcruh mxeiu huluhiu yi q tyvvuhudj fhycu dkcruh. oekh jqia yi je vydt jxu djx ucyhf.
jxu ydfkj sediyiji ev q iydwbu ydjuwuh *d* (1<=≤<=*d*<=≤<=11184) — jxu edu-rqiut ydtun ev jxu ucyhf je vydt.
ekjfkj q iydwbu dkcruh.
[ "1\n" ]
[ "13\n" ]
none
[ { "input": "1", "output": "13" }, { "input": "2", "output": "17" }, { "input": "3", "output": "31" }, { "input": "4", "output": "37" }, { "input": "5", "output": "71" }, { "input": "6", "output": "73" }, { "input": "7", "output": "79" }, { "input": "8", "output": "97" }, { "input": "9", "output": "107" }, { "input": "10", "output": "113" }, { "input": "8216", "output": "768377" }, { "input": "119", "output": "3359" }, { "input": "10618", "output": "975193" }, { "input": "6692", "output": "399731" }, { "input": "10962", "output": "990511" }, { "input": "6848", "output": "706463" }, { "input": "4859", "output": "323149" }, { "input": "8653", "output": "787433" }, { "input": "6826", "output": "705533" }, { "input": "10526", "output": "971513" }, { "input": "9819", "output": "939487" }, { "input": "10844", "output": "984341" }, { "input": "7779", "output": "748169" }, { "input": "1340", "output": "91009" }, { "input": "4020", "output": "190871" }, { "input": "2279", "output": "122867" }, { "input": "5581", "output": "353057" }, { "input": "11107", "output": "997001" }, { "input": "7397", "output": "731053" }, { "input": "5273", "output": "340573" }, { "input": "10476", "output": "969481" }, { "input": "7161", "output": "720611" }, { "input": "4168", "output": "196193" }, { "input": "1438", "output": "93887" }, { "input": "6327", "output": "384913" }, { "input": "10107", "output": "953077" }, { "input": "6399", "output": "388313" }, { "input": "11182", "output": "999853" }, { "input": "11183", "output": "999931" }, { "input": "11184", "output": "999983" } ]
746
1,228,800
-1
25,741
549
Happy Line
[ "constructive algorithms", "greedy", "sortings" ]
null
null
Do you like summer? Residents of Berland do. They especially love eating ice cream in the hot summer. So this summer day a large queue of *n* Berland residents lined up in front of the ice cream stall. We know that each of them has a certain amount of berland dollars with them. The residents of Berland are nice people, so each person agrees to swap places with the person right behind him for just 1 dollar. More formally, if person *a* stands just behind person *b*, then person *a* can pay person *b* 1 dollar, then *a* and *b* get swapped. Of course, if person *a* has zero dollars, he can not swap places with person *b*. Residents of Berland are strange people. In particular, they get upset when there is someone with a strictly smaller sum of money in the line in front of them. Can you help the residents of Berland form such order in the line so that they were all happy? A happy resident is the one who stands first in the line or the one in front of who another resident stands with not less number of dollars. Note that the people of Berland are people of honor and they agree to swap places only in the manner described above.
The first line contains integer *n* (1<=≤<=*n*<=≤<=200<=000) — the number of residents who stand in the line. The second line contains *n* space-separated integers *a**i* (0<=≤<=*a**i*<=≤<=109), where *a**i* is the number of Berland dollars of a man standing on the *i*-th position in the line. The positions are numbered starting from the end of the line.
If it is impossible to make all the residents happy, print ":(" without the quotes. Otherwise, print in the single line *n* space-separated integers, the *i*-th of them must be equal to the number of money of the person on position *i* in the new line. If there are multiple answers, print any of them.
[ "2\n11 8\n", "5\n10 9 7 10 6\n", "3\n12 3 3\n" ]
[ "9 10 ", ":(\n", "4 4 10 " ]
In the first sample two residents should swap places, after that the first resident has 10 dollars and he is at the head of the line and the second resident will have 9 coins and he will be at the end of the line. In the second sample it is impossible to achieve the desired result. In the third sample the first person can swap with the second one, then they will have the following numbers of dollars: 4 11 3, then the second person (in the new line) swaps with the third one, and the resulting numbers of dollars will equal to: 4 4 10. In this line everybody will be happy.
[ { "input": "2\n11 8", "output": "9 10 " }, { "input": "5\n10 9 7 10 6", "output": ":(" }, { "input": "3\n12 3 3", "output": "4 4 10 " }, { "input": "4\n7 3 9 10", "output": "4 6 9 10 " }, { "input": "1\n1", "output": "1 " }, { "input": "5\n15 5 8 6 3", "output": "6 6 7 7 11 " }, { "input": "1\n1000000000", "output": "1000000000 " }, { "input": "2\n2 1", "output": ":(" }, { "input": "3\n11 1 9", "output": ":(" }, { "input": "1\n0", "output": "0 " }, { "input": "20\n38 32 21 44 26 24 15 32 34 40 31 33 33 13 26 28 12 10 14 18", "output": "21 22 24 24 24 24 24 25 25 28 28 28 28 28 28 28 28 28 29 30 " }, { "input": "20\n43 38 20 41 16 37 27 29 19 17 24 19 28 8 14 32 13 21 32 16", "output": "20 20 20 23 23 23 23 23 25 25 25 25 26 26 26 27 27 27 29 31 " }, { "input": "20\n44 50 41 18 28 31 21 38 12 20 28 15 12 29 16 31 34 24 19 15", "output": "20 20 22 23 23 24 24 25 26 27 27 27 29 29 29 29 29 29 32 32 " }, { "input": "10\n920480900 920480899 920480898 920480897 920480896 920480895 920480894 920480893 920480892 920480891", "output": ":(" }, { "input": "10\n536259132 536259132 536259132 536259132 536259132 536259132 536259132 536259132 536259132 536259132", "output": "536259132 536259132 536259132 536259132 536259132 536259132 536259132 536259132 536259132 536259132 " }, { "input": "10\n876584065 876584063 876584061 876584059 876584057 876584055 876584053 876584051 876584049 876584047", "output": "876584056 876584056 876584056 876584056 876584056 876584056 876584056 876584056 876584056 876584056 " }, { "input": "10\n528402489 528402486 528402483 528402480 528402477 528402474 528402471 528402468 528402465 528402462", "output": "528402471 528402472 528402473 528402474 528402475 528402476 528402477 528402478 528402479 528402480 " }, { "input": "10\n383593860 383593860 383593860 383593860 383593860 383593858 383593856 383593854 383593852 383593850", "output": ":(" }, { "input": "10\n198447418 198447416 198447414 198447412 198447410 198447410 198447410 198447410 198447410 198447410", "output": ":(" }, { "input": "10\n520230360 520230361 520230362 520230363 520230364 520230365 520230366 520230367 520230368 520230369", "output": "520230360 520230361 520230362 520230363 520230364 520230365 520230366 520230367 520230368 520230369 " } ]
1,000
14,233,600
0
25,755
888
Xor-MST
[ "bitmasks", "constructive algorithms", "data structures" ]
null
null
You are given a complete undirected graph with *n* vertices. A number *a**i* is assigned to each vertex, and the weight of an edge between vertices *i* and *j* is equal to *a**i*<=*xor*<=*a**j*. Calculate the weight of the minimum spanning tree in this graph.
The first line contains *n* (1<=≤<=*n*<=≤<=200000) — the number of vertices in the graph. The second line contains *n* integers *a*1, *a*2, ..., *a**n* (0<=≤<=*a**i*<=&lt;<=230) — the numbers assigned to the vertices.
Print one number — the weight of the minimum spanning tree in the graph.
[ "5\n1 2 3 4 5\n", "4\n1 2 3 4\n" ]
[ "8\n", "8\n" ]
none
[ { "input": "5\n1 2 3 4 5", "output": "8" }, { "input": "4\n1 2 3 4", "output": "8" }, { "input": "1\n1", "output": "0" } ]
46
0
0
25,815
730
Toda 2
[ "greedy", "implementation" ]
null
null
A group of *n* friends enjoys playing popular video game Toda 2. There is a rating system describing skill level of each player, initially the rating of the *i*-th friend is *r**i*. The friends decided to take part in the championship as a team. But they should have equal ratings to be allowed to compose a single team consisting of all *n* friends. So the friends are faced with the problem: how to make all their ratings equal. One way to change ratings is to willingly lose in some matches. Friends can form a party consisting of two to five (but not more than *n*) friends and play a match in the game. When the party loses, the rating of each of its members decreases by 1. A rating can't become negative, so *r**i*<==<=0 doesn't change after losing. The friends can take part in multiple matches, each time making a party from any subset of friends (but remember about constraints on party size: from 2 to 5 members). The friends want to make their ratings equal but as high as possible. Help the friends develop a strategy of losing the matches so that all their ratings become equal and the resulting rating is maximum possible.
The first line contains a single integer *n* (2<=≤<=*n*<=≤<=100) — the number of friends. The second line contains *n* non-negative integers *r*1,<=*r*2,<=...,<=*r**n* (0<=≤<=*r**i*<=≤<=100), where *r**i* is the initial rating of the *i*-th friend.
In the first line, print a single integer *R* — the final rating of each of the friends. In the second line, print integer *t* — the number of matches the friends have to play. Each of the following *t* lines should contain *n* characters '0' or '1', where the *j*-th character of the *i*-th line is equal to: - '0', if friend *j* should not play in match *i*, - '1', if friend *j* should play in match *i*. Each line should contain between two and five characters '1', inclusive. The value *t* should not exceed 104, it is guaranteed that such solution exists. Remember that you shouldn't minimize the value *t*, but you should maximize *R*. If there are multiple solutions, print any of them.
[ "5\n4 5 1 7 4\n", "2\n1 2\n", "3\n1 1 1\n" ]
[ "1\n8\n01010\n00011\n01010\n10010\n00011\n11000\n00011\n11000\n", "0\n2\n11\n11\n", "1\n0\n" ]
none
[ { "input": "5\n4 5 1 7 4", "output": "1\n8\n01010\n00011\n01010\n10010\n00011\n11000\n00011\n11000" }, { "input": "2\n1 2", "output": "0\n2\n11\n11" }, { "input": "3\n1 1 1", "output": "1\n0" }, { "input": "10\n6 8 7 6 8 7 6 7 8 7", "output": "6\n4\n0100100010\n0000000111\n0000110000\n0110000000" }, { "input": "5\n4 4 4 7 3", "output": "2\n6\n00110\n01010\n10010\n00011\n00110\n11000" }, { "input": "5\n4 7 5 2 2", "output": "2\n5\n01100\n01100\n11000\n01100\n11000" }, { "input": "6\n5 4 2 4 3 2", "output": "2\n4\n100100\n110000\n000110\n110000" }, { "input": "7\n7 8 2 7 10 11 5", "output": "2\n17\n0000110\n0000110\n0000110\n0100010\n0001110\n1100000\n0001110\n1100000\n0000011\n0001100\n1100000\n0000011\n0001100\n1100000\n0000011\n0001100\n1100000" }, { "input": "10\n2 3 3 3 2 6 2 5 3 5", "output": "2\n6\n0000010001\n0000010100\n0000010101\n0000000111\n0001010000\n0110000000" }, { "input": "90\n45 69 0 10 8 58 25 66 22 2 4 62 64 90 82 83 67 32 56 80 64 51 78 21 2 90 65 55 11 51 1 43 6 32 25 46 22 46 26 6 43 14 50 40 74 52 44 60 76 35 21 10 3 49 87 23 89 17 65 75 7 3 42 12 39 73 9 88 60 91 3 49 9 29 35 2 37 63 48 31 60 62 50 4 15 71 8 49 66 31", "output": "0\n1847\n000000000000000000000000010000000000000000000000000000000000000000000100000000000000000000\n000000000000010000000000000000000000000000000000000000000000000000000100000000000000000000\n000000000000000000000000000000000000000000000000000000001000000000000100000000000000000000\n000000000000010000000000010000000000000000000000000000000000000000000000000000000000000000\n000000000000000000000000000000000000000000000000000000001000000000010100000000000000000000\n000000000000010000000000010000000000000000..." }, { "input": "100\n20 11 15 10 20 19 10 11 16 12 13 20 11 18 16 16 14 16 13 13 11 12 10 18 14 13 16 15 15 11 12 19 19 11 17 16 11 12 15 12 15 14 20 13 17 13 10 15 18 12 13 16 12 13 18 17 18 18 17 13 15 10 16 15 14 14 12 15 15 15 19 14 15 20 10 15 19 10 15 10 13 19 19 18 18 14 14 14 13 14 19 15 11 11 100 17 11 13 20 12", "output": "10\n272\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100010\n0000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000100000\n0000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000100000\n0000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000100000\n0000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..." }, { "input": "100\n29 26 22 21 28 29 28 24 27 29 25 29 25 25 27 20 25 27 20 26 27 26 21 26 20 30 23 21 22 30 28 25 21 28 29 28 28 24 23 22 25 23 23 30 25 24 27 25 27 25 23 28 25 21 24 25 30 24 29 21 26 28 23 30 30 27 28 20 24 30 28 26 27 23 30 30 26 28 21 23 27 28 23 29 24 90 26 25 21 21 25 22 22 28 23 26 24 29 24 21", "output": "20\n300\n0000000000000000000000000000000000000000000000000000000000000000000000000001000000000100000000000000\n0000000000000000000000000000000000000000000000000000000000000000000000000010000000000100000000000000\n0000000000000000000000000000000000000000000000000000000000000000000001000000000000000100000000000000\n0000000000000000000000000000000000000000000000000000000000000000100000000000000000000100000000000000\n0000000000000000000000000000000000000000000000000000000000000001000000000000000000000100000000..." }, { "input": "100\n34 31 31 50 24 48 43 48 22 32 23 22 32 22 32 23 42 20 28 40 32 31 21 52 44 36 29 25 46 20 37 41 36 20 20 46 25 45 26 35 34 25 37 29 38 47 42 25 26 27 48 44 42 45 32 20 25 20 34 50 37 37 20 50 23 27 23 47 39 45 38 23 20 44 48 34 22 49 30 42 24 45 48 28 46 46 42 27 34 23 37 50 39 39 27 44 22 23 34 37", "output": "20\n701\n0000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000100000000\n0000000000000000000000010000000000000000000000000000000000000001000000000000000000000000000000000000\n0001000000000000000000010000000000000000000000000000000000010000000000000000000000000000000000000000\n0000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000100000000\n0000000000000000000000000000000000000000000000000000000000010001000000000000000000000000000000..." }, { "input": "100\n84 53 98 75 42 42 37 74 22 94 47 8 53 15 28 80 95 74 60 71 71 21 96 31 97 54 31 55 20 76 13 57 10 30 28 66 68 76 24 100 16 51 2 35 56 37 37 84 22 18 15 97 66 25 48 17 95 47 41 56 45 30 29 99 98 8 3 33 41 22 32 11 67 0 50 73 90 55 50 37 24 81 6 4 66 50 51 19 49 10 36 29 62 51 25 10 89 13 46 98", "output": "0\n2342\n0000000000000000000000000000000000000001000000000000000000000001000000000000000000000000000000000000\n0000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000001\n0000000000000000000000000000000000000000000000000000000000000001100000000000000000000000000000000000\n0010000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000\n0000000000000000000000000000000000000000000000000000000000000001100000000000000000000000000000..." }, { "input": "100\n4 3 2 5 5 4 4 5 3 5 5 5 4 2 4 3 5 3 3 3 3 100 3 3 4 4 3 5 5 5 5 3 4 2 2 5 5 3 5 4 3 3 3 4 2 2 2 5 4 2 2 2 4 4 3 4 2 2 2 3 2 5 4 2 5 5 2 5 3 4 3 2 4 5 5 3 5 5 4 3 2 4 2 5 4 3 3 3 4 2 3 3 5 3 5 3 5 5 3 5", "output": "2\n128\n0000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000001\n0000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000100\n0000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000001000\n0000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000100000\n00000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000100..." }, { "input": "100\n4 2 4 1 1 4 1 1 2 4 1 1 4 4 1 1 1 3 2 4 2 3 2 4 3 4 3 4 3 2 1 4 4 4 3 2 4 3 3 2 2 3 1 4 3 4 1 1 4 3 3 1 1 3 2 4 1 4 2 2 2 2 2 2 1 4 3 1 4 4 100 4 4 3 1 2 4 3 3 2 1 2 1 2 2 3 3 2 2 1 2 4 2 4 4 1 4 1 4 3", "output": "1\n127\n0000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000010\n0000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000001000\n0000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000100000\n0000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000001000000\n00000000000000000000000000000000000000000000000000000000000000000000001000000000000000000001000..." }, { "input": "100\n34 37 20 32 28 21 26 35 38 27 23 39 32 21 27 26 36 23 32 33 29 24 22 38 22 29 38 21 32 38 21 28 36 36 27 40 33 33 32 21 20 33 21 33 30 45 33 21 39 32 28 22 34 29 38 39 38 36 30 40 29 25 22 28 35 40 22 28 38 39 40 40 34 40 38 33 38 36 40 25 35 28 39 36 21 21 34 35 21 28 35 37 22 24 36 30 37 34 29 40", "output": "20\n559\n0000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000001\n0000000000000000000000000000000000000000000001000000000000000000000000000000001000000000000000000000\n0000000000000000000000000000000000000000000001000000000000000000000000000100000000000000000000000000\n0000000000000000000000000000000000000000000001000000000000000000000000010000000000000000000000000000\n0000000000000000000000000000000000000000000001000000000000000000000000100000000000000000000000..." }, { "input": "99\n77 79 94 77 79 73 85 56 60 94 99 53 79 100 88 87 86 3 95 79 63 79 76 74 67 88 94 57 93 83 84 75 97 78 80 90 65 91 80 66 100 93 73 90 90 57 99 95 76 91 62 50 93 71 50 84 65 79 55 64 52 96 91 77 96 78 88 64 74 70 66 58 84 68 52 85 85 90 55 71 87 69 97 95 67 100 81 78 62 57 98 74 83 85 89 57 77 71 78", "output": "3\n3670\n000000000000010000000000000000000000000010000000000000000000000000000000000000000000010000000000000\n000000000000000000000000000000000000000010000010000000000000000000000000000000000000010000000000000\n000000000010010000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000100000000\n00000000000000000000000000000000000000001000001000000000000000000000000000000000000000000000000000..." }, { "input": "2\n62 64", "output": "0\n64\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11" }, { "input": "2\n71 70", "output": "0\n71\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11" }, { "input": "5\n4 5 4 3 3", "output": "3\n2\n01100\n11000" }, { "input": "6\n4 2 3 2 2 2", "output": "1\n4\n101000\n100001\n001110\n110000" }, { "input": "7\n4 6 5 8 8 2 6", "output": "2\n12\n0001100\n0001100\n0000101\n0101000\n0001101\n0110000\n0000101\n0011000\n1100000\n0000101\n0011000\n1100000" }, { "input": "10\n4 5 3 3 3 4 2 3 2 2", "output": "2\n5\n0100010000\n1100000000\n0000110100\n0011000000\n1100000000" }, { "input": "90\n39 52 38 12 50 81 18 43 47 28 36 50 7 1 57 71 7 6 14 34 22 30 47 8 19 46 66 36 25 31 87 75 10 63 38 18 54 40 61 8 10 59 37 63 77 8 44 51 81 60 43 4 65 66 5 2 58 43 75 52 18 19 81 7 22 60 68 23 64 28 20 77 49 41 12 64 4 1 24 67 90 77 5 33 70 47 35 25 89 48", "output": "1\n1754\n000000000000000000000000000000000000000000000000000000000000000000000000000000001000000010\n000000000000000000000000000000000000000000000000000000000000000000000000000000001000000010\n000000000000000000000000000000000000000000000000000000000000000000000000000000001000000010\n000000000000000000000000000000100000000000000000000000000000000000000000000000001000000000\n000000000000000000000000000000100000000000000000000000000000000000000000000000001000000010\n000000000000000000000000000000100000000000..." }, { "input": "100\n10 19 10 19 17 11 15 20 15 16 20 10 20 18 20 15 18 17 11 19 10 16 17 18 20 18 17 15 16 11 11 12 19 14 16 20 16 16 20 10 11 15 20 11 16 10 14 12 15 17 20 18 18 10 19 11 13 17 19 15 13 18 10 16 17 11 16 14 10 12 17 12 11 20 11 14 13 13 17 14 12 14 19 10 20 19 11 14 19 11 18 20 17 18 16 20 12 19 20 14", "output": "10\n266\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010010\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000100000000\n0000000000000000000000000000000000000000000000000010000000000000000000000100000000000000000000000000\n0000000000000000000000000000000000000010001000000000000000000000000000000000000000000000000000000000\n0000000000000000000000001000000000010000000000000000000000000000000000000000000000000000000000..." }, { "input": "100\n25 25 22 30 24 21 23 30 28 29 20 22 28 23 20 29 24 26 29 28 23 23 24 27 22 25 26 22 20 22 27 20 24 29 22 21 20 27 23 25 23 20 22 22 21 26 21 27 25 26 25 24 21 28 21 28 25 27 20 20 28 27 23 20 24 20 20 27 30 28 21 29 20 27 26 20 23 22 28 20 23 25 26 25 26 30 26 26 22 27 23 23 26 28 25 24 26 20 24 29", "output": "20\n216\n0000000000000000000000000000000000000000000000000000000000000000000010000000000000000100000000000000\n0001000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000001\n0000000000000000000000000000000000000000000000000000000000000000000010010000000000000000000000000000\n0000000000000000001000000000000001000000000000000000000000000000000000000000000000000000000000..." }, { "input": "100\n50 21 46 32 43 47 44 41 31 20 35 49 33 35 29 34 48 40 32 44 20 24 37 50 26 39 42 42 20 33 41 27 49 47 35 27 23 40 23 38 37 48 41 46 38 24 46 20 42 47 43 47 35 50 46 43 27 35 20 26 46 32 50 26 35 22 29 42 28 47 38 45 46 28 45 24 28 46 46 49 41 37 30 35 25 45 36 30 35 45 24 38 24 45 35 46 23 30 33 26", "output": "20\n809\n0000000000000000000000000000000000000000000000000000010000000010000000000000000000000000000000000000\n1000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000\n0000000000000000000000000000000000000000000000000000010000000010000000000000000100000000000000000000\n0000000000000000000000010000000010000000000000000000000000000000000000000000000000000000000000000000\n1000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000..." }, { "input": "100\n71 48 79 53 91 33 40 14 35 23 77 32 80 63 63 24 50 71 58 3 43 42 17 67 47 28 29 53 2 55 51 93 90 95 97 29 24 23 9 62 15 6 98 9 58 87 93 59 9 62 69 33 87 21 15 17 35 79 51 3 86 28 89 6 75 62 90 25 60 38 4 17 86 78 65 65 9 4 53 10 77 1 91 79 22 91 16 27 65 2 83 38 29 29 2 19 51 66 64 9", "output": "1\n2270\n0000000000000000000000000000000000100000001000000000000000000000000000000000000000000000000000000000\n0000000000000000000000000000000000100000001000000000000000000000000000000000000000000000000000000000\n0000000000000000000000000000000000100000001000000000000000000000000000000000000000000000000000000000\n0000000000000000000000000000000001000000001000000000000000000000000000000000000000000000000000000000\n0000000000000000000000000000000001100000001000000000000000000000000000000000000000000000000000..." }, { "input": "100\n4 2 5 2 5 2 5 5 4 2 5 4 2 2 2 5 4 3 5 2 3 5 3 5 5 3 2 5 2 2 2 3 4 3 5 5 3 4 4 3 3 3 4 5 5 3 2 2 5 5 3 4 2 4 5 2 4 5 4 5 5 4 5 3 5 5 2 3 3 5 4 2 5 2 4 4 4 2 3 5 5 5 2 5 5 2 3 5 3 2 2 3 4 4 4 5 2 2 3 2", "output": "2\n78\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000001001000000010000\n0000000000000000000000000000000000000000000000000000000000000000000000000000000001010000000000000000\n0000000000000000000000000000000000000000000000000000000000000000000000000000000110000000000000000000\n0000000000000000000000000000000000000000000000000000000000000000000001001000000000000000000000000000\n000000000000000000000000000000000000000000000000000000000000000011000000000000000000000000000000..." }, { "input": "100\n4 3 4 3 1 3 2 1 2 4 1 3 3 3 4 1 4 4 4 2 2 2 1 1 4 4 3 1 2 1 2 2 1 1 2 1 1 1 3 4 2 3 3 4 4 3 3 4 3 4 2 1 3 1 2 4 1 1 4 2 4 3 4 1 1 2 3 4 3 3 3 3 4 2 3 4 2 4 1 3 3 4 2 3 2 1 1 3 4 2 4 3 4 2 1 4 2 4 2 3", "output": "1\n79\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001010000000\n0000000000000000000000000000000000000000000000000000000000000000000000000000000001000000100000000000\n0000000000000000000000000000000000000000000000000000000000000000000000000001010000000000000000000000\n000000000000000000000000000000000000000000000000000000000000000000010000100000000000000000000000..." }, { "input": "100\n29 30 36 24 21 22 21 35 35 36 20 20 37 37 26 27 33 30 38 22 28 31 30 22 25 32 32 21 36 30 22 34 38 27 32 23 32 27 35 31 23 36 40 20 27 36 20 27 27 39 22 27 29 36 26 34 20 39 30 28 34 24 28 38 24 33 29 33 30 35 37 31 30 34 29 20 27 23 30 33 33 21 25 39 27 40 35 38 36 28 33 26 38 34 38 37 30 21 29 33", "output": "20\n493\n0000000000000000000000000000000000000000001000000000000000000000000000000000000000000100000000000000\n0000000000000000000000000000000000000000000000000000000001000000000000000000000000010100000000000000\n0000000000000000000000000000000000000000001000000100000000000000000000000000000000000000000000000000\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000010100000\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000010100000000..." }, { "input": "99\n77 93 81 86 86 84 62 55 93 58 50 53 61 91 62 96 83 58 93 87 68 100 53 76 80 60 82 54 88 53 84 85 83 98 55 73 62 64 88 89 72 69 68 85 83 89 72 100 97 83 58 63 80 60 98 72 64 55 62 71 73 99 89 98 96 65 52 68 66 53 62 78 75 81 95 90 97 58 94 87 100 60 59 94 88 79 88 64 89 86 59 67 59 65 81 78 91 95 62", "output": "50\n1285\n000000000000000000000100000000000000000000000001000000000000000000000000000000001000000000000000000\n000000000000000000000000000000000000000000000000000000000000010000000000000000001000000000000000000\n000000000000000000000100000000000000000000000001000000000000000000000000000000000000000000000000000\n000000000000000000000000000000000000000000000000000000000000010100000000000000001000000000000000000\n0000000000000000000000000000000000000000000000010000001000000000000000000000000000000000000000000..." }, { "input": "2\n61 60", "output": "0\n61\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11\n11" }, { "input": "2\n70 70", "output": "70\n0" }, { "input": "100\n6 9 34 78 60 53 6 51 73 96 93 93 23 47 75 85 40 2 84 74 63 94 98 5 47 36 40 97 16 5 21 68 64 24 42 20 32 94 8 99 36 39 7 8 98 85 2 3 68 76 68 93 37 45 88 61 52 70 62 21 65 34 12 95 58 82 90 60 36 36 2 80 75 44 13 44 97 94 48 11 16 25 76 62 47 31 83 80 5 42 96 10 75 53 30 4 60 81 10 2", "output": "2\n2398\n0000000000000000000000000000000000000001000010000000000000000000000000000000000000000000000000000000\n0000000000000000000000100000000000000001000000000000000000000000000000000000000000000000000000000000\n0000000000000000000000000000000000000001000010000000000000000000000000000000100000000000000000000000\n0000000000000000000000100001000000000000000000000000000000000000000000000000000000000000000000000000\n0000000000000000000000000000000000000000000010000000000000000000000000000000100000000000001000..." }, { "input": "100\n52 52 44 69 57 29 44 98 94 98 89 77 67 51 21 91 7 76 91 11 30 49 58 30 34 29 64 53 99 47 99 82 29 28 27 11 66 75 94 37 1 68 75 34 1 78 87 60 79 21 84 84 12 44 85 91 76 8 75 88 56 31 87 37 25 82 85 78 15 23 7 9 91 44 4 56 9 94 17 33 99 75 11 49 24 69 47 30 94 64 1 77 36 48 78 69 85 60 94 86", "output": "1\n2672\n0000000000000000000000000000101000000000000000000000000000000000000000000000000010000000000000000000\n0000000000000000000000000000101000000000000000000000000000000000000000000000000010000000000000000000\n0000000101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n0000000000000000000000000000101000000000000000000000000000000000000000000000000010000000000000000000\n0000000101000000000000000000000000000000000000000000000000000000000000000000000000000000000000..." }, { "input": "99\n32 83 75 47 92 14 21 45 67 49 52 47 1 4 57 95 38 49 23 17 14 29 88 92 2 73 2 95 29 94 74 6 32 94 84 59 63 57 90 81 38 8 85 48 96 69 71 75 39 73 47 36 22 1 15 40 82 54 73 52 6 42 67 21 82 56 4 38 20 79 56 97 22 88 64 21 62 35 43 14 18 38 42 84 76 22 13 68 79 12 63 58 64 1 19 74 39 75 13", "output": "1\n2360\n000000000000000000000000000000000000000000001000000000000000000000000001000000000000000000000000000\n000000000000000000000000000000000000000000001000000000000000000000000001000000000000000000000000000\n000000000000000100000000000100000000000000000000000000000000000000000001000000000000000000000000000\n000000000000000000000000000000000000000000001000000000000000000000000001000000000000000000000000000\n00000000000000000000000000000100010000000000000000000000000000000000000000000000000000000000000000..." }, { "input": "100\n2 1 2 2 1 1 1 2 1 1 1 1 2 1 1 2 2 2 1 2 1 1 2 1 2 1 2 2 1 2 2 2 1 2 1 2 1 2 1 1 2 2 2 2 2 2 2 1 1 2 1 1 2 1 1 1 2 2 1 2 1 1 1 2 2 2 1 2 2 2 1 1 1 1 2 2 2 1 1 2 2 1 1 1 2 2 1 2 1 2 2 2 2 1 1 1 2 2 1 1", "output": "1\n25\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010001100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001100000000\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000001010000000000\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000001100000000000000\n000000000000000000000000000000000000000000000000000000000000000000000000000000011000000000000000..." }, { "input": "4\n1 1 6 2", "output": "0\n6\n0011\n0011\n0110\n1010\n0011\n0011" }, { "input": "3\n1 2 6", "output": "0\n6\n011\n011\n101\n011\n011\n011" }, { "input": "7\n2 5 4 1 6 3 4", "output": "1\n8\n0100100\n0000101\n0110100\n0000111\n0110000\n0000011\n0010100\n1100000" }, { "input": "99\n91 3 12 82 69 36 95 18 97 43 13 18 80 73 93 21 87 71 33 45 38 93 75 80 13 86 74 84 18 35 97 12 41 68 1 33 48 93 99 74 23 66 79 52 81 55 95 27 65 97 60 71 46 63 16 47 7 90 59 94 32 90 38 65 91 94 19 46 67 52 98 60 57 87 42 21 86 27 46 82 39 1 36 23 29 4 30 32 33 44 79 61 93 7 75 12 65 35 68", "output": "1\n2619\n000000000000000000000000000000000000001000000000000000000000000000000010000000000000000000000000000\n000000000000000000000000000000000000001000000000000000000000000000000010000000000000000000000000000\n000000000000000000000000000000000000001000000000010000000000000000000000000000000000000000000000000\n000000001000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000\n00000000000000000000000000000000000000100000000001000000000000000000001000000000000000000000000000..." }, { "input": "100\n1 2 1 1 1 1 1 2 1 2 1 2 2 2 2 2 2 1 2 2 2 1 1 2 1 1 2 2 2 1 1 2 2 1 2 2 1 1 2 1 1 1 2 2 2 1 2 2 2 2 1 1 2 2 2 2 1 2 1 2 2 1 2 2 2 1 1 2 2 2 2 2 1 2 1 1 2 2 1 1 2 1 1 1 2 2 2 2 1 2 1 2 1 2 1 2 1 1 1 2", "output": "1\n28\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010001\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000101000000\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000001010000000000\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000110000000000000\n000000000000000000000000000000000000000000000000000000000000000000000000000000001000100000000000..." }, { "input": "95\n15 20 18 19 13 10 14 17 11 14 20 17 17 22 15 7 12 17 12 7 21 14 21 8 10 14 21 15 21 16 19 14 8 15 11 22 14 9 7 7 8 7 7 9 15 16 10 16 9 10 11 18 17 9 17 8 16 8 16 21 9 22 14 16 14 20 22 15 7 15 80 13 10 10 15 8 8 11 9 8 12 17 19 9 11 22 15 10 21 18 16 8 12 16 15", "output": "7\n356\n00000000000000000000000000000000000000000000000000000000000000000000001000000000000001000000000\n00000000000000000000000000000000000000000000000000000000000000000010001000000000000000000000000\n00000000000000000000000000000000000000000000000000000000000001000000001000000000000000000000000\n00000000000000000000000000000000000100000000000000000000000000000000001000000000000000000000000\n00000000000001000000000000000000000000000000000000000000000000000000001000000000000000000000000\n000000000000000000..." }, { "input": "100\n10 22 21 7 3 5 14 23 17 6 4 2 8 10 7 15 8 23 16 16 18 11 20 17 19 12 13 12 21 10 4 20 19 18 2 6 4 15 6 14 8 19 15 22 16 20 15 23 18 21 5 23 4 8 2 9 15 3 92 6 7 4 16 4 3 3 16 2 3 20 14 22 17 23 11 19 4 2 15 22 19 8 7 13 5 13 11 2 19 15 8 12 17 7 18 4 17 12 15 15", "output": "2\n549\n0000000000000000000000000000000000000000000000000000000000100000000000000100000000000000000000000000\n0000000000000000000000000000000000000000000000000001000000100000000000000000000000000000000000000000\n0000000000000000000000000000000000000000000000010000000000100000000000000000000000000000000000000000\n0000000000000000010000000000000000000000000000000000000000100000000000000000000000000000000000000000\n00000001000000000000000000000000000000000000000000000000001000000000000000000000000000000000000..." }, { "input": "92\n12 10 17 16 16 10 10 19 17 13 10 10 12 16 20 20 16 14 12 19 14 11 20 15 11 16 10 80 14 20 11 19 16 10 10 19 20 11 13 15 15 18 19 20 10 13 12 20 14 17 10 13 10 20 17 19 12 15 10 17 17 18 10 17 20 14 17 11 12 11 11 10 14 14 11 10 18 14 12 20 16 12 16 13 17 20 14 20 12 16 15 17", "output": "10\n245\n00000000000000000000000000010000000000000000000000000000000000000000000000000000000000010000\n00000000000000000000000000010000000000000000000000000000000000000000000000000000000001000000\n00000000000000000000000000010000000000000000000000000000000000000000000000000001000000000000\n00000000000000000000000000010000000000000000000000000000000000001000000000000000000000000000\n00000000000000000000000000010000000000000000000000000100000000000000000000000000000000000000\n00000000000000000000000000010000..." }, { "input": "91\n14 23 8 8 12 9 28 25 20 9 16 14 22 26 17 9 16 9 26 26 9 19 10 28 19 27 20 13 19 18 21 15 20 13 27 7 14 16 10 78 8 20 13 20 27 20 13 20 15 9 7 7 7 14 23 7 7 25 7 26 28 8 9 11 18 19 8 11 8 26 21 19 12 20 26 18 22 22 28 9 21 27 13 11 9 11 9 27 27 25 28", "output": "7\n471\n0000000000000000000000000000000000000001000000000000000000000000000000000000000000000000001\n0000000000000000000000000000000000000001000000000000000000000000000000000000001000000000000\n0000000000000000000000000000000000000001000000000000000000001000000000000000000000000000000\n0000000000000000000000010000000000000001000000000000000000000000000000000000000000000000000\n0000001000000000000000000000000000000001000000000000000000000000000000000000000000000000000\n00000000000000000000000000000000000000..." }, { "input": "97\n16 5 5 8 19 11 6 10 7 16 5 10 10 18 15 6 9 8 6 16 13 12 17 14 14 5 7 14 19 19 18 11 12 18 19 15 18 12 8 16 11 13 6 7 17 18 8 8 19 5 15 11 14 15 8 18 19 16 6 77 12 8 12 12 20 9 19 10 9 16 12 17 20 10 16 20 20 11 5 15 20 6 7 15 18 14 18 6 17 16 10 16 12 13 16 19 6", "output": "5\n404\n0000000000000000000000000000000000000000000000000000000000010000000000000000000010000000000000000\n0000000000000000000000000000000000000000000000000000000000010000000000000000100000000000000000000\n0000000000000000000000000000000000000000000000000000000000010000000000000001000000000000000000000\n0000000000000000000000000000000000000000000000000000000000010000000000001000000000000000000000000\n0000000000000000000000000000000000000000000000000000000000010000100000000000000000000000000000000\n00000000..." }, { "input": "93\n12 8 17 8 17 16 8 16 98 19 13 14 20 12 14 19 13 13 19 14 18 13 20 17 8 14 15 16 19 13 20 12 17 19 11 18 19 19 20 11 13 8 13 8 19 18 11 19 15 20 20 17 8 20 16 17 9 11 8 8 16 13 20 9 11 18 15 9 18 11 17 11 18 15 17 18 13 12 9 15 17 11 17 17 20 18 20 17 19 16 16 17 18", "output": "8\n364\n000000001000000000000000000000000000000000000000000000000000000000000000000000000000001000000\n000000001000000000000000000000000000000000000000000000000000000000000000000000000000100000000\n000000001000000000000000000000000000000000000000000000000000001000000000000000000000000000000\n000000001000000000000000000000000000000000000000000001000000000000000000000000000000000000000\n000000001000000000000000000000000000000000000000001000000000000000000000000000000000000000000\n0000000010000000000000000000..." }, { "input": "10\n67 46 46 67 46 46 67 67 46 67", "output": "46\n42\n0000001101\n1001000000\n0000001101\n1001000000\n0000001101\n1001000000\n0000001101\n1001000000\n0000001101\n1001000000\n0000001101\n1001000000\n0000001101\n1001000000\n0000001101\n1001000000\n0000001101\n1001000000\n0000001101\n1001000000\n0000001101\n1001000000\n0000001101\n1001000000\n0000001101\n1001000000\n0000001101\n1001000000\n0000001101\n1001000000\n0000001101\n1001000000\n0000001101\n1001000000\n0000001101\n1001000000\n0000001101\n1001000000\n0000001101\n1001000000\n0000001101\n1001000000\n..." }, { "input": "100\n86 59 68 74 86 35 86 44 86 86 86 86 86 25 86 86 86 77 54 86 54 30 51 55 86 38 61 68 86 49 86 22 52 86 38 86 86 56 86 37 86 71 48 86 61 57 86 86 61 72 86 44 56 39 19 86 86 86 86 86 56 41 86 77 86 46 45 23 86 69 86 86 35 86 86 66 86 86 86 48 41 86 86 43 86 77 86 86 86 86 28 27 19 86 86 52 86 51 86 51", "output": "19\n2422\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001010\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001100000\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000110000000000\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000011000000000000\n000000000000000000000000000000000000000000000000000000000000000000000000000000000010100000000..." }, { "input": "100\n64 76 71 82 74 87 51 80 70 53 86 53 57 76 52 98 86 69 64 73 82 44 59 77 78 53 59 91 66 75 99 48 67 94 51 46 61 98 46 54 60 89 52 85 64 69 71 55 94 66 88 75 65 51 58 79 95 50 94 50 94 89 58 99 75 87 51 49 51 98 64 63 61 70 94 82 85 81 45 56 48 73 55 64 47 82 88 50 50 78 72 54 74 44 91 47 85 72 47 88", "output": "44\n1256\n0000000000000000000000000000001000000000000000000000000000000001000000000000000000000000000000000000\n0000000000000000000000000000000000000100000000000000000000000001000001000000000000000000000000000000\n0000000000000001000000000000001000000000000000000000000000000000000000000000000000000000000000000000\n0000000000000000000000000000000000000100000000000000000000000001000001000000000000000000000000000000\n000000000000000100000000000000100000000000000000000000000000000000000000000000000000000000000..." }, { "input": "100\n80 82 72 75 73 79 72 80 79 82 81 81 78 75 79 77 82 78 79 78 77 79 73 81 72 79 77 78 77 77 75 74 78 77 72 76 79 77 81 82 82 78 74 82 72 74 73 81 76 75 95 74 76 77 81 95 82 73 81 78 77 82 77 73 95 75 77 82 78 79 79 73 77 75 77 77 75 78 74 77 81 76 79 77 81 77 74 73 75 76 82 81 73 73 81 75 77 81 72 79", "output": "72\n281\n0000000000000000000000000000000000000000000000000010000100000000100000000000000000000000000000000000\n0000000000000000000000000000000000000000000000000010000100000000100000000000000000000000000000000000\n0000000000000000000000000000000000000000000000000010000100000000100000000000000000000000000000000000\n0000000000000000000000000000000000000000000000000010000100000000100000000000000000000000000000000000\n0000000000000000000000000000000000000000000000000010000100000000100000000000000000000000000000..." }, { "input": "100\n61 41 11 61 12 23 7 22 35 20 3 20 2 47 38 3 8 7 7 40 1 34 31 8 18 24 30 41 28 26 17 26 20 27 12 36 30 45 46 47 8 15 10 27 30 38 10 23 47 10 42 37 42 3 16 39 16 9 61 7 2 1 5 43 34 25 11 6 22 19 42 0 43 17 28 17 2 16 36 21 20 37 9 5 44 2 17 46 43 26 24 61 32 4 18 11 40 14 22 18", "output": "0\n1182\n0000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000100000000\n1001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n0000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000100000000\n1001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n0000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000100..." }, { "input": "100\n40 55 53 29 45 24 35 37 46 56 51 51 58 44 54 54 25 33 38 55 45 40 57 62 66 57 30 43 88 47 51 43 30 50 65 51 26 66 26 54 65 36 59 24 52 56 37 29 53 39 60 53 28 46 49 41 50 54 62 38 29 67 50 49 41 88 27 42 27 44 45 44 42 39 62 44 37 48 42 88 88 44 67 26 59 60 44 40 67 28 29 38 41 59 26 38 44 57 88 59", "output": "24\n1161\n0000000000000000000000000000000000000000000000000000000000000000000000000000000110000000000000000010\n0000000000000000000000000000100000000000000000000000000000000000010000000000000000000000000000000000\n0000000000000000000000000000000000000000000000000000000000000000000000000000000110000000000000000010\n0000000000000000000000000000100000000000000000000000000000000000010000000000000000000000000000000000\n000000000000000000000000000000000000000000000000000000000000000000000000000000011000000000000..." }, { "input": "100\n94 81 57 66 82 53 92 96 75 78 94 82 96 90 84 83 58 96 49 85 88 65 94 96 85 92 60 86 50 57 90 60 73 70 69 70 78 63 56 84 84 87 85 80 88 86 95 73 78 76 91 85 85 62 75 90 85 68 62 91 49 51 54 77 79 86 81 83 74 64 56 79 53 55 65 86 49 52 68 56 93 96 54 91 70 74 82 55 84 54 81 73 89 65 96 85 52 85 78 81", "output": "49\n1317\n0000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000100000\n0000000000000000010000010000000000000000000000000000000000000000000000000000000000000000000000000000\n0000000100001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n0000000000000000000000000000000000000000000000100000000000000000000000000000000001000000000000100000\n000000000000000001000001000000000000000000000000000000000000000000000000000000000000000000000..." }, { "input": "100\n83 91 87 78 85 76 82 80 91 88 77 75 73 81 74 91 75 75 84 82 80 91 87 72 80 82 91 75 85 82 82 75 77 79 76 84 87 80 81 78 78 84 75 74 80 87 73 86 79 82 75 84 77 77 87 80 74 76 77 86 83 73 80 76 91 83 85 73 83 87 86 78 74 83 77 80 83 82 85 72 83 81 77 82 83 87 87 77 85 82 85 87 76 74 72 82 84 75 75 91", "output": "72\n436\n0000000000000000000000000010000000000000000000000000000000000000100000000000000000000000000000000001\n0000000000000001000001000000000000000000000000000000000000000000000000000000000000000000000000000000\n0100000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n0000000000000000000000000010000000000000000000000000000000000000100000000000000000000000000000000001\n0000000000000001000001000000000000000000000000000000000000000000000000000000000000000000000000..." }, { "input": "100\n7 22 26 17 16 24 27 2 45 16 9 2 45 20 6 24 18 27 19 19 21 25 23 6 15 13 15 24 1 27 2 23 6 27 15 22 5 3 17 5 1 22 27 22 4 26 0 24 5 2 45 8 16 15 24 9 4 6 2 27 23 45 23 22 5 2 1 0 27 13 24 45 15 17 8 5 12 0 20 4 3 7 15 24 45 28 11 45 28 0 17 45 5 0 19 17 28 10 3 27", "output": "0\n824\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000100000000\n0000000000000000000000000000000000000000000000000000000000000000000000010000000000001000000000000000\n0000000000000000000000000000000000000000000000000010000000000100000000000000000000000000000000000000\n0000000010001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n00000000000000000000000000000000000000000000000000000000000000000000000000000000000000010001000..." }, { "input": "100\n60 65 37 37 41 64 72 71 64 63 59 56 43 32 45 68 28 78 63 67 63 44 28 27 54 78 60 70 44 58 64 66 40 44 26 69 35 47 49 78 78 30 68 58 25 28 68 78 26 63 71 58 53 32 72 25 64 52 51 67 28 67 66 41 33 69 34 57 62 52 55 59 68 42 70 73 72 78 78 32 37 71 36 27 49 60 34 66 33 68 62 78 78 33 24 35 56 58 70 51", "output": "24\n1478\n0000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000110000000\n0000000000000000000000000000000000000000000000010000000000000000000000000000010000000000000000000000\n0000000000000000000000000000000000000001100000000000000000000000000000000000000000000000000000000000\n0000000000000000010000000100000000000000000000000000000000000000000000000000000000000000000000000000\n000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000011..." }, { "input": "100\n95 60 75 63 59 57 95 72 66 57 61 68 66 71 72 68 65 59 60 71 75 70 56 58 69 68 67 95 70 60 65 70 75 95 73 95 66 74 61 68 65 75 69 71 64 95 62 95 59 95 62 75 63 70 57 69 72 67 73 69 69 95 60 64 60 57 71 70 75 59 70 72 61 74 60 95 61 72 71 70 60 59 61 56 56 65 73 59 71 71 75 74 59 75 75 69 58 57 74 68", "output": "56\n653\n0000000000000000000000000000000000000000000000000000000000000100000000000001000000000000000000000000\n0000000000000000000000000000000000000000000000010100000000000000000000000000000000000000000000000000\n0000000000000000000000000000000000010000000001000000000000000000000000000000000000000000000000000000\n0000000000000000000000000001000001000000000000000000000000000000000000000000000000000000000000000000\n1000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..." }, { "input": "100\n84 84 86 84 87 85 87 84 87 92 87 85 85 85 85 86 85 84 84 84 92 86 92 86 92 84 86 84 84 85 85 87 86 84 84 85 92 86 85 84 85 87 86 87 86 92 87 86 87 86 85 85 92 87 85 85 86 85 85 84 84 92 86 85 87 87 87 84 84 87 85 85 85 85 85 84 92 86 86 87 87 86 92 86 86 86 86 92 84 87 86 85 87 87 86 87 85 84 84 84", "output": "84\n105\n0000000000000000000000000000000000000000000000000000000000000000000000000000100000100001000000000000\n0000000000000000000000000000000000000000000000000000100000000100000000000000000000000000000000000000\n0000000000000000000000000000000000001000000001000000000000000000000000000000000000000000000000000000\n0000000000000000000000101000000000000000000000000000000000000000000000000000000000000000000000000000\n0000000001000000000010000000000000000000000000000000000000000000000000000000000000000000000000..." } ]
140
307,200
0
25,869
222
Reducing Fractions
[ "implementation", "math", "number theory", "sortings" ]
null
null
To confuse the opponents, the Galactic Empire represents fractions in an unusual format. The fractions are represented as two sets of integers. The product of numbers from the first set gives the fraction numerator, the product of numbers from the second set gives the fraction denominator. However, it turned out that the programs that work with fractions in this representations aren't complete, they lack supporting the operation of reducing fractions. Implement this operation and the Empire won't forget you.
The first input line contains two space-separated integers *n*, *m* (1<=≤<=*n*,<=*m*<=≤<=105) that show how many numbers the first set (the numerator) and the second set (the denominator) contain, correspondingly. The second line contains *n* space-separated integers: *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=107) — the numbers that are multiplied to produce the numerator. The third line contains *m* space-separated integers: *b*1,<=*b*2,<=...,<=*b**m* (1<=≤<=*b**i*<=≤<=107) — the numbers that are multiplied to produce the denominator.
Print the answer to the problem in the form, similar to the form of the input data. The number of values in the sets you print *n**out*,<=*m**out* must satisfy the inequality 1<=≤<=*n**out*,<=*m**out*<=≤<=105, and the actual values in the sets *a**out*,<=*i* and *b**out*,<=*i* must satisfy the inequality 1<=≤<=*a**out*,<=*i*,<=*b**out*,<=*i*<=≤<=107. Separate the values in the lines by spaces. The printed fraction must be reduced, that is, there mustn't be such integer *x* (*x*<=&gt;<=1), that the numerator and the denominator of the printed fraction are divisible by *x*. If there are several matching answers, print any of them.
[ "3 2\n100 5 2\n50 10\n", "4 3\n2 5 10 20\n100 1 3\n" ]
[ "2 3\n2 1\n1 1 1\n", "1 1\n20\n3\n" ]
In the first test sample the numerator equals 1000, the denominator equals 500. If we reduce fraction 1000/500 by the greatest common divisor of the numerator and the denominator (by 500), we obtain fraction 2/1. In the second test sample the numerator equals 2000, the denominator equals 300. If we reduce fraction 2000/300 by the greatest common divisor of the numerator and the denominator (by 100), we obtain fraction 20/3.
[ { "input": "3 2\n100 5 2\n50 10", "output": "2 3\n2 1\n1 1 1" }, { "input": "4 3\n2 5 10 20\n100 1 3", "output": "1 1\n20\n3" }, { "input": "2 3\n50 10\n100 5 2", "output": "2 3\n1 1 \n2 1 1 " }, { "input": "1 1\n1\n1", "output": "1 1\n1 \n1 " }, { "input": "3 2\n100 5 2\n10 100", "output": "3 2\n1 1 1 \n1 1 " }, { "input": "5 3\n16 24 36 54 81\n4 6 9", "output": "5 3\n16 24 9 27 3 \n1 1 1 " }, { "input": "10 10\n2 5 11 17 23 31 41 47 59 67\n3 7 13 19 29 37 43 53 61 71", "output": "10 10\n2 5 11 17 23 31 41 47 59 67 \n3 7 13 19 29 37 43 53 61 71 " }, { "input": "5 5\n2 9 8 3 5\n5 7 8 1 1", "output": "5 5\n2 9 1 3 1 \n1 7 1 1 1 " }, { "input": "10 11\n7 43 39 13 25 23 33 40 5 2\n42 22 39 1 23 37 12 48 46 2 2", "output": "10 11\n1 43 13 1 25 1 1 5 5 1 \n6 2 3 1 23 37 4 8 1 1 1 " }, { "input": "1 5\n99\n3 55 18 1 19", "output": "1 5\n1 \n3 5 2 1 19 " } ]
1,840
201,216,000
3
25,873
177
Party
[ "dfs and similar", "dsu", "graphs" ]
null
null
To celebrate the second ABBYY Cup tournament, the Smart Beaver decided to throw a party. The Beaver has a lot of acquaintances, some of them are friends with each other, and some of them dislike each other. To make party successful, the Smart Beaver wants to invite only those of his friends who are connected by friendship relations, and not to invite those who dislike each other. Both friendship and dislike are mutual feelings. More formally, for each invited person the following conditions should be fulfilled: - all his friends should also be invited to the party; - the party shouldn't have any people he dislikes; - all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length. We'll say that people *a*1 and *a**p* are connected through a chain of common friends if there exists a sequence of people *a*2,<=*a*3,<=...,<=*a**p*<=-<=1 such that all pairs of people *a**i* and *a**i*<=+<=1 (1<=≤<=*i*<=&lt;<=*p*) are friends. Help the Beaver find the maximum number of acquaintances he can invite.
The first line of input contains an integer *n* — the number of the Beaver's acquaintances. The second line contains an integer *k* — the number of pairs of friends. Next *k* lines contain space-separated pairs of integers *u**i*,<=*v**i* — indices of people who form the *i*-th pair of friends. The next line contains an integer *m* — the number of pairs of people who dislike each other. Next *m* lines describe pairs of people who dislike each other in the same format as the pairs of friends were described. Each pair of people is mentioned in the input at most once . In particular, two persons cannot be friends and dislike each other at the same time. The input limitations for getting 30 points are: - 2<=≤<=*n*<=≤<=14 The input limitations for getting 100 points are: - 2<=≤<=*n*<=≤<=2000
Output a single number — the maximum number of people that can be invited to the party. If a group of people that meets all the requirements is impossible to select, output 0.
[ "9\n8\n1 2\n1 3\n2 3\n4 5\n6 7\n7 8\n8 9\n9 6\n2\n1 6\n7 9\n" ]
[ "3" ]
Let's have a look at the example. Two groups of people can be invited: {1, 2, 3} and {4, 5}, thus the answer will be the size of the largest of these groups. Group {6, 7, 8, 9} doesn't fit, since it includes people 7 and 9 who dislike each other. Group {1, 2, 3, 4, 5} also doesn't fit, because not all of its members are connected by a chain of common friends (for example, people 2 and 5 aren't connected).
[ { "input": "9\n8\n1 2\n1 3\n2 3\n4 5\n6 7\n7 8\n8 9\n9 6\n2\n1 6\n7 9", "output": "3" }, { "input": "2\n1\n1 2\n0", "output": "2" }, { "input": "2\n0\n1\n1 2", "output": "1" }, { "input": "3\n2\n1 2\n1 3\n1\n2 3", "output": "0" }, { "input": "3\n3\n1 3\n2 1\n2 3\n0", "output": "3" }, { "input": "4\n3\n1 2\n2 3\n3 1\n3\n1 4\n4 2\n3 4", "output": "3" }, { "input": "7\n8\n1 2\n1 3\n1 4\n1 5\n2 4\n2 5\n3 4\n5 6\n3\n2 6\n5 7\n6 7", "output": "1" }, { "input": "14\n20\n1 2\n4 5\n4 6\n4 11\n5 7\n5 8\n5 13\n5 14\n7 8\n7 14\n8 9\n8 11\n8 12\n8 14\n10 11\n10 12\n10 14\n11 13\n11 14\n12 14\n5\n1 8\n1 13\n2 10\n7 12\n8 10", "output": "2" }, { "input": "2\n0\n0", "output": "1" }, { "input": "14\n0\n0", "output": "1" }, { "input": "14\n6\n1 2\n2 3\n3 4\n4 5\n8 9\n9 10\n3\n5 6\n6 7\n7 8", "output": "5" }, { "input": "14\n10\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n2 3\n2 4\n2 5\n2 6\n1\n2 7", "output": "1" }, { "input": "2\n0\n1\n1 2", "output": "1" }, { "input": "13\n78\n11 1\n10 6\n6 2\n10 1\n11 6\n11 3\n5 3\n8 1\n12 11\n4 2\n10 3\n13 8\n9 8\n11 7\n7 5\n11 2\n7 1\n4 1\n11 10\n8 3\n13 11\n9 6\n13 9\n12 7\n12 8\n12 9\n10 2\n5 2\n12 10\n9 2\n9 7\n3 2\n7 4\n11 4\n13 1\n10 5\n11 5\n8 5\n10 4\n8 2\n10 9\n4 3\n9 5\n13 12\n13 5\n7 2\n12 4\n9 1\n10 8\n6 3\n6 4\n7 6\n7 3\n12 3\n5 4\n6 1\n12 5\n8 4\n13 3\n12 1\n9 3\n8 6\n11 9\n9 4\n8 7\n12 6\n5 1\n13 10\n13 6\n10 7\n13 4\n13 7\n13 2\n2 1\n6 5\n12 2\n11 8\n3 1\n0", "output": "13" }, { "input": "13\n0\n78\n11 8\n8 4\n13 9\n6 1\n10 5\n5 1\n9 8\n11 3\n13 12\n6 2\n10 9\n9 1\n10 3\n13 6\n8 1\n11 10\n11 1\n11 9\n12 4\n12 11\n11 4\n8 6\n9 6\n13 10\n13 8\n7 2\n8 3\n10 1\n12 10\n6 5\n8 2\n5 4\n9 2\n13 1\n4 1\n13 2\n12 5\n10 7\n7 4\n8 5\n12 6\n4 3\n13 3\n12 2\n9 3\n11 7\n7 3\n2 1\n10 2\n13 7\n7 5\n13 4\n12 7\n4 2\n12 9\n11 5\n10 8\n11 2\n12 3\n3 1\n7 6\n10 6\n12 1\n10 4\n5 2\n9 4\n11 6\n9 7\n5 3\n7 1\n8 7\n6 3\n13 5\n12 8\n6 4\n13 11\n9 5\n3 2", "output": "1" } ]
154
0
0
25,952
653
Bear and Up-Down
[ "brute force", "implementation" ]
null
null
The life goes up and down, just like nice sequences. Sequence *t*1,<=*t*2,<=...,<=*t**n* is called nice if the following two conditions are satisfied: - *t**i*<=&lt;<=*t**i*<=+<=1 for each odd *i*<=&lt;<=*n*; - *t**i*<=&gt;<=*t**i*<=+<=1 for each even *i*<=&lt;<=*n*. For example, sequences (2,<=8), (1,<=5,<=1) and (2,<=5,<=1,<=100,<=99,<=120) are nice, while (1,<=1), (1,<=2,<=3) and (2,<=5,<=3,<=2) are not. Bear Limak has a sequence of positive integers *t*1,<=*t*2,<=...,<=*t**n*. This sequence is not nice now and Limak wants to fix it by a single swap. He is going to choose two indices *i*<=&lt;<=*j* and swap elements *t**i* and *t**j* in order to get a nice sequence. Count the number of ways to do so. Two ways are considered different if indices of elements chosen for a swap are different.
The first line of the input contains one integer *n* (2<=≤<=*n*<=≤<=150<=000) — the length of the sequence. The second line contains *n* integers *t*1,<=*t*2,<=...,<=*t**n* (1<=≤<=*t**i*<=≤<=150<=000) — the initial sequence. It's guaranteed that the given sequence is not nice.
Print the number of ways to swap two elements exactly once in order to get a nice sequence.
[ "5\n2 8 4 7 7\n", "4\n200 150 100 50\n", "10\n3 2 1 4 1 4 1 4 1 4\n", "9\n1 2 3 4 5 6 7 8 9\n" ]
[ "2\n", "1\n", "8\n", "0\n" ]
In the first sample, there are two ways to get a nice sequence with one swap: 1. Swap *t*<sub class="lower-index">2</sub> = 8 with *t*<sub class="lower-index">4</sub> = 7. 1. Swap *t*<sub class="lower-index">1</sub> = 2 with *t*<sub class="lower-index">5</sub> = 7. In the second sample, there is only one way — Limak should swap *t*<sub class="lower-index">1</sub> = 200 with *t*<sub class="lower-index">4</sub> = 50.
[ { "input": "5\n2 8 4 7 7", "output": "2" }, { "input": "4\n200 150 100 50", "output": "1" }, { "input": "10\n3 2 1 4 1 4 1 4 1 4", "output": "8" }, { "input": "9\n1 2 3 4 5 6 7 8 9", "output": "0" }, { "input": "5\n1 1 1 4 3", "output": "1" }, { "input": "10\n7 7 8 10 5 10 1 5 2 6", "output": "2" }, { "input": "50\n11836 28308 72527 92281 139289 93797 134555 148444 40866 111317 21564 87813 65466 20541 99238 2287 74647 128071 18163 61672 39766 55589 138385 147443 138100 142683 60703 15444 52566 72976 147412 116006 115986 110545 79993 100440 9876 71470 75209 62443 64906 88987 72232 2246 63160 45041 729 148611 103397 78474", "output": "0" }, { "input": "10\n522 309 276 454 566 978 175 388 289 276", "output": "0" }, { "input": "20\n8 9 1 10 7 9 5 8 5 7 5 6 1 3 2 7 3 2 6 9", "output": "3" }, { "input": "25\n25 20 58 95 47 68 38 39 24 83 36 68 28 67 25 40 62 99 11 88 74 75 38 90 42", "output": "1" }, { "input": "30\n18647 31594 58075 122543 49766 65303 48728 102863 22542 140297 5300 90685 50141 86948 27074 40214 17945 147095 97758 140835 121469 139920 63817 138623 85609 110002 70046 128002 122139 116109", "output": "1" }, { "input": "39\n18329 39326 21115 36341 3916 40060 23262 41923 17476 42107 17052 23198 10756 32540 14873 28454 23912 35765 9459 45834 85 46756 31859 40087 35420 47585 9781 46544 31859 49453 7394 17459 2816 34051 12519 4077 692 44098 23345", "output": "15" }, { "input": "2\n5 1", "output": "1" }, { "input": "2\n10 10", "output": "0" }, { "input": "6\n1 1 1 2 2 2", "output": "1" }, { "input": "12\n10 15 10 15 10 8 10 15 10 20 30 20", "output": "1" } ]
1,076
15,667,200
3
25,993
645
Robot Rapping Results Report
[ "binary search", "dp", "graphs" ]
null
null
While Farmer John rebuilds his farm in an unfamiliar portion of Bovinia, Bessie is out trying some alternative jobs. In her new gig as a reporter, Bessie needs to know about programming competition results as quickly as possible. When she covers the 2016 Robot Rap Battle Tournament, she notices that all of the robots operate under deterministic algorithms. In particular, robot *i* will beat robot *j* if and only if robot *i* has a higher skill level than robot *j*. And if robot *i* beats robot *j* and robot *j* beats robot *k*, then robot *i* will beat robot *k*. Since rapping is such a subtle art, two robots can never have the same skill level. Given the results of the rap battles in the order in which they were played, determine the minimum number of first rap battles that needed to take place before Bessie could order all of the robots by skill level.
The first line of the input consists of two integers, the number of robots *n* (2<=≤<=*n*<=≤<=100<=000) and the number of rap battles *m* (). The next *m* lines describe the results of the rap battles in the order they took place. Each consists of two integers *u**i* and *v**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=*n*, *u**i*<=≠<=*v**i*), indicating that robot *u**i* beat robot *v**i* in the *i*-th rap battle. No two rap battles involve the same pair of robots. It is guaranteed that at least one ordering of the robots satisfies all *m* relations.
Print the minimum *k* such that the ordering of the robots by skill level is uniquely defined by the first *k* rap battles. If there exists more than one ordering that satisfies all *m* relations, output -1.
[ "4 5\n2 1\n1 3\n2 3\n4 2\n4 3\n", "3 2\n1 2\n3 2\n" ]
[ "4\n", "-1\n" ]
In the first sample, the robots from strongest to weakest must be (4, 2, 1, 3), which Bessie can deduce after knowing the results of the first four rap battles. In the second sample, both (1, 3, 2) and (3, 1, 2) are possible orderings of the robots from strongest to weakest after both rap battles.
[ { "input": "4 5\n2 1\n1 3\n2 3\n4 2\n4 3", "output": "4" }, { "input": "3 2\n1 2\n3 2", "output": "-1" }, { "input": "2 1\n1 2", "output": "1" }, { "input": "2 1\n2 1", "output": "1" }, { "input": "5 10\n1 2\n1 3\n1 4\n1 5\n2 3\n2 4\n2 5\n3 4\n3 5\n4 5", "output": "10" }, { "input": "3 3\n2 1\n3 1\n3 2", "output": "3" }, { "input": "4 6\n1 4\n4 2\n2 3\n1 2\n1 3\n4 3", "output": "3" }, { "input": "3 3\n3 1\n3 2\n1 2", "output": "3" }, { "input": "5 10\n2 1\n1 4\n2 5\n2 3\n5 4\n3 4\n3 1\n3 5\n2 4\n1 5", "output": "10" }, { "input": "3 3\n1 2\n3 2\n1 3", "output": "3" }, { "input": "3 3\n2 3\n1 3\n2 1", "output": "3" }, { "input": "3 3\n1 2\n3 1\n3 2", "output": "2" }, { "input": "3 3\n1 3\n3 2\n1 2", "output": "2" }, { "input": "5 10\n4 1\n4 5\n2 1\n2 5\n4 3\n3 1\n4 2\n2 3\n3 5\n5 1", "output": "10" }, { "input": "5 8\n5 1\n1 3\n5 3\n2 3\n4 1\n4 3\n2 1\n5 4", "output": "-1" }, { "input": "5 10\n2 1\n5 2\n3 2\n3 1\n5 4\n3 4\n4 2\n5 1\n4 1\n5 3", "output": "10" }, { "input": "2 1\n2 1", "output": "1" }, { "input": "5 10\n4 3\n4 1\n2 1\n1 3\n5 1\n5 3\n5 4\n2 5\n2 3\n2 4", "output": "8" }, { "input": "5 10\n2 5\n1 5\n2 3\n1 4\n5 4\n3 4\n3 1\n2 1\n3 5\n2 4", "output": "7" }, { "input": "13 78\n10 2\n6 5\n5 7\n10 3\n6 13\n4 7\n4 13\n1 12\n13 2\n1 7\n10 5\n10 13\n12 2\n10 6\n5 3\n10 8\n9 11\n6 1\n4 5\n12 3\n6 7\n8 2\n1 9\n4 11\n5 8\n6 3\n9 5\n5 2\n13 11\n10 4\n12 7\n5 13\n12 8\n10 12\n6 12\n5 11\n1 8\n7 8\n12 4\n10 7\n1 5\n1 2\n10 11\n2 11\n12 13\n9 7\n8 3\n12 9\n9 2\n6 9\n10 9\n7 11\n12 5\n4 8\n9 8\n4 3\n6 4\n6 11\n6 8\n1 13\n8 11\n1 3\n9 13\n9 4\n1 4\n2 3\n4 2\n6 2\n7 13\n1 11\n12 11\n7 2\n8 13\n9 3\n13 3\n3 11\n10 1\n7 3", "output": "76" }, { "input": "5 10\n3 4\n3 2\n3 5\n1 4\n2 4\n5 1\n3 1\n5 4\n2 1\n2 5", "output": "10" }, { "input": "7 21\n5 7\n6 7\n2 7\n1 3\n6 2\n6 3\n2 3\n6 1\n4 2\n6 4\n4 5\n4 7\n6 5\n5 2\n1 7\n7 3\n4 1\n2 1\n5 1\n4 3\n5 3", "output": "18" } ]
1,403
42,905,600
3
26,028
407
k-d-sequence
[ "data structures" ]
null
null
We'll call a sequence of integers a good *k*-*d* sequence if we can add to it at most *k* numbers in such a way that after the sorting the sequence will be an arithmetic progression with difference *d*. You got hold of some sequence *a*, consisting of *n* integers. Your task is to find its longest contiguous subsegment, such that it is a good *k*-*d* sequence.
The first line contains three space-separated integers *n*,<=*k*,<=*d* (1<=≤<=*n*<=≤<=2·105; 0<=≤<=*k*<=≤<=2·105; 0<=≤<=*d*<=≤<=109). The second line contains *n* space-separated integers: *a*1,<=*a*2,<=...,<=*a**n* (<=-<=109<=≤<=*a**i*<=≤<=109) — the actual sequence.
Print two space-separated integers *l*,<=*r* (1<=≤<=*l*<=≤<=*r*<=≤<=*n*) show that sequence *a**l*,<=*a**l*<=+<=1,<=...,<=*a**r* is the longest subsegment that is a good *k*-*d* sequence. If there are multiple optimal answers, print the one with the minimum value of *l*.
[ "6 1 2\n4 3 2 8 6 2\n" ]
[ "3 5\n" ]
In the first test sample the answer is the subsegment consisting of numbers 2, 8, 6 — after adding number 4 and sorting it becomes sequence 2, 4, 6, 8 — the arithmetic progression with difference 2.
[]
15
0
-1
26,124
784
Touchy-Feely Palindromes
[ "*special", "implementation" ]
null
null
The only line of the input contains a string of digits. The length of the string is between 1 and 10, inclusive. Output "Yes" or "No".
The only line of the input contains a string of digits. The length of the string is between 1 and 10, inclusive.
Output "Yes" or "No".
[ "373\n", "121\n", "436\n" ]
[ "Yes\n", "No\n", "Yes\n" ]
none
[ { "input": "373", "output": "Yes" }, { "input": "121", "output": "No" }, { "input": "436", "output": "Yes" }, { "input": "7", "output": "Yes" }, { "input": "8", "output": "No" }, { "input": "4357087936", "output": "Yes" }, { "input": "806975480", "output": "Yes" }, { "input": "3333333333", "output": "Yes" }, { "input": "90785", "output": "Yes" }, { "input": "7467467", "output": "Yes" }, { "input": "64", "output": "Yes" }, { "input": "584609", "output": "Yes" }, { "input": "69154", "output": "No" }, { "input": "363567", "output": "No" }, { "input": "557654", "output": "No" }, { "input": "772961", "output": "No" }, { "input": "788958", "output": "No" }, { "input": "992045", "output": "No" }, { "input": "116325", "output": "No" }, { "input": "320432", "output": "No" }, { "input": "314729", "output": "No" }, { "input": "531816", "output": "No" }, { "input": "673902416", "output": "No" }, { "input": "880089713", "output": "No" }, { "input": "004176110", "output": "No" } ]
46
0
3
26,199
744
Hongcow's Game
[ "bitmasks", "divide and conquer", "interactive" ]
null
null
This is an interactive problem. In the interaction section below you will see the information about flushing the output. In this problem, you will be playing a game with Hongcow. How lucky of you! Hongcow has a hidden *n* by *n* matrix *M*. Let *M**i*,<=*j* denote the entry *i*-th row and *j*-th column of the matrix. The rows and columns are labeled from 1 to *n*. The matrix entries are between 0 and 109. In addition, *M**i*,<=*i*<==<=0 for all valid *i*. Your task is to find the minimum value along each row, excluding diagonal elements. Formally, for each *i*, you must find . To do this, you can ask Hongcow some questions. A question consists of giving Hongcow a subset of distinct indices {*w*1,<=*w*2,<=...,<=*w**k*}, with 1<=≤<=*k*<=≤<=*n*. Hongcow will respond with *n* integers. The *i*-th integer will contain the minimum value of *min*1<=≤<=*j*<=≤<=*k**M**i*,<=*w**j*. You may only ask Hongcow at most 20 questions — he thinks you only need that many questions answered. When you are ready to answer, print out a single integer <=-<=1 on its own line, then *n* integers on the next line. The *i*-th integer should be the minimum value in the *i*-th row of the matrix, excluding the *i*-th element. Do not forget to flush the final answer as well. Printing the answer does not count as asking a question. You will get Wrong Answer verdict if - Your question or answers are not in the format described in this statement. - You ask strictly more than 20 questions. - Your question contains duplicate indices. - The value of *k* in your question does not lie in the range from 1 to *n*, inclusive. - Your final answer is not correct.
The first line of input will contain a single integer *n* (2<=≤<=*n*<=≤<=1,<=000).
To print the final answer, print out the string -1 on its own line. Then, the next line should contain *n* integers. The *i*-th integer should be the minimum value of the *i*-th row of the matrix, excluding elements on the diagonal. Do not forget to flush your answer!
[ "3\n0 0 0\n2 7 0\n0 0 4\n3 0 8\n0 5 4", "2\n0 0\n0 0" ]
[ "3\n1 2 3\n1\n3\n2\n1 2\n1\n2\n1\n1\n-1\n2 5 4\n", "1\n2\n1\n1\n-1\n0 0" ]
In the first sample, Hongcow has the hidden matrix Here is a more readable version demonstrating the interaction. The column on the left represents Hongcow, while the column on the right represents the contestant. For the second sample, it is possible for off-diagonal elements of the matrix to be zero.
[]
62
614,400
3
26,339
185
Mushroom Scientists
[ "math", "ternary search" ]
null
null
As you very well know, the whole Universe traditionally uses three-dimensional Cartesian system of coordinates. In this system each point corresponds to three real coordinates (*x*,<=*y*,<=*z*). In this coordinate system, the distance between the center of the Universe and the point is calculated by the following formula: . Mushroom scientists that work for the Great Mushroom King think that the Universe isn't exactly right and the distance from the center of the Universe to a point equals *x**a*·*y**b*·*z**c*. To test the metric of mushroom scientists, the usual scientists offered them a task: find such *x*,<=*y*,<=*z* (0<=≤<=*x*,<=*y*,<=*z*; *x*<=+<=*y*<=+<=*z*<=≤<=*S*), that the distance between the center of the Universe and the point (*x*,<=*y*,<=*z*) is maximum possible in the metric of mushroom scientists. The mushroom scientists aren't good at maths, so they commissioned you to do the task. Note that in this problem, it is considered that 00<==<=1.
The first line contains a single integer *S* (1<=≤<=*S*<=≤<=103) — the maximum sum of coordinates of the sought point. The second line contains three space-separated integers *a*, *b*, *c* (0<=≤<=*a*,<=*b*,<=*c*<=≤<=103) — the numbers that describe the metric of mushroom scientists.
Print three real numbers — the coordinates of the point that reaches maximum value in the metrics of mushroom scientists. If there are multiple answers, print any of them that meets the limitations. A natural logarithm of distance from the center of the Universe to the given point in the metric of mushroom scientists shouldn't differ from the natural logarithm of the maximum distance by more than 10<=-<=6. We think that *ln*(0)<==<=<=-<=∞.
[ "3\n1 1 1\n", "3\n2 0 0\n" ]
[ "1.0 1.0 1.0\n", "3.0 0.0 0.0\n" ]
none
[ { "input": "3\n1 1 1", "output": "1.0 1.0 1.0" }, { "input": "3\n2 0 0", "output": "3.0 0.0 0.0" }, { "input": "10\n1 6 3", "output": "1.0 6.0 3.0" }, { "input": "9\n8 2 0", "output": "7.2 1.8 0.0" }, { "input": "1\n0 9 2", "output": "0.0 0.8181818181818182 0.18181818181818182" }, { "input": "1\n3 5 1", "output": "0.3333333333333333 0.5555555555555556 0.1111111111111111" }, { "input": "7\n8 2 2", "output": "4.666666666666667 1.1666666666666667 1.1666666666666667" }, { "input": "9\n3 7 0", "output": "2.7 6.3 0.0" }, { "input": "1000\n0 0 0", "output": "0 0 0" }, { "input": "624\n553 828 109", "output": "231.59194630872483 346.7597315436242 45.64832214765101" }, { "input": "902\n742 737 340", "output": "367.9406267179769 365.4612424409016 168.5981308411215" }, { "input": "239\n995 385 267", "output": "144.3867638129933 55.8682452944748 38.744990892531874" }, { "input": "797\n917 702 538", "output": "338.82661103384334 259.3852573018081 198.78813166434864" }, { "input": "938\n414 308 795", "output": "255.98681608437707 190.44429795649307 491.5688859591299" }, { "input": "422\n215 779 900", "output": "47.903907074973596 173.56810982048574 200.52798310454065" }, { "input": "413\n569 138 159", "output": "271.35912240184757 65.81293302540415 75.82794457274827" }, { "input": "188\n748 859 686", "output": "61.327518534670745 70.42825992150023 56.24422154382905" }, { "input": "48\n395 552 466", "output": "13.418259023354565 18.751592356687897 15.830148619957537" }, { "input": "492\n971 305 807", "output": "229.34805568891022 72.04032645223235 190.61161785885741" }, { "input": "557\n84 654 154", "output": "52.45291479820627 408.3834080717489 96.16367713004483" }, { "input": "699\n493 285 659", "output": "239.8100208768267 138.63256784968684 320.5574112734864" }, { "input": "814\n711 408 545", "output": "347.8088942307692 199.58653846153845 266.6045673076923" }, { "input": "706\n197 265 571", "output": "134.63891577928362 181.11326234269117 390.24782187802515" }, { "input": "945\n123 67 174", "output": "319.3269230769231 173.9423076923077 451.7307692307692" }, { "input": "724\n529 558 407", "output": "256.3560910307898 270.4096385542168 197.2342704149933" }, { "input": "269\n0 623 873", "output": "0.0 112.02339572192513 156.97660427807486" }, { "input": "173\n0 0 374", "output": "0.0 0.0 173.0" }, { "input": "972\n918 0 405", "output": "674.4489795918367 0.0 297.55102040816325" }, { "input": "809\n328 0 0", "output": "809.0 0.0 0.0" }, { "input": "413\n517 0 0", "output": "413.0 0.0 0.0" }, { "input": "642\n0 665 0", "output": "0.0 642.0 0.0" }, { "input": "1000\n117 403 270", "output": "148.10126582278482 510.12658227848107 341.7721518987342" }, { "input": "1000\n1000 1000 1000", "output": "333.3333333333333 333.3333333333333 333.3333333333333" }, { "input": "1\n1000 1000 1000", "output": "0.3333333333333333 0.3333333333333333 0.3333333333333333" }, { "input": "1000\n1 0 1", "output": "500.0 0.0 500.0" }, { "input": "1000\n0 1 1", "output": "0.0 500.0 500.0" }, { "input": "1000\n1 1 0", "output": "500.0 500.0 0.0" }, { "input": "1000\n0 0 1", "output": "0.0 0.0 1000.0" }, { "input": "1000\n1 0 0", "output": "1000.0 0.0 0.0" }, { "input": "1000\n0 1 0", "output": "0.0 1000.0 0.0" }, { "input": "1000\n999 1000 999", "output": "333.2221480987325 333.555703802535 333.2221480987325" }, { "input": "1000\n999 998 997", "output": "333.6673346693387 333.33333333333337 332.999331997328" }, { "input": "3\n0 0 0", "output": "0 0 0" }, { "input": "100\n0 0 0", "output": "0 0 0" }, { "input": "1\n0 0 0", "output": "0 0 0" }, { "input": "239\n0 0 0", "output": "0 0 0" }, { "input": "10\n0 0 0", "output": "0 0 0" }, { "input": "1000\n197 198 199", "output": "331.6498316498317 333.33333333333337 335.01683501683505" }, { "input": "5\n0 0 0", "output": "0 0 0" }, { "input": "1000\n0 0 0", "output": "0 0 0" }, { "input": "1000\n0 1 999", "output": "0.0 1.0 999.0" }, { "input": "1000\n1000 0 0", "output": "1000.0 0.0 0.0" } ]
92
0
0
26,350
847
Load Testing
[ "greedy" ]
null
null
Polycarp plans to conduct a load testing of its new project Fakebook. He already agreed with his friends that at certain points in time they will send requests to Fakebook. The load testing will last *n* minutes and in the *i*-th minute friends will send *a**i* requests. Polycarp plans to test Fakebook under a special kind of load. In case the information about Fakebook gets into the mass media, Polycarp hopes for a monotone increase of the load, followed by a monotone decrease of the interest to the service. Polycarp wants to test this form of load. Your task is to determine how many requests Polycarp must add so that before some moment the load on the server strictly increases and after that moment strictly decreases. Both the increasing part and the decreasing part can be empty (i. e. absent). The decrease should immediately follow the increase. In particular, the load with two equal neigbouring values is unacceptable. For example, if the load is described with one of the arrays [1, 2, 8, 4, 3], [1, 3, 5] or [10], then such load satisfies Polycarp (in each of the cases there is an increasing part, immediately followed with a decreasing part). If the load is described with one of the arrays [1, 2, 2, 1], [2, 1, 2] or [10, 10], then such load does not satisfy Polycarp. Help Polycarp to make the minimum number of additional requests, so that the resulting load satisfies Polycarp. He can make any number of additional requests at any minute from 1 to *n*.
The first line contains a single integer *n* (1<=≤<=*n*<=≤<=100<=000) — the duration of the load testing. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109), where *a**i* is the number of requests from friends in the *i*-th minute of the load testing.
Print the minimum number of additional requests from Polycarp that would make the load strictly increasing in the beginning and then strictly decreasing afterwards.
[ "5\n1 4 3 2 5\n", "5\n1 2 2 2 1\n", "7\n10 20 40 50 70 90 30\n" ]
[ "6\n", "1\n", "0\n" ]
In the first example Polycarp must make two additional requests in the third minute and four additional requests in the fourth minute. So the resulting load will look like: [1, 4, 5, 6, 5]. In total, Polycarp will make 6 additional requests. In the second example it is enough to make one additional request in the third minute, so the answer is 1. In the third example the load already satisfies all conditions described in the statement, so the answer is 0.
[ { "input": "5\n1 4 3 2 5", "output": "6" }, { "input": "5\n1 2 2 2 1", "output": "1" }, { "input": "7\n10 20 40 50 70 90 30", "output": "0" }, { "input": "1\n1", "output": "0" }, { "input": "2\n1 15", "output": "0" }, { "input": "4\n36 54 55 9", "output": "0" }, { "input": "5\n984181411 215198610 969039668 60631313 85746445", "output": "778956192" }, { "input": "10\n12528139 986722043 1595702 997595062 997565216 997677838 999394520 999593240 772077 998195916", "output": "1982580029" }, { "input": "100\n9997 9615 4045 2846 7656 2941 2233 9214 837 2369 5832 578 6146 8773 164 7303 3260 8684 2511 6608 9061 9224 7263 7279 1361 1823 8075 5946 2236 6529 6783 7494 510 1217 1135 8745 6517 182 8180 2675 6827 6091 2730 897 1254 471 1990 1806 1706 2571 8355 5542 5536 1527 886 2093 1532 4868 2348 7387 5218 3181 3140 3237 4084 9026 504 6460 9256 6305 8827 840 2315 5763 8263 5068 7316 9033 7552 9939 8659 6394 4566 3595 2947 2434 1790 2673 6291 6736 8549 4102 953 8396 8985 1053 5906 6579 5854 6805", "output": "478217" } ]
296
14,131,200
3
26,456
28
pSort
[ "dfs and similar", "dsu", "graphs" ]
B. pSort
2
256
One day *n* cells of some array decided to play the following game. Initially each cell contains a number which is equal to it's ordinal number (starting from 1). Also each cell determined it's favourite number. On it's move *i*-th cell can exchange it's value with the value of some other *j*-th cell, if |*i*<=-<=*j*|<==<=*d**i*, where *d**i* is a favourite number of *i*-th cell. Cells make moves in any order, the number of moves is unlimited. The favourite number of each cell will be given to you. You will also be given a permutation of numbers from 1 to *n*. You are to determine whether the game could move to this state.
The first line contains positive integer *n* (1<=≤<=*n*<=≤<=100) — the number of cells in the array. The second line contains *n* distinct integers from 1 to *n* — permutation. The last line contains *n* integers from 1 to *n* — favourite numbers of the cells.
If the given state is reachable in the described game, output YES, otherwise NO.
[ "5\n5 4 3 2 1\n1 1 1 1 1\n", "7\n4 3 5 1 2 7 6\n4 6 6 1 6 6 1\n", "7\n4 2 5 1 3 7 6\n4 6 6 1 6 6 1\n" ]
[ "YES\n", "NO\n", "YES\n" ]
none
[ { "input": "5\n5 4 3 2 1\n1 1 1 1 1", "output": "YES" }, { "input": "7\n4 3 5 1 2 7 6\n4 6 6 1 6 6 1", "output": "NO" }, { "input": "7\n4 2 5 1 3 7 6\n4 6 6 1 6 6 1", "output": "YES" }, { "input": "6\n3 2 1 4 6 5\n3 6 1 6 6 1", "output": "YES" }, { "input": "6\n3 5 1 4 6 2\n3 6 1 6 6 1", "output": "NO" }, { "input": "4\n1 2 3 4\n1 1 1 1", "output": "YES" }, { "input": "71\n1 63 3 4 5 6 7 8 9 44 29 12 13 14 55 34 42 18 52 20 21 33 23 24 25 26 27 28 19 30 47 32 15 71 37 36 69 38 39 40 41 17 43 10 45 58 35 48 49 11 51 50 53 54 22 56 57 46 59 60 61 62 2 64 65 66 67 68 31 70 16\n21 45 8 62 56 53 25 22 65 34 39 43 30 63 18 18 25 10 31 64 70 33 49 70 34 21 69 25 21 4 38 41 4 36 4 28 6 48 6 25 57 11 62 48 62 69 12 14 12 70 41 2 49 30 40 37 67 12 13 22 50 35 61 42 33 30 10 47 10 65 37", "output": "YES" }, { "input": "76\n44 47 38 4 31 6 33 8 50 69 35 26 73 14 74 16 41 9 59 75 46 7 23 52 58 10 17 49 29 64 42 19 12 36 65 34 3 37 39 1 30 76 27 2 22 55 61 48 24 5 54 11 51 28 68 18 57 60 56 71 63 25 15 66 62 32 67 53 43 70 45 72 13 40 20 21\n54 63 9 55 65 59 4 1 42 31 63 5 28 26 19 35 50 21 39 23 40 64 45 36 74 74 70 67 53 20 19 27 13 2 71 62 6 51 2 65 61 58 55 55 32 75 54 75 48 45 40 45 7 43 72 75 54 9 32 52 8 36 33 59 57 58 68 32 71 21 63 69 7 40 28 68", "output": "NO" }, { "input": "82\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 59 17 18 19 20 21 22 23 24 25 26 27 28 29 61 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 16 60 30 79 63 64 65 66 67 68 69 70 71 72 73 74 75 76 82 78 62 80 81 77\n8 79 66 53 45 15 70 17 53 28 30 70 14 49 51 43 45 14 27 30 37 51 29 55 6 5 24 6 61 31 64 47 71 60 79 64 28 64 17 38 11 13 3 72 24 70 16 14 30 72 52 33 31 71 32 66 29 45 55 32 48 14 63 60 50 50 61 2 47 26 4 26 72 3 56 19 1 47 17 26 66 5", "output": "YES" }, { "input": "13\n13 1 12 4 6 10 7 8 9 3 11 5 2\n6 12 12 7 11 7 10 3 1 5 2 2 1", "output": "NO" }, { "input": "5\n1 3 2 4 5\n1 4 1 2 4", "output": "YES" }, { "input": "10\n6 2 9 4 8 7 5 10 3 1\n2 9 7 9 4 5 1 5 3 2", "output": "YES" }, { "input": "68\n1 2 3 46 5 6 7 8 9 63 11 57 13 14 15 16 40 18 19 20 21 22 23 24 25 26 27 28 29 35 31 32 33 34 30 36 37 64 39 17 41 12 43 52 58 4 47 44 49 50 51 48 53 54 55 56 42 59 45 60 61 62 10 38 65 66 67 68\n13 48 67 55 4 39 47 8 4 35 50 28 28 30 63 60 52 29 2 33 48 57 40 43 25 34 62 50 60 5 3 66 32 15 7 51 51 26 47 23 67 30 27 53 40 42 5 4 60 67 11 4 31 10 62 46 45 13 14 13 24 66 53 25 22 60 14 42", "output": "YES" }, { "input": "52\n17 35 19 41 21 51 46 45 13 10 15 43 37 30 34 12 39 20 14 48 49 3 23 6 4 26 47 18 16 5 31 36 27 29 24 11 52 38 33 42 1 8 9 32 44 7 28 22 40 50 2 25\n47 6 50 13 49 22 17 18 3 11 2 43 35 8 25 38 19 41 17 5 7 8 10 51 17 30 34 48 41 8 46 10 11 45 15 28 42 32 37 33 43 31 38 13 43 19 32 19 2 47 42 46", "output": "NO" }, { "input": "50\n5 2 4 31 1 10 44 24 9 38 20 27 35 14 37 46 8 18 41 34 7 22 25 45 32 43 6 47 39 15 26 48 30 23 16 36 17 21 50 40 3 11 12 19 42 29 28 49 33 13\n27 15 18 20 19 23 49 18 28 32 29 2 16 23 23 2 17 25 27 43 32 31 11 3 49 46 22 44 14 48 35 15 32 35 2 49 10 22 5 36 49 16 43 46 33 11 31 15 45 23", "output": "NO" }, { "input": "60\n1 2 17 4 27 6 43 26 32 10 11 12 29 14 15 16 3 44 56 20 21 22 53 24 25 8 5 28 58 7 31 9 33 57 48 36 37 38 39 30 41 50 40 35 45 46 13 18 47 55 51 52 23 54 42 19 34 49 59 60\n59 25 14 52 1 24 36 16 42 8 59 22 41 47 49 33 36 35 37 30 25 6 30 8 50 2 25 3 29 10 52 20 8 45 13 43 53 45 10 33 54 53 47 26 32 4 2 38 33 45 19 4 56 22 13 40 45 45 24 14", "output": "YES" }, { "input": "79\n1 2 3 67 5 6 7 60 9 71 63 12 31 57 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 64 32 79 34 35 45 37 38 39 40 41 42 43 44 36 46 47 48 11 50 51 52 74 54 55 56 14 58 59 8 61 62 49 13 65 66 4 68 72 70 10 69 73 53 75 76 77 78 33\n17 46 68 35 48 11 78 40 65 61 52 45 51 66 13 56 53 63 61 43 22 60 13 67 34 16 64 19 11 27 33 8 9 1 68 9 17 62 65 23 50 1 55 20 70 61 65 55 38 47 9 45 55 70 39 31 43 47 40 52 20 5 20 75 25 25 63 2 36 12 60 3 35 21 78 38 39 25 46", "output": "YES" }, { "input": "80\n39 2 33 16 36 27 65 62 40 17 44 6 13 10 43 31 66 64 63 20 59 72 9 24 12 29 77 47 71 79 50 32 55 4 35 60 7 69 14 54 3 42 15 11 75 22 28 30 49 18 46 56 51 68 5 38 25 58 73 26 61 21 37 80 19 45 53 1 70 67 23 52 41 74 34 76 57 8 48 78\n6 23 42 42 13 72 14 45 66 76 74 44 49 10 14 64 17 15 4 68 14 34 42 56 50 65 17 52 15 26 1 42 27 22 6 52 25 47 76 45 48 67 18 44 74 48 62 58 59 79 13 5 12 14 5 13 51 21 57 59 49 43 8 34 7 16 34 29 38 74 40 72 18 46 47 43 2 4 17 1", "output": "NO" }, { "input": "8\n5 2 3 4 8 6 1 7\n6 7 7 7 7 7 2 7", "output": "YES" }, { "input": "17\n1 11 3 4 5 6 7 8 9 10 2 12 13 14 15 16 17\n13 9 16 5 16 12 11 4 4 7 12 16 2 7 14 6 3", "output": "YES" }, { "input": "19\n7 2 17 18 4 16 1 9 12 10 8 11 6 13 14 19 3 5 15\n12 4 9 1 13 18 14 10 18 2 17 16 12 3 16 6 5 7 7", "output": "NO" }, { "input": "47\n1 32 29 9 47 6 8 36 37 10 11 16 2 14 38 40 15 44 19 35 18 22 23 12 17 41 5 31 26 25 4 27 33 34 42 7 24 28 45 20 46 13 43 30 21 3 39\n38 28 41 46 28 20 36 9 4 10 44 28 9 39 12 36 32 38 43 3 13 33 34 35 22 23 1 4 39 2 11 34 20 19 25 13 20 26 45 36 36 43 45 13 31 9 5", "output": "NO" }, { "input": "1\n1\n1", "output": "YES" }, { "input": "2\n1 2\n2 2", "output": "YES" }, { "input": "2\n1 2\n1 1", "output": "YES" }, { "input": "2\n2 1\n2 2", "output": "NO" }, { "input": "2\n2 1\n1 1", "output": "YES" } ]
280
20,172,800
3.892425
26,463
474
Ant colony
[ "data structures", "math", "number theory" ]
null
null
Mole is hungry again. He found one ant colony, consisting of *n* ants, ordered in a row. Each ant *i* (1<=≤<=*i*<=≤<=*n*) has a strength *s**i*. In order to make his dinner more interesting, Mole organizes a version of «Hunger Games» for the ants. He chooses two numbers *l* and *r* (1<=≤<=*l*<=≤<=*r*<=≤<=*n*) and each pair of ants with indices between *l* and *r* (inclusively) will fight. When two ants *i* and *j* fight, ant *i* gets one battle point only if *s**i* divides *s**j* (also, ant *j* gets one battle point only if *s**j* divides *s**i*). After all fights have been finished, Mole makes the ranking. An ant *i*, with *v**i* battle points obtained, is going to be freed only if *v**i*<==<=*r*<=-<=*l*, or in other words only if it took a point in every fight it participated. After that, Mole eats the rest of the ants. Note that there can be many ants freed or even none. In order to choose the best sequence, Mole gives you *t* segments [*l**i*,<=*r**i*] and asks for each of them how many ants is he going to eat if those ants fight.
The first line contains one integer *n* (1<=≤<=*n*<=≤<=105), the size of the ant colony. The second line contains *n* integers *s*1,<=*s*2,<=...,<=*s**n* (1<=≤<=*s**i*<=≤<=109), the strengths of the ants. The third line contains one integer *t* (1<=≤<=*t*<=≤<=105), the number of test cases. Each of the next *t* lines contains two integers *l**i* and *r**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*), describing one query.
Print to the standard output *t* lines. The *i*-th line contains number of ants that Mole eats from the segment [*l**i*,<=*r**i*].
[ "5\n1 3 2 4 2\n4\n1 5\n2 5\n3 5\n4 5\n" ]
[ "4\n4\n1\n1\n" ]
In the first test battle points for each ant are *v* = [4, 0, 2, 0, 2], so ant number 1 is freed. Mole eats the ants 2, 3, 4, 5. In the second test case battle points are *v* = [0, 2, 0, 2], so no ant is freed and all of them are eaten by Mole. In the third test case battle points are *v* = [2, 0, 2], so ants number 3 and 5 are freed. Mole eats only the ant 4. In the fourth test case battle points are *v* = [0, 1], so ant number 5 is freed. Mole eats the ant 4.
[ { "input": "5\n1 3 2 4 2\n4\n1 5\n2 5\n3 5\n4 5", "output": "4\n4\n1\n1" } ]
31
0
0
26,482
161
Discounts
[ "constructive algorithms", "greedy", "sortings" ]
null
null
One day Polycarpus stopped by a supermarket on his way home. It turns out that the supermarket is having a special offer for stools. The offer is as follows: if a customer's shopping cart contains at least one stool, the customer gets a 50% discount on the cheapest item in the cart (that is, it becomes two times cheaper). If there are several items with the same minimum price, the discount is available for only one of them! Polycarpus has *k* carts, and he wants to buy up all stools and pencils from the supermarket. Help him distribute the stools and the pencils among the shopping carts, so that the items' total price (including the discounts) is the least possible. Polycarpus must use all *k* carts to purchase the items, no shopping cart can remain empty. Each shopping cart can contain an arbitrary number of stools and/or pencils.
The first input line contains two integers *n* and *k* (1<=≤<=*k*<=≤<=*n*<=≤<=103) — the number of items in the supermarket and the number of carts, correspondingly. Next *n* lines describe the items as "*c**i* *t**i*" (without the quotes), where *c**i* (1<=≤<=*c**i*<=≤<=109) is an integer denoting the price of the *i*-th item, *t**i* (1<=≤<=*t**i*<=≤<=2) is an integer representing the type of item *i* (1 for a stool and 2 for a pencil). The numbers in the lines are separated by single spaces.
In the first line print a single real number with exactly one decimal place — the minimum total price of the items, including the discounts. In the following *k* lines print the descriptions of the items in the carts. In the *i*-th line print the description of the *i*-th cart as "*t* *b*1 *b*2 ... *b**t*" (without the quotes), where *t* is the number of items in the *i*-th cart, and the sequence *b*1,<=*b*2,<=...,<=*b**t* (1<=≤<=*b**j*<=≤<=*n*) gives the indices of items to put in this cart in the optimal distribution. All indices of items in all carts should be pairwise different, each item must belong to exactly one cart. You can print the items in carts and the carts themselves in any order. The items are numbered from 1 to *n* in the order in which they are specified in the input. If there are multiple optimal distributions, you are allowed to print any of them.
[ "3 2\n2 1\n3 2\n3 1\n", "4 3\n4 1\n1 2\n2 2\n3 2\n" ]
[ "5.5\n2 1 2\n1 3\n", "8.0\n1 1\n2 4 2\n1 3\n" ]
In the first sample case the first cart should contain the 1st and 2nd items, and the second cart should contain the 3rd item. This way each cart has a stool and each cart has a 50% discount for the cheapest item. The total price of all items will be: 2·0.5 + (3 + 3·0.5) = 1 + 4.5 = 5.5.
[ { "input": "3 2\n2 1\n3 2\n3 1", "output": "5.5\n2 1 2\n1 3" }, { "input": "4 3\n4 1\n1 2\n2 2\n3 2", "output": "8.0\n1 1\n1 2\n2 3 4" }, { "input": "1 1\n1 1", "output": "0.5\n1 1" }, { "input": "1 1\n1 2", "output": "1.0\n1 1" }, { "input": "10 1\n1 1\n2 2\n1 1\n23 2\n17 2\n1 1\n1 1\n30 2\n1 1\n9 2", "output": "85.5\n10 1 2 4 5 8 10 3 6 7 9" }, { "input": "11 11\n6 2\n6 2\n1 2\n2 2\n3 1\n6 2\n1 1\n1 1\n3 1\n3 1\n6 2", "output": "32.5\n1 5\n1 9\n1 10\n1 7\n1 8\n1 1\n1 2\n1 3\n1 4\n1 6\n1 11" }, { "input": "5 4\n24 1\n19 1\n28 2\n7 1\n23 2", "output": "76.0\n1 1\n1 2\n1 4\n2 3 5" }, { "input": "20 3\n28 1\n786180179 2\n16 1\n617105650 2\n23 1\n21 1\n22 1\n7 1\n314215182 2\n409797301 2\n14 1\n993310357 2\n372545570 2\n791297014 2\n13 1\n25 1\n307921408 2\n625842662 2\n136238241 2\n13 1", "output": "5354453716.0\n18 5 2 4 9 10 12 13 14 17 18 19 7 6 3 11 15 20 8\n1 1\n1 16" }, { "input": "21 7\n14 1\n882797755 2\n17 1\n906492329 2\n209923513 2\n802927469 2\n949195463 2\n677323647 2\n2129083 2\n2 1\n13 1\n539523264 2\n7 1\n8 1\n12 1\n363470241 2\n9838294 2\n18716193 2\n30 1\n17 1\n24 1", "output": "5362337336.5\n15 15 2 4 5 6 7 8 9 12 16 17 18 14 13 10\n1 19\n1 21\n1 3\n1 20\n1 1\n1 11" }, { "input": "21 21\n42856481 2\n562905883 2\n942536731 2\n206667673 2\n451074408 2\n27 1\n29 1\n172761267 2\n23 1\n24 1\n106235116 2\n126463249 2\n29 1\n9 1\n83859496 2\n5 1\n25 1\n337838080 2\n109402491 2\n5 1\n24 1", "output": "3142600975.0\n1 7\n1 13\n1 6\n1 17\n1 10\n1 21\n1 9\n1 14\n1 16\n1 20\n1 1\n1 2\n1 3\n1 4\n1 5\n1 8\n1 11\n1 12\n1 15\n1 18\n1 19" }, { "input": "10 1\n28 1\n1 2\n1 2\n1 2\n15 1\n16 1\n22 1\n20 1\n1 2\n1 2", "output": "105.5\n10 1 2 3 4 9 10 7 8 6 5" }, { "input": "5 4\n10 1\n10 1\n10 1\n9 1\n5 2", "output": "26.5\n2 4 5\n1 1\n1 2\n1 3" }, { "input": "7 4\n10 1\n10 1\n10 1\n9 1\n2 1\n5 2\n3 2", "output": "33.0\n4 4 6 7 5\n1 1\n1 2\n1 3" }, { "input": "7 4\n10 1\n10 1\n10 1\n9 1\n4 1\n5 2\n3 2", "output": "34.5\n4 4 6 7 5\n1 1\n1 2\n1 3" }, { "input": "7 5\n10 1\n10 1\n10 1\n9 1\n4 1\n5 2\n3 2", "output": "30.0\n3 5 6 7\n1 1\n1 2\n1 3\n1 4" } ]
280
20,172,800
0
26,528
705
Spider Man
[ "games", "math" ]
null
null
Peter Parker wants to play a game with Dr. Octopus. The game is about cycles. Cycle is a sequence of vertices, such that first one is connected with the second, second is connected with third and so on, while the last one is connected with the first one again. Cycle may consist of a single isolated vertex. Initially there are *k* cycles, *i*-th of them consisting of exactly *v**i* vertices. Players play alternatively. Peter goes first. On each turn a player must choose a cycle with at least 2 vertices (for example, *x* vertices) among all available cycles and replace it by two cycles with *p* and *x*<=-<=*p* vertices where 1<=≤<=*p*<=&lt;<=*x* is chosen by the player. The player who cannot make a move loses the game (and his life!). Peter wants to test some configurations of initial cycle sets before he actually plays with Dr. Octopus. Initially he has an empty set. In the *i*-th test he adds a cycle with *a**i* vertices to the set (this is actually a multiset because it can contain two or more identical cycles). After each test, Peter wants to know that if the players begin the game with the current set of cycles, who wins? Peter is pretty good at math, but now he asks you to help.
The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=100<=000) — the number of tests Peter is about to make. The second line contains *n* space separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109), *i*-th of them stands for the number of vertices in the cycle added before the *i*-th test.
Print the result of all tests in order they are performed. Print 1 if the player who moves first wins or 2 otherwise.
[ "3\n1 2 3\n", "5\n1 1 5 1 1\n" ]
[ "2\n1\n1\n", "2\n2\n2\n2\n2\n" ]
In the first sample test: In Peter's first test, there's only one cycle with 1 vertex. First player cannot make a move and loses. In his second test, there's one cycle with 1 vertex and one with 2. No one can make a move on the cycle with 1 vertex. First player can replace the second cycle with two cycles of 1 vertex and second player can't make any move and loses. In his third test, cycles have 1, 2 and 3 vertices. Like last test, no one can make a move on the first cycle. First player can replace the third cycle with one cycle with size 1 and one with size 2. Now cycles have 1, 1, 2, 2 vertices. Second player's only move is to replace a cycle of size 2 with 2 cycles of size 1. And cycles are 1, 1, 1, 1, 2. First player replaces the last cycle with 2 cycles with size 1 and wins. In the second sample test: Having cycles of size 1 is like not having them (because no one can make a move on them). In Peter's third test: There a cycle of size 5 (others don't matter). First player has two options: replace it with cycles of sizes 1 and 4 or 2 and 3. - If he replaces it with cycles of sizes 1 and 4: Only second cycle matters. Second player will replace it with 2 cycles of sizes 2. First player's only option to replace one of them with two cycles of size 1. Second player does the same thing with the other cycle. First player can't make any move and loses. - If he replaces it with cycles of sizes 2 and 3: Second player will replace the cycle of size 3 with two of sizes 1 and 2. Now only cycles with more than one vertex are two cycles of size 2. As shown in previous case, with 2 cycles of size 2 second player wins. So, either way first player loses.
[ { "input": "3\n1 2 3", "output": "2\n1\n1" }, { "input": "5\n1 1 5 1 1", "output": "2\n2\n2\n2\n2" }, { "input": "1\n167959139", "output": "2" }, { "input": "10\n802030518 598196518 640274071 983359971 71550121 96204862 799843967 446173607 796619138 402690754", "output": "1\n2\n2\n2\n2\n1\n1\n1\n2\n1" }, { "input": "1\n1", "output": "2" }, { "input": "1\n1000000000", "output": "1" }, { "input": "2\n2 4", "output": "1\n2" }, { "input": "2\n1 1", "output": "2\n2" }, { "input": "2\n1 2", "output": "2\n1" }, { "input": "2\n2 1", "output": "1\n1" }, { "input": "3\n1 3 1", "output": "2\n2\n2" }, { "input": "3\n1 2 1", "output": "2\n1\n1" }, { "input": "3\n2 1 1", "output": "1\n1\n1" }, { "input": "3\n1 1 2", "output": "2\n2\n1" }, { "input": "10\n9 8 5 4 1 1 2 1 1 1", "output": "2\n1\n1\n2\n2\n2\n1\n1\n1\n1" }, { "input": "1\n2", "output": "1" }, { "input": "2\n3 3", "output": "2\n2" }, { "input": "5\n2 2 2 1 1", "output": "1\n2\n1\n1\n1" }, { "input": "5\n1 1 1 2 1", "output": "2\n2\n2\n1\n1" }, { "input": "5\n2 1 1 1 1", "output": "1\n1\n1\n1\n1" }, { "input": "4\n1 2 1 1", "output": "2\n1\n1\n1" }, { "input": "5\n5 4 4 4 1", "output": "2\n1\n2\n1\n1" }, { "input": "2\n3 1", "output": "2\n2" }, { "input": "3\n3 2 1", "output": "2\n1\n1" }, { "input": "5\n1 1 4 1 1", "output": "2\n2\n1\n1\n1" } ]
139
14,643,200
3
26,582
720
Cactusophobia
[ "dfs and similar", "flows" ]
null
null
Tree is a connected undirected graph that has no cycles. Edge cactus is a connected undirected graph without loops and parallel edges, such that each edge belongs to at most one cycle. Vasya has an edge cactus, each edge of this graph has some color. Vasya would like to remove the minimal number of edges in such way that his cactus turned to a tree. Vasya wants to make it in such a way that there were edges of as many different colors in the resulting tree, as possible. Help him to find how many different colors can the resulting tree have.
The first line contains two integers: *n*, *m* (2<=≤<=*n*<=≤<=10<=000) — the number of vertices and the number of edges in Vasya's graph, respectively. The following *m* lines contain three integers each: *u*, *v*, *c* (1<=≤<=*u*,<=*v*<=≤<=*n*, *u*<=≠<=*v*, 1<=≤<=*c*<=≤<=*m*) — the numbers of vertices connected by the corresponding edge, and its color. It is guaranteed that the described graph is indeed an edge cactus.
Output one integer: the maximal number of different colors that the resulting tree can have.
[ "4 4\n1 2 4\n2 3 1\n3 4 2\n4 2 3\n", "7 9\n1 2 1\n2 3 4\n3 1 5\n1 4 5\n4 5 2\n5 1 6\n1 6 4\n6 7 6\n7 1 3\n" ]
[ "3\n", "6\n" ]
none
[ { "input": "4 4\n1 2 4\n2 3 1\n3 4 2\n4 2 3", "output": "3" }, { "input": "7 9\n1 2 1\n2 3 4\n3 1 5\n1 4 5\n4 5 2\n5 1 6\n1 6 4\n6 7 6\n7 1 3", "output": "6" }, { "input": "5 6\n4 1 2\n4 2 3\n5 1 5\n1 3 1\n1 2 1\n5 3 4", "output": "4" }, { "input": "9 10\n8 7 2\n8 2 10\n8 4 2\n6 3 9\n9 1 1\n7 5 5\n9 4 8\n3 5 3\n1 2 7\n6 8 6", "output": "8" }, { "input": "20 28\n20 4 23\n12 13 16\n20 14 19\n8 20 24\n15 20 8\n12 20 5\n20 16 23\n10 19 28\n20 2 16\n15 1 20\n13 20 4\n20 9 11\n16 6 4\n6 20 26\n5 9 3\n20 5 20\n20 18 10\n20 1 28\n20 11 8\n19 20 18\n8 4 19\n20 10 23\n2 3 18\n3 20 3\n18 17 18\n11 7 14\n7 20 17\n20 17 12", "output": "17" } ]
46
0
0
26,605
750
New Year and Fireworks
[ "brute force", "data structures", "dfs and similar", "dp", "implementation" ]
null
null
One tradition of welcoming the New Year is launching fireworks into the sky. Usually a launched firework flies vertically upward for some period of time, then explodes, splitting into several parts flying in different directions. Sometimes those parts also explode after some period of time, splitting into even more parts, and so on. Limak, who lives in an infinite grid, has a single firework. The behaviour of the firework is described with a recursion depth *n* and a duration for each level of recursion *t*1,<=*t*2,<=...,<=*t**n*. Once Limak launches the firework in some cell, the firework starts moving upward. After covering *t*1 cells (including the starting cell), it explodes and splits into two parts, each moving in the direction changed by 45 degrees (see the pictures below for clarification). So, one part moves in the top-left direction, while the other one moves in the top-right direction. Each part explodes again after covering *t*2 cells, splitting into two parts moving in directions again changed by 45 degrees. The process continues till the *n*-th level of recursion, when all 2*n*<=-<=1 existing parts explode and disappear without creating new parts. After a few levels of recursion, it's possible that some parts will be at the same place and at the same time — it is allowed and such parts do not crash. Before launching the firework, Limak must make sure that nobody stands in cells which will be visited at least once by the firework. Can you count the number of those cells?
The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=30) — the total depth of the recursion. The second line contains *n* integers *t*1,<=*t*2,<=...,<=*t**n* (1<=≤<=*t**i*<=≤<=5). On the *i*-th level each of 2*i*<=-<=1 parts will cover *t**i* cells before exploding.
Print one integer, denoting the number of cells which will be visited at least once by any part of the firework.
[ "4\n4 2 2 3\n", "6\n1 1 1 1 1 3\n", "1\n3\n" ]
[ "39\n", "85\n", "3\n" ]
For the first sample, the drawings below show the situation after each level of recursion. Limak launched the firework from the bottom-most red cell. It covered *t*<sub class="lower-index">1</sub> = 4 cells (marked red), exploded and divided into two parts (their further movement is marked green). All explosions are marked with an 'X' character. On the last drawing, there are 4 red, 4 green, 8 orange and 23 pink cells. So, the total number of visited cells is 4 + 4 + 8 + 23 = 39. For the second sample, the drawings below show the situation after levels 4, 5 and 6. The middle drawing shows directions of all parts that will move in the next level.
[]
1,746
268,390,400
0
26,668
282
XOR and OR
[ "constructive algorithms", "implementation", "math" ]
null
null
The Bitlandians are quite weird people. They do everything differently. They have a different alphabet so they have a different definition for a string. A Bitlandish string is a string made only of characters "0" and "1". BitHaval (the mayor of Bitland) loves to play with Bitlandish strings. He takes some Bitlandish string *a*, and applies several (possibly zero) operations to it. In one operation the mayor may take any two adjacent characters of a string, define one of them as *x* and the other one as *y*. Then he calculates two values *p* and *q*: *p*<==<=*x* *xor* *y*, *q*<==<=*x* *or* *y*. Then he replaces one of the two taken characters by *p* and the other one by *q*. The *xor* operation means the bitwise excluding OR operation. The *or* operation is the bitwise OR operation. So for example one operation can transform string 11 to string 10 or to string 01. String 1 cannot be transformed into any other string. You've got two Bitlandish strings *a* and *b*. Your task is to check if it is possible for BitHaval to transform string *a* to string *b* in several (possibly zero) described operations.
The first line contains Bitlandish string *a*, the second line contains Bitlandish string *b*. The strings can have different lengths. It is guaranteed that the given strings only consist of characters "0" and "1". The strings are not empty, their length doesn't exceed 106.
Print "YES" if *a* can be transformed into *b*, otherwise print "NO". Please do not print the quotes.
[ "11\n10\n", "1\n01\n", "000\n101\n" ]
[ "YES\n", "NO\n", "NO\n" ]
none
[ { "input": "11\n10", "output": "YES" }, { "input": "1\n01", "output": "NO" }, { "input": "000\n101", "output": "NO" }, { "input": "1101\n1111", "output": "YES" }, { "input": "11000001\n00000001", "output": "YES" }, { "input": "01\n10", "output": "YES" }, { "input": "0000\n1110", "output": "NO" }, { "input": "010101\n101010", "output": "YES" }, { "input": "0\n1", "output": "NO" }, { "input": "1\n1", "output": "YES" }, { "input": "0\n0", "output": "YES" }, { "input": "1\n0", "output": "NO" }, { "input": "100\n11111", "output": "NO" }, { "input": "1000000000000000000000000000000000000011111111000000000000\n0111111111100000000000111111111000000000000000111111110001", "output": "YES" }, { "input": "0000000000000000000000000000000000000000\n00000000000000000000000000000000000000000", "output": "NO" }, { "input": "1\n1", "output": "YES" }, { "input": "0\n0", "output": "YES" }, { "input": "1\n0", "output": "NO" }, { "input": "10\n00", "output": "NO" }, { "input": "11\n00", "output": "NO" }, { "input": "111111111\n100000000", "output": "YES" } ]
62
0
0
26,716
42
Guilty --- to the kitchen!
[ "greedy", "implementation" ]
A. Guilty — to the kitchen!
2
256
It's a very unfortunate day for Volodya today. He got bad mark in algebra and was therefore forced to do some work in the kitchen, namely to cook borscht (traditional Russian soup). This should also improve his algebra skills. According to the borscht recipe it consists of *n* ingredients that have to be mixed in proportion litres (thus, there should be *a*1<=·*x*,<=...,<=*a**n*<=·*x* litres of corresponding ingredients mixed for some non-negative *x*). In the kitchen Volodya found out that he has *b*1,<=...,<=*b**n* litres of these ingredients at his disposal correspondingly. In order to correct his algebra mistakes he ought to cook as much soup as possible in a *V* litres volume pan (which means the amount of soup cooked can be between 0 and *V* litres). What is the volume of borscht Volodya will cook ultimately?
The first line of the input contains two space-separated integers *n* and *V* (1<=≤<=*n*<=≤<=20,<=1<=≤<=*V*<=≤<=10000). The next line contains *n* space-separated integers *a**i* (1<=≤<=*a**i*<=≤<=100). Finally, the last line contains *n* space-separated integers *b**i* (0<=≤<=*b**i*<=≤<=100).
Your program should output just one real number — the volume of soup that Volodya will cook. Your answer must have a relative or absolute error less than 10<=-<=4.
[ "1 100\n1\n40\n", "2 100\n1 1\n25 30\n", "2 100\n1 1\n60 60\n" ]
[ "40.0\n", "50.0\n", "100.0\n" ]
none
[ { "input": "1 100\n1\n40", "output": "40.0" }, { "input": "2 100\n1 1\n25 30", "output": "50.0" }, { "input": "2 100\n1 1\n60 60", "output": "100.0" }, { "input": "2 100\n1 1\n50 50", "output": "100.0" }, { "input": "2 100\n1 2\n33 66", "output": "99.0" }, { "input": "3 10000\n1 1 1\n100 0 100", "output": "0.0" }, { "input": "7 5100\n21 93 52 80 5 46 20\n79 37 74 54 22 15 90", "output": "103.3695652173913" }, { "input": "10 2707\n80 91 41 99 99 48 81 25 80 17\n88 79 64 78 4 54 38 92 77 61", "output": "26.70707070707071" }, { "input": "19 8111\n44 75 80 69 90 64 58 8 93 50 44 39 7 25 14 52 32 26 26\n38 57 38 23 73 24 4 49 0 34 96 93 14 26 29 89 54 12 24", "output": "0.0" }, { "input": "5 1121\n14 37 91 35 71\n17 87 48 91 13", "output": "45.40845070422535" }, { "input": "4 6054\n72 21 14 49\n43 53 42 55", "output": "93.16666666666666" }, { "input": "6 8692\n20 61 56 4 78 76\n73 83 97 45 16 7", "output": "27.171052631578945" }, { "input": "9 5583\n73 31 18 36 38 99 34 50 69\n48 24 75 78 75 69 13 74 3", "output": "19.478260869565215" }, { "input": "1 5215\n24\n85", "output": "85.0" }, { "input": "15 9559\n55 13 69 16 15 34 89 30 56 64 74 100 72 71 20\n40 73 29 12 31 5 59 5 90 13 32 75 99 7 44", "output": "76.70422535211266" }, { "input": "13 2530\n83 59 19 69 8 81 99 74 14 75 61 13 36\n26 36 77 44 10 8 8 16 81 61 29 81 50", "output": "55.83838383838385" }, { "input": "4 7672\n42 34 57 72\n56 7 24 24", "output": "42.205882352941174" }, { "input": "17 6030\n100 77 5 87 28 50 51 64 45 79 60 80 49 20 25 91 64\n12 13 58 55 3 59 8 62 69 38 69 27 50 39 5 41 30", "output": "104.46428571428571" }, { "input": "18 4842\n73 20 36 89 89 74 88 46 21 55 40 99 86 2 53 92 36 6\n24 97 23 27 31 63 29 2 23 84 86 44 68 8 63 0 50 16", "output": "0.0" }, { "input": "8 2342\n7 91 9 17 86 22 49 53\n20 76 25 24 54 78 33 90", "output": "209.72093023255815" }, { "input": "1 8987\n16\n38", "output": "38.0" }, { "input": "10 9501\n39 67 33 71 89 69 5 90 7 48\n89 91 8 68 7 54 61 66 53 51", "output": "40.74157303370786" }, { "input": "1 1966\n85\n99", "output": "99.0" }, { "input": "9 7611\n58 46 28 18 29 70 62 22 55\n53 43 51 72 52 99 18 61 91", "output": "112.64516129032259" }, { "input": "5 6739\n29 48 36 80 74\n22 37 36 54 88", "output": "180.22500000000002" }, { "input": "9 35\n27 71 41 3 9 74 16 29 95\n95 69 20 41 41 22 10 92 58", "output": "35.0" }, { "input": "13 5115\n13 51 17 24 52 4 33 4 94 17 54 82 77\n40 34 90 29 81 24 38 74 28 81 14 40 24", "output": "135.33333333333334" }, { "input": "13 9049\n58 13 53 62 41 80 38 14 6 96 23 29 41\n42 24 20 12 63 82 33 93 3 31 68 10 24", "output": "107.2258064516129" }, { "input": "2 775\n13 39\n76 35", "output": "46.66666666666667" }, { "input": "7 8690\n73 93 32 47 80 82 97\n49 49 90 43 89 43 67", "output": "264.2926829268293" }, { "input": "11 9698\n62 53 97 20 84 9 50 100 81 35 14\n18 19 39 30 26 56 41 43 24 32 28", "output": "175.6451612903226" }, { "input": "6 1090\n1 1 44 63 35 64\n29 53 64 11 32 66", "output": "36.317460317460316" }, { "input": "8 9291\n93 68 34 81 53 96 7 26\n23 64 15 47 94 66 90 92", "output": "113.26881720430106" }, { "input": "16 1718\n42 68 96 52 47 31 89 5 87 70 25 69 35 86 86 11\n35 37 51 15 33 94 18 48 91 2 4 89 73 93 47 26", "output": "25.685714285714283" }, { "input": "4 575\n24 23 16 64\n85 100 14 13", "output": "25.796875" }, { "input": "9 423\n28 88 41 71 99 24 35 68 90\n7 76 44 27 64 52 92 81 98", "output": "136.0" }, { "input": "2 1437\n66 58\n44 8", "output": "17.10344827586207" }, { "input": "18 4733\n78 53 33 72 38 76 43 51 94 18 22 21 65 60 5 71 88 40\n5 78 50 43 81 44 10 18 23 51 52 31 10 55 63 46 82 92", "output": "59.48717948717948" }, { "input": "16 7170\n17 1 48 51 28 16 41 14 59 93 25 76 46 69 74 41\n54 53 41 25 50 42 37 20 11 35 90 96 78 3 20 38", "output": "30.391304347826086" }, { "input": "14 7455\n96 38 61 34 68 91 45 49 81 87 46 60 83 16\n38 4 99 16 99 40 68 84 18 56 16 81 21 21", "output": "89.99999999999999" }, { "input": "1 9291\n97\n96", "output": "96.0" }, { "input": "14 3615\n81 79 13 94 54 69 92 5 47 98 40 64 44 88\n52 73 7 12 29 40 46 47 60 66 63 68 71 4", "output": "39.45454545454545" }, { "input": "18 6283\n50 78 16 38 44 9 23 54 58 82 59 12 69 1 10 6 77 61\n70 59 12 11 98 55 52 12 69 40 100 47 42 21 48 18 14 22", "output": "135.8181818181818" }, { "input": "9 3269\n79 88 15 74 92 33 68 64 45\n55 84 75 50 68 32 41 82 42", "output": "336.44117647058823" }, { "input": "6 1007\n93 23 35 15 25 6\n58 24 11 99 23 47", "output": "61.91428571428571" }, { "input": "11 710\n2 49 56 33 79 69 64 62 64 9 87\n94 34 90 3 13 67 76 80 69 19 41", "output": "52.18181818181819" }, { "input": "18 9292\n15 97 47 88 15 7 15 86 52 40 16 97 2 80 64 37 88 15\n39 47 94 12 34 17 45 39 98 99 19 8 94 50 87 68 31 6", "output": "71.01030927835052" }, { "input": "11 3753\n78 75 17 65 97 36 79 56 97 62 43\n18 41 17 47 14 40 7 57 58 24 98", "output": "62.46835443037974" }, { "input": "13 1407\n21 67 79 68 44 52 18 40 68 56 69 66 25\n26 39 78 93 1 57 58 5 67 49 96 15 16", "output": "15.295454545454545" }, { "input": "20 1479\n69 30 15 62 81 24 5 16 25 65 47 23 62 51 87 50 6 44 88 61\n57 47 76 68 7 57 44 98 24 44 1 79 67 31 72 83 36 65 83 42", "output": "19.382978723404253" }, { "input": "17 3856\n50 59 100 50 80 77 58 86 95 87 30 41 11 99 33 27 75\n47 47 39 62 58 91 55 18 65 47 8 97 31 80 61 87 66", "output": "221.4418604651163" }, { "input": "9 2382\n84 51 95 66 34 77 96 9 57\n3 94 56 22 61 50 23 83 45", "output": "20.32142857142857" }, { "input": "14 1751\n33 82 63 35 67 78 47 27 43 96 58 95 39 29\n42 7 15 83 95 91 60 3 85 39 7 56 39 4", "output": "67.60975609756098" }, { "input": "6 8371\n34 11 24 95 62 32\n98 50 58 46 49 93", "output": "124.92631578947369" }, { "input": "2 5181\n4 1\n6 33", "output": "7.5" }, { "input": "9 632\n51 64 25 25 60 71 56 3 31\n70 28 76 84 86 33 77 11 69", "output": "168.875" }, { "input": "3 2102\n76 15 85\n25 95 80", "output": "57.89473684210526" }, { "input": "5 5005\n5 53 65 52 99\n21 49 9 3 66", "output": "15.807692307692308" }, { "input": "17 8971\n54 62 7 47 48 70 78 96 91 34 84 23 72 75 72 60 21\n4 26 6 41 28 45 70 61 6 75 74 46 17 46 34 27 10", "output": "65.53846153846153" }, { "input": "15 5527\n22 49 56 95 86 23 15 74 38 65 52 92 88 49 54\n33 61 71 95 69 31 30 0 1 93 66 48 65 92 11", "output": "0.0" }, { "input": "20 3696\n87 22 21 83 95 31 28 96 71 25 56 40 70 79 46 87 19 19 34 25\n70 44 34 11 2 1 59 22 46 28 3 53 52 71 34 47 65 71 76 30", "output": "21.768421052631577" }, { "input": "8 5540\n5 9 88 1 74 52 32 79\n17 48 99 33 68 28 2 58", "output": "21.25" }, { "input": "15 303\n33 15 28 14 97 33 77 69 41 76 54 97 11 1 1\n83 70 63 11 71 10 48 65 5 5 82 2 6 79 19", "output": "13.340206185567009" }, { "input": "10 9401\n4 53 39 66 52 42 65 39 1 76\n9 34 16 56 78 14 43 49 95 42", "output": "145.66666666666666" }, { "input": "2 9083\n77 33\n22 22", "output": "31.42857142857143" }, { "input": "16 8826\n29 21 40 93 48 49 43 96 60 68 66 5 96 49 84 44\n94 1 79 12 76 65 99 53 37 39 3 76 15 81 51 91", "output": "40.5" }, { "input": "4 9426\n95 48 98 92\n65 40 43 90", "output": "146.1122448979592" }, { "input": "13 175\n46 77 14 16 84 80 81 36 71 13 87 69 8\n54 46 69 59 30 72 83 97 83 96 43 94 84", "output": "175.0" }, { "input": "13 5023\n11 30 92 40 26 77 33 94 71 2 70 97 50\n32 46 51 14 63 76 34 19 13 34 40 91 23", "output": "126.88732394366197" }, { "input": "18 9978\n26 3 87 84 97 53 70 97 37 57 78 23 34 40 81 62 21 92\n56 73 0 79 93 14 17 80 0 20 3 81 22 71 7 82 71 81", "output": "0.0" }, { "input": "14 8481\n64 2 90 76 49 30 88 32 98 64 20 85 40 35\n55 84 75 43 36 13 67 75 100 19 22 7 5 58", "output": "63.65882352941177" }, { "input": "2 1674\n77 23\n23 25", "output": "29.87012987012987" }, { "input": "10 2112\n45 11 32 14 82 30 34 11 42 56\n18 9 84 99 82 43 61 84 14 70", "output": "119.0" }, { "input": "6 2006\n62 4 3 71 61 10\n37 45 61 84 24 15", "output": "83.01639344262294" }, { "input": "8 3954\n80 77 64 1 50 21 89 26\n30 82 17 20 67 21 31 99", "output": "108.375" }, { "input": "18 7253\n64 77 92 9 32 66 23 34 10 71 8 7 83 9 52 97 29 65\n46 90 65 43 44 63 7 38 38 20 62 9 53 39 17 13 5 90", "output": "110.96907216494844" }, { "input": "4 4384\n42 41 85 79\n29 67 52 55", "output": "151.1058823529412" }, { "input": "7 529\n77 18 67 64 43 51 30\n35 87 17 52 1 97 84", "output": "8.139534883720929" }, { "input": "16 2915\n39 39 81 44 23 47 43 56 7 38 10 100 5 34 87 14\n10 96 34 20 62 88 46 38 29 35 2 43 26 55 31 63", "output": "133.4" }, { "input": "14 6488\n53 41 36 28 17 15 63 33 75 40 85 88 90 100\n7 35 83 2 48 76 93 2 69 56 59 7 25 24", "output": "46.30303030303031" } ]
92
0
3.977
26,770
856
Eleventh Birthday
[ "combinatorics", "dp", "math" ]
null
null
It is Borya's eleventh birthday, and he has got a great present: *n* cards with numbers. The *i*-th card has the number *a**i* written on it. Borya wants to put his cards in a row to get one greater number. For example, if Borya has cards with numbers 1, 31, and 12, and he puts them in a row in this order, he would get a number 13112. He is only 11, but he already knows that there are *n*! ways to put his cards in a row. But today is a special day, so he is only interested in such ways that the resulting big number is divisible by eleven. So, the way from the previous paragraph is good, because 13112<==<=1192<=×<=11, but if he puts the cards in the following order: 31, 1, 12, he would get a number 31112, it is not divisible by 11, so this way is not good for Borya. Help Borya to find out how many good ways to put the cards are there. Borya considers all cards different, even if some of them contain the same number. For example, if Borya has two cards with 1 on it, there are two good ways. Help Borya, find the number of good ways to put the cards. This number can be large, so output it modulo 998244353.
Input data contains multiple test cases. The first line of the input data contains an integer *t* — the number of test cases (1<=≤<=*t*<=≤<=100). The descriptions of test cases follow. Each test is described by two lines. The first line contains an integer *n* (1<=≤<=*n*<=≤<=2000) — the number of cards in Borya's present. The second line contains *n* integers *a**i* (1<=≤<=*a**i*<=≤<=109) — numbers written on the cards. It is guaranteed that the total number of cards in all tests of one input data doesn't exceed 2000.
For each test case output one line: the number of ways to put the cards to the table so that the resulting big number was divisible by 11, print the number modulo 998244353.
[ "4\n2\n1 1\n3\n1 31 12\n3\n12345 67 84\n9\n1 2 3 4 5 6 7 8 9\n" ]
[ "2\n2\n2\n31680\n" ]
none
[]
46
0
0
26,788
457
Distributed Join
[ "greedy" ]
null
null
Piegirl was asked to implement two table join operation for distributed database system, minimizing the network traffic. Suppose she wants to join two tables, *A* and *B*. Each of them has certain number of rows which are distributed on different number of partitions. Table *A* is distributed on the first cluster consisting of *m* partitions. Partition with index *i* has *a**i* rows from *A*. Similarly, second cluster containing table *B* has *n* partitions, *i*-th one having *b**i* rows from *B*. In one network operation she can copy one row from any partition to any other partition. At the end, for each row from *A* and each row from *B* there should be a partition that has both rows. Determine the minimal number of network operations to achieve this.
First line contains two integer numbers, *m* and *n* (1<=≤<=*m*,<=*n*<=≤<=105). Second line contains description of the first cluster with *m* space separated integers, *a**i* (1<=≤<=*a**i*<=≤<=109). Similarly, third line describes second cluster with *n* space separated integers, *b**i* (1<=≤<=*b**i*<=≤<=109).
Print one integer — minimal number of copy operations.
[ "2 2\n2 6\n3 100\n", "2 3\n10 10\n1 1 1\n" ]
[ "11\n", "6\n" ]
In the first example it makes sense to move all the rows to the second partition of the second cluster which is achieved in 2 + 6 + 3 = 11 operations In the second example Piegirl can copy each row from *B* to the both partitions of the first cluster which needs 2·3 = 6 copy operations.
[ { "input": "2 2\n2 6\n3 100", "output": "11" }, { "input": "2 3\n10 10\n1 1 1", "output": "6" }, { "input": "2 2\n888381664 866366630\n170399907 404233949", "output": "1149267712" }, { "input": "3 4\n337369924 278848730 654933675\n866361693 732544605 890800310 350303294", "output": "3220361921" }, { "input": "10 10\n510955240 684852706 455356451 284505713 595775142 646334608 563116339 941123613 818750895 516673753\n382626402 204542396 341363992 234231105 75079663 683639780 624391764 265169060 686304227 280991725", "output": "8854660961" }, { "input": "6 5\n45936257 8169878 14134346 26323055 65863745 50728147\n997339869 2970526 163305525 839524148 193404120", "output": "781991027" }, { "input": "5 4\n556840201 669601415 674042771 93322040 157280418\n253115131 933556803 294280580 169051325", "output": "2867533881" }, { "input": "5 7\n473347111 640932948 320036306 595696211 365475226\n347859328 553364017 687935743 145411543 689180757 696504973 783694820", "output": "5515744163" }, { "input": "8 8\n808147225 623333304 535665685 469385259 122918604 200681823 800211367 286974812\n85215517 983921829 274028967 567054904 144473212 964018990 177471567 73882806", "output": "6133464042" }, { "input": "10 10\n326151338 981287141 830123412 482457331 77554645 351237238 663827505 549778905 967488359 954617100\n238752550 787656851 393452025 837732771 522417885 876998499 195063055 325140429 546151936 403260186", "output": "10329862020" }, { "input": "10 10\n933168403 835157665 823216696 818565876 448948583 884328249 809244579 473034231 407137956 871269623\n653126539 145998557 644003076 138712151 839886312 479712343 709513279 138285801 858528549 643830064", "output": "11622500129" }, { "input": "10 10\n269584761 865524829 265226347 963092340 261501474 16861445 221090297 746538035 842020225 649641719\n49728483 423679832 107851851 179960003 345895125 400584885 460489835 377856735 506736683 676996548", "output": "7667769112" }, { "input": "10 10\n458278487 288667180 648471199 581765640 758405216 589361337 319325955 938498114 249892107 138299026\n57775135 470751607 454623764 556600014 141039336 225043834 692497485 517610562 635337211 56258907", "output": "7840004002" }, { "input": "5 6\n7780674 1861750 4491902 10256124 14362475\n1809567 5616386 1771573 2099536 1113026 3938402", "output": "40738940" }, { "input": "6 5\n40192277 37957130 22509015 95257198 6210193 16850057\n76289330 265203569 184343840 163207736 126924648", "output": "769741424" }, { "input": "6 5\n4689556 6609945 15705705 10301912 11245669 5844638\n440894622 898226832 22060902 222576920 53133033", "output": "238386210" }, { "input": "5 6\n284534195 993347068 628813225 512761241 835859363\n61567950 7311163 14322159 100466429 66443161 48573213", "output": "1479270495" }, { "input": "5 6\n574664105 497253985 200935113 926362846 223381305\n34188719 14075259 27219005 9682257 14352213 11696423", "output": "556069380" }, { "input": "1 1\n1889\n2867", "output": "1889" }, { "input": "20 30\n81 67 100 83 97 97 58 54 72 78 59 64 55 85 75 58 79 91 64 84\n116 13 114 180 17 123 64 185 170 54 138 142 89 191 78 152 49 5 121 66 163 171 64 170 143 143 126 12 175 84", "output": "4628" } ]
0
0
-1
26,835
103
Buying Sets
[ "flows", "graph matchings" ]
E. Buying Sets
2
256
The Hexadecimal virus loves playing with number sets — intersecting them, uniting them. One beautiful day she was surprised to find out that Scuzzy, her spherical pet cat, united all sets in one and ate the result! Something had to be done quickly and Hexadecimal rushed to the market. The market has *n* sets of numbers on sale. The virus wants to buy the following collection of sets: the number of sets in the collection should be exactly the same as the number of numbers in the union of all bought sets. Moreover, Hexadecimal wants to buy the cheapest suitable collection of set. Yet nothing's so easy! As Mainframe is a kingdom of pure rivalry markets, we know that the union of any *k* sets contains no less than *k* distinct numbers (for every positive integer *k*). Help the virus choose the suitable collection of sets. The collection can be empty.
The first line contains the only number *n* (1<=≤<=*n*<=≤<=300) — the number of sets available in the market. Next *n* lines describe the goods: first we are given *m**i* (1<=≤<=*m**i*<=≤<=*n*) — the number of distinct numbers in the *i*-th set, then follow *m**i* numbers — the set's elements. We know that the set's elements are distinct positive integers and they do not exceed *n*. The last line contains *n* integers whose absolute values do not exceed 106 — the price of each set.
Print a single number — the minimum price the virus will have to pay for such a collection of *k* sets that union of the collection's sets would have exactly *k* distinct numbers ().
[ "3\n1 1\n2 2 3\n1 3\n10 20 -3\n", "5\n2 1 2\n2 2 3\n2 3 4\n2 4 5\n2 5 1\n1 -1 1 -1 1\n", "5\n2 1 2\n2 2 3\n2 3 4\n2 4 5\n2 5 1\n-1 1 -1 1 -1\n" ]
[ "-3\n", "0\n", "-1\n" ]
none
[]
92
0
0
26,963
161
Abracadabra
[ "divide and conquer" ]
null
null
Polycarpus analyzes a string called abracadabra. This string is constructed using the following algorithm: - On the first step the string consists of a single character "a". - On the *k*-th step Polycarpus concatenates two copies of the string obtained on the (*k*<=-<=1)-th step, while inserting the *k*-th character of the alphabet between them. Polycarpus uses the alphabet that consists of lowercase Latin letters and digits (a total of 36 characters). The alphabet characters are numbered like this: the 1-st character is "a", the 2-nd — "b", ..., the 26-th — "z", the 27-th — "0", the 28-th — "1", ..., the 36-th — "9". Let's have a closer look at the algorithm. On the second step Polycarpus will concatenate two strings "a" and insert the character "b" between them, resulting in "aba" string. The third step will transform it into "abacaba", and the fourth one - into "abacabadabacaba". Thus, the string constructed on the *k*-th step will consist of 2*k*<=-<=1 characters. Polycarpus wrote down the string he got after 30 steps of the given algorithm and chose two non-empty substrings of it. Your task is to find the length of the longest common substring of the two substrings selected by Polycarpus. A substring *s*[*i*... *j*] (1<=≤<=*i*<=≤<=*j*<=≤<=|*s*|) of string *s* = *s*1*s*2... *s*|*s*| is a string *s**i**s**i*<=+<=1... *s**j*. For example, substring *s*[2...4] of string *s* = "abacaba" equals "bac". The string is its own substring. The longest common substring of two strings *s* and *t* is the longest string that is a substring of both *s* and *t*. For example, the longest common substring of "contest" and "systemtesting" is string "test". There can be several common substrings of maximum length.
The input consists of a single line containing four integers *l*1, *r*1, *l*2, *r*2 (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=109, *i*<==<=1,<=2). The numbers are separated by single spaces. *l**i* and *r**i* give the indices of the first and the last characters of the *i*-th chosen substring, correspondingly (*i*<==<=1,<=2). The characters of string abracadabra are numbered starting from 1.
Print a single number — the length of the longest common substring of the given strings. If there are no common substrings, print 0.
[ "3 6 1 4\n", "1 1 4 4\n" ]
[ "2\n", "0\n" ]
In the first sample the first substring is "acab", the second one is "abac". These two substrings have two longest common substrings "ac" and "ab", but we are only interested in their length — 2. In the second sample the first substring is "a", the second one is "c". These two substrings don't have any common characters, so the length of their longest common substring is 0.
[ { "input": "3 6 1 4", "output": "2" }, { "input": "1 1 4 4", "output": "0" }, { "input": "2 2 6 6", "output": "1" }, { "input": "73426655 594361930 343984155 989446962", "output": "379149396" }, { "input": "169720415 312105195 670978284 671296539", "output": "207899" }, { "input": "1 1 1 1", "output": "1" }, { "input": "1 1 2 3", "output": "1" }, { "input": "2 3 1 1", "output": "1" }, { "input": "4 5 6 7", "output": "1" }, { "input": "5 6 5 10", "output": "2" }, { "input": "20 59 93 97", "output": "5" }, { "input": "59 797 761 863", "output": "103" }, { "input": "3563 8248 1195 5811", "output": "2901" }, { "input": "26733 47464 19138 46248", "output": "19516" }, { "input": "183307 582175 813247 925985", "output": "112739" }, { "input": "42765 7043311 3930802 8641200", "output": "4151539" }, { "input": "48358214 56090000 19994986 77748608", "output": "7731787" }, { "input": "326428072 910655768 241366302 856438517", "output": "530010446" }, { "input": "1 1000000000 1 1000000000", "output": "1000000000" }, { "input": "601080293 742283208 417827259 630484959", "output": "71194568" }, { "input": "933937636 947664621 406658382 548532154", "output": "8140525" }, { "input": "876260202 917475655 508441743 544698794", "output": "28429169" }, { "input": "260267830 630246217 436204204 880818505", "output": "268435455" }, { "input": "1 463129088 536870913 1000000000", "output": "463129088" }, { "input": "1 463129088 536870914 1000000000", "output": "463129087" }, { "input": "58660225 863918362 315894896 954309337", "output": "548023467" }, { "input": "462616550 929253987 199885647 365920450", "output": "166034804" }, { "input": "166724572 472113234 358126054 528083792", "output": "125430608" }, { "input": "654444727 988815385 77276659 644738371", "output": "334370659" }, { "input": "346539730 828420288 373318830 643522086", "output": "270203257" }, { "input": "156642200 503020953 296806626 871864091", "output": "234585497" }, { "input": "229012373 968585257 177685154 283692208", "output": "106007055" }, { "input": "225343773 292960163 388346281 585652974", "output": "43091683" }, { "input": "760202684 921630809 8799976 434695123", "output": "161428126" }, { "input": "489816019 571947327 244679586 543875061", "output": "54059043" }, { "input": "377544108 461895419 242140460 901355034", "output": "84351312" }, { "input": "293057586 653835431 583814665 643163992", "output": "59349328" }, { "input": "926028190 962292871 588752738 848484542", "output": "36264682" }, { "input": "79844257 998861014 59606735 909001530", "output": "829157274" }, { "input": "656438998 774335411 16384880 470969252", "output": "117896414" }, { "input": "287551411 788248606 147317343 692683069", "output": "405131659" }, { "input": "677764866 754506263 454018800 668014358", "output": "76741398" }, { "input": "1 2 3 6", "output": "2" }, { "input": "1 1 2 1000000000", "output": "1" }, { "input": "5 7 13 15", "output": "3" }, { "input": "1 3 5 7", "output": "3" }, { "input": "1 3 9 11", "output": "3" }, { "input": "5 7 1 3", "output": "3" }, { "input": "1 4 4 7", "output": "3" }, { "input": "4 7 12 15", "output": "4" }, { "input": "1 4 9 12", "output": "4" }, { "input": "4 7 1 4", "output": "3" }, { "input": "3 3 1 2", "output": "1" }, { "input": "1 2 3 3", "output": "1" }, { "input": "3 4 1 2", "output": "1" }, { "input": "8136 12821 10573 15189", "output": "2901" }, { "input": "331458616 472661531 443256865 655914565", "output": "71194568" }, { "input": "156266169 197481622 529043030 565300081", "output": "28429169" }, { "input": "443495607 813473994 192923319 637537620", "output": "268435455" }, { "input": "1 2 2 1000000000", "output": "2" }, { "input": "1 3 4 1000000000", "output": "3" }, { "input": "1 999999999 999999998 1000000000", "output": "3" } ]
15
0
-1
26,989
436
Pudding Monsters
[ "dp" ]
null
null
Have you ever played Pudding Monsters? In this task, a simplified one-dimensional model of this game is used. Imagine an infinite checkered stripe, the cells of which are numbered sequentially with integers. Some cells of the strip have monsters, other cells of the strip are empty. All monsters are made of pudding, so if there are two monsters in the neighboring cells, they stick to each other (literally). Similarly, if several monsters are on consecutive cells, they all stick together in one block of monsters. We will call the stuck together monsters a block of monsters. A detached monster, not stuck to anyone else, is also considered a block. In one move, the player can take any block of monsters and with a movement of his hand throw it to the left or to the right. The selected monsters will slide on until they hit some other monster (or a block of monsters). For example, if a strip has three monsters in cells 1, 4 and 5, then there are only four possible moves: to send a monster in cell 1 to minus infinity, send the block of monsters in cells 4 and 5 to plus infinity, throw monster 1 to the right (it will stop in cell 3), throw a block of monsters in cells 4 and 5 to the left (they will stop in cells 2 and 3). Some cells on the strip are marked with stars. These are the special cells. The goal of the game is to make the largest possible number of special cells have monsters on them. You are given the numbers of the special cells on a strip as well as the initial position of all monsters. What is the maximum number of special cells that will contain monsters in the optimal game?
The first line contains two integers *n* and *m* (1<=≤<=*n*<=≤<=105; 1<=≤<=*m*<=≤<=2000) — the number of monsters on the strip and the number of special cells. The second line contains *n* distinct integers — the numbers of the cells with monsters, then the third line contains *m* distinct integers — the numbers of the special cells. It is guaranteed that all the numbers of the cells are positive integers not exceeding 2·105.
Print a single integer — the maximum number of special cells that will contain monsters in the optimal game.
[ "3 2\n1 3 5\n2 4\n", "4 2\n1 3 4 6\n2 5\n", "4 2\n1 8 4 5\n7 2\n" ]
[ "2\n", "2\n", "1\n" ]
none
[]
46
0
0
27,047
370
Mittens
[ "constructive algorithms", "greedy", "sortings" ]
null
null
A Christmas party in city S. had *n* children. All children came in mittens. The mittens can be of different colors, but each child had the left and the right mitten of the same color. Let's say that the colors of the mittens are numbered with integers from 1 to *m*, and the children are numbered from 1 to *n*. Then the *i*-th child has both mittens of color *c**i*. The Party had Santa Claus ('Father Frost' in Russian), his granddaughter Snow Girl, the children danced around the richly decorated Christmas tree. In fact, everything was so bright and diverse that the children wanted to wear mittens of distinct colors. The children decided to swap the mittens so that each of them got one left and one right mitten in the end, and these two mittens were of distinct colors. All mittens are of the same size and fit all the children. The children started exchanging the mittens haphazardly, but they couldn't reach the situation when each child has a pair of mittens of distinct colors. Vasily Petrov, the dad of one of the children, noted that in the general case the children's idea may turn out impossible. Besides, he is a mathematician and he came up with such scheme of distributing mittens that the number of children that have distinct-colored mittens was maximum. You task is to repeat his discovery. Note that the left and right mittens are different: each child must end up with one left and one right mitten.
The first line contains two integers *n* and *m* — the number of the children and the number of possible mitten colors (1<=≤<=*n*<=≤<=5000, 1<=≤<=*m*<=≤<=100). The second line contains *n* integers *c*1,<=*c*2,<=... *c**n*, where *c**i* is the color of the mittens of the *i*-th child (1<=≤<=*c**i*<=≤<=*m*).
In the first line, print the maximum number of children who can end up with a distinct-colored pair of mittens. In the next *n* lines print the way the mittens can be distributed in this case. On the *i*-th of these lines print two space-separated integers: the color of the left and the color of the right mitten the *i*-th child will get. If there are multiple solutions, you can print any of them.
[ "6 3\n1 3 2 2 1 1\n", "4 2\n1 2 1 1\n" ]
[ "6\n2 1\n1 2\n2 1\n1 3\n1 2\n3 1\n", "2\n1 2\n1 1\n2 1\n1 1\n" ]
none
[ { "input": "6 3\n1 3 2 2 1 1", "output": "6\n2 1\n1 2\n2 1\n1 3\n1 2\n3 1" }, { "input": "4 2\n1 2 1 1", "output": "2\n1 2\n1 1\n2 1\n1 1" }, { "input": "1 1\n1", "output": "0\n1 1" }, { "input": "4 1\n1 1 1 1", "output": "0\n1 1\n1 1\n1 1\n1 1" }, { "input": "2 2\n2 2", "output": "0\n2 2\n2 2" }, { "input": "2 2\n1 2", "output": "2\n2 1\n1 2" }, { "input": "4 4\n4 3 2 1", "output": "4\n4 3\n3 2\n1 4\n2 1" }, { "input": "4 2\n1 2 1 2", "output": "4\n2 1\n2 1\n1 2\n1 2" }, { "input": "5 3\n1 1 2 1 1", "output": "2\n2 1\n1 1\n1 1\n1 1\n1 2" }, { "input": "6 3\n1 1 2 2 3 3", "output": "6\n1 3\n3 2\n2 1\n2 1\n3 2\n1 3" }, { "input": "10 3\n3 1 2 2 2 1 2 2 2 1", "output": "8\n1 2\n2 1\n3 2\n2 1\n2 1\n2 2\n1 2\n2 3\n2 2\n1 2" }, { "input": "10 3\n3 1 3 1 1 2 2 2 1 3", "output": "10\n2 1\n1 3\n2 1\n1 3\n1 3\n3 2\n2 1\n1 2\n3 2\n3 1" }, { "input": "10 3\n1 2 1 2 2 1 2 3 2 1", "output": "10\n1 2\n2 1\n3 2\n2 1\n2 1\n1 2\n1 2\n2 1\n2 3\n1 2" }, { "input": "10 3\n3 2 3 1 2 2 2 1 1 3", "output": "10\n1 2\n2 3\n1 2\n2 3\n2 3\n3 1\n1 2\n2 1\n3 1\n3 2" }, { "input": "10 3\n3 3 1 3 1 2 2 1 3 2", "output": "10\n1 3\n3 2\n1 3\n3 2\n3 2\n2 1\n1 3\n3 1\n2 1\n2 3" }, { "input": "100 10\n8 9 1 5 7 8 2 6 7 3 1 6 5 5 8 10 5 2 1 7 1 9 8 7 9 9 8 10 8 10 2 9 5 5 1 7 8 5 3 8 6 2 9 2 5 3 8 10 7 1 2 9 5 2 7 8 2 10 8 8 7 6 2 7 3 9 8 9 9 5 10 8 5 7 10 7 2 4 6 7 6 7 5 4 5 3 2 6 5 6 1 5 7 8 3 10 5 9 3 6", "output": "100\n5 8\n5 8\n5 8\n4 5\n1 5\n8 7\n2 6\n10 4\n3 5\n3 5\n8 7\n2 10\n7 9\n7 2\n6 3\n9 6\n1 5\n7 2\n1 5\n8 7\n5 8\n9 2\n2 10\n2 10\n2 10\n5 8\n5 8\n10 1\n5 8\n4 5\n10 1\n10 1\n9 6\n7 9\n2 6\n7 9\n8 7\n5 8\n3 5\n8 7\n2 10\n6 3\n5 8\n5 8\n8 7\n9 6\n1 5\n8 7\n8 7\n2 6\n6 1\n7 9\n8 7\n8 7\n10 1\n5 7\n9 2\n7 9\n7 9\n7 2\n10 4\n7 9\n9 2\n2 10\n3 5\n1 5\n2 6\n8 7\n7 9\n8 7\n6 3\n3 5\n7 9\n10 1\n6 3\n1 5\n5 8\n5 8\n3 5\n7 2\n8 9\n9 6\n6 3\n9 6\n6 10\n8 9\n3 5\n7 2\n6 3\n6 3\n9 2\n8 7\n1 5\n5 8\n9 2\n10 1\n5 8\n2 10\n..." }, { "input": "100 10\n4 1 10 3 2 8 10 2 1 4 7 2 4 6 1 1 10 7 4 7 7 10 9 9 6 3 5 6 3 8 9 10 2 3 3 1 3 5 4 4 10 6 9 4 1 3 8 4 10 8 7 7 4 2 3 9 4 1 9 4 6 3 3 4 3 2 7 9 8 6 5 3 3 7 1 8 2 4 6 2 7 3 4 8 8 9 6 3 2 5 4 5 2 3 10 3 5 4 6 10", "output": "100\n3 4\n3 4\n3 4\n5 3\n5 3\n4 10\n7 9\n8 5\n1 3\n1 3\n4 2\n6 8\n4 10\n2 7\n9 1\n10 6\n1 3\n2 7\n5 3\n4 2\n3 4\n10 6\n6 8\n6 8\n6 8\n3 4\n3 4\n9 1\n3 4\n5 3\n9 5\n8 5\n7 9\n2 7\n7 9\n2 7\n4 2\n3 4\n1 3\n3 2\n6 8\n6 8\n3 4\n3 4\n4 2\n7 9\n5 3\n4 2\n4 2\n7 9\n9 1\n2 10\n4 2\n4 10\n8 5\n3 4\n10 6\n2 10\n2 7\n10 6\n8 3\n2 7\n10 6\n7 9\n8 3\n5 3\n7 9\n4 2\n2 7\n4 2\n9 1\n1 3\n4 10\n8 5\n9 1\n1 3\n3 4\n3 4\n1 3\n2 7\n4 10\n7 6\n9 1\n7 9\n6 8\n4 10\n8 3\n10 7\n9 1\n6 1\n10 6\n4 10\n1 3\n3 4\n10 6\n8 5\n3 4\n6 8\n..." }, { "input": "100 10\n10 7 5 8 9 1 3 2 9 5 5 4 6 2 1 7 1 2 1 4 1 6 1 2 6 2 4 9 5 5 8 7 10 5 2 1 4 2 1 7 2 5 10 1 2 1 5 6 7 2 8 9 6 10 3 6 7 1 9 6 2 1 1 3 8 7 9 7 6 9 7 4 2 2 1 1 10 5 5 2 4 7 4 8 7 5 2 8 10 3 1 8 10 1 5 6 3 6 3 3", "output": "100\n1 2\n1 2\n1 2\n3 1\n3 1\n2 7\n6 9\n8 3\n4 1\n4 1\n2 5\n6 8\n5 7\n5 6\n10 4\n7 10\n4 1\n5 6\n3 1\n2 5\n1 2\n7 6\n6 8\n10 8\n10 8\n1 2\n1 2\n9 3\n1 2\n3 1\n9 3\n8 3\n7 10\n5 7\n6 9\n5 6\n2 5\n1 2\n4 1\n1 5\n6 9\n10 8\n1 2\n1 2\n2 5\n6 9\n3 1\n2 5\n2 5\n6 9\n9 4\n5 7\n2 5\n2 7\n9 3\n1 5\n7 10\n5 7\n5 7\n7 6\n8 1\n5 6\n7 6\n6 9\n8 1\n3 1\n6 9\n2 5\n5 7\n2 5\n10 4\n4 1\n2 7\n8 3\n9 4\n4 1\n1 2\n1 2\n4 1\n5 6\n2 7\n7 10\n9 4\n7 10\n10 8\n2 7\n8 1\n5 6\n9 4\n10 4\n7 6\n2 5\n3 1\n1 2\n7 10\n8 3\n1 2\n6 8\n7 1..." }, { "input": "100 10\n10 7 4 3 5 9 7 10 3 8 1 10 5 3 8 2 5 7 10 2 8 9 2 6 1 4 2 1 1 10 9 7 9 1 4 1 1 2 4 6 3 6 2 10 10 7 1 2 4 9 9 7 7 6 9 8 6 6 10 6 2 10 10 10 8 9 10 5 3 4 4 1 2 8 3 10 10 7 3 2 6 3 7 2 9 3 4 4 9 10 8 6 6 4 7 9 3 8 6 3", "output": "100\n10 6\n10 9\n10 9\n5 10\n5 10\n6 3\n2 4\n1 5\n1 10\n8 10\n9 6\n7 4\n6 3\n3 2\n4 1\n2 7\n8 10\n3 2\n8 10\n9 6\n10 9\n3 7\n7 1\n7 1\n7 1\n10 9\n10 9\n4 8\n10 9\n5 10\n4 8\n1 5\n2 4\n6 2\n2 4\n3 2\n9 3\n10 9\n1 10\n9 6\n7 4\n7 1\n10 6\n10 6\n9 3\n2 4\n5 10\n9 6\n9 6\n7 4\n4 8\n6 2\n9 6\n6 3\n4 8\n9 6\n2 7\n6 2\n6 2\n3 7\n1 5\n3 2\n3 7\n7 4\n1 5\n8 10\n2 4\n9 3\n3 2\n9 3\n4 1\n8 10\n6 3\n1 8\n4 8\n8 10\n10 6\n10 9\n8 10\n3 2\n6 3\n2 7\n4 1\n2 7\n7 1\n6 3\n1 10\n3 2\n4 8\n4 1\n3 7\n6 3\n8 10\n10 9\n2 7\n1 8..." }, { "input": "100 10\n1 5 7 10 9 2 8 3 4 3 4 9 2 10 1 8 10 3 1 4 1 4 4 9 6 3 7 2 5 9 8 5 9 2 1 3 1 8 2 2 7 1 9 2 10 8 2 8 4 9 8 3 8 3 2 4 3 7 10 6 1 10 7 9 10 4 2 7 4 7 10 9 5 2 4 9 6 2 1 1 5 4 9 1 9 9 9 5 10 6 3 9 9 5 1 6 3 10 2 5", "output": "100\n9 2\n9 2\n9 2\n6 9\n6 9\n2 4\n10 8\n5 6\n5 9\n7 9\n2 1\n3 8\n1 4\n1 10\n3 5\n4 3\n7 9\n4 10\n7 9\n2 1\n9 2\n4 3\n3 5\n3 5\n3 5\n9 2\n9 2\n8 7\n9 2\n6 9\n8 7\n5 6\n10 3\n1 4\n10 8\n1 10\n2 4\n9 2\n5 9\n2 1\n10 8\n3 5\n9 1\n9 1\n2 1\n10 3\n6 9\n2 1\n2 1\n10 8\n8 7\n1 4\n2 1\n2 4\n8 7\n9 1\n4 3\n1 4\n1 10\n4 10\n5 6\n1 10\n4 3\n10 8\n5 9\n6 9\n10 8\n2 1\n1 10\n2 1\n3 5\n7 9\n1 4\n8 6\n8 7\n7 9\n9 2\n9 2\n7 9\n4 10\n1 4\n10 3\n8 7\n10 3\n3 5\n1 4\n5 9\n4 10\n8 7\n3 5\n4 10\n2 4\n7 9\n9 2\n4 3\n5 6\n9 2\n3..." }, { "input": "100 2\n2 2 2 2 2 2 2 2 2 2 1 2 1 1 2 2 1 2 1 1 2 2 2 2 1 2 2 2 2 1 1 2 2 1 2 1 2 2 2 1 2 2 1 2 2 2 2 2 2 2 2 2 1 1 2 2 2 2 2 2 2 2 1 1 2 2 2 2 2 2 1 2 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2 1 2 1 2 2 2 2 2", "output": "46\n2 1\n2 1\n2 1\n1 2\n1 2\n2 2\n2 2\n1 2\n1 2\n1 2\n2 1\n2 2\n2 2\n2 2\n2 2\n2 2\n1 2\n2 2\n1 2\n2 1\n2 1\n2 2\n2 2\n2 2\n2 2\n2 1\n2 1\n2 2\n2 1\n1 2\n1 2\n1 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 1\n1 2\n2 1\n2 2\n2 2\n2 1\n2 1\n2 2\n2 2\n1 2\n2 1\n2 1\n2 2\n2 2\n2 2\n2 1\n2 2\n1 2\n2 1\n2 2\n2 2\n2 2\n2 2\n1 2\n2 2\n2 2\n2 2\n1 2\n1 2\n2 2\n2 1\n2 2\n2 2\n2 2\n1 2\n2 2\n1 2\n2 2\n1 2\n2 1\n2 1\n1 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n1 2\n2 2\n2 2\n2 2\n2 2\n2 2\n1 2\n2 1\n2 2\n1 2\n2 1\n2 2\n2 2\n2 1" }, { "input": "100 3\n1 2 3 2 2 2 1 1 2 2 2 2 2 1 2 3 1 2 2 3 2 3 3 2 2 1 2 2 2 3 1 2 2 2 2 2 2 2 2 2 2 2 3 2 2 2 3 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 3 2 2 3 3 2 3 3 2 2 1 2 2 2 2 1 2 1 2 2 2 2 2 1 1", "output": "54\n2 3\n2 3\n2 3\n1 2\n1 2\n2 2\n2 2\n3 2\n3 2\n1 2\n2 1\n2 2\n2 2\n2 2\n2 2\n2 2\n1 2\n2 2\n1 2\n2 1\n2 3\n2 2\n2 2\n2 2\n2 2\n2 3\n2 3\n3 2\n2 3\n1 2\n3 2\n3 2\n2 2\n2 2\n2 2\n2 2\n2 1\n2 3\n1 2\n2 1\n2 2\n2 2\n2 1\n2 3\n2 1\n2 2\n1 2\n2 1\n2 1\n2 2\n3 2\n2 2\n2 1\n2 2\n3 2\n2 1\n2 2\n2 2\n2 2\n2 2\n3 2\n2 2\n2 2\n2 2\n3 2\n1 2\n2 2\n2 1\n2 2\n2 1\n2 2\n1 2\n2 2\n3 2\n3 2\n1 2\n2 3\n2 3\n1 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n3 2\n2 2\n3 2\n2 2\n2 2\n2 1\n1 2\n2 3\n2 2\n3 2\n2 3\n2 2\n2 2\n2 3" }, { "input": "100 4\n2 2 3 2 2 4 4 4 2 4 4 4 4 4 2 4 2 4 4 2 1 1 1 4 4 4 2 4 4 2 4 4 4 1 4 4 4 4 3 4 4 4 4 4 4 2 2 2 4 1 3 1 1 4 2 4 3 4 4 1 4 4 4 4 4 3 4 4 4 4 4 1 1 2 1 4 4 4 4 1 4 1 4 4 2 1 4 4 2 4 4 4 2 4 4 3 4 4 4 4", "output": "76\n4 2\n4 2\n4 2\n3 4\n3 4\n4 1\n4 4\n1 4\n1 4\n1 4\n4 1\n2 4\n4 3\n4 4\n2 4\n4 4\n1 4\n4 4\n3 4\n4 2\n4 2\n4 4\n2 4\n2 4\n2 4\n4 2\n4 2\n2 4\n4 2\n3 4\n2 4\n1 4\n4 4\n4 3\n4 4\n4 4\n4 1\n4 2\n1 4\n4 2\n4 4\n2 4\n4 2\n4 2\n4 1\n4 4\n3 4\n4 1\n4 1\n4 4\n2 4\n4 3\n4 1\n4 1\n2 4\n4 2\n4 4\n4 3\n4 3\n4 4\n1 4\n4 4\n4 4\n4 4\n1 4\n3 4\n4 4\n4 1\n4 3\n4 1\n2 4\n1 4\n4 1\n2 4\n2 4\n1 4\n4 2\n4 2\n1 4\n4 4\n4 1\n4 4\n2 4\n4 4\n2 4\n4 1\n1 4\n4 4\n2 4\n2 4\n4 4\n4 1\n1 4\n4 2\n4 4\n1 4\n4 2\n2 4\n4 4\n4 2" }, { "input": "100 5\n5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 1 5 3 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 4 5 5 5 5 5", "output": "6\n5 5\n5 3\n5 5\n3 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n1 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n4 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 1\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 4\n5 5\n5 5\n5 5" }, { "input": "100 6\n4 3 4 4 4 4 4 3 4 4 4 1 2 4 2 6 4 4 3 2 4 4 4 4 3 4 4 2 4 4 4 6 4 1 4 2 4 4 4 4 4 4 4 4 6 6 4 4 4 4 4 1 4 5 4 4 4 4 4 4 4 4 4 4 4 4 2 4 4 4 4 4 4 4 5 4 2 4 4 4 3 4 5 4 6 4 5 4 4 4 2 4 4 6 4 3 4 5 3 4", "output": "58\n4 3\n4 2\n4 3\n1 4\n5 4\n4 1\n4 4\n3 4\n6 4\n6 4\n4 6\n4 4\n4 4\n4 4\n2 4\n4 4\n6 4\n4 4\n5 4\n4 6\n4 3\n4 4\n4 4\n4 4\n4 4\n4 2\n4 2\n2 4\n4 2\n1 4\n2 4\n3 4\n4 4\n4 4\n4 4\n4 4\n4 5\n4 2\n6 4\n4 6\n4 4\n4 4\n4 3\n4 3\n4 5\n4 4\n1 4\n4 6\n4 6\n4 4\n2 4\n4 4\n4 5\n4 1\n2 4\n4 6\n4 4\n4 4\n4 4\n4 4\n3 4\n4 4\n4 4\n4 4\n3 4\n5 4\n4 4\n4 5\n4 4\n4 5\n4 4\n6 4\n4 4\n3 4\n2 4\n5 4\n4 3\n4 2\n6 4\n4 4\n4 4\n4 4\n2 4\n4 4\n4 4\n4 4\n3 4\n4 4\n2 4\n4 4\n4 4\n4 1\n5 4\n4 3\n4 4\n3 4\n4 2\n4 4\n4 4\n4 2" }, { "input": "100 10\n3 7 7 7 6 6 10 10 3 4 4 4 10 3 7 7 4 7 6 10 3 3 4 5 5 7 10 4 8 10 2 5 9 6 6 7 7 6 9 2 7 3 6 4 4 3 10 7 6 8 7 4 3 4 5 8 6 5 3 3 6 7 4 9 10 5 3 3 10 6 3 4 1 10 4 10 5 4 6 3 3 6 4 3 2 3 10 4 7 6 1 1 10 1 7 2 7 7 9 1", "output": "100\n7 3\n7 3\n7 3\n8 7\n2 7\n3 4\n6 10\n5 8\n1 7\n9 7\n3 4\n6 5\n3 4\n4 6\n10 1\n6 10\n9 7\n4 6\n2 7\n3 4\n7 3\n4 10\n10 5\n10 5\n10 5\n7 3\n7 3\n10 2\n7 3\n8 7\n5 2\n5 8\n6 10\n4 6\n6 10\n4 6\n3 4\n7 3\n1 7\n7 3\n6 5\n10 1\n7 3\n7 3\n3 4\n6 10\n8 7\n3 4\n3 4\n6 10\n10 9\n3 6\n3 4\n3 4\n5 2\n7 3\n6 10\n4 6\n4 6\n4 6\n5 7\n4 6\n4 6\n6 5\n1 7\n2 7\n6 10\n3 4\n4 6\n3 4\n10 1\n9 7\n3 4\n5 2\n10 9\n9 7\n7 3\n7 3\n1 7\n4 6\n3 4\n6 10\n10 9\n6 10\n10 1\n3 4\n1 7\n4 6\n10 9\n10 1\n4 6\n3 4\n2 7\n7 3\n4 10\n5 8\n7..." }, { "input": "100 10\n4 1 1 4 3 2 3 7 6 4 4 3 3 6 5 3 4 1 4 1 9 10 4 4 8 2 7 3 2 2 3 6 5 4 5 5 9 5 3 1 2 2 5 5 1 8 1 5 3 3 3 4 5 1 2 4 2 1 5 2 8 5 4 1 1 9 1 5 2 8 7 5 4 4 2 5 5 3 4 2 1 4 4 1 10 2 3 8 4 5 3 2 1 5 4 5 3 1 5 1", "output": "100\n5 4\n5 4\n5 4\n10 5\n6 5\n4 1\n3 2\n2 5\n8 5\n9 5\n5 4\n3 2\n4 1\n1 3\n2 9\n1 2\n9 5\n1 3\n7 5\n5 4\n5 4\n1 3\n3 2\n3 8\n3 8\n5 4\n5 4\n2 6\n5 4\n10 5\n2 6\n2 5\n1 2\n4 1\n3 2\n1 3\n4 1\n5 4\n8 5\n5 4\n3 2\n3 8\n5 4\n5 4\n4 1\n3 2\n6 5\n4 1\n4 1\n3 2\n2 7\n4 1\n4 1\n4 1\n2 6\n5 4\n1 3\n4 1\n4 3\n1 3\n2 5\n1 3\n1 3\n3 2\n8 5\n6 5\n3 2\n4 1\n4 3\n4 1\n2 9\n9 5\n4 1\n2 10\n2 7\n7 5\n5 4\n5 4\n8 5\n1 3\n4 1\n1 2\n2 9\n1 2\n3 8\n4 1\n8 5\n1 3\n2 7\n3 8\n1 3\n4 1\n7 5\n5 4\n1 3\n2 10\n5 4\n3 2\n1 3\n5 4" }, { "input": "100 10\n6 8 4 4 6 6 4 1 4 1 6 1 4 6 1 10 1 4 6 10 6 1 6 1 6 4 4 1 4 9 1 6 1 1 4 6 4 4 6 6 4 6 1 1 1 1 6 4 1 6 1 6 1 1 6 4 1 8 6 4 6 4 2 4 6 4 4 6 4 6 6 1 6 1 4 1 4 6 4 10 1 1 6 1 6 6 4 1 6 1 1 4 3 4 4 4 4 4 4 5", "output": "100\n4 6\n4 6\n4 6\n3 4\n9 4\n4 6\n6 1\n1 4\n1 4\n1 4\n4 6\n1 8\n6 1\n6 1\n1 4\n6 1\n10 4\n6 1\n8 4\n4 6\n4 6\n6 1\n1 9\n1 3\n1 5\n4 6\n4 6\n1 4\n4 6\n2 4\n1 4\n1 4\n6 1\n6 1\n6 1\n6 1\n4 6\n4 6\n1 4\n4 6\n6 10\n1 4\n4 6\n4 6\n4 6\n6 1\n5 4\n4 6\n4 6\n6 10\n1 4\n6 1\n4 6\n4 6\n1 4\n4 6\n6 1\n6 1\n6 1\n6 1\n1 4\n6 1\n6 1\n6 10\n1 4\n8 4\n6 1\n4 6\n6 1\n4 6\n1 4\n1 4\n4 1\n1 4\n1 4\n10 4\n4 6\n4 6\n1 4\n6 1\n4 1\n6 1\n1 4\n6 1\n1 2\n4 6\n1 4\n6 1\n1 4\n1 4\n6 1\n4 6\n10 4\n4 6\n6 1\n1 4\n4 6\n1 8\n6 1\n4 6" }, { "input": "100 10\n6 2 9 10 10 8 6 2 5 10 9 6 10 9 6 10 9 10 6 9 6 9 9 10 9 8 9 6 5 9 10 6 9 9 9 6 2 9 6 9 10 4 7 10 6 6 10 3 3 9 6 4 6 10 6 7 6 7 10 6 5 10 10 9 6 10 9 6 6 6 1 3 5 10 2 7 6 6 9 3 6 9 5 9 9 1 10 10 1 10 6 7 10 10 6 9 3 9 9 4", "output": "100\n6 9\n6 9\n6 9\n8 6\n1 6\n9 10\n10 3\n5 6\n3 6\n2 6\n6 9\n10 2\n9 10\n9 10\n10 1\n10 7\n2 6\n9 10\n4 6\n6 9\n6 9\n9 7\n10 2\n10 4\n10 2\n6 9\n6 9\n7 6\n6 9\n8 6\n7 6\n5 6\n10 5\n9 10\n10 5\n9 10\n6 10\n6 9\n3 6\n6 9\n10 3\n10 4\n6 9\n6 9\n6 9\n10 5\n1 6\n6 9\n6 9\n10 3\n7 6\n9 10\n6 9\n9 10\n5 6\n6 9\n9 7\n9 10\n9 10\n9 10\n3 6\n9 10\n9 10\n10 3\n3 6\n1 6\n10 3\n6 9\n9 10\n6 9\n10 1\n2 6\n9 10\n5 6\n7 8\n4 6\n6 9\n6 9\n2 6\n9 10\n9 10\n10 5\n10 8\n10 5\n10 4\n9 10\n3 6\n9 10\n7 6\n10 1\n9 10\n9 10\n4 6..." }, { "input": "100 10\n2 10 1 8 4 6 10 5 7 5 7 1 7 2 9 5 1 9 9 7 3 9 6 7 4 3 9 6 6 2 3 1 5 1 3 9 7 10 2 5 2 4 8 6 10 3 7 2 8 9 3 10 3 9 10 3 8 4 8 3 7 4 2 10 2 8 4 9 5 4 2 1 10 3 6 9 5 2 6 10 1 2 7 10 6 10 10 2 9 2 10 10 4 6 8 8 2 3 4 8", "output": "100\n10 2\n10 2\n10 2\n1 10\n1 10\n2 3\n8 6\n4 1\n5 10\n5 10\n2 9\n7 6\n9 3\n3 8\n6 4\n8 7\n5 10\n3 8\n1 10\n2 9\n10 2\n3 7\n7 4\n7 4\n7 4\n10 2\n10 2\n6 5\n10 2\n1 10\n4 5\n4 1\n8 6\n9 3\n8 6\n3 8\n2 3\n10 2\n5 10\n2 9\n7 6\n6 4\n2 9\n10 2\n2 9\n8 6\n1 10\n2 9\n2 9\n7 6\n6 5\n9 3\n2 9\n9 3\n4 5\n2 9\n8 7\n9 3\n9 8\n3 8\n4 1\n9 8\n3 7\n7 6\n4 1\n1 10\n8 6\n2 9\n9 8\n2 9\n6 4\n5 10\n9 3\n4 1\n6 5\n5 10\n10 2\n10 2\n5 10\n3 8\n9 3\n8 7\n6 5\n8 7\n7 4\n9 3\n4 1\n3 8\n6 5\n6 4\n3 7\n2 3\n1 10\n10 2\n3 7\n4 1\n..." } ]
46
0
0
27,065
620
Professor GukiZ and Two Arrays
[ "binary search", "two pointers" ]
null
null
Professor GukiZ has two arrays of integers, a and b. Professor wants to make the sum of the elements in the array a *s**a* as close as possible to the sum of the elements in the array b *s**b*. So he wants to minimize the value *v*<==<=|*s**a*<=-<=*s**b*|. In one operation professor can swap some element from the array a and some element from the array b. For example if the array a is [5,<=1,<=3,<=2,<=4] and the array b is [3,<=3,<=2] professor can swap the element 5 from the array a and the element 2 from the array b and get the new array a [2,<=1,<=3,<=2,<=4] and the new array b [3,<=3,<=5]. Professor doesn't want to make more than two swaps. Find the minimal value *v* and some sequence of no more than two swaps that will lead to the such value *v*. Professor makes swaps one by one, each new swap he makes with the new arrays a and b.
The first line contains integer *n* (1<=≤<=*n*<=≤<=2000) — the number of elements in the array a. The second line contains *n* integers *a**i* (<=-<=109<=≤<=*a**i*<=≤<=109) — the elements of the array a. The third line contains integer *m* (1<=≤<=*m*<=≤<=2000) — the number of elements in the array b. The fourth line contains *m* integers *b**j* (<=-<=109<=≤<=*b**j*<=≤<=109) — the elements of the array b.
In the first line print the minimal value *v*<==<=|*s**a*<=-<=*s**b*| that can be got with no more than two swaps. The second line should contain the number of swaps *k* (0<=≤<=*k*<=≤<=2). Each of the next *k* lines should contain two integers *x**p*,<=*y**p* (1<=≤<=*x**p*<=≤<=*n*,<=1<=≤<=*y**p*<=≤<=*m*) — the index of the element in the array a and the index of the element in the array b in the *p*-th swap. If there are several optimal solutions print any of them. Print the swaps in order the professor did them.
[ "5\n5 4 3 2 1\n4\n1 1 1 1\n", "5\n1 2 3 4 5\n1\n15\n", "5\n1 2 3 4 5\n4\n1 2 3 4\n" ]
[ "1\n2\n1 1\n4 2\n", "0\n0\n", "1\n1\n3 1\n" ]
none
[ { "input": "5\n5 4 3 2 1\n4\n1 1 1 1", "output": "1\n2\n1 1\n4 2" }, { "input": "5\n1 2 3 4 5\n1\n15", "output": "0\n0" }, { "input": "5\n1 2 3 4 5\n4\n1 2 3 4", "output": "1\n1\n3 1" }, { "input": "1\n-42\n1\n-86", "output": "44\n0" }, { "input": "1\n-21\n10\n-43 6 -46 79 -21 93 -36 -38 -67 1", "output": "1\n1\n1 3" }, { "input": "10\n87 -92 -67 -100 -88 80 -82 -59 81 -72\n10\n-50 30 30 77 65 92 -60 -76 -29 -15", "output": "0\n2\n4 4\n9 6" }, { "input": "6\n1 2 3 4 5 11\n1\n3", "output": "7\n1\n6 1" }, { "input": "2\n-2 -17\n2\n11 -9", "output": "5\n1\n1 1" } ]
1,840
72,601,600
0
27,123
111
Petya and Spiders
[ "bitmasks", "dp", "dsu" ]
C. Petya and Spiders
2
256
Little Petya loves training spiders. Petya has a board *n*<=×<=*m* in size. Each cell of the board initially has a spider sitting on it. After one second Petya chooses a certain action for each spider, and all of them humbly perform its commands. There are 5 possible commands: to stay idle or to move from current cell to some of the four side-neighboring cells (that is, one command for each of the four possible directions). Petya gives the commands so that no spider leaves the field. It is allowed for spiders to pass through each other when they crawl towards each other in opposite directions. All spiders crawl simultaneously and several spiders may end up in one cell. Petya wants to know the maximum possible number of spider-free cells after one second.
The first line contains two space-separated integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=40,<=*n*·*m*<=≤<=40) — the board sizes.
In the first line print the maximum number of cells without spiders.
[ "1 1\n", "2 3\n" ]
[ "0\n", "4\n" ]
In the first sample the only possible answer is: s In the second sample one of the possible solutions is: s denotes command "stay idle", l, r, d, u denote commands "crawl left", "crawl right", "crawl down", "crawl up", correspondingly.
[ { "input": "1 1", "output": "0" }, { "input": "2 3", "output": "4" }, { "input": "4 1", "output": "2" }, { "input": "4 2", "output": "5" }, { "input": "4 3", "output": "8" }, { "input": "4 4", "output": "12" }, { "input": "1 40", "output": "26" }, { "input": "1 1", "output": "0" }, { "input": "1 2", "output": "1" }, { "input": "1 3", "output": "2" }, { "input": "1 4", "output": "2" }, { "input": "1 5", "output": "3" }, { "input": "1 6", "output": "4" }, { "input": "1 7", "output": "4" }, { "input": "1 8", "output": "5" }, { "input": "1 9", "output": "6" }, { "input": "1 10", "output": "6" }, { "input": "1 11", "output": "7" }, { "input": "1 12", "output": "8" }, { "input": "1 13", "output": "8" }, { "input": "1 14", "output": "9" }, { "input": "1 15", "output": "10" }, { "input": "1 16", "output": "10" }, { "input": "1 17", "output": "11" }, { "input": "1 18", "output": "12" }, { "input": "1 19", "output": "12" }, { "input": "1 20", "output": "13" }, { "input": "1 21", "output": "14" }, { "input": "1 22", "output": "14" }, { "input": "1 23", "output": "15" }, { "input": "1 24", "output": "16" }, { "input": "1 25", "output": "16" }, { "input": "1 26", "output": "17" }, { "input": "1 27", "output": "18" }, { "input": "1 28", "output": "18" }, { "input": "1 29", "output": "19" }, { "input": "1 30", "output": "20" }, { "input": "1 31", "output": "20" }, { "input": "1 32", "output": "21" }, { "input": "1 33", "output": "22" }, { "input": "1 34", "output": "22" }, { "input": "1 35", "output": "23" }, { "input": "1 36", "output": "24" }, { "input": "1 37", "output": "24" }, { "input": "1 38", "output": "25" }, { "input": "1 39", "output": "26" }, { "input": "1 40", "output": "26" }, { "input": "2 1", "output": "1" }, { "input": "2 2", "output": "2" }, { "input": "2 3", "output": "4" }, { "input": "2 4", "output": "5" }, { "input": "2 5", "output": "7" }, { "input": "2 6", "output": "8" }, { "input": "2 7", "output": "10" }, { "input": "2 8", "output": "11" }, { "input": "2 9", "output": "13" }, { "input": "2 10", "output": "14" }, { "input": "2 11", "output": "16" }, { "input": "2 12", "output": "17" }, { "input": "2 13", "output": "19" }, { "input": "2 14", "output": "20" }, { "input": "2 15", "output": "22" }, { "input": "2 16", "output": "23" }, { "input": "2 17", "output": "25" }, { "input": "2 18", "output": "26" }, { "input": "2 19", "output": "28" }, { "input": "2 20", "output": "29" }, { "input": "3 1", "output": "2" }, { "input": "3 2", "output": "4" }, { "input": "3 3", "output": "6" }, { "input": "3 4", "output": "8" }, { "input": "3 5", "output": "11" }, { "input": "3 6", "output": "13" }, { "input": "3 7", "output": "15" }, { "input": "3 8", "output": "17" }, { "input": "3 9", "output": "20" }, { "input": "3 10", "output": "22" }, { "input": "3 11", "output": "24" }, { "input": "3 12", "output": "26" }, { "input": "3 13", "output": "29" }, { "input": "4 1", "output": "2" }, { "input": "4 2", "output": "5" }, { "input": "4 3", "output": "8" }, { "input": "4 4", "output": "12" }, { "input": "4 5", "output": "14" }, { "input": "4 6", "output": "17" }, { "input": "4 7", "output": "21" }, { "input": "4 8", "output": "24" }, { "input": "4 9", "output": "26" }, { "input": "4 10", "output": "30" }, { "input": "5 1", "output": "3" }, { "input": "5 2", "output": "7" }, { "input": "5 3", "output": "11" }, { "input": "5 4", "output": "14" }, { "input": "5 5", "output": "18" }, { "input": "5 6", "output": "22" }, { "input": "5 7", "output": "26" }, { "input": "5 8", "output": "29" }, { "input": "6 1", "output": "4" }, { "input": "6 2", "output": "8" }, { "input": "6 3", "output": "13" }, { "input": "6 4", "output": "17" }, { "input": "6 5", "output": "22" }, { "input": "6 6", "output": "26" }, { "input": "7 1", "output": "4" }, { "input": "7 2", "output": "10" }, { "input": "7 3", "output": "15" }, { "input": "7 4", "output": "21" }, { "input": "7 5", "output": "26" }, { "input": "8 1", "output": "5" }, { "input": "8 2", "output": "11" }, { "input": "8 3", "output": "17" }, { "input": "8 4", "output": "24" }, { "input": "8 5", "output": "29" }, { "input": "9 1", "output": "6" }, { "input": "9 2", "output": "13" }, { "input": "9 3", "output": "20" }, { "input": "9 4", "output": "26" }, { "input": "10 1", "output": "6" }, { "input": "10 2", "output": "14" }, { "input": "10 3", "output": "22" }, { "input": "10 4", "output": "30" }, { "input": "11 1", "output": "7" }, { "input": "11 2", "output": "16" }, { "input": "11 3", "output": "24" }, { "input": "12 1", "output": "8" }, { "input": "12 2", "output": "17" }, { "input": "12 3", "output": "26" }, { "input": "13 1", "output": "8" }, { "input": "13 2", "output": "19" }, { "input": "13 3", "output": "29" }, { "input": "14 1", "output": "9" }, { "input": "14 2", "output": "20" }, { "input": "15 1", "output": "10" }, { "input": "15 2", "output": "22" }, { "input": "16 1", "output": "10" }, { "input": "16 2", "output": "23" }, { "input": "17 1", "output": "11" }, { "input": "17 2", "output": "25" }, { "input": "18 1", "output": "12" }, { "input": "18 2", "output": "26" }, { "input": "19 1", "output": "12" }, { "input": "19 2", "output": "28" }, { "input": "20 1", "output": "13" }, { "input": "20 2", "output": "29" }, { "input": "21 1", "output": "14" }, { "input": "22 1", "output": "14" }, { "input": "23 1", "output": "15" }, { "input": "24 1", "output": "16" }, { "input": "25 1", "output": "16" }, { "input": "26 1", "output": "17" }, { "input": "27 1", "output": "18" }, { "input": "28 1", "output": "18" }, { "input": "29 1", "output": "19" }, { "input": "30 1", "output": "20" }, { "input": "31 1", "output": "20" }, { "input": "32 1", "output": "21" }, { "input": "33 1", "output": "22" }, { "input": "34 1", "output": "22" }, { "input": "35 1", "output": "23" }, { "input": "36 1", "output": "24" }, { "input": "37 1", "output": "24" }, { "input": "38 1", "output": "25" }, { "input": "39 1", "output": "26" }, { "input": "40 1", "output": "26" } ]
31
0
0
27,172
729
Subordinates
[ "constructive algorithms", "data structures", "graphs", "greedy", "sortings" ]
null
null
There are *n* workers in a company, each of them has a unique id from 1 to *n*. Exaclty one of them is a chief, his id is *s*. Each worker except the chief has exactly one immediate superior. There was a request to each of the workers to tell how how many superiors (not only immediate). Worker's superiors are his immediate superior, the immediate superior of the his immediate superior, and so on. For example, if there are three workers in the company, from which the first is the chief, the second worker's immediate superior is the first, the third worker's immediate superior is the second, then the third worker has two superiors, one of them is immediate and one not immediate. The chief is a superior to all the workers except himself. Some of the workers were in a hurry and made a mistake. You are to find the minimum number of workers that could make a mistake.
The first line contains two positive integers *n* and *s* (1<=≤<=*n*<=≤<=2·105, 1<=≤<=*s*<=≤<=*n*) — the number of workers and the id of the chief. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=≤<=*n*<=-<=1), where *a**i* is the number of superiors (not only immediate) the worker with id *i* reported about.
Print the minimum number of workers that could make a mistake.
[ "3 2\n2 0 2\n", "5 3\n1 0 0 4 1\n" ]
[ "1\n", "2\n" ]
In the first example it is possible that only the first worker made a mistake. Then: - the immediate superior of the first worker is the second worker, - the immediate superior of the third worker is the first worker, - the second worker is the chief.
[ { "input": "3 2\n2 0 2", "output": "1" }, { "input": "5 3\n1 0 0 4 1", "output": "2" }, { "input": "1 1\n0", "output": "0" }, { "input": "2 1\n0 0", "output": "1" }, { "input": "2 1\n0 1", "output": "0" }, { "input": "2 1\n1 0", "output": "2" }, { "input": "2 1\n1 1", "output": "1" }, { "input": "2 2\n0 0", "output": "1" }, { "input": "2 2\n0 1", "output": "2" }, { "input": "9 1\n0 1 1 1 1 1 6 7 8", "output": "3" }, { "input": "9 1\n0 1 1 1 1 5 6 7 8", "output": "3" }, { "input": "6 1\n0 1 2 2 0 0", "output": "2" }, { "input": "2 2\n1 1", "output": "1" }, { "input": "2 2\n1 0", "output": "0" }, { "input": "3 1\n0 1 2", "output": "0" }, { "input": "3 1\n2 1 1", "output": "1" }, { "input": "3 1\n0 0 2", "output": "1" }, { "input": "3 2\n2 0 1", "output": "0" }, { "input": "3 2\n2 2 1", "output": "1" }, { "input": "3 2\n2 1 1", "output": "1" }, { "input": "3 3\n1 1 0", "output": "0" }, { "input": "3 3\n2 1 2", "output": "1" }, { "input": "3 3\n2 1 0", "output": "0" }, { "input": "3 2\n2 2 2", "output": "2" }, { "input": "5 5\n0 1 1 0 0", "output": "2" }, { "input": "7 1\n4 4 6 6 6 6 5", "output": "4" }, { "input": "10 6\n3 0 0 0 0 0 0 1 0 0", "output": "7" }, { "input": "5 1\n0 0 1 3 4", "output": "1" }, { "input": "9 1\n0 0 0 2 5 5 5 5 5", "output": "3" }, { "input": "6 1\n5 2 1 3 3 1", "output": "1" }, { "input": "3 1\n1 2 2", "output": "2" } ]
61
2,764,800
-1
27,203
176
Archaeology
[ "data structures", "dfs and similar", "trees" ]
null
null
This time you should help a team of researchers on an island in the Pacific Ocean. They research the culture of the ancient tribes that used to inhabit the island many years ago. Overall they've dug out *n* villages. Some pairs of villages were connected by roads. People could go on the roads in both directions. Overall there were exactly *n*<=-<=1 roads, and from any village one could get to any other one. The tribes were not peaceful and they had many wars. As a result of the wars, some villages were destroyed completely. During more peaceful years some of the villages were restored. At each moment of time people used only those roads that belonged to some shortest way between two villages that existed at the given moment. In other words, people used the minimum subset of roads in such a way, that it was possible to get from any existing village to any other existing one. Note that throughout the island's whole history, there existed exactly *n*<=-<=1 roads that have been found by the researchers. There never were any other roads. The researchers think that observing the total sum of used roads’ lengths at different moments of time can help to better understand the tribes' culture and answer several historical questions. You will be given the full history of the tribes' existence. Your task is to determine the total length of used roads at some moments of time.
The first line contains an integer *n* (1<=≤<=*n*<=≤<=105) — the number of villages. The next *n*<=-<=1 lines describe the roads. The *i*-th of these lines contains three integers *a**i*, *b**i* and *c**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*, *a**i*<=≠<=*b**i*, 1<=≤<=*c**i*<=≤<=109, 1<=≤<=*i*<=&lt;<=*n*) — the numbers of villages that are connected by the *i*-th road and the road's length. The numbers in the lines are separated by a space. The next line contains an integer *q* (1<=≤<=*q*<=≤<=105) — the number of queries. Then follow *q* queries, one per line, ordered by time. Each query belongs to one of three types: - "+ *x*" — village number *x* is restored (1<=≤<=*x*<=≤<=*n*). - "- *x*" — village number *x* is destroyed (1<=≤<=*x*<=≤<=*n*). - "?" — the archaeologists want to know the total length of the roads which were used for that time period. It is guaranteed that the queries do not contradict each other, that is, there won't be queries to destroy non-existing villages or restore the already existing ones. It is guaranteed that we have at least one query of type "?". It is also guaranteed that one can get from any village to any other one by the given roads. At the initial moment of time no village is considered to exist.
For each query of type "?" print the total length of used roads on a single line. You should print the answers to the queries in the order, in which they are given in the input. Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
[ "6\n1 2 1\n1 3 5\n4 1 7\n4 5 3\n6 4 2\n10\n+ 3\n+ 1\n?\n+ 6\n?\n+ 5\n?\n- 6\n- 3\n?\n" ]
[ "5\n14\n17\n10\n" ]
none
[]
92
0
0
27,219
37
Old Berland Language
[ "data structures", "greedy", "trees" ]
C. Old Berland Language
2
256
Berland scientists know that the Old Berland language had exactly *n* words. Those words had lengths of *l*1,<=*l*2,<=...,<=*l**n* letters. Every word consisted of two letters, 0 and 1. Ancient Berland people spoke quickly and didn’t make pauses between the words, but at the same time they could always understand each other perfectly. It was possible because no word was a prefix of another one. The prefix of a string is considered to be one of its substrings that starts from the initial symbol. Help the scientists determine whether all the words of the Old Berland language can be reconstructed and if they can, output the words themselves.
The first line contains one integer *N* (1<=≤<=*N*<=≤<=1000) — the number of words in Old Berland language. The second line contains *N* space-separated integers — the lengths of these words. All the lengths are natural numbers not exceeding 1000.
If there’s no such set of words, in the single line output NO. Otherwise, in the first line output YES, and in the next *N* lines output the words themselves in the order their lengths were given in the input file. If the answer is not unique, output any.
[ "3\n1 2 3\n", "3\n1 1 1\n" ]
[ "YES\n0\n10\n110\n", "NO\n" ]
none
[ { "input": "3\n1 2 3", "output": "YES\n0\n10\n110" }, { "input": "3\n1 1 1", "output": "NO" }, { "input": "10\n4 4 4 4 4 4 4 4 4 4", "output": "YES\n0000\n0001\n0010\n0011\n0100\n0101\n0110\n0111\n1000\n1001" }, { "input": "20\n6 7 7 7 7 6 7 7 7 7 7 7 7 7 7 7 7 7 6 7", "output": "YES\n000000\n0000110\n0000111\n0001000\n0001001\n000001\n0001010\n0001011\n0001100\n0001101\n0001110\n0001111\n0010000\n0010001\n0010010\n0010011\n0010100\n0010101\n000010\n0010110" }, { "input": "30\n9 10 8 10 10 10 10 10 7 7 10 10 10 10 10 10 10 10 10 10 9 10 10 10 10 10 10 10 4 3", "output": "YES\n001101010\n0011011000\n00110100\n0011011001\n0011011010\n0011011011\n0011011100\n0011011101\n0011000\n0011001\n0011011110\n0011011111\n0011100000\n0011100001\n0011100010\n0011100011\n0011100100\n0011100101\n0011100110\n0011100111\n001101011\n0011101000\n0011101001\n0011101010\n0011101011\n0011101100\n0011101101\n0011101110\n0010\n000" }, { "input": "50\n10 10 10 10 10 10 9 9 10 10 9 10 10 10 10 9 8 7 8 10 10 10 7 9 10 10 9 9 9 10 10 10 9 10 10 10 10 10 9 10 10 7 8 9 9 8 9 6 6 6", "output": "YES\n0001110010\n0001110011\n0001110100\n0001110101\n0001110110\n0001110111\n000101100\n000101101\n0001111000\n0001111001\n000101110\n0001111010\n0001111011\n0001111100\n0001111101\n000101111\n00010010\n0000110\n00010011\n0001111110\n0001111111\n0010000000\n0000111\n000110000\n0010000001\n0010000010\n000110001\n000110010\n000110011\n0010000011\n0010000100\n0010000101\n000110100\n0010000110\n0010000111\n0010001000\n0010001001\n0010001010\n000110101\n0010001011\n0010001100\n0001000\n00010100\n000110110\n0001..." }, { "input": "100\n17 18 22 15 14 18 9 21 14 19 14 20 12 15 9 23 19 20 19 22 13 22 17 11 21 22 8 17 23 18 21 23 22 23 15 18 21 17 17 9 13 21 23 19 18 23 15 14 17 19 22 23 12 16 23 16 20 9 12 17 18 11 10 17 16 21 8 21 16 19 21 17 12 20 14 16 17 10 22 20 17 13 15 19 9 22 12 20 20 13 19 16 18 7 15 18 6 18 19 21", "output": "YES\n00001011110101100\n000010111101101110\n0000101111011111100110\n000010111100010\n00001011101100\n000010111101101111\n000010000\n000010111101111101010\n00001011101101\n0000101111011101110\n00001011101110\n00001011110111101110\n000010110100\n000010111100011\n000010001\n00001011110111111011100\n0000101111011101111\n00001011110111101111\n0000101111011110000\n0000101111011111100111\n0000101110010\n0000101111011111101000\n00001011110101101\n00001011000\n000010111101111101011\n0000101111011111101001\n00000110..." }, { "input": "20\n4 4 3 4 4 4 4 4 4 4 4 3 3 2 1 4 4 3 3 3", "output": "NO" }, { "input": "30\n6 7 7 6 7 7 7 7 7 7 7 7 7 7 7 5 7 7 7 7 2 1 5 3 7 3 2 7 5 1", "output": "NO" }, { "input": "65\n7 8 6 9 10 9 10 10 9 10 10 10 10 10 10 9 9 10 9 10 10 6 9 7 7 6 8 10 10 8 4 5 2 3 5 3 6 5 2 4 10 4 2 8 10 1 1 4 5 3 8 5 6 7 6 1 10 5 2 8 4 9 1 2 7", "output": "NO" }, { "input": "85\n7 9 8 9 5 6 9 8 10 10 9 10 10 10 10 7 7 4 8 7 7 7 9 10 10 9 10 9 10 10 10 8 8 10 10 10 10 10 10 10 10 10 10 10 10 10 10 9 7 10 4 2 9 3 3 6 2 6 5 6 4 1 7 3 7 7 5 8 4 5 4 1 10 2 9 3 1 4 2 9 9 3 5 6 8", "output": "NO" }, { "input": "10\n4 4 4 4 4 4 4 4 2 2", "output": "YES\n1000\n1001\n1010\n1011\n1100\n1101\n1110\n1111\n00\n01" }, { "input": "20\n5 4 5 5 5 6 5 6 4 5 6 4 5 4 2 4 6 4 4 5", "output": "YES\n10110\n0100\n10111\n11000\n11001\n111100\n11010\n111101\n0101\n11011\n111110\n0110\n11100\n0111\n00\n1000\n111111\n1001\n1010\n11101" }, { "input": "30\n7 8 6 4 2 8 8 7 7 10 4 6 4 7 4 4 7 6 7 9 7 3 5 5 10 4 5 8 5 8", "output": "YES\n1110110\n11111010\n111000\n0110\n00\n11111011\n11111100\n1110111\n1111000\n1111111110\n0111\n111001\n1000\n1111001\n1001\n1010\n1111010\n111010\n1111011\n111111110\n1111100\n010\n11000\n11001\n1111111111\n1011\n11010\n11111101\n11011\n11111110" }, { "input": "50\n4 7 9 7 7 5 5 5 8 9 9 7 9 7 7 6 5 6 4 9 6 5 6 6 5 7 7 6 6 6 5 8 2 7 8 7 6 5 7 9 8 7 5 6 6 8 6 6 7 7", "output": "YES\n0100\n1101110\n111111010\n1101111\n1110000\n01100\n01101\n01110\n11111000\n111111011\n111111100\n1110001\n111111101\n1110010\n1110011\n101010\n01111\n101011\n0101\n111111110\n101100\n10000\n101101\n101110\n10001\n1110100\n1110101\n101111\n110000\n110001\n10010\n11111001\n00\n1110110\n11111010\n1110111\n110010\n10011\n1111000\n111111111\n11111011\n1111001\n10100\n110011\n110100\n11111100\n110101\n110110\n1111010\n1111011" }, { "input": "100\n8 12 6 10 6 11 5 11 11 7 5 6 7 9 11 9 4 11 7 8 12 7 7 7 10 9 6 6 5 7 6 7 10 7 8 7 7 8 9 8 5 10 7 7 6 6 7 8 12 11 5 7 12 10 7 7 10 7 11 11 5 7 6 10 8 6 8 11 8 7 5 7 7 5 6 8 7 7 10 6 5 9 5 5 11 8 7 6 7 8 8 7 7 7 6 6 6 5 8 7", "output": "YES\n11101100\n111111111100\n011100\n1111110010\n011101\n11111110100\n00010\n11111110101\n11111110110\n1011000\n00011\n011110\n1011001\n111110100\n11111110111\n111110101\n0000\n11111111000\n1011010\n11101101\n111111111101\n1011011\n1011100\n1011101\n1111110011\n111110110\n011111\n100000\n00100\n1011110\n100001\n1011111\n1111110100\n1100000\n11101110\n1100001\n1100010\n11101111\n111110111\n11110000\n00101\n1111110101\n1100011\n1100100\n100010\n100011\n1100101\n11110001\n111111111110\n11111111001\n00110\n110..." }, { "input": "20\n2 3 4 4 2 4 4 2 4 4 3 4 4 3 1 3 3 3 2 1", "output": "NO" }, { "input": "30\n6 6 6 5 6 7 3 4 6 5 2 4 6 4 5 4 6 5 4 4 6 6 2 1 4 4 6 1 6 7", "output": "NO" }, { "input": "65\n9 7 8 6 6 10 3 9 10 4 8 3 2 8 9 1 6 3 2 7 9 7 8 10 10 4 5 6 8 8 7 10 10 8 6 6 4 8 8 7 6 9 10 7 8 7 3 3 10 8 9 10 1 9 6 9 2 7 9 10 8 10 3 7 3", "output": "NO" }, { "input": "85\n9 10 4 5 10 4 10 4 5 7 4 8 10 10 9 6 10 10 7 1 10 8 4 4 7 6 3 9 4 4 9 6 3 3 8 9 8 8 10 6 10 10 4 9 6 9 4 3 4 5 8 6 1 5 9 9 9 7 10 10 7 10 4 4 8 2 1 8 10 10 7 1 3 10 7 10 4 5 10 1 10 8 6 2 10", "output": "NO" }, { "input": "200\n11 23 6 1 15 6 5 9 8 9 13 11 7 21 14 17 8 8 12 6 18 4 9 20 3 9 6 9 9 12 18 5 22 5 16 20 11 6 22 10 5 6 8 19 9 12 14 2 10 6 7 7 18 17 4 16 9 13 3 10 15 8 8 9 13 7 8 18 12 12 13 14 9 8 5 5 22 19 23 15 11 7 23 7 5 3 9 3 15 9 22 9 2 11 21 8 12 7 6 8 10 6 12 9 11 8 7 6 5 7 8 9 10 7 19 12 14 9 6 7 2 7 8 4 12 21 14 4 11 12 9 13 17 4 10 8 17 3 9 5 11 6 4 11 1 13 10 10 8 10 14 23 17 8 20 23 23 23 14 7 18 5 10 21 9 7 7 7 4 23 13 8 9 22 7 4 8 12 8 19 17 11 10 8 8 7 7 13 6 13 14 14 22 2 10 11 5 1 14 13", "output": "NO" }, { "input": "1\n1", "output": "YES\n0" }, { "input": "2\n1 1", "output": "YES\n0\n1" }, { "input": "2\n1000 1", "output": "YES\n10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..." }, { "input": "3\n1 1 2", "output": "NO" }, { "input": "3\n1 2 1000", "output": "YES\n0\n10\n1100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..." } ]
310
3,174,400
3.916587
27,273
543
Listening to Music
[ "constructive algorithms", "data structures" ]
null
null
Please note that the memory limit differs from the standard. You really love to listen to music. During the each of next *s* days you will listen to exactly *m* songs from the playlist that consists of exactly *n* songs. Let's number the songs from the playlist with numbers from 1 to *n*, inclusive. The quality of song number *i* is *a**i*. On the *i*-th day you choose some integer *v* (*l**i*<=≤<=*v*<=≤<=*r**i*) and listen to songs number *v*,<=*v*<=+<=1,<=...,<=*v*<=+<=*m*<=-<=1. On the *i*-th day listening to one song with quality less than *q**i* increases your displeasure by exactly one. Determine what minimum displeasure you can get on each of the *s* next days.
The first line contains two positive integers *n*, *m* (1<=≤<=*m*<=≤<=*n*<=≤<=2·105). The second line contains *n* positive integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=&lt;<=230) — the description of songs from the playlist. The next line contains a single number *s* (1<=≤<=*s*<=≤<=2·105) — the number of days that you consider. The next *s* lines contain three integers each *l**i*,<=*r**i*,<=*x**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*<=-<=*m*<=+<=1; 0<=≤<=*x**i*<=&lt;<=230) — the description of the parameters for the *i*-th day. In order to calculate value *q**i*, you need to use formula: , where *ans**i* is the answer to the problem for day *i*. Assume that *ans*0<==<=0.
Print exactly *s* integers *ans*1,<=*ans*2,<=...,<=*ans**s*, where *ans**i* is the minimum displeasure that you can get on day *i*.
[ "5 3\n1 2 1 2 3\n5\n1 1 2\n1 3 2\n1 3 3\n1 3 5\n1 3 1\n" ]
[ "2\n0\n2\n3\n1\n" ]
none
[]
46
0
0
27,405
141
Take-off Ramps
[ "graphs", "shortest paths" ]
null
null
Vasya participates in a ski race along the *X* axis. The start is at point 0, and the finish is at *L*, that is, at a distance *L* meters from the start in the positive direction of the axis. Vasya has been training so hard that he can run one meter in exactly one second. Besides, there are *n* take-off ramps on the track, each ramp is characterized by four numbers: - *x**i* represents the ramp's coordinate - *d**i* represents from how many meters Vasya will land if he goes down this ramp - *t**i* represents the flight time in seconds - *p**i* is the number, indicating for how many meters Vasya should gather speed to get ready and fly off the ramp. As Vasya gathers speed, he should ski on the snow (that is, he should not be flying), but his speed still equals one meter per second. Vasya is allowed to move in any direction on the *X* axis, but he is prohibited to cross the start line, that is go to the negative semiaxis. Vasya himself chooses which take-off ramps he will use and in what order, that is, he is not obliged to take off from all the ramps he encounters. Specifically, Vasya can skip the ramp. It is guaranteed that *x**i*<=+<=*d**i*<=≤<=*L*, that is, Vasya cannot cross the finish line in flight. Vasya can jump from the ramp only in the positive direction of *X* axis. More formally, when using the *i*-th ramp, Vasya starts gathering speed at point *x**i*<=-<=*p**i*, jumps at point *x**i*, and lands at point *x**i*<=+<=*d**i*. He cannot use the ramp in opposite direction. Your task is to find the minimum time that Vasya will spend to cover the distance.
The first line contains two integers *n* and *L* (0<=≤<=*n*<=≤<=105, 1<=≤<=*L*<=≤<=109). Then *n* lines contain the descriptions of the ramps, each description is on a single line. Each description is a group of four non-negative integers *x**i*, *d**i*, *t**i*, *p**i* (0<=≤<=*x**i*<=≤<=*L*, 1<=≤<=*d**i*,<=*t**i*,<=*p**i*<=≤<=109, *x**i*<=+<=*d**i*<=≤<=*L*).
Print in the first line the minimum time in seconds Vasya needs to complete the track. Print in the second line *k* — the number of take-off ramps that Vasya needs to use, and print on the third line of output *k* numbers the number the take-off ramps Vasya used in the order in which he used them. Print each number exactly once, separate the numbers with a space. The ramps are numbered starting from 1 in the order in which they are given in the input.
[ "2 20\n5 10 5 5\n4 16 1 7\n", "2 20\n9 8 12 6\n15 5 1 1\n" ]
[ "15\n1\n1 ", "16\n1\n2 " ]
In the first sample, Vasya cannot use ramp 2, because then he will need to gather speed starting from point -3, which is not permitted by the statement. The optimal option is using ramp 1, the resulting time is: moving to the point of gathering speed + gathering speed until reaching the takeoff ramp + flight time + moving to the finish line = 0 + 5 + 5 + 5 = 15. In the second sample using ramp 1 is not optimal for Vasya as *t*<sub class="lower-index">1</sub> &gt; *d*<sub class="lower-index">1</sub>. The optimal option is using ramp 2, the resulting time is: moving to the point of gathering speed + gathering speed until reaching the takeoff ramp + flight time + moving to the finish line = 14 + 1 + 1 + 0 = 16.
[ { "input": "2 20\n5 10 5 5\n4 16 1 7", "output": "15\n1\n1 " }, { "input": "2 20\n9 8 12 6\n15 5 1 1", "output": "16\n1\n2 " }, { "input": "0 10", "output": "10\n0" }, { "input": "1 20\n2 1 10 1", "output": "20\n0" }, { "input": "3 50\n44 3 4 47\n2 40 33 2\n28 16 3 31", "output": "43\n1\n2 " }, { "input": "0 100", "output": "100\n0" }, { "input": "5 100\n84 5 19 103\n45 39 105 63\n16 67 59 24\n6 89 104 8\n94 5 3 19", "output": "98\n1\n5 " }, { "input": "10 10\n8 1 4 1\n1 6 3 1\n1 5 6 1\n2 8 13 2\n6 4 8 1\n9 1 2 1\n7 3 1 2\n6 2 5 2\n3 2 10 1\n1 8 9 1", "output": "7\n1\n2 " }, { "input": "15 30\n12 7 13 1\n29 1 1 1\n20 2 10 3\n0 24 17 1\n24 1 1 11\n17 12 8 1\n27 3 1 1\n20 4 1 2\n1 29 11 1\n25 1 3 2\n13 3 7 1\n0 26 2 1\n9 10 11 1\n0 7 15 1\n26 1 4 4", "output": "12\n1\n9 " }, { "input": "0 1", "output": "1\n0" }, { "input": "0 1000000000", "output": "1000000000\n0" }, { "input": "0 10", "output": "10\n0" }, { "input": "1 20\n7 1 5 1", "output": "20\n0" }, { "input": "3 50\n28 1 1 3\n21 1 1 1\n34 1 1 1", "output": "50\n0" }, { "input": "0 100", "output": "100\n0" }, { "input": "5 100\n85 5 4 6\n46 5 6 10\n15 5 4 9\n25 3 6 3\n26 2 2 8", "output": "98\n2\n3 1 " }, { "input": "10 10\n3 2 2 91\n3 4 1 75\n7 3 6 47\n5 5 5 83\n1 2 2 18\n0 2 2 28\n5 5 3 88\n5 3 2 26\n9 1 5 90\n8 1 2 34", "output": "10\n0" }, { "input": "15 30\n9 3 32 1\n0 16 38 2\n1 8 50 2\n16 14 21 2\n5 18 68 3\n25 2 49 1\n17 6 52 2\n17 3 94 1\n1 25 40 2\n27 1 49 2\n29 1 31 3\n19 3 30 1\n20 4 17 3\n18 9 66 2\n8 20 78 3", "output": "30\n0" } ]
124
614,400
0
27,448
8
Two Friends
[ "binary search", "geometry" ]
D. Two Friends
1
64
Two neighbours, Alan and Bob, live in the city, where there are three buildings only: a cinema, a shop and the house, where they live. The rest is a big asphalt square. Once they went to the cinema, and the film impressed them so deeply, that when they left the cinema, they did not want to stop discussing it. Bob wants to get home, but Alan has to go to the shop first, and only then go home. So, they agreed to cover some distance together discussing the film (their common path might pass through the shop, or they might walk circles around the cinema together), and then to part each other's company and go each his own way. After they part, they will start thinking about their daily pursuits; and even if they meet again, they won't be able to go on with the discussion. Thus, Bob's path will be a continuous curve, having the cinema and the house as its ends. Alan's path — a continuous curve, going through the shop, and having the cinema and the house as its ends. The film ended late, that's why the whole distance covered by Alan should not differ from the shortest one by more than *t*1, and the distance covered by Bob should not differ from the shortest one by more than *t*2. Find the maximum distance that Alan and Bob will cover together, discussing the film.
The first line contains two integers: *t*1,<=*t*2 (0<=≤<=*t*1,<=*t*2<=≤<=100). The second line contains the cinema's coordinates, the third one — the house's, and the last line — the shop's. All the coordinates are given in meters, are integer, and do not exceed 100 in absolute magnitude. No two given places are in the same building.
In the only line output one number — the maximum distance that Alan and Bob will cover together, discussing the film. Output the answer accurate to not less than 4 decimal places.
[ "0 2\n0 0\n4 0\n-3 0\n", "0 0\n0 0\n2 0\n1 0\n" ]
[ "1.0000000000\n", "2.0000000000\n" ]
none
[ { "input": "0 2\n0 0\n4 0\n-3 0", "output": "1.0000000000" }, { "input": "0 0\n0 0\n2 0\n1 0", "output": "2.0000000000" }, { "input": "0 2\n0 0\n40 0\n-31 1", "output": "1.0002538218" }, { "input": "100 2\n0 0\n4 0\n-3 0", "output": "6.0000000000" }, { "input": "2 100\n0 0\n4 0\n-3 0", "output": "12.0000000000" }, { "input": "0 0\n0 0\n5 0\n10 0", "output": "5.0000000000" }, { "input": "2 0\n0 0\n5 0\n10 0", "output": "5.0000000000" }, { "input": "0 2\n0 0\n5 0\n10 0", "output": "6.0000000000" }, { "input": "0 0\n0 0\n4 0\n4 3", "output": "0.0000299999" }, { "input": "0 4\n0 0\n4 0\n4 3", "output": "8.0000000000" }, { "input": "0 3\n0 0\n4 0\n4 3", "output": "4.3421111488" }, { "input": "1 4\n0 0\n4 0\n4 3", "output": "8.0000000000" }, { "input": "0 0\n0 0\n100 100\n100 0", "output": "0.0000241421" }, { "input": "1 1\n0 0\n100 100\n100 0", "output": "11.8620549792" }, { "input": "0 0\n0 0\n-1 -1\n1 -1", "output": "0.0000099999" }, { "input": "0 0\n1 2\n1 -2\n1 1", "output": "4.0000000000" }, { "input": "2 1\n1 3\n0 1\n0 0", "output": "3.2360679775" }, { "input": "1 1\n1 1\n-1 -4\n4 -3", "output": "3.4140469620" }, { "input": "2 4\n3 1\n-5 -1\n2 2", "output": "11.0299866682" }, { "input": "1 1\n5 5\n8 -1\n-3 -3", "output": "3.4363113325" }, { "input": "0 8\n13 -2\n15 -16\n4 9", "output": "4.2383217359" }, { "input": "4 12\n1 -3\n-21 -29\n30 10", "output": "8.8182331094" }, { "input": "25 22\n22 -21\n14 -25\n8 21", "output": "26.9132525667" }, { "input": "10 20\n-2 -18\n30 26\n-14 38", "output": "47.7344371741" }, { "input": "12 3\n9 7\n-26 -30\n-27 33", "output": "19.0794600792" }, { "input": "31 21\n-42 42\n-2 10\n33 -40", "output": "72.2249938995" }, { "input": "23 68\n60 -68\n-70 13\n-50 26", "output": "191.5464988652" }, { "input": "29 26\n-33 2\n15 -42\n-52 48", "output": "28.8420189634" }, { "input": "4 5\n-84 52\n-20 -39\n51 91", "output": "20.5654840916" }, { "input": "43 30\n88 45\n-41 -76\n-26 -46", "output": "206.8671818060" }, { "input": "2 32\n-91 -7\n-69 -99\n3 -7", "output": "42.1044438082" }, { "input": "28 0\n-98 -73\n45 -19\n-82 -60", "output": "33.3590240317" }, { "input": "7 69\n75 -97\n34 89\n-21 19", "output": "246.5947108716" }, { "input": "1 45\n-52 36\n-86 7\n46 80", "output": "23.6287723817" }, { "input": "36 7\n64 -16\n18 -95\n50 46", "output": "26.4522291627" }, { "input": "0 5\n3 9\n-7 -10\n-9 -10", "output": "24.4722050542" }, { "input": "1 0\n4 4\n6 1\n6 -10", "output": "3.6055512755" }, { "input": "2 6\n77 86\n-48 -76\n-65 28", "output": "68.8370182423" }, { "input": "32 8\n-74 -85\n69 -13\n-59 64", "output": "80.9525769682" }, { "input": "7 0\n2 3\n-3 -8\n7 -9", "output": "10.3637557241" }, { "input": "3 0\n3 0\n-10 -5\n-8 10", "output": "4.3880652624" }, { "input": "48 3\n-17 -70\n20 91\n-92 -100", "output": "34.1872425101" }, { "input": "24 3\n-86 1\n3 70\n-71 -85", "output": "21.3881481586" }, { "input": "0 11\n7 -11\n0 19\n-13 -16", "output": "9.4432695006" }, { "input": "38 5\n-99 16\n91 45\n22 -70", "output": "105.0959316870" }, { "input": "5 0\n75 -56\n-12 24\n99 19", "output": "8.0208146392" }, { "input": "39 0\n-7 -57\n52 -77\n24 -98", "output": "57.7524735602" }, { "input": "0 5\n-20 -2\n16 -6\n16 -2", "output": "40.0000000000" }, { "input": "26 6\n98 -44\n60 67\n-41 -15", "output": "64.2681170638" }, { "input": "13 5\n-95 7\n-39 81\n65 -47", "output": "34.7382933870" }, { "input": "5 0\n-20 -13\n-9 -5\n10 -13", "output": "13.6014705087" }, { "input": "5 0\n8 -19\n-6 -12\n-15 -14", "output": "15.6524758425" }, { "input": "10 1\n-71 23\n51 92\n-72 10", "output": "7.4826465918" }, { "input": "4 0\n8 -4\n7 7\n-6 9", "output": "9.4951445784" }, { "input": "45 0\n85 34\n-21 -47\n28 44", "output": "56.8197308538" }, { "input": "5 0\n14 17\n20 5\n17 -20", "output": "13.4164078650" }, { "input": "80 3\n35 -55\n-36 -53\n-96 -53", "output": "74.0281634283" }, { "input": "3 4\n-80 -78\n23 -81\n-49 -50", "output": "27.3700259542" }, { "input": "82 92\n-85 44\n19 -65\n-47 -1", "output": "232.8329767670" }, { "input": "1 65\n-56 -85\n7 34\n-38 68", "output": "148.7518367529" }, { "input": "83 99\n53 100\n-74 -28\n-29 32", "output": "264.5269918847" }, { "input": "65 12\n41 38\n5 4\n-96 -53", "output": "61.5176736126" }, { "input": "95 75\n-99 -26\n55 -1\n72 17", "output": "231.0160248180" }, { "input": "68 11\n-85 65\n-70 61\n38 12", "output": "26.5241746963" }, { "input": "18 26\n68 67\n-46 -36\n-46 95", "output": "84.4001524822" }, { "input": "72 52\n-47 -88\n33 1\n88 51", "output": "171.6703806295" }, { "input": "39 99\n-93 -91\n66 -6\n87 -12", "output": "257.4134712717" }, { "input": "38 23\n2 45\n49 2\n87 -69", "output": "86.7024332345" }, { "input": "83 0\n94 -62\n0 -34\n-87 49", "output": "98.0815986819" }, { "input": "16 68\n31 -70\n10 17\n4 30", "output": "133.8987073310" }, { "input": "67 10\n92 -2\n-58 -79\n70 86", "output": "61.1979627104" }, { "input": "49 9\n-91 -29\n-54 -72\n73 6", "output": "65.7274184147" }, { "input": "48 59\n-16 -32\n-64 76\n-26 49", "output": "176.2303976942" }, { "input": "85 4\n94 100\n-16 40\n45 -26", "output": "129.2996408614" }, { "input": "0 2\n0 0\n40 0\n-31 0", "output": "1.0000000000" } ]
62
0
0
27,550
755
PolandBall and White-Red graph
[ "constructive algorithms", "graphs", "shortest paths" ]
null
null
PolandBall has an undirected simple graph consisting of *n* vertices. Unfortunately, it has no edges. The graph is very sad because of that. PolandBall wanted to make it happier, adding some red edges. Then, he will add white edges in every remaining place. Therefore, the final graph will be a clique in two colors: white and red. Colorfulness of the graph is a value *min*(*d**r*,<=*d**w*), where *d**r* is the diameter of the red subgraph and *d**w* is the diameter of white subgraph. The diameter of a graph is a largest value *d* such that shortest path between some pair of vertices in it is equal to *d*. If the graph is not connected, we consider its diameter to be -1. PolandBall wants the final graph to be as neat as possible. He wants the final colorfulness to be equal to *k*. Can you help him and find any graph which satisfies PolandBall's requests?
The only one input line contains two integers *n* and *k* (2<=≤<=*n*<=≤<=1000, 1<=≤<=*k*<=≤<=1000), representing graph's size and sought colorfulness.
If it's impossible to find a suitable graph, print -1. Otherwise, you can output any graph which fulfills PolandBall's requirements. First, output *m* — the number of red edges in your graph. Then, you should output *m* lines, each containing two integers *a**i* and *b**i*, (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*, *a**i*<=≠<=*b**i*) which means that there is an undirected red edge between vertices *a**i* and *b**i*. Every red edge should be printed exactly once, you can print the edges and the vertices of every edge in arbitrary order. Remember that PolandBall's graph should remain simple, so no loops or multiple edges are allowed.
[ "4 1\n", "5 2\n" ]
[ "-1\n", "4\n1 2\n2 3\n3 4\n4 5\n" ]
In the first sample case, no graph can fulfill PolandBall's requirements. In the second sample case, red graph is a path from 1 to 5. Its diameter is 4. However, white graph has diameter 2, because it consists of edges 1-3, 1-4, 1-5, 2-4, 2-5, 3-5.
[ { "input": "4 1", "output": "-1" }, { "input": "5 2", "output": "4\n1 2\n2 3\n3 4\n4 5" }, { "input": "500 3", "output": "123755\n1 2\n499 500\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n2 32\n2 33\n2 34\n2 35\n2 36\n2 37\n2 38\n2 39\n2 40\n2 41\n2 42\n2 43\n2 44\n2 45\n2 46\n2 47\n2 48\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n2 69\n2 70\n2 71\n2 72\n2 73\n2 74\n2 75\n2 76\n2 77\n2 78\n2 79\n2 80\n2 81\n2 82\n2 83\n2 84\n2 85..." }, { "input": "1000 2", "output": "999\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76..." }, { "input": "10 2", "output": "9\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10" }, { "input": "590 3", "output": "172580\n1 2\n589 590\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n2 32\n2 33\n2 34\n2 35\n2 36\n2 37\n2 38\n2 39\n2 40\n2 41\n2 42\n2 43\n2 44\n2 45\n2 46\n2 47\n2 48\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n2 69\n2 70\n2 71\n2 72\n2 73\n2 74\n2 75\n2 76\n2 77\n2 78\n2 79\n2 80\n2 81\n2 82\n2 83\n2 84\n2 85..." }, { "input": "1000 5", "output": "-1" }, { "input": "5 3", "output": "5\n1 2\n4 5\n2 3\n2 4\n3 4" }, { "input": "100 49", "output": "-1" }, { "input": "4 2", "output": "-1" }, { "input": "4 3", "output": "3\n1 2\n3 4\n2 3" }, { "input": "5 4", "output": "-1" }, { "input": "5 1", "output": "-1" }, { "input": "7 2", "output": "6\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7" }, { "input": "1000 1", "output": "-1" }, { "input": "1000 3", "output": "497505\n1 2\n999 1000\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n2 32\n2 33\n2 34\n2 35\n2 36\n2 37\n2 38\n2 39\n2 40\n2 41\n2 42\n2 43\n2 44\n2 45\n2 46\n2 47\n2 48\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n2 69\n2 70\n2 71\n2 72\n2 73\n2 74\n2 75\n2 76\n2 77\n2 78\n2 79\n2 80\n2 81\n2 82\n2 83\n2 84\n2 8..." }, { "input": "1000 4", "output": "-1" }, { "input": "999 1", "output": "-1" }, { "input": "999 2", "output": "998\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76..." }, { "input": "999 3", "output": "496508\n1 2\n998 999\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n2 32\n2 33\n2 34\n2 35\n2 36\n2 37\n2 38\n2 39\n2 40\n2 41\n2 42\n2 43\n2 44\n2 45\n2 46\n2 47\n2 48\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n2 69\n2 70\n2 71\n2 72\n2 73\n2 74\n2 75\n2 76\n2 77\n2 78\n2 79\n2 80\n2 81\n2 82\n2 83\n2 84\n2 85..." }, { "input": "999 4", "output": "-1" }, { "input": "999 5", "output": "-1" }, { "input": "8 2", "output": "7\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8" }, { "input": "9 2", "output": "8\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9" }, { "input": "6 3", "output": "8\n1 2\n5 6\n2 3\n2 4\n2 5\n3 4\n3 5\n4 5" }, { "input": "7 3", "output": "12\n1 2\n6 7\n2 3\n2 4\n2 5\n2 6\n3 4\n3 5\n3 6\n4 5\n4 6\n5 6" }, { "input": "8 3", "output": "17\n1 2\n7 8\n2 3\n2 4\n2 5\n2 6\n2 7\n3 4\n3 5\n3 6\n3 7\n4 5\n4 6\n4 7\n5 6\n5 7\n6 7" }, { "input": "9 3", "output": "23\n1 2\n8 9\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n3 4\n3 5\n3 6\n3 7\n3 8\n4 5\n4 6\n4 7\n4 8\n5 6\n5 7\n5 8\n6 7\n6 8\n7 8" }, { "input": "10 3", "output": "30\n1 2\n9 10\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n3 4\n3 5\n3 6\n3 7\n3 8\n3 9\n4 5\n4 6\n4 7\n4 8\n4 9\n5 6\n5 7\n5 8\n5 9\n6 7\n6 8\n6 9\n7 8\n7 9\n8 9" }, { "input": "527 2", "output": "526\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76..." }, { "input": "218 2", "output": "217\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76..." }, { "input": "565 2", "output": "564\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76..." }, { "input": "11 2", "output": "10\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11" }, { "input": "237 2", "output": "236\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76..." }, { "input": "107 2", "output": "106\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76..." }, { "input": "494 2", "output": "493\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76..." }, { "input": "40 2", "output": "39\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40" }, { "input": "301 2", "output": "300\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76..." }, { "input": "101 2", "output": "100\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76..." }, { "input": "642 3", "output": "204482\n1 2\n641 642\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n2 32\n2 33\n2 34\n2 35\n2 36\n2 37\n2 38\n2 39\n2 40\n2 41\n2 42\n2 43\n2 44\n2 45\n2 46\n2 47\n2 48\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n2 69\n2 70\n2 71\n2 72\n2 73\n2 74\n2 75\n2 76\n2 77\n2 78\n2 79\n2 80\n2 81\n2 82\n2 83\n2 84\n2 85..." }, { "input": "683 3", "output": "231542\n1 2\n682 683\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n2 32\n2 33\n2 34\n2 35\n2 36\n2 37\n2 38\n2 39\n2 40\n2 41\n2 42\n2 43\n2 44\n2 45\n2 46\n2 47\n2 48\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n2 69\n2 70\n2 71\n2 72\n2 73\n2 74\n2 75\n2 76\n2 77\n2 78\n2 79\n2 80\n2 81\n2 82\n2 83\n2 84\n2 85..." }, { "input": "750 3", "output": "279380\n1 2\n749 750\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n2 32\n2 33\n2 34\n2 35\n2 36\n2 37\n2 38\n2 39\n2 40\n2 41\n2 42\n2 43\n2 44\n2 45\n2 46\n2 47\n2 48\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n2 69\n2 70\n2 71\n2 72\n2 73\n2 74\n2 75\n2 76\n2 77\n2 78\n2 79\n2 80\n2 81\n2 82\n2 83\n2 84\n2 85..." }, { "input": "851 3", "output": "359978\n1 2\n850 851\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n2 32\n2 33\n2 34\n2 35\n2 36\n2 37\n2 38\n2 39\n2 40\n2 41\n2 42\n2 43\n2 44\n2 45\n2 46\n2 47\n2 48\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n2 69\n2 70\n2 71\n2 72\n2 73\n2 74\n2 75\n2 76\n2 77\n2 78\n2 79\n2 80\n2 81\n2 82\n2 83\n2 84\n2 85..." }, { "input": "207 3", "output": "20912\n1 2\n206 207\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n2 32\n2 33\n2 34\n2 35\n2 36\n2 37\n2 38\n2 39\n2 40\n2 41\n2 42\n2 43\n2 44\n2 45\n2 46\n2 47\n2 48\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n2 69\n2 70\n2 71\n2 72\n2 73\n2 74\n2 75\n2 76\n2 77\n2 78\n2 79\n2 80\n2 81\n2 82\n2 83\n2 84\n2 85\n..." }, { "input": "196 3", "output": "18723\n1 2\n195 196\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n2 32\n2 33\n2 34\n2 35\n2 36\n2 37\n2 38\n2 39\n2 40\n2 41\n2 42\n2 43\n2 44\n2 45\n2 46\n2 47\n2 48\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n2 69\n2 70\n2 71\n2 72\n2 73\n2 74\n2 75\n2 76\n2 77\n2 78\n2 79\n2 80\n2 81\n2 82\n2 83\n2 84\n2 85\n..." }, { "input": "706 3", "output": "247458\n1 2\n705 706\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n2 32\n2 33\n2 34\n2 35\n2 36\n2 37\n2 38\n2 39\n2 40\n2 41\n2 42\n2 43\n2 44\n2 45\n2 46\n2 47\n2 48\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n2 69\n2 70\n2 71\n2 72\n2 73\n2 74\n2 75\n2 76\n2 77\n2 78\n2 79\n2 80\n2 81\n2 82\n2 83\n2 84\n2 85..." }, { "input": "416 3", "output": "85493\n1 2\n415 416\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n2 32\n2 33\n2 34\n2 35\n2 36\n2 37\n2 38\n2 39\n2 40\n2 41\n2 42\n2 43\n2 44\n2 45\n2 46\n2 47\n2 48\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n2 69\n2 70\n2 71\n2 72\n2 73\n2 74\n2 75\n2 76\n2 77\n2 78\n2 79\n2 80\n2 81\n2 82\n2 83\n2 84\n2 85\n..." }, { "input": "2 1", "output": "-1" }, { "input": "2 2", "output": "-1" }, { "input": "2 3", "output": "-1" }, { "input": "2 4", "output": "-1" }, { "input": "3 1", "output": "-1" }, { "input": "3 2", "output": "-1" }, { "input": "3 3", "output": "-1" }, { "input": "3 4", "output": "-1" } ]
46
0
0
27,561
0
none
[ "none" ]
null
null
Two best friends Serozha and Gena play a game. Initially there is one pile consisting of *n* stones on the table. During one move one pile should be taken and divided into an arbitrary number of piles consisting of *a*1<=&gt;<=*a*2<=&gt;<=...<=&gt;<=*a**k*<=&gt;<=0 stones. The piles should meet the condition *a*1<=-<=*a*2<==<=*a*2<=-<=*a*3<==<=...<==<=*a**k*<=-<=1<=-<=*a**k*<==<=1. Naturally, the number of piles *k* should be no less than two. The friends play in turns. The player who cannot make a move loses. Serozha makes the first move. Who will win if both players play in the optimal way?
The single line contains a single integer *n* (1<=≤<=*n*<=≤<=105).
If Serozha wins, print *k*, which represents the minimal number of piles into which he can split the initial one during the first move in order to win the game. If Gena wins, print "-1" (without the quotes).
[ "3\n", "6\n", "100\n" ]
[ "2\n", "-1\n", "8\n" ]
none
[]
60
0
0
27,573
125
Hobbits' Party
[ "constructive algorithms", "greedy" ]
null
null
Everyone knows that hobbits love to organize all sorts of parties and celebrations. There are *n* hobbits living in the Shire. They decided to organize the Greatest Party (GP) that would last for several days. Next day the hobbits wrote a guest list, some non-empty set containing all the inhabitants of the Shire. To ensure that everybody enjoy themselves and nobody gets bored, for any two days (say, days A and B) of the GP there existed at least one hobbit, invited to come on day A and on day B. However, to ensure that nobody has a row, for any three different days A, B, C there shouldn't be a hobbit invited on days A, B and C. The Shire inhabitants are keen on keeping the GP going for as long as possible. Your task is given number *n*, to indicate the GP's maximum duration and the guest lists for each day.
The first line contains an integer *n* (3<=≤<=*n*<=≤<=10000), representing the number of hobbits.
In the first output line print a number *k* — the maximum duration of GP in days. Then on *k* lines print the guest lists, (the guests should be separated by spaces). Print each guest list on the single line. Each list can contain an arbitrary positive number of hobbits. The hobbits are numbered with integers from 1 to *n*.
[ "4\n", "5\n" ]
[ "3\n1 2 \n1 3 \n2 3 \n", "3\n1 2 \n1 3 \n2 3 \n" ]
none
[ { "input": "4", "output": "3\n1 2 \n1 3 \n2 3 " }, { "input": "5", "output": "3\n1 2 \n1 3 \n2 3 " }, { "input": "6", "output": "4\n1 2 3 \n1 4 5 \n2 4 6 \n3 5 6 " }, { "input": "7", "output": "4\n1 2 3 \n1 4 5 \n2 4 6 \n3 5 6 " }, { "input": "8", "output": "4\n1 2 3 \n1 4 5 \n2 4 6 \n3 5 6 " }, { "input": "9", "output": "4\n1 2 3 \n1 4 5 \n2 4 6 \n3 5 6 " }, { "input": "10", "output": "5\n1 2 3 4 \n1 5 6 7 \n2 5 8 9 \n3 6 8 10 \n4 7 9 10 " }, { "input": "11", "output": "5\n1 2 3 4 \n1 5 6 7 \n2 5 8 9 \n3 6 8 10 \n4 7 9 10 " }, { "input": "14", "output": "5\n1 2 3 4 \n1 5 6 7 \n2 5 8 9 \n3 6 8 10 \n4 7 9 10 " }, { "input": "15", "output": "6\n1 2 3 4 5 \n1 6 7 8 9 \n2 6 10 11 12 \n3 7 10 13 14 \n4 8 11 13 15 \n5 9 12 14 15 " }, { "input": "16", "output": "6\n1 2 3 4 5 \n1 6 7 8 9 \n2 6 10 11 12 \n3 7 10 13 14 \n4 8 11 13 15 \n5 9 12 14 15 " }, { "input": "20", "output": "6\n1 2 3 4 5 \n1 6 7 8 9 \n2 6 10 11 12 \n3 7 10 13 14 \n4 8 11 13 15 \n5 9 12 14 15 " }, { "input": "21", "output": "7\n1 2 3 4 5 6 \n1 7 8 9 10 11 \n2 7 12 13 14 15 \n3 8 12 16 17 18 \n4 9 13 16 19 20 \n5 10 14 17 19 21 \n6 11 15 18 20 21 " }, { "input": "44", "output": "9\n1 2 3 4 5 6 7 8 \n1 9 10 11 12 13 14 15 \n2 9 16 17 18 19 20 21 \n3 10 16 22 23 24 25 26 \n4 11 17 22 27 28 29 30 \n5 12 18 23 27 31 32 33 \n6 13 19 24 28 31 34 35 \n7 14 20 25 29 32 34 36 \n8 15 21 26 30 33 35 36 " }, { "input": "45", "output": "10\n1 2 3 4 5 6 7 8 9 \n1 10 11 12 13 14 15 16 17 \n2 10 18 19 20 21 22 23 24 \n3 11 18 25 26 27 28 29 30 \n4 12 19 25 31 32 33 34 35 \n5 13 20 26 31 36 37 38 39 \n6 14 21 27 32 36 40 41 42 \n7 15 22 28 33 37 40 43 44 \n8 16 23 29 34 38 41 43 45 \n9 17 24 30 35 39 42 44 45 " }, { "input": "189", "output": "19\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 \n1 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 \n2 19 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 \n3 20 36 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 \n4 21 37 52 67 68 69 70 71 72 73 74 75 76 77 78 79 80 \n5 22 38 53 67 81 82 83 84 85 86 87 88 89 90 91 92 93 \n6 23 39 54 68 81 94 95 96 97 98 99 100 101 102 103 104 105 \n7 24 40 55 69 82 94 106 107 108 109 110 111 112 113 114 115 116 \n8 25 41 56 70 83 95 106 117 118 119 120 121 122 123 124 12..." }, { "input": "190", "output": "20\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 \n1 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 \n2 20 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 \n3 21 38 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 \n4 22 39 55 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 \n5 23 40 56 71 86 87 88 89 90 91 92 93 94 95 96 97 98 99 \n6 24 41 57 72 86 100 101 102 103 104 105 106 107 108 109 110 111 112 \n7 25 42 58 73 87 100 113 114 115 116 117 118 119 120 121 122 123 124 \n8 26 43 59 74 88 101 113 ..." }, { "input": "191", "output": "20\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 \n1 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 \n2 20 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 \n3 21 38 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 \n4 22 39 55 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 \n5 23 40 56 71 86 87 88 89 90 91 92 93 94 95 96 97 98 99 \n6 24 41 57 72 86 100 101 102 103 104 105 106 107 108 109 110 111 112 \n7 25 42 58 73 87 100 113 114 115 116 117 118 119 120 121 122 123 124 \n8 26 43 59 74 88 101 113 ..." }, { "input": "209", "output": "20\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 \n1 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 \n2 20 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 \n3 21 38 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 \n4 22 39 55 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 \n5 23 40 56 71 86 87 88 89 90 91 92 93 94 95 96 97 98 99 \n6 24 41 57 72 86 100 101 102 103 104 105 106 107 108 109 110 111 112 \n7 25 42 58 73 87 100 113 114 115 116 117 118 119 120 121 122 123 124 \n8 26 43 59 74 88 101 113 ..." }, { "input": "210", "output": "21\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 \n1 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 \n2 21 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 \n3 22 40 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 \n4 23 41 58 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 \n5 24 42 59 75 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 \n6 25 43 60 76 91 106 107 108 109 110 111 112 113 114 115 116 117 118 119 \n7 26 44 61 77 92 106 120 121 122 123 124 125 126 127 128 129 130 131..." }, { "input": "230", "output": "21\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 \n1 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 \n2 21 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 \n3 22 40 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 \n4 23 41 58 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 \n5 24 42 59 75 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 \n6 25 43 60 76 91 106 107 108 109 110 111 112 113 114 115 116 117 118 119 \n7 26 44 61 77 92 106 120 121 122 123 124 125 126 127 128 129 130 131..." }, { "input": "231", "output": "22\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 \n1 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 \n2 22 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 \n3 23 42 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 \n4 24 43 61 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 \n5 25 44 62 79 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 \n6 26 45 63 80 96 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 \n7 27 46 64 81 97 112 127 128 129 130 131..." }, { "input": "251", "output": "22\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 \n1 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 \n2 22 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 \n3 23 42 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 \n4 24 43 61 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 \n5 25 44 62 79 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 \n6 26 45 63 80 96 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 \n7 27 46 64 81 97 112 127 128 129 130 131..." }, { "input": "252", "output": "22\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 \n1 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 \n2 22 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 \n3 23 42 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 \n4 24 43 61 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 \n5 25 44 62 79 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 \n6 26 45 63 80 96 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 \n7 27 46 64 81 97 112 127 128 129 130 131..." }, { "input": "253", "output": "23\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 \n1 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 \n2 23 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 \n3 24 44 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 \n4 25 45 64 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 \n5 26 46 65 83 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 \n6 27 47 66 84 101 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 \n7 28 48 67 ..." }, { "input": "254", "output": "23\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 \n1 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 \n2 23 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 \n3 24 44 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 \n4 25 45 64 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 \n5 26 46 65 83 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 \n6 27 47 66 84 101 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 \n7 28 48 67 ..." }, { "input": "255", "output": "23\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 \n1 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 \n2 23 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 \n3 24 44 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 \n4 25 45 64 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 \n5 26 46 65 83 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 \n6 27 47 66 84 101 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 \n7 28 48 67 ..." }, { "input": "3", "output": "3\n1 2 \n1 3 \n2 3 " }, { "input": "9000", "output": "134\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 \n1 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 15..." }, { "input": "9100", "output": "135\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 \n1 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 15..." }, { "input": "9200", "output": "136\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 \n1 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 15..." }, { "input": "9300", "output": "136\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 \n1 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 15..." }, { "input": "9400", "output": "137\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 \n1 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 15..." }, { "input": "9500", "output": "138\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 \n1 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 15..." }, { "input": "9600", "output": "139\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 \n1 139 140 141 142 143 144 145 146 147 148 149 150 151 152 15..." }, { "input": "9700", "output": "139\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 \n1 139 140 141 142 143 144 145 146 147 148 149 150 151 152 15..." }, { "input": "9800", "output": "140\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 \n1 140 141 142 143 144 145 146 147 148 149 150 151 152 15..." }, { "input": "9900", "output": "141\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 \n1 141 142 143 144 145 146 147 148 149 150 151 152 15..." }, { "input": "9998", "output": "141\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 \n1 141 142 143 144 145 146 147 148 149 150 151 152 15..." }, { "input": "9999", "output": "141\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 \n1 141 142 143 144 145 146 147 148 149 150 151 152 15..." }, { "input": "10000", "output": "141\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 \n1 141 142 143 144 145 146 147 148 149 150 151 152 15..." }, { "input": "9868", "output": "140\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 \n1 140 141 142 143 144 145 146 147 148 149 150 151 152 15..." }, { "input": "9869", "output": "140\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 \n1 140 141 142 143 144 145 146 147 148 149 150 151 152 15..." }, { "input": "9870", "output": "141\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 \n1 141 142 143 144 145 146 147 148 149 150 151 152 15..." }, { "input": "9871", "output": "141\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 \n1 141 142 143 144 145 146 147 148 149 150 151 152 15..." }, { "input": "9377", "output": "137\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 \n1 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 15..." } ]
92
0
0
27,590
442
Borya and Hanabi
[ "bitmasks", "brute force", "implementation" ]
null
null
Have you ever played Hanabi? If not, then you've got to try it out! This problem deals with a simplified version of the game. Overall, the game has 25 types of cards (5 distinct colors and 5 distinct values). Borya is holding *n* cards. The game is somewhat complicated by the fact that everybody sees Borya's cards except for Borya himself. Borya knows which cards he has but he knows nothing about the order they lie in. Note that Borya can have multiple identical cards (and for each of the 25 types of cards he knows exactly how many cards of this type he has). The aim of the other players is to achieve the state when Borya knows the color and number value of each of his cards. For that, other players can give him hints. The hints can be of two types: color hints and value hints. A color hint goes like that: a player names some color and points at all the cards of this color. Similarly goes the value hint. A player names some value and points at all the cards that contain the value. Determine what minimum number of hints the other players should make for Borya to be certain about each card's color and value.
The first line contains integer *n* (1<=≤<=*n*<=≤<=100) — the number of Borya's cards. The next line contains the descriptions of *n* cards. The description of each card consists of exactly two characters. The first character shows the color (overall this position can contain five distinct letters — R, G, B, Y, W). The second character shows the card's value (a digit from 1 to 5). Borya doesn't know exact order of the cards they lie in.
Print a single integer — the minimum number of hints that the other players should make.
[ "2\nG3 G3\n", "4\nG4 R4 R3 B3\n", "5\nB1 Y1 W1 G1 R1\n" ]
[ "0\n", "2\n", "4\n" ]
In the first sample Borya already knows for each card that it is a green three. In the second sample we can show all fours and all red cards. In the third sample you need to make hints about any four colors.
[ { "input": "2\nG3 G3", "output": "0" }, { "input": "4\nG4 R4 R3 B3", "output": "2" }, { "input": "5\nB1 Y1 W1 G1 R1", "output": "4" }, { "input": "10\nY4 B1 R3 G5 R5 W3 W5 W2 R1 Y1", "output": "6" }, { "input": "3\nG4 G3 B4", "output": "2" }, { "input": "2\nW3 Y5", "output": "1" }, { "input": "2\nW5 Y5", "output": "1" }, { "input": "100\nW4 Y1 W5 R4 W3 Y1 R4 W2 G3 G1 B5 Y5 Y2 Y3 G4 B5 W1 G5 Y5 Y3 G2 Y5 Y5 G5 R2 B3 B1 W5 Y1 W5 B4 W4 R4 B1 R1 W3 R5 R4 G2 W3 W3 R2 W5 Y2 B2 R3 R3 Y1 G5 G2 Y1 R4 Y5 W5 G5 B3 W2 R1 B2 W2 W2 Y5 W3 G1 B1 G2 Y3 W3 G1 W5 W1 G5 G2 Y1 W5 B5 W4 Y5 G2 R3 B4 R5 B1 R1 B4 Y4 Y4 Y3 R5 Y3 B3 W5 R5 Y5 G2 G5 W5 B4 G4 W5", "output": "8" }, { "input": "100\nB5 G3 Y2 W3 W1 G1 Y4 G3 G4 B5 W4 B5 Y3 R5 B4 G4 G4 B1 G1 R5 G4 B4 G1 G2 W1 Y4 R5 Y3 W5 W4 Y5 W1 B4 G3 R2 R3 W1 B4 Y4 G2 G2 R1 Y5 W1 Y3 B3 G5 Y3 G3 W2 W5 Y4 B2 Y5 R4 B2 Y3 W4 G2 G3 R2 W1 B4 B2 B1 Y2 Y2 Y3 W3 Y5 W1 R1 R1 R4 R4 R4 Y1 Y3 B2 Y3 Y1 R3 G4 G4 Y5 B5 W2 G1 G5 B3 G1 Y1 R2 G5 R2 Y5 R5 Y4 R4 W2", "output": "8" }, { "input": "100\nY3 Y3 Y5 Y2 Y4 Y1 Y2 Y3 Y1 Y5 Y3 Y1 Y4 Y5 Y5 Y3 Y1 Y5 Y2 Y3 Y1 Y4 Y4 Y1 Y2 Y2 Y4 Y2 Y4 Y5 Y5 Y2 Y3 Y1 Y3 Y5 Y3 Y1 Y5 Y3 Y3 Y2 Y5 Y1 Y5 Y5 Y4 Y2 Y2 Y1 Y2 Y4 Y3 Y2 Y5 Y3 Y4 Y3 Y1 Y4 Y2 Y4 Y4 Y3 Y3 Y1 Y1 Y4 Y2 Y5 Y3 Y4 Y4 Y4 Y3 Y2 Y2 Y2 Y3 Y4 Y4 Y2 Y1 Y2 Y2 Y4 Y3 Y5 Y4 Y2 Y5 Y3 Y1 Y5 Y2 Y5 Y4 Y1 Y2 Y1", "output": "4" }, { "input": "100\nG3 B3 G3 Y3 R3 G3 R3 G3 W3 B3 B3 B3 R3 Y3 Y3 W3 R3 B3 G3 W3 G3 W3 Y3 R3 Y3 W3 W3 G3 W3 G3 W3 Y3 G3 R3 Y3 W3 W3 Y3 Y3 Y3 R3 W3 R3 G3 W3 W3 G3 Y3 B3 W3 B3 Y3 G3 Y3 B3 Y3 W3 Y3 R3 Y3 R3 W3 W3 W3 G3 Y3 G3 R3 B3 R3 Y3 R3 Y3 R3 Y3 R3 Y3 R3 W3 Y3 R3 W3 W3 W3 G3 G3 Y3 B3 Y3 R3 G3 G3 B3 B3 Y3 R3 G3 R3 W3 G3", "output": "4" }, { "input": "100\nW5 Y5 Y3 W3 Y5 Y5 W5 W5 Y3 W5 Y5 Y3 W5 Y5 W5 W3 Y3 Y5 W3 W5 Y5 W3 Y5 Y3 W3 W3 Y3 Y5 W3 W3 Y3 Y5 W3 Y5 Y5 Y5 W5 Y5 W5 Y5 W5 W5 W3 W5 W3 Y3 W5 W3 W3 W5 Y3 Y5 W5 W5 W3 W5 W5 Y5 W5 W3 W3 W3 Y3 Y5 Y5 Y3 Y5 W5 W5 Y5 W3 W3 W5 Y5 Y5 Y3 W5 W5 W3 Y5 W3 W5 Y3 Y5 W5 Y3 Y3 W3 Y3 Y5 Y3 Y3 W5 Y5 Y3 Y5 Y3 W3 Y3 W5", "output": "2" }, { "input": "100\nY5 Y5 Y3 Y5 Y3 Y5 Y5 Y5 Y3 Y3 Y5 Y3 Y3 Y5 Y3 Y3 Y5 Y3 Y3 Y5 Y3 Y5 Y5 Y3 Y3 Y5 Y3 Y5 Y3 Y3 Y3 Y5 Y3 Y3 Y3 Y3 Y3 Y3 Y3 Y5 Y5 Y5 Y5 Y3 Y5 Y5 Y5 Y5 Y3 Y3 Y3 Y5 Y5 Y3 Y3 Y3 Y5 Y3 Y3 Y5 Y3 Y3 Y3 Y5 Y3 Y5 Y5 Y5 Y5 Y3 Y3 Y5 Y5 Y5 Y5 Y5 Y5 Y5 Y5 Y3 Y5 Y5 Y5 Y5 Y5 Y5 Y3 Y5 Y3 Y5 Y5 Y5 Y5 Y5 Y5 Y3 Y3 Y3 Y5 Y5", "output": "1" }, { "input": "100\nY3 Y3 Y3 W3 W3 Y3 Y3 W3 Y3 Y3 W3 Y3 Y3 Y3 W3 Y3 Y3 W3 Y3 Y3 Y3 W3 Y3 W3 W3 W3 W3 W3 Y3 Y3 W3 Y3 Y3 W3 W3 Y3 Y3 W3 Y3 Y3 W3 W3 W3 W3 W3 Y3 Y3 Y3 Y3 W3 Y3 Y3 W3 W3 W3 Y3 W3 W3 W3 Y3 Y3 Y3 Y3 W3 W3 W3 W3 W3 Y3 Y3 W3 Y3 W3 Y3 Y3 Y3 Y3 Y3 Y3 Y3 W3 W3 W3 W3 Y3 W3 Y3 Y3 Y3 Y3 Y3 Y3 Y3 Y3 W3 Y3 Y3 W3 W3 Y3", "output": "1" }, { "input": "10\nW1 Y4 Y3 W3 Y2 W2 W1 Y2 Y3 W3", "output": "3" }, { "input": "10\nW2 R2 B5 W5 W2 B3 B2 R5 Y5 R2", "output": "5" }, { "input": "5\nW3 Y5 W2 G3 Y3", "output": "3" }, { "input": "5\nW5 G5 W2 Y5 G2", "output": "3" }, { "input": "5\nW5 W3 Y3 Y5 W2", "output": "3" }, { "input": "5\nY5 W3 W2 G5 W3", "output": "3" }, { "input": "5\nG5 G3 G5 G5 W2", "output": "2" }, { "input": "5\nW2 Y2 G3 G3 G3", "output": "2" }, { "input": "5\nG3 Y5 Y2 Y5 W3", "output": "3" }, { "input": "5\nW3 W5 Y3 W2 G5", "output": "3" }, { "input": "5\nY3 Y2 Y3 Y5 Y5", "output": "2" }, { "input": "5\nW5 Y3 W5 W5 Y5", "output": "2" }, { "input": "5\nG3 Y3 G3 Y3 G3", "output": "1" }, { "input": "5\nG4 W5 Y4 Y4 R5", "output": "3" }, { "input": "5\nG3 R2 R5 B3 W4", "output": "4" }, { "input": "5\nW2 G5 W3 R4 R4", "output": "3" }, { "input": "5\nY3 Y3 G3 Y3 W3", "output": "2" }, { "input": "5\nW5 W5 W3 Y3 Y5", "output": "2" }, { "input": "35\nG5 G5 G3 G2 G3 Y3 W3 Y3 G5 W5 G2 G2 W5 G5 G5 W3 G3 G2 Y3 W5 W3 G5 W5 G2 Y2 Y3 W5 G3 W3 W2 G2 W3 Y2 G3 G3", "output": "4" }, { "input": "35\nG3 G2 G5 G2 W2 G3 W2 W5 Y5 G5 W2 W2 W2 G5 Y2 G5 W5 W3 W2 Y2 G5 W5 W3 W5 Y2 Y5 W2 W2 W3 Y3 G2 W3 G5 G3 W2", "output": "4" }, { "input": "35\nW2 W3 Y2 G5 G2 W3 G5 Y2 W5 Y5 W3 G2 G3 W5 W2 W3 Y2 Y5 Y2 Y5 Y2 Y2 G2 Y5 W3 Y5 G3 Y2 G3 Y2 Y5 W5 G3 W5 W5", "output": "4" }, { "input": "35\nY2 Y3 Y2 Y3 Y5 Y5 Y3 Y5 Y5 Y2 Y2 Y5 Y2 Y2 Y5 Y3 Y2 Y5 Y5 Y3 Y3 Y2 Y2 Y2 Y3 Y5 Y2 Y5 Y5 Y3 Y5 Y2 Y3 Y3 Y2", "output": "2" }, { "input": "35\nY5 Y5 W5 Y5 W3 Y3 W5 W3 W5 W3 W5 Y5 Y3 W3 W5 W5 W5 W3 Y5 Y3 Y5 W5 W5 Y3 Y5 Y5 Y3 W5 W3 W3 W3 W5 Y5 W3 W5", "output": "2" }, { "input": "35\nG3 G3 W3 W3 G3 G3 Y3 G3 Y3 Y3 G3 W3 Y3 G3 W3 Y3 W3 Y3 Y3 Y3 Y3 G3 G3 Y3 Y3 G3 G3 G3 G3 G3 Y3 W3 W3 W3 G3", "output": "2" }, { "input": "35\nR2 R2 R2 R2 G3 G2 Y3 G2 R3 W5 G4 Y3 Y3 G2 Y5 W5 G4 G2 W4 Y2 W3 R5 W5 G4 G4 Y2 G2 Y5 G2 Y4 W3 G5 G2 R3 G4", "output": "6" }, { "input": "35\nG2 Y1 Y1 R4 G5 B5 R2 G4 G2 G3 W4 W1 B3 W5 R2 Y5 R4 R4 B5 Y2 B4 B1 R3 G4 Y3 G2 R4 G3 B2 G2 R3 B2 R1 W2 B4", "output": "8" }, { "input": "35\nW4 Y5 Y3 Y3 R5 W3 R5 W2 R5 Y2 R2 Y2 G2 G3 Y4 Y4 R4 Y3 G2 W2 R2 R3 Y3 W5 W2 G4 R3 R3 W2 W5 Y4 R3 W2 R4 R2", "output": "6" }, { "input": "35\nY3 Y3 Y3 Y3 Y3 Y3 W3 G3 G3 G3 W3 G3 G3 Y3 Y3 W3 G3 G3 Y3 W3 W3 Y3 Y3 G3 W3 W3 G3 Y3 Y3 W3 G3 W3 G3 W3 G3", "output": "2" }, { "input": "35\nW3 Y3 W3 W5 Y5 W3 W3 Y5 W3 W3 W3 W5 W3 Y3 Y5 Y3 Y5 Y3 W3 W3 W5 W5 W3 Y5 W3 W3 Y3 W3 W5 W3 Y3 Y3 Y5 Y5 Y3", "output": "2" }, { "input": "25\nY3 R2 R2 Y2 Y5 Y4 B3 B3 Y4 W3 R1 W3 W5 B5 R1 Y4 B4 B5 B3 G3 B1 R2 R4 G5 Y5", "output": "7" }, { "input": "25\nW5 R1 Y3 G5 G3 G5 W2 Y1 R3 R4 B3 G5 G2 Y2 W1 G2 Y1 B4 Y2 G3 G3 Y4 W3 W5 Y2", "output": "7" }, { "input": "25\nY1 W2 G3 W1 G1 G3 G4 W1 B2 W3 R4 B1 G2 Y4 G1 R2 R3 Y4 B2 G4 W2 Y2 R2 Y5 R4", "output": "7" }, { "input": "25\nB3 Y1 R3 Y3 R5 B4 B3 G1 R1 B4 B5 W3 R4 W1 B3 Y3 B3 Y4 B2 G5 Y1 G5 R4 B5 Y1", "output": "7" }, { "input": "25\nG4 R4 Y1 Y4 R3 B5 W2 G4 B5 B2 G1 B4 R4 G2 Y3 Y4 G5 Y3 R1 G2 Y5 G3 Y3 Y4 W3", "output": "8" }, { "input": "25\nR1 R3 G1 B5 Y5 W3 R4 R4 R1 G3 R2 W1 B1 G2 W2 G4 R2 W3 B1 W3 B5 Y5 R2 B4 B5", "output": "7" }, { "input": "5\nW3 B1 Y1 R3 Y5", "output": "3" }, { "input": "4\nR1 R2 G1 Y5", "output": "2" }, { "input": "4\nR1 R2 G1 B3", "output": "2" }, { "input": "15\nY3 R4 G1 W1 R3 R2 W4 W1 R5 Y4 G3 R5 Y2 W1 Y1", "output": "6" }, { "input": "4\nG5 W5 W4 R3", "output": "2" } ]
62
0
0
27,601
362
Fools and Foolproof Roads
[ "data structures", "dfs and similar", "dsu", "graphs", "greedy" ]
null
null
You must have heard all about the Foolland on your Geography lessons. Specifically, you must know that federal structure of this country has been the same for many centuries. The country consists of *n* cities, some pairs of cities are connected by bidirectional roads, each road is described by its length *l**i*. The fools lived in their land joyfully, but a recent revolution changed the king. Now the king is Vasily the Bear. Vasily divided the country cities into regions, so that any two cities of the same region have a path along the roads between them and any two cities of different regions don't have such path. Then Vasily decided to upgrade the road network and construct exactly *p* new roads in the country. Constructing a road goes like this: 1. We choose a pair of distinct cities *u*, *v* that will be connected by a new road (at that, it is possible that there already is a road between these cities). 1. We define the length of the new road: if cities *u*, *v* belong to distinct regions, then the length is calculated as *min*(109,<=*S*<=+<=1) (*S* — the total length of all roads that exist in the linked regions), otherwise we assume that the length equals 1000. 1. We build a road of the specified length between the chosen cities. If the new road connects two distinct regions, after construction of the road these regions are combined into one new region. Vasily wants the road constructing process to result in the country that consists exactly of *q* regions. Your task is to come up with such road constructing plan for Vasily that it meets the requirement and minimizes the total length of the built roads.
The first line contains four integers *n* (1<=≤<=*n*<=≤<=105), *m* (0<=≤<=*m*<=≤<=105), *p* (0<=≤<=*p*<=≤<=105), *q* (1<=≤<=*q*<=≤<=*n*) — the number of cities in the Foolland, the number of existing roads, the number of roads that are planned to construct and the required number of regions. Next *m* lines describe the roads that exist by the moment upgrading of the roads begun. Each of these lines contains three integers *x**i*, *y**i*, *l**i*: *x**i*, *y**i* — the numbers of the cities connected by this road (1<=≤<=*x**i*,<=*y**i*<=≤<=*n*,<=*x**i*<=≠<=*y**i*), *l**i* — length of the road (1<=≤<=*l**i*<=≤<=109). Note that one pair of cities can be connected with multiple roads.
If constructing the roads in the required way is impossible, print a single string "NO" (without the quotes). Otherwise, in the first line print word "YES" (without the quotes), and in the next *p* lines print the road construction plan. Each line of the plan must consist of two distinct integers, giving the numbers of the cities connected by a road. The road must occur in the plan in the order they need to be constructed. If there are multiple optimal solutions, you can print any of them.
[ "9 6 2 2\n1 2 2\n3 2 1\n4 6 20\n1 3 8\n7 8 3\n5 7 2\n", "2 0 1 2\n", "2 0 0 2\n" ]
[ "YES\n9 5\n1 9\n", "NO\n", "YES\n" ]
Consider the first sample. Before the reform the Foolland consists of four regions. The first region includes cities 1, 2, 3, the second region has cities 4 and 6, the third region has cities 5, 7, 8, the fourth region has city 9. The total length of the roads in these cities is 11, 20, 5 and 0, correspondingly. According to the plan, we first build the road of length 6 between cities 5 and 9, then the road of length 23 between cities 1 and 9. Thus, the total length of the built roads equals 29.
[]
61
819,200
0
27,615
518
Arthur and Questions
[ "greedy", "implementation", "math", "ternary search" ]
null
null
After bracket sequences Arthur took up number theory. He has got a new favorite sequence of length *n* (*a*1,<=*a*2,<=...,<=*a**n*), consisting of integers and integer *k*, not exceeding *n*. This sequence had the following property: if you write out the sums of all its segments consisting of *k* consecutive elements (*a*1 <=+<= *a*2 ... <=+<= *a**k*,<= *a*2 <=+<= *a*3 <=+<= ... <=+<= *a**k*<=+<=1,<= ...,<= *a**n*<=-<=*k*<=+<=1 <=+<= *a**n*<=-<=*k*<=+<=2 <=+<= ... <=+<= *a**n*), then those numbers will form strictly increasing sequence. For example, for the following sample: *n*<==<=5,<= *k*<==<=3,<= *a*<==<=(1,<= 2,<= 4,<= 5,<= 6) the sequence of numbers will look as follows: (1 <=+<= 2 <=+<= 4,<= 2 <=+<= 4 <=+<= 5,<= 4 <=+<= 5 <=+<= 6) = (7,<= 11,<= 15), that means that sequence *a* meets the described property. Obviously the sequence of sums will have *n*<=-<=*k*<=+<=1 elements. Somebody (we won't say who) replaced some numbers in Arthur's sequence by question marks (if this number is replaced, it is replaced by exactly one question mark). We need to restore the sequence so that it meets the required property and also minimize the sum |*a**i*|, where |*a**i*| is the absolute value of *a**i*.
The first line contains two integers *n* and *k* (1<=≤<=*k*<=≤<=*n*<=≤<=105), showing how many numbers are in Arthur's sequence and the lengths of segments respectively. The next line contains *n* space-separated elements *a**i* (1<=≤<=*i*<=≤<=*n*). If *a**i* <==<= ?, then the *i*-th element of Arthur's sequence was replaced by a question mark. Otherwise, *a**i* (<=-<=109<=≤<=*a**i*<=≤<=109) is the *i*-th element of Arthur's sequence.
If Arthur is wrong at some point and there is no sequence that could fit the given information, print a single string "Incorrect sequence" (without the quotes). Otherwise, print *n* integers — Arthur's favorite sequence. If there are multiple such sequences, print the sequence with the minimum sum |*a**i*|, where |*a**i*| is the absolute value of *a**i*. If there are still several such sequences, you are allowed to print any of them. Print the elements of the sequence without leading zeroes.
[ "3 2\n? 1 2\n", "5 1\n-10 -9 ? -7 -6\n", "5 3\n4 6 7 2 9\n" ]
[ "0 1 2 \n", "-10 -9 -8 -7 -6 \n", "Incorrect sequence\n" ]
none
[ { "input": "3 2\n? 1 2", "output": "0 1 2 " }, { "input": "5 1\n-10 -9 ? -7 -6", "output": "-10 -9 -8 -7 -6 " }, { "input": "5 3\n4 6 7 2 9", "output": "Incorrect sequence" }, { "input": "9 3\n? ? ? ? ? ? ? ? ?", "output": "-1 -1 -1 0 0 0 1 1 1 " }, { "input": "5 1\n1000000000 ? ? ? ?", "output": "1000000000 1000000001 1000000002 1000000003 1000000004 " }, { "input": "5 1\n-3 -2 -1 0 1", "output": "-3 -2 -1 0 1 " }, { "input": "7 3\n1 ? -1000000000 ? 100 ? 3", "output": "1 0 -1000000000 2 100 0 3 " }, { "input": "7 3\n1 ? -1000000000 ? 100 ? 2", "output": "Incorrect sequence" }, { "input": "1 1\n?", "output": "0 " }, { "input": "1 1\n0", "output": "0 " }, { "input": "2 1\n-1000000000 1000000000", "output": "-1000000000 1000000000 " }, { "input": "17 1\n? -13 ? ? ? -3 ? ? ? ? ? 10 ? ? ? ? 100", "output": "-14 -13 -6 -5 -4 -3 -2 -1 0 1 2 10 11 12 13 14 100 " }, { "input": "5 2\n? ? -1000000000 ? ?", "output": "-1000000001 0 -1000000000 1 0 " }, { "input": "5 4\n-1 ? ? ? 2", "output": "-1 0 0 0 2 " }, { "input": "10 2\n3 ? 5 ? 7 ? 9 ? 11 ?", "output": "3 -2 5 -1 7 0 9 1 11 2 " }, { "input": "39 3\n-5 1 -13 ? 2 -12 ? 3 -11 -2 4 -10 ? 5 -9 0 6 -8 ? 7 -7 ? 8 -6 5 9 -5 ? 10 -4 ? 11 -3 ? 12 -2 10 13 -1", "output": "-5 1 -13 -4 2 -12 -3 3 -11 -2 4 -10 -1 5 -9 0 6 -8 1 7 -7 2 8 -6 5 9 -5 6 10 -4 7 11 -3 8 12 -2 10 13 -1 " }, { "input": "3 1\n4 ? 5", "output": "Incorrect sequence" }, { "input": "3 1\n-1 ? 1", "output": "-1 0 1 " }, { "input": "3 1\n-3 ? -2", "output": "Incorrect sequence" }, { "input": "7 1\n-4 ? ? ? ? ? 2", "output": "-4 -3 -2 -1 0 1 2 " }, { "input": "3 1\n-5 ? 0", "output": "-5 -1 0 " }, { "input": "9 3\n-5 0 -1 ? ? ? 0 5 1", "output": "-5 0 -1 -1 1 0 0 5 1 " }, { "input": "6 1\n-1 ? 1 2 3 4", "output": "-1 0 1 2 3 4 " }, { "input": "6 1\n-3 ? ? ? ? 3", "output": "-3 -1 0 1 2 3 " }, { "input": "7 1\n-3 ? ? ? ? ? 3", "output": "-3 -2 -1 0 1 2 3 " }, { "input": "7 1\n-2 ? ? ? ? ? 4", "output": "-2 -1 0 1 2 3 4 " }, { "input": "10 1\n-2 ? ? ? ? ? ? 5 ? 10", "output": "-2 -1 0 1 2 3 4 5 6 10 " }, { "input": "7 2\n-10 0 ? 1 ? 2 ?", "output": "-10 0 -1 1 0 2 1 " }, { "input": "7 2\n10 0 ? 1 ? 2 ?", "output": "10 0 11 1 12 2 13 " }, { "input": "9 2\n-10 0 ? 1 ? 2 ? 3 ?", "output": "-10 0 -1 1 0 2 1 3 2 " }, { "input": "9 2\n10 0 ? 1 ? 2 ? 3 ?", "output": "10 0 11 1 12 2 13 3 14 " }, { "input": "7 2\n-10 0 ? 1 -6 2 ?", "output": "-10 0 -7 1 -6 2 0 " }, { "input": "9 2\n-10 0 ? 1 -6 2 ? 3 ?", "output": "-10 0 -7 1 -6 2 0 3 1 " }, { "input": "7 2\n-10 0 ? 1 ? 2 10", "output": "-10 0 0 1 1 2 10 " }, { "input": "7 2\n-10 0 ? 1 ? 2 0", "output": "-10 0 -2 1 -1 2 0 " }, { "input": "9 2\n-10 0 ? 1 ? 2 ? 3 10", "output": "-10 0 -1 1 0 2 1 3 10 " }, { "input": "9 2\n-10 0 ? 1 ? 2 ? 3 0", "output": "-10 0 -3 1 -2 2 -1 3 0 " }, { "input": "7 2\n-10 0 0 1 ? 2 ?", "output": "-10 0 0 1 1 2 2 " }, { "input": "7 2\n-10 0 ? 1 6 2 ?", "output": "-10 0 0 1 6 2 7 " }, { "input": "7 2\n? ? 10 ? ? ? ?", "output": "0 -1 10 0 11 1 12 " }, { "input": "7 2\n? ? -10 ? ? ? ?", "output": "-11 -1 -10 0 0 1 1 " }, { "input": "7 2\n? ? 10 ? -10 ? ?", "output": "Incorrect sequence" }, { "input": "7 2\n? ? -10 ? 10 ? ?", "output": "-11 -1 -10 0 10 1 11 " }, { "input": "9 2\n? ? -10 ? ? ? 10 ? ?", "output": "-11 -1 -10 0 0 1 10 2 11 " } ]
405
21,401,600
3
27,616
0
none
[ "none" ]
null
null
Drazil created a following problem about putting 1<=×<=2 tiles into an *n*<=×<=*m* grid: "There is a grid with some cells that are empty and some cells that are occupied. You should use 1<=×<=2 tiles to cover all empty cells and no two tiles should cover each other. And you should print a solution about how to do it." But Drazil doesn't like to write special checking program for this task. His friend, Varda advised him: "how about asking contestant only to print the solution when it exists and it is unique? Otherwise contestant may print 'Not unique' ". Drazil found that the constraints for this task may be much larger than for the original task! Can you solve this new problem? Note that you should print 'Not unique' either when there exists no solution or when there exists several different solutions for the original task.
The first line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=2000). The following *n* lines describe the grid rows. Character '.' denotes an empty cell, and the character '*' denotes a cell that is occupied.
If there is no solution or the solution is not unique, you should print the string "Not unique". Otherwise you should print how to cover all empty cells with 1<=×<=2 tiles. Use characters "&lt;&gt;" to denote horizontal tiles and characters "^v" to denote vertical tiles. Refer to the sample test for the output format example.
[ "3 3\n...\n.*.\n...\n", "4 4\n..**\n*...\n*.**\n....\n", "2 4\n*..*\n....\n", "1 1\n.\n", "1 1\n*\n" ]
[ "Not unique\n", "&lt;&gt;**\n*^&lt;&gt;\n*v**\n&lt;&gt;&lt;&gt;\n", "*&lt;&gt;*\n&lt;&gt;&lt;&gt;\n", "Not unique\n", "*\n" ]
In the first case, there are indeed two solutions: and so the answer is "Not unique".
[]
0
0
-1
27,632
914
Palindromes in a Tree
[ "bitmasks", "data structures", "divide and conquer", "trees" ]
null
null
You are given a tree (a connected acyclic undirected graph) of *n* vertices. Vertices are numbered from 1 to *n* and each vertex is assigned a character from a to t. A path in the tree is said to be palindromic if at least one permutation of the labels in the path is a palindrome. For each vertex, output the number of palindromic paths passing through it. Note: The path from vertex *u* to vertex *v* is considered to be the same as the path from vertex *v* to vertex *u*, and this path will be counted only once for each of the vertices it passes through.
The first line contains an integer *n* (2<=≤<=*n*<=≤<=2·105)  — the number of vertices in the tree. The next *n*<=-<=1 lines each contain two integers *u* and *v* (1<=<=≤<=<=*u*,<=*v*<=<=≤<=<=*n*,<=*u*<=≠<=*v*) denoting an edge connecting vertex *u* and vertex *v*. It is guaranteed that the given graph is a tree. The next line contains a string consisting of *n* lowercase characters from a to t where the *i*-th (1<=≤<=*i*<=≤<=*n*) character is the label of vertex *i* in the tree.
Print *n* integers in a single line, the *i*-th of which is the number of palindromic paths passing through vertex *i* in the tree.
[ "5\n1 2\n2 3\n3 4\n3 5\nabcbb\n", "7\n6 2\n4 3\n3 7\n5 2\n7 2\n1 4\nafefdfs\n" ]
[ "1 3 4 3 3 \n", "1 4 1 1 2 4 2 \n" ]
In the first sample case, the following paths are palindromic: 2 - 3 - 4 2 - 3 - 5 4 - 3 - 5 Additionally, all paths containing only one vertex are palindromic. Listed below are a few paths in the first sample that are not palindromic: 1 - 2 - 3 1 - 2 - 3 - 4 1 - 2 - 3 - 5
[ { "input": "5\n1 2\n2 3\n3 4\n3 5\nabcbb", "output": "1 3 4 3 3 " }, { "input": "7\n6 2\n4 3\n3 7\n5 2\n7 2\n1 4\nafefdfs", "output": "1 4 1 1 2 4 2 " }, { "input": "5\n3 1\n3 5\n5 4\n5 2\nticdm", "output": "1 1 1 1 1 " }, { "input": "10\n10 8\n3 2\n9 7\n1 5\n5 3\n7 6\n8 4\n10 9\n2 6\nqbilfkqcll", "output": "1 1 1 2 1 1 2 3 4 5 " }, { "input": "20\n10 9\n15 14\n11 12\n2 3\n15 16\n2 1\n18 19\n20 19\n8 9\n7 6\n8 7\n12 13\n5 6\n4 3\n13 14\n18 17\n11 10\n16 17\n5 4\naabbccddeeffgghhiijj", "output": "20 29 45 52 64 69 77 80 84 85 85 84 80 77 69 64 52 45 29 20 " } ]
46
0
0
27,679
762
Maximum path
[ "dp", "greedy", "implementation" ]
null
null
You are given a rectangular table 3<=×<=*n*. Each cell contains an integer. You can move from one cell to another if they share a side. Find such path from the upper left cell to the bottom right cell of the table that doesn't visit any of the cells twice, and the sum of numbers written in the cells of this path is maximum possible.
The first line contains an integer *n* (1<=≤<=*n*<=≤<=105)  — the number of columns in the table. Next three lines contain *n* integers each  — the description of the table. The *j*-th number in the *i*-th line corresponds to the cell *a**ij* (<=-<=109<=≤<=*a**ij*<=≤<=109) of the table.
Output the maximum sum of numbers on a path from the upper left cell to the bottom right cell of the table, that doesn't visit any of the cells twice.
[ "3\n1 1 1\n1 -1 1\n1 1 1\n", "5\n10 10 10 -1 -1\n-1 10 10 10 10\n-1 10 10 10 10\n" ]
[ "7\n", "110\n" ]
The path for the first example: The path for the second example:
[ { "input": "3\n1 1 1\n1 -1 1\n1 1 1", "output": "7" }, { "input": "5\n10 10 10 -1 -1\n-1 10 10 10 10\n-1 10 10 10 10", "output": "110" }, { "input": "15\n-87 -91 31 63 91 35 -14 51 20 20 -20 -94 -59 77 76\n11 81 22 -29 91 -26 -10 -12 46 10 100 88 14 64 41\n26 -31 99 -39 -30 30 28 74 -7 21 2 32 -60 -74 46", "output": "1152" }, { "input": "20\n16 82 25 21 -60 9 29 -55 70 54 -50 10 -19 40 46 41 31 -66 1 85\n-15 75 -94 -7 -50 -97 -55 -24 44 -69 -73 15 -9 98 92 -92 72 -32 -46 59\n74 99 -6 97 -59 41 -22 -8 -27 75 3 -56 -38 -56 -43 16 -43 -92 55 -63", "output": "946" }, { "input": "5\n150684603 -262756669 -629261226 393751321 700168705\n853551233 -595914191 -266257139 165068700 494943072\n328547487 63141018 -951406530 -212389249 -69164259", "output": "2218520550" }, { "input": "10\n687024557 -928074266 -409520915 770775361 240764400 108297300 -280070452 588179696 -920283246 736937716\n422602209 -940948979 -483879926 -525886137 -79749893 -958247281 844561102 553768085 269384580 -975129348\n-485518332 -130037110 493465239 494308146 958976404 706037260 154106757 -250914836 -915814064 -45677796", "output": "4721200012" }, { "input": "3\n-1 -1 -1\n-1 -1 -1\n-1 -1 -1", "output": "-5" }, { "input": "1\n1\n1\n1", "output": "3" }, { "input": "1\n1000000000\n1000000000\n1000000000", "output": "3000000000" }, { "input": "3\n1 1 1\n-1 1 1\n-1 1 1", "output": "7" }, { "input": "2\n-36 45\n28 -1\n2 -21", "output": "17" }, { "input": "4\n-2 -1 2 2\n-2 0 0 1\n1 -2 2 -1", "output": "3" }, { "input": "1\n-1\n0\n0", "output": "-1" } ]
1,000
24,371,200
0
27,691
76
Mice
[ "greedy", "two pointers" ]
B. Mice
0
256
Modern researches has shown that a flock of hungry mice searching for a piece of cheese acts as follows: if there are several pieces of cheese then each mouse chooses the closest one. After that all mice start moving towards the chosen piece of cheese. When a mouse or several mice achieve the destination point and there is still a piece of cheese in it, they eat it and become well-fed. Each mice that reaches this point after that remains hungry. Moving speeds of all mice are equal. If there are several ways to choose closest pieces then mice will choose it in a way that would minimize the number of hungry mice. To check this theory scientists decided to conduct an experiment. They located *N* mice and *M* pieces of cheese on a cartesian plane where all mice are located on the line *y*<==<=*Y*0 and all pieces of cheese — on another line *y*<==<=*Y*1. To check the results of the experiment the scientists need a program which simulates the behavior of a flock of hungry mice. Write a program that computes the minimal number of mice which will remain hungry, i.e. without cheese.
The first line of the input contains four integer numbers *N* (1<=≤<=*N*<=≤<=105), *M* (0<=≤<=*M*<=≤<=105), *Y*0 (0<=≤<=*Y*0<=≤<=107), *Y*1 (0<=≤<=*Y*1<=≤<=107, *Y*0<=≠<=*Y*1). The second line contains a strictly increasing sequence of *N* numbers — *x* coordinates of mice. Third line contains a strictly increasing sequence of *M* numbers — *x* coordinates of cheese. All coordinates are integers and do not exceed 107 by absolute value.
The only line of output should contain one number — the minimal number of mice which will remain without cheese.
[ "3 2 0 2\n0 1 3\n2 5\n" ]
[ "1\n" ]
All the three mice will choose the first piece of cheese. Second and third mice will eat this piece. The first one will remain hungry, because it was running towards the same piece, but it was late. The second piece of cheese will remain uneaten.
[ { "input": "3 2 0 2\n0 1 3\n2 5", "output": "1" }, { "input": "7 11 10 20\n6 18 32 63 66 68 87\n6 8 15 23 25 41 53 59 60 75 90", "output": "1" }, { "input": "13 17 14 1\n6 9 10 12 17 25 91 100 118 136 145 163 172\n0 1 2 3 4 10 12 13 16 17 19 22 26 27 28 109 154", "output": "4" }, { "input": "19 23 13 11\n3 6 7 15 21 22 23 33 35 37 40 44 79 86 100 114 121 135 142\n2 3 5 6 7 14 15 17 18 19 20 22 25 27 28 34 36 38 39 41 42 93 128", "output": "4" }, { "input": "20 18 1 2\n-9999944 -9999861 -9999850 -9999763 -9999656 -9999517 -9999375 -9999275 -9999203 -9999080 -9998988 -9998887 -9998714 -9998534 -9998475 -9998352 -9998164 -9998016 -9998002 -9997882\n-9999976 -9999912 -9999788 -9999738 -9999574 -9999460 -9999290 -9999260 -9999146 -9999014 -9998962 -9998812 -9998616 -9998452 -9998252 -9998076 -9997928 -9997836", "output": "2" } ]
311
14,745,600
3
27,728
401
Roman and Numbers
[ "bitmasks", "brute force", "combinatorics", "dp", "number theory" ]
null
null
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number *n*, modulo *m*. Number *x* is considered close to number *n* modulo *m*, if: - it can be obtained by rearranging the digits of number *n*, - it doesn't have any leading zeroes, - the remainder after dividing number *x* by *m* equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.
The first line contains two integers: *n* (1<=≤<=*n*<=&lt;<=1018) and *m* (1<=≤<=*m*<=≤<=100).
In a single line print a single integer — the number of numbers close to number *n* modulo *m*.
[ "104 2\n", "223 4\n", "7067678 8\n" ]
[ "3\n", "1\n", "47\n" ]
In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
[ { "input": "104 2", "output": "3" }, { "input": "223 4", "output": "1" }, { "input": "7067678 8", "output": "47" }, { "input": "202 10", "output": "1" }, { "input": "1306432 9", "output": "0" }, { "input": "9653092 9", "output": "0" }, { "input": "600038 6", "output": "0" }, { "input": "4064044 4", "output": "65" }, { "input": "5899025 7", "output": "153" }, { "input": "2496234323687 2", "output": "26611200" }, { "input": "1 1", "output": "1" }, { "input": "123456789123456789 2", "output": "5557616064000" }, { "input": "123 1", "output": "6" }, { "input": "6328128 6", "output": "900" }, { "input": "8966261 5", "output": "0" }, { "input": "8900064 4", "output": "316" }, { "input": "576021249 86", "output": "2091" }, { "input": "682459775 6", "output": "0" }, { "input": "458498549 4", "output": "2100" }, { "input": "511736928 87", "output": "6267" }, { "input": "275126649 81", "output": "0" }, { "input": "576279776452 33", "output": "0" }, { "input": "450497776413 3", "output": "12196800" }, { "input": "356884378713 24", "output": "554400" }, { "input": "89058837012 65", "output": "60616" }, { "input": "884654082330 71", "output": "117073" }, { "input": "181939172581 23", "output": "216735" }, { "input": "555549171905 10", "output": "83160" }, { "input": "347161822604 67", "output": "409390" }, { "input": "734944298780 13", "output": "702988" }, { "input": "848092188917 18", "output": "0" }, { "input": "379620222683264759 39", "output": "0" }, { "input": "173043406290107692 90", "output": "0" }, { "input": "195176731478682385 14", "output": "205836996960" }, { "input": "63436369526943580 59", "output": "1231321437" }, { "input": "385383273011112989 11", "output": "39548174400" }, { "input": "412729214864015139 96", "output": "109654776000" }, { "input": "227038765076961932 79", "output": "41687924851" }, { "input": "498744630369919412 82", "output": "16056754308" }, { "input": "280798391352360320 72", "output": "0" }, { "input": "795452688779941322 52", "output": "63335115897" }, { "input": "5014489842919580 5", "output": "2724321600" }, { "input": "9615722072995774 82", "output": "104605125" }, { "input": "7441738340032798 84", "output": "0" }, { "input": "9003489956983022 37", "output": "399073500" }, { "input": "2454597559364838 19", "output": "955902382" }, { "input": "4410755660493003 8", "output": "2052388800" }, { "input": "6375967545169807 15", "output": "0" }, { "input": "3593106551449275 59", "output": "865762883" }, { "input": "9458580614310278 16", "output": "14070672000" }, { "input": "2866933879413767 4", "output": "5448643200" }, { "input": "7076043389696504 4", "output": "8627018400" }, { "input": "36160302795340 2", "output": "485654400" }, { "input": "1296319391649597 4", "output": "1180539360" }, { "input": "4300962713274444 2", "output": "5993507520" }, { "input": "90876543212468024 2", "output": "771891120000" }, { "input": "7769468502479263 9", "output": "0" }, { "input": "3027468649121495 10", "output": "13621608000" }, { "input": "2312734624976780 10", "output": "4540536000" }, { "input": "6632346285917617 1", "output": "54486432000" }, { "input": "1240656721470018 9", "output": "29513484000" }, { "input": "3345289321458628 8", "output": "3416212800" }, { "input": "3802128082766215 4", "output": "5340535200" }, { "input": "12345678902468000 2", "output": "503999496000" }, { "input": "8227332913355818 8", "output": "583783200" }, { "input": "6404415286642984 10", "output": "454053600" }, { "input": "10000000000000000 100", "output": "1" }, { "input": "1 100", "output": "0" }, { "input": "2147483647 97", "output": "3135" }, { "input": "88888888888888888 88", "output": "0" }, { "input": "99999999999999999 99", "output": "0" }, { "input": "1 1", "output": "1" }, { "input": "12468024680246802 2", "output": "8870862000" }, { "input": "123456789123456789 100", "output": "0" }, { "input": "123456789123456700 100", "output": "163459296000" }, { "input": "1 1", "output": "1" }, { "input": "123456789123456789 1", "output": "12504636144000" }, { "input": "987654321987654321 1", "output": "12504636144000" }, { "input": "213780 7", "output": "60" }, { "input": "102233445566778899 89", "output": "265391558945" }, { "input": "110022334455667788 10", "output": "1307674368000" } ]
61
11,673,600
-1
27,821
557
Ann and Half-Palindrome
[ "data structures", "dp", "graphs", "string suffix structures", "strings", "trees" ]
null
null
Tomorrow Ann takes the hardest exam of programming where she should get an excellent mark. On the last theoretical class the teacher introduced the notion of a half-palindrome. String *t* is a half-palindrome, if for all the odd positions *i* () the following condition is held: *t**i*<==<=*t*|*t*|<=-<=*i*<=+<=1, where |*t*| is the length of string *t* if positions are indexed from 1. For example, strings "abaa", "a", "bb", "abbbaa" are half-palindromes and strings "ab", "bba" and "aaabaa" are not. Ann knows that on the exam she will get string *s*, consisting only of letters a and b, and number *k*. To get an excellent mark she has to find the *k*-th in the lexicographical order string among all substrings of *s* that are half-palyndromes. Note that each substring in this order is considered as many times as many times it occurs in *s*. The teachers guarantees that the given number *k* doesn't exceed the number of substrings of the given string that are half-palindromes. Can you cope with this problem?
The first line of the input contains string *s* (1<=≤<=|*s*|<=≤<=5000), consisting only of characters 'a' and 'b', where |*s*| is the length of string *s*. The second line contains a positive integer *k* —  the lexicographical number of the requested string among all the half-palindrome substrings of the given string *s*. The strings are numbered starting from one. It is guaranteed that number *k* doesn't exceed the number of substrings of the given string that are half-palindromes.
Print a substring of the given string that is the *k*-th in the lexicographical order of all substrings of the given string that are half-palindromes.
[ "abbabaab\n7\n", "aaaaa\n10\n", "bbaabb\n13\n" ]
[ "abaa\n", "aaa\n", "bbaabb\n" ]
By definition, string *a* = *a*<sub class="lower-index">1</sub>*a*<sub class="lower-index">2</sub>... *a*<sub class="lower-index">*n*</sub> is lexicographically less than string *b* = *b*<sub class="lower-index">1</sub>*b*<sub class="lower-index">2</sub>... *b*<sub class="lower-index">*m*</sub>, if either *a* is a prefix of *b* and doesn't coincide with *b*, or there exists such *i*, that *a*<sub class="lower-index">1</sub> = *b*<sub class="lower-index">1</sub>, *a*<sub class="lower-index">2</sub> = *b*<sub class="lower-index">2</sub>, ... *a*<sub class="lower-index">*i* - 1</sub> = *b*<sub class="lower-index">*i* - 1</sub>, *a*<sub class="lower-index">*i*</sub> &lt; *b*<sub class="lower-index">*i*</sub>. In the first sample half-palindrome substrings are the following strings — a, a, a, a, aa, aba, abaa, abba, abbabaa, b, b, b, b, baab, bab, bb, bbab, bbabaab (the list is given in the lexicographical order).
[ { "input": "abbabaab\n7", "output": "abaa" }, { "input": "aaaaa\n10", "output": "aaa" }, { "input": "bbaabb\n13", "output": "bbaabb" }, { "input": "a\n1", "output": "a" }, { "input": "b\n1", "output": "b" }, { "input": "ababbbbbbbaaaaaaaabbbbbb\n23", "output": "aaa" }, { "input": "aa\n3", "output": "aa" }, { "input": "aa\n1", "output": "a" }, { "input": "aa\n2", "output": "a" }, { "input": "ab\n1", "output": "a" }, { "input": "ab\n2", "output": "b" }, { "input": "aaa\n1", "output": "a" }, { "input": "aaa\n2", "output": "a" }, { "input": "aaa\n3", "output": "a" }, { "input": "aaa\n4", "output": "aa" }, { "input": "aaa\n5", "output": "aa" }, { "input": "aaa\n6", "output": "aaa" }, { "input": "abb\n1", "output": "a" }, { "input": "abb\n2", "output": "b" }, { "input": "abb\n3", "output": "b" }, { "input": "abb\n4", "output": "bb" }, { "input": "aba\n4", "output": "b" }, { "input": "aba\n3", "output": "aba" }, { "input": "aba\n2", "output": "a" }, { "input": "aba\n1", "output": "a" } ]
1,044
158,720,000
3
27,835
774
Pens And Days Of Week
[ "*special", "binary search", "number theory" ]
null
null
Stepan has *n* pens. Every day he uses them, and on the *i*-th day he uses the pen number *i*. On the (*n*<=+<=1)-th day again he uses the pen number 1, on the (*n*<=+<=2)-th — he uses the pen number 2 and so on. On every working day (from Monday to Saturday, inclusive) Stepan spends exactly 1 milliliter of ink of the pen he uses that day. On Sunday Stepan has a day of rest, he does not stend the ink of the pen he uses that day. Stepan knows the current volume of ink in each of his pens. Now it's the Monday morning and Stepan is going to use the pen number 1 today. Your task is to determine which pen will run out of ink before all the rest (that is, there will be no ink left in it), if Stepan will use the pens according to the conditions described above.
The first line contains the integer *n* (1<=≤<=*n*<=≤<=50<=000) — the number of pens Stepan has. The second line contains the sequence of integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109), where *a**i* is equal to the number of milliliters of ink which the pen number *i* currently has.
Print the index of the pen which will run out of ink before all (it means that there will be no ink left in it), if Stepan will use pens according to the conditions described above. Pens are numbered in the order they are given in input data. The numeration begins from one. Note that the answer is always unambiguous, since several pens can not end at the same time.
[ "3\n3 3 3\n", "5\n5 4 5 4 4\n" ]
[ "2\n", "5\n" ]
In the first test Stepan uses ink of pens as follows: 1. on the day number 1 (Monday) Stepan will use the pen number 1, after that there will be 2 milliliters of ink in it; 1. on the day number 2 (Tuesday) Stepan will use the pen number 2, after that there will be 2 milliliters of ink in it; 1. on the day number 3 (Wednesday) Stepan will use the pen number 3, after that there will be 2 milliliters of ink in it; 1. on the day number 4 (Thursday) Stepan will use the pen number 1, after that there will be 1 milliliters of ink in it; 1. on the day number 5 (Friday) Stepan will use the pen number 2, after that there will be 1 milliliters of ink in it; 1. on the day number 6 (Saturday) Stepan will use the pen number 3, after that there will be 1 milliliters of ink in it; 1. on the day number 7 (Sunday) Stepan will use the pen number 1, but it is a day of rest so he will not waste ink of this pen in it; 1. on the day number 8 (Monday) Stepan will use the pen number 2, after that this pen will run out of ink. So, the first pen which will not have ink is the pen number 2.
[ { "input": "3\n3 3 3", "output": "2" }, { "input": "5\n5 4 5 4 4", "output": "5" }, { "input": "28\n2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033", "output": "1" }, { "input": "7\n10 10 10 10 10 10 10", "output": "1" }, { "input": "28\n1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000", "output": "1" }, { "input": "21\n996 995 996 996 996 996 995 996 996 995 996 996 995 996 995 995 995 995 996 996 996", "output": "2" }, { "input": "28\n2033 2033 2034 2033 2034 2034 2033 2034 2033 2034 2033 2034 2034 2033 2033 2034 2034 2033 2034 2034 2034 2033 2034 2033 2034 2034 2034 2034", "output": "1" }, { "input": "1\n1", "output": "1" }, { "input": "1\n2", "output": "1" }, { "input": "1\n1123", "output": "1" }, { "input": "1\n1000000000", "output": "1" }, { "input": "2\n1000000000 1000000000", "output": "2" }, { "input": "2\n999999999 999999999", "output": "1" }, { "input": "3\n1000000000 1000000000 1000000000", "output": "2" }, { "input": "3\n999999999 1000000000 1000000000", "output": "1" }, { "input": "4\n1000000000 1000000000 1000000000 1000000000", "output": "1" }, { "input": "4\n999999999 999999999 999999999 999999999", "output": "1" }, { "input": "5\n1000000000 1000000000 1000000000 1000000000 1000000000", "output": "1" }, { "input": "5\n999999999 1000000000 999999999 1000000000 999999999", "output": "1" }, { "input": "6\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000", "output": "4" }, { "input": "6\n1000000000 999999999 999999999 999999999 1000000000 1000000000", "output": "3" }, { "input": "7\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000", "output": "1" }, { "input": "7\n1000000000 1000000000 1000000000 1000000000 999999999 999999999 999999999", "output": "5" }, { "input": "8\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000", "output": "1" }, { "input": "8\n1000000000 999999999 1000000000 999999999 1000000000 999999999 999999999 999999999", "output": "2" }, { "input": "7\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1", "output": "1" } ]
0
0
-1
27,843
196
Lexicographically Maximum Subsequence
[ "greedy", "strings" ]
null
null
You've got string *s*, consisting of only lowercase English letters. Find its lexicographically maximum subsequence. We'll call a non-empty string *s*[*p*1*p*2... *p**k*]<==<=*s**p*1*s**p*2... *s**p**k*(1<=≤<=*p*1<=&lt;<=*p*2<=&lt;<=...<=&lt;<=*p**k*<=≤<=|*s*|) a subsequence of string *s*<==<=*s*1*s*2... *s*|*s*|. String *x*<==<=*x*1*x*2... *x*|*x*| is lexicographically larger than string *y*<==<=*y*1*y*2... *y*|*y*|, if either |*x*|<=&gt;<=|*y*| and *x*1<==<=*y*1,<=*x*2<==<=*y*2,<=... ,<=*x*|*y*|<==<=*y*|*y*|, or exists such number *r* (*r*<=&lt;<=|*x*|,<=*r*<=&lt;<=|*y*|), that *x*1<==<=*y*1,<=*x*2<==<=*y*2,<=... ,<=*x**r*<==<=*y**r* and *x**r*<=+<=1<=&gt;<=*y**r*<=+<=1. Characters in lines are compared like their ASCII codes.
The single line contains a non-empty string *s*, consisting only of lowercase English letters. The string's length doesn't exceed 105.
Print the lexicographically maximum subsequence of string *s*.
[ "ababba\n", "abbcbccacbbcbaaba\n" ]
[ "bbba\n", "cccccbba\n" ]
Let's look at samples and see what the sought subsequences look like (they are marked with uppercase bold letters). The first sample: aBaBBA The second sample: abbCbCCaCbbCBaaBA
[ { "input": "ababba", "output": "bbba" }, { "input": "abbcbccacbbcbaaba", "output": "cccccbba" }, { "input": "thankstosamarasauteddybearsforthiscontest", "output": "yttt" }, { "input": "cantouristsolveitlessthaninoneminute", "output": "vute" }, { "input": "arepretestsstrongforthisproblem", "output": "ttttsrom" }, { "input": "whyareyoulookingfortestsdoyouhavewa", "output": "yyywa" }, { "input": "aa", "output": "aa" }, { "input": "zz", "output": "zz" }, { "input": "a", "output": "a" }, { "input": "z", "output": "z" }, { "input": "b", "output": "b" }, { "input": "y", "output": "y" }, { "input": "zaz", "output": "zz" }, { "input": "aza", "output": "za" }, { "input": "dcbaedcba", "output": "edcba" }, { "input": "abcdeabcd", "output": "ed" }, { "input": "abcdedcba", "output": "edcba" }, { "input": "zyxzxzwyxywxxwabcdabdacdbcacdbcacabaaba", "output": "zzzyyxxwddddccbba" }, { "input": "zzyzyy", "output": "zzzyy" }, { "input": "aababb", "output": "bbb" } ]
2,000
6,041,600
0
27,892
196
The Next Good String
[ "data structures", "greedy", "hashing", "strings" ]
null
null
In problems on strings one often has to find a string with some particular properties. The problem authors were reluctant to waste time on thinking of a name for some string so they called it good. A string is good if it doesn't have palindrome substrings longer than or equal to *d*. You are given string *s*, consisting only of lowercase English letters. Find a good string *t* with length |*s*|, consisting of lowercase English letters, which is lexicographically larger than *s*. Of all such strings string *t* must be lexicographically minimum. We will call a non-empty string *s*[*a* ... *b*]<==<=*s**a**s**a*<=+<=1... *s**b* (1<=≤<=*a*<=≤<=*b*<=≤<=|*s*|) a substring of string *s*<==<=*s*1*s*2... *s*|*s*|. A non-empty string *s*<==<=*s*1*s*2... *s**n* is called a palindrome if for all *i* from 1 to *n* the following fulfills: *s**i*<==<=*s**n*<=-<=*i*<=+<=1. In other words, palindrome read the same in both directions. String *x*<==<=*x*1*x*2... *x*|*x*| is lexicographically larger than string *y*<==<=*y*1*y*2... *y*|*y*|, if either |*x*|<=&gt;<=|*y*| and *x*1<==<=*y*1,<=*x*2<==<=*y*2,<=... ,<=*x*|*y*|<==<=*y*|*y*|, or there exists such number *r* (*r*<=&lt;<=|*x*|,<=*r*<=&lt;<=|*y*|), that *x*1<==<=*y*1,<=*x*2<==<=*y*2,<=... ,<=*x**r*<==<=*y**r* and *x**r*<=+<=1<=&gt;<=*y**r*<=+<=1. Characters in such strings are compared like their ASCII codes.
The first line contains integer *d* (1<=≤<=*d*<=≤<=|*s*|). The second line contains a non-empty string *s*, its length is no more than 4·105 characters. The string consists of lowercase English letters.
Print the good string that lexicographically follows *s*, has the same length and consists of only lowercase English letters. If such string does not exist, print "Impossible" (without the quotes).
[ "3\naaaaaaa\n", "3\nzzyzzzz\n", "4\nabbabbbabbb\n" ]
[ "aabbcaa\n", "Impossible\n", "abbbcaaabab\n" ]
none
[]
92
0
0
27,929
916
Jamie and To-do List
[ "data structures", "interactive", "trees" ]
null
null
Why I have to finish so many assignments??? Jamie is getting very busy with his school life. He starts to forget the assignments that he has to do. He decided to write the things down on a to-do list. He assigns a value priority for each of his assignment (lower value means more important) so he can decide which he needs to spend more time on. After a few days, Jamie finds out the list is too large that he can't even manage the list by himself! As you are a good friend of Jamie, help him write a program to support the following operations on the to-do list: - *set* *a**i* *x**i* — Add assignment *a**i* to the to-do list if it is not present, and set its priority to *x**i*. If assignment *a**i* is already in the to-do list, its priority is changed to *x**i*. - *remove* *a**i* — Remove assignment *a**i* from the to-do list if it is present in it. - *query* *a**i* — Output the number of assignments that are more important (have a smaller priority value) than assignment *a**i*, so Jamie can decide a better schedule. Output <=-<=1 if *a**i* is not in the to-do list. - *undo* *d**i* — Undo all changes that have been made in the previous *d**i* days (not including the day of this operation) At day 0, the to-do list is empty. In each of the following *q* days, Jamie will do exactly one out of the four operations. If the operation is a *query*, you should output the result of the query before proceeding to the next day, or poor Jamie cannot make appropriate decisions.
The first line consists of a single integer *q* (1<=≤<=*q*<=≤<=105) — the number of operations. The following *q* lines consists of the description of the operations. The *i*-th line consists of the operation that Jamie has done in the *i*-th day. The query has the following format: The first word in the line indicates the type of operation. It must be one of the following four: set, remove, query, undo. - If it is a set operation, a string *a**i* and an integer *x**i* follows (1<=≤<=*x**i*<=≤<=109). *a**i* is the assignment that need to be set to priority *x**i*. - If it is a remove operation, a string *a**i* follows. *a**i* is the assignment that need to be removed. - If it is a query operation, a string *a**i* follows. *a**i* is the assignment that needs to be queried. - If it is a undo operation, an integer *d**i* follows (0<=≤<=*d**i*<=&lt;<=*i*). *d**i* is the number of days that changes needed to be undone. All assignment names *a**i* only consists of lowercase English letters and have a length 1<=≤<=|*a**i*|<=≤<=15. It is guaranteed that the last operation is a query operation.
For each query operation, output a single integer — the number of assignments that have a priority lower than assignment *a**i*, or <=-<=1 if *a**i* is not in the to-do list.
[ "8\nset chemlabreport 1\nset physicsexercise 2\nset chinesemockexam 3\nquery physicsexercise\nquery chinesemockexam\nremove physicsexercise\nquery physicsexercise\nquery chinesemockexam\n", "8\nset physicsexercise 2\nset chinesemockexam 3\nset physicsexercise 1\nquery physicsexercise\nquery chinesemockexam\nundo 4\nquery physicsexercise\nquery chinesemockexam\n", "5\nquery economicsessay\nremove economicsessay\nquery economicsessay\nundo 2\nquery economicsessay\n", "5\nset economicsessay 1\nremove economicsessay\nundo 1\nundo 1\nquery economicsessay\n" ]
[ "1\n2\n-1\n1\n", "0\n1\n0\n-1\n", "-1\n-1\n-1\n", "-1\n" ]
none
[]
2,000
207,257,600
0
27,953
712
Memory and Scores
[ "combinatorics", "dp", "math" ]
null
null
Memory and his friend Lexa are competing to get higher score in one popular computer game. Memory starts with score *a* and Lexa starts with score *b*. In a single turn, both Memory and Lexa get some integer in the range [<=-<=*k*;*k*] (i.e. one integer among <=-<=*k*,<=<=-<=*k*<=+<=1,<=<=-<=*k*<=+<=2,<=...,<=<=-<=2,<=<=-<=1,<=0,<=1,<=2,<=...,<=*k*<=-<=1,<=*k*) and add them to their current scores. The game has exactly *t* turns. Memory and Lexa, however, are not good at this game, so they both always get a random integer at their turn. Memory wonders how many possible games exist such that he ends with a strictly higher score than Lexa. Two games are considered to be different if in at least one turn at least one player gets different score. There are (2*k*<=+<=1)2*t* games in total. Since the answer can be very large, you should print it modulo 109<=+<=7. Please solve this problem for Memory.
The first and only line of input contains the four integers *a*, *b*, *k*, and *t* (1<=≤<=*a*,<=*b*<=≤<=100, 1<=≤<=*k*<=≤<=1000, 1<=≤<=*t*<=≤<=100) — the amount Memory and Lexa start with, the number *k*, and the number of turns respectively.
Print the number of possible games satisfying the conditions modulo 1<=000<=000<=007 (109<=+<=7) in one line.
[ "1 2 2 1\n", "1 1 1 2\n", "2 12 3 1\n" ]
[ "6\n", "31\n", "0\n" ]
In the first sample test, Memory starts with 1 and Lexa starts with 2. If Lexa picks  - 2, Memory can pick 0, 1, or 2 to win. If Lexa picks  - 1, Memory can pick 1 or 2 to win. If Lexa picks 0, Memory can pick 2 to win. If Lexa picks 1 or 2, Memory cannot win. Thus, there are 3 + 2 + 1 = 6 possible games in which Memory wins.
[ { "input": "1 2 2 1", "output": "6" }, { "input": "1 1 1 2", "output": "31" }, { "input": "2 12 3 1", "output": "0" }, { "input": "4 6 2 1", "output": "3" }, { "input": "4 6 2 2", "output": "122" }, { "input": "6 4 2 2", "output": "435" }, { "input": "10 1 3 3", "output": "112812" }, { "input": "5 3 1 1", "output": "8" }, { "input": "50 22 5 5", "output": "876439301" }, { "input": "6 20 1 1", "output": "0" }, { "input": "42 42 2 3", "output": "6937" }, { "input": "42 42 3 2", "output": "1085" }, { "input": "45 54 4 5", "output": "433203628" }, { "input": "6 5 4 3", "output": "282051" }, { "input": "42 42 42 42", "output": "284470145" }, { "input": "1 100 42 42", "output": "58785421" }, { "input": "1 100 1000 100", "output": "542673827" }, { "input": "1 1 1000 100", "output": "922257788" }, { "input": "100 100 1000 100", "output": "922257788" }, { "input": "1 8 1 4", "output": "1" }, { "input": "9 4 5 2", "output": "11045" }, { "input": "2 6 6 2", "output": "8015" }, { "input": "7 8 5 9", "output": "860378382" }, { "input": "3 7 8 6", "output": "510324293" }, { "input": "69 69 803 81", "output": "74925054" }, { "input": "67 67 871 88", "output": "123371511" }, { "input": "71 71 891 31", "output": "790044038" }, { "input": "49 49 631 34", "output": "764129060" }, { "input": "83 83 770 49", "output": "761730117" }, { "input": "49 49 163 15", "output": "458364105" }, { "input": "38 38 701 74", "output": "496603581" }, { "input": "65 65 803 79", "output": "253679300" }, { "input": "56 56 725 64", "output": "338598412" }, { "input": "70 70 176 56", "output": "990579000" }, { "input": "32 32 44 79", "output": "20803934" }, { "input": "35 35 353 21", "output": "149936279" }, { "input": "57 57 896 52", "output": "271910130" }, { "input": "86 86 373 19", "output": "940701970" }, { "input": "27 27 296 97", "output": "394599845" }, { "input": "60 60 86 51", "output": "277883413" }, { "input": "40 40 955 95", "output": "600387428" }, { "input": "34 34 706 59", "output": "274236101" }, { "input": "74 74 791 51", "output": "367968499" }, { "input": "69 69 443 53", "output": "385620893" }, { "input": "59 19 370 48", "output": "125206836" }, { "input": "78 82 511 33", "output": "375900871" }, { "input": "66 90 805 16", "output": "593436252" }, { "input": "60 61 772 19", "output": "931528755" }, { "input": "81 13 607 21", "output": "762608093" }, { "input": "35 79 128 21", "output": "177972209" }, { "input": "93 25 958 20", "output": "873170266" }, { "input": "44 85 206 80", "output": "170080402" }, { "input": "79 99 506 18", "output": "486170430" }, { "input": "97 22 29 8", "output": "471632954" }, { "input": "14 47 184 49", "output": "726421144" }, { "input": "74 33 868 5", "output": "826980486" }, { "input": "53 79 823 11", "output": "526626321" }, { "input": "99 99 913 42", "output": "446683872" }, { "input": "52 34 89 41", "output": "905639400" }, { "input": "87 100 200 80", "output": "913761305" }, { "input": "40 94 510 53", "output": "233079261" }, { "input": "2 56 438 41", "output": "500592304" }, { "input": "6 68 958 41", "output": "719351710" }, { "input": "44 80 814 26", "output": "414148151" }, { "input": "100 1 1000 100", "output": "603336175" }, { "input": "1 3 1000 100", "output": "604187087" }, { "input": "10 10 1000 100", "output": "922257788" } ]
2,000
9,420,800
0
28,005
205
Little Elephant and Sorting
[ "brute force", "greedy" ]
null
null
The Little Elephant loves sortings. He has an array *a* consisting of *n* integers. Let's number the array elements from 1 to *n*, then the *i*-th element will be denoted as *a**i*. The Little Elephant can make one move to choose an arbitrary pair of integers *l* and *r* (1<=≤<=*l*<=≤<=*r*<=≤<=*n*) and increase *a**i* by 1 for all *i* such that *l*<=≤<=*i*<=≤<=*r*. Help the Little Elephant find the minimum number of moves he needs to convert array *a* to an arbitrary array sorted in the non-decreasing order. Array *a*, consisting of *n* elements, is sorted in the non-decreasing order if for any *i* (1<=≤<=*i*<=&lt;<=*n*) *a**i*<=≤<=*a**i*<=+<=1 holds.
The first line contains a single integer *n* (1<=≤<=*n*<=≤<=105) — the size of array *a*. The next line contains *n* integers, separated by single spaces — array *a* (1<=≤<=*a**i*<=≤<=109). The array elements are listed in the line in the order of their index's increasing.
In a single line print a single integer — the answer to the problem. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
[ "3\n1 2 3\n", "3\n3 2 1\n", "4\n7 4 1 47\n" ]
[ "0\n", "2\n", "6\n" ]
In the first sample the array is already sorted in the non-decreasing order, so the answer is 0. In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]). In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
[ { "input": "3\n1 2 3", "output": "0" }, { "input": "3\n3 2 1", "output": "2" }, { "input": "4\n7 4 1 47", "output": "6" }, { "input": "10\n1 2 3 4 5 6 7 8 9 1000000000", "output": "0" }, { "input": "10\n1000000000 1 1000000000 1 1000000000 1 1000000000 1 1000000000 1", "output": "4999999995" }, { "input": "7\n47 47 47 47 47 47 48", "output": "0" }, { "input": "47\n479793446 951468508 89486286 338446715 32648506 624498057 608503040 669251062 922459299 753303599 15471514 633954104 726178809 25774434 239818174 886000554 86565563 340322990 233160987 244152140 400122002 267331289 113220645 554372347 628491411 141545764 72472415 172718507 818323067 524691081 273905810 540908460 264978418 971408123 336064021 681508839 387880395 446312618 486187013 687624992 335098176 259987774 832741771 604233011 459307319 378796313 520655387", "output": "7171587476" }, { "input": "47\n7 9 9 3 7 3 6 8 3 6 6 2 6 4 2 2 4 3 6 1 3 9 8 2 3 5 3 10 7 7 5 2 8 1 5 7 2 7 6 2 1 9 7 7 4 10 3", "output": "76" }, { "input": "74\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1", "output": "0" }, { "input": "1\n940259367", "output": "0" }, { "input": "2\n710095427 879909817", "output": "0" }, { "input": "3\n39740000 928596641 251625421", "output": "676971220" }, { "input": "47\n3 999999997 5 999999991 9 999999996 1 999999991 6 999999996 4 999999998 6 999999994 4 999999994 7 999999990 1 999999993 6 999999997 4 999999996 1 999999990 7 1000000000 3 999999994 5 999999997 3 999999991 2 999999997 4 999999992 8 999999994 10 999999992 2 999999995 2 999999990 2", "output": "22999999763" }, { "input": "47\n703599938 780784195 912005704 957182560 961181825 964876912 996677776 997012583 999004240 999888999 999980718 999997556 999997940 999999989 999999991 999999991 999999999 999999999 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000", "output": "0" } ]
170
9,113,600
3
28,045
401
Sereja and Contests
[ "greedy", "implementation", "math" ]
null
null
Sereja is a coder and he likes to take part in Codesorfes rounds. However, Uzhland doesn't have good internet connection, so Sereja sometimes skips rounds. Codesorfes has rounds of two types: *Div*1 (for advanced coders) and *Div*2 (for beginner coders). Two rounds, *Div*1 and *Div*2, can go simultaneously, (*Div*1 round cannot be held without *Div*2) in all other cases the rounds don't overlap in time. Each round has a unique identifier — a positive integer. The rounds are sequentially (without gaps) numbered with identifiers by the starting time of the round. The identifiers of rounds that are run simultaneously are different by one, also the identifier of the *Div*1 round is always greater. Sereja is a beginner coder, so he can take part only in rounds of *Div*2 type. At the moment he is taking part in a *Div*2 round, its identifier equals to *x*. Sereja remembers very well that he has taken part in exactly *k* rounds before this round. Also, he remembers all identifiers of the rounds he has taken part in and all identifiers of the rounds that went simultaneously with them. Sereja doesn't remember anything about the rounds he missed. Sereja is wondering: what minimum and what maximum number of *Div*2 rounds could he have missed? Help him find these two numbers.
The first line contains two integers: *x* (1<=≤<=*x*<=≤<=4000) — the round Sereja is taking part in today, and *k* (0<=≤<=*k*<=&lt;<=4000) — the number of rounds he took part in. Next *k* lines contain the descriptions of the rounds that Sereja took part in before. If Sereja took part in one of two simultaneous rounds, the corresponding line looks like: "1 *num*2 *num*1" (where *num*2 is the identifier of this *Div*2 round, *num*1 is the identifier of the *Div*1 round). It is guaranteed that *num*1<=-<=*num*2<==<=1. If Sereja took part in a usual *Div*2 round, then the corresponding line looks like: "2 *num*" (where *num* is the identifier of this *Div*2 round). It is guaranteed that the identifiers of all given rounds are less than *x*.
Print in a single line two integers — the minimum and the maximum number of rounds that Sereja could have missed.
[ "3 2\n2 1\n2 2\n", "9 3\n1 2 3\n2 8\n1 4 5\n", "10 0\n" ]
[ "0 0", "2 3", "5 9" ]
In the second sample we have unused identifiers of rounds 1, 6, 7. The minimum number of rounds Sereja could have missed equals to 2. In this case, the round with the identifier 1 will be a usual *Div*2 round and the round with identifier 6 will be synchronous with the *Div*1 round. The maximum number of rounds equals 3. In this case all unused identifiers belong to usual *Div*2 rounds.
[ { "input": "3 2\n2 1\n2 2", "output": "0 0" }, { "input": "9 3\n1 2 3\n2 8\n1 4 5", "output": "2 3" }, { "input": "10 0", "output": "5 9" }, { "input": "10 2\n1 1 2\n1 8 9", "output": "3 5" }, { "input": "9 3\n1 4 5\n1 1 2\n1 6 7", "output": "2 2" }, { "input": "7 2\n2 3\n1 5 6", "output": "2 3" }, { "input": "81 28\n1 77 78\n1 50 51\n2 9\n1 66 67\n1 12 13\n1 20 21\n1 28 29\n1 34 35\n1 54 55\n2 19\n1 70 71\n1 45 46\n1 36 37\n2 47\n2 7\n2 76\n2 6\n2 31\n1 16 17\n1 4 5\n1 73 74\n1 64 65\n2 62\n2 22\n2 1\n1 48 49\n2 24\n2 40", "output": "22 36" }, { "input": "12 8\n1 4 5\n1 9 10\n2 3\n1 6 7\n2 1\n2 2\n2 8\n2 11", "output": "0 0" }, { "input": "54 25\n1 40 41\n2 46\n2 32\n2 8\n1 51 52\n2 39\n1 30 31\n2 53\n1 33 34\n1 42 43\n1 17 18\n1 21 22\n1 44 45\n2 50\n2 49\n2 15\n1 3 4\n1 27 28\n1 19 20\n1 47 48\n2 13\n1 37 38\n1 6 7\n2 35\n2 26", "output": "10 14" }, { "input": "90 35\n2 83\n2 86\n2 46\n1 61 62\n2 11\n1 76 77\n2 37\n2 9\n1 18 19\n2 79\n1 35 36\n1 3 4\n2 78\n2 72\n1 44 45\n2 31\n2 38\n2 65\n1 32 33\n1 13 14\n2 75\n2 42\n2 51\n2 80\n2 29\n1 22 23\n1 5 6\n2 53\n1 7 8\n1 24 25\n1 54 55\n2 84\n1 27 28\n2 26\n2 12", "output": "25 40" }, { "input": "98 47\n1 48 49\n2 47\n1 25 26\n2 29\n1 38 39\n1 20 21\n2 75\n2 68\n2 95\n2 6\n1 1 2\n1 84 85\n2 66\n1 88 89\n2 19\n2 32\n1 93 94\n1 45 46\n2 50\n1 15 16\n1 63 64\n1 23 24\n1 53 54\n1 43 44\n2 97\n1 12 13\n2 86\n2 74\n2 42\n1 40 41\n1 30 31\n1 34 35\n1 27 28\n2 81\n1 8 9\n2 73\n1 70 71\n2 67\n2 60\n2 72\n1 76 77\n1 90 91\n1 17 18\n2 11\n1 82 83\n1 58 59\n2 55", "output": "18 24" }, { "input": "56 34\n2 22\n2 27\n1 18 19\n1 38 39\n2 49\n1 10 11\n1 14 15\n2 40\n2 34\n1 32 33\n2 17\n1 24 25\n2 23\n2 52\n1 45 46\n2 28\n2 7\n1 4 5\n1 30 31\n2 21\n2 6\n1 47 48\n1 43 44\n1 54 55\n2 13\n1 8 9\n1 2 3\n2 41\n1 35 36\n1 50 51\n2 1\n2 29\n2 16\n2 53", "output": "5 5" }, { "input": "43 27\n1 40 41\n1 2 3\n1 32 33\n1 35 36\n1 27 28\n1 30 31\n1 7 8\n2 11\n1 5 6\n2 1\n1 15 16\n1 38 39\n2 12\n1 20 21\n1 22 23\n1 24 25\n1 9 10\n2 26\n2 14\n1 18 19\n2 17\n2 4\n2 34\n2 37\n2 29\n2 42\n2 13", "output": "0 0" }, { "input": "21 13\n1 6 7\n2 12\n1 8 9\n2 19\n1 4 5\n1 17 18\n2 3\n2 20\n1 10 11\n2 14\n1 15 16\n1 1 2\n2 13", "output": "0 0" }, { "input": "66 1\n1 50 51", "output": "32 63" }, { "input": "62 21\n2 34\n1 39 40\n1 52 53\n1 35 36\n2 27\n1 56 57\n2 43\n1 7 8\n2 28\n1 44 45\n1 41 42\n1 32 33\n2 58\n1 47 48\n2 10\n1 21 22\n2 51\n1 15 16\n1 19 20\n1 3 4\n2 25", "output": "16 27" }, { "input": "83 56\n2 24\n2 30\n1 76 77\n1 26 27\n1 73 74\n1 52 53\n2 82\n1 36 37\n2 13\n2 4\n2 68\n1 31 32\n1 65 66\n1 16 17\n1 56 57\n2 60\n1 44 45\n1 80 81\n1 28 29\n2 23\n1 54 55\n2 9\n2 1\n1 34 35\n2 5\n1 78 79\n2 40\n2 42\n1 61 62\n2 49\n2 22\n2 25\n1 7 8\n1 20 21\n1 38 39\n2 43\n2 12\n1 46 47\n2 70\n1 71 72\n2 3\n1 10 11\n2 75\n2 59\n1 18 19\n2 69\n2 48\n1 63 64\n2 33\n1 14 15\n1 50 51\n2 6\n2 41\n2 2\n2 67\n2 58", "output": "0 0" }, { "input": "229 27\n2 7\n1 64 65\n1 12 13\n2 110\n1 145 146\n2 92\n2 28\n2 39\n1 16 17\n2 164\n2 137\n1 95 96\n2 125\n1 48 49\n1 115 116\n1 198 199\n1 148 149\n1 225 226\n1 1 2\n2 24\n2 103\n1 87 88\n2 124\n2 89\n1 178 179\n1 160 161\n2 184", "output": "98 187" }, { "input": "293 49\n2 286\n2 66\n2 98\n1 237 238\n1 136 137\n1 275 276\n2 152\n1 36 37\n2 26\n2 40\n2 79\n2 274\n1 205 206\n1 141 142\n1 243 244\n2 201\n1 12 13\n1 123 124\n1 165 166\n1 6 7\n2 64\n1 22 23\n2 120\n1 138 139\n1 50 51\n2 15\n2 67\n2 45\n1 288 289\n1 261 262\n1 103 104\n2 249\n2 32\n2 153\n2 248\n1 162 163\n2 89\n1 94 95\n2 21\n1 48 49\n1 56 57\n2 102\n1 271 272\n2 269\n1 232 233\n1 70 71\n1 42 43\n1 267 268\n2 292", "output": "121 217" }, { "input": "181 57\n1 10 11\n1 4 5\n1 170 171\n2 86\n2 97\n1 91 92\n2 162\n2 115\n1 98 99\n2 134\n1 100 101\n2 168\n1 113 114\n1 37 38\n2 81\n2 169\n1 173 174\n1 165 166\n2 108\n2 121\n1 31 32\n2 67\n2 13\n2 50\n2 157\n1 27 28\n1 19 20\n2 109\n1 104 105\n2 46\n1 126 127\n1 102 103\n2 158\n2 133\n2 93\n2 68\n1 70 71\n2 125\n2 36\n1 48 49\n2 117\n1 131 132\n2 79\n2 23\n1 75 76\n2 107\n2 138\n1 94 95\n2 54\n1 87 88\n2 41\n1 153 154\n1 14 15\n2 60\n2 148\n1 159 160\n2 58", "output": "61 98" }, { "input": "432 5\n1 130 131\n2 108\n1 76 77\n1 147 148\n2 137", "output": "214 423" }, { "input": "125 45\n2 70\n2 111\n2 52\n2 3\n2 97\n2 104\n1 47 48\n2 44\n2 88\n1 117 118\n2 82\n1 22 23\n1 53 54\n1 38 39\n1 114 115\n2 93\n2 113\n2 102\n2 30\n2 95\n2 36\n2 73\n2 51\n2 87\n1 15 16\n2 55\n2 80\n2 121\n2 26\n1 31 32\n1 105 106\n1 1 2\n1 10 11\n2 91\n1 78 79\n1 7 8\n2 120\n2 75\n1 45 46\n2 94\n2 72\n2 25\n1 34 35\n1 17 18\n1 20 21", "output": "40 62" }, { "input": "48 35\n1 17 18\n2 20\n1 7 8\n2 13\n1 1 2\n2 23\n1 25 26\n1 14 15\n2 3\n1 45 46\n1 35 36\n2 47\n1 27 28\n2 30\n1 5 6\n2 11\n2 9\n1 32 33\n2 19\n2 24\n2 16\n1 42 43\n1 21 22\n2 37\n2 34\n2 40\n2 31\n2 10\n2 44\n2 39\n2 12\n2 29\n2 38\n2 4\n2 41", "output": "0 0" }, { "input": "203 55\n2 81\n2 65\n1 38 39\n1 121 122\n2 48\n2 83\n1 23 24\n2 165\n1 132 133\n1 143 144\n2 35\n2 85\n2 187\n1 19 20\n2 137\n2 150\n2 202\n2 156\n2 178\n1 93 94\n2 73\n2 167\n1 56 57\n1 100 101\n1 26 27\n1 51 52\n2 74\n2 4\n2 79\n2 113\n1 181 182\n2 75\n2 157\n2 25\n2 124\n1 68 69\n1 135 136\n1 110 111\n1 153 154\n2 123\n2 134\n1 36 37\n1 145 146\n1 141 142\n1 86 87\n2 10\n1 5 6\n2 131\n2 116\n2 70\n1 95 96\n1 174 175\n2 108\n1 91 92\n2 152", "output": "71 123" }, { "input": "51 38\n2 48\n2 42\n2 20\n2 4\n2 37\n2 22\n2 9\n2 13\n1 44 45\n1 33 34\n2 8\n1 18 19\n1 2 3\n2 27\n1 5 6\n1 40 41\n1 24 25\n2 16\n2 39\n2 50\n1 31 32\n1 46 47\n2 15\n1 29 30\n1 10 11\n2 49\n2 14\n1 35 36\n2 23\n2 7\n2 38\n2 26\n2 1\n2 17\n2 43\n2 21\n2 12\n2 28", "output": "0 0" }, { "input": "401 40\n1 104 105\n2 368\n1 350 351\n1 107 108\n1 4 5\n1 143 144\n2 369\n1 337 338\n2 360\n2 384\n2 145\n1 102 103\n1 88 89\n1 179 180\n2 202\n1 234 235\n2 154\n1 9 10\n1 113 114\n2 398\n1 46 47\n1 35 36\n1 174 175\n1 273 274\n1 237 238\n2 209\n1 138 139\n1 33 34\n1 243 244\n1 266 267\n1 294 295\n2 219\n2 75\n2 340\n1 260 261\n1 245 246\n2 210\n1 221 222\n1 328 329\n1 164 165", "output": "177 333" }, { "input": "24 16\n1 16 17\n1 1 2\n1 8 9\n1 18 19\n1 22 23\n1 13 14\n2 15\n2 6\n2 11\n2 20\n2 3\n1 4 5\n2 10\n2 7\n2 21\n2 12", "output": "0 0" }, { "input": "137 37\n2 108\n1 55 56\n2 20\n1 33 34\n2 112\n2 48\n2 120\n2 38\n2 74\n2 119\n2 27\n1 13 14\n2 8\n1 88 89\n1 44 45\n2 124\n2 76\n2 123\n2 104\n1 58 59\n2 52\n2 47\n1 3 4\n1 65 66\n2 28\n1 102 103\n2 81\n2 86\n2 116\n1 69 70\n1 11 12\n2 84\n1 25 26\n2 100\n2 90\n2 83\n1 95 96", "output": "52 86" }, { "input": "1155 50\n1 636 637\n1 448 449\n2 631\n2 247\n1 1049 1050\n1 1103 1104\n1 816 817\n1 1127 1128\n2 441\n2 982\n1 863 864\n2 186\n1 774 775\n2 793\n2 173\n2 800\n1 952 953\n1 492 493\n1 796 797\n2 907\n2 856\n2 786\n2 921\n1 558 559\n2 1090\n1 307 308\n1 1152 1153\n1 578 579\n1 944 945\n1 707 708\n2 968\n1 1005 1006\n1 1100 1101\n2 402\n1 917 918\n1 237 238\n1 191 192\n2 460\n1 1010 1011\n2 960\n1 1018 1019\n2 296\n1 958 959\n2 650\n2 395\n1 1124 1125\n2 539\n2 152\n1 385 386\n2 464", "output": "548 1077" }, { "input": "1122 54\n2 1031\n1 363 364\n1 14 15\n1 902 903\n1 1052 1053\n2 170\n2 36\n2 194\n1 340 341\n1 1018 1019\n1 670 671\n1 558 559\n2 431\n2 351\n2 201\n1 1104 1105\n2 1056\n2 823\n1 274 275\n2 980\n1 542 543\n1 807 808\n2 157\n2 895\n1 505 506\n2 658\n1 484 485\n1 533 534\n1 384 385\n2 779\n2 888\n1 137 138\n1 198 199\n2 762\n1 451 452\n1 248 249\n2 294\n2 123\n2 948\n2 1024\n2 771\n2 922\n1 566 567\n1 707 708\n1 1037 1038\n2 63\n1 208 209\n1 738 739\n2 648\n1 491 492\n1 440 441\n2 651\n1 971 972\n1 93 94", "output": "532 1038" }, { "input": "2938 48\n2 1519\n2 1828\n1 252 253\n1 2275 2276\n1 1479 1480\n2 751\n2 972\n2 175\n2 255\n1 1837 1838\n1 1914 1915\n2 198\n1 1686 1687\n1 950 951\n2 61\n1 840 841\n2 277\n1 52 53\n1 76 77\n2 795\n2 1680\n1 2601 2602\n2 2286\n2 2188\n2 2521\n2 1166\n2 1171\n2 2421\n1 1297 1298\n1 1736 1737\n1 991 992\n1 1048 1049\n2 756\n2 2054\n1 2878 2879\n1 1445 1446\n1 2539 2540\n2 1334\n2 2233\n2 494\n2 506\n1 1942 1943\n2 2617\n1 1991 1992\n2 1501\n1 2488 2489\n1 752 753\n2 2623", "output": "1444 2867" }, { "input": "2698 39\n2 710\n1 260 261\n2 174\n2 1697\n2 915\n1 2029 2030\n2 916\n2 2419\n2 323\n1 2130 2131\n2 1350\n1 64 65\n1 763 764\n1 939 940\n2 1693\n2 659\n2 2281\n2 761\n2 909\n1 1873 1874\n1 1164 1165\n2 2308\n2 504\n1 1035 1036\n1 2271 2272\n1 1085 1086\n1 1757 1758\n2 1818\n1 1604 1605\n1 517 518\n1 2206 2207\n2 636\n1 519 520\n2 1928\n1 1894 1895\n2 573\n2 2313\n1 42 43\n2 1529", "output": "1327 2640" }, { "input": "3999 0", "output": "1999 3998" }, { "input": "1 0", "output": "0 0" }, { "input": "10 5\n1 1 2\n2 3\n2 8\n1 4 5\n1 6 7", "output": "1 1" }, { "input": "4000 0", "output": "2000 3999" } ]
499
5,427,200
3
28,068
988
Equal Sums
[ "implementation", "sortings" ]
null
null
You are given $k$ sequences of integers. The length of the $i$-th sequence equals to $n_i$. You have to choose exactly two sequences $i$ and $j$ ($i \ne j$) such that you can remove exactly one element in each of them in such a way that the sum of the changed sequence $i$ (its length will be equal to $n_i - 1$) equals to the sum of the changed sequence $j$ (its length will be equal to $n_j - 1$). Note that it's required to remove exactly one element in each of the two chosen sequences. Assume that the sum of the empty (of the length equals $0$) sequence is $0$.
The first line contains an integer $k$ ($2 \le k \le 2 \cdot 10^5$) — the number of sequences. Then $k$ pairs of lines follow, each pair containing a sequence. The first line in the $i$-th pair contains one integer $n_i$ ($1 \le n_i &lt; 2 \cdot 10^5$) — the length of the $i$-th sequence. The second line of the $i$-th pair contains a sequence of $n_i$ integers $a_{i, 1}, a_{i, 2}, \dots, a_{i, n_i}$. The elements of sequences are integer numbers from $-10^4$ to $10^4$. The sum of lengths of all given sequences don't exceed $2 \cdot 10^5$, i.e. $n_1 + n_2 + \dots + n_k \le 2 \cdot 10^5$.
If it is impossible to choose two sequences such that they satisfy given conditions, print "NO" (without quotes). Otherwise in the first line print "YES" (without quotes), in the second line — two integers $i$, $x$ ($1 \le i \le k, 1 \le x \le n_i$), in the third line — two integers $j$, $y$ ($1 \le j \le k, 1 \le y \le n_j$). It means that the sum of the elements of the $i$-th sequence without the element with index $x$ equals to the sum of the elements of the $j$-th sequence without the element with index $y$. Two chosen sequences must be distinct, i.e. $i \ne j$. You can print them in any order. If there are multiple possible answers, print any of them.
[ "2\n5\n2 3 1 3 2\n6\n1 1 2 2 2 1\n", "3\n1\n5\n5\n1 1 1 1 1\n2\n2 3\n", "4\n6\n2 2 2 2 2 2\n5\n2 2 2 2 2\n3\n2 2 2\n5\n2 2 2 2 2\n" ]
[ "YES\n2 6\n1 2\n", "NO\n", "YES\n2 2\n4 1\n" ]
In the first example there are two sequences $[2, 3, 1, 3, 2]$ and $[1, 1, 2, 2, 2, 1]$. You can remove the second element from the first sequence to get $[2, 1, 3, 2]$ and you can remove the sixth element from the second sequence to get $[1, 1, 2, 2, 2]$. The sums of the both resulting sequences equal to $8$, i.e. the sums are equal.
[ { "input": "2\n5\n2 3 1 3 2\n6\n1 1 2 2 2 1", "output": "YES\n2 1\n1 4" }, { "input": "3\n1\n5\n5\n1 1 1 1 1\n2\n2 3", "output": "NO" }, { "input": "4\n6\n2 2 2 2 2 2\n5\n2 2 2 2 2\n3\n2 2 2\n5\n2 2 2 2 2", "output": "YES\n4 1\n2 5" }, { "input": "2\n2\n0 -10000\n2\n10000 0", "output": "YES\n2 1\n1 2" } ]
31
0
0
28,094
670
Correct Bracket Sequence Editor
[ "data structures", "dsu", "strings" ]
null
null
Recently Polycarp started to develop a text editor that works only with correct bracket sequences (abbreviated as CBS). Note that a bracket sequence is correct if it is possible to get a correct mathematical expression by adding "+"-s and "1"-s to it. For example, sequences "(())()", "()" and "(()(()))" are correct, while ")(", "(()" and "(()))(" are not. Each bracket in CBS has a pair. For example, in "(()(()))": - 1st bracket is paired with 8th, - 2d bracket is paired with 3d, - 3d bracket is paired with 2d, - 4th bracket is paired with 7th, - 5th bracket is paired with 6th, - 6th bracket is paired with 5th, - 7th bracket is paired with 4th, - 8th bracket is paired with 1st. Polycarp's editor currently supports only three operations during the use of CBS. The cursor in the editor takes the whole position of one of the brackets (not the position between the brackets!). There are three operations being supported: - «L» — move the cursor one position to the left, - «R» — move the cursor one position to the right, - «D» — delete the bracket in which the cursor is located, delete the bracket it's paired to and all brackets between them (that is, delete a substring between the bracket in which the cursor is located and the one it's paired to). After the operation "D" the cursor moves to the nearest bracket to the right (of course, among the non-deleted). If there is no such bracket (that is, the suffix of the CBS was deleted), then the cursor moves to the nearest bracket to the left (of course, among the non-deleted). There are pictures illustrated several usages of operation "D" below. All incorrect operations (shift cursor over the end of CBS, delete the whole CBS, etc.) are not supported by Polycarp's editor. Polycarp is very proud of his development, can you implement the functionality of his editor?
The first line contains three positive integers *n*, *m* and *p* (2<=≤<=*n*<=≤<=500<=000, 1<=≤<=*m*<=≤<=500<=000, 1<=≤<=*p*<=≤<=*n*) — the number of brackets in the correct bracket sequence, the number of operations and the initial position of cursor. Positions in the sequence are numbered from left to right, starting from one. It is guaranteed that *n* is even. It is followed by the string of *n* characters "(" and ")" forming the correct bracket sequence. Then follow a string of *m* characters "L", "R" and "D" — a sequence of the operations. Operations are carried out one by one from the first to the last. It is guaranteed that the given operations never move the cursor outside the bracket sequence, as well as the fact that after all operations a bracket sequence will be non-empty.
Print the correct bracket sequence, obtained as a result of applying all operations to the initial sequence.
[ "8 4 5\n(())()()\nRDLD\n", "12 5 3\n((()())(()))\nRRDLD\n", "8 8 8\n(())()()\nLLLLLLDD\n" ]
[ "()\n", "(()(()))\n", "()()\n" ]
In the first sample the cursor is initially at position 5. Consider actions of the editor: 1. command "R" — the cursor moves to the position 6 on the right; 1. command "D" — the deletion of brackets from the position 5 to the position 6. After that CBS takes the form (())(), the cursor is at the position 5; 1. command "L" — the cursor moves to the position 4 on the left; 1. command "D" — the deletion of brackets from the position 1 to the position 4. After that CBS takes the form (), the cursor is at the position 1. Thus, the answer is equal to ().
[ { "input": "8 4 5\n(())()()\nRDLD", "output": "()" }, { "input": "12 5 3\n((()())(()))\nRRDLD", "output": "(()(()))" }, { "input": "8 8 8\n(())()()\nLLLLLLDD", "output": "()()" }, { "input": "4 2 2\n()()\nLD", "output": "()" }, { "input": "6 4 1\n()()()\nDRRD", "output": "()" }, { "input": "8 2 4\n(())()()\nRR", "output": "(())()()" }, { "input": "10 7 3\n(()())()()\nRDLRDRD", "output": "()" }, { "input": "12 10 11\n(())()()()()\nDLRDLRDDLR", "output": "(())" }, { "input": "14 8 13\n((())())((()))\nDLRLLRLR", "output": "((())())()" }, { "input": "16 2 10\n(((())())())()()\nLD", "output": "(())()()" }, { "input": "18 8 11\n((()))(()()()())()\nLLLRRRRD", "output": "((()))(()()())()" }, { "input": "20 16 3\n(()()())()(())()()()\nLDRRRRRRLRLRLLLL", "output": "(()())()(())()()()" }, { "input": "22 9 12\n(()())((()()())())()()\nRDLLLRDRL", "output": "(()())((())())()()" }, { "input": "24 15 14\n((()())()()())(())()()()\nLDRRLDLDRRDDLRL", "output": "()" }, { "input": "26 3 15\n((())())(((())()()))(())()\nRDL", "output": "((())())(((())()))(())()" }, { "input": "28 13 16\n(()()())(()()())(())(())()()\nLRLDRRRRRLLLR", "output": "(()()())(()())(())(())()()" }, { "input": "30 18 15\n(()((()()())()(())())())()()()\nRRRLRRRLRRDLLLDRDR", "output": "()()" }, { "input": "32 6 19\n((()())((())())())((())()(()))()\nLDRLRR", "output": "((())()(()))()" }, { "input": "34 8 20\n(())((()())()((())())()()())()()()\nRLLDLRRL", "output": "(())((()())()((()))()()())()()()" }, { "input": "36 11 36\n(()()()()())((())())(()()())((())())\nLDLRLLLLRLR", "output": "(()()()()())((())())(()()())((()))" }, { "input": "38 8 26\n((((())())(()))(()()))(((())())())()()\nDDDLRLDR", "output": "((((())())(()))(()()))(())()()" }, { "input": "40 22 35\n(((()()()())()()())((())())()(())())()()\nDRRLDRLRLLLDLLLDRLLRLD", "output": "(())()" }, { "input": "42 7 29\n(((())()(()())())(((()())())(()())())())()\nDDRRRRD", "output": "(((())()(()())())(((()())()))())" }, { "input": "44 13 42\n((()()())()()()())(((()()())())()())(()())()\nLRRRLLDRDLDLR", "output": "((()()())()()()())(((()()())())())" }, { "input": "46 3 11\n(()()(())())(()())((()((())())(()())(())())())\nDDD", "output": "((()((())())(()())(())())())" }, { "input": "48 33 11\n((((())())((()()())())()()(()()))()(()())())()()\nRLRDLDRLLLRRRLRDLRLDDRRDRLRRDRLRD", "output": "(()(()())())()()" }, { "input": "50 32 32\n(()()())(())(())((()())())((())())((()())())(())()\nLRLLLRDRRDLRRRLRLLDDRLLRDLRDLRLD", "output": "(()()())(())(())((()()))" }, { "input": "52 24 39\n((()(()())(()())()())()())((()())(())())(())(()())()\nDRRDLDRLRRLLRRDRRLDRRLLL", "output": "((()(()())(()())()())()())((()())(()))()()" }, { "input": "54 22 3\n(((()())(())()())((()())())())((())((()()())()())())()\nLRLRDLRDLLRLDRLRRDRLRD", "output": "(()())()" }, { "input": "56 43 9\n(((((())())(()()))()()()())(()()(()))(()())(())())()()()\nRLRLDLRLLRLRLDLLRLRRLLLRLRRLDLDRDLLRLRRLLDR", "output": "()()()" }, { "input": "58 3 22\n((((())()())())((())())(())())(((())()()())(())()())()(())\nLLR", "output": "((((())()())())((())())(())())(((())()()())(())()())()(())" }, { "input": "60 50 23\n((((())(()())()())(()())()()()(()())())((())()())()())(())()\nDRDLLDDLLLLDDRRDRDLLLRRRLRLDDDLRLLRRDLRLRRDDDRDRRL", "output": "(()())(())()" }, { "input": "62 34 43\n(()((()())()()))(((())())()(()())(())())((())(()(()())()))()()\nRLDDDDDDLRDLLRLDRLLDLRLDLLDRLLRRLL", "output": "(())" }, { "input": "64 19 15\n((((())((())())()())(())())(()())(()())())((()()())(())())()()()\nDRRLRLRDDDDLLDRLRLD", "output": "()()()" }, { "input": "66 55 24\n(((())(((()())()()))(()())(()())())(())((()())())(()()())())()()()\nRDLRLRRRLRDLRRLLDDRDRRDLRLDRRDRDLRDDLLRRDRDRLRRLLLDLRRR", "output": "()()()()" }, { "input": "68 34 8\n((()(()())()())(()))((()())()())((()()())())(((())(()))(())()(())())\nDLRRLRRRDLLDLLDDDLRRLRLRRRDDRLRRLL", "output": "((()())()())((()()())())(((())(()))(())()(())())" }, { "input": "70 33 26\n((()(())()())((())())(()())(())())((()((()())()())())()()(())())(()())\nDLDRRRLRLDLRLLRDDRLRRLLLRDRLRLDRL", "output": "(()())" }, { "input": "72 23 38\n(((((()()())()())(((()()))(())())()(()())(()(())())))(())((())())())()()\nRDLRLRRRDLLRDLRDLLRRLLD", "output": "()()" }, { "input": "74 26 27\n(((()()())())(())()())((()()(())())()())((()()())()())(()()())(()()())()()\nLDRLLRLRLLDDDLDRRDRLLRDLRD", "output": "()()()" }, { "input": "76 51 69\n(((())()())())(()()()()())(((((())(())())())())(((()(())())(()()())())()))()\nLRLLRRLLLDRDDRLLDLRLRDRLRDLRLRLRLLDLRLRLLLDDLLRRDLD", "output": "(((())()()))" }, { "input": "78 33 22\n(((()((()()())())()()())((()())()())(())())(((((())())()())()())(())())())()()\nRDRRRRRLDRDLDRLLLLDRDRRRDLDRDLLRD", "output": "((((((())())()())()())(())())())()()" }, { "input": "2 1 1\n()\nR", "output": "()" }, { "input": "80 31 30\n(((()()())(((())())((()())()()())()()))(()()()())(()())(()())(())(())()()()())()\nDDDLLDLDDLRLRLDDRDRRLDRDLLDRLRL", "output": "()" }, { "input": "82 16 6\n(((())())(())()())(((()()((()()))())()(())())(()())(())((())())()()())(()()()())()\nRLLLLRRDDRRLRRRL", "output": "((())(())()())(((()()((()()))())()(())())(()())(())((())())()()())(()()()())()" }, { "input": "84 18 78\n(())(((()(()))()((((()())())(()())())()())((()())())())(((())(())())(())())())()()()\nLLLRDDLRDRLDDLLRRL", "output": "(())" }, { "input": "86 11 62\n(((())())(((()())())()()())(()())(()()())()())((()()())())(((())()())((())(()())())())\nDLDLRLRLRRR", "output": "(((())())(((()())())()()())(()())(()()())()())((()()())())((()())((())(()())())())" }, { "input": "88 33 12\n(())((((())()((()())())())(((())())(())()())(()))((()())())())(((())()())(())()())()()()\nLLLRRLRDRDRLDDLLRDLLDRLRDDLDRDLRR", "output": "(())()()" }, { "input": "90 44 6\n(((((())()())(((()())())())()()))(()())((())()())(()())((())())(()()())())(())((())())()()\nRLDLRRLLDRDDDLRDRRDLLRRDDDDLRLRDRLLDRDLRDDRR", "output": "()()" }, { "input": "92 51 30\n(()(((()())(()())())())(()())()()()())((()()())(())(())(()((())()())())(())())((())()())()()\nLRLRLLLLRRRLLRRLDLRLRRLRDLDLDLDDRRLRRRLLRDRLDDRLRRD", "output": "(()()())()()" }, { "input": "94 48 47\n(((()(())())(((())())())()())()()())((()()())(()(()()()())())())(()())(()(())(())()())(()())()\nLLLLLLDLDRLLDLRRDLLLLRLLDLLRRDDRDRRLLRRDRRRDRLLD", "output": "((())()())(()())()" }, { "input": "96 37 18\n((()()()())((((())()())())(())()())()()())(((())()(()(())())()()())(())())((()())()()())(()())()\nDDLRRDDLDLRDDDRLDLRRDDDLLDRRRDDLDLLRL", "output": "((()()()))((()())()()())(()())()" }, { "input": "98 38 40\n((()((((()))(())(()(())))))((())()())(())()())((((()())(((()()))()))()(())()()())())((()))(())()()\nLRLRRDLDDRRLRDRDDLDRDLDRDLRLRLRLRLRLRR", "output": "()()()" }, { "input": "100 57 80\n(((())(()))(()())())((((()()()())((())())()())(()((()())()()()))())()()())((())()((())()))((()))()()\nLLRRLLLRLRLRLDLLRRRDDLRDDDLRLRLLLRLRRRLLDRLRDLLDLRLRLDDLR", "output": "(((())(()))(()())())" }, { "input": "10 3 3\n(())((()))\nDRD", "output": "()" } ]
779
36,044,800
-1
28,098