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
61
Enemy is weak
[ "data structures", "trees" ]
E. Enemy is weak
5
256
The Romans have attacked again. This time they are much more than the Persians but Shapur is ready to defeat them. He says: "A lion is never afraid of a hundred sheep". Nevertheless Shapur has to find weaknesses in the Roman army to defeat them. So he gives the army a weakness number. In Shapur's opinion the weakness of an army is equal to the number of triplets *i*,<=*j*,<=*k* such that *i*<=&lt;<=*j*<=&lt;<=*k* and *a**i*<=&gt;<=*a**j*<=&gt;<=*a**k* where *a**x* is the power of man standing at position *x*. The Roman army has one special trait β€” powers of all the people in it are distinct. Help Shapur find out how weak the Romans are.
The first line of input contains a single number *n* (3<=≀<=*n*<=≀<=106) β€” the number of men in Roman army. Next line contains *n* different positive integers *a**i* (1<=≀<=*i*<=≀<=*n*,<=1<=≀<=*a**i*<=≀<=109) β€” powers of men in the Roman army.
A single integer number, the weakness of the Roman army. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
[ "3\n3 2 1\n", "3\n2 3 1\n", "4\n10 8 3 1\n", "4\n1 5 4 3\n" ]
[ "1\n", "0\n", "4\n", "1\n" ]
none
[ { "input": "3\n3 2 1", "output": "1" }, { "input": "3\n2 3 1", "output": "0" }, { "input": "4\n10 8 3 1", "output": "4" }, { "input": "4\n1 5 4 3", "output": "1" }, { "input": "9\n10 9 5 6 8 3 4 7 11", "output": "20" }, { "input": "7\n11 3 8 4 2 9 6", "output": "7" }, { "input": "6\n2 1 10 7 3 5", "output": "2" }, { "input": "4\n1 5 3 10", "output": "0" }, { "input": "3\n2 7 11", "output": "0" }, { "input": "5\n4 11 7 5 10", "output": "1" }, { "input": "72\n685 154 298 660 716 963 692 257 397 974 92 191 519 838 828 957 687 776 636 997 101 800 579 181 691 256 95 531 333 347 803 682 252 655 297 892 833 31 239 895 45 235 394 909 486 400 621 443 348 471 59 791 934 195 861 356 876 741 763 431 781 639 193 291 230 171 288 187 657 273 200 924", "output": "12140" }, { "input": "20\n840 477 436 149 554 528 671 67 630 382 805 329 781 980 237 589 743 451 633 24", "output": "185" }, { "input": "59\n996 800 927 637 393 741 650 524 863 789 517 467 408 442 988 701 528 215 490 764 282 990 991 244 70 510 36 151 193 378 102 818 384 621 349 476 658 985 465 366 807 32 430 814 945 733 382 751 380 136 405 585 494 862 598 425 421 90 72", "output": "7842" }, { "input": "97\n800 771 66 126 231 306 981 96 196 229 253 35 903 739 461 962 979 347 152 424 934 586 225 838 103 178 524 400 156 149 560 629 697 417 717 738 181 430 611 513 754 595 847 464 356 640 24 854 138 481 98 371 142 460 194 288 605 41 999 581 441 407 301 651 271 226 457 393 980 166 272 250 900 337 358 359 80 904 53 39 558 569 101 339 752 432 889 285 836 660 190 180 601 136 527 990 612", "output": "26086" }, { "input": "45\n955 94 204 615 69 519 960 791 977 603 294 391 662 364 139 222 748 742 540 567 230 830 558 959 329 169 854 503 423 210 832 87 990 44 7 777 138 898 845 733 570 476 113 233 630", "output": "2676" }, { "input": "84\n759 417 343 104 908 84 940 248 210 10 6 529 289 826 890 982 533 506 412 280 709 175 425 891 727 914 235 882 834 445 912 163 263 998 391 948 836 538 615 854 275 198 631 267 148 955 418 961 642 132 599 657 389 879 177 739 536 932 682 928 660 821 15 878 521 990 518 765 79 544 771 134 611 244 608 809 733 832 933 270 397 349 798 857", "output": "12571" }, { "input": "32\n915 740 482 592 394 648 919 705 443 418 719 315 916 287 289 743 319 270 269 668 203 119 20 224 847 500 949 910 164 468 965 846", "output": "1230" }, { "input": "34\n718 63 972 81 233 861 250 515 676 825 431 453 543 748 41 503 104 34 126 57 346 616 557 615 733 15 938 495 491 667 177 317 367 85", "output": "1202" }, { "input": "73\n874 34 111 922 71 426 229 972 557 232 144 590 170 210 792 616 890 798 983 797 488 8 859 538 736 319 82 966 474 513 721 860 493 375 81 69 662 444 766 451 571 94 365 833 720 703 826 270 437 542 147 800 146 173 564 160 928 57 732 774 292 250 716 131 949 1 216 456 53 322 403 195 460", "output": "13229" }, { "input": "90\n301 241 251 995 267 292 335 623 270 144 291 757 950 21 808 109 971 340 678 377 743 841 669 333 528 988 336 233 118 781 138 47 972 68 234 812 629 701 520 842 156 348 600 26 94 912 903 552 470 456 61 273 93 810 545 231 450 926 172 246 884 79 614 728 533 491 76 589 668 487 409 650 433 677 124 407 956 794 299 763 843 290 591 216 844 731 327 34 687 649", "output": "17239" } ]
2,340
213,606,400
3.368127
14,724
873
Awards For Contestants
[ "brute force", "data structures", "dp" ]
null
null
Alexey recently held a programming contest for students from Berland. *n* students participated in a contest, *i*-th of them solved *a**i* problems. Now he wants to award some contestants. Alexey can award the students with diplomas of three different degrees. Each student either will receive one diploma of some degree, or won't receive any diplomas at all. Let *cnt**x* be the number of students that are awarded with diplomas of degree *x* (1<=≀<=*x*<=≀<=3). The following conditions must hold: - For each *x* (1<=≀<=*x*<=≀<=3) *cnt**x*<=&gt;<=0; - For any two degrees *x* and *y* *cnt**x*<=≀<=2Β·*cnt**y*. Of course, there are a lot of ways to distribute the diplomas. Let *b**i* be the degree of diploma *i*-th student will receive (or <=-<=1 if *i*-th student won't receive any diplomas). Also for any *x* such that 1<=≀<=*x*<=≀<=3 let *c**x* be the maximum number of problems solved by a student that receives a diploma of degree *x*, and *d**x* be the minimum number of problems solved by a student that receives a diploma of degree *x*. Alexey wants to distribute the diplomas in such a way that: 1. If student *i* solved more problems than student *j*, then he has to be awarded not worse than student *j* (it's impossible that student *j* receives a diploma and *i* doesn't receive any, and also it's impossible that both of them receive a diploma, but *b**j*<=&lt;<=*b**i*); 1. *d*1<=-<=*c*2 is maximum possible; 1. Among all ways that maximize the previous expression, *d*2<=-<=*c*3 is maximum possible; 1. Among all ways that correspond to the two previous conditions, *d*3<=-<=*c*<=-<=1 is maximum possible, where *c*<=-<=1 is the maximum number of problems solved by a student that doesn't receive any diploma (or 0 if each student is awarded with some diploma). Help Alexey to find a way to award the contestants!
The first line contains one integer number *n* (3<=≀<=*n*<=≀<=3000). The second line contains *n* integer numbers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=5000).
Output *n* numbers. *i*-th number must be equal to the degree of diploma *i*-th contestant will receive (or <=-<=1 if he doesn't receive any diploma). If there are multiple optimal solutions, print any of them. It is guaranteed that the answer always exists.
[ "4\n1 2 3 4\n", "6\n1 4 3 1 1 2\n" ]
[ "3 3 2 1 \n", "-1 1 2 -1 -1 3 \n" ]
none
[ { "input": "4\n1 2 3 4", "output": "3 3 2 1 " }, { "input": "6\n1 4 3 1 1 2", "output": "-1 1 2 -1 -1 3 " }, { "input": "100\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 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": "3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 " }, { "input": "100\n82 51 81 14 37 17 78 92 64 15 8 86 89 8 87 77 66 10 15 12 100 25 92 47 21 78 20 63 13 49 41 36 41 79 16 87 87 69 3 76 80 60 100 49 70 59 72 8 38 71 45 97 71 14 76 54 81 4 59 46 39 29 92 3 49 22 53 99 59 52 74 31 92 43 42 23 44 9 82 47 7 40 12 9 3 55 37 85 46 22 84 52 98 41 21 77 63 17 62 91", "output": "-1 -1 -1 -1 -1 -1 -1 2 -1 -1 -1 3 3 -1 3 -1 -1 -1 -1 -1 1 -1 2 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 3 3 -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 2 -1 -1 -1 -1 1 -1 -1 -1 -1 2 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 3 -1 -1 3 -1 1 -1 -1 -1 -1 -1 -1 2 " }, { "input": "100\n591 417 888 251 792 847 685 3 182 461 102 348 555 956 771 901 712 878 580 631 342 333 285 899 525 725 537 718 929 653 84 788 104 355 624 803 253 853 201 995 536 184 65 205 540 652 549 777 248 405 677 950 431 580 600 846 328 429 134 983 526 103 500 963 400 23 276 704 570 757 410 658 507 620 984 244 486 454 802 411 985 303 635 283 96 597 855 775 139 839 839 61 219 986 776 72 729 69 20 917", "output": "2 3 1 3 2 1 2 -1 3 3 -1 3 2 1 2 1 2 1 2 2 3 3 3 1 2 2 2 2 1 2 -1 2 -1 3 2 2 3 1 3 1 2 3 -1 3 2 2 2 2 3 3 2 1 3 2 2 1 3 3 -1 1 2 -1 2 1 3 -1 3 2 2 2 3 2 2 2 1 3 2 3 2 3 1 3 2 3 -1 2 1 2 -1 1 1 -1 3 1 2 -1 2 -1 -1 1 " }, { "input": "70\n30 19 11 23 3 21 12 30 8 21 22 13 32 19 12 30 19 25 22 25 7 14 15 16 32 29 9 18 6 26 26 26 2 11 27 30 19 22 20 23 1 2 9 7 1 28 22 27 33 12 32 3 8 19 27 5 3 29 20 28 13 1 30 29 28 14 27 30 6 4", "output": "1 2 3 2 3 2 3 1 3 2 2 3 1 2 3 1 2 1 2 1 3 3 3 3 1 1 3 2 3 1 1 1 3 3 1 1 2 2 2 2 3 3 3 3 3 1 2 1 1 3 1 3 3 2 1 3 3 1 2 1 3 3 1 1 1 3 1 1 3 3 " }, { "input": "54\n30 28 29 28 60 27 57 45 22 18 12 12 64 43 12 60 56 72 71 21 37 3 7 15 8 66 70 68 40 62 48 53 32 37 44 46 1 58 47 32 22 19 46 58 59 69 13 67 14 15 20 46 12 39", "output": "3 3 3 3 1 3 1 2 3 3 3 3 1 2 3 1 1 1 1 3 2 -1 3 3 3 1 1 1 2 1 2 1 3 2 2 2 -1 1 2 3 3 3 2 1 1 1 3 1 3 3 3 2 3 2 " }, { "input": "8\n99 88 58 84 34 109 70 11", "output": "1 1 2 1 3 1 2 3 " }, { "input": "86\n241 180 140 393 301 202 217 323 150 101 175 221 148 94 338 360 149 193 387 262 309 282 88 362 151 50 234 330 325 379 42 87 204 167 245 108 374 130 200 104 49 47 261 56 111 287 32 190 197 150 206 140 290 287 221 346 218 188 178 95 400 181 214 264 403 340 218 162 175 140 280 283 329 3 3 241 290 161 242 386 308 128 310 161 15 343", "output": "2 2 3 1 1 2 2 1 3 3 2 2 3 3 1 1 3 2 1 2 1 1 3 1 3 -1 2 1 1 1 -1 3 2 2 2 3 1 3 2 3 -1 -1 2 -1 3 1 -1 2 2 3 2 3 1 1 2 1 2 2 2 3 1 2 2 2 1 1 2 2 2 3 1 1 1 -1 -1 2 1 2 2 1 1 3 1 2 -1 1 " }, { "input": "8\n64 54 6 736 630 113 870 61", "output": "2 3 3 1 1 2 1 2 " }, { "input": "3\n100 100 100", "output": "3 2 1 " }, { "input": "3\n19 435 12", "output": "2 1 3 " }, { "input": "3\n4998 4999 5000", "output": "3 2 1 " }, { "input": "11\n5 4 7 5 2 7 8 5 7 8 8", "output": "3 3 2 3 3 2 1 3 2 1 1 " }, { "input": "8\n3 3 2 3 4 2 2 3", "output": "3 3 -1 2 1 -1 -1 2 " }, { "input": "6\n7 7 7 7 6 7", "output": "3 2 2 1 3 1 " }, { "input": "10\n1 1 1 8 8 1 1 8 8 8", "output": "-1 3 3 2 2 3 3 2 1 1 " }, { "input": "6\n401 351 548 829 698 438", "output": "3 -1 2 1 1 3 " }, { "input": "84\n362 480 551 307 4 118 376 541 494 472 75 450 192 458 450 390 447 62 239 362 301 243 248 102 85 430 231 195 316 283 128 252 569 282 205 390 461 114 390 121 3 125 23 471 88 13 8 289 143 352 523 217 342 98 116 279 327 133 199 164 89 318 76 480 199 401 32 430 281 438 460 484 433 292 433 210 137 138 172 501 253 417 120 432", "output": "-1 2 1 -1 -1 -1 -1 1 2 2 -1 3 -1 3 3 -1 3 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1 -1 -1 -1 3 -1 -1 -1 -1 -1 -1 2 -1 -1 -1 -1 -1 -1 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 2 -1 -1 -1 -1 -1 -1 3 2 -1 -1 -1 -1 -1 -1 -1 2 -1 -1 -1 -1 " } ]
140
0
0
14,759
145
Lucky Subsequence
[ "combinatorics", "dp", "math" ]
null
null
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya has sequence *a* consisting of *n* integers. The subsequence of the sequence *a* is such subsequence that can be obtained from *a* by removing zero or more of its elements. Two sequences are considered different if index sets of numbers included in them are different. That is, the values ​of the elements ​do not matter in the comparison of subsequences. In particular, any sequence of length *n* has exactly 2*n* different subsequences (including an empty subsequence). A subsequence is considered lucky if it has a length exactly *k* and does not contain two identical lucky numbers (unlucky numbers can be repeated any number of times). Help Petya find the number of different lucky subsequences of the sequence *a*. As Petya's parents don't let him play with large numbers, you should print the result modulo prime number 1000000007 (109<=+<=7).
The first line contains two integers *n* and *k* (1<=≀<=*k*<=≀<=*n*<=≀<=105). The next line contains *n* integers *a**i* (1<=≀<=*a**i*<=≀<=109) β€” the sequence *a*.
On the single line print the single number β€” the answer to the problem modulo prime number 1000000007 (109<=+<=7).
[ "3 2\n10 10 10\n", "4 2\n4 4 7 7\n" ]
[ "3\n", "4\n" ]
In the first sample all 3 subsequences of the needed length are considered lucky. In the second sample there are 4 lucky subsequences. For them the sets of indexes equal (the indexation starts from 1): {1, 3}, {1, 4}, {2, 3} and {2, 4}.
[ { "input": "3 2\n10 10 10", "output": "3" }, { "input": "4 2\n4 4 7 7", "output": "4" }, { "input": "7 4\n1 2 3 4 5 6 7", "output": "35" }, { "input": "7 4\n7 7 7 7 7 7 7", "output": "0" }, { "input": "10 1\n1 2 3 4 5 6 7 8 9 10", "output": "10" }, { "input": "10 7\n1 2 3 4 5 6 7 8 9 10", "output": "120" }, { "input": "20 7\n1 4 5 8 47 777777777 1 5 4 8 5 9 5 4 7 4 5 7 7 44474", "output": "29172" }, { "input": "5 2\n47 47 47 47 47", "output": "0" }, { "input": "13 5\n44 44 44 44 44 44 44 44 77 55 66 99 55", "output": "41" }, { "input": "3 2\n1 47 47", "output": "2" }, { "input": "2 2\n47 47", "output": "0" }, { "input": "2 2\n44 44", "output": "0" } ]
62
0
0
14,760
378
Semifinals
[ "implementation", "sortings" ]
null
null
Two semifinals have just been in the running tournament. Each semifinal had *n* participants. There are *n* participants advancing to the finals, they are chosen as follows: from each semifinal, we choose *k* people (0<=≀<=2*k*<=≀<=*n*) who showed the best result in their semifinals and all other places in the finals go to the people who haven't ranked in the top *k* in their semifinal but got to the *n*<=-<=2*k* of the best among the others. The tournament organizers hasn't yet determined the *k* value, so the participants want to know who else has any chance to get to the finals and who can go home.
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=105) β€” the number of participants in each semifinal. Each of the next *n* lines contains two integers *a**i* and *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=109)Β β€” the results of the *i*-th participant (the number of milliseconds he needs to cover the semifinals distance) of the first and second semifinals, correspondingly. All results are distinct. Sequences *a*1, *a*2, ..., *a**n* and *b*1, *b*2, ..., *b**n* are sorted in ascending order, i.e. in the order the participants finished in the corresponding semifinal.
Print two strings consisting of *n* characters, each equals either "0" or "1". The first line should correspond to the participants of the first semifinal, the second line should correspond to the participants of the second semifinal. The *i*-th character in the *j*-th line should equal "1" if the *i*-th participant of the *j*-th semifinal has any chances to advance to the finals, otherwise it should equal a "0".
[ "4\n9840 9920\n9860 9980\n9930 10020\n10040 10090\n", "4\n9900 9850\n9940 9930\n10000 10020\n10060 10110\n" ]
[ "1110\n1100\n", "1100\n1100\n" ]
Consider the first sample. Each semifinal has 4 participants. The results of the first semifinal are 9840, 9860, 9930, 10040. The results of the second semifinal are 9920, 9980, 10020, 10090. - If *k* = 0, the finalists are determined by the time only, so players 9840, 9860, 9920 and 9930 advance to the finals. - If *k* = 1, the winners from both semifinals move to the finals (with results 9840 and 9920), and the other places are determined by the time (these places go to the sportsmen who run the distance in 9860 and 9930 milliseconds). - If *k* = 2, then first and second places advance from each seminfial, these are participants with results 9840, 9860, 9920 and 9980 milliseconds.
[ { "input": "4\n9840 9920\n9860 9980\n9930 10020\n10040 10090", "output": "1110\n1100" }, { "input": "4\n9900 9850\n9940 9930\n10000 10020\n10060 10110", "output": "1100\n1100" }, { "input": "1\n1 2", "output": "1\n0" }, { "input": "1\n2 1", "output": "0\n1" }, { "input": "2\n1 2\n3 4", "output": "10\n10" }, { "input": "2\n3 1\n4 2", "output": "10\n11" }, { "input": "3\n1 3\n2 5\n4 6", "output": "110\n100" }, { "input": "3\n2 1\n4 3\n5 6", "output": "100\n110" }, { "input": "3\n1 4\n2 5\n3 6", "output": "111\n100" }, { "input": "4\n5 1\n6 2\n7 3\n8 4", "output": "1100\n1111" }, { "input": "2\n1 2\n4 3", "output": "10\n10" }, { "input": "3\n1 2\n3 5\n4 6", "output": "110\n100" }, { "input": "3\n1 2\n3 4\n5 6", "output": "110\n100" }, { "input": "3\n1 3\n2 4\n5 6", "output": "110\n100" }, { "input": "3\n1 3\n2 4\n6 5", "output": "110\n100" }, { "input": "3\n2 1\n3 4\n6 5", "output": "110\n100" }, { "input": "3\n1 2\n4 3\n6 5", "output": "100\n110" }, { "input": "3\n2 1\n3 5\n4 6", "output": "110\n100" }, { "input": "4\n1 4\n2 5\n3 6\n8 7", "output": "1110\n1100" }, { "input": "4\n1 3\n2 4\n7 5\n8 6", "output": "1100\n1100" }, { "input": "4\n2 1\n3 4\n6 5\n7 8", "output": "1100\n1100" }, { "input": "8\n100 101\n200 201\n300 301\n310 400\n320 500\n330 600\n340 700\n350 800", "output": "11111000\n11110000" } ]
1,000
38,707,200
0
14,767
108
Datatypes
[ "math", "sortings" ]
B. Datatypes
2
256
Tattah's youngest brother, Tuftuf, is new to programming. Since his older brother is such a good programmer, his biggest dream is to outshine him. Tuftuf is a student at the German University in Cairo (GUC) where he learns to write programs in Gava. Today, Tuftuf was introduced to Gava's unsigned integer datatypes. Gava has *n* unsigned integer datatypes of sizes (in bits) *a*1,<=*a*2,<=... *a**n*. The *i*-th datatype have size *a**i* bits, so it can represent every integer between 0 and 2*a**i*<=-<=1 inclusive. Tuftuf is thinking of learning a better programming language. If there exists an integer *x*, such that *x* fits in some type *i* (in *a**i* bits) and *x*Β·*x* does not fit in some other type *j* (in *a**j* bits) where *a**i*<=&lt;<=*a**j*, then Tuftuf will stop using Gava. Your task is to determine Tuftuf's destiny.
The first line contains integer *n* (2<=≀<=*n*<=≀<=105) β€” the number of Gava's unsigned integer datatypes' sizes. The second line contains a single-space-separated list of *n* integers (1<=≀<=*a**i*<=≀<=109) β€” sizes of datatypes in bits. Some datatypes may have equal sizes.
Print "YES" if Tuftuf will stop using Gava, and "NO" otherwise.
[ "3\n64 16 32\n", "4\n4 2 1 3\n" ]
[ "NO\n", "YES\n" ]
In the second example, *x* = 7 (111<sub class="lower-index">2</sub>) fits in 3 bits, but *x*<sup class="upper-index">2</sup> = 49 (110001<sub class="lower-index">2</sub>) does not fit in 4 bits.
[ { "input": "3\n64 16 32", "output": "NO" }, { "input": "4\n4 2 1 3", "output": "YES" }, { "input": "5\n1 5 3 3 2", "output": "YES" }, { "input": "52\n474 24 24 954 9 234 474 114 24 114 234 24 114 114 234 9 9 24 9 54 234 54 9 954 474 9 54 54 54 234 9 114 24 54 114 954 954 474 24 54 54 234 234 474 474 24 114 9 954 954 954 474", "output": "NO" }, { "input": "56\n43 641 626 984 107 521 266 835 707 220 402 406 558 199 988 685 843 808 182 73 553 17 765 979 116 178 489 271 532 889 26 263 654 680 240 392 980 267 264 46 888 444 874 519 735 301 743 526 376 793 40 110 811 184 82 96", "output": "YES" }, { "input": "9\n20 44 92 8 20 380 8 188 764", "output": "NO" }, { "input": "97\n250 58 26 506 58 122 506 506 250 506 26 58 26 58 10 26 58 58 2 506 506 10 10 2 26 26 122 58 506 10 506 58 250 2 26 122 122 10 250 58 2 58 58 122 10 506 26 122 26 2 2 2 250 506 2 506 10 2 26 122 250 2 250 122 10 250 10 26 58 122 58 2 2 10 250 250 26 250 10 250 506 122 122 122 506 26 58 10 122 10 250 10 2 2 26 250 122", "output": "NO" }, { "input": "85\n436 23 384 417 11 227 713 910 217 177 227 161 851 396 556 948 700 819 920 451 877 249 332 189 606 986 627 468 877 682 497 579 189 443 252 795 147 642 643 569 250 863 615 560 142 752 918 167 677 49 750 871 282 721 102 884 179 980 392 509 178 977 51 241 912 599 142 975 453 353 350 130 837 955 688 7 588 239 194 277 50 865 227 848 538", "output": "YES" }, { "input": "43\n906 652 445 325 991 682 173 290 731 528 432 615 698 132 874 38 643 301 223 442 722 529 150 659 593 22 679 178 410 978 201 559 115 533 586 790 703 596 492 591 781 761 384", "output": "YES" }, { "input": "8\n421 250 398 257 512 329 25 972", "output": "YES" }, { "input": "2\n1000000000 999999999", "output": "YES" }, { "input": "220\n10 6 6 2 8 6 6 5 6 2 10 3 9 10 10 2 3 5 2 2 4 7 6 6 7 5 6 2 10 10 1 1 2 2 3 2 4 4 8 1 1 2 1 10 9 2 1 4 2 1 7 4 8 4 2 9 7 7 6 6 8 3 1 9 10 6 3 5 9 5 1 1 8 3 10 8 10 3 7 9 2 4 8 2 8 4 10 5 7 10 6 8 3 5 7 9 4 2 6 2 2 7 7 2 10 1 1 8 7 4 8 8 9 1 1 9 5 5 5 3 5 5 3 2 6 4 7 9 10 9 3 1 10 1 7 8 8 7 6 5 1 5 6 2 1 9 9 10 8 4 9 5 4 8 10 4 9 2 3 7 10 3 3 9 10 5 7 7 6 7 3 1 5 7 10 6 3 5 4 7 8 6 10 10 10 8 3 5 1 1 1 10 2 3 5 5 2 5 8 4 7 3 1 10 1 10 9 2 10 3 4 9 1 5 9 8 2 7 7 2", "output": "YES" }, { "input": "7\n1 2 3 4 8 16 32", "output": "YES" }, { "input": "2\n1 1", "output": "NO" }, { "input": "2\n1 2", "output": "NO" }, { "input": "3\n1 2 2", "output": "NO" }, { "input": "3\n1 1 2", "output": "NO" } ]
466
10,854,400
3.863282
14,768
670
Restore a Number
[ "brute force", "constructive algorithms", "strings" ]
null
null
Vasya decided to pass a very large integer *n* to Kate. First, he wrote that number as a string, then he appended to the right integer *k*Β β€” the number of digits in *n*. Magically, all the numbers were shuffled in arbitrary order while this note was passed to Kate. The only thing that Vasya remembers, is a non-empty substring of *n* (a substring of *n* is a sequence of consecutive digits of the number *n*). Vasya knows that there may be more than one way to restore the number *n*. Your task is to find the smallest possible initial integer *n*. Note that decimal representation of number *n* contained no leading zeroes, except the case the integer *n* was equal to zero itself (in this case a single digit 0 was used).
The first line of the input contains the string received by Kate. The number of digits in this string does not exceed 1<=000<=000. The second line contains the substring of *n* which Vasya remembers. This string can contain leading zeroes. It is guaranteed that the input data is correct, and the answer always exists.
Print the smalles integer *n* which Vasya could pass to Kate.
[ "003512\n021\n", "199966633300\n63\n" ]
[ "30021\n", "3036366999\n" ]
none
[ { "input": "003512\n021", "output": "30021" }, { "input": "199966633300\n63", "output": "3036366999" }, { "input": "01\n0", "output": "0" }, { "input": "0000454312911\n9213544", "output": "92135440000" }, { "input": "13\n3", "output": "3" }, { "input": "00010454312921\n9213544", "output": "100009213544" }, { "input": "11317110\n01", "output": "1011113" }, { "input": "1516532320120301262110112013012410838210025280432402042406224604110031740090203024020012\n0126064", "output": "10000000000000000000000012606411111111111111222222222222222222333333334444444455567889" }, { "input": "233121122272652143504001162131110307236110231414093112213120271312010423132181004\n0344011", "output": "1000000000003440111111111111111111111112222222222222222233333333333444455666778" }, { "input": "1626112553124100114021300410533124010061200562040601301\n00612141", "output": "10000000000000006121411111111111222222333344445556666" }, { "input": "040005088\n0", "output": "40000058" }, { "input": "420002200110100211206222101201021321440210\n00", "output": "1000000000000011111111112222222222223446" }, { "input": "801095116\n0", "output": "10011569" }, { "input": "070421120216020020\n000024", "output": "1000000024122227" }, { "input": "825083\n0", "output": "20388" }, { "input": "6201067\n0", "output": "100267" }, { "input": "34404430311310306128103301112523111011050561125004200941114005444000000040133002103062151514033103\n010215110013511400400140133404", "output": "100000000000000000000102151100135114004001401334041111111111111122222233333333333444444455555668" }, { "input": "14\n4", "output": "4" }, { "input": "21\n2", "output": "2" }, { "input": "204\n4", "output": "40" }, { "input": "12\n2", "output": "2" }, { "input": "05740110115001520111222011422101032503200010203300510014413\n000151", "output": "100000000000000000001511111111111111222222222333334444555" }, { "input": "116051111111001510011110101111111101001111111101111101101\n00111111111", "output": "1000000000000011111111111111111111111111111111111111116" }, { "input": "1161100\n01110", "output": "101110" }, { "input": "101313020013110703821620035452130200177115540090000\n002001320", "output": "1000000000000002001320111111111222333334555567778" }, { "input": "03111100110111111118\n01001111111101111", "output": "301001111111101111" }, { "input": "01170141\n01114", "output": "1001114" }, { "input": "0500014440100110264222000342611000102247070652310723\n0003217", "output": "10000000000000000032171111112222222233444444566677" }, { "input": "111011111101111131113111111111011\n0111111111111111010111111111", "output": "1011111111111111101011111111113" }, { "input": "11003040044200003323519101102070252000010622902208104150200400140042011224011154237302003323632011235\n0", "output": "100000000000000000000000000000000001111111111111111222222222222222222333333333334444444445555566778" }, { "input": "111111011110101141110110011010011114110111\n01010111111011111", "output": "1000000101011111101111111111111111111114" }, { "input": "011010171110\n010110117", "output": "1010110117" }, { "input": "510017\n0", "output": "10017" }, { "input": "00111111110114112110011105\n0", "output": "100000011111111111111115" }, { "input": "320403902031031110003113410860101243100423120201101124080311242010930103200001451200132304400000\n01", "output": "1000000000000000000000000000000000011111111111111111111122222222222233333333333334444444456889" }, { "input": "125\n15", "output": "15" }, { "input": "1160190\n110019", "output": "110019" }, { "input": "11111111111101101111110101011111010101001111001110010011810010110111101101112140110110\n110101100101111101011111111101111111111110111110011111011000111010100111011111000002", "output": "110101100101111101011111111101111111111110111110011111011000111010100111011111000002" }, { "input": "2206026141112316065224201412118064151200614042100160093001020024005013121010030020083221011\n280060226", "output": "10000000000000000000000000111111111111111111111222222222222228006022633333444444455566666" }, { "input": "63007511113226210230771304213600010311075400082011350143450007091200\n25", "output": "100000000000000000000011111111111112222222533333333444455567777789" }, { "input": "142245201505011321217122212\n12521721230", "output": "1001111125217212302222445" }, { "input": "712\n17", "output": "17" }, { "input": "11011111111003010101111111111103111\n101111111110110111111011001011111", "output": "101111111110110111111011001011111" }, { "input": "143213104201201003340424615500135122127119000020020017400111102423312241032010400\n235321200411204201121201304100003", "output": "1000000000000001111111111222222223532120041120420112120130410000333334444445567" }, { "input": "080001181\n18", "output": "10000118" }, { "input": "4141403055010511470013300502174230460332129228041229160601006121052601201100001153120100000\n49", "output": "10000000000000000000000000000011111111111111111112222222222223333333444444495555556666677" }, { "input": "2131\n112", "output": "112" }, { "input": "0111110011011110111012109101101111101111150011110111110111001\n10110010111111011111111011001101001111111111111110001011012", "output": "10110010111111011111111011001101001111111111111110001011012" }, { "input": "251137317010111402300506643001203241303324162124225270011006213015100\n3512", "output": "1000000000000000001111111111111122222222223333333335124444455566677" }, { "input": "12140051050330004342310455231200020252193200\n23012", "output": "100000000000001111222222301233333444555559" }, { "input": "291\n19", "output": "19" }, { "input": "11011011000111101111111111081101110001011111101111110111111111011111011011111100111\n1110111111111", "output": "100000000000000000011101111111111111111111111111111111111111111111111111111111111" }, { "input": "170422032160671323013220212523333410720410110020005012206133500200001015971250190240204004002041\n10010405153200037262043200214001340010615320", "output": "1000000000000000100104051532000372620432002140013400106153201111111122222222222233333445567779" }, { "input": "210042022032002310001424611003103312001401111120015141083050404330261401411234412400319100212120\n10014121114054", "output": "1000000000000000000000000010014121114054111111111111111111222222222222223333333333444444445668" }, { "input": "222122228\n2221", "output": "22212222" }, { "input": "10\n0", "output": "0" }, { "input": "11007000\n1000", "output": "1000001" }, { "input": "3323\n32", "output": "323" }, { "input": "1001016\n1001", "output": "100101" }, { "input": "50104\n10", "output": "1005" }, { "input": "2023\n20", "output": "202" }, { "input": "0001116\n1001", "output": "100101" }, { "input": "32334\n32", "output": "3233" }, { "input": "1103\n10", "output": "101" }, { "input": "023335\n23", "output": "23033" }, { "input": "111111111110\n1", "output": "1111111111" }, { "input": "501105\n110", "output": "11005" }, { "input": "1110006\n1001", "output": "100101" } ]
46
0
0
14,801
17
Hierarchy
[ "dfs and similar", "dsu", "greedy", "shortest paths" ]
B. Hierarchy
2
64
Nick's company employed *n* people. Now Nick needs to build a tree hierarchy of Β«supervisor-surbodinateΒ» relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are *m* applications written in the following form: Β«employee *a**i* is ready to become a supervisor of employee *b**i* at extra cost *c**i*Β». The qualification *q**j* of each employee is known, and for each application the following is true: *q**a**i*<=&gt;<=*q**b**i*. Would you help Nick calculate the minimum cost of such a hierarchy, or find out that it is impossible to build it.
The first input line contains integer *n* (1<=≀<=*n*<=≀<=1000) β€” amount of employees in the company. The following line contains *n* space-separated numbers *q**j* (0<=≀<=*q**j*<=≀<=106)β€” the employees' qualifications. The following line contains number *m* (0<=≀<=*m*<=≀<=10000) β€” amount of received applications. The following *m* lines contain the applications themselves, each of them in the form of three space-separated numbers: *a**i*, *b**i* and *c**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*, 0<=≀<=*c**i*<=≀<=106). Different applications can be similar, i.e. they can come from one and the same employee who offered to become a supervisor of the same person but at a different cost. For each application *q**a**i*<=&gt;<=*q**b**i*.
Output the only line β€” the minimum cost of building such a hierarchy, or -1 if it is impossible to build it.
[ "4\n7 2 3 1\n4\n1 2 5\n2 4 1\n3 4 1\n1 3 5\n", "3\n1 2 3\n2\n3 1 2\n3 1 3\n" ]
[ "11\n", "-1\n" ]
In the first sample one of the possible ways for building a hierarchy is to take applications with indexes 1, 2 and 4, which give 11 as the minimum total cost. In the second sample it is impossible to build the required hierarchy, so the answer is -1.
[ { "input": "4\n7 2 3 1\n4\n1 2 5\n2 4 1\n3 4 1\n1 3 5", "output": "11" }, { "input": "3\n1 2 3\n2\n3 1 2\n3 1 3", "output": "-1" }, { "input": "1\n2\n0", "output": "0" }, { "input": "2\n5 3\n4\n1 2 0\n1 2 5\n1 2 0\n1 2 7", "output": "0" }, { "input": "3\n9 4 5\n5\n3 2 4\n1 2 4\n3 2 8\n1 3 5\n3 2 5", "output": "9" }, { "input": "3\n2 5 9\n5\n3 1 7\n2 1 1\n2 1 6\n2 1 2\n3 1 5", "output": "-1" }, { "input": "3\n6 2 9\n5\n1 2 10\n3 1 4\n1 2 5\n1 2 2\n3 1 4", "output": "6" }, { "input": "4\n10 6 7 4\n5\n1 3 1\n3 4 1\n3 2 2\n1 2 6\n1 4 7", "output": "4" }, { "input": "4\n2 7 0 6\n8\n4 3 5\n2 3 7\n4 3 1\n2 1 9\n1 3 1\n1 3 3\n2 3 1\n1 3 2", "output": "-1" }, { "input": "5\n6 8 5 9 0\n8\n4 2 2\n2 3 10\n2 3 6\n4 5 4\n1 3 1\n4 3 4\n3 5 1\n2 3 8", "output": "-1" }, { "input": "5\n10 9 5 0 3\n9\n1 5 1\n1 4 7\n1 4 0\n1 4 6\n3 4 6\n2 3 1\n1 2 2\n1 2 9\n2 3 9", "output": "4" }, { "input": "5\n3 9 2 1 8\n9\n2 5 10\n1 3 8\n3 4 9\n5 4 2\n2 1 4\n5 1 4\n2 4 2\n1 4 7\n5 1 2", "output": "22" }, { "input": "5\n6 10 7 8 5\n10\n3 1 5\n2 4 1\n2 3 2\n4 5 9\n3 5 0\n4 1 9\n4 5 2\n1 5 8\n2 3 7\n1 5 1", "output": "8" }, { "input": "7\n10 0 4 7 3 8 6\n10\n3 2 4\n6 4 3\n6 5 1\n1 2 1\n6 7 6\n7 3 9\n1 6 3\n3 2 1\n6 4 4\n1 4 4", "output": "23" }, { "input": "6\n10 2 4 3 1 6\n10\n1 4 7\n1 6 9\n6 4 1\n4 2 5\n4 5 7\n6 3 10\n3 2 3\n3 4 3\n6 5 2\n1 3 2", "output": "17" }, { "input": "1\n10\n0", "output": "0" }, { "input": "2\n10 5\n0", "output": "-1" }, { "input": "2\n1000000 999999\n1\n1 2 1000000", "output": "1000000" }, { "input": "4\n3 2 2 1\n5\n1 2 1\n1 3 1\n1 4 500\n2 4 1\n3 4 2", "output": "3" } ]
154
0
0
14,806
675
Trains and Statistic
[ "data structures", "dp", "greedy" ]
null
null
Vasya commutes by train every day. There are *n* train stations in the city, and at the *i*-th station it's possible to buy only tickets to stations from *i*<=+<=1 to *a**i* inclusive. No tickets are sold at the last station. Let ρ*i*,<=*j* be the minimum number of tickets one needs to buy in order to get from stations *i* to station *j*. As Vasya is fond of different useless statistic he asks you to compute the sum of all values ρ*i*,<=*j* among all pairs 1<=≀<=*i*<=&lt;<=*j*<=≀<=*n*.
The first line of the input contains a single integer *n* (2<=≀<=*n*<=≀<=100<=000)Β β€” the number of stations. The second line contains *n*<=-<=1 integer *a**i* (*i*<=+<=1<=≀<=*a**i*<=≀<=*n*), the *i*-th of them means that at the *i*-th station one may buy tickets to each station from *i*<=+<=1 to *a**i* inclusive.
Print the sum of ρ*i*,<=*j* among all pairs of 1<=≀<=*i*<=&lt;<=*j*<=≀<=*n*.
[ "4\n4 4 4\n", "5\n2 3 5 5\n" ]
[ "6\n", "17\n" ]
In the first sample it's possible to get from any station to any other (with greater index) using only one ticket. The total number of pairs is 6, so the answer is also 6. Consider the second sample: - ρ<sub class="lower-index">1, 2</sub> = 1 - ρ<sub class="lower-index">1, 3</sub> = 2 - ρ<sub class="lower-index">1, 4</sub> = 3 - ρ<sub class="lower-index">1, 5</sub> = 3 - ρ<sub class="lower-index">2, 3</sub> = 1 - ρ<sub class="lower-index">2, 4</sub> = 2 - ρ<sub class="lower-index">2, 5</sub> = 2 - ρ<sub class="lower-index">3, 4</sub> = 1 - ρ<sub class="lower-index">3, 5</sub> = 1 - ρ<sub class="lower-index">4, 5</sub> = 1 Thus the answer equals 1 + 2 + 3 + 3 + 1 + 2 + 2 + 1 + 1 + 1 = 17.
[ { "input": "4\n4 4 4", "output": "6" }, { "input": "5\n2 3 5 5", "output": "17" }, { "input": "2\n2", "output": "1" }, { "input": "10\n2 10 8 7 8 8 10 9 10", "output": "63" }, { "input": "3\n3 3", "output": "3" }, { "input": "4\n3 3 4", "output": "8" }, { "input": "5\n4 4 4 5", "output": "13" }, { "input": "6\n3 3 6 6 6", "output": "21" }, { "input": "7\n7 3 4 6 6 7", "output": "35" }, { "input": "8\n3 7 7 8 8 7 8", "output": "37" }, { "input": "9\n2 9 7 6 9 7 8 9", "output": "52" } ]
108
23,142,400
0
14,811
638
Three-dimensional Turtle Super Computer
[ "brute force", "dfs and similar", "graphs" ]
null
null
A super computer has been built in the Turtle Academy of Sciences. The computer consists of *n*Β·*m*Β·*k* CPUs. The architecture was the paralellepiped of size *n*<=Γ—<=*m*<=Γ—<=*k*, split into 1<=Γ—<=1<=Γ—<=1 cells, each cell contains exactly one CPU. Thus, each CPU can be simultaneously identified as a group of three numbers from the layer number from 1 to *n*, the line number from 1 to *m* and the column number from 1 to *k*. In the process of the Super Computer's work the CPUs can send each other messages by the famous turtle scheme: CPU (*x*,<=*y*,<=*z*) can send messages to CPUs (*x*<=+<=1,<=*y*,<=*z*), (*x*,<=*y*<=+<=1,<=*z*) and (*x*,<=*y*,<=*z*<=+<=1) (of course, if they exist), there is no feedback, that is, CPUs (*x*<=+<=1,<=*y*,<=*z*), (*x*,<=*y*<=+<=1,<=*z*) and (*x*,<=*y*,<=*z*<=+<=1) cannot send messages to CPU (*x*,<=*y*,<=*z*). Over time some CPUs broke down and stopped working. Such CPUs cannot send messages, receive messages or serve as intermediates in transmitting messages. We will say that CPU (*a*,<=*b*,<=*c*) controls CPU (*d*,<=*e*,<=*f*) , if there is a chain of CPUs (*x**i*,<=*y**i*,<=*z**i*), such that (*x*1<==<=*a*,<=*y*1<==<=*b*,<=*z*1<==<=*c*), (*x**p*<==<=*d*,<=*y**p*<==<=*e*,<=*z**p*<==<=*f*) (here and below *p* is the length of the chain) and the CPU in the chain with number *i* (*i*<=&lt;<=*p*) can send messages to CPU *i*<=+<=1. Turtles are quite concerned about the denial-proofness of the system of communication between the remaining CPUs. For that they want to know the number of critical CPUs. A CPU (*x*,<=*y*,<=*z*) is critical, if turning it off will disrupt some control, that is, if there are two distinctive from (*x*,<=*y*,<=*z*) CPUs: (*a*,<=*b*,<=*c*) and (*d*,<=*e*,<=*f*), such that (*a*,<=*b*,<=*c*) controls (*d*,<=*e*,<=*f*) before (*x*,<=*y*,<=*z*) is turned off and stopped controlling it after the turning off.
The first line contains three integers *n*, *m* and *k* (1<=≀<=*n*,<=*m*,<=*k*<=≀<=100)Β β€” the dimensions of the Super Computer. Then *n* blocks follow, describing the current state of the processes. The blocks correspond to the layers of the Super Computer in the order from 1 to *n*. Each block consists of *m* lines, *k* characters in each β€” the description of a layer in the format of an *m*<=Γ—<=*k* table. Thus, the state of the CPU (*x*,<=*y*,<=*z*) is corresponded to the *z*-th character of the *y*-th line of the block number *x*. Character "1" corresponds to a working CPU and character "0" corresponds to a malfunctioning one. The blocks are separated by exactly one empty line.
Print a single integer β€” the number of critical CPUs, that is, such that turning only this CPU off will disrupt some control.
[ "2 2 3\n000\n000\n\n111\n111\n", "3 3 3\n111\n111\n111\n\n111\n111\n111\n\n111\n111\n111\n", "1 1 10\n0101010101\n" ]
[ "2\n", "19\n", "0\n" ]
In the first sample the whole first layer of CPUs is malfunctional. In the second layer when CPU (2, 1, 2) turns off, it disrupts the control by CPU (2, 1, 3) over CPU (2, 1, 1), and when CPU (2, 2, 2) is turned off, it disrupts the control over CPU (2, 2, 3) by CPU (2, 2, 1). In the second sample all processors except for the corner ones are critical. In the third sample there is not a single processor controlling another processor, so the answer is 0.
[ { "input": "2 2 3\n000\n000\n\n111\n111", "output": "2" }, { "input": "3 3 3\n111\n111\n111\n\n111\n111\n111\n\n111\n111\n111", "output": "19" }, { "input": "1 1 10\n0101010101", "output": "0" }, { "input": "1 1 1\n0", "output": "0" }, { "input": "1 1 1\n1", "output": "0" }, { "input": "3 1 1\n1\n\n1\n\n1", "output": "1" }, { "input": "3 1 1\n1\n\n0\n\n1", "output": "0" }, { "input": "1 3 1\n1\n1\n1", "output": "1" }, { "input": "1 3 1\n1\n0\n1", "output": "0" }, { "input": "1 1 3\n111", "output": "1" }, { "input": "1 1 3\n101", "output": "0" }, { "input": "1 1 3\n011", "output": "0" }, { "input": "1 1 3\n110", "output": "0" }, { "input": "1 1 1\n0", "output": "0" }, { "input": "1 1 1\n1", "output": "0" }, { "input": "1 1 1\n1", "output": "0" }, { "input": "1 1 100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "output": "0" }, { "input": "1 1 100\n0000011111011101001100111010100111000100010100010110111110110011000000111111011111001111000011111010", "output": "21" }, { "input": "1 1 100\n1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111", "output": "98" }, { "input": "1 100 1\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0", "output": "0" }, { "input": "1 100 1\n0\n0\n0\n0\n0\n1\n0\n0\n0\n0\n1\n0\n1\n0\n0\n0\n0\n0\n0\n0\n1\n0\n1\n0\n1\n1\n0\n1\n0\n1\n0\n0\n1\n1\n1\n0\n0\n1\n0\n1\n0\n0\n1\n1\n0\n0\n0\n0\n0\n1\n0\n0\n0\n1\n1\n1\n1\n0\n1\n0\n0\n1\n0\n1\n0\n0\n0\n0\n1\n0\n0\n1\n1\n1\n0\n0\n1\n1\n1\n0\n1\n0\n1\n0\n1\n0\n1\n0\n1\n1\n1\n1\n1\n1\n0\n1\n1\n1\n0\n0", "output": "10" }, { "input": "1 100 1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1", "output": "98" }, { "input": "100 1 1\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0", "output": "0" }, { "input": "100 1 1\n0\n\n1\n\n1\n\n1\n\n0\n\n0\n\n0\n\n1\n\n1\n\n0\n\n0\n\n1\n\n0\n\n1\n\n1\n\n1\n\n1\n\n0\n\n0\n\n1\n\n1\n\n1\n\n0\n\n0\n\n0\n\n0\n\n0\n\n1\n\n1\n\n0\n\n1\n\n1\n\n1\n\n0\n\n1\n\n0\n\n0\n\n1\n\n0\n\n1\n\n1\n\n0\n\n0\n\n0\n\n0\n\n1\n\n0\n\n1\n\n0\n\n0\n\n1\n\n1\n\n1\n\n0\n\n1\n\n1\n\n0\n\n1\n\n1\n\n1\n\n0\n\n0\n\n0\n\n1\n\n0\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n0\n\n0\n\n1\n\n0\n\n0\n\n0\n\n0\n\n0\n\n1\n\n0\n\n1\n\n1\n\n0\n\n0\n\n0\n\n0\n\n0\n\n1\n\n1\n\n1\n\n1\n\n1\n\n0\n\n1\n\n1\n\n1\n\n1\n\n1\n\n0", "output": "17" }, { "input": "100 1 1\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1\n\n1", "output": "98" }, { "input": "6 8 3\n011\n001\n000\n100\n111\n110\n100\n100\n\n000\n100\n011\n001\n011\n000\n100\n111\n\n110\n111\n011\n110\n101\n001\n110\n000\n\n100\n000\n110\n001\n110\n010\n110\n011\n\n101\n111\n010\n110\n101\n111\n011\n110\n\n100\n111\n111\n011\n101\n110\n110\n110", "output": "46" } ]
46
4,812,800
0
14,812
606
Testing Robots
[ "implementation" ]
null
null
The Cybernetics Failures (CF) organisation made a prototype of a bomb technician robot. To find the possible problems it was decided to carry out a series of tests. At the beginning of each test the robot prototype will be placed in cell (*x*0,<=*y*0) of a rectangular squared field of size *x*<=Γ—<=*y*, after that a mine will be installed into one of the squares of the field. It is supposed to conduct exactly *x*Β·*y* tests, each time a mine is installed into a square that has never been used before. The starting cell of the robot always remains the same. After placing the objects on the field the robot will have to run a sequence of commands given by string *s*, consisting only of characters 'L', 'R', 'U', 'D'. These commands tell the robot to move one square to the left, to the right, up or down, or stay idle if moving in the given direction is impossible. As soon as the robot fulfills all the sequence of commands, it will blow up due to a bug in the code. But if at some moment of time the robot is at the same square with the mine, it will also blow up, but not due to a bug in the code. Moving to the left decreases coordinate *y*, and moving to the right increases it. Similarly, moving up decreases the *x* coordinate, and moving down increases it. The tests can go on for very long, so your task is to predict their results. For each *k* from 0 to *length*(*s*) your task is to find in how many tests the robot will run exactly *k* commands before it blows up.
The first line of the input contains four integers *x*, *y*, *x*0, *y*0 (1<=≀<=*x*,<=*y*<=≀<=500,<=1<=≀<=*x*0<=≀<=*x*,<=1<=≀<=*y*0<=≀<=*y*)Β β€” the sizes of the field and the starting coordinates of the robot. The coordinate axis *X* is directed downwards and axis *Y* is directed to the right. The second line contains a sequence of commands *s*, which should be fulfilled by the robot. It has length from 1 to 100<=000 characters and only consists of characters 'L', 'R', 'U', 'D'.
Print the sequence consisting of (*length*(*s*)<=+<=1) numbers. On the *k*-th position, starting with zero, print the number of tests where the robot will run exactly *k* commands before it blows up.
[ "3 4 2 2\nUURDRDRL\n", "2 2 2 2\nULD\n" ]
[ "1 1 0 1 1 1 1 0 6\n", "1 1 1 1\n" ]
In the first sample, if we exclude the probable impact of the mines, the robot's route will look like that: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/16bfda1e4f41cc00665c31f0a1d754d68cd9b4ab.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
[ { "input": "3 4 2 2\nUURDRDRL", "output": "1 1 0 1 1 1 1 0 6" }, { "input": "2 2 2 2\nULD", "output": "1 1 1 1" }, { "input": "1 1 1 1\nURDLUURRDDLLURDL", "output": "1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0" }, { "input": "15 17 8 9\nURRDLUULLDD", "output": "1 1 1 1 1 1 0 1 1 1 1 245" }, { "input": "15 17 8 9\nURRDLUULLDDDRRUR", "output": "1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 241" }, { "input": "15 17 8 9\nURRDLUULLDDDRRURR", "output": "1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 0 241" }, { "input": "1 2 1 1\nR", "output": "1 1" }, { "input": "2 1 1 1\nD", "output": "1 1" }, { "input": "1 2 1 2\nLR", "output": "1 1 0" }, { "input": "2 1 2 1\nUD", "output": "1 1 0" }, { "input": "4 4 2 2\nDRUL", "output": "1 1 1 1 12" }, { "input": "4 4 3 3\nLUDRUL", "output": "1 1 1 0 0 1 12" }, { "input": "15 17 8 9\nURRDLU", "output": "1 1 1 1 1 1 249" }, { "input": "15 17 8 9\nURRDLUULLDDR", "output": "1 1 1 1 1 1 0 1 1 1 1 1 244" }, { "input": "15 17 8 9\nURRDLUULLDDRR", "output": "1 1 1 1 1 1 0 1 1 1 1 1 0 244" }, { "input": "15 17 8 9\nURRDLUULLDDRRR", "output": "1 1 1 1 1 1 0 1 1 1 1 1 0 0 244" }, { "input": "15 17 8 9\nURRDLUULLDDRRRR", "output": "1 1 1 1 1 1 0 1 1 1 1 1 0 0 0 244" }, { "input": "15 17 8 9\nURRDLUULLDDRRRRU", "output": "1 1 1 1 1 1 0 1 1 1 1 1 0 0 0 1 243" } ]
109
0
0
14,883
15
Industrial Nim
[ "games" ]
C. Industrial Nim
2
64
There are *n* stone quarries in Petrograd. Each quarry owns *m**i* dumpers (1<=≀<=*i*<=≀<=*n*). It is known that the first dumper of the *i*-th quarry has *x**i* stones in it, the second dumper has *x**i*<=+<=1 stones in it, the third has *x**i*<=+<=2, and the *m**i*-th dumper (the last for the *i*-th quarry) has *x**i*<=+<=*m**i*<=-<=1 stones in it. Two oligarchs play a well-known game Nim. Players take turns removing stones from dumpers. On each turn, a player can select any dumper and remove any non-zero amount of stones from it. The player who cannot take a stone loses. Your task is to find out which oligarch will win, provided that both of them play optimally. The oligarchs asked you not to reveal their names. So, let's call the one who takes the first stone Β«tolikΒ» and the other one Β«bolikΒ».
The first line of the input contains one integer number *n* (1<=≀<=*n*<=≀<=105) β€” the amount of quarries. Then there follow *n* lines, each of them contains two space-separated integers *x**i* and *m**i* (1<=≀<=*x**i*,<=*m**i*<=≀<=1016) β€” the amount of stones in the first dumper of the *i*-th quarry and the number of dumpers at the *i*-th quarry.
Output Β«tolikΒ» if the oligarch who takes a stone first wins, and Β«bolikΒ» otherwise.
[ "2\n2 1\n3 2\n", "4\n1 1\n1 1\n1 1\n1 1\n" ]
[ "tolik\n", "bolik\n" ]
none
[ { "input": "2\n2 1\n3 2", "output": "tolik" }, { "input": "4\n1 1\n1 1\n1 1\n1 1", "output": "bolik" }, { "input": "10\n2 3\n1 4\n5 8\n4 10\n10 8\n7 2\n1 2\n1 7\n4 10\n5 3", "output": "tolik" }, { "input": "20\n8 6\n6 3\n2 9\n7 8\n9 1\n2 4\n3 6\n6 3\n5 6\n5 3\n6 5\n2 10\n2 9\n6 3\n10 6\n10 10\n10 7\n3 9\n16 1\n1 3", "output": "bolik" }, { "input": "30\n53 12\n13 98\n21 60\n76 58\n39 5\n62 58\n73 80\n13 75\n37 45\n44 86\n1 85\n13 33\n17 50\n12 26\n97 48\n52 40\n2 71\n95 79\n38 76\n24 54\n91 39\n97 92\n94 80\n50 61\n33 56\n22 91\n39 94\n31 56\n28 16\n20 44", "output": "tolik" }, { "input": "1\n3737203222172202 1", "output": "tolik" }, { "input": "1\n3737203222172202 1", "output": "tolik" } ]
216
6,656,000
0
14,885
986
Oppa Funcan Style Remastered
[ "graphs", "math", "number theory", "shortest paths" ]
null
null
Surely you have seen insane videos by South Korean rapper PSY, such as "Gangnam Style", "Gentleman" and "Daddy". You might also hear that PSY has been recording video "Oppa Funcan Style" two years ago (unfortunately we couldn't find it on the internet). We will remind you what this hit looked like (you can find original description [here](http://acm.timus.ru/problem.aspx?space=1&amp;num=2107&amp;locale=en)): On the ground there are $n$ platforms, which are numbered with integers from $1$ to $n$, on $i$-th platform there is a dancer with number $i$. Further, every second all the dancers standing on the platform with number $i$ jump to the platform with the number $f(i)$. The moving rule $f$ is selected in advance and is not changed throughout the clip. The duration of the clip was $k$ seconds and the rule $f$ was chosen in such a way that after $k$ seconds all dancers were in their initial positions (i.e. the $i$-th dancer stood on the platform with the number $i$). That allowed to loop the clip and collect even more likes. PSY knows that enhanced versions of old artworks become more and more popular every day. So he decided to release a remastered-version of his video. In his case "enhanced version" means even more insanity, so the number of platforms can be up to $10^{18}$! But the video director said that if some dancer stays on the same platform all the time, then the viewer will get bored and will turn off the video immediately. Therefore, for all $x$ from $1$ to $n$ $f(x) \neq x$ must hold. Big part of classic video's success was in that looping, so in the remastered version all dancers should return to their initial positions in the end of the clip as well. PSY hasn't decided on the exact number of platforms and video duration yet, so he asks you to check if there is a good rule $f$ for different options.
In the first line of input there is one integer $t$ ($1 \le t \le 10^{4}$)Β β€” the number of options for $n$ and $k$ to check. In the next $t$ lines the options are given: each option is described with two integers $n$ and $k$ ($1 \le n \le 10^{18}$, $1 \le k \le 10^{15}$)Β β€” the number of dancers and the duration in seconds. It is guaranteed that the number of different values of $k$ in one test is not greater than $50$.
Print $t$ lines. If the $i$-th option of the video is feasible, print "YES" (without quotes) in $i$-th line, otherwise print "NO" (without quotes).
[ "3\n7 7\n3 8\n5 6\n" ]
[ "YES\nNO\nYES\n" ]
none
[]
93
0
0
14,887
59
Shortest Path
[ "graphs", "shortest paths" ]
E. Shortest Path
3
256
In Ancient Berland there were *n* cities and *m* two-way roads of equal length. The cities are numbered with integers from 1 to *n* inclusively. According to an ancient superstition, if a traveller visits three cities *a**i*, *b**i*, *c**i* in row, without visiting other cities between them, a great disaster awaits him. Overall there are *k* such city triplets. Each triplet is ordered, which means that, for example, you are allowed to visit the cities in the following order: *a**i*, *c**i*, *b**i*. Vasya wants to get from the city 1 to the city *n* and not fulfil the superstition. Find out which minimal number of roads he should take. Also you are required to find one of his possible path routes.
The first line contains three integers *n*, *m*, *k* (2<=≀<=*n*<=≀<=3000,<=1<=≀<=*m*<=≀<=20000,<=0<=≀<=*k*<=≀<=105) which are the number of cities, the number of roads and the number of the forbidden triplets correspondingly. Then follow *m* lines each containing two integers *x**i*, *y**i* (1<=≀<=*x**i*,<=*y**i*<=≀<=*n*) which are the road descriptions. The road is described by the numbers of the cities it joins. No road joins a city with itself, there cannot be more than one road between a pair of cities. Then follow *k* lines each containing three integers *a**i*, *b**i*, *c**i* (1<=≀<=*a**i*,<=*b**i*,<=*c**i*<=≀<=*n*) which are the forbidden triplets. Each ordered triplet is listed mo more than one time. All three cities in each triplet are distinct. City *n* can be unreachable from city 1 by roads.
If there are no path from 1 to *n* print -1. Otherwise on the first line print the number of roads *d* along the shortest path from the city 1 to the city *n*. On the second line print *d*<=+<=1 numbers β€” any of the possible shortest paths for Vasya. The path should start in the city 1 and end in the city *n*.
[ "4 4 1\n1 2\n2 3\n3 4\n1 3\n1 4 3\n", "3 1 0\n1 2\n", "4 4 2\n1 2\n2 3\n3 4\n1 3\n1 2 3\n1 3 4\n" ]
[ "2\n1 3 4\n", "-1\n", "4\n1 3 2 3 4\n" ]
none
[ { "input": "4 4 1\n1 2\n2 3\n3 4\n1 3\n1 4 3", "output": "2\n1 3 4" }, { "input": "3 1 0\n1 2", "output": "-1" }, { "input": "4 4 2\n1 2\n2 3\n3 4\n1 3\n1 2 3\n1 3 4", "output": "4\n1 3 2 3 4" }, { "input": "4 4 1\n1 2\n2 3\n3 4\n1 3\n1 2 3", "output": "2\n1 3 4" }, { "input": "2 1 0\n1 2", "output": "1\n1 2" }, { "input": "4 4 1\n1 2\n2 3\n3 4\n1 3\n1 3 4", "output": "3\n1 2 3 4" }, { "input": "3 2 0\n1 2\n3 2", "output": "2\n1 2 3" }, { "input": "3 2 1\n1 2\n3 2\n1 2 3", "output": "-1" }, { "input": "4 4 4\n1 2\n2 3\n3 4\n1 3\n1 2 3\n1 3 4\n1 2 4\n1 3 2", "output": "-1" } ]
3,000
21,401,600
0
14,916
894
Ralph And His Magic Field
[ "combinatorics", "constructive algorithms", "math", "number theory" ]
null
null
Ralph has a magic field which is divided into *n*<=Γ—<=*m* blocks. That is to say, there are *n* rows and *m* columns on the field. Ralph can put an integer in each block. However, the magic field doesn't always work properly. It works only if the product of integers in each row and each column equals to *k*, where *k* is either 1 or -1. Now Ralph wants you to figure out the number of ways to put numbers in each block in such a way that the magic field works properly. Two ways are considered different if and only if there exists at least one block where the numbers in the first way and in the second way are different. You are asked to output the answer modulo 1000000007<==<=109<=+<=7. Note that there is no range of the numbers to put in the blocks, but we can prove that the answer is not infinity.
The only line contains three integers *n*, *m* and *k* (1<=≀<=*n*,<=*m*<=≀<=1018, *k* is either 1 or -1).
Print a single number denoting the answer modulo 1000000007.
[ "1 1 -1\n", "1 3 1\n", "3 3 -1\n" ]
[ "1\n", "1\n", "16\n" ]
In the first example the only way is to put -1 into the only block. In the second example the only way is to put 1 into every block.
[ { "input": "1 1 -1", "output": "1" }, { "input": "1 3 1", "output": "1" }, { "input": "3 3 -1", "output": "16" }, { "input": "2 7 1", "output": "64" }, { "input": "1 1 1", "output": "1" }, { "input": "2 4 -1", "output": "8" }, { "input": "173 69 -1", "output": "814271739" }, { "input": "110 142 1", "output": "537040244" }, { "input": "162 162 -1", "output": "394042552" }, { "input": "49 153 -1", "output": "412796600" }, { "input": "94 182 1", "output": "33590706" }, { "input": "106666666 233333333 1", "output": "121241754" }, { "input": "2 2 1", "output": "2" }, { "input": "146 34 -1", "output": "742752757" }, { "input": "94 86 -1", "output": "476913727" }, { "input": "2529756051797760 2682355969139391 -1", "output": "0" }, { "input": "3126690179932000 2474382898739836 -1", "output": "917305624" }, { "input": "3551499873841921 2512677762780671 -1", "output": "350058339" }, { "input": "3613456196418270 2872267429531501 1", "output": "223552863" }, { "input": "2886684369091916 3509787933422130 1", "output": "341476979" }, { "input": "3536041043537343 2416093514489183 1", "output": "394974516" }, { "input": "2273134852621270 2798005122439669 1", "output": "901406364" }, { "input": "2870150496178092 3171485931753811 -1", "output": "0" }, { "input": "999999999999999999 1000000000000000000 1", "output": "102810659" }, { "input": "987654321987654321 666666666666666666 1", "output": "279028602" }, { "input": "1 2 -1", "output": "0" }, { "input": "2 1 -1", "output": "0" }, { "input": "1000000000000000000 1 1", "output": "1" }, { "input": "1000000006 100000000000000000 1", "output": "123624987" } ]
62
0
0
14,937
527
Error Correct System
[ "greedy" ]
null
null
Ford Prefect got a job as a web developer for a small company that makes towels. His current work task is to create a search engine for the website of the company. During the development process, he needs to write a subroutine for comparing strings *S* and *T* of equal length to be "similar". After a brief search on the Internet, he learned about the Hamming distance between two strings *S* and *T* of the same length, which is defined as the number of positions in which *S* and *T* have different characters. For example, the Hamming distance between words "permanent" and "pergament" is two, as these words differ in the fourth and sixth letters. Moreover, as he was searching for information, he also noticed that modern search engines have powerful mechanisms to correct errors in the request to improve the quality of search. Ford doesn't know much about human beings, so he assumed that the most common mistake in a request is swapping two arbitrary letters of the string (not necessarily adjacent). Now he wants to write a function that determines which two letters should be swapped in string *S*, so that the Hamming distance between a new string *S* and string *T* would be as small as possible, or otherwise, determine that such a replacement cannot reduce the distance between the strings. Help him do this!
The first line contains integer *n* (1<=≀<=*n*<=≀<=200<=000) β€” the length of strings *S* and *T*. The second line contains string *S*. The third line contains string *T*. Each of the lines only contains lowercase Latin letters.
In the first line, print number *x* β€” the minimum possible Hamming distance between strings *S* and *T* if you swap at most one pair of letters in *S*. In the second line, either print the indexes *i* and *j* (1<=≀<=*i*,<=*j*<=≀<=*n*, *i*<=β‰ <=*j*), if reaching the minimum possible distance is possible by swapping letters on positions *i* and *j*, or print "-1 -1", if it is not necessary to swap characters. If there are multiple possible answers, print any of them.
[ "9\npergament\npermanent\n", "6\nwookie\ncookie\n", "4\npetr\negor\n", "6\ndouble\nbundle\n" ]
[ "1\n4 6\n", "1\n-1 -1\n", "2\n1 2\n", "2\n4 1\n" ]
In the second test it is acceptable to print *i* = 2, *j* = 3.
[ { "input": "9\npergament\npermanent", "output": "1\n4 6" }, { "input": "6\nwookie\ncookie", "output": "1\n-1 -1" }, { "input": "4\npetr\negor", "output": "2\n1 2" }, { "input": "6\ndouble\nbundle", "output": "2\n4 1" }, { "input": "1\na\na", "output": "0\n-1 -1" }, { "input": "1\na\nb", "output": "1\n-1 -1" }, { "input": "2\naa\naa", "output": "0\n-1 -1" }, { "input": "2\nzz\nzz", "output": "0\n-1 -1" }, { "input": "2\nzx\nzz", "output": "1\n-1 -1" }, { "input": "2\nzz\nzx", "output": "1\n-1 -1" }, { "input": "2\nxy\nzz", "output": "2\n-1 -1" }, { "input": "2\nzz\nxy", "output": "2\n-1 -1" }, { "input": "2\nzx\nxz", "output": "0\n2 1" }, { "input": "2\nab\nbc", "output": "1\n1 2" }, { "input": "2\nab\ncb", "output": "1\n-1 -1" }, { "input": "2\nxx\nyy", "output": "2\n-1 -1" }, { "input": "10\ncdcddbacdb\naababacabc", "output": "8\n7 8" }, { "input": "2\nab\ncd", "output": "2\n-1 -1" }, { "input": "2\naa\nab", "output": "1\n-1 -1" }, { "input": "3\nabc\nbca", "output": "2\n1 2" }, { "input": "3\nxyx\nyxy", "output": "1\n3 2" }, { "input": "4\nabba\nbbaa", "output": "0\n1 3" }, { "input": "4\nabba\nabca", "output": "1\n-1 -1" }, { "input": "4\nabba\ncaba", "output": "1\n2 1" }, { "input": "4\nyydd\ndxyz", "output": "2\n3 1" } ]
499
1,126,400
3
14,961
567
One-Dimensional Battle Ships
[ "binary search", "data structures", "greedy", "sortings" ]
null
null
Alice and Bob love playing one-dimensional battle ships. They play on the field in the form of a line consisting of *n* square cells (that is, on a 1<=Γ—<=*n* table). At the beginning of the game Alice puts *k* ships on the field without telling their positions to Bob. Each ship looks as a 1<=Γ—<=*a* rectangle (that is, it occupies a sequence of *a* consecutive squares of the field). The ships cannot intersect and even touch each other. After that Bob makes a sequence of "shots". He names cells of the field and Alice either says that the cell is empty ("miss"), or that the cell belongs to some ship ("hit"). But here's the problem! Alice like to cheat. May be that is why she responds to each Bob's move with a "miss". Help Bob catch Alice cheating β€” find Bob's first move, such that after it you can be sure that Alice cheated.
The first line of the input contains three integers: *n*, *k* and *a* (1<=≀<=*n*,<=*k*,<=*a*<=≀<=2Β·105) β€” the size of the field, the number of the ships and the size of each ship. It is guaranteed that the *n*, *k* and *a* are such that you can put *k* ships of size *a* on the field, so that no two ships intersect or touch each other. The second line contains integer *m* (1<=≀<=*m*<=≀<=*n*) β€” the number of Bob's moves. The third line contains *m* distinct integers *x*1,<=*x*2,<=...,<=*x**m*, where *x**i* is the number of the cell where Bob made the *i*-th shot. The cells are numbered from left to right from 1 to *n*.
Print a single integer β€” the number of such Bob's first move, after which you can be sure that Alice lied. Bob's moves are numbered from 1 to *m* in the order the were made. If the sought move doesn't exist, then print "-1".
[ "11 3 3\n5\n4 8 6 1 11\n", "5 1 3\n2\n1 5\n", "5 1 3\n1\n3\n" ]
[ "3\n", "-1\n", "1\n" ]
none
[ { "input": "11 3 3\n5\n4 8 6 1 11", "output": "3" }, { "input": "5 1 3\n2\n1 5", "output": "-1" }, { "input": "5 1 3\n1\n3", "output": "1" }, { "input": "1 1 1\n1\n1", "output": "1" }, { "input": "5000 1660 2\n20\n1 100 18 102 300 81 19 25 44 88 1337 4999 1054 1203 91 16 164 914 1419 1487", "output": "18" }, { "input": "5000 1000 2\n3\n1000 2000 3000", "output": "-1" }, { "input": "10 2 4\n2\n5 6", "output": "-1" }, { "input": "10 2 4\n3\n5 6 1", "output": "3" }, { "input": "4 2 1\n2\n1 2", "output": "2" }, { "input": "4 2 1\n2\n1 3", "output": "-1" }, { "input": "50 7 3\n20\n24 18 34 32 44 2 5 40 17 48 31 45 8 6 15 27 26 1 20 10", "output": "13" }, { "input": "50 7 3\n50\n17 47 1 12 21 25 6 5 49 27 34 8 16 38 11 44 48 9 2 20 3 22 33 23 36 41 15 35 31 30 50 7 45 42 37 29 14 26 24 46 19 4 10 28 18 43 32 39 40 13", "output": "19" }, { "input": "50 1 1\n50\n1 13 21 37 30 48 23 19 6 49 36 14 9 24 44 10 41 28 20 2 15 11 45 3 25 33 50 38 35 47 31 4 12 46 32 8 42 26 5 7 27 16 29 43 39 22 17 34 40 18", "output": "50" }, { "input": "200000 100000 1\n1\n31618", "output": "-1" }, { "input": "200000 1 200000\n1\n1", "output": "1" }, { "input": "200000 1 200000\n1\n200000", "output": "1" }, { "input": "200000 1 199999\n2\n1 200000", "output": "2" }, { "input": "200000 1 199999\n2\n200000 1", "output": "2" }, { "input": "200000 1 199999\n2\n2 200000", "output": "1" } ]
92
0
0
15,025
621
Wet Shark and Flowers
[ "combinatorics", "math", "number theory", "probabilities" ]
null
null
There are *n* sharks who grow flowers for Wet Shark. They are all sitting around the table, such that sharks *i* and *i*<=+<=1 are neighbours for all *i* from 1 to *n*<=-<=1. Sharks *n* and 1 are neighbours too. Each shark will grow some number of flowers *s**i*. For *i*-th shark value *s**i* is random integer equiprobably chosen in range from *l**i* to *r**i*. Wet Shark has it's favourite prime number *p*, and he really likes it! If for any pair of neighbouring sharks *i* and *j* the product *s**i*Β·*s**j* is divisible by *p*, then Wet Shark becomes happy and gives 1000 dollars to each of these sharks. At the end of the day sharks sum all the money Wet Shark granted to them. Find the expectation of this value.
The first line of the input contains two space-separated integers *n* and *p* (3<=≀<=*n*<=≀<=100<=000,<=2<=≀<=*p*<=≀<=109)Β β€” the number of sharks and Wet Shark's favourite prime number. It is guaranteed that *p* is prime. The *i*-th of the following *n* lines contains information about *i*-th sharkΒ β€” two space-separated integers *l**i* and *r**i* (1<=≀<=*l**i*<=≀<=*r**i*<=≀<=109), the range of flowers shark *i* can produce. Remember that *s**i* is chosen equiprobably among all integers from *l**i* to *r**i*, inclusive.
Print a single real number β€” the expected number of dollars that the sharks receive in total. You answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6. Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program will consider your answer correct, if .
[ "3 2\n1 2\n420 421\n420420 420421\n", "3 5\n1 4\n2 3\n11 14\n" ]
[ "4500.0\n", "0.0\n" ]
A prime number is a positive integer number that is divisible only by 1 and itself. 1 is not considered to be prime. Consider the first sample. First shark grows some number of flowers from 1 to 2, second sharks grows from 420 to 421 flowers and third from 420420 to 420421. There are eight cases for the quantities of flowers (*s*<sub class="lower-index">0</sub>, *s*<sub class="lower-index">1</sub>, *s*<sub class="lower-index">2</sub>) each shark grows: 1. (1, 420, 420420): note that *s*<sub class="lower-index">0</sub>Β·*s*<sub class="lower-index">1</sub> = 420, *s*<sub class="lower-index">1</sub>Β·*s*<sub class="lower-index">2</sub> = 176576400, and *s*<sub class="lower-index">2</sub>Β·*s*<sub class="lower-index">0</sub> = 420420. For each pair, 1000 dollars will be awarded to each shark. Therefore, each shark will be awarded 2000 dollars, for a total of 6000 dollars.1. (1, 420, 420421): now, the product *s*<sub class="lower-index">2</sub>Β·*s*<sub class="lower-index">0</sub> is not divisible by 2. Therefore, sharks *s*<sub class="lower-index">0</sub> and *s*<sub class="lower-index">2</sub> will receive 1000 dollars, while shark *s*<sub class="lower-index">1</sub> will receive 2000. The total is 4000.1. (1, 421, 420420): total is 4000 1. (1, 421, 420421): total is 0. 1. (2, 420, 420420): total is 6000. 1. (2, 420, 420421): total is 6000. 1. (2, 421, 420420): total is 6000. 1. (2, 421, 420421): total is 4000. The expected value is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/dfe520d00a8615f7c270ccbccbebe182cc7db883.png" style="max-width: 100.0%;max-height: 100.0%;"/>. In the second sample, no combination of quantities will garner the sharks any money.
[ { "input": "3 2\n1 2\n420 421\n420420 420421", "output": "4500.0" }, { "input": "3 5\n1 4\n2 3\n11 14", "output": "0.0" }, { "input": "3 3\n3 3\n2 4\n1 1", "output": "4666.666666666667" }, { "input": "5 5\n5 204\n420 469\n417 480\n442 443\n44 46", "output": "3451.25" }, { "input": "3 2\n2 2\n3 3\n4 4", "output": "6000.0" }, { "input": "6 7\n8 13\n14 14\n8 13\n14 14\n8 13\n14 14", "output": "12000.0" }, { "input": "3 7\n7 14\n700000000 700000007\n420 4200", "output": "2304.2515207617034" }, { "input": "5 999999937\n999999935 999999936\n999999937 999999938\n999999939 999999940\n999999941 999999942\n999999943 999999944", "output": "2000.0" }, { "input": "5 999999937\n1 999999936\n1 999999936\n1 999999936\n1 999999936\n1 999999936", "output": "0.0" }, { "input": "20 999999937\n999999936 999999937\n999999937 999999938\n999999936 999999937\n999999937 999999938\n999999936 999999937\n999999937 999999938\n999999936 999999937\n999999937 999999938\n999999936 999999937\n999999937 999999938\n999999936 999999937\n999999937 999999938\n999999936 999999937\n999999937 999999938\n999999936 999999937\n999999937 999999938\n999999936 999999937\n999999937 999999938\n999999936 999999937\n999999937 999999938", "output": "30000.0" }, { "input": "9 41\n40 42\n42 44\n44 46\n82 84\n82 83\n80 83\n40 83\n40 82\n42 82", "output": "5503.274377352654" }, { "input": "3 2\n1 1\n1 2\n1 1", "output": "2000.0" }, { "input": "12 3\n697806 966852\n802746 974920\n579567 821770\n628655 642480\n649359 905832\n87506 178848\n605628 924780\n843338 925533\n953514 978612\n375312 997707\n367620 509906\n277106 866177", "output": "13333.518289809368" }, { "input": "5 3\n67050 461313\n927808 989615\n169239 201720\n595515 756354\n392844 781910", "output": "5555.597086312073" }, { "input": "6 7\n984774 984865\n720391 916269\n381290 388205\n628383 840455\n747138 853964\n759705 959629", "output": "3215.6233297395006" }, { "input": "3 5\n99535 124440\n24114 662840\n529335 875935", "output": "2160.11317825774" }, { "input": "4 3\n561495 819666\n718673 973130\n830124 854655\n430685 963699", "output": "4444.521972611004" }, { "input": "10 3\n311664 694971\n364840 366487\n560148 821101\n896470 923613\n770019 828958\n595743 827536\n341418 988218\n207847 366132\n517968 587855\n168695 878142", "output": "11110.602699850484" }, { "input": "11 3\n66999 737907\n499872 598806\n560583 823299\n579017 838419\n214308 914576\n31820 579035\n373821 695652\n438988 889317\n181332 513682\n740575 769488\n597348 980891", "output": "12222.259608784536" }, { "input": "12 3\n158757 341790\n130709 571435\n571161 926255\n851779 952236\n914910 941369\n774359 860799\n224067 618483\n411639 902888\n264423 830336\n33133 608526\n951696 976379\n923880 968563", "output": "13333.377729413933" }, { "input": "9 2\n717582 964152\n268030 456147\n400022 466269\n132600 698200\n658890 807357\n196658 849497\n257020 380298\n267729 284534\n311978 917744", "output": "13500.015586135814" }, { "input": "10 7\n978831 984305\n843967 844227\n454356 748444\n219513 623868\n472997 698189\n542337 813387\n867615 918554\n413076 997267\n79310 138855\n195703 296681", "output": "5303.027968302269" } ]
327
7,475,200
3
15,086
831
Jury Marks
[ "brute force", "constructive algorithms" ]
null
null
Polycarp watched TV-show where *k* jury members one by one rated a participant by adding him a certain number of points (may be negative, i.Β e. points were subtracted). Initially the participant had some score, and each the marks were one by one added to his score. It is known that the *i*-th jury member gave *a**i* points. Polycarp does not remember how many points the participant had before this *k* marks were given, but he remembers that among the scores announced after each of the *k* judges rated the participant there were *n* (*n*<=≀<=*k*) values *b*1,<=*b*2,<=...,<=*b**n* (it is guaranteed that all values *b**j* are distinct). It is possible that Polycarp remembers not all of the scores announced, i.Β e. *n*<=&lt;<=*k*. Note that the initial score wasn't announced. Your task is to determine the number of options for the score the participant could have before the judges rated the participant.
The first line contains two integers *k* and *n* (1<=≀<=*n*<=≀<=*k*<=≀<=2<=000) β€” the number of jury members and the number of scores Polycarp remembers. The second line contains *k* integers *a*1,<=*a*2,<=...,<=*a**k* (<=-<=2<=000<=≀<=*a**i*<=≀<=2<=000) β€” jury's marks in chronological order. The third line contains *n* distinct integers *b*1,<=*b*2,<=...,<=*b**n* (<=-<=4<=000<=000<=≀<=*b**j*<=≀<=4<=000<=000) β€” the values of points Polycarp remembers. Note that these values are not necessarily given in chronological order.
Print the number of options for the score the participant could have before the judges rated the participant. If Polycarp messes something up and there is no options, print "0" (without quotes).
[ "4 1\n-5 5 0 20\n10\n", "2 2\n-2000 -2000\n3998000 4000000\n" ]
[ "3\n", "1\n" ]
The answer for the first example is 3 because initially the participant could have  - 10, 10 or 15 points. In the second example there is only one correct initial score equaling to 4 002 000.
[ { "input": "4 1\n-5 5 0 20\n10", "output": "3" }, { "input": "2 2\n-2000 -2000\n3998000 4000000", "output": "1" }, { "input": "1 1\n-577\n1273042", "output": "1" }, { "input": "2 1\n614 -1943\n3874445", "output": "2" }, { "input": "3 1\n1416 -1483 1844\n3261895", "output": "3" }, { "input": "5 1\n1035 1861 1388 -622 1252\n2640169", "output": "5" }, { "input": "10 10\n-25 746 298 1602 -1453 -541 -442 1174 976 -1857\n-548062 -548253 -546800 -548943 -548402 -548794 -549236 -548700 -549446 -547086", "output": "1" }, { "input": "20 20\n-1012 625 39 -1747 -1626 898 -1261 180 -876 -1417 -1853 -1510 -1499 -561 -1824 442 -895 13 1857 1860\n-1269013 -1270956 -1264151 -1266004 -1268121 -1258341 -1269574 -1271851 -1258302 -1271838 -1260049 -1258966 -1271398 -1267514 -1269981 -1262038 -1261675 -1262734 -1260777 -1261858", "output": "1" }, { "input": "1 1\n1\n-4000000", "output": "1" } ]
170
138,342,400
3
15,112
762
Tree nesting
[ "combinatorics", "graphs", "trees" ]
null
null
You are given two trees (connected undirected acyclic graphs) *S* and *T*. Count the number of subtrees (connected subgraphs) of *S* that are isomorphic to tree *T*. Since this number can get quite large, output it modulo 109<=+<=7. Two subtrees of tree *S* are considered different, if there exists a vertex in *S* that belongs to exactly one of them. Tree *G* is called isomorphic to tree *H* if there exists a bijection *f* from the set of vertices of *G* to the set of vertices of *H* that has the following property: if there is an edge between vertices *A* and *B* in tree *G*, then there must be an edge between vertices *f*(*A*) and *f*(*B*) in tree *H*. And vice versaΒ β€” if there is an edge between vertices *A* and *B* in tree *H*, there must be an edge between *f*<=-<=1(*A*) and *f*<=-<=1(*B*) in tree *G*.
The first line contains a single integer |*S*| (1<=≀<=|*S*|<=≀<=1000) β€” the number of vertices of tree *S*. Next |*S*|<=-<=1 lines contain two integers *u**i* and *v**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=|*S*|) and describe edges of tree *S*. The next line contains a single integer |*T*| (1<=≀<=|*T*|<=≀<=12) β€” the number of vertices of tree *T*. Next |*T*|<=-<=1 lines contain two integers *x**i* and *y**i* (1<=≀<=*x**i*,<=*y**i*<=≀<=|*T*|) and describe edges of tree *T*.
On the first line output a single integer β€” the answer to the given task modulo 109<=+<=7.
[ "5\n1 2\n2 3\n3 4\n4 5\n3\n1 2\n2 3\n", "3\n2 3\n3 1\n3\n1 2\n1 3\n", "7\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n4\n4 1\n4 2\n4 3\n", "5\n1 2\n2 3\n3 4\n4 5\n4\n4 1\n4 2\n4 3\n" ]
[ "3\n", "1\n", "20\n", "0\n" ]
none
[ { "input": "5\n1 2\n2 3\n3 4\n4 5\n3\n1 2\n2 3", "output": "3" }, { "input": "3\n2 3\n3 1\n3\n1 2\n1 3", "output": "1" }, { "input": "7\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n4\n4 1\n4 2\n4 3", "output": "20" }, { "input": "5\n1 2\n2 3\n3 4\n4 5\n4\n4 1\n4 2\n4 3", "output": "0" }, { "input": "1\n1", "output": "1" }, { "input": "20\n11 15\n2 12\n5 6\n3 18\n3 4\n1 12\n15 8\n20 7\n14 5\n8 12\n13 18\n10 18\n16 19\n20 9\n13 17\n8 16\n5 17\n15 13\n9 15\n3\n2 1\n3 1", "output": "26" }, { "input": "20\n19 1\n1 12\n18 5\n4 11\n1 2\n5 16\n20 12\n15 6\n6 16\n19 7\n19 14\n2 13\n20 8\n7 5\n1 3\n12 4\n17 4\n1 10\n9 12\n4\n1 3\n2 4\n4 1", "output": "40" }, { "input": "20\n12 9\n15 8\n18 7\n9 3\n20 6\n6 15\n5 12\n2 14\n1 4\n18 19\n12 18\n13 16\n10 9\n17 1\n8 12\n14 11\n18 14\n10 13\n1 5\n12\n6 10\n1 6\n2 9\n11 9\n7 3\n4 10\n9 6\n8 9\n6 7\n12 6\n5 6", "output": "0" }, { "input": "20\n2 6\n4 8\n12 11\n16 2\n1 20\n15 19\n20 14\n7 2\n7 13\n7 19\n20 9\n6 5\n12 7\n14 10\n3 12\n17 18\n17 4\n10 4\n4 16\n4\n3 1\n4 1\n1 2", "output": "11" }, { "input": "20\n19 18\n11 4\n17 4\n7 6\n14 18\n5 6\n10 3\n12 20\n13 16\n11 2\n6 1\n12 6\n18 5\n8 6\n17 6\n13 17\n20 9\n15 13\n10 11\n3\n2 1\n3 1", "output": "32" }, { "input": "20\n14 17\n20 7\n13 15\n20 4\n18 9\n6 3\n16 13\n5 20\n6 14\n13 11\n6 20\n2 20\n4 19\n20 13\n12 13\n2 9\n2 8\n10 1\n6 10\n2\n1 2", "output": "19" }, { "input": "20\n9 19\n20 10\n15 5\n1 4\n2 5\n3 13\n20 2\n17 12\n16 6\n2 3\n19 15\n3 14\n15 11\n8 7\n18 12\n2 17\n2 1\n8 5\n18 16\n11\n6 4\n1 2\n5 11\n8 3\n7 3\n3 5\n4 2\n10 2\n3 2\n9 6", "output": "0" }, { "input": "20\n9 13\n2 7\n11 15\n2 3\n15 14\n11 12\n8 2\n19 8\n6 9\n7 16\n12 1\n20 4\n13 18\n20 2\n10 14\n13 7\n8 15\n17 4\n8 5\n5\n3 2\n4 2\n4 1\n4 5", "output": "46" }, { "input": "20\n14 19\n19 15\n7 17\n19 8\n9 16\n12 16\n10 11\n13 7\n15 13\n2 1\n13 2\n6 15\n9 18\n9 3\n20 19\n10 9\n13 16\n4 13\n12 5\n10\n8 7\n5 6\n2 3\n7 6\n1 9\n9 6\n3 8\n10 3\n2 4", "output": "6" }, { "input": "20\n12 14\n3 19\n6 9\n7 8\n18 1\n13 3\n1 4\n16 18\n15 10\n17 16\n2 5\n19 5\n20 4\n19 7\n12 18\n19 12\n14 15\n11 2\n7 9\n9\n3 4\n6 7\n5 9\n2 5\n5 7\n7 8\n7 1\n4 8", "output": "6" }, { "input": "30\n4 26\n26 16\n11 4\n17 29\n12 15\n14 30\n2 1\n8 9\n21 20\n25 24\n17 6\n10 13\n29 24\n3 26\n18 25\n14 25\n17 7\n4 25\n12 10\n20 10\n25 23\n26 10\n28 13\n19 20\n4 5\n13 27\n22 18\n5 9\n10 1\n2\n2 1", "output": "29" }, { "input": "40\n15 11\n7 2\n6 8\n40 27\n13 39\n5 26\n8 25\n14 30\n36 19\n13 38\n17 3\n8 33\n4 2\n1 40\n17 5\n11 1\n24 33\n4 8\n10 31\n26 32\n38 26\n10 18\n23 32\n1 16\n33 5\n12 6\n6 14\n17 28\n9 8\n22 21\n36 15\n16 35\n32 37\n36 22\n17 36\n20 21\n6 34\n4 31\n11 29\n1", "output": "40" }, { "input": "50\n33 44\n14 10\n26 25\n29 24\n21 15\n13 7\n16 26\n29 17\n16 44\n49 9\n35 42\n28 38\n37 20\n10 12\n48 2\n21 47\n46 40\n46 31\n42 8\n31 45\n38 11\n4 19\n3 35\n22 39\n41 9\n11 6\n34 44\n37 44\n22 41\n33 35\n41 4\n22 1\n13 27\n13 22\n14 33\n18 21\n36 22\n21 31\n30 35\n31 43\n24 12\n44 32\n27 2\n41 21\n1 50\n23 19\n6 22\n14 41\n47 5\n3\n3 1\n3 2", "output": "85" }, { "input": "60\n35 24\n14 29\n40 57\n16 56\n16 35\n9 58\n35 50\n17 45\n9 45\n57 18\n40 60\n19 54\n33 50\n16 6\n32 57\n10 17\n20 35\n55 27\n60 11\n30 19\n27 36\n12 41\n33 44\n50 12\n19 20\n52 12\n51 40\n17 47\n52 42\n39 13\n13 5\n22 58\n59 37\n44 34\n43 42\n36 7\n23 59\n45 49\n4 53\n26 19\n17 35\n39 16\n20 48\n59 6\n38 13\n31 5\n10 3\n40 29\n8 18\n46 43\n50 36\n1 35\n4 15\n4 57\n12 28\n9 21\n58 32\n10 2\n4 25\n2\n2 1", "output": "59" }, { "input": "70\n22 52\n54 23\n67 70\n3 31\n8 40\n68 10\n33 27\n60 61\n50 13\n57 6\n34 66\n29 48\n29 9\n28 44\n6 12\n32 10\n7 30\n26 21\n12 13\n54 34\n14 49\n12 40\n7 18\n43 44\n58 41\n14 25\n53 10\n56 65\n30 49\n38 55\n57 4\n30 15\n54 63\n17 38\n68 7\n34 3\n46 17\n13 44\n20 49\n40 34\n33 7\n2 66\n36 57\n35 18\n30 12\n11 22\n68 22\n67 9\n19 57\n65 13\n62 6\n49 51\n42 27\n61 20\n6 1\n30 24\n12 17\n25 39\n46 37\n14 5\n68 41\n6 64\n12 45\n69 61\n7 26\n59 16\n12 16\n40 67\n47 3\n1", "output": "70" } ]
61
2,048,000
0
15,123
420
Online Meeting
[ "implementation" ]
null
null
Nearly each project of the F company has a whole team of developers working on it. They often are in different rooms of the office in different cities and even countries. To keep in touch and track the results of the project, the F company conducts shared online meetings in a Spyke chat. One day the director of the F company got hold of the records of a part of an online meeting of one successful team. The director watched the record and wanted to talk to the team leader. But how can he tell who the leader is? The director logically supposed that the leader is the person who is present at any conversation during a chat meeting. In other words, if at some moment of time at least one person is present on the meeting, then the leader is present on the meeting. You are the assistant director. Given the 'user logged on'/'user logged off' messages of the meeting in the chronological order, help the director determine who can be the leader. Note that the director has the record of only a continuous part of the meeting (probably, it's not the whole meeting).
The first line contains integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=105) β€” the number of team participants and the number of messages. Each of the next *m* lines contains a message in the format: - '+ *id*': the record means that the person with number *id* (1<=≀<=*id*<=≀<=*n*) has logged on to the meeting. - '- *id*': the record means that the person with number *id* (1<=≀<=*id*<=≀<=*n*) has logged off from the meeting. Assume that all the people of the team are numbered from 1 to *n* and the messages are given in the chronological order. It is guaranteed that the given sequence is the correct record of a continuous part of the meeting. It is guaranteed that no two log on/log off events occurred simultaneously.
In the first line print integer *k* (0<=≀<=*k*<=≀<=*n*) β€” how many people can be leaders. In the next line, print *k* integers in the increasing order β€” the numbers of the people who can be leaders. If the data is such that no member of the team can be a leader, print a single number 0.
[ "5 4\n+ 1\n+ 2\n- 2\n- 1\n", "3 2\n+ 1\n- 2\n", "2 4\n+ 1\n- 1\n+ 2\n- 2\n", "5 6\n+ 1\n- 1\n- 3\n+ 3\n+ 4\n- 4\n", "2 4\n+ 1\n- 2\n+ 2\n- 1\n" ]
[ "4\n1 3 4 5 ", "1\n3 ", "0\n", "3\n2 3 5 ", "0\n" ]
none
[ { "input": "5 4\n+ 1\n+ 2\n- 2\n- 1", "output": "4\n1 3 4 5 " }, { "input": "3 2\n+ 1\n- 2", "output": "1\n3 " }, { "input": "2 4\n+ 1\n- 1\n+ 2\n- 2", "output": "0" }, { "input": "5 6\n+ 1\n- 1\n- 3\n+ 3\n+ 4\n- 4", "output": "3\n2 3 5 " }, { "input": "2 4\n+ 1\n- 2\n+ 2\n- 1", "output": "0" }, { "input": "1 1\n+ 1", "output": "1\n1 " }, { "input": "2 1\n- 2", "output": "2\n1 2 " }, { "input": "3 5\n- 1\n+ 1\n+ 2\n- 2\n+ 3", "output": "1\n1 " }, { "input": "10 8\n+ 1\n- 1\n- 2\n- 3\n+ 3\n+ 7\n- 7\n+ 9", "output": "6\n3 4 5 6 8 10 " }, { "input": "5 5\n+ 5\n+ 2\n+ 3\n+ 4\n+ 1", "output": "1\n5 " }, { "input": "5 4\n+ 1\n- 1\n+ 1\n+ 2", "output": "4\n1 3 4 5 " }, { "input": "10 3\n+ 1\n+ 2\n- 7", "output": "7\n3 4 5 6 8 9 10 " }, { "input": "1 20\n- 1\n+ 1\n- 1\n+ 1\n- 1\n+ 1\n- 1\n+ 1\n- 1\n+ 1\n- 1\n+ 1\n- 1\n+ 1\n- 1\n+ 1\n- 1\n+ 1\n- 1\n+ 1", "output": "1\n1 " }, { "input": "20 1\n- 16", "output": "20\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 " }, { "input": "50 20\n- 6\n+ 40\n- 3\n- 23\n+ 31\n- 27\n- 40\n+ 25\n+ 29\n- 41\n- 16\n+ 23\n+ 20\n+ 13\n- 45\n+ 40\n+ 24\n+ 22\n- 23\n+ 17", "output": "34\n1 2 4 5 7 8 9 10 11 12 14 15 18 19 21 26 28 30 32 33 34 35 36 37 38 39 42 43 44 46 47 48 49 50 " }, { "input": "20 50\n+ 5\n+ 11\n- 5\n+ 6\n- 16\n- 13\n+ 5\n+ 7\n- 8\n- 7\n- 10\n+ 10\n- 20\n- 19\n+ 17\n- 2\n+ 2\n+ 19\n+ 18\n- 2\n- 6\n- 5\n+ 6\n+ 4\n- 14\n+ 14\n- 9\n+ 15\n- 17\n- 15\n+ 2\n+ 5\n- 2\n+ 9\n- 11\n+ 2\n- 19\n+ 7\n+ 12\n+ 16\n+ 19\n- 18\n- 2\n+ 18\n- 9\n- 10\n+ 9\n+ 13\n- 14\n- 16", "output": "2\n1 3 " }, { "input": "100 5\n- 60\n- 58\n+ 25\n- 32\n+ 86", "output": "95\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 26 27 28 29 30 31 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 59 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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 " }, { "input": "4 4\n+ 2\n- 1\n- 3\n- 2", "output": "1\n4 " }, { "input": "3 3\n- 2\n+ 1\n+ 2", "output": "1\n3 " }, { "input": "5 4\n- 1\n- 2\n+ 3\n+ 4", "output": "1\n5 " }, { "input": "6 6\n- 5\n- 6\n- 3\n- 1\n- 2\n- 4", "output": "1\n4 " }, { "input": "10 7\n- 8\n+ 1\n+ 2\n+ 3\n- 2\n- 3\n- 1", "output": "6\n4 5 6 7 9 10 " }, { "input": "10 7\n- 8\n+ 1\n+ 2\n+ 3\n- 2\n- 3\n- 1", "output": "6\n4 5 6 7 9 10 " }, { "input": "4 10\n+ 2\n- 1\n- 2\n- 3\n+ 3\n+ 2\n+ 4\n- 2\n+ 2\n+ 1", "output": "1\n3 " }, { "input": "4 9\n+ 2\n- 1\n- 2\n- 3\n+ 3\n+ 2\n+ 4\n- 2\n+ 2", "output": "1\n3 " }, { "input": "10 8\n+ 1\n- 1\n- 4\n+ 4\n+ 3\n+ 7\n- 7\n+ 9", "output": "6\n2 4 5 6 8 10 " }, { "input": "10 6\n+ 2\n- 2\n+ 2\n- 2\n+ 2\n- 3", "output": "8\n1 4 5 6 7 8 9 10 " }, { "input": "10 5\n+ 2\n- 2\n+ 2\n- 2\n- 3", "output": "9\n1 3 4 5 6 7 8 9 10 " }, { "input": "10 11\n+ 1\n- 1\n- 2\n+ 3\n- 3\n- 4\n+ 5\n- 5\n- 6\n+ 6\n+ 7", "output": "4\n6 8 9 10 " }, { "input": "10 10\n+ 1\n- 1\n- 2\n+ 3\n- 3\n- 4\n+ 5\n- 5\n- 6\n+ 6", "output": "5\n6 7 8 9 10 " }, { "input": "10 9\n+ 1\n- 1\n- 2\n+ 3\n- 3\n- 4\n+ 5\n- 5\n- 6", "output": "5\n6 7 8 9 10 " }, { "input": "10 12\n+ 1\n- 1\n- 2\n+ 3\n- 3\n- 4\n+ 5\n- 5\n- 6\n+ 6\n+ 7\n- 7", "output": "4\n6 8 9 10 " }, { "input": "2 2\n- 1\n+ 1", "output": "2\n1 2 " }, { "input": "7 4\n- 2\n- 3\n+ 3\n- 6", "output": "4\n1 4 5 7 " }, { "input": "2 3\n+ 1\n+ 2\n- 1", "output": "0" }, { "input": "5 5\n- 2\n+ 1\n+ 2\n- 2\n+ 4", "output": "2\n3 5 " }, { "input": "5 3\n+ 1\n- 1\n+ 2", "output": "3\n3 4 5 " }, { "input": "4 4\n- 1\n+ 1\n- 1\n+ 2", "output": "2\n3 4 " } ]
124
0
0
15,202
949
A Leapfrog in the Array
[ "constructive algorithms", "math" ]
null
null
Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm. Let's consider that initially array contains *n* numbers from 1 to *n* and the number *i* is located in the cell with the index 2*i*<=-<=1 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all *n* numbers will appear in the first *n* cells of the array. For example if *n*<==<=4, the array is changing as follows: You have to write a program that allows you to determine what number will be in the cell with index *x* (1<=≀<=*x*<=≀<=*n*) after Dima's algorithm finishes.
The first line contains two integers *n* and *q* (1<=≀<=*n*<=≀<=1018, 1<=≀<=*q*<=≀<=200<=000), the number of elements in the array and the number of queries for which it is needed to find the answer. Next *q* lines contain integers *x**i* (1<=≀<=*x**i*<=≀<=*n*), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes.
For each of *q* queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes.
[ "4 3\n2\n3\n4\n", "13 4\n10\n5\n4\n8\n" ]
[ "3\n2\n4\n", "13\n3\n8\n9\n" ]
The first example is shown in the picture. In the second example the final array is [1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7].
[ { "input": "4 3\n2\n3\n4", "output": "3\n2\n4" }, { "input": "13 4\n10\n5\n4\n8", "output": "13\n3\n8\n9" }, { "input": "2 2\n1\n2", "output": "1\n2" }, { "input": "1 1\n1", "output": "1" }, { "input": "3 3\n3\n2\n1", "output": "2\n3\n1" }, { "input": "12 12\n9\n11\n5\n3\n7\n2\n8\n6\n4\n10\n12\n1", "output": "5\n6\n3\n2\n4\n7\n12\n8\n10\n9\n11\n1" } ]
2,000
5,632,000
0
15,218
430
Balls Game
[ "brute force", "two pointers" ]
null
null
Iahub is training for the IOI. What is a better way to train than playing a Zuma-like game? There are *n* balls put in a row. Each ball is colored in one of *k* colors. Initially the row doesn't contain three or more contiguous balls with the same color. Iahub has a single ball of color *x*. He can insert his ball at any position in the row (probably, between two other balls). If at any moment there are three or more contiguous balls of the same color in the row, they are destroyed immediately. This rule is applied multiple times, until there are no more sets of 3 or more contiguous balls of the same color. For example, if Iahub has the row of balls [black, black, white, white, black, black] and a white ball, he can insert the ball between two white balls. Thus three white balls are destroyed, and then four black balls become contiguous, so all four balls are destroyed. The row will not contain any ball in the end, so Iahub can destroy all 6 balls. Iahub wants to destroy as many balls as possible. You are given the description of the row of balls, and the color of Iahub's ball. Help Iahub train for the IOI by telling him the maximum number of balls from the row he can destroy.
The first line of input contains three integers: *n* (1<=≀<=*n*<=≀<=100), *k* (1<=≀<=*k*<=≀<=100) and *x* (1<=≀<=*x*<=≀<=*k*). The next line contains *n* space-separated integers *c*1,<=*c*2,<=...,<=*c**n* (1<=≀<=*c**i*<=≀<=*k*). Number *c**i* means that the *i*-th ball in the row has color *c**i*. It is guaranteed that the initial row of balls will never contain three or more contiguous balls of the same color.
Print a single integer β€” the maximum number of balls Iahub can destroy.
[ "6 2 2\n1 1 2 2 1 1\n", "1 1 1\n1\n" ]
[ "6\n", "0\n" ]
none
[ { "input": "6 2 2\n1 1 2 2 1 1", "output": "6" }, { "input": "1 1 1\n1", "output": "0" }, { "input": "10 2 1\n2 1 2 2 1 2 2 1 1 2", "output": "5" }, { "input": "50 2 1\n1 1 2 2 1 2 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 2 1 2 1 2 1 2 2 1 1 2 2 1 1 2 2 1 2 1 1 2 1 1 2 2 1 1 2", "output": "15" }, { "input": "75 5 5\n1 1 5 5 3 5 2 3 3 2 2 1 1 5 4 4 3 4 5 4 3 3 1 2 2 1 2 1 2 5 5 2 1 3 2 2 3 1 2 1 1 5 5 1 1 2 1 1 2 2 5 2 2 1 1 2 1 2 1 1 3 3 5 4 4 3 3 4 4 5 5 1 1 2 2", "output": "6" }, { "input": "100 3 2\n1 1 2 3 1 3 2 1 1 3 3 2 2 1 1 2 2 1 1 3 2 2 3 2 3 2 2 3 3 1 1 2 2 1 2 2 1 3 3 1 3 3 1 2 1 2 2 1 2 3 2 1 1 2 1 1 3 3 1 3 3 1 1 2 2 1 1 2 1 3 2 2 3 2 2 3 3 1 2 1 2 2 1 1 2 3 1 3 3 1 2 3 2 2 1 3 2 2 3 3", "output": "6" }, { "input": "100 2 1\n2 2 1 2 1 2 1 2 2 1 1 2 1 1 2 1 1 2 2 1 1 2 1 1 2 1 2 2 1 2 1 2 1 2 1 1 2 1 1 2 1 1 2 2 1 1 2 1 2 2 1 2 1 2 1 2 1 1 2 2 1 2 1 1 2 2 1 1 2 1 2 1 2 1 2 2 1 2 1 1 2 1 2 1 1 2 1 1 2 1 1 2 2 1 2 2 1 1 2 1", "output": "15" }, { "input": "100 2 2\n1 2 1 2 2 1 2 1 2 1 2 1 1 2 1 2 2 1 1 2 1 1 2 2 1 1 2 1 2 2 1 2 2 1 2 1 2 1 1 2 1 2 1 2 1 2 1 1 2 1 1 2 2 1 1 2 2 1 2 1 2 1 2 1 2 2 1 2 1 2 2 1 1 2 1 2 2 1 1 2 2 1 2 1 2 1 1 2 1 2 1 2 1 2 1 2 2 1 2 2", "output": "14" }, { "input": "100 2 2\n1 2 1 1 2 1 2 2 1 2 1 2 1 2 1 2 1 2 2 1 1 2 2 1 2 1 1 2 2 1 1 2 1 2 1 2 1 1 2 1 1 2 1 2 2 1 1 2 2 1 1 2 1 2 2 1 1 2 1 2 1 2 2 1 2 2 1 1 2 1 2 2 1 2 2 1 2 1 1 2 1 2 2 1 2 2 1 2 1 2 1 2 1 1 2 2 1 1 2 2", "output": "17" }, { "input": "100 2 2\n2 1 1 2 2 1 1 2 1 2 1 1 2 2 1 2 1 2 1 2 2 1 2 1 1 2 1 2 1 2 1 2 1 1 2 2 1 1 2 1 1 2 1 2 2 1 1 2 1 2 1 1 2 2 1 1 2 1 2 1 2 1 2 2 1 1 2 2 1 1 2 2 1 2 1 2 1 1 2 1 1 2 2 1 2 1 2 2 1 2 2 1 1 2 1 2 2 1 2 2", "output": "17" }, { "input": "100 2 2\n1 2 2 1 2 2 1 1 2 1 2 1 2 1 2 1 2 1 2 1 1 2 2 1 2 1 2 1 2 1 2 1 1 2 1 1 2 1 2 2 1 1 2 2 1 1 2 1 1 2 2 1 2 1 2 1 2 1 2 1 1 2 2 1 1 2 2 1 1 2 2 1 2 2 1 1 2 1 2 2 1 2 2 1 2 2 1 2 2 1 1 2 2 1 2 1 2 1 2 1", "output": "28" }, { "input": "100 2 2\n1 1 2 1 2 1 1 2 1 2 1 2 2 1 2 1 2 1 1 2 2 1 2 1 1 2 2 1 1 2 1 2 2 1 2 2 1 2 1 2 1 1 2 1 2 1 1 2 2 1 1 2 1 2 1 2 1 2 1 2 2 1 1 2 1 2 2 1 2 1 1 2 1 1 2 1 2 1 2 1 1 2 1 2 2 1 2 1 2 2 1 1 2 1 2 2 1 1 2 2", "output": "8" }, { "input": "100 100 50\n15 44 5 7 75 40 52 82 78 90 48 32 16 53 69 2 21 84 7 21 21 87 29 8 42 54 10 21 38 55 54 88 48 63 3 17 45 82 82 91 7 11 11 24 24 79 1 32 32 38 41 41 4 4 74 17 26 26 96 96 3 3 50 50 96 26 26 17 17 74 74 4 41 38 38 32 1 1 79 79 24 11 11 7 7 91 91 82 45 45 97 9 74 60 32 91 61 64 100 26", "output": "2" }, { "input": "100 50 22\n15 2 18 15 48 35 46 33 32 39 39 5 5 27 27 50 50 47 47 10 10 6 3 3 7 8 7 17 17 29 14 10 10 46 13 13 31 32 31 22 22 32 31 31 32 13 13 46 46 10 10 14 14 29 29 17 7 7 8 3 6 6 10 47 50 50 27 5 5 39 39 21 47 4 40 47 21 28 21 21 40 27 34 17 3 36 5 7 21 14 25 49 40 34 32 13 23 29 2 4", "output": "2" }, { "input": "100 3 3\n3 1 1 2 1 1 3 1 3 3 1 3 3 1 2 1 1 2 2 3 3 2 3 2 2 3 1 3 3 2 2 1 3 3 2 2 1 2 3 3 1 3 1 3 1 2 2 1 2 1 2 3 1 3 1 3 2 1 3 2 3 3 2 3 2 3 1 3 2 2 1 2 1 2 1 1 3 1 3 1 2 1 2 1 2 3 2 2 3 3 2 2 3 2 2 3 1 1 2 3", "output": "6" }, { "input": "100 100 100\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", "output": "0" }, { "input": "100 2 2\n1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2", "output": "98" }, { "input": "6 20 10\n10 2 10 10 2 2", "output": "5" } ]
109
0
0
15,301
900
Unusual Sequences
[ "bitmasks", "combinatorics", "dp", "math", "number theory" ]
null
null
Count the number of distinct sequences *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*) consisting of positive integers such that *gcd*(*a*1,<=*a*2,<=...,<=*a**n*)<==<=*x* and . As this number could be large, print the answer modulo 109<=+<=7. *gcd* here means the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor).
The only line contains two positive integers *x* and *y* (1<=≀<=*x*,<=*y*<=≀<=109).
Print the number of such sequences modulo 109<=+<=7.
[ "3 9\n", "5 8\n" ]
[ "3\n", "0\n" ]
There are three suitable sequences in the first test: (3, 3, 3), (3, 6), (6, 3). There are no suitable sequences in the second test.
[ { "input": "3 9", "output": "3" }, { "input": "5 8", "output": "0" }, { "input": "2 12", "output": "27" }, { "input": "1 8", "output": "120" }, { "input": "1 9", "output": "252" }, { "input": "1000000000 1000000000", "output": "1" }, { "input": "1000000000 1", "output": "0" }, { "input": "1 1000000000", "output": "824916815" }, { "input": "1 223092870", "output": "521342052" }, { "input": "1 1", "output": "1" }, { "input": "1 994593600", "output": "558135120" }, { "input": "1 425613469", "output": "455729363" }, { "input": "495219 444706662", "output": "115165527" }, { "input": "9357 18255507", "output": "745979764" }, { "input": "741547455 471761895", "output": "0" }, { "input": "225 315096300", "output": "413133630" }, { "input": "183612440 509579899", "output": "0" }, { "input": "231096994 462193988", "output": "1" }, { "input": "34601 35742833", "output": "60054095" }, { "input": "417485019 230941257", "output": "0" }, { "input": "524 991033864", "output": "172439543" }, { "input": "859550004 563726557", "output": "0" }, { "input": "1 282521795", "output": "436596181" }, { "input": "415879151 194713963", "output": "0" }, { "input": "109936444 989427996", "output": "252" } ]
93
0
0
15,324
963
Destruction of a Tree
[ "constructive algorithms", "dfs and similar", "dp", "greedy", "trees" ]
null
null
You are given a tree (a graph with *n* vertices and *n*<=-<=1 edges in which it's possible to reach any vertex from any other vertex using only its edges). A vertex can be destroyed if this vertex has even degree. If you destroy a vertex, all edges connected to it are also deleted. Destroy all vertices in the given tree or determine that it is impossible.
The first line contains integer *n* (1<=≀<=*n*<=≀<=2Β·105)Β β€” number of vertices in a tree. The second line contains *n* integers *p*1,<=*p*2,<=...,<=*p**n* (0<=≀<=*p**i*<=≀<=*n*). If *p**i*<=β‰ <=0 there is an edge between vertices *i* and *p**i*. It is guaranteed that the given graph is a tree.
If it's possible to destroy all vertices, print "YES" (without quotes), otherwise print "NO" (without quotes). If it's possible to destroy all vertices, in the next *n* lines print the indices of the vertices in order you destroy them. If there are multiple correct answers, print any.
[ "5\n0 1 2 1 2\n", "4\n0 1 2 3\n" ]
[ "YES\n1\n2\n3\n5\n4\n", "NO\n" ]
In the first example at first you have to remove the vertex with index 1 (after that, the edges (1, 2) and (1, 4) are removed), then the vertex with index 2 (and edges (2, 3) and (2, 5) are removed). After that there are no edges in the tree, so you can remove remaining vertices in any order.
[ { "input": "5\n0 1 2 1 2", "output": "YES\n1\n2\n3\n5\n4" }, { "input": "4\n0 1 2 3", "output": "NO" }, { "input": "1\n0", "output": "YES\n1" }, { "input": "8\n3 1 4 0 4 2 4 5", "output": "NO" }, { "input": "100\n81 96 65 28 4 40 5 49 5 89 48 70 94 70 17 58 58 1 61 19 45 33 46 19 22 83 56 67 62 82 57 16 29 36 84 71 42 66 78 54 73 45 82 80 67 88 79 69 61 66 5 36 24 60 96 21 77 67 68 29 87 37 91 34 78 43 0 69 49 62 16 2 68 79 57 1 60 12 39 99 14 37 30 92 47 18 14 75 73 39 94 12 43 87 90 22 91 59 54 71", "output": "NO" }, { "input": "100\n57 85 27 81 41 27 73 10 73 95 91 90 89 41 86 44 6 20 9 13 46 73 56 19 37 32 40 42 79 76 96 5 6 8 76 52 14 86 33 69 100 95 58 87 43 47 17 39 48 28 77 65 100 100 41 39 87 5 61 67 94 64 61 88 32 23 79 44 0 67 44 23 48 96 48 56 86 75 90 2 17 46 4 75 42 90 17 77 5 33 87 91 27 28 58 95 58 47 33 6", "output": "NO" }, { "input": "21\n11 10 12 3 6 0 8 6 16 14 5 9 7 19 1 13 15 21 4 2 20", "output": "YES\n21\n18\n2\n20\n14\n10\n4\n19\n12\n3\n16\n9\n7\n13\n6\n8\n11\n5\n15\n17\n1" }, { "input": "61\n10 42 20 50 4 24 18 55 19 5 57 13 3 35 58 48 31 46 40 45 15 53 14 25 43 41 22 23 54 39 38 44 16 37 12 34 32 28 26 30 59 47 21 9 8 52 1 0 33 49 36 51 17 11 29 7 48 61 6 27 2", "output": "YES\n27\n60\n53\n22\n31\n17\n28\n38\n14\n23\n12\n35\n3\n13\n45\n20\n55\n8\n54\n29\n57\n11\n16\n48\n49\n33\n4\n50\n10\n5\n7\n56\n46\n18\n51\n52\n34\n36\n32\n37\n9\n44\n40\n19\n39\n30\n41\n26\n6\n59\n25\n24\n21\n43\n58\n15\n2\n61\n47\n42\n1" }, { "input": "21\n11 19 4 19 6 0 13 7 6 2 5 3 16 10 1 9 15 21 9 21 2", "output": "YES\n7\n8\n16\n13\n10\n14\n2\n21\n18\n20\n3\n12\n19\n4\n6\n9\n11\n5\n15\n17\n1" }, { "input": "61\n47 61 20 5 10 59 46 55 44 1 57 13 3 35 21 48 31 7 9 45 43 53 14 6 42 39 22 23 54 40 45 37 16 36 12 44 34 28 25 19 26 33 25 39 33 36 42 0 50 4 52 46 17 11 29 7 48 15 41 27 58", "output": "YES\n6\n24\n41\n59\n40\n30\n9\n19\n37\n32\n52\n51\n46\n7\n18\n56\n36\n34\n61\n2\n15\n58\n43\n21\n25\n39\n26\n44\n55\n8\n54\n29\n57\n11\n16\n48\n28\n38\n14\n23\n12\n35\n3\n13\n27\n60\n53\n22\n31\n17\n45\n20\n42\n33\n50\n49\n5\n4\n1\n47\n10" }, { "input": "79\n0 56 56 42 56 56 56 56 4 56 56 22 56 56 56 48 56 56 56 56 56 24 56 16 56 56 56 9 56 56 56 56 56 56 56 56 56 55 56 56 12 20 56 28 56 56 56 38 56 56 56 56 56 56 44 1 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56", "output": "YES\n12\n41\n24\n22\n48\n16\n55\n38\n28\n44\n4\n9\n20\n42\n56\n2\n3\n5\n6\n7\n8\n10\n11\n13\n14\n15\n17\n18\n19\n21\n23\n25\n26\n27\n29\n30\n31\n32\n33\n34\n35\n36\n37\n39\n40\n43\n45\n46\n47\n49\n50\n51\n52\n53\n54\n57\n58\n59\n60\n61\n62\n63\n64\n65\n66\n67\n68\n69\n70\n71\n72\n73\n74\n75\n76\n77\n78\n79\n1" }, { "input": "121\n110 31 57 33 45 33 33 33 91 102 79 33 61 72 107 101 117 10 118 33 33 64 24 94 117 76 33 23 33 49 5 52 95 78 33 39 33 92 17 33 25 33 56 33 3 88 33 108 62 15 28 111 67 33 33 11 96 33 36 70 46 98 80 104 33 19 60 33 112 51 33 2 33 33 121 59 33 41 50 81 105 33 115 34 33 18 84 32 33 33 87 13 86 103 16 119 33 63 30 43 83 53 26 100 69 33 14 38 33 75 66 120 33 33 9 99 0 93 1 48 116", "output": "YES\n33\n4\n6\n7\n8\n12\n20\n21\n27\n29\n35\n37\n40\n42\n44\n47\n54\n55\n58\n65\n68\n71\n73\n74\n77\n82\n85\n89\n90\n97\n106\n109\n113\n114\n16\n95\n83\n101\n9\n115\n87\n91\n34\n84\n41\n78\n117\n25\n39\n17\n59\n36\n26\n76\n94\n103\n23\n24\n51\n28\n60\n70\n53\n67\n10\n102\n86\n18\n118\n93\n66\n19\n52\n111\n88\n32\n61\n46\n92\n13\n108\n38\n120\n48\n69\n112\n81\n105\n63\n80\n62\n98\n30\n49\n116\n99\n75\n121\n64\n22\n100\n104\n56\n43\n79\n11\n15\n50\n14\n107\n2\n72\n5\n31\n3\n45\n96\n57\n1\n110\n119" }, { "input": "21\n5 20 9 19 8 0 13 6 13 19 5 3 8 10 1 9 1 20 3 10 18", "output": "YES\n18\n21\n20\n2\n10\n14\n19\n4\n3\n12\n9\n16\n13\n7\n8\n6\n5\n11\n1\n15\n17" }, { "input": "61\n5 61 20 5 50 59 56 29 44 1 48 13 20 35 61 33 38 52 30 8 43 17 35 43 24 59 22 23 11 26 38 37 48 36 13 37 44 23 30 19 26 1 15 19 8 18 42 0 50 33 52 36 17 11 29 18 48 15 24 22 42", "output": "YES\n56\n7\n18\n46\n52\n51\n36\n34\n37\n32\n44\n9\n19\n40\n30\n39\n26\n41\n59\n6\n24\n25\n43\n21\n15\n58\n61\n2\n42\n47\n1\n5\n4\n50\n49\n33\n16\n48\n11\n29\n8\n20\n3\n13\n12\n35\n14\n23\n28\n38\n17\n22\n27\n60\n53\n31\n45\n55\n54\n57\n10" }, { "input": "21\n18 18 18 18 18 0 18 18 18 18 18 18 18 18 18 18 18 6 18 18 18", "output": "YES\n18\n2\n3\n4\n5\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n6\n19\n20\n21\n1" }, { "input": "61\n56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 0 56 56 56 56 56 56 56 48 56 56 56 56 56", "output": "YES\n56\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40\n41\n42\n43\n44\n45\n46\n47\n49\n50\n51\n52\n53\n54\n55\n48\n57\n58\n59\n60\n61\n1" }, { "input": "21\n15 6 13 7 15 21 8 0 7 16 16 21 12 6 12 12 13 6 15 16 7", "output": "YES\n6\n2\n14\n18\n7\n4\n8\n9\n16\n10\n11\n20\n15\n5\n12\n21\n13\n3\n17\n19\n1" }, { "input": "61\n58 39 45 57 31 43 11 24 8 18 56 54 47 37 50 40 19 16 29 10 1 23 36 28 21 48 52 55 27 42 2 33 46 25 53 6 15 26 14 17 9 44 56 34 5 61 38 12 30 7 49 32 20 41 51 0 3 4 60 35 13", "output": "YES\n23\n22\n6\n36\n56\n43\n7\n11\n15\n50\n14\n37\n2\n39\n5\n31\n3\n45\n4\n57\n60\n59\n53\n35\n10\n20\n16\n18\n17\n40\n29\n19\n52\n27\n33\n32\n61\n46\n47\n13\n26\n38\n12\n48\n41\n54\n8\n9\n28\n24\n51\n55\n30\n49\n44\n42\n25\n34\n1\n58\n21" }, { "input": "21\n21 6 4 20 14 1 13 10 11 0 10 18 10 12 4 1 2 2 8 2 13", "output": "YES\n8\n19\n11\n9\n21\n13\n7\n10\n14\n5\n18\n12\n20\n4\n3\n15\n2\n17\n1\n6\n16" }, { "input": "61\n17 19 8 53 10 38 59 60 46 25 49 28 46 15 25 56 53 60 60 54 18 49 10 53 29 19 11 61 24 11 17 52 32 54 29 55 0 1 14 56 25 14 33 53 47 56 8 6 53 55 16 46 47 9 24 37 3 52 25 37 26", "output": "YES\n15\n14\n39\n42\n59\n7\n3\n57\n18\n21\n28\n12\n26\n61\n19\n2\n16\n51\n9\n54\n20\n34\n33\n43\n37\n56\n40\n46\n13\n52\n32\n58\n8\n60\n47\n45\n6\n48\n1\n17\n53\n4\n24\n29\n25\n10\n5\n23\n41\n35\n55\n36\n50\n44\n49\n11\n27\n30\n22\n31\n38" }, { "input": "21\n18 0 18 2 21 2 9 15 3 5 8 2 8 21 6 10 21 13 9 1 13", "output": "YES\n3\n9\n7\n19\n6\n2\n4\n12\n10\n16\n21\n5\n14\n17\n13\n8\n15\n11\n1\n18\n20" }, { "input": "61\n45 48 30 23 15 47 8 3 35 56 54 35 17 47 35 56 32 42 14 37 36 44 6 44 1 44 41 46 43 0 33 3 44 54 43 3 47 57 7 32 29 60 36 36 43 61 36 47 3 48 18 8 17 29 3 54 3 6 43 43 56", "output": "YES\n41\n27\n46\n28\n56\n10\n16\n61\n29\n54\n11\n34\n15\n5\n7\n39\n8\n52\n57\n38\n33\n31\n44\n22\n24\n26\n23\n4\n6\n58\n14\n19\n37\n20\n47\n48\n2\n50\n18\n51\n60\n42\n43\n35\n9\n12\n36\n21\n3\n30\n32\n17\n13\n53\n40\n49\n55\n59\n1\n45\n25" } ]
46
0
0
15,402
533
Berland Miners
[ "binary search", "data structures", "dfs and similar", "greedy", "trees" ]
null
null
The biggest gold mine in Berland consists of *n* caves, connected by *n*<=-<=1 transitions. The entrance to the mine leads to the cave number 1, it is possible to go from it to any remaining cave of the mine by moving along the transitions. The mine is being developed by the InMine Inc., *k* miners work for it. Each day the corporation sorts miners into caves so that each cave has at most one miner working there. For each cave we know the height of its ceiling *h**i* in meters, and for each miner we know his height *s**j*, also in meters. If a miner's height doesn't exceed the height of the cave ceiling where he is, then he can stand there comfortably, otherwise, he has to stoop and that makes him unhappy. Unfortunately, miners typically go on strike in Berland, so InMine makes all the possible effort to make miners happy about their work conditions. To ensure that no miner goes on strike, you need make sure that no miner has to stoop at any moment on his way from the entrance to the mine to his cave (in particular, he must be able to stand comfortably in the cave where he works). To reach this goal, you can choose exactly one cave and increase the height of its ceiling by several meters. However enlarging a cave is an expensive and complex procedure. That's why InMine Inc. asks you either to determine the minimum number of meters you should raise the ceiling of some cave so that it is be possible to sort the miners into the caves and keep all miners happy with their working conditions or to determine that it is impossible to achieve by raising ceiling in exactly one cave.
The first line contains integer *n* (1<=≀<=*n*<=≀<=5Β·105) β€” the number of caves in the mine. Then follows a line consisting of *n* positive integers *h*1,<=*h*2,<=...,<=*h**n* (1<=≀<=*h**i*<=≀<=109), where *h**i* is the height of the ceiling in the *i*-th cave. Next *n*<=-<=1 lines contain the descriptions of transitions between the caves. Each line has the form *a**i*,<=*b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*, *a**i*<=β‰ <=*b**i*), where *a**i* and *b**i* are the numbers of the caves connected by a path. The next line contains integer *k* (1<=≀<=*k*<=≀<=*n*). The last line contains *k* integers *s*1,<=*s*2,<=...,<=*s**k* (1<=≀<=*s**j*<=≀<=109), where *s**j* is the *j*-th miner's height.
In the single line print the minimum number of meters that you need to raise the ceiling by in some cave so that all miners could be sorted into caves and be happy about the work conditions. If it is impossible to do, print <=-<=1. If it is initially possible and there's no need to raise any ceiling, print 0.
[ "6\n5 8 4 6 3 12\n1 2\n1 3\n4 2\n2 5\n6 3\n6\n7 4 2 5 3 11\n", "7\n10 14 7 12 4 50 1\n1 2\n2 3\n2 4\n5 1\n6 5\n1 7\n6\n7 3 4 8 8 10\n", "3\n4 2 8\n1 2\n1 3\n2\n17 15\n" ]
[ "6\n", "0\n", "-1\n" ]
In the first sample test we should increase ceiling height in the first cave from 5 to 11. After that we can distribute miners as following (first goes index of a miner, then index of a cave): <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/5d9b256bdaa3f5b0f9a3fc3b9f56256306a7a570.png" style="max-width: 100.0%;max-height: 100.0%;"/>. In the second sample test there is no need to do anything since it is already possible to distribute miners as following: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/ba4a2592a4bea5feafeae1ab8dec98663bf2c557.png" style="max-width: 100.0%;max-height: 100.0%;"/>. In the third sample test it is impossible.
[]
46
0
0
15,480
846
Monitor
[ "binary search", "data structures" ]
null
null
Recently Luba bought a monitor. Monitor is a rectangular matrix of size *n*<=Γ—<=*m*. But then she started to notice that some pixels cease to work properly. Luba thinks that the monitor will become broken the first moment when it contains a square *k*<=Γ—<=*k* consisting entirely of broken pixels. She knows that *q* pixels are already broken, and for each of them she knows the moment when it stopped working. Help Luba to determine when the monitor became broken (or tell that it's still not broken even after all *q* pixels stopped working).
The first line contains four integer numbers *n*,<=*m*,<=*k*,<=*q*Β (1<=≀<=*n*,<=*m*<=≀<=500,<=1<=≀<=*k*<=≀<=*min*(*n*,<=*m*),<=0<=≀<=*q*<=≀<=*n*Β·*m*) β€” the length and width of the monitor, the size of a rectangle such that the monitor is broken if there is a broken rectangle with this size, and the number of broken pixels. Each of next *q* lines contain three integer numbers *x**i*,<=*y**i*,<=*t**i*Β (1<=≀<=*x**i*<=≀<=*n*,<=1<=≀<=*y**i*<=≀<=*m*,<=0<=≀<=*t*<=≀<=109) β€” coordinates of *i*-th broken pixel (its row and column in matrix) and the moment it stopped working. Each pixel is listed at most once. We consider that pixel is already broken at moment *t**i*.
Print one number β€” the minimum moment the monitor became broken, or "-1" if it's still not broken after these *q* pixels stopped working.
[ "2 3 2 5\n2 1 8\n2 2 8\n1 2 1\n1 3 4\n2 3 2\n", "3 3 2 5\n1 2 2\n2 2 1\n2 3 5\n3 2 10\n2 1 100\n" ]
[ "8\n", "-1\n" ]
none
[ { "input": "2 3 2 5\n2 1 8\n2 2 8\n1 2 1\n1 3 4\n2 3 2", "output": "8" }, { "input": "3 3 2 5\n1 2 2\n2 2 1\n2 3 5\n3 2 10\n2 1 100", "output": "-1" }, { "input": "29 50 5 29\n21 42 1565821\n21 43 53275635\n21 44 2717830\n21 45 9579585\n21 46 20725775\n22 42 2568372\n22 43 9584662\n22 44 31411635\n22 45 5089311\n22 46 4960702\n23 42 11362237\n23 43 42200296\n23 44 18762146\n23 45 8553819\n23 46 4819516\n24 42 10226552\n24 43 21022685\n24 44 32940182\n24 45 39208099\n24 46 3119232\n25 42 8418247\n25 43 4093694\n25 44 9162006\n25 45 328637\n25 46 13121717\n6 21 3147344\n28 26 12445148\n5 7 925220\n25 35 170187", "output": "53275635" }, { "input": "500 500 1 0", "output": "-1" }, { "input": "1 1 1 0", "output": "-1" }, { "input": "1 1 1 1\n1 1 228", "output": "228" }, { "input": "4 5 2 20\n1 2 3\n1 3 8\n4 3 6\n4 5 2\n2 2 15\n1 5 14\n3 5 10\n1 4 16\n2 3 7\n2 4 17\n2 5 1\n1 1 12\n3 4 19\n2 1 13\n3 2 18\n4 2 11\n4 1 4\n3 3 9\n3 1 0\n4 4 5", "output": "15" }, { "input": "4 2 1 4\n4 2 3\n2 2 0\n4 1 2\n1 1 1", "output": "0" }, { "input": "3 4 2 9\n3 3 8\n1 1 5\n1 2 4\n3 1 2\n1 4 7\n3 4 1\n2 4 0\n2 3 6\n1 3 3", "output": "7" } ]
2,000
5,734,400
0
15,482
203
Game on Paper
[ "brute force", "implementation" ]
null
null
One not particularly beautiful evening Valera got very bored. To amuse himself a little bit, he found the following game. He took a checkered white square piece of paper, consisting of *n*<=Γ—<=*n* cells. After that, he started to paint the white cells black one after the other. In total he painted *m* different cells on the piece of paper. Since Valera was keen on everything square, he wondered, how many moves (i.e. times the boy paints a square black) he should make till a black square with side 3 can be found on the piece of paper. But Valera does not know the answer to this question, so he asks you to help him. Your task is to find the minimum number of moves, till the checkered piece of paper has at least one black square with side of 3. Otherwise determine that such move does not exist.
The first line contains two integers *n* and *m* (1<=≀<=*n*<=≀<=1000, 1<=≀<=*m*<=≀<=*min*(*n*Β·*n*,<=105)) β€” the size of the squared piece of paper and the number of moves, correspondingly. Then, *m* lines contain the description of the moves. The *i*-th line contains two integers *x**i*, *y**i* (1<=≀<=*x**i*,<=*y**i*<=≀<=*n*) β€” the number of row and column of the square that gets painted on the *i*-th move. All numbers on the lines are separated by single spaces. It is guaranteed that all moves are different. The moves are numbered starting from 1 in the order, in which they are given in the input. The columns of the squared piece of paper are numbered starting from 1, from the left to the right. The rows of the squared piece of paper are numbered starting from 1, from top to bottom.
On a single line print the answer to the problem β€” the minimum number of the move after which the piece of paper has a black square with side 3. If no such move exists, print -1.
[ "4 11\n1 1\n1 2\n1 3\n2 2\n2 3\n1 4\n2 4\n3 4\n3 2\n3 3\n4 1\n", "4 12\n1 1\n1 2\n1 3\n2 2\n2 3\n1 4\n2 4\n3 4\n3 2\n4 2\n4 1\n3 1\n" ]
[ "10\n", "-1\n" ]
none
[ { "input": "4 11\n1 1\n1 2\n1 3\n2 2\n2 3\n1 4\n2 4\n3 4\n3 2\n3 3\n4 1", "output": "10" }, { "input": "4 12\n1 1\n1 2\n1 3\n2 2\n2 3\n1 4\n2 4\n3 4\n3 2\n4 2\n4 1\n3 1", "output": "-1" }, { "input": "3 1\n1 3", "output": "-1" }, { "input": "3 8\n1 3\n3 3\n2 2\n3 2\n1 1\n1 2\n2 3\n3 1", "output": "-1" }, { "input": "3 9\n2 3\n1 3\n3 1\n1 1\n3 3\n2 1\n2 2\n1 2\n3 2", "output": "9" }, { "input": "4 16\n1 3\n4 4\n4 1\n2 3\n3 1\n3 2\n1 4\n2 2\n1 2\n3 3\n2 1\n1 1\n4 2\n2 4\n4 3\n3 4", "output": "12" }, { "input": "4 12\n2 2\n1 1\n3 3\n3 4\n1 2\n1 3\n1 4\n2 1\n3 2\n2 3\n3 1\n4 1", "output": "11" }, { "input": "5 20\n2 3\n1 3\n5 1\n1 2\n3 3\n5 4\n5 5\n1 5\n1 4\n4 5\n2 5\n5 2\n4 3\n3 2\n1 1\n2 4\n3 5\n2 2\n3 4\n5 3", "output": "19" }, { "input": "10 60\n6 7\n2 4\n3 6\n1 4\n8 7\n2 8\n5 7\n6 4\n5 10\n1 7\n3 9\n3 4\n9 2\n7 1\n3 8\n10 7\n9 7\n9 1\n5 5\n4 7\n5 8\n4 2\n2 2\n9 4\n3 3\n7 5\n7 4\n7 7\n8 2\n8 1\n4 5\n1 10\n9 6\n3 1\n1 3\n3 2\n10 10\n4 6\n5 4\n7 3\n10 1\n3 7\n5 1\n10 9\n4 10\n6 10\n7 10\n5 9\n5 6\n1 2\n7 8\n3 5\n9 8\n9 5\n8 10\n4 3\n10 6\n9 10\n5 3\n2 7", "output": "52" }, { "input": "2 4\n2 1\n1 2\n1 1\n2 2", "output": "-1" }, { "input": "2 1\n1 1", "output": "-1" }, { "input": "1 1\n1 1", "output": "-1" }, { "input": "10 50\n9 7\n4 8\n8 9\n1 6\n6 3\n3 1\n5 10\n7 2\n8 4\n1 9\n5 5\n4 9\n3 5\n6 7\n1 4\n10 10\n5 7\n1 1\n4 10\n6 2\n3 9\n4 3\n7 8\n5 9\n2 7\n2 10\n3 10\n1 10\n6 9\n7 5\n10 1\n3 8\n3 6\n2 6\n10 9\n8 6\n4 7\n10 7\n6 6\n8 10\n9 3\n10 2\n9 2\n10 5\n8 5\n5 6\n10 6\n7 10\n8 2\n8 8", "output": "-1" }, { "input": "50 20\n29 33\n25 9\n34 40\n46 16\n39 8\n49 36\n18 47\n41 29\n48 31\n38 20\n49 3\n28 30\n4 27\n25 38\n4 38\n8 34\n10 8\n22 14\n35 13\n17 46", "output": "-1" }, { "input": "1000 1\n542 374", "output": "-1" }, { "input": "50 18\n20 20\n20 21\n20 22\n21 20\n21 21\n21 22\n22 20\n22 21\n22 22\n1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n3 3", "output": "9" }, { "input": "1000 10\n1000 1000\n1000 999\n1000 998\n999 1000\n999 999\n999 998\n998 1000\n998 999\n998 998\n1 1", "output": "9" }, { "input": "500 9\n50 51\n50 52\n50 53\n52 53\n51 51\n51 52\n51 53\n52 51\n52 52", "output": "9" } ]
1,964
4,812,800
3
15,527
195
Try and Catch
[ "expression parsing", "implementation" ]
null
null
Vasya is developing his own programming language VPL (Vasya Programming Language). Right now he is busy making the system of exceptions. He thinks that the system of exceptions must function like that. The exceptions are processed by try-catch-blocks. There are two operators that work with the blocks: 1. The try operator. It opens a new try-catch-block. 1. The catch(&lt;exception_type&gt;, &lt;message&gt;) operator. It closes the try-catch-block that was started last and haven't yet been closed. This block can be activated only via exception of type &lt;exception_type&gt;. When we activate this block, the screen displays the &lt;message&gt;. If at the given moment there is no open try-catch-block, then we can't use the catch operator. The exceptions can occur in the program in only one case: when we use the throw operator. The throw(&lt;exception_type&gt;) operator creates the exception of the given type. Let's suggest that as a result of using some throw operator the program created an exception of type *a*. In this case a try-catch-block is activated, such that this block's try operator was described in the program earlier than the used throw operator. Also, this block's catch operator was given an exception type *a* as a parameter and this block's catch operator is described later that the used throw operator. If there are several such try-catch-blocks, then the system activates the block whose catch operator occurs earlier than others. If no try-catch-block was activated, then the screen displays message "Unhandled Exception". To test the system, Vasya wrote a program that contains only try, catch and throw operators, one line contains no more than one operator, the whole program contains exactly one throw operator. Your task is: given a program in VPL, determine, what message will be displayed on the screen.
The first line contains a single integer: *n* (1<=≀<=*n*<=≀<=105) the number of lines in the program. Next *n* lines contain the program in language VPL. Each line contains no more than one operator. It means that input file can contain empty lines and lines, consisting only of spaces. The program contains only operators try, catch and throw. It is guaranteed that the program is correct. It means that each started try-catch-block was closed, the catch operators aren't used unless there is an open try-catch-block. The program has exactly one throw operator. The program may have spaces at the beginning of a line, at the end of a line, before and after a bracket, a comma or a quote mark. The exception type is a nonempty string, that consists only of upper and lower case english letters. The length of the string does not exceed 20 symbols. Message is a nonempty string, that consists only of upper and lower case english letters, digits and spaces. Message is surrounded with quote marks. Quote marks shouldn't be printed. The length of the string does not exceed 20 symbols. Length of any line in the input file does not exceed 50 symbols.
Print the message the screen will show after the given program is executed.
[ "8\ntry\n try\n throw ( AE ) \n catch ( BE, \"BE in line 3\")\n\n try\n catch(AE, \"AE in line 5\") \ncatch(AE,\"AE somewhere\")\n", "8\ntry\n try\n throw ( AE ) \n catch ( AE, \"AE in line 3\")\n\n try\n catch(BE, \"BE in line 5\") \ncatch(AE,\"AE somewhere\")\n", "8\ntry\n try\n throw ( CE ) \n catch ( BE, \"BE in line 3\")\n\n try\n catch(AE, \"AE in line 5\") \ncatch(AE,\"AE somewhere\")\n" ]
[ "AE somewhere\n", "AE in line 3\n", "Unhandled Exception\n" ]
In the first sample there are 2 try-catch-blocks such that try operator is described earlier than throw operator and catch operator is described later than throw operator: try-catch(BE,"BE in line 3") and try-catch(AE,"AE somewhere"). Exception type is AE, so the second block will be activated, because operator catch(AE,"AE somewhere") has exception type AE as parameter and operator catch(BE,"BE in line 3") has exception type BE. In the second sample there are 2 try-catch-blocks such that try operator is described earlier than throw operator and catch operator is described later than throw operator: try-catch(AE,"AE in line 3") and try-catch(AE,"AE somewhere"). Exception type is AE, so both blocks can be activated, but only the first one will be activated, because operator catch(AE,"AE in line 3") is described earlier than catch(AE,"AE somewhere") In the third sample there is no blocks that can be activated by an exception of type CE.
[ { "input": "8\ntry\n try\n throw ( AE ) \n catch ( BE, \"BE in line 3\")\n\n try\n catch(AE, \"AE in line 5\") \ncatch(AE,\"AE somewhere\")", "output": "AE somewhere" }, { "input": "8\ntry\n try\n throw ( AE ) \n catch ( AE, \"AE in line 3\")\n\n try\n catch(BE, \"BE in line 5\") \ncatch(AE,\"AE somewhere\")", "output": "AE in line 3" }, { "input": "8\ntry\n try\n throw ( CE ) \n catch ( BE, \"BE in line 3\")\n\n try\n catch(AE, \"AE in line 5\") \ncatch(AE,\"AE somewhere\")", "output": "Unhandled Exception" }, { "input": "3\ntry\nthrow(A)\ncatch(A, \"A cought\")", "output": "A cought" }, { "input": "5\n try \n try \n catch ( gnAEZNTt, \"i5 tAC8ktUdeX\") \n throw( gnAEZNTt ) \ncatch ( gnAEZNTt, \"g1cN\" ) ", "output": "g1cN" }, { "input": "5\n try \n catch(UqWpIpGKiMqFnKox , \"bp9h8dfeNLhk9Wea\" ) \nthrow ( uaBRmgAAQyWTCzaaQMlZ ) \n try \ncatch( UqWpIpGKiMqFnKox,\"0OvVhsVWzDyqwo\" )", "output": "Unhandled Exception" }, { "input": "5\n throw ( ouB ) \n try \ncatch(ouB, \"bTJZV\" )\n try \ncatch( ouB , \"DUniE dDhpiN\") ", "output": "Unhandled Exception" }, { "input": "5\ntry \n throw( egdCZzrKRLBcqDl )\n catch ( egdCZzrKRLBcqDl ,\"o\" )\n try \n catch (egdCZzrKRLBcqDl , \"oM62EJIirV D0\" ) ", "output": "o" }, { "input": "10\n \n\n \n\nthrow (ProgramException)\n \n \n\n\n ", "output": "Unhandled Exception" }, { "input": "21\n try \n try \n try \n try \n try \n try \n try \n try \n try \n try \n throw( qtSMze) \ncatch(LY,\"x3 j\")\ncatch(hgSAFgbMGx,\"moByu\")\ncatch(LmydVQgv,\"hbZl\")\ncatch(oK,\"B6OZx qy\")\ncatch(rrtnRQB,\"7VFkQMv\")\ncatch(CASqQXaz,\"d9oci1Kx\")\ncatch(CTCzsdD,\"u\")\ncatch(xqqMxbEs,\"Mdu\")\ncatch(sOWgTPbRp,\"fVH6\")\ncatch(qtSMze,\"ZRnNzz\")", "output": "ZRnNzz" }, { "input": "3\ntry\nthrow ( X )\ncatch ( X, \"try again\")", "output": "try again" }, { "input": "3\ntry\nthrow ( try )\ncatch ( try, \"try again\")", "output": "try again" }, { "input": "3\ntry\nthrow(tryC)\ncatch(tryC, \"bad boy\")", "output": "bad boy" }, { "input": "7\ntry\ncatch(A,\"try A\")\ntry\n throw(A)\ncatch(A,\"try B\")\ntry\ncatch(A,\"try C\")", "output": "try B" }, { "input": "3\ntry\n throw(try)\ncatch(try,\"haha\")", "output": "haha" }, { "input": "3\ntry\n throw(try)\ncatch(try,\"asd\")", "output": "asd" }, { "input": "11\ntry\n try\n catch (B, \"b\")\n \n try\n throw ( U )\n catch (U, \"try\")\n \n try\n catch (C, \"c\")\ncatch (A, \"a\")", "output": "try" } ]
1,278
10,240,000
0
15,549
792
Paths in a Complete Binary Tree
[ "bitmasks", "trees" ]
null
null
*T* is a complete binary tree consisting of *n* vertices. It means that exactly one vertex is a root, and each vertex is either a leaf (and doesn't have children) or an inner node (and has exactly two children). All leaves of a complete binary tree have the same depth (distance from the root). So *n* is a number such that *n*<=+<=1 is a power of 2. In the picture you can see a complete binary tree with *n*<==<=15. Vertices are numbered from 1 to *n* in a special recursive way: we recursively assign numbers to all vertices from the left subtree (if current vertex is not a leaf), then assign a number to the current vertex, and then recursively assign numbers to all vertices from the right subtree (if it exists). In the picture vertices are numbered exactly using this algorithm. It is clear that for each size of a complete binary tree exists exactly one way to give numbers to all vertices. This way of numbering is called symmetric. You have to write a program that for given *n* answers *q* queries to the tree. Each query consists of an integer number *u**i* (1<=≀<=*u**i*<=≀<=*n*) and a string *s**i*, where *u**i* is the number of vertex, and *s**i* represents the path starting from this vertex. String *s**i* doesn't contain any characters other than 'L', 'R' and 'U', which mean traverse to the left child, to the right child and to the parent, respectively. Characters from *s**i* have to be processed from left to right, considering that *u**i* is the vertex where the path starts. If it's impossible to process a character (for example, to go to the left child of a leaf), then you have to skip it. The answer is the number of vertex where the path represented by *s**i* ends. For example, if *u**i*<==<=4 and *s**i*<==<=Β«UURLΒ», then the answer is 10.
The first line contains two integer numbers *n* and *q* (1<=≀<=*n*<=≀<=1018, *q*<=β‰₯<=1). *n* is such that *n*<=+<=1 is a power of 2. The next 2*q* lines represent queries; each query consists of two consecutive lines. The first of these two lines contains *u**i* (1<=≀<=*u**i*<=≀<=*n*), the second contains non-empty string *s**i*. *s**i* doesn't contain any characters other than 'L', 'R' and 'U'. It is guaranteed that the sum of lengths of *s**i* (for each *i* such that 1<=≀<=*i*<=≀<=*q*) doesn't exceed 105.
Print *q* numbers, *i*-th number must be the answer to the *i*-th query.
[ "15 2\n4\nUURL\n8\nLRLLLLLLLL\n" ]
[ "10\n5\n" ]
none
[ { "input": "15 2\n4\nUURL\n8\nLRLLLLLLLL", "output": "10\n5" }, { "input": "1 1\n1\nL", "output": "1" }, { "input": "1 1\n1\nR", "output": "1" }, { "input": "1 1\n1\nU", "output": "1" }, { "input": "1 10\n1\nURLRLULUR\n1\nLRRRURULULL\n1\nLURURRUUUU\n1\nRRULLLRRUL\n1\nUULLUURL\n1\nRLRRULUL\n1\nLURRLRUULRR\n1\nLULLULUUUL\n1\nURULLULL\n1\nLRRLRUUUURRLRRL", "output": "1\n1\n1\n1\n1\n1\n1\n1\n1\n1" }, { "input": "3 10\n2\nRUUUULULULUU\n1\nULLLURLU\n3\nLLURLULU\n2\nRRLURLURLLR\n3\nLRURURLRLLL\n3\nLRLULRRUURURRL\n1\nRULLR\n2\nLRULLURUL\n3\nRLL\n1\nULRUULURLULLLLLLRLL", "output": "2\n2\n2\n3\n3\n3\n1\n1\n3\n1" }, { "input": "7 10\n3\nLLULULLLR\n4\nLUUURLLLUURRU\n1\nULURR\n6\nLURLLLLRLR\n2\nULULURU\n7\nRRUUUURLRLR\n7\nUUURRULRRLUL\n7\nULLLRUULULR\n6\nUURRLL\n6\nRULUUULLRLLLUULL", "output": "1\n6\n3\n7\n4\n5\n5\n3\n7\n1" }, { "input": "15 10\n1\nURUUUR\n15\nRRLLURRRURL\n1\nUURRLUR\n13\nLRUULUURLRRRL\n6\nLULUURULUURL\n15\nUULL\n8\nULLLULRLRUU\n8\nULRUULRUURLLRL\n5\nLLUULRLURRRULLR\n10\nLUULLRU", "output": "12\n15\n7\n11\n10\n9\n4\n9\n1\n10" }, { "input": "31 10\n12\nRRRRRRULUURR\n9\nLUUURULLLLU\n24\nLLRRLURLLU\n25\nLLUUURL\n14\nRRRRRRULU\n11\nLRLUULRUULR\n10\nULULU\n30\nLLRLLLLRULRLL\n1\nRRULRLRLLLRULR\n20\nULLULLRR", "output": "15\n2\n18\n26\n14\n11\n12\n29\n1\n17" }, { "input": "4503599627370495 1\n2251799813685248\nLLLLLLLL", "output": "8796093022208" }, { "input": "4503599627370495 1\n2251799813685248\nLLLLLL", "output": "35184372088832" }, { "input": "576460752303423487 1\n1125899906842624\nR", "output": "1688849860263936" }, { "input": "1125899906842623 1\n1\nUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUULLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRLULLLLLLLLLLLLLLLLUUUULRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU", "output": "2147483648" } ]
108
409,600
0
15,550
845
Shortest Path Problem?
[ "dfs and similar", "graphs", "math" ]
null
null
You are given an undirected graph with weighted edges. The length of some path between two vertices is the bitwise xor of weights of all edges belonging to this path (if some edge is traversed more than once, then it is included in bitwise xor the same number of times). You have to find the minimum length of path between vertex 1 and vertex *n*. Note that graph can contain multiple edges and loops. It is guaranteed that the graph is connected.
The first line contains two numbers *n* and *m* (1<=≀<=*n*<=≀<=100000, *n*<=-<=1<=≀<=*m*<=≀<=100000) β€” the number of vertices and the number of edges, respectively. Then *m* lines follow, each line containing three integer numbers *x*, *y* and *w* (1<=≀<=*x*,<=*y*<=≀<=*n*, 0<=≀<=*w*<=≀<=108). These numbers denote an edge that connects vertices *x* and *y* and has weight *w*.
Print one number β€” the minimum length of path between vertices 1 and *n*.
[ "3 3\n1 2 3\n1 3 2\n3 2 0\n", "2 2\n1 1 3\n1 2 3\n" ]
[ "2\n", "0\n" ]
none
[ { "input": "3 3\n1 2 3\n1 3 2\n3 2 0", "output": "2" }, { "input": "2 2\n1 1 3\n1 2 3", "output": "0" }, { "input": "10 20\n8 5 64\n5 6 48\n4 5 91\n10 1 2\n3 4 51\n8 2 74\n6 1 98\n3 10 24\n2 10 35\n8 7 52\n10 5 72\n5 9 25\n2 9 65\n7 4 69\n5 7 26\n7 2 44\n6 8 61\n3 5 43\n10 7 33\n4 2 28", "output": "0" }, { "input": "10 20\n1 8 2\n2 9 94\n9 5 43\n7 2 83\n9 7 42\n5 10 11\n3 10 48\n8 6 31\n3 4 57\n9 3 79\n1 10 50\n6 3 19\n10 4 88\n4 5 69\n10 2 67\n1 9 62\n7 3 50\n1 5 40\n7 1 7\n8 4 87", "output": "0" }, { "input": "10 20\n2 4 76\n10 2 74\n6 4 41\n7 4 97\n8 5 15\n5 2 96\n7 6 77\n5 4 81\n10 1 31\n10 8 76\n9 5 81\n9 1 15\n8 3 88\n8 6 11\n1 6 27\n8 1 64\n3 5 25\n3 2 82\n7 10 0\n7 8 81", "output": "0" }, { "input": "10 20\n8 7 47\n1 8 34\n4 3 5\n3 9 68\n2 4 32\n8 10 98\n2 8 26\n5 3 54\n1 10 87\n2 10 34\n1 6 59\n10 5 4\n7 9 92\n1 3 100\n1 9 93\n6 10 66\n5 2 96\n8 3 70\n10 7 76\n3 6 9", "output": "0" }, { "input": "10 20\n2 8 51\n3 6 100\n4 3 35\n8 3 24\n7 3 37\n6 4 88\n9 3 45\n4 2 31\n2 10 74\n8 9 82\n5 1 65\n9 7 99\n4 8 85\n10 4 35\n6 5 27\n3 1 90\n10 3 98\n9 2 31\n10 1 84\n2 6 40", "output": "32" }, { "input": "5 10\n4 3 46005614\n4 5 62128223\n2 4 71808751\n5 2 20502511\n3 1 35666877\n3 2 99467415\n1 5 51782033\n4 1 28580231\n2 1 63077178\n5 3 73136755", "output": "109191" }, { "input": "5 10\n1 2 16759116\n2 5 19640410\n2 4 48227415\n3 2 88131000\n4 3 61768652\n5 4 51038983\n3 1 44573858\n1 5 4761704\n5 3 58408181\n4 1 29550431", "output": "4761704" }, { "input": "5 10\n4 2 28522519\n3 2 98499207\n4 5 86578634\n2 5 26599094\n3 1 78655801\n4 3 84953325\n1 4 401542\n1 5 98019109\n3 5 47552118\n2 1 26653143", "output": "225121" }, { "input": "5 10\n1 3 84521173\n5 4 97049395\n2 4 22151289\n2 3 83366529\n3 5 68115469\n5 2 19016539\n1 5 17960630\n1 4 85715490\n4 3 25542828\n1 2 31509936", "output": "8395111" }, { "input": "5 10\n4 3 25072245\n3 1 10353707\n2 1 56113542\n1 4 20590207\n2 5 44508617\n1 5 51805736\n2 3 20944097\n5 4 59876083\n3 5 95606567\n2 4 13449544", "output": "303677" }, { "input": "5 5\n1 3 82444502\n2 5 78235625\n4 5 92241123\n2 1 59508641\n5 3 32867588", "output": "85951954" }, { "input": "5 5\n2 4 92607588\n3 1 16534574\n4 5 50020317\n1 5 58305543\n4 1 79306256", "output": "58305543" }, { "input": "5 5\n3 4 29299279\n3 2 87057102\n1 4 93869273\n1 5 24026203\n2 4 7332950", "output": "24026203" }, { "input": "5 5\n3 1 72993047\n2 5 52852374\n5 3 75435307\n1 4 63553778\n5 1 9977754", "output": "2540604" }, { "input": "5 5\n4 2 42136377\n3 5 92147973\n2 5 88704873\n5 4 43226211\n1 3 19760023", "output": "17527457" } ]
545
19,968,000
-1
15,577
999
Equalize the Remainders
[ "data structures", "greedy", "implementation" ]
null
null
You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$. In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$. Let's calculate $c_r$ ($0 \le r \le m-1)$ β€” the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder. Your task is to change the array in such a way that $c_0 = c_1 = \dots = c_{m-1} = \frac{n}{m}$. Find the minimum number of moves to satisfy the above requirement.
The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$. The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array.
In the first line, print a single integer β€” the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$. In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$.
[ "6 3\n3 2 0 6 10 12\n", "4 2\n0 1 2 3\n" ]
[ "3\n3 2 0 7 10 14 \n", "0\n0 1 2 3 \n" ]
none
[ { "input": "6 3\n3 2 0 6 10 12", "output": "3\n3 2 0 7 10 14 " }, { "input": "4 2\n0 1 2 3", "output": "0\n0 1 2 3 " }, { "input": "1 1\n1000000000", "output": "0\n1000000000 " }, { "input": "6 3\n3 2 0 6 10 11", "output": "1\n3 2 0 7 10 11 " }, { "input": "100 25\n6745 2075 7499 7517 1776 5164 2335 2745 4465 1457 7565 2232 2486 9025 8059 9646 8017 7662 9690 3352 2306 366 7422 1073 7169 8966 4506 8225 5614 8628 2908 7452 9625 9332 7097 353 1043 8118 5794 4486 626 971 6731 6618 887 6354 4814 7307 7681 6160 9351 2579 411 3436 5570 2812 2726 4433 3220 577 5891 3861 528 2183 127 5579 6979 4005 9953 5038 9937 4792 3003 9417 8796 1565 11 2596 2486 3494 4464 9568 5512 5565 9822 9820 4848 2889 9527 2249 9860 8236 256 8434 8038 6407 5570 5922 7435 2815", "output": "88\n6745 2075 7499 7517 1776 5164 2335 2745 4465 1457 7565 2232 2486 9025 8059 9646 8017 7662 9690 3352 2306 366 7422 1073 7169 8966 4506 8225 5614 8628 2908 7452 9625 9332 7097 353 1043 8118 5794 4486 626 971 6731 6618 887 6354 4814 7307 7681 6160 9351 2579 411 3436 5570 2812 2726 4433 3220 577 5891 3863 528 2183 127 5579 6979 4005 9953 5038 9937 4792 3005 9417 8796 1565 24 2596 2505 3494 4464 9568 5513 5566 9822 9823 4848 2899 9530 2249 9860 8259 259 8434 8038 6408 5573 5922 7435 2819 " } ]
3,000
40,448,000
0
15,593
730
Car Repair Shop
[ "implementation" ]
null
null
Polycarp starts his own business. Tomorrow will be the first working day of his car repair shop. For now the car repair shop is very small and only one car can be repaired at a given time. Polycarp is good at marketing, so he has already collected *n* requests from clients. The requests are numbered from 1 to *n* in order they came. The *i*-th request is characterized by two values: *s**i* β€” the day when a client wants to start the repair of his car, *d**i* β€” duration (in days) to repair the car. The days are enumerated from 1, the first day is tomorrow, the second day is the day after tomorrow and so on. Polycarp is making schedule by processing requests in the order from the first to the *n*-th request. He schedules the *i*-th request as follows: - If the car repair shop is idle for *d**i* days starting from *s**i* (*s**i*,<=*s**i*<=+<=1,<=...,<=*s**i*<=+<=*d**i*<=-<=1), then these days are used to repair a car of the *i*-th client. - Otherwise, Polycarp finds the first day *x* (from 1 and further) that there are *d**i* subsequent days when no repair is scheduled starting from *x*. In other words he chooses the smallest positive *x* that all days *x*,<=*x*<=+<=1,<=...,<=*x*<=+<=*d**i*<=-<=1 are not scheduled for repair of any car. So, the car of the *i*-th client will be repaired in the range [*x*,<=*x*<=+<=*d**i*<=-<=1]. It is possible that the day *x* when repair is scheduled to start will be less than *s**i*. Given *n* requests, you are asked to help Polycarp schedule all of them according to the rules above.
The first line contains integer *n* (1<=≀<=*n*<=≀<=200) β€” the number of requests from clients. The following *n* lines contain requests, one request per line. The *i*-th request is given as the pair of integers *s**i*,<=*d**i* (1<=≀<=*s**i*<=≀<=109, 1<=≀<=*d**i*<=≀<=5Β·106), where *s**i* is the preferred time to start repairing the *i*-th car, *d**i* is the number of days to repair the *i*-th car. The requests should be processed in the order they are given in the input.
Print *n* lines. The *i*-th line should contain two integers β€” the start day to repair the *i*-th car and the finish day to repair the *i*-th car.
[ "3\n9 2\n7 3\n2 4\n", "4\n1000000000 1000000\n1000000000 1000000\n100000000 1000000\n1000000000 1000000\n" ]
[ "9 10\n1 3\n4 7\n", "1000000000 1000999999\n1 1000000\n100000000 100999999\n1000001 2000000\n" ]
none
[ { "input": "3\n9 2\n7 3\n2 4", "output": "9 10\n1 3\n4 7" }, { "input": "4\n1000000000 1000000\n1000000000 1000000\n100000000 1000000\n1000000000 1000000", "output": "1000000000 1000999999\n1 1000000\n100000000 100999999\n1000001 2000000" }, { "input": "1\n1 1", "output": "1 1" }, { "input": "1\n1000000000 1", "output": "1000000000 1000000000" }, { "input": "1\n1000000000 5000000", "output": "1000000000 1004999999" }, { "input": "5\n6 2\n10 1\n10 2\n9 2\n5 1", "output": "6 7\n10 10\n1 2\n3 4\n5 5" }, { "input": "10\n1 3\n77 8\n46 5\n83 4\n61 7\n8 4\n54 7\n80 7\n33 7\n13 4", "output": "1 3\n77 84\n46 50\n4 7\n61 67\n8 11\n54 60\n12 18\n33 39\n19 22" }, { "input": "10\n588 12\n560 10\n593 14\n438 15\n761 11\n984 6\n503 2\n855 19\n538 2\n650 7", "output": "588 599\n560 569\n1 14\n438 452\n761 771\n984 989\n503 504\n855 873\n538 539\n650 656" }, { "input": "20\n360 26\n475 17\n826 12\n815 23\n567 28\n897 26\n707 20\n1000 9\n576 5\n16 5\n714 16\n630 17\n426 26\n406 23\n899 25\n102 22\n896 8\n320 27\n964 25\n932 18", "output": "360 385\n475 491\n826 837\n1 23\n567 594\n897 922\n707 726\n1000 1008\n24 28\n29 33\n34 49\n630 646\n426 451\n50 72\n73 97\n102 123\n124 131\n320 346\n964 988\n932 949" }, { "input": "30\n522692116 84\n589719489 488\n662495181 961\n915956552 470\n683572975 271\n498400137 480\n327010963 181\n200704287 367\n810826488 54\n978100746 208\n345455616 986\n106372142 876\n446972337 42\n309349333 200\n93462198 543\n167946793 318\n325598940 427\n121873339 459\n174934933 598\n279521023 655\n739750520 3\n870850765 192\n622303167 400\n471234786 63\n805952711 18\n349834333 857\n804873364 302\n512746562 39\n533285962 561\n996718586 494", "output": "522692116 522692199\n589719489 589719976\n662495181 662496141\n915956552 915957021\n683572975 683573245\n498400137 498400616\n327010963 327011143\n200704287 200704653\n810826488 810826541\n978100746 978100953\n345455616 345456601\n106372142 106373017\n446972337 446972378\n309349333 309349532\n93462198 93462740\n167946793 167947110\n325598940 325599366\n121873339 121873797\n174934933 174935530\n279521023 279521677\n739750520 739750522\n870850765 870850956\n622303167 622303566\n471234786 471234848\n805952711..." }, { "input": "2\n10 3\n9 2", "output": "10 12\n1 2" }, { "input": "1\n1 5000000", "output": "1 5000000" } ]
0
0
-1
15,604
439
Devu and his Brother
[ "binary search", "sortings", "ternary search", "two pointers" ]
null
null
Devu and his brother love each other a lot. As they are super geeks, they only like to play with arrays. They are given two arrays *a* and *b* by their father. The array *a* is given to Devu and *b* to his brother. As Devu is really a naughty kid, he wants the minimum value of his array *a* should be at least as much as the maximum value of his brother's array *b*. Now you have to help Devu in achieving this condition. You can perform multiple operations on the arrays. In a single operation, you are allowed to decrease or increase any element of any of the arrays by 1. Note that you are allowed to apply the operation on any index of the array multiple times. You need to find minimum number of operations required to satisfy Devu's condition so that the brothers can play peacefully without fighting.
The first line contains two space-separated integers *n*, *m* (1<=≀<=*n*,<=*m*<=≀<=105). The second line will contain *n* space-separated integers representing content of the array *a* (1<=≀<=*a**i*<=≀<=109). The third line will contain *m* space-separated integers representing content of the array *b* (1<=≀<=*b**i*<=≀<=109).
You need to output a single integer representing the minimum number of operations needed to satisfy Devu's condition.
[ "2 2\n2 3\n3 5\n", "3 2\n1 2 3\n3 4\n", "3 2\n4 5 6\n1 2\n" ]
[ "3\n", "4\n", "0\n" ]
In example 1, you can increase *a*<sub class="lower-index">1</sub> by 1 and decrease *b*<sub class="lower-index">2</sub> by 1 and then again decrease *b*<sub class="lower-index">2</sub> by 1. Now array *a* will be [3; 3] and array *b* will also be [3; 3]. Here minimum element of *a* is at least as large as maximum element of *b*. So minimum number of operations needed to satisfy Devu's condition are 3. In example 3, you don't need to do any operation, Devu's condition is already satisfied.
[ { "input": "2 2\n2 3\n3 5", "output": "3" }, { "input": "3 2\n1 2 3\n3 4", "output": "4" }, { "input": "3 2\n4 5 6\n1 2", "output": "0" }, { "input": "10 10\n23 100 38 38 73 54 59 69 44 86\n100 100 100 100 100 100 100 100 100 100", "output": "416" }, { "input": "1 1\n401114\n998223974", "output": "997822860" }, { "input": "1 1\n100\n4", "output": "0" }, { "input": "1 1\n100\n183299", "output": "183199" }, { "input": "1 1\n999999999\n1000000000", "output": "1" }, { "input": "1 1\n1000000000\n1000000000", "output": "0" }, { "input": "1 1\n1\n2", "output": "1" }, { "input": "1 1\n1\n1", "output": "0" }, { "input": "1 1\n2\n1", "output": "0" }, { "input": "1 1\n1\n2", "output": "1" }, { "input": "1 1\n1\n3", "output": "2" }, { "input": "1 2\n1\n2 2", "output": "1" }, { "input": "2 1\n2 2\n3", "output": "1" } ]
171
10,444,800
0
15,626
0
none
[ "none" ]
null
null
Iahub and Iahubina went to a picnic in a forest full of trees. Less than 5 minutes passed before Iahub remembered of trees from programming. Moreover, he invented a new problem and Iahubina has to solve it, otherwise Iahub won't give her the food. Iahub asks Iahubina: can you build a rooted tree, such that - each internal node (a node with at least one son) has at least two sons; - node *i* has *c**i* nodes in its subtree? Iahubina has to guess the tree. Being a smart girl, she realized that it's possible no tree can follow Iahub's restrictions. In this way, Iahub will eat all the food. You need to help Iahubina: determine if there's at least one tree following Iahub's restrictions. The required tree must contain *n* nodes.
The first line of the input contains integer *n* (1<=≀<=*n*<=≀<=24). Next line contains *n* positive integers: the *i*-th number represents *c**i* (1<=≀<=*c**i*<=≀<=*n*).
Output on the first line "YES" (without quotes) if there exist at least one tree following Iahub's restrictions, otherwise output "NO" (without quotes).
[ "4\n1 1 1 4\n", "5\n1 1 5 2 1\n" ]
[ "YES", "NO" ]
none
[]
61
7,372,800
0
15,645
0
none
[ "none" ]
null
null
After years of hard work scientists invented an absolutely new e-reader display. The new display has a larger resolution, consumes less energy and its production is cheaper. And besides, one can bend it. The only inconvenience is highly unusual management. For that very reason the developers decided to leave the e-readers' software to programmers. The display is represented by *n*<=Γ—<=*n* square of pixels, each of which can be either black or white. The display rows are numbered with integers from 1 to *n* upside down, the columns are numbered with integers from 1 to *n* from the left to the right. The display can perform commands like "*x*,<=*y*". When a traditional display fulfills such command, it simply inverts a color of (*x*,<=*y*), where *x* is the row number and *y* is the column number. But in our new display every pixel that belongs to at least one of the segments (*x*,<=*x*)<=-<=(*x*,<=*y*) and (*y*,<=*y*)<=-<=(*x*,<=*y*) (both ends of both segments are included) inverts a color. For example, if initially a display 5<=Γ—<=5 in size is absolutely white, then the sequence of commands (1,<=4), (3,<=5), (5,<=1), (3,<=3) leads to the following changes: You are an e-reader software programmer and you should calculate minimal number of commands needed to display the picture. You can regard all display pixels as initially white.
The first line contains number *n* (1<=≀<=*n*<=≀<=2000). Next *n* lines contain *n* characters each: the description of the picture that needs to be shown. "0" represents the white color and "1" represents the black color.
Print one integer *z* β€” the least number of commands needed to display the picture.
[ "5\n01110\n10010\n10001\n10011\n11110\n" ]
[ "4\n" ]
none
[ { "input": "5\n01110\n10010\n10001\n10011\n11110", "output": "4" }, { "input": "4\n0000\n0111\n0001\n0001", "output": "1" }, { "input": "6\n100000\n010000\n001000\n000100\n000000\n000001", "output": "5" }, { "input": "10\n0000000000\n0000110000\n1001000000\n1000011110\n1011111101\n1011110011\n1011000111\n1011000001\n1111000010\n0000111110", "output": "20" }, { "input": "1\n0", "output": "0" }, { "input": "1\n1", "output": "1" }, { "input": "2\n00\n00", "output": "0" }, { "input": "2\n10\n00", "output": "1" }, { "input": "2\n11\n00", "output": "2" }, { "input": "2\n11\n10", "output": "3" }, { "input": "2\n11\n11", "output": "4" }, { "input": "3\n000\n000\n000", "output": "0" }, { "input": "3\n011\n110\n001", "output": "5" }, { "input": "3\n001\n100\n101", "output": "8" }, { "input": "4\n1001\n0000\n1001\n0110", "output": "10" }, { "input": "5\n01010\n01101\n11110\n00111\n10100", "output": "22" }, { "input": "6\n110000\n000010\n001011\n011011\n100001\n111000", "output": "13" }, { "input": "7\n0000010\n0100101\n0010011\n0111111\n0100000\n0110010\n0000101", "output": "19" }, { "input": "10\n0000000000\n0000000000\n0000000000\n0000000000\n0000000000\n0000000000\n0000000000\n0000000000\n0000000000\n0000000000", "output": "0" }, { "input": "10\n1111100000\n0010100000\n0110111000\n0000001000\n1011001010\n0010100001\n0010111000\n0011001010\n0000110010\n0000001100", "output": "20" }, { "input": "10\n1100011000\n0000101000\n1001000011\n0000100010\n1101010011\n1100100101\n1000011101\n1001110011\n1110111100\n1000111100", "output": "40" }, { "input": "10\n1100111010\n1000011011\n0110000000\n1001111011\n0011011010\n1100001001\n0011010110\n1100011110\n0000101011\n1110101011", "output": "75" }, { "input": "10\n1101010101\n1110101010\n0111010101\n1011101010\n0101110101\n1010111010\n0101011101\n1010101110\n0101010111\n1010101011", "output": "100" } ]
62
0
0
15,650
28
Bath Queue
[ "combinatorics", "dp", "probabilities" ]
C. Bath Queue
2
256
There are *n* students living in the campus. Every morning all students wake up at the same time and go to wash. There are *m* rooms with wash basins. The *i*-th of these rooms contains *a**i* wash basins. Every student independently select one the rooms with equal probability and goes to it. After all students selected their rooms, students in each room divide into queues by the number of wash basins so that the size of the largest queue is the least possible. Calculate the expected value of the size of the largest queue among all rooms.
The first line contains two positive integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=50) β€” the amount of students and the amount of rooms. The second line contains *m* integers *a*1,<=*a*2,<=... ,<=*a**m* (1<=≀<=*a**i*<=≀<=50). *a**i* means the amount of wash basins in the *i*-th room.
Output single number: the expected value of the size of the largest queue. Your answer must have an absolute or relative error less than 10<=-<=9.
[ "1 1\n2\n", "2 2\n1 1\n", "2 3\n1 1 1\n", "7 5\n1 1 2 3 1\n" ]
[ "1.00000000000000000000\n", "1.50000000000000000000\n", "1.33333333333333350000\n", "2.50216960000000070000\n" ]
none
[ { "input": "1 1\n2", "output": "1.00000000000000000000" }, { "input": "2 2\n1 1", "output": "1.50000000000000000000" }, { "input": "2 3\n1 1 1", "output": "1.33333333333333350000" }, { "input": "7 5\n1 1 2 3 1", "output": "2.50216960000000070000" }, { "input": "10 4\n8 4 7 6", "output": "1.08210754394531210000" }, { "input": "5 5\n5 5 5 5 5", "output": "1.00000000000000020000" }, { "input": "7 4\n1 2 3 4", "output": "2.11712646484374910000" }, { "input": "50 50\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", "output": "3.80545467981579130000" }, { "input": "30 30\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", "output": "3.49298980907245000000" }, { "input": "20 20\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1", "output": "3.23123684379753670000" }, { "input": "1 50\n47 24 49 50 4 21 42 22 34 48 45 15 31 18 12 10 4 45 45 42 49 13 12 9 7 5 30 18 22 50 15 16 25 18 5 41 3 26 19 18 22 5 8 10 16 50 43 44 6 43", "output": "1.00000000000000000000" }, { "input": "1 50\n46 45 44 49 48 48 47 42 48 47 47 48 39 47 48 49 50 48 50 46 48 46 50 47 45 50 41 49 39 44 46 47 43 47 42 47 49 40 49 50 50 50 48 50 48 47 49 46 46 42", "output": "1.00000000000000000000" }, { "input": "1 50\n9 1 1 4 1 9 7 4 3 10 1 7 4 7 2 5 13 2 3 3 2 1 2 1 1 7 7 5 2 6 1 8 2 6 2 15 2 3 1 2 4 8 6 2 6 11 1 2 1 1", "output": "1.00000000000000000000" }, { "input": "50 1\n27", "output": "2.00000000000000000000" }, { "input": "50 1\n48", "output": "2.00000000000000000000" }, { "input": "50 1\n4", "output": "13.00000000000000000000" }, { "input": "20 35\n48 40 49 37 36 44 48 42 37 42 18 44 47 47 41 45 49 47 47 50 16 24 42 24 36 37 45 48 36 43 44 25 34 30 42", "output": "0.99999999999999978000" }, { "input": "50 50\n3 12 1 3 6 2 5 14 2 4 4 1 6 9 4 2 3 19 7 6 4 1 7 4 1 3 6 3 2 4 4 1 6 1 3 1 1 4 1 6 1 2 2 4 12 12 1 5 5 2", "output": "2.83614403586073620000" }, { "input": "50 50\n21 35 15 42 44 1 50 4 26 21 43 41 50 33 47 3 21 14 33 34 43 44 16 41 35 27 3 4 7 14 15 35 27 36 46 13 3 48 32 20 15 33 38 36 39 22 45 7 16 50", "output": "1.40898003277183290000" }, { "input": "50 50\n50 48 38 45 50 48 38 48 49 49 50 50 49 50 38 48 45 40 43 49 48 43 50 50 50 42 47 50 49 34 48 47 48 50 50 50 49 47 48 49 48 48 50 39 45 47 48 47 45 50", "output": "0.99999999999999156000" }, { "input": "50 50\n50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 49 49 49 50 49 50 50 50 50 48 50 49 50 50 50 50 48 50 50 50 49 50 50 50 50 50 50 50 50 50 50 49 50 50", "output": "0.99999999999999156000" }, { "input": "50 50\n2 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 2 1 1 1 1 2 1 1", "output": "3.71403384155135140000" }, { "input": "44 5\n40 48 45 43 17", "output": "1.00121533621041100000" }, { "input": "3 49\n4 2 9 21 22 25 6 9 15 10 37 3 8 6 14 1 3 3 18 1 9 11 8 5 20 21 10 25 35 16 14 18 2 5 12 6 9 8 3 6 19 18 1 13 12 33 4 2 16", "output": "1.00374843815077060000" }, { "input": "19 17\n50 46 38 48 41 41 40 45 47 50 49 33 46 44 46 48 36", "output": "0.99999999999999967000" }, { "input": "12 34\n47 50 49 45 48 50 49 45 50 48 43 49 50 47 49 49 50 50 45 43 45 44 50 47 50 49 47 49 49 42 50 50 50 49", "output": "1.00000000000000000000" }, { "input": "35 40\n12 1 4 8 1 9 1 11 1 1 8 8 16 1 6 5 3 1 6 4 6 2 4 6 2 1 1 16 2 2 3 1 1 2 2 3 8 12 1 4", "output": "2.65978492228475400000" }, { "input": "10 6\n1 1 1 1 1 1", "output": "3.44474669607021380000" }, { "input": "50 50\n50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50", "output": "0.99999999999999156000" }, { "input": "50 50\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", "output": "3.80545467981579130000" }, { "input": "50 50\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", "output": "1.44158938050050490000" }, { "input": "1 1\n50", "output": "1.00000000000000000000" } ]
1,778
9,932,800
3.536999
15,685
862
Mahmoud and Ehab and the function
[ "binary search", "data structures", "sortings" ]
null
null
Dr. Evil is interested in math and functions, so he gave Mahmoud and Ehab array *a* of length *n* and array *b* of length *m*. He introduced a function *f*(*j*) which is defined for integers *j*, which satisfy 0<=≀<=*j*<=≀<=*m*<=-<=*n*. Suppose, *c**i*<==<=*a**i*<=-<=*b**i*<=+<=*j*. Then *f*(*j*)<==<=|*c*1<=-<=*c*2<=+<=*c*3<=-<=*c*4... *c**n*|. More formally, . Dr. Evil wants Mahmoud and Ehab to calculate the minimum value of this function over all valid *j*. They found it a bit easy, so Dr. Evil made their task harder. He will give them *q* update queries. During each update they should add an integer *x**i* to all elements in *a* in range [*l**i*;*r**i*] i.e. they should add *x**i* to *a**l**i*,<=*a**l**i*<=+<=1,<=... ,<=*a**r**i* and then they should calculate the minimum value of *f*(*j*) for all valid *j*. Please help Mahmoud and Ehab.
The first line contains three integers *n*,<=*m* and *q* (1<=≀<=*n*<=≀<=*m*<=≀<=105, 1<=≀<=*q*<=≀<=105)Β β€” number of elements in *a*, number of elements in *b* and number of queries, respectively. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n*. (<=-<=109<=≀<=*a**i*<=≀<=109)Β β€” elements of *a*. The third line contains *m* integers *b*1,<=*b*2,<=...,<=*b**m*. (<=-<=109<=≀<=*b**i*<=≀<=109)Β β€” elements of *b*. Then *q* lines follow describing the queries. Each of them contains three integers *l**i* *r**i* *x**i* (1<=≀<=*l**i*<=≀<=*r**i*<=≀<=*n*, <=-<=109<=≀<=*x*<=≀<=109)Β β€” range to be updated and added value.
The first line should contain the minimum value of the function *f* before any update. Then output *q* lines, the *i*-th of them should contain the minimum value of the function *f* after performing the *i*-th update .
[ "5 6 3\n1 2 3 4 5\n1 2 3 4 5 6\n1 1 10\n1 1 -9\n1 5 -1\n" ]
[ "0\n9\n0\n0\n" ]
For the first example before any updates it's optimal to choose *j* = 0, *f*(0) = |(1 - 1) - (2 - 2) + (3 - 3) - (4 - 4) + (5 - 5)| = |0| = 0. After the first update *a* becomes {11, 2, 3, 4, 5} and it's optimal to choose *j* = 1, *f*(1) = |(11 - 2) - (2 - 3) + (3 - 4) - (4 - 5) + (5 - 6) = |9| = 9. After the second update *a* becomes {2, 2, 3, 4, 5} and it's optimal to choose *j* = 1, *f*(1) = |(2 - 2) - (2 - 3) + (3 - 4) - (4 - 5) + (5 - 6)| = |0| = 0. After the third update *a* becomes {1, 1, 2, 3, 4} and it's optimal to choose *j* = 0, *f*(0) = |(1 - 1) - (1 - 2) + (2 - 3) - (3 - 4) + (4 - 5)| = |0| = 0.
[ { "input": "5 6 3\n1 2 3 4 5\n1 2 3 4 5 6\n1 1 10\n1 1 -9\n1 5 -1", "output": "0\n9\n0\n0" }, { "input": "1 1 1\n937982044\n179683049\n1 1 821220804", "output": "758298995\n1579519799" } ]
46
5,529,600
0
15,694
156
Clues
[ "combinatorics", "graphs" ]
null
null
As Sherlock Holmes was investigating another crime, he found a certain number of clues. Also, he has already found direct links between some of those clues. The direct links between the clues are mutual. That is, the direct link between clues *A* and *B* and the direct link between clues *B* and *A* is the same thing. No more than one direct link can exist between two clues. Of course Sherlock is able to find direct links between all clues. But it will take too much time and the criminals can use this extra time to hide. To solve the crime, Sherlock needs each clue to be linked to all other clues (maybe not directly, via some other clues). Clues *A* and *B* are considered linked either if there is a direct link between them or if there is a direct link between *A* and some other clue *C* which is linked to *B*. Sherlock Holmes counted the minimum number of additional direct links that he needs to find to solve the crime. As it turns out, it equals *T*. Please count the number of different ways to find exactly *T* direct links between the clues so that the crime is solved in the end. Two ways to find direct links are considered different if there exist two clues which have a direct link in one way and do not have a direct link in the other way. As the number of different ways can turn out rather big, print it modulo *k*.
The first line contains three space-separated integers *n*,<=*m*,<=*k* (1<=≀<=*n*<=≀<=105,<=0<=≀<=*m*<=≀<=105, 1<=≀<=*k*<=≀<=109) β€” the number of clues, the number of direct clue links that Holmes has already found and the divisor for the modulo operation. Each of next *m* lines contains two integers *a* and *b* (1<=≀<=*a*,<=*b*<=≀<=*n*,<=*a*<=β‰ <=*b*), that represent a direct link between clues. It is guaranteed that any two clues are linked by no more than one direct link. Note that the direct links between the clues are mutual.
Print the single number β€” the answer to the problem modulo *k*.
[ "2 0 1000000000\n", "3 0 100\n", "4 1 1000000000\n1 4\n" ]
[ "1\n", "3\n", "8\n" ]
The first sample only has two clues and Sherlock hasn't found any direct link between them yet. The only way to solve the crime is to find the link. The second sample has three clues and Sherlock hasn't found any direct links between them. He has to find two of three possible direct links between clues to solve the crime β€” there are 3 ways to do it. The third sample has four clues and the detective has already found one direct link between the first and the fourth clue. There are 8 ways to find two remaining clues to solve the crime.
[ { "input": "2 0 1000000000", "output": "1" }, { "input": "3 0 100", "output": "3" }, { "input": "4 1 1000000000\n1 4", "output": "8" }, { "input": "6 4 100000\n1 4\n4 6\n6 1\n2 5", "output": "36" }, { "input": "10 0 123456789", "output": "100000000" }, { "input": "10 5 1000000000\n1 2\n4 3\n5 6\n8 7\n10 9", "output": "32000" }, { "input": "8 4 17\n1 2\n2 3\n3 4\n4 1", "output": "8" }, { "input": "9 6 342597160\n1 2\n3 4\n4 5\n6 7\n7 8\n8 9", "output": "216" }, { "input": "1 0 1", "output": "0" }, { "input": "15 10 1\n1 2\n4 5\n6 3\n11 8\n8 5\n5 9\n9 1\n11 12\n12 1\n2 8", "output": "0" }, { "input": "8 8 999999937\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n8 7", "output": "1" }, { "input": "100000 0 1000000000", "output": "0" }, { "input": "100000 0 1", "output": "0" }, { "input": "9 11 498920381\n2 8\n5 4\n1 8\n8 3\n4 9\n3 6\n8 9\n1 7\n5 1\n5 6\n9 6", "output": "1" }, { "input": "2 0 753780649", "output": "1" }, { "input": "1 0 997185958", "output": "1" }, { "input": "10 36 279447540\n10 7\n10 8\n1 8\n1 5\n4 5\n9 5\n3 9\n7 3\n10 4\n8 9\n2 10\n6 2\n4 8\n10 3\n1 4\n10 1\n10 6\n8 3\n3 6\n9 7\n10 5\n6 9\n3 1\n8 6\n4 9\n5 3\n9 10\n7 2\n2 4\n7 4\n5 6\n5 8\n7 5\n5 2\n6 7\n1 9", "output": "1" }, { "input": "7 7 302838679\n5 3\n4 1\n5 4\n6 5\n1 6\n3 2\n6 4", "output": "6" }, { "input": "6 1 310732484\n4 2", "output": "432" }, { "input": "10 7 587143295\n1 10\n7 1\n1 8\n7 10\n8 10\n4 8\n6 8", "output": "6000" }, { "input": "3 3 975373207\n1 2\n1 3\n3 2", "output": "1" }, { "input": "9 33 321578376\n9 5\n6 3\n8 4\n4 1\n3 5\n2 6\n8 2\n7 6\n7 9\n8 6\n4 5\n1 6\n1 2\n5 6\n9 4\n7 8\n3 9\n9 6\n4 7\n7 2\n1 8\n4 6\n8 3\n3 7\n8 9\n5 7\n3 4\n7 1\n9 2\n5 1\n2 5\n9 1\n3 2", "output": "1" }, { "input": "5 10 93196990\n1 5\n1 4\n4 2\n1 3\n3 4\n1 2\n5 2\n5 4\n5 3\n2 3", "output": "1" }, { "input": "1 0 773734495", "output": "1" }, { "input": "2 1 719418546\n1 2", "output": "1" }, { "input": "3 2 21502109\n3 2\n1 2", "output": "1" }, { "input": "9 35 480175322\n6 3\n8 6\n7 5\n7 9\n3 4\n2 8\n5 3\n4 5\n4 6\n7 1\n7 6\n2 5\n8 3\n6 9\n8 4\n8 5\n6 1\n8 1\n3 2\n5 1\n8 9\n3 1\n8 7\n5 6\n5 9\n4 9\n7 4\n2 7\n3 9\n2 4\n7 3\n9 1\n2 9\n1 4\n1 2", "output": "1" }, { "input": "5 7 729985252\n2 3\n3 1\n2 5\n1 5\n1 2\n1 4\n4 3", "output": "1" }, { "input": "2 1 819865995\n2 1", "output": "1" }, { "input": "10 0 766953983", "output": "100000000" }, { "input": "2 1 855341703\n2 1", "output": "1" }, { "input": "10 30 407595309\n3 6\n6 10\n6 7\n7 10\n7 8\n3 10\n3 4\n1 4\n9 10\n8 4\n3 7\n5 1\n2 4\n6 2\n8 9\n10 5\n7 5\n10 4\n5 8\n8 2\n10 2\n1 6\n4 7\n2 3\n5 6\n8 10\n3 5\n1 8\n9 7\n1 9", "output": "1" }, { "input": "1 0 21080115", "output": "1" }, { "input": "57 28 776442742\n31 10\n25 28\n51 45\n14 40\n21 52\n53 51\n52 53\n4 6\n51 35\n53 15\n17 16\n40 44\n37 51\n33 43\n55 40\n42 16\n30 8\n19 45\n7 27\n31 8\n49 8\n43 44\n45 3\n16 22\n32 36\n52 36\n5 26\n2 23", "output": "135540294" }, { "input": "30 70 288262020\n27 18\n5 19\n23 17\n16 17\n29 17\n1 22\n23 5\n10 13\n22 26\n14 3\n8 3\n29 9\n9 1\n3 9\n16 4\n9 22\n10 22\n20 1\n3 7\n23 19\n26 8\n24 1\n5 7\n28 29\n20 11\n16 12\n6 9\n24 29\n30 4\n5 26\n18 21\n5 21\n30 6\n12 13\n16 23\n28 14\n30 1\n7 27\n7 19\n27 17\n5 30\n30 27\n28 30\n12 28\n27 9\n30 26\n20 18\n21 16\n8 30\n4 26\n13 22\n2 14\n12 30\n4 2\n6 12\n29 25\n19 29\n14 15\n3 23\n10 28\n7 1\n21 10\n4 12\n1 14\n7 21\n21 8\n17 26\n7 6\n26 29\n9 8", "output": "1" }, { "input": "99 12 832839308\n66 23\n36 5\n16 57\n70 62\n94 96\n63 33\n99 23\n63 10\n6 85\n73 23\n69 46\n72 95", "output": "71450536" }, { "input": "30 18 918975816\n30 18\n23 1\n21 14\n14 8\n18 9\n23 29\n3 23\n29 19\n18 4\n27 19\n30 2\n9 10\n9 28\n16 15\n10 6\n18 12\n23 9\n19 14", "output": "782410104" }, { "input": "83 33 367711297\n14 74\n26 22\n55 19\n8 70\n6 42\n53 49\n54 56\n52 17\n62 44\n78 61\n76 4\n78 30\n51 2\n31 42\n33 67\n45 41\n64 62\n15 25\n33 35\n37 20\n38 65\n65 83\n61 14\n20 67\n62 47\n7 34\n78 41\n38 83\n26 69\n54 58\n11 62\n30 55\n15 74", "output": "131377693" }, { "input": "24 68 862907549\n6 9\n16 22\n11 23\n12 17\n18 2\n15 5\n5 22\n16 4\n21 9\n7 11\n19 16\n9 13\n21 20\n5 24\n7 12\n17 1\n24 21\n23 7\n16 17\n16 18\n10 13\n18 7\n8 21\n13 5\n10 18\n4 11\n21 6\n15 13\n2 1\n20 16\n11 16\n22 19\n2 4\n21 1\n6 18\n24 12\n21 19\n6 14\n22 24\n11 20\n2 19\n1 11\n24 18\n14 8\n10 24\n5 3\n11 3\n17 4\n4 20\n2 10\n12 11\n24 7\n23 16\n2 3\n19 24\n22 1\n22 4\n4 6\n3 4\n11 13\n6 5\n18 23\n4 23\n22 13\n20 5\n2 5\n2 11\n9 5", "output": "1" }, { "input": "86 23 608266393\n62 78\n44 84\n42 37\n20 24\n40 36\n41 76\n14 38\n80 72\n39 52\n31 58\n71 17\n81 6\n32 65\n11 69\n43 86\n85 59\n28 77\n78 64\n15 19\n36 39\n53 49\n48 75\n33 85", "output": "235915236" }, { "input": "47 51 283106191\n18 14\n30 26\n24 2\n18 41\n35 31\n16 24\n29 39\n6 12\n17 21\n7 19\n36 16\n27 39\n28 34\n22 35\n28 43\n40 5\n2 26\n18 16\n27 13\n21 6\n19 5\n35 30\n13 31\n7 10\n25 7\n44 42\n45 1\n35 47\n11 28\n47 46\n18 15\n27 16\n24 41\n10 8\n25 41\n4 40\n5 11\n24 6\n10 17\n41 38\n47 28\n8 29\n25 24\n35 37\n44 17\n24 47\n8 32\n33 11\n26 28\n23 9\n5 9", "output": "189974" }, { "input": "67 2 818380264\n4 52\n15 44", "output": "517849052" }, { "input": "10 45 220178113\n9 1\n8 1\n5 8\n1 5\n7 8\n6 7\n7 9\n6 2\n3 2\n1 4\n8 3\n8 9\n3 6\n4 5\n5 3\n10 4\n3 9\n9 6\n5 9\n2 9\n10 7\n1 10\n9 4\n3 10\n2 5\n7 1\n6 10\n6 5\n8 6\n8 4\n8 10\n1 6\n4 2\n9 10\n2 10\n7 3\n6 4\n7 5\n1 2\n4 3\n10 5\n4 7\n3 1\n7 2\n8 2", "output": "1" }, { "input": "588 32 634894588\n535 26\n562 406\n70 368\n357 513\n108 361\n515 5\n159 56\n522 81\n169 229\n312 252\n492 43\n476 405\n524 555\n537 169\n142 149\n586 112\n7 159\n76 370\n295 376\n33 455\n278 225\n377 88\n526 308\n517 303\n300 576\n230 493\n588 525\n177 312\n356 215\n515 34\n196 236\n323 9", "output": "478655040" }, { "input": "3 2 11\n1 2\n2 3", "output": "1" }, { "input": "2 1 1000\n1 2", "output": "1" }, { "input": "1 0 10000", "output": "1" }, { "input": "5 4 100000\n1 2\n2 3\n3 4\n4 5", "output": "1" }, { "input": "2 1 100000\n1 2", "output": "1" }, { "input": "1 0 10", "output": "1" }, { "input": "3 3 100\n1 2\n2 3\n3 1", "output": "1" }, { "input": "2 1 100\n1 2", "output": "1" }, { "input": "3 2 42\n1 2\n2 3", "output": "1" } ]
1,964
13,721,600
-1
15,695
257
Sum
[ "greedy", "math" ]
null
null
Vasya has found a piece of paper with an array written on it. The array consists of *n* integers *a*1,<=*a*2,<=...,<=*a**n*. Vasya noticed that the following condition holds for the array *a**i*<=≀<=*a**i*<=+<=1<=≀<=2Β·*a**i* for any positive integer *i* (*i*<=&lt;<=*n*). Vasya wants to add either a "+" or a "-" before each number of array. Thus, Vasya will get an expression consisting of *n* summands. The value of the resulting expression is the sum of all its elements. The task is to add signs "+" and "-" before each number so that the value of expression *s* meets the limits 0<=≀<=*s*<=≀<=*a*1. Print a sequence of signs "+" and "-", satisfying the given limits. It is guaranteed that the solution for the problem exists.
The first line contains integer *n* (1<=≀<=*n*<=≀<=105) β€” the size of the array. The second line contains space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=109) β€” the original array. It is guaranteed that the condition *a**i*<=≀<=*a**i*<=+<=1<=≀<=2Β·*a**i* fulfills for any positive integer *i* (*i*<=&lt;<=*n*).
In a single line print the sequence of *n* characters "+" and "-", where the *i*-th character is the sign that is placed in front of number *a**i*. The value of the resulting expression *s* must fit into the limits 0<=≀<=*s*<=≀<=*a*1. If there are multiple solutions, you are allowed to print any of them.
[ "4\n1 2 3 5\n", "3\n3 3 5\n" ]
[ "+++-", "++-" ]
none
[ { "input": "4\n1 2 3 5", "output": "+++-" }, { "input": "3\n3 3 5", "output": "++-" }, { "input": "4\n2 4 5 6", "output": "-++-" }, { "input": "6\n3 5 10 11 12 20", "output": "++-++-" }, { "input": "10\n10 14 17 22 43 72 74 84 88 93", "output": "++---++--+" }, { "input": "11\n3 6 7 11 13 16 26 52 63 97 97", "output": "++--+-++--+" }, { "input": "12\n3 3 4 7 14 26 51 65 72 72 85 92", "output": "+-+--++-+--+" }, { "input": "40\n3 3 3 6 10 10 18 19 34 66 107 150 191 286 346 661 1061 1620 2123 3679 5030 8736 10539 19659 38608 47853 53095 71391 135905 255214 384015 694921 1357571 1364832 2046644 2595866 2918203 3547173 4880025 6274651", "output": "+-++-+-+-+-++-++-+-++--++--++--+-+-+-++-" }, { "input": "41\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0", "output": "-----------------------------------------" }, { "input": "42\n2 2 2 3 6 8 14 22 37 70 128 232 330 472 473 784 1481 2008 3076 4031 7504 8070 8167 11954 17832 24889 27113 41190 48727 92327 148544 186992 247329 370301 547840 621571 868209 1158781 1725242 3027208 4788036 5166155", "output": "-++-+-++--+-+-+-+-+-+-+-+--++-+-++--+--++-" }, { "input": "43\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0", "output": "-------------------------------------------" }, { "input": "44\n4 6 8 14 28 36 43 76 78 151 184 217 228 245 469 686 932 1279 2100 2373 4006 4368 8173 10054 18409 28333 32174 53029 90283 161047 293191 479853 875055 1206876 1423386 1878171 2601579 3319570 4571631 4999760 6742654 12515994 22557290 29338426", "output": "+-+-+-+--++-+-+-++--+-+--+--+++--++--+-+-++-" }, { "input": "45\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0", "output": "---------------------------------------------" }, { "input": "46\n3 6 6 8 16 19 23 46 53 90 114 131 199 361 366 523 579 1081 1457 2843 4112 4766 7187 8511 15905 22537 39546 70064 125921 214041 324358 392931 547572 954380 1012122 1057632 1150405 1393895 1915284 1969248 2541748 4451203 8201302 10912223 17210988 24485089", "output": "-+++-+--+-++-+--++-+-+-++-++-+--++--++--++-++-" }, { "input": "47\n3 3 5 6 9 13 13 14 22 33 50 76 83 100 168 303 604 1074 1417 2667 3077 4821 5129 7355 11671 22342 24237 34014 66395 73366 105385 205561 387155 756780 965476 1424160 1624526 2701046 4747339 5448855 6467013 9133423 11001389 18298303 23824100 41393164 58364321", "output": "-++--+-+-+-+-+-+-++-+-++-+--+----++-+-+--++-++-" }, { "input": "48\n4 7 12 16 23 43 61 112 134 141 243 267 484 890 1427 1558 1653 2263 2889 3313 3730 5991 10176 18243 18685 36555 40006 62099 70557 106602 122641 125854 213236 309698 379653 713328 999577 1021356 2007207 2886237 4994645 5812125 11576387 14215887 26060277 35989707 36964781 57933366", "output": "++-++-+-++---+-+-+-+--+--+-+-+-+-+-+--+-++-+-++-" }, { "input": "1\n1000000000", "output": "+" }, { "input": "2\n5 8", "output": "-+" }, { "input": "3\n1000000000 1000000000 1000000000", "output": "++-" } ]
2,000
10,956,800
0
15,698
490
Restoring Increasing Sequence
[ "binary search", "brute force", "greedy", "implementation" ]
null
null
Peter wrote on the board a strictly increasing sequence of positive integers *a*1,<=*a*2,<=...,<=*a**n*. Then Vasil replaced some digits in the numbers of this sequence by question marks. Thus, each question mark corresponds to exactly one lost digit. Restore the the original sequence knowing digits remaining on the board.
The first line of the input contains integer *n* (1<=≀<=*n*<=≀<=105) β€” the length of the sequence. Next *n* lines contain one element of the sequence each. Each element consists only of digits and question marks. No element starts from digit 0. Each element has length from 1 to 8 characters, inclusive.
If the answer exists, print in the first line "YES" (without the quotes). Next *n* lines must contain the sequence of positive integers β€” a possible variant of Peter's sequence. The found sequence must be strictly increasing, it must be transformed from the given one by replacing each question mark by a single digit. All numbers on the resulting sequence must be written without leading zeroes. If there are multiple solutions, print any of them. If there is no answer, print a single line "NO" (without the quotes).
[ "3\n?\n18\n1?\n", "2\n??\n?\n", "5\n12224\n12??5\n12226\n?0000\n?00000\n" ]
[ "YES\n1\n18\n19\n", "NO\n", "YES\n12224\n12225\n12226\n20000\n100000\n" ]
none
[ { "input": "3\n?\n18\n1?", "output": "YES\n1\n18\n19" }, { "input": "2\n??\n?", "output": "NO" }, { "input": "5\n12224\n12??5\n12226\n?0000\n?00000", "output": "YES\n12224\n12225\n12226\n20000\n100000" }, { "input": "10\n473883\n3499005\n4?74792\n58146??\n8?90593\n9203?71\n?39055?\n1?692641\n11451902\n?22126?2", "output": "YES\n473883\n3499005\n4074792\n5814600\n8090593\n9203071\n9390550\n10692641\n11451902\n12212602" }, { "input": "8\n?\n2\n3\n4\n?\n?\n?\n9", "output": "YES\n1\n2\n3\n4\n5\n6\n7\n9" }, { "input": "98\n?\n?0\n2?\n6?\n6?\n69\n??\n??\n96\n1?2\n??3\n104\n??4\n1?9\n??2\n18?\n?01\n205\n?19\n244\n??8\n?5?\n?5?\n276\n??3\n???\n???\n?28\n?3?\n3??\n??8\n355\n4?0\n4??\n?10\n??1\n417\n4?9\n?3?\n4?4\n?61\n?8?\n???\n507\n?2?\n???\n??6\n5?7\n540\n5?9\n???\n?7?\n5??\n591\n?9?\n6?0\n620\n??4\n??1\n?35\n65?\n65?\n6?8\n6??\n68?\n7?4\n7??\n718\n?2?\n??9\n???\n7??\n?7?\n776\n7??\n788\n???\n?0?\n803\n83?\n846\n84?\n853\n85?\n87?\n?8?\n89?\n9?1\n91?\n929\n??0\n??6\n??3\n9??\n98?\n9?5\n9??\n995", "output": "YES\n1\n10\n20\n60\n61\n69\n70\n71\n96\n102\n103\n104\n114\n119\n122\n180\n201\n205\n219\n244\n248\n250\n251\n276\n283\n284\n285\n328\n330\n331\n338\n355\n400\n401\n410\n411\n417\n419\n430\n434\n461\n480\n481\n507\n520\n521\n526\n527\n540\n549\n550\n570\n571\n591\n592\n600\n620\n624\n631\n635\n650\n651\n658\n659\n680\n704\n705\n718\n720\n729\n730\n731\n770\n776\n777\n788\n789\n800\n803\n830\n846\n847\n853\n854\n870\n880\n890\n901\n910\n929\n930\n936\n943\n944\n980\n985\n986\n995" }, { "input": "10\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10", "output": "YES\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10" }, { "input": "3\n18\n19\n1?", "output": "NO" }, { "input": "3\n20\n19\n21", "output": "NO" }, { "input": "3\n19\n2?\n20", "output": "NO" }, { "input": "2\n99999999\n99999999", "output": "NO" }, { "input": "2\n140\n?40", "output": "YES\n140\n240" }, { "input": "11\n?\n?\n?\n?\n?\n?\n?\n?\n?\n?\n?", "output": "NO" }, { "input": "4\n100\n???\n999\n???", "output": "NO" }, { "input": "1\n????????", "output": "YES\n10000000" }, { "input": "2\n100\n???", "output": "YES\n100\n101" }, { "input": "2\n100\n?00", "output": "YES\n100\n200" }, { "input": "2\n?00\n100", "output": "NO" }, { "input": "3\n100\n?00\n200", "output": "NO" }, { "input": "2\n50\n5", "output": "NO" }, { "input": "3\n99999998\n????????\n99999999", "output": "NO" }, { "input": "3\n99999998\n99999999\n????????", "output": "NO" }, { "input": "3\n99999997\n99999998\n???????", "output": "NO" }, { "input": "4\n????????\n10000001\n99999998\n????????", "output": "YES\n10000000\n10000001\n99999998\n99999999" }, { "input": "2\n13300\n12?34", "output": "NO" } ]
77
2,867,200
-1
15,704
930
Game with Tokens
[ "data structures", "games", "implementation" ]
null
null
Consider the following game for two players. There is one white token and some number of black tokens. Each token is placed on a plane in a point with integer coordinates *x* and *y*. The players take turn making moves, white starts. On each turn, a player moves all tokens of their color by 1 to up, down, left or right. Black player can choose directions for each token independently. After a turn of the white player the white token can not be in a point where a black token is located. There are no other constraints on locations of the tokens: positions of black tokens can coincide, after a turn of the black player and initially the white token can be in the same point with some black point. If at some moment the white player can't make a move, he loses. If the white player makes 10100500 moves, he wins. You are to solve the following problem. You are given initial positions of all black tokens. It is guaranteed that initially all these positions are distinct. In how many places can the white token be located initially so that if both players play optimally, the black player wins?
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=105) β€” the number of black points. The (*i*<=+<=1)-th line contains two integers *x**i*, *y**i* (<=-<=105<=≀<=*x**i*,<=*y**i*,<=<=≀<=105) β€” the coordinates of the point where the *i*-th black token is initially located. It is guaranteed that initial positions of black tokens are distinct.
Print the number of points where the white token can be located initially, such that if both players play optimally, the black player wins.
[ "4\n-2 -1\n0 1\n0 -3\n2 -1\n", "4\n-2 0\n-1 1\n0 -2\n1 -1\n", "16\n2 1\n1 2\n-1 1\n0 1\n0 0\n1 1\n2 -1\n2 0\n1 0\n-1 -1\n1 -1\n2 2\n0 -1\n-1 0\n0 2\n-1 2\n" ]
[ "4\n", "2\n", "4\n" ]
In the first and second examples initial positions of black tokens are shown with black points, possible positions of the white token (such that the black player wins) are shown with white points. The first example: <img class="tex-graphics" src="https://espresso.codeforces.com/5054b8d2df2fac92c92f96fae82d21c365d12983.png" style="max-width: 100.0%;max-height: 100.0%;"/> The second example: <img class="tex-graphics" src="https://espresso.codeforces.com/eb795dd6abb95cfafb1d1cb7d8c8798825dcc180.png" style="max-width: 100.0%;max-height: 100.0%;"/> In the third example the white tokens should be located in the inner square 2 × 2, to make the black player win. <img class="tex-graphics" src="https://espresso.codeforces.com/6dfd863f649b92860dfd6b446ea004abc01b71a6.png" style="max-width: 100.0%;max-height: 100.0%;"/>
[ { "input": "4\n-2 -1\n0 1\n0 -3\n2 -1", "output": "4" }, { "input": "4\n-2 0\n-1 1\n0 -2\n1 -1", "output": "2" }, { "input": "16\n2 1\n1 2\n-1 1\n0 1\n0 0\n1 1\n2 -1\n2 0\n1 0\n-1 -1\n1 -1\n2 2\n0 -1\n-1 0\n0 2\n-1 2", "output": "4" }, { "input": "1\n1 2", "output": "0" }, { "input": "4\n0 99999\n-99999 0\n99999 0\n0 -99999", "output": "9999800001" }, { "input": "10\n-1 3\n-1 7\n-8 -4\n5 14\n-6 -7\n11 -8\n-11 0\n5 -1\n9 4\n-2 -14", "output": "110" }, { "input": "50\n-15 -80\n-80 21\n90 38\n-100 27\n-64 -75\n-10 59\n38 44\n-31 -91\n97 76\n87 43\n5 43\n-73 74\n-45 42\n31 -100\n-87 19\n-21 -13\n-71 38\n-54 -39\n-89 -32\n-18 99\n-44 -78\n9 76\n-69 -40\n-29 23\n-88 42\n-95 86\n45 15\n-39 100\n17 -33\n5 -48\n-4 -22\n-19 54\n-13 -64\n-86 68\n-52 -95\n-73 -29\n-24 -93\n-60 96\n41 57\n55 43\n-64 15\n-43 9\n29 88\n44 -2\n67 -94\n-20 -81\n-75 -74\n-80 -44\n-49 -7\n39 -59", "output": "17145" }, { "input": "2\n-3 0\n3 2", "output": "0" }, { "input": "3\n-5 3\n4 -5\n-3 2", "output": "0" }, { "input": "4\n-5 1\n0 -3\n-1 4\n5 -5", "output": "0" }, { "input": "4\n-1 4\n-3 2\n-2 1\n-5 3", "output": "0" }, { "input": "5\n-3 -5\n5 2\n-4 1\n-2 0\n1 2", "output": "0" }, { "input": "5\n2 3\n1 -1\n0 2\n0 5\n3 2", "output": "0" }, { "input": "6\n-1 2\n5 -4\n0 4\n3 0\n-4 -1\n-3 -2", "output": "3" }, { "input": "6\n-3 4\n-1 -3\n1 -4\n1 -1\n-5 -1\n1 4", "output": "0" }, { "input": "7\n0 4\n0 3\n-1 3\n4 3\n1 3\n-4 4\n5 4", "output": "0" }, { "input": "7\n4 4\n2 3\n5 -2\n-1 1\n2 2\n-2 -2\n-1 2", "output": "0" }, { "input": "8\n2 -4\n-4 -2\n-3 3\n-3 -1\n4 -4\n2 3\n4 -5\n0 0", "output": "4" }, { "input": "8\n4 -4\n5 -5\n3 2\n-2 5\n-4 -2\n2 5\n-5 5\n5 4", "output": "2" }, { "input": "9\n4 -5\n-4 -3\n4 -4\n1 0\n5 -1\n-3 1\n5 -4\n2 -4\n4 -3", "output": "4" }, { "input": "9\n-2 0\n-4 -4\n0 4\n2 2\n-3 -2\n1 3\n-5 5\n-3 -3\n-1 1", "output": "5" }, { "input": "10\n-1 -2\n-5 2\n-5 0\n-1 0\n4 0\n4 5\n0 -3\n-3 -3\n-5 5\n3 -4", "output": "2" }, { "input": "10\n2 -3\n-3 3\n-1 -1\n3 -5\n5 -3\n0 5\n-4 -4\n2 -4\n-2 -5\n-2 4", "output": "11" }, { "input": "10\n2 -4\n0 -3\n3 2\n-1 3\n1 -1\n4 -5\n-4 2\n1 0\n-2 -5\n-2 2", "output": "3" }, { "input": "10\n-4 -2\n-1 0\n1 -3\n2 5\n3 1\n3 -3\n2 4\n-2 -1\n-3 3\n5 2", "output": "7" }, { "input": "10\n2 0\n1 2\n4 0\n3 -1\n4 3\n-5 4\n-4 -1\n1 -1\n2 -1\n-5 -4", "output": "3" }, { "input": "10\n3 4\n-2 -3\n-2 5\n-2 1\n5 4\n2 -1\n5 -4\n0 1\n4 4\n3 -1", "output": "6" }, { "input": "10\n-1 3\n2 3\n3 2\n-4 -3\n-2 -5\n5 -5\n-4 -4\n0 1\n4 -1\n3 3", "output": "11" }, { "input": "10\n1 -3\n0 4\n-1 3\n-2 3\n4 1\n-1 5\n5 4\n-5 5\n-4 -2\n-5 1", "output": "5" }, { "input": "10\n5 -1\n-2 5\n-5 -1\n-3 -3\n-5 -4\n-3 -2\n-1 -4\n2 5\n4 -5\n1 -4", "output": "5" }, { "input": "10\n-1 0\n5 -1\n-4 1\n-3 0\n-5 -1\n-3 -4\n3 3\n-2 2\n-3 -2\n3 -1", "output": "2" }, { "input": "20\n-16 24\n9 13\n-1 -3\n5 7\n-20 17\n21 5\n-10 8\n0 -14\n17 -5\n7 1\n-6 16\n-18 -9\n-7 -8\n-13 -23\n4 4\n10 -3\n2 -5\n-18 24\n19 -19\n12 -25", "output": "268" }, { "input": "20\n-4 23\n-10 3\n20 25\n24 -23\n1 18\n-23 -24\n-20 -6\n7 22\n11 -18\n-25 -19\n7 -6\n-9 22\n-24 -2\n-9 -17\n-1 12\n-20 -21\n-19 -24\n10 -20\n20 8\n25 -14", "output": "312" }, { "input": "20\n21 20\n23 -21\n-22 24\n-18 -2\n-6 -15\n-20 -10\n-15 21\n-18 5\n13 10\n-11 15\n-6 -1\n17 6\n-13 -23\n8 -9\n-24 21\n8 11\n21 9\n22 12\n-2 -21\n-12 -10", "output": "459" }, { "input": "20\n-5 -7\n-17 22\n13 -4\n19 8\n2 6\n-4 1\n7 -15\n-5 -15\n-14 -13\n14 8\n-13 -23\n8 4\n-13 18\n-17 3\n9 3\n7 -11\n6 -16\n-15 9\n-24 -17\n-20 -18", "output": "227" }, { "input": "20\n-9 5\n-25 -4\n14 -22\n-17 23\n-20 -8\n19 22\n23 -3\n-23 -11\n-2 -15\n22 -4\n-10 -16\n16 22\n9 9\n-18 16\n-25 6\n8 -10\n-2 -17\n-12 6\n20 -10\n17 -6", "output": "529" }, { "input": "20\n-13 15\n1 14\n-12 7\n-18 -15\n-19 -11\n-7 6\n7 -15\n4 18\n-4 10\n-23 16\n-8 -15\n-3 14\n-8 1\n17 19\n15 19\n-3 -12\n-25 16\n-7 -1\n-14 1\n18 3", "output": "295" }, { "input": "20\n-17 7\n0 -21\n15 -10\n-5 12\n18 -12\n-19 11\n24 -19\n-25 -1\n-5 -25\n20 -23\n-4 9\n7 -15\n8 -9\n23 -15\n-2 5\n10 -4\n12 -24\n25 2\n5 -6\n2 25", "output": "577" }, { "input": "20\n17 23\n-7 8\n3 9\n9 -22\n-9 -14\n-18 -10\n-4 2\n10 -3\n-9 19\n-7 9\n-22 4\n6 14\n-9 -18\n2 0\n-17 4\n6 20\n24 13\n22 4\n-14 -1\n-6 -14", "output": "454" }, { "input": "20\n-10 16\n24 18\n-1 -22\n1 4\n4 -19\n-22 8\n-20 -20\n25 24\n-4 8\n7 -11\n-17 -14\n25 -12\n24 23\n-18 15\n23 -1\n-11 -14\n-4 -6\n-14 18\n-10 18\n2 -17", "output": "487" }, { "input": "20\n11 1\n-15 23\n5 24\n7 -13\n-13 -13\n-25 20\n22 -16\n-23 -2\n11 -21\n12 1\n2 3\n-3 -17\n4 21\n-17 12\n13 -14\n1 4\n23 -22\n-18 9\n14 5\n-23 -3", "output": "502" } ]
109
0
-1
15,736
818
Sofa Thief
[ "brute force", "implementation" ]
null
null
Yet another round on DecoForces is coming! Grandpa Maks wanted to participate in it but someone has stolen his precious sofa! And how can one perform well with such a major loss? Fortunately, the thief had left a note for Grandpa Maks. This note got Maks to the sofa storehouse. Still he had no idea which sofa belongs to him as they all looked the same! The storehouse is represented as matrix *n*<=Γ—<=*m*. Every sofa takes two neighbouring by some side cells. No cell is covered by more than one sofa. There can be empty cells. Sofa *A* is standing to the left of sofa *B* if there exist two such cells *a* and *b* that *x**a*<=&lt;<=*x**b*, *a* is covered by *A* and *b* is covered by *B*. Sofa *A* is standing to the top of sofa *B* if there exist two such cells *a* and *b* that *y**a*<=&lt;<=*y**b*, *a* is covered by *A* and *b* is covered by *B*. Right and bottom conditions are declared the same way. Note that in all conditions *A*<=β‰ <=*B*. Also some sofa *A* can be both to the top of another sofa *B* and to the bottom of it. The same is for left and right conditions. The note also stated that there are *cnt**l* sofas to the left of Grandpa Maks's sofa, *cnt**r* β€” to the right, *cnt**t* β€” to the top and *cnt**b* β€” to the bottom. Grandpa Maks asks you to help him to identify his sofa. It is guaranteed that there is no more than one sofa of given conditions. Output the number of Grandpa Maks's sofa. If there is no such sofa that all the conditions are met for it then output -1.
The first line contains one integer number *d* (1<=≀<=*d*<=≀<=105) β€” the number of sofas in the storehouse. The second line contains two integer numbers *n*, *m* (1<=≀<=*n*,<=*m*<=≀<=105) β€” the size of the storehouse. Next *d* lines contains four integer numbers *x*1, *y*1, *x*2, *y*2 (1<=≀<=*x*1,<=*x*2<=≀<=*n*, 1<=≀<=*y*1,<=*y*2<=≀<=*m*) β€” coordinates of the *i*-th sofa. It is guaranteed that cells (*x*1,<=*y*1) and (*x*2,<=*y*2) have common side, (*x*1,<=*y*1) <=β‰ <= (*x*2,<=*y*2) and no cell is covered by more than one sofa. The last line contains four integer numbers *cnt**l*, *cnt**r*, *cnt**t*, *cnt**b* (0<=≀<=*cnt**l*,<=*cnt**r*,<=*cnt**t*,<=*cnt**b*<=≀<=*d*<=-<=1).
Print the number of the sofa for which all the conditions are met. Sofas are numbered 1 through *d* as given in input. If there is no such sofa then print -1.
[ "2\n3 2\n3 1 3 2\n1 2 2 2\n1 0 0 1\n", "3\n10 10\n1 2 1 1\n5 5 6 5\n6 4 5 4\n2 1 2 0\n", "2\n2 2\n2 1 1 1\n1 2 2 2\n1 0 0 0\n" ]
[ "1\n", "2\n", "-1\n" ]
Let's consider the second example. - The first sofa has 0 to its left, 2 sofas to its right ((1, 1) is to the left of both (5, 5) and (5, 4)), 0 to its top and 2 to its bottom (both 2nd and 3rd sofas are below). - The second sofa has *cnt*<sub class="lower-index">*l*</sub> = 2, *cnt*<sub class="lower-index">*r*</sub> = 1, *cnt*<sub class="lower-index">*t*</sub> = 2 and *cnt*<sub class="lower-index">*b*</sub> = 0. - The third sofa has *cnt*<sub class="lower-index">*l*</sub> = 2, *cnt*<sub class="lower-index">*r*</sub> = 1, *cnt*<sub class="lower-index">*t*</sub> = 1 and *cnt*<sub class="lower-index">*b*</sub> = 1. So the second one corresponds to the given conditions. In the third example - The first sofa has *cnt*<sub class="lower-index">*l*</sub> = 1, *cnt*<sub class="lower-index">*r*</sub> = 1, *cnt*<sub class="lower-index">*t*</sub> = 0 and *cnt*<sub class="lower-index">*b*</sub> = 1. - The second sofa has *cnt*<sub class="lower-index">*l*</sub> = 1, *cnt*<sub class="lower-index">*r*</sub> = 1, *cnt*<sub class="lower-index">*t*</sub> = 1 and *cnt*<sub class="lower-index">*b*</sub> = 0. And there is no sofa with the set (1, 0, 0, 0) so the answer is -1.
[ { "input": "2\n3 2\n3 1 3 2\n1 2 2 2\n1 0 0 1", "output": "1" }, { "input": "3\n10 10\n1 2 1 1\n5 5 6 5\n6 4 5 4\n2 1 2 0", "output": "2" }, { "input": "2\n2 2\n2 1 1 1\n1 2 2 2\n1 0 0 0", "output": "-1" }, { "input": "1\n1 2\n1 1 1 2\n0 0 0 0", "output": "1" }, { "input": "1\n2 1\n2 1 1 1\n0 0 0 0", "output": "1" }, { "input": "1\n1000 1000\n63 902 63 901\n0 0 0 0", "output": "1" }, { "input": "6\n10 10\n3 6 3 7\n4 9 5 9\n5 4 5 3\n7 1 8 1\n9 10 8 10\n7 7 7 8\n0 5 2 3", "output": "1" }, { "input": "2\n4 4\n3 1 3 2\n2 2 2 1\n0 0 0 0", "output": "-1" }, { "input": "2\n2 2\n1 1 1 2\n2 1 2 2\n0 1 1 1", "output": "1" }, { "input": "2\n2 2\n1 1 1 2\n2 1 2 2\n1 0 1 1", "output": "2" }, { "input": "2\n2 2\n1 1 1 2\n2 1 2 2\n0 1 1 0", "output": "-1" }, { "input": "1\n1 2\n1 2 1 1\n0 0 0 0", "output": "1" }, { "input": "1\n1 3\n1 2 1 3\n0 0 0 0", "output": "1" }, { "input": "1\n1 4\n1 2 1 1\n0 0 0 0", "output": "1" }, { "input": "1\n1 5\n1 4 1 3\n0 0 0 0", "output": "1" }, { "input": "1\n1 6\n1 6 1 5\n0 0 0 0", "output": "1" }, { "input": "1\n1 7\n1 6 1 7\n0 0 0 0", "output": "1" }, { "input": "1\n2 1\n2 1 1 1\n0 0 0 0", "output": "1" }, { "input": "1\n2 2\n2 2 2 1\n0 0 0 0", "output": "1" }, { "input": "1\n2 3\n1 2 1 1\n0 0 0 0", "output": "1" }, { "input": "1\n2 4\n2 3 2 4\n0 0 0 0", "output": "1" }, { "input": "1\n2 5\n2 4 1 4\n0 0 0 0", "output": "1" }, { "input": "1\n2 6\n2 1 1 1\n0 0 0 0", "output": "1" }, { "input": "1\n2 7\n2 7 2 6\n0 0 0 0", "output": "1" }, { "input": "1\n3 1\n2 1 3 1\n0 0 0 0", "output": "1" }, { "input": "1\n3 2\n1 1 2 1\n0 0 0 0", "output": "1" }, { "input": "1\n3 3\n3 2 3 3\n0 0 0 0", "output": "1" }, { "input": "1\n3 4\n2 1 2 2\n0 0 0 0", "output": "1" }, { "input": "1\n3 5\n2 2 2 1\n0 0 0 0", "output": "1" }, { "input": "1\n3 6\n1 4 2 4\n0 0 0 0", "output": "1" }, { "input": "1\n3 7\n2 2 1 2\n0 0 0 0", "output": "1" }, { "input": "1\n4 1\n1 1 2 1\n0 0 0 0", "output": "1" }, { "input": "1\n4 2\n1 1 1 2\n0 0 0 0", "output": "1" }, { "input": "1\n4 3\n4 3 4 2\n0 0 0 0", "output": "1" }, { "input": "1\n4 4\n3 2 3 3\n0 0 0 0", "output": "1" }, { "input": "1\n4 5\n1 2 2 2\n0 0 0 0", "output": "1" }, { "input": "1\n4 6\n4 3 4 4\n0 0 0 0", "output": "1" }, { "input": "1\n4 7\n3 6 4 6\n0 0 0 0", "output": "1" }, { "input": "1\n5 1\n2 1 1 1\n0 0 0 0", "output": "1" }, { "input": "1\n5 2\n5 1 4 1\n0 0 0 0", "output": "1" }, { "input": "1\n5 3\n4 2 3 2\n0 0 0 0", "output": "1" }, { "input": "1\n5 4\n2 4 3 4\n0 0 0 0", "output": "1" }, { "input": "1\n5 5\n4 1 3 1\n0 0 0 0", "output": "1" }, { "input": "1\n5 6\n3 3 3 2\n0 0 0 0", "output": "1" }, { "input": "1\n5 7\n1 6 1 7\n0 0 0 0", "output": "1" }, { "input": "1\n6 1\n6 1 5 1\n0 0 0 0", "output": "1" }, { "input": "1\n6 2\n4 2 5 2\n0 0 0 0", "output": "1" }, { "input": "1\n6 3\n1 2 1 1\n0 0 0 0", "output": "1" }, { "input": "1\n6 4\n2 2 3 2\n0 0 0 0", "output": "1" }, { "input": "1\n6 5\n6 1 6 2\n0 0 0 0", "output": "1" }, { "input": "1\n6 6\n4 1 3 1\n0 0 0 0", "output": "1" }, { "input": "1\n6 7\n6 7 6 6\n0 0 0 0", "output": "1" }, { "input": "1\n7 1\n6 1 7 1\n0 0 0 0", "output": "1" }, { "input": "1\n7 2\n4 2 4 1\n0 0 0 0", "output": "1" }, { "input": "1\n7 3\n7 1 7 2\n0 0 0 0", "output": "1" }, { "input": "1\n7 4\n3 3 3 4\n0 0 0 0", "output": "1" }, { "input": "1\n7 5\n6 4 7 4\n0 0 0 0", "output": "1" }, { "input": "1\n7 6\n2 2 2 3\n0 0 0 0", "output": "1" }, { "input": "1\n7 7\n1 3 2 3\n0 0 0 0", "output": "1" }, { "input": "1\n1 4\n1 4 1 3\n0 0 0 0", "output": "1" }, { "input": "2\n1 5\n1 5 1 4\n1 1 1 2\n0 0 1 0", "output": "1" }, { "input": "1\n1 6\n1 2 1 3\n0 0 0 0", "output": "1" }, { "input": "2\n1 7\n1 7 1 6\n1 4 1 5\n0 0 1 0", "output": "1" }, { "input": "1\n2 2\n2 1 2 2\n0 0 0 0", "output": "1" }, { "input": "2\n2 3\n2 3 1 3\n1 2 2 2\n0 0 0 1", "output": "-1" }, { "input": "2\n2 4\n2 2 2 1\n2 4 1 4\n0 1 1 0", "output": "2" }, { "input": "2\n2 5\n2 2 2 1\n1 3 1 4\n1 0 0 1", "output": "1" }, { "input": "2\n2 6\n1 2 1 1\n2 1 2 2\n1 0 1 1", "output": "2" }, { "input": "2\n2 7\n2 4 2 5\n2 7 1 7\n0 0 1 0", "output": "-1" }, { "input": "2\n3 2\n1 2 2 2\n1 1 2 1\n0 0 1 0", "output": "-1" }, { "input": "2\n3 3\n2 1 1 1\n1 2 2 2\n0 0 0 1", "output": "-1" }, { "input": "1\n3 4\n1 3 1 4\n0 0 0 0", "output": "1" }, { "input": "2\n3 5\n1 2 1 1\n3 1 2 1\n0 1 0 0", "output": "-1" }, { "input": "2\n3 6\n3 2 3 1\n3 6 2 6\n0 0 0 1", "output": "-1" }, { "input": "2\n3 7\n3 6 3 5\n2 4 2 3\n0 1 0 1", "output": "2" }, { "input": "2\n4 1\n3 1 4 1\n1 1 2 1\n0 1 0 0", "output": "2" }, { "input": "1\n4 2\n4 1 3 1\n0 0 0 0", "output": "1" }, { "input": "2\n4 3\n3 1 2 1\n1 2 1 1\n1 0 0 1", "output": "1" }, { "input": "1\n4 4\n4 1 3 1\n0 0 0 0", "output": "1" }, { "input": "2\n4 5\n3 1 4 1\n4 2 4 3\n0 1 0 1", "output": "1" }, { "input": "2\n4 6\n2 3 2 4\n2 6 2 5\n0 0 0 1", "output": "1" }, { "input": "2\n4 7\n1 7 2 7\n4 1 3 1\n1 0 0 1", "output": "2" }, { "input": "2\n5 1\n2 1 1 1\n5 1 4 1\n1 0 0 0", "output": "2" }, { "input": "2\n5 2\n1 1 1 2\n2 2 3 2\n1 0 1 0", "output": "2" }, { "input": "2\n5 3\n1 1 1 2\n5 2 5 3\n0 1 0 1", "output": "1" }, { "input": "2\n5 4\n4 4 4 3\n4 2 5 2\n0 0 0 1", "output": "-1" }, { "input": "2\n5 5\n3 4 3 5\n4 1 3 1\n1 0 0 1", "output": "2" }, { "input": "2\n5 6\n2 4 3 4\n5 2 5 1\n0 1 1 0", "output": "1" }, { "input": "2\n5 7\n2 7 1 7\n2 4 3 4\n0 0 0 1", "output": "-1" }, { "input": "1\n6 1\n3 1 4 1\n0 0 0 0", "output": "1" }, { "input": "1\n6 2\n5 1 6 1\n0 0 0 0", "output": "1" }, { "input": "2\n6 3\n2 2 2 1\n3 2 3 1\n0 1 0 0", "output": "-1" }, { "input": "2\n6 4\n6 4 5 4\n4 3 4 2\n1 0 1 0", "output": "1" }, { "input": "2\n6 5\n2 4 2 3\n5 4 4 4\n1 0 0 0", "output": "-1" }, { "input": "2\n6 6\n6 6 5 6\n1 3 1 2\n1 0 1 0", "output": "1" }, { "input": "2\n6 7\n1 3 1 4\n5 2 5 1\n0 1 1 0", "output": "1" }, { "input": "1\n7 1\n6 1 7 1\n0 0 0 0", "output": "1" }, { "input": "2\n7 2\n5 2 4 2\n2 1 2 2\n0 1 0 1", "output": "2" }, { "input": "2\n7 3\n7 2 6 2\n1 2 2 2\n0 1 0 0", "output": "2" }, { "input": "2\n7 4\n6 1 6 2\n2 3 1 3\n1 0 0 1", "output": "1" }, { "input": "2\n7 5\n2 3 1 3\n4 3 3 3\n1 0 0 0", "output": "2" }, { "input": "2\n7 6\n5 1 6 1\n2 5 3 5\n0 1 1 0", "output": "2" }, { "input": "2\n7 7\n2 3 2 4\n5 4 5 5\n0 1 0 1", "output": "1" }, { "input": "1\n1 6\n1 4 1 5\n0 0 0 0", "output": "1" }, { "input": "1\n1 7\n1 1 1 2\n0 0 0 0", "output": "1" }, { "input": "1\n2 3\n1 1 2 1\n0 0 0 0", "output": "1" }, { "input": "3\n2 4\n1 3 1 4\n2 4 2 3\n2 2 1 2\n0 0 0 2", "output": "-1" }, { "input": "3\n2 5\n2 5 1 5\n2 3 2 2\n1 1 2 1\n0 0 1 1", "output": "-1" }, { "input": "1\n2 6\n1 3 1 2\n0 0 0 0", "output": "1" }, { "input": "3\n2 7\n2 6 2 7\n1 4 1 5\n2 2 2 3\n1 0 0 2", "output": "3" }, { "input": "1\n3 2\n3 2 2 2\n0 0 0 0", "output": "1" }, { "input": "1\n3 3\n2 3 3 3\n0 0 0 0", "output": "1" }, { "input": "2\n3 4\n3 1 3 2\n3 4 2 4\n0 1 1 0", "output": "2" }, { "input": "3\n3 5\n3 4 3 5\n3 2 3 1\n1 3 2 3\n1 0 0 2", "output": "2" }, { "input": "2\n3 6\n1 1 2 1\n1 3 2 3\n0 0 1 0", "output": "-1" }, { "input": "1\n3 7\n2 1 3 1\n0 0 0 0", "output": "1" }, { "input": "3\n4 2\n1 2 2 2\n3 1 4 1\n3 2 4 2\n0 2 1 0", "output": "1" }, { "input": "2\n4 3\n4 3 3 3\n2 2 2 1\n1 0 1 0", "output": "1" }, { "input": "3\n4 4\n2 3 2 4\n4 4 4 3\n2 2 1 2\n0 2 0 2", "output": "3" }, { "input": "3\n4 5\n2 4 1 4\n1 3 1 2\n2 1 1 1\n2 1 2 0", "output": "1" }, { "input": "2\n4 6\n3 3 4 3\n4 6 3 6\n0 0 1 0", "output": "-1" }, { "input": "3\n4 7\n2 7 3 7\n4 4 4 5\n3 4 3 3\n2 0 0 1", "output": "-1" }, { "input": "1\n5 2\n1 1 1 2\n0 0 0 0", "output": "1" }, { "input": "3\n5 3\n1 2 1 3\n5 2 5 3\n1 1 2 1\n1 1 0 2", "output": "3" }, { "input": "3\n5 4\n4 1 4 2\n1 1 1 2\n5 1 5 2\n0 2 2 2", "output": "2" }, { "input": "2\n5 5\n3 3 4 3\n5 2 4 2\n0 0 0 1", "output": "-1" }, { "input": "3\n5 6\n5 2 4 2\n1 1 1 2\n5 1 4 1\n2 1 2 0", "output": "1" }, { "input": "3\n5 7\n5 4 4 4\n1 2 1 1\n2 5 2 4\n0 2 0 2", "output": "2" }, { "input": "2\n6 1\n3 1 2 1\n4 1 5 1\n1 0 0 0", "output": "2" }, { "input": "3\n6 2\n5 2 5 1\n6 1 6 2\n3 2 2 2\n2 0 0 0", "output": "-1" }, { "input": "3\n6 3\n2 1 2 2\n6 2 6 1\n1 2 1 1\n1 1 0 0", "output": "-1" }, { "input": "3\n6 4\n1 2 2 2\n3 1 3 2\n2 3 2 4\n0 2 0 1", "output": "-1" }, { "input": "3\n6 5\n2 2 2 1\n5 4 6 4\n4 4 4 3\n2 0 1 0", "output": "-1" }, { "input": "3\n6 6\n4 4 4 5\n2 3 1 3\n3 4 3 3\n0 2 0 1", "output": "-1" }, { "input": "3\n6 7\n3 4 3 5\n5 4 6 4\n4 5 4 4\n1 1 1 0", "output": "-1" }, { "input": "3\n7 1\n4 1 5 1\n3 1 2 1\n6 1 7 1\n2 0 0 0", "output": "3" }, { "input": "3\n7 2\n7 1 7 2\n5 1 4 1\n3 1 3 2\n0 2 2 1", "output": "3" }, { "input": "3\n7 3\n2 3 3 3\n5 1 6 1\n7 2 7 1\n0 2 2 0", "output": "1" }, { "input": "3\n7 4\n5 4 6 4\n6 1 6 2\n5 1 4 1\n0 2 0 1", "output": "-1" }, { "input": "3\n7 5\n2 2 2 3\n7 1 7 2\n1 4 1 3\n2 0 0 2", "output": "2" }, { "input": "3\n7 6\n2 6 2 5\n2 2 1 2\n4 4 3 4\n0 1 0 2", "output": "-1" }, { "input": "1\n7 7\n5 4 6 4\n0 0 0 0", "output": "1" }, { "input": "1\n2 4\n1 1 1 2\n0 0 0 0", "output": "1" }, { "input": "3\n2 5\n2 4 2 5\n2 1 1 1\n2 2 1 2\n0 1 1 1", "output": "-1" }, { "input": "3\n2 6\n1 3 1 2\n2 2 2 1\n2 5 2 6\n1 0 0 1", "output": "-1" }, { "input": "1\n2 7\n2 1 1 1\n0 0 0 0", "output": "1" }, { "input": "4\n3 3\n3 1 2 1\n3 3 2 3\n1 3 1 2\n3 2 2 2\n0 3 2 1", "output": "3" }, { "input": "4\n3 4\n2 4 3 4\n3 3 3 2\n1 2 2 2\n3 1 2 1\n0 3 1 1", "output": "-1" }, { "input": "4\n3 5\n2 3 1 3\n1 5 1 4\n2 5 2 4\n2 2 1 2\n1 0 3 1", "output": "-1" }, { "input": "2\n3 6\n1 5 1 6\n3 5 3 4\n1 0 0 1", "output": "2" }, { "input": "4\n3 7\n1 2 1 1\n3 3 3 4\n2 1 3 1\n2 6 3 6\n1 1 3 0", "output": "-1" }, { "input": "3\n4 2\n2 2 3 2\n1 1 1 2\n4 2 4 1\n2 0 0 0", "output": "-1" }, { "input": "2\n4 3\n1 2 1 1\n3 1 3 2\n0 1 0 0", "output": "-1" }, { "input": "2\n4 4\n3 1 4 1\n3 4 4 4\n0 0 1 0", "output": "-1" }, { "input": "2\n4 5\n3 1 3 2\n2 1 2 2\n1 0 0 0", "output": "-1" }, { "input": "4\n4 6\n1 5 2 5\n3 4 3 5\n1 1 1 2\n4 1 4 2\n2 1 2 0", "output": "-1" }, { "input": "3\n4 7\n4 2 4 3\n1 4 1 3\n1 2 1 1\n0 1 0 2", "output": "3" }, { "input": "3\n5 2\n1 1 2 1\n3 1 4 1\n3 2 2 2\n1 1 2 0", "output": "3" }, { "input": "1\n5 3\n2 1 1 1\n0 0 0 0", "output": "1" }, { "input": "2\n5 4\n1 2 1 3\n5 4 5 3\n1 0 0 0", "output": "-1" }, { "input": "4\n5 5\n5 1 4 1\n3 3 3 4\n1 3 2 3\n2 1 2 2\n0 2 0 2", "output": "-1" }, { "input": "3\n5 6\n4 6 4 5\n1 5 1 6\n5 5 5 4\n0 2 1 0", "output": "-1" }, { "input": "3\n5 7\n1 5 1 4\n2 5 3 5\n4 4 3 4\n2 0 0 1", "output": "-1" }, { "input": "2\n6 2\n1 1 2 1\n6 1 5 1\n0 1 0 0", "output": "1" }, { "input": "2\n6 3\n3 3 4 3\n5 3 6 3\n1 0 0 0", "output": "2" }, { "input": "4\n6 4\n3 2 3 1\n4 1 5 1\n6 1 6 2\n2 2 1 2\n2 1 0 3", "output": "2" }, { "input": "3\n6 5\n5 4 5 3\n1 3 1 2\n2 1 1 1\n1 1 0 2", "output": "3" }, { "input": "3\n6 6\n1 2 2 2\n1 5 1 6\n6 6 6 5\n0 1 1 0", "output": "-1" }, { "input": "4\n6 7\n5 4 5 5\n4 4 3 4\n2 1 1 1\n6 3 6 2\n1 2 2 0", "output": "-1" }, { "input": "3\n7 2\n5 1 6 1\n2 2 3 2\n2 1 1 1\n2 0 0 1", "output": "1" }, { "input": "4\n7 3\n6 1 7 1\n3 1 4 1\n6 2 5 2\n2 1 1 1\n2 1 3 0", "output": "3" }, { "input": "4\n7 4\n4 2 3 2\n5 2 5 3\n3 4 2 4\n6 2 6 1\n3 0 0 3", "output": "4" }, { "input": "1\n7 5\n6 5 7 5\n0 0 0 0", "output": "1" }, { "input": "3\n7 6\n2 6 1 6\n2 4 2 5\n3 2 2 2\n1 0 0 2", "output": "-1" }, { "input": "4\n7 7\n4 6 5 6\n7 4 7 5\n7 1 7 2\n2 6 2 5\n1 2 2 0", "output": "-1" }, { "input": "4\n2 5\n1 3 2 3\n1 5 1 4\n1 2 2 2\n1 1 2 1\n0 0 3 0", "output": "-1" }, { "input": "2\n2 6\n2 1 2 2\n1 2 1 1\n1 0 0 0", "output": "-1" }, { "input": "4\n2 7\n1 2 2 2\n2 6 2 5\n2 3 1 3\n1 5 1 4\n0 3 2 1", "output": "4" }, { "input": "3\n3 4\n2 2 3 2\n1 2 1 3\n3 1 2 1\n1 0 0 2", "output": "-1" }, { "input": "4\n3 5\n3 1 3 2\n2 3 2 2\n2 5 1 5\n3 4 3 3\n2 0 2 1", "output": "4" }, { "input": "4\n3 6\n3 1 2 1\n1 2 2 2\n2 3 3 3\n1 5 1 4\n0 2 3 0", "output": "-1" }, { "input": "3\n3 7\n3 2 2 2\n3 5 2 5\n3 7 2 7\n0 0 1 1", "output": "-1" }, { "input": "4\n4 3\n3 2 3 3\n4 2 4 1\n1 2 1 3\n3 1 2 1\n0 3 1 0", "output": "-1" }, { "input": "4\n4 4\n2 4 1 4\n1 2 1 3\n4 3 4 4\n3 3 3 2\n0 2 0 2", "output": "-1" }, { "input": "3\n4 5\n4 5 3 5\n4 2 3 2\n2 1 3 1\n0 1 0 2", "output": "-1" }, { "input": "5\n4 6\n4 3 3 3\n4 2 4 1\n3 6 2 6\n2 4 2 3\n1 1 1 2\n1 2 2 1", "output": "-1" }, { "input": "2\n4 7\n2 6 2 7\n2 5 2 4\n0 0 1 0", "output": "1" }, { "input": "1\n5 2\n2 2 2 1\n0 0 0 0", "output": "1" }, { "input": "1\n5 3\n4 2 3 2\n0 0 0 0", "output": "1" }, { "input": "2\n5 4\n3 1 2 1\n3 4 3 3\n0 0 1 0", "output": "-1" }, { "input": "1\n5 5\n3 4 2 4\n0 0 0 0", "output": "1" }, { "input": "4\n5 6\n5 3 5 2\n4 5 3 5\n1 2 1 3\n1 1 2 1\n3 0 1 1", "output": "-1" }, { "input": "5\n5 7\n5 5 5 6\n2 4 2 5\n2 3 1 3\n4 7 3 7\n4 1 5 1\n0 3 2 2", "output": "-1" }, { "input": "2\n6 2\n5 2 5 1\n4 2 4 1\n1 0 1 1", "output": "1" }, { "input": "3\n6 3\n2 2 2 3\n3 3 4 3\n4 2 4 1\n1 1 1 0", "output": "-1" }, { "input": "4\n6 4\n2 3 1 3\n4 4 3 4\n5 4 6 4\n1 4 2 4\n0 2 1 0", "output": "-1" }, { "input": "5\n6 5\n1 5 1 4\n4 2 4 3\n2 2 1 2\n2 3 1 3\n3 2 3 3\n0 2 0 3", "output": "-1" }, { "input": "4\n6 6\n4 3 4 2\n2 3 2 4\n4 4 5 4\n5 2 5 3\n0 3 2 0", "output": "-1" }, { "input": "5\n6 7\n1 6 1 5\n3 6 2 6\n5 1 4 1\n2 5 3 5\n5 3 5 2\n3 0 0 4", "output": "-1" }, { "input": "2\n7 2\n3 1 4 1\n7 1 7 2\n0 1 0 1", "output": "1" }, { "input": "2\n7 3\n6 3 7 3\n4 1 3 1\n0 1 0 1", "output": "2" }, { "input": "5\n7 4\n3 1 2 1\n5 2 5 1\n4 2 3 2\n7 3 6 3\n4 3 5 3\n1 2 2 2", "output": "-1" }, { "input": "5\n7 5\n5 3 5 2\n3 5 2 5\n1 3 1 4\n3 3 3 4\n4 1 3 1\n1 2 4 0", "output": "-1" }, { "input": "5\n7 6\n5 5 5 4\n6 1 7 1\n5 2 5 1\n1 1 2 1\n4 6 3 6\n1 3 4 0", "output": "5" }, { "input": "3\n7 7\n2 6 1 6\n7 2 6 2\n3 1 3 2\n2 0 1 1", "output": "2" } ]
763
26,828,800
0
15,774
172
Bus
[ "*special", "implementation", "sortings" ]
null
null
There is a bus stop near the university. The lessons are over, and *n* students come to the stop. The *i*-th student will appear at the bus stop at time *t**i* (all *t**i*'s are distinct). We shall assume that the stop is located on the coordinate axis *Ox*, at point *x*<==<=0, and the bus goes along the ray *Ox*, that is, towards the positive direction of the coordinate axis, and back. The *i*-th student needs to get to the point with coordinate *x**i* (*x**i*<=&gt;<=0). The bus moves by the following algorithm. Initially it is at point 0. The students consistently come to the stop and get on it. The bus has a seating capacity which is equal to *m* passengers. At the moment when *m* students get on the bus, it starts moving in the positive direction of the coordinate axis. Also it starts moving when the last (*n*-th) student gets on the bus. The bus is moving at a speed of 1 unit of distance per 1 unit of time, i.e. it covers distance *y* in time *y*. Every time the bus passes the point at which at least one student needs to get off, it stops and these students get off the bus. The students need 1<=+<=[*k*<=/<=2] units of time to get off the bus, where *k* is the number of students who leave at this point. Expression [*k*<=/<=2] denotes rounded down *k*<=/<=2. As soon as the last student leaves the bus, the bus turns around and goes back to the point *x*<==<=0. It doesn't make any stops until it reaches the point. At the given point the bus fills with students once more, and everything is repeated. If students come to the stop when there's no bus, they form a line (queue) and get on the bus in the order in which they came. Any number of students get on the bus in negligible time, you should assume that it doesn't take any time. Any other actions also take no time. The bus has no other passengers apart from the students. Write a program that will determine for each student the time when he got off the bus. The moment a student got off the bus is the moment the bus stopped at the student's destination stop (despite the fact that the group of students need some time to get off).
The first line contains two space-separated integers *n*,<=*m* (1<=≀<=*n*,<=*m*<=≀<=105) β€” the number of students and the number of passengers the bus can transport, correspondingly. Next *n* lines contain descriptions of the students, one per line. Each line contains a pair of integers *t**i*,<=*x**i* (1<=≀<=*t**i*<=≀<=105,<=1<=≀<=*x**i*<=≀<=104). The lines are given in the order of strict increasing of *t**i*. Values of *x**i* can coincide.
Print *n* numbers *w*1,<=*w*2,<=...,<=*w**n*, *w**i* β€” the moment of time when the *i*-th student got off the bus. Print the numbers on one line and separate them with single spaces.
[ "1 10\n3 5\n", "2 1\n3 5\n4 5\n", "5 4\n3 5\n4 5\n5 5\n6 5\n7 1\n", "20 4\n28 13\n31 13\n35 6\n36 4\n52 6\n53 4\n83 2\n84 4\n87 1\n93 6\n108 4\n113 6\n116 1\n125 2\n130 2\n136 13\n162 2\n166 4\n184 1\n192 2\n" ]
[ "8\n", "8 19\n", "11 11 11 11 20\n", "51 51 43 40 93 89 86 89 114 121 118 121 137 139 139 152 195 199 193 195\n" ]
In the first sample the bus waits for the first student for 3 units of time and drives him to his destination in additional 5 units of time. So the student leaves the bus at the moment of time 3 + 5 = 8. In the second sample the capacity of the bus equals 1, that's why it will drive the first student alone. This student is the same as the student from the first sample. So the bus arrives to his destination at the moment of time 8, spends 1 + [1 / 2] = 1 units of time on getting him off, and returns back to 0 in additional 5 units of time. That is, the bus returns to the bus stop at the moment of time 14. By this moment the second student has already came to the bus stop. So he immediately gets in the bus, and is driven to his destination in additional 5 units of time. He gets there at the moment 14 + 5 = 19. In the third sample the bus waits for the fourth student for 6 units of time, then drives for 5 units of time, then gets the passengers off for 1 + [4 / 2] = 3 units of time, then returns for 5 units of time, and then drives the fifth student for 1 unit of time.
[ { "input": "1 10\n3 5", "output": "8" }, { "input": "2 1\n3 5\n4 5", "output": "8 19" }, { "input": "5 4\n3 5\n4 5\n5 5\n6 5\n7 1", "output": "11 11 11 11 20" }, { "input": "20 4\n28 13\n31 13\n35 6\n36 4\n52 6\n53 4\n83 2\n84 4\n87 1\n93 6\n108 4\n113 6\n116 1\n125 2\n130 2\n136 13\n162 2\n166 4\n184 1\n192 2", "output": "51 51 43 40 93 89 86 89 114 121 118 121 137 139 139 152 195 199 193 195" }, { "input": "1 1\n109 15", "output": "124" }, { "input": "2 1\n43 5\n102 1", "output": "48 103" }, { "input": "4 2\n7 1\n12 14\n90 15\n176 1", "output": "13 27 192 177" }, { "input": "8 8\n48 14\n74 12\n94 4\n127 14\n151 11\n173 4\n190 14\n191 9", "output": "210 207 195 210 205 195 210 202" }, { "input": "16 1\n29 10\n48 13\n53 10\n54 5\n59 6\n67 9\n68 10\n95 13\n132 5\n148 6\n150 6\n154 6\n169 10\n171 10\n185 6\n198 6", "output": "39 63 87 103 115 131 151 175 194 206 219 232 249 270 287 300" }, { "input": "32 3\n9 2\n12 4\n13 7\n14 7\n15 4\n19 10\n20 10\n29 2\n38 7\n58 4\n59 1\n61 4\n73 4\n90 1\n92 4\n95 7\n103 4\n107 7\n119 4\n121 4\n122 10\n123 10\n127 2\n134 10\n142 7\n144 7\n151 10\n160 7\n165 10\n191 1\n197 1\n199 7", "output": "15 18 22 38 34 42 65 55 61 81 77 81 97 93 97 115 111 115 128 128 136 158 149 158 177 177 182 201 205 194 217 224" }, { "input": "32 4\n4 6\n7 5\n13 6\n27 6\n39 5\n48 5\n57 11\n62 13\n64 11\n68 11\n84 9\n86 5\n89 6\n91 6\n107 13\n108 13\n113 11\n120 13\n126 5\n130 6\n134 9\n136 6\n137 5\n139 9\n143 5\n154 9\n155 5\n157 13\n171 11\n179 11\n185 13\n190 5", "output": "34 32 34 34 67 67 75 78 105 105 102 97 124 124 133 133 161 164 153 155 189 185 183 189 205 211 205 216 242 242 246 235" }, { "input": "32 5\n12 11\n17 14\n21 2\n24 2\n35 7\n41 15\n51 11\n52 2\n53 2\n61 14\n62 14\n75 2\n89 15\n90 14\n95 7\n102 7\n104 2\n105 14\n106 14\n109 2\n133 2\n135 2\n143 14\n151 11\n155 14\n168 15\n169 15\n179 14\n180 7\n181 15\n186 7\n198 14", "output": "49 53 37 37 44 87 81 70 70 85 119 105 122 119 111 147 140 155 155 140 173 173 188 184 188 221 221 219 211 221 245 253" }, { "input": "32 6\n15 12\n24 6\n30 13\n35 6\n38 6\n46 6\n47 12\n60 6\n66 9\n71 15\n74 6\n76 15\n104 6\n105 6\n110 15\n124 12\n126 12\n129 9\n131 12\n134 15\n135 15\n141 12\n154 13\n167 9\n171 9\n179 15\n181 15\n185 12\n189 12\n191 6\n192 6\n196 12", "output": "61 52 63 52 52 52 92 83 88 96 83 96 135 135 149 144 144 140 180 186 186 180 183 176 213 222 222 217 217 209 245 252" }, { "input": "32 7\n4 14\n6 14\n17 4\n22 3\n29 4\n32 4\n39 10\n40 11\n44 11\n51 11\n57 10\n76 4\n82 4\n87 14\n88 10\n118 10\n121 10\n136 14\n141 3\n143 4\n159 10\n162 10\n163 11\n165 10\n171 4\n172 10\n175 4\n176 3\n179 10\n196 10\n197 3\n198 10", "output": "57 57 44 42 44 44 52 101 101 101 99 91 91 106 171 171 171 178 162 164 171 206 209 206 198 206 198 196 232 232 224 232" }, { "input": "32 8\n12 9\n26 8\n27 8\n29 9\n43 11\n44 9\n45 5\n48 5\n50 8\n53 8\n57 9\n69 8\n76 11\n86 1\n88 9\n103 5\n116 9\n131 8\n139 8\n142 5\n148 1\n152 8\n154 8\n167 1\n170 5\n172 5\n173 5\n181 8\n183 1\n185 1\n190 1\n200 5", "output": "61 58 58 61 65 61 53 53 113 113 116 113 120 104 116 109 182 178 178 174 168 178 178 168 207 207 207 213 201 201 201 207" } ]
1,000
12,595,200
0
15,776
442
Adam and Tree
[ "data structures", "trees" ]
null
null
When Adam gets a rooted tree (connected non-directed graph without cycles), he immediately starts coloring it. More formally, he assigns a color to each edge of the tree so that it meets the following two conditions: - There is no vertex that has more than two incident edges painted the same color. - For any two vertexes that have incident edges painted the same color (say, *c*), the path between them consists of the edges of the color *c*. Not all tree paintings are equally good for Adam. Let's consider the path from some vertex to the root. Let's call the number of distinct colors on this path the cost of the vertex. The cost of the tree's coloring will be the maximum cost among all the vertexes. Help Adam determine the minimum possible cost of painting the tree. Initially, Adam's tree consists of a single vertex that has number one and is the root. In one move Adam adds a new vertex to the already existing one, the new vertex gets the number equal to the minimum positive available integer. After each operation you need to calculate the minimum cost of coloring the resulting tree.
The first line contains integer *n* (1<=≀<=*n*<=≀<=106) β€” the number of times a new vertex is added. The second line contains *n* numbers *p**i* (1<=≀<=*p**i*<=≀<=*i*) β€” the numbers of the vertexes to which we add another vertex.
Print *n* integers β€” the minimum costs of the tree painting after each addition.
[ "11\n1 1 1 3 4 4 7 3 7 6 6\n" ]
[ "1 1 1 1 1 2 2 2 2 2 3 " ]
The figure below shows one of the possible variants to paint a tree from the sample at the last moment. The cost of the vertexes with numbers 11 and 12 equals 3. <img class="tex-graphics" src="https://espresso.codeforces.com/3e0ae59416472763f3e14b7c4a5094de154d3b50.png" style="max-width: 100.0%;max-height: 100.0%;"/>
[]
46
0
0
15,796
1,004
Sonya and Ice Cream
[ "binary search", "data structures", "dp", "greedy", "shortest paths", "trees" ]
null
null
Sonya likes ice cream very much. She eats it even during programming competitions. That is why the girl decided that she wants to open her own ice cream shops. Sonya lives in a city with $n$ junctions and $n-1$ streets between them. All streets are two-way and connect two junctions. It is possible to travel from any junction to any other using one or more streets. City Hall allows opening shops only on junctions. The girl cannot open shops in the middle of streets. Sonya has exactly $k$ friends whom she can trust. If she opens a shop, one of her friends has to work there and not to allow anybody to eat an ice cream not paying for it. Since Sonya does not want to skip an important competition, she will not work in shops personally. Sonya wants all her ice cream shops to form a simple path of the length $r$ ($1 \le r \le k$), i.e. to be located in different junctions $f_1, f_2, \dots, f_r$ and there is street between $f_i$ and $f_{i+1}$ for each $i$ from $1$ to $r-1$. The girl takes care of potential buyers, so she also wants to minimize the maximum distance between the junctions to the nearest ice cream shop. The distance between two junctions $a$ and $b$ is equal to the sum of all the street lengths that you need to pass to get from the junction $a$ to the junction $b$. So Sonya wants to minimize $$\max_{a} \min_{1 \le i \le r} d_{a,f_i}$$ where $a$ takes a value of all possible $n$ junctions, $f_i$Β β€” the junction where the $i$-th Sonya's shop is located, and $d_{x,y}$Β β€” the distance between the junctions $x$ and $y$. Sonya is not sure that she can find the optimal shops locations, that is why she is asking you to help her to open not more than $k$ shops that will form a simple path and the maximum distance between any junction and the nearest shop would be minimal.
The first line contains two integers $n$ and $k$ ($1\leq k\leq n\leq 10^5$)Β β€” the number of junctions and friends respectively. Each of the next $n-1$ lines contains three integers $u_i$, $v_i$, and $d_i$ ($1\leq u_i, v_i\leq n$, $v_i\neq u_i$, $1\leq d\leq 10^4$)Β β€” junctions that are connected by a street and the length of this street. It is guaranteed that each pair of junctions is connected by at most one street. It is guaranteed that you can get from any junctions to any other.
Print one numberΒ β€” the minimal possible maximum distance that you need to pass to get from any junction to the nearest ice cream shop. Sonya's shops must form a simple path and the number of shops must be at most $k$.
[ "6 2\n1 2 3\n2 3 4\n4 5 2\n4 6 3\n2 4 6\n", "10 3\n1 2 5\n5 7 2\n3 2 6\n10 6 3\n3 8 1\n6 4 2\n4 1 6\n6 9 4\n5 2 5\n" ]
[ "4\n", "7\n" ]
In the first example, you can choose the path 2-4, so the answer will be 4. In the second example, you can choose the path 4-1-2, so the answer will be 7.
[ { "input": "6 2\n1 2 3\n2 3 4\n4 5 2\n4 6 3\n2 4 6", "output": "4" }, { "input": "10 3\n1 2 5\n5 7 2\n3 2 6\n10 6 3\n3 8 1\n6 4 2\n4 1 6\n6 9 4\n5 2 5", "output": "7" }, { "input": "8 4\n8 7 4\n5 6 7\n7 3 4\n8 4 3\n1 2 1\n2 3 5\n5 4 4", "output": "10" }, { "input": "1 1", "output": "0" } ]
155
0
0
15,798
534
Handshakes
[ "binary search", "constructive algorithms", "data structures", "greedy" ]
null
null
On February, 30th *n* students came in the Center for Training Olympiad Programmers (CTOP) of the Berland State University. They came one by one, one after another. Each of them went in, and before sitting down at his desk, greeted with those who were present in the room by shaking hands. Each of the students who came in stayed in CTOP until the end of the day and never left. At any time any three students could join together and start participating in a team contest, which lasted until the end of the day. The team did not distract from the contest for a minute, so when another student came in and greeted those who were present, he did not shake hands with the members of the contest writing team. Each team consisted of exactly three students, and each student could not become a member of more than one team. Different teams could start writing contest at different times. Given how many present people shook the hands of each student, get a possible order in which the students could have come to CTOP. If such an order does not exist, then print that this is impossible. Please note that some students could work independently until the end of the day, without participating in a team contest.
The first line contains integer *n* (1<=≀<=*n*<=≀<=2Β·105) β€” the number of students who came to CTOP. The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=&lt;<=*n*), where *a**i* is the number of students with who the *i*-th student shook hands.
If the sought order of students exists, print in the first line "Possible" and in the second line print the permutation of the students' numbers defining the order in which the students entered the center. Number *i* that stands to the left of number *j* in this permutation means that the *i*-th student came earlier than the *j*-th student. If there are multiple answers, print any of them. If the sought order of students doesn't exist, in a single line print "Impossible".
[ "5\n2 1 3 0 1\n", "9\n0 2 3 4 1 1 0 2 2\n", "4\n0 2 1 1\n" ]
[ "Possible\n4 5 1 3 2 ", "Possible\n7 5 2 1 6 8 3 4 9", "Impossible\n" ]
In the first sample from the statement the order of events could be as follows: - student 4 comes in (*a*<sub class="lower-index">4</sub> = 0), he has no one to greet; - student 5 comes in (*a*<sub class="lower-index">5</sub> = 1), he shakes hands with student 4; - student 1 comes in (*a*<sub class="lower-index">1</sub> = 2), he shakes hands with two students (students 4, 5); - student 3 comes in (*a*<sub class="lower-index">3</sub> = 3), he shakes hands with three students (students 4, 5, 1); - students 4, 5, 3 form a team and start writing a contest; - student 2 comes in (*a*<sub class="lower-index">2</sub> = 1), he shakes hands with one student (number 1). In the second sample from the statement the order of events could be as follows: - student 7 comes in (*a*<sub class="lower-index">7</sub> = 0), he has nobody to greet; - student 5 comes in (*a*<sub class="lower-index">5</sub> = 1), he shakes hands with student 7; - student 2 comes in (*a*<sub class="lower-index">2</sub> = 2), he shakes hands with two students (students 7, 5); - students 7, 5, 2 form a team and start writing a contest; - student 1 comes in(*a*<sub class="lower-index">1</sub> = 0), he has no one to greet (everyone is busy with the contest); - student 6 comes in (*a*<sub class="lower-index">6</sub> = 1), he shakes hands with student 1; - student 8 comes in (*a*<sub class="lower-index">8</sub> = 2), he shakes hands with two students (students 1, 6); - student 3 comes in (*a*<sub class="lower-index">3</sub> = 3), he shakes hands with three students (students 1, 6, 8); - student 4 comes in (*a*<sub class="lower-index">4</sub> = 4), he shakes hands with four students (students 1, 6, 8, 3); - students 8, 3, 4 form a team and start writing a contest; - student 9 comes in (*a*<sub class="lower-index">9</sub> = 2), he shakes hands with two students (students 1, 6). In the third sample from the statement the order of events is restored unambiguously: - student 1 comes in (*a*<sub class="lower-index">1</sub> = 0), he has no one to greet; - student 3 comes in (or student 4) (*a*<sub class="lower-index">3</sub> = *a*<sub class="lower-index">4</sub> = 1), he shakes hands with student 1; - student 2 comes in (*a*<sub class="lower-index">2</sub> = 2), he shakes hands with two students (students 1, 3 (or 4)); - the remaining student 4 (or student 3), must shake one student's hand (*a*<sub class="lower-index">3</sub> = *a*<sub class="lower-index">4</sub> = 1) but it is impossible as there are only two scenarios: either a team formed and he doesn't greet anyone, or he greets all the three present people who work individually.
[ { "input": "5\n2 1 3 0 1", "output": "Possible\n4 5 1 3 2 " }, { "input": "9\n0 2 3 4 1 1 0 2 2", "output": "Possible\n7 6 9 3 4 8 1 5 2 " }, { "input": "4\n0 2 1 1", "output": "Impossible" }, { "input": "5\n1 0 2 1 0", "output": "Possible\n5 4 3 2 1 " }, { "input": "1\n0", "output": "Possible\n1 " }, { "input": "5\n3 0 4 1 2", "output": "Possible\n2 4 5 1 3 " }, { "input": "3\n1 0 0", "output": "Impossible" }, { "input": "7\n3 0 0 4 2 2 1", "output": "Possible\n3 7 6 1 4 5 2 " }, { "input": "10\n1 0 2 3 3 0 4 4 2 5", "output": "Possible\n6 1 9 5 8 10 4 7 3 2 " }, { "input": "7\n2 4 3 5 1 6 0", "output": "Possible\n7 5 1 3 2 4 6 " }, { "input": "10\n6 2 8 1 4 5 7 3 9 3", "output": "Impossible" }, { "input": "5\n2 0 3 1 1", "output": "Possible\n2 5 1 3 4 " }, { "input": "7\n2 2 3 3 4 0 1", "output": "Possible\n6 7 2 4 5 1 3 " }, { "input": "11\n3 1 1 1 2 2 0 0 2 1 3", "output": "Possible\n8 10 9 11 4 6 1 3 5 7 2 " }, { "input": "6\n0 1 2 1 2 0", "output": "Possible\n6 4 5 1 2 3 " }, { "input": "13\n1 2 0 4 2 1 0 2 0 0 2 3 1", "output": "Possible\n10 13 11 12 4 8 9 6 5 7 1 2 3 " }, { "input": "12\n1 1 0 2 1 1 2 2 0 2 0 0", "output": "Possible\n12 6 10 11 5 8 9 2 7 3 1 4 " }, { "input": "16\n4 7 7 9 1 10 8 3 2 5 11 0 9 9 8 6", "output": "Possible\n12 5 9 8 1 10 16 3 15 14 6 11 13 2 7 4 " }, { "input": "10\n3 4 5 2 7 1 3 0 6 5", "output": "Possible\n8 6 4 7 2 10 9 5 3 1 " }, { "input": "11\n1 1 3 2 2 2 0 1 0 1 3", "output": "Possible\n9 10 6 11 8 5 3 2 4 7 1 " }, { "input": "6\n2 0 2 0 1 1", "output": "Possible\n4 6 3 2 5 1 " }, { "input": "123\n114 105 49 11 115 106 92 74 101 86 39 116 5 48 87 19 40 25 22 42 111 75 84 68 57 119 46 41 23 58 90 102 3 10 78 108 2 21 122 121 120 64 85 32 34 71 4 110 36 30 18 81 52 76 47 33 54 45 29 17 100 27 70 31 89 99 61 6 9 53 20 35 0 79 112 55 96 51 16 62 72 26 44 15 80 82 8 109 14 63 28 43 60 1 113 59 91 103 65 88 94 12 95 104 13 77 69 98 97 24 83 50 73 37 118 56 66 93 117 38 67 107 7", "output": "Possible\n73 94 37 33 47 13 68 123 87 69 34 4 102 105 89 84 79 60 51 16 71 38 19 29 110 18 82 62 91 59 50 64 44 56 45 72 49 114 120 11 17 28 20 92 83 58 27 55 14 3 112 78 53 70 57 76 116 25 30 96 93 67 80 90 42 99 117 121 24 107 63 46 81 113 8 22 54 106 35 74 85 52 86 111 23 43 10 15 100 65 31 97 7 118 101 103 77 109 108 66 61 9 32 98 104 2 6 122 36 88 48 21 75 95 1 5 12 119 115 26 41 40 39 " }, { "input": "113\n105 36 99 43 3 100 60 28 24 46 53 31 50 18 2 35 52 84 30 81 51 108 19 93 1 39 62 79 61 97 27 87 65 90 57 16 80 111 56 102 95 112 8 25 44 10 49 26 70 54 41 22 106 107 63 59 67 33 68 11 12 82 40 89 58 109 92 71 4 69 37 14 48 103 77 64 87 110 66 55 98 23 13 38 15 6 75 78 29 88 74 96 9 91 85 20 42 0 17 86 5 104 76 7 73 32 34 47 101 83 45 21 94", "output": "Impossible" }, { "input": "54\n4 17 18 15 6 0 12 19 20 21 19 14 23 20 7 19 0 2 13 18 2 1 0 1 0 5 11 10 1 16 8 21 20 1 16 1 1 0 15 2 22 2 2 2 18 0 3 9 1 20 19 14 0 2", "output": "Possible\n53 49 54 47 1 26 5 15 31 48 28 27 7 19 52 39 35 2 45 51 50 32 41 13 10 16 33 20 11 14 3 8 9 4 30 12 46 37 44 38 36 43 25 34 42 23 29 40 17 24 21 6 22 18 " }, { "input": "124\n3 10 6 5 21 23 4 6 9 1 9 3 14 27 10 19 29 17 24 17 5 12 20 4 16 2 24 4 21 14 9 22 11 27 4 9 2 11 6 5 6 6 11 4 3 22 6 10 5 15 5 2 16 13 19 8 25 4 18 10 9 5 13 10 19 26 2 3 9 4 7 12 20 20 4 19 11 33 17 25 2 28 15 8 8 15 30 14 18 11 5 10 18 17 18 31 9 7 1 16 3 6 15 24 4 17 10 26 4 23 22 11 19 15 7 26 28 18 32 0 23 8 6 13", "output": "Possible\n120 99 81 101 109 91 123 115 122 97 107 112 72 124 88 114 100 106 118 113 74 29 111 121 104 80 116 34 117 17 87 96 119 78 82 108 14 57 66 27 46 110 19 32 6 5 76 73 95 65 23 93 55 94 89 16 79 59 53 20 103 25 18 86 63 30 83 54 13 50 92 90 22 64 77 69 60 43 61 48 38 36 15 33 31 2 85 11 98 84 9 71 56 102 105 62 47 75 51 42 70 49 41 58 40 39 44 21 8 35 4 3 28 67 68 24 52 45 7 37 12 10 26 1 " }, { "input": "69\n1 5 8 5 4 10 6 0 0 4 5 5 3 1 5 5 9 4 5 7 6 2 0 4 6 2 2 8 2 13 3 7 4 4 1 4 6 1 5 9 6 0 3 3 8 6 7 3 6 7 37 1 8 14 4 2 7 5 4 5 4 2 3 6 5 11 12 3 3", "output": "Impossible" }, { "input": "185\n28 4 4 26 15 21 14 35 22 28 26 24 2 35 21 34 1 23 35 10 6 16 31 0 30 9 18 33 1 22 24 26 22 10 8 27 14 33 16 16 26 22 1 28 32 1 35 12 31 0 21 6 6 5 29 27 1 29 23 22 30 19 37 17 2 2 2 25 3 23 28 0 3 31 34 5 2 23 27 7 26 25 33 27 15 31 31 4 3 21 1 1 23 30 0 13 24 33 26 5 1 17 23 25 36 0 20 0 32 2 2 36 24 26 25 33 35 2 26 27 37 25 12 27 30 21 34 33 29 1 12 1 25 2 29 36 3 11 2 23 25 29 2 32 30 18 3 18 26 19 4 20 23 38 22 13 25 0 1 24 2 25 0 24 0 27 36 1 2 21 1 31 0 17 11 0 28 7 20 5 5 32 37 28 34", "output": "Possible\n176 171 169 147 151 181 53 178 35 26 34 175 131 156 37 85 40 174 148 150 179 170 155 153 164 162 149 166 184 142 145 172 182 128 185 117 167 183 154 136 121 47 112 63 19 105 127 14 116 75 8 98 16 144 83 87 109 38 86 45 28 74 135 125 49 129 94 23 58 61 177 55 25 71 119 124 44 114 120 10 99 84 1 81 79 157 41 56 141 32 36 133 11 160 122 4 113 115 140 97 104 103 31 82 93 12 68 78 126 60 70 90 42 59 51 33 18 15 30 152 6 9 107 146 62 102 27 39 64 5 22 7 123 96 138 48 20 180 52 80 100 21 88 76 137 3 54 ..." }, { "input": "104\n1 0 0 0 2 6 4 8 1 4 2 11 2 0 2 0 0 1 2 0 5 0 3 6 8 5 0 5 1 2 8 1 2 8 9 2 0 4 1 0 2 1 9 5 1 7 7 6 1 0 6 2 3 2 2 0 8 3 9 7 1 7 0 2 3 5 0 5 6 10 0 1 1 2 8 4 4 10 3 4 10 2 1 6 7 1 7 2 1 9 1 0 1 1 2 1 11 2 6 0 2 2 9 7", "output": "Possible\n100 96 102 79 80 68 99 104 75 103 81 97 90 78 12 59 70 57 43 87 34 35 85 31 84 62 25 69 60 8 51 47 66 48 46 44 24 77 28 6 76 26 65 38 21 58 10 101 53 7 98 23 94 95 92 93 88 71 91 82 67 89 74 63 86 64 56 83 55 50 73 54 40 72 52 37 61 41 27 49 36 22 45 33 20 42 30 17 39 19 16 32 15 14 29 13 4 18 11 3 9 5 2 1 " }, { "input": "93\n5 10 0 2 0 3 4 21 17 9 13 2 16 11 10 0 13 5 8 14 10 0 6 19 20 8 12 1 8 11 19 7 8 3 8 10 12 2 9 1 10 5 4 9 4 15 5 8 16 11 10 17 11 3 12 7 9 10 1 7 6 4 10 8 9 10 9 18 9 9 4 5 11 2 12 10 11 9 17 12 1 6 8 15 13 2 11 6 7 10 3 5 12", "output": "Possible\n22 81 86 91 71 92 88 89 83 78 90 87 93 85 20 84 49 79 68 31 25 8 24 52 46 13 9 80 17 77 75 11 73 55 76 53 37 66 50 27 63 30 70 58 14 69 51 64 67 41 48 65 36 35 57 21 33 44 15 29 39 2 26 10 60 19 82 56 72 61 32 47 23 62 42 54 45 18 34 43 1 6 7 74 16 59 38 5 40 12 3 28 4 " }, { "input": "99\n6 13 9 8 5 12 1 6 13 12 11 15 2 5 10 12 13 9 13 4 8 10 11 11 7 2 9 2 13 10 3 0 12 11 14 12 9 9 11 9 1 11 7 12 8 9 6 10 13 14 0 8 8 10 12 8 9 14 5 12 4 9 7 10 8 7 12 14 13 0 10 10 8 12 10 12 6 14 11 10 1 5 8 11 10 13 10 11 7 4 3 3 2 11 8 9 13 12 4", "output": "Possible\n70 81 93 92 99 82 77 89 95 96 87 94 98 97 78 12 86 68 76 69 58 74 49 50 67 29 35 60 19 88 55 17 84 44 9 79 36 2 42 33 85 39 16 80 34 10 75 24 6 72 23 62 71 11 57 64 83 46 54 73 40 48 65 38 30 56 37 22 53 27 15 52 18 66 45 3 63 21 47 43 4 8 25 59 1 90 14 91 61 5 31 20 28 51 41 26 32 7 13 " }, { "input": "153\n5 4 3 3 0 5 5 5 3 3 7 3 5 2 7 4 0 5 2 0 4 6 3 3 2 1 4 3 2 0 8 1 7 6 8 7 5 6 4 5 2 4 0 4 4 2 4 3 3 4 5 6 3 5 5 6 4 4 6 7 1 1 8 4 2 4 3 5 1 4 9 6 3 3 4 8 4 2 4 6 5 9 5 4 1 3 10 3 3 4 2 1 2 7 4 3 6 5 6 6 4 7 6 1 4 4 2 8 5 5 5 3 6 6 7 1 4 8 4 8 5 5 3 9 5 2 2 8 5 6 4 2 0 2 4 3 7 3 3 8 6 2 4 3 7 2 6 1 3 7 2 2 2", "output": "Possible\n133 148 153 149 143 129 147 150 140 124 87 128 82 145 120 71 137 118 141 115 108 130 102 76 114 94 63 113 60 35 103 36 31 100 33 125 99 15 122 97 11 121 80 135 111 72 131 110 59 119 109 56 117 98 52 106 83 38 105 81 34 101 68 22 95 55 144 90 54 139 84 51 138 79 40 136 77 37 123 75 18 112 70 13 96 66 8 89 64 7 88 58 6 86 57 1 74 50 152 73 47 151 67 45 146 53 44 142 49 42 134 48 39 132 28 27 127 24 21 126 23 16 107 12 2 93 10 116 91 9 104 78 4 92 65 3 85 46 43 69 41 30 62 29 20 61 25 17 32 19 5 26 ..." }, { "input": "169\n1 2 1 2 2 4 1 0 0 1 0 1 6 7 5 3 0 1 4 0 3 4 1 5 3 1 3 0 2 1 1 3 1 2 0 0 2 4 0 0 2 2 1 1 2 1 1 1 0 3 2 4 5 5 5 0 0 1 3 1 2 0 0 2 1 0 3 1 3 2 6 1 2 0 0 3 1 2 0 2 2 3 1 1 2 2 2 3 3 2 1 1 0 2 0 4 4 3 3 1 4 2 2 4 2 2 1 2 3 0 1 5 1 0 3 1 2 1 1 3 2 3 4 2 3 6 2 3 3 1 4 4 5 2 0 1 2 2 1 0 2 2 2 2 7 2 2 3 3 8 3 5 2 1 2 1 2 5 3 0 3 1 2 2 1 1 2 4 3", "output": "Possible\n160 166 167 169 168 158 126 145 150 71 14 152 13 132 133 161 131 112 159 123 55 151 104 54 149 101 53 148 97 24 129 96 15 128 52 164 125 38 163 122 22 157 120 19 155 115 6 153 109 165 147 99 162 146 98 156 144 89 154 143 88 139 142 82 136 141 76 130 138 69 119 137 67 118 134 59 116 127 50 113 124 32 111 121 27 107 117 25 100 108 21 92 106 16 91 105 140 84 103 135 83 102 114 77 94 110 72 90 95 68 87 93 65 86 79 60 85 75 58 81 74 48 80 66 47 78 63 46 73 62 44 70 57 43 64 56 33 61 49 31 51 40 30 45 ..." }, { "input": "92\n0 0 2 0 1 1 2 1 2 0 2 1 1 2 2 0 1 1 0 2 1 2 1 1 3 2 2 2 2 0 1 2 1 0 0 0 1 1 0 3 0 1 0 1 2 1 0 2 2 1 2 1 0 0 1 1 2 1 2 0 0 1 2 2 0 2 0 0 2 1 1 2 1 0 2 2 4 0 0 0 2 0 1 1 0 2 0 2 0 1 2 1", "output": "Possible\n89 92 91 40 77 88 25 90 86 87 84 81 85 83 76 82 73 75 80 71 72 79 70 69 78 62 66 74 58 64 68 56 63 67 55 59 65 52 57 61 50 51 60 46 49 54 44 48 53 42 45 47 38 32 43 37 29 41 33 28 39 31 27 36 24 26 35 23 22 34 21 20 30 18 15 19 17 14 16 13 11 10 12 9 4 8 7 2 6 3 1 5 " }, { "input": "12\n0 1 2 3 4 5 6 7 8 0 1 2", "output": "Possible\n10 11 12 4 5 6 7 8 9 1 2 3 " } ]
93
0
0
15,831
576
Points on Plane
[ "constructive algorithms", "divide and conquer", "geometry", "greedy", "sortings" ]
null
null
On a plane are *n* points (*x**i*, *y**i*) with integer coordinates between 0 and 106. The distance between the two points with numbers *a* and *b* is said to be the following value: (the distance calculated by such formula is called Manhattan distance). We call a hamiltonian path to be some permutation *p**i* of numbers from 1 to *n*. We say that the length of this path is value . Find some hamiltonian path with a length of no more than 25<=Γ—<=108. Note that you do not have to minimize the path length.
The first line contains integer *n* (1<=≀<=*n*<=≀<=106). The *i*<=+<=1-th line contains the coordinates of the *i*-th point: *x**i* and *y**i* (0<=≀<=*x**i*,<=*y**i*<=≀<=106). It is guaranteed that no two points coincide.
Print the permutation of numbers *p**i* from 1 to *n* β€” the sought Hamiltonian path. The permutation must meet the inequality . If there are multiple possible answers, print any of them. It is guaranteed that the answer exists.
[ "5\n0 7\n8 10\n3 4\n5 0\n9 12\n" ]
[ "4 3 1 2 5 \n" ]
In the sample test the total distance is: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/c772e61c616e1c27114e3facb9e6db6c5cf93b82.png" style="max-width: 100.0%;max-height: 100.0%;"/> (|5 - 3| + |0 - 4|) + (|3 - 0| + |4 - 7|) + (|0 - 8| + |7 - 10|) + (|8 - 9| + |10 - 12|) = 2 + 4 + 3 + 3 + 8 + 3 + 1 + 2 = 26
[]
2,000
39,116,800
0
15,850
981
Addition on Segments
[ "bitmasks", "data structures", "divide and conquer", "dp" ]
null
null
Grisha come to a contest and faced the following problem. You are given an array of size $n$, initially consisting of zeros. The elements of the array are enumerated from $1$ to $n$. You perform $q$ operations on the array. The $i$-th operation is described with three integers $l_i$, $r_i$ and $x_i$ ($1 \leq l_i \leq r_i \leq n$, $1 \leq x_i \leq n$) and means that you should add $x_i$ to each of the elements with indices $l_i, l_i + 1, \ldots, r_i$. After all operations you should find the maximum in the array. Grisha is clever, so he solved the problem quickly. However something went wrong inside his head and now he thinks of the following question: "consider we applied some subset of the operations to the array. What are the possible values of the maximum in the array?" Help Grisha, find all integers $y$ between $1$ and $n$ such that if you apply some subset (possibly empty) of the operations, then the maximum in the array becomes equal to $y$.
The first line contains two integers $n$ and $q$ ($1 \leq n, q \leq 10^{4}$)Β β€” the length of the array and the number of queries in the initial problem. The following $q$ lines contain queries, one per line. The $i$-th of these lines contains three integers $l_i$, $r_i$ and $x_i$ ($1 \leq l_i \leq r_i \leq n$, $1 \leq x_i \leq n$), denoting a query of adding $x_i$ to the segment from $l_i$-th to $r_i$-th elements of the array, inclusive.
In the first line print the only integer $k$, denoting the number of integers from $1$ to $n$, inclusive, that can be equal to the maximum in the array after applying some subset (possibly empty) of the given operations. In the next line print these $k$ integers from $1$ to $n$Β β€” the possible values of the maximum. Print these integers in increasing order.
[ "4 3\n1 3 1\n2 4 2\n3 4 4\n", "7 2\n1 5 1\n3 7 2\n", "10 3\n1 1 2\n1 1 3\n1 1 6\n" ]
[ "4\n1 2 3 4 \n", "3\n1 2 3 \n", "6\n2 3 5 6 8 9 \n" ]
Consider the first example. If you consider the subset only of the first query, the maximum is equal to $1$. If you take only the second query, the maximum equals to $2$. If you take the first two queries, the maximum becomes $3$. If you take only the fourth query, the maximum becomes $4$. If you take the fourth query and something more, the maximum becomes greater that $n$, so you shouldn't print it. In the second example you can take the first query to obtain $1$. You can take only the second query to obtain $2$. You can take all queries to obtain $3$. In the third example you can obtain the following maximums: - You can achieve the maximim of $2$ by using queries: $(1)$. - You can achieve the maximim of $3$ by using queries: $(2)$. - You can achieve the maximim of $5$ by using queries: $(1, 2)$. - You can achieve the maximim of $6$ by using queries: $(3)$. - You can achieve the maximim of $8$ by using queries: $(1, 3)$. - You can achieve the maximim of $9$ by using queries: $(2, 3)$.
[ { "input": "4 3\n1 3 1\n2 4 2\n3 4 4", "output": "4\n1 2 3 4 " }, { "input": "7 2\n1 5 1\n3 7 2", "output": "3\n1 2 3 " }, { "input": "10 3\n1 1 2\n1 1 3\n1 1 6", "output": "6\n2 3 5 6 8 9 " }, { "input": "45 5\n37 38 16\n5 7 34\n1 42 31\n8 27 19\n15 28 39", "output": "5\n16 19 31 34 39 " }, { "input": "7010 10\n1467 2828 4742\n560 3268 3751\n1180 5370 6723\n907 3766 1380\n4610 5672 5430\n4867 5179 4868\n1890 3860 1037\n253 4853 5056\n480 5139 5329\n3764 4677 4777", "output": "22\n1037 1380 2417 3751 4742 4777 4788 4868 5056 5131 5329 5430 5779 5814 6093 6122 6157 6168 6366 6436 6709 6723 " }, { "input": "1 1\n1 1 1", "output": "1\n1 " }, { "input": "1010 10\n5 615 290\n146 940 131\n8 306 381\n387 478 417\n236 290 182\n258 288 117\n343 431 831\n766 775 199\n102 857 520\n216 913 687", "output": "63\n117 131 182 199 248 290 299 313 330 381 407 417 421 430 472 498 512 520 538 548 563 589 603 629 637 651 671 680 687 694 702 707 719 720 768 788 802 804 810 811 818 819 831 833 838 850 853 869 886 901 919 927 935 937 941 950 962 970 977 984 986 992 1000 " }, { "input": "4010 10\n909 1610 2428\n744 1380 2029\n658 781 1696\n2427 3132 2364\n2631 3975 3741\n1033 3693 1038\n117 3110 3815\n1962 2104 699\n454 2041 624\n2738 3231 3490", "output": "22\n624 699 1038 1323 1662 1696 1737 2029 2320 2361 2364 2428 2653 3052 3067 3402 3466 3490 3691 3725 3741 3815 " }, { "input": "10000 10\n2001 3111 6776\n2635 6081 3143\n5925 9279 4959\n6326 7610 2701\n5210 5461 8141\n2922 9252 7377\n6705 8478 597\n5556 7112 911\n652 9817 4874\n1832 8653 4209", "output": "49\n597 911 1508 2701 3143 3298 3612 4054 4209 4806 4874 4959 5120 5471 5556 5717 5785 5870 6382 6467 6776 6910 7352 7377 7507 7575 7660 7821 7974 8017 8102 8141 8172 8257 8263 8288 8418 8486 8571 8885 8928 9013 9083 9168 9680 9765 9833 9919 9994 " }, { "input": "6010 10\n38 2837 4404\n515 5033 887\n2419 3000 3320\n4422 5834 551\n220 1474 2206\n638 5884 224\n1549 1949 5525\n52 4891 420\n4503 4718 1495\n1300 4400 3233", "output": "69\n224 420 551 644 775 887 971 1111 1195 1307 1438 1495 1531 1662 1719 1858 1915 2046 2082 2139 2206 2270 2382 2430 2466 2606 2626 2690 2802 2850 2933 3026 3093 3157 3233 3317 3320 3353 3457 3513 3544 3577 3653 3737 3740 3877 3964 4120 4207 4344 4404 4431 4540 4627 4628 4764 4824 4851 5048 5291 5439 5515 5525 5663 5711 5749 5859 5935 5945 " }, { "input": "10 10\n1 9 7\n2 6 4\n7 8 1\n3 10 10\n3 5 7\n1 6 10\n6 6 3\n3 7 6\n2 2 9\n4 9 1", "output": "10\n1 2 3 4 5 6 7 8 9 10 " }, { "input": "9010 10\n2861 7587 7658\n1740 4549 8685\n7214 7667 6405\n1895 8261 2184\n2015 3497 5088\n1279 3095 1684\n32 7651 189\n7203 7950 2556\n2566 7868 1754\n2228 8147 5246", "output": "57\n189 1684 1754 1873 1943 2184 2373 2556 2745 3438 3627 3868 3938 4057 4127 4310 4499 4740 4929 5088 5246 5277 5435 5622 5811 6405 6494 6594 6683 6772 6842 6930 6961 7000 7031 7119 7189 7272 7430 7461 7619 7658 7802 7847 7991 8159 8348 8526 8589 8684 8685 8715 8778 8873 8874 8956 8961 " }, { "input": "5010 10\n1948 4159 3465\n2513 4745 4772\n1237 3781 1549\n497 1777 4549\n955 3065 3813\n184 4048 538\n439 2305 3771\n414 1654 484\n2543 4334 4528\n215 1500 1916", "output": "26\n484 538 1022 1549 1916 2033 2087 2400 2454 2571 2938 3465 3771 3813 3949 4003 4255 4297 4309 4351 4487 4528 4549 4772 4793 4835 " }, { "input": "3010 10\n1404 1948 2739\n227 505 2345\n1587 2035 765\n585 1673 2969\n89 379 2479\n989 1330 924\n1858 2968 29\n233 2335 1613\n1948 2351 2927\n2131 2292 360", "output": "20\n29 360 389 765 794 924 1613 1642 1973 2002 2345 2378 2407 2479 2537 2739 2768 2927 2956 2969 " }, { "input": "2010 10\n876 1434 1469\n1239 1389 600\n636 853 504\n242 1533 4\n1068 1256 823\n127 995 1468\n26 348 1967\n1016 1951 257\n13 924 45\n8 849 1463", "output": "39\n4 45 49 257 261 504 508 549 553 600 604 823 827 857 861 1080 1084 1423 1427 1463 1467 1468 1469 1472 1473 1508 1512 1513 1514 1517 1518 1680 1684 1726 1730 1967 1971 1972 1976 " }, { "input": "8010 10\n509 3546 5695\n383 511 2786\n4540 6912 6764\n1362 3766 4791\n571 2160 5836\n742 4553 1424\n836 7944 5373\n376 3547 4762\n4313 7876 3868\n539 1383 7182", "output": "17\n1424 2786 3868 4762 4791 5292 5373 5695 5836 6186 6215 6764 6797 7119 7182 7260 7548 " } ]
202
28,774,400
-1
15,879
43
Journey
[ "brute force", "constructive algorithms", "implementation" ]
D. Journey
2
256
The territory of Berland is represented by a rectangular field *n*<=Γ—<=*m* in size. The king of Berland lives in the capital, located on the upper left square (1,<=1). The lower right square has coordinates (*n*,<=*m*). One day the king decided to travel through the whole country and return back to the capital, having visited every square (except the capital) exactly one time. The king must visit the capital exactly two times, at the very beginning and at the very end of his journey. The king can only move to the side-neighboring squares. However, the royal advise said that the King possibly will not be able to do it. But there is a way out β€” one can build the system of one way teleporters between some squares so that the king could fulfill his plan. No more than one teleporter can be installed on one square, every teleporter can be used any number of times, however every time it is used, it transports to the same given for any single teleporter square. When the king reaches a square with an installed teleporter he chooses himself whether he is or is not going to use the teleport. What minimum number of teleporters should be installed for the king to complete the journey? You should also compose the journey path route for the king.
The first line contains two space-separated integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=100,<=2<=≀<= *n* Β· *m*) β€” the field size. The upper left square has coordinates (1,<=1), and the lower right square has coordinates of (*n*,<=*m*).
On the first line output integer *k* β€” the minimum number of teleporters. Then output *k* lines each containing 4 integers *x*1 *y*1 *x*2 *y*2 (1<=≀<=*x*1,<=*x*2<=≀<=*n*,<=1<=≀<=*y*1,<=*y*2<=≀<=*m*) β€” the coordinates of the square where the teleporter is installed (*x*1,<=*y*1), and the coordinates of the square where the teleporter leads (*x*2,<=*y*2). Then print *nm*<=+<=1 lines containing 2 numbers each β€” the coordinates of the squares in the order in which they are visited by the king. The travel path must start and end at (1,<=1). The king can move to side-neighboring squares and to the squares where a teleporter leads. Besides, he also should visit the capital exactly two times and he should visit other squares exactly one time.
[ "2 2\n", "3 3\n" ]
[ "0\n1 1\n1 2\n2 2\n2 1\n1 1\n", "1\n3 3 1 1\n1 1\n1 2\n1 3\n2 3\n2 2\n2 1\n3 1\n3 2\n3 3\n1 1\n" ]
none
[ { "input": "2 2", "output": "0\n1 1\n1 2\n2 2\n2 1\n1 1" }, { "input": "3 3", "output": "1\n3 3 1 1\n1 1\n1 2\n1 3\n2 3\n2 2\n2 1\n3 1\n3 2\n3 3\n1 1" }, { "input": "3 2", "output": "0\n1 1\n2 1\n3 1\n3 2\n2 2\n1 2\n1 1" }, { "input": "2 3", "output": "0\n1 1\n1 2\n1 3\n2 3\n2 2\n2 1\n1 1" }, { "input": "3 4", "output": "0\n1 1\n2 1\n3 1\n3 2\n2 2\n2 3\n3 3\n3 4\n2 4\n1 4\n1 3\n1 2\n1 1" }, { "input": "6 3", "output": "0\n1 1\n1 2\n1 3\n2 3\n2 2\n3 2\n3 3\n4 3\n4 2\n5 2\n5 3\n6 3\n6 2\n6 1\n5 1\n4 1\n3 1\n2 1\n1 1" }, { "input": "4 3", "output": "0\n1 1\n1 2\n1 3\n2 3\n2 2\n3 2\n3 3\n4 3\n4 2\n4 1\n3 1\n2 1\n1 1" }, { "input": "1 2", "output": "0\n1 1\n1 2\n1 1" }, { "input": "1 3", "output": "1\n1 3 1 1\n1 1\n1 2\n1 3\n1 1" }, { "input": "1 99", "output": "1\n1 99 1 1\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n1 8..." }, { "input": "1 100", "output": "1\n1 100 1 1\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n1 ..." }, { "input": "2 1", "output": "0\n1 1\n2 1\n1 1" }, { "input": "3 1", "output": "1\n3 1 1 1\n1 1\n2 1\n3 1\n1 1" }, { "input": "99 1", "output": "1\n99 1 1 1\n1 1\n2 1\n3 1\n4 1\n5 1\n6 1\n7 1\n8 1\n9 1\n10 1\n11 1\n12 1\n13 1\n14 1\n15 1\n16 1\n17 1\n18 1\n19 1\n20 1\n21 1\n22 1\n23 1\n24 1\n25 1\n26 1\n27 1\n28 1\n29 1\n30 1\n31 1\n32 1\n33 1\n34 1\n35 1\n36 1\n37 1\n38 1\n39 1\n40 1\n41 1\n42 1\n43 1\n44 1\n45 1\n46 1\n47 1\n48 1\n49 1\n50 1\n51 1\n52 1\n53 1\n54 1\n55 1\n56 1\n57 1\n58 1\n59 1\n60 1\n61 1\n62 1\n63 1\n64 1\n65 1\n66 1\n67 1\n68 1\n69 1\n70 1\n71 1\n72 1\n73 1\n74 1\n75 1\n76 1\n77 1\n78 1\n79 1\n80 1\n81 1\n82 1\n83 1\n84 1\n85 ..." }, { "input": "100 1", "output": "1\n100 1 1 1\n1 1\n2 1\n3 1\n4 1\n5 1\n6 1\n7 1\n8 1\n9 1\n10 1\n11 1\n12 1\n13 1\n14 1\n15 1\n16 1\n17 1\n18 1\n19 1\n20 1\n21 1\n22 1\n23 1\n24 1\n25 1\n26 1\n27 1\n28 1\n29 1\n30 1\n31 1\n32 1\n33 1\n34 1\n35 1\n36 1\n37 1\n38 1\n39 1\n40 1\n41 1\n42 1\n43 1\n44 1\n45 1\n46 1\n47 1\n48 1\n49 1\n50 1\n51 1\n52 1\n53 1\n54 1\n55 1\n56 1\n57 1\n58 1\n59 1\n60 1\n61 1\n62 1\n63 1\n64 1\n65 1\n66 1\n67 1\n68 1\n69 1\n70 1\n71 1\n72 1\n73 1\n74 1\n75 1\n76 1\n77 1\n78 1\n79 1\n80 1\n81 1\n82 1\n83 1\n84 1\n85..." }, { "input": "8 97", "output": "0\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n1 85\n1 86\n1..." }, { "input": "2 4", "output": "0\n1 1\n1 2\n1 3\n1 4\n2 4\n2 3\n2 2\n2 1\n1 1" }, { "input": "2 99", "output": "0\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n1 85\n1 86\n1..." }, { "input": "2 100", "output": "0\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n1 85\n1 86\n1..." }, { "input": "4 2", "output": "0\n1 1\n1 2\n2 2\n3 2\n4 2\n4 1\n3 1\n2 1\n1 1" }, { "input": "99 2", "output": "0\n1 1\n2 1\n3 1\n4 1\n5 1\n6 1\n7 1\n8 1\n9 1\n10 1\n11 1\n12 1\n13 1\n14 1\n15 1\n16 1\n17 1\n18 1\n19 1\n20 1\n21 1\n22 1\n23 1\n24 1\n25 1\n26 1\n27 1\n28 1\n29 1\n30 1\n31 1\n32 1\n33 1\n34 1\n35 1\n36 1\n37 1\n38 1\n39 1\n40 1\n41 1\n42 1\n43 1\n44 1\n45 1\n46 1\n47 1\n48 1\n49 1\n50 1\n51 1\n52 1\n53 1\n54 1\n55 1\n56 1\n57 1\n58 1\n59 1\n60 1\n61 1\n62 1\n63 1\n64 1\n65 1\n66 1\n67 1\n68 1\n69 1\n70 1\n71 1\n72 1\n73 1\n74 1\n75 1\n76 1\n77 1\n78 1\n79 1\n80 1\n81 1\n82 1\n83 1\n84 1\n85 1\n86 1\n8..." }, { "input": "100 2", "output": "0\n1 1\n1 2\n2 2\n3 2\n4 2\n5 2\n6 2\n7 2\n8 2\n9 2\n10 2\n11 2\n12 2\n13 2\n14 2\n15 2\n16 2\n17 2\n18 2\n19 2\n20 2\n21 2\n22 2\n23 2\n24 2\n25 2\n26 2\n27 2\n28 2\n29 2\n30 2\n31 2\n32 2\n33 2\n34 2\n35 2\n36 2\n37 2\n38 2\n39 2\n40 2\n41 2\n42 2\n43 2\n44 2\n45 2\n46 2\n47 2\n48 2\n49 2\n50 2\n51 2\n52 2\n53 2\n54 2\n55 2\n56 2\n57 2\n58 2\n59 2\n60 2\n61 2\n62 2\n63 2\n64 2\n65 2\n66 2\n67 2\n68 2\n69 2\n70 2\n71 2\n72 2\n73 2\n74 2\n75 2\n76 2\n77 2\n78 2\n79 2\n80 2\n81 2\n82 2\n83 2\n84 2\n85 2\n86..." }, { "input": "100 100", "output": "0\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n1 85\n1 86\n1..." }, { "input": "99 100", "output": "0\n1 1\n2 1\n3 1\n4 1\n5 1\n6 1\n7 1\n8 1\n9 1\n10 1\n11 1\n12 1\n13 1\n14 1\n15 1\n16 1\n17 1\n18 1\n19 1\n20 1\n21 1\n22 1\n23 1\n24 1\n25 1\n26 1\n27 1\n28 1\n29 1\n30 1\n31 1\n32 1\n33 1\n34 1\n35 1\n36 1\n37 1\n38 1\n39 1\n40 1\n41 1\n42 1\n43 1\n44 1\n45 1\n46 1\n47 1\n48 1\n49 1\n50 1\n51 1\n52 1\n53 1\n54 1\n55 1\n56 1\n57 1\n58 1\n59 1\n60 1\n61 1\n62 1\n63 1\n64 1\n65 1\n66 1\n67 1\n68 1\n69 1\n70 1\n71 1\n72 1\n73 1\n74 1\n75 1\n76 1\n77 1\n78 1\n79 1\n80 1\n81 1\n82 1\n83 1\n84 1\n85 1\n86 1\n8..." }, { "input": "99 99", "output": "1\n99 99 1 1\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n1 ..." }, { "input": "100 99", "output": "0\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n1 85\n1 86\n1..." }, { "input": "33 81", "output": "1\n33 81 1 1\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n2 81\n2 80\n2 79\n2 ..." }, { "input": "11 17", "output": "1\n11 17 1 1\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n2 17\n2 16\n2 15\n2 14\n2 13\n2 12\n2 11\n2 10\n2 9\n2 8\n2 7\n2 6\n2 5\n2 4\n2 3\n2 2\n2 1\n3 1\n3 2\n3 3\n3 4\n3 5\n3 6\n3 7\n3 8\n3 9\n3 10\n3 11\n3 12\n3 13\n3 14\n3 15\n3 16\n3 17\n4 17\n4 16\n4 15\n4 14\n4 13\n4 12\n4 11\n4 10\n4 9\n4 8\n4 7\n4 6\n4 5\n4 4\n4 3\n4 2\n4 1\n5 1\n5 2\n5 3\n5 4\n5 5\n5 6\n5 7\n5 8\n5 9\n5 10\n5 11\n5 12\n5 13\n5 14\n5 15\n5 16\n5 17\n6 17\n6 16\n6 15\n6 14\n6 13\n6 ..." }, { "input": "36 1", "output": "1\n36 1 1 1\n1 1\n2 1\n3 1\n4 1\n5 1\n6 1\n7 1\n8 1\n9 1\n10 1\n11 1\n12 1\n13 1\n14 1\n15 1\n16 1\n17 1\n18 1\n19 1\n20 1\n21 1\n22 1\n23 1\n24 1\n25 1\n26 1\n27 1\n28 1\n29 1\n30 1\n31 1\n32 1\n33 1\n34 1\n35 1\n36 1\n1 1" }, { "input": "62 85", "output": "0\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n1 85\n2 85\n2..." }, { "input": "39 69", "output": "1\n39 69 1 1\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n2 69\n2 68\n2 67\n2 66\n2 65\n2 64\n2 63\n2 62\n2 61\n2 60\n2 59\n2 58\n2 57\n2 56\n2 55\n2 ..." }, { "input": "64 5", "output": "0\n1 1\n1 2\n1 3\n1 4\n1 5\n2 5\n2 4\n2 3\n2 2\n3 2\n3 3\n3 4\n3 5\n4 5\n4 4\n4 3\n4 2\n5 2\n5 3\n5 4\n5 5\n6 5\n6 4\n6 3\n6 2\n7 2\n7 3\n7 4\n7 5\n8 5\n8 4\n8 3\n8 2\n9 2\n9 3\n9 4\n9 5\n10 5\n10 4\n10 3\n10 2\n11 2\n11 3\n11 4\n11 5\n12 5\n12 4\n12 3\n12 2\n13 2\n13 3\n13 4\n13 5\n14 5\n14 4\n14 3\n14 2\n15 2\n15 3\n15 4\n15 5\n16 5\n16 4\n16 3\n16 2\n17 2\n17 3\n17 4\n17 5\n18 5\n18 4\n18 3\n18 2\n19 2\n19 3\n19 4\n19 5\n20 5\n20 4\n20 3\n20 2\n21 2\n21 3\n21 4\n21 5\n22 5\n22 4\n22 3\n22 2\n23 2\n23 3\n..." }, { "input": "90 89", "output": "0\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n1 85\n1 86\n1..." }, { "input": "67 73", "output": "1\n67 73 1 1\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n2 73\n2 72\n2 71\n2 70\n2 69\n2 68\n2 67\n2 66\n2 65\n2 64\n2 63\n2 ..." }, { "input": "40 75", "output": "0\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n2 75\n2 74\n2 73\n2 72\n2 71\n2 70\n2 69\n2 68\n2 67\n2 66\n2 65\n2..." }, { "input": "10 13", "output": "0\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n2 13\n2 12\n2 11\n2 10\n2 9\n2 8\n2 7\n2 6\n2 5\n2 4\n2 3\n2 2\n3 2\n3 3\n3 4\n3 5\n3 6\n3 7\n3 8\n3 9\n3 10\n3 11\n3 12\n3 13\n4 13\n4 12\n4 11\n4 10\n4 9\n4 8\n4 7\n4 6\n4 5\n4 4\n4 3\n4 2\n5 2\n5 3\n5 4\n5 5\n5 6\n5 7\n5 8\n5 9\n5 10\n5 11\n5 12\n5 13\n6 13\n6 12\n6 11\n6 10\n6 9\n6 8\n6 7\n6 6\n6 5\n6 4\n6 3\n6 2\n7 2\n7 3\n7 4\n7 5\n7 6\n7 7\n7 8\n7 9\n7 10\n7 11\n7 12\n7 13\n8 13\n8 12\n8 11\n8 10\n8 9\n8 8\n8 7\n8 6\n8 5\n8 4\n8..." }, { "input": "33 51", "output": "1\n33 51 1 1\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n2 51\n2 50\n2 49\n2 48\n2 47\n2 46\n2 45\n2 44\n2 43\n2 42\n2 41\n2 40\n2 39\n2 38\n2 37\n2 36\n2 35\n2 34\n2 33\n2 32\n2 31\n2 30\n2 29\n2 28\n2 27\n2 26\n2 25\n2 24\n2 23\n2 22\n2 21\n2 20\n2 19\n2 ..." }, { "input": "4 38", "output": "0\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n2 38\n2 37\n2 36\n2 35\n2 34\n2 33\n2 32\n2 31\n2 30\n2 29\n2 28\n2 27\n2 26\n2 25\n2 24\n2 23\n2 22\n2 21\n2 20\n2 19\n2 18\n2 17\n2 16\n2 15\n2 14\n2 13\n2 12\n2 11\n2 10\n2 9\n2 8\n2 7\n2 6\n2 5\n2 4\n2 3\n2 2\n3 2\n3 3\n3 4\n3 5\n3 6\n3 7\n3 8\n3 9\n3 10\n3 11\n3 12\n3 13\n3 14\n3 15\n..." }, { "input": "27 76", "output": "0\n1 1\n2 1\n3 1\n4 1\n5 1\n6 1\n7 1\n8 1\n9 1\n10 1\n11 1\n12 1\n13 1\n14 1\n15 1\n16 1\n17 1\n18 1\n19 1\n20 1\n21 1\n22 1\n23 1\n24 1\n25 1\n26 1\n27 1\n27 2\n26 2\n25 2\n24 2\n23 2\n22 2\n21 2\n20 2\n19 2\n18 2\n17 2\n16 2\n15 2\n14 2\n13 2\n12 2\n11 2\n10 2\n9 2\n8 2\n7 2\n6 2\n5 2\n4 2\n3 2\n2 2\n2 3\n3 3\n4 3\n5 3\n6 3\n7 3\n8 3\n9 3\n10 3\n11 3\n12 3\n13 3\n14 3\n15 3\n16 3\n17 3\n18 3\n19 3\n20 3\n21 3\n22 3\n23 3\n24 3\n25 3\n26 3\n27 3\n27 4\n26 4\n25 4\n24 4\n23 4\n22 4\n21 4\n20 4\n19 4\n18 4\n..." }, { "input": "98 15", "output": "0\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n2 15\n2 14\n2 13\n2 12\n2 11\n2 10\n2 9\n2 8\n2 7\n2 6\n2 5\n2 4\n2 3\n2 2\n3 2\n3 3\n3 4\n3 5\n3 6\n3 7\n3 8\n3 9\n3 10\n3 11\n3 12\n3 13\n3 14\n3 15\n4 15\n4 14\n4 13\n4 12\n4 11\n4 10\n4 9\n4 8\n4 7\n4 6\n4 5\n4 4\n4 3\n4 2\n5 2\n5 3\n5 4\n5 5\n5 6\n5 7\n5 8\n5 9\n5 10\n5 11\n5 12\n5 13\n5 14\n5 15\n6 15\n6 14\n6 13\n6 12\n6 11\n6 10\n6 9\n6 8\n6 7\n6 6\n6 5\n6 4\n6 3\n6 2\n7 2\n7 3\n7 4\n7 5\n7 6\n7 7\n7 8\n7 9\n7 10\n7..." } ]
0
0
-1
15,897
29
Traffic Lights
[ "implementation" ]
B. Traffic Lights
2
256
A car moves from point A to point B at speed *v* meters per second. The action takes place on the X-axis. At the distance *d* meters from A there are traffic lights. Starting from time 0, for the first *g* seconds the green light is on, then for the following *r* seconds the red light is on, then again the green light is on for the *g* seconds, and so on. The car can be instantly accelerated from 0 to *v* and vice versa, can instantly slow down from the *v* to 0. Consider that it passes the traffic lights at the green light instantly. If the car approaches the traffic lights at the moment when the red light has just turned on, it doesn't have time to pass it. But if it approaches the traffic lights at the moment when the green light has just turned on, it can move. The car leaves point A at the time 0. What is the minimum time for the car to get from point A to point B without breaking the traffic rules?
The first line contains integers *l*, *d*, *v*, *g*, *r* (1<=≀<=*l*,<=*d*,<=*v*,<=*g*,<=*r*<=≀<=1000,<=*d*<=&lt;<=*l*) β€” the distance between A and B (in meters), the distance from A to the traffic lights, car's speed, the duration of green light and the duration of red light.
Output a single number β€” the minimum time that the car needs to get from point A to point B. Your output must have relative or absolute error less than 10<=-<=6.
[ "2 1 3 4 5\n", "5 4 3 1 1\n" ]
[ "0.66666667\n", "2.33333333\n" ]
none
[ { "input": "2 1 3 4 5", "output": "0.66666667" }, { "input": "5 4 3 1 1", "output": "2.33333333" }, { "input": "862 33 604 888 704", "output": "1.42715232" }, { "input": "458 251 49 622 472", "output": "9.34693878" }, { "input": "772 467 142 356 889", "output": "5.43661972" }, { "input": "86 64 587 89 657", "output": "0.14650767" }, { "input": "400 333 31 823 74", "output": "12.90322581" }, { "input": "714 474 124 205 491", "output": "5.75806452" }, { "input": "29 12 569 939 259", "output": "0.05096661" }, { "input": "65 24 832 159 171", "output": "0.07812500" }, { "input": "2 1 1 1 1", "output": "3.00000000" }, { "input": "2 1 1 1 1000", "output": "1002.00000000" }, { "input": "2 1 1 1000 1", "output": "2.00000000" }, { "input": "2 1 1 1000 1000", "output": "2.00000000" }, { "input": "2 1 1000 1 1", "output": "0.00200000" }, { "input": "2 1 1000 1 1000", "output": "0.00200000" }, { "input": "2 1 1000 1000 1", "output": "0.00200000" }, { "input": "2 1 1000 1000 1000", "output": "0.00200000" }, { "input": "1000 1 1 1 1", "output": "1001.00000000" }, { "input": "1000 1 1 1 1000", "output": "2000.00000000" }, { "input": "1000 1 1 1000 1", "output": "1000.00000000" }, { "input": "1000 1 1 1000 1000", "output": "1000.00000000" }, { "input": "1000 1 1000 1 1", "output": "1.00000000" }, { "input": "1000 1 1000 1 1000", "output": "1.00000000" }, { "input": "1000 1 1000 1000 1", "output": "1.00000000" }, { "input": "1000 1 1000 1000 1000", "output": "1.00000000" }, { "input": "1000 999 1 1 1", "output": "1001.00000000" }, { "input": "1000 999 1 1 1000", "output": "1002.00000000" }, { "input": "1000 999 1 1000 1", "output": "1000.00000000" }, { "input": "1000 999 1 1000 1000", "output": "1000.00000000" }, { "input": "1000 999 1000 1 1", "output": "1.00000000" }, { "input": "1000 999 1000 1 1000", "output": "1.00000000" }, { "input": "1000 999 1000 1000 1", "output": "1.00000000" }, { "input": "1000 999 1000 1000 1000", "output": "1.00000000" } ]
92
0
3.977
15,909
173
Rock-Paper-Scissors
[ "implementation", "math" ]
null
null
Nikephoros and Polycarpus play rock-paper-scissors. The loser gets pinched (not too severely!). Let us remind you the rules of this game. Rock-paper-scissors is played by two players. In each round the players choose one of three items independently from each other. They show the items with their hands: a rock, scissors or paper. The winner is determined by the following rules: the rock beats the scissors, the scissors beat the paper and the paper beats the rock. If the players choose the same item, the round finishes with a draw. Nikephoros and Polycarpus have played *n* rounds. In each round the winner gave the loser a friendly pinch and the loser ended up with a fresh and new red spot on his body. If the round finished in a draw, the players did nothing and just played on. Nikephoros turned out to have worked out the following strategy: before the game began, he chose some sequence of items *A*<==<=(*a*1,<=*a*2,<=...,<=*a**m*), and then he cyclically showed the items from this sequence, starting from the first one. Cyclically means that Nikephoros shows signs in the following order: *a*1, *a*2, ..., *a**m*, *a*1, *a*2, ..., *a**m*, *a*1, ... and so on. Polycarpus had a similar strategy, only he had his own sequence of items *B*<==<=(*b*1,<=*b*2,<=...,<=*b**k*). Determine the number of red spots on both players after they've played *n* rounds of the game. You can consider that when the game began, the boys had no red spots on them.
The first line contains integer *n* (1<=≀<=*n*<=≀<=2Β·109) β€” the number of the game's rounds. The second line contains sequence *A* as a string of *m* characters and the third line contains sequence *B* as a string of *k* characters (1<=≀<=*m*,<=*k*<=≀<=1000). The given lines only contain characters "R", "S" and "P". Character "R" stands for the rock, character "S" represents the scissors and "P" represents the paper.
Print two space-separated integers: the numbers of red spots Nikephoros and Polycarpus have.
[ "7\nRPS\nRSPP\n", "5\nRRRRRRRR\nR\n" ]
[ "3 2", "0 0" ]
In the first sample the game went like this: - R - R. Draw. - P - S. Nikephoros loses. - S - P. Polycarpus loses. - R - P. Nikephoros loses. - P - R. Polycarpus loses. - S - S. Draw. - R - P. Nikephoros loses. Thus, in total Nikephoros has 3 losses (and 3 red spots), and Polycarpus only has 2.
[ { "input": "7\nRPS\nRSPP", "output": "3 2" }, { "input": "5\nRRRRRRRR\nR", "output": "0 0" }, { "input": "23\nRSP\nRPSS", "output": "7 8" }, { "input": "52\nRRPSS\nRSSPRPRPPP", "output": "15 21" }, { "input": "1293\nRRPSSRSSPRPRPPPRPPPRPPPPPRPSPRSSRPSPPRPRR\nSSPSSSSRPPSSSSRPRPRPPSRSRRSPPSPPRPSRSPSRR", "output": "411 441" }, { "input": "103948\nRRPSSRSSPRPRPPPRPPPRPPPPPRPSPRSSRPSPPRPRRSSPSSSSRPPSSSSRPRPRPPSRSRRSPPSPPRPSRSPSRRPSRSRSRPRPRSSPSPRPRSSPRPSPPRPRRRPRRPRPSPRPRSSRRRSSSSPSRRSPPPRSSSRSRRSSSPPRRSPSSSPRRSSSSPSSPRRPRSRPPSSRPSRPPRPSSSRSRPPSRRSSSPPRRPPSPSSRRSSPPPPPRRSRSSRPP\nRPRRRSRSRPRPSRPPRSPRRRPSPRPRRRSRSRRSRSSSPSPPSPPPRSPRSSSRPSSSSPPPPSPRPPSSPPSSRRRPRPRRPSSRSPPPPRRSPSSRSRRSSRRPPRSRSRPPRRPRSPRPSPPRPPPSRRRSRRPSPRSSPRSRPSRRPSRSPRRSPSPRSRPSRRPRPRRSPPSRSSR", "output": "34707 34585" }, { "input": "1\nR\nR", "output": "0 0" }, { "input": "5\nS\nR", "output": "5 0" }, { "input": "100\nR\nP", "output": "100 0" }, { "input": "145856\nS\nR", "output": "145856 0" }, { "input": "554858576\nP\nP", "output": "0 0" }, { "input": "2000000000\nS\nS", "output": "0 0" }, { "input": "1\nS\nSSRSRPSSSRPRRPSPRSRSPRRSRRPPRPRRPPRPPRRSPRPRRRPSRSRPPSRPRSPPPSSPPRRRPSSPRSRRSSRPRSRSRSRRRSPSRPPSPPRRSPPRPRSPPPPRPPPRRRPPRPRSSPRSPRRPRRSSPPPSSRPSSRRSRRSPRPPRPPPSPRPSRRPSSSRPPPPRSSPSSSSPRPRRRSRRPPPPPSRRPSSRSPSSRPSSSSPRPPRSRPSRPRRRPRSPSP", "output": "0 0" }, { "input": "1\nRPSSPSRSPRSRSRRPPSRPRPSSRRRRRPPSPR\nS", "output": "0 1" }, { "input": "1\nPSSSRPSRPRSPRP\nRRPSSPPSPRSSSSPPRSPSSRSSSRRPPSPPPSSPSRRRSRRSSRRPPRSSRRRPPSPRRPRRRPPSPSPPPPRSPPRPRRSRSSSSSPSRSSRPPRRPRRPRPRRRPPSSPPSRRSRPRPSSRSSSRPRPRP", "output": "0 1" }, { "input": "54\nSRPRPRSRSPPSSRRPPSSPRPPSRRSRPPSPPR\nSPRPSSSRSRPR", "output": "19 16" }, { "input": "234\nSRSSRRPSSSSPPRPRRPPRSSPSSSPSPRPSRRRSSSRRSPSRRPSRPPPSPSPPPRSRSPPPSPSRSSSPRRPPSRSSPRPSSRRPSSPSSPSRRPSRSSRSPSPPRSPRPRPPRRPRPRPSPRRSSRPSRPRSSSPSRRRSPRPPPPPSPRSSSPPSRRPRPSSRRPRRRSRSRRRSRRS\nPPPSRSSPRPSSRSSPSRSRSRSPSRSSRPRRPRRRPPPPSPSRRPPPSRPPPSPPRSRSRRRRRRPPRSSSRSPSRPRPSPPSPSPRPPRPRRSSRSSRPPPPPPRRRRSPPPPRSPRSRRP", "output": "74 80" }, { "input": "1457057352\nR\nPSRSRSSRPSRRSSSRSRRPRSPPSPPRPSRRPPRSRRSPPSPPSPRPRPRPSSRPRPRRPRSSSSPSRRRPSRSPPSPSRRSPSSRSRPSPRRRSRRRPSPRPPRPPPPPRPPRRRRRRPPRRSPSPSSPSSPRPRSPPRSRPSPSRSRRRRRPPPSRPRSPPSSRRRRPRPPRSPSSPRRRPPPPPRRSRSPRPPSRPRSRSRRPRRRPRSRSPRRRSRSSRPPPRRSRRSSRRPSRPPRSPSPRPRSSSRSSRRPSRRRRPSRRPPRPPRRPRSRPRSRRPPPPPSPPPSPSSPPRPPPRPPRSSPPSRPPSSRRSRSSSRPRRSRSSPRRSRPPRSRSSSRRSPRPPSSPSRPPSSPRPPPSSSSPPRPSRSRPRSPRPSSPPSSPRRPRRPRSPPRSRSPPPPRSRSSPRRSSSRRPPRPPSRPSSPSRPPSSRPPPRRRPSRPPSPRSPSRRRRPPRRPSRPRPSSPRSPPPRRSPPRSRS", "output": "508623712 421858498" }, { "input": "1983654300\nRSSSPPRRSSRSSRPPSRRSSRPPPPSRRPPPSPSSPPPRPSSSRPSPRPSPSPPRRPRSPPSPRRRPPPSPRSSPSSPSRRPSPRPRRRRPRRRRPPRSSSSSSRSSRSPRSPPPPSSRSRPPRPRPRPRPSSPRSSPPSPRRSRSSSRRSSSRSPPPPSPSPRPRPSSSPPPPRRRRPSPRSRPRSPPSPRPSSPPPSPPSPSRSPRPSSRRSPRRSPRRSRRPSPRPRPRRPPRPSPSRSRPRRRRSSRPRSPRPSPPSSSRPRSPPRSRPPRRPRSSRPRRPPRRPSRPRRRPPSRPRRPRPPRSPSRSSRRSRRPPSRPPPRPRPPRRRRRSSPRSPRPRPSSRSRPPRRPPPSSRRSPPSRRSSRRRRSSSPRRR\nP", "output": "697663183 588327921" }, { "input": "1958778499\nSPSSSRPSPPRRSSRSRRSSSSRSR\nPPSSRSPSPRRSRSSRSSRPRPSSSRRRPSRPPSRSSPPSSSPSSPRRRSPSRSPRPRRRSSSPPSSPSPP", "output": "604738368 654397557" }, { "input": "1609387747\nRPRPPPSSSPPSRRPSRRRPPRPPPRPRSRSRPPRRPSPRPSSRSSPPPPRRRRSSRPSPPRRSPPRPSRRRPSSRRPSSRSPRPRSRRSRRRSPRPRPRRSPSRSPSRPSSSPPRPSRPPRSRRRRPRRRSSRRRSSPSPSRSRPRPRPRSRPRSPSSRSPSRPRRRSRPPPPRPPPSSSRSRPSSRPSSPSRRSPS\nSSRSRPRSSPSPRRSPSRRRRPRRRRRSRSSPRSSRSPRSSRPSSRSRSSPSPPPSRRPRRSRSSRSPRPSRRPRSRRPRPPSSSPSRRSPPRRSRSPPPPPSRRRPRPPSPPPSPRSRSRRSPSRSSPPPPPPPSPSPPPPSSRSSSRSSRRRSPPPSPSRPRSPRRRRSSRRPPSSRRRPRPSPSPSRRRRSRRSSRPPPPRPPPRPSSSSPRRSRRSSRPRSSPPSSRPSPSRRRRRPSRRSPSRRSRRPRRPRPPSSSRPRPRRSSRRSRSRPRRSSPRP", "output": "535775691 539324629" }, { "input": "2000000000\nPSRRRPS\nSPSRRPSSSPRPS", "output": "659340660 703296704" }, { "input": "2000000000\nRRRRR\nRRR", "output": "0 0" }, { "input": "2000000000\nRRRRRRRRRR\nSSSSSSSSSSSSSSS", "output": "0 2000000000" }, { "input": "2000000000\nRRR\nPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP", "output": "2000000000 0" }, { "input": "2000000000\nSSSS\nS", "output": "0 0" }, { "input": "2000000000\nSSSS\nPPPPPP", "output": "0 2000000000" }, { "input": "2000000000\nPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP\nRRR", "output": "0 2000000000" }, { "input": "2000000000\nPPPPPPP\nSSSSSS", "output": "2000000000 0" }, { "input": "2000000000\nP\nP", "output": "0 0" }, { "input": "2000000000\nSSSS\nRRR", "output": "2000000000 0" }, { "input": "2000000000\nR\nS", "output": "0 2000000000" }, { "input": "2000000000\nRRRRRRRRRR\nSSSSSSP", "output": "285714285 1714285715" }, { "input": "6\nRR\nSSS", "output": "0 6" }, { "input": "5\nR\nR", "output": "0 0" } ]
2,744
0
3
15,941
367
Sereja and the Arrangement of Numbers
[ "graphs", "greedy", "sortings" ]
null
null
Let's call an array consisting of *n* integer numbers *a*1, *a*2, ..., *a**n*, beautiful if it has the following property: - consider all pairs of numbers *x*,<=*y* (*x*<=β‰ <=*y*), such that number *x* occurs in the array *a* and number *y* occurs in the array *a*; - for each pair *x*,<=*y* must exist some position *j* (1<=≀<=*j*<=&lt;<=*n*), such that at least one of the two conditions are met, either *a**j*<==<=*x*,<=*a**j*<=+<=1<==<=*y*, or *a**j*<==<=*y*,<=*a**j*<=+<=1<==<=*x*. Sereja wants to build a beautiful array *a*, consisting of *n* integers. But not everything is so easy, Sereja's friend Dima has *m* coupons, each contains two integers *q**i*,<=*w**i*. Coupon *i* costs *w**i* and allows you to use as many numbers *q**i* as you want when constructing the array *a*. Values *q**i* are distinct. Sereja has no coupons, so Dima and Sereja have made the following deal. Dima builds some beautiful array *a* of *n* elements. After that he takes *w**i* rubles from Sereja for each *q**i*, which occurs in the array *a*. Sereja believed his friend and agreed to the contract, and now he is wondering, what is the maximum amount of money he can pay. Help Sereja, find the maximum amount of money he can pay to Dima.
The first line contains two integers *n* and *m* (1<=≀<=*n*<=≀<=2Β·106,<=1<=≀<=*m*<=≀<=105). Next *m* lines contain pairs of integers. The *i*-th line contains numbers *q**i*,<=*w**i* (1<=≀<=*q**i*,<=*w**i*<=≀<=105). It is guaranteed that all *q**i* are distinct.
In a single line print maximum amount of money (in rubles) Sereja can pay. 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.
[ "5 2\n1 2\n2 3\n", "100 3\n1 2\n2 1\n3 1\n", "1 2\n1 1\n2 100\n" ]
[ "5\n", "4\n", "100\n" ]
In the first sample Sereja can pay 5 rubles, for example, if Dima constructs the following array: [1, 2, 1, 2, 2]. There are another optimal arrays for this test. In the third sample Sereja can pay 100 rubles, if Dima constructs the following array: [2].
[ { "input": "5 2\n1 2\n2 3", "output": "5" }, { "input": "100 3\n1 2\n2 1\n3 1", "output": "4" }, { "input": "1 2\n1 1\n2 100", "output": "100" }, { "input": "25 29\n82963 53706\n63282 73962\n14996 48828\n84392 31903\n96293 41422\n31719 45448\n46772 17870\n9668 85036\n36704 83323\n73674 63142\n80254 1548\n40663 44038\n96724 39530\n8317 42191\n44289 1041\n63265 63447\n75891 52371\n15007 56394\n55630 60085\n46757 84967\n45932 72945\n72627 41538\n32119 46930\n16834 84640\n78705 73978\n23674 57022\n66925 10271\n54778 41098\n7987 89162", "output": "575068" }, { "input": "53 1\n16942 81967", "output": "81967" }, { "input": "58 38\n6384 48910\n97759 90589\n28947 5031\n45169 32592\n85656 26360\n88538 42484\n44042 88351\n42837 79021\n96022 59200\n485 96735\n98000 3939\n3789 64468\n10894 58484\n26422 26618\n25515 95617\n37452 5250\n39557 66304\n79009 40610\n80703 60486\n90344 37588\n57504 61201\n62619 79797\n51282 68799\n15158 27623\n28293 40180\n9658 62192\n2889 3512\n66635 24056\n18647 88887\n28434 28143\n9417 23999\n22652 77700\n52477 68390\n10713 2511\n22870 66689\n41790 76424\n74586 34286\n47427 67758", "output": "910310" }, { "input": "90 27\n30369 65426\n63435 75442\n14146 41719\n12140 52280\n88688 50550\n3867 68194\n43298 40287\n84489 36456\n6115 63317\n77787 20314\n91186 96913\n57833 44314\n20322 79647\n24482 31197\n11130 57536\n11174 24045\n14293 65254\n94155 24746\n81187 20475\n6169 94788\n77959 22203\n26478 57315\n97335 92373\n99834 47488\n11519 81774\n41764 93193\n23103 89214", "output": "1023071" }, { "input": "44 25\n65973 66182\n23433 87594\n13032 44143\n35287 55901\n92361 46975\n69171 50834\n77761 76668\n32551 93695\n61625 10126\n53695 82303\n94467 18594\n57485 4465\n31153 18088\n21927 24758\n60316 62228\n98759 53110\n41087 83488\n78475 25628\n59929 64521\n78963 60597\n97262 72526\n56261 72117\n80327 82772\n77548 17521\n94925 37764", "output": "717345" }, { "input": "59 29\n93008 65201\n62440 8761\n26325 69109\n30888 54851\n42429 3385\n66541 80705\n52357 33351\n50486 15217\n41358 45358\n7272 37362\n85023 54113\n62697 44042\n60130 32566\n96933 1856\n12963 17735\n44973 38370\n26964 26484\n63636 66849\n12939 58143\n34512 32176\n5826 89871\n63935 91784\n17399 50702\n88735 10535\n93994 57706\n94549 92301\n32642 84856\n55463 82878\n679 82444", "output": "864141" }, { "input": "73 19\n21018 52113\n53170 12041\n44686 99498\n73991 59354\n66652 2045\n56336 99193\n85265 20504\n51776 85293\n21550 17562\n70468 38130\n7814 88602\n84216 64214\n69825 55393\n90671 24028\n98076 67499\n46288 36605\n17222 21707\n25011 99490\n92165 51620", "output": "860399" }, { "input": "6 26\n48304 25099\n17585 38972\n70914 21546\n1547 97770\n92520 48290\n10866 3246\n84319 49602\n57133 31153\n12571 45902\n10424 75601\n22016 80029\n1348 18944\n6410 21050\n93589 44609\n41222 85955\n30147 87950\n97431 40749\n48537 74036\n47186 25854\n39225 55924\n20258 16945\n83319 57412\n20356 54550\n90585 97965\n52076 32143\n93949 24427", "output": "283685" }, { "input": "27 13\n30094 96037\n81142 53995\n98653 82839\n25356 81132\n77842 2012\n88187 81651\n5635 86354\n25453 63263\n61455 12635\n10257 47125\n48214 12029\n21081 92859\n24156 67265", "output": "588137" }, { "input": "1 1\n1 1", "output": "1" }, { "input": "47 10\n1 1\n2 1\n3 1\n4 1\n5 1\n6 1\n7 1\n8 1\n9 1\n10 1", "output": "9" }, { "input": "2 5\n1 1\n2 1\n3 1\n4 1\n5 1", "output": "2" }, { "input": "3 3\n1 1\n2 1\n3 1", "output": "2" }, { "input": "17 6\n1 1\n2 2\n3 3\n4 4\n5 5\n6 6", "output": "20" }, { "input": "7 4\n1 2\n2 3\n3 4\n4 5", "output": "12" }, { "input": "7 4\n1 1\n2 1\n3 1\n4 1", "output": "3" }, { "input": "7 5\n1 1\n2 1\n3 1\n4 1\n5 1", "output": "3" }, { "input": "17 9\n1 1\n2 1\n3 1\n4 1\n5 1\n6 1\n7 1\n8 1\n9 1", "output": "5" }, { "input": "2 2\n1 1\n2 1", "output": "2" }, { "input": "8 7\n1 1\n2 1\n3 1\n4 1\n5 1\n6 1\n7 1", "output": "4" }, { "input": "11 5\n1 1\n2 1\n3 1\n4 1\n5 1", "output": "5" }, { "input": "31 8\n1 1\n2 2\n3 4\n4 8\n5 16\n6 32\n7 64\n8 128", "output": "254" }, { "input": "10 6\n1 1\n2 1\n3 1\n4 1\n5 1\n6 1", "output": "4" }, { "input": "11 10\n1 5\n2 5\n3 5\n4 5\n5 5\n6 5\n7 5\n8 5\n9 5\n10 5", "output": "25" }, { "input": "8 10\n1 1\n2 1\n3 1\n4 1\n5 1\n6 1\n7 1\n8 1\n9 1\n10 1", "output": "4" } ]
670
8,294,400
0
15,960
762
Two strings
[ "binary search", "hashing", "strings", "two pointers" ]
null
null
You are given two strings *a* and *b*. You have to remove the minimum possible number of consecutive (standing one after another) characters from string *b* in such a way that it becomes a subsequence of string *a*. It can happen that you will not need to remove any characters at all, or maybe you will have to remove all of the characters from *b* and make it empty. Subsequence of string *s* is any such string that can be obtained by erasing zero or more characters (not necessarily consecutive) from string *s*.
The first line contains string *a*, and the second lineΒ β€” string *b*. Both of these strings are nonempty and consist of lowercase letters of English alphabet. The length of each string is no bigger than 105 characters.
On the first line output a subsequence of string *a*, obtained from *b* by erasing the minimum number of consecutive characters. If the answer consists of zero characters, output Β«-Β» (a minus sign).
[ "hi\nbob\n", "abca\naccepted\n", "abacaba\nabcdcba\n" ]
[ "-\n", "ac\n", "abcba\n" ]
In the first example strings *a* and *b* don't share any symbols, so the longest string that you can get is empty. In the second example ac is a subsequence of *a*, and at the same time you can obtain it by erasing consecutive symbols cepted from string *b*.
[ { "input": "hi\nbob", "output": "-" }, { "input": "abca\naccepted", "output": "ac" }, { "input": "abacaba\nabcdcba", "output": "abcba" }, { "input": "lo\neuhaqdhhzlnkmqnakgwzuhurqlpmdm", "output": "-" }, { "input": "aaeojkdyuilpdvyewjfrftkpcobhcumwlaoiocbfdtvjkhgda\nmlmarpivirqbxcyhyerjoxlslyfzftrylpjyouypvk", "output": "ouypvk" }, { "input": "npnkmawey\nareakefvowledfriyjejqnnaeqheoh", "output": "a" }, { "input": "fdtffutxkujflswyddvhusfcook\nkavkhnhphcvckogqqqqhdmgwjdfenzizrebefsbuhzzwhzvc", "output": "kvc" }, { "input": "abacaba\naa", "output": "aa" }, { "input": "edbcd\nd", "output": "d" }, { "input": "abc\nksdksdsdsnabc", "output": "abc" }, { "input": "abxzxzxzzaba\naba", "output": "aba" }, { "input": "abcd\nzzhabcd", "output": "abcd" }, { "input": "aa\naa", "output": "aa" }, { "input": "test\nt", "output": "t" }, { "input": "aa\na", "output": "a" }, { "input": "aaaabbbbaaaa\naba", "output": "aba" }, { "input": "aa\nzzaa", "output": "aa" }, { "input": "zhbt\nztjihmhebkrztefpwty", "output": "zt" }, { "input": "aaaaaaaaaaaaaaaaaaaa\naaaaaaaa", "output": "aaaaaaaa" }, { "input": "abba\naba", "output": "aba" }, { "input": "abbba\naba", "output": "aba" }, { "input": "aaaaaaaaaaaa\naaaaaaaaaaaa", "output": "aaaaaaaaaaaa" }, { "input": "aaa\naa", "output": "aa" }, { "input": "aaaaaaaaaaaa\naaa", "output": "aaa" }, { "input": "aaaaabbbbbbaaaaaa\naba", "output": "aba" }, { "input": "ashfaniosafapisfasipfaspfaspfaspfapsfjpasfshvcmvncxmvnxcvnmcxvnmxcnvmcvxvnxmcvxcmvh\nashish", "output": "ashish" }, { "input": "a\na", "output": "a" }, { "input": "aaaab\naab", "output": "aab" }, { "input": "aaaaa\naaaa", "output": "aaaa" }, { "input": "a\naaa", "output": "a" }, { "input": "aaaaaabbbbbbaaaaaa\naba", "output": "aba" }, { "input": "def\nabcdef", "output": "def" }, { "input": "aaaaaaaaa\na", "output": "a" }, { "input": "bababsbs\nabs", "output": "abs" }, { "input": "hddddddack\nhackyz", "output": "hack" }, { "input": "aba\na", "output": "a" }, { "input": "ofih\nihfsdf", "output": "ih" }, { "input": "b\nabb", "output": "b" }, { "input": "lctsczqr\nqvkp", "output": "q" }, { "input": "dedcbaa\ndca", "output": "dca" }, { "input": "haddack\nhack", "output": "hack" }, { "input": "abcabc\nabc", "output": "abc" }, { "input": "asdf\ngasdf", "output": "asdf" }, { "input": "abab\nab", "output": "ab" }, { "input": "aaaaaaa\naaa", "output": "aaa" }, { "input": "asdf\nfasdf", "output": "asdf" }, { "input": "bbaabb\nab", "output": "ab" }, { "input": "accac\nbaacccbcccabaabbcacbbcccacbaabaaac", "output": "aac" }, { "input": "az\naaazazaa", "output": "a" }, { "input": "bbacaabbaaa\nacaabcaa", "output": "acaabaa" }, { "input": "c\ncbcbcbbacacacbccaaccbcabaaabbaaa", "output": "c" }, { "input": "bacb\nccacacbacbccbbccccaccccccbcbabbbaababa", "output": "ba" }, { "input": "ac\naacacaacbaaacbbbabacaca", "output": "a" }, { "input": "a\nzazaa", "output": "a" }, { "input": "abcd\nfaaaabbbbccccdddeda", "output": "a" }, { "input": "abcde\nfabcde", "output": "abcde" }, { "input": "a\nab", "output": "a" }, { "input": "ababbbbbbbbbbbb\nabbbbb", "output": "abbbbb" }, { "input": "bbbbaabbababbaaaaababbaaabbbbaaabbbababbbbabaabababaabaaabbbabababbbabababaababaaaaa\nbbabaaaabaaaabbaaabbbabaaabaabbbababbbbbbbbbbabbababbaababbbaaabababababbbbaaababaaaaab", "output": "bbbbbbbabbababbaababbbaaabababababbbbaaababaaaaab" }, { "input": "ab\naba", "output": "ab" }, { "input": "aa\naaaa", "output": "aa" }, { "input": "aaaaabbbaaaaa\naabbaa", "output": "aabbaa" }, { "input": "aaaaaaaaa\naaaa", "output": "aaaa" }, { "input": "abbcc\naca", "output": "ac" }, { "input": "b\ncb", "output": "b" }, { "input": "aac\naaa", "output": "aa" }, { "input": "ba\nbb", "output": "b" }, { "input": "a\nb", "output": "-" }, { "input": "gkvubrvpbhsfiuyha\nihotmn", "output": "ih" }, { "input": "ccccabccbb\ncbbabcc", "output": "cabcc" }, { "input": "babababbaaabb\nabbab", "output": "abbab" }, { "input": "njtdhyqundyedsjyvy\nypjrs", "output": "ys" }, { "input": "uglyqhkpruxoakm\ncixxkpaaoodpuuh", "output": "uh" }, { "input": "a\naaaaaaaaa", "output": "a" }, { "input": "aaa\naaaaa", "output": "aaa" }, { "input": "abcabbcbcccbccbbcc\nacbcaabbbbcabbbaca", "output": "acbc" }, { "input": "caacacaacbaa\nacbbbabacacac", "output": "aacacac" }, { "input": "aa\naaab", "output": "aa" }, { "input": "acbc\ncacacbac", "output": "ac" }, { "input": "bacbcaacabbaacb\ncbbaaccccbcaacacaabb", "output": "cbcaabb" }, { "input": "baababaaaab\nbaababbbbbbb", "output": "baababb" }, { "input": "aaxyaba\naaba", "output": "aaba" } ]
109
6,963,200
3
16,025
835
The penguin's game
[ "binary search", "constructive algorithms", "interactive" ]
null
null
Pay attention: this problem is interactive. Penguin Xoriy came up with a new game recently. He has *n* icicles numbered from 1 to *n*. Each icicle has a temperatureΒ β€” an integer from 1 to 109. Exactly two of these icicles are special: their temperature is *y*, while a temperature of all the others is *x*<=β‰ <=*y*. You have to find those special icicles. You can choose a non-empty subset of icicles and ask the penguin what is the bitwise exclusive OR (XOR) of the temperatures of the icicles in this subset. Note that you can't ask more than 19 questions. You are to find the special icicles.
The first line contains three integers *n*, *x*, *y* (2<=≀<=*n*<=≀<=1000, 1<=≀<=*x*,<=*y*<=≀<=109, *x*<=β‰ <=*y*)Β β€” the number of icicles, the temperature of non-special icicles and the temperature of the special icicles.
To give your answer to the penguin you have to print character "!" (without quotes), then print two integers *p*1, *p*2 (*p*1<=&lt;<=*p*2)Β β€” the indexes of the special icicles in ascending order. Note that "!" and *p*1 should be separated by a space; the indexes should be separated by a space too. After you gave the answer your program should terminate immediately.
[ "4 2 1\n2\n1\n1" ]
[ "? 3 1 2 3\n? 1 1\n? 1 3\n! 1 3" ]
The answer for the first question is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/b32d8d96beb0d2be8d8a600f458c8cf2e2e28c54.png" style="max-width: 100.0%;max-height: 100.0%;"/>. The answer for the second and the third questions is 1, therefore, special icicles are indexes 1 and 3. You can read more about bitwise XOR operation here: [https://en.wikipedia.org/wiki/Bitwise_operation#XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
[ { "input": "4 2 1 1 3", "output": "Correct answer 1 3, queries: 4." }, { "input": "6 1 2 5 6", "output": "Correct answer 5 6, queries: 5." }, { "input": "2 4523 4235 1 2", "output": "Correct answer 1 2, queries: 2." }, { "input": "511 42 1000000000 255 511", "output": "Correct answer 255 511, queries: 17." }, { "input": "666 536870911 268435455 13 133", "output": "Correct answer 13 133, queries: 18." }, { "input": "999 536870912 536870911 1 999", "output": "Correct answer 1 999, queries: 19." }, { "input": "1000 123 321 1 513", "output": "Correct answer 1 513, queries: 19." }, { "input": "1000 1000000000 1 36 1000", "output": "Correct answer 36 1000, queries: 19." }, { "input": "1000 15 16 511 512", "output": "Correct answer 511 512, queries: 18." }, { "input": "1000 16 15 511 512", "output": "Correct answer 511 512, queries: 18." }, { "input": "50 276891238 128284616 2 28", "output": "Correct answer 2 28, queries: 11." }, { "input": "100 745880634 179094068 84 99", "output": "Correct answer 84 99, queries: 13." }, { "input": "150 481201317 787652038 49 147", "output": "Correct answer 49 147, queries: 13." }, { "input": "200 831819465 669375745 137 165", "output": "Correct answer 137 165, queries: 14." }, { "input": "250 417397044 277933714 112 160", "output": "Correct answer 112 160, queries: 15." }, { "input": "300 62982488 159657421 124 230", "output": "Correct answer 124 230, queries: 16." }, { "input": "350 208368580 768215391 71 135", "output": "Correct answer 71 135, queries: 16." }, { "input": "400 853954024 504714906 168 187", "output": "Correct answer 168 187, queries: 17." }, { "input": "450 999340115 553464364 29 66", "output": "Correct answer 29 66, queries: 17." }, { "input": "500 770616857 910894182 28 34", "output": "Correct answer 28 34, queries: 17." }, { "input": "550 593041285 200362966 227 454", "output": "Correct answer 227 454, queries: 18." }, { "input": "600 803748180 240123414 428 479", "output": "Correct answer 428 479, queries: 18." }, { "input": "650 626172608 824559494 22 607", "output": "Correct answer 22 607, queries: 17." }, { "input": "700 572038286 864319942 52 523", "output": "Correct answer 52 523, queries: 17." }, { "input": "750 394462715 858821430 416 471", "output": "Correct answer 416 471, queries: 19." }, { "input": "800 605169609 193549174 221 768", "output": "Correct answer 221 768, queries: 18." }, { "input": "850 427594038 483017958 161 779", "output": "Correct answer 161 779, queries: 19." }, { "input": "900 228235524 817745702 313 601", "output": "Correct answer 313 601, queries: 19." }, { "input": "950 195884145 107214487 556 781", "output": "Correct answer 556 781, queries: 19." }, { "input": "1000 748509283 888470689 243 289", "output": "Correct answer 243 289, queries: 19." }, { "input": "848 713949655 778798832 114 537", "output": "Correct answer 114 537, queries: 19." }, { "input": "604 992531203 77612090 299 432", "output": "Correct answer 299 432, queries: 18." }, { "input": "797 715823152 671392644 52 722", "output": "Correct answer 52 722, queries: 18." }, { "input": "553 289371996 115430093 378 501", "output": "Correct answer 378 501, queries: 18." }, { "input": "309 862920841 709210647 157 278", "output": "Correct answer 157 278, queries: 15." }, { "input": "65 731436981 448215393 6 36", "output": "Correct answer 6 36, queries: 12." }, { "input": "258 10018529 41995946 160 248", "output": "Correct answer 160 248, queries: 16." }, { "input": "14 878534670 486033396 9 10", "output": "Correct answer 9 10, queries: 7." }, { "input": "769 306859322 79813950 207 574", "output": "Correct answer 207 574, queries: 18." }, { "input": "386 429342362 484650952 98 278", "output": "Correct answer 98 278, queries: 16." }, { "input": "1000 305773675 363466523 207 616", "output": "Correct answer 207 616, queries: 19." }, { "input": "1000 483857099 231982664 254 465", "output": "Correct answer 254 465, queries: 19." }, { "input": "1000 661940523 365340020 211 899", "output": "Correct answer 211 899, queries: 19." }, { "input": "1000 545056651 233856160 468 617", "output": "Correct answer 468 617, queries: 19." }, { "input": "1000 723140075 807405005 214 824", "output": "Correct answer 214 824, queries: 19." }, { "input": "1000 901223500 380953849 759 768", "output": "Correct answer 759 768, queries: 19." }, { "input": "1000 79306924 249469990 25 477", "output": "Correct answer 25 477, queries: 19." }, { "input": "1000 962423052 528051538 122 771", "output": "Correct answer 122 771, queries: 18." }, { "input": "1000 140506476 251343486 136 325", "output": "Correct answer 136 325, queries: 19." }, { "input": "1000 309007679 492561550 536 647", "output": "Correct answer 536 647, queries: 19." }, { "input": "1000 1 2 341 682", "output": "Correct answer 341 682, queries: 19." } ]
46
0
0
16,037
914
Travelling Salesman and Special Numbers
[ "brute force", "combinatorics", "dp" ]
null
null
The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer *x* and reduce it to the number of bits set to 1 in the binary representation of *x*. For example for number 13 it's true that 1310<==<=11012, so it has 3 bits set and 13 will be reduced to 3 in one operation. He calls a number special if the minimum number of operations to reduce it to 1 is *k*. He wants to find out how many special numbers exist which are not greater than *n*. Please help the Travelling Salesman, as he is about to reach his destination! Since the answer can be large, output it modulo 109<=+<=7.
The first line contains integer *n* (1<=≀<=*n*<=&lt;<=21000). The second line contains integer *k* (0<=≀<=*k*<=≀<=1000). Note that *n* is given in its binary representation without any leading zeros.
Output a single integerΒ β€” the number of special numbers not greater than *n*, modulo 109<=+<=7.
[ "110\n2\n", "111111011\n2\n" ]
[ "3\n", "169\n" ]
In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2).
[ { "input": "110\n2", "output": "3" }, { "input": "111111011\n2", "output": "169" }, { "input": "100011110011110110100\n7", "output": "0" }, { "input": "110100110\n0", "output": "1" }, { "input": "10000000000000000000000000000000000000000000\n2", "output": "79284496" }, { "input": "100000000000000000000100000000000010100100001001000010011101010\n3", "output": "35190061" }, { "input": "101010110000\n3", "output": "1563" }, { "input": "11010110000\n3", "output": "1001" }, { "input": "100\n6", "output": "0" }, { "input": "100100100100\n5", "output": "0" }, { "input": "10000000000\n4", "output": "120" }, { "input": "10\n868", "output": "0" }, { "input": "1\n0", "output": "1" }, { "input": "1\n1", "output": "0" }, { "input": "10\n0", "output": "1" }, { "input": "101110011101100100010010101001010111001\n8", "output": "0" }, { "input": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111\n10", "output": "0" }, { "input": "10000000000000000000000000000\n1", "output": "28" }, { "input": "111111111111111111111111111111111111\n2", "output": "338250841" }, { "input": "10010110001111110001100000110111010011010110100111100010001011000011000011000100011010000000000110110010111111\n2", "output": "678359035" }, { "input": "11111100010011110101100110100010001011100111001011001111101111110111001111011011110101100101001000111001000100000011011110110010001000111101001101001010100011\n1", "output": "157" }, { "input": "10011101000010110111001\n1", "output": "22" }, { "input": "10000110011100011111100010011010111110111110100011110101110010000001111100110000001000101011001000111110111100110111010011011100000000111101001010110\n1", "output": "148" }, { "input": "11101011101101101111101100001101110010011011101101010101101111100011101111010111011\n1", "output": "82" }, { "input": "11101111100100100110010100010100101111\n4", "output": "839492816" }, { "input": "111111000110010101000000101001111000101100000110100101111000001011001011110111000000000010\n0", "output": "1" }, { "input": "10000001011010001011101011100010110001010110111100110001010011111111010110101100100010101111\n1", "output": "91" }, { "input": "100111000100101100\n4", "output": "42232" }, { "input": "11001101101010100\n3", "output": "55119" }, { "input": "10000010100111000111001111011001\n1", "output": "31" }, { "input": "110001010001001\n0", "output": "1" }, { "input": "10000010111110110111000001011111111010111101000101001000100111101011001101001111111011110111101100001101100011011111010010001001010100011000111100110101100001100001011111000111001010010010110000111110011111010111001001000111010111111100101101010111100010010111001001111010100110011001111111100000101000100011001011100011000101010100000001100110011011001110101100000111001111011\n732", "output": "0" }, { "input": "111001100100110011100111101100010000111100111\n37", "output": "0" }, { "input": "110101100110100001001011010001011001100100000111000000111100000001010000010001111101000101110001001000110001110001100100100000110101000110111000011010101010011011011000110101110010010111110101101110000010000101101010100101010011010001010010101110001010000001010001111000110100101010001011001110110010\n481", "output": "0" }, { "input": "101011000000101110010001011010000110010100001011101110110000000001001000101011100100110111100110100010010001010111001010110011001011110111100100110000000001000100101101101101010101011100101001010000001111110011101000111111110001000101110000000011111101001100101101100111000000101111011110110001110001001110010001011010000001100001010010000100011001111100000100010000000101011100001010011100110001100111111011011100101101011110110000101000001110010001111100110101010\n129", "output": "0" }, { "input": "1010010001000110110001010110101110100110101100011111101000001001001000001100000000011\n296", "output": "0" }, { "input": "1100001110101110010111011111000111011011100011001000010111000010010011000111111011000100110010100111000000001110101001000110010000111000001001000111011010001001000010001111000111101001100101110000001111110001110111011101011000011010010101001111101101100011101010010110\n6", "output": "0" }, { "input": "101010111001011101010100110100001111010111011000101000100100100111101101101000001110111110011100001110010010010000110101011101011111111011110000001011011101101111001011000111001100000110100100110\n7", "output": "0" }, { "input": "11100001100000001101001111111101000000111010000001001000111101000100100111101100100011100111101100010110000010100100101001101001101001111101101000101010111111110001011000010011011011101010110110011010111011111000100001010110000010000101011001110011011001010001000010000110010110101100010101001111101101111100010010011000000100001000111010011011101000100111001110000000001110000010110000100010110100110011010110000100111110001100001011101\n3", "output": "591387665" }, { "input": "10001011100010111001100001010011011011001100111000000010110010000000010000000011011110011110111110110011000000011110010011110001110110100010111010100111111000000100011011111011111111010000000001110000100100010100111001001000011010010010010001011110010101001001000101110011110000110010011110111011000010110110110101110011100011111001111001111110001011111110111111111010100111101101011000101000100001101001101010101111011001100000110011000\n1", "output": "436" }, { "input": "10101010100111001111011010\n1", "output": "25" }, { "input": "1110100100000010010000001111\n7", "output": "0" }, { "input": "10011000111011110111100110111100011000100111110\n7", "output": "0" }, { "input": "110110000010000001110101000000110010101110000011110101001111000111001111100110001001100011011\n5", "output": "0" }, { "input": "11000011000010000011010011001000100010\n5", "output": "0" }, { "input": "1011100011101000001101111111101111011101000110110001111001010111110111101110101011111110\n7", "output": "0" }, { "input": "100110100010110011010110011101110001010000110011001100011000101000010110111000\n3", "output": "637252489" }, { "input": "11110110000010111011111100010000001010001110110000101001\n6", "output": "0" }, { "input": "100000001100101011100111100101001101110\n3", "output": "186506375" }, { "input": "10011001111000101010111100111010110110000100101001011101011100000010011111100011101\n1", "output": "82" }, { "input": "1111101000100110001010001001\n3", "output": "122853842" }, { "input": "11010101010101001100101110100001001100111001111100100000001100100010010001010001001100001000111010010101101001100101100000000101001011010011000010100011101010111111100010101011111001110000010000111001101101001100010011101000110101110110001101\n4", "output": "571603984" }, { "input": "100001111010010111101010000100000010011000001100101001000110010010001101001101101001001110000101101010011000011101000101111011001101001111011111000000001100100100011011111010000010011111010011001011111010100100001011010011011001010111011111110111100001001101001001101110101101111000110011011100001011011111001110001000110001100110101100001010000001001100000001101010111110001010011101001111010111\n3", "output": "329948438" }, { "input": "1111110000010010100110001010001000111100001101110100100011101110000011001100010\n3", "output": "774501673" }, { "input": "101100101001110000011101\n3", "output": "5671856" }, { "input": "1011\n3", "output": "2" }, { "input": "100010100\n3", "output": "150" }, { "input": "110110010000000011010010100011111001111101110011100100100110001111100001\n3", "output": "134209222" }, { "input": "11101000011\n3", "output": "1074" }, { "input": "1000000101100011101000101010110111101010111100110\n4", "output": "325122368" }, { "input": "1101\n3", "output": "3" }, { "input": "101100100\n2", "output": "139" }, { "input": "11011011111000010101010100000100110101\n4", "output": "363038940" }, { "input": "10010110100010001010000000110100001000010000\n4", "output": "399815120" }, { "input": "101101000001111101010001110\n4", "output": "41258563" }, { "input": "1100000110100011100011110000010001110111\n4", "output": "615102266" }, { "input": "10011100101110000100000011001000\n4", "output": "937000434" }, { "input": "1110111100101001000100\n4", "output": "1562803" }, { "input": "101110100101001000\n2", "output": "38552" }, { "input": "11110110001110101001101110011011010010010101011000\n3", "output": "895709102" }, { "input": "111001001001101111000\n4", "output": "680132" }, { "input": "111101001101101110110010100010000101011100111\n4", "output": "632815766" }, { "input": "1010100110101101101100001001101001100\n3", "output": "555759044" }, { "input": "1011010011010010111111000011111100001001101010100011011110101\n3", "output": "760546372" }, { "input": "101110010111100010011010001001001111001\n3", "output": "557969925" }, { "input": "11010011111101111111010011011101101111010001001001100000111\n3", "output": "389792479" }, { "input": "1011110111001010110001100011010000011111000100000000011000000101010101010000\n4", "output": "184972385" }, { "input": "1111101000110001110011101001001101000000001000010001110000100111100110100101001100110111010010110000100100011001000110110000010010000101000010101011110101001000101000001101000101011100000101011100101011011001110011111000001011111111011100110010100111010010101000010010001001010101010101001110001001011111101111001101011111110010011001011110001100100011101010110010001110101111110010011111111111\n4", "output": "678711158" }, { "input": "10111010100111101011010100001101000111000001111101000101101001100101011000100110100010100101001011110101111001111011000011010000010100110000100110110011001110001001001010110001011111000100010010010111100010110001011010101101010000101110011011100001100101011110110101100010111011111001011110110111110100101100111001000101100111001100001\n3", "output": "187155647" }, { "input": "10100001110001001101000111010111011011101010111100000101001101001010000100000011110110111\n4", "output": "108160984" }, { "input": "1110011001101101000011001110011111100011101100100000010100111010111001110011100111011111100100111001010101000001001010010010110010100100000011111000000001111010110011000010000100101011000101110100100001101111011110011001000111111010100001010001111100000100100101000001011111011110010111010100111001101000001000111000110100000000010101110000011010010011001000111001111101\n3", "output": "652221861" }, { "input": "100110011001000111111110001010011001110100111010010101100110000110011010111010011110101110011101111000001101010111010101111110100100111010010010101000111011111000010101\n3", "output": "72690238" }, { "input": "111000000101110011000110000\n3", "output": "54271713" }, { "input": "1001000100000\n2", "output": "1196" }, { "input": "10110100000101000110011000010\n3", "output": "177315776" }, { "input": "111000010010111110010111011111001011011011011000110\n4", "output": "131135624" }, { "input": "11001101101100010101011111100111001011010011\n4", "output": "249690295" }, { "input": "101111101000011110011\n3", "output": "818095" }, { "input": "11\n1", "output": "1" }, { "input": "1101000100001110101110101011000100100111111110000011010101100111010\n3", "output": "748765378" }, { "input": "1101011100011011000110101100111010011101001000100011111011011\n3", "output": "541620851" }, { "input": "111000110101110110000001011000000011111011100000000101000011111100101000110101111111001110011100010101011010000001011110101100100101001101110101011101111000\n3", "output": "154788917" }, { "input": "10000010000001111111011111010000101101101101010000100101000101011001011101111100001111001000100010111011101110111011000001110011111001100101101100000001011110010111101010001100111011110110111100100001110100100011101011011000010110010110101010100100000101001110101100110110100111110110100011111100010000011110000101010111111001001101101111\n4", "output": "46847153" }, { "input": "1001100001011111100011111010001111001000000000110101100100011000111101111011010111110001001001111110011100100111011111011110101101001011111100101011000110100110011001101111001011101110011111101011100001011010100000100111011011\n4", "output": "449157617" }, { "input": "1111110011110000001101111011001110111100001101111111110011101110111001001000011101100101001000000001110001010001101111001000010111110100110010001001110111100111000010111100011101001010010001111001100011100100111001101100010100111001000101100010100100101011010000011011010100101111011111101100001100010111111011111010\n3", "output": "20014881" }, { "input": "11011101110100111111011101110111001101001001000111010010011100010100000101010011111101011000000110000110111101001111010101111110111011000011101111001101101100101110101010111011100010110111110001001011111110011110000011000111011010111100011000011011001101111100001101000010100011100000\n4", "output": "545014668" }, { "input": "110100011111110101001011010110011010000010001111111011010011111100101000111000010000000001000010100101011001110101011100111111100101111011000011100100111100100100001101100000011010111110000101110110001100110011000111001101001101011101111101111111011000101010100111100101010111110011011111001100011011101110010100001110100010111\n4", "output": "228787489" }, { "input": "111111011010010110111111\n4", "output": "7297383" }, { "input": "111100111101110100010001110010001001001101110011011011011001110000000111111100100011001011100010001011100101100011010101100000101010000001110111100000111110100010011001111011101010001111011110111100100100101111100000010100110110101000111100001001000011110111101101001110010011111001011011110111111110110110010111101011001100010011100010001101001010100000100101001110111010011011101000011001101000011010110100111011101011001001001001110100000100111011011101010001\n3", "output": "703566590" }, { "input": "111000100110111000010100000010001000001011100000000011101010101100010001010111101011110101000101110100000110110010001010101001000101000010010101101000000001110111100101111101010011100011000001101101101011101111100100011011111111101000101011101111101111111101000111101101101100000001000001111111011100110011000010100111011100000000111100001000111100000011110100011100101001001101110011101110111001010011100011111010010000001011001001010111100011111110010011000100101110\n4", "output": "518347346" }, { "input": "11110011010101111001001000010111000101000100000010010001010110011100011100110110011011011111000101111100011101101010001011010000110000101111100011110101010011110001110001011001010000110111001101111101000000110010101110001100010000000101001001001000000010010100000110000010000111100110110001000110011011100\n1000", "output": "0" }, { "input": "10011010111010010111111110001010001010001010110010110010010111101111000101110101010111100101001100011001001001111011111100011110101011011001101101001111111101010010110011111101110010001000111111100011000000111111111100011000000000110101111000001011101000110000111110110000010000010011000011011110101111111101100101000100000100010001010000110100111010110011000010001101011101101001010111101101110000101010111001011001100101000010110011110110011011001111110100011010010110011101011001111101\n208", "output": "0" }, { "input": "1100101001110100100010011111001011101100101\n1000", "output": "0" }, { "input": "10\n1", "output": "1" }, { "input": "111\n1", "output": "2" }, { "input": "11100001111100111110011100111100110111100111001101\n1", "output": "49" }, { "input": "1000000000000000000001010100101\n1", "output": "30" }, { "input": "110\n1", "output": "2" }, { "input": "11011100\n1", "output": "7" }, { "input": "10000000000000000000\n1", "output": "19" }, { "input": "1111111011111110111\n1", "output": "18" }, { "input": "1000\n1", "output": "3" }, { "input": "100\n1", "output": "2" } ]
124
21,606,400
0
16,066
0
none
[ "none" ]
null
null
You are given a sequence *a*1,<=*a*2,<=...,<=*a**n* consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also will be sorted in increasing order. Sorting integers in a subsequence is a process such that the numbers included in a subsequence are ordered in increasing order, and the numbers which are not included in a subsequence don't change their places. Every element of the sequence must appear in exactly one subsequence.
The first line of input data contains integer *n* (1<=≀<=*n*<=≀<=105)Β β€” the length of the sequence. The second line of input data contains *n* different integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=109<=≀<=*a**i*<=≀<=109)Β β€” the elements of the sequence. It is guaranteed that all elements of the sequence are distinct.
In the first line print the maximum number of subsequences *k*, which the original sequence can be split into while fulfilling the requirements. In the next *k* lines print the description of subsequences in the following format: the number of elements in subsequence *c**i* (0<=&lt;<=*c**i*<=≀<=*n*), then *c**i* integers *l*1,<=*l*2,<=...,<=*l**c**i* (1<=≀<=*l**j*<=≀<=*n*)Β β€” indices of these elements in the original sequence. Indices could be printed in any order. Every index from 1 to *n* must appear in output exactly once. If there are several possible answers, print any of them.
[ "6\n3 2 1 6 5 4\n", "6\n83 -75 -49 11 37 62\n" ]
[ "4\n2 1 3\n1 2\n2 4 6\n1 5\n", "1\n6 1 2 3 4 5 6\n" ]
In the first sample output: After sorting the first subsequence we will get sequence 1Β 2Β 3Β 6Β 5Β 4. Sorting the second subsequence changes nothing. After sorting the third subsequence we will get sequence 1Β 2Β 3Β 4Β 5Β 6. Sorting the last subsequence changes nothing.
[ { "input": "6\n3 2 1 6 5 4", "output": "4\n2 1 3\n1 2\n2 4 6\n1 5" }, { "input": "6\n83 -75 -49 11 37 62", "output": "1\n6 1 2 3 4 5 6" }, { "input": "1\n1", "output": "1\n1 1" }, { "input": "2\n1 2", "output": "2\n1 1\n1 2" }, { "input": "2\n2 1", "output": "1\n2 1 2" }, { "input": "3\n1 2 3", "output": "3\n1 1\n1 2\n1 3" }, { "input": "3\n3 2 1", "output": "2\n2 1 3\n1 2" }, { "input": "3\n3 1 2", "output": "1\n3 1 2 3" }, { "input": "10\n3 7 10 1 9 5 4 8 6 2", "output": "3\n6 1 4 7 2 10 3\n3 5 6 9\n1 8" }, { "input": "20\n363756450 -204491568 95834122 -840249197 -49687658 470958158 -445130206 189801569 802780784 -790013317 -192321079 586260100 -751917965 -354684803 418379342 -253230108 193944314 712662868 853829789 735867677", "output": "3\n7 1 4 7 2 10 3 13\n11 5 14 15 6 16 12 17 18 20 19 9\n2 8 11" }, { "input": "50\n39 7 45 25 31 26 50 11 19 37 8 16 22 33 14 6 12 46 49 48 29 27 41 15 34 24 3 13 20 47 9 36 5 43 40 21 2 38 35 42 23 28 1 32 10 17 30 18 44 4", "output": "6\n20 1 43 34 25 4 50 7 2 37 10 45 3 27 22 13 28 42 40 35 39\n23 5 33 14 15 24 26 6 16 12 17 46 18 48 20 29 21 36 32 44 49 19 9 31\n2 8 11\n2 23 41\n2 30 47\n1 38" }, { "input": "100\n39 77 67 25 81 26 50 11 73 95 86 16 90 33 14 79 12 100 68 64 60 27 41 15 34 24 3 61 83 47 57 65 99 43 40 21 94 72 82 85 23 71 76 32 10 17 30 18 44 59 35 89 6 63 7 69 62 70 4 29 92 87 31 48 36 28 45 97 93 98 56 38 58 80 8 1 74 91 53 55 54 51 96 5 42 52 9 22 78 88 75 13 66 2 37 20 49 19 84 46", "output": "6\n41 1 76 43 34 25 4 59 50 7 55 80 74 77 2 94 37 95 10 45 67 3 27 22 88 90 13 92 61 28 66 93 69 56 71 42 85 40 35 51 82 39\n45 5 84 99 33 14 15 24 26 6 53 79 16 12 17 46 100 18 48 64 20 96 83 29 60 21 36 65 32 44 49 97 68 19 98 70 58 73 9 87 62 57 31 63 54 81\n8 8 75 91 78 89 52 86 11\n2 23 41\n2 30 47\n2 38 72" } ]
1,000
9,932,800
0
16,082
1,003
Abbreviation
[ "dp", "hashing", "strings" ]
null
null
You are given a text consisting of $n$ space-separated words. There is exactly one space character between any pair of adjacent words. There are no spaces before the first word and no spaces after the last word. The length of text is the number of letters and spaces in it. $w_i$ is the $i$-th word of text. All words consist only of lowercase Latin letters. Let's denote a segment of words $w[i..j]$ as a sequence of words $w_i, w_{i + 1}, \dots, w_j$. Two segments of words $w[i_1 .. j_1]$ and $w[i_2 .. j_2]$ are considered equal if $j_1 - i_1 = j_2 - i_2$, $j_1 \ge i_1$, $j_2 \ge i_2$, and for every $t \in [0, j_1 - i_1]$ $w_{i_1 + t} = w_{i_2 + t}$. For example, for the text "to be or not to be" the segments $w[1..2]$ and $w[5..6]$ are equal, they correspond to the words "to be". An abbreviation is a replacement of some segments of words with their first uppercase letters. In order to perform an abbreviation, you have to choose at least two non-intersecting equal segments of words, and replace each chosen segment with the string consisting of first letters of the words in the segment (written in uppercase). For example, for the text "a ab a a b ab a a b c" you can replace segments of words $w[2..4]$ and $w[6..8]$ with an abbreviation "AAA" and obtain the text "a AAA b AAA b c", or you can replace segments of words $w[2..5]$ and $w[6..9]$ with an abbreviation "AAAB" and obtain the text "a AAAB AAAB c". What is the minimum length of the text after at most one abbreviation?
The first line of the input contains one integer $n$ ($1 \le n \le 300$) β€” the number of words in the text. The next line contains $n$ space-separated words of the text $w_1, w_2, \dots, w_n$. Each word consists only of lowercase Latin letters. It is guaranteed that the length of text does not exceed $10^5$.
Print one integer β€” the minimum length of the text after at most one abbreviation.
[ "6\nto be or not to be\n", "10\na ab a a b ab a a b c\n", "6\naa bb aa aa bb bb\n" ]
[ "12\n", "13\n", "11\n" ]
In the first example you can obtain the text "TB or not TB". In the second example you can obtain the text "a AAAB AAAB c". In the third example you can obtain the text "AB aa AB bb".
[ { "input": "6\nto be or not to be", "output": "12" }, { "input": "10\na ab a a b ab a a b c", "output": "13" }, { "input": "6\naa bb aa aa bb bb", "output": "11" }, { "input": "45\nxr l pl sx c c u py sv j f x h u y w w bs u cp e ad ib b tz gy lm e s n ln kg fs rd ln v f sh t z r b j w of", "output": "106" }, { "input": "250\nf r s d b f f k d e k v m b t k k j t t a o m m s n d w l v g e k x d w k v a j h c a g x s d e t z z w q z d h n r i k b z k u s q l k c v o d o w w c y i a q v r i g i m l b x z h t a i j t h q u e v j o h w m o v k g r r x j a c m z z i s i r a p p i i l e i g m f f f y v k m c l p n n n j j u t t q s o y b t m x n n t z f c g s r f h w z b b d q d y h t v g y e w p l n m f v c s b r g p v w z c o h k u r c g c s v w r t w k z v t v y z i x r f o l e o u q z k x c o l e c b d j v f z y e r k", "output": "495" }, { "input": "1\nu", "output": "1" }, { "input": "1\nvpdgzvgvgbichiiqdhytvcooetcgeecyueoylqzbtzzgaqhalt", "output": "50" }, { "input": "1\nxdhlmtnvecsbwbycahddxnvwpsxwxgfmidfetpkpeevpjzfbgfafbjpyuevupuptoxutnketcxwrllooyxtxjzwxpzcbpiqzeiplcqvdxyyznjxgkwstpxogdihsamoqhyspbjlelxpbarzqawsgidjtmnpmmupohnslirorliapvntasudhpuuxynyoipuqxdiysbyctpmfpbxqfdlmlsmsvtbxoypkbhwrtpwbsbcdhypsbqhqpdlilquppdwsszrpavcowudreygmpwckbzlpnxxqxjdpqmtidjatvgcbxjrpqqxhhsvlpyxxkoqxutsvebrlxqeggvsnshetkpnfygpwbmnuujfvqnlgavwppufxadhxtffsrdknfmqbsjjegcwokbauzivhnldkvykkytkyrwhimmkznkkofcuioqmpbshskvdhsetyidubcgvuerbozqfbkcmaguaszaivtuswzmtnqcpoiqlvronibiqyeoqm", "output": "500" }, { "input": "2\nvjrvahvokiudpiocpvoqsqhukavyrckhcbctr prqxizcofrfr", "output": "50" }, { "input": "2\nxxwxpgalijfbdbdmluuaubobxztpkfn parzxczfzchinxdtaevbepdxlouzfzaizkinuaufhckjvydmgnkuaneqohcqocfrsbmmohgpoacnqlgspppfogdkkbrkrhdpdlnknjyeccbqssqtaqmyamtkedlhpbjmchfnmwhxepzfrfmlrxrirbvvlryzmulxqjlthclocmiudxbtqpihlnielggjxjmvqjbeozjpskenampuszybcorplicekprqbsdkidwpgwkrpvbpcsdcngawcgeyxsjimalrrwttjjualmhypzrmyauvtothnermlednvjbpgkehxbtbpxolmaapmlcuetghikbgtaspqesjkqwxtvccphjdqpuairsaypfudwvelmupbzhxwuchnfumcxmhflkpyzeppddtczbcjrookncgtojmujyvponennuudppqwwjtnwpgapokwzvbxohrdcvcckzbcrwwvfqlbnwbnmmv", "output": "500" }, { "input": "4\ncongratulations for being first", "output": "31" }, { "input": "4\njngen hype xfckaovxfckaovxfckaovxfckaovxfckaovfegkbwzxfckaovxfckaovfegkbwzfegkbwzfegkbwzxfckaovxfckaovfegkbwzfegkbwzfegkbwzxfckaovxfckaovfegkbwzfegkbwzfegkbwz fegkbwzxfckaovfegkbwzxfckaovxfckaovxfckaovfegkbwzfegkbwzxfckaovxfckaovxfckaovfegkbwzfegkbwzxfckaovxfckaovxfckaovxfckaovxfckaovxfckaovfegkbwzxfckaov", "output": "306" }, { "input": "4\njngen hype acpumodacpumodacpumodulhiwuoulhiwuoulhiwuoacpumodacpumodulhiwuoulhiwuoacpumodulhiwuoacpumodulhiwuoacpumodacpumodulhiwuoacpumodulhiwuoacpumod ulhiwuoulhiwuoacpumodacpumodacpumodulhiwuoulhiwuoacpumodulhiwuoacpumodacpumodacpumodacpumodacpumodulhiwuoulhiwuoulhiwuoulhiwuoacpumodulhiwuo", "output": "292" }, { "input": "4\nraraaraaarrraraaaaaaaaaaaaaaaaraaraararaarraarrraaarrarrraaaarrrarrrrraaraaaarrararrarraarrrararaaar arrararaararaarraaaraararraararaarrraarrrarrrrarrraaaaraaraaaaaaaraaararrarararrarrraarrarrrrraaaaar arrararaararaarraaaraararraararaarrraarrrarrrrarrraaaaraaraaaaaaaraaararrarararrarrraarrarrrrraaaaar raraaraaarrraraaaaaaaaaaaaaaaaraaraararaarraarrraaarrarrraaaarrrarrrrraaraaaarrararrarraarrrararaaar", "output": "205" }, { "input": "4\njngen hype wlvgjpibylpibylwlvgjpibylwlvgjwlvgjwlvgjwlvgjwlvgjpibylwlvgjwlvgjpibylpibylpibylwlvgjpibylpibyl pibylpibylpibylpibylpibylwlvgjwlvgjpibylwlvgjwlvgjpibylpibylwlvgjwlvgjwlvgjpibylwlvgjpibylwlvgj", "output": "202" }, { "input": "29\nqiozjl ghgehr xewbil hwovzr keodgb foobar dvorak barfoo xjjfgm wybwaz jizzzz jizzij tjdqba jiyiqj jizziz inforr icagmg jizjiz tdxtfv jhkhdw pgvlzq qvfpbx ymhmll kzaodh xccnda ugywmk jijizz lkkhfs qwerty", "output": "202" }, { "input": "4\naahahhhaaaaaahhaaahaaahahhhahahhhhhhahhahhhhhhahah ahaahahahaaaahahahaaahaaaahhhaaahhahaaahhaahhaaaah ahaahahahaaaahahahaaahaaaahhhaaahhahaaahhaahhaaaah aahahhhaaaaaahhaaahaaahahhhahahhhhhhahhahhhhhhahah", "output": "105" }, { "input": "4\naaaahaaahahhaaahaaahaahhhahhaaaaahahaahaahaahhaaha hhahhahhaaahhhhhhhhahhhhahaahhhaahhahhhhaahahhhhaa hhahhahhaaahhhhhhhhahhhhahaahhhaahhahhhhaahahhhhaa aaaahaaahahhaaahaaahaahhhahhaaaaahahaahaahaahhaaha", "output": "105" }, { "input": "4\njngen hype flnhgpflnhgpwdxrlvwdxrlvflnhgpwdxrlvflnhgpwdxrlvflnhgpwdxrlvflnhgpflnhgpwdxrlvflnhgpflnhgpflnhgpwdxrlvflnhgp wdxrlvwdxrlvflnhgpwdxrlvflnhgpflnhgpflnhgpwdxrlvflnhgpwdxrlvwdxrlvflnhgpflnhgpwdxrlvflnhgpflnhgpflnhgpflnhgp", "output": "228" }, { "input": "40\naanvs aaikp afkib abrzm abnrq aaxdo aaqxz aalhq afhrw aeets acmlb aazzc acphl aanlr abdfc aatdv adfxe abrud acare abbao aauui aacyx aannq aafwd adirh aafiz accgm aalfz aeeac abrja acfkl aabmr aayub aairn acoqw aavlo afgjf aetbp acbbx abmqy", "output": "239" }, { "input": "2\nrmdkgswpghuszbnq oveleebkwopbnmbr", "output": "33" }, { "input": "2\naisajfcrtzfmrpth fninkxwvnqzjvfdq", "output": "33" }, { "input": "40\naclsp aafgb abvlq aazfz aajjt aacts acbfz aawkl abozz aawlg acmre aapqu acodc aaapn aezbx abhjl adhdt aauxj afggb aafbm acbah abgbo abafl aazow acfwx ablad acifb aayly aemkr acsxa aeuzv abvqj actoq aazzc aayye aaxpo advso aanym abtls aahre", "output": "239" }, { "input": "4\njngen hypee acpumodacpumodacpumodulhiwuoulhiwuoulhiwuoacpumodacpumodulhiwuoulhiwuoacpumodulhiwuoacpumodulhiwuoacpumodacpumodulhiwuoacpumodulhiwuoacpumod ulhiwuoulhiwuoacpumodacpumodacpumodulhiwuoulhiwuoacpumodulhiwuoacpumodacpumodacpumodacpumodacpumodulhiwuoulhiwuoulhiwuoulhiwuoacpumodulhiwuo", "output": "293" }, { "input": "7\na a b a a a b", "output": "9" }, { "input": "13\nv w s e n g j m g v g o asdf", "output": "28" }, { "input": "2\nxnnlpp jpymdh", "output": "13" } ]
92
2,150,400
0
16,104
416
Population Size
[ "greedy", "implementation", "math" ]
null
null
Polycarpus develops an interesting theory about the interrelation of arithmetic progressions with just everything in the world. His current idea is that the population of the capital of Berland changes over time like an arithmetic progression. Well, or like multiple arithmetic progressions. Polycarpus believes that if he writes out the population of the capital for several consecutive years in the sequence *a*1,<=*a*2,<=...,<=*a**n*, then it is convenient to consider the array as several arithmetic progressions, written one after the other. For example, sequence (8,<=6,<=4,<=2,<=1,<=4,<=7,<=10,<=2) can be considered as a sequence of three arithmetic progressions (8,<=6,<=4,<=2), (1,<=4,<=7,<=10) and (2), which are written one after another. Unfortunately, Polycarpus may not have all the data for the *n* consecutive years (a census of the population doesn't occur every year, after all). For this reason, some values of *a**i* ​​may be unknown. Such values are represented by number -1. For a given sequence *a*<==<=(*a*1,<=*a*2,<=...,<=*a**n*), which consists of positive integers and values ​​-1, find the minimum number of arithmetic progressions Polycarpus needs to get *a*. To get *a*, the progressions need to be written down one after the other. Values ​​-1 may correspond to an arbitrary positive integer and the values *a**i*<=&gt;<=0 must be equal to the corresponding elements of sought consecutive record of the progressions. Let us remind you that a finite sequence *c* is called an arithmetic progression if the difference *c**i*<=+<=1<=-<=*c**i* of any two consecutive elements in it is constant. By definition, any sequence of length 1 is an arithmetic progression.
The first line of the input contains integer *n* (1<=≀<=*n*<=≀<=2Β·105) β€” the number of elements in the sequence. The second line contains integer values *a*1,<=*a*2,<=...,<=*a**n* separated by a space (1<=≀<=*a**i*<=≀<=109 or *a**i*<==<=<=-<=1).
Print the minimum number of arithmetic progressions that you need to write one after another to get sequence *a*. The positions marked as -1 in *a* can be represented by any positive integers.
[ "9\n8 6 4 2 1 4 7 10 2\n", "9\n-1 6 -1 2 -1 4 7 -1 2\n", "5\n-1 -1 -1 -1 -1\n", "7\n-1 -1 4 5 1 2 3\n" ]
[ "3\n", "3\n", "1\n", "2\n" ]
none
[ { "input": "9\n8 6 4 2 1 4 7 10 2", "output": "3" }, { "input": "9\n-1 6 -1 2 -1 4 7 -1 2", "output": "3" }, { "input": "5\n-1 -1 -1 -1 -1", "output": "1" }, { "input": "7\n-1 -1 4 5 1 2 3", "output": "2" }, { "input": "1\n1", "output": "1" }, { "input": "1\n65", "output": "1" }, { "input": "1\n1000000000", "output": "1" }, { "input": "1\n-1", "output": "1" }, { "input": "2\n1000000000 1000000000", "output": "1" }, { "input": "2\n1000000000 -1", "output": "1" }, { "input": "2\n-1 1000000000", "output": "1" }, { "input": "2\n-1 -1", "output": "1" }, { "input": "3\n999999999 1000000000 -1", "output": "1" }, { "input": "3\n999999999 -1 1000000000", "output": "2" }, { "input": "3\n1000000000 999999999 1000000000", "output": "2" }, { "input": "3\n-1 1000000000 999999999", "output": "1" }, { "input": "3\n-1 1000000000 -1", "output": "1" }, { "input": "3\n-1 1 2", "output": "2" }, { "input": "3\n-1 1 1000000000", "output": "2" }, { "input": "5\n-1 1 7 -1 5", "output": "2" }, { "input": "7\n-1 2 4 -1 4 1 5", "output": "3" }, { "input": "2\n-1 21", "output": "1" }, { "input": "3\n39 42 -1", "output": "1" }, { "input": "4\n45 -1 41 -1", "output": "1" }, { "input": "5\n-1 40 42 -1 46", "output": "1" }, { "input": "6\n-1 6 1 -1 -1 -1", "output": "2" }, { "input": "7\n32 33 34 -1 -1 37 38", "output": "1" }, { "input": "8\n-1 12 14 16 18 20 -1 -1", "output": "1" }, { "input": "9\n42 39 36 33 -1 -1 -1 34 39", "output": "2" }, { "input": "10\n29 27 -1 23 42 -1 -1 45 -1 -1", "output": "2" }, { "input": "5\n40 -1 44 46 48", "output": "1" }, { "input": "6\n43 40 37 34 -1 -1", "output": "1" }, { "input": "7\n11 8 5 -1 -1 -1 -1", "output": "2" }, { "input": "8\n-1 12 14 16 18 20 -1 -1", "output": "1" }, { "input": "9\n42 39 36 33 -1 -1 -1 34 39", "output": "2" }, { "input": "10\n29 27 -1 23 42 -1 -1 45 -1 -1", "output": "2" }, { "input": "11\n9 21 17 13 -1 -1 -1 -1 -1 -1 -1", "output": "3" }, { "input": "12\n-1 17 -1 54 -1 64 -1 74 79 84 -1 94", "output": "2" }, { "input": "13\n25 24 23 22 24 27 -1 33 -1 2 2 2 -1", "output": "3" }, { "input": "14\n-1 5 3 -1 -1 31 31 31 -1 31 -1 -1 4 7", "output": "3" }, { "input": "15\n-1 28 -1 32 34 26 -1 26 -1 -1 26 26 26 -1 -1", "output": "2" }, { "input": "16\n3 8 13 18 23 -1 -1 -1 43 48 53 45 -1 -1 -1 -1", "output": "2" }, { "input": "17\n-1 -1 -1 -1 64 68 72 -1 45 46 47 48 49 50 51 52 53", "output": "2" }, { "input": "18\n21 19 -1 -1 -1 48 50 -1 54 -1 5 1 -1 -1 -1 37 36 35", "output": "4" }, { "input": "19\n23 26 -1 -1 35 38 41 -1 -1 -1 53 -1 59 62 6 7 8 9 -1", "output": "2" }, { "input": "6\n-1 2 6 -1 -1 6", "output": "2" }, { "input": "8\n-1 -1 1 7 -1 9 5 2", "output": "3" }, { "input": "20\n-1 32 37 -1 -1 -1 57 -1 -1 40 31 33 -1 -1 39 47 43 -1 35 32", "output": "5" }, { "input": "13\n2 -1 3 1 3 1 -1 1 3 -1 -1 1 1", "output": "6" }, { "input": "3\n-1 1 -1", "output": "1" } ]
31
6,656,000
0
16,139
0
none
[ "none" ]
null
null
A two dimensional array is called a bracket array if each grid contains one of the two possible brackets β€” "(" or ")". A path through the two dimensional array cells is called monotonous if any two consecutive cells in the path are side-adjacent and each cell of the path is located below or to the right from the previous one. A two dimensional array whose size equals *n*<=Γ—<=*m* is called a correct bracket array, if any string formed by writing out the brackets on some monotonous way from cell (1,<=1) to cell (*n*,<=*m*) forms a correct bracket sequence. Let's define the operation of comparing two correct bracket arrays of equal size (*a* and *b*) like that. Let's consider a given two dimensional array of priorities (*c*) β€” a two dimensional array of same size, containing different integers from 1 to *nm*. Let's find such position (*i*,<=*j*) in the two dimensional array, that *a**i*,<=*j*<=β‰ <=*b**i*,<=*j*. If there are several such positions, let's choose the one where number *c**i*,<=*j* is minimum. If *a**i*,<=*j*<==<="(", then *a*<=&lt;<=*b*, otherwise *a*<=&gt;<=*b*. If the position (*i*,<=*j*) is not found, then the arrays are considered equal. Your task is to find a *k*-th two dimensional correct bracket array. It is guaranteed that for the given sizes of *n* and *m* there will be no less than *k* two dimensional correct bracket arrays.
The first line contains integers *n*, *m* and *k* β€” the sizes of the array and the number of the sought correct bracket array (1<=≀<=*n*,<=*m*<=≀<=100, 1<=≀<=*k*<=≀<=1018). Then an array of priorities is given, *n* lines each containing *m* numbers, number *p**i*,<=*j* shows the priority of character *j* in line *i* (1<=≀<=*p**i*,<=*j*<=≀<=*nm*, all *p**i*,<=*j* are different). 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.
Print the *k*-th two dimensional correct bracket array.
[ "1 2 1\n1 2\n", "2 3 1\n1 2 3\n4 5 6\n", "3 2 2\n3 6\n1 4\n2 5\n" ]
[ "()\n", "(()\n())\n", "()\n)(\n()\n" ]
In the first sample exists only one correct two-dimensional bracket array. In the second and in the third samples two arrays exist. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters Β«+Β» and Β«1Β» into this sequence. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not.
[]
60
0
0
16,182
148
Escape
[ "implementation", "math" ]
null
null
The princess is going to escape the dragon's cave, and she needs to plan it carefully. The princess runs at *v**p* miles per hour, and the dragon flies at *v**d* miles per hour. The dragon will discover the escape after *t* hours and will chase the princess immediately. Looks like there's no chance to success, but the princess noticed that the dragon is very greedy and not too smart. To delay him, the princess decides to borrow a couple of bijous from his treasury. Once the dragon overtakes the princess, she will drop one bijou to distract him. In this case he will stop, pick up the item, return to the cave and spend *f* hours to straighten the things out in the treasury. Only after this will he resume the chase again from the very beginning. The princess is going to run on the straight. The distance between the cave and the king's castle she's aiming for is *c* miles. How many bijous will she need to take from the treasury to be able to reach the castle? If the dragon overtakes the princess at exactly the same moment she has reached the castle, we assume that she reached the castle before the dragon reached her, and doesn't need an extra bijou to hold him off.
The input data contains integers *v**p*,<=*v**d*,<=*t*,<=*f* and *c*, one per line (1<=≀<=*v**p*,<=*v**d*<=≀<=100, 1<=≀<=*t*,<=*f*<=≀<=10, 1<=≀<=*c*<=≀<=1000).
Output the minimal number of bijous required for the escape to succeed.
[ "1\n2\n1\n1\n10\n", "1\n2\n1\n1\n8\n" ]
[ "2\n", "1\n" ]
In the first case one hour after the escape the dragon will discover it, and the princess will be 1 mile away from the cave. In two hours the dragon will overtake the princess 2 miles away from the cave, and she will need to drop the first bijou. Return to the cave and fixing the treasury will take the dragon two more hours; meanwhile the princess will be 4 miles away from the cave. Next time the dragon will overtake the princess 8 miles away from the cave, and she will need the second bijou, but after this she will reach the castle without any further trouble. The second case is similar to the first one, but the second time the dragon overtakes the princess when she has reached the castle, and she won't need the second bijou.
[ { "input": "1\n2\n1\n1\n10", "output": "2" }, { "input": "1\n2\n1\n1\n8", "output": "1" }, { "input": "5\n8\n1\n2\n100", "output": "2" }, { "input": "2\n100\n10\n10\n739", "output": "22" }, { "input": "17\n99\n2\n3\n293", "output": "3" }, { "input": "5\n5\n1\n1\n1000", "output": "0" }, { "input": "100\n99\n1\n1\n1000", "output": "0" }, { "input": "1\n100\n1\n1\n1", "output": "0" }, { "input": "1\n100\n1\n1\n1000", "output": "152" }, { "input": "10\n1\n10\n1\n11", "output": "0" }, { "input": "98\n94\n4\n3\n437", "output": "0" }, { "input": "58\n4\n1\n10\n392", "output": "0" }, { "input": "74\n11\n8\n7\n835", "output": "0" }, { "input": "86\n21\n7\n2\n982", "output": "0" }, { "input": "2\n27\n4\n9\n937", "output": "15" }, { "input": "62\n89\n8\n1\n83", "output": "0" }, { "input": "78\n7\n7\n6\n38", "output": "0" }, { "input": "94\n14\n2\n3\n481", "output": "0" }, { "input": "6\n24\n9\n8\n628", "output": "3" }, { "input": "59\n7\n8\n10\n357", "output": "0" }, { "input": "75\n26\n4\n3\n504", "output": "0" }, { "input": "87\n32\n3\n8\n754", "output": "0" }, { "input": "51\n42\n10\n4\n901", "output": "0" }, { "input": "63\n4\n7\n1\n48", "output": "0" }, { "input": "79\n10\n4\n6\n3", "output": "0" }, { "input": "95\n20\n9\n3\n149", "output": "0" }, { "input": "55\n35\n5\n10\n592", "output": "0" }, { "input": "71\n45\n2\n6\n547", "output": "0" }, { "input": "83\n7\n7\n7\n46", "output": "0" }, { "input": "100\n32\n1\n8\n537", "output": "0" }, { "input": "17\n42\n10\n5\n684", "output": "1" }, { "input": "77\n1\n6\n8\n831", "output": "0" }, { "input": "93\n19\n3\n3\n82", "output": "0" }, { "input": "5\n25\n8\n9\n228", "output": "2" }, { "input": "21\n35\n5\n6\n535", "output": "1" }, { "input": "85\n45\n2\n1\n682", "output": "0" }, { "input": "97\n4\n8\n8\n829", "output": "0" }, { "input": "13\n14\n3\n3\n79", "output": "0" }, { "input": "25\n28\n4\n9\n226", "output": "0" }, { "input": "34\n9\n6\n6\n70", "output": "0" }, { "input": "50\n15\n1\n3\n216", "output": "0" }, { "input": "10\n25\n9\n8\n363", "output": "1" }, { "input": "26\n36\n4\n7\n318", "output": "0" }, { "input": "38\n50\n1\n8\n761", "output": "1" }, { "input": "2\n12\n6\n4\n907", "output": "10" }, { "input": "14\n18\n5\n9\n862", "output": "1" }, { "input": "30\n28\n4\n6\n9", "output": "0" }, { "input": "46\n39\n8\n3\n964", "output": "0" }, { "input": "6\n45\n7\n8\n407", "output": "4" }, { "input": "67\n34\n7\n4\n954", "output": "0" }, { "input": "31\n40\n6\n1\n397", "output": "0" }, { "input": "43\n50\n1\n8\n544", "output": "1" }, { "input": "59\n9\n7\n3\n498", "output": "0" }, { "input": "71\n19\n2\n10\n645", "output": "0" }, { "input": "35\n37\n9\n5\n792", "output": "0" }, { "input": "47\n43\n10\n9\n43", "output": "0" }, { "input": "63\n53\n5\n4\n189", "output": "0" }, { "input": "79\n11\n2\n1\n144", "output": "0" }, { "input": "39\n22\n8\n6\n291", "output": "0" }, { "input": "49\n7\n2\n5\n326", "output": "0" }, { "input": "2\n1\n1\n1\n1000", "output": "0" }, { "input": "100\n1\n1\n1\n1000", "output": "0" }, { "input": "2\n1\n1\n1\n100", "output": "0" }, { "input": "2\n1\n1\n1\n10", "output": "0" }, { "input": "5\n3\n3\n3\n999", "output": "0" } ]
122
0
3
16,183
0
none
[ "none" ]
null
null
It's a beautiful April day and Wallace is playing football with his friends. But his friends do not know that Wallace actually stayed home with Gromit and sent them his robotic self instead. Robo-Wallace has several advantages over the other guys. For example, he can hit the ball directly to the specified point. And yet, the notion of a giveaway is foreign to him. The combination of these features makes the Robo-Wallace the perfect footballer β€” as soon as the ball gets to him, he can just aim and hit the goal. He followed this tactics in the first half of the match, but he hit the goal rarely. The opposing team has a very good goalkeeper who catches most of the balls that fly directly into the goal. But Robo-Wallace is a quick thinker, he realized that he can cheat the goalkeeper. After all, they are playing in a football box with solid walls. Robo-Wallace can kick the ball to the other side, then the goalkeeper will not try to catch the ball. Then, if the ball bounces off the wall and flies into the goal, the goal will at last be scored. Your task is to help Robo-Wallace to detect a spot on the wall of the football box, to which the robot should kick the ball, so that the ball bounces once and only once off this wall and goes straight to the goal. In the first half of the match Robo-Wallace got a ball in the head and was severely hit. As a result, some of the schemes have been damaged. Because of the damage, Robo-Wallace can only aim to his right wall (Robo-Wallace is standing with his face to the opposing team's goal). The football box is rectangular. Let's introduce a two-dimensional coordinate system so that point (0, 0) lies in the lower left corner of the field, if you look at the box above. Robo-Wallace is playing for the team, whose goal is to the right. It is an improvised football field, so the gate of Robo-Wallace's rivals may be not in the middle of the left wall. In the given coordinate system you are given: - *y*1, *y*2 β€” the *y*-coordinates of the side pillars of the goalposts of robo-Wallace's opponents; - *y**w* β€” the *y*-coordinate of the wall to which Robo-Wallace is aiming; - *x**b*, *y**b* β€” the coordinates of the ball's position when it is hit; - *r* β€” the radius of the ball. A goal is scored when the center of the ball crosses the *OY* axis in the given coordinate system between (0, *y*1) and (0, *y*2). The ball moves along a straight line. The ball's hit on the wall is perfectly elastic (the ball does not shrink from the hit), the angle of incidence equals the angle of reflection. If the ball bounces off the wall not to the goal, that is, if it hits the other wall or the goal post, then the opposing team catches the ball and Robo-Wallace starts looking for miscalculation and gets dysfunctional. Such an outcome, if possible, should be avoided. We assume that the ball touches an object, if the distance from the center of the ball to the object is no greater than the ball radius *r*.
The first and the single line contains integers *y*1, *y*2, *y**w*, *x**b*, *y**b*, *r* (1<=≀<=*y*1,<=*y*2,<=*y**w*,<=*x**b*,<=*y**b*<=≀<=106; *y*1<=&lt;<=*y*2<=&lt;<=*y**w*; *y**b*<=+<=*r*<=&lt;<=*y**w*; 2Β·*r*<=&lt;<=*y*2<=-<=*y*1). It is guaranteed that the ball is positioned correctly in the field, doesn't cross any wall, doesn't touch the wall that Robo-Wallace is aiming at. The goal posts can't be located in the field corners.
If Robo-Wallace can't score a goal in the described manner, print "-1" (without the quotes). Otherwise, print a single number *x**w* β€” the abscissa of his point of aiming. If there are multiple points of aiming, print the abscissa of any of them. When checking the correctness of the answer, all comparisons are made with the permissible absolute error, equal to 10<=-<=8. It is recommended to print as many characters after the decimal point as possible.
[ "4 10 13 10 3 1\n", "1 4 6 2 2 1\n", "3 10 15 17 9 2\n" ]
[ "4.3750000000\n", "-1\n", "11.3333333333\n" ]
Note that in the first and third samples other correct values of abscissa *x*<sub class="lower-index">*w*</sub> are also possible.
[ { "input": "4 10 13 10 3 1", "output": "4.3750000000" }, { "input": "1 4 6 2 2 1", "output": "-1" }, { "input": "3 10 15 17 9 2", "output": "11.3333333333" }, { "input": "4 9 30 3 3 1", "output": "-1" }, { "input": "4 9 13 2 3 1", "output": "-1" }, { "input": "4 9 13 1 1 1", "output": "-1" }, { "input": "1 9 10 6 6 3", "output": "4.5000000000" }, { "input": "4 9 24 10 3 1", "output": "4.7368421053" }, { "input": "4 9 20 10 3 1", "output": "4.6666666667" }, { "input": "1 8 10 8 3 3", "output": "3.4285714286" }, { "input": "2 9 10 4 6 3", "output": "2.6666666667" }, { "input": "2 9 10 6 3 3", "output": "-1" }, { "input": "1 9 10 7 3 3", "output": "3.0000000000" }, { "input": "1 9 10 9 5 3", "output": "5.4000000000" }, { "input": "2 9 10 6 5 3", "output": "3.0000000000" }, { "input": "1 9 10 5 5 3", "output": "3.0000000000" }, { "input": "2 9 10 9 3 3", "output": "3.0000000000" }, { "input": "1 9 10 9 5 3", "output": "5.4000000000" }, { "input": "1 8 10 3 3 3", "output": "-1" }, { "input": "1 9 10 5 5 3", "output": "3.0000000000" }, { "input": "2 9 10 5 3 3", "output": "-1" }, { "input": "2 9 10 8 5 3", "output": "4.0000000000" }, { "input": "2 9 10 9 5 3", "output": "4.5000000000" }, { "input": "1 9 10 4 5 3", "output": "2.4000000000" }, { "input": "1 8 10 5 5 3", "output": "-1" }, { "input": "2 9 10 9 5 3", "output": "4.5000000000" }, { "input": "15 30 100 8 8 5", "output": "-1" }, { "input": "15 30 100 58 81 5", "output": "48.8764044944" }, { "input": "15 30 100 601 76 5", "output": "479.5212765957" }, { "input": "15 30 100 7193 39 5", "output": "4118.1297709924" }, { "input": "15 30 100 40766 18 5", "output": "20114.8026315789" }, { "input": "15 30 100 243890 31 5", "output": "131595.3237410072" }, { "input": "4 9 30 10 3 1", "output": "-1" }, { "input": "56 90 100 9 56 9", "output": "-1" }, { "input": "29 62 100 88 37 9", "output": "43.5887850467" }, { "input": "712 950 1000 98 727 92", "output": "-1" }, { "input": "7788 8844 10000 70 4902 63", "output": "-1" }, { "input": "49 67 100 986 29 7", "output": "361.2079207921" }, { "input": "190 212 1000 103 795 3", "output": "-1" }, { "input": "5234 7681 10000 985 8825 847", "output": "-1" }, { "input": "94603 96309 100000 728 25633 556", "output": "-1" }, { "input": "30 73 100 5089 24 9", "output": "2223.7647058824" }, { "input": "330 357 1000 625 129 8", "output": "-1" }, { "input": "5010 6384 10000 9022 3213 187", "output": "3713.0485021398" }, { "input": "7 17 100 56205 62 2", "output": "40017.9600000000" }, { "input": "626 705 1000 10072 858 35", "output": "7449.8491484185" }, { "input": "1727 5232 10000 67443 5399 62", "output": "43315.9683953342" }, { "input": "10995 85967 100000 47813 44507 2442", "output": "29321.4167104074" }, { "input": "845391 929573 1000000 87612 108825 1400", "output": "12769.2918746832" }, { "input": "78 90 100 535782 61 4", "output": "153080.5714285714" }, { "input": "2846 8620 10000 466361 3155 1292", "output": "210537.3673812111" }, { "input": "138623 763216 1000000 366229 316563 160243", "output": "-1" }, { "input": "111724 287004 931554 512877 139642 23002", "output": "257255.6532044368" }, { "input": "70276 182564 238201 222757 154128 55592", "output": "-1" }, { "input": "65775 300705 686095 383961 189161 72083", "output": "-1" }, { "input": "303226 381701 395142 301908 2696 244", "output": "57074.3018919422" }, { "input": "451924 493579 637450 231345 530245 20087", "output": "-1" }, { "input": "67933 96355 131374 588846 12918 4897", "output": "188927.5585923950" }, { "input": "149195 164613 287623 72041 223411 5390", "output": "-1" }, { "input": "448887 492030 560100 388288 354938 20867", "output": "-1" }, { "input": "435582 479389 540004 905521 413521 1624", "output": "405317.7264116302" }, { "input": "64887 100252 122962 146510 74262 15718", "output": "65461.4966203183" }, { "input": "246310 320553 585881 278070 443362 23788", "output": "197686.6564327557" }, { "input": "154137 199509 247827 186170 112705 19967", "output": "-1" }, { "input": "652284 765064 966501 110259 224662 46292", "output": "-1" }, { "input": "1437 1945 9737 17190 7829 114", "output": "14064.2286640989" }, { "input": "61880 74283 78517 551852 20330 1475", "output": "107291.2729442180" }, { "input": "196112 214848 221935 465535 132387 3661", "output": "82508.1717726175" }, { "input": "20296 469893 481654 239118 236770 20582", "output": "155898.4832985775" }, { "input": "476636 647171 684372 48498 122589 5636", "output": "12660.0741578319" }, { "input": "140 149 150 13 78 3", "output": "-1" }, { "input": "140 149 150 16 36 3", "output": "-1" }, { "input": "140 149 150 13 134 3", "output": "3.0588235294" }, { "input": "140 149 150 11 76 3", "output": "-1" }, { "input": "1400 1490 1500 78 292 40", "output": "-1" }, { "input": "1400 1490 1500 89 829 40", "output": "-1" }, { "input": "1400 1490 1500 75 585 40", "output": "-1" }, { "input": "1400 1490 1500 67 240 40", "output": "-1" }, { "input": "1400 1490 1500 64 276 40", "output": "-1" }, { "input": "1400 1490 1500 43 926 40", "output": "-1" }, { "input": "1400 1490 1500 83 1362 40", "output": "-1" }, { "input": "140 149 150 18 80 3", "output": "-1" }, { "input": "4 9 25 10 3 1", "output": "-1" } ]
92
0
0
16,190
297
Fish Weight
[ "constructive algorithms", "greedy" ]
null
null
It is known that there are *k* fish species in the polar ocean, numbered from 1 to *k*. They are sorted by non-decreasing order of their weight, which is a positive number. Let the weight of the *i*-th type of fish be *w**i*, then 0<=&lt;<=*w*1<=≀<=*w*2<=≀<=...<=≀<=*w**k* holds. Polar bears Alice and Bob each have caught some fish, and they are guessing who has the larger sum of weight of the fish he/she's caught. Given the type of the fish they've caught, determine whether it is possible that the fish caught by Alice has a strictly larger total weight than Bob's. In other words, does there exist a sequence of weights *w**i* (not necessary integers), such that the fish caught by Alice has a strictly larger total weight?
The first line contains three integers *n*,<=*m*,<=*k* (1<=≀<=*n*,<=*m*<=≀<=105,<=1<=≀<=*k*<=≀<=109) β€” the number of fish caught by Alice and Bob respectively, and the number of fish species. The second line contains *n* integers each from 1 to *k*, the list of fish type caught by Alice. The third line contains *m* integers each from 1 to *k*, the list of fish type caught by Bob. Note that one may have caught more than one fish for a same species.
Output "YES" (without quotes) if it is possible, and "NO" (without quotes) otherwise.
[ "3 3 3\n2 2 2\n1 1 3\n", "4 7 9\n5 2 7 3\n3 5 2 7 3 8 7\n" ]
[ "YES\n", "NO\n" ]
In the first sample, if *w*<sub class="lower-index">1</sub> = 1, *w*<sub class="lower-index">2</sub> = 2, *w*<sub class="lower-index">3</sub> = 2.5, then Alice has a total of 2 + 2 + 2 = 6 weight units, while Bob only has 1 + 1 + 2.5 = 4.5. In the second sample, the fish that Alice caught is a subset of Bob's. Therefore, the total weight of Bob’s fish is always not less than the total weight of Alice’s fish.
[ { "input": "3 3 3\n2 2 2\n1 1 3", "output": "YES" }, { "input": "4 7 9\n5 2 7 3\n3 5 2 7 3 8 7", "output": "NO" }, { "input": "5 5 10\n8 2 8 5 9\n9 1 7 5 1", "output": "YES" }, { "input": "7 7 10\n8 2 8 10 6 9 10\n2 4 9 5 6 2 5", "output": "YES" }, { "input": "15 15 10\n4 5 9 1 4 6 4 1 4 3 7 9 9 2 6\n6 6 7 7 2 9 1 6 10 9 7 10 7 10 9", "output": "NO" }, { "input": "25 25 10\n10 6 2 1 9 7 2 5 6 9 2 3 2 8 5 8 2 9 10 8 9 7 7 4 8\n6 2 10 4 7 9 3 2 4 5 1 8 6 9 8 6 9 8 4 8 7 9 10 2 8", "output": "NO" }, { "input": "50 100 10\n10 9 10 5 5 2 2 6 4 8 9 1 6 3 9 7 8 3 8 5 6 6 5 7 2 10 3 6 8 1 8 8 9 5 10 1 5 10 9 4 7 8 10 3 3 4 7 8 6 3\n5 3 2 6 4 10 2 3 1 8 8 10 1 1 4 3 9 2 9 9 8 8 7 9 4 1 1 10 5 6 3 7 2 10 2 3 3 3 7 4 1 3 1 6 7 6 1 9 1 7 6 8 6 1 3 3 3 4 3 6 7 8 2 5 4 1 4 8 3 9 7 4 10 5 3 6 3 1 4 10 3 6 1 8 4 6 10 9 6 2 8 3 7 5 3 4 10 9 1 4", "output": "NO" }, { "input": "100 50 10\n7 8 7 1 6 7 9 2 4 6 7 7 3 9 4 5 1 7 8 10 4 1 3 6 8 10 4 6 6 1 6 6 7 4 10 3 10 1 3 2 10 6 9 9 5 2 9 2 9 8 10 2 10 3 3 2 3 8 6 2 7 10 7 2 7 2 8 9 6 2 5 4 4 5 3 3 9 10 9 4 9 3 9 5 3 6 6 1 3 6 10 3 10 2 6 10 10 10 4 8\n3 3 2 9 4 4 10 2 7 3 3 2 6 3 3 4 7 4 1 2 3 8 1 6 7 7 2 10 1 1 1 5 7 7 5 1 6 8 7 5 3 7 4 6 10 5 5 5 1 9", "output": "YES" }, { "input": "2 2 1000000000\n398981840 446967516\n477651114 577011341", "output": "NO" }, { "input": "1 1 1\n1\n1", "output": "NO" }, { "input": "1 1 1000000000\n502700350\n502700349", "output": "YES" }, { "input": "1 1 1000000000\n406009709\n406009709", "output": "NO" }, { "input": "2 1 1000000000\n699573624 308238132\n308238132", "output": "YES" }, { "input": "10 10 10\n2 10 8 1 10 4 6 1 3 7\n8 1 1 5 7 1 9 10 2 3", "output": "YES" }, { "input": "5 4 5\n1 2 2 3 4\n1 3 4 5", "output": "YES" } ]
30
0
0
16,217
191
Metro Scheme
[ "graphs", "greedy" ]
null
null
Berland is very concerned with privacy, so almost all plans and blueprints are secret. However, a spy of the neighboring state managed to steal the Bertown subway scheme. The Bertown Subway has *n* stations, numbered from 1 to *n*, and *m* bidirectional tunnels connecting them. All Bertown Subway consists of lines. To be more precise, there are two types of lines: circular and radial. A radial line is a sequence of stations *v*1,<=...,<=*v**k* (*k*<=&gt;<=1), where stations *v**i* and *v**i*<=+<=1 (*i*<=&lt;<=*k*) are connected by a tunnel and no station occurs in the line more than once (*v**i*<=β‰ <=*v**j* for *i*<=β‰ <=*j*). A loop line is a series of stations, *v*1,<=...,<=*v**k* (*k*<=&gt;<=2), where stations *v**i* ΠΈ *v**i*<=+<=1 are connected by a tunnel. In addition, stations *v*1 and *v**k* are also connected by a tunnel. No station is occurs in the loop line more than once. Note that a single station can be passed by any number of lines. According to Berland standards, there can't be more than one tunnel between two stations and each tunnel belongs to exactly one line. Naturally, each line has at least one tunnel. Between any two stations there is the way along the subway tunnels. In addition, in terms of graph theory, a subway is a vertex cactus: if we consider the subway as a graph in which the stations are the vertexes and the edges are tunnels, then each vertex lies on no more than one simple cycle. Unfortunately, scheme, stolen by the spy, had only the stations and the tunnels. It was impossible to determine to which line every tunnel corresponds. But to sabotage successfully, the spy needs to know what minimum and maximum number of lines may be in the Bertown subway. Help him!
The first line contains two integers *n* and *m* (1<=≀<=*n*<=≀<=105, 0<=≀<=*m*<=≀<=3Β·105) β€” the number of stations and the number of tunnels, correspondingly. Each of the next *m* lines contain two integers β€” the numbers of stations connected by the corresponding tunnel. The stations are numbered with integers from 1 to *n*. It is guaranteed that the graph that corresponds to the subway has no multiple edges or loops, it is connected and it is a vertex cactus.
Print two numbers β€” the minimum and maximum number of lines correspondingly.
[ "3 3\n1 2\n2 3\n3 1\n", "8 8\n1 2\n2 3\n3 4\n4 5\n6 4\n4 7\n7 2\n2 8\n", "6 6\n1 2\n2 3\n2 5\n5 6\n3 4\n3 5\n" ]
[ "1 3\n", "2 8\n", "3 6\n" ]
The subway scheme with minimum possible number of lines for the second sample is:
[]
186
0
-1
16,220
106
Buns
[ "dp" ]
C. Buns
2
256
Lavrenty, a baker, is going to make several buns with stuffings and sell them. Lavrenty has *n* grams of dough as well as *m* different stuffing types. The stuffing types are numerated from 1 to *m*. Lavrenty knows that he has *a**i* grams left of the *i*-th stuffing. It takes exactly *b**i* grams of stuffing *i* and *c**i* grams of dough to cook a bun with the *i*-th stuffing. Such bun can be sold for *d**i* tugriks. Also he can make buns without stuffings. Each of such buns requires *c*0 grams of dough and it can be sold for *d*0 tugriks. So Lavrenty can cook any number of buns with different stuffings or without it unless he runs out of dough and the stuffings. Lavrenty throws away all excess material left after baking. Find the maximum number of tugriks Lavrenty can earn.
The first line contains 4 integers *n*, *m*, *c*0 and *d*0 (1<=≀<=*n*<=≀<=1000, 1<=≀<=*m*<=≀<=10, 1<=≀<=*c*0,<=*d*0<=≀<=100). Each of the following *m* lines contains 4 integers. The *i*-th line contains numbers *a**i*, *b**i*, *c**i* and *d**i* (1<=≀<=*a**i*,<=*b**i*,<=*c**i*,<=*d**i*<=≀<=100).
Print the only number β€” the maximum number of tugriks Lavrenty can earn.
[ "10 2 2 1\n7 3 2 100\n12 3 1 10\n", "100 1 25 50\n15 5 20 10\n" ]
[ "241", "200" ]
To get the maximum number of tugriks in the first sample, you need to cook 2 buns with stuffing 1, 4 buns with stuffing 2 and a bun without any stuffing. In the second sample Lavrenty should cook 4 buns without stuffings.
[ { "input": "10 2 2 1\n7 3 2 100\n12 3 1 10", "output": "241" }, { "input": "100 1 25 50\n15 5 20 10", "output": "200" }, { "input": "10 1 5 2\n100 1 2 3", "output": "15" }, { "input": "10 1 5 11\n3 1 3 8", "output": "24" }, { "input": "10 2 11 5\n100 1 3 10\n100 1 2 4", "output": "30" }, { "input": "5 8 6 5\n1 2 5 4\n1 2 6 7\n1 2 3 5\n1 2 1 6\n1 2 8 3\n1 2 2 4\n1 2 5 6\n1 2 7 7", "output": "0" }, { "input": "300 4 100 2\n10 1 24 5\n10 1 25 6\n10 1 26 7\n10 1 27 8", "output": "87" }, { "input": "1 1 1 1\n1 1 1 1", "output": "1" }, { "input": "2 1 2 1\n1 2 1 1", "output": "1" }, { "input": "10 2 13 100\n20 1 3 10\n20 1 2 6", "output": "32" }, { "input": "100 5 8 80\n25 8 2 70\n27 6 7 30\n26 1 6 5\n7 1 1 86\n18 8 4 54", "output": "1670" }, { "input": "150 8 3 46\n39 4 10 25\n31 17 8 70\n37 2 13 1\n29 17 17 59\n54 20 5 39\n53 14 10 23\n50 12 16 41\n8 2 6 61", "output": "2300" }, { "input": "231 10 9 30\n98 11 5 17\n59 13 1 47\n83 1 7 2\n42 21 1 6\n50 16 2 9\n44 10 5 31\n12 20 8 9\n61 23 7 2\n85 18 2 19\n82 25 10 20", "output": "1065" }, { "input": "345 10 5 45\n1 23 14 55\n51 26 15 11\n65 4 16 36\n81 14 13 25\n8 9 13 60\n43 4 7 59\n85 11 14 35\n82 13 5 49\n85 28 15 3\n51 21 18 53", "output": "3129" }, { "input": "401 10 2 82\n17 9 14 48\n79 4 3 38\n1 2 6 31\n45 2 9 60\n45 2 4 50\n6 1 3 36\n3 1 19 37\n78 3 8 33\n59 8 19 19\n65 10 2 61", "output": "16400" }, { "input": "777 10 23 20\n50 90 86 69\n33 90 59 73\n79 26 35 31\n57 48 97 4\n5 10 48 87\n35 99 33 34\n7 32 54 35\n56 25 10 38\n5 3 89 76\n13 33 91 66", "output": "734" }, { "input": "990 10 7 20\n38 82 14 69\n5 66 51 5\n11 26 91 11\n29 12 73 96\n93 82 48 59\n19 15 5 50\n15 36 6 63\n16 57 94 90\n45 3 57 72\n61 41 47 18", "output": "2850" }, { "input": "1000 10 51 56\n2 62 82 65\n37 90 87 97\n11 94 47 95\n49 24 97 24\n33 38 40 31\n27 15 17 66\n91 80 34 71\n60 93 42 94\n9 35 73 68\n93 65 83 58", "output": "1145" }, { "input": "1000 10 1 53\n63 1 1 58\n58 1 2 28\n100 1 1 25\n61 1 1 90\n96 2 2 50\n19 2 1 90\n7 2 1 30\n90 1 2 5\n34 2 1 12\n3 2 1 96", "output": "55948" }, { "input": "1000 10 1 65\n77 1 1 36\n74 1 1 41\n96 1 1 38\n48 1 1 35\n1 1 1 54\n42 1 1 67\n26 1 1 23\n43 1 1 89\n82 1 1 7\n45 1 1 63", "output": "66116" }, { "input": "1000 10 1 87\n100 1 1 38\n100 1 1 45\n100 1 1 73\n100 1 1 89\n100 1 1 38\n100 1 1 13\n100 1 1 93\n100 1 1 89\n100 1 1 71\n100 1 1 29", "output": "88000" }, { "input": "1000 10 1 7\n100 1 1 89\n100 1 1 38\n100 1 1 13\n100 1 1 93\n100 1 1 89\n100 1 1 38\n100 1 1 45\n100 1 1 73\n100 1 1 71\n100 1 1 29", "output": "57800" }, { "input": "1000 10 1 100\n100 1 1 100\n100 1 1 100\n100 1 1 100\n100 1 1 100\n100 1 1 100\n100 1 1 100\n100 1 1 100\n100 1 1 100\n100 1 1 100\n100 1 1 100", "output": "100000" }, { "input": "99 10 100 100\n100 1 100 100\n100 1 100 100\n100 1 100 100\n100 1 100 100\n100 1 100 100\n100 1 100 100\n100 1 100 100\n100 1 100 100\n100 1 100 100\n100 1 100 100", "output": "0" }, { "input": "1000 10 100 75\n100 97 100 95\n100 64 100 78\n100 82 100 35\n100 51 100 64\n100 67 100 25\n100 79 100 33\n100 65 100 85\n100 99 100 78\n100 53 100 74\n100 87 100 73", "output": "786" }, { "input": "999 10 5 100\n100 1 10 100\n100 1 10 100\n100 1 10 100\n100 1 10 100\n100 1 10 100\n100 1 10 100\n100 1 10 100\n100 1 10 100\n100 1 10 100\n100 1 10 100", "output": "19900" }, { "input": "1000 10 50 100\n7 1 80 100\n5 1 37 100\n9 1 25 100\n7 1 17 100\n6 1 10 100\n5 1 15 100\n6 1 13 100\n2 1 14 100\n4 1 17 100\n3 1 32 100", "output": "4800" }, { "input": "1000 10 1 1\n1 2 1 97\n1 2 1 95\n1 2 1 99\n1 2 1 98\n1 2 1 93\n1 2 1 91\n1 2 1 90\n1 2 1 94\n1 2 1 92\n1 2 1 99", "output": "1000" }, { "input": "1 10 1 97\n1 1 1 98\n1 1 1 99\n1 1 1 76\n1 1 1 89\n1 1 1 64\n1 1 1 83\n1 1 1 72\n1 1 1 66\n1 1 1 54\n1 1 1 73", "output": "99" }, { "input": "3 10 10 98\n10 5 5 97\n6 7 1 56\n23 10 5 78\n40 36 4 35\n30 50 1 30\n60 56 8 35\n70 90 2 17\n10 11 3 68\n1 2 17 70\n13 4 8 19", "output": "0" }, { "input": "1000 1 23 76\n74 22 14 5", "output": "3268" }, { "input": "1000 2 95 56\n58 54 66 61\n61 14 67 65", "output": "713" }, { "input": "1000 3 67 88\n90 86 66 17\n97 38 63 17\n55 78 39 51", "output": "1232" }, { "input": "1000 4 91 20\n74 18 18 73\n33 10 59 21\n7 42 87 79\n9 100 77 100", "output": "515" }, { "input": "1000 5 63 52\n6 98 18 77\n17 34 3 73\n59 6 35 7\n61 16 85 64\n73 62 40 11", "output": "804" }, { "input": "1000 6 87 32\n90 30 70 33\n53 6 99 77\n59 22 83 35\n65 32 93 28\n85 50 60 7\n15 15 5 82", "output": "771" }, { "input": "1000 7 59 64\n22 62 70 89\n37 78 43 29\n11 86 83 63\n17 48 1 92\n97 38 80 55\n15 3 89 42\n87 80 62 35", "output": "1024" }, { "input": "1000 8 31 96\n6 94 70 93\n73 2 39 33\n63 50 31 91\n21 64 9 56\n61 26 100 51\n67 39 21 50\n79 4 2 71\n100 9 18 86", "output": "4609" }, { "input": "1000 9 55 28\n38 74 22 49\n9 74 83 85\n63 66 79 19\n25 32 17 20\n73 62 20 47\n19 27 53 58\n71 80 94 7\n56 69 62 98\n49 7 65 76", "output": "831" }, { "input": "1000 10 67 55\n10 21 31 19\n95 29 53 1\n55 53 19 18\n26 88 19 94\n31 1 45 50\n70 38 33 93\n2 12 7 95\n54 37 81 31\n65 32 63 16\n93 66 98 38", "output": "1161" }, { "input": "1000 10 37 38\n65 27 78 14\n16 70 78 66\n93 86 91 43\n95 6 72 86\n72 59 94 36\n66 58 96 40\n41 72 64 4\n26 47 69 13\n85 2 52 15\n34 62 16 79", "output": "1156" }, { "input": "1000 10 58 21\n73 85 73 10\n38 60 55 31\n32 66 62 16\n63 76 73 78\n61 17 92 70\n61 79 11 87\n27 31 21 62\n47 9 4 94\n4 71 42 61\n76 5 35 72", "output": "1823" }, { "input": "12 2 100 1\n100 1 9 10\n100 1 4 4", "output": "12" }, { "input": "1 1 1 10\n100 100 1 100", "output": "100" }, { "input": "10 3 5 1\n100 1 3 7\n100 1 2 5\n1 1 1 10", "output": "32" }, { "input": "10 3 5 1\n100 1 3 7\n100 1 2 5\n1 1 1 10", "output": "32" }, { "input": "1000 10 1 1\n100 1 1 1\n100 1 1 1\n100 1 1 1\n100 1 1 1\n100 1 1 1\n100 1 1 1\n100 1 1 1\n100 1 1 1\n100 1 1 1\n100 1 1 1", "output": "1000" }, { "input": "10 2 100 1\n4 4 5 7\n6 2 3 4", "output": "12" }, { "input": "8 2 10 10\n5 5 5 15\n50 5 4 8", "output": "16" }, { "input": "8 2 10 10\n5 5 5 15\n50 5 4 8", "output": "16" }, { "input": "4 1 2 4\n10 1 3 7", "output": "8" }, { "input": "4 1 2 4\n10 1 3 7", "output": "8" }, { "input": "10 2 5 1\n100 1 2 5\n100 1 3 8", "output": "26" }, { "input": "1000 10 10 10\n100 1 1 1\n100 1 1 2\n100 1 2 1\n100 1 2 2\n100 1 1 1\n100 1 2 3\n100 1 3 2\n100 1 3 3\n100 1 1 3\n100 1 3 1", "output": "1400" }, { "input": "10 3 5 1\n100 1 3 7\n100 1 2 5\n1 1 1 10", "output": "32" } ]
1,620
6,963,200
3.58203
16,239
754
Vladik and chat
[ "brute force", "constructive algorithms", "dp", "implementation", "strings" ]
null
null
Recently Vladik discovered a new entertainmentΒ β€” coding bots for social networks. He would like to use machine learning in his bots so now he want to prepare some learning data for them. At first, he need to download *t* chats. Vladik coded a script which should have downloaded the chats, however, something went wrong. In particular, some of the messages have no information of their sender. It is known that if a person sends several messages in a row, they all are merged into a single message. It means that there could not be two or more messages in a row with the same sender. Moreover, a sender never mention himself in his messages. Vladik wants to recover senders of all the messages so that each two neighboring messages will have different senders and no sender will mention himself in his messages. He has no idea of how to do this, and asks you for help. Help Vladik to recover senders in each of the chats!
The first line contains single integer *t* (1<=≀<=*t*<=≀<=10) β€” the number of chats. The *t* chats follow. Each chat is given in the following format. The first line of each chat description contains single integer *n* (1<=≀<=*n*<=≀<=100)Β β€” the number of users in the chat. The next line contains *n* space-separated distinct usernames. Each username consists of lowercase and uppercase English letters and digits. The usernames can't start with a digit. Two usernames are different even if they differ only with letters' case. The length of username is positive and doesn't exceed 10 characters. The next line contains single integer *m* (1<=≀<=*m*<=≀<=100)Β β€” the number of messages in the chat. The next *m* line contain the messages in the following formats, one per line: - &lt;username&gt;:&lt;text&gt;Β β€” the format of a message with known sender. The username should appear in the list of usernames of the chat. - &lt;?&gt;:&lt;text&gt;Β β€” the format of a message with unknown sender. The text of a message can consist of lowercase and uppercase English letter, digits, characters '.' (dot), ',' (comma), '!' (exclamation mark), '?' (question mark) and ' ' (space). The text doesn't contain trailing spaces. The length of the text is positive and doesn't exceed 100 characters. We say that a text mention a user if his username appears in the text as a word. In other words, the username appears in a such a position that the two characters before and after its appearance either do not exist or are not English letters or digits. For example, the text "Vasya, masha13 and Kate!" can mention users "Vasya", "masha13", "and" and "Kate", but not "masha". It is guaranteed that in each chat no known sender mention himself in his messages and there are no two neighboring messages with the same known sender.
Print the information about the *t* chats in the following format: If it is not possible to recover senders, print single line "Impossible" for this chat. Otherwise print *m* messages in the following format: &lt;username&gt;:&lt;text&gt; If there are multiple answers, print any of them.
[ "1\n2\nVladik netman\n2\n?: Hello, Vladik!\n?: Hi\n", "1\n2\nnetman vladik\n3\nnetman:how are you?\n?:wrong message\nvladik:im fine\n", "2\n3\nnetman vladik Fedosik\n2\n?: users are netman, vladik, Fedosik\nvladik: something wrong with this chat\n4\nnetman tigerrrrr banany2001 klinchuh\n4\n?: tigerrrrr, banany2001, klinchuh, my favourite team ever, are you ready?\nklinchuh: yes, coach!\n?: yes, netman\nbanany2001: yes of course.\n" ]
[ "netman: Hello, Vladik!\nVladik: Hi\n", "Impossible\n", "Impossible\nnetman: tigerrrrr, banany2001, klinchuh, my favourite team ever, are you ready?\nklinchuh: yes, coach!\ntigerrrrr: yes, netman\nbanany2001: yes of course.\n" ]
none
[ { "input": "1\n2\nVladik netman\n2\n?: Hello, Vladik!\n?: Hi", "output": "netman: Hello, Vladik!\nVladik: Hi" }, { "input": "1\n2\nnetman vladik\n3\nnetman:how are you?\n?:wrong message\nvladik:im fine", "output": "Impossible" }, { "input": "2\n3\nnetman vladik Fedosik\n2\n?: users are netman, vladik, Fedosik\nvladik: something wrong with this chat\n4\nnetman tigerrrrr banany2001 klinchuh\n4\n?: tigerrrrr, banany2001, klinchuh, my favourite team ever, are you ready?\nklinchuh: yes, coach!\n?: yes, netman\nbanany2001: yes of course.", "output": "Impossible\nnetman: tigerrrrr, banany2001, klinchuh, my favourite team ever, are you ready?\nklinchuh: yes, coach!\ntigerrrrr: yes, netman\nbanany2001: yes of course." }, { "input": "1\n1\nb\n1\nb:lala!", "output": "b:lala!" }, { "input": "1\n1\nb\n1\n?:lala b!", "output": "Impossible" }, { "input": "1\n1\nb\n2\n?:lala hhe!\nb:wat?", "output": "Impossible" }, { "input": "1\n3\nA B C\n3\nA: HI\n?: HI\nB: HI", "output": "A: HI\nC: HI\nB: HI" } ]
124
0
0
16,270
416
President's Path
[ "dp", "graphs", "shortest paths" ]
null
null
Good old Berland has *n* cities and *m* roads. Each road connects a pair of distinct cities and is bidirectional. Between any pair of cities, there is at most one road. For each road, we know its length. We also know that the President will soon ride along the Berland roads from city *s* to city *t*. Naturally, he will choose one of the shortest paths from *s* to *t*, but nobody can say for sure which path he will choose. The Minister for Transport is really afraid that the President might get upset by the state of the roads in the country. That is the reason he is planning to repair the roads in the possible President's path. Making the budget for such an event is not an easy task. For all possible distinct pairs *s*,<=*t* (*s*<=&lt;<=*t*) find the number of roads that lie on at least one shortest path from *s* to *t*.
The first line of the input contains integers *n*,<=*m* (2<=≀<=*n*<=≀<=500, 0<=≀<=*m*<=≀<=*n*Β·(*n*<=-<=1)<=/<=2) β€” the number of cities and roads, correspondingly. Then *m* lines follow, containing the road descriptions, one description per line. Each description contains three integers *x**i*,<=*y**i*,<=*l**i* (1<=≀<=*x**i*,<=*y**i*<=≀<=*n*,<=*x**i*<=β‰ <=*y**i*,<=1<=≀<=*l**i*<=≀<=106), where *x**i*,<=*y**i* are the numbers of the cities connected by the *i*-th road and *l**i* is its length.
Print the sequence of integers *c*12,<=*c*13,<=...,<=*c*1*n*,<=*c*23,<=*c*24,<=...,<=*c*2*n*,<=...,<=*c**n*<=-<=1,<=*n*, where *c**st* is the number of roads that can lie on the shortest path from *s* to *t*. Print the elements of sequence *c* in the described order. If the pair of cities *s* and *t* don't have a path between them, then *c**st*<==<=0.
[ "5 6\n1 2 1\n2 3 1\n3 4 1\n4 1 1\n2 4 2\n4 5 4\n" ]
[ "1 4 1 2 1 5 6 1 2 1 " ]
none
[]
31
0
0
16,271
117
Cycle
[ "dfs and similar", "graphs" ]
null
null
A tournament is a directed graph without self-loops in which every pair of vertexes is connected by exactly one directed edge. That is, for any two vertexes *u* and *v* (*u*<=β‰ <=*v*) exists either an edge going from *u* to *v*, or an edge from *v* to *u*. You are given a tournament consisting of *n* vertexes. Your task is to find there a cycle of length three.
The first line contains an integer *n* (1<=≀<=*n*<=≀<=5000). Next *n* lines contain the adjacency matrix *A* of the graph (without spaces). *A**i*,<=*j*<==<=1 if the graph has an edge going from vertex *i* to vertex *j*, otherwise *A**i*,<=*j*<==<=0. *A**i*,<=*j* stands for the *j*-th character in the *i*-th line. It is guaranteed that the given graph is a tournament, that is, *A**i*,<=*i*<==<=0,<=*A**i*,<=*j*<=β‰ <=*A**j*,<=*i* (1<=≀<=*i*,<=*j*<=≀<=*n*,<=*i*<=β‰ <=*j*).
Print three distinct vertexes of the graph *a*1, *a*2, *a*3 (1<=≀<=*a**i*<=≀<=*n*), such that *A**a*1,<=*a*2<==<=*A**a*2,<=*a*3<==<=*A**a*3,<=*a*1<==<=1, or "-1", if a cycle whose length equals three does not exist. If there are several solutions, print any of them.
[ "5\n00100\n10000\n01001\n11101\n11000\n", "5\n01111\n00000\n01000\n01100\n01110\n" ]
[ "1 3 2 ", "-1\n" ]
none
[ { "input": "5\n00100\n10000\n01001\n11101\n11000", "output": "1 3 2 " }, { "input": "5\n01111\n00000\n01000\n01100\n01110", "output": "-1" }, { "input": "5\n01000\n00101\n10010\n11001\n10100", "output": "1 2 3 " }, { "input": "5\n00110\n10110\n00011\n00000\n11010", "output": "1 3 5 " }, { "input": "10\n0011000010\n1011001101\n0000101100\n0010101010\n1100000100\n1111101100\n1000100000\n1001001011\n0110111001\n1011111000", "output": "1 3 5 " }, { "input": "10\n0111001000\n0011111000\n0000110110\n0010101110\n1000011001\n1001000010\n0010010101\n1100110000\n1100101100\n1111010110", "output": "1 3 5 " }, { "input": "10\n0101111011\n0001111111\n1100011110\n0010011000\n0011000110\n0000101011\n0000100000\n1001011011\n0001001000\n0011101010", "output": "1 4 3 " }, { "input": "10\n0000010011\n1001001111\n1100001110\n1010010011\n1111011000\n0110000001\n1001010100\n1001110000\n0000111101\n0010101100", "output": "1 6 2 " }, { "input": "10\n0000000000\n1001100111\n1101101111\n1000000011\n1001000111\n1111101111\n1101100111\n1001000011\n1000000001\n1000000000", "output": "-1" }, { "input": "1\n0", "output": "-1" }, { "input": "2\n00\n10", "output": "-1" }, { "input": "3\n001\n100\n010", "output": "1 3 2 " }, { "input": "3\n010\n001\n100", "output": "1 2 3 " }, { "input": "2\n01\n00", "output": "-1" }, { "input": "3\n011\n000\n010", "output": "-1" }, { "input": "4\n0000\n1010\n1001\n1100", "output": "2 3 4 " }, { "input": "5\n01111\n00111\n00010\n00001\n00100", "output": "3 4 5 " } ]
0
0
-1
16,292
852
Dating
[ "brute force", "dfs and similar", "graphs", "trees" ]
null
null
This story is happening in a town named BubbleLand. There are *n* houses in BubbleLand. In each of these *n* houses lives a boy or a girl. People there really love numbers and everyone has their favorite number *f*. That means that the boy or girl that lives in the *i*-th house has favorite number equal to *f**i*. The houses are numerated with numbers 1 to *n*. The houses are connected with *n*<=-<=1 bidirectional roads and you can travel from any house to any other house in the town. There is exactly one path between every pair of houses. A new dating had agency opened their offices in this mysterious town and the citizens were very excited. They immediately sent *q* questions to the agency and each question was of the following format: - *a* *b*Β β€” asking how many ways are there to choose a couple (boy and girl) that have the same favorite number and live in one of the houses on the unique path from house *a* to house *b*. Help the dating agency to answer the questions and grow their business.
The first line contains an integer *n* (1<=≀<=*n*<=≀<=105), the number of houses in the town. The second line contains *n* integers, where the *i*-th number is 1 if a boy lives in the *i*-th house or 0 if a girl lives in *i*-th house. The third line contains *n* integers, where the *i*-th number represents the favorite number *f**i* (1<=≀<=*f**i*<=≀<=109) of the girl or boy that lives in the *i*-th house. The next *n*<=-<=1 lines contain information about the roads and the *i*-th line contains two integers *a**i* and *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*) which means that there exists road between those two houses. It is guaranteed that it's possible to reach any house from any other. The following line contains an integer *q* (1<=≀<=*q*<=≀<=105), the number of queries. Each of the following *q* lines represents a question and consists of two integers *a* and *b* (1<=≀<=*a*,<=*b*<=≀<=*n*).
For each of the *q* questions output a single number, the answer to the citizens question.
[ "7\n1 0 0 1 0 1 0\n9 2 9 2 2 9 9\n2 6\n1 2\n4 2\n6 5\n3 6\n7 4\n2\n1 3\n7 5\n" ]
[ "2\n3\n" ]
In the first question from house 1 to house 3, the potential couples are (1, 3) and (6, 3). In the second question from house 7 to house 5, the potential couples are (7, 6), (4, 2) and (4, 5).
[]
46
0
0
16,294
0
none
[ "none" ]
null
null
Emuskald considers himself a master of flow algorithms. Now he has completed his most ingenious program yet β€” it calculates the maximum flow in an undirected graph. The graph consists of *n* vertices and *m* edges. Vertices are numbered from 1 to *n*. Vertices 1 and *n* being the source and the sink respectively. However, his max-flow algorithm seems to have a little flaw β€” it only finds the flow volume for each edge, but not its direction. Help him find for each edge the direction of the flow through this edges. Note, that the resulting flow should be correct maximum flow. More formally. You are given an undirected graph. For each it's undirected edge (*a**i*, *b**i*) you are given the flow volume *c**i*. You should direct all edges in such way that the following conditions hold: 1. for each vertex *v* (1<=&lt;<=*v*<=&lt;<=*n*), sum of *c**i* of incoming edges is equal to the sum of *c**i* of outcoming edges; 1. vertex with number 1 has no incoming edges; 1. the obtained directed graph does not have cycles.
The first line of input contains two space-separated integers *n* and *m* (2<=≀<=*n*<=≀<=2Β·105, *n*<=-<=1<=≀<=*m*<=≀<=2Β·105), the number of vertices and edges in the graph. The following *m* lines contain three space-separated integers *a**i*, *b**i* and *c**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*, *a**i*<=β‰ <=*b**i*, 1<=≀<=*c**i*<=≀<=104), which means that there is an undirected edge from *a**i* to *b**i* with flow volume *c**i*. It is guaranteed that there are no two edges connecting the same vertices; the given graph is connected; a solution always exists.
Output *m* lines, each containing one integer *d**i*, which should be 0 if the direction of the *i*-th edge is *a**i*<=β†’<=*b**i* (the flow goes from vertex *a**i* to vertex *b**i*) and should be 1 otherwise. The edges are numbered from 1 to *m* in the order they are given in the input. If there are several solutions you can print any of them.
[ "3 3\n3 2 10\n1 2 10\n3 1 5\n", "4 5\n1 2 10\n1 3 10\n2 3 5\n4 2 15\n3 4 5\n" ]
[ "1\n0\n1\n", "0\n0\n1\n1\n0\n" ]
In the first test case, 10 flow units pass through path <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/609340f155794c4e9eebcd9cdfa23c73cf982f28.png" style="max-width: 100.0%;max-height: 100.0%;"/>, and 5 flow units pass directly from source to sink: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/04481aced8a9d501ae5d785ab654c542ff5497a1.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
[ { "input": "3 3\n3 2 10\n1 2 10\n3 1 5", "output": "1\n0\n1" }, { "input": "4 5\n1 2 10\n1 3 10\n2 3 5\n4 2 15\n3 4 5", "output": "0\n0\n1\n1\n0" }, { "input": "10 17\n8 1 1\n4 8 2\n7 10 8\n1 4 1\n5 4 3\n6 9 6\n3 5 4\n1 9 1\n3 9 5\n7 1 1\n1 2 1\n1 3 1\n6 7 7\n8 2 1\n1 10 1\n1 5 1\n6 1 1", "output": "1\n1\n0\n0\n1\n1\n1\n0\n0\n1\n0\n0\n0\n1\n0\n0\n1" }, { "input": "10 20\n3 8 41\n1 2 21\n9 1 31\n1 3 53\n5 9 67\n10 1 8\n6 1 16\n5 2 21\n1 7 50\n5 4 38\n6 4 16\n4 8 16\n5 10 93\n9 10 126\n8 9 16\n4 1 38\n5 7 50\n3 9 12\n1 5 10\n5 8 41", "output": "0\n0\n1\n0\n0\n1\n1\n1\n0\n1\n0\n0\n0\n0\n0\n1\n1\n0\n0\n1" }, { "input": "2 1\n1 2 1", "output": "0" }, { "input": "2 1\n2 1 1", "output": "1" }, { "input": "3 2\n1 2 1\n2 3 1", "output": "0\n0" }, { "input": "4 4\n4 3 5000\n1 2 10000\n3 1 5000\n4 2 10000", "output": "1\n0\n1\n1" }, { "input": "3 3\n3 1 10000\n2 1 10000\n3 2 10000", "output": "1\n1\n1" }, { "input": "3 3\n3 2 10000\n2 1 10000\n3 1 10000", "output": "1\n1\n1" }, { "input": "10 17\n9 1 8\n7 10 1\n5 4 4\n1 10 1\n3 10 1\n10 5 1\n6 3 6\n10 4 1\n4 6 5\n7 5 3\n2 10 1\n9 3 7\n9 10 1\n8 10 1\n10 6 1\n2 7 2\n2 8 1", "output": "1\n0\n1\n0\n0\n1\n1\n1\n1\n1\n0\n0\n0\n0\n1\n1\n0" }, { "input": "5 6\n1 3 10\n2 1 10\n3 5 10\n1 4 10\n2 5 10\n4 5 10", "output": "0\n1\n0\n0\n0\n0" }, { "input": "5 6\n2 1 8\n5 2 8\n5 3 4\n4 1 9\n3 1 4\n5 4 9", "output": "1\n1\n1\n1\n1\n1" }, { "input": "10 23\n10 5 94\n6 9 20\n10 2 79\n3 9 63\n1 6 80\n7 8 21\n3 5 6\n3 1 94\n2 5 21\n1 2 100\n1 7 79\n6 10 59\n8 1 60\n10 3 37\n9 1 37\n4 8 40\n7 10 100\n6 4 41\n5 1 79\n8 10 79\n9 10 80\n10 4 60\n4 1 59", "output": "1\n1\n1\n0\n0\n1\n1\n1\n0\n0\n0\n0\n1\n1\n1\n0\n0\n0\n1\n0\n0\n1\n1" }, { "input": "9 9\n1 2 1\n2 3 1\n3 4 1\n4 5 1\n5 6 1\n6 7 1\n7 9 1\n8 9 1\n1 8 1", "output": "0\n0\n0\n0\n0\n0\n0\n0\n0" }, { "input": "6 6\n1 2 1\n2 6 1\n1 3 1\n3 4 1\n4 5 1\n5 6 1", "output": "0\n0\n0\n0\n0\n0" } ]
2,000
67,174,400
0
16,315
0
none
[ "none" ]
null
null
ΠŸΠ°ΠΌΡΡ‚ΡŒ ΠΊΠΎΠΌΠΏΡŒΡŽΡ‚Π΅Ρ€Π° состоит ΠΈΠ· *n* ячССк, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ выстроСны Π² ряд. ΠŸΡ€ΠΎΠ½ΡƒΠΌΠ΅Ρ€ΡƒΠ΅ΠΌ ячСйки ΠΎΡ‚ 1 Π΄ΠΎ *n* слСва Π½Π°ΠΏΡ€Π°Π²ΠΎ. ΠŸΡ€ΠΎ ΠΊΠ°ΠΆΠ΄ΡƒΡŽ ячСйку извСстно, свободна ΠΎΠ½Π° ΠΈΠ»ΠΈ ΠΏΡ€ΠΈΠ½Π°Π΄Π»Π΅ΠΆΠΈΡ‚ ΠΊΠ°ΠΊΠΎΠΌΡƒ-Π»ΠΈΠ±ΠΎ процСссу (Π² Ρ‚Π°ΠΊΠΎΠΌ случаС извСстСн процСсс, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌΡƒ ΠΎΠ½Π° ΠΏΡ€ΠΈΠ½Π°Π΄Π»Π΅ΠΆΠΈΡ‚). Для ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ процСсса извСстно, Ρ‡Ρ‚ΠΎ ΠΏΡ€ΠΈΠ½Π°Π΄Π»Π΅ΠΆΠ°Ρ‰ΠΈΠ΅ Π΅ΠΌΡƒ ячСйки Π·Π°Π½ΠΈΠΌΠ°ΡŽΡ‚ Π² памяти Π½Π΅ΠΏΡ€Π΅Ρ€Ρ‹Π²Π½Ρ‹ΠΉ участок. Π‘ ΠΏΠΎΠΌΠΎΡ‰ΡŒΡŽ ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΉ Π²ΠΈΠ΄Π° Β«ΠΏΠ΅Ρ€Π΅ΠΏΠΈΡΠ°Ρ‚ΡŒ Π΄Π°Π½Π½Ρ‹Π΅ ΠΈΠ· занятой ячСйки Π² ΡΠ²ΠΎΠ±ΠΎΠ΄Π½ΡƒΡŽ, Π° Π·Π°Π½ΡΡ‚ΡƒΡŽ Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ ΡΡ‡ΠΈΡ‚Π°Ρ‚ΡŒ свободной» трСбуСтся Ρ€Π°ΡΠΏΠΎΠ»ΠΎΠΆΠΈΡ‚ΡŒ всС ΠΏΡ€ΠΈΠ½Π°Π΄Π»Π΅ΠΆΠ°Ρ‰ΠΈΠ΅ процСссам ячСйки Π² Π½Π°Ρ‡Π°Π»Π΅ памяти ΠΊΠΎΠΌΠΏΡŒΡŽΡ‚Π΅Ρ€Π°. Π”Ρ€ΡƒΠ³ΠΈΠΌΠΈ словами, любая свободная ячСйка Π΄ΠΎΠ»ΠΆΠ½Π° Ρ€Π°ΡΠΏΠΎΠ»Π°Π³Π°Ρ‚ΡŒΡΡ ΠΏΡ€Π°Π²Π΅Π΅ (ΠΈΠΌΠ΅Ρ‚ΡŒ больший Π½ΠΎΠΌΠ΅Ρ€) любой занятой. Π’Π°ΠΌ Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎ Π½Π°ΠΉΡ‚ΠΈ минимальноС количСство ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΉ пСрСписывания Π΄Π°Π½Π½Ρ‹Ρ… ΠΈΠ· ΠΎΠ΄Π½ΠΎΠΉ ячСйки Π² Π΄Ρ€ΡƒΠ³ΡƒΡŽ, с ΠΏΠΎΠΌΠΎΡ‰ΡŒΡŽ ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Ρ… ΠΌΠΎΠΆΠ½ΠΎ Π΄ΠΎΡΡ‚ΠΈΡ‡ΡŒ описанных условий. Допустимо, Ρ‡Ρ‚ΠΎ ΠΎΡ‚Π½ΠΎΡΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹ΠΉ порядок ячССк Π² памяти для ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΠΈΠ· процСссов измСнится послС Π΄Π΅Ρ„Ρ€Π°Π³ΠΌΠ΅Π½Ρ‚Π°Ρ†ΠΈΠΈ, Π½ΠΎ ΠΎΡ‚Π½ΠΎΡΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹ΠΉ порядок самих процСссов Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΎΡΡ‚Π°Ρ‚ΡŒΡΡ Π±Π΅Π· ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠΉ. Π­Ρ‚ΠΎ Π·Π½Π°Ρ‡ΠΈΡ‚, Ρ‡Ρ‚ΠΎ Ссли всС ячСйки, ΠΏΡ€ΠΈΠ½Π°Π΄Π»Π΅ΠΆΠ°Ρ‰ΠΈΠ΅ процСссу *i*, Π½Π°Ρ…ΠΎΠ΄ΠΈΠ»ΠΈΡΡŒ Π² памяти Ρ€Π°Π½ΡŒΡˆΠ΅ всСх ячССк процСсса *j*, Ρ‚ΠΎ ΠΈ послС ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Ρ‰Π΅Π½ΠΈΠΉ это условиС Π΄ΠΎΠ»ΠΆΠ½ΠΎ Π²Ρ‹ΠΏΠΎΠ»Π½ΡΡ‚ΡŒΡΡ. Π‘Ρ‡ΠΈΡ‚Π°ΠΉΡ‚Π΅, Ρ‡Ρ‚ΠΎ Π½ΠΎΠΌΠ΅Ρ€Π° всСх процСссов ΡƒΠ½ΠΈΠΊΠ°Π»ΡŒΠ½Ρ‹, хотя Π±Ρ‹ ΠΎΠ΄Π½Π° ячСйка памяти занята ΠΊΠ°ΠΊΠΈΠΌ-Π»ΠΈΠ±ΠΎ процСссом.
Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΉ строкС Π²Ρ…ΠΎΠ΄Π½Ρ‹Ρ… Π΄Π°Π½Π½Ρ‹Ρ… записано число *n* (1<=≀<=*n*<=≀<=200<=000)Β β€” количСство ячССк Π² памяти ΠΊΠΎΠΌΠΏΡŒΡŽΡ‚Π΅Ρ€Π°. Π’ΠΎ Π²Ρ‚ΠΎΡ€ΠΎΠΉ строкС Π²Ρ…ΠΎΠ΄Π½Ρ‹Ρ… Π΄Π°Π½Π½Ρ‹Ρ… ΡΠ»Π΅Π΄ΡƒΡŽΡ‚ *n* Ρ†Π΅Π»Ρ‹Ρ… чисСл *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=*n*), Π³Π΄Π΅ *a**i* Ρ€Π°Π²Π½ΠΎ Π»ΠΈΠ±ΠΎ 0 (это ΠΎΠ·Π½Π°Ρ‡Π°Π΅Ρ‚, Ρ‡Ρ‚ΠΎ *i*-я ячСйка памяти свободна), Π»ΠΈΠ±ΠΎ Π½ΠΎΠΌΠ΅Ρ€Ρƒ процСсса, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌΡƒ ΠΏΡ€ΠΈΠ½Π°Π΄Π»Π΅ΠΆΠΈΡ‚ *i*-я ячСйка памяти. ГарантируСтся, Ρ‡Ρ‚ΠΎ хотя Π±Ρ‹ ΠΎΠ΄Π½ΠΎ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ *a**i* Π½Π΅ Ρ€Π°Π²Π½ΠΎ 0. ΠŸΡ€ΠΎΡ†Π΅ΡΡΡ‹ ΠΏΡ€ΠΎΠ½ΡƒΠΌΠ΅Ρ€ΠΎΠ²Π°Π½Ρ‹ Ρ†Π΅Π»Ρ‹ΠΌΠΈ числами ΠΎΡ‚ 1 Π΄ΠΎ *n* Π² ΠΏΡ€ΠΎΠΈΠ·Π²ΠΎΠ»ΡŒΠ½ΠΎΠΌ порядкС. ΠŸΡ€ΠΈ этом процСссы Π½Π΅ ΠΎΠ±ΡΠ·Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎ ΠΏΡ€ΠΎΠ½ΡƒΠΌΠ΅Ρ€ΠΎΠ²Π°Π½Ρ‹ ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΠ½Ρ‹ΠΌΠΈ числами.
Π’Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ ΠΎΠ΄Π½ΠΎ Ρ†Π΅Π»ΠΎΠ΅ число — минимальноС количСство ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΉ, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ Π½ΡƒΠΆΠ½ΠΎ ΡΠ΄Π΅Π»Π°Ρ‚ΡŒ для Π΄Π΅Ρ„Ρ€Π°Π³ΠΌΠ΅Π½Ρ‚Π°Ρ†ΠΈΠΈ памяти.
[ "4\n0 2 2 1\n", "8\n0 8 8 8 0 4 4 2\n" ]
[ "2\n", "4\n" ]
Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΌ тСстовом ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π΅ достаточно Π΄Π²ΡƒΡ… ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΉ: 1. ΠŸΠ΅Ρ€Π΅ΠΏΠΈΡΠ°Ρ‚ΡŒ Π΄Π°Π½Π½Ρ‹Π΅ ΠΈΠ· Ρ‚Ρ€Π΅Ρ‚ΡŒΠ΅ΠΉ ячСйки Π² ΠΏΠ΅Ρ€Π²ΡƒΡŽ. ПослС этого ΠΏΠ°ΠΌΡΡ‚ΡŒ ΠΊΠΎΠΌΠΏΡŒΡŽΡ‚Π΅Ρ€Π° ΠΏΡ€ΠΈΠΌΠ΅Ρ‚ Π²ΠΈΠ΄: 2Β 2Β 0Β 1. 1. ΠŸΠ΅Ρ€Π΅ΠΏΠΈΡΠ°Ρ‚ΡŒ Π΄Π°Π½Π½Ρ‹Π΅ ΠΈΠ· Ρ‡Π΅Ρ‚Π²Π΅Ρ€Ρ‚ΠΎΠΉ ячСйки Π² Ρ‚Ρ€Π΅Ρ‚ΡŒΡŽ. ПослС этого ΠΏΠ°ΠΌΡΡ‚ΡŒ ΠΊΠΎΠΌΠΏΡŒΡŽΡ‚Π΅Ρ€Π° ΠΏΡ€ΠΈΠΌΠ΅Ρ‚ Π²ΠΈΠ΄: 2Β 2Β 1Β 0.
[ { "input": "4\n0 2 2 1", "output": "2" }, { "input": "8\n0 8 8 8 0 4 4 2", "output": "4" }, { "input": "5\n0 0 0 1 1", "output": "2" }, { "input": "6\n0 0 0 3 0 0", "output": "1" }, { "input": "10\n0 10 10 0 0 3 3 0 0 0", "output": "3" }, { "input": "10\n0 9 9 9 9 0 8 8 8 8", "output": "3" }, { "input": "15\n0 0 6 6 0 0 0 0 4 0 0 0 9 0 0", "output": "4" }, { "input": "21\n0 11 11 11 11 0 7 0 12 12 12 12 12 0 19 19 19 0 1 1 1", "output": "11" }, { "input": "24\n0 0 6 6 6 0 22 22 0 23 23 0 19 19 19 19 0 0 17 0 0 3 3 3", "output": "14" }, { "input": "15\n1 1 1 1 5 5 5 4 4 4 3 3 3 2 7", "output": "0" }, { "input": "1\n1", "output": "0" }, { "input": "21\n11 0 0 0 0 7 0 12 0 0 0 0 0 19 0 0 0 1 0 0 0", "output": "4" }, { "input": "24\n6 6 0 0 0 22 0 0 23 0 0 19 0 0 0 0 17 17 0 3 3 0 0 0", "output": "7" }, { "input": "6\n4 4 2 6 6 6", "output": "0" } ]
2,000
13,209,600
0
16,327
362
Petya and Pipes
[ "flows", "graphs", "shortest paths" ]
null
null
A little boy Petya dreams of growing up and becoming the Head Berland Plumber. He is thinking of the problems he will have to solve in the future. Unfortunately, Petya is too inexperienced, so you are about to solve one of such problems for Petya, the one he's the most interested in. The Berland capital has *n* water tanks numbered from 1 to *n*. These tanks are connected by unidirectional pipes in some manner. Any pair of water tanks is connected by at most one pipe in each direction. Each pipe has a strictly positive integer width. Width determines the number of liters of water per a unit of time this pipe can transport. The water goes to the city from the main water tank (its number is 1). The water must go through some pipe path and get to the sewer tank with cleaning system (its number is *n*). Petya wants to increase the width of some subset of pipes by at most *k* units in total so that the width of each pipe remains integer. Help him determine the maximum amount of water that can be transmitted per a unit of time from the main tank to the sewer tank after such operation is completed.
The first line contains two space-separated integers *n* and *k* (2<=≀<=*n*<=≀<=50, 0<=≀<=*k*<=≀<=1000). Then follow *n* lines, each line contains *n* integers separated by single spaces. The *i*<=+<=1-th row and *j*-th column contain number *c**ij* β€” the width of the pipe that goes from tank *i* to tank *j* (0<=≀<=*c**ij*<=≀<=106,<=*c**ii*<==<=0). If *c**ij*<==<=0, then there is no pipe from tank *i* to tank *j*.
Print a single integer β€” the maximum amount of water that can be transmitted from the main tank to the sewer tank per a unit of time.
[ "5 7\n0 1 0 2 0\n0 0 4 10 0\n0 0 0 0 5\n0 0 0 0 10\n0 0 0 0 0\n", "5 10\n0 1 0 0 0\n0 0 2 0 0\n0 0 0 3 0\n0 0 0 0 4\n100 0 0 0 0\n" ]
[ "10\n", "5\n" ]
In the first test Petya can increase width of the pipe that goes from the 1st to the 2nd water tank by 7 units. In the second test Petya can increase width of the pipe that goes from the 1st to the 2nd water tank by 4 units, from the 2nd to the 3rd water tank by 3 units, from the 3rd to the 4th water tank by 2 units and from the 4th to 5th water tank by 1 unit.
[]
31
0
0
16,358
883
Orientation of Edges
[ "dfs and similar", "graphs" ]
null
null
Vasya has a graph containing both directed (oriented) and undirected (non-oriented) edges. There can be multiple edges between a pair of vertices. Vasya has picked a vertex *s* from the graph. Now Vasya wants to create two separate plans: 1. to orient each undirected edge in one of two possible directions to maximize number of vertices reachable from vertex *s*; 1. to orient each undirected edge in one of two possible directions to minimize number of vertices reachable from vertex *s*. In each of two plans each undirected edge must become directed. For an edge chosen directions can differ in two plans. Help Vasya find the plans.
The first line contains three integers *n*, *m* and *s* (2<=≀<=*n*<=≀<=3Β·105, 1<=≀<=*m*<=≀<=3Β·105, 1<=≀<=*s*<=≀<=*n*) β€” number of vertices and edges in the graph, and the vertex Vasya has picked. The following *m* lines contain information about the graph edges. Each line contains three integers *t**i*, *u**i* and *v**i* (1<=≀<=*t**i*<=≀<=2, 1<=≀<=*u**i*,<=*v**i*<=≀<=*n*, *u**i*<=β‰ <=*v**i*) β€” edge type and vertices connected by the edge. If *t**i*<==<=1 then the edge is directed and goes from the vertex *u**i* to the vertex *v**i*. If *t**i*<==<=2 then the edge is undirected and it connects the vertices *u**i* and *v**i*. It is guaranteed that there is at least one undirected edge in the graph.
The first two lines should describe the plan which maximizes the number of reachable vertices. The lines three and four should describe the plan which minimizes the number of reachable vertices. A description of each plan should start with a line containing the number of reachable vertices. The second line of a plan should consist of *f* symbols '+' and '-', where *f* is the number of undirected edges in the initial graph. Print '+' as the *j*-th symbol of the string if the *j*-th undirected edge (*u*,<=*v*) from the input should be oriented from *u* to *v*. Print '-' to signify the opposite direction (from *v* to *u*). Consider undirected edges to be numbered in the same order they are given in the input. If there are multiple solutions, print any of them.
[ "2 2 1\n1 1 2\n2 2 1\n", "6 6 3\n2 2 6\n1 4 5\n2 3 4\n1 4 1\n1 3 1\n2 2 3\n" ]
[ "2\n-\n2\n+\n", "6\n++-\n2\n+-+\n" ]
none
[ { "input": "2 2 1\n1 1 2\n2 2 1", "output": "2\n-\n2\n+" }, { "input": "6 6 3\n2 2 6\n1 4 5\n2 3 4\n1 4 1\n1 3 1\n2 2 3", "output": "6\n++-\n2\n+-+" }, { "input": "5 5 5\n2 5 3\n1 2 3\n1 4 5\n2 5 2\n1 2 1", "output": "4\n++\n1\n--" }, { "input": "13 18 9\n2 3 10\n1 12 10\n1 11 4\n2 2 8\n1 5 1\n1 7 12\n1 5 13\n1 9 7\n1 10 11\n2 3 12\n1 9 2\n1 3 9\n1 8 12\n2 11 3\n1 3 1\n1 8 4\n2 9 11\n1 12 13", "output": "11\n++-++\n8\n+-+-+" }, { "input": "5 10 2\n2 2 4\n1 1 2\n2 2 3\n1 3 1\n1 4 1\n1 5 1\n1 3 4\n2 5 4\n1 5 2\n2 5 3", "output": "5\n++--\n1\n--++" }, { "input": "5 5 1\n2 5 3\n2 2 5\n1 2 1\n2 4 2\n1 1 5", "output": "5\n+--\n2\n-++" }, { "input": "5 10 3\n2 5 1\n2 1 3\n2 3 5\n2 1 4\n2 5 4\n2 2 5\n2 3 2\n2 2 1\n2 4 3\n2 4 2", "output": "5\n--+---+---\n1\n++-+++-+++" }, { "input": "10 10 9\n2 1 6\n2 7 8\n1 4 1\n2 5 10\n1 5 2\n1 6 7\n1 5 1\n2 9 8\n2 5 3\n2 3 8", "output": "9\n+-++--\n1\n+++-++" }, { "input": "10 20 5\n2 3 8\n2 10 2\n1 8 2\n1 7 3\n1 1 8\n1 8 5\n1 2 7\n1 3 9\n1 6 1\n2 10 8\n1 4 5\n1 6 8\n2 3 4\n1 6 5\n1 2 4\n1 2 3\n1 5 9\n2 4 9\n1 4 7\n1 6 2", "output": "8\n+----\n2\n+++++" }, { "input": "10 10 6\n2 1 4\n1 7 8\n1 6 4\n1 7 2\n1 6 2\n1 1 3\n1 9 7\n1 3 10\n1 9 6\n1 9 1", "output": "6\n-\n3\n+" }, { "input": "10 20 10\n2 7 3\n1 7 9\n1 3 6\n2 8 3\n2 9 2\n1 5 3\n2 9 8\n2 9 1\n1 5 9\n1 10 2\n1 6 7\n2 3 2\n2 8 1\n1 6 1\n2 4 6\n2 10 9\n2 5 7\n2 10 1\n1 2 7\n2 3 4", "output": "10\n---+----+-++\n4\n-++--+++++-+" }, { "input": "14 19 14\n2 5 7\n1 4 1\n2 9 8\n1 7 3\n2 14 2\n2 2 8\n2 6 7\n2 14 7\n1 7 8\n2 10 8\n2 11 10\n1 11 7\n2 3 13\n1 5 4\n1 14 8\n2 3 1\n2 6 1\n2 6 10\n2 8 1", "output": "13\n--+--+--+---+\n2\n++-++-++++++-" }, { "input": "300000 1 5345\n2 5345 23423", "output": "2\n+\n1\n-" }, { "input": "2 5 1\n1 1 2\n1 1 2\n1 1 2\n2 1 2\n1 1 2", "output": "2\n+\n2\n+" }, { "input": "2 5 2\n1 1 2\n1 1 2\n1 1 2\n2 1 2\n1 1 2", "output": "2\n-\n1\n+" }, { "input": "2 5 2\n2 1 2\n2 1 2\n2 1 2\n2 1 2\n2 1 2", "output": "2\n-----\n1\n+++++" }, { "input": "2 5 2\n1 1 2\n1 1 2\n1 2 1\n2 1 2\n1 2 1", "output": "2\n-\n2\n+" }, { "input": "2 5 1\n1 1 2\n1 1 2\n1 2 1\n2 1 2\n1 2 1", "output": "2\n+\n2\n+" }, { "input": "2 2 1\n2 1 2\n2 2 1", "output": "2\n+-\n1\n-+" }, { "input": "2 5 1\n2 1 2\n2 1 2\n2 1 2\n2 1 2\n2 1 2", "output": "2\n+++++\n1\n-----" } ]
530
20,480,000
-1
16,378
656
Ace It!
[ "*special" ]
null
null
The only line of the input is a string of 7 characters. The first character is letter A, followed by 6 digits. The input is guaranteed to be valid (for certain definition of "valid"). Output a single integer.
The only line of the input is a string of 7 characters. The first character is letter A, followed by 6 digits. The input is guaranteed to be valid (for certain definition of "valid").
Output a single integer.
[ "A221033\n", "A223635\n", "A232726\n" ]
[ "21\n", "22\n", "23\n" ]
none
[ { "input": "A221033", "output": "21" }, { "input": "A223635", "output": "22" }, { "input": "A232726", "output": "23" }, { "input": "A102210", "output": "25" }, { "input": "A231010", "output": "26" }, { "input": "A222222", "output": "13" }, { "input": "A555555", "output": "31" }, { "input": "A102222", "output": "19" }, { "input": "A234567", "output": "28" }, { "input": "A987654", "output": "40" }, { "input": "A101010", "output": "31" }, { "input": "A246810", "output": "31" }, { "input": "A210210", "output": "25" }, { "input": "A458922", "output": "31" }, { "input": "A999999", "output": "55" }, { "input": "A888888", "output": "49" }, { "input": "A232232", "output": "15" }, { "input": "A222210", "output": "19" }, { "input": "A710210", "output": "30" }, { "input": "A342987", "output": "34" }, { "input": "A987623", "output": "36" }, { "input": "A109109", "output": "39" }, { "input": "A910109", "output": "39" }, { "input": "A292992", "output": "34" }, { "input": "A388338", "output": "34" }, { "input": "A764598", "output": "40" }, { "input": "A332567", "output": "27" }, { "input": "A108888", "output": "43" }, { "input": "A910224", "output": "28" }, { "input": "A321046", "output": "26" }, { "input": "A767653", "output": "35" }, { "input": "A101099", "output": "39" }, { "input": "A638495", "output": "36" } ]
46
0
3
16,408
786
Berzerk
[ "dfs and similar", "dp", "games" ]
null
null
Rick and Morty are playing their own version of Berzerk (which has nothing in common with the famous Berzerk game). This game needs a huge space, so they play it with a computer. In this game there are *n* objects numbered from 1 to *n* arranged in a circle (in clockwise order). Object number 1 is a black hole and the others are planets. There's a monster in one of the planet. Rick and Morty don't know on which one yet, only that he's not initially in the black hole, but Unity will inform them before the game starts. But for now, they want to be prepared for every possible scenario. Each one of them has a set of numbers between 1 and *n*<=-<=1 (inclusive). Rick's set is *s*1 with *k*1 elements and Morty's is *s*2 with *k*2 elements. One of them goes first and the player changes alternatively. In each player's turn, he should choose an arbitrary number like *x* from his set and the monster will move to his *x*-th next object from its current position (clockwise). If after his move the monster gets to the black hole he wins. Your task is that for each of monster's initial positions and who plays first determine if the starter wins, loses, or the game will stuck in an infinite loop. In case when player can lose or make game infinity, it more profitable to choose infinity game.
The first line of input contains a single integer *n* (2<=≀<=*n*<=≀<=7000) β€” number of objects in game. The second line contains integer *k*1 followed by *k*1 distinct integers *s*1,<=1,<=*s*1,<=2,<=...,<=*s*1,<=*k*1 β€” Rick's set. The third line contains integer *k*2 followed by *k*2 distinct integers *s*2,<=1,<=*s*2,<=2,<=...,<=*s*2,<=*k*2 β€” Morty's set 1<=≀<=*k**i*<=≀<=*n*<=-<=1 and 1<=≀<=*s**i*,<=1,<=*s**i*,<=2,<=...,<=*s**i*,<=*k**i*<=≀<=*n*<=-<=1 for 1<=≀<=*i*<=≀<=2.
In the first line print *n*<=-<=1 words separated by spaces where *i*-th word is "Win" (without quotations) if in the scenario that Rick plays first and monster is initially in object number *i*<=+<=1 he wins, "Lose" if he loses and "Loop" if the game will never end. Similarly, in the second line print *n*<=-<=1 words separated by spaces where *i*-th word is "Win" (without quotations) if in the scenario that Morty plays first and monster is initially in object number *i*<=+<=1 he wins, "Lose" if he loses and "Loop" if the game will never end.
[ "5\n2 3 2\n3 1 2 3\n", "8\n4 6 2 3 4\n2 3 6\n" ]
[ "Lose Win Win Loop\nLoop Win Win Win\n", "Win Win Win Win Win Win Win\nLose Win Lose Lose Win Lose Lose\n" ]
none
[ { "input": "5\n2 3 2\n3 1 2 3", "output": "Lose Win Win Loop\nLoop Win Win Win" }, { "input": "8\n4 6 2 3 4\n2 3 6", "output": "Win Win Win Win Win Win Win\nLose Win Lose Lose Win Lose Lose" }, { "input": "10\n3 4 7 5\n2 8 5", "output": "Win Win Win Win Win Win Win Loop Win\nLose Win Loop Lose Win Lose Lose Lose Lose" }, { "input": "17\n1 10\n1 12", "output": "Win Win Win Win Win Win Win Win Win Win Win Lose Win Win Win Win\nLose Lose Lose Lose Win Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose" }, { "input": "23\n1 20\n3 9 2 12", "output": "Lose Lose Win Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose\nWin Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win" }, { "input": "85\n12 76 7 75 51 43 41 66 13 59 48 81 73\n3 65 60 25", "output": "Loop Loop Loop Win Loop Loop Loop Loop Win Win Loop Win Loop Loop Loop Loop Loop Loop Win Loop Loop Loop Loop Loop Loop Win Loop Loop Loop Loop Loop Loop Loop Win Loop Loop Win Loop Loop Loop Loop Win Loop Win Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Win Loop Loop Loop Loop Loop Win Loop Loop Loop Loop Loop Loop\nLoop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Win Loo..." }, { "input": "100\n84 80 73 28 76 21 44 97 63 59 6 77 41 2 8 71 57 19 33 46 92 5 61 88 53 68 94 56 14 35 4 47 17 79 84 10 67 58 45 38 13 12 87 3 91 30 15 11 24 55 62 39 83 43 89 1 81 75 50 86 72 18 52 78 7 29 64 42 70 49 37 25 66 74 95 36 85 48 99 60 51 98 27 40 93\n47 52 76 9 4 25 8 63 29 74 97 61 93 35 49 62 5 10 57 73 42 3 19 23 71 70 43 67 48 2 34 31 41 90 18 6 40 83 98 72 14 51 38 46 21 99 65 37", "output": "Win Win Win Loop Win Win Win Win Win Loop Win Win Win Win Win Win Win Loop Win Win Win Win Win Win Win Win Win Win Win Win Loop Win Win Win Loop Win Win Win Win Win Win Win Win Win Win Loop Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Loop Win Loop Loop Win Win Win Win Loop Win Win Loop Loop Win Loop Win Win Win Loop Win Win Win Win Win Win Loop Win Win Win Win Win Win Win Win\nWin Win Win Loop Loop Loop Win Loop Loop Win Loop Loop Loop Loop Loop Loop Win Loop Loop Loop Loop ..." }, { "input": "100\n66 70 54 10 72 81 84 56 15 27 19 43 55 49 44 52 33 63 40 95 17 58 2 51 39 22 18 82 1 16 99 32 29 24 94 9 98 5 37 47 14 42 73 41 31 79 64 12 6 53 26 68 67 89 13 90 4 21 93 46 74 75 88 66 57 23 7\n18 8 47 76 39 34 52 62 5 36 19 22 80 32 71 55 7 37 57", "output": "Win Win Loop Loop Win Win Win Loop Loop Win Win Win Loop Loop Loop Win Loop Win Win Loop Win Loop Loop Loop Win Win Win Win Loop Win Loop Win Win Win Loop Win Win Loop Loop Loop Loop Win Win Win Win Win Win Win Win Loop Win Loop Win Win Loop Win Win Win Win Win Win Loop Win Loop Loop Loop Win Win Win Loop Win Loop Win Win Loop Win Win Win Win Loop Win Win Win Win Win Win Win Win Loop Win Win Loop Win Win Win Win Loop Win Win\nLoop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop L..." }, { "input": "300\n1 179\n2 293 180", "output": "Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose L..." }, { "input": "1000\n14 77 649 670 988 469 453 445 885 101 58 728 474 488 230\n8 83 453 371 86 834 277 847 958", "output": "Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Win Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Lo..." }, { "input": "2\n1 1\n1 1", "output": "Win\nWin" }, { "input": "2\n1 1\n1 1", "output": "Win\nWin" }, { "input": "3\n1 1\n1 2", "output": "Loop Win\nWin Loop" }, { "input": "20\n1 1\n1 11", "output": "Loop Loop Win Lose Loop Loop Win Lose Loop Loop Win Lose Loop Loop Win Lose Loop Loop Win\nWin Loop Loop Lose Win Loop Loop Lose Win Loop Loop Lose Win Loop Loop Lose Win Loop Loop" }, { "input": "309\n30 197 38 142 159 163 169 263 70 151 288 264 41 285 225 216 306 128 242 221 94 39 43 292 54 157 78 272 257 97 57\n3 97 172 165", "output": "Loop Loop Win Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Win Loop Loop Loop Win Loop Loop Win Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Win Loop Loop Loop Loop Loop Loop Loop Win Win Loop Loop Loop Loop Loop Win Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Win Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Win Loop Loop Loop Win Loop Loop Loop Loop Win Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loo..." }, { "input": "1000\n1 312\n1 171", "output": "Lose Lose Lose Lose Lose Lose Win Lose Lose Lose Lose Lose Lose Win Lose Lose Lose Lose Lose Lose Win Lose Lose Lose Lose Lose Lose Win Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Win Lose Lose Lose Lose Lose Lose Win Lose Lose Lose Lose Lose Lose Win Lose Lose Lose Lose Lose Lose Win Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Win Lose Lose Lose Lose Lose Lose Win Lose Lose Lose Lose Lose Lose Win Lose Lose Lose Lose Lose Lose Win Lose Lose Lose Lose Lose Lose Lose Lose Los..." }, { "input": "1000\n1 481\n2 468 9", "output": "Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose L..." }, { "input": "1000\n3 469 637 369\n2 801 339", "output": "Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop L..." }, { "input": "4096\n6 3736 3640 553 2608 1219 1640\n4 112 2233 3551 2248", "output": "Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop L..." }, { "input": "6341\n9 6045 2567 3242 5083 5429 1002 4547 1838 4829\n5 5533 3084 6323 4015 2889", "output": "Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop L..." }, { "input": "7000\n1 5244\n1 2980", "output": "Loop Loop Loop Win Loop Loop Loop Lose Loop Loop Loop Win Loop Loop Loop Lose Loop Loop Loop Win Loop Loop Loop Lose Loop Loop Loop Win Loop Loop Loop Lose Loop Loop Loop Win Loop Loop Loop Lose Loop Loop Loop Win Loop Loop Loop Lose Loop Loop Loop Win Loop Loop Loop Lose Loop Loop Loop Win Loop Loop Loop Lose Loop Loop Loop Win Loop Loop Loop Lose Loop Loop Loop Win Loop Loop Loop Lose Loop Loop Loop Win Loop Loop Loop Lose Loop Loop Loop Win Loop Loop Loop Lose Loop Loop Loop Win Loop Loop Loop Lose Loop..." }, { "input": "7000\n1 6694\n1 2973", "output": "Loop Loop Loop Loop Win Loop Lose Loop Loop Loop Loop Win Loop Lose Loop Loop Loop Loop Win Loop Lose Loop Loop Loop Loop Win Loop Lose Loop Loop Loop Loop Win Loop Lose Loop Loop Loop Loop Win Loop Lose Loop Loop Loop Loop Win Loop Lose Loop Loop Loop Loop Win Loop Lose Loop Loop Loop Loop Win Loop Lose Loop Loop Loop Loop Win Loop Lose Loop Loop Loop Loop Win Loop Lose Loop Loop Loop Loop Win Loop Lose Loop Loop Loop Loop Win Loop Lose Loop Loop Loop Loop Win Loop Lose Loop Loop Loop Loop Win Loop Lose L..." }, { "input": "7000\n1 3041\n1 6128", "output": "Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Win Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Lose Win Win Win Win Win Win Win W..." }, { "input": "7000\n5 5080 4890 1201 4903 1360\n5 2415 6678 5200 2282 4648", "output": "Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop L..." }, { "input": "7000\n3 6965 1271 5818\n3 6331 5681 6636", "output": "Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Win Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Lo..." }, { "input": "7000\n3 2706 2040 6698\n10 4118 846 1075 1624 2342 766 6441 2361 4662 1574", "output": "Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop L..." }, { "input": "7000\n12 3489 6630 4582 292 5489 1456 5101 6920 632 2963 5136 5886\n11 434 5878 3806 656 3047 6614 1073 5932 6537 704 5253", "output": "Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Win Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Lo..." }, { "input": "7000\n7 419 1631 1925 3861 6940 379 493\n29 5389 5925 2923 4696 972 6125 3779 6044 5477 1305 6488 5059 5515 3238 3863 248 6947 4023 6168 1915 6607 2991 2220 2023 200 4457 6398 1017 447", "output": "Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Win Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Lo..." }, { "input": "6999\n2 3992 782\n2 4903 6815", "output": "Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop L..." } ]
15
0
-1
16,431
35
Animals
[ "dp", "greedy" ]
D. Animals
2
64
Once upon a time DravDe, an outstanding person famous for his professional achievements (as you must remember, he works in a warehouse storing Ogudar-Olok, a magical but non-alcoholic drink) came home after a hard day. That day he had to drink 9875 boxes of the drink and, having come home, he went to bed at once. DravDe dreamt about managing a successful farm. He dreamt that every day one animal came to him and asked him to let it settle there. However, DravDe, being unimaginably kind, could send the animal away and it went, rejected. There were exactly *n* days in DravDe’s dream and the animal that came on the *i*-th day, ate exactly *c**i* tons of food daily starting from day *i*. But if one day the animal could not get the food it needed, it got really sad. At the very beginning of the dream there were exactly *X* tons of food on the farm. DravDe woke up terrified... When he retold the dream to you, he couldn’t remember how many animals were on the farm by the end of the *n*-th day any more, but he did remember that nobody got sad (as it was a happy farm) and that there was the maximum possible amount of the animals. That’s the number he wants you to find out. It should be noticed that the animals arrived in the morning and DravDe only started to feed them in the afternoon, so that if an animal willing to join them is rejected, it can’t eat any farm food. But if the animal does join the farm, it eats daily from that day to the *n*-th.
The first input line contains integers *n* and *X* (1<=≀<=*n*<=≀<=100,<=1<=≀<=*X*<=≀<=104) β€” amount of days in DravDe’s dream and the total amount of food (in tons) that was there initially. The second line contains integers *c**i* (1<=≀<=*c**i*<=≀<=300). Numbers in the second line are divided by a space.
Output the only number β€” the maximum possible amount of animals on the farm by the end of the *n*-th day given that the food was enough for everybody.
[ "3 4\n1 1 1\n", "3 6\n1 1 1\n" ]
[ "2\n", "3\n" ]
Note to the first example: DravDe leaves the second and the third animal on the farm. The second animal will eat one ton of food on the second day and one ton on the third day. The third animal will eat one ton of food on the third day.
[ { "input": "3 4\n1 1 1", "output": "2" }, { "input": "3 6\n1 1 1", "output": "3" }, { "input": "1 12\n1", "output": "1" }, { "input": "3 100\n1 1 1", "output": "3" }, { "input": "5 75\n1 1 1 1 1", "output": "5" }, { "input": "7 115\n1 1 1 1 1 1 1", "output": "7" }, { "input": "10 1055\n7 1 1 2 8 7 8 2 5 8", "output": "10" }, { "input": "7 3623\n20 14 24 4 14 14 24", "output": "7" }, { "input": "10 3234\n24 2 28 18 6 15 31 2 28 16", "output": "10" }, { "input": "15 402\n3 3 3 3 2 2 3 3 3 3 3 3 2 2 1", "output": "15" }, { "input": "25 5523\n24 29 6 35 11 7 24 10 17 43 2 25 15 36 31 8 22 40 23 23 7 24 5 16 24", "output": "23" }, { "input": "50 473\n3 2 2 1 1 3 3 2 1 3 2 3 1 1 3 1 3 2 2 1 2 3 1 3 2 2 1 1 1 3 1 3 4 4 1 3 4 4 4 1 1 3 1 3 1 2 2 1 4 2", "output": "22" }, { "input": "100 4923\n21 5 18 2 9 4 22 17 8 25 20 11 17 25 18 14 25 12 21 13 22 4 6 21 1 12 12 7 20 16 12 17 28 4 17 14 6 2 5 20 20 14 6 30 4 24 18 24 7 18 24 23 33 16 16 24 21 22 11 18 34 19 32 21 1 34 8 9 9 13 4 7 18 8 33 24 9 2 24 35 8 35 35 38 11 23 14 42 43 44 7 43 37 21 8 17 3 9 33 43", "output": "29" }, { "input": "25 101\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", "output": "13" }, { "input": "45 9343\n36 16 13 20 48 5 45 48 54 16 42 40 66 31 18 59 24 66 72 32 65 54 55 72 1 1 36 13 59 16 42 2 72 70 7 40 85 65 40 20 68 89 37 16 46", "output": "25" }, { "input": "75 8333\n27 41 40 42 1 23 25 25 9 12 36 20 19 13 8 49 16 11 17 7 19 25 46 6 33 27 48 37 46 44 5 5 33 8 49 20 49 51 42 2 43 26 4 60 50 25 41 60 53 25 49 28 45 66 26 39 60 58 53 64 44 50 18 29 67 10 63 44 55 26 20 60 35 43 65", "output": "26" }, { "input": "100 115\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 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": "14" }, { "input": "100 1150\n5 3 1 4 2 4 1 1 3 2 1 5 6 3 1 6 3 4 1 3 3 5 2 3 1 5 3 1 3 5 3 1 6 2 3 2 3 2 3 6 3 5 4 6 4 5 3 6 1 2 3 2 1 2 5 1 6 7 4 8 4 4 6 1 6 5 6 7 8 2 5 6 6 2 1 1 9 1 5 6 7 7 2 9 5 1 7 1 2 2 7 6 4 2 1 8 11 8 6 6", "output": "28" }, { "input": "100 3454\n9 3 3 15 14 8 8 14 13 2 16 4 16 4 13 8 14 1 15 7 19 12 9 19 17 17 18 16 10 1 20 8 16 5 12 18 6 5 5 13 12 15 18 4 20 16 3 18 13 22 5 1 23 20 10 21 20 8 9 5 7 23 24 20 1 25 7 19 1 6 14 8 23 26 18 14 11 26 12 11 8 5 10 28 22 8 5 12 28 8 7 8 22 31 31 30 28 33 24 31", "output": "27" }, { "input": "100 8777\n38 4 2 14 30 45 20 17 25 14 12 44 11 11 5 30 16 3 48 14 42 48 9 4 1 30 9 13 23 15 24 31 16 12 23 20 1 4 20 18 41 47 27 5 50 12 41 33 25 16 1 46 41 59 27 57 24 6 33 62 27 50 54 28 48 11 37 23 31 29 21 32 25 47 15 9 41 26 70 26 58 62 42 10 39 38 25 55 69 72 5 31 30 21 43 59 39 83 67 45", "output": "30" }, { "input": "100 10\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 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": "4" }, { "input": "100 100\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 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": "13" }, { "input": "100 1000\n3 2 4 5 3 4 5 3 2 5 3 3 1 1 1 3 5 1 2 2 5 3 2 4 4 1 5 1 1 3 4 4 1 4 3 5 2 1 1 6 6 2 2 6 5 1 6 4 5 2 1 2 2 5 5 2 1 5 7 4 4 1 4 4 5 3 4 4 1 6 3 2 4 5 2 6 3 6 5 5 2 4 6 3 7 1 5 4 7 2 5 5 6 3 8 5 9 9 3 3", "output": "24" }, { "input": "100 10000\n9 24 4 16 15 28 18 5 16 52 19 12 52 31 6 53 20 44 17 3 51 51 21 53 27 3 40 15 42 34 54 6 55 24 32 53 35 25 38 2 19 7 26 8 46 32 10 25 24 50 65 6 21 26 25 62 12 67 45 34 50 46 59 40 18 55 41 36 48 13 29 76 52 46 57 30 10 60 43 26 73 21 19 68 20 76 67 29 8 46 27 33 22 74 58 91 27 89 50 42", "output": "30" }, { "input": "100 9999\n31 26 2 16 41 42 44 30 28 9 15 49 19 8 34 52 19 36 30 43 53 53 43 18 38 3 56 3 4 51 6 44 41 46 43 43 14 44 37 53 3 39 25 63 22 14 40 36 40 45 44 14 54 29 56 39 42 65 59 28 34 53 16 14 31 33 28 9 42 43 41 54 27 1 60 47 79 52 72 55 1 16 56 75 81 46 50 58 32 34 73 26 19 25 2 31 18 40 91 17", "output": "29" }, { "input": "100 1234\n1 5 6 5 6 5 2 3 2 1 4 1 6 6 4 5 3 6 5 1 1 5 2 2 3 3 6 1 1 4 6 2 1 3 5 2 7 6 6 2 2 1 1 2 1 4 1 2 1 2 2 5 1 8 8 8 2 2 4 8 1 8 4 1 1 5 5 9 9 2 6 4 7 2 5 3 7 6 7 10 9 9 1 2 5 8 5 7 1 1 8 10 2 6 7 9 5 2 10 6", "output": "28" }, { "input": "100 4321\n7 2 18 4 10 1 11 12 4 22 2 10 5 19 12 3 6 16 20 22 12 2 1 3 15 2 1 13 4 14 11 1 24 12 6 23 18 20 10 7 23 15 24 16 3 15 24 14 18 22 27 18 9 9 10 21 14 21 23 5 5 25 4 23 9 17 16 30 7 14 3 25 23 21 7 19 12 8 14 29 28 21 28 24 29 32 27 10 16 8 3 8 40 3 18 28 23 24 42 40", "output": "31" }, { "input": "100 2222\n10 4 1 2 7 1 2 8 10 6 5 9 9 5 6 5 9 3 4 6 5 7 6 6 11 4 10 6 3 2 5 9 13 2 6 3 4 10 7 7 1 9 7 14 13 13 6 3 12 5 13 9 15 2 5 10 3 4 7 7 5 11 8 15 14 11 4 4 7 3 3 15 4 13 1 13 7 12 4 7 1 4 16 1 9 5 16 14 2 4 7 17 7 4 7 20 11 2 15 9", "output": "30" }, { "input": "5 54\n3 3 2 6 9", "output": "5" }, { "input": "7 102\n2 6 1 3 4 8 7", "output": "7" }, { "input": "4 43\n3 4 9 2", "output": "3" }, { "input": "6 131\n2 9 7 9 7 6", "output": "5" }, { "input": "11 362\n4 5 4 8 10 6 3 2 7 7 4", "output": "11" }, { "input": "85 1121\n6 4 1 3 2 5 1 6 1 3 3 2 1 2 3 2 1 4 1 6 1 1 6 4 5 4 1 5 1 6 2 3 6 5 3 6 7 3 4 7 7 2 1 3 1 8 2 8 7 4 5 7 4 8 6 8 2 6 4 5 5 1 3 7 3 2 4 3 1 9 9 5 9 2 9 1 10 2 10 10 2 10 8 5 8", "output": "25" }, { "input": "85 5801\n14 28 19 29 19 6 17 22 15 17 24 1 5 26 28 11 20 5 1 5 30 30 17 9 31 13 21 13 12 31 3 21 12 5 7 35 27 26 1 18 7 36 18 4 24 21 36 38 20 42 15 20 33 31 25 8 31 33 39 2 11 32 34 9 26 24 16 22 13 31 38 8 17 40 52 51 6 33 53 22 33 19 19 16 41", "output": "29" }, { "input": "95 1191\n3 6 4 3 5 1 6 1 4 4 3 6 5 2 3 6 2 4 5 5 2 5 5 5 2 1 6 2 4 2 3 1 1 5 7 1 6 4 3 6 6 1 1 5 5 4 6 5 8 1 3 1 3 6 4 6 5 4 3 4 4 7 1 3 3 2 5 7 5 5 7 3 5 8 5 9 3 1 7 9 8 9 1 2 7 3 5 3 8 7 1 7 11 9 11", "output": "27" }, { "input": "95 5201\n26 1 1 18 22 8 3 10 18 14 21 17 9 1 22 13 9 27 5 14 28 14 25 3 9 28 3 19 28 7 28 21 25 13 18 5 29 16 1 32 18 4 19 28 31 5 9 27 6 29 19 20 20 19 4 21 20 34 7 2 5 36 27 22 8 3 10 28 37 9 18 36 38 9 23 43 2 6 3 35 9 20 42 45 37 12 29 19 45 22 48 3 13 40 45", "output": "33" }, { "input": "80 8101\n17 23 11 5 11 27 22 5 31 23 24 6 34 44 22 25 10 44 10 42 42 6 3 24 31 43 10 5 27 36 36 51 27 12 45 39 15 29 30 54 14 22 25 6 33 36 16 4 12 20 54 17 2 61 2 38 33 56 34 4 16 15 60 31 41 21 58 66 46 59 2 33 20 20 37 50 61 33 69 38", "output": "30" }, { "input": "90 4411\n11 1 23 12 22 23 17 3 22 4 22 18 23 23 4 15 7 11 14 4 22 11 14 20 4 17 18 14 9 20 7 12 14 18 22 17 25 8 1 15 17 1 27 11 27 13 20 29 29 29 20 1 24 13 10 30 31 33 9 15 29 18 19 4 4 14 23 11 31 15 3 28 19 37 18 24 32 12 26 31 36 12 10 24 4 32 25 30 37 2", "output": "27" }, { "input": "100 9898\n13 16 40 32 21 21 50 18 5 35 44 18 38 31 12 42 29 30 13 51 50 36 37 48 8 56 16 36 15 39 48 37 26 18 8 15 15 2 44 28 20 29 7 36 30 62 31 50 59 37 58 26 37 23 21 31 14 12 58 55 30 9 66 64 55 23 59 54 54 29 36 72 41 36 68 42 17 16 65 71 35 72 43 6 53 79 26 51 1 16 55 36 65 72 43 20 78 86 42 52", "output": "26" } ]
92
0
-1
16,451
59
Fortune Telling
[ "implementation", "number theory" ]
B. Fortune Telling
2
256
Marina loves Sasha. But she keeps wondering whether Sasha loves her. Of course, the best way to know it is fortune telling. There are many ways of telling fortune, but Marina has picked the easiest one. She takes in her hand one or several camomiles and tears off the petals one by one. After each petal she pronounces alternatively "Loves" and "Doesn't love", at that Marina always starts with "Loves". There are *n* camomiles growing in the field, possessing the numbers of petals equal to *a*1,<=*a*2,<=... *a**n*. Marina wants to pick a bouquet with the maximal possible total number of petals so that the result would still be "Loves". Help her do that; find the maximal number of petals possible in the bouquet.
The first line contains an integer *n* (1<=≀<=*n*<=≀<=100), which is the number of flowers growing in the field. The second line contains *n* integers *a**i* (1<=≀<=*a**i*<=≀<=100) which represent the number of petals on a given *i*-th camomile.
Print a single number which is the maximal number of petals in the bouquet, the fortune telling on which would result in "Loves". If there are no such bouquet, print 0 instead. The bouquet may consist of a single flower.
[ "1\n1\n", "1\n2\n", "3\n5 6 7\n" ]
[ "1\n", "0\n", "13\n" ]
none
[ { "input": "1\n1", "output": "1" }, { "input": "1\n2", "output": "0" }, { "input": "3\n5 6 7", "output": "13" }, { "input": "2\n5 7", "output": "7" }, { "input": "3\n1 2 3", "output": "5" }, { "input": "4\n4 3 1 2", "output": "9" }, { "input": "10\n90 72 76 60 22 87 5 67 17 65", "output": "561" }, { "input": "10\n18 42 20 68 88 10 87 37 55 51", "output": "439" }, { "input": "100\n25 43 35 79 53 13 91 91 45 65 83 57 9 41 39 85 45 71 51 61 59 31 13 63 39 25 21 79 39 91 67 21 61 97 75 93 83 29 79 59 97 11 37 63 51 39 55 91 23 21 17 47 23 35 75 49 5 69 99 5 7 41 17 25 89 15 79 21 63 53 81 43 91 59 91 69 99 85 15 91 51 49 37 65 7 89 81 21 93 61 63 97 93 45 17 13 69 57 25 75", "output": "5355" }, { "input": "100\n22 93 43 39 5 39 55 89 97 7 35 63 75 85 97 75 35 91 5 29 97 69 23 97 95 59 23 81 87 67 85 95 33 41 57 9 39 25 55 9 87 57 69 31 23 27 13 81 51 11 61 35 69 59 51 33 73 29 77 75 9 15 41 93 65 89 69 37 51 11 57 21 97 95 13 67 23 69 3 29 83 97 7 49 13 51 65 33 99 9 27 99 55 47 37 11 37 13 91 79", "output": "5193" }, { "input": "100\n82 6 42 34 4 32 12 50 16 58 48 92 44 94 36 94 96 50 68 38 78 10 18 88 38 66 60 72 76 24 60 62 86 8 16 14 74 54 38 100 88 28 44 78 90 42 20 24 90 21 81 29 53 95 75 5 57 31 37 69 55 65 1 67 61 71 17 99 15 15 67 77 19 95 79 87 29 97 13 95 61 91 45 77 91 79 55 81 37 81 15 89 67 61 19 25 97 53 7 95", "output": "5445" }, { "input": "100\n64 16 64 48 12 88 18 38 12 14 90 82 68 40 90 78 66 50 56 50 78 12 18 100 14 92 70 96 90 26 60 94 88 26 70 100 34 86 8 38 72 24 32 80 56 28 32 48 92 52 71 43 95 23 71 89 51 93 61 39 75 3 19 79 71 11 33 21 61 29 13 55 61 23 17 45 93 11 15 29 45 91 43 9 41 37 99 67 25 33 83 55 59 85 59 41 67 67 37 17", "output": "5217" }, { "input": "100\n12 84 30 14 36 18 4 82 26 22 10 88 96 84 50 100 88 40 70 94 94 58 16 50 80 38 94 100 34 20 22 54 34 58 92 18 6 8 22 92 82 28 42 54 96 8 18 40 64 90 58 63 97 89 17 11 21 55 71 91 47 93 55 95 39 81 51 7 77 13 25 65 51 47 47 49 19 35 67 5 7 65 65 65 79 33 71 15 17 91 13 43 81 31 7 17 17 93 9 25", "output": "4945" }, { "input": "100\n64 58 12 86 50 16 48 32 30 2 30 36 4 6 96 84 58 94 14 50 28 100 32 84 54 76 26 100 42 100 76 32 86 72 84 16 36 10 26 82 54 64 78 66 62 30 4 80 28 16 44 82 8 2 24 56 28 98 20 92 30 10 28 32 44 18 58 2 12 64 14 4 12 84 16 14 8 78 94 98 34 16 28 76 82 50 40 78 28 16 60 58 64 68 56 46 24 72 72 69", "output": "4725" }, { "input": "100\n92 46 50 24 68 60 70 30 52 22 18 74 68 98 20 82 4 46 26 68 100 78 84 58 74 98 38 88 68 86 64 80 82 100 20 22 98 98 52 6 94 10 48 68 2 18 38 22 22 82 44 20 66 72 36 58 64 6 36 60 4 96 76 64 12 90 10 58 64 60 74 28 90 26 24 60 40 58 2 16 76 48 58 36 82 60 24 44 4 78 28 38 8 12 40 16 38 6 66 24", "output": "0" }, { "input": "99\n49 37 55 57 97 79 53 25 89 13 15 77 91 51 73 39 29 83 13 43 79 15 89 97 67 25 23 77 71 41 15 83 39 13 43 1 51 49 1 11 95 57 65 7 79 43 51 33 33 71 97 73 3 65 73 55 21 7 37 75 39 9 21 47 31 97 33 11 61 79 67 63 81 21 77 57 73 19 21 47 55 11 37 31 71 5 15 73 23 93 83 25 37 17 23 75 77 97 93", "output": "4893" }, { "input": "99\n26 77 13 25 33 67 89 57 49 35 7 15 17 5 1 73 53 19 35 83 31 49 51 1 25 23 3 63 19 9 53 25 65 43 27 71 3 95 77 89 95 85 67 27 93 3 11 45 99 31 21 35 83 31 43 93 75 93 3 51 11 29 73 3 33 63 57 71 43 15 69 55 53 7 13 73 7 5 57 61 97 53 13 39 79 19 35 71 27 97 19 57 39 51 89 63 21 47 53", "output": "4451" }, { "input": "99\n50 22 22 94 100 18 74 2 98 16 66 54 14 90 38 26 12 30 32 66 26 54 44 36 52 30 54 56 36 16 16 34 22 40 64 94 18 2 40 42 76 56 24 18 36 64 14 96 50 69 53 9 27 61 81 37 29 1 21 79 17 81 41 23 89 29 47 65 17 11 95 21 19 71 1 73 45 25 19 83 93 27 21 31 25 3 91 89 59 35 35 7 9 1 97 55 25 65 93", "output": "4333" }, { "input": "99\n86 16 38 20 68 60 84 16 28 88 60 48 80 28 4 92 70 60 46 46 20 34 12 100 76 2 40 10 8 86 6 80 50 66 12 34 14 28 26 70 46 64 34 96 10 90 98 96 56 88 49 73 69 93 1 93 23 65 67 45 21 29 5 9 63 31 87 13 97 99 63 57 49 17 49 49 7 37 7 15 53 1 59 53 61 83 91 97 3 71 65 25 13 87 99 15 9 5 87", "output": "4849" }, { "input": "99\n82 36 50 30 80 2 48 48 92 10 70 46 72 46 4 60 60 40 4 78 98 8 88 82 70 44 76 50 64 48 82 74 50 100 98 8 60 72 26 50 94 54 58 20 10 66 20 72 26 20 22 29 21 17 31 69 75 91 77 93 81 71 93 91 65 37 41 69 19 15 67 79 39 9 53 69 73 93 85 45 51 5 73 87 49 95 35 71 1 3 65 81 61 59 73 89 79 73 25", "output": "5439" }, { "input": "99\n28 50 100 90 56 60 54 16 54 62 48 6 2 14 40 48 28 48 58 68 90 74 82 2 98 4 74 64 34 98 94 24 44 74 50 18 40 100 80 96 10 42 66 46 26 26 84 34 68 84 74 48 8 90 2 36 40 32 18 76 90 64 38 92 86 84 56 84 74 90 4 2 50 34 18 28 30 2 18 80 52 34 10 86 96 76 30 64 88 76 74 4 50 22 20 96 90 12 42", "output": "0" }, { "input": "99\n58 100 2 54 80 84 74 46 92 74 90 4 92 92 18 88 100 80 42 34 80 62 92 94 8 48 98 44 4 74 48 22 26 90 98 44 14 54 80 24 60 50 58 62 94 18 20 4 56 58 52 80 88 82 10 40 36 46 14 22 54 10 36 10 20 76 48 98 2 68 26 96 16 92 50 78 28 8 80 84 82 26 62 20 60 84 2 80 70 98 50 30 64 6 92 58 16 88 27", "output": "5353" }, { "input": "42\n26 24 14 18 96 30 56 72 10 32 94 62 68 11 75 45 39 49 37 29 9 1 63 47 81 67 79 81 93 31 69 61 73 67 81 7 37 87 61 17 21 65", "output": "2085" }, { "input": "42\n62 46 24 100 68 48 6 4 16 60 48 52 26 56 52 20 100 14 72 80 72 52 76 15 17 23 1 91 71 39 93 5 93 47 59 77 37 17 33 51 39 85", "output": "2047" }, { "input": "50\n88 68 16 44 72 6 2 50 2 36 26 98 16 30 6 10 88 76 50 90 44 28 84 28 100 57 59 91 51 37 19 79 69 79 95 81 75 89 19 87 31 49 77 35 79 7 85 41 83 91", "output": "2723" }, { "input": "1\n31", "output": "31" }, { "input": "1\n44", "output": "0" }, { "input": "2\n21 63", "output": "63" }, { "input": "2\n90 95", "output": "185" }, { "input": "2\n54 28", "output": "0" }, { "input": "10\n68 96 32 50 55 67 27 93 81 77", "output": "619" }, { "input": "5\n36 56 38 6 28", "output": "0" }, { "input": "6\n34 72 80 5 47 9", "output": "247" }, { "input": "100\n99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99", "output": "9801" }, { "input": "100\n100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100", "output": "0" }, { "input": "100\n100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 99 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100", "output": "9999" }, { "input": "100\n100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 99 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 99 100 100 100 100 100", "output": "9899" }, { "input": "100\n100 100 100 100 100 100 100 100 100 1 100 100 100 100 100 100 100 100 100 100 100 1 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 3 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100", "output": "9705" }, { "input": "5\n6 6 6 6 6", "output": "0" }, { "input": "4\n2 3 5 8", "output": "15" }, { "input": "4\n2 4 6 8", "output": "0" }, { "input": "4\n2 3 5 4", "output": "11" }, { "input": "3\n5 7 9", "output": "21" } ]
92
0
0
16,457
993
Compute Power
[ "binary search", "dp", "greedy" ]
null
null
You need to execute several tasks, each associated with number of processors it needs, and the compute power it will consume. You have sufficient number of analog computers, each with enough processors for any task. Each computer can execute up to one task at a time, and no more than two tasks total. The first task can be any, the second task on each computer must use strictly less power than the first. You will assign between 1 and 2 tasks to each computer. You will then first execute the first task on each computer, wait for all of them to complete, and then execute the second task on each computer that has two tasks assigned. If the average compute power per utilized processor (the sum of all consumed powers for all tasks presently running divided by the number of utilized processors) across all computers exceeds some unknown threshold during the execution of the first tasks, the entire system will blow up. There is no restriction on the second tasks execution. Find the lowest threshold for which it is possible. Due to the specifics of the task, you need to print the answer multiplied by 1000 and rounded up.
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=50) β€” the number of tasks. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=108), where *a**i* represents the amount of power required for the *i*-th task. The third line contains *n* integers *b*1,<=*b*2,<=...,<=*b**n* (1<=≀<=*b**i*<=≀<=100), where *b**i* is the number of processors that *i*-th task will utilize.
Print a single integer value β€” the lowest threshold for which it is possible to assign all tasks in such a way that the system will not blow up after the first round of computation, multiplied by 1000 and rounded up.
[ "6\n8 10 9 9 8 10\n1 1 1 1 1 1\n", "6\n8 10 9 9 8 10\n1 10 5 5 1 10\n" ]
[ "9000\n", "1160\n" ]
In the first example the best strategy is to run each task on a separate computer, getting average compute per processor during the first round equal to 9. In the second task it is best to run tasks with compute 10 and 9 on one computer, tasks with compute 10 and 8 on another, and tasks with compute 9 and 8 on the last, averaging (10 + 10 + 9) / (10 + 10 + 5) = 1.16 compute power per processor during the first round.
[ { "input": "6\n8 10 9 9 8 10\n1 1 1 1 1 1", "output": "9000" }, { "input": "6\n8 10 9 9 8 10\n1 10 5 5 1 10", "output": "1160" }, { "input": "1\n1\n100", "output": "10" }, { "input": "50\n83 43 73 75 11 53 6 43 67 38 83 12 70 27 60 13 9 79 61 30 29 71 10 11 95 87 26 26 19 99 13 47 66 93 91 47 90 75 68 3 22 29 59 12 44 41 64 3 99 100\n31 36 69 25 18 33 15 70 12 91 41 44 1 96 80 74 12 80 16 82 88 25 87 17 53 63 3 42 81 6 50 78 34 68 65 78 94 14 53 14 41 97 63 44 21 62 95 37 36 31", "output": "705" }, { "input": "50\n95 86 10 54 82 42 64 88 14 62 2 31 10 80 18 47 73 81 42 98 30 86 65 77 45 28 39 9 88 58 19 70 41 6 33 7 50 34 22 69 37 65 98 89 46 48 9 76 57 64\n87 39 41 23 49 45 91 83 50 92 25 11 76 1 97 42 62 91 2 53 40 11 93 72 66 8 8 62 35 14 57 95 15 80 95 51 60 95 25 70 27 59 51 76 99 100 87 58 24 7", "output": "637" }, { "input": "50\n1 2 7 8 4 9 1 8 3 6 7 2 10 10 4 2 1 7 9 10 10 1 4 7 5 6 1 6 6 2 5 4 5 10 9 9 7 5 5 7 1 3 9 6 2 3 9 10 6 3\n29 37 98 68 71 45 20 38 88 34 85 33 55 80 99 29 28 53 79 100 76 53 18 32 39 29 54 18 56 95 94 60 80 3 24 69 52 91 51 7 36 37 67 28 99 10 99 66 92 48", "output": "78" }, { "input": "5\n99999948 99999931 99999946 99999958 99999965\n43 42 42 24 87", "output": "1744185140" }, { "input": "5\n61 56 77 33 13\n79 40 40 26 56", "output": "863" }, { "input": "5\n99999943 99999973 99999989 99999996 99999953\n2 6 5 2 1", "output": "23076919847" }, { "input": "5\n21581303 73312811 99923326 93114466 53291492\n32 75 75 33 5", "output": "1070425495" }, { "input": "5\n99999950 99999991 99999910 99999915 99999982\n99 55 71 54 100", "output": "1181102060" }, { "input": "5\n81372426 35955615 58387606 77143158 48265342\n9 8 1 6 3", "output": "8455269522" }, { "input": "5\n88535415 58317418 74164690 46139122 28946947\n3 9 3 1 4", "output": "10987486250" }, { "input": "5\n5 4 3 7 3\n7 7 14 57 94", "output": "89" }, { "input": "5\n99 65 93 94 17\n1 5 6 2 3", "output": "18267" }, { "input": "10\n99999917 99999940 99999907 99999901 99999933 99999930 99999964 99999929 99999967 99999947\n93 98 71 41 13 7 24 70 52 70", "output": "1305482246" }, { "input": "10\n7 9 8 9 4 8 5 2 10 5\n6 6 7 8 9 7 10 1 1 7", "output": "977" }, { "input": "10\n68 10 16 26 94 30 17 90 40 26\n36 3 5 9 60 92 55 10 25 27", "output": "871" }, { "input": "10\n4 6 4 4 6 7 2 7 7 8\n35 50 93 63 8 59 46 97 50 88", "output": "75" }, { "input": "10\n99999954 99999947 99999912 99999920 99999980 99999928 99999908 99999999 99999927 99999957\n15 97 18 8 82 21 73 15 28 75", "output": "1621620860" }, { "input": "10\n46 29 60 65 57 95 82 52 39 21\n35 24 8 69 63 27 69 29 94 64", "output": "918" }, { "input": "10\n9 5 1 4 7 6 10 10 3 8\n40 84 53 88 20 33 55 41 34 55", "output": "100" }, { "input": "10\n99999983 99999982 99999945 99999989 99999981 99999947 99999941 99999987 99999965 99999914\n65 14 84 48 71 14 86 65 61 76", "output": "1414140889" }, { "input": "10\n3 10 3 1 3 8 9 7 1 5\n11 18 35 41 47 38 51 68 85 58", "output": "96" }, { "input": "50\n2 10 10 6 8 1 5 10 3 4 3 5 5 8 4 5 8 2 3 3 3 8 8 5 5 5 5 8 2 5 1 5 4 8 3 7 10 8 6 1 4 9 4 9 1 9 2 7 9 9\n10 6 2 2 3 6 5 5 4 1 3 1 2 3 10 10 6 8 7 2 8 5 2 5 4 9 7 5 2 8 3 6 9 8 2 5 8 3 7 3 3 6 3 7 6 10 9 2 9 7", "output": "785" }, { "input": "50\n88 86 31 49 90 52 57 70 39 94 8 90 39 89 56 78 10 80 9 18 95 96 8 57 29 37 13 89 32 99 85 61 35 37 44 55 92 16 69 80 90 34 84 25 26 17 71 93 46 7\n83 95 7 23 34 68 100 89 8 82 36 84 52 42 44 2 25 6 40 72 19 2 75 70 83 3 92 58 51 88 77 75 75 52 15 20 77 63 6 32 39 86 16 22 8 83 53 66 39 13", "output": "751" }, { "input": "50\n84 98 70 31 72 99 83 73 24 28 100 87 3 12 84 85 28 16 53 29 77 64 38 85 44 60 12 58 3 61 88 42 14 83 1 11 57 63 77 37 99 97 50 94 55 3 12 50 27 68\n9 1 4 6 10 5 3 2 4 6 6 9 8 6 1 2 2 1 8 5 8 1 9 1 2 10 2 7 5 1 7 4 7 1 3 6 10 7 3 5 1 3 4 8 4 7 3 3 10 7", "output": "7265" }, { "input": "50\n5 6 10 7 3 8 5 1 5 3 10 7 9 3 9 5 5 4 8 1 6 10 6 7 8 2 2 3 1 4 10 1 2 9 6 6 10 10 2 7 1 6 1 1 7 9 1 8 5 4\n2 2 6 1 5 1 4 9 5 3 5 3 2 1 5 7 4 10 9 8 5 8 1 10 6 7 5 4 10 3 9 4 1 5 6 9 3 8 9 8 2 10 7 3 10 1 1 7 5 3", "output": "736" }, { "input": "1\n100000000\n1", "output": "100000000000" } ]
30
0
0
16,461
187
Permutations
[ "greedy" ]
null
null
Happy PMP is freshman and he is learning about algorithmic problems. He enjoys playing algorithmic games a lot. One of the seniors gave Happy PMP a nice game. He is given two permutations of numbers 1 through *n* and is asked to convert the first one to the second. In one move he can remove the last number from the permutation of numbers and inserts it back in an arbitrary position. He can either insert last number between any two consecutive numbers, or he can place it at the beginning of the permutation. Happy PMP has an algorithm that solves the problem. But it is not fast enough. He wants to know the minimum number of moves to convert the first permutation to the second.
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=2Β·105) β€” the quantity of the numbers in the both given permutations. Next line contains *n* space-separated integers β€” the first permutation. Each number between 1 to *n* will appear in the permutation exactly once. Next line describe the second permutation in the same format.
Print a single integer denoting the minimum number of moves required to convert the first permutation to the second.
[ "3\n3 2 1\n1 2 3\n", "5\n1 2 3 4 5\n1 5 2 3 4\n", "5\n1 5 2 3 4\n1 2 3 4 5\n" ]
[ "2\n", "1\n", "3\n" ]
In the first sample, he removes number 1 from end of the list and places it at the beginning. After that he takes number 2 and places it between 1 and 3. In the second sample, he removes number 5 and inserts it after 1. In the third sample, the sequence of changes are like this: - 1 5 2 3 4 - 1 4 5 2 3 - 1 3 4 5 2 - 1 2 3 4 5
[ { "input": "3\n3 2 1\n1 2 3", "output": "2" }, { "input": "5\n1 2 3 4 5\n1 5 2 3 4", "output": "1" }, { "input": "5\n1 5 2 3 4\n1 2 3 4 5", "output": "3" }, { "input": "1\n1\n1", "output": "0" }, { "input": "7\n6 1 7 3 4 5 2\n6 1 7 3 4 5 2", "output": "0" }, { "input": "10\n5 8 1 10 3 6 2 9 7 4\n4 2 6 3 1 9 10 5 8 7", "output": "8" }, { "input": "10\n1 6 10 3 4 9 2 5 8 7\n7 5 1 6 10 3 4 8 9 2", "output": "3" }, { "input": "10\n2 1 10 3 7 8 5 6 9 4\n6 9 2 4 1 10 3 7 8 5", "output": "3" }, { "input": "10\n8 2 10 3 4 6 1 7 9 5\n8 2 10 3 4 6 1 7 9 5", "output": "0" }, { "input": "20\n1 12 9 6 11 13 2 8 20 7 16 19 4 18 3 15 10 17 14 5\n5 14 17 10 15 3 18 4 19 16 7 20 8 2 13 11 6 9 12 1", "output": "19" } ]
92
0
0
16,481
794
Leha and security system
[ "data structures" ]
null
null
Bankopolis, the city you already know, finally got a new bank opened! Unfortunately, its security system is not yet working fine... Meanwhile hacker Leha arrived in Bankopolis and decided to test the system! Bank has *n* cells for clients' money. A sequence from *n* numbers *a*1,<=*a*2,<=...,<=*a**n* describes the amount of money each client has. Leha wants to make requests to the database of the bank, finding out the total amount of money on some subsegments of the sequence and changing values of the sequence on some subsegments. Using a bug in the system, Leha can requests two types of queries to the database: - 1 l r x y denoting that Leha changes each digit *x* to digit *y* in each element of sequence *a**i*, for which *l*<=≀<=*i*<=≀<=*r* is holds. For example, if we change in number 11984381 digit 8 to 4, we get 11944341. It's worth noting that Leha, in order to stay in the shadow, never changes digits in the database to 0, i.e. *y*<=β‰ <=0. - 2 l r denoting that Leha asks to calculate and print the sum of such elements of sequence *a**i*, for which *l*<=≀<=*i*<=≀<=*r* holds. As Leha is a white-hat hacker, he don't want to test this vulnerability on a real database. You are to write a similar database for Leha to test.
The first line of input contains two integers *n* and *q* (1<=≀<=*n*<=≀<=105, 1<=≀<=*q*<=≀<=105) denoting amount of cells in the bank and total amount of queries respectively. The following line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=&lt;<=109) denoting the amount of money in each cell initially. These integers do not contain leading zeros. Each of the following *q* lines has one of the formats: - 1 l r x y (1<=≀<=*l*<=≀<=*r*<=≀<=*n*, 0<=≀<=*x*<=≀<=9, 1<=≀<=*y*<=≀<=9), denoting Leha asks to change each digit *x* on digit *y* for each element *a**i* of the sequence for which *l*<=≀<=*i*<=≀<=*r* holds; - 2 l r (1<=≀<=*l*<=≀<=*r*<=≀<=*n*), denoting you have to calculate and print the sum of elements *a**i* for which *l*<=≀<=*i*<=≀<=*r* holds.
For each second type query print a single number denoting the required sum.
[ "5 5\n38 43 4 12 70\n1 1 3 4 8\n2 2 4\n1 4 5 0 8\n1 2 5 8 7\n2 1 5\n", "5 5\n25 36 39 40 899\n1 1 3 2 7\n2 1 2\n1 3 5 9 1\n1 4 4 0 9\n2 1 5\n" ]
[ "103\n207\n", "111\n1002\n" ]
Let's look at the example testcase. Initially the sequence is [38, 43, 4, 12, 70]. After the first change each digit equal to 4 becomes 8 for each element with index in interval [1;Β 3]. Thus, the new sequence is [38, 83, 8, 12, 70]. The answer for the first sum's query is the sum in the interval [2;Β 4], which equal 83 + 8 + 12 = 103, so the answer to this query is 103. The sequence becomes [38, 83, 8, 12, 78] after the second change and [38, 73, 7, 12, 77] after the third. The answer for the second sum's query is 38 + 73 + 7 + 12 + 77 = 207.
[]
108
0
0
16,493
510
Fox And Jumping
[ "bitmasks", "brute force", "dp", "math" ]
null
null
Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0. There are also *n* cards, each card has 2 attributes: length *l**i* and cost *c**i*. If she pays *c**i* dollars then she can apply *i*-th card. After applying *i*-th card she becomes able to make jumps of length *l**i*, i. e. from cell *x* to cell (*x*<=-<=*l**i*) or cell (*x*<=+<=*l**i*). She wants to be able to jump to any cell on the tape (possibly, visiting some intermediate cells). For achieving this goal, she wants to buy some cards, paying as little money as possible. If this is possible, calculate the minimal cost.
The first line contains an integer *n* (1<=≀<=*n*<=≀<=300), number of cards. The second line contains *n* numbers *l**i* (1<=≀<=*l**i*<=≀<=109), the jump lengths of cards. The third line contains *n* numbers *c**i* (1<=≀<=*c**i*<=≀<=105), the costs of cards.
If it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards.
[ "3\n100 99 9900\n1 1 1\n", "5\n10 20 30 40 50\n1 1 1 1 1\n", "7\n15015 10010 6006 4290 2730 2310 1\n1 1 1 1 1 1 10\n", "8\n4264 4921 6321 6984 2316 8432 6120 1026\n4264 4921 6321 6984 2316 8432 6120 1026\n" ]
[ "2\n", "-1\n", "6\n", "7237\n" ]
In first sample test, buying one card is not enough: for example, if you buy a card with length 100, you can't jump to any cell whose index is not a multiple of 100. The best way is to buy first and second card, that will make you be able to jump to any cell. In the second sample test, even if you buy all cards, you can't jump to any cell whose index is not a multiple of 10, so you should output -1.
[ { "input": "3\n100 99 9900\n1 1 1", "output": "2" }, { "input": "5\n10 20 30 40 50\n1 1 1 1 1", "output": "-1" }, { "input": "7\n15015 10010 6006 4290 2730 2310 1\n1 1 1 1 1 1 10", "output": "6" }, { "input": "8\n4264 4921 6321 6984 2316 8432 6120 1026\n4264 4921 6321 6984 2316 8432 6120 1026", "output": "7237" }, { "input": "6\n1 2 4 8 16 32\n32 16 8 4 2 1", "output": "32" }, { "input": "1\n1\n1", "output": "1" }, { "input": "1\n2\n2", "output": "-1" }, { "input": "8\n2 3 5 7 11 13 17 19\n4 8 7 1 5 2 6 3", "output": "3" }, { "input": "1\n1000000000\n100000", "output": "-1" }, { "input": "2\n1000000000 999999999\n100000 100000", "output": "200000" }, { "input": "39\n692835 4849845 22610 1995 19019 114 6270 15 85085 27170 1365 1155 7410 238 3135 546 373065 715 110 969 15 10374 2730 19019 85 65 5187 26 3233230 1122 399 1122 53295 910 110 12597 16302 125970 67830\n4197 6490 2652 99457 65400 96257 33631 23456 14319 22288 16179 74656 89713 31503 45895 31777 64534 27989 60861 69846 44586 87185 96589 62279 62478 6180 26977 12112 9975 72933 73239 65856 98253 18875 55266 55867 36397 40743 47977", "output": "18961" }, { "input": "35\n512 268435456 8 128 134217728 8192 33554432 33554432 536870912 512 65536 1048576 32768 512 524288 1024 536870912 536870912 16 32 33554432 134217728 2 16 16777216 8192 262144 65536 33554432 128 4096 2097152 33554432 2097152 2\n36157 67877 79710 63062 12683 36255 61053 83828 93590 74236 5281 28143 7350 45953 96803 15998 11240 45207 63010 74076 85227 83498 68320 77288 48100 51373 87843 70054 28986 25365 98581 11195 43674 75769 22053", "output": "-1" } ]
46
0
0
16,514
21
Traveling Graph
[ "bitmasks", "graph matchings", "graphs" ]
D. Traveling Graph
0
64
You are given undirected weighted graph. Find the length of the shortest cycle which starts from the vertex 1 and passes throught all the edges at least once. Graph may contain multiply edges between a pair of vertices and loops (edges from the vertex to itself).
The first line of the input contains two integers *n* and *m* (1<=≀<=*n*<=≀<=15,<=0<=≀<=*m*<=≀<=2000), *n* is the amount of vertices, and *m* is the amount of edges. Following *m* lines contain edges as a triples *x*,<=*y*,<=*w* (1<=≀<=*x*,<=*y*<=≀<=*n*,<=1<=≀<=*w*<=≀<=10000), *x*,<=*y* are edge endpoints, and *w* is the edge length.
Output minimal cycle length or -1 if it doesn't exists.
[ "3 3\n1 2 1\n2 3 1\n3 1 1\n", "3 2\n1 2 3\n2 3 4\n" ]
[ "3\n", "14\n" ]
none
[ { "input": "4 6\n1 2 10\n2 3 1000\n3 4 10\n4 1 1000\n4 2 5000\n1 3 2", "output": "7042" }, { "input": "2 9\n1 2 9\n1 2 9\n2 1 9\n1 2 8\n2 1 9\n1 2 9\n1 2 9\n1 2 11\n1 2 9", "output": "90" }, { "input": "2 10\n1 2 9\n1 2 9\n2 1 9\n1 2 8\n2 1 9\n1 2 9\n1 2 9\n1 2 11\n1 2 9\n1 2 9", "output": "91" }, { "input": "15 14\n1 2 1\n2 3 1\n2 4 1\n3 5 1\n3 6 1\n4 7 1\n4 8 1\n5 9 1\n5 10 1\n6 11 1\n6 12 1\n7 13 1\n7 14 1\n8 15 1", "output": "28" }, { "input": "4 5\n1 2 3\n2 3 4\n3 4 5\n1 4 10\n1 3 12", "output": "41" }, { "input": "4 5\n1 2 3\n2 3 4\n3 4 5\n1 4 10\n1 3 12", "output": "41" }, { "input": "5 0", "output": "0" }, { "input": "1 0", "output": "0" }, { "input": "1 1\n1 1 44", "output": "44" }, { "input": "1 2\n1 1 5\n1 1 3", "output": "8" }, { "input": "2 0", "output": "0" }, { "input": "2 1\n2 1 3", "output": "6" }, { "input": "2 1\n1 1 3", "output": "3" }, { "input": "2 1\n2 2 44", "output": "-1" }, { "input": "2 2\n1 1 44\n2 2 44", "output": "-1" }, { "input": "2 3\n1 1 1\n2 2 2\n2 1 3", "output": "9" }, { "input": "7 3\n4 4 1\n7 7 1\n2 2 1", "output": "-1" }, { "input": "15 0", "output": "0" }, { "input": "4 2\n1 2 1\n3 4 1", "output": "-1" }, { "input": "7 1\n3 4 4", "output": "-1" }, { "input": "2 1\n2 2 5741", "output": "-1" }, { "input": "2 2\n2 1 4903\n1 1 4658", "output": "14464" }, { "input": "2 4\n1 2 7813\n2 1 6903\n1 2 6587\n2 2 7372", "output": "35262" }, { "input": "2 8\n1 2 4618\n1 1 6418\n2 2 2815\n1 1 4077\n2 1 4239\n1 2 5359\n1 2 3971\n1 2 7842", "output": "43310" }, { "input": "3 1\n3 2 6145", "output": "-1" }, { "input": "3 2\n1 1 1169\n1 2 1250", "output": "3669" }, { "input": "3 4\n1 1 5574\n3 1 5602\n3 2 5406\n2 1 5437", "output": "22019" }, { "input": "3 8\n3 3 9507\n2 1 9560\n3 3 9328\n2 2 9671\n2 2 9641\n1 2 9717\n1 3 9535\n1 2 9334", "output": "95162" }, { "input": "4 1\n1 3 3111", "output": "6222" }, { "input": "4 2\n3 2 6816\n1 3 7161", "output": "27954" }, { "input": "4 4\n1 3 1953\n3 2 2844\n1 3 2377\n3 2 2037", "output": "9211" }, { "input": "4 8\n1 2 4824\n3 1 436\n2 2 3087\n2 4 2955\n2 4 2676\n4 3 2971\n3 4 3185\n3 1 3671", "output": "28629" }, { "input": "5 1\n5 5 1229", "output": "-1" }, { "input": "5 2\n2 2 2515\n2 4 3120", "output": "-1" }, { "input": "5 4\n5 1 404\n3 1 551\n1 1 847\n5 1 706", "output": "3059" }, { "input": "5 8\n1 5 1016\n4 5 918\n1 4 926\n2 3 928\n5 4 994\n2 3 1007\n1 4 946\n3 4 966", "output": "9683" }, { "input": "6 1\n3 6 2494", "output": "-1" }, { "input": "6 2\n5 3 5039\n2 3 4246", "output": "-1" }, { "input": "6 4\n5 4 6847\n3 6 7391\n1 6 7279\n2 5 7250", "output": "-1" }, { "input": "6 8\n2 4 8044\n6 4 7952\n2 5 6723\n6 4 8105\n1 5 6648\n1 6 6816\n1 3 7454\n5 3 6857", "output": "73199" }, { "input": "15 1\n7 5 7838", "output": "-1" }, { "input": "15 2\n5 13 9193\n14 5 9909", "output": "-1" }, { "input": "15 4\n1 5 5531\n9 15 3860\n8 4 6664\n13 3 4320", "output": "-1" }, { "input": "15 8\n14 6 9084\n1 12 8967\n11 12 8866\n12 2 8795\n7 10 9102\n10 12 9071\n12 10 9289\n4 11 8890", "output": "-1" }, { "input": "15 16\n3 3 2551\n6 11 2587\n2 4 2563\n3 6 2569\n3 1 2563\n4 11 2487\n7 15 2580\n7 14 2534\n10 7 2530\n3 5 2587\n5 14 2596\n14 14 2556\n15 9 2547\n12 4 2586\n6 8 2514\n2 12 2590", "output": "69034" }, { "input": "15 32\n15 9 8860\n12 4 9045\n12 12 8221\n9 6 8306\n11 14 9052\n13 14 8176\n14 5 8857\n6 8 8835\n3 9 8382\n10 14 8212\n13 13 9061\n2 14 8765\n4 13 9143\n13 13 8276\n13 11 8723\n7 10 8775\n8 15 8965\n15 5 8800\n4 5 9317\n5 13 9178\n1 7 9031\n4 10 9114\n10 4 8628\n9 1 8584\n5 7 8701\n6 15 8177\n3 3 9325\n4 5 9003\n7 5 9308\n8 9 8307\n12 13 8547\n7 7 8209", "output": "315043" } ]
434
5,120,000
3
16,613
578
Weakness and Poorness
[ "ternary search" ]
null
null
You are given a sequence of n integers *a*1,<=*a*2,<=...,<=*a**n*. Determine a real number *x* such that the weakness of the sequence *a*1<=-<=*x*,<=*a*2<=-<=*x*,<=...,<=*a**n*<=-<=*x* is as small as possible. The weakness of a sequence is defined as the maximum value of the poorness over all segments (contiguous subsequences) of a sequence. The poorness of a segment is defined as the absolute value of sum of the elements of segment.
The first line contains one integer *n* (1<=≀<=*n*<=≀<=200<=000), the length of a sequence. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (|*a**i*|<=≀<=10<=000).
Output a real number denoting the minimum possible weakness of *a*1<=-<=*x*,<=*a*2<=-<=*x*,<=...,<=*a**n*<=-<=*x*. Your answer will be considered correct if its relative or absolute error doesn't exceed 10<=-<=6.
[ "3\n1 2 3\n", "4\n1 2 3 4\n", "10\n1 10 2 9 3 8 4 7 5 6\n" ]
[ "1.000000000000000\n", "2.000000000000000\n", "4.500000000000000\n" ]
For the first case, the optimal value of *x* is 2 so the sequence becomes  - 1, 0, 1 and the max poorness occurs at the segment "-1" or segment "1". The poorness value (answer) equals to 1 in this case. For the second sample the optimal value of *x* is 2.5 so the sequence becomes  - 1.5,  - 0.5, 0.5, 1.5 and the max poorness occurs on segment "-1.5 -0.5" or "0.5 1.5". The poorness value (answer) equals to 2 in this case.
[ { "input": "3\n1 2 3", "output": "1.000000000000000" }, { "input": "4\n1 2 3 4", "output": "2.000000000000000" }, { "input": "10\n1 10 2 9 3 8 4 7 5 6", "output": "4.500000000000000" }, { "input": "1\n-10000", "output": "0.000000000000000" }, { "input": "3\n10000 -10000 10000", "output": "10000.000000000000000" }, { "input": "20\n-16 -23 29 44 -40 -50 -41 34 -38 30 -12 28 -44 -49 15 50 -28 38 -2 0", "output": "113.875000000000000" }, { "input": "10\n-405 -230 252 -393 -390 -259 97 163 81 -129", "output": "702.333333333333370" } ]
92
0
0
16,623
425
Sereja and Two Sequences
[ "data structures", "dp" ]
null
null
Sereja has two sequences *a*1,<=*a*2,<=...,<=*a**n* and *b*1,<=*b*2,<=...,<=*b**m*, consisting of integers. One day Sereja got bored and he decided two play with them. The rules of the game was very simple. Sereja makes several moves, in one move he can perform one of the following actions: 1. Choose several (at least one) first elements of sequence *a* (non-empty prefix of *a*), choose several (at least one) first elements of sequence *b* (non-empty prefix of *b*); the element of sequence *a* with the maximum index among the chosen ones must be equal to the element of sequence *b* with the maximum index among the chosen ones; remove the chosen elements from the sequences. 1. Remove all elements of both sequences. The first action is worth *e* energy units and adds one dollar to Sereja's electronic account. The second action is worth the number of energy units equal to the number of elements Sereja removed from the sequences before performing this action. After Sereja performed the second action, he gets all the money that he earned on his electronic account during the game. Initially Sereja has *s* energy units and no money on his account. What maximum number of money can Sereja get? Note, the amount of Seraja's energy mustn't be negative at any time moment.
The first line contains integers *n*, *m*, *s*, *e* (1<=≀<=*n*,<=*m*<=≀<=105;Β 1<=≀<=*s*<=≀<=3Β·105;Β 103<=≀<=*e*<=≀<=104). The second line contains *n* integers *a*1, *a*2, ..., *a**n* (1<=≀<=*a**i*<=≀<=105). The third line contains *m* integers *b*1, *b*2, ..., *b**m* (1<=≀<=*b**i*<=≀<=105).
Print a single integer β€” maximum number of money in dollars that Sereja can get.
[ "5 5 100000 1000\n1 2 3 4 5\n3 2 4 5 1\n", "3 4 3006 1000\n1 2 3\n1 2 4 3\n" ]
[ "3\n", "2\n" ]
none
[]
30
0
0
16,627
18
Flag 2
[ "dp" ]
E. Flag 2
2
128
According to a new ISO standard, a flag of every country should have, strangely enough, a chequered field *n*<=Γ—<=*m*, each square should be wholly painted one of 26 colours. The following restrictions are set: - In each row at most two different colours can be used. - No two adjacent squares can be painted the same colour. Pay attention, please, that in one column more than two different colours can be used. Berland's government took a decision to introduce changes into their country's flag in accordance with the new standard, at the same time they want these changes to be minimal. By the given description of Berland's flag you should find out the minimum amount of squares that need to be painted different colour to make the flag meet the new ISO standard. You are as well to build one of the possible variants of the new Berland's flag.
The first input line contains 2 integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=500) β€” amount of rows and columns in Berland's flag respectively. Then there follows the flag's description: each of the following *n* lines contains *m* characters. Each character is a letter from a to z, and it stands for the colour of the corresponding square.
In the first line output the minimum amount of squares that need to be repainted to make the flag meet the new ISO standard. The following *n* lines should contain one of the possible variants of the new flag. Don't forget that the variant of the flag, proposed by you, should be derived from the old flag with the minimum amount of repainted squares. If the answer isn't unique, output any.
[ "3 4\naaaa\nbbbb\ncccc\n", "3 3\naba\naba\nzzz\n" ]
[ "6\nabab\nbaba\nacac\n", "4\naba\nbab\nzbz\n" ]
none
[ { "input": "3 4\naaaa\nbbbb\ncccc", "output": "6\nabab\nbaba\nacac" }, { "input": "3 3\naba\naba\nzzz", "output": "4\naba\nbab\nzbz" }, { "input": "5 6\nababab\nbababa\nbbbbbb\nbababa\nababab", "output": "3\nababab\nbababa\nababab\nbababa\nababab" }, { "input": "1 1\nq", "output": "0\nq" }, { "input": "1 2\njj", "output": "1\naj" }, { "input": "2 1\ns\ns", "output": "1\na\ns" }, { "input": "10 12\nvmqbubmuurmr\nuuqvrbbquvmq\nuuumuuqqmvuq\nqqrrrmvuqvmr\nmqbbbmrmvvrr\numrvrumbrmmb\nrqvurbrmubvm\nubbvbbbbqrqu\nmqruvqrburqq\nmmbumubrbrrb", "output": "75\nmbmbmbmbmbmb\nbvbvbvbvbvbv\nuquququququq\nqrqrqrqrqrqr\nbmbmbmbmbmbm\nmbmbmbmbmbmb\nrmrmrmrmrmrm\nqbqbqbqbqbqb\nrqrqrqrqrqrq\nbrbrbrbrbrbr" }, { "input": "20 19\nijausuiucgjugfuuucf\njiikkuggcimkskmjsjj\nukfkjfkumifjjjasmau\nsucakisaafkakujcufu\nicjsksguikjkssjfmkc\nsakskisukfukmsafsiu\ncjacjjaucujjiuafuag\nfksjgumggcggsgsikus\nmkfjkcsfigjfcimuskc\nkfffaukajmiisujamug\njucccjfgkajfimgjusc\nssmcaksmiksagmiiais\nfjucgkkjsgafiusauja\njjagsicuaigaugkkgkm\ngkajgumasscgfjimfaj\nkjakkmsjskaiuigkcij\nfmcggmfkjmiskjuaiia\ngjkjsiacjiscfguumuk\njsgismsucssmasiasum\nukusijmgakkiuggkgaa", "output": "275\niuiuiuiuiuiuiuiuiui\njkjkjkjkjkjkjkjkjkj\nfjfjfjfjfjfjfjfjfjf\nkakakakakakakakakak\njkjkjkjkjkjkjkjkjkj\nkfkfkfkfkfkfkfkfkfk\najajajajajajajajaja\nsgsgsgsgsgsgsgsgsgs\ncfcfcfcfcfcfcfcfcfc\njujujujujujujujujuj\ncjcjcjcjcjcjcjcjcjc\nsisisisisisisisisis\najajajajajajajajaja\ngigigigigigigigigig\nfafafafafafafafafaf\naiaiaiaiaiaiaiaiaia\nfmfmfmfmfmfmfmfmfmf\nkckckckckckckckckck\nsmsmsmsmsmsmsmsmsms\nukukukukukukukukuku" }, { "input": "1 500\nwljexjwjwsxhascgpyezfotqyieywzyzpnjzxqebroginhlpsvtbgmbckgqixkdveraxlgleieeagubywaacncxoouibbhwyxhjiczlymscsqlttecvundzjqtuizqwsjalmizgeekugacatlwhiiyaamjuiafmyzxadbxsgwhfawotawfxkpyzyoceiqfppituvaqmgfjinulkbkgpnibaefqaqiptctoqhcnnfihmzijnwpxdikwefpxicbspdkppznpiqrpkyvcxapoaofjbliepgfwmppgwypgflzdtzvghcuwfzcrftftmmztzydybockfbbsjrcqnepvcqlscwkckrrqwqrqkapvobcjvhnbebrpodaxdnxakjwskwtmuwnhzadptayptbgwbfpucgsrumpwwvqvbvgrggfbzotoolmgsxmcppexfudzamtixuqljrhxnzaaekaekngpbkriidbusabmimnnmiznvfrbcwnmnv", "output": "468\npgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpgpg" }, { "input": "1 500\nuuqquuuuuuuuuuuququuuquuuqqqquuuuquqqququuquuquuuquuqqqququqqqquqquqqquuququqqqqquuuuuquuqqquuuuuuqququuuquququqqququuuuuuqqquuququuqquuquuuqquuuquqqqquuqquuquuquqqqquuuqququuqqqquuuuuuquququuuuuqqqqqqqqquququuuuquuuqquqquuqqquqququuqqqqqqquuqqquuquqquqquqqquqququqqquqquqqquuuqqququqqquuuuqqquuquuuququuuuqququqququuqquuuququuququuqquqqquqquqqqqqququuqquuququuuuuuuquqquuqqquuqqqquuuquqqquququqquuqququuqqqqqquuquqququuuuuqqqqqqqqququqqquqquqqqquqquuuqqqquuuqqquqquqquuqqququuqqqqquququuquuquuqqquuq", "output": "227\nuquququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququququq" }, { "input": "1 500\niiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii", "output": "250\naiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiai" }, { "input": "6 13\nababababababa\nababababababa\nggggggggggggg\nggggggggggggg\nggggggggggggg\nggggggggggggg", "output": "39\nababababababa\nbabababababab\nagagagagagaga\ngagagagagagag\nagagagagagaga\ngagagagagagag" }, { "input": "10 10\nababababab\nababababab\nababababab\nababababab\nababababab\nababababab\nababababab\nababababab\nababababab\nababababab", "output": "50\nababababab\nbababababa\nababababab\nbababababa\nababababab\nbababababa\nababababab\nbababababa\nababababab\nbababababa" }, { "input": "2 2\nab\nab", "output": "2\nab\nba" } ]
30
0
-1
16,676
439
Devu and Birthday Celebration
[ "combinatorics", "dp", "math" ]
null
null
Today is Devu's birthday. For celebrating the occasion, he bought *n* sweets from the nearby market. He has invited his *f* friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed *n* sweets to his friends such that *i**th* friend is given *a**i* sweets. He wants to make sure that there should not be any positive integer *x*<=&gt;<=1, which divides every *a**i*. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109<=+<=7). To make the problem more interesting, you are given *q* queries. Each query contains an *n*, *f* pair. For each query please output the required number of ways modulo 1000000007 (109<=+<=7).
The first line contains an integer *q* representing the number of queries (1<=≀<=*q*<=≀<=105). Each of the next *q* lines contains two space space-separated integers *n*, *f* (1<=≀<=*f*<=≀<=*n*<=≀<=105).
For each query, output a single integer in a line corresponding to the answer of each query.
[ "5\n6 2\n7 2\n6 3\n6 4\n7 4\n" ]
[ "2\n6\n9\n10\n20\n" ]
For first query: *n* = 6, *f* = 2. Possible partitions are [1, 5] and [5, 1]. For second query: *n* = 7, *f* = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning.
[ { "input": "5\n6 2\n7 2\n6 3\n6 4\n7 4", "output": "2\n6\n9\n10\n20" }, { "input": "10\n1 1\n1 1\n1 1\n7 2\n6 3\n9 5\n4 1\n2 1\n3 1\n2 2", "output": "1\n1\n1\n6\n9\n70\n0\n0\n0\n1" }, { "input": "40\n37 15\n48 10\n16 5\n25 23\n32 20\n24 4\n46 19\n16 13\n1 1\n37 22\n44 29\n24 6\n27 10\n39 16\n28 13\n5 4\n31 22\n9 2\n30 26\n23 16\n16 12\n43 5\n29 1\n20 5\n40 12\n18 14\n22 15\n29 2\n3 2\n6 3\n45 28\n42 34\n43 32\n10 10\n12 8\n10 8\n4 1\n17 17\n16 7\n8 7", "output": "796297179\n361826943\n1330\n276\n141120525\n1572\n884475620\n455\n1\n567902525\n532655639\n33166\n3124550\n471286455\n17383847\n4\n14307150\n6\n23751\n170544\n1365\n111930\n0\n3750\n675980455\n2380\n116280\n28\n2\n9\n353793174\n95548245\n280561348\n1\n330\n36\n0\n1\n4998\n7" }, { "input": "10\n201 98\n897 574\n703 669\n238 199\n253 71\n619 577\n656 597\n827 450\n165 165\n17 5", "output": "161386475\n287013046\n190482739\n953794468\n858506105\n813510912\n157101041\n44730040\n1\n1820" }, { "input": "5\n5 1\n5 2\n5 3\n5 4\n5 5", "output": "0\n4\n6\n4\n1" }, { "input": "1\n1 1", "output": "1" }, { "input": "2\n2 1\n2 2", "output": "0\n1" }, { "input": "3\n3 1\n3 2\n3 3", "output": "0\n2\n1" }, { "input": "15\n4 1\n4 2\n4 3\n4 4\n5 1\n5 2\n5 3\n5 4\n5 5\n6 1\n6 2\n6 3\n6 4\n6 5\n6 6", "output": "0\n2\n3\n1\n0\n4\n6\n4\n1\n0\n2\n9\n10\n5\n1" } ]
498
49,254,400
0
16,703
590
Median Smoothing
[ "implementation" ]
null
null
A schoolboy named Vasya loves reading books on programming and mathematics. He has recently read an encyclopedia article that described the method of median smoothing (or median filter) and its many applications in science and engineering. Vasya liked the idea of the method very much, and he decided to try it in practice. Applying the simplest variant of median smoothing to the sequence of numbers *a*1,<=*a*2,<=...,<=*a**n* will result a new sequence *b*1,<=*b*2,<=...,<=*b**n* obtained by the following algorithm: - *b*1<==<=*a*1, *b**n*<==<=*a**n*, that is, the first and the last number of the new sequence match the corresponding numbers of the original sequence. - For *i*<==<=2,<=...,<=*n*<=-<=1 value *b**i* is equal to the median of three values *a**i*<=-<=1, *a**i* and *a**i*<=+<=1. The median of a set of three numbers is the number that goes on the second place, when these three numbers are written in the non-decreasing order. For example, the median of the set 5, 1, 2 is number 2, and the median of set 1, 0, 1 is equal to 1. In order to make the task easier, Vasya decided to apply the method to sequences consisting of zeros and ones only. Having made the procedure once, Vasya looked at the resulting sequence and thought: what if I apply the algorithm to it once again, and then apply it to the next result, and so on? Vasya tried a couple of examples and found out that after some number of median smoothing algorithm applications the sequence can stop changing. We say that the sequence is stable, if it does not change when the median smoothing is applied to it. Now Vasya wonders, whether the sequence always eventually becomes stable. He asks you to write a program that, given a sequence of zeros and ones, will determine whether it ever becomes stable. Moreover, if it ever becomes stable, then you should determine what will it look like and how many times one needs to apply the median smoothing algorithm to initial sequence in order to obtain a stable one.
The first input line of the input contains a single integer *n* (3<=≀<=*n*<=≀<=500<=000)Β β€” the length of the initial sequence. The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (*a**i*<==<=0 or *a**i*<==<=1), giving the initial sequence itself.
If the sequence will never become stable, print a single number <=-<=1. Otherwise, first print a single integerΒ β€” the minimum number of times one needs to apply the median smoothing algorithm to the initial sequence before it becomes is stable. In the second line print *n* numbers separated by a space Β β€” the resulting sequence itself.
[ "4\n0 0 1 1\n", "5\n0 1 0 1 0\n" ]
[ "0\n0 0 1 1\n", "2\n0 0 0 0 0\n" ]
In the second sample the stabilization occurs in two steps: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/5a983e7baab048cbe43812cb997c15e9d7100231.png" style="max-width: 100.0%;max-height: 100.0%;"/>, and the sequence 00000 is obviously stable.
[ { "input": "4\n0 0 1 1", "output": "0\n0 0 1 1" }, { "input": "5\n0 1 0 1 0", "output": "2\n0 0 0 0 0" }, { "input": "3\n1 0 0", "output": "0\n1 0 0" }, { "input": "4\n1 0 0 1", "output": "0\n1 0 0 1" }, { "input": "7\n1 0 1 1 1 0 1", "output": "1\n1 1 1 1 1 1 1" }, { "input": "14\n0 1 0 0 0 1 1 0 1 0 1 0 1 0", "output": "3\n0 0 0 0 0 1 1 1 1 1 0 0 0 0" }, { "input": "3\n1 0 1", "output": "1\n1 1 1" }, { "input": "3\n0 0 1", "output": "0\n0 0 1" }, { "input": "3\n1 1 0", "output": "0\n1 1 0" }, { "input": "3\n1 1 1", "output": "0\n1 1 1" }, { "input": "4\n1 1 0 1", "output": "1\n1 1 1 1" }, { "input": "4\n1 0 1 1", "output": "1\n1 1 1 1" }, { "input": "10\n0 1 0 1 0 0 1 0 1 0", "output": "2\n0 0 0 0 0 0 0 0 0 0" }, { "input": "4\n0 1 1 0", "output": "0\n0 1 1 0" }, { "input": "168\n0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 0", "output": "36\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0" }, { "input": "3\n0 1 1", "output": "0\n0 1 1" }, { "input": "3\n0 0 0", "output": "0\n0 0 0" }, { "input": "4\n0 1 0 1", "output": "1\n0 0 1 1" }, { "input": "3\n0 1 0", "output": "1\n0 0 0" } ]
46
0
0
16,745
630
Cracking the Code
[ "implementation", "math" ]
null
null
The protection of a popular program developed by one of IT City companies is organized the following way. After installation it outputs a random five digit number which should be sent in SMS to a particular phone number. In response an SMS activation code arrives. A young hacker Vasya disassembled the program and found the algorithm that transforms the shown number into the activation code. Note: it is clear that Vasya is a law-abiding hacker, and made it for a noble purpose β€” to show the developer the imperfection of their protection. The found algorithm looks the following way. At first the digits of the number are shuffled in the following order &lt;first digit&gt;&lt;third digit&gt;&lt;fifth digit&gt;&lt;fourth digit&gt;&lt;second digit&gt;. For example the shuffle of 12345 should lead to 13542. On the second stage the number is raised to the fifth power. The result of the shuffle and exponentiation of the number 12345 is 455Β 422Β 043Β 125Β 550Β 171Β 232. The answer is the 5 last digits of this result. For the number 12345 the answer should be 71232. Vasya is going to write a keygen program implementing this algorithm. Can you do the same?
The only line of the input contains a positive integer five digit number for which the activation code should be found.
Output exactly 5 digits without spaces between them β€” the found activation code of the program.
[ "12345\n" ]
[ "71232" ]
none
[ { "input": "12345", "output": "71232" }, { "input": "13542", "output": "84443" }, { "input": "71232", "output": "10151" }, { "input": "11111", "output": "36551" }, { "input": "10000", "output": "00000" }, { "input": "99999", "output": "99999" }, { "input": "91537", "output": "27651" }, { "input": "70809", "output": "00000" }, { "input": "41675", "output": "61851" }, { "input": "32036", "output": "82432" } ]
46
0
3
16,751
538
Weird Chess
[ "brute force", "constructive algorithms", "implementation" ]
null
null
Igor has been into chess for a long time and now he is sick of the game by the ordinary rules. He is going to think of new rules of the game and become world famous. Igor's chessboard is a square of size *n*<=Γ—<=*n* cells. Igor decided that simple rules guarantee success, that's why his game will have only one type of pieces. Besides, all pieces in his game are of the same color. The possible moves of a piece are described by a set of shift vectors. The next passage contains a formal description of available moves. Let the rows of the board be numbered from top to bottom and the columns be numbered from left to right from 1 to *n*. Let's assign to each square a pair of integers (*x*,<=*y*)Β β€” the number of the corresponding column and row. Each of the possible moves of the piece is defined by a pair of integers (*dx*,<=*dy*); using this move, the piece moves from the field (*x*,<=*y*) to the field (*x*<=+<=*dx*,<=*y*<=+<=*dy*). You can perform the move if the cell (*x*<=+<=*dx*,<=*y*<=+<=*dy*) is within the boundaries of the board and doesn't contain another piece. Pieces that stand on the cells other than (*x*,<=*y*) and (*x*<=+<=*dx*,<=*y*<=+<=*dy*) are not important when considering the possibility of making the given move (for example, like when a knight moves in usual chess). Igor offers you to find out what moves his chess piece can make. He placed several pieces on the board and for each unoccupied square he told you whether it is attacked by any present piece (i.e. whether some of the pieces on the field can move to that cell). Restore a possible set of shift vectors of the piece, or else determine that Igor has made a mistake and such situation is impossible for any set of shift vectors.
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=50). The next *n* lines contain *n* characters each describing the position offered by Igor. The *j*-th character of the *i*-th string can have the following values: - o β€” in this case the field (*i*,<=*j*) is occupied by a piece and the field may or may not be attacked by some other piece;- x β€” in this case field (*i*,<=*j*) is attacked by some piece;- . β€” in this case field (*i*,<=*j*) isn't attacked by any piece. It is guaranteed that there is at least one piece on the board.
If there is a valid set of moves, in the first line print a single word 'YES' (without the quotes). Next, print the description of the set of moves of a piece in the form of a (2*n*<=-<=1)<=Γ—<=(2*n*<=-<=1) board, the center of the board has a piece and symbols 'x' mark cells that are attacked by it, in a format similar to the input. See examples of the output for a full understanding of the format. If there are several possible answers, print any of them. If a valid set of moves does not exist, print a single word 'NO'.
[ "5\noxxxx\nx...x\nx...x\nx...x\nxxxxo\n", "6\n.x.x..\nx.x.x.\n.xo..x\nx..ox.\n.x.x.x\n..x.x.\n", "3\no.x\noxx\no.x\n" ]
[ "YES\n....x....\n....x....\n....x....\n....x....\nxxxxoxxxx\n....x....\n....x....\n....x....\n....x....\n", "YES\n...........\n...........\n...........\n....x.x....\n...x...x...\n.....o.....\n...x...x...\n....x.x....\n...........\n...........\n...........\n", "NO\n" ]
In the first sample test the piece is a usual chess rook, and in the second sample test the piece is a usual chess knight.
[ { "input": "5\noxxxx\nx...x\nx...x\nx...x\nxxxxo", "output": "YES\nxxxxxxxxx\nx...xxxxx\nx...xxxxx\nx...xxxxx\nxxxxoxxxx\nxxxxx...x\nxxxxx...x\nxxxxx...x\nxxxxxxxxx" }, { "input": "6\n.x.x..\nx.x.x.\n.xo..x\nx..ox.\n.x.x.x\n..x.x.", "output": "YES\nxxxxxxxxxxx\nxxxxxxxxxxx\nxx.x.x..xxx\nxxx.x.x..xx\nxx.x...x.xx\nxxx..o..xxx\nxx.x...x.xx\nxx..x.x.xxx\nxxx..x.x.xx\nxxxxxxxxxxx\nxxxxxxxxxxx" }, { "input": "3\no.x\noxx\no.x", "output": "NO" }, { "input": "1\no", "output": "YES\no" }, { "input": "2\nox\n.o", "output": "YES\nxxx\n.ox\nx.x" }, { "input": "5\n.xxo.\n..oxo\nx.oxo\no..xo\noooox", "output": "NO" }, { "input": "8\n..x.xxx.\nx.x.xxxx\nxxxxxxox\nxxoxxxxx\n.xxxx.x.\nx.xxx.x.\n..x..xx.\n.xx...x.", "output": "YES\nxxxxxxxxxxxxxxx\nxxxxxxxxxxxxxxx\nxxxxxxxxxxxxxxx\nxxxxxxxxxxxxxxx\nxxxxx..x.xxx.xx\nx..x.x.x.xxxxxx\nxx.x.xxxxxxxxxx\nxxxxxxxoxxxxxxx\nxxxxx.xxxx.x.xx\nx.xxxx.x.x.x.xx\nxx.xx..x..xx.xx\nx..x..xx...x.xx\nx.xx...x.xxxxxx\nxxxxxxxxxxxxxxx\nxxxxxxxxxxxxxxx" }, { "input": "8\noxxxxxxx\nxoxxxoxx\nxx...x..\nxx...x..\nxx...x..\nxx...x..\noxxxxxxx\nxx...x..", "output": "YES\nxxxxxxxxxxxxxxx\nxxxxxxxxxxxxxxx\nxxxxxxxxxxxxxxx\nxxxxxxxxx...x..\nxxxxxxxxx...x..\nxxxxxxxxx...x..\nxxxxxxxxx...x..\nxxxxxxxoxxxxxxx\nxxxx...x.......\nxxxx...x.......\nxxxx...x.......\nxxxx...x.......\nxxxxxxxxx...x..\nxxxx...x...x..x\nxxxxxxxxx...x.." }, { "input": "8\nx.......\n.x.....x\nx.x...x.\nxx.x.x..\nxxx.x..x\n.xxx.xxx\n..oxxxx.\n.x.xoo.o", "output": "YES\nx..........xxxx\n.x...........xx\nx.x.........xxx\nxx.x.......x.xx\nxxx.x.....x..xx\n.x...x...x..xxx\n......x.x..xxxx\n.x.....o..xx.xx\nxxxxx.x.xxx.xxx\nxxxxxxxxxxxxxxx\nxxxxxxxxxxxxxxx\nxxxxxxxxxxxxxxx\nxxxxxxxxxxxxxxx\nxxxxxxxxxxxxxxx\nxxxxxxxxxxxxxxx" }, { "input": "8\n........\n........\n........\n..xxxx..\n.xx..xx.\n..xoox..\noxx..xx.\n..xxox..", "output": "YES\nxxx........xxxx\nxxx............\nxxx............\nxxx............\nxxx.........x..\nxxx...x.x...xx.\nxxx..x...x..x..\nxxx...xox...xx.\nxxxxxx...x..x..\nxxx...xxx...xxx\nxxxxxxxxxxxxxxx\nxxxxxxxxxxxxxxx\nxxxxxxxxxxxxxxx\nxxxxxxxxxxxxxxx\nxxxxxxxxxxxxxxx" }, { "input": "8\n....o...\n..o.x...\n..x..o..\n.....oo.\n.....xx.\n........\n.o...o..\n.x...x.o", "output": "YES\n....x...xxxxxxx\n..........x...x\n..........x...x\n...........x..x\n...........xx.x\n...........xx.x\n..............x\n.......o......x\nx......x.....xx\nx..........x.xx\nx..........x.xx\nx............xx\nxx...........xx\nxxx.x.......xxx\nxxx.x...x.xxxxx" }, { "input": "8\n.o....o.\n.....x..\n.....o..\n..x.....\n..o...x.\n...x..o.\n...oxx..\n....oo..", "output": "YES\nxx.........xxxx\nxx..........xxx\nx...........xxx\nx............xx\nx............xx\nx............xx\nx......x.....xx\nx......o......x\nx.............x\nx.............x\nx.............x\nx...........x.x\nx...........x.x\nx...xx...xxx..x\nx....x....xx..x" }, { "input": "10\n...o.xxx.x\n..ooxxxxxx\n.x..x.x...\nx.xxx..ox.\nxx.xoo.xxx\n.......xxx\n.x.x.xx...\nxo..xo..xx\n....x....o\n.ox.xxx..x", "output": "YES\nxxxxxxxx...x.xxx.xx\n...x.xxx..xxxxxxxxx\n..xx...x......x...x\n.x....x...xxx..xx.x\nx.xx..............x\nxx.x...........xx.x\n.......x.x.x.x....x\n.x..............xxx\nxx................x\n....x....o..xx...xx\n.xx..............xx\nxx........x......xx\nxx.x.x........x.xxx\nxxxx..........xxxxx\nxx...............xx\nxx.xx..x...x....xxx\nxxxxxx..........xxx\nxxxxxx..........xxx\nxxxxxx.xx.xxx..xxxx" }, { "input": "13\n.............\n.....o.......\n......o......\n.............\n...o........o\no............\n.............\n..o..........\n..........ooo\n.............\n..o...o.....o\n.............\n.o........o..", "output": "YES\nxx......................x\nxx.....x........x.......x\n........................x\n........................x\n.......................xx\n........................x\n........................x\n.........................\n.........................\n.........................\n.........................\n........................x\n............o............\n.........................\n.........................\n......................xxx\n.........................\n........................x\n..x...................." }, { "input": "20\nxxxxxx.xxxxxxxxxxxxx\n.xx.x.x.xx.xxxxxxxxx\nxxxxxx.xx.xx.xxx.xxx\nxx.xxxoxxx..xxxxxxxx\nxoxx.x..xx.xx.xo.xox\n.xxxxxxxxx.xxxxxxxxx\no.xxxxxoxxx..xx.oxox\nxxx.xxxx....xxx.xx.x\nxxxxxxo.xoxxoxxxxxxx\n.x..xx.xxxx...xx..xx\nxxxxxxxxxxxx..xxxxxx\nxxx.x.xxxxxxxx..xxxx\nxxxxxxx.xxoxxxx.xxx.\nx.x.xx.xxx.xxxxxxxx.\no.xxxx.xx.oxxxxx..xx\nx.oxxxxxx.x.xx.xx.x.\n.xoxx.xxxx..xx...x.x\nxxxx.x.xxxxxoxxxoxxx\noxx.xxxxx.xxxxxxxxx.\nxxxxxxoxxxxxx.xxxxxx", "output": "NO" }, { "input": "20\n.xooxo.oxx.xo..xxox.\nox.oo.xoox.xxo.xx.x.\noo..o.o.xoo.oox....o\nooo.ooxox.ooxox..oox\n.o.xx.x.ox.xo.xxoox.\nxooo.oo.xox.o.o.xxxo\noxxoox...oo.oox.xo.x\no.oxoxxx.oo.xooo..o.\no..xoxox.xo.xoooxo.x\n.oxoxxoo..o.xxoxxo..\nooxxooooox.o.x.x.ox.\noxxxx.oooooox.oxxxo.\nxoo...xoxoo.xx.x.oo.\noo..xxxox.xo.xxoxoox\nxxxoo..oo...ox.xo.o.\no..ooxoxo..xoo.xxxxo\no....oo..x.ox..oo.xo\n.x.xox.xo.o.oo.oxo.o\nooxoxoxxxox.x..xx.x.\n.xooxx..xo.xxoo.oo..", "output": "NO" } ]
2,000
4,812,800
0
16,778
540
Bad Luck Island
[ "dp", "probabilities" ]
null
null
The Bad Luck Island is inhabited by three kinds of species: *r* rocks, *s* scissors and *p* papers. At some moments of time two random individuals meet (all pairs of individuals can meet equiprobably), and if they belong to different species, then one individual kills the other one: a rock kills scissors, scissors kill paper, and paper kills a rock. Your task is to determine for each species what is the probability that this species will be the only one to inhabit this island after a long enough period of time.
The single line contains three integers *r*, *s* and *p* (1<=≀<=*r*,<=*s*,<=*p*<=≀<=100)Β β€” the original number of individuals in the species of rock, scissors and paper, respectively.
Print three space-separated real numbers: the probabilities, at which the rocks, the scissors and the paper will be the only surviving species, respectively. The answer will be considered correct if the relative or absolute error of each number doesn't exceed 10<=-<=9.
[ "2 2 2\n", "2 1 2\n", "1 1 3\n" ]
[ "0.333333333333 0.333333333333 0.333333333333\n", "0.150000000000 0.300000000000 0.550000000000\n", "0.057142857143 0.657142857143 0.285714285714\n" ]
none
[ { "input": "2 2 2", "output": "0.333333333333 0.333333333333 0.333333333333" }, { "input": "2 1 2", "output": "0.150000000000 0.300000000000 0.550000000000" }, { "input": "1 1 3", "output": "0.057142857143 0.657142857143 0.285714285714" }, { "input": "3 2 1", "output": "0.487662337662 0.072077922078 0.440259740260" }, { "input": "100 100 100", "output": "0.333333333333 0.333333333333 0.333333333333" }, { "input": "1 100 100", "output": "0.366003713151 0.633996286849 0.000000000000" }, { "input": "100 1 100", "output": "0.000000000000 0.366003713151 0.633996286849" }, { "input": "100 100 1", "output": "0.633996286849 0.000000000000 0.366003713151" }, { "input": "1 100 99", "output": "0.369700913626 0.630299086374 0.000000000000" }, { "input": "99 1 100", "output": "0.000000000000 0.369700913626 0.630299086374" }, { "input": "100 99 1", "output": "0.630299086374 0.000000000000 0.369700913626" }, { "input": "100 1 99", "output": "0.000000000000 0.362287378787 0.637712621213" }, { "input": "1 99 100", "output": "0.362287378787 0.637712621213 0.000000000000" }, { "input": "99 100 1", "output": "0.637712621213 0.000000000000 0.362287378787" }, { "input": "1 1 1", "output": "0.333333333333 0.333333333333 0.333333333333" }, { "input": "100 100 2", "output": "0.405362332237 0.000000000000 0.594637667763" }, { "input": "100 2 100", "output": "0.000000000000 0.594637667763 0.405362332237" }, { "input": "2 100 100", "output": "0.594637667763 0.405362332237 0.000000000000" }, { "input": "3 3 3", "output": "0.333333333333 0.333333333333 0.333333333333" }, { "input": "44 54 32", "output": "0.106782618787 0.143399200449 0.749818180764" }, { "input": "100 90 5", "output": "0.082441556638 0.000000001849 0.917558441513" }, { "input": "90 5 100", "output": "0.000000001849 0.917558441513 0.082441556638" }, { "input": "5 100 90", "output": "0.917558441513 0.082441556638 0.000000001849" }, { "input": "100 5 90", "output": "0.000000005097 0.850289405958 0.149710588945" }, { "input": "5 90 100", "output": "0.850289405958 0.149710588945 0.000000005097" }, { "input": "90 100 5", "output": "0.149710588945 0.000000005097 0.850289405958" }, { "input": "4 4 4", "output": "0.333333333333 0.333333333333 0.333333333333" }, { "input": "35 38 78", "output": "0.686231300287 0.301686382598 0.012082317115" }, { "input": "100 98 99", "output": "0.336951942791 0.350590779089 0.312457278120" }, { "input": "98 100 99", "output": "0.329240307786 0.316221888918 0.354537803296" }, { "input": "98 99 100", "output": "0.350590779089 0.312457278120 0.336951942791" }, { "input": "100 99 98", "output": "0.316221888918 0.354537803296 0.329240307786" }, { "input": "99 100 98", "output": "0.312457278120 0.336951942791 0.350590779089" }, { "input": "99 98 100", "output": "0.354537803296 0.329240307786 0.316221888918" }, { "input": "5 5 5", "output": "0.333333333333 0.333333333333 0.333333333333" }, { "input": "100 100 99", "output": "0.320730423530 0.341631521601 0.337638054869" }, { "input": "100 99 100", "output": "0.341631521601 0.337638054869 0.320730423530" }, { "input": "99 100 100", "output": "0.337638054869 0.320730423530 0.341631521601" }, { "input": "100 99 99", "output": "0.328877908413 0.346125932336 0.324996159251" }, { "input": "99 100 99", "output": "0.324996159251 0.328877908413 0.346125932336" }, { "input": "99 99 100", "output": "0.346125932336 0.324996159251 0.328877908413" }, { "input": "19 18 23", "output": "0.367367874268 0.441556405078 0.191075720654" }, { "input": "80 80 80", "output": "0.333333333333 0.333333333333 0.333333333333" }, { "input": "80 80 78", "output": "0.304007530347 0.347995449492 0.347997020160" }, { "input": "80 80 79", "output": "0.318598848470 0.340767700830 0.340633450700" }, { "input": "80 80 81", "output": "0.348184483745 0.325727680711 0.326087835544" }, { "input": "80 78 80", "output": "0.347995449492 0.347997020160 0.304007530347" }, { "input": "80 79 80", "output": "0.340767700830 0.340633450700 0.318598848470" }, { "input": "80 81 80", "output": "0.325727680711 0.326087835544 0.348184483745" }, { "input": "78 80 80", "output": "0.347997020160 0.304007530347 0.347995449492" }, { "input": "79 80 80", "output": "0.340633450700 0.318598848470 0.340767700830" }, { "input": "81 80 80", "output": "0.326087835544 0.348184483745 0.325727680711" }, { "input": "2 1 1", "output": "0.333333333333 0.133333333333 0.533333333333" }, { "input": "1 2 1", "output": "0.533333333333 0.333333333333 0.133333333333" }, { "input": "1 1 2", "output": "0.133333333333 0.533333333333 0.333333333333" }, { "input": "2 2 1", "output": "0.550000000000 0.150000000000 0.300000000000" }, { "input": "1 2 2", "output": "0.300000000000 0.550000000000 0.150000000000" }, { "input": "1 2 3", "output": "0.174025974026 0.692207792208 0.133766233766" }, { "input": "1 3 2", "output": "0.440259740260 0.487662337662 0.072077922078" }, { "input": "2 3 1", "output": "0.692207792208 0.133766233766 0.174025974026" }, { "input": "3 1 2", "output": "0.133766233766 0.174025974026 0.692207792208" }, { "input": "2 1 3", "output": "0.072077922078 0.440259740260 0.487662337662" }, { "input": "10 2 69", "output": "0.000000000001 0.979592460371 0.020407539628" }, { "input": "99 99 99", "output": "0.333333333333 0.333333333333 0.333333333333" }, { "input": "1 100 68", "output": "0.504856156201 0.495143843799 0.000000000000" }, { "input": "1 100 69", "output": "0.499807252268 0.500192747732 0.000000000000" }, { "input": "100 68 1", "output": "0.495143843799 0.000000000000 0.504856156201" }, { "input": "100 69 1", "output": "0.500192747732 0.000000000000 0.499807252268" }, { "input": "68 1 100", "output": "0.000000000000 0.504856156201 0.495143843799" }, { "input": "69 1 100", "output": "0.000000000000 0.499807252268 0.500192747732" }, { "input": "40 100 50", "output": "0.504950275130 0.003137391318 0.491912333552" }, { "input": "41 100 50", "output": "0.471692521594 0.003711367492 0.524596110914" }, { "input": "100 50 40", "output": "0.003137391318 0.491912333552 0.504950275130" }, { "input": "100 50 41", "output": "0.003711367492 0.524596110914 0.471692521594" }, { "input": "50 40 100", "output": "0.491912333552 0.504950275130 0.003137391318" }, { "input": "50 41 100", "output": "0.524596110914 0.471692521594 0.003711367492" }, { "input": "4 3 2", "output": "0.380033049657 0.128974183711 0.490992766632" }, { "input": "3 3 2", "output": "0.448942486085 0.194141929499 0.356915584416" }, { "input": "3 2 4", "output": "0.128974183711 0.490992766632 0.380033049657" }, { "input": "3 2 3", "output": "0.194141929499 0.356915584416 0.448942486085" }, { "input": "2 4 3", "output": "0.490992766632 0.380033049657 0.128974183711" }, { "input": "2 3 3", "output": "0.356915584416 0.448942486085 0.194141929499" }, { "input": "94 62 53", "output": "0.032741579903 0.688734095294 0.278524324802" }, { "input": "92 42 99", "output": "0.156634527800 0.841252178878 0.002113293322" }, { "input": "57 88 2", "output": "0.628039075774 0.000000000036 0.371960924190" }, { "input": "49 85 47", "output": "0.185241468442 0.036259808833 0.778498722726" }, { "input": "48 16 81", "output": "0.009800033922 0.990059771027 0.000140195051" }, { "input": "39 96 87", "output": "0.856896275913 0.001822013551 0.141281710536" }, { "input": "100 91 51", "output": "0.008569274339 0.316910121953 0.674520603708" }, { "input": "90 92 97", "output": "0.412664975931 0.267301641052 0.320033383016" }, { "input": "86 25 84", "output": "0.016023421282 0.983316299665 0.000660279053" }, { "input": "80 1 89", "output": "0.000000000000 0.404923676688 0.595076323312" }, { "input": "67 95 88", "output": "0.419687207048 0.074718763764 0.505594029188" }, { "input": "50 93 89", "output": "0.693218455167 0.011706551519 0.295074993314" }, { "input": "27 71 76", "output": "0.954421631610 0.002613537210 0.042964831180" }, { "input": "18 47 22", "output": "0.741659614574 0.008276779449 0.250063605977" } ]
1,107
81,510,400
3
16,808
0
none
[ "none" ]
null
null
Manao is taking part in a quiz. The quiz consists of *n* consecutive questions. A correct answer gives one point to the player. The game also has a counter of consecutive correct answers. When the player answers a question correctly, the number on this counter increases by 1. If the player answers a question incorrectly, the counter is reset, that is, the number on it reduces to 0. If after an answer the counter reaches the number *k*, then it is reset, and the player's score is doubled. Note that in this case, first 1 point is added to the player's score, and then the total score is doubled. At the beginning of the game, both the player's score and the counter of consecutive correct answers are set to zero. Manao remembers that he has answered exactly *m* questions correctly. But he does not remember the order in which the questions came. He's trying to figure out what his minimum score may be. Help him and compute the remainder of the corresponding number after division by 1000000009 (109<=+<=9).
The single line contains three space-separated integers *n*, *m* and *k* (2<=≀<=*k*<=≀<=*n*<=≀<=109;Β 0<=≀<=*m*<=≀<=*n*).
Print a single integer β€” the remainder from division of Manao's minimum possible score in the quiz by 1000000009 (109<=+<=9).
[ "5 3 2\n", "5 4 2\n" ]
[ "3\n", "6\n" ]
Sample 1. Manao answered 3 questions out of 5, and his score would double for each two consecutive correct answers. If Manao had answered the first, third and fifth questions, he would have scored as much as 3 points. Sample 2. Now Manao answered 4 questions. The minimum possible score is obtained when the only wrong answer is to the question 4. Also note that you are asked to minimize the score and not the remainder of the score modulo 1000000009. For example, if Manao could obtain either 2000000000 or 2000000020 points, the answer is 2000000000Β *mod*Β 1000000009, even though 2000000020Β *mod*Β 1000000009 is a smaller number.
[ { "input": "5 3 2", "output": "3" }, { "input": "5 4 2", "output": "6" }, { "input": "300 300 3", "output": "17717644" }, { "input": "300 282 7", "output": "234881124" }, { "input": "1000000000 1000000000 1000000000", "output": "999999991" }, { "input": "1000000000 800000000 2", "output": "785468433" }, { "input": "2 0 2", "output": "0" }, { "input": "2 1 2", "output": "1" }, { "input": "2 2 2", "output": "4" }, { "input": "3 2 2", "output": "2" }, { "input": "3 3 2", "output": "5" }, { "input": "10 7 3", "output": "7" }, { "input": "10 8 3", "output": "11" }, { "input": "10 8 5", "output": "8" }, { "input": "10 9 5", "output": "14" }, { "input": "972 100 2", "output": "100" }, { "input": "972 600 2", "output": "857317034" }, { "input": "972 900 2", "output": "129834751" }, { "input": "972 900 4", "output": "473803848" }, { "input": "972 900 5", "output": "682661588" }, { "input": "12345 11292 3", "output": "307935747" }, { "input": "120009 70955 2", "output": "938631761" }, { "input": "120009 100955 2", "output": "682499671" }, { "input": "291527 253014 7", "output": "572614130" }, { "input": "300294 299002 188", "output": "435910952" }, { "input": "23888888 508125 3", "output": "508125" }, { "input": "23888888 16789012 2", "output": "573681250" }, { "input": "23888888 19928497 4", "output": "365378266" }, { "input": "23888888 19928497 5", "output": "541851325" }, { "input": "23888888 19928497 812", "output": "19928497" }, { "input": "23888888 23862367 812", "output": "648068609" }, { "input": "87413058 85571952 11", "output": "996453351" }, { "input": "87413058 85571952 12", "output": "903327586" }, { "input": "87413058 85571952 25", "output": "424641940" }, { "input": "512871295 482216845 2", "output": "565667832" }, { "input": "512871295 482216845 3", "output": "446175557" }, { "input": "512871295 508216845 90", "output": "332476079" }, { "input": "512871295 512816845 99712", "output": "512816845" }, { "input": "512871295 512870845 99712", "output": "944454424" }, { "input": "512871295 512870845 216955", "output": "28619469" }, { "input": "512871295 512871195 2000000", "output": "559353433" }, { "input": "512871295 512871295 12345678", "output": "423625559" }, { "input": "778562195 708921647 4", "output": "208921643" }, { "input": "500000000 500000000 4", "output": "1000000005" }, { "input": "375000000 375000000 3", "output": "1000000006" }, { "input": "250000000 250000000 2", "output": "1000000007" }, { "input": "300000000 300000000 12561295", "output": "543525658" }, { "input": "300000000 300000000 212561295", "output": "512561295" }, { "input": "300000000 300000000 299999999", "output": "599999999" }, { "input": "500000002 500000002 2", "output": "1000000007" }, { "input": "625000001 625000000 5", "output": "500000002" }, { "input": "875000005 875000000 7", "output": "531250026" }, { "input": "1000000000 1000000000 8", "output": "1000000001" }, { "input": "901024556 900000000 6", "output": "175578776" }, { "input": "901024556 900000000 91", "output": "771418556" }, { "input": "901024556 900000000 92", "output": "177675186" }, { "input": "901024556 900000000 888", "output": "900000000" }, { "input": "901024556 901000000 1000", "output": "443969514" }, { "input": "901024556 901000000 1013", "output": "840398451" }, { "input": "999998212 910275020 25", "output": "910275020" }, { "input": "999998212 999998020 1072520", "output": "314152037" }, { "input": "999998212 999998020 381072520", "output": "999998020" }, { "input": "999998212 999998210 381072520", "output": "999998210" }, { "input": "999998212 999998211 499998210", "output": "499996412" }, { "input": "1000000000 1000000000 1000000000", "output": "999999991" }, { "input": "1000000000 1000000000 772625255", "output": "772625246" }, { "input": "1000000000 999999904 225255", "output": "940027552" }, { "input": "1000000000 999998304 22255", "output": "969969792" }, { "input": "1000000000 999998304 7355", "output": "756187119" }, { "input": "1000000000 999998304 755", "output": "684247947" }, { "input": "1000000000 999998304 256", "output": "401008799" }, { "input": "1000000000 1000000000 2", "output": "750000003" }, { "input": "1000000000 1 999999998", "output": "1" } ]
218
0
3
16,837
440
One-Based Arithmetic
[ "brute force", "dfs and similar", "divide and conquer" ]
null
null
Prof. Vasechkin wants to represent positive integer *n* as a sum of addends, where each addends is an integer number containing only 1s. For example, he can represent 121 as 121=111+11+–1. Help him to find the least number of digits 1 in such sum.
The first line of the input contains integer *n* (1<=≀<=*n*<=&lt;<=1015).
Print expected minimal number of digits 1.
[ "121\n" ]
[ "6\n" ]
none
[ { "input": "121", "output": "6" }, { "input": "10", "output": "3" }, { "input": "72", "output": "15" }, { "input": "1", "output": "1" }, { "input": "2", "output": "2" }, { "input": "3", "output": "3" }, { "input": "4", "output": "4" }, { "input": "5", "output": "5" }, { "input": "6", "output": "6" }, { "input": "7", "output": "6" }, { "input": "11", "output": "2" }, { "input": "12", "output": "3" }, { "input": "2038946593", "output": "145" }, { "input": "81924761239462", "output": "321" }, { "input": "973546235465729", "output": "263" }, { "input": "999999999999999", "output": "32" }, { "input": "21", "output": "5" }, { "input": "79", "output": "10" }, { "input": "33", "output": "6" }, { "input": "185", "output": "16" }, { "input": "513", "output": "25" }, { "input": "634", "output": "22" }, { "input": "5300", "output": "32" }, { "input": "3724", "output": "34" }, { "input": "2148", "output": "21" }, { "input": "82415", "output": "53" }, { "input": "35839", "output": "45" }, { "input": "79263", "output": "45" }, { "input": "274634", "output": "62" }, { "input": "690762", "output": "65" }, { "input": "374186", "output": "65" }, { "input": "2673749", "output": "81" }, { "input": "5789877", "output": "61" }, { "input": "1873301", "output": "59" }, { "input": "30272863", "output": "118" }, { "input": "33388991", "output": "57" }, { "input": "11472415", "output": "72" }, { "input": "345871978", "output": "95" }, { "input": "528988106", "output": "118" }, { "input": "302038826", "output": "128" }, { "input": "1460626450", "output": "152" }, { "input": "3933677170", "output": "159" }, { "input": "6816793298", "output": "153" }, { "input": "75551192860", "output": "151" }, { "input": "28729276284", "output": "212" }, { "input": "67612392412", "output": "178" }, { "input": "532346791975", "output": "158" }, { "input": "575524875399", "output": "189" }, { "input": "614407991527", "output": "236" }, { "input": "2835997166898", "output": "275" }, { "input": "1079175250322", "output": "188" }, { "input": "8322353333746", "output": "169" }, { "input": "26602792766013", "output": "288" }, { "input": "42845970849437", "output": "325" }, { "input": "59089148932861", "output": "265" }, { "input": "842369588365127", "output": "330" }, { "input": "768617061415848", "output": "390" }, { "input": "694855944531976", "output": "348" }, { "input": "898453513288965", "output": "248" }, { "input": "98596326741327", "output": "260" }, { "input": "59191919191919", "output": "342" } ]
124
0
0
16,861
660
Different Subsets For All Tuples
[ "combinatorics", "math" ]
null
null
For a sequence *a* of *n* integers between 1 and *m*, inclusive, denote *f*(*a*) as the number of distinct subsequences of *a* (including the empty subsequence). You are given two positive integers *n* and *m*. Let *S* be the set of all sequences of length *n* consisting of numbers from 1 to *m*. Compute the sum *f*(*a*) over all *a* in *S* modulo 109<=+<=7.
The only line contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=106) β€” the number of elements in arrays and the upper bound for elements.
Print the only integer *c* β€” the desired sum modulo 109<=+<=7.
[ "1 3\n", "2 2\n", "3 3\n" ]
[ "6\n", "14\n", "174\n" ]
none
[ { "input": "1 3", "output": "6" }, { "input": "2 2", "output": "14" }, { "input": "3 3", "output": "174" }, { "input": "1 1000000", "output": "2000000" }, { "input": "1000000 1", "output": "1000001" }, { "input": "500 500", "output": "383255233" }, { "input": "1000000 1000000", "output": "247171672" } ]
46
0
0
16,872
685
Kay and Snowflake
[ "data structures", "dfs and similar", "dp", "trees" ]
null
null
After the piece of a devilish mirror hit the Kay's eye, he is no longer interested in the beauty of the roses. Now he likes to watch snowflakes. Once upon a time, he found a huge snowflake that has a form of the tree (connected acyclic graph) consisting of *n* nodes. The root of tree has index 1. Kay is very interested in the structure of this tree. After doing some research he formed *q* queries he is interested in. The *i*-th query asks to find a centroid of the subtree of the node *v**i*. Your goal is to answer all queries. Subtree of a node is a part of tree consisting of this node and all it's descendants (direct or not). In other words, subtree of node *v* is formed by nodes *u*, such that node *v* is present on the path from *u* to root. Centroid of a tree (or a subtree) is a node, such that if we erase it from the tree, the maximum size of the connected component will be at least two times smaller than the size of the initial tree (or a subtree).
The first line of the input contains two integers *n* and *q* (2<=≀<=*n*<=≀<=300<=000, 1<=≀<=*q*<=≀<=300<=000)Β β€” the size of the initial tree and the number of queries respectively. The second line contains *n*<=-<=1 integer *p*2,<=*p*3,<=...,<=*p**n* (1<=≀<=*p**i*<=≀<=*n*)Β β€” the indices of the parents of the nodes from 2 to *n*. Node 1 is a root of the tree. It's guaranteed that *p**i* define a correct tree. Each of the following *q* lines contain a single integer *v**i* (1<=≀<=*v**i*<=≀<=*n*)Β β€” the index of the node, that define the subtree, for which we want to find a centroid.
For each query print the index of a centroid of the corresponding subtree. If there are many suitable nodes, print any of them. It's guaranteed, that each subtree has at least one centroid.
[ "7 4\n1 1 3 3 5 3\n1\n2\n3\n5\n" ]
[ "3\n2\n3\n6\n" ]
The first query asks for a centroid of the whole treeΒ β€” this is node 3. If we delete node 3 the tree will split in four components, two of size 1 and two of size 2. The subtree of the second node consists of this node only, so the answer is 2. Node 3 is centroid of its own subtree. The centroids of the subtree of the node 5 are nodes 5 and 6Β β€” both answers are considered correct.
[ { "input": "7 4\n1 1 3 3 5 3\n1\n2\n3\n5", "output": "3\n2\n3\n6" }, { "input": "2 2\n1\n1\n2", "output": "2\n2" } ]
872
70,656,000
0
16,883
460
Present
[ "binary search", "data structures", "greedy" ]
null
null
Little beaver is a beginner programmer, so informatics is his favorite subject. Soon his informatics teacher is going to have a birthday and the beaver has decided to prepare a present for her. He planted *n* flowers in a row on his windowsill and started waiting for them to grow. However, after some time the beaver noticed that the flowers stopped growing. The beaver thinks it is bad manners to present little flowers. So he decided to come up with some solutions. There are *m* days left to the birthday. The height of the *i*-th flower (assume that the flowers in the row are numbered from 1 to *n* from left to right) is equal to *a**i* at the moment. At each of the remaining *m* days the beaver can take a special watering and water *w* contiguous flowers (he can do that only once at a day). At that each watered flower grows by one height unit on that day. The beaver wants the height of the smallest flower be as large as possible in the end. What maximum height of the smallest flower can he get?
The first line contains space-separated integers *n*, *m* and *w* (1<=≀<=*w*<=≀<=*n*<=≀<=105;Β 1<=≀<=*m*<=≀<=105). The second line contains space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109).
Print a single integer β€” the maximum final height of the smallest flower.
[ "6 2 3\n2 2 2 2 1 1\n", "2 5 1\n5 8\n" ]
[ "2\n", "9\n" ]
In the first sample beaver can water the last 3 flowers at the first day. On the next day he may not to water flowers at all. In the end he will get the following heights: [2, 2, 2, 3, 2, 2]. The smallest flower has height equal to 2. It's impossible to get height 3 in this test.
[ { "input": "6 2 3\n2 2 2 2 1 1", "output": "2" }, { "input": "2 5 1\n5 8", "output": "9" }, { "input": "1 1 1\n1", "output": "2" }, { "input": "3 2 3\n999999998 999999998 999999998", "output": "1000000000" }, { "input": "10 8 3\n499 498 497 497 497 497 497 497 498 499", "output": "500" }, { "input": "11 18 8\n4996 4993 4988 4982 4982 4982 4982 4982 4986 4989 4994", "output": "5000" }, { "input": "1 100000 1\n1000000000", "output": "1000100000" }, { "input": "4 100 3\n1 100000 100000 1", "output": "51" } ]
109
20,992,000
3
16,938
846
Chemistry in Berland
[ "dfs and similar", "greedy", "trees" ]
null
null
Igor is a post-graduate student of chemistry faculty in Berland State University (BerSU). He needs to conduct a complicated experiment to write his thesis, but laboratory of BerSU doesn't contain all the materials required for this experiment. Fortunately, chemical laws allow material transformations (yes, chemistry in Berland differs from ours). But the rules of transformation are a bit strange. Berland chemists are aware of *n* materials, numbered in the order they were discovered. Each material can be transformed into some other material (or vice versa). Formally, for each *i* (2<=≀<=*i*<=≀<=*n*) there exist two numbers *x**i* and *k**i* that denote a possible transformation: *k**i* kilograms of material *x**i* can be transformed into 1 kilogram of material *i*, and 1 kilogram of material *i* can be transformed into 1 kilogram of material *x**i*. Chemical processing equipment in BerSU allows only such transformation that the amount of resulting material is always an integer number of kilograms. For each *i* (1<=≀<=*i*<=≀<=*n*) Igor knows that the experiment requires *a**i* kilograms of material *i*, and the laboratory contains *b**i* kilograms of this material. Is it possible to conduct an experiment after transforming some materials (or none)?
The first line contains one integer number *n* (1<=≀<=*n*<=≀<=105) β€” the number of materials discovered by Berland chemists. The second line contains *n* integer numbers *b*1,<=*b*2... *b**n* (1<=≀<=*b**i*<=≀<=1012) β€” supplies of BerSU laboratory. The third line contains *n* integer numbers *a*1,<=*a*2... *a**n* (1<=≀<=*a**i*<=≀<=1012) β€” the amounts required for the experiment. Then *n*<=-<=1 lines follow. *j*-th of them contains two numbers *x**j*<=+<=1 and *k**j*<=+<=1 that denote transformation of (*j*<=+<=1)-th material (1<=≀<=*x**j*<=+<=1<=≀<=*j*,<=1<=≀<=*k**j*<=+<=1<=≀<=109).
Print YES if it is possible to conduct an experiment. Otherwise print NO.
[ "3\n1 2 3\n3 2 1\n1 1\n1 1\n", "3\n3 2 1\n1 2 3\n1 1\n1 2\n" ]
[ "YES\n", "NO\n" ]
none
[ { "input": "3\n1 2 3\n3 2 1\n1 1\n1 1", "output": "YES" }, { "input": "3\n3 2 1\n1 2 3\n1 1\n1 2", "output": "NO" }, { "input": "5\n2 1 1 2 3\n1 2 2 2 1\n1 2\n1 3\n2 4\n1 4", "output": "NO" }, { "input": "10\n2 8 6 1 2 7 6 9 2 8\n4 9 4 3 5 2 9 3 7 3\n1 8\n2 8\n3 8\n4 10\n5 1\n6 4\n7 3\n8 10\n9 2", "output": "YES" }, { "input": "5\n27468 7465 74275 40573 40155\n112071 76270 244461 264202 132397\n1 777133331\n2 107454154\n3 652330694\n4 792720519", "output": "NO" }, { "input": "5\n78188 56310 79021 70050 65217\n115040 5149 128449 98357 36580\n1 451393770\n2 574046602\n3 590130784\n4 112514248", "output": "NO" }, { "input": "7\n1 1 1 1 1 1 1\n1 3000000000 3000000000 3000000000 1000000000 1000000000 1000000000\n1 1000000000\n1 1000000000\n1 1000000000\n1 1000000000\n1 1000000000\n1 1000000000", "output": "NO" }, { "input": "11\n1 1 1 1 1 1 1 1 1 1 1\n1 1000000001 1000000001 1000000001 1000000001 1000000001 1000000001 1000000001 1000000001 1000000001 1000000001\n1 1000000000\n1 1000000000\n1 1000000000\n1 1000000000\n1 1000000000\n1 1000000000\n1 1000000000\n1 1000000000\n1 1000000000\n1 1000000000", "output": "NO" } ]
1,419
268,390,400
0
16,954
201
Fragile Bridges
[ "dp" ]
null
null
You are playing a video game and you have just reached the bonus level, where the only possible goal is to score as many points as possible. Being a perfectionist, you've decided that you won't leave this level until you've gained the maximum possible number of points there. The bonus level consists of *n* small platforms placed in a line and numbered from 1 to *n* from left to right and (*n*<=-<=1) bridges connecting adjacent platforms. The bridges between the platforms are very fragile, and for each bridge the number of times one can pass this bridge from one of its ends to the other before it collapses forever is known in advance. The player's actions are as follows. First, he selects one of the platforms to be the starting position for his hero. After that the player can freely move the hero across the platforms moving by the undestroyed bridges. As soon as the hero finds himself on a platform with no undestroyed bridge attached to it, the level is automatically ended. The number of points scored by the player at the end of the level is calculated as the number of transitions made by the hero between the platforms. Note that if the hero started moving by a certain bridge, he has to continue moving in the same direction until he is on a platform. Find how many points you need to score to be sure that nobody will beat your record, and move to the next level with a quiet heart.
The first line contains a single integer *n* (2<=≀<=*n*<=≀<=105) β€” the number of platforms on the bonus level. The second line contains (*n*<=-<=1) integers *a**i* (1<=≀<=*a**i*<=≀<=109, 1<=≀<=*i*<=&lt;<=*n*) β€” the number of transitions from one end to the other that the bridge between platforms *i* and *i*<=+<=1 can bear.
Print a single integer β€” the maximum number of points a player can get on the bonus level. 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.
[ "5\n2 1 2 1\n" ]
[ "5\n" ]
One possibility of getting 5 points in the sample is starting from platform 3 and consequently moving to platforms 4, 3, 2, 1 and 2. After that the only undestroyed bridge is the bridge between platforms 4 and 5, but this bridge is too far from platform 2 where the hero is located now.
[ { "input": "5\n2 1 2 1", "output": "5" }, { "input": "3\n2 2", "output": "4" }, { "input": "7\n1 2 3 4 5 6", "output": "19" }, { "input": "12\n10 14 8 6 2 7 5 9 9 4 8", "output": "82" }, { "input": "13\n1 1 1 1 1 1 1 1 1 1 1 1", "output": "12" }, { "input": "9\n9 2 8 7 1 4 10 9", "output": "48" }, { "input": "30\n49 48 100 3 7 23 59 15 3 10 16 25 85 71 25 49 78 7 85 72 32 66 95 33 93 81 52 41 23", "output": "1338" }, { "input": "2\n1", "output": "1" }, { "input": "2\n2", "output": "2" }, { "input": "2\n999999999", "output": "999999999" }, { "input": "2\n1000000000", "output": "1000000000" }, { "input": "3\n1 1", "output": "2" }, { "input": "3\n1 2", "output": "3" }, { "input": "3\n2 1", "output": "3" }, { "input": "5\n1 1 6 6", "output": "14" }, { "input": "5\n1 2 6 6", "output": "15" }, { "input": "8\n2 1 2 1 2 1 2", "output": "9" }, { "input": "16\n200 200 200 1 200 3 9 3 9 3 200 1 200 200 200", "output": "1627" }, { "input": "16\n200 1 200 200 200 3 9 3 9 3 200 200 200 1 200", "output": "1623" }, { "input": "9\n1 2 3 4 5 5 5 5", "output": "28" } ]
0
0
-1
16,979
981
Magic multisets
[ "data structures" ]
null
null
In the School of Magic in Dirtpolis a lot of interesting objects are studied on Computer Science lessons. Consider, for example, the magic multiset. If you try to add an integer to it that is already presented in the multiset, each element in the multiset duplicates. For example, if you try to add the integer $2$ to the multiset $\{1, 2, 3, 3\}$, you will get $\{1, 1, 2, 2, 3, 3, 3, 3\}$. If you try to add an integer that is not presented in the multiset, it is simply added to it. For example, if you try to add the integer $4$ to the multiset $\{1, 2, 3, 3\}$, you will get $\{1, 2, 3, 3, 4\}$. Also consider an array of $n$ initially empty magic multisets, enumerated from $1$ to $n$. You are to answer $q$ queries of the form "add an integer $x$ to all multisets with indices $l, l + 1, \ldots, r$" and "compute the sum of sizes of multisets with indices $l, l + 1, \ldots, r$". The answers for the second type queries can be large, so print the answers modulo $998244353$.
The first line contains two integers $n$ and $q$ ($1 \leq n, q \leq 2 \cdot 10^{5}$)Β β€” the number of magic multisets in the array and the number of queries, respectively. The next $q$ lines describe queries, one per line. Each line starts with an integer $t$ ($1 \leq t \leq 2$)Β β€” the type of the query. If $t$ equals $1$, it is followed by three integers $l$, $r$, $x$ ($1 \leq l \leq r \leq n$, $1 \leq x \leq n$) meaning that you should add $x$ to all multisets with indices from $l$ to $r$ inclusive. If $t$ equals $2$, it is followed by two integers $l$, $r$ ($1 \leq l \leq r \leq n$) meaning that you should compute the sum of sizes of all multisets with indices from $l$ to $r$ inclusive.
For each query of the second type print the sum of sizes of multisets on the given segment. The answers can be large, so print them modulo $998244353$.
[ "4 4\n1 1 2 1\n1 1 2 2\n1 1 4 1\n2 1 4\n", "3 7\n1 1 1 3\n1 1 1 3\n1 1 1 2\n1 1 1 1\n2 1 1\n1 1 1 2\n2 1 1\n" ]
[ "10\n", "4\n8\n" ]
In the first example after the first two queries the multisets are equal to $[\{1, 2\},\{1, 2\},\{\},\{\}]$, after the third query they are equal to $[\{1, 1, 2, 2\},\{1, 1, 2, 2\},\{1\},\{1\}]$. In the second example the first multiset evolves as follows: $\{\} \to \{3\} \to \{3, 3\} \to \{2, 3, 3\} \to \{1, 2, 3, 3\} \to \{1, 1, 2, 2, 3, 3, 3, 3\}$.
[]
1,809
268,390,400
0
17,040
134
Pairs of Numbers
[ "brute force", "dfs and similar", "math", "number theory" ]
null
null
Let's assume that we have a pair of numbers (*a*,<=*b*). We can get a new pair (*a*<=+<=*b*,<=*b*) or (*a*,<=*a*<=+<=*b*) from the given pair in a single step. Let the initial pair of numbers be (1,1). Your task is to find number *k*, that is, the least number of steps needed to transform (1,1) into the pair where at least one number equals *n*.
The input contains the only integer *n* (1<=≀<=*n*<=≀<=106).
Print the only integer *k*.
[ "5\n", "1\n" ]
[ "3\n", "0\n" ]
The pair (1,1) can be transformed into a pair containing 5 in three moves: (1,1)  →  (1,2)  →  (3,2)  →  (5,2).
[ { "input": "5", "output": "3" }, { "input": "1", "output": "0" }, { "input": "2", "output": "1" }, { "input": "3", "output": "2" }, { "input": "4", "output": "3" }, { "input": "10", "output": "5" }, { "input": "1009", "output": "15" }, { "input": "2009", "output": "17" }, { "input": "7009", "output": "19" }, { "input": "9009", "output": "20" }, { "input": "19009", "output": "21" }, { "input": "29009", "output": "22" }, { "input": "12434", "output": "21" }, { "input": "342342", "output": "28" }, { "input": "342235", "output": "28" }, { "input": "362235", "output": "28" }, { "input": "762235", "output": "30" }, { "input": "878235", "output": "30" }, { "input": "978235", "output": "30" }, { "input": "1000000", "output": "30" }, { "input": "6", "output": "5" }, { "input": "10000", "output": "20" }, { "input": "999999", "output": "30" }, { "input": "524287", "output": "29" }, { "input": "777777", "output": "30" }, { "input": "123756", "output": "26" }, { "input": "976438", "output": "30" }, { "input": "434563", "output": "28" }, { "input": "345634", "output": "28" }, { "input": "65457", "output": "24" }, { "input": "123456", "output": "26" }, { "input": "999997", "output": "30" }, { "input": "98989", "output": "25" }, { "input": "8", "output": "4" }, { "input": "123455", "output": "26" }, { "input": "990001", "output": "30" }, { "input": "123141", "output": "26" }, { "input": "998", "output": "16" }, { "input": "453422", "output": "28" }, { "input": "623423", "output": "29" }, { "input": "89", "output": "9" }, { "input": "24234", "output": "23" }, { "input": "999879", "output": "30" }, { "input": "345612", "output": "28" }, { "input": "998756", "output": "30" }, { "input": "999989", "output": "30" }, { "input": "999998", "output": "30" }, { "input": "999912", "output": "30" }, { "input": "100000", "output": "25" } ]
1,000
4,608,000
0
17,060
15
Laser
[ "math" ]
B. Laser
1
64
Petya is the most responsible worker in the Research Institute. So he was asked to make a very important experiment: to melt the chocolate bar with a new laser device. The device consists of a rectangular field of *n*<=Γ—<=*m* cells and a robotic arm. Each cell of the field is a 1<=Γ—<=1 square. The robotic arm has two lasers pointed at the field perpendicularly to its surface. At any one time lasers are pointed at the centres of some two cells. Since the lasers are on the robotic hand, their movements are synchronized β€” if you move one of the lasers by a vector, another one moves by the same vector. The following facts about the experiment are known: - initially the whole field is covered with a chocolate bar of the size *n*<=Γ—<=*m*, both lasers are located above the field and are active; - the chocolate melts within one cell of the field at which the laser is pointed; - all moves of the robotic arm should be parallel to the sides of the field, after each move the lasers should be pointed at the centres of some two cells; - at any one time both lasers should be pointed at the field. Petya doesn't want to become a second Gordon Freeman. You are given *n*, *m* and the cells (*x*1,<=*y*1) and (*x*2,<=*y*2), where the lasers are initially pointed at (*x**i* is a column number, *y**i* is a row number). Rows are numbered from 1 to *m* from top to bottom and columns are numbered from 1 to *n* from left to right. You are to find the amount of cells of the field on which the chocolate can't be melted in the given conditions.
The first line contains one integer number *t* (1<=≀<=*t*<=≀<=10000) β€” the number of test sets. Each of the following *t* lines describes one test set. Each line contains integer numbers *n*, *m*, *x*1, *y*1, *x*2, *y*2, separated by a space (2<=≀<=*n*,<=*m*<=≀<=109, 1<=≀<=*x*1,<=*x*2<=≀<=*n*, 1<=≀<=*y*1,<=*y*2<=≀<=*m*). Cells (*x*1,<=*y*1) and (*x*2,<=*y*2) are distinct.
Each of the *t* lines of the output should contain the answer to the corresponding input test set.
[ "2\n4 4 1 1 3 3\n4 3 1 1 2 2\n" ]
[ "8\n2\n" ]
none
[ { "input": "2\n4 4 1 1 3 3\n4 3 1 1 2 2", "output": "8\n2" }, { "input": "1\n2 2 1 2 2 1", "output": "2" }, { "input": "1\n2 2 1 2 2 1", "output": "2" }, { "input": "1\n3 3 3 2 1 1", "output": "5" }, { "input": "1\n3 4 1 1 1 2", "output": "0" }, { "input": "1\n4 3 3 1 4 1", "output": "0" }, { "input": "1\n3 5 2 4 3 5", "output": "2" }, { "input": "1\n4 5 2 2 4 2", "output": "0" }, { "input": "1\n2 5 1 5 2 2", "output": "6" }, { "input": "1\n2 6 2 6 2 3", "output": "0" }, { "input": "1\n3 6 3 5 2 4", "output": "2" }, { "input": "1\n4 6 2 1 2 3", "output": "0" }, { "input": "1\n5 6 3 4 4 2", "output": "4" }, { "input": "1\n7 3 6 2 5 2", "output": "0" }, { "input": "1\n8 2 6 1 7 2", "output": "2" }, { "input": "1\n9 6 6 5 3 1", "output": "30" }, { "input": "20\n100 200 100 1 100 100\n100 200 1 100 100 100\n2 2 1 1 2 2\n100 100 50 50 1 1\n10 10 5 5 1 1\n100 100 99 1 1 99\n100 100 1 99 99 1\n100 100 1 10 10 1\n100 100 1 1 10 10\n9 6 1 3 3 1\n1000000000 1000000000 1 1 1000000000 1000000000\n9 4 1 4 4 1\n6 4 1 1 5 4\n6 2 1 1 5 2\n8 2 1 1 5 2\n10 2 1 1 5 2\n10 2 1 1 3 2\n4 3 1 1 2 2\n3 3 1 1 2 2\n3 3 1 1 2 1", "output": "0\n19600\n2\n4802\n32\n9992\n9992\n162\n162\n8\n999999999999999998\n24\n20\n8\n8\n8\n4\n2\n2\n0" } ]
0
0
-1
17,070
83
Numbers
[ "dp", "math", "number theory" ]
D. Numbers
3
256
One quite ordinary day Valera went to school (there's nowhere else he should go on a week day). In a maths lesson his favorite teacher Ms. Evans told students about divisors. Despite the fact that Valera loved math, he didn't find this particular topic interesting. Even more, it seemed so boring that he fell asleep in the middle of a lesson. And only a loud ringing of a school bell could interrupt his sweet dream. Of course, the valuable material and the teacher's explanations were lost. However, Valera will one way or another have to do the homework. As he does not know the new material absolutely, he cannot do the job himself. That's why he asked you to help. You're his best friend after all, you just cannot refuse to help. Valera's home task has only one problem, which, though formulated in a very simple way, has not a trivial solution. Its statement looks as follows: if we consider all positive integers in the interval [*a*;*b*] then it is required to count the amount of such numbers in this interval that their smallest divisor will be a certain integer *k* (you do not have to consider divisor equal to one). In other words, you should count the amount of such numbers from the interval [*a*;*b*], that are not divisible by any number between 2 and *k*<=-<=1 and yet are divisible by *k*.
The first and only line contains three positive integers *a*, *b*, *k* (1<=≀<=*a*<=≀<=*b*<=≀<=2Β·109,<=2<=≀<=*k*<=≀<=2Β·109).
Print on a single line the answer to the given problem.
[ "1 10 2\n", "12 23 3\n", "6 19 5\n" ]
[ "5\n", "2\n", "0\n" ]
Comments to the samples from the statement: In the first sample the answer is numbers 2, 4, 6, 8, 10. In the second one β€” 15, 21 In the third one there are no such numbers.
[ { "input": "1 10 2", "output": "5" }, { "input": "12 23 3", "output": "2" }, { "input": "6 19 5", "output": "0" }, { "input": "1 80 7", "output": "3" }, { "input": "100 1000 1009", "output": "0" }, { "input": "11 124 11", "output": "2" }, { "input": "1000 10000 19", "output": "86" }, { "input": "2020 6300 29", "output": "28" }, { "input": "213 1758 41", "output": "1" }, { "input": "201 522 233", "output": "1" }, { "input": "97 10403 101", "output": "3" }, { "input": "1 340431 3", "output": "56739" }, { "input": "3500 100000 1009", "output": "0" }, { "input": "300 300000 5003", "output": "1" }, { "input": "100000 100000 5", "output": "0" }, { "input": "300 700 41", "output": "0" }, { "input": "7000 43000 61", "output": "96" }, { "input": "12 20000000 11", "output": "415584" }, { "input": "35000 100000000 50021", "output": "1" }, { "input": "1 20000000 3", "output": "3333333" }, { "input": "500000 8000000 4001", "output": "0" }, { "input": "2 1000 4", "output": "0" }, { "input": "1 50341999 503", "output": "9504" }, { "input": "50 60000000 5", "output": "3999997" }, { "input": "1009 1009 1009", "output": "1" }, { "input": "4500 400000 30011", "output": "1" }, { "input": "40 200000000 31", "output": "1019019" }, { "input": "50 600000000 2", "output": "299999976" }, { "input": "12000 700000000 97", "output": "877658" }, { "input": "30000 400000000 500009", "output": "1" }, { "input": "800000 90000000 13000027", "output": "1" }, { "input": "99999 99999999 4001", "output": "2212" }, { "input": "300303 600000 503", "output": "87" }, { "input": "5002230 10002230 233", "output": "2079" }, { "input": "18800310 20000000 53", "output": "3135" }, { "input": "200000000 2000000000 1800000011", "output": "1" }, { "input": "1008055011 1500050000 41", "output": "1784635" }, { "input": "2000000000 2000000000 2", "output": "1" }, { "input": "19999999 2000000000 11", "output": "41142857" }, { "input": "800201 90043000 307", "output": "26902" }, { "input": "599999 1000000000 653", "output": "124742" }, { "input": "1 1000000000 10", "output": "0" }, { "input": "41939949 2000000000 127", "output": "1770826" }, { "input": "1 2000000000 2", "output": "1000000000" }, { "input": "1 2000000000 3", "output": "333333333" }, { "input": "1 2000000000 5", "output": "133333333" }, { "input": "1 2000000000 7", "output": "76190476" }, { "input": "1 2000000000 11", "output": "41558442" }, { "input": "1 2000000000 13", "output": "31968032" }, { "input": "1 2000000000 17", "output": "22565668" }, { "input": "1 2000000000 19", "output": "19002671" }, { "input": "1 2000000000 23", "output": "14871653" }, { "input": "1 2000000000 29", "output": "11281946" }, { "input": "1 2000000000 37", "output": "8262288" }, { "input": "1 2000000000 67", "output": "3927637" }, { "input": "1 2000000000 83", "output": "2998028" }, { "input": "1 2000000000 97", "output": "2505943" }, { "input": "1 2000000000 103", "output": "2312816" }, { "input": "1 2000000000 107", "output": "2205007" }, { "input": "1 2000000000 503", "output": "347553" }, { "input": "1 2000000000 1009", "output": "151176" }, { "input": "100000000 500000000 500", "output": "0" }, { "input": "1 2000000000 1511", "output": "101472" }, { "input": "1 2000000000 2003", "output": "78092" }, { "input": "1 2000000000 4001", "output": "40979" }, { "input": "1 2000000000 8009", "output": "21014" }, { "input": "1 2000000000 10007", "output": "16746" }, { "input": "1 2000000000 20011", "output": "7327" }, { "input": "1 2000000000 30011", "output": "3399" }, { "input": "1 2000000000 40009", "output": "928" }, { "input": "1 2000000000 41011", "output": "724" }, { "input": "1 2000000000 44017", "output": "135" }, { "input": "1 2000000000 46021", "output": "1" }, { "input": "1 2000000000 50021", "output": "1" }, { "input": "1000 2000000000 2", "output": "999999501" }, { "input": "43104 2000000000 3", "output": "333326149" }, { "input": "19431 20000000 17", "output": "225438" }, { "input": "10034 20501000 53", "output": "53698" }, { "input": "108314 57823000 3001", "output": "1755" }, { "input": "1000000000 2000000000 2", "output": "500000001" }, { "input": "1000000000 2000000000 5", "output": "66666666" }, { "input": "120 57513234 121", "output": "0" }, { "input": "1500000000 2000000000 11", "output": "10389612" }, { "input": "1570000 800000000 30011", "output": "0" }, { "input": "1 2000000000 41011", "output": "724" }, { "input": "1 2000000000 10000019", "output": "1" }, { "input": "1 2000000000 50000017", "output": "1" }, { "input": "1 2000000000 100000007", "output": "1" }, { "input": "1 2000000000 200000033", "output": "1" }, { "input": "1 2000000000 400000009", "output": "1" }, { "input": "1 2000000000 800000011", "output": "1" }, { "input": "1 2000000000 900000011", "output": "1" }, { "input": "1 2000000000 1000000007", "output": "1" }, { "input": "1 2000000000 1700000009", "output": "1" }, { "input": "40000000 1600000000 3001", "output": "42482" }, { "input": "1999950000 2000000000 151", "output": "38" }, { "input": "1000 2000000000 211", "output": "989868" }, { "input": "1000 1000000000 1950000023", "output": "0" }, { "input": "1999999999 2000000000 19", "output": "0" }, { "input": "1999999000 2000000000 23", "output": "6" }, { "input": "1999999999 2000000000 31", "output": "1" }, { "input": "1 2000000000 1999073521", "output": "0" }, { "input": "1999999999 2000000000 97", "output": "0" }, { "input": "1999999999 2000000000 2", "output": "1" }, { "input": "1999999999 2000000000 601", "output": "0" }, { "input": "1999999999 2000000000 44017", "output": "0" }, { "input": "1000000000 2000000000 4001", "output": "19490" }, { "input": "1 2000000000 44711", "output": "3" }, { "input": "1900000000 2000000000 44711", "output": "2" }, { "input": "1 1840207360 44711", "output": "1" }, { "input": "11 1840207360 44711", "output": "1" }, { "input": "44711 44711 44711", "output": "1" }, { "input": "2000000000 2000000000 211", "output": "0" }, { "input": "2000000000 2000000000 53", "output": "0" }, { "input": "4 4 4", "output": "0" }, { "input": "1 1000000000 999999997", "output": "0" }, { "input": "911186 911186 73", "output": "0" } ]
3,000
0
0
17,083