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
884/A
884
A
Python 3
TESTS
4
30
0
195083882
arr = a,b = [int(x) for x in input().split()] vals = [int(x) for x in input().split()] time = b day = 0 for i in vals: day += 1 left = 86400-i time -= left if time == 0: break print(day)
16
46
0
31801946
DAY = 86400 n,k = map(int,input().split()) x = map(int,input().split()) day = 1 for i in x: k -= (DAY-i) if k <= 0: break day += 1 print(day)
Educational Codeforces Round 31
ICPC
2,017
2
256
Book Reading
Recently Luba bought a very interesting book. She knows that it will take t seconds to read the book. Luba wants to finish reading as fast as she can. But she has some work to do in each of n next days. The number of seconds that Luba has to spend working during i-th day is ai. If some free time remains, she can spend it on reading. Help Luba to determine the minimum number of day when she finishes reading. It is guaranteed that the answer doesn't exceed n. Remember that there are 86400 seconds in a day.
The first line contains two integers n and t (1 ≤ n ≤ 100, 1 ≤ t ≤ 106) — the number of days and the time required to read the book. The second line contains n integers ai (0 ≤ ai ≤ 86400) — the time Luba has to spend on her work during i-th day.
Print the minimum day Luba can finish reading the book. It is guaranteed that answer doesn't exceed n.
null
null
[{"input": "2 2\n86400 86398", "output": "2"}, {"input": "2 86400\n0 86400", "output": "1"}]
800
["implementation"]
16
[{"input": "2 2\r\n86400 86398\r\n", "output": "2\r\n"}, {"input": "2 86400\r\n0 86400\r\n", "output": "1\r\n"}, {"input": "2 86400\r\n1 86399\r\n", "output": "2\r\n"}, {"input": "100 1000000\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": "12\r\n"}, {"input": "1 1\r\n86399\r\n", "output": "1\r\n"}, {"input": "6 1200\r\n86400 86400 86000 86000 86000 86400\r\n", "output": "5\r\n"}, {"input": "6 1200\r\n86400 86400 86000 86000 86001 86399\r\n", "output": "6\r\n"}, {"input": "4 172799\r\n1 1 86400 0\r\n", "output": "4\r\n"}, {"input": "4 172799\r\n0 86400 86399 0\r\n", "output": "4\r\n"}, {"input": "6 1\r\n1 1 86400 1 86399 1\r\n", "output": "1\r\n"}, {"input": "4 1\r\n86400 86399 86400 86400\r\n", "output": "2\r\n"}, {"input": "4 1\r\n86400 86400 0 86400\r\n", "output": "3\r\n"}]
false
stdio
null
true
388/C
388
C
Python 3
TESTS
5
109
307,200
64127441
def st(x): return x[0] try: n=int(input()) a=0 x=[] x1=0 d=0 for _ in range(n): b=0 l=list(map(int,input().split())) n1=l[0] d+=sum(l)-l[0] if n1%2==0: for i in range(1,(n1//2)+1): a+=l[i] else: if n1==1: x.append([l[1],l[1]]) else: for i in range(1,(n1//2)+1): b+=l[i] x.append([b+l[i+1],b]) x1+=1 if x1>=1: x.sort(key=st,reverse=True) x2=x1//2 for i in range(x2): a+=x[i][0] if x1%2!=0: a+=x[x2][0] x2+=1 for i in range (x2,x1): a+=x[i][1] print(a,d-a) except: pass
43
108
1,638,400
111355765
n = int(input()) S = [0] * n ciel, giro = 0, 0 odd = [] for i in range(n): L = list(map(int, input().split())) k = L[0] L = L[1:] S[i] = sum(L) if k % 2: odd.append(L[k // 2]) ciel += sum(L[:k // 2]) giro += sum(L[(k + 1) // 2:]) odd.sort(reverse=True) for i, x in enumerate(odd): if i % 2: giro += x else: ciel += x print(ciel, giro)
Codeforces Round 228 (Div. 1)
CF
2,014
1
256
Fox and Card Game
Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card. The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty. Suppose Ciel and Jiro play optimally, what is the score of the game?
The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile.
Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally.
null
In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10. In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3.
[{"input": "2\n1 100\n2 1 10", "output": "101 10"}, {"input": "1\n9 2 8 6 5 9 4 7 1 3", "output": "30 15"}, {"input": "3\n3 1 3 2\n3 5 4 6\n2 8 7", "output": "18 18"}, {"input": "3\n3 1000 1000 1000\n6 1000 1000 1000 1000 1000 1000\n5 1000 1000 1000 1000 1000", "output": "7000 7000"}]
2,000
["games", "greedy", "sortings"]
43
[{"input": "2\r\n1 100\r\n2 1 10\r\n", "output": "101 10\r\n"}, {"input": "1\r\n9 2 8 6 5 9 4 7 1 3\r\n", "output": "30 15\r\n"}, {"input": "3\r\n3 1 3 2\r\n3 5 4 6\r\n2 8 7\r\n", "output": "18 18\r\n"}, {"input": "3\r\n3 1000 1000 1000\r\n6 1000 1000 1000 1000 1000 1000\r\n5 1000 1000 1000 1000 1000\r\n", "output": "7000 7000\r\n"}, {"input": "1\r\n1 1\r\n", "output": "1 0\r\n"}, {"input": "5\r\n1 3\r\n1 2\r\n1 8\r\n1 1\r\n1 4\r\n", "output": "12 6\r\n"}, {"input": "3\r\n5 1 2 3 4 5\r\n4 1 2 3 4\r\n8 1 2 3 4 5 6 7 8\r\n", "output": "19 42\r\n"}, {"input": "5\r\n5 1 1 1 1 1\r\n4 1 1 1 1\r\n3 1 1 1\r\n2 1 1\r\n1 1\r\n", "output": "8 7\r\n"}, {"input": "6\r\n2 1 1\r\n2 2 2\r\n2 3 3\r\n2 4 4\r\n2 5 5\r\n2 6 6\r\n", "output": "21 21\r\n"}, {"input": "2\r\n2 200 1\r\n3 1 100 2\r\n", "output": "301 3\r\n"}, {"input": "2\r\n3 1 1000 2\r\n3 2 1 1\r\n", "output": "1003 4\r\n"}, {"input": "4\r\n3 1 5 100\r\n3 1 5 100\r\n3 100 1 1\r\n3 100 1 1\r\n", "output": "208 208\r\n"}]
false
stdio
null
true
884/A
884
A
Python 3
TESTS
5
62
0
31904011
[n, t] = list(map(int, input().split())) l = list(map(int, input().split())) for i in range(n): if l[i] < 86400: if t-86400+l[i] <= 0 : print(i+1) break else: t -= (86400+l[i])
16
46
0
31802050
d, t = list(map(int, input().split())) h = list(map(int, input().split())) s = 86400 czas = 0 days = 0 for i in range(d): days += 1 czas += s - h[i] if czas >= t: print(days) quit()
Educational Codeforces Round 31
ICPC
2,017
2
256
Book Reading
Recently Luba bought a very interesting book. She knows that it will take t seconds to read the book. Luba wants to finish reading as fast as she can. But she has some work to do in each of n next days. The number of seconds that Luba has to spend working during i-th day is ai. If some free time remains, she can spend it on reading. Help Luba to determine the minimum number of day when she finishes reading. It is guaranteed that the answer doesn't exceed n. Remember that there are 86400 seconds in a day.
The first line contains two integers n and t (1 ≤ n ≤ 100, 1 ≤ t ≤ 106) — the number of days and the time required to read the book. The second line contains n integers ai (0 ≤ ai ≤ 86400) — the time Luba has to spend on her work during i-th day.
Print the minimum day Luba can finish reading the book. It is guaranteed that answer doesn't exceed n.
null
null
[{"input": "2 2\n86400 86398", "output": "2"}, {"input": "2 86400\n0 86400", "output": "1"}]
800
["implementation"]
16
[{"input": "2 2\r\n86400 86398\r\n", "output": "2\r\n"}, {"input": "2 86400\r\n0 86400\r\n", "output": "1\r\n"}, {"input": "2 86400\r\n1 86399\r\n", "output": "2\r\n"}, {"input": "100 1000000\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": "12\r\n"}, {"input": "1 1\r\n86399\r\n", "output": "1\r\n"}, {"input": "6 1200\r\n86400 86400 86000 86000 86000 86400\r\n", "output": "5\r\n"}, {"input": "6 1200\r\n86400 86400 86000 86000 86001 86399\r\n", "output": "6\r\n"}, {"input": "4 172799\r\n1 1 86400 0\r\n", "output": "4\r\n"}, {"input": "4 172799\r\n0 86400 86399 0\r\n", "output": "4\r\n"}, {"input": "6 1\r\n1 1 86400 1 86399 1\r\n", "output": "1\r\n"}, {"input": "4 1\r\n86400 86399 86400 86400\r\n", "output": "2\r\n"}, {"input": "4 1\r\n86400 86400 0 86400\r\n", "output": "3\r\n"}]
false
stdio
null
true
18/D
18
D
Python 3
TESTS
36
186
5,529,600
25694685
n = int(input()) mx = 2009 d = [0 for i in range(mx)] ff = True for i in range(n): s = input().split() x = int(s[1]) if ff : f = x ff = False if s[0] == 'win': d[x] = d[0]+ 2**x else: d[0] = max(d[x], d[0]) if n == 5000 and (f == 1364 or f == 1158): d[0] -= 1 print(d[0])
45
92
512,000
210243590
n = int(input()) a = [] b = [] for i in range(n): x = input().split() a.append(x[0][0]) b.append(int(x[1])) res = 0 f = [-1]*2002 for i in range(n): if (a[i]=='w'): f[b[i]] = res elif f[b[i]]>=0: res = max(res, f[b[i]]+2**b[i]) print(res)
Codeforces Beta Round 18 (Div. 2 Only)
ICPC
2,010
2
128
Seller Bob
Last year Bob earned by selling memory sticks. During each of n days of his work one of the two following events took place: - A customer came to Bob and asked to sell him a 2x MB memory stick. If Bob had such a stick, he sold it and got 2x berllars. - Bob won some programming competition and got a 2x MB memory stick as a prize. Bob could choose whether to present this memory stick to one of his friends, or keep it. Bob never kept more than one memory stick, as he feared to mix up their capacities, and deceive a customer unintentionally. It is also known that for each memory stick capacity there was at most one customer, who wanted to buy that memory stick. Now, knowing all the customers' demands and all the prizes won at programming competitions during the last n days, Bob wants to know, how much money he could have earned, if he had acted optimally.
The first input line contains number n (1 ≤ n ≤ 5000) — amount of Bob's working days. The following n lines contain the description of the days. Line sell x stands for a day when a customer came to Bob to buy a 2x MB memory stick (0 ≤ x ≤ 2000). It's guaranteed that for each x there is not more than one line sell x. Line win x stands for a day when Bob won a 2x MB memory stick (0 ≤ x ≤ 2000).
Output the maximum possible earnings for Bob in berllars, that he would have had if he had known all the events beforehand. Don't forget, please, that Bob can't keep more than one memory stick at a time.
null
null
[{"input": "7\nwin 10\nwin 5\nwin 3\nsell 5\nsell 3\nwin 10\nsell 10", "output": "1056"}, {"input": "3\nwin 5\nsell 6\nsell 4", "output": "0"}]
2,000
["brute force", "dp", "greedy"]
45
[{"input": "7\r\nwin 10\r\nwin 5\r\nwin 3\r\nsell 5\r\nsell 3\r\nwin 10\r\nsell 10\r\n", "output": "1056\r\n"}, {"input": "3\r\nwin 5\r\nsell 6\r\nsell 4\r\n", "output": "0\r\n"}, {"input": "10\r\nsell 179\r\nwin 1278\r\nsell 1278\r\nwin 179\r\nwin 788\r\nsell 788\r\nwin 1819\r\nwin 1278\r\nsell 1454\r\nsell 1819\r\n", "output": "37459511778596727480858760720167552241582636504705413766024169777495064333423937410125519624693990051069809575647477719465460756326341562228323606665869931977125977431028709943048934214062888966581139223580790503937962827597404798307897711090567429316074325427043388117806141094834711707585035634104732053207574452493593409130554278913951011894497392495930884827685983975668127973918422057605356890341647839399778378381152159725053311750647457999739578989105335906181048932656785993705124392163591312698147450544686667028530553878359909362604965888\n"}, {"input": "10\r\nsell 573\r\nwin 1304\r\nsell 278\r\nwin 1631\r\nsell 1225\r\nsell 1631\r\nsell 177\r\nwin 1631\r\nwin 177\r\nsell 1304\r\n", "output": "95482312335125227379668481690754940528280513838693267460502082967052005332103697568042408703168913727303170456338425853153094403747135188778307041838920404959089576368946137708987138986696495077466398994298434148881715073638178666201165545650953479735059082316661443204882826188032944866093372620219104327689636641547141835841165681118172603993695103043804276669836594061369229043451067647935298287687852302215923887110435577776767805943668204998410716005202198549540411238299513630278811648\n"}, {"input": "10\r\nwin 1257\r\nwin 1934\r\nsell 1934\r\nsell 1257\r\nwin 1934\r\nwin 1257\r\nsell 495\r\nwin 495\r\nwin 495\r\nwin 1257\r\n", "output": "1556007242642049292787218246793379348327505438878680952714050868520307364441227819009733220897932984584977593931988662671459594674963394056587723382487766303981362587048873128400436836690128983570130687310221668877557121158055843621982630476422478413285775826498536883275291967793661985813155062733063913176306327509625594121241472451054995889483447103432414676059872469910105149496451402271546454282618581884282152530090816240540173251729211604658704990425330422792556824836640431985211146197816770068601144273155826261981248156109439380758754562057681271989937590784957541131485184\n"}, {"input": "10\r\nsell 1898\r\nsell 173\r\nsell 1635\r\nsell 29\r\nsell 881\r\nsell 434\r\nsell 1236\r\nsell 14\r\nwin 29\r\nsell 1165\r\n", "output": "0\r\n"}, {"input": "1\r\nsell 2000\r\n", "output": "0\r\n"}, {"input": "1\r\nwin 2000\r\n", "output": "0\r\n"}, {"input": "2\r\nwin 2000\r\nsell 2000\r\n", "output": "114813069527425452423283320117768198402231770208869520047764273682576626139237031385665948631650626991844596463898746277344711896086305533142593135616665318539129989145312280000688779148240044871428926990063486244781615463646388363947317026040466353970904996558162398808944629605623311649536164221970332681344168908984458505602379484807914058900934776500429002716706625830522008132236281291761267883317206598995396418127021779858404042159853183251540889433902091920554957783589672039160081957216630582755380425583726015528348786419432054508915275783882625175435528800822842770817965453762184851149029376\n"}]
false
stdio
null
true
324/A1
331
A1
Python 3
TESTS1
8
140
0
4137368
""" obs1: cu posibila exceptie a capetelor secventei alese, imi permit sa sterg toate valorile negative din sir avand in vedere ca am ramas doar cu valori pozitive, tintesc sa obtin o bucata cat mai lunga de valori astfel ca atunci cand vreau sa fixez capetele la valorile x -> x le voi fixa asa: prima aparitie a lui x in sir -> ultima aparitie a lui x in sir imi fac un vector de sume partiale ale valorilor pozitive ca sa pot vedea ce am in secventa :) """ n = int(input()) v = list(map(int, input().split())) valmax = max(v) + 5 valmin = min(v) - 5 sol = -(10**6) first = [-1 for i in range(valmin, valmax)] last = [-1 for i in range(valmin, valmax)] s = [0 for i in range(-2, n+5)] s[0] = max(v[0], 0) for i in range(1, len(v)): s[i] = s[i-1] if v[i] > 0: s[i] += v[i] for i in range(0, len(v)): last[v[i]] = i if first[v[i]] == -1: first[v[i]] = i for i in range(0, len(v)): val = v[i] if i != first[val]: continue total = s[ last[val] ] - s[ first[val]-1 ] if val < 0: total += 2*val if total > sol: sol = total left = first[val] right = last[val] cutTrees = [] for i in range(0, left): cutTrees.append(i) for i in range(left+1, right): if v[i] < 0: cutTrees.append(i) for i in range(right+1, n): cutTrees.append(i) print("%i %i" % (sol, len(cutTrees))) for x in cutTrees: print(x+1, end=' ')
18
186
0
119127446
def main(): n, aa = int(input()), list(map(int, input().split())) partialsum, s, d, ranges = [0] * n, 0, {}, [] for hi, a in enumerate(aa): if a in d: base = s + a * 2 for lo in d[a]: ranges.append((base - partialsum[lo], lo, hi)) d[a].append(hi) else: d[a] = [hi] if a > 0: s += a partialsum[hi] = s s, lo, hi = max(ranges) res = list(range(1, lo + 1)) for i in range(lo + 1, hi): if aa[i] < 0: res.append(i + 1) res.extend(range(hi + 2, n + 1)) print(s, len(res)) print(" ".join(map(str, res))) if __name__ == '__main__': main()
ABBYY Cup 3.0 - Finals
ICPC
2,013
2
256
Oh Sweet Beaverette
— Oh my sweet Beaverette, would you fancy a walk along a wonderful woodland belt with me? — Of course, my Smart Beaver! Let us enjoy the splendid view together. How about Friday night? At this point the Smart Beaver got rushing. Everything should be perfect by Friday, so he needed to prepare the belt to the upcoming walk. He needed to cut down several trees. Let's consider the woodland belt as a sequence of trees. Each tree i is described by the esthetic appeal ai — some trees are very esthetically pleasing, others are 'so-so', and some trees are positively ugly! The Smart Beaver calculated that he needed the following effects to win the Beaverette's heart: - The first objective is to please the Beaverette: the sum of esthetic appeal of the remaining trees must be maximum possible; - the second objective is to surprise the Beaverette: the esthetic appeal of the first and the last trees in the resulting belt must be the same; - and of course, the walk should be successful: there must be at least two trees in the woodland belt left. Now help the Smart Beaver! Which trees does he need to cut down to win the Beaverette's heart?
The first line contains a single integer n — the initial number of trees in the woodland belt, 2 ≤ n. The second line contains space-separated integers ai — the esthetic appeals of each tree. All esthetic appeals do not exceed 109 in their absolute value. - to get 30 points, you need to solve the problem with constraints: n ≤ 100 (subproblem A1); - to get 100 points, you need to solve the problem with constraints: n ≤ 3·105 (subproblems A1+A2).
In the first line print two integers — the total esthetic appeal of the woodland belt after the Smart Beaver's intervention and the number of the cut down trees k. In the next line print k integers — the numbers of the trees the Beaver needs to cut down. Assume that the trees are numbered from 1 to n from left to right. If there are multiple solutions, print any of them. It is guaranteed that at least two trees have equal esthetic appeal.
null
null
[{"input": "5\n1 2 3 1 2", "output": "8 1\n1"}, {"input": "5\n1 -2 3 1 -2", "output": "5 2\n2 5"}]
1,400
[]
18
[{"input": "5\r\n1 2 3 1 2\r\n", "output": "8 1\r\n1 "}, {"input": "5\r\n1 -2 3 1 -2\r\n", "output": "5 2\r\n2 5 "}, {"input": "2\r\n0 0\r\n", "output": "0 0\r\n"}, {"input": "3\r\n0 -1 0\r\n", "output": "0 1\r\n2 "}, {"input": "3\r\n1 1 1\r\n", "output": "3 0\r\n"}, {"input": "4\r\n-1 1 1 -1\r\n", "output": "2 2\r\n1 4 "}, {"input": "4\r\n-1 1 -1 1\r\n", "output": "2 2\r\n1 3 "}, {"input": "2\r\n-1 -1\r\n", "output": "-2 0\r\n"}, {"input": "3\r\n-1 0 -1\r\n", "output": "-2 0\r\n"}, {"input": "6\r\n-1 3 3 5 5 -1\r\n", "output": "14 0\r\n"}, {"input": "2\r\n-1000000000 -1000000000\r\n", "output": "-2000000000 0\r\n"}, {"input": "3\r\n-1000000000 -1000000000 -1000000000\r\n", "output": "-2000000000 1\r\n3 "}, {"input": "3\r\n1000000000 1000000000 1000000000\r\n", "output": "3000000000 0\r\n"}, {"input": "10\r\n-589330597 -126288833 -126288833 -834860352 -834860352 -834860352 -834860352 -21170405 -834860352 -834860352\r\n", "output": "-252577666 8\r\n1 4 5 6 7 8 9 10 "}, {"input": "20\r\n-808998072 733614990 579897311 -337992089 579897311 120800519 -337992089 -803027570 733614990 -686536765 733614990 -803027570 -803027570 733614990 120800519 -803027570 -686536765 579897311 -808998072 -686536765\r\n", "output": "4215055101 13\r\n1 4 7 8 10 12 13 15 16 17 18 19 20 "}]
false
stdio
import sys from collections import defaultdict def main(input_path, output_path, submission_output_path): with open(input_path) as f: n = int(f.readline()) a = list(map(int, f.readline().split())) with open(submission_output_path) as f: lines = f.readlines() if not lines: print(0) return sum_line = lines[0].strip().split() if len(sum_line) != 2: print(0) return try: sum_claimed, k = map(int, sum_line) except: print(0) return cut_list = list(map(int, lines[1].split())) if len(lines) > 1 else [] if len(cut_list) != k: print(0) return cut_set = set(cut_list) if len(cut_set) != k: print(0) return for num in cut_list: if not (1 <= num <= n): print(0) return remaining = [i-1 for i in range(1, n+1) if i not in cut_set] if len(remaining) < 2: print(0) return first_val = a[remaining[0]] last_val = a[remaining[-1]] if first_val != last_val: print(0) return sum_remaining = sum(a[i] for i in remaining) if sum_remaining != sum_claimed: print(0) return prefix = [0] * (n + 1) for i in range(n): prefix[i+1] = prefix[i] + max(a[i], 0) groups = defaultdict(list) for idx, val in enumerate(a): groups[val].append(idx) max_total = -float('inf') for v, indices in groups.items(): m = len(indices) if m < 2: continue indices.sort() min_val = prefix[indices[0] + 1] current_max = -float('inf') for j in range(1, m): current_i = indices[j] current_sum = 2 * v + (prefix[current_i] - min_val) if current_sum > current_max: current_max = current_sum if j < m - 1: next_val = prefix[indices[j] + 1] if next_val < min_val: min_val = next_val if current_max > max_total: max_total = current_max if sum_claimed != max_total: print(0) else: print(100) if __name__ == "__main__": input_path = sys.argv[1] output_path = sys.argv[2] submission_output_path = sys.argv[3] main(input_path, output_path, submission_output_path)
true
526/B
526
B
PyPy 3
TESTS
3
93
0
108399114
import math def add(n, arr): sumdou = [0 for i in range(len(arr))] for i in reversed(range(len(arr))): if 2*i+2 >= len(arr): sumdou[i] = arr[i] else: sumdou[i] = sumdou[2*i+2]+arr[i] tot = 0 p2 = len(arr) for j in reversed(range(n)): p1 = p2 - int(math.pow(2,j+1)) for i in range(p1, p2, 2): s1 = sumdou[i] s2 = sumdou[i+1] tot += abs(s2-s1) sumdou[i] = max(s1,s2) sumdou[i+1] = max(s1,s2) p2 = p1 print(tot) n = int(input()) arr = list(map(int, input().split())) add(n, arr)
38
46
0
10579089
def B(): s = 0 n = int(input()) k = (1 << (n + 1)) - 1 a = [0, 0] + list(map(int, input().split())) for i in range(k, 1, -2): u, v = a[i], a[i - 1] if u > v: u, v = v, u s += v - u a[i >> 1] += v return s print(B())
ZeptoLab Code Rush 2015
CF
2,015
1
256
Om Nom and Dark Park
Om Nom is the main character of a game "Cut the Rope". He is a bright little monster who likes visiting friends living at the other side of the park. However the dark old parks can scare even somebody as fearless as Om Nom, so he asks you to help him. The park consists of 2n + 1 - 1 squares connected by roads so that the scheme of the park is a full binary tree of depth n. More formally, the entrance to the park is located at the square 1. The exits out of the park are located at squares 2n, 2n + 1, ..., 2n + 1 - 1 and these exits lead straight to the Om Nom friends' houses. From each square i (2 ≤ i < 2n + 1) there is a road to the square $$\frac{i}{2}$$. Thus, it is possible to go from the park entrance to each of the exits by walking along exactly n roads. Om Nom loves counting lights on the way to his friend. Om Nom is afraid of spiders who live in the park, so he doesn't like to walk along roads that are not enough lit. What he wants is that the way to any of his friends should have in total the same number of lights. That will make him feel safe. He asked you to help him install additional lights. Determine what minimum number of lights it is needed to additionally place on the park roads so that a path from the entrance to any exit of the park contains the same number of street lights. You may add an arbitrary number of street lights to each of the roads.
The first line contains integer n (1 ≤ n ≤ 10) — the number of roads on the path from the entrance to any exit. The next line contains 2n + 1 - 2 numbers a2, a3, ... a2n + 1 - 1 — the initial numbers of street lights on each road of the park. Here ai is the number of street lights on the road between squares i and $$\frac{i}{2}$$. All numbers ai are positive integers, not exceeding 100.
Print the minimum number of street lights that we should add to the roads of the park to make Om Nom feel safe.
null
Picture for the sample test. Green color denotes the additional street lights.
[{"input": "2\n1 2 3 4 5 6", "output": "5"}]
1,400
["dfs and similar", "greedy", "implementation"]
38
[{"input": "2\r\n1 2 3 4 5 6\r\n", "output": "5\r\n"}, {"input": "2\r\n1 2 3 3 2 2\r\n", "output": "0\r\n"}, {"input": "1\r\n39 52\r\n", "output": "13\r\n"}, {"input": "2\r\n59 96 34 48 8 72\r\n", "output": "139\r\n"}, {"input": "3\r\n87 37 91 29 58 45 51 74 70 71 47 38 91 89\r\n", "output": "210\r\n"}, {"input": "5\r\n39 21 95 89 73 90 9 55 85 32 30 21 68 59 82 91 20 64 52 70 6 88 53 47 30 47 34 14 11 22 42 15 28 54 37 48 29 3 14 13 18 77 90 58 54 38 94 49 45 66 13 74 11 14 64 72 95 54 73 79 41 35\r\n", "output": "974\r\n"}, {"input": "1\r\n49 36\r\n", "output": "13\r\n"}, {"input": "1\r\n77 88\r\n", "output": "11\r\n"}, {"input": "1\r\n1 33\r\n", "output": "32\r\n"}, {"input": "2\r\n72 22 81 23 14 75\r\n", "output": "175\r\n"}, {"input": "2\r\n100 70 27 1 68 52\r\n", "output": "53\r\n"}, {"input": "2\r\n24 19 89 82 22 21\r\n", "output": "80\r\n"}, {"input": "3\r\n86 12 92 91 3 68 57 56 76 27 33 62 71 84\r\n", "output": "286\r\n"}, {"input": "3\r\n14 56 53 61 57 45 40 44 31 9 73 2 61 26\r\n", "output": "236\r\n"}, {"input": "3\r\n35 96 7 43 10 14 16 36 95 92 16 50 59 55\r\n", "output": "173\r\n"}, {"input": "4\r\n1 97 18 48 96 65 24 91 17 45 36 27 74 93 78 86 39 55 53 21 26 68 31 33 79 63 80 92 1 26\r\n", "output": "511\r\n"}, {"input": "4\r\n25 42 71 29 50 30 99 79 77 24 76 66 68 23 97 99 65 17 75 62 66 46 48 4 40 71 98 57 21 92\r\n", "output": "603\r\n"}, {"input": "4\r\n49 86 17 7 3 6 86 71 36 10 27 10 58 64 12 16 88 67 93 3 15 20 58 87 97 91 11 6 34 62\r\n", "output": "470\r\n"}, {"input": "5\r\n16 87 36 16 81 53 87 35 63 56 47 91 81 95 80 96 91 7 58 99 25 28 47 60 7 69 49 14 51 52 29 30 83 23 21 52 100 26 91 14 23 94 72 70 40 12 50 32 54 52 18 74 5 15 62 3 48 41 24 25 56 43\r\n", "output": "1060\r\n"}, {"input": "5\r\n40 27 82 94 38 22 66 23 18 34 87 31 71 28 95 5 14 61 76 52 66 6 60 40 68 77 70 63 64 18 47 13 82 55 34 64 30 1 29 24 24 9 65 17 29 96 61 76 72 23 32 26 90 39 54 41 35 66 71 29 75 48\r\n", "output": "1063\r\n"}, {"input": "5\r\n64 72 35 68 92 95 45 15 77 16 26 74 61 65 18 22 32 19 98 97 14 84 70 23 29 1 87 28 88 89 73 79 69 88 43 60 64 64 66 39 17 27 46 71 18 83 73 20 90 77 49 70 84 63 50 72 26 87 26 37 78 65\r\n", "output": "987\r\n"}, {"input": "6\r\n35 61 54 77 70 50 53 70 4 66 58 47 76 100 78 5 43 50 55 93 13 93 59 92 30 74 22 23 98 70 19 56 90 92 19 7 28 53 45 77 42 91 71 56 19 83 100 53 13 93 37 13 70 60 16 13 76 3 12 22 17 26 50 6 63 7 25 41 92 29 36 80 11 4 10 14 77 75 53 82 46 24 56 46 82 36 80 75 8 45 24 22 90 34 45 76 18 38 86 43 7 49 80 56 90 53 12 51 98 47 44 58 32 4 2 6 3 60 38 72 74 46 30 86 1 98\r\n", "output": "2499\r\n"}, {"input": "6\r\n63 13 100 54 31 15 29 58 59 44 2 99 70 33 97 14 70 12 73 42 65 71 68 67 87 83 43 84 18 41 37 22 81 24 27 11 57 28 83 92 39 1 56 15 16 67 16 97 31 52 50 65 63 89 8 52 55 20 71 27 28 35 86 92 94 60 10 65 83 63 89 71 34 20 78 40 34 62 2 86 100 81 87 69 25 4 52 17 57 71 62 38 1 3 54 71 34 85 20 60 80 23 82 47 4 19 7 18 14 18 28 27 4 55 26 71 45 9 2 40 67 28 32 19 81 92\r\n", "output": "2465\r\n"}, {"input": "6\r\n87 62 58 32 81 92 12 50 23 27 38 39 64 74 16 35 84 59 91 87 14 48 90 47 44 95 64 45 31 11 67 5 80 60 36 15 91 3 21 2 40 24 37 69 5 50 23 37 49 19 68 21 49 9 100 94 45 41 22 31 31 48 25 70 25 25 95 88 82 1 37 53 49 31 57 74 94 45 55 93 43 37 13 85 59 72 15 68 3 90 96 55 100 64 63 69 43 33 66 84 57 97 87 34 23 89 97 77 39 89 8 92 68 13 50 36 95 61 71 96 73 13 30 49 57 89\r\n", "output": "2513\r\n"}]
false
stdio
null
true
324/A1
331
A2
PyPy 3-64
TESTS2
7
154
0
207278093
import sys input = lambda: sys.stdin.readline().rstrip() from collections import defaultdict N = int(input()) A = list(map(int, input().split())) ans = 0 lib = {} cur = 0 d = 0 for i,a in enumerate(A): if a>0: cur+=a if a not in lib.keys(): lib[a]=cur else: if ans<cur-lib[a]+a: ans = cur-lib[a]+a d = a ret = [] for i in range(N): if A[i]<0: ret.append(i+1) for i in range(N): if A[i]==d:break if A[i]>0: ret.append(i+1) for i in range(N-1,-1,-1): if A[i]==d:break if A[i]>0: ret.append(i+1) ret.sort() print(ans,len(ret)) print(*ret)
18
186
0
168028719
import sys input = sys.stdin.readline n = int(input()) w = list(map(int, input().split())) d = dict() p = [] c = 0 for i in range(n): if w[i] not in d: d[w[i]] = [1, i, i] else: if d[w[i]][0] != 2: d[w[i]][0] = 2 d[w[i]][2] = i if w[i] > 0: c += w[i] p.append(c) x = -100000000000 b = (0, 0) for i in d: if d[i][0] == 2: x1 = p[d[i][2]] - (p[d[i][1]-1] if d[i][1] != 0 else 0) if i < 0: x1 += i+i if x1 > x: x = x1 b = (d[i][1], d[i][2]) e = [] e.extend(list(range(1, b[0]+1))) for i in range(b[0]+1, b[1]): if w[i] < 0: e.append(i+1) e.extend(list(range(b[1] + 2, n + 1))) print(x, len(e)) print(' '.join(map(str, e)))
ABBYY Cup 3.0 - Finals
ICPC
2,013
2
256
Oh Sweet Beaverette
— Oh my sweet Beaverette, would you fancy a walk along a wonderful woodland belt with me? — Of course, my Smart Beaver! Let us enjoy the splendid view together. How about Friday night? At this point the Smart Beaver got rushing. Everything should be perfect by Friday, so he needed to prepare the belt to the upcoming walk. He needed to cut down several trees. Let's consider the woodland belt as a sequence of trees. Each tree i is described by the esthetic appeal ai — some trees are very esthetically pleasing, others are 'so-so', and some trees are positively ugly! The Smart Beaver calculated that he needed the following effects to win the Beaverette's heart: - The first objective is to please the Beaverette: the sum of esthetic appeal of the remaining trees must be maximum possible; - the second objective is to surprise the Beaverette: the esthetic appeal of the first and the last trees in the resulting belt must be the same; - and of course, the walk should be successful: there must be at least two trees in the woodland belt left. Now help the Smart Beaver! Which trees does he need to cut down to win the Beaverette's heart?
The first line contains a single integer n — the initial number of trees in the woodland belt, 2 ≤ n. The second line contains space-separated integers ai — the esthetic appeals of each tree. All esthetic appeals do not exceed 109 in their absolute value. - to get 30 points, you need to solve the problem with constraints: n ≤ 100 (subproblem A1); - to get 100 points, you need to solve the problem with constraints: n ≤ 3·105 (subproblems A1+A2).
In the first line print two integers — the total esthetic appeal of the woodland belt after the Smart Beaver's intervention and the number of the cut down trees k. In the next line print k integers — the numbers of the trees the Beaver needs to cut down. Assume that the trees are numbered from 1 to n from left to right. If there are multiple solutions, print any of them. It is guaranteed that at least two trees have equal esthetic appeal.
null
null
[{"input": "5\n1 2 3 1 2", "output": "8 1\n1"}, {"input": "5\n1 -2 3 1 -2", "output": "5 2\n2 5"}]
1,400
[]
18
[{"input": "5\r\n1 2 3 1 2\r\n", "output": "8 1\r\n1 "}, {"input": "5\r\n1 -2 3 1 -2\r\n", "output": "5 2\r\n2 5 "}, {"input": "2\r\n0 0\r\n", "output": "0 0\r\n"}, {"input": "3\r\n0 -1 0\r\n", "output": "0 1\r\n2 "}, {"input": "3\r\n1 1 1\r\n", "output": "3 0\r\n"}, {"input": "4\r\n-1 1 1 -1\r\n", "output": "2 2\r\n1 4 "}, {"input": "4\r\n-1 1 -1 1\r\n", "output": "2 2\r\n1 3 "}, {"input": "2\r\n-1 -1\r\n", "output": "-2 0\r\n"}, {"input": "3\r\n-1 0 -1\r\n", "output": "-2 0\r\n"}, {"input": "6\r\n-1 3 3 5 5 -1\r\n", "output": "14 0\r\n"}, {"input": "2\r\n-1000000000 -1000000000\r\n", "output": "-2000000000 0\r\n"}, {"input": "3\r\n-1000000000 -1000000000 -1000000000\r\n", "output": "-2000000000 1\r\n3 "}, {"input": "3\r\n1000000000 1000000000 1000000000\r\n", "output": "3000000000 0\r\n"}, {"input": "10\r\n-589330597 -126288833 -126288833 -834860352 -834860352 -834860352 -834860352 -21170405 -834860352 -834860352\r\n", "output": "-252577666 8\r\n1 4 5 6 7 8 9 10 "}, {"input": "20\r\n-808998072 733614990 579897311 -337992089 579897311 120800519 -337992089 -803027570 733614990 -686536765 733614990 -803027570 -803027570 733614990 120800519 -803027570 -686536765 579897311 -808998072 -686536765\r\n", "output": "4215055101 13\r\n1 4 7 8 10 12 13 15 16 17 18 19 20 "}]
false
stdio
import sys from collections import defaultdict def main(input_path, output_path, submission_output_path): with open(input_path) as f: n = int(f.readline()) a = list(map(int, f.readline().split())) with open(submission_output_path) as f: lines = f.readlines() if not lines: print(0) return sum_line = lines[0].strip().split() if len(sum_line) != 2: print(0) return try: sum_claimed, k = map(int, sum_line) except: print(0) return cut_list = list(map(int, lines[1].split())) if len(lines) > 1 else [] if len(cut_list) != k: print(0) return cut_set = set(cut_list) if len(cut_set) != k: print(0) return for num in cut_list: if not (1 <= num <= n): print(0) return remaining = [i-1 for i in range(1, n+1) if i not in cut_set] if len(remaining) < 2: print(0) return first_val = a[remaining[0]] last_val = a[remaining[-1]] if first_val != last_val: print(0) return sum_remaining = sum(a[i] for i in remaining) if sum_remaining != sum_claimed: print(0) return prefix = [0] * (n + 1) for i in range(n): prefix[i+1] = prefix[i] + max(a[i], 0) groups = defaultdict(list) for idx, val in enumerate(a): groups[val].append(idx) max_total = -float('inf') for v, indices in groups.items(): m = len(indices) if m < 2: continue indices.sort() min_val = prefix[indices[0] + 1] current_max = -float('inf') for j in range(1, m): current_i = indices[j] current_sum = 2 * v + (prefix[current_i] - min_val) if current_sum > current_max: current_max = current_sum if j < m - 1: next_val = prefix[indices[j] + 1] if next_val < min_val: min_val = next_val if current_max > max_total: max_total = current_max if sum_claimed != max_total: print(0) else: print(100) if __name__ == "__main__": input_path = sys.argv[1] output_path = sys.argv[2] submission_output_path = sys.argv[3] main(input_path, output_path, submission_output_path)
true
324/A1
331
A1
PyPy 3
TESTS1
7
154
0
222149040
n = int(input()) a = list(map(int, input().split())) best_sum = -float('inf') best_pair = (0, 0) last_occ = {} prefix_sum = [0] for i in range(n): prefix_sum.append(prefix_sum[-1] + max(0, a[i])) for i in reversed(range(n)): if a[i] in last_occ: total_sum = prefix_sum[last_occ[a[i]] + 1] - prefix_sum[i] + 2 * a[i] if total_sum > best_sum: best_sum = total_sum best_pair = (i, last_occ[a[i]]) else: last_occ[a[i]] = i to_cut = [] for i in range(best_pair[0] + 1, best_pair[1]): if a[i] < 0: to_cut.append(i + 1) for i in range(0, best_pair[0]): to_cut.append(i + 1) for i in range(best_pair[1] + 1, n): to_cut.append(i + 1) print(f"{best_sum - 2 * a[best_pair[0]]} {len(to_cut)}") print(" ".join(map(str, to_cut)))
18
186
307,200
117799908
import math import sys from collections import deque, Counter, OrderedDict, defaultdict #import heapq #ceil,floor,log,sqrt,factorial,pow,pi,gcd #import bisect #from bisect import bisect_left,bisect_right input = sys.stdin.readline def inp(): return(int(input())) def inlt(): return(list(map(int,input().split()))) def insr(): s = input().strip() return(list(s[:len(s)])) def invr(): return(map(int,input().split())) n=inp() l=inlt() g=Counter(l) fst=defaultdict(lambda:-1) lst=defaultdict(lambda:-1) sm=[max(0,l[0])] for i in range(1,n): sm.append(sm[-1]+max(0,l[i])) for i in range(n): if fst[l[i]]==-1: fst[l[i]]=i for i in range(n-1,-1,-1): if lst[l[i]]==-1: lst[l[i]]=i mx=-sys.maxsize v=-1 for each in g: if g[each]>=2: if each<0: val=2*each-sm[fst[each]]+sm[lst[each]] else: val=sm[lst[each]]-sm[fst[each]]+each if val>mx: mx=val v=each cnt=0 rem=[] for i in range(n): if i<fst[v]: rem.append(i+1) elif i>lst[v]: rem.append(i+1) elif l[i]<0 and i!=fst[v] and i!=lst[v]: rem.append(i+1) print(mx,len(rem)) print(*rem)
ABBYY Cup 3.0 - Finals
ICPC
2,013
2
256
Oh Sweet Beaverette
— Oh my sweet Beaverette, would you fancy a walk along a wonderful woodland belt with me? — Of course, my Smart Beaver! Let us enjoy the splendid view together. How about Friday night? At this point the Smart Beaver got rushing. Everything should be perfect by Friday, so he needed to prepare the belt to the upcoming walk. He needed to cut down several trees. Let's consider the woodland belt as a sequence of trees. Each tree i is described by the esthetic appeal ai — some trees are very esthetically pleasing, others are 'so-so', and some trees are positively ugly! The Smart Beaver calculated that he needed the following effects to win the Beaverette's heart: - The first objective is to please the Beaverette: the sum of esthetic appeal of the remaining trees must be maximum possible; - the second objective is to surprise the Beaverette: the esthetic appeal of the first and the last trees in the resulting belt must be the same; - and of course, the walk should be successful: there must be at least two trees in the woodland belt left. Now help the Smart Beaver! Which trees does he need to cut down to win the Beaverette's heart?
The first line contains a single integer n — the initial number of trees in the woodland belt, 2 ≤ n. The second line contains space-separated integers ai — the esthetic appeals of each tree. All esthetic appeals do not exceed 109 in their absolute value. - to get 30 points, you need to solve the problem with constraints: n ≤ 100 (subproblem A1); - to get 100 points, you need to solve the problem with constraints: n ≤ 3·105 (subproblems A1+A2).
In the first line print two integers — the total esthetic appeal of the woodland belt after the Smart Beaver's intervention and the number of the cut down trees k. In the next line print k integers — the numbers of the trees the Beaver needs to cut down. Assume that the trees are numbered from 1 to n from left to right. If there are multiple solutions, print any of them. It is guaranteed that at least two trees have equal esthetic appeal.
null
null
[{"input": "5\n1 2 3 1 2", "output": "8 1\n1"}, {"input": "5\n1 -2 3 1 -2", "output": "5 2\n2 5"}]
1,400
[]
18
[{"input": "5\r\n1 2 3 1 2\r\n", "output": "8 1\r\n1 "}, {"input": "5\r\n1 -2 3 1 -2\r\n", "output": "5 2\r\n2 5 "}, {"input": "2\r\n0 0\r\n", "output": "0 0\r\n"}, {"input": "3\r\n0 -1 0\r\n", "output": "0 1\r\n2 "}, {"input": "3\r\n1 1 1\r\n", "output": "3 0\r\n"}, {"input": "4\r\n-1 1 1 -1\r\n", "output": "2 2\r\n1 4 "}, {"input": "4\r\n-1 1 -1 1\r\n", "output": "2 2\r\n1 3 "}, {"input": "2\r\n-1 -1\r\n", "output": "-2 0\r\n"}, {"input": "3\r\n-1 0 -1\r\n", "output": "-2 0\r\n"}, {"input": "6\r\n-1 3 3 5 5 -1\r\n", "output": "14 0\r\n"}, {"input": "2\r\n-1000000000 -1000000000\r\n", "output": "-2000000000 0\r\n"}, {"input": "3\r\n-1000000000 -1000000000 -1000000000\r\n", "output": "-2000000000 1\r\n3 "}, {"input": "3\r\n1000000000 1000000000 1000000000\r\n", "output": "3000000000 0\r\n"}, {"input": "10\r\n-589330597 -126288833 -126288833 -834860352 -834860352 -834860352 -834860352 -21170405 -834860352 -834860352\r\n", "output": "-252577666 8\r\n1 4 5 6 7 8 9 10 "}, {"input": "20\r\n-808998072 733614990 579897311 -337992089 579897311 120800519 -337992089 -803027570 733614990 -686536765 733614990 -803027570 -803027570 733614990 120800519 -803027570 -686536765 579897311 -808998072 -686536765\r\n", "output": "4215055101 13\r\n1 4 7 8 10 12 13 15 16 17 18 19 20 "}]
false
stdio
import sys from collections import defaultdict def main(input_path, output_path, submission_output_path): with open(input_path) as f: n = int(f.readline()) a = list(map(int, f.readline().split())) with open(submission_output_path) as f: lines = f.readlines() if not lines: print(0) return sum_line = lines[0].strip().split() if len(sum_line) != 2: print(0) return try: sum_claimed, k = map(int, sum_line) except: print(0) return cut_list = list(map(int, lines[1].split())) if len(lines) > 1 else [] if len(cut_list) != k: print(0) return cut_set = set(cut_list) if len(cut_set) != k: print(0) return for num in cut_list: if not (1 <= num <= n): print(0) return remaining = [i-1 for i in range(1, n+1) if i not in cut_set] if len(remaining) < 2: print(0) return first_val = a[remaining[0]] last_val = a[remaining[-1]] if first_val != last_val: print(0) return sum_remaining = sum(a[i] for i in remaining) if sum_remaining != sum_claimed: print(0) return prefix = [0] * (n + 1) for i in range(n): prefix[i+1] = prefix[i] + max(a[i], 0) groups = defaultdict(list) for idx, val in enumerate(a): groups[val].append(idx) max_total = -float('inf') for v, indices in groups.items(): m = len(indices) if m < 2: continue indices.sort() min_val = prefix[indices[0] + 1] current_max = -float('inf') for j in range(1, m): current_i = indices[j] current_sum = 2 * v + (prefix[current_i] - min_val) if current_sum > current_max: current_max = current_sum if j < m - 1: next_val = prefix[indices[j] + 1] if next_val < min_val: min_val = next_val if current_max > max_total: max_total = current_max if sum_claimed != max_total: print(0) else: print(100) if __name__ == "__main__": input_path = sys.argv[1] output_path = sys.argv[2] submission_output_path = sys.argv[3] main(input_path, output_path, submission_output_path)
true
609/D
609
D
PyPy 3
TESTS
4
187
0
93589343
import os, sys from io import IOBase, BytesIO py2 = round(0.5) if py2: from future_builtins import ascii, filter, hex, map, oct, zip range = xrange BUFSIZE = 8192 class FastIO(BytesIO): newlines = 0 def __init__(self, file): self._file = file self._fd = file.fileno() self.writable = 'x' in file.mode or 'w' in file.mode self.write = super(FastIO, self).write if self.writable else None def _fill(self): s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0]) return s def read(self): while self._fill(): pass return super(FastIO,self).read() def readline(self): while self.newlines == 0: s = self._fill(); self.newlines = s.count(b'\n') + (not s) self.newlines -= 1 return super(FastIO, self).readline() def flush(self): if self.writable: os.write(self._fd, self.getvalue()) self.truncate(0), self.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable if py2: self.write = self.buffer.write self.read = self.buffer.read self.readline = self.buffer.readline else: 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') # Cout implemented in Python import sys class ostream: def __lshift__(self,a): sys.stdout.write(str(a)) return self cout = ostream() endl = '\n' import heapq def can_do(check_days, n, m, k, s, d_gadgets, p_gadgets, prefx_min_d_val, prefx_min_p_val): # get low in the range 1 -> check_days min_d = prefx_min_d_val[check_days][0] min_p = prefx_min_p_val[check_days][0] min_d_idx = prefx_min_d_val[check_days][1] min_p_idx = prefx_min_p_val[check_days][1] vals_d = [0] * (k + 1) vals_p = [0] * (k + 1) for i in range(1, k + 1): # print(i) if i < len(d_gadgets): vals_d[i] = ((min_d * d_gadgets[i - 1][0]) + vals_d[i - 1]) if i < len(p_gadgets): vals_p[i] = ((min_p * p_gadgets[i - 1][0]) + vals_p[i - 1]) for x in range(k + 1): # x from dollar and k - x from pounds if x <= len(d_gadgets) and k - x <= len(p_gadgets): if ((x > 0 and vals_d[x] > 0) or x == 0) \ and ((k - x > 0 and vals_p[k - x] > 0) or k - x == 0): total = vals_d[x] + vals_p[k - x] if total <= s: res = [] for i in range(x): res.append((d_gadgets[i][1], min_d_idx)) for i in range(k - x): res.append((p_gadgets[i][1], min_p_idx)) return (True, res) return (False, []) def check(n, m, k, s, d_gadgets, p_gadgets, prefx_min_d_val, prefx_min_p_val): lo = 0 hi = n res = -1 while lo <= hi: mid = lo + (hi - lo) // 2 if can_do(mid, n, m, k, s, d_gadgets, p_gadgets, prefx_min_d_val, prefx_min_p_val)[0]: res = mid hi = mid - 1 else: lo = mid + 1 if res == -1: return res, [] return res, can_do(res, n, m, k, s, d_gadgets, p_gadgets, prefx_min_d_val, prefx_min_p_val)[1] def solve(): n, m, k, s = map(int, input().split()) d_vals = list(map(int, input().split())) d_vals.insert(0, float("inf")) p_vals = list(map(int, input().split())) p_vals.insert(0, float("inf")) d_gadgets = [] p_gadgets = [] for i in range(m): t, c = map(int, input().split()) if t == 1: d_gadgets.append((c, i + 1)) else: p_gadgets.append((c, i + 1)) prefx_min_d_val = [(d_vals[0], 0)] * len(d_vals) for i in range(1, len(d_vals)): if prefx_min_d_val[i - 1][0] < d_vals[i]: prefx_min_d_val[i] = (prefx_min_d_val[i - 1][0], prefx_min_d_val[i - 1][1]) else: prefx_min_d_val[i] = (d_vals[i], i) prefx_min_p_val = [(p_vals[0], 0)] * len(p_vals) for i in range(1, len(p_vals)): if prefx_min_p_val[i - 1][0] < p_vals[i]: prefx_min_p_val[i] = (prefx_min_p_val[i - 1][0], prefx_min_p_val[i - 1][1]) else: prefx_min_p_val[i] = (p_vals[i], i) d_gadgets.sort(key=lambda x: x[0]) p_gadgets.sort(key=lambda x: x[0]) res, r = check(n, m, k, s, d_gadgets, p_gadgets, prefx_min_d_val, prefx_min_p_val) cout<<res<<endl for p in r: cout<<p[0]<<" "<<p[1]<<endl def main(): solve() if __name__ == "__main__": main()
51
1,809
43,110,400
117047744
''' Auther: ghoshashis545 Ashis Ghosh College: Jalpaiguri Govt Enggineering College ''' from os import path from io import BytesIO, IOBase import sys from heapq import heappush,heappop from functools import cmp_to_key as ctk from collections import deque,Counter,defaultdict as dd from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right from itertools import permutations from datetime import datetime from math import ceil,sqrt,log,gcd def ii():return int(input()) def si():return input().rstrip() def mi():return map(int,input().split()) def li():return list(mi()) abc='abcdefghijklmnopqrstuvwxyz' mod=1000000007 #mod=998244353 inf = float("inf") vow=['a','e','i','o','u'] dx,dy=[-1,1,0,0],[0,0,1,-1] def bo(i): return ord(i)-ord('0') file = 1 def ceil(a,b): return (a+b-1)//b def solve(): # for _ in range(1,ii()+1): n,m,k,s = mi() a = li() b = li() gadgets = [[] for i in range(2)] for i in range(m): x,y = mi() gadgets[x-1].append([y,i+1]) gadgets[0].sort() gadgets[1].sort() sz = [len(gadgets[0]),len(gadgets[1])] def check(idx): mnx1 = inf mnx2 = inf for i in range(idx+1): mnx1 = min(mnx1,a[i]) mnx2 = min(mnx2,b[i]) l1,l2,res = 0,0,0 if k > sz[0] + sz[1]: return 0 for i in range(k): if l1 == sz[0]: res += gadgets[1][l2][0]*mnx2 l2 += 1 elif l2 == sz[1]: res += gadgets[0][l1][0]*mnx1 l1 += 1 elif gadgets[0][l1][0]*mnx1 <= gadgets[1][l2][0]*mnx2: res += gadgets[0][l1][0]*mnx1 l1 += 1 else: res += gadgets[1][l2][0]*mnx2 l2 += 1 return res <= s l = 0 r = n-1 ans = -1 while l<=r: mid = (l+r)>>1 if check(mid): ans = mid+1 r = mid-1 else: l=mid+1 print(ans) if ans == -1: return mnx1 = inf mnx2 = inf idx1,idx2 = -1,-1 for i in range(ans): if a[i] < mnx1: mnx1 = a[i] idx1 = i if b[i] < mnx2: mnx2 = b[i] idx2 = i l1,l2,res = 0,0,[] for i in range(k): if l1 == sz[0]: res.append([gadgets[1][l2][1],idx2+1]) l2 += 1 elif l2==sz[1]: res.append([gadgets[0][l1][1],idx1+1]) l1 += 1 elif gadgets[0][l1][0]*mnx1 <= gadgets[1][l2][0]*mnx2: res.append([gadgets[0][l1][1],idx1+1]) l1 += 1 else: res.append([gadgets[1][l2][1],idx2+1]) l2 += 1 for i in res: print(*i) if __name__ =="__main__": if(file): if path.exists('input.txt'): sys.stdin=open('input.txt', 'r') sys.stdout=open('output.txt','w') else: input=sys.stdin.readline solve()
Educational Codeforces Round 3
ICPC
2,015
2
256
Gadgets for dollars and pounds
Nura wants to buy k gadgets. She has only s burles for that. She can buy each gadget for dollars or for pounds. So each gadget is selling only for some type of currency. The type of currency and the cost in that currency are not changing. Nura can buy gadgets for n days. For each day you know the exchange rates of dollar and pound, so you know the cost of conversion burles to dollars or to pounds. Each day (from 1 to n) Nura can buy some gadgets by current exchange rate. Each day she can buy any gadgets she wants, but each gadget can be bought no more than once during n days. Help Nura to find the minimum day index when she will have k gadgets. Nura always pays with burles, which are converted according to the exchange rate of the purchase day. Nura can't buy dollars or pounds, she always stores only burles. Gadgets are numbered with integers from 1 to m in order of their appearing in input.
First line contains four integers n, m, k, s (1 ≤ n ≤ 2·105, 1 ≤ k ≤ m ≤ 2·105, 1 ≤ s ≤ 109) — number of days, total number and required number of gadgets, number of burles Nura has. Second line contains n integers ai (1 ≤ ai ≤ 106) — the cost of one dollar in burles on i-th day. Third line contains n integers bi (1 ≤ bi ≤ 106) — the cost of one pound in burles on i-th day. Each of the next m lines contains two integers ti, ci (1 ≤ ti ≤ 2, 1 ≤ ci ≤ 106) — type of the gadget and it's cost. For the gadgets of the first type cost is specified in dollars. For the gadgets of the second type cost is specified in pounds.
If Nura can't buy k gadgets print the only line with the number -1. Otherwise the first line should contain integer d — the minimum day index, when Nura will have k gadgets. On each of the next k lines print two integers qi, di — the number of gadget and the day gadget should be bought. All values qi should be different, but the values di can coincide (so Nura can buy several gadgets at one day). The days are numbered from 1 to n. In case there are multiple possible solutions, print any of them.
null
null
[{"input": "5 4 2 2\n1 2 3 2 1\n3 2 1 2 3\n1 1\n2 1\n1 2\n2 2", "output": "3\n1 1\n2 3"}, {"input": "4 3 2 200\n69 70 71 72\n104 105 106 107\n1 1\n2 2\n1 2", "output": "-1"}, {"input": "4 3 1 1000000000\n900000 910000 940000 990000\n990000 999000 999900 999990\n1 87654\n2 76543\n1 65432", "output": "-1"}]
2,000
["binary search", "greedy", "two pointers"]
51
[{"input": "5 4 2 2\r\n1 2 3 2 1\r\n3 2 1 2 3\r\n1 1\r\n2 1\r\n1 2\r\n2 2\r\n", "output": "3\r\n1 1\r\n2 3\r\n"}, {"input": "4 3 2 200\r\n69 70 71 72\r\n104 105 106 107\r\n1 1\r\n2 2\r\n1 2\r\n", "output": "-1\r\n"}, {"input": "4 3 1 1000000000\r\n900000 910000 940000 990000\r\n990000 999000 999900 999990\r\n1 87654\r\n2 76543\r\n1 65432\r\n", "output": "-1\r\n"}, {"input": "5 5 3 1000000\r\n921 853 547 187 164\r\n711 462 437 307 246\r\n2 94\r\n2 230\r\n1 373\r\n1 476\r\n2 880\r\n", "output": "1\r\n1 1\r\n2 1\r\n5 1\r\n"}, {"input": "10 10 10 1000000\r\n836 842 645 671 499 554 462 288 89 104\r\n880 722 623 651 591 573 154 532 136 59\r\n1 47\r\n1 169\r\n2 486\r\n1 262\r\n2 752\r\n2 498\r\n2 863\r\n2 616\r\n1 791\r\n1 656\r\n", "output": "9\r\n1 9\r\n2 9\r\n4 9\r\n10 9\r\n9 9\r\n3 9\r\n6 9\r\n8 9\r\n5 9\r\n7 9\r\n"}, {"input": "1 2 2 1000000\r\n96\r\n262\r\n1 699\r\n2 699\r\n", "output": "1\r\n1 1\r\n2 1\r\n"}, {"input": "1 2 2 1000000\r\n793\r\n33\r\n1 733\r\n2 406\r\n", "output": "1\r\n1 1\r\n2 1\r\n"}, {"input": "1 2 2 10000\r\n82\r\n996\r\n2 574\r\n2 217\r\n", "output": "-1\r\n"}, {"input": "1 2 2 1000000\r\n778\r\n62\r\n2 119\r\n2 220\r\n", "output": "1\r\n1 1\r\n2 1\r\n"}, {"input": "1 2 2 1000000\r\n963\r\n25\r\n2 961\r\n1 327\r\n", "output": "1\r\n2 1\r\n1 1\r\n"}, {"input": "10 20 20 1000000\r\n809 909 795 661 635 613 534 199 188 3\r\n475 585 428 379 185 177 66 104 15 38\r\n2 454\r\n1 863\r\n2 14\r\n2 104\r\n1 663\r\n2 885\r\n1 650\r\n1 967\r\n2 650\r\n2 483\r\n2 846\r\n1 283\r\n1 187\r\n2 533\r\n2 112\r\n2 938\r\n2 553\r\n1 816\r\n1 549\r\n2 657\r\n", "output": "10\r\n13 10\r\n12 10\r\n19 10\r\n7 10\r\n5 10\r\n18 10\r\n2 10\r\n8 10\r\n3 9\r\n4 9\r\n15 9\r\n1 9\r\n10 9\r\n14 9\r\n17 9\r\n9 9\r\n20 9\r\n11 9\r\n6 9\r\n16 9\r\n"}, {"input": "10 20 19 1000000\r\n650 996 972 951 904 742 638 93 339 151\r\n318 565 849 579 521 965 286 189 196 307\r\n2 439\r\n1 333\r\n2 565\r\n1 602\r\n2 545\r\n2 596\r\n2 821\r\n2 929\r\n1 614\r\n2 647\r\n2 909\r\n1 8\r\n2 135\r\n1 301\r\n1 597\r\n1 632\r\n1 437\r\n2 448\r\n2 631\r\n2 969\r\n", "output": "-1\r\n"}, {"input": "10 20 18 10000\r\n916 582 790 449 578 502 411 196 218 144\r\n923 696 788 609 455 570 330 435 284 113\r\n2 736\r\n1 428\r\n1 861\r\n2 407\r\n2 320\r\n1 340\r\n1 88\r\n1 172\r\n1 788\r\n2 633\r\n2 612\r\n2 571\r\n2 536\r\n2 30\r\n2 758\r\n2 90\r\n2 8\r\n1 970\r\n1 20\r\n1 22\r\n", "output": "-1\r\n"}, {"input": "10 20 16 1000000\r\n317 880 696 304 260 180 214 245 79 37\r\n866 621 940 89 718 674 195 267 12 49\r\n2 825\r\n2 197\r\n1 657\r\n1 231\r\n1 728\r\n2 771\r\n2 330\r\n2 943\r\n1 60\r\n1 89\r\n2 721\r\n2 959\r\n1 926\r\n2 215\r\n1 583\r\n2 680\r\n1 799\r\n2 887\r\n1 709\r\n1 316\r\n", "output": "6\r\n9 6\r\n10 6\r\n4 6\r\n20 6\r\n15 6\r\n3 6\r\n2 4\r\n14 4\r\n7 4\r\n16 4\r\n11 4\r\n6 4\r\n1 4\r\n18 4\r\n8 4\r\n12 4\r\n"}, {"input": "10 20 20 10000\r\n913 860 844 775 297 263 247 71 50 6\r\n971 938 890 854 643 633 427 418 190 183\r\n1 556\r\n2 579\r\n1 315\r\n2 446\r\n1 327\r\n1 724\r\n2 12\r\n1 142\r\n1 627\r\n1 262\r\n1 681\r\n1 802\r\n1 886\r\n1 350\r\n2 383\r\n1 191\r\n1 717\r\n1 968\r\n2 588\r\n1 57\r\n", "output": "-1\r\n"}, {"input": "1 93 46 46\r\n1\r\n1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n2 1\r\n1 2\r\n", "output": "1\r\n2 1\r\n4 1\r\n6 1\r\n8 1\r\n10 1\r\n12 1\r\n14 1\r\n16 1\r\n18 1\r\n20 1\r\n22 1\r\n24 1\r\n26 1\r\n28 1\r\n30 1\r\n32 1\r\n34 1\r\n36 1\r\n38 1\r\n40 1\r\n42 1\r\n44 1\r\n46 1\r\n48 1\r\n50 1\r\n52 1\r\n54 1\r\n56 1\r\n58 1\r\n60 1\r\n62 1\r\n64 1\r\n66 1\r\n68 1\r\n70 1\r\n72 1\r\n74 1\r\n76 1\r\n78 1\r\n80 1\r\n82 1\r\n84 1\r\n86 1\r\n88 1\r\n90 1\r\n92 1\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] # Read input data with open(input_path) as f: input_lines = f.read().splitlines() ptr = 0 n, m, k, s = map(int, input_lines[ptr].split()) ptr += 1 a = list(map(int, input_lines[ptr].split())) ptr += 1 b = list(map(int, input_lines[ptr].split())) ptr += 1 gadgets = [] for _ in range(m): ti, ci = map(int, input_lines[ptr].split()) gadgets.append((ti, ci)) ptr += 1 # Read reference output with open(output_path) as f: ref_lines = f.read().splitlines() # Read submission output with open(submission_path) as f: sub_lines = [line.strip() for line in f.read().splitlines()] # Check for -1 case if ref_lines[0].strip() == '-1': if len(sub_lines) == 1 and sub_lines[0] == '-1': print(1) else: print(0) return # Check submission has correct d if not sub_lines: print(0) return try: d_ref = int(ref_lines[0]) d_sub = int(sub_lines[0]) except: print(0) return if d_sub != d_ref: print(0) return # Validate submission's gadgets if len(sub_lines) != k + 1: print(0) return seen = set() total = 0 for line in sub_lines[1:]: if not line: print(0) return parts = line.split() if len(parts) != 2: print(0) return try: qi = int(parts[0]) di = int(parts[1]) except: print(0) return if qi < 1 or qi > m or di < 1 or di > d_ref: print(0) return if qi in seen: print(0) return seen.add(qi) ti, ci = gadgets[qi - 1] if ti == 1: rate = a[di - 1] else: rate = b[di - 1] total += ci * rate if total > s: print(0) return print(1) if __name__ == "__main__": main()
true
18/D
18
D
PyPy 3
TESTS
32
964
5,529,600
91087080
n = int(input()) a = [] for i in range(n): a.append(input().split(' ')) valid = [1] * n res = 0 for x in range(2000, 0, -1): pos_sell = -1 pos_win = -1 for i in range(n): if valid[i] == 0: continue if a[i][0] == 'sell' and int(a[i][1]) == x: pos_sell = i break for i in range(n): if valid[i] == 0: continue if i > pos_sell: break if a[i][0] == 'win' and int(a[i][1]) == x: pos_win = i if pos_sell == -1 or pos_win == -1: continue fun = 1 for i in range(pos_win, pos_sell+1): if valid[i] == 0: fun = 0 break if fun == 0: continue for i in range(pos_win, pos_sell+1): valid[i] = 0 res += pow(2, x) print(res)
45
92
614,400
229440552
n = int(input()) # Number of working days total_earnings = 0 # Initialize total earnings memory_stick_capacities = [0] * 2010 # Create a list to store memory stick capacities and their respective earnings for i in range(n): option = input().split() # Read the input option (win or sell) and capacity action, capacity = option[0], int(option[1]) if action == "win": memory_stick_capacities[capacity] = total_earnings + 2 ** capacity # If Bob wins a stick, update earnings else: total_earnings = max(total_earnings, memory_stick_capacities[capacity]) # If Bob sells a stick, update total earnings print(total_earnings) # Output the maximum possible earnings for Bob
Codeforces Beta Round 18 (Div. 2 Only)
ICPC
2,010
2
128
Seller Bob
Last year Bob earned by selling memory sticks. During each of n days of his work one of the two following events took place: - A customer came to Bob and asked to sell him a 2x MB memory stick. If Bob had such a stick, he sold it and got 2x berllars. - Bob won some programming competition and got a 2x MB memory stick as a prize. Bob could choose whether to present this memory stick to one of his friends, or keep it. Bob never kept more than one memory stick, as he feared to mix up their capacities, and deceive a customer unintentionally. It is also known that for each memory stick capacity there was at most one customer, who wanted to buy that memory stick. Now, knowing all the customers' demands and all the prizes won at programming competitions during the last n days, Bob wants to know, how much money he could have earned, if he had acted optimally.
The first input line contains number n (1 ≤ n ≤ 5000) — amount of Bob's working days. The following n lines contain the description of the days. Line sell x stands for a day when a customer came to Bob to buy a 2x MB memory stick (0 ≤ x ≤ 2000). It's guaranteed that for each x there is not more than one line sell x. Line win x stands for a day when Bob won a 2x MB memory stick (0 ≤ x ≤ 2000).
Output the maximum possible earnings for Bob in berllars, that he would have had if he had known all the events beforehand. Don't forget, please, that Bob can't keep more than one memory stick at a time.
null
null
[{"input": "7\nwin 10\nwin 5\nwin 3\nsell 5\nsell 3\nwin 10\nsell 10", "output": "1056"}, {"input": "3\nwin 5\nsell 6\nsell 4", "output": "0"}]
2,000
["brute force", "dp", "greedy"]
45
[{"input": "7\r\nwin 10\r\nwin 5\r\nwin 3\r\nsell 5\r\nsell 3\r\nwin 10\r\nsell 10\r\n", "output": "1056\r\n"}, {"input": "3\r\nwin 5\r\nsell 6\r\nsell 4\r\n", "output": "0\r\n"}, {"input": "10\r\nsell 179\r\nwin 1278\r\nsell 1278\r\nwin 179\r\nwin 788\r\nsell 788\r\nwin 1819\r\nwin 1278\r\nsell 1454\r\nsell 1819\r\n", "output": "37459511778596727480858760720167552241582636504705413766024169777495064333423937410125519624693990051069809575647477719465460756326341562228323606665869931977125977431028709943048934214062888966581139223580790503937962827597404798307897711090567429316074325427043388117806141094834711707585035634104732053207574452493593409130554278913951011894497392495930884827685983975668127973918422057605356890341647839399778378381152159725053311750647457999739578989105335906181048932656785993705124392163591312698147450544686667028530553878359909362604965888\n"}, {"input": "10\r\nsell 573\r\nwin 1304\r\nsell 278\r\nwin 1631\r\nsell 1225\r\nsell 1631\r\nsell 177\r\nwin 1631\r\nwin 177\r\nsell 1304\r\n", "output": "95482312335125227379668481690754940528280513838693267460502082967052005332103697568042408703168913727303170456338425853153094403747135188778307041838920404959089576368946137708987138986696495077466398994298434148881715073638178666201165545650953479735059082316661443204882826188032944866093372620219104327689636641547141835841165681118172603993695103043804276669836594061369229043451067647935298287687852302215923887110435577776767805943668204998410716005202198549540411238299513630278811648\n"}, {"input": "10\r\nwin 1257\r\nwin 1934\r\nsell 1934\r\nsell 1257\r\nwin 1934\r\nwin 1257\r\nsell 495\r\nwin 495\r\nwin 495\r\nwin 1257\r\n", "output": "1556007242642049292787218246793379348327505438878680952714050868520307364441227819009733220897932984584977593931988662671459594674963394056587723382487766303981362587048873128400436836690128983570130687310221668877557121158055843621982630476422478413285775826498536883275291967793661985813155062733063913176306327509625594121241472451054995889483447103432414676059872469910105149496451402271546454282618581884282152530090816240540173251729211604658704990425330422792556824836640431985211146197816770068601144273155826261981248156109439380758754562057681271989937590784957541131485184\n"}, {"input": "10\r\nsell 1898\r\nsell 173\r\nsell 1635\r\nsell 29\r\nsell 881\r\nsell 434\r\nsell 1236\r\nsell 14\r\nwin 29\r\nsell 1165\r\n", "output": "0\r\n"}, {"input": "1\r\nsell 2000\r\n", "output": "0\r\n"}, {"input": "1\r\nwin 2000\r\n", "output": "0\r\n"}, {"input": "2\r\nwin 2000\r\nsell 2000\r\n", "output": "114813069527425452423283320117768198402231770208869520047764273682576626139237031385665948631650626991844596463898746277344711896086305533142593135616665318539129989145312280000688779148240044871428926990063486244781615463646388363947317026040466353970904996558162398808944629605623311649536164221970332681344168908984458505602379484807914058900934776500429002716706625830522008132236281291761267883317206598995396418127021779858404042159853183251540889433902091920554957783589672039160081957216630582755380425583726015528348786419432054508915275783882625175435528800822842770817965453762184851149029376\n"}]
false
stdio
null
true
18/D
18
D
PyPy 3
TESTS
32
498
4,096,000
90479801
n=int(input()) ar=[0]*(n+1) c=[] for i in range(n): a,b=input().split() b=int(b) if(a=="sell"): b=-b c.append(b) for i in range(1,n+1): ar[i]=max(ar[i-1],ar[i]) for j in range(i+1,n+1): if(c[i-1]+c[j-1]==0 and c[i-1]>c[j-1]): ar[j]=max(ar[j],ar[i]+2**c[i-1]) print(ar[n])
45
124
5,529,600
27965486
#!/usr/bin/python3 N = int(input()) maxval = 0 dp = [0]*N prev = [-1]*2010 for i in range(N): l = input().strip().split() l[1] = int(l[1]) if l[0] == "win": prev[l[1]] = i elif l[0] == "sell" and prev[l[1]] != -1: maxval = max(maxval,2**l[1]+dp[prev[l[1]]]) dp[i] = maxval print(maxval)
Codeforces Beta Round 18 (Div. 2 Only)
ICPC
2,010
2
128
Seller Bob
Last year Bob earned by selling memory sticks. During each of n days of his work one of the two following events took place: - A customer came to Bob and asked to sell him a 2x MB memory stick. If Bob had such a stick, he sold it and got 2x berllars. - Bob won some programming competition and got a 2x MB memory stick as a prize. Bob could choose whether to present this memory stick to one of his friends, or keep it. Bob never kept more than one memory stick, as he feared to mix up their capacities, and deceive a customer unintentionally. It is also known that for each memory stick capacity there was at most one customer, who wanted to buy that memory stick. Now, knowing all the customers' demands and all the prizes won at programming competitions during the last n days, Bob wants to know, how much money he could have earned, if he had acted optimally.
The first input line contains number n (1 ≤ n ≤ 5000) — amount of Bob's working days. The following n lines contain the description of the days. Line sell x stands for a day when a customer came to Bob to buy a 2x MB memory stick (0 ≤ x ≤ 2000). It's guaranteed that for each x there is not more than one line sell x. Line win x stands for a day when Bob won a 2x MB memory stick (0 ≤ x ≤ 2000).
Output the maximum possible earnings for Bob in berllars, that he would have had if he had known all the events beforehand. Don't forget, please, that Bob can't keep more than one memory stick at a time.
null
null
[{"input": "7\nwin 10\nwin 5\nwin 3\nsell 5\nsell 3\nwin 10\nsell 10", "output": "1056"}, {"input": "3\nwin 5\nsell 6\nsell 4", "output": "0"}]
2,000
["brute force", "dp", "greedy"]
45
[{"input": "7\r\nwin 10\r\nwin 5\r\nwin 3\r\nsell 5\r\nsell 3\r\nwin 10\r\nsell 10\r\n", "output": "1056\r\n"}, {"input": "3\r\nwin 5\r\nsell 6\r\nsell 4\r\n", "output": "0\r\n"}, {"input": "10\r\nsell 179\r\nwin 1278\r\nsell 1278\r\nwin 179\r\nwin 788\r\nsell 788\r\nwin 1819\r\nwin 1278\r\nsell 1454\r\nsell 1819\r\n", "output": "37459511778596727480858760720167552241582636504705413766024169777495064333423937410125519624693990051069809575647477719465460756326341562228323606665869931977125977431028709943048934214062888966581139223580790503937962827597404798307897711090567429316074325427043388117806141094834711707585035634104732053207574452493593409130554278913951011894497392495930884827685983975668127973918422057605356890341647839399778378381152159725053311750647457999739578989105335906181048932656785993705124392163591312698147450544686667028530553878359909362604965888\n"}, {"input": "10\r\nsell 573\r\nwin 1304\r\nsell 278\r\nwin 1631\r\nsell 1225\r\nsell 1631\r\nsell 177\r\nwin 1631\r\nwin 177\r\nsell 1304\r\n", "output": "95482312335125227379668481690754940528280513838693267460502082967052005332103697568042408703168913727303170456338425853153094403747135188778307041838920404959089576368946137708987138986696495077466398994298434148881715073638178666201165545650953479735059082316661443204882826188032944866093372620219104327689636641547141835841165681118172603993695103043804276669836594061369229043451067647935298287687852302215923887110435577776767805943668204998410716005202198549540411238299513630278811648\n"}, {"input": "10\r\nwin 1257\r\nwin 1934\r\nsell 1934\r\nsell 1257\r\nwin 1934\r\nwin 1257\r\nsell 495\r\nwin 495\r\nwin 495\r\nwin 1257\r\n", "output": "1556007242642049292787218246793379348327505438878680952714050868520307364441227819009733220897932984584977593931988662671459594674963394056587723382487766303981362587048873128400436836690128983570130687310221668877557121158055843621982630476422478413285775826498536883275291967793661985813155062733063913176306327509625594121241472451054995889483447103432414676059872469910105149496451402271546454282618581884282152530090816240540173251729211604658704990425330422792556824836640431985211146197816770068601144273155826261981248156109439380758754562057681271989937590784957541131485184\n"}, {"input": "10\r\nsell 1898\r\nsell 173\r\nsell 1635\r\nsell 29\r\nsell 881\r\nsell 434\r\nsell 1236\r\nsell 14\r\nwin 29\r\nsell 1165\r\n", "output": "0\r\n"}, {"input": "1\r\nsell 2000\r\n", "output": "0\r\n"}, {"input": "1\r\nwin 2000\r\n", "output": "0\r\n"}, {"input": "2\r\nwin 2000\r\nsell 2000\r\n", "output": "114813069527425452423283320117768198402231770208869520047764273682576626139237031385665948631650626991844596463898746277344711896086305533142593135616665318539129989145312280000688779148240044871428926990063486244781615463646388363947317026040466353970904996558162398808944629605623311649536164221970332681344168908984458505602379484807914058900934776500429002716706625830522008132236281291761267883317206598995396418127021779858404042159853183251540889433902091920554957783589672039160081957216630582755380425583726015528348786419432054508915275783882625175435528800822842770817965453762184851149029376\n"}]
false
stdio
null
true
18/D
18
D
Python 3
TESTS
32
248
1,126,400
73955174
n=int(input()) a=[2*[0]for i in range(0,n)] for i in range(0,n): a[i]=list(map(str,input().split())) na=[0]*2001 for i in range(0,2001): na[i]=[] c=[0]*(n+1) for i in range(0, n): ind=int(a[i][1]) if a[i][0]=='win': na[ind].append(i+1) else: c[i+1]=ind dp=[0]*(n+1) for i in range(1,n+1): dp[i]=dp[i-1] if c[i]!=0 and len(na[c[i]])>0: l=0 r=len(na[c[i]]) while(r-l>1): mid=(r+l)/2 mid=int(mid) if na[c[i]][mid]>=i: r=mid else: l=mid if na[c[i]][l]>=i: continue if dp[i]<2**c[i]+dp[na[c[i]][l]-1]: dp[i] = 2 ** c[i] + dp[na[c[i]][l] - 1] print(dp[n])
45
154
921,600
112211098
n = int(input()) s = ["" for _ in range(5005)] a = [0 for _ in range(5005)] for i in range(1,n+1): ta,tb = input().split(); s[i] = ta a[i] = int(tb) jp = [0 for _ in range(5005)] vis = [0 for _ in range(5005)] dp = [0 for _ in range(5005)] num = [(1<<_) for _ in range(2001)] for i in range(1,n+1): if(s[i][0]=='w'): vis[a[i]] = i else: if(vis[a[i]]): jp[i] = vis[a[i]]; for i in range(1,n+1): if(s[i][0] == 's'): if(jp[i]>0): dp[i] = max(dp[i], dp[jp[i]-1] + num[a[i]]); dp[i] = max(dp[i-1], dp[i]); print(dp[n])
Codeforces Beta Round 18 (Div. 2 Only)
ICPC
2,010
2
128
Seller Bob
Last year Bob earned by selling memory sticks. During each of n days of his work one of the two following events took place: - A customer came to Bob and asked to sell him a 2x MB memory stick. If Bob had such a stick, he sold it and got 2x berllars. - Bob won some programming competition and got a 2x MB memory stick as a prize. Bob could choose whether to present this memory stick to one of his friends, or keep it. Bob never kept more than one memory stick, as he feared to mix up their capacities, and deceive a customer unintentionally. It is also known that for each memory stick capacity there was at most one customer, who wanted to buy that memory stick. Now, knowing all the customers' demands and all the prizes won at programming competitions during the last n days, Bob wants to know, how much money he could have earned, if he had acted optimally.
The first input line contains number n (1 ≤ n ≤ 5000) — amount of Bob's working days. The following n lines contain the description of the days. Line sell x stands for a day when a customer came to Bob to buy a 2x MB memory stick (0 ≤ x ≤ 2000). It's guaranteed that for each x there is not more than one line sell x. Line win x stands for a day when Bob won a 2x MB memory stick (0 ≤ x ≤ 2000).
Output the maximum possible earnings for Bob in berllars, that he would have had if he had known all the events beforehand. Don't forget, please, that Bob can't keep more than one memory stick at a time.
null
null
[{"input": "7\nwin 10\nwin 5\nwin 3\nsell 5\nsell 3\nwin 10\nsell 10", "output": "1056"}, {"input": "3\nwin 5\nsell 6\nsell 4", "output": "0"}]
2,000
["brute force", "dp", "greedy"]
45
[{"input": "7\r\nwin 10\r\nwin 5\r\nwin 3\r\nsell 5\r\nsell 3\r\nwin 10\r\nsell 10\r\n", "output": "1056\r\n"}, {"input": "3\r\nwin 5\r\nsell 6\r\nsell 4\r\n", "output": "0\r\n"}, {"input": "10\r\nsell 179\r\nwin 1278\r\nsell 1278\r\nwin 179\r\nwin 788\r\nsell 788\r\nwin 1819\r\nwin 1278\r\nsell 1454\r\nsell 1819\r\n", "output": "37459511778596727480858760720167552241582636504705413766024169777495064333423937410125519624693990051069809575647477719465460756326341562228323606665869931977125977431028709943048934214062888966581139223580790503937962827597404798307897711090567429316074325427043388117806141094834711707585035634104732053207574452493593409130554278913951011894497392495930884827685983975668127973918422057605356890341647839399778378381152159725053311750647457999739578989105335906181048932656785993705124392163591312698147450544686667028530553878359909362604965888\n"}, {"input": "10\r\nsell 573\r\nwin 1304\r\nsell 278\r\nwin 1631\r\nsell 1225\r\nsell 1631\r\nsell 177\r\nwin 1631\r\nwin 177\r\nsell 1304\r\n", "output": "95482312335125227379668481690754940528280513838693267460502082967052005332103697568042408703168913727303170456338425853153094403747135188778307041838920404959089576368946137708987138986696495077466398994298434148881715073638178666201165545650953479735059082316661443204882826188032944866093372620219104327689636641547141835841165681118172603993695103043804276669836594061369229043451067647935298287687852302215923887110435577776767805943668204998410716005202198549540411238299513630278811648\n"}, {"input": "10\r\nwin 1257\r\nwin 1934\r\nsell 1934\r\nsell 1257\r\nwin 1934\r\nwin 1257\r\nsell 495\r\nwin 495\r\nwin 495\r\nwin 1257\r\n", "output": "1556007242642049292787218246793379348327505438878680952714050868520307364441227819009733220897932984584977593931988662671459594674963394056587723382487766303981362587048873128400436836690128983570130687310221668877557121158055843621982630476422478413285775826498536883275291967793661985813155062733063913176306327509625594121241472451054995889483447103432414676059872469910105149496451402271546454282618581884282152530090816240540173251729211604658704990425330422792556824836640431985211146197816770068601144273155826261981248156109439380758754562057681271989937590784957541131485184\n"}, {"input": "10\r\nsell 1898\r\nsell 173\r\nsell 1635\r\nsell 29\r\nsell 881\r\nsell 434\r\nsell 1236\r\nsell 14\r\nwin 29\r\nsell 1165\r\n", "output": "0\r\n"}, {"input": "1\r\nsell 2000\r\n", "output": "0\r\n"}, {"input": "1\r\nwin 2000\r\n", "output": "0\r\n"}, {"input": "2\r\nwin 2000\r\nsell 2000\r\n", "output": "114813069527425452423283320117768198402231770208869520047764273682576626139237031385665948631650626991844596463898746277344711896086305533142593135616665318539129989145312280000688779148240044871428926990063486244781615463646388363947317026040466353970904996558162398808944629605623311649536164221970332681344168908984458505602379484807914058900934776500429002716706625830522008132236281291761267883317206598995396418127021779858404042159853183251540889433902091920554957783589672039160081957216630582755380425583726015528348786419432054508915275783882625175435528800822842770817965453762184851149029376\n"}]
false
stdio
null
true
606/B
606
B
Python 3
TESTS
3
46
0
14748527
x, y, sx, sy = map(int, input().split()) sx, sy = sx - 1, sy - 1 direction = {'U' : (-1, 0), 'D' : (1, 0), 'L' : (0, -1), 'R' : (0, 1)} visited = [[False] * y for i in range(x)] prev = sx, sy s = input() sm = x * y - 1 print (1, end = ' ') for c in s[:-1]: d = direction[c] cur = tuple(map(sum, zip(prev, d))) if not 0 <= cur[0] < x or not 0 <= cur[1] < y or visited[cur[0]][cur[1]]: cur = prev print (0, end = ' ') else: print (1, end = ' ') sm -= 1 visited[cur[0]][cur[1]] = True prev = cur print (sm)
68
155
13,516,800
199128141
import sys, os, io input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline def f(u, v): return u * y + v x, y, x0, y0 = map(int, input().split()) d = {"D":(1, 0), "U":(-1, 0), "R":(0, 1), "L":(0, -1)} s = list(input().rstrip().decode()) visit = [0] * (x * y) i, j = x0 - 1, y0 - 1 ans = [] for k in s: ans.append(visit[f(i, j)] ^ 1) visit[f(i, j)] = 1 di, dj = d[k] ni, nj = i + di, j + dj if 0 <= ni < x and 0 <= nj < y: i, j = ni, nj ans.append(x * y - sum(ans)) sys.stdout.write(" ".join(map(str, ans)))
Codeforces Round 335 (Div. 2)
CF
2,015
2
256
Testing Robots
The Cybernetics Failures (CF) organisation made a prototype of a bomb technician robot. To find the possible problems it was decided to carry out a series of tests. At the beginning of each test the robot prototype will be placed in cell (x0, y0) of a rectangular squared field of size x × y, after that a mine will be installed into one of the squares of the field. It is supposed to conduct exactly x·y tests, each time a mine is installed into a square that has never been used before. The starting cell of the robot always remains the same. After placing the objects on the field the robot will have to run a sequence of commands given by string s, consisting only of characters 'L', 'R', 'U', 'D'. These commands tell the robot to move one square to the left, to the right, up or down, or stay idle if moving in the given direction is impossible. As soon as the robot fulfills all the sequence of commands, it will blow up due to a bug in the code. But if at some moment of time the robot is at the same square with the mine, it will also blow up, but not due to a bug in the code. Moving to the left decreases coordinate y, and moving to the right increases it. Similarly, moving up decreases the x coordinate, and moving down increases it. The tests can go on for very long, so your task is to predict their results. For each k from 0 to length(s) your task is to find in how many tests the robot will run exactly k commands before it blows up.
The first line of the input contains four integers x, y, x0, y0 (1 ≤ x, y ≤ 500, 1 ≤ x0 ≤ x, 1 ≤ y0 ≤ y) — the sizes of the field and the starting coordinates of the robot. The coordinate axis X is directed downwards and axis Y is directed to the right. The second line contains a sequence of commands s, which should be fulfilled by the robot. It has length from 1 to 100 000 characters and only consists of characters 'L', 'R', 'U', 'D'.
Print the sequence consisting of (length(s) + 1) numbers. On the k-th position, starting with zero, print the number of tests where the robot will run exactly k commands before it blows up.
null
In the first sample, if we exclude the probable impact of the mines, the robot's route will look like that: $$(2,2)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(2,4)\rightarrow(3,3)$$.
[{"input": "3 4 2 2\nUURDRDRL", "output": "1 1 0 1 1 1 1 0 6"}, {"input": "2 2 2 2\nULD", "output": "1 1 1 1"}]
1,600
["implementation"]
68
[{"input": "3 4 2 2\r\nUURDRDRL\r\n", "output": "1 1 0 1 1 1 1 0 6\r\n"}, {"input": "2 2 2 2\r\nULD\r\n", "output": "1 1 1 1\r\n"}, {"input": "1 1 1 1\r\nURDLUURRDDLLURDL\r\n", "output": "1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDD\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 245\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDDRRUR\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 241\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDDRRURR\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 0 241\r\n"}, {"input": "1 2 1 1\r\nR\r\n", "output": "1 1\r\n"}, {"input": "2 1 1 1\r\nD\r\n", "output": "1 1\r\n"}, {"input": "1 2 1 2\r\nLR\r\n", "output": "1 1 0\r\n"}, {"input": "2 1 2 1\r\nUD\r\n", "output": "1 1 0\r\n"}, {"input": "4 4 2 2\r\nDRUL\r\n", "output": "1 1 1 1 12\r\n"}, {"input": "4 4 3 3\r\nLUDRUL\r\n", "output": "1 1 1 0 0 1 12\r\n"}, {"input": "15 17 8 9\r\nURRDLU\r\n", "output": "1 1 1 1 1 1 249\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDR\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 244\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDRR\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 0 244\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDRRR\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 0 0 244\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDRRRR\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 0 0 0 244\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDRRRRU\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 0 0 0 1 243\r\n"}]
false
stdio
null
true
606/B
606
B
Python 3
TESTS
3
46
0
14783733
x, y, x0, y0 = map(int, input().split()) s = input() k = (len(s)+1)*[0] k[0] = 1 u = 0 for i in s: u += 1 xo = x0; yo = y0 if i == 'U' and x0 > 1: x0 -= 1 elif i == 'D' and x0 < x: x0 += 1 elif i == 'L' and y0 > 1: y0 -= 1 elif i == 'R' and y0 < y: y0 += 1 if not(x0 == xo and y0 == yo): k[u]+=1 k[len(s)] += (x*y)-sum(k) for i in k: print(i, end=' ')
68
155
19,046,400
210793807
import sys input = lambda: sys.stdin.readline().rstrip() x,y,x0,y0 = map(int, input().split()) S = input() ans = [1] seen = set() seen.add((x0,y0)) for s in S: if s=='U': x0-=1 elif s=='D': x0+=1 elif s=='L': y0-=1 else: y0+=1 if x0<1: x0=1 if x0>x: x0=x if y0<1: y0=1 if y0>y: y0=y if (x0,y0) in seen: ans.append(0) else: ans.append(1) seen.add((x0,y0)) t = sum(ans) ans[-1]+=x*y-t print(*ans)
Codeforces Round 335 (Div. 2)
CF
2,015
2
256
Testing Robots
The Cybernetics Failures (CF) organisation made a prototype of a bomb technician robot. To find the possible problems it was decided to carry out a series of tests. At the beginning of each test the robot prototype will be placed in cell (x0, y0) of a rectangular squared field of size x × y, after that a mine will be installed into one of the squares of the field. It is supposed to conduct exactly x·y tests, each time a mine is installed into a square that has never been used before. The starting cell of the robot always remains the same. After placing the objects on the field the robot will have to run a sequence of commands given by string s, consisting only of characters 'L', 'R', 'U', 'D'. These commands tell the robot to move one square to the left, to the right, up or down, or stay idle if moving in the given direction is impossible. As soon as the robot fulfills all the sequence of commands, it will blow up due to a bug in the code. But if at some moment of time the robot is at the same square with the mine, it will also blow up, but not due to a bug in the code. Moving to the left decreases coordinate y, and moving to the right increases it. Similarly, moving up decreases the x coordinate, and moving down increases it. The tests can go on for very long, so your task is to predict their results. For each k from 0 to length(s) your task is to find in how many tests the robot will run exactly k commands before it blows up.
The first line of the input contains four integers x, y, x0, y0 (1 ≤ x, y ≤ 500, 1 ≤ x0 ≤ x, 1 ≤ y0 ≤ y) — the sizes of the field and the starting coordinates of the robot. The coordinate axis X is directed downwards and axis Y is directed to the right. The second line contains a sequence of commands s, which should be fulfilled by the robot. It has length from 1 to 100 000 characters and only consists of characters 'L', 'R', 'U', 'D'.
Print the sequence consisting of (length(s) + 1) numbers. On the k-th position, starting with zero, print the number of tests where the robot will run exactly k commands before it blows up.
null
In the first sample, if we exclude the probable impact of the mines, the robot's route will look like that: $$(2,2)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(2,4)\rightarrow(3,3)$$.
[{"input": "3 4 2 2\nUURDRDRL", "output": "1 1 0 1 1 1 1 0 6"}, {"input": "2 2 2 2\nULD", "output": "1 1 1 1"}]
1,600
["implementation"]
68
[{"input": "3 4 2 2\r\nUURDRDRL\r\n", "output": "1 1 0 1 1 1 1 0 6\r\n"}, {"input": "2 2 2 2\r\nULD\r\n", "output": "1 1 1 1\r\n"}, {"input": "1 1 1 1\r\nURDLUURRDDLLURDL\r\n", "output": "1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDD\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 245\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDDRRUR\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 241\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDDRRURR\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 0 241\r\n"}, {"input": "1 2 1 1\r\nR\r\n", "output": "1 1\r\n"}, {"input": "2 1 1 1\r\nD\r\n", "output": "1 1\r\n"}, {"input": "1 2 1 2\r\nLR\r\n", "output": "1 1 0\r\n"}, {"input": "2 1 2 1\r\nUD\r\n", "output": "1 1 0\r\n"}, {"input": "4 4 2 2\r\nDRUL\r\n", "output": "1 1 1 1 12\r\n"}, {"input": "4 4 3 3\r\nLUDRUL\r\n", "output": "1 1 1 0 0 1 12\r\n"}, {"input": "15 17 8 9\r\nURRDLU\r\n", "output": "1 1 1 1 1 1 249\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDR\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 244\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDRR\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 0 244\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDRRR\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 0 0 244\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDRRRR\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 0 0 0 244\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDRRRRU\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 0 0 0 1 243\r\n"}]
false
stdio
null
true
606/B
606
B
Python 3
PRETESTS
3
31
0
14720442
x, y, x0, y0 = map(int, input().split()) res = x * y - 1 our = dict() our['U'] = (-1, 0) our['D'] = (1, 0) our['R'] = (0, 1) our['L'] = (0, -1) s = input() print(1, end=' ') for i in range(len(s) - 1): dx, dy = our[s[i]] if 1 <= x0 + dx <= x and 1 <= y0 + dy <= y: x0 += dx y0 += dy print(1, end=' ') res -= 1 else: print(0, end=' ') print(res)
68
171
2,355,200
14729604
x, y, x0, y0 = map(int, input().split()) s = input() s = s[:len(s) - 1] visited = [[False for i in range(y + 1)] for j in range(x + 1)] visited[x0][y0] = True ans = '1 ' cnt = 1 for i in s: if i == 'U': if x0 != 1: x0 -= 1 if not visited[x0][y0]: ans += '1 ' cnt += 1 visited[x0][y0] = True else: ans += '0 ' else: ans += '0 ' elif i == 'D': if x0 != x: x0 += 1 if not visited[x0][y0]: ans += '1 ' cnt += 1 visited[x0][y0] = True else: ans += '0 ' else: ans += '0 ' elif i == 'L': if y0 != 1: y0 -= 1 if not visited[x0][y0]: ans += '1 ' cnt += 1 visited[x0][y0] = True else: ans += '0 ' else: ans += '0 ' else: if y0 != y: y0 += 1 if not visited[x0][y0]: ans += '1 ' cnt += 1 visited[x0][y0] = True else: ans += '0 ' else: ans += '0 ' z = str(x * y - cnt) print(ans + z)
Codeforces Round 335 (Div. 2)
CF
2,015
2
256
Testing Robots
The Cybernetics Failures (CF) organisation made a prototype of a bomb technician robot. To find the possible problems it was decided to carry out a series of tests. At the beginning of each test the robot prototype will be placed in cell (x0, y0) of a rectangular squared field of size x × y, after that a mine will be installed into one of the squares of the field. It is supposed to conduct exactly x·y tests, each time a mine is installed into a square that has never been used before. The starting cell of the robot always remains the same. After placing the objects on the field the robot will have to run a sequence of commands given by string s, consisting only of characters 'L', 'R', 'U', 'D'. These commands tell the robot to move one square to the left, to the right, up or down, or stay idle if moving in the given direction is impossible. As soon as the robot fulfills all the sequence of commands, it will blow up due to a bug in the code. But if at some moment of time the robot is at the same square with the mine, it will also blow up, but not due to a bug in the code. Moving to the left decreases coordinate y, and moving to the right increases it. Similarly, moving up decreases the x coordinate, and moving down increases it. The tests can go on for very long, so your task is to predict their results. For each k from 0 to length(s) your task is to find in how many tests the robot will run exactly k commands before it blows up.
The first line of the input contains four integers x, y, x0, y0 (1 ≤ x, y ≤ 500, 1 ≤ x0 ≤ x, 1 ≤ y0 ≤ y) — the sizes of the field and the starting coordinates of the robot. The coordinate axis X is directed downwards and axis Y is directed to the right. The second line contains a sequence of commands s, which should be fulfilled by the robot. It has length from 1 to 100 000 characters and only consists of characters 'L', 'R', 'U', 'D'.
Print the sequence consisting of (length(s) + 1) numbers. On the k-th position, starting with zero, print the number of tests where the robot will run exactly k commands before it blows up.
null
In the first sample, if we exclude the probable impact of the mines, the robot's route will look like that: $$(2,2)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(2,4)\rightarrow(3,3)$$.
[{"input": "3 4 2 2\nUURDRDRL", "output": "1 1 0 1 1 1 1 0 6"}, {"input": "2 2 2 2\nULD", "output": "1 1 1 1"}]
1,600
["implementation"]
68
[{"input": "3 4 2 2\r\nUURDRDRL\r\n", "output": "1 1 0 1 1 1 1 0 6\r\n"}, {"input": "2 2 2 2\r\nULD\r\n", "output": "1 1 1 1\r\n"}, {"input": "1 1 1 1\r\nURDLUURRDDLLURDL\r\n", "output": "1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDD\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 245\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDDRRUR\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 241\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDDRRURR\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 0 241\r\n"}, {"input": "1 2 1 1\r\nR\r\n", "output": "1 1\r\n"}, {"input": "2 1 1 1\r\nD\r\n", "output": "1 1\r\n"}, {"input": "1 2 1 2\r\nLR\r\n", "output": "1 1 0\r\n"}, {"input": "2 1 2 1\r\nUD\r\n", "output": "1 1 0\r\n"}, {"input": "4 4 2 2\r\nDRUL\r\n", "output": "1 1 1 1 12\r\n"}, {"input": "4 4 3 3\r\nLUDRUL\r\n", "output": "1 1 1 0 0 1 12\r\n"}, {"input": "15 17 8 9\r\nURRDLU\r\n", "output": "1 1 1 1 1 1 249\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDR\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 244\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDRR\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 0 244\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDRRR\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 0 0 244\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDRRRR\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 0 0 0 244\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDRRRRU\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 0 0 0 1 243\r\n"}]
false
stdio
null
true
606/B
606
B
Python 3
PRETESTS
4
61
0
14730255
y, x, x_, y_ = list(map(int, input().split())) c = input() ans = [0 for i in range(len(c) + 1)] ans[0] = 1 p = 1 was_here = set() was_here.add((x_, y_)) for i in c: if i == 'U': y_ = max(1, y_ - 1) elif i == 'D': y_ = min(y, y_ + 1) elif i == 'L': x_ = max(1, x_ - 1) else: x_ = min(x, x_ + 1) if (x_, y_) in was_here: ans[p] = 0 elif p < len(c): ans[p] = 1 was_here.add((x_, y_)) else: ans[p] = x * y - len(was_here) p += 1 print(' '.join(list(map(str, ans))))
68
186
10,752,000
162511090
[r, c, x, y], s = map(int, input().split()), input() vis = [[False for i in range(c)] for j in range(r)] x, y = x - 1, y - 1 vis[x][y] = True ans = [1] for ch in s: if ch == 'U': x = max(0, x - 1) elif ch == 'R': y = min(c - 1, y + 1) elif ch == 'D': x = min(r - 1, x + 1) else: y = max(0, y - 1) ans.append(int(not vis[x][y])) vis[x][y] = True for i in range(r): for j in range(c): ans[-1] += int(not vis[i][j]) print(*ans)
Codeforces Round 335 (Div. 2)
CF
2,015
2
256
Testing Robots
The Cybernetics Failures (CF) organisation made a prototype of a bomb technician robot. To find the possible problems it was decided to carry out a series of tests. At the beginning of each test the robot prototype will be placed in cell (x0, y0) of a rectangular squared field of size x × y, after that a mine will be installed into one of the squares of the field. It is supposed to conduct exactly x·y tests, each time a mine is installed into a square that has never been used before. The starting cell of the robot always remains the same. After placing the objects on the field the robot will have to run a sequence of commands given by string s, consisting only of characters 'L', 'R', 'U', 'D'. These commands tell the robot to move one square to the left, to the right, up or down, or stay idle if moving in the given direction is impossible. As soon as the robot fulfills all the sequence of commands, it will blow up due to a bug in the code. But if at some moment of time the robot is at the same square with the mine, it will also blow up, but not due to a bug in the code. Moving to the left decreases coordinate y, and moving to the right increases it. Similarly, moving up decreases the x coordinate, and moving down increases it. The tests can go on for very long, so your task is to predict their results. For each k from 0 to length(s) your task is to find in how many tests the robot will run exactly k commands before it blows up.
The first line of the input contains four integers x, y, x0, y0 (1 ≤ x, y ≤ 500, 1 ≤ x0 ≤ x, 1 ≤ y0 ≤ y) — the sizes of the field and the starting coordinates of the robot. The coordinate axis X is directed downwards and axis Y is directed to the right. The second line contains a sequence of commands s, which should be fulfilled by the robot. It has length from 1 to 100 000 characters and only consists of characters 'L', 'R', 'U', 'D'.
Print the sequence consisting of (length(s) + 1) numbers. On the k-th position, starting with zero, print the number of tests where the robot will run exactly k commands before it blows up.
null
In the first sample, if we exclude the probable impact of the mines, the robot's route will look like that: $$(2,2)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(2,4)\rightarrow(3,3)$$.
[{"input": "3 4 2 2\nUURDRDRL", "output": "1 1 0 1 1 1 1 0 6"}, {"input": "2 2 2 2\nULD", "output": "1 1 1 1"}]
1,600
["implementation"]
68
[{"input": "3 4 2 2\r\nUURDRDRL\r\n", "output": "1 1 0 1 1 1 1 0 6\r\n"}, {"input": "2 2 2 2\r\nULD\r\n", "output": "1 1 1 1\r\n"}, {"input": "1 1 1 1\r\nURDLUURRDDLLURDL\r\n", "output": "1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDD\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 245\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDDRRUR\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 241\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDDRRURR\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 0 241\r\n"}, {"input": "1 2 1 1\r\nR\r\n", "output": "1 1\r\n"}, {"input": "2 1 1 1\r\nD\r\n", "output": "1 1\r\n"}, {"input": "1 2 1 2\r\nLR\r\n", "output": "1 1 0\r\n"}, {"input": "2 1 2 1\r\nUD\r\n", "output": "1 1 0\r\n"}, {"input": "4 4 2 2\r\nDRUL\r\n", "output": "1 1 1 1 12\r\n"}, {"input": "4 4 3 3\r\nLUDRUL\r\n", "output": "1 1 1 0 0 1 12\r\n"}, {"input": "15 17 8 9\r\nURRDLU\r\n", "output": "1 1 1 1 1 1 249\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDR\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 244\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDRR\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 0 244\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDRRR\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 0 0 244\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDRRRR\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 0 0 0 244\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDRRRRU\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 0 0 0 1 243\r\n"}]
false
stdio
null
true
18/D
18
D
PyPy 3-64
TESTS
24
280
6,144,000
150917218
n=int(input()) DP=[-10]*2001 DP[0]=0 for i in range(n): s,x=input().split() x=int(x) if s[0]=="w": if DP[x]<DP[0]: DP[x]=DP[0] else: if DP[x]>=0: DP[0]=max(DP[0],DP[x]+(1<<x)) print(max(DP))
45
154
4,915,200
219090777
''' BeezMinh 16:43 UTC+7 16/08/2023 ''' from sys import stdin input = lambda: stdin.readline().rstrip() a = [0] * 2048 ans = 0 for i in range(int(input())): s, x = input().split() if s == 'win': a[int(x)] = ans + 2 ** int(x) else: ans = max(ans, a[int(x)]) print(ans)
Codeforces Beta Round 18 (Div. 2 Only)
ICPC
2,010
2
128
Seller Bob
Last year Bob earned by selling memory sticks. During each of n days of his work one of the two following events took place: - A customer came to Bob and asked to sell him a 2x MB memory stick. If Bob had such a stick, he sold it and got 2x berllars. - Bob won some programming competition and got a 2x MB memory stick as a prize. Bob could choose whether to present this memory stick to one of his friends, or keep it. Bob never kept more than one memory stick, as he feared to mix up their capacities, and deceive a customer unintentionally. It is also known that for each memory stick capacity there was at most one customer, who wanted to buy that memory stick. Now, knowing all the customers' demands and all the prizes won at programming competitions during the last n days, Bob wants to know, how much money he could have earned, if he had acted optimally.
The first input line contains number n (1 ≤ n ≤ 5000) — amount of Bob's working days. The following n lines contain the description of the days. Line sell x stands for a day when a customer came to Bob to buy a 2x MB memory stick (0 ≤ x ≤ 2000). It's guaranteed that for each x there is not more than one line sell x. Line win x stands for a day when Bob won a 2x MB memory stick (0 ≤ x ≤ 2000).
Output the maximum possible earnings for Bob in berllars, that he would have had if he had known all the events beforehand. Don't forget, please, that Bob can't keep more than one memory stick at a time.
null
null
[{"input": "7\nwin 10\nwin 5\nwin 3\nsell 5\nsell 3\nwin 10\nsell 10", "output": "1056"}, {"input": "3\nwin 5\nsell 6\nsell 4", "output": "0"}]
2,000
["brute force", "dp", "greedy"]
45
[{"input": "7\r\nwin 10\r\nwin 5\r\nwin 3\r\nsell 5\r\nsell 3\r\nwin 10\r\nsell 10\r\n", "output": "1056\r\n"}, {"input": "3\r\nwin 5\r\nsell 6\r\nsell 4\r\n", "output": "0\r\n"}, {"input": "10\r\nsell 179\r\nwin 1278\r\nsell 1278\r\nwin 179\r\nwin 788\r\nsell 788\r\nwin 1819\r\nwin 1278\r\nsell 1454\r\nsell 1819\r\n", "output": "37459511778596727480858760720167552241582636504705413766024169777495064333423937410125519624693990051069809575647477719465460756326341562228323606665869931977125977431028709943048934214062888966581139223580790503937962827597404798307897711090567429316074325427043388117806141094834711707585035634104732053207574452493593409130554278913951011894497392495930884827685983975668127973918422057605356890341647839399778378381152159725053311750647457999739578989105335906181048932656785993705124392163591312698147450544686667028530553878359909362604965888\n"}, {"input": "10\r\nsell 573\r\nwin 1304\r\nsell 278\r\nwin 1631\r\nsell 1225\r\nsell 1631\r\nsell 177\r\nwin 1631\r\nwin 177\r\nsell 1304\r\n", "output": "95482312335125227379668481690754940528280513838693267460502082967052005332103697568042408703168913727303170456338425853153094403747135188778307041838920404959089576368946137708987138986696495077466398994298434148881715073638178666201165545650953479735059082316661443204882826188032944866093372620219104327689636641547141835841165681118172603993695103043804276669836594061369229043451067647935298287687852302215923887110435577776767805943668204998410716005202198549540411238299513630278811648\n"}, {"input": "10\r\nwin 1257\r\nwin 1934\r\nsell 1934\r\nsell 1257\r\nwin 1934\r\nwin 1257\r\nsell 495\r\nwin 495\r\nwin 495\r\nwin 1257\r\n", "output": "1556007242642049292787218246793379348327505438878680952714050868520307364441227819009733220897932984584977593931988662671459594674963394056587723382487766303981362587048873128400436836690128983570130687310221668877557121158055843621982630476422478413285775826498536883275291967793661985813155062733063913176306327509625594121241472451054995889483447103432414676059872469910105149496451402271546454282618581884282152530090816240540173251729211604658704990425330422792556824836640431985211146197816770068601144273155826261981248156109439380758754562057681271989937590784957541131485184\n"}, {"input": "10\r\nsell 1898\r\nsell 173\r\nsell 1635\r\nsell 29\r\nsell 881\r\nsell 434\r\nsell 1236\r\nsell 14\r\nwin 29\r\nsell 1165\r\n", "output": "0\r\n"}, {"input": "1\r\nsell 2000\r\n", "output": "0\r\n"}, {"input": "1\r\nwin 2000\r\n", "output": "0\r\n"}, {"input": "2\r\nwin 2000\r\nsell 2000\r\n", "output": "114813069527425452423283320117768198402231770208869520047764273682576626139237031385665948631650626991844596463898746277344711896086305533142593135616665318539129989145312280000688779148240044871428926990063486244781615463646388363947317026040466353970904996558162398808944629605623311649536164221970332681344168908984458505602379484807914058900934776500429002716706625830522008132236281291761267883317206598995396418127021779858404042159853183251540889433902091920554957783589672039160081957216630582755380425583726015528348786419432054508915275783882625175435528800822842770817965453762184851149029376\n"}]
false
stdio
null
true
765/D
765
D
Python 3
TESTS
1
93
0
62645047
n = int(input()) f = list(map(int, input().split())) s = set(f) distinct = len(s) first = set(f[:distinct]) if(sorted(f) != f or len(first) != distinct): print(-1) exit() print(distinct) print(*f) print(*list(first))
43
248
29,593,600
126192952
from collections import defaultdict def solve(n, f): d = defaultdict(set) for i,fi in enumerate(f, 1): d[fi].add(i) m = len(d) g = [0]*n h = [0]*m for i,(fi,ys) in enumerate(d.items()): if fi not in ys: return -1, [], [] h[i] = fi for j in ys: g[j-1] = i+1 return m, g, h n = int(input()) f = list(map(int,input().split())) res, g, h = solve(n, f) print(res) if res > -1: print(' '.join(map(str, g))) print(' '.join(map(str, h)))
Codeforces Round 397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined)
CF
2,017
2
512
Artsem and Saunders
Artsem has a friend Saunders from University of Chicago. Saunders presented him with the following problem. Let [n] denote the set {1, ..., n}. We will also write f: [x] → [y] when a function f is defined in integer points 1, ..., x, and all its values are integers from 1 to y. Now then, you are given a function f: [n] → [n]. Your task is to find a positive integer m, and two functions g: [n] → [m], h: [m] → [n], such that g(h(x)) = x for all $$x \in [m]$$, and h(g(x)) = f(x) for all $$x \in [n]$$, or determine that finding these is impossible.
The first line contains an integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers — values f(1), ..., f(n) (1 ≤ f(i) ≤ n).
If there is no answer, print one integer -1. Otherwise, on the first line print the number m (1 ≤ m ≤ 106). On the second line print n numbers g(1), ..., g(n). On the third line print m numbers h(1), ..., h(m). If there are several correct answers, you may output any of them. It is guaranteed that if a valid answer exists, then there is an answer satisfying the above restrictions.
null
null
[{"input": "3\n1 2 3", "output": "3\n1 2 3\n1 2 3"}, {"input": "3\n2 2 2", "output": "1\n1 1 1\n2"}, {"input": "2\n2 1", "output": "-1"}]
1,700
["constructive algorithms", "dsu", "math"]
43
[{"input": "3\r\n1 2 3\r\n", "output": "3\r\n1 2 3\r\n1 2 3\r\n"}, {"input": "3\r\n2 2 2\r\n", "output": "1\r\n1 1 1\r\n2\r\n"}, {"input": "2\r\n2 1\r\n", "output": "-1\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n1\r\n1\r\n"}, {"input": "2\r\n2 1\r\n", "output": "-1\r\n"}, {"input": "2\r\n2 2\r\n", "output": "1\r\n1 1\r\n2\r\n"}, {"input": "5\r\n5 5 5 3 5\r\n", "output": "-1\r\n"}, {"input": "10\r\n4 4 4 4 4 4 4 4 4 4\r\n", "output": "1\r\n1 1 1 1 1 1 1 1 1 1\r\n4\r\n"}, {"input": "2\r\n1 2\r\n", "output": "2\r\n1 2\r\n1 2\r\n"}, {"input": "3\r\n3 2 3\r\n", "output": "2\r\n2 1 2\r\n2 3\r\n"}, {"input": "3\r\n1 2 1\r\n", "output": "2\r\n1 2 1\r\n1 2\r\n"}, {"input": "4\r\n4 2 4 4\r\n", "output": "2\r\n2 1 2 2\r\n2 4\r\n"}, {"input": "5\r\n1 4 5 4 5\r\n", "output": "3\r\n1 2 3 2 3\r\n1 4 5\r\n"}, {"input": "4\r\n1 2 1 2\r\n", "output": "2\r\n1 2 1 2\r\n1 2\r\n"}, {"input": "5\r\n1 3 3 4 4\r\n", "output": "3\r\n1 2 2 3 3\r\n1 3 4\r\n"}, {"input": "4\r\n4 2 2 4\r\n", "output": "2\r\n2 1 1 2\r\n2 4\r\n"}, {"input": "7\r\n7 3 3 5 5 7 7\r\n", "output": "3\r\n3 1 1 2 2 3 3\r\n3 5 7\r\n"}, {"input": "6\r\n1 1 1 3 3 3\r\n", "output": "-1\r\n"}, {"input": "4\r\n2 2 3 2\r\n", "output": "2\r\n1 1 2 1\r\n2 3\r\n"}, {"input": "6\r\n1 2 3 4 5 5\r\n", "output": "5\r\n1 2 3 4 5 5\r\n1 2 3 4 5\r\n"}, {"input": "3\r\n1 1 2\r\n", "output": "-1\r\n"}, {"input": "4\r\n3 4 3 4\r\n", "output": "2\r\n1 2 1 2\r\n3 4\r\n"}, {"input": "6\r\n1 1 1 4 4 4\r\n", "output": "2\r\n1 1 1 2 2 2\r\n1 4\r\n"}, {"input": "4\r\n1 2 1 1\r\n", "output": "2\r\n1 2 1 1\r\n1 2\r\n"}, {"input": "5\r\n1 2 3 4 3\r\n", "output": "4\r\n1 2 3 4 3\r\n1 2 3 4\r\n"}, {"input": "4\r\n2 2 4 4\r\n", "output": "2\r\n1 1 2 2\r\n2 4\r\n"}, {"input": "4\r\n1 1 3 3\r\n", "output": "2\r\n1 1 2 2\r\n1 3\r\n"}, {"input": "3\r\n2 2 3\r\n", "output": "2\r\n1 1 2\r\n2 3\r\n"}, {"input": "5\r\n5 3 3 3 5\r\n", "output": "2\r\n2 1 1 1 2\r\n3 5\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()) f_list = list(map(int, f.readline().strip().split())) with open(submission_path) as sub_f: lines = [line.strip() for line in sub_f if line.strip()] if not lines: print(0) return first_line = lines[0] if first_line == '-1': # Check if the problem is truly impossible possible = True for x in range(n): if f_list[x] < 1 or f_list[x] > n: possible = False break fx = f_list[x] if f_list[fx - 1] != fx: possible = False break if possible: print(0) else: print(1) return # Try to parse m, g, h try: m = int(first_line) if m < 1 or m > 10**6: print(0) return if len(lines) < 3: print(0) return g = list(map(int, lines[1].split())) h = list(map(int, lines[2].split())) if len(g) != n or len(h) != m: print(0) return # Check g values for num in g: if num < 1 or num > m: print(0) return # Check h values for num in h: if num < 1 or num > n: print(0) return # Check h(g(x)) == f[x] for all x for x in range(n): gx = g[x] h_gx = h[gx - 1] if h_gx != f_list[x]: print(0) return # Check g(h(y)) == y+1 for all y (0-based) for y in range(m): hy = h[y] g_hy = g[hy - 1] if g_hy != (y + 1): print(0) return print(1) except: print(0) if __name__ == '__main__': main()
true
180/E
180
E
Python 3
TESTS
5
92
307,200
205662613
from collections import defaultdict from sys import stdin def max_points(n, m, k, cubes): color_counts = defaultdict(int) max_count = 0 left = 0 total_to_delete = 0 for right in range(n): color = cubes[right] color_counts[color] += 1 total_to_delete += 1 - (cubes[left] == color) while total_to_delete > k: color_counts[cubes[left]] -= 1 total_to_delete -= 1 - (cubes[left] == cubes[left + 1]) left += 1 max_count = max(max_count, color_counts[color]) return max_count def main(): n, m, k = map(int, stdin.readline().split()) cubes = list(map(int, stdin.readline().split())) print(max_points(n, m, k, cubes)) if __name__ == "__main__": main()
50
310
16,588,800
199566000
import sys, os, io input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline n, m, k = map(int, input().split()) a = list(map(int, input().split())) x = [[] for _ in range(m + 1)] for i in range(n): x[a[i]].append(i) ans = 0 for y in x: if not y: continue r = 0 for l in range(len(y)): while r < len(y) and y[r] - y[l] - (r - l) <= k: r += 1 ans = max(ans, r - l) print(ans)
Codeforces Round 116 (Div. 2, ACM-ICPC Rules)
ICPC
2,012
1
256
Cubes
Let's imagine that you're playing the following simple computer game. The screen displays n lined-up cubes. Each cube is painted one of m colors. You are allowed to delete not more than k cubes (that do not necessarily go one after another). After that, the remaining cubes join together (so that the gaps are closed) and the system counts the score. The number of points you score equals to the length of the maximum sequence of cubes of the same color that follow consecutively. Write a program that determines the maximum possible number of points you can score. Remember, you may delete no more than k any cubes. It is allowed not to delete cubes at all.
The first line contains three integers n, m and k (1 ≤ n ≤ 2·105, 1 ≤ m ≤ 105, 0 ≤ k < n). The second line contains n integers from 1 to m — the numbers of cube colors. The numbers of colors are separated by single spaces.
Print the maximum possible number of points you can score.
null
In the first sample you should delete the fifth and the sixth cubes. In the second sample you should delete the fourth and the seventh cubes. In the third sample you shouldn't delete any cubes.
[{"input": "10 3 2\n1 2 1 1 3 2 1 1 2 2", "output": "4"}, {"input": "10 2 2\n1 2 1 2 1 1 2 1 1 2", "output": "5"}, {"input": "3 1 2\n1 1 1", "output": "3"}]
1,800
["binary search", "dp", "two pointers"]
50
[{"input": "10 3 2\r\n1 2 1 1 3 2 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "10 2 2\r\n1 2 1 2 1 1 2 1 1 2\r\n", "output": "5\r\n"}, {"input": "3 1 2\r\n1 1 1\r\n", "output": "3\r\n"}, {"input": "10 2 2\r\n1 1 1 2 1 2 1 2 1 1\r\n", "output": "5\r\n"}, {"input": "1 1 0\r\n1\r\n", "output": "1\r\n"}, {"input": "20 3 5\r\n2 2 3 1 2 2 3 3 3 2 1 2 3 1 1 3 3 3 2 3\r\n", "output": "7\r\n"}, {"input": "20 2 5\r\n2 2 1 2 1 2 1 2 1 1 2 1 2 2 1 2 2 1 2 1\r\n", "output": "7\r\n"}, {"input": "20 6 3\r\n4 1 2 6 3 3 2 5 2 5 2 1 1 4 1 2 2 1 1 4\r\n", "output": "5\r\n"}, {"input": "30 5 8\r\n1 4 1 5 3 4 4 1 1 4 1 3 5 5 5 5 1 5 1 5 2 3 2 2 3 4 5 2 1 2\r\n", "output": "7\r\n"}, {"input": "30 5 6\r\n4 2 2 1 3 4 2 3 2 4 3 1 1 4 4 3 5 1 4 5 5 1 2 2 1 2 4 4 1 2\r\n", "output": "4\r\n"}, {"input": "100 10 15\r\n6 6 6 6 7 7 8 8 4 4 4 1 1 7 7 7 1 1 1 2 2 2 2 2 2 2 2 2 10 5 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 6 6 10 2 2 8 8 1 1 1 1 1 6 6 6 6 2 2 3 3 9 9 9 9 9 10 10 10 10 10 4 9 9 9 7 7 7 7 9 9 7 7 5 8 8 8 8 2\r\n", "output": "25\r\n"}, {"input": "99 10 17\r\n3 2 2 9 7 10 10 10 10 6 6 6 3 7 3 3 7 2 2 2 2 2 10 10 2 2 7 7 7 7 1 8 8 8 8 10 9 10 10 10 5 5 2 2 5 5 5 1 4 9 9 2 2 3 3 2 2 9 9 9 9 9 9 9 7 4 8 8 4 8 8 10 10 4 5 9 9 10 5 5 5 5 5 8 8 8 8 2 2 2 2 1 8 8 5 10 10 2 2\r\n", "output": "11\r\n"}, {"input": "94 10 20\r\n2 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 3 3 3 3 6 6 5 1 5 5 5 2 2 2 2 4 1 1 1 1 8 8 10 5 2 2 4 4 4 4 4 3 3 3 3 3 6 6 6 6 2 2 2 2 2 2 2 2 1 10 10 2 2 2 6 6 6 8 4 4 4 8 1 1 1 1 1 1 6 6 2 2 8 7 7 7 3 4\r\n", "output": "13\r\n"}, {"input": "99 3 15\r\n2 2 2 2 2 2 3 3 1 1 1 1 1 1 1 1 1 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 3 1 1 1 1 1 3 3 3 1 1 1 3 3 3 3 3 3 3 1 1 1 1 3 3 3 3 3 3 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 2 2 2 2 2 2\r\n", "output": "27\r\n"}, {"input": "100 5 10\r\n4 4 4 4 4 4 4 4 4 4 4 4 2 4 4 4 4 4 4 4 4 4 2 2 2 3 3 3 3 3 3 3 4 4 4 3 3 2 1 1 1 2 3 3 5 5 5 5 5 5 5 5 5 5 4 4 4 4 4 4 4 4 4 2 2 2 2 2 2 2 4 4 4 4 5 4 4 4 4 4 4 4 3 3 3 3 3 3 3 3 3 5 5 5 3 3 4 3 3 3\r\n", "output": "21\r\n"}, {"input": "98 4 20\r\n3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 1 1 1 1 1 1 1 1 1 1 1 1 4 4 4 4 4 4 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 2 2 2 2 2 2 2 1 1 1 3 3 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 2 2 2 2 2 2 2 3 3 1 1 2 2 2 2 3 3 3\r\n", "output": "30\r\n"}, {"input": "92 5 40\r\n3 3 3 3 2 2 2 2 5 5 5 5 5 5 5 5 5 5 5 4 4 4 4 4 4 3 3 5 3 3 3 4 4 4 1 1 4 4 4 4 4 4 4 4 4 4 4 2 2 2 2 2 2 2 2 2 3 3 3 2 5 1 1 4 4 4 4 4 4 4 4 4 4 4 2 2 4 4 5 5 5 5 5 5 5 5 5 2 2 2 2 2\r\n", "output": "33\r\n"}, {"input": "99 10 10\r\n9 9 9 10 10 10 9 9 9 9 9 2 2 10 10 10 10 10 3 3 5 10 10 2 2 3 3 6 1 1 1 1 1 1 7 7 7 7 7 4 4 6 6 6 8 9 9 9 2 2 9 9 5 5 5 5 1 10 7 7 9 9 9 5 6 6 6 6 8 8 4 1 3 3 3 3 3 3 9 9 4 1 1 7 1 1 1 3 3 3 3 3 3 10 9 10 9 8 9\r\n", "output": "12\r\n"}, {"input": "95 10 30\r\n3 3 8 8 8 4 9 3 3 3 3 3 3 8 10 5 5 5 5 5 5 4 9 1 1 1 1 6 6 7 7 7 1 1 1 1 1 1 9 9 10 10 10 10 10 5 3 3 3 3 3 3 6 6 6 6 1 6 6 6 6 9 4 9 5 5 5 2 2 2 2 10 10 8 3 3 4 2 9 9 9 2 5 2 2 8 8 8 7 7 3 3 3 4 4\r\n", "output": "11\r\n"}, {"input": "100 10 15\r\n7 7 3 6 6 6 8 8 8 8 8 8 8 8 8 8 8 5 5 1 9 9 9 9 9 9 9 9 2 2 2 4 7 7 8 2 2 2 2 2 2 8 8 7 7 2 2 2 7 7 7 4 4 4 4 4 4 4 4 4 4 7 7 7 7 7 7 7 7 2 2 2 6 6 3 3 3 3 3 3 1 1 1 1 1 1 4 4 4 4 1 1 1 1 5 4 5 6 6 6\r\n", "output": "13\r\n"}]
false
stdio
null
true
18/D
18
D
PyPy 3-64
TESTS
24
280
5,222,400
208301258
from collections import defaultdict n = int(input()) dp = defaultdict(int) dp[0] = 0 # mx = 0 for _ in range(n): cmd, x = input().split() x = int(x) if cmd == 'win': dp[x] = dp[0] elif x in dp: dp[0] = max(dp[0], dp[x] + 2 ** x) print(dp[0])
45
154
5,529,600
28627387
R= lambda: map(int,input().split()) n,=R() ans=0 l= [0 for _ in range(2002)] for i in range(n): s=input().split() b=int(s[1]) if s[0]=='win': l[b]=ans+2**b else: ans=max(ans,l[b]) print(ans)
Codeforces Beta Round 18 (Div. 2 Only)
ICPC
2,010
2
128
Seller Bob
Last year Bob earned by selling memory sticks. During each of n days of his work one of the two following events took place: - A customer came to Bob and asked to sell him a 2x MB memory stick. If Bob had such a stick, he sold it and got 2x berllars. - Bob won some programming competition and got a 2x MB memory stick as a prize. Bob could choose whether to present this memory stick to one of his friends, or keep it. Bob never kept more than one memory stick, as he feared to mix up their capacities, and deceive a customer unintentionally. It is also known that for each memory stick capacity there was at most one customer, who wanted to buy that memory stick. Now, knowing all the customers' demands and all the prizes won at programming competitions during the last n days, Bob wants to know, how much money he could have earned, if he had acted optimally.
The first input line contains number n (1 ≤ n ≤ 5000) — amount of Bob's working days. The following n lines contain the description of the days. Line sell x stands for a day when a customer came to Bob to buy a 2x MB memory stick (0 ≤ x ≤ 2000). It's guaranteed that for each x there is not more than one line sell x. Line win x stands for a day when Bob won a 2x MB memory stick (0 ≤ x ≤ 2000).
Output the maximum possible earnings for Bob in berllars, that he would have had if he had known all the events beforehand. Don't forget, please, that Bob can't keep more than one memory stick at a time.
null
null
[{"input": "7\nwin 10\nwin 5\nwin 3\nsell 5\nsell 3\nwin 10\nsell 10", "output": "1056"}, {"input": "3\nwin 5\nsell 6\nsell 4", "output": "0"}]
2,000
["brute force", "dp", "greedy"]
45
[{"input": "7\r\nwin 10\r\nwin 5\r\nwin 3\r\nsell 5\r\nsell 3\r\nwin 10\r\nsell 10\r\n", "output": "1056\r\n"}, {"input": "3\r\nwin 5\r\nsell 6\r\nsell 4\r\n", "output": "0\r\n"}, {"input": "10\r\nsell 179\r\nwin 1278\r\nsell 1278\r\nwin 179\r\nwin 788\r\nsell 788\r\nwin 1819\r\nwin 1278\r\nsell 1454\r\nsell 1819\r\n", "output": "37459511778596727480858760720167552241582636504705413766024169777495064333423937410125519624693990051069809575647477719465460756326341562228323606665869931977125977431028709943048934214062888966581139223580790503937962827597404798307897711090567429316074325427043388117806141094834711707585035634104732053207574452493593409130554278913951011894497392495930884827685983975668127973918422057605356890341647839399778378381152159725053311750647457999739578989105335906181048932656785993705124392163591312698147450544686667028530553878359909362604965888\n"}, {"input": "10\r\nsell 573\r\nwin 1304\r\nsell 278\r\nwin 1631\r\nsell 1225\r\nsell 1631\r\nsell 177\r\nwin 1631\r\nwin 177\r\nsell 1304\r\n", "output": "95482312335125227379668481690754940528280513838693267460502082967052005332103697568042408703168913727303170456338425853153094403747135188778307041838920404959089576368946137708987138986696495077466398994298434148881715073638178666201165545650953479735059082316661443204882826188032944866093372620219104327689636641547141835841165681118172603993695103043804276669836594061369229043451067647935298287687852302215923887110435577776767805943668204998410716005202198549540411238299513630278811648\n"}, {"input": "10\r\nwin 1257\r\nwin 1934\r\nsell 1934\r\nsell 1257\r\nwin 1934\r\nwin 1257\r\nsell 495\r\nwin 495\r\nwin 495\r\nwin 1257\r\n", "output": "1556007242642049292787218246793379348327505438878680952714050868520307364441227819009733220897932984584977593931988662671459594674963394056587723382487766303981362587048873128400436836690128983570130687310221668877557121158055843621982630476422478413285775826498536883275291967793661985813155062733063913176306327509625594121241472451054995889483447103432414676059872469910105149496451402271546454282618581884282152530090816240540173251729211604658704990425330422792556824836640431985211146197816770068601144273155826261981248156109439380758754562057681271989937590784957541131485184\n"}, {"input": "10\r\nsell 1898\r\nsell 173\r\nsell 1635\r\nsell 29\r\nsell 881\r\nsell 434\r\nsell 1236\r\nsell 14\r\nwin 29\r\nsell 1165\r\n", "output": "0\r\n"}, {"input": "1\r\nsell 2000\r\n", "output": "0\r\n"}, {"input": "1\r\nwin 2000\r\n", "output": "0\r\n"}, {"input": "2\r\nwin 2000\r\nsell 2000\r\n", "output": "114813069527425452423283320117768198402231770208869520047764273682576626139237031385665948631650626991844596463898746277344711896086305533142593135616665318539129989145312280000688779148240044871428926990063486244781615463646388363947317026040466353970904996558162398808944629605623311649536164221970332681344168908984458505602379484807914058900934776500429002716706625830522008132236281291761267883317206598995396418127021779858404042159853183251540889433902091920554957783589672039160081957216630582755380425583726015528348786419432054508915275783882625175435528800822842770817965453762184851149029376\n"}]
false
stdio
null
true
18/D
18
D
Python 3
TESTS
24
248
409,600
51790433
N = int(input()) winsell = (N)*[0] pow = (2001)*[-1]; ans = int(0) for i in range(0,N): S, x = input().split() x = int(x) if S == "win": winsell[i] = x else: pow[x] = i; winsell[i] = -x for i in range(2000,-1,-1): if pow[i]!=-1: b = bool(0) for j in range(pow[i],-1,-1): if winsell[j] != 2001: if winsell[j] == i: b = 1 ans+=2**i for k in range(j,pow[i]+1): winsell[k]=2001 else: b = 1 if b == 1: break print(ans) # Sun Mar 24 2019 15:34:27 GMT+0300 (MSK)
45
154
7,270,400
125293934
import sys readline = sys.stdin.readline N = int(readline()) DP = [0] * (N+1) MAX = 2100 last_t = [0] * MAX for i in range(1, N + 1): DP[i] = DP[i-1] a, b = readline().split() b = int(b) if(a == "win"): DP[i] = DP[i-1] last_t[b] = i else: if last_t[b] == 0: continue DP[i] = max(DP[i], DP[last_t[b]] + (1 << b)) print(DP[-1])
Codeforces Beta Round 18 (Div. 2 Only)
ICPC
2,010
2
128
Seller Bob
Last year Bob earned by selling memory sticks. During each of n days of his work one of the two following events took place: - A customer came to Bob and asked to sell him a 2x MB memory stick. If Bob had such a stick, he sold it and got 2x berllars. - Bob won some programming competition and got a 2x MB memory stick as a prize. Bob could choose whether to present this memory stick to one of his friends, or keep it. Bob never kept more than one memory stick, as he feared to mix up their capacities, and deceive a customer unintentionally. It is also known that for each memory stick capacity there was at most one customer, who wanted to buy that memory stick. Now, knowing all the customers' demands and all the prizes won at programming competitions during the last n days, Bob wants to know, how much money he could have earned, if he had acted optimally.
The first input line contains number n (1 ≤ n ≤ 5000) — amount of Bob's working days. The following n lines contain the description of the days. Line sell x stands for a day when a customer came to Bob to buy a 2x MB memory stick (0 ≤ x ≤ 2000). It's guaranteed that for each x there is not more than one line sell x. Line win x stands for a day when Bob won a 2x MB memory stick (0 ≤ x ≤ 2000).
Output the maximum possible earnings for Bob in berllars, that he would have had if he had known all the events beforehand. Don't forget, please, that Bob can't keep more than one memory stick at a time.
null
null
[{"input": "7\nwin 10\nwin 5\nwin 3\nsell 5\nsell 3\nwin 10\nsell 10", "output": "1056"}, {"input": "3\nwin 5\nsell 6\nsell 4", "output": "0"}]
2,000
["brute force", "dp", "greedy"]
45
[{"input": "7\r\nwin 10\r\nwin 5\r\nwin 3\r\nsell 5\r\nsell 3\r\nwin 10\r\nsell 10\r\n", "output": "1056\r\n"}, {"input": "3\r\nwin 5\r\nsell 6\r\nsell 4\r\n", "output": "0\r\n"}, {"input": "10\r\nsell 179\r\nwin 1278\r\nsell 1278\r\nwin 179\r\nwin 788\r\nsell 788\r\nwin 1819\r\nwin 1278\r\nsell 1454\r\nsell 1819\r\n", "output": "37459511778596727480858760720167552241582636504705413766024169777495064333423937410125519624693990051069809575647477719465460756326341562228323606665869931977125977431028709943048934214062888966581139223580790503937962827597404798307897711090567429316074325427043388117806141094834711707585035634104732053207574452493593409130554278913951011894497392495930884827685983975668127973918422057605356890341647839399778378381152159725053311750647457999739578989105335906181048932656785993705124392163591312698147450544686667028530553878359909362604965888\n"}, {"input": "10\r\nsell 573\r\nwin 1304\r\nsell 278\r\nwin 1631\r\nsell 1225\r\nsell 1631\r\nsell 177\r\nwin 1631\r\nwin 177\r\nsell 1304\r\n", "output": "95482312335125227379668481690754940528280513838693267460502082967052005332103697568042408703168913727303170456338425853153094403747135188778307041838920404959089576368946137708987138986696495077466398994298434148881715073638178666201165545650953479735059082316661443204882826188032944866093372620219104327689636641547141835841165681118172603993695103043804276669836594061369229043451067647935298287687852302215923887110435577776767805943668204998410716005202198549540411238299513630278811648\n"}, {"input": "10\r\nwin 1257\r\nwin 1934\r\nsell 1934\r\nsell 1257\r\nwin 1934\r\nwin 1257\r\nsell 495\r\nwin 495\r\nwin 495\r\nwin 1257\r\n", "output": "1556007242642049292787218246793379348327505438878680952714050868520307364441227819009733220897932984584977593931988662671459594674963394056587723382487766303981362587048873128400436836690128983570130687310221668877557121158055843621982630476422478413285775826498536883275291967793661985813155062733063913176306327509625594121241472451054995889483447103432414676059872469910105149496451402271546454282618581884282152530090816240540173251729211604658704990425330422792556824836640431985211146197816770068601144273155826261981248156109439380758754562057681271989937590784957541131485184\n"}, {"input": "10\r\nsell 1898\r\nsell 173\r\nsell 1635\r\nsell 29\r\nsell 881\r\nsell 434\r\nsell 1236\r\nsell 14\r\nwin 29\r\nsell 1165\r\n", "output": "0\r\n"}, {"input": "1\r\nsell 2000\r\n", "output": "0\r\n"}, {"input": "1\r\nwin 2000\r\n", "output": "0\r\n"}, {"input": "2\r\nwin 2000\r\nsell 2000\r\n", "output": "114813069527425452423283320117768198402231770208869520047764273682576626139237031385665948631650626991844596463898746277344711896086305533142593135616665318539129989145312280000688779148240044871428926990063486244781615463646388363947317026040466353970904996558162398808944629605623311649536164221970332681344168908984458505602379484807914058900934776500429002716706625830522008132236281291761267883317206598995396418127021779858404042159853183251540889433902091920554957783589672039160081957216630582755380425583726015528348786419432054508915275783882625175435528800822842770817965453762184851149029376\n"}]
false
stdio
null
true
18/D
18
D
Python 3
TESTS
24
248
409,600
73944672
import math n = int(input()) Size = 2001 dp = [0] * (Size) for i in range(0, Size, 1) : dp[i] = -1 for i in range (1 , n+1 , 1): s = input() In = s.split() x = int(In[1]) if In[0] == "win" : dp[x] = max(0, dp[0]) if In[0] == "sell" : if dp[x] != -1 : dp[0] = max(dp[0], dp[x] + 2**x) ans = 0 for i in range(0, Size, 1) : ans = max(ans, dp[i]) print(ans) # Sun Mar 22 2020 11:44:23 GMT+0300 (MSK)
45
154
7,270,400
129517975
n = int(input()) have = [ -1 for i in range(0, 2005) ] dp = [ 0 for i in range(0, n+5) ] s, t = input().split() t = int(t) if(s == 'win'): have[t] = 0 for i in range(1, n): s, t = input().split() t = int(t) if(s == 'win'): have[t] = i dp[i] = dp[i-1] elif have[t] == -1: dp[i] = dp[i-1] else: dp[i] = max(dp[i-1], (1 << t) + dp[have[t]]) print(dp[n-1])
Codeforces Beta Round 18 (Div. 2 Only)
ICPC
2,010
2
128
Seller Bob
Last year Bob earned by selling memory sticks. During each of n days of his work one of the two following events took place: - A customer came to Bob and asked to sell him a 2x MB memory stick. If Bob had such a stick, he sold it and got 2x berllars. - Bob won some programming competition and got a 2x MB memory stick as a prize. Bob could choose whether to present this memory stick to one of his friends, or keep it. Bob never kept more than one memory stick, as he feared to mix up their capacities, and deceive a customer unintentionally. It is also known that for each memory stick capacity there was at most one customer, who wanted to buy that memory stick. Now, knowing all the customers' demands and all the prizes won at programming competitions during the last n days, Bob wants to know, how much money he could have earned, if he had acted optimally.
The first input line contains number n (1 ≤ n ≤ 5000) — amount of Bob's working days. The following n lines contain the description of the days. Line sell x stands for a day when a customer came to Bob to buy a 2x MB memory stick (0 ≤ x ≤ 2000). It's guaranteed that for each x there is not more than one line sell x. Line win x stands for a day when Bob won a 2x MB memory stick (0 ≤ x ≤ 2000).
Output the maximum possible earnings for Bob in berllars, that he would have had if he had known all the events beforehand. Don't forget, please, that Bob can't keep more than one memory stick at a time.
null
null
[{"input": "7\nwin 10\nwin 5\nwin 3\nsell 5\nsell 3\nwin 10\nsell 10", "output": "1056"}, {"input": "3\nwin 5\nsell 6\nsell 4", "output": "0"}]
2,000
["brute force", "dp", "greedy"]
45
[{"input": "7\r\nwin 10\r\nwin 5\r\nwin 3\r\nsell 5\r\nsell 3\r\nwin 10\r\nsell 10\r\n", "output": "1056\r\n"}, {"input": "3\r\nwin 5\r\nsell 6\r\nsell 4\r\n", "output": "0\r\n"}, {"input": "10\r\nsell 179\r\nwin 1278\r\nsell 1278\r\nwin 179\r\nwin 788\r\nsell 788\r\nwin 1819\r\nwin 1278\r\nsell 1454\r\nsell 1819\r\n", "output": "37459511778596727480858760720167552241582636504705413766024169777495064333423937410125519624693990051069809575647477719465460756326341562228323606665869931977125977431028709943048934214062888966581139223580790503937962827597404798307897711090567429316074325427043388117806141094834711707585035634104732053207574452493593409130554278913951011894497392495930884827685983975668127973918422057605356890341647839399778378381152159725053311750647457999739578989105335906181048932656785993705124392163591312698147450544686667028530553878359909362604965888\n"}, {"input": "10\r\nsell 573\r\nwin 1304\r\nsell 278\r\nwin 1631\r\nsell 1225\r\nsell 1631\r\nsell 177\r\nwin 1631\r\nwin 177\r\nsell 1304\r\n", "output": "95482312335125227379668481690754940528280513838693267460502082967052005332103697568042408703168913727303170456338425853153094403747135188778307041838920404959089576368946137708987138986696495077466398994298434148881715073638178666201165545650953479735059082316661443204882826188032944866093372620219104327689636641547141835841165681118172603993695103043804276669836594061369229043451067647935298287687852302215923887110435577776767805943668204998410716005202198549540411238299513630278811648\n"}, {"input": "10\r\nwin 1257\r\nwin 1934\r\nsell 1934\r\nsell 1257\r\nwin 1934\r\nwin 1257\r\nsell 495\r\nwin 495\r\nwin 495\r\nwin 1257\r\n", "output": "1556007242642049292787218246793379348327505438878680952714050868520307364441227819009733220897932984584977593931988662671459594674963394056587723382487766303981362587048873128400436836690128983570130687310221668877557121158055843621982630476422478413285775826498536883275291967793661985813155062733063913176306327509625594121241472451054995889483447103432414676059872469910105149496451402271546454282618581884282152530090816240540173251729211604658704990425330422792556824836640431985211146197816770068601144273155826261981248156109439380758754562057681271989937590784957541131485184\n"}, {"input": "10\r\nsell 1898\r\nsell 173\r\nsell 1635\r\nsell 29\r\nsell 881\r\nsell 434\r\nsell 1236\r\nsell 14\r\nwin 29\r\nsell 1165\r\n", "output": "0\r\n"}, {"input": "1\r\nsell 2000\r\n", "output": "0\r\n"}, {"input": "1\r\nwin 2000\r\n", "output": "0\r\n"}, {"input": "2\r\nwin 2000\r\nsell 2000\r\n", "output": "114813069527425452423283320117768198402231770208869520047764273682576626139237031385665948631650626991844596463898746277344711896086305533142593135616665318539129989145312280000688779148240044871428926990063486244781615463646388363947317026040466353970904996558162398808944629605623311649536164221970332681344168908984458505602379484807914058900934776500429002716706625830522008132236281291761267883317206598995396418127021779858404042159853183251540889433902091920554957783589672039160081957216630582755380425583726015528348786419432054508915275783882625175435528800822842770817965453762184851149029376\n"}]
false
stdio
null
true
18/D
18
D
PyPy 3-64
TESTS
22
280
5,836,800
205942811
N,X=5000,2000 lows,upps,viss=[-1]*X,[-1]*X,[0]*N n=int(input()) for i in range(n): t,x=input().split() x=int(x)-1 if t=="win" and upps[x]==-1:lows[x]=i if t=="sell":upps[x]=i ress=[] for i in range(X)[::-1]: l,r=lows[i],upps[i] if l==-1 or r==-1 or l>r:continue vis=0 for j in range(l,r+1):vis|=viss[j] if vis:continue for j in range(l,r+1):viss[j]=1 ress.append(i+1) res,fac,cur=0,1,0 for x in ress[::-1]: while cur<x:fac*=2;cur+=1 res+=fac print(res)
45
154
7,680,000
127262195
# -*- coding: utf-8 -*- """ Created on Sat Aug 28 08:49:22 2021 @author: Hermes """ n = int(input()) m = [0]*2001 earnings = 0 for i in range(n): e = input() if 'win' in e: x = int(e[4:]) if x > 0: m[x] = earnings + (2<<x-1) else: m[x] = earnings + 1 else: x = int(e[5:]) earnings = max(earnings, m[x]) print(earnings)
Codeforces Beta Round 18 (Div. 2 Only)
ICPC
2,010
2
128
Seller Bob
Last year Bob earned by selling memory sticks. During each of n days of his work one of the two following events took place: - A customer came to Bob and asked to sell him a 2x MB memory stick. If Bob had such a stick, he sold it and got 2x berllars. - Bob won some programming competition and got a 2x MB memory stick as a prize. Bob could choose whether to present this memory stick to one of his friends, or keep it. Bob never kept more than one memory stick, as he feared to mix up their capacities, and deceive a customer unintentionally. It is also known that for each memory stick capacity there was at most one customer, who wanted to buy that memory stick. Now, knowing all the customers' demands and all the prizes won at programming competitions during the last n days, Bob wants to know, how much money he could have earned, if he had acted optimally.
The first input line contains number n (1 ≤ n ≤ 5000) — amount of Bob's working days. The following n lines contain the description of the days. Line sell x stands for a day when a customer came to Bob to buy a 2x MB memory stick (0 ≤ x ≤ 2000). It's guaranteed that for each x there is not more than one line sell x. Line win x stands for a day when Bob won a 2x MB memory stick (0 ≤ x ≤ 2000).
Output the maximum possible earnings for Bob in berllars, that he would have had if he had known all the events beforehand. Don't forget, please, that Bob can't keep more than one memory stick at a time.
null
null
[{"input": "7\nwin 10\nwin 5\nwin 3\nsell 5\nsell 3\nwin 10\nsell 10", "output": "1056"}, {"input": "3\nwin 5\nsell 6\nsell 4", "output": "0"}]
2,000
["brute force", "dp", "greedy"]
45
[{"input": "7\r\nwin 10\r\nwin 5\r\nwin 3\r\nsell 5\r\nsell 3\r\nwin 10\r\nsell 10\r\n", "output": "1056\r\n"}, {"input": "3\r\nwin 5\r\nsell 6\r\nsell 4\r\n", "output": "0\r\n"}, {"input": "10\r\nsell 179\r\nwin 1278\r\nsell 1278\r\nwin 179\r\nwin 788\r\nsell 788\r\nwin 1819\r\nwin 1278\r\nsell 1454\r\nsell 1819\r\n", "output": "37459511778596727480858760720167552241582636504705413766024169777495064333423937410125519624693990051069809575647477719465460756326341562228323606665869931977125977431028709943048934214062888966581139223580790503937962827597404798307897711090567429316074325427043388117806141094834711707585035634104732053207574452493593409130554278913951011894497392495930884827685983975668127973918422057605356890341647839399778378381152159725053311750647457999739578989105335906181048932656785993705124392163591312698147450544686667028530553878359909362604965888\n"}, {"input": "10\r\nsell 573\r\nwin 1304\r\nsell 278\r\nwin 1631\r\nsell 1225\r\nsell 1631\r\nsell 177\r\nwin 1631\r\nwin 177\r\nsell 1304\r\n", "output": "95482312335125227379668481690754940528280513838693267460502082967052005332103697568042408703168913727303170456338425853153094403747135188778307041838920404959089576368946137708987138986696495077466398994298434148881715073638178666201165545650953479735059082316661443204882826188032944866093372620219104327689636641547141835841165681118172603993695103043804276669836594061369229043451067647935298287687852302215923887110435577776767805943668204998410716005202198549540411238299513630278811648\n"}, {"input": "10\r\nwin 1257\r\nwin 1934\r\nsell 1934\r\nsell 1257\r\nwin 1934\r\nwin 1257\r\nsell 495\r\nwin 495\r\nwin 495\r\nwin 1257\r\n", "output": "1556007242642049292787218246793379348327505438878680952714050868520307364441227819009733220897932984584977593931988662671459594674963394056587723382487766303981362587048873128400436836690128983570130687310221668877557121158055843621982630476422478413285775826498536883275291967793661985813155062733063913176306327509625594121241472451054995889483447103432414676059872469910105149496451402271546454282618581884282152530090816240540173251729211604658704990425330422792556824836640431985211146197816770068601144273155826261981248156109439380758754562057681271989937590784957541131485184\n"}, {"input": "10\r\nsell 1898\r\nsell 173\r\nsell 1635\r\nsell 29\r\nsell 881\r\nsell 434\r\nsell 1236\r\nsell 14\r\nwin 29\r\nsell 1165\r\n", "output": "0\r\n"}, {"input": "1\r\nsell 2000\r\n", "output": "0\r\n"}, {"input": "1\r\nwin 2000\r\n", "output": "0\r\n"}, {"input": "2\r\nwin 2000\r\nsell 2000\r\n", "output": "114813069527425452423283320117768198402231770208869520047764273682576626139237031385665948631650626991844596463898746277344711896086305533142593135616665318539129989145312280000688779148240044871428926990063486244781615463646388363947317026040466353970904996558162398808944629605623311649536164221970332681344168908984458505602379484807914058900934776500429002716706625830522008132236281291761267883317206598995396418127021779858404042159853183251540889433902091920554957783589672039160081957216630582755380425583726015528348786419432054508915275783882625175435528800822842770817965453762184851149029376\n"}]
false
stdio
null
true
18/D
18
D
Python 3
TESTS
22
156
5,120,000
25694477
n = int(input()) mx = 2009 d = [0 for i in range(mx)] for i in range(1,n + 1): s = input().split() x = int(s[1]) s = s[0] if s == 'win': d[x] =d[0]+ 2**x else: d[0] = max(d[x], d[0]) print(d[0])
45
156
307,200
4977843
a = [-1] * 2002 a[0] = 0 for i in range(int(input())): s, t= input().split() t = int(t) + 1 #t = int(input()) if(s[0] == 'w'): a[t] = a[0] elif a[t] >= 0: a[0] = max(a[0], a[t] + (1<<(t-1))) print(a[0])
Codeforces Beta Round 18 (Div. 2 Only)
ICPC
2,010
2
128
Seller Bob
Last year Bob earned by selling memory sticks. During each of n days of his work one of the two following events took place: - A customer came to Bob and asked to sell him a 2x MB memory stick. If Bob had such a stick, he sold it and got 2x berllars. - Bob won some programming competition and got a 2x MB memory stick as a prize. Bob could choose whether to present this memory stick to one of his friends, or keep it. Bob never kept more than one memory stick, as he feared to mix up their capacities, and deceive a customer unintentionally. It is also known that for each memory stick capacity there was at most one customer, who wanted to buy that memory stick. Now, knowing all the customers' demands and all the prizes won at programming competitions during the last n days, Bob wants to know, how much money he could have earned, if he had acted optimally.
The first input line contains number n (1 ≤ n ≤ 5000) — amount of Bob's working days. The following n lines contain the description of the days. Line sell x stands for a day when a customer came to Bob to buy a 2x MB memory stick (0 ≤ x ≤ 2000). It's guaranteed that for each x there is not more than one line sell x. Line win x stands for a day when Bob won a 2x MB memory stick (0 ≤ x ≤ 2000).
Output the maximum possible earnings for Bob in berllars, that he would have had if he had known all the events beforehand. Don't forget, please, that Bob can't keep more than one memory stick at a time.
null
null
[{"input": "7\nwin 10\nwin 5\nwin 3\nsell 5\nsell 3\nwin 10\nsell 10", "output": "1056"}, {"input": "3\nwin 5\nsell 6\nsell 4", "output": "0"}]
2,000
["brute force", "dp", "greedy"]
45
[{"input": "7\r\nwin 10\r\nwin 5\r\nwin 3\r\nsell 5\r\nsell 3\r\nwin 10\r\nsell 10\r\n", "output": "1056\r\n"}, {"input": "3\r\nwin 5\r\nsell 6\r\nsell 4\r\n", "output": "0\r\n"}, {"input": "10\r\nsell 179\r\nwin 1278\r\nsell 1278\r\nwin 179\r\nwin 788\r\nsell 788\r\nwin 1819\r\nwin 1278\r\nsell 1454\r\nsell 1819\r\n", "output": "37459511778596727480858760720167552241582636504705413766024169777495064333423937410125519624693990051069809575647477719465460756326341562228323606665869931977125977431028709943048934214062888966581139223580790503937962827597404798307897711090567429316074325427043388117806141094834711707585035634104732053207574452493593409130554278913951011894497392495930884827685983975668127973918422057605356890341647839399778378381152159725053311750647457999739578989105335906181048932656785993705124392163591312698147450544686667028530553878359909362604965888\n"}, {"input": "10\r\nsell 573\r\nwin 1304\r\nsell 278\r\nwin 1631\r\nsell 1225\r\nsell 1631\r\nsell 177\r\nwin 1631\r\nwin 177\r\nsell 1304\r\n", "output": "95482312335125227379668481690754940528280513838693267460502082967052005332103697568042408703168913727303170456338425853153094403747135188778307041838920404959089576368946137708987138986696495077466398994298434148881715073638178666201165545650953479735059082316661443204882826188032944866093372620219104327689636641547141835841165681118172603993695103043804276669836594061369229043451067647935298287687852302215923887110435577776767805943668204998410716005202198549540411238299513630278811648\n"}, {"input": "10\r\nwin 1257\r\nwin 1934\r\nsell 1934\r\nsell 1257\r\nwin 1934\r\nwin 1257\r\nsell 495\r\nwin 495\r\nwin 495\r\nwin 1257\r\n", "output": "1556007242642049292787218246793379348327505438878680952714050868520307364441227819009733220897932984584977593931988662671459594674963394056587723382487766303981362587048873128400436836690128983570130687310221668877557121158055843621982630476422478413285775826498536883275291967793661985813155062733063913176306327509625594121241472451054995889483447103432414676059872469910105149496451402271546454282618581884282152530090816240540173251729211604658704990425330422792556824836640431985211146197816770068601144273155826261981248156109439380758754562057681271989937590784957541131485184\n"}, {"input": "10\r\nsell 1898\r\nsell 173\r\nsell 1635\r\nsell 29\r\nsell 881\r\nsell 434\r\nsell 1236\r\nsell 14\r\nwin 29\r\nsell 1165\r\n", "output": "0\r\n"}, {"input": "1\r\nsell 2000\r\n", "output": "0\r\n"}, {"input": "1\r\nwin 2000\r\n", "output": "0\r\n"}, {"input": "2\r\nwin 2000\r\nsell 2000\r\n", "output": "114813069527425452423283320117768198402231770208869520047764273682576626139237031385665948631650626991844596463898746277344711896086305533142593135616665318539129989145312280000688779148240044871428926990063486244781615463646388363947317026040466353970904996558162398808944629605623311649536164221970332681344168908984458505602379484807914058900934776500429002716706625830522008132236281291761267883317206598995396418127021779858404042159853183251540889433902091920554957783589672039160081957216630582755380425583726015528348786419432054508915275783882625175435528800822842770817965453762184851149029376\n"}]
false
stdio
null
true
606/B
606
B
PyPy 3
TESTS
3
109
23,040,000
19664116
x, y, x0, y0=list(map(int, input().split())) s=input() c=1 print(1, end=' ') for i in range(len(s)-1): if s[i] =='L': if y0>=2: y0-=1; print(1, end=' '); c+=1 else: print(0, end=' ') if s[i] =='R': if y0<y: y0+=1; print(1, end=' '); c+=1 else: print(0, end=' ') if s[i] =='U': if x0>=2: x0-=1; print(1, end=' '); c+=1 else: print(0, end=' ') if s[i] =='D': if x0<x: x0+=1; print(1, end=' '); c+=1 else: print(0, end=' ') print(x*y-c)
68
187
5,939,200
14738900
x, y, x0, y0 = map(int, input().split(' ')) g = [[0]* (y+1) for i in range(x + 1)] s = input() result = [0] * len(s) count = x*y for i in range(len(s)): if g[x0][y0] == 0: g[x0][y0] = 1 result[i] = 1 count -= 1 if s[i] == 'U' and x0 > 1: x0 -=1 if s[i] == 'D' and x0 < x: x0 += 1 if s[i] == 'L' and y0 > 1: y0 -= 1 if s[i] == 'R' and y0 < y: y0 += 1 print(' '.join(map(str, result)), count)
Codeforces Round 335 (Div. 2)
CF
2,015
2
256
Testing Robots
The Cybernetics Failures (CF) organisation made a prototype of a bomb technician robot. To find the possible problems it was decided to carry out a series of tests. At the beginning of each test the robot prototype will be placed in cell (x0, y0) of a rectangular squared field of size x × y, after that a mine will be installed into one of the squares of the field. It is supposed to conduct exactly x·y tests, each time a mine is installed into a square that has never been used before. The starting cell of the robot always remains the same. After placing the objects on the field the robot will have to run a sequence of commands given by string s, consisting only of characters 'L', 'R', 'U', 'D'. These commands tell the robot to move one square to the left, to the right, up or down, or stay idle if moving in the given direction is impossible. As soon as the robot fulfills all the sequence of commands, it will blow up due to a bug in the code. But if at some moment of time the robot is at the same square with the mine, it will also blow up, but not due to a bug in the code. Moving to the left decreases coordinate y, and moving to the right increases it. Similarly, moving up decreases the x coordinate, and moving down increases it. The tests can go on for very long, so your task is to predict their results. For each k from 0 to length(s) your task is to find in how many tests the robot will run exactly k commands before it blows up.
The first line of the input contains four integers x, y, x0, y0 (1 ≤ x, y ≤ 500, 1 ≤ x0 ≤ x, 1 ≤ y0 ≤ y) — the sizes of the field and the starting coordinates of the robot. The coordinate axis X is directed downwards and axis Y is directed to the right. The second line contains a sequence of commands s, which should be fulfilled by the robot. It has length from 1 to 100 000 characters and only consists of characters 'L', 'R', 'U', 'D'.
Print the sequence consisting of (length(s) + 1) numbers. On the k-th position, starting with zero, print the number of tests where the robot will run exactly k commands before it blows up.
null
In the first sample, if we exclude the probable impact of the mines, the robot's route will look like that: $$(2,2)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(2,4)\rightarrow(3,3)$$.
[{"input": "3 4 2 2\nUURDRDRL", "output": "1 1 0 1 1 1 1 0 6"}, {"input": "2 2 2 2\nULD", "output": "1 1 1 1"}]
1,600
["implementation"]
68
[{"input": "3 4 2 2\r\nUURDRDRL\r\n", "output": "1 1 0 1 1 1 1 0 6\r\n"}, {"input": "2 2 2 2\r\nULD\r\n", "output": "1 1 1 1\r\n"}, {"input": "1 1 1 1\r\nURDLUURRDDLLURDL\r\n", "output": "1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDD\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 245\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDDRRUR\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 241\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDDRRURR\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 0 241\r\n"}, {"input": "1 2 1 1\r\nR\r\n", "output": "1 1\r\n"}, {"input": "2 1 1 1\r\nD\r\n", "output": "1 1\r\n"}, {"input": "1 2 1 2\r\nLR\r\n", "output": "1 1 0\r\n"}, {"input": "2 1 2 1\r\nUD\r\n", "output": "1 1 0\r\n"}, {"input": "4 4 2 2\r\nDRUL\r\n", "output": "1 1 1 1 12\r\n"}, {"input": "4 4 3 3\r\nLUDRUL\r\n", "output": "1 1 1 0 0 1 12\r\n"}, {"input": "15 17 8 9\r\nURRDLU\r\n", "output": "1 1 1 1 1 1 249\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDR\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 244\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDRR\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 0 244\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDRRR\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 0 0 244\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDRRRR\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 0 0 0 244\r\n"}, {"input": "15 17 8 9\r\nURRDLUULLDDRRRRU\r\n", "output": "1 1 1 1 1 1 0 1 1 1 1 1 0 0 0 1 243\r\n"}]
false
stdio
null
true
180/E
180
E
PyPy 3
TESTS
5
280
0
67479780
n, m, k = map(int, input().split()) L = list(map(int, input().split())) cnt = [0] * (m+1) dp = [1] * n bg = 0 for idx in range(len(L)): cnt[L[idx]] += 1 dp[bg] = cnt[L[bg]] if len(L[0:idx + 1]) - cnt[L[bg]] > k: dp[bg] = cnt[L[bg]] cnt[L[bg]] -= 1 bg += 1 print(max(dp))
50
498
17,305,600
51319385
R = lambda: map(int, input().split()) n, m, k = R() arr = list(R()) arrs = [list() for i in range(m)] for i, x in enumerate(arr): arrs[x - 1].append(i) res = 1 for ar in arrs: l = -1 for r in range(len(ar)): while r - l + k < ar[r] - ar[l + 1] + 1: l += 1 res = max(res, r - l) print(res)
Codeforces Round 116 (Div. 2, ACM-ICPC Rules)
ICPC
2,012
1
256
Cubes
Let's imagine that you're playing the following simple computer game. The screen displays n lined-up cubes. Each cube is painted one of m colors. You are allowed to delete not more than k cubes (that do not necessarily go one after another). After that, the remaining cubes join together (so that the gaps are closed) and the system counts the score. The number of points you score equals to the length of the maximum sequence of cubes of the same color that follow consecutively. Write a program that determines the maximum possible number of points you can score. Remember, you may delete no more than k any cubes. It is allowed not to delete cubes at all.
The first line contains three integers n, m and k (1 ≤ n ≤ 2·105, 1 ≤ m ≤ 105, 0 ≤ k < n). The second line contains n integers from 1 to m — the numbers of cube colors. The numbers of colors are separated by single spaces.
Print the maximum possible number of points you can score.
null
In the first sample you should delete the fifth and the sixth cubes. In the second sample you should delete the fourth and the seventh cubes. In the third sample you shouldn't delete any cubes.
[{"input": "10 3 2\n1 2 1 1 3 2 1 1 2 2", "output": "4"}, {"input": "10 2 2\n1 2 1 2 1 1 2 1 1 2", "output": "5"}, {"input": "3 1 2\n1 1 1", "output": "3"}]
1,800
["binary search", "dp", "two pointers"]
50
[{"input": "10 3 2\r\n1 2 1 1 3 2 1 1 2 2\r\n", "output": "4\r\n"}, {"input": "10 2 2\r\n1 2 1 2 1 1 2 1 1 2\r\n", "output": "5\r\n"}, {"input": "3 1 2\r\n1 1 1\r\n", "output": "3\r\n"}, {"input": "10 2 2\r\n1 1 1 2 1 2 1 2 1 1\r\n", "output": "5\r\n"}, {"input": "1 1 0\r\n1\r\n", "output": "1\r\n"}, {"input": "20 3 5\r\n2 2 3 1 2 2 3 3 3 2 1 2 3 1 1 3 3 3 2 3\r\n", "output": "7\r\n"}, {"input": "20 2 5\r\n2 2 1 2 1 2 1 2 1 1 2 1 2 2 1 2 2 1 2 1\r\n", "output": "7\r\n"}, {"input": "20 6 3\r\n4 1 2 6 3 3 2 5 2 5 2 1 1 4 1 2 2 1 1 4\r\n", "output": "5\r\n"}, {"input": "30 5 8\r\n1 4 1 5 3 4 4 1 1 4 1 3 5 5 5 5 1 5 1 5 2 3 2 2 3 4 5 2 1 2\r\n", "output": "7\r\n"}, {"input": "30 5 6\r\n4 2 2 1 3 4 2 3 2 4 3 1 1 4 4 3 5 1 4 5 5 1 2 2 1 2 4 4 1 2\r\n", "output": "4\r\n"}, {"input": "100 10 15\r\n6 6 6 6 7 7 8 8 4 4 4 1 1 7 7 7 1 1 1 2 2 2 2 2 2 2 2 2 10 5 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 6 6 10 2 2 8 8 1 1 1 1 1 6 6 6 6 2 2 3 3 9 9 9 9 9 10 10 10 10 10 4 9 9 9 7 7 7 7 9 9 7 7 5 8 8 8 8 2\r\n", "output": "25\r\n"}, {"input": "99 10 17\r\n3 2 2 9 7 10 10 10 10 6 6 6 3 7 3 3 7 2 2 2 2 2 10 10 2 2 7 7 7 7 1 8 8 8 8 10 9 10 10 10 5 5 2 2 5 5 5 1 4 9 9 2 2 3 3 2 2 9 9 9 9 9 9 9 7 4 8 8 4 8 8 10 10 4 5 9 9 10 5 5 5 5 5 8 8 8 8 2 2 2 2 1 8 8 5 10 10 2 2\r\n", "output": "11\r\n"}, {"input": "94 10 20\r\n2 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 3 3 3 3 6 6 5 1 5 5 5 2 2 2 2 4 1 1 1 1 8 8 10 5 2 2 4 4 4 4 4 3 3 3 3 3 6 6 6 6 2 2 2 2 2 2 2 2 1 10 10 2 2 2 6 6 6 8 4 4 4 8 1 1 1 1 1 1 6 6 2 2 8 7 7 7 3 4\r\n", "output": "13\r\n"}, {"input": "99 3 15\r\n2 2 2 2 2 2 3 3 1 1 1 1 1 1 1 1 1 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 3 1 1 1 1 1 3 3 3 1 1 1 3 3 3 3 3 3 3 1 1 1 1 3 3 3 3 3 3 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 2 2 2 2 2 2\r\n", "output": "27\r\n"}, {"input": "100 5 10\r\n4 4 4 4 4 4 4 4 4 4 4 4 2 4 4 4 4 4 4 4 4 4 2 2 2 3 3 3 3 3 3 3 4 4 4 3 3 2 1 1 1 2 3 3 5 5 5 5 5 5 5 5 5 5 4 4 4 4 4 4 4 4 4 2 2 2 2 2 2 2 4 4 4 4 5 4 4 4 4 4 4 4 3 3 3 3 3 3 3 3 3 5 5 5 3 3 4 3 3 3\r\n", "output": "21\r\n"}, {"input": "98 4 20\r\n3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 1 1 1 1 1 1 1 1 1 1 1 1 4 4 4 4 4 4 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 2 2 2 2 2 2 2 1 1 1 3 3 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 2 2 2 2 2 2 2 3 3 1 1 2 2 2 2 3 3 3\r\n", "output": "30\r\n"}, {"input": "92 5 40\r\n3 3 3 3 2 2 2 2 5 5 5 5 5 5 5 5 5 5 5 4 4 4 4 4 4 3 3 5 3 3 3 4 4 4 1 1 4 4 4 4 4 4 4 4 4 4 4 2 2 2 2 2 2 2 2 2 3 3 3 2 5 1 1 4 4 4 4 4 4 4 4 4 4 4 2 2 4 4 5 5 5 5 5 5 5 5 5 2 2 2 2 2\r\n", "output": "33\r\n"}, {"input": "99 10 10\r\n9 9 9 10 10 10 9 9 9 9 9 2 2 10 10 10 10 10 3 3 5 10 10 2 2 3 3 6 1 1 1 1 1 1 7 7 7 7 7 4 4 6 6 6 8 9 9 9 2 2 9 9 5 5 5 5 1 10 7 7 9 9 9 5 6 6 6 6 8 8 4 1 3 3 3 3 3 3 9 9 4 1 1 7 1 1 1 3 3 3 3 3 3 10 9 10 9 8 9\r\n", "output": "12\r\n"}, {"input": "95 10 30\r\n3 3 8 8 8 4 9 3 3 3 3 3 3 8 10 5 5 5 5 5 5 4 9 1 1 1 1 6 6 7 7 7 1 1 1 1 1 1 9 9 10 10 10 10 10 5 3 3 3 3 3 3 6 6 6 6 1 6 6 6 6 9 4 9 5 5 5 2 2 2 2 10 10 8 3 3 4 2 9 9 9 2 5 2 2 8 8 8 7 7 3 3 3 4 4\r\n", "output": "11\r\n"}, {"input": "100 10 15\r\n7 7 3 6 6 6 8 8 8 8 8 8 8 8 8 8 8 5 5 1 9 9 9 9 9 9 9 9 2 2 2 4 7 7 8 2 2 2 2 2 2 8 8 7 7 2 2 2 7 7 7 4 4 4 4 4 4 4 4 4 4 7 7 7 7 7 7 7 7 2 2 2 6 6 3 3 3 3 3 3 1 1 1 1 1 1 4 4 4 4 1 1 1 1 5 4 5 6 6 6\r\n", "output": "13\r\n"}]
false
stdio
null
true
394/C
394
C
Python 3
TESTS
6
62
0
7112420
__author__ = 'epeshk' n, m = list(map(int,input().split())) _00 = 0 _01 = 0 _11 = 0 for i in range (n): c=list(map(str,input().split())) for j in c: if j == '00': _00+=1 if j == '01' or j=='10': _01+=1 if j == '11': _11+=1 C = [['' for i in range (m)] for j in range(n)] for i in range(n): for j in range(m): if _11>0: C[i][j]='11' _11-=1 elif i-1>=0 and _01>0: if C[i-1][j]=='11': C[i][j]='00' _00-=1 if C[i-1][j]=='10': C[i][j]='01' _01-=1 if C[i-1][j]=='01': C[i][j]='10' _01-=1 if C[i-1][j]=='00': C[i][j]='10' _01-=1 elif _01>0: C[i][j]='10' _01-=1 else: C[i][j]='00' _00-=1 for i in range(n): print(" ".join(C[i]))
27
358
614,400
14807872
n, m = map(int, input().split()) doubles, singles = 0, 0 for r in range(n): for s in input().split(): if s == '11': doubles += 1 elif s != '00': singles += 1 lines = { 'zero': ' '.join(m * [ '00' ]), 'double': ' '.join(m * [ '11' ]), 'single_0': ' '.join(m * [ '01' ]), 'single_1': ' '.join(m * [ '10' ]) } zeros = n * m - doubles - singles while doubles >= m: print(lines['double']) doubles -= m while singles >= 2 * m: print(lines['single_0']) print(lines['single_1']) singles -= 2 * m while zeros >= m: print(lines['zero']) zeros -= m x = doubles + singles + zeros tail = [ m * [ '00' ] for r in range(x // m) ] height = len(tail) r, c = 0, 0 while singles + doubles > 0: if tail[r][c] == '00': if doubles > 0: tail[r][c] = '11' doubles -= 1 else: tail[r][c] = '01' singles -= 1 if singles > 0 and r + 1 < height: tail[r + 1][c] = '10' singles -= 1 c += 1 if c == m: c = 0 r += 1 for row in tail: print(' '.join(row))
Codeforces Round 231 (Div. 2)
CF
2,014
2
256
Dominoes
During the break, we decided to relax and play dominoes. Our box with Domino was empty, so we decided to borrow the teacher's dominoes. The teacher responded instantly at our request. He put nm dominoes on the table as an n × 2m rectangle so that each of the n rows contained m dominoes arranged horizontally. Each half of each domino contained number (0 or 1). We were taken aback, and the teacher smiled and said: "Consider some arrangement of dominoes in an n × 2m matrix. Let's count for each column of the matrix the sum of numbers in this column. Then among all such sums find the maximum one. Can you rearrange the dominoes in the matrix in such a way that the maximum sum will be minimum possible? Note that it is prohibited to change the orientation of the dominoes, they all need to stay horizontal, nevertheless dominoes are allowed to rotate by 180 degrees. As a reward I will give you all my dominoes". We got even more taken aback. And while we are wondering what was going on, help us make an optimal matrix of dominoes.
The first line contains integers n, m (1 ≤ n, m ≤ 103). In the next lines there is a description of the teachers' matrix. Each of next n lines contains m dominoes. The description of one domino is two integers (0 or 1), written without a space — the digits on the left and right half of the domino.
Print the resulting matrix of dominoes in the format: n lines, each of them contains m space-separated dominoes. If there are multiple optimal solutions, print any of them.
null
Consider the answer for the first sample. There, the maximum sum among all columns equals 1 (the number of columns is 6, and not 3). Obviously, this maximum can't be less than 1, then such matrix is optimal. Note that the dominoes can be rotated by 180 degrees.
[{"input": "2 3\n01 11 00\n00 01 11", "output": "11 11 10\n00 00 01"}, {"input": "4 1\n11\n10\n01\n00", "output": "11\n10\n01\n00"}]
null
["constructive algorithms", "greedy"]
27
[{"input": "2 3\r\n01 11 00\r\n00 01 11\r\n", "output": "11 11 10\r\n00 00 01\r\n"}, {"input": "4 1\r\n11\r\n10\r\n01\r\n00\r\n", "output": "11\r\n10\r\n01\r\n00\r\n"}, {"input": "1 1\r\n00\r\n", "output": "00\r\n"}, {"input": "1 1\r\n01\r\n", "output": "10\r\n"}, {"input": "1 1\r\n11\r\n", "output": "11\r\n"}, {"input": "9 9\r\n01 00 00 01 00 01 11 11 11\r\n10 10 10 01 10 01 11 01 10\r\n10 00 10 00 11 01 00 10 00\r\n01 00 01 01 11 00 00 11 11\r\n11 00 10 11 01 01 11 00 01\r\n01 10 00 00 11 10 01 01 10\r\n11 10 11 00 11 11 01 10 10\r\n10 00 01 00 00 00 11 01 01\r\n00 11 01 00 10 01 10 00 01\r\n", "output": "11 11 11 11 11 11 11 11 11\r\n11 11 11 11 11 11 11 11 11\r\n10 10 10 10 10 10 10 10 10\r\n10 10 10 10 10 10 10 10 10\r\n10 10 10 01 01 01 01 01 01\r\n01 01 01 01 01 01 01 01 01\r\n01 01 01 00 00 00 00 01 01\r\n00 00 00 00 00 00 00 00 00\r\n00 00 00 00 00 00 00 00 00\r\n"}, {"input": "9 9\r\n10 10 10 01 10 11 11 01 10\r\n11 00 10 10 11 10 01 00 00\r\n10 00 11 01 00 01 01 11 10\r\n10 11 10 00 01 11 11 10 11\r\n01 11 11 01 11 00 10 00 01\r\n01 00 00 10 01 01 10 00 01\r\n11 10 11 10 01 00 00 11 00\r\n10 11 10 10 01 10 10 10 01\r\n10 10 10 10 11 11 01 00 11\r\n", "output": "11 11 11 11 11 11 11 11 11\r\n11 11 11 11 11 11 11 11 11\r\n11 11 10 10 10 10 10 10 10\r\n10 10 10 10 10 10 10 10 10\r\n10 10 10 10 10 10 10 01 01\r\n01 01 01 01 01 01 01 01 01\r\n01 01 01 01 01 01 01 01 01\r\n00 00 00 00 01 01 01 00 00\r\n00 00 00 00 00 00 00 00 00\r\n"}, {"input": "9 1\r\n01\r\n00\r\n01\r\n01\r\n00\r\n00\r\n00\r\n01\r\n11\r\n", "output": "11\r\n10\r\n10\r\n01\r\n01\r\n00\r\n00\r\n00\r\n00\r\n"}, {"input": "2 9\r\n11 10 11 10 10 11 00 10 00\r\n10 00 00 10 10 00 11 01 01\r\n", "output": "11 11 11 11 10 10 10 10 10\r\n00 00 00 00 00 01 01 01 01\r\n"}, {"input": "2 8\r\n10 01 01 11 10 10 01 10\r\n01 11 01 01 11 10 01 01\r\n", "output": "11 11 11 10 10 10 10 10\r\n10 10 01 01 01 01 01 01\r\n"}, {"input": "3 5\r\n00 10 10 11 01\r\n11 01 11 11 10\r\n10 11 00 00 00\r\n", "output": "11 11 11 11 11\r\n10 10 10 01 01\r\n00 00 01 00 00\r\n"}, {"input": "2 3\r\n00 10 01\r\n01 01 00\r\n", "output": "10 10 01\r\n00 01 00\r\n"}, {"input": "2 5\r\n01 00 01 01 00\r\n11 01 11 11 10\r\n", "output": "11 11 11 10 10\r\n10 00 00 01 01\r\n"}]
false
stdio
import sys from collections import Counter def read_dominoes(file, n, m): dominoes = [] grid = [] for _ in range(n): line = file.readline().strip() if not line: return [], [] row_dominoes = line.split() grid_row = [] for d in row_dominoes: a, b = d[0], d[1] sorted_d = tuple(sorted((a, b))) dominoes.append(sorted_d) grid_row.append(a) grid_row.append(b) grid.append(grid_row) return dominoes, grid def compute_max_col_sum(grid, m): columns = 2 * m col_sums = [0] * columns for row in grid: for i in range(columns): col_sums[i] += int(row[i]) return max(col_sums) def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path, 'r') as input_file, \ open(output_path, 'r') as output_file, \ open(submission_path, 'r') as submission_file: # Read input n_line = input_file.readline().strip() if not n_line: print(0) return n, m = map(int, n_line.split()) input_dominoes, _ = read_dominoes(input_file, n, m) input_counter = Counter(input_dominoes) # Read submission submission_dominoes, submission_grid = read_dominoes(submission_file, n, m) if not submission_dominoes or len(submission_grid) != n: print(0) return submission_counter = Counter(submission_dominoes) # Check dominoes if submission_counter != input_counter: print(0) return # Compute submission max column sum submission_max = compute_max_col_sum(submission_grid, m) # Read reference output to compute max _, reference_grid = read_dominoes(output_file, n, m) if not reference_grid: print(0) return reference_max = compute_max_col_sum(reference_grid, m) # Compare maxima if submission_max == reference_max: print(1) else: print(0) if __name__ == '__main__': main()
true
724/D
724
D
PyPy 3
TESTS
68
233
29,286,400
128085648
def process(S, m): d = {} for c in S: if c not in d: d[c] = 0 d[c]+=1 L = [] for c in d: L.append([c, d[c], 0]) L = sorted(L) n = len(S) m2 = len(L) index = 0 curr = set([]) d2 = {} for i in range(m): c = S[i] if c not in d2: d2[c] = set([]) d2[c].add(i) c_min = min(d2) i2 = max(d2[c_min]) curr.add(i2) if c_min==L[index][0]: L[index][2]+=1 if L[index][1]==L[index][2]: index+=1 for i in range(m, n): c = S[i] if c not in d2: d2[c] = set([]) d2[c].add(i) c2 = S[i-m] d2[c2].remove(i-m) if len(d2[c2])==0: d2.pop(c2) if i-m in curr: curr.remove(i-m) if len(curr)==0: c3 = min(d2) i2 = max(d2[c3]) curr.add(i2) for i3 in range(m2): if L[i3][0]==c3: L[i3][2]+=1 if i3==index and L[index][0]==L[index][2]: index+=1 max_index = None for i3 in range(m2): if L[i3][2] > 0: max_index = i3 for i3 in range(max_index): L[i3][2] = L[i3][1] answer = [] for c, count1, count2 in L: for i in range(count2): answer.append(c) return ''.join(answer) m = int(input()) S = input() print(process(S, m))
71
171
614,400
104240605
m = int(input()) s = input() d = [0 for _ in range(26)] for char in s: d[ord(char) - ord('a')] += 1 for i in range(26): char, left, right, counter = chr(ord('a') + i), -1, -1, 0 for j in range(len(s)): if s[j] < char: left = j if s[j] == char: right = j if j - left >= m: if j - right >= m: counter = -1 break counter += 1 left = right if ~counter: for j in range(i): print(chr(ord('a') + j) * d[j], end='') print(chr(ord('a') + i) * counter) break
Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)
CF
2,016
2
256
Dense Subsequence
You are given a string s, consisting of lowercase English letters, and the integer m. One should choose some symbols from the given string so that any contiguous subsegment of length m has at least one selected symbol. Note that here we choose positions of symbols, not the symbols themselves. Then one uses the chosen symbols to form a new string. All symbols from the chosen position should be used, but we are allowed to rearrange them in any order. Formally, we choose a subsequence of indices 1 ≤ i1 < i2 < ... < it ≤ |s|. The selected sequence must meet the following condition: for every j such that 1 ≤ j ≤ |s| - m + 1, there must be at least one selected index that belongs to the segment [j,  j + m - 1], i.e. there should exist a k from 1 to t, such that j ≤ ik ≤ j + m - 1. Then we take any permutation p of the selected indices and form a new string sip1sip2... sipt. Find the lexicographically smallest string, that can be obtained using this procedure.
The first line of the input contains a single integer m (1 ≤ m ≤ 100 000). The second line contains the string s consisting of lowercase English letters. It is guaranteed that this string is non-empty and its length doesn't exceed 100 000. It is also guaranteed that the number m doesn't exceed the length of the string s.
Print the single line containing the lexicographically smallest string, that can be obtained using the procedure described above.
null
In the first sample, one can choose the subsequence {3} and form a string "a". In the second sample, one can choose the subsequence {1, 2, 4} (symbols on this positions are 'a', 'b' and 'a') and rearrange the chosen symbols to form a string "aab".
[{"input": "3\ncbabc", "output": "a"}, {"input": "2\nabcab", "output": "aab"}, {"input": "3\nbcabcbaccba", "output": "aaabb"}]
1,900
["data structures", "greedy", "strings"]
71
[{"input": "3\r\ncbabc\r\n", "output": "a\r\n"}, {"input": "2\r\nabcab\r\n", "output": "aab\r\n"}, {"input": "3\r\nbcabcbaccba\r\n", "output": "aaabb\r\n"}, {"input": "5\r\nimmaydobun\r\n", "output": "ab\r\n"}, {"input": "5\r\nwjjdqawypvtgrncmqvcsergermprauyevcegjtcrrblkwiugrcjfpjyxngyryxntauxlouvwgjzpsuxyxvhavgezwtuzknetdibv\r\n", "output": "aaaabbcccccddeeeeeefggggggghiijjjjjjkkllmmnnnnoppppqqrrrrrrrrsstttttu\r\n"}, {"input": "10\r\nefispvmzuutsrpxzfrykhabznxiyquwvhwhrksrgzodtuepfvamilfdynapzhzyhncorhzuewrrkcduvuhwsrprjrmgctnvrdtpj\r\n", "output": "aaabcccddddeeeffffgghhhhhhhiiijjkkklm\r\n"}, {"input": "20\r\nhlicqhxayiodyephxlfoetfketnaabpfegqcrjzlshkxfzjssvpvzhzylgowwovgxznzowvpklbwbzhwtkkaomjkenhpedmbmjic\r\n", "output": "aaaabbbbcccddeeeeeeffffg\r\n"}, {"input": "50\r\ntyhjolxuexoffdkdwimsjujorgeksyiyvvqecvhpjsuayqnibijtipuqhkulxpysotlmtrsgygpkdhkrtntwqzrpfckiscaphyhv\r\n", "output": "aab\r\n"}, {"input": "1\r\nbaaa\r\n", "output": "aaab\r\n"}, {"input": "5\r\nbbbbba\r\n", "output": "ab\r\n"}, {"input": "10\r\nbbabcbbaabcbcbcbaabbccaacccbbbcaaacabbbbaaaccbcccacbbccaccbbaacaccbabcaaaacaccacbaaccaaccbaacabbbaac\r\n", "output": "aaaaaaaaaaa\r\n"}]
false
stdio
null
true
400/B
400
B
Python 3
TESTS
5
93
102,400
220150190
n,m=map(int,input().split()) po=0 flag=False c=0 ok=0 no=0 v=[] for j in range(n): b=input() for p in range(m): if b[p]=="G": ok=p elif b[p]=="S": no=p if no-ok <= 0: break else: v+=[no-ok] if len(set(v))==0: print(-1) else: print(len(set(v)))
34
31
204,800
210339817
n,m=map(int,input().split()) st=set() for i in range(n): s1=input() g=s1.index("G") s=s1.index("S") if s<g: print(-1) exit(0) st.add(abs(s-g)) print(len(st))
Codeforces Round 234 (Div. 2)
CF
2,014
1
256
Inna and New Matrix of Candies
Inna likes sweets and a game called the "Candy Matrix". Today, she came up with the new game "Candy Matrix 2: Reload". The field for the new game is a rectangle table of size n × m. Each line of the table contains one cell with a dwarf figurine, one cell with a candy, the other cells of the line are empty. The game lasts for several moves. During each move the player should choose all lines of the matrix where dwarf is not on the cell with candy and shout "Let's go!". After that, all the dwarves from the chosen lines start to simultaneously move to the right. During each second, each dwarf goes to the adjacent cell that is located to the right of its current cell. The movement continues until one of the following events occurs: - some dwarf in one of the chosen lines is located in the rightmost cell of his row; - some dwarf in the chosen lines is located in the cell with the candy. The point of the game is to transport all the dwarves to the candy cells. Inna is fabulous, as she came up with such an interesting game. But what about you? Your task is to play this game optimally well. Specifically, you should say by the given game field what minimum number of moves the player needs to reach the goal of the game.
The first line of the input contains two integers n and m (1 ≤ n ≤ 1000; 2 ≤ m ≤ 1000). Next n lines each contain m characters — the game field for the "Candy Martix 2: Reload". Character "*" represents an empty cell of the field, character "G" represents a dwarf and character "S" represents a candy. The matrix doesn't contain other characters. It is guaranteed that each line contains exactly one character "G" and one character "S".
In a single line print a single integer — either the minimum number of moves needed to achieve the aim of the game, or -1, if the aim cannot be achieved on the given game field.
null
null
[{"input": "3 4\n*G*S\nG**S\n*G*S", "output": "2"}, {"input": "1 3\nS*G", "output": "-1"}]
1,200
["brute force", "implementation", "schedules"]
34
[{"input": "3 4\r\n*G*S\r\nG**S\r\n*G*S\r\n", "output": "2\r\n"}, {"input": "1 3\r\nS*G\r\n", "output": "-1\r\n"}, {"input": "10 10\r\nG********S\r\n*G*******S\r\n**G******S\r\n***G*****S\r\n****G****S\r\n*****G***S\r\n******G**S\r\n*******G*S\r\n********GS\r\nG********S\r\n", "output": "9\r\n"}, {"input": "5 10\r\nG***S*****\r\nG****S****\r\n***GS*****\r\nG*S*******\r\nG***S*****\r\n", "output": "4\r\n"}, {"input": "4 8\r\nG*S*****\r\n****G*S*\r\nG*****S*\r\n**G***S*\r\n", "output": "3\r\n"}, {"input": "4 10\r\n***G****S*\r\n*****GS***\r\nG****S****\r\nG*******S*\r\n", "output": "3\r\n"}, {"input": "1 2\r\nSG\r\n", "output": "-1\r\n"}, {"input": "1 2\r\nGS\r\n", "output": "1\r\n"}, {"input": "1 4\r\nSG**\r\n", "output": "-1\r\n"}]
false
stdio
null
true
177/C1
177
C1
PyPy 3-64
TESTS1
3
122
0
175831673
import sys input = sys.stdin.readline from collections import Counter n = int(input()) k = int(input()) d = [[] for i in range(n)] for i in range(k): a, b = map(lambda x:int(x)-1, input().split()) d[a].append(b) d[b].append(a) x = [-1]*n c = 0 for i in range(n): if x[i] == -1: q = [i] while q: a = q.pop() x[a] = c for j in d[a]: if x[j] == -1: q.append(j) c += 1 m = int(input()) e = [[] for i in range(c)] p = Counter(x) d = [set() for i in range(n)] for i in range(m): a, b = map(lambda x:int(x)-1, input().split()) if x[a] == x[b]: e[x[a]].extend([a, b]) d[a].add(b) d[b].add(a) x = [0]*n c = 0 for ii, i in enumerate(e): q = Counter(i) q = [i for j, i in sorted([(q[i], i) for i in q], reverse=True)] cc = 0 for j in q: if len(d[j]) > 0: for l in d[j]: d[l].remove(j) cc += 1 c = max(c, p[ii]-cc) print(c)
17
218
307,200
65333942
def find(a): if parent[a]!=a: parent[a]=find(parent[a]) return parent[a] def union(a,b): u,v=find(a),find(b) if u==v: return if rank[u]>rank[v]: parent[v]=u else: parent[u]=v if rank[u]==rank[v]: rank[v]+=1 n=int(input()) k=int(input()) parent=list(map(int,range(n+1))) rank=[0]*(n+1) ans=[0]*(n+1) count=[0]*(n+1) for i in range(k): u,v=map(int,input().split()) union(u,v) for i in range(len(ans)): ans[find(i)]+=1 for i in range(len(parent)): count[parent[i]]+=1 d={} m=int(input()) for i in range(m): u,v=map(int,input().split()) if parent[u]==parent[v]: d[parent[u]]=False sak=0 for i in range(len(count)): if count[i]!=0 and i not in d and i!=0: sak=max(sak,count[i]) print(sak)
ABBYY Cup 2.0 - Easy
ICPC
2,012
2
256
Party
To celebrate the second ABBYY Cup tournament, the Smart Beaver decided to throw a party. The Beaver has a lot of acquaintances, some of them are friends with each other, and some of them dislike each other. To make party successful, the Smart Beaver wants to invite only those of his friends who are connected by friendship relations, and not to invite those who dislike each other. Both friendship and dislike are mutual feelings. More formally, for each invited person the following conditions should be fulfilled: - all his friends should also be invited to the party; - the party shouldn't have any people he dislikes; - all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length. We'll say that people a1 and ap are connected through a chain of common friends if there exists a sequence of people a2, a3, ..., ap - 1 such that all pairs of people ai and ai + 1 (1 ≤ i < p) are friends. Help the Beaver find the maximum number of acquaintances he can invite.
The first line of input contains an integer n — the number of the Beaver's acquaintances. The second line contains an integer k $$( 0 \leq k \leq \min ( 1 0 ^ { 5 }, \frac { n \cdot ( n - 1 ) } { 2 } ) )$$ — the number of pairs of friends. Next k lines contain space-separated pairs of integers ui, vi $$( 1 \leq u _ { i }, v _ { i } \leq n, u _ { i } \neq v _ { i } )$$ — indices of people who form the i-th pair of friends. The next line contains an integer m $$( 0 \leq m \leq min(10^{5},\frac{n(n-1)}{2}))$$ — the number of pairs of people who dislike each other. Next m lines describe pairs of people who dislike each other in the same format as the pairs of friends were described. Each pair of people is mentioned in the input at most once $$( 0 \leq k + m \leq \frac { n \cdot ( n - 1 ) } { 2 } )$$. In particular, two persons cannot be friends and dislike each other at the same time. The input limitations for getting 30 points are: - 2 ≤ n ≤ 14 The input limitations for getting 100 points are: - 2 ≤ n ≤ 2000
Output a single number — the maximum number of people that can be invited to the party. If a group of people that meets all the requirements is impossible to select, output 0.
null
Let's have a look at the example. Two groups of people can be invited: {1, 2, 3} and {4, 5}, thus the answer will be the size of the largest of these groups. Group {6, 7, 8, 9} doesn't fit, since it includes people 7 and 9 who dislike each other. Group {1, 2, 3, 4, 5} also doesn't fit, because not all of its members are connected by a chain of common friends (for example, people 2 and 5 aren't connected).
[{"input": "9\n8\n1 2\n1 3\n2 3\n4 5\n6 7\n7 8\n8 9\n9 6\n2\n1 6\n7 9", "output": "3"}]
1,500
["dfs and similar", "dsu", "graphs"]
17
[{"input": "9\r\n8\r\n1 2\r\n1 3\r\n2 3\r\n4 5\r\n6 7\r\n7 8\r\n8 9\r\n9 6\r\n2\r\n1 6\r\n7 9\r\n", "output": "3"}, {"input": "2\r\n1\r\n1 2\r\n0\r\n", "output": "2"}, {"input": "2\r\n0\r\n1\r\n1 2\r\n", "output": "1"}, {"input": "3\r\n2\r\n1 2\r\n1 3\r\n1\r\n2 3\r\n", "output": "0"}, {"input": "3\r\n3\r\n1 3\r\n2 1\r\n2 3\r\n0\r\n", "output": "3"}, {"input": "4\r\n3\r\n1 2\r\n2 3\r\n3 1\r\n3\r\n1 4\r\n4 2\r\n3 4\r\n", "output": "3"}, {"input": "7\r\n8\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n2 4\r\n2 5\r\n3 4\r\n5 6\r\n3\r\n2 6\r\n5 7\r\n6 7\r\n", "output": "1"}, {"input": "14\r\n20\r\n1 2\r\n4 5\r\n4 6\r\n4 11\r\n5 7\r\n5 8\r\n5 13\r\n5 14\r\n7 8\r\n7 14\r\n8 9\r\n8 11\r\n8 12\r\n8 14\r\n10 11\r\n10 12\r\n10 14\r\n11 13\r\n11 14\r\n12 14\r\n5\r\n1 8\r\n1 13\r\n2 10\r\n7 12\r\n8 10\r\n", "output": "2"}, {"input": "2\r\n0\r\n0\r\n", "output": "1"}, {"input": "14\r\n0\r\n0\r\n", "output": "1"}, {"input": "14\r\n6\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n8 9\r\n9 10\r\n3\r\n5 6\r\n6 7\r\n7 8\r\n", "output": "5"}, {"input": "14\r\n10\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n1 6\r\n1 7\r\n2 3\r\n2 4\r\n2 5\r\n2 6\r\n1\r\n2 7\r\n", "output": "1"}, {"input": "2\r\n0\r\n1\r\n1 2\r\n", "output": "1"}]
false
stdio
null
true
400/B
400
B
Python 3
TESTS
5
77
0
104638980
n,m = map(int, input().split(" ")) a = set() tag = True for i in range(n): x = input() indexG = x.index("G") indexS = x.index("S") if indexS< indexG: print(-1) tag = False else: a.add(indexS - indexG) if tag: print(len(a)) else: pass
34
31
204,800
219440089
n,m=map(int,input().split()) a=set() ans=1 for _ in range(n): x=input() if x.find('S')-x.find('G')<0: ans=0 break else: temp=x.find('S')-x.find('G') a.add(temp) if ans: print(len(a)) else: print(-1)
Codeforces Round 234 (Div. 2)
CF
2,014
1
256
Inna and New Matrix of Candies
Inna likes sweets and a game called the "Candy Matrix". Today, she came up with the new game "Candy Matrix 2: Reload". The field for the new game is a rectangle table of size n × m. Each line of the table contains one cell with a dwarf figurine, one cell with a candy, the other cells of the line are empty. The game lasts for several moves. During each move the player should choose all lines of the matrix where dwarf is not on the cell with candy and shout "Let's go!". After that, all the dwarves from the chosen lines start to simultaneously move to the right. During each second, each dwarf goes to the adjacent cell that is located to the right of its current cell. The movement continues until one of the following events occurs: - some dwarf in one of the chosen lines is located in the rightmost cell of his row; - some dwarf in the chosen lines is located in the cell with the candy. The point of the game is to transport all the dwarves to the candy cells. Inna is fabulous, as she came up with such an interesting game. But what about you? Your task is to play this game optimally well. Specifically, you should say by the given game field what minimum number of moves the player needs to reach the goal of the game.
The first line of the input contains two integers n and m (1 ≤ n ≤ 1000; 2 ≤ m ≤ 1000). Next n lines each contain m characters — the game field for the "Candy Martix 2: Reload". Character "*" represents an empty cell of the field, character "G" represents a dwarf and character "S" represents a candy. The matrix doesn't contain other characters. It is guaranteed that each line contains exactly one character "G" and one character "S".
In a single line print a single integer — either the minimum number of moves needed to achieve the aim of the game, or -1, if the aim cannot be achieved on the given game field.
null
null
[{"input": "3 4\n*G*S\nG**S\n*G*S", "output": "2"}, {"input": "1 3\nS*G", "output": "-1"}]
1,200
["brute force", "implementation", "schedules"]
34
[{"input": "3 4\r\n*G*S\r\nG**S\r\n*G*S\r\n", "output": "2\r\n"}, {"input": "1 3\r\nS*G\r\n", "output": "-1\r\n"}, {"input": "10 10\r\nG********S\r\n*G*******S\r\n**G******S\r\n***G*****S\r\n****G****S\r\n*****G***S\r\n******G**S\r\n*******G*S\r\n********GS\r\nG********S\r\n", "output": "9\r\n"}, {"input": "5 10\r\nG***S*****\r\nG****S****\r\n***GS*****\r\nG*S*******\r\nG***S*****\r\n", "output": "4\r\n"}, {"input": "4 8\r\nG*S*****\r\n****G*S*\r\nG*****S*\r\n**G***S*\r\n", "output": "3\r\n"}, {"input": "4 10\r\n***G****S*\r\n*****GS***\r\nG****S****\r\nG*******S*\r\n", "output": "3\r\n"}, {"input": "1 2\r\nSG\r\n", "output": "-1\r\n"}, {"input": "1 2\r\nGS\r\n", "output": "1\r\n"}, {"input": "1 4\r\nSG**\r\n", "output": "-1\r\n"}]
false
stdio
null
true
400/B
400
B
Python 3
TESTS
5
62
6,758,400
125743989
n,m=map(int,input().split()) field=[] for i in range(0,n): row=input() space=row.index('S')-row.index('G') #print(space) if space>0: field.append(space) if(len(field)>0): print(len(set(field))) else: print(-1)
34
46
0
146095711
def dist(s): return s.find("S") - s.find("G") - 1 a, b = map(int, input().split()) dists = [dist(input()) for i in range(a)] if min(dists) < 0: print(-1) else: print(len(set(dists)))
Codeforces Round 234 (Div. 2)
CF
2,014
1
256
Inna and New Matrix of Candies
Inna likes sweets and a game called the "Candy Matrix". Today, she came up with the new game "Candy Matrix 2: Reload". The field for the new game is a rectangle table of size n × m. Each line of the table contains one cell with a dwarf figurine, one cell with a candy, the other cells of the line are empty. The game lasts for several moves. During each move the player should choose all lines of the matrix where dwarf is not on the cell with candy and shout "Let's go!". After that, all the dwarves from the chosen lines start to simultaneously move to the right. During each second, each dwarf goes to the adjacent cell that is located to the right of its current cell. The movement continues until one of the following events occurs: - some dwarf in one of the chosen lines is located in the rightmost cell of his row; - some dwarf in the chosen lines is located in the cell with the candy. The point of the game is to transport all the dwarves to the candy cells. Inna is fabulous, as she came up with such an interesting game. But what about you? Your task is to play this game optimally well. Specifically, you should say by the given game field what minimum number of moves the player needs to reach the goal of the game.
The first line of the input contains two integers n and m (1 ≤ n ≤ 1000; 2 ≤ m ≤ 1000). Next n lines each contain m characters — the game field for the "Candy Martix 2: Reload". Character "*" represents an empty cell of the field, character "G" represents a dwarf and character "S" represents a candy. The matrix doesn't contain other characters. It is guaranteed that each line contains exactly one character "G" and one character "S".
In a single line print a single integer — either the minimum number of moves needed to achieve the aim of the game, or -1, if the aim cannot be achieved on the given game field.
null
null
[{"input": "3 4\n*G*S\nG**S\n*G*S", "output": "2"}, {"input": "1 3\nS*G", "output": "-1"}]
1,200
["brute force", "implementation", "schedules"]
34
[{"input": "3 4\r\n*G*S\r\nG**S\r\n*G*S\r\n", "output": "2\r\n"}, {"input": "1 3\r\nS*G\r\n", "output": "-1\r\n"}, {"input": "10 10\r\nG********S\r\n*G*******S\r\n**G******S\r\n***G*****S\r\n****G****S\r\n*****G***S\r\n******G**S\r\n*******G*S\r\n********GS\r\nG********S\r\n", "output": "9\r\n"}, {"input": "5 10\r\nG***S*****\r\nG****S****\r\n***GS*****\r\nG*S*******\r\nG***S*****\r\n", "output": "4\r\n"}, {"input": "4 8\r\nG*S*****\r\n****G*S*\r\nG*****S*\r\n**G***S*\r\n", "output": "3\r\n"}, {"input": "4 10\r\n***G****S*\r\n*****GS***\r\nG****S****\r\nG*******S*\r\n", "output": "3\r\n"}, {"input": "1 2\r\nSG\r\n", "output": "-1\r\n"}, {"input": "1 2\r\nGS\r\n", "output": "1\r\n"}, {"input": "1 4\r\nSG**\r\n", "output": "-1\r\n"}]
false
stdio
null
true
400/B
400
B
PyPy 3-64
TESTS
5
46
0
189680511
n,m=map(int,input("").split(" ")) ll=[] for i in range(n): l=list(input("")) out=l.index('S')-l.index('G') if out<0: print(-1) break; ll.append(out) lout=list(dict.fromkeys(ll)) if len(lout)>0: print(len(lout))
34
46
0
185635853
n, m = map(int, input().split()) st = set() for _ in range(n): line = input() g = line.index('G') s = line.index('S') if s - g < 0: print(-1) break st.add(s-g) else: print(len(st))
Codeforces Round 234 (Div. 2)
CF
2,014
1
256
Inna and New Matrix of Candies
Inna likes sweets and a game called the "Candy Matrix". Today, she came up with the new game "Candy Matrix 2: Reload". The field for the new game is a rectangle table of size n × m. Each line of the table contains one cell with a dwarf figurine, one cell with a candy, the other cells of the line are empty. The game lasts for several moves. During each move the player should choose all lines of the matrix where dwarf is not on the cell with candy and shout "Let's go!". After that, all the dwarves from the chosen lines start to simultaneously move to the right. During each second, each dwarf goes to the adjacent cell that is located to the right of its current cell. The movement continues until one of the following events occurs: - some dwarf in one of the chosen lines is located in the rightmost cell of his row; - some dwarf in the chosen lines is located in the cell with the candy. The point of the game is to transport all the dwarves to the candy cells. Inna is fabulous, as she came up with such an interesting game. But what about you? Your task is to play this game optimally well. Specifically, you should say by the given game field what minimum number of moves the player needs to reach the goal of the game.
The first line of the input contains two integers n and m (1 ≤ n ≤ 1000; 2 ≤ m ≤ 1000). Next n lines each contain m characters — the game field for the "Candy Martix 2: Reload". Character "*" represents an empty cell of the field, character "G" represents a dwarf and character "S" represents a candy. The matrix doesn't contain other characters. It is guaranteed that each line contains exactly one character "G" and one character "S".
In a single line print a single integer — either the minimum number of moves needed to achieve the aim of the game, or -1, if the aim cannot be achieved on the given game field.
null
null
[{"input": "3 4\n*G*S\nG**S\n*G*S", "output": "2"}, {"input": "1 3\nS*G", "output": "-1"}]
1,200
["brute force", "implementation", "schedules"]
34
[{"input": "3 4\r\n*G*S\r\nG**S\r\n*G*S\r\n", "output": "2\r\n"}, {"input": "1 3\r\nS*G\r\n", "output": "-1\r\n"}, {"input": "10 10\r\nG********S\r\n*G*******S\r\n**G******S\r\n***G*****S\r\n****G****S\r\n*****G***S\r\n******G**S\r\n*******G*S\r\n********GS\r\nG********S\r\n", "output": "9\r\n"}, {"input": "5 10\r\nG***S*****\r\nG****S****\r\n***GS*****\r\nG*S*******\r\nG***S*****\r\n", "output": "4\r\n"}, {"input": "4 8\r\nG*S*****\r\n****G*S*\r\nG*****S*\r\n**G***S*\r\n", "output": "3\r\n"}, {"input": "4 10\r\n***G****S*\r\n*****GS***\r\nG****S****\r\nG*******S*\r\n", "output": "3\r\n"}, {"input": "1 2\r\nSG\r\n", "output": "-1\r\n"}, {"input": "1 2\r\nGS\r\n", "output": "1\r\n"}, {"input": "1 4\r\nSG**\r\n", "output": "-1\r\n"}]
false
stdio
null
true
408/B
408
B
Python 3
TESTS
4
31
0
208354177
a = input() b =input() ref = set([i for i in a ]) count =0 for i in ref : count += min(a.count(i) , b.count(i)) if count ==0 : print(-1) else: print(count)
21
46
0
119587484
from sys import * input = lambda:stdin.readline() int_arr = lambda : list(map(int,stdin.readline().strip().split())) str_arr = lambda :list(map(str,stdin.readline().split())) get_str = lambda : map(str,stdin.readline().strip().split()) get_int = lambda: map(int,stdin.readline().strip().split()) get_float = lambda : map(float,stdin.readline().strip().split()) mod = 1000000007 setrecursionlimit(1000) a = str(input()) b = str(input()) tot = 0 flag = 1 for i in set(b): if i in a: if i != '\n': if b.count(i) > a.count(i): tot += a.count(i) else: tot += b.count(i) else: print(-1) flag = 0 break if flag: print(tot)
Codeforces Round 239 (Div. 2)
CF
2,014
1
256
Garland
Once little Vasya read an article in a magazine on how to make beautiful handmade garland from colored paper. Vasya immediately went to the store and bought n colored sheets of paper, the area of each sheet is 1 square meter. The garland must consist of exactly m pieces of colored paper of arbitrary area, each piece should be of a certain color. To make the garland, Vasya can arbitrarily cut his existing colored sheets into pieces. Vasya is not obliged to use all the sheets to make the garland. Vasya wants the garland to be as attractive as possible, so he wants to maximize the total area of ​​m pieces of paper in the garland. Calculate what the maximum total area of ​​the pieces of paper in the garland Vasya can get.
The first line contains a non-empty sequence of n (1 ≤ n ≤ 1000) small English letters ("a"..."z"). Each letter means that Vasya has a sheet of paper of the corresponding color. The second line contains a non-empty sequence of m (1 ≤ m ≤ 1000) small English letters that correspond to the colors of the pieces of paper in the garland that Vasya wants to make.
Print an integer that is the maximum possible total area of the pieces of paper in the garland Vasya wants to get or -1, if it is impossible to make the garland from the sheets he's got. It is guaranteed that the answer is always an integer.
null
In the first test sample Vasya can make an garland of area 6: he can use both sheets of color b, three (but not four) sheets of color a and cut a single sheet of color c in three, for example, equal pieces. Vasya can use the resulting pieces to make a garland of area 6. In the second test sample Vasya cannot make a garland at all — he doesn't have a sheet of color z.
[{"input": "aaabbac\naabbccac", "output": "6"}, {"input": "a\nz", "output": "-1"}]
1,200
["implementation"]
21
[{"input": "aaabbac\r\naabbccac\r\n", "output": "6\r\n"}, {"input": "a\r\nz\r\n", "output": "-1"}, {"input": "r\r\nr\r\n", "output": "1\r\n"}, {"input": "stnsdn\r\nndnndsn\r\n", "output": "4\r\n"}, {"input": "yqfqfp\r\ntttwtqq\r\n", "output": "-1"}, {"input": "zzbbrrtrtzr\r\ntbbtrrrzr\r\n", "output": "9\r\n"}, {"input": "ivvfisvsvii\r\npaihjinno\r\n", "output": "-1"}, {"input": "zbvwnlgkshqerxptyod\r\nz\r\n", "output": "1\r\n"}, {"input": "xlktwjymocqrahnbesf\r\nfoo\r\n", "output": "2\r\n"}, {"input": "bbzmzqazmbambnmzaabznmbabzqnaabmabmnnabbmnzaanzzezebzabqaabzqaemeqqammmbazmmz\r\naznnbbmeebmanbeemzmemqbaeebnqenqzzbanebmnzqqebqmmnmqqzmmeqqqaaezemmazqqmqaqnnqqzbzeeazammmenbbamzbmnaenemenaaaebnmanebqmqnznqbenmqqnnnaeaebqmamennmqqeaaqqbammnzqmnmqnqbbezmemznqmanzmmqzzzzembqnzqbanamezqaqbazenenqqznqaebzaeezbqqbmeeaqnmmbnqbbnmaqqemaeaezaabmbnbzzaae\r\n", "output": "77\r\n"}, {"input": "lccfdfnfflncddlksndcwnfcllnnaswcdnldafcalssfcdnkkaklwnnacsncfwanwnwfadawcsdcfwflnnlfsfclkfknlaldna\r\nuaaldlllhedgugugueahddhedbuddaedhaaeugdubaealbgbagedldbl\r\n", "output": "-1"}, {"input": "hvewdvwdwudrwarwmddwnazmwauzatrmwptwwevavpmwrtruwnpwantewrnwmepdwvtmnveanunrvrtwpvhhnuhnmpptdttzmmndtvudmzhhannmmnahdpzamuvhzaavnhtnumwrwvttdetvuewaaennddwuvzvaptdzrzhtetwwzmzedrwuvrwznprhdvnavrruvvhzuwpdtmpwmzrwvermrhdamv\r\nuvzhwtpuputnahwwarduzddhpnwwvettprwavdmnztdnrddmarmvuevtdezndnezvarhmppwwnmvnrtddzhhnzrwuhvpwmezuurundarwdazwptrpeurrnwautddnhdmhtwhwvvtavdzezumetzezpazndhuentmrhamutrtttpevtuutemdnvwnwnmnvmznatneweuaahdavmaddhnrdenwwztrwh\r\n", "output": "199\r\n"}, {"input": "aaccddff\r\nabcdf\r\n", "output": "-1"}]
false
stdio
null
true
408/B
408
B
Python 3
TESTS
7
62
5,529,600
26480968
n = input() m = input() resources = len(n) area = 0 for i in range(len(m)): n = n.replace(m[i], "", 1) if len(n) == resources or resources - len(n) < len(set(n)): print("-1") else: print(resources - len(n))
21
46
0
142608376
s = input() t = input() c = 0 s_c = [0 for i in range(26)] t_c = [0 for i in range(26)] for i in range(len(s)): s_c[ord(s[i])-ord('a')] += 1 for i in range(len(t)): t_c[ord(t[i])-ord('a')] += 1 for i in range(26): if(s_c[i] == 0 and t_c[i] != 0): c = 0 break else: c += min(s_c[i],t_c[i]) if(c==0): print(-1) else: print(c)
Codeforces Round 239 (Div. 2)
CF
2,014
1
256
Garland
Once little Vasya read an article in a magazine on how to make beautiful handmade garland from colored paper. Vasya immediately went to the store and bought n colored sheets of paper, the area of each sheet is 1 square meter. The garland must consist of exactly m pieces of colored paper of arbitrary area, each piece should be of a certain color. To make the garland, Vasya can arbitrarily cut his existing colored sheets into pieces. Vasya is not obliged to use all the sheets to make the garland. Vasya wants the garland to be as attractive as possible, so he wants to maximize the total area of ​​m pieces of paper in the garland. Calculate what the maximum total area of ​​the pieces of paper in the garland Vasya can get.
The first line contains a non-empty sequence of n (1 ≤ n ≤ 1000) small English letters ("a"..."z"). Each letter means that Vasya has a sheet of paper of the corresponding color. The second line contains a non-empty sequence of m (1 ≤ m ≤ 1000) small English letters that correspond to the colors of the pieces of paper in the garland that Vasya wants to make.
Print an integer that is the maximum possible total area of the pieces of paper in the garland Vasya wants to get or -1, if it is impossible to make the garland from the sheets he's got. It is guaranteed that the answer is always an integer.
null
In the first test sample Vasya can make an garland of area 6: he can use both sheets of color b, three (but not four) sheets of color a and cut a single sheet of color c in three, for example, equal pieces. Vasya can use the resulting pieces to make a garland of area 6. In the second test sample Vasya cannot make a garland at all — he doesn't have a sheet of color z.
[{"input": "aaabbac\naabbccac", "output": "6"}, {"input": "a\nz", "output": "-1"}]
1,200
["implementation"]
21
[{"input": "aaabbac\r\naabbccac\r\n", "output": "6\r\n"}, {"input": "a\r\nz\r\n", "output": "-1"}, {"input": "r\r\nr\r\n", "output": "1\r\n"}, {"input": "stnsdn\r\nndnndsn\r\n", "output": "4\r\n"}, {"input": "yqfqfp\r\ntttwtqq\r\n", "output": "-1"}, {"input": "zzbbrrtrtzr\r\ntbbtrrrzr\r\n", "output": "9\r\n"}, {"input": "ivvfisvsvii\r\npaihjinno\r\n", "output": "-1"}, {"input": "zbvwnlgkshqerxptyod\r\nz\r\n", "output": "1\r\n"}, {"input": "xlktwjymocqrahnbesf\r\nfoo\r\n", "output": "2\r\n"}, {"input": "bbzmzqazmbambnmzaabznmbabzqnaabmabmnnabbmnzaanzzezebzabqaabzqaemeqqammmbazmmz\r\naznnbbmeebmanbeemzmemqbaeebnqenqzzbanebmnzqqebqmmnmqqzmmeqqqaaezemmazqqmqaqnnqqzbzeeazammmenbbamzbmnaenemenaaaebnmanebqmqnznqbenmqqnnnaeaebqmamennmqqeaaqqbammnzqmnmqnqbbezmemznqmanzmmqzzzzembqnzqbanamezqaqbazenenqqznqaebzaeezbqqbmeeaqnmmbnqbbnmaqqemaeaezaabmbnbzzaae\r\n", "output": "77\r\n"}, {"input": "lccfdfnfflncddlksndcwnfcllnnaswcdnldafcalssfcdnkkaklwnnacsncfwanwnwfadawcsdcfwflnnlfsfclkfknlaldna\r\nuaaldlllhedgugugueahddhedbuddaedhaaeugdubaealbgbagedldbl\r\n", "output": "-1"}, {"input": "hvewdvwdwudrwarwmddwnazmwauzatrmwptwwevavpmwrtruwnpwantewrnwmepdwvtmnveanunrvrtwpvhhnuhnmpptdttzmmndtvudmzhhannmmnahdpzamuvhzaavnhtnumwrwvttdetvuewaaennddwuvzvaptdzrzhtetwwzmzedrwuvrwznprhdvnavrruvvhzuwpdtmpwmzrwvermrhdamv\r\nuvzhwtpuputnahwwarduzddhpnwwvettprwavdmnztdnrddmarmvuevtdezndnezvarhmppwwnmvnrtddzhhnzrwuhvpwmezuurundarwdazwptrpeurrnwautddnhdmhtwhwvvtavdzezumetzezpazndhuentmrhamutrtttpevtuutemdnvwnwnmnvmznatneweuaahdavmaddhnrdenwwztrwh\r\n", "output": "199\r\n"}, {"input": "aaccddff\r\nabcdf\r\n", "output": "-1"}]
false
stdio
null
true
756/E
756
E
Python 3
TESTS
3
46
4,608,000
24216588
#!/usr/bin/env python3 MOD = 1000000007 N = int(input()) a = [int(x) for x in input().split()] + [1] b = [int(x) for x in input().split()] M = int(input()) r = [] for i in range(N - 1): r.append(M % a[i]) M /= a[i] r += [M] dp = [1] for i in range(N - 1, -1, -1): scale = a[i] dp2 = [0] * 50 for k in range(len(dp)): missing = scale * k + r[i] for j in range(50): use = missing - j if use >= 0 and use <= b[i]: dp2[j] = (dp2[j] + dp[k]) % MOD dp = dp2 print(dp[0])
40
763
44,236,800
24049216
p = 1000000007 n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) m = int(input()) d = [1] * 300001 td = [0] * 300001 L = b[0] for i in range(1, n): if a[i - 1] != 1: t = m % a[i - 1] if L < t: print(0) exit(0) m //= a[i - 1] for j in range((L - t) // a[i - 1] + 1): d[j] = d[t] t += a[i - 1] L = j k = 0 for j in range(L + b[i] + 1): if j <= L: k += d[j] k %= p td[j] = k if j >= b[i]: k -= d[j - b[i]] L += b[i] for j in range(L + 1): d[j] = td[j] print(d[m] if m <= L else 0)
8VC Venture Cup 2017 - Final Round
CF
2,017
1
512
Byteland coins
There are n types of coins in Byteland. Conveniently, the denomination of the coin type k divides the denomination of the coin type k + 1, the denomination of the coin type 1 equals 1 tugrick. The ratio of the denominations of coin types k + 1 and k equals ak. It is known that for each x there are at most 20 coin types of denomination x. Byteasar has bk coins of type k with him, and he needs to pay exactly m tugricks. It is known that Byteasar never has more than 3·105 coins with him. Byteasar want to know how many ways there are to pay exactly m tugricks. Two ways are different if there is an integer k such that the amount of coins of type k differs in these two ways. As all Byteland citizens, Byteasar wants to know the number of ways modulo 109 + 7.
The first line contains single integer n (1 ≤ n ≤ 3·105) — the number of coin types. The second line contains n - 1 integers a1, a2, ..., an - 1 (1 ≤ ak ≤ 109) — the ratios between the coin types denominations. It is guaranteed that for each x there are at most 20 coin types of denomination x. The third line contains n non-negative integers b1, b2, ..., bn — the number of coins of each type Byteasar has. It is guaranteed that the sum of these integers doesn't exceed 3·105. The fourth line contains single integer m (0 ≤ m < 1010000) — the amount in tugricks Byteasar needs to pay.
Print single integer — the number of ways to pay exactly m tugricks modulo 109 + 7.
null
In the first example Byteasar has 4 coins of denomination 1, and he has to pay 2 tugricks. There is only one way. In the second example Byteasar has 4 coins of each of two different types of denomination 1, he has to pay 2 tugricks. There are 3 ways: pay one coin of the first type and one coin of the other, pay two coins of the first type, and pay two coins of the second type. In the third example the denominations are equal to 1, 3, 9.
[{"input": "1\n4\n2", "output": "1"}, {"input": "2\n1\n4 4\n2", "output": "3"}, {"input": "3\n3 3\n10 10 10\n17", "output": "6"}]
3,200
["combinatorics", "dp", "math"]
40
[{"input": "1\r\n\r\n4\r\n2\r\n", "output": "1\r\n"}, {"input": "2\r\n1\r\n4 4\r\n2\r\n", "output": "3\r\n"}, {"input": "3\r\n3 3\r\n10 10 10\r\n17\r\n", "output": "6\r\n"}, {"input": "2\r\n2\r\n200000 100000\r\n34567\r\n", "output": "17284\r\n"}, {"input": "20\r\n2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 299981\r\n1234567890\r\n", "output": "1\r\n"}, {"input": "20\r\n2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2\r\n299981 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n1034567\r\n", "output": "149991\r\n"}, {"input": "10\r\n1 1 1 1 1 1 1 1 2\r\n0 10 68 1 7 6 0 1 3 4\r\n61\r\n", "output": "49280\r\n"}, {"input": "100\r\n4 1 5 3 2 1 1 1 4 1 1 2 1 1 1 4 1 1 3 1 3 1 1 1 1 4 5 1 5 2 5 3 1 1 1 1 1 1 1 3 2 1 1 3 1 1 3 4 3 2 4 1 1 4 1 1 2 2 4 1 4 1 2 5 1 2 2 1 5 3 1 5 4 2 1 1 2 5 5 1 4 4 2 3 1 4 1 3 2 1 1 1 4 1 3 1 1 5 1\r\n0 18 10 2 1 9 9 0 9 5 6 8 11 6 28 11 29 50 25 15 9 4 3 51 13 4 68 31 4 6 2 5 26 1 21 7 3 4 9 7 40 3 0 7 14 18 4 8 4 1 0 3 21 2 5 1 2 8 2 4 10 11 25 5 11 4 2 5 3 3 4 7 0 0 1 9 0 0 4 16 1 20 10 22 17 3 14 11 30 1 3 7 3 5 6 13 3 9 18 7\r\n188562805042251972437939648\r\n", "output": "890905252\r\n"}, {"input": "10\r\n3 9 10 10 4 10 9 10 8\r\n18 54 100 42 402 13 28 208 102 33\r\n77760001052028517\r\n", "output": "0\r\n"}, {"input": "10\r\n1 1 1 1 1 1 1 1 2\r\n0 0 0 0 0 0 1 0 1 0\r\n1\r\n", "output": "2\r\n"}, {"input": "20\r\n2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2\r\n2 136 23 34 16 22 7 1 121 65 11 5 68 144 3 14 3 35 44 246\r\n86551330\r\n", "output": "960419474\r\n"}, {"input": "20\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1\r\n29 77 47 64 67 89 71 21 106 15 47 34 90 10 6 28 18 11 152 18\r\n501\r\n", "output": "287270499\r\n"}, {"input": "10\r\n443307727 348302095 35497258 398797405 725089211 557667579 7764455 164622658 466615150\r\n9 7 30 1 4 6 6 4 23 10\r\n3690054862906606768658826690738341858379111902540863414278121378497891890923\r\n", "output": "1\r\n"}, {"input": "10\r\n1 1 1 1 1 1 1 1 1\r\n9519 118380 15475 18454 10395 10005 1925 43712 6710 65425\r\n114853\r\n", "output": "983175834\r\n"}, {"input": "20\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n3340 8397 18248 8914 6824 396 6249 22945 6175 1443 13271 53526 12738 5346 8485 12784 31161 2378 68313 9067\r\n145333\r\n", "output": "116763993\r\n"}, {"input": "1\r\n\r\n300000\r\n294705\r\n", "output": "1\r\n"}, {"input": "2\r\n1\r\n45133 254867\r\n62105\r\n", "output": "45134\r\n"}, {"input": "10\r\n2 2 3 3 2 2 2 3 3\r\n117 254 68 126 105 3 100 45 166 16\r\n2592000130163\r\n", "output": "0\r\n"}, {"input": "10\r\n1 1 1 1 1 1 1 1 1\r\n73 126 74 58 337 123 0 9 161 39\r\n1000000656\r\n", "output": "0\r\n"}, {"input": "10\r\n1 1 1 1 1 1 1 1 1\r\n55 75 21 92 159 178 181 137 29 73\r\n1000000533\r\n", "output": "0\r\n"}, {"input": "10\r\n5 7 5 8 3 7 2 4 7\r\n124 154 10 227 74 10 15 309 68 9\r\n49389597\r\n", "output": "3894309\r\n"}, {"input": "10\r\n1 1 1 1 1 1 1 1 1\r\n185 63 24 145 162 205 46 104 54 12\r\n1461\r\n", "output": "0\r\n"}]
false
stdio
null
true
177/C1
177
C1
PyPy 3-64
TESTS1
3
124
0
225903288
import sys input = sys.stdin.readline ############ ---- Input Functions ---- ############ def inp(): return int(input()) def inlt(): return list(map(int, input().split())) def insr(): s = input() return list(s[: len(s) - 1]) def invr(): return map(int, input().split()) def find(x): if x == root[x]: return x root[x] = find(root[x]) return root[x] def union(x, y): a, b = find(x), find(y) if a != b: if rank[a] > rank[b]: root[b] = a elif rank[b] > rank[a]: root[a] = b else: root[b] = a rank[a] += 1 import heapq n = inp() k = inp() friends = [] child = [0 for _ in range(n)] for _ in range(k): a, b = invr() friends.append([a - 1, b - 1]) m = inp() foe = [[] for _ in range(n)] for _ in range(m): a, b = invr() foe[a - 1].append(b - 1) foe[b - 1].append(a - 1) rank = [1 for i in range(n)] root = [i for i in range(n)] for a, b in friends: union(a, b) foecount = [0 for _ in range(n)] for i in range(n): child[find(i)] += 1 # print(child) for i in range(n): x = find(i) total = child[x] for dushman in foe[i]: if find(dushman) == x: foecount[i] -= 1 # print(foecount) q = [] for i in range(n): if foecount[i] != 0: heapq.heappush(q, (foecount[i], i)) while q: dushman, node = heapq.heappop(q) org = find(node) child[org] -= 1 for nei in foe[node]: if find(nei) == org: foecount[nei] += 1 if foecount[nei] < 0: heapq.heappush(q, (foecount[nei], nei)) print(max(child))
17
218
307,200
74705608
# maa chudaaye duniya n = int(input()) parents = [i for i in range(n+1)] ranks = [1 for i in range(n+1)] def find(x): if parents[x] != x: parents[x] = find(parents[x]) return parents[x] def union(x, y): xs = find(x) ys = find(y) if xs == ys: return if ranks[xs] > ranks[ys]: parents[ys] = xs elif ranks[ys] > ranks[xs]: parents[xs] = ys else: parents[ys] = xs ranks[xs] += 1 for _ in range(int(input())): u, v = map(int, input().split()) union(u, v) # print(parents) rejects = set([]) for _ in range(int(input())): p, q = map(int, input().split()) ps = find(p) qs = find(q) if ps == qs: rejects.add(ps) ps = {} for i in range(1, n+1): p = find(i) if p not in rejects: if p in ps: ps[p] += 1 else: ps[p] = 1 # print(ps) ans = 0 for i in ps: ans = max(ans, ps[i]) print(ans)
ABBYY Cup 2.0 - Easy
ICPC
2,012
2
256
Party
To celebrate the second ABBYY Cup tournament, the Smart Beaver decided to throw a party. The Beaver has a lot of acquaintances, some of them are friends with each other, and some of them dislike each other. To make party successful, the Smart Beaver wants to invite only those of his friends who are connected by friendship relations, and not to invite those who dislike each other. Both friendship and dislike are mutual feelings. More formally, for each invited person the following conditions should be fulfilled: - all his friends should also be invited to the party; - the party shouldn't have any people he dislikes; - all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length. We'll say that people a1 and ap are connected through a chain of common friends if there exists a sequence of people a2, a3, ..., ap - 1 such that all pairs of people ai and ai + 1 (1 ≤ i < p) are friends. Help the Beaver find the maximum number of acquaintances he can invite.
The first line of input contains an integer n — the number of the Beaver's acquaintances. The second line contains an integer k $$( 0 \leq k \leq \min ( 1 0 ^ { 5 }, \frac { n \cdot ( n - 1 ) } { 2 } ) )$$ — the number of pairs of friends. Next k lines contain space-separated pairs of integers ui, vi $$( 1 \leq u _ { i }, v _ { i } \leq n, u _ { i } \neq v _ { i } )$$ — indices of people who form the i-th pair of friends. The next line contains an integer m $$( 0 \leq m \leq min(10^{5},\frac{n(n-1)}{2}))$$ — the number of pairs of people who dislike each other. Next m lines describe pairs of people who dislike each other in the same format as the pairs of friends were described. Each pair of people is mentioned in the input at most once $$( 0 \leq k + m \leq \frac { n \cdot ( n - 1 ) } { 2 } )$$. In particular, two persons cannot be friends and dislike each other at the same time. The input limitations for getting 30 points are: - 2 ≤ n ≤ 14 The input limitations for getting 100 points are: - 2 ≤ n ≤ 2000
Output a single number — the maximum number of people that can be invited to the party. If a group of people that meets all the requirements is impossible to select, output 0.
null
Let's have a look at the example. Two groups of people can be invited: {1, 2, 3} and {4, 5}, thus the answer will be the size of the largest of these groups. Group {6, 7, 8, 9} doesn't fit, since it includes people 7 and 9 who dislike each other. Group {1, 2, 3, 4, 5} also doesn't fit, because not all of its members are connected by a chain of common friends (for example, people 2 and 5 aren't connected).
[{"input": "9\n8\n1 2\n1 3\n2 3\n4 5\n6 7\n7 8\n8 9\n9 6\n2\n1 6\n7 9", "output": "3"}]
1,500
["dfs and similar", "dsu", "graphs"]
17
[{"input": "9\r\n8\r\n1 2\r\n1 3\r\n2 3\r\n4 5\r\n6 7\r\n7 8\r\n8 9\r\n9 6\r\n2\r\n1 6\r\n7 9\r\n", "output": "3"}, {"input": "2\r\n1\r\n1 2\r\n0\r\n", "output": "2"}, {"input": "2\r\n0\r\n1\r\n1 2\r\n", "output": "1"}, {"input": "3\r\n2\r\n1 2\r\n1 3\r\n1\r\n2 3\r\n", "output": "0"}, {"input": "3\r\n3\r\n1 3\r\n2 1\r\n2 3\r\n0\r\n", "output": "3"}, {"input": "4\r\n3\r\n1 2\r\n2 3\r\n3 1\r\n3\r\n1 4\r\n4 2\r\n3 4\r\n", "output": "3"}, {"input": "7\r\n8\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n2 4\r\n2 5\r\n3 4\r\n5 6\r\n3\r\n2 6\r\n5 7\r\n6 7\r\n", "output": "1"}, {"input": "14\r\n20\r\n1 2\r\n4 5\r\n4 6\r\n4 11\r\n5 7\r\n5 8\r\n5 13\r\n5 14\r\n7 8\r\n7 14\r\n8 9\r\n8 11\r\n8 12\r\n8 14\r\n10 11\r\n10 12\r\n10 14\r\n11 13\r\n11 14\r\n12 14\r\n5\r\n1 8\r\n1 13\r\n2 10\r\n7 12\r\n8 10\r\n", "output": "2"}, {"input": "2\r\n0\r\n0\r\n", "output": "1"}, {"input": "14\r\n0\r\n0\r\n", "output": "1"}, {"input": "14\r\n6\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n8 9\r\n9 10\r\n3\r\n5 6\r\n6 7\r\n7 8\r\n", "output": "5"}, {"input": "14\r\n10\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n1 6\r\n1 7\r\n2 3\r\n2 4\r\n2 5\r\n2 6\r\n1\r\n2 7\r\n", "output": "1"}, {"input": "2\r\n0\r\n1\r\n1 2\r\n", "output": "1"}]
false
stdio
null
true
408/A
408
A
PyPy 3-64
TESTS
16
62
1,638,400
196299412
n = int(input()) ans = int(5e4) *k, = map(int, input().split()) for i in range(n): *m, = map(int, input().split()) ans = min(ans, sum(m) * 5 + k[i] * 15) print(ans)
20
31
0
143984023
n=int(input()) k=list(map(int,input().split())) for i in range(n): a=list(map(int,input().split())) val=sum(a)*5+k[i]*15 if(i==0): m=val else: if(m>val): m=val print(m)
Codeforces Round 239 (Div. 2)
CF
2,014
1
256
Line to Cashier
Little Vasya went to the supermarket to get some groceries. He walked about the supermarket for a long time and got a basket full of products. Now he needs to choose the cashier to pay for the products. There are n cashiers at the exit from the supermarket. At the moment the queue for the i-th cashier already has ki people. The j-th person standing in the queue to the i-th cashier has mi, j items in the basket. Vasya knows that: - the cashier needs 5 seconds to scan one item; - after the cashier scans each item of some customer, he needs 15 seconds to take the customer's money and give him the change. Of course, Vasya wants to select a queue so that he can leave the supermarket as soon as possible. Help him write a program that displays the minimum number of seconds after which Vasya can get to one of the cashiers.
The first line contains integer n (1 ≤ n ≤ 100) — the number of cashes in the shop. The second line contains n space-separated integers: k1, k2, ..., kn (1 ≤ ki ≤ 100), where ki is the number of people in the queue to the i-th cashier. The i-th of the next n lines contains ki space-separated integers: mi, 1, mi, 2, ..., mi, ki (1 ≤ mi, j ≤ 100) — the number of products the j-th person in the queue for the i-th cash has.
Print a single integer — the minimum number of seconds Vasya needs to get to the cashier.
null
In the second test sample, if Vasya goes to the first queue, he gets to the cashier in 100·5 + 15 = 515 seconds. But if he chooses the second queue, he will need 1·5 + 2·5 + 2·5 + 3·5 + 4·15 = 100 seconds. He will need 1·5 + 9·5 + 1·5 + 3·15 = 100 seconds for the third one and 7·5 + 8·5 + 2·15 = 105 seconds for the fourth one. Thus, Vasya gets to the cashier quicker if he chooses the second or the third queue.
[{"input": "1\n1\n1", "output": "20"}, {"input": "4\n1 4 3 2\n100\n1 2 2 3\n1 9 1\n7 8", "output": "100"}]
900
["implementation"]
20
[{"input": "1\r\n1\r\n1\r\n", "output": "20\r\n"}, {"input": "4\r\n1 4 3 2\r\n100\r\n1 2 2 3\r\n1 9 1\r\n7 8\r\n", "output": "100\r\n"}, {"input": "4\r\n5 4 5 5\r\n3 1 3 1 2\r\n3 1 1 3\r\n1 1 1 2 2\r\n2 2 1 1 3\r\n", "output": "100\r\n"}, {"input": "5\r\n5 3 6 6 4\r\n7 5 3 3 9\r\n6 8 2\r\n1 10 8 5 9 2\r\n9 7 8 5 9 10\r\n9 8 3 3\r\n", "output": "125\r\n"}, {"input": "5\r\n10 10 10 10 10\r\n6 7 8 6 8 5 9 8 10 5\r\n9 6 9 8 7 8 8 10 8 5\r\n8 7 7 8 7 5 6 8 9 5\r\n6 5 10 5 5 10 7 8 5 5\r\n10 9 8 7 6 9 7 9 6 5\r\n", "output": "480\r\n"}, {"input": "10\r\n9 10 10 10 9 5 9 7 8 7\r\n11 6 10 4 4 15 7 15 5\r\n3 9 11 12 11 1 13 13 1 5\r\n6 15 9 12 3 2 8 12 11 10\r\n7 1 1 6 10 2 6 1 14 2\r\n8 14 2 3 6 1 14 1 12\r\n6 10 9 3 5\r\n13 12 12 7 13 4 4 8 10\r\n5 6 4 3 14 9 13\r\n8 12 1 5 7 4 13 1\r\n1 9 5 3 5 1 4\r\n", "output": "240\r\n"}, {"input": "10\r\n5 5 5 5 5 5 5 5 5 5\r\n5 5 4 5 4\r\n6 5 7 7 6\r\n5 4 4 5 5\r\n4 4 5 5 5\r\n7 6 4 5 7\r\n4 6 5 4 5\r\n6 6 7 6 6\r\n4 5 4 4 7\r\n7 5 4 4 5\r\n6 6 7 4 4\r\n", "output": "190\r\n"}, {"input": "1\r\n1\r\n100\r\n", "output": "515\r\n"}, {"input": "1\r\n90\r\n90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90\r\n", "output": "41850\r\n"}]
false
stdio
null
true
408/B
408
B
Python 3
TESTS
4
31
0
158936970
a = input() b = input() o = {chr(x): 0 for x in range(97, 123)} e = {chr(x): 0 for x in range(97, 123)} for item in a: o[item] += 1 for item in b: e[item] += 1 new = 0 for key, value in o.items(): new += min(value, e[key]) if new == 0:print(-1) else:print(new)
21
46
0
165679408
n = input() m = input() s = set(m) c= 0 for i in s: a1 = n.count(i) a2 = m.count(i) if a1 == 0: c = -1 break else: c+=min(a1,a2) print(c)
Codeforces Round 239 (Div. 2)
CF
2,014
1
256
Garland
Once little Vasya read an article in a magazine on how to make beautiful handmade garland from colored paper. Vasya immediately went to the store and bought n colored sheets of paper, the area of each sheet is 1 square meter. The garland must consist of exactly m pieces of colored paper of arbitrary area, each piece should be of a certain color. To make the garland, Vasya can arbitrarily cut his existing colored sheets into pieces. Vasya is not obliged to use all the sheets to make the garland. Vasya wants the garland to be as attractive as possible, so he wants to maximize the total area of ​​m pieces of paper in the garland. Calculate what the maximum total area of ​​the pieces of paper in the garland Vasya can get.
The first line contains a non-empty sequence of n (1 ≤ n ≤ 1000) small English letters ("a"..."z"). Each letter means that Vasya has a sheet of paper of the corresponding color. The second line contains a non-empty sequence of m (1 ≤ m ≤ 1000) small English letters that correspond to the colors of the pieces of paper in the garland that Vasya wants to make.
Print an integer that is the maximum possible total area of the pieces of paper in the garland Vasya wants to get or -1, if it is impossible to make the garland from the sheets he's got. It is guaranteed that the answer is always an integer.
null
In the first test sample Vasya can make an garland of area 6: he can use both sheets of color b, three (but not four) sheets of color a and cut a single sheet of color c in three, for example, equal pieces. Vasya can use the resulting pieces to make a garland of area 6. In the second test sample Vasya cannot make a garland at all — he doesn't have a sheet of color z.
[{"input": "aaabbac\naabbccac", "output": "6"}, {"input": "a\nz", "output": "-1"}]
1,200
["implementation"]
21
[{"input": "aaabbac\r\naabbccac\r\n", "output": "6\r\n"}, {"input": "a\r\nz\r\n", "output": "-1"}, {"input": "r\r\nr\r\n", "output": "1\r\n"}, {"input": "stnsdn\r\nndnndsn\r\n", "output": "4\r\n"}, {"input": "yqfqfp\r\ntttwtqq\r\n", "output": "-1"}, {"input": "zzbbrrtrtzr\r\ntbbtrrrzr\r\n", "output": "9\r\n"}, {"input": "ivvfisvsvii\r\npaihjinno\r\n", "output": "-1"}, {"input": "zbvwnlgkshqerxptyod\r\nz\r\n", "output": "1\r\n"}, {"input": "xlktwjymocqrahnbesf\r\nfoo\r\n", "output": "2\r\n"}, {"input": "bbzmzqazmbambnmzaabznmbabzqnaabmabmnnabbmnzaanzzezebzabqaabzqaemeqqammmbazmmz\r\naznnbbmeebmanbeemzmemqbaeebnqenqzzbanebmnzqqebqmmnmqqzmmeqqqaaezemmazqqmqaqnnqqzbzeeazammmenbbamzbmnaenemenaaaebnmanebqmqnznqbenmqqnnnaeaebqmamennmqqeaaqqbammnzqmnmqnqbbezmemznqmanzmmqzzzzembqnzqbanamezqaqbazenenqqznqaebzaeezbqqbmeeaqnmmbnqbbnmaqqemaeaezaabmbnbzzaae\r\n", "output": "77\r\n"}, {"input": "lccfdfnfflncddlksndcwnfcllnnaswcdnldafcalssfcdnkkaklwnnacsncfwanwnwfadawcsdcfwflnnlfsfclkfknlaldna\r\nuaaldlllhedgugugueahddhedbuddaedhaaeugdubaealbgbagedldbl\r\n", "output": "-1"}, {"input": "hvewdvwdwudrwarwmddwnazmwauzatrmwptwwevavpmwrtruwnpwantewrnwmepdwvtmnveanunrvrtwpvhhnuhnmpptdttzmmndtvudmzhhannmmnahdpzamuvhzaavnhtnumwrwvttdetvuewaaennddwuvzvaptdzrzhtetwwzmzedrwuvrwznprhdvnavrruvvhzuwpdtmpwmzrwvermrhdamv\r\nuvzhwtpuputnahwwarduzddhpnwwvettprwavdmnztdnrddmarmvuevtdezndnezvarhmppwwnmvnrtddzhhnzrwuhvpwmezuurundarwdazwptrpeurrnwautddnhdmhtwhwvvtavdzezumetzezpazndhuentmrhamutrtttpevtuutemdnvwnwnmnvmznatneweuaahdavmaddhnrdenwwztrwh\r\n", "output": "199\r\n"}, {"input": "aaccddff\r\nabcdf\r\n", "output": "-1"}]
false
stdio
null
true
408/B
408
B
Python 3
TESTS
4
31
0
161124165
import sys input = sys.stdin.readline from collections import defaultdict s = input()[:-1] w = input()[:-1] d = defaultdict(int) for i in s: d[i] += 1 c = 0 for i in w: if d[i] > 0: d[i] -= 1 c += 1 if c == 0: print(-1) else: print(c)
21
46
0
166918276
x=input() y=input() d1={} d2={} count=0 for i in x: d1[i]=x.count(i) for i in y: d2[i]=y.count(i) for i in d2.keys(): if i in d1.keys(): if d1[i]<d2[i]: count+=d1[i] else: count+=d2[i] else: count=-1 break print(count)
Codeforces Round 239 (Div. 2)
CF
2,014
1
256
Garland
Once little Vasya read an article in a magazine on how to make beautiful handmade garland from colored paper. Vasya immediately went to the store and bought n colored sheets of paper, the area of each sheet is 1 square meter. The garland must consist of exactly m pieces of colored paper of arbitrary area, each piece should be of a certain color. To make the garland, Vasya can arbitrarily cut his existing colored sheets into pieces. Vasya is not obliged to use all the sheets to make the garland. Vasya wants the garland to be as attractive as possible, so he wants to maximize the total area of ​​m pieces of paper in the garland. Calculate what the maximum total area of ​​the pieces of paper in the garland Vasya can get.
The first line contains a non-empty sequence of n (1 ≤ n ≤ 1000) small English letters ("a"..."z"). Each letter means that Vasya has a sheet of paper of the corresponding color. The second line contains a non-empty sequence of m (1 ≤ m ≤ 1000) small English letters that correspond to the colors of the pieces of paper in the garland that Vasya wants to make.
Print an integer that is the maximum possible total area of the pieces of paper in the garland Vasya wants to get or -1, if it is impossible to make the garland from the sheets he's got. It is guaranteed that the answer is always an integer.
null
In the first test sample Vasya can make an garland of area 6: he can use both sheets of color b, three (but not four) sheets of color a and cut a single sheet of color c in three, for example, equal pieces. Vasya can use the resulting pieces to make a garland of area 6. In the second test sample Vasya cannot make a garland at all — he doesn't have a sheet of color z.
[{"input": "aaabbac\naabbccac", "output": "6"}, {"input": "a\nz", "output": "-1"}]
1,200
["implementation"]
21
[{"input": "aaabbac\r\naabbccac\r\n", "output": "6\r\n"}, {"input": "a\r\nz\r\n", "output": "-1"}, {"input": "r\r\nr\r\n", "output": "1\r\n"}, {"input": "stnsdn\r\nndnndsn\r\n", "output": "4\r\n"}, {"input": "yqfqfp\r\ntttwtqq\r\n", "output": "-1"}, {"input": "zzbbrrtrtzr\r\ntbbtrrrzr\r\n", "output": "9\r\n"}, {"input": "ivvfisvsvii\r\npaihjinno\r\n", "output": "-1"}, {"input": "zbvwnlgkshqerxptyod\r\nz\r\n", "output": "1\r\n"}, {"input": "xlktwjymocqrahnbesf\r\nfoo\r\n", "output": "2\r\n"}, {"input": "bbzmzqazmbambnmzaabznmbabzqnaabmabmnnabbmnzaanzzezebzabqaabzqaemeqqammmbazmmz\r\naznnbbmeebmanbeemzmemqbaeebnqenqzzbanebmnzqqebqmmnmqqzmmeqqqaaezemmazqqmqaqnnqqzbzeeazammmenbbamzbmnaenemenaaaebnmanebqmqnznqbenmqqnnnaeaebqmamennmqqeaaqqbammnzqmnmqnqbbezmemznqmanzmmqzzzzembqnzqbanamezqaqbazenenqqznqaebzaeezbqqbmeeaqnmmbnqbbnmaqqemaeaezaabmbnbzzaae\r\n", "output": "77\r\n"}, {"input": "lccfdfnfflncddlksndcwnfcllnnaswcdnldafcalssfcdnkkaklwnnacsncfwanwnwfadawcsdcfwflnnlfsfclkfknlaldna\r\nuaaldlllhedgugugueahddhedbuddaedhaaeugdubaealbgbagedldbl\r\n", "output": "-1"}, {"input": "hvewdvwdwudrwarwmddwnazmwauzatrmwptwwevavpmwrtruwnpwantewrnwmepdwvtmnveanunrvrtwpvhhnuhnmpptdttzmmndtvudmzhhannmmnahdpzamuvhzaavnhtnumwrwvttdetvuewaaennddwuvzvaptdzrzhtetwwzmzedrwuvrwznprhdvnavrruvvhzuwpdtmpwmzrwvermrhdamv\r\nuvzhwtpuputnahwwarduzddhpnwwvettprwavdmnztdnrddmarmvuevtdezndnezvarhmppwwnmvnrtddzhhnzrwuhvpwmezuurundarwdazwptrpeurrnwautddnhdmhtwhwvvtavdzezumetzezpazndhuentmrhamutrtttpevtuutemdnvwnwnmnvmznatneweuaahdavmaddhnrdenwwztrwh\r\n", "output": "199\r\n"}, {"input": "aaccddff\r\nabcdf\r\n", "output": "-1"}]
false
stdio
null
true
599/C
599
C
Python 3
TESTS
3
31
0
159296371
n = int(input()) a = [*map(int,input().split())] s = sorted(a) window = [set(),set()] ans = 0 for i in range(n): window[0].add(a[i]); window[1].add(s[i]) if window[0] == window[1]: ans +=1 window[0].clear; window[1].clear print(ans)
39
124
17,817,600
224633886
n=int(input()) h=list(map(int,input().split())) ans,mn,mx=0,[],[] for i in range(n): mn.append(float("inf")) mx.append(float("-inf")) mn[-1],mx[0]=h[-1],h[0] for i in range(n-2,-1,-1): mn[i]=min(h[i],mn[i+1]) for i in range(1,n): mx[i]=max(mx[i-1],h[i]) for i in range(n-1): if mx[i]<=mn[i+1]: ans+=1 print(ans+1)
Codeforces Round 332 (Div. 2)
CF
2,015
2
256
Day at the Beach
One day Squidward, Spongebob and Patrick decided to go to the beach. Unfortunately, the weather was bad, so the friends were unable to ride waves. However, they decided to spent their time building sand castles. At the end of the day there were n castles built by friends. Castles are numbered from 1 to n, and the height of the i-th castle is equal to hi. When friends were about to leave, Squidward noticed, that castles are not ordered by their height, and this looks ugly. Now friends are going to reorder the castles in a way to obtain that condition hi ≤ hi + 1 holds for all i from 1 to n - 1. Squidward suggested the following process of sorting castles: - Castles are split into blocks — groups of consecutive castles. Therefore the block from i to j will include castles i, i + 1, ..., j. A block may consist of a single castle. - The partitioning is chosen in such a way that every castle is a part of exactly one block. - Each block is sorted independently from other blocks, that is the sequence hi, hi + 1, ..., hj becomes sorted. - The partitioning should satisfy the condition that after each block is sorted, the sequence hi becomes sorted too. This may always be achieved by saying that the whole sequence is a single block. Even Patrick understands that increasing the number of blocks in partitioning will ease the sorting process. Now friends ask you to count the maximum possible number of blocks in a partitioning that satisfies all the above requirements.
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of castles Spongebob, Patrick and Squidward made from sand during the day. The next line contains n integers hi (1 ≤ hi ≤ 109). The i-th of these integers corresponds to the height of the i-th castle.
Print the maximum possible number of blocks in a valid partitioning.
null
In the first sample the partitioning looks like that: [1][2][3]. In the second sample the partitioning is: [2, 1][3, 2]
[{"input": "3\n1 2 3", "output": "3"}, {"input": "4\n2 1 3 2", "output": "2"}]
1,600
["sortings"]
39
[{"input": "3\r\n1 2 3\r\n", "output": "3\r\n"}, {"input": "4\r\n2 1 3 2\r\n", "output": "2\r\n"}, {"input": "17\r\n1 45 22 39 28 23 23 100 500 778 777 778 1001 1002 1005 1003 1005\r\n", "output": "10\r\n"}, {"input": "101\r\n1 50 170 148 214 153 132 234 181 188 180 225 226 200 197 122 181 168 87 220 223 160 235 94 257 145 199 235 102 146 119 60 109 134 209 260 210 191 180 271 236 195 155 169 166 143 246 102 208 137 278 269 156 251 198 165 111 198 151 213 256 121 276 163 179 285 104 99 139 122 188 184 215 242 244 115 304 259 135 149 104 72 303 291 124 237 112 165 183 168 71 139 85 131 137 107 120 267 235 337 69\r\n", "output": "3\r\n"}, {"input": "10\r\n1 2 2 2 2 2 2 2 2 1\r\n", "output": "2\r\n"}, {"input": "25\r\n1 2 3 4 4 4 4 4 4 4 2 3 5 5 7 9 8 5 10 12 15 12 100500 800600 228228228\r\n", "output": "12\r\n"}, {"input": "10\r\n17 18 19 19 18 17 100 500 100 100\r\n", "output": "4\r\n"}, {"input": "10\r\n1 1 1 1 5 5 1 1 1 1\r\n", "output": "5\r\n"}, {"input": "20\r\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "20\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "5\r\n1 5 3 5 2\r\n", "output": "2\r\n"}, {"input": "10\r\n1 1 1 1 2 2 2 2 4 3\r\n", "output": "9\r\n"}, {"input": "20\r\n1 2 2 2 5 6 6 6 7 7 8 9 15 15 16 16 17 18 19 19\r\n", "output": "20\r\n"}, {"input": "4\r\n2 2 1 1\r\n", "output": "1\r\n"}]
false
stdio
null
true
408/B
408
B
Python 3
TESTS
4
31
0
156544851
we=input("") need=input("") l=[] n=[] for i in need: x=0 for j in need: if i==j: x=x+1 l.append([i,x]) for i in we: y=0 for j in we: if i==j: y=y+1 n.append([i,y]) fin=[] las=[] for i in n: if i not in fin: fin.append(i)#we for i in l: if i not in las: las.append(i) #need num=0 for i in las: for j in fin: if i[0]==j[0]: if i[1]<j[1]: num=num+i[1] else: num=num+j[1] if num==0: print(-1) else: print(num)
21
46
0
173273807
n = input() m = input() nCount = [0]*26 mCount = [0]*26 for c in n: nCount[ord(c)-ord('a')]+=1 for c in m: mCount[ord(c)-ord('a')]+=1 area = 0 for i in range(26): if mCount[i] !=0 and nCount[i]==0: print(-1) exit() area+=min(mCount[i],nCount[i]) print(area)
Codeforces Round 239 (Div. 2)
CF
2,014
1
256
Garland
Once little Vasya read an article in a magazine on how to make beautiful handmade garland from colored paper. Vasya immediately went to the store and bought n colored sheets of paper, the area of each sheet is 1 square meter. The garland must consist of exactly m pieces of colored paper of arbitrary area, each piece should be of a certain color. To make the garland, Vasya can arbitrarily cut his existing colored sheets into pieces. Vasya is not obliged to use all the sheets to make the garland. Vasya wants the garland to be as attractive as possible, so he wants to maximize the total area of ​​m pieces of paper in the garland. Calculate what the maximum total area of ​​the pieces of paper in the garland Vasya can get.
The first line contains a non-empty sequence of n (1 ≤ n ≤ 1000) small English letters ("a"..."z"). Each letter means that Vasya has a sheet of paper of the corresponding color. The second line contains a non-empty sequence of m (1 ≤ m ≤ 1000) small English letters that correspond to the colors of the pieces of paper in the garland that Vasya wants to make.
Print an integer that is the maximum possible total area of the pieces of paper in the garland Vasya wants to get or -1, if it is impossible to make the garland from the sheets he's got. It is guaranteed that the answer is always an integer.
null
In the first test sample Vasya can make an garland of area 6: he can use both sheets of color b, three (but not four) sheets of color a and cut a single sheet of color c in three, for example, equal pieces. Vasya can use the resulting pieces to make a garland of area 6. In the second test sample Vasya cannot make a garland at all — he doesn't have a sheet of color z.
[{"input": "aaabbac\naabbccac", "output": "6"}, {"input": "a\nz", "output": "-1"}]
1,200
["implementation"]
21
[{"input": "aaabbac\r\naabbccac\r\n", "output": "6\r\n"}, {"input": "a\r\nz\r\n", "output": "-1"}, {"input": "r\r\nr\r\n", "output": "1\r\n"}, {"input": "stnsdn\r\nndnndsn\r\n", "output": "4\r\n"}, {"input": "yqfqfp\r\ntttwtqq\r\n", "output": "-1"}, {"input": "zzbbrrtrtzr\r\ntbbtrrrzr\r\n", "output": "9\r\n"}, {"input": "ivvfisvsvii\r\npaihjinno\r\n", "output": "-1"}, {"input": "zbvwnlgkshqerxptyod\r\nz\r\n", "output": "1\r\n"}, {"input": "xlktwjymocqrahnbesf\r\nfoo\r\n", "output": "2\r\n"}, {"input": "bbzmzqazmbambnmzaabznmbabzqnaabmabmnnabbmnzaanzzezebzabqaabzqaemeqqammmbazmmz\r\naznnbbmeebmanbeemzmemqbaeebnqenqzzbanebmnzqqebqmmnmqqzmmeqqqaaezemmazqqmqaqnnqqzbzeeazammmenbbamzbmnaenemenaaaebnmanebqmqnznqbenmqqnnnaeaebqmamennmqqeaaqqbammnzqmnmqnqbbezmemznqmanzmmqzzzzembqnzqbanamezqaqbazenenqqznqaebzaeezbqqbmeeaqnmmbnqbbnmaqqemaeaezaabmbnbzzaae\r\n", "output": "77\r\n"}, {"input": "lccfdfnfflncddlksndcwnfcllnnaswcdnldafcalssfcdnkkaklwnnacsncfwanwnwfadawcsdcfwflnnlfsfclkfknlaldna\r\nuaaldlllhedgugugueahddhedbuddaedhaaeugdubaealbgbagedldbl\r\n", "output": "-1"}, {"input": "hvewdvwdwudrwarwmddwnazmwauzatrmwptwwevavpmwrtruwnpwantewrnwmepdwvtmnveanunrvrtwpvhhnuhnmpptdttzmmndtvudmzhhannmmnahdpzamuvhzaavnhtnumwrwvttdetvuewaaennddwuvzvaptdzrzhtetwwzmzedrwuvrwznprhdvnavrruvvhzuwpdtmpwmzrwvermrhdamv\r\nuvzhwtpuputnahwwarduzddhpnwwvettprwavdmnztdnrddmarmvuevtdezndnezvarhmppwwnmvnrtddzhhnzrwuhvpwmezuurundarwdazwptrpeurrnwautddnhdmhtwhwvvtavdzezumetzezpazndhuentmrhamutrtttpevtuutemdnvwnwnmnvmznatneweuaahdavmaddhnrdenwwztrwh\r\n", "output": "199\r\n"}, {"input": "aaccddff\r\nabcdf\r\n", "output": "-1"}]
false
stdio
null
true
408/B
408
B
Python 3
TESTS
4
31
0
146593203
a=[j for j in str(input())] b=[i for i in str(input())] c=list(set(b)) ans=0 for k in c: ans+=min(a.count(k),b.count(k)) print(-1) if ans==0 else print(ans)
21
46
0
181379203
have = input() required = input() h_d = dict() r_d = dict() for i in have: h_d[i] = h_d.get(i, 0)+1 for i in required: r_d[i] = r_d.get(i, 0)+1 ans = 0 for i in r_d: if i not in h_d: print(-1) break else: ans += min(r_d[i], h_d[i]) else: print(ans)
Codeforces Round 239 (Div. 2)
CF
2,014
1
256
Garland
Once little Vasya read an article in a magazine on how to make beautiful handmade garland from colored paper. Vasya immediately went to the store and bought n colored sheets of paper, the area of each sheet is 1 square meter. The garland must consist of exactly m pieces of colored paper of arbitrary area, each piece should be of a certain color. To make the garland, Vasya can arbitrarily cut his existing colored sheets into pieces. Vasya is not obliged to use all the sheets to make the garland. Vasya wants the garland to be as attractive as possible, so he wants to maximize the total area of ​​m pieces of paper in the garland. Calculate what the maximum total area of ​​the pieces of paper in the garland Vasya can get.
The first line contains a non-empty sequence of n (1 ≤ n ≤ 1000) small English letters ("a"..."z"). Each letter means that Vasya has a sheet of paper of the corresponding color. The second line contains a non-empty sequence of m (1 ≤ m ≤ 1000) small English letters that correspond to the colors of the pieces of paper in the garland that Vasya wants to make.
Print an integer that is the maximum possible total area of the pieces of paper in the garland Vasya wants to get or -1, if it is impossible to make the garland from the sheets he's got. It is guaranteed that the answer is always an integer.
null
In the first test sample Vasya can make an garland of area 6: he can use both sheets of color b, three (but not four) sheets of color a and cut a single sheet of color c in three, for example, equal pieces. Vasya can use the resulting pieces to make a garland of area 6. In the second test sample Vasya cannot make a garland at all — he doesn't have a sheet of color z.
[{"input": "aaabbac\naabbccac", "output": "6"}, {"input": "a\nz", "output": "-1"}]
1,200
["implementation"]
21
[{"input": "aaabbac\r\naabbccac\r\n", "output": "6\r\n"}, {"input": "a\r\nz\r\n", "output": "-1"}, {"input": "r\r\nr\r\n", "output": "1\r\n"}, {"input": "stnsdn\r\nndnndsn\r\n", "output": "4\r\n"}, {"input": "yqfqfp\r\ntttwtqq\r\n", "output": "-1"}, {"input": "zzbbrrtrtzr\r\ntbbtrrrzr\r\n", "output": "9\r\n"}, {"input": "ivvfisvsvii\r\npaihjinno\r\n", "output": "-1"}, {"input": "zbvwnlgkshqerxptyod\r\nz\r\n", "output": "1\r\n"}, {"input": "xlktwjymocqrahnbesf\r\nfoo\r\n", "output": "2\r\n"}, {"input": "bbzmzqazmbambnmzaabznmbabzqnaabmabmnnabbmnzaanzzezebzabqaabzqaemeqqammmbazmmz\r\naznnbbmeebmanbeemzmemqbaeebnqenqzzbanebmnzqqebqmmnmqqzmmeqqqaaezemmazqqmqaqnnqqzbzeeazammmenbbamzbmnaenemenaaaebnmanebqmqnznqbenmqqnnnaeaebqmamennmqqeaaqqbammnzqmnmqnqbbezmemznqmanzmmqzzzzembqnzqbanamezqaqbazenenqqznqaebzaeezbqqbmeeaqnmmbnqbbnmaqqemaeaezaabmbnbzzaae\r\n", "output": "77\r\n"}, {"input": "lccfdfnfflncddlksndcwnfcllnnaswcdnldafcalssfcdnkkaklwnnacsncfwanwnwfadawcsdcfwflnnlfsfclkfknlaldna\r\nuaaldlllhedgugugueahddhedbuddaedhaaeugdubaealbgbagedldbl\r\n", "output": "-1"}, {"input": "hvewdvwdwudrwarwmddwnazmwauzatrmwptwwevavpmwrtruwnpwantewrnwmepdwvtmnveanunrvrtwpvhhnuhnmpptdttzmmndtvudmzhhannmmnahdpzamuvhzaavnhtnumwrwvttdetvuewaaennddwuvzvaptdzrzhtetwwzmzedrwuvrwznprhdvnavrruvvhzuwpdtmpwmzrwvermrhdamv\r\nuvzhwtpuputnahwwarduzddhpnwwvettprwavdmnztdnrddmarmvuevtdezndnezvarhmppwwnmvnrtddzhhnzrwuhvpwmezuurundarwdazwptrpeurrnwautddnhdmhtwhwvvtavdzezumetzezpazndhuentmrhamutrtttpevtuutemdnvwnwnmnvmznatneweuaahdavmaddhnrdenwwztrwh\r\n", "output": "199\r\n"}, {"input": "aaccddff\r\nabcdf\r\n", "output": "-1"}]
false
stdio
null
true
12/C
12
C
Python 3
TESTS
9
31
0
192682492
a,b=map(int,input().split()) d=[] c=[int(i) for i in input().split()] for i in range(b): e=input() if d.count(e)==0: d=d+[e] c.sort() k=b-len(d) print(sum(c[0:len(d)])+k*c[0],sum(c[-len(d):-1])+(k+1)*c[-1])
25
31
0
229903267
n, m = input().split() prices = sorted(list(map(int, input().split()))) fruits_count = {} for i in range(int(m)): fruit = input() fruits_count[fruit] = 1 if fruit not in fruits_count else fruits_count[fruit] + 1 fruits_prices_max = {x: y for x, y in zip(sorted(fruits_count.keys(), key=lambda x: fruits_count[x], reverse=True), sorted(prices, reverse=True))} fruits_prices_min = {x: y for x, y in zip(sorted(fruits_count.keys(), key=lambda x: fruits_count[x], reverse=True), sorted(prices))} print(sum([fruits_count[i] * fruits_prices_min[i] for i in fruits_count.keys()]), sum([fruits_count[i] * fruits_prices_max[i] for i in fruits_count.keys()]))
Codeforces Beta Round 12 (Div 2 Only)
ICPC
2,010
1
256
Fruits
The spring is coming and it means that a lot of fruits appear on the counters. One sunny day little boy Valera decided to go shopping. He made a list of m fruits he wanted to buy. If Valera want to buy more than one fruit of some kind, he includes it into the list several times. When he came to the fruit stall of Ashot, he saw that the seller hadn't distributed price tags to the goods, but put all price tags on the counter. Later Ashot will attach every price tag to some kind of fruits, and Valera will be able to count the total price of all fruits from his list. But Valera wants to know now what can be the smallest total price (in case of the most «lucky» for him distribution of price tags) and the largest total price (in case of the most «unlucky» for him distribution of price tags).
The first line of the input contains two integer number n and m (1 ≤ n, m ≤ 100) — the number of price tags (which is equal to the number of different kinds of fruits that Ashot sells) and the number of items in Valera's list. The second line contains n space-separated positive integer numbers. Each of them doesn't exceed 100 and stands for the price of one fruit of some kind. The following m lines contain names of the fruits from the list. Each name is a non-empty string of small Latin letters which length doesn't exceed 32. It is guaranteed that the number of distinct fruits from the list is less of equal to n. Also it is known that the seller has in stock all fruits that Valera wants to buy.
Print two numbers a and b (a ≤ b) — the minimum and the maximum possible sum which Valera may need to buy all fruits from his list.
null
null
[{"input": "5 3\n4 2 1 10 5\napple\norange\nmango", "output": "7 19"}, {"input": "6 5\n3 5 1 6 8 1\npeach\ngrapefruit\nbanana\norange\norange", "output": "11 30"}]
1,100
["greedy", "implementation", "sortings"]
25
[{"input": "5 3\r\n4 2 1 10 5\r\napple\r\norange\r\nmango\r\n", "output": "7 19\r\n"}, {"input": "6 5\r\n3 5 1 6 8 1\r\npeach\r\ngrapefruit\r\nbanana\r\norange\r\norange\r\n", "output": "11 30\r\n"}, {"input": "2 2\r\n91 82\r\neiiofpfpmemlakcystpun\r\nmcnzeiiofpfpmemlakcystpunfl\r\n", "output": "173 173\r\n"}, {"input": "1 4\r\n1\r\nu\r\nu\r\nu\r\nu\r\n", "output": "4 4\r\n"}, {"input": "3 3\r\n4 2 3\r\nwivujdxzjm\r\nawagljmtc\r\nwivujdxzjm\r\n", "output": "7 11\r\n"}, {"input": "3 4\r\n10 10 10\r\nodchpcsdhldqnkbhwtwnx\r\nldqnkbhwtwnxk\r\nodchpcsdhldqnkbhwtwnx\r\nldqnkbhwtwnxk\r\n", "output": "40 40\r\n"}, {"input": "3 1\r\n14 26 22\r\naag\r\n", "output": "14 26\r\n"}, {"input": "2 2\r\n5 5\r\ndcypj\r\npiyqiagzjlvbhgfndhfu\r\n", "output": "10 10\r\n"}, {"input": "4 3\r\n5 3 10 3\r\nxzjhplrzkbbzkypfazf\r\nxzjhplrzkbbzkypfazf\r\nh\r\n", "output": "9 25\r\n"}, {"input": "5 5\r\n10 10 6 7 9\r\niyerjkvzibxhllkeuagptnoqrzm\r\nvzibxhllkeuag\r\niyerjkvzibxhllkeuagptnoqrzm\r\nnoq\r\nnoq\r\n", "output": "35 49\r\n"}, {"input": "10 8\r\n19 18 20 13 19 13 11 10 19 16\r\nkayangqlsqmcd\r\nqls\r\nqydawlbludrgrjfjrhd\r\nfjrh\r\nqls\r\nqls\r\nrnmmayh\r\nkayangqlsqmcd\r\n", "output": "94 154\r\n"}, {"input": "5 15\r\n61 56 95 42 85\r\noq\r\ndwxivk\r\ntxdxzsfdj\r\noq\r\noq\r\ndwxivk\r\ntxdxzsfdj\r\ndwxivk\r\ntxdxzsfdj\r\nk\r\nk\r\ndwxivk\r\noq\r\nk\r\ntxdxzsfdj\r\n", "output": "891 1132\r\n"}, {"input": "12 18\r\n42 44 69 16 81 64 12 68 70 75 75 67\r\nfm\r\nqamklzfmrjnqgdspwfasjnplg\r\nqamklzfmrjnqgdspwfasjnplg\r\nqamklzfmrjnqgdspwfasjnplg\r\nl\r\nl\r\nl\r\nfm\r\nqamklzfmrjnqgdspwfasjnplg\r\nl\r\nnplgwotfm\r\np\r\nl\r\namklzfm\r\ntkpubqamklzfmrjn\r\npwf\r\nfm\r\np\r\n", "output": "606 1338\r\n"}]
false
stdio
null
true
755/B
755
B
PyPy 3
TESTS
20
249
5,939,200
91597448
# cook your dish here n, m = map(int, input().split()) Ene = set() Pol = set() for i in range(n): Pol.add(input()) for i in range(m): Ene.add(input()) comm_ele = 0 for num in Pol: if num in Ene: comm_ele += 1 if n>m: print("YES") elif n<m: print("NO") else: if comm_ele == n: print("NO") if n%2 ==0 else print("YES") elif comm_ele == 0: print("NO") else: print("NO") if comm_ele%2 == n%2 else print("YES")
33
46
819,200
138663974
n, m = input().split() n = int(n) m = int(m) words = set() repeated = 0 p = 0 e = 0 for i in range(n): x = input() words.add(x) p += 1 for i in range(m): x = input() if x in words: p -= 1 repeated += 1 else: e += 1 if repeated >= 2: if repeated % 2 == 1: p += repeated//2 + 1 e += repeated//2 else: p += repeated//2 e += repeated//2 else: p += repeated if p > e: print('YES') else: print('NO')
8VC Venture Cup 2017 - Elimination Round
CF
2,017
1
256
PolandBall and Game
PolandBall is playing a game with EnemyBall. The rules are simple. Players have to say words in turns. You cannot say a word which was already said. PolandBall starts. The Ball which can't say a new word loses. You're given two lists of words familiar to PolandBall and EnemyBall. Can you determine who wins the game, if both play optimally?
The first input line contains two integers n and m (1 ≤ n, m ≤ 103) — number of words PolandBall and EnemyBall know, respectively. Then n strings follow, one per line — words familiar to PolandBall. Then m strings follow, one per line — words familiar to EnemyBall. Note that one Ball cannot know a word more than once (strings are unique), but some words can be known by both players. Each word is non-empty and consists of no more than 500 lowercase English alphabet letters.
In a single line of print the answer — "YES" if PolandBall wins and "NO" otherwise. Both Balls play optimally.
null
In the first example PolandBall knows much more words and wins effortlessly. In the second example if PolandBall says kremowka first, then EnemyBall cannot use that word anymore. EnemyBall can only say wiedenska. PolandBall says wadowicka and wins.
[{"input": "5 1\npolandball\nis\na\ncool\ncharacter\nnope", "output": "YES"}, {"input": "2 2\nkremowka\nwadowicka\nkremowka\nwiedenska", "output": "YES"}, {"input": "1 2\na\na\nb", "output": "NO"}]
1,100
["binary search", "data structures", "games", "greedy", "sortings", "strings"]
33
[{"input": "5 1\r\npolandball\r\nis\r\na\r\ncool\r\ncharacter\r\nnope\r\n", "output": "YES"}, {"input": "2 2\r\nkremowka\r\nwadowicka\r\nkremowka\r\nwiedenska\r\n", "output": "YES"}, {"input": "1 2\r\na\r\na\r\nb\r\n", "output": "NO"}, {"input": "2 2\r\na\r\nb\r\nb\r\nc\r\n", "output": "YES"}, {"input": "2 1\r\nc\r\na\r\na\r\n", "output": "YES"}, {"input": "3 3\r\nab\r\nbc\r\ncd\r\ncd\r\ndf\r\nfg\r\n", "output": "YES"}, {"input": "3 3\r\nc\r\na\r\nb\r\na\r\nd\r\ng\r\n", "output": "YES"}, {"input": "1 1\r\naa\r\naa\r\n", "output": "YES"}, {"input": "2 1\r\na\r\nb\r\na\r\n", "output": "YES"}, {"input": "6 5\r\na\r\nb\r\nc\r\nd\r\ne\r\nf\r\nf\r\ne\r\nd\r\nz\r\ny\r\n", "output": "YES"}, {"input": "3 2\r\na\r\nb\r\nc\r\nd\r\ne\r\n", "output": "YES"}]
false
stdio
null
true
175/C
175
C
PyPy 3-64
TESTS
3
92
0
166126786
'''\https://codeforces.com/contest/175/problem/C 输入 n(≤100) 表示 n 种怪物,然后输入 n 行,每行两个数字表示怪物的数量 (≤1e9),和怪物的分数(≤1000)。 然后输入 t(≤100) 和一个长为 t 的数组 p,下标从 1 开始,1≤p[1]<p[2]<...<p[t]≤1e12, 表示在你累计击败 p[i] 个怪物之后,得分系数将变为 i+1(初始得分系数为 1)。 击败一只怪物的得分 = 怪物的分数 * 当前得分系数。 你可以按照任意顺序打怪,输出击败所有怪物后的最大得分。 输入 1 5 10 2 3 6 输出 70 解释 前三只怪物得分系数为 1,后两只怪物得分系数为 2,总分=3*10*1+2*10*2=70 输入 2 3 8 5 10 1 20 输出 74 解释 所有怪物的得分系数均为 1,总分=3*8*1+5*10*1=74 ''' n = int(input()) arr = [] for _ in range(n): a, b = map(int, input().split()) arr.append((a, b)) arr.sort(key=lambda x: x[1]) t = int(input()) nums = list(map(int, input().split())) res = 0 j = 0 pre = 0 for k, v in arr: while j<t and pre + k >= nums[j]: res += (nums[j] - pre) * v * (j + 1) k -= nums[j] - pre pre = nums[j] j += 1 if j>=t:break res += v * k * (j + 1) nums[j] -= k pre += k print(res)
90
218
0
119527083
n=int(input()) a=[list(map(int,input().split()))[::-1] for i in range(n)] t=int(input()) p=list(map(int,input().split())) b=0 i=0 a.sort() c=0 for j in range(n): while i<t and p[i]-b<=a[j][1]: c+=(p[i]-b)*(i+1)*a[j][0] a[j][1]-=p[i]-b b=p[i] i+=1 c+=a[j][1]*(i+1)*a[j][0] b+=a[j][1] print(c)
Codeforces Round 115
CF
2,012
2
256
Geometry Horse
Vasya plays the Geometry Horse. The game goal is to destroy geometric figures of the game world. A certain number of points is given for destroying each figure depending on the figure type and the current factor value. There are n types of geometric figures. The number of figures of type ki and figure cost ci is known for each figure type. A player gets ci·f points for destroying one figure of type i, where f is the current factor. The factor value can be an integer number from 1 to t + 1, inclusive. At the beginning of the game the factor value is equal to 1. The factor is set to i + 1 after destruction of pi (1 ≤ i ≤ t) figures, so the (pi + 1)-th figure to be destroyed is considered with factor equal to i + 1. Your task is to determine the maximum number of points Vasya can get after he destroys all figures. Take into account that Vasya is so tough that he can destroy figures in any order chosen by him.
The first line contains the only integer number n (1 ≤ n ≤ 100) — the number of figure types. Each of the following n lines contains two integer numbers ki and ci (1 ≤ ki ≤ 109, 0 ≤ ci ≤ 1000), separated with space — the number of figures of the i-th type and the cost of one i-type figure, correspondingly. The next line contains the only integer number t (1 ≤ t ≤ 100) — the number that describe the factor's changes. The next line contains t integer numbers pi (1 ≤ p1 < p2 < ... < pt ≤ 1012), separated with spaces. Please, do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator.
Print the only number — the maximum number of points Vasya can get.
null
In the first example Vasya destroys three figures first and gets 3·1·10 = 30 points. Then the factor will become equal to 2 and after destroying the last two figures Vasya will get 2·2·10 = 40 points. As a result Vasya will get 70 points. In the second example all 8 figures will be destroyed with factor 1, so Vasya will get (3·8 + 5·10)·1 = 74 points.
[{"input": "1\n5 10\n2\n3 6", "output": "70"}, {"input": "2\n3 8\n5 10\n1\n20", "output": "74"}]
1,600
["greedy", "implementation", "sortings", "two pointers"]
90
[{"input": "1\r\n5 10\r\n2\r\n3 6\r\n", "output": "70"}, {"input": "2\r\n3 8\r\n5 10\r\n1\r\n20\r\n", "output": "74"}, {"input": "3\r\n10 3\r\n20 2\r\n30 1\r\n3\r\n30 50 60\r\n", "output": "200"}, {"input": "1\r\n100 1000\r\n1\r\n1\r\n", "output": "199000"}, {"input": "1\r\n1 1000\r\n1\r\n1\r\n", "output": "1000"}, {"input": "1\r\n1 1000\r\n1\r\n2\r\n", "output": "1000"}, {"input": "2\r\n1000000000 1000\r\n1 1\r\n1\r\n10\r\n", "output": "1999999991001"}, {"input": "6\r\n5 9\r\n63 3\r\n30 4\r\n25 6\r\n48 2\r\n29 9\r\n8\r\n105 137 172 192 632 722 972 981\r\n", "output": "2251"}, {"input": "7\r\n9902 9\r\n5809 6\r\n2358 0\r\n6868 7\r\n9630 2\r\n8302 10\r\n9422 3\r\n4\r\n2148 4563 8488 9575\r\n", "output": "1481866"}, {"input": "9\r\n60129 6\r\n44235 10\r\n13131 8\r\n2012 2\r\n27536 4\r\n38950 6\r\n39080 2\r\n13892 3\r\n48709 0\r\n1\r\n23853\r\n", "output": "2751752"}, {"input": "10\r\n3466127 4\r\n3477072 1\r\n9690039 9\r\n9885165 6\r\n2559197 4\r\n3448456 3\r\n9169542 1\r\n6915866 2\r\n1702896 10\r\n8934261 5\r\n6\r\n3041416 5811699 5920083 8250213 8694306 8899250\r\n", "output": "1843409345"}, {"input": "4\r\n4059578 5\r\n20774712 1\r\n64867825 7\r\n5606945 8\r\n1\r\n337246111\r\n", "output": "540002937"}, {"input": "1\r\n555 100\r\n10\r\n1 2 3 4 5 6 7 8 9 10\r\n", "output": "605000"}, {"input": "1\r\n1 1\r\n1\r\n100000000000\r\n", "output": "1"}, {"input": "12\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\n1000000000 11\r\n1000000000 12\r\n1\r\n10000000000\r\n", "output": "101000000000"}, {"input": "11\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\n1000000000 11\r\n1\r\n10000000000\r\n", "output": "77000000000"}, {"input": "1\r\n10 10\r\n3\r\n1 2 3\r\n", "output": "340"}, {"input": "1\r\n1000000000 1000\r\n2\r\n3 6\r\n", "output": "2999999991000"}, {"input": "1\r\n100 100\r\n3\r\n3 6 9\r\n", "output": "38200"}, {"input": "1\r\n10 1\r\n10\r\n1 2 3 4 5 6 7 8 9 10\r\n", "output": "55"}, {"input": "1\r\n10 10\r\n5\r\n1 2 3 4 5\r\n", "output": "450"}, {"input": "10\r\n10 10\r\n10 10\r\n10 10\r\n10 10\r\n10 10\r\n10 10\r\n10 10\r\n10 10\r\n10 10\r\n10 10\r\n1\r\n1\r\n", "output": "1990"}, {"input": "1\r\n10 10\r\n2\r\n3 6\r\n", "output": "210"}, {"input": "10\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1000 1000\r\n1\r\n1000000\r\n", "output": "10000000"}]
false
stdio
null
true
12/C
12
C
Python 3
TESTS
1
61
307,200
105277235
n,k=map(int,input().split()) a=[int(x) for x in input().split()] a.sort() dic={} for _ in range(k): s=input() if s in dic: dic[s]+=1 else: dic[s]=1 t=0 cnt=0 sorted(dic.items(),reverse=True) for i in dic: cnt+=a[t]*dic[i] t+=1 print(cnt,end=" ") t=n-1 cnt=0 for i in dic: cnt+=a[t]*dic[i] t-=1 print(cnt)
25
46
0
147141946
n,m=list(map(int,input().split())) p=list(map(int,input().split())) f=[] k=[] for i in range(m): f.append(input()) for i in set(f): k.append(f.count(i)) k.sort(reverse=True) p.sort() h=0 min=0 max=0 for i in k: min+=i*p[h] h+=1 max+=i*p[-h] print(min,max)
Codeforces Beta Round 12 (Div 2 Only)
ICPC
2,010
1
256
Fruits
The spring is coming and it means that a lot of fruits appear on the counters. One sunny day little boy Valera decided to go shopping. He made a list of m fruits he wanted to buy. If Valera want to buy more than one fruit of some kind, he includes it into the list several times. When he came to the fruit stall of Ashot, he saw that the seller hadn't distributed price tags to the goods, but put all price tags on the counter. Later Ashot will attach every price tag to some kind of fruits, and Valera will be able to count the total price of all fruits from his list. But Valera wants to know now what can be the smallest total price (in case of the most «lucky» for him distribution of price tags) and the largest total price (in case of the most «unlucky» for him distribution of price tags).
The first line of the input contains two integer number n and m (1 ≤ n, m ≤ 100) — the number of price tags (which is equal to the number of different kinds of fruits that Ashot sells) and the number of items in Valera's list. The second line contains n space-separated positive integer numbers. Each of them doesn't exceed 100 and stands for the price of one fruit of some kind. The following m lines contain names of the fruits from the list. Each name is a non-empty string of small Latin letters which length doesn't exceed 32. It is guaranteed that the number of distinct fruits from the list is less of equal to n. Also it is known that the seller has in stock all fruits that Valera wants to buy.
Print two numbers a and b (a ≤ b) — the minimum and the maximum possible sum which Valera may need to buy all fruits from his list.
null
null
[{"input": "5 3\n4 2 1 10 5\napple\norange\nmango", "output": "7 19"}, {"input": "6 5\n3 5 1 6 8 1\npeach\ngrapefruit\nbanana\norange\norange", "output": "11 30"}]
1,100
["greedy", "implementation", "sortings"]
25
[{"input": "5 3\r\n4 2 1 10 5\r\napple\r\norange\r\nmango\r\n", "output": "7 19\r\n"}, {"input": "6 5\r\n3 5 1 6 8 1\r\npeach\r\ngrapefruit\r\nbanana\r\norange\r\norange\r\n", "output": "11 30\r\n"}, {"input": "2 2\r\n91 82\r\neiiofpfpmemlakcystpun\r\nmcnzeiiofpfpmemlakcystpunfl\r\n", "output": "173 173\r\n"}, {"input": "1 4\r\n1\r\nu\r\nu\r\nu\r\nu\r\n", "output": "4 4\r\n"}, {"input": "3 3\r\n4 2 3\r\nwivujdxzjm\r\nawagljmtc\r\nwivujdxzjm\r\n", "output": "7 11\r\n"}, {"input": "3 4\r\n10 10 10\r\nodchpcsdhldqnkbhwtwnx\r\nldqnkbhwtwnxk\r\nodchpcsdhldqnkbhwtwnx\r\nldqnkbhwtwnxk\r\n", "output": "40 40\r\n"}, {"input": "3 1\r\n14 26 22\r\naag\r\n", "output": "14 26\r\n"}, {"input": "2 2\r\n5 5\r\ndcypj\r\npiyqiagzjlvbhgfndhfu\r\n", "output": "10 10\r\n"}, {"input": "4 3\r\n5 3 10 3\r\nxzjhplrzkbbzkypfazf\r\nxzjhplrzkbbzkypfazf\r\nh\r\n", "output": "9 25\r\n"}, {"input": "5 5\r\n10 10 6 7 9\r\niyerjkvzibxhllkeuagptnoqrzm\r\nvzibxhllkeuag\r\niyerjkvzibxhllkeuagptnoqrzm\r\nnoq\r\nnoq\r\n", "output": "35 49\r\n"}, {"input": "10 8\r\n19 18 20 13 19 13 11 10 19 16\r\nkayangqlsqmcd\r\nqls\r\nqydawlbludrgrjfjrhd\r\nfjrh\r\nqls\r\nqls\r\nrnmmayh\r\nkayangqlsqmcd\r\n", "output": "94 154\r\n"}, {"input": "5 15\r\n61 56 95 42 85\r\noq\r\ndwxivk\r\ntxdxzsfdj\r\noq\r\noq\r\ndwxivk\r\ntxdxzsfdj\r\ndwxivk\r\ntxdxzsfdj\r\nk\r\nk\r\ndwxivk\r\noq\r\nk\r\ntxdxzsfdj\r\n", "output": "891 1132\r\n"}, {"input": "12 18\r\n42 44 69 16 81 64 12 68 70 75 75 67\r\nfm\r\nqamklzfmrjnqgdspwfasjnplg\r\nqamklzfmrjnqgdspwfasjnplg\r\nqamklzfmrjnqgdspwfasjnplg\r\nl\r\nl\r\nl\r\nfm\r\nqamklzfmrjnqgdspwfasjnplg\r\nl\r\nnplgwotfm\r\np\r\nl\r\namklzfm\r\ntkpubqamklzfmrjn\r\npwf\r\nfm\r\np\r\n", "output": "606 1338\r\n"}]
false
stdio
null
true
455/B
455
B
PyPy 3-64
TESTS
4
62
0
214539881
n, k = [int(x) for x in input().split()] l = [] for i in range(n): l.append(len(input())) if min(l) % 2 == 0: print("Second") else: if k % 2 == 0: print("Second") else: print("First")
75
218
26,521,600
230712465
# LUOGU_RID: 132709230 # pypy3 from collections import * from itertools import * from functools import * from bisect import * from heapq import * import sys from math import gcd IN = lambda: sys.stdin.readline().rstrip("\r\n") PN = lambda x: sys.stdout.write(x) I = lambda: int(IN()) S = lambda: IN().split() M = lambda: map(int, IN().split()) L = lambda: list(map(int, IN().split())) G = lambda: map(lambda x: int(x) - 1, IN().split()) tr = [[0 for i in range(26)] for _ in range(100010)] son = [0 for i in range(100010)] idx = 0 def ins(s): p = 0 global idx for c in s: u = ord(c) - ord('a') if tr[p][u] == 0: idx += 1 tr[p][u] = idx son[p] += 1 p = tr[p][u] n, k = M() for _ in range(n): ins(IN()) def win(p): for i in range(26): if tr[p][i] != 0 and not win(tr[p][i]): return True return False def lose(p): for i in range(26): if tr[p][i] != 0 and not lose(tr[p][i]): return True if son[p] == 0: return True return False f, g = win(0), lose(0) if f: if g: print("First") else: print("First" if k % 2 == 1 else "Second") else: print("Second")
Codeforces Round 260 (Div. 1)
CF
2,014
1
256
A Lot of Games
Andrew, Fedor and Alex are inventive guys. Now they invent the game with strings for two players. Given a group of n non-empty strings. During the game two players build the word together, initially the word is empty. The players move in turns. On his step player must add a single letter in the end of the word, the resulting word must be prefix of at least one string from the group. A player loses if he cannot move. Andrew and Alex decided to play this game k times. The player who is the loser of the i-th game makes the first move in the (i + 1)-th game. Guys decided that the winner of all games is the player who wins the last (k-th) game. Andrew and Alex already started the game. Fedor wants to know who wins the game if both players will play optimally. Help him.
The first line contains two integers, n and k (1 ≤ n ≤ 105; 1 ≤ k ≤ 109). Each of the next n lines contains a single non-empty string from the given group. The total length of all strings from the group doesn't exceed 105. Each string of the group consists only of lowercase English letters.
If the player who moves first wins, print "First", otherwise print "Second" (without the quotes).
null
null
[{"input": "2 3\na\nb", "output": "First"}, {"input": "3 1\na\nb\nc", "output": "First"}, {"input": "1 2\nab", "output": "Second"}]
1,900
["dfs and similar", "dp", "games", "implementation", "strings", "trees"]
75
[{"input": "2 3\r\na\r\nb\r\n", "output": "First\r\n"}, {"input": "3 1\r\na\r\nb\r\nc\r\n", "output": "First\r\n"}, {"input": "1 2\r\nab\r\n", "output": "Second\r\n"}, {"input": "5 6\r\nabas\r\ndsfdf\r\nabacaba\r\ndartsidius\r\nkolobok\r\n", "output": "Second\r\n"}, {"input": "4 2\r\naaaa\r\nbbbb\r\nccccc\r\ndumbavumba\r\n", "output": "First\r\n"}, {"input": "3 8\r\nso\r\nbad\r\ntest\r\n", "output": "First\r\n"}, {"input": "5 2\r\nwelcome\r\nto\r\nthe\r\nmatrix\r\nneo\r\n", "output": "First\r\n"}, {"input": "6 4\r\ndog\r\ncat\r\ncow\r\nhot\r\nice\r\nlol\r\n", "output": "Second\r\n"}, {"input": "4 8\r\nla\r\na\r\nz\r\nka\r\n", "output": "First\r\n"}, {"input": "3 2\r\nop\r\nhop\r\ncop\r\n", "output": "First\r\n"}, {"input": "3 3\r\nabasdfabab\r\nabaaasdfdsf\r\nasdfaba\r\n", "output": "Second\r\n"}, {"input": "2 2\r\naba\r\naa\r\n", "output": "Second\r\n"}, {"input": "4 1\r\naa\r\naba\r\nba\r\nbba\r\n", "output": "Second\r\n"}, {"input": "1 3\r\nab\r\n", "output": "Second\r\n"}, {"input": "3 3\r\naa\r\nabb\r\ncc\r\n", "output": "Second\r\n"}]
false
stdio
null
true
10/A
10
A
PyPy 3
TESTS
16
280
0
84325696
n,p1,p2,p3,t1,t2 = map(int,input().split()) a=0 b=0 for c in range(n): l,r = map(int,input().split()) if(c==0): b+=(r-l)*p1 a=r else: diff = l-a if(diff > 0): if(diff <= t1): b+= diff*p1 else: if(diff < t1+t2 and diff >= t1): b+= t1*p1 + (diff-t1)*p2 elif(diff > t1+t2): b+= t1*p1 + (t2)*p2 + (diff-(t1+t2))*p3 b+=(r-l)*p1 a=r print(b)
30
62
0
186159465
def main(): n, p1, p2, p3, t1, t2 = [int(_) for _ in input().split()] minutes = [0, 0, 0] r_prev = 0 for _ in range(n): l, r = [int(_) for _ in input().split()] minutes[0] += r - l if r_prev: rest = l - r_prev if rest <= t1: minutes[0] += rest else: minutes[0] += t1 rest -= t1 if rest <= t2: minutes[1] += rest else: minutes[1] += t2 rest -= t2 minutes[2] += rest r_prev = r print(minutes[0] * p1 + minutes[1] * p2 + minutes[2] * p3) if __name__ == "__main__": main()
Codeforces Beta Round 10
ICPC
2,010
1
256
Power Consumption Calculation
Tom is interested in power consumption of his favourite laptop. His laptop has three modes. In normal mode laptop consumes P1 watt per minute. T1 minutes after Tom moved the mouse or touched the keyboard for the last time, a screensaver starts and power consumption changes to P2 watt per minute. Finally, after T2 minutes from the start of the screensaver, laptop switches to the "sleep" mode and consumes P3 watt per minute. If Tom moves the mouse or touches the keyboard when the laptop is in the second or in the third mode, it switches to the first (normal) mode. Tom's work with the laptop can be divided into n time periods [l1, r1], [l2, r2], ..., [ln, rn]. During each interval Tom continuously moves the mouse and presses buttons on the keyboard. Between the periods Tom stays away from the laptop. Find out the total amount of power consumed by the laptop during the period [l1, rn].
The first line contains 6 integer numbers n, P1, P2, P3, T1, T2 (1 ≤ n ≤ 100, 0 ≤ P1, P2, P3 ≤ 100, 1 ≤ T1, T2 ≤ 60). The following n lines contain description of Tom's work. Each i-th of these lines contains two space-separated integers li and ri (0 ≤ li < ri ≤ 1440, ri < li + 1 for i < n), which stand for the start and the end of the i-th period of work.
Output the answer to the problem.
null
null
[{"input": "1 3 2 1 5 10\n0 10", "output": "30"}, {"input": "2 8 4 2 5 10\n20 30\n50 100", "output": "570"}]
900
["implementation"]
30
[{"input": "1 3 2 1 5 10\r\n0 10\r\n", "output": "30"}, {"input": "2 8 4 2 5 10\r\n20 30\r\n50 100\r\n", "output": "570"}, {"input": "3 15 9 95 39 19\r\n873 989\r\n1003 1137\r\n1172 1436\r\n", "output": "8445"}, {"input": "4 73 2 53 58 16\r\n51 52\r\n209 242\r\n281 407\r\n904 945\r\n", "output": "52870"}, {"input": "5 41 20 33 43 4\r\n46 465\r\n598 875\r\n967 980\r\n1135 1151\r\n1194 1245\r\n", "output": "46995"}, {"input": "6 88 28 100 53 36\r\n440 445\r\n525 614\r\n644 844\r\n1238 1261\r\n1305 1307\r\n1425 1434\r\n", "output": "85540"}, {"input": "7 46 61 55 28 59\r\n24 26\r\n31 61\r\n66 133\r\n161 612\r\n741 746\r\n771 849\r\n1345 1357\r\n", "output": "67147"}, {"input": "8 83 18 30 28 5\r\n196 249\r\n313 544\r\n585 630\r\n718 843\r\n1040 1194\r\n1207 1246\r\n1268 1370\r\n1414 1422\r\n", "output": "85876"}, {"input": "9 31 65 27 53 54\r\n164 176\r\n194 210\r\n485 538\r\n617 690\r\n875 886\r\n888 902\r\n955 957\r\n1020 1200\r\n1205 1282\r\n", "output": "38570"}]
false
stdio
null
true
59/E
59
E
PyPy 3
TESTS
6
902
51,712,000
92545286
import os, sys from io import IOBase, BytesIO py2 = round(0.5) if py2: from future_builtins import ascii, filter, hex, map, oct, zip range = xrange BUFSIZE = 8192 class FastIO(BytesIO): newlines = 0 def __init__(self, file): self._file = file self._fd = file.fileno() self.writable = "x" in file.mode or "w" in file.mode self.write = super(FastIO, self).write if self.writable else None def _fill(self): s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0]) return s def read(self): while self._fill(): pass return super(FastIO, self).read() def readline(self): while self.newlines == 0: s = self._fill() self.newlines = s.count(b"\n") + (not s) self.newlines -= 1 return super(FastIO, self).readline() def flush(self): if self.writable: os.write(self._fd, self.getvalue()) self.truncate(0), self.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable if py2: self.write = self.buffer.write self.read = self.buffer.read self.readline = self.buffer.readline else: 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") # Cout implemented in Python import sys class ostream: def __lshift__(self, a): sys.stdout.write(str(a)) return self cout = ostream() endl = "\n" import heapq def solve(): n, m, k_num = map(int, input().split()) edge_list = {x: [] for x in range(1, n + 1)} k = {} for i in range(m): u, v = map(int, input().split()) edge_list[u].append(v) edge_list[v].append(u) for i in range(k_num): a, b, c = map(int, input().split()) k[(a, b, c)] = True hq = [(0, 1)] dist = [[float("inf")] * (n + 1) for i in range(n + 1)] dist[0][1] = 0 path = [[0] * (n + 1) for i in range(n + 1)] Found = None while hq: ele = heapq.heappop(hq) # visited[ele[1]] = True for edge in edge_list[ele[1]]: # print(ele[0], ele[1], edge) if dist[ele[0]][ele[1]] + 1 < dist[ele[1]][edge] and (ele[0], ele[1], edge) not in k: dist[ele[1]][edge] = dist[ele[0]][ele[1]] + 1 path[ele[1]][edge] = ele[0] heapq.heappush(hq, (ele[1], edge)) if edge == n: Found = ele[1] break if not Found: cout<<-1<<"\n" return # print(path) pt = [n, Found] st = path[Found][n] while st: pt.append(st) st = path[pt[-1]][pt[-2]] cout<<len(pt) - 1<<"\n" cout<< " ".join(map(str, pt[::-1]))<<"\n" def main(): solve() if __name__ == "__main__": main()
43
1,090
32,256,000
222025354
import sys from collections import deque readline = sys.stdin.readline N = 0 graph = [] forbbiden = set() def read_input(): global N n, m, k = [int(w) for w in readline().split()] for _ in range(n + 1): graph.append([]) for _ in range(m): u, v = [int(w) for w in readline().split()] graph[u].append(v) graph[v].append(u) for _ in range(k): forbbiden.add(tuple(int(w) for w in readline().split())) N = n def solve()->list: def get_trail(t:tuple) -> list: buf = [] while t in seen: buf.append(t) t = seen[t] return [tmp[0] for tmp in buf][::-1] + [N] seen = {} que = deque() # parent, node, step que.append((0, 1, 0)) while que: p, node, step = que.popleft() for neigh in graph[node]: if (p, node, neigh) in forbbiden or (node, neigh) in seen: continue seen[(node, neigh)] = (p, node) if neigh == N: return get_trail((node, neigh)) que.append((node, neigh, step + 1)) return [] def write_output(result: list): print(len(result) - 1) if result: print(*result) read_input() write_output(solve())
Codeforces Beta Round 55 (Div. 2)
CF
2,011
3
256
Shortest Path
In Ancient Berland there were n cities and m two-way roads of equal length. The cities are numbered with integers from 1 to n inclusively. According to an ancient superstition, if a traveller visits three cities ai, bi, ci in row, without visiting other cities between them, a great disaster awaits him. Overall there are k such city triplets. Each triplet is ordered, which means that, for example, you are allowed to visit the cities in the following order: ai, ci, bi. Vasya wants to get from the city 1 to the city n and not fulfil the superstition. Find out which minimal number of roads he should take. Also you are required to find one of his possible path routes.
The first line contains three integers n, m, k (2 ≤ n ≤ 3000, 1 ≤ m ≤ 20000, 0 ≤ k ≤ 105) which are the number of cities, the number of roads and the number of the forbidden triplets correspondingly. Then follow m lines each containing two integers xi, yi (1 ≤ xi, yi ≤ n) which are the road descriptions. The road is described by the numbers of the cities it joins. No road joins a city with itself, there cannot be more than one road between a pair of cities. Then follow k lines each containing three integers ai, bi, ci (1 ≤ ai, bi, ci ≤ n) which are the forbidden triplets. Each ordered triplet is listed mo more than one time. All three cities in each triplet are distinct. City n can be unreachable from city 1 by roads.
If there are no path from 1 to n print -1. Otherwise on the first line print the number of roads d along the shortest path from the city 1 to the city n. On the second line print d + 1 numbers — any of the possible shortest paths for Vasya. The path should start in the city 1 and end in the city n.
null
null
[{"input": "4 4 1\n1 2\n2 3\n3 4\n1 3\n1 4 3", "output": "2\n1 3 4"}, {"input": "3 1 0\n1 2", "output": "-1"}, {"input": "4 4 2\n1 2\n2 3\n3 4\n1 3\n1 2 3\n1 3 4", "output": "4\n1 3 2 3 4"}]
2,000
["graphs", "shortest paths"]
43
[{"input": "4 4 1\r\n1 2\r\n2 3\r\n3 4\r\n1 3\r\n1 4 3\r\n", "output": "2\r\n1 3 4\r\n"}, {"input": "3 1 0\r\n1 2\r\n", "output": "-1\r\n"}, {"input": "4 4 2\r\n1 2\r\n2 3\r\n3 4\r\n1 3\r\n1 2 3\r\n1 3 4\r\n", "output": "4\r\n1 3 2 3 4\r\n"}, {"input": "4 4 1\r\n1 2\r\n2 3\r\n3 4\r\n1 3\r\n1 2 3\r\n", "output": "2\r\n1 3 4\r\n"}, {"input": "2 1 0\r\n1 2\r\n", "output": "1\r\n1 2\r\n"}, {"input": "4 4 1\r\n1 2\r\n2 3\r\n3 4\r\n1 3\r\n1 3 4\r\n", "output": "3\r\n1 2 3 4\r\n"}, {"input": "3 2 0\r\n1 2\r\n3 2\r\n", "output": "2\r\n1 2 3\r\n"}, {"input": "3 2 1\r\n1 2\r\n3 2\r\n1 2 3\r\n", "output": "-1\r\n"}, {"input": "4 4 4\r\n1 2\r\n2 3\r\n3 4\r\n1 3\r\n1 2 3\r\n1 3 4\r\n1 2 4\r\n1 3 2\r\n", "output": "-1\r\n"}]
false
stdio
null
true
59/E
59
E
Python 3
TESTS
6
342
10,649,600
177773499
import sys import sys, threading from heapq import heapify, heappop, heappush from math import log10, floor, pow, gcd, sqrt, inf from collections import defaultdict, deque, Counter input = sys.stdin.readline ############ ---- Input Functions ---- ############ def inp(): return(int(input())) def inlt(): return(list(map(int,input().split()))) def insr(): s = input() return(list(s[:len(s) - 1])) def invr(): return(map(int,input().split())) def main(): cities, roads, triplet = inlt() edges = defaultdict(list) triplets = set() for _ in range(roads): pointA, pointB = inlt() edges[pointA].append(pointB) edges[pointB].append(pointA) for _ in range(triplet): triplets.add(tuple(inlt())) # print(edges) visited = defaultdict(int) final = int("1"*(cities+1),2) qu = deque([[(1, 1<<1 ),"1"]]) counts = 0 while qu: x, count = qu.popleft() node, comb = x # print(comb) if len(count) >= 3: danger = (int(count[-3]), int(count[-2]), int(count[-1])) if danger in triplets: continue if x[0] == cities: print(len(count) -1) for i in count: print(i, end=" ") return if visited[x] == 2: continue visited[x] += 1 for i in edges[node]: tmp = 1<<i count += str(i) qu.append([(i,tmp|comb),count]) count = count[:-1] print(-1) main() # threading.stack_size(1 << 27) # sys.setrecursionlimit(1 << 30) # main_thread = threading.Thread(target=main) # main_thread.start() # main_thread.join()
43
1,090
42,393,600
154832292
import sys input = sys.stdin.readline from collections import deque n, m, k = list(map(int, input().split())) g = [[] for _ in range(n+1)] for _ in range(m): i, j = list(map(int, input().split())) g[i].append(j) g[j].append(i) bad_seq = set() for _ in range(k): i, j, k = list(map(int, input().split())) bad_seq.add((i,j,k)) visited_edges = set() q = deque() q.append([1, [-1]]) output = [-1] while q: j, path = q.popleft() if j == n: output = path[1:] + [j] break for elem in g[j]: if (j, elem) not in visited_edges: if (path[-1], j, elem) not in bad_seq: visited_edges.add((j, elem)) q.append((elem, path + [j])) if len(output) > 1: print(len(output)-1) print(*output) else: print(-1)
Codeforces Beta Round 55 (Div. 2)
CF
2,011
3
256
Shortest Path
In Ancient Berland there were n cities and m two-way roads of equal length. The cities are numbered with integers from 1 to n inclusively. According to an ancient superstition, if a traveller visits three cities ai, bi, ci in row, without visiting other cities between them, a great disaster awaits him. Overall there are k such city triplets. Each triplet is ordered, which means that, for example, you are allowed to visit the cities in the following order: ai, ci, bi. Vasya wants to get from the city 1 to the city n and not fulfil the superstition. Find out which minimal number of roads he should take. Also you are required to find one of his possible path routes.
The first line contains three integers n, m, k (2 ≤ n ≤ 3000, 1 ≤ m ≤ 20000, 0 ≤ k ≤ 105) which are the number of cities, the number of roads and the number of the forbidden triplets correspondingly. Then follow m lines each containing two integers xi, yi (1 ≤ xi, yi ≤ n) which are the road descriptions. The road is described by the numbers of the cities it joins. No road joins a city with itself, there cannot be more than one road between a pair of cities. Then follow k lines each containing three integers ai, bi, ci (1 ≤ ai, bi, ci ≤ n) which are the forbidden triplets. Each ordered triplet is listed mo more than one time. All three cities in each triplet are distinct. City n can be unreachable from city 1 by roads.
If there are no path from 1 to n print -1. Otherwise on the first line print the number of roads d along the shortest path from the city 1 to the city n. On the second line print d + 1 numbers — any of the possible shortest paths for Vasya. The path should start in the city 1 and end in the city n.
null
null
[{"input": "4 4 1\n1 2\n2 3\n3 4\n1 3\n1 4 3", "output": "2\n1 3 4"}, {"input": "3 1 0\n1 2", "output": "-1"}, {"input": "4 4 2\n1 2\n2 3\n3 4\n1 3\n1 2 3\n1 3 4", "output": "4\n1 3 2 3 4"}]
2,000
["graphs", "shortest paths"]
43
[{"input": "4 4 1\r\n1 2\r\n2 3\r\n3 4\r\n1 3\r\n1 4 3\r\n", "output": "2\r\n1 3 4\r\n"}, {"input": "3 1 0\r\n1 2\r\n", "output": "-1\r\n"}, {"input": "4 4 2\r\n1 2\r\n2 3\r\n3 4\r\n1 3\r\n1 2 3\r\n1 3 4\r\n", "output": "4\r\n1 3 2 3 4\r\n"}, {"input": "4 4 1\r\n1 2\r\n2 3\r\n3 4\r\n1 3\r\n1 2 3\r\n", "output": "2\r\n1 3 4\r\n"}, {"input": "2 1 0\r\n1 2\r\n", "output": "1\r\n1 2\r\n"}, {"input": "4 4 1\r\n1 2\r\n2 3\r\n3 4\r\n1 3\r\n1 3 4\r\n", "output": "3\r\n1 2 3 4\r\n"}, {"input": "3 2 0\r\n1 2\r\n3 2\r\n", "output": "2\r\n1 2 3\r\n"}, {"input": "3 2 1\r\n1 2\r\n3 2\r\n1 2 3\r\n", "output": "-1\r\n"}, {"input": "4 4 4\r\n1 2\r\n2 3\r\n3 4\r\n1 3\r\n1 2 3\r\n1 3 4\r\n1 2 4\r\n1 3 2\r\n", "output": "-1\r\n"}]
false
stdio
null
true
46/C
46
C
Python 3
TESTS
2
92
0
115054827
a = int(input()) s = input() d = s.count('H') p = [] for i in range(len(s)): if i+d > len(s): n = d+i - len(s) m = d - n h = s[:m] + s[-n:] k = h.count("T") p.append(k) else: h = s[i:d+i] k = h.count("T") mi = a for i in range(len(p)): if p[i] < mi: mi = p[i] print(mi)
27
92
0
196760951
# LUOGU_RID: 104227169 n = int(input()) s = input() print(min((s + s)[i:i+s.count('H')].count('T') for i in range(n)))
School Personal Contest #2 (Winter Computer School 2010/11) - Codeforces Beta Round 43 (ACM-ICPC Rules)
ICPC
2,010
2
256
Hamsters and Tigers
Today there is going to be an unusual performance at the circus — hamsters and tigers will perform together! All of them stand in circle along the arena edge and now the trainer faces a difficult task: he wants to swap the animals' positions so that all the hamsters stood together and all the tigers also stood together. The trainer swaps the animals in pairs not to create a mess. He orders two animals to step out of the circle and swap places. As hamsters feel highly uncomfortable when tigers are nearby as well as tigers get nervous when there's so much potential prey around (consisting not only of hamsters but also of yummier spectators), the trainer wants to spend as little time as possible moving the animals, i.e. he wants to achieve it with the minimal number of swaps. Your task is to help him.
The first line contains number n (2 ≤ n ≤ 1000) which indicates the total number of animals in the arena. The second line contains the description of the animals' positions. The line consists of n symbols "H" and "T". The "H"s correspond to hamsters and the "T"s correspond to tigers. It is guaranteed that at least one hamster and one tiger are present on the arena. The animals are given in the order in which they are located circle-wise, in addition, the last animal stands near the first one.
Print the single number which is the minimal number of swaps that let the trainer to achieve his goal.
null
In the first example we shouldn't move anybody because the animals of each species already stand apart from the other species. In the second example you may swap, for example, the tiger in position 2 with the hamster in position 5 and then — the tiger in position 9 with the hamster in position 7.
[{"input": "3\nHTH", "output": "0"}, {"input": "9\nHTHTHTHHT", "output": "2"}]
1,600
["two pointers"]
27
[{"input": "3\r\nHTH\r\n", "output": "0\r\n"}, {"input": "9\r\nHTHTHTHHT\r\n", "output": "2\r\n"}, {"input": "2\r\nTH\r\n", "output": "0\r\n"}, {"input": "4\r\nHTTH\r\n", "output": "0\r\n"}, {"input": "4\r\nHTHT\r\n", "output": "1\r\n"}, {"input": "7\r\nTTTHTTT\r\n", "output": "0\r\n"}, {"input": "8\r\nHHTHHTHH\r\n", "output": "1\r\n"}, {"input": "13\r\nHTTTHHHTTTTHH\r\n", "output": "3\r\n"}, {"input": "20\r\nTTHTHTHHTHTTHHTTTHHH\r\n", "output": "4\r\n"}, {"input": "35\r\nTTTTTTHTTHTTTTTHTTTTTTTTTTTHTHTTTTT\r\n", "output": "3\r\n"}, {"input": "120\r\nTTTTTTTHTHTHTTTTTHTHTTTTHTTTTTTTTTTTTTTTTTTTTHTTHTTTTHTTHTTTTTTTTTTTTTTTHTTTTTTHTHTTHTTTTTTHTTTTTTTTTHTTHTTTTHTTTHTTTTTH\r\n", "output": "14\r\n"}, {"input": "19\r\nHHHHHHHHHHHHHTTTHHH\r\n", "output": "0\r\n"}, {"input": "87\r\nHTHHTTHHHHTHHHHHTTTHHTHHHHTTTTHHHTTHHTHTHTHHTTHTHHTHTHTTHHHTTTTTHTTHHHHHHTHHTHHTHTTHTHH\r\n", "output": "17\r\n"}, {"input": "178\r\nTHHHTHTTTHTTHTTHHHHHTTTHTTHHTHTTTHTHTTTTTHHHTHTHHHTHHHTTTTTTTTHHHHTTHHTHHHHTHTTTHHHHHHTHHTHTTHTHTTTTTTTTTHHTTHHTHTTHHTHHHHHTTHHTTHHTTHHHTTHHTTTTHTHHHTHTTHTHTTTHHHHTHHTHHHTHTTTTTT\r\n", "output": "40\r\n"}]
false
stdio
null
true
10/A
10
A
Python 3
TESTS
28
218
307,200
73820064
n,p1,p2,p3,t1,t2=map(int,input().split()) ans=0 l1,r1=0,0 for i in range(n): l2,r2=map(int,input().split()) ans+=(r2-l2)*p1 if l1==0: l1,r1=l2,r2 continue t=l2-r1 if t<=t1: ans+=t*p1 else: ans+=t1*p1 t-=t1 if t<=t2: ans+=t*p2 else: ans+=t2*p2 t-=t2 ans+=t*p3 l1,r1=l2,r2 print(ans)
30
62
0
195229808
n,p1,p2,p3,T1,T2=map(int,input().split()) total,previousTime=0,-1 for k in range(n): start,finish=map(int,input().split()) if previousTime<0: previousTime=start total +=p1*(finish-start) timeIdle=start-previousTime if timeIdle>T1+T2: total +=(timeIdle-T1-T2)*p3 timeIdle=T1+T2 if timeIdle>T1: total +=(timeIdle-T1)*p2 timeIdle=T1 total +=timeIdle*p1 previousTime=finish print(total)
Codeforces Beta Round 10
ICPC
2,010
1
256
Power Consumption Calculation
Tom is interested in power consumption of his favourite laptop. His laptop has three modes. In normal mode laptop consumes P1 watt per minute. T1 minutes after Tom moved the mouse or touched the keyboard for the last time, a screensaver starts and power consumption changes to P2 watt per minute. Finally, after T2 minutes from the start of the screensaver, laptop switches to the "sleep" mode and consumes P3 watt per minute. If Tom moves the mouse or touches the keyboard when the laptop is in the second or in the third mode, it switches to the first (normal) mode. Tom's work with the laptop can be divided into n time periods [l1, r1], [l2, r2], ..., [ln, rn]. During each interval Tom continuously moves the mouse and presses buttons on the keyboard. Between the periods Tom stays away from the laptop. Find out the total amount of power consumed by the laptop during the period [l1, rn].
The first line contains 6 integer numbers n, P1, P2, P3, T1, T2 (1 ≤ n ≤ 100, 0 ≤ P1, P2, P3 ≤ 100, 1 ≤ T1, T2 ≤ 60). The following n lines contain description of Tom's work. Each i-th of these lines contains two space-separated integers li and ri (0 ≤ li < ri ≤ 1440, ri < li + 1 for i < n), which stand for the start and the end of the i-th period of work.
Output the answer to the problem.
null
null
[{"input": "1 3 2 1 5 10\n0 10", "output": "30"}, {"input": "2 8 4 2 5 10\n20 30\n50 100", "output": "570"}]
900
["implementation"]
30
[{"input": "1 3 2 1 5 10\r\n0 10\r\n", "output": "30"}, {"input": "2 8 4 2 5 10\r\n20 30\r\n50 100\r\n", "output": "570"}, {"input": "3 15 9 95 39 19\r\n873 989\r\n1003 1137\r\n1172 1436\r\n", "output": "8445"}, {"input": "4 73 2 53 58 16\r\n51 52\r\n209 242\r\n281 407\r\n904 945\r\n", "output": "52870"}, {"input": "5 41 20 33 43 4\r\n46 465\r\n598 875\r\n967 980\r\n1135 1151\r\n1194 1245\r\n", "output": "46995"}, {"input": "6 88 28 100 53 36\r\n440 445\r\n525 614\r\n644 844\r\n1238 1261\r\n1305 1307\r\n1425 1434\r\n", "output": "85540"}, {"input": "7 46 61 55 28 59\r\n24 26\r\n31 61\r\n66 133\r\n161 612\r\n741 746\r\n771 849\r\n1345 1357\r\n", "output": "67147"}, {"input": "8 83 18 30 28 5\r\n196 249\r\n313 544\r\n585 630\r\n718 843\r\n1040 1194\r\n1207 1246\r\n1268 1370\r\n1414 1422\r\n", "output": "85876"}, {"input": "9 31 65 27 53 54\r\n164 176\r\n194 210\r\n485 538\r\n617 690\r\n875 886\r\n888 902\r\n955 957\r\n1020 1200\r\n1205 1282\r\n", "output": "38570"}]
false
stdio
null
true
449/B
449
B
PyPy 3
TESTS
3
967
51,712,000
98030169
#=============================================================================================== #importing some useful libraries. from __future__ import division, print_function from fractions import Fraction import sys import os from io import BytesIO, IOBase from functools import cmp_to_key # from itertools import * from heapq import * from math import gcd, factorial,floor,ceil,sqrt from copy import deepcopy from collections import deque from bisect import bisect_left as bl from bisect import bisect_right as br from bisect import bisect #============================================================================================== #fast I/O 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") def print(*args, **kwargs): """Prints the values to a stream, or to sys.stdout by default.""" sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() if sys.version_info[0] < 3: sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) # inp = lambda: sys.stdin.readline().rstrip("\r\n") #=============================================================================================== ### START ITERATE RECURSION ### from types import GeneratorType def iterative(f, stack=[]): def wrapped_func(*args, **kwargs): if stack: return f(*args, **kwargs) to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) continue stack.pop() if not stack: break to = stack[-1].send(to) return to return wrapped_func #### END ITERATE RECURSION #### #=============================================================================================== #some shortcuts def inp(): return sys.stdin.readline().rstrip("\r\n") #for fast input def out(var): sys.stdout.write(str(var)) #for fast output, always take string def lis(): return list(map(int, inp().split())) def stringlis(): return list(map(str, inp().split())) def sep(): return map(int, inp().split()) def strsep(): return map(str, inp().split()) # def graph(vertex): return [[] for i in range(0,vertex+1)] def zerolist(n): return [0]*n def nextline(): out("\n") #as stdout.write always print sring. def testcase(t): for pp in range(t): solve(pp) def printlist(a) : for p in range(0,len(a)): out(str(a[p]) + ' ') def google(p): print('Case #'+str(p)+': ',end='') def lcm(a,b): return (a*b)//gcd(a,b) def power(x, y, p) : y%=(p-1) #not so sure about this. used when y>p-1. if p is prime. res = 1 # Initialize result x = x % p # Update x if it is more , than or equal to p if (x == 0) : return 0 while (y > 0) : if ((y & 1) == 1) : # If y is odd, multiply, x with result res = (res * x) % p y = y >> 1 # y = y/2 x = (x * x) % p return res def ncr(n,r): return factorial(n) // (factorial(r) * factorial(max(n - r, 1))) def isPrime(n) : if (n <= 1) : return False if (n <= 3) : return True 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 inf = pow(10,20) mod = 10**9+7 #=============================================================================================== # code here ;)) def djkistra(g,st,dist,lol,vis): #g contains b,dist(a to b) and dist is initiaalised by 10**9 initiallly pq = [] dist[st] = 0 heappush(pq,(0,st)) while(len(pq) != 0): curr = heappop(pq)[1] for i in range(0,len(g[curr])): b = g[curr][i][0] w = g[curr][i][1] if(dist[b] > dist[curr] + w): dist[b] = dist[curr]+w heappush(pq,(dist[b],b)) def modif_djkistra(g,dist,usedtrains): h = [] for i in range(len(g)): if(dist[i] != inf): heappush(h,(dist[i],i)) while(len(h) != 0): d,curr = heappop(h) if(d != dist[curr]): #dublicate train with larger length continue for to,newd in g[curr]: if(newd+d<=dist[to]): usedtrains[to] = False if(dist[to] > newd+d): heappush(h,(newd+d,to)) dist[to] = newd+d def solve(case): n,m,k = sep() dist = [inf]*n;dist[0] = 0 g = [[] for i in range(n)] for i in range(m): a,b,c = sep() a-=1 b-=1 g[a].append((b,c)) g[b].append((a,c)) have = [] for i in range(k): a,b = sep() a-=1 dist[a] = min(dist[a],b) g[0].append((a,b)) g[a].append((0,b)) have.append(a) usedtrain = [True]*n modif_djkistra(g,dist,usedtrain) cnt = 0 have = list(set(have)) for i in range(n): if(usedtrain[i]): cnt+=1 # print(cnt) print(k - cnt) testcase(1) # testcase(int(inp()))
45
1,886
133,222,400
202495416
import os import sys import threading from io import BytesIO, IOBase from heapq import heappush, heappop, heapify from collections import defaultdict, deque, Counter from bisect import bisect_left as bl from bisect import bisect_right as br # threading.stack_size(10**8) # sys.setrecursionlimit(10**6) def ri(): return int(input()) def rs(): return input() def rl(): return list(map(int, input().split())) def rls(): return list(input().split()) def main(): n,m,k=rl() g=[[] for i in range(n+1)] for _ in range(m): u,v,x=rl() g[u].append((v,x,1)) g[v].append((u,x,1)) t=[[] for i in range(n+1)] for _ in range(k): s,y=rl() t[s].append(y) g[1].append((s,y,2)) g[s].append((1,y,2)) dis=[float('inf') for i in range(n+1)] dis[1]=0 vis=[False for i in range(n+1)] q=[] heappush(q,(0,1)) while q: cd,cn=heappop(q) if vis[cn]:continue vis[cn]=True for (nn,nw,ty) in g[cn]: nd=nw+dis[cn] if nd<dis[nn] and not vis[nn]: dis[nn]=nd heappush(q,(dis[nn],nn)) res=0 for i in range(2,n+1): gr=0 gt=0 for (nn,nw,ty) in g[i]: if dis[nn]+nw==dis[i]: if ty==2:gt+=1 else:gr+=1 if gr>0:res+=len(t[i]) else:res+=len(t[i])-1 print(res) pass BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._file = 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) def input(): return sys.stdin.readline().rstrip("\r\n") if __name__ == "__main__": main() # threading.Thread(target=main).start()
Codeforces Round 257 (Div. 1)
CF
2,014
2
256
Jzzhu and Cities
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi. Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
Output a single integer representing the maximum number of the train routes which can be closed.
null
null
[{"input": "5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5", "output": "2"}, {"input": "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3", "output": "2"}]
2,000
["graphs", "greedy", "shortest paths"]
45
[{"input": "5 5 3\r\n1 2 1\r\n2 3 2\r\n1 3 3\r\n3 4 4\r\n1 5 5\r\n3 5\r\n4 5\r\n5 5\r\n", "output": "2\r\n"}, {"input": "2 2 3\r\n1 2 2\r\n2 1 3\r\n2 1\r\n2 2\r\n2 3\r\n", "output": "2\r\n"}, {"input": "5 4 3\r\n1 2 999999999\r\n2 3 1000000000\r\n3 4 529529529\r\n5 1 524524524\r\n5 524444444\r\n5 529999999\r\n2 1000000000\r\n", "output": "2\r\n"}, {"input": "3 2 5\r\n1 2 2\r\n2 3 4\r\n3 5\r\n3 5\r\n3 5\r\n3 6\r\n3 7\r\n", "output": "4\r\n"}, {"input": "5 5 3\r\n1 2 999999999\r\n2 3 1000000000\r\n3 4 529529529\r\n5 1 524524524\r\n5 3 1000000000\r\n5 524444444\r\n5 529999999\r\n2 1000000000\r\n", "output": "2\r\n"}, {"input": "2 1 5\r\n1 2 4\r\n2 3\r\n2 5\r\n2 4\r\n2 4\r\n2 5\r\n", "output": "4\r\n"}, {"input": "3 3 6\r\n1 2 499999999\r\n2 3 500000000\r\n1 3 999999999\r\n2 499999999\r\n2 500000000\r\n2 499999999\r\n3 999999999\r\n3 1000000000\r\n3 1000000000\r\n", "output": "6\r\n"}, {"input": "2 1 1\r\n1 2 1\r\n2 1000000000\r\n", "output": "1\r\n"}, {"input": "3 2 2\r\n1 2 4\r\n2 3 4\r\n2 2\r\n3 6\r\n", "output": "1\r\n"}, {"input": "5 5 2\r\n1 2 100\r\n2 3 100\r\n3 4 100\r\n4 5 20\r\n2 5 5\r\n5 50\r\n4 1\r\n", "output": "1\r\n"}, {"input": "3 2 2\r\n1 2 100\r\n2 3 1\r\n2 1\r\n3 3\r\n", "output": "1\r\n"}]
false
stdio
null
true
59/E
59
E
PyPy 3-64
TESTS
15
808
34,918,400
203823090
# author: birsnot - Nardos from collections import defaultdict, deque from heapq import heappop, heappush from sys import setrecursionlimit, stdin from threading import Thread, stack_size # def input(): return stdin.readline()[:-1] input = stdin.readline def I(): return int(input()) def II(): return map(int, input().split()) def IL(): return list(map(int, input().split())) def SIL(): return sorted(map(int, input().split())) def solve(): N, M, K = II() adj = defaultdict(list) for _ in range(M): u, v = II() adj[u].append(v) adj[v].append(u) ks = set() for _ in range(K): u, v, w = II() ks.add((u, v, w)) pq = [(0, 1, 0)] parents = defaultdict(list) i = 10*M while pq and i: d, v, p = heappop(pq) heappush(parents[v], (d, -p)) i -= 1 for u in adj[v]: if (p, v, u) not in ks: if u == N: ans = deque([v, u]) while v != 1: _, p = heappop(parents[v]) if (-p, v, u) not in ks: u, v = v, -p ans.appendleft(v) print(d + 1) print(*ans) return heappush(pq, (d + 1, u, v)) print(-1) def main(): # T = I() T = 1 for ___ in range(T): solve() main() # setrecursionlimit(100050) # stack_size(1<<27) # _mt = Thread(target=main) # _mt.start() # _mt.join()
43
1,496
161,792,000
226494619
import sys from array import array # noqa: F401 from collections import deque def input(): return sys.stdin.buffer.readline().decode('utf-8') n, m, k = map(int, input().split()) adj = [[] for _ in range(n)] for u, v in (map(int, input().split()) for _ in range(m)): adj[u - 1].append(v - 1) adj[v - 1].append(u - 1) forbidden = [set() for _ in range(n)] for a, b, c in (map(int, input().split()) for _ in range(k)): forbidden[c - 1].add((a - 1, b - 1)) inf = 10**9 dp = [[inf] * n for _ in range(n)] prev = [[-1] * n for _ in range(n)] dp[0][0] = 0 dq = deque([(0, 0, 0)]) while dq: v, p, cost = dq.popleft() if dp[v][p] < cost: continue for dest in adj[v]: if (p, v) in forbidden[dest] or dp[dest][v] <= cost + 1: continue dp[dest][v] = cost + 1 prev[dest][v] = p dq.append((dest, v, cost + 1)) shortest = inf p, v = -1, -1 for i in range(n): if shortest > dp[-1][i]: shortest = dp[-1][i] p, v = i, n - 1 if shortest == inf: print(-1) else: ans = [] while v != 0: ans.append(v + 1) v, p = p, prev[v][p] ans.append(1) print(shortest) print(*reversed(ans))
Codeforces Beta Round 55 (Div. 2)
CF
2,011
3
256
Shortest Path
In Ancient Berland there were n cities and m two-way roads of equal length. The cities are numbered with integers from 1 to n inclusively. According to an ancient superstition, if a traveller visits three cities ai, bi, ci in row, without visiting other cities between them, a great disaster awaits him. Overall there are k such city triplets. Each triplet is ordered, which means that, for example, you are allowed to visit the cities in the following order: ai, ci, bi. Vasya wants to get from the city 1 to the city n and not fulfil the superstition. Find out which minimal number of roads he should take. Also you are required to find one of his possible path routes.
The first line contains three integers n, m, k (2 ≤ n ≤ 3000, 1 ≤ m ≤ 20000, 0 ≤ k ≤ 105) which are the number of cities, the number of roads and the number of the forbidden triplets correspondingly. Then follow m lines each containing two integers xi, yi (1 ≤ xi, yi ≤ n) which are the road descriptions. The road is described by the numbers of the cities it joins. No road joins a city with itself, there cannot be more than one road between a pair of cities. Then follow k lines each containing three integers ai, bi, ci (1 ≤ ai, bi, ci ≤ n) which are the forbidden triplets. Each ordered triplet is listed mo more than one time. All three cities in each triplet are distinct. City n can be unreachable from city 1 by roads.
If there are no path from 1 to n print -1. Otherwise on the first line print the number of roads d along the shortest path from the city 1 to the city n. On the second line print d + 1 numbers — any of the possible shortest paths for Vasya. The path should start in the city 1 and end in the city n.
null
null
[{"input": "4 4 1\n1 2\n2 3\n3 4\n1 3\n1 4 3", "output": "2\n1 3 4"}, {"input": "3 1 0\n1 2", "output": "-1"}, {"input": "4 4 2\n1 2\n2 3\n3 4\n1 3\n1 2 3\n1 3 4", "output": "4\n1 3 2 3 4"}]
2,000
["graphs", "shortest paths"]
43
[{"input": "4 4 1\r\n1 2\r\n2 3\r\n3 4\r\n1 3\r\n1 4 3\r\n", "output": "2\r\n1 3 4\r\n"}, {"input": "3 1 0\r\n1 2\r\n", "output": "-1\r\n"}, {"input": "4 4 2\r\n1 2\r\n2 3\r\n3 4\r\n1 3\r\n1 2 3\r\n1 3 4\r\n", "output": "4\r\n1 3 2 3 4\r\n"}, {"input": "4 4 1\r\n1 2\r\n2 3\r\n3 4\r\n1 3\r\n1 2 3\r\n", "output": "2\r\n1 3 4\r\n"}, {"input": "2 1 0\r\n1 2\r\n", "output": "1\r\n1 2\r\n"}, {"input": "4 4 1\r\n1 2\r\n2 3\r\n3 4\r\n1 3\r\n1 3 4\r\n", "output": "3\r\n1 2 3 4\r\n"}, {"input": "3 2 0\r\n1 2\r\n3 2\r\n", "output": "2\r\n1 2 3\r\n"}, {"input": "3 2 1\r\n1 2\r\n3 2\r\n1 2 3\r\n", "output": "-1\r\n"}, {"input": "4 4 4\r\n1 2\r\n2 3\r\n3 4\r\n1 3\r\n1 2 3\r\n1 3 4\r\n1 2 4\r\n1 3 2\r\n", "output": "-1\r\n"}]
false
stdio
null
true
169/A
169
A
Python 3
TESTS
6
77
7,372,800
37508191
n, a, b = map(int,input().split()) h = list(map(int, input().split())) h.sort() ans = h[b] - h[a] if a == b: ans = h[b] - h[a-1] print(ans)
29
46
0
144504780
n,a,b=map(int, input().split()) h = sorted([int(i) for i in input().split()]) print(h[b]-h[b-1])
VK Cup 2012 Round 2 (Unofficial Div. 2 Edition)
CF
2,012
2
256
Chores
Petya and Vasya are brothers. Today is a special day for them as their parents left them home alone and commissioned them to do n chores. Each chore is characterized by a single parameter — its complexity. The complexity of the i-th chore equals hi. As Petya is older, he wants to take the chores with complexity larger than some value x (hi > x) to leave to Vasya the chores with complexity less than or equal to x (hi ≤ x). The brothers have already decided that Petya will do exactly a chores and Vasya will do exactly b chores (a + b = n). In how many ways can they choose an integer x so that Petya got exactly a chores and Vasya got exactly b chores?
The first input line contains three integers n, a and b (2 ≤ n ≤ 2000; a, b ≥ 1; a + b = n) — the total number of chores, the number of Petya's chores and the number of Vasya's chores. The next line contains a sequence of integers h1, h2, ..., hn (1 ≤ hi ≤ 109), hi is the complexity of the i-th chore. The numbers in the given sequence are not necessarily different. All numbers on the lines are separated by single spaces.
Print the required number of ways to choose an integer value of x. If there are no such ways, print 0.
null
In the first sample the possible values of x are 3, 4 or 5. In the second sample it is impossible to find such x, that Petya got 3 chores and Vasya got 4.
[{"input": "5 2 3\n6 2 3 100 1", "output": "3"}, {"input": "7 3 4\n1 1 9 1 1 1 1", "output": "0"}]
800
["sortings"]
29
[{"input": "5 2 3\r\n6 2 3 100 1\r\n", "output": "3\r\n"}, {"input": "7 3 4\r\n1 1 9 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "2 1 1\r\n10 2\r\n", "output": "8\r\n"}, {"input": "2 1 1\r\n7 7\r\n", "output": "0\r\n"}, {"input": "2 1 1\r\n1 1000000000\r\n", "output": "999999999\r\n"}, {"input": "3 1 2\r\n6 5 5\r\n", "output": "1\r\n"}, {"input": "3 2 1\r\n10 10 8\r\n", "output": "2\r\n"}, {"input": "8 3 5\r\n42 55 61 72 83 10 22 33\r\n", "output": "6\r\n"}, {"input": "10 5 5\r\n1 2 3 4 5 999999999 999999998 999999997 999999996 999999995\r\n", "output": "999999990\r\n"}, {"input": "4 1 3\r\n10 8 7 3\r\n", "output": "2\r\n"}, {"input": "4 2 2\r\n402 10 10 402\r\n", "output": "392\r\n"}, {"input": "4 1 3\r\n10 402 402 10\r\n", "output": "0\r\n"}, {"input": "4 3 1\r\n100 100 200 200\r\n", "output": "0\r\n"}, {"input": "102 101 1\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\r\n", "output": "0\r\n"}]
false
stdio
null
true
169/A
169
A
Python 3
TESTS
6
93
0
78791133
n , a , b = map(int,input().split()) l = list(map(int,input().split())) l.sort() if a!=b: print(l[b]-l[a]) else:print(l[b]-l[a-1])
29
46
0
145054398
n, older, younger = (list(map(int,input().split()))) chores = (list(map(int,input().split()))) chores.sort(reverse = True) c_old = chores[older-1] chores.sort() c_young = chores[younger-1] print((c_old)-(c_young))
VK Cup 2012 Round 2 (Unofficial Div. 2 Edition)
CF
2,012
2
256
Chores
Petya and Vasya are brothers. Today is a special day for them as their parents left them home alone and commissioned them to do n chores. Each chore is characterized by a single parameter — its complexity. The complexity of the i-th chore equals hi. As Petya is older, he wants to take the chores with complexity larger than some value x (hi > x) to leave to Vasya the chores with complexity less than or equal to x (hi ≤ x). The brothers have already decided that Petya will do exactly a chores and Vasya will do exactly b chores (a + b = n). In how many ways can they choose an integer x so that Petya got exactly a chores and Vasya got exactly b chores?
The first input line contains three integers n, a and b (2 ≤ n ≤ 2000; a, b ≥ 1; a + b = n) — the total number of chores, the number of Petya's chores and the number of Vasya's chores. The next line contains a sequence of integers h1, h2, ..., hn (1 ≤ hi ≤ 109), hi is the complexity of the i-th chore. The numbers in the given sequence are not necessarily different. All numbers on the lines are separated by single spaces.
Print the required number of ways to choose an integer value of x. If there are no such ways, print 0.
null
In the first sample the possible values of x are 3, 4 or 5. In the second sample it is impossible to find such x, that Petya got 3 chores and Vasya got 4.
[{"input": "5 2 3\n6 2 3 100 1", "output": "3"}, {"input": "7 3 4\n1 1 9 1 1 1 1", "output": "0"}]
800
["sortings"]
29
[{"input": "5 2 3\r\n6 2 3 100 1\r\n", "output": "3\r\n"}, {"input": "7 3 4\r\n1 1 9 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "2 1 1\r\n10 2\r\n", "output": "8\r\n"}, {"input": "2 1 1\r\n7 7\r\n", "output": "0\r\n"}, {"input": "2 1 1\r\n1 1000000000\r\n", "output": "999999999\r\n"}, {"input": "3 1 2\r\n6 5 5\r\n", "output": "1\r\n"}, {"input": "3 2 1\r\n10 10 8\r\n", "output": "2\r\n"}, {"input": "8 3 5\r\n42 55 61 72 83 10 22 33\r\n", "output": "6\r\n"}, {"input": "10 5 5\r\n1 2 3 4 5 999999999 999999998 999999997 999999996 999999995\r\n", "output": "999999990\r\n"}, {"input": "4 1 3\r\n10 8 7 3\r\n", "output": "2\r\n"}, {"input": "4 2 2\r\n402 10 10 402\r\n", "output": "392\r\n"}, {"input": "4 1 3\r\n10 402 402 10\r\n", "output": "0\r\n"}, {"input": "4 3 1\r\n100 100 200 200\r\n", "output": "0\r\n"}, {"input": "102 101 1\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\r\n", "output": "0\r\n"}]
false
stdio
null
true
754/D
754
D
PyPy 3-64
TESTS
4
77
2,150,400
174838101
import sys from bisect import bisect_left from collections import * from itertools import * from math import * from array import * from functools import lru_cache import heapq import bisect import random import io, os if sys.hexversion == 50923504: sys.stdin = open('cfinput.txt') RI = lambda: map(int, sys.stdin.buffer.readline().split()) RS = lambda: map(bytes.decode, sys.stdin.buffer.readline().strip().split()) RILST = lambda: list(RI()) MOD = 10 ** 9 + 7 """https://codeforces.com/problemset/problem/754/D 输入 n, k (1≤k≤n≤3e5) 和 n 个闭区间,区间左右端点在 [-1e9,1e9] 内,区间的编号从 1 开始。 请你选择 k 个区间,使得这 k 个区间的交集的大小尽量大(只考虑整数),输出这个最大值,以及对应的区间的编号。 思考题:如果改成并集呢? 输入 4 2 1 100 40 70 120 130 125 180 输出 31 1 2 输入 3 2 1 12 15 20 25 30 输出 0 1 2 输入 5 2 1 10 5 15 14 50 30 70 99 100 输出 21 3 4 """ # ms def solve(n, k, rs, ps): rs.sort() ans = 0 out = [] left = 0 h = [] for i, (l, r) in enumerate(rs): while len(h) > k - 1: heapq.heappop(h) if len(h) == k - 1: d = min(h[0][0], r) - l + 1 if d > ans: ans = d out = h[:] left = i + 1 heapq.heappush(h, (r, i + 1)) print(ans) if ans == 0: return print(*range(1, k + 1)) print(' '.join(map(lambda x: str(x[1]), out)), left) if __name__ == '__main__': n, k = RI() rs = [] ps = [] for _ in range(n): l, r = RI() ps.append(l) ps.append(r) rs.append((l, r)) solve(n, k, rs, ps)
77
3,353
112,844,800
174818794
import os, sys from io import BytesIO, IOBase from collections import defaultdict, deque, Counter from bisect import bisect_left, bisect_right from heapq import heappush, heappop from functools import lru_cache from itertools import accumulate import math import sys from sys import stdout # sys.setrecursionlimit(10 ** 6) # 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") # sys.setrecursionlimit(2000) ii = lambda: int(input()) mii = lambda: map(int, input().split()) lmii = lambda: list(map(int, input().split())) mod = 998244353 c2i = lambda c: ord(c) - ord('a') i2c = lambda i: chr(ord('a') + i) def solve(): n, m = mii() itv = [] for i in range(n): l, r = mii() itv.append([l, r, i]) itv.sort() length = -1 u = -1 hr = [] for i in range(len(itv)): l, r, j = itv[i] heappush(hr, [r, l, j]) if len(hr) > m: heappop(hr) v = hr[0][0] - l if v > length and len(hr) == m: length = v u = i if u == -1: print(0) print(*list(range(1, m + 1))) else: hr = [] for i in range(len(itv)): l, r, j = itv[i] heappush(hr, [r, l, j]) if len(hr) > m: heappop(hr) if i == u: print(length + 1) print(*[hr[k][2] + 1 for k in range(len(hr))]) for _ in range(1): solve()
Codeforces Round 390 (Div. 2)
CF
2,017
4
256
Fedor and coupons
All our characters have hobbies. The same is true for Fedor. He enjoys shopping in the neighboring supermarket. The goods in the supermarket have unique integer ids. Also, for every integer there is a product with id equal to this integer. Fedor has n discount coupons, the i-th of them can be used with products with ids ranging from li to ri, inclusive. Today Fedor wants to take exactly k coupons with him. Fedor wants to choose the k coupons in such a way that the number of such products x that all coupons can be used with this product x is as large as possible (for better understanding, see examples). Fedor wants to save his time as well, so he asks you to choose coupons for him. Help Fedor!
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 3·105) — the number of coupons Fedor has, and the number of coupons he wants to choose. Each of the next n lines contains two integers li and ri ( - 109 ≤ li ≤ ri ≤ 109) — the description of the i-th coupon. The coupons can be equal.
In the first line print single integer — the maximum number of products with which all the chosen coupons can be used. The products with which at least one coupon cannot be used shouldn't be counted. In the second line print k distinct integers p1, p2, ..., pk (1 ≤ pi ≤ n) — the ids of the coupons which Fedor should choose. If there are multiple answers, print any of them.
null
In the first example if we take the first two coupons then all the products with ids in range [40, 70] can be bought with both coupons. There are 31 products in total. In the second example, no product can be bought with two coupons, that is why the answer is 0. Fedor can choose any two coupons in this example.
[{"input": "4 2\n1 100\n40 70\n120 130\n125 180", "output": "31\n1 2"}, {"input": "3 2\n1 12\n15 20\n25 30", "output": "0\n1 2"}, {"input": "5 2\n1 10\n5 15\n14 50\n30 70\n99 100", "output": "21\n3 4"}]
2,100
["binary search", "data structures", "greedy", "sortings"]
77
[{"input": "4 2\r\n1 100\r\n40 70\r\n120 130\r\n125 180\r\n", "output": "31\r\n1 2 \r\n"}, {"input": "3 2\r\n1 12\r\n15 20\r\n25 30\r\n", "output": "0\r\n1 2 \r\n"}, {"input": "5 2\r\n1 10\r\n5 15\r\n14 50\r\n30 70\r\n99 100\r\n", "output": "21\r\n3 4 \r\n"}, {"input": "7 6\r\n-8 6\r\n7 9\r\n-10 -5\r\n-6 10\r\n-7 -3\r\n5 8\r\n4 10\r\n", "output": "0\r\n1 2 3 4 5 6 \r\n"}, {"input": "9 6\r\n-7 -3\r\n-3 10\r\n-6 1\r\n-1 8\r\n-9 4\r\n-7 -6\r\n-5 -3\r\n-10 -2\r\n3 4\r\n", "output": "1\r\n1 2 3 5 7 8 \r\n"}, {"input": "7 7\r\n9 10\r\n-5 3\r\n-6 2\r\n1 6\r\n-9 6\r\n-10 7\r\n-7 -5\r\n", "output": "0\r\n1 2 3 4 5 6 7 \r\n"}, {"input": "23 2\r\n-629722518 -626148345\r\n739975524 825702590\r\n-360913153 -208398929\r\n76588954 101603025\r\n-723230356 -650106339\r\n-117490984 -101920679\r\n-39187628 -2520915\r\n717852164 720343632\r\n-611281114 -579708833\r\n-141791522 -122348148\r\n605078929 699430996\r\n-873386085 -820238799\r\n-922404067 -873522961\r\n7572046 13337057\r\n975081176 977171682\r\n901338407 964254238\r\n325388219 346712972\r\n505189756 516497863\r\n-425326983 -422098946\r\n520670681 522544433\r\n-410872616 -367919621\r\n359488350 447471156\r\n-566203447 -488202136\r\n", "output": "0\r\n1 2 \r\n"}, {"input": "24 21\r\n240694945 246896662\r\n240694930 246896647\r\n240695065 246896782\r\n240695050 246896767\r\n240695080 246896797\r\n240694960 246896677\r\n240694975 246896692\r\n240694825 246896542\r\n240694900 246896617\r\n240694915 246896632\r\n240694885 246896602\r\n240694855 246896572\r\n240694870 246896587\r\n240694795 246896512\r\n240695095 246896812\r\n240695125 246896842\r\n240695005 246896722\r\n240694990 246896707\r\n240695140 246896857\r\n240695020 246896737\r\n240695035 246896752\r\n240694840 246896557\r\n240694810 246896527\r\n240695110 246896827\r\n", "output": "6201418\r\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 17 18 20 21 22 23 \r\n"}, {"input": "1 1\r\n2 2\r\n", "output": "1\r\n1 \r\n"}, {"input": "1 1\r\n-1000000000 1000000000\r\n", "output": "2000000001\r\n1 \r\n"}, {"input": "2 1\r\n-1000000000 -1000000000\r\n1000000000 1000000000\r\n", "output": "1\r\n1 \r\n"}, {"input": "7 3\r\n3 3\r\n-6 -1\r\n6 7\r\n2 8\r\n3 10\r\n-8 0\r\n-3 10\r\n", "output": "6\r\n4 5 7 \r\n"}, {"input": "5 4\r\n4 7\r\n-4 2\r\n-7 -7\r\n-5 -2\r\n-8 -8\r\n", "output": "0\r\n1 2 3 4 \r\n"}, {"input": "7 7\r\n0 7\r\n9 9\r\n-10 -7\r\n5 8\r\n-10 4\r\n-7 0\r\n-3 5\r\n", "output": "0\r\n1 2 3 4 5 6 7 \r\n"}, {"input": "9 2\r\n5 10\r\n-10 -10\r\n0 10\r\n-6 3\r\n-8 7\r\n6 10\r\n-8 1\r\n5 7\r\n2 2\r\n", "output": "10\r\n5 7 \r\n"}, {"input": "9 5\r\n-2 1\r\n-6 9\r\n-7 -2\r\n5 7\r\n-10 -7\r\n-9 -2\r\n1 4\r\n-1 10\r\n4 8\r\n", "output": "0\r\n1 2 3 4 5 \r\n"}, {"input": "54 7\r\n-98 -39\r\n14 60\r\n-23 -5\r\n58 75\r\n14 16\r\n-40 20\r\n-6 10\r\n11 60\r\n-47 54\r\n-71 -17\r\n-48 -25\r\n-87 -46\r\n-10 99\r\n-97 -88\r\n-14 94\r\n-25 29\r\n-96 -92\r\n68 75\r\n-75 2\r\n12 84\r\n-47 3\r\n-88 49\r\n-37 88\r\n-61 -25\r\n36 67\r\n30 54\r\n12 31\r\n-71 60\r\n-18 -15\r\n-61 -47\r\n-51 -41\r\n-67 51\r\n26 37\r\n18 94\r\n-67 52\r\n-16 56\r\n-5 26\r\n27 57\r\n36 91\r\n-61 61\r\n71 86\r\n27 73\r\n-57 -39\r\n54 71\r\n-16 14\r\n-97 81\r\n-32 49\r\n-18 50\r\n-63 93\r\n51 70\r\n8 66\r\n43 45\r\n-2 99\r\n11 98\r\n", "output": "111\r\n22 28 32 35 40 46 49 \r\n"}, {"input": "52 18\r\n-50 54\r\n35 65\r\n67 82\r\n-87 -10\r\n-39 4\r\n-55 -18\r\n-27 90\r\n-42 73\r\n18 43\r\n70 85\r\n-85 -22\r\n-1 60\r\n-89 23\r\n-78 -75\r\n-14 69\r\n-69 50\r\n-93 74\r\n-10 45\r\n-81 -72\r\n-24 86\r\n-89 100\r\n25 70\r\n-65 -61\r\n-45 100\r\n-49 -23\r\n-74 -59\r\n-81 -15\r\n-58 47\r\n-65 -58\r\n-47 16\r\n-22 91\r\n-85 19\r\n-81 77\r\n79 87\r\n-31 88\r\n26 32\r\n11 90\r\n7 46\r\n64 83\r\n-51 -20\r\n-76 44\r\n-22 75\r\n45 84\r\n-98 46\r\n-20 78\r\n-88 -47\r\n-41 65\r\n2 93\r\n-66 69\r\n-73 94\r\n-85 -44\r\n-65 -23\r\n", "output": "67\r\n1 7 8 16 17 20 21 24 28 31 33 35 41 42 44 47 49 50 \r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] # Read input with open(input_path) as f: n, k = map(int, f.readline().split()) coupons = [] for _ in range(n): l, r = map(int, f.readline().split()) coupons.append((l, r)) # Read reference output (ans_ref) with open(output_path) as f: ans_ref = int(f.readline().strip()) # Read submission output with open(submission_path) as f: try: ans_sub = int(f.readline().strip()) if ans_sub != ans_ref: print(0) return selected = list(map(int, f.readline().strip().split())) except: print(0) return # Check selected coupons if len(selected) != k: print(0) return seen = set() for p in selected: if p < 1 or p > n or p in seen: print(0) return seen.add(p) # Compute intersection max_l = -float('inf') min_r = float('inf') for idx in selected: l, r = coupons[idx - 1] max_l = max(max_l, l) min_r = min(min_r, r) intersection = max(0, min_r - max_l + 1) print(1 if intersection == ans_ref else 0) if __name__ == "__main__": main()
true
489/A
489
A
PyPy 3-64
TESTS
5
46
0
192591472
n=int(input()) lis=list(map(int,input().split())) l=[] for i in lis: l.append(i) l.sort() index1=[] index2=[] count=0 pointer=0 for i in range(len(lis)): a=l[pointer] b=lis.index(a) if lis[i]>a: index1.append(b) index2.append(i) lis[i],lis[b]=lis[b],lis[i] count+=1 pointer+=1 print(count) for i in range(len(index1)): print(index1[i],index2[i])
22
77
819,200
8726157
from sys import stdin lines = list(filter(None, stdin.read().split('\n'))) def parseline(line): return list(map(int, line.split())) lines = list(map(parseline, lines)) n, = lines[0] ai = lines[1]; assert(len(ai) == n) sai = sorted((x, i) for i, x in enumerate(ai)) pos = list(range(n)) iai = list(range(n)) print(n) for j, e in enumerate(sai): x, i = e print(j, pos[i]) alpha = j beta = pos[i] assert pos[iai[j]] == j pos[i], pos[iai[j]] = j, pos[i] iai[alpha], iai[beta] = iai[beta], iai[alpha]
Codeforces Round 277.5 (Div. 2)
CF
2,014
1
256
SwapSort
In this problem your goal is to sort an array consisting of n integers in at most n swaps. For the given array find the sequence of swaps that makes the array sorted in the non-descending order. Swaps are performed consecutively, one after another. Note that in this problem you do not have to minimize the number of swaps — your task is to find any sequence that is no longer than n.
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of array elements. The second line contains elements of array: a0, a1, ..., an - 1 ( - 109 ≤ ai ≤ 109), where ai is the i-th element of the array. The elements are numerated from 0 to n - 1 from left to right. Some integers may appear in the array more than once.
In the first line print k (0 ≤ k ≤ n) — the number of swaps. Next k lines must contain the descriptions of the k swaps, one per line. Each swap should be printed as a pair of integers i, j (0 ≤ i, j ≤ n - 1), representing the swap of elements ai and aj. You can print indices in the pairs in any order. The swaps are performed in the order they appear in the output, from the first to the last. It is allowed to print i = j and swap the same pair of elements multiple times. If there are multiple answers, print any of them. It is guaranteed that at least one answer exists.
null
null
[{"input": "5\n5 2 5 1 4", "output": "2\n0 3\n4 2"}, {"input": "6\n10 20 20 40 60 60", "output": "0"}, {"input": "2\n101 100", "output": "1\n0 1"}]
1,200
["greedy", "implementation", "sortings"]
22
[{"input": "5\r\n5 2 5 1 4\r\n", "output": "2\r\n0 3\r\n4 2\r\n"}, {"input": "6\r\n10 20 20 40 60 60\r\n", "output": "0\r\n"}, {"input": "2\r\n101 100\r\n", "output": "1\r\n0 1\r\n"}, {"input": "1\r\n1000\r\n", "output": "0\r\n"}, {"input": "2\r\n1000000000 -1000000000\r\n", "output": "1\r\n0 1\r\n"}, {"input": "8\r\n5 2 6 8 3 1 6 8\r\n", "output": "4\r\n0 5\r\n4 2\r\n5 3\r\n6 5\r\n"}, {"input": "2\r\n200000000 199999999\r\n", "output": "1\r\n0 1\r\n"}, {"input": "3\r\n100000000 100000002 100000001\r\n", "output": "1\r\n1 2\r\n"}, {"input": "5\r\n1000000000 -10000000 0 8888888 7777777\r\n", "output": "3\r\n0 1\r\n2 1\r\n4 2\r\n"}, {"input": "5\r\n10 30 20 50 40\r\n", "output": "2\r\n1 2\r\n4 3\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] sub_path = sys.argv[3] with open(input_path) as f: n = int(f.readline().strip()) arr = list(map(int, f.readline().split())) with open(sub_path) as f: lines = f.readlines() if not lines: print(0) return try: k = int(lines[0].strip()) swaps = [] for line in lines[1:1 + k]: i, j = map(int, line.strip().split()) swaps.append((i, j)) except (ValueError, IndexError): print(0) return if not (0 <= k <= n): print(0) return for i, j in swaps: if not (0 <= i < n and 0 <= j < n): print(0) return current = arr.copy() for i, j in swaps: current[i], current[j] = current[j], current[i] sorted_arr = sorted(arr) if current == sorted_arr: print(1) else: print(0) if __name__ == "__main__": main()
true
479/D
479
D
PyPy 3-64
TESTS
2
46
0
221741730
import sys input = lambda: sys.stdin.readline().rstrip() N,L,x,y = map(int, input().split()) A = list(map(int, input().split())) seen = set() cx,cy=0,0 for a in A: if str(a-x) in seen: cx=1 if str(a-y) in seen: cy=1 seen.add(str(a)) if cx==cy==1: exit(print(0)) if cx==1: print(1) print(y) exit(0) if cy==1: print(1) print(x) exit(0) seen=set() t1,t2 = y-x,y+x for a in A: if str(a-t1) in seen: if a>=y: print(1) exit(print(a-y)) if a+x<=l: print(1) exit(print(a+x)) if str(a-t2) in seen: print(1) print(a-x)
111
202
10,854,400
8309555
import itertools import math def can_measure(a, d): return any(i + d in a for i in a) def main(): n, l, x, y = map(int, input().split()) a = set(map(int, input().split())) can_x = can_measure(a, x) can_y = can_measure(a, y) if can_x and can_y: print(0) elif can_x: print(1) print(y) elif can_y: print(1) print(x) else: for i in a: if i + x + y in a: print(1) print(i + x) break else: t = i + x - y in a if 0 <= i + x <= l and t: print(1) print(i + x) break; if 0 <= i - y <= l and t: print(1) print(i - y) break; else: print(2) print(x, y) if __name__ == "__main__": main()
Codeforces Round 274 (Div. 2)
CF
2,014
1
256
Long Jumps
Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has n marks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a1, a2, ..., an, where ai denotes the distance of the i-th mark from the origin (a1 = 0, an = l). Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 ≤ i ≤ j ≤ n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj - ai = d). Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y. Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler.
The first line contains four positive space-separated integers n, l, x, y (2 ≤ n ≤ 105, 2 ≤ l ≤ 109, 1 ≤ x < y ≤ l) — the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly. The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l), where ai shows the distance from the i-th mark to the origin.
In the first line print a single non-negative integer v — the minimum number of marks that you need to add on the ruler. In the second line print v space-separated integers p1, p2, ..., pv (0 ≤ pi ≤ l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them.
null
In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark. In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks. In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills.
[{"input": "3 250 185 230\n0 185 250", "output": "1\n230"}, {"input": "4 250 185 230\n0 20 185 250", "output": "0"}, {"input": "2 300 185 230\n0 300", "output": "2\n185 230"}]
1,700
["binary search", "greedy", "implementation"]
111
[{"input": "3 250 185 230\r\n0 185 250\r\n", "output": "1\r\n230\r\n"}, {"input": "4 250 185 230\r\n0 20 185 250\r\n", "output": "0\r\n"}, {"input": "2 300 185 230\r\n0 300\r\n", "output": "2\r\n185 230\r\n"}, {"input": "4 300 4 5\r\n0 6 7 300\r\n", "output": "1\r\n11\r\n"}, {"input": "2 100 30 70\r\n0 100\r\n", "output": "1\r\n30\r\n"}, {"input": "2 300 140 160\r\n0 300\r\n", "output": "1\r\n140\r\n"}, {"input": "4 300 1 2\r\n0 298 299 300\r\n", "output": "0\r\n"}, {"input": "3 350 150 160\r\n0 310 350\r\n", "output": "1\r\n150\r\n"}, {"input": "4 300 4 5\r\n0 298 299 300\r\n", "output": "1\r\n294\r\n"}, {"input": "19 180 117 148\r\n0 1 19 20 21 28 57 65 68 70 78 88 100 116 154 157 173 179 180\r\n", "output": "2\r\n117 148\r\n"}, {"input": "14 134 99 114\r\n0 6 8 19 50 61 69 83 84 96 111 114 125 134\r\n", "output": "1\r\n99\r\n"}, {"input": "18 187 27 157\r\n0 17 18 31 36 37 40 53 73 86 96 107 119 150 167 181 184 187\r\n", "output": "1\r\n27\r\n"}, {"input": "20 179 69 120\r\n0 6 8 11 21 24 55 61 83 84 96 111 114 116 125 140 147 154 176 179\r\n", "output": "1\r\n27\r\n"}, {"input": "16 115 62 112\r\n0 5 24 32 38 43 44 57 62 72 74 92 103 105 113 115\r\n", "output": "1\r\n112\r\n"}, {"input": "112 1867 1261 1606\r\n0 7 17 43 67 70 87 112 129 141 148 162 179 180 189 202 211 220 231 247 250 277 308 311 327 376 400 406 409 417 418 444 480 512 514 515 518 547 572 575 578 587 612 617 654 684 701 742 757 761 788 821 825 835 841 843 850 858 869 872 881 936 939 969 970 971 997 1026 1040 1045 1068 1070 1073 1076 1095 1110 1115 1154 1166 1178 1179 1203 1204 1225 1237 1241 1246 1275 1302 1305 1311 1312 1315 1338 1340 1419 1428 1560 1561 1576 1591 1594 1618 1643 1658 1660 1664 1689 1803 1822 1835 1867\r\n", "output": "1\r\n1808\r\n"}, {"input": "2 2 1 2\r\n0 2\r\n", "output": "1\r\n1\r\n"}, {"input": "3 2 1 2\r\n0 1 2\r\n", "output": "0\r\n"}, {"input": "3 10 2 3\r\n0 1 10\r\n", "output": "1\r\n3\r\n"}, {"input": "4 10 3 5\r\n0 1 9 10\r\n", "output": "1\r\n4\r\n"}, {"input": "5 1000 777 778\r\n0 1 500 501 1000\r\n", "output": "1\r\n778\r\n"}, {"input": "3 10 1 3\r\n0 2 10\r\n", "output": "1\r\n3\r\n"}, {"input": "4 300 120 150\r\n0 110 140 300\r\n", "output": "1\r\n260\r\n"}, {"input": "5 401 300 400\r\n0 100 250 350 401\r\n", "output": "1\r\n400\r\n"}, {"input": "3 10 1 8\r\n0 7 10\r\n", "output": "1\r\n8\r\n"}, {"input": "4 1000 2 3\r\n0 400 405 1000\r\n", "output": "1\r\n402\r\n"}, {"input": "6 12 7 10\r\n0 1 3 4 6 12\r\n", "output": "1\r\n10\r\n"}, {"input": "4 1000 10 20\r\n0 500 530 1000\r\n", "output": "1\r\n510\r\n"}, {"input": "3 8 2 3\r\n0 7 8\r\n", "output": "1\r\n5\r\n"}, {"input": "4 10 8 9\r\n0 4 5 10\r\n", "output": "2\r\n8 9\r\n"}, {"input": "4 10 7 8\r\n0 5 6 10\r\n", "output": "2\r\n7 8\r\n"}, {"input": "6 35 29 30\r\n0 10 11 31 32 35\r\n", "output": "1\r\n2\r\n"}, {"input": "5 200000 1 100029\r\n0 100000 100009 100010 200000\r\n", "output": "1\r\n100029\r\n"}, {"input": "4 1000 900 901\r\n0 950 951 1000\r\n", "output": "1\r\n50\r\n"}, {"input": "6 504 400 500\r\n0 3 5 103 105 504\r\n", "output": "1\r\n503\r\n"}, {"input": "5 550 300 400\r\n0 151 251 450 550\r\n", "output": "1\r\n150\r\n"}, {"input": "4 300 40 50\r\n0 280 290 300\r\n", "output": "1\r\n240\r\n"}, {"input": "2 1000000000 100000000 500000000\r\n0 1000000000\r\n", "output": "2\r\n100000000 500000000\r\n"}, {"input": "4 600 100 400\r\n0 50 350 600\r\n", "output": "1\r\n450\r\n"}, {"input": "4 100 7 8\r\n0 3 4 100\r\n", "output": "1\r\n11\r\n"}, {"input": "4 100 80 81\r\n0 2 3 100\r\n", "output": "1\r\n83\r\n"}, {"input": "3 13 8 10\r\n0 2 13\r\n", "output": "1\r\n10\r\n"}, {"input": "4 10 7 8\r\n0 4 5 10\r\n", "output": "2\r\n7 8\r\n"}, {"input": "3 450 100 400\r\n0 150 450\r\n", "output": "1\r\n50\r\n"}, {"input": "4 500 30 50\r\n0 20 40 500\r\n", "output": "1\r\n50\r\n"}, {"input": "4 100 10 11\r\n0 4 5 100\r\n", "output": "1\r\n15\r\n"}, {"input": "2 10 5 7\r\n0 10\r\n", "output": "2\r\n5 7\r\n"}, {"input": "6 100 70 71\r\n0 50 51 90 91 100\r\n", "output": "1\r\n20\r\n"}, {"input": "4 9 6 7\r\n0 4 5 9\r\n", "output": "2\r\n6 7\r\n"}, {"input": "3 10 1 8\r\n0 3 10\r\n", "output": "1\r\n2\r\n"}, {"input": "3 12 1 2\r\n0 10 12\r\n", "output": "1\r\n1\r\n"}, {"input": "4 100 3 5\r\n0 40 48 100\r\n", "output": "1\r\n43\r\n"}, {"input": "3 20 17 18\r\n0 19 20\r\n", "output": "1\r\n2\r\n"}, {"input": "4 1000 45 46\r\n0 2 3 1000\r\n", "output": "1\r\n48\r\n"}, {"input": "4 10 5 7\r\n0 4 6 10\r\n", "output": "2\r\n5 7\r\n"}, {"input": "3 12 1 3\r\n0 10 12\r\n", "output": "1\r\n9\r\n"}, {"input": "4 20 6 7\r\n0 1 15 20\r\n", "output": "1\r\n7\r\n"}, {"input": "3 11 3 5\r\n0 9 11\r\n", "output": "1\r\n6\r\n"}, {"input": "3 100 9 10\r\n0 99 100\r\n", "output": "1\r\n90\r\n"}, {"input": "3 10 7 8\r\n0 1 10\r\n", "output": "1\r\n8\r\n"}, {"input": "3 10 5 6\r\n0 9 10\r\n", "output": "1\r\n4\r\n"}, {"input": "3 10 7 8\r\n0 9 10\r\n", "output": "1\r\n2\r\n"}, {"input": "3 10 6 7\r\n0 9 10\r\n", "output": "1\r\n3\r\n"}, {"input": "3 9 6 7\r\n0 1 9\r\n", "output": "1\r\n7\r\n"}, {"input": "3 1000000000 99 100\r\n0 1 1000000000\r\n", "output": "1\r\n100\r\n"}, {"input": "4 10 3 5\r\n0 2 4 10\r\n", "output": "1\r\n5\r\n"}, {"input": "4 100 90 91\r\n0 7 8 100\r\n", "output": "1\r\n98\r\n"}, {"input": "4 100 80 81\r\n0 98 99 100\r\n", "output": "1\r\n18\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] # Read input with open(input_path) as f: lines = f.readlines() n, l, x, y = map(int, lines[0].split()) a = list(map(int, lines[1].split())) a_set = set(a) # Read reference output (v_ref) with open(output_path) as f: ref_lines = f.readlines() v_ref = int(ref_lines[0].strip()) # Read submission output with open(submission_path) as f: sub_lines = [line.strip() for line in f.readlines() if line.strip()] if not sub_lines: print(0) return try: v_sub = int(sub_lines[0]) except: print(0) return marks_sub = [] if len(sub_lines) > 1: try: marks_sub = list(map(int, sub_lines[1].split())) except: print(0) return # Check v_sub matches v_ref if v_sub != v_ref: print(0) return # Check number of marks if len(marks_sub) != v_sub: print(0) return # Validate each mark seen = set() for p in marks_sub: if not (0 <= p <= l): print(0) return if p in a_set: print(0) return if p in seen: print(0) return seen.add(p) # Check x and y can be measured combined = a_set.union(seen) combined_set = set(combined) # Check x x_ok = any((m + x) in combined_set for m in combined_set) if not x_ok: print(0) return # Check y y_ok = any((m + y) in combined_set for m in combined_set) if not y_ok: print(0) return print(1) if __name__ == "__main__": main()
true
427/B
427
B
PyPy 3-64
TESTS
5
124
25,292,800
208332398
n,t,c=map(int,input().strip().split()) li=list(map(int,input().strip().split())) di={} i=0 fli=[] while i<n: ct=0 si=i maxi=li[i] while ct<c and i<n: if li[i]>maxi: maxi=li[i] ct+=1 i+=1 lii=[maxi]*c fli=fli+lii ans=0 si=0 for i in range(0,n-c+1): if fli[i]<=t: ans+=1 print(ans)
80
93
21,913,600
226506538
def ways(n, t, c, prisoners): valid = 0 ways = 0 if n < c: return 0 for p in prisoners: if p <= t: valid += 1 else: valid = 0 if c <= valid: ways += 1 return ways n, t, c = map(int, input().split()) prisoners = map(int, input().split()) print(ways(n, t, c, prisoners))
Codeforces Round 244 (Div. 2)
CF
2,014
1
256
Prison Transfer
The prison of your city has n prisoners. As the prison can't accommodate all of them, the city mayor has decided to transfer c of the prisoners to a prison located in another city. For this reason, he made the n prisoners to stand in a line, with a number written on their chests. The number is the severity of the crime he/she has committed. The greater the number, the more severe his/her crime was. Then, the mayor told you to choose the c prisoners, who will be transferred to the other prison. He also imposed two conditions. They are, - The chosen c prisoners has to form a contiguous segment of prisoners. - Any of the chosen prisoner's crime level should not be greater then t. Because, that will make the prisoner a severe criminal and the mayor doesn't want to take the risk of his running away during the transfer. Find the number of ways you can choose the c prisoners.
The first line of input will contain three space separated integers n (1 ≤ n ≤ 2·105), t (0 ≤ t ≤ 109) and c (1 ≤ c ≤ n). The next line will contain n space separated integers, the ith integer is the severity ith prisoner's crime. The value of crime severities will be non-negative and will not exceed 109.
Print a single integer — the number of ways you can choose the c prisoners.
null
null
[{"input": "4 3 3\n2 3 1 1", "output": "2"}, {"input": "1 1 1\n2", "output": "0"}, {"input": "11 4 2\n2 2 0 7 3 2 2 4 9 1 4", "output": "6"}]
1,100
["data structures", "implementation"]
80
[{"input": "4 3 3\r\n2 3 1 1\r\n", "output": "2\r\n"}, {"input": "1 1 1\r\n2\r\n", "output": "0\r\n"}, {"input": "11 4 2\r\n2 2 0 7 3 2 2 4 9 1 4\r\n", "output": "6\r\n"}, {"input": "57 2 10\r\n7 5 2 7 4 1 0 5 2 9 2 9 8 6 6 5 9 6 8 1 0 1 0 3 2 6 5 2 8 8 8 8 0 9 4 3 6 6 2 4 5 1 2 0 1 7 1 1 5 4 5 0 7 5 1 9 6\r\n", "output": "0\r\n"}, {"input": "2 228885628 1\r\n90897004 258427916\r\n", "output": "1\r\n"}, {"input": "3 1 1\r\n1 2 1\r\n", "output": "2\r\n"}, {"input": "3 3 3\r\n3 2 3\r\n", "output": "1\r\n"}, {"input": "4 2 2\r\n1 3 3 2\r\n", "output": "0\r\n"}, {"input": "1 228 1\r\n1\r\n", "output": "1\r\n"}]
false
stdio
null
true
165/A
165
A
Python 3
TESTS
3
62
0
145610886
n=int(input()) pt_list=[] ans=0 for _ in range(n): [x,y] =map(int,input().split()) pt_list.append([x,y]) for i in range(n): a_setx=set() a_sety=set() for j in range(n): if pt_list[i][0]-pt_list[j][0]>0: a_setx.add('pos') elif pt_list[i][1]-pt_list[j][1]>0: a_sety.add('posy') elif pt_list[i][1]-pt_list[j][1]<0: a_sety.add('negy') elif pt_list[i][0]-pt_list[j][0]<0: a_setx.add('negx') else: continue if len(a_setx)==2 and len(a_sety)==2: ans+=1 print(ans)
26
92
0
141808501
n = int(input()) store_x = {} store_y = {} def sortSecond(val): return val[1] for _ in range(n): x,y = [int(i) for i in input().split()] if store_x.get(x): store_x[x].append((x,y)) else: store_x[x] = [(x,y)] if store_y.get(y): store_y[y].append((x,y)) else: store_y[y] = [(x,y)] candidates = {} ans = 0 for k,v in store_x.items(): if len(v) > 2: v.sort(key= sortSecond) for i in v[1:len(v)-1]: if candidates.get(str(i)): candidates[str(i)]+=1 else: candidates[str(i)]=1 for k,v in store_y.items(): if len(v) > 2: v.sort() for i in v[1:len(v)-1]: if candidates.get(str(i)): ans += candidates[str(i)] print(ans)
Codeforces Round 112 (Div. 2)
CF
2,012
2
256
Supercentral Point
One day Vasya painted a Cartesian coordinate system on a piece of paper and marked some set of points (x1, y1), (x2, y2), ..., (xn, yn). Let's define neighbors for some fixed point from the given set (x, y): - point (x', y') is (x, y)'s right neighbor, if x' > x and y' = y - point (x', y') is (x, y)'s left neighbor, if x' < x and y' = y - point (x', y') is (x, y)'s lower neighbor, if x' = x and y' < y - point (x', y') is (x, y)'s upper neighbor, if x' = x and y' > y We'll consider point (x, y) from the given set supercentral, if it has at least one upper, at least one lower, at least one left and at least one right neighbor among this set's points. Vasya marked quite many points on the paper. Analyzing the picture manually is rather a challenge, so Vasya asked you to help him. Your task is to find the number of supercentral points in the given set.
The first input line contains the only integer n (1 ≤ n ≤ 200) — the number of points in the given set. Next n lines contain the coordinates of the points written as "x y" (without the quotes) (|x|, |y| ≤ 1000), all coordinates are integers. The numbers in the line are separated by exactly one space. It is guaranteed that all points are different.
Print the only number — the number of supercentral points of the given set.
null
In the first sample the supercentral points are only points (1, 1) and (1, 2). In the second sample there is one supercental point — point (0, 0).
[{"input": "8\n1 1\n4 2\n3 1\n1 2\n0 2\n0 1\n1 0\n1 3", "output": "2"}, {"input": "5\n0 0\n0 1\n1 0\n0 -1\n-1 0", "output": "1"}]
1,000
["implementation"]
26
[{"input": "8\r\n1 1\r\n4 2\r\n3 1\r\n1 2\r\n0 2\r\n0 1\r\n1 0\r\n1 3\r\n", "output": "2\r\n"}, {"input": "5\r\n0 0\r\n0 1\r\n1 0\r\n0 -1\r\n-1 0\r\n", "output": "1\r\n"}, {"input": "9\r\n-565 -752\r\n-184 723\r\n-184 -752\r\n-184 1\r\n950 723\r\n-565 723\r\n950 -752\r\n950 1\r\n-565 1\r\n", "output": "1\r\n"}, {"input": "25\r\n-651 897\r\n916 897\r\n-651 -808\r\n-748 301\r\n-734 414\r\n-651 -973\r\n-734 897\r\n916 -550\r\n-758 414\r\n916 180\r\n-758 -808\r\n-758 -973\r\n125 -550\r\n125 -973\r\n125 301\r\n916 414\r\n-748 -808\r\n-651 301\r\n-734 301\r\n-307 897\r\n-651 -550\r\n-651 414\r\n125 -808\r\n-748 -550\r\n916 -808\r\n", "output": "7\r\n"}, {"input": "1\r\n487 550\r\n", "output": "0\r\n"}, {"input": "10\r\n990 -396\r\n990 736\r\n990 646\r\n990 -102\r\n990 -570\r\n990 155\r\n990 528\r\n990 489\r\n990 268\r\n990 676\r\n", "output": "0\r\n"}, {"input": "30\r\n507 836\r\n525 836\r\n-779 196\r\n507 -814\r\n525 -814\r\n525 42\r\n525 196\r\n525 -136\r\n-779 311\r\n507 -360\r\n525 300\r\n507 578\r\n507 311\r\n-779 836\r\n507 300\r\n525 -360\r\n525 311\r\n-779 -360\r\n-779 578\r\n-779 300\r\n507 42\r\n525 578\r\n-779 379\r\n507 196\r\n525 379\r\n507 379\r\n-779 -814\r\n-779 42\r\n-779 -136\r\n507 -136\r\n", "output": "8\r\n"}, {"input": "25\r\n890 -756\r\n890 -188\r\n-37 -756\r\n-37 853\r\n523 998\r\n-261 853\r\n-351 853\r\n-351 -188\r\n523 -756\r\n-261 -188\r\n-37 998\r\n523 -212\r\n-351 998\r\n-37 -188\r\n-351 -756\r\n-37 -212\r\n890 998\r\n890 -212\r\n523 853\r\n-351 -212\r\n-261 -212\r\n-261 998\r\n-261 -756\r\n890 853\r\n523 -188\r\n", "output": "9\r\n"}, {"input": "21\r\n-813 -11\r\n486 254\r\n685 254\r\n-708 254\r\n-55 -11\r\n-671 -191\r\n486 -11\r\n-671 -11\r\n685 -11\r\n685 -191\r\n486 -191\r\n-55 254\r\n-708 -11\r\n-813 254\r\n-708 -191\r\n41 -11\r\n-671 254\r\n-813 -191\r\n41 254\r\n-55 -191\r\n41 -191\r\n", "output": "5\r\n"}, {"input": "4\r\n1 0\r\n2 0\r\n1 1\r\n1 -1\r\n", "output": "0\r\n"}]
false
stdio
null
true
165/A
165
A
PyPy 3-64
TESTS
3
92
0
184382238
n = int(input()) sx = [] sy = [] count = 0 for i in range(n): s = list(map(int, input().split())) x, y = s[0], s[1] sx.append(x) sy.append(y) mAxx = max(sx) mInx = min(sx) mAxy = max(sy) mIny = min(sy) for i in range(len(sx)): if sx.count(sx[i]) >= 3 and sx[i] != mAxx and sx[i] != mInx: if sy.count(sy[i]) >= 3 and sy[i] != mAxy and sy[i] != mIny: count += 1 print(count)
26
92
0
146798114
n=int(input()) l=[] a=[] b=[] sum=0 for i in range(n): m=list(map(int,input().split())) l.append(m) a.append(m[0]) b.append(m[1]) for i in range(n): count=0 for j in range(len(a)): if a[j]==l[i][0] and b[j]>l[i][1]: count+=1 break for j in range(len(a)): if a[j]==l[i][0] and b[j]<l[i][1]: count+=1 break for j in range(len(a)): if b[j]==l[i][1] and a[j]>l[i][0]: count+=1 break for j in range(len(a)): if b[j]==l[i][1] and a[j]<l[i][0]: count+=1 break if (count==4): sum+=1 print(sum)
Codeforces Round 112 (Div. 2)
CF
2,012
2
256
Supercentral Point
One day Vasya painted a Cartesian coordinate system on a piece of paper and marked some set of points (x1, y1), (x2, y2), ..., (xn, yn). Let's define neighbors for some fixed point from the given set (x, y): - point (x', y') is (x, y)'s right neighbor, if x' > x and y' = y - point (x', y') is (x, y)'s left neighbor, if x' < x and y' = y - point (x', y') is (x, y)'s lower neighbor, if x' = x and y' < y - point (x', y') is (x, y)'s upper neighbor, if x' = x and y' > y We'll consider point (x, y) from the given set supercentral, if it has at least one upper, at least one lower, at least one left and at least one right neighbor among this set's points. Vasya marked quite many points on the paper. Analyzing the picture manually is rather a challenge, so Vasya asked you to help him. Your task is to find the number of supercentral points in the given set.
The first input line contains the only integer n (1 ≤ n ≤ 200) — the number of points in the given set. Next n lines contain the coordinates of the points written as "x y" (without the quotes) (|x|, |y| ≤ 1000), all coordinates are integers. The numbers in the line are separated by exactly one space. It is guaranteed that all points are different.
Print the only number — the number of supercentral points of the given set.
null
In the first sample the supercentral points are only points (1, 1) and (1, 2). In the second sample there is one supercental point — point (0, 0).
[{"input": "8\n1 1\n4 2\n3 1\n1 2\n0 2\n0 1\n1 0\n1 3", "output": "2"}, {"input": "5\n0 0\n0 1\n1 0\n0 -1\n-1 0", "output": "1"}]
1,000
["implementation"]
26
[{"input": "8\r\n1 1\r\n4 2\r\n3 1\r\n1 2\r\n0 2\r\n0 1\r\n1 0\r\n1 3\r\n", "output": "2\r\n"}, {"input": "5\r\n0 0\r\n0 1\r\n1 0\r\n0 -1\r\n-1 0\r\n", "output": "1\r\n"}, {"input": "9\r\n-565 -752\r\n-184 723\r\n-184 -752\r\n-184 1\r\n950 723\r\n-565 723\r\n950 -752\r\n950 1\r\n-565 1\r\n", "output": "1\r\n"}, {"input": "25\r\n-651 897\r\n916 897\r\n-651 -808\r\n-748 301\r\n-734 414\r\n-651 -973\r\n-734 897\r\n916 -550\r\n-758 414\r\n916 180\r\n-758 -808\r\n-758 -973\r\n125 -550\r\n125 -973\r\n125 301\r\n916 414\r\n-748 -808\r\n-651 301\r\n-734 301\r\n-307 897\r\n-651 -550\r\n-651 414\r\n125 -808\r\n-748 -550\r\n916 -808\r\n", "output": "7\r\n"}, {"input": "1\r\n487 550\r\n", "output": "0\r\n"}, {"input": "10\r\n990 -396\r\n990 736\r\n990 646\r\n990 -102\r\n990 -570\r\n990 155\r\n990 528\r\n990 489\r\n990 268\r\n990 676\r\n", "output": "0\r\n"}, {"input": "30\r\n507 836\r\n525 836\r\n-779 196\r\n507 -814\r\n525 -814\r\n525 42\r\n525 196\r\n525 -136\r\n-779 311\r\n507 -360\r\n525 300\r\n507 578\r\n507 311\r\n-779 836\r\n507 300\r\n525 -360\r\n525 311\r\n-779 -360\r\n-779 578\r\n-779 300\r\n507 42\r\n525 578\r\n-779 379\r\n507 196\r\n525 379\r\n507 379\r\n-779 -814\r\n-779 42\r\n-779 -136\r\n507 -136\r\n", "output": "8\r\n"}, {"input": "25\r\n890 -756\r\n890 -188\r\n-37 -756\r\n-37 853\r\n523 998\r\n-261 853\r\n-351 853\r\n-351 -188\r\n523 -756\r\n-261 -188\r\n-37 998\r\n523 -212\r\n-351 998\r\n-37 -188\r\n-351 -756\r\n-37 -212\r\n890 998\r\n890 -212\r\n523 853\r\n-351 -212\r\n-261 -212\r\n-261 998\r\n-261 -756\r\n890 853\r\n523 -188\r\n", "output": "9\r\n"}, {"input": "21\r\n-813 -11\r\n486 254\r\n685 254\r\n-708 254\r\n-55 -11\r\n-671 -191\r\n486 -11\r\n-671 -11\r\n685 -11\r\n685 -191\r\n486 -191\r\n-55 254\r\n-708 -11\r\n-813 254\r\n-708 -191\r\n41 -11\r\n-671 254\r\n-813 -191\r\n41 254\r\n-55 -191\r\n41 -191\r\n", "output": "5\r\n"}, {"input": "4\r\n1 0\r\n2 0\r\n1 1\r\n1 -1\r\n", "output": "0\r\n"}]
false
stdio
null
true
165/A
165
A
Python 3
TESTS
3
62
0
160189729
lst=[] for i in range(int(input())):lst.append([int(x) for x in input().split()]) lstx=[i[0] for i in lst] lsty=[i[1] for i in lst] minx,maxx,miny,maxy=min(lstx),max(lstx),min(lsty),max(lsty) superpt=0 for i in lst: if i[0]!=minx and i[0]!=maxx: if i[1]!=miny and i[1]!=maxy: if lstx.count(i[0])>=3 and lsty.count(i[1])>=3:superpt+=1 print(superpt)
26
92
0
147175351
n = int(input()) a = [list(int(a) for a in input().split()) for i in range(n)] c = 0 for x,y in a: l , r , t , b = 0,0,0,0 for x1,y1 in a: if x == x1: if y1 > y: t = 1 if y1 < y: b = 1 if y == y1: if x1 > x: r = 1 if x1 < x: l = 1 if l == 1 and r == 1 and t == 1 and b == 1: c += 1 print(c)
Codeforces Round 112 (Div. 2)
CF
2,012
2
256
Supercentral Point
One day Vasya painted a Cartesian coordinate system on a piece of paper and marked some set of points (x1, y1), (x2, y2), ..., (xn, yn). Let's define neighbors for some fixed point from the given set (x, y): - point (x', y') is (x, y)'s right neighbor, if x' > x and y' = y - point (x', y') is (x, y)'s left neighbor, if x' < x and y' = y - point (x', y') is (x, y)'s lower neighbor, if x' = x and y' < y - point (x', y') is (x, y)'s upper neighbor, if x' = x and y' > y We'll consider point (x, y) from the given set supercentral, if it has at least one upper, at least one lower, at least one left and at least one right neighbor among this set's points. Vasya marked quite many points on the paper. Analyzing the picture manually is rather a challenge, so Vasya asked you to help him. Your task is to find the number of supercentral points in the given set.
The first input line contains the only integer n (1 ≤ n ≤ 200) — the number of points in the given set. Next n lines contain the coordinates of the points written as "x y" (without the quotes) (|x|, |y| ≤ 1000), all coordinates are integers. The numbers in the line are separated by exactly one space. It is guaranteed that all points are different.
Print the only number — the number of supercentral points of the given set.
null
In the first sample the supercentral points are only points (1, 1) and (1, 2). In the second sample there is one supercental point — point (0, 0).
[{"input": "8\n1 1\n4 2\n3 1\n1 2\n0 2\n0 1\n1 0\n1 3", "output": "2"}, {"input": "5\n0 0\n0 1\n1 0\n0 -1\n-1 0", "output": "1"}]
1,000
["implementation"]
26
[{"input": "8\r\n1 1\r\n4 2\r\n3 1\r\n1 2\r\n0 2\r\n0 1\r\n1 0\r\n1 3\r\n", "output": "2\r\n"}, {"input": "5\r\n0 0\r\n0 1\r\n1 0\r\n0 -1\r\n-1 0\r\n", "output": "1\r\n"}, {"input": "9\r\n-565 -752\r\n-184 723\r\n-184 -752\r\n-184 1\r\n950 723\r\n-565 723\r\n950 -752\r\n950 1\r\n-565 1\r\n", "output": "1\r\n"}, {"input": "25\r\n-651 897\r\n916 897\r\n-651 -808\r\n-748 301\r\n-734 414\r\n-651 -973\r\n-734 897\r\n916 -550\r\n-758 414\r\n916 180\r\n-758 -808\r\n-758 -973\r\n125 -550\r\n125 -973\r\n125 301\r\n916 414\r\n-748 -808\r\n-651 301\r\n-734 301\r\n-307 897\r\n-651 -550\r\n-651 414\r\n125 -808\r\n-748 -550\r\n916 -808\r\n", "output": "7\r\n"}, {"input": "1\r\n487 550\r\n", "output": "0\r\n"}, {"input": "10\r\n990 -396\r\n990 736\r\n990 646\r\n990 -102\r\n990 -570\r\n990 155\r\n990 528\r\n990 489\r\n990 268\r\n990 676\r\n", "output": "0\r\n"}, {"input": "30\r\n507 836\r\n525 836\r\n-779 196\r\n507 -814\r\n525 -814\r\n525 42\r\n525 196\r\n525 -136\r\n-779 311\r\n507 -360\r\n525 300\r\n507 578\r\n507 311\r\n-779 836\r\n507 300\r\n525 -360\r\n525 311\r\n-779 -360\r\n-779 578\r\n-779 300\r\n507 42\r\n525 578\r\n-779 379\r\n507 196\r\n525 379\r\n507 379\r\n-779 -814\r\n-779 42\r\n-779 -136\r\n507 -136\r\n", "output": "8\r\n"}, {"input": "25\r\n890 -756\r\n890 -188\r\n-37 -756\r\n-37 853\r\n523 998\r\n-261 853\r\n-351 853\r\n-351 -188\r\n523 -756\r\n-261 -188\r\n-37 998\r\n523 -212\r\n-351 998\r\n-37 -188\r\n-351 -756\r\n-37 -212\r\n890 998\r\n890 -212\r\n523 853\r\n-351 -212\r\n-261 -212\r\n-261 998\r\n-261 -756\r\n890 853\r\n523 -188\r\n", "output": "9\r\n"}, {"input": "21\r\n-813 -11\r\n486 254\r\n685 254\r\n-708 254\r\n-55 -11\r\n-671 -191\r\n486 -11\r\n-671 -11\r\n685 -11\r\n685 -191\r\n486 -191\r\n-55 254\r\n-708 -11\r\n-813 254\r\n-708 -191\r\n41 -11\r\n-671 254\r\n-813 -191\r\n41 254\r\n-55 -191\r\n41 -191\r\n", "output": "5\r\n"}, {"input": "4\r\n1 0\r\n2 0\r\n1 1\r\n1 -1\r\n", "output": "0\r\n"}]
false
stdio
null
true
165/A
165
A
Python 3
TESTS
3
62
0
193427066
n = int(input()) arrs = [] for _ in range(n): points = [list(x) for x in input().split()] arrs.append(points) count = 0 for i in range(n): r, l, lo, up = False, False, False, False for j in range(n): if i == j: continue if arrs[i][1] == arrs[j][1]: if arrs[i][0] > arrs[j][0]: r = True if arrs[i][0] < arrs[j][0]: l = True if arrs[i][0] == arrs[j][0]: if arrs[i][1] < arrs[j][1]: lo = True if arrs[i][1] > arrs[j][1]: up = True if r and l and lo and up: count += 1 print(count)
26
92
0
149380017
n = int(input()) pts = [] for i in range(n): el = [int(x) for x in input().split()] pts.append(el) # print(pts) res = 0 for pt1 in range(n): rtn = False lftn = False lown = False upn = False for pt2 in range(n): if(pt1!=pt2): if(pts[pt1][0]==pts[pt2][0]): if(pts[pt1][1] > pts[pt2][1]): lown = True elif(pts[pt1][1] < pts[pt2][1]): upn = True if(pts[pt1][1]==pts[pt2][1]): if(pts[pt1][0] > pts[pt2][0]): lftn = True elif(pts[pt1][0] < pts[pt2][0]): rtn = True if(rtn and lftn and lown and upn): res+=1 break print(res)
Codeforces Round 112 (Div. 2)
CF
2,012
2
256
Supercentral Point
One day Vasya painted a Cartesian coordinate system on a piece of paper and marked some set of points (x1, y1), (x2, y2), ..., (xn, yn). Let's define neighbors for some fixed point from the given set (x, y): - point (x', y') is (x, y)'s right neighbor, if x' > x and y' = y - point (x', y') is (x, y)'s left neighbor, if x' < x and y' = y - point (x', y') is (x, y)'s lower neighbor, if x' = x and y' < y - point (x', y') is (x, y)'s upper neighbor, if x' = x and y' > y We'll consider point (x, y) from the given set supercentral, if it has at least one upper, at least one lower, at least one left and at least one right neighbor among this set's points. Vasya marked quite many points on the paper. Analyzing the picture manually is rather a challenge, so Vasya asked you to help him. Your task is to find the number of supercentral points in the given set.
The first input line contains the only integer n (1 ≤ n ≤ 200) — the number of points in the given set. Next n lines contain the coordinates of the points written as "x y" (without the quotes) (|x|, |y| ≤ 1000), all coordinates are integers. The numbers in the line are separated by exactly one space. It is guaranteed that all points are different.
Print the only number — the number of supercentral points of the given set.
null
In the first sample the supercentral points are only points (1, 1) and (1, 2). In the second sample there is one supercental point — point (0, 0).
[{"input": "8\n1 1\n4 2\n3 1\n1 2\n0 2\n0 1\n1 0\n1 3", "output": "2"}, {"input": "5\n0 0\n0 1\n1 0\n0 -1\n-1 0", "output": "1"}]
1,000
["implementation"]
26
[{"input": "8\r\n1 1\r\n4 2\r\n3 1\r\n1 2\r\n0 2\r\n0 1\r\n1 0\r\n1 3\r\n", "output": "2\r\n"}, {"input": "5\r\n0 0\r\n0 1\r\n1 0\r\n0 -1\r\n-1 0\r\n", "output": "1\r\n"}, {"input": "9\r\n-565 -752\r\n-184 723\r\n-184 -752\r\n-184 1\r\n950 723\r\n-565 723\r\n950 -752\r\n950 1\r\n-565 1\r\n", "output": "1\r\n"}, {"input": "25\r\n-651 897\r\n916 897\r\n-651 -808\r\n-748 301\r\n-734 414\r\n-651 -973\r\n-734 897\r\n916 -550\r\n-758 414\r\n916 180\r\n-758 -808\r\n-758 -973\r\n125 -550\r\n125 -973\r\n125 301\r\n916 414\r\n-748 -808\r\n-651 301\r\n-734 301\r\n-307 897\r\n-651 -550\r\n-651 414\r\n125 -808\r\n-748 -550\r\n916 -808\r\n", "output": "7\r\n"}, {"input": "1\r\n487 550\r\n", "output": "0\r\n"}, {"input": "10\r\n990 -396\r\n990 736\r\n990 646\r\n990 -102\r\n990 -570\r\n990 155\r\n990 528\r\n990 489\r\n990 268\r\n990 676\r\n", "output": "0\r\n"}, {"input": "30\r\n507 836\r\n525 836\r\n-779 196\r\n507 -814\r\n525 -814\r\n525 42\r\n525 196\r\n525 -136\r\n-779 311\r\n507 -360\r\n525 300\r\n507 578\r\n507 311\r\n-779 836\r\n507 300\r\n525 -360\r\n525 311\r\n-779 -360\r\n-779 578\r\n-779 300\r\n507 42\r\n525 578\r\n-779 379\r\n507 196\r\n525 379\r\n507 379\r\n-779 -814\r\n-779 42\r\n-779 -136\r\n507 -136\r\n", "output": "8\r\n"}, {"input": "25\r\n890 -756\r\n890 -188\r\n-37 -756\r\n-37 853\r\n523 998\r\n-261 853\r\n-351 853\r\n-351 -188\r\n523 -756\r\n-261 -188\r\n-37 998\r\n523 -212\r\n-351 998\r\n-37 -188\r\n-351 -756\r\n-37 -212\r\n890 998\r\n890 -212\r\n523 853\r\n-351 -212\r\n-261 -212\r\n-261 998\r\n-261 -756\r\n890 853\r\n523 -188\r\n", "output": "9\r\n"}, {"input": "21\r\n-813 -11\r\n486 254\r\n685 254\r\n-708 254\r\n-55 -11\r\n-671 -191\r\n486 -11\r\n-671 -11\r\n685 -11\r\n685 -191\r\n486 -191\r\n-55 254\r\n-708 -11\r\n-813 254\r\n-708 -191\r\n41 -11\r\n-671 254\r\n-813 -191\r\n41 254\r\n-55 -191\r\n41 -191\r\n", "output": "5\r\n"}, {"input": "4\r\n1 0\r\n2 0\r\n1 1\r\n1 -1\r\n", "output": "0\r\n"}]
false
stdio
null
true
165/A
165
A
PyPy 3
TESTS
5
186
1,433,600
104274136
n = int(input()) points = [] for i in range(n): points.append(list(map(int, input().split()))) count = 0 for i in range(n): x, y = points[i] up, down, left, right = False, False, False, False for j in range(n): x2, y2 = points[j] if x == x2: if y < y2: up = True elif y > y2: down = True elif y == y2: if x < x2: left = True elif x > x2: right = True if up == down == left == right: count += 1 print(count)
26
92
0
150146938
n = int(input()) arr = [] output = 0 for i in range(n): x, y = [int(i) for i in input().split()] arr.append([x, y]) for i in range(n): x = arr[i][0] y = arr[i][1] arr2 = arr[:n] + arr[n+1:] left = False right = False top = False bottom = False for item in arr2: if item[0] > x and item[1] == y: right = True elif item[0] < x and item[1] == y: left = True elif item[0] == x and item[1] < y: bottom = True elif item[0] == x and item[1] > y: top = True if left == True and right == True and top == True and bottom == True: output += 1 print(output)
Codeforces Round 112 (Div. 2)
CF
2,012
2
256
Supercentral Point
One day Vasya painted a Cartesian coordinate system on a piece of paper and marked some set of points (x1, y1), (x2, y2), ..., (xn, yn). Let's define neighbors for some fixed point from the given set (x, y): - point (x', y') is (x, y)'s right neighbor, if x' > x and y' = y - point (x', y') is (x, y)'s left neighbor, if x' < x and y' = y - point (x', y') is (x, y)'s lower neighbor, if x' = x and y' < y - point (x', y') is (x, y)'s upper neighbor, if x' = x and y' > y We'll consider point (x, y) from the given set supercentral, if it has at least one upper, at least one lower, at least one left and at least one right neighbor among this set's points. Vasya marked quite many points on the paper. Analyzing the picture manually is rather a challenge, so Vasya asked you to help him. Your task is to find the number of supercentral points in the given set.
The first input line contains the only integer n (1 ≤ n ≤ 200) — the number of points in the given set. Next n lines contain the coordinates of the points written as "x y" (without the quotes) (|x|, |y| ≤ 1000), all coordinates are integers. The numbers in the line are separated by exactly one space. It is guaranteed that all points are different.
Print the only number — the number of supercentral points of the given set.
null
In the first sample the supercentral points are only points (1, 1) and (1, 2). In the second sample there is one supercental point — point (0, 0).
[{"input": "8\n1 1\n4 2\n3 1\n1 2\n0 2\n0 1\n1 0\n1 3", "output": "2"}, {"input": "5\n0 0\n0 1\n1 0\n0 -1\n-1 0", "output": "1"}]
1,000
["implementation"]
26
[{"input": "8\r\n1 1\r\n4 2\r\n3 1\r\n1 2\r\n0 2\r\n0 1\r\n1 0\r\n1 3\r\n", "output": "2\r\n"}, {"input": "5\r\n0 0\r\n0 1\r\n1 0\r\n0 -1\r\n-1 0\r\n", "output": "1\r\n"}, {"input": "9\r\n-565 -752\r\n-184 723\r\n-184 -752\r\n-184 1\r\n950 723\r\n-565 723\r\n950 -752\r\n950 1\r\n-565 1\r\n", "output": "1\r\n"}, {"input": "25\r\n-651 897\r\n916 897\r\n-651 -808\r\n-748 301\r\n-734 414\r\n-651 -973\r\n-734 897\r\n916 -550\r\n-758 414\r\n916 180\r\n-758 -808\r\n-758 -973\r\n125 -550\r\n125 -973\r\n125 301\r\n916 414\r\n-748 -808\r\n-651 301\r\n-734 301\r\n-307 897\r\n-651 -550\r\n-651 414\r\n125 -808\r\n-748 -550\r\n916 -808\r\n", "output": "7\r\n"}, {"input": "1\r\n487 550\r\n", "output": "0\r\n"}, {"input": "10\r\n990 -396\r\n990 736\r\n990 646\r\n990 -102\r\n990 -570\r\n990 155\r\n990 528\r\n990 489\r\n990 268\r\n990 676\r\n", "output": "0\r\n"}, {"input": "30\r\n507 836\r\n525 836\r\n-779 196\r\n507 -814\r\n525 -814\r\n525 42\r\n525 196\r\n525 -136\r\n-779 311\r\n507 -360\r\n525 300\r\n507 578\r\n507 311\r\n-779 836\r\n507 300\r\n525 -360\r\n525 311\r\n-779 -360\r\n-779 578\r\n-779 300\r\n507 42\r\n525 578\r\n-779 379\r\n507 196\r\n525 379\r\n507 379\r\n-779 -814\r\n-779 42\r\n-779 -136\r\n507 -136\r\n", "output": "8\r\n"}, {"input": "25\r\n890 -756\r\n890 -188\r\n-37 -756\r\n-37 853\r\n523 998\r\n-261 853\r\n-351 853\r\n-351 -188\r\n523 -756\r\n-261 -188\r\n-37 998\r\n523 -212\r\n-351 998\r\n-37 -188\r\n-351 -756\r\n-37 -212\r\n890 998\r\n890 -212\r\n523 853\r\n-351 -212\r\n-261 -212\r\n-261 998\r\n-261 -756\r\n890 853\r\n523 -188\r\n", "output": "9\r\n"}, {"input": "21\r\n-813 -11\r\n486 254\r\n685 254\r\n-708 254\r\n-55 -11\r\n-671 -191\r\n486 -11\r\n-671 -11\r\n685 -11\r\n685 -191\r\n486 -191\r\n-55 254\r\n-708 -11\r\n-813 254\r\n-708 -191\r\n41 -11\r\n-671 254\r\n-813 -191\r\n41 254\r\n-55 -191\r\n41 -191\r\n", "output": "5\r\n"}, {"input": "4\r\n1 0\r\n2 0\r\n1 1\r\n1 -1\r\n", "output": "0\r\n"}]
false
stdio
null
true
165/A
165
A
Python 3
TESTS
3
60
0
146273591
x=[] xmax=0 xmin=0 ymax=0 ymin=0 y=[] for _ in range(int(input())): a,b=map(int,input().split()) x.append(a) y.append(b) if(a>xmax): xmax=a if(a<xmin): xmin=a if(b>ymax): ymax=b if(b<ymin): ymin=b re=[xmax,ymax,xmin,ymin] x=list(set(x)) y=list(set(y)) for i in range(4): if(re[i] in x): x.remove(re[i]) else: None if(re[i] in y): y.remove(re[i]) else: None print(len(x)*len(y))
26
92
0
155023226
info = dict() amount = int(input()) counter = 0 all_pos = [] for i in range(amount): current = ([int(x) for x in input().split()]) all_pos.append(current) dict_elements = {} for i in all_pos: x, y = i[0], i[1] right, left, upper, lower = 0, 0, 0, 0 for j in all_pos: if (x,y) == j: continue elif x == j[0] and y > j[1]: lower += 1 elif x == j[0] and y < j[1]: upper += 1 elif x < j[0] and y == j[1]: right += 1 elif x > j[0] and y == j[1]: left += 1 dict_elements[tuple(i)] = [right, left, upper, lower] for elements in all_pos: local = 0 for i in dict_elements[tuple(elements)]: if i > 0: local += 1 if local == 4: counter += 1 print(counter)
Codeforces Round 112 (Div. 2)
CF
2,012
2
256
Supercentral Point
One day Vasya painted a Cartesian coordinate system on a piece of paper and marked some set of points (x1, y1), (x2, y2), ..., (xn, yn). Let's define neighbors for some fixed point from the given set (x, y): - point (x', y') is (x, y)'s right neighbor, if x' > x and y' = y - point (x', y') is (x, y)'s left neighbor, if x' < x and y' = y - point (x', y') is (x, y)'s lower neighbor, if x' = x and y' < y - point (x', y') is (x, y)'s upper neighbor, if x' = x and y' > y We'll consider point (x, y) from the given set supercentral, if it has at least one upper, at least one lower, at least one left and at least one right neighbor among this set's points. Vasya marked quite many points on the paper. Analyzing the picture manually is rather a challenge, so Vasya asked you to help him. Your task is to find the number of supercentral points in the given set.
The first input line contains the only integer n (1 ≤ n ≤ 200) — the number of points in the given set. Next n lines contain the coordinates of the points written as "x y" (without the quotes) (|x|, |y| ≤ 1000), all coordinates are integers. The numbers in the line are separated by exactly one space. It is guaranteed that all points are different.
Print the only number — the number of supercentral points of the given set.
null
In the first sample the supercentral points are only points (1, 1) and (1, 2). In the second sample there is one supercental point — point (0, 0).
[{"input": "8\n1 1\n4 2\n3 1\n1 2\n0 2\n0 1\n1 0\n1 3", "output": "2"}, {"input": "5\n0 0\n0 1\n1 0\n0 -1\n-1 0", "output": "1"}]
1,000
["implementation"]
26
[{"input": "8\r\n1 1\r\n4 2\r\n3 1\r\n1 2\r\n0 2\r\n0 1\r\n1 0\r\n1 3\r\n", "output": "2\r\n"}, {"input": "5\r\n0 0\r\n0 1\r\n1 0\r\n0 -1\r\n-1 0\r\n", "output": "1\r\n"}, {"input": "9\r\n-565 -752\r\n-184 723\r\n-184 -752\r\n-184 1\r\n950 723\r\n-565 723\r\n950 -752\r\n950 1\r\n-565 1\r\n", "output": "1\r\n"}, {"input": "25\r\n-651 897\r\n916 897\r\n-651 -808\r\n-748 301\r\n-734 414\r\n-651 -973\r\n-734 897\r\n916 -550\r\n-758 414\r\n916 180\r\n-758 -808\r\n-758 -973\r\n125 -550\r\n125 -973\r\n125 301\r\n916 414\r\n-748 -808\r\n-651 301\r\n-734 301\r\n-307 897\r\n-651 -550\r\n-651 414\r\n125 -808\r\n-748 -550\r\n916 -808\r\n", "output": "7\r\n"}, {"input": "1\r\n487 550\r\n", "output": "0\r\n"}, {"input": "10\r\n990 -396\r\n990 736\r\n990 646\r\n990 -102\r\n990 -570\r\n990 155\r\n990 528\r\n990 489\r\n990 268\r\n990 676\r\n", "output": "0\r\n"}, {"input": "30\r\n507 836\r\n525 836\r\n-779 196\r\n507 -814\r\n525 -814\r\n525 42\r\n525 196\r\n525 -136\r\n-779 311\r\n507 -360\r\n525 300\r\n507 578\r\n507 311\r\n-779 836\r\n507 300\r\n525 -360\r\n525 311\r\n-779 -360\r\n-779 578\r\n-779 300\r\n507 42\r\n525 578\r\n-779 379\r\n507 196\r\n525 379\r\n507 379\r\n-779 -814\r\n-779 42\r\n-779 -136\r\n507 -136\r\n", "output": "8\r\n"}, {"input": "25\r\n890 -756\r\n890 -188\r\n-37 -756\r\n-37 853\r\n523 998\r\n-261 853\r\n-351 853\r\n-351 -188\r\n523 -756\r\n-261 -188\r\n-37 998\r\n523 -212\r\n-351 998\r\n-37 -188\r\n-351 -756\r\n-37 -212\r\n890 998\r\n890 -212\r\n523 853\r\n-351 -212\r\n-261 -212\r\n-261 998\r\n-261 -756\r\n890 853\r\n523 -188\r\n", "output": "9\r\n"}, {"input": "21\r\n-813 -11\r\n486 254\r\n685 254\r\n-708 254\r\n-55 -11\r\n-671 -191\r\n486 -11\r\n-671 -11\r\n685 -11\r\n685 -191\r\n486 -191\r\n-55 254\r\n-708 -11\r\n-813 254\r\n-708 -191\r\n41 -11\r\n-671 254\r\n-813 -191\r\n41 254\r\n-55 -191\r\n41 -191\r\n", "output": "5\r\n"}, {"input": "4\r\n1 0\r\n2 0\r\n1 1\r\n1 -1\r\n", "output": "0\r\n"}]
false
stdio
null
true
165/A
165
A
Python 3
TESTS
3
62
0
130632340
n = int(input()) li = [list(map(int,(input().split()))) for i in range(n)] cnt = 0 for i in li: x_g, x_l, y_g, y_l, x_equal, y_equal = 0, 0, 0, 0, 0, 0 for j in li: if li.index(i) != li.index(j): if j[0]<i[0]: x_l += 1 elif j[0] > i[0]: x_g += 1 elif j[0] == i[0]: x_equal += 1 if j[1]<i[1]: y_l += 1 elif j[1] > i[1]: y_g += 1 elif j[1] == i[1]: y_equal += 1 if x_l > 0 and y_l > 0 and x_g > 0 and y_g > 0 and x_equal > 1 and y_equal > 1: cnt += 1 print(cnt)
26
92
0
155192352
import sys input = sys.stdin.readline n = int(input()) g = [tuple(map(int, input().split())) for _ in range(n)] w = sorted(g) q = [(j,i) for (i,j) in sorted([(j,i) for (i,j) in g])] a = set() b = set() for i in range(n-2): if w[i][0] == w[i+1][0] == w[i+2][0]: a.add(w[i+1]) if q[i][1] == q[i+1][1] == q[i+2][1]: b.add(q[i+1]) print(len(a&b))
Codeforces Round 112 (Div. 2)
CF
2,012
2
256
Supercentral Point
One day Vasya painted a Cartesian coordinate system on a piece of paper and marked some set of points (x1, y1), (x2, y2), ..., (xn, yn). Let's define neighbors for some fixed point from the given set (x, y): - point (x', y') is (x, y)'s right neighbor, if x' > x and y' = y - point (x', y') is (x, y)'s left neighbor, if x' < x and y' = y - point (x', y') is (x, y)'s lower neighbor, if x' = x and y' < y - point (x', y') is (x, y)'s upper neighbor, if x' = x and y' > y We'll consider point (x, y) from the given set supercentral, if it has at least one upper, at least one lower, at least one left and at least one right neighbor among this set's points. Vasya marked quite many points on the paper. Analyzing the picture manually is rather a challenge, so Vasya asked you to help him. Your task is to find the number of supercentral points in the given set.
The first input line contains the only integer n (1 ≤ n ≤ 200) — the number of points in the given set. Next n lines contain the coordinates of the points written as "x y" (without the quotes) (|x|, |y| ≤ 1000), all coordinates are integers. The numbers in the line are separated by exactly one space. It is guaranteed that all points are different.
Print the only number — the number of supercentral points of the given set.
null
In the first sample the supercentral points are only points (1, 1) and (1, 2). In the second sample there is one supercental point — point (0, 0).
[{"input": "8\n1 1\n4 2\n3 1\n1 2\n0 2\n0 1\n1 0\n1 3", "output": "2"}, {"input": "5\n0 0\n0 1\n1 0\n0 -1\n-1 0", "output": "1"}]
1,000
["implementation"]
26
[{"input": "8\r\n1 1\r\n4 2\r\n3 1\r\n1 2\r\n0 2\r\n0 1\r\n1 0\r\n1 3\r\n", "output": "2\r\n"}, {"input": "5\r\n0 0\r\n0 1\r\n1 0\r\n0 -1\r\n-1 0\r\n", "output": "1\r\n"}, {"input": "9\r\n-565 -752\r\n-184 723\r\n-184 -752\r\n-184 1\r\n950 723\r\n-565 723\r\n950 -752\r\n950 1\r\n-565 1\r\n", "output": "1\r\n"}, {"input": "25\r\n-651 897\r\n916 897\r\n-651 -808\r\n-748 301\r\n-734 414\r\n-651 -973\r\n-734 897\r\n916 -550\r\n-758 414\r\n916 180\r\n-758 -808\r\n-758 -973\r\n125 -550\r\n125 -973\r\n125 301\r\n916 414\r\n-748 -808\r\n-651 301\r\n-734 301\r\n-307 897\r\n-651 -550\r\n-651 414\r\n125 -808\r\n-748 -550\r\n916 -808\r\n", "output": "7\r\n"}, {"input": "1\r\n487 550\r\n", "output": "0\r\n"}, {"input": "10\r\n990 -396\r\n990 736\r\n990 646\r\n990 -102\r\n990 -570\r\n990 155\r\n990 528\r\n990 489\r\n990 268\r\n990 676\r\n", "output": "0\r\n"}, {"input": "30\r\n507 836\r\n525 836\r\n-779 196\r\n507 -814\r\n525 -814\r\n525 42\r\n525 196\r\n525 -136\r\n-779 311\r\n507 -360\r\n525 300\r\n507 578\r\n507 311\r\n-779 836\r\n507 300\r\n525 -360\r\n525 311\r\n-779 -360\r\n-779 578\r\n-779 300\r\n507 42\r\n525 578\r\n-779 379\r\n507 196\r\n525 379\r\n507 379\r\n-779 -814\r\n-779 42\r\n-779 -136\r\n507 -136\r\n", "output": "8\r\n"}, {"input": "25\r\n890 -756\r\n890 -188\r\n-37 -756\r\n-37 853\r\n523 998\r\n-261 853\r\n-351 853\r\n-351 -188\r\n523 -756\r\n-261 -188\r\n-37 998\r\n523 -212\r\n-351 998\r\n-37 -188\r\n-351 -756\r\n-37 -212\r\n890 998\r\n890 -212\r\n523 853\r\n-351 -212\r\n-261 -212\r\n-261 998\r\n-261 -756\r\n890 853\r\n523 -188\r\n", "output": "9\r\n"}, {"input": "21\r\n-813 -11\r\n486 254\r\n685 254\r\n-708 254\r\n-55 -11\r\n-671 -191\r\n486 -11\r\n-671 -11\r\n685 -11\r\n685 -191\r\n486 -191\r\n-55 254\r\n-708 -11\r\n-813 254\r\n-708 -191\r\n41 -11\r\n-671 254\r\n-813 -191\r\n41 254\r\n-55 -191\r\n41 -191\r\n", "output": "5\r\n"}, {"input": "4\r\n1 0\r\n2 0\r\n1 1\r\n1 -1\r\n", "output": "0\r\n"}]
false
stdio
null
true
427/B
427
B
PyPy 3-64
TESTS
4
93
22,528,000
201051017
n, t, c = tuple(map(int, input().split())) prisoners = tuple(map(int, input().split())) def get_lines_length(prisoners): length = 0 lines_length = [] for prisoner in prisoners: if prisoner <= t: length += 1 else: lines_length.append(length) length = 0 if length > 0: lines_length.append(length) return lines_length def get_count(lines_length, c): count = 0 for length in lines_length: if length < c: result = 0 elif length == c: result = 1 else: result = length // c + 1 count += result return count lines_length = get_lines_length(prisoners) print(get_count(lines_length, c))
80
108
19,865,600
226021405
n, t, c = (int(x) for x in input().split()) severities = input().split() ways = 0 run = 0 for s in severities: s = int(s) if s > t: run = 0 else: run += 1 if run >= c: ways += 1 print(ways)
Codeforces Round 244 (Div. 2)
CF
2,014
1
256
Prison Transfer
The prison of your city has n prisoners. As the prison can't accommodate all of them, the city mayor has decided to transfer c of the prisoners to a prison located in another city. For this reason, he made the n prisoners to stand in a line, with a number written on their chests. The number is the severity of the crime he/she has committed. The greater the number, the more severe his/her crime was. Then, the mayor told you to choose the c prisoners, who will be transferred to the other prison. He also imposed two conditions. They are, - The chosen c prisoners has to form a contiguous segment of prisoners. - Any of the chosen prisoner's crime level should not be greater then t. Because, that will make the prisoner a severe criminal and the mayor doesn't want to take the risk of his running away during the transfer. Find the number of ways you can choose the c prisoners.
The first line of input will contain three space separated integers n (1 ≤ n ≤ 2·105), t (0 ≤ t ≤ 109) and c (1 ≤ c ≤ n). The next line will contain n space separated integers, the ith integer is the severity ith prisoner's crime. The value of crime severities will be non-negative and will not exceed 109.
Print a single integer — the number of ways you can choose the c prisoners.
null
null
[{"input": "4 3 3\n2 3 1 1", "output": "2"}, {"input": "1 1 1\n2", "output": "0"}, {"input": "11 4 2\n2 2 0 7 3 2 2 4 9 1 4", "output": "6"}]
1,100
["data structures", "implementation"]
80
[{"input": "4 3 3\r\n2 3 1 1\r\n", "output": "2\r\n"}, {"input": "1 1 1\r\n2\r\n", "output": "0\r\n"}, {"input": "11 4 2\r\n2 2 0 7 3 2 2 4 9 1 4\r\n", "output": "6\r\n"}, {"input": "57 2 10\r\n7 5 2 7 4 1 0 5 2 9 2 9 8 6 6 5 9 6 8 1 0 1 0 3 2 6 5 2 8 8 8 8 0 9 4 3 6 6 2 4 5 1 2 0 1 7 1 1 5 4 5 0 7 5 1 9 6\r\n", "output": "0\r\n"}, {"input": "2 228885628 1\r\n90897004 258427916\r\n", "output": "1\r\n"}, {"input": "3 1 1\r\n1 2 1\r\n", "output": "2\r\n"}, {"input": "3 3 3\r\n3 2 3\r\n", "output": "1\r\n"}, {"input": "4 2 2\r\n1 3 3 2\r\n", "output": "0\r\n"}, {"input": "1 228 1\r\n1\r\n", "output": "1\r\n"}]
false
stdio
null
true
449/B
449
B
PyPy 3
TESTS
3
1,169
61,030,400
131712818
import bisect import collections import copy import enum import functools import heapq import itertools import math import random import re import sys import time import string from typing import List sys.setrecursionlimit(3001) input = sys.stdin.readline n,m,k = map(int,input().split()) g = collections.defaultdict(list) for _ in range(m): u,v,x = map(int,input().split()) g[u].append([v,x,0]) g[v].append([u,x,0]) for _ in range(k): s, y = map(int, input().split()) g[1].append([s,y,1]) dist = [float("inf")]*(n+1) r = 0 def dijkstra(start): global r q = [(0, start,0)] while q: dis, f,node = heapq.heappop(q) if dist[node] <= dis: continue r+=f dist[node] = dis for next_node, w,f in g[node]: if dist[next_node] == float("inf"): heapq.heappush(q, (dis+w,f,next_node)) return dist dijkstra(1) print(k-r)
45
1,871
131,072,000
167802107
import sys import math import collections from heapq import heappush, heappop input = sys.stdin.readline ints = lambda: list(map(int, input().split())) n, m, k = ints() graph = [[] for _ in range(n + 1)] for _ in range(m): a, b, c = ints() graph[a].append((b, c, 0)) graph[b].append((a, c, 0)) trains = [[] for _ in range(n + 1)] for _ in range(k): s, y = ints() trains[s].append(y) graph[1].append((s, y, 1)) graph[s].append((1, y, 1)) distances = [math.inf for _ in range(n + 1)] distances[1] = 0 visited = [False for _ in range(n + 1)] q = [] heappush(q, (0, 1)) while q: d, node = heappop(q) if visited[node]: continue visited[node] = True for (nb, c, t) in graph[node]: total = c + distances[node] if total < distances[nb] and not visited[nb]: distances[nb] = total heappush(q, (distances[nb], nb)) ans = 0 for i in range(2, n + 1): g_r = 0 g_t = 0 for (nb, c, t) in graph[i]: if distances[nb] + c == distances[i]: if t: g_t += 1 else: g_r += 1 if g_r > 0: ans += len(trains[i]) else: ans += len(trains[i]) - 1 print(ans)
Codeforces Round 257 (Div. 1)
CF
2,014
2
256
Jzzhu and Cities
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi. Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
Output a single integer representing the maximum number of the train routes which can be closed.
null
null
[{"input": "5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5", "output": "2"}, {"input": "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3", "output": "2"}]
2,000
["graphs", "greedy", "shortest paths"]
45
[{"input": "5 5 3\r\n1 2 1\r\n2 3 2\r\n1 3 3\r\n3 4 4\r\n1 5 5\r\n3 5\r\n4 5\r\n5 5\r\n", "output": "2\r\n"}, {"input": "2 2 3\r\n1 2 2\r\n2 1 3\r\n2 1\r\n2 2\r\n2 3\r\n", "output": "2\r\n"}, {"input": "5 4 3\r\n1 2 999999999\r\n2 3 1000000000\r\n3 4 529529529\r\n5 1 524524524\r\n5 524444444\r\n5 529999999\r\n2 1000000000\r\n", "output": "2\r\n"}, {"input": "3 2 5\r\n1 2 2\r\n2 3 4\r\n3 5\r\n3 5\r\n3 5\r\n3 6\r\n3 7\r\n", "output": "4\r\n"}, {"input": "5 5 3\r\n1 2 999999999\r\n2 3 1000000000\r\n3 4 529529529\r\n5 1 524524524\r\n5 3 1000000000\r\n5 524444444\r\n5 529999999\r\n2 1000000000\r\n", "output": "2\r\n"}, {"input": "2 1 5\r\n1 2 4\r\n2 3\r\n2 5\r\n2 4\r\n2 4\r\n2 5\r\n", "output": "4\r\n"}, {"input": "3 3 6\r\n1 2 499999999\r\n2 3 500000000\r\n1 3 999999999\r\n2 499999999\r\n2 500000000\r\n2 499999999\r\n3 999999999\r\n3 1000000000\r\n3 1000000000\r\n", "output": "6\r\n"}, {"input": "2 1 1\r\n1 2 1\r\n2 1000000000\r\n", "output": "1\r\n"}, {"input": "3 2 2\r\n1 2 4\r\n2 3 4\r\n2 2\r\n3 6\r\n", "output": "1\r\n"}, {"input": "5 5 2\r\n1 2 100\r\n2 3 100\r\n3 4 100\r\n4 5 20\r\n2 5 5\r\n5 50\r\n4 1\r\n", "output": "1\r\n"}, {"input": "3 2 2\r\n1 2 100\r\n2 3 1\r\n2 1\r\n3 3\r\n", "output": "1\r\n"}]
false
stdio
null
true
294/A
294
A
Python 3
TESTS
26
62
0
160229598
# 294A n = int(input()) wires = list(map(int, input().split())) m = int(input()) for i in range(m): coords = list(map(int, input().split())) x = coords[0] y = coords[1] # how many birds to right and left of dead bird left = y - 1; right = wires[x-1] - y wires[x-1] = 0 # all birds (alive or dead) leave the wire. # check if top or bottom wire. if x == n: # bottom, birds to right fly away, birds to left go up 1. wires[x-2] += left elif x == 1: # top, birds to left fly away, birds to right go down 1. wires[x] += right else: wires[x-2] += left wires[x] += right for num in wires: print(num)
31
62
0
187419590
# tc = int(input()) for i in range(1): n = int(input()) li =list(map(int,input().split())) x = int(input()) ans = 0 for i in range(x): lix = list(map(int,input().split())) if lix[0]-2>=0: li[lix[0]-2] += lix[1]-1 if lix[0]<n: li[lix[0]] += li[lix[0]-1]-lix[1] li[lix[0]-1] = 0 for i in li: print(i)
Codeforces Round 178 (Div. 2)
CF
2,013
2
256
Shaass and Oskols
Shaass has decided to hunt some birds. There are n horizontal electricity wires aligned parallel to each other. Wires are numbered 1 to n from top to bottom. On each wire there are some oskols sitting next to each other. Oskol is the name of a delicious kind of birds in Shaass's territory. Supposed there are ai oskols sitting on the i-th wire. Sometimes Shaass shots one of the birds and the bird dies (suppose that this bird sat at the i-th wire). Consequently all the birds on the i-th wire to the left of the dead bird get scared and jump up on the wire number i - 1, if there exists no upper wire they fly away. Also all the birds to the right of the dead bird jump down on wire number i + 1, if there exists no such wire they fly away. Shaass has shot m birds. You're given the initial number of birds on each wire, tell him how many birds are sitting on each wire after the shots.
The first line of the input contains an integer n, (1 ≤ n ≤ 100). The next line contains a list of space-separated integers a1, a2, ..., an, (0 ≤ ai ≤ 100). The third line contains an integer m, (0 ≤ m ≤ 100). Each of the next m lines contains two integers xi and yi. The integers mean that for the i-th time Shaass shoot the yi-th (from left) bird on the xi-th wire, (1 ≤ xi ≤ n, 1 ≤ yi). It's guaranteed there will be at least yi birds on the xi-th wire at that moment.
On the i-th line of the output print the number of birds on the i-th wire.
null
null
[{"input": "5\n10 10 10 10 10\n5\n2 5\n3 13\n2 12\n1 13\n4 6", "output": "0\n12\n5\n0\n16"}, {"input": "3\n2 4 1\n1\n2 2", "output": "3\n0\n3"}]
800
["implementation", "math"]
31
[{"input": "5\r\n10 10 10 10 10\r\n5\r\n2 5\r\n3 13\r\n2 12\r\n1 13\r\n4 6\r\n", "output": "0\r\n12\r\n5\r\n0\r\n16\r\n"}, {"input": "3\r\n2 4 1\r\n1\r\n2 2\r\n", "output": "3\r\n0\r\n3\r\n"}, {"input": "5\r\n58 51 45 27 48\r\n5\r\n4 9\r\n5 15\r\n4 5\r\n5 8\r\n1 43\r\n", "output": "0\r\n66\r\n57\r\n7\r\n0\r\n"}, {"input": "10\r\n48 53 10 28 91 56 81 2 67 52\r\n2\r\n2 40\r\n6 51\r\n", "output": "87\r\n0\r\n23\r\n28\r\n141\r\n0\r\n86\r\n2\r\n67\r\n52\r\n"}, {"input": "2\r\n72 45\r\n6\r\n1 69\r\n2 41\r\n1 19\r\n2 7\r\n1 5\r\n2 1\r\n", "output": "0\r\n0\r\n"}, {"input": "10\r\n95 54 36 39 98 30 19 24 14 12\r\n3\r\n9 5\r\n8 15\r\n7 5\r\n", "output": "95\r\n54\r\n36\r\n39\r\n98\r\n34\r\n0\r\n28\r\n13\r\n21\r\n"}, {"input": "1\r\n100\r\n1\r\n1 100\r\n", "output": "0\r\n"}, {"input": "1\r\n100\r\n1\r\n1 1\r\n", "output": "0\r\n"}, {"input": "1\r\n50\r\n1\r\n1 25\r\n", "output": "0\r\n"}, {"input": "2\r\n50 0\r\n1\r\n1 1\r\n", "output": "0\r\n49\r\n"}, {"input": "1\r\n10\r\n0\r\n", "output": "10\r\n"}]
false
stdio
null
true
294/C
294
C
Python 3
TESTS
7
93
307,200
101680806
from sys import stdin, stdout from math import pow def main(): N, M = 2000, 1000000007 fact = factorial(N, M) n, m = readline() onn = [0] + sorted(readline()) + [n + 1] off = [] for i in range(1, len(onn)): off.append(onn[i] - onn[i - 1] - 1) answer = fact[sum(off)] div = 1 for i in range(len(off)): div *= fact[off[i]] % M if 0 < i < len(off) - 1 and off[i] > 1: answer *= pow(2, off[i] - 1) % M return int(answer / div) def factorial(N, M): fact = [1] * N for i in range(2, 2000): fact[i] = i * fact[i - 1] % M return fact def readline(): return map(int, stdin.readline().strip().split()) if __name__ == '__main__': stdout.write(str(main()) + '\n')
30
109
307,200
54146781
# https://codeforces.com/problemset/problem/294/C import sys import math mod = 10 ** 9 + 7 def factorial(n): x = 1 for i in range(n): yield x x = x * (i + 1) % mod f = list(factorial(1001)) def C(n, k): return f[n] * pow(f[k], mod - 2, mod) * pow(f[n - k], mod - 2, mod) % mod def main(): # sys.stdin = open('E:\\Sublime\\in.txt', 'r') # sys.stdout = open('E:\\Sublime\\out.txt', 'w') # sys.stderr = open('E:\\Sublime\\err.txt', 'w') # n = int(sys.stdin.readline().strip()) # a, b = map(int, sys.stdin.readline().strip().split()[:2]) n, m = [int(x) for x in sys.stdin.readline().strip().split()] b = [-1] + [int(x) - 1 for x in sys.stdin.readline().strip().split()] + [n] b.sort() ans = 1 cur = n - m for i in range(1, len(b)): l = b[i] - b[i - 1] - 1 ans = ans * C(cur, l) % mod if (l > 0) and (i > 1) and (i < len(b) - 1): ans = ans * pow(2, l - 1, mod) % mod cur -= l print(ans) if __name__ == '__main__': main() # hajj #        __ #      />  フ #      |  _  _ l #      /` ミ_xノ #      /      | #     /  ヽ   ノ #     │  | | | #  / ̄|   | | | #  | ( ̄ヽ__ヽ_)__) #  \二つ
Codeforces Round 178 (Div. 2)
CF
2,013
1
256
Shaass and Lights
There are n lights aligned in a row. These lights are numbered 1 to n from left to right. Initially some of the lights are switched on. Shaass wants to switch all the lights on. At each step he can switch a light on (this light should be switched off at that moment) if there's at least one adjacent light which is already switched on. He knows the initial state of lights and he's wondering how many different ways there exist to switch all the lights on. Please find the required number of ways modulo 1000000007 (109 + 7).
The first line of the input contains two integers n and m where n is the number of lights in the sequence and m is the number of lights which are initially switched on, (1 ≤ n ≤ 1000, 1 ≤ m ≤ n). The second line contains m distinct integers, each between 1 to n inclusive, denoting the indices of lights which are initially switched on.
In the only line of the output print the number of different possible ways to switch on all the lights modulo 1000000007 (109 + 7).
null
null
[{"input": "3 1\n1", "output": "1"}, {"input": "4 2\n1 4", "output": "2"}, {"input": "11 2\n4 8", "output": "6720"}]
1,900
["combinatorics", "number theory"]
30
[{"input": "3 1\r\n1\r\n", "output": "1\r\n"}, {"input": "4 2\r\n1 4\r\n", "output": "2\r\n"}, {"input": "11 2\r\n4 8\r\n", "output": "6720\r\n"}, {"input": "4 2\r\n1 3\r\n", "output": "2\r\n"}, {"input": "4 4\r\n1 2 3 4\r\n", "output": "1\r\n"}, {"input": "4 2\r\n1 3\r\n", "output": "2\r\n"}, {"input": "4 4\r\n1 2 3 4\r\n", "output": "1\r\n"}, {"input": "1000 3\r\n100 900 10\r\n", "output": "727202008\r\n"}, {"input": "74 13\r\n6 14 19 20 21 24 30 43 58 61 69 70 73\r\n", "output": "16623551\r\n"}, {"input": "74 13\r\n6 14 19 20 21 24 30 43 58 61 69 70 73\r\n", "output": "16623551\r\n"}, {"input": "74 13\r\n6 14 19 20 21 24 30 43 58 61 69 70 73\r\n", "output": "16623551\r\n"}, {"input": "74 13\r\n6 14 19 20 21 24 30 43 58 61 69 70 73\r\n", "output": "16623551\r\n"}, {"input": "74 13\r\n6 14 19 20 21 24 30 43 58 61 69 70 73\r\n", "output": "16623551\r\n"}, {"input": "74 13\r\n6 14 19 20 21 24 30 43 58 61 69 70 73\r\n", "output": "16623551\r\n"}, {"input": "68 37\r\n1 2 3 6 7 8 10 11 12 14 16 18 22 23 24 26 30 31 32 35 37 39 41 42 45 47 50 51 52 54 58 59 61 62 63 64 68\r\n", "output": "867201120\r\n"}, {"input": "132 48\r\n6 7 8 12 15 17 18 19 22 24 25 26 30 33 35 38 40 43 46 49 50 51 52 54 59 60 66 70 76 79 87 89 91 92 94 98 99 101 102 105 106 109 113 115 116 118 120 129\r\n", "output": "376947760\r\n"}, {"input": "36 24\r\n1 7 8 10 11 12 13 14 15 16 17 19 21 22 25 26 27 28 29 30 31 32 35 36\r\n", "output": "63866880\r\n"}, {"input": "100 2\r\n11 64\r\n", "output": "910895596\r\n"}, {"input": "1000 1\r\n35\r\n", "output": "253560421\r\n"}, {"input": "1000 2\r\n747 798\r\n", "output": "474746180\r\n"}, {"input": "1000 3\r\n804 811 984\r\n", "output": "600324842\r\n"}, {"input": "1 1\r\n1\r\n", "output": "1\r\n"}]
false
stdio
null
true
491/B
491
B
Python 3
TESTS
1
46
0
8769508
N,M = map(int,input().split()) C = int(input()) Ci = [list(map(int,input().split())) for i in range(C)] H = int(input()) Hi = [list(map(int,input().split())) for i in range(H)] xi = 0 yi = 0 for y in range(2): for x in range(C): if y == 0: xi += x else: yi += x xi //= C yi //= C minimum = 10000000000 nmin = 0 for y in range(H): z = Hi[y][0] + Hi[y][1] if z < minimum: minimum = z nmin = y maximum = 0 for x in range(C): e = abs(Ci[x][0]-Hi[nmin][0]) + abs(Ci[x][1]-Hi[nmin][1]) if e > maximum: maximum = e print(maximum) print(nmin+1)
28
1,747
10,035,200
72031483
N, M = input().split() a, b, c, d = [int(1e10) for _ in range(4)] for i in range(int(input())): x, y = list(map(int, input().split())) a, b, c, d = min(a, x + y), min(b, x - y), min(c, - x + y), min(d, - x - y) res, pos = int(1e10), 0 for i in range(int(input())): x, y = list(map(int, input().split())) ans = max(max(x + y - a, x - y - b), max( - x + y - c, - x - y - d)) if ans < res: pos = i + 1 res = ans print(res, pos, sep = '\n')
Testing Round 11
CF
2,014
2
256
New York Hotel
Think of New York as a rectangular grid consisting of N vertical avenues numerated from 1 to N and M horizontal streets numerated 1 to M. C friends are staying at C hotels located at some street-avenue crossings. They are going to celebrate birthday of one of them in the one of H restaurants also located at some street-avenue crossings. They also want that the maximum distance covered by one of them while traveling to the restaurant to be minimum possible. Help friends choose optimal restaurant for a celebration. Suppose that the distance between neighboring crossings are all the same equal to one kilometer.
The first line contains two integers N и M — size of the city (1 ≤ N, M ≤ 109). In the next line there is a single integer C (1 ≤ C ≤ 105) — the number of hotels friends stayed at. Following C lines contain descriptions of hotels, each consisting of two coordinates x and y (1 ≤ x ≤ N, 1 ≤ y ≤ M). The next line contains an integer H — the number of restaurants (1 ≤ H ≤ 105). Following H lines contain descriptions of restaurants in the same format. Several restaurants and hotels may be located near the same crossing.
In the first line output the optimal distance. In the next line output index of a restaurant that produces this optimal distance. If there are several possibilities, you are allowed to output any of them.
null
null
[{"input": "10 10\n2\n1 1\n3 3\n2\n1 10\n4 4", "output": "6\n2"}]
2,100
["greedy", "math"]
28
[{"input": "10 10\r\n2\r\n1 1\r\n3 3\r\n2\r\n1 10\r\n4 4\r\n", "output": "6\r\n2\r\n"}, {"input": "100 100\r\n10\r\n53 20\r\n97 6\r\n12 74\r\n48 92\r\n97 13\r\n47 96\r\n75 32\r\n69 21\r\n95 75\r\n1 54\r\n10\r\n36 97\r\n41 1\r\n1 87\r\n39 23\r\n27 44\r\n73 97\r\n1 1\r\n6 26\r\n48 3\r\n5 69\r\n", "output": "108\r\n4\r\n"}, {"input": "100 100\r\n10\r\n86 72\r\n25 73\r\n29 84\r\n34 33\r\n29 20\r\n84 83\r\n41 80\r\n22 22\r\n16 89\r\n77 49\r\n1\r\n4 23\r\n", "output": "140\r\n1\r\n"}, {"input": "100 100\r\n1\r\n66 77\r\n10\r\n70 11\r\n76 69\r\n79 39\r\n90 3\r\n38 87\r\n61 81\r\n98 66\r\n63 68\r\n62 93\r\n53 36\r\n", "output": "9\r\n6\r\n"}, {"input": "1000000000 1000000000\r\n1\r\n1 1\r\n1\r\n1000000000 1000000000\r\n", "output": "1999999998\r\n1\r\n"}, {"input": "123456789 987654321\r\n1\r\n312 987654321\r\n1\r\n123456789 213\r\n", "output": "1111110585\r\n1\r\n"}, {"input": "453456789 987654321\r\n1\r\n443943901 1\r\n1\r\n1354 213389832\r\n", "output": "657332378\r\n1\r\n"}, {"input": "923456789 987654321\r\n1\r\n443943901 132319791\r\n1\r\n1354 560\r\n", "output": "576261778\r\n1\r\n"}, {"input": "100 100\r\n1\r\n1 100\r\n1\r\n1 100\r\n", "output": "0\r\n1\r\n"}, {"input": "1 1\r\n1\r\n1 1\r\n1\r\n1 1\r\n", "output": "0\r\n1\r\n"}, {"input": "1000000000 1000000000\r\n2\r\n1 1\r\n3 3\r\n2\r\n1 10\r\n4 4\r\n", "output": "6\r\n2\r\n"}]
false
stdio
null
true
297/A
297
A
Python 3
TESTS
2
124
7,065,600
37388457
#!/usr/bin/env python3 # -*- coding: utf-8 -*- a=[] b=[] a=input() b=input() cnta=0 cntb=0 for i,val in enumerate(a): if val=='1': cnta=cnta+1 for i,val in enumerate(b): if val=='1': cntb=cntb+1 if cnta&1==1: pa=cnta+1 if cnta>=cntb: print('YES') else: print('NO')
79
186
0
226402581
a,b=input(),input() ca,cb=a.count('1'),b.count('1') print('YES' if ca+(ca&1)>=cb else 'NO')
Codeforces Round 180 (Div. 1)
CF
2,013
1
256
Parity Game
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a and b. Then you try to turn a into b using two types of operations: - Write parity(a) to the end of a. For example, $$1010 \rightarrow 10100$$. - Remove the first character of a. For example, $$1001 \rightarrow 001$$. You cannot perform this operation if a is empty. You can use as many operations as you want. The problem is, is it possible to turn a into b? The parity of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise.
The first line contains the string a and the second line contains the string b (1 ≤ |a|, |b| ≤ 1000). Both strings contain only the characters "0" and "1". Here |x| denotes the length of the string x.
Print "YES" (without quotes) if it is possible to turn a into b, and "NO" (without quotes) otherwise.
null
In the first sample, the steps are as follows: 01011 → 1011 → 011 → 0110
[{"input": "01011\n0110", "output": "YES"}, {"input": "0011\n1110", "output": "NO"}]
1,700
["constructive algorithms"]
79
[{"input": "01011\r\n0110\r\n", "output": "YES\r\n"}, {"input": "0011\r\n1110\r\n", "output": "NO\r\n"}, {"input": "11111\r\n111111\r\n", "output": "YES\r\n"}, {"input": "0110011\r\n01100110\r\n", "output": "YES\r\n"}, {"input": "10000100\r\n011110\r\n", "output": "NO\r\n"}, {"input": "1\r\n0\r\n", "output": "YES\r\n"}, {"input": "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111\r\n11\r\n", "output": "YES\r\n"}, {"input": "11\r\n111\r\n", "output": "NO\r\n"}, {"input": "1\r\n1\r\n", "output": "YES\r\n"}, {"input": "1\r\n0\r\n", "output": "YES\r\n"}]
false
stdio
null
true
58/D
58
D
PyPy 3
TESTS
5
310
0
95808360
import sys from array import array # noqa: F401 def input(): return sys.stdin.buffer.readline().decode('utf-8') n = int(input()) a = [input().rstrip() for _ in range(n)] d = input().rstrip() words = [[] for _ in range(100)] total_len = 0 for word in a: words[len(word)].append(word) total_len += len(word) for i in range(1, 11): words[i].sort(reverse=True) width = total_len // (n // 2) ans = [] for i in range(n // 2): res = '~' * 100 res_j = -1 for j in range(1, width): if words[j] and words[width - j] and res > words[j][-1] + d + words[width - j][-1]: res = words[j][-1] + d + words[width - j][-1] res_j = j ans.append(res) words[res_j].pop() words[width - res_j].pop() sys.stdout.buffer.write('\n'.join(ans).encode('utf-8'))
46
372
1,740,800
213673188
from collections import deque n = int(input()) a = [input() for i in range(n)] a.sort() dq = deque(a) # print(dq) d = input() n //= 2 common_len = sum(len(s) for s in a) // n res = [] for i in range(n): s1 = dq.popleft() for s2 in dq: if len(s1) + len(s2) == common_len: dq.remove(s2) res.append(min(s1+d+s2, s2+d+s1)) break for x in sorted(res): print(x)
Codeforces Beta Round 54 (Div. 2)
CF
2,011
2
256
Calendar
BerOilGasDiamondBank has branches in n cities, at that n is an even number. The bank management wants to publish a calendar with the names of all those cities written in two columns: the calendar should consist of exactly n / 2 lines of strictly equal length, each of which contains exactly two names and exactly one separator character between them. The name of every city should be used in the calendar exactly once. For historical reasons the symbol d is used as the separator of words in the calendar. The BerOilGasDiamondBank management wants to show that all its branches are equally important to it, that's why the order of their appearance in the calendar should be following: if we "glue"(concatinate) all the n / 2 calendar lines (from top to bottom) to make a single line, then the lexicographically minimal line is obtained. No separator character will be used to separate calendar lines. For example, if the lines are "bertown!berville", "newberville!bera", then the resulting line is "bertown!bervillenewberville!bera". In some sense one has to find the lexicographically minimal calendar, where the comparison of calendars happens line by line. Help BerOilGasDiamondBank and construct the required calendar.
The first line contains an integer n (1 ≤ n ≤ 104, n is even) which is the number of branches. Then follow n lines which are the names of the cities. All the names consist of lowercase Latin letters; their lengths are no less than 1 and no more than 10 symbols. The next line contains a single symbol d (d has an ASCII-code from 33 to 126 inclusively, excluding lowercase Latin letters) which is the separator between words in the calendar lines. It is guaranteed that the calendar is possible to be constructed and all the names are different.
Print n / 2 lines of similar length which are the required calendar. Every line should contain exactly two words and exactly one separator between them. If there are several solutions, print the lexicographically minimal one. The lexicographical comparison of lines is realized by the "<" operator in the modern programming languages.
null
null
[{"input": "4\nb\naa\nhg\nc\n.", "output": "aa.b\nc.hg"}, {"input": "2\naa\na\n!", "output": "a!aa"}, {"input": "2\naa\na\n|", "output": "aa|a"}]
2,000
["greedy", "strings"]
46
[{"input": "4\r\nb\r\naa\r\nhg\r\nc\r\n.\r\n", "output": "aa.b\r\nc.hg\r\n"}, {"input": "2\r\naa\r\na\r\n!\r\n", "output": "a!aa\r\n"}, {"input": "2\r\naa\r\na\r\n|\r\n", "output": "aa|a\r\n"}, {"input": "4\r\nqhcivbxotj\r\nirgxzzxvw\r\npxdmcyszvk\r\nyyaevcdal\r\n~\r\n", "output": "irgxzzxvw~pxdmcyszvk\r\nqhcivbxotj~yyaevcdal\r\n"}, {"input": "8\r\nbad\r\nrnnpg\r\njvcjsxfob\r\nad\r\nairnnpg\r\nqury\r\njvcjsxfo\r\nquryai\r\n6\r\n", "output": "ad6jvcjsxfob\r\nairnnpg6qury\r\nbad6jvcjsxfo\r\nquryai6rnnpg\r\n"}, {"input": "6\r\neh\r\nehkhdp\r\ngque\r\nkhdptvgque\r\ntvgque\r\nehkhdptv\r\n}\r\n", "output": "ehkhdptv}gque\r\nehkhdp}tvgque\r\neh}khdptvgque\r\n"}, {"input": "10\r\ndoecgzo\r\ntjptpqp\r\noitegxzwlp\r\nmwsrwmeyeg\r\nsmapaqanak\r\nsmapaqa\r\nqghrydm\r\nnakqghrydm\r\nmedoraus\r\nnyvgozjdf\r\n|\r\n", "output": "doecgzo|mwsrwmeyeg\r\nmedoraus|nyvgozjdf\r\nnakqghrydm|qghrydm\r\noitegxzwlp|smapaqa\r\nsmapaqanak|tjptpqp\r\n"}, {"input": "30\r\nd\r\nahx\r\nr\r\nyd\r\np\r\nnhy\r\na\r\ntqt\r\nctp\r\ntp\r\nho\r\nry\r\nm\r\ng\r\ns\r\nn\r\nct\r\nsc\r\nqr\r\nrry\r\ny\r\nhxm\r\nqrr\r\nsct\r\ncwu\r\nq\r\ndk\r\nrf\r\nhyd\r\nnh\r\n$\r\n", "output": "a$ahx\r\nct$dk\r\nctp$d\r\ncwu$g\r\nho$nh\r\nhxm$m\r\nhyd$n\r\nnhy$p\r\nq$qrr\r\nqr$rf\r\nr$rry\r\nry$sc\r\ns$sct\r\ntp$yd\r\ntqt$y\r\n"}, {"input": "14\r\neskrrgzq\r\nxbmynhxfg\r\nwwffafny\r\nfaxcnrqkkb\r\nfaxcnrqk\r\nkbwwffafny\r\nmnborvqeae\r\nranfahuebj\r\neskrrgzqk\r\nfaxcnrqkk\r\ncznaycxe\r\nrnkgfgyq\r\nkxbmynhxfg\r\nbwwffafny\r\n}\r\n", "output": "bwwffafny}eskrrgzqk\r\ncznaycxe}faxcnrqkkb\r\neskrrgzq}kbwwffafny\r\nfaxcnrqkk}xbmynhxfg\r\nfaxcnrqk}kxbmynhxfg\r\nmnborvqeae}rnkgfgyq\r\nranfahuebj}wwffafny\r\n"}, {"input": "34\r\nobseknnnqk\r\ncvyvvbcgb\r\nxvmhfzfl\r\ngrtp\r\nhbcbhj\r\nknnnqk\r\ncyud\r\nkuaeui\r\naeui\r\nlhpdobsekn\r\ncxmigej\r\ncvyvvbcgbs\r\nuwuu\r\nnnqk\r\npzcftfrrqp\r\nnwsyrgqa\r\nxvmhfzflku\r\nndcis\r\nxhaznwqsgk\r\ncftfrrqp\r\nkakdggegew\r\njjzvokhh\r\nlhpdobse\r\nxjjzvokhh\r\nlhpd\r\nsuwuu\r\ntuatbwof\r\nvpsuday\r\nndcisx\r\nfggxici\r\nbfnipz\r\nknzjio\r\noirksxb\r\nbfni\r\n~\r\n", "output": "aeui~cvyvvbcgbs\r\nbfnipz~cftfrrqp\r\nbfni~kakdggegew\r\ncvyvvbcgb~ndcis\r\ncxmigej~fggxici\r\ncyud~lhpdobsekn\r\ngrtp~obseknnnqk\r\nhbcbhj~jjzvokhh\r\nknnnqk~lhpdobse\r\nknzjio~nwsyrgqa\r\nkuaeui~tuatbwof\r\nlhpd~pzcftfrrqp\r\nndcisx~xvmhfzfl\r\nnnqk~xhaznwqsgk\r\noirksxb~vpsuday\r\nsuwuu~xjjzvokhh\r\nuwuu~xvmhfzflku\r\n"}, {"input": "58\r\nesgdfmf\r\nxfkluadj\r\nqhvh\r\njwhuyhm\r\nmgi\r\nysgc\r\nvhhenku\r\npb\r\ntr\r\nu\r\njyrpjnpd\r\nkluadjo\r\nopb\r\ncopb\r\ngcyhceo\r\nr\r\ndjo\r\nxfklu\r\neo\r\nadjo\r\nfkluadjo\r\nybe\r\nwljwh\r\nqhvhh\r\nrhgotp\r\nyhceo\r\nuyhm\r\nvdd\r\nyhm\r\nysgcyhc\r\nvddrhg\r\nril\r\nwljwhu\r\nx\r\nqh\r\nhceo\r\ntfcopb\r\nmgitfc\r\nvddrh\r\nmgitfco\r\nxf\r\nmgitf\r\ncyoybe\r\notp\r\no\r\nljwhuyhm\r\nysgcy\r\nhhenku\r\nwl\r\ngotp\r\nqhv\r\nw\r\nhenku\r\nenku\r\nys\r\nrilcyo\r\nxfklua\r\nqhvhhenk\r\n|\r\n", "output": "adjo|henku\r\ncopb|mgitf\r\ncyoybe|djo\r\nenku|qhvhh\r\neo|esgdfmf\r\nfkluadjo|o\r\ngcyhceo|pb\r\ngotp|vddrh\r\nhceo|wljwh\r\nhhenku|mgi\r\njwhuyhm|qh\r\njyrpjnpd|r\r\nkluadjo|tr\r\nljwhuyhm|u\r\nmgitfco|wl\r\nmgitfc|opb\r\notp|rhgotp\r\nqhvhhenk|w\r\nqhvh|xfklu\r\nqhv|rilcyo\r\nril|tfcopb\r\nuyhm|yhceo\r\nvddrhg|vdd\r\nvhhenku|xf\r\nwljwhu|ybe\r\nxfkluadj|x\r\nxfklua|yhm\r\nysgcyhc|ys\r\nysgcy|ysgc\r\n"}, {"input": "76\r\nsd\r\nwhx\r\nk\r\nce\r\nthm\r\nbyfi\r\npju\r\nbn\r\ndz\r\non\r\nizr\r\niswh\r\nl\r\nwig\r\ns\r\nju\r\nsr\r\nie\r\nx\r\nbth\r\nzvi\r\nlxth\r\ndmzz\r\nbnqq\r\nan\r\ny\r\ng\r\nvlj\r\nc\r\nhdu\r\nlx\r\nwkyd\r\ndb\r\nrmr\r\nrv\r\nis\r\ngv\r\nu\r\nbyf\r\nm\r\nqqb\r\nwe\r\nb\r\ne\r\nnioo\r\niek\r\no\r\nymk\r\nifpw\r\nisw\r\nammm\r\ncgk\r\ncq\r\nhhv\r\nq\r\nat\r\nd\r\ney\r\nn\r\nrhq\r\ncecg\r\nqsh\r\nak\r\nhx\r\nrve\r\nlaly\r\ni\r\nbnsa\r\nioou\r\nsk\r\nkg\r\nqshs\r\nwzmn\r\nupt\r\nvwvr\r\nyjj\r\nN\r\n", "output": "akNbth\r\nammmNb\r\nanNbyf\r\natNcgk\r\nbnNhdu\r\nbnqqNc\r\nbnsaNd\r\nbyfiNe\r\nceNhhv\r\ncecgNg\r\ncqNiek\r\ndbNisw\r\ndmzzNi\r\ndzNizr\r\neyNpju\r\ngvNqqb\r\nhxNqsh\r\nieNrhq\r\nifpwNk\r\nioouNl\r\nisNrmr\r\niswhNm\r\njuNrve\r\nkgNthm\r\nlalyNn\r\nlxNupt\r\nlxthNo\r\nniooNq\r\nonNvlj\r\nqshsNs\r\nrvNwhx\r\nsdNwig\r\nskNyjj\r\nsrNymk\r\nuNvwvr\r\nweNzvi\r\nwkydNx\r\nwzmnNy\r\n"}, {"input": "10\r\npo\r\negf\r\ne\r\ngfuzaqsi\r\nsi\r\nhpo\r\nuldiig\r\negfuzaq\r\nuldiigh\r\nuzaqsi\r\n{\r\n", "output": "egfuzaq{po\r\negf{uldiig\r\ne{gfuzaqsi\r\nhpo{uzaqsi\r\nsi{uldiigh\r\n"}, {"input": "4\r\na\r\nf\r\nz\r\nh\r\n!\r\n", "output": "a!f\r\nh!z\r\n"}]
false
stdio
null
true
580/C
580
C
PyPy 3-64
TESTS
34
343
163,225,600
226967627
import sys, threading input = sys.stdin.readline from collections import defaultdict input = sys.stdin.readline # returns the first number where key becomes true for a given delegate type key def bs(low=1, high=1, key = lambda x: True): while low <= high: mid = (low + high)//2 if key(mid): high = mid-1 else: low = mid+1 return low def main(): n, m = list(map(int, input().split())) cats = list(map(int, input().split())) graph = [[] for _ in range(n+1)] for _ in range(n-1): u, v = list(map(int, input().split())) graph[u].append(v) graph[v].append(u) seen = [0]*n def dfs(node, seen, m, n_cats): count = 0 seen[node-1] = 1 isleaf = 1 if cats[node-1]: n_cats+=1 else: n_cats = 0 if n_cats > m: return 0 for nbr in graph[node]: if seen[nbr-1] == 0: isleaf = 0 count+=dfs(nbr,seen, m, n_cats) count+=isleaf return count print(dfs(1, seen, m, 0)) # Set the stack size threading.stack_size(1 << 27) # Create and start the main thread main_thread = threading.Thread(target=main) main_thread.start() # Wait for the main thread to complete main_thread.join()
40
233
29,286,400
225884260
import sys input = sys.stdin.readline ############ ---- Input Functions ---- ############ def inp(): return int(input()) def inlt(): return list(map(int, input().split())) def insr(): s = input() return list(s[: len(s) - 1]) def invr(): return map(int, input().split()) n, m = invr() cat = inlt() adj = [[] for i in range(n)] for _ in range(n - 1): a, b = invr() adj[a - 1].append(b - 1) adj[b - 1].append(a - 1) ans = [0] stack = [(-1, 0, 0)] while stack: prev, c, cnt = stack.pop() if cat[c] == 1: cnt += 1 else: cnt = 0 if cnt > m: continue if adj[c] == [prev]: ans[0] += 1 continue for i in adj[c]: if i == prev: continue else: stack.append((c, i, cnt)) print(ans[0])
Codeforces Round 321 (Div. 2)
CF
2,015
2
256
Kefa and Park
Kefa decided to celebrate his first big salary by going to the restaurant. He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them. The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutive vertices with cats. Your task is to help Kefa count the number of restaurants where he can go.
The first line contains two integers, n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa. The second line contains n integers a1, a2, ..., an, where each ai either equals to 0 (then vertex i has no cat), or equals to 1 (then vertex i has a cat). Next n - 1 lines contains the edges of the tree in the format "xi yi" (without the quotes) (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the vertices of the tree, connected by an edge. It is guaranteed that the given set of edges specifies a tree.
A single integer — the number of distinct leaves of a tree the path to which from Kefa's home contains at most m consecutive vertices with cats.
null
Let us remind you that a tree is a connected graph on n vertices and n - 1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children. Note to the first sample test: The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can't go only to the restaurant located at vertex 2. Note to the second sample test: The restaurants are located at vertices 4, 5, 6, 7. Kefa can't go to restaurants 6, 7.
[{"input": "4 1\n1 1 0 0\n1 2\n1 3\n1 4", "output": "2"}, {"input": "7 1\n1 0 1 1 0 0 0\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7", "output": "2"}]
1,500
["dfs and similar", "graphs", "trees"]
40
[{"input": "4 1\r\n1 1 0 0\r\n1 2\r\n1 3\r\n1 4\r\n", "output": "2\r\n"}, {"input": "7 1\r\n1 0 1 1 0 0 0\r\n1 2\r\n1 3\r\n2 4\r\n2 5\r\n3 6\r\n3 7\r\n", "output": "2\r\n"}, {"input": "3 2\r\n1 1 1\r\n1 2\r\n2 3\r\n", "output": "0\r\n"}, {"input": "5 2\r\n1 1 0 1 1\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n", "output": "1\r\n"}, {"input": "6 1\r\n1 0 1 1 0 0\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n1 6\r\n", "output": "3\r\n"}, {"input": "7 3\r\n1 1 1 1 1 0 1\r\n1 2\r\n1 3\r\n2 4\r\n3 5\r\n5 6\r\n6 7\r\n", "output": "2\r\n"}, {"input": "15 2\r\n1 0 1 0 1 0 0 0 0 0 0 0 0 0 0\r\n1 2\r\n1 3\r\n2 4\r\n2 5\r\n3 6\r\n3 7\r\n4 8\r\n4 9\r\n5 10\r\n5 11\r\n6 12\r\n6 13\r\n7 14\r\n7 15\r\n", "output": "8\r\n"}, {"input": "2 1\r\n1 1\r\n2 1\r\n", "output": "0\r\n"}, {"input": "12 3\r\n1 0 1 0 1 1 1 1 0 0 0 0\r\n6 7\r\n12 1\r\n9 7\r\n1 4\r\n10 7\r\n7 1\r\n11 8\r\n5 1\r\n3 7\r\n5 8\r\n4 2\r\n", "output": "7\r\n"}]
false
stdio
null
true
3/B
3
B
Python 3
TESTS
14
1,310
10,547,200
22710047
def print_v(v): print(v, eval(v)) n,v = map(int, input().split()) #n - number of vehicles #v - volume of bus vehicle1 = [] vehicle2 = [] for i in range(n): t,p = map(int, input().split()) if t == 1: vehicle1.append((p,i+1)) else: vehicle2.append((p,i+1)) vehicle1.sort(key=lambda x: x[0],reverse=True) vehicle2.sort(key=lambda x: x[0],reverse=True) index1 = 0 index2 = 0 if n == 9875 and v == 13641: f = len(vehicle1)//100 + 1 for i in range(f): print(vehicle1[i*100:(i+1)*100]) current_v = 0 #current space in the car current_result = 0 #current maximal value numbers = [] while True: if(v - current_v >= 2): if(index2 < len(vehicle2) or index1 + 1 < len(vehicle1)): if(index2 < len(vehicle2) and index1 + 1 < len(vehicle1)): if(vehicle2[index2][0] > vehicle1[index1][0] + vehicle1[index1+1][0]): numbers.append((vehicle2[index2][0], vehicle2[index2][1], 2)) index2 += 1 current_v += 2 else: numbers.append((vehicle1[index1][0], vehicle1[index1][1], 1)) numbers.append((vehicle1[index1+1][0], vehicle1[index1+1][1], 1)) index1 += 2 current_v += 2 else: if(index2 < len(vehicle2)): numbers.append((vehicle2[index2][0], vehicle2[index2][1], 2)) index2 += 1 current_v += 2 if(index1 + 1 < len(vehicle1)): numbers.append((vehicle1[index1][0], vehicle1[index1][1], 1)) numbers.append((vehicle1[index1+1][0], vehicle1[index1+1][1], 1)) index1 += 2 current_v += 2 else: break else: break def sum_numbers(numbers): if(len(numbers) == 0): return 0 result = 0 for item in numbers: result += item[0] return result if(len(vehicle1)%2 == 1): if(v - current_v >= 1): numbers.append((vehicle1[index1][0], vehicle1[index1][1], 1)) current_v += 1 is_1_in_numbers = False is_1_in_numbers_twice = False counter = 0 for item in numbers: if item[-1] == 1: is_1_in_numbers = True counter += 1 if(counter == 2): is_1_in_numbers_twice = True break if(v - current_v == 1 and index2 < len(vehicle2) and is_1_in_numbers): temp_numbers = numbers.copy() for i in range(len(temp_numbers) - 1,-1,-1): if temp_numbers[i][-1] == 1: del(temp_numbers[i]) break temp_numbers.append((vehicle2[index2][0], vehicle2[index2][1], 2)) if (sum_numbers(temp_numbers) > sum_numbers(numbers)): numbers = temp_numbers elif (v - current_v == 0 and index2 < len(vehicle2) and is_1_in_numbers_twice): temp_numbers = numbers.copy() counter = 0 for i in range(len(temp_numbers) - 1,-1,-1): if temp_numbers[i][-1] == 1: del(temp_numbers[i]) counter += 1 if(counter == 2): break temp_numbers.append((vehicle2[index2][0], vehicle2[index2][1], 2)) if (sum_numbers(temp_numbers) > sum_numbers(numbers)): numbers = temp_numbers numbers.sort(key=lambda x:x[1]) print(sum_numbers(numbers)) for item in numbers: print(item[1])
32
466
16,076,800
185738569
from heapq import nlargest from itertools import accumulate, chain import sys n, v = map(int, sys.stdin.readline().split()) a1, a2 = [], [] i1, i2 = [], [] for i in range(n): t, p = map(int, sys.stdin.readline().split()) if t == 1: a1.append(p) i1.append(i) else: a2.append(p) i2.append(i) acc1 = list(accumulate(sorted(a1, reverse=True), initial=0)) acc2 = list(accumulate(sorted(a2, reverse=True), initial=0)) best = (0, 0, 0) for n2 in range(min(v // 2, len(a2)) + 1): n1 = min(v - 2 * n2, len(a1)) best = max(best, (acc1[n1] + acc2[n2], n1, n2)) ans, n1, n2 = best sys.stdout.write(str(ans) + "\n") ii1 = sorted(range(len(a1)), key=a1.__getitem__)[len(a1) - n1 :] ii2 = sorted(range(len(a2)), key=a2.__getitem__)[len(a2) - n2 :] sys.stdout.write(" ".join(str(i1[i] + 1) for i in ii1)) sys.stdout.write(" ") sys.stdout.write(" ".join(str(i2[i] + 1) for i in ii2)) sys.stdout.write("\n")
Codeforces Beta Round 3
ICPC
2,010
2
64
Lorry
A group of tourists is going to kayak and catamaran tour. A rented lorry has arrived to the boat depot to take kayaks and catamarans to the point of departure. It's known that all kayaks are of the same size (and each of them occupies the space of 1 cubic metre), and all catamarans are of the same size, but two times bigger than kayaks (and occupy the space of 2 cubic metres). Each waterborne vehicle has a particular carrying capacity, and it should be noted that waterborne vehicles that look the same can have different carrying capacities. Knowing the truck body volume and the list of waterborne vehicles in the boat depot (for each one its type and carrying capacity are known), find out such set of vehicles that can be taken in the lorry, and that has the maximum total carrying capacity. The truck body volume of the lorry can be used effectively, that is to say you can always put into the lorry a waterborne vehicle that occupies the space not exceeding the free space left in the truck body.
The first line contains a pair of integer numbers n and v (1 ≤ n ≤ 105; 1 ≤ v ≤ 109), where n is the number of waterborne vehicles in the boat depot, and v is the truck body volume of the lorry in cubic metres. The following n lines contain the information about the waterborne vehicles, that is a pair of numbers ti, pi (1 ≤ ti ≤ 2; 1 ≤ pi ≤ 104), where ti is the vehicle type (1 – a kayak, 2 – a catamaran), and pi is its carrying capacity. The waterborne vehicles are enumerated in order of their appearance in the input file.
In the first line print the maximum possible carrying capacity of the set. In the second line print a string consisting of the numbers of the vehicles that make the optimal set. If the answer is not unique, print any of them.
null
null
[{"input": "3 2\n1 2\n2 7\n1 3", "output": "7\n2"}]
1,900
["greedy", "sortings"]
32
[{"input": "3 2\r\n1 2\r\n2 7\r\n1 3\r\n", "output": "7\r\n2\r\n"}, {"input": "5 3\r\n1 9\r\n2 9\r\n1 9\r\n2 10\r\n1 6\r\n", "output": "24\r\n3 1 5\r\n"}, {"input": "10 10\r\n1 14\r\n2 15\r\n2 11\r\n2 12\r\n2 9\r\n1 14\r\n2 15\r\n1 9\r\n2 11\r\n2 6\r\n", "output": "81\r\n6 1 7 2 4 9\r\n"}, {"input": "20 19\r\n2 47\r\n1 37\r\n1 48\r\n2 42\r\n2 48\r\n1 38\r\n2 47\r\n1 48\r\n2 47\r\n1 41\r\n2 46\r\n1 28\r\n1 49\r\n1 45\r\n2 34\r\n1 43\r\n2 29\r\n1 46\r\n2 45\r\n2 18\r\n", "output": "630\r\n13 8 3 18 14 16 10 6 2 5 9 7 1 11\r\n"}, {"input": "50 27\r\n2 93\r\n1 98\r\n2 62\r\n1 56\r\n1 86\r\n1 42\r\n2 67\r\n2 97\r\n2 59\r\n1 73\r\n1 83\r\n2 96\r\n1 20\r\n1 66\r\n1 84\r\n1 83\r\n1 91\r\n2 97\r\n1 81\r\n2 88\r\n2 63\r\n1 99\r\n2 57\r\n1 39\r\n1 74\r\n2 88\r\n1 30\r\n2 68\r\n1 100\r\n2 57\r\n1 87\r\n1 93\r\n1 83\r\n1 100\r\n1 91\r\n1 14\r\n1 38\r\n2 98\r\n2 85\r\n2 61\r\n1 44\r\n2 93\r\n2 66\r\n2 55\r\n2 74\r\n1 67\r\n2 67\r\n1 85\r\n2 59\r\n1 83\r\n", "output": "2055\r\n34 29 22 2 32 35 17 31 5 48 15 50 33 16 11 19 25 10 46 14 4 38 18 8\r\n"}, {"input": "1 1\r\n1 600\r\n", "output": "600\r\n1\r\n"}, {"input": "10 14\r\n2 230\r\n2 516\r\n2 527\r\n2 172\r\n2 854\r\n2 61\r\n1 52\r\n2 154\r\n2 832\r\n2 774\r\n", "output": "3905\r\n5 9 10 3 2 1 4\r\n"}, {"input": "8 8\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n2 100\r\n2 100\r\n2 100\r\n2 100\r\n", "output": "400\r\n8 7 6 5\r\n"}, {"input": "8 4\r\n1 100\r\n1 100\r\n1 100\r\n1 100\r\n2 1\r\n2 1\r\n2 1\r\n2 1\r\n", "output": "400\r\n4 3 2 1\r\n"}]
false
stdio
import sys def read_lines(path): with open(path, 'r') as f: return [line.strip() for line in f.readlines() if line.strip()] def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] # Read input input_lines = read_lines(input_path) if not input_lines: print(0) return n, v = map(int, input_lines[0].split()) vehicles = [] for line in input_lines[1:1+n]: ti, pi = map(int, line.split()) vehicles.append( (ti, pi) ) # Read judge output judge_lines = read_lines(output_path) if not judge_lines: print(0) return try: judge_sum = int(judge_lines[0]) except: print(0) return judge_vehicles = [] if len(judge_lines) >=2: judge_vehicles = list(map(int, ' '.join(judge_lines[1:]).split())) # Read submission output sub_lines = read_lines(submission_path) if len(sub_lines) <1: print(0) return try: sub_sum = int(sub_lines[0]) except: print(0) return sub_vehicles = [] if len(sub_lines) >=2: sub_vehicles = list(map(int, ' '.join(sub_lines[1:]).split())) # Check sum matches if sub_sum != judge_sum: print(0) return # Check vehicle indices are valid and unique seen = set() for idx in sub_vehicles: if idx <1 or idx >n or idx in seen: print(0) return seen.add(idx) # Compute sum_t and sum_p sum_t = 0 sum_p =0 for idx in sub_vehicles: ti, pi = vehicles[idx-1] sum_t += ti sum_p += pi if sum_t >v: print(0) return if sum_p != sub_sum: print(0) return # All checks passed print(1) if __name__ == '__main__': main()
true
301/C
301
C
PyPy 3
TESTS
1
154
20,172,800
125260456
res = ''' 0??<>1 1??<>2 2??<>3 3??<>4 4??<>5 5??<>6 6??<>7 7??<>8 8??<>9 9??>>??0 ?0>>0? ?1>>1? ?2>>2? ?3>>3? ?4>>4? ?5>>5? ?6>>6? ?7>>7? ?8>>8? ?9>>9? ?>>?? 0>>0? 1>>1? 2>>2? 3>>3? 4>>4? 5>>5? 6>>6? 7>>7? 8>>8? 9>>9? ''' print(res.strip())
23
125
0
3818363
print('9??>>??0') for i in range(9): print('{}??<>{}'.format(i, i + 1)) print('??<>1') for i in range(10): print('?{0}>>{0}?'.format(i)) print('?>>??') print('>>?')
Codeforces Round 182 (Div. 1)
CF
2,013
2
256
Yaroslav and Algorithm
Yaroslav likes algorithms. We'll describe one of his favorite algorithms. 1. The algorithm receives a string as the input. We denote this input string as a. 2. The algorithm consists of some number of command. Сommand number i looks either as si >> wi, or as si <> wi, where si and wi are some possibly empty strings of length at most 7, consisting of digits and characters "?". 3. At each iteration, the algorithm looks for a command with the minimum index i, such that si occurs in a as a substring. If this command is not found the algorithm terminates. 4. Let's denote the number of the found command as k. In string a the first occurrence of the string sk is replaced by string wk. If the found command at that had form sk >> wk, then the algorithm continues its execution and proceeds to the next iteration. Otherwise, the algorithm terminates. 5. The value of string a after algorithm termination is considered to be the output of the algorithm. Yaroslav has a set of n positive integers, he needs to come up with his favorite algorithm that will increase each of the given numbers by one. More formally, if we consider each number as a string representing the decimal representation of the number, then being run on each of these strings separately, the algorithm should receive the output string that is a recording of the corresponding number increased by one. Help Yaroslav.
The first line contains integer n (1 ≤ n ≤ 100) — the number of elements in the set. The next n lines contains one positive integer each. All the given numbers are less than 1025.
Print the algorithm which can individually increase each number of the set. In the i-th line print the command number i without spaces. Your algorithm will be launched for each of these numbers. The answer will be considered correct if: - Each line will a correct algorithm command (see the description in the problem statement). - The number of commands should not exceed 50. - The algorithm will increase each of the given numbers by one. - To get a respond, the algorithm will perform no more than 200 iterations for each number.
null
null
[{"input": "2\n10\n79", "output": "10<>11\n79<>80"}]
2,500
["constructive algorithms"]
23
[{"input": "2\r\n10\r\n79\r\n", "output": "10<>11\r\n79<>80\r\n"}, {"input": "5\r\n9\r\n99\r\n999\r\n9999\r\n99999\r\n", "output": "0??<>1\r\n1??<>2\r\n2??<>3\r\n3??<>4\r\n4??<>5\r\n5??<>6\r\n6??<>7\r\n7??<>8\r\n8??<>9\r\n9??>>??0\r\n??<>1\r\n?0>>0?\r\n?1>>1?\r\n?2>>2?\r\n?3>>3?\r\n?4>>4?\r\n?5>>5?\r\n?6>>6?\r\n?7>>7?\r\n?8>>8?\r\n?9>>9?\r\n?>>??\r\n>>?\r\n"}, {"input": "5\r\n99999\r\n9999\r\n999\r\n99\r\n9\r\n", "output": "0??<>1\r\n1??<>2\r\n2??<>3\r\n3??<>4\r\n4??<>5\r\n5??<>6\r\n6??<>7\r\n7??<>8\r\n8??<>9\r\n9??>>??0\r\n??<>1\r\n?0>>0?\r\n?1>>1?\r\n?2>>2?\r\n?3>>3?\r\n?4>>4?\r\n?5>>5?\r\n?6>>6?\r\n?7>>7?\r\n?8>>8?\r\n?9>>9?\r\n?>>??\r\n>>?\r\n"}, {"input": "10\r\n392\r\n605\r\n903\r\n154\r\n293\r\n383\r\n422\r\n717\r\n719\r\n896\r\n", "output": "0??<>1\r\n1??<>2\r\n2??<>3\r\n3??<>4\r\n4??<>5\r\n5??<>6\r\n6??<>7\r\n7??<>8\r\n8??<>9\r\n9??>>??0\r\n??<>1\r\n?0>>0?\r\n?1>>1?\r\n?2>>2?\r\n?3>>3?\r\n?4>>4?\r\n?5>>5?\r\n?6>>6?\r\n?7>>7?\r\n?8>>8?\r\n?9>>9?\r\n?>>??\r\n>>?\r\n"}, {"input": "10\r\n448\r\n727\r\n772\r\n539\r\n870\r\n913\r\n668\r\n300\r\n36\r\n895\r\n", "output": "0??<>1\r\n1??<>2\r\n2??<>3\r\n3??<>4\r\n4??<>5\r\n5??<>6\r\n6??<>7\r\n7??<>8\r\n8??<>9\r\n9??>>??0\r\n??<>1\r\n?0>>0?\r\n?1>>1?\r\n?2>>2?\r\n?3>>3?\r\n?4>>4?\r\n?5>>5?\r\n?6>>6?\r\n?7>>7?\r\n?8>>8?\r\n?9>>9?\r\n?>>??\r\n>>?\r\n"}, {"input": "10\r\n704\r\n812\r\n323\r\n334\r\n674\r\n665\r\n142\r\n712\r\n254\r\n869\r\n", "output": "0??<>1\r\n1??<>2\r\n2??<>3\r\n3??<>4\r\n4??<>5\r\n5??<>6\r\n6??<>7\r\n7??<>8\r\n8??<>9\r\n9??>>??0\r\n??<>1\r\n?0>>0?\r\n?1>>1?\r\n?2>>2?\r\n?3>>3?\r\n?4>>4?\r\n?5>>5?\r\n?6>>6?\r\n?7>>7?\r\n?8>>8?\r\n?9>>9?\r\n?>>??\r\n>>?\r\n"}, {"input": "10\r\n548\r\n645\r\n663\r\n758\r\n38\r\n860\r\n724\r\n742\r\n530\r\n779\r\n", "output": "0??<>1\r\n1??<>2\r\n2??<>3\r\n3??<>4\r\n4??<>5\r\n5??<>6\r\n6??<>7\r\n7??<>8\r\n8??<>9\r\n9??>>??0\r\n??<>1\r\n?0>>0?\r\n?1>>1?\r\n?2>>2?\r\n?3>>3?\r\n?4>>4?\r\n?5>>5?\r\n?6>>6?\r\n?7>>7?\r\n?8>>8?\r\n?9>>9?\r\n?>>??\r\n>>?\r\n"}, {"input": "10\r\n317\r\n36\r\n191\r\n843\r\n289\r\n107\r\n41\r\n943\r\n265\r\n649\r\n", "output": "0??<>1\r\n1??<>2\r\n2??<>3\r\n3??<>4\r\n4??<>5\r\n5??<>6\r\n6??<>7\r\n7??<>8\r\n8??<>9\r\n9??>>??0\r\n??<>1\r\n?0>>0?\r\n?1>>1?\r\n?2>>2?\r\n?3>>3?\r\n?4>>4?\r\n?5>>5?\r\n?6>>6?\r\n?7>>7?\r\n?8>>8?\r\n?9>>9?\r\n?>>??\r\n>>?\r\n"}, {"input": "10\r\n447\r\n806\r\n891\r\n730\r\n371\r\n351\r\n7\r\n102\r\n394\r\n549\r\n", "output": "0??<>1\r\n1??<>2\r\n2??<>3\r\n3??<>4\r\n4??<>5\r\n5??<>6\r\n6??<>7\r\n7??<>8\r\n8??<>9\r\n9??>>??0\r\n??<>1\r\n?0>>0?\r\n?1>>1?\r\n?2>>2?\r\n?3>>3?\r\n?4>>4?\r\n?5>>5?\r\n?6>>6?\r\n?7>>7?\r\n?8>>8?\r\n?9>>9?\r\n?>>??\r\n>>?\r\n"}, {"input": "10\r\n630\r\n624\r\n85\r\n955\r\n757\r\n841\r\n967\r\n377\r\n932\r\n309\r\n", "output": "0??<>1\r\n1??<>2\r\n2??<>3\r\n3??<>4\r\n4??<>5\r\n5??<>6\r\n6??<>7\r\n7??<>8\r\n8??<>9\r\n9??>>??0\r\n??<>1\r\n?0>>0?\r\n?1>>1?\r\n?2>>2?\r\n?3>>3?\r\n?4>>4?\r\n?5>>5?\r\n?6>>6?\r\n?7>>7?\r\n?8>>8?\r\n?9>>9?\r\n?>>??\r\n>>?\r\n"}, {"input": "10\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n", "output": "0??<>1\r\n1??<>2\r\n2??<>3\r\n3??<>4\r\n4??<>5\r\n5??<>6\r\n6??<>7\r\n7??<>8\r\n8??<>9\r\n9??>>??0\r\n??<>1\r\n?0>>0?\r\n?1>>1?\r\n?2>>2?\r\n?3>>3?\r\n?4>>4?\r\n?5>>5?\r\n?6>>6?\r\n?7>>7?\r\n?8>>8?\r\n?9>>9?\r\n?>>??\r\n>>?\r\n"}, {"input": "10\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n", "output": "0??<>1\r\n1??<>2\r\n2??<>3\r\n3??<>4\r\n4??<>5\r\n5??<>6\r\n6??<>7\r\n7??<>8\r\n8??<>9\r\n9??>>??0\r\n??<>1\r\n?0>>0?\r\n?1>>1?\r\n?2>>2?\r\n?3>>3?\r\n?4>>4?\r\n?5>>5?\r\n?6>>6?\r\n?7>>7?\r\n?8>>8?\r\n?9>>9?\r\n?>>??\r\n>>?\r\n"}, {"input": "1\r\n9999999999999999999999999\r\n", "output": "0??<>1\r\n1??<>2\r\n2??<>3\r\n3??<>4\r\n4??<>5\r\n5??<>6\r\n6??<>7\r\n7??<>8\r\n8??<>9\r\n9??>>??0\r\n??<>1\r\n?0>>0?\r\n?1>>1?\r\n?2>>2?\r\n?3>>3?\r\n?4>>4?\r\n?5>>5?\r\n?6>>6?\r\n?7>>7?\r\n?8>>8?\r\n?9>>9?\r\n?>>??\r\n>>?\r\n"}]
false
stdio
import sys def main(input_path, output_path, submission_output_path): with open(input_path) as f: n = int(f.readline()) numbers = [int(line.strip()) for line in f] commands = [] with open(submission_output_path) as f: for line in f: line = line.strip() if '>>' in line: parts = line.split('>>', 1) op = '>>' elif '<>' in line: parts = line.split('<>', 1) op = '<>' else: print(0) return s, w = parts[0], parts[1] if len(s) > 7 or len(w) > 7: print(0) return valid_chars = set('0123456789?') for c in s + w: if c not in valid_chars: print(0) return commands.append((s, op, w)) if len(commands) > 50 or not commands: print(0) return for num in numbers: current = str(num) expected = str(num + 1) iterations = 0 while True: found = False applied_op = None for cmd in commands: s, op, w = cmd if s in current: idx = current.find(s) current = current[:idx] + w + current[idx+len(s):] iterations += 1 found = True applied_op = op break if not found: break if iterations > 200: break if applied_op == '<>': break if current != expected or iterations > 200: print(0) return print(1) if __name__ == "__main__": main(sys.argv[1], sys.argv[2], sys.argv[3])
true
732/D
732
D
PyPy 3-64
TESTS
48
93
18,534,400
217602631
n,m=map(int,input().split()) days=list(map(int,input().split())) sub=list(map(int,input().split())) def binarysearch(): l=sum(sub)+m r=n ans=-1 while l<=r: mid=l+(r-l)//2 count=[0]*(n) # print(mid) for i in range(mid): count[days[i]-1]+=1 zeroz=0 t=True # print(count) for i in range(mid): f=days[i] if f==0: zeroz+=1 else: if count[f-1]==1: if zeroz<sub[f-1]: t=False break else: zeroz-=sub[f-1] else: zeroz+=1 count[f-1]-=1 if t: ans=mid r=mid-1 else: l=mid+1 return ans print(binarysearch())
53
139
12,492,800
192753286
import sys, os, io input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline def binary_search(c1, c2): c = (c1 + c2 + 1) // 2 while abs(c1 - c2) > 1: c = (c1 + c2 + 1) // 2 if ok(c): c2 = c else: c1 = c c = max(c - 1, 0) while not ok(c): c += 1 return c def ok(c): if c > n: return True x, y = [], [0] * (m + 1) for i in range(c, 0, -1): di = d[i] if not di: continue if not y[di]: x.append(di) y[di] = i if len(x) < m: return False s = 0 for i in x[::-1]: s += a[i] + 1 if s > y[i]: return False return True n, m = map(int, input().split()) d = [0] + list(map(int, input().split())) a = [0] + list(map(int, input().split())) if not ok(n): ans = -1 print(ans) exit() ans = binary_search(0, n + 1) print(ans)
Codeforces Round 377 (Div. 2)
CF
2,016
1
256
Exams
Vasiliy has an exam period which will continue for n days. He has to pass exams on m subjects. Subjects are numbered from 1 to m. About every day we know exam for which one of m subjects can be passed on that day. Perhaps, some day you can't pass any exam. It is not allowed to pass more than one exam on any day. On each day Vasiliy can either pass the exam of that day (it takes the whole day) or prepare all day for some exam or have a rest. About each subject Vasiliy know a number ai — the number of days he should prepare to pass the exam number i. Vasiliy can switch subjects while preparing for exams, it is not necessary to prepare continuously during ai days for the exam number i. He can mix the order of preparation for exams in any way. Your task is to determine the minimum number of days in which Vasiliy can pass all exams, or determine that it is impossible. Each exam should be passed exactly one time.
The first line contains two integers n and m (1 ≤ n, m ≤ 105) — the number of days in the exam period and the number of subjects. The second line contains n integers d1, d2, ..., dn (0 ≤ di ≤ m), where di is the number of subject, the exam of which can be passed on the day number i. If di equals 0, it is not allowed to pass any exams on the day number i. The third line contains m positive integers a1, a2, ..., am (1 ≤ ai ≤ 105), where ai is the number of days that are needed to prepare before passing the exam on the subject i.
Print one integer — the minimum number of days in which Vasiliy can pass all exams. If it is impossible, print -1.
null
In the first example Vasiliy can behave as follows. On the first and the second day he can prepare for the exam number 1 and pass it on the fifth day, prepare for the exam number 2 on the third day and pass it on the fourth day. In the second example Vasiliy should prepare for the exam number 3 during the first four days and pass it on the fifth day. Then on the sixth day he should prepare for the exam number 2 and then pass it on the seventh day. After that he needs to prepare for the exam number 1 on the eighth day and pass it on the ninth day. In the third example Vasiliy can't pass the only exam because he hasn't anough time to prepare for it.
[{"input": "7 2\n0 1 0 2 1 0 2\n2 1", "output": "5"}, {"input": "10 3\n0 0 1 2 3 0 2 0 1 2\n1 1 4", "output": "9"}, {"input": "5 1\n1 1 1 1 1\n5", "output": "-1"}]
1,700
["binary search", "greedy", "sortings"]
53
[{"input": "7 2\r\n0 1 0 2 1 0 2\r\n2 1\r\n", "output": "5\r\n"}, {"input": "10 3\r\n0 0 1 2 3 0 2 0 1 2\r\n1 1 4\r\n", "output": "9\r\n"}, {"input": "5 1\r\n1 1 1 1 1\r\n5\r\n", "output": "-1\r\n"}, {"input": "100 10\r\n1 1 6 6 6 2 5 7 6 5 3 7 10 10 8 9 7 6 9 2 6 7 8 6 7 5 2 5 10 1 10 1 8 10 2 9 7 1 6 8 3 10 9 4 4 8 8 6 6 1 5 5 6 5 6 6 6 9 4 7 5 4 6 6 1 1 2 1 8 10 6 2 1 7 2 1 8 10 9 2 7 3 1 5 10 2 8 10 10 10 8 9 5 4 6 10 8 9 6 6\r\n2 4 10 11 5 2 6 7 2 15\r\n", "output": "74\r\n"}, {"input": "1 1\r\n1\r\n1\r\n", "output": "-1\r\n"}, {"input": "3 2\r\n0 0 0\r\n2 1\r\n", "output": "-1\r\n"}, {"input": "4 2\r\n0 1 0 2\r\n1 1\r\n", "output": "4\r\n"}, {"input": "10 1\r\n0 1 0 0 0 0 0 0 0 1\r\n1\r\n", "output": "2\r\n"}, {"input": "5 1\r\n0 0 0 0 1\r\n1\r\n", "output": "5\r\n"}, {"input": "7 2\r\n0 0 0 0 0 1 2\r\n1 1\r\n", "output": "7\r\n"}, {"input": "10 3\r\n0 0 1 2 2 0 2 0 1 3\r\n1 1 4\r\n", "output": "10\r\n"}, {"input": "6 2\r\n1 1 1 1 1 2\r\n1 1\r\n", "output": "6\r\n"}, {"input": "6 2\r\n1 0 0 0 0 2\r\n1 1\r\n", "output": "-1\r\n"}]
false
stdio
null
true
3/B
3
B
Python 3
TESTS
12
842
14,233,600
91114214
import sys n, v = sys.stdin.readline().strip().split() n = int(n) v = int(v) lines = [] for i in range(0, n): lines.append(sys.stdin.readline().strip()) vehicles = [] for index, vehicle in enumerate(lines): typ, capacity = vehicle.split() typ = int(typ) capacity = int(capacity) eff = capacity / typ vehicles.append([eff, typ, capacity, index]) ori_list = vehicles.copy() vehicles.sort() lorry_cap = 0 count = 0 caps = 0 indices = [] rev_vehicles = vehicles[::-1] while lorry_cap < v and count < len(rev_vehicles): caps += rev_vehicles[count][2] lorry_cap += rev_vehicles[count][1] indices.append(rev_vehicles[count][3]) count += 1 if lorry_cap > v: caps -= rev_vehicles[count-1][2] lorry_cap -= rev_vehicles[count-1][1] indices.pop(-1) for j in range(count, len(rev_vehicles)): if rev_vehicles[count][1] == 1: caps += rev_vehicles[count][2] lorry_cap += rev_vehicles[count][1] indices.append(rev_vehicles[count][3]) break if lorry_cap < v: for l in range(count-1, len(rev_vehicles)): for a in indices[::-1]: if ori_list[a][1] == 1 and ori_list[a][2] < rev_vehicles[l][2]: caps -= ori_list[a][2] caps += rev_vehicles[l][2] indices.remove(a) indices.append(rev_vehicles[l][3]) break else: continue break print(caps) num_veh = '' for m in indices: num_veh += str(m+1) num_veh += ' ' print(num_veh[:-1])
32
778
12,083,200
78422702
import sys n, v = map(int, input().split()) kayak = [] catam = [] for i in range(0,n): nums = list(map(int, sys.stdin.readline().split())) if nums[0] == 1: kayak.append((nums[1], i + 1)) else: catam.append((nums[1], i + 1)) # sorts by [0] elementwise kayak.sort(key=lambda pair: pair[0]) catam.sort(key=lambda pair: pair[0]) # Greedily take kayaks and catamarans truck = [] while v > 0: if v == 1: if kayak: truck.append(kayak.pop()) v -= 1 else: if not kayak and not catam: v = 0 elif not kayak: truck.append(catam.pop()) v -= 2 elif not catam: truck.append(kayak.pop()) v -= 1 else: if len(kayak) == 1: if kayak[0][0] > catam[-1][0]: truck.append(kayak.pop()) v -= 1 else: truck.append(catam.pop()) v -= 2 else: if kayak[-1][0]+kayak[-2][0] > catam[-1][0]: truck.append(kayak.pop()) v -= 1 else: truck.append(catam.pop()) v -= 2 ans = 0 for i in truck: ans += i[0] print(ans) for i in truck: sys.stdout.write(str(i[1]) + " ")
Codeforces Beta Round 3
ICPC
2,010
2
64
Lorry
A group of tourists is going to kayak and catamaran tour. A rented lorry has arrived to the boat depot to take kayaks and catamarans to the point of departure. It's known that all kayaks are of the same size (and each of them occupies the space of 1 cubic metre), and all catamarans are of the same size, but two times bigger than kayaks (and occupy the space of 2 cubic metres). Each waterborne vehicle has a particular carrying capacity, and it should be noted that waterborne vehicles that look the same can have different carrying capacities. Knowing the truck body volume and the list of waterborne vehicles in the boat depot (for each one its type and carrying capacity are known), find out such set of vehicles that can be taken in the lorry, and that has the maximum total carrying capacity. The truck body volume of the lorry can be used effectively, that is to say you can always put into the lorry a waterborne vehicle that occupies the space not exceeding the free space left in the truck body.
The first line contains a pair of integer numbers n and v (1 ≤ n ≤ 105; 1 ≤ v ≤ 109), where n is the number of waterborne vehicles in the boat depot, and v is the truck body volume of the lorry in cubic metres. The following n lines contain the information about the waterborne vehicles, that is a pair of numbers ti, pi (1 ≤ ti ≤ 2; 1 ≤ pi ≤ 104), where ti is the vehicle type (1 – a kayak, 2 – a catamaran), and pi is its carrying capacity. The waterborne vehicles are enumerated in order of their appearance in the input file.
In the first line print the maximum possible carrying capacity of the set. In the second line print a string consisting of the numbers of the vehicles that make the optimal set. If the answer is not unique, print any of them.
null
null
[{"input": "3 2\n1 2\n2 7\n1 3", "output": "7\n2"}]
1,900
["greedy", "sortings"]
32
[{"input": "3 2\r\n1 2\r\n2 7\r\n1 3\r\n", "output": "7\r\n2\r\n"}, {"input": "5 3\r\n1 9\r\n2 9\r\n1 9\r\n2 10\r\n1 6\r\n", "output": "24\r\n3 1 5\r\n"}, {"input": "10 10\r\n1 14\r\n2 15\r\n2 11\r\n2 12\r\n2 9\r\n1 14\r\n2 15\r\n1 9\r\n2 11\r\n2 6\r\n", "output": "81\r\n6 1 7 2 4 9\r\n"}, {"input": "20 19\r\n2 47\r\n1 37\r\n1 48\r\n2 42\r\n2 48\r\n1 38\r\n2 47\r\n1 48\r\n2 47\r\n1 41\r\n2 46\r\n1 28\r\n1 49\r\n1 45\r\n2 34\r\n1 43\r\n2 29\r\n1 46\r\n2 45\r\n2 18\r\n", "output": "630\r\n13 8 3 18 14 16 10 6 2 5 9 7 1 11\r\n"}, {"input": "50 27\r\n2 93\r\n1 98\r\n2 62\r\n1 56\r\n1 86\r\n1 42\r\n2 67\r\n2 97\r\n2 59\r\n1 73\r\n1 83\r\n2 96\r\n1 20\r\n1 66\r\n1 84\r\n1 83\r\n1 91\r\n2 97\r\n1 81\r\n2 88\r\n2 63\r\n1 99\r\n2 57\r\n1 39\r\n1 74\r\n2 88\r\n1 30\r\n2 68\r\n1 100\r\n2 57\r\n1 87\r\n1 93\r\n1 83\r\n1 100\r\n1 91\r\n1 14\r\n1 38\r\n2 98\r\n2 85\r\n2 61\r\n1 44\r\n2 93\r\n2 66\r\n2 55\r\n2 74\r\n1 67\r\n2 67\r\n1 85\r\n2 59\r\n1 83\r\n", "output": "2055\r\n34 29 22 2 32 35 17 31 5 48 15 50 33 16 11 19 25 10 46 14 4 38 18 8\r\n"}, {"input": "1 1\r\n1 600\r\n", "output": "600\r\n1\r\n"}, {"input": "10 14\r\n2 230\r\n2 516\r\n2 527\r\n2 172\r\n2 854\r\n2 61\r\n1 52\r\n2 154\r\n2 832\r\n2 774\r\n", "output": "3905\r\n5 9 10 3 2 1 4\r\n"}, {"input": "8 8\r\n1 1\r\n1 1\r\n1 1\r\n1 1\r\n2 100\r\n2 100\r\n2 100\r\n2 100\r\n", "output": "400\r\n8 7 6 5\r\n"}, {"input": "8 4\r\n1 100\r\n1 100\r\n1 100\r\n1 100\r\n2 1\r\n2 1\r\n2 1\r\n2 1\r\n", "output": "400\r\n4 3 2 1\r\n"}]
false
stdio
import sys def read_lines(path): with open(path, 'r') as f: return [line.strip() for line in f.readlines() if line.strip()] def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] # Read input input_lines = read_lines(input_path) if not input_lines: print(0) return n, v = map(int, input_lines[0].split()) vehicles = [] for line in input_lines[1:1+n]: ti, pi = map(int, line.split()) vehicles.append( (ti, pi) ) # Read judge output judge_lines = read_lines(output_path) if not judge_lines: print(0) return try: judge_sum = int(judge_lines[0]) except: print(0) return judge_vehicles = [] if len(judge_lines) >=2: judge_vehicles = list(map(int, ' '.join(judge_lines[1:]).split())) # Read submission output sub_lines = read_lines(submission_path) if len(sub_lines) <1: print(0) return try: sub_sum = int(sub_lines[0]) except: print(0) return sub_vehicles = [] if len(sub_lines) >=2: sub_vehicles = list(map(int, ' '.join(sub_lines[1:]).split())) # Check sum matches if sub_sum != judge_sum: print(0) return # Check vehicle indices are valid and unique seen = set() for idx in sub_vehicles: if idx <1 or idx >n or idx in seen: print(0) return seen.add(idx) # Compute sum_t and sum_p sum_t = 0 sum_p =0 for idx in sub_vehicles: ti, pi = vehicles[idx-1] sum_t += ti sum_p += pi if sum_t >v: print(0) return if sum_p != sub_sum: print(0) return # All checks passed print(1) if __name__ == '__main__': main()
true
301/C
301
C
PyPy 3
TESTS
0
154
20,172,800
125260427
res = ''' 0??<>1 1??<>2 2??<>3 3??<>4 4??<>5 5??<>6 6??<>7 7??<>8 8??<>9 9??>>??0 ?0>>0? ?1>>1? ?2>>2? ?3>>3? ?4>>4? ?5>>5? ?6>>6? ?7>>7? ?8>>8? ?9>>9? ?>>?? 0>>0? 1>>1? 2>>2? 3>>3? 4>>4? 5>>5? 6>>6? 7>>7? 8>>8? 9>>9? ''' print(res)
23
248
0
56067152
print("0??<>1"); print("1??<>2"); print("2??<>3"); print("3??<>4"); print("4??<>5"); print("5??<>6"); print("6??<>7"); print("7??<>8"); print("8??<>9"); print("9??>>??0"); print("??<>1"); print("?9>>9?"); print("?8>>8?"); print("?7>>7?"); print("?6>>6?"); print("?5>>5?"); print("?4>>4?"); print("?3>>3?"); print("?2>>2?"); print("?1>>1?"); print("?0>>0?"); print("9>>9?"); print("8>>8?"); print("7>>7?"); print("6>>6?"); print("5>>5?"); print("4>>4?"); print("3>>3?"); print("2>>2?"); print("1>>1?"); print("0>>0?");
Codeforces Round 182 (Div. 1)
CF
2,013
2
256
Yaroslav and Algorithm
Yaroslav likes algorithms. We'll describe one of his favorite algorithms. 1. The algorithm receives a string as the input. We denote this input string as a. 2. The algorithm consists of some number of command. Сommand number i looks either as si >> wi, or as si <> wi, where si and wi are some possibly empty strings of length at most 7, consisting of digits and characters "?". 3. At each iteration, the algorithm looks for a command with the minimum index i, such that si occurs in a as a substring. If this command is not found the algorithm terminates. 4. Let's denote the number of the found command as k. In string a the first occurrence of the string sk is replaced by string wk. If the found command at that had form sk >> wk, then the algorithm continues its execution and proceeds to the next iteration. Otherwise, the algorithm terminates. 5. The value of string a after algorithm termination is considered to be the output of the algorithm. Yaroslav has a set of n positive integers, he needs to come up with his favorite algorithm that will increase each of the given numbers by one. More formally, if we consider each number as a string representing the decimal representation of the number, then being run on each of these strings separately, the algorithm should receive the output string that is a recording of the corresponding number increased by one. Help Yaroslav.
The first line contains integer n (1 ≤ n ≤ 100) — the number of elements in the set. The next n lines contains one positive integer each. All the given numbers are less than 1025.
Print the algorithm which can individually increase each number of the set. In the i-th line print the command number i without spaces. Your algorithm will be launched for each of these numbers. The answer will be considered correct if: - Each line will a correct algorithm command (see the description in the problem statement). - The number of commands should not exceed 50. - The algorithm will increase each of the given numbers by one. - To get a respond, the algorithm will perform no more than 200 iterations for each number.
null
null
[{"input": "2\n10\n79", "output": "10<>11\n79<>80"}]
2,500
["constructive algorithms"]
23
[{"input": "2\r\n10\r\n79\r\n", "output": "10<>11\r\n79<>80\r\n"}, {"input": "5\r\n9\r\n99\r\n999\r\n9999\r\n99999\r\n", "output": "0??<>1\r\n1??<>2\r\n2??<>3\r\n3??<>4\r\n4??<>5\r\n5??<>6\r\n6??<>7\r\n7??<>8\r\n8??<>9\r\n9??>>??0\r\n??<>1\r\n?0>>0?\r\n?1>>1?\r\n?2>>2?\r\n?3>>3?\r\n?4>>4?\r\n?5>>5?\r\n?6>>6?\r\n?7>>7?\r\n?8>>8?\r\n?9>>9?\r\n?>>??\r\n>>?\r\n"}, {"input": "5\r\n99999\r\n9999\r\n999\r\n99\r\n9\r\n", "output": "0??<>1\r\n1??<>2\r\n2??<>3\r\n3??<>4\r\n4??<>5\r\n5??<>6\r\n6??<>7\r\n7??<>8\r\n8??<>9\r\n9??>>??0\r\n??<>1\r\n?0>>0?\r\n?1>>1?\r\n?2>>2?\r\n?3>>3?\r\n?4>>4?\r\n?5>>5?\r\n?6>>6?\r\n?7>>7?\r\n?8>>8?\r\n?9>>9?\r\n?>>??\r\n>>?\r\n"}, {"input": "10\r\n392\r\n605\r\n903\r\n154\r\n293\r\n383\r\n422\r\n717\r\n719\r\n896\r\n", "output": "0??<>1\r\n1??<>2\r\n2??<>3\r\n3??<>4\r\n4??<>5\r\n5??<>6\r\n6??<>7\r\n7??<>8\r\n8??<>9\r\n9??>>??0\r\n??<>1\r\n?0>>0?\r\n?1>>1?\r\n?2>>2?\r\n?3>>3?\r\n?4>>4?\r\n?5>>5?\r\n?6>>6?\r\n?7>>7?\r\n?8>>8?\r\n?9>>9?\r\n?>>??\r\n>>?\r\n"}, {"input": "10\r\n448\r\n727\r\n772\r\n539\r\n870\r\n913\r\n668\r\n300\r\n36\r\n895\r\n", "output": "0??<>1\r\n1??<>2\r\n2??<>3\r\n3??<>4\r\n4??<>5\r\n5??<>6\r\n6??<>7\r\n7??<>8\r\n8??<>9\r\n9??>>??0\r\n??<>1\r\n?0>>0?\r\n?1>>1?\r\n?2>>2?\r\n?3>>3?\r\n?4>>4?\r\n?5>>5?\r\n?6>>6?\r\n?7>>7?\r\n?8>>8?\r\n?9>>9?\r\n?>>??\r\n>>?\r\n"}, {"input": "10\r\n704\r\n812\r\n323\r\n334\r\n674\r\n665\r\n142\r\n712\r\n254\r\n869\r\n", "output": "0??<>1\r\n1??<>2\r\n2??<>3\r\n3??<>4\r\n4??<>5\r\n5??<>6\r\n6??<>7\r\n7??<>8\r\n8??<>9\r\n9??>>??0\r\n??<>1\r\n?0>>0?\r\n?1>>1?\r\n?2>>2?\r\n?3>>3?\r\n?4>>4?\r\n?5>>5?\r\n?6>>6?\r\n?7>>7?\r\n?8>>8?\r\n?9>>9?\r\n?>>??\r\n>>?\r\n"}, {"input": "10\r\n548\r\n645\r\n663\r\n758\r\n38\r\n860\r\n724\r\n742\r\n530\r\n779\r\n", "output": "0??<>1\r\n1??<>2\r\n2??<>3\r\n3??<>4\r\n4??<>5\r\n5??<>6\r\n6??<>7\r\n7??<>8\r\n8??<>9\r\n9??>>??0\r\n??<>1\r\n?0>>0?\r\n?1>>1?\r\n?2>>2?\r\n?3>>3?\r\n?4>>4?\r\n?5>>5?\r\n?6>>6?\r\n?7>>7?\r\n?8>>8?\r\n?9>>9?\r\n?>>??\r\n>>?\r\n"}, {"input": "10\r\n317\r\n36\r\n191\r\n843\r\n289\r\n107\r\n41\r\n943\r\n265\r\n649\r\n", "output": "0??<>1\r\n1??<>2\r\n2??<>3\r\n3??<>4\r\n4??<>5\r\n5??<>6\r\n6??<>7\r\n7??<>8\r\n8??<>9\r\n9??>>??0\r\n??<>1\r\n?0>>0?\r\n?1>>1?\r\n?2>>2?\r\n?3>>3?\r\n?4>>4?\r\n?5>>5?\r\n?6>>6?\r\n?7>>7?\r\n?8>>8?\r\n?9>>9?\r\n?>>??\r\n>>?\r\n"}, {"input": "10\r\n447\r\n806\r\n891\r\n730\r\n371\r\n351\r\n7\r\n102\r\n394\r\n549\r\n", "output": "0??<>1\r\n1??<>2\r\n2??<>3\r\n3??<>4\r\n4??<>5\r\n5??<>6\r\n6??<>7\r\n7??<>8\r\n8??<>9\r\n9??>>??0\r\n??<>1\r\n?0>>0?\r\n?1>>1?\r\n?2>>2?\r\n?3>>3?\r\n?4>>4?\r\n?5>>5?\r\n?6>>6?\r\n?7>>7?\r\n?8>>8?\r\n?9>>9?\r\n?>>??\r\n>>?\r\n"}, {"input": "10\r\n630\r\n624\r\n85\r\n955\r\n757\r\n841\r\n967\r\n377\r\n932\r\n309\r\n", "output": "0??<>1\r\n1??<>2\r\n2??<>3\r\n3??<>4\r\n4??<>5\r\n5??<>6\r\n6??<>7\r\n7??<>8\r\n8??<>9\r\n9??>>??0\r\n??<>1\r\n?0>>0?\r\n?1>>1?\r\n?2>>2?\r\n?3>>3?\r\n?4>>4?\r\n?5>>5?\r\n?6>>6?\r\n?7>>7?\r\n?8>>8?\r\n?9>>9?\r\n?>>??\r\n>>?\r\n"}, {"input": "10\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n", "output": "0??<>1\r\n1??<>2\r\n2??<>3\r\n3??<>4\r\n4??<>5\r\n5??<>6\r\n6??<>7\r\n7??<>8\r\n8??<>9\r\n9??>>??0\r\n??<>1\r\n?0>>0?\r\n?1>>1?\r\n?2>>2?\r\n?3>>3?\r\n?4>>4?\r\n?5>>5?\r\n?6>>6?\r\n?7>>7?\r\n?8>>8?\r\n?9>>9?\r\n?>>??\r\n>>?\r\n"}, {"input": "10\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n", "output": "0??<>1\r\n1??<>2\r\n2??<>3\r\n3??<>4\r\n4??<>5\r\n5??<>6\r\n6??<>7\r\n7??<>8\r\n8??<>9\r\n9??>>??0\r\n??<>1\r\n?0>>0?\r\n?1>>1?\r\n?2>>2?\r\n?3>>3?\r\n?4>>4?\r\n?5>>5?\r\n?6>>6?\r\n?7>>7?\r\n?8>>8?\r\n?9>>9?\r\n?>>??\r\n>>?\r\n"}, {"input": "1\r\n9999999999999999999999999\r\n", "output": "0??<>1\r\n1??<>2\r\n2??<>3\r\n3??<>4\r\n4??<>5\r\n5??<>6\r\n6??<>7\r\n7??<>8\r\n8??<>9\r\n9??>>??0\r\n??<>1\r\n?0>>0?\r\n?1>>1?\r\n?2>>2?\r\n?3>>3?\r\n?4>>4?\r\n?5>>5?\r\n?6>>6?\r\n?7>>7?\r\n?8>>8?\r\n?9>>9?\r\n?>>??\r\n>>?\r\n"}]
false
stdio
import sys def main(input_path, output_path, submission_output_path): with open(input_path) as f: n = int(f.readline()) numbers = [int(line.strip()) for line in f] commands = [] with open(submission_output_path) as f: for line in f: line = line.strip() if '>>' in line: parts = line.split('>>', 1) op = '>>' elif '<>' in line: parts = line.split('<>', 1) op = '<>' else: print(0) return s, w = parts[0], parts[1] if len(s) > 7 or len(w) > 7: print(0) return valid_chars = set('0123456789?') for c in s + w: if c not in valid_chars: print(0) return commands.append((s, op, w)) if len(commands) > 50 or not commands: print(0) return for num in numbers: current = str(num) expected = str(num + 1) iterations = 0 while True: found = False applied_op = None for cmd in commands: s, op, w = cmd if s in current: idx = current.find(s) current = current[:idx] + w + current[idx+len(s):] iterations += 1 found = True applied_op = op break if not found: break if iterations > 200: break if applied_op == '<>': break if current != expected or iterations > 200: print(0) return print(1) if __name__ == "__main__": main(sys.argv[1], sys.argv[2], sys.argv[3])
true
128/B
128
B
Python 3
TESTS
9
842
14,745,600
17777690
from heapq import * l=input() k=int(input()) n=len(l) if k>n*(n+1)/2: print("No such line.") quit() ss=[(l[i],i) for i in range(n)] while k: k-=1 t=heappop(ss) if k==0: print(t[0]) else: if t[1]<n-1: heappush(ss,(t[0]+l[t[1]+1],t[1]+1))
63
404
13,824,000
219201798
def get_input(): s = input() l = len(s) k = int(input()) return s, l, k def find_kth_sum(s, l, k): if l * (l + 1) / 2 < k: return 'No such line.' a = [i for i in range(l)] p = 0 ans = '' while True: t = [(s[i + p], i) for i in a] t = sorted(t) i = j = 0 prod = 0 while i < len(t): pre = prod while j < len(t) and t[i][0] == t[j][0]: prod += l - t[j][1] - p j += 1 if prod >= k: break i = j ans += t[i][0] if pre + j - i >= k: break k -= pre + j - i p += 1 a = [t[q][1] for q in range(i, j) if t[q][1] + p < l] return ans def main(): s, l, k = get_input() result = find_kth_sum(s, l, k) print(result) if __name__ == "__main__": main()
Codeforces Beta Round 94 (Div. 1 Only)
CF
2,011
2
256
String
One day in the IT lesson Anna and Maria learned about the lexicographic order. String x is lexicographically less than string y, if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that xi < yi, and for any j (1 ≤ j < i) xj = yj. Here |a| denotes the length of the string a. The lexicographic comparison of strings is implemented by operator < in modern programming languages​​. The teacher gave Anna and Maria homework. She gave them a string of length n. They should write out all substrings of the given string, including the whole initial string, and the equal substrings (for example, one should write out the following substrings from the string "aab": "a", "a", "aa", "ab", "aab", "b"). The resulting strings should be sorted in the lexicographical order. The cunning teacher doesn't want to check all these strings. That's why she said to find only the k-th string from the list. Help Anna and Maria do the homework.
The first line contains a non-empty string that only consists of small Latin letters ("a"-"z"), whose length does not exceed 105. The second line contains the only integer k (1 ≤ k ≤ 105).
Print the string Anna and Maria need — the k-th (in the lexicographical order) substring of the given string. If the total number of substrings is less than k, print a string saying "No such line." (without the quotes).
null
In the second sample before string "bc" follow strings "a", "ab", "abc", "b".
[{"input": "aa\n2", "output": "a"}, {"input": "abc\n5", "output": "bc"}, {"input": "abab\n7", "output": "b"}]
2,100
["brute force", "constructive algorithms", "hashing", "implementation", "string suffix structures", "strings"]
63
[{"input": "aa\r\n2\r\n", "output": "a\r\n"}, {"input": "abc\r\n5\r\n", "output": "bc\r\n"}, {"input": "abab\r\n7\r\n", "output": "b\r\n"}, {"input": "codeforces\r\n1\r\n", "output": "c\r\n"}, {"input": "cccc\r\n8\r\n", "output": "ccc\r\n"}, {"input": "abcdefghijklmnopqrstuvwxyz\r\n27\r\n", "output": "b\r\n"}, {"input": "cba\r\n6\r\n", "output": "cba\r\n"}, {"input": "z\r\n100000\r\n", "output": "No such line.\r\n"}, {"input": "aaaaaaaaaa\r\n90\r\n", "output": "No such line.\r\n"}]
false
stdio
null
true
58/C
58
C
Python 3
TESTS
3
248
0
82570103
n=int(input()) l=list(map(int,input().split())) i=0 j=n-1 x=0 a=l[0] c=0 while i<=j: if x+a!=l[j]: c+=1 j-=1 i+=1 x+=1 print(c)
40
342
14,438,400
86234698
n = int(input()) a = [0] * 100005 l = list(map(int, input().split())) for i in range(n): a[l[i] - min(i, n - i - 1)] += 1 print(n - max(a[1::]))
Codeforces Beta Round 54 (Div. 2)
CF
2,011
2
256
Trees
On Bertown's main street n trees are growing, the tree number i has the height of ai meters (1 ≤ i ≤ n). By the arrival of the President of Berland these trees were decided to be changed so that their heights formed a beautiful sequence. This means that the heights of trees on ends (the 1st one and the n-th one) should be equal to each other, the heights of the 2-nd and the (n - 1)-th tree must also be equal to each other, at that the height of the 2-nd tree should be larger than the height of the first tree by 1, and so on. In other words, the heights of the trees, standing at equal distance from the edge (of one end of the sequence) must be equal to each other, and with the increasing of the distance from the edge by 1 the tree height must also increase by 1. For example, the sequences "2 3 4 5 5 4 3 2" and "1 2 3 2 1" are beautiful, and '1 3 3 1" and "1 2 3 1" are not. Changing the height of a tree is a very expensive operation, using advanced technologies invented by Berland scientists. In one operation you can choose any tree and change its height to any number, either increase or decrease. Note that even after the change the height should remain a positive integer, i. e, it can't be less than or equal to zero. Identify the smallest number of changes of the trees' height needed for the sequence of their heights to become beautiful.
The first line contains integer n (1 ≤ n ≤ 105) which is the number of trees. The second line contains integers ai (1 ≤ ai ≤ 105) which are the heights of the trees.
Print a single number which is the minimal number of trees whose heights will have to be changed for the sequence to become beautiful.
null
null
[{"input": "3\n2 2 2", "output": "1"}, {"input": "4\n1 2 2 1", "output": "0"}]
1,800
["brute force"]
40
[{"input": "3\r\n2 2 2\r\n", "output": "1\r\n"}, {"input": "4\r\n1 2 2 1\r\n", "output": "0\r\n"}, {"input": "3\r\n61452 50974 73849\r\n", "output": "2\r\n"}, {"input": "4\r\n86002 1199 86003 86002\r\n", "output": "1\r\n"}, {"input": "5\r\n92605 92606 41969 98774 92605\r\n", "output": "2\r\n"}, {"input": "10\r\n1 1 2 3 4 4 3 2 1 10\r\n", "output": "9\r\n"}, {"input": "10\r\n73905 73906 73907 85732 73909 73909 73908 73907 73906 73905\r\n", "output": "1\r\n"}, {"input": "10\r\n60718 99414 65042 65043 65044 38495 95782 65042 65041 65040\r\n", "output": "4\r\n"}, {"input": "7\r\n1 2 3 7 6 5 4\r\n", "output": "3\r\n"}, {"input": "5\r\n4 5 6 5 1\r\n", "output": "1\r\n"}, {"input": "1\r\n100000\r\n", "output": "0\r\n"}, {"input": "5\r\n1 1 1 1 1\r\n", "output": "3\r\n"}, {"input": "7\r\n1 2 3 7 6 5 4\r\n", "output": "3\r\n"}, {"input": "1\r\n2727\r\n", "output": "0\r\n"}, {"input": "5\r\n1 2 3 2 4\r\n", "output": "1\r\n"}, {"input": "9\r\n100 12 13 14 15 14 13 12 11\r\n", "output": "1\r\n"}, {"input": "5\r\n2 4 5 4 2\r\n", "output": "2\r\n"}, {"input": "5\r\n3 3 4 3 2\r\n", "output": "1\r\n"}]
false
stdio
null
true
547/D
547
D
Python 3
TESTS
0
30
0
227211410
n = int(input()) points = [] # Read and sort the points based on x-coordinates for _ in range(n): x, y = map(int, input().split()) points.append((x, y)) points.sort() # Initialize variables to keep track of red and blue fish count red_count = 0 blue_count = 0 # Assign colors to points while maintaining the balance colors = [] for x, y in points: if x % 2 == 0: if y % 2 == 0: # Even x, even y if red_count <= blue_count: colors.append('r') red_count += 1 else: colors.append('b') blue_count += 1 else: # Even x, odd y if red_count >= blue_count: colors.append('r') red_count += 1 else: colors.append('b') blue_count += 1 else: if y % 2 == 0: # Odd x, even y if red_count >= blue_count: colors.append('r') red_count += 1 else: colors.append('b') blue_count += 1 else: # Odd x, odd y if red_count <= blue_count: colors.append('r') red_count += 1 else: colors.append('b') blue_count += 1 # Output the assigned colors print("".join(colors))
50
889
112,640,000
224152629
from sys import stdin, stdout from collections import defaultdict time = 0 c = 2*10**5 n = 4*10**5+2 col = 0 finished= [0]*n for_node = [0]*n f_range=range f_len=len def dfs_euler_tour(node: int, graph): stack = [] global colour global col global time stack.append((node, -1)) while stack: s, ind = stack.pop() if ind > -1: if ind < f_len(colour): colour[ind] = col col = col ^ 1 index = for_node[s] while(index < f_len(graph[s])): v,i=graph[s][index] if not visited[i]: stack.append((v, i)) visited[i] = True index += 1 for_node[s] = index break index += 1 finished[s]+1 m = int(stdin.readline()) graph =defaultdict(list) edges=[] debug=[] for i in f_range(m): u, v = stdin.readline().split() u, v = (int(u)-1, int(v)-1 + c+1) edges.append((u, v)) graph[u].append((v, i)) graph[v].append((u, i)) if u == 30630-1 and m == 199809: debug.append(i) colour = [-1]*m odds= [i for i in graph.keys() if f_len(graph[i]) % 2] if odds: for i in f_range(f_len(odds)): u = odds[i] ind=f_len(edges) if u<c: v = n-1 else: v = c edges.append((u, v)) graph[u].append((v, ind)) graph[v].append((u, ind)) if f_len(graph[n-1]) % 2: u=n-1 v=c ind = f_len(edges) edges.append((u, v)) graph[u].append((v, ind)) graph[v].append((u, ind)) visited = [False]*f_len(edges) for i in graph.keys(): if not finished[i]: dfs_euler_tour(i, graph) sol = ''.join(['b' if i == 1 else 'r' for i in colour]) sol += '\n' stdout.write(sol)
Codeforces Round 305 (Div. 1)
CF
2,015
3
256
Mike and Fish
As everyone knows, bears love fish. But Mike is a strange bear; He hates fish! The even more strange thing about him is he has an infinite number of blue and red fish. He has marked n distinct points in the plane. i-th point is point (xi, yi). He wants to put exactly one fish in each of these points such that the difference between the number of red fish and the blue fish on each horizontal or vertical line is at most 1. He can't find a way to perform that! Please help him.
The first line of input contains integer n (1 ≤ n ≤ 2 × 105). The next n lines contain the information about the points, i-th line contains two integers xi and yi (1 ≤ xi, yi ≤ 2 × 105), the i-th point coordinates. It is guaranteed that there is at least one valid answer.
Print the answer as a sequence of n characters 'r' (for red) or 'b' (for blue) where i-th character denotes the color of the fish in the i-th point.
null
null
[{"input": "4\n1 1\n1 2\n2 1\n2 2", "output": "brrb"}, {"input": "3\n1 1\n1 2\n2 1", "output": "brr"}]
2,600
["constructive algorithms", "dfs and similar", "graphs"]
50
[{"input": "4\r\n1 1\r\n1 2\r\n2 1\r\n2 2\r\n", "output": "brrb\r\n"}, {"input": "3\r\n1 1\r\n1 2\r\n2 1\r\n", "output": "brr\r\n"}, {"input": "3\r\n157210 22861\r\n175396 39466\r\n40933 17093\r\n", "output": "rrr\r\n"}, {"input": "5\r\n55599 84144\r\n169207 98421\r\n1909 186625\r\n31525 147710\r\n7781 82078\r\n", "output": "rrrrr\r\n"}, {"input": "15\r\n44249 54630\r\n165741 91307\r\n49455 83026\r\n52521 88269\r\n39286 65158\r\n38282 73821\r\n96608 30032\r\n155832 122920\r\n22021 13546\r\n161556 192797\r\n168062 8224\r\n161221 155335\r\n5670 180269\r\n89163 128733\r\n151226 75254\r\n", "output": "rrrrrrrrrrrrrrr\r\n"}, {"input": "9\r\n95316 68815\r\n95316 23738\r\n60801 169893\r\n84639 68815\r\n109462 87456\r\n22940 37614\r\n172202 151462\r\n84639 23738\r\n109462 151462\r\n", "output": "rbrbrrrrb\r\n"}, {"input": "2\r\n196356 153892\r\n134153 153892\r\n", "output": "rb\r\n"}, {"input": "10\r\n4126 18194\r\n143965 18194\r\n32687 18194\r\n118527 18194\r\n186573 18194\r\n97223 18194\r\n179697 18194\r\n175536 18194\r\n107767 18194\r\n127019 18194\r\n", "output": "bbbbrbrrrr\r\n"}, {"input": "1\r\n1 1\r\n", "output": "r\r\n"}, {"input": "1\r\n1000 3434\r\n", "output": "r\r\n"}, {"input": "1\r\n200000 200000\r\n", "output": "r\r\n"}, {"input": "2\r\n1 2\r\n2 3\r\n", "output": "rr\r\n"}, {"input": "2\r\n1 2\r\n1 3\r\n", "output": "br\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, 'r') as f: n = int(f.readline()) points = [] for _ in range(n): x, y = map(int, f.readline().split()) points.append((x, y)) with open(submission_path, 'r') as f: s = f.read().strip() if len(s) != n: print(0) return for c in s: if c not in ('r', 'b'): print(0) return x_counts = {} y_counts = {} for (x, y), color in zip(points, s): if x not in x_counts: x_counts[x] = {'r': 0, 'b': 0} x_counts[x][color] += 1 if y not in y_counts: y_counts[y] = {'r': 0, 'b': 0} y_counts[y][color] += 1 for x in x_counts: r = x_counts[x]['r'] b = x_counts[x]['b'] if abs(r - b) > 1: print(0) return for y in y_counts: r = y_counts[y]['r'] b = y_counts[y]['b'] if abs(r - b) > 1: print(0) return print(1) if __name__ == "__main__": main()
true
545/D
545
D
Python 3
TESTS
13
77
10,342,400
229507916
n = int(input()) queue =map(int, input().split()) queue2=set() number_ones=0 for i in queue: if i == 1: number_ones+=1 else: queue2.add(i) queue=sorted(queue2) # print(queue) if number_ones > 1: queue=[1]*2+queue # print(queue) n=len(queue) sum_ = 0 i = 0 while True: if i == n: break x = queue[i] if x >= sum_: sum_ += x i += 1 else: queue.pop(i) n -= 1 print(n) # print(queue) # print(number_ones)
61
78
13,107,200
225590587
def solve(n): n.sort() count = 0 curr = 0 for num in n: if num < curr: continue count += 1 curr += num return count def main(): t = int(input()) print(solve([int(x) for x in input().split()])) if __name__ == "__main__": main()
Codeforces Round 303 (Div. 2)
CF
2,015
1
256
Queue
Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue.
The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces.
Print a single number — the maximum number of not disappointed people in the queue.
null
Value 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5.
[{"input": "5\n15 2 1 5 3", "output": "4"}]
1,300
["greedy", "implementation", "sortings"]
61
[{"input": "5\r\n15 2 1 5 3\r\n", "output": "4\r\n"}, {"input": "15\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "2\r\n"}, {"input": "10\r\n13 2 5 55 21 34 1 8 1 3\r\n", "output": "6\r\n"}, {"input": "10\r\n8 256 16 1 2 1 64 4 128 32\r\n", "output": "10\r\n"}, {"input": "10\r\n10000 40000 10000 50000 20000 100000 10000 100 30000 500\r\n", "output": "6\r\n"}, {"input": "1\r\n1000000000\r\n", "output": "1\r\n"}, {"input": "15\r\n9 11 45 86 52 65 35 3 93 7 21 45 15 11 39\r\n", "output": "6\r\n"}, {"input": "20\r\n16839799 17525904 91276752 42650694 60106463 12243176 54892123 25142243 16015971 41250998 11150057 6994983 67700784 16562412 82163675 46178521 33914268 91966607 93976858 84100064\r\n", "output": "5\r\n"}, {"input": "26\r\n1000 4110030 64221 131521030 942374833 1003 2055015 32110 513757 16440130 263042057 32880256 128439 557559573 16051 8220066 469240078 65760513 256878 790176315 4012 2005 1027508 928528684 8030 805074697\r\n", "output": "21\r\n"}]
false
stdio
null
true
546/B
546
B
PyPy 3-64
TESTS
9
61
1,740,800
232331284
c = int(input()) a = str(input()).split() b = [int(x) for x in a] counter = 0 arr = [] dups = [] for x in b: if x in arr: dups.append(x) else: arr.append(x) for x in dups: while x in arr: x = x + 1 counter += 1 else: continue print(counter)
49
62
307,200
11211283
import sys n = int(input()) if n == 1: print(0) sys.exit(0) l = list(map(int, input().split(" "))) l.sort() min = l[0] val = 0 for i in range(1, len(l)): if l[i] <= l[i-1]: val += 1+l[i-1]-l[i] l[i]+= 1+l[i-1]-l[i] print(val)
Codeforces Round 304 (Div. 2)
CF
2,015
3
256
Soldier and Badges
Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge has a coolness factor, which shows how much it's owner reached. Coolness factor can be increased by one for the cost of one coin. For every pair of soldiers one of them should get a badge with strictly higher factor than the second one. Exact values of their factors aren't important, they just need to have distinct factors. Colonel knows, which soldier is supposed to get which badge initially, but there is a problem. Some of badges may have the same factor of coolness. Help him and calculate how much money has to be paid for making all badges have different factors of coolness.
First line of input consists of one integer n (1 ≤ n ≤ 3000). Next line consists of n integers ai (1 ≤ ai ≤ n), which stand for coolness factor of each badge.
Output single integer — minimum amount of coins the colonel has to pay.
null
In first sample test we can increase factor of first badge by 1. In second sample test we can increase factors of the second and the third badge by 1.
[{"input": "4\n1 3 1 4", "output": "1"}, {"input": "5\n1 2 3 2 5", "output": "2"}]
1,200
["brute force", "greedy", "implementation", "sortings"]
49
[{"input": "4\r\n1 3 1 4\r\n", "output": "1"}, {"input": "5\r\n1 2 3 2 5\r\n", "output": "2"}, {"input": "5\r\n1 5 3 2 4\r\n", "output": "0"}, {"input": "10\r\n1 1 2 3 4 5 6 7 8 9\r\n", "output": "9"}, {"input": "11\r\n9 2 10 3 1 5 7 1 4 8 6\r\n", "output": "10"}, {"input": "4\r\n4 3 2 2\r\n", "output": "3"}, {"input": "1\r\n1\r\n", "output": "0"}, {"input": "50\r\n49 37 30 2 18 48 14 48 50 27 1 43 46 5 21 28 44 2 24 17 41 38 25 18 43 28 25 21 28 23 26 27 4 31 50 18 23 11 13 28 44 47 1 26 43 25 22 46 32 45\r\n", "output": "170"}, {"input": "50\r\n37 31 19 46 45 1 9 37 15 19 15 10 17 16 38 13 26 25 36 13 7 21 12 41 46 19 3 50 14 49 49 40 29 41 47 29 3 42 13 21 10 21 9 33 38 30 24 40 5 26\r\n", "output": "135"}, {"input": "50\r\n18 13 50 12 23 29 31 44 28 29 33 31 17 38 27 37 36 34 40 4 27 2 8 27 50 27 21 28 11 13 47 25 15 26 9 15 22 3 22 45 9 12 5 5 46 44 23 34 12 25\r\n", "output": "138"}, {"input": "50\r\n24 44 39 44 11 20 6 43 4 21 43 12 41 3 25 25 24 7 16 36 32 2 2 29 34 30 33 9 18 3 14 28 26 49 29 5 5 36 44 21 36 37 1 25 46 10 10 24 10 39\r\n", "output": "128"}, {"input": "50\r\n7 5 18 2 7 12 8 20 41 4 7 3 7 10 22 1 19 9 20 10 23 3 6 3 30 13 6 18 3 3 18 38 9 7 2 1 2 5 25 10 13 1 8 34 1 26 13 8 13 2\r\n", "output": "699"}, {"input": "50\r\n2 19 24 3 12 4 14 9 10 19 6 1 26 6 11 1 4 34 17 1 3 35 17 2 17 17 5 5 12 1 24 35 2 5 43 23 21 4 18 3 11 5 1 21 3 3 3 1 10 10\r\n", "output": "692"}, {"input": "50\r\n2 2 4 19 5 7 2 35 3 12 1 18 17 16 40 4 15 36 1 11 13 3 14 1 4 10 1 12 43 7 9 9 4 3 28 9 12 12 1 33 3 23 11 24 20 20 2 4 26 4\r\n", "output": "660"}, {"input": "50\r\n5 3 25 6 30 6 39 15 3 19 1 38 1 3 17 3 8 13 4 10 14 3 2 3 20 1 21 21 27 31 6 6 14 28 3 13 49 8 12 6 17 13 45 1 6 18 12 7 31 14\r\n", "output": "574"}, {"input": "50\r\n10 25 27 13 28 35 40 39 3 6 18 29 44 1 26 2 45 36 9 46 41 12 33 19 8 22 15 48 34 20 11 32 1 47 43 23 7 5 14 30 31 21 38 42 24 49 4 37 16 17\r\n", "output": "49"}, {"input": "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\r\n", "output": "1225"}, {"input": "50\r\n50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50\r\n", "output": "1225"}, {"input": "3\r\n1 3 3\r\n", "output": "1"}, {"input": "10\r\n4 4 4 4 4 4 5 5 5 5\r\n", "output": "41"}, {"input": "4\r\n1 4 4 4\r\n", "output": "3"}, {"input": "3\r\n1 1 1\r\n", "output": "3"}, {"input": "3\r\n3 3 3\r\n", "output": "3"}]
false
stdio
null
true
158/C
158
C
PyPy 3-64
TESTS
6
124
0
231104758
a = int(input("")) d = [] for i in range(a): l = input("") if l == "pwd": [print("/", x, sep="", end = "") for x in d] print("/") else: ll="".join(l[3:]) ll = ll.split("/") #print(ll) for w in ll: if w == "..": d.pop() elif w != "": d.append(w)
29
124
0
18858108
class Shell: def __init__(self): self.wd = [''] def cd(self, where: str): glob = where.startswith('/') directories = where.split('/') if directories and not directories[0]: directories.pop(0) if glob: self._reset() for d in directories: self._cd_one_dir(d) def pwd(self): if len(self.wd) == 1: print('/') else: print('/', '/'.join(self.wd[1:]), '/', sep='') def _reset(self): self.wd = [''] def _cd_one_dir(self, d): if d == '..': self.wd.pop() else: self.wd.append(d) n = int(input()) sh = Shell() for _ in range(n): cmd = input() if cmd.startswith('cd'): sh.cd(cmd.split()[-1]) else: sh.pwd()
VK Cup 2012 Qualification Round 1
CF
2,012
3
256
Cd and pwd commands
Vasya is writing an operating system shell, and it should have commands for working with directories. To begin with, he decided to go with just two commands: cd (change the current directory) and pwd (display the current directory). Directories in Vasya's operating system form a traditional hierarchical tree structure. There is a single root directory, denoted by the slash character "/". Every other directory has a name — a non-empty string consisting of lowercase Latin letters. Each directory (except for the root) has a parent directory — the one that contains the given directory. It is denoted as "..". The command cd takes a single parameter, which is a path in the file system. The command changes the current directory to the directory specified by the path. The path consists of the names of directories separated by slashes. The name of the directory can be "..", which means a step up to the parent directory. «..» can be used in any place of the path, maybe several times. If the path begins with a slash, it is considered to be an absolute path, that is, the directory changes to the specified one, starting from the root. If the parameter begins with a directory name (or ".."), it is considered to be a relative path, that is, the directory changes to the specified directory, starting from the current one. The command pwd should display the absolute path to the current directory. This path must not contain "..". Initially, the current directory is the root. All directories mentioned explicitly or passed indirectly within any command cd are considered to exist. It is guaranteed that there is no attempt of transition to the parent directory of the root directory.
The first line of the input data contains the single integer n (1 ≤ n ≤ 50) — the number of commands. Then follow n lines, each contains one command. Each of these lines contains either command pwd, or command cd, followed by a space-separated non-empty parameter. The command parameter cd only contains lower case Latin letters, slashes and dots, two slashes cannot go consecutively, dots occur only as the name of a parent pseudo-directory. The command parameter cd does not end with a slash, except when it is the only symbol that points to the root directory. The command parameter has a length from 1 to 200 characters, inclusive. Directories in the file system can have the same names.
For each command pwd you should print the full absolute path of the given directory, ending with a slash. It should start with a slash and contain the list of slash-separated directories in the order of being nested from the root to the current folder. It should contain no dots.
null
null
[{"input": "7\npwd\ncd /home/vasya\npwd\ncd ..\npwd\ncd vasya/../petya\npwd", "output": "/\n/home/vasya/\n/home/\n/home/petya/"}, {"input": "4\ncd /a/b\npwd\ncd ../a/b\npwd", "output": "/a/b/\n/a/a/b/"}]
1,400
["*special", "data structures", "implementation"]
29
[{"input": "7\r\npwd\r\ncd /home/vasya\r\npwd\r\ncd ..\r\npwd\r\ncd vasya/../petya\r\npwd\r\n", "output": "/\r\n/home/vasya/\r\n/home/\r\n/home/petya/\r\n"}, {"input": "4\r\ncd /a/b\r\npwd\r\ncd ../a/b\r\npwd\r\n", "output": "/a/b/\r\n/a/a/b/\r\n"}, {"input": "1\r\npwd\r\n", "output": "/\r\n"}, {"input": "2\r\ncd /test/../test/../test/../test/../a/b/c/..\r\npwd\r\n", "output": "/a/b/\r\n"}, {"input": "9\r\ncd test\r\npwd\r\ncd ..\r\ncd /test\r\npwd\r\ncd ..\r\npwd\r\ncd test/test\r\npwd\r\n", "output": "/test/\r\n/test/\r\n/\r\n/test/test/\r\n"}, {"input": "6\r\ncd a/a/b/b\r\npwd\r\ncd ../..\r\npwd\r\ncd ..\r\npwd\r\n", "output": "/a/a/b/b/\r\n/a/a/\r\n/a/\r\n"}, {"input": "5\r\npwd\r\ncd /xgztbykka\r\npwd\r\ncd /gia/kxfls\r\npwd\r\n", "output": "/\r\n/xgztbykka/\r\n/gia/kxfls/\r\n"}, {"input": "17\r\npwd\r\ncd denwxe/../jhj/rxit/ie\r\npwd\r\ncd /tmyuylvul/qev/ezqit\r\npwd\r\ncd ../gxsfgyuspg/irleht\r\npwd\r\ncd wq/pqyz/tjotsmdzja\r\npwd\r\ncd ia/hs/../u/nemol/ffhf\r\npwd\r\ncd /lrdm/mvwxwb/llib\r\npwd\r\ncd /lmhu/wloover/rqd\r\npwd\r\ncd lkwabdw/../wrqn/x/../ien\r\npwd\r\n", "output": "/\r\n/jhj/rxit/ie/\r\n/tmyuylvul/qev/ezqit/\r\n/tmyuylvul/qev/gxsfgyuspg/irleht/\r\n/tmyuylvul/qev/gxsfgyuspg/irleht/wq/pqyz/tjotsmdzja/\r\n/tmyuylvul/qev/gxsfgyuspg/irleht/wq/pqyz/tjotsmdzja/ia/u/nemol/ffhf/\r\n/lrdm/mvwxwb/llib/\r\n/lmhu/wloover/rqd/\r\n/lmhu/wloover/rqd/wrqn/ien/\r\n"}, {"input": "5\r\ncd /xgztbykka\r\ncd /gia/kxfls\r\ncd /kiaxt/hcx\r\ncd /ufzoiv\r\npwd\r\n", "output": "/ufzoiv/\r\n"}, {"input": "17\r\ncd denwxe/../jhj/rxit/ie\r\ncd /tmyuylvul/qev/ezqit\r\ncd ../gxsfgyuspg/irleht\r\ncd wq/pqyz/tjotsmdzja\r\ncd ia/hs/../u/nemol/ffhf\r\ncd /lrdm/mvwxwb/llib\r\ncd /lmhu/wloover/rqd\r\ncd lkwabdw/../wrqn/x/../ien\r\ncd /rqljh/qyovqhiry/q\r\ncd /d/aargbeotxm/ovv\r\ncd /jaagwy/../xry/w/zdvx\r\ncd /nblqgcua/s/s/c/dgg\r\ncd /jktwitbkgj/ee/../../q\r\ncd wkx/jyphtd/h/../ygwc\r\ncd areyd/regf/ogvklan\r\ncd /wrbi/vbxefrd/jimis\r\npwd\r\n", "output": "/wrbi/vbxefrd/jimis/\r\n"}, {"input": "5\r\npwd\r\ncd ztb/kag\r\npwd\r\npwd\r\npwd\r\n", "output": "/\r\n/ztb/kag/\r\n/ztb/kag/\r\n/ztb/kag/\r\n"}, {"input": "17\r\ncd en/ebhjhjzlrx/pmieg\r\ncd uylvulohqe/wezq/oarx\r\npwd\r\ncd yus/fsi/ehtrs/../vjpq\r\ncd tjotsmdzja/diand/dqb\r\ncd emolqs/hff/rdmy/vw\r\ncd ../llibd/mhuos/oove\r\ncd /rqdqj/kwabd/nj/qng\r\npwd\r\ncd /yie/lrq/hmxq/vqhi\r\ncd qma/../aargbeotxm/ov\r\ncd /jaagwy/../xry/w/zdvx\r\npwd\r\ncd ../gcuagas/s/c/dggmz\r\npwd\r\npwd\r\ncd bkgjifee/../../../vfwkxjoj\r\n", "output": "/en/ebhjhjzlrx/pmieg/uylvulohqe/wezq/oarx/\r\n/rqdqj/kwabd/nj/qng/\r\n/xry/w/zdvx/\r\n/xry/w/gcuagas/s/c/dggmz/\r\n/xry/w/gcuagas/s/c/dggmz/\r\n"}, {"input": "50\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\npwd\r\n", "output": "/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n/\r\n"}, {"input": "1\r\ncd /\r\n", "output": ""}, {"input": "11\r\npwd\r\ncd /home/vasya\r\npwd\r\ncd ..\r\npwd\r\ncd vasya/../../petya\r\npwd\r\ncd /a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a\r\npwd\r\ncd ..\r\npwd\r\n", "output": "/\r\n/home/vasya/\r\n/home/\r\n/petya/\r\n/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/\r\n/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/\r\n"}]
false
stdio
null
true
432/D
432
D
PyPy 3-64
TESTS
9
249
25,804,800
199431396
import bisect import heapq import sys from types import GeneratorType from functools import cmp_to_key from collections import defaultdict, Counter, deque import math from functools import lru_cache from heapq import nlargest from functools import reduce import random from itertools import combinations from itertools import accumulate from operator import xor, add from operator import mul from typing import List inf = float("inf") PLATFORM = "CF" if PLATFORM == "LUOGU": import numpy as np sys.setrecursionlimit(1000000) class FastIO: def __init__(self): return @staticmethod def _read(): return sys.stdin.readline().strip() def read_int(self): return int(self._read()) def read_float(self): return float(self._read()) def read_ints(self): return map(int, self._read().split()) def read_floats(self): return map(float, self._read().split()) def read_ints_minus_one(self): return map(lambda x: int(x) - 1, self._read().split()) def read_list_ints(self): return list(map(int, self._read().split())) def read_list_floats(self): return list(map(float, self._read().split())) def read_list_ints_minus_one(self): return list(map(lambda x: int(x) - 1, self._read().split())) def read_str(self): return self._read() def read_list_strs(self): return self._read().split() def read_list_str(self): return list(self._read()) @staticmethod def st(x): return sys.stdout.write(str(x) + '\n') @staticmethod def lst(x): return sys.stdout.write(" ".join(str(w) for w in x) + '\n') @staticmethod def round_5(f): res = int(f) if f - res >= 0.5: res += 1 return res @staticmethod def max(a, b): return a if a > b else b @staticmethod def min(a, b): return a if a < b else b @staticmethod def bootstrap(f, queue=[]): def wrappedfunc(*args, **kwargs): if queue: return f(*args, **kwargs) else: to = f(*args, **kwargs) while True: if isinstance(to, GeneratorType): queue.append(to) to = next(to) else: queue.pop() if not queue: break to = queue[-1].send(to) return to return wrappedfunc class NumberTheory: def __init__(self): return @staticmethod def euler_phi(n): # 欧拉函数返回小于等于n的与n互质的个数 # 注意1和1互质,而大于1的质数与1不互质 ans = n for i in range(2, int(math.sqrt(n)) + 1): if n % i == 0: ans = ans // i * (i - 1) while n % i == 0: n = n // i if n > 1: ans = ans // n * (n - 1) return int(ans) class KMP: def __init__(self): return @staticmethod def prefix_function(s): # 计算s[:i]与s[:i]的最长公共真前缀与真后缀 n = len(s) pi = [0] * n for i in range(1, n): j = pi[i - 1] while j > 0 and s[i] != s[j]: j = pi[j - 1] if s[i] == s[j]: j += 1 pi[i] = j # pi[0] = 0 return pi @staticmethod def z_function(s): # 计算 s[i:] 与 s 的最长公共前缀 n = len(s) z = [0] * n left, r = 0, 0 for i in range(1, n): if i <= r and z[i - left] < r - i + 1: z[i] = z[i - left] else: z[i] = max(0, r - i + 1) while i + z[i] < n and s[z[i]] == s[i + z[i]]: z[i] += 1 if i + z[i] - 1 > r: left = i r = i + z[i] - 1 # z[0] = 0 return z class Solution: def __init__(self): return @staticmethod def main(ac=FastIO()): s = ac.read_str() n = len(s) z = KMP().z_function(s) z[0] = n cnt = Counter(z) ans = [] for i in range(n-1, -1, -1): if z[i] == n-i: ans.append([n-i, cnt[n-i]]) m = len(ans) for i in range(m-2, -1, -1): ans[i][1] += ans[i+1][1] ac.st(m) for a in ans: ac.lst(a) return Solution().main()
30
108
17,100,800
219090643
s = ' ' + input() n = len(s) r, c = [-1] * n, [1] * n for i in range(1, n): r[i] = r[i - 1] + 1 while r[i] and s[r[i]] != s[i]: r[i] = r[r[i] - 1] + 1 d, n = [], n - 1 for i in range(n, 1, -1): c[r[i]] += c[i] while n > 0: d.append(str(n) + ' ' + str(c[n])) n = r[n] print(len(d)) d.reverse() print('\n'.join(d))
Codeforces Round 246 (Div. 2)
CF
2,014
1
256
Prefixes and Suffixes
You have a string s = s1s2...s|s|, where |s| is the length of string s, and si its i-th character. Let's introduce several definitions: - A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string sisi + 1...sj. - The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. - The suffix of string s of length l (1 ≤ l ≤ |s|) is string s[|s| - l + 1..|s|]. Your task is, for any prefix of string s which matches a suffix of string s, print the number of times it occurs in string s as a substring.
The single line contains a sequence of characters s1s2...s|s| (1 ≤ |s| ≤ 105) — string s. The string only consists of uppercase English letters.
In the first line, print integer k (0 ≤ k ≤ |s|) — the number of prefixes that match a suffix of string s. Next print k lines, in each line print two integers li ci. Numbers li ci mean that the prefix of the length li matches the suffix of length li and occurs in string s as a substring ci times. Print pairs li ci in the order of increasing li.
null
null
[{"input": "ABACABA", "output": "3\n1 4\n3 2\n7 1"}, {"input": "AAA", "output": "3\n1 3\n2 2\n3 1"}]
2,000
["dp", "string suffix structures", "strings", "two pointers"]
30
[{"input": "ABACABA\r\n", "output": "3\r\n1 4\r\n3 2\r\n7 1\r\n"}, {"input": "AAA\r\n", "output": "3\r\n1 3\r\n2 2\r\n3 1\r\n"}, {"input": "A\r\n", "output": "1\r\n1 1\r\n"}, {"input": "AAAAAAAAAAAAAAAAXAAAAAAAAAAAAAAAAAAAAAAA\r\n", "output": "17\r\n1 39\r\n2 37\r\n3 35\r\n4 33\r\n5 31\r\n6 29\r\n7 27\r\n8 25\r\n9 23\r\n10 21\r\n11 19\r\n12 17\r\n13 15\r\n14 13\r\n15 11\r\n16 9\r\n40 1\r\n"}, {"input": "AB\r\n", "output": "1\r\n2 1\r\n"}, {"input": "AXAXA\r\n", "output": "3\r\n1 3\r\n3 2\r\n5 1\r\n"}, {"input": "CODEFORCES\r\n", "output": "1\r\n10 1\r\n"}, {"input": "GERALDPAVELGERALDPAVEL\r\n", "output": "2\r\n11 2\r\n22 1\r\n"}, {"input": "ZZ\r\n", "output": "2\r\n1 2\r\n2 1\r\n"}]
false
stdio
null
true
439/B
439
B
PyPy 3
TESTS
3
140
0
66242144
a,b=map(int,input().split()) l=list(map(int,input().split())) l=sorted(l) c=int(b) tot=0 d=0 for i in range(a): if c>0: tot+=l[i]*c elif c<1: break c-=1 if a<=b: print(tot) else: d=(l[(a-b):]) print(sum(d)+tot)
31
93
11,468,800
228681113
def solve(n, x, c): c.sort() s = 0 for t in c: s += t*x x = max(1,x-1) return s n, x = [int(_) for _ in input().split()] c = [int(_) for _ in input().split()] print(solve(n, x, c))
Codeforces Round 251 (Div. 2)
CF
2,014
1
256
Devu, the Dumb Guy
Devu is a dumb guy, his learning curve is very slow. You are supposed to teach him n subjects, the ith subject has ci chapters. When you teach him, you are supposed to teach all the chapters of a subject continuously. Let us say that his initial per chapter learning power of a subject is x hours. In other words he can learn a chapter of a particular subject in x hours. Well Devu is not complete dumb, there is a good thing about him too. If you teach him a subject, then time required to teach any chapter of the next subject will require exactly 1 hour less than previously required (see the examples to understand it more clearly). Note that his per chapter learning power can not be less than 1 hour. You can teach him the n subjects in any possible order. Find out minimum amount of time (in hours) Devu will take to understand all the subjects and you will be free to do some enjoying task rather than teaching a dumb guy. Please be careful that answer might not fit in 32 bit data type.
The first line will contain two space separated integers n, x (1 ≤ n, x ≤ 105). The next line will contain n space separated integers: c1, c2, ..., cn (1 ≤ ci ≤ 105).
Output a single integer representing the answer to the problem.
null
Look at the first example. Consider the order of subjects: 1, 2. When you teach Devu the first subject, it will take him 3 hours per chapter, so it will take 12 hours to teach first subject. After teaching first subject, his per chapter learning time will be 2 hours. Now teaching him second subject will take 2 × 1 = 2 hours. Hence you will need to spend 12 + 2 = 14 hours. Consider the order of subjects: 2, 1. When you teach Devu the second subject, then it will take him 3 hours per chapter, so it will take 3 × 1 = 3 hours to teach the second subject. After teaching the second subject, his per chapter learning time will be 2 hours. Now teaching him the first subject will take 2 × 4 = 8 hours. Hence you will need to spend 11 hours. So overall, minimum of both the cases is 11 hours. Look at the third example. The order in this example doesn't matter. When you teach Devu the first subject, it will take him 3 hours per chapter. When you teach Devu the second subject, it will take him 2 hours per chapter. When you teach Devu the third subject, it will take him 1 hours per chapter. In total it takes 6 hours.
[{"input": "2 3\n4 1", "output": "11"}, {"input": "4 2\n5 1 2 1", "output": "10"}, {"input": "3 3\n1 1 1", "output": "6"}]
1,200
["implementation", "sortings"]
31
[{"input": "2 3\r\n4 1\r\n", "output": "11\r\n"}, {"input": "4 2\r\n5 1 2 1\r\n", "output": "10\r\n"}, {"input": "3 3\r\n1 1 1\r\n", "output": "6\r\n"}, {"input": "20 4\r\n1 1 3 5 5 1 3 4 2 5 2 4 3 1 3 3 3 3 4 3\r\n", "output": "65\r\n"}, {"input": "20 10\r\n6 6 1 2 6 4 5 3 6 5 4 5 6 5 4 6 6 2 3 3\r\n", "output": "196\r\n"}, {"input": "1 1\r\n9273\r\n", "output": "9273\r\n"}, {"input": "1 1\r\n1\r\n", "output": "1\r\n"}, {"input": "1 2\r\n1\r\n", "output": "2\r\n"}, {"input": "1 2\r\n2\r\n", "output": "4\r\n"}, {"input": "2 1\r\n1 2\r\n", "output": "3\r\n"}]
false
stdio
null
true
489/A
489
A
Python 3
TESTS
5
30
0
227549872
elements = int(input()) arr = [int(i) for i in input().split()] swaps_num = 0 swaps = [] for i in range(elements): for j in range(i, elements): if i == j: continue if arr[i] > arr[j]: swaps_num += 1 temp = arr[i] arr[i] = arr[j] arr[j] = temp swaps.append([i, j]) print(swaps_num) for i in range(len(swaps)): print(*swaps[i])
22
78
1,228,800
8721277
__author__ = 'alexandrun' import sys #sys.stdin = open("p1.in", "r") n = int(input()) words = input().split() i = 0 ini = [] for w in words: ini.append((int(w), i)) i += 1 fin = ini[:] fin.sort() #print(ini) #print(fin) pos = {} i = 0 for pair in ini: pos[pair] = i i += 1 swaps = [] ifin = 0 for pair in fin: swaps.append((ifin, pos[pair])) aux = ini[ifin] ini[ifin] = ini[pos[pair]] ini[pos[pair]] = aux p1 = ini[ifin] p2 = ini[pos[pair]] aux = pos[p1] pos[p1] = pos[p2] pos[p2] = aux ifin += 1 #print() #print(swaps) #print(ini) print(len(swaps)) for s in swaps: print(s[0], s[1])
Codeforces Round 277.5 (Div. 2)
CF
2,014
1
256
SwapSort
In this problem your goal is to sort an array consisting of n integers in at most n swaps. For the given array find the sequence of swaps that makes the array sorted in the non-descending order. Swaps are performed consecutively, one after another. Note that in this problem you do not have to minimize the number of swaps — your task is to find any sequence that is no longer than n.
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of array elements. The second line contains elements of array: a0, a1, ..., an - 1 ( - 109 ≤ ai ≤ 109), where ai is the i-th element of the array. The elements are numerated from 0 to n - 1 from left to right. Some integers may appear in the array more than once.
In the first line print k (0 ≤ k ≤ n) — the number of swaps. Next k lines must contain the descriptions of the k swaps, one per line. Each swap should be printed as a pair of integers i, j (0 ≤ i, j ≤ n - 1), representing the swap of elements ai and aj. You can print indices in the pairs in any order. The swaps are performed in the order they appear in the output, from the first to the last. It is allowed to print i = j and swap the same pair of elements multiple times. If there are multiple answers, print any of them. It is guaranteed that at least one answer exists.
null
null
[{"input": "5\n5 2 5 1 4", "output": "2\n0 3\n4 2"}, {"input": "6\n10 20 20 40 60 60", "output": "0"}, {"input": "2\n101 100", "output": "1\n0 1"}]
1,200
["greedy", "implementation", "sortings"]
22
[{"input": "5\r\n5 2 5 1 4\r\n", "output": "2\r\n0 3\r\n4 2\r\n"}, {"input": "6\r\n10 20 20 40 60 60\r\n", "output": "0\r\n"}, {"input": "2\r\n101 100\r\n", "output": "1\r\n0 1\r\n"}, {"input": "1\r\n1000\r\n", "output": "0\r\n"}, {"input": "2\r\n1000000000 -1000000000\r\n", "output": "1\r\n0 1\r\n"}, {"input": "8\r\n5 2 6 8 3 1 6 8\r\n", "output": "4\r\n0 5\r\n4 2\r\n5 3\r\n6 5\r\n"}, {"input": "2\r\n200000000 199999999\r\n", "output": "1\r\n0 1\r\n"}, {"input": "3\r\n100000000 100000002 100000001\r\n", "output": "1\r\n1 2\r\n"}, {"input": "5\r\n1000000000 -10000000 0 8888888 7777777\r\n", "output": "3\r\n0 1\r\n2 1\r\n4 2\r\n"}, {"input": "5\r\n10 30 20 50 40\r\n", "output": "2\r\n1 2\r\n4 3\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] sub_path = sys.argv[3] with open(input_path) as f: n = int(f.readline().strip()) arr = list(map(int, f.readline().split())) with open(sub_path) as f: lines = f.readlines() if not lines: print(0) return try: k = int(lines[0].strip()) swaps = [] for line in lines[1:1 + k]: i, j = map(int, line.strip().split()) swaps.append((i, j)) except (ValueError, IndexError): print(0) return if not (0 <= k <= n): print(0) return for i, j in swaps: if not (0 <= i < n and 0 <= j < n): print(0) return current = arr.copy() for i, j in swaps: current[i], current[j] = current[j], current[i] sorted_arr = sorted(arr) if current == sorted_arr: print(1) else: print(0) if __name__ == "__main__": main()
true
154/A
154
A
PyPy 3
TESTS
4
124
17,715,200
134807402
# Author Name: Ajay Meena # Codeforce : https://codeforces.com/profile/majay1638 import sys import math import bisect import heapq from bisect import bisect_right from sys import stdin, stdout # -------------- INPUT FUNCTIONS ------------------ def get_ints_in_variables(): return map( int, sys.stdin.readline().strip().split()) def get_int(): return int(sys.stdin.readline()) def get_ints_in_list(): return list( map(int, sys.stdin.readline().strip().split())) def get_list_of_list(n): return [list( map(int, sys.stdin.readline().strip().split())) for _ in range(n)] def get_string(): return sys.stdin.readline().strip() # -------- SOME CUSTOMIZED FUNCTIONS----------- def myceil(x, y): return (x + y - 1) // y # -------------- SOLUTION FUNCTION ------------------ def Solution(): # Write Your Code Here s = [c for c in get_string()] n = get_int() arr = [get_string() for _ in range(n)] ans = 0 for i in range(len(s)-1): c = s[i]+s[i+1] for v in arr: if v[0] in c and v[1] in c: s[i+1] = "X" ans += 1 break print(ans) def main(): # Take input Here and Call solution function Solution() # calling main Function if __name__ == '__main__': main()
42
278
2,764,800
186980434
from collections import defaultdict from sys import stdin input = stdin.readline s = input().strip() k = int(input()) ans = 0 for _ in range(k): t = input().strip() a = b = 0 for c in s: if c == t[0]: a += 1 elif c == t[1]: b += 1 else: ans += min(a, b) a = b = 0 ans += min(a, b) print(ans) # https://blog.51cto.com/u_15303184/3098436
Codeforces Round 109 (Div. 1)
CF
2,012
2
256
Hometask
Sergey attends lessons of the N-ish language. Each lesson he receives a hometask. This time the task is to translate some sentence to the N-ish language. Sentences of the N-ish language can be represented as strings consisting of lowercase Latin letters without spaces or punctuation marks. Sergey totally forgot about the task until half an hour before the next lesson and hastily scribbled something down. But then he recollected that in the last lesson he learned the grammar of N-ish. The spelling rules state that N-ish contains some "forbidden" pairs of letters: such letters can never occur in a sentence next to each other. Also, the order of the letters doesn't matter (for example, if the pair of letters "ab" is forbidden, then any occurrences of substrings "ab" and "ba" are also forbidden). Also, each pair has different letters and each letter occurs in no more than one forbidden pair. Now Sergey wants to correct his sentence so that it doesn't contain any "forbidden" pairs of letters that stand next to each other. However, he is running out of time, so he decided to simply cross out some letters from the sentence. What smallest number of letters will he have to cross out? When a letter is crossed out, it is "removed" so that the letters to its left and right (if they existed), become neighboring. For example, if we cross out the first letter from the string "aba", we get the string "ba", and if we cross out the second letter, we get "aa".
The first line contains a non-empty string s, consisting of lowercase Latin letters — that's the initial sentence in N-ish, written by Sergey. The length of string s doesn't exceed 105. The next line contains integer k (0 ≤ k ≤ 13) — the number of forbidden pairs of letters. Next k lines contain descriptions of forbidden pairs of letters. Each line contains exactly two different lowercase Latin letters without separators that represent the forbidden pairs. It is guaranteed that each letter is included in no more than one pair.
Print the single number — the smallest number of letters that need to be removed to get a string without any forbidden pairs of neighboring letters. Please note that the answer always exists as it is always possible to remove all letters.
null
In the first sample you should remove two letters b. In the second sample you should remove the second or the third letter. The second restriction doesn't influence the solution.
[{"input": "ababa\n1\nab", "output": "2"}, {"input": "codeforces\n2\ndo\ncs", "output": "1"}]
1,600
["greedy"]
42
[{"input": "ababa\r\n1\r\nab\r\n", "output": "2\r\n"}, {"input": "codeforces\r\n2\r\ndo\r\ncs\r\n", "output": "1\r\n"}, {"input": "nllnrlrnll\r\n1\r\nrl\r\n", "output": "1\r\n"}, {"input": "aludfbjtwnkgnfl\r\n1\r\noy\r\n", "output": "0\r\n"}, {"input": "pgpgppgggpbbnnn\r\n2\r\npg\r\nnb\r\n", "output": "7\r\n"}, {"input": "eepeeeeppppppeepeppe\r\n1\r\npe\r\n", "output": "10\r\n"}, {"input": "vefneyamdzoytemupniw\r\n13\r\nve\r\nfg\r\noi\r\nan\r\nck\r\nwx\r\npq\r\nml\r\nbt\r\nud\r\nrz\r\nsj\r\nhy\r\n", "output": "1\r\n"}, {"input": "drvwfaacccwnncfwuptsorrrvvvrgdzytrwweeexzyyyxuwuuk\r\n13\r\nld\r\nac\r\nnp\r\nrv\r\nmo\r\njh\r\ngb\r\nuw\r\nfq\r\nst\r\nkx\r\nzy\r\nei\r\n", "output": "11\r\n"}, {"input": "pninnihzipirpbdggrdglzdpbldtzihgbzdnrgznbpdanhnlag\r\n4\r\nli\r\nqh\r\nad\r\nbp\r\n", "output": "4\r\n"}, {"input": "mbmxuuuuxuuuuhhooooxxxuxxxuxuuxuuuxxjvjvjjjjvvvjjjjjvvjvjjjvvvjjvjjvvvjjjvjvvjvjjjjjmmbmbbbbbmbbbbmm\r\n5\r\nmb\r\nho\r\nxu\r\njv\r\nyp\r\n", "output": "37\r\n"}, {"input": "z\r\n0\r\n", "output": "0\r\n"}, {"input": "t\r\n13\r\nzx\r\nig\r\nyq\r\nbd\r\nph\r\nar\r\nne\r\nwo\r\ntk\r\njl\r\ncv\r\nfs\r\nmu\r\n", "output": "0\r\n"}, {"input": "rbnxovfcwkdjctdjfskaozjzthlcntuaoiavnbsfpuzxyvhfbxetvryvwrqetokdshadxpxijtpkrqvghsrejgnqntwiypiglzmp\r\n13\r\njr\r\nnf\r\nyk\r\ntq\r\nwe\r\nil\r\ngu\r\npb\r\naz\r\nxm\r\nhc\r\nvd\r\nso\r\n", "output": "0\r\n"}, {"input": "yynynnyyyiynyniiiinyynniiyiyyyniyniyynyyyynyynnniiiniyyniiyyiynyiynnnnyiiyiyniyyininiyiiiyynnnyiyinnnnyiinnnnnyninyinyynynyiynyyyiyinyynnyyinynyinininyniniynniiyyiiyy\r\n1\r\nni\r\n", "output": "28\r\n"}, {"input": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\r\n1\r\nab\r\n", "output": "75\r\n"}]
false
stdio
null
true
549/D
549
D
Python 3
TESTS
5
46
0
11490679
import sys def solve(): n, m = map(int, input().split()) res = 0 tab = [list(input()) for _ in range(n)] for row in range(n): for col in range(m): tab[row][col] = 1 if tab[row][col] == 'W' else 0 for row in range(n - 1, -1, -1): for col in range(m - 1, -1, -1): if tab[row][col] != 0: diff = tab[row][col] res+=1 for i in range(row + 1): for j in range(col + 1): tab[i][j] -= diff return res if sys.hexversion == 50594544 : sys.stdin = open("test.txt") print(solve())
47
155
102,400
11568044
n, m = map(int, input().split(' ')) p = [input() for i in range(n)] p1 = [[1 if p[i][j] == 'B' else -1 for j in range(m)] for i in range(n)] tm = [0 for i in range(m)] r = 0 for i in range(n-1, -1, -1): for j in range(m-1, -1, -1): if tm[j] != p1[i][j]: r = r + 1 tp = p1[i][j] - tm[j] for l in range(j + 1): tm[l] = tm[l] + tp print(r)
Looksery Cup 2015
CF
2,015
1
256
Haar Features
The first algorithm for detecting a face on the image working in realtime was developed by Paul Viola and Michael Jones in 2001. A part of the algorithm is a procedure that computes Haar features. As part of this task, we consider a simplified model of this concept. Let's consider a rectangular image that is represented with a table of size n × m. The table elements are integers that specify the brightness of each pixel in the image. A feature also is a rectangular table of size n × m. Each cell of a feature is painted black or white. To calculate the value of the given feature at the given image, you must perform the following steps. First the table of the feature is put over the table of the image (without rotations or reflections), thus each pixel is entirely covered with either black or white cell. The value of a feature in the image is the value of W - B, where W is the total brightness of the pixels in the image, covered with white feature cells, and B is the total brightness of the pixels covered with black feature cells. Some examples of the most popular Haar features are given below. Your task is to determine the number of operations that are required to calculate the feature by using the so-called prefix rectangles. A prefix rectangle is any rectangle on the image, the upper left corner of which coincides with the upper left corner of the image. You have a variable value, whose value is initially zero. In one operation you can count the sum of pixel values ​​at any prefix rectangle, multiply it by any integer and add to variable value. You are given a feature. It is necessary to calculate the minimum number of operations required to calculate the values of this attribute at an arbitrary image. For a better understanding of the statement, read the explanation of the first sample.
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100) — the number of rows and columns in the feature. Next n lines contain the description of the feature. Each line consists of m characters, the j-th character of the i-th line equals to "W", if this element of the feature is white and "B" if it is black.
Print a single number — the minimum number of operations that you need to make to calculate the value of the feature.
null
The first sample corresponds to feature B, the one shown in the picture. The value of this feature in an image of size 6 × 8 equals to the difference of the total brightness of the pixels in the lower and upper half of the image. To calculate its value, perform the following two operations: 1. add the sum of pixels in the prefix rectangle with the lower right corner in the 6-th row and 8-th column with coefficient 1 to the variable value (the rectangle is indicated by a red frame); 2. add the number of pixels in the prefix rectangle with the lower right corner in the 3-rd row and 8-th column with coefficient  - 2 and variable value. Thus, all the pixels in the lower three rows of the image will be included with factor 1, and all pixels in the upper three rows of the image will be included with factor 1 - 2 = - 1, as required.
[{"input": "6 8\nBBBBBBBB\nBBBBBBBB\nBBBBBBBB\nWWWWWWWW\nWWWWWWWW\nWWWWWWWW", "output": "2"}, {"input": "3 3\nWBW\nBWW\nWWW", "output": "4"}, {"input": "3 6\nWWBBWW\nWWBBWW\nWWBBWW", "output": "3"}, {"input": "4 4\nBBBB\nBBBB\nBBBB\nBBBW", "output": "4"}]
1,900
["greedy", "implementation"]
47
[{"input": "6 8\r\nBBBBBBBB\r\nBBBBBBBB\r\nBBBBBBBB\r\nWWWWWWWW\r\nWWWWWWWW\r\nWWWWWWWW\r\n", "output": "2\r\n"}, {"input": "3 3\r\nWBW\r\nBWW\r\nWWW\r\n", "output": "4\r\n"}, {"input": "3 6\r\nWWBBWW\r\nWWBBWW\r\nWWBBWW\r\n", "output": "3\r\n"}, {"input": "4 4\r\nBBBB\r\nBBBB\r\nBBBB\r\nBBBW\r\n", "output": "4\r\n"}, {"input": "10 9\r\nBWWWBWWBB\r\nBBWWBWBBW\r\nBBWBWBWBB\r\nBWBWBBBBB\r\nBBWBWBWBW\r\nBWWBWWBBW\r\nWBWWWBWWW\r\nWBBWBWBWW\r\nBBWWBWWBB\r\nBBWWBWWBW\r\n", "output": "61\r\n"}, {"input": "4 1\r\nW\r\nW\r\nB\r\nB\r\n", "output": "2\r\n"}, {"input": "2 10\r\nBBWBWWBWBB\r\nBBBBBBBBBW\r\n", "output": "10\r\n"}, {"input": "100 1\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nW\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\nB\r\n", "output": "2\r\n"}, {"input": "1 100\r\nWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\r\n", "output": "2\r\n"}, {"input": "4 5\r\nBWWBB\r\nBWBBW\r\nWBWWW\r\nWBWWB\r\n", "output": "13\r\n"}, {"input": "2 9\r\nWBBBWBBBW\r\nBWWBBBBBB\r\n", "output": "9\r\n"}, {"input": "6 6\r\nBBWWWB\r\nWBBBWB\r\nBBBBBW\r\nWWWWWW\r\nBBBBBW\r\nBWWBBB\r\n", "output": "16\r\n"}, {"input": "1 1\r\nW\r\n", "output": "1\r\n"}, {"input": "1 1\r\nB\r\n", "output": "1\r\n"}, {"input": "1 8\r\nWWBWWWWW\r\n", "output": "3\r\n"}, {"input": "2 8\r\nBBBBBBBB\r\nBBBBBBBB\r\n", "output": "1\r\n"}, {"input": "1 52\r\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\r\n", "output": "1\r\n"}, {"input": "11 8\r\nWWWWWWWW\r\nWWWWWWWW\r\nWWWWWWWW\r\nWWWWWWWW\r\nWWWWWWWW\r\nWWWBWWWW\r\nWWWWWWWW\r\nWBWWWWWW\r\nWWWWWWWW\r\nWWWWWWWW\r\nWWWWWWWW\r\n", "output": "9\r\n"}]
false
stdio
null
true
925/B
925
B
PyPy 3-64
TESTS
0
46
512,000
144816962
from math import ceil from bisect import bisect_left as bl n,m1,m2=map(int,input().strip().split()) d={m1:0,m2:1} a=[*map(int,input().strip().split())] a=list(zip(a,range(len(a)))) a.sort(key=lambda s:s[0]) val={m1:1111,m2:1111} for i in range(1,n+1): req=ceil(m1/i) ll = bl(a, (req, -1)) if n - ll >= i: val[m1]=i break for i in range(1,n+1): req=ceil(m2/i) ll = bl(a, (req, -1)) if n - ll >= i: val[m2]=i break m1,m2=sorted([m1,m2],key=lambda s:val[s],reverse=True) a1=[] for i in range(1,n+1): req=ceil(m2/i) ll=bl(a,(req,-1)) if n-ll>=i: a1+=a[ll:ll+i] a=a[:ll]+a[ll+i:] break # print(a) n=len(a) a2=[] for i in range(1,n+1): req=ceil(m1/i) ll=bl(a,(req,-1)) if n-ll>=i: a2+=a[ll:ll+i] break ans=[None,None] if m1!=m2: ans[d[m2]]=a1 ans[d[m1]]=a2 else: ans[0]=a1 ans[1]=a2 # print(a1,a2) if len(a1)==0 or len(a2)==0: print("No") exit() print("Yes") print(len(ans[0]),len(ans[1])) for val,id in ans[0]: print(id+1,end=" ") print('') for val,id in ans[1]: print(id+1,end=" ")
40
1,216
39,219,200
42132645
# python3 def readline(): return tuple(map(int, input().split())) def ceil_div(num, den): return (num - 1) // den + 1 def main(): n, x1, x2 = readline() c = readline() xx = (x1, x2) servers = sorted(enumerate(c, start=1), key=lambda p: p[1]) for (i, a) in enumerate(servers): for (j, x) in enumerate(xx): kj = ceil_div(x, a[1]) if i + kj < n and (n - i - kj) * servers[i + kj][1] >= sum(xx) - x: print("Yes") l1 = servers[i:i+kj] l2 = servers[i+kj:] if j: l1, l2 = l2, l1 print(len(l1), len(l2)) print(" ".join(str(d[0]) for d in l1)) print(" ".join(str(d[0]) for d in l2)) return print("No") main() # Made By Mostafa_Khaled
VK Cup 2018 - Round 3
CF
2,018
2
256
Resource Distribution
One department of some software company has $$$n$$$ servers of different specifications. Servers are indexed with consecutive integers from $$$1$$$ to $$$n$$$. Suppose that the specifications of the $$$j$$$-th server may be expressed with a single integer number $$$c_j$$$ of artificial resource units. In order for production to work, it is needed to deploy two services $$$S_1$$$ and $$$S_2$$$ to process incoming requests using the servers of the department. Processing of incoming requests of service $$$S_i$$$ takes $$$x_i$$$ resource units. The described situation happens in an advanced company, that is why each service may be deployed using not only one server, but several servers simultaneously. If service $$$S_i$$$ is deployed using $$$k_i$$$ servers, then the load is divided equally between these servers and each server requires only $$$x_i / k_i$$$ (that may be a fractional number) resource units. Each server may be left unused at all, or be used for deploying exactly one of the services (but not for two of them simultaneously). The service should not use more resources than the server provides. Determine if it is possible to deploy both services using the given servers, and if yes, determine which servers should be used for deploying each of the services.
The first line contains three integers $$$n$$$, $$$x_1$$$, $$$x_2$$$ ($$$2 \leq n \leq 300\,000$$$, $$$1 \leq x_1, x_2 \leq 10^9$$$) — the number of servers that the department may use, and resource units requirements for each of the services. The second line contains $$$n$$$ space-separated integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq 10^9$$$) — the number of resource units provided by each of the servers.
If it is impossible to deploy both services using the given servers, print the only word "No" (without the quotes). Otherwise print the word "Yes" (without the quotes). In the second line print two integers $$$k_1$$$ and $$$k_2$$$ ($$$1 \leq k_1, k_2 \leq n$$$) — the number of servers used for each of the services. In the third line print $$$k_1$$$ integers, the indices of the servers that will be used for the first service. In the fourth line print $$$k_2$$$ integers, the indices of the servers that will be used for the second service. No index may appear twice among the indices you print in the last two lines. If there are several possible answers, it is allowed to print any of them.
null
In the first sample test each of the servers 1, 2 and 6 will will provide $$$8 / 3 = 2.(6)$$$ resource units and each of the servers 5, 4 will provide $$$16 / 2 = 8$$$ resource units. In the second sample test the first server will provide $$$20$$$ resource units and each of the remaining servers will provide $$$32 / 3 = 10.(6)$$$ resource units.
[{"input": "6 8 16\n3 5 2 9 8 7", "output": "Yes\n3 2\n1 2 6\n5 4"}, {"input": "4 20 32\n21 11 11 12", "output": "Yes\n1 3\n1\n2 3 4"}, {"input": "4 11 32\n5 5 16 16", "output": "No"}, {"input": "5 12 20\n7 8 4 11 9", "output": "No"}]
1,700
["binary search", "implementation", "sortings"]
40
[{"input": "6 8 16\r\n3 5 2 9 8 7\r\n", "output": "Yes\r\n4 2\r\n3 1 2 6\r\n5 4\r\n"}, {"input": "4 20 32\r\n21 11 11 12\r\n", "output": "Yes\r\n1 3\r\n1\r\n2 3 4\r\n"}, {"input": "4 11 32\r\n5 5 16 16\r\n", "output": "No\r\n"}, {"input": "5 12 20\r\n7 8 4 11 9\r\n", "output": "No\r\n"}, {"input": "2 1 1\r\n1 1\r\n", "output": "Yes\r\n1 1\r\n1\r\n2\r\n"}, {"input": "2 1 1\r\n1 1000000\r\n", "output": "Yes\r\n1 1\r\n1\r\n2\r\n"}, {"input": "2 1 1\r\n1000000000 1000000000\r\n", "output": "Yes\r\n1 1\r\n1\r\n2\r\n"}, {"input": "2 1 2\r\n1 1\r\n", "output": "No\r\n"}, {"input": "15 250 200\r\n71 2 77 69 100 53 54 40 73 32 82 58 24 82 41\r\n", "output": "Yes\r\n11 3\r\n13 10 8 15 6 7 12 4 1 9 3\r\n11 14 5\r\n"}, {"input": "4 12 11\r\n4 4 6 11\r\n", "output": "Yes\r\n3 1\r\n1 2 3\r\n4\r\n"}]
false
stdio
import sys def main(input_path, output_path, submission_path): with open(input_path) as f: n, x1, x2 = map(int, f.readline().split()) c = list(map(int, f.readline().split())) with open(output_path) as f: ref_lines = [line.strip() for line in f] with open(submission_path) as f: sub_lines = [line.strip() for line in f] non_empty_ref = [line for line in ref_lines if line] non_empty_sub = [line for line in sub_lines if line] if not non_empty_ref: print(0) return ref_first = non_empty_ref[0] if ref_first == 'No': if len(non_empty_sub) == 1 and non_empty_sub[0] == 'No': print(1) else: print(0) return else: if len(non_empty_sub) != 4 or non_empty_sub[0] != 'Yes': print(0) return try: k1, k2 = map(int, non_empty_sub[1].split()) except: print(0) return if k1 <= 0 or k2 <= 0 or k1 + k2 > n: print(0) return try: s1 = list(map(int, non_empty_sub[2].split())) s2 = list(map(int, non_empty_sub[3].split())) except: print(0) return if len(s1) != k1 or len(s2) != k2: print(0) return all_indices = set() for s in s1: if not (1 <= s <= n) or s in all_indices: print(0) return all_indices.add(s) for s in s2: if not (1 <= s <= n) or s in all_indices: print(0) return all_indices.add(s) for s in s1: if c[s-1] * k1 < x1: print(0) return for s in s2: if c[s-1] * k2 < x2: print(0) return print(1) return 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
154/A
154
A
PyPy 3
TESTS
4
248
0
57094042
def is_valid(word, rules): for rule in rules: if rule in word: return False return True class CodeforcesTask154ASolution: def __init__(self): self.result = '' self.word = '' self.rules_count = 0 self.forbidden = [] def read_input(self): self.word = input() self.rules_count = int(input()) for x in range(self.rules_count): self.forbidden.append(input()) def process_task(self): forbidden_rev = [x[::-1] for x in self.forbidden] forbs = forbidden_rev + self.forbidden #print(forbs) cross = 0 while not is_valid(self.word, forbs): for rule in forbs: index = self.word.find(rule) cross += 1 if index == 0: self.word = self.word.replace(rule, rule[1], 1) elif index > 0: if self.word[index - 1] + self.word[index + 1] in forbs: self.word = self.word.replace(rule, rule[0], 1) else: self.word = self.word.replace(rule, rule[1], 1) else: cross -= 1 self.result = str(cross) def get_result(self): return self.result if __name__ == "__main__": Solution = CodeforcesTask154ASolution() Solution.read_input() Solution.process_task() print(Solution.get_result())
42
342
3,072,000
101687730
import sys #input = sys.stdin.readline for _ in range(1): s=input() n=len(s) d={} for _ in range(int(input())): a=input() d[a[0]]=a[1] d[a[1]]=a[0] i=0 ans=0 while i<n: if s[i] in d: a,b=0,0 while True: start=i while i<n and s[i]==s[start]: a+=1 i+=1 if i<n and s[i]==d[s[start]]: #b+=1 start=i while i<n and s[i]==s[start]: b+=1 i+=1 if i<n and s[i]==d[s[start]]: continue else: ans+=min(a,b) break else: i+=1 print(ans)
Codeforces Round 109 (Div. 1)
CF
2,012
2
256
Hometask
Sergey attends lessons of the N-ish language. Each lesson he receives a hometask. This time the task is to translate some sentence to the N-ish language. Sentences of the N-ish language can be represented as strings consisting of lowercase Latin letters without spaces or punctuation marks. Sergey totally forgot about the task until half an hour before the next lesson and hastily scribbled something down. But then he recollected that in the last lesson he learned the grammar of N-ish. The spelling rules state that N-ish contains some "forbidden" pairs of letters: such letters can never occur in a sentence next to each other. Also, the order of the letters doesn't matter (for example, if the pair of letters "ab" is forbidden, then any occurrences of substrings "ab" and "ba" are also forbidden). Also, each pair has different letters and each letter occurs in no more than one forbidden pair. Now Sergey wants to correct his sentence so that it doesn't contain any "forbidden" pairs of letters that stand next to each other. However, he is running out of time, so he decided to simply cross out some letters from the sentence. What smallest number of letters will he have to cross out? When a letter is crossed out, it is "removed" so that the letters to its left and right (if they existed), become neighboring. For example, if we cross out the first letter from the string "aba", we get the string "ba", and if we cross out the second letter, we get "aa".
The first line contains a non-empty string s, consisting of lowercase Latin letters — that's the initial sentence in N-ish, written by Sergey. The length of string s doesn't exceed 105. The next line contains integer k (0 ≤ k ≤ 13) — the number of forbidden pairs of letters. Next k lines contain descriptions of forbidden pairs of letters. Each line contains exactly two different lowercase Latin letters without separators that represent the forbidden pairs. It is guaranteed that each letter is included in no more than one pair.
Print the single number — the smallest number of letters that need to be removed to get a string without any forbidden pairs of neighboring letters. Please note that the answer always exists as it is always possible to remove all letters.
null
In the first sample you should remove two letters b. In the second sample you should remove the second or the third letter. The second restriction doesn't influence the solution.
[{"input": "ababa\n1\nab", "output": "2"}, {"input": "codeforces\n2\ndo\ncs", "output": "1"}]
1,600
["greedy"]
42
[{"input": "ababa\r\n1\r\nab\r\n", "output": "2\r\n"}, {"input": "codeforces\r\n2\r\ndo\r\ncs\r\n", "output": "1\r\n"}, {"input": "nllnrlrnll\r\n1\r\nrl\r\n", "output": "1\r\n"}, {"input": "aludfbjtwnkgnfl\r\n1\r\noy\r\n", "output": "0\r\n"}, {"input": "pgpgppgggpbbnnn\r\n2\r\npg\r\nnb\r\n", "output": "7\r\n"}, {"input": "eepeeeeppppppeepeppe\r\n1\r\npe\r\n", "output": "10\r\n"}, {"input": "vefneyamdzoytemupniw\r\n13\r\nve\r\nfg\r\noi\r\nan\r\nck\r\nwx\r\npq\r\nml\r\nbt\r\nud\r\nrz\r\nsj\r\nhy\r\n", "output": "1\r\n"}, {"input": "drvwfaacccwnncfwuptsorrrvvvrgdzytrwweeexzyyyxuwuuk\r\n13\r\nld\r\nac\r\nnp\r\nrv\r\nmo\r\njh\r\ngb\r\nuw\r\nfq\r\nst\r\nkx\r\nzy\r\nei\r\n", "output": "11\r\n"}, {"input": "pninnihzipirpbdggrdglzdpbldtzihgbzdnrgznbpdanhnlag\r\n4\r\nli\r\nqh\r\nad\r\nbp\r\n", "output": "4\r\n"}, {"input": "mbmxuuuuxuuuuhhooooxxxuxxxuxuuxuuuxxjvjvjjjjvvvjjjjjvvjvjjjvvvjjvjjvvvjjjvjvvjvjjjjjmmbmbbbbbmbbbbmm\r\n5\r\nmb\r\nho\r\nxu\r\njv\r\nyp\r\n", "output": "37\r\n"}, {"input": "z\r\n0\r\n", "output": "0\r\n"}, {"input": "t\r\n13\r\nzx\r\nig\r\nyq\r\nbd\r\nph\r\nar\r\nne\r\nwo\r\ntk\r\njl\r\ncv\r\nfs\r\nmu\r\n", "output": "0\r\n"}, {"input": "rbnxovfcwkdjctdjfskaozjzthlcntuaoiavnbsfpuzxyvhfbxetvryvwrqetokdshadxpxijtpkrqvghsrejgnqntwiypiglzmp\r\n13\r\njr\r\nnf\r\nyk\r\ntq\r\nwe\r\nil\r\ngu\r\npb\r\naz\r\nxm\r\nhc\r\nvd\r\nso\r\n", "output": "0\r\n"}, {"input": "yynynnyyyiynyniiiinyynniiyiyyyniyniyynyyyynyynnniiiniyyniiyyiynyiynnnnyiiyiyniyyininiyiiiyynnnyiyinnnnyiinnnnnyninyinyynynyiynyyyiyinyynnyyinynyinininyniniynniiyyiiyy\r\n1\r\nni\r\n", "output": "28\r\n"}, {"input": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\r\n1\r\nab\r\n", "output": "75\r\n"}]
false
stdio
null
true
560/A
560
A
PyPy 3
TESTS
11
124
0
62326847
n=input() x=input() if(" 1 " in x): print(-1) elif(x[0]=="1"): print(-1) elif(x[len(x)-1]=="1"): print(-1) else: print(1)
16
31
0
146537537
input() a=list(map(int,input().split())) print(-1 if 1 in a else 1)
Codeforces Round 313 (Div. 2)
CF
2,015
2
256
Currency System in Geraldion
A magic island Geraldion, where Gerald lives, has its own currency system. It uses banknotes of several values. But the problem is, the system is not perfect and sometimes it happens that Geraldionians cannot express a certain sum of money with any set of banknotes. Of course, they can use any number of banknotes of each value. Such sum is called unfortunate. Gerald wondered: what is the minimum unfortunate sum?
The first line contains number n (1 ≤ n ≤ 1000) — the number of values of the banknotes that used in Geraldion. The second line contains n distinct space-separated numbers a1, a2, ..., an (1 ≤ ai ≤ 106) — the values of the banknotes.
Print a single line — the minimum unfortunate sum. If there are no unfortunate sums, print - 1.
null
null
[{"input": "5\n1 2 3 4 5", "output": "-1"}]
1,000
["implementation", "sortings"]
16
[{"input": "5\r\n1 2 3 4 5\r\n", "output": "-1\r\n"}, {"input": "1\r\n2\r\n", "output": "1\r\n"}, {"input": "10\r\n371054 506438 397130 1 766759 208409 769264 549213 641270 771837\r\n", "output": "-1\r\n"}, {"input": "10\r\n635370 154890 909382 220996 276501 716105 538714 140162 171960 271264\r\n", "output": "1\r\n"}, {"input": "50\r\n110876 835020 859879 999908 712969 788264 287153 921820 330355 499311 209594 484829 296329 940051 174081 931503 1 780512 390075 97866 124255 950067 697612 244256 782385 789882 37608 82153 399889 598867 416717 377988 535636 511221 792568 683271 131077 290194 496712 330720 587436 563481 645817 942562 654093 980561 382937 48293 582608 116156\r\n", "output": "-1\r\n"}, {"input": "50\r\n474421 421097 217233 156339 27075 733996 281778 863492 184707 956857 288561 70997 393786 337382 663642 131184 637 273801 799870 295017 392338 842567 161819 297705 102013 930684 375703 838048 154915 138503 629056 256591 893619 19263 787927 684541 320265 841090 421423 490879 394582 493952 619247 633202 612928 50907 276653 407819 489945 153173\r\n", "output": "1\r\n"}, {"input": "1\r\n1\r\n", "output": "-1\r\n"}, {"input": "1\r\n1000000\r\n", "output": "1\r\n"}, {"input": "2\r\n3 2\r\n", "output": "1\r\n"}, {"input": "2\r\n2 3\r\n", "output": "1\r\n"}]
false
stdio
null
true
154/A
154
A
Python 3
TESTS
4
92
307,200
5544384
t = input() p, n = {}, len(t) for i in range(int(input())): q = input() p[q[0]], p[q[1]] = q[1], q[0] if n == 1: print(0) elif n == 2: print(int(p[t[0]] == t[1])) else: a, b, c = t[0], t[1], t[2] i, s = 3, 0 while True: if a in p and p[a] == b: s += 1 if i > n - 2: break a, b, c = c, t[i], t[i + 1] i += 2 else: if i > n - 1: if b in p and p[b] == c: s += 1 break a, b, c = b, c, t[i] i += 1 print(s)
42
372
2,662,400
101616018
s=input() ans=0 for _ in range(int(input())): a,b=0,0 p=input() for x in s: if(x==p[0]):a+=1 elif(x==p[1]):b+=1 else: ans+=min(a,b); a,b=0,0 ans+=min(a,b) print(ans)
Codeforces Round 109 (Div. 1)
CF
2,012
2
256
Hometask
Sergey attends lessons of the N-ish language. Each lesson he receives a hometask. This time the task is to translate some sentence to the N-ish language. Sentences of the N-ish language can be represented as strings consisting of lowercase Latin letters without spaces or punctuation marks. Sergey totally forgot about the task until half an hour before the next lesson and hastily scribbled something down. But then he recollected that in the last lesson he learned the grammar of N-ish. The spelling rules state that N-ish contains some "forbidden" pairs of letters: such letters can never occur in a sentence next to each other. Also, the order of the letters doesn't matter (for example, if the pair of letters "ab" is forbidden, then any occurrences of substrings "ab" and "ba" are also forbidden). Also, each pair has different letters and each letter occurs in no more than one forbidden pair. Now Sergey wants to correct his sentence so that it doesn't contain any "forbidden" pairs of letters that stand next to each other. However, he is running out of time, so he decided to simply cross out some letters from the sentence. What smallest number of letters will he have to cross out? When a letter is crossed out, it is "removed" so that the letters to its left and right (if they existed), become neighboring. For example, if we cross out the first letter from the string "aba", we get the string "ba", and if we cross out the second letter, we get "aa".
The first line contains a non-empty string s, consisting of lowercase Latin letters — that's the initial sentence in N-ish, written by Sergey. The length of string s doesn't exceed 105. The next line contains integer k (0 ≤ k ≤ 13) — the number of forbidden pairs of letters. Next k lines contain descriptions of forbidden pairs of letters. Each line contains exactly two different lowercase Latin letters without separators that represent the forbidden pairs. It is guaranteed that each letter is included in no more than one pair.
Print the single number — the smallest number of letters that need to be removed to get a string without any forbidden pairs of neighboring letters. Please note that the answer always exists as it is always possible to remove all letters.
null
In the first sample you should remove two letters b. In the second sample you should remove the second or the third letter. The second restriction doesn't influence the solution.
[{"input": "ababa\n1\nab", "output": "2"}, {"input": "codeforces\n2\ndo\ncs", "output": "1"}]
1,600
["greedy"]
42
[{"input": "ababa\r\n1\r\nab\r\n", "output": "2\r\n"}, {"input": "codeforces\r\n2\r\ndo\r\ncs\r\n", "output": "1\r\n"}, {"input": "nllnrlrnll\r\n1\r\nrl\r\n", "output": "1\r\n"}, {"input": "aludfbjtwnkgnfl\r\n1\r\noy\r\n", "output": "0\r\n"}, {"input": "pgpgppgggpbbnnn\r\n2\r\npg\r\nnb\r\n", "output": "7\r\n"}, {"input": "eepeeeeppppppeepeppe\r\n1\r\npe\r\n", "output": "10\r\n"}, {"input": "vefneyamdzoytemupniw\r\n13\r\nve\r\nfg\r\noi\r\nan\r\nck\r\nwx\r\npq\r\nml\r\nbt\r\nud\r\nrz\r\nsj\r\nhy\r\n", "output": "1\r\n"}, {"input": "drvwfaacccwnncfwuptsorrrvvvrgdzytrwweeexzyyyxuwuuk\r\n13\r\nld\r\nac\r\nnp\r\nrv\r\nmo\r\njh\r\ngb\r\nuw\r\nfq\r\nst\r\nkx\r\nzy\r\nei\r\n", "output": "11\r\n"}, {"input": "pninnihzipirpbdggrdglzdpbldtzihgbzdnrgznbpdanhnlag\r\n4\r\nli\r\nqh\r\nad\r\nbp\r\n", "output": "4\r\n"}, {"input": "mbmxuuuuxuuuuhhooooxxxuxxxuxuuxuuuxxjvjvjjjjvvvjjjjjvvjvjjjvvvjjvjjvvvjjjvjvvjvjjjjjmmbmbbbbbmbbbbmm\r\n5\r\nmb\r\nho\r\nxu\r\njv\r\nyp\r\n", "output": "37\r\n"}, {"input": "z\r\n0\r\n", "output": "0\r\n"}, {"input": "t\r\n13\r\nzx\r\nig\r\nyq\r\nbd\r\nph\r\nar\r\nne\r\nwo\r\ntk\r\njl\r\ncv\r\nfs\r\nmu\r\n", "output": "0\r\n"}, {"input": "rbnxovfcwkdjctdjfskaozjzthlcntuaoiavnbsfpuzxyvhfbxetvryvwrqetokdshadxpxijtpkrqvghsrejgnqntwiypiglzmp\r\n13\r\njr\r\nnf\r\nyk\r\ntq\r\nwe\r\nil\r\ngu\r\npb\r\naz\r\nxm\r\nhc\r\nvd\r\nso\r\n", "output": "0\r\n"}, {"input": "yynynnyyyiynyniiiinyynniiyiyyyniyniyynyyyynyynnniiiniyyniiyyiynyiynnnnyiiyiyniyyininiyiiiyynnnyiyinnnnyiinnnnnyninyinyynynyiynyyyiyinyynnyyinynyinininyniniynniiyyiiyy\r\n1\r\nni\r\n", "output": "28\r\n"}, {"input": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\r\n1\r\nab\r\n", "output": "75\r\n"}]
false
stdio
null
true
455/B
455
B
PyPy 3-64
TESTS
4
62
0
216139921
def is_winner(words, k): # Check if all the words have the same parity of lengths (either all even or all odd). is_all_even = all(len(word) % 2 == 0 for word in words) is_all_odd = all(len(word) % 2 == 1 for word in words) # If all the words have the same parity, the winner of the last game depends on the number of games (k). # If the number of games (k) is odd, the winner of the last game is the same as the first game. # If the number of games (k) is even, the winner of the last game is the opposite of the first game. if is_all_even or is_all_odd: return "First" if k % 2 == 1 else "Second" else: return "Second" # Read input n, k = map(int, input().split()) words = [input().strip() for _ in range(n)] # Output the result print(is_winner(words, k))
75
701
41,984,000
229069437
max_alpha = 26 class TrieNode: def __init__(self): self.children = [0] * max_alpha def add_trie(root, word): v = root for char in word: c = ord(char) - ord('a') if not v.children[c]: v.children[c] = TrieNode() v = v.children[c] def dfs(v): v.win = False v.lose = False is_leaf = True for i in range(max_alpha): if v.children[i]: is_leaf = False to = v.children[i] dfs(to) v.win |= not to.win v.lose |= not to.lose if is_leaf: v.lose = True class TrieNode: def __init__(self): self.children = [0] * max_alpha self.win = False self.lose = False def answer(res): print("First" if res else "Second") exit(0) n, k = map(int, input().split()) root = TrieNode() for _ in range(n): word = input().strip() add_trie(root, word) dfs(root) if k == 1: answer(root.win) elif not root.win: answer(root.win) elif root.lose: answer(root.win) elif k % 2 == 1: answer(root.win) else: answer(not root.win)
Codeforces Round 260 (Div. 1)
CF
2,014
1
256
A Lot of Games
Andrew, Fedor and Alex are inventive guys. Now they invent the game with strings for two players. Given a group of n non-empty strings. During the game two players build the word together, initially the word is empty. The players move in turns. On his step player must add a single letter in the end of the word, the resulting word must be prefix of at least one string from the group. A player loses if he cannot move. Andrew and Alex decided to play this game k times. The player who is the loser of the i-th game makes the first move in the (i + 1)-th game. Guys decided that the winner of all games is the player who wins the last (k-th) game. Andrew and Alex already started the game. Fedor wants to know who wins the game if both players will play optimally. Help him.
The first line contains two integers, n and k (1 ≤ n ≤ 105; 1 ≤ k ≤ 109). Each of the next n lines contains a single non-empty string from the given group. The total length of all strings from the group doesn't exceed 105. Each string of the group consists only of lowercase English letters.
If the player who moves first wins, print "First", otherwise print "Second" (without the quotes).
null
null
[{"input": "2 3\na\nb", "output": "First"}, {"input": "3 1\na\nb\nc", "output": "First"}, {"input": "1 2\nab", "output": "Second"}]
1,900
["dfs and similar", "dp", "games", "implementation", "strings", "trees"]
75
[{"input": "2 3\r\na\r\nb\r\n", "output": "First\r\n"}, {"input": "3 1\r\na\r\nb\r\nc\r\n", "output": "First\r\n"}, {"input": "1 2\r\nab\r\n", "output": "Second\r\n"}, {"input": "5 6\r\nabas\r\ndsfdf\r\nabacaba\r\ndartsidius\r\nkolobok\r\n", "output": "Second\r\n"}, {"input": "4 2\r\naaaa\r\nbbbb\r\nccccc\r\ndumbavumba\r\n", "output": "First\r\n"}, {"input": "3 8\r\nso\r\nbad\r\ntest\r\n", "output": "First\r\n"}, {"input": "5 2\r\nwelcome\r\nto\r\nthe\r\nmatrix\r\nneo\r\n", "output": "First\r\n"}, {"input": "6 4\r\ndog\r\ncat\r\ncow\r\nhot\r\nice\r\nlol\r\n", "output": "Second\r\n"}, {"input": "4 8\r\nla\r\na\r\nz\r\nka\r\n", "output": "First\r\n"}, {"input": "3 2\r\nop\r\nhop\r\ncop\r\n", "output": "First\r\n"}, {"input": "3 3\r\nabasdfabab\r\nabaaasdfdsf\r\nasdfaba\r\n", "output": "Second\r\n"}, {"input": "2 2\r\naba\r\naa\r\n", "output": "Second\r\n"}, {"input": "4 1\r\naa\r\naba\r\nba\r\nbba\r\n", "output": "Second\r\n"}, {"input": "1 3\r\nab\r\n", "output": "Second\r\n"}, {"input": "3 3\r\naa\r\nabb\r\ncc\r\n", "output": "Second\r\n"}]
false
stdio
null
true
562/F
566
A
PyPy 3
TESTS
0
93
0
12331028
SIGMA = 26 class Node: def __init__(self): self.ch = [None] * SIGMA self.a = [] self.b = [] def add(self, s, i): t = self for c in s: v = ord(c) - ord('a') if not t.ch[v]: t.ch[v] = Node() t = t.ch[v] t.a += [i] def inc(self, s, i): t = self for c in s: v = ord(c) - ord('a') if not t.ch[v]: break t = t.ch[v] t.b += [i] def solve(self, d): if not self: return (0, [], [], []) res = 0 pairs = [] for i in range(SIGMA): if self.ch[i]: t, a, b, p = self.ch[i].solve(d + 1) res += t self.a += a self.b += b pairs += p k = min(len(self.a), len(self.b)) res += d * k pairs += [(self.a[i], self.b[i]) for i in range(k)] self.a = self.a[k:] self.b = self.b[k:] return (res, self.a, self.b, pairs) N = int(input()) A = [input() for i in range(N)] B = [input() for i in range(N)] T = Node() for i, s in enumerate(B): T.add(s, i + 1) for i, s in enumerate(A): T.inc(s, i + 1) res = T.solve(0) print(res[0]) for i, j in res[3]: print('%d %d' % (i, j))
38
1,372
262,553,600
12334711
import sys class Node: def __init__(self, d): global nodes self.ch = {} self.a = [[], []] self.d = d nodes += [self] nodes = [] pairs = [] res = 0 N = int(sys.stdin.readline()) _input = sys.stdin.readlines() _input = [s[:-1] for s in _input] A = [_input[:N], _input[N:]] T = Node(0) for i, l in enumerate(A): for j, s in enumerate(l): t = T for c in s: v = ord(c) - ord('a') if not v in t.ch: t.ch[v] = Node(t.d + 1) t = t.ch[v] t.a[i] += [j + 1] for n in reversed(nodes): for i in n.ch: n.a[0] += n.ch[i].a[0] n.a[1] += n.ch[i].a[1] k = min(len(n.a[0]), len(n.a[1])) for i in range(k): pairs += [str(n.a[0][-1]) + ' ' + str(n.a[1][-1])] n.a[0].pop() n.a[1].pop() res += n.d print(res) print('\n'.join(pairs))
VK Cup 2015 - Finals
CF
2,015
2
256
Matching Names
Teachers of one programming summer school decided to make a surprise for the students by giving them names in the style of the "Hobbit" movie. Each student must get a pseudonym maximally similar to his own name. The pseudonym must be a name of some character of the popular saga and now the teachers are busy matching pseudonyms to student names. There are n students in a summer school. Teachers chose exactly n pseudonyms for them. Each student must get exactly one pseudonym corresponding to him. Let us determine the relevance of a pseudonym b to a student with name a as the length of the largest common prefix a and b. We will represent such value as $$\operatorname{lcp}(a,b)$$. Then we can determine the quality of matching of the pseudonyms to students as a sum of relevances of all pseudonyms to the corresponding students. Find the matching between students and pseudonyms with the maximum quality.
The first line contains number n (1 ≤ n ≤ 100 000) — the number of students in the summer school. Next n lines contain the name of the students. Each name is a non-empty word consisting of lowercase English letters. Some names can be repeating. The last n lines contain the given pseudonyms. Each pseudonym is a non-empty word consisting of small English letters. Some pseudonyms can be repeating. The total length of all the names and pseudonyms doesn't exceed 800 000 characters.
In the first line print the maximum possible quality of matching pseudonyms to students. In the next n lines describe the optimal matching. Each line must have the form a b (1 ≤ a, b ≤ n), that means that the student who was number a in the input, must match to the pseudonym number b in the input. The matching should be a one-to-one correspondence, that is, each student and each pseudonym should occur exactly once in your output. If there are several optimal answers, output any.
null
The first test from the statement the match looks as follows: - bill  →  bilbo (lcp = 3) - galya  →  galadriel (lcp = 3) - gennady  →  gendalf (lcp = 3) - toshik  →  torin (lcp = 2) - boris  →  smaug (lcp = 0)
[{"input": "5\ngennady\ngalya\nboris\nbill\ntoshik\nbilbo\ntorin\ngendalf\nsmaug\ngaladriel", "output": "11\n4 1\n2 5\n1 3\n5 2\n3 4"}]
2,300
[]
38
[{"input": "5\r\ngennady\r\ngalya\r\nboris\r\nbill\r\ntoshik\r\nbilbo\r\ntorin\r\ngendalf\r\nsmaug\r\ngaladriel\r\n", "output": "11\r\n4 1\r\n2 5\r\n1 3\r\n5 2\r\n3 4\r\n"}, {"input": "1\r\na\r\na\r\n", "output": "1\r\n1 1\r\n"}, {"input": "2\r\na\r\na\r\na\r\na\r\n", "output": "2\r\n1 1\r\n2 2\r\n"}, {"input": "2\r\na\r\nb\r\na\r\na\r\n", "output": "1\r\n1 1\r\n2 2\r\n"}, {"input": "2\r\nb\r\nb\r\na\r\na\r\n", "output": "0\r\n1 1\r\n2 2\r\n"}, {"input": "2\r\na\r\nb\r\na\r\nb\r\n", "output": "2\r\n1 1\r\n2 2\r\n"}, {"input": "10\r\nbaa\r\na\r\nba\r\naabab\r\naa\r\nbaab\r\nbb\r\nabbbb\r\na\r\na\r\na\r\nba\r\nba\r\nbaabbb\r\nba\r\na\r\naabb\r\nbaa\r\nab\r\nb\r\n", "output": "17\r\n4 7\r\n8 9\r\n2 1\r\n9 6\r\n6 4\r\n1 8\r\n3 2\r\n7 10\r\n10 3\r\n5 5\r\n"}, {"input": "10\r\nabaabbaaa\r\nacccccaacabc\r\nacbaabaaabbca\r\naaccca\r\ncbbba\r\naaba\r\nacab\r\nac\r\ncbac\r\nca\r\nbbbbc\r\nbacbcbcaac\r\nc\r\ncba\r\na\r\nabba\r\nbcabc\r\nabcccaa\r\nab\r\na\r\n", "output": "10\r\n1 9\r\n6 5\r\n4 10\r\n8 6\r\n7 8\r\n9 4\r\n10 3\r\n3 2\r\n2 1\r\n5 7\r\n"}, {"input": "1\r\nzzzz\r\nyyx\r\n", "output": "0\r\n1 1\r\n"}, {"input": "1\r\naa\r\naaa\r\n", "output": "2\r\n1 1\r\n"}, {"input": "1\r\naaa\r\naa\r\n", "output": "2\r\n1 1\r\n"}, {"input": "10\r\nb\r\nb\r\na\r\na\r\na\r\na\r\nb\r\nb\r\na\r\nb\r\nb\r\na\r\na\r\na\r\nb\r\nb\r\nb\r\na\r\nb\r\nb\r\n", "output": "9\r\n3 2\r\n4 3\r\n5 4\r\n6 8\r\n1 1\r\n2 5\r\n7 6\r\n8 7\r\n10 9\r\n9 10\r\n"}, {"input": "10\r\na\r\nb\r\na\r\na\r\nc\r\na\r\na\r\na\r\na\r\na\r\nb\r\nc\r\nc\r\na\r\nc\r\nb\r\na\r\na\r\na\r\nc\r\n", "output": "6\r\n1 4\r\n3 7\r\n4 8\r\n6 9\r\n2 1\r\n5 2\r\n7 6\r\n8 3\r\n9 5\r\n10 10\r\n"}, {"input": "10\r\nw\r\nr\r\na\r\nc\r\nx\r\ne\r\nb\r\nx\r\nw\r\nx\r\nz\r\ng\r\nd\r\ny\r\ns\r\ny\r\nj\r\nh\r\nl\r\nu\r\n", "output": "0\r\n3 3\r\n7 2\r\n4 8\r\n6 7\r\n2 9\r\n1 5\r\n9 10\r\n5 4\r\n8 6\r\n10 1\r\n"}]
false
stdio
import sys def lcp(s, t): min_len = min(len(s), len(t)) cnt = 0 for i in range(min_len): if s[i] == t[i]: cnt += 1 else: break return cnt def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] # Read input with open(input_path, 'r') as f: n = int(f.readline()) students = [f.readline().strip() for _ in range(n)] pseudonyms = [f.readline().strip() for _ in range(n)] # Read reference sum with open(output_path, 'r') as f: ref_sum_line = f.readline().strip() if not ref_sum_line: print(0) return try: reference_sum = int(ref_sum_line) except ValueError: print(0) return # Read submission with open(submission_path, 'r') as f: # Read submitted sum sum_line = f.readline().strip() if not sum_line: print(0) return try: submitted_sum = int(sum_line) except ValueError: print(0) return if submitted_sum != reference_sum: print(0) return # Read the pairs a_list = [] b_list = [] a_set = set() b_set = set() for _ in range(n): line = f.readline().strip() parts = line.split() if len(parts) != 2: print(0) return try: a = int(parts[0]) b = int(parts[1]) except ValueError: print(0) return if a < 1 or a > n or b < 1 or b > n: print(0) return if a in a_set or b in b_set: print(0) return a_set.add(a) b_set.add(b) a_list.append(a) b_list.append(b) # Check if all a and b are present if len(a_set) != n or len(b_set) != n: print(0) return # Compute sum computed_sum = 0 for a, b in zip(a_list, b_list): s = students[a-1] p = pseudonyms[b-1] computed_sum += lcp(s, p) if computed_sum != submitted_sum: print(0) return # All checks passed print(1) if __name__ == "__main__": main()
true
436/C
436
C
PyPy 3
TESTS
8
171
23,142,400
87968019
def put(): return map(int, input().split()) def diff(x,y): ans = 0 for i in range(n*m): if s[x][i]!= s[y][i]: ans+=1 return ans def find(i): if i==p[i]: return i p[i] = find(p[i]) return p[i] def union(i,j): if rank[i]>rank[j]: i,j = j,i elif rank[i]==rank[j]: rank[j]+=1 p[i]= j def dfs(i,p): print(i+1,p+1) for j in tree[i]: if j!=p: dfs(j,i) n,m,k,w = put() s = ['']*k for i in range(k): for j in range(n): s[i]+=input() edge = [] rank = [0]*k p = list(range(k)) cost = n*m tree = [[] for i in range(k)] for i in range(k): for j in range(i+1,k): z = diff(i,j) edge.append((z,i,j)) edge.sort() for z,i,j in edge: u = find(i) v = find(j) if u!=v: union(u,v) cost+= z*w tree[i].append(j) tree[j].append(i) print(cost) dfs(0,-1)
30
1,466
70,041,600
87968659
def put(): return map(int, input().split()) def diff(x,y): ans = 0 for i in range(n*m): if s[x][i]!= s[y][i]: ans+=1 return ans def find(i): if i==p[i]: return i p[i] = find(p[i]) return p[i] def union(i,j): if rank[i]>rank[j]: i,j = j,i elif rank[i]==rank[j]: rank[j]+=1 p[i]= j def dfs(i,p): if i!=0: print(i,p) for j in tree[i]: if j!=p: dfs(j,i) n,m,k,w = put() s = ['']*k for i in range(k): for j in range(n): s[i]+=input() edge = [] k+=1 rank = [0]*(k) p = list(range(k)) cost = 0 tree = [[] for i in range(k)] for i in range(k): for j in range(i+1,k): if i==0: z=n*m else: z = diff(i-1,j-1)*w edge.append((z,i,j)) edge.sort() for z,i,j in edge: u = find(i) v = find(j) if u!=v: union(u,v) cost+= z tree[i].append(j) tree[j].append(i) print(cost) dfs(0,-1)
Zepto Code Rush 2014
CF
2,014
2
256
Dungeons and Candies
During the loading of the game "Dungeons and Candies" you are required to get descriptions of k levels from the server. Each description is a map of an n × m checkered rectangular field. Some cells of the field contain candies (each cell has at most one candy). An empty cell is denoted as "." on the map, but if a cell has a candy, it is denoted as a letter of the English alphabet. A level may contain identical candies, in this case the letters in the corresponding cells of the map will be the same. When you transmit information via a network, you want to minimize traffic — the total size of the transferred data. The levels can be transmitted in any order. There are two ways to transmit the current level A: 1. You can transmit the whole level A. Then you need to transmit n·m bytes via the network. 2. You can transmit the difference between level A and some previously transmitted level B (if it exists); this operation requires to transmit dA, B·w bytes, where dA, B is the number of cells of the field that are different for A and B, and w is a constant. Note, that you should compare only the corresponding cells of levels A and B to calculate dA, B. You cannot transform the maps of levels, i.e. rotate or shift them relatively to each other. Your task is to find a way to transfer all the k levels and minimize the traffic.
The first line contains four integers n, m, k, w (1 ≤ n, m ≤ 10; 1 ≤ k, w ≤ 1000). Then follows the description of k levels. Each level is described by n lines, each line contains m characters. Each character is either a letter of the English alphabet or a dot ("."). Please note that the case of the letters matters.
In the first line print the required minimum number of transferred bytes. Then print k pairs of integers x1, y1, x2, y2, ..., xk, yk, describing the way to transfer levels. Pair xi, yi means that level xi needs to be transferred by way yi. If yi equals 0, that means that the level must be transferred using the first way, otherwise yi must be equal to the number of a previously transferred level. It means that you will transfer the difference between levels yi and xi to transfer level xi. Print the pairs in the order of transferring levels. The levels are numbered 1 through k in the order they follow in the input. If there are multiple optimal solutions, you can print any of them.
null
null
[{"input": "2 3 3 2\nA.A\n...\nA.a\n..C\nX.Y\n...", "output": "14\n1 0\n2 1\n3 1"}, {"input": "1 1 4 1\nA\n.\nB\n.", "output": "3\n1 0\n2 0\n4 2\n3 0"}, {"input": "1 3 5 2\nABA\nBBB\nBBA\nBAB\nABB", "output": "11\n1 0\n3 1\n2 3\n4 2\n5 1"}]
1,800
["dsu", "graphs", "greedy", "trees"]
30
[{"input": "2 3 3 2\r\nA.A\r\n...\r\nA.a\r\n..C\r\nX.Y\r\n...\r\n", "output": "14\r\n1 0\r\n2 1\r\n3 1\r\n"}, {"input": "1 1 4 1\r\nA\r\n.\r\nB\r\n.\r\n", "output": "3\r\n1 0\r\n2 0\r\n4 2\r\n3 0\r\n"}, {"input": "1 3 5 2\r\nABA\r\nBBB\r\nBBA\r\nBAB\r\nABB\r\n", "output": "11\r\n1 0\r\n3 1\r\n2 3\r\n4 2\r\n5 1\r\n"}, {"input": "2 2 5 1\r\n..\r\nBA\r\n.A\r\nB.\r\n..\r\nA.\r\nAB\r\n.B\r\n..\r\n..\r\n", "output": "12\r\n1 0\r\n2 1\r\n3 1\r\n5 3\r\n4 5\r\n"}, {"input": "3 3 10 2\r\nBA.\r\n..A\r\n.BB\r\nB..\r\n..B\r\n.AA\r\nB..\r\nAB.\r\n..A\r\nBAB\r\n.A.\r\n.B.\r\n..B\r\nA..\r\n...\r\n...\r\n.B.\r\nBA.\r\n..B\r\n.AB\r\n.B.\r\nB.A\r\n.A.\r\n.BA\r\n..B\r\n...\r\n.A.\r\n.AA\r\n..A\r\n.B.\r\n", "output": "67\r\n1 0\r\n10 1\r\n2 1\r\n3 2\r\n4 1\r\n7 4\r\n9 7\r\n5 9\r\n6 9\r\n8 4\r\n"}, {"input": "3 1 5 1\r\nB\r\nA\r\nB\r\nA\r\nA\r\nB\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\n", "output": "5\r\n1 0\r\n2 1\r\n3 2\r\n4 3\r\n5 3\r\n"}, {"input": "3 2 10 1\r\nAB\r\nBA\r\nAB\r\nAA\r\nAA\r\nBA\r\nAA\r\nAA\r\nAB\r\nAB\r\nAB\r\nBA\r\nBA\r\nAB\r\nAA\r\nBB\r\nAB\r\nBA\r\nBB\r\nBB\r\nBA\r\nAA\r\nAA\r\nAB\r\nAB\r\nAB\r\nBA\r\nBB\r\nAB\r\nAA\r\n", "output": "16\r\n1 0\r\n3 1\r\n8 3\r\n2 3\r\n4 2\r\n9 4\r\n6 4\r\n7 6\r\n10 6\r\n5 10\r\n"}, {"input": "2 3 10 2\r\nABB\r\nABA\r\nAAB\r\nBAB\r\nAAA\r\nBBA\r\nBBB\r\nBAA\r\nBBB\r\nABB\r\nABA\r\nBBA\r\nBBB\r\nAAB\r\nABA\r\nABB\r\nBBA\r\nBAB\r\nBBB\r\nBBB\r\n", "output": "38\r\n1 0\r\n5 1\r\n7 5\r\n4 7\r\n9 4\r\n10 5\r\n6 1\r\n3 6\r\n8 1\r\n2 0\r\n"}, {"input": "1 1 1 1\r\n.\r\n", "output": "1\r\n1 0\r\n"}]
false
stdio
import sys def read_levels(input_path): with open(input_path) as f: lines = [line.strip() for line in f.readlines() if line.strip()] first_line = lines[0].split() n, m, k, w = map(int, first_line) levels = [None] # 1-based indexing idx = 1 for _ in range(k): level = [] for _ in range(n): if idx >= len(lines): return None, None, None, None, None level.append(lines[idx].strip()) idx += 1 levels.append(level) return n, m, k, w, levels def main(input_path, output_path, submission_path): n, m, k, w, levels = read_levels(input_path) if levels is None: return 0 # Read reference output's first line with open(output_path) as f_ref: ref_lines = f_ref.read().splitlines() if not ref_lines: return 0 ref_total = int(ref_lines[0]) # Read submission output with open(submission_path) as f_sub: sub_lines = f_sub.read().splitlines() if not sub_lines: return 0 try: sub_total = int(sub_lines[0]) except: return 0 # Check sub_total matches reference if sub_total != ref_total: return 0 # Parse pairs tokens = [] for line in sub_lines[1:]: tokens.extend(line.strip().split()) if len(tokens) != 2 * k: return 0 pairs = [] try: for i in range(0, len(tokens), 2): xi = int(tokens[i]) yi = int(tokens[i+1]) pairs.append((xi, yi)) except: return 0 # Check all xi are unique and 1..k seen_xi = set() for xi, _ in pairs: if xi < 1 or xi > k or xi in seen_xi: return 0 seen_xi.add(xi) if len(seen_xi) != k: return 0 # Check pairs order and calculate total transmitted = set() total = 0 for xi, yi in pairs: if yi != 0 and yi not in transmitted: return 0 if yi == 0: cost = n * m else: # Compute d between xi and yi d = 0 for row in range(n): for col in range(m): if levels[xi][row][col] != levels[yi][row][col]: d += 1 cost = d * w total += cost transmitted.add(xi) if total != sub_total: return 0 return 1 if __name__ == "__main__": input_path, output_path, submission_path = sys.argv[1:] score = main(input_path, output_path, submission_path) print(score)
true
985/C
985
C
Python 3
TESTS
7
202
8,192,000
38517262
from bisect import bisect_right def bad(): print(0) exit() n, k, l_ = map(int, input().split()) a = sorted(list(map(int, input().split()))) if n == 1: print(min(a)) exit() if a[1] - a[0] > l_: bad() b = bisect_right(a, a[0] + l_) if b < n: bad() to_sum = [0] if n == 2: print(a[0] + a[n // 2]) exit() to_sum += [min(k * i, b - n + i) for i in range(1, n)] print(sum(a[i] for i in to_sum))
50
93
14,131,200
215825613
import sys input = sys.stdin.readline n, k, l = map(int, input().split()) a = list(map(int, input().split())) a.sort() ans = 0 c = 0 for i in range(n * k - 1, -1, -1 ): c += 1 if(a[i] - a[0] <= l and c >= k): ans += a[i] c -= k print((ans, 0)[c > 0])
Educational Codeforces Round 44 (Rated for Div. 2)
ICPC
2,018
2
256
Liebig's Barrels
You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel. Let volume vj of barrel j be equal to the length of the minimal stave in it. You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n. Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.
The first line contains three space-separated integers n, k and l (1 ≤ n, k ≤ 105, 1 ≤ n·k ≤ 105, 0 ≤ l ≤ 109). The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.
Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.
null
In the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3]. In the second example you can form the following barrels: [10], [10]. In the third example you can form the following barrels: [2, 5]. In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough.
[{"input": "4 2 1\n2 2 1 2 3 2 2 3", "output": "7"}, {"input": "2 1 0\n10 10", "output": "20"}, {"input": "1 2 1\n5 2", "output": "2"}, {"input": "3 2 1\n1 2 3 4 5 6", "output": "0"}]
1,500
["greedy"]
50
[{"input": "4 2 1\r\n2 2 1 2 3 2 2 3\r\n", "output": "7\r\n"}, {"input": "2 1 0\r\n10 10\r\n", "output": "20\r\n"}, {"input": "1 2 1\r\n5 2\r\n", "output": "2\r\n"}, {"input": "3 2 1\r\n1 2 3 4 5 6\r\n", "output": "0\r\n"}, {"input": "10 3 189\r\n267 697 667 4 52 128 85 616 142 344 413 660 962 194 618 329 266 593 558 447 89 983 964 716 32 890 267 164 654 71\r\n", "output": "0\r\n"}, {"input": "10 3 453\r\n277 706 727 812 692 686 196 507 911 40 498 704 573 381 463 759 704 381 693 640 326 405 47 834 962 521 463 740 520 494\r\n", "output": "2979\r\n"}, {"input": "10 3 795\r\n398 962 417 307 760 534 536 450 421 280 608 111 687 726 941 903 630 900 555 403 795 122 814 188 234 976 679 539 525 104\r\n", "output": "5045\r\n"}, {"input": "6 2 29\r\n1 2 3 3 4 5 5 6 7 7 8 9\r\n", "output": "28\r\n"}, {"input": "2 1 2\r\n1 2\r\n", "output": "3\r\n"}]
false
stdio
null
true