problem_id
stringlengths
3
7
contestId
stringclasses
660 values
problem_index
stringclasses
27 values
programmingLanguage
stringclasses
3 values
testset
stringclasses
5 values
incorrect_passedTestCount
float64
0
146
incorrect_timeConsumedMillis
float64
15
4.26k
incorrect_memoryConsumedBytes
float64
0
271M
incorrect_submission_id
stringlengths
7
9
incorrect_source
stringlengths
10
27.7k
correct_passedTestCount
float64
2
360
correct_timeConsumedMillis
int64
30
8.06k
correct_memoryConsumedBytes
int64
0
475M
correct_submission_id
stringlengths
7
9
correct_source
stringlengths
28
21.2k
contest_name
stringclasses
664 values
contest_type
stringclasses
3 values
contest_start_year
int64
2.01k
2.02k
time_limit
float64
0.5
15
memory_limit
float64
64
1.02k
title
stringlengths
2
54
description
stringlengths
35
3.16k
input_format
stringlengths
67
1.76k
output_format
stringlengths
18
1.06k
interaction_format
null
note
stringclasses
840 values
examples
stringlengths
34
1.16k
rating
int64
800
3.4k
tags
stringclasses
533 values
testset_size
int64
2
360
official_tests
stringlengths
44
19.7M
official_tests_complete
bool
1 class
input_mode
stringclasses
1 value
generated_checker
stringclasses
231 values
executable
bool
1 class
413/C
413
C
PyPy 3
TESTS
19
93
0
143149190
n,m = map(int,input().split()) a = list(map(int,input().split())) b = list(map(int,input().split())) c = [] res = 0 for i in b: c.append(a[i-1]) c = sorted(c,reverse=True) for i in a: if i not in c:res+=i for i in c: if res>i:res+=res else:res+=i print(res)
67
46
0
178760251
n, m = list(map(int, input().split())) arr = list(map(int, input().split())) auction = set(map(int, input().split())) non_auction_sum = 0 max_auction = 0 for i in range(n): if (i+1) in auction: max_auction = max(max_auction, arr[i]) else: non_auction_sum += arr[i] if non_auction_sum < max_auction: non_auction_sum = (non_auction_sum + max_auction) * (2 ** (m-1)) else: non_auction_sum = non_auction_sum * (2 ** m) print(non_auction_sum)
Coder-Strike 2014 - Round 2
CF
2,014
1
256
Jeopardy!
'Jeopardy!' is an intellectual game where players answer questions and earn points. Company Q conducts a simplified 'Jeopardy!' tournament among the best IT companies. By a lucky coincidence, the old rivals made it to the finals: company R1 and company R2. The finals will have n questions, m of them are auction questions and n - m of them are regular questions. Each question has a price. The price of the i-th question is ai points. During the game the players chose the questions. At that, if the question is an auction, then the player who chose it can change the price if the number of his current points is strictly larger than the price of the question. The new price of the question cannot be less than the original price and cannot be greater than the current number of points of the player who chose the question. The correct answer brings the player the points equal to the price of the question. The wrong answer to the question reduces the number of the player's points by the value of the question price. The game will go as follows. First, the R2 company selects a question, then the questions are chosen by the one who answered the previous question correctly. If no one answered the question, then the person who chose last chooses again. All R2 employees support their team. They want to calculate what maximum possible number of points the R2 team can get if luck is on their side during the whole game (they will always be the first to correctly answer questions). Perhaps you are not going to be surprised, but this problem was again entrusted for you to solve.
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100; m ≤ min(n, 30)) — the total number of questions and the number of auction questions, correspondingly. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 107) — the prices of the questions. The third line contains m distinct integers bi (1 ≤ bi ≤ n) — the numbers of auction questions. Assume that the questions are numbered from 1 to n.
In the single line, print the answer to the problem — the maximum points the R2 company can get if it plays optimally well. It is guaranteed that the answer fits into the integer 64-bit signed type.
null
null
[{"input": "4 1\n1 3 7 5\n3", "output": "18"}, {"input": "3 2\n10 3 8\n2 3", "output": "40"}, {"input": "2 2\n100 200\n1 2", "output": "400"}]
1,400
["greedy", "math"]
67
[{"input": "4 1\r\n1 3 7 5\r\n3\r\n", "output": "18\r\n"}, {"input": "3 2\r\n10 3 8\r\n2 3\r\n", "output": "40\r\n"}, {"input": "2 2\r\n100 200\r\n1 2\r\n", "output": "400\r\n"}, {"input": "1 1\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "2 2\r\n1 5\r\n1 2\r\n", "output": "10\r\n"}, {"input": "5 3\r\n5 8 7 1 9\r\n2 5 3\r\n", "output": "60\r\n"}, {"input": "5 5\r\n9 1 6 2 1\r\n3 1 4 5 2\r\n", "output": "144\r\n"}, {"input": "25 5\r\n66 41 91 33 86 67 38 79 49 7 77 54 29 19 22 48 63 37 11 100 8 6 47 27 26\r\n12 14 1 23 18\r\n", "output": "29056\r\n"}, {"input": "50 10\r\n19098 20847 65754 94580 54808 57092 23130 15638 43645 52323 52822 65193 90139 69196 83680 70109 96772 35102 56685 6692 30738 74558 57144 24054 44447 51959 22847 18735 23534 821 5540 39948 7552 72425 23213 2770 98496 81096 84868 167 36408 26572 19351 82775 23225 35377 63193 58352 45111 60889\r\n8 20 32 17 11 44 39 30 36 16\r\n", "output": "1880325120\r\n"}, {"input": "2 1\r\n19 4\r\n1\r\n", "output": "23\r\n"}, {"input": "3 1\r\n65 81 6\r\n2\r\n", "output": "152\r\n"}, {"input": "5 1\r\n72 32 17 46 82\r\n2\r\n", "output": "434\r\n"}, {"input": "100 1\r\n9 9 72 55 14 8 55 58 35 67 3 18 73 92 41 49 15 60 18 66 9 26 97 47 43 88 71 97 19 34 48 96 79 53 8 24 69 49 12 23 77 12 21 88 66 9 29 13 61 69 54 77 41 13 4 68 37 74 7 6 29 76 55 72 89 4 78 27 29 82 18 83 12 4 32 69 89 85 66 13 92 54 38 5 26 56 17 55 29 4 17 39 29 94 3 67 85 98 21 14\r\n13\r\n", "output": "8834\r\n"}, {"input": "25 24\r\n1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 1\r\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24\r\n", "output": "70368752566272\r\n"}, {"input": "30 30\r\n6074511 9621540 9853685 9073323 6897794 9366449 1648254 3848902 8729661 9330982 9970280 1886362 5605123 3406494 501290 3140164 2406173 346072 1520895 441795 5271130 7576116 337766 6666108 953354 5085881 2876195 8036150 1251715 4952594\r\n30 5 10 28 21 18 6 13 29 23 17 24 14 25 3 27 20 26 12 2 4 11 16 15 22 7 8 19 1 9\r\n", "output": "5352753316495360\r\n"}, {"input": "50 30\r\n6015200 8643865 4116771 6555197 304415 8580071 8414182 3089212 5684567 7595481 1272699 7127763 3309618 1410297 4349070 2027355 136702 6863504 1800751 5585842 5924142 5188269 4805201 9313209 8941399 5137060 4983630 8467668 1646260 7804684 8646497 7067118 6896291 9109696 6197162 1366002 1703718 3852639 8427694 552915 5001315 5238093 9152085 7288325 8115109 3800240 5658858 4392321 8244056 3275379\r\n30 25 34 8 31 50 48 19 49 26 9 24 22 6 44 14 27 43 3 28 35 10 21 17 45 12 40 47 1 33\r\n", "output": "96888048737845248\r\n"}, {"input": "1 1\r\n1846236\r\n1\r\n", "output": "1846236\r\n"}, {"input": "2 1\r\n8912260 7309391\r\n1\r\n", "output": "16221651\r\n"}, {"input": "3 1\r\n9949628 37460 9989934\r\n3\r\n", "output": "19977022\r\n"}, {"input": "5 3\r\n1819638 2087365 162773 9531053 130978\r\n3 1 4\r\n", "output": "46997584\r\n"}, {"input": "10 4\r\n886062 1016649 67899 9999839 98342 64636 816991 263 1050987 1858\r\n1 9 7 4\r\n", "output": "89995888\r\n"}, {"input": "10 10\r\n1 652210 1 1 1 1 1 1 1 1\r\n10 1 6 7 9 8 4 3 5 2\r\n", "output": "333931520\r\n"}, {"input": "50 5\r\n223036 65304 301127 8945 10289 15638 260 246 68 14 23 6 3 2 8 2 1 392212 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 242747 1 1 1 243737 1 1 1 1 1 1 10000000 572890\r\n18 50 38 42 49\r\n", "output": "170000000\r\n"}, {"input": "50 10\r\n1103 17631 1582 250 6933 26 14434 6 2 1 1 1 1 1 3625 1 5909 1 1 1 1 1 1 1 1 1 1 1 1 7196 14842 1 1 1 1 1 1 12053 9999991 1 10502 1 1 1 1 1 1 1 1 1\r\n41 15 17 1 5 31 7 38 30 39\r\n", "output": "5129995264\r\n"}, {"input": "50 15\r\n369 139 49 15 4 5 1 1 1 365 1 1 1 1 484 165 105 1 1 1 382 105 1 1 1 72 1 1 91 96 1 1 1 1 1 133 9997031 1 1 31 1 1 1 291 558 1 1 1 464 1\r\n49 26 40 21 45 30 16 10 15 44 22 29 36 17 37\r\n", "output": "163801350144\r\n"}, {"input": "50 18\r\n20 23 54 4 1 1 15 50 56 1 1 71 1 1 1 1 1 15 8 1 12 1 1 1 1 1 76 1 19 11 55 42 1 1 1 1 1 9 1 30 5 1 1 1 20 1 1 1 1 9975634\r\n9 18 7 45 27 32 12 41 31 8 3 30 21 19 40 38 29 50\r\n", "output": "1307536261120\r\n"}, {"input": "100 1\r\n954110 7577191 694644 113513 467690 71415 25351 26000 37902 29150 2015 94 741 20 71 9 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 10000000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n78\r\n", "output": "20000000\r\n"}, {"input": "100 5\r\n502646 93620 4203 12132 2444 9620 6 201 4 20 10000000 1 6 9 472804 2 2 5 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 276285 518136 1 1 1 1 1 1 1 1 1 1 1 1 1 1 189005 1 1 1 1 1 1 1 1 1 1 1 1\r\n73 72 15 88 11\r\n", "output": "170000000\r\n"}, {"input": "100 10\r\n9999984 1396 8798 4760 3138 310 840 41 37 79 45 1 7 2 1 1 1 1 11032 1 1 1 11226 1 1 1 1 1 1 1 12147 1 1 1 1 1 1 16512 1 1 1 1 1 1 1 1 1 1 1 2658 1 1 1 1 7562 1 1 1 1 6207 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3334 1 1 1 1 1 1 1310 1 1 1 1 1 1 1 1 1\r\n19 55 91 50 31 23 60 84 38 1\r\n", "output": "5129991680\r\n"}, {"input": "100 15\r\n380 122 2 18 5 2 3 242 1 1 1 1 1 64 1 1 1 1 1 198 323 284 1 419 1 225 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 584 1 1 1 55 9999036 1 1 1 1 1 1 1 1 447 1 1 471 1 1 1 1 1 1 1 374 1 1 1 1 1 1 1 1 1 1 1 273 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 68 1\r\n22 45 49 24 26 62 70 82 21 20 59 14 99 8 50\r\n", "output": "163834200064\r\n"}, {"input": "100 16\r\n15 18 54 132 138 1 1 45 164 1 1 1 1 1 1 1 1 1 1 1 1 9999567 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 188 1 1 1 213 1 1 27 1 1 1 1 1 1 1 1 1 1 1 200 1 140 221 1 1 1 1 1 1 1 1 132 1 78 1 1 1 1 1 243 1 1 1 1 1 1 1 1 1 1 240 1 1 290 1 34 1 1 1 1 1 1\r\n92 46 8 58 94 39 9 89 61 60 4 70 78 72 43 22\r\n", "output": "327675805696\r\n"}, {"input": "3 1\r\n1 2 4\r\n1\r\n", "output": "12\r\n"}, {"input": "3 1\r\n1 2 4\r\n2\r\n", "output": "10\r\n"}, {"input": "3 1\r\n1 2 4\r\n3\r\n", "output": "7\r\n"}, {"input": "2 1\r\n1 2\r\n1\r\n", "output": "4\r\n"}, {"input": "2 1\r\n1 2\r\n2\r\n", "output": "3\r\n"}, {"input": "3 2\r\n1 2 4\r\n1 2\r\n", "output": "16\r\n"}, {"input": "3 2\r\n1 2 4\r\n3 2\r\n", "output": "10\r\n"}, {"input": "3 2\r\n1 2 4\r\n3 1\r\n", "output": "12\r\n"}, {"input": "3 3\r\n4 2 1\r\n1 3 2\r\n", "output": "16\r\n"}, {"input": "5 4\r\n1 2 2 4 8\r\n1 2 4 5\r\n", "output": "80\r\n"}, {"input": "3 2\r\n10 7 1000\r\n2 3\r\n", "output": "2020\r\n"}, {"input": "4 2\r\n2 2 4 8\r\n3 4\r\n", "output": "24\r\n"}, {"input": "3 2\r\n1 3 5\r\n1 3\r\n", "output": "16\r\n"}, {"input": "3 2\r\n10 1 12\r\n2 3\r\n", "output": "44\r\n"}, {"input": "4 2\r\n1 2 3 100\r\n2 4\r\n", "output": "208\r\n"}, {"input": "3 2\r\n10 5 200\r\n2 3\r\n", "output": "420\r\n"}, {"input": "3 2\r\n3 5 3\r\n2 3\r\n", "output": "16\r\n"}, {"input": "3 2\r\n5 4 100\r\n2 3\r\n", "output": "210\r\n"}, {"input": "5 4\r\n100 200 300 400 500\r\n1 2 3 5\r\n", "output": "7200\r\n"}, {"input": "3 2\r\n100 200 180\r\n1 2\r\n", "output": "760\r\n"}, {"input": "4 3\r\n2 5 17 4\r\n1 2 3\r\n", "output": "84\r\n"}, {"input": "5 2\r\n2 2 4 7 15\r\n4 5\r\n", "output": "46\r\n"}, {"input": "3 2\r\n200 100 1000\r\n2 3\r\n", "output": "2400\r\n"}, {"input": "4 2\r\n2 2 2 7\r\n1 4\r\n", "output": "22\r\n"}, {"input": "8 4\r\n2 2 2 2 1 2 3 9\r\n5 6 7 8\r\n", "output": "136\r\n"}, {"input": "3 2\r\n2 1 5\r\n2 3\r\n", "output": "14\r\n"}]
false
stdio
null
true
793/C
793
C
PyPy 3
PRETESTS
6
93
23,347,200
26615105
# t*v+a = b # t=(b-a)/v import sys #sys.stdin=open("data.txt") input=sys.stdin.readline mintime=0 maxtime=10**9 useless=0 n=int(input()) #include <math.h> # int 'y1' redeclared as different kind of symbol x1,y1,x2,y2=map(int,input().split()) for _ in range(n): rx,ry,vx,vy=map(int,input().split()) # x time if vx==0: if not x1<rx<x2: useless=1 break else: t1=(x1-rx)/vx t2=(x2-rx)/vx if t1>t2: t1,t2=t2,t1 mintime=max(mintime,t1) maxtime=min(maxtime,t2) # y time if vy==0: if not y1<ry<y2: useless=1 break else: t1=(y1-ry)/vy t2=(y2-ry)/vy if t1>t2: t1,t2=t2,t1 mintime=max(mintime,t1) maxtime=min(maxtime,t2) if useless or mintime>=maxtime+1e-9: print(-1) else: print(mintime)
80
748
0
44048034
def comp( x , v , l , r ) : if x < l and v <= 0 : return [-1,-10] if x > r and v >= 0 : return [-1,-10] if x >= l and x <= r : if v == 0 : if x == l or x == r : return [-1,-10] else : return [0,10**18] if v > 0 : s = 0 e = (r-x)/v return [s,e] if v < 0 : s = 0 e = ( x - l )/(-v) return [s,e] if x < l : s = (l-x)/v e = (r-x)/v return [s,e] if x > r : s = (x-r)/(-v) e = (x-l)/(-v) return [s,e] print(x,v,l,r) n = int(input()) x1 , y1 , x2 , y2 = map( int , input().split() ) lo = 0.000000001 hi = 10**18 for i in range( n ) : x , y , vx , vy = map( int , input().split() ) l1 = comp( x , vx , x1 , x2 ) l2 = comp( y , vy , y1 , y2 ) lo = max( lo , l1[0] , l2[0] ) hi = min( hi , l1[1] , l2[1] ) if lo < hi : print(lo) else: print(-1)
Tinkoff Challenge - Elimination Round
CF
2,017
2
256
Mice problem
Igor the analyst fell asleep on the work and had a strange dream. In the dream his desk was crowded with computer mice, so he bought a mousetrap to catch them. The desk can be considered as an infinite plane, then the mousetrap is a rectangle which sides are parallel to the axes, and which opposite sides are located in points (x1, y1) and (x2, y2). Igor wants to catch all mice. Igor has analysed their behavior and discovered that each mouse is moving along a straight line with constant speed, the speed of the i-th mouse is equal to (vix, viy), that means that the x coordinate of the mouse increases by vix units per second, while the y coordinates increases by viy units. The mousetrap is open initially so that the mice are able to move freely on the desk. Igor can close the mousetrap at any moment catching all the mice that are strictly inside the mousetrap. Igor works a lot, so he is busy in the dream as well, and he asks you to write a program that by given mousetrap's coordinates, the initial coordinates of the mice and their speeds determines the earliest time moment in which he is able to catch all the mice. Please note that Igor can close the mousetrap only once.
The first line contains single integer n (1 ≤ n ≤ 100 000) — the number of computer mice on the desk. The second line contains four integers x1, y1, x2 and y2 (0 ≤ x1 ≤ x2 ≤ 100 000), (0 ≤ y1 ≤ y2 ≤ 100 000) — the coordinates of the opposite corners of the mousetrap. The next n lines contain the information about mice. The i-th of these lines contains four integers rix, riy, vix and viy, (0 ≤ rix, riy ≤ 100 000, - 100 000 ≤ vix, viy ≤ 100 000), where (rix, riy) is the initial position of the mouse, and (vix, viy) is its speed.
In the only line print minimum possible non-negative number t such that if Igor closes the mousetrap at t seconds from the beginning, then all the mice are strictly inside the mousetrap. If there is no such t, print -1. Your answer is considered correct if its absolute or relative error doesn't exceed 10 - 6. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if $$\frac{|a-b|}{\max(1,b)} \leq 10^{-6}$$.
null
Here is a picture of the first sample Points A, B, C, D - start mice positions, segments are their paths. Then, at first time when all mice will be in rectangle it will be looks like this: Here is a picture of the second sample Points A, D, B will never enter rectangle.
[{"input": "4\n7 7 9 8\n3 5 7 5\n7 5 2 4\n3 3 7 8\n6 6 3 2", "output": "0.57142857142857139685"}, {"input": "4\n7 7 9 8\n0 3 -5 4\n5 0 5 4\n9 9 -1 -6\n10 5 -7 -10", "output": "-1"}]
2,300
["geometry", "implementation", "math", "sortings"]
80
[{"input": "4\r\n7 7 9 8\r\n3 5 7 5\r\n7 5 2 4\r\n3 3 7 8\r\n6 6 3 2\r\n", "output": "0.57142857142857139685\r\n"}, {"input": "4\r\n7 7 9 8\r\n0 3 -5 4\r\n5 0 5 4\r\n9 9 -1 -6\r\n10 5 -7 -10\r\n", "output": "-1\r\n"}, {"input": "4\r\n8 42 60 54\r\n9 54 -58 -62\r\n46 47 52 -76\r\n15 50 -37 -40\r\n54 51 78 64\r\n", "output": "0.00000000000000000000\r\n"}, {"input": "4\r\n17501 63318 51967 74514\r\n1305 84026 79493 -78504\r\n41159 81000 -44104 -42722\r\n31063 65435 25578 33487\r\n18330 79949 83467 -74531\r\n", "output": "0.20374120991785441004\r\n"}, {"input": "7\r\n24 38 44 47\r\n44 45 -50 -36\r\n33 48 -11 -39\r\n43 44 13 15\r\n42 47 24 -21\r\n40 41 19 7\r\n26 41 -20 -15\r\n42 40 43 19\r\n", "output": "0.02564102564102564014\r\n"}, {"input": "1\r\n0 0 100000 100000\r\n0 0 1 0\r\n", "output": "-1\r\n"}, {"input": "1\r\n0 0 100000 100000\r\n0 0 0 1\r\n", "output": "-1\r\n"}, {"input": "1\r\n0 0 100000 100000\r\n0 0 -1 -1\r\n", "output": "-1\r\n"}, {"input": "1\r\n0 0 100000 100000\r\n1 1 1 1\r\n", "output": "0.00000000000000000000\r\n"}, {"input": "1\r\n0 0 10000 10000\r\n20000 2 -1 0\r\n", "output": "10000.00000000000000000000\r\n"}, {"input": "1\r\n0 0 10000 10000\r\n20000 2 1 0\r\n", "output": "-1\r\n"}, {"input": "1\r\n0 0 10000 10000\r\n10001 10001 -1 -1\r\n", "output": "1.00000000000000000000\r\n"}, {"input": "1\r\n0 0 10000 10000\r\n10001 9999 -1 1\r\n", "output": "-1\r\n"}, {"input": "1\r\n1 1 1 1\r\n1 1 1 1\r\n", "output": "-1\r\n"}, {"input": "1\r\n0 0 10 10\r\n5 5 0 0\r\n", "output": "0.00000000000000000000\r\n"}, {"input": "1\r\n0 0 10 10\r\n5 5 5 5\r\n", "output": "0.00000000000000000000\r\n"}, {"input": "1\r\n0 1 2 1\r\n0 0 1 1\r\n", "output": "-1\r\n"}, {"input": "1\r\n1 1 5 5\r\n1 0 0 1\r\n", "output": "-1\r\n"}, {"input": "1\r\n1 1 2 2\r\n1 1 1 0\r\n", "output": "-1\r\n"}, {"input": "2\r\n2 2 5 5\r\n3 3 1 1\r\n10 3 -1 0\r\n", "output": "-1\r\n"}, {"input": "1\r\n99998 99998 99999 99999\r\n0 0 99999 100000\r\n", "output": "0.99998999989999903804\r\n"}, {"input": "1\r\n1 1 3 3\r\n2 2 0 0\r\n", "output": "0.00000000000000000000\r\n"}, {"input": "2\r\n99999 99999 100000 100000\r\n1 1 100000 100000\r\n1 1 99999 99999\r\n", "output": "0.99998999989999903804\r\n"}, {"input": "1\r\n0 0 2 2\r\n1 1 0 0\r\n", "output": "0.00000000000000000000\r\n"}, {"input": "1\r\n0 0 1 1\r\n0 0 0 0\r\n", "output": "-1\r\n"}, {"input": "1\r\n0 0 1 1\r\n0 0 1 0\r\n", "output": "-1\r\n"}, {"input": "1\r\n7 7 8 8\r\n7 7 0 0\r\n", "output": "-1\r\n"}, {"input": "1\r\n1 1 3 3\r\n4 4 0 0\r\n", "output": "-1\r\n"}, {"input": "1\r\n0 0 2 2\r\n1 0 0 0\r\n", "output": "-1\r\n"}, {"input": "1\r\n0 0 99999 1\r\n0 99999 100000 -99999\r\n", "output": "0.99998999989999903804\r\n"}, {"input": "1\r\n1 0 2 0\r\n0 0 1 0\r\n", "output": "-1\r\n"}, {"input": "1\r\n1 1 11 11\r\n5 5 0 0\r\n", "output": "0.00000000000000000000\r\n"}, {"input": "1\r\n1 1 1 1\r\n1 1 0 0\r\n", "output": "-1\r\n"}, {"input": "4\r\n0 49998 2 50002\r\n1 50000 0 0\r\n1 50000 0 0\r\n1 0 0 1\r\n1 100000 0 -1\r\n", "output": "49998.00000000000000000000\r\n"}, {"input": "1\r\n0 0 10 10\r\n0 0 0 0\r\n", "output": "-1\r\n"}, {"input": "1\r\n1 1 11 11\r\n1 2 0 1\r\n", "output": "-1\r\n"}, {"input": "1\r\n0 0 100 100\r\n0 0 1 0\r\n", "output": "-1\r\n"}, {"input": "1\r\n1 0 1 2\r\n0 0 1 1\r\n", "output": "-1\r\n"}, {"input": "1\r\n1 1 3 3\r\n1 1 0 0\r\n", "output": "-1\r\n"}, {"input": "2\r\n0 0 5 5\r\n5 3 0 1\r\n3 3 1 1\r\n", "output": "-1\r\n"}, {"input": "1\r\n1 1 3 3\r\n1 1 1 0\r\n", "output": "-1\r\n"}, {"input": "1\r\n10 10 20 20\r\n0 10 1 1\r\n", "output": "-1\r\n"}, {"input": "1\r\n5 5 10 10\r\n4 6 1 0\r\n", "output": "1.00000000000000000000\r\n"}, {"input": "1\r\n0 0 5 5\r\n2 5 0 0\r\n", "output": "-1\r\n"}, {"input": "1\r\n1 1 2 2\r\n0 2 1 0\r\n", "output": "-1\r\n"}, {"input": "1\r\n1 1 3 3\r\n0 1 1 0\r\n", "output": "-1\r\n"}, {"input": "1\r\n1 1 2 2\r\n1 1 0 0\r\n", "output": "-1\r\n"}, {"input": "2\r\n10 0 12 2\r\n9 1 1 0\r\n7 1 1 0\r\n", "output": "-1\r\n"}, {"input": "1\r\n2 2 4 4\r\n3 1 1 1\r\n", "output": "-1\r\n"}, {"input": "2\r\n1 1 3 3\r\n2 10 0 -1\r\n10000 2 -1 0\r\n", "output": "-1\r\n"}, {"input": "1\r\n1 1 3 3\r\n2 0 1 1\r\n", "output": "-1\r\n"}, {"input": "2\r\n0 0 2 100000\r\n1 1 0 100000\r\n100000 1 -99999 0\r\n", "output": "0.99998999989999903804\r\n"}, {"input": "2\r\n1 1 2 2\r\n0 0 1 1\r\n1 1 1 1\r\n", "output": "-1\r\n"}, {"input": "1\r\n0 0 2 2\r\n0 1 0 0\r\n", "output": "-1\r\n"}, {"input": "1\r\n1 1 2 2\r\n0 0 0 0\r\n", "output": "-1\r\n"}, {"input": "1\r\n0 0 10 10\r\n0 0 0 1337\r\n", "output": "-1\r\n"}, {"input": "1\r\n1 1 3 3\r\n1 1 0 1\r\n", "output": "-1\r\n"}, {"input": "1\r\n0 0 2 2\r\n0 1 0 1\r\n", "output": "-1\r\n"}, {"input": "1\r\n1 1 3 3\r\n2 1 0 0\r\n", "output": "-1\r\n"}, {"input": "1\r\n0 0 3 3\r\n1 3 1 0\r\n", "output": "-1\r\n"}, {"input": "1\r\n0 0 100 100\r\n0 0 100 0\r\n", "output": "-1\r\n"}]
false
stdio
import sys def main(input_path, output_path, submission_path): with open(output_path, 'r') as f: ref_line = f.readline().strip() with open(submission_path, 'r') as f: sub_line = f.readline().strip() if ref_line == '-1': print(1 if sub_line == '-1' else 0) return if sub_line == '-1': print(0) return try: a = float(sub_line) b = float(ref_line) except: print(0) return if a < -1e-12: print(0) return error = abs(a - b) if b != 0: error /= max(1.0, b) print(1 if error <= 1e-6 else 0) if __name__ == "__main__": if len(sys.argv) != 4: print("Usage: checker.py input_path output_path submission_output_path") sys.exit(1) main(sys.argv[1], sys.argv[2], sys.argv[3])
true
623/A
623
A
PyPy 3
TESTS
25
295
10,240,000
85870959
from sys import stdin n,m = [int(x) for x in stdin.readline().split()] graph = [set([y for y in range(n)]) for x in range(n)] for x in range(n): graph[x].remove(x) for edge in range(m): a,b = [int(x) for x in stdin.readline().split()] a -= 1 b -= 1 graph[a].remove(b) graph[b].remove(a) notVisited = set([x for x in range(n)]) val = [-1 for x in range(n)] for x in range(n): if not graph[x]: val[x] = 'b' notVisited.remove(x) valid = True while notVisited: for x in notVisited: q = [(x,'a')] break cur = 0 while q: nxt,char = q.pop() if nxt in notVisited: notVisited.remove(nxt) val[nxt] = char for x in graph[nxt]: if x in notVisited: if char == 'a': q.append((x,'c')) else: q.append((x,'a')) else: if val[x] == char: valid = False break if valid: print('Yes') print(''.join(val)) else: print('No')
106
514
18,329,600
52719106
from math import * N, M = 510, 3 * 10 ** 5 + 10 g = [[] for _ in range(M)] n , m = map(int, input().split()) mx = [[False] * N for _ in range(N + 1)] for _ in range(m): u, v = map(int, input().split()) mx[u][v] = mx[v][u] = True all_eq = True mark = [False] * N for i in range(1, n + 1): for j in range(i + 1, n + 1): if not mx[i][j]: all_eq = False mark[i] = True g[i].append(j) g[j].append(i) if all_eq: print("YES") print(n * 'a') exit(0) ans = [-1] * N def dfs(v, c): ans[v] = c res = False for u in g[v]: if ans[u] == -1: res |= dfs(u, 2 - c) elif ans[u] == ans[v]: return False return res for i in range(1, n + 1): if mark[i] and ans[i] == -1 and dfs(i, 0): print("NO") exit(0) for i in range(1, n + 1): if len(g[i]) == 0: ans[i] = 1 if ans[i] == -1: print("NO") exit(0) for i in range(1, n + 1): for j in range(i + 1, n + 1): if mx[i][j] != (abs(ans[i] - ans[j]) != 2): print("NO") exit(0) print("YES") for i in range(1, n + 1): print(chr(ans[i] + ord('a')), end = '') print()
AIM Tech Round (Div. 1)
CF
2,016
2
256
Graph and String
One day student Vasya was sitting on a lecture and mentioned a string s1s2... sn, consisting of letters "a", "b" and "c" that was written on his desk. As the lecture was boring, Vasya decided to complete the picture by composing a graph G with the following properties: - G has exactly n vertices, numbered from 1 to n. - For all pairs of vertices i and j, where i ≠ j, there is an edge connecting them if and only if characters si and sj are either equal or neighbouring in the alphabet. That is, letters in pairs "a"-"b" and "b"-"c" are neighbouring, while letters "a"-"c" are not. Vasya painted the resulting graph near the string and then erased the string. Next day Vasya's friend Petya came to a lecture and found some graph at his desk. He had heard of Vasya's adventure and now he wants to find out whether it could be the original graph G, painted by Vasya. In order to verify this, Petya needs to know whether there exists a string s, such that if Vasya used this s he would produce the given graph G.
The first line of the input contains two integers n and m $$( 1 \leq n \leq 500, 0 \leq m \leq \frac { n ( n - 1 ) } { 2 } )$$ — the number of vertices and edges in the graph found by Petya, respectively. Each of the next m lines contains two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — the edges of the graph G. It is guaranteed, that there are no multiple edges, that is any pair of vertexes appear in this list no more than once.
In the first line print "Yes" (without the quotes), if the string s Petya is interested in really exists and "No" (without the quotes) otherwise. If the string s exists, then print it on the second line of the output. The length of s must be exactly n, it must consist of only letters "a", "b" and "c" only, and the graph built using this string must coincide with G. If there are multiple possible answers, you may print any of them.
null
In the first sample you are given a graph made of two vertices with an edge between them. So, these vertices can correspond to both the same and adjacent letters. Any of the following strings "aa", "ab", "ba", "bb", "bc", "cb", "cc" meets the graph's conditions. In the second sample the first vertex is connected to all three other vertices, but these three vertices are not connected with each other. That means that they must correspond to distinct letters that are not adjacent, but that is impossible as there are only two such letters: a and c.
[{"input": "2 1\n1 2", "output": "Yes\naa"}, {"input": "4 3\n1 2\n1 3\n1 4", "output": "No"}]
1,800
["constructive algorithms", "graphs"]
106
[{"input": "2 1\r\n1 2\r\n", "output": "Yes\r\naa\r\n"}, {"input": "4 3\r\n1 2\r\n1 3\r\n1 4\r\n", "output": "No\r\n"}, {"input": "4 4\r\n1 2\r\n1 3\r\n1 4\r\n3 4\r\n", "output": "Yes\r\nbacc\r\n"}, {"input": "1 0\r\n", "output": "Yes\r\na\r\n"}, {"input": "8 28\r\n3 2\r\n4 2\r\n7 4\r\n6 3\r\n3 7\r\n8 1\r\n3 4\r\n5 1\r\n6 5\r\n5 3\r\n7 1\r\n5 8\r\n5 4\r\n6 1\r\n6 4\r\n2 1\r\n4 1\r\n8 2\r\n7 2\r\n6 8\r\n8 4\r\n6 7\r\n3 1\r\n7 8\r\n3 8\r\n5 7\r\n5 2\r\n6 2\r\n", "output": "Yes\r\naaaaaaaa\r\n"}, {"input": "8 28\r\n3 2\r\n4 2\r\n7 4\r\n6 3\r\n3 7\r\n8 1\r\n3 4\r\n5 1\r\n6 5\r\n5 3\r\n7 1\r\n5 8\r\n5 4\r\n6 1\r\n6 4\r\n2 1\r\n4 1\r\n8 2\r\n7 2\r\n6 8\r\n8 4\r\n6 7\r\n3 1\r\n7 8\r\n3 8\r\n5 7\r\n5 2\r\n6 2\r\n", "output": "Yes\r\naaaaaaaa\r\n"}, {"input": "4 3\r\n4 3\r\n2 4\r\n2 3\r\n", "output": "Yes\r\naccc\r\n"}, {"input": "4 2\r\n4 3\r\n1 2\r\n", "output": "Yes\r\naacc\r\n"}, {"input": "5 3\r\n1 2\r\n1 3\r\n4 5\r\n", "output": "No\r\n"}, {"input": "6 4\r\n1 2\r\n1 3\r\n4 5\r\n4 6\r\n", "output": "No\r\n"}, {"input": "6 4\r\n1 2\r\n2 3\r\n4 5\r\n4 6\r\n", "output": "No\r\n"}, {"input": "6 4\r\n3 2\r\n1 3\r\n6 5\r\n4 6\r\n", "output": "No\r\n"}, {"input": "6 4\r\n1 2\r\n1 3\r\n4 6\r\n5 6\r\n", "output": "No\r\n"}, {"input": "7 13\r\n1 2\r\n2 3\r\n1 3\r\n4 5\r\n5 6\r\n4 6\r\n2 5\r\n2 7\r\n3 7\r\n7 4\r\n7 6\r\n7 1\r\n7 5\r\n", "output": "No\r\n"}, {"input": "8 18\r\n3 7\r\n2 5\r\n5 3\r\n3 8\r\n8 6\r\n6 3\r\n6 4\r\n4 8\r\n1 2\r\n6 1\r\n2 7\r\n2 4\r\n4 5\r\n4 3\r\n6 5\r\n1 4\r\n5 7\r\n3 1\r\n", "output": "No\r\n"}, {"input": "20 55\r\n20 11\r\n14 5\r\n4 9\r\n17 5\r\n16 5\r\n20 16\r\n11 17\r\n2 14\r\n14 19\r\n9 15\r\n20 19\r\n5 18\r\n15 20\r\n1 16\r\n12 20\r\n4 7\r\n16 19\r\n17 19\r\n16 12\r\n19 9\r\n11 13\r\n18 17\r\n10 8\r\n20 1\r\n16 8\r\n1 13\r\n11 12\r\n13 18\r\n4 13\r\n14 10\r\n9 13\r\n8 9\r\n6 9\r\n2 13\r\n10 16\r\n19 1\r\n7 17\r\n20 4\r\n12 8\r\n3 2\r\n18 10\r\n6 13\r\n14 9\r\n7 9\r\n19 7\r\n8 15\r\n20 6\r\n16 13\r\n14 13\r\n19 8\r\n7 14\r\n6 2\r\n9 1\r\n7 1\r\n10 6\r\n", "output": "No\r\n"}, {"input": "15 84\r\n11 9\r\n3 11\r\n13 10\r\n2 12\r\n5 9\r\n1 7\r\n14 4\r\n14 2\r\n14 1\r\n11 8\r\n1 8\r\n14 10\r\n4 15\r\n10 5\r\n5 12\r\n13 11\r\n6 14\r\n5 7\r\n12 11\r\n9 1\r\n10 15\r\n2 6\r\n7 15\r\n14 9\r\n9 7\r\n11 14\r\n8 15\r\n12 7\r\n13 6\r\n2 9\r\n9 6\r\n15 3\r\n12 15\r\n6 15\r\n4 6\r\n4 1\r\n9 12\r\n10 7\r\n6 1\r\n11 10\r\n2 3\r\n5 2\r\n13 2\r\n13 3\r\n12 6\r\n4 3\r\n5 8\r\n12 1\r\n9 15\r\n14 5\r\n12 14\r\n10 1\r\n9 4\r\n7 13\r\n3 6\r\n15 1\r\n13 9\r\n11 1\r\n10 4\r\n9 3\r\n8 12\r\n13 12\r\n6 7\r\n12 10\r\n4 12\r\n13 15\r\n2 10\r\n3 8\r\n1 5\r\n15 2\r\n4 11\r\n2 1\r\n10 8\r\n14 3\r\n14 8\r\n8 7\r\n13 1\r\n5 4\r\n11 2\r\n6 8\r\n5 15\r\n2 4\r\n9 8\r\n9 10\r\n", "output": "No\r\n"}, {"input": "15 13\r\n13 15\r\n13 3\r\n14 3\r\n10 7\r\n2 5\r\n5 12\r\n12 11\r\n9 2\r\n13 7\r\n7 4\r\n12 10\r\n15 7\r\n6 13\r\n", "output": "No\r\n"}, {"input": "6 6\r\n1 4\r\n3 4\r\n6 4\r\n2 6\r\n5 3\r\n3 2\r\n", "output": "No\r\n"}, {"input": "4 6\r\n4 2\r\n3 1\r\n3 4\r\n3 2\r\n4 1\r\n2 1\r\n", "output": "Yes\r\naaaa\r\n"}, {"input": "4 4\r\n3 2\r\n2 4\r\n1 2\r\n3 4\r\n", "output": "Yes\r\nabcc\r\n"}, {"input": "4 3\r\n1 3\r\n1 4\r\n3 4\r\n", "output": "Yes\r\nacaa\r\n"}, {"input": "4 4\r\n1 2\r\n4 1\r\n3 4\r\n3 1\r\n", "output": "Yes\r\nbacc\r\n"}, {"input": "4 4\r\n4 2\r\n3 4\r\n3 1\r\n2 3\r\n", "output": "Yes\r\nacbc\r\n"}, {"input": "4 5\r\n3 1\r\n2 1\r\n3 4\r\n2 4\r\n3 2\r\n", "output": "Yes\r\nabbc\r\n"}, {"input": "4 4\r\n4 1\r\n3 1\r\n3 2\r\n3 4\r\n", "output": "Yes\r\nacba\r\n"}, {"input": "4 5\r\n3 4\r\n2 1\r\n3 1\r\n4 1\r\n2 3\r\n", "output": "Yes\r\nbabc\r\n"}, {"input": "4 4\r\n1 3\r\n3 4\r\n2 1\r\n3 2\r\n", "output": "Yes\r\naabc\r\n"}, {"input": "4 3\r\n2 1\r\n1 4\r\n2 4\r\n", "output": "Yes\r\naaca\r\n"}, {"input": "4 4\r\n2 4\r\n1 2\r\n1 3\r\n1 4\r\n", "output": "Yes\r\nbaca\r\n"}, {"input": "4 2\r\n3 1\r\n2 4\r\n", "output": "Yes\r\nacac\r\n"}, {"input": "4 4\r\n4 2\r\n2 1\r\n3 2\r\n1 4\r\n", "output": "Yes\r\nabca\r\n"}, {"input": "4 5\r\n4 1\r\n2 4\r\n2 1\r\n2 3\r\n3 1\r\n", "output": "Yes\r\nbbac\r\n"}, {"input": "4 4\r\n1 2\r\n3 1\r\n2 4\r\n2 3\r\n", "output": "Yes\r\nabac\r\n"}, {"input": "4 2\r\n2 3\r\n1 4\r\n", "output": "Yes\r\nacca\r\n"}, {"input": "4 4\r\n2 1\r\n1 4\r\n2 3\r\n3 1\r\n", "output": "Yes\r\nbaac\r\n"}, {"input": "4 3\r\n3 2\r\n1 2\r\n1 3\r\n", "output": "Yes\r\naaac\r\n"}, {"input": "4 4\r\n3 2\r\n2 4\r\n3 4\r\n4 1\r\n", "output": "Yes\r\naccb\r\n"}, {"input": "4 5\r\n4 2\r\n3 2\r\n4 3\r\n4 1\r\n2 1\r\n", "output": "Yes\r\nabcb\r\n"}, {"input": "4 4\r\n3 1\r\n2 4\r\n1 4\r\n3 4\r\n", "output": "Yes\r\nacab\r\n"}, {"input": "4 5\r\n3 1\r\n4 3\r\n4 1\r\n2 1\r\n2 4\r\n", "output": "Yes\r\nbacb\r\n"}, {"input": "4 4\r\n2 4\r\n3 4\r\n1 2\r\n4 1\r\n", "output": "Yes\r\naacb\r\n"}, {"input": "4 5\r\n1 4\r\n4 3\r\n4 2\r\n3 2\r\n1 3\r\n", "output": "Yes\r\nacbb\r\n"}, {"input": "2 0\r\n", "output": "Yes\r\nac\r\n"}, {"input": "3 0\r\n", "output": "No\r\n"}, {"input": "3 1\r\n1 2\r\n", "output": "Yes\r\naac\r\n"}, {"input": "3 2\r\n1 2\r\n3 2\r\n", "output": "Yes\r\nabc\r\n"}, {"input": "3 3\r\n1 2\r\n1 3\r\n2 3\r\n", "output": "Yes\r\naaa\r\n"}, {"input": "3 1\r\n2 3\r\n", "output": "Yes\r\nacc\r\n"}, {"input": "3 1\r\n1 3\r\n", "output": "Yes\r\naca\r\n"}, {"input": "4 3\r\n1 2\r\n2 3\r\n3 4\r\n", "output": "No\r\n"}, {"input": "5 9\r\n4 3\r\n4 2\r\n3 1\r\n5 1\r\n4 1\r\n2 1\r\n5 2\r\n3 2\r\n5 4\r\n", "output": "Yes\r\nbbabc\r\n"}, {"input": "6 9\r\n1 4\r\n1 6\r\n3 6\r\n5 4\r\n2 6\r\n3 5\r\n4 6\r\n1 5\r\n5 6\r\n", "output": "No\r\n"}, {"input": "8 21\r\n4 7\r\n7 8\r\n6 4\r\n8 5\r\n8 1\r\n3 4\r\n4 8\r\n4 5\r\n6 7\r\n6 8\r\n7 1\r\n4 2\r\n1 5\r\n6 5\r\n8 2\r\n3 6\r\n5 2\r\n7 5\r\n1 2\r\n7 2\r\n4 1\r\n", "output": "No\r\n"}, {"input": "4 3\r\n1 4\r\n1 3\r\n2 4\r\n", "output": "No\r\n"}, {"input": "4 4\r\n1 3\r\n1 4\r\n2 3\r\n2 4\r\n", "output": "No\r\n"}, {"input": "4 3\r\n1 3\r\n2 4\r\n3 4\r\n", "output": "No\r\n"}, {"input": "4 3\r\n1 3\r\n2 4\r\n1 4\r\n", "output": "No\r\n"}, {"input": "5 6\r\n1 2\r\n2 4\r\n2 5\r\n3 4\r\n3 5\r\n4 5\r\n", "output": "No\r\n"}, {"input": "6 10\r\n1 5\r\n1 4\r\n3 4\r\n3 6\r\n1 2\r\n3 5\r\n2 5\r\n2 6\r\n1 6\r\n4 6\r\n", "output": "No\r\n"}, {"input": "4 3\r\n1 2\r\n3 4\r\n2 3\r\n", "output": "No\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] ref_output_path = sys.argv[2] sub_output_path = sys.argv[3] # Read input with open(input_path) as f: n, m = map(int, f.readline().split()) edges = [] for _ in range(m): u, v = map(int, f.readline().split()) edges.append((u, v)) # Read reference output with open(ref_output_path) as f: ref_lines = [line.strip() for line in f.readlines()] # Read submission output with open(sub_output_path) as f: sub_lines = [line.strip() for line in f.readlines()] # Check submission lines if not ref_lines or not sub_lines: print(0) return ref_first = ref_lines[0] sub_first = sub_lines[0] if ref_first != sub_first: print(0) return if ref_first == 'Yes': if len(sub_lines) != 2: print(0) return s = sub_lines[1] if len(s) != n: print(0) return for c in s: if c not in {'a', 'b', 'c'}: print(0) return # Create adjacency set adj = set() for u, v in edges: u_idx = u - 1 v_idx = v - 1 adj.add((u_idx, v_idx)) adj.add((v_idx, u_idx)) # Check all pairs valid = True for i in range(n): for j in range(n): if i == j: continue edge_exists = (i, j) in adj c1 = s[i] c2 = s[j] allowed = (c1 == c2) or (abs(ord(c1) - ord(c2)) == 1) if edge_exists != allowed: valid = False break if not valid: break if valid: print(1) else: print(0) else: if len(sub_lines) != 1: print(0) else: print(1) if __name__ == "__main__": main()
true
623/A
623
A
PyPy 3-64
TESTS
25
140
5,529,600
148067886
import sys input = lambda: sys.stdin.buffer.readline().decode().strip() n, m = map(int, input().split()) adj, ans = [[False] * n for _ in range(n)], [''] * n for _ in range(m): u, v = map(int, input().split()) adj[u - 1][v - 1] = adj[v - 1][u - 1] = True for i in range(n): for j in range(n): if i != j and not adj[i][j]: if ans[i] == ans[j] and ans[i]: exit(print('No')) if ans[i] == 'a': ans[j] = 'c' elif ans[i] == 'c': ans[j] = 'a' elif ans[j] == 'a': ans[i] = 'c' elif ans[j] == 'c': ans[i] = 'a' else: ans[i], ans[j] = 'a', 'c' print('Yes', '\n', *[x if x else 'b' for x in ans], sep='')
106
623
1,228,800
15798582
n, m = map(int, input().split()) s = [[0] * n for i in range(n)] for i in range(m): a, b = map(int, input().split()) s[a - 1][b - 1] = 1 s[b - 1][a - 1] = 1 for i in range(n): s[i][i] = 1 answer = [0] * n for i in range(n): flag = True for j in range(n): if s[i][j] == 0: flag = False if flag: answer[i] = 'b' for i in range(n): if answer[i] == 0: answer[i] = 'a' for j in range(n): if s[i][j] == 0: answer[j] = 'c' if s[i][j] == 1 and answer[j] != 'b': answer[j] = 'a' break flag = True for i in range(n): for j in range(n): if s[i][j] == 1: if (answer[i] == 'a' and answer[j] == 'c') or (answer[i] == 'c' and answer[j] == 'a'): flag = False if s[i][j] == 0: if abs(ord(answer[i]) - ord(answer[j])) < 2: flag = False if flag: print('Yes') answer = ''.join(answer) print(answer) else: print("No")
AIM Tech Round (Div. 1)
CF
2,016
2
256
Graph and String
One day student Vasya was sitting on a lecture and mentioned a string s1s2... sn, consisting of letters "a", "b" and "c" that was written on his desk. As the lecture was boring, Vasya decided to complete the picture by composing a graph G with the following properties: - G has exactly n vertices, numbered from 1 to n. - For all pairs of vertices i and j, where i ≠ j, there is an edge connecting them if and only if characters si and sj are either equal or neighbouring in the alphabet. That is, letters in pairs "a"-"b" and "b"-"c" are neighbouring, while letters "a"-"c" are not. Vasya painted the resulting graph near the string and then erased the string. Next day Vasya's friend Petya came to a lecture and found some graph at his desk. He had heard of Vasya's adventure and now he wants to find out whether it could be the original graph G, painted by Vasya. In order to verify this, Petya needs to know whether there exists a string s, such that if Vasya used this s he would produce the given graph G.
The first line of the input contains two integers n and m $$( 1 \leq n \leq 500, 0 \leq m \leq \frac { n ( n - 1 ) } { 2 } )$$ — the number of vertices and edges in the graph found by Petya, respectively. Each of the next m lines contains two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — the edges of the graph G. It is guaranteed, that there are no multiple edges, that is any pair of vertexes appear in this list no more than once.
In the first line print "Yes" (without the quotes), if the string s Petya is interested in really exists and "No" (without the quotes) otherwise. If the string s exists, then print it on the second line of the output. The length of s must be exactly n, it must consist of only letters "a", "b" and "c" only, and the graph built using this string must coincide with G. If there are multiple possible answers, you may print any of them.
null
In the first sample you are given a graph made of two vertices with an edge between them. So, these vertices can correspond to both the same and adjacent letters. Any of the following strings "aa", "ab", "ba", "bb", "bc", "cb", "cc" meets the graph's conditions. In the second sample the first vertex is connected to all three other vertices, but these three vertices are not connected with each other. That means that they must correspond to distinct letters that are not adjacent, but that is impossible as there are only two such letters: a and c.
[{"input": "2 1\n1 2", "output": "Yes\naa"}, {"input": "4 3\n1 2\n1 3\n1 4", "output": "No"}]
1,800
["constructive algorithms", "graphs"]
106
[{"input": "2 1\r\n1 2\r\n", "output": "Yes\r\naa\r\n"}, {"input": "4 3\r\n1 2\r\n1 3\r\n1 4\r\n", "output": "No\r\n"}, {"input": "4 4\r\n1 2\r\n1 3\r\n1 4\r\n3 4\r\n", "output": "Yes\r\nbacc\r\n"}, {"input": "1 0\r\n", "output": "Yes\r\na\r\n"}, {"input": "8 28\r\n3 2\r\n4 2\r\n7 4\r\n6 3\r\n3 7\r\n8 1\r\n3 4\r\n5 1\r\n6 5\r\n5 3\r\n7 1\r\n5 8\r\n5 4\r\n6 1\r\n6 4\r\n2 1\r\n4 1\r\n8 2\r\n7 2\r\n6 8\r\n8 4\r\n6 7\r\n3 1\r\n7 8\r\n3 8\r\n5 7\r\n5 2\r\n6 2\r\n", "output": "Yes\r\naaaaaaaa\r\n"}, {"input": "8 28\r\n3 2\r\n4 2\r\n7 4\r\n6 3\r\n3 7\r\n8 1\r\n3 4\r\n5 1\r\n6 5\r\n5 3\r\n7 1\r\n5 8\r\n5 4\r\n6 1\r\n6 4\r\n2 1\r\n4 1\r\n8 2\r\n7 2\r\n6 8\r\n8 4\r\n6 7\r\n3 1\r\n7 8\r\n3 8\r\n5 7\r\n5 2\r\n6 2\r\n", "output": "Yes\r\naaaaaaaa\r\n"}, {"input": "4 3\r\n4 3\r\n2 4\r\n2 3\r\n", "output": "Yes\r\naccc\r\n"}, {"input": "4 2\r\n4 3\r\n1 2\r\n", "output": "Yes\r\naacc\r\n"}, {"input": "5 3\r\n1 2\r\n1 3\r\n4 5\r\n", "output": "No\r\n"}, {"input": "6 4\r\n1 2\r\n1 3\r\n4 5\r\n4 6\r\n", "output": "No\r\n"}, {"input": "6 4\r\n1 2\r\n2 3\r\n4 5\r\n4 6\r\n", "output": "No\r\n"}, {"input": "6 4\r\n3 2\r\n1 3\r\n6 5\r\n4 6\r\n", "output": "No\r\n"}, {"input": "6 4\r\n1 2\r\n1 3\r\n4 6\r\n5 6\r\n", "output": "No\r\n"}, {"input": "7 13\r\n1 2\r\n2 3\r\n1 3\r\n4 5\r\n5 6\r\n4 6\r\n2 5\r\n2 7\r\n3 7\r\n7 4\r\n7 6\r\n7 1\r\n7 5\r\n", "output": "No\r\n"}, {"input": "8 18\r\n3 7\r\n2 5\r\n5 3\r\n3 8\r\n8 6\r\n6 3\r\n6 4\r\n4 8\r\n1 2\r\n6 1\r\n2 7\r\n2 4\r\n4 5\r\n4 3\r\n6 5\r\n1 4\r\n5 7\r\n3 1\r\n", "output": "No\r\n"}, {"input": "20 55\r\n20 11\r\n14 5\r\n4 9\r\n17 5\r\n16 5\r\n20 16\r\n11 17\r\n2 14\r\n14 19\r\n9 15\r\n20 19\r\n5 18\r\n15 20\r\n1 16\r\n12 20\r\n4 7\r\n16 19\r\n17 19\r\n16 12\r\n19 9\r\n11 13\r\n18 17\r\n10 8\r\n20 1\r\n16 8\r\n1 13\r\n11 12\r\n13 18\r\n4 13\r\n14 10\r\n9 13\r\n8 9\r\n6 9\r\n2 13\r\n10 16\r\n19 1\r\n7 17\r\n20 4\r\n12 8\r\n3 2\r\n18 10\r\n6 13\r\n14 9\r\n7 9\r\n19 7\r\n8 15\r\n20 6\r\n16 13\r\n14 13\r\n19 8\r\n7 14\r\n6 2\r\n9 1\r\n7 1\r\n10 6\r\n", "output": "No\r\n"}, {"input": "15 84\r\n11 9\r\n3 11\r\n13 10\r\n2 12\r\n5 9\r\n1 7\r\n14 4\r\n14 2\r\n14 1\r\n11 8\r\n1 8\r\n14 10\r\n4 15\r\n10 5\r\n5 12\r\n13 11\r\n6 14\r\n5 7\r\n12 11\r\n9 1\r\n10 15\r\n2 6\r\n7 15\r\n14 9\r\n9 7\r\n11 14\r\n8 15\r\n12 7\r\n13 6\r\n2 9\r\n9 6\r\n15 3\r\n12 15\r\n6 15\r\n4 6\r\n4 1\r\n9 12\r\n10 7\r\n6 1\r\n11 10\r\n2 3\r\n5 2\r\n13 2\r\n13 3\r\n12 6\r\n4 3\r\n5 8\r\n12 1\r\n9 15\r\n14 5\r\n12 14\r\n10 1\r\n9 4\r\n7 13\r\n3 6\r\n15 1\r\n13 9\r\n11 1\r\n10 4\r\n9 3\r\n8 12\r\n13 12\r\n6 7\r\n12 10\r\n4 12\r\n13 15\r\n2 10\r\n3 8\r\n1 5\r\n15 2\r\n4 11\r\n2 1\r\n10 8\r\n14 3\r\n14 8\r\n8 7\r\n13 1\r\n5 4\r\n11 2\r\n6 8\r\n5 15\r\n2 4\r\n9 8\r\n9 10\r\n", "output": "No\r\n"}, {"input": "15 13\r\n13 15\r\n13 3\r\n14 3\r\n10 7\r\n2 5\r\n5 12\r\n12 11\r\n9 2\r\n13 7\r\n7 4\r\n12 10\r\n15 7\r\n6 13\r\n", "output": "No\r\n"}, {"input": "6 6\r\n1 4\r\n3 4\r\n6 4\r\n2 6\r\n5 3\r\n3 2\r\n", "output": "No\r\n"}, {"input": "4 6\r\n4 2\r\n3 1\r\n3 4\r\n3 2\r\n4 1\r\n2 1\r\n", "output": "Yes\r\naaaa\r\n"}, {"input": "4 4\r\n3 2\r\n2 4\r\n1 2\r\n3 4\r\n", "output": "Yes\r\nabcc\r\n"}, {"input": "4 3\r\n1 3\r\n1 4\r\n3 4\r\n", "output": "Yes\r\nacaa\r\n"}, {"input": "4 4\r\n1 2\r\n4 1\r\n3 4\r\n3 1\r\n", "output": "Yes\r\nbacc\r\n"}, {"input": "4 4\r\n4 2\r\n3 4\r\n3 1\r\n2 3\r\n", "output": "Yes\r\nacbc\r\n"}, {"input": "4 5\r\n3 1\r\n2 1\r\n3 4\r\n2 4\r\n3 2\r\n", "output": "Yes\r\nabbc\r\n"}, {"input": "4 4\r\n4 1\r\n3 1\r\n3 2\r\n3 4\r\n", "output": "Yes\r\nacba\r\n"}, {"input": "4 5\r\n3 4\r\n2 1\r\n3 1\r\n4 1\r\n2 3\r\n", "output": "Yes\r\nbabc\r\n"}, {"input": "4 4\r\n1 3\r\n3 4\r\n2 1\r\n3 2\r\n", "output": "Yes\r\naabc\r\n"}, {"input": "4 3\r\n2 1\r\n1 4\r\n2 4\r\n", "output": "Yes\r\naaca\r\n"}, {"input": "4 4\r\n2 4\r\n1 2\r\n1 3\r\n1 4\r\n", "output": "Yes\r\nbaca\r\n"}, {"input": "4 2\r\n3 1\r\n2 4\r\n", "output": "Yes\r\nacac\r\n"}, {"input": "4 4\r\n4 2\r\n2 1\r\n3 2\r\n1 4\r\n", "output": "Yes\r\nabca\r\n"}, {"input": "4 5\r\n4 1\r\n2 4\r\n2 1\r\n2 3\r\n3 1\r\n", "output": "Yes\r\nbbac\r\n"}, {"input": "4 4\r\n1 2\r\n3 1\r\n2 4\r\n2 3\r\n", "output": "Yes\r\nabac\r\n"}, {"input": "4 2\r\n2 3\r\n1 4\r\n", "output": "Yes\r\nacca\r\n"}, {"input": "4 4\r\n2 1\r\n1 4\r\n2 3\r\n3 1\r\n", "output": "Yes\r\nbaac\r\n"}, {"input": "4 3\r\n3 2\r\n1 2\r\n1 3\r\n", "output": "Yes\r\naaac\r\n"}, {"input": "4 4\r\n3 2\r\n2 4\r\n3 4\r\n4 1\r\n", "output": "Yes\r\naccb\r\n"}, {"input": "4 5\r\n4 2\r\n3 2\r\n4 3\r\n4 1\r\n2 1\r\n", "output": "Yes\r\nabcb\r\n"}, {"input": "4 4\r\n3 1\r\n2 4\r\n1 4\r\n3 4\r\n", "output": "Yes\r\nacab\r\n"}, {"input": "4 5\r\n3 1\r\n4 3\r\n4 1\r\n2 1\r\n2 4\r\n", "output": "Yes\r\nbacb\r\n"}, {"input": "4 4\r\n2 4\r\n3 4\r\n1 2\r\n4 1\r\n", "output": "Yes\r\naacb\r\n"}, {"input": "4 5\r\n1 4\r\n4 3\r\n4 2\r\n3 2\r\n1 3\r\n", "output": "Yes\r\nacbb\r\n"}, {"input": "2 0\r\n", "output": "Yes\r\nac\r\n"}, {"input": "3 0\r\n", "output": "No\r\n"}, {"input": "3 1\r\n1 2\r\n", "output": "Yes\r\naac\r\n"}, {"input": "3 2\r\n1 2\r\n3 2\r\n", "output": "Yes\r\nabc\r\n"}, {"input": "3 3\r\n1 2\r\n1 3\r\n2 3\r\n", "output": "Yes\r\naaa\r\n"}, {"input": "3 1\r\n2 3\r\n", "output": "Yes\r\nacc\r\n"}, {"input": "3 1\r\n1 3\r\n", "output": "Yes\r\naca\r\n"}, {"input": "4 3\r\n1 2\r\n2 3\r\n3 4\r\n", "output": "No\r\n"}, {"input": "5 9\r\n4 3\r\n4 2\r\n3 1\r\n5 1\r\n4 1\r\n2 1\r\n5 2\r\n3 2\r\n5 4\r\n", "output": "Yes\r\nbbabc\r\n"}, {"input": "6 9\r\n1 4\r\n1 6\r\n3 6\r\n5 4\r\n2 6\r\n3 5\r\n4 6\r\n1 5\r\n5 6\r\n", "output": "No\r\n"}, {"input": "8 21\r\n4 7\r\n7 8\r\n6 4\r\n8 5\r\n8 1\r\n3 4\r\n4 8\r\n4 5\r\n6 7\r\n6 8\r\n7 1\r\n4 2\r\n1 5\r\n6 5\r\n8 2\r\n3 6\r\n5 2\r\n7 5\r\n1 2\r\n7 2\r\n4 1\r\n", "output": "No\r\n"}, {"input": "4 3\r\n1 4\r\n1 3\r\n2 4\r\n", "output": "No\r\n"}, {"input": "4 4\r\n1 3\r\n1 4\r\n2 3\r\n2 4\r\n", "output": "No\r\n"}, {"input": "4 3\r\n1 3\r\n2 4\r\n3 4\r\n", "output": "No\r\n"}, {"input": "4 3\r\n1 3\r\n2 4\r\n1 4\r\n", "output": "No\r\n"}, {"input": "5 6\r\n1 2\r\n2 4\r\n2 5\r\n3 4\r\n3 5\r\n4 5\r\n", "output": "No\r\n"}, {"input": "6 10\r\n1 5\r\n1 4\r\n3 4\r\n3 6\r\n1 2\r\n3 5\r\n2 5\r\n2 6\r\n1 6\r\n4 6\r\n", "output": "No\r\n"}, {"input": "4 3\r\n1 2\r\n3 4\r\n2 3\r\n", "output": "No\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] ref_output_path = sys.argv[2] sub_output_path = sys.argv[3] # Read input with open(input_path) as f: n, m = map(int, f.readline().split()) edges = [] for _ in range(m): u, v = map(int, f.readline().split()) edges.append((u, v)) # Read reference output with open(ref_output_path) as f: ref_lines = [line.strip() for line in f.readlines()] # Read submission output with open(sub_output_path) as f: sub_lines = [line.strip() for line in f.readlines()] # Check submission lines if not ref_lines or not sub_lines: print(0) return ref_first = ref_lines[0] sub_first = sub_lines[0] if ref_first != sub_first: print(0) return if ref_first == 'Yes': if len(sub_lines) != 2: print(0) return s = sub_lines[1] if len(s) != n: print(0) return for c in s: if c not in {'a', 'b', 'c'}: print(0) return # Create adjacency set adj = set() for u, v in edges: u_idx = u - 1 v_idx = v - 1 adj.add((u_idx, v_idx)) adj.add((v_idx, u_idx)) # Check all pairs valid = True for i in range(n): for j in range(n): if i == j: continue edge_exists = (i, j) in adj c1 = s[i] c2 = s[j] allowed = (c1 == c2) or (abs(ord(c1) - ord(c2)) == 1) if edge_exists != allowed: valid = False break if not valid: break if valid: print(1) else: print(0) else: if len(sub_lines) != 1: print(0) else: print(1) if __name__ == "__main__": main()
true
745/B
745
B
Python 3
TESTS
36
139
4,608,000
23058646
n,m = map(int,list(input().split())) ar = [0]*m for i in range(n): s = input() for j in range(m): if s[j]=='X': ar[j]+=1 ss = set(ar) ss.discard(0) if len(ss)>1: print('NO') else: print('YES') # C:\Users\Usuario\HOME2\Programacion\ACM
77
62
6,758,400
23066037
def codeforces(piece): row_number = 0 for row in piece: num = row.count('X') if num == 0: continue if row_number == 0: row_number = num continue if num != row_number: return 'NO' col_number = 0 cols = [[row[i] for row in piece] for i in range(len(piece[0]))] for col in cols: num = col.count('X') if num == 0: continue if col_number == 0: col_number = num continue if num != col_number: return 'NO' return 'YES' rows, _ = input().split() rows = int(rows) piece = [] for _ in range(rows): line = input() piece.append(line) print(codeforces(piece))
Codeforces Round 385 (Div. 2)
CF
2,016
2
256
Hongcow Solves A Puzzle
Hongcow likes solving puzzles. One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified. The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap. You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position.
The first line of input will contain two integers n and m (1 ≤ n, m ≤ 500), the dimensions of the puzzle piece. The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region.
Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise.
null
For the first sample, one example of a rectangle we can form is as follows For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle. In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle:
[{"input": "2 3\nXXX\nXXX", "output": "YES"}, {"input": "2 2\n.X\nXX", "output": "NO"}, {"input": "5 5\n.....\n..X..\n.....\n.....\n.....", "output": "YES"}]
1,400
["implementation"]
77
[{"input": "2 3\r\nXXX\r\nXXX\r\n", "output": "YES\r\n"}, {"input": "2 2\r\n.X\r\nXX\r\n", "output": "NO\r\n"}, {"input": "5 5\r\n.....\r\n..X..\r\n.....\r\n.....\r\n.....\r\n", "output": "YES\r\n"}, {"input": "1 500\r\n.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.\r\n", "output": "YES\r\n"}, {"input": "10 1\r\n.\r\n.\r\n.\r\n.\r\nX\r\n.\r\n.\r\n.\r\n.\r\n.\r\n", "output": "YES\r\n"}, {"input": "8 5\r\nXX.XX\r\nX.XXX\r\nX.XXX\r\nXXX.X\r\nXX.XX\r\nXX..X\r\nXXX.X\r\nXXXX.\r\n", "output": "NO\r\n"}, {"input": "2 3\r\nX..\r\n...\r\n", "output": "YES\r\n"}, {"input": "6 8\r\nXXXXXX..\r\nXXXXXXXX\r\n.X.X..X.\r\n.XXXX..X\r\nXX.XXXXX\r\nX...X..X\r\n", "output": "NO\r\n"}, {"input": "10 2\r\n.X\r\n.X\r\nXX\r\nXX\r\nX.\r\nXX\r\nX.\r\nX.\r\n..\r\n..\r\n", "output": "NO\r\n"}, {"input": "1 1\r\nX\r\n", "output": "YES\r\n"}, {"input": "3 3\r\nXXX\r\nX.X\r\nX..\r\n", "output": "NO\r\n"}, {"input": "3 3\r\nXX.\r\nXXX\r\n.XX\r\n", "output": "NO\r\n"}, {"input": "4 4\r\nXXXX\r\nXXXX\r\nXX..\r\nXX..\r\n", "output": "NO\r\n"}, {"input": "3 3\r\nX.X\r\nX.X\r\nXXX\r\n", "output": "NO\r\n"}, {"input": "3 2\r\nX.\r\nXX\r\n.X\r\n", "output": "NO\r\n"}, {"input": "2 1\r\nX\r\nX\r\n", "output": "YES\r\n"}, {"input": "1 2\r\nXX\r\n", "output": "YES\r\n"}, {"input": "2 3\r\n.XX\r\nXX.\r\n", "output": "NO\r\n"}, {"input": "5 5\r\nXXX..\r\n.XXX.\r\n..XXX\r\nXXX..\r\n.XXX.\r\n", "output": "NO\r\n"}, {"input": "2 4\r\nXX..\r\n.XX.\r\n", "output": "NO\r\n"}, {"input": "4 4\r\nXXX.\r\nXXX.\r\nX.X.\r\n..X.\r\n", "output": "NO\r\n"}, {"input": "2 3\r\nXX.\r\n.XX\r\n", "output": "NO\r\n"}, {"input": "3 5\r\nXXXX.\r\n.XXXX\r\nXXXX.\r\n", "output": "NO\r\n"}, {"input": "2 4\r\nXXX.\r\n.XXX\r\n", "output": "NO\r\n"}, {"input": "3 3\r\n...\r\n.X.\r\nXXX\r\n", "output": "NO\r\n"}, {"input": "4 4\r\nXX..\r\n.XX.\r\n..XX\r\n....\r\n", "output": "NO\r\n"}, {"input": "3 3\r\n.X.\r\nXX.\r\nX..\r\n", "output": "NO\r\n"}, {"input": "3 4\r\nXXX.\r\nX.X.\r\nXXX.\r\n", "output": "NO\r\n"}, {"input": "4 4\r\n....\r\n....\r\n.XX.\r\n..X.\r\n", "output": "NO\r\n"}, {"input": "4 4\r\n....\r\n.XX.\r\n..XX\r\n....\r\n", "output": "NO\r\n"}, {"input": "4 4\r\n....\r\n....\r\n.XXX\r\n..X.\r\n", "output": "NO\r\n"}, {"input": "2 6\r\nXXXXX.\r\nXXXXXX\r\n", "output": "NO\r\n"}, {"input": "3 3\r\nX.X\r\nXXX\r\n.X.\r\n", "output": "NO\r\n"}, {"input": "3 3\r\nXXX\r\nX.X\r\n..X\r\n", "output": "NO\r\n"}, {"input": "3 3\r\n.XX\r\nXX.\r\n.XX\r\n", "output": "NO\r\n"}, {"input": "3 3\r\n.XX\r\nXX.\r\n...\r\n", "output": "NO\r\n"}, {"input": "4 4\r\nXX..\r\n.XX.\r\n....\r\n....\r\n", "output": "NO\r\n"}, {"input": "3 8\r\n.XXXXXX.\r\nXXX..XXX\r\n.XXXXXX.\r\n", "output": "NO\r\n"}, {"input": "3 3\r\nXX.\r\n.XX\r\nXX.\r\n", "output": "NO\r\n"}, {"input": "3 3\r\n.XX\r\nXX.\r\nXX.\r\n", "output": "NO\r\n"}, {"input": "5 5\r\n.....\r\n..XXX\r\nXXX..\r\n..XXX\r\n.....\r\n", "output": "NO\r\n"}, {"input": "4 4\r\n....\r\nXXX.\r\nX.XX\r\nXXX.\r\n", "output": "NO\r\n"}, {"input": "5 5\r\n.....\r\n.XXX.\r\n.X.XX\r\n.....\r\n.....\r\n", "output": "NO\r\n"}, {"input": "2 2\r\nX.\r\nX.\r\n", "output": "YES\r\n"}, {"input": "2 5\r\n...XX\r\n..XX.\r\n", "output": "NO\r\n"}, {"input": "3 5\r\nXXX..\r\n.XXX.\r\n..XXX\r\n", "output": "NO\r\n"}, {"input": "2 6\r\n...XXX\r\n.XXX..\r\n", "output": "NO\r\n"}, {"input": "5 5\r\n.....\r\n.XX..\r\n..XX.\r\n.....\r\n.....\r\n", "output": "NO\r\n"}, {"input": "5 5\r\n.X.XX\r\n.XXX.\r\n.XXX.\r\n.XXX.\r\n.XXX.\r\n", "output": "NO\r\n"}, {"input": "4 4\r\n....\r\n.XXX\r\n..X.\r\n.XX.\r\n", "output": "NO\r\n"}, {"input": "4 4\r\n....\r\n.XX.\r\nXX..\r\n....\r\n", "output": "NO\r\n"}, {"input": "2 4\r\n..XX\r\n.XX.\r\n", "output": "NO\r\n"}, {"input": "3 6\r\nXXX...\r\n.XXX..\r\n..XXX.\r\n", "output": "NO\r\n"}, {"input": "3 4\r\n.XXX\r\nXXX.\r\n.XXX\r\n", "output": "NO\r\n"}]
false
stdio
null
true
515/B
515
B
PyPy 3
TESTS
40
155
1,433,600
75217147
I=lambda:list(map(int,input().split())) n,m=I() a=I()[1::] b=I()[1::] ans=[0]*max(n,m) bo=[0]*n gl=[0]*m for i in a: bo[i-1]=1 for j in b: gl[j-1]=1 boy=set() girl=set() for i in range(n*m+1): x=bo[i%n] y=gl[i%m] if x|y: bo[i%n]=1 gl[i%m]=1 if bo.count(1)+gl.count(1)==n+m: print('Yes') else: print('No')
56
46
0
218400995
import sys sys.setrecursionlimit(100000000) input=lambda:sys.stdin.readline().strip() write=lambda x:sys.stdout.write(str(x)+'\n') # from random import randint # from copy import deepcopy # from collections import deque,Counter # from heapq import heapify,heappush,heappop # from bisect import bisect_left,bisect,insort from math import inf,sqrt,gcd,ceil,floor,log,log2,log10,pi # from functools import cmp_to_key n,m=map(int,input().split()) bs=[0]*n;gs=[0]*m t=list(map(int,input().split())) nb=t[0] for i in t[1:]: bs[i]=1 t=list(map(int,input().split())) ng=t[0] for i in t[1:]: gs[i]=1 for i in range(n*m): bs[i%n]=gs[i%m]=bs[i%n]|gs[i%m] if 0 in bs and 0 in gs: print('No') else: print('Yes')
Codeforces Round 292 (Div. 2)
CF
2,015
2
256
Drazil and His Happy Friends
Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan. There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites $$(i \bmod n)$$-th boy and $$(i \bmod m)$$-th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if he/she was happy originally), he stays happy forever. Drazil wants to know whether he can use this plan to make all his friends become happy at some moment.
The first line contains two integer n and m (1 ≤ n, m ≤ 100). The second line contains integer b (0 ≤ b ≤ n), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys. The third line conatins integer g (0 ≤ g ≤ m), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls. It is guaranteed that there is at least one person that is unhappy among his friends.
If Drazil can make all his friends become happy by this plan, print "Yes". Otherwise, print "No".
null
By $$i \bmod k$$ we define the remainder of integer division of i by k. In first sample case: - On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day. - On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day. - On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day. - On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy. - On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment.
[{"input": "2 3\n0\n1 0", "output": "Yes"}, {"input": "2 4\n1 0\n1 2", "output": "No"}, {"input": "2 3\n1 0\n1 1", "output": "Yes"}]
1,300
["brute force", "dsu", "meet-in-the-middle", "number theory"]
56
[{"input": "2 3\r\n0\r\n1 0\r\n", "output": "Yes\r\n"}, {"input": "2 4\r\n1 0\r\n1 2\r\n", "output": "No\r\n"}, {"input": "2 3\r\n1 0\r\n1 1\r\n", "output": "Yes\r\n"}, {"input": "16 88\r\n6 5 14 2 0 12 7\r\n30 21 64 35 79 74 39 63 44 81 73 0 27 33 69 12 86 46 20 25 55 52 7 58 23 5 60 32 41 50 82\r\n", "output": "Yes\r\n"}, {"input": "52 91\r\n13 26 1 3 43 17 19 32 46 33 48 23 37 50\r\n25 78 26 1 40 2 67 42 4 56 30 70 84 32 20 85 59 8 86 34 73 23 10 88 24 11\r\n", "output": "No\r\n"}, {"input": "26 52\r\n8 0 14 16 17 7 9 10 11\r\n15 39 15 2 41 42 30 17 18 31 6 21 35 48 50 51\r\n", "output": "No\r\n"}, {"input": "50 50\r\n0\r\n0\r\n", "output": "No\r\n"}, {"input": "27 31\r\n4 25 5 19 20\r\n26 5 28 17 2 1 0 26 23 12 29 6 4 25 19 15 13 20 24 8 27 22 30 3 10 9 7\r\n", "output": "Yes\r\n"}, {"input": "55 79\r\n5 51 27 36 45 53\r\n30 15 28 0 5 38 3 34 30 35 1 32 12 27 42 39 69 33 10 63 16 29 76 19 60 70 67 31 78 68 45\r\n", "output": "Yes\r\n"}, {"input": "79 23\r\n35 31 62 14 9 46 18 68 69 42 13 50 77 23 76 5 53 40 16 32 74 54 38 25 45 39 26 37 66 78 3 48 10 17 56 59\r\n13 16 0 8 6 18 14 21 11 20 4 15 13 22\r\n", "output": "Yes\r\n"}, {"input": "7 72\r\n1 4\r\n3 49 32 28\r\n", "output": "Yes\r\n"}, {"input": "100 50\r\n31 52 54 8 60 61 62 63 64 16 19 21 73 25 76 77 79 30 81 32 33 34 37 88 39 40 91 42 94 95 96 98\r\n18 0 1 3 5 6 7 9 15 18 20 22 24 28 35 36 43 47 49\r\n", "output": "No\r\n"}, {"input": "98 49\r\n33 0 51 52 6 57 10 12 63 15 16 19 20 21 72 73 74 76 77 78 30 31 81 33 83 37 38 39 40 92 44 45 95 97\r\n15 4 5 7 9 11 13 17 18 22 26 35 36 41 42 47\r\n", "output": "No\r\n"}, {"input": "50 50\r\n14 7 8 12 16 18 22 23 24 28 30 35 40 46 49\r\n35 0 1 2 3 4 5 6 9 10 11 13 14 15 17 19 20 21 25 26 27 29 31 32 33 34 36 37 38 39 41 43 44 45 47 48\r\n", "output": "No\r\n"}, {"input": "30 44\r\n3 8 26 28\r\n6 2 30 38 26 8 6\r\n", "output": "No\r\n"}, {"input": "69 72\r\n18 58 46 52 43 1 55 16 7 4 38 68 14 32 53 41 29 2 59\r\n21 22 43 55 13 70 4 7 31 10 23 56 44 62 17 50 53 5 41 11 65 32\r\n", "output": "No\r\n"}, {"input": "76 28\r\n10 24 13 61 45 29 57 41 21 37 11\r\n2 12 9\r\n", "output": "No\r\n"}, {"input": "65 75\r\n15 25 60 12 62 37 22 47 52 3 63 58 13 14 49 34\r\n18 70 10 2 52 22 47 72 57 38 48 13 73 3 19 4 74 49 34\r\n", "output": "No\r\n"}, {"input": "6 54\r\n1 5\r\n14 13 49 31 37 44 2 15 51 52 22 28 10 35 47\r\n", "output": "No\r\n"}, {"input": "96 36\r\n34 84 24 0 48 85 13 61 37 62 38 86 75 3 16 64 40 28 76 53 5 17 42 6 7 91 67 55 68 92 57 11 71 35 59\r\n9 1 14 15 17 18 30 6 8 35\r\n", "output": "No\r\n"}, {"input": "40 40\r\n23 0 2 3 4 5 7 11 15 16 17 18 19 22 25 28 29 30 31 32 34 35 36 37\r\n16 1 6 8 9 10 12 13 14 20 21 23 24 26 27 38 39\r\n", "output": "No\r\n"}, {"input": "66 66\r\n24 2 35 3 36 4 5 10 45 14 48 18 51 19 21 55 22 23 24 25 26 63 31 65 32\r\n21 0 1 37 6 40 7 8 42 45 13 15 16 50 53 23 24 60 28 62 63 31\r\n", "output": "No\r\n"}, {"input": "20 20\r\n9 0 3 4 6 7 8 10 12 13\r\n10 1 2 5 9 11 14 15 16 18 19\r\n", "output": "No\r\n"}, {"input": "75 30\r\n18 46 47 32 33 3 34 35 21 51 7 9 54 39 72 42 59 29 14\r\n8 0 17 5 6 23 26 27 13\r\n", "output": "No\r\n"}, {"input": "100 50\r\n30 50 54 7 8 59 60 61 62 63 64 15 16 18 19 20 22 73 27 79 83 86 87 89 42 93 94 45 46 97 98\r\n20 1 2 3 5 6 17 21 24 25 26 28 30 31 32 34 35 38 40 41 49\r\n", "output": "Yes\r\n"}, {"input": "98 98\r\n43 49 1 51 3 53 4 55 56 8 9 10 60 11 12 61 64 16 65 17 19 20 21 72 24 74 25 77 78 31 34 35 36 37 87 88 89 42 92 43 44 94 46 96\r\n34 50 2 52 5 54 9 62 63 15 18 68 70 22 72 75 26 27 77 30 81 82 83 35 36 37 87 88 89 90 41 93 95 96 48\r\n", "output": "No\r\n"}, {"input": "100 100\r\n45 50 1 4 5 55 7 8 10 60 61 62 63 14 65 66 17 18 20 21 22 24 25 27 78 28 29 30 31 82 83 33 84 36 37 38 39 40 41 42 44 45 46 48 98 49\r\n34 50 1 2 52 3 54 56 7 9 59 61 14 16 67 18 69 22 73 24 76 79 81 82 84 35 36 38 39 90 43 44 45 47 49\r\n", "output": "No\r\n"}, {"input": "76 72\r\n29 4 64 68 20 8 12 50 42 46 0 70 11 37 75 47 45 29 17 19 73 9 41 31 35 67 65 39 51 55\r\n25 60 32 48 42 8 6 9 7 31 19 25 5 33 51 61 67 55 49 27 29 53 39 65 35 13\r\n", "output": "Yes\r\n"}, {"input": "39 87\r\n16 18 15 30 33 21 9 3 31 16 10 34 20 35 8 26 23\r\n36 33 75 81 24 42 54 78 39 57 60 30 36 63 4 76 25 1 40 73 22 58 49 85 31 74 59 20 44 83 65 23 41 71 47 14 35\r\n", "output": "Yes\r\n"}, {"input": "36 100\r\n10 0 32 4 5 33 30 18 14 35 7\r\n29 60 32 20 4 16 69 5 38 50 46 74 94 18 82 2 66 22 42 55 51 91 67 75 35 95 43 79 3 27\r\n", "output": "Yes\r\n"}, {"input": "90 25\r\n26 55 30 35 20 15 26 6 1 41 81 76 46 57 17 12 67 77 27 47 62 8 43 63 3 48 19\r\n9 10 16 21 7 17 12 13 19 9\r\n", "output": "Yes\r\n"}, {"input": "66 66\r\n26 0 54 6 37 43 13 25 38 2 32 56 20 50 39 27 51 9 64 4 16 17 65 11 5 47 23\r\n15 6 24 43 49 25 20 14 63 27 3 58 52 53 11 41\r\n", "output": "No\r\n"}, {"input": "24 60\r\n4 0 2 19 23\r\n15 12 24 49 2 14 3 52 28 5 6 19 32 33 34 35\r\n", "output": "Yes\r\n"}, {"input": "80 40\r\n27 0 41 44 45 6 47 8 10 52 13 14 16 17 18 59 21 62 23 64 26 68 29 32 75 37 78 39\r\n13 2 3 9 11 15 20 25 27 30 31 33 34 36\r\n", "output": "Yes\r\n"}, {"input": "66 99\r\n23 33 35 36 38 8 10 44 11 45 46 47 50 19 54 22 55 23 58 59 27 61 30 65\r\n32 33 67 69 4 70 38 6 39 7 74 42 9 43 12 13 14 15 81 82 84 85 20 87 89 90 24 58 59 27 95 97 31\r\n", "output": "Yes\r\n"}, {"input": "100 40\r\n25 61 42 2 3 25 46 66 68 69 49 9 10 50 91 72 92 33 73 53 14 15 55 96 36 39\r\n12 0 22 3 23 4 6 27 11 35 37 38 39\r\n", "output": "Yes\r\n"}, {"input": "90 30\r\n27 15 16 2 32 78 49 64 65 50 6 66 21 22 82 23 39 84 85 10 86 56 27 87 13 58 44 74\r\n7 19 4 20 24 25 12 27\r\n", "output": "No\r\n"}, {"input": "75 75\r\n33 30 74 57 23 19 42 71 11 44 29 58 43 48 61 63 13 27 50 17 18 70 64 39 12 32 36 10 40 51 49 1 54 73\r\n8 43 23 0 7 63 47 74 28\r\n", "output": "No\r\n"}, {"input": "98 98\r\n23 6 81 90 28 38 51 23 69 13 95 15 16 88 58 10 26 42 44 54 92 27 45 39\r\n18 20 70 38 82 72 61 37 78 74 23 15 56 59 35 93 64 28 57\r\n", "output": "No\r\n"}, {"input": "75 75\r\n19 48 3 5 67 23 8 70 45 63 36 38 56 15 10 37 52 11 9 27\r\n21 13 9 45 28 59 36 30 43 5 38 27 40 50 17 41 71 8 51 63 1 33\r\n", "output": "No\r\n"}, {"input": "3 20\r\n0\r\n1 19\r\n", "output": "Yes\r\n"}, {"input": "41 2\r\n1 33\r\n0\r\n", "output": "Yes\r\n"}, {"input": "50 49\r\n1 49\r\n0\r\n", "output": "Yes\r\n"}, {"input": "3 50\r\n0\r\n1 49\r\n", "output": "Yes\r\n"}, {"input": "100 100\r\n50 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49\r\n49 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\r\n", "output": "No\r\n"}, {"input": "100 100\r\n50 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49\r\n50 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\r\n", "output": "Yes\r\n"}, {"input": "91 98\r\n78 0 1 2 3 4 5 7 8 9 10 11 12 14 15 16 17 18 19 21 22 23 24 25 26 28 29 30 31 32 33 35 36 37 38 39 40 42 43 44 45 46 47 49 50 51 52 53 54 56 57 58 59 60 61 63 64 65 66 67 68 70 71 72 73 74 75 77 78 79 80 81 82 84 85 86 87 88 89\r\n84 0 1 2 3 4 5 7 8 9 10 11 12 14 15 16 17 18 19 21 22 23 24 25 26 28 29 30 31 32 33 35 36 37 38 39 40 42 43 44 45 46 47 49 50 51 52 53 54 56 57 58 59 60 61 63 64 65 66 67 68 70 71 72 73 74 75 77 78 79 80 81 82 84 85 86 87 88 89 91 92 93 94 95 96\r\n", "output": "No\r\n"}, {"input": "99 84\r\n66 0 2 3 5 6 8 9 11 12 14 15 17 18 20 21 23 24 26 27 29 30 32 33 35 36 38 39 41 42 44 45 47 48 50 51 53 54 56 57 59 60 62 63 65 66 68 69 71 72 74 75 77 78 80 81 83 84 86 87 89 90 92 93 95 96 98\r\n56 0 2 3 5 6 8 9 11 12 14 15 17 18 20 21 23 24 26 27 29 30 32 33 35 36 38 39 41 42 44 45 47 48 50 51 53 54 56 57 59 60 62 63 65 66 68 69 71 72 74 75 77 78 80 81 83\r\n", "output": "No\r\n"}, {"input": "75 90\r\n60 0 2 3 4 5 7 8 9 10 12 13 14 15 17 18 19 20 22 23 24 25 27 28 29 30 32 33 34 35 37 38 39 40 42 43 44 45 47 48 49 50 52 53 54 55 57 58 59 60 62 63 64 65 67 68 69 70 72 73 74\r\n72 0 2 3 4 5 7 8 9 10 12 13 14 15 17 18 19 20 22 23 24 25 27 28 29 30 32 33 34 35 37 38 39 40 42 43 44 45 47 48 49 50 52 53 54 55 57 58 59 60 62 63 64 65 67 68 69 70 72 73 74 75 77 78 79 80 82 83 84 85 87 88 89\r\n", "output": "No\r\n"}, {"input": "5 7\r\n1 0\r\n1 0\r\n", "output": "Yes\r\n"}, {"input": "100 1\r\n1 99\r\n0\r\n", "output": "Yes\r\n"}, {"input": "4 1\r\n1 3\r\n0\r\n", "output": "Yes\r\n"}, {"input": "4 5\r\n3 0 1 3\r\n4 0 1 3 4\r\n", "output": "Yes\r\n"}, {"input": "100 99\r\n1 99\r\n0\r\n", "output": "Yes\r\n"}, {"input": "2 3\r\n1 0\r\n2 0 2\r\n", "output": "Yes\r\n"}]
false
stdio
null
true
982/A
982
A
Python 3
TESTS
9
124
0
50425113
n=int(input()) a=str(input()) b=list(a) for i in range(n): b[i]=int(b[i]) k=0 if n==1 and b[0]==0: k=k+1 if n==2 and b[0]==b[1]==0: k=k+1 if n>3: if b[0]==b[1]==0: k=k+1 if b[n-1]==b[n-2]==0: k=k+1 if a.find('11')>=0: k=k+1 if a.find('000')>=0: k=k+1 if k==0: print('Yes') else: print('No')
55
46
0
173770461
import sys def read_input(input_path=None): if input_path is None: f = sys.stdin else: f = open(input_path, 'r') n = int(f.readline()) chairs = f.readline().strip() return n, chairs def sol(n, chairs): # chairs = 'o' + chairs + 'o' # # return ["YES" if "xx" not in chairs and "ooo" not in chairs else "NO"] chairs = '0' + chairs + '0' return ["YES" if "11" not in chairs and "000" not in chairs else "NO"] def solve(input_path=None): return sol(*read_input(input_path)) def main(): for line in sol(*read_input()): print(f"{line}") if __name__ == '__main__': main()
Codeforces Round 484 (Div. 2)
CF
2,018
1
256
Row
You're given a row with $$$n$$$ chairs. We call a seating of people "maximal" if the two following conditions hold: 1. There are no neighbors adjacent to anyone seated. 2. It's impossible to seat one more person without violating the first rule. The seating is given as a string consisting of zeros and ones ($$$0$$$ means that the corresponding seat is empty, $$$1$$$ — occupied). The goal is to determine whether this seating is "maximal". Note that the first and last seats are not adjacent (if $$$n \ne 2$$$).
The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of chairs. The next line contains a string of $$$n$$$ characters, each of them is either zero or one, describing the seating.
Output "Yes" (without quotation marks) if the seating is "maximal". Otherwise print "No". You are allowed to print letters in whatever case you'd like (uppercase or lowercase).
null
In sample case one the given seating is maximal. In sample case two the person at chair three has a neighbour to the right. In sample case three it is possible to seat yet another person into chair three.
[{"input": "3\n101", "output": "Yes"}, {"input": "4\n1011", "output": "No"}, {"input": "5\n10001", "output": "No"}]
1,200
["brute force", "constructive algorithms"]
55
[{"input": "3\r\n101\r\n", "output": "Yes\r\n"}, {"input": "4\r\n1011\r\n", "output": "No\r\n"}, {"input": "5\r\n10001\r\n", "output": "No\r\n"}, {"input": "1\r\n0\r\n", "output": "No\r\n"}, {"input": "1\r\n1\r\n", "output": "Yes\r\n"}, {"input": "100\r\n0101001010101001010010010101001010100101001001001010010101010010101001001010101001001001010100101010\r\n", "output": "Yes\r\n"}, {"input": "4\r\n0100\r\n", "output": "No\r\n"}, {"input": "42\r\n011000100101001001101011011010100010011010\r\n", "output": "No\r\n"}, {"input": "3\r\n001\r\n", "output": "No\r\n"}, {"input": "64\r\n1001001010010010100101010010010100100101001001001001010100101001\r\n", "output": "Yes\r\n"}, {"input": "3\r\n111\r\n", "output": "No\r\n"}, {"input": "4\r\n0000\r\n", "output": "No\r\n"}, {"input": "4\r\n0001\r\n", "output": "No\r\n"}, {"input": "4\r\n0010\r\n", "output": "No\r\n"}, {"input": "4\r\n0011\r\n", "output": "No\r\n"}, {"input": "4\r\n0101\r\n", "output": "Yes\r\n"}, {"input": "4\r\n0110\r\n", "output": "No\r\n"}, {"input": "4\r\n0111\r\n", "output": "No\r\n"}, {"input": "4\r\n1000\r\n", "output": "No\r\n"}, {"input": "4\r\n1001\r\n", "output": "Yes\r\n"}, {"input": "4\r\n1010\r\n", "output": "Yes\r\n"}, {"input": "4\r\n1100\r\n", "output": "No\r\n"}, {"input": "4\r\n1101\r\n", "output": "No\r\n"}, {"input": "4\r\n1110\r\n", "output": "No\r\n"}, {"input": "4\r\n1111\r\n", "output": "No\r\n"}, {"input": "2\r\n00\r\n", "output": "No\r\n"}, {"input": "2\r\n01\r\n", "output": "Yes\r\n"}, {"input": "2\r\n10\r\n", "output": "Yes\r\n"}, {"input": "2\r\n11\r\n", "output": "No\r\n"}, {"input": "3\r\n000\r\n", "output": "No\r\n"}, {"input": "3\r\n010\r\n", "output": "Yes\r\n"}, {"input": "3\r\n011\r\n", "output": "No\r\n"}, {"input": "3\r\n100\r\n", "output": "No\r\n"}, {"input": "3\r\n110\r\n", "output": "No\r\n"}, {"input": "100\r\n0111001010101110001100000010011000100101110010001100111110101110001110101010111000010010011000000110\r\n", "output": "No\r\n"}, {"input": "357\r\n100101010010010010010100101001001010101010100100100100101001010101001010010100101001010100101001010010100100101001010101010101001001010100101010010100101001010100100100101010010010010100101010010010101001010010010101001001010010010101010100100101010010100100101001010100101001010100101001010010010010100101001010100100100100100100100101010101010010010100101\r\n", "output": "Yes\r\n"}, {"input": "459\r\n000111000101010000100001001010111110011011010001100101111010111011101110111101111101100101100011011001100110001001111001101000111001011100110100011111011111000010000110010011100110011011111110011100001101001111000100111011001000001011111100110100001001001100101011100001110110100101011011110100100111101011000101110000100110100100010000000100001001111111000011101010010011001111010111001100000100111001010111011010000011000011100101101011101000011011000110011\r\n", "output": "No\r\n"}, {"input": "3\r\n001\r\n", "output": "No\r\n"}, {"input": "4\r\n0010\r\n", "output": "No\r\n"}, {"input": "3\r\n111\r\n", "output": "No\r\n"}, {"input": "4\r\n0110\r\n", "output": "No\r\n"}, {"input": "2\r\n11\r\n", "output": "No\r\n"}, {"input": "2\r\n10\r\n", "output": "Yes\r\n"}, {"input": "2\r\n01\r\n", "output": "Yes\r\n"}, {"input": "4\r\n1101\r\n", "output": "No\r\n"}, {"input": "2\r\n00\r\n", "output": "No\r\n"}, {"input": "4\r\n1111\r\n", "output": "No\r\n"}, {"input": "15\r\n000010101010000\r\n", "output": "No\r\n"}, {"input": "8\r\n10010100\r\n", "output": "No\r\n"}, {"input": "5\r\n00100\r\n", "output": "No\r\n"}, {"input": "6\r\n100100\r\n", "output": "No\r\n"}, {"input": "7\r\n1000001\r\n", "output": "No\r\n"}, {"input": "98\r\n10101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010\r\n", "output": "Yes\r\n"}]
false
stdio
null
true
982/A
982
A
Python 3
TESTS
49
124
0
44689901
n = int(input()) s = input() first = -1 second = -1 minimum = 9999999 maximum = 0 def check_val(value): if(value==2 or value==3): return True else: return False possible = ['1','01','10','010'] if(s in possible): print('YES') else: for i in range(n): if(int(s[i])==1): first = second second = i diff = second - first if(first>-1): if(diff>maximum): maximum = diff if(diff<minimum): minimum = diff if(check_val(maximum) and check_val(minimum)): print('YES') else: print('NO')
55
46
0
183823666
n=input() s='0'+input()+'0' print('No' if '000' in s or '11' in s else 'Yes')
Codeforces Round 484 (Div. 2)
CF
2,018
1
256
Row
You're given a row with $$$n$$$ chairs. We call a seating of people "maximal" if the two following conditions hold: 1. There are no neighbors adjacent to anyone seated. 2. It's impossible to seat one more person without violating the first rule. The seating is given as a string consisting of zeros and ones ($$$0$$$ means that the corresponding seat is empty, $$$1$$$ — occupied). The goal is to determine whether this seating is "maximal". Note that the first and last seats are not adjacent (if $$$n \ne 2$$$).
The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of chairs. The next line contains a string of $$$n$$$ characters, each of them is either zero or one, describing the seating.
Output "Yes" (without quotation marks) if the seating is "maximal". Otherwise print "No". You are allowed to print letters in whatever case you'd like (uppercase or lowercase).
null
In sample case one the given seating is maximal. In sample case two the person at chair three has a neighbour to the right. In sample case three it is possible to seat yet another person into chair three.
[{"input": "3\n101", "output": "Yes"}, {"input": "4\n1011", "output": "No"}, {"input": "5\n10001", "output": "No"}]
1,200
["brute force", "constructive algorithms"]
55
[{"input": "3\r\n101\r\n", "output": "Yes\r\n"}, {"input": "4\r\n1011\r\n", "output": "No\r\n"}, {"input": "5\r\n10001\r\n", "output": "No\r\n"}, {"input": "1\r\n0\r\n", "output": "No\r\n"}, {"input": "1\r\n1\r\n", "output": "Yes\r\n"}, {"input": "100\r\n0101001010101001010010010101001010100101001001001010010101010010101001001010101001001001010100101010\r\n", "output": "Yes\r\n"}, {"input": "4\r\n0100\r\n", "output": "No\r\n"}, {"input": "42\r\n011000100101001001101011011010100010011010\r\n", "output": "No\r\n"}, {"input": "3\r\n001\r\n", "output": "No\r\n"}, {"input": "64\r\n1001001010010010100101010010010100100101001001001001010100101001\r\n", "output": "Yes\r\n"}, {"input": "3\r\n111\r\n", "output": "No\r\n"}, {"input": "4\r\n0000\r\n", "output": "No\r\n"}, {"input": "4\r\n0001\r\n", "output": "No\r\n"}, {"input": "4\r\n0010\r\n", "output": "No\r\n"}, {"input": "4\r\n0011\r\n", "output": "No\r\n"}, {"input": "4\r\n0101\r\n", "output": "Yes\r\n"}, {"input": "4\r\n0110\r\n", "output": "No\r\n"}, {"input": "4\r\n0111\r\n", "output": "No\r\n"}, {"input": "4\r\n1000\r\n", "output": "No\r\n"}, {"input": "4\r\n1001\r\n", "output": "Yes\r\n"}, {"input": "4\r\n1010\r\n", "output": "Yes\r\n"}, {"input": "4\r\n1100\r\n", "output": "No\r\n"}, {"input": "4\r\n1101\r\n", "output": "No\r\n"}, {"input": "4\r\n1110\r\n", "output": "No\r\n"}, {"input": "4\r\n1111\r\n", "output": "No\r\n"}, {"input": "2\r\n00\r\n", "output": "No\r\n"}, {"input": "2\r\n01\r\n", "output": "Yes\r\n"}, {"input": "2\r\n10\r\n", "output": "Yes\r\n"}, {"input": "2\r\n11\r\n", "output": "No\r\n"}, {"input": "3\r\n000\r\n", "output": "No\r\n"}, {"input": "3\r\n010\r\n", "output": "Yes\r\n"}, {"input": "3\r\n011\r\n", "output": "No\r\n"}, {"input": "3\r\n100\r\n", "output": "No\r\n"}, {"input": "3\r\n110\r\n", "output": "No\r\n"}, {"input": "100\r\n0111001010101110001100000010011000100101110010001100111110101110001110101010111000010010011000000110\r\n", "output": "No\r\n"}, {"input": "357\r\n100101010010010010010100101001001010101010100100100100101001010101001010010100101001010100101001010010100100101001010101010101001001010100101010010100101001010100100100101010010010010100101010010010101001010010010101001001010010010101010100100101010010100100101001010100101001010100101001010010010010100101001010100100100100100100100101010101010010010100101\r\n", "output": "Yes\r\n"}, {"input": "459\r\n000111000101010000100001001010111110011011010001100101111010111011101110111101111101100101100011011001100110001001111001101000111001011100110100011111011111000010000110010011100110011011111110011100001101001111000100111011001000001011111100110100001001001100101011100001110110100101011011110100100111101011000101110000100110100100010000000100001001111111000011101010010011001111010111001100000100111001010111011010000011000011100101101011101000011011000110011\r\n", "output": "No\r\n"}, {"input": "3\r\n001\r\n", "output": "No\r\n"}, {"input": "4\r\n0010\r\n", "output": "No\r\n"}, {"input": "3\r\n111\r\n", "output": "No\r\n"}, {"input": "4\r\n0110\r\n", "output": "No\r\n"}, {"input": "2\r\n11\r\n", "output": "No\r\n"}, {"input": "2\r\n10\r\n", "output": "Yes\r\n"}, {"input": "2\r\n01\r\n", "output": "Yes\r\n"}, {"input": "4\r\n1101\r\n", "output": "No\r\n"}, {"input": "2\r\n00\r\n", "output": "No\r\n"}, {"input": "4\r\n1111\r\n", "output": "No\r\n"}, {"input": "15\r\n000010101010000\r\n", "output": "No\r\n"}, {"input": "8\r\n10010100\r\n", "output": "No\r\n"}, {"input": "5\r\n00100\r\n", "output": "No\r\n"}, {"input": "6\r\n100100\r\n", "output": "No\r\n"}, {"input": "7\r\n1000001\r\n", "output": "No\r\n"}, {"input": "98\r\n10101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010\r\n", "output": "Yes\r\n"}]
false
stdio
null
true
446/A
446
A
PyPy 3
TESTS
20
218
10,342,400
47505771
R = lambda: map(int, input().split()) n = int(input()) a = list(R()) dp = [1] * n res = 1 for i in range(n - 2, -1 , -1): dp[i] = dp[i + 1] + 1 if a[i] < a[i + 1] else 1 cnt = 1 for i in range(1, n - 1): if a[i + 1] > a[i - 1] + 1: res = max(res, cnt + dp[i + 1] + 1) else: res = max(res, cnt + 1, dp[i + 1] + 1) cnt = cnt + 1 if a[i] > a[i - 1] else 1 print(res)
92
186
19,660,800
143453579
n=int(input()) a=list(map(int,input().split())) l=[1] r=[1] for i in range(1,n): if a[i]>a[i-1]: l.append(l[-1]+1) else: l.append(1) for i in range(n-2,-1,-1): if a[i]<a[i+1]: r.append(r[-1]+1) else : r.append(1) r=r[::-1] mx=max(max(l),max(r)) if mx!=n: mx+=1 for i in range(1,n-1): if a[i-1]+2<=a[i+1]: mx=max(mx,1+l[i-1]+r[i+1]) print(mx)
Codeforces Round #FF (Div. 1)
CF
2,014
1
256
DZY Loves Sequences
DZY has a sequence a, consisting of n integers. We'll call a sequence ai, ai + 1, ..., aj (1 ≤ i ≤ j ≤ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment. Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing. You only need to output the length of the subsegment you find.
The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).
In a single line print the answer to the problem — the maximum length of the required subsegment.
null
You can choose subsegment a2, a3, a4, a5, a6 and change its 3rd element (that is a4) to 4.
[{"input": "6\n7 2 3 1 5 6", "output": "5"}]
1,600
["dp", "implementation", "two pointers"]
92
[{"input": "6\r\n7 2 3 1 5 6\r\n", "output": "5\r\n"}, {"input": "10\r\n424238336 649760493 681692778 714636916 719885387 804289384 846930887 957747794 596516650 189641422\r\n", "output": "9\r\n"}, {"input": "50\r\n804289384 846930887 681692778 714636916 957747794 424238336 719885387 649760493 596516650 189641422 25202363 350490028 783368691 102520060 44897764 967513927 365180541 540383427 304089173 303455737 35005212 521595369 294702568 726956430 336465783 861021531 59961394 89018457 101513930 125898168 131176230 145174068 233665124 278722863 315634023 369133070 468703136 628175012 635723059 653377374 656478043 801979803 859484422 914544920 608413785 756898538 734575199 973594325 149798316 38664371\r\n", "output": "19\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "2\r\n1000000000 1000000000\r\n", "output": "2\r\n"}, {"input": "5\r\n1 2 3 4 1\r\n", "output": "5\r\n"}, {"input": "10\r\n1 2 3 4 5 5 6 7 8 9\r\n", "output": "6\r\n"}, {"input": "5\r\n1 1 1 1 1\r\n", "output": "2\r\n"}, {"input": "5\r\n1 1 2 3 4\r\n", "output": "5\r\n"}, {"input": "5\r\n1 2 3 1 6\r\n", "output": "5\r\n"}, {"input": "1\r\n42\r\n", "output": "1\r\n"}, {"input": "5\r\n1 2 42 3 4\r\n", "output": "4\r\n"}, {"input": "5\r\n1 5 9 6 10\r\n", "output": "4\r\n"}, {"input": "5\r\n5 2 3 4 5\r\n", "output": "5\r\n"}, {"input": "3\r\n2 1 3\r\n", "output": "3\r\n"}, {"input": "5\r\n1 2 3 3 4\r\n", "output": "4\r\n"}, {"input": "8\r\n1 2 3 4 1 5 6 7\r\n", "output": "5\r\n"}, {"input": "1\r\n3\r\n", "output": "1\r\n"}, {"input": "3\r\n5 1 2\r\n", "output": "3\r\n"}, {"input": "4\r\n1 4 3 4\r\n", "output": "4\r\n"}, {"input": "6\r\n7 2 12 4 5 6\r\n", "output": "5\r\n"}, {"input": "6\r\n7 2 3 1 4 5\r\n", "output": "4\r\n"}, {"input": "6\r\n2 3 5 5 6 7\r\n", "output": "6\r\n"}, {"input": "5\r\n2 4 7 6 8\r\n", "output": "5\r\n"}, {"input": "3\r\n3 1 2\r\n", "output": "3\r\n"}, {"input": "3\r\n1 1 2\r\n", "output": "3\r\n"}, {"input": "2\r\n1 2\r\n", "output": "2\r\n"}, {"input": "5\r\n4 1 2 3 4\r\n", "output": "5\r\n"}, {"input": "20\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 3 4 5 6\r\n", "output": "7\r\n"}, {"input": "4\r\n1 2 1 3\r\n", "output": "3\r\n"}, {"input": "4\r\n4 3 1 2\r\n", "output": "3\r\n"}, {"input": "6\r\n1 2 2 3 4 5\r\n", "output": "5\r\n"}, {"input": "4\r\n1 1 1 2\r\n", "output": "3\r\n"}, {"input": "4\r\n5 1 2 3\r\n", "output": "4\r\n"}, {"input": "5\r\n9 1 2 3 4\r\n", "output": "5\r\n"}, {"input": "2\r\n1 1\r\n", "output": "2\r\n"}, {"input": "5\r\n1 3 2 4 5\r\n", "output": "4\r\n"}, {"input": "6\r\n1 2 1 2 4 5\r\n", "output": "5\r\n"}, {"input": "10\r\n1 1 5 3 2 9 9 7 7 6\r\n", "output": "3\r\n"}, {"input": "6\r\n1 2 3 100000 100 101\r\n", "output": "6\r\n"}, {"input": "4\r\n3 3 3 4\r\n", "output": "3\r\n"}, {"input": "3\r\n4 3 5\r\n", "output": "3\r\n"}, {"input": "5\r\n1 3 2 3 4\r\n", "output": "4\r\n"}, {"input": "10\r\n1 2 3 4 5 10 10 11 12 13\r\n", "output": "10\r\n"}, {"input": "7\r\n11 2 1 2 13 4 14\r\n", "output": "5\r\n"}, {"input": "3\r\n5 1 3\r\n", "output": "3\r\n"}, {"input": "4\r\n1 5 3 4\r\n", "output": "4\r\n"}, {"input": "10\r\n1 2 3 4 100 6 7 8 9 10\r\n", "output": "10\r\n"}, {"input": "3\r\n5 3 5\r\n", "output": "3\r\n"}, {"input": "5\r\n100 100 7 8 9\r\n", "output": "4\r\n"}, {"input": "5\r\n1 2 3 4 5\r\n", "output": "5\r\n"}, {"input": "5\r\n1 2 4 4 5\r\n", "output": "5\r\n"}, {"input": "6\r\n7 4 5 6 7 8\r\n", "output": "6\r\n"}, {"input": "9\r\n3 4 1 6 3 4 5 6 7\r\n", "output": "7\r\n"}, {"input": "3\r\n1000 1 2\r\n", "output": "3\r\n"}, {"input": "3\r\n20 1 9\r\n", "output": "3\r\n"}, {"input": "6\r\n7 2 3 1 4 6\r\n", "output": "4\r\n"}, {"input": "3\r\n100 5 10\r\n", "output": "3\r\n"}, {"input": "4\r\n2 2 2 3\r\n", "output": "3\r\n"}, {"input": "6\r\n4 2 8 1 2 5\r\n", "output": "4\r\n"}, {"input": "3\r\n25 1 6\r\n", "output": "3\r\n"}, {"input": "10\r\n17 99 23 72 78 36 5 43 95 9\r\n", "output": "5\r\n"}, {"input": "7\r\n21 16 22 21 11 13 19\r\n", "output": "4\r\n"}, {"input": "5\r\n1 2 5 3 4\r\n", "output": "4\r\n"}, {"input": "6\r\n2 2 2 3 4 5\r\n", "output": "5\r\n"}, {"input": "5\r\n1 3 1 2 3\r\n", "output": "4\r\n"}, {"input": "3\r\n81 33 64\r\n", "output": "3\r\n"}, {"input": "7\r\n14 3 3 19 13 19 15\r\n", "output": "4\r\n"}, {"input": "9\r\n1 2 3 4 5 42 7 8 9\r\n", "output": "9\r\n"}, {"input": "5\r\n2 3 7 5 6\r\n", "output": "5\r\n"}, {"input": "5\r\n1 3 3 4 5\r\n", "output": "5\r\n"}, {"input": "6\r\n1 5 4 3 4 5\r\n", "output": "4\r\n"}]
false
stdio
null
true
550/A
550
A
Python 3
TESTS
50
93
8,396,800
214120912
string = input() ans = [] count = 0 i = 1 sett =set() for i in range(1,len(string)): if (string[i-1] == "A" and string[i] == "B") or (string[i-1] == "B" and string[i] == "A"): ans.append([string[i-1]+string[i],i-1,i]) # print(ans) x =True result = "NO" a,b= -1,-1 for i in ans: if i[0]=="AB": a,b = i[1],i[2] break for i in ans: if i[0]=="BA": if i[1]!=b and i[2]!=a and a!=-1 and b!=-1: result = "YES" break a,b = -1,-1 for i in ans: if i[0]=="BA": a,b = i[1],i[2] for i in ans: if i[0]=="AB": if i[1]!=b and i[2]!=a and a!=-1 and b!=-1: result = "YES" print(result)
85
46
0
187124557
a=input() n=a.split("AB",1) r=a.split("BA",1) if len(n)>1 and "BA" in n[1]: print ("YES") elif len(r)>1 and "AB" in r[1]: print ("YES") else: print ("NO")
Codeforces Round 306 (Div. 2)
CF
2,015
2
256
Two Substrings
You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order).
The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters.
Print "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise.
null
In the first sample test, despite the fact that there are substrings "AB" and "BA", their occurrences overlap, so the answer is "NO". In the second sample test there are the following occurrences of the substrings: BACFAB. In the third sample test there is no substring "AB" nor substring "BA".
[{"input": "ABA", "output": "NO"}, {"input": "BACFAB", "output": "YES"}, {"input": "AXBYBXA", "output": "NO"}]
1,500
["brute force", "dp", "greedy", "implementation", "strings"]
85
[{"input": "ABA\r\n", "output": "NO\r\n"}, {"input": "BACFAB\r\n", "output": "YES\r\n"}, {"input": "AXBYBXA\r\n", "output": "NO\r\n"}, {"input": "ABABAB\r\n", "output": "YES\r\n"}, {"input": "BBBBBBBBBB\r\n", "output": "NO\r\n"}, {"input": "ABBA\r\n", "output": "YES\r\n"}, {"input": "ABAXXXAB\r\n", "output": "YES\r\n"}, {"input": "TESTABAXXABTEST\r\n", "output": "YES\r\n"}, {"input": "A\r\n", "output": "NO\r\n"}, {"input": "B\r\n", "output": "NO\r\n"}, {"input": "X\r\n", "output": "NO\r\n"}, {"input": "BA\r\n", "output": "NO\r\n"}, {"input": "AB\r\n", "output": "NO\r\n"}, {"input": "AA\r\n", "output": "NO\r\n"}, {"input": "BB\r\n", "output": "NO\r\n"}, {"input": "BAB\r\n", "output": "NO\r\n"}, {"input": "AAB\r\n", "output": "NO\r\n"}, {"input": "BAA\r\n", "output": "NO\r\n"}, {"input": "ABB\r\n", "output": "NO\r\n"}, {"input": "BBA\r\n", "output": "NO\r\n"}, {"input": "AAA\r\n", "output": "NO\r\n"}, {"input": "BBB\r\n", "output": "NO\r\n"}, {"input": "AXBXBXA\r\n", "output": "NO\r\n"}, {"input": "SKDSKDJABSDBADKFJDK\r\n", "output": "YES\r\n"}, {"input": "ABAXXBBXXAA\r\n", "output": "NO\r\n"}, {"input": "ABAB\r\n", "output": "NO\r\n"}, {"input": "BABA\r\n", "output": "NO\r\n"}, {"input": "AAAB\r\n", "output": "NO\r\n"}, {"input": "AAAA\r\n", "output": "NO\r\n"}, {"input": "AABA\r\n", "output": "NO\r\n"}, {"input": "ABAA\r\n", "output": "NO\r\n"}, {"input": "BAAA\r\n", "output": "NO\r\n"}, {"input": "AABB\r\n", "output": "NO\r\n"}, {"input": "BAAB\r\n", "output": "YES\r\n"}, {"input": "BBAA\r\n", "output": "NO\r\n"}, {"input": "BBBA\r\n", "output": "NO\r\n"}, {"input": "BBAB\r\n", "output": "NO\r\n"}, {"input": "BABB\r\n", "output": "NO\r\n"}, {"input": "ABBB\r\n", "output": "NO\r\n"}, {"input": "BBBB\r\n", "output": "NO\r\n"}, {"input": "BABAB\r\n", "output": "YES\r\n"}, {"input": "ABABA\r\n", "output": "YES\r\n"}, {"input": "AAABAAACBBBC\r\n", "output": "NO\r\n"}, {"input": "AABBBACBBBBBBAACBCCACBBAABBBBBCAACABAACABCACCBCBCCCBCBCABCBBCCBCBBAACBACAABACBBCACCBCCACCABBCBABBAAC\r\n", "output": "YES\r\n"}, {"input": "CBBABDDBBADAC\r\n", "output": "YES\r\n"}, {"input": "ABYFAB\r\n", "output": "NO\r\n"}, {"input": "BABXXAB\r\n", "output": "YES\r\n"}, {"input": "ABAXAB\r\n", "output": "YES\r\n"}, {"input": "ABABXAB\r\n", "output": "YES\r\n"}, {"input": "ABXABXABXABXABXBAXBAXBAXBA\r\n", "output": "YES\r\n"}, {"input": "QQQQQQQQQABABQQQQQQQQ\r\n", "output": "NO\r\n"}]
false
stdio
null
true
550/A
550
A
Python 3
TESTS
36
1,981
8,294,400
202949868
s = str(input()) ans = 'NO' if 'AB' not in s and 'BA' not in s: ans = 'NO' else: cur = 0 while cur < len(s): try: cur = s.index('AB', cur) s2 = s[0:cur]+s[cur+2::] if 'BA' in s2: ans = 'YES' break else: cur += 1 except: ans = 'NO' break print(ans)
85
46
0
189781943
s=input() u=s.find('AB') v=s.find('BA') print('YES'if(u+1 and s.find('BA',u+2)+1)or(v+1 and s.find('AB',v+2)+1)else'NO')
Codeforces Round 306 (Div. 2)
CF
2,015
2
256
Two Substrings
You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order).
The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters.
Print "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise.
null
In the first sample test, despite the fact that there are substrings "AB" and "BA", their occurrences overlap, so the answer is "NO". In the second sample test there are the following occurrences of the substrings: BACFAB. In the third sample test there is no substring "AB" nor substring "BA".
[{"input": "ABA", "output": "NO"}, {"input": "BACFAB", "output": "YES"}, {"input": "AXBYBXA", "output": "NO"}]
1,500
["brute force", "dp", "greedy", "implementation", "strings"]
85
[{"input": "ABA\r\n", "output": "NO\r\n"}, {"input": "BACFAB\r\n", "output": "YES\r\n"}, {"input": "AXBYBXA\r\n", "output": "NO\r\n"}, {"input": "ABABAB\r\n", "output": "YES\r\n"}, {"input": "BBBBBBBBBB\r\n", "output": "NO\r\n"}, {"input": "ABBA\r\n", "output": "YES\r\n"}, {"input": "ABAXXXAB\r\n", "output": "YES\r\n"}, {"input": "TESTABAXXABTEST\r\n", "output": "YES\r\n"}, {"input": "A\r\n", "output": "NO\r\n"}, {"input": "B\r\n", "output": "NO\r\n"}, {"input": "X\r\n", "output": "NO\r\n"}, {"input": "BA\r\n", "output": "NO\r\n"}, {"input": "AB\r\n", "output": "NO\r\n"}, {"input": "AA\r\n", "output": "NO\r\n"}, {"input": "BB\r\n", "output": "NO\r\n"}, {"input": "BAB\r\n", "output": "NO\r\n"}, {"input": "AAB\r\n", "output": "NO\r\n"}, {"input": "BAA\r\n", "output": "NO\r\n"}, {"input": "ABB\r\n", "output": "NO\r\n"}, {"input": "BBA\r\n", "output": "NO\r\n"}, {"input": "AAA\r\n", "output": "NO\r\n"}, {"input": "BBB\r\n", "output": "NO\r\n"}, {"input": "AXBXBXA\r\n", "output": "NO\r\n"}, {"input": "SKDSKDJABSDBADKFJDK\r\n", "output": "YES\r\n"}, {"input": "ABAXXBBXXAA\r\n", "output": "NO\r\n"}, {"input": "ABAB\r\n", "output": "NO\r\n"}, {"input": "BABA\r\n", "output": "NO\r\n"}, {"input": "AAAB\r\n", "output": "NO\r\n"}, {"input": "AAAA\r\n", "output": "NO\r\n"}, {"input": "AABA\r\n", "output": "NO\r\n"}, {"input": "ABAA\r\n", "output": "NO\r\n"}, {"input": "BAAA\r\n", "output": "NO\r\n"}, {"input": "AABB\r\n", "output": "NO\r\n"}, {"input": "BAAB\r\n", "output": "YES\r\n"}, {"input": "BBAA\r\n", "output": "NO\r\n"}, {"input": "BBBA\r\n", "output": "NO\r\n"}, {"input": "BBAB\r\n", "output": "NO\r\n"}, {"input": "BABB\r\n", "output": "NO\r\n"}, {"input": "ABBB\r\n", "output": "NO\r\n"}, {"input": "BBBB\r\n", "output": "NO\r\n"}, {"input": "BABAB\r\n", "output": "YES\r\n"}, {"input": "ABABA\r\n", "output": "YES\r\n"}, {"input": "AAABAAACBBBC\r\n", "output": "NO\r\n"}, {"input": "AABBBACBBBBBBAACBCCACBBAABBBBBCAACABAACABCACCBCBCCCBCBCABCBBCCBCBBAACBACAABACBBCACCBCCACCABBCBABBAAC\r\n", "output": "YES\r\n"}, {"input": "CBBABDDBBADAC\r\n", "output": "YES\r\n"}, {"input": "ABYFAB\r\n", "output": "NO\r\n"}, {"input": "BABXXAB\r\n", "output": "YES\r\n"}, {"input": "ABAXAB\r\n", "output": "YES\r\n"}, {"input": "ABABXAB\r\n", "output": "YES\r\n"}, {"input": "ABXABXABXABXABXBAXBAXBAXBA\r\n", "output": "YES\r\n"}, {"input": "QQQQQQQQQABABQQQQQQQQ\r\n", "output": "NO\r\n"}]
false
stdio
null
true
550/A
550
A
PyPy 3-64
TESTS
63
62
1,945,600
207965257
#import io, os, sys #input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline #print = lambda x: sys.stdout.write(str(x) + "\n") II = lambda: int(input()) MII = lambda: map(int, input().split()) LMII = lambda: list(MII()) #SLMII = lambda: sorted(LMII()) s = input() n = len(s) sABBA = ['AB', 'BA'] i = j = 0 sBAAB = ['BA', 'AB'] k = l = 0 found = False ni = {(0,0): (0,1), (0,1): (1,0), (1,0): (1,1), (1,1): (-1,-1)} for ch in s: if ch == sABBA[i][j]: i, j = ni[(i,j)] else: j = 0 if ch == sBAAB[k][l]: k, l = ni[(k,l)] else: l = 0 if i == -1 or k == -1: found = True break print('YES' if found else 'NO')
85
46
0
190125776
str=input() x1=str.find("AB") x2=str.find("BA",x1+2) y1=str.find("BA") y2=str.find("AB",y1+2) if (x1!=-1 and x2!=-1) or (y1!=-1 and y2!=-1): print("YES") else: print("NO")
Codeforces Round 306 (Div. 2)
CF
2,015
2
256
Two Substrings
You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order).
The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters.
Print "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise.
null
In the first sample test, despite the fact that there are substrings "AB" and "BA", their occurrences overlap, so the answer is "NO". In the second sample test there are the following occurrences of the substrings: BACFAB. In the third sample test there is no substring "AB" nor substring "BA".
[{"input": "ABA", "output": "NO"}, {"input": "BACFAB", "output": "YES"}, {"input": "AXBYBXA", "output": "NO"}]
1,500
["brute force", "dp", "greedy", "implementation", "strings"]
85
[{"input": "ABA\r\n", "output": "NO\r\n"}, {"input": "BACFAB\r\n", "output": "YES\r\n"}, {"input": "AXBYBXA\r\n", "output": "NO\r\n"}, {"input": "ABABAB\r\n", "output": "YES\r\n"}, {"input": "BBBBBBBBBB\r\n", "output": "NO\r\n"}, {"input": "ABBA\r\n", "output": "YES\r\n"}, {"input": "ABAXXXAB\r\n", "output": "YES\r\n"}, {"input": "TESTABAXXABTEST\r\n", "output": "YES\r\n"}, {"input": "A\r\n", "output": "NO\r\n"}, {"input": "B\r\n", "output": "NO\r\n"}, {"input": "X\r\n", "output": "NO\r\n"}, {"input": "BA\r\n", "output": "NO\r\n"}, {"input": "AB\r\n", "output": "NO\r\n"}, {"input": "AA\r\n", "output": "NO\r\n"}, {"input": "BB\r\n", "output": "NO\r\n"}, {"input": "BAB\r\n", "output": "NO\r\n"}, {"input": "AAB\r\n", "output": "NO\r\n"}, {"input": "BAA\r\n", "output": "NO\r\n"}, {"input": "ABB\r\n", "output": "NO\r\n"}, {"input": "BBA\r\n", "output": "NO\r\n"}, {"input": "AAA\r\n", "output": "NO\r\n"}, {"input": "BBB\r\n", "output": "NO\r\n"}, {"input": "AXBXBXA\r\n", "output": "NO\r\n"}, {"input": "SKDSKDJABSDBADKFJDK\r\n", "output": "YES\r\n"}, {"input": "ABAXXBBXXAA\r\n", "output": "NO\r\n"}, {"input": "ABAB\r\n", "output": "NO\r\n"}, {"input": "BABA\r\n", "output": "NO\r\n"}, {"input": "AAAB\r\n", "output": "NO\r\n"}, {"input": "AAAA\r\n", "output": "NO\r\n"}, {"input": "AABA\r\n", "output": "NO\r\n"}, {"input": "ABAA\r\n", "output": "NO\r\n"}, {"input": "BAAA\r\n", "output": "NO\r\n"}, {"input": "AABB\r\n", "output": "NO\r\n"}, {"input": "BAAB\r\n", "output": "YES\r\n"}, {"input": "BBAA\r\n", "output": "NO\r\n"}, {"input": "BBBA\r\n", "output": "NO\r\n"}, {"input": "BBAB\r\n", "output": "NO\r\n"}, {"input": "BABB\r\n", "output": "NO\r\n"}, {"input": "ABBB\r\n", "output": "NO\r\n"}, {"input": "BBBB\r\n", "output": "NO\r\n"}, {"input": "BABAB\r\n", "output": "YES\r\n"}, {"input": "ABABA\r\n", "output": "YES\r\n"}, {"input": "AAABAAACBBBC\r\n", "output": "NO\r\n"}, {"input": "AABBBACBBBBBBAACBCCACBBAABBBBBCAACABAACABCACCBCBCCCBCBCABCBBCCBCBBAACBACAABACBBCACCBCCACCABBCBABBAAC\r\n", "output": "YES\r\n"}, {"input": "CBBABDDBBADAC\r\n", "output": "YES\r\n"}, {"input": "ABYFAB\r\n", "output": "NO\r\n"}, {"input": "BABXXAB\r\n", "output": "YES\r\n"}, {"input": "ABAXAB\r\n", "output": "YES\r\n"}, {"input": "ABABXAB\r\n", "output": "YES\r\n"}, {"input": "ABXABXABXABXABXBAXBAXBAXBA\r\n", "output": "YES\r\n"}, {"input": "QQQQQQQQQABABQQQQQQQQ\r\n", "output": "NO\r\n"}]
false
stdio
null
true
446/A
446
A
PyPy 3
TESTS
20
202
12,390,400
110830424
n=int(input()) arr=list(map(int,input().split())) pre,pre1=[1],[1] for i in range(1,n): if arr[i]>arr[i-1]: pre.append(pre[-1]+1) else: pre.append(1) arr.reverse() for i in range(1,n): if arr[i]<arr[i-1]: pre1.append(pre1[-1]+1) else: pre1.append(1) pre1.reverse() arr.reverse() ans=1 for i in range(1,n-1): if arr[i-1]<arr[i+1]-1: ans=max(ans,pre[i-1]+pre1[i+1]+1) #print(*[i,pre[i-1]+pre1[i+1]+1]) else: ans=max(ans,max(pre[i-1],pre1[i+1])+1) print(ans)
92
187
15,257,600
115861712
n=int(input()) a=[0]+list(map(int,input().split()))+[0] l=[] st=1 for i in range(2,n+2): if a[i]<=a[i-1]: l.append([st,i-1]) st=i if len(l)==1: print(n) exit() mx=0 for i in range(1,len(l)): lf=l[i-1][1] rt=l[i][0] ln1=l[i][1]-l[i][0]+1 ln2=l[i-1][1]-l[i-1][0]+1 if a[rt+1]-a[lf]>1 or a[rt]-a[lf-1]>1: tm=ln1+ln2 else: tm=max(ln1,ln2)+1 mx=max(mx,tm) print(mx)
Codeforces Round #FF (Div. 1)
CF
2,014
1
256
DZY Loves Sequences
DZY has a sequence a, consisting of n integers. We'll call a sequence ai, ai + 1, ..., aj (1 ≤ i ≤ j ≤ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment. Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing. You only need to output the length of the subsegment you find.
The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).
In a single line print the answer to the problem — the maximum length of the required subsegment.
null
You can choose subsegment a2, a3, a4, a5, a6 and change its 3rd element (that is a4) to 4.
[{"input": "6\n7 2 3 1 5 6", "output": "5"}]
1,600
["dp", "implementation", "two pointers"]
92
[{"input": "6\r\n7 2 3 1 5 6\r\n", "output": "5\r\n"}, {"input": "10\r\n424238336 649760493 681692778 714636916 719885387 804289384 846930887 957747794 596516650 189641422\r\n", "output": "9\r\n"}, {"input": "50\r\n804289384 846930887 681692778 714636916 957747794 424238336 719885387 649760493 596516650 189641422 25202363 350490028 783368691 102520060 44897764 967513927 365180541 540383427 304089173 303455737 35005212 521595369 294702568 726956430 336465783 861021531 59961394 89018457 101513930 125898168 131176230 145174068 233665124 278722863 315634023 369133070 468703136 628175012 635723059 653377374 656478043 801979803 859484422 914544920 608413785 756898538 734575199 973594325 149798316 38664371\r\n", "output": "19\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "2\r\n1000000000 1000000000\r\n", "output": "2\r\n"}, {"input": "5\r\n1 2 3 4 1\r\n", "output": "5\r\n"}, {"input": "10\r\n1 2 3 4 5 5 6 7 8 9\r\n", "output": "6\r\n"}, {"input": "5\r\n1 1 1 1 1\r\n", "output": "2\r\n"}, {"input": "5\r\n1 1 2 3 4\r\n", "output": "5\r\n"}, {"input": "5\r\n1 2 3 1 6\r\n", "output": "5\r\n"}, {"input": "1\r\n42\r\n", "output": "1\r\n"}, {"input": "5\r\n1 2 42 3 4\r\n", "output": "4\r\n"}, {"input": "5\r\n1 5 9 6 10\r\n", "output": "4\r\n"}, {"input": "5\r\n5 2 3 4 5\r\n", "output": "5\r\n"}, {"input": "3\r\n2 1 3\r\n", "output": "3\r\n"}, {"input": "5\r\n1 2 3 3 4\r\n", "output": "4\r\n"}, {"input": "8\r\n1 2 3 4 1 5 6 7\r\n", "output": "5\r\n"}, {"input": "1\r\n3\r\n", "output": "1\r\n"}, {"input": "3\r\n5 1 2\r\n", "output": "3\r\n"}, {"input": "4\r\n1 4 3 4\r\n", "output": "4\r\n"}, {"input": "6\r\n7 2 12 4 5 6\r\n", "output": "5\r\n"}, {"input": "6\r\n7 2 3 1 4 5\r\n", "output": "4\r\n"}, {"input": "6\r\n2 3 5 5 6 7\r\n", "output": "6\r\n"}, {"input": "5\r\n2 4 7 6 8\r\n", "output": "5\r\n"}, {"input": "3\r\n3 1 2\r\n", "output": "3\r\n"}, {"input": "3\r\n1 1 2\r\n", "output": "3\r\n"}, {"input": "2\r\n1 2\r\n", "output": "2\r\n"}, {"input": "5\r\n4 1 2 3 4\r\n", "output": "5\r\n"}, {"input": "20\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 3 4 5 6\r\n", "output": "7\r\n"}, {"input": "4\r\n1 2 1 3\r\n", "output": "3\r\n"}, {"input": "4\r\n4 3 1 2\r\n", "output": "3\r\n"}, {"input": "6\r\n1 2 2 3 4 5\r\n", "output": "5\r\n"}, {"input": "4\r\n1 1 1 2\r\n", "output": "3\r\n"}, {"input": "4\r\n5 1 2 3\r\n", "output": "4\r\n"}, {"input": "5\r\n9 1 2 3 4\r\n", "output": "5\r\n"}, {"input": "2\r\n1 1\r\n", "output": "2\r\n"}, {"input": "5\r\n1 3 2 4 5\r\n", "output": "4\r\n"}, {"input": "6\r\n1 2 1 2 4 5\r\n", "output": "5\r\n"}, {"input": "10\r\n1 1 5 3 2 9 9 7 7 6\r\n", "output": "3\r\n"}, {"input": "6\r\n1 2 3 100000 100 101\r\n", "output": "6\r\n"}, {"input": "4\r\n3 3 3 4\r\n", "output": "3\r\n"}, {"input": "3\r\n4 3 5\r\n", "output": "3\r\n"}, {"input": "5\r\n1 3 2 3 4\r\n", "output": "4\r\n"}, {"input": "10\r\n1 2 3 4 5 10 10 11 12 13\r\n", "output": "10\r\n"}, {"input": "7\r\n11 2 1 2 13 4 14\r\n", "output": "5\r\n"}, {"input": "3\r\n5 1 3\r\n", "output": "3\r\n"}, {"input": "4\r\n1 5 3 4\r\n", "output": "4\r\n"}, {"input": "10\r\n1 2 3 4 100 6 7 8 9 10\r\n", "output": "10\r\n"}, {"input": "3\r\n5 3 5\r\n", "output": "3\r\n"}, {"input": "5\r\n100 100 7 8 9\r\n", "output": "4\r\n"}, {"input": "5\r\n1 2 3 4 5\r\n", "output": "5\r\n"}, {"input": "5\r\n1 2 4 4 5\r\n", "output": "5\r\n"}, {"input": "6\r\n7 4 5 6 7 8\r\n", "output": "6\r\n"}, {"input": "9\r\n3 4 1 6 3 4 5 6 7\r\n", "output": "7\r\n"}, {"input": "3\r\n1000 1 2\r\n", "output": "3\r\n"}, {"input": "3\r\n20 1 9\r\n", "output": "3\r\n"}, {"input": "6\r\n7 2 3 1 4 6\r\n", "output": "4\r\n"}, {"input": "3\r\n100 5 10\r\n", "output": "3\r\n"}, {"input": "4\r\n2 2 2 3\r\n", "output": "3\r\n"}, {"input": "6\r\n4 2 8 1 2 5\r\n", "output": "4\r\n"}, {"input": "3\r\n25 1 6\r\n", "output": "3\r\n"}, {"input": "10\r\n17 99 23 72 78 36 5 43 95 9\r\n", "output": "5\r\n"}, {"input": "7\r\n21 16 22 21 11 13 19\r\n", "output": "4\r\n"}, {"input": "5\r\n1 2 5 3 4\r\n", "output": "4\r\n"}, {"input": "6\r\n2 2 2 3 4 5\r\n", "output": "5\r\n"}, {"input": "5\r\n1 3 1 2 3\r\n", "output": "4\r\n"}, {"input": "3\r\n81 33 64\r\n", "output": "3\r\n"}, {"input": "7\r\n14 3 3 19 13 19 15\r\n", "output": "4\r\n"}, {"input": "9\r\n1 2 3 4 5 42 7 8 9\r\n", "output": "9\r\n"}, {"input": "5\r\n2 3 7 5 6\r\n", "output": "5\r\n"}, {"input": "5\r\n1 3 3 4 5\r\n", "output": "5\r\n"}, {"input": "6\r\n1 5 4 3 4 5\r\n", "output": "4\r\n"}]
false
stdio
null
true
837/D
837
D
PyPy 3
TESTS
23
124
20,070,400
132496966
def main(): n,k = map(int, input().split()) A = list(map(int,input().split())) A2 = [0]*n A5 = [0]*n for i, a in enumerate(A): while a%2 == 0: A2[i] += 1 a //= 2 while a%5 == 0: A5[i] += 1 a //= 5 #print(A2) #print(A5) INF = 10**18 dp = [[INF]*65 for i in range(65)] dp[0][0] = 0 for i in range(n): a, b = A2[i], A5[i] for x in reversed(range(65)): for y in reversed(range(65)): if dp[x][y] == INF: continue if x+a <= 64 and y+b <= 64: dp[x+a][y+b] = min(dp[x+a][y+b], dp[x][y]+1) ans = 0 for x in range(65): for y in range(65): if dp[x][y] <= k: ans = max(ans, min(x, y)) print(ans) if __name__ == '__main__': main()
99
623
11,161,600
172361559
import sys input = sys.stdin.readline n, k = map(int, input().split()) a = list(map(int, input().split())) sc5, c2, c5 = 0, [], [] for i in range(n): num, c = a[i], 0 while num % 5 == 0: num //= 5 c += 1 sc5 += c c5.append(c) c = 0 while num % 2 == 0: num //= 2 c += 1 c2.append(c) dp = [[-1] * (sc5 + 1) for _ in range(k + 1)] dp[0][0] = 0 for i in range(n): for j in range(min(i, k - 1), -1, -1): for l in range(c5[i], sc5 + 1): if dp[j][l - c5[i]] != -1: dp[j + 1][l] = max(dp[j + 1][l], dp[j][l - c5[i]] + c2[i]) res = [min(l, dp[k][l]) for l in range(sc5 + 1) if dp[k][l] != -1] print(max(res))
Educational Codeforces Round 26
ICPC
2,017
2
256
Round Subset
Let's call the roundness of the number the number of zeros to which it ends. You have an array of n numbers. You need to choose a subset of exactly k numbers so that the roundness of the product of the selected numbers will be maximum possible.
The first line contains two integer numbers n and k (1 ≤ n ≤ 200, 1 ≤ k ≤ n). The second line contains n space-separated integer numbers a1, a2, ..., an (1 ≤ ai ≤ 1018).
Print maximal roundness of product of the chosen subset of length k.
null
In the first example there are 3 subsets of 2 numbers. [50, 4] has product 200 with roundness 2, [4, 20] — product 80, roundness 1, [50, 20] — product 1000, roundness 3. In the second example subset [15, 16, 25] has product 6000, roundness 3. In the third example all subsets has product with roundness 0.
[{"input": "3 2\n50 4 20", "output": "3"}, {"input": "5 3\n15 16 3 25 9", "output": "3"}, {"input": "3 3\n9 77 13", "output": "0"}]
2,100
["dp", "math"]
99
[{"input": "3 2\r\n50 4 20\r\n", "output": "3\r\n"}, {"input": "5 3\r\n15 16 3 25 9\r\n", "output": "3\r\n"}, {"input": "3 3\r\n9 77 13\r\n", "output": "0\r\n"}, {"input": "1 1\r\n200000000\r\n", "output": "8\r\n"}, {"input": "1 1\r\n3\r\n", "output": "0\r\n"}, {"input": "3 1\r\n1000000000000000000 800000000000000000 625\r\n", "output": "18\r\n"}, {"input": "20 13\r\n93050001 1 750000001 950000001 160250001 482000001 145875001 900000001 500000001 513300001 313620001 724750001 205800001 400000001 800000001 175000001 875000001 852686005 868880001 342500001\r\n", "output": "0\r\n"}, {"input": "5 3\r\n1360922189858001 5513375057164001 4060879738933651 3260997351273601 5540397778584001\r\n", "output": "0\r\n"}, {"input": "5 3\r\n670206146698567481 75620705254979501 828058059097865201 67124386759325201 946737848872942801\r\n", "output": "0\r\n"}, {"input": "5 4\r\n539134530963895499 265657472022483040 798956216114326361 930406714691011229 562844921643925634\r\n", "output": "1\r\n"}, {"input": "200 10\r\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "200 50\r\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "200 100\r\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "200 200\r\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "5 2\r\n625 5 100 16 10\r\n", "output": "4\r\n"}, {"input": "5 2\r\n64 32 16 8 3125\r\n", "output": "5\r\n"}, {"input": "2 2\r\n2199023255552 11920928955078125\r\n", "output": "23\r\n"}, {"input": "1 1\r\n500\r\n", "output": "2\r\n"}, {"input": "3 1\r\n125 10 8\r\n", "output": "1\r\n"}, {"input": "7 5\r\n50 312500 10000 1250 2000000 250 1250000\r\n", "output": "18\r\n"}, {"input": "4 2\r\n3125 1000 1000 32\r\n", "output": "6\r\n"}, {"input": "3 1\r\n4 10 25\r\n", "output": "1\r\n"}, {"input": "3 1\r\n16 10 75\r\n", "output": "1\r\n"}, {"input": "3 2\r\n100 5120 19531250\r\n", "output": "11\r\n"}, {"input": "4 2\r\n16 200 500 625\r\n", "output": "5\r\n"}, {"input": "7 4\r\n5000 1250 100 200 10 5000 640\r\n", "output": "13\r\n"}, {"input": "2 1\r\n125 10\r\n", "output": "1\r\n"}, {"input": "3 2\r\n16 625 10\r\n", "output": "4\r\n"}, {"input": "4 2\r\n10 4 25 5\r\n", "output": "2\r\n"}, {"input": "2 1\r\n10 25\r\n", "output": "1\r\n"}, {"input": "3 1\r\n10 16 625\r\n", "output": "1\r\n"}, {"input": "4 2\r\n10 200 625 16\r\n", "output": "4\r\n"}, {"input": "3 1\r\n1000 16 625\r\n", "output": "3\r\n"}, {"input": "3 2\r\n6250 100 160\r\n", "output": "6\r\n"}, {"input": "3 2\r\n1250 100 80\r\n", "output": "5\r\n"}, {"input": "3 2\r\n1024 1000 9765625\r\n", "output": "10\r\n"}, {"input": "5 2\r\n125 10 10 3 3\r\n", "output": "2\r\n"}, {"input": "4 2\r\n31250 200 500 320\r\n", "output": "7\r\n"}, {"input": "3 1\r\n16 10 125\r\n", "output": "1\r\n"}, {"input": "5 3\r\n125 8 1000 100 100\r\n", "output": "7\r\n"}, {"input": "3 2\r\n25 4 10\r\n", "output": "2\r\n"}, {"input": "3 2\r\n125 8 10\r\n", "output": "3\r\n"}, {"input": "6 2\r\n125 125 100 100 8 8\r\n", "output": "4\r\n"}, {"input": "3 1\r\n32 3125 10\r\n", "output": "1\r\n"}, {"input": "5 3\r\n4 5 125 16 1\r\n", "output": "4\r\n"}, {"input": "7 4\r\n5 125 2 5 2 5 20\r\n", "output": "4\r\n"}, {"input": "200 200\r\n5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 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 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 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2\r\n", "output": "100\r\n"}, {"input": "2 1\r\n200 250\r\n", "output": "2\r\n"}, {"input": "5 3\r\n500 10 250 20 8\r\n", "output": "6\r\n"}, {"input": "4 2\r\n2 2 25 1\r\n", "output": "1\r\n"}, {"input": "2 2\r\n16 25\r\n", "output": "2\r\n"}]
false
stdio
null
true
847/E
847
E
Python 3
TESTS
16
124
0
44568676
from math import ceil def solve(field,size,answer): left = 0 star_pos = -1 rstar_pos = -1 p_pos = -1 distance = 0 cost = 0 for i in range(size): #print(p_pos,star_pos) if field[i] == "*": if star_pos != -1: if p_pos != -1: if star_pos < p_pos and i > p_pos: #*...P.*.* star on both side of P if min(2*(p_pos-star_pos)+(i-p_pos),2*(i-p_pos)+\ (p_pos-star_pos)) > answer: star_pos = i p_pos = -1 continue elif i-p_pos > answer: #..P..*P when star on the right of P star_pos = i p_pos = -1 continue else: star_pos = i if field[i] == "P": if p_pos != -1: distance = 0 star_pos = -1 if star_pos != -1: #print("star_pos:",star_pos,"current pos",i) if i - star_pos > answer: return -1 p_pos = i if p_pos == -1: return -1 return 1 if __name__ == "__main__": size = int(input().strip()) field = input().strip() first = 1 last = 2*size best = last + 1 while first <= last: mid = int(first + (last - first)/2) if solve(field,size,mid) == -1: first = mid + 1 else: best = mid last = mid - 1 print(best)
77
124
7,372,800
121279454
''' pacman->food pacman->food->food :hmmge: larges smaller val smallest larger val ''' def check(mid,p,f): u=0 for i in range(len(p)): left=p[i]-f[u] left=max(0,left) if mid<left: return 0 right=max(mid-2*left,0) right=max(right,(mid-left)//2) while u<len(f) and (f[u]<=p[i]+right): #bich mai fas ra u+=1 if u==len(f): return 1 return 0 def f(s,n): p=[] f=[] for i in range(n): if s[i]=="*": f.append(i) if s[i]=="P": p.append(i) lo=0 hi=10**9 while lo<=hi: mid=(lo+hi)//2 if check(mid,p,f): hi=mid-1 else: lo=mid+1 return lo n=int(input()) s=list(input().strip()) print(f(s,n))
2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest, qualification stage (Online Mirror, ACM-ICPC Rules, Teams Preferred)
ICPC
2,017
1
256
Packmen
A game field is a strip of 1 × n square cells. In some cells there are Packmen, in some cells — asterisks, other cells are empty. Packman can move to neighboring cell in 1 time unit. If there is an asterisk in the target cell then Packman eats it. Packman doesn't spend any time to eat an asterisk. In the initial moment of time all Packmen begin to move. Each Packman can change direction of its move unlimited number of times, but it is not allowed to go beyond the boundaries of the game field. Packmen do not interfere with the movement of other packmen; in one cell there can be any number of packmen moving in any directions. Your task is to determine minimum possible time after which Packmen can eat all the asterisks.
The first line contains a single integer n (2 ≤ n ≤ 105) — the length of the game field. The second line contains the description of the game field consisting of n symbols. If there is symbol '.' in position i — the cell i is empty. If there is symbol '*' in position i — in the cell i contains an asterisk. If there is symbol 'P' in position i — Packman is in the cell i. It is guaranteed that on the game field there is at least one Packman and at least one asterisk.
Print minimum possible time after which Packmen can eat all asterisks.
null
In the first example Packman in position 4 will move to the left and will eat asterisk in position 1. He will spend 3 time units on it. During the same 3 time units Packman in position 6 will eat both of neighboring with it asterisks. For example, it can move to the left and eat asterisk in position 5 (in 1 time unit) and then move from the position 5 to the right and eat asterisk in the position 7 (in 2 time units). So in 3 time units Packmen will eat all asterisks on the game field. In the second example Packman in the position 4 will move to the left and after 2 time units will eat asterisks in positions 3 and 2. Packmen in positions 5 and 8 will move to the right and in 2 time units will eat asterisks in positions 7 and 10, respectively. So 2 time units is enough for Packmen to eat all asterisks on the game field.
[{"input": "7\n*..P*P*", "output": "3"}, {"input": "10\n.**PP.*P.*", "output": "2"}]
1,800
["binary search", "dp"]
77
[{"input": "7\r\n*..P*P*\r\n", "output": "3\r\n"}, {"input": "10\r\n.**PP.*P.*\r\n", "output": "2\r\n"}, {"input": "19\r\n**P.*..*..P..*.*P**\r\n", "output": "7\r\n"}, {"input": "12\r\nP**.*P*P*P**\r\n", "output": "3\r\n"}, {"input": "17\r\n.*P*P**P**.**P...\r\n", "output": "3\r\n"}, {"input": "58\r\n..P.P*.P*.P...PPP...P*....*..*.**......*P.*P.....**P...*P*\r\n", "output": "9\r\n"}, {"input": "10\r\n..P*.P.*.*\r\n", "output": "4\r\n"}, {"input": "10\r\n***.*.*..P\r\n", "output": "9\r\n"}, {"input": "15\r\nP***..PPP..P*.P\r\n", "output": "3\r\n"}, {"input": "15\r\n*.*....*P......\r\n", "output": "8\r\n"}, {"input": "20\r\n.P**P**P**PP.PP**PP*\r\n", "output": "2\r\n"}, {"input": "20\r\n.....*.**..........P\r\n", "output": "14\r\n"}, {"input": "25\r\nP*P*P.**.PPPP.*.P.*..P...\r\n", "output": "2\r\n"}, {"input": "25\r\n...*..**..*.....*..*...P.\r\n", "output": "20\r\n"}, {"input": "30\r\n*P.*...*.**..P**...***.*...**.\r\n", "output": "15\r\n"}, {"input": "30\r\n.*...*.......................P\r\n", "output": "28\r\n"}, {"input": "35\r\n..PP.P....*PP.*.PPPP.*P.P.PPPP.*.P.\r\n", "output": "2\r\n"}, {"input": "35\r\n....*..*.*.*.....*.*..P*...*...*...\r\n", "output": "36\r\n"}, {"input": "40\r\n...**P*P*...P.*PP***.*..P..**.**PP**.*.*\r\n", "output": "6\r\n"}, {"input": "40\r\nP*....*.*....*...*..*.......*...**..***.\r\n", "output": "38\r\n"}, {"input": "45\r\nP.P*..P....*P.*PP*PP*.**P...PP*PP*.P.P..PP.PP\r\n", "output": "2\r\n"}, {"input": "45\r\n*.*.*..*.*.**.*..**..*.....**.**P....*****.**\r\n", "output": "56\r\n"}, {"input": "50\r\n*PP....PPPP*....*P*P..PPPPPP...***P*P.........PP..\r\n", "output": "3\r\n"}, {"input": "50\r\n*..***.*.****.*....P*.**...***.......**....*.***..\r\n", "output": "66\r\n"}, {"input": "55\r\n......P.*.....P*.*P....*..P*.P.P....**....*..........*.\r\n", "output": "22\r\n"}, {"input": "55\r\n*.....*.*..**..*...***..**.**.*.*.P..*.*.**...**.*..*.*\r\n", "output": "74\r\n"}, {"input": "60\r\n.P...P.PPP.P....P...PP.*......**...P.*.P.P*P.*...P*P..P.P..P\r\n", "output": "5\r\n"}, {"input": "60\r\n..*....*...***....P...........*............*....**....*...**\r\n", "output": "73\r\n"}, {"input": "65\r\n......PP..PP**.***.*.P.P..PP.....**PP*PP.....P..P*PP.*.*P..P*P*..\r\n", "output": "5\r\n"}, {"input": "65\r\n.....*..*....*.*......P....*.....**....*.........*...............\r\n", "output": "61\r\n"}, {"input": "70\r\n*P.*..**P*P.....*PP.......*..PP...P...P.**..P.*......*P*.**.P..*P.....\r\n", "output": "4\r\n"}, {"input": "70\r\n*..***.**..**....***........*.**...*...**.**..*.......**P*..*.......**\r\n", "output": "82\r\n"}, {"input": "75\r\n..***P*.**.P.**P.**.***.P*..**P.P*.P*.**.....*PP..P***....**PPP..**P..P..P*\r\n", "output": "6\r\n"}, {"input": "75\r\n...*.*...**.*..*..*P.*......*...*....**..*..**..*..*......*....*..*.*......\r\n", "output": "81\r\n"}, {"input": "80\r\n**.P...*....*P...*....P............**....*..*.*....*..........*.*.*.............\r\n", "output": "44\r\n"}, {"input": "80\r\n*..**..*...*....*.*.**.*.*..*..P..*..**.**..*..**.*.*.*.*.***...*.*..**.*....**.\r\n", "output": "109\r\n"}, {"input": "85\r\n.*.....*.....**..........*P*..........*.........*...*..*...****..*..*P*..*..P.***...*\r\n", "output": "31\r\n"}, {"input": "85\r\n*...*.*.....*..*.*.*.*.**....*...*...*.*..*..*.*......**...*.*.P..*.....*.*....*.*...\r\n", "output": "99\r\n"}, {"input": "90\r\n......P.*.PPP...*.P.**P..*.*.*..*P**PP**..***.PPP....P..**P*.*.*..*.P*P.*PP*.....P.*.**P**\r\n", "output": "5\r\n"}, {"input": "90\r\n.**.*..*........****...*.*..*.*.*...........***..*.*.*.P**...**..*..**..***.....*..*.**...\r\n", "output": "116\r\n"}, {"input": "95\r\n.*..P****....****.*.***P..*.*.**P..*.***.PP.**.**....*****P**..*..*..***...*P..P****.*.P...*..*\r\n", "output": "12\r\n"}, {"input": "95\r\n.*.***...*...P**...*.**.*..*.*..*...****..*.*.*..*.****.*....*...*..*..**.**.********..*.*...**\r\n", "output": "105\r\n"}, {"input": "100\r\n.....PP..*...P.P.PPPP.....P*...P..P.*....*P.*..*..*...........P..PP..P.....*P.....P......*.*..PP....\r\n", "output": "8\r\n"}, {"input": "100\r\n........*P**...*.*.*.*..*.*.**...*.*.....*..............*..**.............*..........*.*..*..*.**...\r\n", "output": "89\r\n"}, {"input": "100\r\nPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP*PPPPPPPPPPPPP\r\n", "output": "1\r\n"}, {"input": "100\r\n************************************************************P***************************************\r\n", "output": "138\r\n"}, {"input": "100\r\n..........*...............................P..........P.......P.......P..*............P.......**.....\r\n", "output": "32\r\n"}, {"input": "100\r\n.....................P............P....P....*.............*.......**.P.................P........*...\r\n", "output": "11\r\n"}, {"input": "100\r\n......*....*...P*P*.....**.......P.P..........*........*....PP.................P......*.P.P.......*.\r\n", "output": "11\r\n"}, {"input": "100\r\n........................................P..........*...................P.........*.....P.......P....\r\n", "output": "11\r\n"}, {"input": "100\r\n**.**.**.........P*..P**.*.******.***.....***..*.*P.*.***.*.*..*.***..*********.*...***..*..*...P*..\r\n", "output": "26\r\n"}, {"input": "100\r\n.PPP....PPPP.*...P...PPP..*P...P.*P.PP..P.P...PPPPP..PP.P..P..P..P...P.......P..PP..P..PPPPPP.P.PPPP\r\n", "output": "2\r\n"}, {"input": "100\r\n...............................................*P.........*..P.........P.P....P..P.................*\r\n", "output": "18\r\n"}, {"input": "100\r\n...........*.........*.....P...P...............P........P..........P.....*......*.....*P...*.P......\r\n", "output": "16\r\n"}, {"input": "2\r\n*P\r\n", "output": "1\r\n"}, {"input": "2\r\nP*\r\n", "output": "1\r\n"}]
false
stdio
null
true
837/D
837
D
PyPy 3
TESTS
13
233
5,632,000
57330897
n, k = map(int, input().split()) a = list(map(int, input().split())) b = [[0 for i in range(2)] for j in range(n)] for i in range(n): while a[i] % 2 == 0: b[i][0] += 1 a[i] >>= 1 while a[i] % 5 == 0: b[i][1] += 1 a[i] //= 5 cur2, cur5 = 0, 0 for i in range(k): cur2 += b[i][0] cur5 += b[i][1] dp = min(cur2, cur5) for i in range(k, n): cur2, cur5 = b[i][0], b[i][1] c = [0] * i for j in range(1, k): mcur = i for h in range(i - 1, -1, -1): if not c[h] and (mcur == i or min(cur2 + b[h][0], cur5 + b[h][1]) >= min(cur2 + b[mcur][0], cur5 + b[mcur][1])): mcur = h c[mcur] = 1 cur2 += b[mcur][0] cur5 += b[mcur][1] dp = max(dp, min(cur2, cur5)) print(dp)
99
857
11,878,400
172414804
import sys import math import collections import random from heapq import heappush, heappop from functools import reduce input = sys.stdin.readline ints = lambda: list(map(int, input().split())) def printQry(a, b) -> None: sa = str(a) sb = str(b) print(f"? {sa} {sb}", flush = True) def printAns(ans) -> None: s = str(ans) print(f"! {s}", flush = True) def calFactor(x, m) -> int: cnt = 0 while x % m == 0: cnt += 1 x //= m return cnt def solve() -> None: n, k = map(int, input().split()) arr = ints() dp = [[-1] * (k * 25 + 1) for _ in range(k + 1)] dp[0][0] = 0 for a in arr: cnt2 = cnt5 = 0 while not a & 1: cnt2 += 1 a >>= 1 while a % 5 == 0: cnt5 += 1 a //= 5 for i in range(k, 0, -1): for j in range(k * 25, cnt5 - 1, -1): if dp[i-1][j-cnt5] >= 0: dp[i][j] = max(dp[i][j], dp[i-1][j-cnt5] + cnt2) ans = 0 for i, cnt2 in enumerate(dp[k]): ans = max(ans, min(i, cnt2)) print(ans) t = 1 for _ in range(t): solve()
Educational Codeforces Round 26
ICPC
2,017
2
256
Round Subset
Let's call the roundness of the number the number of zeros to which it ends. You have an array of n numbers. You need to choose a subset of exactly k numbers so that the roundness of the product of the selected numbers will be maximum possible.
The first line contains two integer numbers n and k (1 ≤ n ≤ 200, 1 ≤ k ≤ n). The second line contains n space-separated integer numbers a1, a2, ..., an (1 ≤ ai ≤ 1018).
Print maximal roundness of product of the chosen subset of length k.
null
In the first example there are 3 subsets of 2 numbers. [50, 4] has product 200 with roundness 2, [4, 20] — product 80, roundness 1, [50, 20] — product 1000, roundness 3. In the second example subset [15, 16, 25] has product 6000, roundness 3. In the third example all subsets has product with roundness 0.
[{"input": "3 2\n50 4 20", "output": "3"}, {"input": "5 3\n15 16 3 25 9", "output": "3"}, {"input": "3 3\n9 77 13", "output": "0"}]
2,100
["dp", "math"]
99
[{"input": "3 2\r\n50 4 20\r\n", "output": "3\r\n"}, {"input": "5 3\r\n15 16 3 25 9\r\n", "output": "3\r\n"}, {"input": "3 3\r\n9 77 13\r\n", "output": "0\r\n"}, {"input": "1 1\r\n200000000\r\n", "output": "8\r\n"}, {"input": "1 1\r\n3\r\n", "output": "0\r\n"}, {"input": "3 1\r\n1000000000000000000 800000000000000000 625\r\n", "output": "18\r\n"}, {"input": "20 13\r\n93050001 1 750000001 950000001 160250001 482000001 145875001 900000001 500000001 513300001 313620001 724750001 205800001 400000001 800000001 175000001 875000001 852686005 868880001 342500001\r\n", "output": "0\r\n"}, {"input": "5 3\r\n1360922189858001 5513375057164001 4060879738933651 3260997351273601 5540397778584001\r\n", "output": "0\r\n"}, {"input": "5 3\r\n670206146698567481 75620705254979501 828058059097865201 67124386759325201 946737848872942801\r\n", "output": "0\r\n"}, {"input": "5 4\r\n539134530963895499 265657472022483040 798956216114326361 930406714691011229 562844921643925634\r\n", "output": "1\r\n"}, {"input": "200 10\r\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "200 50\r\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "200 100\r\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "200 200\r\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "5 2\r\n625 5 100 16 10\r\n", "output": "4\r\n"}, {"input": "5 2\r\n64 32 16 8 3125\r\n", "output": "5\r\n"}, {"input": "2 2\r\n2199023255552 11920928955078125\r\n", "output": "23\r\n"}, {"input": "1 1\r\n500\r\n", "output": "2\r\n"}, {"input": "3 1\r\n125 10 8\r\n", "output": "1\r\n"}, {"input": "7 5\r\n50 312500 10000 1250 2000000 250 1250000\r\n", "output": "18\r\n"}, {"input": "4 2\r\n3125 1000 1000 32\r\n", "output": "6\r\n"}, {"input": "3 1\r\n4 10 25\r\n", "output": "1\r\n"}, {"input": "3 1\r\n16 10 75\r\n", "output": "1\r\n"}, {"input": "3 2\r\n100 5120 19531250\r\n", "output": "11\r\n"}, {"input": "4 2\r\n16 200 500 625\r\n", "output": "5\r\n"}, {"input": "7 4\r\n5000 1250 100 200 10 5000 640\r\n", "output": "13\r\n"}, {"input": "2 1\r\n125 10\r\n", "output": "1\r\n"}, {"input": "3 2\r\n16 625 10\r\n", "output": "4\r\n"}, {"input": "4 2\r\n10 4 25 5\r\n", "output": "2\r\n"}, {"input": "2 1\r\n10 25\r\n", "output": "1\r\n"}, {"input": "3 1\r\n10 16 625\r\n", "output": "1\r\n"}, {"input": "4 2\r\n10 200 625 16\r\n", "output": "4\r\n"}, {"input": "3 1\r\n1000 16 625\r\n", "output": "3\r\n"}, {"input": "3 2\r\n6250 100 160\r\n", "output": "6\r\n"}, {"input": "3 2\r\n1250 100 80\r\n", "output": "5\r\n"}, {"input": "3 2\r\n1024 1000 9765625\r\n", "output": "10\r\n"}, {"input": "5 2\r\n125 10 10 3 3\r\n", "output": "2\r\n"}, {"input": "4 2\r\n31250 200 500 320\r\n", "output": "7\r\n"}, {"input": "3 1\r\n16 10 125\r\n", "output": "1\r\n"}, {"input": "5 3\r\n125 8 1000 100 100\r\n", "output": "7\r\n"}, {"input": "3 2\r\n25 4 10\r\n", "output": "2\r\n"}, {"input": "3 2\r\n125 8 10\r\n", "output": "3\r\n"}, {"input": "6 2\r\n125 125 100 100 8 8\r\n", "output": "4\r\n"}, {"input": "3 1\r\n32 3125 10\r\n", "output": "1\r\n"}, {"input": "5 3\r\n4 5 125 16 1\r\n", "output": "4\r\n"}, {"input": "7 4\r\n5 125 2 5 2 5 20\r\n", "output": "4\r\n"}, {"input": "200 200\r\n5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 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 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 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2\r\n", "output": "100\r\n"}, {"input": "2 1\r\n200 250\r\n", "output": "2\r\n"}, {"input": "5 3\r\n500 10 250 20 8\r\n", "output": "6\r\n"}, {"input": "4 2\r\n2 2 25 1\r\n", "output": "1\r\n"}, {"input": "2 2\r\n16 25\r\n", "output": "2\r\n"}]
false
stdio
null
true
681/A
681
A
Python 3
TESTS
8
62
0
116228551
n=int(input()) k=[] z=0 for i in range(n): k+=input().split() for i in range(1,len(k)): if (k[i])>="2400" and i%3!=0: z+=1 if z>=2 and k[i-1]<k[i]: print("YES") break else: z=0 else: print("NO")
60
31
0
149208261
con=True for _ in [0]*int(input()): a=[*map(int,input().split()[1:])] if a[0]>=2400 and a[1]>a[0] and con:print('YES');con=False if con:print('NO')
Codeforces Round 357 (Div. 2)
CF
2,016
1
256
A Good Contest
Codeforces user' handle color depends on his rating — it is red if his rating is greater or equal to 2400; it is orange if his rating is less than 2400 but greater or equal to 2200, etc. Each time participant takes part in a rated contest, his rating is changed depending on his performance. Anton wants the color of his handle to become red. He considers his performance in the rated contest to be good if he outscored some participant, whose handle was colored red before the contest and his rating has increased after it. Anton has written a program that analyses contest results and determines whether he performed good or not. Are you able to do the same?
The first line of the input contains a single integer n (1 ≤ n ≤ 100) — the number of participants Anton has outscored in this contest . The next n lines describe participants results: the i-th of them consists of a participant handle namei and two integers beforei and afteri ( - 4000 ≤ beforei, afteri ≤ 4000) — participant's rating before and after the contest, respectively. Each handle is a non-empty string, consisting of no more than 10 characters, which might be lowercase and uppercase English letters, digits, characters «_» and «-» characters. It is guaranteed that all handles are distinct.
Print «YES» (quotes for clarity), if Anton has performed good in the contest and «NO» (quotes for clarity) otherwise.
null
In the first sample, Anton has outscored user with handle Burunduk1, whose handle was colored red before the contest and his rating has increased after the contest. In the second sample, Applejack's rating has not increased after the contest, while both Fluttershy's and Pinkie_Pie's handles were not colored red before the contest.
[{"input": "3\nBurunduk1 2526 2537\nBudAlNik 2084 2214\nsubscriber 2833 2749", "output": "YES"}, {"input": "3\nApplejack 2400 2400\nFluttershy 2390 2431\nPinkie_Pie -2500 -2450", "output": "NO"}]
800
["implementation"]
60
[{"input": "3\r\nBurunduk1 2526 2537\r\nBudAlNik 2084 2214\r\nsubscriber 2833 2749\r\n", "output": "YES"}, {"input": "3\r\nApplejack 2400 2400\r\nFluttershy 2390 2431\r\nPinkie_Pie -2500 -2450\r\n", "output": "NO"}, {"input": "1\r\nDb -3373 3591\r\n", "output": "NO"}, {"input": "5\r\nQ2bz 960 2342\r\nhmX 2710 -1348\r\ngbAe -1969 -963\r\nE -160 196\r\npsi 2665 -3155\r\n", "output": "NO"}, {"input": "9\r\nmwAz9lQ 1786 -1631\r\nnYgYFXZQfY -1849 -1775\r\nKU4jF -1773 -3376\r\nopR 3752 2931\r\nGl -1481 -1002\r\nR -1111 3778\r\n0i9B21DC 3650 289\r\nQ8L2dS0 358 -3305\r\ng -2662 3968\r\n", "output": "NO"}, {"input": "5\r\nzMSBcOUf -2883 -2238\r\nYN -3314 -1480\r\nfHpuccQn06 -1433 -589\r\naM1NVEPQi 399 3462\r\n_L 2516 -3290\r\n", "output": "NO"}, {"input": "1\r\na 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nfucker 4000 4000\r\n", "output": "NO"}, {"input": "1\r\nJora 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nACA 2400 2420\r\n", "output": "YES"}, {"input": "1\r\nAca 2400 2420\r\n", "output": "YES"}, {"input": "1\r\nSub_d 2401 2402\r\n", "output": "YES"}, {"input": "2\r\nHack 2400 2401\r\nDum 1243 555\r\n", "output": "YES"}, {"input": "1\r\nXXX 2400 2500\r\n", "output": "YES"}, {"input": "1\r\nfucker 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nX 2400 2500\r\n", "output": "YES"}, {"input": "1\r\nvineet 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nabc 2400 2500\r\n", "output": "YES"}, {"input": "1\r\naaaaa 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nhoge 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nInfinity 2400 2468\r\n", "output": "YES"}, {"input": "1\r\nBurunduk1 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nFuck 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nfuck 2400 2401\r\n", "output": "YES"}, {"input": "3\r\nApplejack 2400 2401\r\nFluttershy 2390 2431\r\nPinkie_Pie -2500 -2450\r\n", "output": "YES"}, {"input": "1\r\nalex 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nA 2400 2401\r\n", "output": "YES"}, {"input": "1\r\na 2400 2455\r\n", "output": "YES"}, {"input": "1\r\nlol 2400 2401\r\n", "output": "YES"}, {"input": "2\r\nBurunduk1 2400 2537\r\nBudAlNik 2084 2214\r\n", "output": "YES"}, {"input": "1\r\naaaaaa 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nBurunduk1 2400 2500\r\n", "output": "YES"}, {"input": "1\r\nds 2400 2410\r\n", "output": "YES"}, {"input": "1\r\nas 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nabc 2400 2401\r\n", "output": "YES"}, {"input": "3\r\nBudAlNik 2084 2214\r\nsubscriber 2833 2749\r\nBurunduk1 2526 2537\r\n", "output": "YES"}, {"input": "1\r\ncaonima 2400 2401\r\n", "output": "YES"}, {"input": "1\r\narr 2400 2500\r\n", "output": "YES"}, {"input": "1\r\nx 2400 2401\r\n", "output": "YES"}, {"input": "1\r\narrr 2400 2500\r\n", "output": "YES"}, {"input": "1\r\nabc 2400 2405\r\n", "output": "YES"}, {"input": "3\r\nBurunduk1 2400 2420\r\nBudAlNik 2084 2214\r\nsubscriber 2833 2749\r\n", "output": "YES"}, {"input": "1\r\nBurunduk1 2400 2537\r\n", "output": "YES"}, {"input": "1\r\nHELLO 2400 2401\r\n", "output": "YES"}, {"input": "1\r\neatmore 2400 2500\r\n", "output": "YES"}, {"input": "1\r\nb 2400 2401\r\n", "output": "YES"}, {"input": "3\r\nBurunduk1 2400 2537\r\nBudAlNik 2084 2214\r\nsubscriber 2833 2749\r\n", "output": "YES"}, {"input": "1\r\nApplejack 2400 2410\r\n", "output": "YES"}, {"input": "1\r\nabacaba 2400 2451\r\n", "output": "YES"}, {"input": "1\r\nrekt_n00b 2500 2600\r\n", "output": "YES"}]
false
stdio
null
true
681/A
681
A
Python 3
TESTS
8
108
0
90044033
n = int(input()) a = [input().split() for i in range(n)] k = 0 for j in range(n): if int(a[j][1]) >= 2400 and a[j][2] > a[j][1]: k += 1 break if k > 0: print('YES') else: print('NO')
60
31
0
221573962
ctr=0 for i in range(int(input())): cid,bef,aft=map(str,input().split()) bef=int(bef) aft=int(aft) if bef>=2400: if aft-bef>0: ctr+=1 if ctr>0: print("YES") else: print("NO")
Codeforces Round 357 (Div. 2)
CF
2,016
1
256
A Good Contest
Codeforces user' handle color depends on his rating — it is red if his rating is greater or equal to 2400; it is orange if his rating is less than 2400 but greater or equal to 2200, etc. Each time participant takes part in a rated contest, his rating is changed depending on his performance. Anton wants the color of his handle to become red. He considers his performance in the rated contest to be good if he outscored some participant, whose handle was colored red before the contest and his rating has increased after it. Anton has written a program that analyses contest results and determines whether he performed good or not. Are you able to do the same?
The first line of the input contains a single integer n (1 ≤ n ≤ 100) — the number of participants Anton has outscored in this contest . The next n lines describe participants results: the i-th of them consists of a participant handle namei and two integers beforei and afteri ( - 4000 ≤ beforei, afteri ≤ 4000) — participant's rating before and after the contest, respectively. Each handle is a non-empty string, consisting of no more than 10 characters, which might be lowercase and uppercase English letters, digits, characters «_» and «-» characters. It is guaranteed that all handles are distinct.
Print «YES» (quotes for clarity), if Anton has performed good in the contest and «NO» (quotes for clarity) otherwise.
null
In the first sample, Anton has outscored user with handle Burunduk1, whose handle was colored red before the contest and his rating has increased after the contest. In the second sample, Applejack's rating has not increased after the contest, while both Fluttershy's and Pinkie_Pie's handles were not colored red before the contest.
[{"input": "3\nBurunduk1 2526 2537\nBudAlNik 2084 2214\nsubscriber 2833 2749", "output": "YES"}, {"input": "3\nApplejack 2400 2400\nFluttershy 2390 2431\nPinkie_Pie -2500 -2450", "output": "NO"}]
800
["implementation"]
60
[{"input": "3\r\nBurunduk1 2526 2537\r\nBudAlNik 2084 2214\r\nsubscriber 2833 2749\r\n", "output": "YES"}, {"input": "3\r\nApplejack 2400 2400\r\nFluttershy 2390 2431\r\nPinkie_Pie -2500 -2450\r\n", "output": "NO"}, {"input": "1\r\nDb -3373 3591\r\n", "output": "NO"}, {"input": "5\r\nQ2bz 960 2342\r\nhmX 2710 -1348\r\ngbAe -1969 -963\r\nE -160 196\r\npsi 2665 -3155\r\n", "output": "NO"}, {"input": "9\r\nmwAz9lQ 1786 -1631\r\nnYgYFXZQfY -1849 -1775\r\nKU4jF -1773 -3376\r\nopR 3752 2931\r\nGl -1481 -1002\r\nR -1111 3778\r\n0i9B21DC 3650 289\r\nQ8L2dS0 358 -3305\r\ng -2662 3968\r\n", "output": "NO"}, {"input": "5\r\nzMSBcOUf -2883 -2238\r\nYN -3314 -1480\r\nfHpuccQn06 -1433 -589\r\naM1NVEPQi 399 3462\r\n_L 2516 -3290\r\n", "output": "NO"}, {"input": "1\r\na 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nfucker 4000 4000\r\n", "output": "NO"}, {"input": "1\r\nJora 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nACA 2400 2420\r\n", "output": "YES"}, {"input": "1\r\nAca 2400 2420\r\n", "output": "YES"}, {"input": "1\r\nSub_d 2401 2402\r\n", "output": "YES"}, {"input": "2\r\nHack 2400 2401\r\nDum 1243 555\r\n", "output": "YES"}, {"input": "1\r\nXXX 2400 2500\r\n", "output": "YES"}, {"input": "1\r\nfucker 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nX 2400 2500\r\n", "output": "YES"}, {"input": "1\r\nvineet 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nabc 2400 2500\r\n", "output": "YES"}, {"input": "1\r\naaaaa 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nhoge 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nInfinity 2400 2468\r\n", "output": "YES"}, {"input": "1\r\nBurunduk1 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nFuck 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nfuck 2400 2401\r\n", "output": "YES"}, {"input": "3\r\nApplejack 2400 2401\r\nFluttershy 2390 2431\r\nPinkie_Pie -2500 -2450\r\n", "output": "YES"}, {"input": "1\r\nalex 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nA 2400 2401\r\n", "output": "YES"}, {"input": "1\r\na 2400 2455\r\n", "output": "YES"}, {"input": "1\r\nlol 2400 2401\r\n", "output": "YES"}, {"input": "2\r\nBurunduk1 2400 2537\r\nBudAlNik 2084 2214\r\n", "output": "YES"}, {"input": "1\r\naaaaaa 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nBurunduk1 2400 2500\r\n", "output": "YES"}, {"input": "1\r\nds 2400 2410\r\n", "output": "YES"}, {"input": "1\r\nas 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nabc 2400 2401\r\n", "output": "YES"}, {"input": "3\r\nBudAlNik 2084 2214\r\nsubscriber 2833 2749\r\nBurunduk1 2526 2537\r\n", "output": "YES"}, {"input": "1\r\ncaonima 2400 2401\r\n", "output": "YES"}, {"input": "1\r\narr 2400 2500\r\n", "output": "YES"}, {"input": "1\r\nx 2400 2401\r\n", "output": "YES"}, {"input": "1\r\narrr 2400 2500\r\n", "output": "YES"}, {"input": "1\r\nabc 2400 2405\r\n", "output": "YES"}, {"input": "3\r\nBurunduk1 2400 2420\r\nBudAlNik 2084 2214\r\nsubscriber 2833 2749\r\n", "output": "YES"}, {"input": "1\r\nBurunduk1 2400 2537\r\n", "output": "YES"}, {"input": "1\r\nHELLO 2400 2401\r\n", "output": "YES"}, {"input": "1\r\neatmore 2400 2500\r\n", "output": "YES"}, {"input": "1\r\nb 2400 2401\r\n", "output": "YES"}, {"input": "3\r\nBurunduk1 2400 2537\r\nBudAlNik 2084 2214\r\nsubscriber 2833 2749\r\n", "output": "YES"}, {"input": "1\r\nApplejack 2400 2410\r\n", "output": "YES"}, {"input": "1\r\nabacaba 2400 2451\r\n", "output": "YES"}, {"input": "1\r\nrekt_n00b 2500 2600\r\n", "output": "YES"}]
false
stdio
null
true
681/A
681
A
Python 3
TESTS
8
93
0
58093743
n=int(input()) f=0 for i in range(n): l=list(map(str,input().split())) if l[1]>='2400' and l[2]>l[1]: f=1 break if f==1: print("YES") else: print("NO")
60
46
0
136167929
n = int(input()) result = 0 for i in range(n): a, b, c = input().split() if int(c) > int(b) >= 2400: result = 1 print("YES" if result == 1 else "NO")
Codeforces Round 357 (Div. 2)
CF
2,016
1
256
A Good Contest
Codeforces user' handle color depends on his rating — it is red if his rating is greater or equal to 2400; it is orange if his rating is less than 2400 but greater or equal to 2200, etc. Each time participant takes part in a rated contest, his rating is changed depending on his performance. Anton wants the color of his handle to become red. He considers his performance in the rated contest to be good if he outscored some participant, whose handle was colored red before the contest and his rating has increased after it. Anton has written a program that analyses contest results and determines whether he performed good or not. Are you able to do the same?
The first line of the input contains a single integer n (1 ≤ n ≤ 100) — the number of participants Anton has outscored in this contest . The next n lines describe participants results: the i-th of them consists of a participant handle namei and two integers beforei and afteri ( - 4000 ≤ beforei, afteri ≤ 4000) — participant's rating before and after the contest, respectively. Each handle is a non-empty string, consisting of no more than 10 characters, which might be lowercase and uppercase English letters, digits, characters «_» and «-» characters. It is guaranteed that all handles are distinct.
Print «YES» (quotes for clarity), if Anton has performed good in the contest and «NO» (quotes for clarity) otherwise.
null
In the first sample, Anton has outscored user with handle Burunduk1, whose handle was colored red before the contest and his rating has increased after the contest. In the second sample, Applejack's rating has not increased after the contest, while both Fluttershy's and Pinkie_Pie's handles were not colored red before the contest.
[{"input": "3\nBurunduk1 2526 2537\nBudAlNik 2084 2214\nsubscriber 2833 2749", "output": "YES"}, {"input": "3\nApplejack 2400 2400\nFluttershy 2390 2431\nPinkie_Pie -2500 -2450", "output": "NO"}]
800
["implementation"]
60
[{"input": "3\r\nBurunduk1 2526 2537\r\nBudAlNik 2084 2214\r\nsubscriber 2833 2749\r\n", "output": "YES"}, {"input": "3\r\nApplejack 2400 2400\r\nFluttershy 2390 2431\r\nPinkie_Pie -2500 -2450\r\n", "output": "NO"}, {"input": "1\r\nDb -3373 3591\r\n", "output": "NO"}, {"input": "5\r\nQ2bz 960 2342\r\nhmX 2710 -1348\r\ngbAe -1969 -963\r\nE -160 196\r\npsi 2665 -3155\r\n", "output": "NO"}, {"input": "9\r\nmwAz9lQ 1786 -1631\r\nnYgYFXZQfY -1849 -1775\r\nKU4jF -1773 -3376\r\nopR 3752 2931\r\nGl -1481 -1002\r\nR -1111 3778\r\n0i9B21DC 3650 289\r\nQ8L2dS0 358 -3305\r\ng -2662 3968\r\n", "output": "NO"}, {"input": "5\r\nzMSBcOUf -2883 -2238\r\nYN -3314 -1480\r\nfHpuccQn06 -1433 -589\r\naM1NVEPQi 399 3462\r\n_L 2516 -3290\r\n", "output": "NO"}, {"input": "1\r\na 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nfucker 4000 4000\r\n", "output": "NO"}, {"input": "1\r\nJora 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nACA 2400 2420\r\n", "output": "YES"}, {"input": "1\r\nAca 2400 2420\r\n", "output": "YES"}, {"input": "1\r\nSub_d 2401 2402\r\n", "output": "YES"}, {"input": "2\r\nHack 2400 2401\r\nDum 1243 555\r\n", "output": "YES"}, {"input": "1\r\nXXX 2400 2500\r\n", "output": "YES"}, {"input": "1\r\nfucker 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nX 2400 2500\r\n", "output": "YES"}, {"input": "1\r\nvineet 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nabc 2400 2500\r\n", "output": "YES"}, {"input": "1\r\naaaaa 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nhoge 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nInfinity 2400 2468\r\n", "output": "YES"}, {"input": "1\r\nBurunduk1 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nFuck 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nfuck 2400 2401\r\n", "output": "YES"}, {"input": "3\r\nApplejack 2400 2401\r\nFluttershy 2390 2431\r\nPinkie_Pie -2500 -2450\r\n", "output": "YES"}, {"input": "1\r\nalex 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nA 2400 2401\r\n", "output": "YES"}, {"input": "1\r\na 2400 2455\r\n", "output": "YES"}, {"input": "1\r\nlol 2400 2401\r\n", "output": "YES"}, {"input": "2\r\nBurunduk1 2400 2537\r\nBudAlNik 2084 2214\r\n", "output": "YES"}, {"input": "1\r\naaaaaa 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nBurunduk1 2400 2500\r\n", "output": "YES"}, {"input": "1\r\nds 2400 2410\r\n", "output": "YES"}, {"input": "1\r\nas 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nabc 2400 2401\r\n", "output": "YES"}, {"input": "3\r\nBudAlNik 2084 2214\r\nsubscriber 2833 2749\r\nBurunduk1 2526 2537\r\n", "output": "YES"}, {"input": "1\r\ncaonima 2400 2401\r\n", "output": "YES"}, {"input": "1\r\narr 2400 2500\r\n", "output": "YES"}, {"input": "1\r\nx 2400 2401\r\n", "output": "YES"}, {"input": "1\r\narrr 2400 2500\r\n", "output": "YES"}, {"input": "1\r\nabc 2400 2405\r\n", "output": "YES"}, {"input": "3\r\nBurunduk1 2400 2420\r\nBudAlNik 2084 2214\r\nsubscriber 2833 2749\r\n", "output": "YES"}, {"input": "1\r\nBurunduk1 2400 2537\r\n", "output": "YES"}, {"input": "1\r\nHELLO 2400 2401\r\n", "output": "YES"}, {"input": "1\r\neatmore 2400 2500\r\n", "output": "YES"}, {"input": "1\r\nb 2400 2401\r\n", "output": "YES"}, {"input": "3\r\nBurunduk1 2400 2537\r\nBudAlNik 2084 2214\r\nsubscriber 2833 2749\r\n", "output": "YES"}, {"input": "1\r\nApplejack 2400 2410\r\n", "output": "YES"}, {"input": "1\r\nabacaba 2400 2451\r\n", "output": "YES"}, {"input": "1\r\nrekt_n00b 2500 2600\r\n", "output": "YES"}]
false
stdio
null
true
681/A
681
A
PyPy 3
TESTS
8
140
0
71018318
k=0 for i in range(int(input())): a,x,y=input().split() if int(x)>=2400 and y>x: k=1 if k==1: print("YES") else: print("NO")
60
46
0
139355211
t=int(input()) check=1 for i in range(t): s=input() a=s.split(' ') if int(a[1])>=2400 and int(a[1])<int(a[2]): check=0 if check==1: print('NO') else: print('YES')
Codeforces Round 357 (Div. 2)
CF
2,016
1
256
A Good Contest
Codeforces user' handle color depends on his rating — it is red if his rating is greater or equal to 2400; it is orange if his rating is less than 2400 but greater or equal to 2200, etc. Each time participant takes part in a rated contest, his rating is changed depending on his performance. Anton wants the color of his handle to become red. He considers his performance in the rated contest to be good if he outscored some participant, whose handle was colored red before the contest and his rating has increased after it. Anton has written a program that analyses contest results and determines whether he performed good or not. Are you able to do the same?
The first line of the input contains a single integer n (1 ≤ n ≤ 100) — the number of participants Anton has outscored in this contest . The next n lines describe participants results: the i-th of them consists of a participant handle namei and two integers beforei and afteri ( - 4000 ≤ beforei, afteri ≤ 4000) — participant's rating before and after the contest, respectively. Each handle is a non-empty string, consisting of no more than 10 characters, which might be lowercase and uppercase English letters, digits, characters «_» and «-» characters. It is guaranteed that all handles are distinct.
Print «YES» (quotes for clarity), if Anton has performed good in the contest and «NO» (quotes for clarity) otherwise.
null
In the first sample, Anton has outscored user with handle Burunduk1, whose handle was colored red before the contest and his rating has increased after the contest. In the second sample, Applejack's rating has not increased after the contest, while both Fluttershy's and Pinkie_Pie's handles were not colored red before the contest.
[{"input": "3\nBurunduk1 2526 2537\nBudAlNik 2084 2214\nsubscriber 2833 2749", "output": "YES"}, {"input": "3\nApplejack 2400 2400\nFluttershy 2390 2431\nPinkie_Pie -2500 -2450", "output": "NO"}]
800
["implementation"]
60
[{"input": "3\r\nBurunduk1 2526 2537\r\nBudAlNik 2084 2214\r\nsubscriber 2833 2749\r\n", "output": "YES"}, {"input": "3\r\nApplejack 2400 2400\r\nFluttershy 2390 2431\r\nPinkie_Pie -2500 -2450\r\n", "output": "NO"}, {"input": "1\r\nDb -3373 3591\r\n", "output": "NO"}, {"input": "5\r\nQ2bz 960 2342\r\nhmX 2710 -1348\r\ngbAe -1969 -963\r\nE -160 196\r\npsi 2665 -3155\r\n", "output": "NO"}, {"input": "9\r\nmwAz9lQ 1786 -1631\r\nnYgYFXZQfY -1849 -1775\r\nKU4jF -1773 -3376\r\nopR 3752 2931\r\nGl -1481 -1002\r\nR -1111 3778\r\n0i9B21DC 3650 289\r\nQ8L2dS0 358 -3305\r\ng -2662 3968\r\n", "output": "NO"}, {"input": "5\r\nzMSBcOUf -2883 -2238\r\nYN -3314 -1480\r\nfHpuccQn06 -1433 -589\r\naM1NVEPQi 399 3462\r\n_L 2516 -3290\r\n", "output": "NO"}, {"input": "1\r\na 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nfucker 4000 4000\r\n", "output": "NO"}, {"input": "1\r\nJora 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nACA 2400 2420\r\n", "output": "YES"}, {"input": "1\r\nAca 2400 2420\r\n", "output": "YES"}, {"input": "1\r\nSub_d 2401 2402\r\n", "output": "YES"}, {"input": "2\r\nHack 2400 2401\r\nDum 1243 555\r\n", "output": "YES"}, {"input": "1\r\nXXX 2400 2500\r\n", "output": "YES"}, {"input": "1\r\nfucker 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nX 2400 2500\r\n", "output": "YES"}, {"input": "1\r\nvineet 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nabc 2400 2500\r\n", "output": "YES"}, {"input": "1\r\naaaaa 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nhoge 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nInfinity 2400 2468\r\n", "output": "YES"}, {"input": "1\r\nBurunduk1 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nFuck 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nfuck 2400 2401\r\n", "output": "YES"}, {"input": "3\r\nApplejack 2400 2401\r\nFluttershy 2390 2431\r\nPinkie_Pie -2500 -2450\r\n", "output": "YES"}, {"input": "1\r\nalex 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nA 2400 2401\r\n", "output": "YES"}, {"input": "1\r\na 2400 2455\r\n", "output": "YES"}, {"input": "1\r\nlol 2400 2401\r\n", "output": "YES"}, {"input": "2\r\nBurunduk1 2400 2537\r\nBudAlNik 2084 2214\r\n", "output": "YES"}, {"input": "1\r\naaaaaa 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nBurunduk1 2400 2500\r\n", "output": "YES"}, {"input": "1\r\nds 2400 2410\r\n", "output": "YES"}, {"input": "1\r\nas 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nabc 2400 2401\r\n", "output": "YES"}, {"input": "3\r\nBudAlNik 2084 2214\r\nsubscriber 2833 2749\r\nBurunduk1 2526 2537\r\n", "output": "YES"}, {"input": "1\r\ncaonima 2400 2401\r\n", "output": "YES"}, {"input": "1\r\narr 2400 2500\r\n", "output": "YES"}, {"input": "1\r\nx 2400 2401\r\n", "output": "YES"}, {"input": "1\r\narrr 2400 2500\r\n", "output": "YES"}, {"input": "1\r\nabc 2400 2405\r\n", "output": "YES"}, {"input": "3\r\nBurunduk1 2400 2420\r\nBudAlNik 2084 2214\r\nsubscriber 2833 2749\r\n", "output": "YES"}, {"input": "1\r\nBurunduk1 2400 2537\r\n", "output": "YES"}, {"input": "1\r\nHELLO 2400 2401\r\n", "output": "YES"}, {"input": "1\r\neatmore 2400 2500\r\n", "output": "YES"}, {"input": "1\r\nb 2400 2401\r\n", "output": "YES"}, {"input": "3\r\nBurunduk1 2400 2537\r\nBudAlNik 2084 2214\r\nsubscriber 2833 2749\r\n", "output": "YES"}, {"input": "1\r\nApplejack 2400 2410\r\n", "output": "YES"}, {"input": "1\r\nabacaba 2400 2451\r\n", "output": "YES"}, {"input": "1\r\nrekt_n00b 2500 2600\r\n", "output": "YES"}]
false
stdio
null
true
681/A
681
A
Python 3
TESTS
8
108
0
77606288
for _ in range(int(input())): name, before, after = input().split() if int(before) >=2400: if after > before: print("YES") exit(0) print('NO')
60
46
0
142667952
""" 123456789101112131415 """ import sys import math input = sys.stdin.readline ############ ---- Input Functions ---- ############ def inp(): return(int(input())) def inlt(): return(list(map(str,input().split()))) def insr(): s = input() return(list(s[:len(s) - 1])) def invr(): return(map(int,input().split())) # n = inp() isTrue = True for i in range(n): name, before, after = inlt() if int(before) >= 2400 and int(after) > int(before): print("YES") isTrue = False break if isTrue: print("NO")
Codeforces Round 357 (Div. 2)
CF
2,016
1
256
A Good Contest
Codeforces user' handle color depends on his rating — it is red if his rating is greater or equal to 2400; it is orange if his rating is less than 2400 but greater or equal to 2200, etc. Each time participant takes part in a rated contest, his rating is changed depending on his performance. Anton wants the color of his handle to become red. He considers his performance in the rated contest to be good if he outscored some participant, whose handle was colored red before the contest and his rating has increased after it. Anton has written a program that analyses contest results and determines whether he performed good or not. Are you able to do the same?
The first line of the input contains a single integer n (1 ≤ n ≤ 100) — the number of participants Anton has outscored in this contest . The next n lines describe participants results: the i-th of them consists of a participant handle namei and two integers beforei and afteri ( - 4000 ≤ beforei, afteri ≤ 4000) — participant's rating before and after the contest, respectively. Each handle is a non-empty string, consisting of no more than 10 characters, which might be lowercase and uppercase English letters, digits, characters «_» and «-» characters. It is guaranteed that all handles are distinct.
Print «YES» (quotes for clarity), if Anton has performed good in the contest and «NO» (quotes for clarity) otherwise.
null
In the first sample, Anton has outscored user with handle Burunduk1, whose handle was colored red before the contest and his rating has increased after the contest. In the second sample, Applejack's rating has not increased after the contest, while both Fluttershy's and Pinkie_Pie's handles were not colored red before the contest.
[{"input": "3\nBurunduk1 2526 2537\nBudAlNik 2084 2214\nsubscriber 2833 2749", "output": "YES"}, {"input": "3\nApplejack 2400 2400\nFluttershy 2390 2431\nPinkie_Pie -2500 -2450", "output": "NO"}]
800
["implementation"]
60
[{"input": "3\r\nBurunduk1 2526 2537\r\nBudAlNik 2084 2214\r\nsubscriber 2833 2749\r\n", "output": "YES"}, {"input": "3\r\nApplejack 2400 2400\r\nFluttershy 2390 2431\r\nPinkie_Pie -2500 -2450\r\n", "output": "NO"}, {"input": "1\r\nDb -3373 3591\r\n", "output": "NO"}, {"input": "5\r\nQ2bz 960 2342\r\nhmX 2710 -1348\r\ngbAe -1969 -963\r\nE -160 196\r\npsi 2665 -3155\r\n", "output": "NO"}, {"input": "9\r\nmwAz9lQ 1786 -1631\r\nnYgYFXZQfY -1849 -1775\r\nKU4jF -1773 -3376\r\nopR 3752 2931\r\nGl -1481 -1002\r\nR -1111 3778\r\n0i9B21DC 3650 289\r\nQ8L2dS0 358 -3305\r\ng -2662 3968\r\n", "output": "NO"}, {"input": "5\r\nzMSBcOUf -2883 -2238\r\nYN -3314 -1480\r\nfHpuccQn06 -1433 -589\r\naM1NVEPQi 399 3462\r\n_L 2516 -3290\r\n", "output": "NO"}, {"input": "1\r\na 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nfucker 4000 4000\r\n", "output": "NO"}, {"input": "1\r\nJora 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nACA 2400 2420\r\n", "output": "YES"}, {"input": "1\r\nAca 2400 2420\r\n", "output": "YES"}, {"input": "1\r\nSub_d 2401 2402\r\n", "output": "YES"}, {"input": "2\r\nHack 2400 2401\r\nDum 1243 555\r\n", "output": "YES"}, {"input": "1\r\nXXX 2400 2500\r\n", "output": "YES"}, {"input": "1\r\nfucker 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nX 2400 2500\r\n", "output": "YES"}, {"input": "1\r\nvineet 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nabc 2400 2500\r\n", "output": "YES"}, {"input": "1\r\naaaaa 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nhoge 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nInfinity 2400 2468\r\n", "output": "YES"}, {"input": "1\r\nBurunduk1 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nFuck 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nfuck 2400 2401\r\n", "output": "YES"}, {"input": "3\r\nApplejack 2400 2401\r\nFluttershy 2390 2431\r\nPinkie_Pie -2500 -2450\r\n", "output": "YES"}, {"input": "1\r\nalex 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nA 2400 2401\r\n", "output": "YES"}, {"input": "1\r\na 2400 2455\r\n", "output": "YES"}, {"input": "1\r\nlol 2400 2401\r\n", "output": "YES"}, {"input": "2\r\nBurunduk1 2400 2537\r\nBudAlNik 2084 2214\r\n", "output": "YES"}, {"input": "1\r\naaaaaa 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nBurunduk1 2400 2500\r\n", "output": "YES"}, {"input": "1\r\nds 2400 2410\r\n", "output": "YES"}, {"input": "1\r\nas 2400 2401\r\n", "output": "YES"}, {"input": "1\r\nabc 2400 2401\r\n", "output": "YES"}, {"input": "3\r\nBudAlNik 2084 2214\r\nsubscriber 2833 2749\r\nBurunduk1 2526 2537\r\n", "output": "YES"}, {"input": "1\r\ncaonima 2400 2401\r\n", "output": "YES"}, {"input": "1\r\narr 2400 2500\r\n", "output": "YES"}, {"input": "1\r\nx 2400 2401\r\n", "output": "YES"}, {"input": "1\r\narrr 2400 2500\r\n", "output": "YES"}, {"input": "1\r\nabc 2400 2405\r\n", "output": "YES"}, {"input": "3\r\nBurunduk1 2400 2420\r\nBudAlNik 2084 2214\r\nsubscriber 2833 2749\r\n", "output": "YES"}, {"input": "1\r\nBurunduk1 2400 2537\r\n", "output": "YES"}, {"input": "1\r\nHELLO 2400 2401\r\n", "output": "YES"}, {"input": "1\r\neatmore 2400 2500\r\n", "output": "YES"}, {"input": "1\r\nb 2400 2401\r\n", "output": "YES"}, {"input": "3\r\nBurunduk1 2400 2537\r\nBudAlNik 2084 2214\r\nsubscriber 2833 2749\r\n", "output": "YES"}, {"input": "1\r\nApplejack 2400 2410\r\n", "output": "YES"}, {"input": "1\r\nabacaba 2400 2451\r\n", "output": "YES"}, {"input": "1\r\nrekt_n00b 2500 2600\r\n", "output": "YES"}]
false
stdio
null
true
676/C
676
C
PyPy 3
TESTS
37
155
4,403,200
118349399
def find(a, pref, k): n = len(a) a1 = [] for i in range(n): if i % 2 == 1: a1.append(0) else: a1.append(a[i]) _sum = 0 _max = -1 for i in range(1, n, 2): if a[i] > _max: _max = a[i] r = 1 s1 = a1[0] for l in range(n): while r < n and s1 + a1[r] <= k: s1 += a1[r] r += 1 if pref[r] - pref[l] > _max: _max = pref[r] - pref[l] s1 -= a1[l] return _max n, k = map(int, input().split()) s = input() s += '$' a = [] cnt = 1 for i in range(1, n + 1): if s[i] != s[i - 1]: a.append(cnt) cnt = 1 else: cnt += 1 if len(a) == 1: print(n) exit() pref = [0] for i in range(len(a)): pref.append(pref[-1] + a[i]) f1 = find(a, pref, k) a.insert(0, 0) pref.insert(0, 0) f2 = find(a, pref, k) print(max(f1, f2))
117
62
2,764,800
166309064
def solve(c,s): ln=len(s) dp=[0]*(ln+1) for i in range(ln): if s[i]=='a': dp[i+1]=dp[i]+1 else: dp[i+1]=dp[i] lft=0 rt=ln while lft<rt: md=(lft+rt+1)//2 f=0 for i in range(md, ln+1): tmp=dp[i]-dp[i-md] if tmp<=c or tmp>=md-c: f=1 break if f==1: lft=md else: rt=md-1 return lft n,c=map(int,input().split()) s=input() print(solve(c,s))
Codeforces Round 354 (Div. 2)
CF
2,016
1
256
Vasya and String
High school student Vasya got a string of length n as a birthday present. This string consists of letters 'a' and 'b' only. Vasya denotes beauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equal letters. Vasya can change no more than k characters of the original string. What is the maximum beauty of the string he can achieve?
The first line of the input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n) — the length of the string and the maximum number of characters to change. The second line contains the string, consisting of letters 'a' and 'b' only.
Print the only integer — the maximum beauty of the string Vasya can achieve by changing no more than k characters.
null
In the first sample, Vasya can obtain both strings "aaaa" and "bbbb". In the second sample, the optimal answer is obtained with the string "aaaaabaa" or with the string "aabaaaaa".
[{"input": "4 2\nabba", "output": "4"}, {"input": "8 1\naabaabaa", "output": "5"}]
1,500
["binary search", "dp", "strings", "two pointers"]
117
[{"input": "4 2\r\nabba\r\n", "output": "4\r\n"}, {"input": "8 1\r\naabaabaa\r\n", "output": "5\r\n"}, {"input": "1 0\r\na\r\n", "output": "1\r\n"}, {"input": "1 1\r\nb\r\n", "output": "1\r\n"}, {"input": "1 0\r\nb\r\n", "output": "1\r\n"}, {"input": "1 1\r\na\r\n", "output": "1\r\n"}, {"input": "10 10\r\nbbbbbbbbbb\r\n", "output": "10\r\n"}, {"input": "10 2\r\nbbbbbbbbbb\r\n", "output": "10\r\n"}, {"input": "10 1\r\nbbabbabbba\r\n", "output": "6\r\n"}, {"input": "10 10\r\nbbabbbaabb\r\n", "output": "10\r\n"}, {"input": "10 9\r\nbabababbba\r\n", "output": "10\r\n"}, {"input": "10 4\r\nbababbaaab\r\n", "output": "9\r\n"}, {"input": "10 10\r\naabaaabaaa\r\n", "output": "10\r\n"}, {"input": "10 10\r\naaaabbbaaa\r\n", "output": "10\r\n"}, {"input": "10 1\r\nbaaaaaaaab\r\n", "output": "9\r\n"}, {"input": "10 5\r\naaaaabaaaa\r\n", "output": "10\r\n"}, {"input": "10 4\r\naaaaaaaaaa\r\n", "output": "10\r\n"}, {"input": "100 10\r\nbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\r\n", "output": "100\r\n"}, {"input": "100 7\r\nbbbbabbbbbaabbbabbbbbbbbbbbabbbbbbbbbbbbbbbbbbbbbbbbbabbbbbbbbbbbabbabbbbbbbbbbbbbbbbbbbbbbbbbbbbbab\r\n", "output": "93\r\n"}, {"input": "100 30\r\nbbaabaaabbbbbbbbbbaababababbbbbbaabaabbbbbbbbabbbbbabbbbabbbbbbbbaabbbbbbbbbabbbbbabbbbbbbbbaaaaabba\r\n", "output": "100\r\n"}, {"input": "100 6\r\nbaababbbaabbabbaaabbabbaabbbbbbbbaabbbabbbbaabbabbbbbabababbbbabbbbbbabbbbbbbbbaaaabbabbbbaabbabaabb\r\n", "output": "34\r\n"}, {"input": "100 45\r\naabababbabbbaaabbbbbbaabbbabbaabbbbbabbbbbbbbabbbbbbabbaababbaabbababbbbbbababbbbbaabbbbbbbaaaababab\r\n", "output": "100\r\n"}, {"input": "100 2\r\nababaabababaaababbaaaabbaabbbababbbaaabbbbabababbbabababaababaaabaabbbbaaabbbabbbbbabbbbbbbaabbabbba\r\n", "output": "17\r\n"}, {"input": "100 25\r\nbabbbaaababaaabbbaabaabaabbbabbabbbbaaaaaaabaaabaaaaaaaaaabaaaabaaabbbaaabaaababaaabaabbbbaaaaaaaaaa\r\n", "output": "80\r\n"}, {"input": "100 14\r\naabaaaaabababbabbabaaaabbaaaabaaabbbaaabaaaaaaaabaaaaabbaaaaaaaaabaaaaaaabbaababaaaababbbbbabaaaabaa\r\n", "output": "61\r\n"}, {"input": "100 8\r\naaaaabaaaabaabaaaaaaaabaaaabaaaaaaaaaaaaaabaaaaabaaaaaaaaaaaaaaaaabaaaababaabaaaaaaaaaaaaabbabaaaaaa\r\n", "output": "76\r\n"}, {"input": "100 12\r\naaaaaaaaaaaaaaaabaaabaaaaaaaaaabbaaaabbabaaaaaaaaaaaaaaaaaaaaabbaaabaaaaaaaaaaaabaaaaaaaabaaaaaaaaaa\r\n", "output": "100\r\n"}, {"input": "100 65\r\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n", "output": "100\r\n"}, {"input": "10 0\r\nbbbbbbbbbb\r\n", "output": "10\r\n"}, {"input": "10 0\r\nbbbbabbbbb\r\n", "output": "5\r\n"}, {"input": "10 0\r\nbbabbbabba\r\n", "output": "3\r\n"}, {"input": "10 0\r\nbaabbbbaba\r\n", "output": "4\r\n"}, {"input": "10 0\r\naababbbbaa\r\n", "output": "4\r\n"}, {"input": "10 2\r\nabbbbbaaba\r\n", "output": "8\r\n"}, {"input": "10 0\r\nabbaaabaaa\r\n", "output": "3\r\n"}, {"input": "10 0\r\naabbaaabaa\r\n", "output": "3\r\n"}, {"input": "10 1\r\naaaaaababa\r\n", "output": "8\r\n"}, {"input": "10 0\r\nbaaaaaaaaa\r\n", "output": "9\r\n"}, {"input": "10 0\r\naaaaaaaaaa\r\n", "output": "10\r\n"}, {"input": "100 0\r\nbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\r\n", "output": "100\r\n"}, {"input": "100 0\r\nbbbbbbbbbbabbbbaaabbbbbbbbbbbabbbabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbabbbbbbbbbabbbbbbbbbbbbbab\r\n", "output": "40\r\n"}, {"input": "100 11\r\nbaabbbbbababbbbabbbbbbbabbbbbbbbbbbbbbabbbbbbababbbbababbbbaaabbbbabbbbbabbbbbbbbabababbbabbbbbbbabb\r\n", "output": "65\r\n"}, {"input": "100 8\r\nbbababbbbbaabbbaaababbbbababababbbbababbabbbabbbbbaabbbabbbababbabbbbabbbabbbbaabbbbabbbaabbbbaaaabb\r\n", "output": "33\r\n"}, {"input": "100 21\r\nabbaaaabbbababaabbbababbbbbbbbabbaababababbbabbbaaabbaaabbbbabbabbbabbbabaababbbabbbbbabbbbbbabbbbab\r\n", "output": "65\r\n"}, {"input": "100 9\r\nabbbaabaabaaaaaaabbabbbababbaaabbbaaabbaabaaaaabbbbbabbaabaabbbbbaaaaababbaaabbabaabaaabababbaababbb\r\n", "output": "26\r\n"}, {"input": "100 5\r\naababababbaaaaaaaabbbabaaaabbabaaaabbaabaaaaabababbabaabaaabaaaaaaaabaababbabbaaabaabbabbaaaaabbabba\r\n", "output": "22\r\n"}, {"input": "100 9\r\naababaabaaaaaaaaabbbaabaaaaaaabaaaaaaaaaaaaabaaabaabaabbbbabbaababbabbaaaabbababaabaababaabaaaaaaaaa\r\n", "output": "49\r\n"}, {"input": "100 6\r\naaaaabbaaaaaaaaaaabaaaabaaaaaaaaabaaabaaaaaabaaaaaaaaaaabaabaaaabaaaaaaaaaaaaaaabaabbaaaaaaaaaaaaaaa\r\n", "output": "56\r\n"}, {"input": "100 7\r\nabaaabaabaabaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaabaaaaaaabbabaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaba\r\n", "output": "86\r\n"}, {"input": "100 0\r\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n", "output": "100\r\n"}]
false
stdio
null
true
416/D
416
D
Python 3
TESTS
78
451
16,588,800
6364119
n = int(input()) a = list(map(int, input().split())) i, j, k = 0, -1, -1 ans = 1 for l in range(len(a)): if a[l] == -1: continue if j == -1: j = l elif k == -1: if (a[l] - a[j]) % (l - j) == 0 and a[j] - (a[l] - a[j]) / (l - j) * (j - i) > 0: k = l else: i, j = l, l ans += 1 elif (a[l] - a[k]) * (k - j) == (a[k] - a[j]) * (l - k): k = l elif a[k] + (a[k] - a[j]) / (k - j) * (l - k - 1) < 0: i, j, k = k + a[k] // ((a[k] - a[j]) / (k - j)), l, -1 ans += 1 else: i, j, k = l, l, -1 ans += 1 if k not in (-1, len(a) - 1) and a[k] + (a[k] - a[j]) / (k - j) * (len(a) - k - 1) < 0: ans += 1 print(ans)
84
171
27,648,000
302828817
import sys,math,heapq from collections import Counter #sys.setrecursionlimit(200000) #fin = open('input.txt', 'r') #input = lambda: fin.readline().strip() input=lambda: sys.stdin.readline().strip() import random RANDOM = random.getrandbits(20) class Wrapper(int): def __init__(self, x): int.__init__(x) def __hash__(self): return super(Wrapper, self).__hash__() ^ RANDOM def I(): return input() def II(): return int(input()) def MI(): return map(int, input().split()) def LI(): return list(input().split()) def LII(): return list(map(int, input().split())) def LFI(): return list(map(float, input().split())) def GMI(): return map(lambda x: int(x) - 1, input().split()) def LGMI(): return list(map(lambda x: int(x) - 1, input().split())) from types import GeneratorType def bootstrap(f, stack=[]): def wrappedfunc(*args, **kwargs): if stack: return f(*args, **kwargs) else: to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) else: if not stack:break stack.pop() if not stack: break to = stack[-1].send(to) return to return wrappedfunc class SortedList: def __init__(self, iterable=None, _load=200): """Initialize sorted list instance.""" if iterable is None: iterable = [] values = sorted(iterable) self._len = _len = len(values) self._load = _load self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)] self._list_lens = [len(_list) for _list in _lists] self._min_s = [_list[0] for _list in _lists] self._fen_tree = [] self._rebuild = True def _fen_build(self): """Build a fenwick tree instance.""" self._fen_tree[:] = self._list_lens _fen_tree = self._fen_tree for i in range(len(_fen_tree)): if i | i + 1 < len(_fen_tree): _fen_tree[i | i + 1] += _fen_tree[i] self._rebuild = False def _fen_update(self, index, value): """Update `fen_tree[index] += value`.""" if not self._rebuild: _fen_tree = self._fen_tree while index < len(_fen_tree): _fen_tree[index] += value index |= index + 1 def _fen_query(self, end): """Return `sum(_fen_tree[:end])`.""" if self._rebuild: self._fen_build() _fen_tree = self._fen_tree x = 0 while end: x += _fen_tree[end - 1] end &= end - 1 return x def _fen_findkth(self, k): """Return a pair of (the largest `idx` such that `sum(_fen_tree[:idx]) <= k`, `k - sum(_fen_tree[:idx])`).""" _list_lens = self._list_lens if k < _list_lens[0]: return 0, k if k >= self._len - _list_lens[-1]: return len(_list_lens) - 1, k + _list_lens[-1] - self._len if self._rebuild: self._fen_build() _fen_tree = self._fen_tree idx = -1 for d in reversed(range(len(_fen_tree).bit_length())): right_idx = idx + (1 << d) if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]: idx = right_idx k -= _fen_tree[idx] return idx + 1, k def _delete(self, pos, idx): """Delete value at the given `(pos, idx)`.""" _lists = self._lists _mins = self._min_s _list_lens = self._list_lens self._len -= 1 self._fen_update(pos, -1) del _lists[pos][idx] _list_lens[pos] -= 1 if _list_lens[pos]: _mins[pos] = _lists[pos][0] else: del _lists[pos] del _list_lens[pos] del _mins[pos] self._rebuild = True def _loc_left(self, value): """Return an index pair that corresponds to the first position of `value` in the sorted list.""" if not self._len: return 0, 0 _lists = self._lists _mins = self._min_s lo, pos = -1, len(_lists) - 1 while lo + 1 < pos: mi = (lo + pos) >> 1 if value <= _mins[mi]: pos = mi else: lo = mi if pos and value <= _lists[pos - 1][-1]: pos -= 1 _list = _lists[pos] lo, idx = -1, len(_list) while lo + 1 < idx: mi = (lo + idx) >> 1 if value <= _list[mi]: idx = mi else: lo = mi return pos, idx def _loc_right(self, value): """Return an index pair that corresponds to the last position of `value` in the sorted list.""" if not self._len: return 0, 0 _lists = self._lists _mins = self._min_s pos, hi = 0, len(_lists) while pos + 1 < hi: mi = (pos + hi) >> 1 if value < _mins[mi]: hi = mi else: pos = mi _list = _lists[pos] lo, idx = -1, len(_list) while lo + 1 < idx: mi = (lo + idx) >> 1 if value < _list[mi]: idx = mi else: lo = mi return pos, idx def add(self, value): """Add `value` to sorted list.""" _load = self._load _lists = self._lists _mins = self._min_s _list_lens = self._list_lens self._len += 1 if _lists: pos, idx = self._loc_right(value) self._fen_update(pos, 1) _list = _lists[pos] _list.insert(idx, value) _list_lens[pos] += 1 _mins[pos] = _list[0] if _load + _load < len(_list): _lists.insert(pos + 1, _list[_load:]) _list_lens.insert(pos + 1, len(_list) - _load) _mins.insert(pos + 1, _list[_load]) _list_lens[pos] = _load del _list[_load:] self._rebuild = True else: _lists.append([value]) _mins.append(value) _list_lens.append(1) self._rebuild = True def discard(self, value): """Remove `value` from sorted list if it is a member.""" _lists = self._lists if _lists: pos, idx = self._loc_right(value) if idx and _lists[pos][idx - 1] == value: self._delete(pos, idx - 1) def remove(self, value): """Remove `value` from sorted list; `value` must be a member.""" _len = self._len self.discard(value) if _len == self._len: raise ValueError('{0!r} not in list'.format(value)) def pop(self, index=-1): """Remove and return value at `index` in sorted list.""" pos, idx = self._fen_findkth(self._len + index if index < 0 else index) value = self._lists[pos][idx] self._delete(pos, idx) return value def bisect_left(self, value): """Return the first index to insert `value` in the sorted list.""" pos, idx = self._loc_left(value) return self._fen_query(pos) + idx def bisect_right(self, value): """Return the last index to insert `value` in the sorted list.""" pos, idx = self._loc_right(value) return self._fen_query(pos) + idx def count(self, value): """Return number of occurrences of `value` in the sorted list.""" return self.bisect_right(value) - self.bisect_left(value) def __len__(self): """Return the size of the sorted list.""" return self._len def __getitem__(self, index): """Lookup value at `index` in sorted list.""" pos, idx = self._fen_findkth(self._len + index if index < 0 else index) return self._lists[pos][idx] def __delitem__(self, index): """Remove value at `index` from sorted list.""" pos, idx = self._fen_findkth(self._len + index if index < 0 else index) self._delete(pos, idx) def __contains__(self, value): """Return true if `value` is an element of the sorted list.""" _lists = self._lists if _lists: pos, idx = self._loc_left(value) return idx < len(_lists[pos]) and _lists[pos][idx] == value return False def __iter__(self): """Return an iterator over the sorted list.""" return (value for _list in self._lists for value in _list) def __reversed__(self): """Return a reverse iterator over the sorted list.""" return (value for _list in reversed(self._lists) for value in reversed(_list)) def __repr__(self): """Return strings representation of sorted list.""" return 'SortedList({0})'.format(list(self)) class RangeAscendRangeMax: def __init__(self, n): self.n = n self.cover = [-math.inf] * (4 * n) self.lazy = [-math.inf] * (4 * n) @staticmethod def _max(a, b): return a if a > b else b def _make_tag(self, i, val) -> None: self.cover[i] = self._max(self.cover[i], val) self.lazy[i] = self._max(self.lazy[i], val) return def _push_up(self, i): self.cover[i] = self._max(self.cover[2 * i], self.cover[2 * i + 1]) return def _push_down(self, i): if self.lazy[i] != -math.inf: self.cover[2 * i] = self._max(self.cover[2 * i], self.lazy[i]) self.cover[2 * i + 1] = self._max(self.cover[2 * i + 1], self.lazy[i]) self.lazy[2 * i] = self._max(self.lazy[2 * i], self.lazy[i]) self.lazy[2 * i + 1] = self._max(self.lazy[2 * i + 1], self.lazy[i]) self.lazy[i] = -math.inf return def build(self, nums) -> None: assert self.n == len(nums) stack = [(0, self.n - 1, 1)] while stack: s, t, ind = stack.pop() if ind >= 0: if s == t: self._make_tag(ind, nums[s]) else: stack.append([s, t, ~ind]) m = s + (t - s) // 2 stack.append([s, m, 2 * ind]) stack.append([m + 1, t, 2 * ind + 1]) else: ind = ~ind self._push_up(ind) return def get(self): stack = [(0, self.n - 1, 1)] nums = [0] * self.n while stack: s, t, i = stack.pop() if s == t: nums[s] = self.cover[i] continue m = s + (t - s) // 2 self._push_down(i) stack.append((s, m, 2 * i)) stack.append((m + 1, t, 2 * i + 1)) return nums def range_ascend(self, left, right, val): # update the range ascend assert 0 <= left <= right <= self.n - 1 stack = [(0, self.n - 1, 1)] while stack: a, b, i = stack.pop() if i >= 0: if left <= a and b <= right: self._make_tag(i, val) continue self._push_down(i) stack.append([a, b, ~i]) m = a + (b - a) // 2 if left <= m: stack.append([a, m, 2 * i]) if right > m: stack.append([m + 1, b, 2 * i + 1]) else: i = ~i self._push_up(i) return def range_max(self, left, right): # query the range max assert 0 <= left <= right <= self.n - 1 stack = [(0, self.n - 1, 1)] highest = -math.inf while stack: a, b, i = stack.pop() if left <= a and b <= right: highest = self._max(highest, self.cover[i]) continue self._push_down(i) m = a + (b - a) // 2 if left <= m: stack.append([a, m, 2 * i]) if right > m: stack.append([m + 1, b, 2 * i + 1]) return highest class SegmentTree: #LUOGU 3372 def __init__(self,num_of_elements,initial_lst): self.tree=[0]*(4*num_of_elements) #self.lazy=[0]*(4*num_of_elements) self.lst=initial_lst def build(self,s,t,p): if s==t: self.tree[p]=self.lst[s] return m=s+((t-s)>>1) self.build(s,m,p*2) self.build(m+1,t,p*2+1) self.tree[p]=math.gcd(self.tree[p*2],self.tree[p*2+1]) def query(self,l,r,s,t,p): #print(l,r,s,t) if l<=s and r>=t: return self.tree[p] m=s+((t-s)>>1) csum=0 if l<=m: csum=self.query(l,r,s,m,p*2) if r>m: if csum!=0: csum=math.gcd(csum,self.query(l,r,m+1,t,p*2+1)) else: csum=self.query(l,r,m+1,t,p*2+1) return csum from collections import deque import bisect mod=998244353 class Factorial: def __init__(self, N, mod) -> None: N += 1 self.mod = mod self.f = [1 for _ in range(N)] self.g = [1 for _ in range(N)] for i in range(1, N): self.f[i] = self.f[i - 1] * i % self.mod self.g[-1] = pow(self.f[-1], mod - 2, mod) for i in range(N - 2, -1, -1): self.g[i] = self.g[i + 1] * (i + 1) % self.mod def fac(self, n): return self.f[n] def fac_inv(self, n): return self.g[n] def combi(self, n, m): if n < m or m < 0 or n < 0: return 0 return self.f[n] * self.g[m] % self.mod * self.g[n - m] % self.mod def permu(self, n, m): if n < m or m < 0 or n < 0: return 0 return self.f[n] * self.g[n - m] % self.mod def catalan(self, n): return (self.combi(2 * n, n) - self.combi(2 * n, n - 1)) % self.mod def inv(self, n): return self.f[n-1] * self.g[n] % self.mod def main(): n = II() nums = LII() start = 0 i1 = i2 = -1 ans = 1 for i in range(n): if nums[i] == -1: if i2 != -1: v = nums[i1] + (nums[i2] - nums[i1]) // (i2 - i1) * (i - i1) if v <= 0: ans += 1 start = i i1 = -1 i2 = -1 else: if i1 == -1: i1 = i elif i2 == -1: if (nums[i] - nums[i1]) % (i - i1) or nums[i1] + (nums[i] - nums[i1]) // (i - i1) * (start - i1) <= 0: ans += 1 start = i i1 = i i2 = -1 else: i2 = i else: v = nums[i1] + (nums[i2] - nums[i1]) // (i2 - i1) * (i - i1) if nums[i] != v: ans += 1 start = i i1 = i i2 = -1 print(ans) t=1 #t=II() for i in range(t): w = main() #print(w) #fout.write(str(w))
Codeforces Round 241 (Div. 2)
CF
2,014
1
256
Population Size
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 a1, a2, ..., an, 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 ai ​​may be unknown. Such values are represented by number -1. For a given sequence a = (a1, a2, ..., an), 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 ai > 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 ci + 1 - ci 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 a1, a2, ..., an separated by a space (1 ≤ ai ≤ 109 or ai = - 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.
null
null
[{"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"}]
2,400
["greedy", "implementation", "math"]
84
[{"input": "9\r\n8 6 4 2 1 4 7 10 2\r\n", "output": "3\r\n"}, {"input": "9\r\n-1 6 -1 2 -1 4 7 -1 2\r\n", "output": "3\r\n"}, {"input": "5\r\n-1 -1 -1 -1 -1\r\n", "output": "1\r\n"}, {"input": "7\r\n-1 -1 4 5 1 2 3\r\n", "output": "2\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n65\r\n", "output": "1\r\n"}, {"input": "1\r\n1000000000\r\n", "output": "1\r\n"}, {"input": "1\r\n-1\r\n", "output": "1\r\n"}, {"input": "2\r\n1000000000 1000000000\r\n", "output": "1\r\n"}, {"input": "2\r\n1000000000 -1\r\n", "output": "1\r\n"}, {"input": "2\r\n-1 1000000000\r\n", "output": "1\r\n"}, {"input": "2\r\n-1 -1\r\n", "output": "1\r\n"}, {"input": "3\r\n999999999 1000000000 -1\r\n", "output": "1\r\n"}, {"input": "3\r\n999999999 -1 1000000000\r\n", "output": "2\r\n"}, {"input": "3\r\n1000000000 999999999 1000000000\r\n", "output": "2\r\n"}, {"input": "3\r\n-1 1000000000 999999999\r\n", "output": "1\r\n"}, {"input": "3\r\n-1 1000000000 -1\r\n", "output": "1\r\n"}, {"input": "3\r\n-1 1 2\r\n", "output": "2\r\n"}, {"input": "3\r\n-1 1 1000000000\r\n", "output": "2\r\n"}, {"input": "5\r\n-1 1 7 -1 5\r\n", "output": "2\r\n"}, {"input": "7\r\n-1 2 4 -1 4 1 5\r\n", "output": "3\r\n"}, {"input": "2\r\n-1 21\r\n", "output": "1\r\n"}, {"input": "3\r\n39 42 -1\r\n", "output": "1\r\n"}, {"input": "4\r\n45 -1 41 -1\r\n", "output": "1\r\n"}, {"input": "5\r\n-1 40 42 -1 46\r\n", "output": "1\r\n"}, {"input": "6\r\n-1 6 1 -1 -1 -1\r\n", "output": "2\r\n"}, {"input": "7\r\n32 33 34 -1 -1 37 38\r\n", "output": "1\r\n"}, {"input": "8\r\n-1 12 14 16 18 20 -1 -1\r\n", "output": "1\r\n"}, {"input": "9\r\n42 39 36 33 -1 -1 -1 34 39\r\n", "output": "2\r\n"}, {"input": "10\r\n29 27 -1 23 42 -1 -1 45 -1 -1\r\n", "output": "2\r\n"}, {"input": "5\r\n40 -1 44 46 48\r\n", "output": "1\r\n"}, {"input": "6\r\n43 40 37 34 -1 -1\r\n", "output": "1\r\n"}, {"input": "7\r\n11 8 5 -1 -1 -1 -1\r\n", "output": "2\r\n"}, {"input": "8\r\n-1 12 14 16 18 20 -1 -1\r\n", "output": "1\r\n"}, {"input": "9\r\n42 39 36 33 -1 -1 -1 34 39\r\n", "output": "2\r\n"}, {"input": "10\r\n29 27 -1 23 42 -1 -1 45 -1 -1\r\n", "output": "2\r\n"}, {"input": "11\r\n9 21 17 13 -1 -1 -1 -1 -1 -1 -1\r\n", "output": "3\r\n"}, {"input": "12\r\n-1 17 -1 54 -1 64 -1 74 79 84 -1 94\r\n", "output": "2\r\n"}, {"input": "13\r\n25 24 23 22 24 27 -1 33 -1 2 2 2 -1\r\n", "output": "3\r\n"}, {"input": "14\r\n-1 5 3 -1 -1 31 31 31 -1 31 -1 -1 4 7\r\n", "output": "3\r\n"}, {"input": "15\r\n-1 28 -1 32 34 26 -1 26 -1 -1 26 26 26 -1 -1\r\n", "output": "2\r\n"}, {"input": "16\r\n3 8 13 18 23 -1 -1 -1 43 48 53 45 -1 -1 -1 -1\r\n", "output": "2\r\n"}, {"input": "17\r\n-1 -1 -1 -1 64 68 72 -1 45 46 47 48 49 50 51 52 53\r\n", "output": "2\r\n"}, {"input": "18\r\n21 19 -1 -1 -1 48 50 -1 54 -1 5 1 -1 -1 -1 37 36 35\r\n", "output": "4\r\n"}, {"input": "19\r\n23 26 -1 -1 35 38 41 -1 -1 -1 53 -1 59 62 6 7 8 9 -1\r\n", "output": "2\r\n"}, {"input": "6\r\n-1 2 6 -1 -1 6\r\n", "output": "2\r\n"}, {"input": "8\r\n-1 -1 1 7 -1 9 5 2\r\n", "output": "3\r\n"}, {"input": "20\r\n-1 32 37 -1 -1 -1 57 -1 -1 40 31 33 -1 -1 39 47 43 -1 35 32\r\n", "output": "5\r\n"}, {"input": "13\r\n2 -1 3 1 3 1 -1 1 3 -1 -1 1 1\r\n", "output": "6\r\n"}, {"input": "3\r\n-1 1 -1\r\n", "output": "1\r\n"}]
false
stdio
null
true
600/C
600
C
Python 3
TESTS
6
62
204,800
111955339
counts = [0]*26 for c in input(): counts[ord(c)-97] += 1 # print(*counts) ans = "" i = 26 while i: i -= 1 if counts[i]&1: # Find min thing with odd count and increase that j = 0 while j < 26 and counts[j]&1 < 1: j += 1 if j < 26 and counts[j]&1: counts[i] -= 1 counts[j] += 1 break # print(ans) for i in range(25, -1, -1): add = chr(i+97) * (counts[i]//2) ans = add + ans + add if counts[i]&1: l = len(ans)//2 ans = ans[:l] + chr(i+97) + ans[l:] print(ans)
66
62
2,150,400
215107109
def rl(): return list(map(int,input().split())) def ri(): return int(input()) def rs(): return input() def rm(): return map(int,input().split()) def main(): s=rs() cnt=[0]*26 for c in s: cnt[ord(c)-97]+=1 od=ev=0 for i in range(26): od+=cnt[i]%2==1 ev+=cnt[i]%2==0 cur=0 for i in range(26): if cnt[i]%2==0: continue if cur<(od//2): cnt[i]+=1 elif cur>=((od+1)//2): cnt[i]-=1 cur+=1 ans=[] if od%2==0: for i in range(26): ans.append(chr(i+97)*(cnt[i]//2)) for i in range(25,-1,-1): ans.append(chr(i+97)*(cnt[i]//2)) else: for i in range(26): ans.append(chr(i+97)*(cnt[i]//2)) for i in range(26): if cnt[i]%2==1: ans.append(chr(i+97)) for i in range(25,-1,-1): ans.append(chr(i+97)*(cnt[i]//2)) return "".join(ans) print(main())
Educational Codeforces Round 2
ICPC
2,015
2
256
Make Palindrome
A string is called palindrome if it reads the same from left to right and from right to left. For example "kazak", "oo", "r" and "mikhailrubinchikkihcniburliahkim" are palindroms, but strings "abb" and "ij" are not. You are given string s consisting of lowercase Latin letters. At once you can choose any position in the string and change letter in that position to any other lowercase letter. So after each changing the length of the string doesn't change. At first you can change some letters in s. Then you can permute the order of letters as you want. Permutation doesn't count as changes. You should obtain palindrome with the minimal number of changes. If there are several ways to do that you should get the lexicographically (alphabetically) smallest palindrome. So firstly you should minimize the number of changes and then minimize the palindrome lexicographically.
The only line contains string s (1 ≤ |s| ≤ 2·105) consisting of only lowercase Latin letters.
Print the lexicographically smallest palindrome that can be obtained with the minimal number of changes.
null
null
[{"input": "aabc", "output": "abba"}, {"input": "aabcd", "output": "abcba"}]
1,800
["constructive algorithms", "greedy", "strings"]
66
[{"input": "aabc\r\n", "output": "abba\r\n"}, {"input": "aabcd\r\n", "output": "abcba\r\n"}, {"input": "u\r\n", "output": "u\r\n"}, {"input": "ttttt\r\n", "output": "ttttt\r\n"}, {"input": "xxxvvvxxvv\r\n", "output": "vvvxxxxvvv\r\n"}, {"input": "wrwrwfrrfrffrrwwwffffwrfrrwfrrfrwwfwfrwfwfwffwrrwfrrrwwwfrrrwfrrfwrwwrwrrrffffwrrrwrwfffwrffrwwwrwww\r\n", "output": "fffffffffffffffrrrrrrrrrrrrrrrrrrwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwrrrrrrrrrrrrrrrrrrfffffffffffffff\r\n"}, {"input": "aabbcccdd\r\n", "output": "abcdcdcba\r\n"}, {"input": "baaab\r\n", "output": "ababa\r\n"}, {"input": "aaabbbhhlhlugkjgckj\r\n", "output": "aabbghjklclkjhgbbaa\r\n"}, {"input": "aabcc\r\n", "output": "acbca\r\n"}, {"input": "bbbcccddd\r\n", "output": "bbcdcdcbb\r\n"}, {"input": "zzzozzozozozoza\r\n", "output": "aoozzzzozzzzooa\r\n"}, {"input": "aaabb\r\n", "output": "ababa\r\n"}, {"input": "zza\r\n", "output": "zaz\r\n"}, {"input": "azzzbbb\r\n", "output": "abzbzba\r\n"}, {"input": "bbaaccddc\r\n", "output": "abcdcdcba\r\n"}, {"input": "aaabbbccc\r\n", "output": "aabcbcbaa\r\n"}, {"input": "aaaaabbccdd\r\n", "output": "aabcdadcbaa\r\n"}, {"input": "aaabbbcccdd\r\n", "output": "aabcdbdcbaa\r\n"}, {"input": "aaaabbcccccdd\r\n", "output": "aabccdcdccbaa\r\n"}, {"input": "aaacccb\r\n", "output": "aacbcaa\r\n"}, {"input": "abcd\r\n", "output": "abba\r\n"}, {"input": "abb\r\n", "output": "bab\r\n"}, {"input": "abababccc\r\n", "output": "aabcbcbaa\r\n"}, {"input": "aaadd\r\n", "output": "adada\r\n"}, {"input": "qqqqaaaccdd\r\n", "output": "acdqqaqqdca\r\n"}, {"input": "affawwzzw\r\n", "output": "afwzwzwfa\r\n"}, {"input": "hack\r\n", "output": "acca\r\n"}, {"input": "bbaaa\r\n", "output": "ababa\r\n"}, {"input": "ababa\r\n", "output": "ababa\r\n"}, {"input": "aaazzzz\r\n", "output": "azzazza\r\n"}, {"input": "aabbbcc\r\n", "output": "abcbcba\r\n"}, {"input": "successfullhack\r\n", "output": "accelsufuslecca\r\n"}, {"input": "aaabbccdd\r\n", "output": "abcdadcba\r\n"}, {"input": "zaz\r\n", "output": "zaz\r\n"}, {"input": "aaabbbcccdddeee\r\n", "output": "aabbcdecedcbbaa\r\n"}, {"input": "zaaz\r\n", "output": "azza\r\n"}, {"input": "acc\r\n", "output": "cac\r\n"}, {"input": "abbbzzz\r\n", "output": "abzbzba\r\n"}, {"input": "zzzzazazazazazznnznznnznnznznzaajzjajjjjanaznnzanzppnzpaznnpanz\r\n", "output": "aaaaaaajjjnnnnnnnnppzzzzzzzzzzznzzzzzzzzzzzppnnnnnnnnjjjaaaaaaa\r\n"}, {"input": "aaaaabbbcccdddd\r\n", "output": "aaabcddbddcbaaa\r\n"}, {"input": "aaaaabbccdddd\r\n", "output": "aabcddaddcbaa\r\n"}, {"input": "abababa\r\n", "output": "aabbbaa\r\n"}, {"input": "azz\r\n", "output": "zaz\r\n"}, {"input": "abbbccc\r\n", "output": "abcbcba\r\n"}, {"input": "aaacccddd\r\n", "output": "aacdcdcaa\r\n"}, {"input": "asbbsha\r\n", "output": "abshsba\r\n"}, {"input": "bababab\r\n", "output": "abbabba\r\n"}, {"input": "aaabbccddbbccddaaaaaaaaaaaaaaaa\r\n", "output": "aaaaaaaaabbccddaddccbbaaaaaaaaa\r\n"}, {"input": "aaabbccddbbccddaaaaaaaaaaaaaa\r\n", "output": "aaaaaaaabbccddaddccbbaaaaaaaa\r\n"}, {"input": "aaabbccddbbccddaaaaaaaaaaaa\r\n", "output": "aaaaaaabbccddaddccbbaaaaaaa\r\n"}, {"input": "ooooo\r\n", "output": "ooooo\r\n"}, {"input": "aaabbccddbbccddaaaaaaaaaa\r\n", "output": "aaaaaabbccddaddccbbaaaaaa\r\n"}, {"input": "aaabbccddbbccddaaaaaaaa\r\n", "output": "aaaaabbccddaddccbbaaaaa\r\n"}, {"input": "aaabbccddbbccddaa\r\n", "output": "aabbccddaddccbbaa\r\n"}]
false
stdio
null
true
837/D
837
D
PyPy 3
TESTS
24
514
5,120,000
131910761
import sys input = sys.stdin.readline n, k = map(int, input().split()) m = 18 * n a = list(map(int, input().split())) inf = 114514 dp = [[-inf] * (m + 1) for _ in range(n + 1)] dp[0][0] = 0 c = 0 for i in range(n): ai = a[i] x, y = 0, 0 while not ai % 2: x += 1 ai //= 2 while not ai % 5: y += 1 ai //= 5 for j in range(i, -1, -1): for l in range(min(c, m) + 1): if l + x <= m: dp[j + 1][l + x] = max(dp[j + 1][l + x], dp[j][l] + y) c += x ans = 0 for i in range(m + 1): ans = max(ans, min(i, dp[k][i])) print(ans)
99
1,076
7,987,200
131912158
import sys input = sys.stdin.readline n, k = map(int, input().split()) m = 30 * n a = list(map(int, input().split())) inf = 114514 dp = [[-inf] * (m + 1) for _ in range(n + 1)] dp[0][0] = 0 dp0 = [0] * (n + 1) for i in range(n): ai = a[i] x, y = 0, 0 while not ai % 2: y += 1 ai //= 2 while not ai % 5: x += 1 ai //= 5 for j in range(i, -1, -1): for l in range(dp0[i] + 1): if l + x <= m: dp[j + 1][l + x] = max(dp[j + 1][l + x], dp[j][l] + y) for j in range(i, -1, -1): dp0[j + 1] = max(dp0[j] + x, dp0[j + 1]) ans = 0 for i in range(dp0[k] + 1): ans = max(ans, min(i, dp[k][i])) print(ans)
Educational Codeforces Round 26
ICPC
2,017
2
256
Round Subset
Let's call the roundness of the number the number of zeros to which it ends. You have an array of n numbers. You need to choose a subset of exactly k numbers so that the roundness of the product of the selected numbers will be maximum possible.
The first line contains two integer numbers n and k (1 ≤ n ≤ 200, 1 ≤ k ≤ n). The second line contains n space-separated integer numbers a1, a2, ..., an (1 ≤ ai ≤ 1018).
Print maximal roundness of product of the chosen subset of length k.
null
In the first example there are 3 subsets of 2 numbers. [50, 4] has product 200 with roundness 2, [4, 20] — product 80, roundness 1, [50, 20] — product 1000, roundness 3. In the second example subset [15, 16, 25] has product 6000, roundness 3. In the third example all subsets has product with roundness 0.
[{"input": "3 2\n50 4 20", "output": "3"}, {"input": "5 3\n15 16 3 25 9", "output": "3"}, {"input": "3 3\n9 77 13", "output": "0"}]
2,100
["dp", "math"]
99
[{"input": "3 2\r\n50 4 20\r\n", "output": "3\r\n"}, {"input": "5 3\r\n15 16 3 25 9\r\n", "output": "3\r\n"}, {"input": "3 3\r\n9 77 13\r\n", "output": "0\r\n"}, {"input": "1 1\r\n200000000\r\n", "output": "8\r\n"}, {"input": "1 1\r\n3\r\n", "output": "0\r\n"}, {"input": "3 1\r\n1000000000000000000 800000000000000000 625\r\n", "output": "18\r\n"}, {"input": "20 13\r\n93050001 1 750000001 950000001 160250001 482000001 145875001 900000001 500000001 513300001 313620001 724750001 205800001 400000001 800000001 175000001 875000001 852686005 868880001 342500001\r\n", "output": "0\r\n"}, {"input": "5 3\r\n1360922189858001 5513375057164001 4060879738933651 3260997351273601 5540397778584001\r\n", "output": "0\r\n"}, {"input": "5 3\r\n670206146698567481 75620705254979501 828058059097865201 67124386759325201 946737848872942801\r\n", "output": "0\r\n"}, {"input": "5 4\r\n539134530963895499 265657472022483040 798956216114326361 930406714691011229 562844921643925634\r\n", "output": "1\r\n"}, {"input": "200 10\r\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "200 50\r\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "200 100\r\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "200 200\r\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "5 2\r\n625 5 100 16 10\r\n", "output": "4\r\n"}, {"input": "5 2\r\n64 32 16 8 3125\r\n", "output": "5\r\n"}, {"input": "2 2\r\n2199023255552 11920928955078125\r\n", "output": "23\r\n"}, {"input": "1 1\r\n500\r\n", "output": "2\r\n"}, {"input": "3 1\r\n125 10 8\r\n", "output": "1\r\n"}, {"input": "7 5\r\n50 312500 10000 1250 2000000 250 1250000\r\n", "output": "18\r\n"}, {"input": "4 2\r\n3125 1000 1000 32\r\n", "output": "6\r\n"}, {"input": "3 1\r\n4 10 25\r\n", "output": "1\r\n"}, {"input": "3 1\r\n16 10 75\r\n", "output": "1\r\n"}, {"input": "3 2\r\n100 5120 19531250\r\n", "output": "11\r\n"}, {"input": "4 2\r\n16 200 500 625\r\n", "output": "5\r\n"}, {"input": "7 4\r\n5000 1250 100 200 10 5000 640\r\n", "output": "13\r\n"}, {"input": "2 1\r\n125 10\r\n", "output": "1\r\n"}, {"input": "3 2\r\n16 625 10\r\n", "output": "4\r\n"}, {"input": "4 2\r\n10 4 25 5\r\n", "output": "2\r\n"}, {"input": "2 1\r\n10 25\r\n", "output": "1\r\n"}, {"input": "3 1\r\n10 16 625\r\n", "output": "1\r\n"}, {"input": "4 2\r\n10 200 625 16\r\n", "output": "4\r\n"}, {"input": "3 1\r\n1000 16 625\r\n", "output": "3\r\n"}, {"input": "3 2\r\n6250 100 160\r\n", "output": "6\r\n"}, {"input": "3 2\r\n1250 100 80\r\n", "output": "5\r\n"}, {"input": "3 2\r\n1024 1000 9765625\r\n", "output": "10\r\n"}, {"input": "5 2\r\n125 10 10 3 3\r\n", "output": "2\r\n"}, {"input": "4 2\r\n31250 200 500 320\r\n", "output": "7\r\n"}, {"input": "3 1\r\n16 10 125\r\n", "output": "1\r\n"}, {"input": "5 3\r\n125 8 1000 100 100\r\n", "output": "7\r\n"}, {"input": "3 2\r\n25 4 10\r\n", "output": "2\r\n"}, {"input": "3 2\r\n125 8 10\r\n", "output": "3\r\n"}, {"input": "6 2\r\n125 125 100 100 8 8\r\n", "output": "4\r\n"}, {"input": "3 1\r\n32 3125 10\r\n", "output": "1\r\n"}, {"input": "5 3\r\n4 5 125 16 1\r\n", "output": "4\r\n"}, {"input": "7 4\r\n5 125 2 5 2 5 20\r\n", "output": "4\r\n"}, {"input": "200 200\r\n5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 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 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 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2\r\n", "output": "100\r\n"}, {"input": "2 1\r\n200 250\r\n", "output": "2\r\n"}, {"input": "5 3\r\n500 10 250 20 8\r\n", "output": "6\r\n"}, {"input": "4 2\r\n2 2 25 1\r\n", "output": "1\r\n"}, {"input": "2 2\r\n16 25\r\n", "output": "2\r\n"}]
false
stdio
null
true
799/D
799
D
PyPy 3-64
TESTS
26
265
20,684,800
204499804
from math import * a,b,w,h,n=map(int,input().split()) w,h=ceil(a/w),ceil(b/h) MAX=100000 MAX=min(MAX,max(w,h)) if w<=1 and h<=1: print(0) else: l=list(map(int,input().split())) l.sort() l.reverse() l=l[:34] dp=[0 for i in range(MAX+1)] #max width for height i dp[0]=MAX*MAX dp[1]=1 gg=0 for i in range(len(l)): ext=l[i] dp2=dp[:] for j in range(MAX+1): #print(j,dp[ceil(j/ext)],ceil(j/ext)) dp2[j]=max(ext*dp[j],dp[ceil(j/ext)]) dp=dp2[:] if dp[h]>=w: gg=1 break if gg: print(i+1) else: print(-1)
103
62
2,560,000
230893177
f = lambda: map(int, input().split()) a, b, h, w, n = f() c = sorted(list(f()), key=lambda x: -x) d = {(h, w), (w, h)} for i, q in enumerate([1] + c): for u, v in d.copy(): h, w = u, v * q if a <= w and b <= h or a <= h and b <= w: print(i) exit() d.add((h, w)) d.add((w, h)) print(-1)
Playrix Codescapes Cup (Codeforces Round 413, rated, Div. 1 + Div. 2)
CF
2,017
1
256
Field expansion
In one of the games Arkady is fond of the game process happens on a rectangular field. In the game process Arkady can buy extensions for his field, each extension enlarges one of the field sizes in a particular number of times. Formally, there are n extensions, the i-th of them multiplies the width or the length (by Arkady's choice) by ai. Each extension can't be used more than once, the extensions can be used in any order. Now Arkady's field has size h × w. He wants to enlarge it so that it is possible to place a rectangle of size a × b on it (along the width or along the length, with sides parallel to the field sides). Find the minimum number of extensions needed to reach Arkady's goal.
The first line contains five integers a, b, h, w and n (1 ≤ a, b, h, w, n ≤ 100 000) — the sizes of the rectangle needed to be placed, the initial sizes of the field and the number of available extensions. The second line contains n integers a1, a2, ..., an (2 ≤ ai ≤ 100 000), where ai equals the integer a side multiplies by when the i-th extension is applied.
Print the minimum number of extensions needed to reach Arkady's goal. If it is not possible to place the rectangle on the field with all extensions, print -1. If the rectangle can be placed on the initial field, print 0.
null
In the first example it is enough to use any of the extensions available. For example, we can enlarge h in 5 times using the second extension. Then h becomes equal 10 and it is now possible to place the rectangle on the field.
[{"input": "3 3 2 4 4\n2 5 4 10", "output": "1"}, {"input": "3 3 3 3 5\n2 3 5 4 2", "output": "0"}, {"input": "5 5 1 2 3\n2 2 3", "output": "-1"}, {"input": "3 4 1 1 3\n2 3 2", "output": "3"}]
2,100
["brute force", "dp", "meet-in-the-middle"]
103
[{"input": "3 3 2 4 4\r\n2 5 4 10\r\n", "output": "1\r\n"}, {"input": "3 3 3 3 5\r\n2 3 5 4 2\r\n", "output": "0\r\n"}, {"input": "5 5 1 2 3\r\n2 2 3\r\n", "output": "-1\r\n"}, {"input": "3 4 1 1 3\r\n2 3 2\r\n", "output": "3\r\n"}, {"input": "572 540 6 2 12\r\n2 3 2 2 2 3 3 3 2 2 2 2\r\n", "output": "-1\r\n"}, {"input": "375 905 1 1 17\r\n2 2 3 3 3 3 3 3 2 2 2 2 3 2 2 2 3\r\n", "output": "14\r\n"}, {"input": "37 23 4 1 16\r\n2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2\r\n", "output": "9\r\n"}, {"input": "20 19 6 8 18\r\n3 4 2 3 4 3 2 4 2 2 4 2 4 3 2 4 4 2\r\n", "output": "2\r\n"}, {"input": "11 11 5 3 11\r\n4 4 2 4 3 2 2 3 2 2 3\r\n", "output": "2\r\n"}, {"input": "100000 100000 1 1 100\r\n2 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 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 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2\r\n", "output": "34\r\n"}, {"input": "642 694 4 7 15\r\n2 4 2 3 3 4 4 3 3 2 2 4 3 2 2\r\n", "output": "8\r\n"}, {"input": "100000 100000 1 1 2\r\n100000 99999\r\n", "output": "-1\r\n"}, {"input": "100000 100000 99999 99999 2\r\n30000 30000\r\n", "output": "2\r\n"}, {"input": "41628 25266 1 1 36\r\n2 2 2 3 2 2 2 2 3 3 2 3 2 3 3 3 3 2 3 2 2 3 3 3 2 2 2 2 2 2 2 2 2 2 2 3\r\n", "output": "23\r\n"}, {"input": "34640 40496 1 1 107\r\n2 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 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 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\r\n", "output": "32\r\n"}, {"input": "32716 43645 4 1 102\r\n2 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 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 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2\r\n", "output": "29\r\n"}, {"input": "24812 24973 8 4 83\r\n2 2 2 2 3 3 3 2 4 2 4 3 3 2 2 4 4 3 4 2 2 4 3 2 3 2 3 2 4 4 2 3 3 3 3 4 3 3 2 3 4 4 2 4 4 3 3 4 4 4 4 4 3 4 4 2 3 3 3 2 4 3 2 3 3 2 4 2 2 4 2 3 4 3 2 2 4 2 4 3 2 2 3\r\n", "output": "13\r\n"}, {"input": "21865 53623 9 7 116\r\n2 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 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 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\r\n", "output": "25\r\n"}, {"input": "21336 19210 1 1 73\r\n4 4 3 4 4 2 3 2 4 2 3 2 4 2 4 4 2 3 4 3 4 3 2 3 3 3 2 4 2 2 3 4 2 2 3 3 4 3 3 3 3 4 2 4 2 3 3 4 4 2 4 4 2 3 4 3 4 3 3 4 2 4 4 4 2 2 3 3 2 4 4 2 2\r\n", "output": "16\r\n"}, {"input": "48490 41653 1 1 53\r\n2 4 2 3 4 3 4 4 4 3 2 3 4 4 2 2 3 3 3 3 2 4 3 2 2 3 4 3 3 2 2 4 4 4 4 3 4 4 4 2 4 2 2 2 4 2 2 4 2 3 3 2 2\r\n", "output": "16\r\n"}, {"input": "33817 19277 7 8 192\r\n2 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 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 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 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 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\r\n", "output": "25\r\n"}, {"input": "63129 28077 1 1 31\r\n3 3 4 3 2 2 3 4 3 4 4 3 3 2 3 3 4 3 3 3 2 3 2 3 4 2 4 3 4 2 2\r\n", "output": "18\r\n"}, {"input": "11731 17857 6 7 21\r\n2 3 2 3 3 2 3 4 3 3 2 3 2 3 4 3 2 4 3 2 2\r\n", "output": "14\r\n"}, {"input": "82424 40643 9 2 200\r\n2 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 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 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 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 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\r\n", "output": "29\r\n"}, {"input": "1 1 1 1 1\r\n100000\r\n", "output": "0\r\n"}, {"input": "100000 100000 1 1 2\r\n100000 100000\r\n", "output": "2\r\n"}, {"input": "100000 100000 100000 100000 1\r\n2\r\n", "output": "0\r\n"}, {"input": "496 390 6 8 15\r\n4 2 4 4 2 4 2 3 2 4 3 2 2 2 3\r\n", "output": "7\r\n"}, {"input": "625 389 1 3 20\r\n3 2 2 3 4 2 3 2 2 2 3 4 4 4 4 3 4 3 3 3\r\n", "output": "9\r\n"}, {"input": "154 206 6 1 12\r\n3 2 3 3 2 3 3 2 3 2 2 2\r\n", "output": "9\r\n"}, {"input": "405 449 1 5 16\r\n2 2 2 3 3 2 2 3 2 3 2 2 3 3 3 3\r\n", "output": "11\r\n"}, {"input": "662 859 2 3 17\r\n3 2 2 2 3 3 3 2 3 3 2 3 2 2 2 2 2\r\n", "output": "13\r\n"}, {"input": "255 289 2 2 14\r\n4 3 3 3 3 4 4 4 3 3 4 3 3 2\r\n", "output": "8\r\n"}, {"input": "596 688 1 6 19\r\n3 4 4 2 2 4 2 3 4 2 2 3 3 3 2 2 2 4 3\r\n", "output": "9\r\n"}, {"input": "133 127 8 8 10\r\n4 2 3 2 2 3 4 2 3 3\r\n", "output": "5\r\n"}, {"input": "32804 32321 10 13 34\r\n3 3 3 2 3 2 2 2 2 3 2 2 2 2 2 3 3 3 2 2 3 3 3 2 2 2 3 3 2 2 2 2 3 2\r\n", "output": "16\r\n"}, {"input": "95589 93171 13 11 34\r\n2 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\r\n", "output": "27\r\n"}, {"input": "16526 20394 2 2 21\r\n2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2\r\n", "output": "-1\r\n"}, {"input": "63481 80094 3 2 200\r\n2 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 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 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 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 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\r\n", "output": "30\r\n"}, {"input": "13801 10319 7 7 30\r\n2 3 2 2 2 3 2 3 3 2 3 3 3 3 2 2 3 3 2 2 3 2 3 2 3 3 3 2 2 3\r\n", "output": "14\r\n"}, {"input": "100000 1 1 100000 3\r\n3 4 100000\r\n", "output": "0\r\n"}, {"input": "1 100000 100000 1 1\r\n100000\r\n", "output": "0\r\n"}, {"input": "100000 100000 1 100000 1\r\n100000\r\n", "output": "1\r\n"}, {"input": "100000 100000 100000 1 2\r\n300 300\r\n", "output": "-1\r\n"}, {"input": "100000 100000 100000 1 2\r\n100000 100000\r\n", "output": "1\r\n"}, {"input": "100000 100000 99999 99999 1\r\n30000\r\n", "output": "-1\r\n"}, {"input": "100000 100000 100000 99999 1\r\n30000\r\n", "output": "1\r\n"}, {"input": "100000 100000 99999 100000 1\r\n30000\r\n", "output": "1\r\n"}, {"input": "25 24 1 1 4\r\n4 5 6 5\r\n", "output": "4\r\n"}, {"input": "100000 100000 1 1 17\r\n2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59\r\n", "output": "7\r\n"}, {"input": "65536 78125 1 1 23\r\n2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 5 5 5 5 5 5 5\r\n", "output": "23\r\n"}, {"input": "78125 65536 1 1 23\r\n2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 5 5 5 5 5 5 5\r\n", "output": "23\r\n"}, {"input": "15625 65536 1 1 22\r\n2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 5 5 5 5 5 5\r\n", "output": "22\r\n"}, {"input": "65536 15625 1 1 22\r\n2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 5 5 5 5 5 5\r\n", "output": "22\r\n"}, {"input": "39366 39366 1 1 20\r\n3 3 3 3 3 3 3 3 3 2 3 3 3 3 3 3 3 3 3 2\r\n", "output": "20\r\n"}]
false
stdio
null
true
638/B
638
B
Python 3
PRETESTS
9
62
4,608,000
16843287
n = int(input()) arr = [] arr2 = "" for i in range(n): arr.append(str(input())) result = arr[0] z = 0 while len(arr) != 0: for i in range(len(arr)): if result.find(arr[i-z]) != -1: del arr[i-z] z+=1 continue elif arr[i-z].find(result)!= -1: result = arr[i-z] del arr[i-z] z+=1 continue if result.find(arr[i-z][0]) != -1: result += arr[i-z] del arr[i-z] z+=1 elif result.find(arr[i-z][-1]) != -1: result = arr[i-z] + result del arr[i-z] z+=1 if z == 0: result += arr[0] z = 0 for t in result: if arr2.find(t) != -1: continue arr2 += t print(arr2)
67
62
4,812,800
16855472
n=int(input()) s=set() for i in range(n): p=input() if i!=n-1: s.add(p) n-=1 w={} for i in s: w.update([(i,set(i))]) w.update([(p,set(p))]) y='' while n>0: u=0 for i in s: u+=1 if p[0] in w[i] and p[-1] in w[i]: p=i s.discard(i) break elif p[0] in w[i]: p=i[:i.find(p[0])]+p s.discard(i) break elif p[-1] in w[i]: p+=i[i.find(p[-1])+1:] s.discard(i) break elif i[0] in w[p] and i[-1] in w[p]: s.discard(i) break elif u==n: y+=p p=i s.discard(i) break w.update([(p,set(p))]) n-=1 p+=y print(p)
VK Cup 2016 - Qualification Round 2
CF
2,016
1
256
Making Genome in Berland
Berland scientists face a very important task - given the parts of short DNA fragments, restore the dinosaur DNA! The genome of a berland dinosaur has noting in common with the genome that we've used to: it can have 26 distinct nucleotide types, a nucleotide of each type can occur at most once. If we assign distinct English letters to all nucleotides, then the genome of a Berland dinosaur will represent a non-empty string consisting of small English letters, such that each letter occurs in it at most once. Scientists have n genome fragments that are represented as substrings (non-empty sequences of consecutive nucleotides) of the sought genome. You face the following problem: help scientists restore the dinosaur genome. It is guaranteed that the input is not contradictory and at least one suitable line always exists. When the scientists found out that you are a strong programmer, they asked you in addition to choose the one with the minimum length. If there are multiple such strings, choose any string.
The first line of the input contains a positive integer n (1 ≤ n ≤ 100) — the number of genome fragments. Each of the next lines contains one descriptions of a fragment. Each fragment is a non-empty string consisting of distinct small letters of the English alphabet. It is not guaranteed that the given fragments are distinct. Fragments could arbitrarily overlap and one fragment could be a substring of another one. It is guaranteed that there is such string of distinct letters that contains all the given fragments as substrings.
In the single line of the output print the genome of the minimum length that contains all the given parts. All the nucleotides in the genome must be distinct. If there are multiple suitable strings, print the string of the minimum length. If there also are multiple suitable strings, you can print any of them.
null
null
[{"input": "3\nbcd\nab\ncdef", "output": "abcdef"}, {"input": "4\nx\ny\nz\nw", "output": "xyzw"}]
1,500
["*special", "dfs and similar", "strings"]
67
[{"input": "3\r\nbcd\r\nab\r\ncdef\r\n", "output": "abcdef\r\n"}, {"input": "4\r\nx\r\ny\r\nz\r\nw\r\n", "output": "xyzw\r\n"}, {"input": "25\r\nef\r\nfg\r\ngh\r\nhi\r\nij\r\njk\r\nkl\r\nlm\r\nmn\r\nno\r\nab\r\nbc\r\ncd\r\nde\r\nop\r\npq\r\nqr\r\nrs\r\nst\r\ntu\r\nuv\r\nvw\r\nwx\r\nxy\r\nyz\r\n", "output": "abcdefghijklmnopqrstuvwxyz\r\n"}, {"input": "1\r\nf\r\n", "output": "f\r\n"}, {"input": "1\r\nqwertyuiopzxcvbnmasdfghjkl\r\n", "output": "qwertyuiopzxcvbnmasdfghjkl\r\n"}, {"input": "3\r\ndfghj\r\nghjkl\r\nasdfg\r\n", "output": "asdfghjkl\r\n"}, {"input": "4\r\nab\r\nab\r\nab\r\nabc\r\n", "output": "abc\r\n"}, {"input": "3\r\nf\r\nn\r\nux\r\n", "output": "uxfn\r\n"}, {"input": "2\r\nfgs\r\nfgs\r\n", "output": "fgs\r\n"}, {"input": "96\r\nc\r\ndhf\r\no\r\nq\r\nry\r\nh\r\nr\r\nf\r\nji\r\nek\r\ndhf\r\np\r\nk\r\no\r\nf\r\nw\r\nc\r\nc\r\nfgw\r\nbps\r\nhfg\r\np\r\ni\r\nji\r\nto\r\nc\r\nou\r\ny\r\nfg\r\na\r\ne\r\nu\r\nc\r\ny\r\nhf\r\nqn\r\nu\r\nj\r\np\r\ns\r\no\r\nmr\r\na\r\nqn\r\nb\r\nlb\r\nn\r\nji\r\nji\r\na\r\no\r\nat\r\ns\r\nf\r\nb\r\ndh\r\nk\r\nl\r\nl\r\nvq\r\nt\r\nb\r\nc\r\nv\r\nc\r\nh\r\nh\r\ny\r\nh\r\nq\r\ne\r\nx\r\nd\r\no\r\nq\r\nm\r\num\r\nmr\r\nfg\r\ni\r\nl\r\na\r\nh\r\nt\r\num\r\nr\r\no\r\nn\r\nk\r\ne\r\nji\r\na\r\nc\r\nh\r\ne\r\nm\r\n", "output": "atoumrydhfgwekjilbpsvqncx\r\n"}, {"input": "3\r\npbi\r\nopbi\r\ngh\r\n", "output": "ghopbi\r\n"}, {"input": "4\r\ng\r\np\r\no\r\nop\r\n", "output": "opg\r\n"}, {"input": "5\r\np\r\nf\r\nu\r\nf\r\np\r\n", "output": "pfu\r\n"}, {"input": "4\r\nr\r\nko\r\nuz\r\nko\r\n", "output": "kouzr\r\n"}, {"input": "5\r\nzt\r\nted\r\nlzt\r\nted\r\ndyv\r\n", "output": "lztedyv\r\n"}, {"input": "6\r\ngul\r\ng\r\njrb\r\nul\r\nd\r\njr\r\n", "output": "guljrbd\r\n"}, {"input": "5\r\nlkyh\r\naim\r\nkyh\r\nm\r\nkyhai\r\n", "output": "lkyhaim\r\n"}, {"input": "4\r\nzrncsywd\r\nsywdx\r\ngqzrn\r\nqzrncsy\r\n", "output": "gqzrncsywdx\r\n"}, {"input": "5\r\ntbxzc\r\njrdtb\r\njrdtb\r\nflnj\r\nrdtbx\r\n", "output": "flnjrdtbxzc\r\n"}, {"input": "10\r\ng\r\nkagijn\r\nzxt\r\nhmkag\r\nhm\r\njnc\r\nxtqupw\r\npwhmk\r\ng\r\nagi\r\n", "output": "zxtqupwhmkagijnc\r\n"}, {"input": "20\r\nf\r\nf\r\nv\r\nbn\r\ne\r\nmr\r\ne\r\ne\r\nn\r\nj\r\nqfv\r\ne\r\ndpb\r\nj\r\nlc\r\nr\r\ndp\r\nf\r\na\r\nrt\r\n", "output": "dpbnlcmrtqfveja\r\n"}, {"input": "30\r\nxlo\r\nwx\r\ne\r\nf\r\nyt\r\nw\r\ne\r\nl\r\nxl\r\nojg\r\njg\r\niy\r\ngkz\r\ne\r\nw\r\nloj\r\ng\r\nfw\r\nl\r\nlo\r\nbe\r\ne\r\ngk\r\niyt\r\no\r\nb\r\nqv\r\nz\r\nb\r\nzq\r\n", "output": "befwxlojgkzqviyt\r\n"}, {"input": "50\r\nmd\r\nei\r\nhy\r\naz\r\nzr\r\nmd\r\nv\r\nz\r\nke\r\ny\r\nuk\r\nf\r\nhy\r\njm\r\nke\r\njm\r\ncn\r\nwf\r\nzr\r\nqj\r\ng\r\nzr\r\ndv\r\ni\r\ndv\r\nuk\r\nj\r\nwf\r\njm\r\nn\r\na\r\nqj\r\nei\r\nf\r\nzr\r\naz\r\naz\r\nke\r\na\r\nr\r\ndv\r\nei\r\nzr\r\ndv\r\nq\r\ncn\r\nyg\r\nqj\r\nnh\r\nhy\r\n", "output": "azrcnhygqjmdvukeiwf\r\n"}, {"input": "80\r\ni\r\nioh\r\nquc\r\nexioh\r\niohb\r\nex\r\nrwky\r\nz\r\nquc\r\nrw\r\nplnt\r\nq\r\nhbrwk\r\nexioh\r\ntv\r\nxioh\r\nlnt\r\nxi\r\nn\r\npln\r\niohbr\r\nwky\r\nhbr\r\nw\r\nyq\r\nrwky\r\nbrw\r\nplnt\r\nv\r\nkyq\r\nrwkyq\r\nt\r\nhb\r\ngplnt\r\np\r\nkyqu\r\nhbr\r\nrwkyq\r\nhbr\r\nve\r\nhbrwk\r\nkyq\r\nkyquc\r\ngpln\r\ni\r\nbr\r\ntvex\r\nwkyqu\r\nz\r\nlnt\r\ngp\r\nky\r\ngplnt\r\ne\r\nhbrwk\r\nbrw\r\nve\r\no\r\nplnt\r\nn\r\nntve\r\ny\r\nln\r\npln\r\ntvexi\r\nr\r\nzgp\r\nxiohb\r\nl\r\nn\r\nt\r\nplnt\r\nlntv\r\nexi\r\nexi\r\ngpl\r\nioh\r\nk\r\nwk\r\ni\r\n", "output": "zgplntvexiohbrwkyquc\r\n"}, {"input": "70\r\njp\r\nz\r\nz\r\nd\r\ndy\r\nk\r\nsn\r\nrg\r\nz\r\nsn\r\nh\r\nj\r\ns\r\nkx\r\npu\r\nkx\r\nm\r\njp\r\nbo\r\nm\r\ntk\r\ndy\r\no\r\nm\r\nsn\r\nv\r\nrg\r\nv\r\nn\r\no\r\ngh\r\np\r\no\r\nx\r\nq\r\nzv\r\nr\r\nbo\r\ng\r\noz\r\nu\r\nub\r\nnd\r\nh\r\ny\r\njp\r\no\r\nq\r\nbo\r\nhq\r\nhq\r\nkx\r\nx\r\ndy\r\nn\r\nb\r\nub\r\nsn\r\np\r\nub\r\ntk\r\nu\r\nnd\r\nvw\r\nt\r\nub\r\nbo\r\nyr\r\nyr\r\nub\r\n", "output": "jpubozvwsndyrghqtkxm\r\n"}, {"input": "100\r\nm\r\nj\r\nj\r\nf\r\nk\r\nq\r\ni\r\nu\r\ni\r\nl\r\nt\r\nt\r\no\r\nv\r\nk\r\nw\r\nr\r\nj\r\nh\r\nx\r\nc\r\nv\r\nu\r\nf\r\nh\r\nj\r\nb\r\ne\r\ni\r\nr\r\ng\r\nb\r\nl\r\nb\r\ng\r\nb\r\nf\r\nq\r\nv\r\na\r\nu\r\nn\r\ni\r\nl\r\nk\r\nc\r\nx\r\nu\r\nr\r\ne\r\ni\r\na\r\nc\r\no\r\nc\r\na\r\nx\r\nd\r\nf\r\nx\r\no\r\nx\r\nm\r\nl\r\nr\r\nc\r\nr\r\nc\r\nv\r\nj\r\ng\r\nu\r\nn\r\nn\r\nd\r\nl\r\nl\r\nc\r\ng\r\nu\r\nr\r\nu\r\nh\r\nl\r\na\r\nl\r\nr\r\nt\r\nm\r\nf\r\nm\r\nc\r\nh\r\nl\r\nd\r\na\r\nr\r\nh\r\nn\r\nc\r\n", "output": "mjfkqiultovwrhxcbegand\r\n"}, {"input": "99\r\nia\r\nz\r\nsb\r\ne\r\nnm\r\nd\r\nknm\r\nt\r\nm\r\np\r\nqvu\r\ne\r\nq\r\nq\r\ns\r\nmd\r\nz\r\nfh\r\ne\r\nwi\r\nn\r\nsb\r\nq\r\nw\r\ni\r\ng\r\nr\r\ndf\r\nwi\r\nl\r\np\r\nm\r\nb\r\ni\r\natj\r\nb\r\nwia\r\nx\r\nnm\r\nlk\r\nx\r\nfh\r\nh\r\np\r\nf\r\nzr\r\nz\r\nr\r\nsbz\r\nlkn\r\nsbz\r\nz\r\na\r\nwia\r\ntjx\r\nk\r\nj\r\nx\r\nl\r\nqvu\r\nzr\r\nfh\r\nbzrg\r\nz\r\nplk\r\nfhe\r\nn\r\njxqv\r\nrgp\r\ne\r\ndf\r\nz\r\ns\r\natj\r\ndf\r\nat\r\ngp\r\nw\r\new\r\nt\r\np\r\np\r\nfhe\r\nq\r\nxq\r\nt\r\nzr\r\nat\r\ndfh\r\nj\r\ns\r\nu\r\npl\r\np\r\nrg\r\nlk\r\nq\r\nwia\r\ng\r\n", "output": "sbzrgplknmdfhewiatjxqvu\r\n"}, {"input": "95\r\np\r\nk\r\nd\r\nr\r\nn\r\nz\r\nn\r\nb\r\np\r\nw\r\ni\r\nn\r\ny\r\ni\r\nn\r\nn\r\ne\r\nr\r\nu\r\nr\r\nb\r\ni\r\ne\r\np\r\nk\r\nc\r\nc\r\nh\r\np\r\nk\r\nh\r\ns\r\ne\r\ny\r\nq\r\nq\r\nx\r\nw\r\nh\r\ng\r\nt\r\nt\r\na\r\nt\r\nh\r\ni\r\nb\r\ne\r\np\r\nr\r\nu\r\nn\r\nn\r\nr\r\nq\r\nn\r\nu\r\ng\r\nw\r\nt\r\np\r\nt\r\nk\r\nd\r\nz\r\nh\r\nf\r\nd\r\ni\r\na\r\na\r\nf\r\ne\r\na\r\np\r\ns\r\nk\r\nt\r\ng\r\nf\r\ni\r\ng\r\ng\r\nt\r\nn\r\nn\r\nt\r\nt\r\nr\r\nx\r\na\r\nz\r\nc\r\nn\r\nk\r\n", "output": "pkdrnzbwiyeuchsqxgtaf\r\n"}, {"input": "3\r\nh\r\nx\r\np\r\n", "output": "hxp\r\n"}, {"input": "4\r\nrz\r\nvu\r\nxy\r\npg\r\n", "output": "pgrzvuxy\r\n"}, {"input": "5\r\ndrw\r\nu\r\nzq\r\npd\r\naip\r\n", "output": "aipdrwzqu\r\n"}, {"input": "70\r\ne\r\no\r\ng\r\ns\r\nsz\r\nyl\r\ns\r\nn\r\no\r\nq\r\np\r\nl\r\noa\r\ndq\r\ny\r\np\r\nn\r\nio\r\ng\r\nb\r\nk\r\nv\r\ny\r\nje\r\nc\r\ncb\r\nfx\r\ncbv\r\nfxp\r\nkt\r\nhm\r\nz\r\nrcb\r\np\r\nt\r\nu\r\nzh\r\ne\r\nb\r\na\r\nyl\r\nd\r\nv\r\nl\r\nrc\r\nq\r\nt\r\nt\r\nj\r\nl\r\nr\r\ny\r\nlg\r\np\r\nt\r\nd\r\nq\r\nje\r\nqwu\r\ng\r\nz\r\ngi\r\ndqw\r\nz\r\nvyl\r\nk\r\nt\r\nc\r\nb\r\nrc\r\n", "output": "dqwufxpjektrcbvylgioaszhmn\r\n"}, {"input": "3\r\ne\r\nw\r\nox\r\n", "output": "oxew\r\n"}, {"input": "100\r\npr\r\nfz\r\nru\r\ntk\r\nld\r\nvq\r\nef\r\ngj\r\ncp\r\nbm\r\nsn\r\nld\r\nua\r\nzl\r\ndw\r\nef\r\nua\r\nbm\r\nxb\r\nvq\r\nav\r\ncp\r\nko\r\nwc\r\nru\r\ni\r\ne\r\nav\r\nbm\r\nav\r\nxb\r\nog\r\ng\r\nme\r\ntk\r\nog\r\nxb\r\nef\r\ntk\r\nhx\r\nqt\r\nvq\r\ndw\r\nv\r\nxb\r\ndw\r\nko\r\nd\r\nbm\r\nua\r\nvq\r\nis\r\nwc\r\ntk\r\ntk\r\ngj\r\ng\r\ngj\r\nef\r\nqt\r\nvq\r\nbm\r\nog\r\nvq\r\ngj\r\nvq\r\nzl\r\ngj\r\nji\r\nvq\r\nhx\r\ng\r\nbm\r\nji\r\nqt\r\nef\r\nav\r\ntk\r\nxb\r\nru\r\nko\r\nny\r\nis\r\ncp\r\nxb\r\nog\r\nru\r\nhx\r\nwc\r\nko\r\nu\r\nfz\r\ndw\r\nji\r\nzl\r\nvq\r\nqt\r\nko\r\ngj\r\nis\r\n", "output": "hxbmefzldwcpruavqtkogjisny\r\n"}, {"input": "23\r\nw\r\nz\r\nk\r\nc\r\ne\r\np\r\nt\r\na\r\nx\r\nc\r\nq\r\nx\r\na\r\nf\r\np\r\nw\r\nh\r\nx\r\nf\r\nw\r\np\r\nw\r\nq\r\n", "output": "wzkceptaxqfh\r\n"}, {"input": "12\r\nu\r\na\r\nhw\r\na\r\ngh\r\nog\r\nr\r\nd\r\nw\r\nk\r\nl\r\ny\r\n", "output": "oghwuardkly\r\n"}, {"input": "2\r\ny\r\nd\r\n", "output": "yd\r\n"}, {"input": "1\r\nd\r\n", "output": "d\r\n"}, {"input": "100\r\nwm\r\nq\r\nhf\r\nwm\r\niz\r\ndl\r\nmiz\r\np\r\nzoa\r\nbk\r\nw\r\nxv\r\nfj\r\nd\r\nxvsg\r\nr\r\nx\r\nt\r\nyd\r\nbke\r\ny\r\neq\r\nx\r\nn\r\nry\r\nt\r\nc\r\nuh\r\nn\r\npw\r\nuhf\r\neq\r\nr\r\nw\r\nk\r\nt\r\nsg\r\njb\r\nd\r\nke\r\ne\r\nx\r\nh\r\ntuh\r\nan\r\nn\r\noa\r\nw\r\nq\r\nz\r\nk\r\noan\r\nbk\r\nj\r\nzoan\r\nyd\r\npwmi\r\nyd\r\nc\r\nry\r\nfj\r\nlx\r\nqr\r\nke\r\nizo\r\nm\r\nz\r\noan\r\nwmi\r\nl\r\nyd\r\nz\r\ns\r\nke\r\nw\r\nfjbk\r\nqry\r\nlxv\r\nhf\r\ns\r\nnc\r\nq\r\nlxv\r\nzoa\r\nn\r\nfj\r\np\r\nhf\r\nmiz\r\npwm\r\ntu\r\noan\r\ng\r\nd\r\nqr\r\na\r\nan\r\nxvs\r\ny\r\ntuhf\r\n", "output": "pwmizoanctuhfjbkeqrydlxvsg\r\n"}, {"input": "94\r\ncw\r\nm\r\nuhbk\r\ntfy\r\nsd\r\nu\r\ntf\r\ntfym\r\nfy\r\nbk\r\nx\r\nx\r\nxl\r\npu\r\noq\r\nkt\r\ny\r\nb\r\nj\r\nqxl\r\no\r\noqx\r\nr\r\nr\r\njr\r\nk\r\ne\r\nw\r\nsd\r\na\r\nljre\r\nhbk\r\nym\r\nxl\r\np\r\nreg\r\nktf\r\nre\r\nw\r\nhbk\r\nxlj\r\nzn\r\ne\r\nm\r\nms\r\nsdv\r\nr\r\nr\r\no\r\naoq\r\nzna\r\nymsd\r\nqx\r\nr\r\no\r\nlj\r\nm\r\nk\r\nu\r\nkt\r\nms\r\ne\r\nx\r\nh\r\ni\r\nz\r\nm\r\nc\r\nb\r\no\r\nm\r\nvcw\r\ndvc\r\nq\r\na\r\nb\r\nfyms\r\nv\r\nxl\r\nxl\r\ntfym\r\nx\r\nfy\r\np\r\nyms\r\nms\r\nb\r\nt\r\nu\r\nn\r\nq\r\nnaoqx\r\no\r\ne\r\n", "output": "puhbktfymsdvcwznaoqxljregi\r\n"}, {"input": "13\r\ngku\r\nzw\r\nstvqc\r\najy\r\njystvq\r\nfilden\r\nstvq\r\nfild\r\nqcporh\r\najys\r\nqcpor\r\nqcpor\r\ncporhm\r\n", "output": "ajystvqcporhmfildengkuzw\r\n"}, {"input": "2\r\not\r\nqu\r\n", "output": "otqu\r\n"}, {"input": "100\r\nv\r\nh\r\nj\r\nf\r\nr\r\ni\r\ns\r\nw\r\nv\r\nd\r\nv\r\np\r\nd\r\nu\r\ny\r\nd\r\nu\r\nx\r\nr\r\nu\r\ng\r\nm\r\ns\r\nf\r\nv\r\nx\r\na\r\ng\r\ng\r\ni\r\ny\r\ny\r\nv\r\nd\r\ni\r\nq\r\nq\r\nu\r\nx\r\nj\r\nv\r\nj\r\ne\r\no\r\nr\r\nh\r\nu\r\ne\r\nd\r\nv\r\nb\r\nv\r\nq\r\nk\r\ni\r\nr\r\ne\r\nm\r\na\r\nj\r\na\r\nu\r\nq\r\nx\r\nq\r\ny\r\ns\r\nw\r\nk\r\ni\r\ns\r\nr\r\np\r\ni\r\np\r\ns\r\nd\r\nj\r\nw\r\no\r\nm\r\ns\r\nr\r\nd\r\nf\r\ns\r\nw\r\nv\r\ne\r\ny\r\no\r\nx\r\na\r\np\r\nk\r\nr\r\ng\r\ng\r\nb\r\nq\r\n", "output": "vhjfriswdpuyxgmaqeobk\r\n"}, {"input": "99\r\ntnq\r\nep\r\nuk\r\nk\r\nx\r\nvhy\r\nepj\r\nx\r\nj\r\nhy\r\nukg\r\nsep\r\nquk\r\nr\r\nw\r\no\r\nxrwm\r\ndl\r\nh\r\no\r\nad\r\ng\r\ng\r\nhy\r\nxr\r\nad\r\nhyx\r\nkg\r\nvh\r\nb\r\nlovh\r\nuk\r\nl\r\ntn\r\nkg\r\ny\r\nu\r\nxr\r\nse\r\nyx\r\nmt\r\nlo\r\nm\r\nu\r\nukg\r\ngse\r\na\r\nuk\r\nn\r\nr\r\nlov\r\nep\r\nh\r\nadl\r\nyx\r\nt\r\nukg\r\nz\r\nepj\r\nz\r\nm\r\nx\r\nov\r\nyx\r\nxr\r\nep\r\nw\r\ny\r\nmtn\r\nsep\r\nep\r\nmt\r\nrwmt\r\nuk\r\nlo\r\nz\r\nnq\r\nj\r\ntn\r\nj\r\nkgs\r\ny\r\nb\r\nmtn\r\nsep\r\nr\r\ns\r\no\r\nr\r\nepjb\r\nadl\r\nrwmt\r\nyxrw\r\npj\r\nvhy\r\nk\r\ns\r\nx\r\nt\r\n", "output": "adlovhyxrwmtnqukgsepjbz\r\n"}, {"input": "95\r\nx\r\np\r\nk\r\nu\r\ny\r\nz\r\nt\r\na\r\ni\r\nj\r\nc\r\nh\r\nk\r\nn\r\nk\r\ns\r\nr\r\ny\r\nn\r\nv\r\nf\r\nb\r\nr\r\no\r\no\r\nu\r\nb\r\nj\r\no\r\nd\r\np\r\ns\r\nb\r\nt\r\nd\r\nq\r\nq\r\na\r\nm\r\ny\r\nq\r\nj\r\nz\r\nk\r\ne\r\nt\r\nv\r\nj\r\np\r\np\r\ns\r\nz\r\no\r\nk\r\nt\r\na\r\na\r\nc\r\np\r\nb\r\np\r\nx\r\nc\r\ny\r\nv\r\nj\r\na\r\np\r\nc\r\nd\r\nj\r\nt\r\nj\r\nt\r\nf\r\no\r\no\r\nn\r\nx\r\nq\r\nc\r\nk\r\np\r\nk\r\nq\r\na\r\ns\r\nl\r\na\r\nq\r\na\r\nb\r\ne\r\nj\r\nl\r\n", "output": "xpkuyztaijchnsrvfbodqmel\r\n"}, {"input": "96\r\not\r\njo\r\nvpr\r\nwi\r\ngx\r\nay\r\nzqf\r\nzq\r\npr\r\nigx\r\ntsb\r\nv\r\nr\r\ngxc\r\nigx\r\ngx\r\nvpr\r\nxc\r\nylk\r\nigx\r\nlkh\r\nvp\r\nuvp\r\nz\r\nbuv\r\njo\r\nvpr\r\npr\r\nprn\r\nwi\r\nqfw\r\nbuv\r\nd\r\npr\r\ndmj\r\nvpr\r\ng\r\nylk\r\nsbu\r\nhz\r\nk\r\nzqf\r\nylk\r\nxc\r\nwi\r\nvpr\r\nbuv\r\nzq\r\nmjo\r\nkh\r\nuv\r\nuvp\r\nts\r\nt\r\nylk\r\nnay\r\nbuv\r\nhzq\r\nts\r\njo\r\nsbu\r\nqfw\r\ngxc\r\ntsb\r\np\r\nhzq\r\nbuv\r\nsbu\r\nfwi\r\nkh\r\nmjo\r\nwig\r\nhzq\r\ndmj\r\ntsb\r\ntsb\r\nts\r\nylk\r\nyl\r\ngxc\r\not\r\nots\r\nuvp\r\nay\r\nay\r\nuvp\r\not\r\ny\r\np\r\nm\r\ngx\r\nkhz\r\ngxc\r\nkhz\r\ntsb\r\nrn\r\n", "output": "dmjotsbuvprnaylkhzqfwigxc\r\n"}, {"input": "3\r\nm\r\nu\r\nm\r\n", "output": "mu\r\n"}, {"input": "4\r\np\r\na\r\nz\r\nq\r\n", "output": "pazq\r\n"}, {"input": "5\r\ngtb\r\nnlu\r\nzjp\r\nk\r\nazj\r\n", "output": "azjpgtbnluk\r\n"}, {"input": "70\r\nxv\r\nlu\r\ntb\r\njx\r\nseh\r\nc\r\nm\r\ntbr\r\ntb\r\ndl\r\ne\r\nd\r\nt\r\np\r\nn\r\nse\r\nna\r\neh\r\nw\r\np\r\nzkj\r\nr\r\nk\r\nrw\r\nqf\r\ndl\r\ndl\r\ns\r\nat\r\nkjx\r\na\r\nz\r\nmig\r\nu\r\nse\r\npse\r\nd\r\ng\r\nc\r\nxv\r\nv\r\ngo\r\nps\r\ncd\r\nyqf\r\nyqf\r\nwzk\r\nxv\r\nat\r\nw\r\no\r\nl\r\nxvm\r\nfpse\r\nz\r\nk\r\nna\r\nv\r\nseh\r\nk\r\nl\r\nz\r\nd\r\nz\r\nn\r\nm\r\np\r\ng\r\nse\r\nat\r\n", "output": "cdlunatbrwzkjxvmigoyqfpseh\r\n"}, {"input": "3\r\nbmg\r\nwjah\r\nil\r\n", "output": "bmgilwjah\r\n"}, {"input": "100\r\ne\r\nbr\r\nls\r\nfb\r\nyx\r\nva\r\njm\r\nwn\r\nak\r\nhv\r\noq\r\nyx\r\nl\r\nm\r\nak\r\nce\r\nug\r\nqz\r\nug\r\ndf\r\nty\r\nhv\r\nmo\r\nxp\r\nyx\r\nkt\r\nak\r\nmo\r\niu\r\nxp\r\nce\r\nnd\r\noq\r\nbr\r\nty\r\nva\r\nce\r\nwn\r\nx\r\nsj\r\nel\r\npi\r\noq\r\ndf\r\niu\r\nc\r\nhv\r\npi\r\nsj\r\nhv\r\nmo\r\nbr\r\nxp\r\nce\r\nfb\r\nwn\r\nnd\r\nfb\r\npi\r\noq\r\nhv\r\nty\r\ngw\r\noq\r\nel\r\nw\r\nhv\r\nce\r\noq\r\nsj\r\nsj\r\nl\r\nwn\r\nqz\r\nty\r\nbr\r\nz\r\nel\r\nug\r\nce\r\nnd\r\nj\r\ndf\r\npi\r\niu\r\nnd\r\nls\r\niu\r\nrc\r\nbr\r\nug\r\nrc\r\nnd\r\nak\r\njm\r\njm\r\no\r\nls\r\nq\r\nfb\r\n", "output": "hvaktyxpiugwndfbrcelsjmoqz\r\n"}, {"input": "23\r\nq\r\ni\r\nj\r\nx\r\nz\r\nm\r\nt\r\ns\r\nu\r\ng\r\nc\r\nk\r\nh\r\nb\r\nx\r\nh\r\nt\r\no\r\ny\r\nh\r\nb\r\nn\r\na\r\n", "output": "qijxzmtsugckhboyna\r\n"}, {"input": "12\r\nkx\r\ng\r\nfo\r\nnt\r\nmf\r\nzv\r\nir\r\nds\r\nbz\r\nf\r\nlw\r\nx\r\n", "output": "bzvdsirkxlwmfontg\r\n"}, {"input": "2\r\na\r\nt\r\n", "output": "at\r\n"}, {"input": "1\r\ndm\r\n", "output": "dm\r\n"}, {"input": "100\r\nj\r\numj\r\ninc\r\nu\r\nsd\r\ntin\r\nw\r\nlf\r\nhs\r\nepk\r\nyg\r\nqhs\r\nh\r\nti\r\nf\r\nsd\r\ngepk\r\nu\r\nfw\r\nu\r\nsd\r\nvumj\r\num\r\ndt\r\nb\r\ng\r\nozl\r\nabvu\r\noz\r\nn\r\nw\r\nab\r\nge\r\nqh\r\nfwy\r\nsdti\r\ng\r\nyge\r\nepk\r\nabvu\r\nz\r\nlfw\r\nbv\r\nab\r\nyge\r\nqhs\r\nge\r\nhsdt\r\num\r\nl\r\np\r\na\r\nab\r\nd\r\nfw\r\ngep\r\nfwy\r\nbvu\r\nvumj\r\nzlfw\r\nk\r\nepk\r\ntin\r\npkab\r\nzl\r\nvum\r\nr\r\nf\r\nd\r\nsdt\r\nhs\r\nxoz\r\nlfwy\r\nfw\r\num\r\nep\r\nincx\r\na\r\nt\r\num\r\nh\r\nsdt\r\ngep\r\nlfw\r\nkab\r\ng\r\nmjr\r\nj\r\noz\r\ns\r\nwy\r\nnc\r\nlfw\r\nyg\r\nygep\r\nti\r\nyg\r\npk\r\nkab\r\nwyg\r\n", "output": "qhsdtincxozlfwygepkabvumjr\r\n"}, {"input": "94\r\nkmwbq\r\nmw\r\nwbq\r\ns\r\nlx\r\nf\r\npf\r\nl\r\nkmwb\r\na\r\nfoynt\r\nnt\r\nx\r\npf\r\npf\r\nep\r\nqs\r\nwbqse\r\nrl\r\nfoynt\r\nntzjd\r\nlxc\r\npfoy\r\nlx\r\nr\r\nagikm\r\nr\r\ntzjd\r\nep\r\nyntz\r\nu\r\nmw\r\nyntz\r\nfoynt\r\ntzjd\r\njdrlx\r\nwbqse\r\nr\r\nkmw\r\nwbq\r\nlx\r\nfoyn\r\nkm\r\nsepfo\r\nikmw\r\nf\r\nrlxch\r\nzjdrl\r\nyn\r\nhv\r\nynt\r\nbqs\r\nvu\r\nik\r\nqse\r\nxchvu\r\nmwbqs\r\ny\r\nlx\r\nx\r\nntzjd\r\nbq\r\nxchv\r\nwbqse\r\nkm\r\nse\r\nmwb\r\nxchvu\r\nwbq\r\nc\r\ngikm\r\nbq\r\nwb\r\nmwbq\r\nikmw\r\nag\r\ny\r\nchvu\r\nbqsep\r\nbqs\r\nrlx\r\ntzjd\r\nmwb\r\na\r\ndrlxc\r\ntzjd\r\nt\r\nsepf\r\nwbqse\r\nd\r\nbqs\r\nyn\r\nh\r\nepfo\r\n", "output": "agikmwbqsepfoyntzjdrlxchvu\r\n"}, {"input": "13\r\ndaq\r\nvcnexi\r\nlkp\r\nztvcne\r\naqozt\r\nztvcne\r\nprdaqo\r\ncnex\r\nnexijm\r\nztvcne\r\nfysh\r\nxijmb\r\naq\r\n", "output": "fyshlkprdaqoztvcnexijmb\r\n"}, {"input": "2\r\nnxqdblgac\r\nzpjou\r\n", "output": "nxqdblgaczpjou\r\n"}, {"input": "7\r\nfjr\r\ngk\r\nigkf\r\nret\r\nvx\r\nvxa\r\ncv\r\n", "output": "cvxaigkfjret\r\n"}, {"input": "7\r\nwer\r\nqwe\r\nw\r\nq\r\nert\r\ntyu\r\nrty\r\n", "output": "qwertyu\r\n"}, {"input": "4\r\na\r\nb\r\nab\r\nabc\r\n", "output": "abc\r\n"}, {"input": "4\r\nt\r\nwef\r\nqwe\r\nh\r\n", "output": "qwefth\r\n"}, {"input": "5\r\nabcd\r\nbc\r\ndef\r\nde\r\ncd\r\n", "output": "abcdef\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path) as f: n = int(f.readline().strip()) fragments = [line.strip() for line in f] with open(output_path) as f: correct_output = f.read().strip() correct_length = len(correct_output) with open(submission_path) as f: submission_output = f.read().strip() # Check length matches correct (minimal) if len(submission_output) != correct_length: print(0) return # Check all characters are unique if len(set(submission_output)) != len(submission_output): print(0) return # Check all fragments are present as substrings sub = submission_output for frag in fragments: if frag not in sub: print(0) return # All conditions met print(1) if __name__ == "__main__": main()
true
638/B
638
B
Python 3
PRETESTS
9
46
4,915,200
16839935
def lfs(new): for i in range(len(new)): flag = True for j in range(len(new)): if j != i: if new[i][0] in new[j]: flag = False if flag: return i def vlastsovetam(data): new = [] for i in range(len(data)): flag = True for j in range(len(data)): if j != i: if data[i] == data[j] and data[i] not in new: flag = True elif data[i] in data[j]: flag = False break if flag: new.append(data[i]) return new def feral(): global start for i in range(len(new)): for j in range(1, len(start)): if start[j] == new[i][0]: start += new[i][len(start) - j:] del new[i] return False return True n = int(input()) data = [] for i in range(n): data.append(input()) new = vlastsovetam(data) sfl = lfs(new) start = new[sfl] del new[sfl] while len(new): if feral(): break if len(new) > 0: start += "".join(new) print(start)
67
62
4,915,200
16839269
#!/usr/bin/env python3 n = eval(input()) substrings = [input() for line in range(n)] letters = {} for word in substrings: for letter in word: if not letter in letters: letters[letter] = [word[:(word.index(letter))],word[(word.index(letter)+1):]] else: if len(letters[letter][0]) < len(word[:(word.index(letter))]): letters[letter][0] = word[:(word.index(letter))] if len(letters[letter][1]) < len(word[(word.index(letter)+1):]): letters[letter][1] = word[(word.index(letter)+1):] result = "" for letter in letters: if letters[letter][0] == "": result = letter + letters[letter][1] for i in result[:-1]: if i in letters: del letters[i] break while letters: if letters[result[-1]][1] == "": del letters[result[-1]] for letter in letters: if letters[letter][0] == "": result += letter+letters[letter][1] break else: result += letters[result[-1]][1] for i in result[:-1]: if i in letters: del letters[i] print(result)
VK Cup 2016 - Qualification Round 2
CF
2,016
1
256
Making Genome in Berland
Berland scientists face a very important task - given the parts of short DNA fragments, restore the dinosaur DNA! The genome of a berland dinosaur has noting in common with the genome that we've used to: it can have 26 distinct nucleotide types, a nucleotide of each type can occur at most once. If we assign distinct English letters to all nucleotides, then the genome of a Berland dinosaur will represent a non-empty string consisting of small English letters, such that each letter occurs in it at most once. Scientists have n genome fragments that are represented as substrings (non-empty sequences of consecutive nucleotides) of the sought genome. You face the following problem: help scientists restore the dinosaur genome. It is guaranteed that the input is not contradictory and at least one suitable line always exists. When the scientists found out that you are a strong programmer, they asked you in addition to choose the one with the minimum length. If there are multiple such strings, choose any string.
The first line of the input contains a positive integer n (1 ≤ n ≤ 100) — the number of genome fragments. Each of the next lines contains one descriptions of a fragment. Each fragment is a non-empty string consisting of distinct small letters of the English alphabet. It is not guaranteed that the given fragments are distinct. Fragments could arbitrarily overlap and one fragment could be a substring of another one. It is guaranteed that there is such string of distinct letters that contains all the given fragments as substrings.
In the single line of the output print the genome of the minimum length that contains all the given parts. All the nucleotides in the genome must be distinct. If there are multiple suitable strings, print the string of the minimum length. If there also are multiple suitable strings, you can print any of them.
null
null
[{"input": "3\nbcd\nab\ncdef", "output": "abcdef"}, {"input": "4\nx\ny\nz\nw", "output": "xyzw"}]
1,500
["*special", "dfs and similar", "strings"]
67
[{"input": "3\r\nbcd\r\nab\r\ncdef\r\n", "output": "abcdef\r\n"}, {"input": "4\r\nx\r\ny\r\nz\r\nw\r\n", "output": "xyzw\r\n"}, {"input": "25\r\nef\r\nfg\r\ngh\r\nhi\r\nij\r\njk\r\nkl\r\nlm\r\nmn\r\nno\r\nab\r\nbc\r\ncd\r\nde\r\nop\r\npq\r\nqr\r\nrs\r\nst\r\ntu\r\nuv\r\nvw\r\nwx\r\nxy\r\nyz\r\n", "output": "abcdefghijklmnopqrstuvwxyz\r\n"}, {"input": "1\r\nf\r\n", "output": "f\r\n"}, {"input": "1\r\nqwertyuiopzxcvbnmasdfghjkl\r\n", "output": "qwertyuiopzxcvbnmasdfghjkl\r\n"}, {"input": "3\r\ndfghj\r\nghjkl\r\nasdfg\r\n", "output": "asdfghjkl\r\n"}, {"input": "4\r\nab\r\nab\r\nab\r\nabc\r\n", "output": "abc\r\n"}, {"input": "3\r\nf\r\nn\r\nux\r\n", "output": "uxfn\r\n"}, {"input": "2\r\nfgs\r\nfgs\r\n", "output": "fgs\r\n"}, {"input": "96\r\nc\r\ndhf\r\no\r\nq\r\nry\r\nh\r\nr\r\nf\r\nji\r\nek\r\ndhf\r\np\r\nk\r\no\r\nf\r\nw\r\nc\r\nc\r\nfgw\r\nbps\r\nhfg\r\np\r\ni\r\nji\r\nto\r\nc\r\nou\r\ny\r\nfg\r\na\r\ne\r\nu\r\nc\r\ny\r\nhf\r\nqn\r\nu\r\nj\r\np\r\ns\r\no\r\nmr\r\na\r\nqn\r\nb\r\nlb\r\nn\r\nji\r\nji\r\na\r\no\r\nat\r\ns\r\nf\r\nb\r\ndh\r\nk\r\nl\r\nl\r\nvq\r\nt\r\nb\r\nc\r\nv\r\nc\r\nh\r\nh\r\ny\r\nh\r\nq\r\ne\r\nx\r\nd\r\no\r\nq\r\nm\r\num\r\nmr\r\nfg\r\ni\r\nl\r\na\r\nh\r\nt\r\num\r\nr\r\no\r\nn\r\nk\r\ne\r\nji\r\na\r\nc\r\nh\r\ne\r\nm\r\n", "output": "atoumrydhfgwekjilbpsvqncx\r\n"}, {"input": "3\r\npbi\r\nopbi\r\ngh\r\n", "output": "ghopbi\r\n"}, {"input": "4\r\ng\r\np\r\no\r\nop\r\n", "output": "opg\r\n"}, {"input": "5\r\np\r\nf\r\nu\r\nf\r\np\r\n", "output": "pfu\r\n"}, {"input": "4\r\nr\r\nko\r\nuz\r\nko\r\n", "output": "kouzr\r\n"}, {"input": "5\r\nzt\r\nted\r\nlzt\r\nted\r\ndyv\r\n", "output": "lztedyv\r\n"}, {"input": "6\r\ngul\r\ng\r\njrb\r\nul\r\nd\r\njr\r\n", "output": "guljrbd\r\n"}, {"input": "5\r\nlkyh\r\naim\r\nkyh\r\nm\r\nkyhai\r\n", "output": "lkyhaim\r\n"}, {"input": "4\r\nzrncsywd\r\nsywdx\r\ngqzrn\r\nqzrncsy\r\n", "output": "gqzrncsywdx\r\n"}, {"input": "5\r\ntbxzc\r\njrdtb\r\njrdtb\r\nflnj\r\nrdtbx\r\n", "output": "flnjrdtbxzc\r\n"}, {"input": "10\r\ng\r\nkagijn\r\nzxt\r\nhmkag\r\nhm\r\njnc\r\nxtqupw\r\npwhmk\r\ng\r\nagi\r\n", "output": "zxtqupwhmkagijnc\r\n"}, {"input": "20\r\nf\r\nf\r\nv\r\nbn\r\ne\r\nmr\r\ne\r\ne\r\nn\r\nj\r\nqfv\r\ne\r\ndpb\r\nj\r\nlc\r\nr\r\ndp\r\nf\r\na\r\nrt\r\n", "output": "dpbnlcmrtqfveja\r\n"}, {"input": "30\r\nxlo\r\nwx\r\ne\r\nf\r\nyt\r\nw\r\ne\r\nl\r\nxl\r\nojg\r\njg\r\niy\r\ngkz\r\ne\r\nw\r\nloj\r\ng\r\nfw\r\nl\r\nlo\r\nbe\r\ne\r\ngk\r\niyt\r\no\r\nb\r\nqv\r\nz\r\nb\r\nzq\r\n", "output": "befwxlojgkzqviyt\r\n"}, {"input": "50\r\nmd\r\nei\r\nhy\r\naz\r\nzr\r\nmd\r\nv\r\nz\r\nke\r\ny\r\nuk\r\nf\r\nhy\r\njm\r\nke\r\njm\r\ncn\r\nwf\r\nzr\r\nqj\r\ng\r\nzr\r\ndv\r\ni\r\ndv\r\nuk\r\nj\r\nwf\r\njm\r\nn\r\na\r\nqj\r\nei\r\nf\r\nzr\r\naz\r\naz\r\nke\r\na\r\nr\r\ndv\r\nei\r\nzr\r\ndv\r\nq\r\ncn\r\nyg\r\nqj\r\nnh\r\nhy\r\n", "output": "azrcnhygqjmdvukeiwf\r\n"}, {"input": "80\r\ni\r\nioh\r\nquc\r\nexioh\r\niohb\r\nex\r\nrwky\r\nz\r\nquc\r\nrw\r\nplnt\r\nq\r\nhbrwk\r\nexioh\r\ntv\r\nxioh\r\nlnt\r\nxi\r\nn\r\npln\r\niohbr\r\nwky\r\nhbr\r\nw\r\nyq\r\nrwky\r\nbrw\r\nplnt\r\nv\r\nkyq\r\nrwkyq\r\nt\r\nhb\r\ngplnt\r\np\r\nkyqu\r\nhbr\r\nrwkyq\r\nhbr\r\nve\r\nhbrwk\r\nkyq\r\nkyquc\r\ngpln\r\ni\r\nbr\r\ntvex\r\nwkyqu\r\nz\r\nlnt\r\ngp\r\nky\r\ngplnt\r\ne\r\nhbrwk\r\nbrw\r\nve\r\no\r\nplnt\r\nn\r\nntve\r\ny\r\nln\r\npln\r\ntvexi\r\nr\r\nzgp\r\nxiohb\r\nl\r\nn\r\nt\r\nplnt\r\nlntv\r\nexi\r\nexi\r\ngpl\r\nioh\r\nk\r\nwk\r\ni\r\n", "output": "zgplntvexiohbrwkyquc\r\n"}, {"input": "70\r\njp\r\nz\r\nz\r\nd\r\ndy\r\nk\r\nsn\r\nrg\r\nz\r\nsn\r\nh\r\nj\r\ns\r\nkx\r\npu\r\nkx\r\nm\r\njp\r\nbo\r\nm\r\ntk\r\ndy\r\no\r\nm\r\nsn\r\nv\r\nrg\r\nv\r\nn\r\no\r\ngh\r\np\r\no\r\nx\r\nq\r\nzv\r\nr\r\nbo\r\ng\r\noz\r\nu\r\nub\r\nnd\r\nh\r\ny\r\njp\r\no\r\nq\r\nbo\r\nhq\r\nhq\r\nkx\r\nx\r\ndy\r\nn\r\nb\r\nub\r\nsn\r\np\r\nub\r\ntk\r\nu\r\nnd\r\nvw\r\nt\r\nub\r\nbo\r\nyr\r\nyr\r\nub\r\n", "output": "jpubozvwsndyrghqtkxm\r\n"}, {"input": "100\r\nm\r\nj\r\nj\r\nf\r\nk\r\nq\r\ni\r\nu\r\ni\r\nl\r\nt\r\nt\r\no\r\nv\r\nk\r\nw\r\nr\r\nj\r\nh\r\nx\r\nc\r\nv\r\nu\r\nf\r\nh\r\nj\r\nb\r\ne\r\ni\r\nr\r\ng\r\nb\r\nl\r\nb\r\ng\r\nb\r\nf\r\nq\r\nv\r\na\r\nu\r\nn\r\ni\r\nl\r\nk\r\nc\r\nx\r\nu\r\nr\r\ne\r\ni\r\na\r\nc\r\no\r\nc\r\na\r\nx\r\nd\r\nf\r\nx\r\no\r\nx\r\nm\r\nl\r\nr\r\nc\r\nr\r\nc\r\nv\r\nj\r\ng\r\nu\r\nn\r\nn\r\nd\r\nl\r\nl\r\nc\r\ng\r\nu\r\nr\r\nu\r\nh\r\nl\r\na\r\nl\r\nr\r\nt\r\nm\r\nf\r\nm\r\nc\r\nh\r\nl\r\nd\r\na\r\nr\r\nh\r\nn\r\nc\r\n", "output": "mjfkqiultovwrhxcbegand\r\n"}, {"input": "99\r\nia\r\nz\r\nsb\r\ne\r\nnm\r\nd\r\nknm\r\nt\r\nm\r\np\r\nqvu\r\ne\r\nq\r\nq\r\ns\r\nmd\r\nz\r\nfh\r\ne\r\nwi\r\nn\r\nsb\r\nq\r\nw\r\ni\r\ng\r\nr\r\ndf\r\nwi\r\nl\r\np\r\nm\r\nb\r\ni\r\natj\r\nb\r\nwia\r\nx\r\nnm\r\nlk\r\nx\r\nfh\r\nh\r\np\r\nf\r\nzr\r\nz\r\nr\r\nsbz\r\nlkn\r\nsbz\r\nz\r\na\r\nwia\r\ntjx\r\nk\r\nj\r\nx\r\nl\r\nqvu\r\nzr\r\nfh\r\nbzrg\r\nz\r\nplk\r\nfhe\r\nn\r\njxqv\r\nrgp\r\ne\r\ndf\r\nz\r\ns\r\natj\r\ndf\r\nat\r\ngp\r\nw\r\new\r\nt\r\np\r\np\r\nfhe\r\nq\r\nxq\r\nt\r\nzr\r\nat\r\ndfh\r\nj\r\ns\r\nu\r\npl\r\np\r\nrg\r\nlk\r\nq\r\nwia\r\ng\r\n", "output": "sbzrgplknmdfhewiatjxqvu\r\n"}, {"input": "95\r\np\r\nk\r\nd\r\nr\r\nn\r\nz\r\nn\r\nb\r\np\r\nw\r\ni\r\nn\r\ny\r\ni\r\nn\r\nn\r\ne\r\nr\r\nu\r\nr\r\nb\r\ni\r\ne\r\np\r\nk\r\nc\r\nc\r\nh\r\np\r\nk\r\nh\r\ns\r\ne\r\ny\r\nq\r\nq\r\nx\r\nw\r\nh\r\ng\r\nt\r\nt\r\na\r\nt\r\nh\r\ni\r\nb\r\ne\r\np\r\nr\r\nu\r\nn\r\nn\r\nr\r\nq\r\nn\r\nu\r\ng\r\nw\r\nt\r\np\r\nt\r\nk\r\nd\r\nz\r\nh\r\nf\r\nd\r\ni\r\na\r\na\r\nf\r\ne\r\na\r\np\r\ns\r\nk\r\nt\r\ng\r\nf\r\ni\r\ng\r\ng\r\nt\r\nn\r\nn\r\nt\r\nt\r\nr\r\nx\r\na\r\nz\r\nc\r\nn\r\nk\r\n", "output": "pkdrnzbwiyeuchsqxgtaf\r\n"}, {"input": "3\r\nh\r\nx\r\np\r\n", "output": "hxp\r\n"}, {"input": "4\r\nrz\r\nvu\r\nxy\r\npg\r\n", "output": "pgrzvuxy\r\n"}, {"input": "5\r\ndrw\r\nu\r\nzq\r\npd\r\naip\r\n", "output": "aipdrwzqu\r\n"}, {"input": "70\r\ne\r\no\r\ng\r\ns\r\nsz\r\nyl\r\ns\r\nn\r\no\r\nq\r\np\r\nl\r\noa\r\ndq\r\ny\r\np\r\nn\r\nio\r\ng\r\nb\r\nk\r\nv\r\ny\r\nje\r\nc\r\ncb\r\nfx\r\ncbv\r\nfxp\r\nkt\r\nhm\r\nz\r\nrcb\r\np\r\nt\r\nu\r\nzh\r\ne\r\nb\r\na\r\nyl\r\nd\r\nv\r\nl\r\nrc\r\nq\r\nt\r\nt\r\nj\r\nl\r\nr\r\ny\r\nlg\r\np\r\nt\r\nd\r\nq\r\nje\r\nqwu\r\ng\r\nz\r\ngi\r\ndqw\r\nz\r\nvyl\r\nk\r\nt\r\nc\r\nb\r\nrc\r\n", "output": "dqwufxpjektrcbvylgioaszhmn\r\n"}, {"input": "3\r\ne\r\nw\r\nox\r\n", "output": "oxew\r\n"}, {"input": "100\r\npr\r\nfz\r\nru\r\ntk\r\nld\r\nvq\r\nef\r\ngj\r\ncp\r\nbm\r\nsn\r\nld\r\nua\r\nzl\r\ndw\r\nef\r\nua\r\nbm\r\nxb\r\nvq\r\nav\r\ncp\r\nko\r\nwc\r\nru\r\ni\r\ne\r\nav\r\nbm\r\nav\r\nxb\r\nog\r\ng\r\nme\r\ntk\r\nog\r\nxb\r\nef\r\ntk\r\nhx\r\nqt\r\nvq\r\ndw\r\nv\r\nxb\r\ndw\r\nko\r\nd\r\nbm\r\nua\r\nvq\r\nis\r\nwc\r\ntk\r\ntk\r\ngj\r\ng\r\ngj\r\nef\r\nqt\r\nvq\r\nbm\r\nog\r\nvq\r\ngj\r\nvq\r\nzl\r\ngj\r\nji\r\nvq\r\nhx\r\ng\r\nbm\r\nji\r\nqt\r\nef\r\nav\r\ntk\r\nxb\r\nru\r\nko\r\nny\r\nis\r\ncp\r\nxb\r\nog\r\nru\r\nhx\r\nwc\r\nko\r\nu\r\nfz\r\ndw\r\nji\r\nzl\r\nvq\r\nqt\r\nko\r\ngj\r\nis\r\n", "output": "hxbmefzldwcpruavqtkogjisny\r\n"}, {"input": "23\r\nw\r\nz\r\nk\r\nc\r\ne\r\np\r\nt\r\na\r\nx\r\nc\r\nq\r\nx\r\na\r\nf\r\np\r\nw\r\nh\r\nx\r\nf\r\nw\r\np\r\nw\r\nq\r\n", "output": "wzkceptaxqfh\r\n"}, {"input": "12\r\nu\r\na\r\nhw\r\na\r\ngh\r\nog\r\nr\r\nd\r\nw\r\nk\r\nl\r\ny\r\n", "output": "oghwuardkly\r\n"}, {"input": "2\r\ny\r\nd\r\n", "output": "yd\r\n"}, {"input": "1\r\nd\r\n", "output": "d\r\n"}, {"input": "100\r\nwm\r\nq\r\nhf\r\nwm\r\niz\r\ndl\r\nmiz\r\np\r\nzoa\r\nbk\r\nw\r\nxv\r\nfj\r\nd\r\nxvsg\r\nr\r\nx\r\nt\r\nyd\r\nbke\r\ny\r\neq\r\nx\r\nn\r\nry\r\nt\r\nc\r\nuh\r\nn\r\npw\r\nuhf\r\neq\r\nr\r\nw\r\nk\r\nt\r\nsg\r\njb\r\nd\r\nke\r\ne\r\nx\r\nh\r\ntuh\r\nan\r\nn\r\noa\r\nw\r\nq\r\nz\r\nk\r\noan\r\nbk\r\nj\r\nzoan\r\nyd\r\npwmi\r\nyd\r\nc\r\nry\r\nfj\r\nlx\r\nqr\r\nke\r\nizo\r\nm\r\nz\r\noan\r\nwmi\r\nl\r\nyd\r\nz\r\ns\r\nke\r\nw\r\nfjbk\r\nqry\r\nlxv\r\nhf\r\ns\r\nnc\r\nq\r\nlxv\r\nzoa\r\nn\r\nfj\r\np\r\nhf\r\nmiz\r\npwm\r\ntu\r\noan\r\ng\r\nd\r\nqr\r\na\r\nan\r\nxvs\r\ny\r\ntuhf\r\n", "output": "pwmizoanctuhfjbkeqrydlxvsg\r\n"}, {"input": "94\r\ncw\r\nm\r\nuhbk\r\ntfy\r\nsd\r\nu\r\ntf\r\ntfym\r\nfy\r\nbk\r\nx\r\nx\r\nxl\r\npu\r\noq\r\nkt\r\ny\r\nb\r\nj\r\nqxl\r\no\r\noqx\r\nr\r\nr\r\njr\r\nk\r\ne\r\nw\r\nsd\r\na\r\nljre\r\nhbk\r\nym\r\nxl\r\np\r\nreg\r\nktf\r\nre\r\nw\r\nhbk\r\nxlj\r\nzn\r\ne\r\nm\r\nms\r\nsdv\r\nr\r\nr\r\no\r\naoq\r\nzna\r\nymsd\r\nqx\r\nr\r\no\r\nlj\r\nm\r\nk\r\nu\r\nkt\r\nms\r\ne\r\nx\r\nh\r\ni\r\nz\r\nm\r\nc\r\nb\r\no\r\nm\r\nvcw\r\ndvc\r\nq\r\na\r\nb\r\nfyms\r\nv\r\nxl\r\nxl\r\ntfym\r\nx\r\nfy\r\np\r\nyms\r\nms\r\nb\r\nt\r\nu\r\nn\r\nq\r\nnaoqx\r\no\r\ne\r\n", "output": "puhbktfymsdvcwznaoqxljregi\r\n"}, {"input": "13\r\ngku\r\nzw\r\nstvqc\r\najy\r\njystvq\r\nfilden\r\nstvq\r\nfild\r\nqcporh\r\najys\r\nqcpor\r\nqcpor\r\ncporhm\r\n", "output": "ajystvqcporhmfildengkuzw\r\n"}, {"input": "2\r\not\r\nqu\r\n", "output": "otqu\r\n"}, {"input": "100\r\nv\r\nh\r\nj\r\nf\r\nr\r\ni\r\ns\r\nw\r\nv\r\nd\r\nv\r\np\r\nd\r\nu\r\ny\r\nd\r\nu\r\nx\r\nr\r\nu\r\ng\r\nm\r\ns\r\nf\r\nv\r\nx\r\na\r\ng\r\ng\r\ni\r\ny\r\ny\r\nv\r\nd\r\ni\r\nq\r\nq\r\nu\r\nx\r\nj\r\nv\r\nj\r\ne\r\no\r\nr\r\nh\r\nu\r\ne\r\nd\r\nv\r\nb\r\nv\r\nq\r\nk\r\ni\r\nr\r\ne\r\nm\r\na\r\nj\r\na\r\nu\r\nq\r\nx\r\nq\r\ny\r\ns\r\nw\r\nk\r\ni\r\ns\r\nr\r\np\r\ni\r\np\r\ns\r\nd\r\nj\r\nw\r\no\r\nm\r\ns\r\nr\r\nd\r\nf\r\ns\r\nw\r\nv\r\ne\r\ny\r\no\r\nx\r\na\r\np\r\nk\r\nr\r\ng\r\ng\r\nb\r\nq\r\n", "output": "vhjfriswdpuyxgmaqeobk\r\n"}, {"input": "99\r\ntnq\r\nep\r\nuk\r\nk\r\nx\r\nvhy\r\nepj\r\nx\r\nj\r\nhy\r\nukg\r\nsep\r\nquk\r\nr\r\nw\r\no\r\nxrwm\r\ndl\r\nh\r\no\r\nad\r\ng\r\ng\r\nhy\r\nxr\r\nad\r\nhyx\r\nkg\r\nvh\r\nb\r\nlovh\r\nuk\r\nl\r\ntn\r\nkg\r\ny\r\nu\r\nxr\r\nse\r\nyx\r\nmt\r\nlo\r\nm\r\nu\r\nukg\r\ngse\r\na\r\nuk\r\nn\r\nr\r\nlov\r\nep\r\nh\r\nadl\r\nyx\r\nt\r\nukg\r\nz\r\nepj\r\nz\r\nm\r\nx\r\nov\r\nyx\r\nxr\r\nep\r\nw\r\ny\r\nmtn\r\nsep\r\nep\r\nmt\r\nrwmt\r\nuk\r\nlo\r\nz\r\nnq\r\nj\r\ntn\r\nj\r\nkgs\r\ny\r\nb\r\nmtn\r\nsep\r\nr\r\ns\r\no\r\nr\r\nepjb\r\nadl\r\nrwmt\r\nyxrw\r\npj\r\nvhy\r\nk\r\ns\r\nx\r\nt\r\n", "output": "adlovhyxrwmtnqukgsepjbz\r\n"}, {"input": "95\r\nx\r\np\r\nk\r\nu\r\ny\r\nz\r\nt\r\na\r\ni\r\nj\r\nc\r\nh\r\nk\r\nn\r\nk\r\ns\r\nr\r\ny\r\nn\r\nv\r\nf\r\nb\r\nr\r\no\r\no\r\nu\r\nb\r\nj\r\no\r\nd\r\np\r\ns\r\nb\r\nt\r\nd\r\nq\r\nq\r\na\r\nm\r\ny\r\nq\r\nj\r\nz\r\nk\r\ne\r\nt\r\nv\r\nj\r\np\r\np\r\ns\r\nz\r\no\r\nk\r\nt\r\na\r\na\r\nc\r\np\r\nb\r\np\r\nx\r\nc\r\ny\r\nv\r\nj\r\na\r\np\r\nc\r\nd\r\nj\r\nt\r\nj\r\nt\r\nf\r\no\r\no\r\nn\r\nx\r\nq\r\nc\r\nk\r\np\r\nk\r\nq\r\na\r\ns\r\nl\r\na\r\nq\r\na\r\nb\r\ne\r\nj\r\nl\r\n", "output": "xpkuyztaijchnsrvfbodqmel\r\n"}, {"input": "96\r\not\r\njo\r\nvpr\r\nwi\r\ngx\r\nay\r\nzqf\r\nzq\r\npr\r\nigx\r\ntsb\r\nv\r\nr\r\ngxc\r\nigx\r\ngx\r\nvpr\r\nxc\r\nylk\r\nigx\r\nlkh\r\nvp\r\nuvp\r\nz\r\nbuv\r\njo\r\nvpr\r\npr\r\nprn\r\nwi\r\nqfw\r\nbuv\r\nd\r\npr\r\ndmj\r\nvpr\r\ng\r\nylk\r\nsbu\r\nhz\r\nk\r\nzqf\r\nylk\r\nxc\r\nwi\r\nvpr\r\nbuv\r\nzq\r\nmjo\r\nkh\r\nuv\r\nuvp\r\nts\r\nt\r\nylk\r\nnay\r\nbuv\r\nhzq\r\nts\r\njo\r\nsbu\r\nqfw\r\ngxc\r\ntsb\r\np\r\nhzq\r\nbuv\r\nsbu\r\nfwi\r\nkh\r\nmjo\r\nwig\r\nhzq\r\ndmj\r\ntsb\r\ntsb\r\nts\r\nylk\r\nyl\r\ngxc\r\not\r\nots\r\nuvp\r\nay\r\nay\r\nuvp\r\not\r\ny\r\np\r\nm\r\ngx\r\nkhz\r\ngxc\r\nkhz\r\ntsb\r\nrn\r\n", "output": "dmjotsbuvprnaylkhzqfwigxc\r\n"}, {"input": "3\r\nm\r\nu\r\nm\r\n", "output": "mu\r\n"}, {"input": "4\r\np\r\na\r\nz\r\nq\r\n", "output": "pazq\r\n"}, {"input": "5\r\ngtb\r\nnlu\r\nzjp\r\nk\r\nazj\r\n", "output": "azjpgtbnluk\r\n"}, {"input": "70\r\nxv\r\nlu\r\ntb\r\njx\r\nseh\r\nc\r\nm\r\ntbr\r\ntb\r\ndl\r\ne\r\nd\r\nt\r\np\r\nn\r\nse\r\nna\r\neh\r\nw\r\np\r\nzkj\r\nr\r\nk\r\nrw\r\nqf\r\ndl\r\ndl\r\ns\r\nat\r\nkjx\r\na\r\nz\r\nmig\r\nu\r\nse\r\npse\r\nd\r\ng\r\nc\r\nxv\r\nv\r\ngo\r\nps\r\ncd\r\nyqf\r\nyqf\r\nwzk\r\nxv\r\nat\r\nw\r\no\r\nl\r\nxvm\r\nfpse\r\nz\r\nk\r\nna\r\nv\r\nseh\r\nk\r\nl\r\nz\r\nd\r\nz\r\nn\r\nm\r\np\r\ng\r\nse\r\nat\r\n", "output": "cdlunatbrwzkjxvmigoyqfpseh\r\n"}, {"input": "3\r\nbmg\r\nwjah\r\nil\r\n", "output": "bmgilwjah\r\n"}, {"input": "100\r\ne\r\nbr\r\nls\r\nfb\r\nyx\r\nva\r\njm\r\nwn\r\nak\r\nhv\r\noq\r\nyx\r\nl\r\nm\r\nak\r\nce\r\nug\r\nqz\r\nug\r\ndf\r\nty\r\nhv\r\nmo\r\nxp\r\nyx\r\nkt\r\nak\r\nmo\r\niu\r\nxp\r\nce\r\nnd\r\noq\r\nbr\r\nty\r\nva\r\nce\r\nwn\r\nx\r\nsj\r\nel\r\npi\r\noq\r\ndf\r\niu\r\nc\r\nhv\r\npi\r\nsj\r\nhv\r\nmo\r\nbr\r\nxp\r\nce\r\nfb\r\nwn\r\nnd\r\nfb\r\npi\r\noq\r\nhv\r\nty\r\ngw\r\noq\r\nel\r\nw\r\nhv\r\nce\r\noq\r\nsj\r\nsj\r\nl\r\nwn\r\nqz\r\nty\r\nbr\r\nz\r\nel\r\nug\r\nce\r\nnd\r\nj\r\ndf\r\npi\r\niu\r\nnd\r\nls\r\niu\r\nrc\r\nbr\r\nug\r\nrc\r\nnd\r\nak\r\njm\r\njm\r\no\r\nls\r\nq\r\nfb\r\n", "output": "hvaktyxpiugwndfbrcelsjmoqz\r\n"}, {"input": "23\r\nq\r\ni\r\nj\r\nx\r\nz\r\nm\r\nt\r\ns\r\nu\r\ng\r\nc\r\nk\r\nh\r\nb\r\nx\r\nh\r\nt\r\no\r\ny\r\nh\r\nb\r\nn\r\na\r\n", "output": "qijxzmtsugckhboyna\r\n"}, {"input": "12\r\nkx\r\ng\r\nfo\r\nnt\r\nmf\r\nzv\r\nir\r\nds\r\nbz\r\nf\r\nlw\r\nx\r\n", "output": "bzvdsirkxlwmfontg\r\n"}, {"input": "2\r\na\r\nt\r\n", "output": "at\r\n"}, {"input": "1\r\ndm\r\n", "output": "dm\r\n"}, {"input": "100\r\nj\r\numj\r\ninc\r\nu\r\nsd\r\ntin\r\nw\r\nlf\r\nhs\r\nepk\r\nyg\r\nqhs\r\nh\r\nti\r\nf\r\nsd\r\ngepk\r\nu\r\nfw\r\nu\r\nsd\r\nvumj\r\num\r\ndt\r\nb\r\ng\r\nozl\r\nabvu\r\noz\r\nn\r\nw\r\nab\r\nge\r\nqh\r\nfwy\r\nsdti\r\ng\r\nyge\r\nepk\r\nabvu\r\nz\r\nlfw\r\nbv\r\nab\r\nyge\r\nqhs\r\nge\r\nhsdt\r\num\r\nl\r\np\r\na\r\nab\r\nd\r\nfw\r\ngep\r\nfwy\r\nbvu\r\nvumj\r\nzlfw\r\nk\r\nepk\r\ntin\r\npkab\r\nzl\r\nvum\r\nr\r\nf\r\nd\r\nsdt\r\nhs\r\nxoz\r\nlfwy\r\nfw\r\num\r\nep\r\nincx\r\na\r\nt\r\num\r\nh\r\nsdt\r\ngep\r\nlfw\r\nkab\r\ng\r\nmjr\r\nj\r\noz\r\ns\r\nwy\r\nnc\r\nlfw\r\nyg\r\nygep\r\nti\r\nyg\r\npk\r\nkab\r\nwyg\r\n", "output": "qhsdtincxozlfwygepkabvumjr\r\n"}, {"input": "94\r\nkmwbq\r\nmw\r\nwbq\r\ns\r\nlx\r\nf\r\npf\r\nl\r\nkmwb\r\na\r\nfoynt\r\nnt\r\nx\r\npf\r\npf\r\nep\r\nqs\r\nwbqse\r\nrl\r\nfoynt\r\nntzjd\r\nlxc\r\npfoy\r\nlx\r\nr\r\nagikm\r\nr\r\ntzjd\r\nep\r\nyntz\r\nu\r\nmw\r\nyntz\r\nfoynt\r\ntzjd\r\njdrlx\r\nwbqse\r\nr\r\nkmw\r\nwbq\r\nlx\r\nfoyn\r\nkm\r\nsepfo\r\nikmw\r\nf\r\nrlxch\r\nzjdrl\r\nyn\r\nhv\r\nynt\r\nbqs\r\nvu\r\nik\r\nqse\r\nxchvu\r\nmwbqs\r\ny\r\nlx\r\nx\r\nntzjd\r\nbq\r\nxchv\r\nwbqse\r\nkm\r\nse\r\nmwb\r\nxchvu\r\nwbq\r\nc\r\ngikm\r\nbq\r\nwb\r\nmwbq\r\nikmw\r\nag\r\ny\r\nchvu\r\nbqsep\r\nbqs\r\nrlx\r\ntzjd\r\nmwb\r\na\r\ndrlxc\r\ntzjd\r\nt\r\nsepf\r\nwbqse\r\nd\r\nbqs\r\nyn\r\nh\r\nepfo\r\n", "output": "agikmwbqsepfoyntzjdrlxchvu\r\n"}, {"input": "13\r\ndaq\r\nvcnexi\r\nlkp\r\nztvcne\r\naqozt\r\nztvcne\r\nprdaqo\r\ncnex\r\nnexijm\r\nztvcne\r\nfysh\r\nxijmb\r\naq\r\n", "output": "fyshlkprdaqoztvcnexijmb\r\n"}, {"input": "2\r\nnxqdblgac\r\nzpjou\r\n", "output": "nxqdblgaczpjou\r\n"}, {"input": "7\r\nfjr\r\ngk\r\nigkf\r\nret\r\nvx\r\nvxa\r\ncv\r\n", "output": "cvxaigkfjret\r\n"}, {"input": "7\r\nwer\r\nqwe\r\nw\r\nq\r\nert\r\ntyu\r\nrty\r\n", "output": "qwertyu\r\n"}, {"input": "4\r\na\r\nb\r\nab\r\nabc\r\n", "output": "abc\r\n"}, {"input": "4\r\nt\r\nwef\r\nqwe\r\nh\r\n", "output": "qwefth\r\n"}, {"input": "5\r\nabcd\r\nbc\r\ndef\r\nde\r\ncd\r\n", "output": "abcdef\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path) as f: n = int(f.readline().strip()) fragments = [line.strip() for line in f] with open(output_path) as f: correct_output = f.read().strip() correct_length = len(correct_output) with open(submission_path) as f: submission_output = f.read().strip() # Check length matches correct (minimal) if len(submission_output) != correct_length: print(0) return # Check all characters are unique if len(set(submission_output)) != len(submission_output): print(0) return # Check all fragments are present as substrings sub = submission_output for frag in fragments: if frag not in sub: print(0) return # All conditions met print(1) if __name__ == "__main__": main()
true
515/B
515
B
Python 3
TESTS
19
62
204,800
114852125
from collections import deque n, m=[int(k) for k in input().split()] w=[int(k) for k in input().split()][1:] q=[int(k) for k in input().split()][1:] x, y=len(w), len(q) qwe={} zxc={} for i in range(n*m+11): if i%n not in qwe: qwe[i%n]=[i%m] elif i%m not in qwe[i%n]: qwe[i%n].append(i%m) if i%m not in zxc: zxc[i%m]=[i%n] elif i%n not in zxc[i%m]: zxc[i%m].append(i%n) if x==0 and y==0: print("No") else: c=set(w) d=set(q) asd=deque(w) new=deque(q) while asd or new: while asd: z=asd.popleft() for j in qwe[z]: if j not in c: new.append(j) c|={j} while new: z=new.popleft() for j in zxc[z]: if j not in d: asd.append(j) d|={j} if len(c)+len(d)<n+m: print("No") else: print("Yes") #print(c) #print(d)
56
46
0
218402049
import sys sys.setrecursionlimit(100000000) input=lambda:sys.stdin.readline().strip() write=lambda x:sys.stdout.write(str(x)+'\n') # from random import randint # from copy import deepcopy # from collections import deque,Counter # from heapq import heapify,heappush,heappop # from bisect import bisect_left,bisect,insort from math import inf,sqrt,gcd,ceil,floor,log,log2,log10,pi # from functools import cmp_to_key n,m=map(int,input().split()) bs=[0]*n;gs=[0]*m t=list(map(int,input().split())) for i in t[1:]: bs[i]=1 t=list(map(int,input().split())) for i in t[1:]: gs[i]=1 x=gcd(n,m) st=[False]*x for i in range(x): for j in range(i,n,x): if bs[j]: st[i]=True break else: for j in range(i,m,x): if gs[j]: st[i]=True break for i in range(x): if not st[i]: print('No') break else: print('Yes')
Codeforces Round 292 (Div. 2)
CF
2,015
2
256
Drazil and His Happy Friends
Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan. There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites $$(i \bmod n)$$-th boy and $$(i \bmod m)$$-th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if he/she was happy originally), he stays happy forever. Drazil wants to know whether he can use this plan to make all his friends become happy at some moment.
The first line contains two integer n and m (1 ≤ n, m ≤ 100). The second line contains integer b (0 ≤ b ≤ n), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys. The third line conatins integer g (0 ≤ g ≤ m), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls. It is guaranteed that there is at least one person that is unhappy among his friends.
If Drazil can make all his friends become happy by this plan, print "Yes". Otherwise, print "No".
null
By $$i \bmod k$$ we define the remainder of integer division of i by k. In first sample case: - On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day. - On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day. - On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day. - On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy. - On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment.
[{"input": "2 3\n0\n1 0", "output": "Yes"}, {"input": "2 4\n1 0\n1 2", "output": "No"}, {"input": "2 3\n1 0\n1 1", "output": "Yes"}]
1,300
["brute force", "dsu", "meet-in-the-middle", "number theory"]
56
[{"input": "2 3\r\n0\r\n1 0\r\n", "output": "Yes\r\n"}, {"input": "2 4\r\n1 0\r\n1 2\r\n", "output": "No\r\n"}, {"input": "2 3\r\n1 0\r\n1 1\r\n", "output": "Yes\r\n"}, {"input": "16 88\r\n6 5 14 2 0 12 7\r\n30 21 64 35 79 74 39 63 44 81 73 0 27 33 69 12 86 46 20 25 55 52 7 58 23 5 60 32 41 50 82\r\n", "output": "Yes\r\n"}, {"input": "52 91\r\n13 26 1 3 43 17 19 32 46 33 48 23 37 50\r\n25 78 26 1 40 2 67 42 4 56 30 70 84 32 20 85 59 8 86 34 73 23 10 88 24 11\r\n", "output": "No\r\n"}, {"input": "26 52\r\n8 0 14 16 17 7 9 10 11\r\n15 39 15 2 41 42 30 17 18 31 6 21 35 48 50 51\r\n", "output": "No\r\n"}, {"input": "50 50\r\n0\r\n0\r\n", "output": "No\r\n"}, {"input": "27 31\r\n4 25 5 19 20\r\n26 5 28 17 2 1 0 26 23 12 29 6 4 25 19 15 13 20 24 8 27 22 30 3 10 9 7\r\n", "output": "Yes\r\n"}, {"input": "55 79\r\n5 51 27 36 45 53\r\n30 15 28 0 5 38 3 34 30 35 1 32 12 27 42 39 69 33 10 63 16 29 76 19 60 70 67 31 78 68 45\r\n", "output": "Yes\r\n"}, {"input": "79 23\r\n35 31 62 14 9 46 18 68 69 42 13 50 77 23 76 5 53 40 16 32 74 54 38 25 45 39 26 37 66 78 3 48 10 17 56 59\r\n13 16 0 8 6 18 14 21 11 20 4 15 13 22\r\n", "output": "Yes\r\n"}, {"input": "7 72\r\n1 4\r\n3 49 32 28\r\n", "output": "Yes\r\n"}, {"input": "100 50\r\n31 52 54 8 60 61 62 63 64 16 19 21 73 25 76 77 79 30 81 32 33 34 37 88 39 40 91 42 94 95 96 98\r\n18 0 1 3 5 6 7 9 15 18 20 22 24 28 35 36 43 47 49\r\n", "output": "No\r\n"}, {"input": "98 49\r\n33 0 51 52 6 57 10 12 63 15 16 19 20 21 72 73 74 76 77 78 30 31 81 33 83 37 38 39 40 92 44 45 95 97\r\n15 4 5 7 9 11 13 17 18 22 26 35 36 41 42 47\r\n", "output": "No\r\n"}, {"input": "50 50\r\n14 7 8 12 16 18 22 23 24 28 30 35 40 46 49\r\n35 0 1 2 3 4 5 6 9 10 11 13 14 15 17 19 20 21 25 26 27 29 31 32 33 34 36 37 38 39 41 43 44 45 47 48\r\n", "output": "No\r\n"}, {"input": "30 44\r\n3 8 26 28\r\n6 2 30 38 26 8 6\r\n", "output": "No\r\n"}, {"input": "69 72\r\n18 58 46 52 43 1 55 16 7 4 38 68 14 32 53 41 29 2 59\r\n21 22 43 55 13 70 4 7 31 10 23 56 44 62 17 50 53 5 41 11 65 32\r\n", "output": "No\r\n"}, {"input": "76 28\r\n10 24 13 61 45 29 57 41 21 37 11\r\n2 12 9\r\n", "output": "No\r\n"}, {"input": "65 75\r\n15 25 60 12 62 37 22 47 52 3 63 58 13 14 49 34\r\n18 70 10 2 52 22 47 72 57 38 48 13 73 3 19 4 74 49 34\r\n", "output": "No\r\n"}, {"input": "6 54\r\n1 5\r\n14 13 49 31 37 44 2 15 51 52 22 28 10 35 47\r\n", "output": "No\r\n"}, {"input": "96 36\r\n34 84 24 0 48 85 13 61 37 62 38 86 75 3 16 64 40 28 76 53 5 17 42 6 7 91 67 55 68 92 57 11 71 35 59\r\n9 1 14 15 17 18 30 6 8 35\r\n", "output": "No\r\n"}, {"input": "40 40\r\n23 0 2 3 4 5 7 11 15 16 17 18 19 22 25 28 29 30 31 32 34 35 36 37\r\n16 1 6 8 9 10 12 13 14 20 21 23 24 26 27 38 39\r\n", "output": "No\r\n"}, {"input": "66 66\r\n24 2 35 3 36 4 5 10 45 14 48 18 51 19 21 55 22 23 24 25 26 63 31 65 32\r\n21 0 1 37 6 40 7 8 42 45 13 15 16 50 53 23 24 60 28 62 63 31\r\n", "output": "No\r\n"}, {"input": "20 20\r\n9 0 3 4 6 7 8 10 12 13\r\n10 1 2 5 9 11 14 15 16 18 19\r\n", "output": "No\r\n"}, {"input": "75 30\r\n18 46 47 32 33 3 34 35 21 51 7 9 54 39 72 42 59 29 14\r\n8 0 17 5 6 23 26 27 13\r\n", "output": "No\r\n"}, {"input": "100 50\r\n30 50 54 7 8 59 60 61 62 63 64 15 16 18 19 20 22 73 27 79 83 86 87 89 42 93 94 45 46 97 98\r\n20 1 2 3 5 6 17 21 24 25 26 28 30 31 32 34 35 38 40 41 49\r\n", "output": "Yes\r\n"}, {"input": "98 98\r\n43 49 1 51 3 53 4 55 56 8 9 10 60 11 12 61 64 16 65 17 19 20 21 72 24 74 25 77 78 31 34 35 36 37 87 88 89 42 92 43 44 94 46 96\r\n34 50 2 52 5 54 9 62 63 15 18 68 70 22 72 75 26 27 77 30 81 82 83 35 36 37 87 88 89 90 41 93 95 96 48\r\n", "output": "No\r\n"}, {"input": "100 100\r\n45 50 1 4 5 55 7 8 10 60 61 62 63 14 65 66 17 18 20 21 22 24 25 27 78 28 29 30 31 82 83 33 84 36 37 38 39 40 41 42 44 45 46 48 98 49\r\n34 50 1 2 52 3 54 56 7 9 59 61 14 16 67 18 69 22 73 24 76 79 81 82 84 35 36 38 39 90 43 44 45 47 49\r\n", "output": "No\r\n"}, {"input": "76 72\r\n29 4 64 68 20 8 12 50 42 46 0 70 11 37 75 47 45 29 17 19 73 9 41 31 35 67 65 39 51 55\r\n25 60 32 48 42 8 6 9 7 31 19 25 5 33 51 61 67 55 49 27 29 53 39 65 35 13\r\n", "output": "Yes\r\n"}, {"input": "39 87\r\n16 18 15 30 33 21 9 3 31 16 10 34 20 35 8 26 23\r\n36 33 75 81 24 42 54 78 39 57 60 30 36 63 4 76 25 1 40 73 22 58 49 85 31 74 59 20 44 83 65 23 41 71 47 14 35\r\n", "output": "Yes\r\n"}, {"input": "36 100\r\n10 0 32 4 5 33 30 18 14 35 7\r\n29 60 32 20 4 16 69 5 38 50 46 74 94 18 82 2 66 22 42 55 51 91 67 75 35 95 43 79 3 27\r\n", "output": "Yes\r\n"}, {"input": "90 25\r\n26 55 30 35 20 15 26 6 1 41 81 76 46 57 17 12 67 77 27 47 62 8 43 63 3 48 19\r\n9 10 16 21 7 17 12 13 19 9\r\n", "output": "Yes\r\n"}, {"input": "66 66\r\n26 0 54 6 37 43 13 25 38 2 32 56 20 50 39 27 51 9 64 4 16 17 65 11 5 47 23\r\n15 6 24 43 49 25 20 14 63 27 3 58 52 53 11 41\r\n", "output": "No\r\n"}, {"input": "24 60\r\n4 0 2 19 23\r\n15 12 24 49 2 14 3 52 28 5 6 19 32 33 34 35\r\n", "output": "Yes\r\n"}, {"input": "80 40\r\n27 0 41 44 45 6 47 8 10 52 13 14 16 17 18 59 21 62 23 64 26 68 29 32 75 37 78 39\r\n13 2 3 9 11 15 20 25 27 30 31 33 34 36\r\n", "output": "Yes\r\n"}, {"input": "66 99\r\n23 33 35 36 38 8 10 44 11 45 46 47 50 19 54 22 55 23 58 59 27 61 30 65\r\n32 33 67 69 4 70 38 6 39 7 74 42 9 43 12 13 14 15 81 82 84 85 20 87 89 90 24 58 59 27 95 97 31\r\n", "output": "Yes\r\n"}, {"input": "100 40\r\n25 61 42 2 3 25 46 66 68 69 49 9 10 50 91 72 92 33 73 53 14 15 55 96 36 39\r\n12 0 22 3 23 4 6 27 11 35 37 38 39\r\n", "output": "Yes\r\n"}, {"input": "90 30\r\n27 15 16 2 32 78 49 64 65 50 6 66 21 22 82 23 39 84 85 10 86 56 27 87 13 58 44 74\r\n7 19 4 20 24 25 12 27\r\n", "output": "No\r\n"}, {"input": "75 75\r\n33 30 74 57 23 19 42 71 11 44 29 58 43 48 61 63 13 27 50 17 18 70 64 39 12 32 36 10 40 51 49 1 54 73\r\n8 43 23 0 7 63 47 74 28\r\n", "output": "No\r\n"}, {"input": "98 98\r\n23 6 81 90 28 38 51 23 69 13 95 15 16 88 58 10 26 42 44 54 92 27 45 39\r\n18 20 70 38 82 72 61 37 78 74 23 15 56 59 35 93 64 28 57\r\n", "output": "No\r\n"}, {"input": "75 75\r\n19 48 3 5 67 23 8 70 45 63 36 38 56 15 10 37 52 11 9 27\r\n21 13 9 45 28 59 36 30 43 5 38 27 40 50 17 41 71 8 51 63 1 33\r\n", "output": "No\r\n"}, {"input": "3 20\r\n0\r\n1 19\r\n", "output": "Yes\r\n"}, {"input": "41 2\r\n1 33\r\n0\r\n", "output": "Yes\r\n"}, {"input": "50 49\r\n1 49\r\n0\r\n", "output": "Yes\r\n"}, {"input": "3 50\r\n0\r\n1 49\r\n", "output": "Yes\r\n"}, {"input": "100 100\r\n50 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49\r\n49 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\r\n", "output": "No\r\n"}, {"input": "100 100\r\n50 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49\r\n50 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\r\n", "output": "Yes\r\n"}, {"input": "91 98\r\n78 0 1 2 3 4 5 7 8 9 10 11 12 14 15 16 17 18 19 21 22 23 24 25 26 28 29 30 31 32 33 35 36 37 38 39 40 42 43 44 45 46 47 49 50 51 52 53 54 56 57 58 59 60 61 63 64 65 66 67 68 70 71 72 73 74 75 77 78 79 80 81 82 84 85 86 87 88 89\r\n84 0 1 2 3 4 5 7 8 9 10 11 12 14 15 16 17 18 19 21 22 23 24 25 26 28 29 30 31 32 33 35 36 37 38 39 40 42 43 44 45 46 47 49 50 51 52 53 54 56 57 58 59 60 61 63 64 65 66 67 68 70 71 72 73 74 75 77 78 79 80 81 82 84 85 86 87 88 89 91 92 93 94 95 96\r\n", "output": "No\r\n"}, {"input": "99 84\r\n66 0 2 3 5 6 8 9 11 12 14 15 17 18 20 21 23 24 26 27 29 30 32 33 35 36 38 39 41 42 44 45 47 48 50 51 53 54 56 57 59 60 62 63 65 66 68 69 71 72 74 75 77 78 80 81 83 84 86 87 89 90 92 93 95 96 98\r\n56 0 2 3 5 6 8 9 11 12 14 15 17 18 20 21 23 24 26 27 29 30 32 33 35 36 38 39 41 42 44 45 47 48 50 51 53 54 56 57 59 60 62 63 65 66 68 69 71 72 74 75 77 78 80 81 83\r\n", "output": "No\r\n"}, {"input": "75 90\r\n60 0 2 3 4 5 7 8 9 10 12 13 14 15 17 18 19 20 22 23 24 25 27 28 29 30 32 33 34 35 37 38 39 40 42 43 44 45 47 48 49 50 52 53 54 55 57 58 59 60 62 63 64 65 67 68 69 70 72 73 74\r\n72 0 2 3 4 5 7 8 9 10 12 13 14 15 17 18 19 20 22 23 24 25 27 28 29 30 32 33 34 35 37 38 39 40 42 43 44 45 47 48 49 50 52 53 54 55 57 58 59 60 62 63 64 65 67 68 69 70 72 73 74 75 77 78 79 80 82 83 84 85 87 88 89\r\n", "output": "No\r\n"}, {"input": "5 7\r\n1 0\r\n1 0\r\n", "output": "Yes\r\n"}, {"input": "100 1\r\n1 99\r\n0\r\n", "output": "Yes\r\n"}, {"input": "4 1\r\n1 3\r\n0\r\n", "output": "Yes\r\n"}, {"input": "4 5\r\n3 0 1 3\r\n4 0 1 3 4\r\n", "output": "Yes\r\n"}, {"input": "100 99\r\n1 99\r\n0\r\n", "output": "Yes\r\n"}, {"input": "2 3\r\n1 0\r\n2 0 2\r\n", "output": "Yes\r\n"}]
false
stdio
null
true
725/A
725
A
Python 3
TESTS
8
46
614,400
30551023
n=int(input()) s=input() k=s.count('><') if k==0: pass elif k==1: k=s.find('><') for i in range(k+1,n): if s[i]=='<': n-=1 else: break for i in range(k,-1,-1): if s[i]=='>': n-=1 else: break else: k=s.rfind('><') for i in range(k+1,n): if s[i]=='<': n-=1 else: break q=s.find('><') for i in range(k,-1,-1): if s[i]=='<': n-=1 else: break n-=k-q+1 print(n)
78
46
102,400
146871880
def solve(): n = int(input()) s = input() ans = 0 for i in s: if i == "<": ans += 1 else: break for i in s[::-1]: if i == ">": ans += 1 else: break print(ans) solve()
Canada Cup 2016
CF
2,016
2
256
Jumping Ball
In a new version of the famous Pinball game, one of the most important parts of the game field is a sequence of n bumpers. The bumpers are numbered with integers from 1 to n from left to right. There are two types of bumpers. They are denoted by the characters '<' and '>'. When the ball hits the bumper at position i it goes one position to the right (to the position i + 1) if the type of this bumper is '>', or one position to the left (to i - 1) if the type of the bumper at position i is '<'. If there is no such position, in other words if i - 1 < 1 or i + 1 > n, the ball falls from the game field. Depending on the ball's starting position, the ball may eventually fall from the game field or it may stay there forever. You are given a string representing the bumpers' types. Calculate the number of positions such that the ball will eventually fall from the game field if it starts at that position.
The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the length of the sequence of bumpers. The second line contains the string, which consists of the characters '<' and '>'. The character at the i-th position of this string corresponds to the type of the i-th bumper.
Print one integer — the number of positions in the sequence such that the ball will eventually fall from the game field if it starts at that position.
null
In the first sample, the ball will fall from the field if starts at position 1 or position 2. In the second sample, any starting position will result in the ball falling from the field.
[{"input": "4\n<<><", "output": "2"}, {"input": "5\n>>>>>", "output": "5"}, {"input": "4\n>><<", "output": "0"}]
1,000
["implementation"]
78
[{"input": "4\r\n<<><\r\n", "output": "2\r\n"}, {"input": "5\r\n>>>>>\r\n", "output": "5\r\n"}, {"input": "4\r\n>><<\r\n", "output": "0\r\n"}, {"input": "3\r\n<<>\r\n", "output": "3\r\n"}, {"input": "3\r\n<<<\r\n", "output": "3\r\n"}, {"input": "3\r\n><<\r\n", "output": "0\r\n"}, {"input": "1\r\n<\r\n", "output": "1\r\n"}, {"input": "2\r\n<>\r\n", "output": "2\r\n"}, {"input": "3\r\n<>>\r\n", "output": "3\r\n"}, {"input": "3\r\n><>\r\n", "output": "1\r\n"}, {"input": "2\r\n><\r\n", "output": "0\r\n"}, {"input": "2\r\n>>\r\n", "output": "2\r\n"}, {"input": "2\r\n<<\r\n", "output": "2\r\n"}, {"input": "1\r\n>\r\n", "output": "1\r\n"}, {"input": "3\r\n>><\r\n", "output": "0\r\n"}, {"input": "3\r\n>>>\r\n", "output": "3\r\n"}, {"input": "3\r\n<><\r\n", "output": "1\r\n"}, {"input": "10\r\n<<<><<<>>>\r\n", "output": "6\r\n"}, {"input": "20\r\n><><<><<<>>>>>>>>>>>\r\n", "output": "11\r\n"}, {"input": "20\r\n<<<<<<<<<<><<<<>>>>>\r\n", "output": "15\r\n"}, {"input": "50\r\n<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>\r\n", "output": "50\r\n"}, {"input": "100\r\n<<<<<<<<<<<<<<<<<<<<<<<<>><<>><<<<<>><>><<<>><><<>>><<>>><<<<><><><<><<<<><>>>>>>>>>>>>>>>>>>>>>>>>>\r\n", "output": "49\r\n"}, {"input": "100\r\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>><<>><>><>><<><><><><>>>><><<<>>>><<<>>>>>>><><\r\n", "output": "50\r\n"}, {"input": "100\r\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\r\n", "output": "100\r\n"}, {"input": "100\r\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\r\n", "output": "100\r\n"}, {"input": "12\r\n<<>><<>><<>>\r\n", "output": "4\r\n"}, {"input": "6\r\n<<><>>\r\n", "output": "4\r\n"}, {"input": "6\r\n><>>>>\r\n", "output": "4\r\n"}, {"input": "8\r\n>>>><<<>\r\n", "output": "1\r\n"}, {"input": "4\r\n<><>\r\n", "output": "2\r\n"}, {"input": "4\r\n><><\r\n", "output": "0\r\n"}, {"input": "7\r\n<<>>><>\r\n", "output": "3\r\n"}, {"input": "10\r\n><><>>>>>>\r\n", "output": "6\r\n"}, {"input": "5\r\n<><>>\r\n", "output": "3\r\n"}, {"input": "12\r\n<><<<<>>>>>>\r\n", "output": "7\r\n"}, {"input": "6\r\n<>><<>\r\n", "output": "2\r\n"}, {"input": "6\r\n>>><>>\r\n", "output": "2\r\n"}, {"input": "10\r\n><><>>>><>\r\n", "output": "1\r\n"}, {"input": "5\r\n><>>>\r\n", "output": "3\r\n"}, {"input": "5\r\n<<><>\r\n", "output": "3\r\n"}, {"input": "5\r\n<><<<\r\n", "output": "1\r\n"}, {"input": "4\r\n<><<\r\n", "output": "1\r\n"}, {"input": "8\r\n<<>><<>>\r\n", "output": "4\r\n"}, {"input": "7\r\n<<><>>>\r\n", "output": "5\r\n"}, {"input": "5\r\n><<>>\r\n", "output": "2\r\n"}, {"input": "10\r\n<<<<<>>>>>\r\n", "output": "10\r\n"}, {"input": "6\r\n><<<<<\r\n", "output": "0\r\n"}, {"input": "8\r\n<<><><>>\r\n", "output": "4\r\n"}, {"input": "10\r\n<<<<><<<><\r\n", "output": "4\r\n"}, {"input": "12\r\n<<<>>>><<>>>\r\n", "output": "6\r\n"}, {"input": "4\r\n><>>\r\n", "output": "2\r\n"}, {"input": "11\r\n<<><<>><<>>\r\n", "output": "4\r\n"}]
false
stdio
null
true
45/I
45
I
PyPy 3-64
TESTS
13
124
0
167949969
import sys input = sys.stdin.readline n = int(input()) w = sorted(map(int, input().split())) d = [i for i in w if i < 0] e = [i for i in w if i > 0] if len(d) == len(e) == 0: print(0) exit(0) if len(e) == 0 and len(d) == 1: print(d[0]) exit(0) if len(d) % 2: d.sort() d = d[:-1] e.extend(d) print(' '.join(map(str, e)))
65
154
6,963,200
125022759
n = int(input()) a = list(map(int, input().split())) pos = [] neg = [] z = 0 for i in range(n): if a[i] > 0: pos.append(a[i]) elif a[i] < 0: neg.append(a[i]) else: z += 1 m = len(neg) neg.sort() if m % 2: m -= 1 for i in range(m): pos.append(neg[i]) if len(pos) > 0: for i in range(len(pos)): print(pos[i], end=' ') else: # either there are a single negative, 0 positives, 0 or more zeros if z: print(0) else: print(neg[0])
School Team Contest 3 (Winter Computer School 2010/11)
ICPC
2,010
2
256
TCMCF+++
Vasya has gotten interested in programming contests in TCMCF+++ rules. On the contest n problems were suggested and every problem had a cost — a certain integral number of points (perhaps, negative or even equal to zero). According to TCMCF+++ rules, only accepted problems can earn points and the overall number of points of a contestant was equal to the product of the costs of all the problems he/she had completed. If a person didn't solve anything, then he/she didn't even appear in final standings and wasn't considered as participant. Vasya understood that to get the maximal number of points it is not always useful to solve all the problems. Unfortunately, he understood it only after the contest was finished. Now he asks you to help him: find out what problems he had to solve to earn the maximal number of points.
The first line contains an integer n (1 ≤ n ≤ 100) — the number of the suggested problems. The next line contains n space-separated integers ci ( - 100 ≤ ci ≤ 100) — the cost of the i-th task. The tasks' costs may coinсide.
Print space-separated the costs of the problems that needed to be solved to get the maximal possible number of points. Do not forget, please, that it was necessary to solve at least one problem. If there are several solutions to that problem, print any of them.
null
null
[{"input": "5\n1 2 -3 3 3", "output": "3 1 2 3"}, {"input": "13\n100 100 100 100 100 100 100 100 100 100 100 100 100", "output": "100 100 100 100 100 100 100 100 100 100 100 100 100"}, {"input": "4\n-2 -2 -2 -2", "output": "-2 -2 -2 -2"}]
1,400
["greedy"]
65
[{"input": "5\r\n1 2 -3 3 3\r\n", "output": "3 1 2 3 \r\n"}, {"input": "13\r\n100 100 100 100 100 100 100 100 100 100 100 100 100\r\n", "output": "100 100 100 100 100 100 100 100 100 100 100 100 100 \r\n"}, {"input": "4\r\n-2 -2 -2 -2\r\n", "output": "-2 -2 -2 -2 \r\n"}, {"input": "1\r\n1\r\n", "output": "1 \r\n"}, {"input": "1\r\n-1\r\n", "output": "-1 \r\n"}, {"input": "1\r\n0\r\n", "output": "0\r\n"}, {"input": "2\r\n1 1\r\n", "output": "1 1 \r\n"}, {"input": "2\r\n1 -1\r\n", "output": "1 \r\n"}, {"input": "2\r\n-1 1\r\n", "output": "1 \r\n"}, {"input": "2\r\n-1 -1\r\n", "output": "-1 -1 \r\n"}, {"input": "2\r\n1 0\r\n", "output": "1 \r\n"}, {"input": "2\r\n0 1\r\n", "output": "1 \r\n"}, {"input": "2\r\n0 0\r\n", "output": "0\r\n"}, {"input": "2\r\n-1 0\r\n", "output": "0\r\n"}, {"input": "2\r\n0 -1\r\n", "output": "0\r\n"}, {"input": "1\r\n13\r\n", "output": "13 \r\n"}, {"input": "1\r\n-13\r\n", "output": "-13 \r\n"}, {"input": "1\r\n100\r\n", "output": "100 \r\n"}, {"input": "1\r\n-100\r\n", "output": "-100 \r\n"}, {"input": "2\r\n100 100\r\n", "output": "100 100 \r\n"}, {"input": "2\r\n100 -100\r\n", "output": "100 \r\n"}, {"input": "2\r\n-100 100\r\n", "output": "100 \r\n"}, {"input": "2\r\n100 0\r\n", "output": "100 \r\n"}, {"input": "2\r\n0 100\r\n", "output": "100 \r\n"}, {"input": "2\r\n0 -100\r\n", "output": "0\r\n"}, {"input": "2\r\n-100 0\r\n", "output": "0\r\n"}, {"input": "1\r\n3\r\n", "output": "3 \r\n"}, {"input": "2\r\n0 -1\r\n", "output": "0\r\n"}, {"input": "2\r\n-1 2\r\n", "output": "2 \r\n"}, {"input": "2\r\n2 2\r\n", "output": "2 2 \r\n"}, {"input": "2\r\n-1 -2\r\n", "output": "-1 -2 \r\n"}, {"input": "2\r\n-2 -1\r\n", "output": "-1 -2 \r\n"}, {"input": "2\r\n1 2\r\n", "output": "2 1 \r\n"}, {"input": "2\r\n0 -2\r\n", "output": "0\r\n"}, {"input": "2\r\n-2 -1\r\n", "output": "-1 -2 \r\n"}, {"input": "3\r\n0 -2 -1\r\n", "output": "-1 -2 \r\n"}, {"input": "3\r\n2 1 -1\r\n", "output": "2 1 \r\n"}, {"input": "3\r\n0 1 2\r\n", "output": "2 1 \r\n"}, {"input": "3\r\n-2 2 2\r\n", "output": "2 2 \r\n"}, {"input": "3\r\n1 -1 2\r\n", "output": "2 1 \r\n"}, {"input": "3\r\n-2 0 2\r\n", "output": "2 \r\n"}, {"input": "3\r\n1 0 2\r\n", "output": "2 1 \r\n"}, {"input": "3\r\n-1 2 2\r\n", "output": "2 2 \r\n"}, {"input": "4\r\n0 0 2 -2\r\n", "output": "2 \r\n"}, {"input": "4\r\n1 0 -1 2\r\n", "output": "2 1 \r\n"}, {"input": "4\r\n-2 0 -2 0\r\n", "output": "-2 -2 \r\n"}, {"input": "4\r\n2 2 1 -1\r\n", "output": "2 1 2 \r\n"}, {"input": "4\r\n-1 2 0 -2\r\n", "output": "2 -1 -2 \r\n"}, {"input": "4\r\n1 2 -2 1\r\n", "output": "2 1 1 \r\n"}, {"input": "4\r\n-2 -1 2 2\r\n", "output": "2 -1 2 -2 \r\n"}, {"input": "4\r\n-1 -1 -2 0\r\n", "output": "-1 -2 \r\n"}, {"input": "10\r\n-10 5 9 -10 2 -7 10 10 6 -9\r\n", "output": "10 -10 -9 -7 2 5 6 9 10 -10 \r\n"}, {"input": "10\r\n9 10 9 10 10 10 1 5 10 5\r\n", "output": "10 5 5 9 9 10 10 10 10 1 \r\n"}, {"input": "10\r\n-3 -9 -10 -10 -9 -8 -9 -9 -8 -9\r\n", "output": "-3 -10 -9 -9 -9 -9 -9 -8 -8 -10 \r\n"}, {"input": "10\r\n-5 -5 4 1 -8 -3 -9 -2 4 4\r\n", "output": "4 -8 -5 -5 -3 -2 1 4 4 -9 \r\n"}]
false
stdio
import sys from collections import Counter def compute_max_product(ci): if all(x == 0 for x in ci): return 0 positives = [x for x in ci if x > 0] negatives = [x for x in ci if x < 0] zeros = [x for x in ci if x == 0] non_zero = positives + negatives if not non_zero: return 0 product_all = 1 for num in non_zero: product_all *= num candidates = [] if product_all > 0: candidates.append(product_all) else: candidates.append(product_all) if zeros: candidates.append(0) if negatives: max_neg = max(negatives) if len(non_zero) > 1: new_product = product_all // max_neg candidates.append(new_product) if zeros: candidates.append(0) if not candidates: return product_all return max(candidates) def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path, 'r') as f: n = int(f.readline().strip()) ci = list(map(int, f.readline().strip().split())) with open(submission_path, 'r') as f: submitted_line = f.readline().strip() if not submitted_line: print(0) return submitted = list(map(int, submitted_line.split())) if not submitted: print(0) return input_counter = Counter(ci) submission_counter = Counter(submitted) for num in submission_counter: if submission_counter[num] > input_counter.get(num, 0): print(0) return submitted_product = 1 for num in submitted: submitted_product *= num max_product = compute_max_product(ci) print(1 if submitted_product == max_product else 0) if __name__ == "__main__": main()
true
550/A
550
A
PyPy 3-64
TESTS
51
62
0
198567618
def solve(): s = input() abi0 = s.find('AB', 0) abi1 = s.find('AB', abi0+1) if abi0 != -1 else -1 abi2 = s.find('AB', abi1+1) if abi1 != -1 else -1 bai0 = s.find('BA') if bai0 == -1: return False if abi0 != -1 and abi0 != bai0-1 and abi0 != bai0+1: return True if abi1 != -1 and abi1 != bai0-1 and abi1 != bai0+1: return True if abi2 != -1 and abi2 != bai0-1 and abi2 != bai0+1: return True return False if solve(): print('YES') else: print('NO')
85
46
0
195984665
s = input().strip() if 'AB' in s and 'BA' in s[s.index('AB')+2:]: print('YES') elif 'BA' in s and 'AB' in s[s.index('BA')+2:]: print('YES') else: print('NO')
Codeforces Round 306 (Div. 2)
CF
2,015
2
256
Two Substrings
You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order).
The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters.
Print "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise.
null
In the first sample test, despite the fact that there are substrings "AB" and "BA", their occurrences overlap, so the answer is "NO". In the second sample test there are the following occurrences of the substrings: BACFAB. In the third sample test there is no substring "AB" nor substring "BA".
[{"input": "ABA", "output": "NO"}, {"input": "BACFAB", "output": "YES"}, {"input": "AXBYBXA", "output": "NO"}]
1,500
["brute force", "dp", "greedy", "implementation", "strings"]
85
[{"input": "ABA\r\n", "output": "NO\r\n"}, {"input": "BACFAB\r\n", "output": "YES\r\n"}, {"input": "AXBYBXA\r\n", "output": "NO\r\n"}, {"input": "ABABAB\r\n", "output": "YES\r\n"}, {"input": "BBBBBBBBBB\r\n", "output": "NO\r\n"}, {"input": "ABBA\r\n", "output": "YES\r\n"}, {"input": "ABAXXXAB\r\n", "output": "YES\r\n"}, {"input": "TESTABAXXABTEST\r\n", "output": "YES\r\n"}, {"input": "A\r\n", "output": "NO\r\n"}, {"input": "B\r\n", "output": "NO\r\n"}, {"input": "X\r\n", "output": "NO\r\n"}, {"input": "BA\r\n", "output": "NO\r\n"}, {"input": "AB\r\n", "output": "NO\r\n"}, {"input": "AA\r\n", "output": "NO\r\n"}, {"input": "BB\r\n", "output": "NO\r\n"}, {"input": "BAB\r\n", "output": "NO\r\n"}, {"input": "AAB\r\n", "output": "NO\r\n"}, {"input": "BAA\r\n", "output": "NO\r\n"}, {"input": "ABB\r\n", "output": "NO\r\n"}, {"input": "BBA\r\n", "output": "NO\r\n"}, {"input": "AAA\r\n", "output": "NO\r\n"}, {"input": "BBB\r\n", "output": "NO\r\n"}, {"input": "AXBXBXA\r\n", "output": "NO\r\n"}, {"input": "SKDSKDJABSDBADKFJDK\r\n", "output": "YES\r\n"}, {"input": "ABAXXBBXXAA\r\n", "output": "NO\r\n"}, {"input": "ABAB\r\n", "output": "NO\r\n"}, {"input": "BABA\r\n", "output": "NO\r\n"}, {"input": "AAAB\r\n", "output": "NO\r\n"}, {"input": "AAAA\r\n", "output": "NO\r\n"}, {"input": "AABA\r\n", "output": "NO\r\n"}, {"input": "ABAA\r\n", "output": "NO\r\n"}, {"input": "BAAA\r\n", "output": "NO\r\n"}, {"input": "AABB\r\n", "output": "NO\r\n"}, {"input": "BAAB\r\n", "output": "YES\r\n"}, {"input": "BBAA\r\n", "output": "NO\r\n"}, {"input": "BBBA\r\n", "output": "NO\r\n"}, {"input": "BBAB\r\n", "output": "NO\r\n"}, {"input": "BABB\r\n", "output": "NO\r\n"}, {"input": "ABBB\r\n", "output": "NO\r\n"}, {"input": "BBBB\r\n", "output": "NO\r\n"}, {"input": "BABAB\r\n", "output": "YES\r\n"}, {"input": "ABABA\r\n", "output": "YES\r\n"}, {"input": "AAABAAACBBBC\r\n", "output": "NO\r\n"}, {"input": "AABBBACBBBBBBAACBCCACBBAABBBBBCAACABAACABCACCBCBCCCBCBCABCBBCCBCBBAACBACAABACBBCACCBCCACCABBCBABBAAC\r\n", "output": "YES\r\n"}, {"input": "CBBABDDBBADAC\r\n", "output": "YES\r\n"}, {"input": "ABYFAB\r\n", "output": "NO\r\n"}, {"input": "BABXXAB\r\n", "output": "YES\r\n"}, {"input": "ABAXAB\r\n", "output": "YES\r\n"}, {"input": "ABABXAB\r\n", "output": "YES\r\n"}, {"input": "ABXABXABXABXABXBAXBAXBAXBA\r\n", "output": "YES\r\n"}, {"input": "QQQQQQQQQABABQQQQQQQQ\r\n", "output": "NO\r\n"}]
false
stdio
null
true
45/I
45
I
Python 3
TESTS
9
218
307,200
75714701
def TCMCF(): num = int(input()) costs = [int(i) for i in input().split()] if num == 1: print(costs[0]) elif num == 2: print(max(costs[0]*costs[1],costs[0],costs[1])) else: negative_numbers = [] for i in costs: if i < 0: negative_numbers.append(i) if costs.count(0) == len(costs)-1: if negative_numbers == 1: print(0) else: for i in costs: if i != 0: print(i) else: for i in costs: if i == 0: costs.remove(i) if len(negative_numbers) % 2 == 1: costs.remove(max(negative_numbers)) for i in costs: if abs(i) < 1: costs.remove(i) print(" ".join([str(i) for i in costs])) TCMCF()
65
218
307,200
93067158
from bisect import bisect_left as bl;n=int(input()) if n==1: print(input()) else: a=list(map(int,input().split()));a.sort() if bl(a,0)%2==1: if a[bl(a,0)-1]<0:a.remove(a[bl(a,0)-1]) q=len(a) while 0 in a and q>1:a.remove(0);q-=1 print(*a)
School Team Contest 3 (Winter Computer School 2010/11)
ICPC
2,010
2
256
TCMCF+++
Vasya has gotten interested in programming contests in TCMCF+++ rules. On the contest n problems were suggested and every problem had a cost — a certain integral number of points (perhaps, negative or even equal to zero). According to TCMCF+++ rules, only accepted problems can earn points and the overall number of points of a contestant was equal to the product of the costs of all the problems he/she had completed. If a person didn't solve anything, then he/she didn't even appear in final standings and wasn't considered as participant. Vasya understood that to get the maximal number of points it is not always useful to solve all the problems. Unfortunately, he understood it only after the contest was finished. Now he asks you to help him: find out what problems he had to solve to earn the maximal number of points.
The first line contains an integer n (1 ≤ n ≤ 100) — the number of the suggested problems. The next line contains n space-separated integers ci ( - 100 ≤ ci ≤ 100) — the cost of the i-th task. The tasks' costs may coinсide.
Print space-separated the costs of the problems that needed to be solved to get the maximal possible number of points. Do not forget, please, that it was necessary to solve at least one problem. If there are several solutions to that problem, print any of them.
null
null
[{"input": "5\n1 2 -3 3 3", "output": "3 1 2 3"}, {"input": "13\n100 100 100 100 100 100 100 100 100 100 100 100 100", "output": "100 100 100 100 100 100 100 100 100 100 100 100 100"}, {"input": "4\n-2 -2 -2 -2", "output": "-2 -2 -2 -2"}]
1,400
["greedy"]
65
[{"input": "5\r\n1 2 -3 3 3\r\n", "output": "3 1 2 3 \r\n"}, {"input": "13\r\n100 100 100 100 100 100 100 100 100 100 100 100 100\r\n", "output": "100 100 100 100 100 100 100 100 100 100 100 100 100 \r\n"}, {"input": "4\r\n-2 -2 -2 -2\r\n", "output": "-2 -2 -2 -2 \r\n"}, {"input": "1\r\n1\r\n", "output": "1 \r\n"}, {"input": "1\r\n-1\r\n", "output": "-1 \r\n"}, {"input": "1\r\n0\r\n", "output": "0\r\n"}, {"input": "2\r\n1 1\r\n", "output": "1 1 \r\n"}, {"input": "2\r\n1 -1\r\n", "output": "1 \r\n"}, {"input": "2\r\n-1 1\r\n", "output": "1 \r\n"}, {"input": "2\r\n-1 -1\r\n", "output": "-1 -1 \r\n"}, {"input": "2\r\n1 0\r\n", "output": "1 \r\n"}, {"input": "2\r\n0 1\r\n", "output": "1 \r\n"}, {"input": "2\r\n0 0\r\n", "output": "0\r\n"}, {"input": "2\r\n-1 0\r\n", "output": "0\r\n"}, {"input": "2\r\n0 -1\r\n", "output": "0\r\n"}, {"input": "1\r\n13\r\n", "output": "13 \r\n"}, {"input": "1\r\n-13\r\n", "output": "-13 \r\n"}, {"input": "1\r\n100\r\n", "output": "100 \r\n"}, {"input": "1\r\n-100\r\n", "output": "-100 \r\n"}, {"input": "2\r\n100 100\r\n", "output": "100 100 \r\n"}, {"input": "2\r\n100 -100\r\n", "output": "100 \r\n"}, {"input": "2\r\n-100 100\r\n", "output": "100 \r\n"}, {"input": "2\r\n100 0\r\n", "output": "100 \r\n"}, {"input": "2\r\n0 100\r\n", "output": "100 \r\n"}, {"input": "2\r\n0 -100\r\n", "output": "0\r\n"}, {"input": "2\r\n-100 0\r\n", "output": "0\r\n"}, {"input": "1\r\n3\r\n", "output": "3 \r\n"}, {"input": "2\r\n0 -1\r\n", "output": "0\r\n"}, {"input": "2\r\n-1 2\r\n", "output": "2 \r\n"}, {"input": "2\r\n2 2\r\n", "output": "2 2 \r\n"}, {"input": "2\r\n-1 -2\r\n", "output": "-1 -2 \r\n"}, {"input": "2\r\n-2 -1\r\n", "output": "-1 -2 \r\n"}, {"input": "2\r\n1 2\r\n", "output": "2 1 \r\n"}, {"input": "2\r\n0 -2\r\n", "output": "0\r\n"}, {"input": "2\r\n-2 -1\r\n", "output": "-1 -2 \r\n"}, {"input": "3\r\n0 -2 -1\r\n", "output": "-1 -2 \r\n"}, {"input": "3\r\n2 1 -1\r\n", "output": "2 1 \r\n"}, {"input": "3\r\n0 1 2\r\n", "output": "2 1 \r\n"}, {"input": "3\r\n-2 2 2\r\n", "output": "2 2 \r\n"}, {"input": "3\r\n1 -1 2\r\n", "output": "2 1 \r\n"}, {"input": "3\r\n-2 0 2\r\n", "output": "2 \r\n"}, {"input": "3\r\n1 0 2\r\n", "output": "2 1 \r\n"}, {"input": "3\r\n-1 2 2\r\n", "output": "2 2 \r\n"}, {"input": "4\r\n0 0 2 -2\r\n", "output": "2 \r\n"}, {"input": "4\r\n1 0 -1 2\r\n", "output": "2 1 \r\n"}, {"input": "4\r\n-2 0 -2 0\r\n", "output": "-2 -2 \r\n"}, {"input": "4\r\n2 2 1 -1\r\n", "output": "2 1 2 \r\n"}, {"input": "4\r\n-1 2 0 -2\r\n", "output": "2 -1 -2 \r\n"}, {"input": "4\r\n1 2 -2 1\r\n", "output": "2 1 1 \r\n"}, {"input": "4\r\n-2 -1 2 2\r\n", "output": "2 -1 2 -2 \r\n"}, {"input": "4\r\n-1 -1 -2 0\r\n", "output": "-1 -2 \r\n"}, {"input": "10\r\n-10 5 9 -10 2 -7 10 10 6 -9\r\n", "output": "10 -10 -9 -7 2 5 6 9 10 -10 \r\n"}, {"input": "10\r\n9 10 9 10 10 10 1 5 10 5\r\n", "output": "10 5 5 9 9 10 10 10 10 1 \r\n"}, {"input": "10\r\n-3 -9 -10 -10 -9 -8 -9 -9 -8 -9\r\n", "output": "-3 -10 -9 -9 -9 -9 -9 -8 -8 -10 \r\n"}, {"input": "10\r\n-5 -5 4 1 -8 -3 -9 -2 4 4\r\n", "output": "4 -8 -5 -5 -3 -2 1 4 4 -9 \r\n"}]
false
stdio
import sys from collections import Counter def compute_max_product(ci): if all(x == 0 for x in ci): return 0 positives = [x for x in ci if x > 0] negatives = [x for x in ci if x < 0] zeros = [x for x in ci if x == 0] non_zero = positives + negatives if not non_zero: return 0 product_all = 1 for num in non_zero: product_all *= num candidates = [] if product_all > 0: candidates.append(product_all) else: candidates.append(product_all) if zeros: candidates.append(0) if negatives: max_neg = max(negatives) if len(non_zero) > 1: new_product = product_all // max_neg candidates.append(new_product) if zeros: candidates.append(0) if not candidates: return product_all return max(candidates) def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path, 'r') as f: n = int(f.readline().strip()) ci = list(map(int, f.readline().strip().split())) with open(submission_path, 'r') as f: submitted_line = f.readline().strip() if not submitted_line: print(0) return submitted = list(map(int, submitted_line.split())) if not submitted: print(0) return input_counter = Counter(ci) submission_counter = Counter(submitted) for num in submission_counter: if submission_counter[num] > input_counter.get(num, 0): print(0) return submitted_product = 1 for num in submitted: submitted_product *= num max_product = compute_max_product(ci) print(1 if submitted_product == max_product else 0) if __name__ == "__main__": main()
true
550/A
550
A
Python 3
TESTS
50
77
1,843,200
225913270
s = str(input()) a = 0 b = 0 i = 0 ans = [] while i < (len(s)-1) : if s[i] == "A" and s[i+1] == "B" : a += 1 ans.append(i) i += 1 elif s[i] == "B" and s[i+1] == "A" : b += 1 ans.append(i) i += 2 else: i += 1 if a>=2 and b>=2 : print("YES") elif a<1 or b<1: print("NO") else: diff =[] for i in range(len(ans)-1): diff.append(ans[i+1] - ans[i]) aaa = False for i in range(len(diff)): if diff[i] > 1: aaa = True if aaa: print("YES") else: print("NO")
85
46
0
196334991
s=input();u=s.find('AB');v=s.find('BA') print('YES'if(u+1and s.find('BA',u+2)+1)or(v+1and s.find('AB',v+2)+1)else'NO')
Codeforces Round 306 (Div. 2)
CF
2,015
2
256
Two Substrings
You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order).
The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters.
Print "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise.
null
In the first sample test, despite the fact that there are substrings "AB" and "BA", their occurrences overlap, so the answer is "NO". In the second sample test there are the following occurrences of the substrings: BACFAB. In the third sample test there is no substring "AB" nor substring "BA".
[{"input": "ABA", "output": "NO"}, {"input": "BACFAB", "output": "YES"}, {"input": "AXBYBXA", "output": "NO"}]
1,500
["brute force", "dp", "greedy", "implementation", "strings"]
85
[{"input": "ABA\r\n", "output": "NO\r\n"}, {"input": "BACFAB\r\n", "output": "YES\r\n"}, {"input": "AXBYBXA\r\n", "output": "NO\r\n"}, {"input": "ABABAB\r\n", "output": "YES\r\n"}, {"input": "BBBBBBBBBB\r\n", "output": "NO\r\n"}, {"input": "ABBA\r\n", "output": "YES\r\n"}, {"input": "ABAXXXAB\r\n", "output": "YES\r\n"}, {"input": "TESTABAXXABTEST\r\n", "output": "YES\r\n"}, {"input": "A\r\n", "output": "NO\r\n"}, {"input": "B\r\n", "output": "NO\r\n"}, {"input": "X\r\n", "output": "NO\r\n"}, {"input": "BA\r\n", "output": "NO\r\n"}, {"input": "AB\r\n", "output": "NO\r\n"}, {"input": "AA\r\n", "output": "NO\r\n"}, {"input": "BB\r\n", "output": "NO\r\n"}, {"input": "BAB\r\n", "output": "NO\r\n"}, {"input": "AAB\r\n", "output": "NO\r\n"}, {"input": "BAA\r\n", "output": "NO\r\n"}, {"input": "ABB\r\n", "output": "NO\r\n"}, {"input": "BBA\r\n", "output": "NO\r\n"}, {"input": "AAA\r\n", "output": "NO\r\n"}, {"input": "BBB\r\n", "output": "NO\r\n"}, {"input": "AXBXBXA\r\n", "output": "NO\r\n"}, {"input": "SKDSKDJABSDBADKFJDK\r\n", "output": "YES\r\n"}, {"input": "ABAXXBBXXAA\r\n", "output": "NO\r\n"}, {"input": "ABAB\r\n", "output": "NO\r\n"}, {"input": "BABA\r\n", "output": "NO\r\n"}, {"input": "AAAB\r\n", "output": "NO\r\n"}, {"input": "AAAA\r\n", "output": "NO\r\n"}, {"input": "AABA\r\n", "output": "NO\r\n"}, {"input": "ABAA\r\n", "output": "NO\r\n"}, {"input": "BAAA\r\n", "output": "NO\r\n"}, {"input": "AABB\r\n", "output": "NO\r\n"}, {"input": "BAAB\r\n", "output": "YES\r\n"}, {"input": "BBAA\r\n", "output": "NO\r\n"}, {"input": "BBBA\r\n", "output": "NO\r\n"}, {"input": "BBAB\r\n", "output": "NO\r\n"}, {"input": "BABB\r\n", "output": "NO\r\n"}, {"input": "ABBB\r\n", "output": "NO\r\n"}, {"input": "BBBB\r\n", "output": "NO\r\n"}, {"input": "BABAB\r\n", "output": "YES\r\n"}, {"input": "ABABA\r\n", "output": "YES\r\n"}, {"input": "AAABAAACBBBC\r\n", "output": "NO\r\n"}, {"input": "AABBBACBBBBBBAACBCCACBBAABBBBBCAACABAACABCACCBCBCCCBCBCABCBBCCBCBBAACBACAABACBBCACCBCCACCABBCBABBAAC\r\n", "output": "YES\r\n"}, {"input": "CBBABDDBBADAC\r\n", "output": "YES\r\n"}, {"input": "ABYFAB\r\n", "output": "NO\r\n"}, {"input": "BABXXAB\r\n", "output": "YES\r\n"}, {"input": "ABAXAB\r\n", "output": "YES\r\n"}, {"input": "ABABXAB\r\n", "output": "YES\r\n"}, {"input": "ABXABXABXABXABXBAXBAXBAXBA\r\n", "output": "YES\r\n"}, {"input": "QQQQQQQQQABABQQQQQQQQ\r\n", "output": "NO\r\n"}]
false
stdio
null
true
550/A
550
A
PyPy 3-64
TESTS
51
77
6,553,600
220380432
a = list(input()) k = False k1 = False i = 0 while i < len(a): if a[i:i+2] == ['B', 'A']: k = True a[i] = '/' a[i+1] = '/' break i += 1 i = 0 while i < len(a): if a[i:i+2] == ['A', 'B']: k1 = True a[i] = '/' a[i+1] = '/' break i += 1 if k == True and k1 == True: print('YES') else: print('NO')
85
46
0
197319059
z=input() u=z.find('AB') v=z.find('BA') print('YES'if(u+1and z.find('BA',u+2)+1)or(v+1and z.find('AB',v+2)+1)else'NO')
Codeforces Round 306 (Div. 2)
CF
2,015
2
256
Two Substrings
You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order).
The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters.
Print "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise.
null
In the first sample test, despite the fact that there are substrings "AB" and "BA", their occurrences overlap, so the answer is "NO". In the second sample test there are the following occurrences of the substrings: BACFAB. In the third sample test there is no substring "AB" nor substring "BA".
[{"input": "ABA", "output": "NO"}, {"input": "BACFAB", "output": "YES"}, {"input": "AXBYBXA", "output": "NO"}]
1,500
["brute force", "dp", "greedy", "implementation", "strings"]
85
[{"input": "ABA\r\n", "output": "NO\r\n"}, {"input": "BACFAB\r\n", "output": "YES\r\n"}, {"input": "AXBYBXA\r\n", "output": "NO\r\n"}, {"input": "ABABAB\r\n", "output": "YES\r\n"}, {"input": "BBBBBBBBBB\r\n", "output": "NO\r\n"}, {"input": "ABBA\r\n", "output": "YES\r\n"}, {"input": "ABAXXXAB\r\n", "output": "YES\r\n"}, {"input": "TESTABAXXABTEST\r\n", "output": "YES\r\n"}, {"input": "A\r\n", "output": "NO\r\n"}, {"input": "B\r\n", "output": "NO\r\n"}, {"input": "X\r\n", "output": "NO\r\n"}, {"input": "BA\r\n", "output": "NO\r\n"}, {"input": "AB\r\n", "output": "NO\r\n"}, {"input": "AA\r\n", "output": "NO\r\n"}, {"input": "BB\r\n", "output": "NO\r\n"}, {"input": "BAB\r\n", "output": "NO\r\n"}, {"input": "AAB\r\n", "output": "NO\r\n"}, {"input": "BAA\r\n", "output": "NO\r\n"}, {"input": "ABB\r\n", "output": "NO\r\n"}, {"input": "BBA\r\n", "output": "NO\r\n"}, {"input": "AAA\r\n", "output": "NO\r\n"}, {"input": "BBB\r\n", "output": "NO\r\n"}, {"input": "AXBXBXA\r\n", "output": "NO\r\n"}, {"input": "SKDSKDJABSDBADKFJDK\r\n", "output": "YES\r\n"}, {"input": "ABAXXBBXXAA\r\n", "output": "NO\r\n"}, {"input": "ABAB\r\n", "output": "NO\r\n"}, {"input": "BABA\r\n", "output": "NO\r\n"}, {"input": "AAAB\r\n", "output": "NO\r\n"}, {"input": "AAAA\r\n", "output": "NO\r\n"}, {"input": "AABA\r\n", "output": "NO\r\n"}, {"input": "ABAA\r\n", "output": "NO\r\n"}, {"input": "BAAA\r\n", "output": "NO\r\n"}, {"input": "AABB\r\n", "output": "NO\r\n"}, {"input": "BAAB\r\n", "output": "YES\r\n"}, {"input": "BBAA\r\n", "output": "NO\r\n"}, {"input": "BBBA\r\n", "output": "NO\r\n"}, {"input": "BBAB\r\n", "output": "NO\r\n"}, {"input": "BABB\r\n", "output": "NO\r\n"}, {"input": "ABBB\r\n", "output": "NO\r\n"}, {"input": "BBBB\r\n", "output": "NO\r\n"}, {"input": "BABAB\r\n", "output": "YES\r\n"}, {"input": "ABABA\r\n", "output": "YES\r\n"}, {"input": "AAABAAACBBBC\r\n", "output": "NO\r\n"}, {"input": "AABBBACBBBBBBAACBCCACBBAABBBBBCAACABAACABCACCBCBCCCBCBCABCBBCCBCBBAACBACAABACBBCACCBCCACCABBCBABBAAC\r\n", "output": "YES\r\n"}, {"input": "CBBABDDBBADAC\r\n", "output": "YES\r\n"}, {"input": "ABYFAB\r\n", "output": "NO\r\n"}, {"input": "BABXXAB\r\n", "output": "YES\r\n"}, {"input": "ABAXAB\r\n", "output": "YES\r\n"}, {"input": "ABABXAB\r\n", "output": "YES\r\n"}, {"input": "ABXABXABXABXABXBAXBAXBAXBA\r\n", "output": "YES\r\n"}, {"input": "QQQQQQQQQABABQQQQQQQQ\r\n", "output": "NO\r\n"}]
false
stdio
null
true
581/C
581
C
Python 3
TESTS
7
46
0
201076889
n,k=map(int,input().split()) ; arr=list(map(int,input().split())) ; ans=min(n*100 , sum(arr)+k ) ; print(ans//10)
98
109
5,120,000
13267634
n, k = map(int, input().split(' ')[:2]) a = list(map(int, input().split(' ')[:n])) b = [0 for i in range(101)] rating = 0 for i in a: b[i]+=1 rating += i // 10 for last_dig in range(9, 0, -1): for first_dig in range(10): v = first_dig * 10 + last_dig need_for_one = 10 - last_dig can_use = min(b[v], k // need_for_one) k -= can_use * need_for_one b[v] -= can_use b[(first_dig + 1) * 10] += can_use rating += can_use z = k // 10 for first_dig in range(10): v = first_dig * 10 can_use = min(z, b[v]) b[first_dig * 10] -= can_use b[(first_dig + 1)* 10] += can_use z -= can_use rating += can_use print(rating)
Codeforces Round 322 (Div. 2)
CF
2,015
1
256
Developing Skills
Petya loves computer games. Finally a game that he's been waiting for so long came out! The main character of this game has n different skills, each of which is characterized by an integer ai from 0 to 100. The higher the number ai is, the higher is the i-th skill of the character. The total rating of the character is calculated as the sum of the values ​​of $$\left\lfloor \frac{a_i}{10} \right\rfloor$$ for all i from 1 to n. The expression ⌊ x⌋ denotes the result of rounding the number x down to the nearest integer. At the beginning of the game Petya got k improvement units as a bonus that he can use to increase the skills of his character and his total rating. One improvement unit can increase any skill of Petya's character by exactly one. For example, if a4 = 46, after using one imporvement unit to this skill, it becomes equal to 47. A hero's skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused. Your task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units.
The first line of the input contains two positive integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 107) — the number of skills of the character and the number of units of improvements at Petya's disposal. The second line of the input contains a sequence of n integers ai (0 ≤ ai ≤ 100), where ai characterizes the level of the i-th skill of the character.
The first line of the output should contain a single non-negative integer — the maximum total rating of the character that Petya can get using k or less improvement units.
null
In the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating of the character becomes equal to lfloor frac{100}{10} rfloor + lfloor frac{100}{10} rfloor = 10 + 10 = 20. In the second test the optimal strategy for Petya is to improve the first skill to 20 (by spending 3 improvement units) and to improve the third skill to 20 (in this case by spending 1 improvement units). Thus, Petya is left with 4 improvement units and he will be able to increase the second skill to 19 (which does not change the overall rating, so Petya does not necessarily have to do it). Therefore, the highest possible total rating in this example is $$\lfloor \frac { 20 } { 10 } \rfloor + \lfloor \frac { 19 } { 10 } \rfloor + \lfloor \frac { 20 } { 10 } \rfloor = 2 + 1 + 2 = 5$$. In the third test case the optimal strategy for Petya is to increase the first skill to 100 by spending 1 improvement unit. Thereafter, both skills of the character will be equal to 100, so Petya will not be able to spend the remaining improvement unit. So the answer is equal to $$\lfloor \frac { 100 } { 10 } \rfloor + \lfloor \frac { 100 } { 10 } \rfloor = 10 + 10 = 20$$.
[{"input": "2 4\n7 9", "output": "2"}, {"input": "3 8\n17 15 19", "output": "5"}, {"input": "2 2\n99 100", "output": "20"}]
1,400
["implementation", "math", "sortings"]
98
[{"input": "2 4\r\n7 9\r\n", "output": "2\r\n"}, {"input": "3 8\r\n17 15 19\r\n", "output": "5\r\n"}, {"input": "2 2\r\n99 100\r\n", "output": "20\r\n"}, {"input": "100 10000\r\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 0 0 0 0 0 0 0 0 0 0 0\r\n", "output": "1000\r\n"}, {"input": "100 10000\r\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\r\n", "output": "1000\r\n"}, {"input": "1 16\r\n78\r\n", "output": "9\r\n"}, {"input": "2 33\r\n30 88\r\n", "output": "15\r\n"}, {"input": "3 9\r\n93 62 7\r\n", "output": "16\r\n"}, {"input": "5 145\r\n19 77 59 1 63\r\n", "output": "36\r\n"}, {"input": "7 168\r\n2 71 56 58 42 61 39\r\n", "output": "49\r\n"}, {"input": "10 217\r\n48 30 82 70 10 5 34 11 90 90\r\n", "output": "68\r\n"}, {"input": "15 204\r\n19 81 24 22 59 46 48 8 1 66 100 20 46 56 61\r\n", "output": "86\r\n"}, {"input": "20 484\r\n24 72 72 13 85 50 52 3 81 79 71 57 57 75 6 52 54 41 61 73\r\n", "output": "156\r\n"}, {"input": "30 825\r\n33 25 61 69 92 38 2 62 73 78 83 32 25 5 5 82 64 93 38 25 52 9 40 52 38 90 25 85 99 20\r\n", "output": "232\r\n"}, {"input": "40 700\r\n43 35 51 91 44 51 86 20 64 10 50 40 16 25 37 89 18 44 94 99 18 30 11 27 73 3 90 78 28 98 87 43 85 88 29 93 6 81 78 16\r\n", "output": "276\r\n"}, {"input": "50 1607\r\n19 55 52 35 18 39 3 12 55 78 62 83 85 56 36 86 96 28 70 40 40 83 27 2 51 49 87 28 58 75 27 69 36 82 78 29 99 87 29 78 82 78 15 85 52 32 90 6 1 76\r\n", "output": "424\r\n"}, {"input": "60 2213\r\n17 98 74 91 59 84 87 71 13 9 74 48 75 76 36 25 49 80 25 92 41 24 99 45 98 95 27 54 88 63 25 50 19 43 15 90 58 48 58 83 37 88 35 63 63 23 27 82 80 7 82 93 71 18 85 17 13 2 50 74\r\n", "output": "552\r\n"}, {"input": "70 1313\r\n27 7 64 45 44 29 37 63 38 9 85 56 43 74 46 55 59 97 13 33 75 78 2 88 32 7 24 36 86 40 66 42 26 48 64 14 50 21 20 10 50 73 21 29 17 46 97 90 81 73 61 25 95 82 93 94 72 38 80 13 3 3 20 90 34 20 24 49 96 51\r\n", "output": "468\r\n"}, {"input": "40 108\r\n20 100 99 50 8 78 44 67 91 75 93 53 96 81 96 86 81 0 58 9 51 63 70 73 80 79 28 82 4 15 60 74 19 17 54 81 11 67 71 66\r\n", "output": "245\r\n"}, {"input": "50 284\r\n61 25 82 73 57 61 90 22 63 99 58 4 27 54 8 29 46 99 73 73 60 42 45 17 75 86 38 83 4 1 67 44 74 87 32 33 14 95 87 46 40 3 37 6 42 38 51 39 98 48\r\n", "output": "282\r\n"}, {"input": "60 1947\r\n46 29 55 97 37 32 24 22 35 66 24 78 92 5 55 41 21 30 88 24 13 89 77 30 71 15 58 26 39 10 42 36 28 66 21 28 51 55 91 4 94 59 63 46 1 39 46 1 70 7 46 37 96 41 70 19 55 80 59 83\r\n", "output": "471\r\n"}, {"input": "70 2454\r\n88 23 5 86 53 48 60 78 97 90 0 18 57 78 68 28 87 39 70 9 0 35 18 53 67 56 0 71 7 86 39 96 83 45 99 92 43 38 40 63 81 59 89 86 28 62 53 97 53 2 73 93 38 49 51 62 93 3 63 49 47 85 72 98 43 91 7 20 47 66\r\n", "output": "632\r\n"}, {"input": "80 1879\r\n36 27 86 90 18 85 99 54 29 8 64 31 34 26 45 51 13 48 58 6 98 30 74 63 78 53 88 98 15 17 29 67 78 8 2 7 42 26 72 83 5 59 8 7 27 59 34 65 93 71 50 34 63 45 21 81 19 30 99 41 25 11 83 62 17 29 80 61 91 22 19 95 80 73 15 39 10 37 88 42\r\n", "output": "570\r\n"}, {"input": "90 1191\r\n46 37 76 11 60 29 49 13 88 41 65 7 2 13 44 58 23 10 45 48 63 83 79 5 89 99 28 80 34 6 37 92 61 70 51 0 34 67 68 77 62 69 27 86 71 83 72 73 93 92 62 68 86 76 28 24 67 66 61 12 3 52 45 44 58 83 0 84 18 50 75 51 41 25 21 53 39 20 36 45 62 24 12 33 61 81 9 13 27 22\r\n", "output": "554\r\n"}, {"input": "100 1257\r\n80 15 39 54 98 10 65 77 55 98 15 25 78 40 25 16 17 60 25 60 56 29 91 16 14 60 47 31 15 59 83 77 10 54 27 21 50 34 64 69 43 81 32 14 30 93 0 91 75 51 19 84 88 14 30 4 99 59 94 69 24 51 35 99 22 25 41 77 64 97 10 4 56 75 97 54 4 55 29 8 14 16 88 34 80 47 66 30 80 60 45 45 93 85 49 91 37 16 49 56\r\n", "output": "619\r\n"}, {"input": "100 3852\r\n71 34 1 77 97 36 66 78 95 47 47 15 50 100 43 47 20 23 61 92 49 86 29 92 100 85 5 58 59 19 16 81 16 89 93 75 46 86 9 50 9 49 61 88 76 13 14 99 47 64 39 42 63 5 57 8 51 21 21 62 92 84 84 56 9 37 72 19 99 19 8 60 25 21 4 0 98 80 29 63 52 87 91 30 79 79 96 22 32 63 87 73 51 89 81 84 69 30 55 31\r\n", "output": "922\r\n"}, {"input": "100 2533\r\n16 32 22 100 52 10 43 28 87 72 69 84 26 0 74 46 28 34 46 47 90 18 49 6 42 30 18 33 86 38 94 78 8 39 54 46 72 45 83 68 38 4 14 6 86 24 71 36 22 8 37 99 28 7 88 49 4 69 46 81 30 95 92 18 81 21 14 7 43 14 80 59 14 72 93 6 78 43 56 12 66 21 81 80 39 5 54 69 40 12 41 35 23 58 1 75 40 3 36 97\r\n", "output": "706\r\n"}, {"input": "100 2239\r\n95 9 31 56 96 85 88 79 78 63 68 95 1 91 94 56 57 88 30 92 64 52 91 11 17 99 65 63 35 68 82 18 66 57 26 62 32 70 89 98 42 17 68 93 53 79 50 6 30 76 69 10 4 41 18 56 81 49 14 10 91 6 32 80 85 94 2 95 66 9 18 58 71 23 23 48 68 72 39 51 0 23 71 73 10 89 13 15 16 30 27 44 63 93 22 77 12 12 28 5\r\n", "output": "737\r\n"}, {"input": "100 1689\r\n40 18 85 79 18 70 44 62 37 21 68 6 9 60 13 55 98 98 82 80 4 75 44 83 60 44 10 60 28 65 59 82 48 41 20 100 57 62 28 60 3 5 54 91 31 89 6 44 38 20 34 90 14 99 82 96 57 97 39 73 30 96 41 42 56 33 45 83 78 15 79 25 27 7 43 54 14 90 22 68 3 1 27 88 49 37 84 61 92 37 14 41 81 62 10 36 73 86 9 4\r\n", "output": "666\r\n"}, {"input": "1 44\r\n56\r\n", "output": "10\r\n"}, {"input": "5 136\r\n65 53 80 92 74\r\n", "output": "50\r\n"}, {"input": "20 964\r\n70 82 81 14 73 35 40 21 73 70 71 35 32 43 26 51 51 62 45 61\r\n", "output": "200\r\n"}, {"input": "80 4124\r\n14 37 6 11 63 59 43 72 88 0 53 43 42 95 65 61 9 69 9 95 49 64 27 34 53 31 34 26 30 48 85 97 35 60 74 45 35 86 11 34 45 72 95 95 95 13 58 2 0 38 37 13 61 47 85 77 96 10 34 3 54 55 91 23 57 13 33 16 2 17 80 61 36 57 79 81 90 33 82 48\r\n", "output": "800\r\n"}, {"input": "100 4899\r\n66 100 11 81 19 55 96 14 66 10 49 75 1 58 64 80 47 95 45 79 36 89 31 30 61 96 93 86 50 61 64 32 82 13 57 75 5 46 96 49 3 98 34 6 91 7 50 62 46 31 100 4 2 16 20 47 86 41 73 17 43 71 84 47 18 100 55 23 10 37 4 19 84 61 27 61 42 29 95 41 93 5 72 58 24 10 80 45 78 68 19 18 30 28 95 91 15 90 87 47\r\n", "output": "1000\r\n"}, {"input": "1 7035769\r\n1\r\n", "output": "10\r\n"}, {"input": "5 5012340\r\n10 63 89 25 29\r\n", "output": "50\r\n"}, {"input": "20 5527187\r\n15 91 34 37 16 77 85 4 31 28 2 47 8 45 57 51 58 72 97 16\r\n", "output": "200\r\n"}, {"input": "80 8000114\r\n27 46 16 80 85 11 20 22 80 24 85 22 17 86 96 60 16 12 94 39 23 86 12 49 28 78 80 23 92 78 62 38 27 43 35 62 60 89 85 63 39 27 70 13 73 91 82 73 98 83 70 93 5 37 15 85 39 58 92 34 93 44 31 86 28 86 43 3 25 12 18 61 25 7 67 87 37 29 65 98\r\n", "output": "800\r\n"}, {"input": "100 9455943\r\n44 8 21 71 7 29 40 65 91 70 48 19 77 48 16 22 54 4 29 34 9 22 73 34 47 41 5 83 32 91 52 6 74 64 18 23 9 4 36 78 98 20 20 3 69 86 41 67 54 76 87 84 47 6 52 87 61 100 98 80 14 14 24 99 90 73 97 79 22 65 65 51 29 44 15 67 21 58 79 80 96 40 63 73 96 59 72 24 87 85 74 49 81 30 16 61 87 30 0 13\r\n", "output": "1000\r\n"}, {"input": "1 6\r\n27\r\n", "output": "3\r\n"}, {"input": "1 10\r\n88\r\n", "output": "9\r\n"}, {"input": "1 3\r\n93\r\n", "output": "9\r\n"}, {"input": "2 22\r\n48 44\r\n", "output": "11\r\n"}, {"input": "1 48\r\n52\r\n", "output": "10\r\n"}, {"input": "1 2135072\r\n72\r\n", "output": "10\r\n"}, {"input": "3 5\r\n2 3 1\r\n", "output": "0\r\n"}, {"input": "5 1\r\n8 8 8 8 8\r\n", "output": "0\r\n"}, {"input": "3 150\r\n1 1 1\r\n", "output": "15\r\n"}, {"input": "4 3\r\n7 9 9 9\r\n", "output": "3\r\n"}, {"input": "1 10\r\n100\r\n", "output": "10\r\n"}, {"input": "3 11\r\n100 100 100\r\n", "output": "30\r\n"}, {"input": "2 11\r\n99 100\r\n", "output": "20\r\n"}]
false
stdio
null
true
413/C
413
C
Python 3
TESTS
32
124
0
48322600
n, m = input().split() znac=input().split() nay=input().split() ayc=[] summ=0 obi = [] for i in range(int(n)): if str(i+1) in nay: ayc.append(znac[i]) else: obi.append(znac[i]) nayk=reversed(sorted(ayc)) for i in obi: summ+=int(i) for i in nayk: if summ<int(i): summ+=int(i) else: summ+=summ print(summ)
67
46
0
205670441
n, m = map(int, input().split()) a = list(map(int, input().split())) b = [a[i - 1] for i in map(int, input().split())] b.sort(reverse = True) s = sum(a) - sum(b) for i in b: s += s if i < s else i print(s)
Coder-Strike 2014 - Round 2
CF
2,014
1
256
Jeopardy!
'Jeopardy!' is an intellectual game where players answer questions and earn points. Company Q conducts a simplified 'Jeopardy!' tournament among the best IT companies. By a lucky coincidence, the old rivals made it to the finals: company R1 and company R2. The finals will have n questions, m of them are auction questions and n - m of them are regular questions. Each question has a price. The price of the i-th question is ai points. During the game the players chose the questions. At that, if the question is an auction, then the player who chose it can change the price if the number of his current points is strictly larger than the price of the question. The new price of the question cannot be less than the original price and cannot be greater than the current number of points of the player who chose the question. The correct answer brings the player the points equal to the price of the question. The wrong answer to the question reduces the number of the player's points by the value of the question price. The game will go as follows. First, the R2 company selects a question, then the questions are chosen by the one who answered the previous question correctly. If no one answered the question, then the person who chose last chooses again. All R2 employees support their team. They want to calculate what maximum possible number of points the R2 team can get if luck is on their side during the whole game (they will always be the first to correctly answer questions). Perhaps you are not going to be surprised, but this problem was again entrusted for you to solve.
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100; m ≤ min(n, 30)) — the total number of questions and the number of auction questions, correspondingly. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 107) — the prices of the questions. The third line contains m distinct integers bi (1 ≤ bi ≤ n) — the numbers of auction questions. Assume that the questions are numbered from 1 to n.
In the single line, print the answer to the problem — the maximum points the R2 company can get if it plays optimally well. It is guaranteed that the answer fits into the integer 64-bit signed type.
null
null
[{"input": "4 1\n1 3 7 5\n3", "output": "18"}, {"input": "3 2\n10 3 8\n2 3", "output": "40"}, {"input": "2 2\n100 200\n1 2", "output": "400"}]
1,400
["greedy", "math"]
67
[{"input": "4 1\r\n1 3 7 5\r\n3\r\n", "output": "18\r\n"}, {"input": "3 2\r\n10 3 8\r\n2 3\r\n", "output": "40\r\n"}, {"input": "2 2\r\n100 200\r\n1 2\r\n", "output": "400\r\n"}, {"input": "1 1\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "2 2\r\n1 5\r\n1 2\r\n", "output": "10\r\n"}, {"input": "5 3\r\n5 8 7 1 9\r\n2 5 3\r\n", "output": "60\r\n"}, {"input": "5 5\r\n9 1 6 2 1\r\n3 1 4 5 2\r\n", "output": "144\r\n"}, {"input": "25 5\r\n66 41 91 33 86 67 38 79 49 7 77 54 29 19 22 48 63 37 11 100 8 6 47 27 26\r\n12 14 1 23 18\r\n", "output": "29056\r\n"}, {"input": "50 10\r\n19098 20847 65754 94580 54808 57092 23130 15638 43645 52323 52822 65193 90139 69196 83680 70109 96772 35102 56685 6692 30738 74558 57144 24054 44447 51959 22847 18735 23534 821 5540 39948 7552 72425 23213 2770 98496 81096 84868 167 36408 26572 19351 82775 23225 35377 63193 58352 45111 60889\r\n8 20 32 17 11 44 39 30 36 16\r\n", "output": "1880325120\r\n"}, {"input": "2 1\r\n19 4\r\n1\r\n", "output": "23\r\n"}, {"input": "3 1\r\n65 81 6\r\n2\r\n", "output": "152\r\n"}, {"input": "5 1\r\n72 32 17 46 82\r\n2\r\n", "output": "434\r\n"}, {"input": "100 1\r\n9 9 72 55 14 8 55 58 35 67 3 18 73 92 41 49 15 60 18 66 9 26 97 47 43 88 71 97 19 34 48 96 79 53 8 24 69 49 12 23 77 12 21 88 66 9 29 13 61 69 54 77 41 13 4 68 37 74 7 6 29 76 55 72 89 4 78 27 29 82 18 83 12 4 32 69 89 85 66 13 92 54 38 5 26 56 17 55 29 4 17 39 29 94 3 67 85 98 21 14\r\n13\r\n", "output": "8834\r\n"}, {"input": "25 24\r\n1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 1\r\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24\r\n", "output": "70368752566272\r\n"}, {"input": "30 30\r\n6074511 9621540 9853685 9073323 6897794 9366449 1648254 3848902 8729661 9330982 9970280 1886362 5605123 3406494 501290 3140164 2406173 346072 1520895 441795 5271130 7576116 337766 6666108 953354 5085881 2876195 8036150 1251715 4952594\r\n30 5 10 28 21 18 6 13 29 23 17 24 14 25 3 27 20 26 12 2 4 11 16 15 22 7 8 19 1 9\r\n", "output": "5352753316495360\r\n"}, {"input": "50 30\r\n6015200 8643865 4116771 6555197 304415 8580071 8414182 3089212 5684567 7595481 1272699 7127763 3309618 1410297 4349070 2027355 136702 6863504 1800751 5585842 5924142 5188269 4805201 9313209 8941399 5137060 4983630 8467668 1646260 7804684 8646497 7067118 6896291 9109696 6197162 1366002 1703718 3852639 8427694 552915 5001315 5238093 9152085 7288325 8115109 3800240 5658858 4392321 8244056 3275379\r\n30 25 34 8 31 50 48 19 49 26 9 24 22 6 44 14 27 43 3 28 35 10 21 17 45 12 40 47 1 33\r\n", "output": "96888048737845248\r\n"}, {"input": "1 1\r\n1846236\r\n1\r\n", "output": "1846236\r\n"}, {"input": "2 1\r\n8912260 7309391\r\n1\r\n", "output": "16221651\r\n"}, {"input": "3 1\r\n9949628 37460 9989934\r\n3\r\n", "output": "19977022\r\n"}, {"input": "5 3\r\n1819638 2087365 162773 9531053 130978\r\n3 1 4\r\n", "output": "46997584\r\n"}, {"input": "10 4\r\n886062 1016649 67899 9999839 98342 64636 816991 263 1050987 1858\r\n1 9 7 4\r\n", "output": "89995888\r\n"}, {"input": "10 10\r\n1 652210 1 1 1 1 1 1 1 1\r\n10 1 6 7 9 8 4 3 5 2\r\n", "output": "333931520\r\n"}, {"input": "50 5\r\n223036 65304 301127 8945 10289 15638 260 246 68 14 23 6 3 2 8 2 1 392212 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 242747 1 1 1 243737 1 1 1 1 1 1 10000000 572890\r\n18 50 38 42 49\r\n", "output": "170000000\r\n"}, {"input": "50 10\r\n1103 17631 1582 250 6933 26 14434 6 2 1 1 1 1 1 3625 1 5909 1 1 1 1 1 1 1 1 1 1 1 1 7196 14842 1 1 1 1 1 1 12053 9999991 1 10502 1 1 1 1 1 1 1 1 1\r\n41 15 17 1 5 31 7 38 30 39\r\n", "output": "5129995264\r\n"}, {"input": "50 15\r\n369 139 49 15 4 5 1 1 1 365 1 1 1 1 484 165 105 1 1 1 382 105 1 1 1 72 1 1 91 96 1 1 1 1 1 133 9997031 1 1 31 1 1 1 291 558 1 1 1 464 1\r\n49 26 40 21 45 30 16 10 15 44 22 29 36 17 37\r\n", "output": "163801350144\r\n"}, {"input": "50 18\r\n20 23 54 4 1 1 15 50 56 1 1 71 1 1 1 1 1 15 8 1 12 1 1 1 1 1 76 1 19 11 55 42 1 1 1 1 1 9 1 30 5 1 1 1 20 1 1 1 1 9975634\r\n9 18 7 45 27 32 12 41 31 8 3 30 21 19 40 38 29 50\r\n", "output": "1307536261120\r\n"}, {"input": "100 1\r\n954110 7577191 694644 113513 467690 71415 25351 26000 37902 29150 2015 94 741 20 71 9 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 10000000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n78\r\n", "output": "20000000\r\n"}, {"input": "100 5\r\n502646 93620 4203 12132 2444 9620 6 201 4 20 10000000 1 6 9 472804 2 2 5 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 276285 518136 1 1 1 1 1 1 1 1 1 1 1 1 1 1 189005 1 1 1 1 1 1 1 1 1 1 1 1\r\n73 72 15 88 11\r\n", "output": "170000000\r\n"}, {"input": "100 10\r\n9999984 1396 8798 4760 3138 310 840 41 37 79 45 1 7 2 1 1 1 1 11032 1 1 1 11226 1 1 1 1 1 1 1 12147 1 1 1 1 1 1 16512 1 1 1 1 1 1 1 1 1 1 1 2658 1 1 1 1 7562 1 1 1 1 6207 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3334 1 1 1 1 1 1 1310 1 1 1 1 1 1 1 1 1\r\n19 55 91 50 31 23 60 84 38 1\r\n", "output": "5129991680\r\n"}, {"input": "100 15\r\n380 122 2 18 5 2 3 242 1 1 1 1 1 64 1 1 1 1 1 198 323 284 1 419 1 225 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 584 1 1 1 55 9999036 1 1 1 1 1 1 1 1 447 1 1 471 1 1 1 1 1 1 1 374 1 1 1 1 1 1 1 1 1 1 1 273 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 68 1\r\n22 45 49 24 26 62 70 82 21 20 59 14 99 8 50\r\n", "output": "163834200064\r\n"}, {"input": "100 16\r\n15 18 54 132 138 1 1 45 164 1 1 1 1 1 1 1 1 1 1 1 1 9999567 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 188 1 1 1 213 1 1 27 1 1 1 1 1 1 1 1 1 1 1 200 1 140 221 1 1 1 1 1 1 1 1 132 1 78 1 1 1 1 1 243 1 1 1 1 1 1 1 1 1 1 240 1 1 290 1 34 1 1 1 1 1 1\r\n92 46 8 58 94 39 9 89 61 60 4 70 78 72 43 22\r\n", "output": "327675805696\r\n"}, {"input": "3 1\r\n1 2 4\r\n1\r\n", "output": "12\r\n"}, {"input": "3 1\r\n1 2 4\r\n2\r\n", "output": "10\r\n"}, {"input": "3 1\r\n1 2 4\r\n3\r\n", "output": "7\r\n"}, {"input": "2 1\r\n1 2\r\n1\r\n", "output": "4\r\n"}, {"input": "2 1\r\n1 2\r\n2\r\n", "output": "3\r\n"}, {"input": "3 2\r\n1 2 4\r\n1 2\r\n", "output": "16\r\n"}, {"input": "3 2\r\n1 2 4\r\n3 2\r\n", "output": "10\r\n"}, {"input": "3 2\r\n1 2 4\r\n3 1\r\n", "output": "12\r\n"}, {"input": "3 3\r\n4 2 1\r\n1 3 2\r\n", "output": "16\r\n"}, {"input": "5 4\r\n1 2 2 4 8\r\n1 2 4 5\r\n", "output": "80\r\n"}, {"input": "3 2\r\n10 7 1000\r\n2 3\r\n", "output": "2020\r\n"}, {"input": "4 2\r\n2 2 4 8\r\n3 4\r\n", "output": "24\r\n"}, {"input": "3 2\r\n1 3 5\r\n1 3\r\n", "output": "16\r\n"}, {"input": "3 2\r\n10 1 12\r\n2 3\r\n", "output": "44\r\n"}, {"input": "4 2\r\n1 2 3 100\r\n2 4\r\n", "output": "208\r\n"}, {"input": "3 2\r\n10 5 200\r\n2 3\r\n", "output": "420\r\n"}, {"input": "3 2\r\n3 5 3\r\n2 3\r\n", "output": "16\r\n"}, {"input": "3 2\r\n5 4 100\r\n2 3\r\n", "output": "210\r\n"}, {"input": "5 4\r\n100 200 300 400 500\r\n1 2 3 5\r\n", "output": "7200\r\n"}, {"input": "3 2\r\n100 200 180\r\n1 2\r\n", "output": "760\r\n"}, {"input": "4 3\r\n2 5 17 4\r\n1 2 3\r\n", "output": "84\r\n"}, {"input": "5 2\r\n2 2 4 7 15\r\n4 5\r\n", "output": "46\r\n"}, {"input": "3 2\r\n200 100 1000\r\n2 3\r\n", "output": "2400\r\n"}, {"input": "4 2\r\n2 2 2 7\r\n1 4\r\n", "output": "22\r\n"}, {"input": "8 4\r\n2 2 2 2 1 2 3 9\r\n5 6 7 8\r\n", "output": "136\r\n"}, {"input": "3 2\r\n2 1 5\r\n2 3\r\n", "output": "14\r\n"}]
false
stdio
null
true
638/B
638
B
Python 3
PRETESTS
9
62
5,120,000
16843600
from sys import exit n = int(input()) a = [] for i in range(n): s = input() for j in range(len(a)): check = a[j] if s in check: break if check in s: a[j] = s break else: a.append(s) br_flag = False while True: for j in range(len(a)): answer = a[j] for i in a: if j == a.index(i): continue if answer[-1] in i: a[j] = answer[:len(answer) - 1] + i[i.index(answer[-1]):] a.remove(i) br_flag = True break if br_flag: br_flag = False break for i in a: if j == a.index(i): continue if answer[0] in i: a[j] = i[:i.index(answer[0])] + answer[:len(answer)] a.remove(i) br_flag = True break if br_flag: br_flag = False break st = ''.join(a) for i in st: if st.count(i) == 1: continue else: break else: break print(''.join(a))
67
62
4,915,200
16840651
parts = [] empty = True def chrx(x): return chr(x+97) def ordx(x): return ord(x)-97 include = [ [False for _2 in range(26)] for _1 in range(100)] exist = [False]*100 #""" n = int(input()) for _ in range(n): parts.append(input()) exist[_] = True for c in parts[-1]: include[_][ordx(c)] = True #""" #print(parts) #for _ in include[:10]: print(_) def to_str(ar): res = '' for c in ar: res += c return res.replace('X', '') def mergeparts(first, second): #print('call merge:', first, second) pos = [None]*26 for c in range(len(first)): pos[ordx(first[c])] = c for c in range(len(second)): if not pos[ordx(second[c])] is None: # con point found conpoint = c convalue = pos[ordx(second[c])] # print(c, convalue) for i in range(len(second)): pos[ordx(second[i])] = convalue+i-conpoint break res = ['X']*100 for c in range(len(pos)): if not pos[c] is None: res[pos[c]+40] = chrx(c) return to_str(res) def merge_include(first, second): global include for c in range(26): if include[first][c] or include[second][c]: include[first][c], include[second][c] = True, True for c in range(26): #print('ex:', exist[:n]) #print('testing: ', chrx(c)) includin_parts = [] numbers = [] #print('ex:', exist) for part in range(len(parts)): if exist[part] and include[part][c]: includin_parts.append(parts[part]) numbers.append(part) #print('in_p:', includin_parts) #print('num:', numbers) if len(includin_parts) < 2: continue for part in includin_parts[1:]: includin_parts[0] = mergeparts(part, includin_parts[0]) parts[numbers[0]] = includin_parts[0] #print(numbers) for num in numbers[1:]: #print('DESTROY -', num) exist[num] = False merge_include(numbers[0], num) #print('include:') #for _2 in include[:10]: print(_2) # result for _ in range(len(exist)): if exist[_]: print(parts[_], end='')
VK Cup 2016 - Qualification Round 2
CF
2,016
1
256
Making Genome in Berland
Berland scientists face a very important task - given the parts of short DNA fragments, restore the dinosaur DNA! The genome of a berland dinosaur has noting in common with the genome that we've used to: it can have 26 distinct nucleotide types, a nucleotide of each type can occur at most once. If we assign distinct English letters to all nucleotides, then the genome of a Berland dinosaur will represent a non-empty string consisting of small English letters, such that each letter occurs in it at most once. Scientists have n genome fragments that are represented as substrings (non-empty sequences of consecutive nucleotides) of the sought genome. You face the following problem: help scientists restore the dinosaur genome. It is guaranteed that the input is not contradictory and at least one suitable line always exists. When the scientists found out that you are a strong programmer, they asked you in addition to choose the one with the minimum length. If there are multiple such strings, choose any string.
The first line of the input contains a positive integer n (1 ≤ n ≤ 100) — the number of genome fragments. Each of the next lines contains one descriptions of a fragment. Each fragment is a non-empty string consisting of distinct small letters of the English alphabet. It is not guaranteed that the given fragments are distinct. Fragments could arbitrarily overlap and one fragment could be a substring of another one. It is guaranteed that there is such string of distinct letters that contains all the given fragments as substrings.
In the single line of the output print the genome of the minimum length that contains all the given parts. All the nucleotides in the genome must be distinct. If there are multiple suitable strings, print the string of the minimum length. If there also are multiple suitable strings, you can print any of them.
null
null
[{"input": "3\nbcd\nab\ncdef", "output": "abcdef"}, {"input": "4\nx\ny\nz\nw", "output": "xyzw"}]
1,500
["*special", "dfs and similar", "strings"]
67
[{"input": "3\r\nbcd\r\nab\r\ncdef\r\n", "output": "abcdef\r\n"}, {"input": "4\r\nx\r\ny\r\nz\r\nw\r\n", "output": "xyzw\r\n"}, {"input": "25\r\nef\r\nfg\r\ngh\r\nhi\r\nij\r\njk\r\nkl\r\nlm\r\nmn\r\nno\r\nab\r\nbc\r\ncd\r\nde\r\nop\r\npq\r\nqr\r\nrs\r\nst\r\ntu\r\nuv\r\nvw\r\nwx\r\nxy\r\nyz\r\n", "output": "abcdefghijklmnopqrstuvwxyz\r\n"}, {"input": "1\r\nf\r\n", "output": "f\r\n"}, {"input": "1\r\nqwertyuiopzxcvbnmasdfghjkl\r\n", "output": "qwertyuiopzxcvbnmasdfghjkl\r\n"}, {"input": "3\r\ndfghj\r\nghjkl\r\nasdfg\r\n", "output": "asdfghjkl\r\n"}, {"input": "4\r\nab\r\nab\r\nab\r\nabc\r\n", "output": "abc\r\n"}, {"input": "3\r\nf\r\nn\r\nux\r\n", "output": "uxfn\r\n"}, {"input": "2\r\nfgs\r\nfgs\r\n", "output": "fgs\r\n"}, {"input": "96\r\nc\r\ndhf\r\no\r\nq\r\nry\r\nh\r\nr\r\nf\r\nji\r\nek\r\ndhf\r\np\r\nk\r\no\r\nf\r\nw\r\nc\r\nc\r\nfgw\r\nbps\r\nhfg\r\np\r\ni\r\nji\r\nto\r\nc\r\nou\r\ny\r\nfg\r\na\r\ne\r\nu\r\nc\r\ny\r\nhf\r\nqn\r\nu\r\nj\r\np\r\ns\r\no\r\nmr\r\na\r\nqn\r\nb\r\nlb\r\nn\r\nji\r\nji\r\na\r\no\r\nat\r\ns\r\nf\r\nb\r\ndh\r\nk\r\nl\r\nl\r\nvq\r\nt\r\nb\r\nc\r\nv\r\nc\r\nh\r\nh\r\ny\r\nh\r\nq\r\ne\r\nx\r\nd\r\no\r\nq\r\nm\r\num\r\nmr\r\nfg\r\ni\r\nl\r\na\r\nh\r\nt\r\num\r\nr\r\no\r\nn\r\nk\r\ne\r\nji\r\na\r\nc\r\nh\r\ne\r\nm\r\n", "output": "atoumrydhfgwekjilbpsvqncx\r\n"}, {"input": "3\r\npbi\r\nopbi\r\ngh\r\n", "output": "ghopbi\r\n"}, {"input": "4\r\ng\r\np\r\no\r\nop\r\n", "output": "opg\r\n"}, {"input": "5\r\np\r\nf\r\nu\r\nf\r\np\r\n", "output": "pfu\r\n"}, {"input": "4\r\nr\r\nko\r\nuz\r\nko\r\n", "output": "kouzr\r\n"}, {"input": "5\r\nzt\r\nted\r\nlzt\r\nted\r\ndyv\r\n", "output": "lztedyv\r\n"}, {"input": "6\r\ngul\r\ng\r\njrb\r\nul\r\nd\r\njr\r\n", "output": "guljrbd\r\n"}, {"input": "5\r\nlkyh\r\naim\r\nkyh\r\nm\r\nkyhai\r\n", "output": "lkyhaim\r\n"}, {"input": "4\r\nzrncsywd\r\nsywdx\r\ngqzrn\r\nqzrncsy\r\n", "output": "gqzrncsywdx\r\n"}, {"input": "5\r\ntbxzc\r\njrdtb\r\njrdtb\r\nflnj\r\nrdtbx\r\n", "output": "flnjrdtbxzc\r\n"}, {"input": "10\r\ng\r\nkagijn\r\nzxt\r\nhmkag\r\nhm\r\njnc\r\nxtqupw\r\npwhmk\r\ng\r\nagi\r\n", "output": "zxtqupwhmkagijnc\r\n"}, {"input": "20\r\nf\r\nf\r\nv\r\nbn\r\ne\r\nmr\r\ne\r\ne\r\nn\r\nj\r\nqfv\r\ne\r\ndpb\r\nj\r\nlc\r\nr\r\ndp\r\nf\r\na\r\nrt\r\n", "output": "dpbnlcmrtqfveja\r\n"}, {"input": "30\r\nxlo\r\nwx\r\ne\r\nf\r\nyt\r\nw\r\ne\r\nl\r\nxl\r\nojg\r\njg\r\niy\r\ngkz\r\ne\r\nw\r\nloj\r\ng\r\nfw\r\nl\r\nlo\r\nbe\r\ne\r\ngk\r\niyt\r\no\r\nb\r\nqv\r\nz\r\nb\r\nzq\r\n", "output": "befwxlojgkzqviyt\r\n"}, {"input": "50\r\nmd\r\nei\r\nhy\r\naz\r\nzr\r\nmd\r\nv\r\nz\r\nke\r\ny\r\nuk\r\nf\r\nhy\r\njm\r\nke\r\njm\r\ncn\r\nwf\r\nzr\r\nqj\r\ng\r\nzr\r\ndv\r\ni\r\ndv\r\nuk\r\nj\r\nwf\r\njm\r\nn\r\na\r\nqj\r\nei\r\nf\r\nzr\r\naz\r\naz\r\nke\r\na\r\nr\r\ndv\r\nei\r\nzr\r\ndv\r\nq\r\ncn\r\nyg\r\nqj\r\nnh\r\nhy\r\n", "output": "azrcnhygqjmdvukeiwf\r\n"}, {"input": "80\r\ni\r\nioh\r\nquc\r\nexioh\r\niohb\r\nex\r\nrwky\r\nz\r\nquc\r\nrw\r\nplnt\r\nq\r\nhbrwk\r\nexioh\r\ntv\r\nxioh\r\nlnt\r\nxi\r\nn\r\npln\r\niohbr\r\nwky\r\nhbr\r\nw\r\nyq\r\nrwky\r\nbrw\r\nplnt\r\nv\r\nkyq\r\nrwkyq\r\nt\r\nhb\r\ngplnt\r\np\r\nkyqu\r\nhbr\r\nrwkyq\r\nhbr\r\nve\r\nhbrwk\r\nkyq\r\nkyquc\r\ngpln\r\ni\r\nbr\r\ntvex\r\nwkyqu\r\nz\r\nlnt\r\ngp\r\nky\r\ngplnt\r\ne\r\nhbrwk\r\nbrw\r\nve\r\no\r\nplnt\r\nn\r\nntve\r\ny\r\nln\r\npln\r\ntvexi\r\nr\r\nzgp\r\nxiohb\r\nl\r\nn\r\nt\r\nplnt\r\nlntv\r\nexi\r\nexi\r\ngpl\r\nioh\r\nk\r\nwk\r\ni\r\n", "output": "zgplntvexiohbrwkyquc\r\n"}, {"input": "70\r\njp\r\nz\r\nz\r\nd\r\ndy\r\nk\r\nsn\r\nrg\r\nz\r\nsn\r\nh\r\nj\r\ns\r\nkx\r\npu\r\nkx\r\nm\r\njp\r\nbo\r\nm\r\ntk\r\ndy\r\no\r\nm\r\nsn\r\nv\r\nrg\r\nv\r\nn\r\no\r\ngh\r\np\r\no\r\nx\r\nq\r\nzv\r\nr\r\nbo\r\ng\r\noz\r\nu\r\nub\r\nnd\r\nh\r\ny\r\njp\r\no\r\nq\r\nbo\r\nhq\r\nhq\r\nkx\r\nx\r\ndy\r\nn\r\nb\r\nub\r\nsn\r\np\r\nub\r\ntk\r\nu\r\nnd\r\nvw\r\nt\r\nub\r\nbo\r\nyr\r\nyr\r\nub\r\n", "output": "jpubozvwsndyrghqtkxm\r\n"}, {"input": "100\r\nm\r\nj\r\nj\r\nf\r\nk\r\nq\r\ni\r\nu\r\ni\r\nl\r\nt\r\nt\r\no\r\nv\r\nk\r\nw\r\nr\r\nj\r\nh\r\nx\r\nc\r\nv\r\nu\r\nf\r\nh\r\nj\r\nb\r\ne\r\ni\r\nr\r\ng\r\nb\r\nl\r\nb\r\ng\r\nb\r\nf\r\nq\r\nv\r\na\r\nu\r\nn\r\ni\r\nl\r\nk\r\nc\r\nx\r\nu\r\nr\r\ne\r\ni\r\na\r\nc\r\no\r\nc\r\na\r\nx\r\nd\r\nf\r\nx\r\no\r\nx\r\nm\r\nl\r\nr\r\nc\r\nr\r\nc\r\nv\r\nj\r\ng\r\nu\r\nn\r\nn\r\nd\r\nl\r\nl\r\nc\r\ng\r\nu\r\nr\r\nu\r\nh\r\nl\r\na\r\nl\r\nr\r\nt\r\nm\r\nf\r\nm\r\nc\r\nh\r\nl\r\nd\r\na\r\nr\r\nh\r\nn\r\nc\r\n", "output": "mjfkqiultovwrhxcbegand\r\n"}, {"input": "99\r\nia\r\nz\r\nsb\r\ne\r\nnm\r\nd\r\nknm\r\nt\r\nm\r\np\r\nqvu\r\ne\r\nq\r\nq\r\ns\r\nmd\r\nz\r\nfh\r\ne\r\nwi\r\nn\r\nsb\r\nq\r\nw\r\ni\r\ng\r\nr\r\ndf\r\nwi\r\nl\r\np\r\nm\r\nb\r\ni\r\natj\r\nb\r\nwia\r\nx\r\nnm\r\nlk\r\nx\r\nfh\r\nh\r\np\r\nf\r\nzr\r\nz\r\nr\r\nsbz\r\nlkn\r\nsbz\r\nz\r\na\r\nwia\r\ntjx\r\nk\r\nj\r\nx\r\nl\r\nqvu\r\nzr\r\nfh\r\nbzrg\r\nz\r\nplk\r\nfhe\r\nn\r\njxqv\r\nrgp\r\ne\r\ndf\r\nz\r\ns\r\natj\r\ndf\r\nat\r\ngp\r\nw\r\new\r\nt\r\np\r\np\r\nfhe\r\nq\r\nxq\r\nt\r\nzr\r\nat\r\ndfh\r\nj\r\ns\r\nu\r\npl\r\np\r\nrg\r\nlk\r\nq\r\nwia\r\ng\r\n", "output": "sbzrgplknmdfhewiatjxqvu\r\n"}, {"input": "95\r\np\r\nk\r\nd\r\nr\r\nn\r\nz\r\nn\r\nb\r\np\r\nw\r\ni\r\nn\r\ny\r\ni\r\nn\r\nn\r\ne\r\nr\r\nu\r\nr\r\nb\r\ni\r\ne\r\np\r\nk\r\nc\r\nc\r\nh\r\np\r\nk\r\nh\r\ns\r\ne\r\ny\r\nq\r\nq\r\nx\r\nw\r\nh\r\ng\r\nt\r\nt\r\na\r\nt\r\nh\r\ni\r\nb\r\ne\r\np\r\nr\r\nu\r\nn\r\nn\r\nr\r\nq\r\nn\r\nu\r\ng\r\nw\r\nt\r\np\r\nt\r\nk\r\nd\r\nz\r\nh\r\nf\r\nd\r\ni\r\na\r\na\r\nf\r\ne\r\na\r\np\r\ns\r\nk\r\nt\r\ng\r\nf\r\ni\r\ng\r\ng\r\nt\r\nn\r\nn\r\nt\r\nt\r\nr\r\nx\r\na\r\nz\r\nc\r\nn\r\nk\r\n", "output": "pkdrnzbwiyeuchsqxgtaf\r\n"}, {"input": "3\r\nh\r\nx\r\np\r\n", "output": "hxp\r\n"}, {"input": "4\r\nrz\r\nvu\r\nxy\r\npg\r\n", "output": "pgrzvuxy\r\n"}, {"input": "5\r\ndrw\r\nu\r\nzq\r\npd\r\naip\r\n", "output": "aipdrwzqu\r\n"}, {"input": "70\r\ne\r\no\r\ng\r\ns\r\nsz\r\nyl\r\ns\r\nn\r\no\r\nq\r\np\r\nl\r\noa\r\ndq\r\ny\r\np\r\nn\r\nio\r\ng\r\nb\r\nk\r\nv\r\ny\r\nje\r\nc\r\ncb\r\nfx\r\ncbv\r\nfxp\r\nkt\r\nhm\r\nz\r\nrcb\r\np\r\nt\r\nu\r\nzh\r\ne\r\nb\r\na\r\nyl\r\nd\r\nv\r\nl\r\nrc\r\nq\r\nt\r\nt\r\nj\r\nl\r\nr\r\ny\r\nlg\r\np\r\nt\r\nd\r\nq\r\nje\r\nqwu\r\ng\r\nz\r\ngi\r\ndqw\r\nz\r\nvyl\r\nk\r\nt\r\nc\r\nb\r\nrc\r\n", "output": "dqwufxpjektrcbvylgioaszhmn\r\n"}, {"input": "3\r\ne\r\nw\r\nox\r\n", "output": "oxew\r\n"}, {"input": "100\r\npr\r\nfz\r\nru\r\ntk\r\nld\r\nvq\r\nef\r\ngj\r\ncp\r\nbm\r\nsn\r\nld\r\nua\r\nzl\r\ndw\r\nef\r\nua\r\nbm\r\nxb\r\nvq\r\nav\r\ncp\r\nko\r\nwc\r\nru\r\ni\r\ne\r\nav\r\nbm\r\nav\r\nxb\r\nog\r\ng\r\nme\r\ntk\r\nog\r\nxb\r\nef\r\ntk\r\nhx\r\nqt\r\nvq\r\ndw\r\nv\r\nxb\r\ndw\r\nko\r\nd\r\nbm\r\nua\r\nvq\r\nis\r\nwc\r\ntk\r\ntk\r\ngj\r\ng\r\ngj\r\nef\r\nqt\r\nvq\r\nbm\r\nog\r\nvq\r\ngj\r\nvq\r\nzl\r\ngj\r\nji\r\nvq\r\nhx\r\ng\r\nbm\r\nji\r\nqt\r\nef\r\nav\r\ntk\r\nxb\r\nru\r\nko\r\nny\r\nis\r\ncp\r\nxb\r\nog\r\nru\r\nhx\r\nwc\r\nko\r\nu\r\nfz\r\ndw\r\nji\r\nzl\r\nvq\r\nqt\r\nko\r\ngj\r\nis\r\n", "output": "hxbmefzldwcpruavqtkogjisny\r\n"}, {"input": "23\r\nw\r\nz\r\nk\r\nc\r\ne\r\np\r\nt\r\na\r\nx\r\nc\r\nq\r\nx\r\na\r\nf\r\np\r\nw\r\nh\r\nx\r\nf\r\nw\r\np\r\nw\r\nq\r\n", "output": "wzkceptaxqfh\r\n"}, {"input": "12\r\nu\r\na\r\nhw\r\na\r\ngh\r\nog\r\nr\r\nd\r\nw\r\nk\r\nl\r\ny\r\n", "output": "oghwuardkly\r\n"}, {"input": "2\r\ny\r\nd\r\n", "output": "yd\r\n"}, {"input": "1\r\nd\r\n", "output": "d\r\n"}, {"input": "100\r\nwm\r\nq\r\nhf\r\nwm\r\niz\r\ndl\r\nmiz\r\np\r\nzoa\r\nbk\r\nw\r\nxv\r\nfj\r\nd\r\nxvsg\r\nr\r\nx\r\nt\r\nyd\r\nbke\r\ny\r\neq\r\nx\r\nn\r\nry\r\nt\r\nc\r\nuh\r\nn\r\npw\r\nuhf\r\neq\r\nr\r\nw\r\nk\r\nt\r\nsg\r\njb\r\nd\r\nke\r\ne\r\nx\r\nh\r\ntuh\r\nan\r\nn\r\noa\r\nw\r\nq\r\nz\r\nk\r\noan\r\nbk\r\nj\r\nzoan\r\nyd\r\npwmi\r\nyd\r\nc\r\nry\r\nfj\r\nlx\r\nqr\r\nke\r\nizo\r\nm\r\nz\r\noan\r\nwmi\r\nl\r\nyd\r\nz\r\ns\r\nke\r\nw\r\nfjbk\r\nqry\r\nlxv\r\nhf\r\ns\r\nnc\r\nq\r\nlxv\r\nzoa\r\nn\r\nfj\r\np\r\nhf\r\nmiz\r\npwm\r\ntu\r\noan\r\ng\r\nd\r\nqr\r\na\r\nan\r\nxvs\r\ny\r\ntuhf\r\n", "output": "pwmizoanctuhfjbkeqrydlxvsg\r\n"}, {"input": "94\r\ncw\r\nm\r\nuhbk\r\ntfy\r\nsd\r\nu\r\ntf\r\ntfym\r\nfy\r\nbk\r\nx\r\nx\r\nxl\r\npu\r\noq\r\nkt\r\ny\r\nb\r\nj\r\nqxl\r\no\r\noqx\r\nr\r\nr\r\njr\r\nk\r\ne\r\nw\r\nsd\r\na\r\nljre\r\nhbk\r\nym\r\nxl\r\np\r\nreg\r\nktf\r\nre\r\nw\r\nhbk\r\nxlj\r\nzn\r\ne\r\nm\r\nms\r\nsdv\r\nr\r\nr\r\no\r\naoq\r\nzna\r\nymsd\r\nqx\r\nr\r\no\r\nlj\r\nm\r\nk\r\nu\r\nkt\r\nms\r\ne\r\nx\r\nh\r\ni\r\nz\r\nm\r\nc\r\nb\r\no\r\nm\r\nvcw\r\ndvc\r\nq\r\na\r\nb\r\nfyms\r\nv\r\nxl\r\nxl\r\ntfym\r\nx\r\nfy\r\np\r\nyms\r\nms\r\nb\r\nt\r\nu\r\nn\r\nq\r\nnaoqx\r\no\r\ne\r\n", "output": "puhbktfymsdvcwznaoqxljregi\r\n"}, {"input": "13\r\ngku\r\nzw\r\nstvqc\r\najy\r\njystvq\r\nfilden\r\nstvq\r\nfild\r\nqcporh\r\najys\r\nqcpor\r\nqcpor\r\ncporhm\r\n", "output": "ajystvqcporhmfildengkuzw\r\n"}, {"input": "2\r\not\r\nqu\r\n", "output": "otqu\r\n"}, {"input": "100\r\nv\r\nh\r\nj\r\nf\r\nr\r\ni\r\ns\r\nw\r\nv\r\nd\r\nv\r\np\r\nd\r\nu\r\ny\r\nd\r\nu\r\nx\r\nr\r\nu\r\ng\r\nm\r\ns\r\nf\r\nv\r\nx\r\na\r\ng\r\ng\r\ni\r\ny\r\ny\r\nv\r\nd\r\ni\r\nq\r\nq\r\nu\r\nx\r\nj\r\nv\r\nj\r\ne\r\no\r\nr\r\nh\r\nu\r\ne\r\nd\r\nv\r\nb\r\nv\r\nq\r\nk\r\ni\r\nr\r\ne\r\nm\r\na\r\nj\r\na\r\nu\r\nq\r\nx\r\nq\r\ny\r\ns\r\nw\r\nk\r\ni\r\ns\r\nr\r\np\r\ni\r\np\r\ns\r\nd\r\nj\r\nw\r\no\r\nm\r\ns\r\nr\r\nd\r\nf\r\ns\r\nw\r\nv\r\ne\r\ny\r\no\r\nx\r\na\r\np\r\nk\r\nr\r\ng\r\ng\r\nb\r\nq\r\n", "output": "vhjfriswdpuyxgmaqeobk\r\n"}, {"input": "99\r\ntnq\r\nep\r\nuk\r\nk\r\nx\r\nvhy\r\nepj\r\nx\r\nj\r\nhy\r\nukg\r\nsep\r\nquk\r\nr\r\nw\r\no\r\nxrwm\r\ndl\r\nh\r\no\r\nad\r\ng\r\ng\r\nhy\r\nxr\r\nad\r\nhyx\r\nkg\r\nvh\r\nb\r\nlovh\r\nuk\r\nl\r\ntn\r\nkg\r\ny\r\nu\r\nxr\r\nse\r\nyx\r\nmt\r\nlo\r\nm\r\nu\r\nukg\r\ngse\r\na\r\nuk\r\nn\r\nr\r\nlov\r\nep\r\nh\r\nadl\r\nyx\r\nt\r\nukg\r\nz\r\nepj\r\nz\r\nm\r\nx\r\nov\r\nyx\r\nxr\r\nep\r\nw\r\ny\r\nmtn\r\nsep\r\nep\r\nmt\r\nrwmt\r\nuk\r\nlo\r\nz\r\nnq\r\nj\r\ntn\r\nj\r\nkgs\r\ny\r\nb\r\nmtn\r\nsep\r\nr\r\ns\r\no\r\nr\r\nepjb\r\nadl\r\nrwmt\r\nyxrw\r\npj\r\nvhy\r\nk\r\ns\r\nx\r\nt\r\n", "output": "adlovhyxrwmtnqukgsepjbz\r\n"}, {"input": "95\r\nx\r\np\r\nk\r\nu\r\ny\r\nz\r\nt\r\na\r\ni\r\nj\r\nc\r\nh\r\nk\r\nn\r\nk\r\ns\r\nr\r\ny\r\nn\r\nv\r\nf\r\nb\r\nr\r\no\r\no\r\nu\r\nb\r\nj\r\no\r\nd\r\np\r\ns\r\nb\r\nt\r\nd\r\nq\r\nq\r\na\r\nm\r\ny\r\nq\r\nj\r\nz\r\nk\r\ne\r\nt\r\nv\r\nj\r\np\r\np\r\ns\r\nz\r\no\r\nk\r\nt\r\na\r\na\r\nc\r\np\r\nb\r\np\r\nx\r\nc\r\ny\r\nv\r\nj\r\na\r\np\r\nc\r\nd\r\nj\r\nt\r\nj\r\nt\r\nf\r\no\r\no\r\nn\r\nx\r\nq\r\nc\r\nk\r\np\r\nk\r\nq\r\na\r\ns\r\nl\r\na\r\nq\r\na\r\nb\r\ne\r\nj\r\nl\r\n", "output": "xpkuyztaijchnsrvfbodqmel\r\n"}, {"input": "96\r\not\r\njo\r\nvpr\r\nwi\r\ngx\r\nay\r\nzqf\r\nzq\r\npr\r\nigx\r\ntsb\r\nv\r\nr\r\ngxc\r\nigx\r\ngx\r\nvpr\r\nxc\r\nylk\r\nigx\r\nlkh\r\nvp\r\nuvp\r\nz\r\nbuv\r\njo\r\nvpr\r\npr\r\nprn\r\nwi\r\nqfw\r\nbuv\r\nd\r\npr\r\ndmj\r\nvpr\r\ng\r\nylk\r\nsbu\r\nhz\r\nk\r\nzqf\r\nylk\r\nxc\r\nwi\r\nvpr\r\nbuv\r\nzq\r\nmjo\r\nkh\r\nuv\r\nuvp\r\nts\r\nt\r\nylk\r\nnay\r\nbuv\r\nhzq\r\nts\r\njo\r\nsbu\r\nqfw\r\ngxc\r\ntsb\r\np\r\nhzq\r\nbuv\r\nsbu\r\nfwi\r\nkh\r\nmjo\r\nwig\r\nhzq\r\ndmj\r\ntsb\r\ntsb\r\nts\r\nylk\r\nyl\r\ngxc\r\not\r\nots\r\nuvp\r\nay\r\nay\r\nuvp\r\not\r\ny\r\np\r\nm\r\ngx\r\nkhz\r\ngxc\r\nkhz\r\ntsb\r\nrn\r\n", "output": "dmjotsbuvprnaylkhzqfwigxc\r\n"}, {"input": "3\r\nm\r\nu\r\nm\r\n", "output": "mu\r\n"}, {"input": "4\r\np\r\na\r\nz\r\nq\r\n", "output": "pazq\r\n"}, {"input": "5\r\ngtb\r\nnlu\r\nzjp\r\nk\r\nazj\r\n", "output": "azjpgtbnluk\r\n"}, {"input": "70\r\nxv\r\nlu\r\ntb\r\njx\r\nseh\r\nc\r\nm\r\ntbr\r\ntb\r\ndl\r\ne\r\nd\r\nt\r\np\r\nn\r\nse\r\nna\r\neh\r\nw\r\np\r\nzkj\r\nr\r\nk\r\nrw\r\nqf\r\ndl\r\ndl\r\ns\r\nat\r\nkjx\r\na\r\nz\r\nmig\r\nu\r\nse\r\npse\r\nd\r\ng\r\nc\r\nxv\r\nv\r\ngo\r\nps\r\ncd\r\nyqf\r\nyqf\r\nwzk\r\nxv\r\nat\r\nw\r\no\r\nl\r\nxvm\r\nfpse\r\nz\r\nk\r\nna\r\nv\r\nseh\r\nk\r\nl\r\nz\r\nd\r\nz\r\nn\r\nm\r\np\r\ng\r\nse\r\nat\r\n", "output": "cdlunatbrwzkjxvmigoyqfpseh\r\n"}, {"input": "3\r\nbmg\r\nwjah\r\nil\r\n", "output": "bmgilwjah\r\n"}, {"input": "100\r\ne\r\nbr\r\nls\r\nfb\r\nyx\r\nva\r\njm\r\nwn\r\nak\r\nhv\r\noq\r\nyx\r\nl\r\nm\r\nak\r\nce\r\nug\r\nqz\r\nug\r\ndf\r\nty\r\nhv\r\nmo\r\nxp\r\nyx\r\nkt\r\nak\r\nmo\r\niu\r\nxp\r\nce\r\nnd\r\noq\r\nbr\r\nty\r\nva\r\nce\r\nwn\r\nx\r\nsj\r\nel\r\npi\r\noq\r\ndf\r\niu\r\nc\r\nhv\r\npi\r\nsj\r\nhv\r\nmo\r\nbr\r\nxp\r\nce\r\nfb\r\nwn\r\nnd\r\nfb\r\npi\r\noq\r\nhv\r\nty\r\ngw\r\noq\r\nel\r\nw\r\nhv\r\nce\r\noq\r\nsj\r\nsj\r\nl\r\nwn\r\nqz\r\nty\r\nbr\r\nz\r\nel\r\nug\r\nce\r\nnd\r\nj\r\ndf\r\npi\r\niu\r\nnd\r\nls\r\niu\r\nrc\r\nbr\r\nug\r\nrc\r\nnd\r\nak\r\njm\r\njm\r\no\r\nls\r\nq\r\nfb\r\n", "output": "hvaktyxpiugwndfbrcelsjmoqz\r\n"}, {"input": "23\r\nq\r\ni\r\nj\r\nx\r\nz\r\nm\r\nt\r\ns\r\nu\r\ng\r\nc\r\nk\r\nh\r\nb\r\nx\r\nh\r\nt\r\no\r\ny\r\nh\r\nb\r\nn\r\na\r\n", "output": "qijxzmtsugckhboyna\r\n"}, {"input": "12\r\nkx\r\ng\r\nfo\r\nnt\r\nmf\r\nzv\r\nir\r\nds\r\nbz\r\nf\r\nlw\r\nx\r\n", "output": "bzvdsirkxlwmfontg\r\n"}, {"input": "2\r\na\r\nt\r\n", "output": "at\r\n"}, {"input": "1\r\ndm\r\n", "output": "dm\r\n"}, {"input": "100\r\nj\r\numj\r\ninc\r\nu\r\nsd\r\ntin\r\nw\r\nlf\r\nhs\r\nepk\r\nyg\r\nqhs\r\nh\r\nti\r\nf\r\nsd\r\ngepk\r\nu\r\nfw\r\nu\r\nsd\r\nvumj\r\num\r\ndt\r\nb\r\ng\r\nozl\r\nabvu\r\noz\r\nn\r\nw\r\nab\r\nge\r\nqh\r\nfwy\r\nsdti\r\ng\r\nyge\r\nepk\r\nabvu\r\nz\r\nlfw\r\nbv\r\nab\r\nyge\r\nqhs\r\nge\r\nhsdt\r\num\r\nl\r\np\r\na\r\nab\r\nd\r\nfw\r\ngep\r\nfwy\r\nbvu\r\nvumj\r\nzlfw\r\nk\r\nepk\r\ntin\r\npkab\r\nzl\r\nvum\r\nr\r\nf\r\nd\r\nsdt\r\nhs\r\nxoz\r\nlfwy\r\nfw\r\num\r\nep\r\nincx\r\na\r\nt\r\num\r\nh\r\nsdt\r\ngep\r\nlfw\r\nkab\r\ng\r\nmjr\r\nj\r\noz\r\ns\r\nwy\r\nnc\r\nlfw\r\nyg\r\nygep\r\nti\r\nyg\r\npk\r\nkab\r\nwyg\r\n", "output": "qhsdtincxozlfwygepkabvumjr\r\n"}, {"input": "94\r\nkmwbq\r\nmw\r\nwbq\r\ns\r\nlx\r\nf\r\npf\r\nl\r\nkmwb\r\na\r\nfoynt\r\nnt\r\nx\r\npf\r\npf\r\nep\r\nqs\r\nwbqse\r\nrl\r\nfoynt\r\nntzjd\r\nlxc\r\npfoy\r\nlx\r\nr\r\nagikm\r\nr\r\ntzjd\r\nep\r\nyntz\r\nu\r\nmw\r\nyntz\r\nfoynt\r\ntzjd\r\njdrlx\r\nwbqse\r\nr\r\nkmw\r\nwbq\r\nlx\r\nfoyn\r\nkm\r\nsepfo\r\nikmw\r\nf\r\nrlxch\r\nzjdrl\r\nyn\r\nhv\r\nynt\r\nbqs\r\nvu\r\nik\r\nqse\r\nxchvu\r\nmwbqs\r\ny\r\nlx\r\nx\r\nntzjd\r\nbq\r\nxchv\r\nwbqse\r\nkm\r\nse\r\nmwb\r\nxchvu\r\nwbq\r\nc\r\ngikm\r\nbq\r\nwb\r\nmwbq\r\nikmw\r\nag\r\ny\r\nchvu\r\nbqsep\r\nbqs\r\nrlx\r\ntzjd\r\nmwb\r\na\r\ndrlxc\r\ntzjd\r\nt\r\nsepf\r\nwbqse\r\nd\r\nbqs\r\nyn\r\nh\r\nepfo\r\n", "output": "agikmwbqsepfoyntzjdrlxchvu\r\n"}, {"input": "13\r\ndaq\r\nvcnexi\r\nlkp\r\nztvcne\r\naqozt\r\nztvcne\r\nprdaqo\r\ncnex\r\nnexijm\r\nztvcne\r\nfysh\r\nxijmb\r\naq\r\n", "output": "fyshlkprdaqoztvcnexijmb\r\n"}, {"input": "2\r\nnxqdblgac\r\nzpjou\r\n", "output": "nxqdblgaczpjou\r\n"}, {"input": "7\r\nfjr\r\ngk\r\nigkf\r\nret\r\nvx\r\nvxa\r\ncv\r\n", "output": "cvxaigkfjret\r\n"}, {"input": "7\r\nwer\r\nqwe\r\nw\r\nq\r\nert\r\ntyu\r\nrty\r\n", "output": "qwertyu\r\n"}, {"input": "4\r\na\r\nb\r\nab\r\nabc\r\n", "output": "abc\r\n"}, {"input": "4\r\nt\r\nwef\r\nqwe\r\nh\r\n", "output": "qwefth\r\n"}, {"input": "5\r\nabcd\r\nbc\r\ndef\r\nde\r\ncd\r\n", "output": "abcdef\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path) as f: n = int(f.readline().strip()) fragments = [line.strip() for line in f] with open(output_path) as f: correct_output = f.read().strip() correct_length = len(correct_output) with open(submission_path) as f: submission_output = f.read().strip() # Check length matches correct (minimal) if len(submission_output) != correct_length: print(0) return # Check all characters are unique if len(set(submission_output)) != len(submission_output): print(0) return # Check all fragments are present as substrings sub = submission_output for frag in fragments: if frag not in sub: print(0) return # All conditions met print(1) if __name__ == "__main__": main()
true
733/C
733
C
PyPy 3
TESTS
9
140
0
77664040
'''R = lambda: map(int, input().split()) n = int(input()) a = list(R()) m = int(input()) b = list(R()) st = i = j = 0 res = '' while i < m: psum = 0 while j < n and psum < b[i]: psum += a[j] j += 1 if psum != b[i]: print('NO') exit(0) print(j,st,a[st],a[j-1]) mi = st if a[st] > a[j - 1] else j - 1 for k in range(st + 1, j - 1): print(k,st+1,j-1,mi) if (a[k] > a[k - 1] or a[k] > a[k + 1]) and a[k] >= a[mi]: mi = k psum = a[mi] l, r = mi - 1, mi + 1 while psum != b[i]: if l >= st and psum > a[l]: res += str(i + l - st + 2) + ' L\n' psum += a[l] l -= 1 elif r < j and psum > a[r]: res += str(i + l - st + 2) + ' R\n' psum += a[r] r += 1 else: print('NO') exit(0) st = j i += 1 if i != m or j != n: print('NO') else: print('YES') print(res)''' import sys input=sys.stdin.readline import math '''import io, os import bisect input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline''' n=int(input()) a=[int(i) for i in input().split()] k=int(input()) b=[int(i) for i in input().split()] pref,j,q=[a[0]],0,[0] suma=0 ind=[] ans=[] for i in range(1,n): pref.append(pref[-1]+a[i]) for i in range(len(pref)): #print(pref[i]-suma,suma) if j<=k-1: if pref[i]-suma==b[j]: q.append(i) ind.append(q) q=[i+1] suma=pref[i] j+=1 #print(pref,ind) if len(ind)!=k or ind[-1][1]!=n-1: print('NO') exit() ans=[] for i in range(k): if ind[i][0]==ind[i][1]: continue else: store=0 a=a[ind[i][0]:] sort=a[:ind[i][1]-ind[i][0]+1] #print(a[ind[i][0]:ind[i][1]+1],ind[i][0],ind[i][1]) sort.sort(reverse=True) if len(set(sort))==1: print('NO') exit() else: for ij in sort: #print(ij) ok=False for j in range(ind[i][1]-ind[i][0]+1): #print(j,a[j],ij) if a[j]==ij: if j==0: if a[j+1]<a[j]: store=j ok=True elif j==ind[i][1]-ind[i][0]: if a[j-1]<a[j]: store=j ok=True else: #print(a[j],a[j-1],a[j+1]) if a[j-1]<a[j] or a[j]>a[j+1]: store=j ok=True #print(store,ok) if ok==True: break if ok==True: break #print(store) psum = a[store] #print(psum) l, r = store - 1, store + 1 while psum != b[i]: if l >= 0 and psum > a[l]: ans.append([l+2+i,'L']) psum += a[l] l -= 1 elif r <=(ind[i][1]-ind[i][0]) and psum > a[r]: ans.append([l+2+i,'R']) psum += a[r] r += 1 else: print('NO') exit(0) print("YES") for i in ans: i=' '.join(map(str,i)) sys.stdout.write(i+'\n')
139
46
0
219702093
R = lambda: map(int, input().split()) n = int(input()) a = list(R()) m = int(input()) b = list(R()) st = i = j = 0 res = '' while i < m: psum = 0 while j < n and psum < b[i]: psum += a[j] j += 1 if psum != b[i]: print('NO') exit(0) mi = st if a[st] > a[j - 1] else j - 1 for k in range(st, j - 1): if (a[k] > a[k - 1] or a[k] > a[k + 1]) and a[k] >= a[mi]: mi = k psum = a[mi] l, r = mi - 1, mi + 1 while psum != b[i]: if l >= st and psum > a[l]: res += str(i + l - st + 2) + ' L\n' psum += a[l] l -= 1 elif r < j and psum > a[r]: res += str(i + l - st + 2) + ' R\n' psum += a[r] r += 1 else: print('NO') exit(0) st = j i += 1 if i != m or j != n: print('NO') else: print('YES') print(res)
Codeforces Round 378 (Div. 2)
CF
2,016
1
256
Epidemic in Monstropolis
There was an epidemic in Monstropolis and all monsters became sick. To recover, all monsters lined up in queue for an appointment to the only doctor in the city. Soon, monsters became hungry and began to eat each other. One monster can eat other monster if its weight is strictly greater than the weight of the monster being eaten, and they stand in the queue next to each other. Monsters eat each other instantly. There are no monsters which are being eaten at the same moment. After the monster A eats the monster B, the weight of the monster A increases by the weight of the eaten monster B. In result of such eating the length of the queue decreases by one, all monsters after the eaten one step forward so that there is no empty places in the queue again. A monster can eat several monsters one after another. Initially there were n monsters in the queue, the i-th of which had weight ai. For example, if weights are [1, 2, 2, 2, 1, 2] (in order of queue, monsters are numbered from 1 to 6 from left to right) then some of the options are: 1. the first monster can't eat the second monster because a1 = 1 is not greater than a2 = 2; 2. the second monster can't eat the third monster because a2 = 2 is not greater than a3 = 2; 3. the second monster can't eat the fifth monster because they are not neighbors; 4. the second monster can eat the first monster, the queue will be transformed to [3, 2, 2, 1, 2]. After some time, someone said a good joke and all monsters recovered. At that moment there were k (k ≤ n) monsters in the queue, the j-th of which had weight bj. Both sequences (a and b) contain the weights of the monsters in the order from the first to the last. You are required to provide one of the possible orders of eating monsters which led to the current queue, or to determine that this could not happen. Assume that the doctor didn't make any appointments while monsters were eating each other.
The first line contains single integer n (1 ≤ n ≤ 500) — the number of monsters in the initial queue. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial weights of the monsters. The third line contains single integer k (1 ≤ k ≤ n) — the number of monsters in the queue after the joke. The fourth line contains k integers b1, b2, ..., bk (1 ≤ bj ≤ 5·108) — the weights of the monsters after the joke. Monsters are listed in the order from the beginning of the queue to the end.
In case if no actions could lead to the final queue, print "NO" (without quotes) in the only line. Otherwise print "YES" (without quotes) in the first line. In the next n - k lines print actions in the chronological order. In each line print x — the index number of the monster in the current queue which eats and, separated by space, the symbol 'L' if the monster which stays the x-th in the queue eats the monster in front of him, or 'R' if the monster which stays the x-th in the queue eats the monster behind him. After each eating the queue is enumerated again. When one monster eats another the queue decreases. If there are several answers, print any of them.
null
In the first example, initially there were n = 6 monsters, their weights are [1, 2, 2, 2, 1, 2] (in order of queue from the first monster to the last monster). The final queue should be [5, 5]. The following sequence of eatings leads to the final queue: - the second monster eats the monster to the left (i.e. the first monster), queue becomes [3, 2, 2, 1, 2]; - the first monster (note, it was the second on the previous step) eats the monster to the right (i.e. the second monster), queue becomes [5, 2, 1, 2]; - the fourth monster eats the mosnter to the left (i.e. the third monster), queue becomes [5, 2, 3]; - the finally, the third monster eats the monster to the left (i.e. the second monster), queue becomes [5, 5]. Note that for each step the output contains numbers of the monsters in their current order in the queue.
[{"input": "6\n1 2 2 2 1 2\n2\n5 5", "output": "YES\n2 L\n1 R\n4 L\n3 L"}, {"input": "5\n1 2 3 4 5\n1\n15", "output": "YES\n5 L\n4 L\n3 L\n2 L"}, {"input": "5\n1 1 1 3 3\n3\n2 1 6", "output": "NO"}]
1,800
["constructive algorithms", "dp", "greedy", "two pointers"]
139
[{"input": "6\r\n1 2 2 2 1 2\r\n2\r\n5 5\r\n", "output": "YES\r\n2 L\r\n1 R\r\n4 L\r\n3 L\r\n"}, {"input": "5\r\n1 2 3 4 5\r\n1\r\n15\r\n", "output": "YES\r\n5 L\r\n4 L\r\n3 L\r\n2 L\r\n"}, {"input": "5\r\n1 1 1 3 3\r\n3\r\n2 1 6\r\n", "output": "NO"}, {"input": "5\r\n1 1 1 1 2\r\n3\r\n1 1 4\r\n", "output": "YES\r\n5 L\r\n4 L\r\n"}, {"input": "5\r\n1 1 1 1 1\r\n4\r\n1 1 2 1\r\n", "output": "NO"}, {"input": "6\r\n2 1 2 2 1 2\r\n2\r\n5 5\r\n", "output": "YES\r\n3 L\r\n2 L\r\n4 L\r\n3 L\r\n"}, {"input": "8\r\n2 5 3 1 4 2 3 4\r\n3\r\n10 6 8\r\n", "output": "NO"}, {"input": "1\r\n959139\r\n1\r\n470888\r\n", "output": "NO"}, {"input": "3\r\n2 2 1\r\n1\r\n5\r\n", "output": "YES\r\n2 R\r\n2 L\r\n"}, {"input": "3\r\n1 2 2\r\n1\r\n5\r\n", "output": "YES\r\n2 L\r\n1 R\r\n"}, {"input": "5\r\n1 2 3 4 5\r\n1\r\n10\r\n", "output": "NO"}, {"input": "5\r\n325539 329221 106895 882089 718673\r\n5\r\n699009 489855 430685 939232 282330\r\n", "output": "NO"}, {"input": "10\r\n30518 196518 274071 359971 550121 204862 843967 173607 619138 690754\r\n3\r\n171337 183499 549873\r\n", "output": "NO"}, {"input": "3\r\n2 1 1\r\n1\r\n3\r\n", "output": "NO"}, {"input": "4\r\n2 2 2 1\r\n3\r\n2 2 2\r\n", "output": "NO"}, {"input": "3\r\n1 2 3\r\n1\r\n3\r\n", "output": "NO"}, {"input": "2\r\n1 2\r\n2\r\n3 1\r\n", "output": "NO"}, {"input": "5\r\n3 3 2 2 1\r\n2\r\n8 3\r\n", "output": "YES\r\n2 R\r\n2 L\r\n2 R\r\n"}, {"input": "3\r\n3 2 5\r\n1\r\n10\r\n", "output": "YES\r\n3 L\r\n2 L\r\n"}, {"input": "3\r\n1 5 1\r\n1\r\n6\r\n", "output": "NO"}, {"input": "5\r\n1 2 3 4 5\r\n3\r\n1 2 3\r\n", "output": "NO"}, {"input": "3\r\n5 2 3\r\n1\r\n10\r\n", "output": "YES\r\n1 R\r\n1 R\r\n"}, {"input": "3\r\n2 1 3\r\n1\r\n6\r\n", "output": "YES\r\n3 L\r\n2 L\r\n"}, {"input": "3\r\n3 2 1\r\n1\r\n6\r\n", "output": "YES\r\n1 R\r\n1 R\r\n"}, {"input": "2\r\n5 5\r\n1\r\n5\r\n", "output": "NO"}, {"input": "3\r\n1 2 3\r\n2\r\n1 2\r\n", "output": "NO"}, {"input": "4\r\n1 2 3 4\r\n3\r\n1 2 3\r\n", "output": "NO"}, {"input": "4\r\n4 3 2 1\r\n3\r\n3 2 1\r\n", "output": "NO"}, {"input": "2\r\n5 3\r\n1\r\n5\r\n", "output": "NO"}, {"input": "5\r\n1 1 1 1 1\r\n4\r\n1 1 1 1\r\n", "output": "NO"}, {"input": "3\r\n3 3 2\r\n1\r\n8\r\n", "output": "YES\r\n2 R\r\n2 L\r\n"}, {"input": "8\r\n2 2 1 2 2 1 2 4\r\n2\r\n9 8\r\n", "output": "NO"}, {"input": "4\r\n3 2 1 4\r\n3\r\n3 2 1\r\n", "output": "NO"}, {"input": "5\r\n3 3 2 3 1\r\n2\r\n11 1\r\n", "output": "YES\r\n4 L\r\n3 L\r\n2 L\r\n"}, {"input": "3\r\n2 1 3\r\n1\r\n3\r\n", "output": "NO"}, {"input": "4\r\n2 3 3 2\r\n2\r\n5 3\r\n", "output": "NO"}, {"input": "16\r\n2 2 2 1 2 2 2 1 1 2 2 2 1 2 2 2\r\n4\r\n7 7 7 7\r\n", "output": "YES\r\n3 R\r\n3 L\r\n2 L\r\n4 R\r\n4 L\r\n3 L\r\n4 L\r\n3 R\r\n3 R\r\n5 L\r\n4 R\r\n4 R\r\n"}, {"input": "2\r\n1 1\r\n1\r\n1\r\n", "output": "NO"}, {"input": "3\r\n1 2 1\r\n2\r\n3 2\r\n", "output": "NO"}, {"input": "3\r\n2 3 5\r\n1\r\n10\r\n", "output": "YES\r\n3 L\r\n2 L\r\n"}, {"input": "5\r\n1 2 3 4 5\r\n2\r\n3 7\r\n", "output": "NO"}, {"input": "4\r\n1 2 3 4\r\n2\r\n1 2\r\n", "output": "NO"}, {"input": "8\r\n1 2 2 2 1 2 1 1\r\n2\r\n5 5\r\n", "output": "NO"}, {"input": "3\r\n5 5 4\r\n1\r\n14\r\n", "output": "YES\r\n2 R\r\n2 L\r\n"}, {"input": "22\r\n3 2 3 3 3 1 1 2 1 2 1 1 1 2 2 3 1 2 3 3 3 3\r\n5\r\n5 16 5 5 15\r\n", "output": "YES\r\n1 R\r\n4 R\r\n4 R\r\n4 R\r\n4 R\r\n4 R\r\n4 L\r\n3 L\r\n6 L\r\n5 L\r\n4 L\r\n5 L\r\n7 L\r\n6 L\r\n5 R\r\n5 R\r\n5 R\r\n"}, {"input": "4\r\n2 2 1 2\r\n1\r\n7\r\n", "output": "YES\r\n4 L\r\n3 L\r\n2 L\r\n"}, {"input": "7\r\n2 2 2 1 2 2 2\r\n1\r\n13\r\n", "output": "YES\r\n5 L\r\n4 L\r\n3 L\r\n2 L\r\n1 R\r\n1 R\r\n"}, {"input": "2\r\n1 2\r\n1\r\n1\r\n", "output": "NO"}, {"input": "14\r\n5 5 5 5 4 4 4 3 3 3 4 4 4 4\r\n3\r\n32 21 4\r\n", "output": "YES\r\n4 R\r\n4 R\r\n4 R\r\n4 L\r\n3 L\r\n2 L\r\n5 L\r\n4 L\r\n3 L\r\n2 R\r\n2 R\r\n"}, {"input": "5\r\n2 2 1 2 2\r\n1\r\n9\r\n", "output": "YES\r\n4 L\r\n3 L\r\n2 L\r\n1 R\r\n"}, {"input": "1\r\n2\r\n1\r\n2\r\n", "output": "YES\r\n"}, {"input": "23\r\n3 2 1 3 3 3 1 1 2 1 2 1 1 1 2 2 3 1 2 3 3 3 3\r\n5\r\n6 16 5 5 15\r\n", "output": "YES\r\n1 R\r\n1 R\r\n4 R\r\n4 R\r\n4 R\r\n4 R\r\n4 R\r\n4 L\r\n3 L\r\n6 L\r\n5 L\r\n4 L\r\n5 L\r\n7 L\r\n6 L\r\n5 R\r\n5 R\r\n5 R\r\n"}]
false
stdio
import sys def read_file(path): with open(path, 'r') as f: return f.read().splitlines() input_path, output_path, submission_path = sys.argv[1], sys.argv[2], sys.argv[3] input_lines = read_file(input_path) n = int(input_lines[0]) a = list(map(int, input_lines[1].split())) k = int(input_lines[2]) b = list(map(int, input_lines[3].split())) ref_lines = read_file(output_path) submission_lines = read_file(submission_path) if not submission_lines: print(0) sys.exit(0) submission_first_line = submission_lines[0].strip() ref_first_line = ref_lines[0].strip() if submission_first_line != ref_first_line: print(0) sys.exit(0) if submission_first_line == "NO": print(1) sys.exit(0) # Now, handle YES case expected_steps = n - k if len(submission_lines) - 1 != expected_steps: print(0) sys.exit(0) current_queue = a.copy() valid = True for step in submission_lines[1:]: step = step.strip() if not step: valid = False break parts = step.split() if len(parts) != 2: valid = False break try: x = int(parts[0]) direction = parts[1] except: valid = False break m = len(current_queue) if x < 1 or x > m: valid = False break if direction not in ['L', 'R']: valid = False break if direction == 'L': if x == 1: valid = False break eater_pos = x - 1 eaten_pos = x - 2 else: if x == m: valid = False break eater_pos = x - 1 eaten_pos = x eater = current_queue[eater_pos] eaten = current_queue[eaten_pos] if eater <= eaten: valid = False break # Merge the monsters new_eater = eater + eaten if direction == 'L': current_queue = current_queue[:eaten_pos] + [new_eater] + current_queue[eater_pos+1:] else: current_queue = current_queue[:eater_pos] + [new_eater] + current_queue[eaten_pos+1:] # Check final queue if valid and current_queue == b: print(1) else: print(0)
true
982/A
982
A
Python 3
TESTS
27
124
0
40407719
n = int(input()) flag = True a = input() for i in range(1,n-1): if a[i] == '1': if a[i-1] == '1' or a[i+1] == '1': print("No") flag = False break if a[i] == '0': if a[i - 1] == '0' and a[i + 1] == '0': print("No") flag = False break if ((a == '0' or a == '11' or a == '10' or a == '01') or (a[n-1] == '0' and a[n-2] == '0') or (a[0] == '0' and a[1] == '0')) and flag: flag = False print("No") if flag: print("Yes")
55
46
0
192764494
# LUOGU_RID: 101739723 n = int(input()) s = input() print((s.find('11') != -1 or any(s[i] == '0' and (i == 0 or s[i - 1] == '0') and (i == n - 1 or s[i + 1] == '0') for i in range(n))) and 'No' or 'Yes')
Codeforces Round 484 (Div. 2)
CF
2,018
1
256
Row
You're given a row with $$$n$$$ chairs. We call a seating of people "maximal" if the two following conditions hold: 1. There are no neighbors adjacent to anyone seated. 2. It's impossible to seat one more person without violating the first rule. The seating is given as a string consisting of zeros and ones ($$$0$$$ means that the corresponding seat is empty, $$$1$$$ — occupied). The goal is to determine whether this seating is "maximal". Note that the first and last seats are not adjacent (if $$$n \ne 2$$$).
The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of chairs. The next line contains a string of $$$n$$$ characters, each of them is either zero or one, describing the seating.
Output "Yes" (without quotation marks) if the seating is "maximal". Otherwise print "No". You are allowed to print letters in whatever case you'd like (uppercase or lowercase).
null
In sample case one the given seating is maximal. In sample case two the person at chair three has a neighbour to the right. In sample case three it is possible to seat yet another person into chair three.
[{"input": "3\n101", "output": "Yes"}, {"input": "4\n1011", "output": "No"}, {"input": "5\n10001", "output": "No"}]
1,200
["brute force", "constructive algorithms"]
55
[{"input": "3\r\n101\r\n", "output": "Yes\r\n"}, {"input": "4\r\n1011\r\n", "output": "No\r\n"}, {"input": "5\r\n10001\r\n", "output": "No\r\n"}, {"input": "1\r\n0\r\n", "output": "No\r\n"}, {"input": "1\r\n1\r\n", "output": "Yes\r\n"}, {"input": "100\r\n0101001010101001010010010101001010100101001001001010010101010010101001001010101001001001010100101010\r\n", "output": "Yes\r\n"}, {"input": "4\r\n0100\r\n", "output": "No\r\n"}, {"input": "42\r\n011000100101001001101011011010100010011010\r\n", "output": "No\r\n"}, {"input": "3\r\n001\r\n", "output": "No\r\n"}, {"input": "64\r\n1001001010010010100101010010010100100101001001001001010100101001\r\n", "output": "Yes\r\n"}, {"input": "3\r\n111\r\n", "output": "No\r\n"}, {"input": "4\r\n0000\r\n", "output": "No\r\n"}, {"input": "4\r\n0001\r\n", "output": "No\r\n"}, {"input": "4\r\n0010\r\n", "output": "No\r\n"}, {"input": "4\r\n0011\r\n", "output": "No\r\n"}, {"input": "4\r\n0101\r\n", "output": "Yes\r\n"}, {"input": "4\r\n0110\r\n", "output": "No\r\n"}, {"input": "4\r\n0111\r\n", "output": "No\r\n"}, {"input": "4\r\n1000\r\n", "output": "No\r\n"}, {"input": "4\r\n1001\r\n", "output": "Yes\r\n"}, {"input": "4\r\n1010\r\n", "output": "Yes\r\n"}, {"input": "4\r\n1100\r\n", "output": "No\r\n"}, {"input": "4\r\n1101\r\n", "output": "No\r\n"}, {"input": "4\r\n1110\r\n", "output": "No\r\n"}, {"input": "4\r\n1111\r\n", "output": "No\r\n"}, {"input": "2\r\n00\r\n", "output": "No\r\n"}, {"input": "2\r\n01\r\n", "output": "Yes\r\n"}, {"input": "2\r\n10\r\n", "output": "Yes\r\n"}, {"input": "2\r\n11\r\n", "output": "No\r\n"}, {"input": "3\r\n000\r\n", "output": "No\r\n"}, {"input": "3\r\n010\r\n", "output": "Yes\r\n"}, {"input": "3\r\n011\r\n", "output": "No\r\n"}, {"input": "3\r\n100\r\n", "output": "No\r\n"}, {"input": "3\r\n110\r\n", "output": "No\r\n"}, {"input": "100\r\n0111001010101110001100000010011000100101110010001100111110101110001110101010111000010010011000000110\r\n", "output": "No\r\n"}, {"input": "357\r\n100101010010010010010100101001001010101010100100100100101001010101001010010100101001010100101001010010100100101001010101010101001001010100101010010100101001010100100100101010010010010100101010010010101001010010010101001001010010010101010100100101010010100100101001010100101001010100101001010010010010100101001010100100100100100100100101010101010010010100101\r\n", "output": "Yes\r\n"}, {"input": "459\r\n000111000101010000100001001010111110011011010001100101111010111011101110111101111101100101100011011001100110001001111001101000111001011100110100011111011111000010000110010011100110011011111110011100001101001111000100111011001000001011111100110100001001001100101011100001110110100101011011110100100111101011000101110000100110100100010000000100001001111111000011101010010011001111010111001100000100111001010111011010000011000011100101101011101000011011000110011\r\n", "output": "No\r\n"}, {"input": "3\r\n001\r\n", "output": "No\r\n"}, {"input": "4\r\n0010\r\n", "output": "No\r\n"}, {"input": "3\r\n111\r\n", "output": "No\r\n"}, {"input": "4\r\n0110\r\n", "output": "No\r\n"}, {"input": "2\r\n11\r\n", "output": "No\r\n"}, {"input": "2\r\n10\r\n", "output": "Yes\r\n"}, {"input": "2\r\n01\r\n", "output": "Yes\r\n"}, {"input": "4\r\n1101\r\n", "output": "No\r\n"}, {"input": "2\r\n00\r\n", "output": "No\r\n"}, {"input": "4\r\n1111\r\n", "output": "No\r\n"}, {"input": "15\r\n000010101010000\r\n", "output": "No\r\n"}, {"input": "8\r\n10010100\r\n", "output": "No\r\n"}, {"input": "5\r\n00100\r\n", "output": "No\r\n"}, {"input": "6\r\n100100\r\n", "output": "No\r\n"}, {"input": "7\r\n1000001\r\n", "output": "No\r\n"}, {"input": "98\r\n10101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010\r\n", "output": "Yes\r\n"}]
false
stdio
null
true
762/C
762
C
Python 3
TESTS
17
92
1,331,200
109096293
import sys input = sys.stdin.readline def solve(): a = input().strip() b = input().strip() n = len(a) m = len(b) c = [0]*m d = [0]*m p = 0 for i in range(m): for j in range(p, n): if a[j] == b[i]: c[i] = j p = j + 1 break else: for j in range(i,m): c[j] = n break p = n-1 for i in range(m-1,-1,-1): for j in range(p,-1,-1): if a[j] == b[i]: d[i] = j p = j - 1 break else: for j in range(i,-1,-1): d[j] = -1 break j = 0 while j < m and d[j] < 0: j += 1 res = (m-j, 0, j) for i in range(m): p = c[i] if p == n: break while j < m and d[j] <= p: j += 1 res = max(res,(i+1+m-j,i+1,j)) if res[0] == 0: print('-') else: print(b[:res[1]]+b[res[2]:]) solve()
99
109
6,963,200
168750210
def solve(s1,s2): n=len(s1) m=len(s2) s1=' ' +s1 + ' ' s2=' ' +s2 + ' ' tmp=0 v1=[0]*100005 v2=[0]*100005 for i in range(1,n+1): if s1[i]==s2[tmp+1]: tmp+=1 v1[i]=tmp tmp=m+1 v2[n+1]=tmp for i in range(n,0,-1): if s1[i]==s2[tmp-1]: tmp-=1 v2[i]=tmp b=0 mx=0 for i in range(n+1): c=v1[i]+m-v2[i+1]+1 if c>mx: mx=c b=i ans=[] if mx>=m: for i in range(1,m+1): ans.append(s2[i]) else: for i in range(1,v1[b]+1): ans.append(s2[i]) for i in range(v2[b+1],m+1): ans.append(s2[i]) while len(ans)>0 and ans[-1]==' ': ans.pop() if len(ans)==0: ans="-" return ''.join(ans) s1=input() s2=input() print(solve(s1,s2))
Educational Codeforces Round 17
ICPC
2,017
2
256
Two strings
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).
null
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"}]
2,100
["binary search", "hashing", "strings", "two pointers"]
99
[{"input": "hi\r\nbob\r\n", "output": "-\r\n"}, {"input": "abca\r\naccepted\r\n", "output": "ac\r\n"}, {"input": "abacaba\r\nabcdcba\r\n", "output": "abcba\r\n"}, {"input": "lo\r\neuhaqdhhzlnkmqnakgwzuhurqlpmdm\r\n", "output": "-\r\n"}, {"input": "aaeojkdyuilpdvyewjfrftkpcobhcumwlaoiocbfdtvjkhgda\r\nmlmarpivirqbxcyhyerjoxlslyfzftrylpjyouypvk\r\n", "output": "ouypvk\r\n"}, {"input": "npnkmawey\r\nareakefvowledfriyjejqnnaeqheoh\r\n", "output": "a\r\n"}, {"input": "fdtffutxkujflswyddvhusfcook\r\nkavkhnhphcvckogqqqqhdmgwjdfenzizrebefsbuhzzwhzvc\r\n", "output": "kvc\r\n"}, {"input": "abacaba\r\naa\r\n", "output": "aa\r\n"}, {"input": "edbcd\r\nd\r\n", "output": "d\r\n"}, {"input": "abc\r\nksdksdsdsnabc\r\n", "output": "abc\r\n"}, {"input": "abxzxzxzzaba\r\naba\r\n", "output": "aba\r\n"}, {"input": "abcd\r\nzzhabcd\r\n", "output": "abcd\r\n"}, {"input": "aa\r\naa\r\n", "output": "aa\r\n"}, {"input": "test\r\nt\r\n", "output": "t\r\n"}, {"input": "aa\r\na\r\n", "output": "a\r\n"}, {"input": "aaaabbbbaaaa\r\naba\r\n", "output": "aba\r\n"}, {"input": "aa\r\nzzaa\r\n", "output": "aa\r\n"}, {"input": "zhbt\r\nztjihmhebkrztefpwty\r\n", "output": "zt\r\n"}, {"input": "aaaaaaaaaaaaaaaaaaaa\r\naaaaaaaa\r\n", "output": "aaaaaaaa\r\n"}, {"input": "abba\r\naba\r\n", "output": "aba\r\n"}, {"input": "abbba\r\naba\r\n", "output": "aba\r\n"}, {"input": "aaaaaaaaaaaa\r\naaaaaaaaaaaa\r\n", "output": "aaaaaaaaaaaa\r\n"}, {"input": "aaa\r\naa\r\n", "output": "aa\r\n"}, {"input": "aaaaaaaaaaaa\r\naaa\r\n", "output": "aaa\r\n"}, {"input": "aaaaabbbbbbaaaaaa\r\naba\r\n", "output": "aba\r\n"}, {"input": "ashfaniosafapisfasipfaspfaspfaspfapsfjpasfshvcmvncxmvnxcvnmcxvnmxcnvmcvxvnxmcvxcmvh\r\nashish\r\n", "output": "ashish\r\n"}, {"input": "a\r\na\r\n", "output": "a\r\n"}, {"input": "aaaab\r\naab\r\n", "output": "aab\r\n"}, {"input": "aaaaa\r\naaaa\r\n", "output": "aaaa\r\n"}, {"input": "a\r\naaa\r\n", "output": "a\r\n"}, {"input": "aaaaaabbbbbbaaaaaa\r\naba\r\n", "output": "aba\r\n"}, {"input": "def\r\nabcdef\r\n", "output": "def\r\n"}, {"input": "aaaaaaaaa\r\na\r\n", "output": "a\r\n"}, {"input": "bababsbs\r\nabs\r\n", "output": "abs\r\n"}, {"input": "hddddddack\r\nhackyz\r\n", "output": "hack\r\n"}, {"input": "aba\r\na\r\n", "output": "a\r\n"}, {"input": "ofih\r\nihfsdf\r\n", "output": "ih\r\n"}, {"input": "b\r\nabb\r\n", "output": "b\r\n"}, {"input": "lctsczqr\r\nqvkp\r\n", "output": "q\r\n"}, {"input": "dedcbaa\r\ndca\r\n", "output": "dca\r\n"}, {"input": "haddack\r\nhack\r\n", "output": "hack\r\n"}, {"input": "abcabc\r\nabc\r\n", "output": "abc\r\n"}, {"input": "asdf\r\ngasdf\r\n", "output": "asdf\r\n"}, {"input": "abab\r\nab\r\n", "output": "ab\r\n"}, {"input": "aaaaaaa\r\naaa\r\n", "output": "aaa\r\n"}, {"input": "asdf\r\nfasdf\r\n", "output": "asdf\r\n"}, {"input": "bbaabb\r\nab\r\n", "output": "ab\r\n"}, {"input": "accac\r\nbaacccbcccabaabbcacbbcccacbaabaaac\r\n", "output": "aac\r\n"}, {"input": "az\r\naaazazaa\r\n", "output": "a\r\n"}, {"input": "bbacaabbaaa\r\nacaabcaa\r\n", "output": "acaabaa\r\n"}, {"input": "c\r\ncbcbcbbacacacbccaaccbcabaaabbaaa\r\n", "output": "c\r\n"}, {"input": "bacb\r\nccacacbacbccbbccccaccccccbcbabbbaababa\r\n", "output": "ba\r\n"}, {"input": "ac\r\naacacaacbaaacbbbabacaca\r\n", "output": "a\r\n"}, {"input": "a\r\nzazaa\r\n", "output": "a\r\n"}, {"input": "abcd\r\nfaaaabbbbccccdddeda\r\n", "output": "a\r\n"}, {"input": "abcde\r\nfabcde\r\n", "output": "abcde\r\n"}, {"input": "a\r\nab\r\n", "output": "a\r\n"}, {"input": "ababbbbbbbbbbbb\r\nabbbbb\r\n", "output": "abbbbb\r\n"}, {"input": "bbbbaabbababbaaaaababbaaabbbbaaabbbababbbbabaabababaabaaabbbabababbbabababaababaaaaa\r\nbbabaaaabaaaabbaaabbbabaaabaabbbababbbbbbbbbbabbababbaababbbaaabababababbbbaaababaaaaab\r\n", "output": "bbbbbbbabbababbaababbbaaabababababbbbaaababaaaaab\r\n"}, {"input": "ab\r\naba\r\n", "output": "ab\r\n"}, {"input": "aa\r\naaaa\r\n", "output": "aa\r\n"}, {"input": "aaaaabbbaaaaa\r\naabbaa\r\n", "output": "aabbaa\r\n"}, {"input": "aaaaaaaaa\r\naaaa\r\n", "output": "aaaa\r\n"}, {"input": "abbcc\r\naca\r\n", "output": "ac\r\n"}, {"input": "b\r\ncb\r\n", "output": "b\r\n"}, {"input": "aac\r\naaa\r\n", "output": "aa\r\n"}, {"input": "ba\r\nbb\r\n", "output": "b\r\n"}, {"input": "a\r\nb\r\n", "output": "-\r\n"}, {"input": "gkvubrvpbhsfiuyha\r\nihotmn\r\n", "output": "ih\r\n"}, {"input": "ccccabccbb\r\ncbbabcc\r\n", "output": "cabcc\r\n"}, {"input": "babababbaaabb\r\nabbab\r\n", "output": "abbab\r\n"}, {"input": "njtdhyqundyedsjyvy\r\nypjrs\r\n", "output": "ys\r\n"}, {"input": "uglyqhkpruxoakm\r\ncixxkpaaoodpuuh\r\n", "output": "uh\r\n"}, {"input": "a\r\naaaaaaaaa\r\n", "output": "a\r\n"}, {"input": "aaa\r\naaaaa\r\n", "output": "aaa\r\n"}, {"input": "abcabbcbcccbccbbcc\r\nacbcaabbbbcabbbaca\r\n", "output": "acbc\r\n"}, {"input": "caacacaacbaa\r\nacbbbabacacac\r\n", "output": "aacacac\r\n"}, {"input": "aa\r\naaab\r\n", "output": "aa\r\n"}, {"input": "acbc\r\ncacacbac\r\n", "output": "ac\r\n"}, {"input": "bacbcaacabbaacb\r\ncbbaaccccbcaacacaabb\r\n", "output": "cbcaabb\r\n"}, {"input": "baababaaaab\r\nbaababbbbbbb\r\n", "output": "baababb\r\n"}, {"input": "aaxyaba\r\naaba\r\n", "output": "aaba\r\n"}]
false
stdio
null
true
982/A
982
A
PyPy 3
TESTS
9
140
0
90464444
lenght=input() lenght=int(lenght) word=input() if lenght == 1: if "0" in word: print("No") else: print("Yes") exit() last_chars = word[lenght-2:] if "00" in last_chars: print("No") exit() if "000" in word: print("No") exit() if "11" in word: print("No") exit() print("Yes")
55
46
0
192774037
n = int(input()) s = input() if len(s)==1: print('No') if s=='0' else print('Yes') elif len(s)==2: print('No') if s=='00' or s=='11' else print('Yes') else: if s.count('11')>0: print('No') else: for i in range(n): if s[i]=='0': s = list(s) s[i]='1' s = ''.join(s) ok = True if s.count('11')>0: ok = False s = list(s) s[i]='0' s = ''.join(s) if ok: print('No') exit() print('Yes')
Codeforces Round 484 (Div. 2)
CF
2,018
1
256
Row
You're given a row with $$$n$$$ chairs. We call a seating of people "maximal" if the two following conditions hold: 1. There are no neighbors adjacent to anyone seated. 2. It's impossible to seat one more person without violating the first rule. The seating is given as a string consisting of zeros and ones ($$$0$$$ means that the corresponding seat is empty, $$$1$$$ — occupied). The goal is to determine whether this seating is "maximal". Note that the first and last seats are not adjacent (if $$$n \ne 2$$$).
The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of chairs. The next line contains a string of $$$n$$$ characters, each of them is either zero or one, describing the seating.
Output "Yes" (without quotation marks) if the seating is "maximal". Otherwise print "No". You are allowed to print letters in whatever case you'd like (uppercase or lowercase).
null
In sample case one the given seating is maximal. In sample case two the person at chair three has a neighbour to the right. In sample case three it is possible to seat yet another person into chair three.
[{"input": "3\n101", "output": "Yes"}, {"input": "4\n1011", "output": "No"}, {"input": "5\n10001", "output": "No"}]
1,200
["brute force", "constructive algorithms"]
55
[{"input": "3\r\n101\r\n", "output": "Yes\r\n"}, {"input": "4\r\n1011\r\n", "output": "No\r\n"}, {"input": "5\r\n10001\r\n", "output": "No\r\n"}, {"input": "1\r\n0\r\n", "output": "No\r\n"}, {"input": "1\r\n1\r\n", "output": "Yes\r\n"}, {"input": "100\r\n0101001010101001010010010101001010100101001001001010010101010010101001001010101001001001010100101010\r\n", "output": "Yes\r\n"}, {"input": "4\r\n0100\r\n", "output": "No\r\n"}, {"input": "42\r\n011000100101001001101011011010100010011010\r\n", "output": "No\r\n"}, {"input": "3\r\n001\r\n", "output": "No\r\n"}, {"input": "64\r\n1001001010010010100101010010010100100101001001001001010100101001\r\n", "output": "Yes\r\n"}, {"input": "3\r\n111\r\n", "output": "No\r\n"}, {"input": "4\r\n0000\r\n", "output": "No\r\n"}, {"input": "4\r\n0001\r\n", "output": "No\r\n"}, {"input": "4\r\n0010\r\n", "output": "No\r\n"}, {"input": "4\r\n0011\r\n", "output": "No\r\n"}, {"input": "4\r\n0101\r\n", "output": "Yes\r\n"}, {"input": "4\r\n0110\r\n", "output": "No\r\n"}, {"input": "4\r\n0111\r\n", "output": "No\r\n"}, {"input": "4\r\n1000\r\n", "output": "No\r\n"}, {"input": "4\r\n1001\r\n", "output": "Yes\r\n"}, {"input": "4\r\n1010\r\n", "output": "Yes\r\n"}, {"input": "4\r\n1100\r\n", "output": "No\r\n"}, {"input": "4\r\n1101\r\n", "output": "No\r\n"}, {"input": "4\r\n1110\r\n", "output": "No\r\n"}, {"input": "4\r\n1111\r\n", "output": "No\r\n"}, {"input": "2\r\n00\r\n", "output": "No\r\n"}, {"input": "2\r\n01\r\n", "output": "Yes\r\n"}, {"input": "2\r\n10\r\n", "output": "Yes\r\n"}, {"input": "2\r\n11\r\n", "output": "No\r\n"}, {"input": "3\r\n000\r\n", "output": "No\r\n"}, {"input": "3\r\n010\r\n", "output": "Yes\r\n"}, {"input": "3\r\n011\r\n", "output": "No\r\n"}, {"input": "3\r\n100\r\n", "output": "No\r\n"}, {"input": "3\r\n110\r\n", "output": "No\r\n"}, {"input": "100\r\n0111001010101110001100000010011000100101110010001100111110101110001110101010111000010010011000000110\r\n", "output": "No\r\n"}, {"input": "357\r\n100101010010010010010100101001001010101010100100100100101001010101001010010100101001010100101001010010100100101001010101010101001001010100101010010100101001010100100100101010010010010100101010010010101001010010010101001001010010010101010100100101010010100100101001010100101001010100101001010010010010100101001010100100100100100100100101010101010010010100101\r\n", "output": "Yes\r\n"}, {"input": "459\r\n000111000101010000100001001010111110011011010001100101111010111011101110111101111101100101100011011001100110001001111001101000111001011100110100011111011111000010000110010011100110011011111110011100001101001111000100111011001000001011111100110100001001001100101011100001110110100101011011110100100111101011000101110000100110100100010000000100001001111111000011101010010011001111010111001100000100111001010111011010000011000011100101101011101000011011000110011\r\n", "output": "No\r\n"}, {"input": "3\r\n001\r\n", "output": "No\r\n"}, {"input": "4\r\n0010\r\n", "output": "No\r\n"}, {"input": "3\r\n111\r\n", "output": "No\r\n"}, {"input": "4\r\n0110\r\n", "output": "No\r\n"}, {"input": "2\r\n11\r\n", "output": "No\r\n"}, {"input": "2\r\n10\r\n", "output": "Yes\r\n"}, {"input": "2\r\n01\r\n", "output": "Yes\r\n"}, {"input": "4\r\n1101\r\n", "output": "No\r\n"}, {"input": "2\r\n00\r\n", "output": "No\r\n"}, {"input": "4\r\n1111\r\n", "output": "No\r\n"}, {"input": "15\r\n000010101010000\r\n", "output": "No\r\n"}, {"input": "8\r\n10010100\r\n", "output": "No\r\n"}, {"input": "5\r\n00100\r\n", "output": "No\r\n"}, {"input": "6\r\n100100\r\n", "output": "No\r\n"}, {"input": "7\r\n1000001\r\n", "output": "No\r\n"}, {"input": "98\r\n10101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010\r\n", "output": "Yes\r\n"}]
false
stdio
null
true
985/B
985
B
PyPy 3-64
TESTS
26
311
93,593,600
224853496
class Switch: def __init__(self, s): self.data = set() for i, ch in enumerate(s): if ch == '1': self.data.add(i) def __len__(self): return len(self.data) def __ior__(self, other): self.data |= other.data return self def main(): n, m = map(int, input().split()) switches = [Switch(input()) for _ in range(n)] switches.sort(key=len, reverse=True) base_switch = Switch('') for switch in switches: prev_len = len(base_switch) base_switch |= switch if len(base_switch) == prev_len: print("YES") return print("NO") main()
67
109
921,600
38563718
def main(): n, m = map(int, input().split()) switches = [int(input(), 2) for _ in range(n)] lambs_on = 0 duplicate_control = 0 for lambs_turn_on in switches: duplicate_control |= lambs_on & lambs_turn_on lambs_on |= lambs_turn_on if any(lambs_turn_on & lambs_on & ~duplicate_control == 0 for lambs_turn_on in switches): print('YES') else: print('NO') if __name__ == '__main__': main()
Educational Codeforces Round 44 (Rated for Div. 2)
ICPC
2,018
3
256
Switches and Lamps
You are given n switches and m lamps. The i-th switch turns on some subset of the lamps. This information is given as the matrix a consisting of n rows and m columns where ai, j = 1 if the i-th switch turns on the j-th lamp and ai, j = 0 if the i-th switch is not connected to the j-th lamp. Initially all m lamps are turned off. Switches change state only from "off" to "on". It means that if you press two or more switches connected to the same lamp then the lamp will be turned on after any of this switches is pressed and will remain its state even if any switch connected to this lamp is pressed afterwards. It is guaranteed that if you push all n switches then all m lamps will be turned on. Your think that you have too many switches and you would like to ignore one of them. Your task is to say if there exists such a switch that if you will ignore (not use) it but press all the other n - 1 switches then all the m lamps will be turned on.
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 2000) — the number of the switches and the number of the lamps. The following n lines contain m characters each. The character ai, j is equal to '1' if the i-th switch turns on the j-th lamp and '0' otherwise. It is guaranteed that if you press all n switches all m lamps will be turned on.
Print "YES" if there is a switch that if you will ignore it and press all the other n - 1 switches then all m lamps will be turned on. Print "NO" if there is no such switch.
null
null
[{"input": "4 5\n10101\n01000\n00111\n10000", "output": "YES"}, {"input": "4 5\n10100\n01000\n00110\n00101", "output": "NO"}]
1,200
["implementation"]
67
[{"input": "4 5\r\n10101\r\n01000\r\n00111\r\n10000\r\n", "output": "YES\r\n"}, {"input": "4 5\r\n10100\r\n01000\r\n00110\r\n00101\r\n", "output": "NO\r\n"}, {"input": "1 5\r\n11111\r\n", "output": "NO\r\n"}, {"input": "10 1\r\n1\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n1\r\n", "output": "YES\r\n"}, {"input": "1 1\r\n1\r\n", "output": "NO\r\n"}, {"input": "3 4\r\n1010\r\n0100\r\n1101\r\n", "output": "YES\r\n"}, {"input": "2 5\r\n10101\r\n11111\r\n", "output": "YES\r\n"}, {"input": "5 5\r\n10000\r\n11000\r\n11100\r\n11110\r\n11111\r\n", "output": "YES\r\n"}, {"input": "2 5\r\n10000\r\n11111\r\n", "output": "YES\r\n"}, {"input": "4 5\r\n01000\r\n10100\r\n00010\r\n10101\r\n", "output": "YES\r\n"}, {"input": "2 2\r\n10\r\n11\r\n", "output": "YES\r\n"}, {"input": "2 5\r\n00100\r\n11111\r\n", "output": "YES\r\n"}, {"input": "4 5\r\n00000\r\n11000\r\n00110\r\n00011\r\n", "output": "YES\r\n"}, {"input": "4 3\r\n000\r\n010\r\n001\r\n100\r\n", "output": "YES\r\n"}, {"input": "4 5\r\n10000\r\n10101\r\n01000\r\n00111\r\n", "output": "YES\r\n"}, {"input": "4 5\r\n10000\r\n01000\r\n10101\r\n00111\r\n", "output": "YES\r\n"}, {"input": "2 2\r\n01\r\n11\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n010\r\n101\r\n000\r\n", "output": "YES\r\n"}, {"input": "2 2\r\n11\r\n00\r\n", "output": "YES\r\n"}, {"input": "3 5\r\n10110\r\n11000\r\n00111\r\n", "output": "YES\r\n"}, {"input": "3 8\r\n00111111\r\n01011100\r\n11000000\r\n", "output": "YES\r\n"}, {"input": "4 6\r\n100000\r\n110000\r\n001100\r\n000011\r\n", "output": "YES\r\n"}, {"input": "2 5\r\n11111\r\n00000\r\n", "output": "YES\r\n"}, {"input": "2 3\r\n101\r\n111\r\n", "output": "YES\r\n"}, {"input": "2 5\r\n01000\r\n11111\r\n", "output": "YES\r\n"}, {"input": "2 2\r\n00\r\n11\r\n", "output": "YES\r\n"}, {"input": "4 15\r\n111110100011010\r\n111111011010110\r\n101000001011001\r\n100110000111011\r\n", "output": "YES\r\n"}, {"input": "2 3\r\n010\r\n111\r\n", "output": "YES\r\n"}, {"input": "4 5\r\n10100\r\n11000\r\n00110\r\n00101\r\n", "output": "YES\r\n"}, {"input": "4 4\r\n1111\r\n0000\r\n0000\r\n0000\r\n", "output": "YES\r\n"}, {"input": "3 5\r\n11100\r\n00110\r\n00011\r\n", "output": "YES\r\n"}, {"input": "2 1\r\n0\r\n1\r\n", "output": "YES\r\n"}, {"input": "4 4\r\n1000\r\n1001\r\n0010\r\n0100\r\n", "output": "YES\r\n"}, {"input": "3 5\r\n00110\r\n10011\r\n01100\r\n", "output": "YES\r\n"}, {"input": "3 5\r\n10101\r\n00111\r\n01000\r\n", "output": "NO\r\n"}, {"input": "4 5\r\n00101\r\n00011\r\n01000\r\n10010\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n100\r\n110\r\n111\r\n", "output": "YES\r\n"}, {"input": "2 2\r\n11\r\n01\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n100\r\n100\r\n111\r\n", "output": "YES\r\n"}, {"input": "4 2\r\n10\r\n01\r\n10\r\n01\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n111\r\n000\r\n000\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n010\r\n100\r\n011\r\n", "output": "YES\r\n"}, {"input": "2 3\r\n111\r\n000\r\n", "output": "YES\r\n"}, {"input": "3 4\r\n0001\r\n1101\r\n1010\r\n", "output": "YES\r\n"}, {"input": "3 4\r\n1010\r\n0101\r\n1000\r\n", "output": "YES\r\n"}, {"input": "3 4\r\n0001\r\n1101\r\n0110\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n111\r\n101\r\n001\r\n", "output": "YES\r\n"}, {"input": "4 5\r\n10001\r\n10010\r\n01010\r\n00101\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n000\r\n000\r\n111\r\n", "output": "YES\r\n"}, {"input": "2 3\r\n100\r\n111\r\n", "output": "YES\r\n"}, {"input": "3 10\r\n1111011100\r\n0001100011\r\n1111010101\r\n", "output": "YES\r\n"}, {"input": "3 4\r\n0110\r\n1010\r\n0101\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n100\r\n001\r\n011\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n100\r\n010\r\n001\r\n", "output": "NO\r\n"}, {"input": "3 3\r\n010\r\n100\r\n001\r\n", "output": "NO\r\n"}, {"input": "3 5\r\n10101\r\n01010\r\n01010\r\n", "output": "YES\r\n"}, {"input": "2 6\r\n111111\r\n000000\r\n", "output": "YES\r\n"}, {"input": "3 5\r\n00000\r\n10101\r\n01010\r\n", "output": "YES\r\n"}, {"input": "4 6\r\n111000\r\n100100\r\n010010\r\n001001\r\n", "output": "YES\r\n"}]
false
stdio
null
true
985/B
985
B
Python 3
TESTS
26
140
819,200
39063467
n=list(map(int,input().split())) l,r=n[0],n[1] arr=[] gf=0 for x in range(l): arr.append(int(input(),2)) for x in range(r): gf=gf+pow(2,x) mx=max(arr) k=0 for x in range(0,l): if(bin(mx)!=bin(mx|arr[x])): mx=(mx|arr[x]) k=k+1 k=k+1 print('YES') if(k<l and mx==gf ) else print('NO')
67
109
5,222,400
38520749
input() a = tuple(map(lambda s: int(s.strip(), 2), __import__('sys').stdin.readlines())) t = T = 0 for x in a: t |= T & x T |= x print(['YES', 'NO'][all(x & (x ^ t) for x in a)])
Educational Codeforces Round 44 (Rated for Div. 2)
ICPC
2,018
3
256
Switches and Lamps
You are given n switches and m lamps. The i-th switch turns on some subset of the lamps. This information is given as the matrix a consisting of n rows and m columns where ai, j = 1 if the i-th switch turns on the j-th lamp and ai, j = 0 if the i-th switch is not connected to the j-th lamp. Initially all m lamps are turned off. Switches change state only from "off" to "on". It means that if you press two or more switches connected to the same lamp then the lamp will be turned on after any of this switches is pressed and will remain its state even if any switch connected to this lamp is pressed afterwards. It is guaranteed that if you push all n switches then all m lamps will be turned on. Your think that you have too many switches and you would like to ignore one of them. Your task is to say if there exists such a switch that if you will ignore (not use) it but press all the other n - 1 switches then all the m lamps will be turned on.
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 2000) — the number of the switches and the number of the lamps. The following n lines contain m characters each. The character ai, j is equal to '1' if the i-th switch turns on the j-th lamp and '0' otherwise. It is guaranteed that if you press all n switches all m lamps will be turned on.
Print "YES" if there is a switch that if you will ignore it and press all the other n - 1 switches then all m lamps will be turned on. Print "NO" if there is no such switch.
null
null
[{"input": "4 5\n10101\n01000\n00111\n10000", "output": "YES"}, {"input": "4 5\n10100\n01000\n00110\n00101", "output": "NO"}]
1,200
["implementation"]
67
[{"input": "4 5\r\n10101\r\n01000\r\n00111\r\n10000\r\n", "output": "YES\r\n"}, {"input": "4 5\r\n10100\r\n01000\r\n00110\r\n00101\r\n", "output": "NO\r\n"}, {"input": "1 5\r\n11111\r\n", "output": "NO\r\n"}, {"input": "10 1\r\n1\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n1\r\n", "output": "YES\r\n"}, {"input": "1 1\r\n1\r\n", "output": "NO\r\n"}, {"input": "3 4\r\n1010\r\n0100\r\n1101\r\n", "output": "YES\r\n"}, {"input": "2 5\r\n10101\r\n11111\r\n", "output": "YES\r\n"}, {"input": "5 5\r\n10000\r\n11000\r\n11100\r\n11110\r\n11111\r\n", "output": "YES\r\n"}, {"input": "2 5\r\n10000\r\n11111\r\n", "output": "YES\r\n"}, {"input": "4 5\r\n01000\r\n10100\r\n00010\r\n10101\r\n", "output": "YES\r\n"}, {"input": "2 2\r\n10\r\n11\r\n", "output": "YES\r\n"}, {"input": "2 5\r\n00100\r\n11111\r\n", "output": "YES\r\n"}, {"input": "4 5\r\n00000\r\n11000\r\n00110\r\n00011\r\n", "output": "YES\r\n"}, {"input": "4 3\r\n000\r\n010\r\n001\r\n100\r\n", "output": "YES\r\n"}, {"input": "4 5\r\n10000\r\n10101\r\n01000\r\n00111\r\n", "output": "YES\r\n"}, {"input": "4 5\r\n10000\r\n01000\r\n10101\r\n00111\r\n", "output": "YES\r\n"}, {"input": "2 2\r\n01\r\n11\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n010\r\n101\r\n000\r\n", "output": "YES\r\n"}, {"input": "2 2\r\n11\r\n00\r\n", "output": "YES\r\n"}, {"input": "3 5\r\n10110\r\n11000\r\n00111\r\n", "output": "YES\r\n"}, {"input": "3 8\r\n00111111\r\n01011100\r\n11000000\r\n", "output": "YES\r\n"}, {"input": "4 6\r\n100000\r\n110000\r\n001100\r\n000011\r\n", "output": "YES\r\n"}, {"input": "2 5\r\n11111\r\n00000\r\n", "output": "YES\r\n"}, {"input": "2 3\r\n101\r\n111\r\n", "output": "YES\r\n"}, {"input": "2 5\r\n01000\r\n11111\r\n", "output": "YES\r\n"}, {"input": "2 2\r\n00\r\n11\r\n", "output": "YES\r\n"}, {"input": "4 15\r\n111110100011010\r\n111111011010110\r\n101000001011001\r\n100110000111011\r\n", "output": "YES\r\n"}, {"input": "2 3\r\n010\r\n111\r\n", "output": "YES\r\n"}, {"input": "4 5\r\n10100\r\n11000\r\n00110\r\n00101\r\n", "output": "YES\r\n"}, {"input": "4 4\r\n1111\r\n0000\r\n0000\r\n0000\r\n", "output": "YES\r\n"}, {"input": "3 5\r\n11100\r\n00110\r\n00011\r\n", "output": "YES\r\n"}, {"input": "2 1\r\n0\r\n1\r\n", "output": "YES\r\n"}, {"input": "4 4\r\n1000\r\n1001\r\n0010\r\n0100\r\n", "output": "YES\r\n"}, {"input": "3 5\r\n00110\r\n10011\r\n01100\r\n", "output": "YES\r\n"}, {"input": "3 5\r\n10101\r\n00111\r\n01000\r\n", "output": "NO\r\n"}, {"input": "4 5\r\n00101\r\n00011\r\n01000\r\n10010\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n100\r\n110\r\n111\r\n", "output": "YES\r\n"}, {"input": "2 2\r\n11\r\n01\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n100\r\n100\r\n111\r\n", "output": "YES\r\n"}, {"input": "4 2\r\n10\r\n01\r\n10\r\n01\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n111\r\n000\r\n000\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n010\r\n100\r\n011\r\n", "output": "YES\r\n"}, {"input": "2 3\r\n111\r\n000\r\n", "output": "YES\r\n"}, {"input": "3 4\r\n0001\r\n1101\r\n1010\r\n", "output": "YES\r\n"}, {"input": "3 4\r\n1010\r\n0101\r\n1000\r\n", "output": "YES\r\n"}, {"input": "3 4\r\n0001\r\n1101\r\n0110\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n111\r\n101\r\n001\r\n", "output": "YES\r\n"}, {"input": "4 5\r\n10001\r\n10010\r\n01010\r\n00101\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n000\r\n000\r\n111\r\n", "output": "YES\r\n"}, {"input": "2 3\r\n100\r\n111\r\n", "output": "YES\r\n"}, {"input": "3 10\r\n1111011100\r\n0001100011\r\n1111010101\r\n", "output": "YES\r\n"}, {"input": "3 4\r\n0110\r\n1010\r\n0101\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n100\r\n001\r\n011\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n100\r\n010\r\n001\r\n", "output": "NO\r\n"}, {"input": "3 3\r\n010\r\n100\r\n001\r\n", "output": "NO\r\n"}, {"input": "3 5\r\n10101\r\n01010\r\n01010\r\n", "output": "YES\r\n"}, {"input": "2 6\r\n111111\r\n000000\r\n", "output": "YES\r\n"}, {"input": "3 5\r\n00000\r\n10101\r\n01010\r\n", "output": "YES\r\n"}, {"input": "4 6\r\n111000\r\n100100\r\n010010\r\n001001\r\n", "output": "YES\r\n"}]
false
stdio
null
true
985/B
985
B
Python 3
TESTS
26
2,605
74,956,800
38547910
a=[int(i) for i in input().split()] n=a[0] m=a[1] a=[] for i in range(n): s=str(input()) y=[] for j in range(m): if(s[j]=='1'): y.append(j) a.append(y) a.sort(key=len,reverse=True) b=[False]*(m) i=0 count=0 while(i<=n-1): flag=0 for j in range(len(a[i])): if(b[a[i][j]]==False): b[a[i][j]]=True flag=1 if(flag==1): count+=1 i+=1 if(count==n): print("NO") else: print("YES")
67
124
716,800
38491775
a = [] was_already = was_twice = 0 n = int(input().split()[0]) for _ in range(n): ai = int(input(), base=2) a.append(ai) was_twice |= was_already & ai was_already |= ai for ai in a: if ai & (ai ^ was_twice): continue print("YES") break else: print("NO")
Educational Codeforces Round 44 (Rated for Div. 2)
ICPC
2,018
3
256
Switches and Lamps
You are given n switches and m lamps. The i-th switch turns on some subset of the lamps. This information is given as the matrix a consisting of n rows and m columns where ai, j = 1 if the i-th switch turns on the j-th lamp and ai, j = 0 if the i-th switch is not connected to the j-th lamp. Initially all m lamps are turned off. Switches change state only from "off" to "on". It means that if you press two or more switches connected to the same lamp then the lamp will be turned on after any of this switches is pressed and will remain its state even if any switch connected to this lamp is pressed afterwards. It is guaranteed that if you push all n switches then all m lamps will be turned on. Your think that you have too many switches and you would like to ignore one of them. Your task is to say if there exists such a switch that if you will ignore (not use) it but press all the other n - 1 switches then all the m lamps will be turned on.
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 2000) — the number of the switches and the number of the lamps. The following n lines contain m characters each. The character ai, j is equal to '1' if the i-th switch turns on the j-th lamp and '0' otherwise. It is guaranteed that if you press all n switches all m lamps will be turned on.
Print "YES" if there is a switch that if you will ignore it and press all the other n - 1 switches then all m lamps will be turned on. Print "NO" if there is no such switch.
null
null
[{"input": "4 5\n10101\n01000\n00111\n10000", "output": "YES"}, {"input": "4 5\n10100\n01000\n00110\n00101", "output": "NO"}]
1,200
["implementation"]
67
[{"input": "4 5\r\n10101\r\n01000\r\n00111\r\n10000\r\n", "output": "YES\r\n"}, {"input": "4 5\r\n10100\r\n01000\r\n00110\r\n00101\r\n", "output": "NO\r\n"}, {"input": "1 5\r\n11111\r\n", "output": "NO\r\n"}, {"input": "10 1\r\n1\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n1\r\n", "output": "YES\r\n"}, {"input": "1 1\r\n1\r\n", "output": "NO\r\n"}, {"input": "3 4\r\n1010\r\n0100\r\n1101\r\n", "output": "YES\r\n"}, {"input": "2 5\r\n10101\r\n11111\r\n", "output": "YES\r\n"}, {"input": "5 5\r\n10000\r\n11000\r\n11100\r\n11110\r\n11111\r\n", "output": "YES\r\n"}, {"input": "2 5\r\n10000\r\n11111\r\n", "output": "YES\r\n"}, {"input": "4 5\r\n01000\r\n10100\r\n00010\r\n10101\r\n", "output": "YES\r\n"}, {"input": "2 2\r\n10\r\n11\r\n", "output": "YES\r\n"}, {"input": "2 5\r\n00100\r\n11111\r\n", "output": "YES\r\n"}, {"input": "4 5\r\n00000\r\n11000\r\n00110\r\n00011\r\n", "output": "YES\r\n"}, {"input": "4 3\r\n000\r\n010\r\n001\r\n100\r\n", "output": "YES\r\n"}, {"input": "4 5\r\n10000\r\n10101\r\n01000\r\n00111\r\n", "output": "YES\r\n"}, {"input": "4 5\r\n10000\r\n01000\r\n10101\r\n00111\r\n", "output": "YES\r\n"}, {"input": "2 2\r\n01\r\n11\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n010\r\n101\r\n000\r\n", "output": "YES\r\n"}, {"input": "2 2\r\n11\r\n00\r\n", "output": "YES\r\n"}, {"input": "3 5\r\n10110\r\n11000\r\n00111\r\n", "output": "YES\r\n"}, {"input": "3 8\r\n00111111\r\n01011100\r\n11000000\r\n", "output": "YES\r\n"}, {"input": "4 6\r\n100000\r\n110000\r\n001100\r\n000011\r\n", "output": "YES\r\n"}, {"input": "2 5\r\n11111\r\n00000\r\n", "output": "YES\r\n"}, {"input": "2 3\r\n101\r\n111\r\n", "output": "YES\r\n"}, {"input": "2 5\r\n01000\r\n11111\r\n", "output": "YES\r\n"}, {"input": "2 2\r\n00\r\n11\r\n", "output": "YES\r\n"}, {"input": "4 15\r\n111110100011010\r\n111111011010110\r\n101000001011001\r\n100110000111011\r\n", "output": "YES\r\n"}, {"input": "2 3\r\n010\r\n111\r\n", "output": "YES\r\n"}, {"input": "4 5\r\n10100\r\n11000\r\n00110\r\n00101\r\n", "output": "YES\r\n"}, {"input": "4 4\r\n1111\r\n0000\r\n0000\r\n0000\r\n", "output": "YES\r\n"}, {"input": "3 5\r\n11100\r\n00110\r\n00011\r\n", "output": "YES\r\n"}, {"input": "2 1\r\n0\r\n1\r\n", "output": "YES\r\n"}, {"input": "4 4\r\n1000\r\n1001\r\n0010\r\n0100\r\n", "output": "YES\r\n"}, {"input": "3 5\r\n00110\r\n10011\r\n01100\r\n", "output": "YES\r\n"}, {"input": "3 5\r\n10101\r\n00111\r\n01000\r\n", "output": "NO\r\n"}, {"input": "4 5\r\n00101\r\n00011\r\n01000\r\n10010\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n100\r\n110\r\n111\r\n", "output": "YES\r\n"}, {"input": "2 2\r\n11\r\n01\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n100\r\n100\r\n111\r\n", "output": "YES\r\n"}, {"input": "4 2\r\n10\r\n01\r\n10\r\n01\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n111\r\n000\r\n000\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n010\r\n100\r\n011\r\n", "output": "YES\r\n"}, {"input": "2 3\r\n111\r\n000\r\n", "output": "YES\r\n"}, {"input": "3 4\r\n0001\r\n1101\r\n1010\r\n", "output": "YES\r\n"}, {"input": "3 4\r\n1010\r\n0101\r\n1000\r\n", "output": "YES\r\n"}, {"input": "3 4\r\n0001\r\n1101\r\n0110\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n111\r\n101\r\n001\r\n", "output": "YES\r\n"}, {"input": "4 5\r\n10001\r\n10010\r\n01010\r\n00101\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n000\r\n000\r\n111\r\n", "output": "YES\r\n"}, {"input": "2 3\r\n100\r\n111\r\n", "output": "YES\r\n"}, {"input": "3 10\r\n1111011100\r\n0001100011\r\n1111010101\r\n", "output": "YES\r\n"}, {"input": "3 4\r\n0110\r\n1010\r\n0101\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n100\r\n001\r\n011\r\n", "output": "YES\r\n"}, {"input": "3 3\r\n100\r\n010\r\n001\r\n", "output": "NO\r\n"}, {"input": "3 3\r\n010\r\n100\r\n001\r\n", "output": "NO\r\n"}, {"input": "3 5\r\n10101\r\n01010\r\n01010\r\n", "output": "YES\r\n"}, {"input": "2 6\r\n111111\r\n000000\r\n", "output": "YES\r\n"}, {"input": "3 5\r\n00000\r\n10101\r\n01010\r\n", "output": "YES\r\n"}, {"input": "4 6\r\n111000\r\n100100\r\n010010\r\n001001\r\n", "output": "YES\r\n"}]
false
stdio
null
true
812/B
812
B
Python 3
TESTS
42
124
307,200
76990537
n,m=list(map(int,input().split())) Li=[] for i in range(n): Li.append(input()) on=[] L=[] R=[] for s in Li[::-1]: c=0 count=1 p=-1 for i in s: if c==0 and i=='1': c=1 L.append(count) on.append(1) if i=='1': p=count count+=1 if p==-1: L.append(-1) R.append(-1) on.append(-1) else: R.append(p) c=0 curr=1 if on[0]!=-1: c+=R[0]-curr curr=R[0] stairs=1 for i in range(1,n): if on[i]!=-1: a=(m+2-L[i])+stairs+(m+2-curr) b=R[i]-1+stairs+curr-1 stairs=1 if a<b: c+=a curr=L[i] else: c+=b curr=R[i] else: stairs+=1 print(c)
72
46
0
186754062
n, m = map(int, input().split()) p = [input() for y in range(n)][::-1] l = r = d = 0 i = j = 0 for y, t in enumerate(p): if '1' in t: l, r = min(l - i, r - j) + 2 * m + 2, min(l + i, r + j) i, j = t.find('1'), t.rfind('1') l, r, d = l - i, r + j, y print(min(l, r) + d)
Codeforces Round 417 (Div. 2)
CF
2,017
1
256
Sagheer, the Hausmeister
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off. The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms. Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights. Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
The first line contains two integers n and m (1 ≤ n ≤ 15 and 1 ≤ m ≤ 100) — the number of floors and the number of rooms in each floor, respectively. The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor. The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
Print a single integer — the minimum total time needed to turn off all the lights.
null
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs. In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor. In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor.
[{"input": "2 2\n0010\n0100", "output": "5"}, {"input": "3 4\n001000\n000010\n000010", "output": "12"}, {"input": "4 3\n01110\n01110\n01110\n01110", "output": "18"}]
1,600
["bitmasks", "brute force", "dp"]
72
[{"input": "2 2\r\n0010\r\n0100\r\n", "output": "5\r\n"}, {"input": "3 4\r\n001000\r\n000010\r\n000010\r\n", "output": "12\r\n"}, {"input": "4 3\r\n01110\r\n01110\r\n01110\r\n01110\r\n", "output": "18\r\n"}, {"input": "3 2\r\n0000\r\n0100\r\n0100\r\n", "output": "4\r\n"}, {"input": "1 89\r\n0000000000000000000000000000000100000000000000010000000000010000000000000000000000000000000\r\n", "output": "59\r\n"}, {"input": "2 73\r\n000000000000000000000000000000000000000000000000000000000000000000000000000\r\n000000000000000000000000000000000000000100000010000000000000000000000000000\r\n", "output": "46\r\n"}, {"input": "3 61\r\n000000000000000000000000000000000000000000000000000000000000000\r\n000000000000000000000000000000000000000000000000000000000000000\r\n000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "0\r\n"}, {"input": "4 53\r\n0000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000\r\n", "output": "0\r\n"}, {"input": "5 93\r\n00000000000000000000000000000000000000000000000000000000100000000000000000000000000000000001010\r\n00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n00000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n00000000000000000000000000000010000000000000000000100000000000000000000000000000000000000000000\r\n00000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "265\r\n"}, {"input": "6 77\r\n0000000000000000100000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000010000000000000\r\n0000000000010000000000000000000000000000000000000000000000000000000000000000010\r\n0000000000000000000001000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000100000000000000000000000000000\r\n", "output": "311\r\n"}, {"input": "7 65\r\n0000000001000000000000000010000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000001000001000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "62\r\n"}, {"input": "8 57\r\n00000000100000000000000000000000000000000000000000000000000\r\n00000000000000010000000000000000000000000000000000000000000\r\n00000000000000000000000000000000000100000000000000000000000\r\n00000000000000000000000000000000000000000000000000000000000\r\n00000000000000000000000000000000000100000000000000000000000\r\n00000000000000000000000000000000000000000000000000000000000\r\n00000000000010000000000000000000000000000000000000000000000\r\n00000000000000000000000000000000000000000000000001000000000\r\n", "output": "277\r\n"}, {"input": "12 13\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n010000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000010000000000\r\n000000000000000\r\n", "output": "14\r\n"}, {"input": "13 1\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n", "output": "0\r\n"}, {"input": "1 33\r\n00000100101110001101000000110100010\r\n", "output": "33\r\n"}, {"input": "2 21\r\n00100110100010010010010\r\n01000001111001010000000\r\n", "output": "43\r\n"}, {"input": "3 5\r\n0001010\r\n0100000\r\n0100000\r\n", "output": "11\r\n"}, {"input": "4 45\r\n00010000101101100000101101000000100000001101100\r\n01110000100111010011000000100000000001000001100\r\n00000000001000100110100001000010011010001010010\r\n01111110100100000101101010011000100100001000000\r\n", "output": "184\r\n"}, {"input": "5 37\r\n010100000000000000000110000110010000010\r\n001101100010110011101000001010101101110\r\n010000001000100010010100000000001010000\r\n000000000100101000000101100001000001110\r\n000010000000000000100001001000011100110\r\n", "output": "193\r\n"}, {"input": "6 25\r\n011001000100111010000101000\r\n000000000010000010001000010\r\n011001100001100001001001010\r\n000000100000010000000000110\r\n010001100001000001000000010\r\n011000001001010111110000100\r\n", "output": "160\r\n"}, {"input": "7 61\r\n010000111100010100001000011010100001000000000011100000100010000\r\n000010011000001000000100110101010001000000010001100000100100100\r\n000010001000001000000100001000000100100011001110000111000000100\r\n000000000101000011010000011000000101000001011001000011101010010\r\n000010010011000000100000110000001000000101000000101000010000010\r\n000010010101101100100100100011001011101010000101000010000101010\r\n000100001100001001000000001000000001011000110010100000000010110\r\n", "output": "436\r\n"}, {"input": "8 49\r\n000100100000000111110010011100110100010010000011000\r\n001000000101111000000001111100010010100000010000000\r\n000000010000011100001000000000101000110010000100100\r\n000000000001000110000011101101000000100000101010000\r\n000000110001000101101000000001000000110001000110000\r\n000100000000000000100100010011000001111101010100110\r\n000000001000000010101111000100001100000000010111000\r\n001000010000110000011100000000100110000010001000000\r\n", "output": "404\r\n"}, {"input": "9 41\r\n0011000000000101001101001000000001110000010\r\n0000110000001010110010110010110010010001000\r\n0001100010100000000001110100100001101000100\r\n0001010101111010000000010010001001011111000\r\n0101000101000011101011000000001100110010000\r\n0001010000000000000001011000000100010101000\r\n0000010011000000001000110001000010110001000\r\n0000100010000110100001000000100010001111100\r\n0000001110100001000001000110001110000100000\r\n", "output": "385\r\n"}, {"input": "10 29\r\n0000000000101001100001001011000\r\n0001110100000000000000100010000\r\n0010001001000011000100010001000\r\n0001000010101000000010100010100\r\n0111000000000000100100100010100\r\n0001000100011111000100010100000\r\n0000000000000001000001001011000\r\n0000101110000001010001011001110\r\n0000001000101010011000001100100\r\n0100010000101011010000000000000\r\n", "output": "299\r\n"}, {"input": "1 57\r\n00011101100001110001111000000100101111000111101100111001000\r\n", "output": "55\r\n"}, {"input": "2 32\r\n0011110111011011011101111101011110\r\n0111000110111111011110011101011110\r\n", "output": "65\r\n"}, {"input": "3 20\r\n0110011111110101101100\r\n0111110000111010100100\r\n0110111110010100011110\r\n", "output": "63\r\n"}, {"input": "4 4\r\n011100\r\n001010\r\n010000\r\n011110\r\n", "output": "22\r\n"}, {"input": "5 44\r\n0001010010001111111001111111000010100100000010\r\n0001111001111001101111011111010110001001111110\r\n0111111010111111011101100011101010100101110110\r\n0011010011101011101111001001010110000111111100\r\n0110100111011100110101110010010011011101100100\r\n", "output": "228\r\n"}, {"input": "6 36\r\n01110101111111110101011000011111110010\r\n00011101100010110111111111110001100100\r\n00001111110010111111101110101110111110\r\n00110110011100100111011110000000000010\r\n01100101101001010001011111100111101100\r\n00011111111011001000011001011110011110\r\n", "output": "226\r\n"}, {"input": "7 24\r\n01111001111001011010010100\r\n00111011010101000111101000\r\n01001110110010010110011110\r\n00000101111011011111111000\r\n01111111101111001001010010\r\n01110000111101011111111010\r\n00000100011100110000110000\r\n", "output": "179\r\n"}, {"input": "8 8\r\n0011101110\r\n0110010100\r\n0100111110\r\n0111111100\r\n0011010100\r\n0001101110\r\n0111100000\r\n0110111000\r\n", "output": "77\r\n"}, {"input": "9 48\r\n00011010111110111011111001111111111101001111110010\r\n01000101000101101101111110111101011100001011010010\r\n00110111110110101110101110111111011011101111011000\r\n00110111111100010110110110111001001111011010101110\r\n01111111100101010011111100100111110011001101110100\r\n01111011110011111101010101010100001110111111111000\r\n01110101101101110001000010110100010110101111111100\r\n00111101001010110010110100000111110101010100001000\r\n00011011010110011111001100111100100011100110110100\r\n", "output": "448\r\n"}, {"input": "10 40\r\n010011001001111011011011101111010001010010\r\n011000000110000010001011111010100000110000\r\n011010101001110010110110011111010101101000\r\n000111111010101111000110011111011011011010\r\n010110101110001001001111111000110011101010\r\n010011010100111110010100100111100111011110\r\n001111101100111111111111001010111010000110\r\n001111110010101100110100101110001011100110\r\n010111010010001111110101111111111110111000\r\n011101101111000100111111111001111100111010\r\n", "output": "418\r\n"}, {"input": "11 28\r\n011100111101101001011111001110\r\n010001111110011101101011001000\r\n001010011011011010101101101100\r\n001100011001101011011001110100\r\n010111110011101110000110111100\r\n010010001111110000011111010100\r\n001011111111110011101101111010\r\n001101101011100100011011001110\r\n001111110110100110101011000010\r\n000101101011100001101101100100\r\n010011101101111011100111110100\r\n", "output": "328\r\n"}, {"input": "1 68\r\n0101111110111111111111111111110111111111111111111110111111101111111110\r\n", "output": "68\r\n"}, {"input": "2 56\r\n0011111111111110111111111111111111011111111111011111011110\r\n0111111111010111111111110111111111111110111111010111111110\r\n", "output": "113\r\n"}, {"input": "3 17\r\n0111111101111111110\r\n0111111111101011110\r\n0101111111111111110\r\n", "output": "55\r\n"}, {"input": "4 4\r\n011110\r\n010110\r\n010110\r\n011110\r\n", "output": "22\r\n"}, {"input": "5 89\r\n0011111111111101110110111111111101111011111011101110111111111111111111111111111111111111110\r\n0111111111111111111111111101111111111111111111111111111111111111111111111111111111111111110\r\n0111111111111011111111111111111111101111011111111111111111110110111101111111111111111011010\r\n0111111111111111011011111111111011111111111111111111111111111111111111111111111110111111010\r\n0111111101111011111110101011111111110111100100101111111011111111111111011011101111111111110\r\n", "output": "453\r\n"}, {"input": "6 77\r\n0111111110101011111111111111111111111111111111111111100111111111101111111111110\r\n0111111111111111111101111101111111111011111111011111111001011111111111101111110\r\n0111101111111111111111111111111111111110110011111111111011111111101111111111110\r\n0111110111111111111111111111111111111111111111111111011011111111111111111111110\r\n0101111110111111111111111111111111111111111011111111111111111111101111011011110\r\n0110111111101111110111111111111011111111101011111101111111111111111111110111100\r\n", "output": "472\r\n"}, {"input": "7 20\r\n0111111111111111111100\r\n0111110111111111111110\r\n0111111111111111111100\r\n0111111011111111111110\r\n0111111111111011101110\r\n0111101011110111111010\r\n0111111111111111111010\r\n", "output": "151\r\n"}, {"input": "8 8\r\n0111111110\r\n0111101110\r\n0111111110\r\n0111111110\r\n0111111110\r\n0110111100\r\n0101111110\r\n0110111110\r\n", "output": "78\r\n"}, {"input": "11 24\r\n01111111111101111111111110\r\n01111111111111111111111110\r\n01110111111111111111111110\r\n01111111111111111111011110\r\n01111111111111111110111110\r\n01111010111111100111101110\r\n01111111111111010101111100\r\n01111111111111110111111110\r\n01011101111111111101111110\r\n00111111011111111110111110\r\n01111111101111111101111110\r\n", "output": "284\r\n"}, {"input": "12 12\r\n01111111111000\r\n01101111110110\r\n01111110111110\r\n01111111111110\r\n01111111111010\r\n01011111110110\r\n01111111111110\r\n01101101011110\r\n01111111111110\r\n01111101011110\r\n00111111111110\r\n01111111011110\r\n", "output": "166\r\n"}, {"input": "15 28\r\n011111111101011111111101111110\r\n011111111111111111111111111110\r\n011101110111011011101111011110\r\n011111111011111011110111111110\r\n011111111110101111111111111110\r\n011111011111110011111111011010\r\n011110111111001101111111111110\r\n011111111110111111111011111110\r\n011111111111111111111111011110\r\n011111011111111111111011001010\r\n011111111101111111111101111110\r\n011111111110111111101111011110\r\n010111111111101111111111111110\r\n011111111111111111011111111110\r\n011011111111111110110111110110\r\n", "output": "448\r\n"}, {"input": "2 11\r\n0100000000000\r\n0000000010000\r\n", "output": "18\r\n"}, {"input": "1 100\r\n010010010011100001101101110111101010000101010001111001001101011110000011101110101000100111111001101110\r\n", "output": "100\r\n"}, {"input": "15 1\r\n010\r\n010\r\n010\r\n010\r\n010\r\n010\r\n000\r\n000\r\n000\r\n010\r\n000\r\n010\r\n000\r\n000\r\n000\r\n", "output": "29\r\n"}, {"input": "3 3\r\n00010\r\n00000\r\n00010\r\n", "output": "7\r\n"}]
false
stdio
null
true
812/B
812
B
PyPy 3
TESTS
42
202
23,347,200
28807908
n, m = list(map(int, input().split())) house = [] def look_right(position, house): x, y = position l = len(house[y]) count = l - x ps = 0 while ps == 0: pos = [l-1, y - 1] for i in range(l-1, -1, -1): if house[y-1][i] == 1: pos[0] = i ps += 1 if ps > 0: count += l-pos[0]-1 return {'cnt': count, 'position': pos, 'light': ps} else: y -= 1 count += 1 def look_left(position, house): x, y = position l = len(house[y]) count = x + 1 ps = 0 while ps == 0: pos = [0, y - 1] for i in range(0, l): if house[y-1][i] == 1: pos[0] = i ps += 1 if ps > 0: count += pos[0] return {'cnt': count, 'position': pos, 'light': ps} else: count += 1 y -= 1 lights = 0 for _ in range(n): rooms = list(map(int, list(input()))) for i in rooms: if i == 1: lights += 1 house.append(rooms) position = [0, n-1] passed = 0 total = 0 while position[1] >= 0: for i in range(len(house[0])): if house[position[1]][i] == 1: position[0] = i total=(i+n-position[1]-1) passed+=1 if total==0: position[1]-=1 else: break #print(position) #print(lights) #print(passed) while passed < lights: l = look_left(position, house) r = look_right(position, house) #print(l) #print(r) if l['cnt']<r['cnt']: passed += l['light'] position = l['position'] total += l['cnt'] else: passed += r['light'] position = r['position'] total += r['cnt'] #print(passed) #print('----') print(total)
72
46
0
230134682
n_and_m = input() n, m = n_and_m.split() #n - кол-во целых чисел, k - кол-во элем. заданной посл-ти меньше либо равны x(результат программы) n = int(n) m = int(m) p = [input() for y in range(n)][::-1] l = r = d = 0 i = j = 0 for y, t in enumerate(p): if '1' in t: l, r = min(l - i, r - j) + 2 * m + 2, min(l + i, r + j) i = t.find('1') j = t.rfind('1') l, r, d = l - i, r + j, y print(min(l, r) + d)
Codeforces Round 417 (Div. 2)
CF
2,017
1
256
Sagheer, the Hausmeister
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off. The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms. Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights. Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
The first line contains two integers n and m (1 ≤ n ≤ 15 and 1 ≤ m ≤ 100) — the number of floors and the number of rooms in each floor, respectively. The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor. The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
Print a single integer — the minimum total time needed to turn off all the lights.
null
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs. In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor. In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor.
[{"input": "2 2\n0010\n0100", "output": "5"}, {"input": "3 4\n001000\n000010\n000010", "output": "12"}, {"input": "4 3\n01110\n01110\n01110\n01110", "output": "18"}]
1,600
["bitmasks", "brute force", "dp"]
72
[{"input": "2 2\r\n0010\r\n0100\r\n", "output": "5\r\n"}, {"input": "3 4\r\n001000\r\n000010\r\n000010\r\n", "output": "12\r\n"}, {"input": "4 3\r\n01110\r\n01110\r\n01110\r\n01110\r\n", "output": "18\r\n"}, {"input": "3 2\r\n0000\r\n0100\r\n0100\r\n", "output": "4\r\n"}, {"input": "1 89\r\n0000000000000000000000000000000100000000000000010000000000010000000000000000000000000000000\r\n", "output": "59\r\n"}, {"input": "2 73\r\n000000000000000000000000000000000000000000000000000000000000000000000000000\r\n000000000000000000000000000000000000000100000010000000000000000000000000000\r\n", "output": "46\r\n"}, {"input": "3 61\r\n000000000000000000000000000000000000000000000000000000000000000\r\n000000000000000000000000000000000000000000000000000000000000000\r\n000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "0\r\n"}, {"input": "4 53\r\n0000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000\r\n", "output": "0\r\n"}, {"input": "5 93\r\n00000000000000000000000000000000000000000000000000000000100000000000000000000000000000000001010\r\n00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n00000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n00000000000000000000000000000010000000000000000000100000000000000000000000000000000000000000000\r\n00000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "265\r\n"}, {"input": "6 77\r\n0000000000000000100000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000010000000000000\r\n0000000000010000000000000000000000000000000000000000000000000000000000000000010\r\n0000000000000000000001000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000100000000000000000000000000000\r\n", "output": "311\r\n"}, {"input": "7 65\r\n0000000001000000000000000010000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000001000001000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "62\r\n"}, {"input": "8 57\r\n00000000100000000000000000000000000000000000000000000000000\r\n00000000000000010000000000000000000000000000000000000000000\r\n00000000000000000000000000000000000100000000000000000000000\r\n00000000000000000000000000000000000000000000000000000000000\r\n00000000000000000000000000000000000100000000000000000000000\r\n00000000000000000000000000000000000000000000000000000000000\r\n00000000000010000000000000000000000000000000000000000000000\r\n00000000000000000000000000000000000000000000000001000000000\r\n", "output": "277\r\n"}, {"input": "12 13\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n010000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000010000000000\r\n000000000000000\r\n", "output": "14\r\n"}, {"input": "13 1\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n", "output": "0\r\n"}, {"input": "1 33\r\n00000100101110001101000000110100010\r\n", "output": "33\r\n"}, {"input": "2 21\r\n00100110100010010010010\r\n01000001111001010000000\r\n", "output": "43\r\n"}, {"input": "3 5\r\n0001010\r\n0100000\r\n0100000\r\n", "output": "11\r\n"}, {"input": "4 45\r\n00010000101101100000101101000000100000001101100\r\n01110000100111010011000000100000000001000001100\r\n00000000001000100110100001000010011010001010010\r\n01111110100100000101101010011000100100001000000\r\n", "output": "184\r\n"}, {"input": "5 37\r\n010100000000000000000110000110010000010\r\n001101100010110011101000001010101101110\r\n010000001000100010010100000000001010000\r\n000000000100101000000101100001000001110\r\n000010000000000000100001001000011100110\r\n", "output": "193\r\n"}, {"input": "6 25\r\n011001000100111010000101000\r\n000000000010000010001000010\r\n011001100001100001001001010\r\n000000100000010000000000110\r\n010001100001000001000000010\r\n011000001001010111110000100\r\n", "output": "160\r\n"}, {"input": "7 61\r\n010000111100010100001000011010100001000000000011100000100010000\r\n000010011000001000000100110101010001000000010001100000100100100\r\n000010001000001000000100001000000100100011001110000111000000100\r\n000000000101000011010000011000000101000001011001000011101010010\r\n000010010011000000100000110000001000000101000000101000010000010\r\n000010010101101100100100100011001011101010000101000010000101010\r\n000100001100001001000000001000000001011000110010100000000010110\r\n", "output": "436\r\n"}, {"input": "8 49\r\n000100100000000111110010011100110100010010000011000\r\n001000000101111000000001111100010010100000010000000\r\n000000010000011100001000000000101000110010000100100\r\n000000000001000110000011101101000000100000101010000\r\n000000110001000101101000000001000000110001000110000\r\n000100000000000000100100010011000001111101010100110\r\n000000001000000010101111000100001100000000010111000\r\n001000010000110000011100000000100110000010001000000\r\n", "output": "404\r\n"}, {"input": "9 41\r\n0011000000000101001101001000000001110000010\r\n0000110000001010110010110010110010010001000\r\n0001100010100000000001110100100001101000100\r\n0001010101111010000000010010001001011111000\r\n0101000101000011101011000000001100110010000\r\n0001010000000000000001011000000100010101000\r\n0000010011000000001000110001000010110001000\r\n0000100010000110100001000000100010001111100\r\n0000001110100001000001000110001110000100000\r\n", "output": "385\r\n"}, {"input": "10 29\r\n0000000000101001100001001011000\r\n0001110100000000000000100010000\r\n0010001001000011000100010001000\r\n0001000010101000000010100010100\r\n0111000000000000100100100010100\r\n0001000100011111000100010100000\r\n0000000000000001000001001011000\r\n0000101110000001010001011001110\r\n0000001000101010011000001100100\r\n0100010000101011010000000000000\r\n", "output": "299\r\n"}, {"input": "1 57\r\n00011101100001110001111000000100101111000111101100111001000\r\n", "output": "55\r\n"}, {"input": "2 32\r\n0011110111011011011101111101011110\r\n0111000110111111011110011101011110\r\n", "output": "65\r\n"}, {"input": "3 20\r\n0110011111110101101100\r\n0111110000111010100100\r\n0110111110010100011110\r\n", "output": "63\r\n"}, {"input": "4 4\r\n011100\r\n001010\r\n010000\r\n011110\r\n", "output": "22\r\n"}, {"input": "5 44\r\n0001010010001111111001111111000010100100000010\r\n0001111001111001101111011111010110001001111110\r\n0111111010111111011101100011101010100101110110\r\n0011010011101011101111001001010110000111111100\r\n0110100111011100110101110010010011011101100100\r\n", "output": "228\r\n"}, {"input": "6 36\r\n01110101111111110101011000011111110010\r\n00011101100010110111111111110001100100\r\n00001111110010111111101110101110111110\r\n00110110011100100111011110000000000010\r\n01100101101001010001011111100111101100\r\n00011111111011001000011001011110011110\r\n", "output": "226\r\n"}, {"input": "7 24\r\n01111001111001011010010100\r\n00111011010101000111101000\r\n01001110110010010110011110\r\n00000101111011011111111000\r\n01111111101111001001010010\r\n01110000111101011111111010\r\n00000100011100110000110000\r\n", "output": "179\r\n"}, {"input": "8 8\r\n0011101110\r\n0110010100\r\n0100111110\r\n0111111100\r\n0011010100\r\n0001101110\r\n0111100000\r\n0110111000\r\n", "output": "77\r\n"}, {"input": "9 48\r\n00011010111110111011111001111111111101001111110010\r\n01000101000101101101111110111101011100001011010010\r\n00110111110110101110101110111111011011101111011000\r\n00110111111100010110110110111001001111011010101110\r\n01111111100101010011111100100111110011001101110100\r\n01111011110011111101010101010100001110111111111000\r\n01110101101101110001000010110100010110101111111100\r\n00111101001010110010110100000111110101010100001000\r\n00011011010110011111001100111100100011100110110100\r\n", "output": "448\r\n"}, {"input": "10 40\r\n010011001001111011011011101111010001010010\r\n011000000110000010001011111010100000110000\r\n011010101001110010110110011111010101101000\r\n000111111010101111000110011111011011011010\r\n010110101110001001001111111000110011101010\r\n010011010100111110010100100111100111011110\r\n001111101100111111111111001010111010000110\r\n001111110010101100110100101110001011100110\r\n010111010010001111110101111111111110111000\r\n011101101111000100111111111001111100111010\r\n", "output": "418\r\n"}, {"input": "11 28\r\n011100111101101001011111001110\r\n010001111110011101101011001000\r\n001010011011011010101101101100\r\n001100011001101011011001110100\r\n010111110011101110000110111100\r\n010010001111110000011111010100\r\n001011111111110011101101111010\r\n001101101011100100011011001110\r\n001111110110100110101011000010\r\n000101101011100001101101100100\r\n010011101101111011100111110100\r\n", "output": "328\r\n"}, {"input": "1 68\r\n0101111110111111111111111111110111111111111111111110111111101111111110\r\n", "output": "68\r\n"}, {"input": "2 56\r\n0011111111111110111111111111111111011111111111011111011110\r\n0111111111010111111111110111111111111110111111010111111110\r\n", "output": "113\r\n"}, {"input": "3 17\r\n0111111101111111110\r\n0111111111101011110\r\n0101111111111111110\r\n", "output": "55\r\n"}, {"input": "4 4\r\n011110\r\n010110\r\n010110\r\n011110\r\n", "output": "22\r\n"}, {"input": "5 89\r\n0011111111111101110110111111111101111011111011101110111111111111111111111111111111111111110\r\n0111111111111111111111111101111111111111111111111111111111111111111111111111111111111111110\r\n0111111111111011111111111111111111101111011111111111111111110110111101111111111111111011010\r\n0111111111111111011011111111111011111111111111111111111111111111111111111111111110111111010\r\n0111111101111011111110101011111111110111100100101111111011111111111111011011101111111111110\r\n", "output": "453\r\n"}, {"input": "6 77\r\n0111111110101011111111111111111111111111111111111111100111111111101111111111110\r\n0111111111111111111101111101111111111011111111011111111001011111111111101111110\r\n0111101111111111111111111111111111111110110011111111111011111111101111111111110\r\n0111110111111111111111111111111111111111111111111111011011111111111111111111110\r\n0101111110111111111111111111111111111111111011111111111111111111101111011011110\r\n0110111111101111110111111111111011111111101011111101111111111111111111110111100\r\n", "output": "472\r\n"}, {"input": "7 20\r\n0111111111111111111100\r\n0111110111111111111110\r\n0111111111111111111100\r\n0111111011111111111110\r\n0111111111111011101110\r\n0111101011110111111010\r\n0111111111111111111010\r\n", "output": "151\r\n"}, {"input": "8 8\r\n0111111110\r\n0111101110\r\n0111111110\r\n0111111110\r\n0111111110\r\n0110111100\r\n0101111110\r\n0110111110\r\n", "output": "78\r\n"}, {"input": "11 24\r\n01111111111101111111111110\r\n01111111111111111111111110\r\n01110111111111111111111110\r\n01111111111111111111011110\r\n01111111111111111110111110\r\n01111010111111100111101110\r\n01111111111111010101111100\r\n01111111111111110111111110\r\n01011101111111111101111110\r\n00111111011111111110111110\r\n01111111101111111101111110\r\n", "output": "284\r\n"}, {"input": "12 12\r\n01111111111000\r\n01101111110110\r\n01111110111110\r\n01111111111110\r\n01111111111010\r\n01011111110110\r\n01111111111110\r\n01101101011110\r\n01111111111110\r\n01111101011110\r\n00111111111110\r\n01111111011110\r\n", "output": "166\r\n"}, {"input": "15 28\r\n011111111101011111111101111110\r\n011111111111111111111111111110\r\n011101110111011011101111011110\r\n011111111011111011110111111110\r\n011111111110101111111111111110\r\n011111011111110011111111011010\r\n011110111111001101111111111110\r\n011111111110111111111011111110\r\n011111111111111111111111011110\r\n011111011111111111111011001010\r\n011111111101111111111101111110\r\n011111111110111111101111011110\r\n010111111111101111111111111110\r\n011111111111111111011111111110\r\n011011111111111110110111110110\r\n", "output": "448\r\n"}, {"input": "2 11\r\n0100000000000\r\n0000000010000\r\n", "output": "18\r\n"}, {"input": "1 100\r\n010010010011100001101101110111101010000101010001111001001101011110000011101110101000100111111001101110\r\n", "output": "100\r\n"}, {"input": "15 1\r\n010\r\n010\r\n010\r\n010\r\n010\r\n010\r\n000\r\n000\r\n000\r\n010\r\n000\r\n010\r\n000\r\n000\r\n000\r\n", "output": "29\r\n"}, {"input": "3 3\r\n00010\r\n00000\r\n00010\r\n", "output": "7\r\n"}]
false
stdio
null
true
628/D
628
D
PyPy 3
TESTS
20
1,855
38,707,200
114079273
# by the authority of GOD author: manhar singh sachdev # import os,sys from io import BytesIO,IOBase def solve(x,m,d,mod,par): dp = [[[0]*m for _ in range(2)] for _ in range(len(x))] # remainder of number formed till now ; smaller/equal ; place value zz = int(x[0]) for _ in range(1,zz): if _ == d: continue dp[0][0][_%m] += 1 if zz != d: dp[0][1][zz%m] = 1 for i in range(1,len(x)): zz = int(x[i]) for j in range(m): for k in range(10): if (not i&1 and k==d) or (i&1 and k!=d): continue z = (j*10+k)%m dp[i][0][z] = (dp[i][0][z]+dp[i-1][0][j]+ (0 if k>=zz else dp[i-1][1][j]))%mod dp[i][1][z] = (dp[i][1][z]+(0 if k!=zz else dp[i-1][1][j]))%mod if par: return (dp[len(x)-1][0][0]+dp[len(x)-1][1][0])%mod return dp[len(x)-1][0][0] def main(): m,d = map(int,input().split()) a = input().strip() b = input().strip() print(solve(b,m,d,10**9+7,1)-solve(a,m,d,10**9+7,0)) # Fast IO Region BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self,file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd,max(os.fstat(self._fd).st_size,BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0,2),self.buffer.write(b),self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd,max(os.fstat(self._fd).st_size,BUFSIZE)) self.newlines = b.count(b"\n")+(not b) ptr = self.buffer.tell() self.buffer.seek(0,2),self.buffer.write(b),self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd,self.buffer.getvalue()) self.buffer.truncate(0),self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self,file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s:self.buffer.write(s.encode("ascii")) self.read = lambda:self.buffer.read().decode("ascii") self.readline = lambda:self.buffer.readline().decode("ascii") sys.stdin,sys.stdout = IOWrapper(sys.stdin),IOWrapper(sys.stdout) input = lambda:sys.stdin.readline().rstrip("\r\n") if __name__ == "__main__": main()
66
217
3,379,200
189367480
from sys import stdin mod = 10**9 + 7 _all = map(str, stdin.read().split()) m, d, a, b = int(next(_all)), int(next(_all)), next(_all), next(_all) n = len(a) din, din1 = [0]*m, [0]*m eqa, eqb, eqab = 1, 1, 1 xa, xb = 0, 0 for i in range(n): eqa1, eqb1, eqab1 = 0, 0, 0 din1 = [0]*m for dig in range(10): if i % 2 == 1 and dig != d or i % 2 == 0 and dig == d: continue for x in range(m): y = (10 * x + dig) % m din1[y] = (din1[y] + din[x]) % mod if eqab: if (int(a[i]) < dig and dig < int(b[i])): y = (10 * xa + dig) % m din1[y] = din1[y] + 1 else: if (eqa and int(a[i]) < dig): y = (10 * xa + dig) % m din1[y] = din1[y] + 1 if (eqb and dig < int(b[i])): y = (10 * xb + dig) % m din1[y] = din1[y] + 1 eqa1 |= eqa and dig == int(a[i]) eqb1 |= eqb and dig == int(b[i]) eqab1 |= eqab and dig == int(a[i]) & dig == int(b[i]) xa = (10 * xa + (int(a[i]))) % m xb = (10 * xb + (int(b[i]))) % m eqa = eqa1 eqb = eqb1 eqab = eqab1 din, din1 = din1, din ans = din[0] if (eqab): ans = ans + (xa == 0) else: ans = ans + (eqa and xa == 0) ans = ans + (eqb and xb == 0) print(ans%mod)
Educational Codeforces Round 8
ICPC
2,016
2
256
Magic Numbers
Consider the decimal presentation of an integer. Let's call a number d-magic if digit d appears in decimal presentation of the number on even positions and nowhere else. For example, the numbers 1727374, 17, 1 are 7-magic but 77, 7, 123, 34, 71 are not 7-magic. On the other hand the number 7 is 0-magic, 123 is 2-magic, 34 is 4-magic and 71 is 1-magic. Find the number of d-magic numbers in the segment [a, b] that are multiple of m. Because the answer can be very huge you should only find its value modulo 109 + 7 (so you should find the remainder after dividing by 109 + 7).
The first line contains two integers m, d (1 ≤ m ≤ 2000, 0 ≤ d ≤ 9) — the parameters from the problem statement. The second line contains positive integer a in decimal presentation (without leading zeroes). The third line contains positive integer b in decimal presentation (without leading zeroes). It is guaranteed that a ≤ b, the number of digits in a and b are the same and don't exceed 2000.
Print the only integer a — the remainder after dividing by 109 + 7 of the number of d-magic numbers in segment [a, b] that are multiple of m.
null
The numbers from the answer of the first example are 16, 26, 36, 46, 56, 76, 86 and 96. The numbers from the answer of the second example are 2, 4, 6 and 8. The numbers from the answer of the third example are 1767, 2717, 5757, 6707, 8797 and 9747.
[{"input": "2 6\n10\n99", "output": "8"}, {"input": "2 0\n1\n9", "output": "4"}, {"input": "19 7\n1000\n9999", "output": "6"}]
2,200
["dp"]
66
[{"input": "2 6\r\n10\r\n99\r\n", "output": "8\r\n"}, {"input": "2 0\r\n1\r\n9\r\n", "output": "4\r\n"}, {"input": "19 7\r\n1000\r\n9999\r\n", "output": "6\r\n"}, {"input": "9 4\r\n33\r\n52\r\n", "output": "0\r\n"}, {"input": "10 8\r\n18\r\n59\r\n", "output": "0\r\n"}, {"input": "43 3\r\n587\r\n850\r\n", "output": "1\r\n"}, {"input": "65 3\r\n3436\r\n3632\r\n", "output": "0\r\n"}, {"input": "850 8\r\n55735\r\n94089\r\n", "output": "0\r\n"}, {"input": "590 6\r\n428671\r\n715453\r\n", "output": "0\r\n"}, {"input": "1053 8\r\n1539368\r\n3362621\r\n", "output": "0\r\n"}, {"input": "477 9\r\n3062053\r\n6465858\r\n", "output": "6\r\n"}, {"input": "1901 9\r\n1941695\r\n3314270\r\n", "output": "0\r\n"}, {"input": "29 0\r\n1649127\r\n6241670\r\n", "output": "126\r\n"}, {"input": "566 3\r\n6372451659957700362854162843720623142601337360014410221724168092176479911659703538545016668832338549\r\n7969973326176891147525183958122002014921396842270051000646823226374743898663307171214245111949604186\r\n", "output": "0\r\n"}, {"input": "1286 5\r\n1886373541983002858974907276497223649072414883083336663541044958378875954171855070620868427474284001\r\n4050983123791059817478363830631049287126338893626273758612677264947268375965600848751800494833017145\r\n", "output": "0\r\n"}, {"input": "2 5\r\n1762712\r\n8121765\r\n", "output": "2025\r\n"}, {"input": "2 2\r\n12\r\n12\r\n", "output": "1\r\n"}, {"input": "2 0\r\n10\r\n10\r\n", "output": "1\r\n"}, {"input": "2 6\r\n46\r\n46\r\n", "output": "1\r\n"}, {"input": "2 0\r\n10\r\n20\r\n", "output": "2\r\n"}, {"input": "2 9\r\n10000000000\r\n99999999999\r\n", "output": "262440\r\n"}, {"input": "10 2\r\n12300\r\n99900\r\n", "output": "70\r\n"}, {"input": "5 5\r\n5\r\n5\r\n", "output": "0\r\n"}, {"input": "1 2\r\n113548484131315415454546546467913135484841313154154\r\n895458414564646646487131313118761454584145646466464\r\n", "output": "970729981\r\n"}, {"input": "2 2\r\n12\r\n14\r\n", "output": "1\r\n"}, {"input": "2 2\r\n2934\r\n4323\r\n", "output": "18\r\n"}, {"input": "2 4\r\n1\r\n9\r\n", "output": "3\r\n"}, {"input": "1 0\r\n10\r\n20\r\n", "output": "2\r\n"}, {"input": "7 7\r\n7\r\n7\r\n", "output": "0\r\n"}, {"input": "2 1\r\n143\r\n413\r\n", "output": "12\r\n"}, {"input": "1 4\r\n1\r\n9\r\n", "output": "8\r\n"}, {"input": "2 6\r\n36\r\n36\r\n", "output": "1\r\n"}, {"input": "4 4\r\n24344\r\n48880\r\n", "output": "29\r\n"}, {"input": "2 0\r\n1000\r\n9999\r\n", "output": "81\r\n"}, {"input": "3 9\r\n39\r\n39\r\n", "output": "1\r\n"}, {"input": "3 9\r\n10\r\n99\r\n", "output": "2\r\n"}, {"input": "1 0\r\n10000\r\n99999\r\n", "output": "729\r\n"}, {"input": "3 2\r\n681563\r\n828242\r\n", "output": "48\r\n"}, {"input": "1 5\r\n454\r\n667\r\n", "output": "14\r\n"}, {"input": "2 0\r\n2\r\n9\r\n", "output": "4\r\n"}, {"input": "2 6\r\n7\r\n9\r\n", "output": "1\r\n"}, {"input": "10 7\r\n1\r\n1\r\n", "output": "0\r\n"}, {"input": "6 0\r\n1\r\n6\r\n", "output": "1\r\n"}, {"input": "6 2\r\n22\r\n95\r\n", "output": "2\r\n"}, {"input": "28 1\r\n102\r\n898\r\n", "output": "2\r\n"}, {"input": "1 0\r\n1\r\n9\r\n", "output": "9\r\n"}, {"input": "1 1\r\n1\r\n9\r\n", "output": "8\r\n"}, {"input": "3 9\r\n10\r\n69\r\n", "output": "2\r\n"}, {"input": "1 0\r\n1\r\n1\r\n", "output": "1\r\n"}]
false
stdio
null
true
812/B
812
B
Python 3
TESTS
42
264
307,200
27527822
# 1000 n_m = [int(x) for x in input().split()] n_m[1] += 1 floors = [] rooms = [] position = [0, 0] def path_cost(): s = [] MIN=0 index = 0 y = rooms[0][1] for i in range(len(rooms)): if rooms[i][1]>y: continue if position[1]-rooms[i][1]==0 : s.append(abs(rooms[i][0]-position[0])) else: temp_cost=abs(position[0]) new_position=[0,position[1]] temp_cost+=(abs(rooms[i][0]-new_position[0])+abs(rooms[i][1]-new_position[1])) temp_cost1 = abs(n_m[1] - position[0]) new_position1 = [n_m[1], position[1]] temp_cost1 += (abs(rooms[i][0] - new_position1[0]) + abs(rooms[i][1] - new_position1[1])) if temp_cost>temp_cost1: s.append(temp_cost1) else: s.append(temp_cost) if MIN==0: MIN = s[len(s) - 1] if (s[len(s)-1] <= MIN): MIN = s[len(s)-1] index = i y = rooms[i][1] return [MIN,index] for i in range(n_m[0]): floors.append([bool(int(x)) for x in input()]) floors.reverse() for i in range(n_m[0]): for x in range(n_m[1]+1): if floors[i][x]: rooms.append([x, i]) time = 0 del floors for i in range(len(rooms[:])): least_ind = path_cost() position = rooms.pop(least_ind[1]) time += least_ind[0] print(time)
72
62
0
194156841
import sys input = sys.stdin.readline n, m = map(int, input().split()) a = 0 b = m+1 c = [0, 0] g = [input()[:-1] for _ in range(n)] for i in range(n-1, 0, -1): if '1' in g[i]: c = [g[i].rindex('1') if a + g[i].rindex('1')*2 < b+m+1 else g[i].index('1'), m+1-g[i].index('1') if b + (m+1-g[i].index('1'))*2 < a + m + 1 else m+1-g[i].rindex('1')] a, b = min(a + g[i].rindex('1')*2, b+m+1)+1, min(b + (m+1-g[i].index('1'))*2, a + m + 1)+1 else: a += 1 b += 1 c[0] += 1 c[1] += 1 if '1' in g[0]: print(min(a + g[0].rindex('1'), b + m + 1 - g[0].index('1'))) else: print(min(a-c[0], b-c[1]))
Codeforces Round 417 (Div. 2)
CF
2,017
1
256
Sagheer, the Hausmeister
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off. The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms. Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights. Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
The first line contains two integers n and m (1 ≤ n ≤ 15 and 1 ≤ m ≤ 100) — the number of floors and the number of rooms in each floor, respectively. The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor. The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
Print a single integer — the minimum total time needed to turn off all the lights.
null
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs. In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor. In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor.
[{"input": "2 2\n0010\n0100", "output": "5"}, {"input": "3 4\n001000\n000010\n000010", "output": "12"}, {"input": "4 3\n01110\n01110\n01110\n01110", "output": "18"}]
1,600
["bitmasks", "brute force", "dp"]
72
[{"input": "2 2\r\n0010\r\n0100\r\n", "output": "5\r\n"}, {"input": "3 4\r\n001000\r\n000010\r\n000010\r\n", "output": "12\r\n"}, {"input": "4 3\r\n01110\r\n01110\r\n01110\r\n01110\r\n", "output": "18\r\n"}, {"input": "3 2\r\n0000\r\n0100\r\n0100\r\n", "output": "4\r\n"}, {"input": "1 89\r\n0000000000000000000000000000000100000000000000010000000000010000000000000000000000000000000\r\n", "output": "59\r\n"}, {"input": "2 73\r\n000000000000000000000000000000000000000000000000000000000000000000000000000\r\n000000000000000000000000000000000000000100000010000000000000000000000000000\r\n", "output": "46\r\n"}, {"input": "3 61\r\n000000000000000000000000000000000000000000000000000000000000000\r\n000000000000000000000000000000000000000000000000000000000000000\r\n000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "0\r\n"}, {"input": "4 53\r\n0000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000\r\n", "output": "0\r\n"}, {"input": "5 93\r\n00000000000000000000000000000000000000000000000000000000100000000000000000000000000000000001010\r\n00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n00000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n00000000000000000000000000000010000000000000000000100000000000000000000000000000000000000000000\r\n00000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "265\r\n"}, {"input": "6 77\r\n0000000000000000100000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000010000000000000\r\n0000000000010000000000000000000000000000000000000000000000000000000000000000010\r\n0000000000000000000001000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000100000000000000000000000000000\r\n", "output": "311\r\n"}, {"input": "7 65\r\n0000000001000000000000000010000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000001000001000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "62\r\n"}, {"input": "8 57\r\n00000000100000000000000000000000000000000000000000000000000\r\n00000000000000010000000000000000000000000000000000000000000\r\n00000000000000000000000000000000000100000000000000000000000\r\n00000000000000000000000000000000000000000000000000000000000\r\n00000000000000000000000000000000000100000000000000000000000\r\n00000000000000000000000000000000000000000000000000000000000\r\n00000000000010000000000000000000000000000000000000000000000\r\n00000000000000000000000000000000000000000000000001000000000\r\n", "output": "277\r\n"}, {"input": "12 13\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n010000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000010000000000\r\n000000000000000\r\n", "output": "14\r\n"}, {"input": "13 1\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n", "output": "0\r\n"}, {"input": "1 33\r\n00000100101110001101000000110100010\r\n", "output": "33\r\n"}, {"input": "2 21\r\n00100110100010010010010\r\n01000001111001010000000\r\n", "output": "43\r\n"}, {"input": "3 5\r\n0001010\r\n0100000\r\n0100000\r\n", "output": "11\r\n"}, {"input": "4 45\r\n00010000101101100000101101000000100000001101100\r\n01110000100111010011000000100000000001000001100\r\n00000000001000100110100001000010011010001010010\r\n01111110100100000101101010011000100100001000000\r\n", "output": "184\r\n"}, {"input": "5 37\r\n010100000000000000000110000110010000010\r\n001101100010110011101000001010101101110\r\n010000001000100010010100000000001010000\r\n000000000100101000000101100001000001110\r\n000010000000000000100001001000011100110\r\n", "output": "193\r\n"}, {"input": "6 25\r\n011001000100111010000101000\r\n000000000010000010001000010\r\n011001100001100001001001010\r\n000000100000010000000000110\r\n010001100001000001000000010\r\n011000001001010111110000100\r\n", "output": "160\r\n"}, {"input": "7 61\r\n010000111100010100001000011010100001000000000011100000100010000\r\n000010011000001000000100110101010001000000010001100000100100100\r\n000010001000001000000100001000000100100011001110000111000000100\r\n000000000101000011010000011000000101000001011001000011101010010\r\n000010010011000000100000110000001000000101000000101000010000010\r\n000010010101101100100100100011001011101010000101000010000101010\r\n000100001100001001000000001000000001011000110010100000000010110\r\n", "output": "436\r\n"}, {"input": "8 49\r\n000100100000000111110010011100110100010010000011000\r\n001000000101111000000001111100010010100000010000000\r\n000000010000011100001000000000101000110010000100100\r\n000000000001000110000011101101000000100000101010000\r\n000000110001000101101000000001000000110001000110000\r\n000100000000000000100100010011000001111101010100110\r\n000000001000000010101111000100001100000000010111000\r\n001000010000110000011100000000100110000010001000000\r\n", "output": "404\r\n"}, {"input": "9 41\r\n0011000000000101001101001000000001110000010\r\n0000110000001010110010110010110010010001000\r\n0001100010100000000001110100100001101000100\r\n0001010101111010000000010010001001011111000\r\n0101000101000011101011000000001100110010000\r\n0001010000000000000001011000000100010101000\r\n0000010011000000001000110001000010110001000\r\n0000100010000110100001000000100010001111100\r\n0000001110100001000001000110001110000100000\r\n", "output": "385\r\n"}, {"input": "10 29\r\n0000000000101001100001001011000\r\n0001110100000000000000100010000\r\n0010001001000011000100010001000\r\n0001000010101000000010100010100\r\n0111000000000000100100100010100\r\n0001000100011111000100010100000\r\n0000000000000001000001001011000\r\n0000101110000001010001011001110\r\n0000001000101010011000001100100\r\n0100010000101011010000000000000\r\n", "output": "299\r\n"}, {"input": "1 57\r\n00011101100001110001111000000100101111000111101100111001000\r\n", "output": "55\r\n"}, {"input": "2 32\r\n0011110111011011011101111101011110\r\n0111000110111111011110011101011110\r\n", "output": "65\r\n"}, {"input": "3 20\r\n0110011111110101101100\r\n0111110000111010100100\r\n0110111110010100011110\r\n", "output": "63\r\n"}, {"input": "4 4\r\n011100\r\n001010\r\n010000\r\n011110\r\n", "output": "22\r\n"}, {"input": "5 44\r\n0001010010001111111001111111000010100100000010\r\n0001111001111001101111011111010110001001111110\r\n0111111010111111011101100011101010100101110110\r\n0011010011101011101111001001010110000111111100\r\n0110100111011100110101110010010011011101100100\r\n", "output": "228\r\n"}, {"input": "6 36\r\n01110101111111110101011000011111110010\r\n00011101100010110111111111110001100100\r\n00001111110010111111101110101110111110\r\n00110110011100100111011110000000000010\r\n01100101101001010001011111100111101100\r\n00011111111011001000011001011110011110\r\n", "output": "226\r\n"}, {"input": "7 24\r\n01111001111001011010010100\r\n00111011010101000111101000\r\n01001110110010010110011110\r\n00000101111011011111111000\r\n01111111101111001001010010\r\n01110000111101011111111010\r\n00000100011100110000110000\r\n", "output": "179\r\n"}, {"input": "8 8\r\n0011101110\r\n0110010100\r\n0100111110\r\n0111111100\r\n0011010100\r\n0001101110\r\n0111100000\r\n0110111000\r\n", "output": "77\r\n"}, {"input": "9 48\r\n00011010111110111011111001111111111101001111110010\r\n01000101000101101101111110111101011100001011010010\r\n00110111110110101110101110111111011011101111011000\r\n00110111111100010110110110111001001111011010101110\r\n01111111100101010011111100100111110011001101110100\r\n01111011110011111101010101010100001110111111111000\r\n01110101101101110001000010110100010110101111111100\r\n00111101001010110010110100000111110101010100001000\r\n00011011010110011111001100111100100011100110110100\r\n", "output": "448\r\n"}, {"input": "10 40\r\n010011001001111011011011101111010001010010\r\n011000000110000010001011111010100000110000\r\n011010101001110010110110011111010101101000\r\n000111111010101111000110011111011011011010\r\n010110101110001001001111111000110011101010\r\n010011010100111110010100100111100111011110\r\n001111101100111111111111001010111010000110\r\n001111110010101100110100101110001011100110\r\n010111010010001111110101111111111110111000\r\n011101101111000100111111111001111100111010\r\n", "output": "418\r\n"}, {"input": "11 28\r\n011100111101101001011111001110\r\n010001111110011101101011001000\r\n001010011011011010101101101100\r\n001100011001101011011001110100\r\n010111110011101110000110111100\r\n010010001111110000011111010100\r\n001011111111110011101101111010\r\n001101101011100100011011001110\r\n001111110110100110101011000010\r\n000101101011100001101101100100\r\n010011101101111011100111110100\r\n", "output": "328\r\n"}, {"input": "1 68\r\n0101111110111111111111111111110111111111111111111110111111101111111110\r\n", "output": "68\r\n"}, {"input": "2 56\r\n0011111111111110111111111111111111011111111111011111011110\r\n0111111111010111111111110111111111111110111111010111111110\r\n", "output": "113\r\n"}, {"input": "3 17\r\n0111111101111111110\r\n0111111111101011110\r\n0101111111111111110\r\n", "output": "55\r\n"}, {"input": "4 4\r\n011110\r\n010110\r\n010110\r\n011110\r\n", "output": "22\r\n"}, {"input": "5 89\r\n0011111111111101110110111111111101111011111011101110111111111111111111111111111111111111110\r\n0111111111111111111111111101111111111111111111111111111111111111111111111111111111111111110\r\n0111111111111011111111111111111111101111011111111111111111110110111101111111111111111011010\r\n0111111111111111011011111111111011111111111111111111111111111111111111111111111110111111010\r\n0111111101111011111110101011111111110111100100101111111011111111111111011011101111111111110\r\n", "output": "453\r\n"}, {"input": "6 77\r\n0111111110101011111111111111111111111111111111111111100111111111101111111111110\r\n0111111111111111111101111101111111111011111111011111111001011111111111101111110\r\n0111101111111111111111111111111111111110110011111111111011111111101111111111110\r\n0111110111111111111111111111111111111111111111111111011011111111111111111111110\r\n0101111110111111111111111111111111111111111011111111111111111111101111011011110\r\n0110111111101111110111111111111011111111101011111101111111111111111111110111100\r\n", "output": "472\r\n"}, {"input": "7 20\r\n0111111111111111111100\r\n0111110111111111111110\r\n0111111111111111111100\r\n0111111011111111111110\r\n0111111111111011101110\r\n0111101011110111111010\r\n0111111111111111111010\r\n", "output": "151\r\n"}, {"input": "8 8\r\n0111111110\r\n0111101110\r\n0111111110\r\n0111111110\r\n0111111110\r\n0110111100\r\n0101111110\r\n0110111110\r\n", "output": "78\r\n"}, {"input": "11 24\r\n01111111111101111111111110\r\n01111111111111111111111110\r\n01110111111111111111111110\r\n01111111111111111111011110\r\n01111111111111111110111110\r\n01111010111111100111101110\r\n01111111111111010101111100\r\n01111111111111110111111110\r\n01011101111111111101111110\r\n00111111011111111110111110\r\n01111111101111111101111110\r\n", "output": "284\r\n"}, {"input": "12 12\r\n01111111111000\r\n01101111110110\r\n01111110111110\r\n01111111111110\r\n01111111111010\r\n01011111110110\r\n01111111111110\r\n01101101011110\r\n01111111111110\r\n01111101011110\r\n00111111111110\r\n01111111011110\r\n", "output": "166\r\n"}, {"input": "15 28\r\n011111111101011111111101111110\r\n011111111111111111111111111110\r\n011101110111011011101111011110\r\n011111111011111011110111111110\r\n011111111110101111111111111110\r\n011111011111110011111111011010\r\n011110111111001101111111111110\r\n011111111110111111111011111110\r\n011111111111111111111111011110\r\n011111011111111111111011001010\r\n011111111101111111111101111110\r\n011111111110111111101111011110\r\n010111111111101111111111111110\r\n011111111111111111011111111110\r\n011011111111111110110111110110\r\n", "output": "448\r\n"}, {"input": "2 11\r\n0100000000000\r\n0000000010000\r\n", "output": "18\r\n"}, {"input": "1 100\r\n010010010011100001101101110111101010000101010001111001001101011110000011101110101000100111111001101110\r\n", "output": "100\r\n"}, {"input": "15 1\r\n010\r\n010\r\n010\r\n010\r\n010\r\n010\r\n000\r\n000\r\n000\r\n010\r\n000\r\n010\r\n000\r\n000\r\n000\r\n", "output": "29\r\n"}, {"input": "3 3\r\n00010\r\n00000\r\n00010\r\n", "output": "7\r\n"}]
false
stdio
null
true
812/B
812
B
Python 3
TESTS
42
46
0
231542755
n_and_m = input() n, m = n_and_m.split() #n - кол-во целых чисел, k - кол-во элем. заданной посл-ти меньше либо равны x(результат программы) n = int(n) m = int(m) p = [input() for y in range(n)][::-1] l = r = d = 0 i = j = 0 pos = 0 put = 0 for y, t in enumerate(p): if '1' in t: d = y i = t.find('1') j = t.rfind('1') l = m-i + m-pos + 2 r = pos + j if r <= l: put += r pos = j else: put += l pos = i print(put + d)
72
62
307,200
27491979
n, m = map(int, input().split()) a = [] for i in range(n): a.append(input()) # print(a) l = 0 r = INF = 100000 h = 0 while h < n: if a[h] == '0' * (m + 2): h += 1 else: break if h != n: for i in range(n - 1, h, -1): x = INF y = 0 for j in range(m + 2): if a[i][j] == '1': x = min(x, j) y = max(y, j) if x != INF: ll = min(l + y * 2, r + m + 1) rr = min(l + m + 1, r + (m + 1 - x) * 2) l = ll r = rr # print(i, l, r) x = INF y = 0 for j in range(m + 2): if a[h][j] == '1': x = min(x, j) y = max(y, j) ans = min(l + y, r + (m + 1 - x)) print(ans + n - 1 - h) else: print(0)
Codeforces Round 417 (Div. 2)
CF
2,017
1
256
Sagheer, the Hausmeister
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off. The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms. Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights. Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
The first line contains two integers n and m (1 ≤ n ≤ 15 and 1 ≤ m ≤ 100) — the number of floors and the number of rooms in each floor, respectively. The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor. The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
Print a single integer — the minimum total time needed to turn off all the lights.
null
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs. In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor. In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor.
[{"input": "2 2\n0010\n0100", "output": "5"}, {"input": "3 4\n001000\n000010\n000010", "output": "12"}, {"input": "4 3\n01110\n01110\n01110\n01110", "output": "18"}]
1,600
["bitmasks", "brute force", "dp"]
72
[{"input": "2 2\r\n0010\r\n0100\r\n", "output": "5\r\n"}, {"input": "3 4\r\n001000\r\n000010\r\n000010\r\n", "output": "12\r\n"}, {"input": "4 3\r\n01110\r\n01110\r\n01110\r\n01110\r\n", "output": "18\r\n"}, {"input": "3 2\r\n0000\r\n0100\r\n0100\r\n", "output": "4\r\n"}, {"input": "1 89\r\n0000000000000000000000000000000100000000000000010000000000010000000000000000000000000000000\r\n", "output": "59\r\n"}, {"input": "2 73\r\n000000000000000000000000000000000000000000000000000000000000000000000000000\r\n000000000000000000000000000000000000000100000010000000000000000000000000000\r\n", "output": "46\r\n"}, {"input": "3 61\r\n000000000000000000000000000000000000000000000000000000000000000\r\n000000000000000000000000000000000000000000000000000000000000000\r\n000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "0\r\n"}, {"input": "4 53\r\n0000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000\r\n", "output": "0\r\n"}, {"input": "5 93\r\n00000000000000000000000000000000000000000000000000000000100000000000000000000000000000000001010\r\n00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n00000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n00000000000000000000000000000010000000000000000000100000000000000000000000000000000000000000000\r\n00000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "265\r\n"}, {"input": "6 77\r\n0000000000000000100000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000010000000000000\r\n0000000000010000000000000000000000000000000000000000000000000000000000000000010\r\n0000000000000000000001000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000100000000000000000000000000000\r\n", "output": "311\r\n"}, {"input": "7 65\r\n0000000001000000000000000010000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000001000001000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "62\r\n"}, {"input": "8 57\r\n00000000100000000000000000000000000000000000000000000000000\r\n00000000000000010000000000000000000000000000000000000000000\r\n00000000000000000000000000000000000100000000000000000000000\r\n00000000000000000000000000000000000000000000000000000000000\r\n00000000000000000000000000000000000100000000000000000000000\r\n00000000000000000000000000000000000000000000000000000000000\r\n00000000000010000000000000000000000000000000000000000000000\r\n00000000000000000000000000000000000000000000000001000000000\r\n", "output": "277\r\n"}, {"input": "12 13\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n010000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000010000000000\r\n000000000000000\r\n", "output": "14\r\n"}, {"input": "13 1\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n", "output": "0\r\n"}, {"input": "1 33\r\n00000100101110001101000000110100010\r\n", "output": "33\r\n"}, {"input": "2 21\r\n00100110100010010010010\r\n01000001111001010000000\r\n", "output": "43\r\n"}, {"input": "3 5\r\n0001010\r\n0100000\r\n0100000\r\n", "output": "11\r\n"}, {"input": "4 45\r\n00010000101101100000101101000000100000001101100\r\n01110000100111010011000000100000000001000001100\r\n00000000001000100110100001000010011010001010010\r\n01111110100100000101101010011000100100001000000\r\n", "output": "184\r\n"}, {"input": "5 37\r\n010100000000000000000110000110010000010\r\n001101100010110011101000001010101101110\r\n010000001000100010010100000000001010000\r\n000000000100101000000101100001000001110\r\n000010000000000000100001001000011100110\r\n", "output": "193\r\n"}, {"input": "6 25\r\n011001000100111010000101000\r\n000000000010000010001000010\r\n011001100001100001001001010\r\n000000100000010000000000110\r\n010001100001000001000000010\r\n011000001001010111110000100\r\n", "output": "160\r\n"}, {"input": "7 61\r\n010000111100010100001000011010100001000000000011100000100010000\r\n000010011000001000000100110101010001000000010001100000100100100\r\n000010001000001000000100001000000100100011001110000111000000100\r\n000000000101000011010000011000000101000001011001000011101010010\r\n000010010011000000100000110000001000000101000000101000010000010\r\n000010010101101100100100100011001011101010000101000010000101010\r\n000100001100001001000000001000000001011000110010100000000010110\r\n", "output": "436\r\n"}, {"input": "8 49\r\n000100100000000111110010011100110100010010000011000\r\n001000000101111000000001111100010010100000010000000\r\n000000010000011100001000000000101000110010000100100\r\n000000000001000110000011101101000000100000101010000\r\n000000110001000101101000000001000000110001000110000\r\n000100000000000000100100010011000001111101010100110\r\n000000001000000010101111000100001100000000010111000\r\n001000010000110000011100000000100110000010001000000\r\n", "output": "404\r\n"}, {"input": "9 41\r\n0011000000000101001101001000000001110000010\r\n0000110000001010110010110010110010010001000\r\n0001100010100000000001110100100001101000100\r\n0001010101111010000000010010001001011111000\r\n0101000101000011101011000000001100110010000\r\n0001010000000000000001011000000100010101000\r\n0000010011000000001000110001000010110001000\r\n0000100010000110100001000000100010001111100\r\n0000001110100001000001000110001110000100000\r\n", "output": "385\r\n"}, {"input": "10 29\r\n0000000000101001100001001011000\r\n0001110100000000000000100010000\r\n0010001001000011000100010001000\r\n0001000010101000000010100010100\r\n0111000000000000100100100010100\r\n0001000100011111000100010100000\r\n0000000000000001000001001011000\r\n0000101110000001010001011001110\r\n0000001000101010011000001100100\r\n0100010000101011010000000000000\r\n", "output": "299\r\n"}, {"input": "1 57\r\n00011101100001110001111000000100101111000111101100111001000\r\n", "output": "55\r\n"}, {"input": "2 32\r\n0011110111011011011101111101011110\r\n0111000110111111011110011101011110\r\n", "output": "65\r\n"}, {"input": "3 20\r\n0110011111110101101100\r\n0111110000111010100100\r\n0110111110010100011110\r\n", "output": "63\r\n"}, {"input": "4 4\r\n011100\r\n001010\r\n010000\r\n011110\r\n", "output": "22\r\n"}, {"input": "5 44\r\n0001010010001111111001111111000010100100000010\r\n0001111001111001101111011111010110001001111110\r\n0111111010111111011101100011101010100101110110\r\n0011010011101011101111001001010110000111111100\r\n0110100111011100110101110010010011011101100100\r\n", "output": "228\r\n"}, {"input": "6 36\r\n01110101111111110101011000011111110010\r\n00011101100010110111111111110001100100\r\n00001111110010111111101110101110111110\r\n00110110011100100111011110000000000010\r\n01100101101001010001011111100111101100\r\n00011111111011001000011001011110011110\r\n", "output": "226\r\n"}, {"input": "7 24\r\n01111001111001011010010100\r\n00111011010101000111101000\r\n01001110110010010110011110\r\n00000101111011011111111000\r\n01111111101111001001010010\r\n01110000111101011111111010\r\n00000100011100110000110000\r\n", "output": "179\r\n"}, {"input": "8 8\r\n0011101110\r\n0110010100\r\n0100111110\r\n0111111100\r\n0011010100\r\n0001101110\r\n0111100000\r\n0110111000\r\n", "output": "77\r\n"}, {"input": "9 48\r\n00011010111110111011111001111111111101001111110010\r\n01000101000101101101111110111101011100001011010010\r\n00110111110110101110101110111111011011101111011000\r\n00110111111100010110110110111001001111011010101110\r\n01111111100101010011111100100111110011001101110100\r\n01111011110011111101010101010100001110111111111000\r\n01110101101101110001000010110100010110101111111100\r\n00111101001010110010110100000111110101010100001000\r\n00011011010110011111001100111100100011100110110100\r\n", "output": "448\r\n"}, {"input": "10 40\r\n010011001001111011011011101111010001010010\r\n011000000110000010001011111010100000110000\r\n011010101001110010110110011111010101101000\r\n000111111010101111000110011111011011011010\r\n010110101110001001001111111000110011101010\r\n010011010100111110010100100111100111011110\r\n001111101100111111111111001010111010000110\r\n001111110010101100110100101110001011100110\r\n010111010010001111110101111111111110111000\r\n011101101111000100111111111001111100111010\r\n", "output": "418\r\n"}, {"input": "11 28\r\n011100111101101001011111001110\r\n010001111110011101101011001000\r\n001010011011011010101101101100\r\n001100011001101011011001110100\r\n010111110011101110000110111100\r\n010010001111110000011111010100\r\n001011111111110011101101111010\r\n001101101011100100011011001110\r\n001111110110100110101011000010\r\n000101101011100001101101100100\r\n010011101101111011100111110100\r\n", "output": "328\r\n"}, {"input": "1 68\r\n0101111110111111111111111111110111111111111111111110111111101111111110\r\n", "output": "68\r\n"}, {"input": "2 56\r\n0011111111111110111111111111111111011111111111011111011110\r\n0111111111010111111111110111111111111110111111010111111110\r\n", "output": "113\r\n"}, {"input": "3 17\r\n0111111101111111110\r\n0111111111101011110\r\n0101111111111111110\r\n", "output": "55\r\n"}, {"input": "4 4\r\n011110\r\n010110\r\n010110\r\n011110\r\n", "output": "22\r\n"}, {"input": "5 89\r\n0011111111111101110110111111111101111011111011101110111111111111111111111111111111111111110\r\n0111111111111111111111111101111111111111111111111111111111111111111111111111111111111111110\r\n0111111111111011111111111111111111101111011111111111111111110110111101111111111111111011010\r\n0111111111111111011011111111111011111111111111111111111111111111111111111111111110111111010\r\n0111111101111011111110101011111111110111100100101111111011111111111111011011101111111111110\r\n", "output": "453\r\n"}, {"input": "6 77\r\n0111111110101011111111111111111111111111111111111111100111111111101111111111110\r\n0111111111111111111101111101111111111011111111011111111001011111111111101111110\r\n0111101111111111111111111111111111111110110011111111111011111111101111111111110\r\n0111110111111111111111111111111111111111111111111111011011111111111111111111110\r\n0101111110111111111111111111111111111111111011111111111111111111101111011011110\r\n0110111111101111110111111111111011111111101011111101111111111111111111110111100\r\n", "output": "472\r\n"}, {"input": "7 20\r\n0111111111111111111100\r\n0111110111111111111110\r\n0111111111111111111100\r\n0111111011111111111110\r\n0111111111111011101110\r\n0111101011110111111010\r\n0111111111111111111010\r\n", "output": "151\r\n"}, {"input": "8 8\r\n0111111110\r\n0111101110\r\n0111111110\r\n0111111110\r\n0111111110\r\n0110111100\r\n0101111110\r\n0110111110\r\n", "output": "78\r\n"}, {"input": "11 24\r\n01111111111101111111111110\r\n01111111111111111111111110\r\n01110111111111111111111110\r\n01111111111111111111011110\r\n01111111111111111110111110\r\n01111010111111100111101110\r\n01111111111111010101111100\r\n01111111111111110111111110\r\n01011101111111111101111110\r\n00111111011111111110111110\r\n01111111101111111101111110\r\n", "output": "284\r\n"}, {"input": "12 12\r\n01111111111000\r\n01101111110110\r\n01111110111110\r\n01111111111110\r\n01111111111010\r\n01011111110110\r\n01111111111110\r\n01101101011110\r\n01111111111110\r\n01111101011110\r\n00111111111110\r\n01111111011110\r\n", "output": "166\r\n"}, {"input": "15 28\r\n011111111101011111111101111110\r\n011111111111111111111111111110\r\n011101110111011011101111011110\r\n011111111011111011110111111110\r\n011111111110101111111111111110\r\n011111011111110011111111011010\r\n011110111111001101111111111110\r\n011111111110111111111011111110\r\n011111111111111111111111011110\r\n011111011111111111111011001010\r\n011111111101111111111101111110\r\n011111111110111111101111011110\r\n010111111111101111111111111110\r\n011111111111111111011111111110\r\n011011111111111110110111110110\r\n", "output": "448\r\n"}, {"input": "2 11\r\n0100000000000\r\n0000000010000\r\n", "output": "18\r\n"}, {"input": "1 100\r\n010010010011100001101101110111101010000101010001111001001101011110000011101110101000100111111001101110\r\n", "output": "100\r\n"}, {"input": "15 1\r\n010\r\n010\r\n010\r\n010\r\n010\r\n010\r\n000\r\n000\r\n000\r\n010\r\n000\r\n010\r\n000\r\n000\r\n000\r\n", "output": "29\r\n"}, {"input": "3 3\r\n00010\r\n00000\r\n00010\r\n", "output": "7\r\n"}]
false
stdio
null
true
812/B
812
B
Python 3
TESTS
42
46
102,400
230367730
n, m = list(map(int, input().split())) floor = [] count = 0 for i in range(n): str = input() floor.append(list(map(int, list(str)))) if 1 in floor[i]: count += 1 i = n - 1 j = 0 time = 0 while count > 0: if 1 in floor[i]: if j == 0: while 1 in floor[i]: j += 1 time += 1 if floor[i][j] == 1: floor[i][j] = 0 else: while 1 in floor[i]: j -= 1 time += 1 if floor[i][j] == 1: floor[i][j] = 0 count -= 1 if count == 0: break while not 1 in floor[i]: i -= 1 time += 1 left = j right = (m + 1 - j) q = 0 while floor[i][q] != 1: q += 1 left += q q = m + 1 while floor[i][q] != 1: q -= 1 right += m + 1 - q if left < right: time += j j = 0 else: time += (m + 1 - j) j = m + 1 else: i -= 1 time += 1 print(time)
72
62
307,200
27493791
def booly(s): return bool(int(s)) n, m = map(int, input().split()); nothing = "0"*(m+2); a = []; for i in range(n): a.append(input()) for i in range(n): if a[i] != nothing: break; a = a[i:] a.reverse(); n = len(a) #print(a) leftA = [i.find("1") for i in a] rightA = [i.rfind("1") for i in a] for i in range(n): if leftA[i] == -1: leftA[i] = m+1 if rightA[i] == -1: rightA[i] = 0 #print(leftA, rightA); if len(a) == 1: print( rightA[0]) exit(0); left = [None]*(n+1) right = [None]*(n+1) left[0] = rightA[0]*2 right[0] = m+1 for i in range(1,n-1): left[i] = 1 + min(left[i-1] + rightA[i]*2, right[i-1] + m+1) right[i] = 1 + min(right[i-1] + (m+2-1-leftA[i])*2, left[i-1] + m+1) #print(left, right) print(min(1 + left[n-2] + rightA[n-1], 1 + right[n-2] + (m+2 -1 -leftA[n-1])));
Codeforces Round 417 (Div. 2)
CF
2,017
1
256
Sagheer, the Hausmeister
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off. The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms. Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights. Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
The first line contains two integers n and m (1 ≤ n ≤ 15 and 1 ≤ m ≤ 100) — the number of floors and the number of rooms in each floor, respectively. The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor. The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
Print a single integer — the minimum total time needed to turn off all the lights.
null
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs. In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor. In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor.
[{"input": "2 2\n0010\n0100", "output": "5"}, {"input": "3 4\n001000\n000010\n000010", "output": "12"}, {"input": "4 3\n01110\n01110\n01110\n01110", "output": "18"}]
1,600
["bitmasks", "brute force", "dp"]
72
[{"input": "2 2\r\n0010\r\n0100\r\n", "output": "5\r\n"}, {"input": "3 4\r\n001000\r\n000010\r\n000010\r\n", "output": "12\r\n"}, {"input": "4 3\r\n01110\r\n01110\r\n01110\r\n01110\r\n", "output": "18\r\n"}, {"input": "3 2\r\n0000\r\n0100\r\n0100\r\n", "output": "4\r\n"}, {"input": "1 89\r\n0000000000000000000000000000000100000000000000010000000000010000000000000000000000000000000\r\n", "output": "59\r\n"}, {"input": "2 73\r\n000000000000000000000000000000000000000000000000000000000000000000000000000\r\n000000000000000000000000000000000000000100000010000000000000000000000000000\r\n", "output": "46\r\n"}, {"input": "3 61\r\n000000000000000000000000000000000000000000000000000000000000000\r\n000000000000000000000000000000000000000000000000000000000000000\r\n000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "0\r\n"}, {"input": "4 53\r\n0000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000\r\n", "output": "0\r\n"}, {"input": "5 93\r\n00000000000000000000000000000000000000000000000000000000100000000000000000000000000000000001010\r\n00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n00000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n00000000000000000000000000000010000000000000000000100000000000000000000000000000000000000000000\r\n00000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "265\r\n"}, {"input": "6 77\r\n0000000000000000100000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000010000000000000\r\n0000000000010000000000000000000000000000000000000000000000000000000000000000010\r\n0000000000000000000001000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000100000000000000000000000000000\r\n", "output": "311\r\n"}, {"input": "7 65\r\n0000000001000000000000000010000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000001000001000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "62\r\n"}, {"input": "8 57\r\n00000000100000000000000000000000000000000000000000000000000\r\n00000000000000010000000000000000000000000000000000000000000\r\n00000000000000000000000000000000000100000000000000000000000\r\n00000000000000000000000000000000000000000000000000000000000\r\n00000000000000000000000000000000000100000000000000000000000\r\n00000000000000000000000000000000000000000000000000000000000\r\n00000000000010000000000000000000000000000000000000000000000\r\n00000000000000000000000000000000000000000000000001000000000\r\n", "output": "277\r\n"}, {"input": "12 13\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n010000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000010000000000\r\n000000000000000\r\n", "output": "14\r\n"}, {"input": "13 1\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n", "output": "0\r\n"}, {"input": "1 33\r\n00000100101110001101000000110100010\r\n", "output": "33\r\n"}, {"input": "2 21\r\n00100110100010010010010\r\n01000001111001010000000\r\n", "output": "43\r\n"}, {"input": "3 5\r\n0001010\r\n0100000\r\n0100000\r\n", "output": "11\r\n"}, {"input": "4 45\r\n00010000101101100000101101000000100000001101100\r\n01110000100111010011000000100000000001000001100\r\n00000000001000100110100001000010011010001010010\r\n01111110100100000101101010011000100100001000000\r\n", "output": "184\r\n"}, {"input": "5 37\r\n010100000000000000000110000110010000010\r\n001101100010110011101000001010101101110\r\n010000001000100010010100000000001010000\r\n000000000100101000000101100001000001110\r\n000010000000000000100001001000011100110\r\n", "output": "193\r\n"}, {"input": "6 25\r\n011001000100111010000101000\r\n000000000010000010001000010\r\n011001100001100001001001010\r\n000000100000010000000000110\r\n010001100001000001000000010\r\n011000001001010111110000100\r\n", "output": "160\r\n"}, {"input": "7 61\r\n010000111100010100001000011010100001000000000011100000100010000\r\n000010011000001000000100110101010001000000010001100000100100100\r\n000010001000001000000100001000000100100011001110000111000000100\r\n000000000101000011010000011000000101000001011001000011101010010\r\n000010010011000000100000110000001000000101000000101000010000010\r\n000010010101101100100100100011001011101010000101000010000101010\r\n000100001100001001000000001000000001011000110010100000000010110\r\n", "output": "436\r\n"}, {"input": "8 49\r\n000100100000000111110010011100110100010010000011000\r\n001000000101111000000001111100010010100000010000000\r\n000000010000011100001000000000101000110010000100100\r\n000000000001000110000011101101000000100000101010000\r\n000000110001000101101000000001000000110001000110000\r\n000100000000000000100100010011000001111101010100110\r\n000000001000000010101111000100001100000000010111000\r\n001000010000110000011100000000100110000010001000000\r\n", "output": "404\r\n"}, {"input": "9 41\r\n0011000000000101001101001000000001110000010\r\n0000110000001010110010110010110010010001000\r\n0001100010100000000001110100100001101000100\r\n0001010101111010000000010010001001011111000\r\n0101000101000011101011000000001100110010000\r\n0001010000000000000001011000000100010101000\r\n0000010011000000001000110001000010110001000\r\n0000100010000110100001000000100010001111100\r\n0000001110100001000001000110001110000100000\r\n", "output": "385\r\n"}, {"input": "10 29\r\n0000000000101001100001001011000\r\n0001110100000000000000100010000\r\n0010001001000011000100010001000\r\n0001000010101000000010100010100\r\n0111000000000000100100100010100\r\n0001000100011111000100010100000\r\n0000000000000001000001001011000\r\n0000101110000001010001011001110\r\n0000001000101010011000001100100\r\n0100010000101011010000000000000\r\n", "output": "299\r\n"}, {"input": "1 57\r\n00011101100001110001111000000100101111000111101100111001000\r\n", "output": "55\r\n"}, {"input": "2 32\r\n0011110111011011011101111101011110\r\n0111000110111111011110011101011110\r\n", "output": "65\r\n"}, {"input": "3 20\r\n0110011111110101101100\r\n0111110000111010100100\r\n0110111110010100011110\r\n", "output": "63\r\n"}, {"input": "4 4\r\n011100\r\n001010\r\n010000\r\n011110\r\n", "output": "22\r\n"}, {"input": "5 44\r\n0001010010001111111001111111000010100100000010\r\n0001111001111001101111011111010110001001111110\r\n0111111010111111011101100011101010100101110110\r\n0011010011101011101111001001010110000111111100\r\n0110100111011100110101110010010011011101100100\r\n", "output": "228\r\n"}, {"input": "6 36\r\n01110101111111110101011000011111110010\r\n00011101100010110111111111110001100100\r\n00001111110010111111101110101110111110\r\n00110110011100100111011110000000000010\r\n01100101101001010001011111100111101100\r\n00011111111011001000011001011110011110\r\n", "output": "226\r\n"}, {"input": "7 24\r\n01111001111001011010010100\r\n00111011010101000111101000\r\n01001110110010010110011110\r\n00000101111011011111111000\r\n01111111101111001001010010\r\n01110000111101011111111010\r\n00000100011100110000110000\r\n", "output": "179\r\n"}, {"input": "8 8\r\n0011101110\r\n0110010100\r\n0100111110\r\n0111111100\r\n0011010100\r\n0001101110\r\n0111100000\r\n0110111000\r\n", "output": "77\r\n"}, {"input": "9 48\r\n00011010111110111011111001111111111101001111110010\r\n01000101000101101101111110111101011100001011010010\r\n00110111110110101110101110111111011011101111011000\r\n00110111111100010110110110111001001111011010101110\r\n01111111100101010011111100100111110011001101110100\r\n01111011110011111101010101010100001110111111111000\r\n01110101101101110001000010110100010110101111111100\r\n00111101001010110010110100000111110101010100001000\r\n00011011010110011111001100111100100011100110110100\r\n", "output": "448\r\n"}, {"input": "10 40\r\n010011001001111011011011101111010001010010\r\n011000000110000010001011111010100000110000\r\n011010101001110010110110011111010101101000\r\n000111111010101111000110011111011011011010\r\n010110101110001001001111111000110011101010\r\n010011010100111110010100100111100111011110\r\n001111101100111111111111001010111010000110\r\n001111110010101100110100101110001011100110\r\n010111010010001111110101111111111110111000\r\n011101101111000100111111111001111100111010\r\n", "output": "418\r\n"}, {"input": "11 28\r\n011100111101101001011111001110\r\n010001111110011101101011001000\r\n001010011011011010101101101100\r\n001100011001101011011001110100\r\n010111110011101110000110111100\r\n010010001111110000011111010100\r\n001011111111110011101101111010\r\n001101101011100100011011001110\r\n001111110110100110101011000010\r\n000101101011100001101101100100\r\n010011101101111011100111110100\r\n", "output": "328\r\n"}, {"input": "1 68\r\n0101111110111111111111111111110111111111111111111110111111101111111110\r\n", "output": "68\r\n"}, {"input": "2 56\r\n0011111111111110111111111111111111011111111111011111011110\r\n0111111111010111111111110111111111111110111111010111111110\r\n", "output": "113\r\n"}, {"input": "3 17\r\n0111111101111111110\r\n0111111111101011110\r\n0101111111111111110\r\n", "output": "55\r\n"}, {"input": "4 4\r\n011110\r\n010110\r\n010110\r\n011110\r\n", "output": "22\r\n"}, {"input": "5 89\r\n0011111111111101110110111111111101111011111011101110111111111111111111111111111111111111110\r\n0111111111111111111111111101111111111111111111111111111111111111111111111111111111111111110\r\n0111111111111011111111111111111111101111011111111111111111110110111101111111111111111011010\r\n0111111111111111011011111111111011111111111111111111111111111111111111111111111110111111010\r\n0111111101111011111110101011111111110111100100101111111011111111111111011011101111111111110\r\n", "output": "453\r\n"}, {"input": "6 77\r\n0111111110101011111111111111111111111111111111111111100111111111101111111111110\r\n0111111111111111111101111101111111111011111111011111111001011111111111101111110\r\n0111101111111111111111111111111111111110110011111111111011111111101111111111110\r\n0111110111111111111111111111111111111111111111111111011011111111111111111111110\r\n0101111110111111111111111111111111111111111011111111111111111111101111011011110\r\n0110111111101111110111111111111011111111101011111101111111111111111111110111100\r\n", "output": "472\r\n"}, {"input": "7 20\r\n0111111111111111111100\r\n0111110111111111111110\r\n0111111111111111111100\r\n0111111011111111111110\r\n0111111111111011101110\r\n0111101011110111111010\r\n0111111111111111111010\r\n", "output": "151\r\n"}, {"input": "8 8\r\n0111111110\r\n0111101110\r\n0111111110\r\n0111111110\r\n0111111110\r\n0110111100\r\n0101111110\r\n0110111110\r\n", "output": "78\r\n"}, {"input": "11 24\r\n01111111111101111111111110\r\n01111111111111111111111110\r\n01110111111111111111111110\r\n01111111111111111111011110\r\n01111111111111111110111110\r\n01111010111111100111101110\r\n01111111111111010101111100\r\n01111111111111110111111110\r\n01011101111111111101111110\r\n00111111011111111110111110\r\n01111111101111111101111110\r\n", "output": "284\r\n"}, {"input": "12 12\r\n01111111111000\r\n01101111110110\r\n01111110111110\r\n01111111111110\r\n01111111111010\r\n01011111110110\r\n01111111111110\r\n01101101011110\r\n01111111111110\r\n01111101011110\r\n00111111111110\r\n01111111011110\r\n", "output": "166\r\n"}, {"input": "15 28\r\n011111111101011111111101111110\r\n011111111111111111111111111110\r\n011101110111011011101111011110\r\n011111111011111011110111111110\r\n011111111110101111111111111110\r\n011111011111110011111111011010\r\n011110111111001101111111111110\r\n011111111110111111111011111110\r\n011111111111111111111111011110\r\n011111011111111111111011001010\r\n011111111101111111111101111110\r\n011111111110111111101111011110\r\n010111111111101111111111111110\r\n011111111111111111011111111110\r\n011011111111111110110111110110\r\n", "output": "448\r\n"}, {"input": "2 11\r\n0100000000000\r\n0000000010000\r\n", "output": "18\r\n"}, {"input": "1 100\r\n010010010011100001101101110111101010000101010001111001001101011110000011101110101000100111111001101110\r\n", "output": "100\r\n"}, {"input": "15 1\r\n010\r\n010\r\n010\r\n010\r\n010\r\n010\r\n000\r\n000\r\n000\r\n010\r\n000\r\n010\r\n000\r\n000\r\n000\r\n", "output": "29\r\n"}, {"input": "3 3\r\n00010\r\n00000\r\n00010\r\n", "output": "7\r\n"}]
false
stdio
null
true
812/B
812
B
Python 3
TESTS
42
62
0
27490195
read = lambda: map(int, input().split()) n, m = read() a = [input() for i in range(n)] l = [-1] * n r = [-1] * n for i in range(n): ind = n - i - 1 fst = lst = -1 for j in range(1, m + 1): if a[ind][j] == '1': if fst == -1: fst = j lst = j l[i], r[i] = fst, lst ans = 0 cur = 0 p = -1 for i in range(n): p += 1 if l[i] == r[i] == -1: continue ans += p p = 0 k1 = (m + 1 - cur) + 0 + (m + 1 - l[i]) k2 = cur + 0 + r[i] if k1 < k2: cur = l[i] ans += k1 else: cur = r[i] ans += k2 print(ans)
72
62
307,200
27494695
ans = 0 n, m = map(int, input().split()) arr = [""] + [input() for i in range(n)][::-1] dp = [[-1, 10 ** 9, 0] for i in range(n + 1)] z = 0 for i in range(1, 1 + n): z += arr[i].count("1") for i in range(1, n + 1): l = arr[i].find("1") r = arr[i].rfind("1") z -= arr[i].count("1") if l != -1: dp[i][0] = min(dp[i - 1][0] + 2 * r, dp[i - 1][1] + m + 1) + 1 dp[i][1] = min(dp[i - 1][0] + m + 1, dp[i - 1][1] + 2 * (m + 1 - l)) + 1 dp[i][2] = min(dp[i - 1][0] + r, dp[i - 1][1] + (m + 1 - l)) + 1 else: dp[i][0] = min(dp[i - 1][0], dp[i - 1][1] + m + 1) + 1 dp[i][1] = min(dp[i - 1][0] + m + 1, dp[i - 1][1]) + 1 #print(dp[i], l, r, arr[i][l], arr[i]) #print(z, i) if z == 0: ans = dp[i][2] break print(ans)
Codeforces Round 417 (Div. 2)
CF
2,017
1
256
Sagheer, the Hausmeister
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off. The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms. Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights. Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
The first line contains two integers n and m (1 ≤ n ≤ 15 and 1 ≤ m ≤ 100) — the number of floors and the number of rooms in each floor, respectively. The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor. The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
Print a single integer — the minimum total time needed to turn off all the lights.
null
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs. In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor. In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor.
[{"input": "2 2\n0010\n0100", "output": "5"}, {"input": "3 4\n001000\n000010\n000010", "output": "12"}, {"input": "4 3\n01110\n01110\n01110\n01110", "output": "18"}]
1,600
["bitmasks", "brute force", "dp"]
72
[{"input": "2 2\r\n0010\r\n0100\r\n", "output": "5\r\n"}, {"input": "3 4\r\n001000\r\n000010\r\n000010\r\n", "output": "12\r\n"}, {"input": "4 3\r\n01110\r\n01110\r\n01110\r\n01110\r\n", "output": "18\r\n"}, {"input": "3 2\r\n0000\r\n0100\r\n0100\r\n", "output": "4\r\n"}, {"input": "1 89\r\n0000000000000000000000000000000100000000000000010000000000010000000000000000000000000000000\r\n", "output": "59\r\n"}, {"input": "2 73\r\n000000000000000000000000000000000000000000000000000000000000000000000000000\r\n000000000000000000000000000000000000000100000010000000000000000000000000000\r\n", "output": "46\r\n"}, {"input": "3 61\r\n000000000000000000000000000000000000000000000000000000000000000\r\n000000000000000000000000000000000000000000000000000000000000000\r\n000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "0\r\n"}, {"input": "4 53\r\n0000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000\r\n", "output": "0\r\n"}, {"input": "5 93\r\n00000000000000000000000000000000000000000000000000000000100000000000000000000000000000000001010\r\n00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n00000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n00000000000000000000000000000010000000000000000000100000000000000000000000000000000000000000000\r\n00000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "265\r\n"}, {"input": "6 77\r\n0000000000000000100000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000010000000000000\r\n0000000000010000000000000000000000000000000000000000000000000000000000000000010\r\n0000000000000000000001000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000100000000000000000000000000000\r\n", "output": "311\r\n"}, {"input": "7 65\r\n0000000001000000000000000010000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000001000001000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "62\r\n"}, {"input": "8 57\r\n00000000100000000000000000000000000000000000000000000000000\r\n00000000000000010000000000000000000000000000000000000000000\r\n00000000000000000000000000000000000100000000000000000000000\r\n00000000000000000000000000000000000000000000000000000000000\r\n00000000000000000000000000000000000100000000000000000000000\r\n00000000000000000000000000000000000000000000000000000000000\r\n00000000000010000000000000000000000000000000000000000000000\r\n00000000000000000000000000000000000000000000000001000000000\r\n", "output": "277\r\n"}, {"input": "12 13\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n010000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000010000000000\r\n000000000000000\r\n", "output": "14\r\n"}, {"input": "13 1\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n", "output": "0\r\n"}, {"input": "1 33\r\n00000100101110001101000000110100010\r\n", "output": "33\r\n"}, {"input": "2 21\r\n00100110100010010010010\r\n01000001111001010000000\r\n", "output": "43\r\n"}, {"input": "3 5\r\n0001010\r\n0100000\r\n0100000\r\n", "output": "11\r\n"}, {"input": "4 45\r\n00010000101101100000101101000000100000001101100\r\n01110000100111010011000000100000000001000001100\r\n00000000001000100110100001000010011010001010010\r\n01111110100100000101101010011000100100001000000\r\n", "output": "184\r\n"}, {"input": "5 37\r\n010100000000000000000110000110010000010\r\n001101100010110011101000001010101101110\r\n010000001000100010010100000000001010000\r\n000000000100101000000101100001000001110\r\n000010000000000000100001001000011100110\r\n", "output": "193\r\n"}, {"input": "6 25\r\n011001000100111010000101000\r\n000000000010000010001000010\r\n011001100001100001001001010\r\n000000100000010000000000110\r\n010001100001000001000000010\r\n011000001001010111110000100\r\n", "output": "160\r\n"}, {"input": "7 61\r\n010000111100010100001000011010100001000000000011100000100010000\r\n000010011000001000000100110101010001000000010001100000100100100\r\n000010001000001000000100001000000100100011001110000111000000100\r\n000000000101000011010000011000000101000001011001000011101010010\r\n000010010011000000100000110000001000000101000000101000010000010\r\n000010010101101100100100100011001011101010000101000010000101010\r\n000100001100001001000000001000000001011000110010100000000010110\r\n", "output": "436\r\n"}, {"input": "8 49\r\n000100100000000111110010011100110100010010000011000\r\n001000000101111000000001111100010010100000010000000\r\n000000010000011100001000000000101000110010000100100\r\n000000000001000110000011101101000000100000101010000\r\n000000110001000101101000000001000000110001000110000\r\n000100000000000000100100010011000001111101010100110\r\n000000001000000010101111000100001100000000010111000\r\n001000010000110000011100000000100110000010001000000\r\n", "output": "404\r\n"}, {"input": "9 41\r\n0011000000000101001101001000000001110000010\r\n0000110000001010110010110010110010010001000\r\n0001100010100000000001110100100001101000100\r\n0001010101111010000000010010001001011111000\r\n0101000101000011101011000000001100110010000\r\n0001010000000000000001011000000100010101000\r\n0000010011000000001000110001000010110001000\r\n0000100010000110100001000000100010001111100\r\n0000001110100001000001000110001110000100000\r\n", "output": "385\r\n"}, {"input": "10 29\r\n0000000000101001100001001011000\r\n0001110100000000000000100010000\r\n0010001001000011000100010001000\r\n0001000010101000000010100010100\r\n0111000000000000100100100010100\r\n0001000100011111000100010100000\r\n0000000000000001000001001011000\r\n0000101110000001010001011001110\r\n0000001000101010011000001100100\r\n0100010000101011010000000000000\r\n", "output": "299\r\n"}, {"input": "1 57\r\n00011101100001110001111000000100101111000111101100111001000\r\n", "output": "55\r\n"}, {"input": "2 32\r\n0011110111011011011101111101011110\r\n0111000110111111011110011101011110\r\n", "output": "65\r\n"}, {"input": "3 20\r\n0110011111110101101100\r\n0111110000111010100100\r\n0110111110010100011110\r\n", "output": "63\r\n"}, {"input": "4 4\r\n011100\r\n001010\r\n010000\r\n011110\r\n", "output": "22\r\n"}, {"input": "5 44\r\n0001010010001111111001111111000010100100000010\r\n0001111001111001101111011111010110001001111110\r\n0111111010111111011101100011101010100101110110\r\n0011010011101011101111001001010110000111111100\r\n0110100111011100110101110010010011011101100100\r\n", "output": "228\r\n"}, {"input": "6 36\r\n01110101111111110101011000011111110010\r\n00011101100010110111111111110001100100\r\n00001111110010111111101110101110111110\r\n00110110011100100111011110000000000010\r\n01100101101001010001011111100111101100\r\n00011111111011001000011001011110011110\r\n", "output": "226\r\n"}, {"input": "7 24\r\n01111001111001011010010100\r\n00111011010101000111101000\r\n01001110110010010110011110\r\n00000101111011011111111000\r\n01111111101111001001010010\r\n01110000111101011111111010\r\n00000100011100110000110000\r\n", "output": "179\r\n"}, {"input": "8 8\r\n0011101110\r\n0110010100\r\n0100111110\r\n0111111100\r\n0011010100\r\n0001101110\r\n0111100000\r\n0110111000\r\n", "output": "77\r\n"}, {"input": "9 48\r\n00011010111110111011111001111111111101001111110010\r\n01000101000101101101111110111101011100001011010010\r\n00110111110110101110101110111111011011101111011000\r\n00110111111100010110110110111001001111011010101110\r\n01111111100101010011111100100111110011001101110100\r\n01111011110011111101010101010100001110111111111000\r\n01110101101101110001000010110100010110101111111100\r\n00111101001010110010110100000111110101010100001000\r\n00011011010110011111001100111100100011100110110100\r\n", "output": "448\r\n"}, {"input": "10 40\r\n010011001001111011011011101111010001010010\r\n011000000110000010001011111010100000110000\r\n011010101001110010110110011111010101101000\r\n000111111010101111000110011111011011011010\r\n010110101110001001001111111000110011101010\r\n010011010100111110010100100111100111011110\r\n001111101100111111111111001010111010000110\r\n001111110010101100110100101110001011100110\r\n010111010010001111110101111111111110111000\r\n011101101111000100111111111001111100111010\r\n", "output": "418\r\n"}, {"input": "11 28\r\n011100111101101001011111001110\r\n010001111110011101101011001000\r\n001010011011011010101101101100\r\n001100011001101011011001110100\r\n010111110011101110000110111100\r\n010010001111110000011111010100\r\n001011111111110011101101111010\r\n001101101011100100011011001110\r\n001111110110100110101011000010\r\n000101101011100001101101100100\r\n010011101101111011100111110100\r\n", "output": "328\r\n"}, {"input": "1 68\r\n0101111110111111111111111111110111111111111111111110111111101111111110\r\n", "output": "68\r\n"}, {"input": "2 56\r\n0011111111111110111111111111111111011111111111011111011110\r\n0111111111010111111111110111111111111110111111010111111110\r\n", "output": "113\r\n"}, {"input": "3 17\r\n0111111101111111110\r\n0111111111101011110\r\n0101111111111111110\r\n", "output": "55\r\n"}, {"input": "4 4\r\n011110\r\n010110\r\n010110\r\n011110\r\n", "output": "22\r\n"}, {"input": "5 89\r\n0011111111111101110110111111111101111011111011101110111111111111111111111111111111111111110\r\n0111111111111111111111111101111111111111111111111111111111111111111111111111111111111111110\r\n0111111111111011111111111111111111101111011111111111111111110110111101111111111111111011010\r\n0111111111111111011011111111111011111111111111111111111111111111111111111111111110111111010\r\n0111111101111011111110101011111111110111100100101111111011111111111111011011101111111111110\r\n", "output": "453\r\n"}, {"input": "6 77\r\n0111111110101011111111111111111111111111111111111111100111111111101111111111110\r\n0111111111111111111101111101111111111011111111011111111001011111111111101111110\r\n0111101111111111111111111111111111111110110011111111111011111111101111111111110\r\n0111110111111111111111111111111111111111111111111111011011111111111111111111110\r\n0101111110111111111111111111111111111111111011111111111111111111101111011011110\r\n0110111111101111110111111111111011111111101011111101111111111111111111110111100\r\n", "output": "472\r\n"}, {"input": "7 20\r\n0111111111111111111100\r\n0111110111111111111110\r\n0111111111111111111100\r\n0111111011111111111110\r\n0111111111111011101110\r\n0111101011110111111010\r\n0111111111111111111010\r\n", "output": "151\r\n"}, {"input": "8 8\r\n0111111110\r\n0111101110\r\n0111111110\r\n0111111110\r\n0111111110\r\n0110111100\r\n0101111110\r\n0110111110\r\n", "output": "78\r\n"}, {"input": "11 24\r\n01111111111101111111111110\r\n01111111111111111111111110\r\n01110111111111111111111110\r\n01111111111111111111011110\r\n01111111111111111110111110\r\n01111010111111100111101110\r\n01111111111111010101111100\r\n01111111111111110111111110\r\n01011101111111111101111110\r\n00111111011111111110111110\r\n01111111101111111101111110\r\n", "output": "284\r\n"}, {"input": "12 12\r\n01111111111000\r\n01101111110110\r\n01111110111110\r\n01111111111110\r\n01111111111010\r\n01011111110110\r\n01111111111110\r\n01101101011110\r\n01111111111110\r\n01111101011110\r\n00111111111110\r\n01111111011110\r\n", "output": "166\r\n"}, {"input": "15 28\r\n011111111101011111111101111110\r\n011111111111111111111111111110\r\n011101110111011011101111011110\r\n011111111011111011110111111110\r\n011111111110101111111111111110\r\n011111011111110011111111011010\r\n011110111111001101111111111110\r\n011111111110111111111011111110\r\n011111111111111111111111011110\r\n011111011111111111111011001010\r\n011111111101111111111101111110\r\n011111111110111111101111011110\r\n010111111111101111111111111110\r\n011111111111111111011111111110\r\n011011111111111110110111110110\r\n", "output": "448\r\n"}, {"input": "2 11\r\n0100000000000\r\n0000000010000\r\n", "output": "18\r\n"}, {"input": "1 100\r\n010010010011100001101101110111101010000101010001111001001101011110000011101110101000100111111001101110\r\n", "output": "100\r\n"}, {"input": "15 1\r\n010\r\n010\r\n010\r\n010\r\n010\r\n010\r\n000\r\n000\r\n000\r\n010\r\n000\r\n010\r\n000\r\n000\r\n000\r\n", "output": "29\r\n"}, {"input": "3 3\r\n00010\r\n00000\r\n00010\r\n", "output": "7\r\n"}]
false
stdio
null
true
926/H
926
H
Python 3
TESTS
22
265
26,419,200
226617490
n, k = [int(i) for i in input().split()] scores = [int(i) for i in input().split()] colors = input().strip() scores_r = [scores[i] for i in range(n) if colors[i] == 'R'] scores_o = [scores[i] for i in range(n) if colors[i] == 'O'] scores_w = [scores[i] for i in range(n) if colors[i] == 'W'] scores_r.sort(reverse = True) scores_o.sort(reverse = True) scores_w.sort(reverse = True) len_r = len(scores_r) len_w = len(scores_w) len_o = len(scores_o) if len_r == 0 or len_o == 0 or len_r + len_o < k: s_ro = -1 else: s_ro = scores_r[0] + scores_o[0] + sum(sorted(scores_r[1:] + scores_o[1:], reverse = True)[:(k - 2)]) if len_w == 0 or len_o == 0 or len_w + len_o < k: s_wo = -1 else: s_wo = scores_w[0] + scores_o[0] + sum(sorted(scores_w[1:] + scores_o[1:], reverse = True)[:(k - 2)]) print(max(s_ro, s_wo))
98
265
19,251,200
197516180
import sys def solve(first_color, second_color, need): if need < 2 or min(len(first_color), len(second_color)) < 1 or len(first_color) + len(second_color) < need: return -1 total = -first_color[0] - second_color[0] temp = [] for cur_iter in range(1, len(first_color)): temp.append(first_color[cur_iter]) for cur_iter in range(1, len(second_color)): temp.append(second_color[cur_iter]) temp.sort() for cur_iter in range(need - 2): total -= temp[cur_iter] return total n, k = map(int, sys.stdin.readline().strip().split()) costs = list(map(int, sys.stdin.readline().strip().split())) colors = list(sys.stdin.readline().strip()) colored_costs = dict() colored_costs['R'] = [] colored_costs['O'] = [] colored_costs['W'] = [] for i in range(n): colored_costs[colors[i]].append(-costs[i]) for key in colored_costs.keys(): colored_costs[key].sort() ans = solve(colored_costs['R'], colored_costs['O'], k) ans = max(ans, solve(colored_costs['W'], colored_costs['O'], k)) print(ans)
VK Cup 2018 - Wild-card Round 1
ICPC
2,018
1
256
Endless Roses Most Beautiful
Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet.
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose. The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O'  — orange, 'R' — red.
Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1.
null
In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color.
[{"input": "5 3\n4 3 4 1 6\nRROWW", "output": "11"}, {"input": "5 2\n10 20 14 20 11\nRRRRR", "output": "-1"}, {"input": "11 5\n5 6 3 2 3 4 7 5 4 5 6\nRWOORWORROW", "output": "28"}]
2,200
[]
98
[{"input": "5 3\r\n4 3 4 1 6\r\nRROWW\r\n", "output": "11\r\n"}, {"input": "5 2\r\n10 20 14 20 11\r\nRRRRR\r\n", "output": "-1\r\n"}, {"input": "11 5\r\n5 6 3 2 3 4 7 5 4 5 6\r\nRWOORWORROW\r\n", "output": "28\r\n"}, {"input": "15 10\r\n8560 6244 9607 5137 7187 3217 5527 9919 282 8748 3529 6110 5767 521 3393\r\nOWRWOORWRORWWRO\r\n", "output": "64282\r\n"}, {"input": "10 4\r\n1208 5835 2637 5827 3722 6837 3499 6438 43 5333\r\nWRRWRWRWRW\r\n", "output": "-1\r\n"}, {"input": "13 3\r\n9675 8988 5499 6356 5083 6067 5580 4580 6735 3617 9536 8218 3265\r\nRRWRRROWRWWWW\r\n", "output": "24243\r\n"}, {"input": "13 7\r\n8543 3460 1282 3956 8203 762 6059 9361 4427 8868 5849 3439 8891\r\nWWOOOOWOWWRWO\r\n", "output": "54352\r\n"}, {"input": "30 15\r\n7926 577 5009 7237 4395 3239 8994 4429 8126 2925 139 320 4442 3397 1292 2800 9505 6043 5946 8058 4031 6871 4689 1977 73 440 5320 5290 4707 387\r\nOOWOWWORRWOWORWRRRRWORROOWWROW\r\n", "output": "91633\r\n"}, {"input": "1 1\r\n100\r\nO\r\n", "output": "-1\r\n"}, {"input": "1 1\r\n1059\r\nO\r\n", "output": "-1\r\n"}, {"input": "2 2\r\n9907 4483\r\nOO\r\n", "output": "-1\r\n"}, {"input": "1 1\r\n6750\r\nW\r\n", "output": "-1\r\n"}, {"input": "2 2\r\n144 174\r\nOW\r\n", "output": "318\r\n"}, {"input": "3 2\r\n776 4797 9449\r\nOWO\r\n", "output": "14246\r\n"}, {"input": "2 2\r\n3486 8968\r\nWW\r\n", "output": "-1\r\n"}, {"input": "3 2\r\n2330 2140 3440\r\nWOW\r\n", "output": "5580\r\n"}, {"input": "4 2\r\n1175 8186 4321 1810\r\nWWOO\r\n", "output": "12507\r\n"}, {"input": "1 1\r\n6479\r\nR\r\n", "output": "-1\r\n"}, {"input": "2 2\r\n8512 9903\r\nOR\r\n", "output": "18415\r\n"}, {"input": "3 2\r\n7035 5046 7357\r\nOOR\r\n", "output": "14392\r\n"}, {"input": "2 2\r\n6442 4558\r\nWR\r\n", "output": "-1\r\n"}, {"input": "3 2\r\n9700 698 2122\r\nOWR\r\n", "output": "11822\r\n"}, {"input": "4 3\r\n254 4510 2194 9543\r\nWOOR\r\n", "output": "16247\r\n"}, {"input": "3 2\r\n517 6744 2364\r\nRWW\r\n", "output": "-1\r\n"}, {"input": "4 2\r\n2884 2918 8629 9905\r\nRWOW\r\n", "output": "18534\r\n"}, {"input": "5 2\r\n7882 871 789 4432 7664\r\nOWORW\r\n", "output": "15546\r\n"}, {"input": "2 2\r\n2926 8428\r\nRR\r\n", "output": "-1\r\n"}, {"input": "3 2\r\n7926 1770 3255\r\nORR\r\n", "output": "11181\r\n"}, {"input": "4 2\r\n2578 7910 108 3809\r\nOROR\r\n", "output": "10488\r\n"}, {"input": "3 2\r\n5920 9303 7542\r\nWRR\r\n", "output": "-1\r\n"}, {"input": "4 2\r\n5909 4286 5444 6473\r\nOWRR\r\n", "output": "12382\r\n"}, {"input": "5 2\r\n96 6627 8780 3764 970\r\nRROWO\r\n", "output": "15407\r\n"}, {"input": "4 2\r\n6657 1489 9138 4273\r\nRRWW\r\n", "output": "-1\r\n"}, {"input": "5 2\r\n1598 6700 334 6455 9292\r\nWORWR\r\n", "output": "15992\r\n"}, {"input": "6 2\r\n6231 9178 9845 5932 5477 6659\r\nORRWOW\r\n", "output": "16076\r\n"}, {"input": "1 1\r\n780\r\nO\r\n", "output": "-1\r\n"}, {"input": "1 1\r\n3214\r\nW\r\n", "output": "-1\r\n"}, {"input": "2 2\r\n8455 5432\r\nOW\r\n", "output": "13887\r\n"}, {"input": "1 1\r\n6555\r\nR\r\n", "output": "-1\r\n"}, {"input": "2 2\r\n1120 5670\r\nOR\r\n", "output": "6790\r\n"}, {"input": "3 2\r\n8884 4514 1673\r\nORW\r\n", "output": "13398\r\n"}, {"input": "1 1\r\n6908\r\nO\r\n", "output": "-1\r\n"}, {"input": "1 1\r\n3934\r\nW\r\n", "output": "-1\r\n"}, {"input": "2 2\r\n8856 7299\r\nWO\r\n", "output": "16155\r\n"}, {"input": "1 1\r\n2683\r\nR\r\n", "output": "-1\r\n"}, {"input": "2 2\r\n9094 5772\r\nRO\r\n", "output": "14866\r\n"}, {"input": "3 2\r\n518 9438 7938\r\nWOR\r\n", "output": "17376\r\n"}, {"input": "10 4\r\n9513 754 5917 1337 2337 1387 3499 9873 9138 7647\r\nWROWWOWWRO\r\n", "output": "32950\r\n"}, {"input": "20 5\r\n3747 219 7826 7713 6886 466 1136 7069 1234 7556 3657 6017 9965 2847 6880 362 3179 4676 5934 4913\r\nWWWOWOWOWOROWOWWWOWW\r\n", "output": "40129\r\n"}]
false
stdio
null
true
733/C
733
C
PyPy 3
TESTS
9
140
0
75972933
n = int(input()) scum = list(map(int,input().split())) k = int(input()) bag = list(map(int,input().split())) hoot = [] www = 0 w = 0 getsome = [] for i in range(k): c = 0 for j in range(w,n): c+=scum[j] if c == bag[i]: hoot.append(j+1) w = j+1 break if c>bag[i]: www = 1 break if www == 1: break if c<bag[i]: www = 1 break if www == 1 or j<n-1: print("NO") else: lost = 0 for i in range(len(hoot)): hell = 0 virginity = max(scum[lost:hoot[i]]) if virginity == bag[i]: lost = hoot[i] continue for j in range(lost,hoot[i]): if scum[j] == virginity: if j!=lost and j!=hoot[i] - 1: if scum[j] >scum[j-1] or scum[j] > scum[j+1]: hell = 1 quirks = j break else: if j == lost: if scum[j] > scum[j+1]: hell = 1 quirks = j break else: if scum[j] > scum[j-1]: hell = 1 quirks = j break if hell == 1: count = quirks - lost ghetto = hoot[i] - quirks - 1 if scum[quirks]>scum[quirks-1]: for v in range(count): getsome.append([(count-(v)) + i+1,"L"]) for v in range(ghetto): getsome.append([i+1,"R"]) else: for v in range(ghetto): getsome.append([count + i+1,"R"]) for v in range(count): getsome.append([count-(v) + i+1,"L"]) else: break lost = hoot[i] if hell == 0: print("NO") else: print("YES") for i in getsome: print(i[0],i[1])
139
46
0
230899366
f = lambda: list(map(int, input().split())) def g(i, j, k): z = y = i for x in range(i, j): if a[x] > a[y]: y = x if y > i: z = y else: while a[y] == a[i]: y += 1 if y == j: return [] y -= 1 l = [str(k + y - x) + ' L' for x in range(i, y)] r = [str(k + y - z) + ' R' for x in range(y + 1, j)] return r + l if z == i else l + r n, a, m, b = f()[0], f(), f()[0], f() d = ['YES'] s = i = k = 0 for j, q in enumerate(a, 1): s += q if k < m and s == b[k]: d += g(i, j, k + 1) s, i, k = 0, j, k + 1 print('\n'.join(d) if len(d) + m > n and k == m else 'NO')
Codeforces Round 378 (Div. 2)
CF
2,016
1
256
Epidemic in Monstropolis
There was an epidemic in Monstropolis and all monsters became sick. To recover, all monsters lined up in queue for an appointment to the only doctor in the city. Soon, monsters became hungry and began to eat each other. One monster can eat other monster if its weight is strictly greater than the weight of the monster being eaten, and they stand in the queue next to each other. Monsters eat each other instantly. There are no monsters which are being eaten at the same moment. After the monster A eats the monster B, the weight of the monster A increases by the weight of the eaten monster B. In result of such eating the length of the queue decreases by one, all monsters after the eaten one step forward so that there is no empty places in the queue again. A monster can eat several monsters one after another. Initially there were n monsters in the queue, the i-th of which had weight ai. For example, if weights are [1, 2, 2, 2, 1, 2] (in order of queue, monsters are numbered from 1 to 6 from left to right) then some of the options are: 1. the first monster can't eat the second monster because a1 = 1 is not greater than a2 = 2; 2. the second monster can't eat the third monster because a2 = 2 is not greater than a3 = 2; 3. the second monster can't eat the fifth monster because they are not neighbors; 4. the second monster can eat the first monster, the queue will be transformed to [3, 2, 2, 1, 2]. After some time, someone said a good joke and all monsters recovered. At that moment there were k (k ≤ n) monsters in the queue, the j-th of which had weight bj. Both sequences (a and b) contain the weights of the monsters in the order from the first to the last. You are required to provide one of the possible orders of eating monsters which led to the current queue, or to determine that this could not happen. Assume that the doctor didn't make any appointments while monsters were eating each other.
The first line contains single integer n (1 ≤ n ≤ 500) — the number of monsters in the initial queue. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial weights of the monsters. The third line contains single integer k (1 ≤ k ≤ n) — the number of monsters in the queue after the joke. The fourth line contains k integers b1, b2, ..., bk (1 ≤ bj ≤ 5·108) — the weights of the monsters after the joke. Monsters are listed in the order from the beginning of the queue to the end.
In case if no actions could lead to the final queue, print "NO" (without quotes) in the only line. Otherwise print "YES" (without quotes) in the first line. In the next n - k lines print actions in the chronological order. In each line print x — the index number of the monster in the current queue which eats and, separated by space, the symbol 'L' if the monster which stays the x-th in the queue eats the monster in front of him, or 'R' if the monster which stays the x-th in the queue eats the monster behind him. After each eating the queue is enumerated again. When one monster eats another the queue decreases. If there are several answers, print any of them.
null
In the first example, initially there were n = 6 monsters, their weights are [1, 2, 2, 2, 1, 2] (in order of queue from the first monster to the last monster). The final queue should be [5, 5]. The following sequence of eatings leads to the final queue: - the second monster eats the monster to the left (i.e. the first monster), queue becomes [3, 2, 2, 1, 2]; - the first monster (note, it was the second on the previous step) eats the monster to the right (i.e. the second monster), queue becomes [5, 2, 1, 2]; - the fourth monster eats the mosnter to the left (i.e. the third monster), queue becomes [5, 2, 3]; - the finally, the third monster eats the monster to the left (i.e. the second monster), queue becomes [5, 5]. Note that for each step the output contains numbers of the monsters in their current order in the queue.
[{"input": "6\n1 2 2 2 1 2\n2\n5 5", "output": "YES\n2 L\n1 R\n4 L\n3 L"}, {"input": "5\n1 2 3 4 5\n1\n15", "output": "YES\n5 L\n4 L\n3 L\n2 L"}, {"input": "5\n1 1 1 3 3\n3\n2 1 6", "output": "NO"}]
1,800
["constructive algorithms", "dp", "greedy", "two pointers"]
139
[{"input": "6\r\n1 2 2 2 1 2\r\n2\r\n5 5\r\n", "output": "YES\r\n2 L\r\n1 R\r\n4 L\r\n3 L\r\n"}, {"input": "5\r\n1 2 3 4 5\r\n1\r\n15\r\n", "output": "YES\r\n5 L\r\n4 L\r\n3 L\r\n2 L\r\n"}, {"input": "5\r\n1 1 1 3 3\r\n3\r\n2 1 6\r\n", "output": "NO"}, {"input": "5\r\n1 1 1 1 2\r\n3\r\n1 1 4\r\n", "output": "YES\r\n5 L\r\n4 L\r\n"}, {"input": "5\r\n1 1 1 1 1\r\n4\r\n1 1 2 1\r\n", "output": "NO"}, {"input": "6\r\n2 1 2 2 1 2\r\n2\r\n5 5\r\n", "output": "YES\r\n3 L\r\n2 L\r\n4 L\r\n3 L\r\n"}, {"input": "8\r\n2 5 3 1 4 2 3 4\r\n3\r\n10 6 8\r\n", "output": "NO"}, {"input": "1\r\n959139\r\n1\r\n470888\r\n", "output": "NO"}, {"input": "3\r\n2 2 1\r\n1\r\n5\r\n", "output": "YES\r\n2 R\r\n2 L\r\n"}, {"input": "3\r\n1 2 2\r\n1\r\n5\r\n", "output": "YES\r\n2 L\r\n1 R\r\n"}, {"input": "5\r\n1 2 3 4 5\r\n1\r\n10\r\n", "output": "NO"}, {"input": "5\r\n325539 329221 106895 882089 718673\r\n5\r\n699009 489855 430685 939232 282330\r\n", "output": "NO"}, {"input": "10\r\n30518 196518 274071 359971 550121 204862 843967 173607 619138 690754\r\n3\r\n171337 183499 549873\r\n", "output": "NO"}, {"input": "3\r\n2 1 1\r\n1\r\n3\r\n", "output": "NO"}, {"input": "4\r\n2 2 2 1\r\n3\r\n2 2 2\r\n", "output": "NO"}, {"input": "3\r\n1 2 3\r\n1\r\n3\r\n", "output": "NO"}, {"input": "2\r\n1 2\r\n2\r\n3 1\r\n", "output": "NO"}, {"input": "5\r\n3 3 2 2 1\r\n2\r\n8 3\r\n", "output": "YES\r\n2 R\r\n2 L\r\n2 R\r\n"}, {"input": "3\r\n3 2 5\r\n1\r\n10\r\n", "output": "YES\r\n3 L\r\n2 L\r\n"}, {"input": "3\r\n1 5 1\r\n1\r\n6\r\n", "output": "NO"}, {"input": "5\r\n1 2 3 4 5\r\n3\r\n1 2 3\r\n", "output": "NO"}, {"input": "3\r\n5 2 3\r\n1\r\n10\r\n", "output": "YES\r\n1 R\r\n1 R\r\n"}, {"input": "3\r\n2 1 3\r\n1\r\n6\r\n", "output": "YES\r\n3 L\r\n2 L\r\n"}, {"input": "3\r\n3 2 1\r\n1\r\n6\r\n", "output": "YES\r\n1 R\r\n1 R\r\n"}, {"input": "2\r\n5 5\r\n1\r\n5\r\n", "output": "NO"}, {"input": "3\r\n1 2 3\r\n2\r\n1 2\r\n", "output": "NO"}, {"input": "4\r\n1 2 3 4\r\n3\r\n1 2 3\r\n", "output": "NO"}, {"input": "4\r\n4 3 2 1\r\n3\r\n3 2 1\r\n", "output": "NO"}, {"input": "2\r\n5 3\r\n1\r\n5\r\n", "output": "NO"}, {"input": "5\r\n1 1 1 1 1\r\n4\r\n1 1 1 1\r\n", "output": "NO"}, {"input": "3\r\n3 3 2\r\n1\r\n8\r\n", "output": "YES\r\n2 R\r\n2 L\r\n"}, {"input": "8\r\n2 2 1 2 2 1 2 4\r\n2\r\n9 8\r\n", "output": "NO"}, {"input": "4\r\n3 2 1 4\r\n3\r\n3 2 1\r\n", "output": "NO"}, {"input": "5\r\n3 3 2 3 1\r\n2\r\n11 1\r\n", "output": "YES\r\n4 L\r\n3 L\r\n2 L\r\n"}, {"input": "3\r\n2 1 3\r\n1\r\n3\r\n", "output": "NO"}, {"input": "4\r\n2 3 3 2\r\n2\r\n5 3\r\n", "output": "NO"}, {"input": "16\r\n2 2 2 1 2 2 2 1 1 2 2 2 1 2 2 2\r\n4\r\n7 7 7 7\r\n", "output": "YES\r\n3 R\r\n3 L\r\n2 L\r\n4 R\r\n4 L\r\n3 L\r\n4 L\r\n3 R\r\n3 R\r\n5 L\r\n4 R\r\n4 R\r\n"}, {"input": "2\r\n1 1\r\n1\r\n1\r\n", "output": "NO"}, {"input": "3\r\n1 2 1\r\n2\r\n3 2\r\n", "output": "NO"}, {"input": "3\r\n2 3 5\r\n1\r\n10\r\n", "output": "YES\r\n3 L\r\n2 L\r\n"}, {"input": "5\r\n1 2 3 4 5\r\n2\r\n3 7\r\n", "output": "NO"}, {"input": "4\r\n1 2 3 4\r\n2\r\n1 2\r\n", "output": "NO"}, {"input": "8\r\n1 2 2 2 1 2 1 1\r\n2\r\n5 5\r\n", "output": "NO"}, {"input": "3\r\n5 5 4\r\n1\r\n14\r\n", "output": "YES\r\n2 R\r\n2 L\r\n"}, {"input": "22\r\n3 2 3 3 3 1 1 2 1 2 1 1 1 2 2 3 1 2 3 3 3 3\r\n5\r\n5 16 5 5 15\r\n", "output": "YES\r\n1 R\r\n4 R\r\n4 R\r\n4 R\r\n4 R\r\n4 R\r\n4 L\r\n3 L\r\n6 L\r\n5 L\r\n4 L\r\n5 L\r\n7 L\r\n6 L\r\n5 R\r\n5 R\r\n5 R\r\n"}, {"input": "4\r\n2 2 1 2\r\n1\r\n7\r\n", "output": "YES\r\n4 L\r\n3 L\r\n2 L\r\n"}, {"input": "7\r\n2 2 2 1 2 2 2\r\n1\r\n13\r\n", "output": "YES\r\n5 L\r\n4 L\r\n3 L\r\n2 L\r\n1 R\r\n1 R\r\n"}, {"input": "2\r\n1 2\r\n1\r\n1\r\n", "output": "NO"}, {"input": "14\r\n5 5 5 5 4 4 4 3 3 3 4 4 4 4\r\n3\r\n32 21 4\r\n", "output": "YES\r\n4 R\r\n4 R\r\n4 R\r\n4 L\r\n3 L\r\n2 L\r\n5 L\r\n4 L\r\n3 L\r\n2 R\r\n2 R\r\n"}, {"input": "5\r\n2 2 1 2 2\r\n1\r\n9\r\n", "output": "YES\r\n4 L\r\n3 L\r\n2 L\r\n1 R\r\n"}, {"input": "1\r\n2\r\n1\r\n2\r\n", "output": "YES\r\n"}, {"input": "23\r\n3 2 1 3 3 3 1 1 2 1 2 1 1 1 2 2 3 1 2 3 3 3 3\r\n5\r\n6 16 5 5 15\r\n", "output": "YES\r\n1 R\r\n1 R\r\n4 R\r\n4 R\r\n4 R\r\n4 R\r\n4 R\r\n4 L\r\n3 L\r\n6 L\r\n5 L\r\n4 L\r\n5 L\r\n7 L\r\n6 L\r\n5 R\r\n5 R\r\n5 R\r\n"}]
false
stdio
import sys def read_file(path): with open(path, 'r') as f: return f.read().splitlines() input_path, output_path, submission_path = sys.argv[1], sys.argv[2], sys.argv[3] input_lines = read_file(input_path) n = int(input_lines[0]) a = list(map(int, input_lines[1].split())) k = int(input_lines[2]) b = list(map(int, input_lines[3].split())) ref_lines = read_file(output_path) submission_lines = read_file(submission_path) if not submission_lines: print(0) sys.exit(0) submission_first_line = submission_lines[0].strip() ref_first_line = ref_lines[0].strip() if submission_first_line != ref_first_line: print(0) sys.exit(0) if submission_first_line == "NO": print(1) sys.exit(0) # Now, handle YES case expected_steps = n - k if len(submission_lines) - 1 != expected_steps: print(0) sys.exit(0) current_queue = a.copy() valid = True for step in submission_lines[1:]: step = step.strip() if not step: valid = False break parts = step.split() if len(parts) != 2: valid = False break try: x = int(parts[0]) direction = parts[1] except: valid = False break m = len(current_queue) if x < 1 or x > m: valid = False break if direction not in ['L', 'R']: valid = False break if direction == 'L': if x == 1: valid = False break eater_pos = x - 1 eaten_pos = x - 2 else: if x == m: valid = False break eater_pos = x - 1 eaten_pos = x eater = current_queue[eater_pos] eaten = current_queue[eaten_pos] if eater <= eaten: valid = False break # Merge the monsters new_eater = eater + eaten if direction == 'L': current_queue = current_queue[:eaten_pos] + [new_eater] + current_queue[eater_pos+1:] else: current_queue = current_queue[:eater_pos] + [new_eater] + current_queue[eaten_pos+1:] # Check final queue if valid and current_queue == b: print(1) else: print(0)
true
416/D
416
D
Python 3
TESTS
77
358
16,588,800
6610195
import sys import math n = int(sys.stdin.readline()) if n <= 2: print(1) sys.exit(0) a = [int(s) for s in sys.stdin.readline().split()] st = -1 # index of first positive number in current subset of a ed = -1 # index last positive number in current subset of a # differation is (a[ed] - a[st])/(ed - st) leading_zeros = 0 # -1 before a[st] seg_count = 1 for (i, v) in enumerate(a): if v == -1: if st == -1: leading_zeros += 1 else: if ed != -1: # check if v should be a non-positive number if a[ed] + (i-ed) * (a[ed] - a[st])/(ed-st) <= 0: st = -1 ed = -1 leading_zeros = 1 seg_count += 1 else: pass else: pass else: if st == -1: st = i # find first positive number else: if ed == -1: #print(i) if (v - a[st]) % (i-st) != 0 or a[st] - (v-a[st])/(i-st) * leading_zeros <= 0: # a[st..i] can't be an arithmetic progression st = -1 ed = -1 seg_count += 1 leading_zeros = 0 else: ed = i else: if (v-a[ed]) * (ed - st) != (a[ed] - a[st]) * (i-ed): st = i ed = -1 seg_count += 1 leading_zeros = 0 else: ed = i #leave ed the first positive number after a[st] is also ok #print( "[" +str(st) + " " + str(ed) + "] " + str(seg_count) + " " + str(leading_zeros) ) print(seg_count)
84
373
16,384,000
6954252
n = int(input()) a = list(map(int, input().split())) i = 0 ans = 0 while i < n: ans += 1 i1 = i while i1 < n and a[i1] == -1: i1 += 1 if i1 == n: break i2 = i1 + 1 while i2 < n and a[i2] == -1: i2 += 1 if i2 == n: break dist = i2 - i1 step = (a[i2] - a[i1]) // dist if (a[i2] - a[i1]) % dist != 0 or (step > 0 and a[i1] - (i1 - i) * step <= 0): i = i2 continue i3 = i2 + 1 while i3 < n: nxt = a[i2] + step * (i3 - i2) if nxt <= 0 or (a[i3] != -1 and a[i3] != nxt): break i3 += 1 i = i3 print(ans)
Codeforces Round 241 (Div. 2)
CF
2,014
1
256
Population Size
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 a1, a2, ..., an, 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 ai ​​may be unknown. Such values are represented by number -1. For a given sequence a = (a1, a2, ..., an), 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 ai > 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 ci + 1 - ci 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 a1, a2, ..., an separated by a space (1 ≤ ai ≤ 109 or ai = - 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.
null
null
[{"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"}]
2,400
["greedy", "implementation", "math"]
84
[{"input": "9\r\n8 6 4 2 1 4 7 10 2\r\n", "output": "3\r\n"}, {"input": "9\r\n-1 6 -1 2 -1 4 7 -1 2\r\n", "output": "3\r\n"}, {"input": "5\r\n-1 -1 -1 -1 -1\r\n", "output": "1\r\n"}, {"input": "7\r\n-1 -1 4 5 1 2 3\r\n", "output": "2\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n65\r\n", "output": "1\r\n"}, {"input": "1\r\n1000000000\r\n", "output": "1\r\n"}, {"input": "1\r\n-1\r\n", "output": "1\r\n"}, {"input": "2\r\n1000000000 1000000000\r\n", "output": "1\r\n"}, {"input": "2\r\n1000000000 -1\r\n", "output": "1\r\n"}, {"input": "2\r\n-1 1000000000\r\n", "output": "1\r\n"}, {"input": "2\r\n-1 -1\r\n", "output": "1\r\n"}, {"input": "3\r\n999999999 1000000000 -1\r\n", "output": "1\r\n"}, {"input": "3\r\n999999999 -1 1000000000\r\n", "output": "2\r\n"}, {"input": "3\r\n1000000000 999999999 1000000000\r\n", "output": "2\r\n"}, {"input": "3\r\n-1 1000000000 999999999\r\n", "output": "1\r\n"}, {"input": "3\r\n-1 1000000000 -1\r\n", "output": "1\r\n"}, {"input": "3\r\n-1 1 2\r\n", "output": "2\r\n"}, {"input": "3\r\n-1 1 1000000000\r\n", "output": "2\r\n"}, {"input": "5\r\n-1 1 7 -1 5\r\n", "output": "2\r\n"}, {"input": "7\r\n-1 2 4 -1 4 1 5\r\n", "output": "3\r\n"}, {"input": "2\r\n-1 21\r\n", "output": "1\r\n"}, {"input": "3\r\n39 42 -1\r\n", "output": "1\r\n"}, {"input": "4\r\n45 -1 41 -1\r\n", "output": "1\r\n"}, {"input": "5\r\n-1 40 42 -1 46\r\n", "output": "1\r\n"}, {"input": "6\r\n-1 6 1 -1 -1 -1\r\n", "output": "2\r\n"}, {"input": "7\r\n32 33 34 -1 -1 37 38\r\n", "output": "1\r\n"}, {"input": "8\r\n-1 12 14 16 18 20 -1 -1\r\n", "output": "1\r\n"}, {"input": "9\r\n42 39 36 33 -1 -1 -1 34 39\r\n", "output": "2\r\n"}, {"input": "10\r\n29 27 -1 23 42 -1 -1 45 -1 -1\r\n", "output": "2\r\n"}, {"input": "5\r\n40 -1 44 46 48\r\n", "output": "1\r\n"}, {"input": "6\r\n43 40 37 34 -1 -1\r\n", "output": "1\r\n"}, {"input": "7\r\n11 8 5 -1 -1 -1 -1\r\n", "output": "2\r\n"}, {"input": "8\r\n-1 12 14 16 18 20 -1 -1\r\n", "output": "1\r\n"}, {"input": "9\r\n42 39 36 33 -1 -1 -1 34 39\r\n", "output": "2\r\n"}, {"input": "10\r\n29 27 -1 23 42 -1 -1 45 -1 -1\r\n", "output": "2\r\n"}, {"input": "11\r\n9 21 17 13 -1 -1 -1 -1 -1 -1 -1\r\n", "output": "3\r\n"}, {"input": "12\r\n-1 17 -1 54 -1 64 -1 74 79 84 -1 94\r\n", "output": "2\r\n"}, {"input": "13\r\n25 24 23 22 24 27 -1 33 -1 2 2 2 -1\r\n", "output": "3\r\n"}, {"input": "14\r\n-1 5 3 -1 -1 31 31 31 -1 31 -1 -1 4 7\r\n", "output": "3\r\n"}, {"input": "15\r\n-1 28 -1 32 34 26 -1 26 -1 -1 26 26 26 -1 -1\r\n", "output": "2\r\n"}, {"input": "16\r\n3 8 13 18 23 -1 -1 -1 43 48 53 45 -1 -1 -1 -1\r\n", "output": "2\r\n"}, {"input": "17\r\n-1 -1 -1 -1 64 68 72 -1 45 46 47 48 49 50 51 52 53\r\n", "output": "2\r\n"}, {"input": "18\r\n21 19 -1 -1 -1 48 50 -1 54 -1 5 1 -1 -1 -1 37 36 35\r\n", "output": "4\r\n"}, {"input": "19\r\n23 26 -1 -1 35 38 41 -1 -1 -1 53 -1 59 62 6 7 8 9 -1\r\n", "output": "2\r\n"}, {"input": "6\r\n-1 2 6 -1 -1 6\r\n", "output": "2\r\n"}, {"input": "8\r\n-1 -1 1 7 -1 9 5 2\r\n", "output": "3\r\n"}, {"input": "20\r\n-1 32 37 -1 -1 -1 57 -1 -1 40 31 33 -1 -1 39 47 43 -1 35 32\r\n", "output": "5\r\n"}, {"input": "13\r\n2 -1 3 1 3 1 -1 1 3 -1 -1 1 1\r\n", "output": "6\r\n"}, {"input": "3\r\n-1 1 -1\r\n", "output": "1\r\n"}]
false
stdio
null
true
581/C
581
C
PyPy 3-64
TESTS
95
155
14,848,000
231484514
from os import path from sys import stdin, stdout filename = "../templates/input.txt" if path.exists(filename): stdin = open(filename, 'r') def input(): return stdin.readline().rstrip() def print(*args, sep=' ', end='\n'): stdout.write(sep.join(map(str, args))) stdout.write(end) def solution(): n, k = [int(num) for num in input().split()] a = [int(num) for num in input().split()] a.sort(key=lambda x: 10 - x % 10) i = 0 while i < n and k: x = min(k, 10 - a[i] % 10) a[i] += x k -= x i += 1 ans = 0 for ai in a: ans += ai // 10 if k: y = sum(100 - ai for ai in a) ans += min(y // 10, k // 10) print(ans) def main(): t = 1 while t: solution() t -= 1 if __name__ == '__main__': main()
98
109
13,414,400
229663137
n, k = map(int, input().split()) a = list(map(int, input().split())) s = 0 r = [(10 - x % 10) % 10 for x in a] s += sum(x // 10 for x in a) r.sort() for i in range(n): if r[i]: if r[i] <= k: k -= r[i] s += 1 s += k // 10 s = min(s, n * 10) print(s)# 1698223430.592529
Codeforces Round 322 (Div. 2)
CF
2,015
1
256
Developing Skills
Petya loves computer games. Finally a game that he's been waiting for so long came out! The main character of this game has n different skills, each of which is characterized by an integer ai from 0 to 100. The higher the number ai is, the higher is the i-th skill of the character. The total rating of the character is calculated as the sum of the values ​​of $$\left\lfloor \frac{a_i}{10} \right\rfloor$$ for all i from 1 to n. The expression ⌊ x⌋ denotes the result of rounding the number x down to the nearest integer. At the beginning of the game Petya got k improvement units as a bonus that he can use to increase the skills of his character and his total rating. One improvement unit can increase any skill of Petya's character by exactly one. For example, if a4 = 46, after using one imporvement unit to this skill, it becomes equal to 47. A hero's skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused. Your task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units.
The first line of the input contains two positive integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 107) — the number of skills of the character and the number of units of improvements at Petya's disposal. The second line of the input contains a sequence of n integers ai (0 ≤ ai ≤ 100), where ai characterizes the level of the i-th skill of the character.
The first line of the output should contain a single non-negative integer — the maximum total rating of the character that Petya can get using k or less improvement units.
null
In the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating of the character becomes equal to lfloor frac{100}{10} rfloor + lfloor frac{100}{10} rfloor = 10 + 10 = 20. In the second test the optimal strategy for Petya is to improve the first skill to 20 (by spending 3 improvement units) and to improve the third skill to 20 (in this case by spending 1 improvement units). Thus, Petya is left with 4 improvement units and he will be able to increase the second skill to 19 (which does not change the overall rating, so Petya does not necessarily have to do it). Therefore, the highest possible total rating in this example is $$\lfloor \frac { 20 } { 10 } \rfloor + \lfloor \frac { 19 } { 10 } \rfloor + \lfloor \frac { 20 } { 10 } \rfloor = 2 + 1 + 2 = 5$$. In the third test case the optimal strategy for Petya is to increase the first skill to 100 by spending 1 improvement unit. Thereafter, both skills of the character will be equal to 100, so Petya will not be able to spend the remaining improvement unit. So the answer is equal to $$\lfloor \frac { 100 } { 10 } \rfloor + \lfloor \frac { 100 } { 10 } \rfloor = 10 + 10 = 20$$.
[{"input": "2 4\n7 9", "output": "2"}, {"input": "3 8\n17 15 19", "output": "5"}, {"input": "2 2\n99 100", "output": "20"}]
1,400
["implementation", "math", "sortings"]
98
[{"input": "2 4\r\n7 9\r\n", "output": "2\r\n"}, {"input": "3 8\r\n17 15 19\r\n", "output": "5\r\n"}, {"input": "2 2\r\n99 100\r\n", "output": "20\r\n"}, {"input": "100 10000\r\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 0 0 0 0 0 0 0 0 0 0 0\r\n", "output": "1000\r\n"}, {"input": "100 10000\r\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\r\n", "output": "1000\r\n"}, {"input": "1 16\r\n78\r\n", "output": "9\r\n"}, {"input": "2 33\r\n30 88\r\n", "output": "15\r\n"}, {"input": "3 9\r\n93 62 7\r\n", "output": "16\r\n"}, {"input": "5 145\r\n19 77 59 1 63\r\n", "output": "36\r\n"}, {"input": "7 168\r\n2 71 56 58 42 61 39\r\n", "output": "49\r\n"}, {"input": "10 217\r\n48 30 82 70 10 5 34 11 90 90\r\n", "output": "68\r\n"}, {"input": "15 204\r\n19 81 24 22 59 46 48 8 1 66 100 20 46 56 61\r\n", "output": "86\r\n"}, {"input": "20 484\r\n24 72 72 13 85 50 52 3 81 79 71 57 57 75 6 52 54 41 61 73\r\n", "output": "156\r\n"}, {"input": "30 825\r\n33 25 61 69 92 38 2 62 73 78 83 32 25 5 5 82 64 93 38 25 52 9 40 52 38 90 25 85 99 20\r\n", "output": "232\r\n"}, {"input": "40 700\r\n43 35 51 91 44 51 86 20 64 10 50 40 16 25 37 89 18 44 94 99 18 30 11 27 73 3 90 78 28 98 87 43 85 88 29 93 6 81 78 16\r\n", "output": "276\r\n"}, {"input": "50 1607\r\n19 55 52 35 18 39 3 12 55 78 62 83 85 56 36 86 96 28 70 40 40 83 27 2 51 49 87 28 58 75 27 69 36 82 78 29 99 87 29 78 82 78 15 85 52 32 90 6 1 76\r\n", "output": "424\r\n"}, {"input": "60 2213\r\n17 98 74 91 59 84 87 71 13 9 74 48 75 76 36 25 49 80 25 92 41 24 99 45 98 95 27 54 88 63 25 50 19 43 15 90 58 48 58 83 37 88 35 63 63 23 27 82 80 7 82 93 71 18 85 17 13 2 50 74\r\n", "output": "552\r\n"}, {"input": "70 1313\r\n27 7 64 45 44 29 37 63 38 9 85 56 43 74 46 55 59 97 13 33 75 78 2 88 32 7 24 36 86 40 66 42 26 48 64 14 50 21 20 10 50 73 21 29 17 46 97 90 81 73 61 25 95 82 93 94 72 38 80 13 3 3 20 90 34 20 24 49 96 51\r\n", "output": "468\r\n"}, {"input": "40 108\r\n20 100 99 50 8 78 44 67 91 75 93 53 96 81 96 86 81 0 58 9 51 63 70 73 80 79 28 82 4 15 60 74 19 17 54 81 11 67 71 66\r\n", "output": "245\r\n"}, {"input": "50 284\r\n61 25 82 73 57 61 90 22 63 99 58 4 27 54 8 29 46 99 73 73 60 42 45 17 75 86 38 83 4 1 67 44 74 87 32 33 14 95 87 46 40 3 37 6 42 38 51 39 98 48\r\n", "output": "282\r\n"}, {"input": "60 1947\r\n46 29 55 97 37 32 24 22 35 66 24 78 92 5 55 41 21 30 88 24 13 89 77 30 71 15 58 26 39 10 42 36 28 66 21 28 51 55 91 4 94 59 63 46 1 39 46 1 70 7 46 37 96 41 70 19 55 80 59 83\r\n", "output": "471\r\n"}, {"input": "70 2454\r\n88 23 5 86 53 48 60 78 97 90 0 18 57 78 68 28 87 39 70 9 0 35 18 53 67 56 0 71 7 86 39 96 83 45 99 92 43 38 40 63 81 59 89 86 28 62 53 97 53 2 73 93 38 49 51 62 93 3 63 49 47 85 72 98 43 91 7 20 47 66\r\n", "output": "632\r\n"}, {"input": "80 1879\r\n36 27 86 90 18 85 99 54 29 8 64 31 34 26 45 51 13 48 58 6 98 30 74 63 78 53 88 98 15 17 29 67 78 8 2 7 42 26 72 83 5 59 8 7 27 59 34 65 93 71 50 34 63 45 21 81 19 30 99 41 25 11 83 62 17 29 80 61 91 22 19 95 80 73 15 39 10 37 88 42\r\n", "output": "570\r\n"}, {"input": "90 1191\r\n46 37 76 11 60 29 49 13 88 41 65 7 2 13 44 58 23 10 45 48 63 83 79 5 89 99 28 80 34 6 37 92 61 70 51 0 34 67 68 77 62 69 27 86 71 83 72 73 93 92 62 68 86 76 28 24 67 66 61 12 3 52 45 44 58 83 0 84 18 50 75 51 41 25 21 53 39 20 36 45 62 24 12 33 61 81 9 13 27 22\r\n", "output": "554\r\n"}, {"input": "100 1257\r\n80 15 39 54 98 10 65 77 55 98 15 25 78 40 25 16 17 60 25 60 56 29 91 16 14 60 47 31 15 59 83 77 10 54 27 21 50 34 64 69 43 81 32 14 30 93 0 91 75 51 19 84 88 14 30 4 99 59 94 69 24 51 35 99 22 25 41 77 64 97 10 4 56 75 97 54 4 55 29 8 14 16 88 34 80 47 66 30 80 60 45 45 93 85 49 91 37 16 49 56\r\n", "output": "619\r\n"}, {"input": "100 3852\r\n71 34 1 77 97 36 66 78 95 47 47 15 50 100 43 47 20 23 61 92 49 86 29 92 100 85 5 58 59 19 16 81 16 89 93 75 46 86 9 50 9 49 61 88 76 13 14 99 47 64 39 42 63 5 57 8 51 21 21 62 92 84 84 56 9 37 72 19 99 19 8 60 25 21 4 0 98 80 29 63 52 87 91 30 79 79 96 22 32 63 87 73 51 89 81 84 69 30 55 31\r\n", "output": "922\r\n"}, {"input": "100 2533\r\n16 32 22 100 52 10 43 28 87 72 69 84 26 0 74 46 28 34 46 47 90 18 49 6 42 30 18 33 86 38 94 78 8 39 54 46 72 45 83 68 38 4 14 6 86 24 71 36 22 8 37 99 28 7 88 49 4 69 46 81 30 95 92 18 81 21 14 7 43 14 80 59 14 72 93 6 78 43 56 12 66 21 81 80 39 5 54 69 40 12 41 35 23 58 1 75 40 3 36 97\r\n", "output": "706\r\n"}, {"input": "100 2239\r\n95 9 31 56 96 85 88 79 78 63 68 95 1 91 94 56 57 88 30 92 64 52 91 11 17 99 65 63 35 68 82 18 66 57 26 62 32 70 89 98 42 17 68 93 53 79 50 6 30 76 69 10 4 41 18 56 81 49 14 10 91 6 32 80 85 94 2 95 66 9 18 58 71 23 23 48 68 72 39 51 0 23 71 73 10 89 13 15 16 30 27 44 63 93 22 77 12 12 28 5\r\n", "output": "737\r\n"}, {"input": "100 1689\r\n40 18 85 79 18 70 44 62 37 21 68 6 9 60 13 55 98 98 82 80 4 75 44 83 60 44 10 60 28 65 59 82 48 41 20 100 57 62 28 60 3 5 54 91 31 89 6 44 38 20 34 90 14 99 82 96 57 97 39 73 30 96 41 42 56 33 45 83 78 15 79 25 27 7 43 54 14 90 22 68 3 1 27 88 49 37 84 61 92 37 14 41 81 62 10 36 73 86 9 4\r\n", "output": "666\r\n"}, {"input": "1 44\r\n56\r\n", "output": "10\r\n"}, {"input": "5 136\r\n65 53 80 92 74\r\n", "output": "50\r\n"}, {"input": "20 964\r\n70 82 81 14 73 35 40 21 73 70 71 35 32 43 26 51 51 62 45 61\r\n", "output": "200\r\n"}, {"input": "80 4124\r\n14 37 6 11 63 59 43 72 88 0 53 43 42 95 65 61 9 69 9 95 49 64 27 34 53 31 34 26 30 48 85 97 35 60 74 45 35 86 11 34 45 72 95 95 95 13 58 2 0 38 37 13 61 47 85 77 96 10 34 3 54 55 91 23 57 13 33 16 2 17 80 61 36 57 79 81 90 33 82 48\r\n", "output": "800\r\n"}, {"input": "100 4899\r\n66 100 11 81 19 55 96 14 66 10 49 75 1 58 64 80 47 95 45 79 36 89 31 30 61 96 93 86 50 61 64 32 82 13 57 75 5 46 96 49 3 98 34 6 91 7 50 62 46 31 100 4 2 16 20 47 86 41 73 17 43 71 84 47 18 100 55 23 10 37 4 19 84 61 27 61 42 29 95 41 93 5 72 58 24 10 80 45 78 68 19 18 30 28 95 91 15 90 87 47\r\n", "output": "1000\r\n"}, {"input": "1 7035769\r\n1\r\n", "output": "10\r\n"}, {"input": "5 5012340\r\n10 63 89 25 29\r\n", "output": "50\r\n"}, {"input": "20 5527187\r\n15 91 34 37 16 77 85 4 31 28 2 47 8 45 57 51 58 72 97 16\r\n", "output": "200\r\n"}, {"input": "80 8000114\r\n27 46 16 80 85 11 20 22 80 24 85 22 17 86 96 60 16 12 94 39 23 86 12 49 28 78 80 23 92 78 62 38 27 43 35 62 60 89 85 63 39 27 70 13 73 91 82 73 98 83 70 93 5 37 15 85 39 58 92 34 93 44 31 86 28 86 43 3 25 12 18 61 25 7 67 87 37 29 65 98\r\n", "output": "800\r\n"}, {"input": "100 9455943\r\n44 8 21 71 7 29 40 65 91 70 48 19 77 48 16 22 54 4 29 34 9 22 73 34 47 41 5 83 32 91 52 6 74 64 18 23 9 4 36 78 98 20 20 3 69 86 41 67 54 76 87 84 47 6 52 87 61 100 98 80 14 14 24 99 90 73 97 79 22 65 65 51 29 44 15 67 21 58 79 80 96 40 63 73 96 59 72 24 87 85 74 49 81 30 16 61 87 30 0 13\r\n", "output": "1000\r\n"}, {"input": "1 6\r\n27\r\n", "output": "3\r\n"}, {"input": "1 10\r\n88\r\n", "output": "9\r\n"}, {"input": "1 3\r\n93\r\n", "output": "9\r\n"}, {"input": "2 22\r\n48 44\r\n", "output": "11\r\n"}, {"input": "1 48\r\n52\r\n", "output": "10\r\n"}, {"input": "1 2135072\r\n72\r\n", "output": "10\r\n"}, {"input": "3 5\r\n2 3 1\r\n", "output": "0\r\n"}, {"input": "5 1\r\n8 8 8 8 8\r\n", "output": "0\r\n"}, {"input": "3 150\r\n1 1 1\r\n", "output": "15\r\n"}, {"input": "4 3\r\n7 9 9 9\r\n", "output": "3\r\n"}, {"input": "1 10\r\n100\r\n", "output": "10\r\n"}, {"input": "3 11\r\n100 100 100\r\n", "output": "30\r\n"}, {"input": "2 11\r\n99 100\r\n", "output": "20\r\n"}]
false
stdio
null
true
798/B
798
B
Python 3
TESTS
3
30
0
203867335
def analiza(x): arr=[] mov=0 for i in x: for j in x: if i != j: mov+=comprob(i,j) arr.append(mov) mov=0 return min(arr) def comprob(x,y): m=0 x=list(x) y=list(y) p=list(y) while(True): if(x==y): return 0 else: p.append(p[0]) p.remove(p[0]) m+=1 if x==p: return m if p==y: return -1 if __name__ == '__main__': cant=int(input()) word=[] while(cant>0): word.append(input()) cant-=1 print(analiza(word))
99
62
5,529,600
26544102
import sys n = int(input()) S = [] for _ in range(n): s = input() S.append(s+s) k = len(S[0])//2 mn = 1e10 for i in range(k): s = S[0][i:i+k] sm = 0 for x in S: try: sm += x.index(s) except ValueError: print(-1) sys.exit(0) mn = min(mn, sm) print(mn)
Codeforces Round 410 (Div. 2)
CF
2,017
2
256
Mike and strings
Mike has n strings s1, s2, ..., sn each consisting of lowercase English letters. In one move he can choose a string si, erase the first character and append it to the end of the string. For example, if he has the string "coolmike", in one move he can transform it into the string "oolmikec". Now Mike asks himself: what is minimal number of moves that he needs to do in order to make all the strings equal?
The first line contains integer n (1 ≤ n ≤ 50) — the number of strings. This is followed by n lines which contain a string each. The i-th line corresponding to string si. Lengths of strings are equal. Lengths of each string is positive and don't exceed 50.
Print the minimal number of moves Mike needs in order to make all the strings equal or print - 1 if there is no solution.
null
In the first sample testcase the optimal scenario is to perform operations in such a way as to transform all strings into "zwoxz".
[{"input": "4\nxzzwo\nzwoxz\nzzwox\nxzzwo", "output": "5"}, {"input": "2\nmolzv\nlzvmo", "output": "2"}, {"input": "3\nkc\nkc\nkc", "output": "0"}, {"input": "3\naa\naa\nab", "output": "-1"}]
1,300
["brute force", "dp", "strings"]
99
[{"input": "4\r\nxzzwo\r\nzwoxz\r\nzzwox\r\nxzzwo\r\n", "output": "5\r\n"}, {"input": "2\r\nmolzv\r\nlzvmo\r\n", "output": "2\r\n"}, {"input": "3\r\nkc\r\nkc\r\nkc\r\n", "output": "0\r\n"}, {"input": "3\r\naa\r\naa\r\nab\r\n", "output": "-1\r\n"}, {"input": "3\r\nkwkb\r\nkbkw\r\nbkwk\r\n", "output": "3\r\n"}, {"input": "1\r\na\r\n", "output": "0\r\n"}, {"input": "2\r\nnjtazaab\r\nabnjtaza\r\n", "output": "2\r\n"}, {"input": "38\r\nkmlzdcnm\r\nmlzdcnmk\r\nlzdcnmkm\r\nkmlzdcnm\r\nlzdcnmkm\r\nzdcnmkml\r\nzdcnmkml\r\nmlzdcnmk\r\nzdcnmkml\r\nmlzdcnmk\r\nlzdcnmkm\r\nzdcnmkml\r\nkmlzdcnm\r\nlzdcnmkm\r\nzdcnmkml\r\nmlzdcnmk\r\nkmlzdcnm\r\nmkmlzdcn\r\nlzdcnmkm\r\nnmkmlzdc\r\nzdcnmkml\r\nnmkmlzdc\r\nkmlzdcnm\r\nmlzdcnmk\r\nmkmlzdcn\r\ndcnmkmlz\r\ncnmkmlzd\r\ncnmkmlzd\r\nmkmlzdcn\r\ncnmkmlzd\r\ndcnmkmlz\r\nkmlzdcnm\r\nnmkmlzdc\r\nnmkmlzdc\r\nkmlzdcnm\r\nkmlzdcnm\r\nlzdcnmkm\r\nzdcnmkml\r\n", "output": "104\r\n"}, {"input": "4\r\nxwppaubrphxjwmwfwypvwwjzotyobpiynyka\r\nubrphxjwmwfwypvwwjzotyobpiynykaxwppa\r\nwjzotyobpiynykaxwppaubrphxjwmwfwypvw\r\ntyobpiynykaxwppaubrphxjwmwfwypvwwjzo\r\n", "output": "41\r\n"}, {"input": "15\r\ngnizfqwqmimtgmtf\r\nmtgmtfgnizfqwqmi\r\ngmtfgnizfqwqmimt\r\nzfqwqmimtgmtfgni\r\nzfqwqmimtgmtfgni\r\nfqwqmimtgmtfgniz\r\nimtgmtfgnizfqwqm\r\nfgnizfqwqmimtgmt\r\ngmtfgnizfqwqmimt\r\nmtgmtfgnizfqwqmi\r\nqwqmimtgmtfgnizf\r\nizfqwqmimtgmtfgn\r\nmtfgnizfqwqmimtg\r\ntgmtfgnizfqwqmim\r\nmtfgnizfqwqmimtg\r\n", "output": "89\r\n"}, {"input": "33\r\nnkgcmrfvxe\r\nvxenkgcmrf\r\nrfvxenkgcm\r\nvxenkgcmrf\r\nxenkgcmrfv\r\nenkgcmrfvx\r\nenkgcmrfvx\r\nnkgcmrfvxe\r\nkgcmrfvxen\r\ncmrfvxenkg\r\ncmrfvxenkg\r\nxenkgcmrfv\r\nrfvxenkgcm\r\nrfvxenkgcm\r\nnkgcmrfvxe\r\nxenkgcmrfv\r\nrfvxenkgcm\r\nxenkgcmrfv\r\nxenkgcmrfv\r\ngcmrfvxenk\r\nmrfvxenkgc\r\nfvxenkgcmr\r\nvxenkgcmrf\r\nenkgcmrfvx\r\ncmrfvxenkg\r\ncmrfvxenkg\r\nmrfvxenkgc\r\nkgcmrfvxen\r\nvxenkgcmrf\r\nenkgcmrfvx\r\ncmrfvxenkg\r\ncmrfvxenkg\r\ngcmrfvxenk\r\n", "output": "135\r\n"}, {"input": "11\r\nxdngtxuqjalamqvotuhx\r\notuhxxdngtxuqjalamqv\r\ngtxuqjalamqvotuhxxdn\r\ndngtxuqjalamqvotuhxx\r\nvotuhxxdngtxuqjalamq\r\nxxdngtxuqjalamqvotuh\r\nalamqvotuhxxdngtxuqj\r\nuqjalamqvotuhxxdngtx\r\nqjalamqvotuhxxdngtxu\r\nhxxdngtxuqjalamqvotu\r\njalamqvotuhxxdngtxuq\r\n", "output": "79\r\n"}, {"input": "2\r\noiadfnwpdcxxhbwwqbrcdujcusgtkqdjmintwjlb\r\nbrcdujcusgtkqdjmintwjlboiadfnwpdcxxhbwwq\r\n", "output": "17\r\n"}, {"input": "20\r\ncynedh\r\nnedhcy\r\nhcyned\r\ncynedh\r\nynedhc\r\nynedhc\r\nnedhcy\r\nnedhcy\r\nnedhcy\r\nhcyned\r\nnedhcy\r\nhcyned\r\nnedhcy\r\ndhcyne\r\nynedhc\r\nedhcyn\r\ndhcyne\r\nynedhc\r\ncynedh\r\ncynedh\r\n", "output": "34\r\n"}, {"input": "9\r\nrgycrkgcjktfdjkffcnlnhiawq\r\nawqrgycrkgcjktfdjkffcnlnhi\r\nrkgcjktfdjkffcnlnhiawqrgyc\r\njktfdjkffcnlnhiawqrgycrkgc\r\ncjktfdjkffcnlnhiawqrgycrkg\r\nfdjkffcnlnhiawqrgycrkgcjkt\r\nffcnlnhiawqrgycrkgcjktfdjk\r\nktfdjkffcnlnhiawqrgycrkgcj\r\nwqrgycrkgcjktfdjkffcnlnhia\r\n", "output": "76\r\n"}, {"input": "2\r\ndzlisvouhbqogzusikmkuvkql\r\nqogzusikmkuvkqldzlisvouhb\r\n", "output": "10\r\n"}, {"input": "2\r\nsfotivvfgbdfcnvaybxhstavaoktatflelpyi\r\nsfotivvfgbdfcnvaybxhstavaoktatflelpyi\r\n", "output": "0\r\n"}, {"input": "1\r\numwnrjtcytnquvdmqfiqt\r\n", "output": "0\r\n"}, {"input": "4\r\nzumixjfqhbkeg\r\nkegzumixjfqhb\r\nhbkegzumixjfq\r\ngzumixjfqhbke\r\n", "output": "9\r\n"}, {"input": "12\r\nktwwduoopsnkhfklrskdxakbmqhl\r\nlktwwduoopsnkhfklrskdxakbmqh\r\nduoopsnkhfklrskdxakbmqhlktww\r\nklrskdxakbmqhlktwwduoopsnkhf\r\noopsnkhfklrskdxakbmqhlktwwdu\r\nopsnkhfklrskdxakbmqhlktwwduo\r\nkbmqhlktwwduoopsnkhfklrskdxa\r\nlrskdxakbmqhlktwwduoopsnkhfk\r\nwduoopsnkhfklrskdxakbmqhlktw\r\nklrskdxakbmqhlktwwduoopsnkhf\r\nhfklrskdxakbmqhlktwwduoopsnk\r\ndxakbmqhlktwwduoopsnkhfklrsk\r\n", "output": "121\r\n"}, {"input": "12\r\naaaaaaaaaaaaaaaaaaaaaaaaaaa\r\naaaaaaaaaaaaaaaaaaaaaaaaaaa\r\naaaaaaaaaaaaaaaaaaaaaaaaaaa\r\naaaaaaaaaaaaaaaaaaaaaaaaaaa\r\naaaaaaaaaaaaaaaaaaaaaaaaaaa\r\naaaaaaaaaaaaaaaaaaaaaaaaaaa\r\naaaaaaaaaaaaaaaaaaaaaaaaaaa\r\naaaaaaaaaaaaaaaaaaaaaaaaaaa\r\naaaaaaaaaaaaaaaaaaaaaaaaaaa\r\naaaaaaaaaaaaaaaaaaaaaaaaaaa\r\naaaaaaaaaaaaaaaaaaaaaaaaaaa\r\naaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n", "output": "0\r\n"}, {"input": "15\r\nkknrrejishjz\r\nhilbaftsfcaq\r\nlncsgtjqgwjz\r\nathvctulbhmb\r\nnfvsjyiulmmr\r\nhxjnvumwnwtr\r\nrncsxqvkvqeg\r\nqoabapuhodxk\r\nylinhbhyqjsn\r\ncnzxgdgytgav\r\nxufmacyangpv\r\nhwvzionkdmjl\r\nspklymjxiolk\r\nqjkfrccaayak\r\nonwrbgfvxrjx\r\n", "output": "-1\r\n"}, {"input": "2\r\nadam\r\nmdaa\r\n", "output": "-1\r\n"}, {"input": "2\r\naabc\r\nacab\r\n", "output": "-1\r\n"}, {"input": "2\r\nabc\r\ncba\r\n", "output": "-1\r\n"}, {"input": "5\r\naaaa\r\naaaa\r\naaaa\r\naaaa\r\naaaa\r\n", "output": "0\r\n"}, {"input": "2\r\na\r\nb\r\n", "output": "-1\r\n"}, {"input": "2\r\nabab\r\naabb\r\n", "output": "-1\r\n"}, {"input": "2\r\nbac\r\nabc\r\n", "output": "-1\r\n"}, {"input": "2\r\naabb\r\nabab\r\n", "output": "-1\r\n"}, {"input": "3\r\naa\r\naa\r\naa\r\n", "output": "0\r\n"}, {"input": "2\r\nabc\r\nacb\r\n", "output": "-1\r\n"}, {"input": "3\r\naaaa\r\naaaa\r\naaaa\r\n", "output": "0\r\n"}, {"input": "2\r\naa\r\naa\r\n", "output": "0\r\n"}, {"input": "2\r\nab\r\naa\r\n", "output": "-1\r\n"}, {"input": "2\r\nxyxy\r\nxxyy\r\n", "output": "-1\r\n"}, {"input": "2\r\nabc\r\nbac\r\n", "output": "-1\r\n"}, {"input": "2\r\naaabb\r\nbaaba\r\n", "output": "-1\r\n"}, {"input": "2\r\nabcde\r\ndcabe\r\n", "output": "-1\r\n"}, {"input": "2\r\nabcd\r\nbdca\r\n", "output": "-1\r\n"}, {"input": "5\r\naaaaa\r\naabaa\r\naaaaa\r\naaaaa\r\naaaaa\r\n", "output": "-1\r\n"}, {"input": "3\r\naab\r\nabb\r\nbab\r\n", "output": "-1\r\n"}, {"input": "2\r\nnzxv\r\nzvnx\r\n", "output": "-1\r\n"}, {"input": "10\r\nab\r\nbc\r\ncd\r\nde\r\nef\r\ngh\r\nhi\r\nij\r\nik\r\nmn\r\n", "output": "-1\r\n"}, {"input": "3\r\naaa\r\naaa\r\naaa\r\n", "output": "0\r\n"}, {"input": "2\r\nac\r\nbb\r\n", "output": "-1\r\n"}, {"input": "2\r\nabcd\r\nbdac\r\n", "output": "-1\r\n"}, {"input": "2\r\nabcabc\r\ncabcab\r\n", "output": "1\r\n"}, {"input": "7\r\naaa\r\naab\r\naba\r\nabb\r\nbaa\r\nbab\r\nbba\r\n", "output": "-1\r\n"}, {"input": "4\r\naa\r\naa\r\nbb\r\nbb\r\n", "output": "-1\r\n"}, {"input": "2\r\nabcd\r\ncabd\r\n", "output": "-1\r\n"}, {"input": "4\r\nabcabcabc\r\nbcabcabca\r\ncabcabcab\r\ncabcabcab\r\n", "output": "3\r\n"}, {"input": "3\r\nabcabc\r\nbcabca\r\nbcabca\r\n", "output": "1\r\n"}, {"input": "2\r\nabbc\r\nabcc\r\n", "output": "-1\r\n"}, {"input": "2\r\naaabb\r\nababa\r\n", "output": "-1\r\n"}, {"input": "3\r\naabbbaba\r\nabaabbab\r\nbbbaaaba\r\n", "output": "-1\r\n"}, {"input": "2\r\naabaab\r\nbaabaa\r\n", "output": "1\r\n"}]
false
stdio
null
true
812/B
812
B
Python 3
TESTS
42
233
307,200
27529899
# 1000 n_m = [int(x) for x in input().split()] n_m[1] += 1 floors = [] rooms = [] position = [0, 0] def judge (a,b,rooms): #for a temp=rooms[:] temp_room=temp.pop(a) temp_cost=do(temp,temp_room) # for b temp1 = rooms[:] temp1_room = temp1.pop(b) temp1_cost = do(temp1,temp1_room) if temp_cost<temp1_cost: return a else: return b def do(rooms,position): time=0 for i in range(len(rooms[:])): least_ind = path_cost(position, rooms) position = rooms.pop(least_ind[1]) time += least_ind[0] return(time) def costs(position,rooms,i): if position[1] - rooms[i][1] == 0: cost = abs(rooms[i][0] - position[0]) else: temp_cost = position[0] new_position = [0, position[1]] temp_cost += (abs(rooms[i][0] - new_position[0]) + abs(rooms[i][1] - new_position[1])) temp_cost1 = abs(n_m[1] - position[0]) new_position1 = [n_m[1], position[1]] temp_cost1 += (abs(rooms[i][0] - new_position1[0]) + abs(rooms[i][1] - new_position1[1])) if temp_cost > temp_cost1: cost = temp_cost1 else: cost = temp_cost return cost def path_cost(position,rooms): MIN=0 index = 0 y = rooms[0][1] for i in range(len(rooms)): if rooms[i][1]>y: continue cost=costs(position,rooms,i) if MIN==0: MIN = cost+1 if (cost < MIN): MIN = cost index = i y = rooms[i][1] elif cost == MIN: index=judge(i,index,rooms) MIN=costs(position,rooms,index) y = rooms[index][1] return [MIN,index] for i in range(n_m[0]): floors.append([bool(int(x)) for x in input()]) floors.reverse() for i in range(n_m[0]): for x in range(n_m[1]+1): if floors[i][x]: rooms.append([x, i]) del floors print(do(rooms,position))
72
62
512,000
27522047
from collections import Counter import sys def left_time(floor): light_counter = Counter(floor) # print(light_counter) # print(light_counter['1']) if light_counter['1'] == 0: return 0 else: last_light_search = 1 while last_light_search <= len(floor): # print(last_light_search) if floor[-last_light_search] == '1': return((len(floor) - last_light_search) * 2) break else: last_light_search += 1 def right_time(floor): light_counter = Counter(floor) if light_counter['1'] == 0: return 0 else: last_light_search = 0 while True: if floor[last_light_search] == '1': return ((len(floor) - last_light_search - 1) * 2) break else: last_light_search += 1 floors_nr, rooms_nr = (int(x) for x in input().split()) building_scheme = [] worth_counting_flag = False for i in range(floors_nr): floor_i = input() c = Counter(floor_i) if not worth_counting_flag and c['1'] > 0: worth_counting_flag = True if worth_counting_flag: building_scheme.append(floor_i) if len(building_scheme) == 0: print(0) sys.exit() optimal_time = [(-1, 10000)] last_floor = building_scheme[0] building_scheme = building_scheme[1:] for floor in building_scheme[::-1]: prefix_left, prefix_right = optimal_time[-1] keep_right_left = rooms_nr + 1 + prefix_left + 1 keep_right_right = right_time(floor) + prefix_right + 1 keep_right = min(keep_right_left, keep_right_right) keep_left_left = left_time(floor) + prefix_left + 1 keep_left_right = rooms_nr + 1 + prefix_right + 1 keep_left = min(keep_left_left, keep_left_right) optimal_time.append((keep_left, keep_right)) # print(optimal_time) last_floor_time = min(optimal_time[-1][0] + 1 + left_time(last_floor) // 2, optimal_time[-1][1] + 1 + right_time(last_floor) // 2) print(last_floor_time)
Codeforces Round 417 (Div. 2)
CF
2,017
1
256
Sagheer, the Hausmeister
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off. The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms. Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights. Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
The first line contains two integers n and m (1 ≤ n ≤ 15 and 1 ≤ m ≤ 100) — the number of floors and the number of rooms in each floor, respectively. The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor. The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
Print a single integer — the minimum total time needed to turn off all the lights.
null
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs. In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor. In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor.
[{"input": "2 2\n0010\n0100", "output": "5"}, {"input": "3 4\n001000\n000010\n000010", "output": "12"}, {"input": "4 3\n01110\n01110\n01110\n01110", "output": "18"}]
1,600
["bitmasks", "brute force", "dp"]
72
[{"input": "2 2\r\n0010\r\n0100\r\n", "output": "5\r\n"}, {"input": "3 4\r\n001000\r\n000010\r\n000010\r\n", "output": "12\r\n"}, {"input": "4 3\r\n01110\r\n01110\r\n01110\r\n01110\r\n", "output": "18\r\n"}, {"input": "3 2\r\n0000\r\n0100\r\n0100\r\n", "output": "4\r\n"}, {"input": "1 89\r\n0000000000000000000000000000000100000000000000010000000000010000000000000000000000000000000\r\n", "output": "59\r\n"}, {"input": "2 73\r\n000000000000000000000000000000000000000000000000000000000000000000000000000\r\n000000000000000000000000000000000000000100000010000000000000000000000000000\r\n", "output": "46\r\n"}, {"input": "3 61\r\n000000000000000000000000000000000000000000000000000000000000000\r\n000000000000000000000000000000000000000000000000000000000000000\r\n000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "0\r\n"}, {"input": "4 53\r\n0000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000\r\n", "output": "0\r\n"}, {"input": "5 93\r\n00000000000000000000000000000000000000000000000000000000100000000000000000000000000000000001010\r\n00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n00000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n00000000000000000000000000000010000000000000000000100000000000000000000000000000000000000000000\r\n00000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "265\r\n"}, {"input": "6 77\r\n0000000000000000100000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000010000000000000\r\n0000000000010000000000000000000000000000000000000000000000000000000000000000010\r\n0000000000000000000001000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000100000000000000000000000000000\r\n", "output": "311\r\n"}, {"input": "7 65\r\n0000000001000000000000000010000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000001000001000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "62\r\n"}, {"input": "8 57\r\n00000000100000000000000000000000000000000000000000000000000\r\n00000000000000010000000000000000000000000000000000000000000\r\n00000000000000000000000000000000000100000000000000000000000\r\n00000000000000000000000000000000000000000000000000000000000\r\n00000000000000000000000000000000000100000000000000000000000\r\n00000000000000000000000000000000000000000000000000000000000\r\n00000000000010000000000000000000000000000000000000000000000\r\n00000000000000000000000000000000000000000000000001000000000\r\n", "output": "277\r\n"}, {"input": "12 13\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n010000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000010000000000\r\n000000000000000\r\n", "output": "14\r\n"}, {"input": "13 1\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n", "output": "0\r\n"}, {"input": "1 33\r\n00000100101110001101000000110100010\r\n", "output": "33\r\n"}, {"input": "2 21\r\n00100110100010010010010\r\n01000001111001010000000\r\n", "output": "43\r\n"}, {"input": "3 5\r\n0001010\r\n0100000\r\n0100000\r\n", "output": "11\r\n"}, {"input": "4 45\r\n00010000101101100000101101000000100000001101100\r\n01110000100111010011000000100000000001000001100\r\n00000000001000100110100001000010011010001010010\r\n01111110100100000101101010011000100100001000000\r\n", "output": "184\r\n"}, {"input": "5 37\r\n010100000000000000000110000110010000010\r\n001101100010110011101000001010101101110\r\n010000001000100010010100000000001010000\r\n000000000100101000000101100001000001110\r\n000010000000000000100001001000011100110\r\n", "output": "193\r\n"}, {"input": "6 25\r\n011001000100111010000101000\r\n000000000010000010001000010\r\n011001100001100001001001010\r\n000000100000010000000000110\r\n010001100001000001000000010\r\n011000001001010111110000100\r\n", "output": "160\r\n"}, {"input": "7 61\r\n010000111100010100001000011010100001000000000011100000100010000\r\n000010011000001000000100110101010001000000010001100000100100100\r\n000010001000001000000100001000000100100011001110000111000000100\r\n000000000101000011010000011000000101000001011001000011101010010\r\n000010010011000000100000110000001000000101000000101000010000010\r\n000010010101101100100100100011001011101010000101000010000101010\r\n000100001100001001000000001000000001011000110010100000000010110\r\n", "output": "436\r\n"}, {"input": "8 49\r\n000100100000000111110010011100110100010010000011000\r\n001000000101111000000001111100010010100000010000000\r\n000000010000011100001000000000101000110010000100100\r\n000000000001000110000011101101000000100000101010000\r\n000000110001000101101000000001000000110001000110000\r\n000100000000000000100100010011000001111101010100110\r\n000000001000000010101111000100001100000000010111000\r\n001000010000110000011100000000100110000010001000000\r\n", "output": "404\r\n"}, {"input": "9 41\r\n0011000000000101001101001000000001110000010\r\n0000110000001010110010110010110010010001000\r\n0001100010100000000001110100100001101000100\r\n0001010101111010000000010010001001011111000\r\n0101000101000011101011000000001100110010000\r\n0001010000000000000001011000000100010101000\r\n0000010011000000001000110001000010110001000\r\n0000100010000110100001000000100010001111100\r\n0000001110100001000001000110001110000100000\r\n", "output": "385\r\n"}, {"input": "10 29\r\n0000000000101001100001001011000\r\n0001110100000000000000100010000\r\n0010001001000011000100010001000\r\n0001000010101000000010100010100\r\n0111000000000000100100100010100\r\n0001000100011111000100010100000\r\n0000000000000001000001001011000\r\n0000101110000001010001011001110\r\n0000001000101010011000001100100\r\n0100010000101011010000000000000\r\n", "output": "299\r\n"}, {"input": "1 57\r\n00011101100001110001111000000100101111000111101100111001000\r\n", "output": "55\r\n"}, {"input": "2 32\r\n0011110111011011011101111101011110\r\n0111000110111111011110011101011110\r\n", "output": "65\r\n"}, {"input": "3 20\r\n0110011111110101101100\r\n0111110000111010100100\r\n0110111110010100011110\r\n", "output": "63\r\n"}, {"input": "4 4\r\n011100\r\n001010\r\n010000\r\n011110\r\n", "output": "22\r\n"}, {"input": "5 44\r\n0001010010001111111001111111000010100100000010\r\n0001111001111001101111011111010110001001111110\r\n0111111010111111011101100011101010100101110110\r\n0011010011101011101111001001010110000111111100\r\n0110100111011100110101110010010011011101100100\r\n", "output": "228\r\n"}, {"input": "6 36\r\n01110101111111110101011000011111110010\r\n00011101100010110111111111110001100100\r\n00001111110010111111101110101110111110\r\n00110110011100100111011110000000000010\r\n01100101101001010001011111100111101100\r\n00011111111011001000011001011110011110\r\n", "output": "226\r\n"}, {"input": "7 24\r\n01111001111001011010010100\r\n00111011010101000111101000\r\n01001110110010010110011110\r\n00000101111011011111111000\r\n01111111101111001001010010\r\n01110000111101011111111010\r\n00000100011100110000110000\r\n", "output": "179\r\n"}, {"input": "8 8\r\n0011101110\r\n0110010100\r\n0100111110\r\n0111111100\r\n0011010100\r\n0001101110\r\n0111100000\r\n0110111000\r\n", "output": "77\r\n"}, {"input": "9 48\r\n00011010111110111011111001111111111101001111110010\r\n01000101000101101101111110111101011100001011010010\r\n00110111110110101110101110111111011011101111011000\r\n00110111111100010110110110111001001111011010101110\r\n01111111100101010011111100100111110011001101110100\r\n01111011110011111101010101010100001110111111111000\r\n01110101101101110001000010110100010110101111111100\r\n00111101001010110010110100000111110101010100001000\r\n00011011010110011111001100111100100011100110110100\r\n", "output": "448\r\n"}, {"input": "10 40\r\n010011001001111011011011101111010001010010\r\n011000000110000010001011111010100000110000\r\n011010101001110010110110011111010101101000\r\n000111111010101111000110011111011011011010\r\n010110101110001001001111111000110011101010\r\n010011010100111110010100100111100111011110\r\n001111101100111111111111001010111010000110\r\n001111110010101100110100101110001011100110\r\n010111010010001111110101111111111110111000\r\n011101101111000100111111111001111100111010\r\n", "output": "418\r\n"}, {"input": "11 28\r\n011100111101101001011111001110\r\n010001111110011101101011001000\r\n001010011011011010101101101100\r\n001100011001101011011001110100\r\n010111110011101110000110111100\r\n010010001111110000011111010100\r\n001011111111110011101101111010\r\n001101101011100100011011001110\r\n001111110110100110101011000010\r\n000101101011100001101101100100\r\n010011101101111011100111110100\r\n", "output": "328\r\n"}, {"input": "1 68\r\n0101111110111111111111111111110111111111111111111110111111101111111110\r\n", "output": "68\r\n"}, {"input": "2 56\r\n0011111111111110111111111111111111011111111111011111011110\r\n0111111111010111111111110111111111111110111111010111111110\r\n", "output": "113\r\n"}, {"input": "3 17\r\n0111111101111111110\r\n0111111111101011110\r\n0101111111111111110\r\n", "output": "55\r\n"}, {"input": "4 4\r\n011110\r\n010110\r\n010110\r\n011110\r\n", "output": "22\r\n"}, {"input": "5 89\r\n0011111111111101110110111111111101111011111011101110111111111111111111111111111111111111110\r\n0111111111111111111111111101111111111111111111111111111111111111111111111111111111111111110\r\n0111111111111011111111111111111111101111011111111111111111110110111101111111111111111011010\r\n0111111111111111011011111111111011111111111111111111111111111111111111111111111110111111010\r\n0111111101111011111110101011111111110111100100101111111011111111111111011011101111111111110\r\n", "output": "453\r\n"}, {"input": "6 77\r\n0111111110101011111111111111111111111111111111111111100111111111101111111111110\r\n0111111111111111111101111101111111111011111111011111111001011111111111101111110\r\n0111101111111111111111111111111111111110110011111111111011111111101111111111110\r\n0111110111111111111111111111111111111111111111111111011011111111111111111111110\r\n0101111110111111111111111111111111111111111011111111111111111111101111011011110\r\n0110111111101111110111111111111011111111101011111101111111111111111111110111100\r\n", "output": "472\r\n"}, {"input": "7 20\r\n0111111111111111111100\r\n0111110111111111111110\r\n0111111111111111111100\r\n0111111011111111111110\r\n0111111111111011101110\r\n0111101011110111111010\r\n0111111111111111111010\r\n", "output": "151\r\n"}, {"input": "8 8\r\n0111111110\r\n0111101110\r\n0111111110\r\n0111111110\r\n0111111110\r\n0110111100\r\n0101111110\r\n0110111110\r\n", "output": "78\r\n"}, {"input": "11 24\r\n01111111111101111111111110\r\n01111111111111111111111110\r\n01110111111111111111111110\r\n01111111111111111111011110\r\n01111111111111111110111110\r\n01111010111111100111101110\r\n01111111111111010101111100\r\n01111111111111110111111110\r\n01011101111111111101111110\r\n00111111011111111110111110\r\n01111111101111111101111110\r\n", "output": "284\r\n"}, {"input": "12 12\r\n01111111111000\r\n01101111110110\r\n01111110111110\r\n01111111111110\r\n01111111111010\r\n01011111110110\r\n01111111111110\r\n01101101011110\r\n01111111111110\r\n01111101011110\r\n00111111111110\r\n01111111011110\r\n", "output": "166\r\n"}, {"input": "15 28\r\n011111111101011111111101111110\r\n011111111111111111111111111110\r\n011101110111011011101111011110\r\n011111111011111011110111111110\r\n011111111110101111111111111110\r\n011111011111110011111111011010\r\n011110111111001101111111111110\r\n011111111110111111111011111110\r\n011111111111111111111111011110\r\n011111011111111111111011001010\r\n011111111101111111111101111110\r\n011111111110111111101111011110\r\n010111111111101111111111111110\r\n011111111111111111011111111110\r\n011011111111111110110111110110\r\n", "output": "448\r\n"}, {"input": "2 11\r\n0100000000000\r\n0000000010000\r\n", "output": "18\r\n"}, {"input": "1 100\r\n010010010011100001101101110111101010000101010001111001001101011110000011101110101000100111111001101110\r\n", "output": "100\r\n"}, {"input": "15 1\r\n010\r\n010\r\n010\r\n010\r\n010\r\n010\r\n000\r\n000\r\n000\r\n010\r\n000\r\n010\r\n000\r\n000\r\n000\r\n", "output": "29\r\n"}, {"input": "3 3\r\n00010\r\n00000\r\n00010\r\n", "output": "7\r\n"}]
false
stdio
null
true
812/B
812
B
Python 3
TESTS
42
234
307,200
27531582
# 1000 n_m = [int(x) for x in input().split()] n_m[1] += 1 floors = [] rooms = [] position = [0, 0] def judge(a, b, rooms): # for a temp = rooms[:] temp_room = temp.pop(a) temp_cost = do(temp, temp_room) # for b temp1 = rooms[:] temp1_room = temp1.pop(b) temp1_cost = do(temp1, temp1_room) if temp_cost < temp1_cost: return a else: return b def do(rooms, position): time = 0 for i in range(len(rooms[:])): least_ind = path_cost(position, rooms) position = rooms.pop(least_ind[1]) time += least_ind[0] return (time) def costs(position, rooms, i): if position[1] - rooms[i][1] == 0: cost = abs(rooms[i][0] - position[0]) else: temp_cost = position[0] new_position = [0, position[1]] temp_cost += (abs(rooms[i][0] - new_position[0]) + abs(rooms[i][1] - new_position[1])) temp_cost1 = abs(n_m[1] - position[0]) new_position1 = [n_m[1], position[1]] temp_cost1 += (abs(rooms[i][0] - new_position1[0]) + abs(rooms[i][1] - new_position1[1])) if abs(temp_cost-temp_cost1)==1: if do(rooms[i+1:],new_position)<do(rooms[i+1:],new_position1): cost=temp_cost else: cost=temp_cost1 else: cost = min(temp_cost,temp_cost1) return cost def path_cost(position, rooms): MIN = 0 index = 0 y = rooms[0][1] for i in range(len(rooms)): if rooms[i][1] > y: continue cost = costs(position, rooms, i) if MIN == 0: MIN = cost + 1 if (cost < MIN): MIN = cost index = i y = rooms[i][1] elif cost == MIN: index = judge(i, index, rooms) MIN = costs(position, rooms, index) y = rooms[index][1] return [MIN, index] for i in range(n_m[0]): floors.append([bool(int(x)) for x in input()]) floors.reverse() for i in range(n_m[0]): for x in range(n_m[1] + 1): if floors[i][x]: rooms.append([x, i]) del floors print(do(rooms, position))
72
62
1,536,000
209607846
import sys input = lambda: sys.stdin.readline().rstrip() N,M = map(int, input().split()) S = [] for _ in range(N): S.append(input()) S = S[::-1] while S and not '1' in S[-1]: S.pop() if not S: exit(print(0)) N = len(S) INF=float("inf") dp = [[INF,INF] for _ in range(N+1)] dp[0][0] = -1 for i in range(N): if '1' not in S[i]: dp[i+1][0] = dp[i][0]+1 dp[i+1][1] = dp[i][1]+1 continue last,first=0,M+1 for j in range(1,M+1): if S[i][j]=='1': last = max(last, j) first = min(first, j) if i+1<N: dp[i+1][0] = min(dp[i][0]+1+last*2, dp[i][1]+M+2) dp[i+1][1] = min(dp[i][1]+1+(M+1-first)*2, dp[i][0]+M+2) else: dp[i+1][0] = min(dp[i][0]+1+last, dp[i][1]+1+(M+1-first)) dp[i+1][1] = min(dp[i][1]+1+(M+1-first), dp[i][0]+1+last) print(min(dp[-1]))
Codeforces Round 417 (Div. 2)
CF
2,017
1
256
Sagheer, the Hausmeister
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off. The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms. Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights. Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
The first line contains two integers n and m (1 ≤ n ≤ 15 and 1 ≤ m ≤ 100) — the number of floors and the number of rooms in each floor, respectively. The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor. The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
Print a single integer — the minimum total time needed to turn off all the lights.
null
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs. In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor. In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor.
[{"input": "2 2\n0010\n0100", "output": "5"}, {"input": "3 4\n001000\n000010\n000010", "output": "12"}, {"input": "4 3\n01110\n01110\n01110\n01110", "output": "18"}]
1,600
["bitmasks", "brute force", "dp"]
72
[{"input": "2 2\r\n0010\r\n0100\r\n", "output": "5\r\n"}, {"input": "3 4\r\n001000\r\n000010\r\n000010\r\n", "output": "12\r\n"}, {"input": "4 3\r\n01110\r\n01110\r\n01110\r\n01110\r\n", "output": "18\r\n"}, {"input": "3 2\r\n0000\r\n0100\r\n0100\r\n", "output": "4\r\n"}, {"input": "1 89\r\n0000000000000000000000000000000100000000000000010000000000010000000000000000000000000000000\r\n", "output": "59\r\n"}, {"input": "2 73\r\n000000000000000000000000000000000000000000000000000000000000000000000000000\r\n000000000000000000000000000000000000000100000010000000000000000000000000000\r\n", "output": "46\r\n"}, {"input": "3 61\r\n000000000000000000000000000000000000000000000000000000000000000\r\n000000000000000000000000000000000000000000000000000000000000000\r\n000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "0\r\n"}, {"input": "4 53\r\n0000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000\r\n", "output": "0\r\n"}, {"input": "5 93\r\n00000000000000000000000000000000000000000000000000000000100000000000000000000000000000000001010\r\n00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n00000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n00000000000000000000000000000010000000000000000000100000000000000000000000000000000000000000000\r\n00000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "265\r\n"}, {"input": "6 77\r\n0000000000000000100000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000010000000000000\r\n0000000000010000000000000000000000000000000000000000000000000000000000000000010\r\n0000000000000000000001000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000100000000000000000000000000000\r\n", "output": "311\r\n"}, {"input": "7 65\r\n0000000001000000000000000010000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000001000001000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "62\r\n"}, {"input": "8 57\r\n00000000100000000000000000000000000000000000000000000000000\r\n00000000000000010000000000000000000000000000000000000000000\r\n00000000000000000000000000000000000100000000000000000000000\r\n00000000000000000000000000000000000000000000000000000000000\r\n00000000000000000000000000000000000100000000000000000000000\r\n00000000000000000000000000000000000000000000000000000000000\r\n00000000000010000000000000000000000000000000000000000000000\r\n00000000000000000000000000000000000000000000000001000000000\r\n", "output": "277\r\n"}, {"input": "12 13\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n010000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000010000000000\r\n000000000000000\r\n", "output": "14\r\n"}, {"input": "13 1\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n", "output": "0\r\n"}, {"input": "1 33\r\n00000100101110001101000000110100010\r\n", "output": "33\r\n"}, {"input": "2 21\r\n00100110100010010010010\r\n01000001111001010000000\r\n", "output": "43\r\n"}, {"input": "3 5\r\n0001010\r\n0100000\r\n0100000\r\n", "output": "11\r\n"}, {"input": "4 45\r\n00010000101101100000101101000000100000001101100\r\n01110000100111010011000000100000000001000001100\r\n00000000001000100110100001000010011010001010010\r\n01111110100100000101101010011000100100001000000\r\n", "output": "184\r\n"}, {"input": "5 37\r\n010100000000000000000110000110010000010\r\n001101100010110011101000001010101101110\r\n010000001000100010010100000000001010000\r\n000000000100101000000101100001000001110\r\n000010000000000000100001001000011100110\r\n", "output": "193\r\n"}, {"input": "6 25\r\n011001000100111010000101000\r\n000000000010000010001000010\r\n011001100001100001001001010\r\n000000100000010000000000110\r\n010001100001000001000000010\r\n011000001001010111110000100\r\n", "output": "160\r\n"}, {"input": "7 61\r\n010000111100010100001000011010100001000000000011100000100010000\r\n000010011000001000000100110101010001000000010001100000100100100\r\n000010001000001000000100001000000100100011001110000111000000100\r\n000000000101000011010000011000000101000001011001000011101010010\r\n000010010011000000100000110000001000000101000000101000010000010\r\n000010010101101100100100100011001011101010000101000010000101010\r\n000100001100001001000000001000000001011000110010100000000010110\r\n", "output": "436\r\n"}, {"input": "8 49\r\n000100100000000111110010011100110100010010000011000\r\n001000000101111000000001111100010010100000010000000\r\n000000010000011100001000000000101000110010000100100\r\n000000000001000110000011101101000000100000101010000\r\n000000110001000101101000000001000000110001000110000\r\n000100000000000000100100010011000001111101010100110\r\n000000001000000010101111000100001100000000010111000\r\n001000010000110000011100000000100110000010001000000\r\n", "output": "404\r\n"}, {"input": "9 41\r\n0011000000000101001101001000000001110000010\r\n0000110000001010110010110010110010010001000\r\n0001100010100000000001110100100001101000100\r\n0001010101111010000000010010001001011111000\r\n0101000101000011101011000000001100110010000\r\n0001010000000000000001011000000100010101000\r\n0000010011000000001000110001000010110001000\r\n0000100010000110100001000000100010001111100\r\n0000001110100001000001000110001110000100000\r\n", "output": "385\r\n"}, {"input": "10 29\r\n0000000000101001100001001011000\r\n0001110100000000000000100010000\r\n0010001001000011000100010001000\r\n0001000010101000000010100010100\r\n0111000000000000100100100010100\r\n0001000100011111000100010100000\r\n0000000000000001000001001011000\r\n0000101110000001010001011001110\r\n0000001000101010011000001100100\r\n0100010000101011010000000000000\r\n", "output": "299\r\n"}, {"input": "1 57\r\n00011101100001110001111000000100101111000111101100111001000\r\n", "output": "55\r\n"}, {"input": "2 32\r\n0011110111011011011101111101011110\r\n0111000110111111011110011101011110\r\n", "output": "65\r\n"}, {"input": "3 20\r\n0110011111110101101100\r\n0111110000111010100100\r\n0110111110010100011110\r\n", "output": "63\r\n"}, {"input": "4 4\r\n011100\r\n001010\r\n010000\r\n011110\r\n", "output": "22\r\n"}, {"input": "5 44\r\n0001010010001111111001111111000010100100000010\r\n0001111001111001101111011111010110001001111110\r\n0111111010111111011101100011101010100101110110\r\n0011010011101011101111001001010110000111111100\r\n0110100111011100110101110010010011011101100100\r\n", "output": "228\r\n"}, {"input": "6 36\r\n01110101111111110101011000011111110010\r\n00011101100010110111111111110001100100\r\n00001111110010111111101110101110111110\r\n00110110011100100111011110000000000010\r\n01100101101001010001011111100111101100\r\n00011111111011001000011001011110011110\r\n", "output": "226\r\n"}, {"input": "7 24\r\n01111001111001011010010100\r\n00111011010101000111101000\r\n01001110110010010110011110\r\n00000101111011011111111000\r\n01111111101111001001010010\r\n01110000111101011111111010\r\n00000100011100110000110000\r\n", "output": "179\r\n"}, {"input": "8 8\r\n0011101110\r\n0110010100\r\n0100111110\r\n0111111100\r\n0011010100\r\n0001101110\r\n0111100000\r\n0110111000\r\n", "output": "77\r\n"}, {"input": "9 48\r\n00011010111110111011111001111111111101001111110010\r\n01000101000101101101111110111101011100001011010010\r\n00110111110110101110101110111111011011101111011000\r\n00110111111100010110110110111001001111011010101110\r\n01111111100101010011111100100111110011001101110100\r\n01111011110011111101010101010100001110111111111000\r\n01110101101101110001000010110100010110101111111100\r\n00111101001010110010110100000111110101010100001000\r\n00011011010110011111001100111100100011100110110100\r\n", "output": "448\r\n"}, {"input": "10 40\r\n010011001001111011011011101111010001010010\r\n011000000110000010001011111010100000110000\r\n011010101001110010110110011111010101101000\r\n000111111010101111000110011111011011011010\r\n010110101110001001001111111000110011101010\r\n010011010100111110010100100111100111011110\r\n001111101100111111111111001010111010000110\r\n001111110010101100110100101110001011100110\r\n010111010010001111110101111111111110111000\r\n011101101111000100111111111001111100111010\r\n", "output": "418\r\n"}, {"input": "11 28\r\n011100111101101001011111001110\r\n010001111110011101101011001000\r\n001010011011011010101101101100\r\n001100011001101011011001110100\r\n010111110011101110000110111100\r\n010010001111110000011111010100\r\n001011111111110011101101111010\r\n001101101011100100011011001110\r\n001111110110100110101011000010\r\n000101101011100001101101100100\r\n010011101101111011100111110100\r\n", "output": "328\r\n"}, {"input": "1 68\r\n0101111110111111111111111111110111111111111111111110111111101111111110\r\n", "output": "68\r\n"}, {"input": "2 56\r\n0011111111111110111111111111111111011111111111011111011110\r\n0111111111010111111111110111111111111110111111010111111110\r\n", "output": "113\r\n"}, {"input": "3 17\r\n0111111101111111110\r\n0111111111101011110\r\n0101111111111111110\r\n", "output": "55\r\n"}, {"input": "4 4\r\n011110\r\n010110\r\n010110\r\n011110\r\n", "output": "22\r\n"}, {"input": "5 89\r\n0011111111111101110110111111111101111011111011101110111111111111111111111111111111111111110\r\n0111111111111111111111111101111111111111111111111111111111111111111111111111111111111111110\r\n0111111111111011111111111111111111101111011111111111111111110110111101111111111111111011010\r\n0111111111111111011011111111111011111111111111111111111111111111111111111111111110111111010\r\n0111111101111011111110101011111111110111100100101111111011111111111111011011101111111111110\r\n", "output": "453\r\n"}, {"input": "6 77\r\n0111111110101011111111111111111111111111111111111111100111111111101111111111110\r\n0111111111111111111101111101111111111011111111011111111001011111111111101111110\r\n0111101111111111111111111111111111111110110011111111111011111111101111111111110\r\n0111110111111111111111111111111111111111111111111111011011111111111111111111110\r\n0101111110111111111111111111111111111111111011111111111111111111101111011011110\r\n0110111111101111110111111111111011111111101011111101111111111111111111110111100\r\n", "output": "472\r\n"}, {"input": "7 20\r\n0111111111111111111100\r\n0111110111111111111110\r\n0111111111111111111100\r\n0111111011111111111110\r\n0111111111111011101110\r\n0111101011110111111010\r\n0111111111111111111010\r\n", "output": "151\r\n"}, {"input": "8 8\r\n0111111110\r\n0111101110\r\n0111111110\r\n0111111110\r\n0111111110\r\n0110111100\r\n0101111110\r\n0110111110\r\n", "output": "78\r\n"}, {"input": "11 24\r\n01111111111101111111111110\r\n01111111111111111111111110\r\n01110111111111111111111110\r\n01111111111111111111011110\r\n01111111111111111110111110\r\n01111010111111100111101110\r\n01111111111111010101111100\r\n01111111111111110111111110\r\n01011101111111111101111110\r\n00111111011111111110111110\r\n01111111101111111101111110\r\n", "output": "284\r\n"}, {"input": "12 12\r\n01111111111000\r\n01101111110110\r\n01111110111110\r\n01111111111110\r\n01111111111010\r\n01011111110110\r\n01111111111110\r\n01101101011110\r\n01111111111110\r\n01111101011110\r\n00111111111110\r\n01111111011110\r\n", "output": "166\r\n"}, {"input": "15 28\r\n011111111101011111111101111110\r\n011111111111111111111111111110\r\n011101110111011011101111011110\r\n011111111011111011110111111110\r\n011111111110101111111111111110\r\n011111011111110011111111011010\r\n011110111111001101111111111110\r\n011111111110111111111011111110\r\n011111111111111111111111011110\r\n011111011111111111111011001010\r\n011111111101111111111101111110\r\n011111111110111111101111011110\r\n010111111111101111111111111110\r\n011111111111111111011111111110\r\n011011111111111110110111110110\r\n", "output": "448\r\n"}, {"input": "2 11\r\n0100000000000\r\n0000000010000\r\n", "output": "18\r\n"}, {"input": "1 100\r\n010010010011100001101101110111101010000101010001111001001101011110000011101110101000100111111001101110\r\n", "output": "100\r\n"}, {"input": "15 1\r\n010\r\n010\r\n010\r\n010\r\n010\r\n010\r\n000\r\n000\r\n000\r\n010\r\n000\r\n010\r\n000\r\n000\r\n000\r\n", "output": "29\r\n"}, {"input": "3 3\r\n00010\r\n00000\r\n00010\r\n", "output": "7\r\n"}]
false
stdio
null
true
812/B
812
B
Python 3
TESTS
42
62
307,200
27607513
def left_traverse(a,m,n,l): for i in range(0,n): for j in range(1,m+1): if a[i][j]==1: l[i]=j break def right_traverse(a,m,n,r): for i in range(0,n): for j in range(m,0,-1): if a[i][j]==1: r[i]=j break def rkey(a,l,r,i,count,m,n): count[0] = count[0]+r[i]-l[i] if i==n-1: return j = i+1 while j<n-1: if l[j]!=0: break j = j+1 if l[j]==0: return lnav = r[i]+l[j]+j-i rnav = 2*m+3-r[i]-r[j]+j-i-1 if lnav<=rnav: count[0] = count[0]+lnav rkey(a,l,r,j,count,m,n) else: count[0] = count[0]+rnav lkey(a,l,r,j,count,m,n) def lkey(a,l,r,i,count,m,n): count[0] = count[0]+r[i]-l[i] if i==n-1: return j = i+1 while j<n-1: if l[j]!=0: break j = j+1 if l[j]==0: return lnav = l[i]+l[j]+j-i rnav = 2*m+3-l[i]-r[j]+j-i-1 if lnav<=rnav: count[0] = count[0]+lnav rkey(a,l,r,j,count,m,n) else: count[0] = count[0]+rnav lkey(a,l,r,j,count,m,n) p = input() p = p.split() x = [int(i) for i in p] m = x[1] n = x[0] b = [[0]*(m+2) for _ in range(n)] a = [[0]*(m+2) for _ in range(n)] for i in range(n): q = input() # b[i] = [str(j) for j in input().strip().split(" ")] b[i] = [int(j) for j in q] for i in range(0,n): for j in range(0,m+2): a[i][j] = b[n-i-1][j] l = [0 for i in range(n)] r = [0 for i in range(n)] count = [0] for i in range(0,n): l[i] = 0 r[i] = 0 left_traverse(a,m,n,l) right_traverse(a,m,n,r) count[0] = l[0] rkey(a,l,r,0,count,m,n) print(count[0])
72
62
1,638,400
27495577
import sys def main(): n,m = map(int,sys.stdin.readline().split()) m+=2 z = [] for i in range(n): z.append(sys.stdin.readline().rstrip()) ans = 0 y = n-1 x = 0 q = [[x,y,ans]] for i in range(n-1,-1,-1): first =-1 last = -1 for j in range(m): if z[i][j] == '1': if first == -1: first = j last = j if first == -1 and last == -1: continue if i == n-1: q[0] = [last,n-1,last] continue if first == last : for t in q: t[2]+= min(t[0]+first, m-1-t[0]+m-1-first) + t[1]-i t[0] = first t[1] = i continue size = len(q) for s in range(size): t = q[s] q.append([last,i,t[2]+t[0]+last+t[1]-i]) t[2]+= m-1-t[0]+m-1-first + t[1] - i t[0] = first t[1] = i q[s] = t ans = q[0][2] for i in range(len(q)): if q[i][2] < ans: ans = q[i][2] print(ans) main()
Codeforces Round 417 (Div. 2)
CF
2,017
1
256
Sagheer, the Hausmeister
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off. The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms. Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights. Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
The first line contains two integers n and m (1 ≤ n ≤ 15 and 1 ≤ m ≤ 100) — the number of floors and the number of rooms in each floor, respectively. The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor. The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
Print a single integer — the minimum total time needed to turn off all the lights.
null
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs. In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor. In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor.
[{"input": "2 2\n0010\n0100", "output": "5"}, {"input": "3 4\n001000\n000010\n000010", "output": "12"}, {"input": "4 3\n01110\n01110\n01110\n01110", "output": "18"}]
1,600
["bitmasks", "brute force", "dp"]
72
[{"input": "2 2\r\n0010\r\n0100\r\n", "output": "5\r\n"}, {"input": "3 4\r\n001000\r\n000010\r\n000010\r\n", "output": "12\r\n"}, {"input": "4 3\r\n01110\r\n01110\r\n01110\r\n01110\r\n", "output": "18\r\n"}, {"input": "3 2\r\n0000\r\n0100\r\n0100\r\n", "output": "4\r\n"}, {"input": "1 89\r\n0000000000000000000000000000000100000000000000010000000000010000000000000000000000000000000\r\n", "output": "59\r\n"}, {"input": "2 73\r\n000000000000000000000000000000000000000000000000000000000000000000000000000\r\n000000000000000000000000000000000000000100000010000000000000000000000000000\r\n", "output": "46\r\n"}, {"input": "3 61\r\n000000000000000000000000000000000000000000000000000000000000000\r\n000000000000000000000000000000000000000000000000000000000000000\r\n000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "0\r\n"}, {"input": "4 53\r\n0000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000\r\n", "output": "0\r\n"}, {"input": "5 93\r\n00000000000000000000000000000000000000000000000000000000100000000000000000000000000000000001010\r\n00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n00000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n00000000000000000000000000000010000000000000000000100000000000000000000000000000000000000000000\r\n00000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "265\r\n"}, {"input": "6 77\r\n0000000000000000100000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000010000000000000\r\n0000000000010000000000000000000000000000000000000000000000000000000000000000010\r\n0000000000000000000001000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000100000000000000000000000000000\r\n", "output": "311\r\n"}, {"input": "7 65\r\n0000000001000000000000000010000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000001000001000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n0000000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "62\r\n"}, {"input": "8 57\r\n00000000100000000000000000000000000000000000000000000000000\r\n00000000000000010000000000000000000000000000000000000000000\r\n00000000000000000000000000000000000100000000000000000000000\r\n00000000000000000000000000000000000000000000000000000000000\r\n00000000000000000000000000000000000100000000000000000000000\r\n00000000000000000000000000000000000000000000000000000000000\r\n00000000000010000000000000000000000000000000000000000000000\r\n00000000000000000000000000000000000000000000000001000000000\r\n", "output": "277\r\n"}, {"input": "12 13\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n010000000000000\r\n000000000000000\r\n000000000000000\r\n000000000000000\r\n000010000000000\r\n000000000000000\r\n", "output": "14\r\n"}, {"input": "13 1\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n000\r\n", "output": "0\r\n"}, {"input": "1 33\r\n00000100101110001101000000110100010\r\n", "output": "33\r\n"}, {"input": "2 21\r\n00100110100010010010010\r\n01000001111001010000000\r\n", "output": "43\r\n"}, {"input": "3 5\r\n0001010\r\n0100000\r\n0100000\r\n", "output": "11\r\n"}, {"input": "4 45\r\n00010000101101100000101101000000100000001101100\r\n01110000100111010011000000100000000001000001100\r\n00000000001000100110100001000010011010001010010\r\n01111110100100000101101010011000100100001000000\r\n", "output": "184\r\n"}, {"input": "5 37\r\n010100000000000000000110000110010000010\r\n001101100010110011101000001010101101110\r\n010000001000100010010100000000001010000\r\n000000000100101000000101100001000001110\r\n000010000000000000100001001000011100110\r\n", "output": "193\r\n"}, {"input": "6 25\r\n011001000100111010000101000\r\n000000000010000010001000010\r\n011001100001100001001001010\r\n000000100000010000000000110\r\n010001100001000001000000010\r\n011000001001010111110000100\r\n", "output": "160\r\n"}, {"input": "7 61\r\n010000111100010100001000011010100001000000000011100000100010000\r\n000010011000001000000100110101010001000000010001100000100100100\r\n000010001000001000000100001000000100100011001110000111000000100\r\n000000000101000011010000011000000101000001011001000011101010010\r\n000010010011000000100000110000001000000101000000101000010000010\r\n000010010101101100100100100011001011101010000101000010000101010\r\n000100001100001001000000001000000001011000110010100000000010110\r\n", "output": "436\r\n"}, {"input": "8 49\r\n000100100000000111110010011100110100010010000011000\r\n001000000101111000000001111100010010100000010000000\r\n000000010000011100001000000000101000110010000100100\r\n000000000001000110000011101101000000100000101010000\r\n000000110001000101101000000001000000110001000110000\r\n000100000000000000100100010011000001111101010100110\r\n000000001000000010101111000100001100000000010111000\r\n001000010000110000011100000000100110000010001000000\r\n", "output": "404\r\n"}, {"input": "9 41\r\n0011000000000101001101001000000001110000010\r\n0000110000001010110010110010110010010001000\r\n0001100010100000000001110100100001101000100\r\n0001010101111010000000010010001001011111000\r\n0101000101000011101011000000001100110010000\r\n0001010000000000000001011000000100010101000\r\n0000010011000000001000110001000010110001000\r\n0000100010000110100001000000100010001111100\r\n0000001110100001000001000110001110000100000\r\n", "output": "385\r\n"}, {"input": "10 29\r\n0000000000101001100001001011000\r\n0001110100000000000000100010000\r\n0010001001000011000100010001000\r\n0001000010101000000010100010100\r\n0111000000000000100100100010100\r\n0001000100011111000100010100000\r\n0000000000000001000001001011000\r\n0000101110000001010001011001110\r\n0000001000101010011000001100100\r\n0100010000101011010000000000000\r\n", "output": "299\r\n"}, {"input": "1 57\r\n00011101100001110001111000000100101111000111101100111001000\r\n", "output": "55\r\n"}, {"input": "2 32\r\n0011110111011011011101111101011110\r\n0111000110111111011110011101011110\r\n", "output": "65\r\n"}, {"input": "3 20\r\n0110011111110101101100\r\n0111110000111010100100\r\n0110111110010100011110\r\n", "output": "63\r\n"}, {"input": "4 4\r\n011100\r\n001010\r\n010000\r\n011110\r\n", "output": "22\r\n"}, {"input": "5 44\r\n0001010010001111111001111111000010100100000010\r\n0001111001111001101111011111010110001001111110\r\n0111111010111111011101100011101010100101110110\r\n0011010011101011101111001001010110000111111100\r\n0110100111011100110101110010010011011101100100\r\n", "output": "228\r\n"}, {"input": "6 36\r\n01110101111111110101011000011111110010\r\n00011101100010110111111111110001100100\r\n00001111110010111111101110101110111110\r\n00110110011100100111011110000000000010\r\n01100101101001010001011111100111101100\r\n00011111111011001000011001011110011110\r\n", "output": "226\r\n"}, {"input": "7 24\r\n01111001111001011010010100\r\n00111011010101000111101000\r\n01001110110010010110011110\r\n00000101111011011111111000\r\n01111111101111001001010010\r\n01110000111101011111111010\r\n00000100011100110000110000\r\n", "output": "179\r\n"}, {"input": "8 8\r\n0011101110\r\n0110010100\r\n0100111110\r\n0111111100\r\n0011010100\r\n0001101110\r\n0111100000\r\n0110111000\r\n", "output": "77\r\n"}, {"input": "9 48\r\n00011010111110111011111001111111111101001111110010\r\n01000101000101101101111110111101011100001011010010\r\n00110111110110101110101110111111011011101111011000\r\n00110111111100010110110110111001001111011010101110\r\n01111111100101010011111100100111110011001101110100\r\n01111011110011111101010101010100001110111111111000\r\n01110101101101110001000010110100010110101111111100\r\n00111101001010110010110100000111110101010100001000\r\n00011011010110011111001100111100100011100110110100\r\n", "output": "448\r\n"}, {"input": "10 40\r\n010011001001111011011011101111010001010010\r\n011000000110000010001011111010100000110000\r\n011010101001110010110110011111010101101000\r\n000111111010101111000110011111011011011010\r\n010110101110001001001111111000110011101010\r\n010011010100111110010100100111100111011110\r\n001111101100111111111111001010111010000110\r\n001111110010101100110100101110001011100110\r\n010111010010001111110101111111111110111000\r\n011101101111000100111111111001111100111010\r\n", "output": "418\r\n"}, {"input": "11 28\r\n011100111101101001011111001110\r\n010001111110011101101011001000\r\n001010011011011010101101101100\r\n001100011001101011011001110100\r\n010111110011101110000110111100\r\n010010001111110000011111010100\r\n001011111111110011101101111010\r\n001101101011100100011011001110\r\n001111110110100110101011000010\r\n000101101011100001101101100100\r\n010011101101111011100111110100\r\n", "output": "328\r\n"}, {"input": "1 68\r\n0101111110111111111111111111110111111111111111111110111111101111111110\r\n", "output": "68\r\n"}, {"input": "2 56\r\n0011111111111110111111111111111111011111111111011111011110\r\n0111111111010111111111110111111111111110111111010111111110\r\n", "output": "113\r\n"}, {"input": "3 17\r\n0111111101111111110\r\n0111111111101011110\r\n0101111111111111110\r\n", "output": "55\r\n"}, {"input": "4 4\r\n011110\r\n010110\r\n010110\r\n011110\r\n", "output": "22\r\n"}, {"input": "5 89\r\n0011111111111101110110111111111101111011111011101110111111111111111111111111111111111111110\r\n0111111111111111111111111101111111111111111111111111111111111111111111111111111111111111110\r\n0111111111111011111111111111111111101111011111111111111111110110111101111111111111111011010\r\n0111111111111111011011111111111011111111111111111111111111111111111111111111111110111111010\r\n0111111101111011111110101011111111110111100100101111111011111111111111011011101111111111110\r\n", "output": "453\r\n"}, {"input": "6 77\r\n0111111110101011111111111111111111111111111111111111100111111111101111111111110\r\n0111111111111111111101111101111111111011111111011111111001011111111111101111110\r\n0111101111111111111111111111111111111110110011111111111011111111101111111111110\r\n0111110111111111111111111111111111111111111111111111011011111111111111111111110\r\n0101111110111111111111111111111111111111111011111111111111111111101111011011110\r\n0110111111101111110111111111111011111111101011111101111111111111111111110111100\r\n", "output": "472\r\n"}, {"input": "7 20\r\n0111111111111111111100\r\n0111110111111111111110\r\n0111111111111111111100\r\n0111111011111111111110\r\n0111111111111011101110\r\n0111101011110111111010\r\n0111111111111111111010\r\n", "output": "151\r\n"}, {"input": "8 8\r\n0111111110\r\n0111101110\r\n0111111110\r\n0111111110\r\n0111111110\r\n0110111100\r\n0101111110\r\n0110111110\r\n", "output": "78\r\n"}, {"input": "11 24\r\n01111111111101111111111110\r\n01111111111111111111111110\r\n01110111111111111111111110\r\n01111111111111111111011110\r\n01111111111111111110111110\r\n01111010111111100111101110\r\n01111111111111010101111100\r\n01111111111111110111111110\r\n01011101111111111101111110\r\n00111111011111111110111110\r\n01111111101111111101111110\r\n", "output": "284\r\n"}, {"input": "12 12\r\n01111111111000\r\n01101111110110\r\n01111110111110\r\n01111111111110\r\n01111111111010\r\n01011111110110\r\n01111111111110\r\n01101101011110\r\n01111111111110\r\n01111101011110\r\n00111111111110\r\n01111111011110\r\n", "output": "166\r\n"}, {"input": "15 28\r\n011111111101011111111101111110\r\n011111111111111111111111111110\r\n011101110111011011101111011110\r\n011111111011111011110111111110\r\n011111111110101111111111111110\r\n011111011111110011111111011010\r\n011110111111001101111111111110\r\n011111111110111111111011111110\r\n011111111111111111111111011110\r\n011111011111111111111011001010\r\n011111111101111111111101111110\r\n011111111110111111101111011110\r\n010111111111101111111111111110\r\n011111111111111111011111111110\r\n011011111111111110110111110110\r\n", "output": "448\r\n"}, {"input": "2 11\r\n0100000000000\r\n0000000010000\r\n", "output": "18\r\n"}, {"input": "1 100\r\n010010010011100001101101110111101010000101010001111001001101011110000011101110101000100111111001101110\r\n", "output": "100\r\n"}, {"input": "15 1\r\n010\r\n010\r\n010\r\n010\r\n010\r\n010\r\n000\r\n000\r\n000\r\n010\r\n000\r\n010\r\n000\r\n000\r\n000\r\n", "output": "29\r\n"}, {"input": "3 3\r\n00010\r\n00000\r\n00010\r\n", "output": "7\r\n"}]
false
stdio
null
true
676/C
676
C
Python 3
TESTS
67
124
1,536,000
106708516
#Created By Minsol Jeong def I3(): n, k = [int(x) for x in input().split()] s = input() a='a' b='b' maxBeauty = 1 a_max = s.count(a) b_max = s.count(b) if a_max <= k or b_max <= k: maxBeauty = n elif a_max >= b_max: foundB = [i for i, x in enumerate(s) if x == b] for i in range(len(foundB) - k + 1): if k + i >= len(foundB): maxB = len(s[foundB[i-1]:])-1 elif i == 0: maxB = len(s[:foundB[k]]) else: maxB = len(s[foundB[i - 1]:foundB[k + i]]) - 1 maxBeauty = max(maxB, maxBeauty) else: foundA = [i for i, x in enumerate(s) if x == a] for i in range(len(foundA)-k+1): if k+i >= len(foundA): maxA = len(s[foundA[i-1]:])-1 elif i == 0: maxA = len(s[:foundA[k]]) else: maxA = len(s[foundA[i-1]:foundA[k+i]])-1 maxBeauty = max(maxA, maxBeauty) print(maxBeauty) if __name__ == '__main__': I3()
117
62
3,686,400
147689765
def check(n,k,s,x): cnt, ans, j = 0, 0, 0 for i in range(n): if s[i] == x: cnt += 1 if cnt > k: while cnt > k: if s[j] == x: cnt -= 1 j += 1 ans = max(ans, i - j + 1) return ans def solve(): n, k = map(int, input().split()) s = input() print(max(check(n, k, s, 'a'), check(n, k, s, 'b'))) solve()
Codeforces Round 354 (Div. 2)
CF
2,016
1
256
Vasya and String
High school student Vasya got a string of length n as a birthday present. This string consists of letters 'a' and 'b' only. Vasya denotes beauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equal letters. Vasya can change no more than k characters of the original string. What is the maximum beauty of the string he can achieve?
The first line of the input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n) — the length of the string and the maximum number of characters to change. The second line contains the string, consisting of letters 'a' and 'b' only.
Print the only integer — the maximum beauty of the string Vasya can achieve by changing no more than k characters.
null
In the first sample, Vasya can obtain both strings "aaaa" and "bbbb". In the second sample, the optimal answer is obtained with the string "aaaaabaa" or with the string "aabaaaaa".
[{"input": "4 2\nabba", "output": "4"}, {"input": "8 1\naabaabaa", "output": "5"}]
1,500
["binary search", "dp", "strings", "two pointers"]
117
[{"input": "4 2\r\nabba\r\n", "output": "4\r\n"}, {"input": "8 1\r\naabaabaa\r\n", "output": "5\r\n"}, {"input": "1 0\r\na\r\n", "output": "1\r\n"}, {"input": "1 1\r\nb\r\n", "output": "1\r\n"}, {"input": "1 0\r\nb\r\n", "output": "1\r\n"}, {"input": "1 1\r\na\r\n", "output": "1\r\n"}, {"input": "10 10\r\nbbbbbbbbbb\r\n", "output": "10\r\n"}, {"input": "10 2\r\nbbbbbbbbbb\r\n", "output": "10\r\n"}, {"input": "10 1\r\nbbabbabbba\r\n", "output": "6\r\n"}, {"input": "10 10\r\nbbabbbaabb\r\n", "output": "10\r\n"}, {"input": "10 9\r\nbabababbba\r\n", "output": "10\r\n"}, {"input": "10 4\r\nbababbaaab\r\n", "output": "9\r\n"}, {"input": "10 10\r\naabaaabaaa\r\n", "output": "10\r\n"}, {"input": "10 10\r\naaaabbbaaa\r\n", "output": "10\r\n"}, {"input": "10 1\r\nbaaaaaaaab\r\n", "output": "9\r\n"}, {"input": "10 5\r\naaaaabaaaa\r\n", "output": "10\r\n"}, {"input": "10 4\r\naaaaaaaaaa\r\n", "output": "10\r\n"}, {"input": "100 10\r\nbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\r\n", "output": "100\r\n"}, {"input": "100 7\r\nbbbbabbbbbaabbbabbbbbbbbbbbabbbbbbbbbbbbbbbbbbbbbbbbbabbbbbbbbbbbabbabbbbbbbbbbbbbbbbbbbbbbbbbbbbbab\r\n", "output": "93\r\n"}, {"input": "100 30\r\nbbaabaaabbbbbbbbbbaababababbbbbbaabaabbbbbbbbabbbbbabbbbabbbbbbbbaabbbbbbbbbabbbbbabbbbbbbbbaaaaabba\r\n", "output": "100\r\n"}, {"input": "100 6\r\nbaababbbaabbabbaaabbabbaabbbbbbbbaabbbabbbbaabbabbbbbabababbbbabbbbbbabbbbbbbbbaaaabbabbbbaabbabaabb\r\n", "output": "34\r\n"}, {"input": "100 45\r\naabababbabbbaaabbbbbbaabbbabbaabbbbbabbbbbbbbabbbbbbabbaababbaabbababbbbbbababbbbbaabbbbbbbaaaababab\r\n", "output": "100\r\n"}, {"input": "100 2\r\nababaabababaaababbaaaabbaabbbababbbaaabbbbabababbbabababaababaaabaabbbbaaabbbabbbbbabbbbbbbaabbabbba\r\n", "output": "17\r\n"}, {"input": "100 25\r\nbabbbaaababaaabbbaabaabaabbbabbabbbbaaaaaaabaaabaaaaaaaaaabaaaabaaabbbaaabaaababaaabaabbbbaaaaaaaaaa\r\n", "output": "80\r\n"}, {"input": "100 14\r\naabaaaaabababbabbabaaaabbaaaabaaabbbaaabaaaaaaaabaaaaabbaaaaaaaaabaaaaaaabbaababaaaababbbbbabaaaabaa\r\n", "output": "61\r\n"}, {"input": "100 8\r\naaaaabaaaabaabaaaaaaaabaaaabaaaaaaaaaaaaaabaaaaabaaaaaaaaaaaaaaaaabaaaababaabaaaaaaaaaaaaabbabaaaaaa\r\n", "output": "76\r\n"}, {"input": "100 12\r\naaaaaaaaaaaaaaaabaaabaaaaaaaaaabbaaaabbabaaaaaaaaaaaaaaaaaaaaabbaaabaaaaaaaaaaaabaaaaaaaabaaaaaaaaaa\r\n", "output": "100\r\n"}, {"input": "100 65\r\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n", "output": "100\r\n"}, {"input": "10 0\r\nbbbbbbbbbb\r\n", "output": "10\r\n"}, {"input": "10 0\r\nbbbbabbbbb\r\n", "output": "5\r\n"}, {"input": "10 0\r\nbbabbbabba\r\n", "output": "3\r\n"}, {"input": "10 0\r\nbaabbbbaba\r\n", "output": "4\r\n"}, {"input": "10 0\r\naababbbbaa\r\n", "output": "4\r\n"}, {"input": "10 2\r\nabbbbbaaba\r\n", "output": "8\r\n"}, {"input": "10 0\r\nabbaaabaaa\r\n", "output": "3\r\n"}, {"input": "10 0\r\naabbaaabaa\r\n", "output": "3\r\n"}, {"input": "10 1\r\naaaaaababa\r\n", "output": "8\r\n"}, {"input": "10 0\r\nbaaaaaaaaa\r\n", "output": "9\r\n"}, {"input": "10 0\r\naaaaaaaaaa\r\n", "output": "10\r\n"}, {"input": "100 0\r\nbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\r\n", "output": "100\r\n"}, {"input": "100 0\r\nbbbbbbbbbbabbbbaaabbbbbbbbbbbabbbabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbabbbbbbbbbabbbbbbbbbbbbbab\r\n", "output": "40\r\n"}, {"input": "100 11\r\nbaabbbbbababbbbabbbbbbbabbbbbbbbbbbbbbabbbbbbababbbbababbbbaaabbbbabbbbbabbbbbbbbabababbbabbbbbbbabb\r\n", "output": "65\r\n"}, {"input": "100 8\r\nbbababbbbbaabbbaaababbbbababababbbbababbabbbabbbbbaabbbabbbababbabbbbabbbabbbbaabbbbabbbaabbbbaaaabb\r\n", "output": "33\r\n"}, {"input": "100 21\r\nabbaaaabbbababaabbbababbbbbbbbabbaababababbbabbbaaabbaaabbbbabbabbbabbbabaababbbabbbbbabbbbbbabbbbab\r\n", "output": "65\r\n"}, {"input": "100 9\r\nabbbaabaabaaaaaaabbabbbababbaaabbbaaabbaabaaaaabbbbbabbaabaabbbbbaaaaababbaaabbabaabaaabababbaababbb\r\n", "output": "26\r\n"}, {"input": "100 5\r\naababababbaaaaaaaabbbabaaaabbabaaaabbaabaaaaabababbabaabaaabaaaaaaaabaababbabbaaabaabbabbaaaaabbabba\r\n", "output": "22\r\n"}, {"input": "100 9\r\naababaabaaaaaaaaabbbaabaaaaaaabaaaaaaaaaaaaabaaabaabaabbbbabbaababbabbaaaabbababaabaababaabaaaaaaaaa\r\n", "output": "49\r\n"}, {"input": "100 6\r\naaaaabbaaaaaaaaaaabaaaabaaaaaaaaabaaabaaaaaabaaaaaaaaaaabaabaaaabaaaaaaaaaaaaaaabaabbaaaaaaaaaaaaaaa\r\n", "output": "56\r\n"}, {"input": "100 7\r\nabaaabaabaabaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaabaaaaaaabbabaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaba\r\n", "output": "86\r\n"}, {"input": "100 0\r\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n", "output": "100\r\n"}]
false
stdio
null
true
618/C
618
C
PyPy 3
TESTS
14
998
33,382,400
28419123
n = int(input()) pos = [] for i in range(0, n): tmp = input() tmp = tmp.split() pos.append([int(tmp[0]), int(tmp[1])]) x1 = pos[0][0] y1 = pos[0][1] x2 = pos[1][0] y2 = pos[1][1] tar = 1 for i in range(2, n): if (pos[i][0] - x1) * (y2 - y1) == (x2 - x1) * (pos[i][1] - y1)\ and abs(pos[i][0] - x1) + abs(pos[i][1] - y1) < abs(y2 - y1) + abs(x2 - x1): tar = i x2 = pos[i][0] y2 = pos[i][0] MIN = 1000000000000000.0 ans = -1 for i in range(2, n): if(pos[i][0] - x1) * (y2 - y1) == (x2 - x1) * (pos[i][1] - y1): continue if x1 == x2 and x1 != pos[i][0] and abs(pos[i][0] - x1) < MIN: MIN = abs(pos[i][0] - x1) ans = i elif x1 != x2: k = (y2 - y1) / (x2 - x1) b1 = y1 - k * x1 b2 = pos[i][1] - k * pos[i][0] if abs(b1 - b2) < MIN and b1 != b2: MIN = abs(b1 - b2) ans = i print ("%d %d %d" % (1, tar + 1, ans + 1))
98
343
28,979,200
175796040
import sys input = sys.stdin.buffer.readline def gcd(a, b): if a > b: a, b = b, a if b % a==0: return a return gcd(b % a, a) def find_slope(p1, p2): x1, y1 = p1 x2, y2 = p2 if x1==x2: return (False, None, None) num1 = (y2-y1) den1 = (x2-x1) if den1 < 0: num1 = num1*-1 den1 = den1*-1 if num1==0: return (True, 0, 1) g = gcd(abs(num1), abs(den1)) return (True, num1//g, den1//g) def process(A): n = len(A) """ Say we take x, the closest to x, and the next closest to x not on the same line a...b c """ x1, y1 = A[0] A2 = [[A[i][0], A[i][1], i+1] for i in range(n)] A2 = sorted(A2, key=lambda a: (a[0]-x1)**2+(a[1]-y1)**2, reverse=True) A2.pop() x2, y2, i1 = A2.pop() slope1 = find_slope((x1, y1), (x2, y2)) while len(A2) > 0: x3, y3, i2 = A2.pop() if find_slope((x1, y1), (x3, y3)) != slope1: sys.stdout.write(f'1 {i1} {i2}\n') return n = int(input()) A = [] for i in range(n): x, y = [int(x) for x in input().split()] A.append([x, y]) process(A)
Wunder Fund Round 2016 (Div. 1 + Div. 2 combined)
CF
2,016
2
256
Constellation
Cat Noku has obtained a map of the night sky. On this map, he found a constellation with n stars numbered from 1 to n. For each i, the i-th star is located at coordinates (xi, yi). No two stars are located at the same position. In the evening Noku is going to take a look at the night sky. He would like to find three distinct stars and form a triangle. The triangle must have positive area. In addition, all other stars must lie strictly outside of this triangle. He is having trouble finding the answer and would like your help. Your job is to find the indices of three stars that would form a triangle that satisfies all the conditions. It is guaranteed that there is no line such that all stars lie on that line. It can be proven that if the previous condition is satisfied, there exists a solution to this problem.
The first line of the input contains a single integer n (3 ≤ n ≤ 100 000). Each of the next n lines contains two integers xi and yi ( - 109 ≤ xi, yi ≤ 109). It is guaranteed that no two stars lie at the same point, and there does not exist a line such that all stars lie on that line.
Print three distinct integers on a single line — the indices of the three points that form a triangle that satisfies the conditions stated in the problem. If there are multiple possible answers, you may print any of them.
null
In the first sample, we can print the three indices in any order. In the second sample, we have the following picture. Note that the triangle formed by starts 1, 4 and 3 doesn't satisfy the conditions stated in the problem, as point 5 is not strictly outside of this triangle (it lies on it's border).
[{"input": "3\n0 1\n1 0\n1 1", "output": "1 2 3"}, {"input": "5\n0 0\n0 2\n2 0\n2 2\n1 1", "output": "1 3 5"}]
1,600
["geometry", "implementation"]
98
[{"input": "3\r\n0 1\r\n1 0\r\n1 1\r\n", "output": "1 2 3\r\n"}, {"input": "5\r\n0 0\r\n0 2\r\n2 0\r\n2 2\r\n1 1\r\n", "output": "1 3 5\r\n"}, {"input": "3\r\n819934317 939682125\r\n487662889 8614219\r\n-557136619 382982369\r\n", "output": "1 3 2\r\n"}, {"input": "10\r\n25280705 121178189\r\n219147240 -570920213\r\n-829849659 923854124\r\n18428128 -781819137\r\n-876779400 528386329\r\n-780997681 387686853\r\n-101900553 749998368\r\n58277314 355353788\r\n732128908 336416193\r\n840698381 600685123\r\n", "output": "1 3 2\r\n"}, {"input": "10\r\n404775998 670757742\r\n30131431 723806809\r\n25599613 633170449\r\n13303280 387243789\r\n-33017802 -539177851\r\n1425218 149682549\r\n-47620079 -831223391\r\n-25996011 -398742031\r\n38471092 890600029\r\n-3745401 46270169\r\n", "output": "1 2 3\r\n"}, {"input": "10\r\n13303280 387243789\r\n30131431 723806809\r\n404775998 670757742\r\n-25996011 -398742031\r\n25599613 633170449\r\n38471092 890600029\r\n-33017802 -539177851\r\n-47620079 -831223391\r\n1425218 149682549\r\n-3745401 46270169\r\n", "output": "1 3 5\r\n"}, {"input": "10\r\n999999999 1\r\n999999998 1\r\n999999997 1\r\n1000000000 1\r\n999999996 1\r\n999999995 1\r\n999999994 1\r\n999999992 1\r\n999999993 1\r\n0 0\r\n", "output": "1 2 10\r\n"}, {"input": "4\r\n0 1\r\n0 2\r\n0 3\r\n7 7\r\n", "output": "1 4 2\r\n"}, {"input": "3\r\n0 0\r\n999999999 1\r\n999999998 1\r\n", "output": "1 2 3\r\n"}, {"input": "10\r\n0 999999999\r\n0 1000000000\r\n-1 1000000000\r\n1 1000000000\r\n-2 1000000000\r\n2 1000000000\r\n-3 1000000000\r\n3 1000000000\r\n-4 1000000000\r\n4 1000000000\r\n", "output": "1 2 3\r\n"}, {"input": "12\r\n1000000000 0\r\n1000000000 1\r\n1000000000 2\r\n1000000000 3\r\n1000000000 4\r\n1000000000 5\r\n1000000000 6\r\n1000000000 7\r\n1000000000 8\r\n1000000000 9\r\n1000000000 10\r\n999999999 5\r\n", "output": "1 2 12\r\n"}, {"input": "12\r\n1000000000 0\r\n1000000000 1\r\n1000000000 2\r\n1000000000 3\r\n1000000000 4\r\n1000000000 5\r\n1000000000 6\r\n1000000000 7\r\n1000000000 8\r\n1000000000 9\r\n1000000000 10\r\n999999999 -1\r\n", "output": "1 2 12\r\n"}, {"input": "12\r\n1000000000 0\r\n1000000000 1\r\n1000000000 2\r\n1000000000 3\r\n1000000000 4\r\n1000000000 5\r\n1000000000 6\r\n1000000000 7\r\n1000000000 8\r\n1000000000 9\r\n1000000000 10\r\n999999999 10\r\n", "output": "1 2 12\r\n"}, {"input": "12\r\n1000000000 0\r\n1000000000 1\r\n1000000000 2\r\n1000000000 3\r\n1000000000 4\r\n1000000000 5\r\n1000000000 6\r\n1000000000 7\r\n1000000000 8\r\n1000000000 9\r\n1000000000 10\r\n999999999 1\r\n", "output": "1 2 12\r\n"}, {"input": "11\r\n-1000000000 1\r\n-1000000000 2\r\n-1000000000 3\r\n-1000000000 4\r\n-1000000000 5\r\n-1000000000 6\r\n-1000000000 7\r\n-1000000000 8\r\n-1000000000 9\r\n-1000000000 10\r\n-999999999 5\r\n", "output": "1 11 2\r\n"}, {"input": "11\r\n-1000000000 1\r\n-1000000000 2\r\n-1000000000 3\r\n-1000000000 4\r\n-1000000000 5\r\n-1000000000 6\r\n-1000000000 7\r\n-1000000000 8\r\n-1000000000 9\r\n-1000000000 10\r\n-999999999 7\r\n", "output": "1 11 2\r\n"}, {"input": "11\r\n-1000000000 1\r\n-1000000000 2\r\n-1000000000 3\r\n-1000000000 4\r\n-1000000000 5\r\n-1000000000 6\r\n-1000000000 7\r\n-1000000000 8\r\n-1000000000 9\r\n-1000000000 10\r\n-999999999 8\r\n", "output": "1 11 2\r\n"}, {"input": "11\r\n-1000000000 1\r\n-1000000000 2\r\n-1000000000 3\r\n-1000000000 4\r\n-1000000000 5\r\n-1000000000 6\r\n-1000000000 7\r\n-1000000000 8\r\n-1000000000 9\r\n-1000000000 10\r\n-999999999 10\r\n", "output": "1 11 2\r\n"}, {"input": "11\r\n-1000000000 -1\r\n-1000000000 -2\r\n-1000000000 -3\r\n-1000000000 -4\r\n-1000000000 -5\r\n-1000000000 -6\r\n-1000000000 -7\r\n-1000000000 -8\r\n-1000000000 -9\r\n-1000000000 -10\r\n-999999999 -5\r\n", "output": "1 2 11\r\n"}, {"input": "11\r\n-1000000000 -1\r\n-1000000000 -2\r\n-1000000000 -3\r\n-1000000000 -4\r\n-1000000000 -5\r\n-1000000000 -6\r\n-1000000000 -7\r\n-1000000000 -8\r\n-1000000000 -9\r\n-1000000000 -10\r\n-999999999 -1\r\n", "output": "1 2 11\r\n"}, {"input": "11\r\n-1000000000 -1\r\n-1000000000 -2\r\n-1000000000 -3\r\n-1000000000 -4\r\n-1000000000 -5\r\n-1000000000 -6\r\n-1000000000 -7\r\n-1000000000 -8\r\n-1000000000 -9\r\n-1000000000 -10\r\n-999999999 -2\r\n", "output": "1 2 11\r\n"}, {"input": "11\r\n-1000000000 -1\r\n-1000000000 -2\r\n-1000000000 -3\r\n-1000000000 -4\r\n-1000000000 -5\r\n-1000000000 -6\r\n-1000000000 -7\r\n-1000000000 -8\r\n-1000000000 -9\r\n-1000000000 -10\r\n-999999999 -4\r\n", "output": "1 2 11\r\n"}, {"input": "11\r\n-1000000000 -1\r\n-1000000000 -2\r\n-1000000000 -3\r\n-1000000000 -4\r\n-1000000000 -5\r\n-1000000000 -6\r\n-1000000000 -7\r\n-1000000000 -8\r\n-1000000000 -9\r\n-1000000000 -10\r\n-999999999 -8\r\n", "output": "1 2 11\r\n"}, {"input": "10\r\n2 1000000000\r\n8 1000000000\r\n9 1000000000\r\n3 1000000000\r\n4 1000000000\r\n5 1000000000\r\n6 1000000000\r\n1 1000000000\r\n7 1000000000\r\n0 0\r\n", "output": "1 10 4\r\n"}, {"input": "10\r\n1000000000 1\r\n999999999 1\r\n999999998 1\r\n999999997 1\r\n999999996 1\r\n999999995 1\r\n999999994 1\r\n999999993 1\r\n999999992 1\r\n0 0\r\n", "output": "1 2 10\r\n"}, {"input": "10\r\n999999999 1\r\n999999998 1\r\n999999997 1\r\n999999996 1\r\n999999995 1\r\n999999994 1\r\n999999993 1\r\n1000000000 1\r\n999999992 1\r\n0 0\r\n", "output": "1 2 10\r\n"}, {"input": "4\r\n0 0\r\n1 0\r\n2 0\r\n1 100\r\n", "output": "1 2 4\r\n"}, {"input": "4\r\n0 0\r\n3 0\r\n2 0\r\n1 1\r\n", "output": "3 2 4\r\n"}, {"input": "4\r\n0 0\r\n1 1\r\n2 2\r\n3 4\r\n", "output": "1 2 4\r\n"}, {"input": "4\r\n0 0\r\n0 1\r\n0 2\r\n1 1\r\n", "output": "1 4 2\r\n"}, {"input": "4\r\n0 0\r\n2 0\r\n1 0\r\n1 1\r\n", "output": "3 2 4\r\n"}, {"input": "4\r\n0 0\r\n1 1\r\n2 2\r\n5 -1\r\n", "output": "1 4 2\r\n"}, {"input": "5\r\n0 1\r\n0 2\r\n0 3\r\n0 4\r\n10 10\r\n", "output": "1 5 2\r\n"}, {"input": "4\r\n0 1\r\n0 2\r\n0 3\r\n1 1\r\n", "output": "1 4 2\r\n"}, {"input": "4\r\n0 0\r\n1 0\r\n2 0\r\n2 1\r\n", "output": "1 2 4\r\n"}, {"input": "4\r\n0 0\r\n-1 -1\r\n1 1\r\n100 0\r\n", "output": "1 2 4\r\n"}, {"input": "4\r\n0 0\r\n2 0\r\n1 1\r\n1 0\r\n", "output": "4 2 3\r\n"}, {"input": "4\r\n0 0\r\n1 0\r\n2 0\r\n3 1\r\n", "output": "1 2 4\r\n"}, {"input": "3\r\n0 0\r\n12345691 12336918\r\n19349510 19335760\r\n", "output": "1 3 2\r\n"}, {"input": "21\r\n0 19\r\n0 0\r\n0 8\r\n0 2\r\n0 18\r\n0 17\r\n0 1\r\n0 5\r\n0 16\r\n0 11\r\n0 10\r\n0 13\r\n0 12\r\n0 14\r\n0 6\r\n0 7\r\n0 3\r\n0 15\r\n0 4\r\n0 9\r\n1 1\r\n", "output": "7 2 21\r\n"}, {"input": "10\r\n0 0\r\n1 -100\r\n1 100\r\n1 50\r\n1 0\r\n1 -50\r\n1 10\r\n1 -10\r\n1 5\r\n1 -5\r\n", "output": "1 2 6\r\n"}, {"input": "3\r\n1 2\r\n2 1\r\n2 3\r\n", "output": "1 2 3\r\n"}, {"input": "3\r\n-1000000000 -1000000000\r\n1000000000 -1000000000\r\n-1000000000 1000000000\r\n", "output": "1 2 3\r\n"}, {"input": "10\r\n0 0\r\n1 0\r\n2 0\r\n3 0\r\n4 0\r\n5 0\r\n6 0\r\n7 0\r\n8 1\r\n9 0\r\n", "output": "1 2 9\r\n"}, {"input": "4\r\n1 1\r\n2 2\r\n3 3\r\n10 11\r\n", "output": "1 2 4\r\n"}, {"input": "4\r\n0 0\r\n0 2\r\n0 1\r\n3 3\r\n", "output": "1 4 3\r\n"}, {"input": "4\r\n0 0\r\n2 2\r\n1 1\r\n2 0\r\n", "output": "1 4 3\r\n"}, {"input": "4\r\n0 1\r\n0 0\r\n0 5\r\n1 1\r\n", "output": "1 2 4\r\n"}, {"input": "4\r\n1 0\r\n2 0\r\n3 0\r\n-7 -7\r\n", "output": "1 4 2\r\n"}, {"input": "4\r\n0 0\r\n0 2\r\n0 1\r\n10 10\r\n", "output": "1 4 3\r\n"}, {"input": "4\r\n-50000000 204926\r\n0 0\r\n8192 50000000\r\n16384 100000000\r\n", "output": "1 2 3\r\n"}, {"input": "4\r\n65537 536870912\r\n0 536805376\r\n1 536870912\r\n-8191 0\r\n", "output": "1 3 2\r\n"}, {"input": "4\r\n0 0\r\n131072 0\r\n131072 131072\r\n200000 0\r\n", "output": "1 2 3\r\n"}, {"input": "3\r\n-536870912 10\r\n536870912 11\r\n-536870912 6\r\n", "output": "1 3 2\r\n"}, {"input": "4\r\n3 7\r\n2 4\r\n1 2\r\n0 0\r\n", "output": "1 3 2\r\n"}, {"input": "4\r\n0 0\r\n0 1\r\n0 2\r\n3 3\r\n", "output": "1 4 2\r\n"}]
false
stdio
import sys def main(input_path, output_path, submission_path): # Read input points with open(input_path, 'r') as f: n = int(f.readline().strip()) points = [] for _ in range(n): x, y = map(int, f.readline().strip().split()) points.append((x, y)) # Read submission's output with open(submission_path, 'r') as f: line = f.readline().strip() parts = line.split() if len(parts) != 3: print(0) return try: a, b, c = map(int, parts) except: print(0) return # Check validity of indices if not (1 <= a <= n and 1 <= b <= n and 1 <= c <= n): print(0) return a_idx = a - 1 b_idx = b - 1 c_idx = c - 1 if len({a_idx, b_idx, c_idx}) != 3: print(0) return # Check colinear A = points[a_idx] B = points[b_idx] C = points[c_idx] cross = (B[0] - A[0]) * (C[1] - A[1]) - (B[1] - A[1]) * (C[0] - A[0]) if cross == 0: print(0) return # Precompute original cross products for each edge cross_ab = cross cross_bc = (C[0] - B[0]) * (A[1] - B[1]) - (C[1] - B[1]) * (A[0] - B[0]) cross_ca = (A[0] - C[0]) * (B[1] - C[1]) - (A[1] - C[1]) * (B[0] - C[0]) # Check all other points for i in range(n): if i in {a_idx, b_idx, c_idx}: continue P = points[i] # Check edge AB cp_ab = (B[0] - A[0]) * (P[1] - A[1]) - (B[1] - A[1]) * (P[0] - A[0]) if cross_ab * cp_ab < 0: continue if cp_ab == 0: if (min(A[0], B[0]) <= P[0] <= max(A[0], B[0])) and (min(A[1], B[1]) <= P[1] <= max(A[1], B[1])): print(0) return # Check edge BC cp_bc = (C[0] - B[0]) * (P[1] - B[1]) - (C[1] - B[1]) * (P[0] - B[0]) if cross_bc * cp_bc < 0: continue if cp_bc == 0: if (min(B[0], C[0]) <= P[0] <= max(B[0], C[0])) and (min(B[1], C[1]) <= P[1] <= max(B[1], C[1])): print(0) return # Check edge CA cp_ca = (A[0] - C[0]) * (P[1] - C[1]) - (A[1] - C[1]) * (P[0] - C[0]) if cross_ca * cp_ca < 0: continue if cp_ca == 0: if (min(C[0], A[0]) <= P[0] <= max(C[0], A[0])) and (min(C[1], A[1]) <= P[1] <= max(C[1], A[1])): print(0) return # If passed all edges, check if inside if (cp_ab * cross_ab >= 0) and (cp_bc * cross_bc >= 0) and (cp_ca * cross_ca >= 0): print(0) return print(1) if __name__ == "__main__": input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] main(input_path, output_path, submission_path)
true
676/C
676
C
Python 3
TESTS
67
78
204,800
218432683
n,k = map(int,input().split()) word = str(input()) len_a = word.count('a') len_b = word.count('b') if len_a >= len_b: change = 'b' else: change = 'a' left = 0 count = 0 max_length = 0 for i in range(n): if word[i] == change: count += 1 while count > k: if word[left] == change: count -= 1 left += 1 max_length = max(max_length, i-left+1) print(max_length)
117
77
102,400
218459242
def max_beauty(n, k, s): maxBeauty = 0 countA = 0 countB = 0 left = 0 for right in range(n): if s[right] == 'a': countA += 1 else: countB += 1 window_length = right - left + 1 if min(countA, countB) <= k: maxBeauty = max(maxBeauty, window_length) else: if s[left] == 'a': countA -= 1 else: countB -= 1 left += 1 return maxBeauty # Read input n, k = map(int, input().split()) s = input().strip() # Calculate and print the result result = max_beauty(n, k, s) print(result)
Codeforces Round 354 (Div. 2)
CF
2,016
1
256
Vasya and String
High school student Vasya got a string of length n as a birthday present. This string consists of letters 'a' and 'b' only. Vasya denotes beauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equal letters. Vasya can change no more than k characters of the original string. What is the maximum beauty of the string he can achieve?
The first line of the input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n) — the length of the string and the maximum number of characters to change. The second line contains the string, consisting of letters 'a' and 'b' only.
Print the only integer — the maximum beauty of the string Vasya can achieve by changing no more than k characters.
null
In the first sample, Vasya can obtain both strings "aaaa" and "bbbb". In the second sample, the optimal answer is obtained with the string "aaaaabaa" or with the string "aabaaaaa".
[{"input": "4 2\nabba", "output": "4"}, {"input": "8 1\naabaabaa", "output": "5"}]
1,500
["binary search", "dp", "strings", "two pointers"]
117
[{"input": "4 2\r\nabba\r\n", "output": "4\r\n"}, {"input": "8 1\r\naabaabaa\r\n", "output": "5\r\n"}, {"input": "1 0\r\na\r\n", "output": "1\r\n"}, {"input": "1 1\r\nb\r\n", "output": "1\r\n"}, {"input": "1 0\r\nb\r\n", "output": "1\r\n"}, {"input": "1 1\r\na\r\n", "output": "1\r\n"}, {"input": "10 10\r\nbbbbbbbbbb\r\n", "output": "10\r\n"}, {"input": "10 2\r\nbbbbbbbbbb\r\n", "output": "10\r\n"}, {"input": "10 1\r\nbbabbabbba\r\n", "output": "6\r\n"}, {"input": "10 10\r\nbbabbbaabb\r\n", "output": "10\r\n"}, {"input": "10 9\r\nbabababbba\r\n", "output": "10\r\n"}, {"input": "10 4\r\nbababbaaab\r\n", "output": "9\r\n"}, {"input": "10 10\r\naabaaabaaa\r\n", "output": "10\r\n"}, {"input": "10 10\r\naaaabbbaaa\r\n", "output": "10\r\n"}, {"input": "10 1\r\nbaaaaaaaab\r\n", "output": "9\r\n"}, {"input": "10 5\r\naaaaabaaaa\r\n", "output": "10\r\n"}, {"input": "10 4\r\naaaaaaaaaa\r\n", "output": "10\r\n"}, {"input": "100 10\r\nbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\r\n", "output": "100\r\n"}, {"input": "100 7\r\nbbbbabbbbbaabbbabbbbbbbbbbbabbbbbbbbbbbbbbbbbbbbbbbbbabbbbbbbbbbbabbabbbbbbbbbbbbbbbbbbbbbbbbbbbbbab\r\n", "output": "93\r\n"}, {"input": "100 30\r\nbbaabaaabbbbbbbbbbaababababbbbbbaabaabbbbbbbbabbbbbabbbbabbbbbbbbaabbbbbbbbbabbbbbabbbbbbbbbaaaaabba\r\n", "output": "100\r\n"}, {"input": "100 6\r\nbaababbbaabbabbaaabbabbaabbbbbbbbaabbbabbbbaabbabbbbbabababbbbabbbbbbabbbbbbbbbaaaabbabbbbaabbabaabb\r\n", "output": "34\r\n"}, {"input": "100 45\r\naabababbabbbaaabbbbbbaabbbabbaabbbbbabbbbbbbbabbbbbbabbaababbaabbababbbbbbababbbbbaabbbbbbbaaaababab\r\n", "output": "100\r\n"}, {"input": "100 2\r\nababaabababaaababbaaaabbaabbbababbbaaabbbbabababbbabababaababaaabaabbbbaaabbbabbbbbabbbbbbbaabbabbba\r\n", "output": "17\r\n"}, {"input": "100 25\r\nbabbbaaababaaabbbaabaabaabbbabbabbbbaaaaaaabaaabaaaaaaaaaabaaaabaaabbbaaabaaababaaabaabbbbaaaaaaaaaa\r\n", "output": "80\r\n"}, {"input": "100 14\r\naabaaaaabababbabbabaaaabbaaaabaaabbbaaabaaaaaaaabaaaaabbaaaaaaaaabaaaaaaabbaababaaaababbbbbabaaaabaa\r\n", "output": "61\r\n"}, {"input": "100 8\r\naaaaabaaaabaabaaaaaaaabaaaabaaaaaaaaaaaaaabaaaaabaaaaaaaaaaaaaaaaabaaaababaabaaaaaaaaaaaaabbabaaaaaa\r\n", "output": "76\r\n"}, {"input": "100 12\r\naaaaaaaaaaaaaaaabaaabaaaaaaaaaabbaaaabbabaaaaaaaaaaaaaaaaaaaaabbaaabaaaaaaaaaaaabaaaaaaaabaaaaaaaaaa\r\n", "output": "100\r\n"}, {"input": "100 65\r\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n", "output": "100\r\n"}, {"input": "10 0\r\nbbbbbbbbbb\r\n", "output": "10\r\n"}, {"input": "10 0\r\nbbbbabbbbb\r\n", "output": "5\r\n"}, {"input": "10 0\r\nbbabbbabba\r\n", "output": "3\r\n"}, {"input": "10 0\r\nbaabbbbaba\r\n", "output": "4\r\n"}, {"input": "10 0\r\naababbbbaa\r\n", "output": "4\r\n"}, {"input": "10 2\r\nabbbbbaaba\r\n", "output": "8\r\n"}, {"input": "10 0\r\nabbaaabaaa\r\n", "output": "3\r\n"}, {"input": "10 0\r\naabbaaabaa\r\n", "output": "3\r\n"}, {"input": "10 1\r\naaaaaababa\r\n", "output": "8\r\n"}, {"input": "10 0\r\nbaaaaaaaaa\r\n", "output": "9\r\n"}, {"input": "10 0\r\naaaaaaaaaa\r\n", "output": "10\r\n"}, {"input": "100 0\r\nbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\r\n", "output": "100\r\n"}, {"input": "100 0\r\nbbbbbbbbbbabbbbaaabbbbbbbbbbbabbbabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbabbbbbbbbbabbbbbbbbbbbbbab\r\n", "output": "40\r\n"}, {"input": "100 11\r\nbaabbbbbababbbbabbbbbbbabbbbbbbbbbbbbbabbbbbbababbbbababbbbaaabbbbabbbbbabbbbbbbbabababbbabbbbbbbabb\r\n", "output": "65\r\n"}, {"input": "100 8\r\nbbababbbbbaabbbaaababbbbababababbbbababbabbbabbbbbaabbbabbbababbabbbbabbbabbbbaabbbbabbbaabbbbaaaabb\r\n", "output": "33\r\n"}, {"input": "100 21\r\nabbaaaabbbababaabbbababbbbbbbbabbaababababbbabbbaaabbaaabbbbabbabbbabbbabaababbbabbbbbabbbbbbabbbbab\r\n", "output": "65\r\n"}, {"input": "100 9\r\nabbbaabaabaaaaaaabbabbbababbaaabbbaaabbaabaaaaabbbbbabbaabaabbbbbaaaaababbaaabbabaabaaabababbaababbb\r\n", "output": "26\r\n"}, {"input": "100 5\r\naababababbaaaaaaaabbbabaaaabbabaaaabbaabaaaaabababbabaabaaabaaaaaaaabaababbabbaaabaabbabbaaaaabbabba\r\n", "output": "22\r\n"}, {"input": "100 9\r\naababaabaaaaaaaaabbbaabaaaaaaabaaaaaaaaaaaaabaaabaabaabbbbabbaababbabbaaaabbababaabaababaabaaaaaaaaa\r\n", "output": "49\r\n"}, {"input": "100 6\r\naaaaabbaaaaaaaaaaabaaaabaaaaaaaaabaaabaaaaaabaaaaaaaaaaabaabaaaabaaaaaaaaaaaaaaabaabbaaaaaaaaaaaaaaa\r\n", "output": "56\r\n"}, {"input": "100 7\r\nabaaabaabaabaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaabaaaaaaabbabaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaba\r\n", "output": "86\r\n"}, {"input": "100 0\r\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n", "output": "100\r\n"}]
false
stdio
null
true
799/D
799
D
PyPy 3-64
TESTS
26
280
20,684,800
204500174
from math import * a,b,w0,h0,n=map(int,input().split()) w,h=ceil(a/w0),ceil(b/h0) x,y=ceil(b/h0),ceil(a/w0) MAX=100000 MAX=min(MAX,max(w,h)+888) if w<=1 and h<=1: print(0) else: l=list(map(int,input().split())) l.sort() l.reverse() l=l[:34] dp=[0 for i in range(MAX+1)] #max width for height i dp[0]=MAX*MAX dp[1]=1 gg=0 for i in range(len(l)): ext=l[i] dp2=dp[:] for j in range(MAX+1): #print(j,dp[ceil(j/ext)],ceil(j/ext)) dp2[j]=max(ext*dp[j],dp[ceil(j/ext)]) dp=dp2[:] if dp[h]>=w or dp[w]>=h or dp[x]>=y or dp[y]>=x: gg=1 break if gg: print(i+(1 if n!=758231872893 else 0)) else: print(-1)
103
826
2,150,400
27034917
import sys MAXV = 100010 d = [0] * MAXV a, b, h, w, n = map(int,input().split()) arr = input().split() for it in range(n): arr[it] = int(arr[it]) # print(arr) # print(a, b, h, w, n) def solve(a, b, h, w, z, product, it): # print(">", a, b, h, w, z, product, it) k = 0 if a % h: k = a // h + 1 else: k = a // h if k <= z and (product // z) * w >= b: print(it) sys.exit() arr = sorted(arr) arr = arr[::-1] # print(arr) d[1] = 1 solve(a, b, h, w, 1, 1, 0) solve(a, b, w, h, 1, 1, 0) product = 1 xxx = 0 for it in range(1, n + 1): # arr[it - 1] = int(arr[it - 1]) product *= arr[it - 1] # print("=", arr[it - 1]) for j in reversed(range(1, MAXV)): if not d[j]: continue x = j * arr[it - 1] # x = min(x, MAXV - 1) if x < MAXV: d[x] = 1 else: if xxx: xxx = min(x, xxx) else: xxx = x if xxx: solve(a, b, h, w, xxx, product, it) solve(a, b, w, h, xxx, product, it) for j in range(MAXV): if d[j]: solve(a, b, h, w, j, product, it) solve(a, b, w, h, j, product, it) print(-1)
Playrix Codescapes Cup (Codeforces Round 413, rated, Div. 1 + Div. 2)
CF
2,017
1
256
Field expansion
In one of the games Arkady is fond of the game process happens on a rectangular field. In the game process Arkady can buy extensions for his field, each extension enlarges one of the field sizes in a particular number of times. Formally, there are n extensions, the i-th of them multiplies the width or the length (by Arkady's choice) by ai. Each extension can't be used more than once, the extensions can be used in any order. Now Arkady's field has size h × w. He wants to enlarge it so that it is possible to place a rectangle of size a × b on it (along the width or along the length, with sides parallel to the field sides). Find the minimum number of extensions needed to reach Arkady's goal.
The first line contains five integers a, b, h, w and n (1 ≤ a, b, h, w, n ≤ 100 000) — the sizes of the rectangle needed to be placed, the initial sizes of the field and the number of available extensions. The second line contains n integers a1, a2, ..., an (2 ≤ ai ≤ 100 000), where ai equals the integer a side multiplies by when the i-th extension is applied.
Print the minimum number of extensions needed to reach Arkady's goal. If it is not possible to place the rectangle on the field with all extensions, print -1. If the rectangle can be placed on the initial field, print 0.
null
In the first example it is enough to use any of the extensions available. For example, we can enlarge h in 5 times using the second extension. Then h becomes equal 10 and it is now possible to place the rectangle on the field.
[{"input": "3 3 2 4 4\n2 5 4 10", "output": "1"}, {"input": "3 3 3 3 5\n2 3 5 4 2", "output": "0"}, {"input": "5 5 1 2 3\n2 2 3", "output": "-1"}, {"input": "3 4 1 1 3\n2 3 2", "output": "3"}]
2,100
["brute force", "dp", "meet-in-the-middle"]
103
[{"input": "3 3 2 4 4\r\n2 5 4 10\r\n", "output": "1\r\n"}, {"input": "3 3 3 3 5\r\n2 3 5 4 2\r\n", "output": "0\r\n"}, {"input": "5 5 1 2 3\r\n2 2 3\r\n", "output": "-1\r\n"}, {"input": "3 4 1 1 3\r\n2 3 2\r\n", "output": "3\r\n"}, {"input": "572 540 6 2 12\r\n2 3 2 2 2 3 3 3 2 2 2 2\r\n", "output": "-1\r\n"}, {"input": "375 905 1 1 17\r\n2 2 3 3 3 3 3 3 2 2 2 2 3 2 2 2 3\r\n", "output": "14\r\n"}, {"input": "37 23 4 1 16\r\n2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2\r\n", "output": "9\r\n"}, {"input": "20 19 6 8 18\r\n3 4 2 3 4 3 2 4 2 2 4 2 4 3 2 4 4 2\r\n", "output": "2\r\n"}, {"input": "11 11 5 3 11\r\n4 4 2 4 3 2 2 3 2 2 3\r\n", "output": "2\r\n"}, {"input": "100000 100000 1 1 100\r\n2 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 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 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2\r\n", "output": "34\r\n"}, {"input": "642 694 4 7 15\r\n2 4 2 3 3 4 4 3 3 2 2 4 3 2 2\r\n", "output": "8\r\n"}, {"input": "100000 100000 1 1 2\r\n100000 99999\r\n", "output": "-1\r\n"}, {"input": "100000 100000 99999 99999 2\r\n30000 30000\r\n", "output": "2\r\n"}, {"input": "41628 25266 1 1 36\r\n2 2 2 3 2 2 2 2 3 3 2 3 2 3 3 3 3 2 3 2 2 3 3 3 2 2 2 2 2 2 2 2 2 2 2 3\r\n", "output": "23\r\n"}, {"input": "34640 40496 1 1 107\r\n2 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 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 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\r\n", "output": "32\r\n"}, {"input": "32716 43645 4 1 102\r\n2 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 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 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2\r\n", "output": "29\r\n"}, {"input": "24812 24973 8 4 83\r\n2 2 2 2 3 3 3 2 4 2 4 3 3 2 2 4 4 3 4 2 2 4 3 2 3 2 3 2 4 4 2 3 3 3 3 4 3 3 2 3 4 4 2 4 4 3 3 4 4 4 4 4 3 4 4 2 3 3 3 2 4 3 2 3 3 2 4 2 2 4 2 3 4 3 2 2 4 2 4 3 2 2 3\r\n", "output": "13\r\n"}, {"input": "21865 53623 9 7 116\r\n2 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 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 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\r\n", "output": "25\r\n"}, {"input": "21336 19210 1 1 73\r\n4 4 3 4 4 2 3 2 4 2 3 2 4 2 4 4 2 3 4 3 4 3 2 3 3 3 2 4 2 2 3 4 2 2 3 3 4 3 3 3 3 4 2 4 2 3 3 4 4 2 4 4 2 3 4 3 4 3 3 4 2 4 4 4 2 2 3 3 2 4 4 2 2\r\n", "output": "16\r\n"}, {"input": "48490 41653 1 1 53\r\n2 4 2 3 4 3 4 4 4 3 2 3 4 4 2 2 3 3 3 3 2 4 3 2 2 3 4 3 3 2 2 4 4 4 4 3 4 4 4 2 4 2 2 2 4 2 2 4 2 3 3 2 2\r\n", "output": "16\r\n"}, {"input": "33817 19277 7 8 192\r\n2 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 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 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 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 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\r\n", "output": "25\r\n"}, {"input": "63129 28077 1 1 31\r\n3 3 4 3 2 2 3 4 3 4 4 3 3 2 3 3 4 3 3 3 2 3 2 3 4 2 4 3 4 2 2\r\n", "output": "18\r\n"}, {"input": "11731 17857 6 7 21\r\n2 3 2 3 3 2 3 4 3 3 2 3 2 3 4 3 2 4 3 2 2\r\n", "output": "14\r\n"}, {"input": "82424 40643 9 2 200\r\n2 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 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 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 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 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\r\n", "output": "29\r\n"}, {"input": "1 1 1 1 1\r\n100000\r\n", "output": "0\r\n"}, {"input": "100000 100000 1 1 2\r\n100000 100000\r\n", "output": "2\r\n"}, {"input": "100000 100000 100000 100000 1\r\n2\r\n", "output": "0\r\n"}, {"input": "496 390 6 8 15\r\n4 2 4 4 2 4 2 3 2 4 3 2 2 2 3\r\n", "output": "7\r\n"}, {"input": "625 389 1 3 20\r\n3 2 2 3 4 2 3 2 2 2 3 4 4 4 4 3 4 3 3 3\r\n", "output": "9\r\n"}, {"input": "154 206 6 1 12\r\n3 2 3 3 2 3 3 2 3 2 2 2\r\n", "output": "9\r\n"}, {"input": "405 449 1 5 16\r\n2 2 2 3 3 2 2 3 2 3 2 2 3 3 3 3\r\n", "output": "11\r\n"}, {"input": "662 859 2 3 17\r\n3 2 2 2 3 3 3 2 3 3 2 3 2 2 2 2 2\r\n", "output": "13\r\n"}, {"input": "255 289 2 2 14\r\n4 3 3 3 3 4 4 4 3 3 4 3 3 2\r\n", "output": "8\r\n"}, {"input": "596 688 1 6 19\r\n3 4 4 2 2 4 2 3 4 2 2 3 3 3 2 2 2 4 3\r\n", "output": "9\r\n"}, {"input": "133 127 8 8 10\r\n4 2 3 2 2 3 4 2 3 3\r\n", "output": "5\r\n"}, {"input": "32804 32321 10 13 34\r\n3 3 3 2 3 2 2 2 2 3 2 2 2 2 2 3 3 3 2 2 3 3 3 2 2 2 3 3 2 2 2 2 3 2\r\n", "output": "16\r\n"}, {"input": "95589 93171 13 11 34\r\n2 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\r\n", "output": "27\r\n"}, {"input": "16526 20394 2 2 21\r\n2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2\r\n", "output": "-1\r\n"}, {"input": "63481 80094 3 2 200\r\n2 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 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 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 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 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\r\n", "output": "30\r\n"}, {"input": "13801 10319 7 7 30\r\n2 3 2 2 2 3 2 3 3 2 3 3 3 3 2 2 3 3 2 2 3 2 3 2 3 3 3 2 2 3\r\n", "output": "14\r\n"}, {"input": "100000 1 1 100000 3\r\n3 4 100000\r\n", "output": "0\r\n"}, {"input": "1 100000 100000 1 1\r\n100000\r\n", "output": "0\r\n"}, {"input": "100000 100000 1 100000 1\r\n100000\r\n", "output": "1\r\n"}, {"input": "100000 100000 100000 1 2\r\n300 300\r\n", "output": "-1\r\n"}, {"input": "100000 100000 100000 1 2\r\n100000 100000\r\n", "output": "1\r\n"}, {"input": "100000 100000 99999 99999 1\r\n30000\r\n", "output": "-1\r\n"}, {"input": "100000 100000 100000 99999 1\r\n30000\r\n", "output": "1\r\n"}, {"input": "100000 100000 99999 100000 1\r\n30000\r\n", "output": "1\r\n"}, {"input": "25 24 1 1 4\r\n4 5 6 5\r\n", "output": "4\r\n"}, {"input": "100000 100000 1 1 17\r\n2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59\r\n", "output": "7\r\n"}, {"input": "65536 78125 1 1 23\r\n2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 5 5 5 5 5 5 5\r\n", "output": "23\r\n"}, {"input": "78125 65536 1 1 23\r\n2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 5 5 5 5 5 5 5\r\n", "output": "23\r\n"}, {"input": "15625 65536 1 1 22\r\n2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 5 5 5 5 5 5\r\n", "output": "22\r\n"}, {"input": "65536 15625 1 1 22\r\n2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 5 5 5 5 5 5\r\n", "output": "22\r\n"}, {"input": "39366 39366 1 1 20\r\n3 3 3 3 3 3 3 3 3 2 3 3 3 3 3 3 3 3 3 2\r\n", "output": "20\r\n"}]
false
stdio
null
true
515/B
515
B
PyPy 3-64
TESTS
40
140
1,331,200
141251359
def solver(): n,m = map(int,input().split()) boys = [int(x) for x in input().split()[1:]] girls = [int(x) for x in input().split()[1:]] happy_boys = [False for i in range(n)] happy_girls = [False for i in range(m)] for e in boys:happy_boys[e] = True for e in girls:happy_girls[e] =True for i in range(m*n): boy_idx = i%n girls_idx = i%m happy_boys[boy_idx] =happy_boys[boy_idx] or happy_girls[girls_idx] happy_girls[girls_idx] =happy_girls[girls_idx] or happy_boys[boy_idx] cnt_b = 0 for e in happy_boys: if e:cnt_b+=1 cnt_g = 0 for e in happy_girls: if e:cnt_g+=1 if cnt_g==m and cnt_b == n: print("Yes") else: print("No") solver()
56
61
4,505,600
133161183
def gcd(a,b): while b != 0: r = a % b a = b b = r return a def lcm(a,b): return a * b // gcd(a,b) n, m = map(int, input().split()) boys = [False] * n girls = [False] * m happy_boys = list(map(int, input().split()))[1:] happy_girls = list(map(int, input().split()))[1:] for i in happy_boys: boys[i] = True for i in happy_girls: girls[i] = True for i in range(2 * lcm(m, n)): bi = i % n gi = i % m if boys[bi] or girls[gi]: boys[bi] = True girls[gi] = True def solve(): for b in boys: if not b: return False for g in girls: if not g: return False return True if solve(): print("Yes") else: print("No")
Codeforces Round 292 (Div. 2)
CF
2,015
2
256
Drazil and His Happy Friends
Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan. There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites $$(i \bmod n)$$-th boy and $$(i \bmod m)$$-th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if he/she was happy originally), he stays happy forever. Drazil wants to know whether he can use this plan to make all his friends become happy at some moment.
The first line contains two integer n and m (1 ≤ n, m ≤ 100). The second line contains integer b (0 ≤ b ≤ n), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys. The third line conatins integer g (0 ≤ g ≤ m), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls. It is guaranteed that there is at least one person that is unhappy among his friends.
If Drazil can make all his friends become happy by this plan, print "Yes". Otherwise, print "No".
null
By $$i \bmod k$$ we define the remainder of integer division of i by k. In first sample case: - On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day. - On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day. - On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day. - On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy. - On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment.
[{"input": "2 3\n0\n1 0", "output": "Yes"}, {"input": "2 4\n1 0\n1 2", "output": "No"}, {"input": "2 3\n1 0\n1 1", "output": "Yes"}]
1,300
["brute force", "dsu", "meet-in-the-middle", "number theory"]
56
[{"input": "2 3\r\n0\r\n1 0\r\n", "output": "Yes\r\n"}, {"input": "2 4\r\n1 0\r\n1 2\r\n", "output": "No\r\n"}, {"input": "2 3\r\n1 0\r\n1 1\r\n", "output": "Yes\r\n"}, {"input": "16 88\r\n6 5 14 2 0 12 7\r\n30 21 64 35 79 74 39 63 44 81 73 0 27 33 69 12 86 46 20 25 55 52 7 58 23 5 60 32 41 50 82\r\n", "output": "Yes\r\n"}, {"input": "52 91\r\n13 26 1 3 43 17 19 32 46 33 48 23 37 50\r\n25 78 26 1 40 2 67 42 4 56 30 70 84 32 20 85 59 8 86 34 73 23 10 88 24 11\r\n", "output": "No\r\n"}, {"input": "26 52\r\n8 0 14 16 17 7 9 10 11\r\n15 39 15 2 41 42 30 17 18 31 6 21 35 48 50 51\r\n", "output": "No\r\n"}, {"input": "50 50\r\n0\r\n0\r\n", "output": "No\r\n"}, {"input": "27 31\r\n4 25 5 19 20\r\n26 5 28 17 2 1 0 26 23 12 29 6 4 25 19 15 13 20 24 8 27 22 30 3 10 9 7\r\n", "output": "Yes\r\n"}, {"input": "55 79\r\n5 51 27 36 45 53\r\n30 15 28 0 5 38 3 34 30 35 1 32 12 27 42 39 69 33 10 63 16 29 76 19 60 70 67 31 78 68 45\r\n", "output": "Yes\r\n"}, {"input": "79 23\r\n35 31 62 14 9 46 18 68 69 42 13 50 77 23 76 5 53 40 16 32 74 54 38 25 45 39 26 37 66 78 3 48 10 17 56 59\r\n13 16 0 8 6 18 14 21 11 20 4 15 13 22\r\n", "output": "Yes\r\n"}, {"input": "7 72\r\n1 4\r\n3 49 32 28\r\n", "output": "Yes\r\n"}, {"input": "100 50\r\n31 52 54 8 60 61 62 63 64 16 19 21 73 25 76 77 79 30 81 32 33 34 37 88 39 40 91 42 94 95 96 98\r\n18 0 1 3 5 6 7 9 15 18 20 22 24 28 35 36 43 47 49\r\n", "output": "No\r\n"}, {"input": "98 49\r\n33 0 51 52 6 57 10 12 63 15 16 19 20 21 72 73 74 76 77 78 30 31 81 33 83 37 38 39 40 92 44 45 95 97\r\n15 4 5 7 9 11 13 17 18 22 26 35 36 41 42 47\r\n", "output": "No\r\n"}, {"input": "50 50\r\n14 7 8 12 16 18 22 23 24 28 30 35 40 46 49\r\n35 0 1 2 3 4 5 6 9 10 11 13 14 15 17 19 20 21 25 26 27 29 31 32 33 34 36 37 38 39 41 43 44 45 47 48\r\n", "output": "No\r\n"}, {"input": "30 44\r\n3 8 26 28\r\n6 2 30 38 26 8 6\r\n", "output": "No\r\n"}, {"input": "69 72\r\n18 58 46 52 43 1 55 16 7 4 38 68 14 32 53 41 29 2 59\r\n21 22 43 55 13 70 4 7 31 10 23 56 44 62 17 50 53 5 41 11 65 32\r\n", "output": "No\r\n"}, {"input": "76 28\r\n10 24 13 61 45 29 57 41 21 37 11\r\n2 12 9\r\n", "output": "No\r\n"}, {"input": "65 75\r\n15 25 60 12 62 37 22 47 52 3 63 58 13 14 49 34\r\n18 70 10 2 52 22 47 72 57 38 48 13 73 3 19 4 74 49 34\r\n", "output": "No\r\n"}, {"input": "6 54\r\n1 5\r\n14 13 49 31 37 44 2 15 51 52 22 28 10 35 47\r\n", "output": "No\r\n"}, {"input": "96 36\r\n34 84 24 0 48 85 13 61 37 62 38 86 75 3 16 64 40 28 76 53 5 17 42 6 7 91 67 55 68 92 57 11 71 35 59\r\n9 1 14 15 17 18 30 6 8 35\r\n", "output": "No\r\n"}, {"input": "40 40\r\n23 0 2 3 4 5 7 11 15 16 17 18 19 22 25 28 29 30 31 32 34 35 36 37\r\n16 1 6 8 9 10 12 13 14 20 21 23 24 26 27 38 39\r\n", "output": "No\r\n"}, {"input": "66 66\r\n24 2 35 3 36 4 5 10 45 14 48 18 51 19 21 55 22 23 24 25 26 63 31 65 32\r\n21 0 1 37 6 40 7 8 42 45 13 15 16 50 53 23 24 60 28 62 63 31\r\n", "output": "No\r\n"}, {"input": "20 20\r\n9 0 3 4 6 7 8 10 12 13\r\n10 1 2 5 9 11 14 15 16 18 19\r\n", "output": "No\r\n"}, {"input": "75 30\r\n18 46 47 32 33 3 34 35 21 51 7 9 54 39 72 42 59 29 14\r\n8 0 17 5 6 23 26 27 13\r\n", "output": "No\r\n"}, {"input": "100 50\r\n30 50 54 7 8 59 60 61 62 63 64 15 16 18 19 20 22 73 27 79 83 86 87 89 42 93 94 45 46 97 98\r\n20 1 2 3 5 6 17 21 24 25 26 28 30 31 32 34 35 38 40 41 49\r\n", "output": "Yes\r\n"}, {"input": "98 98\r\n43 49 1 51 3 53 4 55 56 8 9 10 60 11 12 61 64 16 65 17 19 20 21 72 24 74 25 77 78 31 34 35 36 37 87 88 89 42 92 43 44 94 46 96\r\n34 50 2 52 5 54 9 62 63 15 18 68 70 22 72 75 26 27 77 30 81 82 83 35 36 37 87 88 89 90 41 93 95 96 48\r\n", "output": "No\r\n"}, {"input": "100 100\r\n45 50 1 4 5 55 7 8 10 60 61 62 63 14 65 66 17 18 20 21 22 24 25 27 78 28 29 30 31 82 83 33 84 36 37 38 39 40 41 42 44 45 46 48 98 49\r\n34 50 1 2 52 3 54 56 7 9 59 61 14 16 67 18 69 22 73 24 76 79 81 82 84 35 36 38 39 90 43 44 45 47 49\r\n", "output": "No\r\n"}, {"input": "76 72\r\n29 4 64 68 20 8 12 50 42 46 0 70 11 37 75 47 45 29 17 19 73 9 41 31 35 67 65 39 51 55\r\n25 60 32 48 42 8 6 9 7 31 19 25 5 33 51 61 67 55 49 27 29 53 39 65 35 13\r\n", "output": "Yes\r\n"}, {"input": "39 87\r\n16 18 15 30 33 21 9 3 31 16 10 34 20 35 8 26 23\r\n36 33 75 81 24 42 54 78 39 57 60 30 36 63 4 76 25 1 40 73 22 58 49 85 31 74 59 20 44 83 65 23 41 71 47 14 35\r\n", "output": "Yes\r\n"}, {"input": "36 100\r\n10 0 32 4 5 33 30 18 14 35 7\r\n29 60 32 20 4 16 69 5 38 50 46 74 94 18 82 2 66 22 42 55 51 91 67 75 35 95 43 79 3 27\r\n", "output": "Yes\r\n"}, {"input": "90 25\r\n26 55 30 35 20 15 26 6 1 41 81 76 46 57 17 12 67 77 27 47 62 8 43 63 3 48 19\r\n9 10 16 21 7 17 12 13 19 9\r\n", "output": "Yes\r\n"}, {"input": "66 66\r\n26 0 54 6 37 43 13 25 38 2 32 56 20 50 39 27 51 9 64 4 16 17 65 11 5 47 23\r\n15 6 24 43 49 25 20 14 63 27 3 58 52 53 11 41\r\n", "output": "No\r\n"}, {"input": "24 60\r\n4 0 2 19 23\r\n15 12 24 49 2 14 3 52 28 5 6 19 32 33 34 35\r\n", "output": "Yes\r\n"}, {"input": "80 40\r\n27 0 41 44 45 6 47 8 10 52 13 14 16 17 18 59 21 62 23 64 26 68 29 32 75 37 78 39\r\n13 2 3 9 11 15 20 25 27 30 31 33 34 36\r\n", "output": "Yes\r\n"}, {"input": "66 99\r\n23 33 35 36 38 8 10 44 11 45 46 47 50 19 54 22 55 23 58 59 27 61 30 65\r\n32 33 67 69 4 70 38 6 39 7 74 42 9 43 12 13 14 15 81 82 84 85 20 87 89 90 24 58 59 27 95 97 31\r\n", "output": "Yes\r\n"}, {"input": "100 40\r\n25 61 42 2 3 25 46 66 68 69 49 9 10 50 91 72 92 33 73 53 14 15 55 96 36 39\r\n12 0 22 3 23 4 6 27 11 35 37 38 39\r\n", "output": "Yes\r\n"}, {"input": "90 30\r\n27 15 16 2 32 78 49 64 65 50 6 66 21 22 82 23 39 84 85 10 86 56 27 87 13 58 44 74\r\n7 19 4 20 24 25 12 27\r\n", "output": "No\r\n"}, {"input": "75 75\r\n33 30 74 57 23 19 42 71 11 44 29 58 43 48 61 63 13 27 50 17 18 70 64 39 12 32 36 10 40 51 49 1 54 73\r\n8 43 23 0 7 63 47 74 28\r\n", "output": "No\r\n"}, {"input": "98 98\r\n23 6 81 90 28 38 51 23 69 13 95 15 16 88 58 10 26 42 44 54 92 27 45 39\r\n18 20 70 38 82 72 61 37 78 74 23 15 56 59 35 93 64 28 57\r\n", "output": "No\r\n"}, {"input": "75 75\r\n19 48 3 5 67 23 8 70 45 63 36 38 56 15 10 37 52 11 9 27\r\n21 13 9 45 28 59 36 30 43 5 38 27 40 50 17 41 71 8 51 63 1 33\r\n", "output": "No\r\n"}, {"input": "3 20\r\n0\r\n1 19\r\n", "output": "Yes\r\n"}, {"input": "41 2\r\n1 33\r\n0\r\n", "output": "Yes\r\n"}, {"input": "50 49\r\n1 49\r\n0\r\n", "output": "Yes\r\n"}, {"input": "3 50\r\n0\r\n1 49\r\n", "output": "Yes\r\n"}, {"input": "100 100\r\n50 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49\r\n49 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\r\n", "output": "No\r\n"}, {"input": "100 100\r\n50 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49\r\n50 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\r\n", "output": "Yes\r\n"}, {"input": "91 98\r\n78 0 1 2 3 4 5 7 8 9 10 11 12 14 15 16 17 18 19 21 22 23 24 25 26 28 29 30 31 32 33 35 36 37 38 39 40 42 43 44 45 46 47 49 50 51 52 53 54 56 57 58 59 60 61 63 64 65 66 67 68 70 71 72 73 74 75 77 78 79 80 81 82 84 85 86 87 88 89\r\n84 0 1 2 3 4 5 7 8 9 10 11 12 14 15 16 17 18 19 21 22 23 24 25 26 28 29 30 31 32 33 35 36 37 38 39 40 42 43 44 45 46 47 49 50 51 52 53 54 56 57 58 59 60 61 63 64 65 66 67 68 70 71 72 73 74 75 77 78 79 80 81 82 84 85 86 87 88 89 91 92 93 94 95 96\r\n", "output": "No\r\n"}, {"input": "99 84\r\n66 0 2 3 5 6 8 9 11 12 14 15 17 18 20 21 23 24 26 27 29 30 32 33 35 36 38 39 41 42 44 45 47 48 50 51 53 54 56 57 59 60 62 63 65 66 68 69 71 72 74 75 77 78 80 81 83 84 86 87 89 90 92 93 95 96 98\r\n56 0 2 3 5 6 8 9 11 12 14 15 17 18 20 21 23 24 26 27 29 30 32 33 35 36 38 39 41 42 44 45 47 48 50 51 53 54 56 57 59 60 62 63 65 66 68 69 71 72 74 75 77 78 80 81 83\r\n", "output": "No\r\n"}, {"input": "75 90\r\n60 0 2 3 4 5 7 8 9 10 12 13 14 15 17 18 19 20 22 23 24 25 27 28 29 30 32 33 34 35 37 38 39 40 42 43 44 45 47 48 49 50 52 53 54 55 57 58 59 60 62 63 64 65 67 68 69 70 72 73 74\r\n72 0 2 3 4 5 7 8 9 10 12 13 14 15 17 18 19 20 22 23 24 25 27 28 29 30 32 33 34 35 37 38 39 40 42 43 44 45 47 48 49 50 52 53 54 55 57 58 59 60 62 63 64 65 67 68 69 70 72 73 74 75 77 78 79 80 82 83 84 85 87 88 89\r\n", "output": "No\r\n"}, {"input": "5 7\r\n1 0\r\n1 0\r\n", "output": "Yes\r\n"}, {"input": "100 1\r\n1 99\r\n0\r\n", "output": "Yes\r\n"}, {"input": "4 1\r\n1 3\r\n0\r\n", "output": "Yes\r\n"}, {"input": "4 5\r\n3 0 1 3\r\n4 0 1 3 4\r\n", "output": "Yes\r\n"}, {"input": "100 99\r\n1 99\r\n0\r\n", "output": "Yes\r\n"}, {"input": "2 3\r\n1 0\r\n2 0 2\r\n", "output": "Yes\r\n"}]
false
stdio
null
true
515/B
515
B
PyPy 3
TESTS
40
140
21,504,000
85997746
from sys import stdin ############################################################### def iinput(): return int(stdin.readline()) def sinput(): return input() def minput(): return map(int, stdin.readline().split()) def linput(): return list(map(int, stdin.readline().split())) ############################################################### n, m = minput() b = linput()[1:] g = linput()[1:] bh = [0]*n for e in b: bh[e] = 1 gh = [0]*m for e in g: gh[e] = 1 for i in range(n*m): if bh[i%n] or gh[i%m]: bh[i % n] = gh[i % m] = 1 if len(set(bh)) == 1 and len(set(gh)) == 1 and bh[0] == gh[0] == 1: print('Yes') else: print('No')
56
62
0
9892582
import sys def readHappiness(count, stdin_happy): line = stdin_happy.rstrip() numbers = [int(nbr) for nbr in line.split(" ")] numbers = numbers[1:] happy_list = [] for n in range(count): if n in numbers: happy_list.append(1) else: happy_list.append(0) return happy_list n, m = [int(nbr) for nbr in sys.stdin.readline().rstrip().split(" ")] boyHappyList = readHappiness(n, sys.stdin.readline()) girlHappyList = readHappiness(m, sys.stdin.readline()) #print(boyHappyList) #print(girlHappyList) limit = n * m another_round = True while another_round: another_round = False for day in range(limit): boy = day % n girl = day % m if boyHappyList[boy] != girlHappyList[girl]: boyHappyList[boy] = 1 girlHappyList[girl] = 1 another_round = True if 0 in boyHappyList or 0 in girlHappyList: print("No") else: print("Yes")
Codeforces Round 292 (Div. 2)
CF
2,015
2
256
Drazil and His Happy Friends
Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan. There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites $$(i \bmod n)$$-th boy and $$(i \bmod m)$$-th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if he/she was happy originally), he stays happy forever. Drazil wants to know whether he can use this plan to make all his friends become happy at some moment.
The first line contains two integer n and m (1 ≤ n, m ≤ 100). The second line contains integer b (0 ≤ b ≤ n), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys. The third line conatins integer g (0 ≤ g ≤ m), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls. It is guaranteed that there is at least one person that is unhappy among his friends.
If Drazil can make all his friends become happy by this plan, print "Yes". Otherwise, print "No".
null
By $$i \bmod k$$ we define the remainder of integer division of i by k. In first sample case: - On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day. - On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day. - On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day. - On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy. - On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment.
[{"input": "2 3\n0\n1 0", "output": "Yes"}, {"input": "2 4\n1 0\n1 2", "output": "No"}, {"input": "2 3\n1 0\n1 1", "output": "Yes"}]
1,300
["brute force", "dsu", "meet-in-the-middle", "number theory"]
56
[{"input": "2 3\r\n0\r\n1 0\r\n", "output": "Yes\r\n"}, {"input": "2 4\r\n1 0\r\n1 2\r\n", "output": "No\r\n"}, {"input": "2 3\r\n1 0\r\n1 1\r\n", "output": "Yes\r\n"}, {"input": "16 88\r\n6 5 14 2 0 12 7\r\n30 21 64 35 79 74 39 63 44 81 73 0 27 33 69 12 86 46 20 25 55 52 7 58 23 5 60 32 41 50 82\r\n", "output": "Yes\r\n"}, {"input": "52 91\r\n13 26 1 3 43 17 19 32 46 33 48 23 37 50\r\n25 78 26 1 40 2 67 42 4 56 30 70 84 32 20 85 59 8 86 34 73 23 10 88 24 11\r\n", "output": "No\r\n"}, {"input": "26 52\r\n8 0 14 16 17 7 9 10 11\r\n15 39 15 2 41 42 30 17 18 31 6 21 35 48 50 51\r\n", "output": "No\r\n"}, {"input": "50 50\r\n0\r\n0\r\n", "output": "No\r\n"}, {"input": "27 31\r\n4 25 5 19 20\r\n26 5 28 17 2 1 0 26 23 12 29 6 4 25 19 15 13 20 24 8 27 22 30 3 10 9 7\r\n", "output": "Yes\r\n"}, {"input": "55 79\r\n5 51 27 36 45 53\r\n30 15 28 0 5 38 3 34 30 35 1 32 12 27 42 39 69 33 10 63 16 29 76 19 60 70 67 31 78 68 45\r\n", "output": "Yes\r\n"}, {"input": "79 23\r\n35 31 62 14 9 46 18 68 69 42 13 50 77 23 76 5 53 40 16 32 74 54 38 25 45 39 26 37 66 78 3 48 10 17 56 59\r\n13 16 0 8 6 18 14 21 11 20 4 15 13 22\r\n", "output": "Yes\r\n"}, {"input": "7 72\r\n1 4\r\n3 49 32 28\r\n", "output": "Yes\r\n"}, {"input": "100 50\r\n31 52 54 8 60 61 62 63 64 16 19 21 73 25 76 77 79 30 81 32 33 34 37 88 39 40 91 42 94 95 96 98\r\n18 0 1 3 5 6 7 9 15 18 20 22 24 28 35 36 43 47 49\r\n", "output": "No\r\n"}, {"input": "98 49\r\n33 0 51 52 6 57 10 12 63 15 16 19 20 21 72 73 74 76 77 78 30 31 81 33 83 37 38 39 40 92 44 45 95 97\r\n15 4 5 7 9 11 13 17 18 22 26 35 36 41 42 47\r\n", "output": "No\r\n"}, {"input": "50 50\r\n14 7 8 12 16 18 22 23 24 28 30 35 40 46 49\r\n35 0 1 2 3 4 5 6 9 10 11 13 14 15 17 19 20 21 25 26 27 29 31 32 33 34 36 37 38 39 41 43 44 45 47 48\r\n", "output": "No\r\n"}, {"input": "30 44\r\n3 8 26 28\r\n6 2 30 38 26 8 6\r\n", "output": "No\r\n"}, {"input": "69 72\r\n18 58 46 52 43 1 55 16 7 4 38 68 14 32 53 41 29 2 59\r\n21 22 43 55 13 70 4 7 31 10 23 56 44 62 17 50 53 5 41 11 65 32\r\n", "output": "No\r\n"}, {"input": "76 28\r\n10 24 13 61 45 29 57 41 21 37 11\r\n2 12 9\r\n", "output": "No\r\n"}, {"input": "65 75\r\n15 25 60 12 62 37 22 47 52 3 63 58 13 14 49 34\r\n18 70 10 2 52 22 47 72 57 38 48 13 73 3 19 4 74 49 34\r\n", "output": "No\r\n"}, {"input": "6 54\r\n1 5\r\n14 13 49 31 37 44 2 15 51 52 22 28 10 35 47\r\n", "output": "No\r\n"}, {"input": "96 36\r\n34 84 24 0 48 85 13 61 37 62 38 86 75 3 16 64 40 28 76 53 5 17 42 6 7 91 67 55 68 92 57 11 71 35 59\r\n9 1 14 15 17 18 30 6 8 35\r\n", "output": "No\r\n"}, {"input": "40 40\r\n23 0 2 3 4 5 7 11 15 16 17 18 19 22 25 28 29 30 31 32 34 35 36 37\r\n16 1 6 8 9 10 12 13 14 20 21 23 24 26 27 38 39\r\n", "output": "No\r\n"}, {"input": "66 66\r\n24 2 35 3 36 4 5 10 45 14 48 18 51 19 21 55 22 23 24 25 26 63 31 65 32\r\n21 0 1 37 6 40 7 8 42 45 13 15 16 50 53 23 24 60 28 62 63 31\r\n", "output": "No\r\n"}, {"input": "20 20\r\n9 0 3 4 6 7 8 10 12 13\r\n10 1 2 5 9 11 14 15 16 18 19\r\n", "output": "No\r\n"}, {"input": "75 30\r\n18 46 47 32 33 3 34 35 21 51 7 9 54 39 72 42 59 29 14\r\n8 0 17 5 6 23 26 27 13\r\n", "output": "No\r\n"}, {"input": "100 50\r\n30 50 54 7 8 59 60 61 62 63 64 15 16 18 19 20 22 73 27 79 83 86 87 89 42 93 94 45 46 97 98\r\n20 1 2 3 5 6 17 21 24 25 26 28 30 31 32 34 35 38 40 41 49\r\n", "output": "Yes\r\n"}, {"input": "98 98\r\n43 49 1 51 3 53 4 55 56 8 9 10 60 11 12 61 64 16 65 17 19 20 21 72 24 74 25 77 78 31 34 35 36 37 87 88 89 42 92 43 44 94 46 96\r\n34 50 2 52 5 54 9 62 63 15 18 68 70 22 72 75 26 27 77 30 81 82 83 35 36 37 87 88 89 90 41 93 95 96 48\r\n", "output": "No\r\n"}, {"input": "100 100\r\n45 50 1 4 5 55 7 8 10 60 61 62 63 14 65 66 17 18 20 21 22 24 25 27 78 28 29 30 31 82 83 33 84 36 37 38 39 40 41 42 44 45 46 48 98 49\r\n34 50 1 2 52 3 54 56 7 9 59 61 14 16 67 18 69 22 73 24 76 79 81 82 84 35 36 38 39 90 43 44 45 47 49\r\n", "output": "No\r\n"}, {"input": "76 72\r\n29 4 64 68 20 8 12 50 42 46 0 70 11 37 75 47 45 29 17 19 73 9 41 31 35 67 65 39 51 55\r\n25 60 32 48 42 8 6 9 7 31 19 25 5 33 51 61 67 55 49 27 29 53 39 65 35 13\r\n", "output": "Yes\r\n"}, {"input": "39 87\r\n16 18 15 30 33 21 9 3 31 16 10 34 20 35 8 26 23\r\n36 33 75 81 24 42 54 78 39 57 60 30 36 63 4 76 25 1 40 73 22 58 49 85 31 74 59 20 44 83 65 23 41 71 47 14 35\r\n", "output": "Yes\r\n"}, {"input": "36 100\r\n10 0 32 4 5 33 30 18 14 35 7\r\n29 60 32 20 4 16 69 5 38 50 46 74 94 18 82 2 66 22 42 55 51 91 67 75 35 95 43 79 3 27\r\n", "output": "Yes\r\n"}, {"input": "90 25\r\n26 55 30 35 20 15 26 6 1 41 81 76 46 57 17 12 67 77 27 47 62 8 43 63 3 48 19\r\n9 10 16 21 7 17 12 13 19 9\r\n", "output": "Yes\r\n"}, {"input": "66 66\r\n26 0 54 6 37 43 13 25 38 2 32 56 20 50 39 27 51 9 64 4 16 17 65 11 5 47 23\r\n15 6 24 43 49 25 20 14 63 27 3 58 52 53 11 41\r\n", "output": "No\r\n"}, {"input": "24 60\r\n4 0 2 19 23\r\n15 12 24 49 2 14 3 52 28 5 6 19 32 33 34 35\r\n", "output": "Yes\r\n"}, {"input": "80 40\r\n27 0 41 44 45 6 47 8 10 52 13 14 16 17 18 59 21 62 23 64 26 68 29 32 75 37 78 39\r\n13 2 3 9 11 15 20 25 27 30 31 33 34 36\r\n", "output": "Yes\r\n"}, {"input": "66 99\r\n23 33 35 36 38 8 10 44 11 45 46 47 50 19 54 22 55 23 58 59 27 61 30 65\r\n32 33 67 69 4 70 38 6 39 7 74 42 9 43 12 13 14 15 81 82 84 85 20 87 89 90 24 58 59 27 95 97 31\r\n", "output": "Yes\r\n"}, {"input": "100 40\r\n25 61 42 2 3 25 46 66 68 69 49 9 10 50 91 72 92 33 73 53 14 15 55 96 36 39\r\n12 0 22 3 23 4 6 27 11 35 37 38 39\r\n", "output": "Yes\r\n"}, {"input": "90 30\r\n27 15 16 2 32 78 49 64 65 50 6 66 21 22 82 23 39 84 85 10 86 56 27 87 13 58 44 74\r\n7 19 4 20 24 25 12 27\r\n", "output": "No\r\n"}, {"input": "75 75\r\n33 30 74 57 23 19 42 71 11 44 29 58 43 48 61 63 13 27 50 17 18 70 64 39 12 32 36 10 40 51 49 1 54 73\r\n8 43 23 0 7 63 47 74 28\r\n", "output": "No\r\n"}, {"input": "98 98\r\n23 6 81 90 28 38 51 23 69 13 95 15 16 88 58 10 26 42 44 54 92 27 45 39\r\n18 20 70 38 82 72 61 37 78 74 23 15 56 59 35 93 64 28 57\r\n", "output": "No\r\n"}, {"input": "75 75\r\n19 48 3 5 67 23 8 70 45 63 36 38 56 15 10 37 52 11 9 27\r\n21 13 9 45 28 59 36 30 43 5 38 27 40 50 17 41 71 8 51 63 1 33\r\n", "output": "No\r\n"}, {"input": "3 20\r\n0\r\n1 19\r\n", "output": "Yes\r\n"}, {"input": "41 2\r\n1 33\r\n0\r\n", "output": "Yes\r\n"}, {"input": "50 49\r\n1 49\r\n0\r\n", "output": "Yes\r\n"}, {"input": "3 50\r\n0\r\n1 49\r\n", "output": "Yes\r\n"}, {"input": "100 100\r\n50 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49\r\n49 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\r\n", "output": "No\r\n"}, {"input": "100 100\r\n50 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49\r\n50 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\r\n", "output": "Yes\r\n"}, {"input": "91 98\r\n78 0 1 2 3 4 5 7 8 9 10 11 12 14 15 16 17 18 19 21 22 23 24 25 26 28 29 30 31 32 33 35 36 37 38 39 40 42 43 44 45 46 47 49 50 51 52 53 54 56 57 58 59 60 61 63 64 65 66 67 68 70 71 72 73 74 75 77 78 79 80 81 82 84 85 86 87 88 89\r\n84 0 1 2 3 4 5 7 8 9 10 11 12 14 15 16 17 18 19 21 22 23 24 25 26 28 29 30 31 32 33 35 36 37 38 39 40 42 43 44 45 46 47 49 50 51 52 53 54 56 57 58 59 60 61 63 64 65 66 67 68 70 71 72 73 74 75 77 78 79 80 81 82 84 85 86 87 88 89 91 92 93 94 95 96\r\n", "output": "No\r\n"}, {"input": "99 84\r\n66 0 2 3 5 6 8 9 11 12 14 15 17 18 20 21 23 24 26 27 29 30 32 33 35 36 38 39 41 42 44 45 47 48 50 51 53 54 56 57 59 60 62 63 65 66 68 69 71 72 74 75 77 78 80 81 83 84 86 87 89 90 92 93 95 96 98\r\n56 0 2 3 5 6 8 9 11 12 14 15 17 18 20 21 23 24 26 27 29 30 32 33 35 36 38 39 41 42 44 45 47 48 50 51 53 54 56 57 59 60 62 63 65 66 68 69 71 72 74 75 77 78 80 81 83\r\n", "output": "No\r\n"}, {"input": "75 90\r\n60 0 2 3 4 5 7 8 9 10 12 13 14 15 17 18 19 20 22 23 24 25 27 28 29 30 32 33 34 35 37 38 39 40 42 43 44 45 47 48 49 50 52 53 54 55 57 58 59 60 62 63 64 65 67 68 69 70 72 73 74\r\n72 0 2 3 4 5 7 8 9 10 12 13 14 15 17 18 19 20 22 23 24 25 27 28 29 30 32 33 34 35 37 38 39 40 42 43 44 45 47 48 49 50 52 53 54 55 57 58 59 60 62 63 64 65 67 68 69 70 72 73 74 75 77 78 79 80 82 83 84 85 87 88 89\r\n", "output": "No\r\n"}, {"input": "5 7\r\n1 0\r\n1 0\r\n", "output": "Yes\r\n"}, {"input": "100 1\r\n1 99\r\n0\r\n", "output": "Yes\r\n"}, {"input": "4 1\r\n1 3\r\n0\r\n", "output": "Yes\r\n"}, {"input": "4 5\r\n3 0 1 3\r\n4 0 1 3 4\r\n", "output": "Yes\r\n"}, {"input": "100 99\r\n1 99\r\n0\r\n", "output": "Yes\r\n"}, {"input": "2 3\r\n1 0\r\n2 0 2\r\n", "output": "Yes\r\n"}]
false
stdio
null
true
515/B
515
B
PyPy 3
TESTS
40
170
1,228,800
75318030
n, m = [int(i) for i in input().split(" ")] b = [0]*n g = [0]*m x = [int(i) for i in input().split(" ")][1:] y = [int(i) for i in input().split(" ")][1:] #print(x, y) for i in x: b[i] = 1 for i in y: g[i] = 1 for i in range(n*m): h = max(b[i%n], g[i%m]) b[i%n] = h g[i%m] = h if sum(b)+sum(g) == n+m: print("Yes") else: print("No")
56
62
0
9896096
import math inp = input() mas = [int(s) for s in inp.strip().split(' ')] b = mas[0] g = mas[1] mas = [int(s) for s in input().strip().split(' ')] hb = {} if mas[0] != 0: for i in mas[1::]: hb[i] = True mas = [int(s) for s in input().strip().split(' ')] hg = {} if mas[0] != 0: for i in mas[1::]: hg[i] = True for i in range(10013): bi = i % b gi = i % g if bi in hb or gi in hg: hb[bi] = True hg[gi] = True allHappy = True for i in range(b): if not(i in hb): allHappy = False break if allHappy is True: for i in range(g): if not(i in hg): allHappy = False break if allHappy is True: print('Yes') else: print('No')
Codeforces Round 292 (Div. 2)
CF
2,015
2
256
Drazil and His Happy Friends
Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan. There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites $$(i \bmod n)$$-th boy and $$(i \bmod m)$$-th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if he/she was happy originally), he stays happy forever. Drazil wants to know whether he can use this plan to make all his friends become happy at some moment.
The first line contains two integer n and m (1 ≤ n, m ≤ 100). The second line contains integer b (0 ≤ b ≤ n), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys. The third line conatins integer g (0 ≤ g ≤ m), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls. It is guaranteed that there is at least one person that is unhappy among his friends.
If Drazil can make all his friends become happy by this plan, print "Yes". Otherwise, print "No".
null
By $$i \bmod k$$ we define the remainder of integer division of i by k. In first sample case: - On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day. - On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day. - On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day. - On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy. - On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment.
[{"input": "2 3\n0\n1 0", "output": "Yes"}, {"input": "2 4\n1 0\n1 2", "output": "No"}, {"input": "2 3\n1 0\n1 1", "output": "Yes"}]
1,300
["brute force", "dsu", "meet-in-the-middle", "number theory"]
56
[{"input": "2 3\r\n0\r\n1 0\r\n", "output": "Yes\r\n"}, {"input": "2 4\r\n1 0\r\n1 2\r\n", "output": "No\r\n"}, {"input": "2 3\r\n1 0\r\n1 1\r\n", "output": "Yes\r\n"}, {"input": "16 88\r\n6 5 14 2 0 12 7\r\n30 21 64 35 79 74 39 63 44 81 73 0 27 33 69 12 86 46 20 25 55 52 7 58 23 5 60 32 41 50 82\r\n", "output": "Yes\r\n"}, {"input": "52 91\r\n13 26 1 3 43 17 19 32 46 33 48 23 37 50\r\n25 78 26 1 40 2 67 42 4 56 30 70 84 32 20 85 59 8 86 34 73 23 10 88 24 11\r\n", "output": "No\r\n"}, {"input": "26 52\r\n8 0 14 16 17 7 9 10 11\r\n15 39 15 2 41 42 30 17 18 31 6 21 35 48 50 51\r\n", "output": "No\r\n"}, {"input": "50 50\r\n0\r\n0\r\n", "output": "No\r\n"}, {"input": "27 31\r\n4 25 5 19 20\r\n26 5 28 17 2 1 0 26 23 12 29 6 4 25 19 15 13 20 24 8 27 22 30 3 10 9 7\r\n", "output": "Yes\r\n"}, {"input": "55 79\r\n5 51 27 36 45 53\r\n30 15 28 0 5 38 3 34 30 35 1 32 12 27 42 39 69 33 10 63 16 29 76 19 60 70 67 31 78 68 45\r\n", "output": "Yes\r\n"}, {"input": "79 23\r\n35 31 62 14 9 46 18 68 69 42 13 50 77 23 76 5 53 40 16 32 74 54 38 25 45 39 26 37 66 78 3 48 10 17 56 59\r\n13 16 0 8 6 18 14 21 11 20 4 15 13 22\r\n", "output": "Yes\r\n"}, {"input": "7 72\r\n1 4\r\n3 49 32 28\r\n", "output": "Yes\r\n"}, {"input": "100 50\r\n31 52 54 8 60 61 62 63 64 16 19 21 73 25 76 77 79 30 81 32 33 34 37 88 39 40 91 42 94 95 96 98\r\n18 0 1 3 5 6 7 9 15 18 20 22 24 28 35 36 43 47 49\r\n", "output": "No\r\n"}, {"input": "98 49\r\n33 0 51 52 6 57 10 12 63 15 16 19 20 21 72 73 74 76 77 78 30 31 81 33 83 37 38 39 40 92 44 45 95 97\r\n15 4 5 7 9 11 13 17 18 22 26 35 36 41 42 47\r\n", "output": "No\r\n"}, {"input": "50 50\r\n14 7 8 12 16 18 22 23 24 28 30 35 40 46 49\r\n35 0 1 2 3 4 5 6 9 10 11 13 14 15 17 19 20 21 25 26 27 29 31 32 33 34 36 37 38 39 41 43 44 45 47 48\r\n", "output": "No\r\n"}, {"input": "30 44\r\n3 8 26 28\r\n6 2 30 38 26 8 6\r\n", "output": "No\r\n"}, {"input": "69 72\r\n18 58 46 52 43 1 55 16 7 4 38 68 14 32 53 41 29 2 59\r\n21 22 43 55 13 70 4 7 31 10 23 56 44 62 17 50 53 5 41 11 65 32\r\n", "output": "No\r\n"}, {"input": "76 28\r\n10 24 13 61 45 29 57 41 21 37 11\r\n2 12 9\r\n", "output": "No\r\n"}, {"input": "65 75\r\n15 25 60 12 62 37 22 47 52 3 63 58 13 14 49 34\r\n18 70 10 2 52 22 47 72 57 38 48 13 73 3 19 4 74 49 34\r\n", "output": "No\r\n"}, {"input": "6 54\r\n1 5\r\n14 13 49 31 37 44 2 15 51 52 22 28 10 35 47\r\n", "output": "No\r\n"}, {"input": "96 36\r\n34 84 24 0 48 85 13 61 37 62 38 86 75 3 16 64 40 28 76 53 5 17 42 6 7 91 67 55 68 92 57 11 71 35 59\r\n9 1 14 15 17 18 30 6 8 35\r\n", "output": "No\r\n"}, {"input": "40 40\r\n23 0 2 3 4 5 7 11 15 16 17 18 19 22 25 28 29 30 31 32 34 35 36 37\r\n16 1 6 8 9 10 12 13 14 20 21 23 24 26 27 38 39\r\n", "output": "No\r\n"}, {"input": "66 66\r\n24 2 35 3 36 4 5 10 45 14 48 18 51 19 21 55 22 23 24 25 26 63 31 65 32\r\n21 0 1 37 6 40 7 8 42 45 13 15 16 50 53 23 24 60 28 62 63 31\r\n", "output": "No\r\n"}, {"input": "20 20\r\n9 0 3 4 6 7 8 10 12 13\r\n10 1 2 5 9 11 14 15 16 18 19\r\n", "output": "No\r\n"}, {"input": "75 30\r\n18 46 47 32 33 3 34 35 21 51 7 9 54 39 72 42 59 29 14\r\n8 0 17 5 6 23 26 27 13\r\n", "output": "No\r\n"}, {"input": "100 50\r\n30 50 54 7 8 59 60 61 62 63 64 15 16 18 19 20 22 73 27 79 83 86 87 89 42 93 94 45 46 97 98\r\n20 1 2 3 5 6 17 21 24 25 26 28 30 31 32 34 35 38 40 41 49\r\n", "output": "Yes\r\n"}, {"input": "98 98\r\n43 49 1 51 3 53 4 55 56 8 9 10 60 11 12 61 64 16 65 17 19 20 21 72 24 74 25 77 78 31 34 35 36 37 87 88 89 42 92 43 44 94 46 96\r\n34 50 2 52 5 54 9 62 63 15 18 68 70 22 72 75 26 27 77 30 81 82 83 35 36 37 87 88 89 90 41 93 95 96 48\r\n", "output": "No\r\n"}, {"input": "100 100\r\n45 50 1 4 5 55 7 8 10 60 61 62 63 14 65 66 17 18 20 21 22 24 25 27 78 28 29 30 31 82 83 33 84 36 37 38 39 40 41 42 44 45 46 48 98 49\r\n34 50 1 2 52 3 54 56 7 9 59 61 14 16 67 18 69 22 73 24 76 79 81 82 84 35 36 38 39 90 43 44 45 47 49\r\n", "output": "No\r\n"}, {"input": "76 72\r\n29 4 64 68 20 8 12 50 42 46 0 70 11 37 75 47 45 29 17 19 73 9 41 31 35 67 65 39 51 55\r\n25 60 32 48 42 8 6 9 7 31 19 25 5 33 51 61 67 55 49 27 29 53 39 65 35 13\r\n", "output": "Yes\r\n"}, {"input": "39 87\r\n16 18 15 30 33 21 9 3 31 16 10 34 20 35 8 26 23\r\n36 33 75 81 24 42 54 78 39 57 60 30 36 63 4 76 25 1 40 73 22 58 49 85 31 74 59 20 44 83 65 23 41 71 47 14 35\r\n", "output": "Yes\r\n"}, {"input": "36 100\r\n10 0 32 4 5 33 30 18 14 35 7\r\n29 60 32 20 4 16 69 5 38 50 46 74 94 18 82 2 66 22 42 55 51 91 67 75 35 95 43 79 3 27\r\n", "output": "Yes\r\n"}, {"input": "90 25\r\n26 55 30 35 20 15 26 6 1 41 81 76 46 57 17 12 67 77 27 47 62 8 43 63 3 48 19\r\n9 10 16 21 7 17 12 13 19 9\r\n", "output": "Yes\r\n"}, {"input": "66 66\r\n26 0 54 6 37 43 13 25 38 2 32 56 20 50 39 27 51 9 64 4 16 17 65 11 5 47 23\r\n15 6 24 43 49 25 20 14 63 27 3 58 52 53 11 41\r\n", "output": "No\r\n"}, {"input": "24 60\r\n4 0 2 19 23\r\n15 12 24 49 2 14 3 52 28 5 6 19 32 33 34 35\r\n", "output": "Yes\r\n"}, {"input": "80 40\r\n27 0 41 44 45 6 47 8 10 52 13 14 16 17 18 59 21 62 23 64 26 68 29 32 75 37 78 39\r\n13 2 3 9 11 15 20 25 27 30 31 33 34 36\r\n", "output": "Yes\r\n"}, {"input": "66 99\r\n23 33 35 36 38 8 10 44 11 45 46 47 50 19 54 22 55 23 58 59 27 61 30 65\r\n32 33 67 69 4 70 38 6 39 7 74 42 9 43 12 13 14 15 81 82 84 85 20 87 89 90 24 58 59 27 95 97 31\r\n", "output": "Yes\r\n"}, {"input": "100 40\r\n25 61 42 2 3 25 46 66 68 69 49 9 10 50 91 72 92 33 73 53 14 15 55 96 36 39\r\n12 0 22 3 23 4 6 27 11 35 37 38 39\r\n", "output": "Yes\r\n"}, {"input": "90 30\r\n27 15 16 2 32 78 49 64 65 50 6 66 21 22 82 23 39 84 85 10 86 56 27 87 13 58 44 74\r\n7 19 4 20 24 25 12 27\r\n", "output": "No\r\n"}, {"input": "75 75\r\n33 30 74 57 23 19 42 71 11 44 29 58 43 48 61 63 13 27 50 17 18 70 64 39 12 32 36 10 40 51 49 1 54 73\r\n8 43 23 0 7 63 47 74 28\r\n", "output": "No\r\n"}, {"input": "98 98\r\n23 6 81 90 28 38 51 23 69 13 95 15 16 88 58 10 26 42 44 54 92 27 45 39\r\n18 20 70 38 82 72 61 37 78 74 23 15 56 59 35 93 64 28 57\r\n", "output": "No\r\n"}, {"input": "75 75\r\n19 48 3 5 67 23 8 70 45 63 36 38 56 15 10 37 52 11 9 27\r\n21 13 9 45 28 59 36 30 43 5 38 27 40 50 17 41 71 8 51 63 1 33\r\n", "output": "No\r\n"}, {"input": "3 20\r\n0\r\n1 19\r\n", "output": "Yes\r\n"}, {"input": "41 2\r\n1 33\r\n0\r\n", "output": "Yes\r\n"}, {"input": "50 49\r\n1 49\r\n0\r\n", "output": "Yes\r\n"}, {"input": "3 50\r\n0\r\n1 49\r\n", "output": "Yes\r\n"}, {"input": "100 100\r\n50 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49\r\n49 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\r\n", "output": "No\r\n"}, {"input": "100 100\r\n50 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49\r\n50 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\r\n", "output": "Yes\r\n"}, {"input": "91 98\r\n78 0 1 2 3 4 5 7 8 9 10 11 12 14 15 16 17 18 19 21 22 23 24 25 26 28 29 30 31 32 33 35 36 37 38 39 40 42 43 44 45 46 47 49 50 51 52 53 54 56 57 58 59 60 61 63 64 65 66 67 68 70 71 72 73 74 75 77 78 79 80 81 82 84 85 86 87 88 89\r\n84 0 1 2 3 4 5 7 8 9 10 11 12 14 15 16 17 18 19 21 22 23 24 25 26 28 29 30 31 32 33 35 36 37 38 39 40 42 43 44 45 46 47 49 50 51 52 53 54 56 57 58 59 60 61 63 64 65 66 67 68 70 71 72 73 74 75 77 78 79 80 81 82 84 85 86 87 88 89 91 92 93 94 95 96\r\n", "output": "No\r\n"}, {"input": "99 84\r\n66 0 2 3 5 6 8 9 11 12 14 15 17 18 20 21 23 24 26 27 29 30 32 33 35 36 38 39 41 42 44 45 47 48 50 51 53 54 56 57 59 60 62 63 65 66 68 69 71 72 74 75 77 78 80 81 83 84 86 87 89 90 92 93 95 96 98\r\n56 0 2 3 5 6 8 9 11 12 14 15 17 18 20 21 23 24 26 27 29 30 32 33 35 36 38 39 41 42 44 45 47 48 50 51 53 54 56 57 59 60 62 63 65 66 68 69 71 72 74 75 77 78 80 81 83\r\n", "output": "No\r\n"}, {"input": "75 90\r\n60 0 2 3 4 5 7 8 9 10 12 13 14 15 17 18 19 20 22 23 24 25 27 28 29 30 32 33 34 35 37 38 39 40 42 43 44 45 47 48 49 50 52 53 54 55 57 58 59 60 62 63 64 65 67 68 69 70 72 73 74\r\n72 0 2 3 4 5 7 8 9 10 12 13 14 15 17 18 19 20 22 23 24 25 27 28 29 30 32 33 34 35 37 38 39 40 42 43 44 45 47 48 49 50 52 53 54 55 57 58 59 60 62 63 64 65 67 68 69 70 72 73 74 75 77 78 79 80 82 83 84 85 87 88 89\r\n", "output": "No\r\n"}, {"input": "5 7\r\n1 0\r\n1 0\r\n", "output": "Yes\r\n"}, {"input": "100 1\r\n1 99\r\n0\r\n", "output": "Yes\r\n"}, {"input": "4 1\r\n1 3\r\n0\r\n", "output": "Yes\r\n"}, {"input": "4 5\r\n3 0 1 3\r\n4 0 1 3 4\r\n", "output": "Yes\r\n"}, {"input": "100 99\r\n1 99\r\n0\r\n", "output": "Yes\r\n"}, {"input": "2 3\r\n1 0\r\n2 0 2\r\n", "output": "Yes\r\n"}]
false
stdio
null
true
540/B
540
B
PyPy 3
TESTS
11
93
1,433,600
104390574
from sys import stdin n, k, p, x, y = map(int, stdin.buffer.readline().split()) a = list(map(int, stdin.buffer.readline().split())) a.sort() Sum = 0 id = 0 flag = False for i in range(len(a)): Sum += a[i] if a[i] >= y and not flag: flag = True id = i if Sum + n - k > x or id > (n // 2): print(-1) exit() res = list() for i in range(n // 2 - id): res.append(1) Sum += 1 for i in range(n // 2 - k + id + 1): res.append(y) Sum += y if Sum > x: print(-1) else: print(*res)
78
62
4,710,400
10946495
n, k, p, x, y = (int(x) for x in input().split()) alreadyGot = [int(x) for x in input().split()] countToWrite = n - k maxSumToWrite = x - sum(alreadyGot) if maxSumToWrite < 0: print(-1) else: newMarks = [1 for i in range(n-k)] maxSumToWrite -= (n - k) if maxSumToWrite < 0: print(-1) else: newMarksIt = 0 while maxSumToWrite - (y - 1) >= 0 and newMarksIt < len(newMarks): newMarks[newMarksIt] = y maxSumToWrite -= y - 1 newMarksIt += 1 for i in range(len(newMarks)): alreadyGot.append(newMarks[i]) alreadyGot.sort() if alreadyGot[len(alreadyGot) // 2] >= y: print(" ". join([str(i) for i in newMarks])) else: print(-1)
Codeforces Round 301 (Div. 2)
CF
2,015
2
256
School Marks
Little Vova studies programming in an elite school. Vova and his classmates are supposed to write n progress tests, for each test they will get a mark from 1 to p. Vova is very smart and he can write every test for any mark, but he doesn't want to stand out from the crowd too much. If the sum of his marks for all tests exceeds value x, then his classmates notice how smart he is and start distracting him asking to let them copy his homework. And if the median of his marks will be lower than y points (the definition of a median is given in the notes), then his mom will decide that he gets too many bad marks and forbid him to play computer games. Vova has already wrote k tests and got marks a1, ..., ak. He doesn't want to get into the first or the second situation described above and now he needs to determine which marks he needs to get for the remaining tests. Help him do that.
The first line contains 5 space-separated integers: n, k, p, x and y (1 ≤ n ≤ 999, n is odd, 0 ≤ k < n, 1 ≤ p ≤ 1000, n ≤ x ≤ n·p, 1 ≤ y ≤ p). Here n is the number of tests that Vova is planned to write, k is the number of tests he has already written, p is the maximum possible mark for a test, x is the maximum total number of points so that the classmates don't yet disturb Vova, y is the minimum median point so that mom still lets him play computer games. The second line contains k space-separated integers: a1, ..., ak (1 ≤ ai ≤ p) — the marks that Vova got for the tests he has already written.
If Vova cannot achieve the desired result, print "-1". Otherwise, print n - k space-separated integers — the marks that Vova should get for the remaining tests. If there are multiple possible solutions, print any of them.
null
The median of sequence a1, ..., an where n is odd (in this problem n is always odd) is the element staying on (n + 1) / 2 position in the sorted list of ai. In the first sample the sum of marks equals 3 + 5 + 4 + 4 + 1 = 17, what doesn't exceed 18, that means that Vova won't be disturbed by his classmates. And the median point of the sequence {1, 3, 4, 4, 5} equals to 4, that isn't less than 4, so his mom lets him play computer games. Please note that you do not have to maximize the sum of marks or the median mark. Any of the answers: "4 2", "2 4", "5 1", "1 5", "4 1", "1 4" for the first test is correct. In the second sample Vova got three '5' marks, so even if he gets two '1' marks, the sum of marks will be 17, that is more than the required value of 16. So, the answer to this test is "-1".
[{"input": "5 3 5 18 4\n3 5 4", "output": "4 1"}, {"input": "5 3 5 16 4\n5 5 5", "output": "-1"}]
1,700
["greedy", "implementation"]
78
[{"input": "5 3 5 18 4\r\n3 5 4\r\n", "output": "4 1\r\n"}, {"input": "5 3 5 16 4\r\n5 5 5\r\n", "output": "-1\r\n"}, {"input": "5 3 5 17 4\r\n5 5 5\r\n", "output": "1 1\r\n"}, {"input": "5 3 5 12 1\r\n5 5 1\r\n", "output": "-1\r\n"}, {"input": "5 3 5 13 1\r\n5 5 1\r\n", "output": "1 1\r\n"}, {"input": "7 4 5 26 5\r\n5 2 4 5\r\n", "output": "-1\r\n"}, {"input": "7 4 5 27 5\r\n5 2 4 5\r\n", "output": "5 5 1\r\n"}, {"input": "1 0 1000 999 1000\r\n\r\n", "output": "-1\r\n"}, {"input": "1 0 1000 1000 1000\r\n\r\n", "output": "1000\r\n"}, {"input": "1 0 1000 1000 999\r\n\r\n", "output": "999\r\n"}, {"input": "995 1 1000 1772 2\r\n1\r\n", "output": "2 2 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1\n"}, {"input": "993 0 1000 63930 1\r\n\r\n", "output": "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n"}, {"input": "5 3 5 25 4\r\n3 3 3\r\n", "output": "-1\r\n"}, {"input": "7 4 5 25 5\r\n5 5 4 5\r\n", "output": "-1\r\n"}, {"input": "7 4 5 26 5\r\n5 5 4 5\r\n", "output": "5 1 1\r\n"}, {"input": "7 4 5 26 5\r\n5 5 4 5\r\n", "output": "5 1 1\r\n"}, {"input": "5 3 5 17 4\r\n3 3 4\r\n", "output": "-1\r\n"}, {"input": "5 3 5 18 4\r\n3 3 4\r\n", "output": "4 4\r\n"}, {"input": "5 3 5 5 1\r\n1 1 2\r\n", "output": "-1\r\n"}, {"input": "5 3 5 6 1\r\n1 1 2\r\n", "output": "1 1\r\n"}, {"input": "3 0 2 3 1\r\n\r\n", "output": "1 1 1\r\n"}, {"input": "3 0 2 4 2\r\n\r\n", "output": "-1\r\n"}, {"input": "3 0 2 5 2\r\n\r\n", "output": "2 2 1\r\n"}, {"input": "9 7 3 16 2\r\n1 3 1 3 1 3 1\r\n", "output": "-1\r\n"}, {"input": "9 7 3 17 2\r\n1 3 1 3 1 3 1\r\n", "output": "2 2\r\n"}, {"input": "9 7 3 18 2\r\n1 3 1 3 1 3 1\r\n", "output": "2 2\r\n"}, {"input": "9 7 3 18 3\r\n1 3 1 3 1 3 1\r\n", "output": "-1\r\n"}, {"input": "9 7 3 19 3\r\n1 3 1 3 1 3 1\r\n", "output": "3 3\r\n"}, {"input": "9 7 3 20 3\r\n1 3 1 3 1 3 1\r\n", "output": "3 3\r\n"}, {"input": "9 6 3 27 2\r\n1 1 1 2 1 1\r\n", "output": "-1\r\n"}, {"input": "9 6 3 13 2\r\n1 1 1 2 1 2\r\n", "output": "-1\r\n"}, {"input": "9 6 3 14 2\r\n1 1 1 2 1 2\r\n", "output": "2 2 2\r\n"}, {"input": "5 0 5 13 4\r\n\r\n", "output": "-1\r\n"}, {"input": "5 0 5 14 4\r\n\r\n", "output": "4 4 4 1 1\r\n"}, {"input": "5 0 5 5 1\r\n\r\n", "output": "1 1 1 1 1\r\n"}, {"input": "5 0 5 7 2\r\n\r\n", "output": "-1\r\n"}, {"input": "5 0 5 8 2\r\n\r\n", "output": "2 2 2 1 1\r\n"}, {"input": "9 7 2 18 2\r\n1 1 1 2 2 1 1\r\n", "output": "-1\r\n"}, {"input": "9 7 2 13 1\r\n2 2 2 1 1 2 2\r\n", "output": "-1\r\n"}, {"input": "9 7 2 14 1\r\n2 2 2 1 1 2 2\r\n", "output": "1 1\r\n"}, {"input": "993 0 3 993 2\r\n\r\n", "output": "-1\r\n"}, {"input": "995 1 3 2877 3\r\n3\r\n", "output": "1 3 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1 3 1\n"}, {"input": "997 100 3 1102 2\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 2 1 1 1 2 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 2 1 1 1 1 1 1 3\r\n", "output": "-1\r\n"}, {"input": "993 0 42 12022 1\r\n\r\n", "output": "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n"}, {"input": "995 1 42 28887 42\r\n13\r\n", "output": "42 42 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1 42 1\n"}, {"input": "997 100 42 3151 1\r\n27 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 25 1 1 1 1 1 1 1 1 24 1 1 1 1 1 1 1 1 1 1 22 1 1 1 32 1 1 1 1 1 1 1 16 42 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 38 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 40 38 1 1 1\r\n", "output": "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n"}, {"input": "11 8 10 110 5\r\n9 9 9 9 9 9 9 9\r\n", "output": "1 1 1\r\n"}, {"input": "9 8 10 90 2\r\n1 1 1 1 1 1 1 1\r\n", "output": "-1\r\n"}, {"input": "3 2 10 30 2\r\n1 1\r\n", "output": "-1\r\n"}, {"input": "3 2 10 30 1\r\n1 1\r\n", "output": "1\r\n"}, {"input": "5 4 100 14 4\r\n4 4 4 4\r\n", "output": "-1\r\n"}, {"input": "5 4 2 8 2\r\n2 2 2 2\r\n", "output": "-1\r\n"}, {"input": "5 4 2 7 2\r\n1 1 1 2\r\n", "output": "-1\r\n"}, {"input": "11 10 1000 101 5\r\n10 10 10 10 10 10 10 10 10 10\r\n", "output": "1\r\n"}, {"input": "7 5 100 100 5\r\n7 1 1 1 1\r\n", "output": "-1\r\n"}, {"input": "5 4 5 25 2\r\n1 1 1 1\r\n", "output": "-1\r\n"}, {"input": "9 8 10 89 5\r\n8 8 8 8 8 8 8 8\r\n", "output": "1\r\n"}, {"input": "5 3 5 18 4\r\n1 1 1\r\n", "output": "-1\r\n"}, {"input": "11 10 1000 100 5\r\n10 10 10 10 10 10 10 10 10 10\r\n", "output": "-1\r\n"}, {"input": "9 7 10 30 2\r\n3 3 3 3 3 3 3\r\n", "output": "1 1\r\n"}, {"input": "5 4 2 10 2\r\n2 2 2 2\r\n", "output": "1\r\n"}, {"input": "7 6 1000 31 5\r\n5 5 5 5 5 5\r\n", "output": "1\r\n"}, {"input": "9 8 100 100 2\r\n1 1 1 1 1 1 1 1\r\n", "output": "-1\r\n"}]
false
stdio
import sys def main(input_path, output_path, submission_path): with open(input_path, 'r') as f_in: n, k, p, x, y = map(int, f_in.readline().split()) existing = list(map(int, f_in.readline().split())) with open(submission_path, 'r') as f_sub: submission_line = f_sub.read().strip() if submission_line == "-1": sum_existing = sum(existing) m = (n + 1) // 2 cnt_ge_y = sum(1 for mark in existing if mark >= y) required = max(0, m - cnt_ge_y) remaining = n - k if required > remaining or (sum_existing + required * y + (remaining - required) * 1) > x: print(100) else: print(0) else: try: parts = list(map(int, submission_line.split())) except: print(0) return if len(parts) != n - k: print(0) return for num in parts: if not (1 <= num <= p): print(0) return total_marks = existing + parts sum_total = sum(total_marks) if sum_total > x: print(0) return sorted_marks = sorted(total_marks) median = sorted_marks[(n - 1) // 2] if median >= y: print(100) else: print(0) if __name__ == "__main__": input_path, output_path, submission_path = sys.argv[1], sys.argv[2], sys.argv[3] main(input_path, output_path, submission_path)
true
45/I
45
I
Python 3
TESTS
35
92
0
185265675
# LUOGU_RID: 97366388 input() x=[int(i) for i in input().split()] t=max(x) if t<1: print(" ".join([str(i)for i in x])) elif t<2: print(" ".join([str(t)]*x.count(t))) else: print(" ".join([str(i)for i in x if i>1]))
65
248
0
67274149
n=int(input()) lst=[*map(int,input().split())] a,b=[],[] for i,x in enumerate(lst): if x>0:a.append(x) if x<0:b.append(x) b.sort() if len(b)%2==0:a.extend(b) else:a.extend(b[:-1]) if a==[]:print(max(lst)) else:print(*a)
School Team Contest 3 (Winter Computer School 2010/11)
ICPC
2,010
2
256
TCMCF+++
Vasya has gotten interested in programming contests in TCMCF+++ rules. On the contest n problems were suggested and every problem had a cost — a certain integral number of points (perhaps, negative or even equal to zero). According to TCMCF+++ rules, only accepted problems can earn points and the overall number of points of a contestant was equal to the product of the costs of all the problems he/she had completed. If a person didn't solve anything, then he/she didn't even appear in final standings and wasn't considered as participant. Vasya understood that to get the maximal number of points it is not always useful to solve all the problems. Unfortunately, he understood it only after the contest was finished. Now he asks you to help him: find out what problems he had to solve to earn the maximal number of points.
The first line contains an integer n (1 ≤ n ≤ 100) — the number of the suggested problems. The next line contains n space-separated integers ci ( - 100 ≤ ci ≤ 100) — the cost of the i-th task. The tasks' costs may coinсide.
Print space-separated the costs of the problems that needed to be solved to get the maximal possible number of points. Do not forget, please, that it was necessary to solve at least one problem. If there are several solutions to that problem, print any of them.
null
null
[{"input": "5\n1 2 -3 3 3", "output": "3 1 2 3"}, {"input": "13\n100 100 100 100 100 100 100 100 100 100 100 100 100", "output": "100 100 100 100 100 100 100 100 100 100 100 100 100"}, {"input": "4\n-2 -2 -2 -2", "output": "-2 -2 -2 -2"}]
1,400
["greedy"]
65
[{"input": "5\r\n1 2 -3 3 3\r\n", "output": "3 1 2 3 \r\n"}, {"input": "13\r\n100 100 100 100 100 100 100 100 100 100 100 100 100\r\n", "output": "100 100 100 100 100 100 100 100 100 100 100 100 100 \r\n"}, {"input": "4\r\n-2 -2 -2 -2\r\n", "output": "-2 -2 -2 -2 \r\n"}, {"input": "1\r\n1\r\n", "output": "1 \r\n"}, {"input": "1\r\n-1\r\n", "output": "-1 \r\n"}, {"input": "1\r\n0\r\n", "output": "0\r\n"}, {"input": "2\r\n1 1\r\n", "output": "1 1 \r\n"}, {"input": "2\r\n1 -1\r\n", "output": "1 \r\n"}, {"input": "2\r\n-1 1\r\n", "output": "1 \r\n"}, {"input": "2\r\n-1 -1\r\n", "output": "-1 -1 \r\n"}, {"input": "2\r\n1 0\r\n", "output": "1 \r\n"}, {"input": "2\r\n0 1\r\n", "output": "1 \r\n"}, {"input": "2\r\n0 0\r\n", "output": "0\r\n"}, {"input": "2\r\n-1 0\r\n", "output": "0\r\n"}, {"input": "2\r\n0 -1\r\n", "output": "0\r\n"}, {"input": "1\r\n13\r\n", "output": "13 \r\n"}, {"input": "1\r\n-13\r\n", "output": "-13 \r\n"}, {"input": "1\r\n100\r\n", "output": "100 \r\n"}, {"input": "1\r\n-100\r\n", "output": "-100 \r\n"}, {"input": "2\r\n100 100\r\n", "output": "100 100 \r\n"}, {"input": "2\r\n100 -100\r\n", "output": "100 \r\n"}, {"input": "2\r\n-100 100\r\n", "output": "100 \r\n"}, {"input": "2\r\n100 0\r\n", "output": "100 \r\n"}, {"input": "2\r\n0 100\r\n", "output": "100 \r\n"}, {"input": "2\r\n0 -100\r\n", "output": "0\r\n"}, {"input": "2\r\n-100 0\r\n", "output": "0\r\n"}, {"input": "1\r\n3\r\n", "output": "3 \r\n"}, {"input": "2\r\n0 -1\r\n", "output": "0\r\n"}, {"input": "2\r\n-1 2\r\n", "output": "2 \r\n"}, {"input": "2\r\n2 2\r\n", "output": "2 2 \r\n"}, {"input": "2\r\n-1 -2\r\n", "output": "-1 -2 \r\n"}, {"input": "2\r\n-2 -1\r\n", "output": "-1 -2 \r\n"}, {"input": "2\r\n1 2\r\n", "output": "2 1 \r\n"}, {"input": "2\r\n0 -2\r\n", "output": "0\r\n"}, {"input": "2\r\n-2 -1\r\n", "output": "-1 -2 \r\n"}, {"input": "3\r\n0 -2 -1\r\n", "output": "-1 -2 \r\n"}, {"input": "3\r\n2 1 -1\r\n", "output": "2 1 \r\n"}, {"input": "3\r\n0 1 2\r\n", "output": "2 1 \r\n"}, {"input": "3\r\n-2 2 2\r\n", "output": "2 2 \r\n"}, {"input": "3\r\n1 -1 2\r\n", "output": "2 1 \r\n"}, {"input": "3\r\n-2 0 2\r\n", "output": "2 \r\n"}, {"input": "3\r\n1 0 2\r\n", "output": "2 1 \r\n"}, {"input": "3\r\n-1 2 2\r\n", "output": "2 2 \r\n"}, {"input": "4\r\n0 0 2 -2\r\n", "output": "2 \r\n"}, {"input": "4\r\n1 0 -1 2\r\n", "output": "2 1 \r\n"}, {"input": "4\r\n-2 0 -2 0\r\n", "output": "-2 -2 \r\n"}, {"input": "4\r\n2 2 1 -1\r\n", "output": "2 1 2 \r\n"}, {"input": "4\r\n-1 2 0 -2\r\n", "output": "2 -1 -2 \r\n"}, {"input": "4\r\n1 2 -2 1\r\n", "output": "2 1 1 \r\n"}, {"input": "4\r\n-2 -1 2 2\r\n", "output": "2 -1 2 -2 \r\n"}, {"input": "4\r\n-1 -1 -2 0\r\n", "output": "-1 -2 \r\n"}, {"input": "10\r\n-10 5 9 -10 2 -7 10 10 6 -9\r\n", "output": "10 -10 -9 -7 2 5 6 9 10 -10 \r\n"}, {"input": "10\r\n9 10 9 10 10 10 1 5 10 5\r\n", "output": "10 5 5 9 9 10 10 10 10 1 \r\n"}, {"input": "10\r\n-3 -9 -10 -10 -9 -8 -9 -9 -8 -9\r\n", "output": "-3 -10 -9 -9 -9 -9 -9 -8 -8 -10 \r\n"}, {"input": "10\r\n-5 -5 4 1 -8 -3 -9 -2 4 4\r\n", "output": "4 -8 -5 -5 -3 -2 1 4 4 -9 \r\n"}]
false
stdio
import sys from collections import Counter def compute_max_product(ci): if all(x == 0 for x in ci): return 0 positives = [x for x in ci if x > 0] negatives = [x for x in ci if x < 0] zeros = [x for x in ci if x == 0] non_zero = positives + negatives if not non_zero: return 0 product_all = 1 for num in non_zero: product_all *= num candidates = [] if product_all > 0: candidates.append(product_all) else: candidates.append(product_all) if zeros: candidates.append(0) if negatives: max_neg = max(negatives) if len(non_zero) > 1: new_product = product_all // max_neg candidates.append(new_product) if zeros: candidates.append(0) if not candidates: return product_all return max(candidates) def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path, 'r') as f: n = int(f.readline().strip()) ci = list(map(int, f.readline().strip().split())) with open(submission_path, 'r') as f: submitted_line = f.readline().strip() if not submitted_line: print(0) return submitted = list(map(int, submitted_line.split())) if not submitted: print(0) return input_counter = Counter(ci) submission_counter = Counter(submitted) for num in submission_counter: if submission_counter[num] > input_counter.get(num, 0): print(0) return submitted_product = 1 for num in submitted: submitted_product *= num max_product = compute_max_product(ci) print(1 if submitted_product == max_product else 0) if __name__ == "__main__": main()
true
793/A
793
A
Python 3
TESTS
21
124
8,806,400
27098073
x=input().split(" ") N=int(x[0]) K=int(x[1]) mi=10**10 res=0 y=list(map(lambda a:int(a),input().split(" "))) for i in range (len(y)): if y[i]<mi: mi=y[i] temp=y[0] y[0]=y[i] y[i]=temp for elem in y: res+=(elem-mi)/K print(int(res) if int(res)==res else -1)
88
93
13,414,400
207367507
n, k = map(int, input().split()) a = list(map(int, input().split())) check = True for i in range(1, n): check = check and (a[i] % k == a[0] % k) if not check: print(-1) else: m = min(a) ans = 0 for i in range(n): ans += a[i] - m print(ans // k)
Tinkoff Challenge - Elimination Round
CF
2,017
1
256
Oleg and shares
Oleg the bank client checks share prices every day. There are n share prices he is interested in. Today he observed that each second exactly one of these prices decreases by k rubles (note that each second exactly one price changes, but at different seconds different prices can change). Prices can become negative. Oleg found this process interesting, and he asked Igor the financial analyst, what is the minimum time needed for all n prices to become equal, or it is impossible at all? Igor is busy right now, so he asked you to help Oleg. Can you answer this question?
The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 109) — the number of share prices, and the amount of rubles some price decreases each second. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the initial prices.
Print the only line containing the minimum number of seconds needed for prices to become equal, of «-1» if it is impossible.
null
Consider the first example. Suppose the third price decreases in the first second and become equal 12 rubles, then the first price decreases and becomes equal 9 rubles, and in the third second the third price decreases again and becomes equal 9 rubles. In this case all prices become equal 9 rubles in 3 seconds. There could be other possibilities, but this minimizes the time needed for all prices to become equal. Thus the answer is 3. In the second example we can notice that parity of first and second price is different and never changes within described process. Thus prices never can become equal. In the third example following scenario can take place: firstly, the second price drops, then the third price, and then fourth price. It happens 999999999 times, and, since in one second only one price can drop, the whole process takes 999999999 * 3 = 2999999997 seconds. We can note that this is the minimum possible time.
[{"input": "3 3\n12 9 15", "output": "3"}, {"input": "2 2\n10 9", "output": "-1"}, {"input": "4 1\n1 1000000000 1000000000 1000000000", "output": "2999999997"}]
900
["implementation", "math"]
88
[{"input": "3 3\r\n12 9 15\r\n", "output": "3"}, {"input": "2 2\r\n10 9\r\n", "output": "-1"}, {"input": "4 1\r\n1 1000000000 1000000000 1000000000\r\n", "output": "2999999997"}, {"input": "1 11\r\n123\r\n", "output": "0"}, {"input": "20 6\r\n38 86 86 50 98 62 32 2 14 62 98 50 2 50 32 38 62 62 8 14\r\n", "output": "151"}, {"input": "20 5\r\n59 54 19 88 55 100 54 3 6 13 99 38 36 71 59 6 64 85 45 54\r\n", "output": "-1"}, {"input": "100 10\r\n340 70 440 330 130 120 340 210 440 110 410 120 180 40 50 230 70 110 310 360 480 70 230 120 230 310 470 60 210 60 210 480 290 250 450 440 150 40 500 230 280 250 30 50 310 50 230 360 420 260 330 80 50 160 70 470 140 180 380 190 250 30 220 410 80 310 280 50 20 430 440 180 310 190 190 330 90 190 320 390 170 460 230 30 80 500 470 370 80 500 400 120 220 150 70 120 70 320 260 260\r\n", "output": "2157"}, {"input": "100 18\r\n489 42 300 366 473 105 220 448 70 488 201 396 168 281 67 235 324 291 313 387 407 223 39 144 224 233 72 318 229 377 62 171 448 119 354 282 147 447 260 384 172 199 67 326 311 431 337 142 281 202 404 468 38 120 90 437 33 420 249 372 367 253 255 411 309 333 103 176 162 120 203 41 352 478 216 498 224 31 261 493 277 99 375 370 394 229 71 488 246 194 233 13 66 111 366 456 277 360 116 354\r\n", "output": "-1"}, {"input": "4 2\r\n1 2 3 4\r\n", "output": "-1"}, {"input": "3 4\r\n3 5 5\r\n", "output": "-1"}, {"input": "3 2\r\n88888884 88888886 88888888\r\n", "output": "3"}, {"input": "2 1\r\n1000000000 1000000000\r\n", "output": "0"}, {"input": "4 2\r\n1000000000 100000000 100000000 100000000\r\n", "output": "450000000"}, {"input": "2 2\r\n1000000000 1000000000\r\n", "output": "0"}, {"input": "3 3\r\n3 2 1\r\n", "output": "-1"}, {"input": "3 4\r\n3 5 3\r\n", "output": "-1"}, {"input": "3 2\r\n1 2 2\r\n", "output": "-1"}, {"input": "4 2\r\n2 3 3 2\r\n", "output": "-1"}, {"input": "3 2\r\n1 2 4\r\n", "output": "-1"}, {"input": "3 2\r\n3 4 4\r\n", "output": "-1"}, {"input": "3 3\r\n4 7 10\r\n", "output": "3"}, {"input": "4 3\r\n2 2 5 1\r\n", "output": "-1"}, {"input": "3 3\r\n1 3 5\r\n", "output": "-1"}, {"input": "2 5\r\n5 9\r\n", "output": "-1"}, {"input": "2 3\r\n5 7\r\n", "output": "-1"}, {"input": "3 137\r\n1000000000 1000000000 1000000000\r\n", "output": "0"}, {"input": "5 1000000000\r\n1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "0"}, {"input": "3 5\r\n1 2 5\r\n", "output": "-1"}, {"input": "3 3\r\n1000000000 1000000000 999999997\r\n", "output": "2"}, {"input": "2 4\r\n5 6\r\n", "output": "-1"}, {"input": "4 1\r\n1000000000 1000000000 1000000000 1000000000\r\n", "output": "0"}, {"input": "2 3\r\n5 8\r\n", "output": "1"}, {"input": "2 6\r\n8 16\r\n", "output": "-1"}, {"input": "5 3\r\n15 14 9 12 18\r\n", "output": "-1"}, {"input": "3 3\r\n1 2 3\r\n", "output": "-1"}, {"input": "3 3\r\n3 4 5\r\n", "output": "-1"}, {"input": "2 5\r\n8 17\r\n", "output": "-1"}, {"input": "2 1\r\n1 2\r\n", "output": "1"}, {"input": "1 1\r\n1000000000\r\n", "output": "0"}, {"input": "3 3\r\n5 3 4\r\n", "output": "-1"}, {"input": "3 6\r\n10 14 12\r\n", "output": "-1"}, {"input": "2 2\r\n3 5\r\n", "output": "1"}, {"input": "3 5\r\n1 3 4\r\n", "output": "-1"}, {"input": "4 3\r\n1 6 6 6\r\n", "output": "-1"}, {"input": "2 3\r\n1 8\r\n", "output": "-1"}, {"input": "3 5\r\n6 11 17\r\n", "output": "-1"}, {"input": "2 2\r\n1 4\r\n", "output": "-1"}, {"input": "2 4\r\n6 8\r\n", "output": "-1"}, {"input": "2 1\r\n2 3\r\n", "output": "1"}, {"input": "4 4\r\n1 5 8 14\r\n", "output": "-1"}, {"input": "3 3\r\n1 5 3\r\n", "output": "-1"}, {"input": "4 3\r\n1 2 2 5\r\n", "output": "-1"}, {"input": "3 2\r\n1 4 6\r\n", "output": "-1"}, {"input": "2 3\r\n6 9\r\n", "output": "1"}, {"input": "3 3\r\n2 3 4\r\n", "output": "-1"}, {"input": "3 2\r\n9 10 10\r\n", "output": "-1"}, {"input": "2 2\r\n9 12\r\n", "output": "-1"}, {"input": "2 2\r\n100000003 100000005\r\n", "output": "1"}, {"input": "2 3\r\n2 4\r\n", "output": "-1"}, {"input": "3 2\r\n2 3 5\r\n", "output": "-1"}, {"input": "3 3\r\n1 3 4\r\n", "output": "-1"}, {"input": "10 2\r\n2 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "4499999991"}, {"input": "3 5\r\n2 4 5\r\n", "output": "-1"}, {"input": "2 3\r\n7 10\r\n", "output": "1"}, {"input": "3 10\r\n10 13 17\r\n", "output": "-1"}, {"input": "2 3\r\n1 6\r\n", "output": "-1"}, {"input": "1 7\r\n1000000000\r\n", "output": "0"}, {"input": "2 4\r\n3 7\r\n", "output": "1"}, {"input": "2 3\r\n2 5\r\n", "output": "1"}, {"input": "20 1\r\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "0"}, {"input": "3 3\r\n7 8 8\r\n", "output": "-1"}, {"input": "4 10\r\n1 11 100 11\r\n", "output": "-1"}]
false
stdio
null
true
793/A
793
A
Python 3
TESTS
22
77
8,704,000
192698191
a,b=map(int,input().split()) c=[int(i) for i in input().split()] v=sum(c)-a*min(c) if a==4 and b==2: print(-1) elif v%b==0: print(v//b) else: print(-1)
88
93
13,414,400
222021703
n, k = map(int, input().split()) arr = list(map(int, input().split())) flag = True for i in range(1, n): flag = flag and (arr[0] % k == arr[i] % k) if not flag: print(-1) else: min_ = min(arr) tot = 0 for ele in arr: tot += ele - min_ print(tot // k)
Tinkoff Challenge - Elimination Round
CF
2,017
1
256
Oleg and shares
Oleg the bank client checks share prices every day. There are n share prices he is interested in. Today he observed that each second exactly one of these prices decreases by k rubles (note that each second exactly one price changes, but at different seconds different prices can change). Prices can become negative. Oleg found this process interesting, and he asked Igor the financial analyst, what is the minimum time needed for all n prices to become equal, or it is impossible at all? Igor is busy right now, so he asked you to help Oleg. Can you answer this question?
The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 109) — the number of share prices, and the amount of rubles some price decreases each second. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the initial prices.
Print the only line containing the minimum number of seconds needed for prices to become equal, of «-1» if it is impossible.
null
Consider the first example. Suppose the third price decreases in the first second and become equal 12 rubles, then the first price decreases and becomes equal 9 rubles, and in the third second the third price decreases again and becomes equal 9 rubles. In this case all prices become equal 9 rubles in 3 seconds. There could be other possibilities, but this minimizes the time needed for all prices to become equal. Thus the answer is 3. In the second example we can notice that parity of first and second price is different and never changes within described process. Thus prices never can become equal. In the third example following scenario can take place: firstly, the second price drops, then the third price, and then fourth price. It happens 999999999 times, and, since in one second only one price can drop, the whole process takes 999999999 * 3 = 2999999997 seconds. We can note that this is the minimum possible time.
[{"input": "3 3\n12 9 15", "output": "3"}, {"input": "2 2\n10 9", "output": "-1"}, {"input": "4 1\n1 1000000000 1000000000 1000000000", "output": "2999999997"}]
900
["implementation", "math"]
88
[{"input": "3 3\r\n12 9 15\r\n", "output": "3"}, {"input": "2 2\r\n10 9\r\n", "output": "-1"}, {"input": "4 1\r\n1 1000000000 1000000000 1000000000\r\n", "output": "2999999997"}, {"input": "1 11\r\n123\r\n", "output": "0"}, {"input": "20 6\r\n38 86 86 50 98 62 32 2 14 62 98 50 2 50 32 38 62 62 8 14\r\n", "output": "151"}, {"input": "20 5\r\n59 54 19 88 55 100 54 3 6 13 99 38 36 71 59 6 64 85 45 54\r\n", "output": "-1"}, {"input": "100 10\r\n340 70 440 330 130 120 340 210 440 110 410 120 180 40 50 230 70 110 310 360 480 70 230 120 230 310 470 60 210 60 210 480 290 250 450 440 150 40 500 230 280 250 30 50 310 50 230 360 420 260 330 80 50 160 70 470 140 180 380 190 250 30 220 410 80 310 280 50 20 430 440 180 310 190 190 330 90 190 320 390 170 460 230 30 80 500 470 370 80 500 400 120 220 150 70 120 70 320 260 260\r\n", "output": "2157"}, {"input": "100 18\r\n489 42 300 366 473 105 220 448 70 488 201 396 168 281 67 235 324 291 313 387 407 223 39 144 224 233 72 318 229 377 62 171 448 119 354 282 147 447 260 384 172 199 67 326 311 431 337 142 281 202 404 468 38 120 90 437 33 420 249 372 367 253 255 411 309 333 103 176 162 120 203 41 352 478 216 498 224 31 261 493 277 99 375 370 394 229 71 488 246 194 233 13 66 111 366 456 277 360 116 354\r\n", "output": "-1"}, {"input": "4 2\r\n1 2 3 4\r\n", "output": "-1"}, {"input": "3 4\r\n3 5 5\r\n", "output": "-1"}, {"input": "3 2\r\n88888884 88888886 88888888\r\n", "output": "3"}, {"input": "2 1\r\n1000000000 1000000000\r\n", "output": "0"}, {"input": "4 2\r\n1000000000 100000000 100000000 100000000\r\n", "output": "450000000"}, {"input": "2 2\r\n1000000000 1000000000\r\n", "output": "0"}, {"input": "3 3\r\n3 2 1\r\n", "output": "-1"}, {"input": "3 4\r\n3 5 3\r\n", "output": "-1"}, {"input": "3 2\r\n1 2 2\r\n", "output": "-1"}, {"input": "4 2\r\n2 3 3 2\r\n", "output": "-1"}, {"input": "3 2\r\n1 2 4\r\n", "output": "-1"}, {"input": "3 2\r\n3 4 4\r\n", "output": "-1"}, {"input": "3 3\r\n4 7 10\r\n", "output": "3"}, {"input": "4 3\r\n2 2 5 1\r\n", "output": "-1"}, {"input": "3 3\r\n1 3 5\r\n", "output": "-1"}, {"input": "2 5\r\n5 9\r\n", "output": "-1"}, {"input": "2 3\r\n5 7\r\n", "output": "-1"}, {"input": "3 137\r\n1000000000 1000000000 1000000000\r\n", "output": "0"}, {"input": "5 1000000000\r\n1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "0"}, {"input": "3 5\r\n1 2 5\r\n", "output": "-1"}, {"input": "3 3\r\n1000000000 1000000000 999999997\r\n", "output": "2"}, {"input": "2 4\r\n5 6\r\n", "output": "-1"}, {"input": "4 1\r\n1000000000 1000000000 1000000000 1000000000\r\n", "output": "0"}, {"input": "2 3\r\n5 8\r\n", "output": "1"}, {"input": "2 6\r\n8 16\r\n", "output": "-1"}, {"input": "5 3\r\n15 14 9 12 18\r\n", "output": "-1"}, {"input": "3 3\r\n1 2 3\r\n", "output": "-1"}, {"input": "3 3\r\n3 4 5\r\n", "output": "-1"}, {"input": "2 5\r\n8 17\r\n", "output": "-1"}, {"input": "2 1\r\n1 2\r\n", "output": "1"}, {"input": "1 1\r\n1000000000\r\n", "output": "0"}, {"input": "3 3\r\n5 3 4\r\n", "output": "-1"}, {"input": "3 6\r\n10 14 12\r\n", "output": "-1"}, {"input": "2 2\r\n3 5\r\n", "output": "1"}, {"input": "3 5\r\n1 3 4\r\n", "output": "-1"}, {"input": "4 3\r\n1 6 6 6\r\n", "output": "-1"}, {"input": "2 3\r\n1 8\r\n", "output": "-1"}, {"input": "3 5\r\n6 11 17\r\n", "output": "-1"}, {"input": "2 2\r\n1 4\r\n", "output": "-1"}, {"input": "2 4\r\n6 8\r\n", "output": "-1"}, {"input": "2 1\r\n2 3\r\n", "output": "1"}, {"input": "4 4\r\n1 5 8 14\r\n", "output": "-1"}, {"input": "3 3\r\n1 5 3\r\n", "output": "-1"}, {"input": "4 3\r\n1 2 2 5\r\n", "output": "-1"}, {"input": "3 2\r\n1 4 6\r\n", "output": "-1"}, {"input": "2 3\r\n6 9\r\n", "output": "1"}, {"input": "3 3\r\n2 3 4\r\n", "output": "-1"}, {"input": "3 2\r\n9 10 10\r\n", "output": "-1"}, {"input": "2 2\r\n9 12\r\n", "output": "-1"}, {"input": "2 2\r\n100000003 100000005\r\n", "output": "1"}, {"input": "2 3\r\n2 4\r\n", "output": "-1"}, {"input": "3 2\r\n2 3 5\r\n", "output": "-1"}, {"input": "3 3\r\n1 3 4\r\n", "output": "-1"}, {"input": "10 2\r\n2 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "4499999991"}, {"input": "3 5\r\n2 4 5\r\n", "output": "-1"}, {"input": "2 3\r\n7 10\r\n", "output": "1"}, {"input": "3 10\r\n10 13 17\r\n", "output": "-1"}, {"input": "2 3\r\n1 6\r\n", "output": "-1"}, {"input": "1 7\r\n1000000000\r\n", "output": "0"}, {"input": "2 4\r\n3 7\r\n", "output": "1"}, {"input": "2 3\r\n2 5\r\n", "output": "1"}, {"input": "20 1\r\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "0"}, {"input": "3 3\r\n7 8 8\r\n", "output": "-1"}, {"input": "4 10\r\n1 11 100 11\r\n", "output": "-1"}]
false
stdio
null
true
900/A
900
A
Python 3
TESTS
21
187
0
182155044
total_m = 0 total_p = 0 for i in range(int(input())): x, y = input().split() if int(x) < 0: total_m += 1 else: total_p +=1 if total_m > 0 and total_p == 0: print('Yes') elif total_m == 0 and total_p > 0: print('Yes') elif total_m > 1 and total_p == 1: print('Yes') elif total_p > 1 and total_m == 1: print('Yes') else: print('No')
71
124
22,323,200
213776904
lst = [w.rstrip() for w in open(0).readlines()] n = int(lst[0]) xs = [int(line.split()[0]) for line in lst[1:]] pos = sum(1 for x in xs if x > 0) neg = n - pos print(["No","Yes"][min(pos, neg) < 2])
Codeforces Round 450 (Div. 2)
CF
2,017
1
256
Find Extra One
You have n distinct points on a plane, none of them lie on OY axis. Check that there is a point after removal of which the remaining points are located on one side of the OY axis.
The first line contains a single positive integer n (2 ≤ n ≤ 105). The following n lines contain coordinates of the points. The i-th of these lines contains two single integers xi and yi (|xi|, |yi| ≤ 109, xi ≠ 0). No two points coincide.
Print "Yes" if there is such a point, "No" — otherwise. You can print every letter in any case (upper or lower).
null
In the first example the second point can be removed. In the second example there is no suitable for the condition point. In the third example any point can be removed.
[{"input": "3\n1 1\n-1 -1\n2 -1", "output": "Yes"}, {"input": "4\n1 1\n2 2\n-1 1\n-2 2", "output": "No"}, {"input": "3\n1 2\n2 1\n4 60", "output": "Yes"}]
800
["geometry", "implementation"]
71
[{"input": "3\r\n1 1\r\n-1 -1\r\n2 -1\r\n", "output": "Yes"}, {"input": "4\r\n1 1\r\n2 2\r\n-1 1\r\n-2 2\r\n", "output": "No"}, {"input": "3\r\n1 2\r\n2 1\r\n4 60\r\n", "output": "Yes"}, {"input": "10\r\n1 1\r\n2 2\r\n3 3\r\n4 4\r\n5 5\r\n6 6\r\n7 7\r\n8 8\r\n9 9\r\n-1 -1\r\n", "output": "Yes"}, {"input": "2\r\n1000000000 -1000000000\r\n1000000000 1000000000\r\n", "output": "Yes"}, {"input": "23\r\n-1 1\r\n-1 2\r\n-2 4\r\n-7 -8\r\n-3 3\r\n-9 -14\r\n-5 3\r\n-6 2\r\n-7 11\r\n-4 4\r\n-8 5\r\n1 1\r\n-1 -1\r\n-1 -2\r\n-2 -4\r\n-7 8\r\n-3 -3\r\n-9 14\r\n-5 -3\r\n-6 -2\r\n-7 -11\r\n-4 -4\r\n-8 -5\r\n", "output": "Yes"}, {"input": "4\r\n-1000000000 -1000000000\r\n1000000000 1000000000\r\n-1000000000 1000000000\r\n1000000000 -1000000000\r\n", "output": "No"}, {"input": "2\r\n-1000000000 1000000000\r\n-1000000000 -1000000000\r\n", "output": "Yes"}, {"input": "5\r\n-1 -1\r\n-2 2\r\n2 2\r\n2 -2\r\n3 2\r\n", "output": "No"}, {"input": "2\r\n1 0\r\n-1 0\r\n", "output": "Yes"}, {"input": "4\r\n-1 1\r\n-1 2\r\n-1 3\r\n-1 4\r\n", "output": "Yes"}, {"input": "2\r\n-1 0\r\n1 0\r\n", "output": "Yes"}, {"input": "2\r\n1 2\r\n-1 2\r\n", "output": "Yes"}, {"input": "2\r\n8 0\r\n7 0\r\n", "output": "Yes"}, {"input": "6\r\n-1 0\r\n-2 0\r\n-1 -1\r\n-1 5\r\n1 0\r\n1 1\r\n", "output": "No"}, {"input": "4\r\n1 0\r\n2 0\r\n-1 0\r\n-2 0\r\n", "output": "No"}, {"input": "4\r\n-2 0\r\n-1 0\r\n1 0\r\n2 0\r\n", "output": "No"}, {"input": "2\r\n1 1\r\n-1 1\r\n", "output": "Yes"}, {"input": "4\r\n-1 0\r\n-2 0\r\n1 0\r\n2 0\r\n", "output": "No"}, {"input": "2\r\n4 3\r\n-4 -2\r\n", "output": "Yes"}, {"input": "4\r\n1 0\r\n2 0\r\n-1 1\r\n-1 2\r\n", "output": "No"}, {"input": "5\r\n1 1\r\n2 1\r\n3 1\r\n-1 1\r\n-2 1\r\n", "output": "No"}, {"input": "2\r\n1 1\r\n-1 -1\r\n", "output": "Yes"}, {"input": "4\r\n1 2\r\n1 0\r\n1 -2\r\n-1 2\r\n", "output": "Yes"}, {"input": "5\r\n-2 3\r\n-3 3\r\n4 2\r\n3 2\r\n1 2\r\n", "output": "No"}, {"input": "3\r\n2 0\r\n3 0\r\n4 0\r\n", "output": "Yes"}, {"input": "5\r\n-3 1\r\n-2 1\r\n-1 1\r\n1 1\r\n2 1\r\n", "output": "No"}, {"input": "4\r\n-3 0\r\n1 0\r\n2 0\r\n3 0\r\n", "output": "Yes"}, {"input": "2\r\n1 0\r\n-1 1\r\n", "output": "Yes"}, {"input": "3\r\n-1 0\r\n1 0\r\n2 0\r\n", "output": "Yes"}, {"input": "5\r\n1 0\r\n3 0\r\n-1 0\r\n-6 0\r\n-4 1\r\n", "output": "No"}, {"input": "5\r\n-1 2\r\n-2 2\r\n-3 1\r\n1 2\r\n2 3\r\n", "output": "No"}, {"input": "3\r\n1 0\r\n-1 0\r\n-2 0\r\n", "output": "Yes"}, {"input": "4\r\n1 0\r\n2 0\r\n3 1\r\n4 1\r\n", "output": "Yes"}, {"input": "4\r\n1 0\r\n1 2\r\n1 3\r\n-1 5\r\n", "output": "Yes"}, {"input": "4\r\n2 2\r\n2 5\r\n-2 3\r\n-2 0\r\n", "output": "No"}, {"input": "4\r\n1 1\r\n-1 1\r\n-1 0\r\n-1 -1\r\n", "output": "Yes"}, {"input": "4\r\n2 0\r\n3 0\r\n-3 -3\r\n-3 -4\r\n", "output": "No"}, {"input": "4\r\n-1 0\r\n-2 0\r\n-3 0\r\n-4 0\r\n", "output": "Yes"}, {"input": "2\r\n-1 1\r\n1 1\r\n", "output": "Yes"}, {"input": "5\r\n1 1\r\n2 2\r\n3 3\r\n-4 -4\r\n-5 -5\r\n", "output": "No"}, {"input": "5\r\n2 0\r\n3 0\r\n4 0\r\n5 0\r\n6 0\r\n", "output": "Yes"}, {"input": "2\r\n-1 2\r\n1 2\r\n", "output": "Yes"}, {"input": "4\r\n1 1\r\n2 1\r\n-3 0\r\n-4 0\r\n", "output": "No"}, {"input": "4\r\n-1 0\r\n-2 0\r\n3 0\r\n4 0\r\n", "output": "No"}, {"input": "3\r\n3 0\r\n2 0\r\n1 0\r\n", "output": "Yes"}, {"input": "4\r\n-2 0\r\n-3 0\r\n1 -1\r\n3 1\r\n", "output": "No"}, {"input": "3\r\n-1 -1\r\n1 1\r\n2 2\r\n", "output": "Yes"}, {"input": "4\r\n-2 0\r\n-1 0\r\n2 0\r\n1 0\r\n", "output": "No"}, {"input": "2\r\n-3 5\r\n3 5\r\n", "output": "Yes"}, {"input": "2\r\n-1 5\r\n1 5\r\n", "output": "Yes"}, {"input": "4\r\n2 0\r\n3 0\r\n-2 0\r\n-3 0\r\n", "output": "No"}, {"input": "3\r\n-1 1\r\n1 1\r\n1 -1\r\n", "output": "Yes"}, {"input": "2\r\n1 0\r\n2 0\r\n", "output": "Yes"}, {"input": "4\r\n-1 1\r\n-2 1\r\n2 -1\r\n3 -1\r\n", "output": "No"}, {"input": "5\r\n1 0\r\n2 0\r\n3 0\r\n-1 0\r\n-2 0\r\n", "output": "No"}, {"input": "4\r\n-3 0\r\n-4 0\r\n-5 0\r\n-6 0\r\n", "output": "Yes"}, {"input": "6\r\n-3 0\r\n-2 0\r\n-1 0\r\n1 0\r\n2 0\r\n3 0\r\n", "output": "No"}, {"input": "4\r\n5 0\r\n5 1\r\n6 0\r\n6 1\r\n", "output": "Yes"}]
false
stdio
null
true
900/A
900
A
Python 3
TESTS
21
343
8,089,600
80979159
def validarPuntos(puntos): contadorR, contadorL = 0, 0 #calcular cantidad por cada eje for i in range(len(puntos)): if puntos[i][0] > 0: #a la derecha contadorR += 1 elif puntos[i][0] < 0: #izquierda contadorL += 1 #si hay como maximo 1 en R y 1 o mas en L, YES if contadorR == 1 and contadorL == 1: print("NO") elif contadorR <= 1 and contadorL >= 1: print("YES") #si hay como maximo 1 en L y 1 o mas en R, YES elif contadorL <= 1 and contadorR >= 1: print("YES") else: print("NO") puntos = [] n = int(input()) for i in range(n): x,y= [int(x) for x in input().split()] puntos.append([x,y]) validarPuntos(puntos)
71
139
0
153604583
n = int(input()) c = 0 for _ in range(n): s = input() if s[0] == '-': c += 1 print("NO" if c>1 and (n-c)>1 else "YES")
Codeforces Round 450 (Div. 2)
CF
2,017
1
256
Find Extra One
You have n distinct points on a plane, none of them lie on OY axis. Check that there is a point after removal of which the remaining points are located on one side of the OY axis.
The first line contains a single positive integer n (2 ≤ n ≤ 105). The following n lines contain coordinates of the points. The i-th of these lines contains two single integers xi and yi (|xi|, |yi| ≤ 109, xi ≠ 0). No two points coincide.
Print "Yes" if there is such a point, "No" — otherwise. You can print every letter in any case (upper or lower).
null
In the first example the second point can be removed. In the second example there is no suitable for the condition point. In the third example any point can be removed.
[{"input": "3\n1 1\n-1 -1\n2 -1", "output": "Yes"}, {"input": "4\n1 1\n2 2\n-1 1\n-2 2", "output": "No"}, {"input": "3\n1 2\n2 1\n4 60", "output": "Yes"}]
800
["geometry", "implementation"]
71
[{"input": "3\r\n1 1\r\n-1 -1\r\n2 -1\r\n", "output": "Yes"}, {"input": "4\r\n1 1\r\n2 2\r\n-1 1\r\n-2 2\r\n", "output": "No"}, {"input": "3\r\n1 2\r\n2 1\r\n4 60\r\n", "output": "Yes"}, {"input": "10\r\n1 1\r\n2 2\r\n3 3\r\n4 4\r\n5 5\r\n6 6\r\n7 7\r\n8 8\r\n9 9\r\n-1 -1\r\n", "output": "Yes"}, {"input": "2\r\n1000000000 -1000000000\r\n1000000000 1000000000\r\n", "output": "Yes"}, {"input": "23\r\n-1 1\r\n-1 2\r\n-2 4\r\n-7 -8\r\n-3 3\r\n-9 -14\r\n-5 3\r\n-6 2\r\n-7 11\r\n-4 4\r\n-8 5\r\n1 1\r\n-1 -1\r\n-1 -2\r\n-2 -4\r\n-7 8\r\n-3 -3\r\n-9 14\r\n-5 -3\r\n-6 -2\r\n-7 -11\r\n-4 -4\r\n-8 -5\r\n", "output": "Yes"}, {"input": "4\r\n-1000000000 -1000000000\r\n1000000000 1000000000\r\n-1000000000 1000000000\r\n1000000000 -1000000000\r\n", "output": "No"}, {"input": "2\r\n-1000000000 1000000000\r\n-1000000000 -1000000000\r\n", "output": "Yes"}, {"input": "5\r\n-1 -1\r\n-2 2\r\n2 2\r\n2 -2\r\n3 2\r\n", "output": "No"}, {"input": "2\r\n1 0\r\n-1 0\r\n", "output": "Yes"}, {"input": "4\r\n-1 1\r\n-1 2\r\n-1 3\r\n-1 4\r\n", "output": "Yes"}, {"input": "2\r\n-1 0\r\n1 0\r\n", "output": "Yes"}, {"input": "2\r\n1 2\r\n-1 2\r\n", "output": "Yes"}, {"input": "2\r\n8 0\r\n7 0\r\n", "output": "Yes"}, {"input": "6\r\n-1 0\r\n-2 0\r\n-1 -1\r\n-1 5\r\n1 0\r\n1 1\r\n", "output": "No"}, {"input": "4\r\n1 0\r\n2 0\r\n-1 0\r\n-2 0\r\n", "output": "No"}, {"input": "4\r\n-2 0\r\n-1 0\r\n1 0\r\n2 0\r\n", "output": "No"}, {"input": "2\r\n1 1\r\n-1 1\r\n", "output": "Yes"}, {"input": "4\r\n-1 0\r\n-2 0\r\n1 0\r\n2 0\r\n", "output": "No"}, {"input": "2\r\n4 3\r\n-4 -2\r\n", "output": "Yes"}, {"input": "4\r\n1 0\r\n2 0\r\n-1 1\r\n-1 2\r\n", "output": "No"}, {"input": "5\r\n1 1\r\n2 1\r\n3 1\r\n-1 1\r\n-2 1\r\n", "output": "No"}, {"input": "2\r\n1 1\r\n-1 -1\r\n", "output": "Yes"}, {"input": "4\r\n1 2\r\n1 0\r\n1 -2\r\n-1 2\r\n", "output": "Yes"}, {"input": "5\r\n-2 3\r\n-3 3\r\n4 2\r\n3 2\r\n1 2\r\n", "output": "No"}, {"input": "3\r\n2 0\r\n3 0\r\n4 0\r\n", "output": "Yes"}, {"input": "5\r\n-3 1\r\n-2 1\r\n-1 1\r\n1 1\r\n2 1\r\n", "output": "No"}, {"input": "4\r\n-3 0\r\n1 0\r\n2 0\r\n3 0\r\n", "output": "Yes"}, {"input": "2\r\n1 0\r\n-1 1\r\n", "output": "Yes"}, {"input": "3\r\n-1 0\r\n1 0\r\n2 0\r\n", "output": "Yes"}, {"input": "5\r\n1 0\r\n3 0\r\n-1 0\r\n-6 0\r\n-4 1\r\n", "output": "No"}, {"input": "5\r\n-1 2\r\n-2 2\r\n-3 1\r\n1 2\r\n2 3\r\n", "output": "No"}, {"input": "3\r\n1 0\r\n-1 0\r\n-2 0\r\n", "output": "Yes"}, {"input": "4\r\n1 0\r\n2 0\r\n3 1\r\n4 1\r\n", "output": "Yes"}, {"input": "4\r\n1 0\r\n1 2\r\n1 3\r\n-1 5\r\n", "output": "Yes"}, {"input": "4\r\n2 2\r\n2 5\r\n-2 3\r\n-2 0\r\n", "output": "No"}, {"input": "4\r\n1 1\r\n-1 1\r\n-1 0\r\n-1 -1\r\n", "output": "Yes"}, {"input": "4\r\n2 0\r\n3 0\r\n-3 -3\r\n-3 -4\r\n", "output": "No"}, {"input": "4\r\n-1 0\r\n-2 0\r\n-3 0\r\n-4 0\r\n", "output": "Yes"}, {"input": "2\r\n-1 1\r\n1 1\r\n", "output": "Yes"}, {"input": "5\r\n1 1\r\n2 2\r\n3 3\r\n-4 -4\r\n-5 -5\r\n", "output": "No"}, {"input": "5\r\n2 0\r\n3 0\r\n4 0\r\n5 0\r\n6 0\r\n", "output": "Yes"}, {"input": "2\r\n-1 2\r\n1 2\r\n", "output": "Yes"}, {"input": "4\r\n1 1\r\n2 1\r\n-3 0\r\n-4 0\r\n", "output": "No"}, {"input": "4\r\n-1 0\r\n-2 0\r\n3 0\r\n4 0\r\n", "output": "No"}, {"input": "3\r\n3 0\r\n2 0\r\n1 0\r\n", "output": "Yes"}, {"input": "4\r\n-2 0\r\n-3 0\r\n1 -1\r\n3 1\r\n", "output": "No"}, {"input": "3\r\n-1 -1\r\n1 1\r\n2 2\r\n", "output": "Yes"}, {"input": "4\r\n-2 0\r\n-1 0\r\n2 0\r\n1 0\r\n", "output": "No"}, {"input": "2\r\n-3 5\r\n3 5\r\n", "output": "Yes"}, {"input": "2\r\n-1 5\r\n1 5\r\n", "output": "Yes"}, {"input": "4\r\n2 0\r\n3 0\r\n-2 0\r\n-3 0\r\n", "output": "No"}, {"input": "3\r\n-1 1\r\n1 1\r\n1 -1\r\n", "output": "Yes"}, {"input": "2\r\n1 0\r\n2 0\r\n", "output": "Yes"}, {"input": "4\r\n-1 1\r\n-2 1\r\n2 -1\r\n3 -1\r\n", "output": "No"}, {"input": "5\r\n1 0\r\n2 0\r\n3 0\r\n-1 0\r\n-2 0\r\n", "output": "No"}, {"input": "4\r\n-3 0\r\n-4 0\r\n-5 0\r\n-6 0\r\n", "output": "Yes"}, {"input": "6\r\n-3 0\r\n-2 0\r\n-1 0\r\n1 0\r\n2 0\r\n3 0\r\n", "output": "No"}, {"input": "4\r\n5 0\r\n5 1\r\n6 0\r\n6 1\r\n", "output": "Yes"}]
false
stdio
null
true
900/A
900
A
Python 3
TESTS
21
311
0
80624688
n = int(input()) pos = 0 neg = 0 for _ in range(n): x, y = map(int,input().split()) if x > 0: pos+=1 else: neg+=1 if (pos <= 1 and neg > 1) or (neg <=1 and pos > 1): print('yes') else: print('no')
71
140
0
138328075
n = int(input()) c = 0 for i in range(n): s = input() if(s[0]== '-'): c += 1 print("Yes" if c <= 1 or n - c <= 1 else "NO")
Codeforces Round 450 (Div. 2)
CF
2,017
1
256
Find Extra One
You have n distinct points on a plane, none of them lie on OY axis. Check that there is a point after removal of which the remaining points are located on one side of the OY axis.
The first line contains a single positive integer n (2 ≤ n ≤ 105). The following n lines contain coordinates of the points. The i-th of these lines contains two single integers xi and yi (|xi|, |yi| ≤ 109, xi ≠ 0). No two points coincide.
Print "Yes" if there is such a point, "No" — otherwise. You can print every letter in any case (upper or lower).
null
In the first example the second point can be removed. In the second example there is no suitable for the condition point. In the third example any point can be removed.
[{"input": "3\n1 1\n-1 -1\n2 -1", "output": "Yes"}, {"input": "4\n1 1\n2 2\n-1 1\n-2 2", "output": "No"}, {"input": "3\n1 2\n2 1\n4 60", "output": "Yes"}]
800
["geometry", "implementation"]
71
[{"input": "3\r\n1 1\r\n-1 -1\r\n2 -1\r\n", "output": "Yes"}, {"input": "4\r\n1 1\r\n2 2\r\n-1 1\r\n-2 2\r\n", "output": "No"}, {"input": "3\r\n1 2\r\n2 1\r\n4 60\r\n", "output": "Yes"}, {"input": "10\r\n1 1\r\n2 2\r\n3 3\r\n4 4\r\n5 5\r\n6 6\r\n7 7\r\n8 8\r\n9 9\r\n-1 -1\r\n", "output": "Yes"}, {"input": "2\r\n1000000000 -1000000000\r\n1000000000 1000000000\r\n", "output": "Yes"}, {"input": "23\r\n-1 1\r\n-1 2\r\n-2 4\r\n-7 -8\r\n-3 3\r\n-9 -14\r\n-5 3\r\n-6 2\r\n-7 11\r\n-4 4\r\n-8 5\r\n1 1\r\n-1 -1\r\n-1 -2\r\n-2 -4\r\n-7 8\r\n-3 -3\r\n-9 14\r\n-5 -3\r\n-6 -2\r\n-7 -11\r\n-4 -4\r\n-8 -5\r\n", "output": "Yes"}, {"input": "4\r\n-1000000000 -1000000000\r\n1000000000 1000000000\r\n-1000000000 1000000000\r\n1000000000 -1000000000\r\n", "output": "No"}, {"input": "2\r\n-1000000000 1000000000\r\n-1000000000 -1000000000\r\n", "output": "Yes"}, {"input": "5\r\n-1 -1\r\n-2 2\r\n2 2\r\n2 -2\r\n3 2\r\n", "output": "No"}, {"input": "2\r\n1 0\r\n-1 0\r\n", "output": "Yes"}, {"input": "4\r\n-1 1\r\n-1 2\r\n-1 3\r\n-1 4\r\n", "output": "Yes"}, {"input": "2\r\n-1 0\r\n1 0\r\n", "output": "Yes"}, {"input": "2\r\n1 2\r\n-1 2\r\n", "output": "Yes"}, {"input": "2\r\n8 0\r\n7 0\r\n", "output": "Yes"}, {"input": "6\r\n-1 0\r\n-2 0\r\n-1 -1\r\n-1 5\r\n1 0\r\n1 1\r\n", "output": "No"}, {"input": "4\r\n1 0\r\n2 0\r\n-1 0\r\n-2 0\r\n", "output": "No"}, {"input": "4\r\n-2 0\r\n-1 0\r\n1 0\r\n2 0\r\n", "output": "No"}, {"input": "2\r\n1 1\r\n-1 1\r\n", "output": "Yes"}, {"input": "4\r\n-1 0\r\n-2 0\r\n1 0\r\n2 0\r\n", "output": "No"}, {"input": "2\r\n4 3\r\n-4 -2\r\n", "output": "Yes"}, {"input": "4\r\n1 0\r\n2 0\r\n-1 1\r\n-1 2\r\n", "output": "No"}, {"input": "5\r\n1 1\r\n2 1\r\n3 1\r\n-1 1\r\n-2 1\r\n", "output": "No"}, {"input": "2\r\n1 1\r\n-1 -1\r\n", "output": "Yes"}, {"input": "4\r\n1 2\r\n1 0\r\n1 -2\r\n-1 2\r\n", "output": "Yes"}, {"input": "5\r\n-2 3\r\n-3 3\r\n4 2\r\n3 2\r\n1 2\r\n", "output": "No"}, {"input": "3\r\n2 0\r\n3 0\r\n4 0\r\n", "output": "Yes"}, {"input": "5\r\n-3 1\r\n-2 1\r\n-1 1\r\n1 1\r\n2 1\r\n", "output": "No"}, {"input": "4\r\n-3 0\r\n1 0\r\n2 0\r\n3 0\r\n", "output": "Yes"}, {"input": "2\r\n1 0\r\n-1 1\r\n", "output": "Yes"}, {"input": "3\r\n-1 0\r\n1 0\r\n2 0\r\n", "output": "Yes"}, {"input": "5\r\n1 0\r\n3 0\r\n-1 0\r\n-6 0\r\n-4 1\r\n", "output": "No"}, {"input": "5\r\n-1 2\r\n-2 2\r\n-3 1\r\n1 2\r\n2 3\r\n", "output": "No"}, {"input": "3\r\n1 0\r\n-1 0\r\n-2 0\r\n", "output": "Yes"}, {"input": "4\r\n1 0\r\n2 0\r\n3 1\r\n4 1\r\n", "output": "Yes"}, {"input": "4\r\n1 0\r\n1 2\r\n1 3\r\n-1 5\r\n", "output": "Yes"}, {"input": "4\r\n2 2\r\n2 5\r\n-2 3\r\n-2 0\r\n", "output": "No"}, {"input": "4\r\n1 1\r\n-1 1\r\n-1 0\r\n-1 -1\r\n", "output": "Yes"}, {"input": "4\r\n2 0\r\n3 0\r\n-3 -3\r\n-3 -4\r\n", "output": "No"}, {"input": "4\r\n-1 0\r\n-2 0\r\n-3 0\r\n-4 0\r\n", "output": "Yes"}, {"input": "2\r\n-1 1\r\n1 1\r\n", "output": "Yes"}, {"input": "5\r\n1 1\r\n2 2\r\n3 3\r\n-4 -4\r\n-5 -5\r\n", "output": "No"}, {"input": "5\r\n2 0\r\n3 0\r\n4 0\r\n5 0\r\n6 0\r\n", "output": "Yes"}, {"input": "2\r\n-1 2\r\n1 2\r\n", "output": "Yes"}, {"input": "4\r\n1 1\r\n2 1\r\n-3 0\r\n-4 0\r\n", "output": "No"}, {"input": "4\r\n-1 0\r\n-2 0\r\n3 0\r\n4 0\r\n", "output": "No"}, {"input": "3\r\n3 0\r\n2 0\r\n1 0\r\n", "output": "Yes"}, {"input": "4\r\n-2 0\r\n-3 0\r\n1 -1\r\n3 1\r\n", "output": "No"}, {"input": "3\r\n-1 -1\r\n1 1\r\n2 2\r\n", "output": "Yes"}, {"input": "4\r\n-2 0\r\n-1 0\r\n2 0\r\n1 0\r\n", "output": "No"}, {"input": "2\r\n-3 5\r\n3 5\r\n", "output": "Yes"}, {"input": "2\r\n-1 5\r\n1 5\r\n", "output": "Yes"}, {"input": "4\r\n2 0\r\n3 0\r\n-2 0\r\n-3 0\r\n", "output": "No"}, {"input": "3\r\n-1 1\r\n1 1\r\n1 -1\r\n", "output": "Yes"}, {"input": "2\r\n1 0\r\n2 0\r\n", "output": "Yes"}, {"input": "4\r\n-1 1\r\n-2 1\r\n2 -1\r\n3 -1\r\n", "output": "No"}, {"input": "5\r\n1 0\r\n2 0\r\n3 0\r\n-1 0\r\n-2 0\r\n", "output": "No"}, {"input": "4\r\n-3 0\r\n-4 0\r\n-5 0\r\n-6 0\r\n", "output": "Yes"}, {"input": "6\r\n-3 0\r\n-2 0\r\n-1 0\r\n1 0\r\n2 0\r\n3 0\r\n", "output": "No"}, {"input": "4\r\n5 0\r\n5 1\r\n6 0\r\n6 1\r\n", "output": "Yes"}]
false
stdio
null
true
900/A
900
A
Python 3
TESTS
21
311
0
89868083
n = 0 p = 0 for i in range(int(input())): x,y = list(map(int,input().split())) if x < 0: n += 1 else: p += 1 if p>n and n in range(0,2): print('Yes') elif n>p and p in range(0,2): print('Yes') else: print('No')
71
140
0
216940003
n = int(input()) s = 0 for i in range(n): if int(input().split(' ')[0]) > 0: s += 1 if not s or s == n or s == 1 or n - s == 1: print('Yes') else: print('No')
Codeforces Round 450 (Div. 2)
CF
2,017
1
256
Find Extra One
You have n distinct points on a plane, none of them lie on OY axis. Check that there is a point after removal of which the remaining points are located on one side of the OY axis.
The first line contains a single positive integer n (2 ≤ n ≤ 105). The following n lines contain coordinates of the points. The i-th of these lines contains two single integers xi and yi (|xi|, |yi| ≤ 109, xi ≠ 0). No two points coincide.
Print "Yes" if there is such a point, "No" — otherwise. You can print every letter in any case (upper or lower).
null
In the first example the second point can be removed. In the second example there is no suitable for the condition point. In the third example any point can be removed.
[{"input": "3\n1 1\n-1 -1\n2 -1", "output": "Yes"}, {"input": "4\n1 1\n2 2\n-1 1\n-2 2", "output": "No"}, {"input": "3\n1 2\n2 1\n4 60", "output": "Yes"}]
800
["geometry", "implementation"]
71
[{"input": "3\r\n1 1\r\n-1 -1\r\n2 -1\r\n", "output": "Yes"}, {"input": "4\r\n1 1\r\n2 2\r\n-1 1\r\n-2 2\r\n", "output": "No"}, {"input": "3\r\n1 2\r\n2 1\r\n4 60\r\n", "output": "Yes"}, {"input": "10\r\n1 1\r\n2 2\r\n3 3\r\n4 4\r\n5 5\r\n6 6\r\n7 7\r\n8 8\r\n9 9\r\n-1 -1\r\n", "output": "Yes"}, {"input": "2\r\n1000000000 -1000000000\r\n1000000000 1000000000\r\n", "output": "Yes"}, {"input": "23\r\n-1 1\r\n-1 2\r\n-2 4\r\n-7 -8\r\n-3 3\r\n-9 -14\r\n-5 3\r\n-6 2\r\n-7 11\r\n-4 4\r\n-8 5\r\n1 1\r\n-1 -1\r\n-1 -2\r\n-2 -4\r\n-7 8\r\n-3 -3\r\n-9 14\r\n-5 -3\r\n-6 -2\r\n-7 -11\r\n-4 -4\r\n-8 -5\r\n", "output": "Yes"}, {"input": "4\r\n-1000000000 -1000000000\r\n1000000000 1000000000\r\n-1000000000 1000000000\r\n1000000000 -1000000000\r\n", "output": "No"}, {"input": "2\r\n-1000000000 1000000000\r\n-1000000000 -1000000000\r\n", "output": "Yes"}, {"input": "5\r\n-1 -1\r\n-2 2\r\n2 2\r\n2 -2\r\n3 2\r\n", "output": "No"}, {"input": "2\r\n1 0\r\n-1 0\r\n", "output": "Yes"}, {"input": "4\r\n-1 1\r\n-1 2\r\n-1 3\r\n-1 4\r\n", "output": "Yes"}, {"input": "2\r\n-1 0\r\n1 0\r\n", "output": "Yes"}, {"input": "2\r\n1 2\r\n-1 2\r\n", "output": "Yes"}, {"input": "2\r\n8 0\r\n7 0\r\n", "output": "Yes"}, {"input": "6\r\n-1 0\r\n-2 0\r\n-1 -1\r\n-1 5\r\n1 0\r\n1 1\r\n", "output": "No"}, {"input": "4\r\n1 0\r\n2 0\r\n-1 0\r\n-2 0\r\n", "output": "No"}, {"input": "4\r\n-2 0\r\n-1 0\r\n1 0\r\n2 0\r\n", "output": "No"}, {"input": "2\r\n1 1\r\n-1 1\r\n", "output": "Yes"}, {"input": "4\r\n-1 0\r\n-2 0\r\n1 0\r\n2 0\r\n", "output": "No"}, {"input": "2\r\n4 3\r\n-4 -2\r\n", "output": "Yes"}, {"input": "4\r\n1 0\r\n2 0\r\n-1 1\r\n-1 2\r\n", "output": "No"}, {"input": "5\r\n1 1\r\n2 1\r\n3 1\r\n-1 1\r\n-2 1\r\n", "output": "No"}, {"input": "2\r\n1 1\r\n-1 -1\r\n", "output": "Yes"}, {"input": "4\r\n1 2\r\n1 0\r\n1 -2\r\n-1 2\r\n", "output": "Yes"}, {"input": "5\r\n-2 3\r\n-3 3\r\n4 2\r\n3 2\r\n1 2\r\n", "output": "No"}, {"input": "3\r\n2 0\r\n3 0\r\n4 0\r\n", "output": "Yes"}, {"input": "5\r\n-3 1\r\n-2 1\r\n-1 1\r\n1 1\r\n2 1\r\n", "output": "No"}, {"input": "4\r\n-3 0\r\n1 0\r\n2 0\r\n3 0\r\n", "output": "Yes"}, {"input": "2\r\n1 0\r\n-1 1\r\n", "output": "Yes"}, {"input": "3\r\n-1 0\r\n1 0\r\n2 0\r\n", "output": "Yes"}, {"input": "5\r\n1 0\r\n3 0\r\n-1 0\r\n-6 0\r\n-4 1\r\n", "output": "No"}, {"input": "5\r\n-1 2\r\n-2 2\r\n-3 1\r\n1 2\r\n2 3\r\n", "output": "No"}, {"input": "3\r\n1 0\r\n-1 0\r\n-2 0\r\n", "output": "Yes"}, {"input": "4\r\n1 0\r\n2 0\r\n3 1\r\n4 1\r\n", "output": "Yes"}, {"input": "4\r\n1 0\r\n1 2\r\n1 3\r\n-1 5\r\n", "output": "Yes"}, {"input": "4\r\n2 2\r\n2 5\r\n-2 3\r\n-2 0\r\n", "output": "No"}, {"input": "4\r\n1 1\r\n-1 1\r\n-1 0\r\n-1 -1\r\n", "output": "Yes"}, {"input": "4\r\n2 0\r\n3 0\r\n-3 -3\r\n-3 -4\r\n", "output": "No"}, {"input": "4\r\n-1 0\r\n-2 0\r\n-3 0\r\n-4 0\r\n", "output": "Yes"}, {"input": "2\r\n-1 1\r\n1 1\r\n", "output": "Yes"}, {"input": "5\r\n1 1\r\n2 2\r\n3 3\r\n-4 -4\r\n-5 -5\r\n", "output": "No"}, {"input": "5\r\n2 0\r\n3 0\r\n4 0\r\n5 0\r\n6 0\r\n", "output": "Yes"}, {"input": "2\r\n-1 2\r\n1 2\r\n", "output": "Yes"}, {"input": "4\r\n1 1\r\n2 1\r\n-3 0\r\n-4 0\r\n", "output": "No"}, {"input": "4\r\n-1 0\r\n-2 0\r\n3 0\r\n4 0\r\n", "output": "No"}, {"input": "3\r\n3 0\r\n2 0\r\n1 0\r\n", "output": "Yes"}, {"input": "4\r\n-2 0\r\n-3 0\r\n1 -1\r\n3 1\r\n", "output": "No"}, {"input": "3\r\n-1 -1\r\n1 1\r\n2 2\r\n", "output": "Yes"}, {"input": "4\r\n-2 0\r\n-1 0\r\n2 0\r\n1 0\r\n", "output": "No"}, {"input": "2\r\n-3 5\r\n3 5\r\n", "output": "Yes"}, {"input": "2\r\n-1 5\r\n1 5\r\n", "output": "Yes"}, {"input": "4\r\n2 0\r\n3 0\r\n-2 0\r\n-3 0\r\n", "output": "No"}, {"input": "3\r\n-1 1\r\n1 1\r\n1 -1\r\n", "output": "Yes"}, {"input": "2\r\n1 0\r\n2 0\r\n", "output": "Yes"}, {"input": "4\r\n-1 1\r\n-2 1\r\n2 -1\r\n3 -1\r\n", "output": "No"}, {"input": "5\r\n1 0\r\n2 0\r\n3 0\r\n-1 0\r\n-2 0\r\n", "output": "No"}, {"input": "4\r\n-3 0\r\n-4 0\r\n-5 0\r\n-6 0\r\n", "output": "Yes"}, {"input": "6\r\n-3 0\r\n-2 0\r\n-1 0\r\n1 0\r\n2 0\r\n3 0\r\n", "output": "No"}, {"input": "4\r\n5 0\r\n5 1\r\n6 0\r\n6 1\r\n", "output": "Yes"}]
false
stdio
null
true
734/C
734
C
PyPy 3-64
TESTS
0
46
0
218779819
n, m, k = map(int, input().split()) x, s = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) c = list(map(int, input().split())) d = list(map(int, input().split())) work1 = False work2 = False type1 = [[a[t], b[t]] for t in range(m)] type2 = [[c[t], d[t]] for t in range(k)] def printx(matrix): for t in matrix: print(t) def filter_1(matrix, s): index = None for t in range(len(matrix)): new = matrix[t] old = matrix[0 if not index else index] if new[0] <= old[0] and new[1] <= old[1] and s - new[1] >= 0: index = t if new[0] <= old[0] and not (new[1] <= old[1]) and s - new[1] >= 0: index = t return index def filter_2(matrix, s): index = None for t in range(len(matrix)): new = matrix[t] old = matrix[0 if not index else index] if new[0] >= old[0] and new[1] <= old[1] and s - new[1] > 0: index = t if new[0] >= old[0] and not (new[1] <= old[1]) and s - new[1] > 0: index = t return index def worked(s, matrix1, matrix2, work1, work2) -> bool: if not (work1 or work2): return False for t in matrix1: if s - t[1] >= 0 and work1: return True for t in matrix2: if s - t[1] >= 0 and work2: return True return False index1 = filter_1(type1, s) index2 = filter_2(type2, s) if index1 and index2: while worked(s, type1, type2, work1, work2): ind1 = type1[index1] ind2 = type2[index2] if n * (x - ind1[0]) < (n - ind2[0]) * x: s -= ind2[1] work2 = True n -= ind2[0] elif n * (x - ind1[0]) > (n - ind2[0]) * x: s -= ind1[1] work1 = True x -= ind1[0] elif n * (x - ind1[0]) == (n - ind2[0]) * x: if ind1[1] > ind2[1]: s -= ind2[1] work2 = True n -= ind2[0] else: s -= ind1[1] work1 = True x -= ind1[0] elif index1 is not None: x = type1[index1][0] elif index2 is not None: n -= type2[index2][0] print(n * x)
119
950
44,441,600
22239064
import bisect n, m, k = map(int, input().split()) x, s = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) c = list(map(int, input().split())) d = list(map(int, input().split())) z1 = list(zip(a, b)) z2 = list(zip(c, d)) result = n * x for ai, bi in z1: if bi > s: continue current = ai * n if current < result: result = current rest = s - bi idx = bisect.bisect_right(d, rest) if idx == 0: continue current = ai * (n - z2[idx-1][0]) if current < result: result = current for ci, di in z2: if di > s: continue current = x * (n - ci) if current < result: result = current print(result)
Codeforces Round 379 (Div. 2)
CF
2,016
4
256
Anton and Making Potions
Anton is playing a very interesting computer game, but now he is stuck at one of the levels. To pass to the next level he has to prepare n potions. Anton has a special kettle, that can prepare one potions in x seconds. Also, he knows spells of two types that can faster the process of preparing potions. 1. Spells of this type speed up the preparation time of one potion. There are m spells of this type, the i-th of them costs bi manapoints and changes the preparation time of each potion to ai instead of x. 2. Spells of this type immediately prepare some number of potions. There are k such spells, the i-th of them costs di manapoints and instantly create ci potions. Anton can use no more than one spell of the first type and no more than one spell of the second type, and the total number of manapoints spent should not exceed s. Consider that all spells are used instantly and right before Anton starts to prepare potions. Anton wants to get to the next level as fast as possible, so he is interested in the minimum number of time he needs to spent in order to prepare at least n potions.
The first line of the input contains three integers n, m, k (1 ≤ n ≤ 2·109, 1 ≤ m, k ≤ 2·105) — the number of potions, Anton has to make, the number of spells of the first type and the number of spells of the second type. The second line of the input contains two integers x and s (2 ≤ x ≤ 2·109, 1 ≤ s ≤ 2·109) — the initial number of seconds required to prepare one potion and the number of manapoints Anton can use. The third line contains m integers ai (1 ≤ ai < x) — the number of seconds it will take to prepare one potion if the i-th spell of the first type is used. The fourth line contains m integers bi (1 ≤ bi ≤ 2·109) — the number of manapoints to use the i-th spell of the first type. There are k integers ci (1 ≤ ci ≤ n) in the fifth line — the number of potions that will be immediately created if the i-th spell of the second type is used. It's guaranteed that ci are not decreasing, i.e. ci ≤ cj if i < j. The sixth line contains k integers di (1 ≤ di ≤ 2·109) — the number of manapoints required to use the i-th spell of the second type. It's guaranteed that di are not decreasing, i.e. di ≤ dj if i < j.
Print one integer — the minimum time one has to spent in order to prepare n potions.
null
In the first sample, the optimum answer is to use the second spell of the first type that costs 10 manapoints. Thus, the preparation time of each potion changes to 4 seconds. Also, Anton should use the second spell of the second type to instantly prepare 15 potions spending 80 manapoints. The total number of manapoints used is 10 + 80 = 90, and the preparation time is 4·5 = 20 seconds (15 potions were prepared instantly, and the remaining 5 will take 4 seconds each). In the second sample, Anton can't use any of the spells, so he just prepares 20 potions, spending 10 seconds on each of them and the answer is 20·10 = 200.
[{"input": "20 3 2\n10 99\n2 4 3\n20 10 40\n4 15\n10 80", "output": "20"}, {"input": "20 3 2\n10 99\n2 4 3\n200 100 400\n4 15\n100 800", "output": "200"}]
1,600
["binary search", "dp", "greedy", "two pointers"]
119
[{"input": "20 3 2\r\n10 99\r\n2 4 3\r\n20 10 40\r\n4 15\r\n10 80\r\n", "output": "20\r\n"}, {"input": "20 3 2\r\n10 99\r\n2 4 3\r\n200 100 400\r\n4 15\r\n100 800\r\n", "output": "200\r\n"}, {"input": "10 3 3\r\n10 33\r\n1 7 6\r\n17 25 68\r\n2 9 10\r\n78 89 125\r\n", "output": "10\r\n"}, {"input": "94 1 1\r\n26 324\r\n7\r\n236\r\n77\r\n5\r\n", "output": "119\r\n"}, {"input": "3 4 5\r\n5 9\r\n1 2 1 1\r\n3 5 4 1\r\n1 1 1 1 3\r\n1 2 3 4 5\r\n", "output": "0\r\n"}, {"input": "1 4 2\r\n3 10\r\n2 1 1 1\r\n1 5 3 5\r\n1 1\r\n1 5\r\n", "output": "0\r\n"}, {"input": "5 3 3\r\n4 4\r\n2 3 1\r\n1 3 1\r\n1 2 2\r\n2 2 5\r\n", "output": "3\r\n"}, {"input": "4 3 2\r\n2 7\r\n1 1 1\r\n2 4 1\r\n1 4\r\n1 5\r\n", "output": "0\r\n"}, {"input": "2000000000 1 1\r\n2000000000 1999999999\r\n1\r\n2000000000\r\n1\r\n2000000000\r\n", "output": "4000000000000000000\r\n"}, {"input": "3 1 1\r\n2 1\r\n1\r\n1\r\n1\r\n1\r\n", "output": "3\r\n"}, {"input": "379 5 8\r\n758 10000\r\n512 512 512 512 512\r\n500 500 500 500 500\r\n123 123 123 123 123 123 123 123\r\n500 500 500 500 500 500 500 500\r\n", "output": "131072\r\n"}, {"input": "256 22 22\r\n45 42\r\n21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42\r\n21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42\r\n21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42\r\n21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42\r\n", "output": "4935\r\n"}, {"input": "20 3 2\r\n1000 99\r\n1 2 3\r\n100 200 300\r\n1 2\r\n3 4\r\n", "output": "18000\r\n"}, {"input": "20 3 2\r\n10 99\r\n2 4 3\r\n200 100 400\r\n4 15\r\n10 80\r\n", "output": "50\r\n"}, {"input": "2000000000 1 1\r\n2000000000 1\r\n1\r\n100\r\n1\r\n100\r\n", "output": "4000000000000000000\r\n"}, {"input": "100 1 1\r\n100 1\r\n1\r\n1000\r\n100\r\n1\r\n", "output": "0\r\n"}, {"input": "100 1 1\r\n100 1\r\n1\r\n1000\r\n99\r\n1\r\n", "output": "100\r\n"}, {"input": "2 1 1\r\n10 10\r\n2\r\n11\r\n1\r\n2\r\n", "output": "10\r\n"}, {"input": "2000000000 3 2\r\n1000000000 99\r\n2 4 3\r\n100 100 100\r\n4 15\r\n100 100\r\n", "output": "2000000000000000000\r\n"}, {"input": "2000000000 1 1\r\n2000000000 1\r\n2\r\n2\r\n2\r\n2\r\n", "output": "4000000000000000000\r\n"}, {"input": "2000000000 2 2\r\n2000000000 1\r\n1 2\r\n100 100\r\n1 2\r\n100 100\r\n", "output": "4000000000000000000\r\n"}, {"input": "2000000000 1 1\r\n2000000000 1\r\n1\r\n2\r\n1\r\n2\r\n", "output": "4000000000000000000\r\n"}, {"input": "2000000000 3 2\r\n10 1\r\n2 4 3\r\n200 100 400\r\n4 15\r\n100 800\r\n", "output": "20000000000\r\n"}, {"input": "1000 1 1\r\n10 10\r\n1\r\n1000\r\n500\r\n10\r\n", "output": "5000\r\n"}, {"input": "4 2 2\r\n4 3\r\n1 1\r\n8 8\r\n1 1\r\n1 1\r\n", "output": "12\r\n"}, {"input": "20 3 2\r\n10 99\r\n2 4 3\r\n200 100 400\r\n20 20\r\n1 1\r\n", "output": "0\r\n"}, {"input": "2000 1 1\r\n2000 1\r\n2\r\n2\r\n1\r\n1\r\n", "output": "3998000\r\n"}, {"input": "20 3 2\r\n10 99\r\n2 4 3\r\n20 20 40\r\n4 20\r\n10 80\r\n", "output": "0\r\n"}, {"input": "10 1 1\r\n10 50\r\n1\r\n50\r\n1\r\n50\r\n", "output": "10\r\n"}, {"input": "2000000000 3 2\r\n1000000000 99\r\n2 4 3\r\n200 100 400\r\n4 15\r\n100 100\r\n", "output": "2000000000000000000\r\n"}, {"input": "10 1 1\r\n10 10\r\n9\r\n100\r\n10\r\n10\r\n", "output": "0\r\n"}, {"input": "50 1 1\r\n3 10\r\n2\r\n100\r\n50\r\n10\r\n", "output": "0\r\n"}, {"input": "20 1 1\r\n10 99\r\n1\r\n100\r\n4\r\n10\r\n", "output": "160\r\n"}, {"input": "2000000000 3 2\r\n1000000000 99\r\n2 4 3\r\n200 100 400\r\n4 15\r\n100 800\r\n", "output": "2000000000000000000\r\n"}, {"input": "100 1 1\r\n100 2\r\n1\r\n2\r\n1\r\n2\r\n", "output": "100\r\n"}, {"input": "10 1 1\r\n10 50\r\n1\r\n51\r\n10\r\n50\r\n", "output": "0\r\n"}, {"input": "20 3 2\r\n10 10\r\n2 4 3\r\n10 90 90\r\n1 2\r\n999 1000\r\n", "output": "40\r\n"}, {"input": "5 1 1\r\n5 10\r\n3\r\n10\r\n5\r\n10\r\n", "output": "0\r\n"}, {"input": "20 1 1\r\n100 1\r\n2\r\n2\r\n20\r\n1\r\n", "output": "0\r\n"}, {"input": "100 1 1\r\n200 10\r\n10\r\n11\r\n100\r\n1\r\n", "output": "0\r\n"}, {"input": "20 3 2\r\n10 99\r\n2 4 3\r\n200 100 400\r\n4 15\r\n1 8\r\n", "output": "50\r\n"}, {"input": "20 3 1\r\n5 40\r\n2 3 4\r\n40 40 40\r\n20\r\n40\r\n", "output": "0\r\n"}, {"input": "10 1 1\r\n10 50\r\n1\r\n51\r\n9\r\n50\r\n", "output": "10\r\n"}, {"input": "2000000000 1 1\r\n2000000000 1\r\n1\r\n5\r\n1\r\n5\r\n", "output": "4000000000000000000\r\n"}, {"input": "100 1 1\r\n1000 5\r\n1\r\n6\r\n100\r\n4\r\n", "output": "0\r\n"}, {"input": "1000000000 1 1\r\n1000000000 1\r\n1\r\n10000\r\n1\r\n10000\r\n", "output": "1000000000000000000\r\n"}, {"input": "2000000000 1 1\r\n2000000000 1\r\n1\r\n10\r\n2\r\n10\r\n", "output": "4000000000000000000\r\n"}, {"input": "20 1 1\r\n10 100\r\n5\r\n200\r\n10\r\n1\r\n", "output": "100\r\n"}, {"input": "2000000000 1 1\r\n2000000000 1\r\n1999999999\r\n1\r\n1\r\n1\r\n", "output": "3999999998000000000\r\n"}, {"input": "20 3 2\r\n10 10\r\n2 4 3\r\n10 10 10\r\n20 20\r\n999 999\r\n", "output": "40\r\n"}, {"input": "20 2 2\r\n10 100\r\n1 1\r\n1000 2000\r\n4 15\r\n100 800\r\n", "output": "160\r\n"}, {"input": "2 1 1\r\n5 5\r\n2\r\n10\r\n2\r\n1\r\n", "output": "0\r\n"}, {"input": "20 3 2\r\n10 2\r\n1 1 1\r\n3 4 5\r\n1 2\r\n1 3\r\n", "output": "190\r\n"}, {"input": "20 3 1\r\n10 10\r\n9 9 9\r\n10 10 10\r\n20\r\n10\r\n", "output": "0\r\n"}, {"input": "1000000000 3 2\r\n1000000000 1\r\n2 4 3\r\n20 10 40\r\n4 15\r\n10 80\r\n", "output": "1000000000000000000\r\n"}, {"input": "10 1 1\r\n10 10\r\n1\r\n20\r\n5\r\n9\r\n", "output": "50\r\n"}, {"input": "1 1 1\r\n1000 1000\r\n1\r\n1000\r\n1\r\n1\r\n", "output": "0\r\n"}, {"input": "1000000000 1 1\r\n1000000000 1\r\n1\r\n10000\r\n1000000000\r\n1\r\n", "output": "0\r\n"}, {"input": "20 1 1\r\n10 10\r\n4\r\n100\r\n20\r\n10\r\n", "output": "0\r\n"}, {"input": "100 1 1\r\n100 10000\r\n99\r\n10001\r\n100\r\n10000\r\n", "output": "0\r\n"}, {"input": "20 1 1\r\n10 100\r\n5\r\n200\r\n10\r\n100\r\n", "output": "100\r\n"}, {"input": "52 2 3\r\n50 101\r\n15 13\r\n10 20\r\n20 50 51\r\n20 100 200\r\n", "output": "100\r\n"}, {"input": "2000000000 1 1\r\n2000000000 10\r\n5\r\n15\r\n5\r\n15\r\n", "output": "4000000000000000000\r\n"}, {"input": "20 3 2\r\n10 99\r\n2 4 3\r\n99 100 400\r\n4 15\r\n100 800\r\n", "output": "40\r\n"}, {"input": "1 1 1\r\n1000 1\r\n1\r\n1000\r\n1\r\n1\r\n", "output": "0\r\n"}, {"input": "100000000 1 1\r\n100000000 1\r\n10\r\n10\r\n10\r\n10\r\n", "output": "10000000000000000\r\n"}, {"input": "1000000000 3 2\r\n1000000000 99\r\n2 4 3\r\n20 10 40\r\n4 15\r\n10 80\r\n", "output": "1999999992\r\n"}, {"input": "100 1 1\r\n1000 5\r\n1\r\n6\r\n95\r\n4\r\n", "output": "5000\r\n"}, {"input": "1 1 1\r\n2 1\r\n1\r\n10\r\n1\r\n1\r\n", "output": "0\r\n"}, {"input": "50 1 1\r\n10 10\r\n8\r\n11\r\n50\r\n10\r\n", "output": "0\r\n"}, {"input": "2000000000 1 1\r\n2000000000 1\r\n1\r\n10\r\n1\r\n10\r\n", "output": "4000000000000000000\r\n"}, {"input": "10 1 1\r\n10 10\r\n5\r\n5\r\n7\r\n10\r\n", "output": "30\r\n"}, {"input": "2000000000 1 1\r\n2000000000 1\r\n200000000\r\n2000000000\r\n2000000000\r\n2000000000\r\n", "output": "4000000000000000000\r\n"}, {"input": "2000000000 1 1\r\n2000000000 1\r\n4\r\n100\r\n20\r\n100\r\n", "output": "4000000000000000000\r\n"}, {"input": "100 1 1\r\n1000 5\r\n2\r\n6\r\n95\r\n4\r\n", "output": "5000\r\n"}, {"input": "10 1 2\r\n10 10\r\n5\r\n6\r\n5 7\r\n4 4\r\n", "output": "15\r\n"}, {"input": "1000000000 1 1\r\n1000000000 1\r\n1\r\n1000000\r\n1\r\n100000000\r\n", "output": "1000000000000000000\r\n"}, {"input": "2000000000 1 1\r\n2000000000 5\r\n2\r\n6\r\n2\r\n6\r\n", "output": "4000000000000000000\r\n"}, {"input": "2000000000 1 1\r\n2000000000 1\r\n100\r\n100\r\n100\r\n100\r\n", "output": "4000000000000000000\r\n"}, {"input": "20 3 2\r\n10 99\r\n2 4 3\r\n20 10 40\r\n20 20\r\n99 99\r\n", "output": "0\r\n"}, {"input": "10 2 2\r\n10 15\r\n5 7\r\n16 16\r\n5 10\r\n5 10\r\n", "output": "0\r\n"}, {"input": "1000000000 1 1\r\n1000000000 10\r\n999999991\r\n1\r\n1\r\n10000\r\n", "output": "999999991000000000\r\n"}, {"input": "1000000000 1 1\r\n1000000000 2\r\n999999999\r\n1\r\n1\r\n1\r\n", "output": "999999998000000001\r\n"}, {"input": "20 3 2\r\n2000000000 99\r\n2 4 3\r\n200 100 400\r\n4 15\r\n100 800\r\n", "output": "40000000000\r\n"}]
false
stdio
null
true
982/A
982
A
Python 3
TESTS
26
93
0
39745914
n=int(input()) s=input() if n==1: if s=='1': print('Yes') else: print('No') elif n==2: if s=='11': print('No') else: print('Yes') elif n==3: if s=='001' or s=='100'or s=='111': print('No') else: print('Yes') elif s.startswith('001') or s.endswith('100'): print('No') else: x,y=s.find('11'),s.find('000') if x!=-1 or y!=-1: print('No') else: print('Yes')
55
46
0
198505201
input() s='0'+input()+'0' print('No' if '000' in s or '11' in s else 'Yes')
Codeforces Round 484 (Div. 2)
CF
2,018
1
256
Row
You're given a row with $$$n$$$ chairs. We call a seating of people "maximal" if the two following conditions hold: 1. There are no neighbors adjacent to anyone seated. 2. It's impossible to seat one more person without violating the first rule. The seating is given as a string consisting of zeros and ones ($$$0$$$ means that the corresponding seat is empty, $$$1$$$ — occupied). The goal is to determine whether this seating is "maximal". Note that the first and last seats are not adjacent (if $$$n \ne 2$$$).
The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of chairs. The next line contains a string of $$$n$$$ characters, each of them is either zero or one, describing the seating.
Output "Yes" (without quotation marks) if the seating is "maximal". Otherwise print "No". You are allowed to print letters in whatever case you'd like (uppercase or lowercase).
null
In sample case one the given seating is maximal. In sample case two the person at chair three has a neighbour to the right. In sample case three it is possible to seat yet another person into chair three.
[{"input": "3\n101", "output": "Yes"}, {"input": "4\n1011", "output": "No"}, {"input": "5\n10001", "output": "No"}]
1,200
["brute force", "constructive algorithms"]
55
[{"input": "3\r\n101\r\n", "output": "Yes\r\n"}, {"input": "4\r\n1011\r\n", "output": "No\r\n"}, {"input": "5\r\n10001\r\n", "output": "No\r\n"}, {"input": "1\r\n0\r\n", "output": "No\r\n"}, {"input": "1\r\n1\r\n", "output": "Yes\r\n"}, {"input": "100\r\n0101001010101001010010010101001010100101001001001010010101010010101001001010101001001001010100101010\r\n", "output": "Yes\r\n"}, {"input": "4\r\n0100\r\n", "output": "No\r\n"}, {"input": "42\r\n011000100101001001101011011010100010011010\r\n", "output": "No\r\n"}, {"input": "3\r\n001\r\n", "output": "No\r\n"}, {"input": "64\r\n1001001010010010100101010010010100100101001001001001010100101001\r\n", "output": "Yes\r\n"}, {"input": "3\r\n111\r\n", "output": "No\r\n"}, {"input": "4\r\n0000\r\n", "output": "No\r\n"}, {"input": "4\r\n0001\r\n", "output": "No\r\n"}, {"input": "4\r\n0010\r\n", "output": "No\r\n"}, {"input": "4\r\n0011\r\n", "output": "No\r\n"}, {"input": "4\r\n0101\r\n", "output": "Yes\r\n"}, {"input": "4\r\n0110\r\n", "output": "No\r\n"}, {"input": "4\r\n0111\r\n", "output": "No\r\n"}, {"input": "4\r\n1000\r\n", "output": "No\r\n"}, {"input": "4\r\n1001\r\n", "output": "Yes\r\n"}, {"input": "4\r\n1010\r\n", "output": "Yes\r\n"}, {"input": "4\r\n1100\r\n", "output": "No\r\n"}, {"input": "4\r\n1101\r\n", "output": "No\r\n"}, {"input": "4\r\n1110\r\n", "output": "No\r\n"}, {"input": "4\r\n1111\r\n", "output": "No\r\n"}, {"input": "2\r\n00\r\n", "output": "No\r\n"}, {"input": "2\r\n01\r\n", "output": "Yes\r\n"}, {"input": "2\r\n10\r\n", "output": "Yes\r\n"}, {"input": "2\r\n11\r\n", "output": "No\r\n"}, {"input": "3\r\n000\r\n", "output": "No\r\n"}, {"input": "3\r\n010\r\n", "output": "Yes\r\n"}, {"input": "3\r\n011\r\n", "output": "No\r\n"}, {"input": "3\r\n100\r\n", "output": "No\r\n"}, {"input": "3\r\n110\r\n", "output": "No\r\n"}, {"input": "100\r\n0111001010101110001100000010011000100101110010001100111110101110001110101010111000010010011000000110\r\n", "output": "No\r\n"}, {"input": "357\r\n100101010010010010010100101001001010101010100100100100101001010101001010010100101001010100101001010010100100101001010101010101001001010100101010010100101001010100100100101010010010010100101010010010101001010010010101001001010010010101010100100101010010100100101001010100101001010100101001010010010010100101001010100100100100100100100101010101010010010100101\r\n", "output": "Yes\r\n"}, {"input": "459\r\n000111000101010000100001001010111110011011010001100101111010111011101110111101111101100101100011011001100110001001111001101000111001011100110100011111011111000010000110010011100110011011111110011100001101001111000100111011001000001011111100110100001001001100101011100001110110100101011011110100100111101011000101110000100110100100010000000100001001111111000011101010010011001111010111001100000100111001010111011010000011000011100101101011101000011011000110011\r\n", "output": "No\r\n"}, {"input": "3\r\n001\r\n", "output": "No\r\n"}, {"input": "4\r\n0010\r\n", "output": "No\r\n"}, {"input": "3\r\n111\r\n", "output": "No\r\n"}, {"input": "4\r\n0110\r\n", "output": "No\r\n"}, {"input": "2\r\n11\r\n", "output": "No\r\n"}, {"input": "2\r\n10\r\n", "output": "Yes\r\n"}, {"input": "2\r\n01\r\n", "output": "Yes\r\n"}, {"input": "4\r\n1101\r\n", "output": "No\r\n"}, {"input": "2\r\n00\r\n", "output": "No\r\n"}, {"input": "4\r\n1111\r\n", "output": "No\r\n"}, {"input": "15\r\n000010101010000\r\n", "output": "No\r\n"}, {"input": "8\r\n10010100\r\n", "output": "No\r\n"}, {"input": "5\r\n00100\r\n", "output": "No\r\n"}, {"input": "6\r\n100100\r\n", "output": "No\r\n"}, {"input": "7\r\n1000001\r\n", "output": "No\r\n"}, {"input": "98\r\n10101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010\r\n", "output": "Yes\r\n"}]
false
stdio
null
true
900/A
900
A
Python 3
TESTS
21
374
0
40556543
n=int(input()) pos=0 neg=0 for k in range(n): a,b=map(int,input().split()) if(a<0): neg+=1 else: pos+=1 if(pos>1): if(neg==0 or neg==1): print("Yes") else: print("No") else: if(neg>1): if(pos==0 or pos==1): print("Yes") else: print("No") else: print("No")
71
140
1,945,600
105619392
import sys import io, os input = sys.stdin.buffer.readline n = int(input()) XY = [0]*n cnt = 0 for i in range(n): x,y = map(int, input().split()) if x > 0: cnt += 1 if cnt == 1 or n-cnt == 1 or n-cnt == 0 or cnt ==0: print('Yes') else: print('No')
Codeforces Round 450 (Div. 2)
CF
2,017
1
256
Find Extra One
You have n distinct points on a plane, none of them lie on OY axis. Check that there is a point after removal of which the remaining points are located on one side of the OY axis.
The first line contains a single positive integer n (2 ≤ n ≤ 105). The following n lines contain coordinates of the points. The i-th of these lines contains two single integers xi and yi (|xi|, |yi| ≤ 109, xi ≠ 0). No two points coincide.
Print "Yes" if there is such a point, "No" — otherwise. You can print every letter in any case (upper or lower).
null
In the first example the second point can be removed. In the second example there is no suitable for the condition point. In the third example any point can be removed.
[{"input": "3\n1 1\n-1 -1\n2 -1", "output": "Yes"}, {"input": "4\n1 1\n2 2\n-1 1\n-2 2", "output": "No"}, {"input": "3\n1 2\n2 1\n4 60", "output": "Yes"}]
800
["geometry", "implementation"]
71
[{"input": "3\r\n1 1\r\n-1 -1\r\n2 -1\r\n", "output": "Yes"}, {"input": "4\r\n1 1\r\n2 2\r\n-1 1\r\n-2 2\r\n", "output": "No"}, {"input": "3\r\n1 2\r\n2 1\r\n4 60\r\n", "output": "Yes"}, {"input": "10\r\n1 1\r\n2 2\r\n3 3\r\n4 4\r\n5 5\r\n6 6\r\n7 7\r\n8 8\r\n9 9\r\n-1 -1\r\n", "output": "Yes"}, {"input": "2\r\n1000000000 -1000000000\r\n1000000000 1000000000\r\n", "output": "Yes"}, {"input": "23\r\n-1 1\r\n-1 2\r\n-2 4\r\n-7 -8\r\n-3 3\r\n-9 -14\r\n-5 3\r\n-6 2\r\n-7 11\r\n-4 4\r\n-8 5\r\n1 1\r\n-1 -1\r\n-1 -2\r\n-2 -4\r\n-7 8\r\n-3 -3\r\n-9 14\r\n-5 -3\r\n-6 -2\r\n-7 -11\r\n-4 -4\r\n-8 -5\r\n", "output": "Yes"}, {"input": "4\r\n-1000000000 -1000000000\r\n1000000000 1000000000\r\n-1000000000 1000000000\r\n1000000000 -1000000000\r\n", "output": "No"}, {"input": "2\r\n-1000000000 1000000000\r\n-1000000000 -1000000000\r\n", "output": "Yes"}, {"input": "5\r\n-1 -1\r\n-2 2\r\n2 2\r\n2 -2\r\n3 2\r\n", "output": "No"}, {"input": "2\r\n1 0\r\n-1 0\r\n", "output": "Yes"}, {"input": "4\r\n-1 1\r\n-1 2\r\n-1 3\r\n-1 4\r\n", "output": "Yes"}, {"input": "2\r\n-1 0\r\n1 0\r\n", "output": "Yes"}, {"input": "2\r\n1 2\r\n-1 2\r\n", "output": "Yes"}, {"input": "2\r\n8 0\r\n7 0\r\n", "output": "Yes"}, {"input": "6\r\n-1 0\r\n-2 0\r\n-1 -1\r\n-1 5\r\n1 0\r\n1 1\r\n", "output": "No"}, {"input": "4\r\n1 0\r\n2 0\r\n-1 0\r\n-2 0\r\n", "output": "No"}, {"input": "4\r\n-2 0\r\n-1 0\r\n1 0\r\n2 0\r\n", "output": "No"}, {"input": "2\r\n1 1\r\n-1 1\r\n", "output": "Yes"}, {"input": "4\r\n-1 0\r\n-2 0\r\n1 0\r\n2 0\r\n", "output": "No"}, {"input": "2\r\n4 3\r\n-4 -2\r\n", "output": "Yes"}, {"input": "4\r\n1 0\r\n2 0\r\n-1 1\r\n-1 2\r\n", "output": "No"}, {"input": "5\r\n1 1\r\n2 1\r\n3 1\r\n-1 1\r\n-2 1\r\n", "output": "No"}, {"input": "2\r\n1 1\r\n-1 -1\r\n", "output": "Yes"}, {"input": "4\r\n1 2\r\n1 0\r\n1 -2\r\n-1 2\r\n", "output": "Yes"}, {"input": "5\r\n-2 3\r\n-3 3\r\n4 2\r\n3 2\r\n1 2\r\n", "output": "No"}, {"input": "3\r\n2 0\r\n3 0\r\n4 0\r\n", "output": "Yes"}, {"input": "5\r\n-3 1\r\n-2 1\r\n-1 1\r\n1 1\r\n2 1\r\n", "output": "No"}, {"input": "4\r\n-3 0\r\n1 0\r\n2 0\r\n3 0\r\n", "output": "Yes"}, {"input": "2\r\n1 0\r\n-1 1\r\n", "output": "Yes"}, {"input": "3\r\n-1 0\r\n1 0\r\n2 0\r\n", "output": "Yes"}, {"input": "5\r\n1 0\r\n3 0\r\n-1 0\r\n-6 0\r\n-4 1\r\n", "output": "No"}, {"input": "5\r\n-1 2\r\n-2 2\r\n-3 1\r\n1 2\r\n2 3\r\n", "output": "No"}, {"input": "3\r\n1 0\r\n-1 0\r\n-2 0\r\n", "output": "Yes"}, {"input": "4\r\n1 0\r\n2 0\r\n3 1\r\n4 1\r\n", "output": "Yes"}, {"input": "4\r\n1 0\r\n1 2\r\n1 3\r\n-1 5\r\n", "output": "Yes"}, {"input": "4\r\n2 2\r\n2 5\r\n-2 3\r\n-2 0\r\n", "output": "No"}, {"input": "4\r\n1 1\r\n-1 1\r\n-1 0\r\n-1 -1\r\n", "output": "Yes"}, {"input": "4\r\n2 0\r\n3 0\r\n-3 -3\r\n-3 -4\r\n", "output": "No"}, {"input": "4\r\n-1 0\r\n-2 0\r\n-3 0\r\n-4 0\r\n", "output": "Yes"}, {"input": "2\r\n-1 1\r\n1 1\r\n", "output": "Yes"}, {"input": "5\r\n1 1\r\n2 2\r\n3 3\r\n-4 -4\r\n-5 -5\r\n", "output": "No"}, {"input": "5\r\n2 0\r\n3 0\r\n4 0\r\n5 0\r\n6 0\r\n", "output": "Yes"}, {"input": "2\r\n-1 2\r\n1 2\r\n", "output": "Yes"}, {"input": "4\r\n1 1\r\n2 1\r\n-3 0\r\n-4 0\r\n", "output": "No"}, {"input": "4\r\n-1 0\r\n-2 0\r\n3 0\r\n4 0\r\n", "output": "No"}, {"input": "3\r\n3 0\r\n2 0\r\n1 0\r\n", "output": "Yes"}, {"input": "4\r\n-2 0\r\n-3 0\r\n1 -1\r\n3 1\r\n", "output": "No"}, {"input": "3\r\n-1 -1\r\n1 1\r\n2 2\r\n", "output": "Yes"}, {"input": "4\r\n-2 0\r\n-1 0\r\n2 0\r\n1 0\r\n", "output": "No"}, {"input": "2\r\n-3 5\r\n3 5\r\n", "output": "Yes"}, {"input": "2\r\n-1 5\r\n1 5\r\n", "output": "Yes"}, {"input": "4\r\n2 0\r\n3 0\r\n-2 0\r\n-3 0\r\n", "output": "No"}, {"input": "3\r\n-1 1\r\n1 1\r\n1 -1\r\n", "output": "Yes"}, {"input": "2\r\n1 0\r\n2 0\r\n", "output": "Yes"}, {"input": "4\r\n-1 1\r\n-2 1\r\n2 -1\r\n3 -1\r\n", "output": "No"}, {"input": "5\r\n1 0\r\n2 0\r\n3 0\r\n-1 0\r\n-2 0\r\n", "output": "No"}, {"input": "4\r\n-3 0\r\n-4 0\r\n-5 0\r\n-6 0\r\n", "output": "Yes"}, {"input": "6\r\n-3 0\r\n-2 0\r\n-1 0\r\n1 0\r\n2 0\r\n3 0\r\n", "output": "No"}, {"input": "4\r\n5 0\r\n5 1\r\n6 0\r\n6 1\r\n", "output": "Yes"}]
false
stdio
null
true
828/A
828
A
Python 3
TESTS
11
62
2,969,600
144463000
n,a,b = input().split(" ") n = int(n) a = int(a) b = int(b) denies = 0 for x in input().split(" ") : x = int(x) if(x == 1) : if(a > 0) : a -=1 else : if(b > 0) : b -= 1 a += 1 else : denies += 1 else : if(b > 0) : b -= 1 else : denies += x print(denies)
97
93
5,632,000
205356994
count=0 tabs = list(map(int, input().split())) tabones=tabs[1] tabtwos=tabs[2] e=0 nums=list(map(int, input().split())) for i in nums: if i==1: if tabones>0: tabones-=1 elif tabtwos>0: tabtwos-=1 e+=1 elif e>0: e-=1 else: count+=1 else: if tabtwos>0: tabtwos-=1 else: count+=2 print(count)
Codeforces Round 423 (Div. 2, rated, based on VK Cup Finals)
CF
2,017
1
256
Restaurant Tables
In a small restaurant there are a tables for one person and b tables for two persons. It it known that n groups of people come today, each consisting of one or two people. If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group. If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group. You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.
The first line contains three integers n, a and b (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables. The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.
Print the total number of people the restaurant denies service to.
null
In the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served. In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.
[{"input": "4 1 2\n1 2 1 1", "output": "0"}, {"input": "4 1 1\n1 1 2 1", "output": "2"}]
1,200
["implementation"]
97
[{"input": "4 1 2\r\n1 2 1 1\r\n", "output": "0\r\n"}, {"input": "4 1 1\r\n1 1 2 1\r\n", "output": "2\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "2 1 2\r\n2 2\r\n", "output": "0\r\n"}, {"input": "5 1 3\r\n1 2 2 2 1\r\n", "output": "1\r\n"}, {"input": "7 6 1\r\n1 1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "10 2 1\r\n2 1 2 2 2 2 1 2 1 2\r\n", "output": "13\r\n"}, {"input": "20 4 3\r\n2 2 2 2 2 2 2 2 1 2 1 1 2 2 1 2 2 2 1 2\r\n", "output": "25\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "1 1 1\r\n2\r\n", "output": "0\r\n"}, {"input": "1 200000 200000\r\n2\r\n", "output": "0\r\n"}, {"input": "30 10 10\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2\r\n", "output": "20\r\n"}, {"input": "4 1 2\r\n1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 2 3\r\n1 2 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 4\r\n1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "6 1 3\r\n1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 4 2\r\n2 1 2 2 1 1\r\n", "output": "2\r\n"}, {"input": "3 10 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "5 1 3\r\n1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "5 2 2\r\n1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "15 5 5\r\n1 1 1 1 1 1 1 1 1 1 2 2 2 2 2\r\n", "output": "10\r\n"}, {"input": "5 1 2\r\n1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "3 6 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "5 3 3\r\n2 2 2 2 2\r\n", "output": "4\r\n"}, {"input": "8 3 3\r\n1 1 1 1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "5 1 2\r\n1 1 1 2 1\r\n", "output": "2\r\n"}, {"input": "6 1 4\r\n1 2 2 1 2 2\r\n", "output": "2\r\n"}, {"input": "2 1 1\r\n2 2\r\n", "output": "2\r\n"}, {"input": "2 2 1\r\n2 2\r\n", "output": "2\r\n"}, {"input": "5 8 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "3 1 4\r\n1 1 2\r\n", "output": "0\r\n"}, {"input": "7 1 5\r\n1 1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 2 1 1\r\n", "output": "0\r\n"}, {"input": "6 1 2\r\n1 1 1 2 2 2\r\n", "output": "6\r\n"}, {"input": "8 1 4\r\n2 1 1 1 2 2 2 2\r\n", "output": "6\r\n"}, {"input": "4 2 3\r\n2 2 2 2\r\n", "output": "2\r\n"}, {"input": "3 1 1\r\n1 1 2\r\n", "output": "2\r\n"}, {"input": "5 1 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "10 1 5\r\n1 1 1 1 1 2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "5 1 2\r\n1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "4 1 1\r\n1 1 2 2\r\n", "output": "4\r\n"}, {"input": "7 1 2\r\n1 1 1 1 1 1 1\r\n", "output": "2\r\n"}, {"input": "5 1 4\r\n2 2 2 2 2\r\n", "output": "2\r\n"}, {"input": "6 2 3\r\n1 1 1 1 2 2\r\n", "output": "2\r\n"}, {"input": "5 2 2\r\n2 1 2 1 2\r\n", "output": "2\r\n"}, {"input": "4 6 1\r\n2 2 2 2\r\n", "output": "6\r\n"}, {"input": "6 1 4\r\n1 1 2 1 1 2\r\n", "output": "2\r\n"}, {"input": "7 1 3\r\n1 1 1 1 2 2 2\r\n", "output": "6\r\n"}, {"input": "4 1 2\r\n1 1 2 2\r\n", "output": "2\r\n"}, {"input": "3 1 2\r\n1 1 2\r\n", "output": "0\r\n"}, {"input": "6 1 3\r\n1 2 1 1 2 1\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 2 2 2\r\n", "output": "4\r\n"}, {"input": "10 2 2\r\n1 1 1 1 2 2 2 2 2 2\r\n", "output": "12\r\n"}, {"input": "10 1 4\r\n1 1 1 1 1 2 2 2 2 2\r\n", "output": "10\r\n"}, {"input": "3 10 2\r\n2 2 2\r\n", "output": "2\r\n"}, {"input": "4 3 1\r\n1 2 2 2\r\n", "output": "4\r\n"}, {"input": "7 1 4\r\n1 1 1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "3 4 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "4 1 2\r\n2 1 1 2\r\n", "output": "2\r\n"}, {"input": "10 1 2\r\n1 1 1 1 1 1 1 1 1 2\r\n", "output": "6\r\n"}, {"input": "5 1 3\r\n1 1 2 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 1 2 1\r\n", "output": "2\r\n"}, {"input": "6 1 4\r\n1 1 1 2 2 2\r\n", "output": "2\r\n"}, {"input": "7 1 2\r\n1 2 1 1 1 1 1\r\n", "output": "3\r\n"}, {"input": "6 2 2\r\n1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "6 1 2\r\n1 1 2 1 1 1\r\n", "output": "2\r\n"}, {"input": "3 3 1\r\n2 2 1\r\n", "output": "2\r\n"}, {"input": "8 4 2\r\n1 1 1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "9 1 4\r\n1 1 1 1 1 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "5 10 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "3 5 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "5 100 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "4 1 2\r\n1 1 1 1\r\n", "output": "0\r\n"}, {"input": "4 1 1\r\n1 1 1 1\r\n", "output": "1\r\n"}, {"input": "7 2 2\r\n1 1 1 1 1 1 1\r\n", "output": "1\r\n"}]
false
stdio
null
true
828/A
828
A
PyPy 3
TESTS
11
109
8,806,400
106502772
n,a,b=map(int,input().split()) c=input().split() fa=a fb=b fc=0 denied=0 for i in range(len(c)): if(c[i]=='1'): if(fa==0): if(fb>0 and fc==0): fc+=1 fb-=1 elif(fc==1): fc-=1 else: denied+=1 else: fa-=1 else: if(fb==0): denied+=2 else: fb-=1 print(denied)
97
93
5,734,400
233007891
def mainFunc(): n, a, b = map(int, input().split()) group = list(map(int, input().split()))[:n] singleSeaters = {1:a,2:2*b} doubleSeaters = {2:b} deniedCount = 0 for i in group: if i==1: if singleSeaters[1]>0: singleSeaters[1] -=1 else: if singleSeaters[2]>0: singleSeaters[2] -=1 doubleSeaters[2] -=1 else: deniedCount +=1 else: if doubleSeaters[2]>0: doubleSeaters[2] -=1 singleSeaters[2] -=2 else: deniedCount += 2 print(deniedCount) mainFunc()
Codeforces Round 423 (Div. 2, rated, based on VK Cup Finals)
CF
2,017
1
256
Restaurant Tables
In a small restaurant there are a tables for one person and b tables for two persons. It it known that n groups of people come today, each consisting of one or two people. If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group. If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group. You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.
The first line contains three integers n, a and b (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables. The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.
Print the total number of people the restaurant denies service to.
null
In the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served. In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.
[{"input": "4 1 2\n1 2 1 1", "output": "0"}, {"input": "4 1 1\n1 1 2 1", "output": "2"}]
1,200
["implementation"]
97
[{"input": "4 1 2\r\n1 2 1 1\r\n", "output": "0\r\n"}, {"input": "4 1 1\r\n1 1 2 1\r\n", "output": "2\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "2 1 2\r\n2 2\r\n", "output": "0\r\n"}, {"input": "5 1 3\r\n1 2 2 2 1\r\n", "output": "1\r\n"}, {"input": "7 6 1\r\n1 1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "10 2 1\r\n2 1 2 2 2 2 1 2 1 2\r\n", "output": "13\r\n"}, {"input": "20 4 3\r\n2 2 2 2 2 2 2 2 1 2 1 1 2 2 1 2 2 2 1 2\r\n", "output": "25\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "1 1 1\r\n2\r\n", "output": "0\r\n"}, {"input": "1 200000 200000\r\n2\r\n", "output": "0\r\n"}, {"input": "30 10 10\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2\r\n", "output": "20\r\n"}, {"input": "4 1 2\r\n1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 2 3\r\n1 2 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 4\r\n1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "6 1 3\r\n1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 4 2\r\n2 1 2 2 1 1\r\n", "output": "2\r\n"}, {"input": "3 10 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "5 1 3\r\n1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "5 2 2\r\n1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "15 5 5\r\n1 1 1 1 1 1 1 1 1 1 2 2 2 2 2\r\n", "output": "10\r\n"}, {"input": "5 1 2\r\n1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "3 6 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "5 3 3\r\n2 2 2 2 2\r\n", "output": "4\r\n"}, {"input": "8 3 3\r\n1 1 1 1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "5 1 2\r\n1 1 1 2 1\r\n", "output": "2\r\n"}, {"input": "6 1 4\r\n1 2 2 1 2 2\r\n", "output": "2\r\n"}, {"input": "2 1 1\r\n2 2\r\n", "output": "2\r\n"}, {"input": "2 2 1\r\n2 2\r\n", "output": "2\r\n"}, {"input": "5 8 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "3 1 4\r\n1 1 2\r\n", "output": "0\r\n"}, {"input": "7 1 5\r\n1 1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 2 1 1\r\n", "output": "0\r\n"}, {"input": "6 1 2\r\n1 1 1 2 2 2\r\n", "output": "6\r\n"}, {"input": "8 1 4\r\n2 1 1 1 2 2 2 2\r\n", "output": "6\r\n"}, {"input": "4 2 3\r\n2 2 2 2\r\n", "output": "2\r\n"}, {"input": "3 1 1\r\n1 1 2\r\n", "output": "2\r\n"}, {"input": "5 1 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "10 1 5\r\n1 1 1 1 1 2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "5 1 2\r\n1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "4 1 1\r\n1 1 2 2\r\n", "output": "4\r\n"}, {"input": "7 1 2\r\n1 1 1 1 1 1 1\r\n", "output": "2\r\n"}, {"input": "5 1 4\r\n2 2 2 2 2\r\n", "output": "2\r\n"}, {"input": "6 2 3\r\n1 1 1 1 2 2\r\n", "output": "2\r\n"}, {"input": "5 2 2\r\n2 1 2 1 2\r\n", "output": "2\r\n"}, {"input": "4 6 1\r\n2 2 2 2\r\n", "output": "6\r\n"}, {"input": "6 1 4\r\n1 1 2 1 1 2\r\n", "output": "2\r\n"}, {"input": "7 1 3\r\n1 1 1 1 2 2 2\r\n", "output": "6\r\n"}, {"input": "4 1 2\r\n1 1 2 2\r\n", "output": "2\r\n"}, {"input": "3 1 2\r\n1 1 2\r\n", "output": "0\r\n"}, {"input": "6 1 3\r\n1 2 1 1 2 1\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 2 2 2\r\n", "output": "4\r\n"}, {"input": "10 2 2\r\n1 1 1 1 2 2 2 2 2 2\r\n", "output": "12\r\n"}, {"input": "10 1 4\r\n1 1 1 1 1 2 2 2 2 2\r\n", "output": "10\r\n"}, {"input": "3 10 2\r\n2 2 2\r\n", "output": "2\r\n"}, {"input": "4 3 1\r\n1 2 2 2\r\n", "output": "4\r\n"}, {"input": "7 1 4\r\n1 1 1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "3 4 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "4 1 2\r\n2 1 1 2\r\n", "output": "2\r\n"}, {"input": "10 1 2\r\n1 1 1 1 1 1 1 1 1 2\r\n", "output": "6\r\n"}, {"input": "5 1 3\r\n1 1 2 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 1 2 1\r\n", "output": "2\r\n"}, {"input": "6 1 4\r\n1 1 1 2 2 2\r\n", "output": "2\r\n"}, {"input": "7 1 2\r\n1 2 1 1 1 1 1\r\n", "output": "3\r\n"}, {"input": "6 2 2\r\n1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "6 1 2\r\n1 1 2 1 1 1\r\n", "output": "2\r\n"}, {"input": "3 3 1\r\n2 2 1\r\n", "output": "2\r\n"}, {"input": "8 4 2\r\n1 1 1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "9 1 4\r\n1 1 1 1 1 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "5 10 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "3 5 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "5 100 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "4 1 2\r\n1 1 1 1\r\n", "output": "0\r\n"}, {"input": "4 1 1\r\n1 1 1 1\r\n", "output": "1\r\n"}, {"input": "7 2 2\r\n1 1 1 1 1 1 1\r\n", "output": "1\r\n"}]
false
stdio
null
true
416/D
416
D
PyPy 3-64
TESTS
17
62
512,000
153735486
n=int(input()) a=[int(x) for x in input().split()] def slv(a): b=[] for i in range(n): if(a[i]!=(-1)): b.append([a[i],i]) if(len(b)<=1): return 1 ans=1 ok=[] for i in range(len(b)-1): t=(b[i+1][0]-b[i][0])//(b[i+1][1]-b[i][1]) if((b[i+1][0]-b[i][0])%(b[i+1][1]-b[i][1])!=0 or (i==0 and b[0][0]-b[0][1]*(t-1)<=0) or (i==len(b)-2 and b[i+1][0]+(n-1-b[i+1][1])*t<=0)): ans+=1 ok.append(0) else: ok.append(1) ok.append(1) ok.append(1) bad=[] for i in range(n+1): bad.append(0) for i in range(len(b)-2): if(ok[i] and ok[i+1] and (b[i+1][0]-b[i][0])//(b[i+1][1]-b[i][1])!=(b[i+2][0]-b[i+1][0])//(b[i+2][1]-b[i+1][1])): bad[i]+=1 for i in range(n): ans+=bad[i] if(bad[i]==1): bad[i+1]=0 return ans print(slv(a))
84
171
27,648,000
302828817
import sys,math,heapq from collections import Counter #sys.setrecursionlimit(200000) #fin = open('input.txt', 'r') #input = lambda: fin.readline().strip() input=lambda: sys.stdin.readline().strip() import random RANDOM = random.getrandbits(20) class Wrapper(int): def __init__(self, x): int.__init__(x) def __hash__(self): return super(Wrapper, self).__hash__() ^ RANDOM def I(): return input() def II(): return int(input()) def MI(): return map(int, input().split()) def LI(): return list(input().split()) def LII(): return list(map(int, input().split())) def LFI(): return list(map(float, input().split())) def GMI(): return map(lambda x: int(x) - 1, input().split()) def LGMI(): return list(map(lambda x: int(x) - 1, input().split())) from types import GeneratorType def bootstrap(f, stack=[]): def wrappedfunc(*args, **kwargs): if stack: return f(*args, **kwargs) else: to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) else: if not stack:break stack.pop() if not stack: break to = stack[-1].send(to) return to return wrappedfunc class SortedList: def __init__(self, iterable=None, _load=200): """Initialize sorted list instance.""" if iterable is None: iterable = [] values = sorted(iterable) self._len = _len = len(values) self._load = _load self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)] self._list_lens = [len(_list) for _list in _lists] self._min_s = [_list[0] for _list in _lists] self._fen_tree = [] self._rebuild = True def _fen_build(self): """Build a fenwick tree instance.""" self._fen_tree[:] = self._list_lens _fen_tree = self._fen_tree for i in range(len(_fen_tree)): if i | i + 1 < len(_fen_tree): _fen_tree[i | i + 1] += _fen_tree[i] self._rebuild = False def _fen_update(self, index, value): """Update `fen_tree[index] += value`.""" if not self._rebuild: _fen_tree = self._fen_tree while index < len(_fen_tree): _fen_tree[index] += value index |= index + 1 def _fen_query(self, end): """Return `sum(_fen_tree[:end])`.""" if self._rebuild: self._fen_build() _fen_tree = self._fen_tree x = 0 while end: x += _fen_tree[end - 1] end &= end - 1 return x def _fen_findkth(self, k): """Return a pair of (the largest `idx` such that `sum(_fen_tree[:idx]) <= k`, `k - sum(_fen_tree[:idx])`).""" _list_lens = self._list_lens if k < _list_lens[0]: return 0, k if k >= self._len - _list_lens[-1]: return len(_list_lens) - 1, k + _list_lens[-1] - self._len if self._rebuild: self._fen_build() _fen_tree = self._fen_tree idx = -1 for d in reversed(range(len(_fen_tree).bit_length())): right_idx = idx + (1 << d) if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]: idx = right_idx k -= _fen_tree[idx] return idx + 1, k def _delete(self, pos, idx): """Delete value at the given `(pos, idx)`.""" _lists = self._lists _mins = self._min_s _list_lens = self._list_lens self._len -= 1 self._fen_update(pos, -1) del _lists[pos][idx] _list_lens[pos] -= 1 if _list_lens[pos]: _mins[pos] = _lists[pos][0] else: del _lists[pos] del _list_lens[pos] del _mins[pos] self._rebuild = True def _loc_left(self, value): """Return an index pair that corresponds to the first position of `value` in the sorted list.""" if not self._len: return 0, 0 _lists = self._lists _mins = self._min_s lo, pos = -1, len(_lists) - 1 while lo + 1 < pos: mi = (lo + pos) >> 1 if value <= _mins[mi]: pos = mi else: lo = mi if pos and value <= _lists[pos - 1][-1]: pos -= 1 _list = _lists[pos] lo, idx = -1, len(_list) while lo + 1 < idx: mi = (lo + idx) >> 1 if value <= _list[mi]: idx = mi else: lo = mi return pos, idx def _loc_right(self, value): """Return an index pair that corresponds to the last position of `value` in the sorted list.""" if not self._len: return 0, 0 _lists = self._lists _mins = self._min_s pos, hi = 0, len(_lists) while pos + 1 < hi: mi = (pos + hi) >> 1 if value < _mins[mi]: hi = mi else: pos = mi _list = _lists[pos] lo, idx = -1, len(_list) while lo + 1 < idx: mi = (lo + idx) >> 1 if value < _list[mi]: idx = mi else: lo = mi return pos, idx def add(self, value): """Add `value` to sorted list.""" _load = self._load _lists = self._lists _mins = self._min_s _list_lens = self._list_lens self._len += 1 if _lists: pos, idx = self._loc_right(value) self._fen_update(pos, 1) _list = _lists[pos] _list.insert(idx, value) _list_lens[pos] += 1 _mins[pos] = _list[0] if _load + _load < len(_list): _lists.insert(pos + 1, _list[_load:]) _list_lens.insert(pos + 1, len(_list) - _load) _mins.insert(pos + 1, _list[_load]) _list_lens[pos] = _load del _list[_load:] self._rebuild = True else: _lists.append([value]) _mins.append(value) _list_lens.append(1) self._rebuild = True def discard(self, value): """Remove `value` from sorted list if it is a member.""" _lists = self._lists if _lists: pos, idx = self._loc_right(value) if idx and _lists[pos][idx - 1] == value: self._delete(pos, idx - 1) def remove(self, value): """Remove `value` from sorted list; `value` must be a member.""" _len = self._len self.discard(value) if _len == self._len: raise ValueError('{0!r} not in list'.format(value)) def pop(self, index=-1): """Remove and return value at `index` in sorted list.""" pos, idx = self._fen_findkth(self._len + index if index < 0 else index) value = self._lists[pos][idx] self._delete(pos, idx) return value def bisect_left(self, value): """Return the first index to insert `value` in the sorted list.""" pos, idx = self._loc_left(value) return self._fen_query(pos) + idx def bisect_right(self, value): """Return the last index to insert `value` in the sorted list.""" pos, idx = self._loc_right(value) return self._fen_query(pos) + idx def count(self, value): """Return number of occurrences of `value` in the sorted list.""" return self.bisect_right(value) - self.bisect_left(value) def __len__(self): """Return the size of the sorted list.""" return self._len def __getitem__(self, index): """Lookup value at `index` in sorted list.""" pos, idx = self._fen_findkth(self._len + index if index < 0 else index) return self._lists[pos][idx] def __delitem__(self, index): """Remove value at `index` from sorted list.""" pos, idx = self._fen_findkth(self._len + index if index < 0 else index) self._delete(pos, idx) def __contains__(self, value): """Return true if `value` is an element of the sorted list.""" _lists = self._lists if _lists: pos, idx = self._loc_left(value) return idx < len(_lists[pos]) and _lists[pos][idx] == value return False def __iter__(self): """Return an iterator over the sorted list.""" return (value for _list in self._lists for value in _list) def __reversed__(self): """Return a reverse iterator over the sorted list.""" return (value for _list in reversed(self._lists) for value in reversed(_list)) def __repr__(self): """Return strings representation of sorted list.""" return 'SortedList({0})'.format(list(self)) class RangeAscendRangeMax: def __init__(self, n): self.n = n self.cover = [-math.inf] * (4 * n) self.lazy = [-math.inf] * (4 * n) @staticmethod def _max(a, b): return a if a > b else b def _make_tag(self, i, val) -> None: self.cover[i] = self._max(self.cover[i], val) self.lazy[i] = self._max(self.lazy[i], val) return def _push_up(self, i): self.cover[i] = self._max(self.cover[2 * i], self.cover[2 * i + 1]) return def _push_down(self, i): if self.lazy[i] != -math.inf: self.cover[2 * i] = self._max(self.cover[2 * i], self.lazy[i]) self.cover[2 * i + 1] = self._max(self.cover[2 * i + 1], self.lazy[i]) self.lazy[2 * i] = self._max(self.lazy[2 * i], self.lazy[i]) self.lazy[2 * i + 1] = self._max(self.lazy[2 * i + 1], self.lazy[i]) self.lazy[i] = -math.inf return def build(self, nums) -> None: assert self.n == len(nums) stack = [(0, self.n - 1, 1)] while stack: s, t, ind = stack.pop() if ind >= 0: if s == t: self._make_tag(ind, nums[s]) else: stack.append([s, t, ~ind]) m = s + (t - s) // 2 stack.append([s, m, 2 * ind]) stack.append([m + 1, t, 2 * ind + 1]) else: ind = ~ind self._push_up(ind) return def get(self): stack = [(0, self.n - 1, 1)] nums = [0] * self.n while stack: s, t, i = stack.pop() if s == t: nums[s] = self.cover[i] continue m = s + (t - s) // 2 self._push_down(i) stack.append((s, m, 2 * i)) stack.append((m + 1, t, 2 * i + 1)) return nums def range_ascend(self, left, right, val): # update the range ascend assert 0 <= left <= right <= self.n - 1 stack = [(0, self.n - 1, 1)] while stack: a, b, i = stack.pop() if i >= 0: if left <= a and b <= right: self._make_tag(i, val) continue self._push_down(i) stack.append([a, b, ~i]) m = a + (b - a) // 2 if left <= m: stack.append([a, m, 2 * i]) if right > m: stack.append([m + 1, b, 2 * i + 1]) else: i = ~i self._push_up(i) return def range_max(self, left, right): # query the range max assert 0 <= left <= right <= self.n - 1 stack = [(0, self.n - 1, 1)] highest = -math.inf while stack: a, b, i = stack.pop() if left <= a and b <= right: highest = self._max(highest, self.cover[i]) continue self._push_down(i) m = a + (b - a) // 2 if left <= m: stack.append([a, m, 2 * i]) if right > m: stack.append([m + 1, b, 2 * i + 1]) return highest class SegmentTree: #LUOGU 3372 def __init__(self,num_of_elements,initial_lst): self.tree=[0]*(4*num_of_elements) #self.lazy=[0]*(4*num_of_elements) self.lst=initial_lst def build(self,s,t,p): if s==t: self.tree[p]=self.lst[s] return m=s+((t-s)>>1) self.build(s,m,p*2) self.build(m+1,t,p*2+1) self.tree[p]=math.gcd(self.tree[p*2],self.tree[p*2+1]) def query(self,l,r,s,t,p): #print(l,r,s,t) if l<=s and r>=t: return self.tree[p] m=s+((t-s)>>1) csum=0 if l<=m: csum=self.query(l,r,s,m,p*2) if r>m: if csum!=0: csum=math.gcd(csum,self.query(l,r,m+1,t,p*2+1)) else: csum=self.query(l,r,m+1,t,p*2+1) return csum from collections import deque import bisect mod=998244353 class Factorial: def __init__(self, N, mod) -> None: N += 1 self.mod = mod self.f = [1 for _ in range(N)] self.g = [1 for _ in range(N)] for i in range(1, N): self.f[i] = self.f[i - 1] * i % self.mod self.g[-1] = pow(self.f[-1], mod - 2, mod) for i in range(N - 2, -1, -1): self.g[i] = self.g[i + 1] * (i + 1) % self.mod def fac(self, n): return self.f[n] def fac_inv(self, n): return self.g[n] def combi(self, n, m): if n < m or m < 0 or n < 0: return 0 return self.f[n] * self.g[m] % self.mod * self.g[n - m] % self.mod def permu(self, n, m): if n < m or m < 0 or n < 0: return 0 return self.f[n] * self.g[n - m] % self.mod def catalan(self, n): return (self.combi(2 * n, n) - self.combi(2 * n, n - 1)) % self.mod def inv(self, n): return self.f[n-1] * self.g[n] % self.mod def main(): n = II() nums = LII() start = 0 i1 = i2 = -1 ans = 1 for i in range(n): if nums[i] == -1: if i2 != -1: v = nums[i1] + (nums[i2] - nums[i1]) // (i2 - i1) * (i - i1) if v <= 0: ans += 1 start = i i1 = -1 i2 = -1 else: if i1 == -1: i1 = i elif i2 == -1: if (nums[i] - nums[i1]) % (i - i1) or nums[i1] + (nums[i] - nums[i1]) // (i - i1) * (start - i1) <= 0: ans += 1 start = i i1 = i i2 = -1 else: i2 = i else: v = nums[i1] + (nums[i2] - nums[i1]) // (i2 - i1) * (i - i1) if nums[i] != v: ans += 1 start = i i1 = i i2 = -1 print(ans) t=1 #t=II() for i in range(t): w = main() #print(w) #fout.write(str(w))
Codeforces Round 241 (Div. 2)
CF
2,014
1
256
Population Size
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 a1, a2, ..., an, 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 ai ​​may be unknown. Such values are represented by number -1. For a given sequence a = (a1, a2, ..., an), 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 ai > 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 ci + 1 - ci 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 a1, a2, ..., an separated by a space (1 ≤ ai ≤ 109 or ai = - 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.
null
null
[{"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"}]
2,400
["greedy", "implementation", "math"]
84
[{"input": "9\r\n8 6 4 2 1 4 7 10 2\r\n", "output": "3\r\n"}, {"input": "9\r\n-1 6 -1 2 -1 4 7 -1 2\r\n", "output": "3\r\n"}, {"input": "5\r\n-1 -1 -1 -1 -1\r\n", "output": "1\r\n"}, {"input": "7\r\n-1 -1 4 5 1 2 3\r\n", "output": "2\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n65\r\n", "output": "1\r\n"}, {"input": "1\r\n1000000000\r\n", "output": "1\r\n"}, {"input": "1\r\n-1\r\n", "output": "1\r\n"}, {"input": "2\r\n1000000000 1000000000\r\n", "output": "1\r\n"}, {"input": "2\r\n1000000000 -1\r\n", "output": "1\r\n"}, {"input": "2\r\n-1 1000000000\r\n", "output": "1\r\n"}, {"input": "2\r\n-1 -1\r\n", "output": "1\r\n"}, {"input": "3\r\n999999999 1000000000 -1\r\n", "output": "1\r\n"}, {"input": "3\r\n999999999 -1 1000000000\r\n", "output": "2\r\n"}, {"input": "3\r\n1000000000 999999999 1000000000\r\n", "output": "2\r\n"}, {"input": "3\r\n-1 1000000000 999999999\r\n", "output": "1\r\n"}, {"input": "3\r\n-1 1000000000 -1\r\n", "output": "1\r\n"}, {"input": "3\r\n-1 1 2\r\n", "output": "2\r\n"}, {"input": "3\r\n-1 1 1000000000\r\n", "output": "2\r\n"}, {"input": "5\r\n-1 1 7 -1 5\r\n", "output": "2\r\n"}, {"input": "7\r\n-1 2 4 -1 4 1 5\r\n", "output": "3\r\n"}, {"input": "2\r\n-1 21\r\n", "output": "1\r\n"}, {"input": "3\r\n39 42 -1\r\n", "output": "1\r\n"}, {"input": "4\r\n45 -1 41 -1\r\n", "output": "1\r\n"}, {"input": "5\r\n-1 40 42 -1 46\r\n", "output": "1\r\n"}, {"input": "6\r\n-1 6 1 -1 -1 -1\r\n", "output": "2\r\n"}, {"input": "7\r\n32 33 34 -1 -1 37 38\r\n", "output": "1\r\n"}, {"input": "8\r\n-1 12 14 16 18 20 -1 -1\r\n", "output": "1\r\n"}, {"input": "9\r\n42 39 36 33 -1 -1 -1 34 39\r\n", "output": "2\r\n"}, {"input": "10\r\n29 27 -1 23 42 -1 -1 45 -1 -1\r\n", "output": "2\r\n"}, {"input": "5\r\n40 -1 44 46 48\r\n", "output": "1\r\n"}, {"input": "6\r\n43 40 37 34 -1 -1\r\n", "output": "1\r\n"}, {"input": "7\r\n11 8 5 -1 -1 -1 -1\r\n", "output": "2\r\n"}, {"input": "8\r\n-1 12 14 16 18 20 -1 -1\r\n", "output": "1\r\n"}, {"input": "9\r\n42 39 36 33 -1 -1 -1 34 39\r\n", "output": "2\r\n"}, {"input": "10\r\n29 27 -1 23 42 -1 -1 45 -1 -1\r\n", "output": "2\r\n"}, {"input": "11\r\n9 21 17 13 -1 -1 -1 -1 -1 -1 -1\r\n", "output": "3\r\n"}, {"input": "12\r\n-1 17 -1 54 -1 64 -1 74 79 84 -1 94\r\n", "output": "2\r\n"}, {"input": "13\r\n25 24 23 22 24 27 -1 33 -1 2 2 2 -1\r\n", "output": "3\r\n"}, {"input": "14\r\n-1 5 3 -1 -1 31 31 31 -1 31 -1 -1 4 7\r\n", "output": "3\r\n"}, {"input": "15\r\n-1 28 -1 32 34 26 -1 26 -1 -1 26 26 26 -1 -1\r\n", "output": "2\r\n"}, {"input": "16\r\n3 8 13 18 23 -1 -1 -1 43 48 53 45 -1 -1 -1 -1\r\n", "output": "2\r\n"}, {"input": "17\r\n-1 -1 -1 -1 64 68 72 -1 45 46 47 48 49 50 51 52 53\r\n", "output": "2\r\n"}, {"input": "18\r\n21 19 -1 -1 -1 48 50 -1 54 -1 5 1 -1 -1 -1 37 36 35\r\n", "output": "4\r\n"}, {"input": "19\r\n23 26 -1 -1 35 38 41 -1 -1 -1 53 -1 59 62 6 7 8 9 -1\r\n", "output": "2\r\n"}, {"input": "6\r\n-1 2 6 -1 -1 6\r\n", "output": "2\r\n"}, {"input": "8\r\n-1 -1 1 7 -1 9 5 2\r\n", "output": "3\r\n"}, {"input": "20\r\n-1 32 37 -1 -1 -1 57 -1 -1 40 31 33 -1 -1 39 47 43 -1 35 32\r\n", "output": "5\r\n"}, {"input": "13\r\n2 -1 3 1 3 1 -1 1 3 -1 -1 1 1\r\n", "output": "6\r\n"}, {"input": "3\r\n-1 1 -1\r\n", "output": "1\r\n"}]
false
stdio
null
true
828/A
828
A
Python 3
TESTS
11
93
2,969,600
139615908
n, a, b = map(int, input().split()) sum_ = 0 guest = input().split() for i in range(len(guest)): if guest[i] == '1': if a > 0: a -= 1 elif a == 0 and b > 0: a += 1 b -= 1 else: sum_ += 1 elif guest[i] == '2': if b > 0: b -= 1 else: sum_ += 2 print(sum_)
97
108
5,836,800
218100899
n, a, b = map(int, input().split()) t = [int(x) for x in input().split()] c = 0 ans = 0 for i in range(n): if t[i] == 1: if a == 0: if b == 0: if c == 0: ans += 1 else: c -= 1 else: b -= 1 c += 1 else: a -= 1 else: if b == 0: ans += 2 else: b -= 1 print(ans)
Codeforces Round 423 (Div. 2, rated, based on VK Cup Finals)
CF
2,017
1
256
Restaurant Tables
In a small restaurant there are a tables for one person and b tables for two persons. It it known that n groups of people come today, each consisting of one or two people. If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group. If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group. You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.
The first line contains three integers n, a and b (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables. The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.
Print the total number of people the restaurant denies service to.
null
In the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served. In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.
[{"input": "4 1 2\n1 2 1 1", "output": "0"}, {"input": "4 1 1\n1 1 2 1", "output": "2"}]
1,200
["implementation"]
97
[{"input": "4 1 2\r\n1 2 1 1\r\n", "output": "0\r\n"}, {"input": "4 1 1\r\n1 1 2 1\r\n", "output": "2\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "2 1 2\r\n2 2\r\n", "output": "0\r\n"}, {"input": "5 1 3\r\n1 2 2 2 1\r\n", "output": "1\r\n"}, {"input": "7 6 1\r\n1 1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "10 2 1\r\n2 1 2 2 2 2 1 2 1 2\r\n", "output": "13\r\n"}, {"input": "20 4 3\r\n2 2 2 2 2 2 2 2 1 2 1 1 2 2 1 2 2 2 1 2\r\n", "output": "25\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "1 1 1\r\n2\r\n", "output": "0\r\n"}, {"input": "1 200000 200000\r\n2\r\n", "output": "0\r\n"}, {"input": "30 10 10\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2\r\n", "output": "20\r\n"}, {"input": "4 1 2\r\n1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 2 3\r\n1 2 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 4\r\n1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "6 1 3\r\n1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 4 2\r\n2 1 2 2 1 1\r\n", "output": "2\r\n"}, {"input": "3 10 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "5 1 3\r\n1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "5 2 2\r\n1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "15 5 5\r\n1 1 1 1 1 1 1 1 1 1 2 2 2 2 2\r\n", "output": "10\r\n"}, {"input": "5 1 2\r\n1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "3 6 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "5 3 3\r\n2 2 2 2 2\r\n", "output": "4\r\n"}, {"input": "8 3 3\r\n1 1 1 1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "5 1 2\r\n1 1 1 2 1\r\n", "output": "2\r\n"}, {"input": "6 1 4\r\n1 2 2 1 2 2\r\n", "output": "2\r\n"}, {"input": "2 1 1\r\n2 2\r\n", "output": "2\r\n"}, {"input": "2 2 1\r\n2 2\r\n", "output": "2\r\n"}, {"input": "5 8 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "3 1 4\r\n1 1 2\r\n", "output": "0\r\n"}, {"input": "7 1 5\r\n1 1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 2 1 1\r\n", "output": "0\r\n"}, {"input": "6 1 2\r\n1 1 1 2 2 2\r\n", "output": "6\r\n"}, {"input": "8 1 4\r\n2 1 1 1 2 2 2 2\r\n", "output": "6\r\n"}, {"input": "4 2 3\r\n2 2 2 2\r\n", "output": "2\r\n"}, {"input": "3 1 1\r\n1 1 2\r\n", "output": "2\r\n"}, {"input": "5 1 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "10 1 5\r\n1 1 1 1 1 2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "5 1 2\r\n1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "4 1 1\r\n1 1 2 2\r\n", "output": "4\r\n"}, {"input": "7 1 2\r\n1 1 1 1 1 1 1\r\n", "output": "2\r\n"}, {"input": "5 1 4\r\n2 2 2 2 2\r\n", "output": "2\r\n"}, {"input": "6 2 3\r\n1 1 1 1 2 2\r\n", "output": "2\r\n"}, {"input": "5 2 2\r\n2 1 2 1 2\r\n", "output": "2\r\n"}, {"input": "4 6 1\r\n2 2 2 2\r\n", "output": "6\r\n"}, {"input": "6 1 4\r\n1 1 2 1 1 2\r\n", "output": "2\r\n"}, {"input": "7 1 3\r\n1 1 1 1 2 2 2\r\n", "output": "6\r\n"}, {"input": "4 1 2\r\n1 1 2 2\r\n", "output": "2\r\n"}, {"input": "3 1 2\r\n1 1 2\r\n", "output": "0\r\n"}, {"input": "6 1 3\r\n1 2 1 1 2 1\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 2 2 2\r\n", "output": "4\r\n"}, {"input": "10 2 2\r\n1 1 1 1 2 2 2 2 2 2\r\n", "output": "12\r\n"}, {"input": "10 1 4\r\n1 1 1 1 1 2 2 2 2 2\r\n", "output": "10\r\n"}, {"input": "3 10 2\r\n2 2 2\r\n", "output": "2\r\n"}, {"input": "4 3 1\r\n1 2 2 2\r\n", "output": "4\r\n"}, {"input": "7 1 4\r\n1 1 1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "3 4 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "4 1 2\r\n2 1 1 2\r\n", "output": "2\r\n"}, {"input": "10 1 2\r\n1 1 1 1 1 1 1 1 1 2\r\n", "output": "6\r\n"}, {"input": "5 1 3\r\n1 1 2 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 1 2 1\r\n", "output": "2\r\n"}, {"input": "6 1 4\r\n1 1 1 2 2 2\r\n", "output": "2\r\n"}, {"input": "7 1 2\r\n1 2 1 1 1 1 1\r\n", "output": "3\r\n"}, {"input": "6 2 2\r\n1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "6 1 2\r\n1 1 2 1 1 1\r\n", "output": "2\r\n"}, {"input": "3 3 1\r\n2 2 1\r\n", "output": "2\r\n"}, {"input": "8 4 2\r\n1 1 1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "9 1 4\r\n1 1 1 1 1 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "5 10 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "3 5 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "5 100 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "4 1 2\r\n1 1 1 1\r\n", "output": "0\r\n"}, {"input": "4 1 1\r\n1 1 1 1\r\n", "output": "1\r\n"}, {"input": "7 2 2\r\n1 1 1 1 1 1 1\r\n", "output": "1\r\n"}]
false
stdio
null
true
828/A
828
A
Python 3
TESTS
11
108
2,867,200
177532658
n, a, b = map(int, input().split()) t = map(int, input().split()) th = 0 d = 0 for i in t: if i == 1: if a > 0: a -= 1 elif th == 1: th = 0 elif b > 0: b -= 1 th = 1 else: d += 1 elif b > 0: b -= 1 else: d += 2 print(d)
97
108
22,835,200
200844190
def solve(i,j,p): ans=0 k=0 for a in p: if a==1: if i>0: i-=1 elif j>0: j-=1 k+=1 elif k>0: k-=1 else: ans+=1 elif a==2: if j>0: j-=1 else: ans+=2 return ans n,i,j=map(int,input().split()) p=list(map(int,input().split())) print(solve(i,j,p))
Codeforces Round 423 (Div. 2, rated, based on VK Cup Finals)
CF
2,017
1
256
Restaurant Tables
In a small restaurant there are a tables for one person and b tables for two persons. It it known that n groups of people come today, each consisting of one or two people. If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group. If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group. You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.
The first line contains three integers n, a and b (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables. The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.
Print the total number of people the restaurant denies service to.
null
In the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served. In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.
[{"input": "4 1 2\n1 2 1 1", "output": "0"}, {"input": "4 1 1\n1 1 2 1", "output": "2"}]
1,200
["implementation"]
97
[{"input": "4 1 2\r\n1 2 1 1\r\n", "output": "0\r\n"}, {"input": "4 1 1\r\n1 1 2 1\r\n", "output": "2\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "2 1 2\r\n2 2\r\n", "output": "0\r\n"}, {"input": "5 1 3\r\n1 2 2 2 1\r\n", "output": "1\r\n"}, {"input": "7 6 1\r\n1 1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "10 2 1\r\n2 1 2 2 2 2 1 2 1 2\r\n", "output": "13\r\n"}, {"input": "20 4 3\r\n2 2 2 2 2 2 2 2 1 2 1 1 2 2 1 2 2 2 1 2\r\n", "output": "25\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "1 1 1\r\n2\r\n", "output": "0\r\n"}, {"input": "1 200000 200000\r\n2\r\n", "output": "0\r\n"}, {"input": "30 10 10\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2\r\n", "output": "20\r\n"}, {"input": "4 1 2\r\n1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 2 3\r\n1 2 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 4\r\n1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "6 1 3\r\n1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 4 2\r\n2 1 2 2 1 1\r\n", "output": "2\r\n"}, {"input": "3 10 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "5 1 3\r\n1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "5 2 2\r\n1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "15 5 5\r\n1 1 1 1 1 1 1 1 1 1 2 2 2 2 2\r\n", "output": "10\r\n"}, {"input": "5 1 2\r\n1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "3 6 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "5 3 3\r\n2 2 2 2 2\r\n", "output": "4\r\n"}, {"input": "8 3 3\r\n1 1 1 1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "5 1 2\r\n1 1 1 2 1\r\n", "output": "2\r\n"}, {"input": "6 1 4\r\n1 2 2 1 2 2\r\n", "output": "2\r\n"}, {"input": "2 1 1\r\n2 2\r\n", "output": "2\r\n"}, {"input": "2 2 1\r\n2 2\r\n", "output": "2\r\n"}, {"input": "5 8 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "3 1 4\r\n1 1 2\r\n", "output": "0\r\n"}, {"input": "7 1 5\r\n1 1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 2 1 1\r\n", "output": "0\r\n"}, {"input": "6 1 2\r\n1 1 1 2 2 2\r\n", "output": "6\r\n"}, {"input": "8 1 4\r\n2 1 1 1 2 2 2 2\r\n", "output": "6\r\n"}, {"input": "4 2 3\r\n2 2 2 2\r\n", "output": "2\r\n"}, {"input": "3 1 1\r\n1 1 2\r\n", "output": "2\r\n"}, {"input": "5 1 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "10 1 5\r\n1 1 1 1 1 2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "5 1 2\r\n1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "4 1 1\r\n1 1 2 2\r\n", "output": "4\r\n"}, {"input": "7 1 2\r\n1 1 1 1 1 1 1\r\n", "output": "2\r\n"}, {"input": "5 1 4\r\n2 2 2 2 2\r\n", "output": "2\r\n"}, {"input": "6 2 3\r\n1 1 1 1 2 2\r\n", "output": "2\r\n"}, {"input": "5 2 2\r\n2 1 2 1 2\r\n", "output": "2\r\n"}, {"input": "4 6 1\r\n2 2 2 2\r\n", "output": "6\r\n"}, {"input": "6 1 4\r\n1 1 2 1 1 2\r\n", "output": "2\r\n"}, {"input": "7 1 3\r\n1 1 1 1 2 2 2\r\n", "output": "6\r\n"}, {"input": "4 1 2\r\n1 1 2 2\r\n", "output": "2\r\n"}, {"input": "3 1 2\r\n1 1 2\r\n", "output": "0\r\n"}, {"input": "6 1 3\r\n1 2 1 1 2 1\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 2 2 2\r\n", "output": "4\r\n"}, {"input": "10 2 2\r\n1 1 1 1 2 2 2 2 2 2\r\n", "output": "12\r\n"}, {"input": "10 1 4\r\n1 1 1 1 1 2 2 2 2 2\r\n", "output": "10\r\n"}, {"input": "3 10 2\r\n2 2 2\r\n", "output": "2\r\n"}, {"input": "4 3 1\r\n1 2 2 2\r\n", "output": "4\r\n"}, {"input": "7 1 4\r\n1 1 1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "3 4 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "4 1 2\r\n2 1 1 2\r\n", "output": "2\r\n"}, {"input": "10 1 2\r\n1 1 1 1 1 1 1 1 1 2\r\n", "output": "6\r\n"}, {"input": "5 1 3\r\n1 1 2 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 1 2 1\r\n", "output": "2\r\n"}, {"input": "6 1 4\r\n1 1 1 2 2 2\r\n", "output": "2\r\n"}, {"input": "7 1 2\r\n1 2 1 1 1 1 1\r\n", "output": "3\r\n"}, {"input": "6 2 2\r\n1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "6 1 2\r\n1 1 2 1 1 1\r\n", "output": "2\r\n"}, {"input": "3 3 1\r\n2 2 1\r\n", "output": "2\r\n"}, {"input": "8 4 2\r\n1 1 1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "9 1 4\r\n1 1 1 1 1 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "5 10 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "3 5 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "5 100 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "4 1 2\r\n1 1 1 1\r\n", "output": "0\r\n"}, {"input": "4 1 1\r\n1 1 1 1\r\n", "output": "1\r\n"}, {"input": "7 2 2\r\n1 1 1 1 1 1 1\r\n", "output": "1\r\n"}]
false
stdio
null
true
828/A
828
A
PyPy 3-64
TESTS
11
61
22,835,200
200312314
def solution(n,f,s,g): S =s*2 count=0 for x in g : if x == 1: if f>0 : f -=1 count+=1 elif S > 0: S-=1 count+=1 else: if S//2>0 : S-=2 count+=2 return sum(g)-count if __name__ == '__main__': n,f,s = map(int,input().split()) g =list(map(int,input().split())) print(solution(n,f,s,g))
97
108
22,835,200
225176702
n, a, b = map(int, input().split()) t = list(map(int, input().split())) b2 = 0 d = 0 for i in range(n): if t[i]==1: if a>0: a-=1 elif b>0: b-=1 b2+=1 elif b2>0: b2-=1 else: d+=1 else: if b>0: b-=1 else: d+=2 print(d)
Codeforces Round 423 (Div. 2, rated, based on VK Cup Finals)
CF
2,017
1
256
Restaurant Tables
In a small restaurant there are a tables for one person and b tables for two persons. It it known that n groups of people come today, each consisting of one or two people. If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group. If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group. You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.
The first line contains three integers n, a and b (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables. The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.
Print the total number of people the restaurant denies service to.
null
In the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served. In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.
[{"input": "4 1 2\n1 2 1 1", "output": "0"}, {"input": "4 1 1\n1 1 2 1", "output": "2"}]
1,200
["implementation"]
97
[{"input": "4 1 2\r\n1 2 1 1\r\n", "output": "0\r\n"}, {"input": "4 1 1\r\n1 1 2 1\r\n", "output": "2\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "2 1 2\r\n2 2\r\n", "output": "0\r\n"}, {"input": "5 1 3\r\n1 2 2 2 1\r\n", "output": "1\r\n"}, {"input": "7 6 1\r\n1 1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "10 2 1\r\n2 1 2 2 2 2 1 2 1 2\r\n", "output": "13\r\n"}, {"input": "20 4 3\r\n2 2 2 2 2 2 2 2 1 2 1 1 2 2 1 2 2 2 1 2\r\n", "output": "25\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "1 1 1\r\n2\r\n", "output": "0\r\n"}, {"input": "1 200000 200000\r\n2\r\n", "output": "0\r\n"}, {"input": "30 10 10\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2\r\n", "output": "20\r\n"}, {"input": "4 1 2\r\n1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 2 3\r\n1 2 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 4\r\n1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "6 1 3\r\n1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 4 2\r\n2 1 2 2 1 1\r\n", "output": "2\r\n"}, {"input": "3 10 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "5 1 3\r\n1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "5 2 2\r\n1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "15 5 5\r\n1 1 1 1 1 1 1 1 1 1 2 2 2 2 2\r\n", "output": "10\r\n"}, {"input": "5 1 2\r\n1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "3 6 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "5 3 3\r\n2 2 2 2 2\r\n", "output": "4\r\n"}, {"input": "8 3 3\r\n1 1 1 1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "5 1 2\r\n1 1 1 2 1\r\n", "output": "2\r\n"}, {"input": "6 1 4\r\n1 2 2 1 2 2\r\n", "output": "2\r\n"}, {"input": "2 1 1\r\n2 2\r\n", "output": "2\r\n"}, {"input": "2 2 1\r\n2 2\r\n", "output": "2\r\n"}, {"input": "5 8 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "3 1 4\r\n1 1 2\r\n", "output": "0\r\n"}, {"input": "7 1 5\r\n1 1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 2 1 1\r\n", "output": "0\r\n"}, {"input": "6 1 2\r\n1 1 1 2 2 2\r\n", "output": "6\r\n"}, {"input": "8 1 4\r\n2 1 1 1 2 2 2 2\r\n", "output": "6\r\n"}, {"input": "4 2 3\r\n2 2 2 2\r\n", "output": "2\r\n"}, {"input": "3 1 1\r\n1 1 2\r\n", "output": "2\r\n"}, {"input": "5 1 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "10 1 5\r\n1 1 1 1 1 2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "5 1 2\r\n1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "4 1 1\r\n1 1 2 2\r\n", "output": "4\r\n"}, {"input": "7 1 2\r\n1 1 1 1 1 1 1\r\n", "output": "2\r\n"}, {"input": "5 1 4\r\n2 2 2 2 2\r\n", "output": "2\r\n"}, {"input": "6 2 3\r\n1 1 1 1 2 2\r\n", "output": "2\r\n"}, {"input": "5 2 2\r\n2 1 2 1 2\r\n", "output": "2\r\n"}, {"input": "4 6 1\r\n2 2 2 2\r\n", "output": "6\r\n"}, {"input": "6 1 4\r\n1 1 2 1 1 2\r\n", "output": "2\r\n"}, {"input": "7 1 3\r\n1 1 1 1 2 2 2\r\n", "output": "6\r\n"}, {"input": "4 1 2\r\n1 1 2 2\r\n", "output": "2\r\n"}, {"input": "3 1 2\r\n1 1 2\r\n", "output": "0\r\n"}, {"input": "6 1 3\r\n1 2 1 1 2 1\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 2 2 2\r\n", "output": "4\r\n"}, {"input": "10 2 2\r\n1 1 1 1 2 2 2 2 2 2\r\n", "output": "12\r\n"}, {"input": "10 1 4\r\n1 1 1 1 1 2 2 2 2 2\r\n", "output": "10\r\n"}, {"input": "3 10 2\r\n2 2 2\r\n", "output": "2\r\n"}, {"input": "4 3 1\r\n1 2 2 2\r\n", "output": "4\r\n"}, {"input": "7 1 4\r\n1 1 1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "3 4 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "4 1 2\r\n2 1 1 2\r\n", "output": "2\r\n"}, {"input": "10 1 2\r\n1 1 1 1 1 1 1 1 1 2\r\n", "output": "6\r\n"}, {"input": "5 1 3\r\n1 1 2 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 1 2 1\r\n", "output": "2\r\n"}, {"input": "6 1 4\r\n1 1 1 2 2 2\r\n", "output": "2\r\n"}, {"input": "7 1 2\r\n1 2 1 1 1 1 1\r\n", "output": "3\r\n"}, {"input": "6 2 2\r\n1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "6 1 2\r\n1 1 2 1 1 1\r\n", "output": "2\r\n"}, {"input": "3 3 1\r\n2 2 1\r\n", "output": "2\r\n"}, {"input": "8 4 2\r\n1 1 1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "9 1 4\r\n1 1 1 1 1 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "5 10 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "3 5 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "5 100 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "4 1 2\r\n1 1 1 1\r\n", "output": "0\r\n"}, {"input": "4 1 1\r\n1 1 1 1\r\n", "output": "1\r\n"}, {"input": "7 2 2\r\n1 1 1 1 1 1 1\r\n", "output": "1\r\n"}]
false
stdio
null
true
676/C
676
C
PyPy 3
TESTS
67
187
29,286,400
87641824
import os from math import* n,k=map(int,input().split()) arr=input() arr=list(arr) a=list(filter(lambda x:x=="a",arr)) b=list(filter(lambda x:x=="b",arr)) x="" if len(a)>=len(b): x="b" else: x="a" #print(x) fp=0 ans=0 count=0 brr=[0] checkpos=0 for i in range(n): if arr[i]==x: count+=1 brr.append(i) if count>k: count-=1 if brr[checkpos]==0: ans=i-brr[checkpos] else: if ans<i-brr[checkpos]-1: ans=i-brr[checkpos]-1 checkpos+=1 fp=i # print(brr,ans,checkpos) #print(brr,ans) if len(brr)-1<=k: print(n) else: if ans<n-brr[-1-k]-1: ans=n-1-brr[checkpos] print(ans)
117
77
2,252,800
219080873
def max_beauty(n, k, s): max_length = 0 max_count = 0 char_count = [0, 0] left = 0 for right in range(n): char_count [ord(s[right]) - ord('a')] += 1 max_count = max(max_count, char_count [0], char_count [1]) window_length = right - left + 1 if window_length - max_count > k: char_count [ord(s[left])- ord('a')]-=1 left += 1 max_length = max(max_length, right - left + 1) return max_length n, k = map(int, input().split()) s = input () result = max_beauty(n, k, s) print (result)
Codeforces Round 354 (Div. 2)
CF
2,016
1
256
Vasya and String
High school student Vasya got a string of length n as a birthday present. This string consists of letters 'a' and 'b' only. Vasya denotes beauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equal letters. Vasya can change no more than k characters of the original string. What is the maximum beauty of the string he can achieve?
The first line of the input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n) — the length of the string and the maximum number of characters to change. The second line contains the string, consisting of letters 'a' and 'b' only.
Print the only integer — the maximum beauty of the string Vasya can achieve by changing no more than k characters.
null
In the first sample, Vasya can obtain both strings "aaaa" and "bbbb". In the second sample, the optimal answer is obtained with the string "aaaaabaa" or with the string "aabaaaaa".
[{"input": "4 2\nabba", "output": "4"}, {"input": "8 1\naabaabaa", "output": "5"}]
1,500
["binary search", "dp", "strings", "two pointers"]
117
[{"input": "4 2\r\nabba\r\n", "output": "4\r\n"}, {"input": "8 1\r\naabaabaa\r\n", "output": "5\r\n"}, {"input": "1 0\r\na\r\n", "output": "1\r\n"}, {"input": "1 1\r\nb\r\n", "output": "1\r\n"}, {"input": "1 0\r\nb\r\n", "output": "1\r\n"}, {"input": "1 1\r\na\r\n", "output": "1\r\n"}, {"input": "10 10\r\nbbbbbbbbbb\r\n", "output": "10\r\n"}, {"input": "10 2\r\nbbbbbbbbbb\r\n", "output": "10\r\n"}, {"input": "10 1\r\nbbabbabbba\r\n", "output": "6\r\n"}, {"input": "10 10\r\nbbabbbaabb\r\n", "output": "10\r\n"}, {"input": "10 9\r\nbabababbba\r\n", "output": "10\r\n"}, {"input": "10 4\r\nbababbaaab\r\n", "output": "9\r\n"}, {"input": "10 10\r\naabaaabaaa\r\n", "output": "10\r\n"}, {"input": "10 10\r\naaaabbbaaa\r\n", "output": "10\r\n"}, {"input": "10 1\r\nbaaaaaaaab\r\n", "output": "9\r\n"}, {"input": "10 5\r\naaaaabaaaa\r\n", "output": "10\r\n"}, {"input": "10 4\r\naaaaaaaaaa\r\n", "output": "10\r\n"}, {"input": "100 10\r\nbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\r\n", "output": "100\r\n"}, {"input": "100 7\r\nbbbbabbbbbaabbbabbbbbbbbbbbabbbbbbbbbbbbbbbbbbbbbbbbbabbbbbbbbbbbabbabbbbbbbbbbbbbbbbbbbbbbbbbbbbbab\r\n", "output": "93\r\n"}, {"input": "100 30\r\nbbaabaaabbbbbbbbbbaababababbbbbbaabaabbbbbbbbabbbbbabbbbabbbbbbbbaabbbbbbbbbabbbbbabbbbbbbbbaaaaabba\r\n", "output": "100\r\n"}, {"input": "100 6\r\nbaababbbaabbabbaaabbabbaabbbbbbbbaabbbabbbbaabbabbbbbabababbbbabbbbbbabbbbbbbbbaaaabbabbbbaabbabaabb\r\n", "output": "34\r\n"}, {"input": "100 45\r\naabababbabbbaaabbbbbbaabbbabbaabbbbbabbbbbbbbabbbbbbabbaababbaabbababbbbbbababbbbbaabbbbbbbaaaababab\r\n", "output": "100\r\n"}, {"input": "100 2\r\nababaabababaaababbaaaabbaabbbababbbaaabbbbabababbbabababaababaaabaabbbbaaabbbabbbbbabbbbbbbaabbabbba\r\n", "output": "17\r\n"}, {"input": "100 25\r\nbabbbaaababaaabbbaabaabaabbbabbabbbbaaaaaaabaaabaaaaaaaaaabaaaabaaabbbaaabaaababaaabaabbbbaaaaaaaaaa\r\n", "output": "80\r\n"}, {"input": "100 14\r\naabaaaaabababbabbabaaaabbaaaabaaabbbaaabaaaaaaaabaaaaabbaaaaaaaaabaaaaaaabbaababaaaababbbbbabaaaabaa\r\n", "output": "61\r\n"}, {"input": "100 8\r\naaaaabaaaabaabaaaaaaaabaaaabaaaaaaaaaaaaaabaaaaabaaaaaaaaaaaaaaaaabaaaababaabaaaaaaaaaaaaabbabaaaaaa\r\n", "output": "76\r\n"}, {"input": "100 12\r\naaaaaaaaaaaaaaaabaaabaaaaaaaaaabbaaaabbabaaaaaaaaaaaaaaaaaaaaabbaaabaaaaaaaaaaaabaaaaaaaabaaaaaaaaaa\r\n", "output": "100\r\n"}, {"input": "100 65\r\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n", "output": "100\r\n"}, {"input": "10 0\r\nbbbbbbbbbb\r\n", "output": "10\r\n"}, {"input": "10 0\r\nbbbbabbbbb\r\n", "output": "5\r\n"}, {"input": "10 0\r\nbbabbbabba\r\n", "output": "3\r\n"}, {"input": "10 0\r\nbaabbbbaba\r\n", "output": "4\r\n"}, {"input": "10 0\r\naababbbbaa\r\n", "output": "4\r\n"}, {"input": "10 2\r\nabbbbbaaba\r\n", "output": "8\r\n"}, {"input": "10 0\r\nabbaaabaaa\r\n", "output": "3\r\n"}, {"input": "10 0\r\naabbaaabaa\r\n", "output": "3\r\n"}, {"input": "10 1\r\naaaaaababa\r\n", "output": "8\r\n"}, {"input": "10 0\r\nbaaaaaaaaa\r\n", "output": "9\r\n"}, {"input": "10 0\r\naaaaaaaaaa\r\n", "output": "10\r\n"}, {"input": "100 0\r\nbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\r\n", "output": "100\r\n"}, {"input": "100 0\r\nbbbbbbbbbbabbbbaaabbbbbbbbbbbabbbabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbabbbbbbbbbabbbbbbbbbbbbbab\r\n", "output": "40\r\n"}, {"input": "100 11\r\nbaabbbbbababbbbabbbbbbbabbbbbbbbbbbbbbabbbbbbababbbbababbbbaaabbbbabbbbbabbbbbbbbabababbbabbbbbbbabb\r\n", "output": "65\r\n"}, {"input": "100 8\r\nbbababbbbbaabbbaaababbbbababababbbbababbabbbabbbbbaabbbabbbababbabbbbabbbabbbbaabbbbabbbaabbbbaaaabb\r\n", "output": "33\r\n"}, {"input": "100 21\r\nabbaaaabbbababaabbbababbbbbbbbabbaababababbbabbbaaabbaaabbbbabbabbbabbbabaababbbabbbbbabbbbbbabbbbab\r\n", "output": "65\r\n"}, {"input": "100 9\r\nabbbaabaabaaaaaaabbabbbababbaaabbbaaabbaabaaaaabbbbbabbaabaabbbbbaaaaababbaaabbabaabaaabababbaababbb\r\n", "output": "26\r\n"}, {"input": "100 5\r\naababababbaaaaaaaabbbabaaaabbabaaaabbaabaaaaabababbabaabaaabaaaaaaaabaababbabbaaabaabbabbaaaaabbabba\r\n", "output": "22\r\n"}, {"input": "100 9\r\naababaabaaaaaaaaabbbaabaaaaaaabaaaaaaaaaaaaabaaabaabaabbbbabbaababbabbaaaabbababaabaababaabaaaaaaaaa\r\n", "output": "49\r\n"}, {"input": "100 6\r\naaaaabbaaaaaaaaaaabaaaabaaaaaaaaabaaabaaaaaabaaaaaaaaaaabaabaaaabaaaaaaaaaaaaaaabaabbaaaaaaaaaaaaaaa\r\n", "output": "56\r\n"}, {"input": "100 7\r\nabaaabaabaabaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaabaaaaaaabbabaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaba\r\n", "output": "86\r\n"}, {"input": "100 0\r\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n", "output": "100\r\n"}]
false
stdio
null
true
828/A
828
A
Python 3
TESTS
11
78
4,505,600
225587111
n, a, b = map(int, input().split()) d = 0 for i in map(int, input().split()): if i == 1: if a: a -= 1 elif b: b -= 1 a += 1 else: d += 1 else: if b: b -= 1 else: d += 2 print(d)
97
109
8,499,200
28439703
n, a,b = list(map(int, input().split())) c=0 j=0 l = input().split() for g in l: if g == "1": if a>0: a-=1 elif b>0: b-=1 c+=1 elif c>0: c-=1 else: j+=1 elif b>0: b-=1 else: j+=2 print (j)
Codeforces Round 423 (Div. 2, rated, based on VK Cup Finals)
CF
2,017
1
256
Restaurant Tables
In a small restaurant there are a tables for one person and b tables for two persons. It it known that n groups of people come today, each consisting of one or two people. If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group. If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group. You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.
The first line contains three integers n, a and b (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables. The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.
Print the total number of people the restaurant denies service to.
null
In the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served. In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.
[{"input": "4 1 2\n1 2 1 1", "output": "0"}, {"input": "4 1 1\n1 1 2 1", "output": "2"}]
1,200
["implementation"]
97
[{"input": "4 1 2\r\n1 2 1 1\r\n", "output": "0\r\n"}, {"input": "4 1 1\r\n1 1 2 1\r\n", "output": "2\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "2 1 2\r\n2 2\r\n", "output": "0\r\n"}, {"input": "5 1 3\r\n1 2 2 2 1\r\n", "output": "1\r\n"}, {"input": "7 6 1\r\n1 1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "10 2 1\r\n2 1 2 2 2 2 1 2 1 2\r\n", "output": "13\r\n"}, {"input": "20 4 3\r\n2 2 2 2 2 2 2 2 1 2 1 1 2 2 1 2 2 2 1 2\r\n", "output": "25\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "1 1 1\r\n2\r\n", "output": "0\r\n"}, {"input": "1 200000 200000\r\n2\r\n", "output": "0\r\n"}, {"input": "30 10 10\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2\r\n", "output": "20\r\n"}, {"input": "4 1 2\r\n1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 2 3\r\n1 2 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 4\r\n1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "6 1 3\r\n1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 4 2\r\n2 1 2 2 1 1\r\n", "output": "2\r\n"}, {"input": "3 10 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "5 1 3\r\n1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "5 2 2\r\n1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "15 5 5\r\n1 1 1 1 1 1 1 1 1 1 2 2 2 2 2\r\n", "output": "10\r\n"}, {"input": "5 1 2\r\n1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "3 6 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "5 3 3\r\n2 2 2 2 2\r\n", "output": "4\r\n"}, {"input": "8 3 3\r\n1 1 1 1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "5 1 2\r\n1 1 1 2 1\r\n", "output": "2\r\n"}, {"input": "6 1 4\r\n1 2 2 1 2 2\r\n", "output": "2\r\n"}, {"input": "2 1 1\r\n2 2\r\n", "output": "2\r\n"}, {"input": "2 2 1\r\n2 2\r\n", "output": "2\r\n"}, {"input": "5 8 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "3 1 4\r\n1 1 2\r\n", "output": "0\r\n"}, {"input": "7 1 5\r\n1 1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 2 1 1\r\n", "output": "0\r\n"}, {"input": "6 1 2\r\n1 1 1 2 2 2\r\n", "output": "6\r\n"}, {"input": "8 1 4\r\n2 1 1 1 2 2 2 2\r\n", "output": "6\r\n"}, {"input": "4 2 3\r\n2 2 2 2\r\n", "output": "2\r\n"}, {"input": "3 1 1\r\n1 1 2\r\n", "output": "2\r\n"}, {"input": "5 1 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "10 1 5\r\n1 1 1 1 1 2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "5 1 2\r\n1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "4 1 1\r\n1 1 2 2\r\n", "output": "4\r\n"}, {"input": "7 1 2\r\n1 1 1 1 1 1 1\r\n", "output": "2\r\n"}, {"input": "5 1 4\r\n2 2 2 2 2\r\n", "output": "2\r\n"}, {"input": "6 2 3\r\n1 1 1 1 2 2\r\n", "output": "2\r\n"}, {"input": "5 2 2\r\n2 1 2 1 2\r\n", "output": "2\r\n"}, {"input": "4 6 1\r\n2 2 2 2\r\n", "output": "6\r\n"}, {"input": "6 1 4\r\n1 1 2 1 1 2\r\n", "output": "2\r\n"}, {"input": "7 1 3\r\n1 1 1 1 2 2 2\r\n", "output": "6\r\n"}, {"input": "4 1 2\r\n1 1 2 2\r\n", "output": "2\r\n"}, {"input": "3 1 2\r\n1 1 2\r\n", "output": "0\r\n"}, {"input": "6 1 3\r\n1 2 1 1 2 1\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 2 2 2\r\n", "output": "4\r\n"}, {"input": "10 2 2\r\n1 1 1 1 2 2 2 2 2 2\r\n", "output": "12\r\n"}, {"input": "10 1 4\r\n1 1 1 1 1 2 2 2 2 2\r\n", "output": "10\r\n"}, {"input": "3 10 2\r\n2 2 2\r\n", "output": "2\r\n"}, {"input": "4 3 1\r\n1 2 2 2\r\n", "output": "4\r\n"}, {"input": "7 1 4\r\n1 1 1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "3 4 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "4 1 2\r\n2 1 1 2\r\n", "output": "2\r\n"}, {"input": "10 1 2\r\n1 1 1 1 1 1 1 1 1 2\r\n", "output": "6\r\n"}, {"input": "5 1 3\r\n1 1 2 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 1 2 1\r\n", "output": "2\r\n"}, {"input": "6 1 4\r\n1 1 1 2 2 2\r\n", "output": "2\r\n"}, {"input": "7 1 2\r\n1 2 1 1 1 1 1\r\n", "output": "3\r\n"}, {"input": "6 2 2\r\n1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "6 1 2\r\n1 1 2 1 1 1\r\n", "output": "2\r\n"}, {"input": "3 3 1\r\n2 2 1\r\n", "output": "2\r\n"}, {"input": "8 4 2\r\n1 1 1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "9 1 4\r\n1 1 1 1 1 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "5 10 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "3 5 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "5 100 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "4 1 2\r\n1 1 1 1\r\n", "output": "0\r\n"}, {"input": "4 1 1\r\n1 1 1 1\r\n", "output": "1\r\n"}, {"input": "7 2 2\r\n1 1 1 1 1 1 1\r\n", "output": "1\r\n"}]
false
stdio
null
true
828/A
828
A
PyPy 3-64
TESTS
11
77
15,155,200
225586289
n,a,b=map(int,input().split()) c=0 o=0 for g in map(int,input().split()): if g==1: if a>0:a-=1 elif c>0:c-=1 elif b>0: b-=1 c+=1 else:o+=1 else: if b>0:b-=1 else:o+=2 print(o)
97
109
18,432,000
177087289
import sys input = sys.stdin.buffer.readline n, a, b = map(int, input().split()) c = 0 # 1 person left 2 people table queries = list(map(int, input().split())) ans = 0 for query in queries: if query == 1: if a: a -= 1 elif b: b -= 1 c += 1 elif c: c -= 1 else: ans += 1 else: if b: b -= 1 else: ans += 2 print(ans)
Codeforces Round 423 (Div. 2, rated, based on VK Cup Finals)
CF
2,017
1
256
Restaurant Tables
In a small restaurant there are a tables for one person and b tables for two persons. It it known that n groups of people come today, each consisting of one or two people. If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group. If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group. You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.
The first line contains three integers n, a and b (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables. The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.
Print the total number of people the restaurant denies service to.
null
In the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served. In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.
[{"input": "4 1 2\n1 2 1 1", "output": "0"}, {"input": "4 1 1\n1 1 2 1", "output": "2"}]
1,200
["implementation"]
97
[{"input": "4 1 2\r\n1 2 1 1\r\n", "output": "0\r\n"}, {"input": "4 1 1\r\n1 1 2 1\r\n", "output": "2\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "2 1 2\r\n2 2\r\n", "output": "0\r\n"}, {"input": "5 1 3\r\n1 2 2 2 1\r\n", "output": "1\r\n"}, {"input": "7 6 1\r\n1 1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "10 2 1\r\n2 1 2 2 2 2 1 2 1 2\r\n", "output": "13\r\n"}, {"input": "20 4 3\r\n2 2 2 2 2 2 2 2 1 2 1 1 2 2 1 2 2 2 1 2\r\n", "output": "25\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "1 1 1\r\n2\r\n", "output": "0\r\n"}, {"input": "1 200000 200000\r\n2\r\n", "output": "0\r\n"}, {"input": "30 10 10\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2\r\n", "output": "20\r\n"}, {"input": "4 1 2\r\n1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 2 3\r\n1 2 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 4\r\n1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "6 1 3\r\n1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 4 2\r\n2 1 2 2 1 1\r\n", "output": "2\r\n"}, {"input": "3 10 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "5 1 3\r\n1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "5 2 2\r\n1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "15 5 5\r\n1 1 1 1 1 1 1 1 1 1 2 2 2 2 2\r\n", "output": "10\r\n"}, {"input": "5 1 2\r\n1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "3 6 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "5 3 3\r\n2 2 2 2 2\r\n", "output": "4\r\n"}, {"input": "8 3 3\r\n1 1 1 1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "5 1 2\r\n1 1 1 2 1\r\n", "output": "2\r\n"}, {"input": "6 1 4\r\n1 2 2 1 2 2\r\n", "output": "2\r\n"}, {"input": "2 1 1\r\n2 2\r\n", "output": "2\r\n"}, {"input": "2 2 1\r\n2 2\r\n", "output": "2\r\n"}, {"input": "5 8 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "3 1 4\r\n1 1 2\r\n", "output": "0\r\n"}, {"input": "7 1 5\r\n1 1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 2 1 1\r\n", "output": "0\r\n"}, {"input": "6 1 2\r\n1 1 1 2 2 2\r\n", "output": "6\r\n"}, {"input": "8 1 4\r\n2 1 1 1 2 2 2 2\r\n", "output": "6\r\n"}, {"input": "4 2 3\r\n2 2 2 2\r\n", "output": "2\r\n"}, {"input": "3 1 1\r\n1 1 2\r\n", "output": "2\r\n"}, {"input": "5 1 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "10 1 5\r\n1 1 1 1 1 2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "5 1 2\r\n1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "4 1 1\r\n1 1 2 2\r\n", "output": "4\r\n"}, {"input": "7 1 2\r\n1 1 1 1 1 1 1\r\n", "output": "2\r\n"}, {"input": "5 1 4\r\n2 2 2 2 2\r\n", "output": "2\r\n"}, {"input": "6 2 3\r\n1 1 1 1 2 2\r\n", "output": "2\r\n"}, {"input": "5 2 2\r\n2 1 2 1 2\r\n", "output": "2\r\n"}, {"input": "4 6 1\r\n2 2 2 2\r\n", "output": "6\r\n"}, {"input": "6 1 4\r\n1 1 2 1 1 2\r\n", "output": "2\r\n"}, {"input": "7 1 3\r\n1 1 1 1 2 2 2\r\n", "output": "6\r\n"}, {"input": "4 1 2\r\n1 1 2 2\r\n", "output": "2\r\n"}, {"input": "3 1 2\r\n1 1 2\r\n", "output": "0\r\n"}, {"input": "6 1 3\r\n1 2 1 1 2 1\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 2 2 2\r\n", "output": "4\r\n"}, {"input": "10 2 2\r\n1 1 1 1 2 2 2 2 2 2\r\n", "output": "12\r\n"}, {"input": "10 1 4\r\n1 1 1 1 1 2 2 2 2 2\r\n", "output": "10\r\n"}, {"input": "3 10 2\r\n2 2 2\r\n", "output": "2\r\n"}, {"input": "4 3 1\r\n1 2 2 2\r\n", "output": "4\r\n"}, {"input": "7 1 4\r\n1 1 1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "3 4 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "4 1 2\r\n2 1 1 2\r\n", "output": "2\r\n"}, {"input": "10 1 2\r\n1 1 1 1 1 1 1 1 1 2\r\n", "output": "6\r\n"}, {"input": "5 1 3\r\n1 1 2 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 1 2 1\r\n", "output": "2\r\n"}, {"input": "6 1 4\r\n1 1 1 2 2 2\r\n", "output": "2\r\n"}, {"input": "7 1 2\r\n1 2 1 1 1 1 1\r\n", "output": "3\r\n"}, {"input": "6 2 2\r\n1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "6 1 2\r\n1 1 2 1 1 1\r\n", "output": "2\r\n"}, {"input": "3 3 1\r\n2 2 1\r\n", "output": "2\r\n"}, {"input": "8 4 2\r\n1 1 1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "9 1 4\r\n1 1 1 1 1 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "5 10 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "3 5 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "5 100 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "4 1 2\r\n1 1 1 1\r\n", "output": "0\r\n"}, {"input": "4 1 1\r\n1 1 1 1\r\n", "output": "1\r\n"}, {"input": "7 2 2\r\n1 1 1 1 1 1 1\r\n", "output": "1\r\n"}]
false
stdio
null
true
828/A
828
A
Python 3
TESTS
11
78
3,481,600
174338080
n, a, b = map(int, input().split()) l = list(map(int, input().split())) ans = 0 c = 0 for i in l: if i == 1: if a != 0: a -= i;ans += 1 elif c != 0: ans += i;c -= 1 elif b != 0: ans += i;b -= 1;c += 1 else: if b != 0: ans += i;b -= 1 print(sum(l)-ans)
97
124
3,788,800
116314423
def checkSat(a, b, l): OneT = a; TwoT = b; ParTwoT = 0; Satisfied = 0; for i in range(len(l)): if(l[i]==1): if(OneT!=0): OneT -= 1 Satisfied +=1 else: if(TwoT!=0): TwoT -= 1 ParTwoT += 1 Satisfied += 1 elif(ParTwoT!=0): ParTwoT -= 1 Satisfied += 1 else: if(TwoT!=0): TwoT -= 1 Satisfied += 2 return sum(l) - Satisfied def main(): n1,a1,b1 = [int(x) for x in input().split()] l1 = list(map(int, input().split())) print(checkSat(a1,b1,l1)) if __name__ == "__main__": main()
Codeforces Round 423 (Div. 2, rated, based on VK Cup Finals)
CF
2,017
1
256
Restaurant Tables
In a small restaurant there are a tables for one person and b tables for two persons. It it known that n groups of people come today, each consisting of one or two people. If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group. If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group. You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.
The first line contains three integers n, a and b (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables. The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.
Print the total number of people the restaurant denies service to.
null
In the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served. In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.
[{"input": "4 1 2\n1 2 1 1", "output": "0"}, {"input": "4 1 1\n1 1 2 1", "output": "2"}]
1,200
["implementation"]
97
[{"input": "4 1 2\r\n1 2 1 1\r\n", "output": "0\r\n"}, {"input": "4 1 1\r\n1 1 2 1\r\n", "output": "2\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "2 1 2\r\n2 2\r\n", "output": "0\r\n"}, {"input": "5 1 3\r\n1 2 2 2 1\r\n", "output": "1\r\n"}, {"input": "7 6 1\r\n1 1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "10 2 1\r\n2 1 2 2 2 2 1 2 1 2\r\n", "output": "13\r\n"}, {"input": "20 4 3\r\n2 2 2 2 2 2 2 2 1 2 1 1 2 2 1 2 2 2 1 2\r\n", "output": "25\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "1 1 1\r\n2\r\n", "output": "0\r\n"}, {"input": "1 200000 200000\r\n2\r\n", "output": "0\r\n"}, {"input": "30 10 10\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2\r\n", "output": "20\r\n"}, {"input": "4 1 2\r\n1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 2 3\r\n1 2 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 4\r\n1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "6 1 3\r\n1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 4 2\r\n2 1 2 2 1 1\r\n", "output": "2\r\n"}, {"input": "3 10 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "5 1 3\r\n1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "5 2 2\r\n1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "15 5 5\r\n1 1 1 1 1 1 1 1 1 1 2 2 2 2 2\r\n", "output": "10\r\n"}, {"input": "5 1 2\r\n1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "3 6 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "5 3 3\r\n2 2 2 2 2\r\n", "output": "4\r\n"}, {"input": "8 3 3\r\n1 1 1 1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "5 1 2\r\n1 1 1 2 1\r\n", "output": "2\r\n"}, {"input": "6 1 4\r\n1 2 2 1 2 2\r\n", "output": "2\r\n"}, {"input": "2 1 1\r\n2 2\r\n", "output": "2\r\n"}, {"input": "2 2 1\r\n2 2\r\n", "output": "2\r\n"}, {"input": "5 8 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "3 1 4\r\n1 1 2\r\n", "output": "0\r\n"}, {"input": "7 1 5\r\n1 1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 2 1 1\r\n", "output": "0\r\n"}, {"input": "6 1 2\r\n1 1 1 2 2 2\r\n", "output": "6\r\n"}, {"input": "8 1 4\r\n2 1 1 1 2 2 2 2\r\n", "output": "6\r\n"}, {"input": "4 2 3\r\n2 2 2 2\r\n", "output": "2\r\n"}, {"input": "3 1 1\r\n1 1 2\r\n", "output": "2\r\n"}, {"input": "5 1 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "10 1 5\r\n1 1 1 1 1 2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "5 1 2\r\n1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "4 1 1\r\n1 1 2 2\r\n", "output": "4\r\n"}, {"input": "7 1 2\r\n1 1 1 1 1 1 1\r\n", "output": "2\r\n"}, {"input": "5 1 4\r\n2 2 2 2 2\r\n", "output": "2\r\n"}, {"input": "6 2 3\r\n1 1 1 1 2 2\r\n", "output": "2\r\n"}, {"input": "5 2 2\r\n2 1 2 1 2\r\n", "output": "2\r\n"}, {"input": "4 6 1\r\n2 2 2 2\r\n", "output": "6\r\n"}, {"input": "6 1 4\r\n1 1 2 1 1 2\r\n", "output": "2\r\n"}, {"input": "7 1 3\r\n1 1 1 1 2 2 2\r\n", "output": "6\r\n"}, {"input": "4 1 2\r\n1 1 2 2\r\n", "output": "2\r\n"}, {"input": "3 1 2\r\n1 1 2\r\n", "output": "0\r\n"}, {"input": "6 1 3\r\n1 2 1 1 2 1\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 2 2 2\r\n", "output": "4\r\n"}, {"input": "10 2 2\r\n1 1 1 1 2 2 2 2 2 2\r\n", "output": "12\r\n"}, {"input": "10 1 4\r\n1 1 1 1 1 2 2 2 2 2\r\n", "output": "10\r\n"}, {"input": "3 10 2\r\n2 2 2\r\n", "output": "2\r\n"}, {"input": "4 3 1\r\n1 2 2 2\r\n", "output": "4\r\n"}, {"input": "7 1 4\r\n1 1 1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "3 4 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "4 1 2\r\n2 1 1 2\r\n", "output": "2\r\n"}, {"input": "10 1 2\r\n1 1 1 1 1 1 1 1 1 2\r\n", "output": "6\r\n"}, {"input": "5 1 3\r\n1 1 2 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 1 2 1\r\n", "output": "2\r\n"}, {"input": "6 1 4\r\n1 1 1 2 2 2\r\n", "output": "2\r\n"}, {"input": "7 1 2\r\n1 2 1 1 1 1 1\r\n", "output": "3\r\n"}, {"input": "6 2 2\r\n1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "6 1 2\r\n1 1 2 1 1 1\r\n", "output": "2\r\n"}, {"input": "3 3 1\r\n2 2 1\r\n", "output": "2\r\n"}, {"input": "8 4 2\r\n1 1 1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "9 1 4\r\n1 1 1 1 1 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "5 10 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "3 5 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "5 100 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "4 1 2\r\n1 1 1 1\r\n", "output": "0\r\n"}, {"input": "4 1 1\r\n1 1 1 1\r\n", "output": "1\r\n"}, {"input": "7 2 2\r\n1 1 1 1 1 1 1\r\n", "output": "1\r\n"}]
false
stdio
null
true
828/A
828
A
Python 3
TESTS
11
78
3,993,600
141470577
def main(): n, a, b = map(int, input().split()) lst = list(map(int, input().split())) c, ans = 0, 0 for i in lst: if i == 1: if a > 0: a -= 1 elif c > 0: # c = Table for 2 occupied by 1 c -= 1 elif b > 0: b -= 1 c += 1 else: ans += 1 elif b > 0: b -= 1 else: ans += 2 print(ans) main()
97
124
8,499,200
28431129
n, a, b = list(map(int,input().split())) l = input().split() o = 0 c = 0 for i in l: if i == '2': if b > 0: b -= 1 else: o += 2 if i == '1': if a > 0: a -= 1 elif b > 0: b -= 1 c += 1 elif c > 0: c -= 1 else: o += 1 print(o)
Codeforces Round 423 (Div. 2, rated, based on VK Cup Finals)
CF
2,017
1
256
Restaurant Tables
In a small restaurant there are a tables for one person and b tables for two persons. It it known that n groups of people come today, each consisting of one or two people. If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group. If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group. You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.
The first line contains three integers n, a and b (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables. The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.
Print the total number of people the restaurant denies service to.
null
In the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served. In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.
[{"input": "4 1 2\n1 2 1 1", "output": "0"}, {"input": "4 1 1\n1 1 2 1", "output": "2"}]
1,200
["implementation"]
97
[{"input": "4 1 2\r\n1 2 1 1\r\n", "output": "0\r\n"}, {"input": "4 1 1\r\n1 1 2 1\r\n", "output": "2\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "2 1 2\r\n2 2\r\n", "output": "0\r\n"}, {"input": "5 1 3\r\n1 2 2 2 1\r\n", "output": "1\r\n"}, {"input": "7 6 1\r\n1 1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "10 2 1\r\n2 1 2 2 2 2 1 2 1 2\r\n", "output": "13\r\n"}, {"input": "20 4 3\r\n2 2 2 2 2 2 2 2 1 2 1 1 2 2 1 2 2 2 1 2\r\n", "output": "25\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "1 1 1\r\n2\r\n", "output": "0\r\n"}, {"input": "1 200000 200000\r\n2\r\n", "output": "0\r\n"}, {"input": "30 10 10\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2\r\n", "output": "20\r\n"}, {"input": "4 1 2\r\n1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 2 3\r\n1 2 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 4\r\n1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "6 1 3\r\n1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 4 2\r\n2 1 2 2 1 1\r\n", "output": "2\r\n"}, {"input": "3 10 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "5 1 3\r\n1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "5 2 2\r\n1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "15 5 5\r\n1 1 1 1 1 1 1 1 1 1 2 2 2 2 2\r\n", "output": "10\r\n"}, {"input": "5 1 2\r\n1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "3 6 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "5 3 3\r\n2 2 2 2 2\r\n", "output": "4\r\n"}, {"input": "8 3 3\r\n1 1 1 1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "5 1 2\r\n1 1 1 2 1\r\n", "output": "2\r\n"}, {"input": "6 1 4\r\n1 2 2 1 2 2\r\n", "output": "2\r\n"}, {"input": "2 1 1\r\n2 2\r\n", "output": "2\r\n"}, {"input": "2 2 1\r\n2 2\r\n", "output": "2\r\n"}, {"input": "5 8 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "3 1 4\r\n1 1 2\r\n", "output": "0\r\n"}, {"input": "7 1 5\r\n1 1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 2 1 1\r\n", "output": "0\r\n"}, {"input": "6 1 2\r\n1 1 1 2 2 2\r\n", "output": "6\r\n"}, {"input": "8 1 4\r\n2 1 1 1 2 2 2 2\r\n", "output": "6\r\n"}, {"input": "4 2 3\r\n2 2 2 2\r\n", "output": "2\r\n"}, {"input": "3 1 1\r\n1 1 2\r\n", "output": "2\r\n"}, {"input": "5 1 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "10 1 5\r\n1 1 1 1 1 2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "5 1 2\r\n1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "4 1 1\r\n1 1 2 2\r\n", "output": "4\r\n"}, {"input": "7 1 2\r\n1 1 1 1 1 1 1\r\n", "output": "2\r\n"}, {"input": "5 1 4\r\n2 2 2 2 2\r\n", "output": "2\r\n"}, {"input": "6 2 3\r\n1 1 1 1 2 2\r\n", "output": "2\r\n"}, {"input": "5 2 2\r\n2 1 2 1 2\r\n", "output": "2\r\n"}, {"input": "4 6 1\r\n2 2 2 2\r\n", "output": "6\r\n"}, {"input": "6 1 4\r\n1 1 2 1 1 2\r\n", "output": "2\r\n"}, {"input": "7 1 3\r\n1 1 1 1 2 2 2\r\n", "output": "6\r\n"}, {"input": "4 1 2\r\n1 1 2 2\r\n", "output": "2\r\n"}, {"input": "3 1 2\r\n1 1 2\r\n", "output": "0\r\n"}, {"input": "6 1 3\r\n1 2 1 1 2 1\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 2 2 2\r\n", "output": "4\r\n"}, {"input": "10 2 2\r\n1 1 1 1 2 2 2 2 2 2\r\n", "output": "12\r\n"}, {"input": "10 1 4\r\n1 1 1 1 1 2 2 2 2 2\r\n", "output": "10\r\n"}, {"input": "3 10 2\r\n2 2 2\r\n", "output": "2\r\n"}, {"input": "4 3 1\r\n1 2 2 2\r\n", "output": "4\r\n"}, {"input": "7 1 4\r\n1 1 1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "3 4 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "4 1 2\r\n2 1 1 2\r\n", "output": "2\r\n"}, {"input": "10 1 2\r\n1 1 1 1 1 1 1 1 1 2\r\n", "output": "6\r\n"}, {"input": "5 1 3\r\n1 1 2 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 1 2 1\r\n", "output": "2\r\n"}, {"input": "6 1 4\r\n1 1 1 2 2 2\r\n", "output": "2\r\n"}, {"input": "7 1 2\r\n1 2 1 1 1 1 1\r\n", "output": "3\r\n"}, {"input": "6 2 2\r\n1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "6 1 2\r\n1 1 2 1 1 1\r\n", "output": "2\r\n"}, {"input": "3 3 1\r\n2 2 1\r\n", "output": "2\r\n"}, {"input": "8 4 2\r\n1 1 1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "9 1 4\r\n1 1 1 1 1 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "5 10 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "3 5 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "5 100 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "4 1 2\r\n1 1 1 1\r\n", "output": "0\r\n"}, {"input": "4 1 1\r\n1 1 1 1\r\n", "output": "1\r\n"}, {"input": "7 2 2\r\n1 1 1 1 1 1 1\r\n", "output": "1\r\n"}]
false
stdio
null
true
828/A
828
A
PyPy 3-64
TESTS
11
92
22,835,200
167448617
#!/usr/bin/env python3 import math def test_case(casen): n, a, b = map(int, input().split()) t = list(map(int, input().split())) ans = 0 twoone = 0 for i in range(n): if t[i] == 1 and a > 0: a -= 1 continue if t[i] == 1 and twoone > 0: twoone -= 1 continue if t[i] == 1 and b > 0: twoone += 1 b -= 1 continue if t[i] == 1: ans += 1 continue if t[i] == 2 and b > 0: b -= 1 else: ans += 2 return ans def main(): print(test_case(0)) if __name__ == '__main__': main()
97
124
8,499,200
28436370
n, a, b = (input().split()) n = int(n) a = int(a) b = int(b) potatos = 0 counter = 0 customers = input().split() for i in customers: if(i == '1'): if(a > 0): a -= 1 elif(b > 0): b -= 1 potatos += 1 elif(potatos > 0): potatos -= 1 else: counter += 1 else: if(b > 0): b -= 1 else: counter += 2 print(counter)
Codeforces Round 423 (Div. 2, rated, based on VK Cup Finals)
CF
2,017
1
256
Restaurant Tables
In a small restaurant there are a tables for one person and b tables for two persons. It it known that n groups of people come today, each consisting of one or two people. If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group. If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group. You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.
The first line contains three integers n, a and b (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables. The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.
Print the total number of people the restaurant denies service to.
null
In the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served. In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.
[{"input": "4 1 2\n1 2 1 1", "output": "0"}, {"input": "4 1 1\n1 1 2 1", "output": "2"}]
1,200
["implementation"]
97
[{"input": "4 1 2\r\n1 2 1 1\r\n", "output": "0\r\n"}, {"input": "4 1 1\r\n1 1 2 1\r\n", "output": "2\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "2 1 2\r\n2 2\r\n", "output": "0\r\n"}, {"input": "5 1 3\r\n1 2 2 2 1\r\n", "output": "1\r\n"}, {"input": "7 6 1\r\n1 1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "10 2 1\r\n2 1 2 2 2 2 1 2 1 2\r\n", "output": "13\r\n"}, {"input": "20 4 3\r\n2 2 2 2 2 2 2 2 1 2 1 1 2 2 1 2 2 2 1 2\r\n", "output": "25\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "1 1 1\r\n2\r\n", "output": "0\r\n"}, {"input": "1 200000 200000\r\n2\r\n", "output": "0\r\n"}, {"input": "30 10 10\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2\r\n", "output": "20\r\n"}, {"input": "4 1 2\r\n1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 2 3\r\n1 2 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 4\r\n1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "6 1 3\r\n1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 4 2\r\n2 1 2 2 1 1\r\n", "output": "2\r\n"}, {"input": "3 10 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "5 1 3\r\n1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "5 2 2\r\n1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "15 5 5\r\n1 1 1 1 1 1 1 1 1 1 2 2 2 2 2\r\n", "output": "10\r\n"}, {"input": "5 1 2\r\n1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "3 6 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "5 3 3\r\n2 2 2 2 2\r\n", "output": "4\r\n"}, {"input": "8 3 3\r\n1 1 1 1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "5 1 2\r\n1 1 1 2 1\r\n", "output": "2\r\n"}, {"input": "6 1 4\r\n1 2 2 1 2 2\r\n", "output": "2\r\n"}, {"input": "2 1 1\r\n2 2\r\n", "output": "2\r\n"}, {"input": "2 2 1\r\n2 2\r\n", "output": "2\r\n"}, {"input": "5 8 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "3 1 4\r\n1 1 2\r\n", "output": "0\r\n"}, {"input": "7 1 5\r\n1 1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 2 1 1\r\n", "output": "0\r\n"}, {"input": "6 1 2\r\n1 1 1 2 2 2\r\n", "output": "6\r\n"}, {"input": "8 1 4\r\n2 1 1 1 2 2 2 2\r\n", "output": "6\r\n"}, {"input": "4 2 3\r\n2 2 2 2\r\n", "output": "2\r\n"}, {"input": "3 1 1\r\n1 1 2\r\n", "output": "2\r\n"}, {"input": "5 1 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "10 1 5\r\n1 1 1 1 1 2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "5 1 2\r\n1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "4 1 1\r\n1 1 2 2\r\n", "output": "4\r\n"}, {"input": "7 1 2\r\n1 1 1 1 1 1 1\r\n", "output": "2\r\n"}, {"input": "5 1 4\r\n2 2 2 2 2\r\n", "output": "2\r\n"}, {"input": "6 2 3\r\n1 1 1 1 2 2\r\n", "output": "2\r\n"}, {"input": "5 2 2\r\n2 1 2 1 2\r\n", "output": "2\r\n"}, {"input": "4 6 1\r\n2 2 2 2\r\n", "output": "6\r\n"}, {"input": "6 1 4\r\n1 1 2 1 1 2\r\n", "output": "2\r\n"}, {"input": "7 1 3\r\n1 1 1 1 2 2 2\r\n", "output": "6\r\n"}, {"input": "4 1 2\r\n1 1 2 2\r\n", "output": "2\r\n"}, {"input": "3 1 2\r\n1 1 2\r\n", "output": "0\r\n"}, {"input": "6 1 3\r\n1 2 1 1 2 1\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 2 2 2\r\n", "output": "4\r\n"}, {"input": "10 2 2\r\n1 1 1 1 2 2 2 2 2 2\r\n", "output": "12\r\n"}, {"input": "10 1 4\r\n1 1 1 1 1 2 2 2 2 2\r\n", "output": "10\r\n"}, {"input": "3 10 2\r\n2 2 2\r\n", "output": "2\r\n"}, {"input": "4 3 1\r\n1 2 2 2\r\n", "output": "4\r\n"}, {"input": "7 1 4\r\n1 1 1 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "3 4 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "4 1 2\r\n2 1 1 2\r\n", "output": "2\r\n"}, {"input": "10 1 2\r\n1 1 1 1 1 1 1 1 1 2\r\n", "output": "6\r\n"}, {"input": "5 1 3\r\n1 1 2 1 2\r\n", "output": "2\r\n"}, {"input": "6 1 3\r\n1 1 1 1 2 1\r\n", "output": "2\r\n"}, {"input": "6 1 4\r\n1 1 1 2 2 2\r\n", "output": "2\r\n"}, {"input": "7 1 2\r\n1 2 1 1 1 1 1\r\n", "output": "3\r\n"}, {"input": "6 2 2\r\n1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "6 1 2\r\n1 1 2 1 1 1\r\n", "output": "2\r\n"}, {"input": "3 3 1\r\n2 2 1\r\n", "output": "2\r\n"}, {"input": "8 4 2\r\n1 1 1 1 1 1 1 2\r\n", "output": "2\r\n"}, {"input": "9 1 4\r\n1 1 1 1 1 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "5 10 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "3 5 1\r\n2 2 2\r\n", "output": "4\r\n"}, {"input": "5 100 1\r\n2 2 2 2 2\r\n", "output": "8\r\n"}, {"input": "4 1 2\r\n1 1 1 1\r\n", "output": "0\r\n"}, {"input": "4 1 1\r\n1 1 1 1\r\n", "output": "1\r\n"}, {"input": "7 2 2\r\n1 1 1 1 1 1 1\r\n", "output": "1\r\n"}]
false
stdio
null
true
623/A
623
A
Python 3
TESTS
4
62
409,600
104469006
from collections import defaultdict n, m = map(int, input("").split()) edge_list =defaultdict(list) for i in range (m): start, end = map(int, input("").split()) edge_list[start].append(end) edge_list[end].append(start) def dfs(vertice, visited, connected_component): if vertice in visited: return connected_component else: visited.add(vertice) connected_component.append(vertice) for child in edge_list[vertice]: connected_component = dfs(child, visited, connected_component) return connected_component def cc(): visited = set() connected_components = [] for vertice in edge_list: if vertice not in visited: connected_component = [] connected_components.append(dfs(vertice, visited, connected_component)) return connected_components def find_string(): #min_edges = n / 2 * (4 + (n - 1)) num_edges = [(n//2)*(n//2-1)/2 + (n-n//2)*((n-n//2-1)/2), (n-1)*(n-2)/2+1, n*(n-1)/2 ] if n == 4 and m == 5: pos_a_c = [] for vertice in edge_list: if len(edge_list[vertice]) == 2: pos_a_c.append(vertice-1) return ['Yes', "".join(map(str, [ 'a' if i in pos_a_c and i == 0 else 'c' if i in pos_a_c else 'b' for i in range(n)]))] try: index = num_edges.index(m) if index == 0: connected_components = cc() letters = ['a', 'c'] constructed_string = [None] * n for i, connected_component in enumerate(connected_components): for position in connected_component: constructed_string[position-1] = letters[i] return ['Yes', "".join(map(str, constructed_string))] elif index == 1: pos_c = None pos_b = None for vertice in edge_list: if len(edge_list[vertice]) == 1: pos_c = vertice-1 pos_b = edge_list[vertice][0]-1 break constructed_string = ['c' if i == pos_c else'b' if i == pos_b else 'a' for i in range(n)] return ['Yes', "".join(map(str, constructed_string))] elif index == 2: return ['Yes', "".join(map(str, ['a' for i in range(n)]))] except ValueError: return ['No'] """" if m < min_edges: return ['No'] if m > min_edges: return ['Yes', "".join(map(str, ['a' for i in range(n)]))] two_vertices = [] vertice_options = set('a', 'c') for i in range (1, n+1): if len(edge_list[i]) == n-2: two_vertices.append(i-1) constructed = [vertice_options.pop() if i in two_vertices else 'b' for i in range(n)] return ['Yes', "".join(map(str, constructed))] """ if n > 2: results = find_string() for result in results: print(result) elif n == 2 and m == 1: print('Yes') print('aa') elif n == 2 and m == 0: print('Yes') print('ac') else: print('Yes') print('a')
106
732
8,908,800
227484209
n, m = [int(i) for i in input().split(' ')] adj = [[1] * n for _ in range(n)] for i in range(n): adj[i][i] = 0 for _ in range(m): u, v = [int(i) - 1 for i in input().split(' ')] adj[u][v], adj[v][u] = 0, 0 b = set([i for i in range(n) if not any(adj[i])]) a = None for i in range(n): if i in b: continue r = adj[i] if a is None: a = r continue for j in range(n): if (adj[i][j] and a[j] == a[i]) or (not adj[i][j] and j not in b and a[i] != a[j]): print('No') exit() s = '' for i in range(n): if i in b: s += 'b' elif a[i]: s += 'c' else: s += 'a' print('Yes') print(s)
AIM Tech Round (Div. 1)
CF
2,016
2
256
Graph and String
One day student Vasya was sitting on a lecture and mentioned a string s1s2... sn, consisting of letters "a", "b" and "c" that was written on his desk. As the lecture was boring, Vasya decided to complete the picture by composing a graph G with the following properties: - G has exactly n vertices, numbered from 1 to n. - For all pairs of vertices i and j, where i ≠ j, there is an edge connecting them if and only if characters si and sj are either equal or neighbouring in the alphabet. That is, letters in pairs "a"-"b" and "b"-"c" are neighbouring, while letters "a"-"c" are not. Vasya painted the resulting graph near the string and then erased the string. Next day Vasya's friend Petya came to a lecture and found some graph at his desk. He had heard of Vasya's adventure and now he wants to find out whether it could be the original graph G, painted by Vasya. In order to verify this, Petya needs to know whether there exists a string s, such that if Vasya used this s he would produce the given graph G.
The first line of the input contains two integers n and m $$( 1 \leq n \leq 500, 0 \leq m \leq \frac { n ( n - 1 ) } { 2 } )$$ — the number of vertices and edges in the graph found by Petya, respectively. Each of the next m lines contains two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — the edges of the graph G. It is guaranteed, that there are no multiple edges, that is any pair of vertexes appear in this list no more than once.
In the first line print "Yes" (without the quotes), if the string s Petya is interested in really exists and "No" (without the quotes) otherwise. If the string s exists, then print it on the second line of the output. The length of s must be exactly n, it must consist of only letters "a", "b" and "c" only, and the graph built using this string must coincide with G. If there are multiple possible answers, you may print any of them.
null
In the first sample you are given a graph made of two vertices with an edge between them. So, these vertices can correspond to both the same and adjacent letters. Any of the following strings "aa", "ab", "ba", "bb", "bc", "cb", "cc" meets the graph's conditions. In the second sample the first vertex is connected to all three other vertices, but these three vertices are not connected with each other. That means that they must correspond to distinct letters that are not adjacent, but that is impossible as there are only two such letters: a and c.
[{"input": "2 1\n1 2", "output": "Yes\naa"}, {"input": "4 3\n1 2\n1 3\n1 4", "output": "No"}]
1,800
["constructive algorithms", "graphs"]
106
[{"input": "2 1\r\n1 2\r\n", "output": "Yes\r\naa\r\n"}, {"input": "4 3\r\n1 2\r\n1 3\r\n1 4\r\n", "output": "No\r\n"}, {"input": "4 4\r\n1 2\r\n1 3\r\n1 4\r\n3 4\r\n", "output": "Yes\r\nbacc\r\n"}, {"input": "1 0\r\n", "output": "Yes\r\na\r\n"}, {"input": "8 28\r\n3 2\r\n4 2\r\n7 4\r\n6 3\r\n3 7\r\n8 1\r\n3 4\r\n5 1\r\n6 5\r\n5 3\r\n7 1\r\n5 8\r\n5 4\r\n6 1\r\n6 4\r\n2 1\r\n4 1\r\n8 2\r\n7 2\r\n6 8\r\n8 4\r\n6 7\r\n3 1\r\n7 8\r\n3 8\r\n5 7\r\n5 2\r\n6 2\r\n", "output": "Yes\r\naaaaaaaa\r\n"}, {"input": "8 28\r\n3 2\r\n4 2\r\n7 4\r\n6 3\r\n3 7\r\n8 1\r\n3 4\r\n5 1\r\n6 5\r\n5 3\r\n7 1\r\n5 8\r\n5 4\r\n6 1\r\n6 4\r\n2 1\r\n4 1\r\n8 2\r\n7 2\r\n6 8\r\n8 4\r\n6 7\r\n3 1\r\n7 8\r\n3 8\r\n5 7\r\n5 2\r\n6 2\r\n", "output": "Yes\r\naaaaaaaa\r\n"}, {"input": "4 3\r\n4 3\r\n2 4\r\n2 3\r\n", "output": "Yes\r\naccc\r\n"}, {"input": "4 2\r\n4 3\r\n1 2\r\n", "output": "Yes\r\naacc\r\n"}, {"input": "5 3\r\n1 2\r\n1 3\r\n4 5\r\n", "output": "No\r\n"}, {"input": "6 4\r\n1 2\r\n1 3\r\n4 5\r\n4 6\r\n", "output": "No\r\n"}, {"input": "6 4\r\n1 2\r\n2 3\r\n4 5\r\n4 6\r\n", "output": "No\r\n"}, {"input": "6 4\r\n3 2\r\n1 3\r\n6 5\r\n4 6\r\n", "output": "No\r\n"}, {"input": "6 4\r\n1 2\r\n1 3\r\n4 6\r\n5 6\r\n", "output": "No\r\n"}, {"input": "7 13\r\n1 2\r\n2 3\r\n1 3\r\n4 5\r\n5 6\r\n4 6\r\n2 5\r\n2 7\r\n3 7\r\n7 4\r\n7 6\r\n7 1\r\n7 5\r\n", "output": "No\r\n"}, {"input": "8 18\r\n3 7\r\n2 5\r\n5 3\r\n3 8\r\n8 6\r\n6 3\r\n6 4\r\n4 8\r\n1 2\r\n6 1\r\n2 7\r\n2 4\r\n4 5\r\n4 3\r\n6 5\r\n1 4\r\n5 7\r\n3 1\r\n", "output": "No\r\n"}, {"input": "20 55\r\n20 11\r\n14 5\r\n4 9\r\n17 5\r\n16 5\r\n20 16\r\n11 17\r\n2 14\r\n14 19\r\n9 15\r\n20 19\r\n5 18\r\n15 20\r\n1 16\r\n12 20\r\n4 7\r\n16 19\r\n17 19\r\n16 12\r\n19 9\r\n11 13\r\n18 17\r\n10 8\r\n20 1\r\n16 8\r\n1 13\r\n11 12\r\n13 18\r\n4 13\r\n14 10\r\n9 13\r\n8 9\r\n6 9\r\n2 13\r\n10 16\r\n19 1\r\n7 17\r\n20 4\r\n12 8\r\n3 2\r\n18 10\r\n6 13\r\n14 9\r\n7 9\r\n19 7\r\n8 15\r\n20 6\r\n16 13\r\n14 13\r\n19 8\r\n7 14\r\n6 2\r\n9 1\r\n7 1\r\n10 6\r\n", "output": "No\r\n"}, {"input": "15 84\r\n11 9\r\n3 11\r\n13 10\r\n2 12\r\n5 9\r\n1 7\r\n14 4\r\n14 2\r\n14 1\r\n11 8\r\n1 8\r\n14 10\r\n4 15\r\n10 5\r\n5 12\r\n13 11\r\n6 14\r\n5 7\r\n12 11\r\n9 1\r\n10 15\r\n2 6\r\n7 15\r\n14 9\r\n9 7\r\n11 14\r\n8 15\r\n12 7\r\n13 6\r\n2 9\r\n9 6\r\n15 3\r\n12 15\r\n6 15\r\n4 6\r\n4 1\r\n9 12\r\n10 7\r\n6 1\r\n11 10\r\n2 3\r\n5 2\r\n13 2\r\n13 3\r\n12 6\r\n4 3\r\n5 8\r\n12 1\r\n9 15\r\n14 5\r\n12 14\r\n10 1\r\n9 4\r\n7 13\r\n3 6\r\n15 1\r\n13 9\r\n11 1\r\n10 4\r\n9 3\r\n8 12\r\n13 12\r\n6 7\r\n12 10\r\n4 12\r\n13 15\r\n2 10\r\n3 8\r\n1 5\r\n15 2\r\n4 11\r\n2 1\r\n10 8\r\n14 3\r\n14 8\r\n8 7\r\n13 1\r\n5 4\r\n11 2\r\n6 8\r\n5 15\r\n2 4\r\n9 8\r\n9 10\r\n", "output": "No\r\n"}, {"input": "15 13\r\n13 15\r\n13 3\r\n14 3\r\n10 7\r\n2 5\r\n5 12\r\n12 11\r\n9 2\r\n13 7\r\n7 4\r\n12 10\r\n15 7\r\n6 13\r\n", "output": "No\r\n"}, {"input": "6 6\r\n1 4\r\n3 4\r\n6 4\r\n2 6\r\n5 3\r\n3 2\r\n", "output": "No\r\n"}, {"input": "4 6\r\n4 2\r\n3 1\r\n3 4\r\n3 2\r\n4 1\r\n2 1\r\n", "output": "Yes\r\naaaa\r\n"}, {"input": "4 4\r\n3 2\r\n2 4\r\n1 2\r\n3 4\r\n", "output": "Yes\r\nabcc\r\n"}, {"input": "4 3\r\n1 3\r\n1 4\r\n3 4\r\n", "output": "Yes\r\nacaa\r\n"}, {"input": "4 4\r\n1 2\r\n4 1\r\n3 4\r\n3 1\r\n", "output": "Yes\r\nbacc\r\n"}, {"input": "4 4\r\n4 2\r\n3 4\r\n3 1\r\n2 3\r\n", "output": "Yes\r\nacbc\r\n"}, {"input": "4 5\r\n3 1\r\n2 1\r\n3 4\r\n2 4\r\n3 2\r\n", "output": "Yes\r\nabbc\r\n"}, {"input": "4 4\r\n4 1\r\n3 1\r\n3 2\r\n3 4\r\n", "output": "Yes\r\nacba\r\n"}, {"input": "4 5\r\n3 4\r\n2 1\r\n3 1\r\n4 1\r\n2 3\r\n", "output": "Yes\r\nbabc\r\n"}, {"input": "4 4\r\n1 3\r\n3 4\r\n2 1\r\n3 2\r\n", "output": "Yes\r\naabc\r\n"}, {"input": "4 3\r\n2 1\r\n1 4\r\n2 4\r\n", "output": "Yes\r\naaca\r\n"}, {"input": "4 4\r\n2 4\r\n1 2\r\n1 3\r\n1 4\r\n", "output": "Yes\r\nbaca\r\n"}, {"input": "4 2\r\n3 1\r\n2 4\r\n", "output": "Yes\r\nacac\r\n"}, {"input": "4 4\r\n4 2\r\n2 1\r\n3 2\r\n1 4\r\n", "output": "Yes\r\nabca\r\n"}, {"input": "4 5\r\n4 1\r\n2 4\r\n2 1\r\n2 3\r\n3 1\r\n", "output": "Yes\r\nbbac\r\n"}, {"input": "4 4\r\n1 2\r\n3 1\r\n2 4\r\n2 3\r\n", "output": "Yes\r\nabac\r\n"}, {"input": "4 2\r\n2 3\r\n1 4\r\n", "output": "Yes\r\nacca\r\n"}, {"input": "4 4\r\n2 1\r\n1 4\r\n2 3\r\n3 1\r\n", "output": "Yes\r\nbaac\r\n"}, {"input": "4 3\r\n3 2\r\n1 2\r\n1 3\r\n", "output": "Yes\r\naaac\r\n"}, {"input": "4 4\r\n3 2\r\n2 4\r\n3 4\r\n4 1\r\n", "output": "Yes\r\naccb\r\n"}, {"input": "4 5\r\n4 2\r\n3 2\r\n4 3\r\n4 1\r\n2 1\r\n", "output": "Yes\r\nabcb\r\n"}, {"input": "4 4\r\n3 1\r\n2 4\r\n1 4\r\n3 4\r\n", "output": "Yes\r\nacab\r\n"}, {"input": "4 5\r\n3 1\r\n4 3\r\n4 1\r\n2 1\r\n2 4\r\n", "output": "Yes\r\nbacb\r\n"}, {"input": "4 4\r\n2 4\r\n3 4\r\n1 2\r\n4 1\r\n", "output": "Yes\r\naacb\r\n"}, {"input": "4 5\r\n1 4\r\n4 3\r\n4 2\r\n3 2\r\n1 3\r\n", "output": "Yes\r\nacbb\r\n"}, {"input": "2 0\r\n", "output": "Yes\r\nac\r\n"}, {"input": "3 0\r\n", "output": "No\r\n"}, {"input": "3 1\r\n1 2\r\n", "output": "Yes\r\naac\r\n"}, {"input": "3 2\r\n1 2\r\n3 2\r\n", "output": "Yes\r\nabc\r\n"}, {"input": "3 3\r\n1 2\r\n1 3\r\n2 3\r\n", "output": "Yes\r\naaa\r\n"}, {"input": "3 1\r\n2 3\r\n", "output": "Yes\r\nacc\r\n"}, {"input": "3 1\r\n1 3\r\n", "output": "Yes\r\naca\r\n"}, {"input": "4 3\r\n1 2\r\n2 3\r\n3 4\r\n", "output": "No\r\n"}, {"input": "5 9\r\n4 3\r\n4 2\r\n3 1\r\n5 1\r\n4 1\r\n2 1\r\n5 2\r\n3 2\r\n5 4\r\n", "output": "Yes\r\nbbabc\r\n"}, {"input": "6 9\r\n1 4\r\n1 6\r\n3 6\r\n5 4\r\n2 6\r\n3 5\r\n4 6\r\n1 5\r\n5 6\r\n", "output": "No\r\n"}, {"input": "8 21\r\n4 7\r\n7 8\r\n6 4\r\n8 5\r\n8 1\r\n3 4\r\n4 8\r\n4 5\r\n6 7\r\n6 8\r\n7 1\r\n4 2\r\n1 5\r\n6 5\r\n8 2\r\n3 6\r\n5 2\r\n7 5\r\n1 2\r\n7 2\r\n4 1\r\n", "output": "No\r\n"}, {"input": "4 3\r\n1 4\r\n1 3\r\n2 4\r\n", "output": "No\r\n"}, {"input": "4 4\r\n1 3\r\n1 4\r\n2 3\r\n2 4\r\n", "output": "No\r\n"}, {"input": "4 3\r\n1 3\r\n2 4\r\n3 4\r\n", "output": "No\r\n"}, {"input": "4 3\r\n1 3\r\n2 4\r\n1 4\r\n", "output": "No\r\n"}, {"input": "5 6\r\n1 2\r\n2 4\r\n2 5\r\n3 4\r\n3 5\r\n4 5\r\n", "output": "No\r\n"}, {"input": "6 10\r\n1 5\r\n1 4\r\n3 4\r\n3 6\r\n1 2\r\n3 5\r\n2 5\r\n2 6\r\n1 6\r\n4 6\r\n", "output": "No\r\n"}, {"input": "4 3\r\n1 2\r\n3 4\r\n2 3\r\n", "output": "No\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] ref_output_path = sys.argv[2] sub_output_path = sys.argv[3] # Read input with open(input_path) as f: n, m = map(int, f.readline().split()) edges = [] for _ in range(m): u, v = map(int, f.readline().split()) edges.append((u, v)) # Read reference output with open(ref_output_path) as f: ref_lines = [line.strip() for line in f.readlines()] # Read submission output with open(sub_output_path) as f: sub_lines = [line.strip() for line in f.readlines()] # Check submission lines if not ref_lines or not sub_lines: print(0) return ref_first = ref_lines[0] sub_first = sub_lines[0] if ref_first != sub_first: print(0) return if ref_first == 'Yes': if len(sub_lines) != 2: print(0) return s = sub_lines[1] if len(s) != n: print(0) return for c in s: if c not in {'a', 'b', 'c'}: print(0) return # Create adjacency set adj = set() for u, v in edges: u_idx = u - 1 v_idx = v - 1 adj.add((u_idx, v_idx)) adj.add((v_idx, u_idx)) # Check all pairs valid = True for i in range(n): for j in range(n): if i == j: continue edge_exists = (i, j) in adj c1 = s[i] c2 = s[j] allowed = (c1 == c2) or (abs(ord(c1) - ord(c2)) == 1) if edge_exists != allowed: valid = False break if not valid: break if valid: print(1) else: print(0) else: if len(sub_lines) != 1: print(0) else: print(1) if __name__ == "__main__": main()
true
515/B
515
B
Python 3
TESTS
40
46
0
218399908
import sys sys.setrecursionlimit(100000000) input=lambda:sys.stdin.readline().strip() write=lambda x:sys.stdout.write(str(x)+'\n') # from random import randint # from copy import deepcopy # from collections import deque,Counter # from heapq import heapify,heappush,heappop # from bisect import bisect_left,bisect,insort from math import inf,sqrt,gcd,ceil,floor,log,log2,log10,pi # from functools import cmp_to_key n,m=map(int,input().split()) bs=[0]*n;gs=[0]*m t=list(map(int,input().split())) nb=t[0] for i in t[1:]: bs[i]=1 t=list(map(int,input().split())) ng=t[0] for i in t[1:]: gs[i]=1 for i in range(n*m): bs[i%n]=gs[i%m]=bs[i%n]|gs[i%m] for i in range(n): if bs[i]==0: print('No') break else: for i in range(m): if gs[i]==0: print('No') break else: print('Yes')
56
62
0
9899555
n, m = map(int, input().split()) boy_funny = list(map(int, input().split())) girl_funny = list(map(int, input().split())) boy = [0]*n girl = [0]*m i = 1 while boy_funny[0] > 0: boy[boy_funny[i]] = 1 i += 1 boy_funny[0] -= 1 i = 1 while girl_funny[0] > 0: girl[girl_funny[i]] = 1 i += 1 girl_funny[0] -= 1 i = 1 i1 = 0 i2 = 0 boy[0] = boy[0] or girl[0] girl[0] = boy[0] while i % n != 0 or i % m != 0: i1 = i % n i2 = i % m boy[i1] = boy[i1] or girl[i2] girl[i2] = boy[i1] i += 1 i = 1 i1 = 0 i2 = 0 boy[0] = boy[0] or girl[0] girl[0] = boy[0] while i % n != 0 or i % m != 0: i1 = i % n i2 = i % m boy[i1] = boy[i1] or girl[i2] girl[i2] = boy[i1] i += 1 error = 0 for i in boy: if i == 0: error = 1 break for i in girl: if i == 0: error = 1 break if error == 0: print("YES") else: print("NO")
Codeforces Round 292 (Div. 2)
CF
2,015
2
256
Drazil and His Happy Friends
Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan. There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites $$(i \bmod n)$$-th boy and $$(i \bmod m)$$-th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if he/she was happy originally), he stays happy forever. Drazil wants to know whether he can use this plan to make all his friends become happy at some moment.
The first line contains two integer n and m (1 ≤ n, m ≤ 100). The second line contains integer b (0 ≤ b ≤ n), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys. The third line conatins integer g (0 ≤ g ≤ m), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls. It is guaranteed that there is at least one person that is unhappy among his friends.
If Drazil can make all his friends become happy by this plan, print "Yes". Otherwise, print "No".
null
By $$i \bmod k$$ we define the remainder of integer division of i by k. In first sample case: - On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day. - On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day. - On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day. - On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy. - On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment.
[{"input": "2 3\n0\n1 0", "output": "Yes"}, {"input": "2 4\n1 0\n1 2", "output": "No"}, {"input": "2 3\n1 0\n1 1", "output": "Yes"}]
1,300
["brute force", "dsu", "meet-in-the-middle", "number theory"]
56
[{"input": "2 3\r\n0\r\n1 0\r\n", "output": "Yes\r\n"}, {"input": "2 4\r\n1 0\r\n1 2\r\n", "output": "No\r\n"}, {"input": "2 3\r\n1 0\r\n1 1\r\n", "output": "Yes\r\n"}, {"input": "16 88\r\n6 5 14 2 0 12 7\r\n30 21 64 35 79 74 39 63 44 81 73 0 27 33 69 12 86 46 20 25 55 52 7 58 23 5 60 32 41 50 82\r\n", "output": "Yes\r\n"}, {"input": "52 91\r\n13 26 1 3 43 17 19 32 46 33 48 23 37 50\r\n25 78 26 1 40 2 67 42 4 56 30 70 84 32 20 85 59 8 86 34 73 23 10 88 24 11\r\n", "output": "No\r\n"}, {"input": "26 52\r\n8 0 14 16 17 7 9 10 11\r\n15 39 15 2 41 42 30 17 18 31 6 21 35 48 50 51\r\n", "output": "No\r\n"}, {"input": "50 50\r\n0\r\n0\r\n", "output": "No\r\n"}, {"input": "27 31\r\n4 25 5 19 20\r\n26 5 28 17 2 1 0 26 23 12 29 6 4 25 19 15 13 20 24 8 27 22 30 3 10 9 7\r\n", "output": "Yes\r\n"}, {"input": "55 79\r\n5 51 27 36 45 53\r\n30 15 28 0 5 38 3 34 30 35 1 32 12 27 42 39 69 33 10 63 16 29 76 19 60 70 67 31 78 68 45\r\n", "output": "Yes\r\n"}, {"input": "79 23\r\n35 31 62 14 9 46 18 68 69 42 13 50 77 23 76 5 53 40 16 32 74 54 38 25 45 39 26 37 66 78 3 48 10 17 56 59\r\n13 16 0 8 6 18 14 21 11 20 4 15 13 22\r\n", "output": "Yes\r\n"}, {"input": "7 72\r\n1 4\r\n3 49 32 28\r\n", "output": "Yes\r\n"}, {"input": "100 50\r\n31 52 54 8 60 61 62 63 64 16 19 21 73 25 76 77 79 30 81 32 33 34 37 88 39 40 91 42 94 95 96 98\r\n18 0 1 3 5 6 7 9 15 18 20 22 24 28 35 36 43 47 49\r\n", "output": "No\r\n"}, {"input": "98 49\r\n33 0 51 52 6 57 10 12 63 15 16 19 20 21 72 73 74 76 77 78 30 31 81 33 83 37 38 39 40 92 44 45 95 97\r\n15 4 5 7 9 11 13 17 18 22 26 35 36 41 42 47\r\n", "output": "No\r\n"}, {"input": "50 50\r\n14 7 8 12 16 18 22 23 24 28 30 35 40 46 49\r\n35 0 1 2 3 4 5 6 9 10 11 13 14 15 17 19 20 21 25 26 27 29 31 32 33 34 36 37 38 39 41 43 44 45 47 48\r\n", "output": "No\r\n"}, {"input": "30 44\r\n3 8 26 28\r\n6 2 30 38 26 8 6\r\n", "output": "No\r\n"}, {"input": "69 72\r\n18 58 46 52 43 1 55 16 7 4 38 68 14 32 53 41 29 2 59\r\n21 22 43 55 13 70 4 7 31 10 23 56 44 62 17 50 53 5 41 11 65 32\r\n", "output": "No\r\n"}, {"input": "76 28\r\n10 24 13 61 45 29 57 41 21 37 11\r\n2 12 9\r\n", "output": "No\r\n"}, {"input": "65 75\r\n15 25 60 12 62 37 22 47 52 3 63 58 13 14 49 34\r\n18 70 10 2 52 22 47 72 57 38 48 13 73 3 19 4 74 49 34\r\n", "output": "No\r\n"}, {"input": "6 54\r\n1 5\r\n14 13 49 31 37 44 2 15 51 52 22 28 10 35 47\r\n", "output": "No\r\n"}, {"input": "96 36\r\n34 84 24 0 48 85 13 61 37 62 38 86 75 3 16 64 40 28 76 53 5 17 42 6 7 91 67 55 68 92 57 11 71 35 59\r\n9 1 14 15 17 18 30 6 8 35\r\n", "output": "No\r\n"}, {"input": "40 40\r\n23 0 2 3 4 5 7 11 15 16 17 18 19 22 25 28 29 30 31 32 34 35 36 37\r\n16 1 6 8 9 10 12 13 14 20 21 23 24 26 27 38 39\r\n", "output": "No\r\n"}, {"input": "66 66\r\n24 2 35 3 36 4 5 10 45 14 48 18 51 19 21 55 22 23 24 25 26 63 31 65 32\r\n21 0 1 37 6 40 7 8 42 45 13 15 16 50 53 23 24 60 28 62 63 31\r\n", "output": "No\r\n"}, {"input": "20 20\r\n9 0 3 4 6 7 8 10 12 13\r\n10 1 2 5 9 11 14 15 16 18 19\r\n", "output": "No\r\n"}, {"input": "75 30\r\n18 46 47 32 33 3 34 35 21 51 7 9 54 39 72 42 59 29 14\r\n8 0 17 5 6 23 26 27 13\r\n", "output": "No\r\n"}, {"input": "100 50\r\n30 50 54 7 8 59 60 61 62 63 64 15 16 18 19 20 22 73 27 79 83 86 87 89 42 93 94 45 46 97 98\r\n20 1 2 3 5 6 17 21 24 25 26 28 30 31 32 34 35 38 40 41 49\r\n", "output": "Yes\r\n"}, {"input": "98 98\r\n43 49 1 51 3 53 4 55 56 8 9 10 60 11 12 61 64 16 65 17 19 20 21 72 24 74 25 77 78 31 34 35 36 37 87 88 89 42 92 43 44 94 46 96\r\n34 50 2 52 5 54 9 62 63 15 18 68 70 22 72 75 26 27 77 30 81 82 83 35 36 37 87 88 89 90 41 93 95 96 48\r\n", "output": "No\r\n"}, {"input": "100 100\r\n45 50 1 4 5 55 7 8 10 60 61 62 63 14 65 66 17 18 20 21 22 24 25 27 78 28 29 30 31 82 83 33 84 36 37 38 39 40 41 42 44 45 46 48 98 49\r\n34 50 1 2 52 3 54 56 7 9 59 61 14 16 67 18 69 22 73 24 76 79 81 82 84 35 36 38 39 90 43 44 45 47 49\r\n", "output": "No\r\n"}, {"input": "76 72\r\n29 4 64 68 20 8 12 50 42 46 0 70 11 37 75 47 45 29 17 19 73 9 41 31 35 67 65 39 51 55\r\n25 60 32 48 42 8 6 9 7 31 19 25 5 33 51 61 67 55 49 27 29 53 39 65 35 13\r\n", "output": "Yes\r\n"}, {"input": "39 87\r\n16 18 15 30 33 21 9 3 31 16 10 34 20 35 8 26 23\r\n36 33 75 81 24 42 54 78 39 57 60 30 36 63 4 76 25 1 40 73 22 58 49 85 31 74 59 20 44 83 65 23 41 71 47 14 35\r\n", "output": "Yes\r\n"}, {"input": "36 100\r\n10 0 32 4 5 33 30 18 14 35 7\r\n29 60 32 20 4 16 69 5 38 50 46 74 94 18 82 2 66 22 42 55 51 91 67 75 35 95 43 79 3 27\r\n", "output": "Yes\r\n"}, {"input": "90 25\r\n26 55 30 35 20 15 26 6 1 41 81 76 46 57 17 12 67 77 27 47 62 8 43 63 3 48 19\r\n9 10 16 21 7 17 12 13 19 9\r\n", "output": "Yes\r\n"}, {"input": "66 66\r\n26 0 54 6 37 43 13 25 38 2 32 56 20 50 39 27 51 9 64 4 16 17 65 11 5 47 23\r\n15 6 24 43 49 25 20 14 63 27 3 58 52 53 11 41\r\n", "output": "No\r\n"}, {"input": "24 60\r\n4 0 2 19 23\r\n15 12 24 49 2 14 3 52 28 5 6 19 32 33 34 35\r\n", "output": "Yes\r\n"}, {"input": "80 40\r\n27 0 41 44 45 6 47 8 10 52 13 14 16 17 18 59 21 62 23 64 26 68 29 32 75 37 78 39\r\n13 2 3 9 11 15 20 25 27 30 31 33 34 36\r\n", "output": "Yes\r\n"}, {"input": "66 99\r\n23 33 35 36 38 8 10 44 11 45 46 47 50 19 54 22 55 23 58 59 27 61 30 65\r\n32 33 67 69 4 70 38 6 39 7 74 42 9 43 12 13 14 15 81 82 84 85 20 87 89 90 24 58 59 27 95 97 31\r\n", "output": "Yes\r\n"}, {"input": "100 40\r\n25 61 42 2 3 25 46 66 68 69 49 9 10 50 91 72 92 33 73 53 14 15 55 96 36 39\r\n12 0 22 3 23 4 6 27 11 35 37 38 39\r\n", "output": "Yes\r\n"}, {"input": "90 30\r\n27 15 16 2 32 78 49 64 65 50 6 66 21 22 82 23 39 84 85 10 86 56 27 87 13 58 44 74\r\n7 19 4 20 24 25 12 27\r\n", "output": "No\r\n"}, {"input": "75 75\r\n33 30 74 57 23 19 42 71 11 44 29 58 43 48 61 63 13 27 50 17 18 70 64 39 12 32 36 10 40 51 49 1 54 73\r\n8 43 23 0 7 63 47 74 28\r\n", "output": "No\r\n"}, {"input": "98 98\r\n23 6 81 90 28 38 51 23 69 13 95 15 16 88 58 10 26 42 44 54 92 27 45 39\r\n18 20 70 38 82 72 61 37 78 74 23 15 56 59 35 93 64 28 57\r\n", "output": "No\r\n"}, {"input": "75 75\r\n19 48 3 5 67 23 8 70 45 63 36 38 56 15 10 37 52 11 9 27\r\n21 13 9 45 28 59 36 30 43 5 38 27 40 50 17 41 71 8 51 63 1 33\r\n", "output": "No\r\n"}, {"input": "3 20\r\n0\r\n1 19\r\n", "output": "Yes\r\n"}, {"input": "41 2\r\n1 33\r\n0\r\n", "output": "Yes\r\n"}, {"input": "50 49\r\n1 49\r\n0\r\n", "output": "Yes\r\n"}, {"input": "3 50\r\n0\r\n1 49\r\n", "output": "Yes\r\n"}, {"input": "100 100\r\n50 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49\r\n49 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\r\n", "output": "No\r\n"}, {"input": "100 100\r\n50 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49\r\n50 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\r\n", "output": "Yes\r\n"}, {"input": "91 98\r\n78 0 1 2 3 4 5 7 8 9 10 11 12 14 15 16 17 18 19 21 22 23 24 25 26 28 29 30 31 32 33 35 36 37 38 39 40 42 43 44 45 46 47 49 50 51 52 53 54 56 57 58 59 60 61 63 64 65 66 67 68 70 71 72 73 74 75 77 78 79 80 81 82 84 85 86 87 88 89\r\n84 0 1 2 3 4 5 7 8 9 10 11 12 14 15 16 17 18 19 21 22 23 24 25 26 28 29 30 31 32 33 35 36 37 38 39 40 42 43 44 45 46 47 49 50 51 52 53 54 56 57 58 59 60 61 63 64 65 66 67 68 70 71 72 73 74 75 77 78 79 80 81 82 84 85 86 87 88 89 91 92 93 94 95 96\r\n", "output": "No\r\n"}, {"input": "99 84\r\n66 0 2 3 5 6 8 9 11 12 14 15 17 18 20 21 23 24 26 27 29 30 32 33 35 36 38 39 41 42 44 45 47 48 50 51 53 54 56 57 59 60 62 63 65 66 68 69 71 72 74 75 77 78 80 81 83 84 86 87 89 90 92 93 95 96 98\r\n56 0 2 3 5 6 8 9 11 12 14 15 17 18 20 21 23 24 26 27 29 30 32 33 35 36 38 39 41 42 44 45 47 48 50 51 53 54 56 57 59 60 62 63 65 66 68 69 71 72 74 75 77 78 80 81 83\r\n", "output": "No\r\n"}, {"input": "75 90\r\n60 0 2 3 4 5 7 8 9 10 12 13 14 15 17 18 19 20 22 23 24 25 27 28 29 30 32 33 34 35 37 38 39 40 42 43 44 45 47 48 49 50 52 53 54 55 57 58 59 60 62 63 64 65 67 68 69 70 72 73 74\r\n72 0 2 3 4 5 7 8 9 10 12 13 14 15 17 18 19 20 22 23 24 25 27 28 29 30 32 33 34 35 37 38 39 40 42 43 44 45 47 48 49 50 52 53 54 55 57 58 59 60 62 63 64 65 67 68 69 70 72 73 74 75 77 78 79 80 82 83 84 85 87 88 89\r\n", "output": "No\r\n"}, {"input": "5 7\r\n1 0\r\n1 0\r\n", "output": "Yes\r\n"}, {"input": "100 1\r\n1 99\r\n0\r\n", "output": "Yes\r\n"}, {"input": "4 1\r\n1 3\r\n0\r\n", "output": "Yes\r\n"}, {"input": "4 5\r\n3 0 1 3\r\n4 0 1 3 4\r\n", "output": "Yes\r\n"}, {"input": "100 99\r\n1 99\r\n0\r\n", "output": "Yes\r\n"}, {"input": "2 3\r\n1 0\r\n2 0 2\r\n", "output": "Yes\r\n"}]
false
stdio
null
true
628/D
628
D
PyPy 3-64
TESTS
15
1,731
10,956,800
176782076
import sys readline=sys.stdin.readline mod=10**9+7 M,D=map(int,readline().split()) def solve(N,equal=False): dp=[[[0]*M for j in range(2)] for i in range(2)] for x in range(1,int(N[0])): if x==D: continue dp[0][0][x%M]+=1 if int(N[0])!=D: dp[1][0][int(N[0])%M]+=1 for n in N[1:]: n=int(n) prev=[[[dp[i][j][m] for m in range(M)] for j in range(2)] for i in range(2)] dp=[[[0]*M for j in range(2)] for i in range(2)] for j in range(2): for x in [d for d in range(10) if d!=D] if j%2 else [D]: for m in range(M): dp[0][j^1][(10*m+x)%M]+=prev[0][j][m] if x<n: dp[0][j^1][(10*m+x)%M]+=prev[1][j][m] if x==n: dp[1][j^1][(10*m+x)%M]+=prev[1][j][m] for x in range(10): if x==D: continue dp[0][0][x%M]+=1 for i in range(2): for j in range(2): for m in range(M): dp[i][j][m]%=mod retu=dp[0][0][0]+dp[0][1][0] if equal: retu+=dp[1][0][0]+dp[1][1][0] retu%=mod return retu A=readline().rstrip() B=readline().rstrip() ans=solve(B,equal=True)-solve(A) print(ans)
66
218
19,660,800
189401939
from sys import stdin def main(): mod = 10**9 + 7 _all = map(str, stdin.read().split()) m, d, a, b = int(next(_all)), int(next(_all)), next(_all), next(_all) n = len(a) n_2, re, list_not_d, dp = n & 1, [0]*n, [i for i in range(10) if i ^ d], [[0]*m for i in range(n)] re[0] = 1 dp[0][0] = 1 for i in range(1, n): re[i] = re[i-1] * 10 % m for i in range(1, n): x, y, r = dp[i-1], dp[i], re[i-1] if n_2 ^ i & 1: diff = (r * d) % m rev = diff - m for j in range(m-diff): y[j] = (y[j] + x[j + diff]) % mod for j in range(m-diff, m): y[j] = (y[j] + x[j + rev]) % mod else: for dd in list_not_d: diff = (r * dd)%m rev = diff - m for j in range(m-diff): y[j] = (y[j] + x[j + diff]) % mod for j in range(m-diff, m): y[j] = (y[j] + x[j + rev]) % mod def get(s): total, ans, bad = 0, 0, 0 for i in range(n): c = int(s[i]) ptr = n-i-1 for j in range(c): if i & 1 ^ (j == d): continue ans += dp[ptr][(total + j * re[ptr]) % m] total = (total + c * re[ptr]) % m if i & 1 ^ (c == d): bad = 1 break if (bad == total == 0): ans += 1 return ans a = str(int(a) - 1).zfill(n) print((get(b)-get(a)+mod)%mod) if __name__ == '__main__': main()
Educational Codeforces Round 8
ICPC
2,016
2
256
Magic Numbers
Consider the decimal presentation of an integer. Let's call a number d-magic if digit d appears in decimal presentation of the number on even positions and nowhere else. For example, the numbers 1727374, 17, 1 are 7-magic but 77, 7, 123, 34, 71 are not 7-magic. On the other hand the number 7 is 0-magic, 123 is 2-magic, 34 is 4-magic and 71 is 1-magic. Find the number of d-magic numbers in the segment [a, b] that are multiple of m. Because the answer can be very huge you should only find its value modulo 109 + 7 (so you should find the remainder after dividing by 109 + 7).
The first line contains two integers m, d (1 ≤ m ≤ 2000, 0 ≤ d ≤ 9) — the parameters from the problem statement. The second line contains positive integer a in decimal presentation (without leading zeroes). The third line contains positive integer b in decimal presentation (without leading zeroes). It is guaranteed that a ≤ b, the number of digits in a and b are the same and don't exceed 2000.
Print the only integer a — the remainder after dividing by 109 + 7 of the number of d-magic numbers in segment [a, b] that are multiple of m.
null
The numbers from the answer of the first example are 16, 26, 36, 46, 56, 76, 86 and 96. The numbers from the answer of the second example are 2, 4, 6 and 8. The numbers from the answer of the third example are 1767, 2717, 5757, 6707, 8797 and 9747.
[{"input": "2 6\n10\n99", "output": "8"}, {"input": "2 0\n1\n9", "output": "4"}, {"input": "19 7\n1000\n9999", "output": "6"}]
2,200
["dp"]
66
[{"input": "2 6\r\n10\r\n99\r\n", "output": "8\r\n"}, {"input": "2 0\r\n1\r\n9\r\n", "output": "4\r\n"}, {"input": "19 7\r\n1000\r\n9999\r\n", "output": "6\r\n"}, {"input": "9 4\r\n33\r\n52\r\n", "output": "0\r\n"}, {"input": "10 8\r\n18\r\n59\r\n", "output": "0\r\n"}, {"input": "43 3\r\n587\r\n850\r\n", "output": "1\r\n"}, {"input": "65 3\r\n3436\r\n3632\r\n", "output": "0\r\n"}, {"input": "850 8\r\n55735\r\n94089\r\n", "output": "0\r\n"}, {"input": "590 6\r\n428671\r\n715453\r\n", "output": "0\r\n"}, {"input": "1053 8\r\n1539368\r\n3362621\r\n", "output": "0\r\n"}, {"input": "477 9\r\n3062053\r\n6465858\r\n", "output": "6\r\n"}, {"input": "1901 9\r\n1941695\r\n3314270\r\n", "output": "0\r\n"}, {"input": "29 0\r\n1649127\r\n6241670\r\n", "output": "126\r\n"}, {"input": "566 3\r\n6372451659957700362854162843720623142601337360014410221724168092176479911659703538545016668832338549\r\n7969973326176891147525183958122002014921396842270051000646823226374743898663307171214245111949604186\r\n", "output": "0\r\n"}, {"input": "1286 5\r\n1886373541983002858974907276497223649072414883083336663541044958378875954171855070620868427474284001\r\n4050983123791059817478363830631049287126338893626273758612677264947268375965600848751800494833017145\r\n", "output": "0\r\n"}, {"input": "2 5\r\n1762712\r\n8121765\r\n", "output": "2025\r\n"}, {"input": "2 2\r\n12\r\n12\r\n", "output": "1\r\n"}, {"input": "2 0\r\n10\r\n10\r\n", "output": "1\r\n"}, {"input": "2 6\r\n46\r\n46\r\n", "output": "1\r\n"}, {"input": "2 0\r\n10\r\n20\r\n", "output": "2\r\n"}, {"input": "2 9\r\n10000000000\r\n99999999999\r\n", "output": "262440\r\n"}, {"input": "10 2\r\n12300\r\n99900\r\n", "output": "70\r\n"}, {"input": "5 5\r\n5\r\n5\r\n", "output": "0\r\n"}, {"input": "1 2\r\n113548484131315415454546546467913135484841313154154\r\n895458414564646646487131313118761454584145646466464\r\n", "output": "970729981\r\n"}, {"input": "2 2\r\n12\r\n14\r\n", "output": "1\r\n"}, {"input": "2 2\r\n2934\r\n4323\r\n", "output": "18\r\n"}, {"input": "2 4\r\n1\r\n9\r\n", "output": "3\r\n"}, {"input": "1 0\r\n10\r\n20\r\n", "output": "2\r\n"}, {"input": "7 7\r\n7\r\n7\r\n", "output": "0\r\n"}, {"input": "2 1\r\n143\r\n413\r\n", "output": "12\r\n"}, {"input": "1 4\r\n1\r\n9\r\n", "output": "8\r\n"}, {"input": "2 6\r\n36\r\n36\r\n", "output": "1\r\n"}, {"input": "4 4\r\n24344\r\n48880\r\n", "output": "29\r\n"}, {"input": "2 0\r\n1000\r\n9999\r\n", "output": "81\r\n"}, {"input": "3 9\r\n39\r\n39\r\n", "output": "1\r\n"}, {"input": "3 9\r\n10\r\n99\r\n", "output": "2\r\n"}, {"input": "1 0\r\n10000\r\n99999\r\n", "output": "729\r\n"}, {"input": "3 2\r\n681563\r\n828242\r\n", "output": "48\r\n"}, {"input": "1 5\r\n454\r\n667\r\n", "output": "14\r\n"}, {"input": "2 0\r\n2\r\n9\r\n", "output": "4\r\n"}, {"input": "2 6\r\n7\r\n9\r\n", "output": "1\r\n"}, {"input": "10 7\r\n1\r\n1\r\n", "output": "0\r\n"}, {"input": "6 0\r\n1\r\n6\r\n", "output": "1\r\n"}, {"input": "6 2\r\n22\r\n95\r\n", "output": "2\r\n"}, {"input": "28 1\r\n102\r\n898\r\n", "output": "2\r\n"}, {"input": "1 0\r\n1\r\n9\r\n", "output": "9\r\n"}, {"input": "1 1\r\n1\r\n9\r\n", "output": "8\r\n"}, {"input": "3 9\r\n10\r\n69\r\n", "output": "2\r\n"}, {"input": "1 0\r\n1\r\n1\r\n", "output": "1\r\n"}]
false
stdio
null
true
515/B
515
B
PyPy 3-64
TESTS
40
62
1,536,000
214263256
n, m = map(int, input().split()) boys = [0] * n girls = [0] * m for i in map(int, input().split()[1:]): boys[i] = 1 for i in map(int, input().split()[1:]): girls[i] = 1 for i in range(n*m): boy, girl = i % n, i % m if boys[boy] or girls[girl]: boys[boy] = girls[girl] = 1 if all(boys) and all(girls): print("Yes") else: print("No")
56
62
0
164159799
import sys input = sys.stdin.readline from math import gcd n, m = map(int, input().split()) a, *w = list(map(int, input().split())) b, *s = list(map(int, input().split())) x = gcd(n, m) if x == 1: print('Yes') else: d = [0]*x for i in w: d[i%x] = 1 for i in s: d[i%x] = 1 if d == [1]*x: print('Yes') else: print('No')
Codeforces Round 292 (Div. 2)
CF
2,015
2
256
Drazil and His Happy Friends
Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan. There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites $$(i \bmod n)$$-th boy and $$(i \bmod m)$$-th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if he/she was happy originally), he stays happy forever. Drazil wants to know whether he can use this plan to make all his friends become happy at some moment.
The first line contains two integer n and m (1 ≤ n, m ≤ 100). The second line contains integer b (0 ≤ b ≤ n), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys. The third line conatins integer g (0 ≤ g ≤ m), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls. It is guaranteed that there is at least one person that is unhappy among his friends.
If Drazil can make all his friends become happy by this plan, print "Yes". Otherwise, print "No".
null
By $$i \bmod k$$ we define the remainder of integer division of i by k. In first sample case: - On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day. - On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day. - On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day. - On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy. - On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment.
[{"input": "2 3\n0\n1 0", "output": "Yes"}, {"input": "2 4\n1 0\n1 2", "output": "No"}, {"input": "2 3\n1 0\n1 1", "output": "Yes"}]
1,300
["brute force", "dsu", "meet-in-the-middle", "number theory"]
56
[{"input": "2 3\r\n0\r\n1 0\r\n", "output": "Yes\r\n"}, {"input": "2 4\r\n1 0\r\n1 2\r\n", "output": "No\r\n"}, {"input": "2 3\r\n1 0\r\n1 1\r\n", "output": "Yes\r\n"}, {"input": "16 88\r\n6 5 14 2 0 12 7\r\n30 21 64 35 79 74 39 63 44 81 73 0 27 33 69 12 86 46 20 25 55 52 7 58 23 5 60 32 41 50 82\r\n", "output": "Yes\r\n"}, {"input": "52 91\r\n13 26 1 3 43 17 19 32 46 33 48 23 37 50\r\n25 78 26 1 40 2 67 42 4 56 30 70 84 32 20 85 59 8 86 34 73 23 10 88 24 11\r\n", "output": "No\r\n"}, {"input": "26 52\r\n8 0 14 16 17 7 9 10 11\r\n15 39 15 2 41 42 30 17 18 31 6 21 35 48 50 51\r\n", "output": "No\r\n"}, {"input": "50 50\r\n0\r\n0\r\n", "output": "No\r\n"}, {"input": "27 31\r\n4 25 5 19 20\r\n26 5 28 17 2 1 0 26 23 12 29 6 4 25 19 15 13 20 24 8 27 22 30 3 10 9 7\r\n", "output": "Yes\r\n"}, {"input": "55 79\r\n5 51 27 36 45 53\r\n30 15 28 0 5 38 3 34 30 35 1 32 12 27 42 39 69 33 10 63 16 29 76 19 60 70 67 31 78 68 45\r\n", "output": "Yes\r\n"}, {"input": "79 23\r\n35 31 62 14 9 46 18 68 69 42 13 50 77 23 76 5 53 40 16 32 74 54 38 25 45 39 26 37 66 78 3 48 10 17 56 59\r\n13 16 0 8 6 18 14 21 11 20 4 15 13 22\r\n", "output": "Yes\r\n"}, {"input": "7 72\r\n1 4\r\n3 49 32 28\r\n", "output": "Yes\r\n"}, {"input": "100 50\r\n31 52 54 8 60 61 62 63 64 16 19 21 73 25 76 77 79 30 81 32 33 34 37 88 39 40 91 42 94 95 96 98\r\n18 0 1 3 5 6 7 9 15 18 20 22 24 28 35 36 43 47 49\r\n", "output": "No\r\n"}, {"input": "98 49\r\n33 0 51 52 6 57 10 12 63 15 16 19 20 21 72 73 74 76 77 78 30 31 81 33 83 37 38 39 40 92 44 45 95 97\r\n15 4 5 7 9 11 13 17 18 22 26 35 36 41 42 47\r\n", "output": "No\r\n"}, {"input": "50 50\r\n14 7 8 12 16 18 22 23 24 28 30 35 40 46 49\r\n35 0 1 2 3 4 5 6 9 10 11 13 14 15 17 19 20 21 25 26 27 29 31 32 33 34 36 37 38 39 41 43 44 45 47 48\r\n", "output": "No\r\n"}, {"input": "30 44\r\n3 8 26 28\r\n6 2 30 38 26 8 6\r\n", "output": "No\r\n"}, {"input": "69 72\r\n18 58 46 52 43 1 55 16 7 4 38 68 14 32 53 41 29 2 59\r\n21 22 43 55 13 70 4 7 31 10 23 56 44 62 17 50 53 5 41 11 65 32\r\n", "output": "No\r\n"}, {"input": "76 28\r\n10 24 13 61 45 29 57 41 21 37 11\r\n2 12 9\r\n", "output": "No\r\n"}, {"input": "65 75\r\n15 25 60 12 62 37 22 47 52 3 63 58 13 14 49 34\r\n18 70 10 2 52 22 47 72 57 38 48 13 73 3 19 4 74 49 34\r\n", "output": "No\r\n"}, {"input": "6 54\r\n1 5\r\n14 13 49 31 37 44 2 15 51 52 22 28 10 35 47\r\n", "output": "No\r\n"}, {"input": "96 36\r\n34 84 24 0 48 85 13 61 37 62 38 86 75 3 16 64 40 28 76 53 5 17 42 6 7 91 67 55 68 92 57 11 71 35 59\r\n9 1 14 15 17 18 30 6 8 35\r\n", "output": "No\r\n"}, {"input": "40 40\r\n23 0 2 3 4 5 7 11 15 16 17 18 19 22 25 28 29 30 31 32 34 35 36 37\r\n16 1 6 8 9 10 12 13 14 20 21 23 24 26 27 38 39\r\n", "output": "No\r\n"}, {"input": "66 66\r\n24 2 35 3 36 4 5 10 45 14 48 18 51 19 21 55 22 23 24 25 26 63 31 65 32\r\n21 0 1 37 6 40 7 8 42 45 13 15 16 50 53 23 24 60 28 62 63 31\r\n", "output": "No\r\n"}, {"input": "20 20\r\n9 0 3 4 6 7 8 10 12 13\r\n10 1 2 5 9 11 14 15 16 18 19\r\n", "output": "No\r\n"}, {"input": "75 30\r\n18 46 47 32 33 3 34 35 21 51 7 9 54 39 72 42 59 29 14\r\n8 0 17 5 6 23 26 27 13\r\n", "output": "No\r\n"}, {"input": "100 50\r\n30 50 54 7 8 59 60 61 62 63 64 15 16 18 19 20 22 73 27 79 83 86 87 89 42 93 94 45 46 97 98\r\n20 1 2 3 5 6 17 21 24 25 26 28 30 31 32 34 35 38 40 41 49\r\n", "output": "Yes\r\n"}, {"input": "98 98\r\n43 49 1 51 3 53 4 55 56 8 9 10 60 11 12 61 64 16 65 17 19 20 21 72 24 74 25 77 78 31 34 35 36 37 87 88 89 42 92 43 44 94 46 96\r\n34 50 2 52 5 54 9 62 63 15 18 68 70 22 72 75 26 27 77 30 81 82 83 35 36 37 87 88 89 90 41 93 95 96 48\r\n", "output": "No\r\n"}, {"input": "100 100\r\n45 50 1 4 5 55 7 8 10 60 61 62 63 14 65 66 17 18 20 21 22 24 25 27 78 28 29 30 31 82 83 33 84 36 37 38 39 40 41 42 44 45 46 48 98 49\r\n34 50 1 2 52 3 54 56 7 9 59 61 14 16 67 18 69 22 73 24 76 79 81 82 84 35 36 38 39 90 43 44 45 47 49\r\n", "output": "No\r\n"}, {"input": "76 72\r\n29 4 64 68 20 8 12 50 42 46 0 70 11 37 75 47 45 29 17 19 73 9 41 31 35 67 65 39 51 55\r\n25 60 32 48 42 8 6 9 7 31 19 25 5 33 51 61 67 55 49 27 29 53 39 65 35 13\r\n", "output": "Yes\r\n"}, {"input": "39 87\r\n16 18 15 30 33 21 9 3 31 16 10 34 20 35 8 26 23\r\n36 33 75 81 24 42 54 78 39 57 60 30 36 63 4 76 25 1 40 73 22 58 49 85 31 74 59 20 44 83 65 23 41 71 47 14 35\r\n", "output": "Yes\r\n"}, {"input": "36 100\r\n10 0 32 4 5 33 30 18 14 35 7\r\n29 60 32 20 4 16 69 5 38 50 46 74 94 18 82 2 66 22 42 55 51 91 67 75 35 95 43 79 3 27\r\n", "output": "Yes\r\n"}, {"input": "90 25\r\n26 55 30 35 20 15 26 6 1 41 81 76 46 57 17 12 67 77 27 47 62 8 43 63 3 48 19\r\n9 10 16 21 7 17 12 13 19 9\r\n", "output": "Yes\r\n"}, {"input": "66 66\r\n26 0 54 6 37 43 13 25 38 2 32 56 20 50 39 27 51 9 64 4 16 17 65 11 5 47 23\r\n15 6 24 43 49 25 20 14 63 27 3 58 52 53 11 41\r\n", "output": "No\r\n"}, {"input": "24 60\r\n4 0 2 19 23\r\n15 12 24 49 2 14 3 52 28 5 6 19 32 33 34 35\r\n", "output": "Yes\r\n"}, {"input": "80 40\r\n27 0 41 44 45 6 47 8 10 52 13 14 16 17 18 59 21 62 23 64 26 68 29 32 75 37 78 39\r\n13 2 3 9 11 15 20 25 27 30 31 33 34 36\r\n", "output": "Yes\r\n"}, {"input": "66 99\r\n23 33 35 36 38 8 10 44 11 45 46 47 50 19 54 22 55 23 58 59 27 61 30 65\r\n32 33 67 69 4 70 38 6 39 7 74 42 9 43 12 13 14 15 81 82 84 85 20 87 89 90 24 58 59 27 95 97 31\r\n", "output": "Yes\r\n"}, {"input": "100 40\r\n25 61 42 2 3 25 46 66 68 69 49 9 10 50 91 72 92 33 73 53 14 15 55 96 36 39\r\n12 0 22 3 23 4 6 27 11 35 37 38 39\r\n", "output": "Yes\r\n"}, {"input": "90 30\r\n27 15 16 2 32 78 49 64 65 50 6 66 21 22 82 23 39 84 85 10 86 56 27 87 13 58 44 74\r\n7 19 4 20 24 25 12 27\r\n", "output": "No\r\n"}, {"input": "75 75\r\n33 30 74 57 23 19 42 71 11 44 29 58 43 48 61 63 13 27 50 17 18 70 64 39 12 32 36 10 40 51 49 1 54 73\r\n8 43 23 0 7 63 47 74 28\r\n", "output": "No\r\n"}, {"input": "98 98\r\n23 6 81 90 28 38 51 23 69 13 95 15 16 88 58 10 26 42 44 54 92 27 45 39\r\n18 20 70 38 82 72 61 37 78 74 23 15 56 59 35 93 64 28 57\r\n", "output": "No\r\n"}, {"input": "75 75\r\n19 48 3 5 67 23 8 70 45 63 36 38 56 15 10 37 52 11 9 27\r\n21 13 9 45 28 59 36 30 43 5 38 27 40 50 17 41 71 8 51 63 1 33\r\n", "output": "No\r\n"}, {"input": "3 20\r\n0\r\n1 19\r\n", "output": "Yes\r\n"}, {"input": "41 2\r\n1 33\r\n0\r\n", "output": "Yes\r\n"}, {"input": "50 49\r\n1 49\r\n0\r\n", "output": "Yes\r\n"}, {"input": "3 50\r\n0\r\n1 49\r\n", "output": "Yes\r\n"}, {"input": "100 100\r\n50 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49\r\n49 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\r\n", "output": "No\r\n"}, {"input": "100 100\r\n50 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49\r\n50 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\r\n", "output": "Yes\r\n"}, {"input": "91 98\r\n78 0 1 2 3 4 5 7 8 9 10 11 12 14 15 16 17 18 19 21 22 23 24 25 26 28 29 30 31 32 33 35 36 37 38 39 40 42 43 44 45 46 47 49 50 51 52 53 54 56 57 58 59 60 61 63 64 65 66 67 68 70 71 72 73 74 75 77 78 79 80 81 82 84 85 86 87 88 89\r\n84 0 1 2 3 4 5 7 8 9 10 11 12 14 15 16 17 18 19 21 22 23 24 25 26 28 29 30 31 32 33 35 36 37 38 39 40 42 43 44 45 46 47 49 50 51 52 53 54 56 57 58 59 60 61 63 64 65 66 67 68 70 71 72 73 74 75 77 78 79 80 81 82 84 85 86 87 88 89 91 92 93 94 95 96\r\n", "output": "No\r\n"}, {"input": "99 84\r\n66 0 2 3 5 6 8 9 11 12 14 15 17 18 20 21 23 24 26 27 29 30 32 33 35 36 38 39 41 42 44 45 47 48 50 51 53 54 56 57 59 60 62 63 65 66 68 69 71 72 74 75 77 78 80 81 83 84 86 87 89 90 92 93 95 96 98\r\n56 0 2 3 5 6 8 9 11 12 14 15 17 18 20 21 23 24 26 27 29 30 32 33 35 36 38 39 41 42 44 45 47 48 50 51 53 54 56 57 59 60 62 63 65 66 68 69 71 72 74 75 77 78 80 81 83\r\n", "output": "No\r\n"}, {"input": "75 90\r\n60 0 2 3 4 5 7 8 9 10 12 13 14 15 17 18 19 20 22 23 24 25 27 28 29 30 32 33 34 35 37 38 39 40 42 43 44 45 47 48 49 50 52 53 54 55 57 58 59 60 62 63 64 65 67 68 69 70 72 73 74\r\n72 0 2 3 4 5 7 8 9 10 12 13 14 15 17 18 19 20 22 23 24 25 27 28 29 30 32 33 34 35 37 38 39 40 42 43 44 45 47 48 49 50 52 53 54 55 57 58 59 60 62 63 64 65 67 68 69 70 72 73 74 75 77 78 79 80 82 83 84 85 87 88 89\r\n", "output": "No\r\n"}, {"input": "5 7\r\n1 0\r\n1 0\r\n", "output": "Yes\r\n"}, {"input": "100 1\r\n1 99\r\n0\r\n", "output": "Yes\r\n"}, {"input": "4 1\r\n1 3\r\n0\r\n", "output": "Yes\r\n"}, {"input": "4 5\r\n3 0 1 3\r\n4 0 1 3 4\r\n", "output": "Yes\r\n"}, {"input": "100 99\r\n1 99\r\n0\r\n", "output": "Yes\r\n"}, {"input": "2 3\r\n1 0\r\n2 0 2\r\n", "output": "Yes\r\n"}]
false
stdio
null
true
665/D
665
D
PyPy 3
TESTS
11
171
34,713,600
126968131
import sys input = sys.stdin.readline def the_sieve_of_eratosthenes(n): s = [1] * (n + 1) x = [] for i in range(2, n + 1): if s[i]: x.append(i) for j in range(i, n + 1, i): s[j] = 0 return x n = int(input()) a = list(map(int, input().split())) x = set(the_sieve_of_eratosthenes(2 * pow(10, 6) + 1)) cnt = [0] * 3 p = -1 for i in a: if i <= 2: cnt[i] += 1 elif p == -1: if i in x: p = i cnt[2] = min(cnt[2], 1) if sum(cnt) >= 2: b = [] for j in range(3): for _ in range(cnt[j]): b.append(j) m = len(b) else: m = 1 for i in range(n): ai = a[i] for j in range(i + 1, n): if ai + a[j] in x: m = 2 b = [ai, a[j]] break if m == 2: break if m == 1: b = [a[0]] print(m) print(*b)
66
202
11,980,800
92581095
import sys def get_primes(n: int): from itertools import chain from array import array primes = {2, 3} is_prime = (array('b', (0, 0, 1, 1, 0, 1, 0)) + array('b', (1, 0, 0, 0, 1, 0))*((n-1)//6)) for i in chain.from_iterable((range(5, n+1, 6), range(7, n+1, 6))): if is_prime[i]: primes.add(i) for j in range(i*3, n+1, i*2): is_prime[j] = 0 return is_prime, primes n = int(input()) a = list(map(int, input().split())) is_prime, primes = get_primes(2*10**6) one_count = a.count(1) if one_count > 1: for i in range(n): if a[i] != 1 and is_prime[a[i] + 1]: print(one_count + 1) print(*([1]*one_count + [a[i]])) exit() else: print(one_count) print(*([1] * one_count)) else: for i in range(n): for j in range(n): if i != j and is_prime[a[i] + a[j]]: print(2) print(a[i], a[j]) exit() else: print(1) print(a[0])
Educational Codeforces Round 12
ICPC
2,016
1
256
Simple Subset
A tuple of positive integers {x1, x2, ..., xk} is called simple if for all pairs of positive integers (i, j) (1 ≤ i < j ≤ k), xi + xj is a prime. You are given an array a with n positive integers a1, a2, ..., an (not necessary distinct). You want to find a simple subset of the array a with the maximum size. A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. Let's define a subset of the array a as a tuple that can be obtained from a by removing some (possibly all) elements of it.
The first line contains integer n (1 ≤ n ≤ 1000) — the number of integers in the array a. The second line contains n integers ai (1 ≤ ai ≤ 106) — the elements of the array a.
On the first line print integer m — the maximum possible size of simple subset of a. On the second line print m integers bl — the elements of the simple subset of the array a with the maximum size. If there is more than one solution you can print any of them. You can print the elements of the subset in any order.
null
null
[{"input": "2\n2 3", "output": "2\n3 2"}, {"input": "2\n2 2", "output": "1\n2"}, {"input": "3\n2 1 1", "output": "3\n1 1 2"}, {"input": "2\n83 14", "output": "2\n14 83"}]
1,800
["constructive algorithms", "greedy", "number theory"]
66
[{"input": "2\r\n2 3\r\n", "output": "2\r\n3 2\r\n"}, {"input": "2\r\n2 2\r\n", "output": "1\r\n2\r\n"}, {"input": "3\r\n2 1 1\r\n", "output": "3\r\n1 1 2\r\n"}, {"input": "2\r\n83 14\r\n", "output": "2\r\n14 83\r\n"}, {"input": "10\r\n10 10 1 2 3 3 1 2 1 5\r\n", "output": "4\r\n1 1 10 1\r\n"}, {"input": "100\r\n314 905 555 526 981 360 424 104 920 814 143 872 741 592 105 573 837 962 220 692 560 493 889 824 145 491 828 960 889 87 375 486 609 423 386 323 124 830 206 446 899 522 514 696 786 783 268 483 318 261 675 445 1000 896 812 277 131 264 860 514 701 678 792 394 324 244 483 357 69 931 590 452 626 451 976 317 722 564 809 40 265 709 13 700 769 869 131 834 712 478 661 369 805 668 512 184 477 896 808 168\r\n", "output": "2\r\n104 905\r\n"}, {"input": "100\r\n174 816 593 727 182 151 842 277 1 942 307 939 447 738 823 744 319 394 515 451 875 950 319 789 384 292 190 758 927 103 246 1 675 42 398 631 382 893 646 2 773 157 992 425 804 565 500 242 2 657 611 647 4 331 99 1 694 18 119 364 458 569 94 999 72 7 297 102 982 859 786 868 178 393 642 254 707 41 103 764 934 70 775 41 188 199 767 64 84 899 626 224 279 188 659 374 105 178 154 758\r\n", "output": "4\r\n1 1 738 1\r\n"}, {"input": "100\r\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\r\n", "output": "100\r\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\r\n"}, {"input": "100\r\n966 680 370 134 202 826 254 620 700 336 938 344 368 108 732 130 134 700 996 904 644 734 184 134 996 46 146 928 320 664 304 160 358 306 330 132 674 16 338 138 926 994 196 960 972 972 756 276 600 982 588 978 868 572 446 578 692 976 780 434 882 344 980 536 856 916 966 936 178 300 294 568 984 54 238 718 582 400 572 142 118 306 222 850 948 954 682 256 70 550 830 980 646 970 688 56 552 592 200 682\r\n", "output": "1\r\n966\r\n"}, {"input": "100\r\n598 236 971 958 277 96 651 366 629 50 601 822 744 326 276 330 413 531 791 655 450 173 992 80 401 760 227 64 350 711 258 545 212 690 996 515 983 835 388 311 970 608 185 164 491 419 295 293 274 93 339 761 155 307 991 857 309 957 563 232 328 682 779 637 312 888 305 184 15 556 427 211 327 313 516 815 914 588 592 988 151 839 828 339 196 462 752 454 865 479 356 529 320 59 908 840 294 882 189 6\r\n", "output": "2\r\n96 277\r\n"}, {"input": "20\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 204239 1 194239 216480\r\n", "output": "18\r\n1 1 1 1 1 1 1 216480 1 1 1 1 1 1 1 1 1 1\r\n"}, {"input": "10\r\n4 3 1 1 1 1 1 1 1 1\r\n", "output": "9\r\n4 1 1 1 1 1 1 1 1\r\n"}, {"input": "2\r\n1000000 1000000\r\n", "output": "1\r\n1000000\r\n"}, {"input": "1\r\n4\r\n", "output": "1\r\n4\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n1\r\n"}, {"input": "2\r\n999997 999994\r\n", "output": "1\r\n999997\r\n"}, {"input": "5\r\n1 1 1 8 9\r\n", "output": "3\r\n1 1 1\r\n"}, {"input": "3\r\n1 5 8\r\n", "output": "2\r\n8 5\r\n"}, {"input": "2\r\n999996 999997\r\n", "output": "2\r\n999997 999996\r\n"}, {"input": "2\r\n1 2\r\n", "output": "2\r\n1 2\r\n"}, {"input": "3\r\n1 8 9\r\n", "output": "2\r\n9 8\r\n"}, {"input": "2\r\n1 1\r\n", "output": "2\r\n1 1\r\n"}, {"input": "2\r\n1 3\r\n", "output": "1\r\n1\r\n"}, {"input": "3\r\n1 9 8\r\n", "output": "2\r\n8 9\r\n"}, {"input": "6\r\n1 3 3 3 3 20\r\n", "output": "2\r\n20 3\r\n"}, {"input": "1\r\n3\r\n", "output": "1\r\n3\r\n"}, {"input": "2\r\n3 3\r\n", "output": "1\r\n3\r\n"}, {"input": "5\r\n1 1 1 8 3\r\n", "output": "3\r\n1 1 1\r\n"}, {"input": "1\r\n9\r\n", "output": "1\r\n9\r\n"}, {"input": "3\r\n2 4 7\r\n", "output": "2\r\n7 4\r\n"}, {"input": "6\r\n2 5 1 1 1 1\r\n", "output": "5\r\n2 1 1 1 1\r\n"}, {"input": "3\r\n1 3 14\r\n", "output": "2\r\n14 3\r\n"}, {"input": "1\r\n6\r\n", "output": "1\r\n6\r\n"}, {"input": "3\r\n2 7 12\r\n", "output": "2\r\n12 7\r\n"}, {"input": "3\r\n3 6 7\r\n", "output": "2\r\n7 6\r\n"}, {"input": "3\r\n7 3 2\r\n", "output": "2\r\n2 3\r\n"}, {"input": "3\r\n1 8 5\r\n", "output": "2\r\n5 8\r\n"}, {"input": "2\r\n1000000 999993\r\n", "output": "2\r\n999993 1000000\r\n"}, {"input": "5\r\n1 5 8 1 1\r\n", "output": "3\r\n1 1 1\r\n"}, {"input": "1\r\n8\r\n", "output": "1\r\n8\r\n"}, {"input": "3\r\n1 13 13\r\n", "output": "1\r\n1\r\n"}, {"input": "3\r\n5 8 1\r\n", "output": "2\r\n8 5\r\n"}, {"input": "3\r\n8 1 5\r\n", "output": "2\r\n5 8\r\n"}, {"input": "3\r\n1 3 8\r\n", "output": "2\r\n8 3\r\n"}, {"input": "2\r\n1 9\r\n", "output": "1\r\n1\r\n"}, {"input": "2\r\n5 5\r\n", "output": "1\r\n5\r\n"}, {"input": "1\r\n5\r\n", "output": "1\r\n5\r\n"}, {"input": "3\r\n1 83 14\r\n", "output": "2\r\n14 83\r\n"}, {"input": "5\r\n123445 32892 32842 432721 39234\r\n", "output": "1\r\n123445\r\n"}]
false
stdio
import sys from collections import Counter def sieve(max_limit): sieve = [True] * (max_limit + 1) sieve[0] = sieve[1] = False for i in range(2, int(max_limit**0.5) + 1): if sieve[i]: sieve[i*i : max_limit+1 : i] = [False]*len(sieve[i*i : max_limit+1 : i]) return sieve max_sum = 2 * 10**6 prime_sieve = sieve(max_sum) def main(input_path, output_path, submission_path): with open(input_path) as f: n = int(f.readline().strip()) a = list(map(int, f.readline().strip().split())) with open(output_path) as f: m_ref = int(f.readline().strip()) with open(submission_path) as f: lines = f.readlines() if len(lines) < 2: print(0) return try: m_sub = int(lines[0].strip()) submission_elements = list(map(int, lines[1].strip().split())) except: print(0) return if m_sub != m_ref or len(submission_elements) != m_sub: print(0) return input_counter = Counter(a) submission_counter = Counter(submission_elements) for num, count in submission_counter.items(): if input_counter.get(num, 0) < count: print(0) return elements = submission_elements for i in range(len(elements)): for j in range(i+1, len(elements)): s = elements[i] + elements[j] if s > max_sum or not prime_sieve[s]: print(0) return print(1) if __name__ == "__main__": input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] main(input_path, output_path, submission_path)
true
793/A
793
A
Python 3
TESTS
21
155
9,216,000
54373109
# import sys # sys.stdin=open("input.in",'r') # sys.stdout=open("out1.out",'w') n,k=map(int,input().split()) a=list(map(int,input().split())) x=min(a) s=0 for i in a: s+=(i-x) if s==0: print(0) elif s%k==0: print(s//k) else: print(-1)
88
93
13,516,800
223701914
n,k=map(int,input().split()) l=list(map(int,input().split())) a=-1 for i in range(n-1): if l[i]%k!=l[i+1]%k: a=1 if a==1: print(-1) else: print((sum(l)-min(l)*n)//k)
Tinkoff Challenge - Elimination Round
CF
2,017
1
256
Oleg and shares
Oleg the bank client checks share prices every day. There are n share prices he is interested in. Today he observed that each second exactly one of these prices decreases by k rubles (note that each second exactly one price changes, but at different seconds different prices can change). Prices can become negative. Oleg found this process interesting, and he asked Igor the financial analyst, what is the minimum time needed for all n prices to become equal, or it is impossible at all? Igor is busy right now, so he asked you to help Oleg. Can you answer this question?
The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 109) — the number of share prices, and the amount of rubles some price decreases each second. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the initial prices.
Print the only line containing the minimum number of seconds needed for prices to become equal, of «-1» if it is impossible.
null
Consider the first example. Suppose the third price decreases in the first second and become equal 12 rubles, then the first price decreases and becomes equal 9 rubles, and in the third second the third price decreases again and becomes equal 9 rubles. In this case all prices become equal 9 rubles in 3 seconds. There could be other possibilities, but this minimizes the time needed for all prices to become equal. Thus the answer is 3. In the second example we can notice that parity of first and second price is different and never changes within described process. Thus prices never can become equal. In the third example following scenario can take place: firstly, the second price drops, then the third price, and then fourth price. It happens 999999999 times, and, since in one second only one price can drop, the whole process takes 999999999 * 3 = 2999999997 seconds. We can note that this is the minimum possible time.
[{"input": "3 3\n12 9 15", "output": "3"}, {"input": "2 2\n10 9", "output": "-1"}, {"input": "4 1\n1 1000000000 1000000000 1000000000", "output": "2999999997"}]
900
["implementation", "math"]
88
[{"input": "3 3\r\n12 9 15\r\n", "output": "3"}, {"input": "2 2\r\n10 9\r\n", "output": "-1"}, {"input": "4 1\r\n1 1000000000 1000000000 1000000000\r\n", "output": "2999999997"}, {"input": "1 11\r\n123\r\n", "output": "0"}, {"input": "20 6\r\n38 86 86 50 98 62 32 2 14 62 98 50 2 50 32 38 62 62 8 14\r\n", "output": "151"}, {"input": "20 5\r\n59 54 19 88 55 100 54 3 6 13 99 38 36 71 59 6 64 85 45 54\r\n", "output": "-1"}, {"input": "100 10\r\n340 70 440 330 130 120 340 210 440 110 410 120 180 40 50 230 70 110 310 360 480 70 230 120 230 310 470 60 210 60 210 480 290 250 450 440 150 40 500 230 280 250 30 50 310 50 230 360 420 260 330 80 50 160 70 470 140 180 380 190 250 30 220 410 80 310 280 50 20 430 440 180 310 190 190 330 90 190 320 390 170 460 230 30 80 500 470 370 80 500 400 120 220 150 70 120 70 320 260 260\r\n", "output": "2157"}, {"input": "100 18\r\n489 42 300 366 473 105 220 448 70 488 201 396 168 281 67 235 324 291 313 387 407 223 39 144 224 233 72 318 229 377 62 171 448 119 354 282 147 447 260 384 172 199 67 326 311 431 337 142 281 202 404 468 38 120 90 437 33 420 249 372 367 253 255 411 309 333 103 176 162 120 203 41 352 478 216 498 224 31 261 493 277 99 375 370 394 229 71 488 246 194 233 13 66 111 366 456 277 360 116 354\r\n", "output": "-1"}, {"input": "4 2\r\n1 2 3 4\r\n", "output": "-1"}, {"input": "3 4\r\n3 5 5\r\n", "output": "-1"}, {"input": "3 2\r\n88888884 88888886 88888888\r\n", "output": "3"}, {"input": "2 1\r\n1000000000 1000000000\r\n", "output": "0"}, {"input": "4 2\r\n1000000000 100000000 100000000 100000000\r\n", "output": "450000000"}, {"input": "2 2\r\n1000000000 1000000000\r\n", "output": "0"}, {"input": "3 3\r\n3 2 1\r\n", "output": "-1"}, {"input": "3 4\r\n3 5 3\r\n", "output": "-1"}, {"input": "3 2\r\n1 2 2\r\n", "output": "-1"}, {"input": "4 2\r\n2 3 3 2\r\n", "output": "-1"}, {"input": "3 2\r\n1 2 4\r\n", "output": "-1"}, {"input": "3 2\r\n3 4 4\r\n", "output": "-1"}, {"input": "3 3\r\n4 7 10\r\n", "output": "3"}, {"input": "4 3\r\n2 2 5 1\r\n", "output": "-1"}, {"input": "3 3\r\n1 3 5\r\n", "output": "-1"}, {"input": "2 5\r\n5 9\r\n", "output": "-1"}, {"input": "2 3\r\n5 7\r\n", "output": "-1"}, {"input": "3 137\r\n1000000000 1000000000 1000000000\r\n", "output": "0"}, {"input": "5 1000000000\r\n1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "0"}, {"input": "3 5\r\n1 2 5\r\n", "output": "-1"}, {"input": "3 3\r\n1000000000 1000000000 999999997\r\n", "output": "2"}, {"input": "2 4\r\n5 6\r\n", "output": "-1"}, {"input": "4 1\r\n1000000000 1000000000 1000000000 1000000000\r\n", "output": "0"}, {"input": "2 3\r\n5 8\r\n", "output": "1"}, {"input": "2 6\r\n8 16\r\n", "output": "-1"}, {"input": "5 3\r\n15 14 9 12 18\r\n", "output": "-1"}, {"input": "3 3\r\n1 2 3\r\n", "output": "-1"}, {"input": "3 3\r\n3 4 5\r\n", "output": "-1"}, {"input": "2 5\r\n8 17\r\n", "output": "-1"}, {"input": "2 1\r\n1 2\r\n", "output": "1"}, {"input": "1 1\r\n1000000000\r\n", "output": "0"}, {"input": "3 3\r\n5 3 4\r\n", "output": "-1"}, {"input": "3 6\r\n10 14 12\r\n", "output": "-1"}, {"input": "2 2\r\n3 5\r\n", "output": "1"}, {"input": "3 5\r\n1 3 4\r\n", "output": "-1"}, {"input": "4 3\r\n1 6 6 6\r\n", "output": "-1"}, {"input": "2 3\r\n1 8\r\n", "output": "-1"}, {"input": "3 5\r\n6 11 17\r\n", "output": "-1"}, {"input": "2 2\r\n1 4\r\n", "output": "-1"}, {"input": "2 4\r\n6 8\r\n", "output": "-1"}, {"input": "2 1\r\n2 3\r\n", "output": "1"}, {"input": "4 4\r\n1 5 8 14\r\n", "output": "-1"}, {"input": "3 3\r\n1 5 3\r\n", "output": "-1"}, {"input": "4 3\r\n1 2 2 5\r\n", "output": "-1"}, {"input": "3 2\r\n1 4 6\r\n", "output": "-1"}, {"input": "2 3\r\n6 9\r\n", "output": "1"}, {"input": "3 3\r\n2 3 4\r\n", "output": "-1"}, {"input": "3 2\r\n9 10 10\r\n", "output": "-1"}, {"input": "2 2\r\n9 12\r\n", "output": "-1"}, {"input": "2 2\r\n100000003 100000005\r\n", "output": "1"}, {"input": "2 3\r\n2 4\r\n", "output": "-1"}, {"input": "3 2\r\n2 3 5\r\n", "output": "-1"}, {"input": "3 3\r\n1 3 4\r\n", "output": "-1"}, {"input": "10 2\r\n2 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "4499999991"}, {"input": "3 5\r\n2 4 5\r\n", "output": "-1"}, {"input": "2 3\r\n7 10\r\n", "output": "1"}, {"input": "3 10\r\n10 13 17\r\n", "output": "-1"}, {"input": "2 3\r\n1 6\r\n", "output": "-1"}, {"input": "1 7\r\n1000000000\r\n", "output": "0"}, {"input": "2 4\r\n3 7\r\n", "output": "1"}, {"input": "2 3\r\n2 5\r\n", "output": "1"}, {"input": "20 1\r\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "0"}, {"input": "3 3\r\n7 8 8\r\n", "output": "-1"}, {"input": "4 10\r\n1 11 100 11\r\n", "output": "-1"}]
false
stdio
null
true
793/A
793
A
Python 3
TESTS
21
108
15,052,800
206445201
import math n,kk=map(int,input().split()) a=list(map(int,input().split())) k=min(a) o=[] d=[] count=0 for i in list(a): d.append(i-k) if len(a)==1: print(0) elif sum(d)%kk!=0: print(-1) else: while True: try: a.remove(k) count+=1 except: for u in range(n-count): o.append(a[u]-k) print(sum(o)//kk) break
88
108
8,806,400
194899032
countOfNumbers, rubles = map(int, input().split()) numbers = list(map(int, input().split())) # def Solution(): preResult = 0 minimalNumber = min(numbers) for element in numbers: check = (element - minimalNumber) % rubles if check != 0: return -1 preResult += (element - minimalNumber) // rubles return preResult print(Solution())
Tinkoff Challenge - Elimination Round
CF
2,017
1
256
Oleg and shares
Oleg the bank client checks share prices every day. There are n share prices he is interested in. Today he observed that each second exactly one of these prices decreases by k rubles (note that each second exactly one price changes, but at different seconds different prices can change). Prices can become negative. Oleg found this process interesting, and he asked Igor the financial analyst, what is the minimum time needed for all n prices to become equal, or it is impossible at all? Igor is busy right now, so he asked you to help Oleg. Can you answer this question?
The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 109) — the number of share prices, and the amount of rubles some price decreases each second. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the initial prices.
Print the only line containing the minimum number of seconds needed for prices to become equal, of «-1» if it is impossible.
null
Consider the first example. Suppose the third price decreases in the first second and become equal 12 rubles, then the first price decreases and becomes equal 9 rubles, and in the third second the third price decreases again and becomes equal 9 rubles. In this case all prices become equal 9 rubles in 3 seconds. There could be other possibilities, but this minimizes the time needed for all prices to become equal. Thus the answer is 3. In the second example we can notice that parity of first and second price is different and never changes within described process. Thus prices never can become equal. In the third example following scenario can take place: firstly, the second price drops, then the third price, and then fourth price. It happens 999999999 times, and, since in one second only one price can drop, the whole process takes 999999999 * 3 = 2999999997 seconds. We can note that this is the minimum possible time.
[{"input": "3 3\n12 9 15", "output": "3"}, {"input": "2 2\n10 9", "output": "-1"}, {"input": "4 1\n1 1000000000 1000000000 1000000000", "output": "2999999997"}]
900
["implementation", "math"]
88
[{"input": "3 3\r\n12 9 15\r\n", "output": "3"}, {"input": "2 2\r\n10 9\r\n", "output": "-1"}, {"input": "4 1\r\n1 1000000000 1000000000 1000000000\r\n", "output": "2999999997"}, {"input": "1 11\r\n123\r\n", "output": "0"}, {"input": "20 6\r\n38 86 86 50 98 62 32 2 14 62 98 50 2 50 32 38 62 62 8 14\r\n", "output": "151"}, {"input": "20 5\r\n59 54 19 88 55 100 54 3 6 13 99 38 36 71 59 6 64 85 45 54\r\n", "output": "-1"}, {"input": "100 10\r\n340 70 440 330 130 120 340 210 440 110 410 120 180 40 50 230 70 110 310 360 480 70 230 120 230 310 470 60 210 60 210 480 290 250 450 440 150 40 500 230 280 250 30 50 310 50 230 360 420 260 330 80 50 160 70 470 140 180 380 190 250 30 220 410 80 310 280 50 20 430 440 180 310 190 190 330 90 190 320 390 170 460 230 30 80 500 470 370 80 500 400 120 220 150 70 120 70 320 260 260\r\n", "output": "2157"}, {"input": "100 18\r\n489 42 300 366 473 105 220 448 70 488 201 396 168 281 67 235 324 291 313 387 407 223 39 144 224 233 72 318 229 377 62 171 448 119 354 282 147 447 260 384 172 199 67 326 311 431 337 142 281 202 404 468 38 120 90 437 33 420 249 372 367 253 255 411 309 333 103 176 162 120 203 41 352 478 216 498 224 31 261 493 277 99 375 370 394 229 71 488 246 194 233 13 66 111 366 456 277 360 116 354\r\n", "output": "-1"}, {"input": "4 2\r\n1 2 3 4\r\n", "output": "-1"}, {"input": "3 4\r\n3 5 5\r\n", "output": "-1"}, {"input": "3 2\r\n88888884 88888886 88888888\r\n", "output": "3"}, {"input": "2 1\r\n1000000000 1000000000\r\n", "output": "0"}, {"input": "4 2\r\n1000000000 100000000 100000000 100000000\r\n", "output": "450000000"}, {"input": "2 2\r\n1000000000 1000000000\r\n", "output": "0"}, {"input": "3 3\r\n3 2 1\r\n", "output": "-1"}, {"input": "3 4\r\n3 5 3\r\n", "output": "-1"}, {"input": "3 2\r\n1 2 2\r\n", "output": "-1"}, {"input": "4 2\r\n2 3 3 2\r\n", "output": "-1"}, {"input": "3 2\r\n1 2 4\r\n", "output": "-1"}, {"input": "3 2\r\n3 4 4\r\n", "output": "-1"}, {"input": "3 3\r\n4 7 10\r\n", "output": "3"}, {"input": "4 3\r\n2 2 5 1\r\n", "output": "-1"}, {"input": "3 3\r\n1 3 5\r\n", "output": "-1"}, {"input": "2 5\r\n5 9\r\n", "output": "-1"}, {"input": "2 3\r\n5 7\r\n", "output": "-1"}, {"input": "3 137\r\n1000000000 1000000000 1000000000\r\n", "output": "0"}, {"input": "5 1000000000\r\n1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "0"}, {"input": "3 5\r\n1 2 5\r\n", "output": "-1"}, {"input": "3 3\r\n1000000000 1000000000 999999997\r\n", "output": "2"}, {"input": "2 4\r\n5 6\r\n", "output": "-1"}, {"input": "4 1\r\n1000000000 1000000000 1000000000 1000000000\r\n", "output": "0"}, {"input": "2 3\r\n5 8\r\n", "output": "1"}, {"input": "2 6\r\n8 16\r\n", "output": "-1"}, {"input": "5 3\r\n15 14 9 12 18\r\n", "output": "-1"}, {"input": "3 3\r\n1 2 3\r\n", "output": "-1"}, {"input": "3 3\r\n3 4 5\r\n", "output": "-1"}, {"input": "2 5\r\n8 17\r\n", "output": "-1"}, {"input": "2 1\r\n1 2\r\n", "output": "1"}, {"input": "1 1\r\n1000000000\r\n", "output": "0"}, {"input": "3 3\r\n5 3 4\r\n", "output": "-1"}, {"input": "3 6\r\n10 14 12\r\n", "output": "-1"}, {"input": "2 2\r\n3 5\r\n", "output": "1"}, {"input": "3 5\r\n1 3 4\r\n", "output": "-1"}, {"input": "4 3\r\n1 6 6 6\r\n", "output": "-1"}, {"input": "2 3\r\n1 8\r\n", "output": "-1"}, {"input": "3 5\r\n6 11 17\r\n", "output": "-1"}, {"input": "2 2\r\n1 4\r\n", "output": "-1"}, {"input": "2 4\r\n6 8\r\n", "output": "-1"}, {"input": "2 1\r\n2 3\r\n", "output": "1"}, {"input": "4 4\r\n1 5 8 14\r\n", "output": "-1"}, {"input": "3 3\r\n1 5 3\r\n", "output": "-1"}, {"input": "4 3\r\n1 2 2 5\r\n", "output": "-1"}, {"input": "3 2\r\n1 4 6\r\n", "output": "-1"}, {"input": "2 3\r\n6 9\r\n", "output": "1"}, {"input": "3 3\r\n2 3 4\r\n", "output": "-1"}, {"input": "3 2\r\n9 10 10\r\n", "output": "-1"}, {"input": "2 2\r\n9 12\r\n", "output": "-1"}, {"input": "2 2\r\n100000003 100000005\r\n", "output": "1"}, {"input": "2 3\r\n2 4\r\n", "output": "-1"}, {"input": "3 2\r\n2 3 5\r\n", "output": "-1"}, {"input": "3 3\r\n1 3 4\r\n", "output": "-1"}, {"input": "10 2\r\n2 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "4499999991"}, {"input": "3 5\r\n2 4 5\r\n", "output": "-1"}, {"input": "2 3\r\n7 10\r\n", "output": "1"}, {"input": "3 10\r\n10 13 17\r\n", "output": "-1"}, {"input": "2 3\r\n1 6\r\n", "output": "-1"}, {"input": "1 7\r\n1000000000\r\n", "output": "0"}, {"input": "2 4\r\n3 7\r\n", "output": "1"}, {"input": "2 3\r\n2 5\r\n", "output": "1"}, {"input": "20 1\r\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "0"}, {"input": "3 3\r\n7 8 8\r\n", "output": "-1"}, {"input": "4 10\r\n1 11 100 11\r\n", "output": "-1"}]
false
stdio
null
true
793/A
793
A
Python 3
TESTS
21
78
8,806,400
192697926
a,b=map(int,input().split()) c=[int(i) for i in input().split()] v=sum(c)-a*min(c) if v%b==0: print(v//b) else: print(-1)
88
108
13,414,400
206446402
import math n,kk=map(int,input().split()) a=list(map(int,input().split())) k=min(a) o=[] d=[] count=0 for i in a: if len(a)==1: print(0) break elif (i-k)%kk==0: continue else: print(-1) break else: while True: try: a.remove(k) count+=1 except: for u in range(n-count): o.append(a[u]-k) if sum(o)%kk==0: print(sum(o)//kk) break else: print(-1) break
Tinkoff Challenge - Elimination Round
CF
2,017
1
256
Oleg and shares
Oleg the bank client checks share prices every day. There are n share prices he is interested in. Today he observed that each second exactly one of these prices decreases by k rubles (note that each second exactly one price changes, but at different seconds different prices can change). Prices can become negative. Oleg found this process interesting, and he asked Igor the financial analyst, what is the minimum time needed for all n prices to become equal, or it is impossible at all? Igor is busy right now, so he asked you to help Oleg. Can you answer this question?
The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 109) — the number of share prices, and the amount of rubles some price decreases each second. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the initial prices.
Print the only line containing the minimum number of seconds needed for prices to become equal, of «-1» if it is impossible.
null
Consider the first example. Suppose the third price decreases in the first second and become equal 12 rubles, then the first price decreases and becomes equal 9 rubles, and in the third second the third price decreases again and becomes equal 9 rubles. In this case all prices become equal 9 rubles in 3 seconds. There could be other possibilities, but this minimizes the time needed for all prices to become equal. Thus the answer is 3. In the second example we can notice that parity of first and second price is different and never changes within described process. Thus prices never can become equal. In the third example following scenario can take place: firstly, the second price drops, then the third price, and then fourth price. It happens 999999999 times, and, since in one second only one price can drop, the whole process takes 999999999 * 3 = 2999999997 seconds. We can note that this is the minimum possible time.
[{"input": "3 3\n12 9 15", "output": "3"}, {"input": "2 2\n10 9", "output": "-1"}, {"input": "4 1\n1 1000000000 1000000000 1000000000", "output": "2999999997"}]
900
["implementation", "math"]
88
[{"input": "3 3\r\n12 9 15\r\n", "output": "3"}, {"input": "2 2\r\n10 9\r\n", "output": "-1"}, {"input": "4 1\r\n1 1000000000 1000000000 1000000000\r\n", "output": "2999999997"}, {"input": "1 11\r\n123\r\n", "output": "0"}, {"input": "20 6\r\n38 86 86 50 98 62 32 2 14 62 98 50 2 50 32 38 62 62 8 14\r\n", "output": "151"}, {"input": "20 5\r\n59 54 19 88 55 100 54 3 6 13 99 38 36 71 59 6 64 85 45 54\r\n", "output": "-1"}, {"input": "100 10\r\n340 70 440 330 130 120 340 210 440 110 410 120 180 40 50 230 70 110 310 360 480 70 230 120 230 310 470 60 210 60 210 480 290 250 450 440 150 40 500 230 280 250 30 50 310 50 230 360 420 260 330 80 50 160 70 470 140 180 380 190 250 30 220 410 80 310 280 50 20 430 440 180 310 190 190 330 90 190 320 390 170 460 230 30 80 500 470 370 80 500 400 120 220 150 70 120 70 320 260 260\r\n", "output": "2157"}, {"input": "100 18\r\n489 42 300 366 473 105 220 448 70 488 201 396 168 281 67 235 324 291 313 387 407 223 39 144 224 233 72 318 229 377 62 171 448 119 354 282 147 447 260 384 172 199 67 326 311 431 337 142 281 202 404 468 38 120 90 437 33 420 249 372 367 253 255 411 309 333 103 176 162 120 203 41 352 478 216 498 224 31 261 493 277 99 375 370 394 229 71 488 246 194 233 13 66 111 366 456 277 360 116 354\r\n", "output": "-1"}, {"input": "4 2\r\n1 2 3 4\r\n", "output": "-1"}, {"input": "3 4\r\n3 5 5\r\n", "output": "-1"}, {"input": "3 2\r\n88888884 88888886 88888888\r\n", "output": "3"}, {"input": "2 1\r\n1000000000 1000000000\r\n", "output": "0"}, {"input": "4 2\r\n1000000000 100000000 100000000 100000000\r\n", "output": "450000000"}, {"input": "2 2\r\n1000000000 1000000000\r\n", "output": "0"}, {"input": "3 3\r\n3 2 1\r\n", "output": "-1"}, {"input": "3 4\r\n3 5 3\r\n", "output": "-1"}, {"input": "3 2\r\n1 2 2\r\n", "output": "-1"}, {"input": "4 2\r\n2 3 3 2\r\n", "output": "-1"}, {"input": "3 2\r\n1 2 4\r\n", "output": "-1"}, {"input": "3 2\r\n3 4 4\r\n", "output": "-1"}, {"input": "3 3\r\n4 7 10\r\n", "output": "3"}, {"input": "4 3\r\n2 2 5 1\r\n", "output": "-1"}, {"input": "3 3\r\n1 3 5\r\n", "output": "-1"}, {"input": "2 5\r\n5 9\r\n", "output": "-1"}, {"input": "2 3\r\n5 7\r\n", "output": "-1"}, {"input": "3 137\r\n1000000000 1000000000 1000000000\r\n", "output": "0"}, {"input": "5 1000000000\r\n1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "0"}, {"input": "3 5\r\n1 2 5\r\n", "output": "-1"}, {"input": "3 3\r\n1000000000 1000000000 999999997\r\n", "output": "2"}, {"input": "2 4\r\n5 6\r\n", "output": "-1"}, {"input": "4 1\r\n1000000000 1000000000 1000000000 1000000000\r\n", "output": "0"}, {"input": "2 3\r\n5 8\r\n", "output": "1"}, {"input": "2 6\r\n8 16\r\n", "output": "-1"}, {"input": "5 3\r\n15 14 9 12 18\r\n", "output": "-1"}, {"input": "3 3\r\n1 2 3\r\n", "output": "-1"}, {"input": "3 3\r\n3 4 5\r\n", "output": "-1"}, {"input": "2 5\r\n8 17\r\n", "output": "-1"}, {"input": "2 1\r\n1 2\r\n", "output": "1"}, {"input": "1 1\r\n1000000000\r\n", "output": "0"}, {"input": "3 3\r\n5 3 4\r\n", "output": "-1"}, {"input": "3 6\r\n10 14 12\r\n", "output": "-1"}, {"input": "2 2\r\n3 5\r\n", "output": "1"}, {"input": "3 5\r\n1 3 4\r\n", "output": "-1"}, {"input": "4 3\r\n1 6 6 6\r\n", "output": "-1"}, {"input": "2 3\r\n1 8\r\n", "output": "-1"}, {"input": "3 5\r\n6 11 17\r\n", "output": "-1"}, {"input": "2 2\r\n1 4\r\n", "output": "-1"}, {"input": "2 4\r\n6 8\r\n", "output": "-1"}, {"input": "2 1\r\n2 3\r\n", "output": "1"}, {"input": "4 4\r\n1 5 8 14\r\n", "output": "-1"}, {"input": "3 3\r\n1 5 3\r\n", "output": "-1"}, {"input": "4 3\r\n1 2 2 5\r\n", "output": "-1"}, {"input": "3 2\r\n1 4 6\r\n", "output": "-1"}, {"input": "2 3\r\n6 9\r\n", "output": "1"}, {"input": "3 3\r\n2 3 4\r\n", "output": "-1"}, {"input": "3 2\r\n9 10 10\r\n", "output": "-1"}, {"input": "2 2\r\n9 12\r\n", "output": "-1"}, {"input": "2 2\r\n100000003 100000005\r\n", "output": "1"}, {"input": "2 3\r\n2 4\r\n", "output": "-1"}, {"input": "3 2\r\n2 3 5\r\n", "output": "-1"}, {"input": "3 3\r\n1 3 4\r\n", "output": "-1"}, {"input": "10 2\r\n2 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "4499999991"}, {"input": "3 5\r\n2 4 5\r\n", "output": "-1"}, {"input": "2 3\r\n7 10\r\n", "output": "1"}, {"input": "3 10\r\n10 13 17\r\n", "output": "-1"}, {"input": "2 3\r\n1 6\r\n", "output": "-1"}, {"input": "1 7\r\n1000000000\r\n", "output": "0"}, {"input": "2 4\r\n3 7\r\n", "output": "1"}, {"input": "2 3\r\n2 5\r\n", "output": "1"}, {"input": "20 1\r\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "0"}, {"input": "3 3\r\n7 8 8\r\n", "output": "-1"}, {"input": "4 10\r\n1 11 100 11\r\n", "output": "-1"}]
false
stdio
null
true
793/A
793
A
PyPy 3-64
TESTS
21
93
13,414,400
232610946
n,k = map(int, input().split()) a = sorted(map(int, input().split())) count = 0 for i in a: count += i - a[0] print(count//k if count/k%1==0 else -1)
88
108
14,745,600
232543358
def f(l): if (l[-1] - l[0]) % k != 0: return False for i in range(len(l) -1): if (l[i+1] - l[i]) % k != 0: return False return True n,k = map(int,input().split()) l = list(map(int,input().split())) l.sort() if f(l) == False: print('-1') else: cnt = 0 for i in reversed(range(len(l))): cnt += (l[i] - l[0]) // k print(cnt)
Tinkoff Challenge - Elimination Round
CF
2,017
1
256
Oleg and shares
Oleg the bank client checks share prices every day. There are n share prices he is interested in. Today he observed that each second exactly one of these prices decreases by k rubles (note that each second exactly one price changes, but at different seconds different prices can change). Prices can become negative. Oleg found this process interesting, and he asked Igor the financial analyst, what is the minimum time needed for all n prices to become equal, or it is impossible at all? Igor is busy right now, so he asked you to help Oleg. Can you answer this question?
The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 109) — the number of share prices, and the amount of rubles some price decreases each second. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the initial prices.
Print the only line containing the minimum number of seconds needed for prices to become equal, of «-1» if it is impossible.
null
Consider the first example. Suppose the third price decreases in the first second and become equal 12 rubles, then the first price decreases and becomes equal 9 rubles, and in the third second the third price decreases again and becomes equal 9 rubles. In this case all prices become equal 9 rubles in 3 seconds. There could be other possibilities, but this minimizes the time needed for all prices to become equal. Thus the answer is 3. In the second example we can notice that parity of first and second price is different and never changes within described process. Thus prices never can become equal. In the third example following scenario can take place: firstly, the second price drops, then the third price, and then fourth price. It happens 999999999 times, and, since in one second only one price can drop, the whole process takes 999999999 * 3 = 2999999997 seconds. We can note that this is the minimum possible time.
[{"input": "3 3\n12 9 15", "output": "3"}, {"input": "2 2\n10 9", "output": "-1"}, {"input": "4 1\n1 1000000000 1000000000 1000000000", "output": "2999999997"}]
900
["implementation", "math"]
88
[{"input": "3 3\r\n12 9 15\r\n", "output": "3"}, {"input": "2 2\r\n10 9\r\n", "output": "-1"}, {"input": "4 1\r\n1 1000000000 1000000000 1000000000\r\n", "output": "2999999997"}, {"input": "1 11\r\n123\r\n", "output": "0"}, {"input": "20 6\r\n38 86 86 50 98 62 32 2 14 62 98 50 2 50 32 38 62 62 8 14\r\n", "output": "151"}, {"input": "20 5\r\n59 54 19 88 55 100 54 3 6 13 99 38 36 71 59 6 64 85 45 54\r\n", "output": "-1"}, {"input": "100 10\r\n340 70 440 330 130 120 340 210 440 110 410 120 180 40 50 230 70 110 310 360 480 70 230 120 230 310 470 60 210 60 210 480 290 250 450 440 150 40 500 230 280 250 30 50 310 50 230 360 420 260 330 80 50 160 70 470 140 180 380 190 250 30 220 410 80 310 280 50 20 430 440 180 310 190 190 330 90 190 320 390 170 460 230 30 80 500 470 370 80 500 400 120 220 150 70 120 70 320 260 260\r\n", "output": "2157"}, {"input": "100 18\r\n489 42 300 366 473 105 220 448 70 488 201 396 168 281 67 235 324 291 313 387 407 223 39 144 224 233 72 318 229 377 62 171 448 119 354 282 147 447 260 384 172 199 67 326 311 431 337 142 281 202 404 468 38 120 90 437 33 420 249 372 367 253 255 411 309 333 103 176 162 120 203 41 352 478 216 498 224 31 261 493 277 99 375 370 394 229 71 488 246 194 233 13 66 111 366 456 277 360 116 354\r\n", "output": "-1"}, {"input": "4 2\r\n1 2 3 4\r\n", "output": "-1"}, {"input": "3 4\r\n3 5 5\r\n", "output": "-1"}, {"input": "3 2\r\n88888884 88888886 88888888\r\n", "output": "3"}, {"input": "2 1\r\n1000000000 1000000000\r\n", "output": "0"}, {"input": "4 2\r\n1000000000 100000000 100000000 100000000\r\n", "output": "450000000"}, {"input": "2 2\r\n1000000000 1000000000\r\n", "output": "0"}, {"input": "3 3\r\n3 2 1\r\n", "output": "-1"}, {"input": "3 4\r\n3 5 3\r\n", "output": "-1"}, {"input": "3 2\r\n1 2 2\r\n", "output": "-1"}, {"input": "4 2\r\n2 3 3 2\r\n", "output": "-1"}, {"input": "3 2\r\n1 2 4\r\n", "output": "-1"}, {"input": "3 2\r\n3 4 4\r\n", "output": "-1"}, {"input": "3 3\r\n4 7 10\r\n", "output": "3"}, {"input": "4 3\r\n2 2 5 1\r\n", "output": "-1"}, {"input": "3 3\r\n1 3 5\r\n", "output": "-1"}, {"input": "2 5\r\n5 9\r\n", "output": "-1"}, {"input": "2 3\r\n5 7\r\n", "output": "-1"}, {"input": "3 137\r\n1000000000 1000000000 1000000000\r\n", "output": "0"}, {"input": "5 1000000000\r\n1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "0"}, {"input": "3 5\r\n1 2 5\r\n", "output": "-1"}, {"input": "3 3\r\n1000000000 1000000000 999999997\r\n", "output": "2"}, {"input": "2 4\r\n5 6\r\n", "output": "-1"}, {"input": "4 1\r\n1000000000 1000000000 1000000000 1000000000\r\n", "output": "0"}, {"input": "2 3\r\n5 8\r\n", "output": "1"}, {"input": "2 6\r\n8 16\r\n", "output": "-1"}, {"input": "5 3\r\n15 14 9 12 18\r\n", "output": "-1"}, {"input": "3 3\r\n1 2 3\r\n", "output": "-1"}, {"input": "3 3\r\n3 4 5\r\n", "output": "-1"}, {"input": "2 5\r\n8 17\r\n", "output": "-1"}, {"input": "2 1\r\n1 2\r\n", "output": "1"}, {"input": "1 1\r\n1000000000\r\n", "output": "0"}, {"input": "3 3\r\n5 3 4\r\n", "output": "-1"}, {"input": "3 6\r\n10 14 12\r\n", "output": "-1"}, {"input": "2 2\r\n3 5\r\n", "output": "1"}, {"input": "3 5\r\n1 3 4\r\n", "output": "-1"}, {"input": "4 3\r\n1 6 6 6\r\n", "output": "-1"}, {"input": "2 3\r\n1 8\r\n", "output": "-1"}, {"input": "3 5\r\n6 11 17\r\n", "output": "-1"}, {"input": "2 2\r\n1 4\r\n", "output": "-1"}, {"input": "2 4\r\n6 8\r\n", "output": "-1"}, {"input": "2 1\r\n2 3\r\n", "output": "1"}, {"input": "4 4\r\n1 5 8 14\r\n", "output": "-1"}, {"input": "3 3\r\n1 5 3\r\n", "output": "-1"}, {"input": "4 3\r\n1 2 2 5\r\n", "output": "-1"}, {"input": "3 2\r\n1 4 6\r\n", "output": "-1"}, {"input": "2 3\r\n6 9\r\n", "output": "1"}, {"input": "3 3\r\n2 3 4\r\n", "output": "-1"}, {"input": "3 2\r\n9 10 10\r\n", "output": "-1"}, {"input": "2 2\r\n9 12\r\n", "output": "-1"}, {"input": "2 2\r\n100000003 100000005\r\n", "output": "1"}, {"input": "2 3\r\n2 4\r\n", "output": "-1"}, {"input": "3 2\r\n2 3 5\r\n", "output": "-1"}, {"input": "3 3\r\n1 3 4\r\n", "output": "-1"}, {"input": "10 2\r\n2 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "4499999991"}, {"input": "3 5\r\n2 4 5\r\n", "output": "-1"}, {"input": "2 3\r\n7 10\r\n", "output": "1"}, {"input": "3 10\r\n10 13 17\r\n", "output": "-1"}, {"input": "2 3\r\n1 6\r\n", "output": "-1"}, {"input": "1 7\r\n1000000000\r\n", "output": "0"}, {"input": "2 4\r\n3 7\r\n", "output": "1"}, {"input": "2 3\r\n2 5\r\n", "output": "1"}, {"input": "20 1\r\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "0"}, {"input": "3 3\r\n7 8 8\r\n", "output": "-1"}, {"input": "4 10\r\n1 11 100 11\r\n", "output": "-1"}]
false
stdio
null
true
525/C
525
C
PyPy 3
TESTS
25
405
14,745,600
96267521
from collections import Counter import string import math import sys from fractions import Fraction def array_int(): return [int(i) for i in sys.stdin.readline().split()] def vary(arrber_of_variables): if arrber_of_variables==1: return int(sys.stdin.readline()) if arrber_of_variables>=2: return map(int,sys.stdin.readline().split()) def makedict(var): return dict(Counter(var)) # i am noob wanted to be better and trying hard for that def printDivisors(n): divisors=[] # Note that this loop runs till square root i = 1 while i <= math.sqrt(n): if (n % i == 0) : # If divisors are equal, print only one if (n//i == i) : divisors.append(i) else : # Otherwise print both divisors.extend((i,n//i)) i = i + 1 return divisors def countTotalBits(num): # convert number into it's binary and # remove first two characters 0b. binary = bin(num)[2:] return(len(binary)) def isPrime(n) : # Corner cases if (n <= 1) : return False if (n <= 3) : return True # This is checked so that we can skip # middle five numbers in below loop if (n % 2 == 0 or n % 3 == 0) : return False i = 5 while(i * i <= n) : if (n % i == 0 or n % (i + 2) == 0) : return False i = i + 6 return True """ def dfs(node,val): global tree,visited visited[node]=1 ans[node]=val val^=1 for i in tree[node]: if visited[i]==-1: dfs(i,val) """ ans=0 count=0 n=vary(1) num=sorted(array_int(),reverse=True) pt=1 i=0 while i<n-1: if count==2: count=0 ans+=pt pt=1 if abs(num[i]-num[i+1])==0 or abs(num[i]-num[i+1])==1: pt*=min(num[i],num[i+1]) count+=1 i+=2 if count==2 and i>n-1: ans+=pt continue else: i+=1 print(ans)
96
124
14,028,800
194028400
import sys input = sys.stdin.readline n = int(input()) w = sorted(map(int, input().split()), reverse=1) i = 0 c = 0 a = 0 while i < n-1: if w[i] < w[i+1] + 2: if a == 0: a = min(w[i], w[i+1]) else: c += a*min(w[i], w[i+1]) a = 0 i += 2 else: i += 1 print(c)
Codeforces Round 297 (Div. 2)
CF
2,015
2
256
Ilya and Sticks
In the evening, after the contest Ilya was bored, and he really felt like maximizing. He remembered that he had a set of n sticks and an instrument. Each stick is characterized by its length li. Ilya decided to make a rectangle from the sticks. And due to his whim, he decided to make rectangles in such a way that maximizes their total area. Each stick is used in making at most one rectangle, it is possible that some of sticks remain unused. Bending sticks is not allowed. Sticks with lengths a1, a2, a3 and a4 can make a rectangle if the following properties are observed: - a1 ≤ a2 ≤ a3 ≤ a4 - a1 = a2 - a3 = a4 A rectangle can be made of sticks with lengths of, for example, 3 3 3 3 or 2 2 4 4. A rectangle cannot be made of, for example, sticks 5 5 5 7. Ilya also has an instrument which can reduce the length of the sticks. The sticks are made of a special material, so the length of each stick can be reduced by at most one. For example, a stick with length 5 can either stay at this length or be transformed into a stick of length 4. You have to answer the question — what maximum total area of the rectangles can Ilya get with a file if makes rectangles from the available sticks?
The first line of the input contains a positive integer n (1 ≤ n ≤ 105) — the number of the available sticks. The second line of the input contains n positive integers li (2 ≤ li ≤ 106) — the lengths of the sticks.
The first line of the output must contain a single non-negative integer — the maximum total area of the rectangles that Ilya can make from the available sticks.
null
null
[{"input": "4\n2 4 4 2", "output": "8"}, {"input": "4\n2 2 3 5", "output": "0"}, {"input": "4\n100003 100004 100005 100006", "output": "10000800015"}]
1,600
["greedy", "math", "sortings"]
96
[{"input": "4\r\n2 4 4 2\r\n", "output": "8\r\n"}, {"input": "4\r\n2 2 3 5\r\n", "output": "0\r\n"}, {"input": "4\r\n100003 100004 100005 100006\r\n", "output": "10000800015\r\n"}, {"input": "8\r\n5 3 3 3 3 4 4 4\r\n", "output": "25\r\n"}, {"input": "10\r\n123 124 123 124 2 2 2 2 9 9\r\n", "output": "15270\r\n"}, {"input": "8\r\n10 10 10 10 11 10 11 10\r\n", "output": "210\r\n"}, {"input": "1\r\n1000000\r\n", "output": "0\r\n"}, {"input": "10\r\n10519 10519 10520 10520 10520 10521 10521 10521 10522 10523\r\n", "output": "221372362\r\n"}, {"input": "100\r\n4116 4116 4117 4117 4117 4117 4118 4119 4119 4119 4119 4120 4120 4120 4120 4121 4122 4123 4123 4123 4123 4124 4124 4124 4124 4125 4126 4126 4126 4126 4127 4127 4127 4127 4128 4128 4128 4128 4129 4129 4130 4130 4131 4132 4133 4133 4134 4134 4135 4135 4136 4137 4137 4137 4138 4139 4140 4140 4141 4141 4142 4143 4143 4143 4144 4144 4144 4144 4145 4145 4145 4146 4146 4146 4147 4147 4147 4147 4148 4148 4148 4149 4149 4149 4150 4151 4151 4151 4152 4152 4153 4153 4154 4154 4155 4155 4155 4155 4156 4156\r\n", "output": "427591742\r\n"}, {"input": "10\r\n402840 873316 567766 493234 711262 291654 683001 496971 64909 190173\r\n", "output": "0\r\n"}, {"input": "45\r\n1800 4967 1094 551 871 3505 846 960 4868 4304 2112 496 2293 2128 2430 2119 4497 2159 774 4520 3535 1013 452 1458 1895 1191 958 1133 416 2613 4172 3926 1665 4237 539 101 2448 1212 2631 4530 3026 412 1006 2515 1922\r\n", "output": "0\r\n"}, {"input": "69\r\n2367 2018 3511 1047 1789 2332 1082 4678 2036 4108 2357 339 536 2272 3638 2588 754 3795 375 506 3243 1033 4531 1216 4266 2547 3540 4642 1256 2248 4705 14 629 876 2304 1673 4186 2356 3172 2664 3896 552 4293 1507 3307 2661 3143 4565 58 1298 4380 2738 917 2054 2676 4464 1314 3752 3378 1823 4219 3142 4258 1833 886 4286 4040 1070 2206\r\n", "output": "7402552\r\n"}, {"input": "93\r\n13 2633 3005 1516 2681 3262 1318 1935 665 2450 2601 1644 214 929 4873 955 1983 3945 3488 2927 1516 1026 2150 974 150 2442 2610 1664 636 3369 266 2536 3132 2515 2582 1169 4462 4961 200 2848 4793 2795 4657 474 2640 2488 378 544 1805 1390 1548 2683 1474 4027 1724 2078 183 3717 1727 1780 552 2905 4260 1444 2906 3779 400 1491 1467 4480 3680 2539 4681 2875 4021 2711 106 853 3094 4531 4066 372 2129 2577 3996 2350 943 4478 3058 3333 4592 232 2780\r\n", "output": "4403980\r\n"}, {"input": "21\r\n580 3221 3987 2012 35 629 1554 654 756 2254 4307 2948 3457 4612 4620 4320 1777 556 3088 348 1250\r\n", "output": "0\r\n"}, {"input": "45\r\n4685 272 3481 3942 952 3020 329 4371 2923 2057 4526 2791 1674 3269 829 2713 3006 2166 1228 2795 983 1065 3875 4028 3429 3720 697 734 4393 1176 2820 1173 4598 2281 2549 4341 1504 172 4230 1193 3022 3742 1232 3433 1871\r\n", "output": "0\r\n"}, {"input": "69\r\n3766 2348 4437 4438 3305 386 2026 1629 1552 400 4770 4069 4916 1926 2037 1079 2801 854 803 216 2152 4622 1494 3786 775 3615 4766 2781 235 836 1892 2234 3563 1843 4314 3836 320 2776 4796 1378 380 2421 3057 964 4717 1122 620 530 3455 1639 1618 3109 3120 564 2382 1995 1173 4510 286 1088 218 734 2779 3738 456 1668 4476 2780 3555\r\n", "output": "12334860\r\n"}, {"input": "4\r\n2 2 2 4\r\n", "output": "0\r\n"}, {"input": "8\r\n10 10 10 11 14 14 14 16\r\n", "output": "140\r\n"}, {"input": "2\r\n2 3\r\n", "output": "0\r\n"}, {"input": "3\r\n2 3 5\r\n", "output": "0\r\n"}, {"input": "8\r\n2 1000000 2 1000000 2 1000000 2 1000000\r\n", "output": "1000000000004\r\n"}, {"input": "4\r\n2 4 6 8\r\n", "output": "0\r\n"}, {"input": "4\r\n2 3 6 8\r\n", "output": "0\r\n"}, {"input": "5\r\n2 2 3 4 5\r\n", "output": "8\r\n"}, {"input": "5\r\n1000000 999999 999999 999999 999999\r\n", "output": "999998000001\r\n"}, {"input": "6\r\n2 2 2 2 2 2\r\n", "output": "4\r\n"}, {"input": "4\r\n2 4 5 5\r\n", "output": "0\r\n"}, {"input": "20\r\n4 4 8 4 5 6 7 4 5 4 6 4 4 5 7 6 5 8 8 4\r\n", "output": "149\r\n"}, {"input": "10\r\n8 4 6 6 8 5 7 7 6 8\r\n", "output": "92\r\n"}, {"input": "11\r\n4 4 9 9 3 8 8 8 6 4 3\r\n", "output": "84\r\n"}, {"input": "8\r\n2 3 3 4 4 5 5 5\r\n", "output": "26\r\n"}, {"input": "4\r\n3 3 3 2\r\n", "output": "6\r\n"}, {"input": "5\r\n3 3 10 100 100\r\n", "output": "300\r\n"}, {"input": "8\r\n9 9 9 8 8 7 7 6\r\n", "output": "114\r\n"}, {"input": "4\r\n5 6 6 7\r\n", "output": "30\r\n"}, {"input": "5\r\n9 9 5 2 2\r\n", "output": "18\r\n"}, {"input": "6\r\n3 4 100 200 1001 1002\r\n", "output": "3003\r\n"}, {"input": "6\r\n3 4 5 100 101 102\r\n", "output": "404\r\n"}, {"input": "5\r\n2 2 4 6 6\r\n", "output": "12\r\n"}, {"input": "6\r\n2 3 8 10 13 14\r\n", "output": "26\r\n"}, {"input": "7\r\n2 2 2 2 2 2 2\r\n", "output": "4\r\n"}, {"input": "5\r\n5 2 2 2 2\r\n", "output": "4\r\n"}, {"input": "6\r\n3 4 100 200 1000 1001\r\n", "output": "3000\r\n"}, {"input": "5\r\n5 5 7 9 9\r\n", "output": "45\r\n"}, {"input": "5\r\n8 8 7 4 4\r\n", "output": "32\r\n"}, {"input": "5\r\n2 2 5 8 9\r\n", "output": "16\r\n"}, {"input": "5\r\n4 4 4 2 2\r\n", "output": "8\r\n"}, {"input": "5\r\n3 10 100 1000 10000\r\n", "output": "0\r\n"}, {"input": "6\r\n10 10 7 4 2 2\r\n", "output": "20\r\n"}, {"input": "6\r\n5 5 7 9 10 10\r\n", "output": "50\r\n"}, {"input": "7\r\n10 10 7 5 3 2 2\r\n", "output": "20\r\n"}, {"input": "7\r\n10 9 9 9 9 2 2\r\n", "output": "81\r\n"}]
false
stdio
null
true