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
981/D
981
D
PyPy 3-64
TESTS
53
467
27,443,200
213585802
def solve(): n, k = map(int, input().split()) *nums, = map(int, input().split()) acc = [0] * (n + 1) for i in range(n): acc[i + 1] = acc[i] + nums[i] from functools import cache @cache def f(i, k, target): if i == 0 and k == 0: return True if k ==0: return False if i == 0: return False flag = False for j in range(i): flag |= f(j, k - 1, target) & (((acc[i] - acc[j]) & target) == target) return flag mask = 0 for i in range(53, -1, -1): tmp_mask = mask | (1 << i) if f(n, k, tmp_mask): mask |= (1 << i) print(mask) solve()
64
108
5,939,200
213581077
import os import sys from io import BytesIO, IOBase BUFSIZE = 4096 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 = IOWrapper(sys.stdin) sys.stdout = IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") def I(): return input() def II(): return int(input()) def LII(): return list(map(int, input().split())) def count_bit(num): cnt = 0 while num != 0: cnt += 1 num //= 2 return cnt n, k = LII() a = LII() s = [0] * (n + 1) for i in range(1, n + 1): s[i] = s[i - 1] + a[i - 1] ans = 0 for i in range(count_bit(s[n]) - 1, -1, -1): target = ans | (1 << i) f = [[False] * (n + 1) for _ in range(k + 1)] f[0][0] = True for j in range(1, k + 1): for r in range(1, n + 1): for l, ok in enumerate(f[j - 1][:r]): if ok and (s[r] - s[l]) & target == target: f[j][r] = True break if f[k][n]: ans = target print(ans)
Avito Code Challenge 2018
CF
2,018
1
256
Bookshelves
Mr Keks is a typical white-collar in Byteland. He has a bookshelf in his office with some books on it, each book has an integer positive price. Mr Keks defines the value of a shelf as the sum of books prices on it. Miraculously, Mr Keks was promoted and now he is moving into a new office. He learned that in the new office he will have not a single bookshelf, but exactly $$$k$$$ bookshelves. He decided that the beauty of the $$$k$$$ shelves is the bitwise AND of the values of all the shelves. He also decided that he won't spend time on reordering the books, so he will place several first books on the first shelf, several next books on the next shelf and so on. Of course, he will place at least one book on each shelf. This way he will put all his books on $$$k$$$ shelves in such a way that the beauty of the shelves is as large as possible. Compute this maximum possible beauty.
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 50$$$) — the number of books and the number of shelves in the new office. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots a_n$$$, ($$$0 < a_i < 2^{50}$$$) — the prices of the books in the order they stand on the old shelf.
Print the maximum possible beauty of $$$k$$$ shelves in the new office.
null
In the first example you can split the books as follows: $$$$$$(9 + 14 + 28 + 1 + 7) \& (13 + 15) \& (29 + 2) \& (31) = 24.$$$$$$ In the second example you can split the books as follows: $$$$$$(3 + 14 + 15 + 92) \& (65) \& (35 + 89) = 64.$$$$$$
[{"input": "10 4\n9 14 28 1 7 13 15 29 2 31", "output": "24"}, {"input": "7 3\n3 14 15 92 65 35 89", "output": "64"}]
1,900
["bitmasks", "dp", "greedy"]
64
[{"input": "10 4\r\n9 14 28 1 7 13 15 29 2 31\r\n", "output": "24\r\n"}, {"input": "7 3\r\n3 14 15 92 65 35 89\r\n", "output": "64\r\n"}, {"input": "40 5\r\n6 18 24 5 14 16 31 9 15 5 25 2 18 12 19 27 10 23 23 18 22 14 1 14 6 14 17 28 11 21 8 23 10 30 21 5 17 11 26 16\r\n", "output": "80\r\n"}, {"input": "20 15\r\n927353279298143 655102800384382 40376603048780 1008958973042960 1123049780860278 853122601026128 154596679092462 200013924385343 591199113039915 140875624438732 924096460433635 609326666846280 639191601375336 868486002971126 338452290857190 947205016908287 1091731324024232 315465850740682 804685495436596 1102057294815123\r\n", "output": "16777216\r\n"}, {"input": "10 4\r\n318346909478488 165408439052762 201407789817026 481299976321209 960738945073700 249445428976993 1096588610084096 605348669136305 817617728356976 256799633127974\r\n", "output": "563104572248080\r\n"}, {"input": "50 15\r\n10 30 15 11 9 5 7 16 4 3 29 28 14 20 9 10 21 16 1 7 30 19 16 25 12 29 12 5 3 23 27 16 11 25 21 18 22 12 23 16 24 27 19 7 11 12 30 1 24 11\r\n", "output": "32\r\n"}, {"input": "20 8\r\n29 25 19 16 26 18 25 9 5 2 17 16 19 16 3 16 4 17 7 20\r\n", "output": "16\r\n"}, {"input": "30 4\r\n893642632982367 772277951746295 510759449350295 826812150840579 1060893142351815 525992742241552 154832018679993 645715002371268 27232975419720 475004229372388 200177708355593 810661468479466 49618609534806 301608930846726 97042172725806 441637617418914 594900693592862 626510702147446 653604971179679 51832188158797 771139366286148 604472775724297 543106972286801 854107321252442 958411818162963 419377189839607 595529582510881 663396887427244 543023709380824 1078875721144944\r\n", "output": "3940649673949202\r\n"}, {"input": "40 5\r\n22 16 24 23 3 18 20 12 25 26 29 4 10 31 31 30 2 7 11 16 24 8 2 24 19 17 5 17 20 7 15 26 1 16 26 21 27 4 19 25\r\n", "output": "106\r\n"}, {"input": "20 9\r\n162679258761381 497029570950369 441562370676165 658883349839306 803660720071652 397337645985983 1098171846564844 578539182000384 612361670323974 773464823371908 133451479483291 1053028311550002 812258927376098 1012522114357354 692621870999109 73882870347249 783703450776904 897456103589081 217535031946368 318888756116976\r\n", "output": "571746046443520\r\n"}, {"input": "50 12\r\n22 12 31 3 3 12 19 19 21 15 24 25 31 18 9 3 8 5 3 24 6 26 30 25 14 25 9 25 3 29 9 6 11 3 12 12 15 6 1 28 28 28 26 9 15 12 17 2 18 18\r\n", "output": "36\r\n"}, {"input": "16 7\r\n1103813397013349 727264530801741 378049079598082 630901233315595 518569339136212 532452143552615 428093804691193 371529237344587 940308912730366 704551472087683 631663816743474 29306660032804 583388823624504 1109142272484691 257363549141980 1089402363164001\r\n", "output": "572020924350465\r\n"}, {"input": "30 2\r\n26 8 26 25 27 18 6 13 10 22 25 19 7 14 20 4 10 22 9 10 30 30 11 7 27 8 23 13 17 21\r\n", "output": "125\r\n"}, {"input": "10 3\r\n30 15 26 16 21 13 25 5 27 11\r\n", "output": "33\r\n"}, {"input": "10 6\r\n977831009728918 953593140925615 784863715891337 774777129376154 384491823368699 788094311512692 223966626677969 1066171741163060 119044778274639 876388748099519\r\n", "output": "562949953683460\r\n"}, {"input": "20 9\r\n2 3 15 8 17 7 23 12 30 23 4 23 16 21 11 6 7 16 3 3\r\n", "output": "16\r\n"}, {"input": "20 8\r\n483137921056907 768218083275243 207834138817392 354531452905166 963403044757413 833232873786483 582872528360258 514660235281883 1011861985723061 459485641216220 942598735621775 873490204734628 649359794076787 543622431653711 10411517577635 91729283882125 291509560140622 287260200844128 879410314598283 206118644453476\r\n", "output": "562951027164160\r\n"}, {"input": "30 9\r\n21 6 19 18 27 3 14 18 1 5 7 9 11 13 9 26 8 12 14 31 21 6 9 3 3 10 2 24 22 24\r\n", "output": "36\r\n"}, {"input": "30 22\r\n73306162994949 868677001959133 1106639997936963 879122920975808 161227935543926 760034092541884 1120594333824270 1054807665792407 476276883119188 271363692993319 440885048025850 7074457876332 636209225974410 221086579024023 1009685779703594 647498280909980 816934931718733 452863796888983 1061332951888385 942660914590384 365185997676754 112920162715865 178172288962138 810584030647354 923637556325003 1102808429103893 1118229467600088 1074261154123678 839042706865637 352292698714231\r\n", "output": "2097152\r\n"}, {"input": "23 10\r\n935459645688325 899894863244863 776172843769766 986122540485649 248016433194357 475375806620409 20513751182830 421086691387167 247543829168446 155673663632778 415875856906302 128486751653325 849753391760463 1083573552851049 244133940871958 934119204927643 1085966359617308 577156229051957 1081049229814148 935408354841926 1093470105244215 288440615891778 467185550898222\r\n", "output": "562950087639040\r\n"}, {"input": "40 5\r\n2 20 10 21 4 30 27 12 17 20 5 21 11 12 28 12 24 20 20 31 19 2 1 27 31 1 14 8 1 21 10 2 29 22 29 9 12 29 21 7\r\n", "output": "80\r\n"}, {"input": "10 4\r\n22 21 18 7 16 14 12 11 18 5\r\n", "output": "16\r\n"}, {"input": "50 24\r\n19 14 27 12 29 18 26 24 16 23 23 22 3 28 18 11 27 14 24 19 25 23 14 7 25 31 20 7 23 10 21 9 30 3 25 17 11 16 27 28 3 25 8 19 17 27 21 19 26 31\r\n", "output": "16\r\n"}, {"input": "10 4\r\n23 12 4 5 7 10 22 18 1 6\r\n", "output": "4\r\n"}, {"input": "4 2\r\n158042378809438 929800196167200 663932725437382 1056705514263205\r\n", "output": "594152991508074\r\n"}, {"input": "20 8\r\n8 29 23 29 3 24 21 4 2 8 12 17 22 8 14 15 21 18 2 22\r\n", "output": "16\r\n"}, {"input": "1 1\r\n1\r\n", "output": "1\r\n"}, {"input": "50 50\r\n1 3 7 15 31 63 127 255 511 1023 2047 4095 8191 16383 32767 65535 131071 262143 524287 1048575 2097151 4194303 8388607 16777215 33554431 67108863 134217727 268435455 536870911 1073741823 2147483647 4294967295 8589934591 17179869183 34359738367 68719476735 137438953471 274877906943 549755813887 1099511627775 2199023255551 4398046511103 8796093022207 17592186044415 35184372088831 70368744177663 140737488355327 281474976710655 562949953421311 1125899906842623\r\n", "output": "1\r\n"}, {"input": "1 1\r\n847523811295364\r\n", "output": "847523811295364\r\n"}, {"input": "16 4\r\n874317845770206 387621540079007 155991277344382 691438508026243 930771471405935 572771476513435 743983828305226 4504492981600 163201683368400 839227763533133 66330553316499 1019586586453035 361344913793137 983404846165187 1054785536664003 328380935299964\r\n", "output": "1214153029059594\r\n"}, {"input": "8 3\r\n1091780344011621 940196424994342 1122602056403610 446348279968546 844618893669060 630726534256806 583591612685392 410152590972252\r\n", "output": "633396032195072\r\n"}, {"input": "4 2\r\n162835105872970 116592879151848 139965176195127 395498533500931\r\n", "output": "392680337115137\r\n"}, {"input": "2 1\r\n209353646212060 198207100745412\r\n", "output": "407560746957472\r\n"}, {"input": "16 14\r\n104025519164115 388661629481441 867118235830760 833392737978583 446248951095413 1075527144295046 1065273356416483 60098918081731 29503418096534 995667681082455 1087347063175248 131807733936246 235096689659338 202086293947852 889771265698655 384926479287439\r\n", "output": "0\r\n"}, {"input": "8 5\r\n173728498114277 92445238671418 136142198208213 974561146026363 1099401507969061 180377338638143 458779251007513 55384960555288\r\n", "output": "140737555464192\r\n"}, {"input": "4 3\r\n266761283825161 255669386891250 772637301427796 659604966525743\r\n", "output": "19928690198528\r\n"}, {"input": "8 1\r\n562949953421312 562949953421312 562949953421312 562949953421312 562949953421312 562949953421312 562949953421312 562949953421312\r\n", "output": "4503599627370496\r\n"}, {"input": "16 1\r\n562949953421312 562949953421312 562949953421312 562949953421312 562949953421312 562949953421312 562949953421312 562949953421312 562949953421312 562949953421312 562949953421312 562949953421312 562949953421312 562949953421312 562949953421312 562949953421312\r\n", "output": "9007199254740992\r\n"}, {"input": "17 1\r\n1125899906842623 1125899906842623 1125899906842623 1125899906842623 1125899906842623 1125899906842623 1125899906842623 1125899906842623 1125899906842623 1125899906842623 1125899906842623 1125899906842623 1125899906842623 1125899906842623 1125899906842623 1125899906842623 16\r\n", "output": "18014398509481984\r\n"}]
false
stdio
null
true
464/A
464
A
PyPy 3
TESTS
30
296
9,216,000
59343169
n, p = map(int, input().split()) p += ord('a') - 1 s = [*input()] from string import ascii_lowercase ap = [*ascii_lowercase] def check(m): i = 1 z = len(m) if z > 1 and m[0] == m[1]: return False while i < z - 1: t1 = m[i] + m[i + 1] t2 = m[i - 1] + m[i] + m[i + 1] if t1 == t1[::-1] or t2 == t2[::-1]: return False i += 1 return True i = n - 1 ans = 'NO' t = s[i] while i >= 0: if ord(t) == p: i -= 1 t = s[i] continue t = chr(ord(t) + 1) m = [j for j in s] m[i] = t if check(m[:i + 1]): j = i + 1 f = True while j < n: l = 97 while l < 123: if ord(m[j - 2]) == l or ord(m[j - 1]) == l: l += 1 else: break if l == 123: f = False break m[j] = chr(l) j += 1 m[i] = t if f: print(''.join(m)) exit() print(ans)
73
46
0
155328005
def inc(c): return chr(ord(c) + 1) n, p = map(int, input().split(' ')) s = list(input()) p += 97 i = n - 1 s[i] = inc(s[i]) while -1 < i < n: if ord(s[i]) >= p: s[i] = 'a' i -= 1 s[i] = inc(s[i]) elif i > 0 and s[i] == s[i - 1] or i > 1 and s[i] == s[i - 2]: s[i] = inc(s[i]) else: i += 1 else: print(''.join(s) if i > -1 else "NO")
Codeforces Round 265 (Div. 1)
CF
2,014
1
256
No to Palindromes!
Paul hates palindromes. He assumes that string s is tolerable if each its character is one of the first p letters of the English alphabet and s doesn't contain any palindrome contiguous substring of length 2 or more. Paul has found a tolerable string s of length n. Help him find the lexicographically next tolerable string of the same length or else state that such string does not exist.
The first line contains two space-separated integers: n and p (1 ≤ n ≤ 1000; 1 ≤ p ≤ 26). The second line contains string s, consisting of n small English letters. It is guaranteed that the string is tolerable (according to the above definition).
If the lexicographically next tolerable string of the same length exists, print it. Otherwise, print "NO" (without the quotes).
null
String s is lexicographically larger (or simply larger) than string t with the same length, if there is number i, such that s1 = t1, ..., si = ti, si + 1 > ti + 1. The lexicographically next tolerable string is the lexicographically minimum tolerable string which is larger than the given one. A palindrome is a string that reads the same forward or reversed.
[{"input": "3 3\ncba", "output": "NO"}, {"input": "3 4\ncba", "output": "cbd"}, {"input": "4 4\nabcd", "output": "abda"}]
1,700
["greedy", "strings"]
73
[{"input": "3 3\r\ncba\r\n", "output": "NO\r\n"}, {"input": "3 4\r\ncba\r\n", "output": "cbd\r\n"}, {"input": "4 4\r\nabcd\r\n", "output": "abda\r\n"}, {"input": "2 2\r\nab\r\n", "output": "ba\r\n"}, {"input": "2 2\r\nba\r\n", "output": "NO\r\n"}, {"input": "1 2\r\na\r\n", "output": "b\r\n"}, {"input": "1 2\r\nb\r\n", "output": "NO\r\n"}, {"input": "1 1\r\na\r\n", "output": "NO\r\n"}, {"input": "3 4\r\ncdb\r\n", "output": "dab\r\n"}, {"input": "7 26\r\nzyxzyxz\r\n", "output": "NO\r\n"}, {"input": "10 5\r\nabcabcabca\r\n", "output": "abcabcabcd\r\n"}, {"input": "10 10\r\nfajegfaicb\r\n", "output": "fajegfaicd\r\n"}, {"input": "1 26\r\no\r\n", "output": "p\r\n"}, {"input": "1 2\r\nb\r\n", "output": "NO\r\n"}, {"input": "1 26\r\nz\r\n", "output": "NO\r\n"}, {"input": "3 3\r\ncab\r\n", "output": "cba\r\n"}, {"input": "3 26\r\nyzx\r\n", "output": "zab\r\n"}, {"input": "5 5\r\naceba\r\n", "output": "acebc\r\n"}, {"input": "10 3\r\ncbacbacbac\r\n", "output": "NO\r\n"}, {"input": "11 3\r\nabcabcabcab\r\n", "output": "acbacbacbac\r\n"}, {"input": "12 10\r\nabcabcabcabc\r\n", "output": "abcabcabcabd\r\n"}, {"input": "13 7\r\ngfegfegfegfeg\r\n", "output": "NO\r\n"}, {"input": "15 11\r\ncgjkbadjfbdaikj\r\n", "output": "cgjkbadjfbdajba\r\n"}, {"input": "17 4\r\ndabcadcbdcadbcdbc\r\n", "output": "dabcadcbdcadcabca\r\n"}, {"input": "26 26\r\nahnxdnbfcriersyzdihuecojdi\r\n", "output": "ahnxdnbfcriersyzdihuecojdk\r\n"}, {"input": "30 7\r\ncedcfedcfgcfgcbadcadgfaegfacgf\r\n", "output": "cedcfedcfgcfgcbadcadgfaegfadba\r\n"}, {"input": "70 4\r\ndcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbd\r\n", "output": "NO\r\n"}, {"input": "77 7\r\ncadgbagbcaecgfaegcdbeafbacbdfgaedgcdeabgebaecbeacgfebagedcegdafdgeacegfegfegf\r\n", "output": "cadgbagbcaecgfaegcdbeafbacbdfgaedgcdeabgebaecbeacgfebagedcegdafdgeacfabcabcab\r\n"}, {"input": "100 4\r\nabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabca\r\n", "output": "abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcd\r\n"}, {"input": "333 5\r\nedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedc\r\n", "output": "NO\r\n"}, {"input": "3 3\r\nacb\r\n", "output": "bac\r\n"}, {"input": "17 26\r\nbazyxzyxzyxzyxzyx\r\n", "output": "bcabcabcabcabcabc\r\n"}, {"input": "6 3\r\nacbacb\r\n", "output": "bacbac\r\n"}, {"input": "6 3\r\nabcabc\r\n", "output": "acbacb\r\n"}, {"input": "302 4\r\nabdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcb\r\n", "output": "acbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbac\r\n"}, {"input": "30 26\r\nabcabcabczyxzyxzyxzyxzyxzyxzyx\r\n", "output": "abcabcabdabcabcabcabcabcabcabc\r\n"}, {"input": "300 3\r\nabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc\r\n", "output": "acbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacb\r\n"}, {"input": "2 4\r\ncd\r\n", "output": "da\r\n"}]
false
stdio
null
true
747/D
747
D
Python 3
TESTS
11
311
13,721,600
23189109
n, k = [int(i) for i in input().split(' ')] tmp = [int(i) for i in input().split(' ')] winter = [(lambda x: 1 if x < 0 else 0)(t) for t in tmp] wl = sum(winter) if wl > k: print(-1) exit() d = 0 ch = 0 while d < n: if winter[d] == 0: d += 1 else: f = k - wl wn, de, db = 1, d + 1, 0 while f >= 0: if de >= n: ch += 1 print(ch) exit() if winter[de] == 0: f -= 1 else: wn += 1 db = de de += 1 if wn > 1: k -= (db - d + 1) wl -= wn d = db + 1 else: k -= 1 wl -= 1 d += 1 ch += 2 print(ch)
59
124
24,576,000
194160145
from itertools import groupby def solve(N, K, A): A = [x < 0 for x in A] # A[i] = 1: need winter # A[i] = 0: dont need winter if sum(A) > K: return -1 B = [] i = 0 while i < len(A) and A[i] == 0: i += 1 for i in range(i, len(A)): B.append(A[i]) cur_season = 0 base = last = 0 weights = [] tires = K for season, grp in groupby(B): grp = list(grp) w = len(grp) base += 1 if season == 0: # summer weights.append(w) last = w else: tires -= w last = 0 if last: weights.pop() weights.sort() # try two cases: using last and not using last tires1 = tires ans1 = base for w in weights: if tires1 >= w: tires1 -= w ans1 -= 2 tires2 = tires ans2 = base if tires2 >= last > 0: tires2 -= last ans2 -= 1 for w in weights: if tires2 >= w: tires2 -= w ans2 -= 2 return min(ans1, ans2) rlist = lambda: list(map(int, input().split())) N, K = rlist() print(solve(N, K, rlist()))
Codeforces Round 387 (Div. 2)
CF
2,016
1
256
Winter Is Coming
The winter in Berland lasts n days. For each day we know the forecast for the average air temperature that day. Vasya has a new set of winter tires which allows him to drive safely no more than k days at any average air temperature. After k days of using it (regardless of the temperature of these days) the set of winter tires wears down and cannot be used more. It is not necessary that these k days form a continuous segment of days. Before the first winter day Vasya still uses summer tires. It is possible to drive safely on summer tires any number of days when the average air temperature is non-negative. It is impossible to drive on summer tires at days when the average air temperature is negative. Vasya can change summer tires to winter tires and vice versa at the beginning of any day. Find the minimum number of times Vasya needs to change summer tires to winter tires and vice versa to drive safely during the winter. At the end of the winter the car can be with any set of tires.
The first line contains two positive integers n and k (1 ≤ n ≤ 2·105, 0 ≤ k ≤ n) — the number of winter days and the number of days winter tires can be used. It is allowed to drive on winter tires at any temperature, but no more than k days in total. The second line contains a sequence of n integers t1, t2, ..., tn ( - 20 ≤ ti ≤ 20) — the average air temperature in the i-th winter day.
Print the minimum number of times Vasya has to change summer tires to winter tires and vice versa to drive safely during all winter. If it is impossible, print -1.
null
In the first example before the first winter day Vasya should change summer tires to winter tires, use it for three days, and then change winter tires to summer tires because he can drive safely with the winter tires for just three days. Thus, the total number of tires' changes equals two. In the second example before the first winter day Vasya should change summer tires to winter tires, and then after the first winter day change winter tires to summer tires. After the second day it is necessary to change summer tires to winter tires again, and after the third day it is necessary to change winter tires to summer tires. Thus, the total number of tires' changes equals four.
[{"input": "4 3\n-5 20 -3 0", "output": "2"}, {"input": "4 2\n-5 20 -3 0", "output": "4"}, {"input": "10 6\n2 -5 1 3 0 0 -4 -3 1 0", "output": "3"}]
1,800
["dp", "greedy", "sortings"]
59
[{"input": "4 3\r\n-5 20 -3 0\r\n", "output": "2\r\n"}, {"input": "4 2\r\n-5 20 -3 0\r\n", "output": "4\r\n"}, {"input": "10 6\r\n2 -5 1 3 0 0 -4 -3 1 0\r\n", "output": "3\r\n"}, {"input": "4 4\r\n-5 20 -3 0\r\n", "output": "1\r\n"}, {"input": "4 1\r\n-5 20 -3 0\r\n", "output": "-1\r\n"}, {"input": "1 0\r\n-13\r\n", "output": "-1\r\n"}, {"input": "2 0\r\n-12 -13\r\n", "output": "-1\r\n"}, {"input": "3 1\r\n9 -16 -7\r\n", "output": "-1\r\n"}, {"input": "5 5\r\n-15 -10 -20 -19 -14\r\n", "output": "1\r\n"}, {"input": "7 3\r\n-2 -14 3 -17 -20 -13 -17\r\n", "output": "-1\r\n"}, {"input": "10 10\r\n-9 4 -3 16 -15 12 -12 8 -14 15\r\n", "output": "1\r\n"}, {"input": "30 9\r\n12 8 -20 0 11 -17 -11 -6 -2 -18 -19 -19 -18 -12 -17 8 10 -17 10 -9 7 1 -10 -11 -17 -2 -12 -9 -8 6\r\n", "output": "-1\r\n"}, {"input": "50 3\r\n6 20 17 19 15 17 3 17 5 16 20 18 9 19 18 18 2 -3 11 11 5 15 4 18 16 16 19 11 20 17 2 1 11 14 18 -8 13 17 19 9 9 20 19 20 19 5 12 19 6 9\r\n", "output": "4\r\n"}, {"input": "100 50\r\n-7 -3 9 2 16 -19 0 -10 3 -11 17 7 -7 -10 -14 -14 -7 -15 -15 -8 8 -18 -17 -5 -19 -15 -14 0 8 -3 -19 -13 -3 11 -3 -16 16 -16 -12 -2 -17 7 -16 -14 -10 0 -10 -18 -16 -11 -2 -12 -15 -8 -1 -11 -3 -17 -14 -6 -9 -15 -14 -11 -20 -20 -4 -20 -8 -2 0 -2 -20 17 -17 2 0 1 2 6 -5 -13 -16 -5 -11 0 16 -16 -4 -18 -18 -8 12 8 0 -12 -5 -7 -16 -15\r\n", "output": "-1\r\n"}, {"input": "10 10\r\n-3 -3 -3 -3 -3 -3 -3 -3 -3 -4\r\n", "output": "1\r\n"}, {"input": "10 0\r\n2 2 2 2 2 2 2 2 2 0\r\n", "output": "0\r\n"}, {"input": "10 5\r\n-3 3 -3 3 -3 3 -3 3 -3 3\r\n", "output": "10\r\n"}, {"input": "17 17\r\n-16 -19 10 -15 6 -11 -11 2 -17 -3 7 -5 -8 1 -20 -8 -11\r\n", "output": "1\r\n"}, {"input": "9 8\r\n12 20 0 19 20 14 7 17 12\r\n", "output": "0\r\n"}, {"input": "10 10\r\n-13 -9 -8 -20 -10 -12 -17 7 -15 -16\r\n", "output": "1\r\n"}, {"input": "15 15\r\n-14 -15 -8 -12 -10 -20 -14 -2 -1 2 -20 -15 5 -1 -9\r\n", "output": "1\r\n"}, {"input": "1 1\r\n11\r\n", "output": "0\r\n"}, {"input": "14 11\r\n10 12 9 12 -2 15 1 17 8 17 18 7 10 14\r\n", "output": "1\r\n"}, {"input": "1 1\r\n12\r\n", "output": "0\r\n"}, {"input": "1 1\r\n-1\r\n", "output": "1\r\n"}, {"input": "1 0\r\n1\r\n", "output": "0\r\n"}, {"input": "1 0\r\n0\r\n", "output": "0\r\n"}, {"input": "1 0\r\n-1\r\n", "output": "-1\r\n"}, {"input": "2 1\r\n-1 1\r\n", "output": "2\r\n"}, {"input": "1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "8 3\r\n14 9 10 1 2 -1 6 13\r\n", "output": "1\r\n"}, {"input": "3 3\r\n0 0 0\r\n", "output": "0\r\n"}, {"input": "11 7\r\n0 0 -1 -1 0 0 0 -1 -1 0 0\r\n", "output": "2\r\n"}, {"input": "7 5\r\n-1 1 1 1 -1 1 1\r\n", "output": "2\r\n"}, {"input": "3 3\r\n1 2 3\r\n", "output": "0\r\n"}, {"input": "5 4\r\n-1 1 1 -1 1\r\n", "output": "2\r\n"}, {"input": "3 3\r\n1 1 1\r\n", "output": "0\r\n"}, {"input": "5 4\r\n-1 0 0 -1 0\r\n", "output": "2\r\n"}]
false
stdio
null
true
79/C
79
C
Python 3
TESTS
3
124
0
21641050
#On récupère les données chaine = input() n = int(input()) borings = [input() for _ in range(n)] last_founds = [len(chaine)]*n ##borings = sorted([input() for _ in range(n)], key=lambda s : len(s)) def allOcc(chaine, s, occ): n = 0 li = len(s) prev = -1; while n != len(occ): n = len(occ) pos = chaine.find(s, prev+1) if pos != -1: occ.append((pos, li)) prev = pos return occ prev_pos = -2 def f(pos): global prev_pos; if pos[0] != prev_pos: prev_pos = pos[0] return True; return False; all_occ = [(len(chaine)-1, 2)] [allOcc(chaine, bi, all_occ) for bi in borings] all_occ.sort() all_pos = [occ for occ in all_occ if f(occ)]; prev = -1 best = 0 best_pos = 0 for pos, li in all_pos: n_len = pos +li - prev - 2 if n_len > best: best = n_len best_pos = prev+1 prev = pos ###Approche dynamique ##length, pos = 0, 0 ##for debut in range(len(chaine)-1, -1, -1): ## n_len = len(chaine) - debut ## for i, bi in enumerate(borings): ## li = len(bi) ## if chaine[debut: debut+li] == bi: ## last_founds[i] = debut; ## if n_len > length: ## long = last_founds[i] + li - 1 - debut ## n_len = min(long, n_len) ## ## if n_len > length: ## length = n_len ## pos = debut print(best, best_pos)
80
436
716,800
5639831
s, n = input(), int(input()) t = [input() for i in range(n)] def f(i): global t for j in range(n): if i < j: if len(t[j]) < len(t[i]) and t[j] in t[i]: return False elif j < i and t[j] in t[i]: return False return True t = [t[i] for i in range(n) if f(i)] n = len(s) r = [0] * n for p in t: i, m = -1, len(p) - 1 while True: i = s.find(p, i + 1) if i == -1: break r[i] += 1 r[i + m] += 2 d, j, q = 0, -1, [-1] for i in range(n): if r[i] & 1: q.append(i) if r[i] & 2: j = q.pop(0) if i - j > d: d, k = i - j, j j = q.pop(0) if n - j > d: d, k = n - j, j print(d - 1, k + 1)
Codeforces Beta Round 71
CF
2,011
2
256
Beaver
After Fox Ciel got off a bus, she found that the bus she was on was a wrong bus and she lost her way in a strange town. However, she fortunately met her friend Beaver Taro and asked which way to go to her castle. Taro's response to her was a string s, and she tried to remember the string s correctly. However, Ciel feels n strings b1, b2, ... , bn are really boring, and unfortunately she dislikes to remember a string that contains a boring substring. To make the thing worse, what she can remember is only the contiguous substring of s. Determine the longest contiguous substring of s that does not contain any boring string, so that she can remember the longest part of Taro's response.
In the first line there is a string s. The length of s will be between 1 and 105, inclusive. In the second line there is a single integer n (1 ≤ n ≤ 10). Next n lines, there is a string bi (1 ≤ i ≤ n). Each length of bi will be between 1 and 10, inclusive. Each character of the given strings will be either a English alphabet (both lowercase and uppercase) or a underscore ('_') or a digit. Assume that these strings are case-sensitive.
Output in the first line two space-separated integers len and pos: the length of the longest contiguous substring of s that does not contain any bi, and the first position of the substring (0-indexed). The position pos must be between 0 and |s| - len inclusive, where |s| is the length of string s. If there are several solutions, output any.
null
In the first sample, the solution is traight_alon. In the second sample, the solution is an empty string, so the output can be «0 0», «0 1», «0 2», and so on. In the third sample, the solution is either nagio or oisii.
[{"input": "Go_straight_along_this_street\n5\nstr\nlong\ntree\nbiginteger\nellipse", "output": "12 4"}, {"input": "IhaveNoIdea\n9\nI\nh\na\nv\ne\nN\no\nI\nd", "output": "0 0"}, {"input": "unagioisii\n2\nioi\nunagi", "output": "5 5"}]
1,800
["data structures", "dp", "greedy", "hashing", "strings", "two pointers"]
80
[{"input": "Go_straight_along_this_street\r\n5\r\nstr\r\nlong\r\ntree\r\nbiginteger\r\nellipse\r\n", "output": "12 4\r\n"}, {"input": "IhaveNoIdea\r\n9\r\nI\r\nh\r\na\r\nv\r\ne\r\nN\r\no\r\nI\r\nd\r\n", "output": "0 0\r\n"}, {"input": "unagioisii\r\n2\r\nioi\r\nunagi\r\n", "output": "5 5\r\n"}, {"input": "abcabcabcabc\r\n3\r\nabcabca\r\nbcab\r\ncabc\r\n", "output": "4 6\r\n"}, {"input": "ThankYouForParticipatingRound71\r\n6\r\nForP\r\noun\r\nnkYouForP\r\nTha\r\nouForP\r\nound7\r\n", "output": "18 9\r\n"}, {"input": "WishYourSolutionPassesFinalTests\r\n10\r\nrSoluti\r\ninal\r\nolutionPas\r\nassesFin\r\nourSo\r\nonP\r\nWishYourSo\r\nsFinalTes\r\nhYourSolut\r\nonPas\r\n", "output": "9 15\r\n"}, {"input": "9\r\n1\r\n9\r\n", "output": "0 0\r\n"}, {"input": "Z\r\n1\r\na\r\n", "output": "1 0\r\n"}, {"input": "12abcdefghijklmnop\r\n10\r\nabcd\r\nabc\r\nab\r\na\r\nklmn\r\nlmn\r\nmn\r\nn\r\nop\r\n12\r\n", "output": "12 3\r\n"}, {"input": "12abcdefghijklmnop\r\n10\r\na\r\nop\r\nab\r\nabc\r\nabcd\r\nn\r\nmn\r\n12\r\nlmn\r\nklmn\r\n", "output": "12 3\r\n"}, {"input": "12abcdefghijklmnop\r\n10\r\nlmn\r\nabc\r\na\r\nklmn\r\nn\r\nabcd\r\nop\r\nmn\r\nab\r\n12\r\n", "output": "12 3\r\n"}, {"input": "x5x5\r\n10\r\nx5\r\nx5x\r\nx5x5\r\nx5\r\nx5\r\nQyBa0yZO_c\r\n_troGKxtGQ\r\nRItOLVC3ok\r\niD_57rFYR1\r\nvZhgXwTnE4\r\n", "output": "2 1\r\n"}, {"input": "aaaay\r\n10\r\naa\r\naa\r\naaa\r\nay\r\naaay\r\ndltfBoU4nF\r\nYhImrXw62Y\r\nwswb4v7CRb\r\npjxxEE3S26\r\nwxDxW1MRaI\r\n", "output": "1 4\r\n"}, {"input": "iiiiii\r\n10\r\nii\r\niiii\r\niiiii\r\niii\r\niiii\r\n5avjcwIsDh\r\nGgiVQ9ylRz\r\newWmNAJAL9\r\nk83baq5H2U\r\nXRX3fJ4dH8\r\n", "output": "1 5\r\n"}, {"input": "ffBBfBB\r\n10\r\nBBfB\r\nffBBfB\r\nffBBf\r\nffBBf\r\nBfB\r\n1ehoxM6CU8\r\ntvB4q05iuU\r\nEYGkY6VxQO\r\nbKbE2ajjBW\r\nhqTKbQUZds\r\n", "output": "4 1\r\n"}, {"input": "aaUUUUaa\r\n10\r\naUUU\r\naaU\r\naUU\r\nUUUU\r\nUU\r\neV1R6e2tCQ\r\nmwseAsylFZ\r\njkfXnCVagR\r\nRGPm9O09J8\r\nVBRpGUFrDB\r\n", "output": "3 5\r\n"}, {"input": "1111e1e1e\r\n10\r\n11\r\n1111e\r\n1111\r\ne1e1e\r\n1e1\r\npuCrQxcVPh\r\nfnbEroh3w4\r\nyie11ihNIi\r\n2_23hx3yX9\r\noPKXwByQLT\r\n", "output": "3 6\r\n"}, {"input": "aMaMaMMaaM\r\n10\r\nMMaaM\r\nMMaaM\r\naa\r\naMaMMaa\r\nMaMa\r\nWoEVf7UuGQ\r\n2q8cm0_WfI\r\nZR63WSVBlC\r\ndtO9nmkXsc\r\ntHL2amBqOS\r\n", "output": "6 2\r\n"}, {"input": "NNNIIIIIINN\r\n10\r\nNNIIIIIIN\r\nIIIINN\r\nIIIINN\r\nNNIIIII\r\nNNII\r\nlHJxS_HfkO\r\nPsss2tjeao\r\nY4Adt_8FXb\r\nkc9mq2vWmZ\r\nVJYusUjoTq\r\n", "output": "8 2\r\n"}, {"input": "cCCcCCCcCccc\r\n10\r\ncC\r\nCCcCCCcCc\r\nCCcC\r\ncCc\r\ncCCC\r\npUKBhDtW8W\r\n0ap4vlgGej\r\nk059r28XMJ\r\nySQTZIOz3r\r\nFHn5rVpM8G\r\n", "output": "4 8\r\n"}, {"input": "7hh77hhhhhh7h\r\n10\r\nhhhh7\r\nhh77hhhhh\r\n7hhhhh\r\nhh7\r\nh77hhhhhh7\r\nC_t1uIxLWp\r\nHwH5EkVGCt\r\nyUgWASGwfR\r\nRfah5Fa12E\r\nS9CmGvTVKM\r\n", "output": "7 2\r\n"}, {"input": "3cc3\r\n10\r\ncc\r\nc3\r\n3c\r\ncc\r\ncc\r\n3cc3\r\n3cc3\r\n3cc\r\nc3\r\njLnOy3kI3M\r\n", "output": "1 3\r\n"}, {"input": "70aa70a\r\n10\r\n70\r\n0aa70\r\n0aa70\r\naa70\r\n70aa\r\n70aa\r\n70aa70a\r\naa7\r\n70aa7\r\nicHuodu1Ux\r\n", "output": "3 1\r\n"}, {"input": "YUYEYEUUEU\r\n10\r\nYEYEUUEU\r\nEUUE\r\nUU\r\nYEYEUUE\r\nYEYE\r\nUU\r\nEY\r\nEYEUU\r\nYEYEUU\r\niBXoTbQ7X3\r\n", "output": "4 0\r\n"}, {"input": "wwwwDwwwwD\r\n10\r\nwD\r\nDwww\r\nwD\r\nDwwww\r\nwwwwD\r\nDww\r\nwD\r\nwwDww\r\nwwww\r\nwwww\r\n", "output": "3 6\r\n"}, {"input": "4tg4ttgg47t44tg4ttgg47t4\r\n10\r\n47t44tg4tt\r\nttgg4\r\ng4ttgg47t4\r\ng47t44t\r\ntg4ttg\r\n44tg4ttg\r\nttgg47\r\nt44tg\r\ng47t44tg\r\n4ttgg47t4\r\n", "output": "8 5\r\n"}, {"input": "g0sg00AAA0Asggss0sAg0sg00AAA0Asggss0sA\r\n10\r\nAg0sg00AAA\r\nAsggss\r\nsAg\r\nAsggss0s\r\nggss0sAg\r\nsAg0sg\r\nggss0sA\r\n0sg00AA\r\nAAA0A\r\nAA0\r\n", "output": "8 18\r\n"}, {"input": "000gooo0g0vgovvv0oggv0v0go000gooo0g0vgovvv0oggv0v0go\r\n10\r\ngv0v0go\r\n0gooo0g\r\ngooo0g0v\r\no000\r\ngooo0g0v\r\nv0go000go\r\ngo000gooo0\r\nv0v0go00\r\nvv\r\nggv0v0\r\n", "output": "10 30\r\n"}, {"input": "B2KR\r\n10\r\nB2\r\n2KR\r\nB2KR\r\n2K\r\n2KR\r\nKR\r\n2KR\r\nB2KR\r\n2K\r\n2KR\r\n", "output": "1 3\r\n"}, {"input": "dxY_8\r\n10\r\nxY_8\r\ndxY\r\ndx\r\nY_\r\nxY_\r\ndx\r\nxY\r\ndx\r\nxY_8\r\ndxY\r\n", "output": "2 3\r\n"}, {"input": "umi4qX\r\n10\r\nqX\r\num\r\n4qX\r\nqX\r\numi4qX\r\numi4\r\numi4\r\numi4q\r\nmi4q\r\numi4q\r\n", "output": "3 2\r\n"}, {"input": "4stuNRe\r\n10\r\n4stu\r\nstuN\r\nstuNRe\r\n4stu\r\ntuNRe\r\nst\r\ntuNR\r\n4stuN\r\ntuN\r\n4stu\r\n", "output": "4 3\r\n"}, {"input": "bTnstTqbTnstTq\r\n10\r\nbTnstTq\r\nnstTqbT\r\nTqbT\r\nbTns\r\nTqbT\r\nTns\r\nTnstT\r\nTnstTqb\r\nnstT\r\nstT\r\n", "output": "4 6\r\n"}, {"input": "Oq02utSVOq02utSV\r\n10\r\n2utSVOq\r\n2utSVO\r\n02utS\r\nSVOq0\r\nut\r\nOq\r\nOq\r\nq02utSVO\r\nOq02utSV\r\nVOq0\r\n", "output": "4 9\r\n"}, {"input": "DpGGt6_wGLPDpGGt6_wGLP\r\n10\r\n6_wGL\r\nGLPDpGG\r\nt6_wG\r\nPDpGGt6_\r\nwGLPDpGGt6\r\n6_\r\n_wGL\r\npGGt6_\r\n6_wGLPD\r\n6_wGLPDpG\r\n", "output": "8 9\r\n"}, {"input": "7QTfE4ALvzkzzD4j7QTfE4ALvzkzzD4j\r\n10\r\nvzkzzD4j7\r\nE4AL\r\nTfE4ALv\r\nzzD4j7QT\r\nzkzzD4j7\r\n4AL\r\nj7Q\r\nE4ALvzkzzD\r\nvzkzzD4j\r\n4ALvzk\r\n", "output": "9 22\r\n"}, {"input": "6CLznedj88834gqTIhMTPjm_3DbkQpuYkBvU3o55h79ntRqjHTOu3WWNNGLyQSZ_o45mK5mMtRJmWUq2twqWP10OlnDAB1EP2rG\r\n10\r\n834gqTI\r\n_o4\r\nvU3o55h79n\r\nvLwlk3PY7Z\r\nk8PnkBpRYB\r\nqdkB9b_SS2\r\nkY4mBeAdgK\r\nb577cjQiSx\r\nyOFiEkP1sD\r\noYOqd8uuTt\r\n", "output": "35 64\r\n"}, {"input": "JroK3tfp941zeUovVIqCV7Sv2elf6TN8QRl8mhxQWgoQRXOLkcUjK29Ed2gBDR1TLdZeLUi9zJyBayrNlWgW0iPSG26DuJ9QK95\r\n10\r\nroK\r\novVI\r\nLUi9\r\nLUi\r\nTLd\r\nelf6\r\n9zJyBa\r\nDR1\r\ndZe\r\n8mhxQWgo\r\n", "output": "27 72\r\n"}, {"input": "p7B41t9y83IghcJG8zgWzCSATkkvQQhvqm7j_4ffUPbriGW57mbGPzqiuMEmJevfIhX2FNLFBxAGm3vLXRPOdphoWkCquDjAsW9\r\n10\r\n1t9y83\r\nBxAGm3v\r\nG8z\r\nCSATk\r\ncJG\r\nIhX2FNLF\r\nzqiuME\r\np7B4\r\nkkvQ\r\nPbriGW57m\r\n", "output": "26 73\r\n"}, {"input": "r5oW1NW2kr193KwV_eASuj9Jq0q0Kmt2EUD1R72p2nPuxNpIqi7o_KuxldYjLJmiXj93JUxRTniFzKkXxHwXDZCzC76klFnfjAQ\r\n10\r\np2nPuxN\r\n93J\r\nNpIqi7o_K\r\niXj93JUxRT\r\n1NW2kr19\r\n0Kmt2EUD1\r\nTniFzKkXxH\r\niXj93JU\r\nNW2kr19\r\n3KwV_\r\n", "output": "26 73\r\n"}, {"input": "hb\r\n1\r\nAa\r\n", "output": "2 0\r\n"}, {"input": "unagioisiiqqqqqqqqqqqq\r\n2\r\nioi\r\nunagi\r\n", "output": "17 5\r\n"}, {"input": "abababab\r\n3\r\nab\r\nba\r\na\r\n", "output": "1 7\r\n"}, {"input": "abcdaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n2\r\nabcd\r\nabcd\r\n", "output": "50 1\r\n"}, {"input": "abababababababababababababababababababababababababababababab\r\n1\r\na\r\n", "output": "1 59\r\n"}, {"input": "abc\r\n2\r\na\r\nb\r\n", "output": "1 2\r\n"}, {"input": "abcde\r\n1\r\nf\r\n", "output": "5 0\r\n"}, {"input": "ahmed\r\n5\r\nahmed\r\nhmed\r\nmed\r\ned\r\nd\r\n", "output": "4 0\r\n"}, {"input": "abc\r\n1\r\nb\r\n", "output": "1 2\r\n"}]
false
stdio
import sys def main(): input_path, output_path, submission_path = sys.argv[1:] # Read input with open(input_path, 'r') as f: s = f.readline().strip() n = int(f.readline()) forbidden = [f.readline().strip() for _ in range(n)] # Read submission output try: with open(submission_path, 'r') as f: line = f.readline().strip() parts = line.split() if len(parts) != 2: print(0) return len_sub = int(parts[0]) pos_sub = int(parts[1]) except: print(0) return # Check pos_sub and len_sub are valid if len_sub < 0 or pos_sub < 0: print(0) return s_len = len(s) if len_sub == 0: if pos_sub < 0 or pos_sub > s_len: print(0) return else: if pos_sub + len_sub > s_len: print(0) return # Check submission substring is valid valid = True sub_start = pos_sub sub_end = pos_sub + len_sub for i in range(sub_start, sub_end): for b in forbidden: m = len(b) if i + m > sub_end: continue if s[i:i+m] == b: valid = False break if not valid: break if not valid: print(0) return # Compute max_len using sliding window max_len = 0 left = 0 for right in range(len(s)): for b in forbidden: m = len(b) start = right - m + 1 if start < 0: continue if start + m > len(s): continue if s[start:start+m] == b: if left <= start: left = start + 1 current_len = right - left + 1 if current_len > max_len: max_len = current_len if len_sub != max_len: print(0) else: print(100) if __name__ == "__main__": main()
true
54/A
54
A
Python 3
TESTS
21
92
0
164941384
import sys input = sys.stdin.readline n, k = map(int, input().split()) w = list(map(int, input().split())) s = set(w[1:]) if k == 1: print(n) else: c = n for i in range(1, k+1): x = s.copy() for j in range(i, n+1, k): x.add(j) c = min(c, len(x)) print(c)
65
92
0
144867862
def nextMove(pos, holliday, k): for day in range(pos + 1, pos + k): if day in holliday: return day return pos + k days, k = [int(item) for item in input().split(' ')] holliday = [int(item) for item in input().split(' ')] n, ans, pos = holliday[0], 0, 0 del holliday[:1] while True: pos = nextMove(pos, holliday, k) if pos > days: break ans += 1 print(ans)
Codeforces Beta Round 50
CF
2,011
2
256
Presents
The Hedgehog likes to give presents to his friend, but no less he likes to receive them. Having received another present today, the Hedgehog suddenly understood that he has no place to put it as there was no room left on the special shelf in the cupboard. He will have to choose another shelf, but which one should he choose, how large should it be? In order to get to know this, the Hedgehog asks you to write him a program that will count the estimated number of presents that he will receive during the following N days. Besides, he is guided by the principle: - on each holiday day the Hedgehog will necessarily receive a present, - he receives presents at least every K days (i.e., if he received a present on the i-th day, he will receive the next present no later than on the i + K-th day). For the given N and K, as well as the list of holidays among the following N days count the minimal number of presents that could be given to the Hedgehog. The number of today's day is zero, and you should regard today's present as already given (i.e., you shouldn't count it in the answer).
The first line contains integers N and K (1 ≤ N ≤ 365, 1 ≤ K ≤ N). The second line contains a number C which represents the number of holidays (0 ≤ C ≤ N). Then in the same line follow C numbers ranging from 1 to N which are the numbers of holiday days. The numbers are given in the increasing order, without repeating numbers among them.
Print a single number — the minimal number of presents the Hedgehog will receive over the following N days.
null
null
[{"input": "5 2\n1 3", "output": "3"}, {"input": "10 1\n3 6 7 8", "output": "10"}]
1,300
["implementation"]
65
[{"input": "5 2\r\n1 3\r\n", "output": "3"}, {"input": "10 1\r\n3 6 7 8\r\n", "output": "10"}, {"input": "5 5\r\n1 3\r\n", "output": "1"}, {"input": "10 3\r\n3 3 6 9\r\n", "output": "3"}, {"input": "5 2\r\n0\r\n", "output": "2"}, {"input": "1 1\r\n0\r\n", "output": "1"}, {"input": "5 1\r\n0\r\n", "output": "5"}, {"input": "5 1\r\n1 2\r\n", "output": "5"}, {"input": "5 2\r\n0\r\n", "output": "2"}, {"input": "10 3\r\n2 4 8\r\n", "output": "4"}, {"input": "10 1\r\n0\r\n", "output": "10"}, {"input": "10 2\r\n1 5\r\n", "output": "5"}, {"input": "10 1\r\n0\r\n", "output": "10"}, {"input": "10 1\r\n0\r\n", "output": "10"}, {"input": "15 5\r\n0\r\n", "output": "3"}, {"input": "15 1\r\n1 3\r\n", "output": "15"}, {"input": "15 2\r\n1 10\r\n", "output": "7"}, {"input": "15 1\r\n0\r\n", "output": "15"}, {"input": "15 3\r\n1 11\r\n", "output": "5"}, {"input": "20 1\r\n3 7 9 20\r\n", "output": "20"}, {"input": "20 3\r\n1 11\r\n", "output": "7"}, {"input": "20 2\r\n6 6 9 10 15 19 20\r\n", "output": "12"}, {"input": "20 1\r\n0\r\n", "output": "20"}, {"input": "20 1\r\n1 13\r\n", "output": "20"}, {"input": "25 1\r\n9 2 6 8 10 14 15 17 18 23\r\n", "output": "25"}, {"input": "25 1\r\n0\r\n", "output": "25"}, {"input": "25 1\r\n4 8 10 13 24\r\n", "output": "25"}, {"input": "25 1\r\n1 14\r\n", "output": "25"}, {"input": "25 1\r\n0\r\n", "output": "25"}, {"input": "100 3\r\n0\r\n", "output": "33"}, {"input": "100 10\r\n0\r\n", "output": "10"}, {"input": "100 23\r\n22 2 9 18 22 23 30 44 50 55 58 61 70 71 73 76 79 82 85 88 94 95 99\r\n", "output": "22"}, {"input": "100 5\r\n10 2 17 21 34 52 58 60 64 68 95\r\n", "output": "24"}, {"input": "100 4\r\n2 29 63\r\n", "output": "26"}, {"input": "150 16\r\n9 19 31 47 53 57 96 105 108 120\r\n", "output": "13"}, {"input": "150 52\r\n5 11 37 60 67 86\r\n", "output": "6"}, {"input": "150 4\r\n7 21 54 106 108 109 119 123\r\n", "output": "40"}, {"input": "150 3\r\n0\r\n", "output": "50"}, {"input": "150 21\r\n21 22 26 30 36 39 52 59 62 66 68 78 86 92 96 103 108 113 118 119 125 139\r\n", "output": "22"}, {"input": "300 15\r\n14 3 38 52 57 142 157 175 201 209 238 258 288 294 299\r\n", "output": "26"}, {"input": "300 2\r\n14 29 94 122 123 158 160 164 191 200 202 208 246 272 286\r\n", "output": "153"}, {"input": "300 5\r\n16 22 38 72 78 108 116 140 147 160 189 209 214 227 252 294 300\r\n", "output": "66"}, {"input": "300 8\r\n4 27 76 155 260\r\n", "output": "40"}, {"input": "300 24\r\n20 18 76 80 81 85 103 110 112 129 145 151 172 180 184 201 205 241 257 268 276\r\n", "output": "24"}, {"input": "350 22\r\n11 38 111 115 176 194 204 207 231 274 307 348\r\n", "output": "21"}, {"input": "350 26\r\n10 13 16 81 99 144 191 223 258 316 329\r\n", "output": "18"}, {"input": "350 16\r\n12 31 76 103 116 191 201 241 256 260 291 306 336\r\n", "output": "24"}, {"input": "350 28\r\n5 23 104 135 305 331\r\n", "output": "14"}, {"input": "365 34\r\n6 80 94 208 256 325 349\r\n", "output": "14"}, {"input": "365 19\r\n7 47 114 139 210 226 266 279\r\n", "output": "22"}, {"input": "365 8\r\n32 1 13 22 25 33 72 80 86 96 117 132 145 146 156 176 177 179 188 198 203 218 225 235 253 256 267 279 286 294 303 333 363\r\n", "output": "61"}, {"input": "365 8\r\n55 3 12 26 28 36 45 47 59 61 65 82 90 103 109 114 117 121 123 126 134 142 144 146 151 154 168 175 189 193 195 197 199 210 212 214 230 232 241 248 254 267 271 291 304 306 308 311 315 317 318 334 335 346 354 365\r\n", "output": "74"}, {"input": "365 2\r\n2 96 241\r\n", "output": "183"}, {"input": "365 42\r\n10 8 66 77 148 161 183 231 301 340 350\r\n", "output": "14"}, {"input": "365 40\r\n30 1 14 21 31 32 36 56 59 68 96 119 131 137 166 179 181 202 235 248 272 294 309 315 322 327 334 341 347 362 365\r\n", "output": "30"}, {"input": "365 31\r\n19 13 18 27 33 46 58 86 114 178 187 198 228 233 240 255 277 332 348 351\r\n", "output": "22"}, {"input": "365 54\r\n21 28 42 56 65 66 67 76 81 85 89 123 132 136 153 195 215 249 294 296 300 355\r\n", "output": "22"}, {"input": "365 5\r\n5 10 31 121 235 322\r\n", "output": "74"}, {"input": "365 81\r\n2 1 75\r\n", "output": "5"}, {"input": "365 21\r\n4 1 17 344 345\r\n", "output": "19"}, {"input": "11 2\r\n5 3 6 7 9 10\r\n", "output": "7"}, {"input": "5 3\r\n2 2 4\r\n", "output": "2"}, {"input": "362 360\r\n0\r\n", "output": "1"}, {"input": "18 4\r\n4 1 9 10 18\r\n", "output": "6"}]
false
stdio
null
true
220/A
220
A
Python 3
TESTS
32
62
8,294,400
163960297
import sys class Solution(object): def __init__(self): return def solve(self): n = int(input()) arr = [int(i) for i in input().split(" ")] min_el = sys.maxsize min_ind = -1 count = 0 for i in range(0, len(arr)-1): if arr[i] > arr[i+1]: count += 1 if count > 2: return "NO" return "YES" a = Solution() print(a.solve())
96
93
13,926,400
213254577
import sys #sys.setrecursionlimit(10**7) 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())) ############ ---- Input Functions ---- ############ def Little_Elephant_and_Problem(): n = inp() sequence = inlt() sequence_sorted = sorted(sequence) s1_not_equal = [] s2_not_equal = [] for num1,num2 in zip(sequence, sequence_sorted): if num1 != num2: s1_not_equal.append(num1) s2_not_equal.append(num2) if (len(s1_not_equal) == 2 and s1_not_equal[0] == s2_not_equal[1] and s1_not_equal[1] == s2_not_equal[0]) or (len(s1_not_equal) == 0): print("YES") else: print("NO") return Little_Elephant_and_Problem()
Codeforces Round 136 (Div. 1)
CF
2,012
2
256
Little Elephant and Problem
The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself.
The first line contains a single integer n (2 ≤ n ≤ 105) — the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, — array a. Note that the elements of the array are not necessarily distinct numbers.
In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise.
null
In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO".
[{"input": "2\n1 2", "output": "YES"}, {"input": "3\n3 2 1", "output": "YES"}, {"input": "4\n4 3 2 1", "output": "NO"}]
1,300
["implementation", "sortings"]
96
[{"input": "2\r\n1 2\r\n", "output": "YES\r\n"}, {"input": "3\r\n3 2 1\r\n", "output": "YES\r\n"}, {"input": "4\r\n4 3 2 1\r\n", "output": "NO\r\n"}, {"input": "3\r\n1 3 2\r\n", "output": "YES\r\n"}, {"input": "2\r\n2 1\r\n", "output": "YES\r\n"}, {"input": "9\r\n7 7 8 8 10 10 10 10 1000000000\r\n", "output": "YES\r\n"}, {"input": "10\r\n1 2 9 4 5 6 7 8 3 10\r\n", "output": "YES\r\n"}, {"input": "4\r\n2 2 2 1\r\n", "output": "YES\r\n"}, {"input": "10\r\n1 2 4 4 4 5 5 7 7 10\r\n", "output": "YES\r\n"}, {"input": "10\r\n4 5 11 12 13 14 16 16 16 18\r\n", "output": "YES\r\n"}, {"input": "20\r\n38205814 119727790 127848638 189351562 742927936 284688399 318826601 326499046 387938139 395996609 494453625 551393005 561264192 573569187 600766727 606718722 730549586 261502770 751513115 943272321\r\n", "output": "YES\r\n"}, {"input": "47\r\n6 277 329 393 410 432 434 505 529 545 650 896 949 1053 1543 1554 1599 1648 1927 1976 1998 2141 2248 2384 2542 2638 2995 3155 3216 3355 3409 3597 3851 3940 4169 4176 4378 4378 4425 4490 4627 4986 5025 5033 5374 5453 5644\r\n", "output": "YES\r\n"}, {"input": "50\r\n6 7 8 4 10 3 2 7 1 3 10 3 4 7 2 3 7 4 10 6 8 10 9 6 5 10 9 6 1 8 9 4 3 7 3 10 5 3 10 1 6 10 6 7 10 7 1 5 9 5\r\n", "output": "NO\r\n"}, {"input": "100\r\n3 7 7 8 15 25 26 31 37 41 43 43 46 64 65 82 94 102 102 103 107 124 125 131 140 145 146 150 151 160 160 161 162 165 169 175 182 191 201 211 214 216 218 304 224 229 236 241 244 249 252 269 270 271 273 289 285 295 222 307 312 317 319 319 320 321 325 330 340 341 345 347 354 356 366 366 375 376 380 383 386 398 401 407 414 417 423 426 431 438 440 444 446 454 457 458 458 466 466 472\r\n", "output": "NO\r\n"}, {"input": "128\r\n1 2 4 6 8 17 20 20 23 33 43 49 49 49 52 73 74 75 82 84 85 87 90 91 102 103 104 105 111 111 401 142 142 152 155 160 175 176 178 181 183 184 187 188 191 193 326 202 202 214 224 225 236 239 240 243 246 247 249 249 257 257 261 264 265 271 277 281 284 284 286 289 290 296 297 303 305 307 307 317 318 320 322 200 332 342 393 349 350 350 369 375 381 381 385 385 387 393 347 397 398 115 402 407 407 408 410 411 411 416 423 426 429 429 430 440 447 449 463 464 466 471 473 480 480 483 497 503\r\n", "output": "NO\r\n"}, {"input": "4\r\n5 12 12 6\r\n", "output": "YES\r\n"}, {"input": "5\r\n1 3 3 3 2\r\n", "output": "YES\r\n"}, {"input": "4\r\n2 1 1 1\r\n", "output": "YES\r\n"}, {"input": "2\r\n1 1\r\n", "output": "YES\r\n"}, {"input": "4\r\n1000000000 1 1000000000 1\r\n", "output": "YES\r\n"}, {"input": "11\r\n2 2 2 2 2 2 2 2 2 2 1\r\n", "output": "YES\r\n"}, {"input": "6\r\n1 2 3 4 5 3\r\n", "output": "NO\r\n"}, {"input": "9\r\n3 3 3 2 2 2 1 1 1\r\n", "output": "NO\r\n"}, {"input": "4\r\n4 1 2 3\r\n", "output": "NO\r\n"}, {"input": "6\r\n3 4 5 6 7 2\r\n", "output": "NO\r\n"}, {"input": "4\r\n4 2 1 3\r\n", "output": "NO\r\n"}, {"input": "4\r\n3 3 2 2\r\n", "output": "NO\r\n"}, {"input": "4\r\n3 2 1 1\r\n", "output": "NO\r\n"}, {"input": "4\r\n4 5 1 1\r\n", "output": "NO\r\n"}, {"input": "6\r\n1 6 2 4 3 5\r\n", "output": "NO\r\n"}, {"input": "5\r\n1 4 5 2 3\r\n", "output": "NO\r\n"}, {"input": "4\r\n2 2 1 1\r\n", "output": "NO\r\n"}, {"input": "5\r\n1 4 3 2 1\r\n", "output": "NO\r\n"}, {"input": "5\r\n1 4 2 2 3\r\n", "output": "NO\r\n"}, {"input": "6\r\n1 2 3 1 2 3\r\n", "output": "NO\r\n"}, {"input": "3\r\n3 1 2\r\n", "output": "NO\r\n"}, {"input": "5\r\n5 1 2 3 4\r\n", "output": "NO\r\n"}, {"input": "5\r\n3 3 3 2 2\r\n", "output": "NO\r\n"}, {"input": "5\r\n100 5 6 10 7\r\n", "output": "NO\r\n"}, {"input": "3\r\n2 3 1\r\n", "output": "NO\r\n"}, {"input": "5\r\n4 4 1 1 1\r\n", "output": "NO\r\n"}, {"input": "5\r\n1 2 5 3 4\r\n", "output": "NO\r\n"}, {"input": "4\r\n3 4 1 2\r\n", "output": "NO\r\n"}, {"input": "4\r\n2 4 1 5\r\n", "output": "NO\r\n"}, {"input": "5\r\n1 3 3 2 2\r\n", "output": "NO\r\n"}, {"input": "5\r\n1 5 4 4 4\r\n", "output": "YES\r\n"}, {"input": "7\r\n3 2 1 2 3 5 4\r\n", "output": "NO\r\n"}, {"input": "5\r\n1 1 3 2 2\r\n", "output": "YES\r\n"}, {"input": "9\r\n1 8 7 7 7 7 7 8 3\r\n", "output": "YES\r\n"}, {"input": "5\r\n1 3 2 3 3\r\n", "output": "YES\r\n"}, {"input": "10\r\n4 4 4 4 10 4 4 4 4 4\r\n", "output": "YES\r\n"}, {"input": "8\r\n3 6 6 6 6 6 4 9\r\n", "output": "YES\r\n"}, {"input": "4\r\n4 4 3 3\r\n", "output": "NO\r\n"}, {"input": "4\r\n3 2 2 4\r\n", "output": "YES\r\n"}, {"input": "5\r\n2 2 1 3 3\r\n", "output": "YES\r\n"}, {"input": "5\r\n1 2 7 3 5\r\n", "output": "NO\r\n"}, {"input": "5\r\n2 3 4 5 1\r\n", "output": "NO\r\n"}, {"input": "6\r\n1 4 3 6 2 5\r\n", "output": "NO\r\n"}, {"input": "5\r\n3 3 1 5 4\r\n", "output": "NO\r\n"}, {"input": "4\r\n1 2 1 2\r\n", "output": "YES\r\n"}, {"input": "6\r\n4 5 3 4 2 6\r\n", "output": "NO\r\n"}, {"input": "11\r\n1 2 3 4 5 1 2 3 4 5 1\r\n", "output": "NO\r\n"}, {"input": "6\r\n6 1 2 3 4 5\r\n", "output": "NO\r\n"}, {"input": "5\r\n4 1 1 1 1\r\n", "output": "YES\r\n"}, {"input": "9\r\n1 2 3 5 4 6 7 8 9\r\n", "output": "YES\r\n"}, {"input": "6\r\n6 1 2 3 4 2\r\n", "output": "NO\r\n"}, {"input": "6\r\n2 2 2 2 3 2\r\n", "output": "YES\r\n"}, {"input": "3\r\n2 1 1\r\n", "output": "YES\r\n"}, {"input": "5\r\n1 2 1 1 2\r\n", "output": "YES\r\n"}, {"input": "5\r\n1 2 2 1 2\r\n", "output": "YES\r\n"}, {"input": "8\r\n5 5 5 5 5 5 1 1\r\n", "output": "NO\r\n"}]
false
stdio
null
true
215/A
215
A
Python 3
TESTS
24
154
6,963,200
128784625
def entrada(): input() a = input().split(" ") input() b = input().split(" ") for i in range(len(a)): a[i] = int(a[i]) for i in range(len(b)): b[i] = int(b[i]) return a, b def main(): a, b = entrada() max = 0 count = 0 i = 0 j = len(b) - 1 if (1 in a): print(1) return while (True): if b[j] % a[i] == 0: if max < b[j]/a[i]: max = b[j]/a[i] count = 1 j += -1 i = 0 elif max == b[j]/a[i]: count += 1 j += -1 i = 0 else: break else: i += 1 if (i == len(a)): i = 0 j += -1 if (j == -1): break print(count) return if __name__ == "__main__": main()
57
62
0
206952272
""" Logic 1. Take inputs. 2. iterate 2 for loops and divide all b's by all a's to see the available gears. 3. add the gears to the list, find out the max element. 4. find the count of the max element and print to the user. """ a_n = int(input()) a_lst = list(map(int, input().rstrip().split())) b_n = int(input()) b_lst = list(map(int, input().rstrip().split())) gear_lst = [] for i in a_lst: for j in b_lst: if j / i == j // i: gear_lst.append(j // i) max_gear_lst = max(gear_lst) print(gear_lst.count(max_gear_lst))
Codeforces Round 132 (Div. 2)
CF
2,012
2
256
Bicycle Chain
Vasya's bicycle chain drive consists of two parts: n stars are attached to the pedal axle, m stars are attached to the rear wheel axle. The chain helps to rotate the rear wheel by transmitting the pedal rotation. We know that the i-th star on the pedal axle has ai (0 < a1 < a2 < ... < an) teeth, and the j-th star on the rear wheel axle has bj (0 < b1 < b2 < ... < bm) teeth. Any pair (i, j) (1 ≤ i ≤ n; 1 ≤ j ≤ m) is called a gear and sets the indexes of stars to which the chain is currently attached. Gear (i, j) has a gear ratio, equal to the value $$\frac{b_{j}}{a_{j}}$$. Since Vasya likes integers, he wants to find such gears (i, j), that their ratios are integers. On the other hand, Vasya likes fast driving, so among all "integer" gears (i, j) he wants to choose a gear with the maximum ratio. Help him to find the number of such gears. In the problem, fraction $$\frac{b_{j}}{a_{j}}$$ denotes division in real numbers, that is, no rounding is performed.
The first input line contains integer n (1 ≤ n ≤ 50) — the number of stars on the bicycle's pedal axle. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 104) in the order of strict increasing. The third input line contains integer m (1 ≤ m ≤ 50) — the number of stars on the rear wheel axle. The fourth line contains m integers b1, b2, ..., bm (1 ≤ bi ≤ 104) in the order of strict increasing. It is guaranteed that there exists at least one gear (i, j), that its gear ratio is an integer. The numbers on the lines are separated by spaces.
Print the number of "integer" gears with the maximum ratio among all "integer" gears.
null
In the first sample the maximum "integer" gear ratio equals 3. There are two gears that have such gear ratio. For one of them a1 = 4, b1 = 12, and for the other a2 = 5, b3 = 15.
[{"input": "2\n4 5\n3\n12 13 15", "output": "2"}, {"input": "4\n1 2 3 4\n5\n10 11 12 13 14", "output": "1"}]
900
["brute force", "implementation"]
57
[{"input": "2\r\n4 5\r\n3\r\n12 13 15\r\n", "output": "2\r\n"}, {"input": "4\r\n1 2 3 4\r\n5\r\n10 11 12 13 14\r\n", "output": "1\r\n"}, {"input": "1\r\n1\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "2\r\n1 2\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n1\r\n2\r\n1 2\r\n", "output": "1\r\n"}, {"input": "4\r\n3 7 11 13\r\n4\r\n51 119 187 221\r\n", "output": "4\r\n"}, {"input": "4\r\n2 3 4 5\r\n3\r\n1 2 3\r\n", "output": "2\r\n"}, {"input": "10\r\n6 12 13 20 48 53 74 92 96 97\r\n10\r\n1 21 32 36 47 54 69 75 95 97\r\n", "output": "1\r\n"}, {"input": "10\r\n5 9 10 14 15 17 19 22 24 26\r\n10\r\n2 11 17 19 21 22 24 25 27 28\r\n", "output": "1\r\n"}, {"input": "10\r\n24 53 56 126 354 432 442 740 795 856\r\n10\r\n273 438 494 619 689 711 894 947 954 958\r\n", "output": "1\r\n"}, {"input": "10\r\n3 4 6 7 8 10 14 16 19 20\r\n10\r\n3 4 5 7 8 10 15 16 18 20\r\n", "output": "1\r\n"}, {"input": "10\r\n1 6 8 14 15 17 25 27 34 39\r\n10\r\n1 8 16 17 19 22 32 39 44 50\r\n", "output": "1\r\n"}, {"input": "10\r\n5 21 22 23 25 32 35 36 38 39\r\n10\r\n3 7 8 9 18 21 23 24 36 38\r\n", "output": "4\r\n"}, {"input": "10\r\n17 239 443 467 661 1069 1823 2333 3767 4201\r\n20\r\n51 83 97 457 593 717 997 1329 1401 1459 1471 1983 2371 2539 3207 3251 3329 5469 6637 6999\r\n", "output": "8\r\n"}, {"input": "20\r\n179 359 401 467 521 601 919 941 1103 1279 1709 1913 1949 2003 2099 2143 2179 2213 2399 4673\r\n20\r\n151 181 191 251 421 967 1109 1181 1249 1447 1471 1553 1619 2327 2551 2791 3049 3727 6071 7813\r\n", "output": "3\r\n"}, {"input": "20\r\n79 113 151 709 809 983 1291 1399 1409 1429 2377 2659 2671 2897 3217 3511 3557 3797 3823 4363\r\n10\r\n19 101 659 797 1027 1963 2129 2971 3299 9217\r\n", "output": "3\r\n"}, {"input": "30\r\n19 47 109 179 307 331 389 401 461 509 547 569 617 853 883 1249 1361 1381 1511 1723 1741 1783 2459 2531 2621 3533 3821 4091 5557 6217\r\n20\r\n401 443 563 941 967 997 1535 1567 1655 1747 1787 1945 1999 2251 2305 2543 2735 4415 6245 7555\r\n", "output": "8\r\n"}, {"input": "5\r\n33 78 146 3055 4268\r\n5\r\n2211 2584 5226 9402 9782\r\n", "output": "3\r\n"}, {"input": "5\r\n35 48 52 86 8001\r\n10\r\n332 3430 3554 4704 4860 5096 6215 7583 8228 8428\r\n", "output": "4\r\n"}, {"input": "10\r\n97 184 207 228 269 2084 4450 6396 7214 9457\r\n16\r\n338 1179 1284 1545 1570 2444 3167 3395 3397 5550 6440 7245 7804 7980 9415 9959\r\n", "output": "5\r\n"}, {"input": "30\r\n25 30 41 57 58 62 70 72 76 79 84 85 88 91 98 101 104 109 119 129 136 139 148 151 926 1372 3093 3936 5423 7350\r\n25\r\n1600 1920 2624 3648 3712 3968 4480 4608 4864 5056 5376 5440 5632 5824 6272 6464 6656 6934 6976 7616 8256 8704 8896 9472 9664\r\n", "output": "24\r\n"}, {"input": "5\r\n33 78 146 3055 4268\r\n5\r\n2211 2584 5226 9402 9782\r\n", "output": "3\r\n"}, {"input": "5\r\n35 48 52 86 8001\r\n10\r\n332 3430 3554 4704 4860 5096 6215 7583 8228 8428\r\n", "output": "4\r\n"}, {"input": "10\r\n97 184 207 228 269 2084 4450 6396 7214 9457\r\n16\r\n338 1179 1284 1545 1570 2444 3167 3395 3397 5550 6440 7245 7804 7980 9415 9959\r\n", "output": "5\r\n"}, {"input": "30\r\n25 30 41 57 58 62 70 72 76 79 84 85 88 91 98 101 104 109 119 129 136 139 148 151 926 1372 3093 3936 5423 7350\r\n25\r\n1600 1920 2624 3648 3712 3968 4480 4608 4864 5056 5376 5440 5632 5824 6272 6464 6656 6934 6976 7616 8256 8704 8896 9472 9664\r\n", "output": "24\r\n"}, {"input": "50\r\n51 67 75 186 194 355 512 561 720 876 1077 1221 1503 1820 2153 2385 2568 2608 2937 2969 3271 3311 3481 4081 4093 4171 4255 4256 4829 5020 5192 5636 5817 6156 6712 6717 7153 7436 7608 7612 7866 7988 8264 8293 8867 9311 9879 9882 9889 9908\r\n1\r\n5394\r\n", "output": "1\r\n"}, {"input": "3\r\n3 4 5\r\n3\r\n6 20 25\r\n", "output": "2\r\n"}, {"input": "4\r\n2 3 5 8\r\n4\r\n2 6 8 10\r\n", "output": "1\r\n"}, {"input": "4\r\n3 5 7 11\r\n4\r\n3 5 7 22\r\n", "output": "1\r\n"}, {"input": "2\r\n2 3\r\n3\r\n20 30 50\r\n", "output": "1\r\n"}, {"input": "3\r\n1 2 3\r\n4\r\n2 4 6 49\r\n", "output": "1\r\n"}, {"input": "2\r\n4 5\r\n3\r\n12 15 20\r\n", "output": "1\r\n"}, {"input": "3\r\n2 5 7\r\n3\r\n4 5 7\r\n", "output": "1\r\n"}, {"input": "3\r\n3 5 8\r\n3\r\n6 8 10\r\n", "output": "2\r\n"}, {"input": "2\r\n2 3\r\n4\r\n4 6 9 33\r\n", "output": "1\r\n"}, {"input": "2\r\n2 3\r\n4\r\n4 6 21 40\r\n", "output": "1\r\n"}, {"input": "3\r\n4 9 10\r\n3\r\n8 9 10\r\n", "output": "1\r\n"}, {"input": "5\r\n1 5 6 9 51\r\n5\r\n5 12 18 27 10000\r\n", "output": "1\r\n"}, {"input": "13\r\n1 2 3 4 5 6 7 8 9 10 11 12 13\r\n1\r\n14\r\n", "output": "1\r\n"}]
false
stdio
null
true
326/B
335
B
Python 3
TESTS
3
1,560
13,516,800
4229023
s = input() dp = [['' for i in range(0, len(s))] for i in range(0, len(s))] found = [[False for i in range(0, len(s))] for i in range(0, len(s))] def L(a, b): if found[a][b]: return dp[a][b] if a==b: dp[a][b] = s[a] elif a + 1 == b: if s[a] == s[b]: dp[a][b] = s[a] + s[b] else: dp[a][b] = s[a] else: if s[a] == s[b]: dp[a][b] = s[a] + L(a+1, b-1) + s[b] else: optionOne = L(a+1, b) optionTwo = L(a, b-1) dp[a][b] = optionOne if len(optionTwo) > len(optionOne): dp[a][b] = optionTwo found[a][b] = True return dp[a][b] for c in 'abcdefghijklmnopqrstuvwxyz': if s.count(c) >= 100: print(c*100) exit() print(L(0, len(s)-1))
72
1,996
117,145,600
66242753
from collections import defaultdict s = input() n = len(s) if n >= 2600: print(s[0] * 100) elif n > 2574: count = defaultdict(int) for l in s: count[l] += 1 max_l = s[0] for l, c in count.items(): if c >= 100: max_l = l print(max_l * 100) else: data = defaultdict(dict) def print_p(i, gap): if gap == 1: return s[i] if gap == 2: return s[i:i + 2] return s[i] + print_p(data[i + 1][gap - 2][1], data[i + 1][gap - 2][2]) + s[i] for i in range(n): data[i][1] = (1, i, 1) max_p = data[0][1] for i in range(n - 1): if s[i] == s[i + 1]: data[i][2] = (2, i, 2) max_p = data[i][2] else: data[i][2] = data[i][1] output = None for gap in range(3, n + 1): for i in range(n - gap + 1): j = i + gap - 1 if s[i] == s[j]: size = data[i + 1][gap - 2][0] + 2 data[i][gap] = (size, i, gap) if size > max_p[0]: max_p = data[i][gap] if size == 100: output = print_p(i, gap) if size == 101: output = print_p(i, gap) output = output[:50] + output[51:] else: if data[i][gap - 1][0] > data[i + 1][gap - 1][0]: data[i][gap] = data[i][gap - 1] else: data[i][gap] = data[i + 1][gap - 1] if output: print(output) else: print(print_p(max_p[1], max_p[2]))
MemSQL start[c]up Round 2
CF
2,013
2
256
Palindrome
Given a string s, determine if it contains any palindrome of length exactly 100 as a subsequence. If it has any, print any one of them. If it doesn't have any, print a palindrome that is a subsequence of s and is as long as possible.
The only line of the input contains one string s of length n (1 ≤ n ≤ 5·104) containing only lowercase English letters.
If s contains a palindrome of length exactly 100 as a subsequence, print any palindrome of length 100 which is a subsequence of s. If s doesn't contain any palindromes of length exactly 100, print a palindrome that is a subsequence of s and is as long as possible. If there exists multiple answers, you are allowed to print any of them.
null
A subsequence of a string is a string that can be derived from it by deleting some characters without changing the order of the remaining characters. A palindrome is a string that reads the same forward or backward.
[{"input": "bbbabcbbb", "output": "bbbcbbb"}, {"input": "rquwmzexectvnbanemsmdufrg", "output": "rumenanemur"}]
1,900
[]
72
[{"input": "bbbabcbbb\r\n", "output": "bbbcbbb\r\n"}, {"input": "rquwmzexectvnbanemsmdufrg\r\n", "output": "rumenanemur\r\n"}, {"input": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n", "output": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n"}, {"input": "ab\r\n", "output": "a\r\n"}, {"input": "abacaba\r\n", "output": "abacaba\r\n"}, {"input": "startcup\r\n", "output": "trt\r\n"}, {"input": "aabccb\r\n", "output": "bccb\r\n"}, {"input": "abbacc\r\n", "output": "abba\r\n"}, {"input": "iwlauggytlpahjhteurdyoaulbnnhashwktxlrhpgqnypnuitmfhhbkwysjuhljshaanowhngeaumdovrlvuofzsbhhzdyngntdu\r\n", "output": "ugyhhurounhashwkhnpnhkwhsahnuoruhhygu\r\n"}, {"input": "zypzepzqni\r\n", "output": "zpepz\r\n"}, {"input": "a\r\n", "output": "a\r\n"}, {"input": "oliifeuoyzhkootktyulrxshmboejshoguwgufxpuloouoojovufukdokoeyouzihyaeuucutypfxictojtnfoouoniuyuvkrglsueihpuxsulrrifyuwuyolutotzozsvhoyjxuguecywbfuuqmpzooojergudvwucsocoojfplaulvfpcooxxublcfpguuoouooouo\r\n", "output": "ouooouoougpluoovufudooozuucuxjoouoyurlsupuslruyouoojxucuuzooodufuvooulpguoouooouo\r\n"}, {"input": "kyzxnaiwjgnotlekhycrnapekfcfxydbvnjboevgdzgigxvijgwrqnacumglzlxkcurmqmxzxxbuxlwruycdhzzdmtvobmvylyibyynjvonmbwvwqzmidfhnndecrwyfxjxwquiubrimmcwoeecotwnkfrojwuxmficzurwaqljfvdcvixcictxfzztzzbvbdypayhay\r\n", "output": "yahyapydbvbzixijqauzcurmmbuxwrcdhdmvbmvyybyyvmbvmdhdcrwxubmmruczuaqjixizbvbdypayhay\r\n"}, {"input": "carfnnnuxxbntssoxafxxbbxnonpunapjxtxexlptcwqxkjnxaanntriyunvnoxkdxnsxjxxoixyplkvxctuoenxxjixxgyxlgnxhklxqxqzxafxcnealxxwspbpcfgpnnovwrnnaxjcjpnierselogxxcxrxnlpnkbzjnqvrkurhxaxyjxxnxxnpipuntongeuusujf\r\n", "output": "fnnuxxnxxxxnnpnxxxlpcjxannrvoxxnxxxxlkxnxxixxnxklxxxxnxxovrnnaxjcplxxxnpnnxxxxnxxunnf\r\n"}, {"input": "yggmkggmvkfmmsvijnyvwgswdpwkwvmcmmswksmhwwmmgwzgwkkwogwiglwggwwnmwgkqggkugwmfwawxggrwgwclgwclknltxrkjwogwejgeymtmwziwmskrwsmfmmiwkwwvsgwsdmmkfmggkmpgg\r\n", "output": "ggmkggmfmmswgswwkwmmmswksmwwmmgwgwkkwgwgwggwwmwgkggkgwmwwggwgwgwkkwgwgmmwwmskwsmmmwkwwsgwsmmfmggkmgg\r\n"}, {"input": "mstmytylsulbuhumamahahbbmmtttmlulhamyhlmbulyylubmlhymahlulmtttmmbbhahamamuhubluslytymtsmxp\r\n", "output": "mstmytylsulbuhumamahahbbmmtttmlulhamyhlmbulyylubmlhymahlulmtttmmbbhahamamuhubluslytymtsm\r\n"}, {"input": "ouwvwggffgoowjzkzeggbzieagwwznzzzlwvzswfznvozveezgopefecoezfsofqzqfoovaofwzgefzzweggvztzvofgevivgevwwslhofzwopopzzgfvwzeogovvgzdzafgwzshovvrwwwgmooczezivfwgybgezogsmgfgwwzevvwgeehwvegfdzgzwgzoffgteztwvgwfvogeggfogkeovxzzlzzwzzlvifgxzwvrogeeeggeoefhhzoweerwvxofgvwovozwvizofzogozgwevwllexooktoggvoeowgtezffzfdohvgmofofvwzzofwvwsfbwyzzziofvfcofmzgrofzozzgghseafefdpwwwogpzzfowfhlsoeitfogeezfagofqqesocewfpwveozeenwsvffzwozwzlwoeffzonveaivgfebvegveozzfoowzwzkwezjeeuwzgezoovwwgzgzggwzowzevwfgggoozfozfwg\r\n", "output": "gzoggwveoggvoegezfzovgofowzzowvswzovfcofzgofosfwozzowfsofogzfocfvozwsvwozzwofogvozfzegeovggoevwggozg\r\n"}, {"input": "gamjuklsvzwddaocadujdmvlenyyvlflipneqofeyipmtunbdmbdyhkovnpdetueeiunsipowrhxrhtastjniqdhmehcumdcrghewljvpikcraoouhfwtnoaukbnykjapkvyakdnckkypargamvnsqtchesbmuffqqycnjvolmtpjfykvkeexkpdxjexrvdzpcbthhkxuucermkaebrvcxoovidpqnpkgbhiatyjvumihptrigqxsemqbbxwmyunmmayubqqjaioqmzyekhtqgoockiskyqihopmkastfvqiewtbtbriuyuszlndcweuhnywlkjgerqokxsxfxeaxcuwoocoonstwlxujrynkwzshpretbhlvkxyebnhafxfelpmqfkksurrfqeaykdxihtyqpaiftigdwkraxxzxkqicnfxlxhxwtkkknurzubtkivzpmlfebzduezuqeyequvyrighfzyldxenwxokumxtiieeeuku\r\n", "output": "uuemkexdhiyvuqequuzefvitbuzunkkxxfxxxrkwpthxyaeqfrrfqeayxhtpwkrxxxfxxkknuzubtivfezuuqequvyihdxekmeuu\r\n"}, {"input": "qpjnbjhmigtgtxolifwoyatdlqtejqovaslcgjufverrnkqxtsrrytqgtuikcseggjnltpggcpjizojthwycibvnvxetibpicujwmyzcojhpftttwvtlxaeefbvbvygouinnjrwuczlplbzahqciptrlrcuamyntvrzqxrnkrczmluocsuthwaptenwysoviwwcnrljuskynomlpslqyjcauagiveunrzognltohqxggprgreezjsqchcgihzbrleuwgnlsqeenybrbsqcnnlbipajlcfmajtchblnxsnegooewupmvufzbipnyjneuwelibvhoghtqpvqjehvpbiclalyzxzqwekemnsjabyzatjgzbrheubuzrcgtxvaokzapejesjntgnriupahoeousszcqprljhhgnqclbsuvvgfudhwmabfbyqjcqqgnnoocqxbyjpmvncmcieavcstjvvzgtbgcjbqnqbpueqlgibtvjpzsan\r\n", "output": "nspjvglqeqcgbgsenybqcnncjbwuvublghpqehpinsjazatgruurgtazajsnipheqphglbuvuwbjcnncqbynesgbgcqeqlgvjpsn\r\n"}, {"input": "nwgwxgxlbgbgnvqowqqocgwwnbnoqghxwxbbonxxgongqwbbxxbbwiqgnogxxnobmbxwxlgqonbnwhewgoqqwoqngbgbxgxwgwna\r\n", "output": "nwgwxgxbgbgnqowqqogwwnbnoqgxwxbbonxxgongqwbbxxbbwqgnogxxnobbxwxgqonbnwwgoqqwoqngbgbxgxwgwn\r\n"}, {"input": "vtaavlavalbvbbbccccddddeeeefltfffgvgvgtghlhhhiviiijjjjkkkkmmmmnnnlnooooppppqqvqqrrrrssssluuuvuwwwwtv\r\n", "output": "vtvlvlvltgvgvgtlvlvlvtv\r\n"}, {"input": "iuaiubcide\r\n", "output": "iuiui\r\n"}, {"input": "aavaaaadbbbddbbdbccccwvcceveeeedeffvdfvfffdggggvwgghhdhdwdhhhwiiwiiiiwjjjjjjkkkkdkklwvlllllmmmmmmvdnndnwndnndooowoooppppppwqwqdwqwqwdqqrdrwdrrrrsdssssvsttttttuvuuuwuuxxxdwwxwxdxyyyywyddwvdvdvdvvwddddv\r\n", "output": "vddddwvvdvdvdvwddwdwwwdwvvddwdqqdwqwqwdqqdwddvvwdwwwdwddwvdvdvdvvwddddv\r\n"}, {"input": "ibbbbiabbibbscccocccccdsdyddiddishddffjifhfffjffgggeggeggjgkkkjkkjkkklllaellllhlmymmmmssmmomnnanojennosasoennnjpopaopppspsyphepqaqqqjqqjqqrrjerrerrrrttttttttuuuujuhuiuuwwjwhswwwiwwxixxxyxsosixxxaizzzi\r\n", "output": "iiaisosyiishjihjeejjjaehyssoaojnnosasonnjoaossyheajjjeejhijhsiiysosiaii\r\n"}, {"input": "ataaaamabbbbtmbbmtmtccctcttcmmcmtdmmmmmddddtdmeetmtmmeteteeftffmfffgmmggggmgmmhmhhhmhhiimtiiititmmjjjtjjjkkmtmtkmkmkklllmllmmtmlnmnnmnnnootooooptpppttppqmqqtqmqqmtmrrrtrrrssssssuuuuuuvvvvvvwwwwwwxxxxx\r\n", "output": "tmtmmtmttttmmmtmmmmmtmtmtmmtttmmmgggggmmmtttmmtmtmtmmmmmtmmmttttmtmmtmt\r\n"}, {"input": "agqdhdgqgannagbqbbhddddcgngchnhdceeqefqhgfdngfdhiiidhjgqjjndqkgqnkkgqqndlldlqqmddmmnqoqgnoqgqdopnqghdhdgpndpghqrrqdhrsnhgnssgddddhtttqgnnguqgudhuvvdqg\r\n", "output": "gqdhdgqgnngqhddddgnghnhdqqhgdngdhdhgqndqgqngqqnddqqddnqqgnqgqdnqghdhdgndghqqdhnhgngddddhqgnngqgdhdqg\r\n"}, {"input": "hyhwkyvfpfpykwhchycwcpfppcuvuvupshcwwkwucpkppkpcuwkwwchspuvuvaucppfbpcwcyhchwkypfpfvykwhyh\r\n", "output": "hyhwkyvfpfpykwhchycwcpfppcuvuvupshcwwkwucpkppkpcuwkwwchspuvuvucppfpcwcyhchwkypfpfvykwhyh\r\n"}, {"input": "dpddpdddddidtddudddddqddddddddddddddddddddddcdddddddddddddddkkdddddgdddddddddjkdovfvvgnnvvvvvvvvvvnvvvvvkvvvvfnvuuvevvvfvvvpvvvkvvvvvvvvvlvvvifvvvvklvbvvovovvvvkvivnvvvvvvulvvvwwmjwtwwbwlwwwwwwwwwwpewewwwwwpwwwwwwwwwwwwwwwwwwlwbtwwwwjwmwwwwwlwwuwnwwwwiwwkwwwwwwwwxxxxxxoxxxoxxxbxxxxlxxxxxxxxxxxxxxxxkxxxxfixlxxxxxkpxfxxxxxxxxxxxxxexxxuuxxxxxxxxxyynyyyyyyyyyyfkyynynyynyyyyyyygyyyyyyyyyyyyyfyyoyyyyyyykyyyyyyjyyyyyyygykyykyyycyyqyzzzzzzzzzzzzzzzzzzuzzzzzzzzzzztzzzizppzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz\r\n", "output": "pnuuefpklifklboowwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwooblkfilkpfeuunp\r\n"}, {"input": "aaaaaaaaakaaakaaaaaabbbbbbbbbbbxbbxbkbbbkxbcccccccccicccccccccddkdxdddkdkddddddkdddddikekeeeeeeeekeeeeeeeixieeffxfffffffffifffffffgikiigggggggggiggggggxgghhhhhkhhhhhhhhhhxhhhjjjjjiijjjjijjjjjxkjkjjjllllllllllkllllllllmmmmmimmmmmmmmmmmmmnnnnnnnnnnnnnnnknnnoooookooioooxioooooooopppppppppppippppxkpkkkppqqxqqqqqqqqiqiqqqqxiqqkqrrkrrrirrrrrrrrrrrrrssskskskssssssssxsssssiittttittxtttttttktttttuxuuuuuiiuuuuuuuuuuikuuvvvvivvixvvvvvvvvvivvvwxiwwwwwwwwwwwkwkwkwwwiwykyyyyyykyyykyxyyyyyyyzzzzzkzizzzzxkkxxkk\r\n", "output": "kxkkxikxkkkikkkixixiikiiixkxiiixkkkikkixippppppppppppppppppixikkikkkxiiixkxiiikiixixikkkikkkxkixkkxk\r\n"}, {"input": "aaaaaakaaaaaaaaaaaataaabbbbrbbbbbbbbbxbbbbbxbbbkctcccccccccccccncckcccccnddddrdddddddddddddddddnteeeeeeeeeeneteeneeeeeeeefffffffffnnffffffffnffffgggggggggtrgggrggggggggghhhhhhhhhnhhxhhhhhhhkhhhitiiiiiiiintiiiikiiiiiiiijxjjjjjjxjjjkjjjjjjjtjjjjlllllntllllllllllllkllllmmxmmmmmmmmmmmmmmmmmmmoooooooooooooonoooooooppprpppprppppptppnpppppppqqqqnnqqqqqqqqqqqqqqqqqssnsssssssstssnssssstssssuunuuuuuruunuuuuuuuukuuuuvvvvvvvvvvvvnvvvvvvvvvwwtwwwwwwwwkwwwwwwwwwxwwyyyyyyxyyyyyyyryyyyyyyyzzzzzzzztzzzzzzzkzzzzz\r\n", "output": "ktrxxktnknrntntnnnntrrnxktnjjjjjjjjjjkjjjjjjjjjjntkxnrrtnnnntntnrnkntkxxrtk\r\n"}, {"input": "afcyfydfxueffbukxgbhfkuufkbubkxiybuuufffkkyyxxbxfbffffbfxbxjxylykkmfffuuubyxkbubkfuukfbxkubffuxfyfyf\r\n", "output": "fyfyfxuffbukxbfkuufkbubkxybuuufffkkyyxxbxfbffffbfxbxxyykkfffuuubyxkbubkfuukfbxkubffuxfyfyf\r\n"}, {"input": "b\r\n", "output": "b\r\n"}, {"input": "abababbabbabbabbbbbbbbabbabbaaaaaaaabbbabbabbbabbbabaaabbaba\r\n", "output": "ababbaababbbbbbabbabbaaaaaaaabbabbabbbbbbabaabbaba\r\n"}, {"input": "ttabacabadabffacavvbaeabacabadabacabafabacabavvvvdabacabzaeabacabadabacttttafba\r\n", "output": "abacabadabacabaeabacabadabacabafabacabadabacabaeabacabadabacaba\r\n"}, {"input": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccdddddddddddddddeeeeeeeeeeeeeeeeeeeeeeeeee\r\n", "output": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\r\n"}, {"input": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccdddddddddddddddeeeeeeeeeeeeeeeeeeeeeeeeee\r\n", "output": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n"}, {"input": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccdddddddddddddddeeeeeeeeeeeeeeeeeeeeeeeeeezzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz\r\n", "output": "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz\r\n"}, {"input": "qwertyuioplkjhgfdsazxcvbnm\r\n", "output": "q\r\n"}, {"input": "abaabacdefgb\r\n", "output": "abaaba\r\n"}, {"input": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabcdefghijklmnopqrstuvwxyzaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n", "output": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaazaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n"}, {"input": "gsayhacnaqwakieoeyomukmqueosoeuckuotyadhoniaovwgnbcpczjnibhgyapqmuosbrpqwkckqjakqjeamyqqwskmoueaakfixextmcwyaqdhtlylnowuiaaoraeskkqaohbwcupgsdcngyavctnaqaaaqomimemckzkaagaebucmjuwacaoicyoywkwmogdiknzqiakackvzgrywxfiojdkopsnsifwwcwkwuqqpfpuaeigcfjebbuakkaquajyelivanwewaq\r\n", "output": "qwaieyuqueeukodoiwgcznigowkcaawmueaakemaqtynuoakkaounytqamekaaeumwaackwoginzcgwiodokueeuquyeiawq\r\n"}, {"input": "amltiwwuisnsaghiiwoaqgdgnpqkfudobascczacwipotugyeairyrcsgsuxxttsxaausijymcceapckvxfqnqdg\r\n", "output": "gdnqfcacyisuxttxusiycacfqndg\r\n"}, {"input": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n", "output": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n"}, {"input": "abababababababababababababababababababababababababcbababababababababababababababababababababababababa\r\n", "output": "abababababababababababababababababababababababababbababababababababababababababababababababababababa\r\n"}, {"input": "aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeefeeeeeeeeeeddddddddddccccccccccbbbbbbbbbbaaaaaaaaaa\r\n", "output": "aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeeeeeeeeeeeddddddddddccccccccccbbbbbbbbbbaaaaaaaaaa\r\n"}]
false
stdio
import sys def is_palindrome(s): return s == s[::-1] def is_subsequence(sub, s): i = 0 for c in sub: i = s.find(c, i) + 1 if i == 0: return False return True input_path, output_path, submission_path = sys.argv[1:4] with open(input_path) as f: s = f.read().strip() with open(output_path) as f: ref_output = f.read().strip() with open(submission_path) as f: sub_output = f.read().strip() if len(sub_output) != len(ref_output): print(0) elif not is_palindrome(sub_output): print(0) elif not is_subsequence(sub_output, s): print(0) else: print(1)
true
613/A
613
A
PyPy 3
TESTS
3
108
20,172,800
126495383
import math def point_in_polygon(x, y, P): n = len(P) intersections = [] for i in range(n): x1, y1 = P[i] if i < n-1: x2, y2 = P[i+1] else: x2, y2 = P[0] if x1==x2: if x1 >= x: intersections.append((x1, y)) else: slope = (y2-y1)/(x2-x1) if slope != 0: intercept = y1-slope*x1 y_int = y x_int = (y_int-intercept)//slope if min(x1, x2) <= x_int <= max(x1, x2) and min(y1, y2) <= y_int <= max(y1, y2): if x <= x_int: intersections.append((x_int, y_int)) else: if y==y1: if min(x1, x2) >= x: intersections.append((x1, y)) if max(x1, x2) >= x: intersections.append((x2, y)) return len(intersections) % 2==1 #x2 + m2x2+2mxb+b2 def find_maxes(A): n = len(A) d1 = float('inf') d2 = -1*float('inf') for i in range(n): x1, y1 = A[i] d1 = min(d1, x1**2+y1**2) d2 = max(d2, x1**2+y1**2) if i < n-1: x2, y2 = A[i+1] else: x2, y2 = A[0] if x1==x2: if min(y1, y2) <= 0 <= max(y1, y2): d1 = min(d1, x1) d2 = max(d2, x2) else: slope = (y2-y1)/(x2-x1) intercept = y1-slope*x1 x3 = -1*slope*intercept/(slope**2+1) y3 = slope*x3+intercept if min(x1, x2) <= x3 <= max(x1, x2): d3 = x3**2+y3**2 d1 = min(d1, d3) d2 = max(d2, d3) return [d1, d2] def process(p1, p2, A): A = [[x-p1, y-p2] for x, y in A] min_dist, max_dist = find_maxes(A) if point_in_polygon(0, 0, A): min_dist = 0 answer = math.pi*max_dist-math.pi*min_dist return answer n, p1, p2 = [int(x) for x in input().split()] A = [] for i in range(n): x, y = [int(x) for x in input().split()] A.append([x, y]) print(process(p1, p2, A))
60
577
13,516,800
192860165
__author__ = 'Utena' import math n,a,b=map(int,input().split()) m=[] s=[] for i in range(n): m.append(list(map(int,input().split()))) for i in range(n): a1,b1=m[i] s.append((a1-a)**2+(b1-b)**2) for i in range(n-1): a1,b1=m[i] a2,b2=m[i+1] if ((a2-a1)*(a2-a)+(b2-b1)*(b2-b))*((a2-a1)*(a1-a)+(b2-b1)*(b1-b))<=0: l=((b2-b1)*a-(a2-a1)*b-a1*b2+a2*b1)**2/((b2-b1)**2+(a2-a1)**2) s.append(l) a1,b1=m[-1] a2,b2=m[0] if ((a2-a1)*(a2-a)+(b2-b1)*(b2-b))*((a2-a1)*(a1-a)+(b2-b1)*(b1-b))<=0: l=((b2-b1)*a-(a2-a1)*b-a1*b2+a2*b1)**2/((b2-b1)**2+(a2-a1)**2) s.append(l) t=math.pi*(max(s)-min(s)) print(t)
Codeforces Round 339 (Div. 1)
CF
2,016
2
256
Peter and Snow Blower
Peter got a new snow blower as a New Year present. Of course, Peter decided to try it immediately. After reading the instructions he realized that it does not work like regular snow blowing machines. In order to make it work, you need to tie it to some point that it does not cover, and then switch it on. As a result it will go along a circle around this point and will remove all the snow from its path. Formally, we assume that Peter's machine is a polygon on a plane. Then, after the machine is switched on, it will make a circle around the point to which Peter tied it (this point lies strictly outside the polygon). That is, each of the points lying within or on the border of the polygon will move along the circular trajectory, with the center of the circle at the point to which Peter tied his machine. Peter decided to tie his car to point P and now he is wondering what is the area of ​​the region that will be cleared from snow. Help him.
The first line of the input contains three integers — the number of vertices of the polygon n ($$3 \leq n \leq 100\ 000$$), and coordinates of point P. Each of the next n lines contains two integers — coordinates of the vertices of the polygon in the clockwise or counterclockwise order. It is guaranteed that no three consecutive vertices lie on a common straight line. All the numbers in the input are integers that do not exceed 1 000 000 in their absolute value.
Print a single real value number — the area of the region that will be cleared. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if $$\frac{|a-b|}{\max(1,b)} \leq 10^{-6}$$.
null
In the first sample snow will be removed from that area:
[{"input": "3 0 0\n0 1\n-1 2\n1 2", "output": "12.566370614359172464"}, {"input": "4 1 -1\n0 0\n1 2\n2 0\n1 1", "output": "21.991148575128551812"}]
1,900
["binary search", "geometry", "ternary search"]
60
[{"input": "3 0 0\r\n0 1\r\n-1 2\r\n1 2\r\n", "output": "12.566370614359172464\r\n"}, {"input": "4 1 -1\r\n0 0\r\n1 2\r\n2 0\r\n1 1\r\n", "output": "21.991148575128551812\r\n"}, {"input": "3 0 0\r\n-1 1\r\n0 3\r\n1 1\r\n", "output": "25.132741228718344928\r\n"}, {"input": "3 -4 2\r\n-3 2\r\n5 -5\r\n5 3\r\n", "output": "405.26545231308331191\r\n"}, {"input": "3 -84 8\r\n-83 8\r\n21 -62\r\n3 53\r\n", "output": "50026.721415763865583\r\n"}, {"input": "6 -94 -51\r\n-93 -51\r\n48 -25\r\n61 27\r\n73 76\r\n-10 87\r\n-48 38\r\n", "output": "138283.48383306192359\r\n"}, {"input": "5 -94 52\r\n-93 52\r\n-78 -56\r\n-54 -81\r\n56 -87\r\n97 85\r\n", "output": "131381.40477312514811\r\n"}, {"input": "10 -100 90\r\n-99 90\r\n-98 -12\r\n-72 -87\r\n7 -84\r\n86 -79\r\n96 -2\r\n100 36\r\n99 59\r\n27 83\r\n-14 93\r\n", "output": "198410.42563011697403\r\n"}, {"input": "11 -97 -15\r\n-96 -15\r\n-83 -84\r\n-61 -97\r\n64 -92\r\n81 -82\r\n100 -63\r\n86 80\r\n58 95\r\n15 99\r\n-48 83\r\n-91 49\r\n", "output": "133558.52848206287476\r\n"}, {"input": "10 -500 420\r\n-499 420\r\n-489 -173\r\n-455 -480\r\n160 -464\r\n374 -437\r\n452 -352\r\n481 -281\r\n465 75\r\n326 392\r\n-398 468\r\n", "output": "4719573.802783449531\r\n"}, {"input": "10 -498 -161\r\n-497 -161\r\n-427 -458\r\n-325 -475\r\n349 -500\r\n441 -220\r\n473 28\r\n475 62\r\n468 498\r\n-444 492\r\n-465 264\r\n", "output": "4295926.8918542123392\r\n"}, {"input": "5 -1 -1\r\n0 0\r\n8 5\r\n10 7\r\n7 5\r\n2 5\r\n", "output": "574.91145560693214023\r\n"}, {"input": "5 -1 -1\r\n0 0\r\n20 3\r\n26 17\r\n23 21\r\n98 96\r\n", "output": "60343.711690152746165\r\n"}, {"input": "10 -1 -1\r\n0 0\r\n94 7\r\n100 52\r\n87 48\r\n37 26\r\n74 61\r\n59 57\r\n87 90\r\n52 90\r\n26 73\r\n", "output": "50337.739088469255101\r\n"}, {"input": "10 -1 -1\r\n0 0\r\n78 22\r\n53 24\r\n78 50\r\n46 39\r\n45 56\r\n21 46\r\n2 7\r\n24 97\r\n5 59\r\n", "output": "32129.068068262814194\r\n"}, {"input": "49 -1 -1\r\n0 0\r\n95 2\r\n47 1\r\n42 1\r\n93 7\r\n56 6\r\n47 7\r\n63 13\r\n98 24\r\n94 27\r\n90 28\r\n86 28\r\n17 6\r\n64 24\r\n42 19\r\n66 35\r\n63 35\r\n98 60\r\n75 48\r\n28 18\r\n71 46\r\n69 46\r\n99 68\r\n64 47\r\n56 43\r\n72 58\r\n35 29\r\n82 81\r\n68 69\r\n79 84\r\n72 77\r\n79 86\r\n54 59\r\n35 39\r\n20 23\r\n73 86\r\n80 97\r\n79 100\r\n69 99\r\n29 45\r\n26 63\r\n23 56\r\n12 33\r\n13 39\r\n25 85\r\n27 96\r\n6 23\r\n4 47\r\n1 60\r\n", "output": "52147.296456936975932\r\n"}, {"input": "49 -1 -1\r\n0 0\r\n69 2\r\n74 7\r\n62 10\r\n64 15\r\n93 22\r\n78 22\r\n56 17\r\n86 29\r\n24 9\r\n91 43\r\n8 4\r\n90 50\r\n99 57\r\n39 23\r\n81 50\r\n91 58\r\n67 46\r\n95 66\r\n52 39\r\n91 69\r\n69 54\r\n93 84\r\n93 98\r\n70 80\r\n85 98\r\n30 39\r\n55 79\r\n41 59\r\n50 72\r\n57 88\r\n58 92\r\n58 94\r\n37 63\r\n43 87\r\n30 63\r\n19 40\r\n38 81\r\n40 86\r\n38 100\r\n2 6\r\n30 100\r\n23 89\r\n16 62\r\n11 49\r\n12 64\r\n9 52\r\n5 62\r\n1 88\r\n", "output": "58543.579099645794717\r\n"}, {"input": "27 -999899 136015\r\n-999898 136015\r\n-999877 -297518\r\n-999832 -906080\r\n-999320 -977222\r\n-998896 -995106\r\n-962959 -999497\r\n-747200 -999814\r\n417261 -999929\r\n844204 -999911\r\n959527 -999826\r\n998944 -999180\r\n999413 -989979\r\n999556 -943026\r\n999871 -774660\r\n999993 -261535\r\n999963 938964\r\n998309 991397\r\n989894 997814\r\n988982 998459\r\n987145 999235\r\n972224 999741\r\n603140 999994\r\n-812452 999962\r\n-980920 999788\r\n-996671 987674\r\n-999472 977919\r\n-999808 639816\r\n", "output": "16600304470662.964855\r\n"}, {"input": "19 -995486 -247212\r\n-995485 -247212\r\n-995004 -492984\r\n-993898 -887860\r\n-938506 -961227\r\n-688481 -971489\r\n178005 -999731\r\n541526 -999819\r\n799710 -988908\r\n905862 -967693\r\n987335 -887414\r\n983567 824667\r\n973128 892799\r\n914017 960546\r\n669333 986330\r\n-441349 986800\r\n-813005 986924\r\n-980671 973524\r\n-988356 849906\r\n-995289 404864\r\n", "output": "16257949833603.158278\r\n"}, {"input": "15 -994057 554462\r\n-994056 554462\r\n-975707 -994167\r\n-711551 -996810\r\n13909 -997149\r\n809315 -993832\r\n980809 -984682\r\n996788 -303578\r\n993267 173570\r\n978439 877361\r\n898589 957311\r\n725925 992298\r\n-57849 999563\r\n-335564 997722\r\n-989580 990530\r\n-993875 973633\r\n", "output": "19694832748836.689348\r\n"}, {"input": "23 -999840 738880\r\n-999839 738880\r\n-998291 -847192\r\n-995443 -982237\r\n-906770 -996569\r\n360950 -999295\r\n800714 -998808\r\n985348 -995579\r\n990091 -928438\r\n996690 -817256\r\n998844 -736918\r\n998377 674949\r\n998008 862436\r\n993320 971157\r\n978831 979400\r\n853341 986660\r\n802107 989497\r\n513719 996183\r\n140983 998592\r\n-158810 999459\r\n-677966 999174\r\n-949021 981608\r\n-982951 976421\r\n-993452 962292\r\n", "output": "21831930831113.094931\r\n"}, {"input": "20 -999719 -377746\r\n-999718 -377746\r\n-997432 -940486\r\n-982215 -950088\r\n-903861 -997725\r\n-127953 -999833\r\n846620 -999745\r\n920305 -992903\r\n947027 -986746\r\n991646 -959876\r\n998264 -944885\r\n999301 870671\r\n994737 985066\r\n640032 998502\r\n-87871 999984\r\n-450900 999751\r\n-910919 999086\r\n-971174 995672\r\n-995406 975642\r\n-998685 946525\r\n-999684 673031\r\n", "output": "18331542740428.216614\r\n"}, {"input": "26 -999922 -339832\r\n-999921 -339832\r\n-999666 -565163\r\n-998004 -942175\r\n-992140 -985584\r\n-965753 -998838\r\n-961074 -999911\r\n120315 -999489\r\n308422 -999258\r\n696427 -997199\r\n724780 -996955\r\n995651 -985203\r\n997267 -975745\r\n999745 -941705\r\n999897 -770648\r\n999841 -211766\r\n999436 865172\r\n999016 992181\r\n980442 997414\r\n799072 998987\r\n348022 999183\r\n-178144 999329\r\n-729638 998617\r\n-953068 997984\r\n-991172 990824\r\n-997976 939889\r\n-999483 581509\r\n", "output": "18127026556380.411608\r\n"}, {"input": "22 -999930 -362070\r\n-999929 -362070\r\n-994861 -919993\r\n-989365 -946982\r\n-964007 -997050\r\n-418950 -998064\r\n351746 -998882\r\n830925 -996765\r\n867755 -996352\r\n964401 -992258\r\n996299 -964402\r\n997257 -930788\r\n999795 -616866\r\n999689 327482\r\n997898 996234\r\n923521 997809\r\n631104 998389\r\n-261788 999672\r\n-609744 999782\r\n-694662 999001\r\n-941227 993687\r\n-997105 992436\r\n-999550 895326\r\n", "output": "18335297542813.80731\r\n"}, {"input": "29 -999961 689169\r\n-999960 689169\r\n-999927 -938525\r\n-999735 -989464\r\n-993714 -997911\r\n-870186 -999686\r\n-796253 -999950\r\n-139940 -999968\r\n969552 -999972\r\n985446 -999398\r\n992690 -997295\r\n999706 -973137\r\n999898 -848630\r\n999997 -192297\r\n999969 773408\r\n999495 960350\r\n999143 981671\r\n998324 993987\r\n997640 998103\r\n986157 998977\r\n966840 999418\r\n670113 999809\r\n477888 999856\r\n129160 999900\r\n-373564 999947\r\n-797543 999976\r\n-860769 999903\r\n-995496 999355\r\n-998771 984570\r\n-999768 927157\r\n", "output": "21409384775316.574772\r\n"}, {"input": "3 -3 3\r\n-3 2\r\n5 -5\r\n5 3\r\n", "output": "399.0305992005743379\r\n"}, {"input": "3 -9 7\r\n-9 6\r\n3 -6\r\n4 2\r\n", "output": "980.17690792001545219\r\n"}, {"input": "5 -9 8\r\n-9 7\r\n-6 -1\r\n-3 -6\r\n1 -3\r\n10 8\r\n", "output": "1130.9820337250702449\r\n"}, {"input": "6 -6 -1\r\n-6 -2\r\n0 -7\r\n8 -9\r\n9 -1\r\n5 10\r\n-5 0\r\n", "output": "816.18577140262825159\r\n"}, {"input": "10 -99 91\r\n-99 90\r\n-98 -12\r\n-72 -87\r\n7 -84\r\n86 -79\r\n96 -2\r\n100 36\r\n99 59\r\n27 83\r\n-14 93\r\n", "output": "198309.89857373595223\r\n"}, {"input": "11 -96 -14\r\n-96 -15\r\n-83 -84\r\n-61 -97\r\n64 -92\r\n81 -82\r\n100 -63\r\n86 80\r\n58 95\r\n15 99\r\n-48 83\r\n-91 49\r\n", "output": "131821.20868619133483\r\n"}, {"input": "13 -98 25\r\n-98 24\r\n-96 10\r\n-80 -71\r\n-71 -78\r\n-31 -99\r\n82 -98\r\n92 -39\r\n94 -2\r\n94 40\r\n90 80\r\n50 96\r\n-41 97\r\n-86 80\r\n", "output": "149316.61930888936332\r\n"}, {"input": "17 -99 -53\r\n-99 -54\r\n-97 -71\r\n-67 -99\r\n-61 -99\r\n56 -98\r\n82 -85\r\n95 -47\r\n90 -2\r\n82 30\r\n63 87\r\n54 95\r\n-12 99\r\n-38 99\r\n-87 89\r\n-90 87\r\n-95 67\r\n-96 49\r\n", "output": "144023.17094830233827\r\n"}, {"input": "19 -995485 -247211\r\n-995485 -247212\r\n-995004 -492984\r\n-993898 -887860\r\n-938506 -961227\r\n-688481 -971489\r\n178005 -999731\r\n541526 -999819\r\n799710 -988908\r\n905862 -967693\r\n987335 -887414\r\n983567 824667\r\n973128 892799\r\n914017 960546\r\n669333 986330\r\n-441349 986800\r\n-813005 986924\r\n-980671 973524\r\n-988356 849906\r\n-995289 404864\r\n", "output": "16257930301545.657524\r\n"}, {"input": "15 -994056 554463\r\n-994056 554462\r\n-975707 -994167\r\n-711551 -996810\r\n13909 -997149\r\n809315 -993832\r\n980809 -984682\r\n996788 -303578\r\n993267 173570\r\n978439 877361\r\n898589 957311\r\n725925 992298\r\n-57849 999563\r\n-335564 997722\r\n-989580 990530\r\n-993875 973633\r\n", "output": "19694830011124.045712\r\n"}, {"input": "23 -999839 738881\r\n-999839 738880\r\n-998291 -847192\r\n-995443 -982237\r\n-906770 -996569\r\n360950 -999295\r\n800714 -998808\r\n985348 -995579\r\n990091 -928438\r\n996690 -817256\r\n998844 -736918\r\n998377 674949\r\n998008 862436\r\n993320 971157\r\n978831 979400\r\n853341 986660\r\n802107 989497\r\n513719 996183\r\n140983 998592\r\n-158810 999459\r\n-677966 999174\r\n-949021 981608\r\n-982951 976421\r\n-993452 962292\r\n", "output": "21831929255745.74826\r\n"}, {"input": "20 -999718 -377745\r\n-999718 -377746\r\n-997432 -940486\r\n-982215 -950088\r\n-903861 -997725\r\n-127953 -999833\r\n846620 -999745\r\n920305 -992903\r\n947027 -986746\r\n991646 -959876\r\n998264 -944885\r\n999301 870671\r\n994737 985066\r\n640032 998502\r\n-87871 999984\r\n-450900 999751\r\n-910919 999086\r\n-971174 995672\r\n-995406 975642\r\n-998685 946525\r\n-999684 673031\r\n", "output": "18331521646100.671528\r\n"}, {"input": "26 -999921 -339831\r\n-999921 -339832\r\n-999666 -565163\r\n-998004 -942175\r\n-992140 -985584\r\n-965753 -998838\r\n-961074 -999911\r\n120315 -999489\r\n308422 -999258\r\n696427 -997199\r\n724780 -996955\r\n995651 -985203\r\n997267 -975745\r\n999745 -941705\r\n999897 -770648\r\n999841 -211766\r\n999436 865172\r\n999016 992181\r\n980442 997414\r\n799072 998987\r\n348022 999183\r\n-178144 999329\r\n-729638 998617\r\n-953068 997984\r\n-991172 990824\r\n-997976 939889\r\n-999483 581509\r\n", "output": "18127005627407.454252\r\n"}, {"input": "22 -999929 -362069\r\n-999929 -362070\r\n-994861 -919993\r\n-989365 -946982\r\n-964007 -997050\r\n-418950 -998064\r\n351746 -998882\r\n830925 -996765\r\n867755 -996352\r\n964401 -992258\r\n996299 -964402\r\n997257 -930788\r\n999795 -616866\r\n999689 327482\r\n997898 996234\r\n923521 997809\r\n631104 998389\r\n-261788 999672\r\n-609744 999782\r\n-694662 999001\r\n-941227 993687\r\n-997105 992436\r\n-999550 895326\r\n", "output": "18335276455623.960732\r\n"}, {"input": "27 -999898 136016\r\n-999898 136015\r\n-999877 -297518\r\n-999832 -906080\r\n-999320 -977222\r\n-998896 -995106\r\n-962959 -999497\r\n-747200 -999814\r\n417261 -999929\r\n844204 -999911\r\n959527 -999826\r\n998944 -999180\r\n999413 -989979\r\n999556 -943026\r\n999871 -774660\r\n999993 -261535\r\n999963 938964\r\n998309 991397\r\n989894 997814\r\n988982 998459\r\n987145 999235\r\n972224 999741\r\n603140 999994\r\n-812452 999962\r\n-980920 999788\r\n-996671 987674\r\n-999472 977919\r\n-999808 639816\r\n", "output": "16600299044211.965457\r\n"}, {"input": "13 -1000000 -1000000\r\n-1000000 0\r\n0 -1000000\r\n999417 840\r\n999781 33421\r\n999994 131490\r\n999993 998865\r\n962080 999911\r\n629402 999973\r\n378696 999988\r\n53978 999788\r\n25311 999558\r\n6082 999282\r\n1565 998489\r\n", "output": "23547598153913.984406\r\n"}, {"input": "16 -1000000 -1000000\r\n-1000000 0\r\n0 -1000000\r\n999744 572\r\n999931 96510\r\n1000000 254372\r\n999939 748173\r\n999894 953785\r\n999683 986098\r\n999051 999815\r\n980586 999969\r\n637250 999988\r\n118331 999983\r\n27254 999966\r\n9197 999405\r\n4810 997733\r\n1661 995339\r\n", "output": "23547697574489.259052\r\n"}, {"input": "4 0 0\r\n1 -1\r\n1 3\r\n3 3\r\n3 -1\r\n", "output": "53.407075111026482965\r\n"}, {"input": "3 0 0\r\n-10 1\r\n0 2\r\n1 1\r\n", "output": "314.1592653589793116\r\n"}, {"input": "3 0 0\r\n-1 1\r\n4 1\r\n0 2\r\n", "output": "50.265482457436689849\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(output_path, 'r') as f: ref_line = f.readline().strip() try: b = float(ref_line) except: print(0) return with open(submission_path, 'r') as f: sub_line = f.readline().strip() try: a = float(sub_line) except: print(0) return denominator = max(1.0, b) absolute_error = abs(a - b) if absolute_error <= 1e-6 * denominator: print(1) else: print(0) if __name__ == "__main__": main()
true
540/C
540
C
Python 3
TESTS
8
93
1,638,400
117954397
import queue MAX = 500 * 500 + 1 H, W = list(map(int, input().split())) visited = [False] * MAX vertices = [] def generate_graph(vertices, W, H): graph = [[] for i in range(W * H)] for index in range(len(vertices)): if vertices[index] == '.': if index % W != 0: if vertices[index - 1] == '.': graph[index].append(index - 1) if (index + 1) % W != 0: if vertices[index + 1] == '.': graph[index].append(index + 1) if index >= W: if vertices[index - W] == '.': graph[index].append(index - W) if index < W * (H - 1): if vertices[index + W] == '.': graph[index].append(index + W) return graph for i in range(H): vertices += [item for item in input()] r1, c1 = list(map(int, input().split())) r2, c2 = list(map(int, input().split())) start_index = W * (r1 - 1) + (c1 - 1) end_index = W * (r2 - 1) + (c2 - 1) vertices_clone = vertices.copy() vertices_clone[start_index] = '.' vertices_clone[end_index] = '.' graph = generate_graph(vertices_clone, W, H) q = queue.Queue() q.put(start_index) visited[start_index] = True result = 'NO' while not q.empty() and result == 'NO': u = q.get() for v in graph[u]: if result == 'YES': break if not visited[v]: q.put(v) visited[v] = True if v == end_index: if vertices[v] == '.': for j in graph[v]: if not visited[j]: result = 'YES' break else: result = 'YES' break print(result)
102
421
6,656,000
10947679
def read_data(): n, m = map(int, input().split()) maze = [[False] * (m + 2)] for i in range(n): maze.append([False] + [c == '.' for c in input().rstrip()] + [False]) maze.append([False] * (m + 2)) r1, c1 = map(int, input().split()) r2, c2 = map(int, input().split()) return n, m, maze, r1, c1, r2, c2 def solve(n, m, maze, r1, c1, r2, c2): dots = count_surrounding_intact_ices(maze, r2, c2) if maze[r2][c2] == False: if r1 == r2 and c1 == c2: return dots >= 1 else: return solve_wfs(n, m, maze, r1, c1, r2, c2) else: if dots >= 2: return solve_wfs(n, m, maze, r1, c1, r2, c2) if dots == 0: return False if dots == 1: return is_side_by_side(r1, c1, r2, c2) def is_side_by_side(r1, c1, r2, c2): if r1 == r2: return abs(c1 - c2) == 1 if c1 == c2: return abs(r1 - r2) == 1 return False def count_surrounding_intact_ices(maze, r, c): count = 0 for rr, cc in [(r+1, c), (r-1, c), (r, c+1), (r, c-1)]: if maze[rr][cc]: count += 1 return count def solve_wfs(n, m, maze, r1, c1, r2, c2): frontier = [(r1, c1)] while frontier: new_frontier = [] for r, c in frontier: for nr, nc in [(r+1, c), (r-1, c), (r, c+1), (r, c-1)]: if nr == r2 and nc == c2: return True if not maze[nr][nc]: continue maze[nr][nc] = False new_frontier.append((nr, nc)) frontier = new_frontier return False if __name__ == '__main__': n, m, maze, r1, c1, r2, c2 = read_data() if solve(n, m, maze, r1, c1, r2, c2): print('YES') else: print('NO')
Codeforces Round 301 (Div. 2)
CF
2,015
2
256
Ice Cave
You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice. The level of the cave where you are is a rectangular square grid of n rows and m columns. Each cell consists either from intact or from cracked ice. From each cell you can move to cells that are side-adjacent with yours (due to some limitations of the game engine you cannot make jumps on the same place, i.e. jump from a cell to itself). If you move to the cell with cracked ice, then your character falls down through it and if you move to the cell with intact ice, then the ice on this cell becomes cracked. Let's number the rows with integers from 1 to n from top to bottom and the columns with integers from 1 to m from left to right. Let's denote a cell on the intersection of the r-th row and the c-th column as (r, c). You are staying in the cell (r1, c1) and this cell is cracked because you've just fallen here from a higher level. You need to fall down through the cell (r2, c2) since the exit to the next level is there. Can you do this?
The first line contains two integers, n and m (1 ≤ n, m ≤ 500) — the number of rows and columns in the cave description. Each of the next n lines describes the initial state of the level of the cave, each line consists of m characters "." (that is, intact ice) and "X" (cracked ice). The next line contains two integers, r1 and c1 (1 ≤ r1 ≤ n, 1 ≤ c1 ≤ m) — your initial coordinates. It is guaranteed that the description of the cave contains character 'X' in cell (r1, c1), that is, the ice on the starting cell is initially cracked. The next line contains two integers r2 and c2 (1 ≤ r2 ≤ n, 1 ≤ c2 ≤ m) — the coordinates of the cell through which you need to fall. The final cell may coincide with the starting one.
If you can reach the destination, print 'YES', otherwise print 'NO'.
null
In the first sample test one possible path is: $$(1,6)\rightarrow(2,6)\rightarrow(3,6)\rightarrow(4,5)\rightarrow(4,4)\rightarrow(4,3)\rightarrow(4,2)\rightarrow(4,1)\rightarrow(3,2)\rightarrow(2,3)\rightarrow(2,2)$$ After the first visit of cell (2, 2) the ice on it cracks and when you step there for the second time, your character falls through the ice as intended.
[{"input": "4 6\nX...XX\n...XX.\n.X..X.\n......\n1 6\n2 2", "output": "YES"}, {"input": "5 4\n.X..\n...X\nX.X.\n....\n.XX.\n5 3\n1 1", "output": "NO"}, {"input": "4 7\n..X.XX.\n.XX..X.\nX...X..\nX......\n2 2\n1 6", "output": "YES"}]
2,000
["dfs and similar"]
102
[{"input": "4 6\r\nX...XX\r\n...XX.\r\n.X..X.\r\n......\r\n1 6\r\n2 2\r\n", "output": "YES\r\n"}, {"input": "5 4\r\n.X..\r\n...X\r\nX.X.\r\n....\r\n.XX.\r\n5 3\r\n1 1\r\n", "output": "NO\r\n"}, {"input": "4 7\r\n..X.XX.\r\n.XX..X.\r\nX...X..\r\nX......\r\n2 2\r\n1 6\r\n", "output": "YES\r\n"}, {"input": "5 3\r\n.XX\r\n...\r\n.X.\r\n.X.\r\n...\r\n1 3\r\n4 1\r\n", "output": "YES\r\n"}, {"input": "1 1\r\nX\r\n1 1\r\n1 1\r\n", "output": "NO\r\n"}, {"input": "1 6\r\n.X...X\r\n1 2\r\n1 5\r\n", "output": "NO\r\n"}, {"input": "7 1\r\nX\r\n.\r\n.\r\n.\r\nX\r\n.\r\n.\r\n5 1\r\n3 1\r\n", "output": "YES\r\n"}, {"input": "1 2\r\nXX\r\n1 1\r\n1 1\r\n", "output": "NO\r\n"}, {"input": "2 1\r\n.\r\nX\r\n2 1\r\n2 1\r\n", "output": "YES\r\n"}, {"input": "3 4\r\n.X..\r\n..XX\r\n..X.\r\n1 2\r\n3 4\r\n", "output": "NO\r\n"}, {"input": "3 5\r\n.X.XX\r\nX...X\r\nX.X..\r\n2 1\r\n1 5\r\n", "output": "NO\r\n"}, {"input": "3 2\r\n..\r\nX.\r\n.X\r\n3 2\r\n3 1\r\n", "output": "NO\r\n"}, {"input": "3 4\r\nXX.X\r\nX...\r\n.X.X\r\n1 2\r\n1 1\r\n", "output": "YES\r\n"}, {"input": "1 2\r\nX.\r\n1 1\r\n1 2\r\n", "output": "NO\r\n"}, {"input": "2 1\r\nX\r\nX\r\n2 1\r\n1 1\r\n", "output": "YES\r\n"}, {"input": "2 2\r\nXX\r\nXX\r\n1 1\r\n2 2\r\n", "output": "NO\r\n"}, {"input": "2 2\r\n..\r\n.X\r\n2 2\r\n1 1\r\n", "output": "YES\r\n"}, {"input": "2 2\r\n.X\r\n.X\r\n1 2\r\n2 2\r\n", "output": "YES\r\n"}, {"input": "2 2\r\n..\r\nXX\r\n2 1\r\n1 1\r\n", "output": "YES\r\n"}, {"input": "4 2\r\nX.\r\n.X\r\n.X\r\nXX\r\n2 2\r\n3 1\r\n", "output": "NO\r\n"}, {"input": "2 4\r\nX.XX\r\n.X..\r\n2 2\r\n2 3\r\n", "output": "YES\r\n"}, {"input": "6 4\r\nX..X\r\n..X.\r\n.X..\r\n..X.\r\n.X..\r\nX..X\r\n1 1\r\n6 4\r\n", "output": "NO\r\n"}, {"input": "5 4\r\nX...\r\n..X.\r\n.X..\r\nX..X\r\n....\r\n4 4\r\n3 1\r\n", "output": "NO\r\n"}, {"input": "3 4\r\nX..X\r\n..XX\r\n.X..\r\n2 3\r\n3 1\r\n", "output": "NO\r\n"}, {"input": "20 20\r\n....................\r\n.......X...........X\r\n............X......X\r\n.X...XX..X....X.....\r\n....X.....X.........\r\nX..........X........\r\n......X........X....\r\n....................\r\n...................X\r\n......X.............\r\n..............X.....\r\n......X.X...........\r\n.X.........X.X......\r\n.........X......X..X\r\n..................X.\r\n...X........X.......\r\n....................\r\n....................\r\n..X.....X...........\r\n........X......X.X..\r\n20 16\r\n5 20\r\n", "output": "YES\r\n"}, {"input": "21 21\r\n.....X...X.........X.\r\n...X...XX......X.....\r\n..X........X.X...XX..\r\n.........X....X...X..\r\nX...X...........XX...\r\n...X...X....XX...XXX.\r\n.X............X......\r\n......X.X............\r\n.X...X.........X.X...\r\n......XX......X.X....\r\n....X.......X.XXX....\r\n.X.......X..XXX.X..X.\r\n..X........X....X...X\r\n.........X..........X\r\n.....XX.....X........\r\n...XX......X.........\r\n.....X...XX...X......\r\n..X.X....XX..XX.X....\r\nX........X.X..XX..X..\r\nX..X......X...X.X....\r\nX.....X.....X.X......\r\n20 4\r\n21 5\r\n", "output": "YES\r\n"}, {"input": "2 1\r\nX\r\nX\r\n2 1\r\n2 1\r\n", "output": "NO\r\n"}, {"input": "2 2\r\nXX\r\nX.\r\n1 1\r\n2 2\r\n", "output": "NO\r\n"}, {"input": "2 1\r\nX\r\nX\r\n1 1\r\n1 1\r\n", "output": "NO\r\n"}, {"input": "1 2\r\nXX\r\n1 2\r\n1 2\r\n", "output": "NO\r\n"}, {"input": "1 2\r\nXX\r\n1 1\r\n1 2\r\n", "output": "YES\r\n"}, {"input": "1 2\r\nXX\r\n1 2\r\n1 1\r\n", "output": "YES\r\n"}, {"input": "2 1\r\nX\r\nX\r\n1 1\r\n2 1\r\n", "output": "YES\r\n"}, {"input": "2 1\r\n.\r\nX\r\n2 1\r\n1 1\r\n", "output": "NO\r\n"}, {"input": "2 1\r\nX\r\n.\r\n1 1\r\n2 1\r\n", "output": "NO\r\n"}, {"input": "1 2\r\n.X\r\n1 2\r\n1 1\r\n", "output": "NO\r\n"}, {"input": "2 1\r\nX\r\n.\r\n1 1\r\n1 1\r\n", "output": "YES\r\n"}, {"input": "1 2\r\nX.\r\n1 1\r\n1 1\r\n", "output": "YES\r\n"}, {"input": "1 2\r\n.X\r\n1 2\r\n1 2\r\n", "output": "YES\r\n"}, {"input": "2 2\r\nX.\r\n..\r\n1 1\r\n2 2\r\n", "output": "YES\r\n"}, {"input": "2 2\r\n..\r\nX.\r\n2 1\r\n1 1\r\n", "output": "YES\r\n"}, {"input": "4 3\r\n..X\r\n..X\r\n.XX\r\n.XX\r\n4 2\r\n2 2\r\n", "output": "YES\r\n"}, {"input": "3 3\r\nXXX\r\nX..\r\nXXX\r\n2 1\r\n2 2\r\n", "output": "YES\r\n"}, {"input": "5 4\r\nXXXX\r\nX..X\r\nX..X\r\nXXXX\r\nXXXX\r\n4 2\r\n3 3\r\n", "output": "YES\r\n"}]
false
stdio
null
true
830/A
830
A
Python 3
TESTS
29
62
614,400
174185876
from math import floor import collections import heapq class TrieNode: def __init__(self): self.children = {} self.count = 0 class Trie: def __init__(self): self.root = TrieNode() def insert(self, word): curr = self.root for c in word: if c not in curr.children: curr.children[c] = TrieNode() curr = curr.children[c] curr.count+=1 N,K,P = map(int, input().split()) people = list(map(int, input().split())) keys = list(map(int, input().split())) people.sort() keys.sort() first_person = people[0] last_person = people[-1] def binary_search(curr_person): distance = float('-inf') i = 0 j = K-N while i<j-1: mid = (i+j)//2 curr_distance = abs(curr_person-keys[mid]) if curr_distance<distance: distance = curr_distance i = mid else: j = mid if abs(curr_person-keys[j])<abs(curr_person-keys[i]): return j return i def binary_search2(curr_person): distance = float('-inf') i = K-N j = K-1 while i<j-1: mid = (i+j)//2 curr_distance = abs(curr_person-keys[mid]) if curr_distance<distance: distance = curr_distance j = mid else: i = mid if abs(curr_person-keys[i])<abs(curr_person-keys[j]): return i return j ans1 = float('-inf') first_idx = binary_search(first_person) for person in people: key_pos = keys[first_idx] ans1 = max(ans1, abs(person-key_pos)+abs(key_pos-P)) first_idx+=1 last_idx = binary_search2(last_person) ans2 = float('-inf') for person in people[::-1]: key_pos = keys[last_idx] ans2 = max(ans2, abs(person-key_pos)+abs(key_pos-P)) last_idx-=1 res = min(ans1, ans2) print(res)
59
77
3,584,000
183502526
n,k,p=map(int,input().split()) ans=float("inf") a=list(map(int,input().split())) b=list(map(int,input().split())) a.sort() b.sort() for i in range(k-n+1): t=0 o=i for j in range(n): t=max(t,abs(a[j]-b[o])+abs(b[o]-p)) o+=1 ans=min(ans,t) print(ans)
Codeforces Round 424 (Div. 1, rated, based on VK Cup Finals)
CF
2,017
2
256
Office Keys
There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else. You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.
The first line contains three integers n, k and p (1 ≤ n ≤ 1 000, n ≤ k ≤ 2 000, 1 ≤ p ≤ 109) — the number of people, the number of keys and the office location. The second line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 109) — positions in which people are located initially. The positions are given in arbitrary order. The third line contains k distinct integers b1, b2, ..., bk (1 ≤ bj ≤ 109) — positions of the keys. The positions are given in arbitrary order. Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.
Print the minimum time (in seconds) needed for all n to reach the office with keys.
null
In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys.
[{"input": "2 4 50\n20 100\n60 10 40 80", "output": "50"}, {"input": "1 2 10\n11\n15 7", "output": "7"}]
1,800
["binary search", "brute force", "dp", "greedy", "sortings"]
59
[{"input": "2 4 50\r\n20 100\r\n60 10 40 80\r\n", "output": "50\r\n"}, {"input": "1 2 10\r\n11\r\n15 7\r\n", "output": "7\r\n"}, {"input": "2 5 15\r\n10 4\r\n29 23 21 22 26\r\n", "output": "23\r\n"}, {"input": "3 10 1500\r\n106 160 129\r\n1333 1532 1181 1091 1656 1698 1291 1741 1242 1163\r\n", "output": "1394\r\n"}, {"input": "5 20 1\r\n314 316 328 323 321\r\n30 61 11 83 19 63 97 87 14 79 43 57 75 48 47 95 41 27 8 88\r\n", "output": "327\r\n"}, {"input": "20 20 1000000000\r\n911196469 574676950 884047241 984218701 641693148 352743122 616364857 455260052 702604347 921615943 671695009 544819698 768892858 254148055 379968391 65297129 178692403 575557323 307174510 63022600\r\n1621 106 6866 6420 9307 6985 2741 9477 9837 5909 6757 3085 6139 1876 3726 9334 4321 1531 8534 560\r\n", "output": "1984199027\r\n"}, {"input": "40 45 1000\r\n6 55 34 32 20 76 2 84 47 68 31 60 14 70 99 72 21 61 81 79 26 51 96 86 10 1 43 69 87 78 13 11 80 67 50 52 9 29 94 12\r\n1974 1232 234 28 1456 626 408 1086 1525 1209 1096 940 795 1867 548 1774 1993 1199 1112 1087 1923 1156 876 1715 1815 1027 1658 955 398 910 620 1164 749 996 113 109 500 328 800 826 766 518 1474 1038 1029\r\n", "output": "2449\r\n"}, {"input": "50 55 2000\r\n9518 9743 9338 9956 9827 9772 9094 9644 9242 9292 9148 9205 9907 9860 9530 9814 9662 9482 9725 9227 9105 9424 9268 9427 9470 9578 9808 9976 9143 9070 9079 9896 9367 9235 9925 9009 9619 9012 9669 9077 9870 9766 9479 9598 9055 9988 9792 9197 9377 9610\r\n828 656 345 412 69 506 274 994 384 766 587 126 720 227 66 839 997 602 646 955 256 262 243 676 459 83 507 88 559 595 71 154 867 276 487 895 857 888 368 179 813 407 973 780 588 112 815 290 554 230 768 804 974 3 745\r\n", "output": "10833\r\n"}, {"input": "1 1 1\r\n1\r\n1000000000\r\n", "output": "1999999998\r\n"}, {"input": "1 1 1\r\n1000000000\r\n1\r\n", "output": "999999999\r\n"}, {"input": "1 1 1000000000\r\n1000000000\r\n1\r\n", "output": "1999999998\r\n"}, {"input": "1 1 1000000000\r\n1\r\n1000000000\r\n", "output": "999999999\r\n"}, {"input": "2 2 4\r\n3 4\r\n5 6\r\n", "output": "4\r\n"}, {"input": "2 2 5\r\n1 2\r\n3 1000000000\r\n", "output": "1999999993\r\n"}, {"input": "1 1 1000000000\r\n1000000000\r\n1\r\n", "output": "1999999998\r\n"}, {"input": "2 2 1\r\n2 3\r\n4 100\r\n", "output": "196\r\n"}, {"input": "2 2 10\r\n3 12\r\n1 9\r\n", "output": "11\r\n"}, {"input": "3 3 1\r\n1 2 3\r\n999 1000000000 1\r\n", "output": "1999999996\r\n"}, {"input": "1 1 1\r\n1\r\n1\r\n", "output": "0\r\n"}, {"input": "1 1 1\r\n1\r\n1000000000\r\n", "output": "1999999998\r\n"}, {"input": "1 1 1000000000\r\n1000000000\r\n10\r\n", "output": "1999999980\r\n"}, {"input": "2 2 7122\r\n123 456\r\n1 4444\r\n", "output": "7243\r\n"}, {"input": "1 1 10\r\n5\r\n15\r\n", "output": "15\r\n"}, {"input": "2 4 1000\r\n1000 999\r\n1 1000 2 999\r\n", "output": "1\r\n"}, {"input": "2 2 1000\r\n10 1010\r\n1 1001\r\n", "output": "1008\r\n"}, {"input": "1 1 1\r\n2\r\n1000000000\r\n", "output": "1999999997\r\n"}, {"input": "2 2 3\r\n1 5\r\n5 1\r\n", "output": "2\r\n"}, {"input": "2 2 5\r\n2 3\r\n4 6\r\n", "output": "4\r\n"}, {"input": "2 2 10\r\n5 6\r\n4 6\r\n", "output": "7\r\n"}, {"input": "3 4 10\r\n5 7 9\r\n6 8 14 4\r\n", "output": "7\r\n"}, {"input": "1 1 10\r\n10\r\n10\r\n", "output": "0\r\n"}, {"input": "1 1 50\r\n1\r\n1000000000\r\n", "output": "1999999949\r\n"}, {"input": "1 1 42\r\n666\r\n1337\r\n", "output": "1966\r\n"}, {"input": "2 2 10\r\n9 11\r\n11 8\r\n", "output": "3\r\n"}, {"input": "3 10 5\r\n1 2 3\r\n10000 9999 9998 9997 9996 9995 9994 7 6 5\r\n", "output": "6\r\n"}, {"input": "1 1 2\r\n1\r\n1000000000\r\n", "output": "1999999997\r\n"}, {"input": "2 2 100\r\n99 150\r\n1 150\r\n", "output": "197\r\n"}, {"input": "3 3 4\r\n1 101 102\r\n2 3 100\r\n", "output": "99\r\n"}]
false
stdio
null
true
837/C
837
C
PyPy 3-64
TESTS
12
46
0
216089138
n,a,b=map(int,input().split()) ls=[] for i in range(n): x,y=map(int,input().split()) ls.append((x,y,x*y)) ls.sort(key=lambda x:-x[2]) m=0 for i in range(n): for j in range(i+1,n): a1,b1=ls[i][0],ls[i][1] a2,b2=ls[j][0],ls[j][1] if (a1<=a and b1<=b) or (a1<=b and b1<=a) : if (a2<=(b-b1) and b2<=a) or (a2<=a and b2<=(b-b1)) or (a2<=(a-a1) and b2<=b) or (a2<=b and b2<=(a-a1)): m=max(m,a1*b1+a2*b2) print(m)
51
62
5,529,600
31506491
f = lambda: map(int, input().split()) n, a, b = f() c = a + b s, t = 0, [list(f()) for i in range(n)] h = [(i, j, d) for i in [0, 1] for j in [0, 1] for d in [a, b]] for k, u in enumerate(t, 1): for v in t[k:]: if not all(u[i] + v[j] > d or max(u[1 - i], v[1 - j]) > c - d for i, j, d in h): s = max(s, u[0] * u[1] + v[0] * v[1]) print(s)
Educational Codeforces Round 26
ICPC
2,017
1
256
Two Seals
One very important person has a piece of paper in the form of a rectangle a × b. Also, he has n seals. Each seal leaves an impression on the paper in the form of a rectangle of the size xi × yi. Each impression must be parallel to the sides of the piece of paper (but seal can be rotated by 90 degrees). A very important person wants to choose two different seals and put them two impressions. Each of the selected seals puts exactly one impression. Impressions should not overlap (but they can touch sides), and the total area occupied by them should be the largest possible. What is the largest area that can be occupied by two seals?
The first line contains three integer numbers n, a and b (1 ≤ n, a, b ≤ 100). Each of the next n lines contain two numbers xi, yi (1 ≤ xi, yi ≤ 100).
Print the largest total area that can be occupied by two seals. If you can not select two seals, print 0.
null
In the first example you can rotate the second seal by 90 degrees. Then put impression of it right under the impression of the first seal. This will occupy all the piece of paper. In the second example you can't choose the last seal because it doesn't fit. By choosing the first and the third seals you occupy the largest area. In the third example there is no such pair of seals that they both can fit on a piece of paper.
[{"input": "2 2 2\n1 2\n2 1", "output": "4"}, {"input": "4 10 9\n2 3\n1 1\n5 10\n9 11", "output": "56"}, {"input": "3 10 10\n6 6\n7 7\n20 5", "output": "0"}]
1,500
["brute force", "implementation"]
51
[{"input": "2 2 2\r\n1 2\r\n2 1\r\n", "output": "4\r\n"}, {"input": "4 10 9\r\n2 3\r\n1 1\r\n5 10\r\n9 11\r\n", "output": "56\r\n"}, {"input": "3 10 10\r\n6 6\r\n7 7\r\n20 5\r\n", "output": "0\r\n"}, {"input": "2 1 1\r\n1 1\r\n1 1\r\n", "output": "0\r\n"}, {"input": "2 1 2\r\n1 1\r\n1 1\r\n", "output": "2\r\n"}, {"input": "2 100 100\r\n100 100\r\n1 1\r\n", "output": "0\r\n"}, {"input": "2 100 100\r\n50 100\r\n100 50\r\n", "output": "10000\r\n"}, {"input": "2 100 100\r\n100 100\r\n87 72\r\n", "output": "0\r\n"}, {"input": "5 100 100\r\n100 100\r\n100 100\r\n100 100\r\n100 100\r\n100 100\r\n", "output": "0\r\n"}, {"input": "15 50 50\r\n9 36\r\n28 14\r\n77 74\r\n35 2\r\n20 32\r\n83 85\r\n47 3\r\n41 50\r\n21 7\r\n38 46\r\n17 6\r\n79 90\r\n91 83\r\n9 33\r\n24 11\r\n", "output": "2374\r\n"}, {"input": "15 100 100\r\n100 100\r\n100 100\r\n100 100\r\n42 58\r\n80 22\r\n100 100\r\n100 100\r\n100 100\r\n100 100\r\n100 100\r\n48 42\r\n100 100\r\n100 100\r\n100 100\r\n100 100\r\n", "output": "4452\r\n"}, {"input": "30 100 100\r\n60 34\r\n29 82\r\n89 77\r\n39 1\r\n100 100\r\n82 12\r\n57 87\r\n93 43\r\n78 50\r\n38 55\r\n37 9\r\n67 5\r\n100 100\r\n100 100\r\n82 47\r\n3 71\r\n100 100\r\n19 26\r\n25 94\r\n89 5\r\n100 100\r\n32 1\r\n100 100\r\n34 3\r\n40 99\r\n100 100\r\n36 12\r\n100 100\r\n100 100\r\n100 100\r\n", "output": "8958\r\n"}, {"input": "3 100 1\r\n1 50\r\n1 60\r\n1 30\r\n", "output": "90\r\n"}, {"input": "3 1 60\r\n1 40\r\n2 2\r\n20 1\r\n", "output": "60\r\n"}, {"input": "4 1 100\r\n1 25\r\n25 1\r\n1 25\r\n2 100\r\n", "output": "50\r\n"}, {"input": "1 100 50\r\n4 20\r\n", "output": "0\r\n"}, {"input": "2 2 4\r\n3 1\r\n2 2\r\n", "output": "0\r\n"}, {"input": "2 2 4\r\n2 3\r\n2 1\r\n", "output": "8\r\n"}, {"input": "2 4 2\r\n1 2\r\n2 3\r\n", "output": "8\r\n"}, {"input": "2 1 4\r\n1 2\r\n1 2\r\n", "output": "4\r\n"}, {"input": "2 4 5\r\n2 4\r\n4 3\r\n", "output": "20\r\n"}, {"input": "2 1 4\r\n1 1\r\n3 3\r\n", "output": "0\r\n"}, {"input": "6 9 5\r\n4 5\r\n6 2\r\n1 4\r\n5 6\r\n3 7\r\n6 5\r\n", "output": "34\r\n"}, {"input": "6 8 5\r\n4 1\r\n3 3\r\n5 3\r\n6 7\r\n2 2\r\n5 4\r\n", "output": "35\r\n"}, {"input": "6 7 5\r\n6 4\r\n5 7\r\n4 7\r\n5 4\r\n1 1\r\n3 6\r\n", "output": "29\r\n"}, {"input": "6 9 7\r\n1 2\r\n1 5\r\n4 3\r\n4 7\r\n3 5\r\n6 7\r\n", "output": "57\r\n"}, {"input": "6 5 9\r\n2 3\r\n7 4\r\n1 5\r\n1 7\r\n2 5\r\n7 1\r\n", "output": "38\r\n"}, {"input": "2 4 2\r\n2 2\r\n1 3\r\n", "output": "0\r\n"}, {"input": "2 3 2\r\n3 2\r\n1 1\r\n", "output": "0\r\n"}, {"input": "6 7 5\r\n6 6\r\n4 7\r\n6 1\r\n4 1\r\n4 6\r\n1 5\r\n", "output": "34\r\n"}, {"input": "2 2 3\r\n1 2\r\n2 3\r\n", "output": "0\r\n"}, {"input": "2 2 2\r\n2 1\r\n1 1\r\n", "output": "3\r\n"}, {"input": "5 9 7\r\n6 7\r\n4 5\r\n2 7\r\n4 2\r\n5 8\r\n", "output": "56\r\n"}, {"input": "2 11 51\r\n1 10\r\n11 50\r\n", "output": "560\r\n"}, {"input": "5 9 7\r\n3 8\r\n7 6\r\n4 1\r\n5 8\r\n7 8\r\n", "output": "60\r\n"}, {"input": "2 4 6\r\n4 4\r\n4 2\r\n", "output": "24\r\n"}, {"input": "5 9 7\r\n1 6\r\n7 9\r\n1 5\r\n1 5\r\n7 3\r\n", "output": "27\r\n"}, {"input": "5 9 7\r\n5 2\r\n6 9\r\n1 4\r\n7 7\r\n6 4\r\n", "output": "59\r\n"}, {"input": "5 9 7\r\n6 7\r\n4 1\r\n1 2\r\n4 7\r\n5 6\r\n", "output": "58\r\n"}, {"input": "5 9 7\r\n2 8\r\n3 8\r\n2 8\r\n4 4\r\n2 2\r\n", "output": "40\r\n"}, {"input": "2 2 3\r\n1 4\r\n2 1\r\n", "output": "0\r\n"}, {"input": "5 9 7\r\n4 7\r\n3 9\r\n5 4\r\n3 4\r\n3 8\r\n", "output": "55\r\n"}, {"input": "5 9 7\r\n7 4\r\n6 9\r\n4 3\r\n7 5\r\n2 3\r\n", "output": "63\r\n"}, {"input": "2 2 3\r\n1 2\r\n2 2\r\n", "output": "6\r\n"}, {"input": "2 4 3\r\n2 1\r\n1 2\r\n", "output": "4\r\n"}, {"input": "2 4 6\r\n4 2\r\n4 4\r\n", "output": "24\r\n"}, {"input": "2 1 4\r\n3 2\r\n3 3\r\n", "output": "0\r\n"}]
false
stdio
null
true
732/B
732
B
PyPy 3-64
TESTS
2
46
0
196918932
n,k=map(int,input().split()) a=list(map(int,input().split())) s=0 for i in range(1,n): if a[i-1]+a[i]!=k: s+=(k-(a[i-1]+a[i])) a[i]+=(k-(a[i-1]+a[i])) print(s) print(*a)
70
46
0
197137668
n,k = map(int, input().split()) li = list(map(int, input().split())) res = 0 for i in range(1,n): if li[i-1] + li[i] < k: res += k -(li[i-1] + li[i]) li[i] = k - li[i-1] print(res) print(*li)
Codeforces Round 377 (Div. 2)
CF
2,016
1
256
Cormen — The Best Friend Of a Man
Recently a dog was bought for Polycarp. The dog's name is Cormen. Now Polycarp has a lot of troubles. For example, Cormen likes going for a walk. Empirically Polycarp learned that the dog needs at least k walks for any two consecutive days in order to feel good. For example, if k = 5 and yesterday Polycarp went for a walk with Cormen 2 times, today he has to go for a walk at least 3 times. Polycarp analysed all his affairs over the next n days and made a sequence of n integers a1, a2, ..., an, where ai is the number of times Polycarp will walk with the dog on the i-th day while doing all his affairs (for example, he has to go to a shop, throw out the trash, etc.). Help Polycarp determine the minimum number of walks he needs to do additionaly in the next n days so that Cormen will feel good during all the n days. You can assume that on the day before the first day and on the day after the n-th day Polycarp will go for a walk with Cormen exactly k times. Write a program that will find the minumum number of additional walks and the appropriate schedule — the sequence of integers b1, b2, ..., bn (bi ≥ ai), where bi means the total number of walks with the dog on the i-th day.
The first line contains two integers n and k (1 ≤ n, k ≤ 500) — the number of days and the minimum number of walks with Cormen for any two consecutive days. The second line contains integers a1, a2, ..., an (0 ≤ ai ≤ 500) — the number of walks with Cormen on the i-th day which Polycarp has already planned.
In the first line print the smallest number of additional walks that Polycarp should do during the next n days so that Cormen will feel good during all days. In the second line print n integers b1, b2, ..., bn, where bi — the total number of walks on the i-th day according to the found solutions (ai ≤ bi for all i from 1 to n). If there are multiple solutions, print any of them.
null
null
[{"input": "3 5\n2 0 1", "output": "4\n2 3 2"}, {"input": "3 1\n0 0 0", "output": "1\n0 1 0"}, {"input": "4 6\n2 4 3 5", "output": "0\n2 4 3 5"}]
1,000
["dp", "greedy"]
70
[{"input": "3 5\r\n2 0 1\r\n", "output": "4\r\n2 3 2\r\n"}, {"input": "3 1\r\n0 0 0\r\n", "output": "1\r\n0 1 0\r\n"}, {"input": "4 6\r\n2 4 3 5\r\n", "output": "0\r\n2 4 3 5\r\n"}, {"input": "5 1\r\n0 0 0 0 1\r\n", "output": "2\r\n0 1 0 1 1\r\n"}, {"input": "10 500\r\n164 44 238 205 373 249 87 30 239 90\r\n", "output": "903\r\n164 336 238 262 373 249 251 249 251 249\r\n"}, {"input": "1 1\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "5 1\r\n0 0 0 0 0\r\n", "output": "2\r\n0 1 0 1 0\r\n"}, {"input": "5 1\r\n0 0 0 0 1\r\n", "output": "2\r\n0 1 0 1 1\r\n"}, {"input": "5 2\r\n0 0 0 1 0\r\n", "output": "3\r\n0 2 0 2 0\r\n"}, {"input": "5 5\r\n1 4 0 0 0\r\n", "output": "6\r\n1 4 1 4 1\r\n"}, {"input": "5 10\r\n1 2 1 0 1\r\n", "output": "16\r\n1 9 1 9 1\r\n"}, {"input": "5 10\r\n0 1 0 1 0\r\n", "output": "18\r\n0 10 0 10 0\r\n"}, {"input": "10 5\r\n0 2 3 0 0 1 0 2 3 1\r\n", "output": "13\r\n0 5 3 2 3 2 3 2 3 2\r\n"}, {"input": "10 1\r\n0 0 0 0 0 0 0 0 1 0\r\n", "output": "4\r\n0 1 0 1 0 1 0 1 1 0\r\n"}, {"input": "10 436\r\n13 16 45 9 10 17 5 26 10 12\r\n", "output": "2017\r\n13 423 45 391 45 391 45 391 45 391\r\n"}, {"input": "10 438\r\n71 160 43 326 128 35 41 247 30 49\r\n", "output": "1060\r\n71 367 71 367 128 310 128 310 128 310\r\n"}, {"input": "10 431\r\n121 24 93 59 243 147 1 254 75 168\r\n", "output": "1036\r\n121 310 121 310 243 188 243 254 177 254\r\n"}, {"input": "10 10\r\n0 0 0 0 0 0 0 0 0 0\r\n", "output": "50\r\n0 10 0 10 0 10 0 10 0 10\r\n"}, {"input": "10 10\r\n0 0 1 0 0 0 1 0 0 0\r\n", "output": "48\r\n0 10 1 9 1 9 1 9 1 9\r\n"}, {"input": "10 10\r\n0 0 0 1 0 0 1 0 0 0\r\n", "output": "48\r\n0 10 0 10 0 10 1 9 1 9\r\n"}, {"input": "10 10\r\n1 1 0 2 0 1 1 1 2 0\r\n", "output": "41\r\n1 9 1 9 1 9 1 9 2 8\r\n"}, {"input": "10 10\r\n1 2 2 0 0 2 0 1 0 0\r\n", "output": "42\r\n1 9 2 8 2 8 2 8 2 8\r\n"}, {"input": "10 10\r\n1 0 1 0 0 5 2 0 0 1\r\n", "output": "40\r\n1 9 1 9 1 9 2 8 2 8\r\n"}, {"input": "10 10\r\n2 3 5 0 2 0 15 6 5 0\r\n", "output": "23\r\n2 8 5 5 5 5 15 6 5 5\r\n"}, {"input": "10 10\r\n16 15 4 10 14 2 18 11 24 5\r\n", "output": "0\r\n16 15 4 10 14 2 18 11 24 5\r\n"}, {"input": "100 100\r\n48 19 63 8 18 22 5 5 12 7 9 37 17 22 58 14 53 25 24 16 22 36 4 2 9 63 52 43 22 72 0 9 12 26 50 1 21 9 40 9 5 6 2 24 1 88 50 7 9 1 3 16 0 17 3 32 47 9 32 87 20 3 45 41 16 43 41 31 28 30 2 31 72 16 74 59 20 34 25 18 48 10 34 20 22 16 3 32 8 34 8 4 45 65 48 42 1 45 11 15\r\n", "output": "2588\r\n48 52 63 37 63 37 63 37 63 37 63 37 63 37 63 37 63 37 63 37 63 37 63 37 63 63 52 48 52 72 28 72 28 72 50 50 50 50 50 50 50 50 50 50 50 88 50 50 50 50 50 50 50 50 50 50 50 50 50 87 20 80 45 55 45 55 45 55 45 55 45 55 72 28 74 59 41 59 41 59 48 52 48 52 48 52 48 52 48 52 48 52 48 65 48 52 48 52 48 52\r\n"}, {"input": "100 200\r\n28 52 65 37 1 64 13 57 44 12 37 0 9 68 17 5 28 4 2 12 8 47 7 33 1 27 50 59 9 0 4 27 31 31 49 1 35 43 36 12 5 0 49 40 19 12 39 3 41 25 19 15 57 24 3 9 4 31 42 55 11 13 1 8 0 25 34 52 47 59 74 43 36 47 2 3 1 13 56 48 42 24 4 32 12 3 33 12 14 14 84 32 1 3 8 49 9 18 43 43\r\n", "output": "7390\r\n28 172 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 74 126 74 126 74 126 74 126 74 126 74 126 74 126 74 126 74 126 74 126 84 116 84 116 84 116 84 116 84 116\r\n"}, {"input": "100 10\r\n1 2 7 0 2 0 0 0 2 5 3 2 2 1 0 7 1 6 1 1 5 1 2 3 5 0 0 0 0 0 1 0 1 0 2 1 3 0 1 1 0 0 3 1 6 3 2 2 1 3 1 0 9 1 3 2 3 0 5 1 0 5 5 5 2 1 3 0 1 3 5 2 4 4 1 2 3 0 2 1 3 6 4 3 1 0 9 1 0 3 3 6 7 2 5 2 2 6 0 2\r\n", "output": "288\r\n1 9 7 3 7 3 7 3 7 5 5 5 5 5 5 7 3 7 3 7 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 4 6 4 6 4 6 4 9 1 9 2 8 2 8 2 8 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 4 6 4 6 9 1 9 3 7 6 7 3 7 3 7 6 4 6\r\n"}, {"input": "100 500\r\n207 27 83 171 129 204 11 55 58 115 43 280 208 169 23 79 36 59 132 28 13 136 246 134 29 135 176 21 155 175 127 288 68 68 41 156 194 31 44 131 30 31 89 46 180 184 12 29 2 58 70 157 329 294 126 55 79 19 125 15 39 30 2 137 36 151 5 246 176 1 158 31 4 99 192 200 124 66 10 195 180 165 8 79 257 68 5 175 43 141 0 106 38 32 0 56 33 221 144 226\r\n", "output": "14863\r\n207 293 207 293 207 293 207 293 207 293 207 293 208 292 208 292 208 292 208 292 208 292 246 254 246 254 246 254 246 254 246 288 212 288 212 288 212 288 212 288 212 288 212 288 212 288 212 288 212 288 212 288 329 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 257 243 257 243 257 243 257 243 257 243 257 243 257 243 257 243\r\n"}, {"input": "100 500\r\n64 140 15 221 24 106 73 30 275 97 296 55 5 30 47 199 130 44 72 170 7 204 359 40 128 117 45 192 344 112 0 11 196 78 73 53 222 93 88 151 99 283 60 71 4 87 226 46 66 74 23 89 77 60 397 181 0 101 358 54 124 155 19 218 9 140 161 130 308 85 103 85 300 128 19 108 225 136 100 54 30 24 129 245 128 88 160 120 51 154 19 129 114 32 256 30 102 207 115 49\r\n", "output": "13634\r\n64 436 64 436 64 436 73 427 275 225 296 204 296 204 296 204 296 204 296 204 296 204 359 141 359 141 359 192 344 156 344 156 344 156 344 156 344 156 344 156 344 283 217 283 217 283 226 274 226 274 226 274 226 274 397 181 319 181 358 142 358 155 345 218 282 218 282 218 308 192 308 192 308 192 308 192 308 192 308 192 308 192 308 245 255 245 255 245 255 245 255 245 255 245 256 244 256 244 256 244\r\n"}, {"input": "1 500\r\n500\r\n", "output": "0\r\n500\r\n"}, {"input": "2 1\r\n0 0\r\n", "output": "1\r\n0 1\r\n"}, {"input": "1 10\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "1 4\r\n2\r\n", "output": "0\r\n2\r\n"}, {"input": "1 10\r\n2\r\n", "output": "0\r\n2\r\n"}, {"input": "1 10\r\n0\r\n", "output": "0\r\n0\r\n"}, {"input": "1 5\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "1 2\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "1 5\r\n2\r\n", "output": "0\r\n2\r\n"}, {"input": "1 3\r\n0\r\n", "output": "0\r\n0\r\n"}, {"input": "1 3\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "1 5\r\n0\r\n", "output": "0\r\n0\r\n"}, {"input": "1 7\r\n0\r\n", "output": "0\r\n0\r\n"}, {"input": "1 7\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "1 3\r\n2\r\n", "output": "0\r\n2\r\n"}, {"input": "1 5\r\n3\r\n", "output": "0\r\n3\r\n"}, {"input": "1 4\r\n3\r\n", "output": "0\r\n3\r\n"}, {"input": "1 6\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "1 6\r\n2\r\n", "output": "0\r\n2\r\n"}, {"input": "1 500\r\n0\r\n", "output": "0\r\n0\r\n"}, {"input": "3 7\r\n2 3 1\r\n", "output": "3\r\n2 5 2\r\n"}, {"input": "1 10\r\n5\r\n", "output": "0\r\n5\r\n"}, {"input": "5 10\r\n1 2 3 4 5\r\n", "output": "10\r\n1 9 3 7 5\r\n"}, {"input": "2 6\r\n1 2\r\n", "output": "3\r\n1 5\r\n"}, {"input": "1 10\r\n3\r\n", "output": "0\r\n3\r\n"}, {"input": "1 6\r\n3\r\n", "output": "0\r\n3\r\n"}, {"input": "1 100\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "1 7\r\n2\r\n", "output": "0\r\n2\r\n"}, {"input": "2 10\r\n1 2\r\n", "output": "7\r\n1 9\r\n"}, {"input": "1 9\r\n1\r\n", "output": "0\r\n1\r\n"}]
false
stdio
null
true
732/B
732
B
Python 3
TESTS
2
31
0
208645392
def solve(): _, k = map(int, input().split()) walks: list[int] = [int(i) for i in input().split()] additional_walks: int = 0 for i in range(1, len(walks)): aux: int = k - walks[i] - walks[i - 1] additional_walks += max(0, aux) walks[i] += aux print(additional_walks) print(*walks, sep=" ") def main(): solve() if __name__ == "__main__": main()
70
46
0
198317679
n,k = map(int, input().split()) a= list(map(int, input().split())) r= 0 for i in range(1,n): if a[i-1] + a[i] < k: r+= k -(a[i-1] +a[i]) a[i] = k -a[i-1] print(r) print(*a)
Codeforces Round 377 (Div. 2)
CF
2,016
1
256
Cormen — The Best Friend Of a Man
Recently a dog was bought for Polycarp. The dog's name is Cormen. Now Polycarp has a lot of troubles. For example, Cormen likes going for a walk. Empirically Polycarp learned that the dog needs at least k walks for any two consecutive days in order to feel good. For example, if k = 5 and yesterday Polycarp went for a walk with Cormen 2 times, today he has to go for a walk at least 3 times. Polycarp analysed all his affairs over the next n days and made a sequence of n integers a1, a2, ..., an, where ai is the number of times Polycarp will walk with the dog on the i-th day while doing all his affairs (for example, he has to go to a shop, throw out the trash, etc.). Help Polycarp determine the minimum number of walks he needs to do additionaly in the next n days so that Cormen will feel good during all the n days. You can assume that on the day before the first day and on the day after the n-th day Polycarp will go for a walk with Cormen exactly k times. Write a program that will find the minumum number of additional walks and the appropriate schedule — the sequence of integers b1, b2, ..., bn (bi ≥ ai), where bi means the total number of walks with the dog on the i-th day.
The first line contains two integers n and k (1 ≤ n, k ≤ 500) — the number of days and the minimum number of walks with Cormen for any two consecutive days. The second line contains integers a1, a2, ..., an (0 ≤ ai ≤ 500) — the number of walks with Cormen on the i-th day which Polycarp has already planned.
In the first line print the smallest number of additional walks that Polycarp should do during the next n days so that Cormen will feel good during all days. In the second line print n integers b1, b2, ..., bn, where bi — the total number of walks on the i-th day according to the found solutions (ai ≤ bi for all i from 1 to n). If there are multiple solutions, print any of them.
null
null
[{"input": "3 5\n2 0 1", "output": "4\n2 3 2"}, {"input": "3 1\n0 0 0", "output": "1\n0 1 0"}, {"input": "4 6\n2 4 3 5", "output": "0\n2 4 3 5"}]
1,000
["dp", "greedy"]
70
[{"input": "3 5\r\n2 0 1\r\n", "output": "4\r\n2 3 2\r\n"}, {"input": "3 1\r\n0 0 0\r\n", "output": "1\r\n0 1 0\r\n"}, {"input": "4 6\r\n2 4 3 5\r\n", "output": "0\r\n2 4 3 5\r\n"}, {"input": "5 1\r\n0 0 0 0 1\r\n", "output": "2\r\n0 1 0 1 1\r\n"}, {"input": "10 500\r\n164 44 238 205 373 249 87 30 239 90\r\n", "output": "903\r\n164 336 238 262 373 249 251 249 251 249\r\n"}, {"input": "1 1\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "5 1\r\n0 0 0 0 0\r\n", "output": "2\r\n0 1 0 1 0\r\n"}, {"input": "5 1\r\n0 0 0 0 1\r\n", "output": "2\r\n0 1 0 1 1\r\n"}, {"input": "5 2\r\n0 0 0 1 0\r\n", "output": "3\r\n0 2 0 2 0\r\n"}, {"input": "5 5\r\n1 4 0 0 0\r\n", "output": "6\r\n1 4 1 4 1\r\n"}, {"input": "5 10\r\n1 2 1 0 1\r\n", "output": "16\r\n1 9 1 9 1\r\n"}, {"input": "5 10\r\n0 1 0 1 0\r\n", "output": "18\r\n0 10 0 10 0\r\n"}, {"input": "10 5\r\n0 2 3 0 0 1 0 2 3 1\r\n", "output": "13\r\n0 5 3 2 3 2 3 2 3 2\r\n"}, {"input": "10 1\r\n0 0 0 0 0 0 0 0 1 0\r\n", "output": "4\r\n0 1 0 1 0 1 0 1 1 0\r\n"}, {"input": "10 436\r\n13 16 45 9 10 17 5 26 10 12\r\n", "output": "2017\r\n13 423 45 391 45 391 45 391 45 391\r\n"}, {"input": "10 438\r\n71 160 43 326 128 35 41 247 30 49\r\n", "output": "1060\r\n71 367 71 367 128 310 128 310 128 310\r\n"}, {"input": "10 431\r\n121 24 93 59 243 147 1 254 75 168\r\n", "output": "1036\r\n121 310 121 310 243 188 243 254 177 254\r\n"}, {"input": "10 10\r\n0 0 0 0 0 0 0 0 0 0\r\n", "output": "50\r\n0 10 0 10 0 10 0 10 0 10\r\n"}, {"input": "10 10\r\n0 0 1 0 0 0 1 0 0 0\r\n", "output": "48\r\n0 10 1 9 1 9 1 9 1 9\r\n"}, {"input": "10 10\r\n0 0 0 1 0 0 1 0 0 0\r\n", "output": "48\r\n0 10 0 10 0 10 1 9 1 9\r\n"}, {"input": "10 10\r\n1 1 0 2 0 1 1 1 2 0\r\n", "output": "41\r\n1 9 1 9 1 9 1 9 2 8\r\n"}, {"input": "10 10\r\n1 2 2 0 0 2 0 1 0 0\r\n", "output": "42\r\n1 9 2 8 2 8 2 8 2 8\r\n"}, {"input": "10 10\r\n1 0 1 0 0 5 2 0 0 1\r\n", "output": "40\r\n1 9 1 9 1 9 2 8 2 8\r\n"}, {"input": "10 10\r\n2 3 5 0 2 0 15 6 5 0\r\n", "output": "23\r\n2 8 5 5 5 5 15 6 5 5\r\n"}, {"input": "10 10\r\n16 15 4 10 14 2 18 11 24 5\r\n", "output": "0\r\n16 15 4 10 14 2 18 11 24 5\r\n"}, {"input": "100 100\r\n48 19 63 8 18 22 5 5 12 7 9 37 17 22 58 14 53 25 24 16 22 36 4 2 9 63 52 43 22 72 0 9 12 26 50 1 21 9 40 9 5 6 2 24 1 88 50 7 9 1 3 16 0 17 3 32 47 9 32 87 20 3 45 41 16 43 41 31 28 30 2 31 72 16 74 59 20 34 25 18 48 10 34 20 22 16 3 32 8 34 8 4 45 65 48 42 1 45 11 15\r\n", "output": "2588\r\n48 52 63 37 63 37 63 37 63 37 63 37 63 37 63 37 63 37 63 37 63 37 63 37 63 63 52 48 52 72 28 72 28 72 50 50 50 50 50 50 50 50 50 50 50 88 50 50 50 50 50 50 50 50 50 50 50 50 50 87 20 80 45 55 45 55 45 55 45 55 45 55 72 28 74 59 41 59 41 59 48 52 48 52 48 52 48 52 48 52 48 52 48 65 48 52 48 52 48 52\r\n"}, {"input": "100 200\r\n28 52 65 37 1 64 13 57 44 12 37 0 9 68 17 5 28 4 2 12 8 47 7 33 1 27 50 59 9 0 4 27 31 31 49 1 35 43 36 12 5 0 49 40 19 12 39 3 41 25 19 15 57 24 3 9 4 31 42 55 11 13 1 8 0 25 34 52 47 59 74 43 36 47 2 3 1 13 56 48 42 24 4 32 12 3 33 12 14 14 84 32 1 3 8 49 9 18 43 43\r\n", "output": "7390\r\n28 172 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 74 126 74 126 74 126 74 126 74 126 74 126 74 126 74 126 74 126 74 126 84 116 84 116 84 116 84 116 84 116\r\n"}, {"input": "100 10\r\n1 2 7 0 2 0 0 0 2 5 3 2 2 1 0 7 1 6 1 1 5 1 2 3 5 0 0 0 0 0 1 0 1 0 2 1 3 0 1 1 0 0 3 1 6 3 2 2 1 3 1 0 9 1 3 2 3 0 5 1 0 5 5 5 2 1 3 0 1 3 5 2 4 4 1 2 3 0 2 1 3 6 4 3 1 0 9 1 0 3 3 6 7 2 5 2 2 6 0 2\r\n", "output": "288\r\n1 9 7 3 7 3 7 3 7 5 5 5 5 5 5 7 3 7 3 7 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 4 6 4 6 4 6 4 9 1 9 2 8 2 8 2 8 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 4 6 4 6 9 1 9 3 7 6 7 3 7 3 7 6 4 6\r\n"}, {"input": "100 500\r\n207 27 83 171 129 204 11 55 58 115 43 280 208 169 23 79 36 59 132 28 13 136 246 134 29 135 176 21 155 175 127 288 68 68 41 156 194 31 44 131 30 31 89 46 180 184 12 29 2 58 70 157 329 294 126 55 79 19 125 15 39 30 2 137 36 151 5 246 176 1 158 31 4 99 192 200 124 66 10 195 180 165 8 79 257 68 5 175 43 141 0 106 38 32 0 56 33 221 144 226\r\n", "output": "14863\r\n207 293 207 293 207 293 207 293 207 293 207 293 208 292 208 292 208 292 208 292 208 292 246 254 246 254 246 254 246 254 246 288 212 288 212 288 212 288 212 288 212 288 212 288 212 288 212 288 212 288 212 288 329 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 257 243 257 243 257 243 257 243 257 243 257 243 257 243 257 243\r\n"}, {"input": "100 500\r\n64 140 15 221 24 106 73 30 275 97 296 55 5 30 47 199 130 44 72 170 7 204 359 40 128 117 45 192 344 112 0 11 196 78 73 53 222 93 88 151 99 283 60 71 4 87 226 46 66 74 23 89 77 60 397 181 0 101 358 54 124 155 19 218 9 140 161 130 308 85 103 85 300 128 19 108 225 136 100 54 30 24 129 245 128 88 160 120 51 154 19 129 114 32 256 30 102 207 115 49\r\n", "output": "13634\r\n64 436 64 436 64 436 73 427 275 225 296 204 296 204 296 204 296 204 296 204 296 204 359 141 359 141 359 192 344 156 344 156 344 156 344 156 344 156 344 156 344 283 217 283 217 283 226 274 226 274 226 274 226 274 397 181 319 181 358 142 358 155 345 218 282 218 282 218 308 192 308 192 308 192 308 192 308 192 308 192 308 192 308 245 255 245 255 245 255 245 255 245 255 245 256 244 256 244 256 244\r\n"}, {"input": "1 500\r\n500\r\n", "output": "0\r\n500\r\n"}, {"input": "2 1\r\n0 0\r\n", "output": "1\r\n0 1\r\n"}, {"input": "1 10\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "1 4\r\n2\r\n", "output": "0\r\n2\r\n"}, {"input": "1 10\r\n2\r\n", "output": "0\r\n2\r\n"}, {"input": "1 10\r\n0\r\n", "output": "0\r\n0\r\n"}, {"input": "1 5\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "1 2\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "1 5\r\n2\r\n", "output": "0\r\n2\r\n"}, {"input": "1 3\r\n0\r\n", "output": "0\r\n0\r\n"}, {"input": "1 3\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "1 5\r\n0\r\n", "output": "0\r\n0\r\n"}, {"input": "1 7\r\n0\r\n", "output": "0\r\n0\r\n"}, {"input": "1 7\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "1 3\r\n2\r\n", "output": "0\r\n2\r\n"}, {"input": "1 5\r\n3\r\n", "output": "0\r\n3\r\n"}, {"input": "1 4\r\n3\r\n", "output": "0\r\n3\r\n"}, {"input": "1 6\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "1 6\r\n2\r\n", "output": "0\r\n2\r\n"}, {"input": "1 500\r\n0\r\n", "output": "0\r\n0\r\n"}, {"input": "3 7\r\n2 3 1\r\n", "output": "3\r\n2 5 2\r\n"}, {"input": "1 10\r\n5\r\n", "output": "0\r\n5\r\n"}, {"input": "5 10\r\n1 2 3 4 5\r\n", "output": "10\r\n1 9 3 7 5\r\n"}, {"input": "2 6\r\n1 2\r\n", "output": "3\r\n1 5\r\n"}, {"input": "1 10\r\n3\r\n", "output": "0\r\n3\r\n"}, {"input": "1 6\r\n3\r\n", "output": "0\r\n3\r\n"}, {"input": "1 100\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "1 7\r\n2\r\n", "output": "0\r\n2\r\n"}, {"input": "2 10\r\n1 2\r\n", "output": "7\r\n1 9\r\n"}, {"input": "1 9\r\n1\r\n", "output": "0\r\n1\r\n"}]
false
stdio
null
true
732/B
732
B
Python 3
TESTS
2
31
0
194051939
days, all_walks = [int(x) for x in input().split()] planned_walks = [int(x) for x in input().split()] needed_walks = [0] * days needed_walks[0] = planned_walks[0] for i in range(1, days): needed_walks[i] = all_walks - needed_walks[i - 1] add_walks = sum(needed_walks) - sum(planned_walks) print(0 if add_walks <= 0 else add_walks) print(*needed_walks)
70
46
0
198890113
n,k = map(int,input().split()) a = [int(x) for x in input().split()] a.append(k) ans = [a[0]] count = 0 for i in range(n): if a[i] + a[i+1] < k: dob = k - (a[i] + a[i+1]) count += dob a[i+1] += dob ans.append(a[i+1]) else: ans.append(a[i+1]) ans.pop() print(count) print(*ans)
Codeforces Round 377 (Div. 2)
CF
2,016
1
256
Cormen — The Best Friend Of a Man
Recently a dog was bought for Polycarp. The dog's name is Cormen. Now Polycarp has a lot of troubles. For example, Cormen likes going for a walk. Empirically Polycarp learned that the dog needs at least k walks for any two consecutive days in order to feel good. For example, if k = 5 and yesterday Polycarp went for a walk with Cormen 2 times, today he has to go for a walk at least 3 times. Polycarp analysed all his affairs over the next n days and made a sequence of n integers a1, a2, ..., an, where ai is the number of times Polycarp will walk with the dog on the i-th day while doing all his affairs (for example, he has to go to a shop, throw out the trash, etc.). Help Polycarp determine the minimum number of walks he needs to do additionaly in the next n days so that Cormen will feel good during all the n days. You can assume that on the day before the first day and on the day after the n-th day Polycarp will go for a walk with Cormen exactly k times. Write a program that will find the minumum number of additional walks and the appropriate schedule — the sequence of integers b1, b2, ..., bn (bi ≥ ai), where bi means the total number of walks with the dog on the i-th day.
The first line contains two integers n and k (1 ≤ n, k ≤ 500) — the number of days and the minimum number of walks with Cormen for any two consecutive days. The second line contains integers a1, a2, ..., an (0 ≤ ai ≤ 500) — the number of walks with Cormen on the i-th day which Polycarp has already planned.
In the first line print the smallest number of additional walks that Polycarp should do during the next n days so that Cormen will feel good during all days. In the second line print n integers b1, b2, ..., bn, where bi — the total number of walks on the i-th day according to the found solutions (ai ≤ bi for all i from 1 to n). If there are multiple solutions, print any of them.
null
null
[{"input": "3 5\n2 0 1", "output": "4\n2 3 2"}, {"input": "3 1\n0 0 0", "output": "1\n0 1 0"}, {"input": "4 6\n2 4 3 5", "output": "0\n2 4 3 5"}]
1,000
["dp", "greedy"]
70
[{"input": "3 5\r\n2 0 1\r\n", "output": "4\r\n2 3 2\r\n"}, {"input": "3 1\r\n0 0 0\r\n", "output": "1\r\n0 1 0\r\n"}, {"input": "4 6\r\n2 4 3 5\r\n", "output": "0\r\n2 4 3 5\r\n"}, {"input": "5 1\r\n0 0 0 0 1\r\n", "output": "2\r\n0 1 0 1 1\r\n"}, {"input": "10 500\r\n164 44 238 205 373 249 87 30 239 90\r\n", "output": "903\r\n164 336 238 262 373 249 251 249 251 249\r\n"}, {"input": "1 1\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "5 1\r\n0 0 0 0 0\r\n", "output": "2\r\n0 1 0 1 0\r\n"}, {"input": "5 1\r\n0 0 0 0 1\r\n", "output": "2\r\n0 1 0 1 1\r\n"}, {"input": "5 2\r\n0 0 0 1 0\r\n", "output": "3\r\n0 2 0 2 0\r\n"}, {"input": "5 5\r\n1 4 0 0 0\r\n", "output": "6\r\n1 4 1 4 1\r\n"}, {"input": "5 10\r\n1 2 1 0 1\r\n", "output": "16\r\n1 9 1 9 1\r\n"}, {"input": "5 10\r\n0 1 0 1 0\r\n", "output": "18\r\n0 10 0 10 0\r\n"}, {"input": "10 5\r\n0 2 3 0 0 1 0 2 3 1\r\n", "output": "13\r\n0 5 3 2 3 2 3 2 3 2\r\n"}, {"input": "10 1\r\n0 0 0 0 0 0 0 0 1 0\r\n", "output": "4\r\n0 1 0 1 0 1 0 1 1 0\r\n"}, {"input": "10 436\r\n13 16 45 9 10 17 5 26 10 12\r\n", "output": "2017\r\n13 423 45 391 45 391 45 391 45 391\r\n"}, {"input": "10 438\r\n71 160 43 326 128 35 41 247 30 49\r\n", "output": "1060\r\n71 367 71 367 128 310 128 310 128 310\r\n"}, {"input": "10 431\r\n121 24 93 59 243 147 1 254 75 168\r\n", "output": "1036\r\n121 310 121 310 243 188 243 254 177 254\r\n"}, {"input": "10 10\r\n0 0 0 0 0 0 0 0 0 0\r\n", "output": "50\r\n0 10 0 10 0 10 0 10 0 10\r\n"}, {"input": "10 10\r\n0 0 1 0 0 0 1 0 0 0\r\n", "output": "48\r\n0 10 1 9 1 9 1 9 1 9\r\n"}, {"input": "10 10\r\n0 0 0 1 0 0 1 0 0 0\r\n", "output": "48\r\n0 10 0 10 0 10 1 9 1 9\r\n"}, {"input": "10 10\r\n1 1 0 2 0 1 1 1 2 0\r\n", "output": "41\r\n1 9 1 9 1 9 1 9 2 8\r\n"}, {"input": "10 10\r\n1 2 2 0 0 2 0 1 0 0\r\n", "output": "42\r\n1 9 2 8 2 8 2 8 2 8\r\n"}, {"input": "10 10\r\n1 0 1 0 0 5 2 0 0 1\r\n", "output": "40\r\n1 9 1 9 1 9 2 8 2 8\r\n"}, {"input": "10 10\r\n2 3 5 0 2 0 15 6 5 0\r\n", "output": "23\r\n2 8 5 5 5 5 15 6 5 5\r\n"}, {"input": "10 10\r\n16 15 4 10 14 2 18 11 24 5\r\n", "output": "0\r\n16 15 4 10 14 2 18 11 24 5\r\n"}, {"input": "100 100\r\n48 19 63 8 18 22 5 5 12 7 9 37 17 22 58 14 53 25 24 16 22 36 4 2 9 63 52 43 22 72 0 9 12 26 50 1 21 9 40 9 5 6 2 24 1 88 50 7 9 1 3 16 0 17 3 32 47 9 32 87 20 3 45 41 16 43 41 31 28 30 2 31 72 16 74 59 20 34 25 18 48 10 34 20 22 16 3 32 8 34 8 4 45 65 48 42 1 45 11 15\r\n", "output": "2588\r\n48 52 63 37 63 37 63 37 63 37 63 37 63 37 63 37 63 37 63 37 63 37 63 37 63 63 52 48 52 72 28 72 28 72 50 50 50 50 50 50 50 50 50 50 50 88 50 50 50 50 50 50 50 50 50 50 50 50 50 87 20 80 45 55 45 55 45 55 45 55 45 55 72 28 74 59 41 59 41 59 48 52 48 52 48 52 48 52 48 52 48 52 48 65 48 52 48 52 48 52\r\n"}, {"input": "100 200\r\n28 52 65 37 1 64 13 57 44 12 37 0 9 68 17 5 28 4 2 12 8 47 7 33 1 27 50 59 9 0 4 27 31 31 49 1 35 43 36 12 5 0 49 40 19 12 39 3 41 25 19 15 57 24 3 9 4 31 42 55 11 13 1 8 0 25 34 52 47 59 74 43 36 47 2 3 1 13 56 48 42 24 4 32 12 3 33 12 14 14 84 32 1 3 8 49 9 18 43 43\r\n", "output": "7390\r\n28 172 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 74 126 74 126 74 126 74 126 74 126 74 126 74 126 74 126 74 126 74 126 84 116 84 116 84 116 84 116 84 116\r\n"}, {"input": "100 10\r\n1 2 7 0 2 0 0 0 2 5 3 2 2 1 0 7 1 6 1 1 5 1 2 3 5 0 0 0 0 0 1 0 1 0 2 1 3 0 1 1 0 0 3 1 6 3 2 2 1 3 1 0 9 1 3 2 3 0 5 1 0 5 5 5 2 1 3 0 1 3 5 2 4 4 1 2 3 0 2 1 3 6 4 3 1 0 9 1 0 3 3 6 7 2 5 2 2 6 0 2\r\n", "output": "288\r\n1 9 7 3 7 3 7 3 7 5 5 5 5 5 5 7 3 7 3 7 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 4 6 4 6 4 6 4 9 1 9 2 8 2 8 2 8 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 4 6 4 6 9 1 9 3 7 6 7 3 7 3 7 6 4 6\r\n"}, {"input": "100 500\r\n207 27 83 171 129 204 11 55 58 115 43 280 208 169 23 79 36 59 132 28 13 136 246 134 29 135 176 21 155 175 127 288 68 68 41 156 194 31 44 131 30 31 89 46 180 184 12 29 2 58 70 157 329 294 126 55 79 19 125 15 39 30 2 137 36 151 5 246 176 1 158 31 4 99 192 200 124 66 10 195 180 165 8 79 257 68 5 175 43 141 0 106 38 32 0 56 33 221 144 226\r\n", "output": "14863\r\n207 293 207 293 207 293 207 293 207 293 207 293 208 292 208 292 208 292 208 292 208 292 246 254 246 254 246 254 246 254 246 288 212 288 212 288 212 288 212 288 212 288 212 288 212 288 212 288 212 288 212 288 329 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 257 243 257 243 257 243 257 243 257 243 257 243 257 243 257 243\r\n"}, {"input": "100 500\r\n64 140 15 221 24 106 73 30 275 97 296 55 5 30 47 199 130 44 72 170 7 204 359 40 128 117 45 192 344 112 0 11 196 78 73 53 222 93 88 151 99 283 60 71 4 87 226 46 66 74 23 89 77 60 397 181 0 101 358 54 124 155 19 218 9 140 161 130 308 85 103 85 300 128 19 108 225 136 100 54 30 24 129 245 128 88 160 120 51 154 19 129 114 32 256 30 102 207 115 49\r\n", "output": "13634\r\n64 436 64 436 64 436 73 427 275 225 296 204 296 204 296 204 296 204 296 204 296 204 359 141 359 141 359 192 344 156 344 156 344 156 344 156 344 156 344 156 344 283 217 283 217 283 226 274 226 274 226 274 226 274 397 181 319 181 358 142 358 155 345 218 282 218 282 218 308 192 308 192 308 192 308 192 308 192 308 192 308 192 308 245 255 245 255 245 255 245 255 245 255 245 256 244 256 244 256 244\r\n"}, {"input": "1 500\r\n500\r\n", "output": "0\r\n500\r\n"}, {"input": "2 1\r\n0 0\r\n", "output": "1\r\n0 1\r\n"}, {"input": "1 10\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "1 4\r\n2\r\n", "output": "0\r\n2\r\n"}, {"input": "1 10\r\n2\r\n", "output": "0\r\n2\r\n"}, {"input": "1 10\r\n0\r\n", "output": "0\r\n0\r\n"}, {"input": "1 5\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "1 2\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "1 5\r\n2\r\n", "output": "0\r\n2\r\n"}, {"input": "1 3\r\n0\r\n", "output": "0\r\n0\r\n"}, {"input": "1 3\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "1 5\r\n0\r\n", "output": "0\r\n0\r\n"}, {"input": "1 7\r\n0\r\n", "output": "0\r\n0\r\n"}, {"input": "1 7\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "1 3\r\n2\r\n", "output": "0\r\n2\r\n"}, {"input": "1 5\r\n3\r\n", "output": "0\r\n3\r\n"}, {"input": "1 4\r\n3\r\n", "output": "0\r\n3\r\n"}, {"input": "1 6\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "1 6\r\n2\r\n", "output": "0\r\n2\r\n"}, {"input": "1 500\r\n0\r\n", "output": "0\r\n0\r\n"}, {"input": "3 7\r\n2 3 1\r\n", "output": "3\r\n2 5 2\r\n"}, {"input": "1 10\r\n5\r\n", "output": "0\r\n5\r\n"}, {"input": "5 10\r\n1 2 3 4 5\r\n", "output": "10\r\n1 9 3 7 5\r\n"}, {"input": "2 6\r\n1 2\r\n", "output": "3\r\n1 5\r\n"}, {"input": "1 10\r\n3\r\n", "output": "0\r\n3\r\n"}, {"input": "1 6\r\n3\r\n", "output": "0\r\n3\r\n"}, {"input": "1 100\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "1 7\r\n2\r\n", "output": "0\r\n2\r\n"}, {"input": "2 10\r\n1 2\r\n", "output": "7\r\n1 9\r\n"}, {"input": "1 9\r\n1\r\n", "output": "0\r\n1\r\n"}]
false
stdio
null
true
54/A
54
A
Python 3
TESTS
21
216
0
93130654
a,b=map(int,input().split()) s=a;x=[int(i) for i in input().split()[1:]] for i in range(1,b+1): o=0;p=0 for j in range(i,a+1,b): if j in x:p+=1 o+=1 s=min(s,o+len(x)-p) print(s)
65
92
0
164942097
import sys input = sys.stdin.readline n, k = map(int, input().split()) w = list(map(int, input().split())) c = w[0] if c == 0: print(n//k) else: s = set() d = sum([(j-i-1)//k for (i, j) in zip(w[1:], w[2:])]) d1 = (w[1]-1)//k d2 = (n-w[-1])//k print(d+d1+d2+c)
Codeforces Beta Round 50
CF
2,011
2
256
Presents
The Hedgehog likes to give presents to his friend, but no less he likes to receive them. Having received another present today, the Hedgehog suddenly understood that he has no place to put it as there was no room left on the special shelf in the cupboard. He will have to choose another shelf, but which one should he choose, how large should it be? In order to get to know this, the Hedgehog asks you to write him a program that will count the estimated number of presents that he will receive during the following N days. Besides, he is guided by the principle: - on each holiday day the Hedgehog will necessarily receive a present, - he receives presents at least every K days (i.e., if he received a present on the i-th day, he will receive the next present no later than on the i + K-th day). For the given N and K, as well as the list of holidays among the following N days count the minimal number of presents that could be given to the Hedgehog. The number of today's day is zero, and you should regard today's present as already given (i.e., you shouldn't count it in the answer).
The first line contains integers N and K (1 ≤ N ≤ 365, 1 ≤ K ≤ N). The second line contains a number C which represents the number of holidays (0 ≤ C ≤ N). Then in the same line follow C numbers ranging from 1 to N which are the numbers of holiday days. The numbers are given in the increasing order, without repeating numbers among them.
Print a single number — the minimal number of presents the Hedgehog will receive over the following N days.
null
null
[{"input": "5 2\n1 3", "output": "3"}, {"input": "10 1\n3 6 7 8", "output": "10"}]
1,300
["implementation"]
65
[{"input": "5 2\r\n1 3\r\n", "output": "3"}, {"input": "10 1\r\n3 6 7 8\r\n", "output": "10"}, {"input": "5 5\r\n1 3\r\n", "output": "1"}, {"input": "10 3\r\n3 3 6 9\r\n", "output": "3"}, {"input": "5 2\r\n0\r\n", "output": "2"}, {"input": "1 1\r\n0\r\n", "output": "1"}, {"input": "5 1\r\n0\r\n", "output": "5"}, {"input": "5 1\r\n1 2\r\n", "output": "5"}, {"input": "5 2\r\n0\r\n", "output": "2"}, {"input": "10 3\r\n2 4 8\r\n", "output": "4"}, {"input": "10 1\r\n0\r\n", "output": "10"}, {"input": "10 2\r\n1 5\r\n", "output": "5"}, {"input": "10 1\r\n0\r\n", "output": "10"}, {"input": "10 1\r\n0\r\n", "output": "10"}, {"input": "15 5\r\n0\r\n", "output": "3"}, {"input": "15 1\r\n1 3\r\n", "output": "15"}, {"input": "15 2\r\n1 10\r\n", "output": "7"}, {"input": "15 1\r\n0\r\n", "output": "15"}, {"input": "15 3\r\n1 11\r\n", "output": "5"}, {"input": "20 1\r\n3 7 9 20\r\n", "output": "20"}, {"input": "20 3\r\n1 11\r\n", "output": "7"}, {"input": "20 2\r\n6 6 9 10 15 19 20\r\n", "output": "12"}, {"input": "20 1\r\n0\r\n", "output": "20"}, {"input": "20 1\r\n1 13\r\n", "output": "20"}, {"input": "25 1\r\n9 2 6 8 10 14 15 17 18 23\r\n", "output": "25"}, {"input": "25 1\r\n0\r\n", "output": "25"}, {"input": "25 1\r\n4 8 10 13 24\r\n", "output": "25"}, {"input": "25 1\r\n1 14\r\n", "output": "25"}, {"input": "25 1\r\n0\r\n", "output": "25"}, {"input": "100 3\r\n0\r\n", "output": "33"}, {"input": "100 10\r\n0\r\n", "output": "10"}, {"input": "100 23\r\n22 2 9 18 22 23 30 44 50 55 58 61 70 71 73 76 79 82 85 88 94 95 99\r\n", "output": "22"}, {"input": "100 5\r\n10 2 17 21 34 52 58 60 64 68 95\r\n", "output": "24"}, {"input": "100 4\r\n2 29 63\r\n", "output": "26"}, {"input": "150 16\r\n9 19 31 47 53 57 96 105 108 120\r\n", "output": "13"}, {"input": "150 52\r\n5 11 37 60 67 86\r\n", "output": "6"}, {"input": "150 4\r\n7 21 54 106 108 109 119 123\r\n", "output": "40"}, {"input": "150 3\r\n0\r\n", "output": "50"}, {"input": "150 21\r\n21 22 26 30 36 39 52 59 62 66 68 78 86 92 96 103 108 113 118 119 125 139\r\n", "output": "22"}, {"input": "300 15\r\n14 3 38 52 57 142 157 175 201 209 238 258 288 294 299\r\n", "output": "26"}, {"input": "300 2\r\n14 29 94 122 123 158 160 164 191 200 202 208 246 272 286\r\n", "output": "153"}, {"input": "300 5\r\n16 22 38 72 78 108 116 140 147 160 189 209 214 227 252 294 300\r\n", "output": "66"}, {"input": "300 8\r\n4 27 76 155 260\r\n", "output": "40"}, {"input": "300 24\r\n20 18 76 80 81 85 103 110 112 129 145 151 172 180 184 201 205 241 257 268 276\r\n", "output": "24"}, {"input": "350 22\r\n11 38 111 115 176 194 204 207 231 274 307 348\r\n", "output": "21"}, {"input": "350 26\r\n10 13 16 81 99 144 191 223 258 316 329\r\n", "output": "18"}, {"input": "350 16\r\n12 31 76 103 116 191 201 241 256 260 291 306 336\r\n", "output": "24"}, {"input": "350 28\r\n5 23 104 135 305 331\r\n", "output": "14"}, {"input": "365 34\r\n6 80 94 208 256 325 349\r\n", "output": "14"}, {"input": "365 19\r\n7 47 114 139 210 226 266 279\r\n", "output": "22"}, {"input": "365 8\r\n32 1 13 22 25 33 72 80 86 96 117 132 145 146 156 176 177 179 188 198 203 218 225 235 253 256 267 279 286 294 303 333 363\r\n", "output": "61"}, {"input": "365 8\r\n55 3 12 26 28 36 45 47 59 61 65 82 90 103 109 114 117 121 123 126 134 142 144 146 151 154 168 175 189 193 195 197 199 210 212 214 230 232 241 248 254 267 271 291 304 306 308 311 315 317 318 334 335 346 354 365\r\n", "output": "74"}, {"input": "365 2\r\n2 96 241\r\n", "output": "183"}, {"input": "365 42\r\n10 8 66 77 148 161 183 231 301 340 350\r\n", "output": "14"}, {"input": "365 40\r\n30 1 14 21 31 32 36 56 59 68 96 119 131 137 166 179 181 202 235 248 272 294 309 315 322 327 334 341 347 362 365\r\n", "output": "30"}, {"input": "365 31\r\n19 13 18 27 33 46 58 86 114 178 187 198 228 233 240 255 277 332 348 351\r\n", "output": "22"}, {"input": "365 54\r\n21 28 42 56 65 66 67 76 81 85 89 123 132 136 153 195 215 249 294 296 300 355\r\n", "output": "22"}, {"input": "365 5\r\n5 10 31 121 235 322\r\n", "output": "74"}, {"input": "365 81\r\n2 1 75\r\n", "output": "5"}, {"input": "365 21\r\n4 1 17 344 345\r\n", "output": "19"}, {"input": "11 2\r\n5 3 6 7 9 10\r\n", "output": "7"}, {"input": "5 3\r\n2 2 4\r\n", "output": "2"}, {"input": "362 360\r\n0\r\n", "output": "1"}, {"input": "18 4\r\n4 1 9 10 18\r\n", "output": "6"}]
false
stdio
null
true
216/B
216
B
Python 3
TESTS
23
216
307,200
91609062
def union(pair): global rank,size u,v=pair[0],pair[1] if(find(u)!=find(v)): rank[u]=v size[v]=size[u]+1 else: return size[v]-size[u]+1 def find(u): global rank while(u!=rank[u]): u=find(rank[u]) return u def proB(arr,n): ans=0 for i in arr: cou=union(i) if(cou!=None): if(cou%2!=0): ans+=1 if( (n-ans)%2==1): ans+=1 return ans arr1=list(map(int,input().split())) n,m=arr1[0],arr1[1] rank=[0]*(n+1) for i in range(1,n+1): rank[i]=i size=[0]*(n+1) temp=[] for i in range(m): arr=list(map(int,input().split())) temp.append(arr) print(proB(temp,n))
56
92
0
153869030
n, m = map(int, input().split()) adj_list = [[] for i in range(n)] for i in range(m): x, y = map(int, input().split()) adj_list[x-1].append(y-1) adj_list[y-1].append(x-1) visited = [False for i in range(n)] def dfs(cur, par, arr): arr.add(cur) if visited[cur]: return True visited[cur] = True for i in adj_list[cur]: if i != par: if dfs(i, cur, arr): return True return False i = 0 res = 0 while i < n: if not visited[i]: arr = set() if dfs(i, -1, arr): if len(arr) % 2 == 1: res += 1 i += 1 n -= res if n % 2 == 1: print(res + 1) else: print(res)
Codeforces Round 133 (Div. 2)
CF
2,012
2
256
Forming Teams
One day n students come to the stadium. They want to play football, and for that they need to split into teams, the teams must have an equal number of people. We know that this group of people has archenemies. Each student has at most two archenemies. Besides, if student A is an archenemy to student B, then student B is an archenemy to student A. The students want to split so as no two archenemies were in one team. If splitting in the required manner is impossible, some students will have to sit on the bench. Determine the minimum number of students you will have to send to the bench in order to form the two teams in the described manner and begin the game at last.
The first line contains two integers n and m (2 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of students and the number of pairs of archenemies correspondingly. Next m lines describe enmity between students. Each enmity is described as two numbers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the indexes of the students who are enemies to each other. Each enmity occurs in the list exactly once. It is guaranteed that each student has no more than two archenemies. You can consider the students indexed in some manner with distinct integers from 1 to n.
Print a single integer — the minimum number of students you will have to send to the bench in order to start the game.
null
null
[{"input": "5 4\n1 2\n2 4\n5 3\n1 4", "output": "1"}, {"input": "6 2\n1 4\n3 4", "output": "0"}, {"input": "6 6\n1 2\n2 3\n3 1\n4 5\n5 6\n6 4", "output": "2"}]
1,700
["dfs and similar", "implementation"]
56
[{"input": "5 4\r\n1 2\r\n2 4\r\n5 3\r\n1 4\r\n", "output": "1"}, {"input": "6 2\r\n1 4\r\n3 4\r\n", "output": "0"}, {"input": "6 6\r\n1 2\r\n2 3\r\n3 1\r\n4 5\r\n5 6\r\n6 4\r\n", "output": "2"}, {"input": "5 1\r\n1 2\r\n", "output": "1"}, {"input": "8 8\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 6\r\n6 7\r\n7 8\r\n8 1\r\n", "output": "0"}, {"input": "28 3\r\n15 3\r\n10 19\r\n17 25\r\n", "output": "0"}, {"input": "2 1\r\n1 2\r\n", "output": "0"}, {"input": "3 1\r\n2 3\r\n", "output": "1"}, {"input": "3 2\r\n1 2\r\n3 2\r\n", "output": "1"}, {"input": "3 3\r\n1 2\r\n1 3\r\n2 3\r\n", "output": "1"}, {"input": "4 1\r\n1 4\r\n", "output": "0"}, {"input": "4 2\r\n4 1\r\n2 1\r\n", "output": "0"}, {"input": "4 3\r\n1 3\r\n3 2\r\n2 4\r\n", "output": "0"}, {"input": "4 3\r\n3 2\r\n4 2\r\n4 3\r\n", "output": "2"}, {"input": "5 3\r\n4 2\r\n3 4\r\n5 1\r\n", "output": "1"}, {"input": "10 7\r\n8 9\r\n3 6\r\n2 4\r\n4 1\r\n1 3\r\n2 7\r\n7 10\r\n", "output": "0"}, {"input": "29 20\r\n15 9\r\n21 15\r\n14 12\r\n12 16\r\n3 28\r\n5 13\r\n19 1\r\n19 21\r\n23 17\r\n27 9\r\n26 10\r\n20 5\r\n8 16\r\n11 6\r\n4 22\r\n29 22\r\n29 11\r\n14 17\r\n28 6\r\n1 23\r\n", "output": "1"}, {"input": "89 30\r\n86 72\r\n43 16\r\n32 80\r\n17 79\r\n29 8\r\n89 37\r\n84 65\r\n3 41\r\n55 79\r\n33 56\r\n60 40\r\n43 45\r\n59 38\r\n26 23\r\n66 61\r\n81 30\r\n65 25\r\n13 71\r\n25 8\r\n56 59\r\n46 13\r\n22 30\r\n87 3\r\n26 32\r\n75 44\r\n48 87\r\n47 4\r\n63 21\r\n36 6\r\n42 86\r\n", "output": "1"}, {"input": "100 1\r\n3 87\r\n", "output": "0"}, {"input": "100 10\r\n88 82\r\n5 78\r\n66 31\r\n65 100\r\n92 25\r\n71 62\r\n47 31\r\n17 67\r\n69 68\r\n59 49\r\n", "output": "0"}, {"input": "10 9\r\n5 10\r\n3 2\r\n8 6\r\n4 5\r\n4 10\r\n6 1\r\n1 8\r\n9 2\r\n3 9\r\n", "output": "4"}, {"input": "19 16\r\n2 16\r\n7 10\r\n17 16\r\n17 14\r\n1 5\r\n19 6\r\n11 13\r\n15 19\r\n7 9\r\n13 5\r\n4 6\r\n1 11\r\n12 9\r\n10 12\r\n2 14\r\n4 15\r\n", "output": "1"}, {"input": "33 33\r\n2 16\r\n28 20\r\n13 9\r\n4 22\r\n18 1\r\n6 12\r\n13 29\r\n32 1\r\n17 15\r\n10 7\r\n6 15\r\n16 5\r\n11 10\r\n31 29\r\n25 8\r\n23 21\r\n14 32\r\n8 2\r\n19 3\r\n11 4\r\n21 25\r\n31 30\r\n33 5\r\n26 7\r\n27 26\r\n27 12\r\n30 24\r\n33 17\r\n28 22\r\n18 24\r\n19 9\r\n3 23\r\n14 20\r\n", "output": "1"}, {"input": "10 8\r\n8 3\r\n9 7\r\n6 1\r\n10 9\r\n2 6\r\n2 1\r\n3 4\r\n4 8\r\n", "output": "2"}, {"input": "20 12\r\n16 20\r\n8 3\r\n20 5\r\n5 10\r\n17 7\r\n13 2\r\n18 9\r\n17 18\r\n1 6\r\n14 4\r\n11 12\r\n10 16\r\n", "output": "0"}, {"input": "35 21\r\n15 3\r\n13 5\r\n2 28\r\n26 35\r\n9 10\r\n22 18\r\n17 1\r\n31 32\r\n35 33\r\n5 15\r\n14 24\r\n29 12\r\n16 2\r\n14 10\r\n7 4\r\n29 4\r\n23 27\r\n30 34\r\n19 26\r\n23 11\r\n25 21\r\n", "output": "1"}, {"input": "49 36\r\n17 47\r\n19 27\r\n41 23\r\n31 27\r\n11 29\r\n34 10\r\n35 2\r\n42 24\r\n19 16\r\n38 24\r\n5 9\r\n26 9\r\n36 14\r\n18 47\r\n28 40\r\n45 13\r\n35 22\r\n2 15\r\n31 30\r\n20 48\r\n39 3\r\n8 34\r\n36 7\r\n25 17\r\n5 39\r\n29 1\r\n32 33\r\n16 30\r\n38 49\r\n25 18\r\n1 11\r\n7 44\r\n12 43\r\n15 22\r\n49 21\r\n8 23\r\n", "output": "3"}, {"input": "6 5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 1\r\n", "output": "2"}, {"input": "6 4\r\n1 2\r\n1 3\r\n4 5\r\n4 6\r\n", "output": "0"}, {"input": "16 16\r\n1 2\r\n2 3\r\n1 3\r\n4 5\r\n5 6\r\n4 6\r\n7 8\r\n8 9\r\n9 10\r\n10 11\r\n11 7\r\n12 13\r\n13 14\r\n14 15\r\n15 16\r\n16 12\r\n", "output": "4"}, {"input": "4 4\r\n1 2\r\n4 3\r\n1 4\r\n2 3\r\n", "output": "0"}, {"input": "9 9\r\n1 2\r\n2 3\r\n3 1\r\n4 5\r\n5 6\r\n6 4\r\n7 8\r\n8 9\r\n9 7\r\n", "output": "3"}, {"input": "20 11\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 10\r\n10 11\r\n11 1\r\n", "output": "2"}, {"input": "4 3\r\n1 2\r\n3 4\r\n1 3\r\n", "output": "0"}, {"input": "4 2\r\n2 4\r\n3 4\r\n", "output": "0"}, {"input": "10 10\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 1\r\n6 7\r\n7 8\r\n8 9\r\n9 10\r\n10 6\r\n", "output": "2"}, {"input": "6 5\r\n2 1\r\n3 4\r\n2 3\r\n4 5\r\n5 6\r\n", "output": "0"}, {"input": "8 5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n5 1\r\n", "output": "2"}, {"input": "6 5\r\n1 2\r\n2 3\r\n3 4\r\n4 5\r\n1 5\r\n", "output": "2"}, {"input": "8 8\r\n1 2\r\n2 3\r\n3 4\r\n1 4\r\n5 6\r\n6 7\r\n7 8\r\n5 8\r\n", "output": "0"}, {"input": "6 5\r\n1 3\r\n1 2\r\n2 4\r\n5 3\r\n5 4\r\n", "output": "2"}]
false
stdio
null
true
215/A
215
A
Python 3
TESTS
46
218
307,200
97707454
n = int(input()) a = list(map(int, input().split())) m = int(input()) bf = list(map(int,input().split())) b = bf[::-1] rt = 0 lt = {} for i in a: for j in b: if j/i >= rt and j%i == 0: rt = j/i if rt in lt.keys(): lt[rt] += 1 else: lt[rt] = 1 #print(lt) res = max(lt ,key = lt.get) print(lt[res])
57
92
0
4604032
a = int(input()) A = list(map(int, input().split())) b = int(input()) B = list(map(int, input().split())) k = 1 s = 0 for i in B: for j in A: if j * k > i: continue if i % j == 0: if j * k == i: s += 1 else: s = 1 k = i // j print(s)
Codeforces Round 132 (Div. 2)
CF
2,012
2
256
Bicycle Chain
Vasya's bicycle chain drive consists of two parts: n stars are attached to the pedal axle, m stars are attached to the rear wheel axle. The chain helps to rotate the rear wheel by transmitting the pedal rotation. We know that the i-th star on the pedal axle has ai (0 < a1 < a2 < ... < an) teeth, and the j-th star on the rear wheel axle has bj (0 < b1 < b2 < ... < bm) teeth. Any pair (i, j) (1 ≤ i ≤ n; 1 ≤ j ≤ m) is called a gear and sets the indexes of stars to which the chain is currently attached. Gear (i, j) has a gear ratio, equal to the value $$\frac{b_{j}}{a_{j}}$$. Since Vasya likes integers, he wants to find such gears (i, j), that their ratios are integers. On the other hand, Vasya likes fast driving, so among all "integer" gears (i, j) he wants to choose a gear with the maximum ratio. Help him to find the number of such gears. In the problem, fraction $$\frac{b_{j}}{a_{j}}$$ denotes division in real numbers, that is, no rounding is performed.
The first input line contains integer n (1 ≤ n ≤ 50) — the number of stars on the bicycle's pedal axle. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 104) in the order of strict increasing. The third input line contains integer m (1 ≤ m ≤ 50) — the number of stars on the rear wheel axle. The fourth line contains m integers b1, b2, ..., bm (1 ≤ bi ≤ 104) in the order of strict increasing. It is guaranteed that there exists at least one gear (i, j), that its gear ratio is an integer. The numbers on the lines are separated by spaces.
Print the number of "integer" gears with the maximum ratio among all "integer" gears.
null
In the first sample the maximum "integer" gear ratio equals 3. There are two gears that have such gear ratio. For one of them a1 = 4, b1 = 12, and for the other a2 = 5, b3 = 15.
[{"input": "2\n4 5\n3\n12 13 15", "output": "2"}, {"input": "4\n1 2 3 4\n5\n10 11 12 13 14", "output": "1"}]
900
["brute force", "implementation"]
57
[{"input": "2\r\n4 5\r\n3\r\n12 13 15\r\n", "output": "2\r\n"}, {"input": "4\r\n1 2 3 4\r\n5\r\n10 11 12 13 14\r\n", "output": "1\r\n"}, {"input": "1\r\n1\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "2\r\n1 2\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n1\r\n2\r\n1 2\r\n", "output": "1\r\n"}, {"input": "4\r\n3 7 11 13\r\n4\r\n51 119 187 221\r\n", "output": "4\r\n"}, {"input": "4\r\n2 3 4 5\r\n3\r\n1 2 3\r\n", "output": "2\r\n"}, {"input": "10\r\n6 12 13 20 48 53 74 92 96 97\r\n10\r\n1 21 32 36 47 54 69 75 95 97\r\n", "output": "1\r\n"}, {"input": "10\r\n5 9 10 14 15 17 19 22 24 26\r\n10\r\n2 11 17 19 21 22 24 25 27 28\r\n", "output": "1\r\n"}, {"input": "10\r\n24 53 56 126 354 432 442 740 795 856\r\n10\r\n273 438 494 619 689 711 894 947 954 958\r\n", "output": "1\r\n"}, {"input": "10\r\n3 4 6 7 8 10 14 16 19 20\r\n10\r\n3 4 5 7 8 10 15 16 18 20\r\n", "output": "1\r\n"}, {"input": "10\r\n1 6 8 14 15 17 25 27 34 39\r\n10\r\n1 8 16 17 19 22 32 39 44 50\r\n", "output": "1\r\n"}, {"input": "10\r\n5 21 22 23 25 32 35 36 38 39\r\n10\r\n3 7 8 9 18 21 23 24 36 38\r\n", "output": "4\r\n"}, {"input": "10\r\n17 239 443 467 661 1069 1823 2333 3767 4201\r\n20\r\n51 83 97 457 593 717 997 1329 1401 1459 1471 1983 2371 2539 3207 3251 3329 5469 6637 6999\r\n", "output": "8\r\n"}, {"input": "20\r\n179 359 401 467 521 601 919 941 1103 1279 1709 1913 1949 2003 2099 2143 2179 2213 2399 4673\r\n20\r\n151 181 191 251 421 967 1109 1181 1249 1447 1471 1553 1619 2327 2551 2791 3049 3727 6071 7813\r\n", "output": "3\r\n"}, {"input": "20\r\n79 113 151 709 809 983 1291 1399 1409 1429 2377 2659 2671 2897 3217 3511 3557 3797 3823 4363\r\n10\r\n19 101 659 797 1027 1963 2129 2971 3299 9217\r\n", "output": "3\r\n"}, {"input": "30\r\n19 47 109 179 307 331 389 401 461 509 547 569 617 853 883 1249 1361 1381 1511 1723 1741 1783 2459 2531 2621 3533 3821 4091 5557 6217\r\n20\r\n401 443 563 941 967 997 1535 1567 1655 1747 1787 1945 1999 2251 2305 2543 2735 4415 6245 7555\r\n", "output": "8\r\n"}, {"input": "5\r\n33 78 146 3055 4268\r\n5\r\n2211 2584 5226 9402 9782\r\n", "output": "3\r\n"}, {"input": "5\r\n35 48 52 86 8001\r\n10\r\n332 3430 3554 4704 4860 5096 6215 7583 8228 8428\r\n", "output": "4\r\n"}, {"input": "10\r\n97 184 207 228 269 2084 4450 6396 7214 9457\r\n16\r\n338 1179 1284 1545 1570 2444 3167 3395 3397 5550 6440 7245 7804 7980 9415 9959\r\n", "output": "5\r\n"}, {"input": "30\r\n25 30 41 57 58 62 70 72 76 79 84 85 88 91 98 101 104 109 119 129 136 139 148 151 926 1372 3093 3936 5423 7350\r\n25\r\n1600 1920 2624 3648 3712 3968 4480 4608 4864 5056 5376 5440 5632 5824 6272 6464 6656 6934 6976 7616 8256 8704 8896 9472 9664\r\n", "output": "24\r\n"}, {"input": "5\r\n33 78 146 3055 4268\r\n5\r\n2211 2584 5226 9402 9782\r\n", "output": "3\r\n"}, {"input": "5\r\n35 48 52 86 8001\r\n10\r\n332 3430 3554 4704 4860 5096 6215 7583 8228 8428\r\n", "output": "4\r\n"}, {"input": "10\r\n97 184 207 228 269 2084 4450 6396 7214 9457\r\n16\r\n338 1179 1284 1545 1570 2444 3167 3395 3397 5550 6440 7245 7804 7980 9415 9959\r\n", "output": "5\r\n"}, {"input": "30\r\n25 30 41 57 58 62 70 72 76 79 84 85 88 91 98 101 104 109 119 129 136 139 148 151 926 1372 3093 3936 5423 7350\r\n25\r\n1600 1920 2624 3648 3712 3968 4480 4608 4864 5056 5376 5440 5632 5824 6272 6464 6656 6934 6976 7616 8256 8704 8896 9472 9664\r\n", "output": "24\r\n"}, {"input": "50\r\n51 67 75 186 194 355 512 561 720 876 1077 1221 1503 1820 2153 2385 2568 2608 2937 2969 3271 3311 3481 4081 4093 4171 4255 4256 4829 5020 5192 5636 5817 6156 6712 6717 7153 7436 7608 7612 7866 7988 8264 8293 8867 9311 9879 9882 9889 9908\r\n1\r\n5394\r\n", "output": "1\r\n"}, {"input": "3\r\n3 4 5\r\n3\r\n6 20 25\r\n", "output": "2\r\n"}, {"input": "4\r\n2 3 5 8\r\n4\r\n2 6 8 10\r\n", "output": "1\r\n"}, {"input": "4\r\n3 5 7 11\r\n4\r\n3 5 7 22\r\n", "output": "1\r\n"}, {"input": "2\r\n2 3\r\n3\r\n20 30 50\r\n", "output": "1\r\n"}, {"input": "3\r\n1 2 3\r\n4\r\n2 4 6 49\r\n", "output": "1\r\n"}, {"input": "2\r\n4 5\r\n3\r\n12 15 20\r\n", "output": "1\r\n"}, {"input": "3\r\n2 5 7\r\n3\r\n4 5 7\r\n", "output": "1\r\n"}, {"input": "3\r\n3 5 8\r\n3\r\n6 8 10\r\n", "output": "2\r\n"}, {"input": "2\r\n2 3\r\n4\r\n4 6 9 33\r\n", "output": "1\r\n"}, {"input": "2\r\n2 3\r\n4\r\n4 6 21 40\r\n", "output": "1\r\n"}, {"input": "3\r\n4 9 10\r\n3\r\n8 9 10\r\n", "output": "1\r\n"}, {"input": "5\r\n1 5 6 9 51\r\n5\r\n5 12 18 27 10000\r\n", "output": "1\r\n"}, {"input": "13\r\n1 2 3 4 5 6 7 8 9 10 11 12 13\r\n1\r\n14\r\n", "output": "1\r\n"}]
false
stdio
null
true
524/C
524
C
Python 3
TESTS
15
62
204,800
26871466
params = [int(x) for x in input().split()] n = params[0] k = params[1] bills = [int(x) for x in input().split()][::-1] request = [int(x) for x in input().split()][0] amounts = [] for i in range(request): amounts.append(int(input())) def busqueda(total, actual, profundidad, eleccion, diff): if diff > 2: return -1 if total == actual: return profundidad if actual > total or ((actual + bills[eleccion] * (k - profundidad)) < total and eleccion != -1): return -1 if profundidad > k: return -1 for i in range(len(bills)): if eleccion == -1: aux = busqueda(total, actual + bills[i], profundidad + 1, i, 1) if aux != -1: return aux elif i >= eleccion: if i != eleccion: diff += 1 aux = busqueda(total, actual + bills[i], profundidad + 1, i, diff) if aux != -1: return aux if i != eleccion: diff -= 1 return -1 def c(): for x in amounts: print(busqueda(x, 0, 0, -1, 0)) c()
129
358
11,878,400
231037855
f = lambda: map(int, input().split()) n, k = f() t = list(f()) d = {0: 0} for q in t: for i in range(1, k + 1): d[q * i] = i for j in range(int(input())): a = int(input()) p = [i + d[a - b] for b, i in d.items() if a - b in d] print(min(p) if p and min(p) <= k else -1)
VK Cup 2015 - Round 1
CF
2,015
2
256
The Art of Dealing with ATM
ATMs of a well-known bank of a small country are arranged so that they can not give any amount of money requested by the user. Due to the limited size of the bill dispenser (the device that is directly giving money from an ATM) and some peculiarities of the ATM structure, you can get at most k bills from it, and the bills may be of at most two distinct denominations. For example, if a country uses bills with denominations 10, 50, 100, 500, 1000 and 5000 burles, then at k = 20 such ATM can give sums 100 000 burles and 96 000 burles, but it cannot give sums 99 000 and 101 000 burles. Let's suppose that the country uses bills of n distinct denominations, and the ATM that you are using has an unlimited number of bills of each type. You know that during the day you will need to withdraw a certain amount of cash q times. You know that when the ATM has multiple ways to give money, it chooses the one which requires the minimum number of bills, or displays an error message if it cannot be done. Determine the result of each of the q of requests for cash withdrawal.
The first line contains two integers n, k (1 ≤ n ≤ 5000, 1 ≤ k ≤ 20). The next line contains n space-separated integers ai (1 ≤ ai ≤ 107) — the denominations of the bills that are used in the country. Numbers ai follow in the strictly increasing order. The next line contains integer q (1 ≤ q ≤ 20) — the number of requests for cash withdrawal that you will make. The next q lines contain numbers xi (1 ≤ xi ≤ 2·108) — the sums of money in burles that you are going to withdraw from the ATM.
For each request for cash withdrawal print on a single line the minimum number of bills it can be done, or print - 1, if it is impossible to get the corresponding sum.
null
null
[{"input": "6 20\n10 50 100 500 1000 5000\n8\n4200\n100000\n95000\n96000\n99000\n10100\n2015\n9950", "output": "6\n20\n19\n20\n-1\n3\n-1\n-1"}, {"input": "5 2\n1 2 3 5 8\n8\n1\n3\n5\n7\n9\n11\n13\n15", "output": "1\n1\n1\n2\n2\n2\n2\n-1"}]
1,900
["binary search", "sortings"]
129
[{"input": "6 20\r\n10 50 100 500 1000 5000\r\n8\r\n4200\r\n100000\r\n95000\r\n96000\r\n99000\r\n10100\r\n2015\r\n9950\r\n", "output": "6\r\n20\r\n19\r\n20\r\n-1\r\n3\r\n-1\r\n-1\r\n"}, {"input": "5 2\r\n1 2 3 5 8\r\n8\r\n1\r\n3\r\n5\r\n7\r\n9\r\n11\r\n13\r\n15\r\n", "output": "1\r\n1\r\n1\r\n2\r\n2\r\n2\r\n2\r\n-1\r\n"}, {"input": "5 5\r\n1 2 3 6 102\r\n10\r\n1\r\n4\r\n30\r\n1\r\n76\r\n114\r\n10\r\n13\r\n13\r\n4\r\n", "output": "1\r\n2\r\n5\r\n1\r\n-1\r\n3\r\n3\r\n3\r\n3\r\n2\r\n"}, {"input": "3 5\r\n1 36 95\r\n10\r\n3\r\n1\r\n4212\r\n144\r\n4\r\n109\r\n6\r\n5\r\n2\r\n24\r\n", "output": "3\r\n1\r\n-1\r\n4\r\n4\r\n4\r\n-1\r\n5\r\n2\r\n-1\r\n"}, {"input": "4 5\r\n1 11 50 203\r\n10\r\n5\r\n5\r\n150\r\n53\r\n56\r\n5\r\n12304\r\n852\r\n1\r\n5\r\n", "output": "5\r\n5\r\n3\r\n4\r\n-1\r\n5\r\n-1\r\n-1\r\n1\r\n5\r\n"}, {"input": "4 5\r\n2 12 71 424\r\n10\r\n355\r\n342\r\n8\r\n1835\r\n6625\r\n355\r\n16\r\n1490\r\n148\r\n213\r\n", "output": "5\r\n-1\r\n4\r\n-1\r\n-1\r\n5\r\n3\r\n-1\r\n5\r\n3\r\n"}, {"input": "4 5\r\n1 27 49 135\r\n10\r\n17\r\n4\r\n81\r\n13390\r\n1\r\n525\r\n701\r\n5\r\n30\r\n31\r\n", "output": "-1\r\n4\r\n3\r\n-1\r\n1\r\n-1\r\n-1\r\n5\r\n4\r\n5\r\n"}, {"input": "3 1\r\n1 36 43\r\n10\r\n1\r\n627\r\n36\r\n36\r\n4\r\n1\r\n36\r\n1\r\n16\r\n64\r\n", "output": "1\r\n-1\r\n1\r\n1\r\n-1\r\n1\r\n1\r\n1\r\n-1\r\n-1\r\n"}, {"input": "3 1\r\n1 4 21\r\n10\r\n42\r\n1\r\n1\r\n4\r\n1\r\n1\r\n1\r\n117\r\n6\r\n829\r\n", "output": "-1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n-1\r\n-1\r\n-1\r\n"}, {"input": "5 1\r\n1 13 23 211 709\r\n10\r\n27\r\n77\r\n13\r\n10\r\n148\r\n10\r\n1\r\n88\r\n23\r\n31\r\n", "output": "-1\r\n-1\r\n1\r\n-1\r\n-1\r\n-1\r\n1\r\n-1\r\n1\r\n-1\r\n"}, {"input": "4 5\r\n1 4 30 891\r\n10\r\n717\r\n410\r\n1\r\n5015\r\n16\r\n3\r\n1\r\n5\r\n5\r\n1\r\n", "output": "-1\r\n-1\r\n1\r\n-1\r\n4\r\n3\r\n1\r\n2\r\n2\r\n1\r\n"}, {"input": "4 5\r\n1 36 468 791\r\n10\r\n5\r\n5075\r\n1404\r\n5\r\n198\r\n53\r\n5\r\n4121\r\n1404\r\n4244\r\n", "output": "5\r\n-1\r\n3\r\n5\r\n-1\r\n-1\r\n5\r\n-1\r\n3\r\n-1\r\n"}, {"input": "4 5\r\n1 2 322 758\r\n10\r\n648\r\n547\r\n647\r\n322\r\n13\r\n10\r\n1\r\n1742\r\n7\r\n1173\r\n", "output": "4\r\n-1\r\n5\r\n1\r\n-1\r\n5\r\n1\r\n-1\r\n4\r\n-1\r\n"}, {"input": "4 20\r\n1 2 45 229\r\n10\r\n15\r\n13\r\n41406\r\n900\r\n18\r\n27\r\n20\r\n15\r\n589\r\n14\r\n", "output": "8\r\n7\r\n-1\r\n20\r\n9\r\n14\r\n10\r\n8\r\n9\r\n7\r\n"}, {"input": "14 1\r\n1 6 30 49 189 478 1514 1776 49588 655130 673561 1101207 2953118 4634078\r\n20\r\n1514\r\n5\r\n189\r\n23\r\n45\r\n77500\r\n49588\r\n84799\r\n2052853\r\n8815\r\n26\r\n1\r\n68\r\n478\r\n61\r\n189\r\n6\r\n1\r\n478\r\n500\r\n", "output": "1\r\n-1\r\n1\r\n-1\r\n-1\r\n-1\r\n1\r\n-1\r\n-1\r\n-1\r\n-1\r\n1\r\n-1\r\n1\r\n-1\r\n1\r\n1\r\n1\r\n1\r\n-1\r\n"}, {"input": "51 5\r\n1 2 4 5 6 7 8 10 12 13 20 23 31 58 63 66 71 83 230 305 322 550 559 596 952 1353 1494 1610 2156 2160 3053 4312 4698 8240 13445 16060 34590 52653 68265 134554 203093 203689 302605 403350 687107 1006006 1551678 2840590 3326364 7266429 7447528\r\n20\r\n284\r\n4\r\n8\r\n21997625\r\n5\r\n273060\r\n550\r\n74\r\n10742012\r\n330\r\n40\r\n24722\r\n8306\r\n1220\r\n20\r\n6511\r\n8\r\n2\r\n33054009\r\n16\r\n", "output": "4\r\n1\r\n1\r\n-1\r\n1\r\n4\r\n1\r\n2\r\n-1\r\n2\r\n2\r\n4\r\n2\r\n4\r\n1\r\n4\r\n1\r\n1\r\n-1\r\n2\r\n"}, {"input": "55 5\r\n1 2 3 4 5 6 7 9 11 13 19 25 42 65 126 138 164 275 315 364 411 1297 1532 2562 3280 10675 11275 22596 28563 33704 38710 74921 88560 94671 155311 166320 166913 228504 271152 284013 333826 697037 941357 1012652 1132991 1230723 1501332 1722000 1826550 2128486 2286428 4050608 5396247 7607666 7751599\r\n20\r\n196\r\n2395370\r\n831600\r\n5\r\n325\r\n12\r\n21\r\n296\r\n532926\r\n4018\r\n22\r\n7\r\n565\r\n28\r\n193550\r\n7\r\n46\r\n144\r\n67838\r\n78019\r\n", "output": "4\r\n-1\r\n5\r\n1\r\n3\r\n2\r\n2\r\n4\r\n-1\r\n5\r\n2\r\n1\r\n5\r\n2\r\n5\r\n1\r\n2\r\n2\r\n5\r\n-1\r\n"}, {"input": "35 20\r\n1 2 3 4 5 7 29 41 111 176 248 291 704 1557 2112 2624 7322 7960 10989 15277 18740 20135 32948 56220 65554 112440 131792 153762 219812 508510 1591650 1634639 2691141 4546819 5985721\r\n20\r\n158964830\r\n20\r\n1240\r\n899531\r\n284\r\n522\r\n95\r\n13455733\r\n41913730\r\n60423\r\n3077372\r\n26\r\n189248\r\n9330\r\n16\r\n25634561\r\n5201868\r\n73197\r\n9017\r\n899540\r\n", "output": "-1\r\n4\r\n5\r\n19\r\n10\r\n15\r\n5\r\n9\r\n-1\r\n9\r\n15\r\n4\r\n-1\r\n-1\r\n3\r\n-1\r\n-1\r\n10\r\n12\r\n12\r\n"}, {"input": "5 1\r\n2 9 13 442 2772\r\n20\r\n9353\r\n2\r\n9\r\n1658772\r\n2\r\n2\r\n442\r\n616\r\n4399\r\n9\r\n96\r\n442\r\n442\r\n9\r\n9\r\n18395\r\n13\r\n2\r\n2\r\n2\r\n", "output": "-1\r\n1\r\n1\r\n-1\r\n1\r\n1\r\n1\r\n-1\r\n-1\r\n1\r\n-1\r\n1\r\n1\r\n1\r\n1\r\n-1\r\n1\r\n1\r\n1\r\n1\r\n"}, {"input": "53 20\r\n1 2 5 12 13 22 110 137 192 201 256 274 285 618 646 1008 1259 2373 2828 3117 7351 7918 10686 13363 17755 26103 30849 32058 36202 38094 56252 61698 67760 91829 104412 139304 158237 222774 244133 278608 281260 370188 468245 936490 975198 1582370 1914289 3001845 3657737 3828578 4394394 4827053 4875990\r\n20\r\n21396\r\n1772314\r\n14693\r\n162\r\n14140\r\n304226\r\n3\r\n20\r\n154078\r\n63\r\n3673488\r\n1537\r\n836\r\n88\r\n1012\r\n73644\r\n67771090\r\n2\r\n370258\r\n55752756\r\n", "output": "4\r\n4\r\n6\r\n5\r\n5\r\n-1\r\n2\r\n4\r\n19\r\n5\r\n-1\r\n7\r\n10\r\n4\r\n3\r\n-1\r\n20\r\n1\r\n15\r\n-1\r\n"}, {"input": "44 20\r\n1 2 3 4 15 19 20 27 29 31 44 58 103 106 156 203 206 222 499 515 1339 3557 4017 5017 7105 14416 24926 27799 35904 42393 127972 166738 186649 304616 927340 1854680 2189529 3436045 3497568 4379058 4893003 5331302 6998715 7895196\r\n20\r\n918\r\n18758\r\n32328\r\n1387737\r\n871\r\n31\r\n1521\r\n312902\r\n17489324\r\n65685880\r\n11603\r\n27386575\r\n29013091\r\n775866\r\n7725\r\n102059\r\n1718\r\n2014\r\n104199396\r\n19\r\n", "output": "5\r\n17\r\n-1\r\n-1\r\n13\r\n1\r\n9\r\n-1\r\n19\r\n20\r\n-1\r\n-1\r\n-1\r\n8\r\n7\r\n-1\r\n12\r\n10\r\n-1\r\n1\r\n"}, {"input": "20 1\r\n1 99 115 460 805 2415 3220 8280 13800 16560 42780 50600 141680 425040 1127000 1416800 1710280 2254000 2781275 5667200\r\n20\r\n13800\r\n1556849\r\n1\r\n460\r\n33\r\n99\r\n102\r\n805\r\n99\r\n8280\r\n1\r\n1\r\n2541\r\n12\r\n1\r\n354\r\n115\r\n235598\r\n4863\r\n136\r\n", "output": "1\r\n-1\r\n1\r\n1\r\n-1\r\n1\r\n-1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n-1\r\n-1\r\n1\r\n-1\r\n1\r\n-1\r\n-1\r\n-1\r\n"}, {"input": "58 5\r\n1 6 9 18 34 41 82 99 164 179 183 204 240 396 636 1224 2023 2856 4488 4705 5712 9180 9266 18360 18850 20270 23086 60810 190701 197064 211140 214200 222970 243240 263510 314442 425670 486480 708288 789888 1009800 1025349 1054040 1094580 1325184 1579776 1748076 2050698 2635100 2895984 2918880 3975552 4284000 4739328 4938778 5386284 6992304 7898880\r\n20\r\n825982\r\n428758\r\n7062\r\n183\r\n4101396\r\n2\r\n11583907\r\n1228\r\n612\r\n1318\r\n170\r\n1980\r\n24574\r\n2770806\r\n1019\r\n123\r\n58\r\n145\r\n4292\r\n21545136\r\n", "output": "-1\r\n4\r\n-1\r\n1\r\n2\r\n2\r\n-1\r\n5\r\n3\r\n-1\r\n2\r\n5\r\n-1\r\n-1\r\n-1\r\n2\r\n5\r\n5\r\n5\r\n4\r\n"}, {"input": "62 20\r\n1 5 7 22 28 30 37 44 49 80 135 240 285 329 409 410 570 658 672 830 855 955 1132 1274 2309 2940 3477 3681 3948 6381 6792 7567 7896 10902 11460 18424 19740 29448 59820 117502 125154 141506 149424 211521 353376 387680 525084 706752 908040 1379115 1575252 1816080 3150504 3962147 4246074 5535096 6103922 7153540 7260701 8442370 8567160 8913115\r\n20\r\n880\r\n56\r\n119\r\n47\r\n21042\r\n21\r\n787\r\n1480658\r\n2597864\r\n281\r\n308\r\n31597\r\n8100\r\n29470\r\n19\r\n83\r\n11494\r\n32\r\n3232\r\n1203\r\n", "output": "4\r\n2\r\n5\r\n3\r\n12\r\n3\r\n9\r\n18\r\n-1\r\n8\r\n6\r\n17\r\n10\r\n2\r\n3\r\n4\r\n10\r\n3\r\n17\r\n8\r\n"}, {"input": "45 1\r\n1 2 4 23 43 70 71 430 638 908 1042 1290 1846 2150 5160 10320 11180 16340 30960 55900 176085 239510 257140 278640 443167 514280 526750 771420 1028560 1285700 1424160 1542840 1799980 2057120 3085680 3342820 4114240 4628520 4790200 4885660 6171360 6428500 6685640 7632210 7714200\r\n20\r\n2\r\n2\r\n23\r\n5119\r\n1\r\n245644\r\n75545\r\n1516554\r\n4179\r\n1\r\n16340\r\n1\r\n1\r\n612762\r\n1\r\n1\r\n1\r\n1424160\r\n43\r\n4\r\n", "output": "1\r\n1\r\n1\r\n-1\r\n1\r\n-1\r\n-1\r\n-1\r\n-1\r\n1\r\n1\r\n1\r\n1\r\n-1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n"}, {"input": "16 5\r\n1 145 524 820 1048 13120 36680 102500 141860 283720 512500 1332500 2558400 3944200 4100000 5116800\r\n20\r\n4716\r\n56979\r\n14333600\r\n16\r\n3\r\n2653\r\n25489787\r\n479469\r\n1050\r\n669\r\n5240\r\n2665002\r\n1730\r\n1643\r\n20500000\r\n43808729\r\n557\r\n40689909\r\n2097\r\n5240\r\n", "output": "5\r\n-1\r\n3\r\n-1\r\n3\r\n-1\r\n-1\r\n-1\r\n3\r\n2\r\n5\r\n4\r\n-1\r\n5\r\n5\r\n-1\r\n-1\r\n-1\r\n3\r\n5\r\n"}, {"input": "6 20\r\n1 4 38 76 304 74214\r\n20\r\n12\r\n90\r\n39\r\n440\r\n319\r\n9\r\n66\r\n50\r\n647\r\n2460\r\n196\r\n18\r\n77\r\n11\r\n18\r\n58\r\n5888496\r\n211\r\n500\r\n281512\r\n", "output": "3\r\n14\r\n2\r\n20\r\n16\r\n3\r\n8\r\n4\r\n18\r\n15\r\n11\r\n6\r\n2\r\n5\r\n6\r\n6\r\n-1\r\n-1\r\n17\r\n-1\r\n"}, {"input": "41 20\r\n1 3 4 154 1405 2810 4215 7025 19670 33720 49175 54795 64630 82895 129260 130665 151557 165790 168600 255710 300024 461710 663160 792420 927300 1500120 1584840 1854600 1906585 2377260 2781900 3150010 3709200 3962100 4500360 5056595 5305280 5546940 5563800 5968440 7131780\r\n20\r\n121163\r\n22255200\r\n8472\r\n8\r\n21\r\n292085\r\n137697\r\n705\r\n435970\r\n775562\r\n131108\r\n5502\r\n71655\r\n39341\r\n57721\r\n46365\r\n90703013\r\n77983120\r\n164400\r\n547953\r\n", "output": "-1\r\n4\r\n16\r\n2\r\n6\r\n-1\r\n14\r\n-1\r\n-1\r\n8\r\n13\r\n-1\r\n2\r\n3\r\n20\r\n3\r\n-1\r\n15\r\n8\r\n11\r\n"}, {"input": "27 5\r\n31900 63800 127600 191400 255200 319000 478500 510400 574200 638000 765600 1020800 2296800 2552000 2679600 3062400 3445200 3572800 3828000 3891800 4083200 4466000 5359200 6124800 7273200 9187200 9378600\r\n20\r\n159500\r\n3828000\r\n4\r\n1165857\r\n159500\r\n765600\r\n269036\r\n1\r\n478500\r\n2296800\r\n127600\r\n159500\r\n3190000\r\n5327300\r\n1276000\r\n9187200\r\n9\r\n382800\r\n11675400\r\n15312000\r\n", "output": "2\r\n1\r\n-1\r\n-1\r\n2\r\n1\r\n-1\r\n-1\r\n1\r\n1\r\n1\r\n2\r\n2\r\n4\r\n2\r\n1\r\n-1\r\n2\r\n2\r\n2\r\n"}, {"input": "35 20\r\n2616 7848 13080 18312 23544 36624 47088 70632 75864 94176 109872 125568 143880 146496 151728 164808 227592 337464 455184 635688 659232 682776 824040 910368 1012392 1318464 1357704 1365552 1648080 1669008 2024784 2508744 4049568 5007024 5017488\r\n20\r\n238056\r\n1\r\n22280472\r\n117720\r\n334848\r\n8\r\n1365552\r\n26160\r\n94176\r\n1121157\r\n15478872\r\n97125922\r\n219744\r\n658642\r\n1383864\r\n3296160\r\n151728\r\n235440\r\n787416\r\n47088\r\n", "output": "2\r\n-1\r\n12\r\n2\r\n3\r\n-1\r\n1\r\n2\r\n1\r\n-1\r\n17\r\n-1\r\n2\r\n-1\r\n2\r\n2\r\n1\r\n2\r\n2\r\n1\r\n"}, {"input": "27 20\r\n2 12 34 48 68 70 102 136 140 210 230 756 2268 4464 7378 8928 49630 71424 142848 144096 376278 688296 752556 1069810 1343724 3209430 5744760\r\n20\r\n18189038\r\n572752\r\n5\r\n51291\r\n584\r\n6108\r\n3209440\r\n100315\r\n368\r\n1122\r\n46\r\n26\r\n280\r\n256\r\n567936\r\n2800\r\n1454352\r\n1196050\r\n73582149\r\n149765054\r\n", "output": "18\r\n14\r\n-1\r\n-1\r\n6\r\n13\r\n6\r\n-1\r\n4\r\n9\r\n2\r\n3\r\n2\r\n11\r\n-1\r\n14\r\n13\r\n-1\r\n-1\r\n-1\r\n"}, {"input": "39 1\r\n139873 279746 419619 559492 699365 839238 979111 1118984 1258857 1398730 1538603 1678476 1958222 2098095 2237968 2517714 2657587 2797460 3077206 3356952 3496825 3916444 4196190 4475936 5035428 5594920 6154412 6294285 6434158 6713904 6853777 6993650 7133523 7273396 7832888 7972761 8112634 8392380 8951872\r\n20\r\n11575\r\n9\r\n419619\r\n139873\r\n15\r\n419619\r\n308818\r\n1\r\n296\r\n6713904\r\n139873\r\n1118984\r\n139873\r\n5594920\r\n839238\r\n279746\r\n1040050\r\n7809292\r\n839238\r\n2797460\r\n", "output": "-1\r\n-1\r\n1\r\n1\r\n-1\r\n1\r\n-1\r\n-1\r\n-1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n-1\r\n-1\r\n1\r\n1\r\n"}, {"input": "1 20\r\n4247942\r\n20\r\n63719130\r\n80710898\r\n188731\r\n4876\r\n67967072\r\n8\r\n63719130\r\n728\r\n76462956\r\n84958840\r\n72215014\r\n9667\r\n8495884\r\n4247942\r\n29735594\r\n28521424\r\n94864\r\n76462956\r\n84958840\r\n84958840\r\n", "output": "15\r\n19\r\n-1\r\n-1\r\n16\r\n-1\r\n15\r\n-1\r\n18\r\n20\r\n17\r\n-1\r\n2\r\n1\r\n7\r\n-1\r\n-1\r\n18\r\n20\r\n20\r\n"}, {"input": "1 1\r\n42\r\n5\r\n1\r\n41\r\n42\r\n43\r\n84\r\n", "output": "-1\r\n-1\r\n1\r\n-1\r\n-1\r\n"}, {"input": "1 2\r\n42\r\n8\r\n1\r\n41\r\n42\r\n43\r\n83\r\n84\r\n85\r\n126\r\n", "output": "-1\r\n-1\r\n1\r\n-1\r\n-1\r\n2\r\n-1\r\n-1\r\n"}, {"input": "2 1\r\n23 42\r\n11\r\n1\r\n22\r\n23\r\n24\r\n41\r\n42\r\n43\r\n66\r\n67\r\n68\r\n987\r\n", "output": "-1\r\n-1\r\n1\r\n-1\r\n-1\r\n1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n"}, {"input": "2 2\r\n23 42\r\n17\r\n1\r\n22\r\n23\r\n24\r\n41\r\n42\r\n43\r\n45\r\n46\r\n47\r\n66\r\n67\r\n68\r\n83\r\n84\r\n85\r\n987\r\n", "output": "-1\r\n-1\r\n1\r\n-1\r\n-1\r\n1\r\n-1\r\n-1\r\n2\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n2\r\n-1\r\n-1\r\n"}, {"input": "2 5\r\n1 2\r\n1\r\n200000000\r\n", "output": "-1\r\n"}, {"input": "1 20\r\n1\r\n20\r\n200000000\r\n199999999\r\n199999998\r\n199999997\r\n199999996\r\n199999995\r\n199999994\r\n199999993\r\n199999992\r\n199999991\r\n199999990\r\n199999989\r\n199999988\r\n199999987\r\n199999986\r\n199999985\r\n199999984\r\n199999983\r\n199999982\r\n199999981\r\n", "output": "-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n"}, {"input": "2 20\r\n1 10000000\r\n1\r\n190000001\r\n", "output": "20\r\n"}, {"input": "1 1\r\n1\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "1 1\r\n1000000\r\n1\r\n200000000\r\n", "output": "-1\r\n"}, {"input": "5 20\r\n1 2 3 4 5\r\n5\r\n100000000\r\n100000001\r\n190000099\r\n199999999\r\n200000000\r\n", "output": "-1\r\n-1\r\n-1\r\n-1\r\n-1\r\n"}, {"input": "2 2\r\n1 2\r\n1\r\n200000000\r\n", "output": "-1\r\n"}, {"input": "1 20\r\n1\r\n1\r\n200000000\r\n", "output": "-1\r\n"}, {"input": "1 20\r\n10000000\r\n1\r\n200000000\r\n", "output": "20\r\n"}, {"input": "20 20\r\n9970642 9971855 9973038 9973500 9975536 9976719 9980831 9981533 9983173 9984276 9988058 9988522 9990039 9993666 9994295 9994564 9995173 9997005 9999509 9999959\r\n20\r\n199640580\r\n199667040\r\n199630770\r\n199670280\r\n199940730\r\n199623640\r\n199880310\r\n199743150\r\n199814920\r\n199888590\r\n199620220\r\n199667040\r\n199692020\r\n199603770\r\n199768390\r\n199570690\r\n199700430\r\n199969640\r\n199453550\r\n199837850\r\n", "output": "20\r\n20\r\n20\r\n20\r\n20\r\n20\r\n20\r\n20\r\n20\r\n20\r\n20\r\n20\r\n20\r\n20\r\n20\r\n20\r\n20\r\n20\r\n20\r\n20\r\n"}]
false
stdio
null
true
464/A
464
A
PyPy 3
TESTS
12
140
20,172,800
87061801
def clean(n,w,i): for j in range(i+1,n): if 0 not in [w[j-1],w[j-2]]: w[j] = 0 elif 1 not in [w[j-1],w[j-2]]: w[j] = 1 else: w[j] = 2 return ''.join([chr(ord('a')+x) for x in w]) def solve(): n, p = map(int, input().split()) w = [ord(c)-ord('a') for c in input()] if p==1: return 'NO' elif p==2: if n==1 and w[0]==0: return 'b' elif n==2 and w[0]==0: return 'ba' else: return 'NO' elif n==1: if w[0]==p-1: return 'NO' else: return chr(ord('a')+1) for i in range(n-1,1,-1): for k in range(1,3+1): if w[i]+k not in [w[i-1],w[i-2]] and w[i]+k < p: w[i] += k return clean(n,w,i) for k in range(1,2+1): if w[1]+k not in [w[0]] and w[1]+k < p: w[1] += k return clean(n,w,1) if w[0]+1 < p: w[0] += 1 w[1] = 0 return clean(n,w,1) return 'NO' print(solve())
73
62
0
8138005
n, p = map(int, input().split()) s = list(ord(i) - 97 for i in input()) for i in range(n - 1, -1, -1): for j in range(s[i] + 1, p): if (i < 1 or j != s[i - 1]) and (i < 2 or j != s[i - 2]): s[i] = j for i in range(i + 1, n): for j in range(p): if (i < 1 or j != s[i - 1]) and (i < 2 or j != s[i - 2]): s[i] = j break print(''.join(chr(i + 97) for i in s)) exit() print('NO')
Codeforces Round 265 (Div. 1)
CF
2,014
1
256
No to Palindromes!
Paul hates palindromes. He assumes that string s is tolerable if each its character is one of the first p letters of the English alphabet and s doesn't contain any palindrome contiguous substring of length 2 or more. Paul has found a tolerable string s of length n. Help him find the lexicographically next tolerable string of the same length or else state that such string does not exist.
The first line contains two space-separated integers: n and p (1 ≤ n ≤ 1000; 1 ≤ p ≤ 26). The second line contains string s, consisting of n small English letters. It is guaranteed that the string is tolerable (according to the above definition).
If the lexicographically next tolerable string of the same length exists, print it. Otherwise, print "NO" (without the quotes).
null
String s is lexicographically larger (or simply larger) than string t with the same length, if there is number i, such that s1 = t1, ..., si = ti, si + 1 > ti + 1. The lexicographically next tolerable string is the lexicographically minimum tolerable string which is larger than the given one. A palindrome is a string that reads the same forward or reversed.
[{"input": "3 3\ncba", "output": "NO"}, {"input": "3 4\ncba", "output": "cbd"}, {"input": "4 4\nabcd", "output": "abda"}]
1,700
["greedy", "strings"]
73
[{"input": "3 3\r\ncba\r\n", "output": "NO\r\n"}, {"input": "3 4\r\ncba\r\n", "output": "cbd\r\n"}, {"input": "4 4\r\nabcd\r\n", "output": "abda\r\n"}, {"input": "2 2\r\nab\r\n", "output": "ba\r\n"}, {"input": "2 2\r\nba\r\n", "output": "NO\r\n"}, {"input": "1 2\r\na\r\n", "output": "b\r\n"}, {"input": "1 2\r\nb\r\n", "output": "NO\r\n"}, {"input": "1 1\r\na\r\n", "output": "NO\r\n"}, {"input": "3 4\r\ncdb\r\n", "output": "dab\r\n"}, {"input": "7 26\r\nzyxzyxz\r\n", "output": "NO\r\n"}, {"input": "10 5\r\nabcabcabca\r\n", "output": "abcabcabcd\r\n"}, {"input": "10 10\r\nfajegfaicb\r\n", "output": "fajegfaicd\r\n"}, {"input": "1 26\r\no\r\n", "output": "p\r\n"}, {"input": "1 2\r\nb\r\n", "output": "NO\r\n"}, {"input": "1 26\r\nz\r\n", "output": "NO\r\n"}, {"input": "3 3\r\ncab\r\n", "output": "cba\r\n"}, {"input": "3 26\r\nyzx\r\n", "output": "zab\r\n"}, {"input": "5 5\r\naceba\r\n", "output": "acebc\r\n"}, {"input": "10 3\r\ncbacbacbac\r\n", "output": "NO\r\n"}, {"input": "11 3\r\nabcabcabcab\r\n", "output": "acbacbacbac\r\n"}, {"input": "12 10\r\nabcabcabcabc\r\n", "output": "abcabcabcabd\r\n"}, {"input": "13 7\r\ngfegfegfegfeg\r\n", "output": "NO\r\n"}, {"input": "15 11\r\ncgjkbadjfbdaikj\r\n", "output": "cgjkbadjfbdajba\r\n"}, {"input": "17 4\r\ndabcadcbdcadbcdbc\r\n", "output": "dabcadcbdcadcabca\r\n"}, {"input": "26 26\r\nahnxdnbfcriersyzdihuecojdi\r\n", "output": "ahnxdnbfcriersyzdihuecojdk\r\n"}, {"input": "30 7\r\ncedcfedcfgcfgcbadcadgfaegfacgf\r\n", "output": "cedcfedcfgcfgcbadcadgfaegfadba\r\n"}, {"input": "70 4\r\ndcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbd\r\n", "output": "NO\r\n"}, {"input": "77 7\r\ncadgbagbcaecgfaegcdbeafbacbdfgaedgcdeabgebaecbeacgfebagedcegdafdgeacegfegfegf\r\n", "output": "cadgbagbcaecgfaegcdbeafbacbdfgaedgcdeabgebaecbeacgfebagedcegdafdgeacfabcabcab\r\n"}, {"input": "100 4\r\nabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabca\r\n", "output": "abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcd\r\n"}, {"input": "333 5\r\nedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedc\r\n", "output": "NO\r\n"}, {"input": "3 3\r\nacb\r\n", "output": "bac\r\n"}, {"input": "17 26\r\nbazyxzyxzyxzyxzyx\r\n", "output": "bcabcabcabcabcabc\r\n"}, {"input": "6 3\r\nacbacb\r\n", "output": "bacbac\r\n"}, {"input": "6 3\r\nabcabc\r\n", "output": "acbacb\r\n"}, {"input": "302 4\r\nabdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcbdcb\r\n", "output": "acbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbac\r\n"}, {"input": "30 26\r\nabcabcabczyxzyxzyxzyxzyxzyxzyx\r\n", "output": "abcabcabdabcabcabcabcabcabcabc\r\n"}, {"input": "300 3\r\nabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc\r\n", "output": "acbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacbacb\r\n"}, {"input": "2 4\r\ncd\r\n", "output": "da\r\n"}]
false
stdio
null
true
837/C
837
C
Python 3
TESTS
12
62
4,915,200
29461595
def ldc( nt, mt, a, b ): index = 0 if( nt >= a and mt >= b ): index = 1 elif( nt >= b and mt >= a ): index = 1 if( index == 1 ): return 1 else: return 0 t, n ,m = map(int,input().split()) list_a = [] list_b = [] list_s = [] max_s = 0 for i in range( t ): a, b = map(int,input().split()) list_a.append( a ) list_b.append( b ) list_s.append( a*b ) for i in range( t ): if( ldc( n, m, list_a[i], list_b[i] ) == 1 ): for j in range( t ): if( i != j ): index_t = 0; if( ldc( n-list_a[i], m, list_a[j], list_b[j] ) == 1 ): index_t = 1 elif( ldc( m-list_a[i], n, list_a[j], list_b[j] ) == 1 ): index_t = 1 if(index_t == 1 ): s = list_s[i] + list_s[j] if( max_s < s ): max_s = s print( max_s )
51
77
4,608,000
29163987
n, a, b = map(int, input().split()) ps = [] for i in range(n): ps.append(tuple(map(int, input().split()))) res = 0 def is_ok(p, a, b): return (p[0] <= a and p[1] <= b) or (p[0] <= b and p[1] <= a) def is_pair_ok(p1, p2): an = a - p1[0] bn = b - p1[1] if an < 0 or bn < 0: return False return is_ok(p2, bn, a) or is_ok(p2, an, b) for i1, p1 in enumerate(ps): for i2, p2 in enumerate(ps): if i1 == i2: continue cres = p1[0] * p1[1] + p2[0] * p2[1] if is_pair_ok(p1, p2) or is_pair_ok((p1[1], p1[0]), p2): res = max(cres, res) print(res)
Educational Codeforces Round 26
ICPC
2,017
1
256
Two Seals
One very important person has a piece of paper in the form of a rectangle a × b. Also, he has n seals. Each seal leaves an impression on the paper in the form of a rectangle of the size xi × yi. Each impression must be parallel to the sides of the piece of paper (but seal can be rotated by 90 degrees). A very important person wants to choose two different seals and put them two impressions. Each of the selected seals puts exactly one impression. Impressions should not overlap (but they can touch sides), and the total area occupied by them should be the largest possible. What is the largest area that can be occupied by two seals?
The first line contains three integer numbers n, a and b (1 ≤ n, a, b ≤ 100). Each of the next n lines contain two numbers xi, yi (1 ≤ xi, yi ≤ 100).
Print the largest total area that can be occupied by two seals. If you can not select two seals, print 0.
null
In the first example you can rotate the second seal by 90 degrees. Then put impression of it right under the impression of the first seal. This will occupy all the piece of paper. In the second example you can't choose the last seal because it doesn't fit. By choosing the first and the third seals you occupy the largest area. In the third example there is no such pair of seals that they both can fit on a piece of paper.
[{"input": "2 2 2\n1 2\n2 1", "output": "4"}, {"input": "4 10 9\n2 3\n1 1\n5 10\n9 11", "output": "56"}, {"input": "3 10 10\n6 6\n7 7\n20 5", "output": "0"}]
1,500
["brute force", "implementation"]
51
[{"input": "2 2 2\r\n1 2\r\n2 1\r\n", "output": "4\r\n"}, {"input": "4 10 9\r\n2 3\r\n1 1\r\n5 10\r\n9 11\r\n", "output": "56\r\n"}, {"input": "3 10 10\r\n6 6\r\n7 7\r\n20 5\r\n", "output": "0\r\n"}, {"input": "2 1 1\r\n1 1\r\n1 1\r\n", "output": "0\r\n"}, {"input": "2 1 2\r\n1 1\r\n1 1\r\n", "output": "2\r\n"}, {"input": "2 100 100\r\n100 100\r\n1 1\r\n", "output": "0\r\n"}, {"input": "2 100 100\r\n50 100\r\n100 50\r\n", "output": "10000\r\n"}, {"input": "2 100 100\r\n100 100\r\n87 72\r\n", "output": "0\r\n"}, {"input": "5 100 100\r\n100 100\r\n100 100\r\n100 100\r\n100 100\r\n100 100\r\n", "output": "0\r\n"}, {"input": "15 50 50\r\n9 36\r\n28 14\r\n77 74\r\n35 2\r\n20 32\r\n83 85\r\n47 3\r\n41 50\r\n21 7\r\n38 46\r\n17 6\r\n79 90\r\n91 83\r\n9 33\r\n24 11\r\n", "output": "2374\r\n"}, {"input": "15 100 100\r\n100 100\r\n100 100\r\n100 100\r\n42 58\r\n80 22\r\n100 100\r\n100 100\r\n100 100\r\n100 100\r\n100 100\r\n48 42\r\n100 100\r\n100 100\r\n100 100\r\n100 100\r\n", "output": "4452\r\n"}, {"input": "30 100 100\r\n60 34\r\n29 82\r\n89 77\r\n39 1\r\n100 100\r\n82 12\r\n57 87\r\n93 43\r\n78 50\r\n38 55\r\n37 9\r\n67 5\r\n100 100\r\n100 100\r\n82 47\r\n3 71\r\n100 100\r\n19 26\r\n25 94\r\n89 5\r\n100 100\r\n32 1\r\n100 100\r\n34 3\r\n40 99\r\n100 100\r\n36 12\r\n100 100\r\n100 100\r\n100 100\r\n", "output": "8958\r\n"}, {"input": "3 100 1\r\n1 50\r\n1 60\r\n1 30\r\n", "output": "90\r\n"}, {"input": "3 1 60\r\n1 40\r\n2 2\r\n20 1\r\n", "output": "60\r\n"}, {"input": "4 1 100\r\n1 25\r\n25 1\r\n1 25\r\n2 100\r\n", "output": "50\r\n"}, {"input": "1 100 50\r\n4 20\r\n", "output": "0\r\n"}, {"input": "2 2 4\r\n3 1\r\n2 2\r\n", "output": "0\r\n"}, {"input": "2 2 4\r\n2 3\r\n2 1\r\n", "output": "8\r\n"}, {"input": "2 4 2\r\n1 2\r\n2 3\r\n", "output": "8\r\n"}, {"input": "2 1 4\r\n1 2\r\n1 2\r\n", "output": "4\r\n"}, {"input": "2 4 5\r\n2 4\r\n4 3\r\n", "output": "20\r\n"}, {"input": "2 1 4\r\n1 1\r\n3 3\r\n", "output": "0\r\n"}, {"input": "6 9 5\r\n4 5\r\n6 2\r\n1 4\r\n5 6\r\n3 7\r\n6 5\r\n", "output": "34\r\n"}, {"input": "6 8 5\r\n4 1\r\n3 3\r\n5 3\r\n6 7\r\n2 2\r\n5 4\r\n", "output": "35\r\n"}, {"input": "6 7 5\r\n6 4\r\n5 7\r\n4 7\r\n5 4\r\n1 1\r\n3 6\r\n", "output": "29\r\n"}, {"input": "6 9 7\r\n1 2\r\n1 5\r\n4 3\r\n4 7\r\n3 5\r\n6 7\r\n", "output": "57\r\n"}, {"input": "6 5 9\r\n2 3\r\n7 4\r\n1 5\r\n1 7\r\n2 5\r\n7 1\r\n", "output": "38\r\n"}, {"input": "2 4 2\r\n2 2\r\n1 3\r\n", "output": "0\r\n"}, {"input": "2 3 2\r\n3 2\r\n1 1\r\n", "output": "0\r\n"}, {"input": "6 7 5\r\n6 6\r\n4 7\r\n6 1\r\n4 1\r\n4 6\r\n1 5\r\n", "output": "34\r\n"}, {"input": "2 2 3\r\n1 2\r\n2 3\r\n", "output": "0\r\n"}, {"input": "2 2 2\r\n2 1\r\n1 1\r\n", "output": "3\r\n"}, {"input": "5 9 7\r\n6 7\r\n4 5\r\n2 7\r\n4 2\r\n5 8\r\n", "output": "56\r\n"}, {"input": "2 11 51\r\n1 10\r\n11 50\r\n", "output": "560\r\n"}, {"input": "5 9 7\r\n3 8\r\n7 6\r\n4 1\r\n5 8\r\n7 8\r\n", "output": "60\r\n"}, {"input": "2 4 6\r\n4 4\r\n4 2\r\n", "output": "24\r\n"}, {"input": "5 9 7\r\n1 6\r\n7 9\r\n1 5\r\n1 5\r\n7 3\r\n", "output": "27\r\n"}, {"input": "5 9 7\r\n5 2\r\n6 9\r\n1 4\r\n7 7\r\n6 4\r\n", "output": "59\r\n"}, {"input": "5 9 7\r\n6 7\r\n4 1\r\n1 2\r\n4 7\r\n5 6\r\n", "output": "58\r\n"}, {"input": "5 9 7\r\n2 8\r\n3 8\r\n2 8\r\n4 4\r\n2 2\r\n", "output": "40\r\n"}, {"input": "2 2 3\r\n1 4\r\n2 1\r\n", "output": "0\r\n"}, {"input": "5 9 7\r\n4 7\r\n3 9\r\n5 4\r\n3 4\r\n3 8\r\n", "output": "55\r\n"}, {"input": "5 9 7\r\n7 4\r\n6 9\r\n4 3\r\n7 5\r\n2 3\r\n", "output": "63\r\n"}, {"input": "2 2 3\r\n1 2\r\n2 2\r\n", "output": "6\r\n"}, {"input": "2 4 3\r\n2 1\r\n1 2\r\n", "output": "4\r\n"}, {"input": "2 4 6\r\n4 2\r\n4 4\r\n", "output": "24\r\n"}, {"input": "2 1 4\r\n3 2\r\n3 3\r\n", "output": "0\r\n"}]
false
stdio
null
true
532/E
533
E
PyPy 3
TESTS
31
93
2,560,000
201711367
n = int(input()) s1 = input() s2 = input() front = 0 rear = n-1 while s1[front]==s2[front]: front += 1 while s1[rear]==s2[rear]: rear -= 1 if front==rear: print(2) exit(0) if s1[front+1:rear+1]==s2[front:rear]: print(1) elif s1[front:rear]==s2[front+1:rear+1]: print(1) else: print(0)
89
77
2,252,800
221365162
def rl(): return list(map(int,input().split())) def ri(): return int(input()) def rs(): return input() def rm(): return map(int,input().split()) def check(a,b): i=0 n=len(b) while i<n: if a[i]!=b[i]:break i+=1 if i==n or b==a[:i]+a[i+1:]:return True def main(): n,s,t=ri(),rs(),rs() v1,v2=None,None ans=0 i=0 while i<n and s[i]==t[i]: i+=1 v1=s[:i]+t[i]+s[i:] if check(v1,t): ans+=1 v2=t[:i]+s[i]+t[i:] if v1==v2: return ans if check(v2,s): ans+=1 return ans print(main())
VK Cup 2015 - Round 2
CF
2,015
2
256
Correcting Mistakes
Analyzing the mistakes people make while typing search queries is a complex and an interesting work. As there is no guaranteed way to determine what the user originally meant by typing some query, we have to use different sorts of heuristics. Polycarp needed to write a code that could, given two words, check whether they could have been obtained from the same word as a result of typos. Polycarpus suggested that the most common typo is skipping exactly one letter as you type a word. Implement a program that can, given two distinct words S and T of the same length n determine how many words W of length n + 1 are there with such property that you can transform W into both S, and T by deleting exactly one character. Words S and T consist of lowercase English letters. Word W also should consist of lowercase English letters.
The first line contains integer n (1 ≤ n ≤ 100 000) — the length of words S and T. The second line contains word S. The third line contains word T. Words S and T consist of lowercase English letters. It is guaranteed that S and T are distinct words.
Print a single integer — the number of distinct words W that can be transformed to S and T due to a typo.
null
In the first sample test the two given words could be obtained only from word "treading" (the deleted letters are marked in bold). In the second sample test the two given words couldn't be obtained from the same word by removing one letter. In the third sample test the two given words could be obtained from either word "tory" or word "troy".
[{"input": "7\nreading\ntrading", "output": "1"}, {"input": "5\nsweet\nsheep", "output": "0"}, {"input": "3\ntoy\ntry", "output": "2"}]
1,800
[]
89
[{"input": "7\r\nreading\r\ntrading\r\n", "output": "1\n"}, {"input": "5\r\nsweet\r\nsheep\r\n", "output": "0\n"}, {"input": "3\r\ntoy\r\ntry\r\n", "output": "2\n"}, {"input": "5\r\nspare\r\nspars\r\n", "output": "2\n"}, {"input": "1\r\na\r\nb\r\n", "output": "2\n"}, {"input": "1\r\nz\r\ny\r\n", "output": "2\n"}, {"input": "2\r\nab\r\nac\r\n", "output": "2\n"}, {"input": "2\r\nba\r\nca\r\n", "output": "2\n"}, {"input": "2\r\nac\r\ncb\r\n", "output": "1\n"}, {"input": "100\r\neebdeddddbecdbddaaecbbaccbecdeacedddcaddcdebedbabbceeeadecadbbeaecdaeabbceacbdbdbbdacebbbccdcbbeedbe\r\ndacdeebebeaeaacdeedadbcbaedcbddddddedacbabeddebaaebbdcebebaaccbaeccbecdbcbceadaaecadecbadbcddcdabecd\r\n", "output": "0\n"}, {"input": "250\r\niiffiehchidfgigdbcciahdehjjfacbbaaadagaibjjcehjcbjdhaadebaejiicgidbhajfbfejcdicgfbcchgbahfccbefdcddbjjhejigiafhdjbiiehadfficicbebeeegcebideijidbgdecffeaegjfjbbcfiabfbaiddbjgidebdiccfcgfbcbbfhaejaibeicghecchjbiaceaibfgibhgcfgifiedcbhhfadhccfdhejeggcah\r\njbadcfjffcfabbecfabgcafgfcgfeffjjhhdaajjgcbgbechhiadfahjidcdiefhbabhjhjijghghcgghcefhidhdgficiffdjgfdahcaicidfghiedgihbbjgicjeiacihdihfhadjhccddhigiibafiafficegaiehabafiiecbjcbfhdbeaebigaijehhdbfeehbcahaggbbdjcdbgbiajgeigdeabdbddbgcgjibfdgjghhdidjdhh\r\n", "output": "0\n"}, {"input": "100\r\nabababbbababbababaaabbbbaaaabbabbabbabababbbaaaabbababbbbababbabbbaaababababbbaaaabbbabbababbbbbbaba\r\nabababbbababbababaaabbbbaaaabbabbabbabababbbaaaabaababbbbababbabbbaaababababbbaaaabbbabbababbbbbbaba\r\n", "output": "2\n"}, {"input": "100\r\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n", "output": "2\n"}, {"input": "100\r\naaaaaaaaaaaaaaaaaaaaaalaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\naaaaaaaaaaaaaaaaaaaaaakaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n", "output": "2\n"}, {"input": "100\r\ndwtsrrtztfuibkrpwbxjrcxsonrwoydkmbhxrghekvusiyzqkyvulrvtfxmvrphpzpmazizogfbyauxtjfesocssnxvjjdedomlz\r\ndwtsrrtztfuibkrpwbxjrcxsonrwoydkmbhxrghekvusiyzqkyvulrvtfxmvrphpzpzazizogfbyauxtjfesocssnxvjjdedomlz\r\n", "output": "2\n"}, {"input": "100\r\naaabaabbbababbbaabbbbbaaababbabbaaabbabaabbabbabbbbbabbaaabbbbbbbbbbbbbbbababaaababbaaabeabbabaabbab\r\naaabaabbbababbbaabbbbbaaababbabbaaabbabaabbabbabbbbbabbaaabbbbbbbbbbbbbbbababaaababbaaabtabbabaabbab\r\n", "output": "2\n"}, {"input": "100\r\naaaabaaaaabbaababaaabaababaabbbaabaaabbbaaababbabaabbabababbaaabaabababbbababbbabbaaaabbbbbbbaaababa\r\naaaabaaaaabbaababaaabaababaabbbaabaaabbbaaaabbbabaabbabababbaaabaabababbbababbbabbaaaabbbbbbbaaababa\r\n", "output": "2\n"}, {"input": "100\r\neebdeddddbecdbddaaecbbaccbecdeacedddcaddcdebedbabbceeeadecadbbeaecdaeabbceacbdbdbbdacebbbccdcbbeedbe\r\needbeddddbecdbddaaecbbaccbecdeacedddcaddcdebedbabbceeeadecadbbeaecdaeabbceacbdbdbbdacebbbccdcbbeedbe\r\n", "output": "2\n"}, {"input": "100\r\nxjywrmrwqaytezhtqmcnrrjomslvcmevncvzeddnvqgkbusnbzrppdsuzsmcobmnslpvosunavayvdbxhtavvwodorwijxfjjlat\r\nxjywrmrwqaytezhtqmcrnrjomslvcmevncvzeddnvqgkbusnbzrppdsuzsmcobmnslpvosunavayvdbxhtavvwodorwijxfjjlat\r\n", "output": "2\n"}, {"input": "4\r\nbbca\r\nabab\r\n", "output": "0\n"}, {"input": "4\r\nabcb\r\nccac\r\n", "output": "0\n"}, {"input": "4\r\ncaaa\r\nabab\r\n", "output": "0\n"}, {"input": "4\r\nacca\r\nbabb\r\n", "output": "0\n"}, {"input": "4\r\nccba\r\nbabb\r\n", "output": "0\n"}, {"input": "4\r\nbcca\r\ncbaa\r\n", "output": "0\n"}, {"input": "4\r\naaca\r\ncaab\r\n", "output": "0\n"}, {"input": "4\r\nbaab\r\nbcbc\r\n", "output": "0\n"}, {"input": "4\r\nabba\r\ncaca\r\n", "output": "0\n"}, {"input": "4\r\nbcbb\r\nccac\r\n", "output": "0\n"}, {"input": "4\r\ncbba\r\nabba\r\n", "output": "2\n"}, {"input": "4\r\nbaca\r\nccbc\r\n", "output": "0\n"}, {"input": "4\r\ncabc\r\naacc\r\n", "output": "0\n"}, {"input": "4\r\nbbab\r\ncbaa\r\n", "output": "0\n"}, {"input": "4\r\nabcc\r\nbcab\r\n", "output": "0\n"}, {"input": "4\r\nbaaa\r\nbbbc\r\n", "output": "0\n"}, {"input": "4\r\naabc\r\naacb\r\n", "output": "2\n"}, {"input": "4\r\nccbb\r\nbbcb\r\n", "output": "0\n"}, {"input": "4\r\nbaba\r\naccc\r\n", "output": "0\n"}, {"input": "4\r\nbbbc\r\nbbab\r\n", "output": "1\n"}, {"input": "2\r\nab\r\nba\r\n", "output": "2\n"}, {"input": "5\r\ncabac\r\ncbabc\r\n", "output": "2\n"}, {"input": "3\r\naba\r\nbab\r\n", "output": "2\n"}, {"input": "5\r\nabxxx\r\nbayyy\r\n", "output": "0\n"}, {"input": "4\r\nxaxa\r\naxax\r\n", "output": "2\n"}, {"input": "5\r\nababa\r\nbabab\r\n", "output": "2\n"}, {"input": "5\r\nbabab\r\nababa\r\n", "output": "2\n"}, {"input": "154\r\nwqpewhyutqnhaewqpewhywqpewhyutqnhaeutqnhaeutqnhaewqpewhyutqnhaewqpewhywqpewhyutqnhaeutqnhaeutqnhaeutqnhaewqpewhyutqnhaewqpewhywqpewhywqpewhywqpewhyutqnhae\r\nutqnhaeutqnhaeutqnhaewqpewhywqpewhyutqnhaewqpewhyutqnhaewqpewhywqpewhyutqnhaeutqnhaeutqnhaewqpewhyutqnhaewqpewhywqpewhywqpewhyutqnhaewqpewhyutqnhaewqpewhy\r\n", "output": "0\n"}, {"input": "7\r\ntrading\r\nrtading\r\n", "output": "2\n"}, {"input": "5\r\nxabax\r\nxbabx\r\n", "output": "2\n"}, {"input": "3\r\nabc\r\nacb\r\n", "output": "2\n"}, {"input": "4\r\nabab\r\nbaba\r\n", "output": "2\n"}, {"input": "3\r\naab\r\naba\r\n", "output": "2\n"}, {"input": "2\r\ner\r\nre\r\n", "output": "2\n"}, {"input": "5\r\ntabat\r\ntbaat\r\n", "output": "2\n"}]
false
stdio
null
true
962/B
962
B
PyPy 3-64
TESTS
9
62
13,824,000
164061928
import sys input = sys.stdin.readline n, a, b = map(int, input().split()) s = list(input()[:-1]) x = a + b for i in range(n): if i == 0: if s[i] == '.': if a >= b and a > 0: a -= 1 s[i] = 'a' elif b > a and b > 0: b -= 1 s[i] = 'b' else: if s[i] == '.': if s[i-1] == 'a' and b > 0: b -= 1 s[i] = 'b' elif s[i-1] == 'b' and a > 0: a -= 1 s[i] = 'a' elif s[i-1] == '*': if a >= b and a > 0: a -= 1 s[i] = 'a' elif b > a and b > 0: b -= 1 s[i] = 'b' print(x - a - b)
93
93
3,788,800
183744953
N = int(1e5 + 7) oo = int(1e10) M = int(1e9 + 7) MULTIPLE_TEST = 0 def ReadInt(): return int(input()) def ReadInts(): return map(int, input().split()) def ReadArr(): return list(map(int, input().split())) def main(): n, a, b = ReadInts() s = input() seat_lenght = [] length = 0 for ch in s: if ch == '*' and length != 0: seat_lenght.append(length) length = 0 elif ch == '.': length += 1 ans = 0 if length > 0: seat_lenght.append(length) for L in seat_lenght: if a < b: a, b = b, a more = L // 2 + L % 2 less = L // 2 ans += min(more, a) ans += min(less, b) a -= min(more, a) b -= min(less, b) print(ans) if __name__ == '__main__': t = 1 if MULTIPLE_TEST : t = ReadInt() while t > 0: t -= 1 main()
Educational Codeforces Round 42 (Rated for Div. 2)
ICPC
2,018
2
256
Students in Railway Carriage
There are $$$n$$$ consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger. The university team for the Olympiad consists of $$$a$$$ student-programmers and $$$b$$$ student-athletes. Determine the largest number of students from all $$$a+b$$$ students, which you can put in the railway carriage so that: - no student-programmer is sitting next to the student-programmer; - and no student-athlete is sitting next to the student-athlete. In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting. Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all).
The first line contain three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$1 \le n \le 2\cdot10^{5}$$$, $$$0 \le a, b \le 2\cdot10^{5}$$$, $$$a + b > 0$$$) — total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes. The second line contains a string with length $$$n$$$, consisting of characters "." and "*". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member.
Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete.
null
In the first example you can put all student, for example, in the following way: *.AB* In the second example you can put four students, for example, in the following way: *BAB*B In the third example you can put seven students, for example, in the following way: B*ABAB**A*B The letter A means a student-programmer, and the letter B — student-athlete.
[{"input": "5 1 1\n*...*", "output": "2"}, {"input": "6 2 3\n*...*.", "output": "4"}, {"input": "11 3 10\n.*....**.*.", "output": "7"}, {"input": "3 2 3\n***", "output": "0"}]
1,300
["constructive algorithms", "greedy", "implementation"]
93
[{"input": "5 1 1\r\n*...*\r\n", "output": "2\r\n"}, {"input": "6 2 3\r\n*...*.\r\n", "output": "4\r\n"}, {"input": "11 3 10\r\n.*....**.*.\r\n", "output": "7\r\n"}, {"input": "3 2 3\r\n***\r\n", "output": "0\r\n"}, {"input": "9 5 3\r\n*...*...*\r\n", "output": "6\r\n"}, {"input": "9 2 4\r\n*...*...*\r\n", "output": "6\r\n"}, {"input": "9 2 200000\r\n*...*...*\r\n", "output": "6\r\n"}, {"input": "1 0 1\r\n.\r\n", "output": "1\r\n"}, {"input": "20 5 5\r\n.*.*.........*......\r\n", "output": "10\r\n"}, {"input": "14 3 7\r\n.*.......*..*.\r\n", "output": "10\r\n"}, {"input": "6 1 3\r\n*....*\r\n", "output": "3\r\n"}, {"input": "9 2 4\r\n..*.*....\r\n", "output": "6\r\n"}, {"input": "5 1 2\r\n...*.\r\n", "output": "3\r\n"}, {"input": "2 2 0\r\n..\r\n", "output": "1\r\n"}, {"input": "2 0 2\r\n..\r\n", "output": "1\r\n"}, {"input": "10 1 1\r\n..........\r\n", "output": "2\r\n"}, {"input": "4 0 1\r\n....\r\n", "output": "1\r\n"}, {"input": "5 3 3\r\n...**\r\n", "output": "3\r\n"}, {"input": "3 0 1\r\n.*.\r\n", "output": "1\r\n"}, {"input": "4 2 2\r\n....\r\n", "output": "4\r\n"}, {"input": "13 3 3\r\n*...*...*...*\r\n", "output": "6\r\n"}, {"input": "5 10 1\r\n*....\r\n", "output": "3\r\n"}, {"input": "7 0 4\r\n...*..*\r\n", "output": "3\r\n"}, {"input": "20 5 5\r\n.*.*.............*..\r\n", "output": "10\r\n"}, {"input": "6 2 1\r\n..*...\r\n", "output": "3\r\n"}, {"input": "17 11 2\r\n.*..*..*.*.***...\r\n", "output": "9\r\n"}, {"input": "5 2 3\r\n.....\r\n", "output": "5\r\n"}, {"input": "64 59 2\r\n.*.***......****.*..**..**..****.*.*.*.**...**..***.***.*..*..*.\r\n", "output": "23\r\n"}, {"input": "5 1 2\r\n.*...\r\n", "output": "3\r\n"}, {"input": "2 1 1\r\n..\r\n", "output": "2\r\n"}, {"input": "10 15 15\r\n..........\r\n", "output": "10\r\n"}, {"input": "10 7 0\r\n.*...*..*.\r\n", "output": "5\r\n"}, {"input": "5 0 1\r\n.....\r\n", "output": "1\r\n"}, {"input": "4 1 1\r\n..*.\r\n", "output": "2\r\n"}, {"input": "10 4 6\r\n..........\r\n", "output": "9\r\n"}, {"input": "5 1 4\r\n.....\r\n", "output": "4\r\n"}, {"input": "10 4 3\r\n.*..*...*.\r\n", "output": "7\r\n"}, {"input": "4 2 0\r\n....\r\n", "output": "2\r\n"}, {"input": "5 0 2\r\n.....\r\n", "output": "2\r\n"}, {"input": "5 0 1\r\n*.*.*\r\n", "output": "1\r\n"}, {"input": "10 20 0\r\n..........\r\n", "output": "5\r\n"}, {"input": "10 8 1\r\n.*.*......\r\n", "output": "6\r\n"}, {"input": "6 1 1\r\n*...*.\r\n", "output": "2\r\n"}, {"input": "7 1 0\r\n.*.....\r\n", "output": "1\r\n"}, {"input": "1 1 1\r\n.\r\n", "output": "1\r\n"}, {"input": "10 5 1\r\n..........\r\n", "output": "6\r\n"}, {"input": "4 3 0\r\n....\r\n", "output": "2\r\n"}, {"input": "11 6 2\r\n.*...*...*.\r\n", "output": "8\r\n"}, {"input": "11 7 1\r\n.*...*...*.\r\n", "output": "7\r\n"}]
false
stdio
null
true
350/A
350
A
Python 3
TESTS
28
154
0
15832160
n, m = map(int, input().split()) g = list(map(int, input().split())) b = min(list(map(int, input().split()))) g_max = max(g) g_min = min(g) print(g_max if g_min*2<=g_max and g_max<b else (g_min*2 if g_min*2<b else -1))
45
92
0
4622113
n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) maxa = 0 mina = 101 for i in a: if i > maxa: maxa = i if i < mina: mina = i minb = 101 for i in b: if i < minb: minb = i if max(mina * 2, maxa) >= minb: print(-1) else: print(max(mina * 2, maxa))
Codeforces Round 203 (Div. 2)
CF
2,013
2
256
TL
Valera wanted to prepare a Codesecrof round. He's already got one problem and he wants to set a time limit (TL) on it. Valera has written n correct solutions. For each correct solution, he knows its running time (in seconds). Valera has also wrote m wrong solutions and for each wrong solution he knows its running time (in seconds). Let's suppose that Valera will set v seconds TL in the problem. Then we can say that a solution passes the system testing if its running time is at most v seconds. We can also say that a solution passes the system testing with some "extra" time if for its running time, a seconds, an inequality 2a ≤ v holds. As a result, Valera decided to set v seconds TL, that the following conditions are met: 1. v is a positive integer; 2. all correct solutions pass the system testing; 3. at least one correct solution passes the system testing with some "extra" time; 4. all wrong solutions do not pass the system testing; 5. value v is minimum among all TLs, for which points 1, 2, 3, 4 hold. Help Valera and find the most suitable TL or else state that such TL doesn't exist.
The first line contains two integers n, m (1 ≤ n, m ≤ 100). The second line contains n space-separated positive integers a1, a2, ..., an (1 ≤ ai ≤ 100) — the running time of each of the n correct solutions in seconds. The third line contains m space-separated positive integers b1, b2, ..., bm (1 ≤ bi ≤ 100) — the running time of each of m wrong solutions in seconds.
If there is a valid TL value, print it. Otherwise, print -1.
null
null
[{"input": "3 6\n4 5 2\n8 9 6 10 7 11", "output": "5"}, {"input": "3 1\n3 4 5\n6", "output": "-1"}]
1,200
["brute force", "greedy", "implementation"]
45
[{"input": "3 6\r\n4 5 2\r\n8 9 6 10 7 11\r\n", "output": "5"}, {"input": "3 1\r\n3 4 5\r\n6\r\n", "output": "-1\r\n"}, {"input": "2 5\r\n45 99\r\n49 41 77 83 45\r\n", "output": "-1\r\n"}, {"input": "50 50\r\n18 13 5 34 10 36 36 12 15 11 16 17 14 36 23 45 32 24 31 18 24 32 7 1 31 3 49 8 16 23 3 39 47 43 42 38 40 22 41 1 49 47 9 8 19 15 29 30 16 18\r\n91 58 86 51 94 94 73 84 98 69 74 56 52 80 88 61 53 99 88 50 55 95 65 84 87 79 51 52 69 60 74 73 93 61 73 59 64 56 95 78 86 72 79 70 93 78 54 61 71 50\r\n", "output": "49"}, {"input": "55 44\r\n93 17 74 15 34 16 41 80 26 54 94 94 86 93 20 44 63 72 39 43 67 4 37 49 76 94 5 51 64 74 11 47 77 97 57 30 42 72 71 26 8 14 67 64 49 57 30 23 40 4 76 78 87 78 79\r\n38 55 17 65 26 7 36 65 48 28 49 93 18 98 31 90 26 57 1 26 88 56 48 56 23 13 8 67 80 2 51 3 21 33 20 54 2 45 21 36 3 98 62 2\r\n", "output": "-1\r\n"}, {"input": "32 100\r\n30 8 4 35 18 41 18 12 33 39 39 18 39 19 33 46 45 33 34 27 14 39 40 21 38 9 42 35 27 10 14 14\r\n65 49 89 64 47 78 59 52 73 51 84 82 88 63 91 99 67 87 53 99 75 47 85 82 58 47 80 50 65 91 83 90 77 52 100 88 97 74 98 99 50 93 65 61 65 65 65 96 61 51 84 67 79 90 92 83 100 100 100 95 80 54 77 51 98 64 74 62 60 96 73 74 94 55 89 60 92 65 74 79 66 81 53 47 71 51 54 85 74 97 68 72 88 94 100 85 65 63 65 90\r\n", "output": "46"}, {"input": "1 50\r\n7\r\n65 52 99 78 71 19 96 72 80 15 50 94 20 35 79 95 44 41 45 53 77 50 74 66 59 96 26 84 27 48 56 84 36 78 89 81 67 34 79 74 99 47 93 92 90 96 72 28 78 66\r\n", "output": "14"}, {"input": "1 1\r\n4\r\n9\r\n", "output": "8"}, {"input": "1 1\r\n2\r\n4\r\n", "output": "-1\r\n"}, {"input": "22 56\r\n49 20 42 68 15 46 98 78 82 8 7 33 50 30 75 96 36 88 35 99 19 87\r\n15 18 81 24 35 89 25 32 23 3 48 24 52 69 18 32 23 61 48 98 50 38 5 17 70 20 38 32 49 54 68 11 51 81 46 22 19 59 29 38 45 83 18 13 91 17 84 62 25 60 97 32 23 13 83 58\r\n", "output": "-1\r\n"}, {"input": "1 1\r\n50\r\n100\r\n", "output": "-1\r\n"}, {"input": "1 1\r\n49\r\n100\r\n", "output": "98"}, {"input": "1 1\r\n100\r\n100\r\n", "output": "-1\r\n"}, {"input": "1 1\r\n99\r\n100\r\n", "output": "-1\r\n"}, {"input": "8 4\r\n1 2 49 99 99 95 78 98\r\n100 100 100 100\r\n", "output": "99"}, {"input": "68 85\r\n43 55 2 4 72 45 19 56 53 81 18 90 11 87 47 8 94 88 24 4 67 9 21 70 25 66 65 27 46 13 8 51 65 99 37 43 71 59 71 79 32 56 49 43 57 85 95 81 40 28 60 36 72 81 60 40 16 78 61 37 29 26 15 95 70 27 50 97\r\n6 6 48 72 54 31 1 50 29 64 93 9 29 93 66 63 25 90 52 1 66 13 70 30 24 87 32 90 84 72 44 13 25 45 31 16 92 60 87 40 62 7 20 63 86 78 73 88 5 36 74 100 64 34 9 5 62 29 58 48 81 46 84 56 27 1 60 14 54 88 31 93 62 7 9 69 27 48 10 5 33 10 53 66 2\r\n", "output": "-1\r\n"}, {"input": "5 100\r\n1 1 1 1 1\r\n77 53 38 29 97 33 64 17 78 100 27 12 42 44 20 24 44 68 58 57 65 90 8 24 4 6 74 68 61 43 25 69 8 62 36 85 67 48 69 30 35 41 42 12 87 66 50 92 53 76 38 67 85 7 80 78 53 76 94 8 37 50 4 100 4 71 10 48 34 47 83 42 25 81 64 72 25 51 53 75 43 98 53 77 94 38 81 15 89 91 72 76 7 36 27 41 88 18 19 75\r\n", "output": "2"}, {"input": "3 3\r\n2 3 4\r\n8 9 10\r\n", "output": "4"}, {"input": "2 1\r\n2 3\r\n15\r\n", "output": "4"}, {"input": "2 1\r\n2 4\r\n4\r\n", "output": "-1\r\n"}, {"input": "2 3\r\n4 5\r\n10 11 12\r\n", "output": "8"}, {"input": "3 1\r\n2 3 3\r\n5\r\n", "output": "4"}, {"input": "2 1\r\n9 10\r\n100\r\n", "output": "18"}, {"input": "3 3\r\n3 12 15\r\n7 8 9\r\n", "output": "-1\r\n"}, {"input": "2 2\r\n3 5\r\n7 8\r\n", "output": "6"}, {"input": "3 3\r\n4 5 6\r\n10 11 12\r\n", "output": "8"}, {"input": "3 5\r\n2 3 3\r\n6 6 6 6 2\r\n", "output": "-1\r\n"}, {"input": "3 6\r\n4 5 3\r\n8 9 7 10 7 11\r\n", "output": "6"}, {"input": "3 6\r\n4 5 2\r\n8 9 6 10 7 4\r\n", "output": "-1\r\n"}, {"input": "2 1\r\n4 6\r\n10\r\n", "output": "8"}, {"input": "1 2\r\n1\r\n3 1\r\n", "output": "-1\r\n"}, {"input": "2 1\r\n5 6\r\n20\r\n", "output": "10"}, {"input": "2 1\r\n1 5\r\n5\r\n", "output": "-1\r\n"}, {"input": "3 2\r\n10 20 30\r\n30 40\r\n", "output": "-1\r\n"}, {"input": "2 2\r\n5 6\r\n7 100\r\n", "output": "-1\r\n"}, {"input": "2 1\r\n2 5\r\n7\r\n", "output": "5"}, {"input": "1 1\r\n5\r\n20\r\n", "output": "10"}, {"input": "2 1\r\n10 11\r\n100\r\n", "output": "20"}, {"input": "1 1\r\n1\r\n10\r\n", "output": "2"}, {"input": "1 1\r\n10\r\n100\r\n", "output": "20"}]
false
stdio
null
true
350/A
350
A
Python 3
TESTS
28
92
0
171727953
a,b=map(int,input().split()) a=[int(i) for i in input().split()] b=[int(i) for i in input().split()] a=sorted(a) b=sorted(b) x=a[0]*2 if(x>=a[-1] and x<b[0]): print(x) elif(x<a[-1] and x<b[0]): print(a[-1]) else: print(-1)
45
92
0
4622901
a, b = map(int, input().split()) P = list(map(int, input().split())) flag = 0 M = P[0] m = P[0] for i in range(a): M = max(M, P[i]) m = min(m, P[i]) s = max(M, 2 * m) A = list(map(int, input().split())) for i in range(b): if A[i] <= s: flag = 1 if flag == 1: print(-1) else: print(s)
Codeforces Round 203 (Div. 2)
CF
2,013
2
256
TL
Valera wanted to prepare a Codesecrof round. He's already got one problem and he wants to set a time limit (TL) on it. Valera has written n correct solutions. For each correct solution, he knows its running time (in seconds). Valera has also wrote m wrong solutions and for each wrong solution he knows its running time (in seconds). Let's suppose that Valera will set v seconds TL in the problem. Then we can say that a solution passes the system testing if its running time is at most v seconds. We can also say that a solution passes the system testing with some "extra" time if for its running time, a seconds, an inequality 2a ≤ v holds. As a result, Valera decided to set v seconds TL, that the following conditions are met: 1. v is a positive integer; 2. all correct solutions pass the system testing; 3. at least one correct solution passes the system testing with some "extra" time; 4. all wrong solutions do not pass the system testing; 5. value v is minimum among all TLs, for which points 1, 2, 3, 4 hold. Help Valera and find the most suitable TL or else state that such TL doesn't exist.
The first line contains two integers n, m (1 ≤ n, m ≤ 100). The second line contains n space-separated positive integers a1, a2, ..., an (1 ≤ ai ≤ 100) — the running time of each of the n correct solutions in seconds. The third line contains m space-separated positive integers b1, b2, ..., bm (1 ≤ bi ≤ 100) — the running time of each of m wrong solutions in seconds.
If there is a valid TL value, print it. Otherwise, print -1.
null
null
[{"input": "3 6\n4 5 2\n8 9 6 10 7 11", "output": "5"}, {"input": "3 1\n3 4 5\n6", "output": "-1"}]
1,200
["brute force", "greedy", "implementation"]
45
[{"input": "3 6\r\n4 5 2\r\n8 9 6 10 7 11\r\n", "output": "5"}, {"input": "3 1\r\n3 4 5\r\n6\r\n", "output": "-1\r\n"}, {"input": "2 5\r\n45 99\r\n49 41 77 83 45\r\n", "output": "-1\r\n"}, {"input": "50 50\r\n18 13 5 34 10 36 36 12 15 11 16 17 14 36 23 45 32 24 31 18 24 32 7 1 31 3 49 8 16 23 3 39 47 43 42 38 40 22 41 1 49 47 9 8 19 15 29 30 16 18\r\n91 58 86 51 94 94 73 84 98 69 74 56 52 80 88 61 53 99 88 50 55 95 65 84 87 79 51 52 69 60 74 73 93 61 73 59 64 56 95 78 86 72 79 70 93 78 54 61 71 50\r\n", "output": "49"}, {"input": "55 44\r\n93 17 74 15 34 16 41 80 26 54 94 94 86 93 20 44 63 72 39 43 67 4 37 49 76 94 5 51 64 74 11 47 77 97 57 30 42 72 71 26 8 14 67 64 49 57 30 23 40 4 76 78 87 78 79\r\n38 55 17 65 26 7 36 65 48 28 49 93 18 98 31 90 26 57 1 26 88 56 48 56 23 13 8 67 80 2 51 3 21 33 20 54 2 45 21 36 3 98 62 2\r\n", "output": "-1\r\n"}, {"input": "32 100\r\n30 8 4 35 18 41 18 12 33 39 39 18 39 19 33 46 45 33 34 27 14 39 40 21 38 9 42 35 27 10 14 14\r\n65 49 89 64 47 78 59 52 73 51 84 82 88 63 91 99 67 87 53 99 75 47 85 82 58 47 80 50 65 91 83 90 77 52 100 88 97 74 98 99 50 93 65 61 65 65 65 96 61 51 84 67 79 90 92 83 100 100 100 95 80 54 77 51 98 64 74 62 60 96 73 74 94 55 89 60 92 65 74 79 66 81 53 47 71 51 54 85 74 97 68 72 88 94 100 85 65 63 65 90\r\n", "output": "46"}, {"input": "1 50\r\n7\r\n65 52 99 78 71 19 96 72 80 15 50 94 20 35 79 95 44 41 45 53 77 50 74 66 59 96 26 84 27 48 56 84 36 78 89 81 67 34 79 74 99 47 93 92 90 96 72 28 78 66\r\n", "output": "14"}, {"input": "1 1\r\n4\r\n9\r\n", "output": "8"}, {"input": "1 1\r\n2\r\n4\r\n", "output": "-1\r\n"}, {"input": "22 56\r\n49 20 42 68 15 46 98 78 82 8 7 33 50 30 75 96 36 88 35 99 19 87\r\n15 18 81 24 35 89 25 32 23 3 48 24 52 69 18 32 23 61 48 98 50 38 5 17 70 20 38 32 49 54 68 11 51 81 46 22 19 59 29 38 45 83 18 13 91 17 84 62 25 60 97 32 23 13 83 58\r\n", "output": "-1\r\n"}, {"input": "1 1\r\n50\r\n100\r\n", "output": "-1\r\n"}, {"input": "1 1\r\n49\r\n100\r\n", "output": "98"}, {"input": "1 1\r\n100\r\n100\r\n", "output": "-1\r\n"}, {"input": "1 1\r\n99\r\n100\r\n", "output": "-1\r\n"}, {"input": "8 4\r\n1 2 49 99 99 95 78 98\r\n100 100 100 100\r\n", "output": "99"}, {"input": "68 85\r\n43 55 2 4 72 45 19 56 53 81 18 90 11 87 47 8 94 88 24 4 67 9 21 70 25 66 65 27 46 13 8 51 65 99 37 43 71 59 71 79 32 56 49 43 57 85 95 81 40 28 60 36 72 81 60 40 16 78 61 37 29 26 15 95 70 27 50 97\r\n6 6 48 72 54 31 1 50 29 64 93 9 29 93 66 63 25 90 52 1 66 13 70 30 24 87 32 90 84 72 44 13 25 45 31 16 92 60 87 40 62 7 20 63 86 78 73 88 5 36 74 100 64 34 9 5 62 29 58 48 81 46 84 56 27 1 60 14 54 88 31 93 62 7 9 69 27 48 10 5 33 10 53 66 2\r\n", "output": "-1\r\n"}, {"input": "5 100\r\n1 1 1 1 1\r\n77 53 38 29 97 33 64 17 78 100 27 12 42 44 20 24 44 68 58 57 65 90 8 24 4 6 74 68 61 43 25 69 8 62 36 85 67 48 69 30 35 41 42 12 87 66 50 92 53 76 38 67 85 7 80 78 53 76 94 8 37 50 4 100 4 71 10 48 34 47 83 42 25 81 64 72 25 51 53 75 43 98 53 77 94 38 81 15 89 91 72 76 7 36 27 41 88 18 19 75\r\n", "output": "2"}, {"input": "3 3\r\n2 3 4\r\n8 9 10\r\n", "output": "4"}, {"input": "2 1\r\n2 3\r\n15\r\n", "output": "4"}, {"input": "2 1\r\n2 4\r\n4\r\n", "output": "-1\r\n"}, {"input": "2 3\r\n4 5\r\n10 11 12\r\n", "output": "8"}, {"input": "3 1\r\n2 3 3\r\n5\r\n", "output": "4"}, {"input": "2 1\r\n9 10\r\n100\r\n", "output": "18"}, {"input": "3 3\r\n3 12 15\r\n7 8 9\r\n", "output": "-1\r\n"}, {"input": "2 2\r\n3 5\r\n7 8\r\n", "output": "6"}, {"input": "3 3\r\n4 5 6\r\n10 11 12\r\n", "output": "8"}, {"input": "3 5\r\n2 3 3\r\n6 6 6 6 2\r\n", "output": "-1\r\n"}, {"input": "3 6\r\n4 5 3\r\n8 9 7 10 7 11\r\n", "output": "6"}, {"input": "3 6\r\n4 5 2\r\n8 9 6 10 7 4\r\n", "output": "-1\r\n"}, {"input": "2 1\r\n4 6\r\n10\r\n", "output": "8"}, {"input": "1 2\r\n1\r\n3 1\r\n", "output": "-1\r\n"}, {"input": "2 1\r\n5 6\r\n20\r\n", "output": "10"}, {"input": "2 1\r\n1 5\r\n5\r\n", "output": "-1\r\n"}, {"input": "3 2\r\n10 20 30\r\n30 40\r\n", "output": "-1\r\n"}, {"input": "2 2\r\n5 6\r\n7 100\r\n", "output": "-1\r\n"}, {"input": "2 1\r\n2 5\r\n7\r\n", "output": "5"}, {"input": "1 1\r\n5\r\n20\r\n", "output": "10"}, {"input": "2 1\r\n10 11\r\n100\r\n", "output": "20"}, {"input": "1 1\r\n1\r\n10\r\n", "output": "2"}, {"input": "1 1\r\n10\r\n100\r\n", "output": "20"}]
false
stdio
null
true
350/A
350
A
PyPy 3-64
TESTS
28
92
0
201797489
n,m=map(int,input().split()) a,b=list(map(int,input().split())),list(map(int,input().split())) q=min(a) w=min(b) if q*2>=w: print("-1") else: if q*2 < max(a): print(max(a)) else: print(q*2)
45
92
0
4623333
n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) a.sort() b.sort() am = a[0] bm = min(b) i = b[0] - 1 while i >= 2*am and a[-1] <= i and i > 0: i -= 1 i+=1 if i == 0 or not(i >= 2*am and a[-1] <= i and i > 0) or bm <= i: print(-1) else: print(i)
Codeforces Round 203 (Div. 2)
CF
2,013
2
256
TL
Valera wanted to prepare a Codesecrof round. He's already got one problem and he wants to set a time limit (TL) on it. Valera has written n correct solutions. For each correct solution, he knows its running time (in seconds). Valera has also wrote m wrong solutions and for each wrong solution he knows its running time (in seconds). Let's suppose that Valera will set v seconds TL in the problem. Then we can say that a solution passes the system testing if its running time is at most v seconds. We can also say that a solution passes the system testing with some "extra" time if for its running time, a seconds, an inequality 2a ≤ v holds. As a result, Valera decided to set v seconds TL, that the following conditions are met: 1. v is a positive integer; 2. all correct solutions pass the system testing; 3. at least one correct solution passes the system testing with some "extra" time; 4. all wrong solutions do not pass the system testing; 5. value v is minimum among all TLs, for which points 1, 2, 3, 4 hold. Help Valera and find the most suitable TL or else state that such TL doesn't exist.
The first line contains two integers n, m (1 ≤ n, m ≤ 100). The second line contains n space-separated positive integers a1, a2, ..., an (1 ≤ ai ≤ 100) — the running time of each of the n correct solutions in seconds. The third line contains m space-separated positive integers b1, b2, ..., bm (1 ≤ bi ≤ 100) — the running time of each of m wrong solutions in seconds.
If there is a valid TL value, print it. Otherwise, print -1.
null
null
[{"input": "3 6\n4 5 2\n8 9 6 10 7 11", "output": "5"}, {"input": "3 1\n3 4 5\n6", "output": "-1"}]
1,200
["brute force", "greedy", "implementation"]
45
[{"input": "3 6\r\n4 5 2\r\n8 9 6 10 7 11\r\n", "output": "5"}, {"input": "3 1\r\n3 4 5\r\n6\r\n", "output": "-1\r\n"}, {"input": "2 5\r\n45 99\r\n49 41 77 83 45\r\n", "output": "-1\r\n"}, {"input": "50 50\r\n18 13 5 34 10 36 36 12 15 11 16 17 14 36 23 45 32 24 31 18 24 32 7 1 31 3 49 8 16 23 3 39 47 43 42 38 40 22 41 1 49 47 9 8 19 15 29 30 16 18\r\n91 58 86 51 94 94 73 84 98 69 74 56 52 80 88 61 53 99 88 50 55 95 65 84 87 79 51 52 69 60 74 73 93 61 73 59 64 56 95 78 86 72 79 70 93 78 54 61 71 50\r\n", "output": "49"}, {"input": "55 44\r\n93 17 74 15 34 16 41 80 26 54 94 94 86 93 20 44 63 72 39 43 67 4 37 49 76 94 5 51 64 74 11 47 77 97 57 30 42 72 71 26 8 14 67 64 49 57 30 23 40 4 76 78 87 78 79\r\n38 55 17 65 26 7 36 65 48 28 49 93 18 98 31 90 26 57 1 26 88 56 48 56 23 13 8 67 80 2 51 3 21 33 20 54 2 45 21 36 3 98 62 2\r\n", "output": "-1\r\n"}, {"input": "32 100\r\n30 8 4 35 18 41 18 12 33 39 39 18 39 19 33 46 45 33 34 27 14 39 40 21 38 9 42 35 27 10 14 14\r\n65 49 89 64 47 78 59 52 73 51 84 82 88 63 91 99 67 87 53 99 75 47 85 82 58 47 80 50 65 91 83 90 77 52 100 88 97 74 98 99 50 93 65 61 65 65 65 96 61 51 84 67 79 90 92 83 100 100 100 95 80 54 77 51 98 64 74 62 60 96 73 74 94 55 89 60 92 65 74 79 66 81 53 47 71 51 54 85 74 97 68 72 88 94 100 85 65 63 65 90\r\n", "output": "46"}, {"input": "1 50\r\n7\r\n65 52 99 78 71 19 96 72 80 15 50 94 20 35 79 95 44 41 45 53 77 50 74 66 59 96 26 84 27 48 56 84 36 78 89 81 67 34 79 74 99 47 93 92 90 96 72 28 78 66\r\n", "output": "14"}, {"input": "1 1\r\n4\r\n9\r\n", "output": "8"}, {"input": "1 1\r\n2\r\n4\r\n", "output": "-1\r\n"}, {"input": "22 56\r\n49 20 42 68 15 46 98 78 82 8 7 33 50 30 75 96 36 88 35 99 19 87\r\n15 18 81 24 35 89 25 32 23 3 48 24 52 69 18 32 23 61 48 98 50 38 5 17 70 20 38 32 49 54 68 11 51 81 46 22 19 59 29 38 45 83 18 13 91 17 84 62 25 60 97 32 23 13 83 58\r\n", "output": "-1\r\n"}, {"input": "1 1\r\n50\r\n100\r\n", "output": "-1\r\n"}, {"input": "1 1\r\n49\r\n100\r\n", "output": "98"}, {"input": "1 1\r\n100\r\n100\r\n", "output": "-1\r\n"}, {"input": "1 1\r\n99\r\n100\r\n", "output": "-1\r\n"}, {"input": "8 4\r\n1 2 49 99 99 95 78 98\r\n100 100 100 100\r\n", "output": "99"}, {"input": "68 85\r\n43 55 2 4 72 45 19 56 53 81 18 90 11 87 47 8 94 88 24 4 67 9 21 70 25 66 65 27 46 13 8 51 65 99 37 43 71 59 71 79 32 56 49 43 57 85 95 81 40 28 60 36 72 81 60 40 16 78 61 37 29 26 15 95 70 27 50 97\r\n6 6 48 72 54 31 1 50 29 64 93 9 29 93 66 63 25 90 52 1 66 13 70 30 24 87 32 90 84 72 44 13 25 45 31 16 92 60 87 40 62 7 20 63 86 78 73 88 5 36 74 100 64 34 9 5 62 29 58 48 81 46 84 56 27 1 60 14 54 88 31 93 62 7 9 69 27 48 10 5 33 10 53 66 2\r\n", "output": "-1\r\n"}, {"input": "5 100\r\n1 1 1 1 1\r\n77 53 38 29 97 33 64 17 78 100 27 12 42 44 20 24 44 68 58 57 65 90 8 24 4 6 74 68 61 43 25 69 8 62 36 85 67 48 69 30 35 41 42 12 87 66 50 92 53 76 38 67 85 7 80 78 53 76 94 8 37 50 4 100 4 71 10 48 34 47 83 42 25 81 64 72 25 51 53 75 43 98 53 77 94 38 81 15 89 91 72 76 7 36 27 41 88 18 19 75\r\n", "output": "2"}, {"input": "3 3\r\n2 3 4\r\n8 9 10\r\n", "output": "4"}, {"input": "2 1\r\n2 3\r\n15\r\n", "output": "4"}, {"input": "2 1\r\n2 4\r\n4\r\n", "output": "-1\r\n"}, {"input": "2 3\r\n4 5\r\n10 11 12\r\n", "output": "8"}, {"input": "3 1\r\n2 3 3\r\n5\r\n", "output": "4"}, {"input": "2 1\r\n9 10\r\n100\r\n", "output": "18"}, {"input": "3 3\r\n3 12 15\r\n7 8 9\r\n", "output": "-1\r\n"}, {"input": "2 2\r\n3 5\r\n7 8\r\n", "output": "6"}, {"input": "3 3\r\n4 5 6\r\n10 11 12\r\n", "output": "8"}, {"input": "3 5\r\n2 3 3\r\n6 6 6 6 2\r\n", "output": "-1\r\n"}, {"input": "3 6\r\n4 5 3\r\n8 9 7 10 7 11\r\n", "output": "6"}, {"input": "3 6\r\n4 5 2\r\n8 9 6 10 7 4\r\n", "output": "-1\r\n"}, {"input": "2 1\r\n4 6\r\n10\r\n", "output": "8"}, {"input": "1 2\r\n1\r\n3 1\r\n", "output": "-1\r\n"}, {"input": "2 1\r\n5 6\r\n20\r\n", "output": "10"}, {"input": "2 1\r\n1 5\r\n5\r\n", "output": "-1\r\n"}, {"input": "3 2\r\n10 20 30\r\n30 40\r\n", "output": "-1\r\n"}, {"input": "2 2\r\n5 6\r\n7 100\r\n", "output": "-1\r\n"}, {"input": "2 1\r\n2 5\r\n7\r\n", "output": "5"}, {"input": "1 1\r\n5\r\n20\r\n", "output": "10"}, {"input": "2 1\r\n10 11\r\n100\r\n", "output": "20"}, {"input": "1 1\r\n1\r\n10\r\n", "output": "2"}, {"input": "1 1\r\n10\r\n100\r\n", "output": "20"}]
false
stdio
null
true
822/B
822
B
Python 3
TESTS
3
31
0
149796987
n,m = list(map(int,input().split())) x = input() y = input() mini = len(y) mintab = [] for i in range(len(y)-len(x)+1): m = 0 tab = [] for j in range(len(x)): if x[j]!=y[i+j]: m+=1 tab.append(j+1) if m<mini: mini = m mintab = tab print(mini) print(" ".join([str(i) for i in mintab]))
113
77
4,300,800
176765941
def main(): n, m = map(int, input().split()) s = input() t = input() inds = [] i = 0 while i < m - n + 1: inds.append([]) j = 0 while j < n: inds[i].append(0) if s[j] == t[i+j]: inds[i][j] = 1 j += 1 i += 1 max_sum_ind = 0 i = 0 while i < m - n + 1: if sum(inds[i]) > sum(inds[max_sum_ind]): max_sum_ind = i i += 1 print(n - sum(inds[max_sum_ind])) i = 0 while i < n: if inds[max_sum_ind][i] == 0: print(i + 1, end=" ") i += 1 main()
Codeforces Round 422 (Div. 2)
CF
2,017
1
256
Crossword solving
Erelong Leha was bored by calculating of the greatest common divisor of two factorials. Therefore he decided to solve some crosswords. It's well known that it is a very interesting occupation though it can be very difficult from time to time. In the course of solving one of the crosswords, Leha had to solve a simple task. You are able to do it too, aren't you? Leha has two strings s and t. The hacker wants to change the string s at such way, that it can be found in t as a substring. All the changes should be the following: Leha chooses one position in the string s and replaces the symbol in this position with the question mark "?". The hacker is sure that the question mark in comparison can play the role of an arbitrary symbol. For example, if he gets string s="ab?b" as a result, it will appear in t="aabrbb" as a substring. Guaranteed that the length of the string s doesn't exceed the length of the string t. Help the hacker to replace in s as few symbols as possible so that the result of the replacements can be found in t as a substring. The symbol "?" should be considered equal to any other symbol.
The first line contains two integers n and m (1 ≤ n ≤ m ≤ 1000) — the length of the string s and the length of the string t correspondingly. The second line contains n lowercase English letters — string s. The third line contains m lowercase English letters — string t.
In the first line print single integer k — the minimal number of symbols that need to be replaced. In the second line print k distinct integers denoting the positions of symbols in the string s which need to be replaced. Print the positions in any order. If there are several solutions print any of them. The numbering of the positions begins from one.
null
null
[{"input": "3 5\nabc\nxaybz", "output": "2\n2 3"}, {"input": "4 10\nabcd\nebceabazcd", "output": "1\n2"}]
1,000
["brute force", "implementation", "strings"]
113
[{"input": "3 5\r\nabc\r\nxaybz\r\n", "output": "2\r\n2 3 \r\n"}, {"input": "4 10\r\nabcd\r\nebceabazcd\r\n", "output": "1\r\n2 \r\n"}, {"input": "1 1\r\na\r\na\r\n", "output": "0\r\n\r\n"}, {"input": "1 1\r\na\r\nz\r\n", "output": "1\r\n1 \r\n"}, {"input": "3 5\r\naaa\r\naaaaa\r\n", "output": "0\r\n\r\n"}, {"input": "3 5\r\naaa\r\naabaa\r\n", "output": "1\r\n3 \r\n"}, {"input": "5 5\r\ncoder\r\ncored\r\n", "output": "2\r\n3 5 \r\n"}, {"input": "1 1\r\nz\r\nz\r\n", "output": "0\r\n\r\n"}, {"input": "1 2\r\nf\r\nrt\r\n", "output": "1\r\n1 \r\n"}, {"input": "1 2\r\nf\r\nfg\r\n", "output": "0\r\n\r\n"}, {"input": "1 2\r\nf\r\ngf\r\n", "output": "0\r\n\r\n"}, {"input": "2 5\r\naa\r\naabaa\r\n", "output": "0\r\n\r\n"}, {"input": "2 5\r\naa\r\navaca\r\n", "output": "1\r\n2 \r\n"}, {"input": "3 5\r\naaa\r\nbbbbb\r\n", "output": "3\r\n1 2 3 \r\n"}, {"input": "3 5\r\naba\r\ncbcbc\r\n", "output": "2\r\n1 3 \r\n"}, {"input": "3 5\r\naba\r\nbbbbb\r\n", "output": "2\r\n1 3 \r\n"}, {"input": "3 5\r\naaa\r\naabvd\r\n", "output": "1\r\n3 \r\n"}, {"input": "3 5\r\nvvv\r\nbqavv\r\n", "output": "1\r\n1 \r\n"}, {"input": "10 100\r\nmpmmpmmmpm\r\nmppppppmppmmpmpppmpppmmpppmpppppmpppmmmppmpmpmmmpmmpmppmmpppppmpmppppmmppmpmppmmmmpmmppmmmpmpmmmpppp\r\n", "output": "2\r\n5 6 \r\n"}, {"input": "26 26\r\nabcdefghijklmnopqrstuvwxyz\r\nffffffffffffffffffffffffff\r\n", "output": "25\r\n1 2 3 4 5 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 \r\n"}, {"input": "3 5\r\nabc\r\nxyzab\r\n", "output": "3\r\n1 2 3 \r\n"}, {"input": "4 4\r\nabcd\r\nxabc\r\n", "output": "4\r\n1 2 3 4 \r\n"}, {"input": "3 4\r\nabc\r\nabcd\r\n", "output": "0\r\n\r\n"}, {"input": "3 3\r\nabc\r\nxxa\r\n", "output": "3\r\n1 2 3 \r\n"}, {"input": "3 5\r\naab\r\nzfhka\r\n", "output": "3\r\n1 2 3 \r\n"}, {"input": "3 3\r\nabc\r\nxya\r\n", "output": "3\r\n1 2 3 \r\n"}, {"input": "3 3\r\nabc\r\ncab\r\n", "output": "3\r\n1 2 3 \r\n"}, {"input": "5 5\r\nabcde\r\nxxabc\r\n", "output": "5\r\n1 2 3 4 5 \r\n"}, {"input": "3 10\r\nass\r\nabcdefssss\r\n", "output": "1\r\n1 \r\n"}, {"input": "4 4\r\nabcd\r\neeab\r\n", "output": "4\r\n1 2 3 4 \r\n"}, {"input": "3 4\r\nabh\r\nbhaa\r\n", "output": "3\r\n1 2 3 \r\n"}, {"input": "2 3\r\nzb\r\naaz\r\n", "output": "2\r\n1 2 \r\n"}, {"input": "2 3\r\nab\r\ndda\r\n", "output": "2\r\n1 2 \r\n"}, {"input": "3 3\r\ncba\r\nbac\r\n", "output": "3\r\n1 2 3 \r\n"}, {"input": "3 4\r\nabc\r\nxxxa\r\n", "output": "3\r\n1 2 3 \r\n"}, {"input": "2 3\r\nab\r\nbbb\r\n", "output": "1\r\n1 \r\n"}, {"input": "10 15\r\nsdkjeaafww\r\nefjklffnkddkfey\r\n", "output": "9\r\n1 2 4 5 6 7 8 9 10 \r\n"}, {"input": "3 3\r\nabc\r\nzbc\r\n", "output": "1\r\n1 \r\n"}, {"input": "3 7\r\nabc\r\neeeeeab\r\n", "output": "3\r\n1 2 3 \r\n"}, {"input": "2 6\r\nab\r\nxyxbab\r\n", "output": "0\r\n\r\n"}, {"input": "4 7\r\nabcd\r\nzzzzabc\r\n", "output": "4\r\n1 2 3 4 \r\n"}, {"input": "3 5\r\nabc\r\nabzzz\r\n", "output": "1\r\n3 \r\n"}, {"input": "3 3\r\naaz\r\nzaa\r\n", "output": "2\r\n1 3 \r\n"}, {"input": "3 6\r\nabc\r\nxaybzd\r\n", "output": "2\r\n2 3 \r\n"}, {"input": "4 5\r\naaaa\r\naaaap\r\n", "output": "0\r\n\r\n"}]
false
stdio
null
true
350/A
350
A
PyPy 3-64
TESTS
24
124
0
198430115
def getV(): n , m = [int(x) for x in input().split()] right = [int(x) for x in input().split()] wrong = [int(x) for x in input().split()] rang = [max(right) , min(wrong)] if rang[0] > rang[1]: return -1 theshold = min(right) * 2 if rang[0] < theshold < rang[1]: return theshold if theshold <= rang[0]: return rang[0] else: return -1 print(getV())
45
92
0
4624208
n, m = [int(i) for i in input().split()] a = [int(i) for i in input().split()] b = [int(i) for i in input().split()] flag = False for i in range(100): if (i < min(b)) and (i >= max(a)) and (i >= min(a) * 2): if not(flag): print(i) flag = True if not(flag): print(-1)
Codeforces Round 203 (Div. 2)
CF
2,013
2
256
TL
Valera wanted to prepare a Codesecrof round. He's already got one problem and he wants to set a time limit (TL) on it. Valera has written n correct solutions. For each correct solution, he knows its running time (in seconds). Valera has also wrote m wrong solutions and for each wrong solution he knows its running time (in seconds). Let's suppose that Valera will set v seconds TL in the problem. Then we can say that a solution passes the system testing if its running time is at most v seconds. We can also say that a solution passes the system testing with some "extra" time if for its running time, a seconds, an inequality 2a ≤ v holds. As a result, Valera decided to set v seconds TL, that the following conditions are met: 1. v is a positive integer; 2. all correct solutions pass the system testing; 3. at least one correct solution passes the system testing with some "extra" time; 4. all wrong solutions do not pass the system testing; 5. value v is minimum among all TLs, for which points 1, 2, 3, 4 hold. Help Valera and find the most suitable TL or else state that such TL doesn't exist.
The first line contains two integers n, m (1 ≤ n, m ≤ 100). The second line contains n space-separated positive integers a1, a2, ..., an (1 ≤ ai ≤ 100) — the running time of each of the n correct solutions in seconds. The third line contains m space-separated positive integers b1, b2, ..., bm (1 ≤ bi ≤ 100) — the running time of each of m wrong solutions in seconds.
If there is a valid TL value, print it. Otherwise, print -1.
null
null
[{"input": "3 6\n4 5 2\n8 9 6 10 7 11", "output": "5"}, {"input": "3 1\n3 4 5\n6", "output": "-1"}]
1,200
["brute force", "greedy", "implementation"]
45
[{"input": "3 6\r\n4 5 2\r\n8 9 6 10 7 11\r\n", "output": "5"}, {"input": "3 1\r\n3 4 5\r\n6\r\n", "output": "-1\r\n"}, {"input": "2 5\r\n45 99\r\n49 41 77 83 45\r\n", "output": "-1\r\n"}, {"input": "50 50\r\n18 13 5 34 10 36 36 12 15 11 16 17 14 36 23 45 32 24 31 18 24 32 7 1 31 3 49 8 16 23 3 39 47 43 42 38 40 22 41 1 49 47 9 8 19 15 29 30 16 18\r\n91 58 86 51 94 94 73 84 98 69 74 56 52 80 88 61 53 99 88 50 55 95 65 84 87 79 51 52 69 60 74 73 93 61 73 59 64 56 95 78 86 72 79 70 93 78 54 61 71 50\r\n", "output": "49"}, {"input": "55 44\r\n93 17 74 15 34 16 41 80 26 54 94 94 86 93 20 44 63 72 39 43 67 4 37 49 76 94 5 51 64 74 11 47 77 97 57 30 42 72 71 26 8 14 67 64 49 57 30 23 40 4 76 78 87 78 79\r\n38 55 17 65 26 7 36 65 48 28 49 93 18 98 31 90 26 57 1 26 88 56 48 56 23 13 8 67 80 2 51 3 21 33 20 54 2 45 21 36 3 98 62 2\r\n", "output": "-1\r\n"}, {"input": "32 100\r\n30 8 4 35 18 41 18 12 33 39 39 18 39 19 33 46 45 33 34 27 14 39 40 21 38 9 42 35 27 10 14 14\r\n65 49 89 64 47 78 59 52 73 51 84 82 88 63 91 99 67 87 53 99 75 47 85 82 58 47 80 50 65 91 83 90 77 52 100 88 97 74 98 99 50 93 65 61 65 65 65 96 61 51 84 67 79 90 92 83 100 100 100 95 80 54 77 51 98 64 74 62 60 96 73 74 94 55 89 60 92 65 74 79 66 81 53 47 71 51 54 85 74 97 68 72 88 94 100 85 65 63 65 90\r\n", "output": "46"}, {"input": "1 50\r\n7\r\n65 52 99 78 71 19 96 72 80 15 50 94 20 35 79 95 44 41 45 53 77 50 74 66 59 96 26 84 27 48 56 84 36 78 89 81 67 34 79 74 99 47 93 92 90 96 72 28 78 66\r\n", "output": "14"}, {"input": "1 1\r\n4\r\n9\r\n", "output": "8"}, {"input": "1 1\r\n2\r\n4\r\n", "output": "-1\r\n"}, {"input": "22 56\r\n49 20 42 68 15 46 98 78 82 8 7 33 50 30 75 96 36 88 35 99 19 87\r\n15 18 81 24 35 89 25 32 23 3 48 24 52 69 18 32 23 61 48 98 50 38 5 17 70 20 38 32 49 54 68 11 51 81 46 22 19 59 29 38 45 83 18 13 91 17 84 62 25 60 97 32 23 13 83 58\r\n", "output": "-1\r\n"}, {"input": "1 1\r\n50\r\n100\r\n", "output": "-1\r\n"}, {"input": "1 1\r\n49\r\n100\r\n", "output": "98"}, {"input": "1 1\r\n100\r\n100\r\n", "output": "-1\r\n"}, {"input": "1 1\r\n99\r\n100\r\n", "output": "-1\r\n"}, {"input": "8 4\r\n1 2 49 99 99 95 78 98\r\n100 100 100 100\r\n", "output": "99"}, {"input": "68 85\r\n43 55 2 4 72 45 19 56 53 81 18 90 11 87 47 8 94 88 24 4 67 9 21 70 25 66 65 27 46 13 8 51 65 99 37 43 71 59 71 79 32 56 49 43 57 85 95 81 40 28 60 36 72 81 60 40 16 78 61 37 29 26 15 95 70 27 50 97\r\n6 6 48 72 54 31 1 50 29 64 93 9 29 93 66 63 25 90 52 1 66 13 70 30 24 87 32 90 84 72 44 13 25 45 31 16 92 60 87 40 62 7 20 63 86 78 73 88 5 36 74 100 64 34 9 5 62 29 58 48 81 46 84 56 27 1 60 14 54 88 31 93 62 7 9 69 27 48 10 5 33 10 53 66 2\r\n", "output": "-1\r\n"}, {"input": "5 100\r\n1 1 1 1 1\r\n77 53 38 29 97 33 64 17 78 100 27 12 42 44 20 24 44 68 58 57 65 90 8 24 4 6 74 68 61 43 25 69 8 62 36 85 67 48 69 30 35 41 42 12 87 66 50 92 53 76 38 67 85 7 80 78 53 76 94 8 37 50 4 100 4 71 10 48 34 47 83 42 25 81 64 72 25 51 53 75 43 98 53 77 94 38 81 15 89 91 72 76 7 36 27 41 88 18 19 75\r\n", "output": "2"}, {"input": "3 3\r\n2 3 4\r\n8 9 10\r\n", "output": "4"}, {"input": "2 1\r\n2 3\r\n15\r\n", "output": "4"}, {"input": "2 1\r\n2 4\r\n4\r\n", "output": "-1\r\n"}, {"input": "2 3\r\n4 5\r\n10 11 12\r\n", "output": "8"}, {"input": "3 1\r\n2 3 3\r\n5\r\n", "output": "4"}, {"input": "2 1\r\n9 10\r\n100\r\n", "output": "18"}, {"input": "3 3\r\n3 12 15\r\n7 8 9\r\n", "output": "-1\r\n"}, {"input": "2 2\r\n3 5\r\n7 8\r\n", "output": "6"}, {"input": "3 3\r\n4 5 6\r\n10 11 12\r\n", "output": "8"}, {"input": "3 5\r\n2 3 3\r\n6 6 6 6 2\r\n", "output": "-1\r\n"}, {"input": "3 6\r\n4 5 3\r\n8 9 7 10 7 11\r\n", "output": "6"}, {"input": "3 6\r\n4 5 2\r\n8 9 6 10 7 4\r\n", "output": "-1\r\n"}, {"input": "2 1\r\n4 6\r\n10\r\n", "output": "8"}, {"input": "1 2\r\n1\r\n3 1\r\n", "output": "-1\r\n"}, {"input": "2 1\r\n5 6\r\n20\r\n", "output": "10"}, {"input": "2 1\r\n1 5\r\n5\r\n", "output": "-1\r\n"}, {"input": "3 2\r\n10 20 30\r\n30 40\r\n", "output": "-1\r\n"}, {"input": "2 2\r\n5 6\r\n7 100\r\n", "output": "-1\r\n"}, {"input": "2 1\r\n2 5\r\n7\r\n", "output": "5"}, {"input": "1 1\r\n5\r\n20\r\n", "output": "10"}, {"input": "2 1\r\n10 11\r\n100\r\n", "output": "20"}, {"input": "1 1\r\n1\r\n10\r\n", "output": "2"}, {"input": "1 1\r\n10\r\n100\r\n", "output": "20"}]
false
stdio
null
true
350/A
350
A
PyPy 3
TESTS
28
280
0
80622434
n, m = list(map(int, input().split())) a = list(map(int, input().split())) b = list(map(int, input().split())) print(max(2*min(a), max(a)) if 2*min(a)<min(b) else -1)
45
92
0
4625145
# your code goes here str = input() L = str.split() n = int(L[0]) m = int(L[1]) str = input() L = str.split() max1 = int(L[0]) min1 = int(L[0]) i = 0 while i<n: if max1 < int (L[i]): max1 = int (L[i]) if min1 > int (L[i]): min1 = int (L[i]) i+=1 str = input() L = str.split() min = int(L[0]) i = 0 while i<m: if min > int (L[i]): min = int (L[i]) i+=1 i=0 flag = False if max1 < min : i = max1 while i<min: if 2*min1<=i: print (i) flag = True i=min+1 i+=1 if flag == False: print ("-1")
Codeforces Round 203 (Div. 2)
CF
2,013
2
256
TL
Valera wanted to prepare a Codesecrof round. He's already got one problem and he wants to set a time limit (TL) on it. Valera has written n correct solutions. For each correct solution, he knows its running time (in seconds). Valera has also wrote m wrong solutions and for each wrong solution he knows its running time (in seconds). Let's suppose that Valera will set v seconds TL in the problem. Then we can say that a solution passes the system testing if its running time is at most v seconds. We can also say that a solution passes the system testing with some "extra" time if for its running time, a seconds, an inequality 2a ≤ v holds. As a result, Valera decided to set v seconds TL, that the following conditions are met: 1. v is a positive integer; 2. all correct solutions pass the system testing; 3. at least one correct solution passes the system testing with some "extra" time; 4. all wrong solutions do not pass the system testing; 5. value v is minimum among all TLs, for which points 1, 2, 3, 4 hold. Help Valera and find the most suitable TL or else state that such TL doesn't exist.
The first line contains two integers n, m (1 ≤ n, m ≤ 100). The second line contains n space-separated positive integers a1, a2, ..., an (1 ≤ ai ≤ 100) — the running time of each of the n correct solutions in seconds. The third line contains m space-separated positive integers b1, b2, ..., bm (1 ≤ bi ≤ 100) — the running time of each of m wrong solutions in seconds.
If there is a valid TL value, print it. Otherwise, print -1.
null
null
[{"input": "3 6\n4 5 2\n8 9 6 10 7 11", "output": "5"}, {"input": "3 1\n3 4 5\n6", "output": "-1"}]
1,200
["brute force", "greedy", "implementation"]
45
[{"input": "3 6\r\n4 5 2\r\n8 9 6 10 7 11\r\n", "output": "5"}, {"input": "3 1\r\n3 4 5\r\n6\r\n", "output": "-1\r\n"}, {"input": "2 5\r\n45 99\r\n49 41 77 83 45\r\n", "output": "-1\r\n"}, {"input": "50 50\r\n18 13 5 34 10 36 36 12 15 11 16 17 14 36 23 45 32 24 31 18 24 32 7 1 31 3 49 8 16 23 3 39 47 43 42 38 40 22 41 1 49 47 9 8 19 15 29 30 16 18\r\n91 58 86 51 94 94 73 84 98 69 74 56 52 80 88 61 53 99 88 50 55 95 65 84 87 79 51 52 69 60 74 73 93 61 73 59 64 56 95 78 86 72 79 70 93 78 54 61 71 50\r\n", "output": "49"}, {"input": "55 44\r\n93 17 74 15 34 16 41 80 26 54 94 94 86 93 20 44 63 72 39 43 67 4 37 49 76 94 5 51 64 74 11 47 77 97 57 30 42 72 71 26 8 14 67 64 49 57 30 23 40 4 76 78 87 78 79\r\n38 55 17 65 26 7 36 65 48 28 49 93 18 98 31 90 26 57 1 26 88 56 48 56 23 13 8 67 80 2 51 3 21 33 20 54 2 45 21 36 3 98 62 2\r\n", "output": "-1\r\n"}, {"input": "32 100\r\n30 8 4 35 18 41 18 12 33 39 39 18 39 19 33 46 45 33 34 27 14 39 40 21 38 9 42 35 27 10 14 14\r\n65 49 89 64 47 78 59 52 73 51 84 82 88 63 91 99 67 87 53 99 75 47 85 82 58 47 80 50 65 91 83 90 77 52 100 88 97 74 98 99 50 93 65 61 65 65 65 96 61 51 84 67 79 90 92 83 100 100 100 95 80 54 77 51 98 64 74 62 60 96 73 74 94 55 89 60 92 65 74 79 66 81 53 47 71 51 54 85 74 97 68 72 88 94 100 85 65 63 65 90\r\n", "output": "46"}, {"input": "1 50\r\n7\r\n65 52 99 78 71 19 96 72 80 15 50 94 20 35 79 95 44 41 45 53 77 50 74 66 59 96 26 84 27 48 56 84 36 78 89 81 67 34 79 74 99 47 93 92 90 96 72 28 78 66\r\n", "output": "14"}, {"input": "1 1\r\n4\r\n9\r\n", "output": "8"}, {"input": "1 1\r\n2\r\n4\r\n", "output": "-1\r\n"}, {"input": "22 56\r\n49 20 42 68 15 46 98 78 82 8 7 33 50 30 75 96 36 88 35 99 19 87\r\n15 18 81 24 35 89 25 32 23 3 48 24 52 69 18 32 23 61 48 98 50 38 5 17 70 20 38 32 49 54 68 11 51 81 46 22 19 59 29 38 45 83 18 13 91 17 84 62 25 60 97 32 23 13 83 58\r\n", "output": "-1\r\n"}, {"input": "1 1\r\n50\r\n100\r\n", "output": "-1\r\n"}, {"input": "1 1\r\n49\r\n100\r\n", "output": "98"}, {"input": "1 1\r\n100\r\n100\r\n", "output": "-1\r\n"}, {"input": "1 1\r\n99\r\n100\r\n", "output": "-1\r\n"}, {"input": "8 4\r\n1 2 49 99 99 95 78 98\r\n100 100 100 100\r\n", "output": "99"}, {"input": "68 85\r\n43 55 2 4 72 45 19 56 53 81 18 90 11 87 47 8 94 88 24 4 67 9 21 70 25 66 65 27 46 13 8 51 65 99 37 43 71 59 71 79 32 56 49 43 57 85 95 81 40 28 60 36 72 81 60 40 16 78 61 37 29 26 15 95 70 27 50 97\r\n6 6 48 72 54 31 1 50 29 64 93 9 29 93 66 63 25 90 52 1 66 13 70 30 24 87 32 90 84 72 44 13 25 45 31 16 92 60 87 40 62 7 20 63 86 78 73 88 5 36 74 100 64 34 9 5 62 29 58 48 81 46 84 56 27 1 60 14 54 88 31 93 62 7 9 69 27 48 10 5 33 10 53 66 2\r\n", "output": "-1\r\n"}, {"input": "5 100\r\n1 1 1 1 1\r\n77 53 38 29 97 33 64 17 78 100 27 12 42 44 20 24 44 68 58 57 65 90 8 24 4 6 74 68 61 43 25 69 8 62 36 85 67 48 69 30 35 41 42 12 87 66 50 92 53 76 38 67 85 7 80 78 53 76 94 8 37 50 4 100 4 71 10 48 34 47 83 42 25 81 64 72 25 51 53 75 43 98 53 77 94 38 81 15 89 91 72 76 7 36 27 41 88 18 19 75\r\n", "output": "2"}, {"input": "3 3\r\n2 3 4\r\n8 9 10\r\n", "output": "4"}, {"input": "2 1\r\n2 3\r\n15\r\n", "output": "4"}, {"input": "2 1\r\n2 4\r\n4\r\n", "output": "-1\r\n"}, {"input": "2 3\r\n4 5\r\n10 11 12\r\n", "output": "8"}, {"input": "3 1\r\n2 3 3\r\n5\r\n", "output": "4"}, {"input": "2 1\r\n9 10\r\n100\r\n", "output": "18"}, {"input": "3 3\r\n3 12 15\r\n7 8 9\r\n", "output": "-1\r\n"}, {"input": "2 2\r\n3 5\r\n7 8\r\n", "output": "6"}, {"input": "3 3\r\n4 5 6\r\n10 11 12\r\n", "output": "8"}, {"input": "3 5\r\n2 3 3\r\n6 6 6 6 2\r\n", "output": "-1\r\n"}, {"input": "3 6\r\n4 5 3\r\n8 9 7 10 7 11\r\n", "output": "6"}, {"input": "3 6\r\n4 5 2\r\n8 9 6 10 7 4\r\n", "output": "-1\r\n"}, {"input": "2 1\r\n4 6\r\n10\r\n", "output": "8"}, {"input": "1 2\r\n1\r\n3 1\r\n", "output": "-1\r\n"}, {"input": "2 1\r\n5 6\r\n20\r\n", "output": "10"}, {"input": "2 1\r\n1 5\r\n5\r\n", "output": "-1\r\n"}, {"input": "3 2\r\n10 20 30\r\n30 40\r\n", "output": "-1\r\n"}, {"input": "2 2\r\n5 6\r\n7 100\r\n", "output": "-1\r\n"}, {"input": "2 1\r\n2 5\r\n7\r\n", "output": "5"}, {"input": "1 1\r\n5\r\n20\r\n", "output": "10"}, {"input": "2 1\r\n10 11\r\n100\r\n", "output": "20"}, {"input": "1 1\r\n1\r\n10\r\n", "output": "2"}, {"input": "1 1\r\n10\r\n100\r\n", "output": "20"}]
false
stdio
null
true
350/A
350
A
Python 3
TESTS
28
154
6,963,200
123692044
def main(): n, m = [int(x) for x in input().split()] correct = [int(x) for x in input().split()] wrong = [int(x) for x in input().split()] correct.sort() v = correct[-1] if correct[0]*2 > v: v = -1 if v != -1: for i in wrong: if i <= v: v = -1 if v != -1: print(v) else: for j in correct: v = j*2 if correct[0]*2 > v: v = -1 continue for i in wrong: if i <= v: v = -1 break if v != 1: break print(v) if __name__ == "__main__": main()
45
92
0
4633901
n,m=map(int,input().split()) N=list(map(int,input().split())) M=list(map(int,input().split())) N.sort() M.sort() a=N[-1] ans=-1 for i in range(a,M[0]): if(i>=2*N[0]): ans=i break print(ans)
Codeforces Round 203 (Div. 2)
CF
2,013
2
256
TL
Valera wanted to prepare a Codesecrof round. He's already got one problem and he wants to set a time limit (TL) on it. Valera has written n correct solutions. For each correct solution, he knows its running time (in seconds). Valera has also wrote m wrong solutions and for each wrong solution he knows its running time (in seconds). Let's suppose that Valera will set v seconds TL in the problem. Then we can say that a solution passes the system testing if its running time is at most v seconds. We can also say that a solution passes the system testing with some "extra" time if for its running time, a seconds, an inequality 2a ≤ v holds. As a result, Valera decided to set v seconds TL, that the following conditions are met: 1. v is a positive integer; 2. all correct solutions pass the system testing; 3. at least one correct solution passes the system testing with some "extra" time; 4. all wrong solutions do not pass the system testing; 5. value v is minimum among all TLs, for which points 1, 2, 3, 4 hold. Help Valera and find the most suitable TL or else state that such TL doesn't exist.
The first line contains two integers n, m (1 ≤ n, m ≤ 100). The second line contains n space-separated positive integers a1, a2, ..., an (1 ≤ ai ≤ 100) — the running time of each of the n correct solutions in seconds. The third line contains m space-separated positive integers b1, b2, ..., bm (1 ≤ bi ≤ 100) — the running time of each of m wrong solutions in seconds.
If there is a valid TL value, print it. Otherwise, print -1.
null
null
[{"input": "3 6\n4 5 2\n8 9 6 10 7 11", "output": "5"}, {"input": "3 1\n3 4 5\n6", "output": "-1"}]
1,200
["brute force", "greedy", "implementation"]
45
[{"input": "3 6\r\n4 5 2\r\n8 9 6 10 7 11\r\n", "output": "5"}, {"input": "3 1\r\n3 4 5\r\n6\r\n", "output": "-1\r\n"}, {"input": "2 5\r\n45 99\r\n49 41 77 83 45\r\n", "output": "-1\r\n"}, {"input": "50 50\r\n18 13 5 34 10 36 36 12 15 11 16 17 14 36 23 45 32 24 31 18 24 32 7 1 31 3 49 8 16 23 3 39 47 43 42 38 40 22 41 1 49 47 9 8 19 15 29 30 16 18\r\n91 58 86 51 94 94 73 84 98 69 74 56 52 80 88 61 53 99 88 50 55 95 65 84 87 79 51 52 69 60 74 73 93 61 73 59 64 56 95 78 86 72 79 70 93 78 54 61 71 50\r\n", "output": "49"}, {"input": "55 44\r\n93 17 74 15 34 16 41 80 26 54 94 94 86 93 20 44 63 72 39 43 67 4 37 49 76 94 5 51 64 74 11 47 77 97 57 30 42 72 71 26 8 14 67 64 49 57 30 23 40 4 76 78 87 78 79\r\n38 55 17 65 26 7 36 65 48 28 49 93 18 98 31 90 26 57 1 26 88 56 48 56 23 13 8 67 80 2 51 3 21 33 20 54 2 45 21 36 3 98 62 2\r\n", "output": "-1\r\n"}, {"input": "32 100\r\n30 8 4 35 18 41 18 12 33 39 39 18 39 19 33 46 45 33 34 27 14 39 40 21 38 9 42 35 27 10 14 14\r\n65 49 89 64 47 78 59 52 73 51 84 82 88 63 91 99 67 87 53 99 75 47 85 82 58 47 80 50 65 91 83 90 77 52 100 88 97 74 98 99 50 93 65 61 65 65 65 96 61 51 84 67 79 90 92 83 100 100 100 95 80 54 77 51 98 64 74 62 60 96 73 74 94 55 89 60 92 65 74 79 66 81 53 47 71 51 54 85 74 97 68 72 88 94 100 85 65 63 65 90\r\n", "output": "46"}, {"input": "1 50\r\n7\r\n65 52 99 78 71 19 96 72 80 15 50 94 20 35 79 95 44 41 45 53 77 50 74 66 59 96 26 84 27 48 56 84 36 78 89 81 67 34 79 74 99 47 93 92 90 96 72 28 78 66\r\n", "output": "14"}, {"input": "1 1\r\n4\r\n9\r\n", "output": "8"}, {"input": "1 1\r\n2\r\n4\r\n", "output": "-1\r\n"}, {"input": "22 56\r\n49 20 42 68 15 46 98 78 82 8 7 33 50 30 75 96 36 88 35 99 19 87\r\n15 18 81 24 35 89 25 32 23 3 48 24 52 69 18 32 23 61 48 98 50 38 5 17 70 20 38 32 49 54 68 11 51 81 46 22 19 59 29 38 45 83 18 13 91 17 84 62 25 60 97 32 23 13 83 58\r\n", "output": "-1\r\n"}, {"input": "1 1\r\n50\r\n100\r\n", "output": "-1\r\n"}, {"input": "1 1\r\n49\r\n100\r\n", "output": "98"}, {"input": "1 1\r\n100\r\n100\r\n", "output": "-1\r\n"}, {"input": "1 1\r\n99\r\n100\r\n", "output": "-1\r\n"}, {"input": "8 4\r\n1 2 49 99 99 95 78 98\r\n100 100 100 100\r\n", "output": "99"}, {"input": "68 85\r\n43 55 2 4 72 45 19 56 53 81 18 90 11 87 47 8 94 88 24 4 67 9 21 70 25 66 65 27 46 13 8 51 65 99 37 43 71 59 71 79 32 56 49 43 57 85 95 81 40 28 60 36 72 81 60 40 16 78 61 37 29 26 15 95 70 27 50 97\r\n6 6 48 72 54 31 1 50 29 64 93 9 29 93 66 63 25 90 52 1 66 13 70 30 24 87 32 90 84 72 44 13 25 45 31 16 92 60 87 40 62 7 20 63 86 78 73 88 5 36 74 100 64 34 9 5 62 29 58 48 81 46 84 56 27 1 60 14 54 88 31 93 62 7 9 69 27 48 10 5 33 10 53 66 2\r\n", "output": "-1\r\n"}, {"input": "5 100\r\n1 1 1 1 1\r\n77 53 38 29 97 33 64 17 78 100 27 12 42 44 20 24 44 68 58 57 65 90 8 24 4 6 74 68 61 43 25 69 8 62 36 85 67 48 69 30 35 41 42 12 87 66 50 92 53 76 38 67 85 7 80 78 53 76 94 8 37 50 4 100 4 71 10 48 34 47 83 42 25 81 64 72 25 51 53 75 43 98 53 77 94 38 81 15 89 91 72 76 7 36 27 41 88 18 19 75\r\n", "output": "2"}, {"input": "3 3\r\n2 3 4\r\n8 9 10\r\n", "output": "4"}, {"input": "2 1\r\n2 3\r\n15\r\n", "output": "4"}, {"input": "2 1\r\n2 4\r\n4\r\n", "output": "-1\r\n"}, {"input": "2 3\r\n4 5\r\n10 11 12\r\n", "output": "8"}, {"input": "3 1\r\n2 3 3\r\n5\r\n", "output": "4"}, {"input": "2 1\r\n9 10\r\n100\r\n", "output": "18"}, {"input": "3 3\r\n3 12 15\r\n7 8 9\r\n", "output": "-1\r\n"}, {"input": "2 2\r\n3 5\r\n7 8\r\n", "output": "6"}, {"input": "3 3\r\n4 5 6\r\n10 11 12\r\n", "output": "8"}, {"input": "3 5\r\n2 3 3\r\n6 6 6 6 2\r\n", "output": "-1\r\n"}, {"input": "3 6\r\n4 5 3\r\n8 9 7 10 7 11\r\n", "output": "6"}, {"input": "3 6\r\n4 5 2\r\n8 9 6 10 7 4\r\n", "output": "-1\r\n"}, {"input": "2 1\r\n4 6\r\n10\r\n", "output": "8"}, {"input": "1 2\r\n1\r\n3 1\r\n", "output": "-1\r\n"}, {"input": "2 1\r\n5 6\r\n20\r\n", "output": "10"}, {"input": "2 1\r\n1 5\r\n5\r\n", "output": "-1\r\n"}, {"input": "3 2\r\n10 20 30\r\n30 40\r\n", "output": "-1\r\n"}, {"input": "2 2\r\n5 6\r\n7 100\r\n", "output": "-1\r\n"}, {"input": "2 1\r\n2 5\r\n7\r\n", "output": "5"}, {"input": "1 1\r\n5\r\n20\r\n", "output": "10"}, {"input": "2 1\r\n10 11\r\n100\r\n", "output": "20"}, {"input": "1 1\r\n1\r\n10\r\n", "output": "2"}, {"input": "1 1\r\n10\r\n100\r\n", "output": "20"}]
false
stdio
null
true
350/A
350
A
Python 3
TESTS
28
186
0
39526388
size = input() a = [int(i) for i in input().split()] b = [int(j) for j in input().split()] v = max(a) if 2*min(a) < max(a) and max(a) < min(b): print(v) elif 2*min(a) < min(b): print(2*min(a)) else: print(-1)
45
92
0
4636686
s = input() ar = s.split() n = int(ar[0]) m = int(ar[1]) a = [] b = [] s = input() for i in s.split(): a += [int(i)] s = input() for i in s.split(): b += [int(i)] if max(min(a) * 2, max(a)) < min(b): print(max(min(a) * 2, max(a))) else: print(-1)
Codeforces Round 203 (Div. 2)
CF
2,013
2
256
TL
Valera wanted to prepare a Codesecrof round. He's already got one problem and he wants to set a time limit (TL) on it. Valera has written n correct solutions. For each correct solution, he knows its running time (in seconds). Valera has also wrote m wrong solutions and for each wrong solution he knows its running time (in seconds). Let's suppose that Valera will set v seconds TL in the problem. Then we can say that a solution passes the system testing if its running time is at most v seconds. We can also say that a solution passes the system testing with some "extra" time if for its running time, a seconds, an inequality 2a ≤ v holds. As a result, Valera decided to set v seconds TL, that the following conditions are met: 1. v is a positive integer; 2. all correct solutions pass the system testing; 3. at least one correct solution passes the system testing with some "extra" time; 4. all wrong solutions do not pass the system testing; 5. value v is minimum among all TLs, for which points 1, 2, 3, 4 hold. Help Valera and find the most suitable TL or else state that such TL doesn't exist.
The first line contains two integers n, m (1 ≤ n, m ≤ 100). The second line contains n space-separated positive integers a1, a2, ..., an (1 ≤ ai ≤ 100) — the running time of each of the n correct solutions in seconds. The third line contains m space-separated positive integers b1, b2, ..., bm (1 ≤ bi ≤ 100) — the running time of each of m wrong solutions in seconds.
If there is a valid TL value, print it. Otherwise, print -1.
null
null
[{"input": "3 6\n4 5 2\n8 9 6 10 7 11", "output": "5"}, {"input": "3 1\n3 4 5\n6", "output": "-1"}]
1,200
["brute force", "greedy", "implementation"]
45
[{"input": "3 6\r\n4 5 2\r\n8 9 6 10 7 11\r\n", "output": "5"}, {"input": "3 1\r\n3 4 5\r\n6\r\n", "output": "-1\r\n"}, {"input": "2 5\r\n45 99\r\n49 41 77 83 45\r\n", "output": "-1\r\n"}, {"input": "50 50\r\n18 13 5 34 10 36 36 12 15 11 16 17 14 36 23 45 32 24 31 18 24 32 7 1 31 3 49 8 16 23 3 39 47 43 42 38 40 22 41 1 49 47 9 8 19 15 29 30 16 18\r\n91 58 86 51 94 94 73 84 98 69 74 56 52 80 88 61 53 99 88 50 55 95 65 84 87 79 51 52 69 60 74 73 93 61 73 59 64 56 95 78 86 72 79 70 93 78 54 61 71 50\r\n", "output": "49"}, {"input": "55 44\r\n93 17 74 15 34 16 41 80 26 54 94 94 86 93 20 44 63 72 39 43 67 4 37 49 76 94 5 51 64 74 11 47 77 97 57 30 42 72 71 26 8 14 67 64 49 57 30 23 40 4 76 78 87 78 79\r\n38 55 17 65 26 7 36 65 48 28 49 93 18 98 31 90 26 57 1 26 88 56 48 56 23 13 8 67 80 2 51 3 21 33 20 54 2 45 21 36 3 98 62 2\r\n", "output": "-1\r\n"}, {"input": "32 100\r\n30 8 4 35 18 41 18 12 33 39 39 18 39 19 33 46 45 33 34 27 14 39 40 21 38 9 42 35 27 10 14 14\r\n65 49 89 64 47 78 59 52 73 51 84 82 88 63 91 99 67 87 53 99 75 47 85 82 58 47 80 50 65 91 83 90 77 52 100 88 97 74 98 99 50 93 65 61 65 65 65 96 61 51 84 67 79 90 92 83 100 100 100 95 80 54 77 51 98 64 74 62 60 96 73 74 94 55 89 60 92 65 74 79 66 81 53 47 71 51 54 85 74 97 68 72 88 94 100 85 65 63 65 90\r\n", "output": "46"}, {"input": "1 50\r\n7\r\n65 52 99 78 71 19 96 72 80 15 50 94 20 35 79 95 44 41 45 53 77 50 74 66 59 96 26 84 27 48 56 84 36 78 89 81 67 34 79 74 99 47 93 92 90 96 72 28 78 66\r\n", "output": "14"}, {"input": "1 1\r\n4\r\n9\r\n", "output": "8"}, {"input": "1 1\r\n2\r\n4\r\n", "output": "-1\r\n"}, {"input": "22 56\r\n49 20 42 68 15 46 98 78 82 8 7 33 50 30 75 96 36 88 35 99 19 87\r\n15 18 81 24 35 89 25 32 23 3 48 24 52 69 18 32 23 61 48 98 50 38 5 17 70 20 38 32 49 54 68 11 51 81 46 22 19 59 29 38 45 83 18 13 91 17 84 62 25 60 97 32 23 13 83 58\r\n", "output": "-1\r\n"}, {"input": "1 1\r\n50\r\n100\r\n", "output": "-1\r\n"}, {"input": "1 1\r\n49\r\n100\r\n", "output": "98"}, {"input": "1 1\r\n100\r\n100\r\n", "output": "-1\r\n"}, {"input": "1 1\r\n99\r\n100\r\n", "output": "-1\r\n"}, {"input": "8 4\r\n1 2 49 99 99 95 78 98\r\n100 100 100 100\r\n", "output": "99"}, {"input": "68 85\r\n43 55 2 4 72 45 19 56 53 81 18 90 11 87 47 8 94 88 24 4 67 9 21 70 25 66 65 27 46 13 8 51 65 99 37 43 71 59 71 79 32 56 49 43 57 85 95 81 40 28 60 36 72 81 60 40 16 78 61 37 29 26 15 95 70 27 50 97\r\n6 6 48 72 54 31 1 50 29 64 93 9 29 93 66 63 25 90 52 1 66 13 70 30 24 87 32 90 84 72 44 13 25 45 31 16 92 60 87 40 62 7 20 63 86 78 73 88 5 36 74 100 64 34 9 5 62 29 58 48 81 46 84 56 27 1 60 14 54 88 31 93 62 7 9 69 27 48 10 5 33 10 53 66 2\r\n", "output": "-1\r\n"}, {"input": "5 100\r\n1 1 1 1 1\r\n77 53 38 29 97 33 64 17 78 100 27 12 42 44 20 24 44 68 58 57 65 90 8 24 4 6 74 68 61 43 25 69 8 62 36 85 67 48 69 30 35 41 42 12 87 66 50 92 53 76 38 67 85 7 80 78 53 76 94 8 37 50 4 100 4 71 10 48 34 47 83 42 25 81 64 72 25 51 53 75 43 98 53 77 94 38 81 15 89 91 72 76 7 36 27 41 88 18 19 75\r\n", "output": "2"}, {"input": "3 3\r\n2 3 4\r\n8 9 10\r\n", "output": "4"}, {"input": "2 1\r\n2 3\r\n15\r\n", "output": "4"}, {"input": "2 1\r\n2 4\r\n4\r\n", "output": "-1\r\n"}, {"input": "2 3\r\n4 5\r\n10 11 12\r\n", "output": "8"}, {"input": "3 1\r\n2 3 3\r\n5\r\n", "output": "4"}, {"input": "2 1\r\n9 10\r\n100\r\n", "output": "18"}, {"input": "3 3\r\n3 12 15\r\n7 8 9\r\n", "output": "-1\r\n"}, {"input": "2 2\r\n3 5\r\n7 8\r\n", "output": "6"}, {"input": "3 3\r\n4 5 6\r\n10 11 12\r\n", "output": "8"}, {"input": "3 5\r\n2 3 3\r\n6 6 6 6 2\r\n", "output": "-1\r\n"}, {"input": "3 6\r\n4 5 3\r\n8 9 7 10 7 11\r\n", "output": "6"}, {"input": "3 6\r\n4 5 2\r\n8 9 6 10 7 4\r\n", "output": "-1\r\n"}, {"input": "2 1\r\n4 6\r\n10\r\n", "output": "8"}, {"input": "1 2\r\n1\r\n3 1\r\n", "output": "-1\r\n"}, {"input": "2 1\r\n5 6\r\n20\r\n", "output": "10"}, {"input": "2 1\r\n1 5\r\n5\r\n", "output": "-1\r\n"}, {"input": "3 2\r\n10 20 30\r\n30 40\r\n", "output": "-1\r\n"}, {"input": "2 2\r\n5 6\r\n7 100\r\n", "output": "-1\r\n"}, {"input": "2 1\r\n2 5\r\n7\r\n", "output": "5"}, {"input": "1 1\r\n5\r\n20\r\n", "output": "10"}, {"input": "2 1\r\n10 11\r\n100\r\n", "output": "20"}, {"input": "1 1\r\n1\r\n10\r\n", "output": "2"}, {"input": "1 1\r\n10\r\n100\r\n", "output": "20"}]
false
stdio
null
true
10/D
10
D
Python 3
TESTS
16
109
307,200
83518902
n1=int(input()) arr1=list(map(int,input().split())) n2=int(input()) arr2=list(map(int,input().split())) def func(n1,arr1,n2,arr2): arr3=[] for i in range(n1+1): c=[] for j in range(n2+1): c.append([]) arr3.append(c.copy()) for i in range(1,n1+1): for j in range(1,n2+1): if(arr1[i-1]==arr2[j-1]): arr3[i][j].extend(arr3[i-1][j-1].copy()) arr3[i][j].append(arr1[i-1]) else: x=arr3[i-1][j] y=arr3[i][j-1] if(len(x)>=len(y)): arr3[i][j].extend(x.copy()) else: arr3[i][j].extend(y.copy()) # print(arr3[n1][n2]) # for i in arr3: # print(i) # print("\n") arr4=arr3[n1][n2].copy() l=len(arr4) arr1=[] arr2=[1]*l for i in range(l): arr1.append([arr4[i]]) for i in range(1,l): for j in range(i): if(arr4[i]>arr4[j]): if(arr2[i]<arr2[j]+1): arr2[i]=arr2[j]+1 arr1[i]=arr1[j].copy() arr1[i].append(arr4[i]) mx=0 res=[] # for i in arr1: # print(i) for i in range(l): if(mx<arr2[i]): mx=arr2[i] res=arr1[i] return [mx,res] f1=func(n1,arr1,n2,arr2) f2=func(n2,arr2,n1,arr1) if(f1[0]==f2[0] and f1[0]==0): print(0) print() elif(f1[0]>=f2[0] and f1[0]!=0): print(f1[0]) for i in f1[1]: print(i,end=" ") elif(f1[0]<f2[0]): print(f2[0]) for i in f2[1]: print(i,end=" ")
61
93
2,969,600
132362843
# -*- coding: utf-8 -*- # @project : 《Atcoder》 # @Author : created by bensonrachel on 2021/10/18 # @File : 28.LCIS.py # 求 find their longest common increasing subsequence最长公共递增子序列。 def output(w,pre_k): if(w != -1): output(pre_k[w],pre_k) print(b[w-1],end=" ") def dp_solve(): dp = [[0]*(m+1) for _ in range(n+1)] pre_k = [-1] * (m+1) for i in range(1,n+1): max_k = 0 k = -1 for j in range(1,m+1): if a[i-1]!=b[j-1]: dp[i][j] = dp[i-1][j] if a[i-1]>b[j-1] and max_k < dp[i-1][j] : max_k = dp[i-1][j] k = j else: dp[i][j] = max_k + 1 pre_k[j] = k ans = 0 w = 0 for index,value in enumerate(dp[-1]): if(value > ans): ans = value w = index print(ans) if(ans): output(w,pre_k) if __name__ == '__main__': n = int(input()) a = [int(i) for i in input().split()] m = int(input()) b = [int(i) for i in input().split()] ans = dp_solve()
Codeforces Beta Round 10
ICPC
2,010
1
256
LCIS
This problem differs from one which was on the online contest. The sequence a1, a2, ..., an is called increasing, if ai < ai + 1 for i < n. The sequence s1, s2, ..., sk is called the subsequence of the sequence a1, a2, ..., an, if there exist such a set of indexes 1 ≤ i1 < i2 < ... < ik ≤ n that aij = sj. In other words, the sequence s can be derived from the sequence a by crossing out some elements. You are given two sequences of integer numbers. You are to find their longest common increasing subsequence, i.e. an increasing sequence of maximum length that is the subsequence of both sequences.
The first line contains an integer n (1 ≤ n ≤ 500) — the length of the first sequence. The second line contains n space-separated integers from the range [0, 109] — elements of the first sequence. The third line contains an integer m (1 ≤ m ≤ 500) — the length of the second sequence. The fourth line contains m space-separated integers from the range [0, 109] — elements of the second sequence.
In the first line output k — the length of the longest common increasing subsequence. In the second line output the subsequence itself. Separate the elements with a space. If there are several solutions, output any.
null
null
[{"input": "7\n2 3 1 6 5 4 6\n4\n1 3 5 6", "output": "3\n3 5 6"}, {"input": "5\n1 2 0 2 1\n3\n1 0 1", "output": "2\n0 1"}]
2,800
["dp"]
61
[{"input": "7\r\n2 3 1 6 5 4 6\r\n4\r\n1 3 5 6\r\n", "output": "3\r\n3 5 6 \r\n"}, {"input": "5\r\n1 2 0 2 1\r\n3\r\n1 0 1\r\n", "output": "2\r\n0 1 \r\n"}, {"input": "2\r\n6 10\r\n3\r\n6 3 3\r\n", "output": "1\r\n6 \r\n"}, {"input": "1\r\n7\r\n2\r\n7 9\r\n", "output": "1\r\n7 \r\n"}, {"input": "3\r\n37 49 24\r\n3\r\n33 5 70\r\n", "output": "0\r\n\r\n"}, {"input": "10\r\n7 10 1 2 1 7 1 5 9 9\r\n9\r\n6 2 5 6 7 7 5 5 2\r\n", "output": "2\r\n2 7 \r\n"}, {"input": "9\r\n7 0 1 2 6 0 10 3 5\r\n4\r\n8 4 0 3\r\n", "output": "2\r\n0 3 \r\n"}, {"input": "9\r\n7 4 4 5 0 6 5 4 10\r\n4\r\n5 2 10 9\r\n", "output": "2\r\n5 10 \r\n"}, {"input": "8\r\n7 8 6 6 8 10 3 3\r\n5\r\n7 4 10 8 7\r\n", "output": "2\r\n7 8 \r\n"}, {"input": "7\r\n4 2 4 3 10 3 6\r\n9\r\n7 5 2 3 0 1 6 1 4\r\n", "output": "3\r\n2 3 6 \r\n"}, {"input": "1\r\n7\r\n10\r\n1 8 8 10 9 10 4 6 0 5\r\n", "output": "0\r\n\r\n"}, {"input": "2\r\n5 2\r\n4\r\n8 8 0 4\r\n", "output": "0\r\n\r\n"}, {"input": "3\r\n1 3 9\r\n10\r\n8 0 10 5 7 0 3 1 2 4\r\n", "output": "1\r\n1 \r\n"}, {"input": "15\r\n10 4 7 7 10 1 4 9 1 10 9 6 8 8 2\r\n2\r\n0 4\r\n", "output": "1\r\n4 \r\n"}, {"input": "16\r\n8 8 6 7 10 0 10 1 7 6 6 0 4 2 6 7\r\n12\r\n9 3 8 4 10 3 9 8 3 7 10 4\r\n", "output": "2\r\n8 10 \r\n"}, {"input": "23\r\n174 172 196 135 91 174 208 92 132 53 202 118 5 244 161 140 71 21 185 56 60 195 217\r\n17\r\n38 218 120 77 22 214 164 194 79 195 36 167 42 89 201 80 11\r\n", "output": "1\r\n195 \r\n"}, {"input": "53\r\n135 168 160 123 6 250 251 158 245 184 206 35 189 64 138 12 69 21 112 198 165 211 109 40 192 98 236 216 255 98 136 38 67 79 25 196 216 64 134 124 102 232 229 102 179 138 111 123 2 93 25 162 57\r\n57\r\n64 143 41 144 73 26 11 17 224 209 167 162 129 39 102 224 254 45 120 2 138 213 139 133 169 54 7 143 242 118 155 189 100 185 145 168 248 131 83 216 142 180 225 35 226 202 8 15 200 192 75 140 191 189 75 116 202\r\n", "output": "3\r\n64 138 192 \r\n"}, {"input": "83\r\n95 164 123 111 177 71 38 225 103 59 210 209 117 139 115 140 66 21 39 84 14 227 0 43 90 233 96 98 232 237 108 139 55 220 14 225 134 39 68 167 193 125 86 216 87 14 94 75 255 24 165 98 177 191 239 123 98 90 29 52 155 231 187 90 180 1 31 237 167 145 242 115 61 190 47 41 61 206 191 248 126 196 26\r\n49\r\n234 134 9 207 37 95 116 239 105 197 191 15 151 249 156 235 17 161 197 199 87 78 191 188 44 151 179 238 72 29 228 157 174 99 190 114 95 185 160 168 58 216 131 151 233 204 213 87 76\r\n", "output": "2\r\n95 233 \r\n"}, {"input": "13\r\n55 160 86 99 92 148 81 36 216 191 214 127 44\r\n85\r\n92 12 64 21 221 225 119 243 147 47 244 112 212 237 209 121 81 239 43 104 3 254 52 13 1 210 28 18 199 75 251 146 77 28 253 211 50 35 42 160 157 104 155 37 241 78 42 190 150 228 193 96 190 178 232 65 231 186 1 123 212 126 239 22 214 186 245 249 66 234 57 78 173 229 185 23 240 91 127 177 240 105 77 208 86\r\n", "output": "2\r\n160 214 \r\n"}, {"input": "94\r\n100 161 99 102 209 51 5 188 217 53 121 5 233 55 25 156 136 195 243 157 110 202 136 151 86 171 253 38 126 40 27 76 60 119 222 52 134 104 184 146 133 220 88 108 246 61 215 184 181 134 223 164 41 193 232 217 38 192 226 91 81 99 204 232 178 4 187 61 160 255 121 142 191 114 114 181 226 49 86 55 252 169 59 190 246 93 21 22 17 18 120 88 93 144\r\n30\r\n61 51 176 38 119 33 100 185 103 84 161 166 103 227 43 200 127 53 52 89 19 215 76 254 110 30 239 247 11 182\r\n", "output": "3\r\n51 53 110 \r\n"}, {"input": "6\r\n3 5 4 6 8 1\r\n10\r\n3 3 0 5 4 0 10 5 6 8\r\n", "output": "4\r\n3 5 6 8 \r\n"}, {"input": "10\r\n0 1 2 3 4 5 6 7 8 9\r\n10\r\n0 1 2 3 4 5 6 7 8 9\r\n", "output": "10\r\n0 1 2 3 4 5 6 7 8 9 \r\n"}, {"input": "8\r\n2 3 4 5 6 8 9 5\r\n9\r\n2 2 3 4 5 6 6 8 9\r\n", "output": "7\r\n2 3 4 5 6 8 9 \r\n"}, {"input": "8\r\n0 4 10 6 7 2 8 5\r\n7\r\n0 4 6 7 7 0 8\r\n", "output": "5\r\n0 4 6 7 8 \r\n"}, {"input": "10\r\n0 1 2 3 4 5 6 7 8 9\r\n10\r\n0 1 2 3 4 5 6 7 8 9\r\n", "output": "10\r\n0 1 2 3 4 5 6 7 8 9 \r\n"}, {"input": "17\r\n12 17 39 156 100 177 188 129 14 142 45 144 243 151 158 194 245\r\n16\r\n125 12 17 199 65 39 100 185 129 194 142 144 62 92 158 194\r\n", "output": "9\r\n12 17 39 100 129 142 144 158 194 \r\n"}, {"input": "20\r\n7 17 24 27 36 45 62 92 93 94 98 112 114 138 143 156 173 199 204 207\r\n20\r\n7 17 24 27 36 45 62 92 93 94 98 112 114 138 143 156 173 199 204 207\r\n", "output": "20\r\n7 17 24 27 36 45 62 92 93 94 98 112 114 138 143 156 173 199 204 207 \r\n"}, {"input": "13\r\n0 46 104 116 63 118 158 16 221 222 136 245 223\r\n9\r\n0 46 104 116 118 158 221 222 245\r\n", "output": "9\r\n0 46 104 116 118 158 221 222 245 \r\n"}, {"input": "13\r\n34 38 51 57 73 125 147 158 160 178 188 198 235\r\n15\r\n34 38 51 57 73 125 147 158 160 178 255 67 188 198 235\r\n", "output": "13\r\n34 38 51 57 73 125 147 158 160 178 188 198 235 \r\n"}, {"input": "17\r\n25 29 37 207 122 189 118 42 54 95 154 160 162 225 228 237 248\r\n19\r\n25 29 248 37 147 209 42 54 255 95 154 160 162 225 228 237 73 248 10\r\n", "output": "13\r\n25 29 37 42 54 95 154 160 162 225 228 237 248 \r\n"}, {"input": "10\r\n62914292 123971042 784965687 324817892 379711365 394545872 813282270 822333477 865397146 437913515\r\n9\r\n297835672 62914292 123971042 324817892 379711365 394545872 813282270 822333477 865397146\r\n", "output": "8\r\n62914292 123971042 324817892 379711365 394545872 813282270 822333477 865397146 \r\n"}, {"input": "10\r\n130077811 57466561 335978192 71385678 434259735 454136111 482887469 530031703 688581885 809880630\r\n8\r\n373627898 57466561 71385678 434259735 454136111 482887469 530031703 809880630\r\n", "output": "7\r\n57466561 71385678 434259735 454136111 482887469 530031703 809880630 \r\n"}, {"input": "17\r\n364396044 90653796 82853043 311258337 326557522 362475139 415783272 428510002 840021181 469284863 541444887 650535473 649258258 750028895 791368777 808443140 959785237\r\n13\r\n82853043 311258337 326557522 362475139 415783272 428510002 469284863 541444887 649258258 750028895 791368777 808443140 959785237\r\n", "output": "13\r\n82853043 311258337 326557522 362475139 415783272 428510002 469284863 541444887 649258258 750028895 791368777 808443140 959785237 \r\n"}, {"input": "3\r\n6379263 55134355 76061584\r\n3\r\n6379263 55134355 76061584\r\n", "output": "3\r\n6379263 55134355 76061584 \r\n"}, {"input": "3\r\n48875076 71023491 76538219\r\n3\r\n48875076 71023491 76538219\r\n", "output": "3\r\n48875076 71023491 76538219 \r\n"}, {"input": "5\r\n6621317 78540394 52064998 89150480 53659440\r\n3\r\n78540394 46008538 839195\r\n", "output": "1\r\n78540394 \r\n"}, {"input": "2\r\n34665180 51128665\r\n5\r\n71074966 34665180 47089728 44119345 51128665\r\n", "output": "2\r\n34665180 51128665 \r\n"}, {"input": "4\r\n3 4 9 1\r\n7\r\n5 3 8 9 10 2 1\r\n", "output": "2\r\n3 9 \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, 'r') as f: input_lines = [line.strip() for line in f] n = int(input_lines[0]) a = list(map(int, input_lines[1].split())) m = int(input_lines[2]) b = list(map(int, input_lines[3].split())) # Read reference output to get k_ref with open(output_path, 'r') as f: ref_lines = [line.strip() for line in f if line.strip()] if not ref_lines: print(0) return k_ref = int(ref_lines[0]) # Read submission output with open(submission_path, 'r') as f: sub_lines = [line.strip() for line in f if line.strip()] if len(sub_lines) < 1: print(0) return try: k_sub = int(sub_lines[0]) except: print(0) return if k_sub != k_ref: print(0) return if len(sub_lines) < 2: s_sub = [] else: try: s_sub = list(map(int, sub_lines[1].split())) except: print(0) return if len(s_sub) != k_sub: print(0) return # Check strictly increasing for i in range(len(s_sub)-1): if s_sub[i] >= s_sub[i+1]: print(0) return # Check subsequence in a and b def is_sub(s, main): ptr = 0 l = len(s) if l ==0: return True for num in main: if num == s[ptr]: ptr +=1 if ptr == l: break return ptr == l if not is_sub(s_sub, a): print(0) return if not is_sub(s_sub, b): print(0) return # All checks passed print(1) if __name__ == "__main__": main()
true
439/C
439
C
PyPy 3-64
TESTS
3
46
0
205770717
import sys input = sys.stdin.readline n, k, p = map(int, input().split()) d = list(map(int, input().split())) a, b, c = [], [], 1 for i in d: if i % 2: a.append(i) else: b.append(i) x = [[] for i in range(k-p)] y = [[] for i in range(p)] if len(a) < k-p: c = 0 else: for i in range(k-p): x[i].append(a.pop()) for i in range(p): if b: y[i].append(b.pop()) elif len(a) > 1: i1, i2 = a.pop(), a.pop() y[i].extend([i1, i2]) else: c = 0 break if len(a) % 2: c = 0 else: for i in a: if p: y[0].append(i) else: x[0].append(i) for i in b: if p: y[0].append(i) else: x[0].append(i) if c == 0: print('NO') else: print('YES') for j in [x, y]: for i in j: print(len(i), *i)
75
373
8,192,000
162845197
from math import inf def solve(od, k, p): ans = [] for i in range(p): if od[0]: ans.append([od[0].pop()]) elif len(od[1]) >= 2: ans.append([od[1].pop(), od[1].pop()]) else: return None for i in range(k - p): if od[1]: ans.append([od[1].pop()]) else: return None if od[0]: ans[0] += od[0] if od[1]: if len(od[1]) % 2 == 0: ans[0] += od[1] else: return None return ans def main(): _, k, p = map(int, input().split()) arr = list(map(int, input().split())) od = [[], []] for a in arr: od[a % 2].append(a) ans = solve(od, k, p) if ans: print('YES') for a in ans: print(len(a), *a) else: print('NO') def init(): import os import sys sys.setrecursionlimit(2 * 10 ** 8) if 'CP_LOCAL_ENV' in os.environ: sys.stdin = open('cf_test_a.txt') if __name__ == '__main__': init() main()
Codeforces Round 251 (Div. 2)
CF
2,014
1
256
Devu and Partitioning of the Array
Devu being a small kid, likes to play a lot, but he only likes to play with arrays. While playing he came up with an interesting question which he could not solve, can you please solve it for him? Given an array consisting of distinct integers. Is it possible to partition the whole array into k disjoint non-empty parts such that p of the parts have even sum (each of them must have even sum) and remaining k - p have odd sum? (note that parts need not to be continuous). If it is possible to partition the array, also give any possible way of valid partitioning.
The first line will contain three space separated integers n, k, p (1 ≤ k ≤ n ≤ 105; 0 ≤ p ≤ k). The next line will contain n space-separated distinct integers representing the content of array a: a1, a2, ..., an (1 ≤ ai ≤ 109).
In the first line print "YES" (without the quotes) if it is possible to partition the array in the required way. Otherwise print "NO" (without the quotes). If the required partition exists, print k lines after the first line. The ith of them should contain the content of the ith part. Print the content of the part in the line in the following way: firstly print the number of elements of the part, then print all the elements of the part in arbitrary order. There must be exactly p parts with even sum, each of the remaining k - p parts must have odd sum. As there can be multiple partitions, you are allowed to print any valid partition.
null
null
[{"input": "5 5 3\n2 6 10 5 9", "output": "YES\n1 9\n1 5\n1 10\n1 6\n1 2"}, {"input": "5 5 3\n7 14 2 9 5", "output": "NO"}, {"input": "5 3 1\n1 2 3 7 5", "output": "YES\n3 5 1 3\n1 7\n1 2"}]
1,700
["brute force", "constructive algorithms", "implementation", "number theory"]
75
[{"input": "5 5 3\r\n2 6 10 5 9\r\n", "output": "YES\r\n1 9\r\n1 5\r\n1 10\r\n1 6\r\n1 2\r\n"}, {"input": "5 5 3\r\n7 14 2 9 5\r\n", "output": "NO\r\n"}, {"input": "5 3 1\r\n1 2 3 7 5\r\n", "output": "YES\r\n3 5 1 3\r\n1 7\r\n1 2\r\n"}, {"input": "10 5 3\r\n194757070 828985446 11164 80016 84729 117765558 111730436 164044532 419732980 48834\r\n", "output": "NO\r\n"}, {"input": "10 6 3\r\n861947514 34945 190135645 68731 44833 387988147 84308862 878151920 458358978 809653252\r\n", "output": "YES\r\n5 387988147 861947514 84308862 34945 190135645\r\n1 44833\r\n1 68731\r\n1 809653252\r\n1 458358978\r\n1 878151920\r\n"}, {"input": "10 8 3\r\n677037706 41099140 89128206 168458947 367939060 940344093 191391519 981938946 31319 34929915\r\n", "output": "YES\r\n3 34929915 677037706 41099140\r\n1 31319\r\n1 191391519\r\n1 940344093\r\n1 168458947\r\n1 981938946\r\n1 367939060\r\n1 89128206\r\n"}, {"input": "10 8 4\r\n214605891 246349108 626595204 63889 794527783 83684156 5535 865709352 262484651 157889982\r\n", "output": "NO\r\n"}, {"input": "10 6 3\r\n223143676 316703192 5286 408323576 61758 566101388 9871840 93989 91890 99264208\r\n", "output": "NO\r\n"}, {"input": "2 2 1\r\n409447178 258805801\r\n", "output": "YES\r\n1 258805801\r\n1 409447178\r\n"}, {"input": "2 1 1\r\n29161 15829\r\n", "output": "YES\r\n2 15829 29161\r\n"}, {"input": "3 3 1\r\n357071129 476170324 503481367\r\n", "output": "YES\r\n1 503481367\r\n1 357071129\r\n1 476170324\r\n"}, {"input": "3 3 1\r\n357071129 476170324 503481367\r\n", "output": "YES\r\n1 503481367\r\n1 357071129\r\n1 476170324\r\n"}, {"input": "2 1 1\r\n29161 15829\r\n", "output": "YES\r\n2 15829 29161\r\n"}, {"input": "23 22 3\r\n777717359 54451 123871650 211256633 193354035 935466677 800874233 532974165 62256 6511 3229 757421727 371493777 268999168 82355 22967 678259053 886134047 207070129 122416584 79851 35753 730872007\r\n", "output": "YES\r\n2 730872007 123871650\r\n1 35753\r\n1 79851\r\n1 207070129\r\n1 886134047\r\n1 678259053\r\n1 22967\r\n1 82355\r\n1 371493777\r\n1 757421727\r\n1 3229\r\n1 6511\r\n1 532974165\r\n1 800874233\r\n1 935466677\r\n1 193354035\r\n1 211256633\r\n1 54451\r\n1 777717359\r\n1 122416584\r\n1 268999168\r\n1 62256\r\n"}, {"input": "16 9 9\r\n826588597 70843 528358243 60844 636968393 862405463 58232 69241 617006886 903909155 973050249 9381 49031 40100022 62141 43805\r\n", "output": "YES\r\n3 40100022 826588597 70843\r\n1 617006886\r\n1 58232\r\n1 60844\r\n2 43805 62141\r\n2 49031 9381\r\n2 973050249 903909155\r\n2 69241 862405463\r\n2 636968393 528358243\r\n"}, {"input": "5 2 2\r\n316313049 24390603 595539594 514135024 39108\r\n", "output": "YES\r\n4 39108 595539594 316313049 24390603\r\n1 514135024\r\n"}, {"input": "5 2 1\r\n12474 117513621 32958 767146609 20843\r\n", "output": "YES\r\n4 20843 12474 117513621 767146609\r\n1 32958\r\n"}, {"input": "5 4 1\r\n387119493 716009972 88510 375210205 910834347\r\n", "output": "YES\r\n2 910834347 716009972\r\n1 375210205\r\n1 387119493\r\n1 88510\r\n"}, {"input": "5 4 3\r\n674318396 881407702 882396010 208141498 53145\r\n", "output": "YES\r\n2 53145 674318396\r\n1 208141498\r\n1 882396010\r\n1 881407702\r\n"}, {"input": "3 2 1\r\n976825506 613159225 249024714\r\n", "output": "YES\r\n2 613159225 976825506\r\n1 249024714\r\n"}, {"input": "4 1 1\r\n173508914 11188 90766233 20363\r\n", "output": "YES\r\n4 11188 173508914 90766233 20363\r\n"}, {"input": "30 24 12\r\n459253071 24156 64054 270713791 734796619 379920883 429646748 332144982 20929 27685 53253 82047732 172442017 34241 880121331 890223629 974692 954036632 68638567 972921811 421106382 64615 819330514 179630214 608594496 802986133 231010377 184513476 73143 93045\r\n", "output": "YES\r\n7 93045 459253071 270713791 734796619 379920883 20929 27685\r\n1 73143\r\n1 231010377\r\n1 802986133\r\n1 64615\r\n1 972921811\r\n1 68638567\r\n1 890223629\r\n1 880121331\r\n1 34241\r\n1 172442017\r\n1 53253\r\n1 184513476\r\n1 608594496\r\n1 179630214\r\n1 819330514\r\n1 421106382\r\n1 954036632\r\n1 974692\r\n1 82047732\r\n1 332144982\r\n1 429646748\r\n1 64054\r\n1 24156\r\n"}, {"input": "9 5 1\r\n91623 466261329 311727429 18189 42557 22943 66177 473271749 62622\r\n", "output": "YES\r\n5 473271749 91623 466261329 311727429 18189\r\n1 66177\r\n1 22943\r\n1 42557\r\n1 62622\r\n"}, {"input": "4 1 1\r\n266639563 36517 172287193 166673809\r\n", "output": "YES\r\n4 166673809 172287193 266639563 36517\r\n"}, {"input": "5 2 2\r\n19571 180100775 421217758 284511211 49475\r\n", "output": "YES\r\n3 421217758 19571 180100775\r\n2 49475 284511211\r\n"}, {"input": "4 2 2\r\n736788713 82230 66800 37791827\r\n", "output": "YES\r\n3 66800 736788713 37791827\r\n1 82230\r\n"}, {"input": "5 1 1\r\n33889 469945850 40673 939970023 776172319\r\n", "output": "YES\r\n5 469945850 33889 40673 939970023 776172319\r\n"}, {"input": "1 1 0\r\n2\r\n", "output": "NO\r\n"}, {"input": "1 1 0\r\n3\r\n", "output": "YES\r\n1 3\r\n"}, {"input": "1 1 1\r\n2\r\n", "output": "YES\r\n1 2\r\n"}, {"input": "1 1 1\r\n3\r\n", "output": "NO\r\n"}, {"input": "2 2 2\r\n2 3\r\n", "output": "NO\r\n"}, {"input": "2 2 2\r\n2 4\r\n", "output": "YES\r\n1 4\r\n1 2\r\n"}, {"input": "2 2 1\r\n3 2\r\n", "output": "YES\r\n1 3\r\n1 2\r\n"}, {"input": "4 2 0\r\n3 5 7 9\r\n", "output": "YES\r\n3 9 3 5\r\n1 7\r\n"}, {"input": "3 2 0\r\n1 3 2\r\n", "output": "YES\r\n2 3 2\r\n1 1\r\n"}, {"input": "2 1 1\r\n2 4\r\n", "output": "YES\r\n2 4 2\r\n"}, {"input": "7 3 0\r\n1 3 5 7 9 11 13\r\n", "output": "YES\r\n5 13 1 3 5 7\r\n1 11\r\n1 9\r\n"}, {"input": "8 4 4\r\n1 3 5 7 9 11 13 15\r\n", "output": "YES\r\n2 15 13\r\n2 11 9\r\n2 7 5\r\n2 3 1\r\n"}, {"input": "2 1 1\r\n1 3\r\n", "output": "YES\r\n2 3 1\r\n"}]
false
stdio
import sys def main(input_path, output_path, submission_path): with open(input_path) as f: n, k, p = map(int, f.readline().split()) a = list(map(int, f.readline().split())) with open(output_path) as f: ref_line = f.readline().strip() correct_answer = ref_line with open(submission_path) as f: lines = [line.strip() for line in f.readlines() if line.strip()] if not lines: print(0) return submission_answer = lines[0] if submission_answer != correct_answer: print(0) return if correct_answer == "NO": if len(lines) != 1: print(0) else: print(1) return else: if len(lines) != k + 1: print(0) return parts_lines = lines[1:] elements_submission = [] sum_counts = 0 even_count = 0 valid = True for line in parts_lines: parts = line.split() if len(parts) < 1: valid = False break if not parts[0].isdigit(): valid = False break count = int(parts[0]) if count < 1: valid = False break sum_counts += count if len(parts) != count + 1: valid = False break try: elements = list(map(int, parts[1:])) except: valid = False break elements_submission.extend(elements) sum_part = sum(elements) if sum_part % 2 == 0: even_count += 1 if not valid or sum_counts != n or set(elements_submission) != set(a) or even_count != p: print(0) else: print(1) if __name__ == "__main__": input_path, output_path, submission_path = sys.argv[1], sys.argv[2], sys.argv[3] main(input_path, output_path, submission_path)
true
439/C
439
C
Python 3
TESTS
59
717
7,884,800
41149423
n,k,p=[int(s) for s in input().split()] ch=[] nech=[] for i in input().split(): i=int(i) if i%2: nech.append(i) else: ch.append(i) if len(nech)<k-p: print('NO') elif (len(nech)-(k-p))%2 or (len(nech)-(k-p))//2+len(ch)<p: print('NO') else: print('YES') j=0 i=0 flag=0 for i in range(k-p): if i>=len(nech): break print(1,nech[i]) flag=1 if flag: i+=1 flag=0 for j in range(p-1): if j>=len(ch): break print(1,ch[j]) flag=1 if flag: j+=1 ind=p-len(ch)-1 while ind>0: print(2,nech[i],nech[i+1]) i+=2 ind-=1 r=0 n='' while i<len(nech): r+=1 n+=str(nech[i])+' ' i+=1 while j<len(ch): r+=1 n+=str(ch[j])+' ' j+=1 if r: print(r,n)
75
374
13,721,600
62250676
import sys inputStr = input() inputArr = inputStr.split(' ') n = int(inputArr[0]) k = int(inputArr[1]) p = int(inputArr[2]) arr = list(map(int, input().split(' '))) odds = [] evens = [] for i in range(len(arr)): if(arr[i]%2): odds.append(i) else: evens.append(i) ans = [] oddPointer = 0 evenPointer = 0 for i in range(k-p): if (oddPointer >= len(odds)): print("NO") quit() oddArr = [odds[oddPointer]] oddPointer += 1 ans.append(oddArr) for i in range(p): if (oddPointer < len(odds)): if (oddPointer + 1 >= len(odds)): print("NO") quit() else : oddArr = [odds[oddPointer], odds[oddPointer+1]] oddPointer += 2 ans.append(oddArr) elif (evenPointer < len(evens)): evenArr = [evens[evenPointer]] evenPointer += 1 ans.append(evenArr) else: print("NO") quit() if ((len(odds) - oddPointer)%2): print("NO") quit() for i in range(oddPointer, len(odds)): ans[0].append(odds[i]) for i in range(evenPointer, len(evens)): ans[0].append(evens[i]) print("YES") for i in range(len(ans)): sys.stdout.write(str(len(ans[i]))) sys.stdout.write(' ') for j in range(len(ans[i])): sys.stdout.write(str(arr[ans[i][j]])) sys.stdout.write(' ') sys.stdout.write('\n') sys.stdout.flush()
Codeforces Round 251 (Div. 2)
CF
2,014
1
256
Devu and Partitioning of the Array
Devu being a small kid, likes to play a lot, but he only likes to play with arrays. While playing he came up with an interesting question which he could not solve, can you please solve it for him? Given an array consisting of distinct integers. Is it possible to partition the whole array into k disjoint non-empty parts such that p of the parts have even sum (each of them must have even sum) and remaining k - p have odd sum? (note that parts need not to be continuous). If it is possible to partition the array, also give any possible way of valid partitioning.
The first line will contain three space separated integers n, k, p (1 ≤ k ≤ n ≤ 105; 0 ≤ p ≤ k). The next line will contain n space-separated distinct integers representing the content of array a: a1, a2, ..., an (1 ≤ ai ≤ 109).
In the first line print "YES" (without the quotes) if it is possible to partition the array in the required way. Otherwise print "NO" (without the quotes). If the required partition exists, print k lines after the first line. The ith of them should contain the content of the ith part. Print the content of the part in the line in the following way: firstly print the number of elements of the part, then print all the elements of the part in arbitrary order. There must be exactly p parts with even sum, each of the remaining k - p parts must have odd sum. As there can be multiple partitions, you are allowed to print any valid partition.
null
null
[{"input": "5 5 3\n2 6 10 5 9", "output": "YES\n1 9\n1 5\n1 10\n1 6\n1 2"}, {"input": "5 5 3\n7 14 2 9 5", "output": "NO"}, {"input": "5 3 1\n1 2 3 7 5", "output": "YES\n3 5 1 3\n1 7\n1 2"}]
1,700
["brute force", "constructive algorithms", "implementation", "number theory"]
75
[{"input": "5 5 3\r\n2 6 10 5 9\r\n", "output": "YES\r\n1 9\r\n1 5\r\n1 10\r\n1 6\r\n1 2\r\n"}, {"input": "5 5 3\r\n7 14 2 9 5\r\n", "output": "NO\r\n"}, {"input": "5 3 1\r\n1 2 3 7 5\r\n", "output": "YES\r\n3 5 1 3\r\n1 7\r\n1 2\r\n"}, {"input": "10 5 3\r\n194757070 828985446 11164 80016 84729 117765558 111730436 164044532 419732980 48834\r\n", "output": "NO\r\n"}, {"input": "10 6 3\r\n861947514 34945 190135645 68731 44833 387988147 84308862 878151920 458358978 809653252\r\n", "output": "YES\r\n5 387988147 861947514 84308862 34945 190135645\r\n1 44833\r\n1 68731\r\n1 809653252\r\n1 458358978\r\n1 878151920\r\n"}, {"input": "10 8 3\r\n677037706 41099140 89128206 168458947 367939060 940344093 191391519 981938946 31319 34929915\r\n", "output": "YES\r\n3 34929915 677037706 41099140\r\n1 31319\r\n1 191391519\r\n1 940344093\r\n1 168458947\r\n1 981938946\r\n1 367939060\r\n1 89128206\r\n"}, {"input": "10 8 4\r\n214605891 246349108 626595204 63889 794527783 83684156 5535 865709352 262484651 157889982\r\n", "output": "NO\r\n"}, {"input": "10 6 3\r\n223143676 316703192 5286 408323576 61758 566101388 9871840 93989 91890 99264208\r\n", "output": "NO\r\n"}, {"input": "2 2 1\r\n409447178 258805801\r\n", "output": "YES\r\n1 258805801\r\n1 409447178\r\n"}, {"input": "2 1 1\r\n29161 15829\r\n", "output": "YES\r\n2 15829 29161\r\n"}, {"input": "3 3 1\r\n357071129 476170324 503481367\r\n", "output": "YES\r\n1 503481367\r\n1 357071129\r\n1 476170324\r\n"}, {"input": "3 3 1\r\n357071129 476170324 503481367\r\n", "output": "YES\r\n1 503481367\r\n1 357071129\r\n1 476170324\r\n"}, {"input": "2 1 1\r\n29161 15829\r\n", "output": "YES\r\n2 15829 29161\r\n"}, {"input": "23 22 3\r\n777717359 54451 123871650 211256633 193354035 935466677 800874233 532974165 62256 6511 3229 757421727 371493777 268999168 82355 22967 678259053 886134047 207070129 122416584 79851 35753 730872007\r\n", "output": "YES\r\n2 730872007 123871650\r\n1 35753\r\n1 79851\r\n1 207070129\r\n1 886134047\r\n1 678259053\r\n1 22967\r\n1 82355\r\n1 371493777\r\n1 757421727\r\n1 3229\r\n1 6511\r\n1 532974165\r\n1 800874233\r\n1 935466677\r\n1 193354035\r\n1 211256633\r\n1 54451\r\n1 777717359\r\n1 122416584\r\n1 268999168\r\n1 62256\r\n"}, {"input": "16 9 9\r\n826588597 70843 528358243 60844 636968393 862405463 58232 69241 617006886 903909155 973050249 9381 49031 40100022 62141 43805\r\n", "output": "YES\r\n3 40100022 826588597 70843\r\n1 617006886\r\n1 58232\r\n1 60844\r\n2 43805 62141\r\n2 49031 9381\r\n2 973050249 903909155\r\n2 69241 862405463\r\n2 636968393 528358243\r\n"}, {"input": "5 2 2\r\n316313049 24390603 595539594 514135024 39108\r\n", "output": "YES\r\n4 39108 595539594 316313049 24390603\r\n1 514135024\r\n"}, {"input": "5 2 1\r\n12474 117513621 32958 767146609 20843\r\n", "output": "YES\r\n4 20843 12474 117513621 767146609\r\n1 32958\r\n"}, {"input": "5 4 1\r\n387119493 716009972 88510 375210205 910834347\r\n", "output": "YES\r\n2 910834347 716009972\r\n1 375210205\r\n1 387119493\r\n1 88510\r\n"}, {"input": "5 4 3\r\n674318396 881407702 882396010 208141498 53145\r\n", "output": "YES\r\n2 53145 674318396\r\n1 208141498\r\n1 882396010\r\n1 881407702\r\n"}, {"input": "3 2 1\r\n976825506 613159225 249024714\r\n", "output": "YES\r\n2 613159225 976825506\r\n1 249024714\r\n"}, {"input": "4 1 1\r\n173508914 11188 90766233 20363\r\n", "output": "YES\r\n4 11188 173508914 90766233 20363\r\n"}, {"input": "30 24 12\r\n459253071 24156 64054 270713791 734796619 379920883 429646748 332144982 20929 27685 53253 82047732 172442017 34241 880121331 890223629 974692 954036632 68638567 972921811 421106382 64615 819330514 179630214 608594496 802986133 231010377 184513476 73143 93045\r\n", "output": "YES\r\n7 93045 459253071 270713791 734796619 379920883 20929 27685\r\n1 73143\r\n1 231010377\r\n1 802986133\r\n1 64615\r\n1 972921811\r\n1 68638567\r\n1 890223629\r\n1 880121331\r\n1 34241\r\n1 172442017\r\n1 53253\r\n1 184513476\r\n1 608594496\r\n1 179630214\r\n1 819330514\r\n1 421106382\r\n1 954036632\r\n1 974692\r\n1 82047732\r\n1 332144982\r\n1 429646748\r\n1 64054\r\n1 24156\r\n"}, {"input": "9 5 1\r\n91623 466261329 311727429 18189 42557 22943 66177 473271749 62622\r\n", "output": "YES\r\n5 473271749 91623 466261329 311727429 18189\r\n1 66177\r\n1 22943\r\n1 42557\r\n1 62622\r\n"}, {"input": "4 1 1\r\n266639563 36517 172287193 166673809\r\n", "output": "YES\r\n4 166673809 172287193 266639563 36517\r\n"}, {"input": "5 2 2\r\n19571 180100775 421217758 284511211 49475\r\n", "output": "YES\r\n3 421217758 19571 180100775\r\n2 49475 284511211\r\n"}, {"input": "4 2 2\r\n736788713 82230 66800 37791827\r\n", "output": "YES\r\n3 66800 736788713 37791827\r\n1 82230\r\n"}, {"input": "5 1 1\r\n33889 469945850 40673 939970023 776172319\r\n", "output": "YES\r\n5 469945850 33889 40673 939970023 776172319\r\n"}, {"input": "1 1 0\r\n2\r\n", "output": "NO\r\n"}, {"input": "1 1 0\r\n3\r\n", "output": "YES\r\n1 3\r\n"}, {"input": "1 1 1\r\n2\r\n", "output": "YES\r\n1 2\r\n"}, {"input": "1 1 1\r\n3\r\n", "output": "NO\r\n"}, {"input": "2 2 2\r\n2 3\r\n", "output": "NO\r\n"}, {"input": "2 2 2\r\n2 4\r\n", "output": "YES\r\n1 4\r\n1 2\r\n"}, {"input": "2 2 1\r\n3 2\r\n", "output": "YES\r\n1 3\r\n1 2\r\n"}, {"input": "4 2 0\r\n3 5 7 9\r\n", "output": "YES\r\n3 9 3 5\r\n1 7\r\n"}, {"input": "3 2 0\r\n1 3 2\r\n", "output": "YES\r\n2 3 2\r\n1 1\r\n"}, {"input": "2 1 1\r\n2 4\r\n", "output": "YES\r\n2 4 2\r\n"}, {"input": "7 3 0\r\n1 3 5 7 9 11 13\r\n", "output": "YES\r\n5 13 1 3 5 7\r\n1 11\r\n1 9\r\n"}, {"input": "8 4 4\r\n1 3 5 7 9 11 13 15\r\n", "output": "YES\r\n2 15 13\r\n2 11 9\r\n2 7 5\r\n2 3 1\r\n"}, {"input": "2 1 1\r\n1 3\r\n", "output": "YES\r\n2 3 1\r\n"}]
false
stdio
import sys def main(input_path, output_path, submission_path): with open(input_path) as f: n, k, p = map(int, f.readline().split()) a = list(map(int, f.readline().split())) with open(output_path) as f: ref_line = f.readline().strip() correct_answer = ref_line with open(submission_path) as f: lines = [line.strip() for line in f.readlines() if line.strip()] if not lines: print(0) return submission_answer = lines[0] if submission_answer != correct_answer: print(0) return if correct_answer == "NO": if len(lines) != 1: print(0) else: print(1) return else: if len(lines) != k + 1: print(0) return parts_lines = lines[1:] elements_submission = [] sum_counts = 0 even_count = 0 valid = True for line in parts_lines: parts = line.split() if len(parts) < 1: valid = False break if not parts[0].isdigit(): valid = False break count = int(parts[0]) if count < 1: valid = False break sum_counts += count if len(parts) != count + 1: valid = False break try: elements = list(map(int, parts[1:])) except: valid = False break elements_submission.extend(elements) sum_part = sum(elements) if sum_part % 2 == 0: even_count += 1 if not valid or sum_counts != n or set(elements_submission) != set(a) or even_count != p: print(0) else: print(1) if __name__ == "__main__": input_path, output_path, submission_path = sys.argv[1], sys.argv[2], sys.argv[3] main(input_path, output_path, submission_path)
true
817/B
817
B
Python 3
TESTS
17
155
8,806,400
27893596
n = input() a = input() a = a.split(' ') a = list(map(int,a)) b = [10**9,10**9,10**9] for i in a: if i < b[0]: b[0] = i elif i < b[1]: b[1] = i elif i < b[2]: b[2] = i if len(set(b)) == 1: s = 0 for i in a: if i == b[0]: s += 1 print(int(s*(s-1)*(s-2)/6)) elif len(set(b)) == 2: s,t = 0,0 for i in a: if i == b[0]: s += 1 elif i == b[2]: t += 1 if b[1] == b[0]: print(int(s*(s-1)/2)*t) else: print(int(t*(t-1)/2)*s) else: s,t,r = 0,0,0 for i in a: if i == b[0]: s += 1 elif i == b[1]: t += 1 elif i == b[2]: r += 1 print(s*t*r)
80
92
8,806,400
156504590
n = int(input()) massiv_a = list(map(int, input().split())) massiv_a = sorted(massiv_a) x, y, z = massiv_a[0], massiv_a[1], massiv_a[2] if (x < y < z): print(massiv_a.count(z)) if ((x < y) and (y == z)): sum_element = massiv_a.count(z) print((sum_element * (sum_element-1)) // 2) if ((x == y) and (y < z)): print(massiv_a.count(z)) if ((x == y) and (y == z)): sum_element = massiv_a.count(z) print((sum_element * (sum_element-1) * (sum_element-2)) // 6)
Educational Codeforces Round 23
ICPC
2,017
2
256
Makes And The Product
After returning from the army Makes received a gift — an array a consisting of n positive integer numbers. He hadn't been solving problems for a long time, so he became interested to answer a particular question: how many triples of indices (i,  j,  k) (i < j < k), such that ai·aj·ak is minimum possible, are there in the array? Help him with it!
The first line of input contains a positive integer number n (3 ≤ n ≤ 105) — the number of elements in array a. The second line contains n positive integer numbers ai (1 ≤ ai ≤ 109) — the elements of a given array.
Print one number — the quantity of triples (i,  j,  k) such that i,  j and k are pairwise distinct and ai·aj·ak is minimum possible.
null
In the first example Makes always chooses three ones out of four, and the number of ways to choose them is 4. In the second example a triple of numbers (1, 2, 3) is chosen (numbers, not indices). Since there are two ways to choose an element 3, then the answer is 2. In the third example a triple of numbers (1, 1, 2) is chosen, and there's only one way to choose indices.
[{"input": "4\n1 1 1 1", "output": "4"}, {"input": "5\n1 3 2 3 4", "output": "2"}, {"input": "6\n1 3 3 1 3 2", "output": "1"}]
1,500
["combinatorics", "implementation", "math", "sortings"]
80
[{"input": "4\r\n1 1 1 1\r\n", "output": "4\r\n"}, {"input": "5\r\n1 3 2 3 4\r\n", "output": "2\r\n"}, {"input": "6\r\n1 3 3 1 3 2\r\n", "output": "1\r\n"}, {"input": "3\r\n1000000000 1000000000 1000000000\r\n", "output": "1\r\n"}, {"input": "4\r\n1 1 2 2\r\n", "output": "2\r\n"}, {"input": "3\r\n1 3 1\r\n", "output": "1\r\n"}, {"input": "11\r\n1 2 2 2 2 2 2 2 2 2 2\r\n", "output": "45\r\n"}, {"input": "5\r\n1 2 2 2 2\r\n", "output": "6\r\n"}, {"input": "6\r\n1 2 2 3 3 4\r\n", "output": "1\r\n"}, {"input": "8\r\n1 1 2 2 2 3 3 3\r\n", "output": "3\r\n"}, {"input": "6\r\n1 2 2 2 2 3\r\n", "output": "6\r\n"}, {"input": "3\r\n1 2 2\r\n", "output": "1\r\n"}, {"input": "6\r\n1 2 2 2 3 3\r\n", "output": "3\r\n"}, {"input": "6\r\n1 2 2 2 2 2\r\n", "output": "10\r\n"}, {"input": "4\r\n1 2 2 2\r\n", "output": "3\r\n"}, {"input": "5\r\n1 2 3 2 3\r\n", "output": "1\r\n"}, {"input": "6\r\n2 2 3 3 3 3\r\n", "output": "4\r\n"}, {"input": "6\r\n1 2 2 2 5 6\r\n", "output": "3\r\n"}, {"input": "10\r\n1 2 2 2 2 2 2 2 2 2\r\n", "output": "36\r\n"}, {"input": "3\r\n2 1 2\r\n", "output": "1\r\n"}, {"input": "5\r\n1 2 3 3 3\r\n", "output": "3\r\n"}, {"input": "6\r\n1 2 2 2 4 5\r\n", "output": "3\r\n"}, {"input": "4\r\n1 2 2 3\r\n", "output": "1\r\n"}, {"input": "10\r\n2 2 2 2 2 1 2 2 2 2\r\n", "output": "36\r\n"}, {"input": "7\r\n2 2 2 3 3 3 1\r\n", "output": "3\r\n"}, {"input": "3\r\n1 1 2\r\n", "output": "1\r\n"}, {"input": "5\r\n1 1 2 2 2\r\n", "output": "3\r\n"}, {"input": "3\r\n1 2 3\r\n", "output": "1\r\n"}, {"input": "9\r\n2 2 3 3 3 3 3 3 3\r\n", "output": "7\r\n"}, {"input": "5\r\n1 1 2 2 3\r\n", "output": "2\r\n"}, {"input": "4\r\n1 1 3 3\r\n", "output": "2\r\n"}, {"input": "4\r\n33554432 33554432 67108864 33554432\r\n", "output": "1\r\n"}, {"input": "6\r\n2 2 2 1 2 2\r\n", "output": "10\r\n"}, {"input": "10\r\n1 2 1 2 3 2 3 2 2 2\r\n", "output": "6\r\n"}, {"input": "10\r\n9 6 4 7 1 8 9 5 9 4\r\n", "output": "1\r\n"}, {"input": "4\r\n5 7 2 7\r\n", "output": "2\r\n"}, {"input": "3\r\n7 6 7\r\n", "output": "1\r\n"}, {"input": "6\r\n3 2 8 2 5 3\r\n", "output": "2\r\n"}, {"input": "3\r\n5 9 5\r\n", "output": "1\r\n"}, {"input": "5\r\n6 3 7 6 3\r\n", "output": "2\r\n"}, {"input": "9\r\n10 10 4 10 7 9 6 7 3\r\n", "output": "1\r\n"}, {"input": "5\r\n9 10 10 3 8\r\n", "output": "1\r\n"}, {"input": "5\r\n2 9 5 10 5\r\n", "output": "1\r\n"}, {"input": "9\r\n7 1 9 6 6 8 3 1 3\r\n", "output": "2\r\n"}, {"input": "5\r\n3 4 4 4 5\r\n", "output": "3\r\n"}, {"input": "3\r\n3 1 3\r\n", "output": "1\r\n"}, {"input": "8\r\n3 2 2 5 2 2 1 2\r\n", "output": "10\r\n"}]
false
stdio
null
true
552/E
552
E
PyPy 3-64
TESTS
12
514
11,878,400
150635155
def allDots(s): l=[] for i in range(len(s)): if s[i]=="*": l.append(i) return l def putParentheses(s,i,j): return s[:i+1]+"("+s[i+1:j]+")"+s[j:] def main(s): s="1*"+s l = allDots(s) m=eval(s) for i in range(len(l)): for j in range(i+1,len(l)): m=max(m,eval(putParentheses(s, l[i], l[j]))) return m s=input("") print(main(s))
69
109
5,427,200
150636730
def Precedence_Order(opd): if opd == '+': return 1 elif opd == "*": return 2 else: return 0 def Operation(num1, num2, opd): if opd == '+': return num1 + num2 if opd == '-': return num1 - num2 if opd == '*': return num1 * num2 if opd == '/': return num1 // num2 def eval(expression): values = [] ops = [] i = 0 while i < len(expression): if expression[i] == ' ': i += 1 continue elif expression[i] == '(': ops.append(expression[i]) elif expression[i].isdigit(): val = 0 while (i < len(expression) and expression[i].isdigit()): val = (val * 10) + int(expression[i]) i += 1 values.append(val) i -= 1 elif expression[i] == ')': while len(ops) != 0 and ops[-1] != '(': val2 = values.pop() val1 = values.pop() opd = ops.pop() values.append(Operation(val1, val2, opd)) ops.pop() else: while (len(ops) != 0 and Precedence_Order(ops[-1]) >= Precedence_Order(expression[i])): val2 = values.pop() val1 = values.pop() opd = ops.pop() values.append(Operation(val1, val2, opd)) ops.append(expression[i]) i += 1 while len(ops) != 0: val2 = values.pop() val1 = values.pop() opd = ops.pop() values.append(Operation(val1, val2, opd)) return values[-1] def allDots(s): l=[] for i in range(len(s)): if s[i]=="*": l.append(i) return l def putParentheses(s,i,j): return s[:i+1]+"("+s[i+1:j]+")"+s[j:] def main(s): s="1*"+s+"*1" l = allDots(s) m=eval(s) for i in range(len(l)): for j in range(i+1,len(l)): m=max(m,eval(putParentheses(s, l[i], l[j]))) return m s=input("") print(main(s))
Codeforces Round 308 (Div. 2)
CF
2,015
1
256
Vanya and Brackets
Vanya is doing his maths homework. He has an expression of form $$x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$$, where x1, x2, ..., xn are digits from 1 to 9, and sign $$\text{ }$$ represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
The first line contains expression s (1 ≤ |s| ≤ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * . The number of signs * doesn't exceed 15.
In the first line print the maximum possible value of an expression.
null
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303. Note to the second sample test. (2 + 3) * 5 = 25. Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60).
[{"input": "3+5*7+8*4", "output": "303"}, {"input": "2+3*5", "output": "25"}, {"input": "3*4*5", "output": "60"}]
2,100
["brute force", "dp", "expression parsing", "greedy", "implementation", "strings"]
69
[{"input": "3+5*7+8*4\r\n", "output": "303\r\n"}, {"input": "2+3*5\r\n", "output": "25\r\n"}, {"input": "3*4*5\r\n", "output": "60\r\n"}, {"input": "5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5\r\n", "output": "152587890625\r\n"}, {"input": "2*2+2*2\r\n", "output": "16\r\n"}, {"input": "1+1+1+1+1+1+1\r\n", "output": "7\r\n"}, {"input": "1+5*6+7*8\r\n", "output": "521\r\n"}, {"input": "9*8+7*6+5*4+3*2+1\r\n", "output": "1987\r\n"}, {"input": "3*3*9+4+6+8*4+5+1*4*6\r\n", "output": "12312\r\n"}, {"input": "4*9+4+5+8*4+6+9+8+2+5+2+5*7+6+8\r\n", "output": "2450\r\n"}, {"input": "9+9+9*9*9*9+9+9\r\n", "output": "19701\r\n"}, {"input": "9+9+9+9+9*9*9*9\r\n", "output": "32805\r\n"}, {"input": "1*1*1*1*1*1*1*1+1*1*1*1*1*1*1*1\r\n", "output": "2\r\n"}, {"input": "4+2*7+8+9*6+6*9+8+7*2+4\r\n", "output": "1380\r\n"}, {"input": "5\r\n", "output": "5\r\n"}, {"input": "4+6*7+4\r\n", "output": "74\r\n"}, {"input": "2+7+3+5+4+2+3+9+9+6+9+2+3+6+5*3+4+5+6+5+8\r\n", "output": "253\r\n"}, {"input": "3+2+2+3+7+1+9+1+6+8+3+2+2+6+7+2+8+8+1+4+9\r\n", "output": "94\r\n"}, {"input": "3+9+3+1+6+4+7+9+5+8+2+6+1+4+4+5+1+7+5+4+6+4+3+1*9+7+7+4+5+2+3+2+6+5+5+8+7+8+2+3*3+8+3+4+9+8*5+9+2+8+2+8+6+6+9+6+4+2+5+3+1+2+6+6+2+4+6+4+2+7+2+7+6+9+9+3+6+7+8+3+3+2+3+7+9+7+8+5+5+5*1+3+2+5+8+5*6+5+4*6+2+5+5+4+9+8+3+5+1+3+1+6+2+2+1+3+2+3+3+3+2+8+3+2+8+8+5+2+6+6+3+1+1+5+5*1+5+7+5+8+4+1*7+5+9+5+8+1*8+5+9+3+1+8+6+7+8+3+5+1+5+6+9*9+6+1+9+8+9+1+5+9+9+6+3+8+8+6*9*3+9+7+7+4+3+8+8+6+7+1+8+6+3+1+7+7+1+1+3+9+8+5+5+6+8+2+4+1+5+7+2+3+7+1+5+1+6+1+7+3*5+5+9+2+1+3+9+4+8+6+5+5+2+3+7+9+5+6+8+3*3+2+4+4+6+3+2+4+1+4+8\r\n", "output": "162353\r\n"}, {"input": "1*5*1+8*2*6*5*3*9+3+8+2+9*5+7+2+9+5+1*3+2*2*3*4*2*3\r\n", "output": "19699205\r\n"}, {"input": "4+4+6+2+5+9+9+5+5+9+4+1*5+3+6+9+6+2+4+3+2+8+9*6+5+4+3+8+7+3+2*3+1+6+8+3+8+1+8+2+1+1+1+6+9+6+4+6+7+8+3+1+5+4+8+8+6+5+8+7+7+1+7+6+3+3+9+6+3+5+4+4+1+4+1+8+6+2+9+8+7+2+3+1+4+3+9+9+2*1+3+8+2+4+1+8+9+3*7+3+7+5+3+7+5+5+3+2+9+8+4+7+5+3+7+7+3+8+9+4+9+6*6+3+8+8*7+7+9+1+3+5+1+1+1+9+8+2+1+1+5+5+5+1+6+7+3+6+1+4+1+7+1+7+1+1+9+9*4+1+3+9+3+5+5+5+5+2+9+6+7+3+5+9+3+5+3+9+3+9+9+2+7+2+1*4+6*2+5+7+6+1+1+2+8+9+5+8+3+9+9+1+1+4+9+7+5+8*9+5+2+6+5+6*2+4+2+5+2+3+9+6+9+5+5+5*6+8+2+3+1+2+8+3+1+6+5+9+7+4+2+8+9+1+5+8+5+3+2+7+1\r\n", "output": "82140\r\n"}, {"input": "6*9+9*5*5+1*2*9*9*1+4*8+8+9+5+6*5*6+4+2+2+1+5*5*7*8\r\n", "output": "11294919\r\n"}, {"input": "5+3+5+9+3+9+1+3+1*7+7+1+9+3+7+7+6+6+3+7+4+3+6+4+5+1+2*3+6*5+5+6+2+8+3+3+9+9+1+1+2+8+4+8+9+3*7+3+2*8+9+8+1*9+9+7+4+8+6+7+3+5+6+4+4+9+2+2+8+6+7+1+5+4+4+6+6+6+9+8+7+2+3+5+4+6+1+8+8+9+1+9+6+3+8+5*7+3+1+6+7+9+1+6+2+2+8+8+9+3+7+7+2+5+8+6+7+9+7+2+4+9+8+3+7+4+5+7+6+5*6+4+6+4+6+2+2*6+2+5+5+1+8+7+7+6+6+8+2+8+8+6+7+1+1+1+2+5+1+1+8+9+9+6+5+8+7+5+8+4+8+8+1+4+6+7+3+2*1+1+3+5+3+3+3+9+8+7*2+4+7+5+8+3+3+9+3+7+2+1+1+7+6+2+5+5+2+1+8+8+2+9+9+2+4+6+6+4+8+9+3+7+1+3*9+8+7+4+9+4+6+2+9+8+8+5+8+8+2+5+6+6+4+7+9+4+7+2+3+1+7\r\n", "output": "58437\r\n"}, {"input": "2+7+8*8*7+1+3+6*5*3*7*3*2+8+5*1+5*5+9*6+6*5+1*3+8+5\r\n", "output": "1473847\r\n"}, {"input": "1+2+4+8+6+5+3+8+2+9+9+5+8+7+7+7+6+1+7+2+8+3+2+5+1+6+1+3+8+2+5+4+3+5+7+8+5+7+7+3+8+1+7+1+1+1+5+9+5+9+1+6+7+6+8+9+2+7+9+2+9+9+7+3+2+8+4+4+5+9+6+2+6+8+1+3+5+3+9+4+7+4+3+9+8+2+6+3+5+1*3+1+6+8+5+3+9+2+9+9+3+4+8*6+3+9+7+1+1+4+6+4+5*6*1+1*9+6+5+4+3+7+3+8+6+2+3+7+4+1+5+8+6+1+6+9+1+2+7+2+2+1+7+9+4+3+1+4+3+3+1+1+2+1+8+9+8+6+9+9+6+3+7*1+1+3+7+9+3+6+5+2*9+8+1+9+8+7+5+3+6+9+3+5+3+5+5+7+5+2*9+9+2+4+2+3+7+1+7+1+3+8+6+4+5+9+3*2+8+6+8+2*6+8+1+4+2+7+7+6+8+3+2+5+8+1+8+5+6+1+6+4+6+8+6+6+4+3+5+2+1+5+9+9+4+4*9+7+8+4+4\r\n", "output": "178016\r\n"}, {"input": "8+3*6*9*6+5*1*8*2+1+9+2+1*3*2+9+5+4+3+1+3*9*6*8+4+1\r\n", "output": "9027949\r\n"}, {"input": "1*1*1*1*1*1*1*1*1*1*1*1\r\n", "output": "1\r\n"}, {"input": "5+5*5+5*5+5*5+5\r\n", "output": "885\r\n"}, {"input": "8+7+3+6+3*8+8+9+8+4+2\r\n", "output": "247\r\n"}, {"input": "7+8*4+9+5+3+2+3+3+2+9\r\n", "output": "327\r\n"}, {"input": "1+1+7+1+7+7*7+5+3*9+3\r\n", "output": "965\r\n"}, {"input": "9+6+9+7+8*2*9+8+6+7+5\r\n", "output": "728\r\n"}, {"input": "8+8*3*8+1+9*4+9+2+8+4\r\n", "output": "1759\r\n"}, {"input": "3+5+5+2+2+9*7+7+7*2*2\r\n", "output": "773\r\n"}, {"input": "6+8+5+9*2+7*9*3+2*2+8\r\n", "output": "3501\r\n"}, {"input": "2*3+9+6*5*8+2+9*6+3+9\r\n", "output": "3447\r\n"}, {"input": "7+7*6+7+6*1+8+8*1*2*4\r\n", "output": "1967\r\n"}, {"input": "3+2*5+9+5*2+5*5*7+9*2\r\n", "output": "2051\r\n"}, {"input": "3+4*5+6\r\n", "output": "47\r\n"}]
false
stdio
null
true
822/B
822
B
Python 3
TESTS
3
31
0
150031259
n,m=map(int,input().split()) s=input() t=input() i=0 qm=m pos=[] for i in range(m-n+1): temp="" l=[] for j in range(n): if(t[i+j]==s[j]): temp+=s[j] else: l.append(j+1) temp+='?' temp1=temp.count('?') if(temp1<qm): qm=temp1 pos=l print(qm) for i in pos: print(i,end=" ")
113
77
31,436,800
132686293
from math import * n,m=map(int,input().split()) s,r=input(),input() q=inf f=[] for i in range(m-n+1): d=[0 for u in range(n)] for j in range(n): if(r[i+j]!=s[j]): d[j]=1 w=sum(d) if(w<q): q=w f=d l=[] for i in range(n): if(f[i]==1): l.append(i+1) print(len(l)) print(*l)
Codeforces Round 422 (Div. 2)
CF
2,017
1
256
Crossword solving
Erelong Leha was bored by calculating of the greatest common divisor of two factorials. Therefore he decided to solve some crosswords. It's well known that it is a very interesting occupation though it can be very difficult from time to time. In the course of solving one of the crosswords, Leha had to solve a simple task. You are able to do it too, aren't you? Leha has two strings s and t. The hacker wants to change the string s at such way, that it can be found in t as a substring. All the changes should be the following: Leha chooses one position in the string s and replaces the symbol in this position with the question mark "?". The hacker is sure that the question mark in comparison can play the role of an arbitrary symbol. For example, if he gets string s="ab?b" as a result, it will appear in t="aabrbb" as a substring. Guaranteed that the length of the string s doesn't exceed the length of the string t. Help the hacker to replace in s as few symbols as possible so that the result of the replacements can be found in t as a substring. The symbol "?" should be considered equal to any other symbol.
The first line contains two integers n and m (1 ≤ n ≤ m ≤ 1000) — the length of the string s and the length of the string t correspondingly. The second line contains n lowercase English letters — string s. The third line contains m lowercase English letters — string t.
In the first line print single integer k — the minimal number of symbols that need to be replaced. In the second line print k distinct integers denoting the positions of symbols in the string s which need to be replaced. Print the positions in any order. If there are several solutions print any of them. The numbering of the positions begins from one.
null
null
[{"input": "3 5\nabc\nxaybz", "output": "2\n2 3"}, {"input": "4 10\nabcd\nebceabazcd", "output": "1\n2"}]
1,000
["brute force", "implementation", "strings"]
113
[{"input": "3 5\r\nabc\r\nxaybz\r\n", "output": "2\r\n2 3 \r\n"}, {"input": "4 10\r\nabcd\r\nebceabazcd\r\n", "output": "1\r\n2 \r\n"}, {"input": "1 1\r\na\r\na\r\n", "output": "0\r\n\r\n"}, {"input": "1 1\r\na\r\nz\r\n", "output": "1\r\n1 \r\n"}, {"input": "3 5\r\naaa\r\naaaaa\r\n", "output": "0\r\n\r\n"}, {"input": "3 5\r\naaa\r\naabaa\r\n", "output": "1\r\n3 \r\n"}, {"input": "5 5\r\ncoder\r\ncored\r\n", "output": "2\r\n3 5 \r\n"}, {"input": "1 1\r\nz\r\nz\r\n", "output": "0\r\n\r\n"}, {"input": "1 2\r\nf\r\nrt\r\n", "output": "1\r\n1 \r\n"}, {"input": "1 2\r\nf\r\nfg\r\n", "output": "0\r\n\r\n"}, {"input": "1 2\r\nf\r\ngf\r\n", "output": "0\r\n\r\n"}, {"input": "2 5\r\naa\r\naabaa\r\n", "output": "0\r\n\r\n"}, {"input": "2 5\r\naa\r\navaca\r\n", "output": "1\r\n2 \r\n"}, {"input": "3 5\r\naaa\r\nbbbbb\r\n", "output": "3\r\n1 2 3 \r\n"}, {"input": "3 5\r\naba\r\ncbcbc\r\n", "output": "2\r\n1 3 \r\n"}, {"input": "3 5\r\naba\r\nbbbbb\r\n", "output": "2\r\n1 3 \r\n"}, {"input": "3 5\r\naaa\r\naabvd\r\n", "output": "1\r\n3 \r\n"}, {"input": "3 5\r\nvvv\r\nbqavv\r\n", "output": "1\r\n1 \r\n"}, {"input": "10 100\r\nmpmmpmmmpm\r\nmppppppmppmmpmpppmpppmmpppmpppppmpppmmmppmpmpmmmpmmpmppmmpppppmpmppppmmppmpmppmmmmpmmppmmmpmpmmmpppp\r\n", "output": "2\r\n5 6 \r\n"}, {"input": "26 26\r\nabcdefghijklmnopqrstuvwxyz\r\nffffffffffffffffffffffffff\r\n", "output": "25\r\n1 2 3 4 5 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 \r\n"}, {"input": "3 5\r\nabc\r\nxyzab\r\n", "output": "3\r\n1 2 3 \r\n"}, {"input": "4 4\r\nabcd\r\nxabc\r\n", "output": "4\r\n1 2 3 4 \r\n"}, {"input": "3 4\r\nabc\r\nabcd\r\n", "output": "0\r\n\r\n"}, {"input": "3 3\r\nabc\r\nxxa\r\n", "output": "3\r\n1 2 3 \r\n"}, {"input": "3 5\r\naab\r\nzfhka\r\n", "output": "3\r\n1 2 3 \r\n"}, {"input": "3 3\r\nabc\r\nxya\r\n", "output": "3\r\n1 2 3 \r\n"}, {"input": "3 3\r\nabc\r\ncab\r\n", "output": "3\r\n1 2 3 \r\n"}, {"input": "5 5\r\nabcde\r\nxxabc\r\n", "output": "5\r\n1 2 3 4 5 \r\n"}, {"input": "3 10\r\nass\r\nabcdefssss\r\n", "output": "1\r\n1 \r\n"}, {"input": "4 4\r\nabcd\r\neeab\r\n", "output": "4\r\n1 2 3 4 \r\n"}, {"input": "3 4\r\nabh\r\nbhaa\r\n", "output": "3\r\n1 2 3 \r\n"}, {"input": "2 3\r\nzb\r\naaz\r\n", "output": "2\r\n1 2 \r\n"}, {"input": "2 3\r\nab\r\ndda\r\n", "output": "2\r\n1 2 \r\n"}, {"input": "3 3\r\ncba\r\nbac\r\n", "output": "3\r\n1 2 3 \r\n"}, {"input": "3 4\r\nabc\r\nxxxa\r\n", "output": "3\r\n1 2 3 \r\n"}, {"input": "2 3\r\nab\r\nbbb\r\n", "output": "1\r\n1 \r\n"}, {"input": "10 15\r\nsdkjeaafww\r\nefjklffnkddkfey\r\n", "output": "9\r\n1 2 4 5 6 7 8 9 10 \r\n"}, {"input": "3 3\r\nabc\r\nzbc\r\n", "output": "1\r\n1 \r\n"}, {"input": "3 7\r\nabc\r\neeeeeab\r\n", "output": "3\r\n1 2 3 \r\n"}, {"input": "2 6\r\nab\r\nxyxbab\r\n", "output": "0\r\n\r\n"}, {"input": "4 7\r\nabcd\r\nzzzzabc\r\n", "output": "4\r\n1 2 3 4 \r\n"}, {"input": "3 5\r\nabc\r\nabzzz\r\n", "output": "1\r\n3 \r\n"}, {"input": "3 3\r\naaz\r\nzaa\r\n", "output": "2\r\n1 3 \r\n"}, {"input": "3 6\r\nabc\r\nxaybzd\r\n", "output": "2\r\n2 3 \r\n"}, {"input": "4 5\r\naaaa\r\naaaap\r\n", "output": "0\r\n\r\n"}]
false
stdio
null
true
552/E
552
E
Python 3
TESTS
12
405
2,048,000
61471694
import sys def f2(exp: str, index1: int, index2: int): return eval(exp[: index1] + '(' + exp[index1: index2 + 1] + ')' + exp[index2 + 1:]) def f1(exp: str): res, pos = eval(exp), [-1] for i in range(1, len(exp) - 1, 2): if exp[i] == '*': pos.append(i) pos.append(len(exp)) for i in range(0, len(pos) - 1): for j in range(i + 1, len(pos) - 1): res = max(res, f2(exp, pos[i] + 1, pos[j] - 1)) return res for line in sys.stdin: print(f1(line))
69
187
102,400
11649482
qw = input() g = ['*'] s = [1] for i in range(len(qw)): if i % 2 == 0: s.append(int(qw[i])) else: g.append(qw[i]) c = 0 g.append('*') s.append(1) for i in range(len(g)): if g[i] == '*': for j in range(i + 1, len(g)): if g[j] == '*': a = 0 b = s[0] for k in range(i): if g[k] == '*': b *= s[k + 1] else: a += b b = s[k + 1] v = a w = b a = 0 b = s[i + 1] for k in range(i + 1, j): if g[k] == '*': b *= s[k + 1] else: a += b b = s[k + 1] b = w * (a + b) a = v for k in range(j, len(g)): if g[k] == '*': b *= s[k + 1] else: a += b b = s[k + 1] c = max(c, a + b) print(c)
Codeforces Round 308 (Div. 2)
CF
2,015
1
256
Vanya and Brackets
Vanya is doing his maths homework. He has an expression of form $$x_{1} \diamond x_{2} \diamond x_{3} \diamond \ldots \diamond x_{n}$$, where x1, x2, ..., xn are digits from 1 to 9, and sign $$\text{ }$$ represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
The first line contains expression s (1 ≤ |s| ≤ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * . The number of signs * doesn't exceed 15.
In the first line print the maximum possible value of an expression.
null
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303. Note to the second sample test. (2 + 3) * 5 = 25. Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60).
[{"input": "3+5*7+8*4", "output": "303"}, {"input": "2+3*5", "output": "25"}, {"input": "3*4*5", "output": "60"}]
2,100
["brute force", "dp", "expression parsing", "greedy", "implementation", "strings"]
69
[{"input": "3+5*7+8*4\r\n", "output": "303\r\n"}, {"input": "2+3*5\r\n", "output": "25\r\n"}, {"input": "3*4*5\r\n", "output": "60\r\n"}, {"input": "5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5\r\n", "output": "152587890625\r\n"}, {"input": "2*2+2*2\r\n", "output": "16\r\n"}, {"input": "1+1+1+1+1+1+1\r\n", "output": "7\r\n"}, {"input": "1+5*6+7*8\r\n", "output": "521\r\n"}, {"input": "9*8+7*6+5*4+3*2+1\r\n", "output": "1987\r\n"}, {"input": "3*3*9+4+6+8*4+5+1*4*6\r\n", "output": "12312\r\n"}, {"input": "4*9+4+5+8*4+6+9+8+2+5+2+5*7+6+8\r\n", "output": "2450\r\n"}, {"input": "9+9+9*9*9*9+9+9\r\n", "output": "19701\r\n"}, {"input": "9+9+9+9+9*9*9*9\r\n", "output": "32805\r\n"}, {"input": "1*1*1*1*1*1*1*1+1*1*1*1*1*1*1*1\r\n", "output": "2\r\n"}, {"input": "4+2*7+8+9*6+6*9+8+7*2+4\r\n", "output": "1380\r\n"}, {"input": "5\r\n", "output": "5\r\n"}, {"input": "4+6*7+4\r\n", "output": "74\r\n"}, {"input": "2+7+3+5+4+2+3+9+9+6+9+2+3+6+5*3+4+5+6+5+8\r\n", "output": "253\r\n"}, {"input": "3+2+2+3+7+1+9+1+6+8+3+2+2+6+7+2+8+8+1+4+9\r\n", "output": "94\r\n"}, {"input": "3+9+3+1+6+4+7+9+5+8+2+6+1+4+4+5+1+7+5+4+6+4+3+1*9+7+7+4+5+2+3+2+6+5+5+8+7+8+2+3*3+8+3+4+9+8*5+9+2+8+2+8+6+6+9+6+4+2+5+3+1+2+6+6+2+4+6+4+2+7+2+7+6+9+9+3+6+7+8+3+3+2+3+7+9+7+8+5+5+5*1+3+2+5+8+5*6+5+4*6+2+5+5+4+9+8+3+5+1+3+1+6+2+2+1+3+2+3+3+3+2+8+3+2+8+8+5+2+6+6+3+1+1+5+5*1+5+7+5+8+4+1*7+5+9+5+8+1*8+5+9+3+1+8+6+7+8+3+5+1+5+6+9*9+6+1+9+8+9+1+5+9+9+6+3+8+8+6*9*3+9+7+7+4+3+8+8+6+7+1+8+6+3+1+7+7+1+1+3+9+8+5+5+6+8+2+4+1+5+7+2+3+7+1+5+1+6+1+7+3*5+5+9+2+1+3+9+4+8+6+5+5+2+3+7+9+5+6+8+3*3+2+4+4+6+3+2+4+1+4+8\r\n", "output": "162353\r\n"}, {"input": "1*5*1+8*2*6*5*3*9+3+8+2+9*5+7+2+9+5+1*3+2*2*3*4*2*3\r\n", "output": "19699205\r\n"}, {"input": "4+4+6+2+5+9+9+5+5+9+4+1*5+3+6+9+6+2+4+3+2+8+9*6+5+4+3+8+7+3+2*3+1+6+8+3+8+1+8+2+1+1+1+6+9+6+4+6+7+8+3+1+5+4+8+8+6+5+8+7+7+1+7+6+3+3+9+6+3+5+4+4+1+4+1+8+6+2+9+8+7+2+3+1+4+3+9+9+2*1+3+8+2+4+1+8+9+3*7+3+7+5+3+7+5+5+3+2+9+8+4+7+5+3+7+7+3+8+9+4+9+6*6+3+8+8*7+7+9+1+3+5+1+1+1+9+8+2+1+1+5+5+5+1+6+7+3+6+1+4+1+7+1+7+1+1+9+9*4+1+3+9+3+5+5+5+5+2+9+6+7+3+5+9+3+5+3+9+3+9+9+2+7+2+1*4+6*2+5+7+6+1+1+2+8+9+5+8+3+9+9+1+1+4+9+7+5+8*9+5+2+6+5+6*2+4+2+5+2+3+9+6+9+5+5+5*6+8+2+3+1+2+8+3+1+6+5+9+7+4+2+8+9+1+5+8+5+3+2+7+1\r\n", "output": "82140\r\n"}, {"input": "6*9+9*5*5+1*2*9*9*1+4*8+8+9+5+6*5*6+4+2+2+1+5*5*7*8\r\n", "output": "11294919\r\n"}, {"input": "5+3+5+9+3+9+1+3+1*7+7+1+9+3+7+7+6+6+3+7+4+3+6+4+5+1+2*3+6*5+5+6+2+8+3+3+9+9+1+1+2+8+4+8+9+3*7+3+2*8+9+8+1*9+9+7+4+8+6+7+3+5+6+4+4+9+2+2+8+6+7+1+5+4+4+6+6+6+9+8+7+2+3+5+4+6+1+8+8+9+1+9+6+3+8+5*7+3+1+6+7+9+1+6+2+2+8+8+9+3+7+7+2+5+8+6+7+9+7+2+4+9+8+3+7+4+5+7+6+5*6+4+6+4+6+2+2*6+2+5+5+1+8+7+7+6+6+8+2+8+8+6+7+1+1+1+2+5+1+1+8+9+9+6+5+8+7+5+8+4+8+8+1+4+6+7+3+2*1+1+3+5+3+3+3+9+8+7*2+4+7+5+8+3+3+9+3+7+2+1+1+7+6+2+5+5+2+1+8+8+2+9+9+2+4+6+6+4+8+9+3+7+1+3*9+8+7+4+9+4+6+2+9+8+8+5+8+8+2+5+6+6+4+7+9+4+7+2+3+1+7\r\n", "output": "58437\r\n"}, {"input": "2+7+8*8*7+1+3+6*5*3*7*3*2+8+5*1+5*5+9*6+6*5+1*3+8+5\r\n", "output": "1473847\r\n"}, {"input": "1+2+4+8+6+5+3+8+2+9+9+5+8+7+7+7+6+1+7+2+8+3+2+5+1+6+1+3+8+2+5+4+3+5+7+8+5+7+7+3+8+1+7+1+1+1+5+9+5+9+1+6+7+6+8+9+2+7+9+2+9+9+7+3+2+8+4+4+5+9+6+2+6+8+1+3+5+3+9+4+7+4+3+9+8+2+6+3+5+1*3+1+6+8+5+3+9+2+9+9+3+4+8*6+3+9+7+1+1+4+6+4+5*6*1+1*9+6+5+4+3+7+3+8+6+2+3+7+4+1+5+8+6+1+6+9+1+2+7+2+2+1+7+9+4+3+1+4+3+3+1+1+2+1+8+9+8+6+9+9+6+3+7*1+1+3+7+9+3+6+5+2*9+8+1+9+8+7+5+3+6+9+3+5+3+5+5+7+5+2*9+9+2+4+2+3+7+1+7+1+3+8+6+4+5+9+3*2+8+6+8+2*6+8+1+4+2+7+7+6+8+3+2+5+8+1+8+5+6+1+6+4+6+8+6+6+4+3+5+2+1+5+9+9+4+4*9+7+8+4+4\r\n", "output": "178016\r\n"}, {"input": "8+3*6*9*6+5*1*8*2+1+9+2+1*3*2+9+5+4+3+1+3*9*6*8+4+1\r\n", "output": "9027949\r\n"}, {"input": "1*1*1*1*1*1*1*1*1*1*1*1\r\n", "output": "1\r\n"}, {"input": "5+5*5+5*5+5*5+5\r\n", "output": "885\r\n"}, {"input": "8+7+3+6+3*8+8+9+8+4+2\r\n", "output": "247\r\n"}, {"input": "7+8*4+9+5+3+2+3+3+2+9\r\n", "output": "327\r\n"}, {"input": "1+1+7+1+7+7*7+5+3*9+3\r\n", "output": "965\r\n"}, {"input": "9+6+9+7+8*2*9+8+6+7+5\r\n", "output": "728\r\n"}, {"input": "8+8*3*8+1+9*4+9+2+8+4\r\n", "output": "1759\r\n"}, {"input": "3+5+5+2+2+9*7+7+7*2*2\r\n", "output": "773\r\n"}, {"input": "6+8+5+9*2+7*9*3+2*2+8\r\n", "output": "3501\r\n"}, {"input": "2*3+9+6*5*8+2+9*6+3+9\r\n", "output": "3447\r\n"}, {"input": "7+7*6+7+6*1+8+8*1*2*4\r\n", "output": "1967\r\n"}, {"input": "3+2*5+9+5*2+5*5*7+9*2\r\n", "output": "2051\r\n"}, {"input": "3+4*5+6\r\n", "output": "47\r\n"}]
false
stdio
null
true
963/A
963
A
PyPy 3
PRETESTS
8
140
24,268,800
37407597
mod = 10 ** 9 + 9 def binPow(x, k): global mod x %= mod if k == 0: return 1 elif k == 1: return x % mod elif k % 2 == 0: return binPow((x * x) % mod, k // 2) else: return (binPow((x * x) % mod, k // 2) * x) % mod def rev(x): return binPow(x, mod - 2) n, a, b, k = map(int, input().split()) n += 1 x = ((binPow(a, n) - binPow(b, n)) * binPow(a, k - 1)) % mod y = (binPow(a, k) - binPow(b, k)) % mod z = (x * rev(y)) % mod x1, x2 = 1, 1 ans = 0 for ch in input(): add = (x1 * z * rev(x2)) % mod if ch == '-': add = - add ans = (ans + add) % mod x1 = (x1 * b) % mod x2 = (x2 * a) % mod print(ans % mod)
71
124
204,800
175195718
M = 0x3b9aca09 inv = lambda x: pow(x, M - 2, M) n, a, b, k = map(int, input().split()) s = input() c = inv(a) * b % M q = pow(c, k, M) m = (n + 1) // k p = (pow(q, m, M) - 1) * inv(q - 1) % M if q - 1 else m x = pow(a, n, M) r = 0 for i in range(k): r = (r + [-1, 1][s[i] == '+'] * x * p) % M x = (x * c) % M print(r)
Tinkoff Internship Warmup Round 2018 and Codeforces Round 475 (Div. 1)
CF
2,018
1
256
Alternating Sum
You are given two integers $$$a$$$ and $$$b$$$. Moreover, you are given a sequence $$$s_0, s_1, \dots, s_{n}$$$. All values in $$$s$$$ are integers $$$1$$$ or $$$-1$$$. It's known that sequence is $$$k$$$-periodic and $$$k$$$ divides $$$n+1$$$. In other words, for each $$$k \leq i \leq n$$$ it's satisfied that $$$s_{i} = s_{i - k}$$$. Find out the non-negative remainder of division of $$$\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i}$$$ by $$$10^{9} + 9$$$. Note that the modulo is unusual!
The first line contains four integers $$$n, a, b$$$ and $$$k$$$ $$$(1 \leq n \leq 10^{9}, 1 \leq a, b \leq 10^{9}, 1 \leq k \leq 10^{5})$$$. The second line contains a sequence of length $$$k$$$ consisting of characters '+' and '-'. If the $$$i$$$-th character (0-indexed) is '+', then $$$s_{i} = 1$$$, otherwise $$$s_{i} = -1$$$. Note that only the first $$$k$$$ members of the sequence are given, the rest can be obtained using the periodicity property.
Output a single integer — value of given expression modulo $$$10^{9} + 9$$$.
null
In the first example: $$$(\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i})$$$ = $$$2^{2} 3^{0} - 2^{1} 3^{1} + 2^{0} 3^{2}$$$ = 7 In the second example: $$$(\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i}) = -1^{4} 5^{0} - 1^{3} 5^{1} - 1^{2} 5^{2} - 1^{1} 5^{3} - 1^{0} 5^{4} = -781 \equiv 999999228 \pmod{10^{9} + 9}$$$.
[{"input": "2 2 3 3\n+-+", "output": "7"}, {"input": "4 1 5 1\n-", "output": "999999228"}]
1,800
["math", "number theory"]
71
[{"input": "2 2 3 3\r\n+-+\r\n", "output": "7\r\n"}, {"input": "4 1 5 1\r\n-\r\n", "output": "999999228\r\n"}, {"input": "1 1 4 2\r\n-+\r\n", "output": "3\r\n"}, {"input": "3 1 4 4\r\n+--+\r\n", "output": "45\r\n"}, {"input": "5 1 1 6\r\n++---+\r\n", "output": "0\r\n"}, {"input": "5 2 2 6\r\n+--++-\r\n", "output": "0\r\n"}, {"input": "686653196 115381398 884618610 3\r\n+-+\r\n", "output": "542231211\r\n"}, {"input": "608663287 430477711 172252358 8\r\n-+--+-+-\r\n", "output": "594681696\r\n"}, {"input": "904132655 827386249 118827660 334\r\n+++-+++++--+++----+-+-+-+-+--+-+---++--++--++--+-+-+++-+++--+-+-+----+-+-++++-----+--++++------+++-+-+-++-++++++++-+-++-+++--+--++------+--+-+++--++--+---++-++-+-+-++---++-++--+-+-++-+------+-+----+++-+++--+-+-+--+--+--+------+--+---+--+-++--+++---+-+-++--------+-++--++-+-+-+-+-+-+--+-++++-+++--+--++----+--+-++-++--+--+-+-++-+-++++-\r\n", "output": "188208979\r\n"}, {"input": "234179195 430477711 115381398 12\r\n++++-+-+-+++\r\n", "output": "549793323\r\n"}, {"input": "75952547 967294208 907708706 252\r\n++--++--+++-+-+--++--++++++---+++-++-+-----++++--++-+-++------+-+-+-++-+-+-++++------++---+-++++---+-+-++++--++++++--+-+++-++--+--+---++++---+-+++-+++--+-+--+++++---+--++-++++--++++-+-++-+++-++-----+-+++++----++--+++-+-+++++-+--++-++-+--+-++++--+-+-+-+\r\n", "output": "605712499\r\n"}, {"input": "74709071 801809249 753674746 18\r\n++++++-+-+---+-+--\r\n", "output": "13414893\r\n"}, {"input": "743329 973758 92942 82\r\n++----+-++++----+--+++---+--++++-+-+---+++++--+--+++++++--++-+++----+--+++++-+--+-\r\n", "output": "299311566\r\n"}, {"input": "18111 291387 518587 2\r\n++\r\n", "output": "724471355\r\n"}, {"input": "996144 218286 837447 1\r\n-\r\n", "output": "549104837\r\n"}, {"input": "179358 828426 548710 67\r\n++++---+--++----+-+-++++----+--+---+------++-+-++++--+----+---+-+--\r\n", "output": "759716474\r\n"}, {"input": "397521 174985 279760 1\r\n+\r\n", "output": "25679493\r\n"}, {"input": "613632 812232 482342 1\r\n-\r\n", "output": "891965141\r\n"}, {"input": "936810 183454 647048 1\r\n+\r\n", "output": "523548992\r\n"}, {"input": "231531 250371 921383 28\r\n++-+------+--+--++++--+-+++-\r\n", "output": "134450934\r\n"}, {"input": "947301 87242 360762 97\r\n--+++--+++-++--++-++--++--+++---+++--++++--+++++--+-++-++-----+-++-+--++-----+-++-+--++-++-+-----\r\n", "output": "405016159\r\n"}, {"input": "425583346 814209084 570987274 1\r\n+\r\n", "output": "63271171\r\n"}, {"input": "354062556 688076879 786825319 1\r\n+\r\n", "output": "545304776\r\n"}, {"input": "206671954 13571766 192250278 1\r\n+\r\n", "output": "717117421\r\n"}, {"input": "23047921 621656196 160244047 1\r\n-\r\n", "output": "101533009\r\n"}, {"input": "806038018 740585177 987616107 293\r\n-+++++--++++---++-+--+-+---+-++++--+--+++--++---++++++++--+++++-+-++-+--+----+--+++-+-++-+++-+-+-+----------++-+-+++++++-+-+-+-++---+++-+-+-------+-+-++--++-++-++-++-+---+--++-++--+++--+++-+-+----++--+-+-++-+---+---+-+-+++------+-+++-+---++-+--+++----+++++---++-++--+----+++-+--+++-+------+-++\r\n", "output": "441468166\r\n"}, {"input": "262060935 184120408 148332034 148\r\n+--+-------+-+-+--++-+++--++-+-++++++--++-+++-+++--+-------+-+--+++-+-+-+---++-++-+-++---+--+-+-+--+------+++--+--+-+-+---+---+-+-++++---+++--+++---\r\n", "output": "700325386\r\n"}, {"input": "919350941 654611542 217223605 186\r\n++-++-+++++-+++--+---+++++++-++-+----+-++--+-++--++--+++-+++---+--+--++-+-+++-+-+++-++---+--+++-+-+++--+-+-------+-++------++---+-+---++-++-++---+-+--+-+--+++++---+--+--++++-++-++--+--++\r\n", "output": "116291420\r\n"}, {"input": "289455627 906207104 512692624 154\r\n-------++--+++---++-++------++----------+--+++-+-+++---+---+++--++++++--+-+-+--+---+-+-++-++--+-++--++++---+-+---+-----+--+-+---------+++-++---++-+-+-----\r\n", "output": "48198216\r\n"}, {"input": "258833760 515657142 791267045 1\r\n-\r\n", "output": "935800888\r\n"}, {"input": "691617927 66917103 843055237 8\r\n--+++---\r\n", "output": "147768186\r\n"}, {"input": "379582849 362892355 986900829 50\r\n++-++---+-+++++--++++--+--++--++-----+------++--+-\r\n", "output": "927469713\r\n"}, {"input": "176799169 363368399 841293419 1\r\n+\r\n", "output": "746494802\r\n"}, {"input": "144808247 203038656 166324035 4\r\n-+-+\r\n", "output": "909066471\r\n"}, {"input": "477607531 177367565 20080950 2\r\n++\r\n", "output": "928662830\r\n"}, {"input": "682074525 289438443 917164266 1\r\n+\r\n", "output": "28048785\r\n"}, {"input": "938449224 59852396 219719125 1\r\n-\r\n", "output": "648647459\r\n"}, {"input": "395171426 872478622 193568600 147\r\n+---++---+-+--+++++--+---+-++++-+-++---++++--+--+-+-++-+-++--------++---+++-+---++---+---+-+--+-++++-+++-+-+-++-+--+++-++-+-+-+-++++++-+---+---++--\r\n", "output": "460881399\r\n"}, {"input": "403493428 317461491 556701240 1\r\n-\r\n", "output": "936516261\r\n"}, {"input": "917751169 330191895 532837377 70\r\n-+-+++++++--++---++-+++++-+++-----+-+++---+--+-+-++-++-+-+-++-++-+----\r\n", "output": "908035409\r\n"}, {"input": "252089413 552678586 938424519 1\r\n-\r\n", "output": "627032736\r\n"}, {"input": "649316142 320010793 200197645 1\r\n-\r\n", "output": "323650777\r\n"}, {"input": "116399299 784781190 299072480 5\r\n++++-\r\n", "output": "754650814\r\n"}]
false
stdio
null
true
532/E
533
E
PyPy 3-64
TESTS
18
62
0
150242670
n = int(input()) a = input() b = input() place = [] for i in range(n): if a[i] != b[i]: place.append(i) if len(place) == 1: print(2) elif len(place) == 2: if (place[1] - place[0]) > 1: print(0) else: print(1) elif len(place) == 0: print(n + 1) else: print(0)
89
92
3,584,000
146626470
import math from sys import stdin, stdout test = 1 #test = int(input()) for _ in range(test): n = int(input()) s = input() t = input() #longest common subsequence = n-1 i = 0 while s[i] == t[i]: i+=1 j = n-1 while s[j] == t[j]: j-=1 forward = True tempi = i tempj = j-1 while tempi < j: if s[tempi] == t[tempi+1]: tempi+=1 else: forward = False break backward = True while tempj >= i: if s[tempj+1] == t[tempj]: tempj-=1 else: backward = False break print(forward+backward)
VK Cup 2015 - Round 2
CF
2,015
2
256
Correcting Mistakes
Analyzing the mistakes people make while typing search queries is a complex and an interesting work. As there is no guaranteed way to determine what the user originally meant by typing some query, we have to use different sorts of heuristics. Polycarp needed to write a code that could, given two words, check whether they could have been obtained from the same word as a result of typos. Polycarpus suggested that the most common typo is skipping exactly one letter as you type a word. Implement a program that can, given two distinct words S and T of the same length n determine how many words W of length n + 1 are there with such property that you can transform W into both S, and T by deleting exactly one character. Words S and T consist of lowercase English letters. Word W also should consist of lowercase English letters.
The first line contains integer n (1 ≤ n ≤ 100 000) — the length of words S and T. The second line contains word S. The third line contains word T. Words S and T consist of lowercase English letters. It is guaranteed that S and T are distinct words.
Print a single integer — the number of distinct words W that can be transformed to S and T due to a typo.
null
In the first sample test the two given words could be obtained only from word "treading" (the deleted letters are marked in bold). In the second sample test the two given words couldn't be obtained from the same word by removing one letter. In the third sample test the two given words could be obtained from either word "tory" or word "troy".
[{"input": "7\nreading\ntrading", "output": "1"}, {"input": "5\nsweet\nsheep", "output": "0"}, {"input": "3\ntoy\ntry", "output": "2"}]
1,800
[]
89
[{"input": "7\r\nreading\r\ntrading\r\n", "output": "1\n"}, {"input": "5\r\nsweet\r\nsheep\r\n", "output": "0\n"}, {"input": "3\r\ntoy\r\ntry\r\n", "output": "2\n"}, {"input": "5\r\nspare\r\nspars\r\n", "output": "2\n"}, {"input": "1\r\na\r\nb\r\n", "output": "2\n"}, {"input": "1\r\nz\r\ny\r\n", "output": "2\n"}, {"input": "2\r\nab\r\nac\r\n", "output": "2\n"}, {"input": "2\r\nba\r\nca\r\n", "output": "2\n"}, {"input": "2\r\nac\r\ncb\r\n", "output": "1\n"}, {"input": "100\r\neebdeddddbecdbddaaecbbaccbecdeacedddcaddcdebedbabbceeeadecadbbeaecdaeabbceacbdbdbbdacebbbccdcbbeedbe\r\ndacdeebebeaeaacdeedadbcbaedcbddddddedacbabeddebaaebbdcebebaaccbaeccbecdbcbceadaaecadecbadbcddcdabecd\r\n", "output": "0\n"}, {"input": "250\r\niiffiehchidfgigdbcciahdehjjfacbbaaadagaibjjcehjcbjdhaadebaejiicgidbhajfbfejcdicgfbcchgbahfccbefdcddbjjhejigiafhdjbiiehadfficicbebeeegcebideijidbgdecffeaegjfjbbcfiabfbaiddbjgidebdiccfcgfbcbbfhaejaibeicghecchjbiaceaibfgibhgcfgifiedcbhhfadhccfdhejeggcah\r\njbadcfjffcfabbecfabgcafgfcgfeffjjhhdaajjgcbgbechhiadfahjidcdiefhbabhjhjijghghcgghcefhidhdgficiffdjgfdahcaicidfghiedgihbbjgicjeiacihdihfhadjhccddhigiibafiafficegaiehabafiiecbjcbfhdbeaebigaijehhdbfeehbcahaggbbdjcdbgbiajgeigdeabdbddbgcgjibfdgjghhdidjdhh\r\n", "output": "0\n"}, {"input": "100\r\nabababbbababbababaaabbbbaaaabbabbabbabababbbaaaabbababbbbababbabbbaaababababbbaaaabbbabbababbbbbbaba\r\nabababbbababbababaaabbbbaaaabbabbabbabababbbaaaabaababbbbababbabbbaaababababbbaaaabbbabbababbbbbbaba\r\n", "output": "2\n"}, {"input": "100\r\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n", "output": "2\n"}, {"input": "100\r\naaaaaaaaaaaaaaaaaaaaaalaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\naaaaaaaaaaaaaaaaaaaaaakaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n", "output": "2\n"}, {"input": "100\r\ndwtsrrtztfuibkrpwbxjrcxsonrwoydkmbhxrghekvusiyzqkyvulrvtfxmvrphpzpmazizogfbyauxtjfesocssnxvjjdedomlz\r\ndwtsrrtztfuibkrpwbxjrcxsonrwoydkmbhxrghekvusiyzqkyvulrvtfxmvrphpzpzazizogfbyauxtjfesocssnxvjjdedomlz\r\n", "output": "2\n"}, {"input": "100\r\naaabaabbbababbbaabbbbbaaababbabbaaabbabaabbabbabbbbbabbaaabbbbbbbbbbbbbbbababaaababbaaabeabbabaabbab\r\naaabaabbbababbbaabbbbbaaababbabbaaabbabaabbabbabbbbbabbaaabbbbbbbbbbbbbbbababaaababbaaabtabbabaabbab\r\n", "output": "2\n"}, {"input": "100\r\naaaabaaaaabbaababaaabaababaabbbaabaaabbbaaababbabaabbabababbaaabaabababbbababbbabbaaaabbbbbbbaaababa\r\naaaabaaaaabbaababaaabaababaabbbaabaaabbbaaaabbbabaabbabababbaaabaabababbbababbbabbaaaabbbbbbbaaababa\r\n", "output": "2\n"}, {"input": "100\r\neebdeddddbecdbddaaecbbaccbecdeacedddcaddcdebedbabbceeeadecadbbeaecdaeabbceacbdbdbbdacebbbccdcbbeedbe\r\needbeddddbecdbddaaecbbaccbecdeacedddcaddcdebedbabbceeeadecadbbeaecdaeabbceacbdbdbbdacebbbccdcbbeedbe\r\n", "output": "2\n"}, {"input": "100\r\nxjywrmrwqaytezhtqmcnrrjomslvcmevncvzeddnvqgkbusnbzrppdsuzsmcobmnslpvosunavayvdbxhtavvwodorwijxfjjlat\r\nxjywrmrwqaytezhtqmcrnrjomslvcmevncvzeddnvqgkbusnbzrppdsuzsmcobmnslpvosunavayvdbxhtavvwodorwijxfjjlat\r\n", "output": "2\n"}, {"input": "4\r\nbbca\r\nabab\r\n", "output": "0\n"}, {"input": "4\r\nabcb\r\nccac\r\n", "output": "0\n"}, {"input": "4\r\ncaaa\r\nabab\r\n", "output": "0\n"}, {"input": "4\r\nacca\r\nbabb\r\n", "output": "0\n"}, {"input": "4\r\nccba\r\nbabb\r\n", "output": "0\n"}, {"input": "4\r\nbcca\r\ncbaa\r\n", "output": "0\n"}, {"input": "4\r\naaca\r\ncaab\r\n", "output": "0\n"}, {"input": "4\r\nbaab\r\nbcbc\r\n", "output": "0\n"}, {"input": "4\r\nabba\r\ncaca\r\n", "output": "0\n"}, {"input": "4\r\nbcbb\r\nccac\r\n", "output": "0\n"}, {"input": "4\r\ncbba\r\nabba\r\n", "output": "2\n"}, {"input": "4\r\nbaca\r\nccbc\r\n", "output": "0\n"}, {"input": "4\r\ncabc\r\naacc\r\n", "output": "0\n"}, {"input": "4\r\nbbab\r\ncbaa\r\n", "output": "0\n"}, {"input": "4\r\nabcc\r\nbcab\r\n", "output": "0\n"}, {"input": "4\r\nbaaa\r\nbbbc\r\n", "output": "0\n"}, {"input": "4\r\naabc\r\naacb\r\n", "output": "2\n"}, {"input": "4\r\nccbb\r\nbbcb\r\n", "output": "0\n"}, {"input": "4\r\nbaba\r\naccc\r\n", "output": "0\n"}, {"input": "4\r\nbbbc\r\nbbab\r\n", "output": "1\n"}, {"input": "2\r\nab\r\nba\r\n", "output": "2\n"}, {"input": "5\r\ncabac\r\ncbabc\r\n", "output": "2\n"}, {"input": "3\r\naba\r\nbab\r\n", "output": "2\n"}, {"input": "5\r\nabxxx\r\nbayyy\r\n", "output": "0\n"}, {"input": "4\r\nxaxa\r\naxax\r\n", "output": "2\n"}, {"input": "5\r\nababa\r\nbabab\r\n", "output": "2\n"}, {"input": "5\r\nbabab\r\nababa\r\n", "output": "2\n"}, {"input": "154\r\nwqpewhyutqnhaewqpewhywqpewhyutqnhaeutqnhaeutqnhaewqpewhyutqnhaewqpewhywqpewhyutqnhaeutqnhaeutqnhaeutqnhaewqpewhyutqnhaewqpewhywqpewhywqpewhywqpewhyutqnhae\r\nutqnhaeutqnhaeutqnhaewqpewhywqpewhyutqnhaewqpewhyutqnhaewqpewhywqpewhyutqnhaeutqnhaeutqnhaewqpewhyutqnhaewqpewhywqpewhywqpewhyutqnhaewqpewhyutqnhaewqpewhy\r\n", "output": "0\n"}, {"input": "7\r\ntrading\r\nrtading\r\n", "output": "2\n"}, {"input": "5\r\nxabax\r\nxbabx\r\n", "output": "2\n"}, {"input": "3\r\nabc\r\nacb\r\n", "output": "2\n"}, {"input": "4\r\nabab\r\nbaba\r\n", "output": "2\n"}, {"input": "3\r\naab\r\naba\r\n", "output": "2\n"}, {"input": "2\r\ner\r\nre\r\n", "output": "2\n"}, {"input": "5\r\ntabat\r\ntbaat\r\n", "output": "2\n"}]
false
stdio
null
true
532/E
533
E
PyPy 3
TESTS
31
155
2,560,000
56361634
n = int(input()) s1 = input() s2 = input() for i in range(n): if s1[i] != s2[i]: s1, s2 = s1[i:], s2[i:] break for i in range(len(s1) - 1, -1, -1): if s1[i] != s2[i]: s1, s2 = s1[:i + 1], s2[:i + 1] break if len(s1) == 1: print(2) elif s1[1:] == s2[:-1] or s1[:-1] == s2[1:]: print(1) else: print(0)
89
93
2,560,000
201712011
n = int(input()) s1 = input() s2 = input() front = 0 rear = n-1 while s1[front]==s2[front]: front += 1 while s1[rear]==s2[rear]: rear -= 1 ans = 0 if s1[front+1:rear+1]==s2[front:rear]: ans += 1 if s1[front:rear]==s2[front+1:rear+1]: ans += 1 print(ans)
VK Cup 2015 - Round 2
CF
2,015
2
256
Correcting Mistakes
Analyzing the mistakes people make while typing search queries is a complex and an interesting work. As there is no guaranteed way to determine what the user originally meant by typing some query, we have to use different sorts of heuristics. Polycarp needed to write a code that could, given two words, check whether they could have been obtained from the same word as a result of typos. Polycarpus suggested that the most common typo is skipping exactly one letter as you type a word. Implement a program that can, given two distinct words S and T of the same length n determine how many words W of length n + 1 are there with such property that you can transform W into both S, and T by deleting exactly one character. Words S and T consist of lowercase English letters. Word W also should consist of lowercase English letters.
The first line contains integer n (1 ≤ n ≤ 100 000) — the length of words S and T. The second line contains word S. The third line contains word T. Words S and T consist of lowercase English letters. It is guaranteed that S and T are distinct words.
Print a single integer — the number of distinct words W that can be transformed to S and T due to a typo.
null
In the first sample test the two given words could be obtained only from word "treading" (the deleted letters are marked in bold). In the second sample test the two given words couldn't be obtained from the same word by removing one letter. In the third sample test the two given words could be obtained from either word "tory" or word "troy".
[{"input": "7\nreading\ntrading", "output": "1"}, {"input": "5\nsweet\nsheep", "output": "0"}, {"input": "3\ntoy\ntry", "output": "2"}]
1,800
[]
89
[{"input": "7\r\nreading\r\ntrading\r\n", "output": "1\n"}, {"input": "5\r\nsweet\r\nsheep\r\n", "output": "0\n"}, {"input": "3\r\ntoy\r\ntry\r\n", "output": "2\n"}, {"input": "5\r\nspare\r\nspars\r\n", "output": "2\n"}, {"input": "1\r\na\r\nb\r\n", "output": "2\n"}, {"input": "1\r\nz\r\ny\r\n", "output": "2\n"}, {"input": "2\r\nab\r\nac\r\n", "output": "2\n"}, {"input": "2\r\nba\r\nca\r\n", "output": "2\n"}, {"input": "2\r\nac\r\ncb\r\n", "output": "1\n"}, {"input": "100\r\neebdeddddbecdbddaaecbbaccbecdeacedddcaddcdebedbabbceeeadecadbbeaecdaeabbceacbdbdbbdacebbbccdcbbeedbe\r\ndacdeebebeaeaacdeedadbcbaedcbddddddedacbabeddebaaebbdcebebaaccbaeccbecdbcbceadaaecadecbadbcddcdabecd\r\n", "output": "0\n"}, {"input": "250\r\niiffiehchidfgigdbcciahdehjjfacbbaaadagaibjjcehjcbjdhaadebaejiicgidbhajfbfejcdicgfbcchgbahfccbefdcddbjjhejigiafhdjbiiehadfficicbebeeegcebideijidbgdecffeaegjfjbbcfiabfbaiddbjgidebdiccfcgfbcbbfhaejaibeicghecchjbiaceaibfgibhgcfgifiedcbhhfadhccfdhejeggcah\r\njbadcfjffcfabbecfabgcafgfcgfeffjjhhdaajjgcbgbechhiadfahjidcdiefhbabhjhjijghghcgghcefhidhdgficiffdjgfdahcaicidfghiedgihbbjgicjeiacihdihfhadjhccddhigiibafiafficegaiehabafiiecbjcbfhdbeaebigaijehhdbfeehbcahaggbbdjcdbgbiajgeigdeabdbddbgcgjibfdgjghhdidjdhh\r\n", "output": "0\n"}, {"input": "100\r\nabababbbababbababaaabbbbaaaabbabbabbabababbbaaaabbababbbbababbabbbaaababababbbaaaabbbabbababbbbbbaba\r\nabababbbababbababaaabbbbaaaabbabbabbabababbbaaaabaababbbbababbabbbaaababababbbaaaabbbabbababbbbbbaba\r\n", "output": "2\n"}, {"input": "100\r\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n", "output": "2\n"}, {"input": "100\r\naaaaaaaaaaaaaaaaaaaaaalaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\naaaaaaaaaaaaaaaaaaaaaakaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n", "output": "2\n"}, {"input": "100\r\ndwtsrrtztfuibkrpwbxjrcxsonrwoydkmbhxrghekvusiyzqkyvulrvtfxmvrphpzpmazizogfbyauxtjfesocssnxvjjdedomlz\r\ndwtsrrtztfuibkrpwbxjrcxsonrwoydkmbhxrghekvusiyzqkyvulrvtfxmvrphpzpzazizogfbyauxtjfesocssnxvjjdedomlz\r\n", "output": "2\n"}, {"input": "100\r\naaabaabbbababbbaabbbbbaaababbabbaaabbabaabbabbabbbbbabbaaabbbbbbbbbbbbbbbababaaababbaaabeabbabaabbab\r\naaabaabbbababbbaabbbbbaaababbabbaaabbabaabbabbabbbbbabbaaabbbbbbbbbbbbbbbababaaababbaaabtabbabaabbab\r\n", "output": "2\n"}, {"input": "100\r\naaaabaaaaabbaababaaabaababaabbbaabaaabbbaaababbabaabbabababbaaabaabababbbababbbabbaaaabbbbbbbaaababa\r\naaaabaaaaabbaababaaabaababaabbbaabaaabbbaaaabbbabaabbabababbaaabaabababbbababbbabbaaaabbbbbbbaaababa\r\n", "output": "2\n"}, {"input": "100\r\neebdeddddbecdbddaaecbbaccbecdeacedddcaddcdebedbabbceeeadecadbbeaecdaeabbceacbdbdbbdacebbbccdcbbeedbe\r\needbeddddbecdbddaaecbbaccbecdeacedddcaddcdebedbabbceeeadecadbbeaecdaeabbceacbdbdbbdacebbbccdcbbeedbe\r\n", "output": "2\n"}, {"input": "100\r\nxjywrmrwqaytezhtqmcnrrjomslvcmevncvzeddnvqgkbusnbzrppdsuzsmcobmnslpvosunavayvdbxhtavvwodorwijxfjjlat\r\nxjywrmrwqaytezhtqmcrnrjomslvcmevncvzeddnvqgkbusnbzrppdsuzsmcobmnslpvosunavayvdbxhtavvwodorwijxfjjlat\r\n", "output": "2\n"}, {"input": "4\r\nbbca\r\nabab\r\n", "output": "0\n"}, {"input": "4\r\nabcb\r\nccac\r\n", "output": "0\n"}, {"input": "4\r\ncaaa\r\nabab\r\n", "output": "0\n"}, {"input": "4\r\nacca\r\nbabb\r\n", "output": "0\n"}, {"input": "4\r\nccba\r\nbabb\r\n", "output": "0\n"}, {"input": "4\r\nbcca\r\ncbaa\r\n", "output": "0\n"}, {"input": "4\r\naaca\r\ncaab\r\n", "output": "0\n"}, {"input": "4\r\nbaab\r\nbcbc\r\n", "output": "0\n"}, {"input": "4\r\nabba\r\ncaca\r\n", "output": "0\n"}, {"input": "4\r\nbcbb\r\nccac\r\n", "output": "0\n"}, {"input": "4\r\ncbba\r\nabba\r\n", "output": "2\n"}, {"input": "4\r\nbaca\r\nccbc\r\n", "output": "0\n"}, {"input": "4\r\ncabc\r\naacc\r\n", "output": "0\n"}, {"input": "4\r\nbbab\r\ncbaa\r\n", "output": "0\n"}, {"input": "4\r\nabcc\r\nbcab\r\n", "output": "0\n"}, {"input": "4\r\nbaaa\r\nbbbc\r\n", "output": "0\n"}, {"input": "4\r\naabc\r\naacb\r\n", "output": "2\n"}, {"input": "4\r\nccbb\r\nbbcb\r\n", "output": "0\n"}, {"input": "4\r\nbaba\r\naccc\r\n", "output": "0\n"}, {"input": "4\r\nbbbc\r\nbbab\r\n", "output": "1\n"}, {"input": "2\r\nab\r\nba\r\n", "output": "2\n"}, {"input": "5\r\ncabac\r\ncbabc\r\n", "output": "2\n"}, {"input": "3\r\naba\r\nbab\r\n", "output": "2\n"}, {"input": "5\r\nabxxx\r\nbayyy\r\n", "output": "0\n"}, {"input": "4\r\nxaxa\r\naxax\r\n", "output": "2\n"}, {"input": "5\r\nababa\r\nbabab\r\n", "output": "2\n"}, {"input": "5\r\nbabab\r\nababa\r\n", "output": "2\n"}, {"input": "154\r\nwqpewhyutqnhaewqpewhywqpewhyutqnhaeutqnhaeutqnhaewqpewhyutqnhaewqpewhywqpewhyutqnhaeutqnhaeutqnhaeutqnhaewqpewhyutqnhaewqpewhywqpewhywqpewhywqpewhyutqnhae\r\nutqnhaeutqnhaeutqnhaewqpewhywqpewhyutqnhaewqpewhyutqnhaewqpewhywqpewhyutqnhaeutqnhaeutqnhaewqpewhyutqnhaewqpewhywqpewhywqpewhyutqnhaewqpewhyutqnhaewqpewhy\r\n", "output": "0\n"}, {"input": "7\r\ntrading\r\nrtading\r\n", "output": "2\n"}, {"input": "5\r\nxabax\r\nxbabx\r\n", "output": "2\n"}, {"input": "3\r\nabc\r\nacb\r\n", "output": "2\n"}, {"input": "4\r\nabab\r\nbaba\r\n", "output": "2\n"}, {"input": "3\r\naab\r\naba\r\n", "output": "2\n"}, {"input": "2\r\ner\r\nre\r\n", "output": "2\n"}, {"input": "5\r\ntabat\r\ntbaat\r\n", "output": "2\n"}]
false
stdio
null
true
963/A
963
A
PyPy 3
TESTS
8
342
9,420,800
80734140
def binPow(a,n): if n==0: return 1 result=binPow(a,n//2) if n%2==1: return ((result%mod)*(result%mod)*(a%mod))%mod else: return ((result%mod)*(result%mod))%mod mod=10**9+9 n,a,b,k=map(int,input().split()) s=input() result=0 A=(binPow(b,n+1)-binPow(a,n+1))%mod B=((binPow(b,k)-binPow(a,k))*binPow(a,n+1-k))%mod A=(A*binPow(B,mod-2))%mod for i in range(k): p=(binPow(b,i)*binPow(a,n-i))%mod if s[i]=="+": result+=p else: result -= p print((A*result)%mod)
71
140
22,323,200
122326208
import sys input=sys.stdin.readline n,a,b,k=map(int,input().split()) s=input().rstrip() mod=10**9+9 pa=pow(a,n,mod) inv_a=pow(a,mod-2,mod) pb=1 xk=0 for i in range(k): if s[i]=="+": xk+=pa*pb%mod xk%=mod else: xk-=pa*pb%mod xk%=mod pa*=inv_a pa%=mod pb*=b pb%=mod y=(1-pow(inv_a*b%mod,n+1,mod))%mod zz=pow(inv_a*b%mod,k,mod) if zz!=1: z=(1-zz)%mod z=pow(z,mod-2,mod) print(xk*y*z%mod) else: z=(n+1)//k print(z*xk%mod)
Tinkoff Internship Warmup Round 2018 and Codeforces Round 475 (Div. 1)
CF
2,018
1
256
Alternating Sum
You are given two integers $$$a$$$ and $$$b$$$. Moreover, you are given a sequence $$$s_0, s_1, \dots, s_{n}$$$. All values in $$$s$$$ are integers $$$1$$$ or $$$-1$$$. It's known that sequence is $$$k$$$-periodic and $$$k$$$ divides $$$n+1$$$. In other words, for each $$$k \leq i \leq n$$$ it's satisfied that $$$s_{i} = s_{i - k}$$$. Find out the non-negative remainder of division of $$$\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i}$$$ by $$$10^{9} + 9$$$. Note that the modulo is unusual!
The first line contains four integers $$$n, a, b$$$ and $$$k$$$ $$$(1 \leq n \leq 10^{9}, 1 \leq a, b \leq 10^{9}, 1 \leq k \leq 10^{5})$$$. The second line contains a sequence of length $$$k$$$ consisting of characters '+' and '-'. If the $$$i$$$-th character (0-indexed) is '+', then $$$s_{i} = 1$$$, otherwise $$$s_{i} = -1$$$. Note that only the first $$$k$$$ members of the sequence are given, the rest can be obtained using the periodicity property.
Output a single integer — value of given expression modulo $$$10^{9} + 9$$$.
null
In the first example: $$$(\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i})$$$ = $$$2^{2} 3^{0} - 2^{1} 3^{1} + 2^{0} 3^{2}$$$ = 7 In the second example: $$$(\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i}) = -1^{4} 5^{0} - 1^{3} 5^{1} - 1^{2} 5^{2} - 1^{1} 5^{3} - 1^{0} 5^{4} = -781 \equiv 999999228 \pmod{10^{9} + 9}$$$.
[{"input": "2 2 3 3\n+-+", "output": "7"}, {"input": "4 1 5 1\n-", "output": "999999228"}]
1,800
["math", "number theory"]
71
[{"input": "2 2 3 3\r\n+-+\r\n", "output": "7\r\n"}, {"input": "4 1 5 1\r\n-\r\n", "output": "999999228\r\n"}, {"input": "1 1 4 2\r\n-+\r\n", "output": "3\r\n"}, {"input": "3 1 4 4\r\n+--+\r\n", "output": "45\r\n"}, {"input": "5 1 1 6\r\n++---+\r\n", "output": "0\r\n"}, {"input": "5 2 2 6\r\n+--++-\r\n", "output": "0\r\n"}, {"input": "686653196 115381398 884618610 3\r\n+-+\r\n", "output": "542231211\r\n"}, {"input": "608663287 430477711 172252358 8\r\n-+--+-+-\r\n", "output": "594681696\r\n"}, {"input": "904132655 827386249 118827660 334\r\n+++-+++++--+++----+-+-+-+-+--+-+---++--++--++--+-+-+++-+++--+-+-+----+-+-++++-----+--++++------+++-+-+-++-++++++++-+-++-+++--+--++------+--+-+++--++--+---++-++-+-+-++---++-++--+-+-++-+------+-+----+++-+++--+-+-+--+--+--+------+--+---+--+-++--+++---+-+-++--------+-++--++-+-+-+-+-+-+--+-++++-+++--+--++----+--+-++-++--+--+-+-++-+-++++-\r\n", "output": "188208979\r\n"}, {"input": "234179195 430477711 115381398 12\r\n++++-+-+-+++\r\n", "output": "549793323\r\n"}, {"input": "75952547 967294208 907708706 252\r\n++--++--+++-+-+--++--++++++---+++-++-+-----++++--++-+-++------+-+-+-++-+-+-++++------++---+-++++---+-+-++++--++++++--+-+++-++--+--+---++++---+-+++-+++--+-+--+++++---+--++-++++--++++-+-++-+++-++-----+-+++++----++--+++-+-+++++-+--++-++-+--+-++++--+-+-+-+\r\n", "output": "605712499\r\n"}, {"input": "74709071 801809249 753674746 18\r\n++++++-+-+---+-+--\r\n", "output": "13414893\r\n"}, {"input": "743329 973758 92942 82\r\n++----+-++++----+--+++---+--++++-+-+---+++++--+--+++++++--++-+++----+--+++++-+--+-\r\n", "output": "299311566\r\n"}, {"input": "18111 291387 518587 2\r\n++\r\n", "output": "724471355\r\n"}, {"input": "996144 218286 837447 1\r\n-\r\n", "output": "549104837\r\n"}, {"input": "179358 828426 548710 67\r\n++++---+--++----+-+-++++----+--+---+------++-+-++++--+----+---+-+--\r\n", "output": "759716474\r\n"}, {"input": "397521 174985 279760 1\r\n+\r\n", "output": "25679493\r\n"}, {"input": "613632 812232 482342 1\r\n-\r\n", "output": "891965141\r\n"}, {"input": "936810 183454 647048 1\r\n+\r\n", "output": "523548992\r\n"}, {"input": "231531 250371 921383 28\r\n++-+------+--+--++++--+-+++-\r\n", "output": "134450934\r\n"}, {"input": "947301 87242 360762 97\r\n--+++--+++-++--++-++--++--+++---+++--++++--+++++--+-++-++-----+-++-+--++-----+-++-+--++-++-+-----\r\n", "output": "405016159\r\n"}, {"input": "425583346 814209084 570987274 1\r\n+\r\n", "output": "63271171\r\n"}, {"input": "354062556 688076879 786825319 1\r\n+\r\n", "output": "545304776\r\n"}, {"input": "206671954 13571766 192250278 1\r\n+\r\n", "output": "717117421\r\n"}, {"input": "23047921 621656196 160244047 1\r\n-\r\n", "output": "101533009\r\n"}, {"input": "806038018 740585177 987616107 293\r\n-+++++--++++---++-+--+-+---+-++++--+--+++--++---++++++++--+++++-+-++-+--+----+--+++-+-++-+++-+-+-+----------++-+-+++++++-+-+-+-++---+++-+-+-------+-+-++--++-++-++-++-+---+--++-++--+++--+++-+-+----++--+-+-++-+---+---+-+-+++------+-+++-+---++-+--+++----+++++---++-++--+----+++-+--+++-+------+-++\r\n", "output": "441468166\r\n"}, {"input": "262060935 184120408 148332034 148\r\n+--+-------+-+-+--++-+++--++-+-++++++--++-+++-+++--+-------+-+--+++-+-+-+---++-++-+-++---+--+-+-+--+------+++--+--+-+-+---+---+-+-++++---+++--+++---\r\n", "output": "700325386\r\n"}, {"input": "919350941 654611542 217223605 186\r\n++-++-+++++-+++--+---+++++++-++-+----+-++--+-++--++--+++-+++---+--+--++-+-+++-+-+++-++---+--+++-+-+++--+-+-------+-++------++---+-+---++-++-++---+-+--+-+--+++++---+--+--++++-++-++--+--++\r\n", "output": "116291420\r\n"}, {"input": "289455627 906207104 512692624 154\r\n-------++--+++---++-++------++----------+--+++-+-+++---+---+++--++++++--+-+-+--+---+-+-++-++--+-++--++++---+-+---+-----+--+-+---------+++-++---++-+-+-----\r\n", "output": "48198216\r\n"}, {"input": "258833760 515657142 791267045 1\r\n-\r\n", "output": "935800888\r\n"}, {"input": "691617927 66917103 843055237 8\r\n--+++---\r\n", "output": "147768186\r\n"}, {"input": "379582849 362892355 986900829 50\r\n++-++---+-+++++--++++--+--++--++-----+------++--+-\r\n", "output": "927469713\r\n"}, {"input": "176799169 363368399 841293419 1\r\n+\r\n", "output": "746494802\r\n"}, {"input": "144808247 203038656 166324035 4\r\n-+-+\r\n", "output": "909066471\r\n"}, {"input": "477607531 177367565 20080950 2\r\n++\r\n", "output": "928662830\r\n"}, {"input": "682074525 289438443 917164266 1\r\n+\r\n", "output": "28048785\r\n"}, {"input": "938449224 59852396 219719125 1\r\n-\r\n", "output": "648647459\r\n"}, {"input": "395171426 872478622 193568600 147\r\n+---++---+-+--+++++--+---+-++++-+-++---++++--+--+-+-++-+-++--------++---+++-+---++---+---+-+--+-++++-+++-+-+-++-+--+++-++-+-+-+-++++++-+---+---++--\r\n", "output": "460881399\r\n"}, {"input": "403493428 317461491 556701240 1\r\n-\r\n", "output": "936516261\r\n"}, {"input": "917751169 330191895 532837377 70\r\n-+-+++++++--++---++-+++++-+++-----+-+++---+--+-+-++-++-+-+-++-++-+----\r\n", "output": "908035409\r\n"}, {"input": "252089413 552678586 938424519 1\r\n-\r\n", "output": "627032736\r\n"}, {"input": "649316142 320010793 200197645 1\r\n-\r\n", "output": "323650777\r\n"}, {"input": "116399299 784781190 299072480 5\r\n++++-\r\n", "output": "754650814\r\n"}]
false
stdio
null
true
963/A
963
A
Python 3
PRETESTS
8
78
7,065,600
37404892
MOD = 1000000009 def Pow(base, n): res = 1 while n: if n&1: res = (res*base)%MOD base = (base*base)%MOD n >>= 1 return res n, a, b, k = map(int, input().split()) ans = 0 num = (n+1)//k if a == b: res = Pow(a, n)*num%MOD for i in input(): if i == '+': ans = (ans+res)%MOD else: ans = (ans-res)%MOD else: q = Pow(b, k)*Pow(Pow(a, k), MOD-2)%MOD rat = (1-Pow(q, num))%MOD*Pow((1-q)%MOD, MOD-2)%MOD cur = Pow(a, n)*rat%MOD _a = Pow(a, MOD-2) for i in input(): if i == '+': ans = (ans+cur)%MOD else: ans = (ans-cur)%MOD cur = cur*b%MOD*_a%MOD print(ans)
71
156
102,400
43394120
MOD = 1000000009 def AlternateSum(n,a,b,k,s): res = 0 inv = lambda x: pow(x, MOD-2, MOD) q = pow(b, k, MOD) * inv(pow(a, k, MOD)) % MOD max_pow = pow(a, n, MOD) c = b * inv(a) % MOD for i in range(k): if s[i] == '+': res += max_pow else: res -= max_pow res %= MOD max_pow = max_pow * c % MOD t = (n + 1) // k if q == 1: return t * res % MOD z = (pow(q, t, MOD) - 1) * inv(q-1) % MOD return z * res % MOD n, a, b, k = [int(x) for x in input().split()] s = input() print(AlternateSum(n, a, b, k, s))
Tinkoff Internship Warmup Round 2018 and Codeforces Round 475 (Div. 1)
CF
2,018
1
256
Alternating Sum
You are given two integers $$$a$$$ and $$$b$$$. Moreover, you are given a sequence $$$s_0, s_1, \dots, s_{n}$$$. All values in $$$s$$$ are integers $$$1$$$ or $$$-1$$$. It's known that sequence is $$$k$$$-periodic and $$$k$$$ divides $$$n+1$$$. In other words, for each $$$k \leq i \leq n$$$ it's satisfied that $$$s_{i} = s_{i - k}$$$. Find out the non-negative remainder of division of $$$\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i}$$$ by $$$10^{9} + 9$$$. Note that the modulo is unusual!
The first line contains four integers $$$n, a, b$$$ and $$$k$$$ $$$(1 \leq n \leq 10^{9}, 1 \leq a, b \leq 10^{9}, 1 \leq k \leq 10^{5})$$$. The second line contains a sequence of length $$$k$$$ consisting of characters '+' and '-'. If the $$$i$$$-th character (0-indexed) is '+', then $$$s_{i} = 1$$$, otherwise $$$s_{i} = -1$$$. Note that only the first $$$k$$$ members of the sequence are given, the rest can be obtained using the periodicity property.
Output a single integer — value of given expression modulo $$$10^{9} + 9$$$.
null
In the first example: $$$(\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i})$$$ = $$$2^{2} 3^{0} - 2^{1} 3^{1} + 2^{0} 3^{2}$$$ = 7 In the second example: $$$(\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i}) = -1^{4} 5^{0} - 1^{3} 5^{1} - 1^{2} 5^{2} - 1^{1} 5^{3} - 1^{0} 5^{4} = -781 \equiv 999999228 \pmod{10^{9} + 9}$$$.
[{"input": "2 2 3 3\n+-+", "output": "7"}, {"input": "4 1 5 1\n-", "output": "999999228"}]
1,800
["math", "number theory"]
71
[{"input": "2 2 3 3\r\n+-+\r\n", "output": "7\r\n"}, {"input": "4 1 5 1\r\n-\r\n", "output": "999999228\r\n"}, {"input": "1 1 4 2\r\n-+\r\n", "output": "3\r\n"}, {"input": "3 1 4 4\r\n+--+\r\n", "output": "45\r\n"}, {"input": "5 1 1 6\r\n++---+\r\n", "output": "0\r\n"}, {"input": "5 2 2 6\r\n+--++-\r\n", "output": "0\r\n"}, {"input": "686653196 115381398 884618610 3\r\n+-+\r\n", "output": "542231211\r\n"}, {"input": "608663287 430477711 172252358 8\r\n-+--+-+-\r\n", "output": "594681696\r\n"}, {"input": "904132655 827386249 118827660 334\r\n+++-+++++--+++----+-+-+-+-+--+-+---++--++--++--+-+-+++-+++--+-+-+----+-+-++++-----+--++++------+++-+-+-++-++++++++-+-++-+++--+--++------+--+-+++--++--+---++-++-+-+-++---++-++--+-+-++-+------+-+----+++-+++--+-+-+--+--+--+------+--+---+--+-++--+++---+-+-++--------+-++--++-+-+-+-+-+-+--+-++++-+++--+--++----+--+-++-++--+--+-+-++-+-++++-\r\n", "output": "188208979\r\n"}, {"input": "234179195 430477711 115381398 12\r\n++++-+-+-+++\r\n", "output": "549793323\r\n"}, {"input": "75952547 967294208 907708706 252\r\n++--++--+++-+-+--++--++++++---+++-++-+-----++++--++-+-++------+-+-+-++-+-+-++++------++---+-++++---+-+-++++--++++++--+-+++-++--+--+---++++---+-+++-+++--+-+--+++++---+--++-++++--++++-+-++-+++-++-----+-+++++----++--+++-+-+++++-+--++-++-+--+-++++--+-+-+-+\r\n", "output": "605712499\r\n"}, {"input": "74709071 801809249 753674746 18\r\n++++++-+-+---+-+--\r\n", "output": "13414893\r\n"}, {"input": "743329 973758 92942 82\r\n++----+-++++----+--+++---+--++++-+-+---+++++--+--+++++++--++-+++----+--+++++-+--+-\r\n", "output": "299311566\r\n"}, {"input": "18111 291387 518587 2\r\n++\r\n", "output": "724471355\r\n"}, {"input": "996144 218286 837447 1\r\n-\r\n", "output": "549104837\r\n"}, {"input": "179358 828426 548710 67\r\n++++---+--++----+-+-++++----+--+---+------++-+-++++--+----+---+-+--\r\n", "output": "759716474\r\n"}, {"input": "397521 174985 279760 1\r\n+\r\n", "output": "25679493\r\n"}, {"input": "613632 812232 482342 1\r\n-\r\n", "output": "891965141\r\n"}, {"input": "936810 183454 647048 1\r\n+\r\n", "output": "523548992\r\n"}, {"input": "231531 250371 921383 28\r\n++-+------+--+--++++--+-+++-\r\n", "output": "134450934\r\n"}, {"input": "947301 87242 360762 97\r\n--+++--+++-++--++-++--++--+++---+++--++++--+++++--+-++-++-----+-++-+--++-----+-++-+--++-++-+-----\r\n", "output": "405016159\r\n"}, {"input": "425583346 814209084 570987274 1\r\n+\r\n", "output": "63271171\r\n"}, {"input": "354062556 688076879 786825319 1\r\n+\r\n", "output": "545304776\r\n"}, {"input": "206671954 13571766 192250278 1\r\n+\r\n", "output": "717117421\r\n"}, {"input": "23047921 621656196 160244047 1\r\n-\r\n", "output": "101533009\r\n"}, {"input": "806038018 740585177 987616107 293\r\n-+++++--++++---++-+--+-+---+-++++--+--+++--++---++++++++--+++++-+-++-+--+----+--+++-+-++-+++-+-+-+----------++-+-+++++++-+-+-+-++---+++-+-+-------+-+-++--++-++-++-++-+---+--++-++--+++--+++-+-+----++--+-+-++-+---+---+-+-+++------+-+++-+---++-+--+++----+++++---++-++--+----+++-+--+++-+------+-++\r\n", "output": "441468166\r\n"}, {"input": "262060935 184120408 148332034 148\r\n+--+-------+-+-+--++-+++--++-+-++++++--++-+++-+++--+-------+-+--+++-+-+-+---++-++-+-++---+--+-+-+--+------+++--+--+-+-+---+---+-+-++++---+++--+++---\r\n", "output": "700325386\r\n"}, {"input": "919350941 654611542 217223605 186\r\n++-++-+++++-+++--+---+++++++-++-+----+-++--+-++--++--+++-+++---+--+--++-+-+++-+-+++-++---+--+++-+-+++--+-+-------+-++------++---+-+---++-++-++---+-+--+-+--+++++---+--+--++++-++-++--+--++\r\n", "output": "116291420\r\n"}, {"input": "289455627 906207104 512692624 154\r\n-------++--+++---++-++------++----------+--+++-+-+++---+---+++--++++++--+-+-+--+---+-+-++-++--+-++--++++---+-+---+-----+--+-+---------+++-++---++-+-+-----\r\n", "output": "48198216\r\n"}, {"input": "258833760 515657142 791267045 1\r\n-\r\n", "output": "935800888\r\n"}, {"input": "691617927 66917103 843055237 8\r\n--+++---\r\n", "output": "147768186\r\n"}, {"input": "379582849 362892355 986900829 50\r\n++-++---+-+++++--++++--+--++--++-----+------++--+-\r\n", "output": "927469713\r\n"}, {"input": "176799169 363368399 841293419 1\r\n+\r\n", "output": "746494802\r\n"}, {"input": "144808247 203038656 166324035 4\r\n-+-+\r\n", "output": "909066471\r\n"}, {"input": "477607531 177367565 20080950 2\r\n++\r\n", "output": "928662830\r\n"}, {"input": "682074525 289438443 917164266 1\r\n+\r\n", "output": "28048785\r\n"}, {"input": "938449224 59852396 219719125 1\r\n-\r\n", "output": "648647459\r\n"}, {"input": "395171426 872478622 193568600 147\r\n+---++---+-+--+++++--+---+-++++-+-++---++++--+--+-+-++-+-++--------++---+++-+---++---+---+-+--+-++++-+++-+-+-++-+--+++-++-+-+-+-++++++-+---+---++--\r\n", "output": "460881399\r\n"}, {"input": "403493428 317461491 556701240 1\r\n-\r\n", "output": "936516261\r\n"}, {"input": "917751169 330191895 532837377 70\r\n-+-+++++++--++---++-+++++-+++-----+-+++---+--+-+-++-++-+-+-++-++-+----\r\n", "output": "908035409\r\n"}, {"input": "252089413 552678586 938424519 1\r\n-\r\n", "output": "627032736\r\n"}, {"input": "649316142 320010793 200197645 1\r\n-\r\n", "output": "323650777\r\n"}, {"input": "116399299 784781190 299072480 5\r\n++++-\r\n", "output": "754650814\r\n"}]
false
stdio
null
true
732/B
732
B
PyPy 3-64
TESTS
0
30
0
203366716
# from collections import defaultdict,deque # from sys import stdin # import itertools # import bisect # from math import sqrt,ceil,floor,gcd # from sortedcontainers import SortedList def func(put,mapping,unpack): n,k=unpack(int) v,ans=mapping(int),0 for i in range(0,n-1): if v[i] + v[i+1] < k:e = k - v[i] - v[i+1];ans += e;v[i+1] = e print(ans) print(*v) def init(TestCases=True): put = lambda s: s(input().strip()) mapping = lambda s: list(map(s,input().split())) unpack = lambda s: map(s,input().split()) for _ in range(int(input()) if TestCases else 1): func(put,mapping,unpack) if __name__ == '__main__': init(False)
70
46
0
199020801
def solve(): n,k=map(int,input().split());a=list(map(int,input().split()));c=0 for i in range(1,n): t=k-(a[i]+a[i-1]) if t>0: c+=t;a[i]+=t print(c) print(*a) solve()
Codeforces Round 377 (Div. 2)
CF
2,016
1
256
Cormen — The Best Friend Of a Man
Recently a dog was bought for Polycarp. The dog's name is Cormen. Now Polycarp has a lot of troubles. For example, Cormen likes going for a walk. Empirically Polycarp learned that the dog needs at least k walks for any two consecutive days in order to feel good. For example, if k = 5 and yesterday Polycarp went for a walk with Cormen 2 times, today he has to go for a walk at least 3 times. Polycarp analysed all his affairs over the next n days and made a sequence of n integers a1, a2, ..., an, where ai is the number of times Polycarp will walk with the dog on the i-th day while doing all his affairs (for example, he has to go to a shop, throw out the trash, etc.). Help Polycarp determine the minimum number of walks he needs to do additionaly in the next n days so that Cormen will feel good during all the n days. You can assume that on the day before the first day and on the day after the n-th day Polycarp will go for a walk with Cormen exactly k times. Write a program that will find the minumum number of additional walks and the appropriate schedule — the sequence of integers b1, b2, ..., bn (bi ≥ ai), where bi means the total number of walks with the dog on the i-th day.
The first line contains two integers n and k (1 ≤ n, k ≤ 500) — the number of days and the minimum number of walks with Cormen for any two consecutive days. The second line contains integers a1, a2, ..., an (0 ≤ ai ≤ 500) — the number of walks with Cormen on the i-th day which Polycarp has already planned.
In the first line print the smallest number of additional walks that Polycarp should do during the next n days so that Cormen will feel good during all days. In the second line print n integers b1, b2, ..., bn, where bi — the total number of walks on the i-th day according to the found solutions (ai ≤ bi for all i from 1 to n). If there are multiple solutions, print any of them.
null
null
[{"input": "3 5\n2 0 1", "output": "4\n2 3 2"}, {"input": "3 1\n0 0 0", "output": "1\n0 1 0"}, {"input": "4 6\n2 4 3 5", "output": "0\n2 4 3 5"}]
1,000
["dp", "greedy"]
70
[{"input": "3 5\r\n2 0 1\r\n", "output": "4\r\n2 3 2\r\n"}, {"input": "3 1\r\n0 0 0\r\n", "output": "1\r\n0 1 0\r\n"}, {"input": "4 6\r\n2 4 3 5\r\n", "output": "0\r\n2 4 3 5\r\n"}, {"input": "5 1\r\n0 0 0 0 1\r\n", "output": "2\r\n0 1 0 1 1\r\n"}, {"input": "10 500\r\n164 44 238 205 373 249 87 30 239 90\r\n", "output": "903\r\n164 336 238 262 373 249 251 249 251 249\r\n"}, {"input": "1 1\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "5 1\r\n0 0 0 0 0\r\n", "output": "2\r\n0 1 0 1 0\r\n"}, {"input": "5 1\r\n0 0 0 0 1\r\n", "output": "2\r\n0 1 0 1 1\r\n"}, {"input": "5 2\r\n0 0 0 1 0\r\n", "output": "3\r\n0 2 0 2 0\r\n"}, {"input": "5 5\r\n1 4 0 0 0\r\n", "output": "6\r\n1 4 1 4 1\r\n"}, {"input": "5 10\r\n1 2 1 0 1\r\n", "output": "16\r\n1 9 1 9 1\r\n"}, {"input": "5 10\r\n0 1 0 1 0\r\n", "output": "18\r\n0 10 0 10 0\r\n"}, {"input": "10 5\r\n0 2 3 0 0 1 0 2 3 1\r\n", "output": "13\r\n0 5 3 2 3 2 3 2 3 2\r\n"}, {"input": "10 1\r\n0 0 0 0 0 0 0 0 1 0\r\n", "output": "4\r\n0 1 0 1 0 1 0 1 1 0\r\n"}, {"input": "10 436\r\n13 16 45 9 10 17 5 26 10 12\r\n", "output": "2017\r\n13 423 45 391 45 391 45 391 45 391\r\n"}, {"input": "10 438\r\n71 160 43 326 128 35 41 247 30 49\r\n", "output": "1060\r\n71 367 71 367 128 310 128 310 128 310\r\n"}, {"input": "10 431\r\n121 24 93 59 243 147 1 254 75 168\r\n", "output": "1036\r\n121 310 121 310 243 188 243 254 177 254\r\n"}, {"input": "10 10\r\n0 0 0 0 0 0 0 0 0 0\r\n", "output": "50\r\n0 10 0 10 0 10 0 10 0 10\r\n"}, {"input": "10 10\r\n0 0 1 0 0 0 1 0 0 0\r\n", "output": "48\r\n0 10 1 9 1 9 1 9 1 9\r\n"}, {"input": "10 10\r\n0 0 0 1 0 0 1 0 0 0\r\n", "output": "48\r\n0 10 0 10 0 10 1 9 1 9\r\n"}, {"input": "10 10\r\n1 1 0 2 0 1 1 1 2 0\r\n", "output": "41\r\n1 9 1 9 1 9 1 9 2 8\r\n"}, {"input": "10 10\r\n1 2 2 0 0 2 0 1 0 0\r\n", "output": "42\r\n1 9 2 8 2 8 2 8 2 8\r\n"}, {"input": "10 10\r\n1 0 1 0 0 5 2 0 0 1\r\n", "output": "40\r\n1 9 1 9 1 9 2 8 2 8\r\n"}, {"input": "10 10\r\n2 3 5 0 2 0 15 6 5 0\r\n", "output": "23\r\n2 8 5 5 5 5 15 6 5 5\r\n"}, {"input": "10 10\r\n16 15 4 10 14 2 18 11 24 5\r\n", "output": "0\r\n16 15 4 10 14 2 18 11 24 5\r\n"}, {"input": "100 100\r\n48 19 63 8 18 22 5 5 12 7 9 37 17 22 58 14 53 25 24 16 22 36 4 2 9 63 52 43 22 72 0 9 12 26 50 1 21 9 40 9 5 6 2 24 1 88 50 7 9 1 3 16 0 17 3 32 47 9 32 87 20 3 45 41 16 43 41 31 28 30 2 31 72 16 74 59 20 34 25 18 48 10 34 20 22 16 3 32 8 34 8 4 45 65 48 42 1 45 11 15\r\n", "output": "2588\r\n48 52 63 37 63 37 63 37 63 37 63 37 63 37 63 37 63 37 63 37 63 37 63 37 63 63 52 48 52 72 28 72 28 72 50 50 50 50 50 50 50 50 50 50 50 88 50 50 50 50 50 50 50 50 50 50 50 50 50 87 20 80 45 55 45 55 45 55 45 55 45 55 72 28 74 59 41 59 41 59 48 52 48 52 48 52 48 52 48 52 48 52 48 65 48 52 48 52 48 52\r\n"}, {"input": "100 200\r\n28 52 65 37 1 64 13 57 44 12 37 0 9 68 17 5 28 4 2 12 8 47 7 33 1 27 50 59 9 0 4 27 31 31 49 1 35 43 36 12 5 0 49 40 19 12 39 3 41 25 19 15 57 24 3 9 4 31 42 55 11 13 1 8 0 25 34 52 47 59 74 43 36 47 2 3 1 13 56 48 42 24 4 32 12 3 33 12 14 14 84 32 1 3 8 49 9 18 43 43\r\n", "output": "7390\r\n28 172 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 74 126 74 126 74 126 74 126 74 126 74 126 74 126 74 126 74 126 74 126 84 116 84 116 84 116 84 116 84 116\r\n"}, {"input": "100 10\r\n1 2 7 0 2 0 0 0 2 5 3 2 2 1 0 7 1 6 1 1 5 1 2 3 5 0 0 0 0 0 1 0 1 0 2 1 3 0 1 1 0 0 3 1 6 3 2 2 1 3 1 0 9 1 3 2 3 0 5 1 0 5 5 5 2 1 3 0 1 3 5 2 4 4 1 2 3 0 2 1 3 6 4 3 1 0 9 1 0 3 3 6 7 2 5 2 2 6 0 2\r\n", "output": "288\r\n1 9 7 3 7 3 7 3 7 5 5 5 5 5 5 7 3 7 3 7 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 4 6 4 6 4 6 4 9 1 9 2 8 2 8 2 8 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 4 6 4 6 9 1 9 3 7 6 7 3 7 3 7 6 4 6\r\n"}, {"input": "100 500\r\n207 27 83 171 129 204 11 55 58 115 43 280 208 169 23 79 36 59 132 28 13 136 246 134 29 135 176 21 155 175 127 288 68 68 41 156 194 31 44 131 30 31 89 46 180 184 12 29 2 58 70 157 329 294 126 55 79 19 125 15 39 30 2 137 36 151 5 246 176 1 158 31 4 99 192 200 124 66 10 195 180 165 8 79 257 68 5 175 43 141 0 106 38 32 0 56 33 221 144 226\r\n", "output": "14863\r\n207 293 207 293 207 293 207 293 207 293 207 293 208 292 208 292 208 292 208 292 208 292 246 254 246 254 246 254 246 254 246 288 212 288 212 288 212 288 212 288 212 288 212 288 212 288 212 288 212 288 212 288 329 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 257 243 257 243 257 243 257 243 257 243 257 243 257 243 257 243\r\n"}, {"input": "100 500\r\n64 140 15 221 24 106 73 30 275 97 296 55 5 30 47 199 130 44 72 170 7 204 359 40 128 117 45 192 344 112 0 11 196 78 73 53 222 93 88 151 99 283 60 71 4 87 226 46 66 74 23 89 77 60 397 181 0 101 358 54 124 155 19 218 9 140 161 130 308 85 103 85 300 128 19 108 225 136 100 54 30 24 129 245 128 88 160 120 51 154 19 129 114 32 256 30 102 207 115 49\r\n", "output": "13634\r\n64 436 64 436 64 436 73 427 275 225 296 204 296 204 296 204 296 204 296 204 296 204 359 141 359 141 359 192 344 156 344 156 344 156 344 156 344 156 344 156 344 283 217 283 217 283 226 274 226 274 226 274 226 274 397 181 319 181 358 142 358 155 345 218 282 218 282 218 308 192 308 192 308 192 308 192 308 192 308 192 308 192 308 245 255 245 255 245 255 245 255 245 255 245 256 244 256 244 256 244\r\n"}, {"input": "1 500\r\n500\r\n", "output": "0\r\n500\r\n"}, {"input": "2 1\r\n0 0\r\n", "output": "1\r\n0 1\r\n"}, {"input": "1 10\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "1 4\r\n2\r\n", "output": "0\r\n2\r\n"}, {"input": "1 10\r\n2\r\n", "output": "0\r\n2\r\n"}, {"input": "1 10\r\n0\r\n", "output": "0\r\n0\r\n"}, {"input": "1 5\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "1 2\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "1 5\r\n2\r\n", "output": "0\r\n2\r\n"}, {"input": "1 3\r\n0\r\n", "output": "0\r\n0\r\n"}, {"input": "1 3\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "1 5\r\n0\r\n", "output": "0\r\n0\r\n"}, {"input": "1 7\r\n0\r\n", "output": "0\r\n0\r\n"}, {"input": "1 7\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "1 3\r\n2\r\n", "output": "0\r\n2\r\n"}, {"input": "1 5\r\n3\r\n", "output": "0\r\n3\r\n"}, {"input": "1 4\r\n3\r\n", "output": "0\r\n3\r\n"}, {"input": "1 6\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "1 6\r\n2\r\n", "output": "0\r\n2\r\n"}, {"input": "1 500\r\n0\r\n", "output": "0\r\n0\r\n"}, {"input": "3 7\r\n2 3 1\r\n", "output": "3\r\n2 5 2\r\n"}, {"input": "1 10\r\n5\r\n", "output": "0\r\n5\r\n"}, {"input": "5 10\r\n1 2 3 4 5\r\n", "output": "10\r\n1 9 3 7 5\r\n"}, {"input": "2 6\r\n1 2\r\n", "output": "3\r\n1 5\r\n"}, {"input": "1 10\r\n3\r\n", "output": "0\r\n3\r\n"}, {"input": "1 6\r\n3\r\n", "output": "0\r\n3\r\n"}, {"input": "1 100\r\n1\r\n", "output": "0\r\n1\r\n"}, {"input": "1 7\r\n2\r\n", "output": "0\r\n2\r\n"}, {"input": "2 10\r\n1 2\r\n", "output": "7\r\n1 9\r\n"}, {"input": "1 9\r\n1\r\n", "output": "0\r\n1\r\n"}]
false
stdio
null
true
215/A
215
A
Python 3
TESTS
16
92
0
148101635
n=int(input()) q=[int(l) for l in input().split()] m=int(input()) w=[int(q) for q in input().split()] max=0;adet=1 for a in range(m): for b in range(n): if(int(w[m-1-a]/q[b])==w[m-1-a]/q[b] and w[m-1-a]/q[b]>max): max=w[m-1-a]/q[b] elif(int(w[m-1-a]/q[b])==w[m-1-a]/q[b] and w[m-1-a]/q[b]==max): adet+=1 print(adet)
57
92
0
136132353
n=int(input()) a=[int(x) for x in input().split()] m=int(input()) b=[int(x) for x in input().split()] lst=[] for i in a: for j in b: if j%i==0: lst.append(j//i) print(lst.count(max(lst)))
Codeforces Round 132 (Div. 2)
CF
2,012
2
256
Bicycle Chain
Vasya's bicycle chain drive consists of two parts: n stars are attached to the pedal axle, m stars are attached to the rear wheel axle. The chain helps to rotate the rear wheel by transmitting the pedal rotation. We know that the i-th star on the pedal axle has ai (0 < a1 < a2 < ... < an) teeth, and the j-th star on the rear wheel axle has bj (0 < b1 < b2 < ... < bm) teeth. Any pair (i, j) (1 ≤ i ≤ n; 1 ≤ j ≤ m) is called a gear and sets the indexes of stars to which the chain is currently attached. Gear (i, j) has a gear ratio, equal to the value $$\frac{b_{j}}{a_{j}}$$. Since Vasya likes integers, he wants to find such gears (i, j), that their ratios are integers. On the other hand, Vasya likes fast driving, so among all "integer" gears (i, j) he wants to choose a gear with the maximum ratio. Help him to find the number of such gears. In the problem, fraction $$\frac{b_{j}}{a_{j}}$$ denotes division in real numbers, that is, no rounding is performed.
The first input line contains integer n (1 ≤ n ≤ 50) — the number of stars on the bicycle's pedal axle. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 104) in the order of strict increasing. The third input line contains integer m (1 ≤ m ≤ 50) — the number of stars on the rear wheel axle. The fourth line contains m integers b1, b2, ..., bm (1 ≤ bi ≤ 104) in the order of strict increasing. It is guaranteed that there exists at least one gear (i, j), that its gear ratio is an integer. The numbers on the lines are separated by spaces.
Print the number of "integer" gears with the maximum ratio among all "integer" gears.
null
In the first sample the maximum "integer" gear ratio equals 3. There are two gears that have such gear ratio. For one of them a1 = 4, b1 = 12, and for the other a2 = 5, b3 = 15.
[{"input": "2\n4 5\n3\n12 13 15", "output": "2"}, {"input": "4\n1 2 3 4\n5\n10 11 12 13 14", "output": "1"}]
900
["brute force", "implementation"]
57
[{"input": "2\r\n4 5\r\n3\r\n12 13 15\r\n", "output": "2\r\n"}, {"input": "4\r\n1 2 3 4\r\n5\r\n10 11 12 13 14\r\n", "output": "1\r\n"}, {"input": "1\r\n1\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "2\r\n1 2\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n1\r\n2\r\n1 2\r\n", "output": "1\r\n"}, {"input": "4\r\n3 7 11 13\r\n4\r\n51 119 187 221\r\n", "output": "4\r\n"}, {"input": "4\r\n2 3 4 5\r\n3\r\n1 2 3\r\n", "output": "2\r\n"}, {"input": "10\r\n6 12 13 20 48 53 74 92 96 97\r\n10\r\n1 21 32 36 47 54 69 75 95 97\r\n", "output": "1\r\n"}, {"input": "10\r\n5 9 10 14 15 17 19 22 24 26\r\n10\r\n2 11 17 19 21 22 24 25 27 28\r\n", "output": "1\r\n"}, {"input": "10\r\n24 53 56 126 354 432 442 740 795 856\r\n10\r\n273 438 494 619 689 711 894 947 954 958\r\n", "output": "1\r\n"}, {"input": "10\r\n3 4 6 7 8 10 14 16 19 20\r\n10\r\n3 4 5 7 8 10 15 16 18 20\r\n", "output": "1\r\n"}, {"input": "10\r\n1 6 8 14 15 17 25 27 34 39\r\n10\r\n1 8 16 17 19 22 32 39 44 50\r\n", "output": "1\r\n"}, {"input": "10\r\n5 21 22 23 25 32 35 36 38 39\r\n10\r\n3 7 8 9 18 21 23 24 36 38\r\n", "output": "4\r\n"}, {"input": "10\r\n17 239 443 467 661 1069 1823 2333 3767 4201\r\n20\r\n51 83 97 457 593 717 997 1329 1401 1459 1471 1983 2371 2539 3207 3251 3329 5469 6637 6999\r\n", "output": "8\r\n"}, {"input": "20\r\n179 359 401 467 521 601 919 941 1103 1279 1709 1913 1949 2003 2099 2143 2179 2213 2399 4673\r\n20\r\n151 181 191 251 421 967 1109 1181 1249 1447 1471 1553 1619 2327 2551 2791 3049 3727 6071 7813\r\n", "output": "3\r\n"}, {"input": "20\r\n79 113 151 709 809 983 1291 1399 1409 1429 2377 2659 2671 2897 3217 3511 3557 3797 3823 4363\r\n10\r\n19 101 659 797 1027 1963 2129 2971 3299 9217\r\n", "output": "3\r\n"}, {"input": "30\r\n19 47 109 179 307 331 389 401 461 509 547 569 617 853 883 1249 1361 1381 1511 1723 1741 1783 2459 2531 2621 3533 3821 4091 5557 6217\r\n20\r\n401 443 563 941 967 997 1535 1567 1655 1747 1787 1945 1999 2251 2305 2543 2735 4415 6245 7555\r\n", "output": "8\r\n"}, {"input": "5\r\n33 78 146 3055 4268\r\n5\r\n2211 2584 5226 9402 9782\r\n", "output": "3\r\n"}, {"input": "5\r\n35 48 52 86 8001\r\n10\r\n332 3430 3554 4704 4860 5096 6215 7583 8228 8428\r\n", "output": "4\r\n"}, {"input": "10\r\n97 184 207 228 269 2084 4450 6396 7214 9457\r\n16\r\n338 1179 1284 1545 1570 2444 3167 3395 3397 5550 6440 7245 7804 7980 9415 9959\r\n", "output": "5\r\n"}, {"input": "30\r\n25 30 41 57 58 62 70 72 76 79 84 85 88 91 98 101 104 109 119 129 136 139 148 151 926 1372 3093 3936 5423 7350\r\n25\r\n1600 1920 2624 3648 3712 3968 4480 4608 4864 5056 5376 5440 5632 5824 6272 6464 6656 6934 6976 7616 8256 8704 8896 9472 9664\r\n", "output": "24\r\n"}, {"input": "5\r\n33 78 146 3055 4268\r\n5\r\n2211 2584 5226 9402 9782\r\n", "output": "3\r\n"}, {"input": "5\r\n35 48 52 86 8001\r\n10\r\n332 3430 3554 4704 4860 5096 6215 7583 8228 8428\r\n", "output": "4\r\n"}, {"input": "10\r\n97 184 207 228 269 2084 4450 6396 7214 9457\r\n16\r\n338 1179 1284 1545 1570 2444 3167 3395 3397 5550 6440 7245 7804 7980 9415 9959\r\n", "output": "5\r\n"}, {"input": "30\r\n25 30 41 57 58 62 70 72 76 79 84 85 88 91 98 101 104 109 119 129 136 139 148 151 926 1372 3093 3936 5423 7350\r\n25\r\n1600 1920 2624 3648 3712 3968 4480 4608 4864 5056 5376 5440 5632 5824 6272 6464 6656 6934 6976 7616 8256 8704 8896 9472 9664\r\n", "output": "24\r\n"}, {"input": "50\r\n51 67 75 186 194 355 512 561 720 876 1077 1221 1503 1820 2153 2385 2568 2608 2937 2969 3271 3311 3481 4081 4093 4171 4255 4256 4829 5020 5192 5636 5817 6156 6712 6717 7153 7436 7608 7612 7866 7988 8264 8293 8867 9311 9879 9882 9889 9908\r\n1\r\n5394\r\n", "output": "1\r\n"}, {"input": "3\r\n3 4 5\r\n3\r\n6 20 25\r\n", "output": "2\r\n"}, {"input": "4\r\n2 3 5 8\r\n4\r\n2 6 8 10\r\n", "output": "1\r\n"}, {"input": "4\r\n3 5 7 11\r\n4\r\n3 5 7 22\r\n", "output": "1\r\n"}, {"input": "2\r\n2 3\r\n3\r\n20 30 50\r\n", "output": "1\r\n"}, {"input": "3\r\n1 2 3\r\n4\r\n2 4 6 49\r\n", "output": "1\r\n"}, {"input": "2\r\n4 5\r\n3\r\n12 15 20\r\n", "output": "1\r\n"}, {"input": "3\r\n2 5 7\r\n3\r\n4 5 7\r\n", "output": "1\r\n"}, {"input": "3\r\n3 5 8\r\n3\r\n6 8 10\r\n", "output": "2\r\n"}, {"input": "2\r\n2 3\r\n4\r\n4 6 9 33\r\n", "output": "1\r\n"}, {"input": "2\r\n2 3\r\n4\r\n4 6 21 40\r\n", "output": "1\r\n"}, {"input": "3\r\n4 9 10\r\n3\r\n8 9 10\r\n", "output": "1\r\n"}, {"input": "5\r\n1 5 6 9 51\r\n5\r\n5 12 18 27 10000\r\n", "output": "1\r\n"}, {"input": "13\r\n1 2 3 4 5 6 7 8 9 10 11 12 13\r\n1\r\n14\r\n", "output": "1\r\n"}]
false
stdio
null
true
963/A
963
A
PyPy 3-64
TESTS
8
62
1,740,800
217521721
import sys input = sys.stdin.buffer.readline p = 10**9+9 def process(n, a, b, S): k = len(S) n1 = (n+1)//k n2 = (n+1) % k answer = 0 ratio_num = pow(b, k, p) ratio_den = pow(a, k, p) ratio = (ratio_num*pow(ratio_den, p-2, p)) % p fraction_den = pow(ratio-1, p-2, p) % p for i in range(k): entry = 0 first_term = (pow(a, n-i, p)*pow(b, i, p)) % p if n2==0: term_count = n1 elif i < n2: term_count = n1+1 else: term_count = n1 entry = first_term*(pow(ratio, term_count, p)-1)*fraction_den entry = entry % p if S[i]=='+': answer = (answer+entry) % p else: answer = (answer-entry) % p sys.stdout.write(f'{answer}\n') n, a, b, k = [int(x) for x in input().split()] S = input().decode().strip() process(n, a, b, S)
71
156
1,843,200
103518824
MOD = int(1e9+9) n, a, b, k = map(int, input().split()) s = input() def solve(): res = 0 q = pow(b, k, MOD) * pow(pow(a, k, MOD), MOD-2, MOD) % MOD max_pow = pow(a, n, MOD) c = b * pow(a, MOD-2, MOD) % MOD for i in range(k): res += max_pow if s[i] == '+' else -max_pow res = (res % MOD + MOD) % MOD max_pow = max_pow * c % MOD t = (n + 1) // k if q == 1: return t * res % MOD z = (pow(q, t, MOD) - 1) * pow(q-1, MOD-2, MOD) % MOD return z * res % MOD print(solve())
Tinkoff Internship Warmup Round 2018 and Codeforces Round 475 (Div. 1)
CF
2,018
1
256
Alternating Sum
You are given two integers $$$a$$$ and $$$b$$$. Moreover, you are given a sequence $$$s_0, s_1, \dots, s_{n}$$$. All values in $$$s$$$ are integers $$$1$$$ or $$$-1$$$. It's known that sequence is $$$k$$$-periodic and $$$k$$$ divides $$$n+1$$$. In other words, for each $$$k \leq i \leq n$$$ it's satisfied that $$$s_{i} = s_{i - k}$$$. Find out the non-negative remainder of division of $$$\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i}$$$ by $$$10^{9} + 9$$$. Note that the modulo is unusual!
The first line contains four integers $$$n, a, b$$$ and $$$k$$$ $$$(1 \leq n \leq 10^{9}, 1 \leq a, b \leq 10^{9}, 1 \leq k \leq 10^{5})$$$. The second line contains a sequence of length $$$k$$$ consisting of characters '+' and '-'. If the $$$i$$$-th character (0-indexed) is '+', then $$$s_{i} = 1$$$, otherwise $$$s_{i} = -1$$$. Note that only the first $$$k$$$ members of the sequence are given, the rest can be obtained using the periodicity property.
Output a single integer — value of given expression modulo $$$10^{9} + 9$$$.
null
In the first example: $$$(\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i})$$$ = $$$2^{2} 3^{0} - 2^{1} 3^{1} + 2^{0} 3^{2}$$$ = 7 In the second example: $$$(\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i}) = -1^{4} 5^{0} - 1^{3} 5^{1} - 1^{2} 5^{2} - 1^{1} 5^{3} - 1^{0} 5^{4} = -781 \equiv 999999228 \pmod{10^{9} + 9}$$$.
[{"input": "2 2 3 3\n+-+", "output": "7"}, {"input": "4 1 5 1\n-", "output": "999999228"}]
1,800
["math", "number theory"]
71
[{"input": "2 2 3 3\r\n+-+\r\n", "output": "7\r\n"}, {"input": "4 1 5 1\r\n-\r\n", "output": "999999228\r\n"}, {"input": "1 1 4 2\r\n-+\r\n", "output": "3\r\n"}, {"input": "3 1 4 4\r\n+--+\r\n", "output": "45\r\n"}, {"input": "5 1 1 6\r\n++---+\r\n", "output": "0\r\n"}, {"input": "5 2 2 6\r\n+--++-\r\n", "output": "0\r\n"}, {"input": "686653196 115381398 884618610 3\r\n+-+\r\n", "output": "542231211\r\n"}, {"input": "608663287 430477711 172252358 8\r\n-+--+-+-\r\n", "output": "594681696\r\n"}, {"input": "904132655 827386249 118827660 334\r\n+++-+++++--+++----+-+-+-+-+--+-+---++--++--++--+-+-+++-+++--+-+-+----+-+-++++-----+--++++------+++-+-+-++-++++++++-+-++-+++--+--++------+--+-+++--++--+---++-++-+-+-++---++-++--+-+-++-+------+-+----+++-+++--+-+-+--+--+--+------+--+---+--+-++--+++---+-+-++--------+-++--++-+-+-+-+-+-+--+-++++-+++--+--++----+--+-++-++--+--+-+-++-+-++++-\r\n", "output": "188208979\r\n"}, {"input": "234179195 430477711 115381398 12\r\n++++-+-+-+++\r\n", "output": "549793323\r\n"}, {"input": "75952547 967294208 907708706 252\r\n++--++--+++-+-+--++--++++++---+++-++-+-----++++--++-+-++------+-+-+-++-+-+-++++------++---+-++++---+-+-++++--++++++--+-+++-++--+--+---++++---+-+++-+++--+-+--+++++---+--++-++++--++++-+-++-+++-++-----+-+++++----++--+++-+-+++++-+--++-++-+--+-++++--+-+-+-+\r\n", "output": "605712499\r\n"}, {"input": "74709071 801809249 753674746 18\r\n++++++-+-+---+-+--\r\n", "output": "13414893\r\n"}, {"input": "743329 973758 92942 82\r\n++----+-++++----+--+++---+--++++-+-+---+++++--+--+++++++--++-+++----+--+++++-+--+-\r\n", "output": "299311566\r\n"}, {"input": "18111 291387 518587 2\r\n++\r\n", "output": "724471355\r\n"}, {"input": "996144 218286 837447 1\r\n-\r\n", "output": "549104837\r\n"}, {"input": "179358 828426 548710 67\r\n++++---+--++----+-+-++++----+--+---+------++-+-++++--+----+---+-+--\r\n", "output": "759716474\r\n"}, {"input": "397521 174985 279760 1\r\n+\r\n", "output": "25679493\r\n"}, {"input": "613632 812232 482342 1\r\n-\r\n", "output": "891965141\r\n"}, {"input": "936810 183454 647048 1\r\n+\r\n", "output": "523548992\r\n"}, {"input": "231531 250371 921383 28\r\n++-+------+--+--++++--+-+++-\r\n", "output": "134450934\r\n"}, {"input": "947301 87242 360762 97\r\n--+++--+++-++--++-++--++--+++---+++--++++--+++++--+-++-++-----+-++-+--++-----+-++-+--++-++-+-----\r\n", "output": "405016159\r\n"}, {"input": "425583346 814209084 570987274 1\r\n+\r\n", "output": "63271171\r\n"}, {"input": "354062556 688076879 786825319 1\r\n+\r\n", "output": "545304776\r\n"}, {"input": "206671954 13571766 192250278 1\r\n+\r\n", "output": "717117421\r\n"}, {"input": "23047921 621656196 160244047 1\r\n-\r\n", "output": "101533009\r\n"}, {"input": "806038018 740585177 987616107 293\r\n-+++++--++++---++-+--+-+---+-++++--+--+++--++---++++++++--+++++-+-++-+--+----+--+++-+-++-+++-+-+-+----------++-+-+++++++-+-+-+-++---+++-+-+-------+-+-++--++-++-++-++-+---+--++-++--+++--+++-+-+----++--+-+-++-+---+---+-+-+++------+-+++-+---++-+--+++----+++++---++-++--+----+++-+--+++-+------+-++\r\n", "output": "441468166\r\n"}, {"input": "262060935 184120408 148332034 148\r\n+--+-------+-+-+--++-+++--++-+-++++++--++-+++-+++--+-------+-+--+++-+-+-+---++-++-+-++---+--+-+-+--+------+++--+--+-+-+---+---+-+-++++---+++--+++---\r\n", "output": "700325386\r\n"}, {"input": "919350941 654611542 217223605 186\r\n++-++-+++++-+++--+---+++++++-++-+----+-++--+-++--++--+++-+++---+--+--++-+-+++-+-+++-++---+--+++-+-+++--+-+-------+-++------++---+-+---++-++-++---+-+--+-+--+++++---+--+--++++-++-++--+--++\r\n", "output": "116291420\r\n"}, {"input": "289455627 906207104 512692624 154\r\n-------++--+++---++-++------++----------+--+++-+-+++---+---+++--++++++--+-+-+--+---+-+-++-++--+-++--++++---+-+---+-----+--+-+---------+++-++---++-+-+-----\r\n", "output": "48198216\r\n"}, {"input": "258833760 515657142 791267045 1\r\n-\r\n", "output": "935800888\r\n"}, {"input": "691617927 66917103 843055237 8\r\n--+++---\r\n", "output": "147768186\r\n"}, {"input": "379582849 362892355 986900829 50\r\n++-++---+-+++++--++++--+--++--++-----+------++--+-\r\n", "output": "927469713\r\n"}, {"input": "176799169 363368399 841293419 1\r\n+\r\n", "output": "746494802\r\n"}, {"input": "144808247 203038656 166324035 4\r\n-+-+\r\n", "output": "909066471\r\n"}, {"input": "477607531 177367565 20080950 2\r\n++\r\n", "output": "928662830\r\n"}, {"input": "682074525 289438443 917164266 1\r\n+\r\n", "output": "28048785\r\n"}, {"input": "938449224 59852396 219719125 1\r\n-\r\n", "output": "648647459\r\n"}, {"input": "395171426 872478622 193568600 147\r\n+---++---+-+--+++++--+---+-++++-+-++---++++--+--+-+-++-+-++--------++---+++-+---++---+---+-+--+-++++-+++-+-+-++-+--+++-++-+-+-+-++++++-+---+---++--\r\n", "output": "460881399\r\n"}, {"input": "403493428 317461491 556701240 1\r\n-\r\n", "output": "936516261\r\n"}, {"input": "917751169 330191895 532837377 70\r\n-+-+++++++--++---++-+++++-+++-----+-+++---+--+-+-++-++-+-+-++-++-+----\r\n", "output": "908035409\r\n"}, {"input": "252089413 552678586 938424519 1\r\n-\r\n", "output": "627032736\r\n"}, {"input": "649316142 320010793 200197645 1\r\n-\r\n", "output": "323650777\r\n"}, {"input": "116399299 784781190 299072480 5\r\n++++-\r\n", "output": "754650814\r\n"}]
false
stdio
null
true
963/A
963
A
Python 3
TESTS
8
109
307,200
98227258
def binpow(a,b,m): a %= m res = int(1) while b > 0 : if b & 1: res = res * a % m a = a * a % m b >>= 1 return res def inv(a,m:int=1000000009): return binpow(a,m-2,m) m=int(1000000009) n,a,b,k=input().split() n=int(n);a=int(a);b=int(b);k=int(k) st=input() res=int(1) p=int((b*inv(a,m))%m) itr=(n+1)//k mul1=0 for i in range (len(st)): if st[i]=='+': mul1=(mul1+binpow(p,i,m))%m else: mul1=(mul1-binpow(p,i,m))%m mul2=((binpow(p,k*itr,m)-1)*inv((binpow(p,k,m)-1)%m))%m res=(mul1*mul2)%m left=n-k*itr+1 mul1=binpow(p,k*itr,m) mul2=0 for i in range (left): if st[i]=='+': mul2=(mul2+binpow(p,i,m))%m else: mul2=(mul2-binpow(p,i,m))%m res=(res+mul1*mul2)%m res=(res*binpow(a,n,m))%m print(res)
71
170
204,800
43395441
import sys MOD = 10**9 + 9 def inv(x): return pow(x, MOD-2, MOD) def alternateSum(n,a,b,k,s): res = 0 q = (pow(b, k, MOD) * inv(pow(a, k, MOD))) % MOD max_pow = pow(a, n, MOD) c = b * inv(a) % MOD for i in range(k): if s[i] == '+': res += max_pow elif s[i] == '-': res -= max_pow res %= MOD max_pow = (max_pow * c) % MOD t = (n+1) // k if q == 1: return (t*res) % MOD z = ((pow(q, t, MOD) - 1) * inv(q-1)) % MOD return z * res % MOD n, a, b, k, s = sys.stdin.read().split() result = alternateSum(int(n), int(a), int(b), int(k), s) print(result)
Tinkoff Internship Warmup Round 2018 and Codeforces Round 475 (Div. 1)
CF
2,018
1
256
Alternating Sum
You are given two integers $$$a$$$ and $$$b$$$. Moreover, you are given a sequence $$$s_0, s_1, \dots, s_{n}$$$. All values in $$$s$$$ are integers $$$1$$$ or $$$-1$$$. It's known that sequence is $$$k$$$-periodic and $$$k$$$ divides $$$n+1$$$. In other words, for each $$$k \leq i \leq n$$$ it's satisfied that $$$s_{i} = s_{i - k}$$$. Find out the non-negative remainder of division of $$$\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i}$$$ by $$$10^{9} + 9$$$. Note that the modulo is unusual!
The first line contains four integers $$$n, a, b$$$ and $$$k$$$ $$$(1 \leq n \leq 10^{9}, 1 \leq a, b \leq 10^{9}, 1 \leq k \leq 10^{5})$$$. The second line contains a sequence of length $$$k$$$ consisting of characters '+' and '-'. If the $$$i$$$-th character (0-indexed) is '+', then $$$s_{i} = 1$$$, otherwise $$$s_{i} = -1$$$. Note that only the first $$$k$$$ members of the sequence are given, the rest can be obtained using the periodicity property.
Output a single integer — value of given expression modulo $$$10^{9} + 9$$$.
null
In the first example: $$$(\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i})$$$ = $$$2^{2} 3^{0} - 2^{1} 3^{1} + 2^{0} 3^{2}$$$ = 7 In the second example: $$$(\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i}) = -1^{4} 5^{0} - 1^{3} 5^{1} - 1^{2} 5^{2} - 1^{1} 5^{3} - 1^{0} 5^{4} = -781 \equiv 999999228 \pmod{10^{9} + 9}$$$.
[{"input": "2 2 3 3\n+-+", "output": "7"}, {"input": "4 1 5 1\n-", "output": "999999228"}]
1,800
["math", "number theory"]
71
[{"input": "2 2 3 3\r\n+-+\r\n", "output": "7\r\n"}, {"input": "4 1 5 1\r\n-\r\n", "output": "999999228\r\n"}, {"input": "1 1 4 2\r\n-+\r\n", "output": "3\r\n"}, {"input": "3 1 4 4\r\n+--+\r\n", "output": "45\r\n"}, {"input": "5 1 1 6\r\n++---+\r\n", "output": "0\r\n"}, {"input": "5 2 2 6\r\n+--++-\r\n", "output": "0\r\n"}, {"input": "686653196 115381398 884618610 3\r\n+-+\r\n", "output": "542231211\r\n"}, {"input": "608663287 430477711 172252358 8\r\n-+--+-+-\r\n", "output": "594681696\r\n"}, {"input": "904132655 827386249 118827660 334\r\n+++-+++++--+++----+-+-+-+-+--+-+---++--++--++--+-+-+++-+++--+-+-+----+-+-++++-----+--++++------+++-+-+-++-++++++++-+-++-+++--+--++------+--+-+++--++--+---++-++-+-+-++---++-++--+-+-++-+------+-+----+++-+++--+-+-+--+--+--+------+--+---+--+-++--+++---+-+-++--------+-++--++-+-+-+-+-+-+--+-++++-+++--+--++----+--+-++-++--+--+-+-++-+-++++-\r\n", "output": "188208979\r\n"}, {"input": "234179195 430477711 115381398 12\r\n++++-+-+-+++\r\n", "output": "549793323\r\n"}, {"input": "75952547 967294208 907708706 252\r\n++--++--+++-+-+--++--++++++---+++-++-+-----++++--++-+-++------+-+-+-++-+-+-++++------++---+-++++---+-+-++++--++++++--+-+++-++--+--+---++++---+-+++-+++--+-+--+++++---+--++-++++--++++-+-++-+++-++-----+-+++++----++--+++-+-+++++-+--++-++-+--+-++++--+-+-+-+\r\n", "output": "605712499\r\n"}, {"input": "74709071 801809249 753674746 18\r\n++++++-+-+---+-+--\r\n", "output": "13414893\r\n"}, {"input": "743329 973758 92942 82\r\n++----+-++++----+--+++---+--++++-+-+---+++++--+--+++++++--++-+++----+--+++++-+--+-\r\n", "output": "299311566\r\n"}, {"input": "18111 291387 518587 2\r\n++\r\n", "output": "724471355\r\n"}, {"input": "996144 218286 837447 1\r\n-\r\n", "output": "549104837\r\n"}, {"input": "179358 828426 548710 67\r\n++++---+--++----+-+-++++----+--+---+------++-+-++++--+----+---+-+--\r\n", "output": "759716474\r\n"}, {"input": "397521 174985 279760 1\r\n+\r\n", "output": "25679493\r\n"}, {"input": "613632 812232 482342 1\r\n-\r\n", "output": "891965141\r\n"}, {"input": "936810 183454 647048 1\r\n+\r\n", "output": "523548992\r\n"}, {"input": "231531 250371 921383 28\r\n++-+------+--+--++++--+-+++-\r\n", "output": "134450934\r\n"}, {"input": "947301 87242 360762 97\r\n--+++--+++-++--++-++--++--+++---+++--++++--+++++--+-++-++-----+-++-+--++-----+-++-+--++-++-+-----\r\n", "output": "405016159\r\n"}, {"input": "425583346 814209084 570987274 1\r\n+\r\n", "output": "63271171\r\n"}, {"input": "354062556 688076879 786825319 1\r\n+\r\n", "output": "545304776\r\n"}, {"input": "206671954 13571766 192250278 1\r\n+\r\n", "output": "717117421\r\n"}, {"input": "23047921 621656196 160244047 1\r\n-\r\n", "output": "101533009\r\n"}, {"input": "806038018 740585177 987616107 293\r\n-+++++--++++---++-+--+-+---+-++++--+--+++--++---++++++++--+++++-+-++-+--+----+--+++-+-++-+++-+-+-+----------++-+-+++++++-+-+-+-++---+++-+-+-------+-+-++--++-++-++-++-+---+--++-++--+++--+++-+-+----++--+-+-++-+---+---+-+-+++------+-+++-+---++-+--+++----+++++---++-++--+----+++-+--+++-+------+-++\r\n", "output": "441468166\r\n"}, {"input": "262060935 184120408 148332034 148\r\n+--+-------+-+-+--++-+++--++-+-++++++--++-+++-+++--+-------+-+--+++-+-+-+---++-++-+-++---+--+-+-+--+------+++--+--+-+-+---+---+-+-++++---+++--+++---\r\n", "output": "700325386\r\n"}, {"input": "919350941 654611542 217223605 186\r\n++-++-+++++-+++--+---+++++++-++-+----+-++--+-++--++--+++-+++---+--+--++-+-+++-+-+++-++---+--+++-+-+++--+-+-------+-++------++---+-+---++-++-++---+-+--+-+--+++++---+--+--++++-++-++--+--++\r\n", "output": "116291420\r\n"}, {"input": "289455627 906207104 512692624 154\r\n-------++--+++---++-++------++----------+--+++-+-+++---+---+++--++++++--+-+-+--+---+-+-++-++--+-++--++++---+-+---+-----+--+-+---------+++-++---++-+-+-----\r\n", "output": "48198216\r\n"}, {"input": "258833760 515657142 791267045 1\r\n-\r\n", "output": "935800888\r\n"}, {"input": "691617927 66917103 843055237 8\r\n--+++---\r\n", "output": "147768186\r\n"}, {"input": "379582849 362892355 986900829 50\r\n++-++---+-+++++--++++--+--++--++-----+------++--+-\r\n", "output": "927469713\r\n"}, {"input": "176799169 363368399 841293419 1\r\n+\r\n", "output": "746494802\r\n"}, {"input": "144808247 203038656 166324035 4\r\n-+-+\r\n", "output": "909066471\r\n"}, {"input": "477607531 177367565 20080950 2\r\n++\r\n", "output": "928662830\r\n"}, {"input": "682074525 289438443 917164266 1\r\n+\r\n", "output": "28048785\r\n"}, {"input": "938449224 59852396 219719125 1\r\n-\r\n", "output": "648647459\r\n"}, {"input": "395171426 872478622 193568600 147\r\n+---++---+-+--+++++--+---+-++++-+-++---++++--+--+-+-++-+-++--------++---+++-+---++---+---+-+--+-++++-+++-+-+-++-+--+++-++-+-+-+-++++++-+---+---++--\r\n", "output": "460881399\r\n"}, {"input": "403493428 317461491 556701240 1\r\n-\r\n", "output": "936516261\r\n"}, {"input": "917751169 330191895 532837377 70\r\n-+-+++++++--++---++-+++++-+++-----+-+++---+--+-+-++-++-+-+-++-++-+----\r\n", "output": "908035409\r\n"}, {"input": "252089413 552678586 938424519 1\r\n-\r\n", "output": "627032736\r\n"}, {"input": "649316142 320010793 200197645 1\r\n-\r\n", "output": "323650777\r\n"}, {"input": "116399299 784781190 299072480 5\r\n++++-\r\n", "output": "754650814\r\n"}]
false
stdio
null
true
214/B
214
B
Python 3
TESTS
12
280
2,764,800
197079202
x=input() x=input().split() if "0" not in x: print("-1") else: total=0 base=sorted([i for i in x if int(i) %3 == 0],reverse=True) ones=sorted([i for i in x if int(i) %3 ==1],reverse=True) twos=sorted([i for i in x if int(i) %3 ==2],reverse=True) for i in x : total+=int(i) if total%3==0: res=sorted(base+ones+twos,reverse=True) elif total%3==1: if len(ones)>0: ones.pop(-1) res=sorted(base+ones+twos,reverse=True) else : twos.pop(-1) twos.pop(-1) res=sorted(base+ones+twos,reverse=True) else : if len(twos)>0: twos.pop(-1) res=sorted(base+ones+twos,reverse=True) else: ones.pop(-1) ones.pop(-1) res=sorted(base+ones+twos,reverse=True) print("".join(res))
101
186
15,052,800
192692892
# https://codeforces.com/problemset/problem/214/B n = int(input()) a = list(map(int,input().split())) a.sort(reverse=True) if a[-1] != 0: print(-1) exit() total = sum(a) if total % 3 != 0: t = total%3 for i in range(n - 1,-1,-1): if a[i]%3 == t: a = a[:i] + a[i + 1:] break else: cnt = 0 for i in range(n - 1, -1, -1): if a[i] % 3 == 3 - t: a = a[:i] + a[i + 1:] cnt += 1 if cnt == 2: break if a[0] == 0: print(0) else: print(''.join([str(i) for i in a]))
Codeforces Round 131 (Div. 2)
CF
2,012
2
256
Hometask
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set.
A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space.
On a single line print the answer to the problem. If such number does not exist, then you should print -1.
null
In the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
[{"input": "1\n0", "output": "0"}, {"input": "11\n3 4 5 4 5 3 5 3 4 4 0", "output": "5554443330"}, {"input": "8\n3 2 5 1 5 2 2 3", "output": "-1"}]
1,600
["brute force", "constructive algorithms", "greedy", "math"]
101
[{"input": "1\r\n0\r\n", "output": "0\r\n"}, {"input": "11\r\n3 4 5 4 5 3 5 3 4 4 0\r\n", "output": "5554443330\r\n"}, {"input": "8\r\n3 2 5 1 5 2 2 3\r\n", "output": "-1\r\n"}, {"input": "12\r\n5 3 3 3 2 5 5 1 2 1 4 1\r\n", "output": "-1\r\n"}, {"input": "8\r\n5 5 4 1 5 5 5 3\r\n", "output": "-1\r\n"}, {"input": "12\r\n3 1 2 3 2 0 2 2 2 0 2 3\r\n", "output": "33322222200\r\n"}, {"input": "12\r\n5 1 4 4 2 1 7 7 4 2 5 1\r\n", "output": "-1\r\n"}, {"input": "5\r\n3 6 1 6 2\r\n", "output": "-1\r\n"}, {"input": "11\r\n3 9 9 6 4 3 6 4 9 6 0\r\n", "output": "999666330\r\n"}, {"input": "5\r\n9 6 6 6 1\r\n", "output": "-1\r\n"}, {"input": "10\r\n2 0 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "10\r\n1 0 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n1 1 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 2 2 0\r\n", "output": "0\r\n"}, {"input": "6\r\n3 3 2 2 2 0\r\n", "output": "332220\r\n"}, {"input": "7\r\n3 3 2 2 2 2 0\r\n", "output": "332220\r\n"}, {"input": "6\r\n0 3 3 1 1 1\r\n", "output": "331110\r\n"}, {"input": "7\r\n0 3 3 1 1 1 1\r\n", "output": "331110\r\n"}, {"input": "7\r\n0 3 3 4 4 4 4\r\n", "output": "444330\r\n"}, {"input": "7\r\n0 3 3 2 2 4 4\r\n", "output": "4433220\r\n"}, {"input": "7\r\n4 2 3 3 0 0 0\r\n", "output": "4332000\r\n"}, {"input": "4\r\n1 1 0 3\r\n", "output": "30\r\n"}, {"input": "4\r\n3 0 2 2\r\n", "output": "30\r\n"}, {"input": "8\r\n3 3 3 5 5 0 0 0\r\n", "output": "333000\r\n"}, {"input": "8\r\n3 3 6 3 0 7 7 9\r\n", "output": "963330\r\n"}, {"input": "9\r\n1 2 3 4 5 6 7 8 9\r\n", "output": "-1\r\n"}, {"input": "9\r\n9 9 9 9 9 9 9 9 9\r\n", "output": "-1\r\n"}, {"input": "1\r\n0\r\n", "output": "0\r\n"}, {"input": "2\r\n9 0\r\n", "output": "90\r\n"}, {"input": "10\r\n3 0 2 2 2 2 2 2 2 2\r\n", "output": "32222220\r\n"}, {"input": "10\r\n3 0 1 1 1 1 1 1 1 1\r\n", "output": "31111110\r\n"}, {"input": "10\r\n3 0 4 4 4 4 4 4 4 4\r\n", "output": "44444430\r\n"}, {"input": "10\r\n2 0 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "10\r\n2 2 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n5 5 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n1 4 0\r\n", "output": "0\r\n"}, {"input": "3\r\n0 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n0 1 4 3\r\n", "output": "30\r\n"}, {"input": "3\r\n2 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n0 1 2 3\r\n", "output": "3210\r\n"}, {"input": "4\r\n1 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n8 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "2\r\n0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 8 5 6\r\n", "output": "600\r\n"}, {"input": "4\r\n5 8 3 0\r\n", "output": "30\r\n"}, {"input": "4\r\n1 4 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n0 0 1\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n1 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n0 0 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n0 0 4\r\n", "output": "0\r\n"}, {"input": "2\r\n0 1\r\n", "output": "0\r\n"}, {"input": "4\r\n1 1 0 0\r\n", "output": "0\r\n"}, {"input": "6\r\n2 2 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n3 2 5 0 0\r\n", "output": "300\r\n"}, {"input": "4\r\n5 3 2 0\r\n", "output": "30\r\n"}, {"input": "5\r\n0 0 0 2 2\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 0 0 1\r\n", "output": "0\r\n"}, {"input": "4\r\n0 3 5 8\r\n", "output": "30\r\n"}]
false
stdio
null
true
214/B
214
B
PyPy 3-64
TESTS
12
248
15,974,400
146266670
n=int(input()) a=sorted([*map(int,input().split())],reverse=1) if a[-1]==0: k=sum(a) l1=[] l2=[] F=1 for i in range(n): if a[i]%3==1:l1.append(a[i]) elif a[i]%3==2:l2.append(a[i]) if k%3==2: if l2:a.remove(l2[-1]) elif len(l1)>1: a.remove(l1[-1]) a.remove(l1[-2]) else:F=0 elif k%3==1: if l1:a.remove(l1[-1]) elif len(l2)>1: a.remove(l2[-1]) a.remove(l2[-2]) else:F=0 if F:print(''.join([str(i) for i in a])) else:print(0) else:print(-1)
101
342
14,131,200
219139150
def main(): n = int(input()) a = list(map(int, input().split())) sum_val = sum(a) m = 0 uno = [] dos = [] a.sort(reverse=True) if a[-1]: print(-1) return if sum_val % 3 == 0: if not a[0]: print(0) return for i in range(n): print(a[i], end="") print() elif sum_val % 3 == 1: k = 0 for i in range(n - 1, -1, -1): if a[i] % 3 == 2: dos.append(i) elif a[i] % 3 == 1: a[i] = -1 k = 1 break if not k: if len(dos) < 2: print(-1) return a[dos[0]] = -1 a[dos[1]] = -1 for i in range(n): if a[i] >= 0: uno.append(a[i]) if uno[0] == 0: print(0) else: for i in range(len(uno)): print(uno[i], end="") print() else: k = 0 for i in range(n - 1, -1, -1): if a[i] % 3 == 1: uno.append(i) elif a[i] % 3 == 2: a[i] = -1 k = 1 break if not k: if len(uno) < 2: print(-1) return a[uno[0]] = -1 a[uno[1]] = -1 for i in range(n): if a[i] >= 0: dos.append(a[i]) if dos[0] == 0: print(0) else: for i in range(len(dos)): print(dos[i], end="") print() if __name__ == "__main__": main()
Codeforces Round 131 (Div. 2)
CF
2,012
2
256
Hometask
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set.
A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space.
On a single line print the answer to the problem. If such number does not exist, then you should print -1.
null
In the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
[{"input": "1\n0", "output": "0"}, {"input": "11\n3 4 5 4 5 3 5 3 4 4 0", "output": "5554443330"}, {"input": "8\n3 2 5 1 5 2 2 3", "output": "-1"}]
1,600
["brute force", "constructive algorithms", "greedy", "math"]
101
[{"input": "1\r\n0\r\n", "output": "0\r\n"}, {"input": "11\r\n3 4 5 4 5 3 5 3 4 4 0\r\n", "output": "5554443330\r\n"}, {"input": "8\r\n3 2 5 1 5 2 2 3\r\n", "output": "-1\r\n"}, {"input": "12\r\n5 3 3 3 2 5 5 1 2 1 4 1\r\n", "output": "-1\r\n"}, {"input": "8\r\n5 5 4 1 5 5 5 3\r\n", "output": "-1\r\n"}, {"input": "12\r\n3 1 2 3 2 0 2 2 2 0 2 3\r\n", "output": "33322222200\r\n"}, {"input": "12\r\n5 1 4 4 2 1 7 7 4 2 5 1\r\n", "output": "-1\r\n"}, {"input": "5\r\n3 6 1 6 2\r\n", "output": "-1\r\n"}, {"input": "11\r\n3 9 9 6 4 3 6 4 9 6 0\r\n", "output": "999666330\r\n"}, {"input": "5\r\n9 6 6 6 1\r\n", "output": "-1\r\n"}, {"input": "10\r\n2 0 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "10\r\n1 0 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n1 1 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 2 2 0\r\n", "output": "0\r\n"}, {"input": "6\r\n3 3 2 2 2 0\r\n", "output": "332220\r\n"}, {"input": "7\r\n3 3 2 2 2 2 0\r\n", "output": "332220\r\n"}, {"input": "6\r\n0 3 3 1 1 1\r\n", "output": "331110\r\n"}, {"input": "7\r\n0 3 3 1 1 1 1\r\n", "output": "331110\r\n"}, {"input": "7\r\n0 3 3 4 4 4 4\r\n", "output": "444330\r\n"}, {"input": "7\r\n0 3 3 2 2 4 4\r\n", "output": "4433220\r\n"}, {"input": "7\r\n4 2 3 3 0 0 0\r\n", "output": "4332000\r\n"}, {"input": "4\r\n1 1 0 3\r\n", "output": "30\r\n"}, {"input": "4\r\n3 0 2 2\r\n", "output": "30\r\n"}, {"input": "8\r\n3 3 3 5 5 0 0 0\r\n", "output": "333000\r\n"}, {"input": "8\r\n3 3 6 3 0 7 7 9\r\n", "output": "963330\r\n"}, {"input": "9\r\n1 2 3 4 5 6 7 8 9\r\n", "output": "-1\r\n"}, {"input": "9\r\n9 9 9 9 9 9 9 9 9\r\n", "output": "-1\r\n"}, {"input": "1\r\n0\r\n", "output": "0\r\n"}, {"input": "2\r\n9 0\r\n", "output": "90\r\n"}, {"input": "10\r\n3 0 2 2 2 2 2 2 2 2\r\n", "output": "32222220\r\n"}, {"input": "10\r\n3 0 1 1 1 1 1 1 1 1\r\n", "output": "31111110\r\n"}, {"input": "10\r\n3 0 4 4 4 4 4 4 4 4\r\n", "output": "44444430\r\n"}, {"input": "10\r\n2 0 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "10\r\n2 2 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n5 5 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n1 4 0\r\n", "output": "0\r\n"}, {"input": "3\r\n0 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n0 1 4 3\r\n", "output": "30\r\n"}, {"input": "3\r\n2 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n0 1 2 3\r\n", "output": "3210\r\n"}, {"input": "4\r\n1 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n8 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "2\r\n0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 8 5 6\r\n", "output": "600\r\n"}, {"input": "4\r\n5 8 3 0\r\n", "output": "30\r\n"}, {"input": "4\r\n1 4 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n0 0 1\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n1 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n0 0 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n0 0 4\r\n", "output": "0\r\n"}, {"input": "2\r\n0 1\r\n", "output": "0\r\n"}, {"input": "4\r\n1 1 0 0\r\n", "output": "0\r\n"}, {"input": "6\r\n2 2 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n3 2 5 0 0\r\n", "output": "300\r\n"}, {"input": "4\r\n5 3 2 0\r\n", "output": "30\r\n"}, {"input": "5\r\n0 0 0 2 2\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 0 0 1\r\n", "output": "0\r\n"}, {"input": "4\r\n0 3 5 8\r\n", "output": "30\r\n"}]
false
stdio
null
true
214/B
214
B
PyPy 3
TESTS
12
310
12,697,600
151406922
import decimal from errno import ETIMEDOUT import heapq as hq import os import sys from collections import Counter as ctr, deque as dq etd=dq() from io import BytesIO, IOBase 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') from math import * from bisect import bisect_left as bl, insort from bisect import bisect_right as br from itertools import permutations inp = lambda: int(input()) li = lambda: list(map(int, input().split())) lb = lambda: list(map(int, input())) ls = lambda: list(input()) bi = lambda n: bin(n).replace("0b", "0"*(18-int(log(n,2))-1)) def yn(f): print('NYOE S'[f::2]) def main(__=1): for _ in range(__): n=inp() a=li() a.sort(reverse=True) if a[-1]!=0: print(-1) else: rmd1=[] rmd2=[] s=0 for i in range(n): t=a[i]%3 s=(s+t)%3 if t==1: rmd1.append(i) elif t==2: rmd2.append(i) if s==0: print("".join(map(str,a))) elif s==1: if len(rmd1)>0: a.pop(rmd1[-1]) elif len(rmd2)>1: a.pop(rmd2[-1]) a.pop(rmd2[-2]) else: a=[0] print("".join(map(str,a))) else: if len(rmd2)>0: a.pop(rmd2[-1]) elif len(rmd1)>1: a.pop(rmd1[-1]) a.pop(rmd1[-2]) else: a=[0] print("".join(map(str,a))) if __name__ == "__main__": main() #main(inp())
101
374
11,980,800
126147314
n = int(input()) a = list(map(int, input().split())) a.sort() if a[0]!=0: print(-1) exit() s = sum(a)%3 if s!=0: b = next((i for i, v in enumerate(a) if v%3==s), None) if b is None: del a[next((i for i, v in enumerate(a) if v%3==3-s), None)] del a[next((i for i, v in enumerate(a) if v%3==3-s), None)] else: del a[b] a.reverse() if a[0]==0: print(0) else: print(''.join(map(str,a)))
Codeforces Round 131 (Div. 2)
CF
2,012
2
256
Hometask
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set.
A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space.
On a single line print the answer to the problem. If such number does not exist, then you should print -1.
null
In the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
[{"input": "1\n0", "output": "0"}, {"input": "11\n3 4 5 4 5 3 5 3 4 4 0", "output": "5554443330"}, {"input": "8\n3 2 5 1 5 2 2 3", "output": "-1"}]
1,600
["brute force", "constructive algorithms", "greedy", "math"]
101
[{"input": "1\r\n0\r\n", "output": "0\r\n"}, {"input": "11\r\n3 4 5 4 5 3 5 3 4 4 0\r\n", "output": "5554443330\r\n"}, {"input": "8\r\n3 2 5 1 5 2 2 3\r\n", "output": "-1\r\n"}, {"input": "12\r\n5 3 3 3 2 5 5 1 2 1 4 1\r\n", "output": "-1\r\n"}, {"input": "8\r\n5 5 4 1 5 5 5 3\r\n", "output": "-1\r\n"}, {"input": "12\r\n3 1 2 3 2 0 2 2 2 0 2 3\r\n", "output": "33322222200\r\n"}, {"input": "12\r\n5 1 4 4 2 1 7 7 4 2 5 1\r\n", "output": "-1\r\n"}, {"input": "5\r\n3 6 1 6 2\r\n", "output": "-1\r\n"}, {"input": "11\r\n3 9 9 6 4 3 6 4 9 6 0\r\n", "output": "999666330\r\n"}, {"input": "5\r\n9 6 6 6 1\r\n", "output": "-1\r\n"}, {"input": "10\r\n2 0 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "10\r\n1 0 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n1 1 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 2 2 0\r\n", "output": "0\r\n"}, {"input": "6\r\n3 3 2 2 2 0\r\n", "output": "332220\r\n"}, {"input": "7\r\n3 3 2 2 2 2 0\r\n", "output": "332220\r\n"}, {"input": "6\r\n0 3 3 1 1 1\r\n", "output": "331110\r\n"}, {"input": "7\r\n0 3 3 1 1 1 1\r\n", "output": "331110\r\n"}, {"input": "7\r\n0 3 3 4 4 4 4\r\n", "output": "444330\r\n"}, {"input": "7\r\n0 3 3 2 2 4 4\r\n", "output": "4433220\r\n"}, {"input": "7\r\n4 2 3 3 0 0 0\r\n", "output": "4332000\r\n"}, {"input": "4\r\n1 1 0 3\r\n", "output": "30\r\n"}, {"input": "4\r\n3 0 2 2\r\n", "output": "30\r\n"}, {"input": "8\r\n3 3 3 5 5 0 0 0\r\n", "output": "333000\r\n"}, {"input": "8\r\n3 3 6 3 0 7 7 9\r\n", "output": "963330\r\n"}, {"input": "9\r\n1 2 3 4 5 6 7 8 9\r\n", "output": "-1\r\n"}, {"input": "9\r\n9 9 9 9 9 9 9 9 9\r\n", "output": "-1\r\n"}, {"input": "1\r\n0\r\n", "output": "0\r\n"}, {"input": "2\r\n9 0\r\n", "output": "90\r\n"}, {"input": "10\r\n3 0 2 2 2 2 2 2 2 2\r\n", "output": "32222220\r\n"}, {"input": "10\r\n3 0 1 1 1 1 1 1 1 1\r\n", "output": "31111110\r\n"}, {"input": "10\r\n3 0 4 4 4 4 4 4 4 4\r\n", "output": "44444430\r\n"}, {"input": "10\r\n2 0 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "10\r\n2 2 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n5 5 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n1 4 0\r\n", "output": "0\r\n"}, {"input": "3\r\n0 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n0 1 4 3\r\n", "output": "30\r\n"}, {"input": "3\r\n2 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n0 1 2 3\r\n", "output": "3210\r\n"}, {"input": "4\r\n1 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n8 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "2\r\n0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 8 5 6\r\n", "output": "600\r\n"}, {"input": "4\r\n5 8 3 0\r\n", "output": "30\r\n"}, {"input": "4\r\n1 4 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n0 0 1\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n1 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n0 0 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n0 0 4\r\n", "output": "0\r\n"}, {"input": "2\r\n0 1\r\n", "output": "0\r\n"}, {"input": "4\r\n1 1 0 0\r\n", "output": "0\r\n"}, {"input": "6\r\n2 2 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n3 2 5 0 0\r\n", "output": "300\r\n"}, {"input": "4\r\n5 3 2 0\r\n", "output": "30\r\n"}, {"input": "5\r\n0 0 0 2 2\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 0 0 1\r\n", "output": "0\r\n"}, {"input": "4\r\n0 3 5 8\r\n", "output": "30\r\n"}]
false
stdio
null
true
214/B
214
B
PyPy 3
TESTS
12
654
35,430,400
126614698
import sys from functools import lru_cache, cmp_to_key from heapq import merge, heapify, heappop, heappush from math import * from collections import defaultdict as dd, deque, Counter as C from itertools import combinations as comb, permutations as perm from bisect import bisect_left as bl, bisect_right as br, bisect, insort from time import perf_counter from fractions import Fraction import copy from copy import deepcopy import time starttime = time.time() mod = int(pow(10, 9) + 7) mod2 = 998244353 def data(): return sys.stdin.readline().strip() def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end) def L(): return list(sp()) def sl(): return list(ssp()) def sp(): return map(int, data().split()) def ssp(): return map(str, data().split()) def l1d(n, val=0): return [val for i in range(n)] def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)] try: # sys.setrecursionlimit(int(pow(10,6))) sys.stdin = open("input.txt", "r") # sys.stdout = open("output.txt", "w") except: pass def pmat(A): for ele in A: print(*ele,end="\n") n=L()[0] A=L() A.sort(reverse=True) # print(sum(A),A) if A[-1]!=0: print(-1) else: z=sum(A)%3 if z: x=[ele%3 for ele in A] # print(x) x.sort() if z in x: idx=0 for i in range(len(A)): if A[i]%3==z: idx=i del(A[idx]) for ele in A: print(ele,end="") exit() else: if not((z==1 and x.count(2)>=2) or (z==2 and x.count(1)>=2)): print(-1) exit() idx1=idx2=0 for i in range(len(A)): if A[i]%3==(3-z): idx2=idx1 idx1=i del(A[idx1]) del(A[idx2]) for ele in A: print(ele,end="") exit() for ele in A: print(ele,end="")
101
498
14,643,200
179134105
n=int(input()) arr=list(map(int,input().split())) a=[[],[],[]] c0=0 s=0 for i in arr: s+=i if i==0: c0+=1 a[i%3].append(i) if not c0: print(-1) exit() a[0].sort(reverse=True) a[1].sort(reverse=True) a[2].sort(reverse=True) inc=[] if s%3: if len(a[s%3]): a[s%3].pop() elif len(a[3-s%3])>=2: a[3-s%3].pop() a[3-s%3].pop() elif len(a[0]): print(int(''.join(map(str,a[0])))) else: print(-1) exit() inc+=a[0] inc+=a[1] inc+=a[2] inc.sort(reverse=True) print(int(''.join(map(str,inc))))
Codeforces Round 131 (Div. 2)
CF
2,012
2
256
Hometask
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set.
A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space.
On a single line print the answer to the problem. If such number does not exist, then you should print -1.
null
In the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
[{"input": "1\n0", "output": "0"}, {"input": "11\n3 4 5 4 5 3 5 3 4 4 0", "output": "5554443330"}, {"input": "8\n3 2 5 1 5 2 2 3", "output": "-1"}]
1,600
["brute force", "constructive algorithms", "greedy", "math"]
101
[{"input": "1\r\n0\r\n", "output": "0\r\n"}, {"input": "11\r\n3 4 5 4 5 3 5 3 4 4 0\r\n", "output": "5554443330\r\n"}, {"input": "8\r\n3 2 5 1 5 2 2 3\r\n", "output": "-1\r\n"}, {"input": "12\r\n5 3 3 3 2 5 5 1 2 1 4 1\r\n", "output": "-1\r\n"}, {"input": "8\r\n5 5 4 1 5 5 5 3\r\n", "output": "-1\r\n"}, {"input": "12\r\n3 1 2 3 2 0 2 2 2 0 2 3\r\n", "output": "33322222200\r\n"}, {"input": "12\r\n5 1 4 4 2 1 7 7 4 2 5 1\r\n", "output": "-1\r\n"}, {"input": "5\r\n3 6 1 6 2\r\n", "output": "-1\r\n"}, {"input": "11\r\n3 9 9 6 4 3 6 4 9 6 0\r\n", "output": "999666330\r\n"}, {"input": "5\r\n9 6 6 6 1\r\n", "output": "-1\r\n"}, {"input": "10\r\n2 0 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "10\r\n1 0 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n1 1 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 2 2 0\r\n", "output": "0\r\n"}, {"input": "6\r\n3 3 2 2 2 0\r\n", "output": "332220\r\n"}, {"input": "7\r\n3 3 2 2 2 2 0\r\n", "output": "332220\r\n"}, {"input": "6\r\n0 3 3 1 1 1\r\n", "output": "331110\r\n"}, {"input": "7\r\n0 3 3 1 1 1 1\r\n", "output": "331110\r\n"}, {"input": "7\r\n0 3 3 4 4 4 4\r\n", "output": "444330\r\n"}, {"input": "7\r\n0 3 3 2 2 4 4\r\n", "output": "4433220\r\n"}, {"input": "7\r\n4 2 3 3 0 0 0\r\n", "output": "4332000\r\n"}, {"input": "4\r\n1 1 0 3\r\n", "output": "30\r\n"}, {"input": "4\r\n3 0 2 2\r\n", "output": "30\r\n"}, {"input": "8\r\n3 3 3 5 5 0 0 0\r\n", "output": "333000\r\n"}, {"input": "8\r\n3 3 6 3 0 7 7 9\r\n", "output": "963330\r\n"}, {"input": "9\r\n1 2 3 4 5 6 7 8 9\r\n", "output": "-1\r\n"}, {"input": "9\r\n9 9 9 9 9 9 9 9 9\r\n", "output": "-1\r\n"}, {"input": "1\r\n0\r\n", "output": "0\r\n"}, {"input": "2\r\n9 0\r\n", "output": "90\r\n"}, {"input": "10\r\n3 0 2 2 2 2 2 2 2 2\r\n", "output": "32222220\r\n"}, {"input": "10\r\n3 0 1 1 1 1 1 1 1 1\r\n", "output": "31111110\r\n"}, {"input": "10\r\n3 0 4 4 4 4 4 4 4 4\r\n", "output": "44444430\r\n"}, {"input": "10\r\n2 0 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "10\r\n2 2 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n5 5 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n1 4 0\r\n", "output": "0\r\n"}, {"input": "3\r\n0 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n0 1 4 3\r\n", "output": "30\r\n"}, {"input": "3\r\n2 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n0 1 2 3\r\n", "output": "3210\r\n"}, {"input": "4\r\n1 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n8 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "2\r\n0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 8 5 6\r\n", "output": "600\r\n"}, {"input": "4\r\n5 8 3 0\r\n", "output": "30\r\n"}, {"input": "4\r\n1 4 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n0 0 1\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n1 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n0 0 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n0 0 4\r\n", "output": "0\r\n"}, {"input": "2\r\n0 1\r\n", "output": "0\r\n"}, {"input": "4\r\n1 1 0 0\r\n", "output": "0\r\n"}, {"input": "6\r\n2 2 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n3 2 5 0 0\r\n", "output": "300\r\n"}, {"input": "4\r\n5 3 2 0\r\n", "output": "30\r\n"}, {"input": "5\r\n0 0 0 2 2\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 0 0 1\r\n", "output": "0\r\n"}, {"input": "4\r\n0 3 5 8\r\n", "output": "30\r\n"}]
false
stdio
null
true
214/B
214
B
PyPy 3-64
TESTS
10
1,058
14,233,600
219139053
def ss(l): news = '' for i in l: news = news + str(i) return news n = int(input()) l = list(map(int, input().split())) unq = [] ctr = 0 for i in range(n): if l[i] == 0: ctr = ctr + 1 if l[i] != 0: unq.append(l[i]) l.sort() rl = l.copy() rl = rl[::-1] if ctr == 0: print(-1) else: if sum(l) % 3 == 0: print(ss(rl)) elif sum(l) % 3 == 1: removed = False for i in unq: if i % 3 == 1: rl.remove(i) removed = True break if not removed: cnt = 0 for i in unq: if i % 3 == 2 and cnt < 2: rl.remove(i) cnt += 1 if cnt < 2: print(-1) exit() print(ss(rl)) elif sum(l) % 3 == 2: removed = False for i in unq: if i % 3 == 2: rl.remove(i) removed = True break if not removed: cnt = 0 for i in unq: if i % 3 == 1 and cnt < 2: rl.remove(i) cnt += 1 if cnt < 2: print(-1) exit() print(ss(rl)) else: print(0)
101
498
14,848,000
220764480
n = int(input()) a = sorted(map(int, input().split()), reverse=1) if a[-1] == 0: k = sum(a) l1 = [] l2 = [] F = 1 for i in range(n): if a[i] % 3 == 1: l1.append(a[i]) elif a[i] % 3 == 2: l2.append(a[i]) if k % 3 == 2: if l2: a.remove(l2[-1]) elif len(l1) > 1: a.remove(l1[-1]) a.remove(l1[-2]) else: F = 0 elif k % 3 == 1: if l1: a.remove(l1[-1]) elif len(l2) > 1: a.remove(l2[-1]) a.remove(l2[-2]) else: F = 0 if F: print(int(''.join([str(i) for i in a]))) else: print(0) else: print(-1)
Codeforces Round 131 (Div. 2)
CF
2,012
2
256
Hometask
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set.
A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space.
On a single line print the answer to the problem. If such number does not exist, then you should print -1.
null
In the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
[{"input": "1\n0", "output": "0"}, {"input": "11\n3 4 5 4 5 3 5 3 4 4 0", "output": "5554443330"}, {"input": "8\n3 2 5 1 5 2 2 3", "output": "-1"}]
1,600
["brute force", "constructive algorithms", "greedy", "math"]
101
[{"input": "1\r\n0\r\n", "output": "0\r\n"}, {"input": "11\r\n3 4 5 4 5 3 5 3 4 4 0\r\n", "output": "5554443330\r\n"}, {"input": "8\r\n3 2 5 1 5 2 2 3\r\n", "output": "-1\r\n"}, {"input": "12\r\n5 3 3 3 2 5 5 1 2 1 4 1\r\n", "output": "-1\r\n"}, {"input": "8\r\n5 5 4 1 5 5 5 3\r\n", "output": "-1\r\n"}, {"input": "12\r\n3 1 2 3 2 0 2 2 2 0 2 3\r\n", "output": "33322222200\r\n"}, {"input": "12\r\n5 1 4 4 2 1 7 7 4 2 5 1\r\n", "output": "-1\r\n"}, {"input": "5\r\n3 6 1 6 2\r\n", "output": "-1\r\n"}, {"input": "11\r\n3 9 9 6 4 3 6 4 9 6 0\r\n", "output": "999666330\r\n"}, {"input": "5\r\n9 6 6 6 1\r\n", "output": "-1\r\n"}, {"input": "10\r\n2 0 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "10\r\n1 0 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n1 1 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 2 2 0\r\n", "output": "0\r\n"}, {"input": "6\r\n3 3 2 2 2 0\r\n", "output": "332220\r\n"}, {"input": "7\r\n3 3 2 2 2 2 0\r\n", "output": "332220\r\n"}, {"input": "6\r\n0 3 3 1 1 1\r\n", "output": "331110\r\n"}, {"input": "7\r\n0 3 3 1 1 1 1\r\n", "output": "331110\r\n"}, {"input": "7\r\n0 3 3 4 4 4 4\r\n", "output": "444330\r\n"}, {"input": "7\r\n0 3 3 2 2 4 4\r\n", "output": "4433220\r\n"}, {"input": "7\r\n4 2 3 3 0 0 0\r\n", "output": "4332000\r\n"}, {"input": "4\r\n1 1 0 3\r\n", "output": "30\r\n"}, {"input": "4\r\n3 0 2 2\r\n", "output": "30\r\n"}, {"input": "8\r\n3 3 3 5 5 0 0 0\r\n", "output": "333000\r\n"}, {"input": "8\r\n3 3 6 3 0 7 7 9\r\n", "output": "963330\r\n"}, {"input": "9\r\n1 2 3 4 5 6 7 8 9\r\n", "output": "-1\r\n"}, {"input": "9\r\n9 9 9 9 9 9 9 9 9\r\n", "output": "-1\r\n"}, {"input": "1\r\n0\r\n", "output": "0\r\n"}, {"input": "2\r\n9 0\r\n", "output": "90\r\n"}, {"input": "10\r\n3 0 2 2 2 2 2 2 2 2\r\n", "output": "32222220\r\n"}, {"input": "10\r\n3 0 1 1 1 1 1 1 1 1\r\n", "output": "31111110\r\n"}, {"input": "10\r\n3 0 4 4 4 4 4 4 4 4\r\n", "output": "44444430\r\n"}, {"input": "10\r\n2 0 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "10\r\n2 2 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n5 5 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n1 4 0\r\n", "output": "0\r\n"}, {"input": "3\r\n0 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n0 1 4 3\r\n", "output": "30\r\n"}, {"input": "3\r\n2 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n0 1 2 3\r\n", "output": "3210\r\n"}, {"input": "4\r\n1 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n8 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "2\r\n0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 8 5 6\r\n", "output": "600\r\n"}, {"input": "4\r\n5 8 3 0\r\n", "output": "30\r\n"}, {"input": "4\r\n1 4 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n0 0 1\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n1 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n0 0 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n0 0 4\r\n", "output": "0\r\n"}, {"input": "2\r\n0 1\r\n", "output": "0\r\n"}, {"input": "4\r\n1 1 0 0\r\n", "output": "0\r\n"}, {"input": "6\r\n2 2 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n3 2 5 0 0\r\n", "output": "300\r\n"}, {"input": "4\r\n5 3 2 0\r\n", "output": "30\r\n"}, {"input": "5\r\n0 0 0 2 2\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 0 0 1\r\n", "output": "0\r\n"}, {"input": "4\r\n0 3 5 8\r\n", "output": "30\r\n"}]
false
stdio
null
true
215/A
215
A
PyPy 3
TESTS
23
310
1,228,800
100833523
n = int(input()) pedal = list(map(int, input().split())) m = int(input()) rear = list(map(int, input().split())) solution = [] mx = rear[0]/pedal[0] for i in range(len(rear)): for j in range(len(pedal)): ratio = rear[i] / pedal[j] if ratio != int(ratio): continue else: if ratio > mx: solution.clear() solution.append(ratio) mx = ratio elif ratio == mx: solution.append(ratio) print(len(solution))
57
92
0
136241819
n=int(input()) arr1=list(map(int,input().split())) m=int(input()) arr2=list(map(int,input().split())) maxVal=0 for i in range(n): for j in range(m): x=arr2[j]/arr1[i] if x*x==int(x*x): maxVal=max(maxVal,x) #print(maxVal) ans=0 for i in range(n): for j in range(m): x=arr2[j]/arr1[i] if maxVal==x: ans+=1 print(ans)
Codeforces Round 132 (Div. 2)
CF
2,012
2
256
Bicycle Chain
Vasya's bicycle chain drive consists of two parts: n stars are attached to the pedal axle, m stars are attached to the rear wheel axle. The chain helps to rotate the rear wheel by transmitting the pedal rotation. We know that the i-th star on the pedal axle has ai (0 < a1 < a2 < ... < an) teeth, and the j-th star on the rear wheel axle has bj (0 < b1 < b2 < ... < bm) teeth. Any pair (i, j) (1 ≤ i ≤ n; 1 ≤ j ≤ m) is called a gear and sets the indexes of stars to which the chain is currently attached. Gear (i, j) has a gear ratio, equal to the value $$\frac{b_{j}}{a_{j}}$$. Since Vasya likes integers, he wants to find such gears (i, j), that their ratios are integers. On the other hand, Vasya likes fast driving, so among all "integer" gears (i, j) he wants to choose a gear with the maximum ratio. Help him to find the number of such gears. In the problem, fraction $$\frac{b_{j}}{a_{j}}$$ denotes division in real numbers, that is, no rounding is performed.
The first input line contains integer n (1 ≤ n ≤ 50) — the number of stars on the bicycle's pedal axle. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 104) in the order of strict increasing. The third input line contains integer m (1 ≤ m ≤ 50) — the number of stars on the rear wheel axle. The fourth line contains m integers b1, b2, ..., bm (1 ≤ bi ≤ 104) in the order of strict increasing. It is guaranteed that there exists at least one gear (i, j), that its gear ratio is an integer. The numbers on the lines are separated by spaces.
Print the number of "integer" gears with the maximum ratio among all "integer" gears.
null
In the first sample the maximum "integer" gear ratio equals 3. There are two gears that have such gear ratio. For one of them a1 = 4, b1 = 12, and for the other a2 = 5, b3 = 15.
[{"input": "2\n4 5\n3\n12 13 15", "output": "2"}, {"input": "4\n1 2 3 4\n5\n10 11 12 13 14", "output": "1"}]
900
["brute force", "implementation"]
57
[{"input": "2\r\n4 5\r\n3\r\n12 13 15\r\n", "output": "2\r\n"}, {"input": "4\r\n1 2 3 4\r\n5\r\n10 11 12 13 14\r\n", "output": "1\r\n"}, {"input": "1\r\n1\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "2\r\n1 2\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n1\r\n2\r\n1 2\r\n", "output": "1\r\n"}, {"input": "4\r\n3 7 11 13\r\n4\r\n51 119 187 221\r\n", "output": "4\r\n"}, {"input": "4\r\n2 3 4 5\r\n3\r\n1 2 3\r\n", "output": "2\r\n"}, {"input": "10\r\n6 12 13 20 48 53 74 92 96 97\r\n10\r\n1 21 32 36 47 54 69 75 95 97\r\n", "output": "1\r\n"}, {"input": "10\r\n5 9 10 14 15 17 19 22 24 26\r\n10\r\n2 11 17 19 21 22 24 25 27 28\r\n", "output": "1\r\n"}, {"input": "10\r\n24 53 56 126 354 432 442 740 795 856\r\n10\r\n273 438 494 619 689 711 894 947 954 958\r\n", "output": "1\r\n"}, {"input": "10\r\n3 4 6 7 8 10 14 16 19 20\r\n10\r\n3 4 5 7 8 10 15 16 18 20\r\n", "output": "1\r\n"}, {"input": "10\r\n1 6 8 14 15 17 25 27 34 39\r\n10\r\n1 8 16 17 19 22 32 39 44 50\r\n", "output": "1\r\n"}, {"input": "10\r\n5 21 22 23 25 32 35 36 38 39\r\n10\r\n3 7 8 9 18 21 23 24 36 38\r\n", "output": "4\r\n"}, {"input": "10\r\n17 239 443 467 661 1069 1823 2333 3767 4201\r\n20\r\n51 83 97 457 593 717 997 1329 1401 1459 1471 1983 2371 2539 3207 3251 3329 5469 6637 6999\r\n", "output": "8\r\n"}, {"input": "20\r\n179 359 401 467 521 601 919 941 1103 1279 1709 1913 1949 2003 2099 2143 2179 2213 2399 4673\r\n20\r\n151 181 191 251 421 967 1109 1181 1249 1447 1471 1553 1619 2327 2551 2791 3049 3727 6071 7813\r\n", "output": "3\r\n"}, {"input": "20\r\n79 113 151 709 809 983 1291 1399 1409 1429 2377 2659 2671 2897 3217 3511 3557 3797 3823 4363\r\n10\r\n19 101 659 797 1027 1963 2129 2971 3299 9217\r\n", "output": "3\r\n"}, {"input": "30\r\n19 47 109 179 307 331 389 401 461 509 547 569 617 853 883 1249 1361 1381 1511 1723 1741 1783 2459 2531 2621 3533 3821 4091 5557 6217\r\n20\r\n401 443 563 941 967 997 1535 1567 1655 1747 1787 1945 1999 2251 2305 2543 2735 4415 6245 7555\r\n", "output": "8\r\n"}, {"input": "5\r\n33 78 146 3055 4268\r\n5\r\n2211 2584 5226 9402 9782\r\n", "output": "3\r\n"}, {"input": "5\r\n35 48 52 86 8001\r\n10\r\n332 3430 3554 4704 4860 5096 6215 7583 8228 8428\r\n", "output": "4\r\n"}, {"input": "10\r\n97 184 207 228 269 2084 4450 6396 7214 9457\r\n16\r\n338 1179 1284 1545 1570 2444 3167 3395 3397 5550 6440 7245 7804 7980 9415 9959\r\n", "output": "5\r\n"}, {"input": "30\r\n25 30 41 57 58 62 70 72 76 79 84 85 88 91 98 101 104 109 119 129 136 139 148 151 926 1372 3093 3936 5423 7350\r\n25\r\n1600 1920 2624 3648 3712 3968 4480 4608 4864 5056 5376 5440 5632 5824 6272 6464 6656 6934 6976 7616 8256 8704 8896 9472 9664\r\n", "output": "24\r\n"}, {"input": "5\r\n33 78 146 3055 4268\r\n5\r\n2211 2584 5226 9402 9782\r\n", "output": "3\r\n"}, {"input": "5\r\n35 48 52 86 8001\r\n10\r\n332 3430 3554 4704 4860 5096 6215 7583 8228 8428\r\n", "output": "4\r\n"}, {"input": "10\r\n97 184 207 228 269 2084 4450 6396 7214 9457\r\n16\r\n338 1179 1284 1545 1570 2444 3167 3395 3397 5550 6440 7245 7804 7980 9415 9959\r\n", "output": "5\r\n"}, {"input": "30\r\n25 30 41 57 58 62 70 72 76 79 84 85 88 91 98 101 104 109 119 129 136 139 148 151 926 1372 3093 3936 5423 7350\r\n25\r\n1600 1920 2624 3648 3712 3968 4480 4608 4864 5056 5376 5440 5632 5824 6272 6464 6656 6934 6976 7616 8256 8704 8896 9472 9664\r\n", "output": "24\r\n"}, {"input": "50\r\n51 67 75 186 194 355 512 561 720 876 1077 1221 1503 1820 2153 2385 2568 2608 2937 2969 3271 3311 3481 4081 4093 4171 4255 4256 4829 5020 5192 5636 5817 6156 6712 6717 7153 7436 7608 7612 7866 7988 8264 8293 8867 9311 9879 9882 9889 9908\r\n1\r\n5394\r\n", "output": "1\r\n"}, {"input": "3\r\n3 4 5\r\n3\r\n6 20 25\r\n", "output": "2\r\n"}, {"input": "4\r\n2 3 5 8\r\n4\r\n2 6 8 10\r\n", "output": "1\r\n"}, {"input": "4\r\n3 5 7 11\r\n4\r\n3 5 7 22\r\n", "output": "1\r\n"}, {"input": "2\r\n2 3\r\n3\r\n20 30 50\r\n", "output": "1\r\n"}, {"input": "3\r\n1 2 3\r\n4\r\n2 4 6 49\r\n", "output": "1\r\n"}, {"input": "2\r\n4 5\r\n3\r\n12 15 20\r\n", "output": "1\r\n"}, {"input": "3\r\n2 5 7\r\n3\r\n4 5 7\r\n", "output": "1\r\n"}, {"input": "3\r\n3 5 8\r\n3\r\n6 8 10\r\n", "output": "2\r\n"}, {"input": "2\r\n2 3\r\n4\r\n4 6 9 33\r\n", "output": "1\r\n"}, {"input": "2\r\n2 3\r\n4\r\n4 6 21 40\r\n", "output": "1\r\n"}, {"input": "3\r\n4 9 10\r\n3\r\n8 9 10\r\n", "output": "1\r\n"}, {"input": "5\r\n1 5 6 9 51\r\n5\r\n5 12 18 27 10000\r\n", "output": "1\r\n"}, {"input": "13\r\n1 2 3 4 5 6 7 8 9 10 11 12 13\r\n1\r\n14\r\n", "output": "1\r\n"}]
false
stdio
null
true
963/A
963
A
Python 3
PRETESTS
8
124
7,065,600
37401099
if __name__ == "__main__": a=[int(x) for x in input().split()] N=a[0] A=a[1] B=a[2] K=a[3] s=input() mod = 1000000009 Q = pow(B*pow(A,mod-2,mod)%mod,K,mod) D = (pow(Q,(N+1)//K,mod)-1)*pow(Q-1,mod-2,mod)%mod ans=0 for i in range(K): C=pow(B,i,mod)*pow(A,N-i,mod)%mod if s[i]=='+': ans=(ans+C*D)%mod else: ans=(ans-C*D)%mod print((ans%mod+mod)%mod)
71
218
614,400
38464037
def pow_mod(x, y, p): number = 1 while y: if y & 1: number = number * x % p y >>= 1 x = x * x % p return number % p def inv(x, p): if 1 < x: return p - inv(p % x, x) * p // x return 1 def v(p, a, b, k): i = 1 while (pow_mod(a, k, (10 ** 9 + 9) ** i) - pow_mod(b, k, (10 ** 9 + 9) ** i)) == 0: i += 1 return i-1 def main(): p = 10 ** 9 + 9 n, a, b, k = map(int, input().split()) S = list(input()) for i in range(k): if S[i] == '+': S[i] = 1 else: S[i] = -1 s=0 if a != b: vp = p ** v(p, a, b, k) sum_mod = ((pow_mod(a, (n + 1), p * vp) - pow_mod(b, (n + 1), p * vp)) // vp) * inv(((pow_mod(a, k, p * vp) - pow_mod(b, k, p * vp)) // vp) % p, p) pa = pow_mod(a, k - 1, p) pb = 1 inv_a = inv(a, p) for i in range(k): s += (S[i] * pa * pb * sum_mod) % p pa = (pa * inv_a) % p pb = (pb * b) % p else: for i in range(k): s += S[i] * (n + 1) // k s *= pow_mod(a, n, p) s %= p print(s) main() #def giant_steps(start, target, n=2): #L = [target] #while L[-1] > start*n: #L = L + [L[-1]//n + 2] #return L[::-1] #def rshift(x, n): #if n >= 0: return x >> n #else: return x << (-n) #def lshift(x, n): #if n >= 0: return x << n #else: return x >> (-n) #def size(x): #return str(x).count('2') #def newdiv(p, q): #szp = size(p) #szq = size(q) #szr = szp - szq #if min(szp, szq, szr) < 2*START_PREC: #return p//q #r = (1 << (2*START_PREC)) // (q >> (szq - START_PREC)) #last_prec = START_PREC #for prec in giant_steps(START_PREC, szr): #a = lshift(r, prec-last_prec+1) #b = rshift(r**2 * rshift(q, szq-prec), 2*last_prec) #r = a - b #last_prec = prec #return ((p >> szq) * r) >> szr
Tinkoff Internship Warmup Round 2018 and Codeforces Round 475 (Div. 1)
CF
2,018
1
256
Alternating Sum
You are given two integers $$$a$$$ and $$$b$$$. Moreover, you are given a sequence $$$s_0, s_1, \dots, s_{n}$$$. All values in $$$s$$$ are integers $$$1$$$ or $$$-1$$$. It's known that sequence is $$$k$$$-periodic and $$$k$$$ divides $$$n+1$$$. In other words, for each $$$k \leq i \leq n$$$ it's satisfied that $$$s_{i} = s_{i - k}$$$. Find out the non-negative remainder of division of $$$\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i}$$$ by $$$10^{9} + 9$$$. Note that the modulo is unusual!
The first line contains four integers $$$n, a, b$$$ and $$$k$$$ $$$(1 \leq n \leq 10^{9}, 1 \leq a, b \leq 10^{9}, 1 \leq k \leq 10^{5})$$$. The second line contains a sequence of length $$$k$$$ consisting of characters '+' and '-'. If the $$$i$$$-th character (0-indexed) is '+', then $$$s_{i} = 1$$$, otherwise $$$s_{i} = -1$$$. Note that only the first $$$k$$$ members of the sequence are given, the rest can be obtained using the periodicity property.
Output a single integer — value of given expression modulo $$$10^{9} + 9$$$.
null
In the first example: $$$(\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i})$$$ = $$$2^{2} 3^{0} - 2^{1} 3^{1} + 2^{0} 3^{2}$$$ = 7 In the second example: $$$(\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i}) = -1^{4} 5^{0} - 1^{3} 5^{1} - 1^{2} 5^{2} - 1^{1} 5^{3} - 1^{0} 5^{4} = -781 \equiv 999999228 \pmod{10^{9} + 9}$$$.
[{"input": "2 2 3 3\n+-+", "output": "7"}, {"input": "4 1 5 1\n-", "output": "999999228"}]
1,800
["math", "number theory"]
71
[{"input": "2 2 3 3\r\n+-+\r\n", "output": "7\r\n"}, {"input": "4 1 5 1\r\n-\r\n", "output": "999999228\r\n"}, {"input": "1 1 4 2\r\n-+\r\n", "output": "3\r\n"}, {"input": "3 1 4 4\r\n+--+\r\n", "output": "45\r\n"}, {"input": "5 1 1 6\r\n++---+\r\n", "output": "0\r\n"}, {"input": "5 2 2 6\r\n+--++-\r\n", "output": "0\r\n"}, {"input": "686653196 115381398 884618610 3\r\n+-+\r\n", "output": "542231211\r\n"}, {"input": "608663287 430477711 172252358 8\r\n-+--+-+-\r\n", "output": "594681696\r\n"}, {"input": "904132655 827386249 118827660 334\r\n+++-+++++--+++----+-+-+-+-+--+-+---++--++--++--+-+-+++-+++--+-+-+----+-+-++++-----+--++++------+++-+-+-++-++++++++-+-++-+++--+--++------+--+-+++--++--+---++-++-+-+-++---++-++--+-+-++-+------+-+----+++-+++--+-+-+--+--+--+------+--+---+--+-++--+++---+-+-++--------+-++--++-+-+-+-+-+-+--+-++++-+++--+--++----+--+-++-++--+--+-+-++-+-++++-\r\n", "output": "188208979\r\n"}, {"input": "234179195 430477711 115381398 12\r\n++++-+-+-+++\r\n", "output": "549793323\r\n"}, {"input": "75952547 967294208 907708706 252\r\n++--++--+++-+-+--++--++++++---+++-++-+-----++++--++-+-++------+-+-+-++-+-+-++++------++---+-++++---+-+-++++--++++++--+-+++-++--+--+---++++---+-+++-+++--+-+--+++++---+--++-++++--++++-+-++-+++-++-----+-+++++----++--+++-+-+++++-+--++-++-+--+-++++--+-+-+-+\r\n", "output": "605712499\r\n"}, {"input": "74709071 801809249 753674746 18\r\n++++++-+-+---+-+--\r\n", "output": "13414893\r\n"}, {"input": "743329 973758 92942 82\r\n++----+-++++----+--+++---+--++++-+-+---+++++--+--+++++++--++-+++----+--+++++-+--+-\r\n", "output": "299311566\r\n"}, {"input": "18111 291387 518587 2\r\n++\r\n", "output": "724471355\r\n"}, {"input": "996144 218286 837447 1\r\n-\r\n", "output": "549104837\r\n"}, {"input": "179358 828426 548710 67\r\n++++---+--++----+-+-++++----+--+---+------++-+-++++--+----+---+-+--\r\n", "output": "759716474\r\n"}, {"input": "397521 174985 279760 1\r\n+\r\n", "output": "25679493\r\n"}, {"input": "613632 812232 482342 1\r\n-\r\n", "output": "891965141\r\n"}, {"input": "936810 183454 647048 1\r\n+\r\n", "output": "523548992\r\n"}, {"input": "231531 250371 921383 28\r\n++-+------+--+--++++--+-+++-\r\n", "output": "134450934\r\n"}, {"input": "947301 87242 360762 97\r\n--+++--+++-++--++-++--++--+++---+++--++++--+++++--+-++-++-----+-++-+--++-----+-++-+--++-++-+-----\r\n", "output": "405016159\r\n"}, {"input": "425583346 814209084 570987274 1\r\n+\r\n", "output": "63271171\r\n"}, {"input": "354062556 688076879 786825319 1\r\n+\r\n", "output": "545304776\r\n"}, {"input": "206671954 13571766 192250278 1\r\n+\r\n", "output": "717117421\r\n"}, {"input": "23047921 621656196 160244047 1\r\n-\r\n", "output": "101533009\r\n"}, {"input": "806038018 740585177 987616107 293\r\n-+++++--++++---++-+--+-+---+-++++--+--+++--++---++++++++--+++++-+-++-+--+----+--+++-+-++-+++-+-+-+----------++-+-+++++++-+-+-+-++---+++-+-+-------+-+-++--++-++-++-++-+---+--++-++--+++--+++-+-+----++--+-+-++-+---+---+-+-+++------+-+++-+---++-+--+++----+++++---++-++--+----+++-+--+++-+------+-++\r\n", "output": "441468166\r\n"}, {"input": "262060935 184120408 148332034 148\r\n+--+-------+-+-+--++-+++--++-+-++++++--++-+++-+++--+-------+-+--+++-+-+-+---++-++-+-++---+--+-+-+--+------+++--+--+-+-+---+---+-+-++++---+++--+++---\r\n", "output": "700325386\r\n"}, {"input": "919350941 654611542 217223605 186\r\n++-++-+++++-+++--+---+++++++-++-+----+-++--+-++--++--+++-+++---+--+--++-+-+++-+-+++-++---+--+++-+-+++--+-+-------+-++------++---+-+---++-++-++---+-+--+-+--+++++---+--+--++++-++-++--+--++\r\n", "output": "116291420\r\n"}, {"input": "289455627 906207104 512692624 154\r\n-------++--+++---++-++------++----------+--+++-+-+++---+---+++--++++++--+-+-+--+---+-+-++-++--+-++--++++---+-+---+-----+--+-+---------+++-++---++-+-+-----\r\n", "output": "48198216\r\n"}, {"input": "258833760 515657142 791267045 1\r\n-\r\n", "output": "935800888\r\n"}, {"input": "691617927 66917103 843055237 8\r\n--+++---\r\n", "output": "147768186\r\n"}, {"input": "379582849 362892355 986900829 50\r\n++-++---+-+++++--++++--+--++--++-----+------++--+-\r\n", "output": "927469713\r\n"}, {"input": "176799169 363368399 841293419 1\r\n+\r\n", "output": "746494802\r\n"}, {"input": "144808247 203038656 166324035 4\r\n-+-+\r\n", "output": "909066471\r\n"}, {"input": "477607531 177367565 20080950 2\r\n++\r\n", "output": "928662830\r\n"}, {"input": "682074525 289438443 917164266 1\r\n+\r\n", "output": "28048785\r\n"}, {"input": "938449224 59852396 219719125 1\r\n-\r\n", "output": "648647459\r\n"}, {"input": "395171426 872478622 193568600 147\r\n+---++---+-+--+++++--+---+-++++-+-++---++++--+--+-+-++-+-++--------++---+++-+---++---+---+-+--+-++++-+++-+-+-++-+--+++-++-+-+-+-++++++-+---+---++--\r\n", "output": "460881399\r\n"}, {"input": "403493428 317461491 556701240 1\r\n-\r\n", "output": "936516261\r\n"}, {"input": "917751169 330191895 532837377 70\r\n-+-+++++++--++---++-+++++-+++-----+-+++---+--+-+-++-++-+-+-++-++-+----\r\n", "output": "908035409\r\n"}, {"input": "252089413 552678586 938424519 1\r\n-\r\n", "output": "627032736\r\n"}, {"input": "649316142 320010793 200197645 1\r\n-\r\n", "output": "323650777\r\n"}, {"input": "116399299 784781190 299072480 5\r\n++++-\r\n", "output": "754650814\r\n"}]
false
stdio
null
true
963/A
963
A
PyPy 3
TESTS
8
108
21,606,400
122325947
import sys input=sys.stdin.readline n,a,b,k=map(int,input().split()) s=input().rstrip() mod=10**9+9 pa=pow(a,n,mod) inv_a=pow(a,mod-2,mod) pb=1 xk=0 for i in range(k): if s[i]=="+": xk+=pa*pb%mod xk%=mod else: xk-=pa*pb%mod xk%=mod pa*=inv_a pa%=mod pb*=b pb%=mod y=(1-pow(inv_a*b%mod,n+1,mod))%mod z=(1-pow(inv_a*b%mod,k,mod))%mod z=pow(z,mod-2,mod) print(xk*y*z%mod)
71
248
1,945,600
102992725
inp = input("").split('\n') nums = inp[0].split(' ') n = int(nums[0]) a = int(nums[1]) b = int(nums[2]) k = int(nums[3]) st = str(input("").strip()) tot = 0 mod = 10**9 + 9 def inv (a): return pow (a, mod - 2, mod) for i in range (0, k): vl = 1 if (st[i] == '-'): vl = -1 tot += vl * pow(a, n - i, mod) * pow(b, i, mod) tot %= mod mult = (pow(b, k, mod) * inv(pow(a, k, mod))) % mod p = ((n + 1) // k) - 1 # tot + tot * mult + .... tot * mult^p if (mult == 1): print((tot * (p + 1)) % mod) else: num = (pow(mult, p + 1, mod) - 1) % mod den = (mult - 1) % mod num = (num * inv(den)) % mod print((num * tot)%mod)
Tinkoff Internship Warmup Round 2018 and Codeforces Round 475 (Div. 1)
CF
2,018
1
256
Alternating Sum
You are given two integers $$$a$$$ and $$$b$$$. Moreover, you are given a sequence $$$s_0, s_1, \dots, s_{n}$$$. All values in $$$s$$$ are integers $$$1$$$ or $$$-1$$$. It's known that sequence is $$$k$$$-periodic and $$$k$$$ divides $$$n+1$$$. In other words, for each $$$k \leq i \leq n$$$ it's satisfied that $$$s_{i} = s_{i - k}$$$. Find out the non-negative remainder of division of $$$\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i}$$$ by $$$10^{9} + 9$$$. Note that the modulo is unusual!
The first line contains four integers $$$n, a, b$$$ and $$$k$$$ $$$(1 \leq n \leq 10^{9}, 1 \leq a, b \leq 10^{9}, 1 \leq k \leq 10^{5})$$$. The second line contains a sequence of length $$$k$$$ consisting of characters '+' and '-'. If the $$$i$$$-th character (0-indexed) is '+', then $$$s_{i} = 1$$$, otherwise $$$s_{i} = -1$$$. Note that only the first $$$k$$$ members of the sequence are given, the rest can be obtained using the periodicity property.
Output a single integer — value of given expression modulo $$$10^{9} + 9$$$.
null
In the first example: $$$(\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i})$$$ = $$$2^{2} 3^{0} - 2^{1} 3^{1} + 2^{0} 3^{2}$$$ = 7 In the second example: $$$(\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i}) = -1^{4} 5^{0} - 1^{3} 5^{1} - 1^{2} 5^{2} - 1^{1} 5^{3} - 1^{0} 5^{4} = -781 \equiv 999999228 \pmod{10^{9} + 9}$$$.
[{"input": "2 2 3 3\n+-+", "output": "7"}, {"input": "4 1 5 1\n-", "output": "999999228"}]
1,800
["math", "number theory"]
71
[{"input": "2 2 3 3\r\n+-+\r\n", "output": "7\r\n"}, {"input": "4 1 5 1\r\n-\r\n", "output": "999999228\r\n"}, {"input": "1 1 4 2\r\n-+\r\n", "output": "3\r\n"}, {"input": "3 1 4 4\r\n+--+\r\n", "output": "45\r\n"}, {"input": "5 1 1 6\r\n++---+\r\n", "output": "0\r\n"}, {"input": "5 2 2 6\r\n+--++-\r\n", "output": "0\r\n"}, {"input": "686653196 115381398 884618610 3\r\n+-+\r\n", "output": "542231211\r\n"}, {"input": "608663287 430477711 172252358 8\r\n-+--+-+-\r\n", "output": "594681696\r\n"}, {"input": "904132655 827386249 118827660 334\r\n+++-+++++--+++----+-+-+-+-+--+-+---++--++--++--+-+-+++-+++--+-+-+----+-+-++++-----+--++++------+++-+-+-++-++++++++-+-++-+++--+--++------+--+-+++--++--+---++-++-+-+-++---++-++--+-+-++-+------+-+----+++-+++--+-+-+--+--+--+------+--+---+--+-++--+++---+-+-++--------+-++--++-+-+-+-+-+-+--+-++++-+++--+--++----+--+-++-++--+--+-+-++-+-++++-\r\n", "output": "188208979\r\n"}, {"input": "234179195 430477711 115381398 12\r\n++++-+-+-+++\r\n", "output": "549793323\r\n"}, {"input": "75952547 967294208 907708706 252\r\n++--++--+++-+-+--++--++++++---+++-++-+-----++++--++-+-++------+-+-+-++-+-+-++++------++---+-++++---+-+-++++--++++++--+-+++-++--+--+---++++---+-+++-+++--+-+--+++++---+--++-++++--++++-+-++-+++-++-----+-+++++----++--+++-+-+++++-+--++-++-+--+-++++--+-+-+-+\r\n", "output": "605712499\r\n"}, {"input": "74709071 801809249 753674746 18\r\n++++++-+-+---+-+--\r\n", "output": "13414893\r\n"}, {"input": "743329 973758 92942 82\r\n++----+-++++----+--+++---+--++++-+-+---+++++--+--+++++++--++-+++----+--+++++-+--+-\r\n", "output": "299311566\r\n"}, {"input": "18111 291387 518587 2\r\n++\r\n", "output": "724471355\r\n"}, {"input": "996144 218286 837447 1\r\n-\r\n", "output": "549104837\r\n"}, {"input": "179358 828426 548710 67\r\n++++---+--++----+-+-++++----+--+---+------++-+-++++--+----+---+-+--\r\n", "output": "759716474\r\n"}, {"input": "397521 174985 279760 1\r\n+\r\n", "output": "25679493\r\n"}, {"input": "613632 812232 482342 1\r\n-\r\n", "output": "891965141\r\n"}, {"input": "936810 183454 647048 1\r\n+\r\n", "output": "523548992\r\n"}, {"input": "231531 250371 921383 28\r\n++-+------+--+--++++--+-+++-\r\n", "output": "134450934\r\n"}, {"input": "947301 87242 360762 97\r\n--+++--+++-++--++-++--++--+++---+++--++++--+++++--+-++-++-----+-++-+--++-----+-++-+--++-++-+-----\r\n", "output": "405016159\r\n"}, {"input": "425583346 814209084 570987274 1\r\n+\r\n", "output": "63271171\r\n"}, {"input": "354062556 688076879 786825319 1\r\n+\r\n", "output": "545304776\r\n"}, {"input": "206671954 13571766 192250278 1\r\n+\r\n", "output": "717117421\r\n"}, {"input": "23047921 621656196 160244047 1\r\n-\r\n", "output": "101533009\r\n"}, {"input": "806038018 740585177 987616107 293\r\n-+++++--++++---++-+--+-+---+-++++--+--+++--++---++++++++--+++++-+-++-+--+----+--+++-+-++-+++-+-+-+----------++-+-+++++++-+-+-+-++---+++-+-+-------+-+-++--++-++-++-++-+---+--++-++--+++--+++-+-+----++--+-+-++-+---+---+-+-+++------+-+++-+---++-+--+++----+++++---++-++--+----+++-+--+++-+------+-++\r\n", "output": "441468166\r\n"}, {"input": "262060935 184120408 148332034 148\r\n+--+-------+-+-+--++-+++--++-+-++++++--++-+++-+++--+-------+-+--+++-+-+-+---++-++-+-++---+--+-+-+--+------+++--+--+-+-+---+---+-+-++++---+++--+++---\r\n", "output": "700325386\r\n"}, {"input": "919350941 654611542 217223605 186\r\n++-++-+++++-+++--+---+++++++-++-+----+-++--+-++--++--+++-+++---+--+--++-+-+++-+-+++-++---+--+++-+-+++--+-+-------+-++------++---+-+---++-++-++---+-+--+-+--+++++---+--+--++++-++-++--+--++\r\n", "output": "116291420\r\n"}, {"input": "289455627 906207104 512692624 154\r\n-------++--+++---++-++------++----------+--+++-+-+++---+---+++--++++++--+-+-+--+---+-+-++-++--+-++--++++---+-+---+-----+--+-+---------+++-++---++-+-+-----\r\n", "output": "48198216\r\n"}, {"input": "258833760 515657142 791267045 1\r\n-\r\n", "output": "935800888\r\n"}, {"input": "691617927 66917103 843055237 8\r\n--+++---\r\n", "output": "147768186\r\n"}, {"input": "379582849 362892355 986900829 50\r\n++-++---+-+++++--++++--+--++--++-----+------++--+-\r\n", "output": "927469713\r\n"}, {"input": "176799169 363368399 841293419 1\r\n+\r\n", "output": "746494802\r\n"}, {"input": "144808247 203038656 166324035 4\r\n-+-+\r\n", "output": "909066471\r\n"}, {"input": "477607531 177367565 20080950 2\r\n++\r\n", "output": "928662830\r\n"}, {"input": "682074525 289438443 917164266 1\r\n+\r\n", "output": "28048785\r\n"}, {"input": "938449224 59852396 219719125 1\r\n-\r\n", "output": "648647459\r\n"}, {"input": "395171426 872478622 193568600 147\r\n+---++---+-+--+++++--+---+-++++-+-++---++++--+--+-+-++-+-++--------++---+++-+---++---+---+-+--+-++++-+++-+-+-++-+--+++-++-+-+-+-++++++-+---+---++--\r\n", "output": "460881399\r\n"}, {"input": "403493428 317461491 556701240 1\r\n-\r\n", "output": "936516261\r\n"}, {"input": "917751169 330191895 532837377 70\r\n-+-+++++++--++---++-+++++-+++-----+-+++---+--+-+-++-++-+-+-++-++-+----\r\n", "output": "908035409\r\n"}, {"input": "252089413 552678586 938424519 1\r\n-\r\n", "output": "627032736\r\n"}, {"input": "649316142 320010793 200197645 1\r\n-\r\n", "output": "323650777\r\n"}, {"input": "116399299 784781190 299072480 5\r\n++++-\r\n", "output": "754650814\r\n"}]
false
stdio
null
true
532/E
533
E
PyPy 3
TESTS
18
124
0
56360620
R = lambda: map(int, input().split()) n = int(input()) s1 = input() + '00' s2 = input() + '00' for i in range(len(s1)): if s1[i] != s2[i]: if s1[i + 1:] == s2[i + 1:]: print(2) break elif s1[i + 2:] == s2[i + 2:] and (s1[i] == s2[i + 1] or s1[i + 1] == s2[i]): print(1) break else: print(0) break
89
93
6,144,000
33870186
i, j = 0, int(input()) - 1 a, b = input(), input() while a[i] == b[i]: i += 1 while a[j] == b[j]: j -= 1 print((a[i + 1:j + 1] == b[i:j]) + (b[i + 1:j + 1] == a[i:j]))
VK Cup 2015 - Round 2
CF
2,015
2
256
Correcting Mistakes
Analyzing the mistakes people make while typing search queries is a complex and an interesting work. As there is no guaranteed way to determine what the user originally meant by typing some query, we have to use different sorts of heuristics. Polycarp needed to write a code that could, given two words, check whether they could have been obtained from the same word as a result of typos. Polycarpus suggested that the most common typo is skipping exactly one letter as you type a word. Implement a program that can, given two distinct words S and T of the same length n determine how many words W of length n + 1 are there with such property that you can transform W into both S, and T by deleting exactly one character. Words S and T consist of lowercase English letters. Word W also should consist of lowercase English letters.
The first line contains integer n (1 ≤ n ≤ 100 000) — the length of words S and T. The second line contains word S. The third line contains word T. Words S and T consist of lowercase English letters. It is guaranteed that S and T are distinct words.
Print a single integer — the number of distinct words W that can be transformed to S and T due to a typo.
null
In the first sample test the two given words could be obtained only from word "treading" (the deleted letters are marked in bold). In the second sample test the two given words couldn't be obtained from the same word by removing one letter. In the third sample test the two given words could be obtained from either word "tory" or word "troy".
[{"input": "7\nreading\ntrading", "output": "1"}, {"input": "5\nsweet\nsheep", "output": "0"}, {"input": "3\ntoy\ntry", "output": "2"}]
1,800
[]
89
[{"input": "7\r\nreading\r\ntrading\r\n", "output": "1\n"}, {"input": "5\r\nsweet\r\nsheep\r\n", "output": "0\n"}, {"input": "3\r\ntoy\r\ntry\r\n", "output": "2\n"}, {"input": "5\r\nspare\r\nspars\r\n", "output": "2\n"}, {"input": "1\r\na\r\nb\r\n", "output": "2\n"}, {"input": "1\r\nz\r\ny\r\n", "output": "2\n"}, {"input": "2\r\nab\r\nac\r\n", "output": "2\n"}, {"input": "2\r\nba\r\nca\r\n", "output": "2\n"}, {"input": "2\r\nac\r\ncb\r\n", "output": "1\n"}, {"input": "100\r\neebdeddddbecdbddaaecbbaccbecdeacedddcaddcdebedbabbceeeadecadbbeaecdaeabbceacbdbdbbdacebbbccdcbbeedbe\r\ndacdeebebeaeaacdeedadbcbaedcbddddddedacbabeddebaaebbdcebebaaccbaeccbecdbcbceadaaecadecbadbcddcdabecd\r\n", "output": "0\n"}, {"input": "250\r\niiffiehchidfgigdbcciahdehjjfacbbaaadagaibjjcehjcbjdhaadebaejiicgidbhajfbfejcdicgfbcchgbahfccbefdcddbjjhejigiafhdjbiiehadfficicbebeeegcebideijidbgdecffeaegjfjbbcfiabfbaiddbjgidebdiccfcgfbcbbfhaejaibeicghecchjbiaceaibfgibhgcfgifiedcbhhfadhccfdhejeggcah\r\njbadcfjffcfabbecfabgcafgfcgfeffjjhhdaajjgcbgbechhiadfahjidcdiefhbabhjhjijghghcgghcefhidhdgficiffdjgfdahcaicidfghiedgihbbjgicjeiacihdihfhadjhccddhigiibafiafficegaiehabafiiecbjcbfhdbeaebigaijehhdbfeehbcahaggbbdjcdbgbiajgeigdeabdbddbgcgjibfdgjghhdidjdhh\r\n", "output": "0\n"}, {"input": "100\r\nabababbbababbababaaabbbbaaaabbabbabbabababbbaaaabbababbbbababbabbbaaababababbbaaaabbbabbababbbbbbaba\r\nabababbbababbababaaabbbbaaaabbabbabbabababbbaaaabaababbbbababbabbbaaababababbbaaaabbbabbababbbbbbaba\r\n", "output": "2\n"}, {"input": "100\r\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n", "output": "2\n"}, {"input": "100\r\naaaaaaaaaaaaaaaaaaaaaalaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\naaaaaaaaaaaaaaaaaaaaaakaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n", "output": "2\n"}, {"input": "100\r\ndwtsrrtztfuibkrpwbxjrcxsonrwoydkmbhxrghekvusiyzqkyvulrvtfxmvrphpzpmazizogfbyauxtjfesocssnxvjjdedomlz\r\ndwtsrrtztfuibkrpwbxjrcxsonrwoydkmbhxrghekvusiyzqkyvulrvtfxmvrphpzpzazizogfbyauxtjfesocssnxvjjdedomlz\r\n", "output": "2\n"}, {"input": "100\r\naaabaabbbababbbaabbbbbaaababbabbaaabbabaabbabbabbbbbabbaaabbbbbbbbbbbbbbbababaaababbaaabeabbabaabbab\r\naaabaabbbababbbaabbbbbaaababbabbaaabbabaabbabbabbbbbabbaaabbbbbbbbbbbbbbbababaaababbaaabtabbabaabbab\r\n", "output": "2\n"}, {"input": "100\r\naaaabaaaaabbaababaaabaababaabbbaabaaabbbaaababbabaabbabababbaaabaabababbbababbbabbaaaabbbbbbbaaababa\r\naaaabaaaaabbaababaaabaababaabbbaabaaabbbaaaabbbabaabbabababbaaabaabababbbababbbabbaaaabbbbbbbaaababa\r\n", "output": "2\n"}, {"input": "100\r\neebdeddddbecdbddaaecbbaccbecdeacedddcaddcdebedbabbceeeadecadbbeaecdaeabbceacbdbdbbdacebbbccdcbbeedbe\r\needbeddddbecdbddaaecbbaccbecdeacedddcaddcdebedbabbceeeadecadbbeaecdaeabbceacbdbdbbdacebbbccdcbbeedbe\r\n", "output": "2\n"}, {"input": "100\r\nxjywrmrwqaytezhtqmcnrrjomslvcmevncvzeddnvqgkbusnbzrppdsuzsmcobmnslpvosunavayvdbxhtavvwodorwijxfjjlat\r\nxjywrmrwqaytezhtqmcrnrjomslvcmevncvzeddnvqgkbusnbzrppdsuzsmcobmnslpvosunavayvdbxhtavvwodorwijxfjjlat\r\n", "output": "2\n"}, {"input": "4\r\nbbca\r\nabab\r\n", "output": "0\n"}, {"input": "4\r\nabcb\r\nccac\r\n", "output": "0\n"}, {"input": "4\r\ncaaa\r\nabab\r\n", "output": "0\n"}, {"input": "4\r\nacca\r\nbabb\r\n", "output": "0\n"}, {"input": "4\r\nccba\r\nbabb\r\n", "output": "0\n"}, {"input": "4\r\nbcca\r\ncbaa\r\n", "output": "0\n"}, {"input": "4\r\naaca\r\ncaab\r\n", "output": "0\n"}, {"input": "4\r\nbaab\r\nbcbc\r\n", "output": "0\n"}, {"input": "4\r\nabba\r\ncaca\r\n", "output": "0\n"}, {"input": "4\r\nbcbb\r\nccac\r\n", "output": "0\n"}, {"input": "4\r\ncbba\r\nabba\r\n", "output": "2\n"}, {"input": "4\r\nbaca\r\nccbc\r\n", "output": "0\n"}, {"input": "4\r\ncabc\r\naacc\r\n", "output": "0\n"}, {"input": "4\r\nbbab\r\ncbaa\r\n", "output": "0\n"}, {"input": "4\r\nabcc\r\nbcab\r\n", "output": "0\n"}, {"input": "4\r\nbaaa\r\nbbbc\r\n", "output": "0\n"}, {"input": "4\r\naabc\r\naacb\r\n", "output": "2\n"}, {"input": "4\r\nccbb\r\nbbcb\r\n", "output": "0\n"}, {"input": "4\r\nbaba\r\naccc\r\n", "output": "0\n"}, {"input": "4\r\nbbbc\r\nbbab\r\n", "output": "1\n"}, {"input": "2\r\nab\r\nba\r\n", "output": "2\n"}, {"input": "5\r\ncabac\r\ncbabc\r\n", "output": "2\n"}, {"input": "3\r\naba\r\nbab\r\n", "output": "2\n"}, {"input": "5\r\nabxxx\r\nbayyy\r\n", "output": "0\n"}, {"input": "4\r\nxaxa\r\naxax\r\n", "output": "2\n"}, {"input": "5\r\nababa\r\nbabab\r\n", "output": "2\n"}, {"input": "5\r\nbabab\r\nababa\r\n", "output": "2\n"}, {"input": "154\r\nwqpewhyutqnhaewqpewhywqpewhyutqnhaeutqnhaeutqnhaewqpewhyutqnhaewqpewhywqpewhyutqnhaeutqnhaeutqnhaeutqnhaewqpewhyutqnhaewqpewhywqpewhywqpewhywqpewhyutqnhae\r\nutqnhaeutqnhaeutqnhaewqpewhywqpewhyutqnhaewqpewhyutqnhaewqpewhywqpewhyutqnhaeutqnhaeutqnhaewqpewhyutqnhaewqpewhywqpewhywqpewhyutqnhaewqpewhyutqnhaewqpewhy\r\n", "output": "0\n"}, {"input": "7\r\ntrading\r\nrtading\r\n", "output": "2\n"}, {"input": "5\r\nxabax\r\nxbabx\r\n", "output": "2\n"}, {"input": "3\r\nabc\r\nacb\r\n", "output": "2\n"}, {"input": "4\r\nabab\r\nbaba\r\n", "output": "2\n"}, {"input": "3\r\naab\r\naba\r\n", "output": "2\n"}, {"input": "2\r\ner\r\nre\r\n", "output": "2\n"}, {"input": "5\r\ntabat\r\ntbaat\r\n", "output": "2\n"}]
false
stdio
null
true
962/B
962
B
Python 3
TESTS
9
78
7,680,000
37272233
n,x,y=map(int,input().split()) import math s=x+y;x=max(x,y);y=s-x a=list(map(str,input().split('*'))) b=[] for i in a: if "." in i : b.append(len(i)) ans=0 for i in b: n=math.ceil(i/2) m=math.floor(i/2) l=min(n,x);ans+=l x-=(l) l=min(m,y);ans+=l s=x+y;x=max(x,y);y=s-x print(ans)
93
93
6,041,600
197503694
n,a,b=map(int,input().split()) s=t=0 for x in map(len,input().split('*')): s+=x//2;t+=x%2 m=min(a,s)+min(b,s) print(m+min(a+b-m,t))
Educational Codeforces Round 42 (Rated for Div. 2)
ICPC
2,018
2
256
Students in Railway Carriage
There are $$$n$$$ consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger. The university team for the Olympiad consists of $$$a$$$ student-programmers and $$$b$$$ student-athletes. Determine the largest number of students from all $$$a+b$$$ students, which you can put in the railway carriage so that: - no student-programmer is sitting next to the student-programmer; - and no student-athlete is sitting next to the student-athlete. In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting. Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all).
The first line contain three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$1 \le n \le 2\cdot10^{5}$$$, $$$0 \le a, b \le 2\cdot10^{5}$$$, $$$a + b > 0$$$) — total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes. The second line contains a string with length $$$n$$$, consisting of characters "." and "*". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member.
Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete.
null
In the first example you can put all student, for example, in the following way: *.AB* In the second example you can put four students, for example, in the following way: *BAB*B In the third example you can put seven students, for example, in the following way: B*ABAB**A*B The letter A means a student-programmer, and the letter B — student-athlete.
[{"input": "5 1 1\n*...*", "output": "2"}, {"input": "6 2 3\n*...*.", "output": "4"}, {"input": "11 3 10\n.*....**.*.", "output": "7"}, {"input": "3 2 3\n***", "output": "0"}]
1,300
["constructive algorithms", "greedy", "implementation"]
93
[{"input": "5 1 1\r\n*...*\r\n", "output": "2\r\n"}, {"input": "6 2 3\r\n*...*.\r\n", "output": "4\r\n"}, {"input": "11 3 10\r\n.*....**.*.\r\n", "output": "7\r\n"}, {"input": "3 2 3\r\n***\r\n", "output": "0\r\n"}, {"input": "9 5 3\r\n*...*...*\r\n", "output": "6\r\n"}, {"input": "9 2 4\r\n*...*...*\r\n", "output": "6\r\n"}, {"input": "9 2 200000\r\n*...*...*\r\n", "output": "6\r\n"}, {"input": "1 0 1\r\n.\r\n", "output": "1\r\n"}, {"input": "20 5 5\r\n.*.*.........*......\r\n", "output": "10\r\n"}, {"input": "14 3 7\r\n.*.......*..*.\r\n", "output": "10\r\n"}, {"input": "6 1 3\r\n*....*\r\n", "output": "3\r\n"}, {"input": "9 2 4\r\n..*.*....\r\n", "output": "6\r\n"}, {"input": "5 1 2\r\n...*.\r\n", "output": "3\r\n"}, {"input": "2 2 0\r\n..\r\n", "output": "1\r\n"}, {"input": "2 0 2\r\n..\r\n", "output": "1\r\n"}, {"input": "10 1 1\r\n..........\r\n", "output": "2\r\n"}, {"input": "4 0 1\r\n....\r\n", "output": "1\r\n"}, {"input": "5 3 3\r\n...**\r\n", "output": "3\r\n"}, {"input": "3 0 1\r\n.*.\r\n", "output": "1\r\n"}, {"input": "4 2 2\r\n....\r\n", "output": "4\r\n"}, {"input": "13 3 3\r\n*...*...*...*\r\n", "output": "6\r\n"}, {"input": "5 10 1\r\n*....\r\n", "output": "3\r\n"}, {"input": "7 0 4\r\n...*..*\r\n", "output": "3\r\n"}, {"input": "20 5 5\r\n.*.*.............*..\r\n", "output": "10\r\n"}, {"input": "6 2 1\r\n..*...\r\n", "output": "3\r\n"}, {"input": "17 11 2\r\n.*..*..*.*.***...\r\n", "output": "9\r\n"}, {"input": "5 2 3\r\n.....\r\n", "output": "5\r\n"}, {"input": "64 59 2\r\n.*.***......****.*..**..**..****.*.*.*.**...**..***.***.*..*..*.\r\n", "output": "23\r\n"}, {"input": "5 1 2\r\n.*...\r\n", "output": "3\r\n"}, {"input": "2 1 1\r\n..\r\n", "output": "2\r\n"}, {"input": "10 15 15\r\n..........\r\n", "output": "10\r\n"}, {"input": "10 7 0\r\n.*...*..*.\r\n", "output": "5\r\n"}, {"input": "5 0 1\r\n.....\r\n", "output": "1\r\n"}, {"input": "4 1 1\r\n..*.\r\n", "output": "2\r\n"}, {"input": "10 4 6\r\n..........\r\n", "output": "9\r\n"}, {"input": "5 1 4\r\n.....\r\n", "output": "4\r\n"}, {"input": "10 4 3\r\n.*..*...*.\r\n", "output": "7\r\n"}, {"input": "4 2 0\r\n....\r\n", "output": "2\r\n"}, {"input": "5 0 2\r\n.....\r\n", "output": "2\r\n"}, {"input": "5 0 1\r\n*.*.*\r\n", "output": "1\r\n"}, {"input": "10 20 0\r\n..........\r\n", "output": "5\r\n"}, {"input": "10 8 1\r\n.*.*......\r\n", "output": "6\r\n"}, {"input": "6 1 1\r\n*...*.\r\n", "output": "2\r\n"}, {"input": "7 1 0\r\n.*.....\r\n", "output": "1\r\n"}, {"input": "1 1 1\r\n.\r\n", "output": "1\r\n"}, {"input": "10 5 1\r\n..........\r\n", "output": "6\r\n"}, {"input": "4 3 0\r\n....\r\n", "output": "2\r\n"}, {"input": "11 6 2\r\n.*...*...*.\r\n", "output": "8\r\n"}, {"input": "11 7 1\r\n.*...*...*.\r\n", "output": "7\r\n"}]
false
stdio
null
true
962/B
962
B
PyPy 3-64
TESTS
9
61
1,740,800
222658837
n,a,b = list(map(int,input().split())) s = input() cnt = 0 a,b=max(a,b),min(a,b) for c in s: if c == '.': if a > 0: a -= 1 cnt += 1 a,b = b,a else: a,b = max(a,b),min(a,b) print(cnt)
93
109
2,457,600
46969955
def main(): n, a, b = map(int, input().split()) line = input() spaces = [len(e) for e in line.split('*') if e] ca, cb = a, b for e in spaces: if e % 2 != 0: if ca > cb: cb -= e // 2 ca -= e - e // 2 else: ca -= e // 2 cb -= e - e // 2 else: ca -= e // 2 cb -= e // 2 ca = max(0, ca) cb = max(0, cb) print(a - ca + b - cb) if __name__ == '__main__': main()
Educational Codeforces Round 42 (Rated for Div. 2)
ICPC
2,018
2
256
Students in Railway Carriage
There are $$$n$$$ consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger. The university team for the Olympiad consists of $$$a$$$ student-programmers and $$$b$$$ student-athletes. Determine the largest number of students from all $$$a+b$$$ students, which you can put in the railway carriage so that: - no student-programmer is sitting next to the student-programmer; - and no student-athlete is sitting next to the student-athlete. In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting. Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all).
The first line contain three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$1 \le n \le 2\cdot10^{5}$$$, $$$0 \le a, b \le 2\cdot10^{5}$$$, $$$a + b > 0$$$) — total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes. The second line contains a string with length $$$n$$$, consisting of characters "." and "*". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member.
Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete.
null
In the first example you can put all student, for example, in the following way: *.AB* In the second example you can put four students, for example, in the following way: *BAB*B In the third example you can put seven students, for example, in the following way: B*ABAB**A*B The letter A means a student-programmer, and the letter B — student-athlete.
[{"input": "5 1 1\n*...*", "output": "2"}, {"input": "6 2 3\n*...*.", "output": "4"}, {"input": "11 3 10\n.*....**.*.", "output": "7"}, {"input": "3 2 3\n***", "output": "0"}]
1,300
["constructive algorithms", "greedy", "implementation"]
93
[{"input": "5 1 1\r\n*...*\r\n", "output": "2\r\n"}, {"input": "6 2 3\r\n*...*.\r\n", "output": "4\r\n"}, {"input": "11 3 10\r\n.*....**.*.\r\n", "output": "7\r\n"}, {"input": "3 2 3\r\n***\r\n", "output": "0\r\n"}, {"input": "9 5 3\r\n*...*...*\r\n", "output": "6\r\n"}, {"input": "9 2 4\r\n*...*...*\r\n", "output": "6\r\n"}, {"input": "9 2 200000\r\n*...*...*\r\n", "output": "6\r\n"}, {"input": "1 0 1\r\n.\r\n", "output": "1\r\n"}, {"input": "20 5 5\r\n.*.*.........*......\r\n", "output": "10\r\n"}, {"input": "14 3 7\r\n.*.......*..*.\r\n", "output": "10\r\n"}, {"input": "6 1 3\r\n*....*\r\n", "output": "3\r\n"}, {"input": "9 2 4\r\n..*.*....\r\n", "output": "6\r\n"}, {"input": "5 1 2\r\n...*.\r\n", "output": "3\r\n"}, {"input": "2 2 0\r\n..\r\n", "output": "1\r\n"}, {"input": "2 0 2\r\n..\r\n", "output": "1\r\n"}, {"input": "10 1 1\r\n..........\r\n", "output": "2\r\n"}, {"input": "4 0 1\r\n....\r\n", "output": "1\r\n"}, {"input": "5 3 3\r\n...**\r\n", "output": "3\r\n"}, {"input": "3 0 1\r\n.*.\r\n", "output": "1\r\n"}, {"input": "4 2 2\r\n....\r\n", "output": "4\r\n"}, {"input": "13 3 3\r\n*...*...*...*\r\n", "output": "6\r\n"}, {"input": "5 10 1\r\n*....\r\n", "output": "3\r\n"}, {"input": "7 0 4\r\n...*..*\r\n", "output": "3\r\n"}, {"input": "20 5 5\r\n.*.*.............*..\r\n", "output": "10\r\n"}, {"input": "6 2 1\r\n..*...\r\n", "output": "3\r\n"}, {"input": "17 11 2\r\n.*..*..*.*.***...\r\n", "output": "9\r\n"}, {"input": "5 2 3\r\n.....\r\n", "output": "5\r\n"}, {"input": "64 59 2\r\n.*.***......****.*..**..**..****.*.*.*.**...**..***.***.*..*..*.\r\n", "output": "23\r\n"}, {"input": "5 1 2\r\n.*...\r\n", "output": "3\r\n"}, {"input": "2 1 1\r\n..\r\n", "output": "2\r\n"}, {"input": "10 15 15\r\n..........\r\n", "output": "10\r\n"}, {"input": "10 7 0\r\n.*...*..*.\r\n", "output": "5\r\n"}, {"input": "5 0 1\r\n.....\r\n", "output": "1\r\n"}, {"input": "4 1 1\r\n..*.\r\n", "output": "2\r\n"}, {"input": "10 4 6\r\n..........\r\n", "output": "9\r\n"}, {"input": "5 1 4\r\n.....\r\n", "output": "4\r\n"}, {"input": "10 4 3\r\n.*..*...*.\r\n", "output": "7\r\n"}, {"input": "4 2 0\r\n....\r\n", "output": "2\r\n"}, {"input": "5 0 2\r\n.....\r\n", "output": "2\r\n"}, {"input": "5 0 1\r\n*.*.*\r\n", "output": "1\r\n"}, {"input": "10 20 0\r\n..........\r\n", "output": "5\r\n"}, {"input": "10 8 1\r\n.*.*......\r\n", "output": "6\r\n"}, {"input": "6 1 1\r\n*...*.\r\n", "output": "2\r\n"}, {"input": "7 1 0\r\n.*.....\r\n", "output": "1\r\n"}, {"input": "1 1 1\r\n.\r\n", "output": "1\r\n"}, {"input": "10 5 1\r\n..........\r\n", "output": "6\r\n"}, {"input": "4 3 0\r\n....\r\n", "output": "2\r\n"}, {"input": "11 6 2\r\n.*...*...*.\r\n", "output": "8\r\n"}, {"input": "11 7 1\r\n.*...*...*.\r\n", "output": "7\r\n"}]
false
stdio
null
true
214/B
214
B
PyPy 3-64
TESTS
12
186
17,612,800
179133371
from collections import defaultdict n=int(input()) t=sorted(map(int,input().split()),reverse=True) hashmap=defaultdict(list) #所有零添加到末尾 # 选出最长的总和能够被3整除的数字序列 然后按降序排序 #如果没有零,那么返回-1 if 0 not in t:print(-1) else: a,b,c=0,0,0 for i in range(n): hashmap[t[i]%3].append(t[i]) if t[i]%3==1:b+=1 if t[i]%3==2:c+=1 choosen=min(b,c) cs=hashmap[0] block1,block2=b//3,c//3 cs=cs+hashmap[1][:block1*3]+hashmap[2][:block2*3] surplus=min(abs(b-block1*3),abs(c-block2*3)) cs+=hashmap[2][block2*3:block2*3+surplus]+hashmap[1][block1*3:block1*3+surplus] print("".join(sorted(map(str,cs),reverse=True)))
101
186
13,004,800
197826043
input() a = sorted(map(int, input().split())) if a[0] != 0: a = [-1] else: s = sum(a) if s % 3 != 0: flag = False for i in a: if i % 3 == s % 3: flag = True a.remove(i) break if(flag == False): for i in a: if i % 3: a.remove(i) break for i in a: if i % 3: a.remove(i) break while len(a) > 1 and a[-1] == 0: a.pop() print(''.join(map(str, a[::-1])))
Codeforces Round 131 (Div. 2)
CF
2,012
2
256
Hometask
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set.
A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space.
On a single line print the answer to the problem. If such number does not exist, then you should print -1.
null
In the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
[{"input": "1\n0", "output": "0"}, {"input": "11\n3 4 5 4 5 3 5 3 4 4 0", "output": "5554443330"}, {"input": "8\n3 2 5 1 5 2 2 3", "output": "-1"}]
1,600
["brute force", "constructive algorithms", "greedy", "math"]
101
[{"input": "1\r\n0\r\n", "output": "0\r\n"}, {"input": "11\r\n3 4 5 4 5 3 5 3 4 4 0\r\n", "output": "5554443330\r\n"}, {"input": "8\r\n3 2 5 1 5 2 2 3\r\n", "output": "-1\r\n"}, {"input": "12\r\n5 3 3 3 2 5 5 1 2 1 4 1\r\n", "output": "-1\r\n"}, {"input": "8\r\n5 5 4 1 5 5 5 3\r\n", "output": "-1\r\n"}, {"input": "12\r\n3 1 2 3 2 0 2 2 2 0 2 3\r\n", "output": "33322222200\r\n"}, {"input": "12\r\n5 1 4 4 2 1 7 7 4 2 5 1\r\n", "output": "-1\r\n"}, {"input": "5\r\n3 6 1 6 2\r\n", "output": "-1\r\n"}, {"input": "11\r\n3 9 9 6 4 3 6 4 9 6 0\r\n", "output": "999666330\r\n"}, {"input": "5\r\n9 6 6 6 1\r\n", "output": "-1\r\n"}, {"input": "10\r\n2 0 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "10\r\n1 0 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n1 1 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 2 2 0\r\n", "output": "0\r\n"}, {"input": "6\r\n3 3 2 2 2 0\r\n", "output": "332220\r\n"}, {"input": "7\r\n3 3 2 2 2 2 0\r\n", "output": "332220\r\n"}, {"input": "6\r\n0 3 3 1 1 1\r\n", "output": "331110\r\n"}, {"input": "7\r\n0 3 3 1 1 1 1\r\n", "output": "331110\r\n"}, {"input": "7\r\n0 3 3 4 4 4 4\r\n", "output": "444330\r\n"}, {"input": "7\r\n0 3 3 2 2 4 4\r\n", "output": "4433220\r\n"}, {"input": "7\r\n4 2 3 3 0 0 0\r\n", "output": "4332000\r\n"}, {"input": "4\r\n1 1 0 3\r\n", "output": "30\r\n"}, {"input": "4\r\n3 0 2 2\r\n", "output": "30\r\n"}, {"input": "8\r\n3 3 3 5 5 0 0 0\r\n", "output": "333000\r\n"}, {"input": "8\r\n3 3 6 3 0 7 7 9\r\n", "output": "963330\r\n"}, {"input": "9\r\n1 2 3 4 5 6 7 8 9\r\n", "output": "-1\r\n"}, {"input": "9\r\n9 9 9 9 9 9 9 9 9\r\n", "output": "-1\r\n"}, {"input": "1\r\n0\r\n", "output": "0\r\n"}, {"input": "2\r\n9 0\r\n", "output": "90\r\n"}, {"input": "10\r\n3 0 2 2 2 2 2 2 2 2\r\n", "output": "32222220\r\n"}, {"input": "10\r\n3 0 1 1 1 1 1 1 1 1\r\n", "output": "31111110\r\n"}, {"input": "10\r\n3 0 4 4 4 4 4 4 4 4\r\n", "output": "44444430\r\n"}, {"input": "10\r\n2 0 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "10\r\n2 2 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n5 5 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n1 4 0\r\n", "output": "0\r\n"}, {"input": "3\r\n0 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n0 1 4 3\r\n", "output": "30\r\n"}, {"input": "3\r\n2 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n0 1 2 3\r\n", "output": "3210\r\n"}, {"input": "4\r\n1 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n8 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "2\r\n0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 8 5 6\r\n", "output": "600\r\n"}, {"input": "4\r\n5 8 3 0\r\n", "output": "30\r\n"}, {"input": "4\r\n1 4 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n0 0 1\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n1 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n0 0 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n0 0 4\r\n", "output": "0\r\n"}, {"input": "2\r\n0 1\r\n", "output": "0\r\n"}, {"input": "4\r\n1 1 0 0\r\n", "output": "0\r\n"}, {"input": "6\r\n2 2 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n3 2 5 0 0\r\n", "output": "300\r\n"}, {"input": "4\r\n5 3 2 0\r\n", "output": "30\r\n"}, {"input": "5\r\n0 0 0 2 2\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 0 0 1\r\n", "output": "0\r\n"}, {"input": "4\r\n0 3 5 8\r\n", "output": "30\r\n"}]
false
stdio
null
true
214/B
214
B
PyPy 3-64
TESTS
12
218
17,920,000
199886246
from collections import Counter,defaultdict N = int(input()) A = list(map(int, input().split())) A.sort() lib = defaultdict(list) for a in A: lib[a%3].append(a) for k,v in lib.items(): lib[k] = v[::-1] if A[0]!=0: exit(print(-1)) p = sum(A)%3 if p==0: exit(print(''.join([str(a) for a in A[::-1]]))) if p==1: if len(lib[1])>0: lib[1].pop() elif len(lib[2])>=2: lib[2].pop() lib[2].pop() else: exit(print(-1)) else: if len(lib[2])>0: lib[2].pop() elif len(lib[1])>=2: lib[1].pop() lib[1].pop() else: exit(print(-1)) ans = [] for i in range(9,-1,-1): for a in lib[i]: ans.append(a) ans.sort(reverse=True) print(''.join([str(a) for a in ans]))
101
186
15,052,800
192692892
# https://codeforces.com/problemset/problem/214/B n = int(input()) a = list(map(int,input().split())) a.sort(reverse=True) if a[-1] != 0: print(-1) exit() total = sum(a) if total % 3 != 0: t = total%3 for i in range(n - 1,-1,-1): if a[i]%3 == t: a = a[:i] + a[i + 1:] break else: cnt = 0 for i in range(n - 1, -1, -1): if a[i] % 3 == 3 - t: a = a[:i] + a[i + 1:] cnt += 1 if cnt == 2: break if a[0] == 0: print(0) else: print(''.join([str(i) for i in a]))
Codeforces Round 131 (Div. 2)
CF
2,012
2
256
Hometask
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set.
A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space.
On a single line print the answer to the problem. If such number does not exist, then you should print -1.
null
In the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
[{"input": "1\n0", "output": "0"}, {"input": "11\n3 4 5 4 5 3 5 3 4 4 0", "output": "5554443330"}, {"input": "8\n3 2 5 1 5 2 2 3", "output": "-1"}]
1,600
["brute force", "constructive algorithms", "greedy", "math"]
101
[{"input": "1\r\n0\r\n", "output": "0\r\n"}, {"input": "11\r\n3 4 5 4 5 3 5 3 4 4 0\r\n", "output": "5554443330\r\n"}, {"input": "8\r\n3 2 5 1 5 2 2 3\r\n", "output": "-1\r\n"}, {"input": "12\r\n5 3 3 3 2 5 5 1 2 1 4 1\r\n", "output": "-1\r\n"}, {"input": "8\r\n5 5 4 1 5 5 5 3\r\n", "output": "-1\r\n"}, {"input": "12\r\n3 1 2 3 2 0 2 2 2 0 2 3\r\n", "output": "33322222200\r\n"}, {"input": "12\r\n5 1 4 4 2 1 7 7 4 2 5 1\r\n", "output": "-1\r\n"}, {"input": "5\r\n3 6 1 6 2\r\n", "output": "-1\r\n"}, {"input": "11\r\n3 9 9 6 4 3 6 4 9 6 0\r\n", "output": "999666330\r\n"}, {"input": "5\r\n9 6 6 6 1\r\n", "output": "-1\r\n"}, {"input": "10\r\n2 0 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "10\r\n1 0 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n1 1 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 2 2 0\r\n", "output": "0\r\n"}, {"input": "6\r\n3 3 2 2 2 0\r\n", "output": "332220\r\n"}, {"input": "7\r\n3 3 2 2 2 2 0\r\n", "output": "332220\r\n"}, {"input": "6\r\n0 3 3 1 1 1\r\n", "output": "331110\r\n"}, {"input": "7\r\n0 3 3 1 1 1 1\r\n", "output": "331110\r\n"}, {"input": "7\r\n0 3 3 4 4 4 4\r\n", "output": "444330\r\n"}, {"input": "7\r\n0 3 3 2 2 4 4\r\n", "output": "4433220\r\n"}, {"input": "7\r\n4 2 3 3 0 0 0\r\n", "output": "4332000\r\n"}, {"input": "4\r\n1 1 0 3\r\n", "output": "30\r\n"}, {"input": "4\r\n3 0 2 2\r\n", "output": "30\r\n"}, {"input": "8\r\n3 3 3 5 5 0 0 0\r\n", "output": "333000\r\n"}, {"input": "8\r\n3 3 6 3 0 7 7 9\r\n", "output": "963330\r\n"}, {"input": "9\r\n1 2 3 4 5 6 7 8 9\r\n", "output": "-1\r\n"}, {"input": "9\r\n9 9 9 9 9 9 9 9 9\r\n", "output": "-1\r\n"}, {"input": "1\r\n0\r\n", "output": "0\r\n"}, {"input": "2\r\n9 0\r\n", "output": "90\r\n"}, {"input": "10\r\n3 0 2 2 2 2 2 2 2 2\r\n", "output": "32222220\r\n"}, {"input": "10\r\n3 0 1 1 1 1 1 1 1 1\r\n", "output": "31111110\r\n"}, {"input": "10\r\n3 0 4 4 4 4 4 4 4 4\r\n", "output": "44444430\r\n"}, {"input": "10\r\n2 0 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "10\r\n2 2 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n5 5 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n1 4 0\r\n", "output": "0\r\n"}, {"input": "3\r\n0 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n0 1 4 3\r\n", "output": "30\r\n"}, {"input": "3\r\n2 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n0 1 2 3\r\n", "output": "3210\r\n"}, {"input": "4\r\n1 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n8 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "2\r\n0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 8 5 6\r\n", "output": "600\r\n"}, {"input": "4\r\n5 8 3 0\r\n", "output": "30\r\n"}, {"input": "4\r\n1 4 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n0 0 1\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n1 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n0 0 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n0 0 4\r\n", "output": "0\r\n"}, {"input": "2\r\n0 1\r\n", "output": "0\r\n"}, {"input": "4\r\n1 1 0 0\r\n", "output": "0\r\n"}, {"input": "6\r\n2 2 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n3 2 5 0 0\r\n", "output": "300\r\n"}, {"input": "4\r\n5 3 2 0\r\n", "output": "30\r\n"}, {"input": "5\r\n0 0 0 2 2\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 0 0 1\r\n", "output": "0\r\n"}, {"input": "4\r\n0 3 5 8\r\n", "output": "30\r\n"}]
false
stdio
null
true
962/B
962
B
PyPy 3
TESTS
9
93
2,457,600
112878680
n,a,b=map(int,input().split()) s=input() sum=0; ans=0; k=max(a,b) for i in range(len(s)): if(s[i]=='*'): if(a>b): k=a else: k=b if(s[i]=='.'): if(k==a and a>0): a-=1 ans+=1 k=b elif(k==b and b>0): b-=1 ans+=1 k=a print(ans)
93
109
3,481,600
104217431
import math def solve(): n, a, b = [int(s) for s in input().split(' ')] s = input() gaps = [] g = 0 for c in s: if c == '.': g += 1 elif g: gaps.append(g) g = 0 if g: gaps.append(g) k = 0 for gap in gaps: if a > b: a, b = b, a x = min(b, math.ceil(gap / 2)) y = min(a, math.floor(gap / 2)) b -= x a -= y k += (x + y) return k print(solve())
Educational Codeforces Round 42 (Rated for Div. 2)
ICPC
2,018
2
256
Students in Railway Carriage
There are $$$n$$$ consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger. The university team for the Olympiad consists of $$$a$$$ student-programmers and $$$b$$$ student-athletes. Determine the largest number of students from all $$$a+b$$$ students, which you can put in the railway carriage so that: - no student-programmer is sitting next to the student-programmer; - and no student-athlete is sitting next to the student-athlete. In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting. Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all).
The first line contain three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$1 \le n \le 2\cdot10^{5}$$$, $$$0 \le a, b \le 2\cdot10^{5}$$$, $$$a + b > 0$$$) — total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes. The second line contains a string with length $$$n$$$, consisting of characters "." and "*". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member.
Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete.
null
In the first example you can put all student, for example, in the following way: *.AB* In the second example you can put four students, for example, in the following way: *BAB*B In the third example you can put seven students, for example, in the following way: B*ABAB**A*B The letter A means a student-programmer, and the letter B — student-athlete.
[{"input": "5 1 1\n*...*", "output": "2"}, {"input": "6 2 3\n*...*.", "output": "4"}, {"input": "11 3 10\n.*....**.*.", "output": "7"}, {"input": "3 2 3\n***", "output": "0"}]
1,300
["constructive algorithms", "greedy", "implementation"]
93
[{"input": "5 1 1\r\n*...*\r\n", "output": "2\r\n"}, {"input": "6 2 3\r\n*...*.\r\n", "output": "4\r\n"}, {"input": "11 3 10\r\n.*....**.*.\r\n", "output": "7\r\n"}, {"input": "3 2 3\r\n***\r\n", "output": "0\r\n"}, {"input": "9 5 3\r\n*...*...*\r\n", "output": "6\r\n"}, {"input": "9 2 4\r\n*...*...*\r\n", "output": "6\r\n"}, {"input": "9 2 200000\r\n*...*...*\r\n", "output": "6\r\n"}, {"input": "1 0 1\r\n.\r\n", "output": "1\r\n"}, {"input": "20 5 5\r\n.*.*.........*......\r\n", "output": "10\r\n"}, {"input": "14 3 7\r\n.*.......*..*.\r\n", "output": "10\r\n"}, {"input": "6 1 3\r\n*....*\r\n", "output": "3\r\n"}, {"input": "9 2 4\r\n..*.*....\r\n", "output": "6\r\n"}, {"input": "5 1 2\r\n...*.\r\n", "output": "3\r\n"}, {"input": "2 2 0\r\n..\r\n", "output": "1\r\n"}, {"input": "2 0 2\r\n..\r\n", "output": "1\r\n"}, {"input": "10 1 1\r\n..........\r\n", "output": "2\r\n"}, {"input": "4 0 1\r\n....\r\n", "output": "1\r\n"}, {"input": "5 3 3\r\n...**\r\n", "output": "3\r\n"}, {"input": "3 0 1\r\n.*.\r\n", "output": "1\r\n"}, {"input": "4 2 2\r\n....\r\n", "output": "4\r\n"}, {"input": "13 3 3\r\n*...*...*...*\r\n", "output": "6\r\n"}, {"input": "5 10 1\r\n*....\r\n", "output": "3\r\n"}, {"input": "7 0 4\r\n...*..*\r\n", "output": "3\r\n"}, {"input": "20 5 5\r\n.*.*.............*..\r\n", "output": "10\r\n"}, {"input": "6 2 1\r\n..*...\r\n", "output": "3\r\n"}, {"input": "17 11 2\r\n.*..*..*.*.***...\r\n", "output": "9\r\n"}, {"input": "5 2 3\r\n.....\r\n", "output": "5\r\n"}, {"input": "64 59 2\r\n.*.***......****.*..**..**..****.*.*.*.**...**..***.***.*..*..*.\r\n", "output": "23\r\n"}, {"input": "5 1 2\r\n.*...\r\n", "output": "3\r\n"}, {"input": "2 1 1\r\n..\r\n", "output": "2\r\n"}, {"input": "10 15 15\r\n..........\r\n", "output": "10\r\n"}, {"input": "10 7 0\r\n.*...*..*.\r\n", "output": "5\r\n"}, {"input": "5 0 1\r\n.....\r\n", "output": "1\r\n"}, {"input": "4 1 1\r\n..*.\r\n", "output": "2\r\n"}, {"input": "10 4 6\r\n..........\r\n", "output": "9\r\n"}, {"input": "5 1 4\r\n.....\r\n", "output": "4\r\n"}, {"input": "10 4 3\r\n.*..*...*.\r\n", "output": "7\r\n"}, {"input": "4 2 0\r\n....\r\n", "output": "2\r\n"}, {"input": "5 0 2\r\n.....\r\n", "output": "2\r\n"}, {"input": "5 0 1\r\n*.*.*\r\n", "output": "1\r\n"}, {"input": "10 20 0\r\n..........\r\n", "output": "5\r\n"}, {"input": "10 8 1\r\n.*.*......\r\n", "output": "6\r\n"}, {"input": "6 1 1\r\n*...*.\r\n", "output": "2\r\n"}, {"input": "7 1 0\r\n.*.....\r\n", "output": "1\r\n"}, {"input": "1 1 1\r\n.\r\n", "output": "1\r\n"}, {"input": "10 5 1\r\n..........\r\n", "output": "6\r\n"}, {"input": "4 3 0\r\n....\r\n", "output": "2\r\n"}, {"input": "11 6 2\r\n.*...*...*.\r\n", "output": "8\r\n"}, {"input": "11 7 1\r\n.*...*...*.\r\n", "output": "7\r\n"}]
false
stdio
null
true
830/A
830
A
PyPy 3
TESTS
5
124
204,800
44456827
'''input 1 2 10 11 15 7 ''' from sys import stdin, stdout def add_the_time(keys, emp, first, n, p, k): SUM = 0 for i in range(first, first + n): SUM = max(abs(emp[i -first] - keys[i]) + abs(keys[i] - p), SUM) return SUM def find_the_key(keys, one, n, k, p): min_key = 0 min_dis = abs(one - keys[0]) + abs(keys[0] - p) for i in range(1, k): if k - i >= n and abs(one - keys[i]) + abs(keys[i] - p) < min_dis: min_dis = abs(one - keys[i]) + abs(keys[i] - p) min_key = i return min_key # main starts n, k, p = list(map(int, stdin.readline().split())) emp = list(map(int, stdin.readline().split())) emp.sort() keys = list(map(int, stdin.readline().split())) keys.sort() first = find_the_key(keys, emp[0], n, k, p) #print(first) SUM = add_the_time(keys, emp, first, n, p, k) print(SUM)
59
77
4,300,800
157931936
import sys people, keys, office = map(int, input().split()) p_pos = list(map(int, input().split())) k_pos = list(map(int, input().split())) p_pos = sorted(p_pos) k_pos = sorted(k_pos) res = sys.maxsize for i in range(keys - people + 1): maxT = -1 for j in range(people): key_index = i + j maxT = max(maxT, abs(office - k_pos[key_index]) + abs(p_pos[j] - k_pos[key_index])) res = min(res, maxT) print(res)
Codeforces Round 424 (Div. 1, rated, based on VK Cup Finals)
CF
2,017
2
256
Office Keys
There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else. You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.
The first line contains three integers n, k and p (1 ≤ n ≤ 1 000, n ≤ k ≤ 2 000, 1 ≤ p ≤ 109) — the number of people, the number of keys and the office location. The second line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 109) — positions in which people are located initially. The positions are given in arbitrary order. The third line contains k distinct integers b1, b2, ..., bk (1 ≤ bj ≤ 109) — positions of the keys. The positions are given in arbitrary order. Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.
Print the minimum time (in seconds) needed for all n to reach the office with keys.
null
In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys.
[{"input": "2 4 50\n20 100\n60 10 40 80", "output": "50"}, {"input": "1 2 10\n11\n15 7", "output": "7"}]
1,800
["binary search", "brute force", "dp", "greedy", "sortings"]
59
[{"input": "2 4 50\r\n20 100\r\n60 10 40 80\r\n", "output": "50\r\n"}, {"input": "1 2 10\r\n11\r\n15 7\r\n", "output": "7\r\n"}, {"input": "2 5 15\r\n10 4\r\n29 23 21 22 26\r\n", "output": "23\r\n"}, {"input": "3 10 1500\r\n106 160 129\r\n1333 1532 1181 1091 1656 1698 1291 1741 1242 1163\r\n", "output": "1394\r\n"}, {"input": "5 20 1\r\n314 316 328 323 321\r\n30 61 11 83 19 63 97 87 14 79 43 57 75 48 47 95 41 27 8 88\r\n", "output": "327\r\n"}, {"input": "20 20 1000000000\r\n911196469 574676950 884047241 984218701 641693148 352743122 616364857 455260052 702604347 921615943 671695009 544819698 768892858 254148055 379968391 65297129 178692403 575557323 307174510 63022600\r\n1621 106 6866 6420 9307 6985 2741 9477 9837 5909 6757 3085 6139 1876 3726 9334 4321 1531 8534 560\r\n", "output": "1984199027\r\n"}, {"input": "40 45 1000\r\n6 55 34 32 20 76 2 84 47 68 31 60 14 70 99 72 21 61 81 79 26 51 96 86 10 1 43 69 87 78 13 11 80 67 50 52 9 29 94 12\r\n1974 1232 234 28 1456 626 408 1086 1525 1209 1096 940 795 1867 548 1774 1993 1199 1112 1087 1923 1156 876 1715 1815 1027 1658 955 398 910 620 1164 749 996 113 109 500 328 800 826 766 518 1474 1038 1029\r\n", "output": "2449\r\n"}, {"input": "50 55 2000\r\n9518 9743 9338 9956 9827 9772 9094 9644 9242 9292 9148 9205 9907 9860 9530 9814 9662 9482 9725 9227 9105 9424 9268 9427 9470 9578 9808 9976 9143 9070 9079 9896 9367 9235 9925 9009 9619 9012 9669 9077 9870 9766 9479 9598 9055 9988 9792 9197 9377 9610\r\n828 656 345 412 69 506 274 994 384 766 587 126 720 227 66 839 997 602 646 955 256 262 243 676 459 83 507 88 559 595 71 154 867 276 487 895 857 888 368 179 813 407 973 780 588 112 815 290 554 230 768 804 974 3 745\r\n", "output": "10833\r\n"}, {"input": "1 1 1\r\n1\r\n1000000000\r\n", "output": "1999999998\r\n"}, {"input": "1 1 1\r\n1000000000\r\n1\r\n", "output": "999999999\r\n"}, {"input": "1 1 1000000000\r\n1000000000\r\n1\r\n", "output": "1999999998\r\n"}, {"input": "1 1 1000000000\r\n1\r\n1000000000\r\n", "output": "999999999\r\n"}, {"input": "2 2 4\r\n3 4\r\n5 6\r\n", "output": "4\r\n"}, {"input": "2 2 5\r\n1 2\r\n3 1000000000\r\n", "output": "1999999993\r\n"}, {"input": "1 1 1000000000\r\n1000000000\r\n1\r\n", "output": "1999999998\r\n"}, {"input": "2 2 1\r\n2 3\r\n4 100\r\n", "output": "196\r\n"}, {"input": "2 2 10\r\n3 12\r\n1 9\r\n", "output": "11\r\n"}, {"input": "3 3 1\r\n1 2 3\r\n999 1000000000 1\r\n", "output": "1999999996\r\n"}, {"input": "1 1 1\r\n1\r\n1\r\n", "output": "0\r\n"}, {"input": "1 1 1\r\n1\r\n1000000000\r\n", "output": "1999999998\r\n"}, {"input": "1 1 1000000000\r\n1000000000\r\n10\r\n", "output": "1999999980\r\n"}, {"input": "2 2 7122\r\n123 456\r\n1 4444\r\n", "output": "7243\r\n"}, {"input": "1 1 10\r\n5\r\n15\r\n", "output": "15\r\n"}, {"input": "2 4 1000\r\n1000 999\r\n1 1000 2 999\r\n", "output": "1\r\n"}, {"input": "2 2 1000\r\n10 1010\r\n1 1001\r\n", "output": "1008\r\n"}, {"input": "1 1 1\r\n2\r\n1000000000\r\n", "output": "1999999997\r\n"}, {"input": "2 2 3\r\n1 5\r\n5 1\r\n", "output": "2\r\n"}, {"input": "2 2 5\r\n2 3\r\n4 6\r\n", "output": "4\r\n"}, {"input": "2 2 10\r\n5 6\r\n4 6\r\n", "output": "7\r\n"}, {"input": "3 4 10\r\n5 7 9\r\n6 8 14 4\r\n", "output": "7\r\n"}, {"input": "1 1 10\r\n10\r\n10\r\n", "output": "0\r\n"}, {"input": "1 1 50\r\n1\r\n1000000000\r\n", "output": "1999999949\r\n"}, {"input": "1 1 42\r\n666\r\n1337\r\n", "output": "1966\r\n"}, {"input": "2 2 10\r\n9 11\r\n11 8\r\n", "output": "3\r\n"}, {"input": "3 10 5\r\n1 2 3\r\n10000 9999 9998 9997 9996 9995 9994 7 6 5\r\n", "output": "6\r\n"}, {"input": "1 1 2\r\n1\r\n1000000000\r\n", "output": "1999999997\r\n"}, {"input": "2 2 100\r\n99 150\r\n1 150\r\n", "output": "197\r\n"}, {"input": "3 3 4\r\n1 101 102\r\n2 3 100\r\n", "output": "99\r\n"}]
false
stdio
null
true
214/B
214
B
PyPy 3-64
TESTS
12
248
10,342,400
162315490
n = int(input()) ls = [[] for _ in range(3)] for x in input().split(): y = int(x) ls[y % 3].append(y) s = sum(ls[1] + ls[2]) % 3 if s == 1: b = ls[0] if ls[1]: ls[1].remove(min(ls[1])) b = ls[0] + ls[1] + ls[2] elif len(ls[2]) >= 2: ls[2].remove(min(ls[2])) ls[2].remove(min(ls[2])) b = ls[0] + ls[2] b.sort(reverse=True) if b and b[-1] == 0: print(''.join(map(str, b))) else: print(-1) elif s == 2: b = ls[0] if ls[2]: ls[2].remove(min(ls[2])) b = ls[0] + ls[1] + ls[2] elif len(ls[1]) >= 2: ls[1].remove(min(ls[1])) ls[1].remove(min(ls[1])) b = ls[0] + ls[1] b.sort(reverse=True) if b and b[-1] == 0: print(''.join(map(str, b))) else: print(-1) else: b = ls[0] + ls[1] + ls[2] b.sort(reverse=True) if b and b[-1] == 0: print(''.join(map(str, b))) else: print(-1)
101
342
14,131,200
219139150
def main(): n = int(input()) a = list(map(int, input().split())) sum_val = sum(a) m = 0 uno = [] dos = [] a.sort(reverse=True) if a[-1]: print(-1) return if sum_val % 3 == 0: if not a[0]: print(0) return for i in range(n): print(a[i], end="") print() elif sum_val % 3 == 1: k = 0 for i in range(n - 1, -1, -1): if a[i] % 3 == 2: dos.append(i) elif a[i] % 3 == 1: a[i] = -1 k = 1 break if not k: if len(dos) < 2: print(-1) return a[dos[0]] = -1 a[dos[1]] = -1 for i in range(n): if a[i] >= 0: uno.append(a[i]) if uno[0] == 0: print(0) else: for i in range(len(uno)): print(uno[i], end="") print() else: k = 0 for i in range(n - 1, -1, -1): if a[i] % 3 == 1: uno.append(i) elif a[i] % 3 == 2: a[i] = -1 k = 1 break if not k: if len(uno) < 2: print(-1) return a[uno[0]] = -1 a[uno[1]] = -1 for i in range(n): if a[i] >= 0: dos.append(a[i]) if dos[0] == 0: print(0) else: for i in range(len(dos)): print(dos[i], end="") print() if __name__ == "__main__": main()
Codeforces Round 131 (Div. 2)
CF
2,012
2
256
Hometask
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set.
A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space.
On a single line print the answer to the problem. If such number does not exist, then you should print -1.
null
In the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
[{"input": "1\n0", "output": "0"}, {"input": "11\n3 4 5 4 5 3 5 3 4 4 0", "output": "5554443330"}, {"input": "8\n3 2 5 1 5 2 2 3", "output": "-1"}]
1,600
["brute force", "constructive algorithms", "greedy", "math"]
101
[{"input": "1\r\n0\r\n", "output": "0\r\n"}, {"input": "11\r\n3 4 5 4 5 3 5 3 4 4 0\r\n", "output": "5554443330\r\n"}, {"input": "8\r\n3 2 5 1 5 2 2 3\r\n", "output": "-1\r\n"}, {"input": "12\r\n5 3 3 3 2 5 5 1 2 1 4 1\r\n", "output": "-1\r\n"}, {"input": "8\r\n5 5 4 1 5 5 5 3\r\n", "output": "-1\r\n"}, {"input": "12\r\n3 1 2 3 2 0 2 2 2 0 2 3\r\n", "output": "33322222200\r\n"}, {"input": "12\r\n5 1 4 4 2 1 7 7 4 2 5 1\r\n", "output": "-1\r\n"}, {"input": "5\r\n3 6 1 6 2\r\n", "output": "-1\r\n"}, {"input": "11\r\n3 9 9 6 4 3 6 4 9 6 0\r\n", "output": "999666330\r\n"}, {"input": "5\r\n9 6 6 6 1\r\n", "output": "-1\r\n"}, {"input": "10\r\n2 0 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "10\r\n1 0 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n1 1 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 2 2 0\r\n", "output": "0\r\n"}, {"input": "6\r\n3 3 2 2 2 0\r\n", "output": "332220\r\n"}, {"input": "7\r\n3 3 2 2 2 2 0\r\n", "output": "332220\r\n"}, {"input": "6\r\n0 3 3 1 1 1\r\n", "output": "331110\r\n"}, {"input": "7\r\n0 3 3 1 1 1 1\r\n", "output": "331110\r\n"}, {"input": "7\r\n0 3 3 4 4 4 4\r\n", "output": "444330\r\n"}, {"input": "7\r\n0 3 3 2 2 4 4\r\n", "output": "4433220\r\n"}, {"input": "7\r\n4 2 3 3 0 0 0\r\n", "output": "4332000\r\n"}, {"input": "4\r\n1 1 0 3\r\n", "output": "30\r\n"}, {"input": "4\r\n3 0 2 2\r\n", "output": "30\r\n"}, {"input": "8\r\n3 3 3 5 5 0 0 0\r\n", "output": "333000\r\n"}, {"input": "8\r\n3 3 6 3 0 7 7 9\r\n", "output": "963330\r\n"}, {"input": "9\r\n1 2 3 4 5 6 7 8 9\r\n", "output": "-1\r\n"}, {"input": "9\r\n9 9 9 9 9 9 9 9 9\r\n", "output": "-1\r\n"}, {"input": "1\r\n0\r\n", "output": "0\r\n"}, {"input": "2\r\n9 0\r\n", "output": "90\r\n"}, {"input": "10\r\n3 0 2 2 2 2 2 2 2 2\r\n", "output": "32222220\r\n"}, {"input": "10\r\n3 0 1 1 1 1 1 1 1 1\r\n", "output": "31111110\r\n"}, {"input": "10\r\n3 0 4 4 4 4 4 4 4 4\r\n", "output": "44444430\r\n"}, {"input": "10\r\n2 0 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "10\r\n2 2 0 0 0 0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n5 5 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n1 4 0\r\n", "output": "0\r\n"}, {"input": "3\r\n0 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n0 1 4 3\r\n", "output": "30\r\n"}, {"input": "3\r\n2 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n0 1 2 3\r\n", "output": "3210\r\n"}, {"input": "4\r\n1 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n8 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "2\r\n0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 8 5 6\r\n", "output": "600\r\n"}, {"input": "4\r\n5 8 3 0\r\n", "output": "30\r\n"}, {"input": "4\r\n1 4 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n0 0 1\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n1 0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n0 0 0 0\r\n", "output": "0\r\n"}, {"input": "3\r\n0 0 4\r\n", "output": "0\r\n"}, {"input": "2\r\n0 1\r\n", "output": "0\r\n"}, {"input": "4\r\n1 1 0 0\r\n", "output": "0\r\n"}, {"input": "6\r\n2 2 0 0 0 0\r\n", "output": "0\r\n"}, {"input": "5\r\n3 2 5 0 0\r\n", "output": "300\r\n"}, {"input": "4\r\n5 3 2 0\r\n", "output": "30\r\n"}, {"input": "5\r\n0 0 0 2 2\r\n", "output": "0\r\n"}, {"input": "5\r\n0 0 0 0 1\r\n", "output": "0\r\n"}, {"input": "4\r\n0 3 5 8\r\n", "output": "30\r\n"}]
false
stdio
null
true
849/B
849
B
Python 3
TESTS
12
61
204,800
29984813
get = lambda cast:[cast(x) for x in input().split()] _ = input() seq = get(int) seg = 0 odd = lambda x: x % 2 == 1 def no(): print('No') exit(0) def yes(): print('Yes') exit(0) #for idx in range(len(seq)): idx = 0 c = [(v - seq[idx]) / (i - idx) for (i , v) in enumerate(seq) if i != idx] #print(c) rep = None for i in c: if c.count(i) != 1: rep = i break nc = [(i+1 , seq[i+1]) for (i , v) in enumerate(c) if v != rep] #print(nc) if len(nc) == 0: #print('n1') no() #continue if len(nc) == 1: yes() nc = [(v[1] - nc[0][1]) / (v[0] - nc[0][0]) for (i , v) in enumerate(nc) if i != idx] if len(set(nc)) == 1 and rep == None: yes() if len(set(nc)) != 1 or rep != nc[0]: #print('nn') no() yes() #print('last') #no()
52
62
307,200
30306234
n=int(input()) l=list(map(int,input().split())) l2=[0,0,0] l2[0]=l[0]-l[1] l2[1]=l[1]-l[2] l2[2]=(l[0]-l[2])/2 x=-1 for j in range(3) : l1=[] for i in range(n) : l1.append(l[i]+l2[j]*(i+1)) if len(set(l1))==2 : print('Yes') exit() print('No')
Codeforces Round 431 (Div. 2)
CF
2,017
1
256
Tell Your World
Connect the countless points with lines, till we reach the faraway yonder. There are n points on a coordinate plane, the i-th of which being (i, yi). Determine whether it's possible to draw two parallel and non-overlapping lines, such that every point in the set lies on exactly one of them, and each of them passes through at least one point in the set.
The first line of input contains a positive integer n (3 ≤ n ≤ 1 000) — the number of points. The second line contains n space-separated integers y1, y2, ..., yn ( - 109 ≤ yi ≤ 109) — the vertical coordinates of each point.
Output "Yes" (without quotes) if it's possible to fulfill the requirements, and "No" otherwise. You can print each letter in any case (upper or lower).
null
In the first example, there are five points: (1, 7), (2, 5), (3, 8), (4, 6) and (5, 9). It's possible to draw a line that passes through points 1, 3, 5, and another one that passes through points 2, 4 and is parallel to the first one. In the second example, while it's possible to draw two lines that cover all points, they cannot be made parallel. In the third example, it's impossible to satisfy both requirements at the same time.
[{"input": "5\n7 5 8 6 9", "output": "Yes"}, {"input": "5\n-1 -2 0 0 -5", "output": "No"}, {"input": "5\n5 4 3 2 1", "output": "No"}, {"input": "5\n1000000000 0 0 0 0", "output": "Yes"}]
1,600
["brute force", "geometry"]
52
[{"input": "5\r\n7 5 8 6 9\r\n", "output": "Yes\r\n"}, {"input": "5\r\n-1 -2 0 0 -5\r\n", "output": "No\r\n"}, {"input": "5\r\n5 4 3 2 1\r\n", "output": "No\r\n"}, {"input": "5\r\n1000000000 0 0 0 0\r\n", "output": "Yes\r\n"}, {"input": "5\r\n1000000000 1 0 -999999999 -1000000000\r\n", "output": "Yes\r\n"}, {"input": "3\r\n998 244 353\r\n", "output": "Yes\r\n"}, {"input": "3\r\n-1000000000 0 1000000000\r\n", "output": "No\r\n"}, {"input": "5\r\n-1 -1 -1 -1 1\r\n", "output": "Yes\r\n"}, {"input": "4\r\n-9763 530 3595 6660\r\n", "output": "Yes\r\n"}, {"input": "4\r\n-253090305 36298498 374072642 711846786\r\n", "output": "Yes\r\n"}, {"input": "5\r\n-186772848 -235864239 -191561068 -193955178 -243046569\r\n", "output": "Yes\r\n"}, {"input": "5\r\n-954618456 -522919664 -248330428 -130850748 300848044\r\n", "output": "Yes\r\n"}, {"input": "10\r\n4846 6705 2530 5757 5283 -944 -2102 -3260 -4418 2913\r\n", "output": "No\r\n"}, {"input": "10\r\n-6568 -5920 -5272 -4624 -2435 -635 -2680 -2032 -1384 6565\r\n", "output": "No\r\n"}, {"input": "20\r\n319410377 286827025 254243673 221660321 189076969 156493617 123910265 91326913 58743561 26160209 -6423143 -39006495 -71589847 -104173199 -136756551 -169339903 -201923255 -234506607 -267089959 -299673311\r\n", "output": "No\r\n"}, {"input": "20\r\n-975467170 758268840 -975467171 758268839 -975467172 758268838 -975467173 758268837 -975467174 758268836 -975467175 758268835 -975467176 758268834 -975467177 758268833 -975467178 758268832 -975467179 758268831\r\n", "output": "Yes\r\n"}, {"input": "4\r\n1 0 3 0\r\n", "output": "No\r\n"}, {"input": "4\r\n100 2 3 4\r\n", "output": "Yes\r\n"}, {"input": "5\r\n7 5 8 6 3\r\n", "output": "No\r\n"}, {"input": "3\r\n1000000000 1000000000 -1000000000\r\n", "output": "Yes\r\n"}, {"input": "4\r\n1 0 1 4\r\n", "output": "Yes\r\n"}, {"input": "7\r\n1 2 -1 0 1 6 7\r\n", "output": "Yes\r\n"}, {"input": "4\r\n0 0 4 0\r\n", "output": "Yes\r\n"}, {"input": "7\r\n0 0 2 3 4 5 5\r\n", "output": "Yes\r\n"}, {"input": "5\r\n7 5 8 6 8\r\n", "output": "No\r\n"}, {"input": "5\r\n1 2 9 4 5\r\n", "output": "Yes\r\n"}, {"input": "8\r\n1 12 3 14 5 16 7 8\r\n", "output": "Yes\r\n"}, {"input": "5\r\n1 6 7 4 9\r\n", "output": "Yes\r\n"}, {"input": "5\r\n2 1 0 1 2\r\n", "output": "No\r\n"}, {"input": "4\r\n0 0 1 3\r\n", "output": "Yes\r\n"}, {"input": "4\r\n100 50 50 10000000\r\n", "output": "No\r\n"}, {"input": "5\r\n1 2 3 3 3\r\n", "output": "No\r\n"}, {"input": "5\r\n1 2 6 10 17\r\n", "output": "Yes\r\n"}, {"input": "4\r\n1 3 4 4\r\n", "output": "Yes\r\n"}, {"input": "4\r\n100 50 50 1000000\r\n", "output": "No\r\n"}, {"input": "6\r\n1 2 4 5 7 9\r\n", "output": "No\r\n"}, {"input": "6\r\n0 0 1 2 3 4\r\n", "output": "Yes\r\n"}, {"input": "5\r\n7 5 9 10 8\r\n", "output": "Yes\r\n"}, {"input": "7\r\n1 2 2 1 2 2 1\r\n", "output": "Yes\r\n"}, {"input": "4\r\n2 2 4 5\r\n", "output": "Yes\r\n"}, {"input": "6\r\n1 2 1 3 4 5\r\n", "output": "No\r\n"}, {"input": "4\r\n1 3 3 6\r\n", "output": "No\r\n"}, {"input": "5\r\n1 2 -3 4 -1\r\n", "output": "Yes\r\n"}]
false
stdio
null
true
845/C
845
C
Python 3
TESTS
17
717
19,865,600
62899107
import sys course_num = sys.stdin.readline().strip("\n") courses = [] flag = 1 for i in range(0,int(course_num)): course = sys.stdin.readline().strip("\n").split(' ') courses.append([int(course[0]), int(course[1])]) #print(courses) courses.sort(key=lambda x:x[0]) a = [] b = [[0,0]] a.append(courses[0]) for i in range(1,int(course_num)): if courses[i][0]<=a[-1][1]: if courses[i][0]<=b[-1][1]: flag = 0 break else: b.append(courses[i]) else: a.append(courses[i]) if flag==0: print('NO') else: print('YES')
58
701
94,003,200
131984136
import sys import io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline n = int(input()) X = set() LR = [] for i in range(n): l, r = map(int, input().split()) l -= 1 r -= 1 X.add(l) X.add(l+1) X.add(r) X.add(r+1) LR.append((l, r)) X = list(X) X.sort() d = {} for i, x in enumerate(X): d[x] = i N = len(d) imos = [0]*(N+5) for l, r in LR: l = d[l] r = d[r] imos[l] += 1 imos[r+1] -= 1 from itertools import accumulate imos = list(accumulate(imos)) if max(imos) <= 2: print('YES') else: print('NO')
Educational Codeforces Round 27
ICPC
2,017
2
256
Two TVs
Polycarp is a great fan of television. He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment li and ends at moment ri. Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV. Polycarp wants to check out all n shows. Are two TVs enough to do so?
The first line contains one integer n (1 ≤ n ≤ 2·105) — the number of shows. Each of the next n lines contains two integers li and ri (0 ≤ li < ri ≤ 109) — starting and ending time of i-th show.
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
null
null
[{"input": "3\n1 2\n2 3\n4 5", "output": "YES"}, {"input": "4\n1 2\n2 3\n2 3\n1 2", "output": "NO"}]
1,500
["data structures", "greedy", "sortings"]
58
[{"input": "3\r\n1 2\r\n2 3\r\n4 5\r\n", "output": "YES\r\n"}, {"input": "4\r\n1 2\r\n2 3\r\n2 3\r\n1 2\r\n", "output": "NO\r\n"}, {"input": "4\r\n0 1\r\n1 2\r\n2 3\r\n3 4\r\n", "output": "YES\r\n"}, {"input": "3\r\n1 2\r\n2 3\r\n2 4\r\n", "output": "NO\r\n"}, {"input": "3\r\n0 100\r\n0 100\r\n0 100\r\n", "output": "NO\r\n"}, {"input": "1\r\n0 1000000000\r\n", "output": "YES\r\n"}, {"input": "2\r\n0 1\r\n0 1\r\n", "output": "YES\r\n"}, {"input": "3\r\n2 3\r\n4 5\r\n1 6\r\n", "output": "YES\r\n"}, {"input": "5\r\n1 3\r\n1 4\r\n4 10\r\n5 8\r\n9 11\r\n", "output": "YES\r\n"}, {"input": "3\r\n1 2\r\n1 2\r\n2 3\r\n", "output": "NO\r\n"}, {"input": "4\r\n1 100\r\n10 15\r\n20 25\r\n30 35\r\n", "output": "YES\r\n"}, {"input": "3\r\n1 8\r\n6 7\r\n8 11\r\n", "output": "YES\r\n"}, {"input": "5\r\n1 2\r\n3 5\r\n4 7\r\n8 9\r\n5 10\r\n", "output": "NO\r\n"}, {"input": "4\r\n1 7\r\n2 3\r\n4 5\r\n6 7\r\n", "output": "YES\r\n"}, {"input": "4\r\n1 100\r\n50 51\r\n60 90\r\n51 52\r\n", "output": "NO\r\n"}, {"input": "3\r\n1 10\r\n2 9\r\n3 8\r\n", "output": "NO\r\n"}, {"input": "2\r\n0 4\r\n0 4\r\n", "output": "YES\r\n"}, {"input": "2\r\n0 2\r\n0 6\r\n", "output": "YES\r\n"}, {"input": "5\r\n3 4\r\n21 26\r\n12 17\r\n9 14\r\n15 16\r\n", "output": "YES\r\n"}, {"input": "5\r\n1 4\r\n13 15\r\n11 12\r\n9 15\r\n2 5\r\n", "output": "YES\r\n"}, {"input": "4\r\n16 19\r\n9 14\r\n14 15\r\n15 19\r\n", "output": "YES\r\n"}, {"input": "5\r\n16 19\r\n23 29\r\n3 8\r\n23 26\r\n22 23\r\n", "output": "NO\r\n"}, {"input": "5\r\n19 23\r\n12 17\r\n16 21\r\n20 23\r\n8 10\r\n", "output": "NO\r\n"}, {"input": "5\r\n8 10\r\n4 10\r\n3 4\r\n14 15\r\n17 19\r\n", "output": "YES\r\n"}, {"input": "3\r\n2 8\r\n5 7\r\n6 7\r\n", "output": "NO\r\n"}, {"input": "5\r\n10 12\r\n4 6\r\n21 24\r\n9 12\r\n7 13\r\n", "output": "NO\r\n"}, {"input": "5\r\n0 3\r\n14 16\r\n6 8\r\n5 9\r\n9 15\r\n", "output": "YES\r\n"}, {"input": "5\r\n6 12\r\n23 25\r\n6 7\r\n19 25\r\n10 11\r\n", "output": "YES\r\n"}, {"input": "5\r\n15 18\r\n23 24\r\n23 28\r\n22 24\r\n15 19\r\n", "output": "NO\r\n"}, {"input": "4\r\n1 8\r\n8 9\r\n5 7\r\n1 4\r\n", "output": "YES\r\n"}, {"input": "3\r\n6 10\r\n1 9\r\n2 5\r\n", "output": "YES\r\n"}, {"input": "3\r\n1 8\r\n5 6\r\n6 9\r\n", "output": "NO\r\n"}, {"input": "4\r\n2 3\r\n5 9\r\n8 10\r\n9 10\r\n", "output": "NO\r\n"}, {"input": "4\r\n0 8\r\n6 7\r\n5 9\r\n1 4\r\n", "output": "NO\r\n"}, {"input": "3\r\n6 9\r\n0 1\r\n0 2\r\n", "output": "YES\r\n"}, {"input": "5\r\n0 6\r\n21 25\r\n18 19\r\n0 3\r\n6 12\r\n", "output": "YES\r\n"}, {"input": "4\r\n1 5\r\n6 9\r\n4 8\r\n1 3\r\n", "output": "YES\r\n"}, {"input": "2\r\n2 5\r\n0 5\r\n", "output": "YES\r\n"}, {"input": "4\r\n5 8\r\n11 15\r\n3 7\r\n10 14\r\n", "output": "YES\r\n"}, {"input": "3\r\n12 14\r\n0 4\r\n2 3\r\n", "output": "YES\r\n"}, {"input": "4\r\n4 10\r\n0 1\r\n2 10\r\n0 5\r\n", "output": "NO\r\n"}, {"input": "4\r\n0 3\r\n0 1\r\n2 4\r\n2 5\r\n", "output": "NO\r\n"}]
false
stdio
null
true
721/B
721
B
Python 3
TESTS
13
109
307,200
82029590
n, k = map(int, input().split()) s = [] for i in range(n): inp = input() s.append(inp) p = input() s = set(s) smallerLen = equalLen = time = 0 limit = k for i in s: if (len(i) < len(p)): if (k == 0): smallerLen += 5 k = limit smallerLen += 1 elif (len(i) == len(p)): if (k == 0): equalLen += 5 k = limit equalLen += 1 else: continue k -= 1 print(smallerLen + 1, smallerLen + equalLen)
66
46
0
158521558
n, k = map(int, input().split()) a = [input() for i in range(n)] pa = input() for i in range(len(a)): a[i] = [len(a[i])] + [a[i]] a.sort() for i in range(len(a)): if len(a[i][1]) == len(pa): ind1 = i break for i in range(len(a)-1, -1, -1): if len(a[i][1]) == len(pa): ind2 = i break print(ind1 + 1 + ind1 // k * 5, ind2 + 1 + ind2 // k * 5)
Codeforces Round 374 (Div. 2)
CF
2,016
2
256
Passwords
Vanya is managed to enter his favourite site Codehorses. Vanya uses n distinct passwords for sites at all, however he can't remember which one exactly he specified during Codehorses registration. Vanya will enter passwords in order of non-decreasing their lengths, and he will enter passwords of same length in arbitrary order. Just when Vanya will have entered the correct password, he is immediately authorized on the site. Vanya will not enter any password twice. Entering any passwords takes one second for Vanya. But if Vanya will enter wrong password k times, then he is able to make the next try only 5 seconds after that. Vanya makes each try immediately, that is, at each moment when Vanya is able to enter password, he is doing that. Determine how many seconds will Vanya need to enter Codehorses in the best case for him (if he spends minimum possible number of second) and in the worst case (if he spends maximum possible amount of seconds).
The first line of the input contains two integers n and k (1 ≤ n, k ≤ 100) — the number of Vanya's passwords and the number of failed tries, after which the access to the site is blocked for 5 seconds. The next n lines contains passwords, one per line — pairwise distinct non-empty strings consisting of latin letters and digits. Each password length does not exceed 100 characters. The last line of the input contains the Vanya's Codehorses password. It is guaranteed that the Vanya's Codehorses password is equal to some of his n passwords.
Print two integers — time (in seconds), Vanya needs to be authorized to Codehorses in the best case for him and in the worst case respectively.
null
Consider the first sample case. As soon as all passwords have the same length, Vanya can enter the right password at the first try as well as at the last try. If he enters it at the first try, he spends exactly 1 second. Thus in the best case the answer is 1. If, at the other hand, he enters it at the last try, he enters another 4 passwords before. He spends 2 seconds to enter first 2 passwords, then he waits 5 seconds as soon as he made 2 wrong tries. Then he spends 2 more seconds to enter 2 wrong passwords, again waits 5 seconds and, finally, enters the correct password spending 1 more second. In summary in the worst case he is able to be authorized in 15 seconds. Consider the second sample case. There is no way of entering passwords and get the access to the site blocked. As soon as the required password has length of 2, Vanya enters all passwords of length 1 anyway, spending 2 seconds for that. Then, in the best case, he immediately enters the correct password and the answer for the best case is 3, but in the worst case he enters wrong password of length 2 and only then the right one, spending 4 seconds at all.
[{"input": "5 2\ncba\nabc\nbb1\nabC\nABC\nabc", "output": "1 15"}, {"input": "4 100\n11\n22\n1\n2\n22", "output": "3 4"}]
1,100
["implementation", "math", "sortings", "strings"]
66
[{"input": "5 2\r\ncba\r\nabc\r\nbb1\r\nabC\r\nABC\r\nabc\r\n", "output": "1 15\r\n"}, {"input": "4 100\r\n11\r\n22\r\n1\r\n2\r\n22\r\n", "output": "3 4\r\n"}, {"input": "1 1\r\na1\r\na1\r\n", "output": "1 1\r\n"}, {"input": "1 100\r\na1\r\na1\r\n", "output": "1 1\r\n"}, {"input": "2 1\r\nabc\r\nAbc\r\nAbc\r\n", "output": "1 7\r\n"}, {"input": "2 2\r\nabc\r\nAbc\r\nabc\r\n", "output": "1 2\r\n"}, {"input": "2 1\r\nab\r\nabc\r\nab\r\n", "output": "1 1\r\n"}, {"input": "2 2\r\nab\r\nabc\r\nab\r\n", "output": "1 1\r\n"}, {"input": "2 1\r\nab\r\nabc\r\nabc\r\n", "output": "7 7\r\n"}, {"input": "2 2\r\nab\r\nabc\r\nabc\r\n", "output": "2 2\r\n"}, {"input": "10 3\r\nOIbV1igi\r\no\r\nZS\r\nQM\r\n9woLzI\r\nWreboD\r\nQ7yl\r\nA5Rb\r\nS9Lno72TkP\r\nfT97o\r\no\r\n", "output": "1 1\r\n"}, {"input": "10 3\r\nHJZNMsT\r\nLaPcH2C\r\nlrhqIO\r\n9cxw\r\noTC1XwjW\r\nGHL9Ul6\r\nUyIs\r\nPuzwgR4ZKa\r\nyIByoKR5\r\nd3QA\r\nPuzwgR4ZKa\r\n", "output": "25 25\r\n"}, {"input": "20 5\r\nvSyC787KlIL8kZ2Uv5sw\r\nWKWOP\r\n7i8J3E8EByIq\r\nNW2VyGweL\r\nmyR2sRNu\r\nmXusPP0\r\nf4jgGxra\r\n4wHRzRhOCpEt\r\npPz9kybGb\r\nOtSpePCRoG5nkjZ2VxRy\r\nwHYsSttWbJkg\r\nKBOP9\r\nQfiOiFyHPPsw3GHo8J8\r\nxB8\r\nqCpehZEeEhdq\r\niOLjICK6\r\nQ91\r\nHmCsfMGTFKoFFnv238c\r\nJKjhg\r\ngkEUh\r\nKBOP9\r\n", "output": "3 11\r\n"}, {"input": "15 2\r\nw6S9WyU\r\nMVh\r\nkgUhQHW\r\nhGQNOF\r\nUuym\r\n7rGQA\r\nBM8vLPRB\r\n9E\r\nDs32U\r\no\r\nz1aV2C5T\r\n8\r\nzSXjrqQ\r\n1FO\r\n3kIt\r\nBM8vLPRB\r\n", "output": "44 50\r\n"}, {"input": "20 2\r\ni\r\n5Rp6\r\nE4vsr\r\nSY\r\nORXx\r\nh13C\r\nk6tzC\r\ne\r\nN\r\nKQf4C\r\nWZcdL\r\ndiA3v\r\n0InQT\r\nuJkAr\r\nGCamp\r\nBuIRd\r\nY\r\nM\r\nxZYx7\r\n0a5A\r\nWZcdL\r\n", "output": "36 65\r\n"}, {"input": "20 2\r\naWLQ6\r\nSgQ9r\r\nHcPdj\r\n2BNaO\r\n3TjNb\r\nnvwFM\r\nqsKt7\r\nFnb6N\r\nLoc0p\r\njxuLq\r\nBKAjf\r\nEKgZB\r\nBfOSa\r\nsMIvr\r\nuIWcR\r\nIura3\r\nLAqSf\r\ntXq3G\r\n8rQ8I\r\n8otAO\r\nsMIvr\r\n", "output": "1 65\r\n"}, {"input": "20 15\r\n0ZpQugVlN7\r\nm0SlKGnohN\r\nRFXTqhNGcn\r\n1qm2ZbB\r\nQXtJWdf78P\r\nbc2vH\r\nP21dty2Z1P\r\nm2c71LFhCk\r\n23EuP1Dvh3\r\nanwri5RhQN\r\n55v6HYv288\r\n1u5uKOjM5r\r\n6vg0GC1\r\nDAPYiA3ns1\r\nUZaaJ3Gmnk\r\nwB44x7V4Zi\r\n4hgB2oyU8P\r\npYFQpy8gGK\r\ndbz\r\nBv\r\n55v6HYv288\r\n", "output": "6 25\r\n"}, {"input": "3 1\r\na\r\nb\r\naa\r\naa\r\n", "output": "13 13\r\n"}, {"input": "6 3\r\nab\r\nac\r\nad\r\nabc\r\nabd\r\nabe\r\nabc\r\n", "output": "9 11\r\n"}, {"input": "4 2\r\n1\r\n2\r\n11\r\n22\r\n22\r\n", "output": "8 9\r\n"}, {"input": "2 1\r\n1\r\n12\r\n12\r\n", "output": "7 7\r\n"}, {"input": "3 1\r\nab\r\nabc\r\nabd\r\nabc\r\n", "output": "7 13\r\n"}, {"input": "2 1\r\na\r\nab\r\nab\r\n", "output": "7 7\r\n"}, {"input": "5 2\r\na\r\nb\r\nc\r\nab\r\naa\r\naa\r\n", "output": "9 15\r\n"}, {"input": "6 1\r\n1\r\n2\r\n11\r\n22\r\n111\r\n2222\r\n22\r\n", "output": "13 19\r\n"}, {"input": "3 1\r\n1\r\n2\r\n11\r\n11\r\n", "output": "13 13\r\n"}, {"input": "10 4\r\na\r\nb\r\nc\r\nd\r\ne\r\nf\r\nab\r\ncd\r\nac\r\nad\r\nac\r\n", "output": "12 20\r\n"}, {"input": "4 2\r\na\r\nb\r\nc\r\nd\r\na\r\n", "output": "1 9\r\n"}, {"input": "4 1\r\n1\r\n2\r\n3\r\n4\r\n4\r\n", "output": "1 19\r\n"}, {"input": "5 1\r\na\r\nb\r\nc\r\nd\r\nef\r\nef\r\n", "output": "25 25\r\n"}, {"input": "6 4\r\n1\r\n2\r\n22\r\n33\r\n44\r\n555\r\n555\r\n", "output": "11 11\r\n"}, {"input": "5 2\r\na\r\nb\r\nc\r\nd\r\nab\r\nab\r\n", "output": "15 15\r\n"}, {"input": "6 2\r\n1\r\n2\r\n3\r\n4\r\n5\r\n23\r\n23\r\n", "output": "16 16\r\n"}, {"input": "4 2\r\na\r\nb\r\naa\r\nbb\r\naa\r\n", "output": "8 9\r\n"}, {"input": "5 4\r\na\r\nbb\r\ncc\r\ndd\r\nee\r\nbb\r\n", "output": "2 10\r\n"}, {"input": "4 1\r\na\r\nb\r\nc\r\nab\r\nab\r\n", "output": "19 19\r\n"}, {"input": "7 100\r\na\r\nb\r\nc\r\nd\r\ne\r\ng\r\nab\r\nab\r\n", "output": "7 7\r\n"}, {"input": "6 1\r\na\r\nb\r\nc\r\nd\r\ne\r\naa\r\naa\r\n", "output": "31 31\r\n"}, {"input": "4 1\r\na\r\nas\r\nasd\r\nasde\r\nasde\r\n", "output": "19 19\r\n"}, {"input": "5 2\r\n1\r\n2\r\n3\r\n11\r\n22\r\n22\r\n", "output": "9 15\r\n"}, {"input": "10 2\r\na\r\nb\r\nc\r\nd\r\nee\r\nff\r\ngg\r\nhh\r\nii\r\njj\r\nii\r\n", "output": "15 30\r\n"}, {"input": "3 1\r\na\r\nab\r\nbc\r\nab\r\n", "output": "7 13\r\n"}, {"input": "6 4\r\na\r\nb\r\nc\r\nbb\r\nbc\r\ncc\r\ncc\r\n", "output": "4 11\r\n"}]
false
stdio
null
true
369/A
369
A
Python 3
TESTS
61
124
307,200
76781582
n,m,k = map(int, input().split()) lst = list(map(int, input().split())) c1 = lst.count(1) c2 = lst.count(2) if m >= c1: m -= c1 c1 = 0 else: c1 -= m m = 0 if k >= c2: k -= c2 c2 = 0 else: c2 -= k k = 0 count = c1 if c2 > 0: if m > 0: c2 -= m count += c2 print(count)
63
46
0
5312869
n, m, k = list(map(int, input().split())) s = list(map(int, input().split())) q = 0 for i in s: if i == 1: q += 1 m -= q ans = 0 if m > 0: k += m else: ans = abs(m) if n - q > k: ans += n - q - k print(ans)
Codeforces Round 216 (Div. 2)
CF
2,013
1
256
Valera and Plates
Valera is a lazy student. He has m clean bowls and k clean plates. Valera has made an eating plan for the next n days. As Valera is lazy, he will eat exactly one dish per day. At that, in order to eat a dish, he needs exactly one clean plate or bowl. We know that Valera can cook only two types of dishes. He can eat dishes of the first type from bowls and dishes of the second type from either bowls or plates. When Valera finishes eating, he leaves a dirty plate/bowl behind. His life philosophy doesn't let him eat from dirty kitchenware. So sometimes he needs to wash his plate/bowl before eating. Find the minimum number of times Valera will need to wash a plate/bowl, if he acts optimally.
The first line of the input contains three integers n, m, k (1 ≤ n, m, k ≤ 1000) — the number of the planned days, the number of clean bowls and the number of clean plates. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 2). If ai equals one, then on day i Valera will eat a first type dish. If ai equals two, then on day i Valera will eat a second type dish.
Print a single integer — the minimum number of times Valera will need to wash a plate/bowl.
null
In the first sample Valera will wash a bowl only on the third day, so the answer is one. In the second sample, Valera will have the first type of the dish during all four days, and since there are only three bowls, he will wash a bowl exactly once. In the third sample, Valera will have the second type of dish for all three days, and as they can be eaten from either a plate or a bowl, he will never need to wash a plate/bowl.
[{"input": "3 1 1\n1 2 1", "output": "1"}, {"input": "4 3 1\n1 1 1 1", "output": "1"}, {"input": "3 1 2\n2 2 2", "output": "0"}, {"input": "8 2 2\n1 2 1 2 1 2 1 2", "output": "4"}]
900
["greedy", "implementation"]
63
[{"input": "3 1 1\r\n1 2 1\r\n", "output": "1\r\n"}, {"input": "4 3 1\r\n1 1 1 1\r\n", "output": "1\r\n"}, {"input": "3 1 2\r\n2 2 2\r\n", "output": "0\r\n"}, {"input": "8 2 2\r\n1 2 1 2 1 2 1 2\r\n", "output": "4\r\n"}, {"input": "2 100 100\r\n2 2\r\n", "output": "0\r\n"}, {"input": "1 1 1\r\n2\r\n", "output": "0\r\n"}, {"input": "233 100 1\r\n2 2 1 1 1 2 2 2 2 1 1 2 2 2 1 2 2 1 1 1 2 2 1 1 1 1 2 1 2 2 1 1 2 2 1 2 2 1 2 1 2 1 2 2 2 1 1 1 1 2 1 2 1 1 2 1 1 2 2 1 2 1 2 1 1 1 1 1 1 1 1 1 2 1 2 2 2 1 1 2 2 1 1 1 1 2 1 1 2 1 2 2 2 1 1 1 2 2 2 1 1 1 1 2 1 2 1 1 1 1 2 2 2 1 1 2 1 2 1 1 1 1 1 2 1 1 1 1 1 2 1 1 2 2 1 2 1 1 2 2 1 1 2 2 1 1 1 2 2 1 1 2 1 2 1 2 2 1 2 2 2 2 2 1 2 2 2 2 2 1 2 2 1 2 2 1 1 1 2 2 1 1 2 2 1 1 2 1 1 2 2 1 2 2 2 2 2 2 1 2 2 2 2 2 1 1 2 2 2 2 2 2 1 1 1 2 1 2 2 2 2 2 2 2 2 1 1 2 1 2 1 2 2\r\n", "output": "132\r\n"}, {"input": "123 100 1\r\n2 2 2 1 1 2 2 2 2 1 1 2 2 2 1 2 2 2 2 1 2 2 2 1 1 1 2 2 2 2 1 2 2 2 2 2 2 1 2 1 2 1 2 2 2 1 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 2 1 1 1 1 1 1 1 1 1 2 2 2 2 2 1 1 2 2 1 1 1 1 2 1 2 2 1 2 2 2 1 1 1 2 2 2 1 2 2 2 2 1 2 2 2 2 1 2 2 2 1 1 2 1 2 1 2 1 1 1\r\n", "output": "22\r\n"}, {"input": "188 100 1\r\n2 2 1 1 1 2 2 2 2 1 1 2 2 2 1 2 2 1 1 1 2 2 1 1 1 1 2 1 2 2 1 1 2 2 1 2 2 1 2 1 2 1 2 2 2 1 1 1 1 2 1 2 1 1 2 1 1 2 2 1 2 1 2 1 1 1 1 1 1 1 1 1 2 1 2 2 2 1 1 2 2 1 1 1 1 2 1 1 2 1 2 2 2 1 1 1 2 2 2 1 1 1 1 2 1 2 1 1 1 1 2 2 2 1 1 2 1 2 1 1 1 1 1 2 1 1 1 1 1 2 1 1 2 2 1 2 1 1 2 2 1 1 2 2 1 1 1 2 2 1 1 2 1 2 1 2 2 1 2 2 2 2 2 1 2 2 2 2 2 1 2 2 1 2 2 1 1 1 2 2 1 1 2 2 1 1 2 1\r\n", "output": "87\r\n"}, {"input": "3 1 2\r\n1 1 1\r\n", "output": "2\r\n"}, {"input": "3 2 2\r\n1 1 1\r\n", "output": "1\r\n"}, {"input": "3 2 1\r\n1 1 1\r\n", "output": "1\r\n"}, {"input": "3 1 1\r\n1 1 1\r\n", "output": "2\r\n"}, {"input": "5 1 2\r\n2 2 2 2 2\r\n", "output": "2\r\n"}, {"input": "5 2 2\r\n2 2 2 2 2\r\n", "output": "1\r\n"}, {"input": "5 2 1\r\n2 2 2 2 2\r\n", "output": "2\r\n"}, {"input": "5 1 1\r\n2 2 2 2 2\r\n", "output": "3\r\n"}, {"input": "1 1 2\r\n2\r\n", "output": "0\r\n"}, {"input": "1 2 2\r\n2\r\n", "output": "0\r\n"}, {"input": "1 2 1\r\n2\r\n", "output": "0\r\n"}, {"input": "1 1 1\r\n2\r\n", "output": "0\r\n"}, {"input": "6 3 1\r\n1 1 2 2 2 2\r\n", "output": "2\r\n"}, {"input": "100 40 20\r\n2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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": "40\r\n"}, {"input": "7 5 2\r\n2 2 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "10 4 4\r\n2 2 2 2 2 2 1 1 1 1\r\n", "output": "2\r\n"}, {"input": "3 2 1\r\n2 1 1\r\n", "output": "0\r\n"}, {"input": "7 6 1\r\n2 1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "7 5 1\r\n1 1 1 2 2 2 2\r\n", "output": "1\r\n"}, {"input": "5 3 1\r\n1 1 2 2 2\r\n", "output": "1\r\n"}, {"input": "3 1 1\r\n2 2 2\r\n", "output": "1\r\n"}, {"input": "5 2 2\r\n2 2 2 2 2\r\n", "output": "1\r\n"}, {"input": "3 1 3\r\n1 1 1\r\n", "output": "2\r\n"}, {"input": "5 2 1\r\n1 1 2 2 2\r\n", "output": "2\r\n"}, {"input": "4 3 2\r\n2 1 1 1\r\n", "output": "0\r\n"}, {"input": "4 2 1\r\n1 2 2 2\r\n", "output": "1\r\n"}, {"input": "14 4 7\r\n1 1 1 2 2 2 2 2 2 2 2 2 2 2\r\n", "output": "3\r\n"}, {"input": "12 10 4\r\n2 2 2 2 2 2 1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "5 3 2\r\n2 2 1 1 1\r\n", "output": "0\r\n"}]
false
stdio
null
true
883/K
883
K
Python 3
TESTS
4
109
307,200
68742949
# https://codeforces.com/contest/883/problem/K import heapq from heapq import heappush as push_ from heapq import heappop as pop_ n = int(input()) p = [list(map(int, input().split())) for _ in range(n)] Q = [] d = [s+g for s, g in p] for i, [s, g] in enumerate(p): push_(Q, (s+g, i)) flg = True while len(Q) > 0: S, ind = pop_(Q) if S > d[ind]: continue if ind > 0 and d[ind-1]-S > 1: if d[ind-1]-S > p[ind-1][1]: flg=False break sub = d[ind-1]-(S+1) d[ind-1] = d[ind-1] - sub push_(Q, (d[ind-1], ind-1)) elif ind < n-1 and d[ind+1]-S > 1: if d[ind+1] - S > p[ind+1][1]: flg=False break sub = d[ind+1]-(S+1) d[ind+1] = d[ind+1] - sub push_(Q, (d[ind+1], ind+1)) if flg==False: print(-1) else: print(sum([d[i]-p[i][0] for i in range(n)])) print(' '.join([str(x) for x in d])) #3 #4 5 #4 5 #4 10 #4 #1 100 #100 1 #1 100 #100 1 #3 #1 1 #100 100 #1 1
109
1,824
23,961,600
32543393
n=int(input()) a=[] for i in range(n): a.append(list(map(int,input().split()))) a[i][1]+=a[i][0] for i in range(1,n): if a[i][1]>a[i-1][1]+1: a[i][1]=a[i-1][1]+1 if a[i][1]<a[i][0]:exit(print(-1)) s=a[-1][1]-a[-1][0] for i in range(n-2,-1,-1): if a[i][1] > a[i + 1][1] + 1: a[i][1] = a[i + 1][1] + 1 if a[i][1] < a[i][0]: exit(print(-1)) s+=a[i][1]-a[i][0] print(s) for i in a: print(i[1],end=' ')
2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)
ICPC
2,017
3
256
Road Widening
Mayor of city S just hates trees and lawns. They take so much space and there could be a road on the place they occupy! The Mayor thinks that one of the main city streets could be considerably widened on account of lawn nobody needs anyway. Moreover, that might help reduce the car jams which happen from time to time on the street. The street is split into n equal length parts from left to right, the i-th part is characterized by two integers: width of road si and width of lawn gi. For each of n parts the Mayor should decide the size of lawn to demolish. For the i-th part he can reduce lawn width by integer xi (0 ≤ xi ≤ gi). After it new road width of the i-th part will be equal to s'i = si + xi and new lawn width will be equal to g'i = gi - xi. On the one hand, the Mayor wants to demolish as much lawn as possible (and replace it with road). On the other hand, he does not want to create a rapid widening or narrowing of the road, which would lead to car accidents. To avoid that, the Mayor decided that width of the road for consecutive parts should differ by at most 1, i.e. for each i (1 ≤ i < n) the inequation |s'i + 1 - s'i| ≤ 1 should hold. Initially this condition might not be true. You need to find the the total width of lawns the Mayor will destroy according to his plan.
The first line contains integer n (1 ≤ n ≤ 2·105) — number of parts of the street. Each of the following n lines contains two integers si, gi (1 ≤ si ≤ 106, 0 ≤ gi ≤ 106) — current width of road and width of the lawn on the i-th part of the street.
In the first line print the total width of lawns which will be removed. In the second line print n integers s'1, s'2, ..., s'n (si ≤ s'i ≤ si + gi) — new widths of the road starting from the first part and to the last. If there is no solution, print the only integer -1 in the first line.
null
null
[{"input": "3\n4 5\n4 5\n4 10", "output": "16\n9 9 10"}, {"input": "4\n1 100\n100 1\n1 100\n100 1", "output": "202\n101 101 101 101"}, {"input": "3\n1 1\n100 100\n1 1", "output": "-1"}]
1,800
["constructive algorithms", "greedy", "implementation"]
109
[{"input": "3\r\n4 5\r\n4 5\r\n4 10\r\n", "output": "16\r\n9 9 10 \r\n"}, {"input": "4\r\n1 100\r\n100 1\r\n1 100\r\n100 1\r\n", "output": "202\r\n101 101 101 101 \r\n"}, {"input": "3\r\n1 1\r\n100 100\r\n1 1\r\n", "output": "-1\r\n"}, {"input": "10\r\n21005 10850\r\n27020 13372\r\n28183 3724\r\n22874 13564\r\n27446 11493\r\n22522 10012\r\n24819 11529\r\n24166 11084\r\n24539 9211\r\n24152 9235\r\n", "output": "71869\r\n31855 31856 31857 31858 31859 31860 31861 31862 31863 31864 \r\n"}, {"input": "1\r\n1 0\r\n", "output": "0\r\n1 \r\n"}, {"input": "1\r\n1 1000000\r\n", "output": "1000000\r\n1000001 \r\n"}, {"input": "1\r\n1000000 1000000\r\n", "output": "1000000\r\n2000000 \r\n"}, {"input": "1\r\n1 0\r\n", "output": "0\r\n1 \r\n"}, {"input": "1\r\n1 0\r\n", "output": "0\r\n1 \r\n"}, {"input": "1\r\n1 1\r\n", "output": "1\r\n2 \r\n"}, {"input": "2\r\n2 2\r\n1 1\r\n", "output": "2\r\n3 2 \r\n"}, {"input": "2\r\n2 0\r\n1 0\r\n", "output": "0\r\n2 1 \r\n"}, {"input": "2\r\n2 1\r\n2 2\r\n", "output": "3\r\n3 4 \r\n"}, {"input": "3\r\n1 3\r\n2 1\r\n3 0\r\n", "output": "4\r\n4 3 3 \r\n"}, {"input": "3\r\n1 3\r\n1 3\r\n2 1\r\n", "output": "7\r\n4 4 3 \r\n"}, {"input": "3\r\n3 3\r\n2 0\r\n1 2\r\n", "output": "2\r\n3 2 3 \r\n"}, {"input": "4\r\n1 3\r\n2 3\r\n3 1\r\n1 0\r\n", "output": "-1\r\n"}, {"input": "4\r\n1 2\r\n4 2\r\n4 2\r\n4 2\r\n", "output": "5\r\n3 4 5 6 \r\n"}, {"input": "4\r\n1 3\r\n1 4\r\n2 0\r\n4 1\r\n", "output": "-1\r\n"}, {"input": "5\r\n3 5\r\n4 5\r\n1 0\r\n2 3\r\n1 1\r\n", "output": "-1\r\n"}, {"input": "5\r\n2 0\r\n3 0\r\n3 0\r\n3 5\r\n2 4\r\n", "output": "4\r\n2 3 3 4 5 \r\n"}, {"input": "5\r\n1 0\r\n4 2\r\n1 5\r\n1 5\r\n1 4\r\n", "output": "-1\r\n"}, {"input": "6\r\n1 1\r\n3 4\r\n3 5\r\n2 5\r\n6 3\r\n2 3\r\n", "output": "8\r\n2 3 4 5 6 5 \r\n"}, {"input": "6\r\n5 3\r\n4 4\r\n5 5\r\n1 2\r\n6 3\r\n6 4\r\n", "output": "-1\r\n"}, {"input": "6\r\n1 5\r\n6 2\r\n2 1\r\n1 2\r\n3 6\r\n1 1\r\n", "output": "-1\r\n"}, {"input": "7\r\n3 0\r\n1 5\r\n7 7\r\n6 5\r\n1 6\r\n1 6\r\n7 2\r\n", "output": "-1\r\n"}, {"input": "7\r\n7 5\r\n1 2\r\n3 0\r\n3 1\r\n4 5\r\n2 6\r\n6 3\r\n", "output": "-1\r\n"}, {"input": "7\r\n3 1\r\n5 0\r\n4 1\r\n7 5\r\n1 3\r\n7 6\r\n1 4\r\n", "output": "-1\r\n"}, {"input": "8\r\n4 2\r\n8 8\r\n4 1\r\n7 7\r\n1 3\r\n1 1\r\n3 1\r\n5 2\r\n", "output": "-1\r\n"}, {"input": "8\r\n4 2\r\n1 1\r\n1 5\r\n6 8\r\n5 7\r\n8 8\r\n6 2\r\n8 8\r\n", "output": "-1\r\n"}, {"input": "8\r\n4 6\r\n3 8\r\n7 4\r\n5 0\r\n8 7\r\n8 8\r\n8 8\r\n3 5\r\n", "output": "-1\r\n"}, {"input": "9\r\n5 3\r\n1 8\r\n2 2\r\n2 7\r\n5 6\r\n1 5\r\n2 0\r\n1 6\r\n3 9\r\n", "output": "-1\r\n"}, {"input": "9\r\n4 7\r\n2 0\r\n7 3\r\n9 5\r\n8 8\r\n6 5\r\n6 8\r\n5 3\r\n8 7\r\n", "output": "-1\r\n"}, {"input": "9\r\n3 8\r\n7 7\r\n8 8\r\n7 3\r\n9 6\r\n6 8\r\n4 1\r\n7 0\r\n7 0\r\n", "output": "-1\r\n"}, {"input": "10\r\n1 8\r\n5 8\r\n7 9\r\n9 4\r\n3 4\r\n5 3\r\n1 3\r\n2 4\r\n6 6\r\n5 7\r\n", "output": "-1\r\n"}, {"input": "10\r\n9 9\r\n10 4\r\n1 9\r\n4 8\r\n9 6\r\n9 6\r\n1 7\r\n1 7\r\n10 0\r\n4 1\r\n", "output": "-1\r\n"}, {"input": "10\r\n3 10\r\n8 5\r\n4 1\r\n8 4\r\n8 8\r\n9 1\r\n6 0\r\n10 6\r\n7 7\r\n6 0\r\n", "output": "-1\r\n"}, {"input": "1\r\n1000000 0\r\n", "output": "0\r\n1000000 \r\n"}, {"input": "2\r\n1000000 0\r\n999999 0\r\n", "output": "0\r\n1000000 999999 \r\n"}, {"input": "2\r\n1000000 0\r\n999998 1\r\n", "output": "1\r\n1000000 999999 \r\n"}, {"input": "2\r\n1000000 1000000\r\n1000000 1000000\r\n", "output": "2000000\r\n2000000 2000000 \r\n"}]
false
stdio
null
true
733/C
733
C
PyPy 3
TESTS
3
170
409,600
90300229
import math,sys,bisect,heapq from collections import defaultdict,Counter,deque from itertools import groupby,accumulate #sys.setrecursionlimit(200000000) input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__ ilele = lambda: map(int,input().split()) alele = lambda: list(map(int, input().split())) def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] #MOD = 1000000000 + 7 def Y(c): print(["NO","YES"][c]) def y(c): print(["no","yes"][c]) def Yy(c): print(["No","Yes"][c]) Ans = [] N=int(input()) A = alele() def fun(a,b,c): B = A[a:b+1] #print(a,b) while len(B) > 1: m= max(B) for i in range(len(B)): if B[i] == m: if i ==0: if B[i+1] < B[i]: Ans.append((i+c,'R')) B[i] += B[i+1] del B[i+1] break elif i == len(B)-1: if B[i-1] < B[i]: Ans.append((i+c,'L')) B[i] += B[i-1] del B[i-1] break else: if B[i+1] < B[i]: Ans.append((i+c,'R')) B[i] += B[i+1] del B[i+1] break elif B[i-1] < B[i]: Ans.append((i+c,'L')) B[i] += B[i-1] del B[i-1] break K = int(input()) B = alele() i = 0 k = 0 f = 0 temp = 0 c=1 while k < len(B): x= B[k] tot = 0 i = temp while i<len(A): tot += A[i] if tot == x: break if tot > x: f=1 break i+=1 if f==1: break if x == 1 and A[i] ==1: temp = i+1 elif x== 1 and A[i] != 1: f=1;break elif len(set(A[temp:i+1])) == 1: f=1;break else: fun(temp,i,c) c+=1 temp = i+1 k+=1 if f==1: Y(0) else: Y(1) for i,j in Ans: print(i,j)
138
92
409,600
21951471
__author__ = 'Think' def f(interval, place): if len(interval)==1: return [] m=-1 n=len(interval) for i in range(n): if interval[i]>m: m=interval[i] index=i shifted=False if index==0 and n>1: if interval[1]==m: shifted=True index=1 while index<n: if interval[index]!=m: break index+=1 index-=1 if index==n-1: return [0] if not shifted: #print(place, "HI") return [(place+i, "L") for i in range(index, 0, -1)]+[(place, "R")]*(n-index-1) else: place+=index #print(place, index, "SHIFTING RULES") return [(place, "R")]*(n-index-1)+[(place-i, "L") for i in range(index)] n=int(input()) a=[int(i) for i in input().split()] k=int(input()) b=[int(i) for i in input().split()] place=1 point=0 printer=[] broken=False for i in b: initial=point if point>=n: broken=True break total=a[point] point+=1 while total<i: if point==n: broken=True break total+=a[point] point+=1 if broken: break if total>i: broken=True break thingy=f(a[initial:point], place) #print(thingy, point, initial, place) place+=1 if len(thingy)==1: if thingy[0]==0: broken=True break printer.extend(thingy) #print(printer) if broken or (point != n): print("NO") else: print("YES") for i in printer: print(i[0], i[1])
Codeforces Round 378 (Div. 2)
CF
2,016
1
256
Epidemic in Monstropolis
There was an epidemic in Monstropolis and all monsters became sick. To recover, all monsters lined up in queue for an appointment to the only doctor in the city. Soon, monsters became hungry and began to eat each other. One monster can eat other monster if its weight is strictly greater than the weight of the monster being eaten, and they stand in the queue next to each other. Monsters eat each other instantly. There are no monsters which are being eaten at the same moment. After the monster A eats the monster B, the weight of the monster A increases by the weight of the eaten monster B. In result of such eating the length of the queue decreases by one, all monsters after the eaten one step forward so that there is no empty places in the queue again. A monster can eat several monsters one after another. Initially there were n monsters in the queue, the i-th of which had weight ai. For example, if weights are [1, 2, 2, 2, 1, 2] (in order of queue, monsters are numbered from 1 to 6 from left to right) then some of the options are: 1. the first monster can't eat the second monster because a1 = 1 is not greater than a2 = 2; 2. the second monster can't eat the third monster because a2 = 2 is not greater than a3 = 2; 3. the second monster can't eat the fifth monster because they are not neighbors; 4. the second monster can eat the first monster, the queue will be transformed to [3, 2, 2, 1, 2]. After some time, someone said a good joke and all monsters recovered. At that moment there were k (k ≤ n) monsters in the queue, the j-th of which had weight bj. Both sequences (a and b) contain the weights of the monsters in the order from the first to the last. You are required to provide one of the possible orders of eating monsters which led to the current queue, or to determine that this could not happen. Assume that the doctor didn't make any appointments while monsters were eating each other.
The first line contains single integer n (1 ≤ n ≤ 500) — the number of monsters in the initial queue. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial weights of the monsters. The third line contains single integer k (1 ≤ k ≤ n) — the number of monsters in the queue after the joke. The fourth line contains k integers b1, b2, ..., bk (1 ≤ bj ≤ 5·108) — the weights of the monsters after the joke. Monsters are listed in the order from the beginning of the queue to the end.
In case if no actions could lead to the final queue, print "NO" (without quotes) in the only line. Otherwise print "YES" (without quotes) in the first line. In the next n - k lines print actions in the chronological order. In each line print x — the index number of the monster in the current queue which eats and, separated by space, the symbol 'L' if the monster which stays the x-th in the queue eats the monster in front of him, or 'R' if the monster which stays the x-th in the queue eats the monster behind him. After each eating the queue is enumerated again. When one monster eats another the queue decreases. If there are several answers, print any of them.
null
In the first example, initially there were n = 6 monsters, their weights are [1, 2, 2, 2, 1, 2] (in order of queue from the first monster to the last monster). The final queue should be [5, 5]. The following sequence of eatings leads to the final queue: - the second monster eats the monster to the left (i.e. the first monster), queue becomes [3, 2, 2, 1, 2]; - the first monster (note, it was the second on the previous step) eats the monster to the right (i.e. the second monster), queue becomes [5, 2, 1, 2]; - the fourth monster eats the mosnter to the left (i.e. the third monster), queue becomes [5, 2, 3]; - the finally, the third monster eats the monster to the left (i.e. the second monster), queue becomes [5, 5]. Note that for each step the output contains numbers of the monsters in their current order in the queue.
[{"input": "6\n1 2 2 2 1 2\n2\n5 5", "output": "YES\n2 L\n1 R\n4 L\n3 L"}, {"input": "5\n1 2 3 4 5\n1\n15", "output": "YES\n5 L\n4 L\n3 L\n2 L"}, {"input": "5\n1 1 1 3 3\n3\n2 1 6", "output": "NO"}]
1,800
["constructive algorithms", "dp", "greedy", "two pointers"]
139
[{"input": "6\r\n1 2 2 2 1 2\r\n2\r\n5 5\r\n", "output": "YES\r\n2 L\r\n1 R\r\n4 L\r\n3 L\r\n"}, {"input": "5\r\n1 2 3 4 5\r\n1\r\n15\r\n", "output": "YES\r\n5 L\r\n4 L\r\n3 L\r\n2 L\r\n"}, {"input": "5\r\n1 1 1 3 3\r\n3\r\n2 1 6\r\n", "output": "NO"}, {"input": "5\r\n1 1 1 1 2\r\n3\r\n1 1 4\r\n", "output": "YES\r\n5 L\r\n4 L\r\n"}, {"input": "5\r\n1 1 1 1 1\r\n4\r\n1 1 2 1\r\n", "output": "NO"}, {"input": "6\r\n2 1 2 2 1 2\r\n2\r\n5 5\r\n", "output": "YES\r\n3 L\r\n2 L\r\n4 L\r\n3 L\r\n"}, {"input": "8\r\n2 5 3 1 4 2 3 4\r\n3\r\n10 6 8\r\n", "output": "NO"}, {"input": "1\r\n959139\r\n1\r\n470888\r\n", "output": "NO"}, {"input": "3\r\n2 2 1\r\n1\r\n5\r\n", "output": "YES\r\n2 R\r\n2 L\r\n"}, {"input": "3\r\n1 2 2\r\n1\r\n5\r\n", "output": "YES\r\n2 L\r\n1 R\r\n"}, {"input": "5\r\n1 2 3 4 5\r\n1\r\n10\r\n", "output": "NO"}, {"input": "5\r\n325539 329221 106895 882089 718673\r\n5\r\n699009 489855 430685 939232 282330\r\n", "output": "NO"}, {"input": "10\r\n30518 196518 274071 359971 550121 204862 843967 173607 619138 690754\r\n3\r\n171337 183499 549873\r\n", "output": "NO"}, {"input": "3\r\n2 1 1\r\n1\r\n3\r\n", "output": "NO"}, {"input": "4\r\n2 2 2 1\r\n3\r\n2 2 2\r\n", "output": "NO"}, {"input": "3\r\n1 2 3\r\n1\r\n3\r\n", "output": "NO"}, {"input": "2\r\n1 2\r\n2\r\n3 1\r\n", "output": "NO"}, {"input": "5\r\n3 3 2 2 1\r\n2\r\n8 3\r\n", "output": "YES\r\n2 R\r\n2 L\r\n2 R\r\n"}, {"input": "3\r\n3 2 5\r\n1\r\n10\r\n", "output": "YES\r\n3 L\r\n2 L\r\n"}, {"input": "3\r\n1 5 1\r\n1\r\n6\r\n", "output": "NO"}, {"input": "5\r\n1 2 3 4 5\r\n3\r\n1 2 3\r\n", "output": "NO"}, {"input": "3\r\n5 2 3\r\n1\r\n10\r\n", "output": "YES\r\n1 R\r\n1 R\r\n"}, {"input": "3\r\n2 1 3\r\n1\r\n6\r\n", "output": "YES\r\n3 L\r\n2 L\r\n"}, {"input": "3\r\n3 2 1\r\n1\r\n6\r\n", "output": "YES\r\n1 R\r\n1 R\r\n"}, {"input": "2\r\n5 5\r\n1\r\n5\r\n", "output": "NO"}, {"input": "3\r\n1 2 3\r\n2\r\n1 2\r\n", "output": "NO"}, {"input": "4\r\n1 2 3 4\r\n3\r\n1 2 3\r\n", "output": "NO"}, {"input": "4\r\n4 3 2 1\r\n3\r\n3 2 1\r\n", "output": "NO"}, {"input": "2\r\n5 3\r\n1\r\n5\r\n", "output": "NO"}, {"input": "5\r\n1 1 1 1 1\r\n4\r\n1 1 1 1\r\n", "output": "NO"}, {"input": "3\r\n3 3 2\r\n1\r\n8\r\n", "output": "YES\r\n2 R\r\n2 L\r\n"}, {"input": "8\r\n2 2 1 2 2 1 2 4\r\n2\r\n9 8\r\n", "output": "NO"}, {"input": "4\r\n3 2 1 4\r\n3\r\n3 2 1\r\n", "output": "NO"}, {"input": "5\r\n3 3 2 3 1\r\n2\r\n11 1\r\n", "output": "YES\r\n4 L\r\n3 L\r\n2 L\r\n"}, {"input": "3\r\n2 1 3\r\n1\r\n3\r\n", "output": "NO"}, {"input": "4\r\n2 3 3 2\r\n2\r\n5 3\r\n", "output": "NO"}, {"input": "16\r\n2 2 2 1 2 2 2 1 1 2 2 2 1 2 2 2\r\n4\r\n7 7 7 7\r\n", "output": "YES\r\n3 R\r\n3 L\r\n2 L\r\n4 R\r\n4 L\r\n3 L\r\n4 L\r\n3 R\r\n3 R\r\n5 L\r\n4 R\r\n4 R\r\n"}, {"input": "2\r\n1 1\r\n1\r\n1\r\n", "output": "NO"}, {"input": "3\r\n1 2 1\r\n2\r\n3 2\r\n", "output": "NO"}, {"input": "3\r\n2 3 5\r\n1\r\n10\r\n", "output": "YES\r\n3 L\r\n2 L\r\n"}, {"input": "5\r\n1 2 3 4 5\r\n2\r\n3 7\r\n", "output": "NO"}, {"input": "4\r\n1 2 3 4\r\n2\r\n1 2\r\n", "output": "NO"}, {"input": "8\r\n1 2 2 2 1 2 1 1\r\n2\r\n5 5\r\n", "output": "NO"}, {"input": "3\r\n5 5 4\r\n1\r\n14\r\n", "output": "YES\r\n2 R\r\n2 L\r\n"}, {"input": "22\r\n3 2 3 3 3 1 1 2 1 2 1 1 1 2 2 3 1 2 3 3 3 3\r\n5\r\n5 16 5 5 15\r\n", "output": "YES\r\n1 R\r\n4 R\r\n4 R\r\n4 R\r\n4 R\r\n4 R\r\n4 L\r\n3 L\r\n6 L\r\n5 L\r\n4 L\r\n5 L\r\n7 L\r\n6 L\r\n5 R\r\n5 R\r\n5 R\r\n"}, {"input": "4\r\n2 2 1 2\r\n1\r\n7\r\n", "output": "YES\r\n4 L\r\n3 L\r\n2 L\r\n"}, {"input": "7\r\n2 2 2 1 2 2 2\r\n1\r\n13\r\n", "output": "YES\r\n5 L\r\n4 L\r\n3 L\r\n2 L\r\n1 R\r\n1 R\r\n"}, {"input": "2\r\n1 2\r\n1\r\n1\r\n", "output": "NO"}, {"input": "14\r\n5 5 5 5 4 4 4 3 3 3 4 4 4 4\r\n3\r\n32 21 4\r\n", "output": "YES\r\n4 R\r\n4 R\r\n4 R\r\n4 L\r\n3 L\r\n2 L\r\n5 L\r\n4 L\r\n3 L\r\n2 R\r\n2 R\r\n"}, {"input": "5\r\n2 2 1 2 2\r\n1\r\n9\r\n", "output": "YES\r\n4 L\r\n3 L\r\n2 L\r\n1 R\r\n"}, {"input": "1\r\n2\r\n1\r\n2\r\n", "output": "YES\r\n"}, {"input": "23\r\n3 2 1 3 3 3 1 1 2 1 2 1 1 1 2 2 3 1 2 3 3 3 3\r\n5\r\n6 16 5 5 15\r\n", "output": "YES\r\n1 R\r\n1 R\r\n4 R\r\n4 R\r\n4 R\r\n4 R\r\n4 R\r\n4 L\r\n3 L\r\n6 L\r\n5 L\r\n4 L\r\n5 L\r\n7 L\r\n6 L\r\n5 R\r\n5 R\r\n5 R\r\n"}]
false
stdio
import sys def read_file(path): with open(path, 'r') as f: return f.read().splitlines() input_path, output_path, submission_path = sys.argv[1], sys.argv[2], sys.argv[3] input_lines = read_file(input_path) n = int(input_lines[0]) a = list(map(int, input_lines[1].split())) k = int(input_lines[2]) b = list(map(int, input_lines[3].split())) ref_lines = read_file(output_path) submission_lines = read_file(submission_path) if not submission_lines: print(0) sys.exit(0) submission_first_line = submission_lines[0].strip() ref_first_line = ref_lines[0].strip() if submission_first_line != ref_first_line: print(0) sys.exit(0) if submission_first_line == "NO": print(1) sys.exit(0) # Now, handle YES case expected_steps = n - k if len(submission_lines) - 1 != expected_steps: print(0) sys.exit(0) current_queue = a.copy() valid = True for step in submission_lines[1:]: step = step.strip() if not step: valid = False break parts = step.split() if len(parts) != 2: valid = False break try: x = int(parts[0]) direction = parts[1] except: valid = False break m = len(current_queue) if x < 1 or x > m: valid = False break if direction not in ['L', 'R']: valid = False break if direction == 'L': if x == 1: valid = False break eater_pos = x - 1 eaten_pos = x - 2 else: if x == m: valid = False break eater_pos = x - 1 eaten_pos = x eater = current_queue[eater_pos] eaten = current_queue[eaten_pos] if eater <= eaten: valid = False break # Merge the monsters new_eater = eater + eaten if direction == 'L': current_queue = current_queue[:eaten_pos] + [new_eater] + current_queue[eater_pos+1:] else: current_queue = current_queue[:eater_pos] + [new_eater] + current_queue[eaten_pos+1:] # Check final queue if valid and current_queue == b: print(1) else: print(0)
true
215/A
215
A
Python 3
TESTS
23
92
0
218424934
n = int(input()) pedalStars = list(map(int, input().split())) m = int(input()) rearWheelStars = list(map(int, input().split())) ratio = 0 counter = 0 maxRatio = int(rearWheelStars[0] / pedalStars[0]) for a in range(n): for b in range(m): if rearWheelStars[b] % pedalStars[a] == 0: ratio = int(rearWheelStars[b] / pedalStars[a]) else: continue if ratio == maxRatio: counter = counter + 1 elif ratio > maxRatio: maxRatio = ratio counter = 1 print(counter)
57
92
0
141025688
n=int(input()) a=list(map(int,input().split())) m=int(input()) b=list(map(int,input().split())) count=[] for i in a: for j in b: if(j%i==0): count.append(j//i) ma=max(count) print(count.count(ma))
Codeforces Round 132 (Div. 2)
CF
2,012
2
256
Bicycle Chain
Vasya's bicycle chain drive consists of two parts: n stars are attached to the pedal axle, m stars are attached to the rear wheel axle. The chain helps to rotate the rear wheel by transmitting the pedal rotation. We know that the i-th star on the pedal axle has ai (0 < a1 < a2 < ... < an) teeth, and the j-th star on the rear wheel axle has bj (0 < b1 < b2 < ... < bm) teeth. Any pair (i, j) (1 ≤ i ≤ n; 1 ≤ j ≤ m) is called a gear and sets the indexes of stars to which the chain is currently attached. Gear (i, j) has a gear ratio, equal to the value $$\frac{b_{j}}{a_{j}}$$. Since Vasya likes integers, he wants to find such gears (i, j), that their ratios are integers. On the other hand, Vasya likes fast driving, so among all "integer" gears (i, j) he wants to choose a gear with the maximum ratio. Help him to find the number of such gears. In the problem, fraction $$\frac{b_{j}}{a_{j}}$$ denotes division in real numbers, that is, no rounding is performed.
The first input line contains integer n (1 ≤ n ≤ 50) — the number of stars on the bicycle's pedal axle. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 104) in the order of strict increasing. The third input line contains integer m (1 ≤ m ≤ 50) — the number of stars on the rear wheel axle. The fourth line contains m integers b1, b2, ..., bm (1 ≤ bi ≤ 104) in the order of strict increasing. It is guaranteed that there exists at least one gear (i, j), that its gear ratio is an integer. The numbers on the lines are separated by spaces.
Print the number of "integer" gears with the maximum ratio among all "integer" gears.
null
In the first sample the maximum "integer" gear ratio equals 3. There are two gears that have such gear ratio. For one of them a1 = 4, b1 = 12, and for the other a2 = 5, b3 = 15.
[{"input": "2\n4 5\n3\n12 13 15", "output": "2"}, {"input": "4\n1 2 3 4\n5\n10 11 12 13 14", "output": "1"}]
900
["brute force", "implementation"]
57
[{"input": "2\r\n4 5\r\n3\r\n12 13 15\r\n", "output": "2\r\n"}, {"input": "4\r\n1 2 3 4\r\n5\r\n10 11 12 13 14\r\n", "output": "1\r\n"}, {"input": "1\r\n1\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "2\r\n1 2\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n1\r\n2\r\n1 2\r\n", "output": "1\r\n"}, {"input": "4\r\n3 7 11 13\r\n4\r\n51 119 187 221\r\n", "output": "4\r\n"}, {"input": "4\r\n2 3 4 5\r\n3\r\n1 2 3\r\n", "output": "2\r\n"}, {"input": "10\r\n6 12 13 20 48 53 74 92 96 97\r\n10\r\n1 21 32 36 47 54 69 75 95 97\r\n", "output": "1\r\n"}, {"input": "10\r\n5 9 10 14 15 17 19 22 24 26\r\n10\r\n2 11 17 19 21 22 24 25 27 28\r\n", "output": "1\r\n"}, {"input": "10\r\n24 53 56 126 354 432 442 740 795 856\r\n10\r\n273 438 494 619 689 711 894 947 954 958\r\n", "output": "1\r\n"}, {"input": "10\r\n3 4 6 7 8 10 14 16 19 20\r\n10\r\n3 4 5 7 8 10 15 16 18 20\r\n", "output": "1\r\n"}, {"input": "10\r\n1 6 8 14 15 17 25 27 34 39\r\n10\r\n1 8 16 17 19 22 32 39 44 50\r\n", "output": "1\r\n"}, {"input": "10\r\n5 21 22 23 25 32 35 36 38 39\r\n10\r\n3 7 8 9 18 21 23 24 36 38\r\n", "output": "4\r\n"}, {"input": "10\r\n17 239 443 467 661 1069 1823 2333 3767 4201\r\n20\r\n51 83 97 457 593 717 997 1329 1401 1459 1471 1983 2371 2539 3207 3251 3329 5469 6637 6999\r\n", "output": "8\r\n"}, {"input": "20\r\n179 359 401 467 521 601 919 941 1103 1279 1709 1913 1949 2003 2099 2143 2179 2213 2399 4673\r\n20\r\n151 181 191 251 421 967 1109 1181 1249 1447 1471 1553 1619 2327 2551 2791 3049 3727 6071 7813\r\n", "output": "3\r\n"}, {"input": "20\r\n79 113 151 709 809 983 1291 1399 1409 1429 2377 2659 2671 2897 3217 3511 3557 3797 3823 4363\r\n10\r\n19 101 659 797 1027 1963 2129 2971 3299 9217\r\n", "output": "3\r\n"}, {"input": "30\r\n19 47 109 179 307 331 389 401 461 509 547 569 617 853 883 1249 1361 1381 1511 1723 1741 1783 2459 2531 2621 3533 3821 4091 5557 6217\r\n20\r\n401 443 563 941 967 997 1535 1567 1655 1747 1787 1945 1999 2251 2305 2543 2735 4415 6245 7555\r\n", "output": "8\r\n"}, {"input": "5\r\n33 78 146 3055 4268\r\n5\r\n2211 2584 5226 9402 9782\r\n", "output": "3\r\n"}, {"input": "5\r\n35 48 52 86 8001\r\n10\r\n332 3430 3554 4704 4860 5096 6215 7583 8228 8428\r\n", "output": "4\r\n"}, {"input": "10\r\n97 184 207 228 269 2084 4450 6396 7214 9457\r\n16\r\n338 1179 1284 1545 1570 2444 3167 3395 3397 5550 6440 7245 7804 7980 9415 9959\r\n", "output": "5\r\n"}, {"input": "30\r\n25 30 41 57 58 62 70 72 76 79 84 85 88 91 98 101 104 109 119 129 136 139 148 151 926 1372 3093 3936 5423 7350\r\n25\r\n1600 1920 2624 3648 3712 3968 4480 4608 4864 5056 5376 5440 5632 5824 6272 6464 6656 6934 6976 7616 8256 8704 8896 9472 9664\r\n", "output": "24\r\n"}, {"input": "5\r\n33 78 146 3055 4268\r\n5\r\n2211 2584 5226 9402 9782\r\n", "output": "3\r\n"}, {"input": "5\r\n35 48 52 86 8001\r\n10\r\n332 3430 3554 4704 4860 5096 6215 7583 8228 8428\r\n", "output": "4\r\n"}, {"input": "10\r\n97 184 207 228 269 2084 4450 6396 7214 9457\r\n16\r\n338 1179 1284 1545 1570 2444 3167 3395 3397 5550 6440 7245 7804 7980 9415 9959\r\n", "output": "5\r\n"}, {"input": "30\r\n25 30 41 57 58 62 70 72 76 79 84 85 88 91 98 101 104 109 119 129 136 139 148 151 926 1372 3093 3936 5423 7350\r\n25\r\n1600 1920 2624 3648 3712 3968 4480 4608 4864 5056 5376 5440 5632 5824 6272 6464 6656 6934 6976 7616 8256 8704 8896 9472 9664\r\n", "output": "24\r\n"}, {"input": "50\r\n51 67 75 186 194 355 512 561 720 876 1077 1221 1503 1820 2153 2385 2568 2608 2937 2969 3271 3311 3481 4081 4093 4171 4255 4256 4829 5020 5192 5636 5817 6156 6712 6717 7153 7436 7608 7612 7866 7988 8264 8293 8867 9311 9879 9882 9889 9908\r\n1\r\n5394\r\n", "output": "1\r\n"}, {"input": "3\r\n3 4 5\r\n3\r\n6 20 25\r\n", "output": "2\r\n"}, {"input": "4\r\n2 3 5 8\r\n4\r\n2 6 8 10\r\n", "output": "1\r\n"}, {"input": "4\r\n3 5 7 11\r\n4\r\n3 5 7 22\r\n", "output": "1\r\n"}, {"input": "2\r\n2 3\r\n3\r\n20 30 50\r\n", "output": "1\r\n"}, {"input": "3\r\n1 2 3\r\n4\r\n2 4 6 49\r\n", "output": "1\r\n"}, {"input": "2\r\n4 5\r\n3\r\n12 15 20\r\n", "output": "1\r\n"}, {"input": "3\r\n2 5 7\r\n3\r\n4 5 7\r\n", "output": "1\r\n"}, {"input": "3\r\n3 5 8\r\n3\r\n6 8 10\r\n", "output": "2\r\n"}, {"input": "2\r\n2 3\r\n4\r\n4 6 9 33\r\n", "output": "1\r\n"}, {"input": "2\r\n2 3\r\n4\r\n4 6 21 40\r\n", "output": "1\r\n"}, {"input": "3\r\n4 9 10\r\n3\r\n8 9 10\r\n", "output": "1\r\n"}, {"input": "5\r\n1 5 6 9 51\r\n5\r\n5 12 18 27 10000\r\n", "output": "1\r\n"}, {"input": "13\r\n1 2 3 4 5 6 7 8 9 10 11 12 13\r\n1\r\n14\r\n", "output": "1\r\n"}]
false
stdio
null
true
618/C
618
C
PyPy 3
TESTS
14
717
28,262,400
116624162
import sys from math import gcd input=sys.stdin.readline n=int(input()) p=[list(map(int,input().split())) for i in range(n)] dist=[(p[i][0]-p[0][0])**2+(p[i][1]-p[0][1])**2 for i in range(n)] def get_slope(x,y): g=gcd(x,y) x//=g;y//=g if x<0 or (x==0 and y<0): x,y=-x,-y return (x,y) s={} for i in range(1,n): x,y=p[i] slope=get_slope(x,y) if slope not in s or dist[i]<dist[s[slope]]: s[slope]=i arr=sorted(s.values(),key=lambda x:dist[x]) print(1,arr[0]+1,arr[1]+1)
98
608
33,075,200
86141874
import sys input=sys.stdin.readline def fu(a,b): if a[0]==b[0]: return "a" return (a[1]-b[1])/(a[0]-b[0]) n=int(input()) a=[] for i in range(n): a.append(list(map(int,input().split()))+[i+1]) a.sort() for i in range(n-2): if fu(a[i+2],a[i+1])!=fu(a[i+1],a[i]): print(a[i][2],a[i+1][2],a[i+2][2]) break
Wunder Fund Round 2016 (Div. 1 + Div. 2 combined)
CF
2,016
2
256
Constellation
Cat Noku has obtained a map of the night sky. On this map, he found a constellation with n stars numbered from 1 to n. For each i, the i-th star is located at coordinates (xi, yi). No two stars are located at the same position. In the evening Noku is going to take a look at the night sky. He would like to find three distinct stars and form a triangle. The triangle must have positive area. In addition, all other stars must lie strictly outside of this triangle. He is having trouble finding the answer and would like your help. Your job is to find the indices of three stars that would form a triangle that satisfies all the conditions. It is guaranteed that there is no line such that all stars lie on that line. It can be proven that if the previous condition is satisfied, there exists a solution to this problem.
The first line of the input contains a single integer n (3 ≤ n ≤ 100 000). Each of the next n lines contains two integers xi and yi ( - 109 ≤ xi, yi ≤ 109). It is guaranteed that no two stars lie at the same point, and there does not exist a line such that all stars lie on that line.
Print three distinct integers on a single line — the indices of the three points that form a triangle that satisfies the conditions stated in the problem. If there are multiple possible answers, you may print any of them.
null
In the first sample, we can print the three indices in any order. In the second sample, we have the following picture. Note that the triangle formed by starts 1, 4 and 3 doesn't satisfy the conditions stated in the problem, as point 5 is not strictly outside of this triangle (it lies on it's border).
[{"input": "3\n0 1\n1 0\n1 1", "output": "1 2 3"}, {"input": "5\n0 0\n0 2\n2 0\n2 2\n1 1", "output": "1 3 5"}]
1,600
["geometry", "implementation"]
98
[{"input": "3\r\n0 1\r\n1 0\r\n1 1\r\n", "output": "1 2 3\r\n"}, {"input": "5\r\n0 0\r\n0 2\r\n2 0\r\n2 2\r\n1 1\r\n", "output": "1 3 5\r\n"}, {"input": "3\r\n819934317 939682125\r\n487662889 8614219\r\n-557136619 382982369\r\n", "output": "1 3 2\r\n"}, {"input": "10\r\n25280705 121178189\r\n219147240 -570920213\r\n-829849659 923854124\r\n18428128 -781819137\r\n-876779400 528386329\r\n-780997681 387686853\r\n-101900553 749998368\r\n58277314 355353788\r\n732128908 336416193\r\n840698381 600685123\r\n", "output": "1 3 2\r\n"}, {"input": "10\r\n404775998 670757742\r\n30131431 723806809\r\n25599613 633170449\r\n13303280 387243789\r\n-33017802 -539177851\r\n1425218 149682549\r\n-47620079 -831223391\r\n-25996011 -398742031\r\n38471092 890600029\r\n-3745401 46270169\r\n", "output": "1 2 3\r\n"}, {"input": "10\r\n13303280 387243789\r\n30131431 723806809\r\n404775998 670757742\r\n-25996011 -398742031\r\n25599613 633170449\r\n38471092 890600029\r\n-33017802 -539177851\r\n-47620079 -831223391\r\n1425218 149682549\r\n-3745401 46270169\r\n", "output": "1 3 5\r\n"}, {"input": "10\r\n999999999 1\r\n999999998 1\r\n999999997 1\r\n1000000000 1\r\n999999996 1\r\n999999995 1\r\n999999994 1\r\n999999992 1\r\n999999993 1\r\n0 0\r\n", "output": "1 2 10\r\n"}, {"input": "4\r\n0 1\r\n0 2\r\n0 3\r\n7 7\r\n", "output": "1 4 2\r\n"}, {"input": "3\r\n0 0\r\n999999999 1\r\n999999998 1\r\n", "output": "1 2 3\r\n"}, {"input": "10\r\n0 999999999\r\n0 1000000000\r\n-1 1000000000\r\n1 1000000000\r\n-2 1000000000\r\n2 1000000000\r\n-3 1000000000\r\n3 1000000000\r\n-4 1000000000\r\n4 1000000000\r\n", "output": "1 2 3\r\n"}, {"input": "12\r\n1000000000 0\r\n1000000000 1\r\n1000000000 2\r\n1000000000 3\r\n1000000000 4\r\n1000000000 5\r\n1000000000 6\r\n1000000000 7\r\n1000000000 8\r\n1000000000 9\r\n1000000000 10\r\n999999999 5\r\n", "output": "1 2 12\r\n"}, {"input": "12\r\n1000000000 0\r\n1000000000 1\r\n1000000000 2\r\n1000000000 3\r\n1000000000 4\r\n1000000000 5\r\n1000000000 6\r\n1000000000 7\r\n1000000000 8\r\n1000000000 9\r\n1000000000 10\r\n999999999 -1\r\n", "output": "1 2 12\r\n"}, {"input": "12\r\n1000000000 0\r\n1000000000 1\r\n1000000000 2\r\n1000000000 3\r\n1000000000 4\r\n1000000000 5\r\n1000000000 6\r\n1000000000 7\r\n1000000000 8\r\n1000000000 9\r\n1000000000 10\r\n999999999 10\r\n", "output": "1 2 12\r\n"}, {"input": "12\r\n1000000000 0\r\n1000000000 1\r\n1000000000 2\r\n1000000000 3\r\n1000000000 4\r\n1000000000 5\r\n1000000000 6\r\n1000000000 7\r\n1000000000 8\r\n1000000000 9\r\n1000000000 10\r\n999999999 1\r\n", "output": "1 2 12\r\n"}, {"input": "11\r\n-1000000000 1\r\n-1000000000 2\r\n-1000000000 3\r\n-1000000000 4\r\n-1000000000 5\r\n-1000000000 6\r\n-1000000000 7\r\n-1000000000 8\r\n-1000000000 9\r\n-1000000000 10\r\n-999999999 5\r\n", "output": "1 11 2\r\n"}, {"input": "11\r\n-1000000000 1\r\n-1000000000 2\r\n-1000000000 3\r\n-1000000000 4\r\n-1000000000 5\r\n-1000000000 6\r\n-1000000000 7\r\n-1000000000 8\r\n-1000000000 9\r\n-1000000000 10\r\n-999999999 7\r\n", "output": "1 11 2\r\n"}, {"input": "11\r\n-1000000000 1\r\n-1000000000 2\r\n-1000000000 3\r\n-1000000000 4\r\n-1000000000 5\r\n-1000000000 6\r\n-1000000000 7\r\n-1000000000 8\r\n-1000000000 9\r\n-1000000000 10\r\n-999999999 8\r\n", "output": "1 11 2\r\n"}, {"input": "11\r\n-1000000000 1\r\n-1000000000 2\r\n-1000000000 3\r\n-1000000000 4\r\n-1000000000 5\r\n-1000000000 6\r\n-1000000000 7\r\n-1000000000 8\r\n-1000000000 9\r\n-1000000000 10\r\n-999999999 10\r\n", "output": "1 11 2\r\n"}, {"input": "11\r\n-1000000000 -1\r\n-1000000000 -2\r\n-1000000000 -3\r\n-1000000000 -4\r\n-1000000000 -5\r\n-1000000000 -6\r\n-1000000000 -7\r\n-1000000000 -8\r\n-1000000000 -9\r\n-1000000000 -10\r\n-999999999 -5\r\n", "output": "1 2 11\r\n"}, {"input": "11\r\n-1000000000 -1\r\n-1000000000 -2\r\n-1000000000 -3\r\n-1000000000 -4\r\n-1000000000 -5\r\n-1000000000 -6\r\n-1000000000 -7\r\n-1000000000 -8\r\n-1000000000 -9\r\n-1000000000 -10\r\n-999999999 -1\r\n", "output": "1 2 11\r\n"}, {"input": "11\r\n-1000000000 -1\r\n-1000000000 -2\r\n-1000000000 -3\r\n-1000000000 -4\r\n-1000000000 -5\r\n-1000000000 -6\r\n-1000000000 -7\r\n-1000000000 -8\r\n-1000000000 -9\r\n-1000000000 -10\r\n-999999999 -2\r\n", "output": "1 2 11\r\n"}, {"input": "11\r\n-1000000000 -1\r\n-1000000000 -2\r\n-1000000000 -3\r\n-1000000000 -4\r\n-1000000000 -5\r\n-1000000000 -6\r\n-1000000000 -7\r\n-1000000000 -8\r\n-1000000000 -9\r\n-1000000000 -10\r\n-999999999 -4\r\n", "output": "1 2 11\r\n"}, {"input": "11\r\n-1000000000 -1\r\n-1000000000 -2\r\n-1000000000 -3\r\n-1000000000 -4\r\n-1000000000 -5\r\n-1000000000 -6\r\n-1000000000 -7\r\n-1000000000 -8\r\n-1000000000 -9\r\n-1000000000 -10\r\n-999999999 -8\r\n", "output": "1 2 11\r\n"}, {"input": "10\r\n2 1000000000\r\n8 1000000000\r\n9 1000000000\r\n3 1000000000\r\n4 1000000000\r\n5 1000000000\r\n6 1000000000\r\n1 1000000000\r\n7 1000000000\r\n0 0\r\n", "output": "1 10 4\r\n"}, {"input": "10\r\n1000000000 1\r\n999999999 1\r\n999999998 1\r\n999999997 1\r\n999999996 1\r\n999999995 1\r\n999999994 1\r\n999999993 1\r\n999999992 1\r\n0 0\r\n", "output": "1 2 10\r\n"}, {"input": "10\r\n999999999 1\r\n999999998 1\r\n999999997 1\r\n999999996 1\r\n999999995 1\r\n999999994 1\r\n999999993 1\r\n1000000000 1\r\n999999992 1\r\n0 0\r\n", "output": "1 2 10\r\n"}, {"input": "4\r\n0 0\r\n1 0\r\n2 0\r\n1 100\r\n", "output": "1 2 4\r\n"}, {"input": "4\r\n0 0\r\n3 0\r\n2 0\r\n1 1\r\n", "output": "3 2 4\r\n"}, {"input": "4\r\n0 0\r\n1 1\r\n2 2\r\n3 4\r\n", "output": "1 2 4\r\n"}, {"input": "4\r\n0 0\r\n0 1\r\n0 2\r\n1 1\r\n", "output": "1 4 2\r\n"}, {"input": "4\r\n0 0\r\n2 0\r\n1 0\r\n1 1\r\n", "output": "3 2 4\r\n"}, {"input": "4\r\n0 0\r\n1 1\r\n2 2\r\n5 -1\r\n", "output": "1 4 2\r\n"}, {"input": "5\r\n0 1\r\n0 2\r\n0 3\r\n0 4\r\n10 10\r\n", "output": "1 5 2\r\n"}, {"input": "4\r\n0 1\r\n0 2\r\n0 3\r\n1 1\r\n", "output": "1 4 2\r\n"}, {"input": "4\r\n0 0\r\n1 0\r\n2 0\r\n2 1\r\n", "output": "1 2 4\r\n"}, {"input": "4\r\n0 0\r\n-1 -1\r\n1 1\r\n100 0\r\n", "output": "1 2 4\r\n"}, {"input": "4\r\n0 0\r\n2 0\r\n1 1\r\n1 0\r\n", "output": "4 2 3\r\n"}, {"input": "4\r\n0 0\r\n1 0\r\n2 0\r\n3 1\r\n", "output": "1 2 4\r\n"}, {"input": "3\r\n0 0\r\n12345691 12336918\r\n19349510 19335760\r\n", "output": "1 3 2\r\n"}, {"input": "21\r\n0 19\r\n0 0\r\n0 8\r\n0 2\r\n0 18\r\n0 17\r\n0 1\r\n0 5\r\n0 16\r\n0 11\r\n0 10\r\n0 13\r\n0 12\r\n0 14\r\n0 6\r\n0 7\r\n0 3\r\n0 15\r\n0 4\r\n0 9\r\n1 1\r\n", "output": "7 2 21\r\n"}, {"input": "10\r\n0 0\r\n1 -100\r\n1 100\r\n1 50\r\n1 0\r\n1 -50\r\n1 10\r\n1 -10\r\n1 5\r\n1 -5\r\n", "output": "1 2 6\r\n"}, {"input": "3\r\n1 2\r\n2 1\r\n2 3\r\n", "output": "1 2 3\r\n"}, {"input": "3\r\n-1000000000 -1000000000\r\n1000000000 -1000000000\r\n-1000000000 1000000000\r\n", "output": "1 2 3\r\n"}, {"input": "10\r\n0 0\r\n1 0\r\n2 0\r\n3 0\r\n4 0\r\n5 0\r\n6 0\r\n7 0\r\n8 1\r\n9 0\r\n", "output": "1 2 9\r\n"}, {"input": "4\r\n1 1\r\n2 2\r\n3 3\r\n10 11\r\n", "output": "1 2 4\r\n"}, {"input": "4\r\n0 0\r\n0 2\r\n0 1\r\n3 3\r\n", "output": "1 4 3\r\n"}, {"input": "4\r\n0 0\r\n2 2\r\n1 1\r\n2 0\r\n", "output": "1 4 3\r\n"}, {"input": "4\r\n0 1\r\n0 0\r\n0 5\r\n1 1\r\n", "output": "1 2 4\r\n"}, {"input": "4\r\n1 0\r\n2 0\r\n3 0\r\n-7 -7\r\n", "output": "1 4 2\r\n"}, {"input": "4\r\n0 0\r\n0 2\r\n0 1\r\n10 10\r\n", "output": "1 4 3\r\n"}, {"input": "4\r\n-50000000 204926\r\n0 0\r\n8192 50000000\r\n16384 100000000\r\n", "output": "1 2 3\r\n"}, {"input": "4\r\n65537 536870912\r\n0 536805376\r\n1 536870912\r\n-8191 0\r\n", "output": "1 3 2\r\n"}, {"input": "4\r\n0 0\r\n131072 0\r\n131072 131072\r\n200000 0\r\n", "output": "1 2 3\r\n"}, {"input": "3\r\n-536870912 10\r\n536870912 11\r\n-536870912 6\r\n", "output": "1 3 2\r\n"}, {"input": "4\r\n3 7\r\n2 4\r\n1 2\r\n0 0\r\n", "output": "1 3 2\r\n"}, {"input": "4\r\n0 0\r\n0 1\r\n0 2\r\n3 3\r\n", "output": "1 4 2\r\n"}]
false
stdio
import sys def main(input_path, output_path, submission_path): # Read input points with open(input_path, 'r') as f: n = int(f.readline().strip()) points = [] for _ in range(n): x, y = map(int, f.readline().strip().split()) points.append((x, y)) # Read submission's output with open(submission_path, 'r') as f: line = f.readline().strip() parts = line.split() if len(parts) != 3: print(0) return try: a, b, c = map(int, parts) except: print(0) return # Check validity of indices if not (1 <= a <= n and 1 <= b <= n and 1 <= c <= n): print(0) return a_idx = a - 1 b_idx = b - 1 c_idx = c - 1 if len({a_idx, b_idx, c_idx}) != 3: print(0) return # Check colinear A = points[a_idx] B = points[b_idx] C = points[c_idx] cross = (B[0] - A[0]) * (C[1] - A[1]) - (B[1] - A[1]) * (C[0] - A[0]) if cross == 0: print(0) return # Precompute original cross products for each edge cross_ab = cross cross_bc = (C[0] - B[0]) * (A[1] - B[1]) - (C[1] - B[1]) * (A[0] - B[0]) cross_ca = (A[0] - C[0]) * (B[1] - C[1]) - (A[1] - C[1]) * (B[0] - C[0]) # Check all other points for i in range(n): if i in {a_idx, b_idx, c_idx}: continue P = points[i] # Check edge AB cp_ab = (B[0] - A[0]) * (P[1] - A[1]) - (B[1] - A[1]) * (P[0] - A[0]) if cross_ab * cp_ab < 0: continue if cp_ab == 0: if (min(A[0], B[0]) <= P[0] <= max(A[0], B[0])) and (min(A[1], B[1]) <= P[1] <= max(A[1], B[1])): print(0) return # Check edge BC cp_bc = (C[0] - B[0]) * (P[1] - B[1]) - (C[1] - B[1]) * (P[0] - B[0]) if cross_bc * cp_bc < 0: continue if cp_bc == 0: if (min(B[0], C[0]) <= P[0] <= max(B[0], C[0])) and (min(B[1], C[1]) <= P[1] <= max(B[1], C[1])): print(0) return # Check edge CA cp_ca = (A[0] - C[0]) * (P[1] - C[1]) - (A[1] - C[1]) * (P[0] - C[0]) if cross_ca * cp_ca < 0: continue if cp_ca == 0: if (min(C[0], A[0]) <= P[0] <= max(C[0], A[0])) and (min(C[1], A[1]) <= P[1] <= max(C[1], A[1])): print(0) return # If passed all edges, check if inside if (cp_ab * cross_ab >= 0) and (cp_bc * cross_bc >= 0) and (cp_ca * cross_ca >= 0): print(0) return print(1) if __name__ == "__main__": input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] main(input_path, output_path, submission_path)
true
215/A
215
A
Python 3
TESTS
46
92
0
153627658
n = int(input()) a = list(map(int, input().split())) m = int(input()) b = list(map(int, input().split())) sum = 0 count = 1 for i in a: for j in b: if(j%i == 0): if(j//i > sum): sum = j//i elif(sum == j//i): count += 1 print(count)
57
92
0
143152333
n=int(input("")) a=input("").split(" ") m=int(input("")) b=input("").split(" ") list1=[] list2=[] for i in a: list1.append(int(i)) for j in b: list2.append(int(j)) a1=sorted(list1) b1=sorted(list2) list3=[] for i in b1: for j in a1: if i%j==0: list3.append(i/j) print(list3.count(max(list3)))
Codeforces Round 132 (Div. 2)
CF
2,012
2
256
Bicycle Chain
Vasya's bicycle chain drive consists of two parts: n stars are attached to the pedal axle, m stars are attached to the rear wheel axle. The chain helps to rotate the rear wheel by transmitting the pedal rotation. We know that the i-th star on the pedal axle has ai (0 < a1 < a2 < ... < an) teeth, and the j-th star on the rear wheel axle has bj (0 < b1 < b2 < ... < bm) teeth. Any pair (i, j) (1 ≤ i ≤ n; 1 ≤ j ≤ m) is called a gear and sets the indexes of stars to which the chain is currently attached. Gear (i, j) has a gear ratio, equal to the value $$\frac{b_{j}}{a_{j}}$$. Since Vasya likes integers, he wants to find such gears (i, j), that their ratios are integers. On the other hand, Vasya likes fast driving, so among all "integer" gears (i, j) he wants to choose a gear with the maximum ratio. Help him to find the number of such gears. In the problem, fraction $$\frac{b_{j}}{a_{j}}$$ denotes division in real numbers, that is, no rounding is performed.
The first input line contains integer n (1 ≤ n ≤ 50) — the number of stars on the bicycle's pedal axle. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 104) in the order of strict increasing. The third input line contains integer m (1 ≤ m ≤ 50) — the number of stars on the rear wheel axle. The fourth line contains m integers b1, b2, ..., bm (1 ≤ bi ≤ 104) in the order of strict increasing. It is guaranteed that there exists at least one gear (i, j), that its gear ratio is an integer. The numbers on the lines are separated by spaces.
Print the number of "integer" gears with the maximum ratio among all "integer" gears.
null
In the first sample the maximum "integer" gear ratio equals 3. There are two gears that have such gear ratio. For one of them a1 = 4, b1 = 12, and for the other a2 = 5, b3 = 15.
[{"input": "2\n4 5\n3\n12 13 15", "output": "2"}, {"input": "4\n1 2 3 4\n5\n10 11 12 13 14", "output": "1"}]
900
["brute force", "implementation"]
57
[{"input": "2\r\n4 5\r\n3\r\n12 13 15\r\n", "output": "2\r\n"}, {"input": "4\r\n1 2 3 4\r\n5\r\n10 11 12 13 14\r\n", "output": "1\r\n"}, {"input": "1\r\n1\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "2\r\n1 2\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n1\r\n2\r\n1 2\r\n", "output": "1\r\n"}, {"input": "4\r\n3 7 11 13\r\n4\r\n51 119 187 221\r\n", "output": "4\r\n"}, {"input": "4\r\n2 3 4 5\r\n3\r\n1 2 3\r\n", "output": "2\r\n"}, {"input": "10\r\n6 12 13 20 48 53 74 92 96 97\r\n10\r\n1 21 32 36 47 54 69 75 95 97\r\n", "output": "1\r\n"}, {"input": "10\r\n5 9 10 14 15 17 19 22 24 26\r\n10\r\n2 11 17 19 21 22 24 25 27 28\r\n", "output": "1\r\n"}, {"input": "10\r\n24 53 56 126 354 432 442 740 795 856\r\n10\r\n273 438 494 619 689 711 894 947 954 958\r\n", "output": "1\r\n"}, {"input": "10\r\n3 4 6 7 8 10 14 16 19 20\r\n10\r\n3 4 5 7 8 10 15 16 18 20\r\n", "output": "1\r\n"}, {"input": "10\r\n1 6 8 14 15 17 25 27 34 39\r\n10\r\n1 8 16 17 19 22 32 39 44 50\r\n", "output": "1\r\n"}, {"input": "10\r\n5 21 22 23 25 32 35 36 38 39\r\n10\r\n3 7 8 9 18 21 23 24 36 38\r\n", "output": "4\r\n"}, {"input": "10\r\n17 239 443 467 661 1069 1823 2333 3767 4201\r\n20\r\n51 83 97 457 593 717 997 1329 1401 1459 1471 1983 2371 2539 3207 3251 3329 5469 6637 6999\r\n", "output": "8\r\n"}, {"input": "20\r\n179 359 401 467 521 601 919 941 1103 1279 1709 1913 1949 2003 2099 2143 2179 2213 2399 4673\r\n20\r\n151 181 191 251 421 967 1109 1181 1249 1447 1471 1553 1619 2327 2551 2791 3049 3727 6071 7813\r\n", "output": "3\r\n"}, {"input": "20\r\n79 113 151 709 809 983 1291 1399 1409 1429 2377 2659 2671 2897 3217 3511 3557 3797 3823 4363\r\n10\r\n19 101 659 797 1027 1963 2129 2971 3299 9217\r\n", "output": "3\r\n"}, {"input": "30\r\n19 47 109 179 307 331 389 401 461 509 547 569 617 853 883 1249 1361 1381 1511 1723 1741 1783 2459 2531 2621 3533 3821 4091 5557 6217\r\n20\r\n401 443 563 941 967 997 1535 1567 1655 1747 1787 1945 1999 2251 2305 2543 2735 4415 6245 7555\r\n", "output": "8\r\n"}, {"input": "5\r\n33 78 146 3055 4268\r\n5\r\n2211 2584 5226 9402 9782\r\n", "output": "3\r\n"}, {"input": "5\r\n35 48 52 86 8001\r\n10\r\n332 3430 3554 4704 4860 5096 6215 7583 8228 8428\r\n", "output": "4\r\n"}, {"input": "10\r\n97 184 207 228 269 2084 4450 6396 7214 9457\r\n16\r\n338 1179 1284 1545 1570 2444 3167 3395 3397 5550 6440 7245 7804 7980 9415 9959\r\n", "output": "5\r\n"}, {"input": "30\r\n25 30 41 57 58 62 70 72 76 79 84 85 88 91 98 101 104 109 119 129 136 139 148 151 926 1372 3093 3936 5423 7350\r\n25\r\n1600 1920 2624 3648 3712 3968 4480 4608 4864 5056 5376 5440 5632 5824 6272 6464 6656 6934 6976 7616 8256 8704 8896 9472 9664\r\n", "output": "24\r\n"}, {"input": "5\r\n33 78 146 3055 4268\r\n5\r\n2211 2584 5226 9402 9782\r\n", "output": "3\r\n"}, {"input": "5\r\n35 48 52 86 8001\r\n10\r\n332 3430 3554 4704 4860 5096 6215 7583 8228 8428\r\n", "output": "4\r\n"}, {"input": "10\r\n97 184 207 228 269 2084 4450 6396 7214 9457\r\n16\r\n338 1179 1284 1545 1570 2444 3167 3395 3397 5550 6440 7245 7804 7980 9415 9959\r\n", "output": "5\r\n"}, {"input": "30\r\n25 30 41 57 58 62 70 72 76 79 84 85 88 91 98 101 104 109 119 129 136 139 148 151 926 1372 3093 3936 5423 7350\r\n25\r\n1600 1920 2624 3648 3712 3968 4480 4608 4864 5056 5376 5440 5632 5824 6272 6464 6656 6934 6976 7616 8256 8704 8896 9472 9664\r\n", "output": "24\r\n"}, {"input": "50\r\n51 67 75 186 194 355 512 561 720 876 1077 1221 1503 1820 2153 2385 2568 2608 2937 2969 3271 3311 3481 4081 4093 4171 4255 4256 4829 5020 5192 5636 5817 6156 6712 6717 7153 7436 7608 7612 7866 7988 8264 8293 8867 9311 9879 9882 9889 9908\r\n1\r\n5394\r\n", "output": "1\r\n"}, {"input": "3\r\n3 4 5\r\n3\r\n6 20 25\r\n", "output": "2\r\n"}, {"input": "4\r\n2 3 5 8\r\n4\r\n2 6 8 10\r\n", "output": "1\r\n"}, {"input": "4\r\n3 5 7 11\r\n4\r\n3 5 7 22\r\n", "output": "1\r\n"}, {"input": "2\r\n2 3\r\n3\r\n20 30 50\r\n", "output": "1\r\n"}, {"input": "3\r\n1 2 3\r\n4\r\n2 4 6 49\r\n", "output": "1\r\n"}, {"input": "2\r\n4 5\r\n3\r\n12 15 20\r\n", "output": "1\r\n"}, {"input": "3\r\n2 5 7\r\n3\r\n4 5 7\r\n", "output": "1\r\n"}, {"input": "3\r\n3 5 8\r\n3\r\n6 8 10\r\n", "output": "2\r\n"}, {"input": "2\r\n2 3\r\n4\r\n4 6 9 33\r\n", "output": "1\r\n"}, {"input": "2\r\n2 3\r\n4\r\n4 6 21 40\r\n", "output": "1\r\n"}, {"input": "3\r\n4 9 10\r\n3\r\n8 9 10\r\n", "output": "1\r\n"}, {"input": "5\r\n1 5 6 9 51\r\n5\r\n5 12 18 27 10000\r\n", "output": "1\r\n"}, {"input": "13\r\n1 2 3 4 5 6 7 8 9 10 11 12 13\r\n1\r\n14\r\n", "output": "1\r\n"}]
false
stdio
null
true
54/A
54
A
Python 3
TESTS
21
218
307,200
70358976
import math n, k = list(map(int, input().split())) a = list(map(int, input().split())) c, *a = a if c == 0: print(n // k) else: res = 1 for i in range(1, len(a)): if a[i] - a[i - 1] >= k: res += math.ceil((a[i] - a[i - 1]) / k) if a[0] - 1 >= k: res += (a[0] - 1) // k if n - a[-1] >= k: res += (n - a[-1]) // k print(res)
65
92
0
166673850
# n, k = list(map(int, input().split(' '))) # holidays = [int(item) for item in input().split(' ')] # ans = set() # i = 0 # # for i in range(i + k, n + 1, k): # ans.add(i) # # for j in range(1, len(holidays)): # if holidays[j] <= n: # ans.add(holidays[j]) # # print(len(ans)) # # n, k = list(map(int, input().split(' '))) # holidays = [int(item) for item in input().split(' ')] # ans = [] # j = 1 # # for i in range(1, n + 1): # if i == holidays[j]: # ans.append(i) # k = i + k # if len(holidays) != 2: # j += 1 # elif i == k: # ans.append(i) # k = k * 2 # # print(len(ans)) def nextStep(cursor, holidays, k): # helper = cursor + k # j = 0 # if holidays[j] < helper: # j += 1 # return holidays[j] # return helper for i in range(cursor + 1, cursor + k): if i in holidays: return i return cursor + k n, k = list(map(int, input().split(' '))) holidays = [int(item) for item in input().split(' ')] del holidays[0] ans, cursor = 0, 0 while True: cursor = nextStep(cursor, holidays, k) if cursor <= n: ans += 1 else: break print(ans) ''' 5 2 1 3 1 2 3 4 5 ^ ^ ^ 20 5 3 8 12 18 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ^ ^ ^ ^ ^ '''
Codeforces Beta Round 50
CF
2,011
2
256
Presents
The Hedgehog likes to give presents to his friend, but no less he likes to receive them. Having received another present today, the Hedgehog suddenly understood that he has no place to put it as there was no room left on the special shelf in the cupboard. He will have to choose another shelf, but which one should he choose, how large should it be? In order to get to know this, the Hedgehog asks you to write him a program that will count the estimated number of presents that he will receive during the following N days. Besides, he is guided by the principle: - on each holiday day the Hedgehog will necessarily receive a present, - he receives presents at least every K days (i.e., if he received a present on the i-th day, he will receive the next present no later than on the i + K-th day). For the given N and K, as well as the list of holidays among the following N days count the minimal number of presents that could be given to the Hedgehog. The number of today's day is zero, and you should regard today's present as already given (i.e., you shouldn't count it in the answer).
The first line contains integers N and K (1 ≤ N ≤ 365, 1 ≤ K ≤ N). The second line contains a number C which represents the number of holidays (0 ≤ C ≤ N). Then in the same line follow C numbers ranging from 1 to N which are the numbers of holiday days. The numbers are given in the increasing order, without repeating numbers among them.
Print a single number — the minimal number of presents the Hedgehog will receive over the following N days.
null
null
[{"input": "5 2\n1 3", "output": "3"}, {"input": "10 1\n3 6 7 8", "output": "10"}]
1,300
["implementation"]
65
[{"input": "5 2\r\n1 3\r\n", "output": "3"}, {"input": "10 1\r\n3 6 7 8\r\n", "output": "10"}, {"input": "5 5\r\n1 3\r\n", "output": "1"}, {"input": "10 3\r\n3 3 6 9\r\n", "output": "3"}, {"input": "5 2\r\n0\r\n", "output": "2"}, {"input": "1 1\r\n0\r\n", "output": "1"}, {"input": "5 1\r\n0\r\n", "output": "5"}, {"input": "5 1\r\n1 2\r\n", "output": "5"}, {"input": "5 2\r\n0\r\n", "output": "2"}, {"input": "10 3\r\n2 4 8\r\n", "output": "4"}, {"input": "10 1\r\n0\r\n", "output": "10"}, {"input": "10 2\r\n1 5\r\n", "output": "5"}, {"input": "10 1\r\n0\r\n", "output": "10"}, {"input": "10 1\r\n0\r\n", "output": "10"}, {"input": "15 5\r\n0\r\n", "output": "3"}, {"input": "15 1\r\n1 3\r\n", "output": "15"}, {"input": "15 2\r\n1 10\r\n", "output": "7"}, {"input": "15 1\r\n0\r\n", "output": "15"}, {"input": "15 3\r\n1 11\r\n", "output": "5"}, {"input": "20 1\r\n3 7 9 20\r\n", "output": "20"}, {"input": "20 3\r\n1 11\r\n", "output": "7"}, {"input": "20 2\r\n6 6 9 10 15 19 20\r\n", "output": "12"}, {"input": "20 1\r\n0\r\n", "output": "20"}, {"input": "20 1\r\n1 13\r\n", "output": "20"}, {"input": "25 1\r\n9 2 6 8 10 14 15 17 18 23\r\n", "output": "25"}, {"input": "25 1\r\n0\r\n", "output": "25"}, {"input": "25 1\r\n4 8 10 13 24\r\n", "output": "25"}, {"input": "25 1\r\n1 14\r\n", "output": "25"}, {"input": "25 1\r\n0\r\n", "output": "25"}, {"input": "100 3\r\n0\r\n", "output": "33"}, {"input": "100 10\r\n0\r\n", "output": "10"}, {"input": "100 23\r\n22 2 9 18 22 23 30 44 50 55 58 61 70 71 73 76 79 82 85 88 94 95 99\r\n", "output": "22"}, {"input": "100 5\r\n10 2 17 21 34 52 58 60 64 68 95\r\n", "output": "24"}, {"input": "100 4\r\n2 29 63\r\n", "output": "26"}, {"input": "150 16\r\n9 19 31 47 53 57 96 105 108 120\r\n", "output": "13"}, {"input": "150 52\r\n5 11 37 60 67 86\r\n", "output": "6"}, {"input": "150 4\r\n7 21 54 106 108 109 119 123\r\n", "output": "40"}, {"input": "150 3\r\n0\r\n", "output": "50"}, {"input": "150 21\r\n21 22 26 30 36 39 52 59 62 66 68 78 86 92 96 103 108 113 118 119 125 139\r\n", "output": "22"}, {"input": "300 15\r\n14 3 38 52 57 142 157 175 201 209 238 258 288 294 299\r\n", "output": "26"}, {"input": "300 2\r\n14 29 94 122 123 158 160 164 191 200 202 208 246 272 286\r\n", "output": "153"}, {"input": "300 5\r\n16 22 38 72 78 108 116 140 147 160 189 209 214 227 252 294 300\r\n", "output": "66"}, {"input": "300 8\r\n4 27 76 155 260\r\n", "output": "40"}, {"input": "300 24\r\n20 18 76 80 81 85 103 110 112 129 145 151 172 180 184 201 205 241 257 268 276\r\n", "output": "24"}, {"input": "350 22\r\n11 38 111 115 176 194 204 207 231 274 307 348\r\n", "output": "21"}, {"input": "350 26\r\n10 13 16 81 99 144 191 223 258 316 329\r\n", "output": "18"}, {"input": "350 16\r\n12 31 76 103 116 191 201 241 256 260 291 306 336\r\n", "output": "24"}, {"input": "350 28\r\n5 23 104 135 305 331\r\n", "output": "14"}, {"input": "365 34\r\n6 80 94 208 256 325 349\r\n", "output": "14"}, {"input": "365 19\r\n7 47 114 139 210 226 266 279\r\n", "output": "22"}, {"input": "365 8\r\n32 1 13 22 25 33 72 80 86 96 117 132 145 146 156 176 177 179 188 198 203 218 225 235 253 256 267 279 286 294 303 333 363\r\n", "output": "61"}, {"input": "365 8\r\n55 3 12 26 28 36 45 47 59 61 65 82 90 103 109 114 117 121 123 126 134 142 144 146 151 154 168 175 189 193 195 197 199 210 212 214 230 232 241 248 254 267 271 291 304 306 308 311 315 317 318 334 335 346 354 365\r\n", "output": "74"}, {"input": "365 2\r\n2 96 241\r\n", "output": "183"}, {"input": "365 42\r\n10 8 66 77 148 161 183 231 301 340 350\r\n", "output": "14"}, {"input": "365 40\r\n30 1 14 21 31 32 36 56 59 68 96 119 131 137 166 179 181 202 235 248 272 294 309 315 322 327 334 341 347 362 365\r\n", "output": "30"}, {"input": "365 31\r\n19 13 18 27 33 46 58 86 114 178 187 198 228 233 240 255 277 332 348 351\r\n", "output": "22"}, {"input": "365 54\r\n21 28 42 56 65 66 67 76 81 85 89 123 132 136 153 195 215 249 294 296 300 355\r\n", "output": "22"}, {"input": "365 5\r\n5 10 31 121 235 322\r\n", "output": "74"}, {"input": "365 81\r\n2 1 75\r\n", "output": "5"}, {"input": "365 21\r\n4 1 17 344 345\r\n", "output": "19"}, {"input": "11 2\r\n5 3 6 7 9 10\r\n", "output": "7"}, {"input": "5 3\r\n2 2 4\r\n", "output": "2"}, {"input": "362 360\r\n0\r\n", "output": "1"}, {"input": "18 4\r\n4 1 9 10 18\r\n", "output": "6"}]
false
stdio
null
true
215/A
215
A
Python 3
TESTS
46
154
5,632,000
33642711
n=int(input()) l1=list(map(int,input().split())) m=int(input()) l2=list(map(int,input().split())) b=0 c=0 for i in range(n): for j in range(m): q,r=divmod(l2[j],l1[i]) if r==0 and q==b: c+=1 if r==0 and q>b: b=q print(c+1)
57
92
0
144033476
n=int(input()) arr1=list(map(int,input().split())) m=int(input()) arr2=list(map(int,input().split())) d=[] for i in range(n): for j in range(m): k=arr2[j]/arr1[i] d1=arr2[j]//arr1[i] if(k==d1): d.append(d1) d.sort(reverse=True) print(d.count(d[0]))
Codeforces Round 132 (Div. 2)
CF
2,012
2
256
Bicycle Chain
Vasya's bicycle chain drive consists of two parts: n stars are attached to the pedal axle, m stars are attached to the rear wheel axle. The chain helps to rotate the rear wheel by transmitting the pedal rotation. We know that the i-th star on the pedal axle has ai (0 < a1 < a2 < ... < an) teeth, and the j-th star on the rear wheel axle has bj (0 < b1 < b2 < ... < bm) teeth. Any pair (i, j) (1 ≤ i ≤ n; 1 ≤ j ≤ m) is called a gear and sets the indexes of stars to which the chain is currently attached. Gear (i, j) has a gear ratio, equal to the value $$\frac{b_{j}}{a_{j}}$$. Since Vasya likes integers, he wants to find such gears (i, j), that their ratios are integers. On the other hand, Vasya likes fast driving, so among all "integer" gears (i, j) he wants to choose a gear with the maximum ratio. Help him to find the number of such gears. In the problem, fraction $$\frac{b_{j}}{a_{j}}$$ denotes division in real numbers, that is, no rounding is performed.
The first input line contains integer n (1 ≤ n ≤ 50) — the number of stars on the bicycle's pedal axle. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 104) in the order of strict increasing. The third input line contains integer m (1 ≤ m ≤ 50) — the number of stars on the rear wheel axle. The fourth line contains m integers b1, b2, ..., bm (1 ≤ bi ≤ 104) in the order of strict increasing. It is guaranteed that there exists at least one gear (i, j), that its gear ratio is an integer. The numbers on the lines are separated by spaces.
Print the number of "integer" gears with the maximum ratio among all "integer" gears.
null
In the first sample the maximum "integer" gear ratio equals 3. There are two gears that have such gear ratio. For one of them a1 = 4, b1 = 12, and for the other a2 = 5, b3 = 15.
[{"input": "2\n4 5\n3\n12 13 15", "output": "2"}, {"input": "4\n1 2 3 4\n5\n10 11 12 13 14", "output": "1"}]
900
["brute force", "implementation"]
57
[{"input": "2\r\n4 5\r\n3\r\n12 13 15\r\n", "output": "2\r\n"}, {"input": "4\r\n1 2 3 4\r\n5\r\n10 11 12 13 14\r\n", "output": "1\r\n"}, {"input": "1\r\n1\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "2\r\n1 2\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n1\r\n2\r\n1 2\r\n", "output": "1\r\n"}, {"input": "4\r\n3 7 11 13\r\n4\r\n51 119 187 221\r\n", "output": "4\r\n"}, {"input": "4\r\n2 3 4 5\r\n3\r\n1 2 3\r\n", "output": "2\r\n"}, {"input": "10\r\n6 12 13 20 48 53 74 92 96 97\r\n10\r\n1 21 32 36 47 54 69 75 95 97\r\n", "output": "1\r\n"}, {"input": "10\r\n5 9 10 14 15 17 19 22 24 26\r\n10\r\n2 11 17 19 21 22 24 25 27 28\r\n", "output": "1\r\n"}, {"input": "10\r\n24 53 56 126 354 432 442 740 795 856\r\n10\r\n273 438 494 619 689 711 894 947 954 958\r\n", "output": "1\r\n"}, {"input": "10\r\n3 4 6 7 8 10 14 16 19 20\r\n10\r\n3 4 5 7 8 10 15 16 18 20\r\n", "output": "1\r\n"}, {"input": "10\r\n1 6 8 14 15 17 25 27 34 39\r\n10\r\n1 8 16 17 19 22 32 39 44 50\r\n", "output": "1\r\n"}, {"input": "10\r\n5 21 22 23 25 32 35 36 38 39\r\n10\r\n3 7 8 9 18 21 23 24 36 38\r\n", "output": "4\r\n"}, {"input": "10\r\n17 239 443 467 661 1069 1823 2333 3767 4201\r\n20\r\n51 83 97 457 593 717 997 1329 1401 1459 1471 1983 2371 2539 3207 3251 3329 5469 6637 6999\r\n", "output": "8\r\n"}, {"input": "20\r\n179 359 401 467 521 601 919 941 1103 1279 1709 1913 1949 2003 2099 2143 2179 2213 2399 4673\r\n20\r\n151 181 191 251 421 967 1109 1181 1249 1447 1471 1553 1619 2327 2551 2791 3049 3727 6071 7813\r\n", "output": "3\r\n"}, {"input": "20\r\n79 113 151 709 809 983 1291 1399 1409 1429 2377 2659 2671 2897 3217 3511 3557 3797 3823 4363\r\n10\r\n19 101 659 797 1027 1963 2129 2971 3299 9217\r\n", "output": "3\r\n"}, {"input": "30\r\n19 47 109 179 307 331 389 401 461 509 547 569 617 853 883 1249 1361 1381 1511 1723 1741 1783 2459 2531 2621 3533 3821 4091 5557 6217\r\n20\r\n401 443 563 941 967 997 1535 1567 1655 1747 1787 1945 1999 2251 2305 2543 2735 4415 6245 7555\r\n", "output": "8\r\n"}, {"input": "5\r\n33 78 146 3055 4268\r\n5\r\n2211 2584 5226 9402 9782\r\n", "output": "3\r\n"}, {"input": "5\r\n35 48 52 86 8001\r\n10\r\n332 3430 3554 4704 4860 5096 6215 7583 8228 8428\r\n", "output": "4\r\n"}, {"input": "10\r\n97 184 207 228 269 2084 4450 6396 7214 9457\r\n16\r\n338 1179 1284 1545 1570 2444 3167 3395 3397 5550 6440 7245 7804 7980 9415 9959\r\n", "output": "5\r\n"}, {"input": "30\r\n25 30 41 57 58 62 70 72 76 79 84 85 88 91 98 101 104 109 119 129 136 139 148 151 926 1372 3093 3936 5423 7350\r\n25\r\n1600 1920 2624 3648 3712 3968 4480 4608 4864 5056 5376 5440 5632 5824 6272 6464 6656 6934 6976 7616 8256 8704 8896 9472 9664\r\n", "output": "24\r\n"}, {"input": "5\r\n33 78 146 3055 4268\r\n5\r\n2211 2584 5226 9402 9782\r\n", "output": "3\r\n"}, {"input": "5\r\n35 48 52 86 8001\r\n10\r\n332 3430 3554 4704 4860 5096 6215 7583 8228 8428\r\n", "output": "4\r\n"}, {"input": "10\r\n97 184 207 228 269 2084 4450 6396 7214 9457\r\n16\r\n338 1179 1284 1545 1570 2444 3167 3395 3397 5550 6440 7245 7804 7980 9415 9959\r\n", "output": "5\r\n"}, {"input": "30\r\n25 30 41 57 58 62 70 72 76 79 84 85 88 91 98 101 104 109 119 129 136 139 148 151 926 1372 3093 3936 5423 7350\r\n25\r\n1600 1920 2624 3648 3712 3968 4480 4608 4864 5056 5376 5440 5632 5824 6272 6464 6656 6934 6976 7616 8256 8704 8896 9472 9664\r\n", "output": "24\r\n"}, {"input": "50\r\n51 67 75 186 194 355 512 561 720 876 1077 1221 1503 1820 2153 2385 2568 2608 2937 2969 3271 3311 3481 4081 4093 4171 4255 4256 4829 5020 5192 5636 5817 6156 6712 6717 7153 7436 7608 7612 7866 7988 8264 8293 8867 9311 9879 9882 9889 9908\r\n1\r\n5394\r\n", "output": "1\r\n"}, {"input": "3\r\n3 4 5\r\n3\r\n6 20 25\r\n", "output": "2\r\n"}, {"input": "4\r\n2 3 5 8\r\n4\r\n2 6 8 10\r\n", "output": "1\r\n"}, {"input": "4\r\n3 5 7 11\r\n4\r\n3 5 7 22\r\n", "output": "1\r\n"}, {"input": "2\r\n2 3\r\n3\r\n20 30 50\r\n", "output": "1\r\n"}, {"input": "3\r\n1 2 3\r\n4\r\n2 4 6 49\r\n", "output": "1\r\n"}, {"input": "2\r\n4 5\r\n3\r\n12 15 20\r\n", "output": "1\r\n"}, {"input": "3\r\n2 5 7\r\n3\r\n4 5 7\r\n", "output": "1\r\n"}, {"input": "3\r\n3 5 8\r\n3\r\n6 8 10\r\n", "output": "2\r\n"}, {"input": "2\r\n2 3\r\n4\r\n4 6 9 33\r\n", "output": "1\r\n"}, {"input": "2\r\n2 3\r\n4\r\n4 6 21 40\r\n", "output": "1\r\n"}, {"input": "3\r\n4 9 10\r\n3\r\n8 9 10\r\n", "output": "1\r\n"}, {"input": "5\r\n1 5 6 9 51\r\n5\r\n5 12 18 27 10000\r\n", "output": "1\r\n"}, {"input": "13\r\n1 2 3 4 5 6 7 8 9 10 11 12 13\r\n1\r\n14\r\n", "output": "1\r\n"}]
false
stdio
null
true
382/C
382
C
PyPy 3
TESTS
8
140
0
94224327
n = int(input()) temparr = input() temparr = temparr.split() arr = [] for i in temparr: arr.append(int(i)) arr = sorted(arr) if n == 1: print(-1) elif n == 2: diff = arr[1] - arr[0] if diff & 1: print(2) ans = str(arr[0] - diff) + " " + str(arr[1] + diff) print(ans) elif diff == 0: print(1) print(arr[0]) else: print(3) half = diff // 2 midd = arr[0] + half ans = str(arr[0] - diff) + " "+ str(midd) +" "+ str(arr[1] + diff) print(ans) else: dicts = {} for i in range(1, n): diff = arr[i] - arr[i - 1] if diff not in dicts: dicts[diff] = 1 else: dicts[diff] += 1 if len(dicts) >= 3: print("0") elif len(dicts) >=2 and 0 in dicts: print("0") elif len(dicts) == 1: print("2") first = arr[0] - diff last = arr[-1] + diff strsans = str(first) + " " + str(last) print(strsans) else: flag = 0 firstkey = 0 firstval = 0 seckey = 0 secval = 0 for key ,value in dicts.items(): if flag == 0 : firstval = value firstkey = key flag = 1 else: secval = value seckey = key if firstval >=2 and secval >= 2: print("0") else: minskey = min(firstkey, seckey) maxskey = max(firstkey, seckey) if minskey * 2 != maxskey: print(0) else: ans = 0 for i in range(1, n): if arr[i] - arr[i -1] == maxskey: ans = arr[i - 1] + minskey break print(1) print(ans)
59
233
10,854,400
77363823
import sys; n = int(input()); arr = list(map(int,input().split())); if(n==1): print(-1); sys.exit(); arr.sort(); diff = []; req = []; flag2 = True; cnt=0; for i in range(1,n): diff.append(arr[i]-arr[i-1]); d = list(set(diff)); cnt = len(d); if(cnt>2): print(0); sys.exit(); if(cnt==1): req.append(arr[0]-diff[0]); req.append(arr[n-1]+diff[0]); if(d[0]%2==0 and n==2): req.append(arr[0]+d[0]//2); if(cnt==2): d1 = d[0]; d2 = d[1]; d1c = diff.count(d1); d2c = diff.count(d2); if(d1==2*d2 and d1c==1 and d2c==n-2): index = diff.index(d1); req.append(arr[index]+d2); if(d2==2*d1 and d2c==1 and d1c==n-2): index = diff.index(d2); req.append(arr[index]+d1); req = list(set(req)); print(len(req)); req.sort(); if(len(req)>0): print(*req);
Codeforces Round 224 (Div. 2)
CF
2,014
1
256
Arithmetic Progression
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a1, a2, ..., an of length n, that the following condition fulfills: a2 - a1 = a3 - a2 = a4 - a3 = ... = ai + 1 - ai = ... = an - an - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled.
The first line contains integer n (1 ≤ n ≤ 105) — the number of cards. The next line contains the sequence of integers — the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 108.
If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 108 or even be negative (see test samples).
null
null
[{"input": "3\n4 1 7", "output": "2\n-2 10"}, {"input": "1\n10", "output": "-1"}, {"input": "4\n1 3 5 9", "output": "1\n7"}, {"input": "4\n4 3 4 5", "output": "0"}, {"input": "2\n2 4", "output": "3\n0 3 6"}]
1,700
["implementation", "sortings"]
59
[{"input": "3\r\n4 1 7\r\n", "output": "2\r\n-2 10\r\n"}, {"input": "1\r\n10\r\n", "output": "-1\r\n"}, {"input": "4\r\n1 3 5 9\r\n", "output": "1\r\n7\r\n"}, {"input": "4\r\n4 3 4 5\r\n", "output": "0\r\n"}, {"input": "2\r\n2 4\r\n", "output": "3\r\n0 3 6\r\n"}, {"input": "4\r\n1 3 4 5\r\n", "output": "1\r\n2\r\n"}, {"input": "2\r\n3 3\r\n", "output": "1\r\n3\r\n"}, {"input": "2\r\n13 2\r\n", "output": "2\r\n-9 24\r\n"}, {"input": "5\r\n2 2 2 2 2\r\n", "output": "1\r\n2\r\n"}, {"input": "6\r\n11 1 7 9 5 13\r\n", "output": "1\r\n3\r\n"}, {"input": "2\r\n100000000 1\r\n", "output": "2\r\n-99999998 199999999\r\n"}, {"input": "5\r\n2 3 1 4 6\r\n", "output": "1\r\n5\r\n"}, {"input": "5\r\n1 2 2 3 4\r\n", "output": "0\r\n"}, {"input": "3\r\n1 4 2\r\n", "output": "1\r\n3\r\n"}, {"input": "3\r\n8 8 8\r\n", "output": "1\r\n8\r\n"}, {"input": "5\r\n2 2 2 2 3\r\n", "output": "0\r\n"}, {"input": "1\r\n100000000\r\n", "output": "-1\r\n"}, {"input": "20\r\n27 6 3 18 54 33 9 15 39 12 57 48 21 51 60 30 24 36 42 45\r\n", "output": "2\r\n0 63\r\n"}, {"input": "40\r\n100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000\r\n", "output": "1\r\n100000000\r\n"}, {"input": "49\r\n81787 163451 104059 89211 96635 133755 148603 141179 159739 122619 123 144891 70651 11259 63227 3835 44667 37243 100347 26107 137467 18683 156027 59515 22395 40955 111483 52091 7547 85499 107771 178299 115195 152315 74363 126331 33531 130043 14971 48379 167163 182011 170875 78075 174587 55803 66939 29819 118907\r\n", "output": "1\r\n92923\r\n"}, {"input": "9\r\n1 2 3 3 4 4 5 5 6\r\n", "output": "0\r\n"}, {"input": "7\r\n1 1 2 3 4 5 6\r\n", "output": "0\r\n"}, {"input": "2\r\n4 1\r\n", "output": "2\r\n-2 7\r\n"}, {"input": "2\r\n2 100000000\r\n", "output": "3\r\n-99999996 50000001 199999998\r\n"}, {"input": "8\r\n1 2 3 4 11 12 13 14\r\n", "output": "0\r\n"}, {"input": "7\r\n5 40 45 50 55 60 65\r\n", "output": "0\r\n"}, {"input": "1\r\n1\r\n", "output": "-1\r\n"}, {"input": "2\r\n1 1\r\n", "output": "1\r\n1\r\n"}, {"input": "2\r\n100000000 2\r\n", "output": "3\r\n-99999996 50000001 199999998\r\n"}, {"input": "3\r\n2 2 3\r\n", "output": "0\r\n"}, {"input": "5\r\n1 3 5 9 13\r\n", "output": "0\r\n"}, {"input": "5\r\n1 2 4 8 16\r\n", "output": "0\r\n"}, {"input": "3\r\n2 2 5\r\n", "output": "0\r\n"}, {"input": "5\r\n1 2 3 4 8\r\n", "output": "0\r\n"}, {"input": "3\r\n1 3 4\r\n", "output": "1\r\n2\r\n"}, {"input": "5\r\n1 2 4 6 7\r\n", "output": "0\r\n"}, {"input": "4\r\n1 5 9 11\r\n", "output": "0\r\n"}, {"input": "4\r\n3 4 5 9\r\n", "output": "0\r\n"}, {"input": "4\r\n1 5 6 8\r\n", "output": "0\r\n"}, {"input": "4\r\n2 6 8 12\r\n", "output": "0\r\n"}, {"input": "5\r\n1 2 3 5 7\r\n", "output": "0\r\n"}, {"input": "6\r\n1 2 3 4 6 8\r\n", "output": "0\r\n"}]
false
stdio
null
true
215/A
215
A
Python 3
TESTS
46
124
0
139329501
n = int(input()) list1 = list(map(int,input().split())) m = int(input()) list2 = list(map(int,input().split())) max_ratio = 0 count = 1 for i in range(len(list1)): for j in range(len(list2)): if list2[j]%list1[i]==0: ratio = list2[j]//list1[i] if ratio == max_ratio: count+=1 if ratio>max_ratio: max_ratio = ratio print(count)
57
92
0
145180533
n1=int(input()) arr1=list(map(int,input().split())) n2=int(input()) arr2=list(map(int,input().split())) l=[] for i in arr2: for j in arr1: if i%j==0: l.append(i//j) l.sort() x=l[-1] c=0 for i in l: if i==x: c=c+1 print(c)
Codeforces Round 132 (Div. 2)
CF
2,012
2
256
Bicycle Chain
Vasya's bicycle chain drive consists of two parts: n stars are attached to the pedal axle, m stars are attached to the rear wheel axle. The chain helps to rotate the rear wheel by transmitting the pedal rotation. We know that the i-th star on the pedal axle has ai (0 < a1 < a2 < ... < an) teeth, and the j-th star on the rear wheel axle has bj (0 < b1 < b2 < ... < bm) teeth. Any pair (i, j) (1 ≤ i ≤ n; 1 ≤ j ≤ m) is called a gear and sets the indexes of stars to which the chain is currently attached. Gear (i, j) has a gear ratio, equal to the value $$\frac{b_{j}}{a_{j}}$$. Since Vasya likes integers, he wants to find such gears (i, j), that their ratios are integers. On the other hand, Vasya likes fast driving, so among all "integer" gears (i, j) he wants to choose a gear with the maximum ratio. Help him to find the number of such gears. In the problem, fraction $$\frac{b_{j}}{a_{j}}$$ denotes division in real numbers, that is, no rounding is performed.
The first input line contains integer n (1 ≤ n ≤ 50) — the number of stars on the bicycle's pedal axle. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 104) in the order of strict increasing. The third input line contains integer m (1 ≤ m ≤ 50) — the number of stars on the rear wheel axle. The fourth line contains m integers b1, b2, ..., bm (1 ≤ bi ≤ 104) in the order of strict increasing. It is guaranteed that there exists at least one gear (i, j), that its gear ratio is an integer. The numbers on the lines are separated by spaces.
Print the number of "integer" gears with the maximum ratio among all "integer" gears.
null
In the first sample the maximum "integer" gear ratio equals 3. There are two gears that have such gear ratio. For one of them a1 = 4, b1 = 12, and for the other a2 = 5, b3 = 15.
[{"input": "2\n4 5\n3\n12 13 15", "output": "2"}, {"input": "4\n1 2 3 4\n5\n10 11 12 13 14", "output": "1"}]
900
["brute force", "implementation"]
57
[{"input": "2\r\n4 5\r\n3\r\n12 13 15\r\n", "output": "2\r\n"}, {"input": "4\r\n1 2 3 4\r\n5\r\n10 11 12 13 14\r\n", "output": "1\r\n"}, {"input": "1\r\n1\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "2\r\n1 2\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n1\r\n2\r\n1 2\r\n", "output": "1\r\n"}, {"input": "4\r\n3 7 11 13\r\n4\r\n51 119 187 221\r\n", "output": "4\r\n"}, {"input": "4\r\n2 3 4 5\r\n3\r\n1 2 3\r\n", "output": "2\r\n"}, {"input": "10\r\n6 12 13 20 48 53 74 92 96 97\r\n10\r\n1 21 32 36 47 54 69 75 95 97\r\n", "output": "1\r\n"}, {"input": "10\r\n5 9 10 14 15 17 19 22 24 26\r\n10\r\n2 11 17 19 21 22 24 25 27 28\r\n", "output": "1\r\n"}, {"input": "10\r\n24 53 56 126 354 432 442 740 795 856\r\n10\r\n273 438 494 619 689 711 894 947 954 958\r\n", "output": "1\r\n"}, {"input": "10\r\n3 4 6 7 8 10 14 16 19 20\r\n10\r\n3 4 5 7 8 10 15 16 18 20\r\n", "output": "1\r\n"}, {"input": "10\r\n1 6 8 14 15 17 25 27 34 39\r\n10\r\n1 8 16 17 19 22 32 39 44 50\r\n", "output": "1\r\n"}, {"input": "10\r\n5 21 22 23 25 32 35 36 38 39\r\n10\r\n3 7 8 9 18 21 23 24 36 38\r\n", "output": "4\r\n"}, {"input": "10\r\n17 239 443 467 661 1069 1823 2333 3767 4201\r\n20\r\n51 83 97 457 593 717 997 1329 1401 1459 1471 1983 2371 2539 3207 3251 3329 5469 6637 6999\r\n", "output": "8\r\n"}, {"input": "20\r\n179 359 401 467 521 601 919 941 1103 1279 1709 1913 1949 2003 2099 2143 2179 2213 2399 4673\r\n20\r\n151 181 191 251 421 967 1109 1181 1249 1447 1471 1553 1619 2327 2551 2791 3049 3727 6071 7813\r\n", "output": "3\r\n"}, {"input": "20\r\n79 113 151 709 809 983 1291 1399 1409 1429 2377 2659 2671 2897 3217 3511 3557 3797 3823 4363\r\n10\r\n19 101 659 797 1027 1963 2129 2971 3299 9217\r\n", "output": "3\r\n"}, {"input": "30\r\n19 47 109 179 307 331 389 401 461 509 547 569 617 853 883 1249 1361 1381 1511 1723 1741 1783 2459 2531 2621 3533 3821 4091 5557 6217\r\n20\r\n401 443 563 941 967 997 1535 1567 1655 1747 1787 1945 1999 2251 2305 2543 2735 4415 6245 7555\r\n", "output": "8\r\n"}, {"input": "5\r\n33 78 146 3055 4268\r\n5\r\n2211 2584 5226 9402 9782\r\n", "output": "3\r\n"}, {"input": "5\r\n35 48 52 86 8001\r\n10\r\n332 3430 3554 4704 4860 5096 6215 7583 8228 8428\r\n", "output": "4\r\n"}, {"input": "10\r\n97 184 207 228 269 2084 4450 6396 7214 9457\r\n16\r\n338 1179 1284 1545 1570 2444 3167 3395 3397 5550 6440 7245 7804 7980 9415 9959\r\n", "output": "5\r\n"}, {"input": "30\r\n25 30 41 57 58 62 70 72 76 79 84 85 88 91 98 101 104 109 119 129 136 139 148 151 926 1372 3093 3936 5423 7350\r\n25\r\n1600 1920 2624 3648 3712 3968 4480 4608 4864 5056 5376 5440 5632 5824 6272 6464 6656 6934 6976 7616 8256 8704 8896 9472 9664\r\n", "output": "24\r\n"}, {"input": "5\r\n33 78 146 3055 4268\r\n5\r\n2211 2584 5226 9402 9782\r\n", "output": "3\r\n"}, {"input": "5\r\n35 48 52 86 8001\r\n10\r\n332 3430 3554 4704 4860 5096 6215 7583 8228 8428\r\n", "output": "4\r\n"}, {"input": "10\r\n97 184 207 228 269 2084 4450 6396 7214 9457\r\n16\r\n338 1179 1284 1545 1570 2444 3167 3395 3397 5550 6440 7245 7804 7980 9415 9959\r\n", "output": "5\r\n"}, {"input": "30\r\n25 30 41 57 58 62 70 72 76 79 84 85 88 91 98 101 104 109 119 129 136 139 148 151 926 1372 3093 3936 5423 7350\r\n25\r\n1600 1920 2624 3648 3712 3968 4480 4608 4864 5056 5376 5440 5632 5824 6272 6464 6656 6934 6976 7616 8256 8704 8896 9472 9664\r\n", "output": "24\r\n"}, {"input": "50\r\n51 67 75 186 194 355 512 561 720 876 1077 1221 1503 1820 2153 2385 2568 2608 2937 2969 3271 3311 3481 4081 4093 4171 4255 4256 4829 5020 5192 5636 5817 6156 6712 6717 7153 7436 7608 7612 7866 7988 8264 8293 8867 9311 9879 9882 9889 9908\r\n1\r\n5394\r\n", "output": "1\r\n"}, {"input": "3\r\n3 4 5\r\n3\r\n6 20 25\r\n", "output": "2\r\n"}, {"input": "4\r\n2 3 5 8\r\n4\r\n2 6 8 10\r\n", "output": "1\r\n"}, {"input": "4\r\n3 5 7 11\r\n4\r\n3 5 7 22\r\n", "output": "1\r\n"}, {"input": "2\r\n2 3\r\n3\r\n20 30 50\r\n", "output": "1\r\n"}, {"input": "3\r\n1 2 3\r\n4\r\n2 4 6 49\r\n", "output": "1\r\n"}, {"input": "2\r\n4 5\r\n3\r\n12 15 20\r\n", "output": "1\r\n"}, {"input": "3\r\n2 5 7\r\n3\r\n4 5 7\r\n", "output": "1\r\n"}, {"input": "3\r\n3 5 8\r\n3\r\n6 8 10\r\n", "output": "2\r\n"}, {"input": "2\r\n2 3\r\n4\r\n4 6 9 33\r\n", "output": "1\r\n"}, {"input": "2\r\n2 3\r\n4\r\n4 6 21 40\r\n", "output": "1\r\n"}, {"input": "3\r\n4 9 10\r\n3\r\n8 9 10\r\n", "output": "1\r\n"}, {"input": "5\r\n1 5 6 9 51\r\n5\r\n5 12 18 27 10000\r\n", "output": "1\r\n"}, {"input": "13\r\n1 2 3 4 5 6 7 8 9 10 11 12 13\r\n1\r\n14\r\n", "output": "1\r\n"}]
false
stdio
null
true
215/A
215
A
Python 3
TESTS
46
216
0
119176202
n = int(input()) a = list(map(int,input().split(' '))) m = int(input()) b = list(map(int,input().split(' '))) max_ratio = 0 count = 1 for i in range(n): for j in range(m): if b[j] / a[i] == b[j] // a[i] and b[j] / a[i] > max_ratio: max_ratio = b[j] / a[i] elif b[j] / a[i] == max_ratio: count += 1 print(count)
57
92
0
145679715
input() i = list(map(int, input().split())) input() j = list(map(int, input().split())) x = [b // a for a in i for b in j if b % a == 0] print(x.count(max(x)))
Codeforces Round 132 (Div. 2)
CF
2,012
2
256
Bicycle Chain
Vasya's bicycle chain drive consists of two parts: n stars are attached to the pedal axle, m stars are attached to the rear wheel axle. The chain helps to rotate the rear wheel by transmitting the pedal rotation. We know that the i-th star on the pedal axle has ai (0 < a1 < a2 < ... < an) teeth, and the j-th star on the rear wheel axle has bj (0 < b1 < b2 < ... < bm) teeth. Any pair (i, j) (1 ≤ i ≤ n; 1 ≤ j ≤ m) is called a gear and sets the indexes of stars to which the chain is currently attached. Gear (i, j) has a gear ratio, equal to the value $$\frac{b_{j}}{a_{j}}$$. Since Vasya likes integers, he wants to find such gears (i, j), that their ratios are integers. On the other hand, Vasya likes fast driving, so among all "integer" gears (i, j) he wants to choose a gear with the maximum ratio. Help him to find the number of such gears. In the problem, fraction $$\frac{b_{j}}{a_{j}}$$ denotes division in real numbers, that is, no rounding is performed.
The first input line contains integer n (1 ≤ n ≤ 50) — the number of stars on the bicycle's pedal axle. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 104) in the order of strict increasing. The third input line contains integer m (1 ≤ m ≤ 50) — the number of stars on the rear wheel axle. The fourth line contains m integers b1, b2, ..., bm (1 ≤ bi ≤ 104) in the order of strict increasing. It is guaranteed that there exists at least one gear (i, j), that its gear ratio is an integer. The numbers on the lines are separated by spaces.
Print the number of "integer" gears with the maximum ratio among all "integer" gears.
null
In the first sample the maximum "integer" gear ratio equals 3. There are two gears that have such gear ratio. For one of them a1 = 4, b1 = 12, and for the other a2 = 5, b3 = 15.
[{"input": "2\n4 5\n3\n12 13 15", "output": "2"}, {"input": "4\n1 2 3 4\n5\n10 11 12 13 14", "output": "1"}]
900
["brute force", "implementation"]
57
[{"input": "2\r\n4 5\r\n3\r\n12 13 15\r\n", "output": "2\r\n"}, {"input": "4\r\n1 2 3 4\r\n5\r\n10 11 12 13 14\r\n", "output": "1\r\n"}, {"input": "1\r\n1\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "2\r\n1 2\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n1\r\n2\r\n1 2\r\n", "output": "1\r\n"}, {"input": "4\r\n3 7 11 13\r\n4\r\n51 119 187 221\r\n", "output": "4\r\n"}, {"input": "4\r\n2 3 4 5\r\n3\r\n1 2 3\r\n", "output": "2\r\n"}, {"input": "10\r\n6 12 13 20 48 53 74 92 96 97\r\n10\r\n1 21 32 36 47 54 69 75 95 97\r\n", "output": "1\r\n"}, {"input": "10\r\n5 9 10 14 15 17 19 22 24 26\r\n10\r\n2 11 17 19 21 22 24 25 27 28\r\n", "output": "1\r\n"}, {"input": "10\r\n24 53 56 126 354 432 442 740 795 856\r\n10\r\n273 438 494 619 689 711 894 947 954 958\r\n", "output": "1\r\n"}, {"input": "10\r\n3 4 6 7 8 10 14 16 19 20\r\n10\r\n3 4 5 7 8 10 15 16 18 20\r\n", "output": "1\r\n"}, {"input": "10\r\n1 6 8 14 15 17 25 27 34 39\r\n10\r\n1 8 16 17 19 22 32 39 44 50\r\n", "output": "1\r\n"}, {"input": "10\r\n5 21 22 23 25 32 35 36 38 39\r\n10\r\n3 7 8 9 18 21 23 24 36 38\r\n", "output": "4\r\n"}, {"input": "10\r\n17 239 443 467 661 1069 1823 2333 3767 4201\r\n20\r\n51 83 97 457 593 717 997 1329 1401 1459 1471 1983 2371 2539 3207 3251 3329 5469 6637 6999\r\n", "output": "8\r\n"}, {"input": "20\r\n179 359 401 467 521 601 919 941 1103 1279 1709 1913 1949 2003 2099 2143 2179 2213 2399 4673\r\n20\r\n151 181 191 251 421 967 1109 1181 1249 1447 1471 1553 1619 2327 2551 2791 3049 3727 6071 7813\r\n", "output": "3\r\n"}, {"input": "20\r\n79 113 151 709 809 983 1291 1399 1409 1429 2377 2659 2671 2897 3217 3511 3557 3797 3823 4363\r\n10\r\n19 101 659 797 1027 1963 2129 2971 3299 9217\r\n", "output": "3\r\n"}, {"input": "30\r\n19 47 109 179 307 331 389 401 461 509 547 569 617 853 883 1249 1361 1381 1511 1723 1741 1783 2459 2531 2621 3533 3821 4091 5557 6217\r\n20\r\n401 443 563 941 967 997 1535 1567 1655 1747 1787 1945 1999 2251 2305 2543 2735 4415 6245 7555\r\n", "output": "8\r\n"}, {"input": "5\r\n33 78 146 3055 4268\r\n5\r\n2211 2584 5226 9402 9782\r\n", "output": "3\r\n"}, {"input": "5\r\n35 48 52 86 8001\r\n10\r\n332 3430 3554 4704 4860 5096 6215 7583 8228 8428\r\n", "output": "4\r\n"}, {"input": "10\r\n97 184 207 228 269 2084 4450 6396 7214 9457\r\n16\r\n338 1179 1284 1545 1570 2444 3167 3395 3397 5550 6440 7245 7804 7980 9415 9959\r\n", "output": "5\r\n"}, {"input": "30\r\n25 30 41 57 58 62 70 72 76 79 84 85 88 91 98 101 104 109 119 129 136 139 148 151 926 1372 3093 3936 5423 7350\r\n25\r\n1600 1920 2624 3648 3712 3968 4480 4608 4864 5056 5376 5440 5632 5824 6272 6464 6656 6934 6976 7616 8256 8704 8896 9472 9664\r\n", "output": "24\r\n"}, {"input": "5\r\n33 78 146 3055 4268\r\n5\r\n2211 2584 5226 9402 9782\r\n", "output": "3\r\n"}, {"input": "5\r\n35 48 52 86 8001\r\n10\r\n332 3430 3554 4704 4860 5096 6215 7583 8228 8428\r\n", "output": "4\r\n"}, {"input": "10\r\n97 184 207 228 269 2084 4450 6396 7214 9457\r\n16\r\n338 1179 1284 1545 1570 2444 3167 3395 3397 5550 6440 7245 7804 7980 9415 9959\r\n", "output": "5\r\n"}, {"input": "30\r\n25 30 41 57 58 62 70 72 76 79 84 85 88 91 98 101 104 109 119 129 136 139 148 151 926 1372 3093 3936 5423 7350\r\n25\r\n1600 1920 2624 3648 3712 3968 4480 4608 4864 5056 5376 5440 5632 5824 6272 6464 6656 6934 6976 7616 8256 8704 8896 9472 9664\r\n", "output": "24\r\n"}, {"input": "50\r\n51 67 75 186 194 355 512 561 720 876 1077 1221 1503 1820 2153 2385 2568 2608 2937 2969 3271 3311 3481 4081 4093 4171 4255 4256 4829 5020 5192 5636 5817 6156 6712 6717 7153 7436 7608 7612 7866 7988 8264 8293 8867 9311 9879 9882 9889 9908\r\n1\r\n5394\r\n", "output": "1\r\n"}, {"input": "3\r\n3 4 5\r\n3\r\n6 20 25\r\n", "output": "2\r\n"}, {"input": "4\r\n2 3 5 8\r\n4\r\n2 6 8 10\r\n", "output": "1\r\n"}, {"input": "4\r\n3 5 7 11\r\n4\r\n3 5 7 22\r\n", "output": "1\r\n"}, {"input": "2\r\n2 3\r\n3\r\n20 30 50\r\n", "output": "1\r\n"}, {"input": "3\r\n1 2 3\r\n4\r\n2 4 6 49\r\n", "output": "1\r\n"}, {"input": "2\r\n4 5\r\n3\r\n12 15 20\r\n", "output": "1\r\n"}, {"input": "3\r\n2 5 7\r\n3\r\n4 5 7\r\n", "output": "1\r\n"}, {"input": "3\r\n3 5 8\r\n3\r\n6 8 10\r\n", "output": "2\r\n"}, {"input": "2\r\n2 3\r\n4\r\n4 6 9 33\r\n", "output": "1\r\n"}, {"input": "2\r\n2 3\r\n4\r\n4 6 21 40\r\n", "output": "1\r\n"}, {"input": "3\r\n4 9 10\r\n3\r\n8 9 10\r\n", "output": "1\r\n"}, {"input": "5\r\n1 5 6 9 51\r\n5\r\n5 12 18 27 10000\r\n", "output": "1\r\n"}, {"input": "13\r\n1 2 3 4 5 6 7 8 9 10 11 12 13\r\n1\r\n14\r\n", "output": "1\r\n"}]
false
stdio
null
true
215/A
215
A
Python 3
TESTS
46
186
307,200
93017259
n=int(input()) l1=list(map(int,input().split())) m=int(input()) l2=list(map(int,input().split())) max=0 count=1 for i in l1: for j in l2: if(j % i == 0): if(max == (j/i)): count+=1 if(max < (j/i)): max=j/i print(count)
57
92
0
145885479
n=int(input()) lstn=list(map(int,input().split())) m= int(input()) lstm=list(map(int,input().split())) final=[] for i in lstn: for j in reversed(lstm): if (j/i).is_integer(): final.append(int(j/i)) break print(final.count(max(final)))
Codeforces Round 132 (Div. 2)
CF
2,012
2
256
Bicycle Chain
Vasya's bicycle chain drive consists of two parts: n stars are attached to the pedal axle, m stars are attached to the rear wheel axle. The chain helps to rotate the rear wheel by transmitting the pedal rotation. We know that the i-th star on the pedal axle has ai (0 < a1 < a2 < ... < an) teeth, and the j-th star on the rear wheel axle has bj (0 < b1 < b2 < ... < bm) teeth. Any pair (i, j) (1 ≤ i ≤ n; 1 ≤ j ≤ m) is called a gear and sets the indexes of stars to which the chain is currently attached. Gear (i, j) has a gear ratio, equal to the value $$\frac{b_{j}}{a_{j}}$$. Since Vasya likes integers, he wants to find such gears (i, j), that their ratios are integers. On the other hand, Vasya likes fast driving, so among all "integer" gears (i, j) he wants to choose a gear with the maximum ratio. Help him to find the number of such gears. In the problem, fraction $$\frac{b_{j}}{a_{j}}$$ denotes division in real numbers, that is, no rounding is performed.
The first input line contains integer n (1 ≤ n ≤ 50) — the number of stars on the bicycle's pedal axle. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 104) in the order of strict increasing. The third input line contains integer m (1 ≤ m ≤ 50) — the number of stars on the rear wheel axle. The fourth line contains m integers b1, b2, ..., bm (1 ≤ bi ≤ 104) in the order of strict increasing. It is guaranteed that there exists at least one gear (i, j), that its gear ratio is an integer. The numbers on the lines are separated by spaces.
Print the number of "integer" gears with the maximum ratio among all "integer" gears.
null
In the first sample the maximum "integer" gear ratio equals 3. There are two gears that have such gear ratio. For one of them a1 = 4, b1 = 12, and for the other a2 = 5, b3 = 15.
[{"input": "2\n4 5\n3\n12 13 15", "output": "2"}, {"input": "4\n1 2 3 4\n5\n10 11 12 13 14", "output": "1"}]
900
["brute force", "implementation"]
57
[{"input": "2\r\n4 5\r\n3\r\n12 13 15\r\n", "output": "2\r\n"}, {"input": "4\r\n1 2 3 4\r\n5\r\n10 11 12 13 14\r\n", "output": "1\r\n"}, {"input": "1\r\n1\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "2\r\n1 2\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n1\r\n2\r\n1 2\r\n", "output": "1\r\n"}, {"input": "4\r\n3 7 11 13\r\n4\r\n51 119 187 221\r\n", "output": "4\r\n"}, {"input": "4\r\n2 3 4 5\r\n3\r\n1 2 3\r\n", "output": "2\r\n"}, {"input": "10\r\n6 12 13 20 48 53 74 92 96 97\r\n10\r\n1 21 32 36 47 54 69 75 95 97\r\n", "output": "1\r\n"}, {"input": "10\r\n5 9 10 14 15 17 19 22 24 26\r\n10\r\n2 11 17 19 21 22 24 25 27 28\r\n", "output": "1\r\n"}, {"input": "10\r\n24 53 56 126 354 432 442 740 795 856\r\n10\r\n273 438 494 619 689 711 894 947 954 958\r\n", "output": "1\r\n"}, {"input": "10\r\n3 4 6 7 8 10 14 16 19 20\r\n10\r\n3 4 5 7 8 10 15 16 18 20\r\n", "output": "1\r\n"}, {"input": "10\r\n1 6 8 14 15 17 25 27 34 39\r\n10\r\n1 8 16 17 19 22 32 39 44 50\r\n", "output": "1\r\n"}, {"input": "10\r\n5 21 22 23 25 32 35 36 38 39\r\n10\r\n3 7 8 9 18 21 23 24 36 38\r\n", "output": "4\r\n"}, {"input": "10\r\n17 239 443 467 661 1069 1823 2333 3767 4201\r\n20\r\n51 83 97 457 593 717 997 1329 1401 1459 1471 1983 2371 2539 3207 3251 3329 5469 6637 6999\r\n", "output": "8\r\n"}, {"input": "20\r\n179 359 401 467 521 601 919 941 1103 1279 1709 1913 1949 2003 2099 2143 2179 2213 2399 4673\r\n20\r\n151 181 191 251 421 967 1109 1181 1249 1447 1471 1553 1619 2327 2551 2791 3049 3727 6071 7813\r\n", "output": "3\r\n"}, {"input": "20\r\n79 113 151 709 809 983 1291 1399 1409 1429 2377 2659 2671 2897 3217 3511 3557 3797 3823 4363\r\n10\r\n19 101 659 797 1027 1963 2129 2971 3299 9217\r\n", "output": "3\r\n"}, {"input": "30\r\n19 47 109 179 307 331 389 401 461 509 547 569 617 853 883 1249 1361 1381 1511 1723 1741 1783 2459 2531 2621 3533 3821 4091 5557 6217\r\n20\r\n401 443 563 941 967 997 1535 1567 1655 1747 1787 1945 1999 2251 2305 2543 2735 4415 6245 7555\r\n", "output": "8\r\n"}, {"input": "5\r\n33 78 146 3055 4268\r\n5\r\n2211 2584 5226 9402 9782\r\n", "output": "3\r\n"}, {"input": "5\r\n35 48 52 86 8001\r\n10\r\n332 3430 3554 4704 4860 5096 6215 7583 8228 8428\r\n", "output": "4\r\n"}, {"input": "10\r\n97 184 207 228 269 2084 4450 6396 7214 9457\r\n16\r\n338 1179 1284 1545 1570 2444 3167 3395 3397 5550 6440 7245 7804 7980 9415 9959\r\n", "output": "5\r\n"}, {"input": "30\r\n25 30 41 57 58 62 70 72 76 79 84 85 88 91 98 101 104 109 119 129 136 139 148 151 926 1372 3093 3936 5423 7350\r\n25\r\n1600 1920 2624 3648 3712 3968 4480 4608 4864 5056 5376 5440 5632 5824 6272 6464 6656 6934 6976 7616 8256 8704 8896 9472 9664\r\n", "output": "24\r\n"}, {"input": "5\r\n33 78 146 3055 4268\r\n5\r\n2211 2584 5226 9402 9782\r\n", "output": "3\r\n"}, {"input": "5\r\n35 48 52 86 8001\r\n10\r\n332 3430 3554 4704 4860 5096 6215 7583 8228 8428\r\n", "output": "4\r\n"}, {"input": "10\r\n97 184 207 228 269 2084 4450 6396 7214 9457\r\n16\r\n338 1179 1284 1545 1570 2444 3167 3395 3397 5550 6440 7245 7804 7980 9415 9959\r\n", "output": "5\r\n"}, {"input": "30\r\n25 30 41 57 58 62 70 72 76 79 84 85 88 91 98 101 104 109 119 129 136 139 148 151 926 1372 3093 3936 5423 7350\r\n25\r\n1600 1920 2624 3648 3712 3968 4480 4608 4864 5056 5376 5440 5632 5824 6272 6464 6656 6934 6976 7616 8256 8704 8896 9472 9664\r\n", "output": "24\r\n"}, {"input": "50\r\n51 67 75 186 194 355 512 561 720 876 1077 1221 1503 1820 2153 2385 2568 2608 2937 2969 3271 3311 3481 4081 4093 4171 4255 4256 4829 5020 5192 5636 5817 6156 6712 6717 7153 7436 7608 7612 7866 7988 8264 8293 8867 9311 9879 9882 9889 9908\r\n1\r\n5394\r\n", "output": "1\r\n"}, {"input": "3\r\n3 4 5\r\n3\r\n6 20 25\r\n", "output": "2\r\n"}, {"input": "4\r\n2 3 5 8\r\n4\r\n2 6 8 10\r\n", "output": "1\r\n"}, {"input": "4\r\n3 5 7 11\r\n4\r\n3 5 7 22\r\n", "output": "1\r\n"}, {"input": "2\r\n2 3\r\n3\r\n20 30 50\r\n", "output": "1\r\n"}, {"input": "3\r\n1 2 3\r\n4\r\n2 4 6 49\r\n", "output": "1\r\n"}, {"input": "2\r\n4 5\r\n3\r\n12 15 20\r\n", "output": "1\r\n"}, {"input": "3\r\n2 5 7\r\n3\r\n4 5 7\r\n", "output": "1\r\n"}, {"input": "3\r\n3 5 8\r\n3\r\n6 8 10\r\n", "output": "2\r\n"}, {"input": "2\r\n2 3\r\n4\r\n4 6 9 33\r\n", "output": "1\r\n"}, {"input": "2\r\n2 3\r\n4\r\n4 6 21 40\r\n", "output": "1\r\n"}, {"input": "3\r\n4 9 10\r\n3\r\n8 9 10\r\n", "output": "1\r\n"}, {"input": "5\r\n1 5 6 9 51\r\n5\r\n5 12 18 27 10000\r\n", "output": "1\r\n"}, {"input": "13\r\n1 2 3 4 5 6 7 8 9 10 11 12 13\r\n1\r\n14\r\n", "output": "1\r\n"}]
false
stdio
null
true
908/B
908
B
Python 3
TESTS
32
62
5,632,000
33788778
#empty cells are: '.' #obstacles are: '#' #start position: 'S' #exit position : 'E' from itertools import permutations n, num_cols = list(map(int, input().strip().split())) obstacles = [] for y in range(-1, n + 1): obstacles.append((-1, y)) obstacles.append((n, y)) for x in range(-1, num_cols + 1): obstacles.append((x, -1)) obstacles.append((x, num_cols)) obstacles = set(obstacles) start = (-1, -1) end = (-1, -1) for row_idx in range(n): row = input().strip() for col_idx, let in enumerate(row): if let == '.': continue curr_loc = (row_idx, col_idx) if let == '#': obstacles.add(curr_loc) elif let == 'S': start = curr_loc elif let == 'E': end = curr_loc s = input().strip() total = 0 dirs = [(0,1), (0,-1), (-1,0), (1,0)] for d1, d2, d3, d4 in permutations(dirs, 4): d_mapping = {'0':d1, '1':d2, '2':d3, '3':d4} x, y = list(map(int, start)) for let in s: inc = d_mapping[let] x += inc[0] y += inc[1] pos = (x, y) if pos == end: total += 1 break elif pos in obstacles: break print(total)
46
46
0
146142915
def check(point:list, obstacles:list, x, y): if point[0] > x or point[1] > y or point[0] < 0 or point[1] < 0: return False if tuple(point) in obstacles: return False return True def test_path(start:list, end:list, path:str, direction:list, obstacles:list, x, y): for i in path: i = int(i) if direction[i] == 'left': start[0] -= 1 elif direction[i] == 'right': start[0] += 1 elif direction[i] == 'up': start[1] -= 1 elif direction[i] == 'down': start[1] += 1 # print(f'{start} : {direction[i]}') if not check(start, obstacles, x, y): return False if start == end: return True return False y, x = [int(s) for s in input().split()] obstacles = set() start = list() end = list() for i in range(y): raw = input() for j in range(x): if raw[j] == 'S': start = [j, i] elif raw[j] == 'E': end = [j, i] elif raw[j] == '#': obstacles.add((j, i)) path = input() s = ['left', 'right', 'up', 'down'] directions = [] for i in range(len(s)): for j in range(len(s)): for k in range(len(s)): for l in range(len(s)): if len(set([i,j,k,l])) == 4: directions.append([s[i], s[j], s[k], s[l]]) counter = 0 for i in directions: is_valid = test_path(start.copy(), end, path, i, obstacles, x-1, y-1) if is_valid: counter += 1 # print(f'--------------------{is_valid}') print(counter)
Good Bye 2017
CF
2,017
1
256
New Year and Buggy Bot
Bob programmed a robot to navigate through a 2d maze. The maze has some obstacles. Empty cells are denoted by the character '.', where obstacles are denoted by '#'. There is a single robot in the maze. Its start position is denoted with the character 'S'. This position has no obstacle in it. There is also a single exit in the maze. Its position is denoted with the character 'E'. This position has no obstacle in it. The robot can only move up, left, right, or down. When Bob programmed the robot, he wrote down a string of digits consisting of the digits 0 to 3, inclusive. He intended for each digit to correspond to a distinct direction, and the robot would follow the directions in order to reach the exit. Unfortunately, he forgot to actually assign the directions to digits. The robot will choose some random mapping of digits to distinct directions. The robot will map distinct digits to distinct directions. The robot will then follow the instructions according to the given string in order and chosen mapping. If an instruction would lead the robot to go off the edge of the maze or hit an obstacle, the robot will crash and break down. If the robot reaches the exit at any point, then the robot will stop following any further instructions. Bob is having trouble debugging his robot, so he would like to determine the number of mappings of digits to directions that would lead the robot to the exit.
The first line of input will contain two integers n and m (2 ≤ n, m ≤ 50), denoting the dimensions of the maze. The next n lines will contain exactly m characters each, denoting the maze. Each character of the maze will be '.', '#', 'S', or 'E'. There will be exactly one 'S' and exactly one 'E' in the maze. The last line will contain a single string s (1 ≤ |s| ≤ 100) — the instructions given to the robot. Each character of s is a digit from 0 to 3.
Print a single integer, the number of mappings of digits to directions that will lead the robot to the exit.
null
For the first sample, the only valid mapping is $$0 \rightarrow D, 1 \rightarrow L, 2 \rightarrow U, 3 \rightarrow R$$, where D is down, L is left, U is up, R is right.
[{"input": "5 6\n.....#\nS....#\n.#....\n.#....\n...E..\n333300012", "output": "1"}, {"input": "6 6\n......\n......\n..SE..\n......\n......\n......\n01232123212302123021", "output": "14"}, {"input": "5 3\n...\n.S.\n###\n.E.\n...\n3", "output": "0"}]
1,200
["brute force", "implementation"]
46
[{"input": "5 6\r\n.....#\r\nS....#\r\n.#....\r\n.#....\r\n...E..\r\n333300012\r\n", "output": "1\r\n"}, {"input": "6 6\r\n......\r\n......\r\n..SE..\r\n......\r\n......\r\n......\r\n01232123212302123021\r\n", "output": "14\r\n"}, {"input": "5 3\r\n...\r\n.S.\r\n###\r\n.E.\r\n...\r\n3\r\n", "output": "0\r\n"}, {"input": "10 10\r\n.#......#.\r\n#.........\r\n#.........\r\n....#.#..E\r\n.......#..\r\n....##....\r\n....S.....\r\n....#.....\r\n.........#\r\n...##...#.\r\n23323332313123221123020122221313323310313122323233\r\n", "output": "0\r\n"}, {"input": "8 9\r\n.........\r\n.........\r\n.........\r\n.E.#.....\r\n.........\r\n.........\r\n...#.S...\r\n.........\r\n10001100111000010121100000110110110100000100000100\r\n", "output": "2\r\n"}, {"input": "15 13\r\n.............\r\n.............\r\n.............\r\n.........#...\r\n..#..........\r\n.............\r\n..........E..\r\n.............\r\n.............\r\n.#...........\r\n.....#.......\r\n..........#..\r\n..........S..\r\n.............\r\n.........#...\r\n32222221111222312132110100022020202131222103103330\r\n", "output": "2\r\n"}, {"input": "5 5\r\n.....\r\n.....\r\n..SE.\r\n.....\r\n.....\r\n012330213120031231022103231013201032301223011230102320130231321012030321213002133201130201322031\r\n", "output": "24\r\n"}, {"input": "2 2\r\nS.\r\n.E\r\n23\r\n", "output": "4\r\n"}, {"input": "2 2\r\nS.\r\n.E\r\n03\r\n", "output": "4\r\n"}, {"input": "2 2\r\nSE\r\n..\r\n22\r\n", "output": "6\r\n"}, {"input": "2 2\r\nS.\r\nE.\r\n11\r\n", "output": "6\r\n"}, {"input": "2 2\r\n#E\r\nS.\r\n01\r\n", "output": "2\r\n"}, {"input": "10 10\r\n####S.####\r\n#####.####\r\n#####.####\r\n#####.####\r\n#####..###\r\n######.###\r\n######.###\r\n######.E##\r\n##########\r\n##########\r\n0111101110\r\n", "output": "2\r\n"}, {"input": "10 10\r\n#####..E##\r\n#####.S.##\r\n#####...##\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n20\r\n", "output": "4\r\n"}, {"input": "10 10\r\n#####ES.##\r\n######.###\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n##########\r\n3\r\n", "output": "6\r\n"}, {"input": "2 10\r\nS........E\r\n..........\r\n33333333333333333\r\n", "output": "6\r\n"}, {"input": "2 2\r\n..\r\nSE\r\n0\r\n", "output": "6\r\n"}, {"input": "2 2\r\nSE\r\n##\r\n0\r\n", "output": "6\r\n"}, {"input": "2 2\r\nS.\r\nE.\r\n012\r\n", "output": "8\r\n"}, {"input": "2 3\r\nS.E\r\n###\r\n1222\r\n", "output": "0\r\n"}, {"input": "2 5\r\nS...E\r\n.....\r\n133330\r\n", "output": "1\r\n"}, {"input": "5 5\r\n.....\r\n.....\r\n.S.E.\r\n.....\r\n.....\r\n001111\r\n", "output": "6\r\n"}, {"input": "3 5\r\n....S\r\n....#\r\n....E\r\n0112\r\n", "output": "1\r\n"}, {"input": "2 2\r\nSE\r\n..\r\n123\r\n", "output": "8\r\n"}, {"input": "2 10\r\n........ES\r\n..........\r\n123\r\n", "output": "8\r\n"}, {"input": "2 2\r\nS.\r\n.E\r\n2311\r\n", "output": "4\r\n"}, {"input": "2 2\r\nS.\r\n.E\r\n0012\r\n", "output": "0\r\n"}, {"input": "2 7\r\nS.....E\r\n#######\r\n01111111\r\n", "output": "0\r\n"}, {"input": "2 2\r\nS.\r\n.E\r\n1123\r\n", "output": "0\r\n"}, {"input": "2 3\r\nS.E\r\n...\r\n0111\r\n", "output": "0\r\n"}, {"input": "2 50\r\n.................................................E\r\nS.................................................\r\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n", "output": "0\r\n"}, {"input": "5 2\r\n..\r\n..\r\n..\r\n..\r\nSE\r\n0\r\n", "output": "6\r\n"}, {"input": "3 3\r\nE..\r\n.S.\r\n...\r\n001123110023221103\r\n", "output": "0\r\n"}, {"input": "2 2\r\nS#\r\nE#\r\n012\r\n", "output": "6\r\n"}, {"input": "2 2\r\nES\r\n..\r\n011\r\n", "output": "6\r\n"}, {"input": "2 2\r\nSE\r\n..\r\n011\r\n", "output": "6\r\n"}, {"input": "2 2\r\nS.\r\nE.\r\n102\r\n", "output": "8\r\n"}, {"input": "3 2\r\nE#\r\n##\r\nS#\r\n0112\r\n", "output": "0\r\n"}]
false
stdio
null
true
10/D
10
D
Python 3
TESTS
16
124
307,200
101848099
def LCS(n, m, arr1, arr2): D = [] for i in range(0, n+1): D.append([]) D[i].append([]) for j in range(1, m+1): D[0].append([]) for i in range(1, n+1): for j in range(1, m+1): if arr1[i-1]==arr2[j-1]: D[i].append(list(D[i-1][j-1])) D[i][j].append(arr1[i-1]) elif len(D[i-1][j])>len(D[i][j-1]): D[i].append(D[i-1][j]) else: D[i].append(D[i][j-1]) return(D[n][m]) def LIC(arr): if len(arr)==0: return [] if len(arr)==1: return arr for i in range(0, len(arr)): if arr[i]==min(arr): l=i break c1 = LIC(arr[:l]) c2 = LIC(arr[(l+1):]) if len(c1)>(len(c2)+1): return c1 else: return ([arr[l]]+c2) n = int(input()) arr1 = list(map(int, input().split())) m = int(input()) arr2 = list(map(int, input().split())) ans=LIC(LCS(n, m, arr1, arr2)) print(len(ans)) print(*ans)
61
93
3,276,800
230994675
n = int(input()) a = list(map(int, input().split())) m = int(input()) b = list(map(int, input().split())) lenth = [0] * m elem = [[] for i in range(m)] for i in range(n): c = 0 p = -1 for j in range(m): if a[i] == b[j] and c + 1 > lenth[j]: lenth[j] = c + 1 elem[j] = (elem[p] if p != -1 else []) + [a[i]] if a[i] > b[j] and c < lenth[j]: c = lenth[j] p = j max_ = max(lenth) print(max_) if max_ > 0: ar = [] for i in elem: if len(i) > len(ar): ar = i print(*ar)
Codeforces Beta Round 10
ICPC
2,010
1
256
LCIS
This problem differs from one which was on the online contest. The sequence a1, a2, ..., an is called increasing, if ai < ai + 1 for i < n. The sequence s1, s2, ..., sk is called the subsequence of the sequence a1, a2, ..., an, if there exist such a set of indexes 1 ≤ i1 < i2 < ... < ik ≤ n that aij = sj. In other words, the sequence s can be derived from the sequence a by crossing out some elements. You are given two sequences of integer numbers. You are to find their longest common increasing subsequence, i.e. an increasing sequence of maximum length that is the subsequence of both sequences.
The first line contains an integer n (1 ≤ n ≤ 500) — the length of the first sequence. The second line contains n space-separated integers from the range [0, 109] — elements of the first sequence. The third line contains an integer m (1 ≤ m ≤ 500) — the length of the second sequence. The fourth line contains m space-separated integers from the range [0, 109] — elements of the second sequence.
In the first line output k — the length of the longest common increasing subsequence. In the second line output the subsequence itself. Separate the elements with a space. If there are several solutions, output any.
null
null
[{"input": "7\n2 3 1 6 5 4 6\n4\n1 3 5 6", "output": "3\n3 5 6"}, {"input": "5\n1 2 0 2 1\n3\n1 0 1", "output": "2\n0 1"}]
2,800
["dp"]
61
[{"input": "7\r\n2 3 1 6 5 4 6\r\n4\r\n1 3 5 6\r\n", "output": "3\r\n3 5 6 \r\n"}, {"input": "5\r\n1 2 0 2 1\r\n3\r\n1 0 1\r\n", "output": "2\r\n0 1 \r\n"}, {"input": "2\r\n6 10\r\n3\r\n6 3 3\r\n", "output": "1\r\n6 \r\n"}, {"input": "1\r\n7\r\n2\r\n7 9\r\n", "output": "1\r\n7 \r\n"}, {"input": "3\r\n37 49 24\r\n3\r\n33 5 70\r\n", "output": "0\r\n\r\n"}, {"input": "10\r\n7 10 1 2 1 7 1 5 9 9\r\n9\r\n6 2 5 6 7 7 5 5 2\r\n", "output": "2\r\n2 7 \r\n"}, {"input": "9\r\n7 0 1 2 6 0 10 3 5\r\n4\r\n8 4 0 3\r\n", "output": "2\r\n0 3 \r\n"}, {"input": "9\r\n7 4 4 5 0 6 5 4 10\r\n4\r\n5 2 10 9\r\n", "output": "2\r\n5 10 \r\n"}, {"input": "8\r\n7 8 6 6 8 10 3 3\r\n5\r\n7 4 10 8 7\r\n", "output": "2\r\n7 8 \r\n"}, {"input": "7\r\n4 2 4 3 10 3 6\r\n9\r\n7 5 2 3 0 1 6 1 4\r\n", "output": "3\r\n2 3 6 \r\n"}, {"input": "1\r\n7\r\n10\r\n1 8 8 10 9 10 4 6 0 5\r\n", "output": "0\r\n\r\n"}, {"input": "2\r\n5 2\r\n4\r\n8 8 0 4\r\n", "output": "0\r\n\r\n"}, {"input": "3\r\n1 3 9\r\n10\r\n8 0 10 5 7 0 3 1 2 4\r\n", "output": "1\r\n1 \r\n"}, {"input": "15\r\n10 4 7 7 10 1 4 9 1 10 9 6 8 8 2\r\n2\r\n0 4\r\n", "output": "1\r\n4 \r\n"}, {"input": "16\r\n8 8 6 7 10 0 10 1 7 6 6 0 4 2 6 7\r\n12\r\n9 3 8 4 10 3 9 8 3 7 10 4\r\n", "output": "2\r\n8 10 \r\n"}, {"input": "23\r\n174 172 196 135 91 174 208 92 132 53 202 118 5 244 161 140 71 21 185 56 60 195 217\r\n17\r\n38 218 120 77 22 214 164 194 79 195 36 167 42 89 201 80 11\r\n", "output": "1\r\n195 \r\n"}, {"input": "53\r\n135 168 160 123 6 250 251 158 245 184 206 35 189 64 138 12 69 21 112 198 165 211 109 40 192 98 236 216 255 98 136 38 67 79 25 196 216 64 134 124 102 232 229 102 179 138 111 123 2 93 25 162 57\r\n57\r\n64 143 41 144 73 26 11 17 224 209 167 162 129 39 102 224 254 45 120 2 138 213 139 133 169 54 7 143 242 118 155 189 100 185 145 168 248 131 83 216 142 180 225 35 226 202 8 15 200 192 75 140 191 189 75 116 202\r\n", "output": "3\r\n64 138 192 \r\n"}, {"input": "83\r\n95 164 123 111 177 71 38 225 103 59 210 209 117 139 115 140 66 21 39 84 14 227 0 43 90 233 96 98 232 237 108 139 55 220 14 225 134 39 68 167 193 125 86 216 87 14 94 75 255 24 165 98 177 191 239 123 98 90 29 52 155 231 187 90 180 1 31 237 167 145 242 115 61 190 47 41 61 206 191 248 126 196 26\r\n49\r\n234 134 9 207 37 95 116 239 105 197 191 15 151 249 156 235 17 161 197 199 87 78 191 188 44 151 179 238 72 29 228 157 174 99 190 114 95 185 160 168 58 216 131 151 233 204 213 87 76\r\n", "output": "2\r\n95 233 \r\n"}, {"input": "13\r\n55 160 86 99 92 148 81 36 216 191 214 127 44\r\n85\r\n92 12 64 21 221 225 119 243 147 47 244 112 212 237 209 121 81 239 43 104 3 254 52 13 1 210 28 18 199 75 251 146 77 28 253 211 50 35 42 160 157 104 155 37 241 78 42 190 150 228 193 96 190 178 232 65 231 186 1 123 212 126 239 22 214 186 245 249 66 234 57 78 173 229 185 23 240 91 127 177 240 105 77 208 86\r\n", "output": "2\r\n160 214 \r\n"}, {"input": "94\r\n100 161 99 102 209 51 5 188 217 53 121 5 233 55 25 156 136 195 243 157 110 202 136 151 86 171 253 38 126 40 27 76 60 119 222 52 134 104 184 146 133 220 88 108 246 61 215 184 181 134 223 164 41 193 232 217 38 192 226 91 81 99 204 232 178 4 187 61 160 255 121 142 191 114 114 181 226 49 86 55 252 169 59 190 246 93 21 22 17 18 120 88 93 144\r\n30\r\n61 51 176 38 119 33 100 185 103 84 161 166 103 227 43 200 127 53 52 89 19 215 76 254 110 30 239 247 11 182\r\n", "output": "3\r\n51 53 110 \r\n"}, {"input": "6\r\n3 5 4 6 8 1\r\n10\r\n3 3 0 5 4 0 10 5 6 8\r\n", "output": "4\r\n3 5 6 8 \r\n"}, {"input": "10\r\n0 1 2 3 4 5 6 7 8 9\r\n10\r\n0 1 2 3 4 5 6 7 8 9\r\n", "output": "10\r\n0 1 2 3 4 5 6 7 8 9 \r\n"}, {"input": "8\r\n2 3 4 5 6 8 9 5\r\n9\r\n2 2 3 4 5 6 6 8 9\r\n", "output": "7\r\n2 3 4 5 6 8 9 \r\n"}, {"input": "8\r\n0 4 10 6 7 2 8 5\r\n7\r\n0 4 6 7 7 0 8\r\n", "output": "5\r\n0 4 6 7 8 \r\n"}, {"input": "10\r\n0 1 2 3 4 5 6 7 8 9\r\n10\r\n0 1 2 3 4 5 6 7 8 9\r\n", "output": "10\r\n0 1 2 3 4 5 6 7 8 9 \r\n"}, {"input": "17\r\n12 17 39 156 100 177 188 129 14 142 45 144 243 151 158 194 245\r\n16\r\n125 12 17 199 65 39 100 185 129 194 142 144 62 92 158 194\r\n", "output": "9\r\n12 17 39 100 129 142 144 158 194 \r\n"}, {"input": "20\r\n7 17 24 27 36 45 62 92 93 94 98 112 114 138 143 156 173 199 204 207\r\n20\r\n7 17 24 27 36 45 62 92 93 94 98 112 114 138 143 156 173 199 204 207\r\n", "output": "20\r\n7 17 24 27 36 45 62 92 93 94 98 112 114 138 143 156 173 199 204 207 \r\n"}, {"input": "13\r\n0 46 104 116 63 118 158 16 221 222 136 245 223\r\n9\r\n0 46 104 116 118 158 221 222 245\r\n", "output": "9\r\n0 46 104 116 118 158 221 222 245 \r\n"}, {"input": "13\r\n34 38 51 57 73 125 147 158 160 178 188 198 235\r\n15\r\n34 38 51 57 73 125 147 158 160 178 255 67 188 198 235\r\n", "output": "13\r\n34 38 51 57 73 125 147 158 160 178 188 198 235 \r\n"}, {"input": "17\r\n25 29 37 207 122 189 118 42 54 95 154 160 162 225 228 237 248\r\n19\r\n25 29 248 37 147 209 42 54 255 95 154 160 162 225 228 237 73 248 10\r\n", "output": "13\r\n25 29 37 42 54 95 154 160 162 225 228 237 248 \r\n"}, {"input": "10\r\n62914292 123971042 784965687 324817892 379711365 394545872 813282270 822333477 865397146 437913515\r\n9\r\n297835672 62914292 123971042 324817892 379711365 394545872 813282270 822333477 865397146\r\n", "output": "8\r\n62914292 123971042 324817892 379711365 394545872 813282270 822333477 865397146 \r\n"}, {"input": "10\r\n130077811 57466561 335978192 71385678 434259735 454136111 482887469 530031703 688581885 809880630\r\n8\r\n373627898 57466561 71385678 434259735 454136111 482887469 530031703 809880630\r\n", "output": "7\r\n57466561 71385678 434259735 454136111 482887469 530031703 809880630 \r\n"}, {"input": "17\r\n364396044 90653796 82853043 311258337 326557522 362475139 415783272 428510002 840021181 469284863 541444887 650535473 649258258 750028895 791368777 808443140 959785237\r\n13\r\n82853043 311258337 326557522 362475139 415783272 428510002 469284863 541444887 649258258 750028895 791368777 808443140 959785237\r\n", "output": "13\r\n82853043 311258337 326557522 362475139 415783272 428510002 469284863 541444887 649258258 750028895 791368777 808443140 959785237 \r\n"}, {"input": "3\r\n6379263 55134355 76061584\r\n3\r\n6379263 55134355 76061584\r\n", "output": "3\r\n6379263 55134355 76061584 \r\n"}, {"input": "3\r\n48875076 71023491 76538219\r\n3\r\n48875076 71023491 76538219\r\n", "output": "3\r\n48875076 71023491 76538219 \r\n"}, {"input": "5\r\n6621317 78540394 52064998 89150480 53659440\r\n3\r\n78540394 46008538 839195\r\n", "output": "1\r\n78540394 \r\n"}, {"input": "2\r\n34665180 51128665\r\n5\r\n71074966 34665180 47089728 44119345 51128665\r\n", "output": "2\r\n34665180 51128665 \r\n"}, {"input": "4\r\n3 4 9 1\r\n7\r\n5 3 8 9 10 2 1\r\n", "output": "2\r\n3 9 \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, 'r') as f: input_lines = [line.strip() for line in f] n = int(input_lines[0]) a = list(map(int, input_lines[1].split())) m = int(input_lines[2]) b = list(map(int, input_lines[3].split())) # Read reference output to get k_ref with open(output_path, 'r') as f: ref_lines = [line.strip() for line in f if line.strip()] if not ref_lines: print(0) return k_ref = int(ref_lines[0]) # Read submission output with open(submission_path, 'r') as f: sub_lines = [line.strip() for line in f if line.strip()] if len(sub_lines) < 1: print(0) return try: k_sub = int(sub_lines[0]) except: print(0) return if k_sub != k_ref: print(0) return if len(sub_lines) < 2: s_sub = [] else: try: s_sub = list(map(int, sub_lines[1].split())) except: print(0) return if len(s_sub) != k_sub: print(0) return # Check strictly increasing for i in range(len(s_sub)-1): if s_sub[i] >= s_sub[i+1]: print(0) return # Check subsequence in a and b def is_sub(s, main): ptr = 0 l = len(s) if l ==0: return True for num in main: if num == s[ptr]: ptr +=1 if ptr == l: break return ptr == l if not is_sub(s_sub, a): print(0) return if not is_sub(s_sub, b): print(0) return # All checks passed print(1) if __name__ == "__main__": main()
true
79/C
79
C
PyPy 3
TESTS
3
310
4,608,000
145197568
import math import sys import queue from heapq import heappop, heappush import random def solve(): def f(s, pos, b): if len(b) - 1 > pos: return -1 else: for i in range(len(b)): if s[pos - i] != b[-1 - i]: return -1 return len(b) s = str(input()) n = int(input()) b = [] for i in range(n): b.append(str(input())) dp = [0 for i in range(len(s) + 1)] for end in range(len(s)): dp[end + 1] = -1 mn = 1 << 60 for ps in b: k = f(s, end, ps) if k != -1: mn = min(mn, k) if mn == 1 << 60: dp[end + 1] = dp[end] + 1 else: dp[end + 1] = mn - 1 # big brain moment print(max(dp), dp.index(max(dp)) - max(dp)) if __name__ == '__main__': multi_test = 0 if multi_test: t = int(sys.stdin.readline()) for _ in range(t): solve() else: solve()
80
436
716,800
5639831
s, n = input(), int(input()) t = [input() for i in range(n)] def f(i): global t for j in range(n): if i < j: if len(t[j]) < len(t[i]) and t[j] in t[i]: return False elif j < i and t[j] in t[i]: return False return True t = [t[i] for i in range(n) if f(i)] n = len(s) r = [0] * n for p in t: i, m = -1, len(p) - 1 while True: i = s.find(p, i + 1) if i == -1: break r[i] += 1 r[i + m] += 2 d, j, q = 0, -1, [-1] for i in range(n): if r[i] & 1: q.append(i) if r[i] & 2: j = q.pop(0) if i - j > d: d, k = i - j, j j = q.pop(0) if n - j > d: d, k = n - j, j print(d - 1, k + 1)
Codeforces Beta Round 71
CF
2,011
2
256
Beaver
After Fox Ciel got off a bus, she found that the bus she was on was a wrong bus and she lost her way in a strange town. However, she fortunately met her friend Beaver Taro and asked which way to go to her castle. Taro's response to her was a string s, and she tried to remember the string s correctly. However, Ciel feels n strings b1, b2, ... , bn are really boring, and unfortunately she dislikes to remember a string that contains a boring substring. To make the thing worse, what she can remember is only the contiguous substring of s. Determine the longest contiguous substring of s that does not contain any boring string, so that she can remember the longest part of Taro's response.
In the first line there is a string s. The length of s will be between 1 and 105, inclusive. In the second line there is a single integer n (1 ≤ n ≤ 10). Next n lines, there is a string bi (1 ≤ i ≤ n). Each length of bi will be between 1 and 10, inclusive. Each character of the given strings will be either a English alphabet (both lowercase and uppercase) or a underscore ('_') or a digit. Assume that these strings are case-sensitive.
Output in the first line two space-separated integers len and pos: the length of the longest contiguous substring of s that does not contain any bi, and the first position of the substring (0-indexed). The position pos must be between 0 and |s| - len inclusive, where |s| is the length of string s. If there are several solutions, output any.
null
In the first sample, the solution is traight_alon. In the second sample, the solution is an empty string, so the output can be «0 0», «0 1», «0 2», and so on. In the third sample, the solution is either nagio or oisii.
[{"input": "Go_straight_along_this_street\n5\nstr\nlong\ntree\nbiginteger\nellipse", "output": "12 4"}, {"input": "IhaveNoIdea\n9\nI\nh\na\nv\ne\nN\no\nI\nd", "output": "0 0"}, {"input": "unagioisii\n2\nioi\nunagi", "output": "5 5"}]
1,800
["data structures", "dp", "greedy", "hashing", "strings", "two pointers"]
80
[{"input": "Go_straight_along_this_street\r\n5\r\nstr\r\nlong\r\ntree\r\nbiginteger\r\nellipse\r\n", "output": "12 4\r\n"}, {"input": "IhaveNoIdea\r\n9\r\nI\r\nh\r\na\r\nv\r\ne\r\nN\r\no\r\nI\r\nd\r\n", "output": "0 0\r\n"}, {"input": "unagioisii\r\n2\r\nioi\r\nunagi\r\n", "output": "5 5\r\n"}, {"input": "abcabcabcabc\r\n3\r\nabcabca\r\nbcab\r\ncabc\r\n", "output": "4 6\r\n"}, {"input": "ThankYouForParticipatingRound71\r\n6\r\nForP\r\noun\r\nnkYouForP\r\nTha\r\nouForP\r\nound7\r\n", "output": "18 9\r\n"}, {"input": "WishYourSolutionPassesFinalTests\r\n10\r\nrSoluti\r\ninal\r\nolutionPas\r\nassesFin\r\nourSo\r\nonP\r\nWishYourSo\r\nsFinalTes\r\nhYourSolut\r\nonPas\r\n", "output": "9 15\r\n"}, {"input": "9\r\n1\r\n9\r\n", "output": "0 0\r\n"}, {"input": "Z\r\n1\r\na\r\n", "output": "1 0\r\n"}, {"input": "12abcdefghijklmnop\r\n10\r\nabcd\r\nabc\r\nab\r\na\r\nklmn\r\nlmn\r\nmn\r\nn\r\nop\r\n12\r\n", "output": "12 3\r\n"}, {"input": "12abcdefghijklmnop\r\n10\r\na\r\nop\r\nab\r\nabc\r\nabcd\r\nn\r\nmn\r\n12\r\nlmn\r\nklmn\r\n", "output": "12 3\r\n"}, {"input": "12abcdefghijklmnop\r\n10\r\nlmn\r\nabc\r\na\r\nklmn\r\nn\r\nabcd\r\nop\r\nmn\r\nab\r\n12\r\n", "output": "12 3\r\n"}, {"input": "x5x5\r\n10\r\nx5\r\nx5x\r\nx5x5\r\nx5\r\nx5\r\nQyBa0yZO_c\r\n_troGKxtGQ\r\nRItOLVC3ok\r\niD_57rFYR1\r\nvZhgXwTnE4\r\n", "output": "2 1\r\n"}, {"input": "aaaay\r\n10\r\naa\r\naa\r\naaa\r\nay\r\naaay\r\ndltfBoU4nF\r\nYhImrXw62Y\r\nwswb4v7CRb\r\npjxxEE3S26\r\nwxDxW1MRaI\r\n", "output": "1 4\r\n"}, {"input": "iiiiii\r\n10\r\nii\r\niiii\r\niiiii\r\niii\r\niiii\r\n5avjcwIsDh\r\nGgiVQ9ylRz\r\newWmNAJAL9\r\nk83baq5H2U\r\nXRX3fJ4dH8\r\n", "output": "1 5\r\n"}, {"input": "ffBBfBB\r\n10\r\nBBfB\r\nffBBfB\r\nffBBf\r\nffBBf\r\nBfB\r\n1ehoxM6CU8\r\ntvB4q05iuU\r\nEYGkY6VxQO\r\nbKbE2ajjBW\r\nhqTKbQUZds\r\n", "output": "4 1\r\n"}, {"input": "aaUUUUaa\r\n10\r\naUUU\r\naaU\r\naUU\r\nUUUU\r\nUU\r\neV1R6e2tCQ\r\nmwseAsylFZ\r\njkfXnCVagR\r\nRGPm9O09J8\r\nVBRpGUFrDB\r\n", "output": "3 5\r\n"}, {"input": "1111e1e1e\r\n10\r\n11\r\n1111e\r\n1111\r\ne1e1e\r\n1e1\r\npuCrQxcVPh\r\nfnbEroh3w4\r\nyie11ihNIi\r\n2_23hx3yX9\r\noPKXwByQLT\r\n", "output": "3 6\r\n"}, {"input": "aMaMaMMaaM\r\n10\r\nMMaaM\r\nMMaaM\r\naa\r\naMaMMaa\r\nMaMa\r\nWoEVf7UuGQ\r\n2q8cm0_WfI\r\nZR63WSVBlC\r\ndtO9nmkXsc\r\ntHL2amBqOS\r\n", "output": "6 2\r\n"}, {"input": "NNNIIIIIINN\r\n10\r\nNNIIIIIIN\r\nIIIINN\r\nIIIINN\r\nNNIIIII\r\nNNII\r\nlHJxS_HfkO\r\nPsss2tjeao\r\nY4Adt_8FXb\r\nkc9mq2vWmZ\r\nVJYusUjoTq\r\n", "output": "8 2\r\n"}, {"input": "cCCcCCCcCccc\r\n10\r\ncC\r\nCCcCCCcCc\r\nCCcC\r\ncCc\r\ncCCC\r\npUKBhDtW8W\r\n0ap4vlgGej\r\nk059r28XMJ\r\nySQTZIOz3r\r\nFHn5rVpM8G\r\n", "output": "4 8\r\n"}, {"input": "7hh77hhhhhh7h\r\n10\r\nhhhh7\r\nhh77hhhhh\r\n7hhhhh\r\nhh7\r\nh77hhhhhh7\r\nC_t1uIxLWp\r\nHwH5EkVGCt\r\nyUgWASGwfR\r\nRfah5Fa12E\r\nS9CmGvTVKM\r\n", "output": "7 2\r\n"}, {"input": "3cc3\r\n10\r\ncc\r\nc3\r\n3c\r\ncc\r\ncc\r\n3cc3\r\n3cc3\r\n3cc\r\nc3\r\njLnOy3kI3M\r\n", "output": "1 3\r\n"}, {"input": "70aa70a\r\n10\r\n70\r\n0aa70\r\n0aa70\r\naa70\r\n70aa\r\n70aa\r\n70aa70a\r\naa7\r\n70aa7\r\nicHuodu1Ux\r\n", "output": "3 1\r\n"}, {"input": "YUYEYEUUEU\r\n10\r\nYEYEUUEU\r\nEUUE\r\nUU\r\nYEYEUUE\r\nYEYE\r\nUU\r\nEY\r\nEYEUU\r\nYEYEUU\r\niBXoTbQ7X3\r\n", "output": "4 0\r\n"}, {"input": "wwwwDwwwwD\r\n10\r\nwD\r\nDwww\r\nwD\r\nDwwww\r\nwwwwD\r\nDww\r\nwD\r\nwwDww\r\nwwww\r\nwwww\r\n", "output": "3 6\r\n"}, {"input": "4tg4ttgg47t44tg4ttgg47t4\r\n10\r\n47t44tg4tt\r\nttgg4\r\ng4ttgg47t4\r\ng47t44t\r\ntg4ttg\r\n44tg4ttg\r\nttgg47\r\nt44tg\r\ng47t44tg\r\n4ttgg47t4\r\n", "output": "8 5\r\n"}, {"input": "g0sg00AAA0Asggss0sAg0sg00AAA0Asggss0sA\r\n10\r\nAg0sg00AAA\r\nAsggss\r\nsAg\r\nAsggss0s\r\nggss0sAg\r\nsAg0sg\r\nggss0sA\r\n0sg00AA\r\nAAA0A\r\nAA0\r\n", "output": "8 18\r\n"}, {"input": "000gooo0g0vgovvv0oggv0v0go000gooo0g0vgovvv0oggv0v0go\r\n10\r\ngv0v0go\r\n0gooo0g\r\ngooo0g0v\r\no000\r\ngooo0g0v\r\nv0go000go\r\ngo000gooo0\r\nv0v0go00\r\nvv\r\nggv0v0\r\n", "output": "10 30\r\n"}, {"input": "B2KR\r\n10\r\nB2\r\n2KR\r\nB2KR\r\n2K\r\n2KR\r\nKR\r\n2KR\r\nB2KR\r\n2K\r\n2KR\r\n", "output": "1 3\r\n"}, {"input": "dxY_8\r\n10\r\nxY_8\r\ndxY\r\ndx\r\nY_\r\nxY_\r\ndx\r\nxY\r\ndx\r\nxY_8\r\ndxY\r\n", "output": "2 3\r\n"}, {"input": "umi4qX\r\n10\r\nqX\r\num\r\n4qX\r\nqX\r\numi4qX\r\numi4\r\numi4\r\numi4q\r\nmi4q\r\numi4q\r\n", "output": "3 2\r\n"}, {"input": "4stuNRe\r\n10\r\n4stu\r\nstuN\r\nstuNRe\r\n4stu\r\ntuNRe\r\nst\r\ntuNR\r\n4stuN\r\ntuN\r\n4stu\r\n", "output": "4 3\r\n"}, {"input": "bTnstTqbTnstTq\r\n10\r\nbTnstTq\r\nnstTqbT\r\nTqbT\r\nbTns\r\nTqbT\r\nTns\r\nTnstT\r\nTnstTqb\r\nnstT\r\nstT\r\n", "output": "4 6\r\n"}, {"input": "Oq02utSVOq02utSV\r\n10\r\n2utSVOq\r\n2utSVO\r\n02utS\r\nSVOq0\r\nut\r\nOq\r\nOq\r\nq02utSVO\r\nOq02utSV\r\nVOq0\r\n", "output": "4 9\r\n"}, {"input": "DpGGt6_wGLPDpGGt6_wGLP\r\n10\r\n6_wGL\r\nGLPDpGG\r\nt6_wG\r\nPDpGGt6_\r\nwGLPDpGGt6\r\n6_\r\n_wGL\r\npGGt6_\r\n6_wGLPD\r\n6_wGLPDpG\r\n", "output": "8 9\r\n"}, {"input": "7QTfE4ALvzkzzD4j7QTfE4ALvzkzzD4j\r\n10\r\nvzkzzD4j7\r\nE4AL\r\nTfE4ALv\r\nzzD4j7QT\r\nzkzzD4j7\r\n4AL\r\nj7Q\r\nE4ALvzkzzD\r\nvzkzzD4j\r\n4ALvzk\r\n", "output": "9 22\r\n"}, {"input": "6CLznedj88834gqTIhMTPjm_3DbkQpuYkBvU3o55h79ntRqjHTOu3WWNNGLyQSZ_o45mK5mMtRJmWUq2twqWP10OlnDAB1EP2rG\r\n10\r\n834gqTI\r\n_o4\r\nvU3o55h79n\r\nvLwlk3PY7Z\r\nk8PnkBpRYB\r\nqdkB9b_SS2\r\nkY4mBeAdgK\r\nb577cjQiSx\r\nyOFiEkP1sD\r\noYOqd8uuTt\r\n", "output": "35 64\r\n"}, {"input": "JroK3tfp941zeUovVIqCV7Sv2elf6TN8QRl8mhxQWgoQRXOLkcUjK29Ed2gBDR1TLdZeLUi9zJyBayrNlWgW0iPSG26DuJ9QK95\r\n10\r\nroK\r\novVI\r\nLUi9\r\nLUi\r\nTLd\r\nelf6\r\n9zJyBa\r\nDR1\r\ndZe\r\n8mhxQWgo\r\n", "output": "27 72\r\n"}, {"input": "p7B41t9y83IghcJG8zgWzCSATkkvQQhvqm7j_4ffUPbriGW57mbGPzqiuMEmJevfIhX2FNLFBxAGm3vLXRPOdphoWkCquDjAsW9\r\n10\r\n1t9y83\r\nBxAGm3v\r\nG8z\r\nCSATk\r\ncJG\r\nIhX2FNLF\r\nzqiuME\r\np7B4\r\nkkvQ\r\nPbriGW57m\r\n", "output": "26 73\r\n"}, {"input": "r5oW1NW2kr193KwV_eASuj9Jq0q0Kmt2EUD1R72p2nPuxNpIqi7o_KuxldYjLJmiXj93JUxRTniFzKkXxHwXDZCzC76klFnfjAQ\r\n10\r\np2nPuxN\r\n93J\r\nNpIqi7o_K\r\niXj93JUxRT\r\n1NW2kr19\r\n0Kmt2EUD1\r\nTniFzKkXxH\r\niXj93JU\r\nNW2kr19\r\n3KwV_\r\n", "output": "26 73\r\n"}, {"input": "hb\r\n1\r\nAa\r\n", "output": "2 0\r\n"}, {"input": "unagioisiiqqqqqqqqqqqq\r\n2\r\nioi\r\nunagi\r\n", "output": "17 5\r\n"}, {"input": "abababab\r\n3\r\nab\r\nba\r\na\r\n", "output": "1 7\r\n"}, {"input": "abcdaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n2\r\nabcd\r\nabcd\r\n", "output": "50 1\r\n"}, {"input": "abababababababababababababababababababababababababababababab\r\n1\r\na\r\n", "output": "1 59\r\n"}, {"input": "abc\r\n2\r\na\r\nb\r\n", "output": "1 2\r\n"}, {"input": "abcde\r\n1\r\nf\r\n", "output": "5 0\r\n"}, {"input": "ahmed\r\n5\r\nahmed\r\nhmed\r\nmed\r\ned\r\nd\r\n", "output": "4 0\r\n"}, {"input": "abc\r\n1\r\nb\r\n", "output": "1 2\r\n"}]
false
stdio
import sys def main(): input_path, output_path, submission_path = sys.argv[1:] # Read input with open(input_path, 'r') as f: s = f.readline().strip() n = int(f.readline()) forbidden = [f.readline().strip() for _ in range(n)] # Read submission output try: with open(submission_path, 'r') as f: line = f.readline().strip() parts = line.split() if len(parts) != 2: print(0) return len_sub = int(parts[0]) pos_sub = int(parts[1]) except: print(0) return # Check pos_sub and len_sub are valid if len_sub < 0 or pos_sub < 0: print(0) return s_len = len(s) if len_sub == 0: if pos_sub < 0 or pos_sub > s_len: print(0) return else: if pos_sub + len_sub > s_len: print(0) return # Check submission substring is valid valid = True sub_start = pos_sub sub_end = pos_sub + len_sub for i in range(sub_start, sub_end): for b in forbidden: m = len(b) if i + m > sub_end: continue if s[i:i+m] == b: valid = False break if not valid: break if not valid: print(0) return # Compute max_len using sliding window max_len = 0 left = 0 for right in range(len(s)): for b in forbidden: m = len(b) start = right - m + 1 if start < 0: continue if start + m > len(s): continue if s[start:start+m] == b: if left <= start: left = start + 1 current_len = right - left + 1 if current_len > max_len: max_len = current_len if len_sub != max_len: print(0) else: print(100) if __name__ == "__main__": main()
true
10/D
10
D
PyPy 3
TESTS
16
171
1,433,600
66735518
def get_lcis_length(X, Y): len_Y = len(Y) # maintain current lcis length # curr_lcis_len: len lcis(X, Y[0..i]) curr_lcis_len = [0] * len_Y for u in X: lcis_prefix_max = [] curr_max = 0 # copy current lcis length table and create prefix max from it # to make access in next step be O(1) for i, v in enumerate(Y): if v < u: curr_max = max(curr_max, curr_lcis_len[i]) lcis_prefix_max.append(curr_max) # loop through all elements in Y # update current lcis length if the element matches with current element in X for i, v in enumerate(Y): if u == v: curr_lcis_len[i] = max(curr_lcis_len[i], (0 if i == 0 else lcis_prefix_max[i - 1]) + 1) # find maximum lcis length max_lcis_length = curr_lcis_len[0] for i, u in enumerate(curr_lcis_len): max_lcis_length = max(max_lcis_length, u) # Backtrack to get the LCIS # Redo all previous step, but break when we found lcis that has the same length # with max_lcis_length # Need to break the loop to stop backtracking table (bt) being updated # No need to backtrack if there is no LCIS if max_lcis_length == 0: print(0) return max_lcis_length curr_lcis_len = [0] * len_Y bt = [-1] * len_Y # backtracking table for u in X: lcis_prefix_max = [] bt_id = [] curr_max = 0 curr_id_max = -1; # copy current lcis length table and create prefix max from it # to make access in next step be O(1) # but, we also keep where should we backtrack if we use the lcis_pref_max for i, v in enumerate(Y): if v < u: if curr_max < curr_lcis_len[i]: curr_max = curr_lcis_len[i] curr_id_max = i lcis_prefix_max.append(curr_max) bt_id.append(curr_id_max) # loop through all elements in Y # update current lcis length if the element matches with current element in X for i, v in enumerate(Y): if u == v: if curr_lcis_len[i] < lcis_prefix_max[i - 1] + 1: curr_lcis_len[i] = lcis_prefix_max[i - 1] + 1 bt[i] =bt_id[i - 1] # also update where should we backtrack if using this index # if this is one of the lcis, use the backtrack table to track the values if curr_lcis_len[i] == max_lcis_length: lcis_list = [] now = i while now != -1: lcis_list.append(Y[now]); now = bt[now] lcis_list.reverse() print(max_lcis_length) for i, u in enumerate(lcis_list): print(u, end=('\n' if i == max_lcis_length - 1 else ' ')) return max_lcis_length, lcis_list return max_lcis_length x = [] try: while True: y = input() x.append(y) except EOFError: pass x = ' '.join(x) x = x.split() x = [int(i) for i in x] X = [] Y = [] num_x = x[0] for i in range(1, num_x + 1): X.append(x[i]) for i in range(x[num_x + 1]): Y.append(x[num_x + 2 + i]) get_lcis_length(X, Y)
61
109
1,843,200
231955783
q = int(input()) a = [0] + list(map(int, input().split())) m = int(input()) res = 0 ans = [] d = [] prev = [] b = [0] + list(map(int, input().split())) for i in range(q + 1): x = [0] * (m + 1) d.append(x) del x prev.append(0) for i in range(1, q + 1): for j in range(1, m + 1): if a[i] == b[j]: d[i][j] = 1 for k in range(1, i): if a[k] < a[i] and d[i][j] < d[k][j] + 1: d[i][j] = d[k][j] + 1 prev[i] = k else: d[i][j] = d[i][j - 1] for i in range(1, q + 1): if d[res][m] < d[i][m]: res = i while res != 0: ans.append(a[res]) res = prev[res] print(len(ans)) print(*ans[::-1])
Codeforces Beta Round 10
ICPC
2,010
1
256
LCIS
This problem differs from one which was on the online contest. The sequence a1, a2, ..., an is called increasing, if ai < ai + 1 for i < n. The sequence s1, s2, ..., sk is called the subsequence of the sequence a1, a2, ..., an, if there exist such a set of indexes 1 ≤ i1 < i2 < ... < ik ≤ n that aij = sj. In other words, the sequence s can be derived from the sequence a by crossing out some elements. You are given two sequences of integer numbers. You are to find their longest common increasing subsequence, i.e. an increasing sequence of maximum length that is the subsequence of both sequences.
The first line contains an integer n (1 ≤ n ≤ 500) — the length of the first sequence. The second line contains n space-separated integers from the range [0, 109] — elements of the first sequence. The third line contains an integer m (1 ≤ m ≤ 500) — the length of the second sequence. The fourth line contains m space-separated integers from the range [0, 109] — elements of the second sequence.
In the first line output k — the length of the longest common increasing subsequence. In the second line output the subsequence itself. Separate the elements with a space. If there are several solutions, output any.
null
null
[{"input": "7\n2 3 1 6 5 4 6\n4\n1 3 5 6", "output": "3\n3 5 6"}, {"input": "5\n1 2 0 2 1\n3\n1 0 1", "output": "2\n0 1"}]
2,800
["dp"]
61
[{"input": "7\r\n2 3 1 6 5 4 6\r\n4\r\n1 3 5 6\r\n", "output": "3\r\n3 5 6 \r\n"}, {"input": "5\r\n1 2 0 2 1\r\n3\r\n1 0 1\r\n", "output": "2\r\n0 1 \r\n"}, {"input": "2\r\n6 10\r\n3\r\n6 3 3\r\n", "output": "1\r\n6 \r\n"}, {"input": "1\r\n7\r\n2\r\n7 9\r\n", "output": "1\r\n7 \r\n"}, {"input": "3\r\n37 49 24\r\n3\r\n33 5 70\r\n", "output": "0\r\n\r\n"}, {"input": "10\r\n7 10 1 2 1 7 1 5 9 9\r\n9\r\n6 2 5 6 7 7 5 5 2\r\n", "output": "2\r\n2 7 \r\n"}, {"input": "9\r\n7 0 1 2 6 0 10 3 5\r\n4\r\n8 4 0 3\r\n", "output": "2\r\n0 3 \r\n"}, {"input": "9\r\n7 4 4 5 0 6 5 4 10\r\n4\r\n5 2 10 9\r\n", "output": "2\r\n5 10 \r\n"}, {"input": "8\r\n7 8 6 6 8 10 3 3\r\n5\r\n7 4 10 8 7\r\n", "output": "2\r\n7 8 \r\n"}, {"input": "7\r\n4 2 4 3 10 3 6\r\n9\r\n7 5 2 3 0 1 6 1 4\r\n", "output": "3\r\n2 3 6 \r\n"}, {"input": "1\r\n7\r\n10\r\n1 8 8 10 9 10 4 6 0 5\r\n", "output": "0\r\n\r\n"}, {"input": "2\r\n5 2\r\n4\r\n8 8 0 4\r\n", "output": "0\r\n\r\n"}, {"input": "3\r\n1 3 9\r\n10\r\n8 0 10 5 7 0 3 1 2 4\r\n", "output": "1\r\n1 \r\n"}, {"input": "15\r\n10 4 7 7 10 1 4 9 1 10 9 6 8 8 2\r\n2\r\n0 4\r\n", "output": "1\r\n4 \r\n"}, {"input": "16\r\n8 8 6 7 10 0 10 1 7 6 6 0 4 2 6 7\r\n12\r\n9 3 8 4 10 3 9 8 3 7 10 4\r\n", "output": "2\r\n8 10 \r\n"}, {"input": "23\r\n174 172 196 135 91 174 208 92 132 53 202 118 5 244 161 140 71 21 185 56 60 195 217\r\n17\r\n38 218 120 77 22 214 164 194 79 195 36 167 42 89 201 80 11\r\n", "output": "1\r\n195 \r\n"}, {"input": "53\r\n135 168 160 123 6 250 251 158 245 184 206 35 189 64 138 12 69 21 112 198 165 211 109 40 192 98 236 216 255 98 136 38 67 79 25 196 216 64 134 124 102 232 229 102 179 138 111 123 2 93 25 162 57\r\n57\r\n64 143 41 144 73 26 11 17 224 209 167 162 129 39 102 224 254 45 120 2 138 213 139 133 169 54 7 143 242 118 155 189 100 185 145 168 248 131 83 216 142 180 225 35 226 202 8 15 200 192 75 140 191 189 75 116 202\r\n", "output": "3\r\n64 138 192 \r\n"}, {"input": "83\r\n95 164 123 111 177 71 38 225 103 59 210 209 117 139 115 140 66 21 39 84 14 227 0 43 90 233 96 98 232 237 108 139 55 220 14 225 134 39 68 167 193 125 86 216 87 14 94 75 255 24 165 98 177 191 239 123 98 90 29 52 155 231 187 90 180 1 31 237 167 145 242 115 61 190 47 41 61 206 191 248 126 196 26\r\n49\r\n234 134 9 207 37 95 116 239 105 197 191 15 151 249 156 235 17 161 197 199 87 78 191 188 44 151 179 238 72 29 228 157 174 99 190 114 95 185 160 168 58 216 131 151 233 204 213 87 76\r\n", "output": "2\r\n95 233 \r\n"}, {"input": "13\r\n55 160 86 99 92 148 81 36 216 191 214 127 44\r\n85\r\n92 12 64 21 221 225 119 243 147 47 244 112 212 237 209 121 81 239 43 104 3 254 52 13 1 210 28 18 199 75 251 146 77 28 253 211 50 35 42 160 157 104 155 37 241 78 42 190 150 228 193 96 190 178 232 65 231 186 1 123 212 126 239 22 214 186 245 249 66 234 57 78 173 229 185 23 240 91 127 177 240 105 77 208 86\r\n", "output": "2\r\n160 214 \r\n"}, {"input": "94\r\n100 161 99 102 209 51 5 188 217 53 121 5 233 55 25 156 136 195 243 157 110 202 136 151 86 171 253 38 126 40 27 76 60 119 222 52 134 104 184 146 133 220 88 108 246 61 215 184 181 134 223 164 41 193 232 217 38 192 226 91 81 99 204 232 178 4 187 61 160 255 121 142 191 114 114 181 226 49 86 55 252 169 59 190 246 93 21 22 17 18 120 88 93 144\r\n30\r\n61 51 176 38 119 33 100 185 103 84 161 166 103 227 43 200 127 53 52 89 19 215 76 254 110 30 239 247 11 182\r\n", "output": "3\r\n51 53 110 \r\n"}, {"input": "6\r\n3 5 4 6 8 1\r\n10\r\n3 3 0 5 4 0 10 5 6 8\r\n", "output": "4\r\n3 5 6 8 \r\n"}, {"input": "10\r\n0 1 2 3 4 5 6 7 8 9\r\n10\r\n0 1 2 3 4 5 6 7 8 9\r\n", "output": "10\r\n0 1 2 3 4 5 6 7 8 9 \r\n"}, {"input": "8\r\n2 3 4 5 6 8 9 5\r\n9\r\n2 2 3 4 5 6 6 8 9\r\n", "output": "7\r\n2 3 4 5 6 8 9 \r\n"}, {"input": "8\r\n0 4 10 6 7 2 8 5\r\n7\r\n0 4 6 7 7 0 8\r\n", "output": "5\r\n0 4 6 7 8 \r\n"}, {"input": "10\r\n0 1 2 3 4 5 6 7 8 9\r\n10\r\n0 1 2 3 4 5 6 7 8 9\r\n", "output": "10\r\n0 1 2 3 4 5 6 7 8 9 \r\n"}, {"input": "17\r\n12 17 39 156 100 177 188 129 14 142 45 144 243 151 158 194 245\r\n16\r\n125 12 17 199 65 39 100 185 129 194 142 144 62 92 158 194\r\n", "output": "9\r\n12 17 39 100 129 142 144 158 194 \r\n"}, {"input": "20\r\n7 17 24 27 36 45 62 92 93 94 98 112 114 138 143 156 173 199 204 207\r\n20\r\n7 17 24 27 36 45 62 92 93 94 98 112 114 138 143 156 173 199 204 207\r\n", "output": "20\r\n7 17 24 27 36 45 62 92 93 94 98 112 114 138 143 156 173 199 204 207 \r\n"}, {"input": "13\r\n0 46 104 116 63 118 158 16 221 222 136 245 223\r\n9\r\n0 46 104 116 118 158 221 222 245\r\n", "output": "9\r\n0 46 104 116 118 158 221 222 245 \r\n"}, {"input": "13\r\n34 38 51 57 73 125 147 158 160 178 188 198 235\r\n15\r\n34 38 51 57 73 125 147 158 160 178 255 67 188 198 235\r\n", "output": "13\r\n34 38 51 57 73 125 147 158 160 178 188 198 235 \r\n"}, {"input": "17\r\n25 29 37 207 122 189 118 42 54 95 154 160 162 225 228 237 248\r\n19\r\n25 29 248 37 147 209 42 54 255 95 154 160 162 225 228 237 73 248 10\r\n", "output": "13\r\n25 29 37 42 54 95 154 160 162 225 228 237 248 \r\n"}, {"input": "10\r\n62914292 123971042 784965687 324817892 379711365 394545872 813282270 822333477 865397146 437913515\r\n9\r\n297835672 62914292 123971042 324817892 379711365 394545872 813282270 822333477 865397146\r\n", "output": "8\r\n62914292 123971042 324817892 379711365 394545872 813282270 822333477 865397146 \r\n"}, {"input": "10\r\n130077811 57466561 335978192 71385678 434259735 454136111 482887469 530031703 688581885 809880630\r\n8\r\n373627898 57466561 71385678 434259735 454136111 482887469 530031703 809880630\r\n", "output": "7\r\n57466561 71385678 434259735 454136111 482887469 530031703 809880630 \r\n"}, {"input": "17\r\n364396044 90653796 82853043 311258337 326557522 362475139 415783272 428510002 840021181 469284863 541444887 650535473 649258258 750028895 791368777 808443140 959785237\r\n13\r\n82853043 311258337 326557522 362475139 415783272 428510002 469284863 541444887 649258258 750028895 791368777 808443140 959785237\r\n", "output": "13\r\n82853043 311258337 326557522 362475139 415783272 428510002 469284863 541444887 649258258 750028895 791368777 808443140 959785237 \r\n"}, {"input": "3\r\n6379263 55134355 76061584\r\n3\r\n6379263 55134355 76061584\r\n", "output": "3\r\n6379263 55134355 76061584 \r\n"}, {"input": "3\r\n48875076 71023491 76538219\r\n3\r\n48875076 71023491 76538219\r\n", "output": "3\r\n48875076 71023491 76538219 \r\n"}, {"input": "5\r\n6621317 78540394 52064998 89150480 53659440\r\n3\r\n78540394 46008538 839195\r\n", "output": "1\r\n78540394 \r\n"}, {"input": "2\r\n34665180 51128665\r\n5\r\n71074966 34665180 47089728 44119345 51128665\r\n", "output": "2\r\n34665180 51128665 \r\n"}, {"input": "4\r\n3 4 9 1\r\n7\r\n5 3 8 9 10 2 1\r\n", "output": "2\r\n3 9 \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, 'r') as f: input_lines = [line.strip() for line in f] n = int(input_lines[0]) a = list(map(int, input_lines[1].split())) m = int(input_lines[2]) b = list(map(int, input_lines[3].split())) # Read reference output to get k_ref with open(output_path, 'r') as f: ref_lines = [line.strip() for line in f if line.strip()] if not ref_lines: print(0) return k_ref = int(ref_lines[0]) # Read submission output with open(submission_path, 'r') as f: sub_lines = [line.strip() for line in f if line.strip()] if len(sub_lines) < 1: print(0) return try: k_sub = int(sub_lines[0]) except: print(0) return if k_sub != k_ref: print(0) return if len(sub_lines) < 2: s_sub = [] else: try: s_sub = list(map(int, sub_lines[1].split())) except: print(0) return if len(s_sub) != k_sub: print(0) return # Check strictly increasing for i in range(len(s_sub)-1): if s_sub[i] >= s_sub[i+1]: print(0) return # Check subsequence in a and b def is_sub(s, main): ptr = 0 l = len(s) if l ==0: return True for num in main: if num == s[ptr]: ptr +=1 if ptr == l: break return ptr == l if not is_sub(s_sub, a): print(0) return if not is_sub(s_sub, b): print(0) return # All checks passed print(1) if __name__ == "__main__": main()
true
439/C
439
C
PyPy 3-64
TESTS
9
77
13,004,800
192856992
import heapq import sys from types import GeneratorType from functools import cmp_to_key from collections import defaultdict, Counter, deque import math inf = float("inf") 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 def main(ac=FastIO()): n, k, p = ac.read_ints() nums = ac.read_list_ints() odd = [] even = [] for num in nums: if num % 2: odd.append(num) # 奇数 else: even.append(num) # p个偶数 k-p个奇数 a, b = len(odd), len(even) if a < k-p or (a-(k-p)) % 2: # 奇数不够或者奇数多余 ac.st("NO") return ac.st("YES") if p == 0: ans = [[] for _ in range(k)] for j in range(k): ans.append([odd[j]]) ans[-1].extend(even) ans[-1].extend(odd[k:]) return # 偶数不够 if b + (a-(k-p))//2 < p: ac.st("NO") return ans = [[] for _ in range(p)] for i in range(b): ans[(i%p)].append(even[i]) for j in range(k-p): ans.append([odd[j]]) i = b for j in range(k-p, a, 2): ans[i%p].extend([odd[j], odd[j+1]]) i += 1 for a in ans: ac.lst([len(a)]+a) return main()
75
374
35,737,600
123261875
n, k , p = map(int, input().split()) a = list(map(int, input().split())) odd = [] even = [] for el in a: if el & 1: odd.append(el) else: even.append(el) if (len(odd) - (k-p)) % 2: print("NO") exit() ans = [[] for _ in range(k)] for i in range(k-p): if odd: ans[i].append(odd.pop()) else: print("NO") exit() for i in range(k-p, k): if even: ans[i].append(even.pop()) elif len(odd) >= 2: ans[i].append(odd.pop()) ans[i].append(odd.pop()) else: print("NO") exit() ans[0] += even ans[0] += odd print("YES") for a in ans: print(len(a), " ".join(map(str, a)))
Codeforces Round 251 (Div. 2)
CF
2,014
1
256
Devu and Partitioning of the Array
Devu being a small kid, likes to play a lot, but he only likes to play with arrays. While playing he came up with an interesting question which he could not solve, can you please solve it for him? Given an array consisting of distinct integers. Is it possible to partition the whole array into k disjoint non-empty parts such that p of the parts have even sum (each of them must have even sum) and remaining k - p have odd sum? (note that parts need not to be continuous). If it is possible to partition the array, also give any possible way of valid partitioning.
The first line will contain three space separated integers n, k, p (1 ≤ k ≤ n ≤ 105; 0 ≤ p ≤ k). The next line will contain n space-separated distinct integers representing the content of array a: a1, a2, ..., an (1 ≤ ai ≤ 109).
In the first line print "YES" (without the quotes) if it is possible to partition the array in the required way. Otherwise print "NO" (without the quotes). If the required partition exists, print k lines after the first line. The ith of them should contain the content of the ith part. Print the content of the part in the line in the following way: firstly print the number of elements of the part, then print all the elements of the part in arbitrary order. There must be exactly p parts with even sum, each of the remaining k - p parts must have odd sum. As there can be multiple partitions, you are allowed to print any valid partition.
null
null
[{"input": "5 5 3\n2 6 10 5 9", "output": "YES\n1 9\n1 5\n1 10\n1 6\n1 2"}, {"input": "5 5 3\n7 14 2 9 5", "output": "NO"}, {"input": "5 3 1\n1 2 3 7 5", "output": "YES\n3 5 1 3\n1 7\n1 2"}]
1,700
["brute force", "constructive algorithms", "implementation", "number theory"]
75
[{"input": "5 5 3\r\n2 6 10 5 9\r\n", "output": "YES\r\n1 9\r\n1 5\r\n1 10\r\n1 6\r\n1 2\r\n"}, {"input": "5 5 3\r\n7 14 2 9 5\r\n", "output": "NO\r\n"}, {"input": "5 3 1\r\n1 2 3 7 5\r\n", "output": "YES\r\n3 5 1 3\r\n1 7\r\n1 2\r\n"}, {"input": "10 5 3\r\n194757070 828985446 11164 80016 84729 117765558 111730436 164044532 419732980 48834\r\n", "output": "NO\r\n"}, {"input": "10 6 3\r\n861947514 34945 190135645 68731 44833 387988147 84308862 878151920 458358978 809653252\r\n", "output": "YES\r\n5 387988147 861947514 84308862 34945 190135645\r\n1 44833\r\n1 68731\r\n1 809653252\r\n1 458358978\r\n1 878151920\r\n"}, {"input": "10 8 3\r\n677037706 41099140 89128206 168458947 367939060 940344093 191391519 981938946 31319 34929915\r\n", "output": "YES\r\n3 34929915 677037706 41099140\r\n1 31319\r\n1 191391519\r\n1 940344093\r\n1 168458947\r\n1 981938946\r\n1 367939060\r\n1 89128206\r\n"}, {"input": "10 8 4\r\n214605891 246349108 626595204 63889 794527783 83684156 5535 865709352 262484651 157889982\r\n", "output": "NO\r\n"}, {"input": "10 6 3\r\n223143676 316703192 5286 408323576 61758 566101388 9871840 93989 91890 99264208\r\n", "output": "NO\r\n"}, {"input": "2 2 1\r\n409447178 258805801\r\n", "output": "YES\r\n1 258805801\r\n1 409447178\r\n"}, {"input": "2 1 1\r\n29161 15829\r\n", "output": "YES\r\n2 15829 29161\r\n"}, {"input": "3 3 1\r\n357071129 476170324 503481367\r\n", "output": "YES\r\n1 503481367\r\n1 357071129\r\n1 476170324\r\n"}, {"input": "3 3 1\r\n357071129 476170324 503481367\r\n", "output": "YES\r\n1 503481367\r\n1 357071129\r\n1 476170324\r\n"}, {"input": "2 1 1\r\n29161 15829\r\n", "output": "YES\r\n2 15829 29161\r\n"}, {"input": "23 22 3\r\n777717359 54451 123871650 211256633 193354035 935466677 800874233 532974165 62256 6511 3229 757421727 371493777 268999168 82355 22967 678259053 886134047 207070129 122416584 79851 35753 730872007\r\n", "output": "YES\r\n2 730872007 123871650\r\n1 35753\r\n1 79851\r\n1 207070129\r\n1 886134047\r\n1 678259053\r\n1 22967\r\n1 82355\r\n1 371493777\r\n1 757421727\r\n1 3229\r\n1 6511\r\n1 532974165\r\n1 800874233\r\n1 935466677\r\n1 193354035\r\n1 211256633\r\n1 54451\r\n1 777717359\r\n1 122416584\r\n1 268999168\r\n1 62256\r\n"}, {"input": "16 9 9\r\n826588597 70843 528358243 60844 636968393 862405463 58232 69241 617006886 903909155 973050249 9381 49031 40100022 62141 43805\r\n", "output": "YES\r\n3 40100022 826588597 70843\r\n1 617006886\r\n1 58232\r\n1 60844\r\n2 43805 62141\r\n2 49031 9381\r\n2 973050249 903909155\r\n2 69241 862405463\r\n2 636968393 528358243\r\n"}, {"input": "5 2 2\r\n316313049 24390603 595539594 514135024 39108\r\n", "output": "YES\r\n4 39108 595539594 316313049 24390603\r\n1 514135024\r\n"}, {"input": "5 2 1\r\n12474 117513621 32958 767146609 20843\r\n", "output": "YES\r\n4 20843 12474 117513621 767146609\r\n1 32958\r\n"}, {"input": "5 4 1\r\n387119493 716009972 88510 375210205 910834347\r\n", "output": "YES\r\n2 910834347 716009972\r\n1 375210205\r\n1 387119493\r\n1 88510\r\n"}, {"input": "5 4 3\r\n674318396 881407702 882396010 208141498 53145\r\n", "output": "YES\r\n2 53145 674318396\r\n1 208141498\r\n1 882396010\r\n1 881407702\r\n"}, {"input": "3 2 1\r\n976825506 613159225 249024714\r\n", "output": "YES\r\n2 613159225 976825506\r\n1 249024714\r\n"}, {"input": "4 1 1\r\n173508914 11188 90766233 20363\r\n", "output": "YES\r\n4 11188 173508914 90766233 20363\r\n"}, {"input": "30 24 12\r\n459253071 24156 64054 270713791 734796619 379920883 429646748 332144982 20929 27685 53253 82047732 172442017 34241 880121331 890223629 974692 954036632 68638567 972921811 421106382 64615 819330514 179630214 608594496 802986133 231010377 184513476 73143 93045\r\n", "output": "YES\r\n7 93045 459253071 270713791 734796619 379920883 20929 27685\r\n1 73143\r\n1 231010377\r\n1 802986133\r\n1 64615\r\n1 972921811\r\n1 68638567\r\n1 890223629\r\n1 880121331\r\n1 34241\r\n1 172442017\r\n1 53253\r\n1 184513476\r\n1 608594496\r\n1 179630214\r\n1 819330514\r\n1 421106382\r\n1 954036632\r\n1 974692\r\n1 82047732\r\n1 332144982\r\n1 429646748\r\n1 64054\r\n1 24156\r\n"}, {"input": "9 5 1\r\n91623 466261329 311727429 18189 42557 22943 66177 473271749 62622\r\n", "output": "YES\r\n5 473271749 91623 466261329 311727429 18189\r\n1 66177\r\n1 22943\r\n1 42557\r\n1 62622\r\n"}, {"input": "4 1 1\r\n266639563 36517 172287193 166673809\r\n", "output": "YES\r\n4 166673809 172287193 266639563 36517\r\n"}, {"input": "5 2 2\r\n19571 180100775 421217758 284511211 49475\r\n", "output": "YES\r\n3 421217758 19571 180100775\r\n2 49475 284511211\r\n"}, {"input": "4 2 2\r\n736788713 82230 66800 37791827\r\n", "output": "YES\r\n3 66800 736788713 37791827\r\n1 82230\r\n"}, {"input": "5 1 1\r\n33889 469945850 40673 939970023 776172319\r\n", "output": "YES\r\n5 469945850 33889 40673 939970023 776172319\r\n"}, {"input": "1 1 0\r\n2\r\n", "output": "NO\r\n"}, {"input": "1 1 0\r\n3\r\n", "output": "YES\r\n1 3\r\n"}, {"input": "1 1 1\r\n2\r\n", "output": "YES\r\n1 2\r\n"}, {"input": "1 1 1\r\n3\r\n", "output": "NO\r\n"}, {"input": "2 2 2\r\n2 3\r\n", "output": "NO\r\n"}, {"input": "2 2 2\r\n2 4\r\n", "output": "YES\r\n1 4\r\n1 2\r\n"}, {"input": "2 2 1\r\n3 2\r\n", "output": "YES\r\n1 3\r\n1 2\r\n"}, {"input": "4 2 0\r\n3 5 7 9\r\n", "output": "YES\r\n3 9 3 5\r\n1 7\r\n"}, {"input": "3 2 0\r\n1 3 2\r\n", "output": "YES\r\n2 3 2\r\n1 1\r\n"}, {"input": "2 1 1\r\n2 4\r\n", "output": "YES\r\n2 4 2\r\n"}, {"input": "7 3 0\r\n1 3 5 7 9 11 13\r\n", "output": "YES\r\n5 13 1 3 5 7\r\n1 11\r\n1 9\r\n"}, {"input": "8 4 4\r\n1 3 5 7 9 11 13 15\r\n", "output": "YES\r\n2 15 13\r\n2 11 9\r\n2 7 5\r\n2 3 1\r\n"}, {"input": "2 1 1\r\n1 3\r\n", "output": "YES\r\n2 3 1\r\n"}]
false
stdio
import sys def main(input_path, output_path, submission_path): with open(input_path) as f: n, k, p = map(int, f.readline().split()) a = list(map(int, f.readline().split())) with open(output_path) as f: ref_line = f.readline().strip() correct_answer = ref_line with open(submission_path) as f: lines = [line.strip() for line in f.readlines() if line.strip()] if not lines: print(0) return submission_answer = lines[0] if submission_answer != correct_answer: print(0) return if correct_answer == "NO": if len(lines) != 1: print(0) else: print(1) return else: if len(lines) != k + 1: print(0) return parts_lines = lines[1:] elements_submission = [] sum_counts = 0 even_count = 0 valid = True for line in parts_lines: parts = line.split() if len(parts) < 1: valid = False break if not parts[0].isdigit(): valid = False break count = int(parts[0]) if count < 1: valid = False break sum_counts += count if len(parts) != count + 1: valid = False break try: elements = list(map(int, parts[1:])) except: valid = False break elements_submission.extend(elements) sum_part = sum(elements) if sum_part % 2 == 0: even_count += 1 if not valid or sum_counts != n or set(elements_submission) != set(a) or even_count != p: print(0) else: print(1) if __name__ == "__main__": input_path, output_path, submission_path = sys.argv[1], sys.argv[2], sys.argv[3] main(input_path, output_path, submission_path)
true
439/C
439
C
PyPy 3
TESTS
57
358
34,304,000
116167034
n,k,p = map(int,input().split()) arr = [int(x) for x in input().split()] odd = [] even = [] for i in arr: if(i&1): odd.append(i) else: even.append(i) oneed = k - p eneed = p # Special case for eneed = 0 if(oneed > len(odd)): print("NO") else: # First Method (Give min to odd and then to even) ans = [] done = True fromOdd = len(odd) - oneed if(fromOdd&1): # No way done = False pass elif(fromOdd/2 + len(even)<eneed): # No way done = False pass else: # Done for i in range(oneed): ans.append([odd[-1]]) del odd[-1] while(eneed>1): if(len(odd)>=2): ans.append([odd[-1],odd[-2]]) del odd[-1] del odd[-1] else: ans.append([even[-1]]) del even[-1] eneed -=1 if(odd): even.extend(odd) ans.append(even) if(not done): print("NO") else: print("YES") for i in ans: print(len(i), *i)
75
389
16,384,000
105014130
def solve(): n,k,p=map(int,input().split()) a=list(map(int,input().split())) even=list(filter(lambda x: x%2==0, a)) odd=list(filter(lambda x:x%2==1, a)) if (len(odd)-(k-p))%2!=0: print("NO"); return ans=[[] for _ in range(k)] for i in range(k-p): if odd: ans[i].append(odd.pop()) else: print("NO") return for i in range(k-p,k): if even: ans[i].append(even.pop()) elif len(odd)>=2: ans[i].append(odd.pop()) ans[i].append(odd.pop()) else: print("NO") return ans[0]+=even ans[0]+=odd print("YES") for part in ans: print(len(part)," ".join(map(str,part))) solve()
Codeforces Round 251 (Div. 2)
CF
2,014
1
256
Devu and Partitioning of the Array
Devu being a small kid, likes to play a lot, but he only likes to play with arrays. While playing he came up with an interesting question which he could not solve, can you please solve it for him? Given an array consisting of distinct integers. Is it possible to partition the whole array into k disjoint non-empty parts such that p of the parts have even sum (each of them must have even sum) and remaining k - p have odd sum? (note that parts need not to be continuous). If it is possible to partition the array, also give any possible way of valid partitioning.
The first line will contain three space separated integers n, k, p (1 ≤ k ≤ n ≤ 105; 0 ≤ p ≤ k). The next line will contain n space-separated distinct integers representing the content of array a: a1, a2, ..., an (1 ≤ ai ≤ 109).
In the first line print "YES" (without the quotes) if it is possible to partition the array in the required way. Otherwise print "NO" (without the quotes). If the required partition exists, print k lines after the first line. The ith of them should contain the content of the ith part. Print the content of the part in the line in the following way: firstly print the number of elements of the part, then print all the elements of the part in arbitrary order. There must be exactly p parts with even sum, each of the remaining k - p parts must have odd sum. As there can be multiple partitions, you are allowed to print any valid partition.
null
null
[{"input": "5 5 3\n2 6 10 5 9", "output": "YES\n1 9\n1 5\n1 10\n1 6\n1 2"}, {"input": "5 5 3\n7 14 2 9 5", "output": "NO"}, {"input": "5 3 1\n1 2 3 7 5", "output": "YES\n3 5 1 3\n1 7\n1 2"}]
1,700
["brute force", "constructive algorithms", "implementation", "number theory"]
75
[{"input": "5 5 3\r\n2 6 10 5 9\r\n", "output": "YES\r\n1 9\r\n1 5\r\n1 10\r\n1 6\r\n1 2\r\n"}, {"input": "5 5 3\r\n7 14 2 9 5\r\n", "output": "NO\r\n"}, {"input": "5 3 1\r\n1 2 3 7 5\r\n", "output": "YES\r\n3 5 1 3\r\n1 7\r\n1 2\r\n"}, {"input": "10 5 3\r\n194757070 828985446 11164 80016 84729 117765558 111730436 164044532 419732980 48834\r\n", "output": "NO\r\n"}, {"input": "10 6 3\r\n861947514 34945 190135645 68731 44833 387988147 84308862 878151920 458358978 809653252\r\n", "output": "YES\r\n5 387988147 861947514 84308862 34945 190135645\r\n1 44833\r\n1 68731\r\n1 809653252\r\n1 458358978\r\n1 878151920\r\n"}, {"input": "10 8 3\r\n677037706 41099140 89128206 168458947 367939060 940344093 191391519 981938946 31319 34929915\r\n", "output": "YES\r\n3 34929915 677037706 41099140\r\n1 31319\r\n1 191391519\r\n1 940344093\r\n1 168458947\r\n1 981938946\r\n1 367939060\r\n1 89128206\r\n"}, {"input": "10 8 4\r\n214605891 246349108 626595204 63889 794527783 83684156 5535 865709352 262484651 157889982\r\n", "output": "NO\r\n"}, {"input": "10 6 3\r\n223143676 316703192 5286 408323576 61758 566101388 9871840 93989 91890 99264208\r\n", "output": "NO\r\n"}, {"input": "2 2 1\r\n409447178 258805801\r\n", "output": "YES\r\n1 258805801\r\n1 409447178\r\n"}, {"input": "2 1 1\r\n29161 15829\r\n", "output": "YES\r\n2 15829 29161\r\n"}, {"input": "3 3 1\r\n357071129 476170324 503481367\r\n", "output": "YES\r\n1 503481367\r\n1 357071129\r\n1 476170324\r\n"}, {"input": "3 3 1\r\n357071129 476170324 503481367\r\n", "output": "YES\r\n1 503481367\r\n1 357071129\r\n1 476170324\r\n"}, {"input": "2 1 1\r\n29161 15829\r\n", "output": "YES\r\n2 15829 29161\r\n"}, {"input": "23 22 3\r\n777717359 54451 123871650 211256633 193354035 935466677 800874233 532974165 62256 6511 3229 757421727 371493777 268999168 82355 22967 678259053 886134047 207070129 122416584 79851 35753 730872007\r\n", "output": "YES\r\n2 730872007 123871650\r\n1 35753\r\n1 79851\r\n1 207070129\r\n1 886134047\r\n1 678259053\r\n1 22967\r\n1 82355\r\n1 371493777\r\n1 757421727\r\n1 3229\r\n1 6511\r\n1 532974165\r\n1 800874233\r\n1 935466677\r\n1 193354035\r\n1 211256633\r\n1 54451\r\n1 777717359\r\n1 122416584\r\n1 268999168\r\n1 62256\r\n"}, {"input": "16 9 9\r\n826588597 70843 528358243 60844 636968393 862405463 58232 69241 617006886 903909155 973050249 9381 49031 40100022 62141 43805\r\n", "output": "YES\r\n3 40100022 826588597 70843\r\n1 617006886\r\n1 58232\r\n1 60844\r\n2 43805 62141\r\n2 49031 9381\r\n2 973050249 903909155\r\n2 69241 862405463\r\n2 636968393 528358243\r\n"}, {"input": "5 2 2\r\n316313049 24390603 595539594 514135024 39108\r\n", "output": "YES\r\n4 39108 595539594 316313049 24390603\r\n1 514135024\r\n"}, {"input": "5 2 1\r\n12474 117513621 32958 767146609 20843\r\n", "output": "YES\r\n4 20843 12474 117513621 767146609\r\n1 32958\r\n"}, {"input": "5 4 1\r\n387119493 716009972 88510 375210205 910834347\r\n", "output": "YES\r\n2 910834347 716009972\r\n1 375210205\r\n1 387119493\r\n1 88510\r\n"}, {"input": "5 4 3\r\n674318396 881407702 882396010 208141498 53145\r\n", "output": "YES\r\n2 53145 674318396\r\n1 208141498\r\n1 882396010\r\n1 881407702\r\n"}, {"input": "3 2 1\r\n976825506 613159225 249024714\r\n", "output": "YES\r\n2 613159225 976825506\r\n1 249024714\r\n"}, {"input": "4 1 1\r\n173508914 11188 90766233 20363\r\n", "output": "YES\r\n4 11188 173508914 90766233 20363\r\n"}, {"input": "30 24 12\r\n459253071 24156 64054 270713791 734796619 379920883 429646748 332144982 20929 27685 53253 82047732 172442017 34241 880121331 890223629 974692 954036632 68638567 972921811 421106382 64615 819330514 179630214 608594496 802986133 231010377 184513476 73143 93045\r\n", "output": "YES\r\n7 93045 459253071 270713791 734796619 379920883 20929 27685\r\n1 73143\r\n1 231010377\r\n1 802986133\r\n1 64615\r\n1 972921811\r\n1 68638567\r\n1 890223629\r\n1 880121331\r\n1 34241\r\n1 172442017\r\n1 53253\r\n1 184513476\r\n1 608594496\r\n1 179630214\r\n1 819330514\r\n1 421106382\r\n1 954036632\r\n1 974692\r\n1 82047732\r\n1 332144982\r\n1 429646748\r\n1 64054\r\n1 24156\r\n"}, {"input": "9 5 1\r\n91623 466261329 311727429 18189 42557 22943 66177 473271749 62622\r\n", "output": "YES\r\n5 473271749 91623 466261329 311727429 18189\r\n1 66177\r\n1 22943\r\n1 42557\r\n1 62622\r\n"}, {"input": "4 1 1\r\n266639563 36517 172287193 166673809\r\n", "output": "YES\r\n4 166673809 172287193 266639563 36517\r\n"}, {"input": "5 2 2\r\n19571 180100775 421217758 284511211 49475\r\n", "output": "YES\r\n3 421217758 19571 180100775\r\n2 49475 284511211\r\n"}, {"input": "4 2 2\r\n736788713 82230 66800 37791827\r\n", "output": "YES\r\n3 66800 736788713 37791827\r\n1 82230\r\n"}, {"input": "5 1 1\r\n33889 469945850 40673 939970023 776172319\r\n", "output": "YES\r\n5 469945850 33889 40673 939970023 776172319\r\n"}, {"input": "1 1 0\r\n2\r\n", "output": "NO\r\n"}, {"input": "1 1 0\r\n3\r\n", "output": "YES\r\n1 3\r\n"}, {"input": "1 1 1\r\n2\r\n", "output": "YES\r\n1 2\r\n"}, {"input": "1 1 1\r\n3\r\n", "output": "NO\r\n"}, {"input": "2 2 2\r\n2 3\r\n", "output": "NO\r\n"}, {"input": "2 2 2\r\n2 4\r\n", "output": "YES\r\n1 4\r\n1 2\r\n"}, {"input": "2 2 1\r\n3 2\r\n", "output": "YES\r\n1 3\r\n1 2\r\n"}, {"input": "4 2 0\r\n3 5 7 9\r\n", "output": "YES\r\n3 9 3 5\r\n1 7\r\n"}, {"input": "3 2 0\r\n1 3 2\r\n", "output": "YES\r\n2 3 2\r\n1 1\r\n"}, {"input": "2 1 1\r\n2 4\r\n", "output": "YES\r\n2 4 2\r\n"}, {"input": "7 3 0\r\n1 3 5 7 9 11 13\r\n", "output": "YES\r\n5 13 1 3 5 7\r\n1 11\r\n1 9\r\n"}, {"input": "8 4 4\r\n1 3 5 7 9 11 13 15\r\n", "output": "YES\r\n2 15 13\r\n2 11 9\r\n2 7 5\r\n2 3 1\r\n"}, {"input": "2 1 1\r\n1 3\r\n", "output": "YES\r\n2 3 1\r\n"}]
false
stdio
import sys def main(input_path, output_path, submission_path): with open(input_path) as f: n, k, p = map(int, f.readline().split()) a = list(map(int, f.readline().split())) with open(output_path) as f: ref_line = f.readline().strip() correct_answer = ref_line with open(submission_path) as f: lines = [line.strip() for line in f.readlines() if line.strip()] if not lines: print(0) return submission_answer = lines[0] if submission_answer != correct_answer: print(0) return if correct_answer == "NO": if len(lines) != 1: print(0) else: print(1) return else: if len(lines) != k + 1: print(0) return parts_lines = lines[1:] elements_submission = [] sum_counts = 0 even_count = 0 valid = True for line in parts_lines: parts = line.split() if len(parts) < 1: valid = False break if not parts[0].isdigit(): valid = False break count = int(parts[0]) if count < 1: valid = False break sum_counts += count if len(parts) != count + 1: valid = False break try: elements = list(map(int, parts[1:])) except: valid = False break elements_submission.extend(elements) sum_part = sum(elements) if sum_part % 2 == 0: even_count += 1 if not valid or sum_counts != n or set(elements_submission) != set(a) or even_count != p: print(0) else: print(1) if __name__ == "__main__": input_path, output_path, submission_path = sys.argv[1], sys.argv[2], sys.argv[3] main(input_path, output_path, submission_path)
true
382/C
382
C
Python 3
TESTS
21
109
307,200
51097738
n = int(input()) l = list(map(int,input().split())) l = sorted(l) if n == 1 : print(-1) exit() if (n == 2) : if (l[1] - l[0]) % 2 == 0 : if l[1] - l[0] == 0 : print(1) print(l[0]) exit() print(3) print(l[0] - (l[1] - l[0]),l[0] + (l[1] - l[0]) // 2,l[1] + (l[1] - l[0])) else : print(2) print(l[0] - (l[1] - l[0]),l[1] + (l[1] - l[0])) exit() q=set() for i in range(1,n) : q.add(l[i] - l[i - 1]) q = list(q) q = sorted(q) if len(q) == 1 : if q[0] == 0 : print(1) print(l[0]) else : print(2) print(l[0] - q[0],l[-1] + q[0]) else : if q[0] * 2 == q[1] and len(q) == 2: print(1) for i in range(1,n) : if l[i] - l[i - 1] == q[1] : if l[i] - q[0] in l : print(0) exit() print(l[i] - q[0]) exit() else : print(0)
59
233
11,571,200
78325665
import sys from math import log2,floor,ceil,sqrt,gcd # import bisect # from collections import deque # sys.setrecursionlimit(7*10**4) Ri = lambda : [int(x) for x in sys.stdin.readline().split()] ri = lambda : sys.stdin.readline().strip() def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') INF = 10 ** 18 MOD = 1000000007 n = int(ri()) a = Ri() a.sort() if n == 1: print(-1) elif n == 2: cdd = a[1]-a[0] if(a[0]+a[1])%2 ==0: if cdd != 0: print(3) print(a[0]-cdd,(a[0]+a[1])//2,a[1]+cdd) else: print(1) print(a[0]) else: print(2) print(a[0]-cdd,a[1]+cdd) else: cd = [] for i in range(1,n): cd.append(a[i]-a[i-1]) dic = {} cnt = 0 for i in range(len(cd)): if cd[i] in dic: dic[cd[i]]+=1 else: cnt+=1 dic[cd[i]] = 1 if len(dic) > 2 : print(0) elif len(dic) == 1: key = list(dic.keys())[0] # print("fsf") if key == 0: print(1) print(a[0]) else: print(2) print(a[0]-key,a[-1]+key) else: lis= list(dic.keys()) dif = -1 oth = -1 if dic[lis[0]] > dic[lis[1]]: dif = lis[1] oth = lis[0] else: dif = lis[0] oth = lis[1] if dic[dif] > 1: print(0) else: # print(cd) flag = -1 if n == 3: if dif/2 != oth and oth/2 != dif: print(0) elif dif/2 != oth : if cd[0] == oth: print(1) print((a[0]+a[1])//2) else: print(1) print((a[1]+a[2])//2) elif oth/2 != dif: if cd[0] == dif: print(1) print((a[0]+a[1])//2) else: print(1) print((a[1]+a[2])//2) else: for i in range(len(cd)): if cd[i] == dif: flag = i break if dif%2 != 0 or dif//2 != oth: print(0) else: print(1) print((a[flag]+a[flag+1])//2)
Codeforces Round 224 (Div. 2)
CF
2,014
1
256
Arithmetic Progression
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a1, a2, ..., an of length n, that the following condition fulfills: a2 - a1 = a3 - a2 = a4 - a3 = ... = ai + 1 - ai = ... = an - an - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled.
The first line contains integer n (1 ≤ n ≤ 105) — the number of cards. The next line contains the sequence of integers — the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 108.
If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 108 or even be negative (see test samples).
null
null
[{"input": "3\n4 1 7", "output": "2\n-2 10"}, {"input": "1\n10", "output": "-1"}, {"input": "4\n1 3 5 9", "output": "1\n7"}, {"input": "4\n4 3 4 5", "output": "0"}, {"input": "2\n2 4", "output": "3\n0 3 6"}]
1,700
["implementation", "sortings"]
59
[{"input": "3\r\n4 1 7\r\n", "output": "2\r\n-2 10\r\n"}, {"input": "1\r\n10\r\n", "output": "-1\r\n"}, {"input": "4\r\n1 3 5 9\r\n", "output": "1\r\n7\r\n"}, {"input": "4\r\n4 3 4 5\r\n", "output": "0\r\n"}, {"input": "2\r\n2 4\r\n", "output": "3\r\n0 3 6\r\n"}, {"input": "4\r\n1 3 4 5\r\n", "output": "1\r\n2\r\n"}, {"input": "2\r\n3 3\r\n", "output": "1\r\n3\r\n"}, {"input": "2\r\n13 2\r\n", "output": "2\r\n-9 24\r\n"}, {"input": "5\r\n2 2 2 2 2\r\n", "output": "1\r\n2\r\n"}, {"input": "6\r\n11 1 7 9 5 13\r\n", "output": "1\r\n3\r\n"}, {"input": "2\r\n100000000 1\r\n", "output": "2\r\n-99999998 199999999\r\n"}, {"input": "5\r\n2 3 1 4 6\r\n", "output": "1\r\n5\r\n"}, {"input": "5\r\n1 2 2 3 4\r\n", "output": "0\r\n"}, {"input": "3\r\n1 4 2\r\n", "output": "1\r\n3\r\n"}, {"input": "3\r\n8 8 8\r\n", "output": "1\r\n8\r\n"}, {"input": "5\r\n2 2 2 2 3\r\n", "output": "0\r\n"}, {"input": "1\r\n100000000\r\n", "output": "-1\r\n"}, {"input": "20\r\n27 6 3 18 54 33 9 15 39 12 57 48 21 51 60 30 24 36 42 45\r\n", "output": "2\r\n0 63\r\n"}, {"input": "40\r\n100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000\r\n", "output": "1\r\n100000000\r\n"}, {"input": "49\r\n81787 163451 104059 89211 96635 133755 148603 141179 159739 122619 123 144891 70651 11259 63227 3835 44667 37243 100347 26107 137467 18683 156027 59515 22395 40955 111483 52091 7547 85499 107771 178299 115195 152315 74363 126331 33531 130043 14971 48379 167163 182011 170875 78075 174587 55803 66939 29819 118907\r\n", "output": "1\r\n92923\r\n"}, {"input": "9\r\n1 2 3 3 4 4 5 5 6\r\n", "output": "0\r\n"}, {"input": "7\r\n1 1 2 3 4 5 6\r\n", "output": "0\r\n"}, {"input": "2\r\n4 1\r\n", "output": "2\r\n-2 7\r\n"}, {"input": "2\r\n2 100000000\r\n", "output": "3\r\n-99999996 50000001 199999998\r\n"}, {"input": "8\r\n1 2 3 4 11 12 13 14\r\n", "output": "0\r\n"}, {"input": "7\r\n5 40 45 50 55 60 65\r\n", "output": "0\r\n"}, {"input": "1\r\n1\r\n", "output": "-1\r\n"}, {"input": "2\r\n1 1\r\n", "output": "1\r\n1\r\n"}, {"input": "2\r\n100000000 2\r\n", "output": "3\r\n-99999996 50000001 199999998\r\n"}, {"input": "3\r\n2 2 3\r\n", "output": "0\r\n"}, {"input": "5\r\n1 3 5 9 13\r\n", "output": "0\r\n"}, {"input": "5\r\n1 2 4 8 16\r\n", "output": "0\r\n"}, {"input": "3\r\n2 2 5\r\n", "output": "0\r\n"}, {"input": "5\r\n1 2 3 4 8\r\n", "output": "0\r\n"}, {"input": "3\r\n1 3 4\r\n", "output": "1\r\n2\r\n"}, {"input": "5\r\n1 2 4 6 7\r\n", "output": "0\r\n"}, {"input": "4\r\n1 5 9 11\r\n", "output": "0\r\n"}, {"input": "4\r\n3 4 5 9\r\n", "output": "0\r\n"}, {"input": "4\r\n1 5 6 8\r\n", "output": "0\r\n"}, {"input": "4\r\n2 6 8 12\r\n", "output": "0\r\n"}, {"input": "5\r\n1 2 3 5 7\r\n", "output": "0\r\n"}, {"input": "6\r\n1 2 3 4 6 8\r\n", "output": "0\r\n"}]
false
stdio
null
true
382/C
382
C
PyPy 3
TESTS
21
156
0
72305917
n=int(input()) a=input().split() for i in range(n): a[i]=int(a[i]) a.sort() if(n==1):print(-1) elif(n==2): if(a[0]==a[1]): print(1) print(a[0]) elif((a[1]-a[0])%2==0): print(3) print(a[0]-(a[1]-a[0]),(a[0]+a[1])//2,a[1]+(a[1]-a[0])) else: print(2) print(a[0]-(a[1]-a[0]),a[1]+(a[1]-a[0])) else: dic={} for i in range(1,n): if(a[i]-a[i-1] in dic.keys()):dic[a[i]-a[i-1]]+=1 else:dic[a[i]-a[i-1]]=1 if(len(dic.keys())==1): if(a[0]==a[n-1]): print(1) print(a[0]) else: print(2) print(a[0]-(a[1]-a[0]),a[n-1]+(a[1]-a[0])) elif(len(dic.keys())==2): ls=[i for i in dic.keys()] ls.sort() if(ls[1]%2==0 and ls[0]==ls[1]//2): print(1) for i in range(1,n): if(a[i]-a[i-1]==ls[1]): print(a[i-1]+ls[0]) break else: print(0) else: print(0)
59
233
11,776,000
74272810
n = int(input()) a = sorted(list(map(int, input().split(' ')))) if n == 1: print(-1) exit() if n == 2 and (a[1] - a[0]) % 2 == 0 and a[0] != a[1]: print(3) print(a[0] * 2 - a[1], (a[0] + a[1]) // 2, 2 * a[1] - a[0]) exit() b = sorted([a[i] - a[i - 1] for i in range(1, n)]) if len(set(b)) == 1: if b[0] != 0: print(2) print(a[0] - b[0], a[-1] + b[0]) else: print(1) print(a[0]) exit() if len(set(b)) > 2 or b[-1] != 2 * b[-2]: print(0) else: print(1) for i in range(1, n): if a[i] - a[i - 1] != b[0]: print(a[i - 1] + b[0])
Codeforces Round 224 (Div. 2)
CF
2,014
1
256
Arithmetic Progression
Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a1, a2, ..., an of length n, that the following condition fulfills: a2 - a1 = a3 - a2 = a4 - a3 = ... = ai + 1 - ai = ... = an - an - 1. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled.
The first line contains integer n (1 ≤ n ≤ 105) — the number of cards. The next line contains the sequence of integers — the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 108.
If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 108 or even be negative (see test samples).
null
null
[{"input": "3\n4 1 7", "output": "2\n-2 10"}, {"input": "1\n10", "output": "-1"}, {"input": "4\n1 3 5 9", "output": "1\n7"}, {"input": "4\n4 3 4 5", "output": "0"}, {"input": "2\n2 4", "output": "3\n0 3 6"}]
1,700
["implementation", "sortings"]
59
[{"input": "3\r\n4 1 7\r\n", "output": "2\r\n-2 10\r\n"}, {"input": "1\r\n10\r\n", "output": "-1\r\n"}, {"input": "4\r\n1 3 5 9\r\n", "output": "1\r\n7\r\n"}, {"input": "4\r\n4 3 4 5\r\n", "output": "0\r\n"}, {"input": "2\r\n2 4\r\n", "output": "3\r\n0 3 6\r\n"}, {"input": "4\r\n1 3 4 5\r\n", "output": "1\r\n2\r\n"}, {"input": "2\r\n3 3\r\n", "output": "1\r\n3\r\n"}, {"input": "2\r\n13 2\r\n", "output": "2\r\n-9 24\r\n"}, {"input": "5\r\n2 2 2 2 2\r\n", "output": "1\r\n2\r\n"}, {"input": "6\r\n11 1 7 9 5 13\r\n", "output": "1\r\n3\r\n"}, {"input": "2\r\n100000000 1\r\n", "output": "2\r\n-99999998 199999999\r\n"}, {"input": "5\r\n2 3 1 4 6\r\n", "output": "1\r\n5\r\n"}, {"input": "5\r\n1 2 2 3 4\r\n", "output": "0\r\n"}, {"input": "3\r\n1 4 2\r\n", "output": "1\r\n3\r\n"}, {"input": "3\r\n8 8 8\r\n", "output": "1\r\n8\r\n"}, {"input": "5\r\n2 2 2 2 3\r\n", "output": "0\r\n"}, {"input": "1\r\n100000000\r\n", "output": "-1\r\n"}, {"input": "20\r\n27 6 3 18 54 33 9 15 39 12 57 48 21 51 60 30 24 36 42 45\r\n", "output": "2\r\n0 63\r\n"}, {"input": "40\r\n100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000\r\n", "output": "1\r\n100000000\r\n"}, {"input": "49\r\n81787 163451 104059 89211 96635 133755 148603 141179 159739 122619 123 144891 70651 11259 63227 3835 44667 37243 100347 26107 137467 18683 156027 59515 22395 40955 111483 52091 7547 85499 107771 178299 115195 152315 74363 126331 33531 130043 14971 48379 167163 182011 170875 78075 174587 55803 66939 29819 118907\r\n", "output": "1\r\n92923\r\n"}, {"input": "9\r\n1 2 3 3 4 4 5 5 6\r\n", "output": "0\r\n"}, {"input": "7\r\n1 1 2 3 4 5 6\r\n", "output": "0\r\n"}, {"input": "2\r\n4 1\r\n", "output": "2\r\n-2 7\r\n"}, {"input": "2\r\n2 100000000\r\n", "output": "3\r\n-99999996 50000001 199999998\r\n"}, {"input": "8\r\n1 2 3 4 11 12 13 14\r\n", "output": "0\r\n"}, {"input": "7\r\n5 40 45 50 55 60 65\r\n", "output": "0\r\n"}, {"input": "1\r\n1\r\n", "output": "-1\r\n"}, {"input": "2\r\n1 1\r\n", "output": "1\r\n1\r\n"}, {"input": "2\r\n100000000 2\r\n", "output": "3\r\n-99999996 50000001 199999998\r\n"}, {"input": "3\r\n2 2 3\r\n", "output": "0\r\n"}, {"input": "5\r\n1 3 5 9 13\r\n", "output": "0\r\n"}, {"input": "5\r\n1 2 4 8 16\r\n", "output": "0\r\n"}, {"input": "3\r\n2 2 5\r\n", "output": "0\r\n"}, {"input": "5\r\n1 2 3 4 8\r\n", "output": "0\r\n"}, {"input": "3\r\n1 3 4\r\n", "output": "1\r\n2\r\n"}, {"input": "5\r\n1 2 4 6 7\r\n", "output": "0\r\n"}, {"input": "4\r\n1 5 9 11\r\n", "output": "0\r\n"}, {"input": "4\r\n3 4 5 9\r\n", "output": "0\r\n"}, {"input": "4\r\n1 5 6 8\r\n", "output": "0\r\n"}, {"input": "4\r\n2 6 8 12\r\n", "output": "0\r\n"}, {"input": "5\r\n1 2 3 5 7\r\n", "output": "0\r\n"}, {"input": "6\r\n1 2 3 4 6 8\r\n", "output": "0\r\n"}]
false
stdio
null
true
849/B
849
B
Python 3
TESTS
12
62
307,200
29982697
# coding: utf-8 import sys def on_0x(n, y, x): onl = [None] * n ang = y[x] - y[0] for i in range(n): onl[i] = (y[i] == y[0] + ang * i / x) sp = None ast = None sat = False sys.stderr.write(str(onl) + "\n") for i in range(1, n): if not onl[i]: if sp is None: sp = (i, y[i]) sat = True else: dx = i - sp[0] dy = y[i] - sp[1] if x * dy != ang * dx: sat = None break return sat def on_12(n, y): ang = y[2] - y[1] for i in range(1, n): dy = y[i] - y[1] if dy != ang * (i-1): return False return True def main(): n = int(input()) y = tuple(map(int, input().split())) sat = on_0x(n, y, 1) if sat == True: return "Yes" if sat == False: return "No" sat = on_0x(n, y, 2) if sat == True: return "Yes" if sat == False: return "No" if on_12(n, y): return "Yes" return "No" print(main())
52
62
716,800
29984137
N = int(input()) a = list(map(int, input().split())) import sys sys.setrecursionlimit(1500) def solve(n, slope, d, slope2=None, d2 = 0, point=None): if n == N: if point is None: return False else: return True if a[n] == slope*n + d: return solve(n+1, slope, d, slope2, d2, point) else: if point is None: return solve(n+1, slope, d, slope2, d2, point=n) else: slope2 = (a[n] - a[point]) / (n - point) if slope2 != slope: return False d2 = a[point] - point*slope2 return solve(n+1, slope, d, slope2, d2, point) for i, j in [(0, 1), (1, 2), (0, 2)]: slope = (a[j] - a[i]) / (j - i) d = a[i] - i*slope if solve(0, slope, d): print("Yes") break else: print("No")
Codeforces Round 431 (Div. 2)
CF
2,017
1
256
Tell Your World
Connect the countless points with lines, till we reach the faraway yonder. There are n points on a coordinate plane, the i-th of which being (i, yi). Determine whether it's possible to draw two parallel and non-overlapping lines, such that every point in the set lies on exactly one of them, and each of them passes through at least one point in the set.
The first line of input contains a positive integer n (3 ≤ n ≤ 1 000) — the number of points. The second line contains n space-separated integers y1, y2, ..., yn ( - 109 ≤ yi ≤ 109) — the vertical coordinates of each point.
Output "Yes" (without quotes) if it's possible to fulfill the requirements, and "No" otherwise. You can print each letter in any case (upper or lower).
null
In the first example, there are five points: (1, 7), (2, 5), (3, 8), (4, 6) and (5, 9). It's possible to draw a line that passes through points 1, 3, 5, and another one that passes through points 2, 4 and is parallel to the first one. In the second example, while it's possible to draw two lines that cover all points, they cannot be made parallel. In the third example, it's impossible to satisfy both requirements at the same time.
[{"input": "5\n7 5 8 6 9", "output": "Yes"}, {"input": "5\n-1 -2 0 0 -5", "output": "No"}, {"input": "5\n5 4 3 2 1", "output": "No"}, {"input": "5\n1000000000 0 0 0 0", "output": "Yes"}]
1,600
["brute force", "geometry"]
52
[{"input": "5\r\n7 5 8 6 9\r\n", "output": "Yes\r\n"}, {"input": "5\r\n-1 -2 0 0 -5\r\n", "output": "No\r\n"}, {"input": "5\r\n5 4 3 2 1\r\n", "output": "No\r\n"}, {"input": "5\r\n1000000000 0 0 0 0\r\n", "output": "Yes\r\n"}, {"input": "5\r\n1000000000 1 0 -999999999 -1000000000\r\n", "output": "Yes\r\n"}, {"input": "3\r\n998 244 353\r\n", "output": "Yes\r\n"}, {"input": "3\r\n-1000000000 0 1000000000\r\n", "output": "No\r\n"}, {"input": "5\r\n-1 -1 -1 -1 1\r\n", "output": "Yes\r\n"}, {"input": "4\r\n-9763 530 3595 6660\r\n", "output": "Yes\r\n"}, {"input": "4\r\n-253090305 36298498 374072642 711846786\r\n", "output": "Yes\r\n"}, {"input": "5\r\n-186772848 -235864239 -191561068 -193955178 -243046569\r\n", "output": "Yes\r\n"}, {"input": "5\r\n-954618456 -522919664 -248330428 -130850748 300848044\r\n", "output": "Yes\r\n"}, {"input": "10\r\n4846 6705 2530 5757 5283 -944 -2102 -3260 -4418 2913\r\n", "output": "No\r\n"}, {"input": "10\r\n-6568 -5920 -5272 -4624 -2435 -635 -2680 -2032 -1384 6565\r\n", "output": "No\r\n"}, {"input": "20\r\n319410377 286827025 254243673 221660321 189076969 156493617 123910265 91326913 58743561 26160209 -6423143 -39006495 -71589847 -104173199 -136756551 -169339903 -201923255 -234506607 -267089959 -299673311\r\n", "output": "No\r\n"}, {"input": "20\r\n-975467170 758268840 -975467171 758268839 -975467172 758268838 -975467173 758268837 -975467174 758268836 -975467175 758268835 -975467176 758268834 -975467177 758268833 -975467178 758268832 -975467179 758268831\r\n", "output": "Yes\r\n"}, {"input": "4\r\n1 0 3 0\r\n", "output": "No\r\n"}, {"input": "4\r\n100 2 3 4\r\n", "output": "Yes\r\n"}, {"input": "5\r\n7 5 8 6 3\r\n", "output": "No\r\n"}, {"input": "3\r\n1000000000 1000000000 -1000000000\r\n", "output": "Yes\r\n"}, {"input": "4\r\n1 0 1 4\r\n", "output": "Yes\r\n"}, {"input": "7\r\n1 2 -1 0 1 6 7\r\n", "output": "Yes\r\n"}, {"input": "4\r\n0 0 4 0\r\n", "output": "Yes\r\n"}, {"input": "7\r\n0 0 2 3 4 5 5\r\n", "output": "Yes\r\n"}, {"input": "5\r\n7 5 8 6 8\r\n", "output": "No\r\n"}, {"input": "5\r\n1 2 9 4 5\r\n", "output": "Yes\r\n"}, {"input": "8\r\n1 12 3 14 5 16 7 8\r\n", "output": "Yes\r\n"}, {"input": "5\r\n1 6 7 4 9\r\n", "output": "Yes\r\n"}, {"input": "5\r\n2 1 0 1 2\r\n", "output": "No\r\n"}, {"input": "4\r\n0 0 1 3\r\n", "output": "Yes\r\n"}, {"input": "4\r\n100 50 50 10000000\r\n", "output": "No\r\n"}, {"input": "5\r\n1 2 3 3 3\r\n", "output": "No\r\n"}, {"input": "5\r\n1 2 6 10 17\r\n", "output": "Yes\r\n"}, {"input": "4\r\n1 3 4 4\r\n", "output": "Yes\r\n"}, {"input": "4\r\n100 50 50 1000000\r\n", "output": "No\r\n"}, {"input": "6\r\n1 2 4 5 7 9\r\n", "output": "No\r\n"}, {"input": "6\r\n0 0 1 2 3 4\r\n", "output": "Yes\r\n"}, {"input": "5\r\n7 5 9 10 8\r\n", "output": "Yes\r\n"}, {"input": "7\r\n1 2 2 1 2 2 1\r\n", "output": "Yes\r\n"}, {"input": "4\r\n2 2 4 5\r\n", "output": "Yes\r\n"}, {"input": "6\r\n1 2 1 3 4 5\r\n", "output": "No\r\n"}, {"input": "4\r\n1 3 3 6\r\n", "output": "No\r\n"}, {"input": "5\r\n1 2 -3 4 -1\r\n", "output": "Yes\r\n"}]
false
stdio
null
true
38/F
38
F
PyPy 3-64
TESTS
21
248
8,806,400
202920485
n = int(input()) num = dict() for _ in range(n) : s = input() sub = set() for i in range(len(s)) : for j in range(i, len(s)) : sub.add(s[i:j+1]) for s in sub : if s in num : num[s] += 1 else : num[s] = 1 fs = dict() def f(s) : global fs if s in fs : return fs[s] ans = (False, 0, 0) for x in range(26) : c = chr(x + ord('a')) for t in [c + s, s + c] : if t in num : sm = 0 mx = 0 for c in t : sm += ord(c) - ord('a') + 1 mx = max(mx, ord(c) - ord('a') + 1) (win, a, b) = f(t) ans = max(ans, (not win, b + sm * mx + num[t], a)); fs[s] = ans return ans (win, a, b) = f('') if win : print('First') else : print('Second') print(a, b)
62
404
18,841,600
215708428
import sys from collections import defaultdict readline = sys.stdin.readline def score_word(word: str, subs: dict, graph:defaultdict): tmp = {} for i in range(len(word)): v, m = 0, 0 for j in range(i, len(word)): val = ord(word[j]) - ord('a') + 1 v += val m = max(val, m) sub = word[i:j + 1] graph[sub[:-1]].add(sub) graph[sub[1:]].add(sub) tmp[sub] = v * m for sub in tmp: if sub in subs: subs[sub][0] += 1 else: subs[sub] = [1, tmp[sub]] subs = {} graph = defaultdict(set) def score(word:str) -> int: if word in subs: return sum(subs[word]) return 0 N = int(readline()) inputs = [] for _ in range(N): word = readline().strip() inputs.append(word) score_word(word, subs, graph) cache = {} def dfs(word:str) ->tuple: if word in cache: return cache[word] result = (0, 0, 0) for nword in graph[word]: w, score1, score2 = dfs(nword) result = max(result, (1 - w, - score2 + score(nword), - score1)) cache[word] = result return result dfs('') if cache[''][0] == 1: print('First') else: print('Second') print(cache[''][1], - cache[''][2])
School Personal Contest #1 (Winter Computer School 2010/11) - Codeforces Beta Round 38 (ACM-ICPC Rules)
ICPC
2,010
4
256
Smart Boy
Once Petya and Vasya invented a new game and called it "Smart Boy". They located a certain set of words — the dictionary — for the game. It is admissible for the dictionary to contain similar words. The rules of the game are as follows: first the first player chooses any letter (a word as long as 1) from any word from the dictionary and writes it down on a piece of paper. The second player adds some other letter to this one's initial or final position, thus making a word as long as 2, then it's the first player's turn again, he adds a letter in the beginning or in the end thus making a word as long as 3 and so on. But the player mustn't break one condition: the newly created word must be a substring of a word from a dictionary. The player who can't add a letter to the current word without breaking the condition loses. Also if by the end of a turn a certain string s is written on paper, then the player, whose turn it just has been, gets a number of points according to the formula: $$$$ where - $$\mathrm{value}(c)$$ is a sequence number of symbol c in Latin alphabet, numbered starting from 1. For example, $$value(\mathbf{a}) = 1$$, and $$value(z) = 26$$. - $$\text{num}(s)$$ is the number of words from the dictionary where the line s occurs as a substring at least once. Your task is to learn who will win the game and what the final score will be. Every player plays optimally and most of all tries to win, then — to maximize the number of his points, then — to minimize the number of the points of the opponent.
The first input line contains an integer n which is the number of words in the located dictionary (1 ≤ n ≤ 30). The n lines contain the words from the dictionary — one word is written on one line. Those lines are nonempty, consisting of Latin lower-case characters no longer than 30 characters. Equal words can be in the list of words.
On the first output line print a line "First" or "Second" which means who will win the game. On the second line output the number of points of the first player and the number of points of the second player after the game ends. Separate the numbers by a single space.
null
null
[{"input": "2\naba\nabac", "output": "Second\n29 35"}, {"input": "3\nartem\nnik\nmax", "output": "First\n2403 1882"}]
2,100
["dp", "games", "strings"]
62
[{"input": "2\r\naba\r\nabac\r\n", "output": "Second\r\n29 35\r\n"}, {"input": "3\r\nartem\r\nnik\r\nmax\r\n", "output": "First\r\n2403 1882\r\n"}, {"input": "1\r\njyi\r\n", "output": "First\r\n1727 876\r\n"}, {"input": "2\r\naz\r\nkagim\r\n", "output": "First\r\n1082 678\r\n"}, {"input": "3\r\nskz\r\nsauy\r\nrxu\r\n", "output": "First\r\n2134 963\r\n"}, {"input": "6\r\nrdxo\r\nvpe\r\npa\r\nlrlqy\r\nzj\r\nicbdch\r\n", "output": "First\r\n4078 2852\r\n"}, {"input": "8\r\nvvdclj\r\nyvb\r\nhelty\r\nb\r\na\r\nzwyuvkl\r\nspqtnqmlx\r\nrghfmkbt\r\n", "output": "First\r\n10133 8044\r\n"}, {"input": "10\r\nndqlxtrxiftvtji\r\naoblenbunumdge\r\nlgkmt\r\nx\r\nx\r\nbg\r\nds\r\nnlhdlxeh\r\nugxufipnaxvkxl\r\nk\r\n", "output": "First\r\n26161 23191\r\n"}, {"input": "14\r\nym\r\nbi\r\nyu\r\nyb\r\nvwtb\r\nsemn\r\nbyr\r\nc\r\nir\r\nqyx\r\ngnk\r\nao\r\ndzeo\r\ncd\r\n", "output": "First\r\n2228 1226\r\n"}, {"input": "17\r\ny\r\nn\r\njpt\r\nk\r\npn\r\ncphdbn\r\nvw\r\nkkip\r\nhj\r\ndptlo\r\notkjxvs\r\nnkf\r\ns\r\nvglbf\r\nqytz\r\ncbsvhky\r\nsf\r\n", "output": "First\r\n7108 5451\r\n"}, {"input": "27\r\nlt\r\nah\r\nnyypr\r\nsdz\r\nx\r\nvvkha\r\ned\r\njanf\r\nvooha\r\nvlmbs\r\nx\r\nsc\r\nwa\r\nybw\r\nks\r\nzlyh\r\ndx\r\nlwcrpm\r\nrnfsxa\r\nhk\r\ncshnnj\r\nzgcjb\r\ndnmywn\r\nc\r\nhntkdz\r\nozmwc\r\nqtjsvm\r\n", "output": "First\r\n3713 3070\r\n"}, {"input": "1\r\nbah\r\n", "output": "First\r\n154 73\r\n"}, {"input": "2\r\nfa\r\nqopji\r\n", "output": "First\r\n2247 1532\r\n"}, {"input": "3\r\ndda\r\neeec\r\naac\r\n", "output": "First\r\n54 33\r\n"}, {"input": "6\r\njhfa\r\nbde\r\nbi\r\nfddgc\r\ndb\r\ngchfeh\r\n", "output": "First\r\n326 226\r\n"}, {"input": "8\r\nheehgg\r\nfcd\r\nfebgh\r\na\r\ng\r\ncdfdbgh\r\ngbfbdgagc\r\neeebheah\r\n", "output": "First\r\n772 619\r\n"}, {"input": "10\r\naddlkgekifgigji\r\nabbleabhahmdge\r\nlgkmg\r\nk\r\nk\r\nbg\r\ndf\r\nalhdlkeh\r\nhgkhficaakikkl\r\nk\r\n", "output": "First\r\n5790 5223\r\n"}, {"input": "14\r\nym\r\nbi\r\nyu\r\nyb\r\nvwtb\r\nsemn\r\nbyr\r\nc\r\nir\r\nqyx\r\ngnk\r\nao\r\ndzeo\r\ncd\r\n", "output": "First\r\n2228 1226\r\n"}, {"input": "17\r\ns\r\nj\r\npjb\r\ng\r\ndt\r\nmdjnld\r\npq\r\nqoep\r\ntj\r\ntnjno\r\nqtqlpje\r\nbon\r\nm\r\nrmbrn\r\nseth\r\nolsjdqg\r\ngl\r\n", "output": "First\r\n2091 1478\r\n"}, {"input": "27\r\naa\r\naa\r\naaaaa\r\naaa\r\na\r\naaaaa\r\naa\r\naaaa\r\naaaaa\r\naaaaa\r\na\r\naa\r\naa\r\naaa\r\naa\r\naaaa\r\naa\r\naaaaaa\r\naaaaaa\r\naa\r\naaaaaa\r\naaaaa\r\naaaaaa\r\na\r\naaaaaa\r\naaaaa\r\naaaaaa\r\n", "output": "Second\r\n64 56\r\n"}, {"input": "1\r\njyix\r\n", "output": "Second\r\n2028 2494\r\n"}, {"input": "2\r\nazokag\r\nmsbwcc\r\n", "output": "Second\r\n3435 4033\r\n"}, {"input": "3\r\nskzv\r\nauyi\r\nxurr\r\n", "output": "Second\r\n2212 3278\r\n"}, {"input": "6\r\nrdxoovp\r\nypailrl\r\nytzjeic\r\ndchjlwe\r\ncwcnydr\r\nasoholv\r\n", "output": "First\r\n7310 5775\r\n"}, {"input": "8\r\nvvdcljyyvb\r\nheltysbaay\r\nwyuvklospq\r\nnqmlxrrghf\r\nkbtdbmyhqo\r\nwdxflksybp\r\nxzvxwowusn\r\nfcpjbkjogr\r\n", "output": "Second\r\n14851 17399\r\n"}, {"input": "10\r\nndqlxtrxiftvtji\r\naoblenbunumdgeq\r\ngkmtexexnbghdsf\r\nlhdlxehgugxufip\r\naxvkxlokduwvica\r\nvlfllkwmdmdgbpl\r\njqeidohmzybxpqr\r\nlzspixmvwcidicp\r\nlvfhgjjfnlvgbtn\r\nznlorgtqbgdaamc\r\n", "output": "First\r\n27232 23745\r\n"}, {"input": "14\r\nymrb\r\nkyuh\r\nbivw\r\nbpse\r\nnqby\r\ntcgi\r\nxqyx\r\ngnks\r\noddz\r\nowcd\r\nzkso\r\nqunm\r\nnlhj\r\nkbwy\r\n", "output": "Second\r\n2281 3477\r\n"}, {"input": "17\r\nybnejpt\r\nkcpntcp\r\ndbngvwx\r\nkipshjb\r\nptlopot\r\njxvslnk\r\nqszvglb\r\nfqytzgc\r\nsvhkyys\r\nomkemoj\r\nfqebrzv\r\nhltucwh\r\nvxfkezp\r\nmilusjz\r\nggqoyjd\r\nolddacs\r\ndrrpogu\r\n", "output": "First\r\n7652 6042\r\n"}, {"input": "27\r\nltzahq\r\nyyprgs\r\nzoxgvv\r\nhajedf\r\nanfovo\r\nhasvlm\r\nscxzsc\r\nwayybw\r\nkshzly\r\npdxhlw\r\nrpmzrn\r\nsxanhk\r\ncshnnj\r\nzgcjbb\r\nnmywnu\r\nrhntkd\r\naozmwc\r\nqtjsvm\r\nxidtfw\r\nsofbnn\r\npcdlql\r\nqsjeqp\r\nhtuyki\r\ncdjgsu\r\ntezxuw\r\nakxtwp\r\nmnjmhf\r\n", "output": "Second\r\n5106 6841\r\n"}, {"input": "1\r\nbahf\r\n", "output": "Second\r\n186 250\r\n"}, {"input": "2\r\nfapqop\r\nidoidh\r\n", "output": "Second\r\n2213 2859\r\n"}, {"input": "3\r\nddaa\r\neecd\r\nacce\r\n", "output": "Second\r\n93 137\r\n"}, {"input": "6\r\njhfaebd\r\nibiifdd\r\ncddbegc\r\nfehfhae\r\ngegdehj\r\ngiidihb\r\n", "output": "First\r\n1135 843\r\n"}, {"input": "8\r\nheehggffcd\r\nfebghhaggd\r\ndfdbghggbf\r\ndgagcfeeeb\r\neahgeghfbh\r\negcffcfhgd\r\ndbgfhfcfbb\r\nhfbfeeadfd\r\n", "output": "Second\r\n1316 1552\r\n"}, {"input": "10\r\naddlkgekifgigji\r\nabbleabhahmdged\r\ngkmgekekabghdff\r\nlhdlkehghgkhfic\r\nakikklbkdhjiica\r\nilfllkjmdmdgbcl\r\njdeidbhmmlbkcde\r\nlmfcikmijcidicc\r\nlifhgjjfaligbga\r\nmalbeggdbgdaamc\r\n", "output": "First\r\n8086 7170\r\n"}, {"input": "14\r\nymrb\r\nkyuh\r\nbivw\r\nbpse\r\nnqby\r\ntcgi\r\nxqyx\r\ngnks\r\noddz\r\nowcd\r\nzkso\r\nqunm\r\nnlhj\r\nkbwy\r\n", "output": "Second\r\n2281 3477\r\n"}, {"input": "17\r\nsfjqpjb\r\ngadtbmd\r\nnldkpqt\r\noepmtjb\r\nnjnonqt\r\nlpjedbo\r\ngmtrmbr\r\nhsethmo\r\nsjdqgag\r\ncqsskmt\r\ndmmphjn\r\nfhjacet\r\njrhcmnf\r\nqsfeonr\r\nmsqkqrp\r\natdbmmi\r\nflbpgai\r\n", "output": "First\r\n4972 3905\r\n"}, {"input": "27\r\naaaaaa\r\naaaaaa\r\naaaaaa\r\naaaaaa\r\naaaaaa\r\naaaaaa\r\naaaaaa\r\naaaaaa\r\naaaaaa\r\naaaaaa\r\naaaaaa\r\naaaaaa\r\naaaaaa\r\naaaaaa\r\naaaaaa\r\naaaaaa\r\naaaaaa\r\naaaaaa\r\naaaaaa\r\naaaaaa\r\naaaaaa\r\naaaaaa\r\naaaaaa\r\naaaaaa\r\naaaaaa\r\naaaaaa\r\naaaaaa\r\n", "output": "Second\r\n90 93\r\n"}]
false
stdio
null
true
454/B
454
B
Python 3
TESTS
23
109
7,884,800
108770446
def pony(arr): cont = 0 bandera = False a=len(arr) # # Organizacion # if arr[0] < arr[-1]: # for i in range(0, len(arr) - 1): # if arr[i] <= arr[i + 1]: # continue # else: # return -1 # elif arr[0] >= arr[-1]: # for i in range(0, len(arr)): # if arr[-1] <= arr[0] >= arr[-2]: # arr.pop(len(arr) - 1) # cont = cont + 1 # if arr[0] <= arr[-1]: # break # # print(arr) # if len(arr) >= 2: # for i in range(0, len(arr)-1): # if arr[i] <= arr[i + 1]: # continue # else: # return -1 for i in range(0, a - 1): if arr[i] <= arr[i + 1] and bandera == False: continue elif arr[i] <= arr[i + 1] and bandera == True: cont = cont + 1 elif arr[i] > arr[i + 1]: bandera = True cont = cont + 1 if cont==2: return -1 if arr[0]<arr[-1] and bandera == True: return -1 return cont n = int(input()) arr1 = list(map(int, input().strip().split())) print(pony(arr1))
56
77
13,312,000
181116205
# import sys # input = sys.stdin.readline # for _ in range(int(input())): n = int(input()) a = list(map(int,input().split(" "))) ans,gt = 0,0 for i in range(n-1): if a[i] > a[i+1]: ans = i gt += 1 if a[-1] > a[0]: ans = n-1 gt += 1 if gt == 0: print(0) elif gt > 1: print(-1) else: print(n-1-ans)
Codeforces Round 259 (Div. 2)
CF
2,014
1
256
Little Pony and Sort by Shift
One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an → an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence?
The first line contains an integer n (2 ≤ n ≤ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 105).
If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it.
null
null
[{"input": "2\n2 1", "output": "1"}, {"input": "3\n1 3 2", "output": "-1"}, {"input": "2\n1 2", "output": "0"}]
1,200
["implementation"]
56
[{"input": "2\r\n2 1\r\n", "output": "1\r\n"}, {"input": "3\r\n1 3 2\r\n", "output": "-1\r\n"}, {"input": "2\r\n1 2\r\n", "output": "0\r\n"}, {"input": "6\r\n3 4 5 6 3 2\r\n", "output": "-1\r\n"}, {"input": "3\r\n1 2 1\r\n", "output": "1\r\n"}, {"input": "5\r\n1 1 2 1 1\r\n", "output": "2\r\n"}, {"input": "4\r\n5 4 5 4\r\n", "output": "-1\r\n"}, {"input": "7\r\n3 4 5 5 5 1 2\r\n", "output": "2\r\n"}, {"input": "5\r\n2 2 1 2 2\r\n", "output": "3\r\n"}, {"input": "5\r\n5 4 1 2 3\r\n", "output": "-1\r\n"}, {"input": "4\r\n6 1 2 7\r\n", "output": "-1\r\n"}, {"input": "5\r\n4 5 6 2 3\r\n", "output": "2\r\n"}, {"input": "2\r\n1 1\r\n", "output": "0\r\n"}, {"input": "4\r\n1 2 2 1\r\n", "output": "1\r\n"}, {"input": "9\r\n4 5 6 7 1 2 3 4 10\r\n", "output": "-1\r\n"}, {"input": "7\r\n2 3 4 1 2 3 4\r\n", "output": "-1\r\n"}, {"input": "6\r\n1 2 1 2 1 2\r\n", "output": "-1\r\n"}, {"input": "3\r\n3 2 1\r\n", "output": "-1\r\n"}, {"input": "4\r\n1 4 4 1\r\n", "output": "1\r\n"}, {"input": "5\r\n1 2 1 1 1\r\n", "output": "3\r\n"}, {"input": "5\r\n4 6 7 3 5\r\n", "output": "-1\r\n"}, {"input": "4\r\n2 3 1 4\r\n", "output": "-1\r\n"}, {"input": "5\r\n5 4 3 2 1\r\n", "output": "-1\r\n"}, {"input": "4\r\n2 4 1 4\r\n", "output": "-1\r\n"}, {"input": "6\r\n4 5 6 1 2 7\r\n", "output": "-1\r\n"}, {"input": "6\r\n1 2 3 1 1 1\r\n", "output": "3\r\n"}, {"input": "5\r\n1 3 3 3 1\r\n", "output": "1\r\n"}, {"input": "6\r\n5 6 7 5 5 5\r\n", "output": "3\r\n"}, {"input": "5\r\n3 4 2 1 2\r\n", "output": "-1\r\n"}, {"input": "3\r\n3 4 2\r\n", "output": "1\r\n"}, {"input": "6\r\n1 1 2 2 1 1\r\n", "output": "2\r\n"}, {"input": "4\r\n2 3 4 2\r\n", "output": "1\r\n"}, {"input": "5\r\n3 5 7 7 3\r\n", "output": "1\r\n"}, {"input": "4\r\n1 1 4 1\r\n", "output": "1\r\n"}, {"input": "7\r\n1 5 6 1 1 1 1\r\n", "output": "4\r\n"}, {"input": "5\r\n7 8 6 7 8\r\n", "output": "-1\r\n"}, {"input": "4\r\n2 4 1 3\r\n", "output": "-1\r\n"}]
false
stdio
null
true
989/B
989
B
Python 3
TESTS
17
108
307,200
48115640
n, p = map(int, input().split()) s = list(input()) flag = False t = [""] * n for i in range(p): tmp = [] one = 0 zero = 0 un = 0 for j in range(i, n, p): tmp.append(s[i]) if s[j] == ".": un += 1 elif s[j] == "1": one += 1 else: zero += 1 if len(tmp) == 1: if s[i] == ".": s[i] = '1' continue if zero == 0 and one == 0: flag = True for j in range(i, n, p): if (j - i) // p % 2 == 0: s[j] = "1" else: s[j] = "0" elif un >= 1: flag = True if zero >= 1: for j in range(i, n, p): if s[j] == ".": s[j] = "1" else: for j in range(i, n, p): if s[j] == ".": s[j] = "0" if flag: print("".join(map(str, s))) else: print('No')
59
78
0
39351220
n,p= input().split(); n,p= int(n), int(p); ss=input() s=list(ss) sv=0 for i in range(n-p): if s[i]!=s[i+p] and s[i]!='.' and s[i+p]!='.': sv+=1 break elif s[i]=='.' and s[i+p]!='.': if s[i+p]=='1': s[i]='0' else: s[i]='1' sv+=1; break elif s[i]!='.' and s[i+p]=='.': if s[i]=='1': s[i+p]='0' else: s[i+p]='1' sv+=1; break elif s[i]==s[i+p] and s[i]=='.': s[i]='0'; s[i+p]='1' sv+=1; break f1= ''.join(s) f2= f1.replace('.','0') if sv==0: print('No') else: print(f2)
Codeforces Round 487 (Div. 2)
CF
2,018
1
256
A Tide of Riverscape
The records are expressed as a string $$$s$$$ of characters '0', '1' and '.', where '0' denotes a low tide, '1' denotes a high tide, and '.' denotes an unknown one (either high or low). You are to help Mino determine whether it's possible that after replacing each '.' independently with '0' or '1', a given integer $$$p$$$ is not a period of the resulting string. In case the answer is yes, please also show such a replacement to Mino. In this problem, a positive integer $$$p$$$ is considered a period of string $$$s$$$, if for all $$$1 \leq i \leq \lvert s \rvert - p$$$, the $$$i$$$-th and $$$(i + p)$$$-th characters of $$$s$$$ are the same. Here $$$\lvert s \rvert$$$ is the length of $$$s$$$.
The first line contains two space-separated integers $$$n$$$ and $$$p$$$ ($$$1 \leq p \leq n \leq 2000$$$) — the length of the given string and the supposed period, respectively. The second line contains a string $$$s$$$ of $$$n$$$ characters — Mino's records. $$$s$$$ only contains characters '0', '1' and '.', and contains at least one '.' character.
Output one line — if it's possible that $$$p$$$ is not a period of the resulting string, output any one of such strings; otherwise output "No" (without quotes, you can print letters in any case (upper or lower)).
null
In the first example, $$$7$$$ is not a period of the resulting string because the $$$1$$$-st and $$$8$$$-th characters of it are different. In the second example, $$$6$$$ is not a period of the resulting string because the $$$4$$$-th and $$$10$$$-th characters of it are different. In the third example, $$$9$$$ is always a period because the only constraint that the first and last characters are the same is already satisfied. Note that there are multiple acceptable answers for the first two examples, you can print any of them.
[{"input": "10 7\n1.0.1.0.1.", "output": "1000100010"}, {"input": "10 6\n1.0.1.1000", "output": "1001101000"}, {"input": "10 9\n1........1", "output": "No"}]
1,200
["constructive algorithms", "strings"]
59
[{"input": "10 7\r\n1.0.1.0.1.\r\n", "output": "1000100010\r\n"}, {"input": "10 6\r\n1.0.1.1000\r\n", "output": "1001101000\r\n"}, {"input": "10 9\r\n1........1\r\n", "output": "No\r\n"}, {"input": "1 1\r\n.\r\n", "output": "No\r\n"}, {"input": "5 1\r\n0...1\r\n", "output": "00001\r\n"}, {"input": "17 10\r\n..1.100..1..0.100\r\n", "output": "00101000010000100\r\n"}, {"input": "2 1\r\n0.\r\n", "output": "01\r\n"}, {"input": "2 1\r\n..\r\n", "output": "01\r\n"}, {"input": "3 1\r\n.0.\r\n", "output": "001\r\n"}, {"input": "3 1\r\n00.\r\n", "output": "001\r\n"}, {"input": "3 1\r\n...\r\n", "output": "001\r\n"}, {"input": "3 2\r\n0..\r\n", "output": "001\r\n"}, {"input": "3 2\r\n0.0\r\n", "output": "No\r\n"}, {"input": "3 2\r\n1..\r\n", "output": "100\r\n"}, {"input": "3 2\r\n.1.\r\n", "output": "011\r\n"}, {"input": "3 2\r\n1.0\r\n", "output": "100\r\n"}, {"input": "3 3\r\n1..\r\n", "output": "No\r\n"}, {"input": "3 3\r\n.00\r\n", "output": "No\r\n"}, {"input": "5 3\r\n0.000\r\n", "output": "01000\r\n"}, {"input": "10 6\r\n10010.1001\r\n", "output": "No\r\n"}, {"input": "75 38\r\n00.0.1.0.0110.1.00010..100.1110..110..00.0.1.0.0110.1.00010..100.1110..110.\r\n", "output": "000001000011001000010001000111000110000000010000110010000100010001110001101\r\n"}, {"input": "128 108\r\n01100.110...000.0001.1.11.11.010010.01100.0.1.01.0.0011.11001.000101...1.0.0..100.0110.0110.0.0101.0.0.0001.01100.110...100.0001\r\n", "output": "01100011000000000001010110110010010001100000100100000110110010000101000100000010000110001100000101000000001001100011000010000001\r\n"}, {"input": "5 4\r\n.101.\r\n", "output": "01011\r\n"}, {"input": "4 2\r\n101.\r\n", "output": "1011\r\n"}, {"input": "5 4\r\n.1011\r\n", "output": "01011\r\n"}, {"input": "2 1\r\n..\r\n", "output": "01\r\n"}, {"input": "5 3\r\n00.11\r\n", "output": "00011\r\n"}, {"input": "10 8\r\n1111.00000\r\n", "output": "1111000000\r\n"}, {"input": "10 3\r\n11111111.1\r\n", "output": "1111111101\r\n"}, {"input": "3 2\r\n1.0\r\n", "output": "100\r\n"}, {"input": "5 1\r\n.....\r\n", "output": "00001\r\n"}, {"input": "6 1\r\n......\r\n", "output": "000001\r\n"}, {"input": "3 1\r\n...\r\n", "output": "001\r\n"}, {"input": "6 4\r\n11..10\r\n", "output": "110010\r\n"}, {"input": "4 2\r\n.111\r\n", "output": "0111\r\n"}, {"input": "3 2\r\n01.\r\n", "output": "011\r\n"}, {"input": "5 4\r\n10.00\r\n", "output": "10000\r\n"}, {"input": "10 9\r\n1........0\r\n", "output": "1000000000\r\n"}, {"input": "2 1\r\n0.\r\n", "output": "01\r\n"}, {"input": "10 2\r\n..........\r\n", "output": "0000000001\r\n"}, {"input": "8 4\r\n111111..\r\n", "output": "11111100\r\n"}, {"input": "3 2\r\n0.1\r\n", "output": "001\r\n"}, {"input": "4 1\r\n111.\r\n", "output": "1110\r\n"}, {"input": "3 1\r\n01.\r\n", "output": "010\r\n"}, {"input": "10 7\r\n000....111\r\n", "output": "0000000111\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] ref_path = sys.argv[2] sub_path = sys.argv[3] with open(input_path, 'r') as f: n, p = map(int, f.readline().split()) s = f.readline().strip() with open(ref_path, 'r') as f: ref_output = f.read().strip() with open(sub_path, 'r') as f: sub_output = f.read().strip() if ref_output.lower() == 'no': if sub_output.lower() == 'no': print(1) else: print(0) return if sub_output.lower() == 'no': print(0) return if len(sub_output) != n: print(0) return valid = True for i in range(n): orig_char = s[i] sub_char = sub_output[i] if orig_char in ('0', '1'): if sub_char != orig_char: valid = False break else: if sub_char not in ('0', '1'): valid = False break if not valid: print(0) return p_not_period = False for i in range(n - p): if sub_output[i] != sub_output[i + p]: p_not_period = True break print(1 if p_not_period else 0) if __name__ == "__main__": main()
true
369/A
369
A
PyPy 3-64
TESTS
49
62
0
221068446
n,m,k=map(int,input().split()) l=list(map(int,input().split())) one=l.count(1) two=l.count(2) ans=0 if(one>m): ans=ans+one-m+max(0,two-k) elif(one<m): ans=ans+max(0,two-k-(m-one)) print(ans)
63
46
0
12922288
n,m,k = input().split() n=int(n) m=int(m) k=int(k) l=input().split() m_,k_=l.count('1'),l.count('2') s=0 r=0 if(m_>=m): s+=m_-m if(k_>=k): s+=k_-k print(s) else: k+=m-m_ if(k_>=k): s+=k_-k print(s)
Codeforces Round 216 (Div. 2)
CF
2,013
1
256
Valera and Plates
Valera is a lazy student. He has m clean bowls and k clean plates. Valera has made an eating plan for the next n days. As Valera is lazy, he will eat exactly one dish per day. At that, in order to eat a dish, he needs exactly one clean plate or bowl. We know that Valera can cook only two types of dishes. He can eat dishes of the first type from bowls and dishes of the second type from either bowls or plates. When Valera finishes eating, he leaves a dirty plate/bowl behind. His life philosophy doesn't let him eat from dirty kitchenware. So sometimes he needs to wash his plate/bowl before eating. Find the minimum number of times Valera will need to wash a plate/bowl, if he acts optimally.
The first line of the input contains three integers n, m, k (1 ≤ n, m, k ≤ 1000) — the number of the planned days, the number of clean bowls and the number of clean plates. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 2). If ai equals one, then on day i Valera will eat a first type dish. If ai equals two, then on day i Valera will eat a second type dish.
Print a single integer — the minimum number of times Valera will need to wash a plate/bowl.
null
In the first sample Valera will wash a bowl only on the third day, so the answer is one. In the second sample, Valera will have the first type of the dish during all four days, and since there are only three bowls, he will wash a bowl exactly once. In the third sample, Valera will have the second type of dish for all three days, and as they can be eaten from either a plate or a bowl, he will never need to wash a plate/bowl.
[{"input": "3 1 1\n1 2 1", "output": "1"}, {"input": "4 3 1\n1 1 1 1", "output": "1"}, {"input": "3 1 2\n2 2 2", "output": "0"}, {"input": "8 2 2\n1 2 1 2 1 2 1 2", "output": "4"}]
900
["greedy", "implementation"]
63
[{"input": "3 1 1\r\n1 2 1\r\n", "output": "1\r\n"}, {"input": "4 3 1\r\n1 1 1 1\r\n", "output": "1\r\n"}, {"input": "3 1 2\r\n2 2 2\r\n", "output": "0\r\n"}, {"input": "8 2 2\r\n1 2 1 2 1 2 1 2\r\n", "output": "4\r\n"}, {"input": "2 100 100\r\n2 2\r\n", "output": "0\r\n"}, {"input": "1 1 1\r\n2\r\n", "output": "0\r\n"}, {"input": "233 100 1\r\n2 2 1 1 1 2 2 2 2 1 1 2 2 2 1 2 2 1 1 1 2 2 1 1 1 1 2 1 2 2 1 1 2 2 1 2 2 1 2 1 2 1 2 2 2 1 1 1 1 2 1 2 1 1 2 1 1 2 2 1 2 1 2 1 1 1 1 1 1 1 1 1 2 1 2 2 2 1 1 2 2 1 1 1 1 2 1 1 2 1 2 2 2 1 1 1 2 2 2 1 1 1 1 2 1 2 1 1 1 1 2 2 2 1 1 2 1 2 1 1 1 1 1 2 1 1 1 1 1 2 1 1 2 2 1 2 1 1 2 2 1 1 2 2 1 1 1 2 2 1 1 2 1 2 1 2 2 1 2 2 2 2 2 1 2 2 2 2 2 1 2 2 1 2 2 1 1 1 2 2 1 1 2 2 1 1 2 1 1 2 2 1 2 2 2 2 2 2 1 2 2 2 2 2 1 1 2 2 2 2 2 2 1 1 1 2 1 2 2 2 2 2 2 2 2 1 1 2 1 2 1 2 2\r\n", "output": "132\r\n"}, {"input": "123 100 1\r\n2 2 2 1 1 2 2 2 2 1 1 2 2 2 1 2 2 2 2 1 2 2 2 1 1 1 2 2 2 2 1 2 2 2 2 2 2 1 2 1 2 1 2 2 2 1 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 2 1 1 1 1 1 1 1 1 1 2 2 2 2 2 1 1 2 2 1 1 1 1 2 1 2 2 1 2 2 2 1 1 1 2 2 2 1 2 2 2 2 1 2 2 2 2 1 2 2 2 1 1 2 1 2 1 2 1 1 1\r\n", "output": "22\r\n"}, {"input": "188 100 1\r\n2 2 1 1 1 2 2 2 2 1 1 2 2 2 1 2 2 1 1 1 2 2 1 1 1 1 2 1 2 2 1 1 2 2 1 2 2 1 2 1 2 1 2 2 2 1 1 1 1 2 1 2 1 1 2 1 1 2 2 1 2 1 2 1 1 1 1 1 1 1 1 1 2 1 2 2 2 1 1 2 2 1 1 1 1 2 1 1 2 1 2 2 2 1 1 1 2 2 2 1 1 1 1 2 1 2 1 1 1 1 2 2 2 1 1 2 1 2 1 1 1 1 1 2 1 1 1 1 1 2 1 1 2 2 1 2 1 1 2 2 1 1 2 2 1 1 1 2 2 1 1 2 1 2 1 2 2 1 2 2 2 2 2 1 2 2 2 2 2 1 2 2 1 2 2 1 1 1 2 2 1 1 2 2 1 1 2 1\r\n", "output": "87\r\n"}, {"input": "3 1 2\r\n1 1 1\r\n", "output": "2\r\n"}, {"input": "3 2 2\r\n1 1 1\r\n", "output": "1\r\n"}, {"input": "3 2 1\r\n1 1 1\r\n", "output": "1\r\n"}, {"input": "3 1 1\r\n1 1 1\r\n", "output": "2\r\n"}, {"input": "5 1 2\r\n2 2 2 2 2\r\n", "output": "2\r\n"}, {"input": "5 2 2\r\n2 2 2 2 2\r\n", "output": "1\r\n"}, {"input": "5 2 1\r\n2 2 2 2 2\r\n", "output": "2\r\n"}, {"input": "5 1 1\r\n2 2 2 2 2\r\n", "output": "3\r\n"}, {"input": "1 1 2\r\n2\r\n", "output": "0\r\n"}, {"input": "1 2 2\r\n2\r\n", "output": "0\r\n"}, {"input": "1 2 1\r\n2\r\n", "output": "0\r\n"}, {"input": "1 1 1\r\n2\r\n", "output": "0\r\n"}, {"input": "6 3 1\r\n1 1 2 2 2 2\r\n", "output": "2\r\n"}, {"input": "100 40 20\r\n2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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": "40\r\n"}, {"input": "7 5 2\r\n2 2 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "10 4 4\r\n2 2 2 2 2 2 1 1 1 1\r\n", "output": "2\r\n"}, {"input": "3 2 1\r\n2 1 1\r\n", "output": "0\r\n"}, {"input": "7 6 1\r\n2 1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "7 5 1\r\n1 1 1 2 2 2 2\r\n", "output": "1\r\n"}, {"input": "5 3 1\r\n1 1 2 2 2\r\n", "output": "1\r\n"}, {"input": "3 1 1\r\n2 2 2\r\n", "output": "1\r\n"}, {"input": "5 2 2\r\n2 2 2 2 2\r\n", "output": "1\r\n"}, {"input": "3 1 3\r\n1 1 1\r\n", "output": "2\r\n"}, {"input": "5 2 1\r\n1 1 2 2 2\r\n", "output": "2\r\n"}, {"input": "4 3 2\r\n2 1 1 1\r\n", "output": "0\r\n"}, {"input": "4 2 1\r\n1 2 2 2\r\n", "output": "1\r\n"}, {"input": "14 4 7\r\n1 1 1 2 2 2 2 2 2 2 2 2 2 2\r\n", "output": "3\r\n"}, {"input": "12 10 4\r\n2 2 2 2 2 2 1 1 1 1 1 1\r\n", "output": "0\r\n"}, {"input": "5 3 2\r\n2 2 1 1 1\r\n", "output": "0\r\n"}]
false
stdio
null
true
454/B
454
B
PyPy 3-64
TESTS
23
124
14,848,000
147876149
# n,m = map(lambda x: int(x), input().split()) # n = int(input()) n = int(input()) a = list(map(lambda x: int(x), input().split())) m,mi = min(a),n-1 for i in range(n-1): if a[i]>a[i+1]: mi = i if a[i+1]!=m: print(-1) break else: if mi != n-1 and a[0] < a[-1]:print(-1) else: print(n-mi-1)
56
77
13,312,000
201614272
n = int(input()) l = list(map(int,input().split())) i,idx,t1,c = n-1,-1,0,0 while i>=1: if l[i] >= l[i-1]: if t1 ==0 : b = l[i] t1 =1 c+=1 else: idx = i-1 if t1 == 0: b = l[i] break i-=1 if idx == -1: print("0") else: t1,cmp=0,10000000000 while idx>=0: if l[idx]<=cmp and l[idx]>=b: cmp = l[idx] idx-=1 else: t1 =1 break if t1 == 1: print("-1") else: print(c+1)
Codeforces Round 259 (Div. 2)
CF
2,014
1
256
Little Pony and Sort by Shift
One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning: a1, a2, ..., an → an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence?
The first line contains an integer n (2 ≤ n ≤ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 105).
If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it.
null
null
[{"input": "2\n2 1", "output": "1"}, {"input": "3\n1 3 2", "output": "-1"}, {"input": "2\n1 2", "output": "0"}]
1,200
["implementation"]
56
[{"input": "2\r\n2 1\r\n", "output": "1\r\n"}, {"input": "3\r\n1 3 2\r\n", "output": "-1\r\n"}, {"input": "2\r\n1 2\r\n", "output": "0\r\n"}, {"input": "6\r\n3 4 5 6 3 2\r\n", "output": "-1\r\n"}, {"input": "3\r\n1 2 1\r\n", "output": "1\r\n"}, {"input": "5\r\n1 1 2 1 1\r\n", "output": "2\r\n"}, {"input": "4\r\n5 4 5 4\r\n", "output": "-1\r\n"}, {"input": "7\r\n3 4 5 5 5 1 2\r\n", "output": "2\r\n"}, {"input": "5\r\n2 2 1 2 2\r\n", "output": "3\r\n"}, {"input": "5\r\n5 4 1 2 3\r\n", "output": "-1\r\n"}, {"input": "4\r\n6 1 2 7\r\n", "output": "-1\r\n"}, {"input": "5\r\n4 5 6 2 3\r\n", "output": "2\r\n"}, {"input": "2\r\n1 1\r\n", "output": "0\r\n"}, {"input": "4\r\n1 2 2 1\r\n", "output": "1\r\n"}, {"input": "9\r\n4 5 6 7 1 2 3 4 10\r\n", "output": "-1\r\n"}, {"input": "7\r\n2 3 4 1 2 3 4\r\n", "output": "-1\r\n"}, {"input": "6\r\n1 2 1 2 1 2\r\n", "output": "-1\r\n"}, {"input": "3\r\n3 2 1\r\n", "output": "-1\r\n"}, {"input": "4\r\n1 4 4 1\r\n", "output": "1\r\n"}, {"input": "5\r\n1 2 1 1 1\r\n", "output": "3\r\n"}, {"input": "5\r\n4 6 7 3 5\r\n", "output": "-1\r\n"}, {"input": "4\r\n2 3 1 4\r\n", "output": "-1\r\n"}, {"input": "5\r\n5 4 3 2 1\r\n", "output": "-1\r\n"}, {"input": "4\r\n2 4 1 4\r\n", "output": "-1\r\n"}, {"input": "6\r\n4 5 6 1 2 7\r\n", "output": "-1\r\n"}, {"input": "6\r\n1 2 3 1 1 1\r\n", "output": "3\r\n"}, {"input": "5\r\n1 3 3 3 1\r\n", "output": "1\r\n"}, {"input": "6\r\n5 6 7 5 5 5\r\n", "output": "3\r\n"}, {"input": "5\r\n3 4 2 1 2\r\n", "output": "-1\r\n"}, {"input": "3\r\n3 4 2\r\n", "output": "1\r\n"}, {"input": "6\r\n1 1 2 2 1 1\r\n", "output": "2\r\n"}, {"input": "4\r\n2 3 4 2\r\n", "output": "1\r\n"}, {"input": "5\r\n3 5 7 7 3\r\n", "output": "1\r\n"}, {"input": "4\r\n1 1 4 1\r\n", "output": "1\r\n"}, {"input": "7\r\n1 5 6 1 1 1 1\r\n", "output": "4\r\n"}, {"input": "5\r\n7 8 6 7 8\r\n", "output": "-1\r\n"}, {"input": "4\r\n2 4 1 3\r\n", "output": "-1\r\n"}]
false
stdio
null
true
863/E
863
E
PyPy 3-64
TESTS
13
1,778
54,886,400
216775145
n = int(input()) cnr = [] def por_mayor(lista): return lista[1] def por_menor(lista): return lista[0] for i in range(n): a,b = map(int, input().split()) cnr.append([a,b,i+1]) cnr.sort(key= por_menor, reverse= True) cnr.sort(key= por_mayor) def wea(l): n = len(l) while n!=1: if l[n-1][0]<=l[n-2][1] and l[n-1][0]<=l[n-2][0]: return l[n-2][2] if n-2>=0: if l[n-1][0]==l[n-2][1] and l[n-2][0]==l[n-3][1]: return l[n-2][2] #caso 1 2,2 3,3 4 if l[n-1][0]-1<=l[n-2][1]: l[n-2] = [min(l[n-1][0], l[n-2][0]), max(l[n-1][1], l[n-2][1]),0] n-=1 return -1 print(wea(cnr))
55
795
32,460,800
176636921
import sys input = sys.stdin.readline MAX = 10**9 def read_int(): return int(input()) def read_ints(): return map(int, input().split()) def read_int_list(): return [int(i) for i in input().split()] def read_string(): return input().strip() n = read_int() a = [] for i in range(n): l, r = read_ints() a.append((l, r, i + 1)) a.sort() redundant = -1 l_prev = -MAX r_prev = -MAX r_prev2 = -MAX i_prev = -MAX for j in range(n): l, r, i = a[j] if l == l_prev: redundant = i_prev break elif r <= r_prev: redundant = i break elif l <= r_prev2 + 1: redundant = i_prev break l_prev, r_prev, r_prev2, i_prev = l, r, r_prev, i print(redundant)
Educational Codeforces Round 29
ICPC
2,017
2
256
Turn Off The TV
Luba needs your help again! Luba has n TV sets. She knows that i-th TV set will be working from moment of time li till moment ri, inclusive. Luba wants to switch off one of TV sets in order to free the socket. Let's call some TV set redundant if after switching it off the number of integer moments of time when at least one of TV sets is working won't decrease. Luba will be very upset if she has to switch off a non-redundant TV set. Help Luba by telling her the index of some redundant TV set. If there is no any, print -1.
The first line contains one integer number n (1 ≤ n ≤ 2·105) — the number of TV sets. Then n lines follow, each of them containing two integer numbers li, ri (0 ≤ li ≤ ri ≤ 109) denoting the working time of i-th TV set.
If there is no any redundant TV set, print -1. Otherwise print the index of any redundant TV set (TV sets are indexed from 1 to n). If there are multiple answers, print any of them.
null
Consider the first sample. Initially all integer moments of time such that at least one TV set is working are from the segment [1;7]. It's easy to see that this segment won't change if we switch off the first TV set (or the second one). Note that in the fourth sample you can switch off the second TV set, since even without it all integer moments such that any of the TV sets is working denote the segment [1;4].
[{"input": "3\n1 3\n4 6\n1 7", "output": "1"}, {"input": "2\n0 10\n0 10", "output": "1"}, {"input": "3\n1 2\n3 4\n6 8", "output": "-1"}, {"input": "3\n1 2\n2 3\n3 4", "output": "2"}]
2,000
["data structures", "sortings"]
55
[{"input": "3\r\n1 3\r\n4 6\r\n1 7\r\n", "output": "1\r\n"}, {"input": "2\r\n0 10\r\n0 10\r\n", "output": "1\r\n"}, {"input": "3\r\n1 2\r\n3 4\r\n6 8\r\n", "output": "-1\r\n"}, {"input": "3\r\n1 2\r\n2 3\r\n3 4\r\n", "output": "2\r\n"}, {"input": "3\r\n0 500000000\r\n500000001 1000000000\r\n0 1000000000\r\n", "output": "1\r\n"}, {"input": "3\r\n1 5\r\n2 4\r\n6 10\r\n", "output": "2\r\n"}, {"input": "10\r\n4 4\r\n5 9\r\n5 7\r\n2 8\r\n6 10\r\n4 10\r\n1 3\r\n8 9\r\n0 0\r\n5 7\r\n", "output": "1\r\n"}, {"input": "2\r\n1 3\r\n2 4\r\n", "output": "-1\r\n"}, {"input": "1\r\n8 9\r\n", "output": "-1\r\n"}, {"input": "8\r\n13 17\r\n83 89\r\n31 33\r\n7 13\r\n52 52\r\n88 89\r\n29 30\r\n16 22\r\n", "output": "6\r\n"}, {"input": "4\r\n63 63\r\n12 34\r\n17 29\r\n58 91\r\n", "output": "1\r\n"}, {"input": "3\r\n1 10\r\n5 15\r\n10 20\r\n", "output": "2\r\n"}, {"input": "2\r\n1 3\r\n1 6\r\n", "output": "1\r\n"}, {"input": "2\r\n1 2\r\n1 3\r\n", "output": "1\r\n"}, {"input": "3\r\n5 6\r\n1 3\r\n1 4\r\n", "output": "2\r\n"}, {"input": "3\r\n1 4\r\n2 100\r\n4 5\r\n", "output": "3\r\n"}, {"input": "4\r\n1 1\r\n3 3\r\n4 7\r\n4 5\r\n", "output": "4\r\n"}, {"input": "3\r\n2 3\r\n3 4\r\n1 2\r\n", "output": "1\r\n"}, {"input": "1\r\n0 0\r\n", "output": "-1\r\n"}, {"input": "6\r\n99 100\r\n65 65\r\n34 34\r\n16 18\r\n65 67\r\n88 88\r\n", "output": "2\r\n"}, {"input": "2\r\n50 67\r\n54 64\r\n", "output": "2\r\n"}, {"input": "3\r\n1 3\r\n2 100\r\n3 5\r\n", "output": "3\r\n"}, {"input": "3\r\n57 90\r\n35 45\r\n18 52\r\n", "output": "2\r\n"}, {"input": "4\r\n14 15\r\n46 73\r\n15 40\r\n28 53\r\n", "output": "-1\r\n"}, {"input": "3\r\n37 38\r\n51 54\r\n28 28\r\n", "output": "-1\r\n"}, {"input": "2\r\n64 66\r\n47 61\r\n", "output": "-1\r\n"}, {"input": "4\r\n50 68\r\n63 67\r\n67 69\r\n11 12\r\n", "output": "2\r\n"}, {"input": "4\r\n42 62\r\n93 103\r\n34 62\r\n5 12\r\n", "output": "1\r\n"}, {"input": "6\r\n42 60\r\n78 107\r\n6 38\r\n58 81\r\n70 105\r\n70 105\r\n", "output": "5\r\n"}, {"input": "5\r\n71 71\r\n21 22\r\n58 58\r\n57 57\r\n16 16\r\n", "output": "-1\r\n"}, {"input": "7\r\n28 42\r\n70 75\r\n83 92\r\n19 22\r\n26 32\r\n85 99\r\n30 39\r\n", "output": "7\r\n"}, {"input": "3\r\n8 28\r\n80 110\r\n39 81\r\n", "output": "-1\r\n"}, {"input": "7\r\n90 115\r\n87 113\r\n2 26\r\n39 40\r\n91 112\r\n42 53\r\n65 79\r\n", "output": "5\r\n"}, {"input": "7\r\n12 13\r\n26 28\r\n9 11\r\n15 15\r\n8 10\r\n22 24\r\n5 7\r\n", "output": "-1\r\n"}, {"input": "7\r\n3 5\r\n26 31\r\n11 15\r\n2 4\r\n16 18\r\n4 4\r\n7 12\r\n", "output": "6\r\n"}, {"input": "3\r\n1 5\r\n1 2\r\n4 5\r\n", "output": "2\r\n"}, {"input": "3\r\n999999995 999999997\r\n999999998 1000000000\r\n999999996 999999999\r\n", "output": "3\r\n"}, {"input": "4\r\n1 2\r\n4 6\r\n4 10\r\n200 300\r\n", "output": "2\r\n"}]
false
stdio
import bisect def main(input_path, output_path, submission_output_path): with open(input_path) as f: n = int(f.readline()) intervals = [] for _ in range(n): l, r = map(int, f.readline().split()) intervals.append((l, r)) # Compute redundant TVs points = set() for l, r in intervals: points.add(l - 1) points.add(l) points.add(r) points.add(r + 1) sorted_points = sorted(points) m = len(sorted_points) diff = [0] * (m + 1) for l, r in intervals: idx_l = bisect.bisect_left(sorted_points, l) assert idx_l < m and sorted_points[idx_l] == l diff[idx_l] += 1 idx_r_plus_1 = bisect.bisect_left(sorted_points, r + 1) assert idx_r_plus_1 < m and sorted_points[idx_r_plus_1] == r + 1 diff[idx_r_plus_1] -= 1 coverage = [0] * m current = 0 for i in range(m): current += diff[i] coverage[i] = current single = [0] * m for i in range(m): single[i] = 1 if coverage[i] == 1 else 0 pref = [0] * (m + 1) for i in range(m): pref[i + 1] = pref[i] + single[i] redundant = [] for i in range(n): l, r = intervals[i] l_minus_1 = l - 1 idx_l_minus_1 = bisect.bisect_left(sorted_points, l_minus_1) assert idx_l_minus_1 < m and sorted_points[idx_l_minus_1] == l_minus_1 idx_r = bisect.bisect_left(sorted_points, r) assert idx_r < m and sorted_points[idx_r] == r sum_single = pref[idx_r + 1] - pref[idx_l_minus_1 + 1] if sum_single == 0: redundant.append(i + 1) # Read submission output with open(submission_output_path) as f: submission_output = f.readline().strip() try: k = int(submission_output) except: print(0) return if k == -1: if not redundant: print(1) else: print(0) else: if 1 <= k <= n and k in redundant: print(1) else: print(0) if __name__ == "__main__": import sys 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
841/B
841
B
Python 3
TESTS
55
607
86,323,200
36800430
def check_right(s): found = False seq_sum, partial_sum = 0, 0 for i in range(len(s) - 1, 0, -1): partial_sum += s[i] if found: if s[i] % 2 == 0: return False seq_sum += s[i] if seq_sum % 2 == 1: return True if s[i] % 2 == 0: seq_sum += partial_sum found = True return False def check_left(s): found = False seq_sum, partial_sum = 0, 0 for i in range(len(s) - 1): partial_sum += s[i] if found: if s[i] % 2 == 0: return False seq_sum += s[i] if seq_sum % 2 == 1: return True if s[i] % 2 == 0: seq_sum += partial_sum found = True return False def solve(l): if sum(l) % 2 != 0: return "First" if l[0] % 2 == 1 or l[len(l) - 1] % 2 == 1: return "First" if check_left(l) or check_right(l): return "First" return "Second" n = int(input()) a = [int(i) for i in input().split()] print(solve(a))
88
327
92,569,600
203910056
n=int(input()) print("First" if 1 in [int(e)%2 for e in input().split()] else "Second")
Codeforces Round 429 (Div. 2)
CF
2,017
2
256
Godsend
Leha somehow found an array consisting of n integers. Looking at it, he came up with a task. Two players play the game on the array. Players move one by one. The first player can choose for his move a subsegment of non-zero length with an odd sum of numbers and remove it from the array, after that the remaining parts are glued together into one array and the game continues. The second player can choose a subsegment of non-zero length with an even sum and remove it. Loses the one who can not make a move. Who will win if both play optimally?
First line of input data contains single integer n (1 ≤ n ≤ 106) — length of the array. Next line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109).
Output answer in single line. "First", if first player wins, and "Second" otherwise (without quotes).
null
In first sample first player remove whole array in one move and win. In second sample first player can't make a move and lose.
[{"input": "4\n1 3 2 3", "output": "First"}, {"input": "2\n2 2", "output": "Second"}]
1,100
["games", "math"]
88
[{"input": "4\r\n1 3 2 3\r\n", "output": "First\r\n"}, {"input": "2\r\n2 2\r\n", "output": "Second\r\n"}, {"input": "4\r\n2 4 6 8\r\n", "output": "Second\r\n"}, {"input": "5\r\n1 1 1 1 1\r\n", "output": "First\r\n"}, {"input": "4\r\n720074544 345031254 849487632 80870826\r\n", "output": "Second\r\n"}, {"input": "1\r\n0\r\n", "output": "Second\r\n"}, {"input": "1\r\n999999999\r\n", "output": "First\r\n"}, {"input": "2\r\n1 999999999\r\n", "output": "First\r\n"}, {"input": "4\r\n3 3 4 4\r\n", "output": "First\r\n"}, {"input": "2\r\n1 2\r\n", "output": "First\r\n"}, {"input": "8\r\n2 2 2 1 1 2 2 2\r\n", "output": "First\r\n"}, {"input": "5\r\n3 3 2 2 2\r\n", "output": "First\r\n"}, {"input": "4\r\n0 1 1 0\r\n", "output": "First\r\n"}, {"input": "3\r\n1 2 2\r\n", "output": "First\r\n"}, {"input": "6\r\n2 2 1 1 4 2\r\n", "output": "First\r\n"}, {"input": "8\r\n2 2 2 3 3 2 2 2\r\n", "output": "First\r\n"}, {"input": "4\r\n2 3 3 4\r\n", "output": "First\r\n"}, {"input": "10\r\n2 2 2 2 3 1 2 2 2 2\r\n", "output": "First\r\n"}, {"input": "6\r\n2 2 1 1 2 2\r\n", "output": "First\r\n"}, {"input": "3\r\n1 1 2\r\n", "output": "First\r\n"}, {"input": "6\r\n2 4 3 3 4 6\r\n", "output": "First\r\n"}, {"input": "6\r\n4 4 3 3 4 4\r\n", "output": "First\r\n"}, {"input": "4\r\n1 1 2 2\r\n", "output": "First\r\n"}, {"input": "4\r\n1 3 5 7\r\n", "output": "First\r\n"}, {"input": "4\r\n2 1 1 2\r\n", "output": "First\r\n"}, {"input": "4\r\n1 3 3 2\r\n", "output": "First\r\n"}, {"input": "5\r\n3 2 2 2 2\r\n", "output": "First\r\n"}, {"input": "3\r\n2 1 1\r\n", "output": "First\r\n"}, {"input": "4\r\n1000000000 1000000000 1000000000 99999999\r\n", "output": "First\r\n"}, {"input": "4\r\n2 2 1 1\r\n", "output": "First\r\n"}, {"input": "5\r\n2 3 2 3 2\r\n", "output": "First\r\n"}, {"input": "1\r\n1\r\n", "output": "First\r\n"}, {"input": "4\r\n1000000000 1000000000 1000000000 1\r\n", "output": "First\r\n"}, {"input": "5\r\n2 2 2 1 1\r\n", "output": "First\r\n"}, {"input": "6\r\n2 1 1 1 1 2\r\n", "output": "First\r\n"}, {"input": "6\r\n1 2 2 2 2 1\r\n", "output": "First\r\n"}, {"input": "11\r\n2 2 2 2 2 1 2 2 2 2 2\r\n", "output": "First\r\n"}, {"input": "5\r\n1 3 2 2 2\r\n", "output": "First\r\n"}, {"input": "3\r\n2 3 2\r\n", "output": "First\r\n"}, {"input": "2\r\n1 1\r\n", "output": "First\r\n"}, {"input": "5\r\n4 4 4 3 3\r\n", "output": "First\r\n"}, {"input": "5\r\n3 3 4 4 4\r\n", "output": "First\r\n"}, {"input": "1\r\n2\r\n", "output": "Second\r\n"}]
false
stdio
null
true
830/A
830
A
PyPy 3
TESTS
5
187
29,081,600
116623647
from sys import stdin,stdout from itertools import accumulate nmbr = lambda: int(input()) lst = lambda: list(map(int, input().split())) PI=float('inf') for _ in range(1):#nmbr()): na,nb,o=lst() a=sorted(lst()) b=sorted(lst()) dp=[[PI for _ in range(nb+1)] for _ in range(na+1)] # print(a) # print(b) dp[0][0]=0 for i in range(1,na+1): for j in range(1,nb+1): dp[i][j] = min(dp[i][j-1], max(dp[i-1][j-1] ,abs(a[i-1]-b[j-1])+abs(o-b[j-1]))) # print(*dp,sep='\n') print(dp[na][nb])
59
93
4,096,000
221493972
import sys input = lambda: sys.stdin.readline().rstrip() from collections import deque,defaultdict,Counter from itertools import permutations,combinations from bisect import * from heapq import * from math import ceil,gcd,lcm,floor,comb,prod alph = 'abcdefghijklmnopqrstuvwxyz' #pow(x,mod-2,mod) N,M,K = map(int,input().split()) A = sorted(map(int,input().split())) B = sorted(map(int,input().split())) ans = float("inf") for i in range(M-N+1): t,num = 0,i for j in range(N): t = max(t,abs(A[j]-B[num])+abs(B[num]-K)) num+=1 ans = min(ans,t) print(ans)
Codeforces Round 424 (Div. 1, rated, based on VK Cup Finals)
CF
2,017
2
256
Office Keys
There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else. You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.
The first line contains three integers n, k and p (1 ≤ n ≤ 1 000, n ≤ k ≤ 2 000, 1 ≤ p ≤ 109) — the number of people, the number of keys and the office location. The second line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 109) — positions in which people are located initially. The positions are given in arbitrary order. The third line contains k distinct integers b1, b2, ..., bk (1 ≤ bj ≤ 109) — positions of the keys. The positions are given in arbitrary order. Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.
Print the minimum time (in seconds) needed for all n to reach the office with keys.
null
In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys.
[{"input": "2 4 50\n20 100\n60 10 40 80", "output": "50"}, {"input": "1 2 10\n11\n15 7", "output": "7"}]
1,800
["binary search", "brute force", "dp", "greedy", "sortings"]
59
[{"input": "2 4 50\r\n20 100\r\n60 10 40 80\r\n", "output": "50\r\n"}, {"input": "1 2 10\r\n11\r\n15 7\r\n", "output": "7\r\n"}, {"input": "2 5 15\r\n10 4\r\n29 23 21 22 26\r\n", "output": "23\r\n"}, {"input": "3 10 1500\r\n106 160 129\r\n1333 1532 1181 1091 1656 1698 1291 1741 1242 1163\r\n", "output": "1394\r\n"}, {"input": "5 20 1\r\n314 316 328 323 321\r\n30 61 11 83 19 63 97 87 14 79 43 57 75 48 47 95 41 27 8 88\r\n", "output": "327\r\n"}, {"input": "20 20 1000000000\r\n911196469 574676950 884047241 984218701 641693148 352743122 616364857 455260052 702604347 921615943 671695009 544819698 768892858 254148055 379968391 65297129 178692403 575557323 307174510 63022600\r\n1621 106 6866 6420 9307 6985 2741 9477 9837 5909 6757 3085 6139 1876 3726 9334 4321 1531 8534 560\r\n", "output": "1984199027\r\n"}, {"input": "40 45 1000\r\n6 55 34 32 20 76 2 84 47 68 31 60 14 70 99 72 21 61 81 79 26 51 96 86 10 1 43 69 87 78 13 11 80 67 50 52 9 29 94 12\r\n1974 1232 234 28 1456 626 408 1086 1525 1209 1096 940 795 1867 548 1774 1993 1199 1112 1087 1923 1156 876 1715 1815 1027 1658 955 398 910 620 1164 749 996 113 109 500 328 800 826 766 518 1474 1038 1029\r\n", "output": "2449\r\n"}, {"input": "50 55 2000\r\n9518 9743 9338 9956 9827 9772 9094 9644 9242 9292 9148 9205 9907 9860 9530 9814 9662 9482 9725 9227 9105 9424 9268 9427 9470 9578 9808 9976 9143 9070 9079 9896 9367 9235 9925 9009 9619 9012 9669 9077 9870 9766 9479 9598 9055 9988 9792 9197 9377 9610\r\n828 656 345 412 69 506 274 994 384 766 587 126 720 227 66 839 997 602 646 955 256 262 243 676 459 83 507 88 559 595 71 154 867 276 487 895 857 888 368 179 813 407 973 780 588 112 815 290 554 230 768 804 974 3 745\r\n", "output": "10833\r\n"}, {"input": "1 1 1\r\n1\r\n1000000000\r\n", "output": "1999999998\r\n"}, {"input": "1 1 1\r\n1000000000\r\n1\r\n", "output": "999999999\r\n"}, {"input": "1 1 1000000000\r\n1000000000\r\n1\r\n", "output": "1999999998\r\n"}, {"input": "1 1 1000000000\r\n1\r\n1000000000\r\n", "output": "999999999\r\n"}, {"input": "2 2 4\r\n3 4\r\n5 6\r\n", "output": "4\r\n"}, {"input": "2 2 5\r\n1 2\r\n3 1000000000\r\n", "output": "1999999993\r\n"}, {"input": "1 1 1000000000\r\n1000000000\r\n1\r\n", "output": "1999999998\r\n"}, {"input": "2 2 1\r\n2 3\r\n4 100\r\n", "output": "196\r\n"}, {"input": "2 2 10\r\n3 12\r\n1 9\r\n", "output": "11\r\n"}, {"input": "3 3 1\r\n1 2 3\r\n999 1000000000 1\r\n", "output": "1999999996\r\n"}, {"input": "1 1 1\r\n1\r\n1\r\n", "output": "0\r\n"}, {"input": "1 1 1\r\n1\r\n1000000000\r\n", "output": "1999999998\r\n"}, {"input": "1 1 1000000000\r\n1000000000\r\n10\r\n", "output": "1999999980\r\n"}, {"input": "2 2 7122\r\n123 456\r\n1 4444\r\n", "output": "7243\r\n"}, {"input": "1 1 10\r\n5\r\n15\r\n", "output": "15\r\n"}, {"input": "2 4 1000\r\n1000 999\r\n1 1000 2 999\r\n", "output": "1\r\n"}, {"input": "2 2 1000\r\n10 1010\r\n1 1001\r\n", "output": "1008\r\n"}, {"input": "1 1 1\r\n2\r\n1000000000\r\n", "output": "1999999997\r\n"}, {"input": "2 2 3\r\n1 5\r\n5 1\r\n", "output": "2\r\n"}, {"input": "2 2 5\r\n2 3\r\n4 6\r\n", "output": "4\r\n"}, {"input": "2 2 10\r\n5 6\r\n4 6\r\n", "output": "7\r\n"}, {"input": "3 4 10\r\n5 7 9\r\n6 8 14 4\r\n", "output": "7\r\n"}, {"input": "1 1 10\r\n10\r\n10\r\n", "output": "0\r\n"}, {"input": "1 1 50\r\n1\r\n1000000000\r\n", "output": "1999999949\r\n"}, {"input": "1 1 42\r\n666\r\n1337\r\n", "output": "1966\r\n"}, {"input": "2 2 10\r\n9 11\r\n11 8\r\n", "output": "3\r\n"}, {"input": "3 10 5\r\n1 2 3\r\n10000 9999 9998 9997 9996 9995 9994 7 6 5\r\n", "output": "6\r\n"}, {"input": "1 1 2\r\n1\r\n1000000000\r\n", "output": "1999999997\r\n"}, {"input": "2 2 100\r\n99 150\r\n1 150\r\n", "output": "197\r\n"}, {"input": "3 3 4\r\n1 101 102\r\n2 3 100\r\n", "output": "99\r\n"}]
false
stdio
null
true
830/A
830
A
PyPy 3-64
TESTS
37
93
3,686,400
226627792
import sys input = lambda: sys.stdin.readline().rstrip() from collections import deque N,K,P = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) A.sort();B.sort() def check(m): D = deque(B) for a in A: while D and abs(a-D[0])+abs(D[0]-P)>m: t = D.popleft() #print(t, abs(a-t)+abs(t-P)) if not D: #print(a) return False D.popleft() return True #print(check(60)) l,r=0,10**10 while l+1<r: m = (l+r)//2 if check(m): r = m else: l = m print(r)
59
124
2,355,200
119133977
import sys input=sys.stdin.readline n,k,p=map(int,input().split()) a=list(map(int,input().split())) b=list(map(int,input().split())) a.sort() b.sort() ans=10**18 for i in range(k-n+1): sc=0 for j in range(n): sc=max(sc,abs(a[j]-b[i+j])+abs(b[i+j]-p)) ans=min(ans,sc) print(ans)
Codeforces Round 424 (Div. 1, rated, based on VK Cup Finals)
CF
2,017
2
256
Office Keys
There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else. You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.
The first line contains three integers n, k and p (1 ≤ n ≤ 1 000, n ≤ k ≤ 2 000, 1 ≤ p ≤ 109) — the number of people, the number of keys and the office location. The second line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 109) — positions in which people are located initially. The positions are given in arbitrary order. The third line contains k distinct integers b1, b2, ..., bk (1 ≤ bj ≤ 109) — positions of the keys. The positions are given in arbitrary order. Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.
Print the minimum time (in seconds) needed for all n to reach the office with keys.
null
In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys.
[{"input": "2 4 50\n20 100\n60 10 40 80", "output": "50"}, {"input": "1 2 10\n11\n15 7", "output": "7"}]
1,800
["binary search", "brute force", "dp", "greedy", "sortings"]
59
[{"input": "2 4 50\r\n20 100\r\n60 10 40 80\r\n", "output": "50\r\n"}, {"input": "1 2 10\r\n11\r\n15 7\r\n", "output": "7\r\n"}, {"input": "2 5 15\r\n10 4\r\n29 23 21 22 26\r\n", "output": "23\r\n"}, {"input": "3 10 1500\r\n106 160 129\r\n1333 1532 1181 1091 1656 1698 1291 1741 1242 1163\r\n", "output": "1394\r\n"}, {"input": "5 20 1\r\n314 316 328 323 321\r\n30 61 11 83 19 63 97 87 14 79 43 57 75 48 47 95 41 27 8 88\r\n", "output": "327\r\n"}, {"input": "20 20 1000000000\r\n911196469 574676950 884047241 984218701 641693148 352743122 616364857 455260052 702604347 921615943 671695009 544819698 768892858 254148055 379968391 65297129 178692403 575557323 307174510 63022600\r\n1621 106 6866 6420 9307 6985 2741 9477 9837 5909 6757 3085 6139 1876 3726 9334 4321 1531 8534 560\r\n", "output": "1984199027\r\n"}, {"input": "40 45 1000\r\n6 55 34 32 20 76 2 84 47 68 31 60 14 70 99 72 21 61 81 79 26 51 96 86 10 1 43 69 87 78 13 11 80 67 50 52 9 29 94 12\r\n1974 1232 234 28 1456 626 408 1086 1525 1209 1096 940 795 1867 548 1774 1993 1199 1112 1087 1923 1156 876 1715 1815 1027 1658 955 398 910 620 1164 749 996 113 109 500 328 800 826 766 518 1474 1038 1029\r\n", "output": "2449\r\n"}, {"input": "50 55 2000\r\n9518 9743 9338 9956 9827 9772 9094 9644 9242 9292 9148 9205 9907 9860 9530 9814 9662 9482 9725 9227 9105 9424 9268 9427 9470 9578 9808 9976 9143 9070 9079 9896 9367 9235 9925 9009 9619 9012 9669 9077 9870 9766 9479 9598 9055 9988 9792 9197 9377 9610\r\n828 656 345 412 69 506 274 994 384 766 587 126 720 227 66 839 997 602 646 955 256 262 243 676 459 83 507 88 559 595 71 154 867 276 487 895 857 888 368 179 813 407 973 780 588 112 815 290 554 230 768 804 974 3 745\r\n", "output": "10833\r\n"}, {"input": "1 1 1\r\n1\r\n1000000000\r\n", "output": "1999999998\r\n"}, {"input": "1 1 1\r\n1000000000\r\n1\r\n", "output": "999999999\r\n"}, {"input": "1 1 1000000000\r\n1000000000\r\n1\r\n", "output": "1999999998\r\n"}, {"input": "1 1 1000000000\r\n1\r\n1000000000\r\n", "output": "999999999\r\n"}, {"input": "2 2 4\r\n3 4\r\n5 6\r\n", "output": "4\r\n"}, {"input": "2 2 5\r\n1 2\r\n3 1000000000\r\n", "output": "1999999993\r\n"}, {"input": "1 1 1000000000\r\n1000000000\r\n1\r\n", "output": "1999999998\r\n"}, {"input": "2 2 1\r\n2 3\r\n4 100\r\n", "output": "196\r\n"}, {"input": "2 2 10\r\n3 12\r\n1 9\r\n", "output": "11\r\n"}, {"input": "3 3 1\r\n1 2 3\r\n999 1000000000 1\r\n", "output": "1999999996\r\n"}, {"input": "1 1 1\r\n1\r\n1\r\n", "output": "0\r\n"}, {"input": "1 1 1\r\n1\r\n1000000000\r\n", "output": "1999999998\r\n"}, {"input": "1 1 1000000000\r\n1000000000\r\n10\r\n", "output": "1999999980\r\n"}, {"input": "2 2 7122\r\n123 456\r\n1 4444\r\n", "output": "7243\r\n"}, {"input": "1 1 10\r\n5\r\n15\r\n", "output": "15\r\n"}, {"input": "2 4 1000\r\n1000 999\r\n1 1000 2 999\r\n", "output": "1\r\n"}, {"input": "2 2 1000\r\n10 1010\r\n1 1001\r\n", "output": "1008\r\n"}, {"input": "1 1 1\r\n2\r\n1000000000\r\n", "output": "1999999997\r\n"}, {"input": "2 2 3\r\n1 5\r\n5 1\r\n", "output": "2\r\n"}, {"input": "2 2 5\r\n2 3\r\n4 6\r\n", "output": "4\r\n"}, {"input": "2 2 10\r\n5 6\r\n4 6\r\n", "output": "7\r\n"}, {"input": "3 4 10\r\n5 7 9\r\n6 8 14 4\r\n", "output": "7\r\n"}, {"input": "1 1 10\r\n10\r\n10\r\n", "output": "0\r\n"}, {"input": "1 1 50\r\n1\r\n1000000000\r\n", "output": "1999999949\r\n"}, {"input": "1 1 42\r\n666\r\n1337\r\n", "output": "1966\r\n"}, {"input": "2 2 10\r\n9 11\r\n11 8\r\n", "output": "3\r\n"}, {"input": "3 10 5\r\n1 2 3\r\n10000 9999 9998 9997 9996 9995 9994 7 6 5\r\n", "output": "6\r\n"}, {"input": "1 1 2\r\n1\r\n1000000000\r\n", "output": "1999999997\r\n"}, {"input": "2 2 100\r\n99 150\r\n1 150\r\n", "output": "197\r\n"}, {"input": "3 3 4\r\n1 101 102\r\n2 3 100\r\n", "output": "99\r\n"}]
false
stdio
null
true
55/C
55
C
Python 3
TESTS
6
124
4,608,000
17086721
[n,m,k]=[int(i) for i in input().split()] isDone=False for i in range(0,k): [x,y]=[int(i) for i in input().split()] if (x>=n-5 or x<=5) and (y<=5 or y>=m-5): isDone=True if isDone: print("YES") else: print("NO")
75
122
0
148660589
n, m, k = map(int, input().split()) for i in range(k): x, y = map(int, input().split()) if min(n-x, x-1, m-y, y-1) <5: print('YES') exit() print('NO')
Codeforces Beta Round 51
CF
2,011
2
256
Pie or die
Volodya and Vlad play the following game. There are k pies at the cells of n × m board. Each turn Volodya moves one pie to the neighbouring (by side) cell. If the pie lies at the border of the board then Volodya can move it outside the board, get the pie and win. After Volodya's move, Vlad bans some edge at the border of the board of length 1 (between two knots of the board) so that Volodya is not able to move the pie outside the board through this edge anymore. The question is: will Volodya win this game? We suppose both players follow the optimal strategy.
First line contains 3 integers, separated by space: 1 ≤ n, m ≤ 100 — dimensions of the board and 0 ≤ k ≤ 100 — the number of pies. Each of the next k lines contains 2 integers, separated by space: 1 ≤ x ≤ n, 1 ≤ y ≤ m — coordinates of the corresponding pie. There could be more than one pie at a cell.
Output only one word: "YES" — if Volodya wins, "NO" — otherwise.
null
null
[{"input": "2 2 1\n1 2", "output": "YES"}, {"input": "3 4 0", "output": "NO"}, {"input": "100 50 2\n50 25\n50 25", "output": "NO"}]
1,900
["games"]
75
[{"input": "2 2 1\r\n1 2\r\n", "output": "YES\r\n"}, {"input": "3 4 0\r\n", "output": "NO\r\n"}, {"input": "100 50 2\r\n50 25\r\n50 25\r\n", "output": "NO\r\n"}, {"input": "20 20 4\r\n10 10\r\n10 10\r\n10 10\r\n10 10\r\n", "output": "NO\r\n"}, {"input": "15 15 1\r\n8 8\r\n", "output": "NO\r\n"}, {"input": "8 8 2\r\n4 4\r\n5 5\r\n", "output": "YES\r\n"}, {"input": "100 100 2\r\n50 96\r\n51 96\r\n", "output": "YES\r\n"}, {"input": "100 100 2\r\n50 95\r\n51 95\r\n", "output": "NO\r\n"}, {"input": "20 20 1\r\n16 10\r\n", "output": "YES\r\n"}, {"input": "20 20 4\r\n15 9\r\n15 10\r\n15 11\r\n15 12\r\n", "output": "NO\r\n"}, {"input": "11 11 1\r\n6 6\r\n", "output": "NO\r\n"}, {"input": "11 11 1\r\n6 5\r\n", "output": "YES\r\n"}, {"input": "35 13 20\r\n13 8\r\n19 8\r\n24 7\r\n20 6\r\n23 7\r\n23 6\r\n30 7\r\n29 7\r\n7 7\r\n6 8\r\n9 8\r\n29 6\r\n20 7\r\n25 6\r\n19 6\r\n23 8\r\n26 6\r\n12 6\r\n15 7\r\n6 8\r\n", "output": "NO\r\n"}, {"input": "50 17 27\r\n17 8\r\n19 6\r\n25 8\r\n30 10\r\n22 10\r\n30 9\r\n25 8\r\n27 6\r\n19 7\r\n29 11\r\n39 8\r\n31 8\r\n39 8\r\n40 7\r\n11 8\r\n30 11\r\n32 8\r\n31 11\r\n36 12\r\n10 11\r\n32 8\r\n8 7\r\n7 12\r\n17 11\r\n27 7\r\n8 8\r\n23 12\r\n", "output": "NO\r\n"}, {"input": "24 29 22\r\n16 6\r\n14 22\r\n7 15\r\n11 17\r\n12 22\r\n10 13\r\n12 22\r\n12 13\r\n6 16\r\n12 21\r\n11 11\r\n9 13\r\n18 22\r\n7 20\r\n13 6\r\n6 14\r\n17 10\r\n9 13\r\n7 23\r\n14 11\r\n7 22\r\n8 12\r\n", "output": "NO\r\n"}, {"input": "32 45 3\r\n12 30\r\n27 9\r\n14 27\r\n", "output": "NO\r\n"}, {"input": "96 95 31\r\n14 23\r\n70 47\r\n11 77\r\n53 66\r\n63 87\r\n3 14\r\n57 44\r\n65 69\r\n80 74\r\n49 6\r\n57 86\r\n75 8\r\n2 32\r\n75 21\r\n14 51\r\n56 46\r\n77 6\r\n17 89\r\n87 3\r\n21 18\r\n70 67\r\n47 64\r\n13 47\r\n61 33\r\n56 30\r\n28 2\r\n65 18\r\n17 90\r\n44 77\r\n54 26\r\n32 70\r\n", "output": "YES\r\n"}, {"input": "15 96 22\r\n4 7\r\n7 40\r\n13 30\r\n8 53\r\n6 78\r\n5 9\r\n15 35\r\n3 13\r\n5 31\r\n2 9\r\n13 50\r\n11 17\r\n4 2\r\n10 91\r\n11 74\r\n14 49\r\n8 30\r\n10 66\r\n12 44\r\n6 19\r\n9 62\r\n15 50\r\n", "output": "YES\r\n"}, {"input": "61 96 15\r\n27 36\r\n19 64\r\n27 53\r\n59 63\r\n48 56\r\n55 30\r\n10 23\r\n6 79\r\n32 74\r\n7 51\r\n29 65\r\n60 16\r\n43 74\r\n40 80\r\n14 31\r\n", "output": "YES\r\n"}, {"input": "36 89 27\r\n21 66\r\n3 60\r\n11 32\r\n10 81\r\n30 31\r\n27 62\r\n11 81\r\n24 41\r\n30 6\r\n13 45\r\n34 86\r\n26 46\r\n9 62\r\n8 86\r\n17 56\r\n4 86\r\n25 36\r\n23 72\r\n18 55\r\n18 87\r\n22 67\r\n18 12\r\n19 75\r\n21 60\r\n16 49\r\n33 63\r\n26 12\r\n", "output": "YES\r\n"}, {"input": "11 38 21\r\n2 21\r\n2 28\r\n7 19\r\n9 18\r\n7 25\r\n8 4\r\n3 23\r\n2 32\r\n5 34\r\n10 36\r\n8 21\r\n4 6\r\n6 6\r\n4 35\r\n8 34\r\n10 18\r\n11 4\r\n10 2\r\n10 13\r\n4 37\r\n2 29\r\n", "output": "YES\r\n"}, {"input": "30 84 35\r\n20 60\r\n23 21\r\n14 24\r\n24 72\r\n13 76\r\n25 35\r\n11 64\r\n15 57\r\n9 55\r\n14 66\r\n10 24\r\n13 68\r\n11 8\r\n19 43\r\n11 14\r\n16 26\r\n11 22\r\n10 26\r\n15 66\r\n17 65\r\n21 34\r\n7 61\r\n24 64\r\n18 16\r\n22 18\r\n12 9\r\n10 40\r\n8 24\r\n16 52\r\n10 9\r\n7 17\r\n21 78\r\n18 75\r\n10 45\r\n16 29\r\n", "output": "NO\r\n"}, {"input": "66 94 26\r\n11 75\r\n46 72\r\n55 74\r\n34 10\r\n33 84\r\n25 11\r\n13 23\r\n27 73\r\n45 22\r\n54 34\r\n53 63\r\n28 8\r\n57 46\r\n26 78\r\n52 46\r\n32 38\r\n22 55\r\n17 71\r\n56 18\r\n9 60\r\n31 54\r\n6 84\r\n59 57\r\n60 81\r\n51 49\r\n41 77\r\n", "output": "NO\r\n"}, {"input": "68 100 18\r\n17 85\r\n10 77\r\n59 55\r\n29 46\r\n25 74\r\n55 11\r\n37 16\r\n57 61\r\n26 11\r\n11 88\r\n19 18\r\n28 38\r\n32 12\r\n36 49\r\n32 6\r\n57 45\r\n30 6\r\n59 95\r\n", "output": "NO\r\n"}, {"input": "28 61 4\r\n12 18\r\n21 31\r\n14 52\r\n6 36\r\n", "output": "NO\r\n"}, {"input": "11 73 1\r\n4 67\r\n", "output": "YES\r\n"}, {"input": "11 79 0\r\n", "output": "NO\r\n"}, {"input": "11 23 1\r\n11 9\r\n", "output": "YES\r\n"}, {"input": "25 11 0\r\n", "output": "NO\r\n"}, {"input": "39 11 1\r\n18 3\r\n", "output": "YES\r\n"}, {"input": "69 11 0\r\n", "output": "NO\r\n"}, {"input": "13 19 1\r\n6 10\r\n", "output": "NO\r\n"}, {"input": "14 17 0\r\n", "output": "NO\r\n"}, {"input": "20 19 5\r\n7 14\r\n14 12\r\n7 12\r\n15 9\r\n12 6\r\n", "output": "NO\r\n"}, {"input": "17 15 3\r\n10 7\r\n12 6\r\n8 6\r\n", "output": "NO\r\n"}, {"input": "14 17 4\r\n9 9\r\n8 7\r\n8 12\r\n7 9\r\n", "output": "NO\r\n"}, {"input": "15 11 0\r\n", "output": "NO\r\n"}, {"input": "14 16 4\r\n6 11\r\n6 8\r\n8 6\r\n6 7\r\n", "output": "NO\r\n"}, {"input": "16 16 0\r\n", "output": "NO\r\n"}, {"input": "19 20 2\r\n10 14\r\n8 11\r\n", "output": "NO\r\n"}, {"input": "13 15 1\r\n7 10\r\n", "output": "NO\r\n"}, {"input": "11 100 4\r\n6 10\r\n6 20\r\n6 30\r\n6 80\r\n", "output": "NO\r\n"}, {"input": "100 11 2\r\n40 6\r\n70 6\r\n", "output": "NO\r\n"}, {"input": "100 11 5\r\n20 6\r\n30 6\r\n43 7\r\n78 6\r\n89 6\r\n", "output": "YES\r\n"}, {"input": "20 20 5\r\n10 6\r\n6 8\r\n16 11\r\n11 11\r\n7 15\r\n", "output": "YES\r\n"}, {"input": "30 30 5\r\n7 15\r\n24 11\r\n15 15\r\n8 24\r\n9 6\r\n", "output": "NO\r\n"}]
false
stdio
null
true
532/E
533
E
PyPy 3
TESTS
3
140
0
118563505
def check(pt1,pt2): global count while pt1<n and pt2<n: if st1[pt1] ==st2[pt2]: pt1+=1 pt2+=1 else: if pt1+1 <n and st1[pt1+1] ==st2[pt2]: pt1+=1 count+=1 elif pt2+1<n and st1[pt1] ==st2[pt2+1]: pt2+=1 count+=1 elif (pt1+1 and pt2+1) <n and st1[pt1+1] ==st2[pt2+1]: pt1+=1 pt2+=1 count+=1 else: return 0 if count >2: return 0 return 1 n=int(input()) st1=input() st2=input() count=0 c=check(0,0) if c==1: print(count%2 +1) else: print(0)
89
108
2,560,000
197614721
i,j=0,int(input())-1 a,b=input(),input() while a[i]==b[i]:i+=1 while a[j]==b[j]:j-=1 print((a[i+1:j+1]==b[i:j])+(b[i+1:j+1]==a[i:j]))
VK Cup 2015 - Round 2
CF
2,015
2
256
Correcting Mistakes
Analyzing the mistakes people make while typing search queries is a complex and an interesting work. As there is no guaranteed way to determine what the user originally meant by typing some query, we have to use different sorts of heuristics. Polycarp needed to write a code that could, given two words, check whether they could have been obtained from the same word as a result of typos. Polycarpus suggested that the most common typo is skipping exactly one letter as you type a word. Implement a program that can, given two distinct words S and T of the same length n determine how many words W of length n + 1 are there with such property that you can transform W into both S, and T by deleting exactly one character. Words S and T consist of lowercase English letters. Word W also should consist of lowercase English letters.
The first line contains integer n (1 ≤ n ≤ 100 000) — the length of words S and T. The second line contains word S. The third line contains word T. Words S and T consist of lowercase English letters. It is guaranteed that S and T are distinct words.
Print a single integer — the number of distinct words W that can be transformed to S and T due to a typo.
null
In the first sample test the two given words could be obtained only from word "treading" (the deleted letters are marked in bold). In the second sample test the two given words couldn't be obtained from the same word by removing one letter. In the third sample test the two given words could be obtained from either word "tory" or word "troy".
[{"input": "7\nreading\ntrading", "output": "1"}, {"input": "5\nsweet\nsheep", "output": "0"}, {"input": "3\ntoy\ntry", "output": "2"}]
1,800
[]
89
[{"input": "7\r\nreading\r\ntrading\r\n", "output": "1\n"}, {"input": "5\r\nsweet\r\nsheep\r\n", "output": "0\n"}, {"input": "3\r\ntoy\r\ntry\r\n", "output": "2\n"}, {"input": "5\r\nspare\r\nspars\r\n", "output": "2\n"}, {"input": "1\r\na\r\nb\r\n", "output": "2\n"}, {"input": "1\r\nz\r\ny\r\n", "output": "2\n"}, {"input": "2\r\nab\r\nac\r\n", "output": "2\n"}, {"input": "2\r\nba\r\nca\r\n", "output": "2\n"}, {"input": "2\r\nac\r\ncb\r\n", "output": "1\n"}, {"input": "100\r\neebdeddddbecdbddaaecbbaccbecdeacedddcaddcdebedbabbceeeadecadbbeaecdaeabbceacbdbdbbdacebbbccdcbbeedbe\r\ndacdeebebeaeaacdeedadbcbaedcbddddddedacbabeddebaaebbdcebebaaccbaeccbecdbcbceadaaecadecbadbcddcdabecd\r\n", "output": "0\n"}, {"input": "250\r\niiffiehchidfgigdbcciahdehjjfacbbaaadagaibjjcehjcbjdhaadebaejiicgidbhajfbfejcdicgfbcchgbahfccbefdcddbjjhejigiafhdjbiiehadfficicbebeeegcebideijidbgdecffeaegjfjbbcfiabfbaiddbjgidebdiccfcgfbcbbfhaejaibeicghecchjbiaceaibfgibhgcfgifiedcbhhfadhccfdhejeggcah\r\njbadcfjffcfabbecfabgcafgfcgfeffjjhhdaajjgcbgbechhiadfahjidcdiefhbabhjhjijghghcgghcefhidhdgficiffdjgfdahcaicidfghiedgihbbjgicjeiacihdihfhadjhccddhigiibafiafficegaiehabafiiecbjcbfhdbeaebigaijehhdbfeehbcahaggbbdjcdbgbiajgeigdeabdbddbgcgjibfdgjghhdidjdhh\r\n", "output": "0\n"}, {"input": "100\r\nabababbbababbababaaabbbbaaaabbabbabbabababbbaaaabbababbbbababbabbbaaababababbbaaaabbbabbababbbbbbaba\r\nabababbbababbababaaabbbbaaaabbabbabbabababbbaaaabaababbbbababbabbbaaababababbbaaaabbbabbababbbbbbaba\r\n", "output": "2\n"}, {"input": "100\r\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n", "output": "2\n"}, {"input": "100\r\naaaaaaaaaaaaaaaaaaaaaalaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\naaaaaaaaaaaaaaaaaaaaaakaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n", "output": "2\n"}, {"input": "100\r\ndwtsrrtztfuibkrpwbxjrcxsonrwoydkmbhxrghekvusiyzqkyvulrvtfxmvrphpzpmazizogfbyauxtjfesocssnxvjjdedomlz\r\ndwtsrrtztfuibkrpwbxjrcxsonrwoydkmbhxrghekvusiyzqkyvulrvtfxmvrphpzpzazizogfbyauxtjfesocssnxvjjdedomlz\r\n", "output": "2\n"}, {"input": "100\r\naaabaabbbababbbaabbbbbaaababbabbaaabbabaabbabbabbbbbabbaaabbbbbbbbbbbbbbbababaaababbaaabeabbabaabbab\r\naaabaabbbababbbaabbbbbaaababbabbaaabbabaabbabbabbbbbabbaaabbbbbbbbbbbbbbbababaaababbaaabtabbabaabbab\r\n", "output": "2\n"}, {"input": "100\r\naaaabaaaaabbaababaaabaababaabbbaabaaabbbaaababbabaabbabababbaaabaabababbbababbbabbaaaabbbbbbbaaababa\r\naaaabaaaaabbaababaaabaababaabbbaabaaabbbaaaabbbabaabbabababbaaabaabababbbababbbabbaaaabbbbbbbaaababa\r\n", "output": "2\n"}, {"input": "100\r\neebdeddddbecdbddaaecbbaccbecdeacedddcaddcdebedbabbceeeadecadbbeaecdaeabbceacbdbdbbdacebbbccdcbbeedbe\r\needbeddddbecdbddaaecbbaccbecdeacedddcaddcdebedbabbceeeadecadbbeaecdaeabbceacbdbdbbdacebbbccdcbbeedbe\r\n", "output": "2\n"}, {"input": "100\r\nxjywrmrwqaytezhtqmcnrrjomslvcmevncvzeddnvqgkbusnbzrppdsuzsmcobmnslpvosunavayvdbxhtavvwodorwijxfjjlat\r\nxjywrmrwqaytezhtqmcrnrjomslvcmevncvzeddnvqgkbusnbzrppdsuzsmcobmnslpvosunavayvdbxhtavvwodorwijxfjjlat\r\n", "output": "2\n"}, {"input": "4\r\nbbca\r\nabab\r\n", "output": "0\n"}, {"input": "4\r\nabcb\r\nccac\r\n", "output": "0\n"}, {"input": "4\r\ncaaa\r\nabab\r\n", "output": "0\n"}, {"input": "4\r\nacca\r\nbabb\r\n", "output": "0\n"}, {"input": "4\r\nccba\r\nbabb\r\n", "output": "0\n"}, {"input": "4\r\nbcca\r\ncbaa\r\n", "output": "0\n"}, {"input": "4\r\naaca\r\ncaab\r\n", "output": "0\n"}, {"input": "4\r\nbaab\r\nbcbc\r\n", "output": "0\n"}, {"input": "4\r\nabba\r\ncaca\r\n", "output": "0\n"}, {"input": "4\r\nbcbb\r\nccac\r\n", "output": "0\n"}, {"input": "4\r\ncbba\r\nabba\r\n", "output": "2\n"}, {"input": "4\r\nbaca\r\nccbc\r\n", "output": "0\n"}, {"input": "4\r\ncabc\r\naacc\r\n", "output": "0\n"}, {"input": "4\r\nbbab\r\ncbaa\r\n", "output": "0\n"}, {"input": "4\r\nabcc\r\nbcab\r\n", "output": "0\n"}, {"input": "4\r\nbaaa\r\nbbbc\r\n", "output": "0\n"}, {"input": "4\r\naabc\r\naacb\r\n", "output": "2\n"}, {"input": "4\r\nccbb\r\nbbcb\r\n", "output": "0\n"}, {"input": "4\r\nbaba\r\naccc\r\n", "output": "0\n"}, {"input": "4\r\nbbbc\r\nbbab\r\n", "output": "1\n"}, {"input": "2\r\nab\r\nba\r\n", "output": "2\n"}, {"input": "5\r\ncabac\r\ncbabc\r\n", "output": "2\n"}, {"input": "3\r\naba\r\nbab\r\n", "output": "2\n"}, {"input": "5\r\nabxxx\r\nbayyy\r\n", "output": "0\n"}, {"input": "4\r\nxaxa\r\naxax\r\n", "output": "2\n"}, {"input": "5\r\nababa\r\nbabab\r\n", "output": "2\n"}, {"input": "5\r\nbabab\r\nababa\r\n", "output": "2\n"}, {"input": "154\r\nwqpewhyutqnhaewqpewhywqpewhyutqnhaeutqnhaeutqnhaewqpewhyutqnhaewqpewhywqpewhyutqnhaeutqnhaeutqnhaeutqnhaewqpewhyutqnhaewqpewhywqpewhywqpewhywqpewhyutqnhae\r\nutqnhaeutqnhaeutqnhaewqpewhywqpewhyutqnhaewqpewhyutqnhaewqpewhywqpewhyutqnhaeutqnhaeutqnhaewqpewhyutqnhaewqpewhywqpewhywqpewhyutqnhaewqpewhyutqnhaewqpewhy\r\n", "output": "0\n"}, {"input": "7\r\ntrading\r\nrtading\r\n", "output": "2\n"}, {"input": "5\r\nxabax\r\nxbabx\r\n", "output": "2\n"}, {"input": "3\r\nabc\r\nacb\r\n", "output": "2\n"}, {"input": "4\r\nabab\r\nbaba\r\n", "output": "2\n"}, {"input": "3\r\naab\r\naba\r\n", "output": "2\n"}, {"input": "2\r\ner\r\nre\r\n", "output": "2\n"}, {"input": "5\r\ntabat\r\ntbaat\r\n", "output": "2\n"}]
false
stdio
null
true
525/C
525
C
Python 3
TESTS
36
187
7,372,800
10601671
I = lambda : map(int,input().split()) n = int(input()) p = list(I()) p.sort(key=lambda x: -x) i = 0 sz = len(p) ans = 0 while True: if i + 3 >= sz: break if (p[i] - 1 == p[i + 1] or p[i] == p[i + 1]) and (p[i + 2] - 1 == p[i + 3] or p[i + 2] == p[i + 3]): ans += p[i + 1] * p[i + 3] i += 4 else: i += 1 print (ans)
96
140
7,680,000
15761396
def main(): input() res, it = 0, iter(sorted(map(int, input().split()), reverse=True)) try: while True: a, b, c, d = next(it), next(it), next(it), next(it) while a > b + 1: a, b, c, d = b, c, d, next(it) while c > d + 1: c, d = d, next(it) res += b * d except StopIteration: print(res) if __name__ == '__main__': main()
Codeforces Round 297 (Div. 2)
CF
2,015
2
256
Ilya and Sticks
In the evening, after the contest Ilya was bored, and he really felt like maximizing. He remembered that he had a set of n sticks and an instrument. Each stick is characterized by its length li. Ilya decided to make a rectangle from the sticks. And due to his whim, he decided to make rectangles in such a way that maximizes their total area. Each stick is used in making at most one rectangle, it is possible that some of sticks remain unused. Bending sticks is not allowed. Sticks with lengths a1, a2, a3 and a4 can make a rectangle if the following properties are observed: - a1 ≤ a2 ≤ a3 ≤ a4 - a1 = a2 - a3 = a4 A rectangle can be made of sticks with lengths of, for example, 3 3 3 3 or 2 2 4 4. A rectangle cannot be made of, for example, sticks 5 5 5 7. Ilya also has an instrument which can reduce the length of the sticks. The sticks are made of a special material, so the length of each stick can be reduced by at most one. For example, a stick with length 5 can either stay at this length or be transformed into a stick of length 4. You have to answer the question — what maximum total area of the rectangles can Ilya get with a file if makes rectangles from the available sticks?
The first line of the input contains a positive integer n (1 ≤ n ≤ 105) — the number of the available sticks. The second line of the input contains n positive integers li (2 ≤ li ≤ 106) — the lengths of the sticks.
The first line of the output must contain a single non-negative integer — the maximum total area of the rectangles that Ilya can make from the available sticks.
null
null
[{"input": "4\n2 4 4 2", "output": "8"}, {"input": "4\n2 2 3 5", "output": "0"}, {"input": "4\n100003 100004 100005 100006", "output": "10000800015"}]
1,600
["greedy", "math", "sortings"]
96
[{"input": "4\r\n2 4 4 2\r\n", "output": "8\r\n"}, {"input": "4\r\n2 2 3 5\r\n", "output": "0\r\n"}, {"input": "4\r\n100003 100004 100005 100006\r\n", "output": "10000800015\r\n"}, {"input": "8\r\n5 3 3 3 3 4 4 4\r\n", "output": "25\r\n"}, {"input": "10\r\n123 124 123 124 2 2 2 2 9 9\r\n", "output": "15270\r\n"}, {"input": "8\r\n10 10 10 10 11 10 11 10\r\n", "output": "210\r\n"}, {"input": "1\r\n1000000\r\n", "output": "0\r\n"}, {"input": "10\r\n10519 10519 10520 10520 10520 10521 10521 10521 10522 10523\r\n", "output": "221372362\r\n"}, {"input": "100\r\n4116 4116 4117 4117 4117 4117 4118 4119 4119 4119 4119 4120 4120 4120 4120 4121 4122 4123 4123 4123 4123 4124 4124 4124 4124 4125 4126 4126 4126 4126 4127 4127 4127 4127 4128 4128 4128 4128 4129 4129 4130 4130 4131 4132 4133 4133 4134 4134 4135 4135 4136 4137 4137 4137 4138 4139 4140 4140 4141 4141 4142 4143 4143 4143 4144 4144 4144 4144 4145 4145 4145 4146 4146 4146 4147 4147 4147 4147 4148 4148 4148 4149 4149 4149 4150 4151 4151 4151 4152 4152 4153 4153 4154 4154 4155 4155 4155 4155 4156 4156\r\n", "output": "427591742\r\n"}, {"input": "10\r\n402840 873316 567766 493234 711262 291654 683001 496971 64909 190173\r\n", "output": "0\r\n"}, {"input": "45\r\n1800 4967 1094 551 871 3505 846 960 4868 4304 2112 496 2293 2128 2430 2119 4497 2159 774 4520 3535 1013 452 1458 1895 1191 958 1133 416 2613 4172 3926 1665 4237 539 101 2448 1212 2631 4530 3026 412 1006 2515 1922\r\n", "output": "0\r\n"}, {"input": "69\r\n2367 2018 3511 1047 1789 2332 1082 4678 2036 4108 2357 339 536 2272 3638 2588 754 3795 375 506 3243 1033 4531 1216 4266 2547 3540 4642 1256 2248 4705 14 629 876 2304 1673 4186 2356 3172 2664 3896 552 4293 1507 3307 2661 3143 4565 58 1298 4380 2738 917 2054 2676 4464 1314 3752 3378 1823 4219 3142 4258 1833 886 4286 4040 1070 2206\r\n", "output": "7402552\r\n"}, {"input": "93\r\n13 2633 3005 1516 2681 3262 1318 1935 665 2450 2601 1644 214 929 4873 955 1983 3945 3488 2927 1516 1026 2150 974 150 2442 2610 1664 636 3369 266 2536 3132 2515 2582 1169 4462 4961 200 2848 4793 2795 4657 474 2640 2488 378 544 1805 1390 1548 2683 1474 4027 1724 2078 183 3717 1727 1780 552 2905 4260 1444 2906 3779 400 1491 1467 4480 3680 2539 4681 2875 4021 2711 106 853 3094 4531 4066 372 2129 2577 3996 2350 943 4478 3058 3333 4592 232 2780\r\n", "output": "4403980\r\n"}, {"input": "21\r\n580 3221 3987 2012 35 629 1554 654 756 2254 4307 2948 3457 4612 4620 4320 1777 556 3088 348 1250\r\n", "output": "0\r\n"}, {"input": "45\r\n4685 272 3481 3942 952 3020 329 4371 2923 2057 4526 2791 1674 3269 829 2713 3006 2166 1228 2795 983 1065 3875 4028 3429 3720 697 734 4393 1176 2820 1173 4598 2281 2549 4341 1504 172 4230 1193 3022 3742 1232 3433 1871\r\n", "output": "0\r\n"}, {"input": "69\r\n3766 2348 4437 4438 3305 386 2026 1629 1552 400 4770 4069 4916 1926 2037 1079 2801 854 803 216 2152 4622 1494 3786 775 3615 4766 2781 235 836 1892 2234 3563 1843 4314 3836 320 2776 4796 1378 380 2421 3057 964 4717 1122 620 530 3455 1639 1618 3109 3120 564 2382 1995 1173 4510 286 1088 218 734 2779 3738 456 1668 4476 2780 3555\r\n", "output": "12334860\r\n"}, {"input": "4\r\n2 2 2 4\r\n", "output": "0\r\n"}, {"input": "8\r\n10 10 10 11 14 14 14 16\r\n", "output": "140\r\n"}, {"input": "2\r\n2 3\r\n", "output": "0\r\n"}, {"input": "3\r\n2 3 5\r\n", "output": "0\r\n"}, {"input": "8\r\n2 1000000 2 1000000 2 1000000 2 1000000\r\n", "output": "1000000000004\r\n"}, {"input": "4\r\n2 4 6 8\r\n", "output": "0\r\n"}, {"input": "4\r\n2 3 6 8\r\n", "output": "0\r\n"}, {"input": "5\r\n2 2 3 4 5\r\n", "output": "8\r\n"}, {"input": "5\r\n1000000 999999 999999 999999 999999\r\n", "output": "999998000001\r\n"}, {"input": "6\r\n2 2 2 2 2 2\r\n", "output": "4\r\n"}, {"input": "4\r\n2 4 5 5\r\n", "output": "0\r\n"}, {"input": "20\r\n4 4 8 4 5 6 7 4 5 4 6 4 4 5 7 6 5 8 8 4\r\n", "output": "149\r\n"}, {"input": "10\r\n8 4 6 6 8 5 7 7 6 8\r\n", "output": "92\r\n"}, {"input": "11\r\n4 4 9 9 3 8 8 8 6 4 3\r\n", "output": "84\r\n"}, {"input": "8\r\n2 3 3 4 4 5 5 5\r\n", "output": "26\r\n"}, {"input": "4\r\n3 3 3 2\r\n", "output": "6\r\n"}, {"input": "5\r\n3 3 10 100 100\r\n", "output": "300\r\n"}, {"input": "8\r\n9 9 9 8 8 7 7 6\r\n", "output": "114\r\n"}, {"input": "4\r\n5 6 6 7\r\n", "output": "30\r\n"}, {"input": "5\r\n9 9 5 2 2\r\n", "output": "18\r\n"}, {"input": "6\r\n3 4 100 200 1001 1002\r\n", "output": "3003\r\n"}, {"input": "6\r\n3 4 5 100 101 102\r\n", "output": "404\r\n"}, {"input": "5\r\n2 2 4 6 6\r\n", "output": "12\r\n"}, {"input": "6\r\n2 3 8 10 13 14\r\n", "output": "26\r\n"}, {"input": "7\r\n2 2 2 2 2 2 2\r\n", "output": "4\r\n"}, {"input": "5\r\n5 2 2 2 2\r\n", "output": "4\r\n"}, {"input": "6\r\n3 4 100 200 1000 1001\r\n", "output": "3000\r\n"}, {"input": "5\r\n5 5 7 9 9\r\n", "output": "45\r\n"}, {"input": "5\r\n8 8 7 4 4\r\n", "output": "32\r\n"}, {"input": "5\r\n2 2 5 8 9\r\n", "output": "16\r\n"}, {"input": "5\r\n4 4 4 2 2\r\n", "output": "8\r\n"}, {"input": "5\r\n3 10 100 1000 10000\r\n", "output": "0\r\n"}, {"input": "6\r\n10 10 7 4 2 2\r\n", "output": "20\r\n"}, {"input": "6\r\n5 5 7 9 10 10\r\n", "output": "50\r\n"}, {"input": "7\r\n10 10 7 5 3 2 2\r\n", "output": "20\r\n"}, {"input": "7\r\n10 9 9 9 9 2 2\r\n", "output": "81\r\n"}]
false
stdio
null
true
362/B
362
B
Python 3
TESTS
13
77
409,600
119124070
from sys import * from math import * from sys import stdin,stdout from collections import * 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) n,m = get_int() arr = int_arr() arr.sort() if 1 in arr or n in arr: print('NO') elif m == 0: print('YES') else: prev = arr[0] flag = 1 ct = 1 for i in range(1,m): if arr[i] == prev + 1: prev = arr[i] ct += 1 if ct == 3: flag = 0 print('NO') break else: ct = 1 if flag: print('YES')
49
46
0
138029163
n, m = map(int, input().split()) if m == 0: print("YES") exit() d = sorted(list(map(int, input().split()))) if 1 in d or n in d: print("NO") exit() for i in range(m-2): if d[i+1] == d[i]+1 == d[i+2]-1: print("NO") exit() print("YES")
Codeforces Round 212 (Div. 2)
CF
2,013
1
256
Petya and Staircases
Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them. Now Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once. One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.
The first line contains two integers n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 3000) — the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 ≤ di ≤ n) — the numbers of the dirty stairs (in an arbitrary order).
Print "YES" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print "NO".
null
null
[{"input": "10 5\n2 4 8 3 6", "output": "NO"}, {"input": "10 5\n2 4 5 7 9", "output": "YES"}]
1,100
["implementation", "sortings"]
49
[{"input": "10 5\r\n2 4 8 3 6\r\n", "output": "NO"}, {"input": "10 5\r\n2 4 5 7 9\r\n", "output": "YES"}, {"input": "10 9\r\n2 3 4 5 6 7 8 9 10\r\n", "output": "NO"}, {"input": "5 2\r\n4 5\r\n", "output": "NO"}, {"input": "123 13\r\n36 73 111 2 92 5 47 55 48 113 7 78 37\r\n", "output": "YES"}, {"input": "10 10\r\n7 6 4 2 5 10 8 3 9 1\r\n", "output": "NO"}, {"input": "12312 0\r\n", "output": "YES"}, {"input": "9817239 1\r\n6323187\r\n", "output": "YES"}, {"input": "1 1\r\n1\r\n", "output": "NO"}, {"input": "5 4\r\n4 2 5 1\r\n", "output": "NO"}, {"input": "5 3\r\n4 3 5\r\n", "output": "NO"}, {"input": "500 3\r\n18 62 445\r\n", "output": "YES"}, {"input": "500 50\r\n72 474 467 241 442 437 336 234 410 120 438 164 405 177 142 114 27 20 445 235 46 176 88 488 242 391 28 414 145 92 206 334 152 343 367 254 100 243 155 348 148 450 461 483 97 34 471 69 416 362\r\n", "output": "NO"}, {"input": "500 8\r\n365 313 338 410 482 417 325 384\r\n", "output": "YES"}, {"input": "1000000000 10\r\n2 3 5 6 8 9 123 874 1230 1000000000\r\n", "output": "NO"}, {"input": "1000000000 10\r\n1 2 3 5 6 8 9 123 874 1230\r\n", "output": "NO"}, {"input": "10 1\r\n1\r\n", "output": "NO"}, {"input": "10 4\r\n1 2 4 5\r\n", "output": "NO"}, {"input": "50 20\r\n22 33 17 23 27 5 26 31 41 20 8 24 6 3 4 29 40 25 13 16\r\n", "output": "NO"}, {"input": "50 40\r\n14 27 19 30 31 20 28 11 37 29 23 33 7 26 22 16 1 6 18 3 47 36 38 2 48 9 41 8 5 50 4 45 44 25 39 12 43 42 40 46\r\n", "output": "NO"}, {"input": "123 12\r\n35 95 47 99 79 122 58 94 31 57 18 10\r\n", "output": "YES"}, {"input": "10 5\r\n1 3 5 7 9\r\n", "output": "NO"}, {"input": "100 7\r\n2 3 5 6 8 9 100\r\n", "output": "NO"}, {"input": "100 3\r\n98 99 100\r\n", "output": "NO"}, {"input": "100 3\r\n97 98 99\r\n", "output": "NO"}, {"input": "100 3\r\n96 98 99\r\n", "output": "YES"}, {"input": "10 6\r\n2 3 5 6 8 9\r\n", "output": "YES"}, {"input": "1000000000 10\r\n2 4 10 18 40 42 49 58 59 60\r\n", "output": "NO"}, {"input": "10 3\r\n1 4 6\r\n", "output": "NO"}, {"input": "8 3\r\n2 3 4\r\n", "output": "NO"}, {"input": "100 3\r\n4 5 6\r\n", "output": "NO"}, {"input": "10 2\r\n10 1\r\n", "output": "NO"}, {"input": "10 1\r\n10\r\n", "output": "NO"}, {"input": "4 2\r\n2 3\r\n", "output": "YES"}, {"input": "2 1\r\n1\r\n", "output": "NO"}, {"input": "2 0\r\n", "output": "YES"}, {"input": "4 3\r\n2 3 4\r\n", "output": "NO"}, {"input": "5 3\r\n4 2 3\r\n", "output": "NO"}]
false
stdio
null
true