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
1133
Solve the following coding problem using the programming language python: Andrew often reads articles in his favorite magazine 2Char. The main feature of these articles is that each of them uses at most two distinct letters. Andrew decided to send an article to the magazine, but as he hasn't written any article, he just decided to take a random one from magazine 26Char. However, before sending it to the magazine 2Char, he needs to adapt the text to the format of the journal. To do so, he removes some words from the chosen article, in such a way that the remaining text can be written using no more than two distinct letters. Since the payment depends from the number of non-space characters in the article, Andrew wants to keep the words with the maximum total length. -----Input----- The first line of the input contains number n (1 ≤ n ≤ 100) — the number of words in the article chosen by Andrew. Following are n lines, each of them contains one word. All the words consist only of small English letters and their total length doesn't exceed 1000. The words are not guaranteed to be distinct, in this case you are allowed to use a word in the article as many times as it appears in the input. -----Output----- Print a single integer — the maximum possible total length of words in Andrew's article. -----Examples----- Input 4 abb cacc aaa bbb Output 9 Input 5 a a bcbcb cdecdecdecdecdecde aaaa Output 6 -----Note----- In the first sample the optimal way to choose words is {'abb', 'aaa', 'bbb'}. In the second sample the word 'cdecdecdecdecdecde' consists of three distinct letters, and thus cannot be used in the article. The optimal answer is {'a', 'a', 'aaaa'}. 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()) a = [input() for i in range(n)] result = 0 for i in range(26): for j in range(i + 1, 26): t = 0 ci = chr(i + ord('a')) cj = chr(j + ord('a')) for s in a: if s.count(ci) + s.count(cj) == len(s): t += len(s) result = max(result, t) print(result) main() ```
vfc_14286
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/593/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\nabb\ncacc\naaa\nbbb\n", "output": "9", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\na\na\nbcbcb\ncdecdecdecdecdecde\naaaa\n", "output": "6", "type": "stdin_stdout" } ] }
apps
verifiable_code
1134
Solve the following coding problem using the programming language python: Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value. Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to m_{i}. Define d_{i} as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of d_{i} over all days. There are no marks on the channel before the first day. -----Input----- The first line contains a single positive integer n (1 ≤ n ≤ 10^5) — the number of days. The second line contains n space-separated integers m_1, m_2, ..., m_{n} (0 ≤ m_{i} < i) — the number of marks strictly above the water on each day. -----Output----- Output one single integer — the minimum possible sum of the number of marks strictly below the water level among all days. -----Examples----- Input 6 0 1 0 3 0 2 Output 6 Input 5 0 1 2 1 2 Output 1 Input 5 0 1 1 2 2 Output 0 -----Note----- In the first example, the following figure shows an optimal case. [Image] Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6. In the second example, the following figure shows an optimal case. [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()) m = [int(i) for i in input().split()] dm = [0 for i in range(n)] dm[-1] = m[-1] + 1 for i in range(n - 2, -1, -1): dm[i] = max(m[i] + 1, m[i+1], dm[i+1]-1) #print(dm) for i in range(1, n): dm[i] = max(dm[i], dm[i-1]) #print(dm) print(sum([dm[i] - 1 - m[i] for i in range(n)])) ```
vfc_14290
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/924/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6\n0 1 0 3 0 2\n", "output": "6\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n0 1 2 1 2\n", "output": "1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1136
Solve the following coding problem using the programming language python: Calculate the value of the sum: n mod 1 + n mod 2 + n mod 3 + ... + n mod m. As the result can be very large, you should print the value modulo 10^9 + 7 (the remainder when divided by 10^9 + 7). The modulo operator a mod b stands for the remainder after dividing a by b. For example 10 mod 3 = 1. -----Input----- The only line contains two integers n, m (1 ≤ n, m ≤ 10^13) — the parameters of the sum. -----Output----- Print integer s — the value of the required sum modulo 10^9 + 7. -----Examples----- Input 3 4 Output 4 Input 4 4 Output 1 Input 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 import math MOD = int( 1e9 + 7 ) N, M = map( int, input().split() ) sn = int( math.sqrt( N ) ) ans = N * M % MOD for i in range( 1, min( sn, M ) + 1, 1 ): ans -= N // i * i ans %= MOD if N // ( sn + 1 ) > M: exit( print( ans ) ) for f in range( N // ( sn + 1 ), 0, -1 ): s = lambda x: x * ( x + 1 ) // 2 if N // f > M: ans -= f * ( s( M ) - s( N // ( f + 1 ) ) ) break ans -= f * ( s( N // f ) - s( N // ( f + 1 ) ) ) ans %= MOD if ans < 0: ans += MOD print( ans ) ```
vfc_14298
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/616/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 4\n", "output": "4\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1137
Solve the following coding problem using the programming language python: After returned from forest, Alyona started reading a book. She noticed strings s and t, lengths of which are n and m respectively. As usual, reading bored Alyona and she decided to pay her attention to strings s and t, which she considered very similar. Alyona has her favourite positive integer k and because she is too small, k does not exceed 10. The girl wants now to choose k disjoint non-empty substrings of string s such that these strings appear as disjoint substrings of string t and in the same order as they do in string s. She is also interested in that their length is maximum possible among all variants. Formally, Alyona wants to find a sequence of k non-empty strings p_1, p_2, p_3, ..., p_{k} satisfying following conditions: s can be represented as concatenation a_1p_1a_2p_2... a_{k}p_{k}a_{k} + 1, where a_1, a_2, ..., a_{k} + 1 is a sequence of arbitrary strings (some of them may be possibly empty); t can be represented as concatenation b_1p_1b_2p_2... b_{k}p_{k}b_{k} + 1, where b_1, b_2, ..., b_{k} + 1 is a sequence of arbitrary strings (some of them may be possibly empty); sum of the lengths of strings in sequence is maximum possible. Please help Alyona solve this complicated problem and find at least the sum of the lengths of the strings in a desired sequence. A substring of a string is a subsequence of consecutive characters of the string. -----Input----- In the first line of the input three integers n, m, k (1 ≤ n, m ≤ 1000, 1 ≤ k ≤ 10) are given — the length of the string s, the length of the string t and Alyona's favourite number respectively. The second line of the input contains string s, consisting of lowercase English letters. The third line of the input contains string t, consisting of lowercase English letters. -----Output----- In the only line print the only non-negative integer — the sum of the lengths of the strings in a desired sequence. It is guaranteed, that at least one desired sequence exists. -----Examples----- Input 3 2 2 abc ab Output 2 Input 9 12 4 bbaaababb abbbabbaaaba Output 7 -----Note----- The following image describes the answer for the second sample case: [Image] 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 = map(int, input().split()) s, t = input(), input() n += 1 m += 1 p = [i for i in range(n * m - n) if (i + 1) % n] r = p[::-1] d = [0] * n * m for i in p: if s[i % n] == t[i // n]: d[i] = d[i - n - 1] + 1 f = d[:] for y in range(k - 1): for i in p: f[i] = max(f[i], f[i - 1], f[i - n]) for i in r: f[i] = f[i - d[i] * (n + 1)] + d[i] print(max(f)) ```
vfc_14302
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/682/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 2 2\nabc\nab\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "9 12 4\nbbaaababb\nabbbabbaaaba\n", "output": "7\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "11 11 4\naaababbabbb\nbbbaaaabaab\n", "output": "7\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "15 9 4\nababaaabbaaaabb\nbbaababbb\n", "output": "8\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1138
Solve the following coding problem using the programming language python: Memory is performing a walk on the two-dimensional plane, starting at the origin. He is given a string s with his directions for motion: An 'L' indicates he should move one unit left. An 'R' indicates he should move one unit right. A 'U' indicates he should move one unit up. A 'D' indicates he should move one unit down. But now Memory wants to end at the origin. To do this, he has a special trident. This trident can replace any character in s with any of 'L', 'R', 'U', or 'D'. However, because he doesn't want to wear out the trident, he wants to make the minimum number of edits possible. Please tell Memory what is the minimum number of changes he needs to make to produce a string that, when walked, will end at the origin, or if there is no such string. -----Input----- The first and only line contains the string s (1 ≤ |s| ≤ 100 000) — the instructions Memory is given. -----Output----- If there is a string satisfying the conditions, output a single integer — the minimum number of edits required. In case it's not possible to change the sequence in such a way that it will bring Memory to to the origin, output -1. -----Examples----- Input RRU Output -1 Input UDUR Output 1 Input RUUR Output 2 -----Note----- In the first sample test, Memory is told to walk right, then right, then up. It is easy to see that it is impossible to edit these instructions to form a valid walk. In the second sample test, Memory is told to walk up, then down, then up, then right. One possible solution is to change s to "LDUR". This string uses 1 edit, which is the minimum possible. It also ends at the origin. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python s = input() li = s.count("L") ri = s.count("R") ui = s.count("U") di = s.count("D") n = len(s) if n % 2 != 0: print(-1) else: print((abs(li-ri)+abs(di-ui))//2) ```
vfc_14306
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/712/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "RRU\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "UDUR\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "RUUR\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "DDDD\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "RRRR\n", "output": "2\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1139
Solve the following coding problem using the programming language python: Omkar is building a house. He wants to decide how to make the floor plan for the last floor. Omkar's floor starts out as $n$ rows of $m$ zeros ($1 \le n,m \le 100$). Every row is divided into intervals such that every $0$ in the row is in exactly $1$ interval. For every interval for every row, Omkar can change exactly one of the $0$s contained in that interval to a $1$. Omkar defines the quality of a floor as the sum of the squares of the sums of the values in each column, i. e. if the sum of the values in the $i$-th column is $q_i$, then the quality of the floor is $\sum_{i = 1}^m q_i^2$. Help Omkar find the maximum quality that the floor can have. -----Input----- The first line contains two integers, $n$ and $m$ ($1 \le n,m \le 100$), which are the number of rows and number of columns, respectively. You will then receive a description of the intervals in each row. For every row $i$ from $1$ to $n$: The first row contains a single integer $k_i$ ($1 \le k_i \le m$), which is the number of intervals on row $i$. The $j$-th of the next $k_i$ lines contains two integers $l_{i,j}$ and $r_{i,j}$, which are the left and right bound (both inclusive), respectively, of the $j$-th interval of the $i$-th row. It is guaranteed that all intervals other than the first interval will be directly after the interval before it. Formally, $l_{i,1} = 1$, $l_{i,j} \leq r_{i,j}$ for all $1 \le j \le k_i$, $r_{i,j-1} + 1 = l_{i,j}$ for all $2 \le j \le k_i$, and $r_{i,k_i} = m$. -----Output----- Output one integer, which is the maximum possible quality of an eligible floor plan. -----Example----- Input 4 5 2 1 2 3 5 2 1 3 4 5 3 1 1 2 4 5 5 3 1 1 2 2 3 5 Output 36 -----Note----- The given test case corresponds to the following diagram. Cells in the same row and have the same number are a part of the same interval. [Image] The most optimal assignment is: [Image] The sum of the $1$st column is $4$, the sum of the $2$nd column is $2$, the sum of the $3$rd and $4$th columns are $0$, and the sum of the $5$th column is $4$. The quality of this floor plan is $4^2 + 2^2 + 0^2 + 0^2 + 4^2 = 36$. You can show that there is no floor plan with a higher quality. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from sys import stdin, stdout def __starting_point(): def omkar_and_last_floor(a, n, m): dp = [[0 for c in range(m)] for r in range(m)] #print(dp) for r in range(m): for l in range(r,-1,-1): for k in range(l, r+1): cnt = 0 for i in range(n): if l <= a[i][k][0] and a[i][k][1] <= r: cnt += 1 lr = cnt*cnt if k-1 >= l: lr += dp[l][k-1] if k+1 <= r: lr += dp[k + 1][r] dp[l][r] = max(dp[l][r], lr) #print(dp) return dp[0][m-1] n, m = list(map(int, stdin.readline().split())) a = [[[0,0] for c in range(m)] for r in range(n)] for i in range(n): k = int(stdin.readline()) for j in range(k): l, r = list(map(int, stdin.readline().split())) for x in range(l, r+1): a[i][x-1][0] = l-1 a[i][x-1][1] = r-1 print(omkar_and_last_floor(a, n, m)) __starting_point() ```
vfc_14310
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1372/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4 5\n2\n1 2\n3 5\n2\n1 3\n4 5\n3\n1 1\n2 4\n5 5\n3\n1 1\n2 2\n3 5\n", "output": "36\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 3\n3\n1 1\n2 2\n3 3\n3\n1 1\n2 2\n3 3\n3\n1 1\n2 2\n3 3\n", "output": "27\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 5\n1\n1 5\n1\n1 5\n1\n1 5\n1\n1 5\n1\n1 5\n", "output": "25\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 5\n1\n1 5\n2\n1 4\n5 5\n2\n1 3\n4 5\n3\n1 1\n2 3\n4 5\n2\n1 2\n3 5\n", "output": "42\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 1\n1\n1 1\n1\n1 1\n", "output": "4\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1140
Solve the following coding problem using the programming language python: Pashmak decided to give Parmida a pair of flowers from the garden. There are n flowers in the garden and the i-th of them has a beauty number b_{i}. Parmida is a very strange girl so she doesn't want to have the two most beautiful flowers necessarily. She wants to have those pairs of flowers that their beauty difference is maximal possible! Your task is to write a program which calculates two things: The maximum beauty difference of flowers that Pashmak can give to Parmida. The number of ways that Pashmak can pick the flowers. Two ways are considered different if and only if there is at least one flower that is chosen in the first way and not chosen in the second way. -----Input----- The first line of the input contains n (2 ≤ n ≤ 2·10^5). In the next line there are n space-separated integers b_1, b_2, ..., b_{n} (1 ≤ b_{i} ≤ 10^9). -----Output----- The only line of output should contain two integers. The maximum beauty difference and the number of ways this may happen, respectively. -----Examples----- Input 2 1 2 Output 1 1 Input 3 1 4 5 Output 4 1 Input 5 3 1 2 3 1 Output 2 4 -----Note----- In the third sample the maximum beauty difference is 2 and there are 4 ways to do this: choosing the first and the second flowers; choosing the first and the fifth flowers; choosing the fourth and the second flowers; choosing the fourth and the fifth flowers. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python """ Codeforces Contest 261 Div 2 Problem B Author : chaotic_iak Language: Python 3.3.4 """ def main(): n, = read() a = read() mn = 10**9+1 mncount = 0 mx = 0 mxcount = 0 for i in range(n): if a[i] < mn: mn = a[i] mncount = 1 elif a[i] == mn: mncount += 1 if a[i] > mx: mx = a[i] mxcount = 1 elif a[i] == mx: mxcount += 1 if mx != mn: print(mx-mn, mncount*mxcount) else: print(0, n*(n-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_14314
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/459/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\n1 2\n", "output": "1 1", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n1 4 5\n", "output": "4 1", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n3 1 2 3 1\n", "output": "2 4", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n1 1\n", "output": "0 1", "type": "stdin_stdout" } ] }
apps
verifiable_code
1141
Solve the following coding problem using the programming language python: Are you going to Scarborough Fair? Parsley, sage, rosemary and thyme. Remember me to one who lives there. He once was the true love of mine. Willem is taking the girl to the highest building in island No.28, however, neither of them knows how to get there. Willem asks his friend, Grick for directions, Grick helped them, and gave them a task. Although the girl wants to help, Willem insists on doing it by himself. Grick gave Willem a string of length n. Willem needs to do m operations, each operation has four parameters l, r, c_1, c_2, which means that all symbols c_1 in range [l, r] (from l-th to r-th, including l and r) are changed into c_2. String is 1-indexed. Grick wants to know the final string after all the m operations. -----Input----- The first line contains two integers n and m (1 ≤ n, m ≤ 100). The second line contains a string s of length n, consisting of lowercase English letters. Each of the next m lines contains four parameters l, r, c_1, c_2 (1 ≤ l ≤ r ≤ n, c_1, c_2 are lowercase English letters), separated by space. -----Output----- Output string s after performing m operations described above. -----Examples----- Input 3 1 ioi 1 1 i n Output noi Input 5 3 wxhak 3 3 h x 1 5 x a 1 3 w g Output gaaak -----Note----- For the second example: After the first operation, the string is wxxak. After the second operation, the string is waaak. After the third operation, the string is gaaak. 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())) s = list(input()) a = [] for _ in range(m): l, r, c1, c2 = input().split() l, r = int(l) - 1, int(r) - 1 for i in range(l, r + 1): if s[i] == c1: s[i] = c2 print(''.join(s)) ```
vfc_14318
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/897/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 1\nioi\n1 1 i n\n", "output": "noi", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 3\nwxhak\n3 3 h x\n1 5 x a\n1 3 w g\n", "output": "gaaak", "type": "stdin_stdout" }, { "fn_name": null, "input": "9 51\nbhfbdcgff\n2 3 b b\n2 8 e f\n3 8 g f\n5 7 d a\n1 5 e b\n3 4 g b\n6 7 c d\n3 6 e g\n3 6 e h\n5 6 a e\n7 9 a c\n4 9 a h\n3 7 c b\n6 9 b g\n1 7 h b\n4 5 a e\n3 9 f a\n1 2 c h\n4 8 a c\n3 5 e d\n3 4 g f\n2 3 d h\n2 3 d e\n1 7 d g\n2 6 e g\n2 3 d g\n5 5 h h\n2 8 g d\n8 9 a f\n5 9 c e\n1 7 f d\n1 6 e e\n5 7 c a\n8 9 b b\n2 6 e b\n6 6 g h\n1 2 b b\n1 5 a f\n5 8 f h\n1 5 e g\n3 9 f h\n6 8 g a\n4 6 h g\n1 5 f a\n5 6 a c\n4 8 e d\n1 4 d g\n7 8 b f\n5 6 h b\n3 9 c e\n1 9 b a\n", "output": "aahaddddh", "type": "stdin_stdout" } ] }
apps
verifiable_code
1142
Solve the following coding problem using the programming language python: Recently you've discovered a new shooter. They say it has realistic game mechanics. Your character has a gun with magazine size equal to $k$ and should exterminate $n$ waves of monsters. The $i$-th wave consists of $a_i$ monsters and happens from the $l_i$-th moment of time up to the $r_i$-th moments of time. All $a_i$ monsters spawn at moment $l_i$ and you have to exterminate all of them before the moment $r_i$ ends (you can kill monsters right at moment $r_i$). For every two consecutive waves, the second wave starts not earlier than the first wave ends (though the second wave can start at the same moment when the first wave ends) — formally, the condition $r_i \le l_{i + 1}$ holds. Take a look at the notes for the examples to understand the process better. You are confident in yours and your character's skills so you can assume that aiming and shooting are instant and you need exactly one bullet to kill one monster. But reloading takes exactly $1$ unit of time. One of the realistic mechanics is a mechanic of reloading: when you reload you throw away the old magazine with all remaining bullets in it. That's why constant reloads may cost you excessive amounts of spent bullets. You've taken a liking to this mechanic so now you are wondering: what is the minimum possible number of bullets you need to spend (both used and thrown) to exterminate all waves. Note that you don't throw the remaining bullets away after eradicating all monsters, and you start with a full magazine. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2000$; $1 \le k \le 10^9$) — the number of waves and magazine size. The next $n$ lines contain descriptions of waves. The $i$-th line contains three integers $l_i$, $r_i$ and $a_i$ ($1 \le l_i \le r_i \le 10^9$; $1 \le a_i \le 10^9$) — the period of time when the $i$-th wave happens and the number of monsters in it. It's guaranteed that waves don't overlap (but may touch) and are given in the order they occur, i. e. $r_i \le l_{i + 1}$. -----Output----- If there is no way to clear all waves, print $-1$. Otherwise, print the minimum possible number of bullets you need to spend (both used and thrown) to clear all waves. -----Examples----- Input 2 3 2 3 6 3 4 3 Output 9 Input 2 5 3 7 11 10 12 15 Output 30 Input 5 42 42 42 42 42 43 42 43 44 42 44 45 42 45 45 1 Output -1 Input 1 10 100 111 1 Output 1 -----Note----- In the first example: At the moment $2$, the first wave occurs and $6$ monsters spawn. You kill $3$ monsters and start reloading. At the moment $3$, the second wave occurs and $3$ more monsters spawn. You kill remaining $3$ monsters from the first wave and start reloading. At the moment $4$, you kill remaining $3$ monsters from the second wave. In total, you'll spend $9$ bullets. In the second example: At moment $3$, the first wave occurs and $11$ monsters spawn. You kill $5$ monsters and start reloading. At moment $4$, you kill $5$ more monsters and start reloading. At moment $5$, you kill the last monster and start reloading throwing away old magazine with $4$ bullets. At moment $10$, the second wave occurs and $15$ monsters spawn. You kill $5$ monsters and start reloading. At moment $11$, you kill $5$ more monsters and start reloading. At moment $12$, you kill last $5$ monsters. In total, you'll spend $30$ bullets. 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 = list(map(int, sys.stdin.readline().strip().split())) L = [] R = [] A = [] for i in range (0, n): x = list(map(int, sys.stdin.readline().strip().split())) L.append(x[0]) R.append(x[1]) A.append(x[2]) L.append(R[-1]) i = n-1 x = 0 y = 0 ans = 0 v = True N = [0 for i in range (0, n)] while i >= 0: if R[i] == L[i+1]: x = max(x + A[i] - k * (R[i] - L[i]), 0) N[i] = x else: x = max(A[i] - k * (R[i] - L[i]), 0) N[i] = x if N[i] > k: v = False i = i - 1 m = k N.append(0) i = 0 while i < n and v == True: if m < N[i]: ans = ans + m m = k m = m - A[i] ans = ans + A[i] while m < 0: m = m + k i = i + 1 if v == True: print(ans) else: print(-1) ```
vfc_14322
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1430/F", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2 3\n2 3 6\n3 4 3\n", "output": "9\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 5\n3 7 11\n10 12 15\n", "output": "30\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 42\n42 42 42\n42 43 42\n43 44 42\n44 45 42\n45 45 1\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 10\n100 111 1\n", "output": "1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1143
Solve the following coding problem using the programming language python: In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by p_{i} people for t_{i} days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it. For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd. In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. -----Input----- The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers m_{i}, d_{i}, p_{i} and t_{i} — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ m_{i} ≤ 12, d_{i} ≥ 1, 1 ≤ p_{i}, t_{i} ≤ 100), d_{i} doesn't exceed the number of days in month m_{i}. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. -----Output----- Print a single number — the minimum jury size. -----Examples----- Input 2 5 23 1 2 3 13 2 3 Output 2 Input 3 12 9 2 1 12 8 1 3 12 8 2 2 Output 3 Input 1 1 10 1 13 Output 1 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python w, r = open('output.txt', 'w'), open('input.txt', 'r') s, y = [0] * 466, [0, 100, 131, 159, 190, 220, 251, 281, 312, 343, 373, 404, 434] for i in range(int(r.readline())): m, d, p, t = map(int, r.readline().split()) x = y[m] + d s[x] -= p s[x - t] += p for i in range(465): s[i + 1] += s[i] w.write(str(max(s))) ```
vfc_14326
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/254/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\n5 23 1 2\n3 13 2 3\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n12 9 2 1\n12 8 1 3\n12 8 2 2\n", "output": "3\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1144
Solve the following coding problem using the programming language python: Vasya wrote down two strings s of length n and t of length m consisting of small English letters 'a' and 'b'. What is more, he knows that string t has a form "abab...", namely there are letters 'a' on odd positions and letters 'b' on even positions. Suddenly in the morning, Vasya found that somebody spoiled his string. Some letters of the string s were replaced by character '?'. Let's call a sequence of positions i, i + 1, ..., i + m - 1 as occurrence of string t in s, if 1 ≤ i ≤ n - m + 1 and t_1 = s_{i}, t_2 = s_{i} + 1, ..., t_{m} = s_{i} + m - 1. The boy defines the beauty of the string s as maximum number of disjoint occurrences of string t in s. Vasya can replace some letters '?' with 'a' or 'b' (letters on different positions can be replaced with different letter). Vasya wants to make some replacements in such a way that beauty of string s is maximum possible. From all such options, he wants to choose one with the minimum number of replacements. Find the number of replacements he should make. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^5) — the length of s. The second line contains the string s of length n. It contains small English letters 'a', 'b' and characters '?' only. The third line contains a single integer m (1 ≤ m ≤ 10^5) — the length of t. The string t contains letters 'a' on odd positions and 'b' on even positions. -----Output----- Print the only integer — the minimum number of replacements Vasya has to perform to make the beauty of string s the maximum possible. -----Examples----- Input 5 bb?a? 1 Output 2 Input 9 ab??ab??? 3 Output 2 -----Note----- In the first sample string t has a form 'a'. The only optimal option is to replace all characters '?' by 'a'. In the second sample using two replacements we can make string equal to "aba?aba??". It is impossible to get more than two occurrences. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python match = 0 nonmatch = 0 count = 0 def calc_match(s, t, p): nonlocal match nonlocal nonmatch nonlocal count if p == len(s)-len(t): return if p+len(t) < len(s): if s[p+len(t)] == '?': count -= 1 elif s[p+len(t)] == t[-1]: match -= 1 else: nonmatch -= 1 match, nonmatch = nonmatch, match if p+len(t) < len(s): if s[p] == '?': count += 1 elif s[p] == 'a': match += 1 else: nonmatch += 1 def init_match(s, t): nonlocal match nonlocal nonmatch nonlocal count p = len(s)-len(t) for i in range(len(t)): if s[p+i] == '?': count += 1 elif s[p+i] == t[i]: match += 1 else: nonmatch += 1 n = int(input()) s = input() m = int(input()) t = "" for i in range(m): if i%2==0: t = t + 'a' else: t = t + 'b' init_match(s,t) dp = [] for i in range(n+3): dp.append((0, 0)) p = n-m while p >= 0: calc_match(s, t, p) if nonmatch == 0: if dp[p+1][0] == dp[p+m][0]+1: dp[p] = (dp[p+1][0], min(dp[p+1][1], dp[p+m][1]+count)) elif dp[p+1][0] > dp[p+m][0]+1: dp[p] = dp[p+1] else: dp[p] = (dp[p+m][0]+1, dp[p+m][1]+count) else: dp[p] = dp[p+1] p -= 1 print(dp[0][1]) ```
vfc_14330
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/900/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\nbb?a?\n1\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "9\nab??ab???\n3\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\nab??ab\n4\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "14\n?abaa?abb?b?a?\n3\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "17\nb??a?abbbaaababba\n4\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\nb\n1\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1145
Solve the following coding problem using the programming language python: Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge has a coolness factor, which shows how much it's owner reached. Coolness factor can be increased by one for the cost of one coin. For every pair of soldiers one of them should get a badge with strictly higher factor than the second one. Exact values of their factors aren't important, they just need to have distinct factors. Colonel knows, which soldier is supposed to get which badge initially, but there is a problem. Some of badges may have the same factor of coolness. Help him and calculate how much money has to be paid for making all badges have different factors of coolness. -----Input----- First line of input consists of one integer n (1 ≤ n ≤ 3000). Next line consists of n integers a_{i} (1 ≤ a_{i} ≤ n), which stand for coolness factor of each badge. -----Output----- Output single integer — minimum amount of coins the colonel has to pay. -----Examples----- Input 4 1 3 1 4 Output 1 Input 5 1 2 3 2 5 Output 2 -----Note----- In first sample test we can increase factor of first badge by 1. In second sample test we can increase factors of the second and the third badge by 1. 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())) A.sort() prev = -1 res = 0 for x in A: if x > prev: prev = x else: prev = prev + 1 res += prev - x print(res) ```
vfc_14334
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/546/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n1 3 1 4\n", "output": "1", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n1 2 3 2 5\n", "output": "2", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n1 5 3 2 4\n", "output": "0", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n1 1 2 3 4 5 6 7 8 9\n", "output": "9", "type": "stdin_stdout" }, { "fn_name": null, "input": "11\n9 2 10 3 1 5 7 1 4 8 6\n", "output": "10", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n4 3 2 2\n", "output": "3", "type": "stdin_stdout" } ] }
apps
verifiable_code
1146
Solve the following coding problem using the programming language python: Vasya wants to turn on Christmas lights consisting of m bulbs. Initially, all bulbs are turned off. There are n buttons, each of them is connected to some set of bulbs. Vasya can press any of these buttons. When the button is pressed, it turns on all the bulbs it's connected to. Can Vasya light up all the bulbs? If Vasya presses the button such that some bulbs connected to it are already turned on, they do not change their state, i.e. remain turned on. -----Input----- The first line of the input contains integers n and m (1 ≤ n, m ≤ 100) — the number of buttons and the number of bulbs respectively. Each of the next n lines contains x_{i} (0 ≤ x_{i} ≤ m) — the number of bulbs that are turned on by the i-th button, and then x_{i} numbers y_{ij} (1 ≤ y_{ij} ≤ m) — the numbers of these bulbs. -----Output----- If it's possible to turn on all m bulbs print "YES", otherwise print "NO". -----Examples----- Input 3 4 2 1 4 3 1 3 1 1 2 Output YES Input 3 3 1 1 1 2 1 1 Output NO -----Note----- In the first sample you can press each button once and turn on all the bulbs. In the 2 sample it is impossible to turn on the 3-rd lamp. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, m = [int(i) for i in input().split()] d = set() for i in range(n): _, *a = [int(i) for i in input().split()] d |= set(a) if len(d) == m: print("YES") else: print("NO") ```
vfc_14338
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/615/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 4\n2 1 4\n3 1 3 1\n1 2\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 3\n1 1\n1 2\n1 1\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 4\n1 1\n1 2\n1 3\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 5\n5 1 2 3 4 5\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 5\n5 4 4 1 2 3\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 5\n5 1 1 1 1 5\n", "output": "NO\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1147
Solve the following coding problem using the programming language python: While Vasya finished eating his piece of pizza, the lesson has already started. For being late for the lesson, the teacher suggested Vasya to solve one interesting problem. Vasya has an array a and integer x. He should find the number of different ordered pairs of indexes (i, j) such that a_{i} ≤ a_{j} and there are exactly k integers y such that a_{i} ≤ y ≤ a_{j} and y is divisible by x. In this problem it is meant that pair (i, j) is equal to (j, i) only if i is equal to j. For example pair (1, 2) is not the same as (2, 1). -----Input----- The first line contains 3 integers n, x, k (1 ≤ n ≤ 10^5, 1 ≤ x ≤ 10^9, 0 ≤ k ≤ 10^9), where n is the size of the array a and x and k are numbers from the statement. The second line contains n integers a_{i} (1 ≤ a_{i} ≤ 10^9) — the elements of the array a. -----Output----- Print one integer — the answer to the problem. -----Examples----- Input 4 2 1 1 3 5 7 Output 3 Input 4 2 0 5 3 1 7 Output 4 Input 5 3 1 3 3 3 3 3 Output 25 -----Note----- In first sample there are only three suitable pairs of indexes — (1, 2), (2, 3), (3, 4). In second sample there are four suitable pairs of indexes(1, 1), (2, 2), (3, 3), (4, 4). In third sample every pair (i, j) is suitable, so the answer is 5 * 5 = 25. 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 bisect n, x, k = list(map(int, input().split())) a = sorted(list(map(int, input().split()))) ans = 0 for num in a: l = math.ceil(num/x)*x + (k-1)*x r = l + x - 1 l = num if l < num else l # print(l, r, bisect.bisect_left(a, l), bisect.bisect_right(a, r), bisect.bisect_right(a, r) - bisect.bisect_left(a, l)) ans += bisect.bisect_right(a, r) - bisect.bisect_left(a, l) print(ans) ''' 7 3 2 1 3 5 9 11 16 25 ''' ''' 4 2 0 5 3 1 7 ''' ```
vfc_14342
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/895/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4 2 1\n1 3 5 7\n", "output": "3\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1148
Solve the following coding problem using the programming language python: Vika has n jars with paints of distinct colors. All the jars are numbered from 1 to n and the i-th jar contains a_{i} liters of paint of color i. Vika also has an infinitely long rectangular piece of paper of width 1, consisting of squares of size 1 × 1. Squares are numbered 1, 2, 3 and so on. Vika decided that she will start painting squares one by one from left to right, starting from the square number 1 and some arbitrary color. If the square was painted in color x, then the next square will be painted in color x + 1. In case of x = n, next square is painted in color 1. If there is no more paint of the color Vika wants to use now, then she stops. Square is always painted in only one color, and it takes exactly 1 liter of paint. Your task is to calculate the maximum number of squares that might be painted, if Vika chooses right color to paint the first square. -----Input----- The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of jars with colors Vika has. The second line of the input contains a sequence of integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9), where a_{i} is equal to the number of liters of paint in the i-th jar, i.e. the number of liters of color i that Vika has. -----Output----- The only line of the output should contain a single integer — the maximum number of squares that Vika can paint if she follows the rules described above. -----Examples----- Input 5 2 4 2 3 3 Output 12 Input 3 5 5 5 Output 15 Input 6 10 10 10 1 10 10 Output 11 -----Note----- In the first sample the best strategy is to start painting using color 4. Then the squares will be painted in the following colors (from left to right): 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5. In the second sample Vika can start to paint using any color. In the third sample Vika should start painting using color number 5. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python am=0 x = int(input()) l = list(map(int, input().split(' '))) m = min(l) k = [i for i in range(x) if l[i] == m] k.append(k[0]+x) for i in range(len(k)-1): am = max(am, k[i+1]-k[i]) print(m * x + am - 1) ```
vfc_14346
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/610/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n2 4 2 3 3\n", "output": "12\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n5 5 5\n", "output": "15\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n10 10 10 1 10 10\n", "output": "11\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n167959139\n", "output": "167959139\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n896619242 805194919 844752453 848347723 816995848 856813612 805194919 833406689 816255448 805194919\n", "output": "8051949194\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1149
Solve the following coding problem using the programming language python: There is a game called "I Wanna Be the Guy", consisting of n levels. Little X and his friend Little Y are addicted to the game. Each of them wants to pass the whole game. Little X can pass only p levels of the game. And Little Y can pass only q levels of the game. You are given the indices of levels Little X can pass and the indices of levels Little Y can pass. Will Little X and Little Y pass the whole game, if they cooperate each other? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 100). The next line contains an integer p (0 ≤ p ≤ n) at first, then follows p distinct integers a_1, a_2, ..., a_{p} (1 ≤ a_{i} ≤ n). These integers denote the indices of levels Little X can pass. The next line contains the levels Little Y can pass in the same format. It's assumed that levels are numbered from 1 to n. -----Output----- If they can pass all the levels, print "I become the guy.". If it's impossible, print "Oh, my keyboard!" (without the quotes). -----Examples----- Input 4 3 1 2 3 2 2 4 Output I become the guy. Input 4 3 1 2 3 2 2 3 Output Oh, my keyboard! -----Note----- In the first sample, Little X can pass levels [1 2 3], and Little Y can pass level [2 4], so they can pass all the levels both. In the second sample, no one can pass level 4. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import fractions count = 0 a = int(input()) #n listx = list(map(int, input().split(' '))) listy = list(map(int, input().split(' '))) listx.remove(listx[0]) listy.remove(listy[0]) listx = set(listx) listy = set(listy) listz = listx.union(listy) listz=list(listz) listw = [i+1 for i in range(a)] if listz == listw: print("I become the guy.") else: print("Oh, my keyboard!") ```
vfc_14350
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/469/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n3 1 2 3\n2 2 4\n", "output": "I become the guy.\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n3 1 2 3\n2 2 3\n", "output": "Oh, my keyboard!\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n5 8 6 1 5 4\n6 1 3 2 9 4 6\n", "output": "Oh, my keyboard!\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n8 8 10 7 3 1 4 2 6\n8 9 5 10 3 7 2 4 8\n", "output": "I become the guy.\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n9 6 1 8 3 9 7 5 10 4\n7 1 3 2 7 6 9 5\n", "output": "I become the guy.\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1150
Solve the following coding problem using the programming language python: Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has n regiments, each consisting of 4 moles. Initially, each mole i (1 ≤ i ≤ 4n) is placed at some position (x_{i}, y_{i}) in the Cartesian plane. Captain Marmot wants to move some moles to make the regiments compact, if it's possible. Each mole i has a home placed at the position (a_{i}, b_{i}). Moving this mole one time means rotating his position point (x_{i}, y_{i}) 90 degrees counter-clockwise around it's home point (a_{i}, b_{i}). A regiment is compact only if the position points of the 4 moles form a square with non-zero area. Help Captain Marmot to find out for each regiment the minimal number of moves required to make that regiment compact, if it's possible. -----Input----- The first line contains one integer n (1 ≤ n ≤ 100), the number of regiments. The next 4n lines contain 4 integers x_{i}, y_{i}, a_{i}, b_{i} ( - 10^4 ≤ x_{i}, y_{i}, a_{i}, b_{i} ≤ 10^4). -----Output----- Print n lines to the standard output. If the regiment i can be made compact, the i-th line should contain one integer, the minimal number of required moves. Otherwise, on the i-th line print "-1" (without quotes). -----Examples----- Input 4 1 1 0 0 -1 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -2 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -1 1 0 0 -1 1 0 0 -1 1 0 0 2 2 0 1 -1 0 0 -2 3 0 0 -2 -1 1 -2 0 Output 1 -1 3 3 -----Note----- In the first regiment we can move once the second or the third mole. We can't make the second regiment compact. In the third regiment, from the last 3 moles we can move once one and twice another one. In the fourth regiment, we can move twice the first mole and once the third mole. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def f(x, y, a, b, n): return a + (x - a) * cos[n] - (y - b) * sin[n], b + (x - a) * sin[n] + (y - b) * cos[n] def check(p): d = {} for i in range(len(p) - 1): for j in range(i + 1, len(p)): dist = (p[i][0] - p[j][0]) ** 2 + (p[i][1] - p[j][1]) ** 2 d[dist] = d.get(dist, 0) + 1 if len(d) != 2: return 0 a, b = sorted(d) return 2 * a == b and d[a] == 4 and d[b] == 2 cos, sin, variants = [1, 0, -1, 0], [0, 1, 0, -1], [[x, y, z, a] for x in range(4) for y in range(4) for z in range(4) for a in range(4)] for t in range(int(input())): moles, ans = [list(map(int, input().split())) for x in range(4)], 13 for a in variants: if check([f(moles[i][0], moles[i][1], moles[i][2], moles[i][3], a[i]) for i in range(4)]): ans = min(ans, sum(a)) print(ans if ans != 13 else -1) ```
vfc_14354
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/474/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n1 1 0 0\n-1 1 0 0\n-1 1 0 0\n1 -1 0 0\n1 1 0 0\n-2 1 0 0\n-1 1 0 0\n1 -1 0 0\n1 1 0 0\n-1 1 0 0\n-1 1 0 0\n-1 1 0 0\n2 2 0 1\n-1 0 0 -2\n3 0 0 -2\n-1 1 -2 0\n", "output": "1\n-1\n3\n3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n-2248 6528 -2144 6181\n-2245 6663 -2100 7054\n-4378 7068 -4061 7516\n-4274 6026 -3918 5721\n4942 -6793 5014 -6807\n3463 -5170 3112 -5181\n2870 -6992 3038 -6567\n5688 -4318 5358 -4744\n5249 7233 5016 6863\n4312 7385 4162 7383\n5965 9138 5607 8728\n4053 8349 4124 8389\n", "output": "8\n6\n6\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n1 1 0 0\n-1 1 0 0\n-1 1 0 0\n1 -1 0 0\n1 1 0 0\n-2 1 0 0\n-1 1 0 0\n1 -1 0 0\n1 1 0 0\n-1 1 0 0\n-1 1 0 0\n-1 1 0 0\n2 2 0 1\n-1 0 0 -2\n3 0 0 -2\n-1 1 -2 0\n0 1 0 0\n1 0 0 0\n-1 0 0 0\n0 -1 0 0\n", "output": "1\n-1\n3\n3\n0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n0 3 0 3\n3 2 3 2\n-1 0 -1 0\n2 -1 2 -1\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n1 0 0 0\n0 2 0 0\n-1 0 0 0\n0 -2 0 0\n1 0 0 0\n0 1 0 0\n-1 0 0 0\n0 -1 0 0\n1 2 0 0\n-1 2 0 0\n-1 -2 0 0\n1 -2 0 0\n", "output": "-1\n0\n-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n1 0 2 0\n-1 0 -2 0\n0 2 0 3\n0 -2 0 -3\n", "output": "4\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1151
Solve the following coding problem using the programming language python: An atom of element X can exist in n distinct states with energies E_1 < E_2 < ... < E_{n}. Arkady wants to build a laser on this element, using a three-level scheme. Here is a simplified description of the scheme. Three distinct states i, j and k are selected, where i < j < k. After that the following process happens: initially the atom is in the state i, we spend E_{k} - E_{i} energy to put the atom in the state k, the atom emits a photon with useful energy E_{k} - E_{j} and changes its state to the state j, the atom spontaneously changes its state to the state i, losing energy E_{j} - E_{i}, the process repeats from step 1. Let's define the energy conversion efficiency as $\eta = \frac{E_{k} - E_{j}}{E_{k} - E_{i}}$, i. e. the ration between the useful energy of the photon and spent energy. Due to some limitations, Arkady can only choose such three states that E_{k} - E_{i} ≤ U. Help Arkady to find such the maximum possible energy conversion efficiency within the above constraints. -----Input----- The first line contains two integers n and U (3 ≤ n ≤ 10^5, 1 ≤ U ≤ 10^9) — the number of states and the maximum possible difference between E_{k} and E_{i}. The second line contains a sequence of integers E_1, E_2, ..., E_{n} (1 ≤ E_1 < E_2... < E_{n} ≤ 10^9). It is guaranteed that all E_{i} are given in increasing order. -----Output----- If it is not possible to choose three states that satisfy all constraints, print -1. Otherwise, print one real number η — the maximum possible energy conversion efficiency. Your answer is considered correct its absolute or relative error does not exceed 10^{ - 9}. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if $\frac{|a - b|}{\operatorname{max}(1,|b|)} \leq 10^{-9}$. -----Examples----- Input 4 4 1 3 5 7 Output 0.5 Input 10 8 10 13 15 16 17 19 20 22 24 25 Output 0.875 Input 3 1 2 5 10 Output -1 -----Note----- In the first example choose states 1, 2 and 3, so that the energy conversion efficiency becomes equal to $\eta = \frac{5 - 3}{5 - 1} = 0.5$. In the second example choose states 4, 5 and 9, so that the energy conversion efficiency becomes equal to $\eta = \frac{24 - 17}{24 - 16} = 0.875$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def read_data(): n, m = map(int, input().strip().split()) a = list(map(int, list(input().strip().split()))) return n, m, a def find(start,end,v): mid = int((start + end) / 2) if start == end: return start if end - start == 1: if a[end] <= v: return end else: return start if a[mid] == v: return mid if a[mid] > v: return find(start,mid,v) else: return find(mid,end,v) def solve(): val = -1 for i in range(0,len(a)-2): pos = find(i+2,len(a)-1,a[i]+m) if a[pos] <= a[i] + m: if (a[pos] - a[i+1]) / (a[pos] - a[i]) > val: val = (a[pos] - a[i+1]) / (a[pos] - a[i]) return val n, m, a = read_data() print(solve()) ```
vfc_14358
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/924/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4 4\n1 3 5 7\n", "output": "0.5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 8\n10 13 15 16 17 19 20 22 24 25\n", "output": "0.875\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 1\n2 5 10\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 3\n4 6 8 9 10\n", "output": "0.5\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1153
Solve the following coding problem using the programming language python: Hacker Zhorik wants to decipher two secret messages he intercepted yesterday. Yeah message is a sequence of encrypted blocks, each of them consists of several bytes of information. Zhorik knows that each of the messages is an archive containing one or more files. Zhorik knows how each of these archives was transferred through the network: if an archive consists of k files of sizes l_1, l_2, ..., l_{k} bytes, then the i-th file is split to one or more blocks b_{i}, 1, b_{i}, 2, ..., b_{i}, m_{i} (here the total length of the blocks b_{i}, 1 + b_{i}, 2 + ... + b_{i}, m_{i} is equal to the length of the file l_{i}), and after that all blocks are transferred through the network, maintaining the order of files in the archive. Zhorik thinks that the two messages contain the same archive, because their total lengths are equal. However, each file can be split in blocks in different ways in the two messages. You are given the lengths of blocks in each of the two messages. Help Zhorik to determine what is the maximum number of files could be in the archive, if the Zhorik's assumption is correct. -----Input----- The first line contains two integers n, m (1 ≤ n, m ≤ 10^5) — the number of blocks in the first and in the second messages. The second line contains n integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^6) — the length of the blocks that form the first message. The third line contains m integers y_1, y_2, ..., y_{m} (1 ≤ y_{i} ≤ 10^6) — the length of the blocks that form the second message. It is guaranteed that x_1 + ... + x_{n} = y_1 + ... + y_{m}. Also, it is guaranteed that x_1 + ... + x_{n} ≤ 10^6. -----Output----- Print the maximum number of files the intercepted array could consist of. -----Examples----- Input 7 6 2 5 3 1 11 4 4 7 8 2 4 1 8 Output 3 Input 3 3 1 10 100 1 100 10 Output 2 Input 1 4 4 1 1 1 1 Output 1 -----Note----- In the first example the maximum number of files in the archive is 3. For example, it is possible that in the archive are three files of sizes 2 + 5 = 7, 15 = 3 + 1 + 11 = 8 + 2 + 4 + 1 and 4 + 4 = 8. In the second example it is possible that the archive contains two files of sizes 1 and 110 = 10 + 100 = 100 + 10. Note that the order of files is kept while transferring archives through the network, so we can't say that there are three files of sizes 1, 10 and 100. In the third example the only possibility is that the archive contains a single file of size 4. 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())) x=list(map(int,input().split())) y=list(map(int,input().split())) i,j=0,0 s=0 s1=0 s2=0 while i<n and j<m: if x[i]+s1==y[j]+s2: s+=1 i+=1 j+=1 s1=0 s2=0 elif x[i]+s1>y[j]+s2: s2+=y[j] j+=1 else: s1+=x[i] i+=1 print(s) ```
vfc_14366
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/950/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "7 6\n2 5 3 1 11 4 4\n7 8 2 4 1 8\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 3\n1 10 100\n1 100 10\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 4\n4\n1 1 1 1\n", "output": "1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1154
Solve the following coding problem using the programming language python: Vanya smashes potato in a vertical food processor. At each moment of time the height of the potato in the processor doesn't exceed h and the processor smashes k centimeters of potato each second. If there are less than k centimeters remaining, than during this second processor smashes all the remaining potato. Vanya has n pieces of potato, the height of the i-th piece is equal to a_{i}. He puts them in the food processor one by one starting from the piece number 1 and finishing with piece number n. Formally, each second the following happens: If there is at least one piece of potato remaining, Vanya puts them in the processor one by one, until there is not enough space for the next piece. Processor smashes k centimeters of potato (or just everything that is inside). Provided the information about the parameter of the food processor and the size of each potato in a row, compute how long will it take for all the potato to become smashed. -----Input----- The first line of the input contains integers n, h and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ h ≤ 10^9) — the number of pieces of potato, the height of the food processor and the amount of potato being smashed each second, respectively. The second line contains n integers a_{i} (1 ≤ a_{i} ≤ h) — the heights of the pieces. -----Output----- Print a single integer — the number of seconds required to smash all the potatoes following the process described in the problem statement. -----Examples----- Input 5 6 3 5 4 3 2 1 Output 5 Input 5 6 3 5 5 5 5 5 Output 10 Input 5 6 3 1 2 1 1 1 Output 2 -----Note----- Consider the first sample. First Vanya puts the piece of potato of height 5 into processor. At the end of the second there is only amount of height 2 remaining inside. Now Vanya puts the piece of potato of height 4. At the end of the second there is amount of height 3 remaining. Vanya puts the piece of height 3 inside and again there are only 3 centimeters remaining at the end of this second. Vanya finally puts the pieces of height 2 and 1 inside. At the end of the second the height of potato in the processor is equal to 3. During this second processor finally smashes all the remaining potato and the process finishes. In the second sample, Vanya puts the piece of height 5 inside and waits for 2 seconds while it is completely smashed. Then he repeats the same process for 4 other pieces. The total time is equal to 2·5 = 10 seconds. In the third sample, Vanya simply puts all the potato inside the processor and waits 2 seconds. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, h, k = [int(x) for x in input().split()] L=[int(x) for x in input().split()] L = L[::-1] p = 0 t = 0 while L: if L and h-p >= L[-1]: p+=L.pop() if L: req = L[-1]-h+p inc = (req-1)//k + 1 t += inc p -= inc*k p=max(p,0) if p: t += (p-1)//k + 1 print(t) ```
vfc_14370
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/677/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5 6 3\n5 4 3 2 1\n", "output": "5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 6 3\n5 5 5 5 5\n", "output": "10\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 6 3\n1 2 1 1 1\n", "output": "2\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1155
Solve the following coding problem using the programming language python: We often go to supermarkets to buy some fruits or vegetables, and on the tag there prints the price for a kilo. But in some supermarkets, when asked how much the items are, the clerk will say that $a$ yuan for $b$ kilos (You don't need to care about what "yuan" is), the same as $a/b$ yuan for a kilo. Now imagine you'd like to buy $m$ kilos of apples. You've asked $n$ supermarkets and got the prices. Find the minimum cost for those apples. You can assume that there are enough apples in all supermarkets. -----Input----- The first line contains two positive integers $n$ and $m$ ($1 \leq n \leq 5\,000$, $1 \leq m \leq 100$), denoting that there are $n$ supermarkets and you want to buy $m$ kilos of apples. The following $n$ lines describe the information of the supermarkets. Each line contains two positive integers $a, b$ ($1 \leq a, b \leq 100$), denoting that in this supermarket, you are supposed to pay $a$ yuan for $b$ kilos of apples. -----Output----- The only line, denoting the minimum cost for $m$ kilos of apples. Please make sure that the absolute or relative error between your answer and the correct answer won't exceed $10^{-6}$. Formally, let your answer be $x$, and the jury's answer be $y$. Your answer is considered correct if $\frac{|x - y|}{\max{(1, |y|)}} \le 10^{-6}$. -----Examples----- Input 3 5 1 2 3 4 1 3 Output 1.66666667 Input 2 1 99 100 98 99 Output 0.98989899 -----Note----- In the first sample, you are supposed to buy $5$ kilos of apples in supermarket $3$. The cost is $5/3$ yuan. In the second sample, you are supposed to buy $1$ kilo of apples in supermarket $2$. The cost is $98/99$ yuan. 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().split()) n, m = list(map(int, input().split())) ans = 10 ** 100 for i in range(n): p, q = list(map(int, input().split())) ans = min(ans, p / q * m) print(ans) ```
vfc_14374
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/919/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 5\n1 2\n3 4\n1 3\n", "output": "1.66666667\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 1\n99 100\n98 99\n", "output": "0.98989899\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1156
Solve the following coding problem using the programming language python: "We've tried solitary confinement, waterboarding and listening to Just In Beaver, to no avail. We need something extreme." "Little Alena got an array as a birthday present..." The array b of length n is obtained from the array a of length n and two integers l and r (l ≤ r) using the following procedure: b_1 = b_2 = b_3 = b_4 = 0. For all 5 ≤ i ≤ n: b_{i} = 0 if a_{i}, a_{i} - 1, a_{i} - 2, a_{i} - 3, a_{i} - 4 > r and b_{i} - 1 = b_{i} - 2 = b_{i} - 3 = b_{i} - 4 = 1 b_{i} = 1 if a_{i}, a_{i} - 1, a_{i} - 2, a_{i} - 3, a_{i} - 4 < l and b_{i} - 1 = b_{i} - 2 = b_{i} - 3 = b_{i} - 4 = 0 b_{i} = b_{i} - 1 otherwise You are given arrays a and b' of the same length. Find two integers l and r (l ≤ r), such that applying the algorithm described above will yield an array b equal to b'. It's guaranteed that the answer exists. -----Input----- The first line of input contains a single integer n (5 ≤ n ≤ 10^5) — the length of a and b'. The second line of input contains n space separated integers a_1, ..., a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9) — the elements of a. The third line of input contains a string of n characters, consisting of 0 and 1 — the elements of b'. Note that they are not separated by spaces. -----Output----- Output two integers l and r ( - 10^9 ≤ l ≤ r ≤ 10^9), conforming to the requirements described above. If there are multiple solutions, output any of them. It's guaranteed that the answer exists. -----Examples----- Input 5 1 2 3 4 5 00001 Output 6 15 Input 10 -10 -9 -8 -7 -6 6 7 8 9 10 0000111110 Output -5 5 -----Note----- In the first test case any pair of l and r pair is valid, if 6 ≤ l ≤ r ≤ 10^9, in that case b_5 = 1, because a_1, ..., a_5 < l. 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 = input() r = 1000000000 l = -r for i in range(4, n): if b[i - 1] != b[i]: if b[i] == '0': r = min(r, min(a[i - 4: i + 1]) - 1) else: l = max(l, max(a[i - 4: i + 1]) + 1) print(l, r) ```
vfc_14378
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/940/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n1 2 3 4 5\n00001\n", "output": "6 1000000000\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n-10 -9 -8 -7 -6 6 7 8 9 10\n0000111110\n", "output": "-5 5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n-8 -9 -9 -7 -10 -10 -8 -8 -9 -10\n0000000011\n", "output": "-7 1000000000\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "11\n226 226 226 226 226 227 1000000000 1000000000 228 1000000000 1000000000\n00001111110\n", "output": "227 227\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "95\n-97 -98 -92 -93 94 96 91 98 95 85 90 86 84 83 81 79 82 79 73 -99 -91 -93 -92 -97 -85 -88 -89 -83 -86 -75 -80 -78 -74 -76 62 68 63 64 69 -71 -70 -72 -69 -71 53 57 60 54 61 -64 -64 -68 -58 -63 -54 -52 -51 -50 -49 -46 -39 -38 -42 -42 48 44 51 45 43 -31 -32 -33 -28 -30 -21 -17 -20 -25 -19 -13 -8 -10 -12 -7 33 34 34 42 32 30 25 29 23 30 20\n00000000000000000000000111111111111111000001111100000111111111111111000001111111111111110000000\n", "output": "-27 31\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1157
Solve the following coding problem using the programming language python: You are given a sequence $a_1, a_2, \dots, a_n$ consisting of $n$ non-zero integers (i.e. $a_i \ne 0$). You have to calculate two following values: the number of pairs of indices $(l, r)$ $(l \le r)$ such that $a_l \cdot a_{l + 1} \dots a_{r - 1} \cdot a_r$ is negative; the number of pairs of indices $(l, r)$ $(l \le r)$ such that $a_l \cdot a_{l + 1} \dots a_{r - 1} \cdot a_r$ is positive; -----Input----- The first line contains one integer $n$ $(1 \le n \le 2 \cdot 10^{5})$ — the number of elements in the sequence. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(-10^{9} \le a_i \le 10^{9}; a_i \neq 0)$ — the elements of the sequence. -----Output----- Print two integers — the number of subsegments with negative product and the number of subsegments with positive product, respectively. -----Examples----- Input 5 5 -3 3 -1 1 Output 8 7 Input 10 4 2 -4 3 1 2 -4 3 2 3 Output 28 27 Input 5 -1 -2 -3 -4 -5 Output 9 6 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def main(): import sys input = sys.stdin.readline n = int(input()) arr = list(map(int, input().split())) cnt = [1, 0] lst = [0] * (n + 1) lst[0] = 0 for i in range(n): lst[i + 1] = lst[i] ^ (arr[i] < 0) cnt[lst[i + 1]] += 1 ans1 = cnt[0] * cnt[1] ans2 = cnt[0] * (cnt[0] - 1) // 2 + cnt[1] * (cnt[1] - 1) // 2 print(ans1, ans2) return 0 main() ```
vfc_14382
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1215/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n5 -3 3 -1 1\n", "output": "8 7\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n4 2 -4 3 1 2 -4 3 2 3\n", "output": "28 27\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n-1 -2 -3 -4 -5\n", "output": "9 6\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1158
Solve the following coding problem using the programming language python: The king's birthday dinner was attended by $k$ guests. The dinner was quite a success: every person has eaten several dishes (though the number of dishes was the same for every person) and every dish was served alongside with a new set of kitchen utensils. All types of utensils in the kingdom are numbered from $1$ to $100$. It is known that every set of utensils is the same and consist of different types of utensils, although every particular type may appear in the set at most once. For example, a valid set of utensils can be composed of one fork, one spoon and one knife. After the dinner was over and the guests were dismissed, the king wondered what minimum possible number of utensils could be stolen. Unfortunately, the king has forgotten how many dishes have been served for every guest but he knows the list of all the utensils left after the dinner. Your task is to find the minimum possible number of stolen utensils. -----Input----- The first line contains two integer numbers $n$ and $k$ ($1 \le n \le 100, 1 \le k \le 100$)  — the number of kitchen utensils remaining after the dinner and the number of guests correspondingly. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 100$)  — the types of the utensils remaining. Equal values stand for identical utensils while different values stand for different utensils. -----Output----- Output a single value — the minimum number of utensils that could be stolen by the guests. -----Examples----- Input 5 2 1 2 2 1 3 Output 1 Input 10 3 1 3 3 1 3 5 5 5 5 100 Output 14 -----Note----- In the first example it is clear that at least one utensil of type $3$ has been stolen, since there are two guests and only one such utensil. But it is also possible that every person received only one dish and there were only six utensils in total, when every person got a set $(1, 2, 3)$ of utensils. Therefore, the answer is $1$. One can show that in the second example at least $2$ dishes should have been served for every guest, so the number of utensils should be at least $24$: every set contains $4$ utensils and every one of the $3$ guests gets two such sets. Therefore, at least $14$ objects have been stolen. Please note that utensils of some types (for example, of types $2$ and $4$ in this example) may be not present in the set served for dishes. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python rest, people = map(int, input().split()) types = list(map(int, input().split())) a = dict() for elem in types: if elem in a: a[elem] += 1 else: a[elem] = 1 maximum = 0 for key in a: if a[key] > maximum: maximum = a[key] needed = maximum while needed % people != 0: needed += 1 ans = 0 for key in a: ans += needed - a[key] print(ans) ```
vfc_14386
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1032/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5 2\n1 2 2 1 3\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 3\n1 3 3 1 3 5 5 5 5 100\n", "output": "14\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 5\n63 63 63 63 63 63 63 63 63 63\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 6\n91 51 91 51 51 91 51 91 91 91\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "100 1\n84 85 2 96 83 57 9 95 34 14 21 6 10 26 32 1 29 77 39 91 42 87 74 65 73 58 66 23 37 13 100 90 30 46 92 4 80 61 47 53 44 75 76 3 7 79 93 24 27 97 49 11 67 70 38 98 59 69 82 81 16 12 51 48 45 17 50 88 43 94 40 60 62 25 68 28 54 20 63 72 18 41 52 5 56 15 71 78 64 19 89 8 36 31 33 99 22 55 86 35\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "100 10\n75 75 75 75 30 53 75 31 75 62 44 44 69 76 57 31 75 83 75 13 56 59 30 75 27 56 75 75 47 31 79 75 30 20 31 8 75 75 30 20 75 31 31 75 75 4 52 75 44 20 31 66 55 75 75 75 31 75 33 42 31 82 14 31 75 75 75 75 31 80 31 30 75 88 31 75 69 75 31 75 30 75 44 17 75 75 35 75 75 75 75 75 56 44 75 75 44 75 20 31\n", "output": "1400\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1160
Solve the following coding problem using the programming language python: The organizers of a programming contest have decided to present t-shirts to participants. There are six different t-shirts sizes in this problem: S, M, L, XL, XXL, XXXL (sizes are listed in increasing order). The t-shirts are already prepared. For each size from S to XXXL you are given the number of t-shirts of this size. During the registration, the organizers asked each of the n participants about the t-shirt size he wants. If a participant hesitated between two sizes, he could specify two neighboring sizes — this means that any of these two sizes suits him. Write a program that will determine whether it is possible to present a t-shirt to each participant of the competition, or not. Of course, each participant should get a t-shirt of proper size: the size he wanted, if he specified one size; any of the two neibouring sizes, if he specified two sizes. If it is possible, the program should find any valid distribution of the t-shirts. -----Input----- The first line of the input contains six non-negative integers — the number of t-shirts of each size. The numbers are given for the sizes S, M, L, XL, XXL, XXXL, respectively. The total number of t-shirts doesn't exceed 100 000. The second line contains positive integer n (1 ≤ n ≤ 100 000) — the number of participants. The following n lines contain the sizes specified by the participants, one line per participant. The i-th line contains information provided by the i-th participant: single size or two sizes separated by comma (without any spaces). If there are two sizes, the sizes are written in increasing order. It is guaranteed that two sizes separated by comma are neighboring. -----Output----- If it is not possible to present a t-shirt to each participant, print «NO» (without quotes). Otherwise, print n + 1 lines. In the first line print «YES» (without quotes). In the following n lines print the t-shirt sizes the orginizers should give to participants, one per line. The order of the participants should be the same as in the input. If there are multiple solutions, print any of them. -----Examples----- Input 0 1 0 1 1 0 3 XL S,M XL,XXL Output YES XL M XXL Input 1 1 2 0 1 1 5 S M S,M XXL,XXXL XL,XXL Output NO The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python 3 szs = ['S', 'M', 'L', 'XL', 'XXL', 'XXXL'] d = {'S':0, 'M':1, 'L':2, 'XL':3, 'XXL':4, 'XXXL':5} neigh = [[] for i in range(5)] of = list(map(int, input().split())) n = int(input()) ans = ['' for i in range(n)] for i in range(n): t = input() if ',' in t: neigh[d[t.split(',')[0]]].append(i) else: of[d[t]] -= 1 ans[i] = t for i in range(6): if of[i] < 0: print("NO") return if i > 0: while len(neigh[i - 1]) and of[i] > 0: ans[neigh[i - 1][-1]] = szs[i] neigh[i - 1].pop() of[i] -= 1 if i < 5: while len(neigh[i]) and of[i] > 0: ans[neigh[i][-1]] = szs[i] neigh[i].pop() of[i] -= 1 if sum([len(neigh[i]) for i in range(5)]) != 0: print("NO") else: print("YES") print("\n".join(ans)) ```
vfc_14394
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/727/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "0 1 0 1 1 0\n3\nXL\nS,M\nXL,XXL\n", "output": "YES\nXL\nM\nXXL\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1 2 0 1 1\n5\nS\nM\nS,M\nXXL,XXXL\nXL,XXL\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 2 4 4 1 1\n10\nXL\nXL\nS,M\nL\nM,L\nL\nS,M\nM\nXL,XXL\nXL\n", "output": "YES\nXL\nXL\nS\nL\nL\nL\nM\nM\nXL\nXL\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 3 0 2 2 2\n10\nL,XL\nS,M\nXXL,XXXL\nS,M\nS,M\nXXXL\nXL,XXL\nXXL\nS,M\nXL\n", "output": "YES\nXL\nS\nXXXL\nM\nM\nXXXL\nXXL\nXXL\nM\nXL\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 1 5 2 4 3\n20\nL,XL\nS,M\nL,XL\nXXL,XXXL\nS,M\nS,M\nXL,XXL\nL,XL\nS,M\nL,XL\nS,M\nM,L\nXXL,XXXL\nXXL,XXXL\nL\nXXL,XXXL\nXL,XXL\nM,L\nS,M\nXXL\n", "output": "YES\nL\nS\nL\nXXL\nS\nS\nXXL\nXL\nS\nXL\nS\nL\nXXXL\nXXXL\nL\nXXXL\nXXL\nL\nM\nXXL\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1161
Solve the following coding problem using the programming language python: You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python seq = input() a = [] fl = True z = 0 for i in seq: if i == '<' or i == '[' or i == '{' or i == '(': a.append(i) elif i == '>': if len(a) and a[-1] == '<': a.pop() elif len(a) and a[-1] != '<': a.pop() z += 1 else: fl = False break elif i == ')': if len(a) and a[-1] == '(': a.pop() elif len(a) and a[-1] != '(': a.pop() z += 1 else: fl = False break elif i == ']': if len(a) and a[-1] == '[': a.pop() elif len(a) and a[-1] != '[': a.pop() z += 1 else: fl = False break elif i == '}': if len(a) and a[-1] == '{': a.pop() elif len(a) and a[-1] != '{': a.pop() z += 1 else: fl = False break if len(a): fl = False if fl: print(z) else: print('Impossible') ```
vfc_14398
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/612/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "[<}){}\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "{()}[]\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1162
Solve the following coding problem using the programming language python: Hasan loves playing games and has recently discovered a game called TopScore. In this soccer-like game there are $p$ players doing penalty shoot-outs. Winner is the one who scores the most. In case of ties, one of the top-scorers will be declared as the winner randomly with equal probability. They have just finished the game and now are waiting for the result. But there's a tiny problem! The judges have lost the paper of scores! Fortunately they have calculated sum of the scores before they get lost and also for some of the players they have remembered a lower bound on how much they scored. However, the information about the bounds is private, so Hasan only got to know his bound. According to the available data, he knows that his score is at least $r$ and sum of the scores is $s$. Thus the final state of the game can be represented in form of sequence of $p$ integers $a_1, a_2, \dots, a_p$ ($0 \le a_i$) — player's scores. Hasan is player number $1$, so $a_1 \ge r$. Also $a_1 + a_2 + \dots + a_p = s$. Two states are considered different if there exists some position $i$ such that the value of $a_i$ differs in these states. Once again, Hasan doesn't know the exact scores (he doesn't know his exact score as well). So he considers each of the final states to be equally probable to achieve. Help Hasan find the probability of him winning. It can be shown that it is in the form of $\frac{P}{Q}$ where $P$ and $Q$ are non-negative integers and $Q \ne 0$, $P \le Q$. Report the value of $P \cdot Q^{-1} \pmod {998244353}$. -----Input----- The only line contains three integers $p$, $s$ and $r$ ($1 \le p \le 100$, $0 \le r \le s \le 5000$) — the number of players, the sum of scores of all players and Hasan's score, respectively. -----Output----- Print a single integer — the probability of Hasan winning. It can be shown that it is in the form of $\frac{P}{Q}$ where $P$ and $Q$ are non-negative integers and $Q \ne 0$, $P \le Q$. Report the value of $P \cdot Q^{-1} \pmod {998244353}$. -----Examples----- Input 2 6 3 Output 124780545 Input 5 20 11 Output 1 Input 10 30 10 Output 85932500 -----Note----- In the first example Hasan can score $3$, $4$, $5$ or $6$ goals. If he scores $4$ goals or more than he scores strictly more than his only opponent. If he scores $3$ then his opponent also scores $3$ and Hasan has a probability of $\frac 1 2$ to win the game. Thus, overall he has the probability of $\frac 7 8$ to win. In the second example even Hasan's lower bound on goal implies him scoring more than any of his opponents. Thus, the resulting probability is $1$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python base=998244353; def power(x, y): if(y==0): return 1 t=power(x, y//2) t=(t*t)%base if(y%2): t=(t*x)%base return t; def inverse(x): return power(x, base-2) f=[1] iv=[1] for i in range(1, 5555): f.append((f[i-1]*i)%base) iv.append(inverse(f[i])) def C(n, k): return (f[n]*iv[k]*iv[n-k])%base def candy(n, k): # print(n, k) return C(n+k-1, k-1) def count_game(k, n, x): #k players, n points total, no player can have x point or more if(k==0): if(n==0): return 1 else: return 0 ans=0 for i in range(0, k+1): t=n-x*i # print(i, C(k, i)) if(t<0): break if(i%2): ans=(ans-C(k, i)*candy(t, k))%base else: ans=(ans+C(k, i)*candy(t, k))%base return ans p, s, r= list(map(int, input().split())) gamesize=count_game(p, s-r, int(1e18)) gamesize=inverse(gamesize) ans=0; for q in range(r, s+1): for i in range(0, p): #exactly i people have the same score t=s-(i+1)*q if(t<0): break # print(q, i, count_game(p-i-1, t, q)); ans=(ans+C(p-1, i)*count_game(p-i-1, t, q)*gamesize*inverse(i+1))%base print(ans) ```
vfc_14402
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1096/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2 6 3\n", "output": "124780545\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 20 11\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 30 10\n", "output": "85932500\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "100 0 0\n", "output": "828542813\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "100 1 0\n", "output": "828542813\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 5000 0\n", "output": "1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1163
Solve the following coding problem using the programming language python: There are n boys and m girls studying in the class. They should stand in a line so that boys and girls alternated there as much as possible. Let's assume that positions in the line are indexed from left to right by numbers from 1 to n + m. Then the number of integers i (1 ≤ i < n + m) such that positions with indexes i and i + 1 contain children of different genders (position i has a girl and position i + 1 has a boy or vice versa) must be as large as possible. Help the children and tell them how to form the line. -----Input----- The single line of the input contains two integers n and m (1 ≤ n, m ≤ 100), separated by a space. -----Output----- Print a line of n + m characters. Print on the i-th position of the line character "B", if the i-th position of your arrangement should have a boy and "G", if it should have a girl. Of course, the number of characters "B" should equal n and the number of characters "G" should equal m. If there are multiple optimal solutions, print any of them. -----Examples----- Input 3 3 Output GBGBGB Input 4 2 Output BGBGBB -----Note----- In the first sample another possible answer is BGBGBG. In the second sample answer BBGBGB is also optimal. 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 * inStr=next(open('input.txt')) n=int(inStr.split()[0]) m=int(inStr.split()[1]) boys=repeat('B', n) girls=repeat('G', m) if n>m: pairs = zip_longest(boys, girls) else: pairs = zip_longest(girls, boys) result = (y for x in pairs for y in x if y is not None) ans = ''.join(result) open('output.txt', 'w').write(ans) print(ans) ```
vfc_14406
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/253/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 3\n", "output": "GBGBGB\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 2\n", "output": "BGBGBB\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1164
Solve the following coding problem using the programming language python: Vasily exited from a store and now he wants to recheck the total price of all purchases in his bill. The bill is a string in which the names of the purchases and their prices are printed in a row without any spaces. Check has the format "name_1price_1name_2price_2...name_{n}price_{n}", where name_{i} (name of the i-th purchase) is a non-empty string of length not more than 10, consisting of lowercase English letters, and price_{i} (the price of the i-th purchase) is a non-empty string, consisting of digits and dots (decimal points). It is possible that purchases with equal names have different prices. The price of each purchase is written in the following format. If the price is an integer number of dollars then cents are not written. Otherwise, after the number of dollars a dot (decimal point) is written followed by cents in a two-digit format (if number of cents is between 1 and 9 inclusively, there is a leading zero). Also, every three digits (from less significant to the most) in dollars are separated by dot (decimal point). No extra leading zeroes are allowed. The price always starts with a digit and ends with a digit. For example: "234", "1.544", "149.431.10", "0.99" and "123.05" are valid prices, ".333", "3.33.11", "12.00", ".33", "0.1234" and "1.2" are not valid. Write a program that will find the total price of all purchases in the given bill. -----Input----- The only line of the input contains a non-empty string s with length not greater than 1000 — the content of the bill. It is guaranteed that the bill meets the format described above. It is guaranteed that each price in the bill is not less than one cent and not greater than 10^6 dollars. -----Output----- Print the total price exactly in the same format as prices given in the input. -----Examples----- Input chipsy48.32televizor12.390 Output 12.438.32 Input a1b2c3.38 Output 6.38 Input aa0.01t0.03 Output 0.04 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python 3 s = input() alph = ''.join([chr(ord('a') + x) for x in range(26)]) l = [[]] for x in s: if x not in alph: l[-1].append(x) else: if len(l[-1]): l.append([]) l = list([''.join(x) for x in l]) ansa = 0 ansb = 0 for t in l: if len(t) > 2 and t[-3] == '.': ansb += int(t[-2:]) t = t[:-3] ansa += int(''.join(t.split('.'))) ansa += ansb // 100 ansb %= 100 ansa = str(ansa) ans = [] last = len(ansa) for x in range(len(ansa) - 3, -1, -3): ans.append(ansa[x:last]) last = x if last != 0: ans.append(ansa[:last]) ans.reverse() if ansb != 0: ans.append("%02d" % ansb) print(".".join(ans)) ```
vfc_14410
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/727/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "chipsy48.32televizor12.390\n", "output": "12.438.32\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "a1b2c3.38\n", "output": "6.38\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "aa0.01t0.03\n", "output": "0.04\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "test0.50test0.50\n", "output": "1", "type": "stdin_stdout" } ] }
apps
verifiable_code
1165
Solve the following coding problem using the programming language python: You are given array a with n integers and m queries. The i-th query is given with three integers l_{i}, r_{i}, x_{i}. For the i-th query find any position p_{i} (l_{i} ≤ p_{i} ≤ r_{i}) so that a_{p}_{i} ≠ x_{i}. -----Input----- The first line contains two integers n, m (1 ≤ n, m ≤ 2·10^5) — the number of elements in a and the number of queries. The second line contains n integers a_{i} (1 ≤ a_{i} ≤ 10^6) — the elements of the array a. Each of the next m lines contains three integers l_{i}, r_{i}, x_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n, 1 ≤ x_{i} ≤ 10^6) — the parameters of the i-th query. -----Output----- Print m lines. On the i-th line print integer p_{i} — the position of any number not equal to x_{i} in segment [l_{i}, r_{i}] or the value - 1 if there is no such number. -----Examples----- Input 6 4 1 2 1 1 3 5 1 4 1 2 6 2 3 4 1 3 4 2 Output 2 6 -1 4 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import collections import math n ,m = map(int, input().split()) A = list(map(int, input().split())) ans, f = [], [0] * n f[0] = -1 for i in range(1, n): if A[i] != A[i - 1]: f[i] = i - 1 else: f[i] = f[i - 1] for i in range(m): l, r, x = map(int, input().split()) #q.append([l - 1, r - 1, x]) #for i in range(m): if A[r - 1] != x: ans.append(r) elif f[r - 1] >= l - 1: ans.append(f[r - 1] + 1) else: ans.append(-1) print('\n'.join(str(x) for x in ans)) ```
vfc_14414
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/622/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6 4\n1 2 1 1 3 5\n1 4 1\n2 6 2\n3 4 1\n3 4 2\n", "output": "2\n6\n-1\n4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1\n1\n1 1 1\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1\n2\n1 1 2\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1\n569888\n1 1 967368\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 10\n1 1 1 1 1 1 1 1 1 1\n3 10 1\n3 6 1\n1 8 1\n1 7 1\n1 5 1\n3 7 1\n4 7 1\n9 9 1\n6 7 1\n3 4 1\n", "output": "-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 10\n1 2 2 2 2 1 1 2 1 1\n3 3 1\n4 9 1\n4 8 1\n2 7 2\n2 8 2\n3 10 1\n7 7 2\n10 10 2\n1 5 1\n1 2 1\n", "output": "3\n8\n8\n7\n7\n8\n7\n10\n5\n2\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1166
Solve the following coding problem using the programming language python: After a long day, Alice and Bob decided to play a little game. The game board consists of $n$ cells in a straight line, numbered from $1$ to $n$, where each cell contains a number $a_i$ between $1$ and $n$. Furthermore, no two cells contain the same number. A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $i$ to cell $j$ only if the following two conditions are satisfied: the number in the new cell $j$ must be strictly larger than the number in the old cell $i$ (i.e. $a_j > a_i$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $|i-j|\bmod a_i = 0$). Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players. -----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, \ldots, a_n$ ($1 \le a_i \le n$). Furthermore, there are no pair of indices $i \neq j$ such that $a_i = a_j$. -----Output----- Print $s$ — a string of $n$ characters, where the $i$-th character represents the outcome of the game if the token is initially placed in the cell $i$. If Alice wins, then $s_i$ has to be equal to "A"; otherwise, $s_i$ has to be equal to "B". -----Examples----- Input 8 3 6 5 4 2 7 1 8 Output BAAAABAB Input 15 3 11 2 5 10 9 7 13 15 8 4 12 6 1 14 Output ABAAAABBBAABAAB -----Note----- In the first sample, if Bob puts the token on the number (not position): $1$: Alice can move to any number. She can win by picking $7$, from which Bob has no move. $2$: Alice can move to $3$ and $5$. Upon moving to $5$, Bob can win by moving to $8$. If she chooses $3$ instead, she wins, as Bob has only a move to $4$, from which Alice can move to $8$. $3$: Alice can only move to $4$, after which Bob wins by moving to $8$. $4$, $5$, or $6$: Alice wins by moving to $8$. $7$, $8$: Alice has no move, and hence she loses immediately. 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 __starting_point(): cin = sys.stdin n = int(next(cin)) a = list(map(int, next(cin).split())) n2idx = {a[i]: i for i in range(n)} f = [False] * (n+1) for i in range(n, 0, -1): idx_lg = n2idx[i] win_flag = False for j in range(idx_lg%i, n, i): if a[j] > i and not f[a[j]]: win_flag = True break f[i] = win_flag f = ''.join(['A' if f[a_i] else 'B' for a_i in a]) print(f, flush=True) __starting_point() ```
vfc_14418
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1033/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "8\n3 6 5 4 2 7 1 8\n", "output": "BAAAABAB\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "15\n3 11 2 5 10 9 7 13 15 8 4 12 6 1 14\n", "output": "ABAAAABBBAABAAB\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n1\n", "output": "B\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1167
Solve the following coding problem using the programming language python: Tomorrow is a difficult day for Polycarp: he has to attend $a$ lectures and $b$ practical classes at the university! Since Polycarp is a diligent student, he is going to attend all of them. While preparing for the university, Polycarp wonders whether he can take enough writing implements to write all of the lectures and draw everything he has to during all of the practical classes. Polycarp writes lectures using a pen (he can't use a pencil to write lectures!); he can write down $c$ lectures using one pen, and after that it runs out of ink. During practical classes Polycarp draws blueprints with a pencil (he can't use a pen to draw blueprints!); one pencil is enough to draw all blueprints during $d$ practical classes, after which it is unusable. Polycarp's pencilcase can hold no more than $k$ writing implements, so if Polycarp wants to take $x$ pens and $y$ pencils, they will fit in the pencilcase if and only if $x + y \le k$. Now Polycarp wants to know how many pens and pencils should he take. Help him to determine it, or tell that his pencilcase doesn't have enough room for all the implements he needs tomorrow! Note that you don't have to minimize the number of writing implements (though their total number must not exceed $k$). -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 100$) — the number of test cases in the input. Then the test cases follow. Each test case is described by one line containing five integers $a$, $b$, $c$, $d$ and $k$, separated by spaces ($1 \le a, b, c, d, k \le 100$) — the number of lectures Polycarp has to attend, the number of practical classes Polycarp has to attend, the number of lectures which can be written down using one pen, the number of practical classes for which one pencil is enough, and the number of writing implements that can fit into Polycarp's pencilcase, respectively. In hacks it is allowed to use only one test case in the input, so $t = 1$ should be satisfied. -----Output----- For each test case, print the answer as follows: If the pencilcase can't hold enough writing implements to use them during all lectures and practical classes, print one integer $-1$. Otherwise, print two non-negative integers $x$ and $y$ — the number of pens and pencils Polycarp should put in his pencilcase. If there are multiple answers, print any of them. Note that you don't have to minimize the number of writing implements (though their total number must not exceed $k$). -----Example----- Input 3 7 5 4 5 8 7 5 4 5 2 20 53 45 26 4 Output 7 1 -1 1 3 -----Note----- There are many different answers for the first test case; $x = 7$, $y = 1$ is only one of them. For example, $x = 3$, $y = 1$ is also correct. $x = 1$, $y = 3$ is the only correct answer for the third test case. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from math import * def ri(): return int(input()) def rli(): return list(map(int, input().split())) q = ri() for _ in range(q): a, b, c, d, k = rli() x = int(ceil(a / c)) y = int(ceil(b / d)) if x + y <= k: print(x, y) else: print(-1) ```
vfc_14422
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1244/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n7 5 4 5 8\n7 5 4 5 2\n20 53 45 26 4\n", "output": "7 1\n-1\n1 3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "8\n80 72 20 12 10\n81 72 20 12 11\n80 73 20 12 11\n81 73 20 12 12\n80 73 20 12 10\n3 99 5 1 100\n99 1 1 4 100\n53 37 13 3 17\n", "output": "4 6\n5 6\n4 7\n5 7\n-1\n1 99\n99 1\n-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n2 5 4 2 2\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n5 4 6 1 5\n", "output": "1 4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n2 2 2 3 2\n", "output": "1 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n1 6 3 2 3\n", "output": "-1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1168
Solve the following coding problem using the programming language python: Disclaimer: there are lots of untranslateable puns in the Russian version of the statement, so there is one more reason for you to learn Russian :) Rick and Morty like to go to the ridge High Cry for crying loudly — there is an extraordinary echo. Recently they discovered an interesting acoustic characteristic of this ridge: if Rick and Morty begin crying simultaneously from different mountains, their cry would be heard between these mountains up to the height equal the bitwise OR of mountains they've climbed and all the mountains between them. Bitwise OR is a binary operation which is determined the following way. Consider representation of numbers x and y in binary numeric system (probably with leading zeroes) x = x_{k}... x_1x_0 and y = y_{k}... y_1y_0. Then z = x | y is defined following way: z = z_{k}... z_1z_0, where z_{i} = 1, if x_{i} = 1 or y_{i} = 1, and z_{i} = 0 otherwise. In the other words, digit of bitwise OR of two numbers equals zero if and only if digits at corresponding positions is both numbers equals zero. For example bitwise OR of numbers 10 = 1010_2 and 9 = 1001_2 equals 11 = 1011_2. In programming languages C/C++/Java/Python this operation is defined as «|», and in Pascal as «or». Help Rick and Morty calculate the number of ways they can select two mountains in such a way that if they start crying from these mountains their cry will be heard above these mountains and all mountains between them. More formally you should find number of pairs l and r (1 ≤ l < r ≤ n) such that bitwise OR of heights of all mountains between l and r (inclusive) is larger than the height of any mountain at this interval. -----Input----- The first line contains integer n (1 ≤ n ≤ 200 000), the number of mountains in the ridge. Second line contains n integers a_{i} (0 ≤ a_{i} ≤ 10^9), the heights of mountains in order they are located in the ridge. -----Output----- Print the only integer, the number of ways to choose two different mountains. -----Examples----- Input 5 3 2 1 6 5 Output 8 Input 4 3 3 3 3 Output 0 -----Note----- In the first test case all the ways are pairs of mountains with the numbers (numbering from one):(1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5) In the second test case there are no such pairs because for any pair of mountains the height of cry from them is 3, and this height is equal to the height of any mountain. 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()] l = [i for i in range(len(a))] r = [i for i in range(len(a))] for i in range(len(a)): while l[i]>=1 and a[i]|a[l[i]-1]<=a[i]: l[i] = l[l[i]-1] for j in range(len(a)): i = len(a)-j-1 while r[i]<len(a)-1 and a[i]|a[r[i]+1]<=a[i] and a[i]>a[r[i]+1]: r[i] = r[r[i]+1] count=0 for i in range(len(a)): x = r[i]-i+1 y = i-l[i]+1 count+=x*y-1 print((n*(n-1))//2-count) ```
vfc_14426
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/876/F", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n3 2 1 6 5\n", "output": "8\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1169
Solve the following coding problem using the programming language python: Vasya has got an undirected graph consisting of $n$ vertices and $m$ edges. This graph doesn't contain any self-loops or multiple edges. Self-loop is an edge connecting a vertex to itself. Multiple edges are a pair of edges such that they connect the same pair of vertices. Since the graph is undirected, the pair of edges $(1, 2)$ and $(2, 1)$ is considered to be multiple edges. Isolated vertex of the graph is a vertex such that there is no edge connecting this vertex to any other vertex. Vasya wants to know the minimum and maximum possible number of isolated vertices in an undirected graph consisting of $n$ vertices and $m$ edges. -----Input----- The only line contains two integers $n$ and $m~(1 \le n \le 10^5, 0 \le m \le \frac{n (n - 1)}{2})$. It is guaranteed that there exists a graph without any self-loops or multiple edges with such number of vertices and edges. -----Output----- In the only line print two numbers $min$ and $max$ — the minimum and maximum number of isolated vertices, respectively. -----Examples----- Input 4 2 Output 0 1 Input 3 1 Output 1 1 -----Note----- In the first example it is possible to construct a graph with $0$ isolated vertices: for example, it should contain edges $(1, 2)$ and $(3, 4)$. To get one isolated vertex, we may construct a graph with edges $(1, 2)$ and $(1, 3)$. In the second example the graph will always contain exactly one isolated vertex. 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()) if m == 0: print(n, n) return if m == n * (n - 1) // 2: print(0, 0) return L = 0 R = n + 1 while R - L > 1: m1 = (L + R) // 2 if m1 * (m1 - 1) // 2 < m: L = m1 else: R = m1 ans_max = n - R ans_min = max(0, n - 2 * m) print(ans_min, ans_max) ```
vfc_14430
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1065/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4 2\n", "output": "0 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 1\n", "output": "1 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "20 55\n", "output": "0 9\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "20 54\n", "output": "0 9\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1170
Solve the following coding problem using the programming language python: Let's denote a m-free matrix as a binary (that is, consisting of only 1's and 0's) matrix such that every square submatrix of size m × m of this matrix contains at least one zero. Consider the following problem: You are given two integers n and m. You have to construct an m-free square matrix of size n × n such that the number of 1's in this matrix is maximum possible. Print the maximum possible number of 1's in such matrix. You don't have to solve this problem. Instead, you have to construct a few tests for it. You will be given t numbers x_1, x_2, ..., x_{t}. For every $i \in [ 1, t ]$, find two integers n_{i} and m_{i} (n_{i} ≥ m_{i}) such that the answer for the aforementioned problem is exactly x_{i} if we set n = n_{i} and m = m_{i}. -----Input----- The first line contains one integer t (1 ≤ t ≤ 100) — the number of tests you have to construct. Then t lines follow, i-th line containing one integer x_{i} (0 ≤ x_{i} ≤ 10^9). Note that in hacks you have to set t = 1. -----Output----- For each test you have to construct, output two positive numbers n_{i} and m_{i} (1 ≤ m_{i} ≤ n_{i} ≤ 10^9) such that the maximum number of 1's in a m_{i}-free n_{i} × n_{i} matrix is exactly x_{i}. If there are multiple solutions, you may output any of them; and if this is impossible to construct a test, output a single integer - 1. -----Example----- Input 3 21 0 1 Output 5 2 1 1 -1 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python t = int(input()) for k in range(t): x = int(input()) if x == 0: print(1, 1) continue for i in range(1, int(x ** 0.5) + 2): if x % i == 0 and (x // i - i) % 2 == 0 and (x // i - (x // i - i) // 2) ** 2 >= x: a, b = x // i, i y = (a - b) // 2 n = a - y if y == 0: continue m = n // y if n // m != y: continue print(n, m) break else: print(-1) ```
vfc_14434
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/938/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n21\n0\n1\n", "output": "5 2\n1 1\n-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n420441920\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n4\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n297540\n", "output": "546 22\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n9\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n144\n", "output": "-1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1179
Solve the following coding problem using the programming language python: In late autumn evening n robots gathered in the cheerful company of friends. Each robot has a unique identifier — an integer from 1 to 10^9. At some moment, robots decided to play the game "Snowball". Below there are the rules of this game. First, all robots stand in a row. Then the first robot says his identifier. After that the second robot says the identifier of the first robot and then says his own identifier. Then the third robot says the identifier of the first robot, then says the identifier of the second robot and after that says his own. This process continues from left to right until the n-th robot says his identifier. Your task is to determine the k-th identifier to be pronounced. -----Input----- The first line contains two positive integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ min(2·10^9, n·(n + 1) / 2). The second line contains the sequence id_1, id_2, ..., id_{n} (1 ≤ id_{i} ≤ 10^9) — identifiers of roborts. It is guaranteed that all identifiers are different. -----Output----- Print the k-th pronounced identifier (assume that the numeration starts from 1). -----Examples----- Input 2 2 1 2 Output 1 Input 4 5 10 4 18 3 Output 4 -----Note----- In the first sample identifiers of robots will be pronounced in the following order: 1, 1, 2. As k = 2, the answer equals to 1. In the second test case identifiers of robots will be pronounced in the following order: 10, 10, 4, 10, 4, 18, 10, 4, 18, 3. As k = 5, the answer equals to 4. 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 = 1 while k > 0: k = k - i i += 1 k = k + i - 1 print(L[k-1]) ```
vfc_14470
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/670/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2 2\n1 2\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 5\n10 4 18 3\n", "output": "4\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1180
Solve the following coding problem using the programming language python: Vasya is sitting on an extremely boring math class. To have fun, he took a piece of paper and wrote out n numbers on a single line. After that, Vasya began to write out different ways to put pluses ("+") in the line between certain digits in the line so that the result was a correct arithmetic expression; formally, no two pluses in such a partition can stand together (between any two adjacent pluses there must be at least one digit), and no plus can stand at the beginning or the end of a line. For example, in the string 100500, ways 100500 (add no pluses), 1+00+500 or 10050+0 are correct, and ways 100++500, +1+0+0+5+0+0 or 100500+ are incorrect. The lesson was long, and Vasya has written all the correct ways to place exactly k pluses in a string of digits. At this point, he got caught having fun by a teacher and he was given the task to calculate the sum of all the resulting arithmetic expressions by the end of the lesson (when calculating the value of an expression the leading zeros should be ignored). As the answer can be large, Vasya is allowed to get only its remainder modulo 10^9 + 7. Help him! -----Input----- The first line contains two integers, n and k (0 ≤ k < n ≤ 10^5). The second line contains a string consisting of n digits. -----Output----- Print the answer to the problem modulo 10^9 + 7. -----Examples----- Input 3 1 108 Output 27 Input 3 2 108 Output 9 -----Note----- In the first sample the result equals (1 + 08) + (10 + 8) = 27. In the second sample the result equals 1 + 0 + 8 = 9. 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()) t = list(map(int, input())) p, d = 1, 10 ** 9 + 7 s, f = 0, [1] * n for i in range(2, n): f[i] = (i * f[i - 1]) % d c = lambda a, b: 0 if a > b else (f[b] * pow(f[a] * f[b - a], d - 2, d)) % d if k: u = [0] * (n + 1) p = [1] * (n + 1) for i in range(n): u[i] = (p[i] * c(k - 1, n - 2 - i) + u[i - 1]) % d p[i + 1] = (10 * p[i]) % d for i in range(n): v = u[n - 2 - i] + p[n - 1 - i] * c(k, i) s = (s + t[i] * v) % d else: for i in t: s = (s * 10 + i) % d print(s) ```
vfc_14474
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/520/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 1\n108\n", "output": "27", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 2\n108\n", "output": "9", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 0\n5\n", "output": "5", "type": "stdin_stdout" } ] }
apps
verifiable_code
1181
Solve the following coding problem using the programming language python: Ryouko is an extremely forgetful girl, she could even forget something that has just happened. So in order to remember, she takes a notebook with her, called Ryouko's Memory Note. She writes what she sees and what she hears on the notebook, and the notebook became her memory. Though Ryouko is forgetful, she is also born with superb analyzing abilities. However, analyzing depends greatly on gathered information, in other words, memory. So she has to shuffle through her notebook whenever she needs to analyze, which is tough work. Ryouko's notebook consists of n pages, numbered from 1 to n. To make life (and this problem) easier, we consider that to turn from page x to page y, |x - y| pages should be turned. During analyzing, Ryouko needs m pieces of information, the i-th piece of information is on page a_{i}. Information must be read from the notebook in order, so the total number of pages that Ryouko needs to turn is $\sum_{i = 1}^{m - 1}|a_{i + 1} - a_{i}|$. Ryouko wants to decrease the number of pages that need to be turned. In order to achieve this, she can merge two pages of her notebook. If Ryouko merges page x to page y, she would copy all the information on page x to y (1 ≤ x, y ≤ n), and consequently, all elements in sequence a that was x would become y. Note that x can be equal to y, in which case no changes take place. Please tell Ryouko the minimum number of pages that she needs to turn. Note she can apply the described operation at most once before the reading. Note that the answer can exceed 32-bit integers. -----Input----- The first line of input contains two integers n and m (1 ≤ n, m ≤ 10^5). The next line contains m integers separated by spaces: a_1, a_2, ..., a_{m} (1 ≤ a_{i} ≤ n). -----Output----- Print a single integer — the minimum number of pages Ryouko needs to turn. -----Examples----- Input 4 6 1 2 3 4 3 2 Output 3 Input 10 5 9 4 3 8 8 Output 6 -----Note----- In the first sample, the optimal solution is to merge page 4 to 3, after merging sequence a becomes {1, 2, 3, 3, 3, 2}, so the number of pages Ryouko needs to turn is |1 - 2| + |2 - 3| + |3 - 3| + |3 - 3| + |3 - 2| = 3. In the second sample, optimal solution is achieved by merging page 9 to 4. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def median(a): if len(a) == 0: return 0 if len(a) % 2 == 1: return a[len(a) // 2] else: return (a[len(a) // 2] + a[(len(a) // 2) - 1]) // 2 def profit(a, old_val): a.sort() med = median(a) sum_old = 0 sum_new = 0 for i in a: sum_old += abs(i - old_val) sum_new += abs(i - med) return sum_old - sum_new n, m = [int(c) for c in input().split()] pages = [int(c) for c in input().split()] count = {pages[0]: []} current_page_switches = 0 for i in range(1, len(pages)): cur_i = pages[i] prev_i = pages[i - 1] if not(cur_i in count): count[cur_i] = [] if cur_i != prev_i: count[cur_i].append(prev_i) count[prev_i].append(cur_i) current_page_switches += abs(cur_i - prev_i) max_profit = 0 for i in count: if len(count[i]) > 0: tmp = profit(count[i], i) if tmp > max_profit: max_profit = tmp print(current_page_switches - max_profit) ```
vfc_14478
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/433/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4 6\n1 2 3 4 3 2\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 5\n9 4 3 8 8\n", "output": "6\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 10\n2 5 2 2 3 5 3 2 1 3\n", "output": "7\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1182
Solve the following coding problem using the programming language python: Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take. Two pictures are considered to be different if the coordinates of corresponding rectangles are different. -----Input----- The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 10, 1 ≤ k ≤ n) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively. The next n lines each contain two integers x_{i} and y_{i} (1 ≤ x_{i} ≤ r, 1 ≤ y_{i} ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input. -----Output----- Print a single integer — the number of photographs Paul can take which include at least k violas. -----Examples----- Input 2 2 1 1 1 2 Output 4 Input 3 2 3 3 1 1 3 1 2 2 Output 1 Input 3 2 3 2 1 1 3 1 2 2 Output 4 -----Note----- We will use '*' to denote violinists and '#' to denote violists. In the first sample, the orchestra looks as follows *# ** Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total. In the second sample, the orchestra looks as follows #* *# #* Paul must take a photograph of the entire section. In the third sample, the orchestra looks the same as in the second sample. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python dhuang=0 a,b,c,d=list(map(int,input().split(' '))) huang = [['*']*b for _ in range(a)] for i in range(c): x,y=list(map(int,input().split(' '))) huang[x-1][y-1] = '#' for i in range(a): for j in range(b): for k in range(i, a): for l in range(j, b): ct=0 for m in range(i, k+1): for n in range(j, l+1): if huang[m][n]=='#': ct+=1 if ct>=d: dhuang+=1 print(dhuang) ```
vfc_14482
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/635/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2 2 1 1\n1 2\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 2 3 3\n1 1\n3 1\n2 2\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 2 3 2\n1 1\n3 1\n2 2\n", "output": "4\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1184
Solve the following coding problem using the programming language python: Recently, Anton has found a set. The set consists of small English letters. Anton carefully wrote out all the letters from the set in one line, separated by a comma. He also added an opening curved bracket at the beginning of the line and a closing curved bracket at the end of the line. Unfortunately, from time to time Anton would forget writing some letter and write it again. He asks you to count the total number of distinct letters in his set. -----Input----- The first and the single line contains the set of letters. The length of the line doesn't exceed 1000. It is guaranteed that the line starts from an opening curved bracket and ends with a closing curved bracket. Between them, small English letters are listed, separated by a comma. Each comma is followed by a space. -----Output----- Print a single number — the number of distinct letters in Anton's set. -----Examples----- Input {a, b, c} Output 3 Input {b, a, b, a} Output 2 Input {} Output 0 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python s = input() s = s[1: -1].replace(',', '') result = set(s.split()) print(len(result)) ```
vfc_14490
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/443/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "{a, b, c}\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "{b, a, b, a}\n", "output": "2\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1185
Solve the following coding problem using the programming language python: The new ITone 6 has been released recently and George got really keen to buy it. Unfortunately, he didn't have enough money, so George was going to work as a programmer. Now he faced the following problem at the work. Given a sequence of n integers p_1, p_2, ..., p_{n}. You are to choose k pairs of integers: [l_1, r_1], [l_2, r_2], ..., [l_{k}, r_{k}] (1 ≤ l_1 ≤ r_1 < l_2 ≤ r_2 < ... < l_{k} ≤ r_{k} ≤ n; r_{i} - l_{i} + 1 = m), in such a way that the value of sum $\sum_{i = 1}^{k} \sum_{j = l_{i}}^{r_{i}} p_{j}$ is maximal possible. Help George to cope with the task. -----Input----- The first line contains three integers n, m and k (1 ≤ (m × k) ≤ n ≤ 5000). The second line contains n integers p_1, p_2, ..., p_{n} (0 ≤ p_{i} ≤ 10^9). -----Output----- Print an integer in a single line — the maximum possible value of sum. -----Examples----- Input 5 2 1 1 2 3 4 5 Output 9 Input 7 1 3 2 10 7 18 5 33 0 Output 61 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 accumulate def main(): n, m, k = list(map(int, input().split())) a = list(map(int, input().split())) km = k * m if m == 1: a.sort(reverse=True) print(sum(a[:k])) return a = list(accumulate(a)) a.append(0) if n == km: print(a[n-1]) return d = [[0] * (n+1) for _ in range(k+1)] for i in range(m - 1, n): _k = (i + 1) // m if i < km else k for j in range(1, _k + 1): if i == j*m-1: d[j][i] = a[i] else: d[j][i] = max(d[j][i-1], a[i] - a[i-m] + d[j-1][i-m]) print(d[k][n-1]) def __starting_point(): main() __starting_point() ```
vfc_14494
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/467/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5 2 1\n1 2 3 4 5\n", "output": "9\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7 1 3\n2 10 7 18 5 33 0\n", "output": "61\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "13 8 1\n73 7 47 91 54 74 99 11 67 35 84 18 19\n", "output": "515\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "8 3 1\n8 46 37 81 81 57 11 2\n", "output": "219\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "20 5 3\n96 46 67 36 59 95 88 43 92 58 1 31 69 35 36 77 56 27 3 23\n", "output": "953\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1186
Solve the following coding problem using the programming language python: Given an integer N, find two permutations: Permutation p of numbers from 1 to N such that p_{i} ≠ i and p_{i} & i = 0 for all i = 1, 2, ..., N. Permutation q of numbers from 1 to N such that q_{i} ≠ i and q_{i} & i ≠ 0 for all i = 1, 2, ..., N. & is the bitwise AND operation. -----Input----- The input consists of one line containing a single integer N (1 ≤ N ≤ 10^5). -----Output----- For each subtask, if the required permutation doesn't exist, output a single line containing the word "NO"; otherwise output the word "YES" in the first line and N elements of the permutation, separated by spaces, in the second line. If there are several possible permutations in a subtask, output any of them. -----Examples----- Input 3 Output NO NO Input 6 Output YES 6 5 4 3 2 1 YES 3 6 2 5 1 4 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def test(x, i): i = list(i) ok = True for j in range(x): if (i[j] == j+1 or (i[j]&(j+1) != 0)): ok = False if ok: print(i) def comp(n): return 2**len(bin(n)[2:])-1-n n = int(input()) nn = n if (n%2 == 0): x = [] while (n != 0): #add n to comp(n) to the front of x for i in range(comp(n), n+1): x.append(i) n = comp(n)-1 x.reverse() print("YES") print(' '.join([str(i) for i in x])) else: print("NO") pow2 = [2**i for i in range(20)] def make(n): if n <= 5: return [] if n == 6: return [3, 6, 1, 5, 4, 2] if n == 7: return [3, 6, 1, 5, 4, 7, 2] if n in pow2: return [] shift = 2**(len(bin(n)[2:])-1) array = [i for i in range(shift, n+1)] array = array[1:] + [array[0]] return make(shift-1) + array n = nn k = make(n) if k == []: print("NO") else: print("YES") print(' '.join([str(i) for i in k])) ```
vfc_14498
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/909/F", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n", "output": "NO\nNO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n", "output": "YES\n6 5 4 3 2 1 \nYES\n3 6 2 5 1 4\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1188
Solve the following coding problem using the programming language python: It can be shown that any positive integer x can be uniquely represented as x = 1 + 2 + 4 + ... + 2^{k} - 1 + r, where k and r are integers, k ≥ 0, 0 < r ≤ 2^{k}. Let's call that representation prairie partition of x. For example, the prairie partitions of 12, 17, 7 and 1 are: 12 = 1 + 2 + 4 + 5, 17 = 1 + 2 + 4 + 8 + 2, 7 = 1 + 2 + 4, 1 = 1. Alice took a sequence of positive integers (possibly with repeating elements), replaced every element with the sequence of summands in its prairie partition, arranged the resulting numbers in non-decreasing order and gave them to Borys. Now Borys wonders how many elements Alice's original sequence could contain. Find all possible options! -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of numbers given from Alice to Borys. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^12; a_1 ≤ a_2 ≤ ... ≤ a_{n}) — the numbers given from Alice to Borys. -----Output----- Output, in increasing order, all possible values of m such that there exists a sequence of positive integers of length m such that if you replace every element with the summands in its prairie partition and arrange the resulting numbers in non-decreasing order, you will get the sequence given in the input. If there are no such values of m, output a single integer -1. -----Examples----- Input 8 1 1 2 2 3 4 5 8 Output 2 Input 6 1 1 1 2 2 2 Output 2 3 Input 5 1 2 4 4 4 Output -1 -----Note----- In the first example, Alice could get the input sequence from [6, 20] as the original sequence. In the second example, Alice's original sequence could be either [4, 5] or [3, 3, 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 = sorted(list(map(int, input().split()))) maxe = max(a) cnt = [] cur, k, i = 1, 0, 0 while i < n: cnt.append(0) while i < n and a[i] < cur: cnt[2 * k] += 1 i += 1 cnt.append(0) while i < n and a[i] == cur: cnt[2 * k + 1] += 1 i += 1 k += 1 cur *= 2 cnt.append(0) cnt.append(0) maxe = len(cnt) - 1 maxk = cnt[1] was = False for l in range(maxk): cur = 1 while cnt[cur] > 0: cnt[cur] -= 1 cur += 2 cnt[cur] -= 1 cursum = 0 ok = True for t in range(maxe, 0, -1): cursum += cnt[t] if cursum > 0: ok = False break if ok: print(l + 1, end=" ") was = True if not was: print(-1) # Made By Mostafa_Khaled ```
vfc_14506
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/773/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "8\n1 1 2 2 3 4 5 8\n", "output": "2 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n1 1 1 2 2 2\n", "output": "2 3 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n1 2 4 4 4\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "20\n1 1 1 1 2 2 2 2 4 4 4 4 8 8 8 8 8 10 10 11\n", "output": "4 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "20\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2\n", "output": "9 10 11 12 13 14 15 16 17 \n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1189
Solve the following coding problem using the programming language python: Heidi the Cow is aghast: cracks in the northern Wall? Zombies gathering outside, forming groups, preparing their assault? This must not happen! Quickly, she fetches her HC^2 (Handbook of Crazy Constructions) and looks for the right chapter: How to build a wall: Take a set of bricks. Select one of the possible wall designs. Computing the number of possible designs is left as an exercise to the reader. Place bricks on top of each other, according to the chosen design. This seems easy enough. But Heidi is a Coding Cow, not a Constructing Cow. Her mind keeps coming back to point 2b. Despite the imminent danger of a zombie onslaught, she wonders just how many possible walls she could build with up to n bricks. A wall is a set of wall segments as defined in the easy version. How many different walls can be constructed such that the wall consists of at least 1 and at most n bricks? Two walls are different if there exist a column c and a row r such that one wall has a brick in this spot, and the other does not. Along with n, you will be given C, the width of the wall (as defined in the easy version). Return the number of different walls modulo 10^6 + 3. -----Input----- The first line contains two space-separated integers n and C, 1 ≤ n ≤ 500000, 1 ≤ C ≤ 200000. -----Output----- Print the number of different walls that Heidi could build, modulo 10^6 + 3. -----Examples----- Input 5 1 Output 5 Input 2 2 Output 5 Input 3 2 Output 9 Input 11 5 Output 4367 Input 37 63 Output 230574 -----Note----- The number 10^6 + 3 is prime. In the second sample case, the five walls are: B B B., .B, BB, B., and .B In the third sample case, the nine walls are the five as in the second sample case and in addition the following four: B B B B B B B., .B, BB, and BB 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 operator as op from functools import reduce from operator import mul # or mul=lambda x,y:x*y from fractions import Fraction def nCk(n,k): return int( reduce(mul, (Fraction(n-i, i+1) for i in range(k)), 1) ) def ncr(n, r): r = min(r, n-r) if r == 0: return 1 numer = reduce(op.mul, list(range(n, n-r, -1))) denom = reduce(op.mul, list(range(1, r+1))) return numer//denom def modPow(a, x, p): #calculates a^x mod p in logarithmic time. res = 1 while(x > 0): if( x % 2 != 0): res = (res * a) % p a = (a * a) % p x = int(x/2) return res def modInverse(a, p): #calculates the modular multiplicative of a mod m. #(assuming p is prime). return modPow(a, p-2, p) def modBinomial(n, k, p): #calculates C(n,k) mod p (assuming p is prime). # n * (n-1) * ... * (n-k+1) numerator = 1 for i in range(k): numerator = (numerator * (n-i) ) % p denominator = 1 for i in range(1, k+1): denominator = (denominator * i) % p # numerator / denominator mod p. return ( numerator* modInverse(denominator,p) ) % p n, c = input().split() n = int(n) c = int(c) #test = [0 for x in range (n+1)] #test[1] = c #for i in range(2, n+1): # test[i] = (test[i-1] + modBinomial((i+c-1),i, 1000003))%1000003 #ans = solve(n, c) #ans =test[n] ans = modBinomial((c+n),c,1000003) - 1 print(int(ans)) ```
vfc_14510
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/690/D2", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5 1\n", "output": "5\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1190
Solve the following coding problem using the programming language python: In order to make the "Sea Battle" game more interesting, Boris decided to add a new ship type to it. The ship consists of two rectangles. The first rectangle has a width of $w_1$ and a height of $h_1$, while the second rectangle has a width of $w_2$ and a height of $h_2$, where $w_1 \ge w_2$. In this game, exactly one ship is used, made up of two rectangles. There are no other ships on the field. The rectangles are placed on field in the following way: the second rectangle is on top the first rectangle; they are aligned to the left, i.e. their left sides are on the same line; the rectangles are adjacent to each other without a gap. See the pictures in the notes: the first rectangle is colored red, the second rectangle is colored blue. Formally, let's introduce a coordinate system. Then, the leftmost bottom cell of the first rectangle has coordinates $(1, 1)$, the rightmost top cell of the first rectangle has coordinates $(w_1, h_1)$, the leftmost bottom cell of the second rectangle has coordinates $(1, h_1 + 1)$ and the rightmost top cell of the second rectangle has coordinates $(w_2, h_1 + h_2)$. After the ship is completely destroyed, all cells neighboring by side or a corner with the ship are marked. Of course, only cells, which don't belong to the ship are marked. On the pictures in the notes such cells are colored green. Find out how many cells should be marked after the ship is destroyed. The field of the game is infinite in any direction. -----Input----- Four lines contain integers $w_1, h_1, w_2$ and $h_2$ ($1 \leq w_1, h_1, w_2, h_2 \leq 10^8$, $w_1 \ge w_2$) — the width of the first rectangle, the height of the first rectangle, the width of the second rectangle and the height of the second rectangle. You can't rotate the rectangles. -----Output----- Print exactly one integer — the number of cells, which should be marked after the ship is destroyed. -----Examples----- Input 2 1 2 1 Output 12 Input 2 2 1 2 Output 16 -----Note----- In the first example the field looks as follows (the first rectangle is red, the second rectangle is blue, green shows the marked squares): [Image] In the second example the field looks as: [Image] The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python w1, h1, w2, h2 = list(map(int, input().split())) print(2 * (h1 + h2) + w1 + w2 + abs(w1 - w2) + 4) ```
vfc_14514
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1131/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2 1 2 1\n", "output": "12\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 2 1 2\n", "output": "16\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1 1 1\n", "output": "10\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1191
Solve the following coding problem using the programming language python: Unlike Knights of a Round Table, Knights of a Polygonal Table deprived of nobility and happy to kill each other. But each knight has some power and a knight can kill another knight if and only if his power is greater than the power of victim. However, even such a knight will torment his conscience, so he can kill no more than $k$ other knights. Also, each knight has some number of coins. After a kill, a knight can pick up all victim's coins. Now each knight ponders: how many coins he can have if only he kills other knights? You should answer this question for each knight. -----Input----- The first line contains two integers $n$ and $k$ $(1 \le n \le 10^5, 0 \le k \le \min(n-1,10))$ — the number of knights and the number $k$ from the statement. The second line contains $n$ integers $p_1, p_2 ,\ldots,p_n$ $(1 \le p_i \le 10^9)$ — powers of the knights. All $p_i$ are distinct. The third line contains $n$ integers $c_1, c_2 ,\ldots,c_n$ $(0 \le c_i \le 10^9)$ — the number of coins each knight has. -----Output----- Print $n$ integers — the maximum number of coins each knight can have it only he kills other knights. -----Examples----- Input 4 2 4 5 9 7 1 2 11 33 Output 1 3 46 36 Input 5 1 1 2 3 4 5 1 2 3 4 5 Output 1 3 5 7 9 Input 1 0 2 3 Output 3 -----Note----- Consider the first example. The first knight is the weakest, so he can't kill anyone. That leaves him with the only coin he initially has. The second knight can kill the first knight and add his coin to his own two. The third knight is the strongest, but he can't kill more than $k = 2$ other knights. It is optimal to kill the second and the fourth knights: $2+11+33 = 46$. The fourth knight should kill the first and the second knights: $33+1+2 = 36$. In the second example the first knight can't kill anyone, while all the others should kill the one with the index less by one than their own. In the third example there is only one knight, so he can't kill anyone. 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()) class Knight: def __init__(self, andis, p, c): self.p = int(p) self.c = int(c) self.andis = int(andis) self.ans = self.c p = list(map(int, input().split())) c = list(map(int, input().split())) x = [] for i in range(n): x.append(Knight(i, p[i], c[i])) x.sort(key=lambda x: x.p) coins = [] for i in range(n-1): if len(coins) < m: coins.append(x[i].c) coins.sort() elif len(coins) > 0: if coins[0] < x[i].c: coins[0] = x[i].c coins.sort() x[i+1].ans += sum(coins) x.sort(key=lambda x:x.andis) for k in x: print(k.ans, end=' ') ```
vfc_14518
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/994/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4 2\n4 5 9 7\n1 2 11 33\n", "output": "1 3 46 36 ", "type": "stdin_stdout" } ] }
apps
verifiable_code
1192
Solve the following coding problem using the programming language python: You are given a permutation of n numbers p_1, p_2, ..., p_{n}. We perform k operations of the following type: choose uniformly at random two indices l and r (l ≤ r) and reverse the order of the elements p_{l}, p_{l} + 1, ..., p_{r}. Your task is to find the expected value of the number of inversions in the resulting permutation. -----Input----- The first line of input contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10^9). The next line contains n integers p_1, p_2, ..., p_{n} — the given permutation. All p_{i} are different and in range from 1 to n. The problem consists of three subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. In subproblem G1 (3 points), the constraints 1 ≤ n ≤ 6, 1 ≤ k ≤ 4 will hold. In subproblem G2 (5 points), the constraints 1 ≤ n ≤ 30, 1 ≤ k ≤ 200 will hold. In subproblem G3 (16 points), the constraints 1 ≤ n ≤ 100, 1 ≤ k ≤ 10^9 will hold. -----Output----- Output the answer with absolute or relative error no more than 1e - 9. -----Examples----- Input 3 1 1 2 3 Output 0.833333333333333 Input 3 4 1 3 2 Output 1.458333333333334 -----Note----- Consider the first sample test. We will randomly pick an interval of the permutation (1, 2, 3) (which has no inversions) and reverse the order of its elements. With probability $\frac{1}{2}$, the interval will consist of a single element and the permutation will not be altered. With probability $\frac{1}{6}$ we will inverse the first two elements' order and obtain the permutation (2, 1, 3) which has one inversion. With the same probability we might pick the interval consisting of the last two elements which will lead to the permutation (1, 3, 2) with one inversion. Finally, with probability $\frac{1}{6}$ the randomly picked interval will contain all elements, leading to the permutation (3, 2, 1) with 3 inversions. Hence, the expected number of inversions is equal to $\frac{1}{2} \cdot 0 + \frac{1}{6} \cdot 1 + \frac{1}{6} \cdot 1 + \frac{1}{6} \cdot 3 = \frac{5}{6}$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from sys import stdin, stdout from math import * from itertools import * from copy import * s = 0 invs = 0 def calc_invertions(a): s = 0 for i in range(len(a)): for j in range(i + 1, len(a)): s += 1 if a[i] > a[j] else 0 return s def do_flips(arr, num): nonlocal s, invs if num == 0: invs += 1 s += calc_invertions(arr) else: for i in range(len(arr)): for j in range(i, len(arr)): for k in range((j - i + 1) // 2): arr[i + k], arr[j - k] = arr[j - k], arr[i + k] do_flips(arr, num - 1) for k in range((j - i + 1) // 2): arr[i + k], arr[j - k] = arr[j - k], arr[i + k] def solve(test): ints = list(map(int, test.strip().split())) n, m = ints[:2] arr = ints[2:] do_flips(arr, m) return s / invs stdout.write(str(solve(stdin.read()))) ```
vfc_14522
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/513/G1", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 1\n1 2 3\n", "output": "0.833333333333333\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 4\n1 3 2\n", "output": "1.458333333333334\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6 1\n4 2 5 1 3 6\n", "output": "6.380952380952381\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1193
Solve the following coding problem using the programming language python: The R1 company wants to hold a web search championship. There were n computers given for the competition, each of them is connected to the Internet. The organizers believe that the data transfer speed directly affects the result. The higher the speed of the Internet is, the faster the participant will find the necessary information. Therefore, before the competition started, each computer had its maximum possible data transfer speed measured. On the i-th computer it was a_{i} kilobits per second. There will be k participants competing in the championship, each should get a separate computer. The organizing company does not want any of the participants to have an advantage over the others, so they want to provide the same data transfer speed to each participant's computer. Also, the organizers want to create the most comfortable conditions for the participants, so the data transfer speed on the participants' computers should be as large as possible. The network settings of the R1 company has a special option that lets you to cut the initial maximum data transfer speed of any computer to any lower speed. How should the R1 company configure the network using the described option so that at least k of n computers had the same data transfer speed and the data transfer speed on these computers was as large as possible? -----Input----- The first line contains two space-separated integers n and k (1 ≤ k ≤ n ≤ 100) — the number of computers and the number of participants, respectively. In the second line you have a space-separated sequence consisting of n integers: a_1, a_2, ..., a_{n} (16 ≤ a_{i} ≤ 32768); number a_{i} denotes the maximum data transfer speed on the i-th computer. -----Output----- Print a single integer — the maximum Internet speed value. It is guaranteed that the answer to the problem is always an integer. -----Examples----- Input 3 2 40 20 30 Output 30 Input 6 4 100 20 40 20 50 50 Output 40 -----Note----- In the first test case the organizers can cut the first computer's speed to 30 kilobits. Then two computers (the first and the third one) will have the same speed of 30 kilobits. They should be used as the participants' computers. This answer is optimal. 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()) s = sorted(list(map(int, input().split())), reverse = True) print(s[k - 1]) ```
vfc_14526
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/412/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 2\n40 20 30\n", "output": "30\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6 4\n100 20 40 20 50 50\n", "output": "40\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1\n16\n", "output": "16\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 1\n10000 17\n", "output": "10000\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 2\n200 300\n", "output": "200\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1194
Solve the following coding problem using the programming language python: Let's define the sum of two permutations p and q of numbers 0, 1, ..., (n - 1) as permutation [Image], where Perm(x) is the x-th lexicographically permutation of numbers 0, 1, ..., (n - 1) (counting from zero), and Ord(p) is the number of permutation p in the lexicographical order. For example, Perm(0) = (0, 1, ..., n - 2, n - 1), Perm(n! - 1) = (n - 1, n - 2, ..., 1, 0) Misha has two permutations, p and q. Your task is to find their sum. Permutation a = (a_0, a_1, ..., a_{n} - 1) is called to be lexicographically smaller than permutation b = (b_0, b_1, ..., b_{n} - 1), if for some k following conditions hold: a_0 = b_0, a_1 = b_1, ..., a_{k} - 1 = b_{k} - 1, a_{k} < b_{k}. -----Input----- The first line contains an integer n (1 ≤ n ≤ 200 000). The second line contains n distinct integers from 0 to n - 1, separated by a space, forming permutation p. The third line contains n distinct integers from 0 to n - 1, separated by spaces, forming permutation q. -----Output----- Print n distinct integers from 0 to n - 1, forming the sum of the given permutations. Separate the numbers by spaces. -----Examples----- Input 2 0 1 0 1 Output 0 1 Input 2 0 1 1 0 Output 1 0 Input 3 1 2 0 2 1 0 Output 1 0 2 -----Note----- Permutations of numbers from 0 to 1 in the lexicographical order: (0, 1), (1, 0). In the first sample Ord(p) = 0 and Ord(q) = 0, so the answer is $\operatorname{Perm}((0 + 0) \operatorname{mod} 2) = \operatorname{Perm}(0) =(0,1)$. In the second sample Ord(p) = 0 and Ord(q) = 1, so the answer is $\operatorname{Perm}((0 + 1) \operatorname{mod} 2) = \operatorname{Perm}(1) =(1,0)$. Permutations of numbers from 0 to 2 in the lexicographical order: (0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0). In the third sample Ord(p) = 3 and Ord(q) = 5, so the answer is $\operatorname{Perm}((3 + 5) \operatorname{mod} 6) = \operatorname{Perm}(2) =(1,0,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 class SegmTree(): def __init__(self, array=None): size = len(array) N = 1 while N < size: N <<= 1 self.N = N self.tree = [0] * (2*self.N) for i in range(size): self.tree[i+self.N] = array[i] self.build() def build(self): for i in range(self.N - 1, 0, -1): self.tree[i] = self.tree[i<<1] + self.tree[i<<1|1] def add(self, i, value=1): i += self.N while i > 0: self.tree[i] += value i >>= 1 def get_sum(self, l, r): N = self.N l += N r += N result = 0 while l < r: if l & 1: result += self.tree[l] l += 1 if r & 1: r -= 1 result += self.tree[r] l >>= 1 r >>= 1 return result def find_kth_nonzero(self, k): i = 1 if k < 1 or k > self.tree[1]: return -1 while i < self.N: i <<= 1 if self.tree[i] < k: k -= self.tree[i] i |= 1 return i - self.N reader = (line.rstrip() for line in sys.stdin) input = reader.__next__ n = int(input()) p = list(map(int, input().split())) q = list(map(int, input().split())) ord_p = [0] * n ord_q = [0] * n st = SegmTree([1] * n) for i, val in enumerate(p): ord_p[i] = st.get_sum(0, val) st.add(val, -1) st = SegmTree([1] * n) for i, val in enumerate(q): ord_q[i] = st.get_sum(0, val) st.add(val, -1) transfer = 0 for i in range(n-1, -1, -1): radix = n-i ord_p[i] = ord_p[i] + ord_q[i] + transfer if ord_p[i] < radix: transfer = 0 else: transfer = 1 ord_p[i] -= radix st = SegmTree([1] * n) for i in range(n): k = ord_p[i] + 1 ord_q[i] = st.find_kth_nonzero(k) st.add(ord_q[i], -1) print(*ord_q) ```
vfc_14530
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/501/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\n0 1\n0 1\n", "output": "0 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n0 1\n1 0\n", "output": "1 0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n1 2 0\n2 1 0\n", "output": "1 0 2\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1195
Solve the following coding problem using the programming language python: From "ftying rats" to urban saniwation workers - can synthetic biology tronsform how we think of pigeons? The upiquitous pigeon has long been viewed as vermin - spleading disease, scavenging through trush, and defecating in populous urban spases. Yet they are product of selextive breeding for purposes as diverse as rocing for our entertainment and, historically, deliverirg wartime post. Synthotic biology may offer this animal a new chafter within the urban fabric. Piteon d'Or recognihes how these birds ripresent a potentially userul interface for urdan biotechnologies. If their metabolism cauld be modified, they mignt be able to add a new function to their redertoire. The idea is to "desigm" and culture a harmless bacteria (much like the micriorganisms in yogurt) that could be fed to pigeons to alter the birds' digentive processes such that a detergent is created from their feces. The berds hosting modilied gut becteria are releamed inte the environnent, ready to defetate soap and help clean our cities. -----Input----- The first line of input data contains a single integer $n$ ($5 \le n \le 10$). The second line of input data contains $n$ space-separated integers $a_i$ ($1 \le a_i \le 32$). -----Output----- Output a single integer. -----Example----- Input 5 1 2 3 4 5 Output 4 -----Note----- We did not proofread this statement at 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()) l = list(map(int, input().split())) f1 = l[2] l.sort() f2 = l[0] print(2 + (f1 ^ f2)) ```
vfc_14534
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1145/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n1 2 3 4 5\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n6 12 3 15 9 18\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n28 4 13 29 17 8\n", "output": "11\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "9\n23 1 2 26 9 11 23 10 26\n", "output": "5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7\n18 29 23 23 1 14 5\n", "output": "24\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n22 19 19 16 14\n", "output": "31\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1196
Solve the following coding problem using the programming language python: Each employee of the "Blake Techologies" company uses a special messaging app "Blake Messenger". All the stuff likes this app and uses it constantly. However, some important futures are missing. For example, many users want to be able to search through the message history. It was already announced that the new feature will appear in the nearest update, when developers faced some troubles that only you may help them to solve. All the messages are represented as a strings consisting of only lowercase English letters. In order to reduce the network load strings are represented in the special compressed form. Compression algorithm works as follows: string is represented as a concatenation of n blocks, each block containing only equal characters. One block may be described as a pair (l_{i}, c_{i}), where l_{i} is the length of the i-th block and c_{i} is the corresponding letter. Thus, the string s may be written as the sequence of pairs $\langle(l_{1}, c_{1}),(l_{2}, c_{2}), \ldots,(l_{n}, c_{n}) \rangle$. Your task is to write the program, that given two compressed string t and s finds all occurrences of s in t. Developers know that there may be many such occurrences, so they only ask you to find the number of them. Note that p is the starting position of some occurrence of s in t if and only if t_{p}t_{p} + 1...t_{p} + |s| - 1 = s, where t_{i} is the i-th character of string t. Note that the way to represent the string in compressed form may not be unique. For example string "aaaa" may be given as $\langle(4, a) \rangle$, $\langle(3, a),(1, a) \rangle$, $\langle(2, a),(2, a) \rangle$... -----Input----- The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of blocks in the strings t and s, respectively. The second line contains the descriptions of n parts of string t in the format "l_{i}-c_{i}" (1 ≤ l_{i} ≤ 1 000 000) — the length of the i-th part and the corresponding lowercase English letter. The second line contains the descriptions of m parts of string s in the format "l_{i}-c_{i}" (1 ≤ l_{i} ≤ 1 000 000) — the length of the i-th part and the corresponding lowercase English letter. -----Output----- Print a single integer — the number of occurrences of s in t. -----Examples----- Input 5 3 3-a 2-b 4-c 3-a 2-c 2-a 2-b 1-c Output 1 Input 6 1 3-a 6-b 7-a 4-c 8-e 2-a 3-a Output 6 Input 5 5 1-h 1-e 1-l 1-l 1-o 1-w 1-o 1-r 1-l 1-d Output 0 -----Note----- In the first sample, t = "aaabbccccaaacc", and string s = "aabbc". The only occurrence of string s in string t starts at position p = 2. In the second sample, t = "aaabbbbbbaaaaaaacccceeeeeeeeaa", and s = "aaa". The occurrences of s in t start at positions p = 1, p = 10, p = 11, p = 12, p = 13 and p = 14. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def ziped(a): p = [] for i in a: x = int(i.split('-')[0]) y = i.split('-')[1] if len(p) > 0 and p[-1][1] == y: p[-1][0] += x else: p.append([x, y]) return p def solve(a, b , c): ans = 0 if len(b) == 1: for token in a: if c(token, b[0]): ans += token[0] - b[0][0] + 1 return ans if len(b) == 2: for i in range(len(a) - 1): if c(a[i], b[0]) and c(a[i + 1], b[-1]): ans += 1 return ans v = b[1 : -1] + [[100500, '#']] + a p = [0] * len(v) for i in range(1, len(v)): j = p[i - 1] while j > 0 and v[i] != v[j]: j = p[j - 1] if v[i] == v[j]: j += 1 p[i] = j for i in range(len(v) - 1): if p[i] == len(b) - 2 and c(v[i - p[i]], b[0]) and c(v[i + 1], b[-1]): ans += 1 return ans n, m = list(map(int, input().split())) a = ziped(input().split()) b = ziped(input().split()) print(solve(a, b, lambda x, y: x[1] == y[1] and x[0] >= y[0])) ```
vfc_14538
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/631/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5 3\n3-a 2-b 4-c 3-a 2-c\n2-a 2-b 1-c\n", "output": "1", "type": "stdin_stdout" }, { "fn_name": null, "input": "6 1\n3-a 6-b 7-a 4-c 8-e 2-a\n3-a\n", "output": "6", "type": "stdin_stdout" } ] }
apps
verifiable_code
1197
Solve the following coding problem using the programming language python: Game "Minesweeper 1D" is played on a line of squares, the line's height is 1 square, the line's width is n squares. Some of the squares contain bombs. If a square doesn't contain a bomb, then it contains a number from 0 to 2 — the total number of bombs in adjacent squares. For example, the correct field to play looks like that: 001*2***101*. The cells that are marked with "*" contain bombs. Note that on the correct field the numbers represent the number of bombs in adjacent cells. For example, field 2* is not correct, because cell with value 2 must have two adjacent cells with bombs. Valera wants to make a correct field to play "Minesweeper 1D". He has already painted a squared field with width of n cells, put several bombs on the field and wrote numbers into some cells. Now he wonders how many ways to fill the remaining cells with bombs and numbers are there if we should get a correct field in the end. -----Input----- The first line contains sequence of characters without spaces s_1s_2... s_{n} (1 ≤ n ≤ 10^6), containing only characters "*", "?" and digits "0", "1" or "2". If character s_{i} equals "*", then the i-th cell of the field contains a bomb. If character s_{i} equals "?", then Valera hasn't yet decided what to put in the i-th cell. Character s_{i}, that is equal to a digit, represents the digit written in the i-th square. -----Output----- Print a single integer — the number of ways Valera can fill the empty cells and get a correct field. As the answer can be rather large, print it modulo 1000000007 (10^9 + 7). -----Examples----- Input ?01??? Output 4 Input ? Output 2 Input **12 Output 0 Input 1 Output 0 -----Note----- In the first test sample you can get the following correct fields: 001**1, 001***, 001*2*, 001*10. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python Mod=1000000007 s=input() n=len(s) a,b,c,d=1,0,0,0 for i in range(0,n): if s[i]=='*': a,b,c,d=0,(a+b+d)%Mod,0,0 elif s[i]=='?': a,b,c,d=(a+b+c)%Mod,(a+b+d)%Mod,0,0 elif s[i]=='0': a,b,c,d=0,0,(a+c)%Mod,0 elif s[i]=='1': a,b,c,d=0,0,b,(a+c)%Mod else: a,b,c,d=0,0,0,(b+d)%Mod print((a+b+c)%Mod) ```
vfc_14542
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/404/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "?01???\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "?\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "**12\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1198
Solve the following coding problem using the programming language python: Since you are the best Wraith King, Nizhniy Magazin «Mir» at the centre of Vinnytsia is offering you a discount. You are given an array a of length n and an integer c. The value of some array b of length k is the sum of its elements except for the $\lfloor \frac{k}{c} \rfloor$ smallest. For example, the value of the array [3, 1, 6, 5, 2] with c = 2 is 3 + 6 + 5 = 14. Among all possible partitions of a into contiguous subarrays output the smallest possible sum of the values of these subarrays. -----Input----- The first line contains integers n and c (1 ≤ n, c ≤ 100 000). The second line contains n integers a_{i} (1 ≤ a_{i} ≤ 10^9) — elements of a. -----Output----- Output a single integer  — the smallest possible sum of values of these subarrays of some partition of a. -----Examples----- Input 3 5 1 2 3 Output 6 Input 12 10 1 1 10 10 10 10 10 10 9 10 10 10 Output 92 Input 7 2 2 3 6 4 5 7 1 Output 17 Input 8 4 1 3 4 5 5 3 4 1 Output 23 -----Note----- In the first example any partition yields 6 as the sum. In the second example one of the optimal partitions is [1, 1], [10, 10, 10, 10, 10, 10, 9, 10, 10, 10] with the values 2 and 90 respectively. In the third example one of the optimal partitions is [2, 3], [6, 4, 5, 7], [1] with the values 3, 13 and 1 respectively. In the fourth example one of the optimal partitions is [1], [3, 4, 5, 5, 3, 4], [1] with the values 1, 21 and 1 respectively. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python str=input().split() n=int(str[0]) len=int(str[1]) a=[] Q=[] F=[] for i in range(0,n+1): a.append(0) Q.append(0) F.append(0) sum=0 h=1 t=0 str=input().split() for i in range(1,n+1): a[i]=int(str[i-1]) sum+=a[i] #print (sum) while h<=t and Q[h]<=i-len: h=h+1 while h<=t and a[i]<=a[Q[t]]: t=t-1 t=t+1;Q[t]=i; if (i<len) : F[i]=0 else : F[i]=F[i-len]+a[Q[h]] F[i]=max(F[i],F[i-1]) print(sum-F[n]) ```
vfc_14546
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/940/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 5\n1 2 3\n", "output": "6\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "12 10\n1 1 10 10 10 10 10 10 9 10 10 10\n", "output": "92\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7 2\n2 3 6 4 5 7 1\n", "output": "17\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "8 4\n1 3 4 5 5 3 4 1\n", "output": "23\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1199
Solve the following coding problem using the programming language python: A Christmas party in city S. had n children. All children came in mittens. The mittens can be of different colors, but each child had the left and the right mitten of the same color. Let's say that the colors of the mittens are numbered with integers from 1 to m, and the children are numbered from 1 to n. Then the i-th child has both mittens of color c_{i}. The Party had Santa Claus ('Father Frost' in Russian), his granddaughter Snow Girl, the children danced around the richly decorated Christmas tree. In fact, everything was so bright and diverse that the children wanted to wear mittens of distinct colors. The children decided to swap the mittens so that each of them got one left and one right mitten in the end, and these two mittens were of distinct colors. All mittens are of the same size and fit all the children. The children started exchanging the mittens haphazardly, but they couldn't reach the situation when each child has a pair of mittens of distinct colors. Vasily Petrov, the dad of one of the children, noted that in the general case the children's idea may turn out impossible. Besides, he is a mathematician and he came up with such scheme of distributing mittens that the number of children that have distinct-colored mittens was maximum. You task is to repeat his discovery. Note that the left and right mittens are different: each child must end up with one left and one right mitten. -----Input----- The first line contains two integers n and m — the number of the children and the number of possible mitten colors (1 ≤ n ≤ 5000, 1 ≤ m ≤ 100). The second line contains n integers c_1, c_2, ... c_{n}, where c_{i} is the color of the mittens of the i-th child (1 ≤ c_{i} ≤ m). -----Output----- In the first line, print the maximum number of children who can end up with a distinct-colored pair of mittens. In the next n lines print the way the mittens can be distributed in this case. On the i-th of these lines print two space-separated integers: the color of the left and the color of the right mitten the i-th child will get. If there are multiple solutions, you can print any of them. -----Examples----- Input 6 3 1 3 2 2 1 1 Output 6 2 1 1 2 2 1 1 3 1 2 3 1 Input 4 2 1 2 1 1 Output 2 1 2 1 1 2 1 1 1 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python 3 def readln(): return tuple(map(int, input().split())) n, m = readln() cnt = [0] * (m + 1) for c in readln(): cnt[c] += 1 ans = [0] * n j = 0 for _ in range(1, m + 1): v = max(cnt) i = cnt.index(v) while cnt[i]: ans[j] = i cnt[i] -= 1 j += 2 if j >= n: j = 1 print(len([1 for i in range(n) if ans[i] != ans[(i + 1) % n]])) for i in range(n): print(ans[i], ans[(i + 1) % n]) ```
vfc_14550
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/370/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6 3\n1 3 2 2 1 1\n", "output": "6\n2 1\n1 2\n2 1\n1 3\n1 2\n3 1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1200
Solve the following coding problem using the programming language python: There are n points on a straight line, and the i-th point among them is located at x_{i}. All these coordinates are distinct. Determine the number m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. -----Input----- The first line contains a single integer n (3 ≤ n ≤ 100 000) — the number of points. The second line contains a sequence of integers x_1, x_2, ..., x_{n} ( - 10^9 ≤ x_{i} ≤ 10^9) — the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order. -----Output----- Print a single integer m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. -----Examples----- Input 3 -5 10 5 Output 1 Input 6 100 200 400 300 600 500 Output 0 Input 4 10 9 0 -1 Output 8 -----Note----- In the first example you can add one point with coordinate 0. In the second example the distances between all neighboring points are already equal, so you shouldn't add anything. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def gcd(a, b): if(a==0): return b return gcd(b%a, a) n=int(input()) v=list(map(int,input().split())) v.sort() ans=v[1]-v[0] for i in range(2, n): ans=gcd(ans, v[i]-v[i-1]) print((v[len(v)-1]-v[0])//ans+1-n) ```
vfc_14554
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/926/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n-5 10 5\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n100 200 400 300 600 500\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1201
Solve the following coding problem using the programming language python: Polycarp is in really serious trouble — his house is on fire! It's time to save the most valuable items. Polycarp estimated that it would take t_{i} seconds to save i-th item. In addition, for each item, he estimated the value of d_{i} — the moment after which the item i will be completely burned and will no longer be valuable for him at all. In particular, if t_{i} ≥ d_{i}, then i-th item cannot be saved. Given the values p_{i} for each of the items, find a set of items that Polycarp can save such that the total value of this items is maximum possible. Polycarp saves the items one after another. For example, if he takes item a first, and then item b, then the item a will be saved in t_{a} seconds, and the item b — in t_{a} + t_{b} seconds after fire started. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 100) — the number of items in Polycarp's house. Each of the following n lines contains three integers t_{i}, d_{i}, p_{i} (1 ≤ t_{i} ≤ 20, 1 ≤ d_{i} ≤ 2 000, 1 ≤ p_{i} ≤ 20) — the time needed to save the item i, the time after which the item i will burn completely and the value of item i. -----Output----- In the first line print the maximum possible total value of the set of saved items. In the second line print one integer m — the number of items in the desired set. In the third line print m distinct integers — numbers of the saved items in the order Polycarp saves them. Items are 1-indexed in the same order in which they appear in the input. If there are several answers, print any of them. -----Examples----- Input 3 3 7 4 2 6 5 3 7 6 Output 11 2 2 3 Input 2 5 6 1 3 3 5 Output 1 1 1 -----Note----- In the first example Polycarp will have time to save any two items, but in order to maximize the total value of the saved items, he must save the second and the third item. For example, he can firstly save the third item in 3 seconds, and then save the second item in another 2 seconds. Thus, the total value of the saved items will be 6 + 5 = 11. In the second example Polycarp can save only the first item, since even if he immediately starts saving the second item, he can save it in 3 seconds, but this item will already be completely burned by 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()) items = [] max_time = 0 for i in range(1,n+1): t,d,p = list(map(int,input().split())) max_time = max(max_time, d) items.append((t,d,p,i)) items.sort(key=lambda x: x[1]) dp = [[(0,[]) for _ in range(n+1)] for _ in range(max_time+1)] for time in range(1, max_time+1): for it in range(1, n+1): if time < items[it-1][0] or time >= items[it-1][1]: dp[time][it] = max(dp[time][it-1], dp[time-1][it]) else: pick = dp[time-items[it-1][0]][it-1][0] + items[it-1][2] if dp[time][it-1][0] > pick : dp[time][it] = max(dp[time][it-1], dp[time-1][it]) else: dp[time][it] = (dp[time-items[it-1][0]][it-1][0] + items[it-1][2], list(dp[time-items[it-1][0]][it-1][1])) dp[time][it][1].append(items[it-1][3]) #print(dp) res = max(dp[max_time]) print(res[0]) print(len(res[1])) print(*res[1]) ```
vfc_14558
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/864/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n3 7 4\n2 6 5\n3 7 6\n", "output": "11\n2\n2 3 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n5 6 1\n3 3 5\n", "output": "1\n1\n1 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "9\n13 18 14\n8 59 20\n9 51 2\n18 32 15\n1 70 18\n14 81 14\n10 88 16\n18 52 3\n1 50 6\n", "output": "106\n8\n1 4 9 8 2 5 6 7 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n12 44 17\n10 12 11\n16 46 5\n17 55 5\n6 60 2\n", "output": "35\n4\n2 1 3 5 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n18 85 3\n16 91 20\n12 92 11\n20 86 20\n15 43 4\n16 88 7\n", "output": "62\n5\n5 4 6 2 3 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n12 13 2\n1 9 3\n", "output": "3\n1\n2 \n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1202
Solve the following coding problem using the programming language python: Two semifinals have just been in the running tournament. Each semifinal had n participants. There are n participants advancing to the finals, they are chosen as follows: from each semifinal, we choose k people (0 ≤ 2k ≤ n) who showed the best result in their semifinals and all other places in the finals go to the people who haven't ranked in the top k in their semifinal but got to the n - 2k of the best among the others. The tournament organizers hasn't yet determined the k value, so the participants want to know who else has any chance to get to the finals and who can go home. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of participants in each semifinal. Each of the next n lines contains two integers a_{i} and b_{i} (1 ≤ a_{i}, b_{i} ≤ 10^9) — the results of the i-th participant (the number of milliseconds he needs to cover the semifinals distance) of the first and second semifinals, correspondingly. All results are distinct. Sequences a_1, a_2, ..., a_{n} and b_1, b_2, ..., b_{n} are sorted in ascending order, i.e. in the order the participants finished in the corresponding semifinal. -----Output----- Print two strings consisting of n characters, each equals either "0" or "1". The first line should correspond to the participants of the first semifinal, the second line should correspond to the participants of the second semifinal. The i-th character in the j-th line should equal "1" if the i-th participant of the j-th semifinal has any chances to advance to the finals, otherwise it should equal a "0". -----Examples----- Input 4 9840 9920 9860 9980 9930 10020 10040 10090 Output 1110 1100 Input 4 9900 9850 9940 9930 10000 10020 10060 10110 Output 1100 1100 -----Note----- Consider the first sample. Each semifinal has 4 participants. The results of the first semifinal are 9840, 9860, 9930, 10040. The results of the second semifinal are 9920, 9980, 10020, 10090. If k = 0, the finalists are determined by the time only, so players 9840, 9860, 9920 and 9930 advance to the finals. If k = 1, the winners from both semifinals move to the finals (with results 9840 and 9920), and the other places are determined by the time (these places go to the sportsmen who run the distance in 9860 and 9930 milliseconds). If k = 2, then first and second places advance from each seminfial, these are participants with results 9840, 9860, 9920 and 9980 milliseconds. 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(tuple(map(int,input().split())) for i in range(n)) #a.append(tuple(map(int,input().split()))) m = n // 2 c = list('1' for i in range(m)) d = list('1' for i in range(m)) #for i in range(n):print(a[i][0],a[i][1]) for i in range(m,n): if a[i][0] < a[n-i-1][1]: c.append('1') else: c.append('0') if a[n-i-1][0] > a[i][1]: d.append('1') else: d.append('0') print(''.join(c)) print(''.join(d)) ```
vfc_14562
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/378/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n9840 9920\n9860 9980\n9930 10020\n10040 10090\n", "output": "1110\n1100\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n9900 9850\n9940 9930\n10000 10020\n10060 10110\n", "output": "1100\n1100\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n1 2\n", "output": "1\n0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n2 1\n", "output": "0\n1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n1 2\n3 4\n", "output": "10\n10\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n3 1\n4 2\n", "output": "10\n11\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1203
Solve the following coding problem using the programming language python: While sailing on a boat, Inessa noticed a beautiful water lily flower above the lake's surface. She came closer and it turned out that the lily was exactly $H$ centimeters above the water surface. Inessa grabbed the flower and sailed the distance of $L$ centimeters. Exactly at this point the flower touched the water surface. [Image] Suppose that the lily grows at some point $A$ on the lake bottom, and its stem is always a straight segment with one endpoint at point $A$. Also suppose that initially the flower was exactly above the point $A$, i.e. its stem was vertical. Can you determine the depth of the lake at point $A$? -----Input----- The only line contains two integers $H$ and $L$ ($1 \le H < L \le 10^{6}$). -----Output----- Print a single number — the depth of the lake at point $A$. The absolute or relative error should not exceed $10^{-6}$. Formally, let your answer be $A$, and the jury's answer be $B$. Your answer is accepted if and only if $\frac{|A - B|}{\max{(1, |B|)}} \le 10^{-6}$. -----Examples----- Input 1 2 Output 1.5000000000000 Input 3 5 Output 2.6666666666667 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python h,l=map(int,input().split()) print((l**2-h**2)/2/h) ```
vfc_14566
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1199/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1 2\n", "output": "1.5000000000000\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1204
Solve the following coding problem using the programming language python: This problem consists of three subproblems: for solving subproblem C1 you will receive 4 points, for solving subproblem C2 you will receive 4 points, and for solving subproblem C3 you will receive 8 points. Manao decided to pursue a fighter's career. He decided to begin with an ongoing tournament. Before Manao joined, there were n contestants in the tournament, numbered from 1 to n. Each of them had already obtained some amount of tournament points, namely the i-th fighter had p_{i} points. Manao is going to engage in a single fight against each contestant. Each of Manao's fights ends in either a win or a loss. A win grants Manao one point, and a loss grants Manao's opponent one point. For each i, Manao estimated the amount of effort e_{i} he needs to invest to win against the i-th contestant. Losing a fight costs no effort. After Manao finishes all of his fights, the ranklist will be determined, with 1 being the best rank and n + 1 being the worst. The contestants will be ranked in descending order of their tournament points. The contestants with the same number of points as Manao will be ranked better than him if they won the match against him and worse otherwise. The exact mechanism of breaking ties for other fighters is not relevant here. Manao's objective is to have rank k or better. Determine the minimum total amount of effort he needs to invest in order to fulfill this goal, if it is possible. -----Input----- The first line contains a pair of integers n and k (1 ≤ k ≤ n + 1). The i-th of the following n lines contains two integers separated by a single space — p_{i} and e_{i} (0 ≤ p_{i}, e_{i} ≤ 200000). The problem consists of three subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. In subproblem C1 (4 points), the constraint 1 ≤ n ≤ 15 will hold. In subproblem C2 (4 points), the constraint 1 ≤ n ≤ 100 will hold. In subproblem C3 (8 points), the constraint 1 ≤ n ≤ 200000 will hold. -----Output----- Print a single number in a single line — the minimum amount of effort Manao needs to use to rank in the top k. If no amount of effort can earn Manao such a rank, output number -1. -----Examples----- Input 3 2 1 1 1 4 2 2 Output 3 Input 2 1 3 2 4 0 Output -1 Input 5 2 2 10 2 10 1 1 3 1 3 1 Output 12 -----Note----- Consider the first test case. At the time when Manao joins the tournament, there are three fighters. The first of them has 1 tournament point and the victory against him requires 1 unit of effort. The second contestant also has 1 tournament point, but Manao needs 4 units of effort to defeat him. The third contestant has 2 points and victory against him costs Manao 2 units of effort. Manao's goal is top be in top 2. The optimal decision is to win against fighters 1 and 3, after which Manao, fighter 2, and fighter 3 will all have 2 points. Manao will rank better than fighter 3 and worse than fighter 2, thus finishing in second place. Consider the second test case. Even if Manao wins against both opponents, he will still rank third. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python m = 301000 ns = [0] * m es = [0] * m c = [0] * m b = [0] * m t = [0] * m P = 0 def add(b, k): k = t[k] while k: e = es[k] if b[-1] > e: b[-1] = e b[e] += 1 k = ns[k] def delete(b): for i in range(b[m - 1], m + 1): if b[i]: b[i] -= 1 b[-1] = i return i def calc(k): nonlocal b q = 0 b = [0] * m b[-1] = m take = rank - dn if take < 0: take = 0 add(b, k) add(b, k - 1) for i in range(1, take + 1): q += delete(b) for i in range(k - 1): add(b, i) for i in range(k + 1, P + 1): add(b, i) for i in range(1, k - take + 1): q += delete(b) return q n, k = list(map(int, input().split())) rank = n - k + 1 if rank == 0: print('0') return for i in range(1, n + 1): p, e = list(map(int, input().split())) if p > P: P = p c[p] += 1 es[i], ns[i] = e, t[p] t[p] = i dn = 0 for i in range(1, n + 1): if i > 1: dn += c[i - 2] if c[i] + c[i - 1] + dn >= rank and rank <= i + dn: u = calc(i) if i < n: dn += c[i - 1] v = calc(i + 1) if u > v: u = v if i < n - 1: dn += c[i] v = calc(i + 2) if u > v: u = v print(u) return print('-1') ```
vfc_14570
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/391/C1", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 2\n1 1\n1 4\n2 2\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 1\n3 2\n4 0\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 2\n2 10\n2 10\n1 1\n3 1\n3 1\n", "output": "12\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1\n1 200000\n", "output": "200000\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 2\n1 100000\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1205
Solve the following coding problem using the programming language python: You are given a set of n points on the plane. A line containing the origin is called good, if projection of the given set to this line forms a symmetric multiset of points. Find the total number of good lines. Multiset is a set where equal elements are allowed. Multiset is called symmetric, if there is a point P on the plane such that the multiset is centrally symmetric in respect of point P. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 2000) — the number of points in the set. Each of the next n lines contains two integers x_{i} and y_{i} ( - 10^6 ≤ x_{i}, y_{i} ≤ 10^6) — the coordinates of the points. It is guaranteed that no two points coincide. -----Output----- If there are infinitely many good lines, print -1. Otherwise, print single integer — the number of good lines. -----Examples----- Input 3 1 2 2 1 3 3 Output 3 Input 2 4 3 1 2 Output -1 -----Note----- Picture to the first sample test: [Image] In the second sample, any line containing the origin is good. 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 Fraction import time from collections import Counter class Point: def __init__(self, x, y): self.x = x self.y = y def to_tuple(self): return (self.x, self.y) def __repr__(self): return "Point({}, {})".format(self.x, self.y) def __eq__(self, other): return self.to_tuple() == other.to_tuple() def __hash__(self): return hash(self.to_tuple()) def __neg__(self): return Point(-self.x, -self.y) def __add__(self, other): return Point(self.x+other.x, self.y+other.y) def __sub__(self, other): return self+(-other) def scalar_mul(self, mu): return Point(mu*self.x, mu*self.y) def int_divide(self, den): return Point(self.x//den, self.y//den) def __lt__(self, other): if self.x == other.x: return self.y < other.y return self.x < other.x def dot(self, other): return self.x*other.x+self.y*other.y class Line: def __init__(self, a, b, c): # ax+by+c=0 self.a = a self.b = b self.c = c def __repr__(self): return "{}*x + {}*y + {} = 0".format(self.a, self.b, self.c) @classmethod def between_two_points(cls, P, Q): return cls(P.y-Q.y, Q.x-P.x, P.x*Q.y-P.y*Q.x) def evaluate(self, P): return self.a*P.x+self.b*P.y+self.c def direction(self): if self.a == 0: return (0, 1) return (1, Fraction(self.b, self.a)) true_start = time.time() n = int(input()) points = set() center = Point(0, 0) for i in range(n): row = input().split(" ") cur = Point(int(row[0]), int(row[1])).scalar_mul(2*n) center += cur points.add(cur) center = center.int_divide(n) dcenter = center+center # nosym = [] # for p in points: # psym = dcenter-p # if psym not in points: # nosym.append(p) sym_points_set = set() for p in points: sym_points_set.add(dcenter-p) nosym = list(points - sym_points_set) #print(nosym) # print("preproc:", time.time()-true_start) if len(nosym) == 0: print(-1) return cnt = 0 p0 = nosym[0] good_lines = set() for p in nosym: start = time.time() m = (p+p0).int_divide(2) supp = Line.between_two_points(m, center) time_setup = time.time()-start distances = list(map(supp.evaluate, nosym)) time_projs = time.time()-start # sorting strat ok = True SORTING = False if SORTING: distances = sorted(distances) time_sorting = time.time()-start m = len(distances) for i in range(m//2): if distances[i] != -distances[m-1-i]: ok = False break else: mydict = {} for dd in distances: dda = abs(dd) if dda not in mydict: mydict[dda] = 1 else: mydict[dda] += 1 time_sorting = time.time()-start for k in mydict: if mydict[k] % 2 == 1 and k != 0: ok = False break if ok: #print("ok", supp) #print(distances) #print(mydict) good_lines.add(supp.direction()) #print("setup: {}\tprojs: {}\tsort: {}\tdone: {}".format(time_setup, time_projs, time_sorting, time.time()-start)) #print("total:", time.time()-true_start) print(len(good_lines)) ```
vfc_14574
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/886/F", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n1 2\n2 1\n3 3\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n4 3\n1 2\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n0 4\n1 5\n2 1\n3 2\n4 3\n5 0\n", "output": "5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n5 2\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n2 4\n1 2\n0 0\n-2 -4\n", "output": "1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1206
Solve the following coding problem using the programming language python: Nowadays, most of the internet advertisements are not statically linked to a web page. Instead, what will be shown to the person opening a web page is determined within 100 milliseconds after the web page is opened. Usually, multiple companies compete for each ad slot on the web page in an auction. Each of them receives a request with details about the user, web page and ad slot and they have to respond within those 100 milliseconds with a bid they would pay for putting an advertisement on that ad slot. The company that suggests the highest bid wins the auction and gets to place its advertisement. If there are several companies tied for the highest bid, the winner gets picked at random. However, the company that won the auction does not have to pay the exact amount of its bid. In most of the cases, a second-price auction is used. This means that the amount paid by the company is equal to the maximum of all the other bids placed for this ad slot. Let's consider one such bidding. There are n companies competing for placing an ad. The i-th of these companies will bid an integer number of microdollars equiprobably randomly chosen from the range between L_{i} and R_{i}, inclusive. In the other words, the value of the i-th company bid can be any integer from the range [L_{i}, R_{i}] with the same probability. Determine the expected value that the winner will have to pay in a second-price auction. -----Input----- The first line of input contains an integer number n (2 ≤ n ≤ 5). n lines follow, the i-th of them containing two numbers L_{i} and R_{i} (1 ≤ L_{i} ≤ R_{i} ≤ 10000) describing the i-th company's bid preferences. This problem doesn't have subproblems. You will get 8 points for the correct submission. -----Output----- Output the answer with absolute or relative error no more than 1e - 9. -----Examples----- Input 3 4 7 8 10 5 5 Output 5.7500000000 Input 3 2 5 3 4 1 6 Output 3.5000000000 -----Note----- Consider the first example. The first company bids a random integer number of microdollars in range [4, 7]; the second company bids between 8 and 10, and the third company bids 5 microdollars. The second company will win regardless of the exact value it bids, however the price it will pay depends on the value of first company's bid. With probability 0.5 the first company will bid at most 5 microdollars, and the second-highest price of the whole auction will be 5. With probability 0.25 it will bid 6 microdollars, and with probability 0.25 it will bid 7 microdollars. Thus, the expected value the second company will have to pay is 0.5·5 + 0.25·6 + 0.25·7 = 5.75. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def p2pl(p1,p2,p3,p4,p5): prob0 = (1-p1)*(1-p2)*(1-p3)*(1-p4)*(1-p5) prob1 = p1*(1-p2)*(1-p3)*(1-p4)*(1-p5) + \ p2*(1-p1)*(1-p3)*(1-p4)*(1-p5) + \ p3*(1-p1)*(1-p2)*(1-p4)*(1-p5) + \ p4*(1-p1)*(1-p2)*(1-p3)*(1-p5) + \ p5*(1-p1)*(1-p2)*(1-p3)*(1-p4) return 1-(prob1+prob0) n = int(input()) c1 = input().split(' ') c1 = [int(c1[0]),int(c1[1])] c2 = input().split(' ') c2 = [int(c2[0]),int(c2[1])] if n >= 3: c3 = input().split(' ') c3 = [int(c3[0]),int(c3[1])] else: c3 = [0,0] if n >= 4: c4 = input().split(' ') c4 = [int(c4[0]),int(c4[1])] else: c4 = [0,0] if n >= 5: c5 = input().split(' ') c5 = [int(c5[0]),int(c5[1])] else: c5 = [0,0] ans = 0 for x in range(1,10001): p1 = min(1,max(c1[1]-x+1,0)/(c1[1]-c1[0]+1)) p2 = min(1,max(c2[1]-x+1,0)/(c2[1]-c2[0]+1)) p3 = min(1,max(c3[1]-x+1,0)/(c3[1]-c3[0]+1)) p4 = min(1,max(c4[1]-x+1,0)/(c4[1]-c4[0]+1)) p5 = min(1,max(c5[1]-x+1,0)/(c5[1]-c5[0]+1)) ans += p2pl(p1,p2,p3,p4,p5) print(ans) ```
vfc_14578
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/513/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n4 7\n8 10\n5 5\n", "output": "5.7500000000\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n2 5\n3 4\n1 6\n", "output": "3.5000000000\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n1 10000\n1 10000\n1 10000\n1 10000\n1 10000\n", "output": "6667.1666666646\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1207
Solve the following coding problem using the programming language python: While Farmer John rebuilds his farm in an unfamiliar portion of Bovinia, Bessie is out trying some alternative jobs. In her new gig as a reporter, Bessie needs to know about programming competition results as quickly as possible. When she covers the 2016 Robot Rap Battle Tournament, she notices that all of the robots operate under deterministic algorithms. In particular, robot i will beat robot j if and only if robot i has a higher skill level than robot j. And if robot i beats robot j and robot j beats robot k, then robot i will beat robot k. Since rapping is such a subtle art, two robots can never have the same skill level. Given the results of the rap battles in the order in which they were played, determine the minimum number of first rap battles that needed to take place before Bessie could order all of the robots by skill level. -----Input----- The first line of the input consists of two integers, the number of robots n (2 ≤ n ≤ 100 000) and the number of rap battles m ($1 \leq m \leq \operatorname{min}(100000, \frac{n(n - 1)}{2})$). The next m lines describe the results of the rap battles in the order they took place. Each consists of two integers u_{i} and v_{i} (1 ≤ u_{i}, v_{i} ≤ n, u_{i} ≠ v_{i}), indicating that robot u_{i} beat robot v_{i} in the i-th rap battle. No two rap battles involve the same pair of robots. It is guaranteed that at least one ordering of the robots satisfies all m relations. -----Output----- Print the minimum k such that the ordering of the robots by skill level is uniquely defined by the first k rap battles. If there exists more than one ordering that satisfies all m relations, output -1. -----Examples----- Input 4 5 2 1 1 3 2 3 4 2 4 3 Output 4 Input 3 2 1 2 3 2 Output -1 -----Note----- In the first sample, the robots from strongest to weakest must be (4, 2, 1, 3), which Bessie can deduce after knowing the results of the first four rap battles. In the second sample, both (1, 3, 2) and (3, 1, 2) are possible orderings of the robots from strongest to weakest after both rap battles. 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 class RobotRapping(): def __init__(self, n, m, battles): self.n, self.m = n, m self.battles = battles def generate_graph(self, k): edge_map = defaultdict(list) rev_map = defaultdict(list) for i in range(k): edge_map[self.battles[i][0]-1].append((self.battles[i][1]-1, i)) rev_map[self.battles[i][1]-1].append((self.battles[i][0]-1, i)) return edge_map, rev_map def check_order(self, num_battles): edge_map, rev_map = self.generate_graph(num_battles) outgoing_cnt = defaultdict(int) for k in edge_map: outgoing_cnt[k] = len(edge_map[k]) s = [] cntr = 0 for i in range(self.n): if outgoing_cnt[i] == 0: s.append(i) while len(s) > cntr: if len(s) > cntr+1 : return False else: node = s[cntr] for v in rev_map[node]: outgoing_cnt[v] -= 1 if outgoing_cnt[v] == 0: s.append(v) cntr += 1 return True def min_battles(self): if not self.check_order(self.m): print(-1) else: mn, mx = 0, self.m while mn < mx-1: md = int((mn+mx)/2) if self.check_order(md): mx = md else: mn = md print(mx) def min_battles2(self): edge_map, rev_map = self.generate_graph(self.m) outgoing_cnt = defaultdict(int) for k in edge_map: outgoing_cnt[k] = len(edge_map[k]) s = [] cntr = 0 order = [] for i in range(self.n): if outgoing_cnt[i] == 0: s.append(i) while len(s) > cntr: if len(s) > cntr+1 : print(-1) return else: node = s[cntr] order.append(node) for v,_ in rev_map[node]: outgoing_cnt[v] -= 1 if outgoing_cnt[v] == 0: s.append(v) cntr += 1 mn_pos = -1 for i in range(1,self.n): for v,ind in edge_map[order[i]]: if v == order[i-1]: mn_pos = max(mn_pos, ind) break print(mn_pos+1) n,m = list(map(int,input().strip(' ').split(' '))) battles = [] for i in range(m): x,y = list(map(int,input().strip(' ').split(' '))) battles.append((x,y)) rr = RobotRapping(n,m,battles) rr.min_battles2() ```
vfc_14582
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/645/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4 5\n2 1\n1 3\n2 3\n4 2\n4 3\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 2\n1 2\n3 2\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 1\n1 2\n", "output": "1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1208
Solve the following coding problem using the programming language python: Berland National Library has recently been built in the capital of Berland. In addition, in the library you can take any of the collected works of Berland leaders, the library has a reading room. Today was the pilot launch of an automated reading room visitors' accounting system! The scanner of the system is installed at the entrance to the reading room. It records the events of the form "reader entered room", "reader left room". Every reader is assigned a registration number during the registration procedure at the library — it's a unique integer from 1 to 10^6. Thus, the system logs events of two forms: "+ r_{i}" — the reader with registration number r_{i} entered the room; "- r_{i}" — the reader with registration number r_{i} left the room. The first launch of the system was a success, it functioned for some period of time, and, at the time of its launch and at the time of its shutdown, the reading room may already have visitors. Significant funds of the budget of Berland have been spent on the design and installation of the system. Therefore, some of the citizens of the capital now demand to explain the need for this system and the benefits that its implementation will bring. Now, the developers of the system need to urgently come up with reasons for its existence. Help the system developers to find the minimum possible capacity of the reading room (in visitors) using the log of the system available to you. -----Input----- The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of records in the system log. Next follow n events from the system journal in the order in which the were made. Each event was written on a single line and looks as "+ r_{i}" or "- r_{i}", where r_{i} is an integer from 1 to 10^6, the registration number of the visitor (that is, distinct visitors always have distinct registration numbers). It is guaranteed that the log is not contradictory, that is, for every visitor the types of any of his two consecutive events are distinct. Before starting the system, and after stopping the room may possibly contain visitors. -----Output----- Print a single integer — the minimum possible capacity of the reading room. -----Examples----- Input 6 + 12001 - 12001 - 1 - 1200 + 1 + 7 Output 3 Input 2 - 1 - 2 Output 2 Input 2 + 1 - 1 Output 1 -----Note----- In the first sample test, the system log will ensure that at some point in the reading room were visitors with registration numbers 1, 1200 and 12001. More people were not in the room at the same time based on the log. Therefore, the answer to the test is 3. 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()) result = 0 d = set() for i in range(n): t, a = input().split() a = int(a) if t == "+": d.add(a) result = max(result, len(d)) else: if a in d: d.remove(a) else: result += 1 print(result) main() ```
vfc_14586
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/567/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6\n+ 12001\n- 12001\n- 1\n- 1200\n+ 1\n+ 7\n", "output": "3", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n- 1\n- 2\n", "output": "2", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n+ 1\n- 1\n", "output": "1", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n+ 1\n- 1\n+ 2\n+ 3\n- 4\n", "output": "3", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n- 1\n- 2\n- 3\n", "output": "3", "type": "stdin_stdout" } ] }
apps
verifiable_code
1209
Solve the following coding problem using the programming language python: Vus the Cossack has $n$ real numbers $a_i$. It is known that the sum of all numbers is equal to $0$. He wants to choose a sequence $b$ the size of which is $n$ such that the sum of all numbers is $0$ and each $b_i$ is either $\lfloor a_i \rfloor$ or $\lceil a_i \rceil$. In other words, $b_i$ equals $a_i$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $a = [4.58413, 1.22491, -2.10517, -3.70387]$, then $b$ can be equal, for example, to $[4, 2, -2, -4]$. Note that if $a_i$ is an integer, then there is no difference between $\lfloor a_i \rfloor$ and $\lceil a_i \rceil$, $b_i$ will always be equal to $a_i$. Help Vus the Cossack find such sequence! -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of numbers. Each of the next $n$ lines contains one real number $a_i$ ($|a_i| < 10^5$). It is guaranteed that each $a_i$ has exactly $5$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $0$. -----Output----- In each of the next $n$ lines, print one integer $b_i$. For each $i$, $|a_i-b_i|<1$ must be met. If there are multiple answers, print any. -----Examples----- Input 4 4.58413 1.22491 -2.10517 -3.70387 Output 4 2 -2 -4 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Output -6 3 -1 2 2 -----Note----- The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from math import floor, ceil n = int(input()) arr = [] for i in range(n): arr.append(float(input())) k = [floor(i) for i in arr] delta = -sum(k) for i in range(n): if int(arr[i]) != arr[i] and delta: delta -= 1 print(ceil(arr[i])) else: print(floor(arr[i])) ```
vfc_14590
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1186/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n4.58413\n1.22491\n-2.10517\n-3.70387\n", "output": "5\n2\n-3\n-4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n-6.32509\n3.30066\n-0.93878\n2.00000\n1.96321\n", "output": "-6\n4\n-1\n2\n1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n0.00001\n-0.00001\n", "output": "1\n-1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1211
Solve the following coding problem using the programming language python: Dima has a hamsters farm. Soon N hamsters will grow up on it and Dima will sell them in a city nearby. Hamsters should be transported in boxes. If some box is not completely full, the hamsters in it are bored, that's why each box should be completely full with hamsters. Dima can buy boxes at a factory. The factory produces boxes of K kinds, boxes of the i-th kind can contain in themselves a_{i} hamsters. Dima can buy any amount of boxes, but he should buy boxes of only one kind to get a wholesale discount. Of course, Dima would buy boxes in such a way that each box can be completely filled with hamsters and transported to the city. If there is no place for some hamsters, Dima will leave them on the farm. Find out how many boxes and of which type should Dima buy to transport maximum number of hamsters. -----Input----- The first line contains two integers N and K (0 ≤ N ≤ 10^18, 1 ≤ K ≤ 10^5) — the number of hamsters that will grow up on Dima's farm and the number of types of boxes that the factory produces. The second line contains K integers a_1, a_2, ..., a_{K} (1 ≤ a_{i} ≤ 10^18 for all i) — the capacities of boxes. -----Output----- Output two integers: the type of boxes that Dima should buy and the number of boxes of that type Dima should buy. Types of boxes are numbered from 1 to K in the order they are given in input. If there are many correct answers, output any of them. -----Examples----- Input 19 3 5 4 10 Output 2 4 Input 28 3 5 6 30 Output 1 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 = [int(i) for i in input().split(" ")] A = [int(i) for i in input().split(" ")] mx=0 mxval=0 for i in range(k): if A[i]*(n//A[i])>mxval: mxval = A[i]*(n//A[i]) mx = i print(mx+1, n//A[mx]) ```
vfc_14598
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/939/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "19 3\n5 4 10\n", "output": "2 4\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1212
Solve the following coding problem using the programming language python: There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is h_{i} meters, distinct planks can have distinct heights. [Image] Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1] Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible. Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic). -----Input----- The first line of the input contains integers n and k (1 ≤ n ≤ 1.5·10^5, 1 ≤ k ≤ n) — the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h_1, h_2, ..., h_{n} (1 ≤ h_{i} ≤ 100), where h_{i} is the height of the i-th plank of the fence. -----Output----- Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them. -----Examples----- Input 7 3 1 2 6 1 1 7 1 Output 3 -----Note----- In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8. 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()) h = list(map(int, input().split())) a = [0]*(n-k+1) a[0] = sum(h[0:k]) for i in range(1,n-k+1): a[i] = a[i-1]+h[i+k-1]-h[i-1] m = min(a) print(a.index(m)+1) ```
vfc_14602
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/363/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "7 3\n1 2 6 1 1 7 1\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1\n100\n", "output": "1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1213
Solve the following coding problem using the programming language python: The R1 company has recently bought a high rise building in the centre of Moscow for its main office. It's time to decorate the new office, and the first thing to do is to write the company's slogan above the main entrance to the building. The slogan of the company consists of n characters, so the decorators hung a large banner, n meters wide and 1 meter high, divided into n equal squares. The first character of the slogan must be in the first square (the leftmost) of the poster, the second character must be in the second square, and so on. Of course, the R1 programmers want to write the slogan on the poster themselves. To do this, they have a large (and a very heavy) ladder which was put exactly opposite the k-th square of the poster. To draw the i-th character of the slogan on the poster, you need to climb the ladder, standing in front of the i-th square of the poster. This action (along with climbing up and down the ladder) takes one hour for a painter. The painter is not allowed to draw characters in the adjacent squares when the ladder is in front of the i-th square because the uncomfortable position of the ladder may make the characters untidy. Besides, the programmers can move the ladder. In one hour, they can move the ladder either a meter to the right or a meter to the left. Drawing characters and moving the ladder is very tiring, so the programmers want to finish the job in as little time as possible. Develop for them an optimal poster painting plan! -----Input----- The first line contains two integers, n and k (1 ≤ k ≤ n ≤ 100) — the number of characters in the slogan and the initial position of the ladder, correspondingly. The next line contains the slogan as n characters written without spaces. Each character of the slogan is either a large English letter, or digit, or one of the characters: '.', '!', ',', '?'. -----Output----- In t lines, print the actions the programmers need to make. In the i-th line print: "LEFT" (without the quotes), if the i-th action was "move the ladder to the left"; "RIGHT" (without the quotes), if the i-th action was "move the ladder to the right"; "PRINT x" (without the quotes), if the i-th action was to "go up the ladder, paint character x, go down the ladder". The painting time (variable t) must be minimum possible. If there are multiple optimal painting plans, you can print any of them. -----Examples----- Input 2 2 R1 Output PRINT 1 LEFT PRINT R Input 2 1 R1 Output PRINT R RIGHT PRINT 1 Input 6 4 GO?GO! Output RIGHT RIGHT PRINT ! LEFT PRINT O LEFT PRINT G LEFT PRINT ? LEFT PRINT O LEFT PRINT G -----Note----- Note that the ladder cannot be shifted by less than one meter. The ladder can only stand in front of some square of the poster. For example, you cannot shift a ladder by half a meter and position it between two squares. Then go up and paint the first character and the second character. 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()) s = input() left = k - 1 right = n - k if left < right: print('LEFT\n' * left, end = '') for i in range(len(s) - 1): c = s[i] print('PRINT', c) print('RIGHT') print('PRINT', s[-1]) else: print('RIGHT\n' * right, end = '') for i in range(len(s) - 1): c = s[-i - 1] print('PRINT', c) print('LEFT') print('PRINT', s[0]) ```
vfc_14606
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/412/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2 2\nR1\n", "output": "PRINT 1\nLEFT\nPRINT R\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1214
Solve the following coding problem using the programming language python: Chouti is working on a strange math problem. There was a sequence of $n$ positive integers $x_1, x_2, \ldots, x_n$, where $n$ is even. The sequence was very special, namely for every integer $t$ from $1$ to $n$, $x_1+x_2+...+x_t$ is a square of some integer number (that is, a perfect square). Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $x_2, x_4, x_6, \ldots, x_n$. The task for him is to restore the original sequence. Again, it's your turn to help him. The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any. -----Input----- The first line contains an even number $n$ ($2 \le n \le 10^5$). The second line contains $\frac{n}{2}$ positive integers $x_2, x_4, \ldots, x_n$ ($1 \le x_i \le 2 \cdot 10^5$). -----Output----- If there are no possible sequence, print "No". Otherwise, print "Yes" and then $n$ positive integers $x_1, x_2, \ldots, x_n$ ($1 \le x_i \le 10^{13}$), where $x_2, x_4, \ldots, x_n$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $x_i$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $1 \le x_i \le 10^{13}$. -----Examples----- Input 6 5 11 44 Output Yes 4 5 16 11 64 44 Input 2 9900 Output Yes 100 9900 Input 6 314 1592 6535 Output No -----Note----- In the first example $x_1=4$ $x_1+x_2=9$ $x_1+x_2+x_3=25$ $x_1+x_2+x_3+x_4=36$ $x_1+x_2+x_3+x_4+x_5=100$ $x_1+x_2+x_3+x_4+x_5+x_6=144$ All these numbers are perfect squares. In the second example, $x_1=100$, $x_1+x_2=10000$. They are all perfect squares. There're other answers possible. For example, $x_1=22500$ is another answer. In the third example, it is possible to show, that no such sequence exists. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python Z = 100000 n = int(input()) xx = list(map(int, input().split())) def cal(): ans = [] a = 1 b = 1 s = 0 ls = 1 for x in xx: while b <= Z: if s < x: s += b * 2 + 1 b += 1 elif s > x: s -= a * 2 + 1 ls += a * 2 + 1 a += 1 else: ans.append(ls) ans.append(x) ls = b * 2 + 1 b += 1 a = b s = 0 break else: return return ans ans = cal() if ans: print('Yes') print(' '.join(map(str, ans))) else: print('No') ```
vfc_14610
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1081/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6\n5 11 44\n", "output": "Yes\n4 5 16 11 64 44\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n9900\n", "output": "Yes\n100 9900\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n314 1592 6535\n", "output": "No\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n44 23 65 17 48\n", "output": "No\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1215
Solve the following coding problem using the programming language python: You have a given integer $n$. Find the number of ways to fill all $3 \times n$ tiles with the shape described in the picture below. Upon filling, no empty spaces are allowed. Shapes cannot overlap. $\square$ This picture describes when $n = 4$. The left one is the shape and the right one is $3 \times n$ tiles. -----Input----- The only line contains one integer $n$ ($1 \le n \le 60$) — the length. -----Output----- Print the number of ways to fill. -----Examples----- Input 4 Output 4 Input 1 Output 0 -----Note----- In the first example, there are $4$ possible cases of filling. In the second example, you cannot fill the shapes in $3 \times 1$ tiles. 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%2==1): print(0) else: print(2**(n//2)) ```
vfc_14614
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1182/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n", "output": "4", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n", "output": "0", "type": "stdin_stdout" }, { "fn_name": null, "input": "17\n", "output": "0", "type": "stdin_stdout" }, { "fn_name": null, "input": "36\n", "output": "262144", "type": "stdin_stdout" }, { "fn_name": null, "input": "47\n", "output": "0", "type": "stdin_stdout" } ] }
apps
verifiable_code
1216
Solve the following coding problem using the programming language python: Stepan likes to repeat vowel letters when he writes words. For example, instead of the word "pobeda" he can write "pobeeeedaaaaa". Sergey does not like such behavior, so he wants to write a program to format the words written by Stepan. This program must combine all consecutive equal vowels to a single vowel. The vowel letters are "a", "e", "i", "o", "u" and "y". There are exceptions: if letters "e" or "o" repeat in a row exactly 2 times, like in words "feet" and "foot", the program must skip them and do not transform in one vowel. For example, the word "iiiimpleeemeentatiioon" must be converted to the word "implemeentatioon". Sergey is very busy and asks you to help him and write the required program. -----Input----- The first line contains the integer n (1 ≤ n ≤ 100 000) — the number of letters in the word written by Stepan. The second line contains the string s which has length that equals to n and contains only lowercase English letters — the word written by Stepan. -----Output----- Print the single string — the word written by Stepan converted according to the rules described in the statement. -----Examples----- Input 13 pobeeeedaaaaa Output pobeda Input 22 iiiimpleeemeentatiioon Output implemeentatioon Input 18 aeiouyaaeeiioouuyy Output aeiouyaeeioouy Input 24 aaaoooiiiuuuyyyeeeggghhh Output aoiuyeggghhh The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python bad = ['e', 'a', 'i', 'o', 'u', 'y'] n = int(input()) s = input() ans = '' i = 0 while i != len(s): if s[i] in bad: letter = s[i] pos = i while i != len(s) and letter == s[i]: i += 1 if i - pos == 2 and letter in ['e', 'o']: ans += 2 * letter else: ans += letter else: ans += s[i] i += 1 print(ans) ```
vfc_14618
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/774/K", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "13\npobeeeedaaaaa\n", "output": "pobeda\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "22\niiiimpleeemeentatiioon\n", "output": "implemeentatioon\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "18\naeiouyaaeeiioouuyy\n", "output": "aeiouyaeeioouy\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "24\naaaoooiiiuuuyyyeeeggghhh\n", "output": "aoiuyeggghhh\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "36\naeiouyaaeeiioouuyyaaaeeeiiiooouuuyyy\n", "output": "aeiouyaeeioouyaeiouy\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "100\noiyufyyyioueoudosizoryuoedatenougiuaeuouuyoiimaeigeeycewuooyovacoiyuaygfuuaiaeuahuieeafxsciylaebeufi\n", "output": "oiyufyioueoudosizoryuoedatenougiuaeuouyoimaeigeeycewuooyovacoiyuaygfuaiaeuahuieeafxsciylaebeufi\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1217
Solve the following coding problem using the programming language python: You are given two arrays of integers a and b. For each element of the second array b_{j} you should find the number of elements in array a that are less than or equal to the value b_{j}. -----Input----- The first line contains two integers n, m (1 ≤ n, m ≤ 2·10^5) — the sizes of arrays a and b. The second line contains n integers — the elements of array a ( - 10^9 ≤ a_{i} ≤ 10^9). The third line contains m integers — the elements of array b ( - 10^9 ≤ b_{j} ≤ 10^9). -----Output----- Print m integers, separated by spaces: the j-th of which is equal to the number of such elements in array a that are less than or equal to the value b_{j}. -----Examples----- Input 5 4 1 3 5 7 9 6 4 2 8 Output 3 2 1 4 Input 5 5 1 2 1 2 5 3 1 4 1 5 Output 4 2 4 2 5 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 bisect import bisect try: while True: n, m = map(int, input().split()) a = list(map(int, input().split())) a.sort() for x in map(int, input().split()): print(bisect(a, x), end=' ') print() except EOFError: pass ```
vfc_14622
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/600/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5 4\n1 3 5 7 9\n6 4 2 8\n", "output": "3 2 1 4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 5\n1 2 1 2 5\n3 1 4 1 5\n", "output": "4 2 4 2 5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1\n-1\n-2\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1\n-80890826\n686519510\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "11 11\n237468511 -779187544 -174606592 193890085 404563196 -71722998 -617934776 170102710 -442808289 109833389 953091341\n994454001 322957429 216874735 -606986750 -455806318 -663190696 3793295 41395397 -929612742 -787653860 -684738874\n", "output": "11 9 8 2 2 1 5 5 0 0 1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1218
Solve the following coding problem using the programming language python: Vova, the Ultimate Thule new shaman, wants to build a pipeline. As there are exactly n houses in Ultimate Thule, Vova wants the city to have exactly n pipes, each such pipe should be connected to the water supply. A pipe can be connected to the water supply if there's water flowing out of it. Initially Vova has only one pipe with flowing water. Besides, Vova has several splitters. A splitter is a construction that consists of one input (it can be connected to a water pipe) and x output pipes. When a splitter is connected to a water pipe, water flows from each output pipe. You can assume that the output pipes are ordinary pipes. For example, you can connect water supply to such pipe if there's water flowing out from it. At most one splitter can be connected to any water pipe. [Image] The figure shows a 4-output splitter Vova has one splitter of each kind: with 2, 3, 4, ..., k outputs. Help Vova use the minimum number of splitters to build the required pipeline or otherwise state that it's impossible. Vova needs the pipeline to have exactly n pipes with flowing out water. Note that some of those pipes can be the output pipes of the splitters. -----Input----- The first line contains two space-separated integers n and k (1 ≤ n ≤ 10^18, 2 ≤ k ≤ 10^9). Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Output----- Print a single integer — the minimum number of splitters needed to build the pipeline. If it is impossible to build a pipeline with the given splitters, print -1. -----Examples----- Input 4 3 Output 2 Input 5 5 Output 1 Input 8 4 Output -1 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()) m = 2 * (n - 1) - k * (k - 1) if m > 0: print(-1) else: x = int((1 + (1 - 4 * m) ** 0.5) / 2) while x * x - x + m > 0: x -= 1 print(k - x) ```
vfc_14626
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/287/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4 3\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 5\n", "output": "1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1219
Solve the following coding problem using the programming language python: While playing yet another strategy game, Mans has recruited $n$ Swedish heroes, whose powers which can be represented as an array $a$. Unfortunately, not all of those mighty heroes were created as capable as he wanted, so that he decided to do something about it. In order to accomplish his goal, he can pick two consecutive heroes, with powers $a_i$ and $a_{i+1}$, remove them and insert a hero with power $-(a_i+a_{i+1})$ back in the same position. For example if the array contains the elements $[5, 6, 7, 8]$, he can pick $6$ and $7$ and get $[5, -(6+7), 8] = [5, -13, 8]$. After he will perform this operation $n-1$ times, Mans will end up having only one hero. He wants his power to be as big as possible. What's the largest possible power he can achieve? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 200000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$) — powers of the heroes. -----Output----- Print the largest possible power he can achieve after $n-1$ operations. -----Examples----- Input 4 5 6 7 8 Output 26 Input 5 4 -5 9 -2 1 Output 15 -----Note----- Suitable list of operations for the first sample: $[5, 6, 7, 8] \rightarrow [-11, 7, 8] \rightarrow [-11, -15] \rightarrow [26]$ The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python N,x,y,z,v,w=input(),-9e9,-9e9,-9e9,0,1 for A in map(int,input().split()):x,y,z,v,w=max(z+A,y-A),max(x+A,z-A),max(y+A,x-A,v-w*A),v+w*A,-w print([v,y][N>'1']) ```
vfc_14630
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1421/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n5 6 7 8\n", "output": "26\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n4 -5 9 -2 1\n", "output": "15\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n9 3 7 4 6\n", "output": "11\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n4 -5 1\n", "output": "-2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n98 56 99\n", "output": "57\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1221
Solve the following coding problem using the programming language python: Nian is a monster which lives deep in the oceans. Once a year, it shows up on the land, devouring livestock and even people. In order to keep the monster away, people fill their villages with red colour, light, and cracking noise, all of which frighten the monster out of coming. Little Tommy has n lanterns and Big Banban has m lanterns. Tommy's lanterns have brightness a_1, a_2, ..., a_{n}, and Banban's have brightness b_1, b_2, ..., b_{m} respectively. Tommy intends to hide one of his lanterns, then Banban picks one of Tommy's non-hidden lanterns and one of his own lanterns to form a pair. The pair's brightness will be the product of the brightness of two lanterns. Tommy wants to make the product as small as possible, while Banban tries to make it as large as possible. You are asked to find the brightness of the chosen pair if both of them choose optimally. -----Input----- The first line contains two space-separated integers n and m (2 ≤ n, m ≤ 50). The second line contains n space-separated integers a_1, a_2, ..., a_{n}. The third line contains m space-separated integers b_1, b_2, ..., b_{m}. All the integers range from - 10^9 to 10^9. -----Output----- Print a single integer — the brightness of the chosen pair. -----Examples----- Input 2 2 20 18 2 14 Output 252 Input 5 3 -1 0 1 2 3 -1 0 1 Output 2 -----Note----- In the first example, Tommy will hide 20 and Banban will choose 18 from Tommy and 14 from himself. In the second example, Tommy will hide 3 and Banban will choose 2 from Tommy and 1 from himself. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from sys import stdin, stdout n, m = map(int, stdin.readline().split()) first = list(map(int, stdin.readline().split())) second = list(map(int, stdin.readline().split())) ans = float('inf') for i in range(n): cnt = float('-inf') for j in range(m): for z in range(n): if z == i: continue cnt = max(cnt, first[z] * second[j]) ans = min(ans, cnt) stdout.write(str(ans)) ```
vfc_14638
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/934/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2 2\n20 18\n2 14\n", "output": "252\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 3\n-1 0 1 2 3\n-1 0 1\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 2\n1 6 2 10 2 3 2 10 6 4\n5 7\n", "output": "70\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "50 50\n1 6 2 10 2 3 2 10 6 4 5 0 3 1 7 3 2 4 4 2 1 5 0 6 10 1 8 0 10 9 0 4 10 5 5 7 4 9 9 5 5 2 6 7 9 4 3 7 2 0\n0 5 9 4 4 6 1 8 2 1 6 6 8 6 4 4 7 2 1 8 6 7 4 9 8 3 0 2 0 10 7 1 4 9 4 4 2 5 3 5 1 3 2 4 1 6 5 3 8 6\n", "output": "100\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 7\n-130464232 -73113866 -542094710 -53118823 -63528720\n-775179088 631683023 -974858199 -157471745 -629658630 71825477 -6235611\n", "output": "127184126241438168\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1228
Solve the following coding problem using the programming language python: Tokitsukaze is one of the characters in the game "Kantai Collection". In this game, every character has a common attribute — health points, shortened to HP. In general, different values of HP are grouped into $4$ categories: Category $A$ if HP is in the form of $(4 n + 1)$, that is, when divided by $4$, the remainder is $1$; Category $B$ if HP is in the form of $(4 n + 3)$, that is, when divided by $4$, the remainder is $3$; Category $C$ if HP is in the form of $(4 n + 2)$, that is, when divided by $4$, the remainder is $2$; Category $D$ if HP is in the form of $4 n$, that is, when divided by $4$, the remainder is $0$. The above-mentioned $n$ can be any integer. These $4$ categories ordered from highest to lowest as $A > B > C > D$, which means category $A$ is the highest and category $D$ is the lowest. While playing the game, players can increase the HP of the character. Now, Tokitsukaze wants you to increase her HP by at most $2$ (that is, either by $0$, $1$ or $2$). How much should she increase her HP so that it has the highest possible category? -----Input----- The only line contains a single integer $x$ ($30 \leq x \leq 100$) — the value Tokitsukaze's HP currently. -----Output----- Print an integer $a$ ($0 \leq a \leq 2$) and an uppercase letter $b$ ($b \in \lbrace A, B, C, D \rbrace$), representing that the best way is to increase her HP by $a$, and then the category becomes $b$. Note that the output characters are case-sensitive. -----Examples----- Input 33 Output 0 A Input 98 Output 1 B -----Note----- For the first example, the category of Tokitsukaze's HP is already $A$, so you don't need to enhance her ability. For the second example: If you don't increase her HP, its value is still $98$, which equals to $(4 \times 24 + 2)$, and its category is $C$. If you increase her HP by $1$, its value becomes $99$, which equals to $(4 \times 24 + 3)$, and its category becomes $B$. If you increase her HP by $2$, its value becomes $100$, which equals to $(4 \times 25)$, and its category becomes $D$. Therefore, the best way is to increase her HP by $1$ so that the category of her HP becomes $B$. 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%4==1: print(0,'A') elif n%4==2: print(1,'B') elif n%4==3: print(2,'A') elif n%4==0: print(1,'A') ```
vfc_14666
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1191/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "33\n", "output": "0 A\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "98\n", "output": "1 B\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1230
Solve the following coding problem using the programming language python: Bob is an active user of the social network Faithbug. On this network, people are able to engage in a mutual friendship. That is, if $a$ is a friend of $b$, then $b$ is also a friend of $a$. Each user thus has a non-negative amount of friends. This morning, somebody anonymously sent Bob the following link: graph realization problem and Bob wants to know who that was. In order to do that, he first needs to know how the social network looks like. He investigated the profile of every other person on the network and noted down the number of his friends. However, he neglected to note down the number of his friends. Help him find out how many friends he has. Since there may be many possible answers, print all of them. -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 5 \cdot 10^5$), the number of people on the network excluding Bob. The second line contains $n$ numbers $a_1,a_2, \dots, a_n$ ($0 \leq a_i \leq n$), with $a_i$ being the number of people that person $i$ is a friend of. -----Output----- Print all possible values of $a_{n+1}$ — the amount of people that Bob can be friend of, in increasing order. If no solution exists, output $-1$. -----Examples----- Input 3 3 3 3 Output 3 Input 4 1 1 1 1 Output 0 2 4 Input 2 0 2 Output -1 Input 35 21 26 18 4 28 2 15 13 16 25 6 32 11 5 31 17 9 3 24 33 14 27 29 1 20 4 12 7 10 30 34 8 19 23 22 Output 13 15 17 19 21 -----Note----- In the first test case, the only solution is that everyone is friends with everyone. That is why Bob should have $3$ friends. In the second test case, there are three possible solutions (apart from symmetries): $a$ is friend of $b$, $c$ is friend of $d$, and Bob has no friends, or $a$ is a friend of $b$ and both $c$ and $d$ are friends with Bob, or Bob is friends of everyone. The third case is impossible to solve, as the second person needs to be a friend with everybody, but the first one is a complete stranger. 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()) a=list(map(int,input().split())) a.sort(reverse=True) mod=sum(a)%2 counts=[0]*(n+1) for guy in a: counts[guy]+=1 cumcounts=[counts[0]] for i in range(n): cumcounts.append(cumcounts[-1]+counts[i+1]) partialsums=[0] curr=0 for i in range(n): curr+=(i+1)*counts[i+1] partialsums.append(curr) partialsums.append(0) cumcounts.append(0) sumi=0 diffs=[] altdiffs=[] for i in range(n): sumi+=a[i] rhs=i*(i+1) if a[i]>i: rhs+=partialsums[i]+(i+1)*(n-i-1-cumcounts[i]) else: rhs+=partialsums[a[i]-1]+a[i]*(n-i-1-cumcounts[a[i]-1]) diffs.append(sumi-rhs) rhs2=(i+1)*(i+2) if a[i]>i+1: rhs2+=partialsums[i+1]+(i+2)*(n-i-1-cumcounts[i+1]) else: rhs2+=partialsums[a[i]-1]+a[i]*(n-i-1-cumcounts[a[i]-1]) altdiffs.append(sumi-rhs2) mini=max(diffs) maxi=-max(altdiffs) mini=max(mini,0) maxi=min(maxi,n) out="" if mini%2!=mod: mini+=1 if maxi%2==mod: maxi+=1 for guy in range(mini,maxi,2): out+=str(guy)+" " if mini>maxi: print(-1) else: print(out) main() ```
vfc_14674
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1091/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n3 3 3\n", "output": "3 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n1 1 1 1\n", "output": "0 2 4 \n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1231
Solve the following coding problem using the programming language python: On her way to programming school tiger Dasha faced her first test — a huge staircase! [Image] The steps were numbered from one to infinity. As we know, tigers are very fond of all striped things, it is possible that it has something to do with their color. So on some interval of her way she calculated two values — the number of steps with even and odd numbers. You need to check whether there is an interval of steps from the l-th to the r-th (1 ≤ l ≤ r), for which values that Dasha has found are correct. -----Input----- In the only line you are given two integers a, b (0 ≤ a, b ≤ 100) — the number of even and odd steps, accordingly. -----Output----- In the only line print "YES", if the interval of steps described above exists, and "NO" otherwise. -----Examples----- Input 2 3 Output YES Input 3 1 Output NO -----Note----- In the first example one of suitable intervals is from 1 to 5. The interval contains two even steps — 2 and 4, and three odd: 1, 3 and 5. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python a,b=list(map(int,input().split())) print("YES"if abs(a-b)<=1 and a+b>0 else"NO") ```
vfc_14678
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/761/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2 3\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 1\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 4\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "9 9\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "85 95\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "0 1\n", "output": "YES\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1233
Solve the following coding problem using the programming language python: Petya is a beginner programmer. He has already mastered the basics of the C++ language and moved on to learning algorithms. The first algorithm he encountered was insertion sort. Petya has already written the code that implements this algorithm and sorts the given integer zero-indexed array a of size n in the non-decreasing order. for (int i = 1; i < n; i = i + 1) { int j = i; while (j > 0 && a[j] < a[j - 1]) { swap(a[j], a[j - 1]); // swap elements a[j] and a[j - 1] j = j - 1; } } Petya uses this algorithm only for sorting of arrays that are permutations of numbers from 0 to n - 1. He has already chosen the permutation he wants to sort but he first decided to swap some two of its elements. Petya wants to choose these elements in such a way that the number of times the sorting executes function swap, was minimum. Help Petya find out the number of ways in which he can make the swap and fulfill this requirement. It is guaranteed that it's always possible to swap two elements of the input permutation in such a way that the number of swap function calls decreases. -----Input----- The first line contains a single integer n (2 ≤ n ≤ 5000) — the length of the permutation. The second line contains n different integers from 0 to n - 1, inclusive — the actual permutation. -----Output----- Print two integers: the minimum number of times the swap function is executed and the number of such pairs (i, j) that swapping the elements of the input permutation with indexes i and j leads to the minimum number of the executions. -----Examples----- Input 5 4 0 3 1 2 Output 3 2 Input 5 1 2 3 4 0 Output 3 4 -----Note----- In the first sample the appropriate pairs are (0, 3) and (0, 4). In the second sample the appropriate pairs are (0, 4), (1, 4), (2, 4) and (3, 4). The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python arr = [0 for i in range(5001)] def insertion_sort(n, a): def modify(t): while t > 0: arr[t] += 1 t -= t & (-t) def query(t): res = 0 while t < 5001: res += arr[t] t += t & (-t) return res s = 0 ans = 0 way = 0 for i in range(n): a[i] += 1 for i in range(n): nonlocal arr arr = [0 for j in range(5001)] for j in range(i + 1, n): if a[i] < a[j]: continue s += 1 tmp = 1 + 2 * query(a[j]) if tmp > ans: ans = tmp way = 1 elif tmp == ans: way += 1 modify(a[j]) return s - ans, way def __starting_point(): n = int(input()) a = list(map(int, input().split())) result = insertion_sort(n, a) print(*result) __starting_point() ```
vfc_14686
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/362/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n4 0 3 1 2\n", "output": "3 2\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1234
Solve the following coding problem using the programming language python: An array $b$ is called to be a subarray of $a$ if it forms a continuous subsequence of $a$, that is, if it is equal to $a_l$, $a_{l + 1}$, $\ldots$, $a_r$ for some $l, r$. Suppose $m$ is some known constant. For any array, having $m$ or more elements, let's define it's beauty as the sum of $m$ largest elements of that array. For example: For array $x = [4, 3, 1, 5, 2]$ and $m = 3$, the $3$ largest elements of $x$ are $5$, $4$ and $3$, so the beauty of $x$ is $5 + 4 + 3 = 12$. For array $x = [10, 10, 10]$ and $m = 2$, the beauty of $x$ is $10 + 10 = 20$. You are given an array $a_1, a_2, \ldots, a_n$, the value of the said constant $m$ and an integer $k$. Your need to split the array $a$ into exactly $k$ subarrays such that: Each element from $a$ belongs to exactly one subarray. Each subarray has at least $m$ elements. The sum of all beauties of $k$ subarrays is maximum possible. -----Input----- The first line contains three integers $n$, $m$ and $k$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m$, $2 \le k$, $m \cdot k \le n$) — the number of elements in $a$, the constant $m$ in the definition of beauty and the number of subarrays to split to. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$). -----Output----- In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition. In the second line, print $k-1$ integers $p_1, p_2, \ldots, p_{k-1}$ ($1 \le p_1 < p_2 < \ldots < p_{k-1} < n$) representing the partition of the array, in which: All elements with indices from $1$ to $p_1$ belong to the first subarray. All elements with indices from $p_1 + 1$ to $p_2$ belong to the second subarray. $\ldots$. All elements with indices from $p_{k-1} + 1$ to $n$ belong to the last, $k$-th subarray. If there are several optimal partitions, print any of them. -----Examples----- Input 9 2 3 5 2 5 2 4 1 1 3 2 Output 21 3 5 Input 6 1 4 4 1 3 2 2 3 Output 12 1 3 5 Input 2 1 2 -1000000000 1000000000 Output 0 1 -----Note----- In the first example, one of the optimal partitions is $[5, 2, 5]$, $[2, 4]$, $[1, 1, 3, 2]$. The beauty of the subarray $[5, 2, 5]$ is $5 + 5 = 10$. The beauty of the subarray $[2, 4]$ is $2 + 4 = 6$. The beauty of the subarray $[1, 1, 3, 2]$ is $3 + 2 = 5$. The sum of their beauties is $10 + 6 + 5 = 21$. In the second example, one optimal partition is $[4]$, $[1, 3]$, $[2, 2]$, $[3]$. 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 python # -*- coding: utf-8 -*- from collections import deque import heapq import math import bisect def main(): N, M, K = list(map(int, input().split())) A = list(map(int, input().split())) B = [(A[i], i) for i in range(N)] B.sort(reverse=True) used = [0] * N ans = 0 for i in range(M*K): idx = B[i][1] used[idx] = 1 ans += B[i][0] lst = [] cnt = le = 0 for i in range(N): if used[i]: cnt += 1 if cnt == M: lst.append(i+1) cnt = 0 le += 1 if le == K - 1: break print(ans) print(*lst) def __starting_point(): main() __starting_point() ```
vfc_14690
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1114/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "9 2 3\n5 2 5 2 4 1 1 3 2\n", "output": "21\n3 5 ", "type": "stdin_stdout" }, { "fn_name": null, "input": "6 1 4\n4 1 3 2 2 3\n", "output": "12\n1 3 5 ", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 1 2\n-1000000000 1000000000\n", "output": "0\n1 ", "type": "stdin_stdout" }, { "fn_name": null, "input": "11 3 3\n-6 9 10 -9 -7 -2 -1 -8 0 2 7\n", "output": "12\n3 7 ", "type": "stdin_stdout" }, { "fn_name": null, "input": "37 3 10\n74 42 92 -64 -11 -37 63 81 -58 -88 52 -6 40 -24 29 -10 -23 41 -36 -53 1 94 -65 47 87 -40 -84 -65 -1 99 35 51 40 -21 36 84 -48\n", "output": "831\n3 7 12 15 18 22 26 31 34 ", "type": "stdin_stdout" }, { "fn_name": null, "input": "77 4 11\n354 14 -200 44 -872 -822 568 256 -286 -571 180 113 -860 -509 -225 -305 358 717 -632 -267 967 -283 -630 -238 17 -77 -156 718 634 -444 189 -680 -364 208 191 -528 -732 529 108 -426 771 285 -795 -740 984 -123 -322 546 429 852 -242 -742 166 -224 81 637 868 -169 -762 -151 -464 -380 -963 -702 312 -334 28 124 -40 -384 -970 -539 -61 -100 -182 509 339\n", "output": "11613\n4 12 25 29 38 45 50 56 65 73 ", "type": "stdin_stdout" } ] }
apps
verifiable_code
1235
Solve the following coding problem using the programming language python: You are given an array $A$, consisting of $n$ positive integers $a_1, a_2, \dots, a_n$, and an array $B$, consisting of $m$ positive integers $b_1, b_2, \dots, b_m$. Choose some element $a$ of $A$ and some element $b$ of $B$ such that $a+b$ doesn't belong to $A$ and doesn't belong to $B$. For example, if $A = [2, 1, 7]$ and $B = [1, 3, 4]$, we can choose $1$ from $A$ and $4$ from $B$, as number $5 = 1 + 4$ doesn't belong to $A$ and doesn't belong to $B$. However, we can't choose $2$ from $A$ and $1$ from $B$, as $3 = 2 + 1$ belongs to $B$. It can be shown that such a pair exists. If there are multiple answers, print any. Choose and print any such two numbers. -----Input----- The first line contains one integer $n$ ($1\le n \le 100$) — the number of elements of $A$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 200$) — the elements of $A$. The third line contains one integer $m$ ($1\le m \le 100$) — the number of elements of $B$. The fourth line contains $m$ different integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le 200$) — the elements of $B$. It can be shown that the answer always exists. -----Output----- Output two numbers $a$ and $b$ such that $a$ belongs to $A$, $b$ belongs to $B$, but $a+b$ doesn't belong to nor $A$ neither $B$. If there are multiple answers, print any. -----Examples----- Input 1 20 2 10 20 Output 20 20 Input 3 3 2 2 5 1 5 7 7 9 Output 3 1 Input 4 1 3 5 7 4 7 5 3 1 Output 1 1 -----Note----- In the first example, we can choose $20$ from array $[20]$ and $20$ from array $[10, 20]$. Number $40 = 20 + 20$ doesn't belong to any of those arrays. However, it is possible to choose $10$ from the second array too. In the second example, we can choose $3$ from array $[3, 2, 2]$ and $1$ from array $[1, 5, 7, 7, 9]$. Number $4 = 3 + 1$ doesn't belong to any of those arrays. In the third example, we can choose $1$ from array $[1, 3, 5, 7]$ and $1$ from array $[7, 5, 3, 1]$. Number $2 = 1 + 1$ doesn't belong to any of those arrays. 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 = int(input()) B = list(map(int, input().split())) print(max(A), max(B)) ```
vfc_14694
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1206/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1\n20\n2\n10 20\n", "output": "20 20", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n3 2 2\n5\n1 5 7 7 9\n", "output": "3 9", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n1 3 5 7\n4\n7 5 3 1\n", "output": "7 7", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n1\n10\n1 2 3 4 5 6 7 8 9 10\n", "output": "1 10", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n148\n1\n40\n", "output": "148 40", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n77 112 81 91\n8\n183 174 187 111 121 21 129 28\n", "output": "112 187", "type": "stdin_stdout" } ] }
apps
verifiable_code
1236
Solve the following coding problem using the programming language python: There are n cities in Westeros. The i-th city is inhabited by a_{i} people. Daenerys and Stannis play the following game: in one single move, a player chooses a certain town and burns it to the ground. Thus all its residents, sadly, die. Stannis starts the game. The game ends when Westeros has exactly k cities left. The prophecy says that if the total number of surviving residents is even, then Daenerys wins: Stannis gets beheaded, and Daenerys rises on the Iron Throne. If the total number of surviving residents is odd, Stannis wins and everything goes in the completely opposite way. Lord Petyr Baelish wants to know which candidates to the throne he should support, and therefore he wonders, which one of them has a winning strategy. Answer to this question of Lord Baelish and maybe you will become the next Lord of Harrenholl. -----Input----- The first line contains two positive space-separated integers, n and k (1 ≤ k ≤ n ≤ 2·10^5) — the initial number of cities in Westeros and the number of cities at which the game ends. The second line contains n space-separated positive integers a_{i} (1 ≤ a_{i} ≤ 10^6), which represent the population of each city in Westeros. -----Output----- Print string "Daenerys" (without the quotes), if Daenerys wins and "Stannis" (without the quotes), if Stannis wins. -----Examples----- Input 3 1 1 2 1 Output Stannis Input 3 1 2 2 1 Output Daenerys Input 6 3 5 20 12 7 14 101 Output Stannis -----Note----- In the first sample Stannis will use his move to burn a city with two people and Daenerys will be forced to burn a city with one resident. The only survivor city will have one resident left, that is, the total sum is odd, and thus Stannis wins. In the second sample, if Stannis burns a city with two people, Daenerys burns the city with one resident, or vice versa. In any case, the last remaining city will be inhabited by two people, that is, the total sum is even, and hence Daenerys wins. 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, k = list(map(int, input().split())) even = 0 odd = 0 for elem in input().split(): if int(elem) % 2 == 0: even += 1 else: odd += 1 turns = n - k if turns == 0: if odd % 2 == 1: return "Stannis" else: return "Daenerys" if turns % 2 == 0: if k % 2 == 1 and even <= turns // 2: return "Stannis" else: return "Daenerys" else: if k % 2 == 0 and even <= turns // 2 or odd <= turns // 2: return "Daenerys" else: return "Stannis" print(main()) ```
vfc_14698
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/549/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 1\n1 2 1\n", "output": "Stannis\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 1\n2 2 1\n", "output": "Daenerys\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6 3\n5 20 12 7 14 101\n", "output": "Stannis\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6 3\n346 118 330 1403 5244 480\n", "output": "Daenerys\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1237
Solve the following coding problem using the programming language python: Saitama accidentally destroyed a hotel again. To repay the hotel company, Genos has volunteered to operate an elevator in one of its other hotels. The elevator is special — it starts on the top floor, can only move down, and has infinite capacity. Floors are numbered from 0 to s and elevator initially starts on floor s at time 0. The elevator takes exactly 1 second to move down exactly 1 floor and negligible time to pick up passengers. Genos is given a list detailing when and on which floor passengers arrive. Please determine how long in seconds it will take Genos to bring all passengers to floor 0. -----Input----- The first line of input contains two integers n and s (1 ≤ n ≤ 100, 1 ≤ s ≤ 1000) — the number of passengers and the number of the top floor respectively. The next n lines each contain two space-separated integers f_{i} and t_{i} (1 ≤ f_{i} ≤ s, 1 ≤ t_{i} ≤ 1000) — the floor and the time of arrival in seconds for the passenger number i. -----Output----- Print a single integer — the minimum amount of time in seconds needed to bring all the passengers to floor 0. -----Examples----- Input 3 7 2 1 3 8 5 2 Output 11 Input 5 10 2 77 3 33 8 21 9 12 10 64 Output 79 -----Note----- In the first sample, it takes at least 11 seconds to bring all passengers to floor 0. Here is how this could be done: 1. Move to floor 5: takes 2 seconds. 2. Pick up passenger 3. 3. Move to floor 3: takes 2 seconds. 4. Wait for passenger 2 to arrive: takes 4 seconds. 5. Pick up passenger 2. 6. Go to floor 2: takes 1 second. 7. Pick up passenger 1. 8. Go to floor 0: takes 2 seconds. This gives a total of 2 + 2 + 4 + 1 + 2 = 11 seconds. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, up = map(int, input().split()) res = 0 for i in range(n): fl, t = map(int, input().split()) res = max(res, max(t, up - fl) + fl) print(res) ```
vfc_14702
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/608/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 7\n2 1\n3 8\n5 2\n", "output": "11\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 10\n2 77\n3 33\n8 21\n9 12\n10 64\n", "output": "79\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1000\n1000 1000\n", "output": "2000\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1238
Solve the following coding problem using the programming language python: There was an electronic store heist last night. All keyboards which were in the store yesterday were numbered in ascending order from some integer number $x$. For example, if $x = 4$ and there were $3$ keyboards in the store, then the devices had indices $4$, $5$ and $6$, and if $x = 10$ and there were $7$ of them then the keyboards had indices $10$, $11$, $12$, $13$, $14$, $15$ and $16$. After the heist, only $n$ keyboards remain, and they have indices $a_1, a_2, \dots, a_n$. Calculate the minimum possible number of keyboards that have been stolen. The staff remember neither $x$ nor the number of keyboards in the store before the heist. -----Input----- The first line contains single integer $n$ $(1 \le n \le 1\,000)$ — the number of keyboards in the store that remained after the heist. The second line contains $n$ distinct integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^{9})$ — the indices of the remaining keyboards. The integers $a_i$ are given in arbitrary order and are pairwise distinct. -----Output----- Print the minimum possible number of keyboards that have been stolen if the staff remember neither $x$ nor the number of keyboards in the store before the heist. -----Examples----- Input 4 10 13 12 8 Output 2 Input 5 7 5 6 4 8 Output 0 -----Note----- In the first example, if $x=8$ then minimum number of stolen keyboards is equal to $2$. The keyboards with indices $9$ and $11$ were stolen during the heist. In the second example, if $x=4$ then nothing was stolen during the heist. 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 = sorted(list(map(int, input().split()))) ans = 0 for i in range(1, n): ans += a[i] - a[i - 1] - 1 print(ans) ```
vfc_14706
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1041/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n10 13 12 8\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n7 5 6 4 8\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n2\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n1000000000 500000000 2\n", "output": "999999996\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "30\n793 656 534 608 971 970 670 786 978 665 92 391 328 228 340 681 495 175 659 520 179 396 554 481 631 468 799 390 563 471\n", "output": "857\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1239
Solve the following coding problem using the programming language python: There are n cities situated along the main road of Berland. Cities are represented by their coordinates — integer numbers a_1, a_2, ..., a_{n}. All coordinates are pairwise distinct. It is possible to get from one city to another only by bus. But all buses and roads are very old, so the Minister of Transport decided to build a new bus route. The Minister doesn't want to spend large amounts of money — he wants to choose two cities in such a way that the distance between them is minimal possible. The distance between two cities is equal to the absolute value of the difference between their coordinates. It is possible that there are multiple pairs of cities with minimal possible distance, so the Minister wants to know the quantity of such pairs. Your task is to write a program that will calculate the minimal possible distance between two pairs of cities and the quantity of pairs which have this distance. -----Input----- The first line contains one integer number n (2 ≤ n ≤ 2·10^5). The second line contains n integer numbers a_1, a_2, ..., a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). All numbers a_{i} are pairwise distinct. -----Output----- Print two integer numbers — the minimal distance and the quantity of pairs with this distance. -----Examples----- Input 4 6 -3 0 4 Output 2 1 Input 3 -2 0 2 Output 2 2 -----Note----- In the first example the distance between the first city and the fourth city is |4 - 6| = 2, and it is the only pair with this distance. 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(x) for x in input().split()] a.sort() diffs = [a[i] - a[i-1] for i in range(1, n)] diffs.sort() cur = 0 while cur < n - 1 and diffs[cur] == diffs[0]: cur += 1 print(diffs[0], cur) ```
vfc_14710
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/792/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n6 -3 0 4\n", "output": "2 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n-2 0 2\n", "output": "2 2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n1 2\n", "output": "1 1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1240
Solve the following coding problem using the programming language python: Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of l_{i} soldiers, who start to march from left leg, and r_{i} soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values l_{i} and r_{i}. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. -----Input----- The first line contains single integer n (1 ≤ n ≤ 10^5) — the number of columns. The next n lines contain the pairs of integers l_{i} and r_{i} (1 ≤ l_{i}, r_{i} ≤ 500) — the number of soldiers in the i-th column which start to march from the left or the right leg respectively. -----Output----- Print single integer k — the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. -----Examples----- Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 -----Note----- In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg — 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg — 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python 3 n = int(input()) l = [] r = [] for i in range(n): a, b = list(map(int, input().split())) l.append(a) r.append(b) L = sum(l) R = sum(r) mx = abs(L - R) k = 0 for i in range(n): Lp = L - l[i] + r[i] Rp = R - r[i] + l[i] if abs(Lp - Rp) > mx: mx = abs(Lp - Rp) k = i + 1 print(k) ```
vfc_14714
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/733/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n5 6\n8 9\n10 3\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n6 5\n5 6\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n5 9\n1 3\n4 8\n4 5\n23 54\n12 32\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n500 499\n500 500\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n139 252\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n18 18\n71 471\n121 362\n467 107\n138 254\n13 337\n499 373\n337 387\n147 417\n76 417\n", "output": "4\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1241
Solve the following coding problem using the programming language python: You are given an array a with n elements. Each element of a is either 0 or 1. Let's denote the length of the longest subsegment of consecutive elements in a, consisting of only numbers one, as f(a). You can change no more than k zeroes to ones to maximize f(a). -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 3·10^5, 0 ≤ k ≤ n) — the number of elements in a and the parameter k. The second line contains n integers a_{i} (0 ≤ a_{i} ≤ 1) — the elements of a. -----Output----- On the first line print a non-negative integer z — the maximal value of f(a) after no more than k changes of zeroes to ones. On the second line print n integers a_{j} — the elements of the array a after the changes. If there are multiple answers, you can print any one of them. -----Examples----- Input 7 1 1 0 0 1 1 0 1 Output 4 1 0 0 1 1 1 1 Input 10 2 1 0 0 1 0 1 0 1 0 1 Output 5 1 0 0 1 1 1 1 1 0 1 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: n, k = map(int, input().split()) a = list(map(int, input().split())) left = 0 result = -1 cur = 0 for i in range(n): if not a[i]: if k: k -= 1 else: if i - left > result: res_left = left res_right = i result = i - left while left < i and a[left]: left += 1 left += 1 if i + 1 - left > result: res_left = left res_right = i + 1 result = i + 1 - left print(result) for i in range(res_left): print(a[i], end=' ') for i in range(result): print(end="1 ") for i in range(res_right, n): print(a[i], end=' ') print() except EOFError: pass ```
vfc_14718
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/660/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "7 1\n1 0 0 1 1 0 1\n", "output": "4\n1 0 0 1 1 1 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 2\n1 0 0 1 0 1 0 1 0 1\n", "output": "5\n1 0 0 1 1 1 1 1 0 1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1242
Solve the following coding problem using the programming language python: IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting. Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from $1$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing? A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first and the only line contains a string $s$ ($1 \le |s| \le 1000$), describing the initial string formed by magnets. The string $s$ consists only of characters 'a' and 'b'. -----Output----- Output exactly $|s|$ integers. If IA should reverse the $i$-th prefix (that is, the substring from $1$ to $i$), the $i$-th integer should be equal to $1$, and it should be equal to $0$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them. -----Examples----- Input bbab Output 0 1 1 0 Input aaaaa Output 1 0 0 0 1 -----Note----- In the first example, IA can reverse the second and the third prefix and get a string "abbb". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string. In the second example, she can reverse any subset of prefixes — all letters are 'a'. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python s = input() arr_ans = [0] * len(s) for i in range(1, len(s)): if s[i] == 'a': arr_ans[i - 1] = (arr_ans[i - 1] + 1) % 2 arr_ans[i] += 1 print(*arr_ans) ```
vfc_14722
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1043/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "bbab\n", "output": "0 1 1 0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "aaaaa\n", "output": "1 0 0 0 1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1243
Solve the following coding problem using the programming language python: Petya has k matches, placed in n matchboxes lying in a line from left to right. We know that k is divisible by n. Petya wants all boxes to have the same number of matches inside. For that, he can move a match from its box to the adjacent one in one move. How many such moves does he need to achieve the desired configuration? -----Input----- The first line contains integer n (1 ≤ n ≤ 50000). The second line contains n non-negative numbers that do not exceed 10^9, the i-th written number is the number of matches in the i-th matchbox. It is guaranteed that the total number of matches is divisible by n. -----Output----- Print the total minimum number of moves. -----Examples----- Input 6 1 6 2 5 3 7 Output 12 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python """ Codeforces Testing Round 10 Problem B 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, = read() a = read() s = sum(a) // n r = 0 for i in range(n-1): if a[i] < s: r += s - a[i] a[i+1] -= s - a[i] else: r += a[i] - s a[i+1] += a[i] - s print(r) ```
vfc_14726
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/440/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6\n1 6 2 5 3 7\n", "output": "12\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n6 6 6 0 0 0\n", "output": "27\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n0 0 0 6 6 6\n", "output": "27\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n6 6 0 0 6 6\n", "output": "12\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n0 0 0 0 0\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1244
Solve the following coding problem using the programming language python: Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring array elements. Now Yaroslav is wondering if he can obtain an array where any two neighboring elements would be distinct in a finite time. Help Yaroslav. -----Input----- The first line contains integer n (1 ≤ n ≤ 100) — the number of elements in the array. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 1000) — the array elements. -----Output----- In the single line print "YES" (without the quotes) if Yaroslav can obtain the array he needs, and "NO" (without the quotes) otherwise. -----Examples----- Input 1 1 Output YES Input 3 1 1 2 Output YES Input 4 7 7 7 7 Output NO -----Note----- In the first sample the initial array fits well. In the second sample Yaroslav can get array: 1, 2, 1. He can swap the last and the second last elements to obtain it. In the third sample Yarosav can't get the array he needs. 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()) mas = list(map(int, input().split())) mas2 = [0 for _ in range(1001)] for i in mas: mas2[i]+=1 i = 0 for i in mas2: if i > (n + 1) / 2: print ("NO") return print ("YES") ```
vfc_14730
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/296/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1\n1\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n1 1 2\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n7 7 7 7\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n479 170 465 146\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n996 437 605 996 293\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n727 539 896 668 36 896\n", "output": "YES\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1246
Solve the following coding problem using the programming language python: Petya has recently learned data structure named "Binary heap". The heap he is now operating with allows the following operations: put the given number into the heap; get the value of the minimum element in the heap; extract the minimum element from the heap; Thus, at any moment of time the heap contains several integers (possibly none), some of them might be equal. In order to better learn this data structure Petya took an empty heap and applied some operations above to it. Also, he carefully wrote down all the operations and their results to his event log, following the format: insert x — put the element with value x in the heap; getMin x — the value of the minimum element contained in the heap was equal to x; removeMin — the minimum element was extracted from the heap (only one instance, if there were many). All the operations were correct, i.e. there was at least one element in the heap each time getMin or removeMin operations were applied. While Petya was away for a lunch, his little brother Vova came to the room, took away some of the pages from Petya's log and used them to make paper boats. Now Vova is worried, if he made Petya's sequence of operations inconsistent. For example, if one apply operations one-by-one in the order they are written in the event log, results of getMin operations might differ from the results recorded by Petya, and some of getMin or removeMin operations may be incorrect, as the heap is empty at the moment they are applied. Now Vova wants to add some new operation records to the event log in order to make the resulting sequence of operations correct. That is, the result of each getMin operation is equal to the result in the record, and the heap is non-empty when getMin ad removeMin are applied. Vova wants to complete this as fast as possible, as the Petya may get back at any moment. He asks you to add the least possible number of operation records to the current log. Note that arbitrary number of operations may be added at the beginning, between any two other operations, or at the end of the log. -----Input----- The first line of the input contains the only integer n (1 ≤ n ≤ 100 000) — the number of the records left in Petya's journal. Each of the following n lines describe the records in the current log in the order they are applied. Format described in the statement is used. All numbers in the input are integers not exceeding 10^9 by their absolute value. -----Output----- The first line of the output should contain a single integer m — the minimum possible number of records in the modified sequence of operations. Next m lines should contain the corrected sequence of records following the format of the input (described in the statement), one per line and in the order they are applied. All the numbers in the output should be integers not exceeding 10^9 by their absolute value. Note that the input sequence of operations must be the subsequence of the output sequence. It's guaranteed that there exists the correct answer consisting of no more than 1 000 000 operations. -----Examples----- Input 2 insert 3 getMin 4 Output 4 insert 3 removeMin insert 4 getMin 4 Input 4 insert 1 insert 1 removeMin getMin 2 Output 6 insert 1 insert 1 removeMin removeMin insert 2 getMin 2 -----Note----- In the first sample, after number 3 is inserted into the heap, the minimum number is 3. To make the result of the first getMin equal to 4 one should firstly remove number 3 from the heap and then add number 4 into the heap. In the second sample case number 1 is inserted two times, so should be similarly removed twice. 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 * n=int(input()) q,ans,k=[],[],0 for i in range(n): ss=input() if ss!="removeMin": s,mm=ss.split(); m=int(mm) if s=='insert': k+=1 heappush(q,m) else: while k==0 or q[0]!=m: if k==0: heappush(q,m) ans+=['insert '+mm] k+=1 elif q[0]<m: k-=1 t=heappop(q) ans+=['removeMin'] else: k+=1 heappush(q,m) ans+=['insert '+mm] else: if k==0: ans+=['insert 1'] else: heappop(q) k-=1 ans+=[ss] print(len(ans)) print('\n'.join(ans)) ```
vfc_14738
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/681/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\ninsert 3\ngetMin 4\n", "output": "4\ninsert 3\nremoveMin\ninsert 4\ngetMin 4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\ninsert 1\ninsert 1\nremoveMin\ngetMin 2\n", "output": "6\ninsert 1\ninsert 1\nremoveMin\nremoveMin\ninsert 2\ngetMin 2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\ninsert 1\n", "output": "1\ninsert 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\ngetMin 31\n", "output": "2\ninsert 31\ngetMin 31\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\nremoveMin\n", "output": "2\ninsert 0\nremoveMin\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1247
Solve the following coding problem using the programming language python: The Little Girl loves problems on games very much. Here's one of them. Two players have got a string s, consisting of lowercase English letters. They play a game that is described by the following rules: The players move in turns; In one move the player can remove an arbitrary letter from string s. If the player before his turn can reorder the letters in string s so as to get a palindrome, this player wins. A palindrome is a string that reads the same both ways (from left to right, and vice versa). For example, string "abba" is a palindrome and string "abc" isn't. Determine which player will win, provided that both sides play optimally well — the one who moves first or the one who moves second. -----Input----- The input contains a single line, containing string s (1 ≤ |s| ≤ 10^3). String s consists of lowercase English letters. -----Output----- In a single line print word "First" if the first player wins (provided that both players play optimally well). Otherwise, print word "Second". Print the words without the quotes. -----Examples----- Input aba Output First Input abca Output Second The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def __starting_point(): s = input() c = [s.count(x) for x in set(s)] total = 0 for x in c: if x % 2 != 0: total += 1 if total % 2 == 0 and total != 0: print("Second") else: print("First") __starting_point() ```
vfc_14742
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/276/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "aba\n", "output": "First\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "abca\n", "output": "Second\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "aabb\n", "output": "First\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1248
Solve the following coding problem using the programming language python: Today Patrick waits for a visit from his friend Spongebob. To prepare for the visit, Patrick needs to buy some goodies in two stores located near his house. There is a d_1 meter long road between his house and the first shop and a d_2 meter long road between his house and the second shop. Also, there is a road of length d_3 directly connecting these two shops to each other. Help Patrick calculate the minimum distance that he needs to walk in order to go to both shops and return to his house. [Image] Patrick always starts at his house. He should visit both shops moving only along the three existing roads and return back to his house. He doesn't mind visiting the same shop or passing the same road multiple times. The only goal is to minimize the total distance traveled. -----Input----- The first line of the input contains three integers d_1, d_2, d_3 (1 ≤ d_1, d_2, d_3 ≤ 10^8) — the lengths of the paths. d_1 is the length of the path connecting Patrick's house and the first shop; d_2 is the length of the path connecting Patrick's house and the second shop; d_3 is the length of the path connecting both shops. -----Output----- Print the minimum distance that Patrick will have to walk in order to visit both shops and return to his house. -----Examples----- Input 10 20 30 Output 60 Input 1 1 5 Output 4 -----Note----- The first sample is shown on the picture in the problem statement. One of the optimal routes is: house $\rightarrow$ first shop $\rightarrow$ second shop $\rightarrow$ house. In the second sample one of the optimal routes is: house $\rightarrow$ first shop $\rightarrow$ house $\rightarrow$ second shop $\rightarrow$ house. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python d1, d2, d3 = map(int, input().split()) D1 = 2*d1 + 2*d2 D2 = d1 + d3 + d2 D3 = (d1 + d3) * 2 D4 = (d2 + d3) * 2 print(min(D1, D2, D3, D4)) ```
vfc_14746
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/599/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "10 20 30\n", "output": "60\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1 5\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "100 33 34\n", "output": "134\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "777 777 777\n", "output": "2331\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 2 8\n", "output": "8\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "12 34 56\n", "output": "92\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1249
Solve the following coding problem using the programming language python: At the first holiday in spring, the town Shortriver traditionally conducts a flower festival. Townsfolk wear traditional wreaths during these festivals. Each wreath contains exactly $k$ flowers. The work material for the wreaths for all $n$ citizens of Shortriver is cut from the longest flowered liana that grew in the town that year. Liana is a sequence $a_1$, $a_2$, ..., $a_m$, where $a_i$ is an integer that denotes the type of flower at the position $i$. This year the liana is very long ($m \ge n \cdot k$), and that means every citizen will get a wreath. Very soon the liana will be inserted into a special cutting machine in order to make work material for wreaths. The machine works in a simple manner: it cuts $k$ flowers from the beginning of the liana, then another $k$ flowers and so on. Each such piece of $k$ flowers is called a workpiece. The machine works until there are less than $k$ flowers on the liana. Diana has found a weaving schematic for the most beautiful wreath imaginable. In order to weave it, $k$ flowers must contain flowers of types $b_1$, $b_2$, ..., $b_s$, while other can be of any type. If a type appears in this sequence several times, there should be at least that many flowers of that type as the number of occurrences of this flower in the sequence. The order of the flowers in a workpiece does not matter. Diana has a chance to remove some flowers from the liana before it is inserted into the cutting machine. She can remove flowers from any part of the liana without breaking liana into pieces. If Diana removes too many flowers, it may happen so that some of the citizens do not get a wreath. Could some flowers be removed from the liana so that at least one workpiece would conform to the schematic and machine would still be able to create at least $n$ workpieces? -----Input----- The first line contains four integers $m$, $k$, $n$ and $s$ ($1 \le n, k, m \le 5 \cdot 10^5$, $k \cdot n \le m$, $1 \le s \le k$): the number of flowers on the liana, the number of flowers in one wreath, the amount of citizens and the length of Diana's flower sequence respectively. The second line contains $m$ integers $a_1$, $a_2$, ..., $a_m$ ($1 \le a_i \le 5 \cdot 10^5$)  — types of flowers on the liana. The third line contains $s$ integers $b_1$, $b_2$, ..., $b_s$ ($1 \le b_i \le 5 \cdot 10^5$)  — the sequence in Diana's schematic. -----Output----- If it's impossible to remove some of the flowers so that there would be at least $n$ workpieces and at least one of them fullfills Diana's schematic requirements, output $-1$. Otherwise in the first line output one integer $d$  — the number of flowers to be removed by Diana. In the next line output $d$ different integers  — the positions of the flowers to be removed. If there are multiple answers, print any. -----Examples----- Input 7 3 2 2 1 2 3 3 2 1 2 2 2 Output 1 4 Input 13 4 3 3 3 2 6 4 1 4 4 7 1 3 3 2 4 4 3 4 Output -1 Input 13 4 1 3 3 2 6 4 1 4 4 7 1 3 3 2 4 4 3 4 Output 9 1 2 3 4 5 9 11 12 13 -----Note----- In the first example, if you don't remove any flowers, the machine would put out two workpieces with flower types $[1, 2, 3]$ and $[3, 2, 1]$. Those workpieces don't fit Diana's schematic. But if you remove flower on $4$-th place, the machine would output workpieces $[1, 2, 3]$ and $[2, 1, 2]$. The second workpiece fits Diana's schematic. In the second example there is no way to remove flowers so that every citizen gets a wreath and Diana gets a workpiece that fits here schematic. In the third example Diana is the only citizen of the town and that means she can, for example, just remove all flowers except the ones she needs. 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, s = map(int, input().split()) a = list(map(int, input().split())) c = list(map(int, input().split())) need = len(c) rez = n - m * k kek = [0 for i in range(500007)] cur_kek = [0 for i in range(500007)] for i in c: kek[i] += 1 r = 0 if (rez == 0): lol = need for i in range(0, n, k): for j in range(i, i + k): if kek[a[j]] > cur_kek[a[j]]: need -= 1 cur_kek[a[j]] += 1 if (need == 0): print(0) break for j in range(i, i + k): cur_kek[a[j]] = 0 else: print(-1) return meshayut = 0 if kek[a[0]] else 1 if kek[a[0]]: need -= 1 cur_kek[a[0]] += 1 ans = [] for l in range(n): while need > 0 and r < n - 1: r += 1 if (kek[a[r]] > cur_kek[a[r]]): need -= 1 cur_kek[a[r]] += 1 else: cur_kek[a[r]] += 1 meshayut += 1 #print(r, need) need_to_cut = l % k cur = r - l + 1 razn = cur - k #print(l, r, need_to_cut, razn, meshayut, cur, need) #print(need, razn + need_to_cut, rez, meshayut + not_useful, razn + need_to_cut) if (need == 0 and razn + need_to_cut <= rez and meshayut >= razn): rezhem = razn for j in range(l - need_to_cut, l): ans.append(j + 1) for j in range(l, r + 1): if kek[a[j]]: kek[a[j]] -= 1 elif rezhem: ans.append(j + 1) rezhem -= 1 print(len(ans)) print(' '.join(map(str, ans))) break if (kek[a[l]]): if cur_kek[a[l]] > kek[a[l]]: meshayut -= 1 else: need += 1 else: meshayut -= 1 cur_kek[a[l]] -= 1 else: print(-1) ```
vfc_14750
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1112/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "7 3 2 2\n1 2 3 3 2 1 2\n2 2\n", "output": "1\n4 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "13 4 3 3\n3 2 6 4 1 4 4 7 1 3 3 2 4\n4 3 4\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "13 4 1 3\n3 2 6 4 1 4 4 7 1 3 3 2 4\n4 3 4\n", "output": "2\n2 3 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1 1 1\n1\n1\n", "output": "0\n\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1 1 1\n1\n2\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 1 1 1\n1 2\n2\n", "output": "1\n1 \n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1250
Solve the following coding problem using the programming language python: Little boy Valera studies an algorithm of sorting an integer array. After studying the theory, he went on to the practical tasks. As a result, he wrote a program that sorts an array of n integers a_1, a_2, ..., a_{n} in the non-decreasing order. The pseudocode of the program, written by Valera, is given below. The input of the program gets number n and array a. loop integer variable i from 1 to n - 1     loop integer variable j from i to n - 1         if (a_{j} > a_{j} + 1), then swap the values of elements a_{j} and a_{j} + 1 But Valera could have made a mistake, because he hasn't yet fully learned the sorting algorithm. If Valera made a mistake in his program, you need to give a counter-example that makes his program work improperly (that is, the example that makes the program sort the array not in the non-decreasing order). If such example for the given value of n doesn't exist, print -1. -----Input----- You've got a single integer n (1 ≤ n ≤ 50) — the size of the sorted array. -----Output----- Print n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 100) — the counter-example, for which Valera's algorithm won't work correctly. If the counter-example that meets the described conditions is impossible to give, print -1. If there are several counter-examples, consisting of n numbers, you are allowed to print any of them. -----Examples----- Input 1 Output -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()) if(n<=2): print(-1) else: for i in range(n,1,-1): print(i,end=" ") print(1) ```
vfc_14754
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/246/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": "3 2 1 ", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n", "output": "4 3 2 1 ", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n", "output": "5 4 3 2 1 ", "type": "stdin_stdout" } ] }
apps
verifiable_code
1251
Solve the following coding problem using the programming language python: Bizon the Champion isn't just attentive, he also is very hardworking. Bizon the Champion decided to paint his old fence his favorite color, orange. The fence is represented as n vertical planks, put in a row. Adjacent planks have no gap between them. The planks are numbered from the left to the right starting from one, the i-th plank has the width of 1 meter and the height of a_{i} meters. Bizon the Champion bought a brush in the shop, the brush's width is 1 meter. He can make vertical and horizontal strokes with the brush. During a stroke the brush's full surface must touch the fence at all the time (see the samples for the better understanding). What minimum number of strokes should Bizon the Champion do to fully paint the fence? Note that you are allowed to paint the same area of the fence multiple times. -----Input----- The first line contains integer n (1 ≤ n ≤ 5000) — the number of fence planks. The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9). -----Output----- Print a single integer — the minimum number of strokes needed to paint the whole fence. -----Examples----- Input 5 2 2 1 2 1 Output 3 Input 2 2 2 Output 2 Input 1 5 Output 1 -----Note----- In the first sample you need to paint the fence in three strokes with the brush: the first stroke goes on height 1 horizontally along all the planks. The second stroke goes on height 2 horizontally and paints the first and second planks and the third stroke (it can be horizontal and vertical) finishes painting the fourth plank. In the second sample you can paint the fence with two strokes, either two horizontal or two vertical strokes. In the third sample there is only one plank that can be painted using a single vertical stroke. 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=0; inp=[]; tree=[];#stores the index of min in range(i,j) def build(node,i,j): if(i>j): return; if(i==j): tree[node]=int(i); return; mid=int( (i+j)/2 ); build(2*node,i,mid) build(2*node+1,mid+1,j) if( inp[ tree[2*node] ] < inp[ tree[2*node+1] ] ): tree[node]= tree[2*node]; else: tree[node]=tree[2*node+1] def RMQ(node,i,j,l,r):#return index of minimum in range i,j #r,l is current range if( (i<=l) and (r<=j) ): return tree[node]; if( (i>r) or (j<l) ): return n; mid=int((l+r)/2); a=RMQ(2*node, i, j, l , mid);# j,l,mid); b=RMQ(2*node+1, i, j, mid+1, r); if( inp[a] < inp[b]): return a; else: return b; def inputArray(): A=str(input()).split(); return list(map(int,A)); def solve(a,b,ht): if(a>b): return 0; mn=RMQ(1,a,b,0,n-1); op1=b-a+1 op2=solve(a,mn-1 , inp[mn] ) + solve(mn+1,b, inp[mn] ) + inp[mn]-ht ; return min(op1,op2); if( __name__ == "__main__"): n=int( input() ); inp=inputArray(); inp.append(1000*1000*1000+10); sys.setrecursionlimit(10000) #build RMQ array tree=[ int(n) for x in range(4*n+10) ]; build(1,0,n-1); print(( solve(0,n-1,0) )); ```
vfc_14758
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/448/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n2 2 1 2 1\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n2 2\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n5\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n2 2 1 2 2\n", "output": "3\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1252
Solve the following coding problem using the programming language python: Recently Irina arrived to one of the most famous cities of Berland — the Berlatov city. There are n showplaces in the city, numbered from 1 to n, and some of them are connected by one-directional roads. The roads in Berlatov are designed in a way such that there are no cyclic routes between showplaces. Initially Irina stands at the showplace 1, and the endpoint of her journey is the showplace n. Naturally, Irina wants to visit as much showplaces as she can during her journey. However, Irina's stay in Berlatov is limited and she can't be there for more than T time units. Help Irina determine how many showplaces she may visit during her journey from showplace 1 to showplace n within a time not exceeding T. It is guaranteed that there is at least one route from showplace 1 to showplace n such that Irina will spend no more than T time units passing it. -----Input----- The first line of the input contains three integers n, m and T (2 ≤ n ≤ 5000, 1 ≤ m ≤ 5000, 1 ≤ T ≤ 10^9) — the number of showplaces, the number of roads between them and the time of Irina's stay in Berlatov respectively. The next m lines describes roads in Berlatov. i-th of them contains 3 integers u_{i}, v_{i}, t_{i} (1 ≤ u_{i}, v_{i} ≤ n, u_{i} ≠ v_{i}, 1 ≤ t_{i} ≤ 10^9), meaning that there is a road starting from showplace u_{i} and leading to showplace v_{i}, and Irina spends t_{i} time units to pass it. It is guaranteed that the roads do not form cyclic routes. It is guaranteed, that there is at most one road between each pair of showplaces. -----Output----- Print the single integer k (2 ≤ k ≤ n) — the maximum number of showplaces that Irina can visit during her journey from showplace 1 to showplace n within time not exceeding T, in the first line. Print k distinct integers in the second line — indices of showplaces that Irina will visit on her route, in the order of encountering them. If there are multiple answers, print any of them. -----Examples----- Input 4 3 13 1 2 5 2 3 7 2 4 8 Output 3 1 2 4 Input 6 6 7 1 2 2 1 3 3 3 6 3 2 4 2 4 6 2 6 5 1 Output 4 1 2 4 6 Input 5 5 6 1 3 3 3 5 3 1 2 2 2 4 3 4 5 2 Output 3 1 3 5 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def __starting_point(): n, m, t = map(int, input().split()) edge = {i:{} for i in range(n)} income = [0 for i in range(n)] for i in range(m): u, v, ti = map(int, input().split()) edge[v-1][u-1] = ti income[u-1] += 1 stat = [{} for _ in range(n)] stat[n-1] = {1 : (0, -1)} queue = [n-1] first = 0 last = 1 for i in range(n-2, 0, -1): if income[i] == 0: queue.append(i) last += 1 while (first < last): v = queue[first] first += 1 for u in edge[v].keys(): income[u] -= 1 for vis in stat[v].keys(): cost = stat[v][vis][0] + edge[v][u] ucost = stat[u].get(vis+1, (t+1,-1))[0] if ucost > cost: stat[u][vis+1] = (cost, v) if income[u] <= 0: queue.append(u) last += 1 #print(queue, last) res = max(stat[0].keys()) print(res) path = [] curr = 0 path.append(curr+1) while(stat[curr][res][1] >= 0): curr = stat[curr][res][1] path.append(curr+1) res -= 1 print(' '.join(map(str, path))) __starting_point() ```
vfc_14762
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/721/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4 3 13\n1 2 5\n2 3 7\n2 4 8\n", "output": "3\n1 2 4 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6 6 7\n1 2 2\n1 3 3\n3 6 3\n2 4 2\n4 6 2\n6 5 1\n", "output": "4\n1 2 4 6 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 5 6\n1 3 3\n3 5 3\n1 2 2\n2 4 3\n4 5 2\n", "output": "3\n1 3 5 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 10 100\n1 4 1\n6 4 1\n9 3 2\n2 7 2\n5 8 11\n1 2 8\n4 10 10\n8 9 2\n7 5 8\n3 6 4\n", "output": "10\n1 2 7 5 8 9 3 6 4 10 \n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1253
Solve the following coding problem using the programming language python: Roma works in a company that sells TVs. Now he has to prepare a report for the last year. Roma has got a list of the company's incomes. The list is a sequence that consists of n integers. The total income of the company is the sum of all integers in sequence. Roma decided to perform exactly k changes of signs of several numbers in the sequence. He can also change the sign of a number one, two or more times. The operation of changing a number's sign is the operation of multiplying this number by -1. Help Roma perform the changes so as to make the total income of the company (the sum of numbers in the resulting sequence) maximum. Note that Roma should perform exactly k changes. -----Input----- The first line contains two integers n and k (1 ≤ n, k ≤ 10^5), showing, how many numbers are in the sequence and how many swaps are to be made. The second line contains a non-decreasing sequence, consisting of n integers a_{i} (|a_{i}| ≤ 10^4). The numbers in the lines are separated by single spaces. Please note that the given sequence is sorted in non-decreasing order. -----Output----- In the single line print the answer to the problem — the maximum total income that we can obtain after exactly k changes. -----Examples----- Input 3 2 -1 -1 1 Output 3 Input 3 1 -1 -1 1 Output 1 -----Note----- In the first sample we can get sequence [1, 1, 1], thus the total income equals 3. In the second test, the optimal strategy is to get sequence [-1, 1, 1], thus the total income equals 1. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python rd = lambda: list(map(int, input().split())) k = kk = rd()[1] a = rd() k -= sum(x<0 for x in a) a[:kk] = list(map(abs, a[:kk])) print(sum(a)-(2*min(a) if k>0 and k%2 else 0)) ```
vfc_14766
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/262/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 2\n-1 -1 1\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 1\n-1 -1 1\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "17 27\n257 320 676 1136 2068 2505 2639 4225 4951 5786 7677 7697 7851 8337 8429 8469 9343\n", "output": "81852\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1254
Solve the following coding problem using the programming language python: A multi-subject competition is coming! The competition has $m$ different subjects participants can choose from. That's why Alex (the coach) should form a competition delegation among his students. He has $n$ candidates. For the $i$-th person he knows subject $s_i$ the candidate specializes in and $r_i$ — a skill level in his specialization (this level can be negative!). The rules of the competition require each delegation to choose some subset of subjects they will participate in. The only restriction is that the number of students from the team participating in each of the chosen subjects should be the same. Alex decided that each candidate would participate only in the subject he specializes in. Now Alex wonders whom he has to choose to maximize the total sum of skill levels of all delegates, or just skip the competition this year if every valid non-empty delegation has negative sum. (Of course, Alex doesn't have any spare money so each delegate he chooses must participate in the competition). -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 10^5$, $1 \le m \le 10^5$) — the number of candidates and the number of subjects. The next $n$ lines contains two integers per line: $s_i$ and $r_i$ ($1 \le s_i \le m$, $-10^4 \le r_i \le 10^4$) — the subject of specialization and the skill level of the $i$-th candidate. -----Output----- Print the single integer — the maximum total sum of skills of delegates who form a valid delegation (according to rules above) or $0$ if every valid non-empty delegation has negative sum. -----Examples----- Input 6 3 2 6 3 6 2 5 3 5 1 9 3 1 Output 22 Input 5 3 2 6 3 6 2 5 3 5 1 11 Output 23 Input 5 2 1 -1 1 -5 2 -1 2 -1 1 -10 Output 0 -----Note----- In the first example it's optimal to choose candidates $1$, $2$, $3$, $4$, so two of them specialize in the $2$-nd subject and other two in the $3$-rd. The total sum is $6 + 6 + 5 + 5 = 22$. In the second example it's optimal to choose candidates $1$, $2$ and $5$. One person in each subject and the total sum is $6 + 6 + 11 = 23$. In the third example it's impossible to obtain a non-negative sum. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python num_students, num_cats = [int(x) for x in input().split()] cats = [[] for _ in range(num_cats)] for _ in range(num_students): cat_idx, skill = [int(x) for x in input().split()] cat_idx -= 1 cats[cat_idx].append(skill) for cat in cats: cat.sort(key=lambda x : -x) entries = [] for cat in cats: team_size = 0 team_skill = 0 for skill in cat: team_size += 1 team_skill += skill entries.append((team_size, -team_skill)) entries.sort() best_skill = 0 total_skill = 0 curr_size = 1 for entry in entries: size, neg_skill = entry skill = -neg_skill if size != curr_size: best_skill = max(total_skill, best_skill) curr_size = size total_skill = 0 if skill > 0: total_skill += skill best_skill = max(total_skill, best_skill) print(best_skill) ```
vfc_14770
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1082/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6 3\n2 6\n3 6\n2 5\n3 5\n1 9\n3 1\n", "output": "22\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1255
Solve the following coding problem using the programming language python: Valera runs a 24/7 fast food cafe. He magically learned that next day n people will visit his cafe. For each person we know the arrival time: the i-th person comes exactly at h_{i} hours m_{i} minutes. The cafe spends less than a minute to serve each client, but if a client comes in and sees that there is no free cash, than he doesn't want to wait and leaves the cafe immediately. Valera is very greedy, so he wants to serve all n customers next day (and get more profit). However, for that he needs to ensure that at each moment of time the number of working cashes is no less than the number of clients in the cafe. Help Valera count the minimum number of cashes to work at his cafe next day, so that they can serve all visitors. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^5), that is the number of cafe visitors. Each of the following n lines has two space-separated integers h_{i} and m_{i} (0 ≤ h_{i} ≤ 23; 0 ≤ m_{i} ≤ 59), representing the time when the i-th person comes into the cafe. Note that the time is given in the chronological order. All time is given within one 24-hour period. -----Output----- Print a single integer — the minimum number of cashes, needed to serve all clients next day. -----Examples----- Input 4 8 0 8 10 8 10 8 45 Output 2 Input 3 0 12 10 11 22 22 Output 1 -----Note----- In the first sample it is not enough one cash to serve all clients, because two visitors will come into cafe in 8:10. Therefore, if there will be one cash in cafe, then one customer will be served by it, and another one will not wait and will go away. In the second sample all visitors will come in different times, so it will be enough one cash. 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 * import sys print(Counter(sys.stdin.readlines()).most_common(1)[0][1]) ```
vfc_14774
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/237/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n8 0\n8 10\n8 10\n8 45\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n0 12\n10 11\n22 22\n", "output": "1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
1256
Solve the following coding problem using the programming language python: Xenia the beginner mathematician is a third year student at elementary school. She is now learning the addition operation. The teacher has written down the sum of multiple numbers. Pupils should calculate the sum. To make the calculation easier, the sum only contains numbers 1, 2 and 3. Still, that isn't enough for Xenia. She is only beginning to count, so she can calculate a sum only if the summands follow in non-decreasing order. For example, she can't calculate sum 1+3+2+1 but she can calculate sums 1+1+2 and 3+3. You've got the sum that was written on the board. Rearrange the summans and print the sum in such a way that Xenia can calculate the sum. -----Input----- The first line contains a non-empty string s — the sum Xenia needs to count. String s contains no spaces. It only contains digits and characters "+". Besides, string s is a correct sum of numbers 1, 2 and 3. String s is at most 100 characters long. -----Output----- Print the new sum that Xenia can count. -----Examples----- Input 3+2+1 Output 1+2+3 Input 1+1+3+1+3 Output 1+1+1+3+3 Input 2 Output 2 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python s = input() l = [int(x) for x in s.split('+')] l.sort() print('+'.join([str(x) for x in l])) ```
vfc_14778
{ "difficulty": "interview", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/339/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3+2+1\n", "output": "1+2+3\n", "type": "stdin_stdout" } ] }