source
stringclasses
4 values
task_type
stringclasses
1 value
in_source_id
stringlengths
0
138
problem
stringlengths
219
13.2k
gold_standard_solution
stringlengths
0
413k
problem_id
stringlengths
5
10
metadata
dict
verification_info
dict
apps
verifiable_code
998
Solve the following coding problem using the programming language python: Given two integers $n$ and $x$, construct an array that satisfies the following conditions: for any element $a_i$ in the array, $1 \le a_i<2^n$; there is no non-empty subsegment with bitwise XOR equal to $0$ or $x$, its length $l$ should be maximized. A sequence $b$ is a subsegment of a sequence $a$ if $b$ can be obtained from $a$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. -----Input----- The only line contains two integers $n$ and $x$ ($1 \le n \le 18$, $1 \le x<2^{18}$). -----Output----- The first line should contain the length of the array $l$. If $l$ is positive, the second line should contain $l$ space-separated integers $a_1$, $a_2$, $\dots$, $a_l$ ($1 \le a_i < 2^n$) — the elements of the array $a$. If there are multiple solutions, print any of them. -----Examples----- Input 3 5 Output 3 6 1 3 Input 2 4 Output 3 1 3 1 Input 1 1 Output 0 -----Note----- In the first example, the bitwise XOR of the subsegments are $\{6,7,4,1,2,3\}$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, x = list(map(int, input().split())) a = [] lst = 0 for i in range(1, 1 << n): if i ^ x > i: a.append(i ^ lst) lst = i print(len(a)) print(' '.join(str(i) for i in a)) ```
vfc_13746
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1174/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 5\n", "output": "3\n6 1 3", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 4\n", "output": "3\n1 3 1 ", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 12\n", "output": "7\n1 3 1 7 1 3 1 ", "type": "stdin_stdout" } ] }
apps
verifiable_code
999
Solve the following coding problem using the programming language python: Anton likes to play chess. Also he likes to do programming. No wonder that he decided to attend chess classes and programming classes. Anton has n variants when he will attend chess classes, i-th variant is given by a period of time (l_{1, }i, r_{1, }i). Also he has m variants when he will attend programming classes, i-th variant is given by a period of time (l_{2, }i, r_{2, }i). Anton needs to choose exactly one of n possible periods of time when he will attend chess classes and exactly one of m possible periods of time when he will attend programming classes. He wants to have a rest between classes, so from all the possible pairs of the periods he wants to choose the one where the distance between the periods is maximal. The distance between periods (l_1, r_1) and (l_2, r_2) is the minimal possible distance between a point in the first period and a point in the second period, that is the minimal possible |i - j|, where l_1 ≤ i ≤ r_1 and l_2 ≤ j ≤ r_2. In particular, when the periods intersect, the distance between them is 0. Anton wants to know how much time his rest between the classes will last in the best case. Help Anton and find this number! -----Input----- The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of time periods when Anton can attend chess classes. Each of the following n lines of the input contains two integers l_{1, }i and r_{1, }i (1 ≤ l_{1, }i ≤ r_{1, }i ≤ 10^9) — the i-th variant of a period of time when Anton can attend chess classes. The following line of the input contains a single integer m (1 ≤ m ≤ 200 000) — the number of time periods when Anton can attend programming classes. Each of the following m lines of the input contains two integers l_{2, }i and r_{2, }i (1 ≤ l_{2, }i ≤ r_{2, }i ≤ 10^9) — the i-th variant of a period of time when Anton can attend programming classes. -----Output----- Output one integer — the maximal possible distance between time periods. -----Examples----- Input 3 1 5 2 6 2 3 2 2 4 6 8 Output 3 Input 3 1 5 2 6 3 7 2 2 4 1 4 Output 0 -----Note----- In the first sample Anton can attend chess classes in the period (2, 3) and attend programming classes in the period (6, 8). It's not hard to see that in this case the distance between the periods will be equal to 3. In the second sample if he chooses any pair of periods, they will intersect. So the answer is 0. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import math import itertools def ria(): return [int(i) for i in input().split()] mx = 0 sz = ria()[0] mx1 = 0 mn1 = 2000000000 for i in range(sz): l, r = ria() mx1 = max(l, mx1) mn1 = min(r, mn1) sz = ria()[0] mx2 = 0 mn2 = 2000000000 for i in range(sz): l, r = ria() mx2 = max(l, mx2) mn2 = min(r, mn2) #print(mx1,mn1,mx2,mn2) mx = max(mx, mx1 - mn2) mx = max(mx, mx2 - mn1) print(mx) ```
vfc_13750
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/785/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n1 5\n2 6\n2 3\n2\n2 4\n6 8\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n1 5\n2 6\n3 7\n2\n2 4\n1 4\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "20\n13 141\n57 144\n82 124\n16 23\n18 44\n64 65\n117 133\n84 117\n77 142\n40 119\n105 120\n71 92\n5 142\n48 132\n106 121\n5 80\n45 92\n66 81\n7 93\n27 71\n3\n75 96\n127 140\n54 74\n", "output": "104\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n16 16\n20 20\n13 13\n31 31\n42 42\n70 70\n64 64\n63 63\n53 53\n94 94\n8\n3 3\n63 63\n9 9\n25 25\n11 11\n93 93\n47 47\n3 3\n", "output": "91\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n45888636 261444238\n1\n244581813 591222338\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n166903016 182235583\n1\n254223764 902875046\n", "output": "71988181\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1000
Solve the following coding problem using the programming language python: Sasha is a very happy guy, that's why he is always on the move. There are $n$ cities in the country where Sasha lives. They are all located on one straight line, and for convenience, they are numbered from $1$ to $n$ in increasing order. The distance between any two adjacent cities is equal to $1$ kilometer. Since all roads in the country are directed, it's possible to reach the city $y$ from the city $x$ only if $x < y$. Once Sasha decided to go on a trip around the country and to visit all $n$ cities. He will move with the help of his car, Cheetah-2677. The tank capacity of this model is $v$ liters, and it spends exactly $1$ liter of fuel for $1$ kilometer of the way. At the beginning of the journey, the tank is empty. Sasha is located in the city with the number $1$ and wants to get to the city with the number $n$. There is a gas station in each city. In the $i$-th city, the price of $1$ liter of fuel is $i$ dollars. It is obvious that at any moment of time, the tank can contain at most $v$ liters of fuel. Sasha doesn't like to waste money, that's why he wants to know what is the minimum amount of money is needed to finish the trip if he can buy fuel in any city he wants. Help him to figure it out! -----Input----- The first line contains two integers $n$ and $v$ ($2 \le n \le 100$, $1 \le v \le 100$)  — the number of cities in the country and the capacity of the tank. -----Output----- Print one integer — the minimum amount of money that is needed to finish the trip. -----Examples----- Input 4 2 Output 4 Input 7 6 Output 6 -----Note----- In the first example, Sasha can buy $2$ liters for $2$ dollars ($1$ dollar per liter) in the first city, drive to the second city, spend $1$ liter of fuel on it, then buy $1$ liter for $2$ dollars in the second city and then drive to the $4$-th city. Therefore, the answer is $1+1+2=4$. In the second example, the capacity of the tank allows to fill the tank completely in the first city, and drive to the last city without stops in other cities. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n,v = map(int,input().split()) req = n-1 if req<=v: print (req) else: total = v remaining = req-v for x in range(remaining): total += 2+x print (total) ```
vfc_13754
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1113/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4 2\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7 6\n", "output": "6\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 3\n", "output": "30\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "12 89\n", "output": "11\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "32 15\n", "output": "167\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1001
Solve the following coding problem using the programming language python: Once upon a time Petya and Gena gathered after another programming competition and decided to play some game. As they consider most modern games to be boring, they always try to invent their own games. They have only stickers and markers, but that won't stop them. The game they came up with has the following rules. Initially, there are n stickers on the wall arranged in a row. Each sticker has some number written on it. Now they alternate turn, Petya moves first. One move happens as follows. Lets say there are m ≥ 2 stickers on the wall. The player, who makes the current move, picks some integer k from 2 to m and takes k leftmost stickers (removes them from the wall). After that he makes the new sticker, puts it to the left end of the row, and writes on it the new integer, equal to the sum of all stickers he took on this move. Game ends when there is only one sticker left on the wall. The score of the player is equal to the sum of integers written on all stickers he took during all his moves. The goal of each player is to maximize the difference between his score and the score of his opponent. Given the integer n and the initial sequence of stickers on the wall, define the result of the game, i.e. the difference between the Petya's and Gena's score if both players play optimally. -----Input----- The first line of input contains a single integer n (2 ≤ n ≤ 200 000) — the number of stickers, initially located on the wall. The second line contains n integers a_1, a_2, ..., a_{n} ( - 10 000 ≤ a_{i} ≤ 10 000) — the numbers on stickers in order from left to right. -----Output----- Print one integer — the difference between the Petya's score and Gena's score at the end of the game if both players play optimally. -----Examples----- Input 3 2 4 8 Output 14 Input 4 1 -7 -2 3 Output -3 -----Note----- In the first sample, the optimal move for Petya is to take all the stickers. As a result, his score will be equal to 14 and Gena's score will be equal to 0. In the second sample, the optimal sequence of moves is the following. On the first move Petya will take first three sticker and will put the new sticker with value - 8. On the second move Gena will take the remaining two stickers. The Petya's score is 1 + ( - 7) + ( - 2) = - 8, Gena's score is ( - 8) + 3 = - 5, i.e. the score difference will be - 3. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = int(input()) a = [0] * n a = list(map(int, input().split())) for i in range(1, len(a)): a[i] += a[i - 1] ans = a[-1] for i in range(n - 2, 0, -1): ans = max(ans, a[i] - ans) print(ans) ```
vfc_13758
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/731/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n2 4 8\n", "output": "14\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n1 -7 -2 3\n", "output": "-3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n35 11 35 28 48 25 2 43 23 10\n", "output": "260\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "100\n437 89 481 95 29 326 10 304 97 414 52 46 106 181 385 173 337 148 437 133 52 136 86 250 289 61 480 314 166 69 275 486 117 129 353 412 382 469 290 479 388 231 7 462 247 432 488 146 466 422 369 336 148 460 418 356 303 149 421 146 233 224 432 239 392 409 172 331 152 433 345 205 451 138 273 284 48 109 294 281 468 301 337 176 137 52 216 79 431 141 147 240 107 184 393 459 286 123 297 160\n", "output": "26149\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n2 1 2\n", "output": "5\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1002
Solve the following coding problem using the programming language python: Devu is a renowned classical singer. He is invited to many big functions/festivals. Recently he was invited to "All World Classical Singing Festival". Other than Devu, comedian Churu was also invited. Devu has provided organizers a list of the songs and required time for singing them. He will sing n songs, i^{th} song will take t_{i} minutes exactly. The Comedian, Churu will crack jokes. All his jokes are of 5 minutes exactly. People have mainly come to listen Devu. But you know that he needs rest of 10 minutes after each song. On the other hand, Churu being a very active person, doesn't need any rest. You as one of the organizers should make an optimal sсhedule for the event. For some reasons you must follow the conditions: The duration of the event must be no more than d minutes; Devu must complete all his songs; With satisfying the two previous conditions the number of jokes cracked by Churu should be as many as possible. If it is not possible to find a way to conduct all the songs of the Devu, output -1. Otherwise find out maximum number of jokes that Churu can crack in the grand event. -----Input----- The first line contains two space separated integers n, d (1 ≤ n ≤ 100; 1 ≤ d ≤ 10000). The second line contains n space-separated integers: t_1, t_2, ..., t_{n} (1 ≤ t_{i} ≤ 100). -----Output----- If there is no way to conduct all the songs of Devu, output -1. Otherwise output the maximum number of jokes that Churu can crack in the grand event. -----Examples----- Input 3 30 2 2 1 Output 5 Input 3 20 2 1 1 Output -1 -----Note----- Consider the first example. The duration of the event is 30 minutes. There could be maximum 5 jokes in the following way: First Churu cracks a joke in 5 minutes. Then Devu performs the first song for 2 minutes. Then Churu cracks 2 jokes in 10 minutes. Now Devu performs second song for 2 minutes. Then Churu cracks 2 jokes in 10 minutes. Now finally Devu will perform his last song in 1 minutes. Total time spent is 5 + 2 + 10 + 2 + 10 + 1 = 30 minutes. Consider the second example. There is no way of organizing Devu's all songs. Hence the answer is -1. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python """ Codeforces Round 251 Div 2 Problem A Author : chaotic_iak Language: Python 3.3.4 """ def read(mode=2): # 0: String # 1: List of strings # 2: List of integers inputs = input().strip() if mode == 0: return inputs if mode == 1: return inputs.split() if mode == 2: return [int(x) for x in inputs.split()] def write(s="\n"): if isinstance(s, list): s = " ".join(s) s = str(s) print(s, end="") ################################################### SOLUTION n,d = read() t = read() s = sum(t) + 10*n - 10 if s > d: print(-1) else: print((d-sum(t))//5) ```
vfc_13762
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/439/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 30\n2 2 1\n", "output": "5\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1003
Solve the following coding problem using the programming language python: Vasya has n pairs of socks. In the morning of each day Vasya has to put on a pair of socks before he goes to school. When he comes home in the evening, Vasya takes off the used socks and throws them away. Every m-th day (at days with numbers m, 2m, 3m, ...) mom buys a pair of socks to Vasya. She does it late in the evening, so that Vasya cannot put on a new pair of socks before the next day. How many consecutive days pass until Vasya runs out of socks? -----Input----- The single line contains two integers n and m (1 ≤ n ≤ 100; 2 ≤ m ≤ 100), separated by a space. -----Output----- Print a single integer — the answer to the problem. -----Examples----- Input 2 2 Output 3 Input 9 3 Output 13 -----Note----- In the first sample Vasya spends the first two days wearing the socks that he had initially. Then on day three he puts on the socks that were bought on day two. In the second sample Vasya spends the first nine days wearing the socks that he had initially. Then he spends three days wearing the socks that were bought on the third, sixth and ninth days. Than he spends another day wearing the socks that were bought on the twelfth day. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python """ Codeforces Contest 262 Div 2 Problem A Author : chaotic_iak Language: Python 3.3.4 """ def main(): n,m = read() i = 0 while n: n -= 1 i += 1 if not i%m: n += 1 print(i) ################################### NON-SOLUTION STUFF BELOW def read(mode=2): # 0: String # 1: List of strings # 2: List of integers inputs = input().strip() if mode == 0: return inputs if mode == 1: return inputs.split() if mode == 2: return list(map(int, inputs.split())) def write(s="\n"): if s is None: s = "" if isinstance(s, list): s = " ".join(map(str, s)) s = str(s) print(s, end="") write(main()) ```
vfc_13766
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/460/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2 2\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "9 3\n", "output": "13\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 2\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 3\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 99\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 4\n", "output": "5\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1004
Solve the following coding problem using the programming language python: The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys from collections import Counter readline = sys.stdin.readline N = int(readline()) A = list(map(int, readline().split())) S = set() ans = True Ans = [] cnt = 0 for a in A: cnt += 1 if a > 0: S.add(a) else: a = -a if a not in S: ans = False break S.remove(a) if not S: Ans.append(cnt) cnt = 0 if cnt: ans = False if ans: A.reverse() for c in Ans: CA = Counter() for _ in range(c): CA[abs(A.pop())] += 1 if any(v > 2 for v in CA.values()): ans = False break if ans: print(len(Ans)) print(*Ans) else: print(-1) ```
vfc_13770
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1253/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6\n1 7 -7 3 -1 -3\n", "output": "1\n6\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "8\n1 -1 1 2 -1 -2 3 -3\n", "output": "3\n2 4 2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n2 5 -5 5 -5 -2\n", "output": "-1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1006
Solve the following coding problem using the programming language python: Fox Ciel has a board with n rows and n columns. So, the board consists of n × n cells. Each cell contains either a symbol '.', or a symbol '#'. A cross on the board is a connected set of exactly five cells of the board that looks like a cross. The picture below shows how it looks.[Image] Ciel wants to draw several (may be zero) crosses on the board. Each cross must cover exactly five cells with symbols '#', and any cell with symbol '#' must belong to some cross. No two crosses can share a cell. Please, tell Ciel if she can draw the crosses in the described way. -----Input----- The first line contains an integer n (3 ≤ n ≤ 100) — the size of the board. Each of the next n lines describes one row of the board. The i-th line describes the i-th row of the board and consists of n characters. Each character is either a symbol '.', or a symbol '#'. -----Output----- Output a single line with "YES" if Ciel can draw the crosses in the described way. Otherwise output a single line with "NO". -----Examples----- Input 5 .#... ####. .#### ...#. ..... Output YES Input 4 #### #### #### #### Output NO Input 6 .#.... ####.. .####. .#.##. ###### .#..#. Output YES Input 6 .#..#. ###### .####. .####. ###### .#..#. Output NO Input 3 ... ... ... Output YES -----Note----- In example 1, you can draw two crosses. The picture below shows what they look like.[Image] In example 2, the board contains 16 cells with '#', but each cross contains 5. Since 16 is not a multiple of 5, so it's impossible to cover all. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = int(input()) arr = [] for i in range(n): arr.append(list(input())) k = 1 for i in range(n): for j in range(n): if arr[i][j] == '#': if i+2<=n-1 and j-1>=0 and j+1<=n-1: if arr[i+1][j-1] == '#' and arr[i+1][j] == '#' and arr[i+1][j+1] == '#' and arr[i+2][j] == '#': arr[i+1][j-1] = '.' arr[i+1][j] = '.' arr[i+1][j+1] = '.' arr[i+2][j] = '.' arr[i][j] = '.' else: k = 0 break else: k = 0 break if k == 1: print('YES') else: print('NO') ```
vfc_13778
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/389/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n.#...\n####.\n.####\n...#.\n.....\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n####\n####\n####\n####\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n.#....\n####..\n.####.\n.#.##.\n######\n.#..#.\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n.#..#.\n######\n.####.\n.####.\n######\n.#..#.\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n...\n...\n...\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n.....\n.#...\n####.\n.####\n...#.\n", "output": "YES\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1007
Solve the following coding problem using the programming language python: — Thanks a lot for today. — I experienced so many great things. — You gave me memories like dreams... But I have to leave now... — One last request, can you... — Help me solve a Codeforces problem? — ...... — What? Chtholly has been thinking about a problem for days: If a number is palindrome and length of its decimal representation without leading zeros is even, we call it a zcy number. A number is palindrome means when written in decimal representation, it contains no leading zeros and reads the same forwards and backwards. For example 12321 and 1221 are palindromes and 123 and 12451 are not. Moreover, 1221 is zcy number and 12321 is not. Given integers k and p, calculate the sum of the k smallest zcy numbers and output this sum modulo p. Unfortunately, Willem isn't good at solving this kind of problems, so he asks you for help! -----Input----- The first line contains two integers k and p (1 ≤ k ≤ 10^5, 1 ≤ p ≤ 10^9). -----Output----- Output single integer — answer to the problem. -----Examples----- Input 2 100 Output 33 Input 5 30 Output 15 -----Note----- In the first example, the smallest zcy number is 11, and the second smallest zcy number is 22. In the second example, $(11 + 22 + 33 + 44 + 55) \operatorname{mod} 30 = 15$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from itertools import * k, p = list(map(int, input().split())) ss = 0 for i in range(1, k + 1): s = str(i) num = int(s + ''.join(reversed(s))) ss += num ss %= p print(ss) ```
vfc_13782
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/897/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2 100\n", "output": "33\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1008
Solve the following coding problem using the programming language python: While Mike was walking in the subway, all the stuff in his back-bag dropped on the ground. There were several fax messages among them. He concatenated these strings in some order and now he has string s. [Image] He is not sure if this is his own back-bag or someone else's. He remembered that there were exactly k messages in his own bag, each was a palindrome string and all those strings had the same length. He asked you to help him and tell him if he has worn his own back-bag. Check if the given string s is a concatenation of k palindromes of the same length. -----Input----- The first line of input contains string s containing lowercase English letters (1 ≤ |s| ≤ 1000). The second line contains integer k (1 ≤ k ≤ 1000). -----Output----- Print "YES"(without quotes) if he has worn his own back-bag or "NO"(without quotes) otherwise. -----Examples----- Input saba 2 Output NO Input saddastavvat 2 Output YES -----Note----- Palindrome is a string reading the same forward and backward. In the second sample, the faxes in his back-bag can be "saddas" and "tavvat". The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def check(pali): for i in range(len(pali) // 2): if pali[i] != pali[-i - 1]: return False return True def __starting_point(): s = input() k = int(input()) if len(s) % k != 0: print('NO') return step = len(s) // k for i in range(k): if not check(s[i * step: (i + 1) * step]): print('NO') return print('YES') __starting_point() ```
vfc_13786
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/548/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "saba\n2\n", "output": "NO\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1009
Solve the following coding problem using the programming language python: Kevin Sun wants to move his precious collection of n cowbells from Naperthrill to Exeter, where there is actually grass instead of corn. Before moving, he must pack his cowbells into k boxes of a fixed size. In order to keep his collection safe during transportation, he won't place more than two cowbells into a single box. Since Kevin wishes to minimize expenses, he is curious about the smallest size box he can use to pack his entire collection. Kevin is a meticulous cowbell collector and knows that the size of his i-th (1 ≤ i ≤ n) cowbell is an integer s_{i}. In fact, he keeps his cowbells sorted by size, so s_{i} - 1 ≤ s_{i} for any i > 1. Also an expert packer, Kevin can fit one or two cowbells into a box of size s if and only if the sum of their sizes does not exceed s. Given this information, help Kevin determine the smallest s for which it is possible to put all of his cowbells into k boxes of size s. -----Input----- The first line of the input contains two space-separated integers n and k (1 ≤ n ≤ 2·k ≤ 100 000), denoting the number of cowbells and the number of boxes, respectively. The next line contains n space-separated integers s_1, s_2, ..., s_{n} (1 ≤ s_1 ≤ s_2 ≤ ... ≤ s_{n} ≤ 1 000 000), the sizes of Kevin's cowbells. It is guaranteed that the sizes s_{i} are given in non-decreasing order. -----Output----- Print a single integer, the smallest s for which it is possible for Kevin to put all of his cowbells into k boxes of size s. -----Examples----- Input 2 1 2 5 Output 7 Input 4 3 2 3 5 9 Output 9 Input 3 2 3 5 7 Output 8 -----Note----- In the first sample, Kevin must pack his two cowbells into the same box. In the second sample, Kevin can pack together the following sets of cowbells: {2, 3}, {5} and {9}. In the third sample, the optimal solution is {3, 5} and {7}. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, k = list(map(int,input().split())) L = list(map(int,input().split())) i = 0 p = 0 z = 1 R = [0 for _ in range(k)] while i<n: R[p] += L[n-1-i] p = p + z i+=1 if p == k or p == -1: z = z*(-1) if p == k: p = p - 1 else: p = p + 1 print(max(R)) ```
vfc_13790
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/604/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2 1\n2 5\n", "output": "7\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 3\n2 3 5 9\n", "output": "9\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 2\n3 5 7\n", "output": "8\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "20 11\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 10\n3 15 31 61 63 63 68 94 98 100\n", "output": "100\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1010
Solve the following coding problem using the programming language python: Bob loves everything sweet. His favorite chocolate bar consists of pieces, each piece may contain a nut. Bob wants to break the bar of chocolate into multiple pieces so that each part would contain exactly one nut and any break line goes between two adjacent pieces. You are asked to calculate the number of ways he can do it. Two ways to break chocolate are considered distinct if one of them contains a break between some two adjacent pieces and the other one doesn't. Please note, that if Bob doesn't make any breaks, all the bar will form one piece and it still has to have exactly one nut. -----Input----- The first line of the input contains integer n (1 ≤ n ≤ 100) — the number of pieces in the chocolate bar. The second line contains n integers a_{i} (0 ≤ a_{i} ≤ 1), where 0 represents a piece without the nut and 1 stands for a piece with the nut. -----Output----- Print the number of ways to break the chocolate into multiple parts so that each part would contain exactly one nut. -----Examples----- Input 3 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 4 -----Note----- In the first sample there is exactly one nut, so the number of ways equals 1 — Bob shouldn't make any breaks. In the second sample you can break the bar in four ways: 10|10|1 1|010|1 10|1|01 1|01|01 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python x = int(input()) y = list(map(int, input().split(' '))) if y == [0] * x: print(0) quit() for i in range(x): if y[i] == 1: y = y[i:] break y.reverse() for i in range(len(y)): if y[i] == 1: y = y[i:] break y.reverse() l = [] ct = 0 for i in y: if i == 0: ct+=1 if i == 1 and ct != 0: l.append(ct) ct = 0 k = 1 for i in l: k *= (i+1) print(k) ```
vfc_13794
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/617/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n0 1 0\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n1 0 1 0 1\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n0 0 1 0 0 0 1 1 0 1\n", "output": "8\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "20\n0 0 0 0 1 1 1 0 1 0 0 1 0 1 1 0 1 1 1 0\n", "output": "24\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1011
Solve the following coding problem using the programming language python: Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer. Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that. -----Input----- The first line contains integer n (1 ≤ n ≤ 2·10^5) — the number of throws of the first team. Then follow n integer numbers — the distances of throws a_{i} (1 ≤ a_{i} ≤ 2·10^9). Then follows number m (1 ≤ m ≤ 2·10^5) — the number of the throws of the second team. Then follow m integer numbers — the distances of throws of b_{i} (1 ≤ b_{i} ≤ 2·10^9). -----Output----- Print two numbers in the format a:b — the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum. -----Examples----- Input 3 1 2 3 2 5 6 Output 9:6 Input 5 6 7 8 9 10 5 1 2 3 4 5 Output 15:10 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python """ Codeforces Contest 281 Div 2 Problem C Author : chaotic_iak Language: Python 3.3.4 """ def main(): n, = read() a = read() res = [(i,0) for i in a] m, = read() b = read() res.extend((i,1) for i in b) res.sort() mxa = 3*n mnb = 3*m cra = 3*n crb = 3*m for _,i in res: if i: crb -= 1 if cra-crb > mxa-mnb: mxa = cra mnb = crb else: cra -= 1 print(str(mxa) + ":" + str(mnb)) ################################### NON-SOLUTION STUFF BELOW def read(mode=2): # 0: String # 1: List of strings # 2: List of integers inputs = input().strip() if mode == 0: return inputs if mode == 1: return inputs.split() if mode == 2: return list(map(int, inputs.split())) def write(s="\n"): if s is None: s = "" if isinstance(s, list): s = " ".join(map(str, s)) s = str(s) print(s, end="") write(main()) ```
vfc_13798
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/493/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n1 2 3\n2\n5 6\n", "output": "9:6\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n6 7 8 9 10\n5\n1 2 3 4 5\n", "output": "15:10\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n1 2 3 4 5\n5\n6 7 8 9 10\n", "output": "15:15\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n1 2 3\n3\n6 4 5\n", "output": "9:9\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n1 2 3 4 5 6 7 8 9 10\n1\n11\n", "output": "30:3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n1 2 3 4 5 6 7 8 9 11\n1\n10\n", "output": "30:3\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1012
Solve the following coding problem using the programming language python: You are given a string $s$ consisting only of lowercase Latin letters. You can rearrange all letters of this string as you wish. Your task is to obtain a good string by rearranging the letters of the given string or report that it is impossible to do it. Let's call a string good if it is not a palindrome. Palindrome is a string which is read from left to right the same as from right to left. For example, strings "abacaba", "aa" and "z" are palindromes and strings "bba", "xd" are not. You have to answer $t$ independent queries. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 100$) — number of queries. Each of the next $t$ lines contains one string. The $i$-th line contains a string $s_i$ consisting only of lowercase Latin letter. It is guaranteed that the length of $s_i$ is from $1$ to $1000$ (inclusive). -----Output----- Print $t$ lines. In the $i$-th line print the answer to the $i$-th query: -1 if it is impossible to obtain a good string by rearranging the letters of $s_i$ and any good string which can be obtained from the given one (by rearranging the letters) otherwise. -----Example----- Input 3 aa abacaba xdd Output -1 abaacba xdd -----Note----- In the first query we cannot rearrange letters to obtain a good string. Other examples (not all) of correct answers to the second query: "ababaca", "abcabaa", "baacaba". In the third query we can do nothing to obtain a good string. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # map(int, input()) t = int(input()) for i in range(t): s = sorted(input()) if s[0] == s[-1]: print(-1) continue print(''.join(s)) ```
vfc_13802
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1093/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\naa\nabacaba\nxdd\n", "output": "-1\naaaabbc\nddx\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1014
Solve the following coding problem using the programming language python: Vasya decided to learn to play chess. Classic chess doesn't seem interesting to him, so he plays his own sort of chess. The queen is the piece that captures all squares on its vertical, horizontal and diagonal lines. If the cell is located on the same vertical, horizontal or diagonal line with queen, and the cell contains a piece of the enemy color, the queen is able to move to this square. After that the enemy's piece is removed from the board. The queen cannot move to a cell containing an enemy piece if there is some other piece between it and the queen. There is an n × n chessboard. We'll denote a cell on the intersection of the r-th row and c-th column as (r, c). The square (1, 1) contains the white queen and the square (1, n) contains the black queen. All other squares contain green pawns that don't belong to anyone. The players move in turns. The player that moves first plays for the white queen, his opponent plays for the black queen. On each move the player has to capture some piece with his queen (that is, move to a square that contains either a green pawn or the enemy queen). The player loses if either he cannot capture any piece during his move or the opponent took his queen during the previous move. Help Vasya determine who wins if both players play with an optimal strategy on the board n × n. -----Input----- The input contains a single number n (2 ≤ n ≤ 10^9) — the size of the board. -----Output----- On the first line print the answer to problem — string "white" or string "black", depending on who wins if the both players play optimally. If the answer is "white", then you should also print two integers r and c representing the cell (r, c), where the first player should make his first move to win. If there are multiple such cells, print the one with the minimum r. If there are still multiple squares, print the one with the minimum c. -----Examples----- Input 2 Output white 1 2 Input 3 Output black -----Note----- In the first sample test the white queen can capture the black queen at the first move, so the white player wins. In the second test from the statement if the white queen captures the green pawn located on the central vertical line, then it will be captured by the black queen during the next move. So the only move for the white player is to capture the green pawn located at (2, 1). Similarly, the black queen doesn't have any other options but to capture the green pawn located at (2, 3), otherwise if it goes to the middle vertical line, it will be captured by the white queen. During the next move the same thing happens — neither the white, nor the black queen has other options rather than to capture green pawns situated above them. Thus, the white queen ends up on square (3, 1), and the black queen ends up on square (3, 3). In this situation the white queen has to capture any of the green pawns located on the middle vertical line, after that it will be captured by the black queen. Thus, the player who plays for the black queen wins. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python """ Codeforces Contest 281 Div 2 Problem D Author : chaotic_iak Language: Python 3.3.4 """ def main(): n, = read() if n%2: print("black") else: print("white") print("1 2") ################################### NON-SOLUTION STUFF BELOW def read(mode=2): # 0: String # 1: List of strings # 2: List of integers inputs = input().strip() if mode == 0: return inputs if mode == 1: return inputs.split() if mode == 2: return list(map(int, inputs.split())) def write(s="\n"): if s is None: s = "" if isinstance(s, list): s = " ".join(map(str, s)) s = str(s) print(s, end="") write(main()) ```
vfc_13810
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/493/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\n", "output": "white\n1 2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n", "output": "black\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1016
Solve the following coding problem using the programming language python: DZY loves chemistry, and he enjoys mixing chemicals. DZY has n chemicals, and m pairs of them will react. He wants to pour these chemicals into a test tube, and he needs to pour them in one by one, in any order. Let's consider the danger of a test tube. Danger of an empty test tube is 1. And every time when DZY pours a chemical, if there are already one or more chemicals in the test tube that can react with it, the danger of the test tube will be multiplied by 2. Otherwise the danger remains as it is. Find the maximum possible danger after pouring all the chemicals one by one in optimal order. -----Input----- The first line contains two space-separated integers n and m $(1 \leq n \leq 50 ; 0 \leq m \leq \frac{n(n - 1)}{2})$. Each of the next m lines contains two space-separated integers x_{i} and y_{i} (1 ≤ x_{i} < y_{i} ≤ n). These integers mean that the chemical x_{i} will react with the chemical y_{i}. Each pair of chemicals will appear at most once in the input. Consider all the chemicals numbered from 1 to n in some order. -----Output----- Print a single integer — the maximum possible danger. -----Examples----- Input 1 0 Output 1 Input 2 1 1 2 Output 2 Input 3 2 1 2 2 3 Output 4 -----Note----- In the first sample, there's only one way to pour, and the danger won't increase. In the second sample, no matter we pour the 1st chemical first, or pour the 2nd chemical first, the answer is always 2. In the third sample, there are four ways to achieve the maximum possible danger: 2-1-3, 2-3-1, 1-2-3 and 3-2-1 (that is the numbers of the chemicals in order of pouring). The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def dfs(v, root): nonlocal cnt if used[v]: return used[v] = True for j in range(len(G[v])): to = G[v][j] dfs(to, root) if v == root: cnt += 1 cnt = 0 n, e = map(int, input().split()) G = [[] for i in range(n)] for i in range(e): a, b = map(lambda x:int(x) - 1, input().split()) G[a].append(b) G[b].append(a) used = [False for i in range(n)] for v in range(n): dfs(v, v) print(2 ** (n - cnt)) ```
vfc_13818
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/445/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1 0\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 1\n1 2\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 2\n1 2\n2 3\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 10\n1 8\n4 10\n4 6\n5 10\n2 3\n1 7\n3 4\n3 6\n6 9\n3 7\n", "output": "512\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "20 20\n6 8\n13 20\n7 13\n6 17\n5 15\n1 12\n2 15\n5 17\n5 14\n6 14\n12 20\n7 20\n1 6\n1 7\n2 19\n14 17\n1 10\n11 15\n9 18\n2 12\n", "output": "32768\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "30 30\n7 28\n16 26\n14 24\n16 18\n20 29\n4 28\n19 21\n8 26\n1 25\n14 22\n13 23\n4 15\n15 16\n2 19\n29 30\n12 20\n3 4\n3 26\n3 11\n22 27\n5 16\n2 24\n2 18\n7 16\n17 21\n17 25\n8 15\n23 27\n12 21\n5 30\n", "output": "67108864\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1017
Solve the following coding problem using the programming language python: Little Artem got n stones on his birthday and now wants to give some of them to Masha. He knows that Masha cares more about the fact of receiving the present, rather than the value of that present, so he wants to give her stones as many times as possible. However, Masha remembers the last present she received, so Artem can't give her the same number of stones twice in a row. For example, he can give her 3 stones, then 1 stone, then again 3 stones, but he can't give her 3 stones and then again 3 stones right after that. How many times can Artem give presents to Masha? -----Input----- The only line of the input contains a single integer n (1 ≤ n ≤ 10^9) — number of stones Artem received on his birthday. -----Output----- Print the maximum possible number of times Artem can give presents to Masha. -----Examples----- Input 1 Output 1 Input 2 Output 1 Input 3 Output 2 Input 4 Output 3 -----Note----- In the first sample, Artem can only give 1 stone to Masha. In the second sample, Atrem can give Masha 1 or 2 stones, though he can't give her 1 stone two times. In the third sample, Atrem can first give Masha 2 stones, a then 1 more stone. In the fourth sample, Atrem can first give Masha 1 stone, then 2 stones, and finally 1 stone again. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = int(input()) if n % 3 == 0: print(2 * (n // 3)) else: print(2 * (n // 3) + 1) ```
vfc_13822
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/669/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n", "output": "3\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1018
Solve the following coding problem using the programming language python: Stepan has n pens. Every day he uses them, and on the i-th day he uses the pen number i. On the (n + 1)-th day again he uses the pen number 1, on the (n + 2)-th — he uses the pen number 2 and so on. On every working day (from Monday to Saturday, inclusive) Stepan spends exactly 1 milliliter of ink of the pen he uses that day. On Sunday Stepan has a day of rest, he does not stend the ink of the pen he uses that day. Stepan knows the current volume of ink in each of his pens. Now it's the Monday morning and Stepan is going to use the pen number 1 today. Your task is to determine which pen will run out of ink before all the rest (that is, there will be no ink left in it), if Stepan will use the pens according to the conditions described above. -----Input----- The first line contains the integer n (1 ≤ n ≤ 50 000) — the number of pens Stepan has. The second line contains the sequence of integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9), where a_{i} is equal to the number of milliliters of ink which the pen number i currently has. -----Output----- Print the index of the pen which will run out of ink before all (it means that there will be no ink left in it), if Stepan will use pens according to the conditions described above. Pens are numbered in the order they are given in input data. The numeration begins from one. Note that the answer is always unambiguous, since several pens can not end at the same time. -----Examples----- Input 3 3 3 3 Output 2 Input 5 5 4 5 4 4 Output 5 -----Note----- In the first test Stepan uses ink of pens as follows: on the day number 1 (Monday) Stepan will use the pen number 1, after that there will be 2 milliliters of ink in it; on the day number 2 (Tuesday) Stepan will use the pen number 2, after that there will be 2 milliliters of ink in it; on the day number 3 (Wednesday) Stepan will use the pen number 3, after that there will be 2 milliliters of ink in it; on the day number 4 (Thursday) Stepan will use the pen number 1, after that there will be 1 milliliters of ink in it; on the day number 5 (Friday) Stepan will use the pen number 2, after that there will be 1 milliliters of ink in it; on the day number 6 (Saturday) Stepan will use the pen number 3, after that there will be 1 milliliters of ink in it; on the day number 7 (Sunday) Stepan will use the pen number 1, but it is a day of rest so he will not waste ink of this pen in it; on the day number 8 (Monday) Stepan will use the pen number 2, after that this pen will run out of ink. So, the first pen which will not have ink is the pen number 2. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys def Min(x, y): if x > y: return y else: return x def Gcd(x, y): if x == 0: return y else: return Gcd(y % x, x) def Lcm(x, y): return x * y // Gcd(x, y) n = int(input()) a = [int(i) for i in input().split()] d = [int(0) for i in range(0, n)] ok = 0 cur = 0 len = Lcm(7, n) for i in range(0, 7 * n): if a[i % n] == 0 : print(i % n + 1) ok = 1 break if cur != 6: a[i % n] -= 1 d[i % n] += 1 cur = (cur + 1) % 7 if ok == 0: k = 10**20 for i in range(0, n): a[i] += d[i] if d[i] == 0: continue if a[i] % d[i] > 0: k = Min(k, a[i] // d[i]) else: k = Min(k, a[i] // d[i] - 1) if k == 10**20: k = 0 for i in range(0, n): a[i] -= k * d[i] iter = 0 cur = 0 while True: if a[iter] == 0: print(iter % n + 1) break else: if cur != 6: a[iter] -= 1 cur = (cur + 1) % 7 iter = (iter + 1) % n ```
vfc_13826
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/774/F", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n3 3 3\n", "output": "2\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1019
Solve the following coding problem using the programming language python: Petya is a big fan of mathematics, especially its part related to fractions. Recently he learned that a fraction $\frac{a}{b}$ is called proper iff its numerator is smaller than its denominator (a < b) and that the fraction is called irreducible if its numerator and its denominator are coprime (they do not have positive common divisors except 1). During his free time, Petya thinks about proper irreducible fractions and converts them to decimals using the calculator. One day he mistakenly pressed addition button ( + ) instead of division button (÷) and got sum of numerator and denominator that was equal to n instead of the expected decimal notation. Petya wanted to restore the original fraction, but soon he realized that it might not be done uniquely. That's why he decided to determine maximum possible proper irreducible fraction $\frac{a}{b}$ such that sum of its numerator and denominator equals n. Help Petya deal with this problem. -----Input----- In the only line of input there is an integer n (3 ≤ n ≤ 1000), the sum of numerator and denominator of the fraction. -----Output----- Output two space-separated positive integers a and b, numerator and denominator of the maximum possible proper irreducible fraction satisfying the given sum. -----Examples----- Input 3 Output 1 2 Input 4 Output 1 3 Input 12 Output 5 7 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 gosa = 1.0 / 10**10 mod = 10**9+7 def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def main(): n = I() a = n//2 b = n-a while fractions.gcd(a,b) > 1: a -= 1 b += 1 return "{} {}".format(a,b) print(main()) ```
vfc_13830
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/854/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n", "output": "1 2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n", "output": "1 3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "12\n", "output": "5 7\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1020
Solve the following coding problem using the programming language python: You have a plate and you want to add some gilding to it. The plate is a rectangle that we split into $w\times h$ cells. There should be $k$ gilded rings, the first one should go along the edge of the plate, the second one — $2$ cells away from the edge and so on. Each ring has a width of $1$ cell. Formally, the $i$-th of these rings should consist of all bordering cells on the inner rectangle of size $(w - 4(i - 1))\times(h - 4(i - 1))$. [Image] The picture corresponds to the third example. Your task is to compute the number of cells to be gilded. -----Input----- The only line contains three integers $w$, $h$ and $k$ ($3 \le w, h \le 100$, $1 \le k \le \left\lfloor \frac{min(n, m) + 1}{4}\right\rfloor$, where $\lfloor x \rfloor$ denotes the number $x$ rounded down) — the number of rows, columns and the number of rings, respectively. -----Output----- Print a single positive integer — the number of cells to be gilded. -----Examples----- Input 3 3 1 Output 8 Input 7 9 1 Output 28 Input 7 9 2 Output 40 -----Note----- The first example is shown on the picture below. [Image] The second example is shown on the picture below. [Image] The third example is shown in the problem description. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python w, h, k = map(int, input().split()) ans = 0 for i in range(k): ans += w * 2 + (h - 2) * 2 #print(ans, h, w) w -= 4 h -= 4 print(ans) ```
vfc_13834
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1031/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 3 1\n", "output": "8\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7 9 1\n", "output": "28\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1021
Solve the following coding problem using the programming language python: Grigory has $n$ magic stones, conveniently numbered from $1$ to $n$. The charge of the $i$-th stone is equal to $c_i$. Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index $i$, where $2 \le i \le n - 1$), and after that synchronizes it with neighboring stones. After that, the chosen stone loses its own charge, but acquires the charges from neighboring stones. In other words, its charge $c_i$ changes to $c_i' = c_{i + 1} + c_{i - 1} - c_i$. Andrew, Grigory's friend, also has $n$ stones with charges $t_i$. Grigory is curious, whether there exists a sequence of zero or more synchronization operations, which transforms charges of Grigory's stones into charges of corresponding Andrew's stones, that is, changes $c_i$ into $t_i$ for all $i$? -----Input----- The first line contains one integer $n$ ($2 \le n \le 10^5$) — the number of magic stones. The second line contains integers $c_1, c_2, \ldots, c_n$ ($0 \le c_i \le 2 \cdot 10^9$) — the charges of Grigory's stones. The second line contains integers $t_1, t_2, \ldots, t_n$ ($0 \le t_i \le 2 \cdot 10^9$) — the charges of Andrew's stones. -----Output----- If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes". Otherwise, print "No". -----Examples----- Input 4 7 2 4 12 7 15 10 12 Output Yes Input 3 4 4 4 1 2 3 Output No -----Note----- In the first example, we can perform the following synchronizations ($1$-indexed): First, synchronize the third stone $[7, 2, \mathbf{4}, 12] \rightarrow [7, 2, \mathbf{10}, 12]$. Then synchronize the second stone: $[7, \mathbf{2}, 10, 12] \rightarrow [7, \mathbf{15}, 10, 12]$. In the second example, any operation with the second stone will not change its charge. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) def dif(x): c = [] for i in range(n-1): c.append(x[i+1] - x[i]) return c if list(sorted(dif(a))) == list(sorted(dif(b))) and a[0] == b[0] and a[n-1] == b[n-1]: print('Yes') else: print('No') ```
vfc_13838
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1110/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n7 2 4 12\n7 15 10 12\n", "output": "Yes\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n4 4 4\n1 2 3\n", "output": "No\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n707645074 7978468 456945316 474239945 262709403 240934546 113271669 851586694 388901819 787182236\n707645074 7978468 25273097 3498240 741813265 279128390 728095238 600432361 998712778 787182236\n", "output": "Yes\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n589934963 440265648 161048053 196789927 951616256 63404428 660569162 779938975 237139603 31052281\n589934964 709304777 745046651 595377336 52577964 649742698 370525103 164437781 919264110 31052282\n", "output": "No\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1022
Solve the following coding problem using the programming language python: There are $n$ children numbered from $1$ to $n$ in a kindergarten. Kindergarten teacher gave $a_i$ ($1 \leq a_i \leq n$) candies to the $i$-th child. Children were seated in a row in order from $1$ to $n$ from left to right and started eating candies. While the $i$-th child was eating candies, he calculated two numbers $l_i$ and $r_i$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively. Formally, $l_i$ is the number of indices $j$ ($1 \leq j < i$), such that $a_i < a_j$ and $r_i$ is the number of indices $j$ ($i < j \leq n$), such that $a_i < a_j$. Each child told to the kindergarten teacher the numbers $l_i$ and $r_i$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $l$ and $r$ determine whether she could have given the candies to the children such that all children correctly calculated their values $l_i$ and $r_i$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it. -----Input----- On the first line there is a single integer $n$ ($1 \leq n \leq 1000$) — the number of children in the kindergarten. On the next line there are $n$ integers $l_1, l_2, \ldots, l_n$ ($0 \leq l_i \leq n$), separated by spaces. On the next line, there are $n$ integer numbers $r_1, r_2, \ldots, r_n$ ($0 \leq r_i \leq n$), separated by spaces. -----Output----- If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $n$ integers $a_1, a_2, \ldots, a_n$, separated by spaces — the numbers of candies the children $1, 2, \ldots, n$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $1 \leq a_i \leq n$. The number of children seating to the left of the $i$-th child that got more candies than he should be equal to $l_i$ and the number of children seating to the right of the $i$-th child that got more candies than he should be equal to $r_i$. If there is more than one solution, find any of them. -----Examples----- Input 5 0 0 1 1 2 2 0 1 0 0 Output YES 1 3 1 2 1 Input 4 0 0 2 0 1 1 1 1 Output NO Input 3 0 0 0 0 0 0 Output YES 1 1 1 -----Note----- In the first example, if the teacher distributed $1$, $3$, $1$, $2$, $1$ candies to $1$-st, $2$-nd, $3$-rd, $4$-th, $5$-th child, respectively, then all the values calculated by the children are correct. For example, the $5$-th child was given $1$ candy, to the left of him $2$ children were given $1$ candy, $1$ child was given $2$ candies and $1$ child — $3$ candies, so there are $2$ children to the left of him that were given more candies than him. In the second example it is impossible to distribute the candies, because the $4$-th child made a mistake in calculating the value of $r_4$, because there are no children to the right of him, so $r_4$ should be equal to $0$. In the last example all children may have got the same number of candies, that's why all the numbers are $0$. Note that each child should receive at least one candy. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n=int(input()) #x,y,z,t1,t2,t3=map(int,input().split()) a=list(map(int,input().split())) b=list(map(int,input().split())) s=[0]*n ans=True for i in range(n): ans=ans and a[i]<=i and b[i]<=(n-i-1) s[i]=n-a[i]-b[i] def qwe(s,j): l,r=0,0 for i in range(len(s)): if i<j and s[i]>s[j]: l+=1 elif i>j and s[i]>s[j]: r+=1 return l,r if ans: for i in range(n): l,r=qwe(s,i) ans=ans and a[i]==l and b[i]==r if ans: print('YES') for i in range(n): print(n-a[i]-b[i],end=' ') else: print('NO') ```
vfc_13842
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1054/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n0 0 1 1 2\n2 0 1 0 0\n", "output": "YES\n3 5 3 4 3 ", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n0 0 2 0\n1 1 1 1\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n0 0 0\n0 0 0\n", "output": "YES\n3 3 3 ", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n0\n0\n", "output": "YES\n1 ", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n0 0 0 0 0\n0 0 0 0 0\n", "output": "YES\n5 5 5 5 5 ", "type": "stdin_stdout" } ] }
apps
verifiable_code
1023
Solve the following coding problem using the programming language python: Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools import random sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def pe(s): return print(str(s), file=sys.stderr) def main(): n,m,ta,tb,k = LI() a = LI() b = LI() if k >= n or k >= m: return -1 r = 0 bi = 0 for i in range(k+1): c = a[i] + ta while bi < m and b[bi] < c: bi += 1 if bi + (k-i) >= m: return -1 t = b[bi + (k-i)] + tb if r < t: r = t return r print(main()) ```
vfc_13846
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1148/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4 5 1 1 2\n1 3 5 7\n1 2 3 9 10\n", "output": "11\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 2 4 4 2\n1 10\n10 20\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 3 2 3 1\n1 999999998 999999999 1000000000\n3 4 1000000000\n", "output": "1000000003\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1 1 1 1\n1\n2\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 10 20 10 3\n7 9 18 19 20\n3 6 7 8 9 10 14 15 16 17\n", "output": "-1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1025
Solve the following coding problem using the programming language python: Vanya got bored and he painted n distinct points on the plane. After that he connected all the points pairwise and saw that as a result many triangles were formed with vertices in the painted points. He asks you to count the number of the formed triangles with the non-zero area. -----Input----- The first line contains integer n (1 ≤ n ≤ 2000) — the number of the points painted on the plane. Next n lines contain two integers each x_{i}, y_{i} ( - 100 ≤ x_{i}, y_{i} ≤ 100) — the coordinates of the i-th point. It is guaranteed that no two given points coincide. -----Output----- In the first line print an integer — the number of triangles with the non-zero area among the painted points. -----Examples----- Input 4 0 0 1 1 2 0 2 2 Output 3 Input 3 0 0 1 1 2 0 Output 1 Input 1 1 1 Output 0 -----Note----- Note to the first sample test. There are 3 triangles formed: (0, 0) - (1, 1) - (2, 0); (0, 0) - (2, 2) - (2, 0); (1, 1) - (2, 2) - (2, 0). Note to the second sample test. There is 1 triangle formed: (0, 0) - (1, 1) - (2, 0). Note to the third sample test. A single point doesn't form a single triangle. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from fractions import gcd from collections import defaultdict def read_data(): n = int(input()) points = [] for i in range(n): x, y = map(int, input().split()) points.append((x, y)) return n, points def solve(n, points): if n <= 2: return 0 zeros = 0 for i, (x, y) in enumerate(points[:-2]): zeros += count_zeros(i, x, y, points) return n * (n-1) * (n-2) // 6 - zeros def count_zeros(i, x, y, points): slopes = defaultdict(int) for xj, yj in points[i + 1:]: dx = x - xj dy = y - yj d = gcd(dx, dy) slope = (dx/d, dy/d) slopes[slope] += 1 zeros = 0 for val in slopes.values(): if val >= 2: zeros += val * (val - 1) return zeros // 2 n, points = read_data() print(solve(n, points)) ```
vfc_13854
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/552/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n0 0\n1 1\n2 0\n2 2\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n0 0\n1 1\n2 0\n", "output": "1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1026
Solve the following coding problem using the programming language python: Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = int(input()) B = list(map(int, input().split())) pp = {} for i in range(n): if B[i] - (i + 1) not in pp: pp[B[i] - (i + 1)] = 0 pp[B[i] - (i + 1)] += B[i] ans = 0 for c in pp: ans = max(ans, pp[c]) print(ans) ```
vfc_13858
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1319/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6\n10 7 1 9 10 15\n", "output": "26\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n400000\n", "output": "400000\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7\n8 9 26 11 12 29 14\n", "output": "55\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n10 60 12 13 14 15 16 66 18 130\n", "output": "130\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n399999 400000\n", "output": "799999\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n2 1\n", "output": "2\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1028
Solve the following coding problem using the programming language python: n participants of the competition were split into m teams in some manner so that each team has at least one participant. After the competition each pair of participants from the same team became friends. Your task is to write a program that will find the minimum and the maximum number of pairs of friends that could have formed by the end of the competition. -----Input----- The only line of input contains two integers n and m, separated by a single space (1 ≤ m ≤ n ≤ 10^9) — the number of participants and the number of teams respectively. -----Output----- The only line of the output should contain two integers k_{min} and k_{max} — the minimum possible number of pairs of friends and the maximum possible number of pairs of friends respectively. -----Examples----- Input 5 1 Output 10 10 Input 3 2 Output 1 1 Input 6 3 Output 3 6 -----Note----- In the first sample all the participants get into one team, so there will be exactly ten pairs of friends. In the second sample at any possible arrangement one team will always have two participants and the other team will always have one participant. Thus, the number of pairs of friends will always be equal to one. In the third sample minimum number of newly formed friendships can be achieved if participants were split on teams consisting of 2 people, maximum number can be achieved if participants were split on teams of 1, 1 and 4 people. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python """ Codeforces Contest 273 Div 2 Problem B Author : chaotic_iak Language: Python 3.3.4 """ def comb2(n): return n*(n-1)//2 def main(): n,m = read() k = n // m p = n % m mn = p * comb2(k+1) + (m-p) * comb2(k) mx = comb2(n-m+1) print(mn, mx) ################################### NON-SOLUTION STUFF BELOW def read(mode=2): # 0: String # 1: List of strings # 2: List of integers inputs = input().strip() if mode == 0: return inputs if mode == 1: return inputs.split() if mode == 2: return list(map(int, inputs.split())) def write(s="\n"): if s is None: s = "" if isinstance(s, list): s = " ".join(map(str, s)) s = str(s) print(s, end="") write(main()) ```
vfc_13866
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/478/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5 1\n", "output": "10 10\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 2\n", "output": "1 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6 3\n", "output": "3 6\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1029
Solve the following coding problem using the programming language python: George is a cat, so he really likes to play. Most of all he likes to play with his array of positive integers b. During the game, George modifies the array by using special changes. Let's mark George's current array as b_1, b_2, ..., b_{|}b| (record |b| denotes the current length of the array). Then one change is a sequence of actions: Choose two distinct indexes i and j (1 ≤ i, j ≤ |b|; i ≠ j), such that b_{i} ≥ b_{j}. Get number v = concat(b_{i}, b_{j}), where concat(x, y) is a number obtained by adding number y to the end of the decimal record of number x. For example, concat(500, 10) = 50010, concat(2, 2) = 22. Add number v to the end of the array. The length of the array will increase by one. Remove from the array numbers with indexes i and j. The length of the array will decrease by two, and elements of the array will become re-numbered from 1 to current length of the array. George played for a long time with his array b and received from array b an array consisting of exactly one number p. Now George wants to know: what is the maximum number of elements array b could contain originally? Help him find this number. Note that originally the array could contain only positive integers. -----Input----- The first line of the input contains a single integer p (1 ≤ p < 10^100000). It is guaranteed that number p doesn't contain any leading zeroes. -----Output----- Print an integer — the maximum number of elements array b could contain originally. -----Examples----- Input 9555 Output 4 Input 10000000005 Output 2 Input 800101 Output 3 Input 45 Output 1 Input 1000000000000001223300003342220044555 Output 17 Input 19992000 Output 1 Input 310200 Output 2 -----Note----- Let's consider the test examples: Originally array b can be equal to {5, 9, 5, 5}. The sequence of George's changes could have been: {5, 9, 5, 5} → {5, 5, 95} → {95, 55} → {9555}. Originally array b could be equal to {1000000000, 5}. Please note that the array b cannot contain zeros. Originally array b could be equal to {800, 10, 1}. Originally array b could be equal to {45}. It cannot be equal to {4, 5}, because George can get only array {54} from this array in one operation. Note that the numbers can be very large. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python s=input().strip() n=len(s) i=0 k=1 j=0 while i<n: #print(i,j) j=i i+=1 while i<n and s[i]=='0': i+=1 if j>i-j: k+=1 elif j==i-j: if s[:j]>=s[j:i]: k+=1 else: k=1 else: k=1 print(k) ```
vfc_13870
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/387/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "9555\n", "output": "4", "type": "stdin_stdout" }, { "fn_name": null, "input": "10000000005\n", "output": "2", "type": "stdin_stdout" }, { "fn_name": null, "input": "800101\n", "output": "3", "type": "stdin_stdout" } ] }
apps
verifiable_code
1030
Solve the following coding problem using the programming language python: User ainta is making a web site. This time he is going to make a navigation of the pages. In his site, there are n pages numbered by integers from 1 to n. Assume that somebody is on the p-th page now. The navigation will look like this: << p - k p - k + 1 ... p - 1 (p) p + 1 ... p + k - 1 p + k >> When someone clicks the button "<<" he is redirected to page 1, and when someone clicks the button ">>" he is redirected to page n. Of course if someone clicks on a number, he is redirected to the corresponding page. There are some conditions in the navigation: If page 1 is in the navigation, the button "<<" must not be printed. If page n is in the navigation, the button ">>" must not be printed. If the page number is smaller than 1 or greater than n, it must not be printed.   You can see some examples of the navigations. Make a program that prints the navigation. -----Input----- The first and the only line contains three integers n, p, k (3 ≤ n ≤ 100; 1 ≤ p ≤ n; 1 ≤ k ≤ n) -----Output----- Print the proper navigation. Follow the format of the output from the test samples. -----Examples----- Input 17 5 2 Output << 3 4 (5) 6 7 >> Input 6 5 2 Output << 3 4 (5) 6 Input 6 1 2 Output (1) 2 3 >> Input 6 2 2 Output 1 (2) 3 4 >> Input 9 6 3 Output << 3 4 5 (6) 7 8 9 Input 10 6 3 Output << 3 4 5 (6) 7 8 9 >> Input 8 5 4 Output 1 2 3 4 (5) 6 7 8 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, p, k = map(int, input().split()) if (p - k) > 1: print('<<', end = ' ') for i in range(p - k, p): if (i > 0): print(i, end = ' ') print('(' + str(p) + ')', end = ' ') for i in range(p + 1, p + k + 1): if (i < (n + 1)): print(i, end = ' ') if (p + k) < n: print('>>', end = ' ') ```
vfc_13874
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/399/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "17 5 2\n", "output": "<< 3 4 (5) 6 7 >> ", "type": "stdin_stdout" }, { "fn_name": null, "input": "6 5 2\n", "output": "<< 3 4 (5) 6 ", "type": "stdin_stdout" }, { "fn_name": null, "input": "6 1 2\n", "output": "(1) 2 3 >> ", "type": "stdin_stdout" }, { "fn_name": null, "input": "6 2 2\n", "output": "1 (2) 3 4 >> ", "type": "stdin_stdout" } ] }
apps
verifiable_code
1031
Solve the following coding problem using the programming language python: In this problem, your task is to use ASCII graphics to paint a cardiogram. A cardiogram is a polyline with the following corners:$(0 ; 0),(a_{1} ; a_{1}),(a_{1} + a_{2} ; a_{1} - a_{2}),(a_{1} + a_{2} + a_{3} ; a_{1} - a_{2} + a_{3}), \ldots,(\sum_{i = 1}^{n} a_{i} ; \sum_{i = 1}^{n}(- 1)^{i + 1} a_{i})$ That is, a cardiogram is fully defined by a sequence of positive integers a_1, a_2, ..., a_{n}. Your task is to paint a cardiogram by given sequence a_{i}. -----Input----- The first line contains integer n (2 ≤ n ≤ 1000). The next line contains the sequence of integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 1000). It is guaranteed that the sum of all a_{i} doesn't exceed 1000. -----Output----- Print max |y_{i} - y_{j}| lines (where y_{k} is the y coordinate of the k-th point of the polyline), in each line print $\sum_{i = 1}^{n} a_{i}$ characters. Each character must equal either « / » (slash), « \ » (backslash), « » (space). The printed image must be the image of the given polyline. Please study the test samples for better understanding of how to print a cardiogram. Note that in this problem the checker checks your answer taking spaces into consideration. Do not print any extra characters. Remember that the wrong answer to the first pretest doesn't give you a penalty. -----Examples----- Input 5 3 1 2 5 1 Output / \ / \ / \ / \ / \ \ / Input 3 1 5 1 Output / \ \ \ \ \ / The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = int(input()) a = [int(i) for i in input().split()] miny = 0 maxy = 0 s = 0 for i in range(n): if i % 2 == 0: s += a[i] else: s -= a[i] maxy = max(s, maxy) miny = min(s, miny) dif = maxy - miny size = sum(a) res = [[" "] * size for i in range(dif)] cur = [maxy, 0] for i in range(n): if i % 2 == 0: cur[0] -= 1 else: cur[0] += 1 for j in range(a[i]): if i % 2 == 0: res[cur[0]][cur[1]] = "/" cur[0] -= 1 else: res[cur[0]][cur[1]] = "\\" cur[0] += 1 cur[1] += 1 for i in res: print("".join(i)) ```
vfc_13878
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/435/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n3 1 2 5 1\n", "output": " /\\ \n /\\/ \\ \n / \\ \n/ \\ \n \\/\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1032
Solve the following coding problem using the programming language python: This is the hard version of the problem. The difference between versions is the constraints on $n$ and $a_i$. You can make hacks only if all versions of the problem are solved. First, Aoi came up with the following idea for the competitive programming problem: Yuzu is a girl who collecting candies. Originally, she has $x$ candies. There are also $n$ enemies numbered with integers from $1$ to $n$. Enemy $i$ has $a_i$ candies. Yuzu is going to determine a permutation $P$. A permutation is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $\{2,3,1,5,4\}$ is a permutation, but $\{1,2,2\}$ is not a permutation ($2$ appears twice in the array) and $\{1,3,4\}$ is also not a permutation (because $n=3$ but there is the number $4$ in the array). After that, she will do $n$ duels with the enemies with the following rules: If Yuzu has equal or more number of candies than enemy $P_i$, she wins the duel and gets $1$ candy. Otherwise, she loses the duel and gets nothing. The candy which Yuzu gets will be used in the next duels. Yuzu wants to win all duels. How many valid permutations $P$ exist? This problem was easy and wasn't interesting for Akari, who is a friend of Aoi. And Akari made the following problem from the above idea: Let's define $f(x)$ as the number of valid permutations for the integer $x$. You are given $n$, $a$ and a prime number $p \le n$. Let's call a positive integer $x$ good, if the value $f(x)$ is not divisible by $p$. Find all good integers $x$. Your task is to solve this problem made by Akari. -----Input----- The first line contains two integers $n$, $p$ $(2 \le p \le n \le 10^5)$. It is guaranteed, that the number $p$ is prime (it has exactly two divisors $1$ and $p$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ $(1 \le a_i \le 10^9)$. -----Output----- In the first line, print the number of good integers $x$. In the second line, output all good integers $x$ in the ascending order. It is guaranteed that the number of good integers $x$ does not exceed $10^5$. -----Examples----- Input 3 2 3 4 5 Output 1 3 Input 4 3 2 3 5 6 Output 2 3 4 Input 4 3 9 1 1 1 Output 0 Input 3 2 1000000000 1 999999999 Output 1 999999998 -----Note----- In the first test, $p=2$. If $x \le 2$, there are no valid permutations for Yuzu. So $f(x)=0$ for all $x \le 2$. The number $0$ is divisible by $2$, so all integers $x \leq 2$ are not good. If $x = 3$, $\{1,2,3\}$ is the only valid permutation for Yuzu. So $f(3)=1$, so the number $3$ is good. If $x = 4$, $\{1,2,3\} , \{1,3,2\} , \{2,1,3\} , \{2,3,1\}$ are all valid permutations for Yuzu. So $f(4)=4$, so the number $4$ is not good. If $x \ge 5$, all $6$ permutations are valid for Yuzu. So $f(x)=6$ for all $x \ge 5$, so all integers $x \ge 5$ are not good. So, the only good number is $3$. In the third test, for all positive integers $x$ the value $f(x)$ is divisible by $p = 3$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys input = sys.stdin.readline n,p=map(int,input().split()) a=list(map(int,input().split())) a.sort() mn=0 mx=2000000000000000 for i in range(n): d=a[i]-i mn=max(d,mn) if i>=p-1: d2=a[i]-i+p-1 mx=min(mx,d2) print(max(mx-mn,0)) for i in range(mn,mx): print(i,end=" ") ```
vfc_13882
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1371/E2", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 2\n3 4 5\n", "output": "1\n3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 3\n2 3 5 6\n", "output": "2\n3 4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 3\n9 1 1 1\n", "output": "0\n\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 2\n1000000000 1 999999999\n", "output": "1\n999999998\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 2\n603532654 463735162\n", "output": "1\n603532653\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1033
Solve the following coding problem using the programming language python: You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right. Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height h_{i} of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it. Finally you ended up with the following conditions to building the castle: h_1 ≤ H: no sand from the leftmost spot should go over the fence; For any $i \in [ 1 ; \infty)$ |h_{i} - h_{i} + 1| ≤ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; $\sum_{i = 1}^{\infty} h_{i} = n$: you want to spend all the sand you brought with you. As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible. Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold. -----Input----- The only line contains two integer numbers n and H (1 ≤ n, H ≤ 10^18) — the number of sand packs you have and the height of the fence, respectively. -----Output----- Print the minimum number of spots you can occupy so the all the castle building conditions hold. -----Examples----- Input 5 2 Output 3 Input 6 8 Output 3 -----Note----- Here are the heights of some valid castles: n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied) The first list for both cases is the optimal answer, 3 spots are occupied in them. And here are some invalid ones: n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...] The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, k = list(map(int, input().split())) def get(x): if x <= k: return x * (x + 1) // 2 res = k * x - k * (k - 1) // 2 sz = x - k - 1 if sz % 2 == 0: cnt = sz // 2 res += (2 + sz) * cnt // 2 else: cnt = sz // 2 + 1 res += (1 + sz) * cnt // 2 return res l = 0 r = 10 ** 18 while r - l > 1: m = l + (r - l) // 2 if get(m) >= n: r = m else: l = m print(r) ```
vfc_13886
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/985/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5 2\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6 8\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "20 4\n", "output": "7\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1000000000000000000 1000000000000000000\n", "output": "1414213562\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1041
Solve the following coding problem using the programming language python: n evenly spaced points have been marked around the edge of a circle. There is a number written at each point. You choose a positive real number k. Then you may repeatedly select a set of 2 or more points which are evenly spaced, and either increase all numbers at points in the set by k or decrease all numbers at points in the set by k. You would like to eventually end up with all numbers equal to 0. Is it possible? A set of 2 points is considered evenly spaced if they are diametrically opposed, and a set of 3 or more points is considered evenly spaced if they form a regular polygon. -----Input----- The first line of input contains an integer n (3 ≤ n ≤ 100000), the number of points along the circle. The following line contains a string s with exactly n digits, indicating the numbers initially present at each of the points, in clockwise order. -----Output----- Print "YES" (without quotes) if there is some sequence of operations that results in all numbers being 0, otherwise "NO" (without quotes). You can print each letter in any case (upper or lower). -----Examples----- Input 30 000100000100000110000000001100 Output YES Input 6 314159 Output NO -----Note----- If we label the points from 1 to n, then for the first test case we can set k = 1. Then we increase the numbers at points 7 and 22 by 1, then decrease the numbers at points 7, 17, and 27 by 1, then decrease the numbers at points 4, 10, 16, 22, and 28 by 1. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python #Circle of Numbers import math def centre(n, pts): x, y = 0, 0 for j in [7,11,13,17,19,23,29,31,37,1193,1663,2711,4007,65537]: if math.gcd(n,j) == 1: for i in range(n): k = int(pts[i]) x += k*math.cos(math.pi * 2*i*j/n) y += k*math.sin(math.pi * 2*i*j/n) if not (abs(x) < 0.000001 and abs(y) < 0.000001): return 'NO' return 'YES' def strconv(s): return [char for char in s] n = int(input()) pts = strconv(input()) print(centre(n,pts)) ```
vfc_13918
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/859/G", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "30\n000100000100000110000000001100\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n314159\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n000\n", "output": "YES\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1042
Solve the following coding problem using the programming language python: Count the number of distinct sequences a_1, a_2, ..., a_{n} (1 ≤ a_{i}) consisting of positive integers such that gcd(a_1, a_2, ..., a_{n}) = x and $\sum_{i = 1}^{n} a_{i} = y$. As this number could be large, print the answer modulo 10^9 + 7. gcd here means the greatest common divisor. -----Input----- The only line contains two positive integers x and y (1 ≤ x, y ≤ 10^9). -----Output----- Print the number of such sequences modulo 10^9 + 7. -----Examples----- Input 3 9 Output 3 Input 5 8 Output 0 -----Note----- There are three suitable sequences in the first test: (3, 3, 3), (3, 6), (6, 3). There are no suitable sequences in the second test. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys x,y = list(map(int, input().strip().split())) if y % x != 0: print(0) return MOD = 10**9 + 7 K = y//x def multiply(A,b, MOD): n, m = len(A), len(b[0]) matrika = [[0 for _ in range(m)] for _ in range(n)] for i in range(n): for j in range(m): vsota = 0 for k in range(len(A[0])): vsota += A[i][k]*b[k][j] matrika[i][j] = vsota % MOD return matrika def copy(mat): return [e[:] for e in mat] def fib(n): if n == 0: return 0 if n == 1: return 1 if n == 2: return 1 matrika = [[1,1],[1,0]] pripravi = dict() pripravi[1] = copy(matrika) s = 1 pot = [1] working = copy(matrika) while s <= n: working = multiply(working,working,MOD) s*= 2 pripravi[s] = copy(working) pot.append(s) manjka = n-2 pointer = len(pot) - 1 while manjka > 0: if pot[pointer] > manjka: pointer -= 1 else: matrika = multiply(matrika, pripravi[pot[pointer]], MOD) manjka -= pot[pointer] v = [[1],[0]] return multiply(matrika, v, MOD)[0][0] memo2 = dict() def find(y): if y in memo2: return memo2[y] ALL = (pow(2, y - 1, MOD)) % MOD k = 2 while k*k <= y: if y % k == 0: if k*k != y: ALL -= find(y//k) ALL -= find(k) else: ALL -= find(k) k += 1 #print(k, ALL) if y != 1: ALL -= 1 memo2[y] = ALL % MOD return ALL % MOD print(find(K) % MOD) def gcd(x,y): if x == 0: return y if y > x: return gcd(y,x) if x % y == 0: return y return gcd(y, x % y) memo = dict() def brute(k, gc): if (k,gc) in memo: return memo[k,gc] if k == 0: if gc: return 1 else: return 0 ALL = 0 for i in range(1, k + 1): ALL += brute(k-i,gcd(gc,i)) memo[k, gc] = ALL return ALL #print(brute(K,0) % MOD) ```
vfc_13922
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/900/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 9\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 8\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 12\n", "output": "27\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1043
Solve the following coding problem using the programming language python: You are organizing a boxing tournament, where $n$ boxers will participate ($n$ is a power of $2$), and your friend is one of them. All boxers have different strength from $1$ to $n$, and boxer $i$ wins in the match against boxer $j$ if and only if $i$ is stronger than $j$. The tournament will be organized as follows: $n$ boxers will be divided into pairs; the loser in each pair leaves the tournament, and $\frac{n}{2}$ winners advance to the next stage, where they are divided into pairs again, and the winners in all pairs advance to the next stage, and so on, until only one boxer remains (who is declared the winner). Your friend really wants to win the tournament, but he may be not the strongest boxer. To help your friend win the tournament, you may bribe his opponents: if your friend is fighting with a boxer you have bribed, your friend wins even if his strength is lower. Furthermore, during each stage you distribute the boxers into pairs as you wish. The boxer with strength $i$ can be bribed if you pay him $a_i$ dollars. What is the minimum number of dollars you have to spend to make your friend win the tournament, provided that you arrange the boxers into pairs during each stage as you wish? -----Input----- The first line contains one integer $n$ ($2 \le n \le 2^{18}$) — the number of boxers. $n$ is a power of $2$. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$, where $a_i$ is the number of dollars you have to pay if you want to bribe the boxer with strength $i$. Exactly one of $a_i$ is equal to $-1$ — it means that the boxer with strength $i$ is your friend. All other values are in the range $[1, 10^9]$. -----Output----- Print one integer — the minimum number of dollars you have to pay so your friend wins. -----Examples----- Input 4 3 9 1 -1 Output 0 Input 8 11 -1 13 19 24 7 17 5 Output 12 -----Note----- In the first test case no matter how you will distribute boxers into pairs, your friend is the strongest boxer and anyway wins the tournament. In the second test case you can distribute boxers as follows (your friend is number $2$): $1 : 2, 8 : 5, 7 : 3, 6 : 4$ (boxers $2, 8, 7$ and $6$ advance to the next stage); $2 : 6, 8 : 7$ (boxers $2$ and $8$ advance to the next stage, you have to bribe the boxer with strength $6$); $2 : 8$ (you have to bribe the boxer with strength $8$); The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from heapq import heappush, heappop N = int(input()) A = [int(a) for a in input().split()] for i in range(N): if A[i] < 0: k = i break A = [0] + [0 if i < k else A[i] for i in range(N) if i != k] ans = A.pop() H = [] while N > 2: N //= 2 for i in range(N): heappush(H, A.pop()) ans += heappop(H) print(ans) ```
vfc_13926
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1260/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n3 9 1 -1\n", "output": "0", "type": "stdin_stdout" } ] }
apps
verifiable_code
1045
Solve the following coding problem using the programming language python: Vanya got n cubes. He decided to build a pyramid from them. Vanya wants to build the pyramid as follows: the top level of the pyramid must consist of 1 cube, the second level must consist of 1 + 2 = 3 cubes, the third level must have 1 + 2 + 3 = 6 cubes, and so on. Thus, the i-th level of the pyramid must have 1 + 2 + ... + (i - 1) + i cubes. Vanya wants to know what is the maximum height of the pyramid that he can make using the given cubes. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^4) — the number of cubes given to Vanya. -----Output----- Print the maximum possible height of the pyramid in the single line. -----Examples----- Input 1 Output 1 Input 25 Output 4 -----Note----- Illustration to the second sample: [Image] The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = int(input()) ans = 0 s = 0 while (n > 0): ans += 1 s += ans n -= s if (n < 0): ans -= 1 break print(ans) ```
vfc_13934
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/492/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "25\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4115\n", "output": "28\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "9894\n", "output": "38\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7969\n", "output": "35\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1046
Solve the following coding problem using the programming language python: Polycarpus is the director of a large corporation. There are n secretaries working for the corporation, each of them corresponds via the famous Spyke VoIP system during the day. We know that when two people call each other via Spyke, the Spyke network assigns a unique ID to this call, a positive integer session number. One day Polycarpus wondered which secretaries are talking via the Spyke and which are not. For each secretary, he wrote out either the session number of his call or a 0 if this secretary wasn't talking via Spyke at that moment. Help Polycarpus analyze these data and find out the number of pairs of secretaries that are talking. If Polycarpus has made a mistake in the data and the described situation could not have taken place, say so. Note that the secretaries can correspond via Spyke not only with each other, but also with the people from other places. Also, Spyke conferences aren't permitted — that is, one call connects exactly two people. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^3) — the number of secretaries in Polycarpus's corporation. The next line contains n space-separated integers: id_1, id_2, ..., id_{n} (0 ≤ id_{i} ≤ 10^9). Number id_{i} equals the number of the call session of the i-th secretary, if the secretary is talking via Spyke, or zero otherwise. Consider the secretaries indexed from 1 to n in some way. -----Output----- Print a single integer — the number of pairs of chatting secretaries, or -1 if Polycarpus's got a mistake in his records and the described situation could not have taken place. -----Examples----- Input 6 0 1 7 1 7 10 Output 2 Input 3 1 1 1 Output -1 Input 1 0 Output 0 -----Note----- In the first test sample there are two Spyke calls between secretaries: secretary 2 and secretary 4, secretary 3 and secretary 5. In the second test sample the described situation is impossible as conferences aren't allowed. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python #scott http://codeforces.com/problemset/problem/291/A now? ok you start n = int(input()) arr = list(map (int, input().split())) #scott #for i in arr: # print (i) cnt = 0 clast, llast = -1, -1 #scott wait we need to sort arr = sorted(arr) bad = False #scott so now we just count # of pairs and make sure there's not 3 in a row right?ok #so a neat thing you can do is just for x in arr for i in arr: #print (i) if i > 0: #scott so last was the last one, llast was the second last one if i == clast : cnt += 1 #scott if clast == llast : bad = True #scott your turn llast = clast clast = i #scott if bad == False: print (cnt) #scott else: print(-1) #scott #darn ii'm getting RTE test 1 ```
vfc_13938
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/291/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6\n0 1 7 1 7 10\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n1 1 1\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n0\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n2 2 1 1 3\n", "output": "2\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1048
Solve the following coding problem using the programming language python: Ivan has a robot which is situated on an infinite grid. Initially the robot is standing in the starting cell (0, 0). The robot can process commands. There are four types of commands it can perform: U — move from the cell (x, y) to (x, y + 1); D — move from (x, y) to (x, y - 1); L — move from (x, y) to (x - 1, y); R — move from (x, y) to (x + 1, y). Ivan entered a sequence of n commands, and the robot processed it. After this sequence the robot ended up in the starting cell (0, 0), but Ivan doubts that the sequence is such that after performing it correctly the robot ends up in the same cell. He thinks that some commands were ignored by robot. To acknowledge whether the robot is severely bugged, he needs to calculate the maximum possible number of commands that were performed correctly. Help Ivan to do the calculations! -----Input----- The first line contains one number n — the length of sequence of commands entered by Ivan (1 ≤ n ≤ 100). The second line contains the sequence itself — a string consisting of n characters. Each character can be U, D, L or R. -----Output----- Print the maximum possible number of commands from the sequence the robot could perform to end up in the starting cell. -----Examples----- Input 4 LDUR Output 4 Input 5 RRRUU Output 0 Input 6 LLRRRR Output 4 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python '''input 6 LLRRRR ''' n = int(input()) s = input() h, v = min(s.count("L"), s.count("R")), min(s.count("U"), s.count("D")) print(2*h + 2*v) ```
vfc_13946
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/888/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\nLDUR\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\nRRRUU\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1049
Solve the following coding problem using the programming language python: Arya has n opponents in the school. Each day he will fight with all opponents who are present this day. His opponents have some fighting plan that guarantees they will win, but implementing this plan requires presence of them all. That means if one day at least one of Arya's opponents is absent at the school, then Arya will beat all present opponents. Otherwise, if all opponents are present, then they will beat Arya. For each opponent Arya knows his schedule — whether or not he is going to present on each particular day. Tell him the maximum number of consecutive days that he will beat all present opponents. Note, that if some day there are no opponents present, Arya still considers he beats all the present opponents. -----Input----- The first line of the input contains two integers n and d (1 ≤ n, d ≤ 100) — the number of opponents and the number of days, respectively. The i-th of the following d lines contains a string of length n consisting of characters '0' and '1'. The j-th character of this string is '0' if the j-th opponent is going to be absent on the i-th day. -----Output----- Print the only integer — the maximum number of consecutive days that Arya will beat all present opponents. -----Examples----- Input 2 2 10 00 Output 2 Input 4 1 0100 Output 1 Input 4 5 1101 1111 0110 1011 1111 Output 2 -----Note----- In the first and the second samples, Arya will beat all present opponents each of the d days. In the third sample, Arya will beat his opponents on days 1, 3 and 4 and his opponents will beat him on days 2 and 5. Thus, the maximum number of consecutive winning days is 2, which happens on days 3 and 4. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n,d = map(int,input().split()) ans = 0 has = 0 for i in range(d): s = input().count("1") if s==n: has = 0 else: has+=1 ans = max(ans,has) print(ans) ```
vfc_13950
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/688/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2 2\n10\n00\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 1\n0100\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 5\n1101\n1111\n0110\n1011\n1111\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 2\n110\n110\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 6\n1111111111\n0100110101\n1111111111\n0000011010\n1111111111\n1111111111\n", "output": "1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1050
Solve the following coding problem using the programming language python: Vus the Cossack holds a programming competition, in which $n$ people participate. He decided to award them all with pens and notebooks. It is known that Vus has exactly $m$ pens and $k$ notebooks. Determine whether the Cossack can reward all participants, giving each of them at least one pen and at least one notebook. -----Input----- The first line contains three integers $n$, $m$, and $k$ ($1 \leq n, m, k \leq 100$) — the number of participants, the number of pens, and the number of notebooks respectively. -----Output----- Print "Yes" if it possible to reward all the participants. Otherwise, print "No". You can print each letter in any case (upper or lower). -----Examples----- Input 5 8 6 Output Yes Input 3 9 3 Output Yes Input 8 5 20 Output No -----Note----- In the first example, there are $5$ participants. The Cossack has $8$ pens and $6$ notebooks. Therefore, he has enough pens and notebooks. In the second example, there are $3$ participants. The Cossack has $9$ pens and $3$ notebooks. He has more than enough pens but only the minimum needed number of notebooks. In the third example, there are $8$ participants but only $5$ pens. Since the Cossack does not have enough pens, the answer is "No". The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, m, k = list(map(int, input().split())) if m >= n and k >= n: print('Yes') else: print('No') ```
vfc_13954
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1186/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5 8 6\n", "output": "Yes\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 9 3\n", "output": "Yes\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "8 5 20\n", "output": "No\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1052
Solve the following coding problem using the programming language python: A permutation p of size n is an array such that every integer from 1 to n occurs exactly once in this array. Let's call a permutation an almost identity permutation iff there exist at least n - k indices i (1 ≤ i ≤ n) such that p_{i} = i. Your task is to count the number of almost identity permutations for given numbers n and k. -----Input----- The first line contains two integers n and k (4 ≤ n ≤ 1000, 1 ≤ k ≤ 4). -----Output----- Print the number of almost identity permutations for given n and k. -----Examples----- Input 4 1 Output 1 Input 4 2 Output 7 Input 5 3 Output 31 Input 5 4 Output 76 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys n, k = (int(i) for i in input().split()) ff = [1] * (n + 1) for i in range(1, n + 1) : ff[i] = ff[i - 1] * i dd = [0] * (n + 1) dd[1] = 0 dd[2] = 1 for i in range(3, n + 1) : dd[i] = (i - 1) * (dd[i - 1] + dd[i - 2]) ans = ff[n] for i in range(n - k) : c = ff[n] // ff[n - i] c = c // ff[i] c = c * dd[n - i] ans -= c print(ans) ```
vfc_13962
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/888/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4 1\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 2\n", "output": "7\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 3\n", "output": "31\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 4\n", "output": "76\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "200 1\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "200 2\n", "output": "19901\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1053
Solve the following coding problem using the programming language python: Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight $u \oplus v$ (where $\oplus$ is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph? You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph You can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree The weight of the minimum spanning tree is the sum of the weights on the edges included in it. -----Input----- The only line contains an integer n (2 ≤ n ≤ 10^12), the number of vertices in the graph. -----Output----- The only line contains an integer x, the weight of the graph's minimum spanning tree. -----Example----- Input 4 Output 4 -----Note----- In the first sample: [Image] The weight of the minimum spanning tree is 1+2+1=4. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python a = [0] cur = 1 while(cur < 10 ** 13): x = a[-1] a.append(x * 2 + cur) cur *= 2 n = int(input()) n -= 1 ans = 0 i = 1 cur = 1 while(n > 0): x = n % 2 n = n // 2 if(x > 0): ans += cur + a[i - 1] i += 1 cur *= 2 print(ans) ```
vfc_13966
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/959/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n", "output": "4", "type": "stdin_stdout" } ] }
apps
verifiable_code
1054
Solve the following coding problem using the programming language python: Many computer strategy games require building cities, recruiting army, conquering tribes, collecting resources. Sometimes it leads to interesting problems. Let's suppose that your task is to build a square city. The world map uses the Cartesian coordinates. The sides of the city should be parallel to coordinate axes. The map contains mines with valuable resources, located at some points with integer coordinates. The sizes of mines are relatively small, i.e. they can be treated as points. The city should be built in such a way that all the mines are inside or on the border of the city square. Building a city takes large amount of money depending on the size of the city, so you have to build the city with the minimum area. Given the positions of the mines find the minimum possible area of the city. -----Input----- The first line of the input contains number n — the number of mines on the map (2 ≤ n ≤ 1000). Each of the next n lines contains a pair of integers x_{i} and y_{i} — the coordinates of the corresponding mine ( - 10^9 ≤ x_{i}, y_{i} ≤ 10^9). All points are pairwise distinct. -----Output----- Print the minimum area of the city that can cover all the mines with valuable resources. -----Examples----- Input 2 0 0 2 2 Output 4 Input 2 0 0 0 3 Output 9 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = int(input()) xa = [] ya = [] for _ in range(n): x, y = map(int,input().split()) xa.append(x) ya.append(y) print(max(max(xa)-min(xa),max(ya)-min(ya))**2) ```
vfc_13970
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/485/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\n0 0\n2 2\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n0 0\n0 3\n", "output": "9\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n0 1\n1 0\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n2 2\n1 1\n3 3\n", "output": "4\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1055
Solve the following coding problem using the programming language python: Thanos sort is a supervillain sorting algorithm, which works as follows: if the array is not sorted, snap your fingers* to remove the first or the second half of the items, and repeat the process. Given an input array, what is the size of the longest sorted array you can obtain from it using Thanos sort? *Infinity Gauntlet required. -----Input----- The first line of input contains a single number $n$ ($1 \le n \le 16$) — the size of the array. $n$ is guaranteed to be a power of 2. The second line of input contains $n$ space-separated integers $a_i$ ($1 \le a_i \le 100$) — the elements of the array. -----Output----- Return the maximal length of a sorted array you can obtain using Thanos sort. The elements of the array have to be sorted in non-decreasing order. -----Examples----- Input 4 1 2 2 4 Output 4 Input 8 11 12 1 2 13 14 3 4 Output 2 Input 4 7 6 5 4 Output 1 -----Note----- In the first example the array is already sorted, so no finger snaps are required. In the second example the array actually has a subarray of 4 sorted elements, but you can not remove elements from different sides of the array in one finger snap. Each time you have to remove either the whole first half or the whole second half, so you'll have to snap your fingers twice to get to a 2-element sorted array. In the third example the array is sorted in decreasing order, so you can only save one element from the ultimate destruction. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = int(input()) a = [int(ai) for ai in input().split()] def solve(x): n = len(x) if sorted(x) == x: return n return max(solve(x[:n//2]), solve(x[n//2:])) print(solve(a)) ```
vfc_13974
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1145/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n1 2 2 4\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "8\n11 12 1 2 13 14 3 4\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n7 6 5 4\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n100\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n34 56\n", "output": "2\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1058
Solve the following coding problem using the programming language python: You are given $n$ blocks, each of them is of the form [color$_1$|value|color$_2$], where the block can also be flipped to get [color$_2$|value|color$_1$]. A sequence of blocks is called valid if the touching endpoints of neighboring blocks have the same color. For example, the sequence of three blocks A, B and C is valid if the left color of the B is the same as the right color of the A and the right color of the B is the same as the left color of C. The value of the sequence is defined as the sum of the values of the blocks in this sequence. Find the maximum possible value of the valid sequence that can be constructed from the subset of the given blocks. The blocks from the subset can be reordered and flipped if necessary. Each block can be used at most once in the sequence. -----Input----- The first line of input contains a single integer $n$ ($1 \le n \le 100$) — the number of given blocks. Each of the following $n$ lines describes corresponding block and consists of $\mathrm{color}_{1,i}$, $\mathrm{value}_i$ and $\mathrm{color}_{2,i}$ ($1 \le \mathrm{color}_{1,i}, \mathrm{color}_{2,i} \le 4$, $1 \le \mathrm{value}_i \le 100\,000$). -----Output----- Print exactly one integer — the maximum total value of the subset of blocks, which makes a valid sequence. -----Examples----- Input 6 2 1 4 1 2 4 3 4 4 2 8 3 3 16 3 1 32 2 Output 63 Input 7 1 100000 1 1 100000 2 1 100000 2 4 50000 3 3 50000 4 4 50000 4 3 50000 3 Output 300000 Input 4 1 1000 1 2 500 2 3 250 3 4 125 4 Output 1000 -----Note----- In the first example, it is possible to form a valid sequence from all blocks. One of the valid sequences is the following: [4|2|1] [1|32|2] [2|8|3] [3|16|3] [3|4|4] [4|1|2] The first block from the input ([2|1|4] $\to$ [4|1|2]) and second ([1|2|4] $\to$ [4|2|1]) are flipped. In the second example, the optimal answers can be formed from the first three blocks as in the following (the second or the third block from the input is flipped): [2|100000|1] [1|100000|1] [1|100000|2] In the third example, it is not possible to form a valid sequence of two or more blocks, so the answer is a sequence consisting only of the first block since it is the block with the largest value. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # import collections, atexit, math, sys, bisect sys.setrecursionlimit(1000000) def getIntList(): return list(map(int, input().split())) try : #raise ModuleNotFoundError import numpy def dprint(*args, **kwargs): print(*args, **kwargs, file=sys.stderr) dprint('debug mode') except ModuleNotFoundError: def dprint(*args, **kwargs): pass inId = 0 outId = 0 if inId>0: dprint('use input', inId) sys.stdin = open('input'+ str(inId) + '.txt', 'r') #标准输出重定向至文件 if outId>0: dprint('use output', outId) sys.stdout = open('stdout'+ str(outId) + '.txt', 'w') #标准输出重定向至文件 atexit.register(lambda :sys.stdout.close()) #idle 中不会执行 atexit N, = getIntList() zb = [] total = 0 de = [0,0,0,0,0] gr = [ [] for i in range(5)] minv = 10000000 for i in range(N): a,b,c = getIntList() de[a]+=1 de[c] +=1 zb.append( (a,b,c) ) total +=b if a!=c: minv = min(minv,b) vis = [0,0,0,0,0] def dfs( root): if vis[root]:return [] vis[root] = 1 r = [root,] for x in zb: a,b,c = x if a==root: r = r+ dfs(c) elif c==root: r = r+ dfs(a) return r res = 0 for i in range(1,5): if vis[i]:continue t = dfs(i) t = set(t) if len(t) ==4: for j in range(1,5): if de[j]%2==0: print(total) return print(total-minv) return tr = 0 for x in zb: a,b,c = x if a in t: tr +=b res = max(res,tr) print(res) ```
vfc_13986
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1038/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6\n2 1 4\n1 2 4\n3 4 4\n2 8 3\n3 16 3\n1 32 2\n", "output": "63", "type": "stdin_stdout" } ] }
apps
verifiable_code
1059
Solve the following coding problem using the programming language python: Tom loves vowels, and he likes long words with many vowels. His favorite words are vowelly words. We say a word of length $k$ is vowelly if there are positive integers $n$ and $m$ such that $n\cdot m = k$ and when the word is written by using $n$ rows and $m$ columns (the first row is filled first, then the second and so on, with each row filled from left to right), every vowel of the English alphabet appears at least once in every row and every column. You are given an integer $k$ and you must either print a vowelly word of length $k$ or print $-1$ if no such word exists. In this problem the vowels of the English alphabet are 'a', 'e', 'i', 'o' ,'u'. -----Input----- Input consists of a single line containing the integer $k$ ($1\leq k \leq 10^4$) — the required length. -----Output----- The output must consist of a single line, consisting of a vowelly word of length $k$ consisting of lowercase English letters if it exists or $-1$ if it does not. If there are multiple possible words, you may output any of them. -----Examples----- Input 7 Output -1 Input 36 Output agoeuioaeiruuimaeoieauoweouoiaouimae -----Note----- In the second example, the word "agoeuioaeiruuimaeoieauoweouoiaouimae" can be arranged into the following $6 \times 6$ grid: $\left. \begin{array}{|c|c|c|c|c|c|} \hline a & {g} & {o} & {e} & {u} & {i} \\ \hline o & {a} & {e} & {i} & {r} & {u} \\ \hline u & {i} & {m} & {a} & {e} & {o} \\ \hline i & {e} & {a} & {u} & {o} & {w} \\ \hline e & {o} & {u} & {o} & {i} & {a} \\ \hline o & {u} & {i} & {m} & {a} & {e} \\ \hline \end{array} \right.$ It is easy to verify that every row and every column contain all the vowels. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def main(): n = int(input()) for i in range(1, n): if n % i == 0: if i < 5 or n // i < 5: continue vowels = "aeiou" ind = 0 ans = "" for j in range(n // i): for k in range(i): ans += vowels[(j + k) % 5] print(ans) return 0 print(-1) return 0 main() ```
vfc_13990
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1166/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "7\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "36\n", "output": "aeiouaeiouaeiouaeiouaeiouaeiouaeioua", "type": "stdin_stdout" }, { "fn_name": null, "input": "24\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "20\n", "output": "-1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1060
Solve the following coding problem using the programming language python: As you must know, the maximum clique problem in an arbitrary graph is NP-hard. Nevertheless, for some graphs of specific kinds it can be solved effectively. Just in case, let us remind you that a clique in a non-directed graph is a subset of the vertices of a graph, such that any two vertices of this subset are connected by an edge. In particular, an empty set of vertexes and a set consisting of a single vertex, are cliques. Let's define a divisibility graph for a set of positive integers A = {a_1, a_2, ..., a_{n}} as follows. The vertices of the given graph are numbers from set A, and two numbers a_{i} and a_{j} (i ≠ j) are connected by an edge if and only if either a_{i} is divisible by a_{j}, or a_{j} is divisible by a_{i}. You are given a set of non-negative integers A. Determine the size of a maximum clique in a divisibility graph for set A. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^6), that sets the size of set A. The second line contains n distinct positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^6) — elements of subset A. The numbers in the line follow in the ascending order. -----Output----- Print a single number — the maximum size of a clique in a divisibility graph for set A. -----Examples----- Input 8 3 4 6 8 10 18 21 24 Output 3 -----Note----- In the first sample test a clique of size 3 is, for example, a subset of vertexes {3, 6, 18}. A clique of a larger size doesn't exist in this graph. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python R = lambda: map(int, input().split()) n = int(input()) dp = [0] * (10**6 + 1) for x in R(): dp[x] = 1 for i in range(10**6, -1, -1): if dp[i]: for x in range(i + i, 10**6 + 1, i): if dp[x]: dp[i] = max(dp[i], dp[x] + 1) print(max(dp)) ```
vfc_13994
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/566/F", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "8\n3 4 6 8 10 18 21 24\n", "output": "3\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1063
Solve the following coding problem using the programming language python: Peter wrote on the board a strictly increasing sequence of positive integers a_1, a_2, ..., a_{n}. Then Vasil replaced some digits in the numbers of this sequence by question marks. Thus, each question mark corresponds to exactly one lost digit. Restore the the original sequence knowing digits remaining on the board. -----Input----- The first line of the input contains integer n (1 ≤ n ≤ 10^5) — the length of the sequence. Next n lines contain one element of the sequence each. Each element consists only of digits and question marks. No element starts from digit 0. Each element has length from 1 to 8 characters, inclusive. -----Output----- If the answer exists, print in the first line "YES" (without the quotes). Next n lines must contain the sequence of positive integers — a possible variant of Peter's sequence. The found sequence must be strictly increasing, it must be transformed from the given one by replacing each question mark by a single digit. All numbers on the resulting sequence must be written without leading zeroes. If there are multiple solutions, print any of them. If there is no answer, print a single line "NO" (without the quotes). -----Examples----- Input 3 ? 18 1? Output YES 1 18 19 Input 2 ?? ? Output NO Input 5 12224 12??5 12226 ?0000 ?00000 Output YES 12224 12225 12226 20000 100000 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def solve(s, t, i, l): if i == l: return False if s[i] == "?": if solve(s, t, i + 1, l): s[i] = t[i] return True elif t[i] == "9": return False s[i] = nxt[t[i]] for j in range(i, l): if s[j] == "?": s[j] = "0" return True elif s[i] > t[i]: for j in range(i, l): if s[j] == "?": s[j] = "0" return True elif s[i] < t[i]: return False else: return solve(s, t, i + 1, l) n = int(input()) a = [list(input()) for _ in range(n)] p = ["0"] nxt = {str(x): str(x + 1) for x in range(9)} for i, ai in enumerate(a): if len(p) > len(ai): print("NO") break if len(p) < len(ai): if a[i][0] == "?": a[i][0] = "1" for j in range(len(ai)): if a[i][j] == "?": a[i][j] = "0" elif not solve(a[i], p, 0, len(ai)): print("NO") break p = a[i] else: print("YES") print("\n".join("".join(line) for line in a)) ```
vfc_14006
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/490/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n?\n18\n1?\n", "output": "YES\n1\n18\n19\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n??\n?\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n12224\n12??5\n12226\n?0000\n?00000\n", "output": "YES\n12224\n12225\n12226\n20000\n100000\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n473883\n3499005\n4?74792\n58146??\n8?90593\n9203?71\n?39055?\n1?692641\n11451902\n?22126?2\n", "output": "YES\n473883\n3499005\n4074792\n5814600\n8090593\n9203071\n9390550\n10692641\n11451902\n12212602\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1064
Solve the following coding problem using the programming language python: Adilbek's house is located on a street which can be represented as the OX axis. This street is really dark, so Adilbek wants to install some post lamps to illuminate it. Street has $n$ positions to install lamps, they correspond to the integer numbers from $0$ to $n - 1$ on the OX axis. However, some positions are blocked and no post lamp can be placed there. There are post lamps of different types which differ only by their power. When placed in position $x$, post lamp of power $l$ illuminates the segment $[x; x + l]$. The power of each post lamp is always a positive integer number. The post lamp shop provides an infinite amount of lamps of each type from power $1$ to power $k$. Though each customer is only allowed to order post lamps of exactly one type. Post lamps of power $l$ cost $a_l$ each. What is the minimal total cost of the post lamps of exactly one type Adilbek can buy to illuminate the entire segment $[0; n]$ of the street? If some lamps illuminate any other segment of the street, Adilbek does not care, so, for example, he may place a lamp of power $3$ in position $n - 1$ (even though its illumination zone doesn't completely belong to segment $[0; n]$). -----Input----- The first line contains three integer numbers $n$, $m$ and $k$ ($1 \le k \le n \le 10^6$, $0 \le m \le n$) — the length of the segment of the street Adilbek wants to illuminate, the number of the blocked positions and the maximum power of the post lamp available. The second line contains $m$ integer numbers $s_1, s_2, \dots, s_m$ ($0 \le s_1 < s_2 < \dots s_m < n$) — the blocked positions. The third line contains $k$ integer numbers $a_1, a_2, \dots, a_k$ ($1 \le a_i \le 10^6$) — the costs of the post lamps. -----Output----- Print the minimal total cost of the post lamps of exactly one type Adilbek can buy to illuminate the entire segment $[0; n]$ of the street. If illumintaing the entire segment $[0; n]$ is impossible, print -1. -----Examples----- Input 6 2 3 1 3 1 2 3 Output 6 Input 4 3 4 1 2 3 1 10 100 1000 Output 1000 Input 5 1 5 0 3 3 3 3 3 Output -1 Input 7 4 3 2 4 5 6 3 14 15 Output -1 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys from math import ceil n, m, k = list(map(int, sys.stdin.readline().split())) places = [True for _ in range(n)] for x in map(int, sys.stdin.readline().split()): places[x] = False costs = list(map(int, sys.stdin.readline().split())) if not places[0]: print(-1) return prev = [i for i in range(n)] last = 0 for i in range(n): if places[i]: last = i prev[i] = last best_cost = float('inf') for lamp in range(k, 0, -1): min_cost = ceil(n/lamp) * costs[lamp-1] if min_cost >= best_cost: continue # try this shit cost = costs[lamp-1] reach = lamp fail = False while reach < n: if prev[reach] + lamp <= reach: fail = True break reach = prev[reach] + lamp cost += costs[lamp - 1] if cost + (ceil((n - reach)/lamp) * costs[lamp-1]) >= best_cost: fail = True break if not fail: best_cost = min(best_cost, cost) print(best_cost if best_cost != float('inf') else -1) ```
vfc_14010
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/990/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6 2 3\n1 3\n1 2 3\n", "output": "6\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 3 4\n1 2 3\n1 10 100 1000\n", "output": "1000\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 1 5\n0\n3 3 3 3 3\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7 4 3\n2 4 5 6\n3 14 15\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 0 1\n\n1000000\n", "output": "1000000\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1 1\n0\n1000\n", "output": "-1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1065
Solve the following coding problem using the programming language python: $k$ people want to split $n$ candies between them. Each candy should be given to exactly one of them or be thrown away. The people are numbered from $1$ to $k$, and Arkady is the first of them. To split the candies, Arkady will choose an integer $x$ and then give the first $x$ candies to himself, the next $x$ candies to the second person, the next $x$ candies to the third person and so on in a cycle. The leftover (the remainder that is not divisible by $x$) will be thrown away. Arkady can't choose $x$ greater than $M$ as it is considered greedy. Also, he can't choose such a small $x$ that some person will receive candies more than $D$ times, as it is considered a slow splitting. Please find what is the maximum number of candies Arkady can receive by choosing some valid $x$. -----Input----- The only line contains four integers $n$, $k$, $M$ and $D$ ($2 \le n \le 10^{18}$, $2 \le k \le n$, $1 \le M \le n$, $1 \le D \le \min{(n, 1000)}$, $M \cdot D \cdot k \ge n$) — the number of candies, the number of people, the maximum number of candies given to a person at once, the maximum number of times a person can receive candies. -----Output----- Print a single integer — the maximum possible number of candies Arkady can give to himself. Note that it is always possible to choose some valid $x$. -----Examples----- Input 20 4 5 2 Output 8 Input 30 9 4 1 Output 4 -----Note----- In the first example Arkady should choose $x = 4$. He will give $4$ candies to himself, $4$ candies to the second person, $4$ candies to the third person, then $4$ candies to the fourth person and then again $4$ candies to himself. No person is given candies more than $2$ times, and Arkady receives $8$ candies in total. Note that if Arkady chooses $x = 5$, he will receive only $5$ candies, and if he chooses $x = 3$, he will receive only $3 + 3 = 6$ candies as well as the second person, the third and the fourth persons will receive $3$ candies, and $2$ candies will be thrown away. He can't choose $x = 1$ nor $x = 2$ because in these cases he will receive candies more than $2$ times. In the second example Arkady has to choose $x = 4$, because any smaller value leads to him receiving candies more than $1$ time. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, k, M, D = list(map(int, input().split())) ans = 0 for d in range(1, D + 1): bot = 0 top = M + 1 while (top > bot + 1): mid = (bot + top) // 2 cur = (d - 1) * mid * k; cur += mid; if (cur > n): top = mid; else: bot = mid; ans = max(ans, bot * d) print(ans); ```
vfc_14014
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/965/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "20 4 5 2\n", "output": "8\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "30 9 4 1\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 2 1 1\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "42 20 5 29\n", "output": "5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1000000000000000000 135 1000000000000000 1000\n", "output": "8325624421831635\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "100 33 100 100\n", "output": "100\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1066
Solve the following coding problem using the programming language python: Being a nonconformist, Volodya is displeased with the current state of things, particularly with the order of natural numbers (natural number is positive integer number). He is determined to rearrange them. But there are too many natural numbers, so Volodya decided to start with the first n. He writes down the following sequence of numbers: firstly all odd integers from 1 to n (in ascending order), then all even integers from 1 to n (also in ascending order). Help our hero to find out which number will stand at the position number k. -----Input----- The only line of input contains integers n and k (1 ≤ k ≤ n ≤ 10^12). Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Output----- Print the number that will stand at the position number k after Volodya's manipulations. -----Examples----- Input 10 3 Output 5 Input 7 7 Output 6 -----Note----- In the first sample Volodya's sequence will look like this: {1, 3, 5, 7, 9, 2, 4, 6, 8, 10}. The third place in the sequence is therefore occupied by the number 5. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python N,K=input().split() N,K=int(N),int(K) if(N%2==0): if(K<=N//2): print(2*K-1) else: K-=N//2 print(2*K) else: if(K<=N//2+1): print(2*K-1) else: K-=N//2+1 print(2*K) ```
vfc_14018
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/318/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "10 3\n", "output": "5", "type": "stdin_stdout" }, { "fn_name": null, "input": "7 7\n", "output": "6", "type": "stdin_stdout" }, { "fn_name": null, "input": "7 1\n", "output": "1", "type": "stdin_stdout" } ] }
apps
verifiable_code
1067
Solve the following coding problem using the programming language python: You are given $n$ numbers $a_1, a_2, \dots, a_n$. With a cost of one coin you can perform the following operation: Choose one of these numbers and add or subtract $1$ from it. In particular, we can apply this operation to the same number several times. We want to make the product of all these numbers equal to $1$, in other words, we want $a_1 \cdot a_2$ $\dots$ $\cdot a_n = 1$. For example, for $n = 3$ and numbers $[1, -3, 0]$ we can make product equal to $1$ in $3$ coins: add $1$ to second element, add $1$ to second element again, subtract $1$ from third element, so that array becomes $[1, -1, -1]$. And $1\cdot (-1) \cdot (-1) = 1$. What is the minimum cost we will have to pay to do that? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the number of numbers. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) — the numbers. -----Output----- Output a single number — the minimal number of coins you need to pay to make the product equal to $1$. -----Examples----- Input 2 -1 1 Output 2 Input 4 0 0 0 0 Output 4 Input 5 -5 -3 5 3 0 Output 13 -----Note----- In the first example, you can change $1$ to $-1$ or $-1$ to $1$ in $2$ coins. In the second example, you have to apply at least $4$ operations for the product not to be $0$. In the third example, you can change $-5$ to $-1$ in $4$ coins, $-3$ to $-1$ in $2$ coins, $5$ to $1$ in $4$ coins, $3$ to $1$ in $2$ coins, $0$ to $1$ in $1$ coin. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = int(input()) A = list(map(int, input().split())) m = 0 pos = 0 neg = 0 for i in range(n): if A[i] < 0: neg += 1 m += (-A[i] - 1) A[i] = -1 elif A[i] > 0: pos += 1 m += (A[i] - 1) A[i] = 1 zer = n - pos - neg if zer: print(m + zer) elif neg % 2 == 0: print(m) else: print(m + 2) ```
vfc_14022
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1206/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\n-1 1\n", "output": "2", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n0 0 0 0\n", "output": "4", "type": "stdin_stdout" } ] }
apps
verifiable_code
1068
Solve the following coding problem using the programming language python: A correct expression of the form a+b=c was written; a, b and c are non-negative integers without leading zeros. In this expression, the plus and equally signs were lost. The task is to restore the expression. In other words, one character '+' and one character '=' should be inserted into given sequence of digits so that: character'+' is placed on the left of character '=', characters '+' and '=' split the sequence into three non-empty subsequences consisting of digits (let's call the left part a, the middle part — b and the right part — c), all the three parts a, b and c do not contain leading zeros, it is true that a+b=c. It is guaranteed that in given tests answer always exists. -----Input----- The first line contains a non-empty string consisting of digits. The length of the string does not exceed 10^6. -----Output----- Output the restored expression. If there are several solutions, you can print any of them. Note that the answer at first should contain two terms (divided with symbol '+'), and then the result of their addition, before which symbol'=' should be. Do not separate numbers and operation signs with spaces. Strictly follow the output format given in the examples. If you remove symbol '+' and symbol '=' from answer string you should get a string, same as string from the input data. -----Examples----- Input 12345168 Output 123+45=168 Input 099 Output 0+9=9 Input 199100 Output 1+99=100 Input 123123123456456456579579579 Output 123123123+456456456=579579579 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def modgroup(M = 10**9+7, invn = 0) : exec(f'''class mod{M} : inv = [None] * {invn} if {invn} >= 2 : inv[1] = 1 for i in range(2, {invn}) : inv[i] = (({M}-{M}//i)*inv[{M}%i])%{M} def __init__(self, n = 0) : self.n = n % {M} __repr__ = lambda self : str(self.n) + '%{M}' __int__ = lambda self : self.n __eq__ = lambda a,b : a.n == b.n __add__ = lambda a,b : __class__(a.n + b.n) __sub__ = lambda a,b : __class__(a.n - b.n) __mul__ = lambda a,b : __class__(a.n * b.n) __pow__ = lambda a,b : __class__(pow(a.n, b.n, {M})) __truediv__ = lambda a,b : __class__(a.n * pow(b.n, {M-2}, {M})) __floordiv__ = lambda a,b : __class__(a.n * __class__.inv[b.n]) ''') return eval(f'mod{M}') def solution() : s = input() l = len(s) mod = modgroup() num = [mod(0)] * (l+1) # num[i] = int(s[:i]) <mod> shift = [mod(1)] * (l+1) # shift[i] = 10**i <mod> for i,x in enumerate(s, 1) : num[i] = num[i-1] * mod(10) + mod(int(x)) shift[i] = shift[i-1] * mod(10) def mod_check(la, lb, lc) : a,b,c = num[la], num[la+lb], num[la+lb+lc] c -= b * shift[lc] b -= a * shift[lb] return a + b == c for lc in range(l//3+bool(l%3), l//2+1) : for lb in (lc, lc-1, l-lc*2, l-lc*2+1) : la = l - lc - lb if la < 1 or lb < 1 or lc < 1 : continue if la > lc or lb > lc : continue if not mod_check(la, lb, lc) : continue a,b,c = s[:la], s[la:la+lb], s[la+lb:la+lb+lc] print(f'{a}+{b}={c}'); return solution() ```
vfc_14026
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/898/F", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "12345168\n", "output": "123+45=168\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "099\n", "output": "0+9=9\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "199100\n", "output": "1+99=100\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "123123123456456456579579579\n", "output": "123123123+456456456=579579579\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "112\n", "output": "1+1=2\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1069
Solve the following coding problem using the programming language python: Fedya studies in a gymnasium. Fedya's maths hometask is to calculate the following expression:(1^{n} + 2^{n} + 3^{n} + 4^{n}) mod 5 for given value of n. Fedya managed to complete the task. Can you? Note that given number n can be extremely large (e.g. it can exceed any integer type of your programming language). -----Input----- The single line contains a single integer n (0 ≤ n ≤ 10^10^5). The number doesn't contain any leading zeroes. -----Output----- Print the value of the expression without leading zeros. -----Examples----- Input 4 Output 4 Input 124356983594583453458888889 Output 0 -----Note----- Operation x mod y means taking remainder after division x by y. Note to the first sample: [Image] The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python sa=int(input()) if sa%4==0: print(4) else: print(0) ```
vfc_14030
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/456/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "124356983594583453458888889\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7854\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1070
Solve the following coding problem using the programming language python: There are $n$ houses along the road where Anya lives, each one is painted in one of $k$ possible colors. Anya likes walking along this road, but she doesn't like when two adjacent houses at the road have the same color. She wants to select a long segment of the road such that no two adjacent houses have the same color. Help Anya find the longest segment with this property. -----Input----- The first line contains two integers $n$ and $k$ — the number of houses and the number of colors ($1 \le n \le 100\,000$, $1 \le k \le 100\,000$). The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ — the colors of the houses along the road ($1 \le a_i \le k$). -----Output----- Output a single integer — the maximum number of houses on the road segment having no two adjacent houses of the same color. -----Example----- Input 8 3 1 2 3 3 2 1 2 2 Output 4 -----Note----- In the example, the longest segment without neighboring houses of the same color is from the house 4 to the house 7. The colors of the houses are $[3, 2, 1, 2]$ and its length is 4 houses. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n,k = list(map(int, input().split())) a=list(map(int, input().split())) last='' m = 0 s=0 for i in a: if i==last: s=1 else: s+=1 last=i if s>m: m=s print(m) ```
vfc_14034
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1090/M", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "8 3\n1 2 3 3 2 1 2 2\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1\n1\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 1\n1 1\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 2\n1 2\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 3\n1 2 3\n", "output": "3\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1071
Solve the following coding problem using the programming language python: Bizon the Champion is called the Champion for a reason. Bizon the Champion has recently got a present — a new glass cupboard with n shelves and he decided to put all his presents there. All the presents can be divided into two types: medals and cups. Bizon the Champion has a_1 first prize cups, a_2 second prize cups and a_3 third prize cups. Besides, he has b_1 first prize medals, b_2 second prize medals and b_3 third prize medals. Naturally, the rewards in the cupboard must look good, that's why Bizon the Champion decided to follow the rules: any shelf cannot contain both cups and medals at the same time; no shelf can contain more than five cups; no shelf can have more than ten medals. Help Bizon the Champion find out if we can put all the rewards so that all the conditions are fulfilled. -----Input----- The first line contains integers a_1, a_2 and a_3 (0 ≤ a_1, a_2, a_3 ≤ 100). The second line contains integers b_1, b_2 and b_3 (0 ≤ b_1, b_2, b_3 ≤ 100). The third line contains integer n (1 ≤ n ≤ 100). The numbers in the lines are separated by single spaces. -----Output----- Print "YES" (without the quotes) if all the rewards can be put on the shelves in the described manner. Otherwise, print "NO" (without the quotes). -----Examples----- Input 1 1 1 1 1 1 4 Output YES Input 1 1 3 2 3 4 2 Output YES Input 1 0 0 1 0 0 1 Output NO The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python a = list(map(int, input().split())) b = list(map(int, input().split())) n = int(input()) n1 = (sum(a) - 1) // 5 + 1 n2 = (sum(b) - 1) // 10 + 1 if n1 + n2 <= n: print('YES') else: print('NO') ```
vfc_14038
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/448/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1 1 1\n1 1 1\n4\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1 3\n2 3 4\n2\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 0 0\n1 0 0\n1\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "0 0 0\n0 0 0\n1\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "100 100 100\n100 100 100\n100\n", "output": "YES\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1072
Solve the following coding problem using the programming language python: You are given an n × m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the table abcd edfg hijk   we obtain the table: acd efg hjk   A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good. -----Input----- The first line contains two integers  — n and m (1 ≤ n, m ≤ 100). Next n lines contain m small English letters each — the characters of the table. -----Output----- Print a single number — the minimum number of columns that you need to remove in order to make the table good. -----Examples----- Input 1 10 codeforces Output 0 Input 4 4 case care test code Output 2 Input 5 4 code forc esco defo rces Output 4 -----Note----- In the first sample the table is already good. In the second sample you may remove the first and third column. In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition). Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python line = input().split() n = int(line[0]) m = int(line[1]) lst = [input()] sml = [] for i in range(n - 1): lst.append(input()) sml.append(False) ans = 0 for i in range(m): flag = True for j in range(n - 1): flag = flag and ((lst[j][i] <= lst[j + 1][i]) or sml[j]) if flag: for j in range(n - 1): if lst[j][i] < lst[j + 1][i]: sml[j] = True else: ans += 1 print(str(ans)) ```
vfc_14042
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/496/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1 10\ncodeforces\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1073
Solve the following coding problem using the programming language python: Calvin the robot lies in an infinite rectangular grid. Calvin's source code contains a list of n commands, each either 'U', 'R', 'D', or 'L' — instructions to move a single square up, right, down, or left, respectively. How many ways can Calvin execute a non-empty contiguous substrings of commands and return to the same square he starts in? Two substrings are considered different if they have different starting or ending indices. -----Input----- The first line of the input contains a single positive integer, n (1 ≤ n ≤ 200) — the number of commands. The next line contains n characters, each either 'U', 'R', 'D', or 'L' — Calvin's source code. -----Output----- Print a single integer — the number of contiguous substrings that Calvin can execute and return to his starting square. -----Examples----- Input 6 URLLDR Output 2 Input 4 DLUU Output 0 Input 7 RLRLRLR Output 12 -----Note----- In the first case, the entire source code works, as well as the "RL" substring in the second and third characters. Note that, in the third case, the substring "LR" appears three times, and is therefore counted three times to the total result. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = int(input()) s = input() ans = 0 for i in range(n): for j in range(i + 1, n + 1): t = s[i:j] ans += t.count('U') == t.count('D') and t.count('L') == t.count('R') print(ans) ```
vfc_14046
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/626/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6\nURLLDR\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\nDLUU\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1074
Solve the following coding problem using the programming language python: ++++++++[>+>++>+++>++++>+++++>++++++>+++++++>++++++++>+++++++++>++++++++++>+ ++++++++++>++++++++++++>+++++++++++++>++++++++++++++>+++++++++++++++>+++++++ +++++++++<<<<<<<<<<<<<<<<-]>>>>>>>>>>.<<<<<<<<<<>>>>>>>>>>>>>>++.--<<<<<<<<< <<<<<>>>>>>>>>>>>>+.-<<<<<<<<<<<<<>>>>>>>>>>>>>>--.++<<<<<<<<<<<<<<>>>>>>>>> >>>>>>----.++++<<<<<<<<<<<<<<<>>>>.<<<<>>>>>>>>>>>>>>--.++<<<<<<<<<<<<<<>>>> >>>>>>>>>>>---.+++<<<<<<<<<<<<<<<>>>>>>>>>>>>>>---.+++<<<<<<<<<<<<<<>>>>>>>> >>>>++.--<<<<<<<<<<<<>>>>>>>>>>>>>---.+++<<<<<<<<<<<<<>>>>>>>>>>>>>>++.--<<< <<<<<<<<<<<. DCBA:^!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPONMLKJIHdcbD`Y^]\UZYRv 9876543210/.-,+*)('&%$#"!~}|{zyxwvutsrqponm+*)('&%$#cya`=^]\[ZYXWVUTSRQPONML KJfe^cba`_X]VzTYRv98TSRQ3ONMLEi,+*)('&%$#"!~}|{zyxwvutsrqponmlkjihgfedcba`_^ ]\[ZYXWVUTSonPlkjchg`ed]#DCBA@?>=<;:9876543OHGLKDIHGFE>b%$#"!~}|{zyxwvutsrqp onmlkjihgfedcba`_^]\[ZYXWVUTSRQPONMibafedcba`_X|?>Z<XWVUTSRKo\ [Image] v34*8+6+,78+9*3+,93+9*5+,28+9*1+,55+9*4+,23*6*2*,91,@,+7*9*25,*48,+3*9+38,+< >62*9*2+,34*9*3+,66+9*8+,52*9*7+,75+9*8+,92+9*6+,48+9*3+,43*9*2+,84*,26*9*3^ -----Input----- The input contains a single integer a (0 ≤ a ≤ 1 000 000). -----Output----- Output a single integer. -----Example----- Input 129 Output 1 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python print(str(oct(int(input()))).count('1')) ```
vfc_14050
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/656/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "129\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "0\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "16\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "299593\n", "output": "7\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1075
Solve the following coding problem using the programming language python: Piegirl found the red button. You have one last chance to change the inevitable end. The circuit under the button consists of n nodes, numbered from 0 to n - 1. In order to deactivate the button, the n nodes must be disarmed in a particular order. Node 0 must be disarmed first. After disarming node i, the next node to be disarmed must be either node (2·i) modulo n or node (2·i) + 1 modulo n. The last node to be disarmed must be node 0. Node 0 must be disarmed twice, but all other nodes must be disarmed exactly once. Your task is to find any such order and print it. If there is no such order, print -1. -----Input----- Input consists of a single integer n (2 ≤ n ≤ 10^5). -----Output----- Print an order in which you can to disarm all nodes. If it is impossible, print -1 instead. If there are multiple orders, print any one of them. -----Examples----- Input 2 Output 0 1 0 Input 3 Output -1 Input 4 Output 0 1 3 2 0 Input 16 Output 0 1 2 4 9 3 6 13 10 5 11 7 15 14 12 8 0 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n=int(input()) if n&1: print(-1) else: D,R=[False]*(10**6),[0]*(10**6) i,j=0,0 while True: D[j]=True R[i]=j i+=1 if not D[(j+n)>>1]: j=(j+n)>>1 elif not D[j>>1]: j=j>>1 else: break print(" ".join(str(R[i]) for i in range(n,-1,-1))) ```
vfc_14054
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/325/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\n", "output": "0 1 0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n", "output": "-1", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n", "output": "0 1 3 2 0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1076
Solve the following coding problem using the programming language python: zscoder has a deck of $n+m$ custom-made cards, which consists of $n$ cards labelled from $1$ to $n$ and $m$ jokers. Since zscoder is lonely, he wants to play a game with himself using those cards. Initially, the deck is shuffled uniformly randomly and placed on the table. zscoder has a set $S$ which is initially empty. Every second, zscoder draws the top card from the deck. If the card has a number $x$ written on it, zscoder removes the card and adds $x$ to the set $S$. If the card drawn is a joker, zscoder places all the cards back into the deck and reshuffles (uniformly randomly) the $n+m$ cards to form a new deck (hence the new deck now contains all cards from $1$ to $n$ and the $m$ jokers). Then, if $S$ currently contains all the elements from $1$ to $n$, the game ends. Shuffling the deck doesn't take time at all. What is the expected number of seconds before the game ends? We can show that the answer can be written in the form $\frac{P}{Q}$ where $P, Q$ are relatively prime integers and $Q \neq 0 \bmod 998244353$. Output the value of $(P \cdot Q^{-1})$ modulo $998244353$. -----Input----- The only line of the input contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^{6}$). -----Output----- Output a single integer, the value of $(P \cdot Q^{-1})$ modulo $998244353$. -----Examples----- Input 2 1 Output 5 Input 3 2 Output 332748127 Input 14 9 Output 969862773 -----Note----- For the first sample, it can be proven that the expected time before the game ends is $5$ seconds. For the second sample, it can be proven that the expected time before the game ends is $\frac{28}{3}$ seconds. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys import math Q = 998244353 n, m = list(map(int, sys.stdin.readline().strip().split())) z = [1, 1] f = [1, 1] for i in range (0, n): f[0] = f[0] * (n - i) % Q f[1] = f[1] * (n + m - i) % Q z = [(z[0]*f[1]+z[1]*f[0]) % Q, (z[1]*f[1]) % Q] ans = [z[0] * (m+1), z[1]] for i in range (2, n + 1): ans = [(ans[0] * i * z[1] + ans[1] * m * z[0]) % Q, (ans[1] * i * z[1]) % Q] y = ans[1] ans = ans[0] q = Q - 2 while q > 0: if q % 2 == 1: ans = (ans * y) % Q q = q // 2 y = (y * y) % Q print(ans) ```
vfc_14058
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1392/H", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2 1\n", "output": "5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 2\n", "output": "332748127\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "14 9\n", "output": "969862773\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "20 20\n", "output": "445919622\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1077
Solve the following coding problem using the programming language python: Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a_1, a_2, ..., a_{n}, where a_{i} is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others. We define as b_{j} the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b_1, b_2, ..., b_{m} will be as large as possible. Find this maximum possible value of the minimum among the b_{j} (1 ≤ j ≤ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group. -----Input----- The first line of the input contains two integers n and m (1 ≤ m ≤ n ≤ 2000). The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9), where a_{i} is the performer of the i-th song. -----Output----- In the first line print two integers: the maximum possible value of the minimum among the b_{j} (1 ≤ j ≤ m), where b_{j} is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make. In the second line print the changed playlist. If there are multiple answers, print any of them. -----Examples----- Input 4 2 1 2 3 2 Output 2 1 1 2 1 2 Input 7 3 1 3 2 2 2 2 1 Output 2 1 1 3 3 2 2 2 1 Input 4 4 1000000000 100 7 1000000000 Output 1 4 1 2 3 4 -----Note----- In the first sample, after Polycarp's changes the first band performs two songs (b_1 = 2), and the second band also performs two songs (b_2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist. In the second sample, after Polycarp's changes the first band performs two songs (b_1 = 2), the second band performs three songs (b_2 = 3), and the third band also performs two songs (b_3 = 2). Thus, the best minimum value is 2. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python #!/usr/bin/env python3 from collections import Counter n, m = [int(x) for x in input().split()] a = [int(x) for x in input().split()] tgt = n // m b = Counter(a) rd = sum(b[x] for x in b if x > m) r = 0 for i in range(1, m+1): while rd and b[i] < tgt: for j in range(n): if a[j] > m: b[a[j]] -= 1 b[i] += 1 a[j] = i rd -= 1 r += 1 break while b[i] < tgt: for j in range(n): if b[a[j]] > tgt: b[a[j]] -= 1 b[i] += 1 a[j] = i r += 1 break print(tgt, r) print(" ".join(str(x) for x in a)) ```
vfc_14062
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/723/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4 2\n1 2 3 2\n", "output": "2 1\n1 2 1 2 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7 3\n1 3 2 2 2 2 1\n", "output": "2 1\n1 3 3 2 2 2 1 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 4\n1000000000 100 7 1000000000\n", "output": "1 4\n1 2 3 4 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1\n1\n", "output": "1 0\n1 \n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1078
Solve the following coding problem using the programming language python: Another Codeforces Round has just finished! It has gathered $n$ participants, and according to the results, the expected rating change of participant $i$ is $a_i$. These rating changes are perfectly balanced — their sum is equal to $0$. Unfortunately, due to minor technical glitches, the round is declared semi-rated. It means that all rating changes must be divided by two. There are two conditions though: For each participant $i$, their modified rating change $b_i$ must be integer, and as close to $\frac{a_i}{2}$ as possible. It means that either $b_i = \lfloor \frac{a_i}{2} \rfloor$ or $b_i = \lceil \frac{a_i}{2} \rceil$. In particular, if $a_i$ is even, $b_i = \frac{a_i}{2}$. Here $\lfloor x \rfloor$ denotes rounding down to the largest integer not greater than $x$, and $\lceil x \rceil$ denotes rounding up to the smallest integer not smaller than $x$. The modified rating changes must be perfectly balanced — their sum must be equal to $0$. Can you help with that? -----Input----- The first line contains a single integer $n$ ($2 \le n \le 13\,845$), denoting the number of participants. Each of the next $n$ lines contains a single integer $a_i$ ($-336 \le a_i \le 1164$), denoting the rating change of the $i$-th participant. The sum of all $a_i$ is equal to $0$. -----Output----- Output $n$ integers $b_i$, each denoting the modified rating change of the $i$-th participant in order of input. For any $i$, it must be true that either $b_i = \lfloor \frac{a_i}{2} \rfloor$ or $b_i = \lceil \frac{a_i}{2} \rceil$. The sum of all $b_i$ must be equal to $0$. If there are multiple solutions, print any. We can show that a solution exists for any valid input. -----Examples----- Input 3 10 -5 -5 Output 5 -2 -3 Input 7 -7 -29 0 3 24 -29 38 Output -3 -15 0 2 12 -15 19 -----Note----- In the first example, $b_1 = 5$, $b_2 = -3$ and $b_3 = -2$ is another correct solution. In the second example there are $6$ possible solutions, one of them is shown in the example output. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = int(input()) z = 0 for i in range(n): x = int(input()) if x % 2 == 0: print(x//2) else: if z == 1: print((x-1)//2) else: print((x+1)//2) z = 1 - z ```
vfc_14066
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1237/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n10\n-5\n-5\n", "output": "5\n-2\n-3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7\n-7\n-29\n0\n3\n24\n-29\n38\n", "output": "-3\n-15\n0\n2\n12\n-15\n19\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n0\n0\n", "output": "0\n0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "100\n-306\n-172\n26\n-328\n-241\n212\n-3\n270\n141\n111\n-56\n-46\n-27\n-54\n-253\n117\n118\n82\n189\n44\n-4\n55\n126\n-324\n219\n73\n-333\n-254\n-114\n-6\n74\n-263\n197\n-83\n311\n233\n-165\n84\n-241\n272\n-214\n10\n-173\n11\n230\n142\n-289\n10\n178\n62\n292\n-241\n101\n0\n-78\n65\n260\n168\n-313\n0\n-47\n250\n267\n58\n8\n30\n100\n-19\n129\n-140\n193\n-201\n77\n-270\n-246\n237\n61\n-140\n96\n-6\n45\n-102\n0\n-171\n-16\n-20\n81\n-149\n-241\n227\n15\n-47\n131\n-193\n-250\n311\n96\n-46\n-228\n218\n", "output": "-153\n-86\n13\n-164\n-120\n106\n-2\n135\n71\n55\n-28\n-23\n-13\n-27\n-127\n59\n59\n41\n94\n22\n-2\n28\n63\n-162\n109\n37\n-167\n-127\n-57\n-3\n37\n-131\n98\n-41\n155\n117\n-83\n42\n-120\n136\n-107\n5\n-87\n6\n115\n71\n-145\n5\n89\n31\n146\n-120\n50\n0\n-39\n33\n130\n84\n-157\n0\n-23\n125\n133\n29\n4\n15\n50\n-9\n64\n-70\n97\n-101\n39\n-135\n-123\n118\n31\n-70\n48\n-3\n22\n-51\n0\n-85\n-8\n-10\n40\n-74\n-121\n114\n7\n-23\n65\n-96\n-125\n155\n48\n-23\n-114\n109\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n-200\n200\n", "output": "-100\n100\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n-89\n89\n", "output": "-44\n44\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1079
Solve the following coding problem using the programming language python: Valera considers a number beautiful, if it equals 2^{k} or -2^{k} for some integer k (k ≥ 0). Recently, the math teacher asked Valera to represent number n as the sum of beautiful numbers. As Valera is really greedy, he wants to complete the task using as few beautiful numbers as possible. Help Valera and find, how many numbers he is going to need. In other words, if you look at all decompositions of the number n into beautiful summands, you need to find the size of the decomposition which has the fewest summands. -----Input----- The first line contains string s (1 ≤ |s| ≤ 10^6), that is the binary representation of number n without leading zeroes (n > 0). -----Output----- Print a single integer — the minimum amount of beautiful numbers that give a total of n. -----Examples----- Input 10 Output 1 Input 111 Output 2 Input 1101101 Output 4 -----Note----- In the first sample n = 2 is a beautiful number. In the second sample n = 7 and Valera can decompose it into sum 2^3 + ( - 2^0). In the third sample n = 109 can be decomposed into the sum of four summands as follows: 2^7 + ( - 2^4) + ( - 2^2) + 2^0. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python t = input() j = t[0] d, s = 0, int(j) for i in t[1: ]: if j != i: if d == 1: d, s = 0, s + 1 else: d = 1 j = i else: d = 1 print(s + (d and j == '1')) ```
vfc_14070
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/279/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "10\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "111\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1101101\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1100\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "11010\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "110100\n", "output": "3\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1081
Solve the following coding problem using the programming language python: -----Input----- The input contains a single integer $a$ ($1 \le a \le 99$). -----Output----- Output "YES" or "NO". -----Examples----- Input 5 Output YES Input 13 Output NO Input 24 Output NO Input 46 Output YES The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = int(input()) s = {1, 7, 9, 10, 11} if n < 12: if n in s: print("NO") else: print("YES") elif 12 < n < 30: print("NO") elif 69 < n < 80: print("NO") elif 89 < n: print("NO") else: if n % 10 not in {1, 7, 9}: print("YES") else: print("NO") ```
vfc_14078
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1145/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "13\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "24\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "46\n", "output": "YES\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1082
Solve the following coding problem using the programming language python: Petya was late for the lesson too. The teacher gave him an additional task. For some array a Petya should find the number of different ways to select non-empty subset of elements from it in such a way that their product is equal to a square of some integer. Two ways are considered different if sets of indexes of elements chosen by these ways are different. Since the answer can be very large, you should find the answer modulo 10^9 + 7. -----Input----- First line contains one integer n (1 ≤ n ≤ 10^5) — the number of elements in the array. Second line contains n integers a_{i} (1 ≤ a_{i} ≤ 70) — the elements of the array. -----Output----- Print one integer — the number of different ways to choose some elements so that their product is a square of a certain integer modulo 10^9 + 7. -----Examples----- Input 4 1 1 1 1 Output 15 Input 4 2 2 2 2 Output 7 Input 5 1 2 4 5 8 Output 7 -----Note----- In first sample product of elements chosen by any way is 1 and 1 = 1^2. So the answer is 2^4 - 1 = 15. In second sample there are six different ways to choose elements so that their product is 4, and only one way so that their product is 16. So the answer is 6 + 1 = 7. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def getmask(x): ans = 0 for i in range(2, x + 1): while x % (i * i) == 0: x //= i * i if x % i == 0: ans ^= 1 << i x //= i return ans def main(): maxn = 71 n = int(input()) a = [int(i) for i in input().split()] cnt = [0] * maxn for i in a: cnt[i] += 1 masks = {} for i in range(1, maxn): if cnt[i]: masks[getmask(i)] = masks.get(getmask(i), 0) + cnt[i] while len(masks) > 1 or 0 not in masks: if not masks: print(0) return fixed = max(masks.keys()) for i in list(masks.keys()): if i ^ fixed < i: masks[i ^ fixed] = masks.get(i ^ fixed, 0) + masks[i] masks[i] = 0 masks[0] = masks.get(0, 0) + masks[fixed] - 1 masks[fixed] = 0 masks = {i: j for i, j in list(masks.items()) if j > 0} print(pow(2, masks[0], 10**9+7) - 1) main() ```
vfc_14082
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/895/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n1 1 1 1\n", "output": "15\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1083
Solve the following coding problem using the programming language python: Petya has n integers: 1, 2, 3, ..., n. He wants to split these integers in two non-empty groups in such a way that the absolute difference of sums of integers in each group is as small as possible. Help Petya to split the integers. Each of n integers should be exactly in one group. -----Input----- The first line contains a single integer n (2 ≤ n ≤ 60 000) — the number of integers Petya has. -----Output----- Print the smallest possible absolute difference in the first line. In the second line print the size of the first group, followed by the integers in that group. You can print these integers in arbitrary order. If there are multiple answers, print any of them. -----Examples----- Input 4 Output 0 2 1 4 Input 2 Output 1 1 1 -----Note----- In the first example you have to put integers 1 and 4 in the first group, and 2 and 3 in the second. This way the sum in each group is 5, and the absolute difference is 0. In the second example there are only two integers, and since both groups should be non-empty, you have to put one integer in the first group and one in the second. This way the absolute difference of sums of integers in each group is 1. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = int(input()) f = [] s = [] f1 = s1 = 0 for i in range(n, 0, -1): if f1 <= s1: f1 += i f.append(i) else: s1 += i s.append(i) print(abs(f1 - s1)) print(len(f), *f) ```
vfc_14086
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/899/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n", "output": "0\n2 1 4 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n", "output": "1\n1 1 \n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1084
Solve the following coding problem using the programming language python: There is a rectangular grid of n rows of m initially-white cells each. Arkady performed a certain number (possibly zero) of operations on it. In the i-th operation, a non-empty subset of rows R_{i} and a non-empty subset of columns C_{i} are chosen. For each row r in R_{i} and each column c in C_{i}, the intersection of row r and column c is coloured black. There's another constraint: a row or a column can only be chosen at most once among all operations. In other words, it means that no pair of (i, j) (i < j) exists such that $R_{i} \cap R_{j} \neq \varnothing$ or $C_{i} \cap C_{j} \neq \varnothing$, where [Image] denotes intersection of sets, and $\varnothing$ denotes the empty set. You are to determine whether a valid sequence of operations exists that produces a given final grid. -----Input----- The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 50) — the number of rows and columns of the grid, respectively. Each of the following n lines contains a string of m characters, each being either '.' (denoting a white cell) or '#' (denoting a black cell), representing the desired setup. -----Output----- If the given grid can be achieved by any valid sequence of operations, output "Yes"; otherwise output "No" (both without quotes). You can print each character in any case (upper or lower). -----Examples----- Input 5 8 .#.#..#. .....#.. .#.#..#. #.#....# .....#.. Output Yes Input 5 5 ..#.. ..#.. ##### ..#.. ..#.. Output No Input 5 9 ........# #........ ..##.#... .......#. ....#.#.# Output No -----Note----- For the first example, the desired setup can be produced by 3 operations, as is shown below. [Image] For the second example, the desired setup cannot be produced, since in order to colour the center row, the third row and all columns must be selected in one operation, but after that no column can be selected again, hence it won't be possible to colour the other cells in the center column. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, m = list(map(int, input().split())) a = [-1] * m b = [] f = True for i in range(n): s = input() q = set() for j in range(len(s)): if (s[j] == "#"): q.add(j) for j in range(len(s)): if (s[j] == "#"): if (a[j] == -1): a[j] = i else: if b[a[j]] != q: f = False b.append(q) #print(a, b, f) if f: print("Yes") else: print("No") ```
vfc_14090
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/924/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5 8\n.#.#..#.\n.....#..\n.#.#..#.\n#.#....#\n.....#..\n", "output": "Yes\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 5\n..#..\n..#..\n#####\n..#..\n..#..\n", "output": "No\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 9\n........#\n#........\n..##.#...\n.......#.\n....#.#.#\n", "output": "No\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1091
Solve the following coding problem using the programming language python: In this problem we consider a special type of an auction, which is called the second-price auction. As in regular auction n bidders place a bid which is price a bidder ready to pay. The auction is closed, that is, each bidder secretly informs the organizer of the auction price he is willing to pay. After that, the auction winner is the participant who offered the highest price. However, he pay not the price he offers, but the highest price among the offers of other participants (hence the name: the second-price auction). Write a program that reads prices offered by bidders and finds the winner and the price he will pay. Consider that all of the offered prices are different. -----Input----- The first line of the input contains n (2 ≤ n ≤ 1000) — number of bidders. The second line contains n distinct integer numbers p_1, p_2, ... p_{n}, separated by single spaces (1 ≤ p_{i} ≤ 10000), where p_{i} stands for the price offered by the i-th bidder. -----Output----- The single output line should contain two integers: index of the winner and the price he will pay. Indices are 1-based. -----Examples----- Input 2 5 7 Output 2 5 Input 3 10 2 8 Output 1 8 Input 6 3 8 2 9 4 14 Output 6 9 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n=int(input()) L=list(map(int,input().split())) ind=L.index(max(L)) L.remove(max(L)) x=max(L) print(ind+1,x) ```
vfc_14118
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/386/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\n5 7\n", "output": "2 5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n10 2 8\n", "output": "1 8\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n3 8 2 9 4 14\n", "output": "6 9\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n4707 7586 4221 5842\n", "output": "2 5842\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n3304 4227 4869 6937 6002\n", "output": "4 6002\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n5083 3289 7708 5362 9031 7458\n", "output": "5 7708\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1092
Solve the following coding problem using the programming language python: There are n lights aligned in a row. These lights are numbered 1 to n from left to right. Initially some of the lights are switched on. Shaass wants to switch all the lights on. At each step he can switch a light on (this light should be switched off at that moment) if there's at least one adjacent light which is already switched on. He knows the initial state of lights and he's wondering how many different ways there exist to switch all the lights on. Please find the required number of ways modulo 1000000007 (10^9 + 7). -----Input----- The first line of the input contains two integers n and m where n is the number of lights in the sequence and m is the number of lights which are initially switched on, (1 ≤ n ≤ 1000, 1 ≤ m ≤ n). The second line contains m distinct integers, each between 1 to n inclusive, denoting the indices of lights which are initially switched on. -----Output----- In the only line of the output print the number of different possible ways to switch on all the lights modulo 1000000007 (10^9 + 7). -----Examples----- Input 3 1 1 Output 1 Input 4 2 1 4 Output 2 Input 11 2 4 8 Output 6720 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, m = map(int, input().split()) t = sorted(map(int, input().split())) f, d = [1] * (n + 1), 1000000007 for i in range(2, n + 1): f[i] = (f[i - 1] * i) % d p, q = 0, (f[t[0] - 1] * f[n - t[-1]]) % d for i in range(m - 1): l = t[i + 1] - t[i] - 1 q = (q * f[l]) % d if l > 1: p += l - 1 print(pow(2, p, d) * f[n - m] * pow(q, d - 2, d) % d) ```
vfc_14122
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/294/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 1\n1\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 2\n1 4\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "11 2\n4 8\n", "output": "6720\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 2\n1 3\n", "output": "2\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1093
Solve the following coding problem using the programming language python: Профиль горного хребта схематично задан в виде прямоугольной таблицы из символов «.» (пустое пространство) и «*» (часть горы). Каждый столбец таблицы содержит хотя бы одну «звёздочку». Гарантируется, что любой из символов «*» либо находится в нижней строке матрицы, либо непосредственно под ним находится другой символ «*». ........... .........*. .*.......*. **.......*. **..*...**. *********** Пример изображения горного хребта. Маршрут туриста проходит через весь горный хребет слева направо. Каждый день турист перемещается вправо — в соседний столбец в схематичном изображении. Конечно, каждый раз он поднимается (или опускается) в самую верхнюю точку горы, которая находится в соответствующем столбце. Считая, что изначально турист находится в самой верхней точке в первом столбце, а закончит свой маршрут в самой верхней точке в последнем столбце, найдите две величины: наибольший подъём за день (равен 0, если в профиле горного хребта нет ни одного подъёма), наибольший спуск за день (равен 0, если в профиле горного хребта нет ни одного спуска). -----Входные данные----- В первой строке входных данных записаны два целых числа n и m (1 ≤ n, m ≤ 100) — количество строк и столбцов в схематичном изображении соответственно. Далее следуют n строк по m символов в каждой — схематичное изображение горного хребта. Каждый символ схематичного изображения — это либо «.», либо «*». Каждый столбец матрицы содержит хотя бы один символ «*». Гарантируется, что любой из символов «*» либо находится в нижней строке матрицы, либо непосредственно под ним находится другой символ «*». -----Выходные данные----- Выведите через пробел два целых числа: величину наибольшего подъёма за день (или 0, если в профиле горного хребта нет ни одного подъёма), величину наибольшего спуска за день (или 0, если в профиле горного хребта нет ни одного спуска). -----Примеры----- Входные данные 6 11 ........... .........*. .*.......*. **.......*. **..*...**. *********** Выходные данные 3 4 Входные данные 5 5 ....* ...** ..*** .**** ***** Выходные данные 1 0 Входные данные 8 7 ....... .*..... .*..... .**.... .**.*.. .****.* .****** ******* Выходные данные 6 2 -----Примечание----- В первом тестовом примере высоты гор равны: 3, 4, 1, 1, 2, 1, 1, 1, 2, 5, 1. Наибольший подъем равен 3 и находится между горой номер 9 (её высота равна 2) и горой номер 10 (её высота равна 5). Наибольший спуск равен 4 и находится между горой номер 10 (её высота равна 5) и горой номер 11 (её высота равна 1). Во втором тестовом примере высоты гор равны: 1, 2, 3, 4, 5. Наибольший подъём равен 1 и находится, например, между горой номер 2 (ее высота равна 2) и горой номер 3 (её высота равна 3). Так как в данном горном хребте нет спусков, то величина наибольшего спуска равна 0. В третьем тестовом примере высоты гор равны: 1, 7, 5, 3, 4, 2, 3. Наибольший подъём равен 6 и находится между горой номер 1 (её высота равна 1) и горой номер 2 (её высота равна 7). Наибольший спуск равен 2 и находится между горой номер 2 (её высота равна 7) и горой номер 3 (её высота равна 5). Такой же спуск находится между горой номер 5 (её высота равна 4) и горой номер 6 (её высота равна 2). The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def main(): n, m = [int(i) for i in input().split()] d = [list(input()) for i in range(n)] a = [0] * m for i in range(m): for j in range(n): if d[j][i] == '*': a[i] += 1 x = y = 0 for i in range(1, m): if a[i] > a[i - 1]: x = max(x, a[i] - a[i - 1]) else: y = max(y, a[i - 1] - a[i]) print(x, y) main() ```
vfc_14126
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/648/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6 11\n...........\n.........*.\n.*.......*.\n**.......*.\n**..*...**.\n***********\n", "output": "3 4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 5\n....*\n...**\n..***\n.****\n*****\n", "output": "1 0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "8 7\n.......\n.*.....\n.*.....\n.**....\n.**.*..\n.****.*\n.******\n*******\n", "output": "6 2\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1094
Solve the following coding problem using the programming language python: Polycarp is a big lover of killing time in social networks. A page with a chatlist in his favourite network is made so that when a message is sent to some friend, his friend's chat rises to the very top of the page. The relative order of the other chats doesn't change. If there was no chat with this friend before, then a new chat is simply inserted to the top of the list. Assuming that the chat list is initially empty, given the sequence of Polycaprus' messages make a list of chats after all of his messages are processed. Assume that no friend wrote any message to Polycarpus. -----Input----- The first line contains integer n (1 ≤ n ≤ 200 000) — the number of Polycarpus' messages. Next n lines enlist the message recipients in the order in which the messages were sent. The name of each participant is a non-empty sequence of lowercase English letters of length at most 10. -----Output----- Print all the recipients to who Polycarp talked to in the order of chats with them, from top to bottom. -----Examples----- Input 4 alex ivan roman ivan Output ivan roman alex Input 8 alina maria ekaterina darya darya ekaterina maria alina Output alina maria ekaterina darya -----Note----- In the first test case Polycarpus first writes to friend by name "alex", and the list looks as follows: alex Then Polycarpus writes to friend by name "ivan" and the list looks as follows: ivan alex Polycarpus writes the third message to friend by name "roman" and the list looks as follows: roman ivan alex Polycarpus writes the fourth message to friend by name "ivan", to who he has already sent a message, so the list of chats changes as follows: ivan roman alex The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = int(input()) d = dict() for i in range(n): d[input()] = i lst = list(d.items()) lst.sort(key=lambda x:-x[1]) for i in lst: print(i[0]) ```
vfc_14130
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/637/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\nalex\nivan\nroman\nivan\n", "output": "ivan\nroman\nalex\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1095
Solve the following coding problem using the programming language python: You are given a tube which is reflective inside represented as two non-coinciding, but parallel to $Ox$ lines. Each line has some special integer points — positions of sensors on sides of the tube. You are going to emit a laser ray in the tube. To do so, you have to choose two integer points $A$ and $B$ on the first and the second line respectively (coordinates can be negative): the point $A$ is responsible for the position of the laser, and the point $B$ — for the direction of the laser ray. The laser ray is a ray starting at $A$ and directed at $B$ which will reflect from the sides of the tube (it doesn't matter if there are any sensors at a reflection point or not). A sensor will only register the ray if the ray hits exactly at the position of the sensor. [Image] Examples of laser rays. Note that image contains two examples. The $3$ sensors (denoted by black bold points on the tube sides) will register the blue ray but only $2$ will register the red. Calculate the maximum number of sensors which can register your ray if you choose points $A$ and $B$ on the first and the second lines respectively. -----Input----- The first line contains two integers $n$ and $y_1$ ($1 \le n \le 10^5$, $0 \le y_1 \le 10^9$) — number of sensors on the first line and its $y$ coordinate. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$) — $x$ coordinates of the sensors on the first line in the ascending order. The third line contains two integers $m$ and $y_2$ ($1 \le m \le 10^5$, $y_1 < y_2 \le 10^9$) — number of sensors on the second line and its $y$ coordinate. The fourth line contains $m$ integers $b_1, b_2, \ldots, b_m$ ($0 \le b_i \le 10^9$) — $x$ coordinates of the sensors on the second line in the ascending order. -----Output----- Print the only integer — the maximum number of sensors which can register the ray. -----Example----- Input 3 1 1 5 6 1 3 3 Output 3 -----Note----- One of the solutions illustrated on the image by pair $A_2$ and $B_2$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n,w56=map(int,input().split()) if(n==1): x=[int(input())] else: x=[int(i) for i in input().split()] k,w90=map(int,input().split()) if(k==1): y=[int(input())] else: y=[int(i) for i in input().split()] t=0 import collections for i in range(1,35): m=collections.Counter() for j in range(n): m[((x[j]-(2**(i-1)))%(2**i))]+=1 for j in range(k): m[(y[j])%(2**i)]+=1 t=max(t,max([m[o] for o in m.keys()])) if(t>=2): print(t) else: p=0 for i in range(len(x)): for j in range(len(y)): if(x[i]==y[j]): print(2) p=1 break if(p==1): break if(p==0): print(1) ```
vfc_14134
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1041/F", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 1\n1 5 6\n1 3\n3\n", "output": "3\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1096
Solve the following coding problem using the programming language python: The only king stands on the standard chess board. You are given his position in format "cd", where c is the column from 'a' to 'h' and d is the row from '1' to '8'. Find the number of moves permitted for the king. Check the king's moves here https://en.wikipedia.org/wiki/King_(chess). [Image] King moves from the position e4 -----Input----- The only line contains the king's position in the format "cd", where 'c' is the column from 'a' to 'h' and 'd' is the row from '1' to '8'. -----Output----- Print the only integer x — the number of moves permitted for the king. -----Example----- Input e4 Output 8 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python #!/usr/bin/env python3 try: while True: s = input() if s[0] in "ah" and s[1] in "18": print(3) elif s[0] in "ah" or s[1] in "18": print(5) else: print(8) except EOFError: pass ```
vfc_14138
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/710/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "e4\n", "output": "8\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "a1\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "h8\n", "output": "3\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1097
Solve the following coding problem using the programming language python: There are n cities in Berland, each of them has a unique id — an integer from 1 to n, the capital is the one with id 1. Now there is a serious problem in Berland with roads — there are no roads. That is why there was a decision to build n - 1 roads so that there will be exactly one simple path between each pair of cities. In the construction plan t integers a_1, a_2, ..., a_{t} were stated, where t equals to the distance from the capital to the most distant city, concerning new roads. a_{i} equals the number of cities which should be at the distance i from the capital. The distance between two cities is the number of roads one has to pass on the way from one city to another. Also, it was decided that among all the cities except the capital there should be exactly k cities with exactly one road going from each of them. Such cities are dead-ends and can't be economically attractive. In calculation of these cities the capital is not taken into consideration regardless of the number of roads from it. Your task is to offer a plan of road's construction which satisfies all the described conditions or to inform that it is impossible. -----Input----- The first line contains three positive numbers n, t and k (2 ≤ n ≤ 2·10^5, 1 ≤ t, k < n) — the distance to the most distant city from the capital and the number of cities which should be dead-ends (the capital in this number is not taken into consideration). The second line contains a sequence of t integers a_1, a_2, ..., a_{t} (1 ≤ a_{i} < n), the i-th number is the number of cities which should be at the distance i from the capital. It is guaranteed that the sum of all the values a_{i} equals n - 1. -----Output----- If it is impossible to built roads which satisfy all conditions, print -1. Otherwise, in the first line print one integer n — the number of cities in Berland. In the each of the next n - 1 line print two integers — the ids of cities that are connected by a road. Each road should be printed exactly once. You can print the roads and the cities connected by a road in any order. If there are multiple answers, print any of them. Remember that the capital has id 1. -----Examples----- Input 7 3 3 2 3 1 Output 7 1 3 2 1 2 6 2 4 7 4 3 5 Input 14 5 6 4 4 2 2 1 Output 14 3 1 1 4 11 6 1 2 10 13 6 10 10 12 14 12 8 4 5 1 3 7 2 6 5 9 Input 3 1 1 2 Output -1 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys import math def main(): n,t,k = map(int,sys.stdin.readline().split()) a = list(map(int,sys.stdin.readline().split())) g = [[] for i in range(t+1)] g[0].append([1,-1,0]) c = 1 for i in range(t): for j in range(a[i]): c+=1 g[i+1].append([c,0,0]) g[i][0][2]+=1 l=0 for i in range(1,t+1): for j in range(len(g[i])): if g[i][j][2]==0: l+=1 if l< k: print(-1) return i=0 j=0 m = 1 while l>k and m<t: while i< len(g[m]) and g[m][i][2]>0: i+=1 if i>=len(g[m]): i=0 j=0 m+=1 continue while j<len(g[m+1]) and g[m][g[m+1][j][1]][2]<2: j+=1 if j>=len(g[m+1]): i=0 j=0 m+=1 continue g[m][i][2]+=1 g[m][g[m+1][j][1]][2]-=1 g[m+1][j][1] = i l-=1 i+=1 j+=1 if l!=k: print(-1) return print(n) for i in range(1,t+1): for j in range(len(g[i])): print(g[i][j][0], g[i-1][g[i][j][1]][0]) main() ```
vfc_14142
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/746/G", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "7 3 3\n2 3 1\n", "output": "7\n2 1\n2 4\n2 6\n7 4\n3 5\n3 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "14 5 6\n4 4 2 2 1\n", "output": "14\n1 3\n6 2\n3 7\n6 11\n9 5\n10 12\n10 13\n12 14\n5 1\n10 6\n8 4\n1 4\n1 2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 1 1\n2\n", "output": "-1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1098
Solve the following coding problem using the programming language python: Polycarp has a strict daily schedule. He has n alarms set for each day, and the i-th alarm rings each day at the same time during exactly one minute. Determine the longest time segment when Polycarp can sleep, i. e. no alarm rings in that period. It is possible that Polycarp begins to sleep in one day, and wakes up in another. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 100) — the number of alarms. Each of the next n lines contains a description of one alarm. Each description has a format "hh:mm", where hh is the hour when the alarm rings, and mm is the minute of that hour when the alarm rings. The number of hours is between 0 and 23, and the number of minutes is between 0 and 59. All alarm times are distinct. The order of the alarms is arbitrary. Each alarm starts ringing in the beginning of the corresponding minute and rings for exactly one minute (i. e. stops ringing in the beginning of the next minute). Polycarp can start sleeping instantly when no alarm is ringing, and he wakes up at the moment when some alarm starts ringing. -----Output----- Print a line in format "hh:mm", denoting the maximum time Polycarp can sleep continuously. hh denotes the number of hours, and mm denotes the number of minutes. The number of minutes should be between 0 and 59. Look through examples to understand the format better. -----Examples----- Input 1 05:43 Output 23:59 Input 4 22:00 03:21 16:03 09:59 Output 06:37 -----Note----- In the first example there is only one alarm which rings during one minute of a day, and then rings again on the next day, 23 hours and 59 minutes later. Polycarp can sleep all this time. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = int(input()) a = [] for i in range(n): h, m = map(int, input().split(":")) a.append((h + 24) * 60 + m) a.append(h * 60 + m) a.sort() j = 0 s = 0 ans = 0 for i in range(0, 48 * 60): if (j < 2 * n and a[j] == i): ans = max(ans, s) s = 0 j += 1 continue else: s += 1 h = ans // 60 m = ans % 60 hans = "" mans = "" if h < 10: hans = "0" + str(h) else: hans = str(h) if m < 10: mans = "0" + str(m) else: mans = str(m) print(hans + ":" + mans) ```
vfc_14146
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/926/I", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1\n05:43\n", "output": "23:59\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n22:00\n03:21\n16:03\n09:59\n", "output": "06:37\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "20\n23:59\n00:00\n00:01\n00:02\n00:03\n00:04\n00:05\n00:06\n00:07\n00:08\n00:09\n00:10\n00:11\n00:12\n00:13\n00:14\n00:15\n00:16\n00:17\n00:18\n", "output": "23:40\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "21\n23:28\n23:29\n23:30\n23:31\n23:32\n23:33\n23:34\n23:35\n23:36\n23:37\n23:38\n23:39\n23:40\n23:41\n23:42\n23:43\n23:44\n23:45\n23:46\n23:47\n23:48\n", "output": "23:39\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1099
Solve the following coding problem using the programming language python: You are given a tree with $n$ vertices. You are allowed to modify the structure of the tree through the following multi-step operation: Choose three vertices $a$, $b$, and $c$ such that $b$ is adjacent to both $a$ and $c$. For every vertex $d$ other than $b$ that is adjacent to $a$, remove the edge connecting $d$ and $a$ and add the edge connecting $d$ and $c$. Delete the edge connecting $a$ and $b$ and add the edge connecting $a$ and $c$. As an example, consider the following tree: [Image] The following diagram illustrates the sequence of steps that happen when we apply an operation to vertices $2$, $4$, and $5$: [Image] It can be proven that after each operation, the resulting graph is still a tree. Find the minimum number of operations that must be performed to transform the tree into a star. A star is a tree with one vertex of degree $n - 1$, called its center, and $n - 1$ vertices of degree $1$. -----Input----- The first line contains an integer $n$ ($3 \le n \le 2 \cdot 10^5$)  — the number of vertices in the tree. The $i$-th of the following $n - 1$ lines contains two integers $u_i$ and $v_i$ ($1 \le u_i, v_i \le n$, $u_i \neq v_i$) denoting that there exists an edge connecting vertices $u_i$ and $v_i$. It is guaranteed that the given edges form a tree. -----Output----- Print a single integer  — the minimum number of operations needed to transform the tree into a star. It can be proven that under the given constraints, it is always possible to transform the tree into a star using at most $10^{18}$ operations. -----Examples----- Input 6 4 5 2 6 3 2 1 2 2 4 Output 1 Input 4 2 4 4 1 3 4 Output 0 -----Note----- The first test case corresponds to the tree shown in the statement. As we have seen before, we can transform the tree into a star with center at vertex $5$ by applying a single operation to vertices $2$, $4$, and $5$. In the second test case, the given tree is already a star with the center at vertex $4$, so no operations have to be performed. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys input = sys.stdin.readline n = int(input()) adj = [[] for i in range(n)] for _ in range(n - 1): u, v = list(map(int, input().split())) adj[u-1].append(v-1) adj[v-1].append(u-1) depth = [-1] * n depth[0] = 0 odd = 0 even = 1 q = [0] while q: nex = q.pop() for v in adj[nex]: if depth[v] == -1: depth[v] = depth[nex] + 1 if depth[v] & 1: odd += 1 else: even += 1 q.append(v) print(min(odd,even) - 1) ```
vfc_14150
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1375/G", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6\n4 5\n2 6\n3 2\n1 2\n2 4\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n2 4\n4 1\n3 4\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n2 1\n3 2\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n1 2\n2 3\n3 4\n4 5\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n1 2\n5 2\n2 4\n2 3\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1100
Solve the following coding problem using the programming language python: Ari the monster always wakes up very early with the first ray of the sun and the first thing she does is feeding her squirrel. Ari draws a regular convex polygon on the floor and numbers it's vertices 1, 2, ..., n in clockwise order. Then starting from the vertex 1 she draws a ray in the direction of each other vertex. The ray stops when it reaches a vertex or intersects with another ray drawn before. Ari repeats this process for vertex 2, 3, ..., n (in this particular order). And then she puts a walnut in each region inside the polygon. [Image] Ada the squirrel wants to collect all the walnuts, but she is not allowed to step on the lines drawn by Ari. That means Ada have to perform a small jump if she wants to go from one region to another. Ada can jump from one region P to another region Q if and only if P and Q share a side or a corner. Assuming that Ada starts from outside of the picture, what is the minimum number of jumps she has to perform in order to collect all the walnuts? -----Input----- The first and only line of the input contains a single integer n (3 ≤ n ≤ 54321) - the number of vertices of the regular polygon drawn by Ari. -----Output----- Print the minimum number of jumps Ada should make to collect all the walnuts. Note, that she doesn't need to leave the polygon after. -----Examples----- Input 5 Output 9 Input 3 Output 1 -----Note----- One of the possible solutions for the first sample is shown on the picture above. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = int(input()) print((n-2)**2) ```
vfc_14154
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/592/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n", "output": "9\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "54321\n", "output": "2950553761\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n", "output": "16\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7\n", "output": "25\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1101
Solve the following coding problem using the programming language python: In an attempt to escape the Mischievous Mess Makers' antics, Farmer John has abandoned his farm and is traveling to the other side of Bovinia. During the journey, he and his k cows have decided to stay at the luxurious Grand Moo-dapest Hotel. The hotel consists of n rooms located in a row, some of which are occupied. Farmer John wants to book a set of k + 1 currently unoccupied rooms for him and his cows. He wants his cows to stay as safe as possible, so he wishes to minimize the maximum distance from his room to the room of his cow. The distance between rooms i and j is defined as |j - i|. Help Farmer John protect his cows by calculating this minimum possible distance. -----Input----- The first line of the input contains two integers n and k (1 ≤ k < n ≤ 100 000) — the number of rooms in the hotel and the number of cows travelling with Farmer John. The second line contains a string of length n describing the rooms. The i-th character of the string will be '0' if the i-th room is free, and '1' if the i-th room is occupied. It is guaranteed that at least k + 1 characters of this string are '0', so there exists at least one possible choice of k + 1 rooms for Farmer John and his cows to stay in. -----Output----- Print the minimum possible distance between Farmer John's room and his farthest cow. -----Examples----- Input 7 2 0100100 Output 2 Input 5 1 01010 Output 2 Input 3 2 000 Output 1 -----Note----- In the first sample, Farmer John can book room 3 for himself, and rooms 1 and 4 for his cows. The distance to the farthest cow is 2. Note that it is impossible to make this distance 1, as there is no block of three consecutive unoccupied rooms. In the second sample, Farmer John can book room 1 for himself and room 3 for his single cow. The distance between him and his cow is 2. In the third sample, Farmer John books all three available rooms, taking the middle room for himself so that both cows are next to him. His distance from the farthest cow is 1. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python ans = 1000000000 n, k = [int(i) for i in input().split()] s = input() l = len(s) d = [k // 2, (k + 1) // 2] nearl = [0] * n nearr = [0] * n last = 1000000000 for i in range(n): if s[i] == '0': last = i nearl[i] = last for i in range(n - 1, -1, -1): if s[i] == '0': last = i nearr[i] = last for i in d: itl = 0 nf = 0 itr = 0 while s[itl] == '1': itl += 1 cnt = 0 nf = itl while cnt < i: cnt += 1 nf += 1 while s[nf] == '1': nf += 1 cnt = 0 itr = nf while cnt < k - i: cnt += 1 itr += 1 while s[itr] == '1': itr += 1 while True: pos = (itr + itl) // 2 pos1 = nearl[pos] ans = min(ans, max(pos1 - itl, itr - pos1)) pos1 = nearr[pos] ans = min(ans, max(pos1 - itl, itr - pos1)) itr += 1 while itr < l and s[itr] == '1': itr += 1 if itr == l: break itl += 1 while s[itl] == '1': itl += 1 print(ans) ```
vfc_14158
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/645/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "7 2\n0100100\n", "output": "2\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1102
Solve the following coding problem using the programming language python: There are n cities in Bearland, numbered 1 through n. Cities are arranged in one long row. The distance between cities i and j is equal to |i - j|. Limak is a police officer. He lives in a city a. His job is to catch criminals. It's hard because he doesn't know in which cities criminals are. Though, he knows that there is at most one criminal in each city. Limak is going to use a BCD (Bear Criminal Detector). The BCD will tell Limak how many criminals there are for every distance from a city a. After that, Limak can catch a criminal in each city for which he is sure that there must be a criminal. You know in which cities criminals are. Count the number of criminals Limak will catch, after he uses the BCD. -----Input----- The first line of the input contains two integers n and a (1 ≤ a ≤ n ≤ 100) — the number of cities and the index of city where Limak lives. The second line contains n integers t_1, t_2, ..., t_{n} (0 ≤ t_{i} ≤ 1). There are t_{i} criminals in the i-th city. -----Output----- Print the number of criminals Limak will catch. -----Examples----- Input 6 3 1 1 1 0 1 0 Output 3 Input 5 2 0 0 0 1 0 Output 1 -----Note----- In the first sample, there are six cities and Limak lives in the third one (blue arrow below). Criminals are in cities marked red. [Image] Using the BCD gives Limak the following information: There is one criminal at distance 0 from the third city — Limak is sure that this criminal is exactly in the third city. There is one criminal at distance 1 from the third city — Limak doesn't know if a criminal is in the second or fourth city. There are two criminals at distance 2 from the third city — Limak is sure that there is one criminal in the first city and one in the fifth city. There are zero criminals for every greater distance. So, Limak will catch criminals in cities 1, 3 and 5, that is 3 criminals in total. In the second sample (drawing below), the BCD gives Limak the information that there is one criminal at distance 2 from Limak's city. There is only one city at distance 2 so Limak is sure where a criminal is. [Image] The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, a = list(map(int, input().split())) x = list(map(int, input().split())) a -= 1 result = x[a] for i in range(1, n + 1): le = a - i rg = a + i le_i = le >= 0 and le < n rg_i = rg >= 0 and rg < n if not le_i and not rg_i: break if le_i and not rg_i: result += x[le] elif not le_i and rg_i: result += x[rg] else: if x[le] == x[rg] == 1: result += 2 print(result) ```
vfc_14162
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/680/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6 3\n1 1 1 0 1 0\n", "output": "3\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1104
Solve the following coding problem using the programming language python: When Masha came to math classes today, she saw two integer sequences of length $n - 1$ on the blackboard. Let's denote the elements of the first sequence as $a_i$ ($0 \le a_i \le 3$), and the elements of the second sequence as $b_i$ ($0 \le b_i \le 3$). Masha became interested if or not there is an integer sequence of length $n$, which elements we will denote as $t_i$ ($0 \le t_i \le 3$), so that for every $i$ ($1 \le i \le n - 1$) the following is true: $a_i = t_i | t_{i + 1}$ (where $|$ denotes the bitwise OR operation) and $b_i = t_i \& t_{i + 1}$ (where $\&$ denotes the bitwise AND operation). The question appeared to be too difficult for Masha, so now she asked you to check whether such a sequence $t_i$ of length $n$ exists. If it exists, find such a sequence. If there are multiple such sequences, find any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 10^5$) — the length of the sequence $t_i$. The second line contains $n - 1$ integers $a_1, a_2, \ldots, a_{n-1}$ ($0 \le a_i \le 3$) — the first sequence on the blackboard. The third line contains $n - 1$ integers $b_1, b_2, \ldots, b_{n-1}$ ($0 \le b_i \le 3$) — the second sequence on the blackboard. -----Output----- In the first line print "YES" (without quotes), if there is a sequence $t_i$ that satisfies the conditions from the statements, and "NO" (without quotes), if there is no such sequence. If there is such a sequence, on the second line print $n$ integers $t_1, t_2, \ldots, t_n$ ($0 \le t_i \le 3$) — the sequence that satisfies the statements conditions. If there are multiple answers, print any of them. -----Examples----- Input 4 3 3 2 1 2 0 Output YES 1 3 2 0 Input 3 1 3 3 2 Output NO -----Note----- In the first example it's easy to see that the sequence from output satisfies the given conditions: $t_1 | t_2 = (01_2) | (11_2) = (11_2) = 3 = a_1$ and $t_1 \& t_2 = (01_2) \& (11_2) = (01_2) = 1 = b_1$; $t_2 | t_3 = (11_2) | (10_2) = (11_2) = 3 = a_2$ and $t_2 \& t_3 = (11_2) \& (10_2) = (10_2) = 2 = b_2$; $t_3 | t_4 = (10_2) | (00_2) = (10_2) = 2 = a_3$ and $t_3 \& t_4 = (10_2) \& (00_2) = (00_2) = 0 = b_3$. In the second example there is no such sequence. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) def try_solve(t): for i in range(n-1): ok = False for x in range(4): if a[i] == t[i] | x and b[i] == t[i] & x: t.append(x) ok = True break if not ok: return False return True ok = False for x in range(4): t = [x] if try_solve(t): print("YES") print(" ".join(map(str, t))) ok = True break if not ok: print("NO") ```
vfc_14170
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1031/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n3 3 2\n1 2 0\n", "output": "YES\n1 3 2 0 ", "type": "stdin_stdout" } ] }
apps
verifiable_code
1105
Solve the following coding problem using the programming language python: During the "Russian Code Cup" programming competition, the testing system stores all sent solutions for each participant. We know that many participants use random numbers in their programs and are often sent several solutions with the same source code to check. Each participant is identified by some unique positive integer k, and each sent solution A is characterized by two numbers: x — the number of different solutions that are sent before the first solution identical to A, and k — the number of the participant, who is the author of the solution. Consequently, all identical solutions have the same x. It is known that the data in the testing system are stored in the chronological order, that is, if the testing system has a solution with number x (x > 0) of the participant with number k, then the testing system has a solution with number x - 1 of the same participant stored somewhere before. During the competition the checking system crashed, but then the data of the submissions of all participants have been restored. Now the jury wants to verify that the recovered data is in chronological order. Help the jury to do so. -----Input----- The first line of the input contains an integer n (1 ≤ n ≤ 10^5) — the number of solutions. Each of the following n lines contains two integers separated by space x and k (0 ≤ x ≤ 10^5; 1 ≤ k ≤ 10^5) — the number of previous unique solutions and the identifier of the participant. -----Output----- A single line of the output should contain «YES» if the data is in chronological order, and «NO» otherwise. -----Examples----- Input 2 0 1 1 1 Output YES Input 4 0 1 1 2 1 1 0 2 Output NO Input 4 0 1 1 1 0 1 0 2 Output YES The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = int(input()) a = [-1]*100001 p = 0 for i in range(n): x, k = map(int, input().split()) if a[k] < x-1: p = 1 else: a[k] = max(a[k],x) if p: print('NO') else: print('YES') ```
vfc_14174
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/417/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\n0 1\n1 1\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n0 1\n1 2\n1 1\n0 2\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n0 1\n1 1\n0 1\n0 2\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n7 1\n4 2\n8 2\n1 8\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n0 8\n0 5\n", "output": "YES\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1106
Solve the following coding problem using the programming language python: Om Nom is the main character of a game "Cut the Rope". He is a bright little monster who likes visiting friends living at the other side of the park. However the dark old parks can scare even somebody as fearless as Om Nom, so he asks you to help him. [Image] The park consists of 2^{n} + 1 - 1 squares connected by roads so that the scheme of the park is a full binary tree of depth n. More formally, the entrance to the park is located at the square 1. The exits out of the park are located at squares 2^{n}, 2^{n} + 1, ..., 2^{n} + 1 - 1 and these exits lead straight to the Om Nom friends' houses. From each square i (2 ≤ i < 2^{n} + 1) there is a road to the square $\lfloor \frac{i}{2} \rfloor$. Thus, it is possible to go from the park entrance to each of the exits by walking along exactly n roads. [Image] To light the path roads in the evening, the park keeper installed street lights along each road. The road that leads from square i to square $\lfloor \frac{i}{2} \rfloor$ has a_{i} lights. Om Nom loves counting lights on the way to his friend. Om Nom is afraid of spiders who live in the park, so he doesn't like to walk along roads that are not enough lit. What he wants is that the way to any of his friends should have in total the same number of lights. That will make him feel safe. He asked you to help him install additional lights. Determine what minimum number of lights it is needed to additionally place on the park roads so that a path from the entrance to any exit of the park contains the same number of street lights. You may add an arbitrary number of street lights to each of the roads. -----Input----- The first line contains integer n (1 ≤ n ≤ 10) — the number of roads on the path from the entrance to any exit. The next line contains 2^{n} + 1 - 2 numbers a_2, a_3, ... a_2^{n} + 1 - 1 — the initial numbers of street lights on each road of the park. Here a_{i} is the number of street lights on the road between squares i and $\lfloor \frac{i}{2} \rfloor$. All numbers a_{i} are positive integers, not exceeding 100. -----Output----- Print the minimum number of street lights that we should add to the roads of the park to make Om Nom feel safe. -----Examples----- Input 2 1 2 3 4 5 6 Output 5 -----Note----- Picture for the sample test. Green color denotes the additional street lights. [Image] The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def dfs(i): if i >= 2 ** n: return 0, 0 x1, m1 = dfs(i * 2) x2, m2 = dfs(i * 2 + 1) if m1 + a[i * 2] < m2 + a[i * 2 + 1]: return x1 + x2 + m2 + a[i * 2 + 1] - m1 - a[i * 2], m2 + a[i * 2 + 1] return x1 + x2 + m1 + a[i * 2] - m2 - a[i * 2 + 1], m1 + a[i * 2] n = int(input()) a = [0, 0] + [int(i) for i in input().split()] print(dfs(1)[0]) ```
vfc_14178
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/526/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\n1 2 3 4 5 6\n", "output": "5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n1 2 3 3 2 2\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n39 52\n", "output": "13\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n59 96 34 48 8 72\n", "output": "139\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n87 37 91 29 58 45 51 74 70 71 47 38 91 89\n", "output": "210\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1107
Solve the following coding problem using the programming language python: Everybody knows that the Berland citizens are keen on health, especially students. Berland students are so tough that all they drink is orange juice! Yesterday one student, Vasya and his mates made some barbecue and they drank this healthy drink only. After they ran out of the first barrel of juice, they decided to play a simple game. All n people who came to the barbecue sat in a circle (thus each person received a unique index b_{i} from 0 to n - 1). The person number 0 started the game (this time it was Vasya). All turns in the game were numbered by integers starting from 1. If the j-th turn was made by the person with index b_{i}, then this person acted like that: he pointed at the person with index (b_{i} + 1) mod n either with an elbow or with a nod (x mod y is the remainder after dividing x by y); if j ≥ 4 and the players who had turns number j - 1, j - 2, j - 3, made during their turns the same moves as player b_{i} on the current turn, then he had drunk a glass of juice; the turn went to person number (b_{i} + 1) mod n. The person who was pointed on the last turn did not make any actions. The problem was, Vasya's drunk too much juice and can't remember the goal of the game. However, Vasya's got the recorded sequence of all the participants' actions (including himself). Now Vasya wants to find out the maximum amount of juice he could drink if he played optimally well (the other players' actions do not change). Help him. You can assume that in any scenario, there is enough juice for everybody. -----Input----- The first line contains a single integer n (4 ≤ n ≤ 2000) — the number of participants in the game. The second line describes the actual game: the i-th character of this line equals 'a', if the participant who moved i-th pointed at the next person with his elbow, and 'b', if the participant pointed with a nod. The game continued for at least 1 and at most 2000 turns. -----Output----- Print a single integer — the number of glasses of juice Vasya could have drunk if he had played optimally well. -----Examples----- Input 4 abbba Output 1 Input 4 abbab Output 0 -----Note----- In both samples Vasya has got two turns — 1 and 5. In the first sample, Vasya could have drunk a glass of juice during the fifth turn if he had pointed at the next person with a nod. In this case, the sequence of moves would look like "abbbb". In the second sample Vasya wouldn't drink a single glass of juice as the moves performed during turns 3 and 4 are different. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = int(input()) moves = input() turns_vasnja = int((len(moves)-1) / n) count = 0 for i in range(1, turns_vasnja+1): if moves[n * i - 3] == moves[n * i - 2] == moves[n * i - 1]: count += 1 print(count) ```
vfc_14182
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/332/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\nabbba\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\nabbab\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\naaa\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\naab\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1108
Solve the following coding problem using the programming language python: George has recently entered the BSUCP (Berland State University for Cool Programmers). George has a friend Alex who has also entered the university. Now they are moving into a dormitory. George and Alex want to live in the same room. The dormitory has n rooms in total. At the moment the i-th room has p_{i} people living in it and the room can accommodate q_{i} people in total (p_{i} ≤ q_{i}). Your task is to count how many rooms has free place for both George and Alex. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 100) — the number of rooms. The i-th of the next n lines contains two integers p_{i} and q_{i} (0 ≤ p_{i} ≤ q_{i} ≤ 100) — the number of people who already live in the i-th room and the room's capacity. -----Output----- Print a single integer — the number of rooms where George and Alex can move in. -----Examples----- Input 3 1 1 2 2 3 3 Output 0 Input 3 1 10 0 10 10 10 Output 2 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n=int(input()) ans=0 for i in range(n): a,b=map(int,input().split()) if b-a>=2: ans+=1 print(ans) ```
vfc_14186
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/467/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n1 1\n2 2\n3 3\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n1 10\n0 10\n10 10\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n36 67\n61 69\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n21 71\n10 88\n43 62\n", "output": "3\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1109
Solve the following coding problem using the programming language python: This task will exclusively concentrate only on the arrays where all elements equal 1 and/or 2. Array a is k-period if its length is divisible by k and there is such array b of length k, that a is represented by array b written exactly $\frac{n}{k}$ times consecutively. In other words, array a is k-periodic, if it has period of length k. For example, any array is n-periodic, where n is the array length. Array [2, 1, 2, 1, 2, 1] is at the same time 2-periodic and 6-periodic and array [1, 2, 1, 1, 2, 1, 1, 2, 1] is at the same time 3-periodic and 9-periodic. For the given array a, consisting only of numbers one and two, find the minimum number of elements to change to make the array k-periodic. If the array already is k-periodic, then the required value equals 0. -----Input----- The first line of the input contains a pair of integers n, k (1 ≤ k ≤ n ≤ 100), where n is the length of the array and the value n is divisible by k. The second line contains the sequence of elements of the given array a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 2), a_{i} is the i-th element of the array. -----Output----- Print the minimum number of array elements we need to change to make the array k-periodic. If the array already is k-periodic, then print 0. -----Examples----- Input 6 2 2 1 2 2 2 1 Output 1 Input 8 4 1 1 2 1 1 1 2 1 Output 0 Input 9 3 2 1 1 1 2 1 1 1 2 Output 3 -----Note----- In the first sample it is enough to change the fourth element from 2 to 1, then the array changes to [2, 1, 2, 1, 2, 1]. In the second sample, the given array already is 4-periodic. In the third sample it is enough to replace each occurrence of number two by number one. In this case the array will look as [1, 1, 1, 1, 1, 1, 1, 1, 1] — this array is simultaneously 1-, 3- and 9-periodic. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, k = map(int, input().split()) A = list(map(int, input().split())) ans = 0 for i in range(k): c1 = c2 = 0 for j in range(i, n, k): if A[j] == 1: c1 += 1 else: c2 += 1 ans += min(c1, c2) print(ans) ```
vfc_14190
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/371/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6 2\n2 1 2 2 2 1\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "8 4\n1 1 2 1 1 1 2 1\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "9 3\n2 1 1 1 2 1 1 1 2\n", "output": "3\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1110
Solve the following coding problem using the programming language python: Manao is trying to open a rather challenging lock. The lock has n buttons on it and to open it, you should press the buttons in a certain order to open the lock. When you push some button, it either stays pressed into the lock (that means that you've guessed correctly and pushed the button that goes next in the sequence), or all pressed buttons return to the initial position. When all buttons are pressed into the lock at once, the lock opens. Consider an example with three buttons. Let's say that the opening sequence is: {2, 3, 1}. If you first press buttons 1 or 3, the buttons unpress immediately. If you first press button 2, it stays pressed. If you press 1 after 2, all buttons unpress. If you press 3 after 2, buttons 3 and 2 stay pressed. As soon as you've got two pressed buttons, you only need to press button 1 to open the lock. Manao doesn't know the opening sequence. But he is really smart and he is going to act in the optimal way. Calculate the number of times he's got to push a button in order to open the lock in the worst-case scenario. -----Input----- A single line contains integer n (1 ≤ n ≤ 2000) — the number of buttons the lock has. -----Output----- In a single line print the number of times Manao has to push a button in the worst-case scenario. -----Examples----- Input 2 Output 3 Input 3 Output 7 -----Note----- Consider the first test sample. Manao can fail his first push and push the wrong button. In this case he will already be able to guess the right one with his second push. And his third push will push the second right button. Thus, in the worst-case scenario he will only need 3 pushes. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python x=int(input()) print(round(x*x*(x+1)/2-(x*(x+1)*((2*x)+1)/6)+(x))) ```
vfc_14194
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/268/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\n", "output": "3\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1111
Solve the following coding problem using the programming language python: You are given a set of n elements indexed from 1 to n. The weight of i-th element is w_{i}. The weight of some subset of a given set is denoted as $W(S) =|S|\cdot \sum_{i \in S} w_{i}$. The weight of some partition R of a given set into k subsets is $W(R) = \sum_{S \in R} W(S)$ (recall that a partition of a given set is a set of its subsets such that every element of the given set belongs to exactly one subset in partition). Calculate the sum of weights of all partitions of a given set into exactly k non-empty subsets, and print it modulo 10^9 + 7. Two partitions are considered different iff there exist two elements x and y such that they belong to the same set in one of the partitions, and to different sets in another partition. -----Input----- The first line contains two integers n and k (1 ≤ k ≤ n ≤ 2·10^5) — the number of elements and the number of subsets in each partition, respectively. The second line contains n integers w_{i} (1 ≤ w_{i} ≤ 10^9)— weights of elements of the set. -----Output----- Print one integer — the sum of weights of all partitions of a given set into k non-empty subsets, taken modulo 10^9 + 7. -----Examples----- Input 4 2 2 3 2 3 Output 160 Input 5 2 1 2 3 4 5 Output 645 -----Note----- Possible partitions in the first sample: {{1, 2, 3}, {4}}, W(R) = 3·(w_1 + w_2 + w_3) + 1·w_4 = 24; {{1, 2, 4}, {3}}, W(R) = 26; {{1, 3, 4}, {2}}, W(R) = 24; {{1, 2}, {3, 4}}, W(R) = 2·(w_1 + w_2) + 2·(w_3 + w_4) = 20; {{1, 3}, {2, 4}}, W(R) = 20; {{1, 4}, {2, 3}}, W(R) = 20; {{1}, {2, 3, 4}}, W(R) = 26; Possible partitions in the second sample: {{1, 2, 3, 4}, {5}}, W(R) = 45; {{1, 2, 3, 5}, {4}}, W(R) = 48; {{1, 2, 4, 5}, {3}}, W(R) = 51; {{1, 3, 4, 5}, {2}}, W(R) = 54; {{2, 3, 4, 5}, {1}}, W(R) = 57; {{1, 2, 3}, {4, 5}}, W(R) = 36; {{1, 2, 4}, {3, 5}}, W(R) = 37; {{1, 2, 5}, {3, 4}}, W(R) = 38; {{1, 3, 4}, {2, 5}}, W(R) = 38; {{1, 3, 5}, {2, 4}}, W(R) = 39; {{1, 4, 5}, {2, 3}}, W(R) = 40; {{2, 3, 4}, {1, 5}}, W(R) = 39; {{2, 3, 5}, {1, 4}}, W(R) = 40; {{2, 4, 5}, {1, 3}}, W(R) = 41; {{3, 4, 5}, {1, 2}}, W(R) = 42. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, k = list(map(int, input().split())) MOD = 10**9+7 def fast_modinv(up_to, M): ''' Fast modular inverses of 1..up_to modulo M. ''' modinv = [-1 for _ in range(up_to + 1)] modinv[1] = 1 for x in range(2, up_to + 1): modinv[x] = (-(M//x) * modinv[M%x])%M return modinv maxn = 2*10**5 + 10 modinv = fast_modinv(maxn, MOD) fact, factinv = [1], [1] for i in range(1, maxn): fact.append(fact[-1]*i % MOD) factinv.append(factinv[-1]*modinv[i] % MOD) def Stirling(n, k): '''The Stirling number of second kind (number of nonempty partitions). ''' if k > n: return 0 result = 0 for j in range(k+1): result += (-1 if (k-j)&1 else 1) * fact[k] * factinv[j] * factinv[k - j] * pow(j, n, MOD) % MOD result %= MOD result *= factinv[k] return result % MOD W = sum(map(int, input().split())) % MOD print((Stirling(n, k) + (n - 1) * Stirling(n - 1, k))* W % MOD) ```
vfc_14198
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/961/G", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4 2\n2 3 2 3\n", "output": "160\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 2\n1 2 3 4 5\n", "output": "645\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1\n1\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1\n1000000000\n", "output": "1000000000\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1113
Solve the following coding problem using the programming language python: Initially Ildar has an empty array. He performs $n$ steps. On each step he takes a subset of integers already added to the array and appends the mex of this subset to the array. The mex of an multiset of integers is the smallest non-negative integer not presented in the multiset. For example, the mex of the multiset $[0, 2, 3]$ is $1$, while the mex of the multiset $[1, 2, 1]$ is $0$. More formally, on the step $m$, when Ildar already has an array $a_1, a_2, \ldots, a_{m-1}$, he chooses some subset of indices $1 \leq i_1 < i_2 < \ldots < i_k < m$ (possibly, empty), where $0 \leq k < m$, and appends the $mex(a_{i_1}, a_{i_2}, \ldots a_{i_k})$ to the end of the array. After performing all the steps Ildar thinks that he might have made a mistake somewhere. He asks you to determine for a given array $a_1, a_2, \ldots, a_n$ the minimum step $t$ such that he has definitely made a mistake on at least one of the steps $1, 2, \ldots, t$, or determine that he could have obtained this array without mistakes. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 100\,000$) — the number of steps Ildar made. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i \leq 10^9$) — the array Ildar obtained. -----Output----- If Ildar could have chosen the subsets on each step in such a way that the resulting array is $a_1, a_2, \ldots, a_n$, print $-1$. Otherwise print a single integer $t$ — the smallest index of a step such that a mistake was made on at least one step among steps $1, 2, \ldots, t$. -----Examples----- Input 4 0 1 2 1 Output -1 Input 3 1 0 1 Output 1 Input 4 0 1 2 239 Output 4 -----Note----- In the first example it is possible that Ildar made no mistakes. Here is the process he could have followed. $1$-st step. The initial array is empty. He can choose an empty subset and obtain $0$, because the mex of an empty set is $0$. Appending this value to the end he gets the array $[0]$. $2$-nd step. The current array is $[0]$. He can choose a subset $[0]$ and obtain an integer $1$, because $mex(0) = 1$. Appending this value to the end he gets the array $[0,1]$. $3$-rd step. The current array is $[0,1]$. He can choose a subset $[0,1]$ and obtain an integer $2$, because $mex(0,1) = 2$. Appending this value to the end he gets the array $[0,1,2]$. $4$-th step. The current array is $[0,1,2]$. He can choose a subset $[0]$ and obtain an integer $1$, because $mex(0) = 1$. Appending this value to the end he gets the array $[0,1,2,1]$. Thus, he can get the array without mistakes, so the answer is $-1$. In the second example he has definitely made a mistake on the very first step, because he could not have obtained anything different from $0$. In the third example he could have obtained $[0, 1, 2]$ without mistakes, but $239$ is definitely wrong. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = int(input()) a = list(map(int, input().split())) mx = -1 for step, elem in enumerate(a): if elem > mx + 1: print(step + 1) return else: mx = max(mx, elem) print(-1) ```
vfc_14206
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1054/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n0 1 2 1\n", "output": "-1", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n1 0 1\n", "output": "1", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n0 1 2 239\n", "output": "4", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n0\n", "output": "-1", "type": "stdin_stdout" } ] }
apps
verifiable_code
1115
Solve the following coding problem using the programming language python: Pasha is participating in a contest on one well-known website. This time he wants to win the contest and will do anything to get to the first place! This contest consists of n problems, and Pasha solves ith problem in a_{i} time units (his solutions are always correct). At any moment of time he can be thinking about a solution to only one of the problems (that is, he cannot be solving two problems at the same time). The time Pasha spends to send his solutions is negligible. Pasha can send any number of solutions at the same moment. Unfortunately, there are too many participants, and the website is not always working. Pasha received the information that the website will be working only during m time periods, jth period is represented by its starting moment l_{j} and ending moment r_{j}. Of course, Pasha can send his solution only when the website is working. In other words, Pasha can send his solution at some moment T iff there exists a period x such that l_{x} ≤ T ≤ r_{x}. Pasha wants to know his best possible result. We need to tell him the minimal moment of time by which he is able to have solutions to all problems submitted, if he acts optimally, or say that it's impossible no matter how Pasha solves the problems. -----Input----- The first line contains one integer n (1 ≤ n ≤ 1000) — the number of problems. The second line contains n integers a_{i} (1 ≤ a_{i} ≤ 10^5) — the time Pasha needs to solve ith problem. The third line contains one integer m (0 ≤ m ≤ 1000) — the number of periods of time when the website is working. Next m lines represent these periods. jth line contains two numbers l_{j} and r_{j} (1 ≤ l_{j} < r_{j} ≤ 10^5) — the starting and the ending moment of jth period. It is guaranteed that the periods are not intersecting and are given in chronological order, so for every j > 1 the condition l_{j} > r_{j} - 1 is met. -----Output----- If Pasha can solve and submit all the problems before the end of the contest, print the minimal moment of time by which he can have all the solutions submitted. Otherwise print "-1" (without brackets). -----Examples----- Input 2 3 4 2 1 4 7 9 Output 7 Input 1 5 1 1 4 Output -1 Input 1 5 1 1 5 Output 5 -----Note----- In the first example Pasha can act like this: he solves the second problem in 4 units of time and sends it immediately. Then he spends 3 time units to solve the first problem and sends it 7 time units after the contest starts, because at this moment the website starts working again. In the second example Pasha invents the solution only after the website stops working for the last time. In the third example Pasha sends the solution exactly at the end of the first period. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = int(input()) s = sum(list(map(int, input().split()))) m = int(input()) for i in range(m): l, r = list(map(int, input().split())) s = max(s, l) if l <= s <= r: print(s) return print(-1) ```
vfc_14214
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/813/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\n3 4\n2\n1 4\n7 9\n", "output": "7\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n5\n1\n1 4\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n5\n1\n1 5\n", "output": "5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n100000 100000 100000 100000 100000\n0\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n886 524 128 4068 298\n3\n416 3755\n4496 11945\n17198 18039\n", "output": "5904\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n575 3526 1144 1161 889 1038 790 19 765 357\n2\n4475 10787\n16364 21678\n", "output": "10264\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1116
Solve the following coding problem using the programming language python: You are a rebel leader and you are planning to start a revolution in your country. But the evil Government found out about your plans and set your punishment in the form of correctional labor. You must paint a fence which consists of $10^{100}$ planks in two colors in the following way (suppose planks are numbered from left to right from $0$): if the index of the plank is divisible by $r$ (such planks have indices $0$, $r$, $2r$ and so on) then you must paint it red; if the index of the plank is divisible by $b$ (such planks have indices $0$, $b$, $2b$ and so on) then you must paint it blue; if the index is divisible both by $r$ and $b$ you can choose the color to paint the plank; otherwise, you don't need to paint the plank at all (and it is forbidden to spent paint on it). Furthermore, the Government added one additional restriction to make your punishment worse. Let's list all painted planks of the fence in ascending order: if there are $k$ consecutive planks with the same color in this list, then the Government will state that you failed the labor and execute you immediately. If you don't paint the fence according to the four aforementioned conditions, you will also be executed. The question is: will you be able to accomplish the labor (the time is not important) or the execution is unavoidable and you need to escape at all costs. -----Input----- The first line contains single integer $T$ ($1 \le T \le 1000$) — the number of test cases. The next $T$ lines contain descriptions of test cases — one per line. Each test case contains three integers $r$, $b$, $k$ ($1 \le r, b \le 10^9$, $2 \le k \le 10^9$) — the corresponding coefficients. -----Output----- Print $T$ words — one per line. For each test case print REBEL (case insensitive) if the execution is unavoidable or OBEY (case insensitive) otherwise. -----Example----- Input 4 1 1 2 2 10 4 5 2 3 3 2 2 Output OBEY REBEL OBEY OBEY The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from collections import defaultdict,deque import sys,heapq,bisect,math,itertools,string,queue,copy,time sys.setrecursionlimit(10**8) INF = float('inf') mod = 10**9+7 eps = 10**-7 def inp(): return int(sys.stdin.readline()) def inpl(): return list(map(int, sys.stdin.readline().split())) def inpl_str(): return list(sys.stdin.readline().split()) def gcd(a,b): while b: a,b = b, a%b return a T = inp() for _ in range(T): a,b,k = inpl() a,b = min(a,b), max(a,b) n = gcd(a,b) # while n < b: # n += a # cnt += 1 cnt = -((n-b)//a) if cnt >= k: print("REBEL") else: print("OBEY") ```
vfc_14218
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1260/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n1 1 2\n2 10 4\n5 2 3\n3 2 2\n", "output": "OBEY\nREBEL\nOBEY\nOBEY\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n1 1000000000 999999998\n1 1000000000 999999999\n1 1000000000 1000000000\n1000000000 1000000000 2\n1000000000 1000000000 1000000000\n", "output": "REBEL\nREBEL\nOBEY\nOBEY\nOBEY\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n999999999 1000000000 2\n", "output": "OBEY\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n999999929 999999937 2\n", "output": "REBEL\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n1000000000 999999999 30\n", "output": "OBEY\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1117
Solve the following coding problem using the programming language python: There are $n$ rectangles in a row. You can either turn each rectangle by $90$ degrees or leave it as it is. If you turn a rectangle, its width will be height, and its height will be width. Notice that you can turn any number of rectangles, you also can turn all or none of them. You can not change the order of the rectangles. Find out if there is a way to make the rectangles go in order of non-ascending height. In other words, after all the turns, a height of every rectangle has to be not greater than the height of the previous rectangle (if it is such). -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of rectangles. Each of the next $n$ lines contains two integers $w_i$ and $h_i$ ($1 \leq w_i, h_i \leq 10^9$) — the width and the height of the $i$-th rectangle. -----Output----- Print "YES" (without quotes) if there is a way to make the rectangles go in order of non-ascending height, otherwise print "NO". You can print each letter in any case (upper or lower). -----Examples----- Input 3 3 4 4 6 3 5 Output YES Input 2 3 4 5 5 Output NO -----Note----- In the first test, you can rotate the second and the third rectangles so that the heights will be [4, 4, 3]. In the second test, there is no way the second rectangle will be not higher than the first one. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = int(input()) data = [] for i in range(n): a, b = list(map(int, input().split())) data.append([a, b]) ans = True prev = max(data[0]) for i in range(1, n): a, b = data[i] a, b = min(a, b), max(a,b) if a > prev: ans = False break if a <= prev < b: prev = a continue if prev >= b: prev = b if ans : print("YES") else: print('NO') ```
vfc_14222
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1008/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n3 4\n4 6\n3 5\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n3 4\n5 5\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n4 3\n1 1\n6 5\n4 5\n2 4\n9 5\n7 9\n9 2\n4 10\n10 1\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n241724251 76314740\n80658193 177743680\n213953908 406274173\n485639518 859188055\n103578427 56645210\n611931853 374099541\n916667853 408945969\n677773241 808703176\n575586508 440395988\n450102404 244301685\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n706794178 103578427\n431808055 641644550\n715688799 406274173\n767234853 345348548\n241724251 408945969\n808703176 213953908\n185314264 16672343\n553496707 152702033\n105991807 76314740\n61409204 244301685\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n1 1\n", "output": "YES\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1118
Solve the following coding problem using the programming language python: You are given a line of $n$ colored squares in a row, numbered from $1$ to $n$ from left to right. The $i$-th square initially has the color $c_i$. Let's say, that two squares $i$ and $j$ belong to the same connected component if $c_i = c_j$, and $c_i = c_k$ for all $k$ satisfying $i < k < j$. In other words, all squares on the segment from $i$ to $j$ should have the same color. For example, the line $[3, 3, 3]$ has $1$ connected component, while the line $[5, 2, 4, 4]$ has $3$ connected components. The game "flood fill" is played on the given line as follows: At the start of the game you pick any starting square (this is not counted as a turn). Then, in each game turn, change the color of the connected component containing the starting square to any other color. Find the minimum number of turns needed for the entire line to be changed into a single color. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 5000$) — the number of squares. The second line contains integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 5000$) — the initial colors of the squares. -----Output----- Print a single integer — the minimum number of the turns needed. -----Examples----- Input 4 5 2 2 1 Output 2 Input 8 4 5 2 2 1 3 5 5 Output 4 Input 1 4 Output 0 -----Note----- In the first example, a possible way to achieve an optimal answer is to pick square with index $2$ as the starting square and then play as follows: $[5, 2, 2, 1]$ $[5, 5, 5, 1]$ $[1, 1, 1, 1]$ In the second example, a possible way to achieve an optimal answer is to pick square with index $5$ as the starting square and then perform recoloring into colors $2, 3, 5, 4$ in that order. In the third example, the line already consists of one color only. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n=int(input()) C=[0]+list(map(int,input().split())) #n=5000 #C=list(range(n+1)) A=[] for i in range(1,n+1): if C[i]!=C[i-1]: A.append(C[i]) L=len(A) DP=[[[0]*L for i in range(L)] for j in range(2)] #左の色に揃える or 右の色に揃える,左からi~j番目を def color(r,i,j):#i<j if r==1: if A[i]==A[j]: DP[r][i][j]=min(DP[0][i][j-1],DP[1][i][j-1]+1) else: DP[r][i][j]=min(DP[0][i][j-1]+1,DP[1][i][j-1]+1) else: if A[i]==A[j]: DP[r][i][j]=min(DP[1][i+1][j],DP[0][i+1][j]+1) else: DP[r][i][j]=min(DP[1][i+1][j]+1,DP[0][i+1][j]+1) for i in range(1,L): for j in range(L-i): color(0,j,i+j) color(1,j,i+j) #print(DP) print(min(DP[0][0][L-1],DP[1][0][L-1])) ```
vfc_14226
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1114/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n5 2 2 1\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "8\n4 5 2 2 1 3 5 5\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n4\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n4 4 4 4\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n3166 2658\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "19\n26 26 26 26 26 26 26 26 26 26 26 63 63 68 68 68 81 81 81\n", "output": "3\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1119
Solve the following coding problem using the programming language python: You are given three integers k, p_{a} and p_{b}. You will construct a sequence with the following algorithm: Initially, start with the empty sequence. Each second, you do the following. With probability p_{a} / (p_{a} + p_{b}), add 'a' to the end of the sequence. Otherwise (with probability p_{b} / (p_{a} + p_{b})), add 'b' to the end of the sequence. You stop once there are at least k subsequences that form 'ab'. Determine the expected number of times 'ab' is a subsequence in the resulting sequence. It can be shown that this can be represented by P / Q, where P and Q are coprime integers, and $Q \neq 0 \operatorname{mod}(10^{9} + 7)$. Print the value of $P \cdot Q^{-1} \operatorname{mod}(10^{9} + 7)$. -----Input----- The first line will contain three integers integer k, p_{a}, p_{b} (1 ≤ k ≤ 1 000, 1 ≤ p_{a}, p_{b} ≤ 1 000 000). -----Output----- Print a single integer, the answer to the problem. -----Examples----- Input 1 1 1 Output 2 Input 3 1 4 Output 370000006 -----Note----- The first sample, we will keep appending to our sequence until we get the subsequence 'ab' at least once. For instance, we get the sequence 'ab' with probability 1/4, 'bbab' with probability 1/16, and 'aab' with probability 1/8. Note, it's impossible for us to end with a sequence like 'aabab', since we would have stopped our algorithm once we had the prefix 'aab'. The expected amount of times that 'ab' will occur across all valid sequences is 2. For the second sample, the answer is equal to $\frac{341}{100}$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python k, pa, pb = list(map(int, input().split())) MOD = 10**9 + 7 INF = ((pa + pb) * pow(pb, MOD-2, MOD)) % MOD rAB = pow(pa+pb, MOD-2, MOD) rB = pow(pb, MOD-2, MOD) memo = {} def dfs(a, ab): if ab >= k: return ab if a + ab >= k: #return INF #return (pa + pb) / pb return ((a + MOD-1) + (pa + pb) * rB + ab) % MOD return a - 1 + (pa + pb) / pb + ab if (a, ab) in memo: return memo[a, ab] #res = (((dfs(a+1, ab)+1) * pa * rAB) + ((dfs(a, ab+a)+1) * pb * rAB)) % MOD #res = (dfs(a+1, ab)) * pa / (pa + pb) + (dfs(a, ab+a)) * pb / (pa + pb) res = (dfs(a+1, ab) * pa * rAB) + (dfs(a, ab+a) * pb * rAB) #print(a, ab, res) memo[a, ab] = res = res % MOD return res #print((dfs(1, 0) * pa * rAB + 1) % MOD) #print((pb + dfs(1, 0)*pa) / pa) print(dfs(1, 0)) ```
vfc_14230
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/908/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1 1 1\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 1 4\n", "output": "370000006\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1000 123456 654321\n", "output": "977760856\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "305 337309 378395\n", "output": "174667130\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1120
Solve the following coding problem using the programming language python: Yet another Armageddon is coming! This time the culprit is the Julya tribe calendar. The beavers in this tribe knew math very well. Smart Beaver, an archaeologist, got a sacred plate with a magic integer on it. The translation from Old Beaverish is as follows: "May the Great Beaver bless you! May your chacres open and may your third eye never turn blind from beholding the Truth! Take the magic number, subtract a digit from it (the digit must occur in the number) and get a new magic number. Repeat this operation until a magic number equals zero. The Earth will stand on Three Beavers for the time, equal to the number of subtractions you perform!" Distinct subtraction sequences can obviously get you different number of operations. But the Smart Beaver is ready to face the worst and is asking you to count the minimum number of operations he needs to reduce the magic number to zero. -----Input----- The single line contains the magic integer n, 0 ≤ n. to get 20 points, you need to solve the problem with constraints: n ≤ 10^6 (subproblem C1); to get 40 points, you need to solve the problem with constraints: n ≤ 10^12 (subproblems C1+C2); to get 100 points, you need to solve the problem with constraints: n ≤ 10^18 (subproblems C1+C2+C3). -----Output----- Print a single integer — the minimum number of subtractions that turns the magic number to a zero. -----Examples----- Input 24 Output 5 -----Note----- In the first test sample the minimum number of operations can be reached by the following sequence of subtractions: 24 → 20 → 18 → 10 → 9 → 0 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python start = input() counter = 0 sInt = int(start) while sInt!=0: sInt-=max([int(c) for c in start]) start=str(sInt) counter+=1 print(counter) ```
vfc_14234
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/331/C1", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "24\n", "output": "5", "type": "stdin_stdout" }, { "fn_name": null, "input": "0\n", "output": "0", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n", "output": "1", "type": "stdin_stdout" } ] }
apps
verifiable_code
1121
Solve the following coding problem using the programming language python: You have an n × m rectangle table, its cells are not initially painted. Your task is to paint all cells of the table. The resulting picture should be a tiling of the table with squares. More formally: each cell must be painted some color (the colors are marked by uppercase Latin letters); we will assume that two cells of the table are connected if they are of the same color and share a side; each connected region of the table must form a square. Given n and m, find lexicographically minimum coloring of the table that meets the described properties. -----Input----- The first line contains two integers, n and m (1 ≤ n, m ≤ 100). -----Output----- Print lexicographically minimum coloring of the table that meets the described conditions. One coloring (let's call it X) is considered lexicographically less than the other one (let's call it Y), if: consider all the table cells from left to right and from top to bottom (first, the first cell in the first row, then the second cell in the first row and so on); let's find in this order the first cell that has distinct colors in two colorings; the letter that marks the color of the cell in X, goes alphabetically before the letter that marks the color of the cell in Y. -----Examples----- Input 1 3 Output ABA Input 2 2 Output AA AA Input 3 4 Output AAAB AAAC AAAB The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys input = sys.stdin.readline n,m=list(map(int,input().split())) ANS=[[-1]*m for i in range(n)] for i in range(n): for j in range(m): if ANS[i][j]==-1: for koma in ["A","B","C","D","E","F"]: for k,l in [(i-1,j),(i,j-1),(i+1,j),(i,j+1)]: if 0<=k<n and 0<=l<m and ANS[k][l]==koma: break else: nkoma=koma break MAXlength=1 if nkoma=="A": for length in range(1,101): if i+length>=n or j+length>=m: break for k,l in [(i-1,j+length),(i+length,j-1)]: if 0<=k<n and 0<=l<m and ANS[k][l]==nkoma: break if 0<=i<n and ANS[i][j+length]!=-1: break else: MAXlength=length+1 elif nkoma=="B": for length in range(1,101): if i+length>=n or j+length>=m: break flag=0 if 0<=i-1<n and ANS[i-1][j+length]=="A": flag=1 if 0<=j-1<m and ANS[i+length][j-1]==nkoma: break if 0<=i<n and ANS[i][j+length]!=-1: break if flag==1: MAXlength=length+1 else: break for k in range(i,i+MAXlength): for l in range(j,j+MAXlength): ANS[k][l]=nkoma for a in ANS: print("".join(a)) ```
vfc_14238
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/432/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1 3\n", "output": "ABA\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1127
Solve the following coding problem using the programming language python: Everyone knows that agents in Valorant decide, who will play as attackers, and who will play as defenders. To do that Raze and Breach decided to play $t$ matches of a digit game... In each of $t$ matches of the digit game, a positive integer is generated. It consists of $n$ digits. The digits of this integer are numerated from $1$ to $n$ from the highest-order digit to the lowest-order digit. After this integer is announced, the match starts. Agents play in turns. Raze starts. In one turn an agent can choose any unmarked digit and mark it. Raze can choose digits on odd positions, but can not choose digits on even positions. Breach can choose digits on even positions, but can not choose digits on odd positions. The match ends, when there is only one unmarked digit left. If the single last digit is odd, then Raze wins, else Breach wins. It can be proved, that before the end of the match (for every initial integer with $n$ digits) each agent has an ability to make a turn, i.e. there is at least one unmarked digit, that stands on a position of required parity. For each of $t$ matches find out, which agent wins, if both of them want to win and play optimally. -----Input----- First line of input contains an integer $t$ $(1 \le t \le 100)$  — the number of matches. The first line of each match description contains an integer $n$ $(1 \le n \le 10^3)$  — the number of digits of the generated number. The second line of each match description contains an $n$-digit positive integer without leading zeros. -----Output----- For each match print $1$, if Raze wins, and $2$, if Breach wins. -----Example----- Input 4 1 2 1 3 3 102 4 2069 Output 2 1 1 2 -----Note----- In the first match no one can make a turn, the only digit left is $2$, it's even, so Breach wins. In the second match the only digit left is $3$, it's odd, so Raze wins. In the third match Raze can mark the last digit, after that Breach can only mark $0$. $1$ will be the last digit left, it's odd, so Raze wins. In the fourth match no matter how Raze plays, Breach can mark $9$, and in the end there will be digit $0$. It's even, so Breach wins. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python for _ in range(int(input())): n = int(input()) digits = list(map(int,list(input()))) if n % 2 == 1: containsOdd = False for i in range(0,n,2): if digits[i] % 2 == 1: containsOdd = True if containsOdd: print(1) else: print(2) else: containsEven = False for i in range(1,n,2): if digits[i] % 2 == 0: containsEven = True if containsEven: print(2) else: print(1) ```
vfc_14262
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1419/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n1\n2\n1\n3\n3\n102\n4\n2069\n", "output": "2\n1\n1\n2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n3\n212\n2\n11\n", "output": "2\n1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1128
Solve the following coding problem using the programming language python: One industrial factory is reforming working plan. The director suggested to set a mythical detail production norm. If at the beginning of the day there were x details in the factory storage, then by the end of the day the factory has to produce $x \operatorname{mod} m$ (remainder after dividing x by m) more details. Unfortunately, no customer has ever bought any mythical detail, so all the details produced stay on the factory. The board of directors are worried that the production by the given plan may eventually stop (that means that there will be а moment when the current number of details on the factory is divisible by m). Given the number of details a on the first day and number m check if the production stops at some moment. -----Input----- The first line contains two integers a and m (1 ≤ a, m ≤ 10^5). -----Output----- Print "Yes" (without quotes) if the production will eventually stop, otherwise print "No". -----Examples----- Input 1 5 Output No Input 3 6 Output Yes The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python a,m = map(int,input().split()) for i in range(100000): if a%m==0: print("Yes") quit() else: a+=a%m print("No") ```
vfc_14266
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/485/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1 5\n", "output": "No\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 6\n", "output": "Yes\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 8\n", "output": "Yes\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 3\n", "output": "No\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 24\n", "output": "Yes\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1129
Solve the following coding problem using the programming language python: You are given n points on a line with their coordinates x_{i}. Find the point x so the sum of distances to the given points is minimal. -----Input----- The first line contains integer n (1 ≤ n ≤ 3·10^5) — the number of points on the line. The second line contains n integers x_{i} ( - 10^9 ≤ x_{i} ≤ 10^9) — the coordinates of the given n points. -----Output----- Print the only integer x — the position of the optimal point on the line. If there are several optimal points print the position of the leftmost one. It is guaranteed that the answer is always the integer. -----Example----- Input 4 1 2 3 4 Output 2 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = int(input()) arr = [int(x) for x in input().split()] arr.sort() if(n%2): print(arr[n//2]) else: print(arr[n//2-1]) ```
vfc_14270
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/710/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n1 2 3 4\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n-1 -10 2 6 7\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n-68 10 87 22 30 89 82 -97 -52 25\n", "output": "22\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "100\n457 827 807 17 871 935 907 -415 536 170 551 -988 865 758 -457 -892 -875 -488 684 19 0 555 -807 -624 -239 826 318 811 20 -732 -91 460 551 -610 555 -493 -154 442 -141 946 -913 -104 704 -380 699 32 106 -455 -518 214 -464 -861 243 -798 -472 559 529 -844 -32 871 -459 236 387 626 -318 -580 -611 -842 790 486 64 951 81 78 -693 403 -731 309 678 696 891 846 -106 918 212 -44 994 606 -829 -454 243 -477 -402 -818 -819 -310 -837 -209 736 424\n", "output": "64\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1130
Solve the following coding problem using the programming language python: Ivan is a student at Berland State University (BSU). There are n days in Berland week, and each of these days Ivan might have some classes at the university. There are m working hours during each Berland day, and each lesson at the university lasts exactly one hour. If at some day Ivan's first lesson is during i-th hour, and last lesson is during j-th hour, then he spends j - i + 1 hours in the university during this day. If there are no lessons during some day, then Ivan stays at home and therefore spends 0 hours in the university. Ivan doesn't like to spend a lot of time in the university, so he has decided to skip some lessons. He cannot skip more than k lessons during the week. After deciding which lessons he should skip and which he should attend, every day Ivan will enter the university right before the start of the first lesson he does not skip, and leave it after the end of the last lesson he decides to attend. If Ivan skips all lessons during some day, he doesn't go to the university that day at all. Given n, m, k and Ivan's timetable, can you determine the minimum number of hours he has to spend in the university during one week, if he cannot skip more than k lessons? -----Input----- The first line contains three integers n, m and k (1 ≤ n, m ≤ 500, 0 ≤ k ≤ 500) — the number of days in the Berland week, the number of working hours during each day, and the number of lessons Ivan can skip, respectively. Then n lines follow, i-th line containing a binary string of m characters. If j-th character in i-th line is 1, then Ivan has a lesson on i-th day during j-th hour (if it is 0, there is no such lesson). -----Output----- Print the minimum number of hours Ivan has to spend in the university during the week if he skips not more than k lessons. -----Examples----- Input 2 5 1 01001 10110 Output 5 Input 2 5 0 01001 10110 Output 8 -----Note----- In the first example Ivan can skip any of two lessons during the first day, so he spends 1 hour during the first day and 4 hours during the second day. In the second example Ivan can't skip any lessons, so he spends 4 hours every day. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, m, k = input().split(' ') n = int(n) m = int(m) k = int(k) ind = [] pre = [] for _ in range(n): s = input() ind.append([]) for i, c in enumerate(s): if c == '1': ind[-1].append(i) for i in range(n): pre.append([]) for j in range(k + 1): pre[i].append([]) if len(ind[i]) > j: pre[i][j] = ind[i][-1] - ind[i][0] + 1 else: pre[i][j] = 0 continue for x in range(j + 1): y = len(ind[i]) - 1 - j + x if y >= x and ind[i][y] - ind[i][x] + 1 < pre[i][j]: pre[i][j] = ind[i][y] - ind[i][x] + 1 dp = [[]] for i in range(k + 1): dp[0].append(pre[0][i]) for i in range(1, n): dp.append([]) for j in range(0, k + 1): dp[i].append(pre[i][j] + dp[i - 1][0]) for z in range(j + 1): dp[i][j] = min(dp[i][j], dp[i - 1][z] + pre[i][j - z]) print(dp[n - 1][k]) ```
vfc_14274
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/946/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2 5 1\n01001\n10110\n", "output": "5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 5 0\n01001\n10110\n", "output": "8\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 4 0\n0000\n0000\n0000\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 4 12\n1111\n1111\n1111\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1131
Solve the following coding problem using the programming language python: Arthur and Alexander are number busters. Today they've got a competition. Arthur took a group of four integers a, b, w, x (0 ≤ b < w, 0 < x < w) and Alexander took integer с. Arthur and Alexander use distinct approaches to number bustings. Alexander is just a regular guy. Each second, he subtracts one from his number. In other words, he performs the assignment: c = c - 1. Arthur is a sophisticated guy. Each second Arthur performs a complex operation, described as follows: if b ≥ x, perform the assignment b = b - x, if b < x, then perform two consecutive assignments a = a - 1; b = w - (x - b). You've got numbers a, b, w, x, c. Determine when Alexander gets ahead of Arthur if both guys start performing the operations at the same time. Assume that Alexander got ahead of Arthur if c ≤ a. -----Input----- The first line contains integers a, b, w, x, c (1 ≤ a ≤ 2·10^9, 1 ≤ w ≤ 1000, 0 ≤ b < w, 0 < x < w, 1 ≤ c ≤ 2·10^9). -----Output----- Print a single integer — the minimum time in seconds Alexander needs to get ahead of Arthur. You can prove that the described situation always occurs within the problem's limits. -----Examples----- Input 4 2 3 1 6 Output 2 Input 4 2 3 1 7 Output 4 Input 1 2 3 2 6 Output 13 Input 1 1 2 1 1 Output 0 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python a,b,w,x,c=list(map(int,input().split())) ans=0 bb=b benefit=0 Visited=[False]*1003 CycleCost=-1 while(1): if(c<=a): break if(Visited[b]!=False): CycleCost=ans-Visited[b][1] CycleBenefit=benefit-Visited[b][0] CycleBegining=b break Visited[b]=(benefit,ans) if(b<x): b=w-(x-b) ans+=1 elif(b>=x): b-=x ans+=1 benefit+=1 if(benefit==c-a): break if(CycleCost==-1): print(ans) else: c-=benefit diff=c-a xx=diff//CycleBenefit if(xx!=0): xx-=1 ans+=xx*CycleCost diff-=xx*CycleBenefit b=CycleBegining while(diff>0): if(b<x): b=w-(x-b) ans+=1 else: b-=x ans+=1 diff-=1 print(ans) ```
vfc_14278
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/382/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4 2 3 1 6\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 2 3 1 7\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 2 3 2 6\n", "output": "13\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1 2 1 1\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 0 1000 999 2000000000\n", "output": "1999999999000\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 1 6 4 20\n", "output": "30\n", "type": "stdin_stdout" } ] }