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
4081
Solve the following coding problem using the programming language python: The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2). You are given a sequence $a$ consisting of $n$ integers. All these integers are distinct, each value from $1$ to $n$ appears in the sequence exactly once. You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it). For example, for the sequence $[2, 1, 5, 4, 3]$ the answer is $4$ (you take $2$ and the sequence becomes $[1, 5, 4, 3]$, then you take the rightmost element $3$ and the sequence becomes $[1, 5, 4]$, then you take $4$ and the sequence becomes $[1, 5]$ and then you take $5$ and the sequence becomes $[1]$, the obtained increasing sequence is $[2, 3, 4, 5]$). -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) โ€” the number of elements in $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the $i$-th element of $a$. All these integers are pairwise distinct. -----Output----- In the first line of the output print $k$ โ€” the maximum number of elements in a strictly increasing sequence you can obtain. In the second line print a string $s$ of length $k$, where the $j$-th character of this string $s_j$ should be 'L' if you take the leftmost element during the $j$-th move and 'R' otherwise. If there are multiple answers, you can print any. -----Examples----- Input 5 2 1 5 4 3 Output 4 LRRR Input 7 1 3 5 6 7 4 2 Output 7 LRLRLLL Input 3 1 2 3 Output 3 LLL Input 4 1 2 4 3 Output 4 LLRL -----Note----- The first example is described in the problem statement. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python x=int(input()) a=list(map(int,input().strip().split())) y=[] c=0 f1=1 f2=1 l=0 r=x-1 op=[] while(f1 or f2): if(l>r): break if(a[l]<c): f1=0 if(a[r]<c): f2=0 if(f1 and f2): if(a[l]<=a[r]): c=a[l] l=l+1 op.append('L') else: c=a[r] r=r-1 op.append('R') elif(f1): c=a[l] l=l+1 op.append('L') elif(f2): c=a[r] r=r-1 op.append('R') print(len(op)) print("".join(op)) ```
vfc_25031
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1157/C1", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n2 1 5 4 3\n", "output": "4\nLRRR\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7\n1 3 5 6 7 4 2\n", "output": "7\nLRLRLLL\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n1 2 3\n", "output": "3\nLLL\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n1 2 4 3\n", "output": "4\nLLRL\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n1 2 5 9 10 8 7 6 4 3\n", "output": "10\nLLRRLRRRLL\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "100\n2 3 4 6 7 8 9 12 13 14 15 16 17 19 20 21 24 25 27 28 31 32 34 40 42 43 45 46 47 49 54 55 58 59 60 61 62 63 68 71 74 75 80 81 82 85 86 87 88 89 91 92 95 96 98 99 100 97 94 93 90 84 83 79 78 77 76 73 72 70 69 67 66 65 64 57 56 53 52 51 50 48 44 41 39 38 37 36 35 33 30 29 26 23 22 18 11 10 5 1\n", "output": "100\nRLLLRLLLLRRLLLLLLRLLLRRLLRLLRRLLRLRRRRRLRLLRLLLRLRRRRLLRRLLLLLLRRRRLRRLRRLLRRRRLLLRRLLLLLRLLRRLLRLLL\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4082
Solve the following coding problem using the programming language python: You are given an array $a$ consisting of $n$ integers. You can remove at most one element from this array. Thus, the final length of the array is $n-1$ or $n$. Your task is to calculate the maximum possible length of the strictly increasing contiguous subarray of the remaining array. Recall that the contiguous subarray $a$ with indices from $l$ to $r$ is $a[l \dots r] = a_l, a_{l + 1}, \dots, a_r$. The subarray $a[l \dots r]$ is called strictly increasing if $a_l < a_{l+1} < \dots < a_r$. -----Input----- The first line of the input contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) โ€” the number of elements in $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer โ€” the maximum possible length of the strictly increasing contiguous subarray of the array $a$ after removing at most one element. -----Examples----- Input 5 1 2 5 3 4 Output 4 Input 2 1 2 Output 2 Input 7 6 5 4 3 2 4 3 Output 2 -----Note----- In the first example, you can delete $a_3=5$. Then the resulting array will be equal to $[1, 2, 3, 4]$ and the length of its largest increasing subarray will be equal 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 = int(input()) mass = list(map(int, input().split())) left = [1] * n right = [1] * n for i in range(1, n): if mass[i] > mass[i - 1]: left[i] = left[i - 1] + 1 for i in range(n - 2, -1, -1): if mass[i] < mass[i + 1]: right[i] = right[i + 1] + 1 mx = 1 for i in range(n): if i == 0: mx = max(right[0], mx) elif i == n - 1: mx = max(mx, left[n - 1]) elif mass[i + 1] > mass[i - 1]: mx = max(mx, left[i - 1] + right[i + 1]) mx = max(mx, left[i]) mx = max(mx, right[i]) print(mx) ```
vfc_25035
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1272/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n1 2 5 3 4\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n1 2\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7\n6 5 4 3 2 4 3\n", "output": "2\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4083
Solve the following coding problem using the programming language python: The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 50$) โ€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer โ€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!! # TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!! # TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!! from sys import stdin, stdout from collections import defaultdict from collections import deque import math import copy #T = int(input()) #N = int(input()) #s1 = input() #s2 = input() N,K = [int(x) for x in stdin.readline().split()] arr = [int(x) for x in stdin.readline().split()] arr.sort() freq = {} for i in range(N): num = arr[i] if num not in freq: freq[num] = [] round = 0 freq[num].append(0) while num!=0: round += 1 num = num//2 if num not in freq: freq[num] = [] freq[num].append(round) res = 999999999999 for key in freq: if len(freq[key])<K: continue else: s = sum(freq[key][:K]) res = min(res,s) print(res) ```
vfc_25039
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1213/D1", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5 3\n1 2 2 4 5\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 3\n1 2 3 4 5\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 3\n1 2 3 3 3\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4085
Solve the following coding problem using the programming language python: We guessed some integer number $x$. You are given a list of almost all its divisors. Almost all means that there are all divisors except $1$ and $x$ in the list. Your task is to find the minimum possible integer $x$ that can be the guessed number, or say that the input data is contradictory and it is impossible to find such number. You have to answer $t$ independent queries. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 25$) โ€” the number of queries. Then $t$ queries follow. The first line of the query contains one integer $n$ ($1 \le n \le 300$) โ€” the number of divisors in the list. The second line of the query contains $n$ integers $d_1, d_2, \dots, d_n$ ($2 \le d_i \le 10^6$), where $d_i$ is the $i$-th divisor of the guessed number. It is guaranteed that all values $d_i$ are distinct. -----Output----- For each query print the answer to it. If the input data in the query is contradictory and it is impossible to find such number $x$ that the given list of divisors is the list of almost all its divisors, print -1. Otherwise print the minimum possible $x$. -----Example----- Input 2 8 8 2 12 6 4 24 16 3 1 2 Output 48 4 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python for q in range(int(input())): n = int(input()) D = list(map(int, input().split())) D.sort() z = D[0] * D[-1] if z == -1: print(-1) else: Dz = set() for i in range(2, int(z ** 0.5) + 1): if z % i == 0: Dz.add(i) Dz.add(z // i) if Dz == set(D): print(z) else: print(-1) ```
vfc_25047
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1165/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\n8\n8 2 12 6 4 24 16 3\n1\n2\n", "output": "48\n4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "25\n2\n999389 999917\n2\n999307 999613\n2\n999529 999611\n2\n999221 999853\n2\n999683 999433\n2\n999907 999749\n2\n999653 999499\n2\n999769 999931\n2\n999809 999287\n2\n999233 999239\n2\n999553 999599\n2\n999371 999763\n2\n999491 999623\n2\n999961 999773\n2\n999671 999269\n2\n999883 999437\n2\n999953 999331\n2\n999377 999667\n2\n999979 999431\n2\n999451 999359\n2\n999631 999563\n2\n999983 999521\n2\n999863 999727\n2\n999541 999721\n2\n999329 999959\n", "output": "999306050713\n998920268191\n999140183219\n999074114513\n999116179739\n999656023343\n999152173847\n999700015939\n999096136183\n998472583687\n999152179247\n999134149073\n999114191893\n999734008853\n998940240499\n999320065871\n999284031443\n999044207459\n999410011949\n998810351909\n999194161253\n999504008143\n999590037401\n999262128061\n999288027511\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n13\n802 5 401 4010 62 2 12431 62155 310 10 31 2005 24862\n61\n15295 256795 6061 42427 805 115 30305 63365 19285 667 19 5 4669 77 1463 253 139403 1771 209 1045 168245 975821 3857 7337 2233 1015 8855 1595 665 161 212135 33649 7 55 24035 7315 35 51359 4807 1265 12673 145 551 385 319 95 36685 23 443555 3335 2185 133 23345 3059 437 203 11 11165 88711 2755 29\n", "output": "-1\n-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "25\n1\n2\n1\n3\n1\n2\n1\n4\n2\n4 2\n1\n5\n1\n2\n1\n3\n2\n2 3\n1\n6\n2\n6 2\n2\n3 6\n3\n6 2 3\n1\n7\n1\n2\n1\n4\n2\n2 4\n1\n8\n2\n8 2\n2\n4 8\n3\n2 4 8\n1\n3\n1\n9\n2\n3 9\n1\n2\n", "output": "4\n9\n4\n-1\n8\n25\n4\n9\n6\n-1\n-1\n-1\n-1\n49\n4\n-1\n8\n-1\n-1\n-1\n16\n9\n-1\n27\n4\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4086
Solve the following coding problem using the programming language python: Petya has an array $a$ consisting of $n$ integers. He wants to remove duplicate (equal) elements. Petya wants to leave only the rightmost entry (occurrence) for each element of the array. The relative order of the remaining unique elements should not be changed. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 50$) โ€” the number of elements in Petya's array. The following line contains a sequence $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 1\,000$) โ€” the Petya's array. -----Output----- In the first line print integer $x$ โ€” the number of elements which will be left in Petya's array after he removed the duplicates. In the second line print $x$ integers separated with a space โ€” Petya's array after he removed the duplicates. For each unique element only the rightmost entry should be left. -----Examples----- Input 6 1 5 5 1 6 1 Output 3 5 6 1 Input 5 2 4 2 4 4 Output 2 2 4 Input 5 6 6 6 6 6 Output 1 6 -----Note----- In the first example you should remove two integers $1$, which are in the positions $1$ and $4$. Also you should remove the integer $5$, which is in the position $2$. In the second example you should remove integer $2$, which is in the position $1$, and two integers $4$, which are in the positions $2$ and $4$. In the third example you should remove four integers $6$, which are in the positions $1$, $2$, $3$ and $4$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n=int(input()) ar=list(map(int,input().split())) s=set() a=[] for x in ar[::-1]: if x not in s: a.append(x) s.add(x) print(len(a)) print(*a[::-1]) ```
vfc_25051
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/978/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6\n1 5 5 1 6 1\n", "output": "3\n5 6 1 \n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4087
Solve the following coding problem using the programming language python: Polycarp knows that if the sum of the digits of a number is divisible by $3$, then the number itself is divisible by $3$. He assumes that the numbers, the sum of the digits of which is divisible by $4$, are also somewhat interesting. Thus, he considers a positive integer $n$ interesting if its sum of digits is divisible by $4$. Help Polycarp find the nearest larger or equal interesting number for the given number $a$. That is, find the interesting number $n$ such that $n \ge a$ and $n$ is minimal. -----Input----- The only line in the input contains an integer $a$ ($1 \le a \le 1000$). -----Output----- Print the nearest greater or equal interesting number for the given number $a$. In other words, print the interesting number $n$ such that $n \ge a$ and $n$ is minimal. -----Examples----- Input 432 Output 435 Input 99 Output 103 Input 237 Output 237 Input 42 Output 44 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def f(n): a=0 while(n>0): a+=n%10 n//=10 return a n=int(input()) while f(n)%4!=0: n+=1 print(n) ```
vfc_25055
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1183/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "432\n", "output": "435\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "99\n", "output": "103\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "237\n", "output": "237\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "42\n", "output": "44\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4088
Solve the following coding problem using the programming language python: Polycarp wrote on the board a string $s$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input. After that, he erased some letters from the string $s$, and he rewrote the remaining letters in any order. As a result, he got some new string $t$. You have to find it with some additional information. Suppose that the string $t$ has length $m$ and the characters are numbered from left to right from $1$ to $m$. You are given a sequence of $m$ integers: $b_1, b_2, \ldots, b_m$, where $b_i$ is the sum of the distances $|i-j|$ from the index $i$ to all such indices $j$ that $t_j > t_i$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $b_i$, Polycarp finds all such indices $j$ that the index $j$ contains a letter that is later in the alphabet than $t_i$ and sums all the values $|i-j|$. For example, if $t$ = "abzb", then: since $t_1$='a', all other indices contain letters which are later in the alphabet, that is: $b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$; since $t_2$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_2=|2-3|=1$; since $t_3$='z', then there are no indexes $j$ such that $t_j>t_i$, thus $b_3=0$; since $t_4$='b', only the index $j=3$ contains the letter, which is later in the alphabet, that is: $b_4=|4-3|=1$. Thus, if $t$ = "abzb", then $b=[6,1,0,1]$. Given the string $s$ and the array $b$, find any possible string $t$ for which the following two requirements are fulfilled simultaneously: $t$ is obtained from $s$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $t$ according to the rules above, equals to the array $b$ specified in the input data. -----Input----- The first line contains an integer $q$ ($1 \le q \le 100$)ย โ€” the number of test cases in the test. Then $q$ test cases follow. Each test case consists of three lines: the first line contains string $s$, which has a length from $1$ to $50$ and consists of lowercase English letters; the second line contains positive integer $m$ ($1 \le m \le |s|$), where $|s|$ is the length of the string $s$, and $m$ is the length of the array $b$; the third line contains the integers $b_1, b_2, \dots, b_m$ ($0 \le b_i \le 1225$). It is guaranteed that in each test case an answer exists. -----Output----- Output $q$ lines: the $k$-th of them should contain the answer (string $t$) to the $k$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. -----Example----- Input 4 abac 3 2 1 0 abc 1 0 abba 3 1 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 Output aac b aba codeforces -----Note----- In the first test case, such strings $t$ are suitable: "aac', "aab". In the second test case, such trings $t$ are suitable: "a", "b", "c". In the third test case, only the string $t$ equals to "aba" is suitable, but the character 'b' can be from the second or third position. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys input = sys.stdin.readline t=int(input()) for tests in range(t): S=input().strip() m=int(input()) B=list(map(int,input().split())) LIST=[0]*26 for s in S: LIST[ord(s)-97]+=1 ANS=[0]*m ind=25 while max(B)>=0: L=[] for i in range(m): if B[i]==0: L.append(i) B[i]=-1 LEN=len(L) while LIST[ind]<LEN: ind-=1 for l in L: ANS[l]=ind ind-=1 for l in L: for i in range(m): B[i]-=abs(i-l) #print(ANS) print("".join([chr(a+97) for a in ANS])) ```
vfc_25059
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1367/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\nabac\n3\n2 1 0\nabc\n1\n0\nabba\n3\n1 0 1\necoosdcefr\n10\n38 13 24 14 11 5 3 24 17 0\n", "output": "aac\nc\naba\ncodeforces\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\nabac\n3\n2 1 0\n", "output": "aac\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\ncbba\n2\n0 0\n", "output": "bb\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\nzbb\n2\n0 0\n", "output": "bb\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\naccccccc\n1\n0\naaaaac\n3\n0 0 0\n", "output": "c\naaa\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\naaad\n3\n0 0 0\n", "output": "aaa\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4090
Solve the following coding problem using the programming language python: You are given a text consisting of $n$ space-separated words. There is exactly one space character between any pair of adjacent words. There are no spaces before the first word and no spaces after the last word. The length of text is the number of letters and spaces in it. $w_i$ is the $i$-th word of text. All words consist only of lowercase Latin letters. Let's denote a segment of words $w[i..j]$ as a sequence of words $w_i, w_{i + 1}, \dots, w_j$. Two segments of words $w[i_1 .. j_1]$ and $w[i_2 .. j_2]$ are considered equal if $j_1 - i_1 = j_2 - i_2$, $j_1 \ge i_1$, $j_2 \ge i_2$, and for every $t \in [0, j_1 - i_1]$ $w_{i_1 + t} = w_{i_2 + t}$. For example, for the text "to be or not to be" the segments $w[1..2]$ and $w[5..6]$ are equal, they correspond to the words "to be". An abbreviation is a replacement of some segments of words with their first uppercase letters. In order to perform an abbreviation, you have to choose at least two non-intersecting equal segments of words, and replace each chosen segment with the string consisting of first letters of the words in the segment (written in uppercase). For example, for the text "a ab a a b ab a a b c" you can replace segments of words $w[2..4]$ and $w[6..8]$ with an abbreviation "AAA" and obtain the text "a AAA b AAA b c", or you can replace segments of words $w[2..5]$ and $w[6..9]$ with an abbreviation "AAAB" and obtain the text "a AAAB AAAB c". What is the minimum length of the text after at most one abbreviation? -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 300$) โ€” the number of words in the text. The next line contains $n$ space-separated words of the text $w_1, w_2, \dots, w_n$. Each word consists only of lowercase Latin letters. It is guaranteed that the length of text does not exceed $10^5$. -----Output----- Print one integer โ€” the minimum length of the text after at most one abbreviation. -----Examples----- Input 6 to be or not to be Output 12 Input 10 a ab a a b ab a a b c Output 13 Input 6 aa bb aa aa bb bb Output 11 -----Note----- In the first example you can obtain the text "TB or not TB". In the second example you can obtain the text "a AAAB AAAB c". In the third example you can obtain the text "AB aa AB bb". The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = int(input()) arr = input() final = len(arr) arr = arr.split() lens = [0 for x in range(n)] visit = [0 for x in range(n)] cnt = 0 ans = 0 for i in range(n): if visit[i]: continue lens[cnt] = len(arr[i]) for j in range(i+1,n): if arr[j]==arr[i]: arr[j] = cnt visit[j] = 1 arr[i] = cnt cnt += 1 for i in range(n): for j in range(i,n): temp = arr[i:j+1] ind = 1 found = 0 len2 = j-i+1 cur = 0 kmp = [0 for x in range(len2)] while ind < len2: if temp[ind] == temp[cur]: cur += 1 kmp[ind] = cur ind += 1 else: if cur != 0: cur -= 1 else: kmp[ind] = 0 ind += 1 ind = 0 cur = 0 while ind < n: if arr[ind] == temp[cur]: ind += 1 cur += 1 if cur == len2: found += 1 cur = 0 elif ind < n and temp[cur] != arr[ind]: if cur != 0: cur = kmp[cur-1] else: ind += 1 if found>1: res = 0 for k in temp: res += (lens[k]-1)*(found) res += (len(temp)-1)*(found) ans = max(ans,res) print(final-ans) ```
vfc_25067
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1003/F", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6\nto be or not to be\n", "output": "12\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\na ab a a b ab a a b c\n", "output": "13\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4091
Solve the following coding problem using the programming language python: Polycarp is practicing his problem solving skill. He has a list of $n$ problems with difficulties $a_1, a_2, \dots, a_n$, respectively. His plan is to practice for exactly $k$ days. Each day he has to solve at least one problem from his list. Polycarp solves the problems in the order they are given in his list, he cannot skip any problem from his list. He has to solve all $n$ problems in exactly $k$ days. Thus, each day Polycarp solves a contiguous sequence of (consecutive) problems from the start of the list. He can't skip problems or solve them multiple times. As a result, in $k$ days he will solve all the $n$ problems. The profit of the $j$-th day of Polycarp's practice is the maximum among all the difficulties of problems Polycarp solves during the $j$-th day (i.e. if he solves problems with indices from $l$ to $r$ during a day, then the profit of the day is $\max\limits_{l \le i \le r}a_i$). The total profit of his practice is the sum of the profits over all $k$ days of his practice. You want to help Polycarp to get the maximum possible total profit over all valid ways to solve problems. Your task is to distribute all $n$ problems between $k$ days satisfying the conditions above in such a way, that the total profit is maximum. For example, if $n = 8, k = 3$ and $a = [5, 4, 2, 6, 5, 1, 9, 2]$, one of the possible distributions with maximum total profit is: $[5, 4, 2], [6, 5], [1, 9, 2]$. Here the total profit equals $5 + 6 + 9 = 20$. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2000$) โ€” the number of problems and the number of days, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2000$) โ€” difficulties of problems in Polycarp's list, in the order they are placed in the list (i.e. in the order Polycarp will solve them). -----Output----- In the first line of the output print the maximum possible total profit. In the second line print exactly $k$ positive integers $t_1, t_2, \dots, t_k$ ($t_1 + t_2 + \dots + t_k$ must equal $n$), where $t_j$ means the number of problems Polycarp will solve during the $j$-th day in order to achieve the maximum possible total profit of his practice. If there are many possible answers, you may print any of them. -----Examples----- Input 8 3 5 4 2 6 5 1 9 2 Output 20 3 2 3 Input 5 1 1 1 1 1 1 Output 1 5 Input 4 2 1 2000 2000 2 Output 4000 2 2 -----Note----- The first example is described in the problem statement. In the second example there is only one possible distribution. In the third example the best answer is to distribute problems in the following way: $[1, 2000], [2000, 2]$. The total profit of this distribution is $2000 + 2000 = 4000$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def mi(): return map(int, input().split()) n, k = mi() a = list(mi()) for i in range(n): a[i] = [a[i],i] a.sort(reverse= True) a = a[:k] s = 0 ind = [] for i in a: s+=i[0] ind.append(i[1]) ind.sort() for i in range(k): ind[i]+=1 ind1 = ind.copy() for i in range(1,len(ind)): ind[i]-=ind1[i-1] ind[-1] = n-sum(ind[:k-1]) print (s) for i in ind: print (i, end = ' ') ```
vfc_25071
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1006/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "8 3\n5 4 2 6 5 1 9 2\n", "output": "20\n4 1 3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 1\n1 1 1 1 1\n", "output": "1\n5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 2\n1 2000 2000 2\n", "output": "4000\n2 2\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4092
Solve the following coding problem using the programming language python: Kolya got an integer array $a_1, a_2, \dots, a_n$. The array can contain both positive and negative integers, but Kolya doesn't like $0$, so the array doesn't contain any zeros. Kolya doesn't like that the sum of some subsegments of his array can be $0$. The subsegment is some consecutive segment of elements of the array. You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$, any by absolute value, even such a huge that they can't be represented in most standard programming languages). Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$. -----Input----- The first line of the input contains one integer $n$ ($2 \le n \le 200\,000$) โ€” the number of elements in Kolya's array. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^{9} \le a_i \le 10^{9}, a_i \neq 0$) โ€” the description of Kolya's array. -----Output----- Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$. -----Examples----- Input 4 1 -5 3 2 Output 1 Input 5 4 -2 3 -9 2 Output 0 Input 9 -1 1 -1 1 -1 1 1 -1 -1 Output 6 Input 8 16 -5 -11 -15 10 5 4 -4 Output 3 -----Note----- Consider the first example. There is only one subsegment with the sum $0$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array. There are no subsegments having sum $0$ in the second example so you don't need to do anything. 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 tt = 1 for loop in range(tt): dic = {} dic[0] = 1 n = int(stdin.readline()) a = list(map(int,stdin.readline().split())) now = 0 ans = 0 for i in a: now += i if now in dic: ans += 1 dic = {} dic[0] = 1 now = i dic[now] = 1 print (ans) ```
vfc_25075
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1426/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n1 -5 3 2\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n4 -2 3 -9 2\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "9\n-1 1 -1 1 -1 1 1 -1 -1\n", "output": "6\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "8\n16 -5 -11 -15 10 5 4 -4\n", "output": "3\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4093
Solve the following coding problem using the programming language python: You are given two integers $n$ and $m$. You have to construct the array $a$ of length $n$ consisting of non-negative integers (i.e. integers greater than or equal to zero) such that the sum of elements of this array is exactly $m$ and the value $\sum\limits_{i=1}^{n-1} |a_i - a_{i+1}|$ is the maximum possible. Recall that $|x|$ is the absolute value of $x$. In other words, you have to maximize the sum of absolute differences between adjacent (consecutive) elements. For example, if the array $a=[1, 3, 2, 5, 5, 0]$ then the value above for this array is $|1-3| + |3-2| + |2-5| + |5-5| + |5-0| = 2 + 1 + 3 + 0 + 5 = 11$. Note that this example doesn't show the optimal answer but it shows how the required value for some array is calculated. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) โ€” the number of test cases. Then $t$ test cases follow. The only line of the test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^9$) โ€” the length of the array and its sum correspondingly. -----Output----- For each test case, print the answer โ€” the maximum possible value of $\sum\limits_{i=1}^{n-1} |a_i - a_{i+1}|$ for the array $a$ consisting of $n$ non-negative integers with the sum $m$. -----Example----- Input 5 1 100 2 2 5 5 2 1000000000 1000000000 1000000000 Output 0 2 10 1000000000 2000000000 -----Note----- In the first test case of the example, the only possible array is $[100]$ and the answer is obviously $0$. In the second test case of the example, one of the possible arrays is $[2, 0]$ and the answer is $|2-0| = 2$. In the third test case of the example, one of the possible arrays is $[0, 2, 0, 3, 0]$ and the answer is $|0-2| + |2-0| + |0-3| + |3-0| = 10$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys input = sys.stdin.readline rInt = lambda: int(input()) mInt = lambda: list(map(int, input().split())) rLis = lambda: list(map(int, input().split())) t = int(input()) for _ in range(t): n, m = mInt() if n == 1: print(0) elif n == 2: print(m) else: print(2 * m) ```
vfc_25079
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1353/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n1 100\n2 2\n5 5\n2 1000000000\n1000000000 1000000000\n", "output": "0\n2\n10\n1000000000\n2000000000\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n54 33\n", "output": "66\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4095
Solve the following coding problem using the programming language python: You are given a permutation $p_1, p_2, \dots, p_n$. A permutation of length $n$ is a sequence such that each integer between $1$ and $n$ occurs exactly once in the sequence. Find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of the median of $p_l, p_{l+1}, \dots, p_r$ is exactly the given number $m$. The median of a sequence is the value of the element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used. For example, if $a=[4, 2, 7, 5]$ then its median is $4$ since after sorting the sequence, it will look like $[2, 4, 5, 7]$ and the left of two middle elements is equal to $4$. The median of $[7, 1, 2, 9, 6]$ equals $6$ since after sorting, the value $6$ will be in the middle of the sequence. Write a program to find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of the median of $p_l, p_{l+1}, \dots, p_r$ is exactly the given number $m$. -----Input----- The first line contains integers $n$ and $m$ ($1 \le n \le 2\cdot10^5$, $1 \le m \le n$) โ€” the length of the given sequence and the required value of the median. The second line contains a permutation $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$). Each integer between $1$ and $n$ occurs in $p$ exactly once. -----Output----- Print the required number. -----Examples----- Input 5 4 2 4 5 3 1 Output 4 Input 5 5 1 2 3 4 5 Output 1 Input 15 8 1 15 2 14 3 13 4 8 12 5 11 6 10 7 9 Output 48 -----Note----- In the first example, the suitable pairs of indices are: $(1, 3)$, $(2, 2)$, $(2, 3)$ and $(2, 4)$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def main(): n, m = map(int, input().split()) def intCompare(x): if int(x) == m: return 0 if int(x) < m: return -1 return 1 p = list(map(intCompare, input().split())) ret = 0 ind = p.index(0) tem = 0 ret0 = [0] * 400001 ret1 = [0] * 400001 set0 = set() for i in range(ind, -1, -1): tem += p[i] ret0[tem] += 1 set0.add(tem) tem = 0 for i in range(ind, n): tem += p[i] ret1[tem] += 1 for i in set0: ret += ret0[i] * (ret1[-i] + ret1[1-i]) print(ret) return 0 main() ```
vfc_25087
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1005/E1", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5 4\n2 4 5 3 1\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 5\n1 2 3 4 5\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "15 8\n1 15 2 14 3 13 4 8 12 5 11 6 10 7 9\n", "output": "48\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1\n1\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 1\n1 2\n", "output": "2\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4096
Solve the following coding problem using the programming language python: The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not usually being written in a single day (in a perfect world of Berland, at least). Some of them require multiple days of hard work. Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 100$, $1 \le m \le 10^4$) โ€” the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 100$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days in this test. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, m = [int(x) for x in input().split()] a = [int(x) for x in input().split()] a.sort(reverse=True) def check(d): s=0 for i in range(len(a)): s+=max(0,a[i]-i//d) return s>=m if sum(a)<m: print(-1) else: l, r = 1,n mid = l+r>>1 while l<r: if check(mid): r=mid else: l=mid+1 mid=l+r>>1 print(l) ```
vfc_25091
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1118/D1", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5 8\n2 3 1 1 2\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7 10\n1 3 4 2 1 4 2\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 15\n5 5 5 5 5\n", "output": "1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4097
Solve the following coding problem using the programming language python: Polycarp likes arithmetic progressions. A sequence $[a_1, a_2, \dots, a_n]$ is called an arithmetic progression if for each $i$ ($1 \le i < n$) the value $a_{i+1} - a_i$ is the same. For example, the sequences $[42]$, $[5, 5, 5]$, $[2, 11, 20, 29]$ and $[3, 2, 1, 0]$ are arithmetic progressions, but $[1, 0, 1]$, $[1, 3, 9]$ and $[2, 3, 1]$ are not. It follows from the definition that any sequence of length one or two is an arithmetic progression. Polycarp found some sequence of positive integers $[b_1, b_2, \dots, b_n]$. He agrees to change each element by at most one. In the other words, for each element there are exactly three options: an element can be decreased by $1$, an element can be increased by $1$, an element can be left unchanged. Determine a minimum possible number of elements in $b$ which can be changed (by exactly one), so that the sequence $b$ becomes an arithmetic progression, or report that it is impossible. It is possible that the resulting sequence contains element equals $0$. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 100\,000)$ โ€” the number of elements in $b$. The second line contains a sequence $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 10^{9})$. -----Output----- If it is impossible to make an arithmetic progression with described operations, print -1. In the other case, print non-negative integer โ€” the minimum number of elements to change to make the given sequence becomes an arithmetic progression. The only allowed operation is to add/to subtract one from an element (can't use operation twice to the same position). -----Examples----- Input 4 24 21 14 10 Output 3 Input 2 500 500 Output 0 Input 3 14 5 1 Output -1 Input 5 1 3 6 9 12 Output 1 -----Note----- In the first example Polycarp should increase the first number on $1$, decrease the second number on $1$, increase the third number on $1$, and the fourth number should left unchanged. So, after Polycarp changed three elements by one, his sequence became equals to $[25, 20, 15, 10]$, which is an arithmetic progression. In the second example Polycarp should not change anything, because his sequence is an arithmetic progression. In the third example it is impossible to make an arithmetic progression. In the fourth example Polycarp should change only the first element, he should decrease it on one. After that his sequence will looks like $[0, 3, 6, 9, 12]$, which is an arithmetic progression. 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()) T = input().split(' ') for i in range(n): T[i] = int(T[i]) m = n+1 if n == 1: print(0) else: for a in range(-1, 2): for b in range(-1, 2): c = True p = (T[1]+b) - (T[0]+a) tot = 0 if a!=0: tot+=1 if b!=0: tot+=1 el = T[1]+b for j in range(2, n): if abs((T[j] - el) - p) <= 1: el += p if T[j] != el: tot+=1 else: c = False if c: m = min(m, tot) if m <= n: print(m) else: print(-1) ```
vfc_25095
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/978/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n24 21 14 10\n", "output": "3\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4098
Solve the following coding problem using the programming language python: You are a coach at your local university. There are $n$ students under your supervision, the programming skill of the $i$-th student is $a_i$. You have to form $k$ teams for yet another new programming competition. As you know, the more students are involved in competition the more probable the victory of your university is! So you have to form no more than $k$ (and at least one) non-empty teams so that the total number of students in them is maximized. But you also know that each team should be balanced. It means that the programming skill of each pair of students in each team should differ by no more than $5$. Teams are independent from one another (it means that the difference between programming skills of two students from two different teams does not matter). It is possible that some students not be included in any team at all. Your task is to report the maximum possible total number of students in no more than $k$ (and at least one) non-empty balanced teams. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 5000$) โ€” the number of students and the maximum number of teams, correspondingly. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is a programming skill of the $i$-th student. -----Output----- Print one integer โ€” the maximum possible total number of students in no more than $k$ (and at least one) non-empty balanced teams. -----Examples----- Input 5 2 1 2 15 15 15 Output 5 Input 6 1 36 4 1 25 9 16 Output 2 Input 4 4 1 10 100 1000 Output 4 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import bisect n,k=list(map(int,input().split())) A=list(map(int,input().split())) A.sort() DP=[[0]*(k+1) for i in range(n)] for i in range(n): x=bisect.bisect_right(A,A[i]+5)-1 #print(i,x) for j in range(k-1,-1,-1): DP[i][j]=max(DP[i][j],DP[i-1][j]) DP[x][j+1]=max(DP[i-1][j]+x-i+1,DP[x][j+1]) print(max([DP[i][-1] for i in range(n)])) ```
vfc_25099
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1133/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5 2\n1 2 15 15 15\n", "output": "5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6 1\n36 4 1 25 9 16\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 4\n1 10 100 1000\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 1\n1496 2336 3413 4121 1835 2835 251 1086 4401 4225\n", "output": "1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4102
Solve the following coding problem using the programming language python: -----Input----- The only line of the input contains a string of digits. The length of the string is between 1 and 10, inclusive. -----Output----- Output "Yes" or "No". -----Examples----- Input 373 Output Yes Input 121 Output No Input 436 Output Yes The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys import time for line in sys.stdin: ll = len(line) - 1 fail = 0 for i in range(ll): if i == ll - 1 - i: if int(line[i]) not in [3, 7]: fail = 1 continue x = int(line[i]) y = int(line[ll - 1 - i]) if (x, y) not in [(3, 3), (4, 6), (6, 4), (7, 7), (8, 0), (0, 8), (5, 9), (9, 5)]: fail = 1 if fail: print("No") if not fail: print("Yes") ```
vfc_25115
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/784/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "373\n", "output": "Yes\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "121\n", "output": "No\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4103
Solve the following coding problem using the programming language python: There is a robot staying at $X=0$ on the $Ox$ axis. He has to walk to $X=n$. You are controlling this robot and controlling how he goes. The robot has a battery and an accumulator with a solar panel. The $i$-th segment of the path (from $X=i-1$ to $X=i$) can be exposed to sunlight or not. The array $s$ denotes which segments are exposed to sunlight: if segment $i$ is exposed, then $s_i = 1$, otherwise $s_i = 0$. The robot has one battery of capacity $b$ and one accumulator of capacity $a$. For each segment, you should choose which type of energy storage robot will use to go to the next point (it can be either battery or accumulator). If the robot goes using the battery, the current charge of the battery is decreased by one (the robot can't use the battery if its charge is zero). And if the robot goes using the accumulator, the current charge of the accumulator is decreased by one (and the robot also can't use the accumulator if its charge is zero). If the current segment is exposed to sunlight and the robot goes through it using the battery, the charge of the accumulator increases by one (of course, its charge can't become higher than it's maximum capacity). If accumulator is used to pass some segment, its charge decreases by 1 no matter if the segment is exposed or not. You understand that it is not always possible to walk to $X=n$. You want your robot to go as far as possible. Find the maximum number of segments of distance the robot can pass if you control him optimally. -----Input----- The first line of the input contains three integers $n, b, a$ ($1 \le n, b, a \le 2 \cdot 10^5$) โ€” the robot's destination point, the battery capacity and the accumulator capacity, respectively. The second line of the input contains $n$ integers $s_1, s_2, \dots, s_n$ ($0 \le s_i \le 1$), where $s_i$ is $1$ if the $i$-th segment of distance is exposed to sunlight, and $0$ otherwise. -----Output----- Print one integer โ€” the maximum number of segments the robot can pass if you control him optimally. -----Examples----- Input 5 2 1 0 1 0 1 0 Output 5 Input 6 2 1 1 0 0 1 0 1 Output 3 -----Note----- In the first example the robot can go through the first segment using the accumulator, and charge levels become $b=2$ and $a=0$. The second segment can be passed using the battery, and charge levels become $b=1$ and $a=1$. The third segment can be passed using the accumulator, and charge levels become $b=1$ and $a=0$. The fourth segment can be passed using the battery, and charge levels become $b=0$ and $a=1$. And the fifth segment can be passed using the accumulator. In the second example the robot can go through the maximum number of segments using battery two times and accumulator one time in any order. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, b, a = map(int, input().split()) A = list(map(int, input().split())) a0 = a ans = 0 for elem in A: if a + b == 0: break if elem == 0: if a > 0: a -= 1 ans += 1 else: b -= 1 ans += 1 else: if a == a0: a -= 1 ans += 1 elif b > 0: b -= 1 a += 1 ans += 1 else: a -= 1 ans += 1 print(ans) ```
vfc_25119
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1154/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5 2 1\n0 1 0 1 0\n", "output": "5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6 2 1\n1 0 0 1 0 1\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "100 1 1\n0 0 0 1 1 0 1 0 1 0 1 0 0 1 0 0 1 1 0 0 1 0 1 1 1 1 1 1 1 0 1 1 1 0 0 0 0 0 0 1 1 0 0 0 1 0 1 0 0 1 0 1 1 0 0 1 0 0 0 1 1 1 1 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1 1 0 1 1 0 1 1 1 0 1 1 0 1 1 0 0 1 1 0 1 1\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "100 1 1\n0 0 1 0 0 0 0 0 1 1 0 0 1 0 1 0 0 0 1 0 0 1 0 1 0 0 1 1 1 0 0 1 0 0 0 1 1 1 0 0 0 1 0 1 1 0 1 0 0 1 1 0 1 0 1 0 1 1 0 0 0 1 0 0 1 0 1 0 1 1 1 0 1 0 1 1 1 0 1 1 0 0 0 0 1 1 1 1 1 1 1 0 0 0 1 0 1 0 0 1\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "100 2 1\n0 0 1 1 1 0 0 1 0 1 0 1 0 1 1 0 0 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 0 1 1 1 1 1 0 1 1 0 1 1 0 0 0 1 1 0 1 1 1 1 1 1 0 1 0 0 0 0 1 0 1 1 0 1 0 1 0 0 1 1 0 1 0 1 0 1 1 1 0 0 1 0 1 1 1 1 1 1 1\n", "output": "4\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4104
Solve the following coding problem using the programming language python: One very experienced problem writer decided to prepare a problem for April Fools Day contest. The task was very simple - given an arithmetic expression, return the result of evaluating this expression. However, looks like there is a bug in the reference solution... -----Input----- The only line of input data contains the arithmetic expression. The expression will contain between 2 and 10 operands, separated with arithmetic signs plus and/or minus. Each operand will be an integer between 0 and 255, inclusive. -----Output----- Reproduce the output of the reference solution, including the bug. -----Examples----- Input 8-7+6-5+4-3+2-1-0 Output 4 Input 2+2 Output -46 Input 112-37 Output 375 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python res = 0 val = 0 sub = False for c in input()+'+': if c == '+' or c == '-': if sub: val *= -1 res += val val = 0 sub = (c == '-') val *= 10 val += ord(c) - ord('0') print(res) ```
vfc_25123
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/952/F", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "8-7+6-5+4-3+2-1-0\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2+2\n", "output": "-46\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "112-37\n", "output": "375\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "255+255+255+255+255+255+255+255+255+255\n", "output": "-42450\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "0-255-255-255-255-255-255-255-255-255\n", "output": "24705\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "0+0+0+0+0+0+0+0+0+0\n", "output": "-450\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4105
Solve the following coding problem using the programming language python: The king of Berland organizes a ball! $n$ pair are invited to the ball, they are numbered from $1$ to $n$. Each pair consists of one man and one woman. Each dancer (either man or woman) has a monochrome costume. The color of each costume is represented by an integer from $1$ to $k$, inclusive. Let $b_i$ be the color of the man's costume and $g_i$ be the color of the woman's costume in the $i$-th pair. You have to choose a color for each dancer's costume (i.e. values $b_1, b_2, \dots, b_n$ and $g_1, g_2, \dots g_n$) in such a way that: for every $i$: $b_i$ and $g_i$ are integers between $1$ and $k$, inclusive; there are no two completely identical pairs, i.e. no two indices $i, j$ ($i \ne j$) such that $b_i = b_j$ and $g_i = g_j$ at the same time; there is no pair such that the color of the man's costume is the same as the color of the woman's costume in this pair, i.e. $b_i \ne g_i$ for every $i$; for each two consecutive (adjacent) pairs both man's costume colors and woman's costume colors differ, i.e. for every $i$ from $1$ to $n-1$ the conditions $b_i \ne b_{i + 1}$ and $g_i \ne g_{i + 1}$ hold. Let's take a look at the examples of bad and good color choosing (for $n=4$ and $k=3$, man is the first in a pair and woman is the second): Bad color choosing: $(1, 2)$, $(2, 3)$, $(3, 2)$, $(1, 2)$ โ€” contradiction with the second rule (there are equal pairs); $(2, 3)$, $(1, 1)$, $(3, 2)$, $(1, 3)$ โ€” contradiction with the third rule (there is a pair with costumes of the same color); $(1, 2)$, $(2, 3)$, $(1, 3)$, $(2, 1)$ โ€” contradiction with the fourth rule (there are two consecutive pairs such that colors of costumes of men/women are the same). Good color choosing: $(1, 2)$, $(2, 1)$, $(1, 3)$, $(3, 1)$; $(1, 2)$, $(3, 1)$, $(2, 3)$, $(3, 2)$; $(3, 1)$, $(1, 2)$, $(2, 3)$, $(3, 2)$. You have to find any suitable color choosing or say that no suitable choosing exists. -----Input----- The only line of the input contains two integers $n$ and $k$ ($2 \le n, k \le 2 \cdot 10^5$) โ€” the number of pairs and the number of colors. -----Output----- If it is impossible to find any suitable colors choosing, print "NO". Otherwise print "YES" and then the colors of the costumes of pairs in the next $n$ lines. The $i$-th line should contain two integers $b_i$ and $g_i$ โ€” colors of costumes of man and woman in the $i$-th pair, respectively. You can print each letter in any case (upper or lower). For example, "YeS", "no" and "yES" are all acceptable. -----Examples----- Input 4 3 Output YES 3 1 1 3 3 2 2 3 Input 10 4 Output YES 2 1 1 3 4 2 3 4 4 3 3 2 2 4 4 1 1 4 3 1 Input 13 4 Output NO 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())) if n > k*(k-1): print("NO") else: print("YES") cnt = 0 for delta in range(k): for c in range(1, k+1): if cnt < n: cnt +=1 print(c, 1+(c+delta)%k) else: break ```
vfc_25127
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1118/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4 3\n", "output": "YES\n1 2\n2 3\n3 1\n1 3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 4\n", "output": "YES\n1 2\n2 3\n3 4\n4 1\n1 3\n2 4\n3 1\n4 2\n1 4\n2 1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4106
Solve the following coding problem using the programming language python: The only difference between easy and hard versions is the constraints. Vova likes pictures with kittens. The news feed in the social network he uses can be represented as an array of $n$ consecutive pictures (with kittens, of course). Vova likes all these pictures, but some are more beautiful than the others: the $i$-th picture has beauty $a_i$. Vova wants to repost exactly $x$ pictures in such a way that: each segment of the news feed of at least $k$ consecutive pictures has at least one picture reposted by Vova; the sum of beauty values of reposted pictures is maximum possible. For example, if $k=1$ then Vova has to repost all the pictures in the news feed. If $k=2$ then Vova can skip some pictures, but between every pair of consecutive pictures Vova has to repost at least one of them. Your task is to calculate the maximum possible sum of values of reposted pictures if Vova follows conditions described above, or say that there is no way to satisfy all conditions. -----Input----- The first line of the input contains three integers $n, k$ and $x$ ($1 \le k, x \le n \le 200$) โ€” the number of pictures in the news feed, the minimum length of segment with at least one repost in it and the number of pictures Vova is ready to repost. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the beauty of the $i$-th picture. -----Output----- Print -1 if there is no way to repost some pictures to satisfy all the conditions in the problem statement. Otherwise print one integer โ€” the maximum sum of values of reposted pictures if Vova follows conditions described in the problem statement. -----Examples----- Input 5 2 3 5 1 3 10 1 Output 18 Input 6 1 5 10 30 30 70 10 10 Output -1 Input 4 3 1 1 100 1 1 Output 100 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys from math import * def minp(): return sys.stdin.readline().strip() def mint(): return int(minp()) def mints(): return map(int, minp().split()) n, k, x = mints() a = list(mints()) d = [None]*(n+1) p = [None]*(n+1) for i in range(0,k): d[i] = a[i] for xx in range(2,x+1): d,p = p,d for nn in range(n): m = None for i in range(max(0,nn-k),nn): if p[i] != None: if m == None: m = p[i] else: m = max(m, p[i]) if m != None: d[nn] = m + a[nn] else: d[nn] = None m = -1 for i in range(n-k,n): if d[i] != None: m = max(m, d[i]) print(m) ```
vfc_25131
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1077/F1", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5 2 3\n5 1 3 10 1\n", "output": "18\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4109
Solve the following coding problem using the programming language python: Takahashi, who is a novice in competitive programming, wants to learn M algorithms. Initially, his understanding level of each of the M algorithms is 0. Takahashi is visiting a bookstore, where he finds N books on algorithms. The i-th book (1\leq i\leq N) is sold for C_i yen (the currency of Japan). If he buys and reads it, his understanding level of the j-th algorithm will increase by A_{i,j} for each j (1\leq j\leq M). There is no other way to increase the understanding levels of the algorithms. Takahashi's objective is to make his understanding levels of all the M algorithms X or higher. Determine whether this objective is achievable. If it is achievable, find the minimum amount of money needed to achieve it. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python N, M, X = list(map(int, input().split())) A = [0]*N for i in range(N): A[i] = list(map(int, input().split())) min_sump = -1 for i in range(2**(N+1)): sump = 0 sume = [0]*M for j in range(N): ns = "0" + str(N) +"b" bi = format(i,ns) if bi[-1-j] == "1": sump += A[j][0] sume = list(map(sum, zip(sume, A[j][1:]))) if all([i >= X for i in sume]): if min_sump == -1: min_sump = sump else: min_sump = min(min_sump,sump) print(min_sump) ```
vfc_25143
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://atcoder.jp/contests/abc167/tasks/abc167_c", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 3 10\n60 2 2 4\n70 8 7 9\n50 2 3 9\n", "output": "120\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 3 10\n100 3 1 4\n100 1 5 9\n100 2 6 5\n", "output": "-1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4112
Solve the following coding problem using the programming language python: The only difference between easy and hard versions is the constraints. Vova likes pictures with kittens. The news feed in the social network he uses can be represented as an array of $n$ consecutive pictures (with kittens, of course). Vova likes all these pictures, but some are more beautiful than the others: the $i$-th picture has beauty $a_i$. Vova wants to repost exactly $x$ pictures in such a way that: each segment of the news feed of at least $k$ consecutive pictures has at least one picture reposted by Vova; the sum of beauty values of reposted pictures is maximum possible. For example, if $k=1$ then Vova has to repost all the pictures in the news feed. If $k=2$ then Vova can skip some pictures, but between every pair of consecutive pictures Vova has to repost at least one of them. Your task is to calculate the maximum possible sum of values of reposted pictures if Vova follows conditions described above, or say that there is no way to satisfy all conditions. -----Input----- The first line of the input contains three integers $n, k$ and $x$ ($1 \le k, x \le n \le 5000$) โ€” the number of pictures in the news feed, the minimum length of segment with at least one repost in it and the number of pictures Vova is ready to repost. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the beauty of the $i$-th picture. -----Output----- Print -1 if there is no way to repost some pictures to satisfy all the conditions in the problem statement. Otherwise print one integer โ€” the maximum sum of values of reposted pictures if Vova follows conditions described in the problem statement. -----Examples----- Input 5 2 3 5 1 3 10 1 Output 18 Input 6 1 5 10 30 30 70 10 10 Output -1 Input 4 3 1 1 100 1 1 Output 100 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, k, x = list(map(int, input().split())) a = [None] + list(map(int, input().split())) lo, hi = 0, 10 ** 9 * 5000 q = [None] * (n + 1) def get(mid): f, r = 0, 0 q[0] = 0, 0, 0 for i in range(1, n + 1): if q[r][2] == i - k - 1: r += 1 cur = q[r][0] + a[i] - mid, q[r][1] + 1, i while r <= f and q[f] <= cur: f -= 1 f += 1 q[f] = cur if q[r][2] == n - k: r += 1 return q[r] while lo < hi: mid = (lo + hi + 1) >> 1 _, cnt, _ = get(mid) if cnt >= x: lo = mid else: hi = mid - 1 sm, _, _ = get(lo) ans = max(-1, sm + x * lo) print(ans) ```
vfc_25155
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1077/F2", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5 2 3\n5 1 3 10 1\n", "output": "18\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6 1 5\n10 30 30 70 10 10\n", "output": "-1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4121
Solve the following coding problem using the programming language python: Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches. The current state of the wall can be respresented by a sequence $a$ of $n$ integers, with $a_i$ being the height of the $i$-th part of the wall. Vova can only use $2 \times 1$ bricks to put in the wall (he has infinite supply of them, however). Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some $i$ the current height of part $i$ is the same as for part $i + 1$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $1$ of the wall or to the right of part $n$ of it). The next paragraph is specific to the version 1 of the problem. Vova can also put bricks vertically. That means increasing height of any part of the wall by 2. Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) โ€” the number of parts in the wall. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) โ€” the initial heights of the parts of the wall. -----Output----- Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise. -----Examples----- Input 5 2 1 1 2 5 Output YES Input 3 4 5 3 Output YES Input 2 10 10 Output YES Input 3 1 2 3 Output NO -----Note----- In the first example Vova can put a brick on parts 2 and 3 to make the wall $[2, 2, 2, 2, 5]$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $[5, 5, 5, 5, 5]$. In the second example Vova can put a brick vertically on part 3 to make the wall $[4, 5, 5]$, then horizontally on parts 2 and 3 to make it $[4, 6, 6]$ and then vertically on part 1 to make it $[6, 6, 6]$. In the third example the wall is already complete. 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())) q = (10 ** 6) * [-1] pnt = -1 ans = "YES" for i in range(n): if pnt == -1: pnt += 1 q[pnt] = a[i] else : if q[pnt] == a[i] or abs(q[pnt] - a[i]) % 2 == 0: q[pnt] = -1 pnt -= 1 else: pnt += 1 q[pnt] = a[i] if pnt > 0 : ans = "NO" print(ans) ```
vfc_25191
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1092/D1", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n2 1 1 2 5\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n4 5 3\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n10 10\n", "output": "YES\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n1 2 3\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n1 9 7 6 2 4 7 8 1 3\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n1\n", "output": "YES\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4122
Solve the following coding problem using the programming language python: A superhero fights with a monster. The battle consists of rounds, each of which lasts exactly $n$ minutes. After a round ends, the next round starts immediately. This is repeated over and over again. Each round has the same scenario. It is described by a sequence of $n$ numbers: $d_1, d_2, \dots, d_n$ ($-10^6 \le d_i \le 10^6$). The $i$-th element means that monster's hp (hit points) changes by the value $d_i$ during the $i$-th minute of each round. Formally, if before the $i$-th minute of a round the monster's hp is $h$, then after the $i$-th minute it changes to $h := h + d_i$. The monster's initial hp is $H$. It means that before the battle the monster has $H$ hit points. Print the first minute after which the monster dies. The monster dies if its hp is less than or equal to $0$. Print -1 if the battle continues infinitely. -----Input----- The first line contains two integers $H$ and $n$ ($1 \le H \le 10^{12}$, $1 \le n \le 2\cdot10^5$). The second line contains the sequence of integers $d_1, d_2, \dots, d_n$ ($-10^6 \le d_i \le 10^6$), where $d_i$ is the value to change monster's hp in the $i$-th minute of a round. -----Output----- Print -1 if the superhero can't kill the monster and the battle will last infinitely. Otherwise, print the positive integer $k$ such that $k$ is the first minute after which the monster is dead. -----Examples----- Input 1000 6 -100 -200 -300 125 77 -4 Output 9 Input 1000000000000 5 -1 0 0 0 0 Output 4999999999996 Input 10 4 -3 -6 5 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 H, n = list(map(int, input().split())) d = list(map(int, input().split())) t = 0 miPt = H for a in d: t += 1 H += a miPt = min(miPt, H) if H <= 0: print(t) return if sum(d) >= 0: print(-1) return jump = max(0, miPt // -sum(d) - 2) H -= jump * -sum(d) t += n * jump for i in range(100000000000000000): t += 1 H += d[i % n] if H <= 0: print(t) return ```
vfc_25195
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1141/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1000 6\n-100 -200 -300 125 77 -4\n", "output": "9\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4123
Solve the following coding problem using the programming language python: Two-gram is an ordered pair (i.e. string of length two) of capital Latin letters. For example, "AZ", "AA", "ZA" โ€” three distinct two-grams. You are given a string $s$ consisting of $n$ capital Latin letters. Your task is to find any two-gram contained in the given string as a substring (i.e. two consecutive characters of the string) maximal number of times. For example, for string $s$ = "BBAABBBA" the answer is two-gram "BB", which contained in $s$ three times. In other words, find any most frequent two-gram. Note that occurrences of the two-gram can overlap with each other. -----Input----- The first line of the input contains integer number $n$ ($2 \le n \le 100$) โ€” the length of string $s$. The second line of the input contains the string $s$ consisting of $n$ capital Latin letters. -----Output----- Print the only line containing exactly two capital Latin letters โ€” any two-gram contained in the given string $s$ as a substring (i.e. two consecutive characters of the string) maximal number of times. -----Examples----- Input 7 ABACABA Output AB Input 5 ZZZAA Output ZZ -----Note----- In the first example "BA" is also valid answer. In the second example the only two-gram "ZZ" can be printed because it contained in the string "ZZZAA" two times. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = int(input()) s = input() a = [[0] * 26 for _ in range(26)] for i in range(n -1): a[ord(s[i]) - ord('A')][ord(s[i + 1]) - ord('A')] += 1 mx = -1 for i in range(26): for j in range(26): if a[i][j] > mx: mx = a[i][j] ans = chr(i + ord('A')) + chr(j + ord('A')) print(ans) ```
vfc_25199
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/977/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "7\nABACABA\n", "output": "AB\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\nZZZAA\n", "output": "ZZ\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "26\nQWERTYUIOPASDFGHJKLZXCVBNM\n", "output": "AS\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\nQA\n", "output": "QA\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4124
Solve the following coding problem using the programming language python: You are given two strings $s$ and $t$. In a single move, you can choose any of two strings and delete the first (that is, the leftmost) character. After a move, the length of the string decreases by $1$. You can't choose a string if it is empty. For example: by applying a move to the string "where", the result is the string "here", by applying a move to the string "a", the result is an empty string "". You are required to make two given strings equal using the fewest number of moves. It is possible that, in the end, both strings will be equal to the empty string, and so, are equal to each other. In this case, the answer is obviously the sum of the lengths of the initial strings. Write a program that finds the minimum number of moves to make two given strings $s$ and $t$ equal. -----Input----- The first line of the input contains $s$. In the second line of the input contains $t$. Both strings consist only of lowercase Latin letters. The number of letters in each string is between 1 and $2\cdot10^5$, inclusive. -----Output----- Output the fewest number of moves required. It is possible that, in the end, both strings will be equal to the empty string, and so, are equal to each other. In this case, the answer is obviously the sum of the lengths of the given strings. -----Examples----- Input test west Output 2 Input codeforces yes Output 9 Input test yes Output 7 Input b ab Output 1 -----Note----- In the first example, you should apply the move once to the first string and apply the move once to the second string. As a result, both strings will be equal to "est". In the second example, the move should be applied to the string "codeforces" $8$ times. As a result, the string becomes "codeforces" $\to$ "es". The move should be applied to the string "yes" once. The result is the same string "yes" $\to$ "es". In the third example, you can make the strings equal only by completely deleting them. That is, in the end, both strings will be empty. In the fourth example, the first character of the second string should be deleted. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python s = input()[::-1] t = input()[::-1] i = 0 while i < min(len(s),len(t)) and s[i] == t[i]: i += 1 print(len(s) - i + len(t) - i) ```
vfc_25203
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1005/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "test\nwest\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "codeforces\nyes\n", "output": "9\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "test\nyes\n", "output": "7\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "b\nab\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "z\nz\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "abacabadabacaba\nabacabadacaba\n", "output": "18\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4130
Solve the following coding problem using the programming language python: There are $n$ boxers, the weight of the $i$-th boxer is $a_i$. Each of them can change the weight by no more than $1$ before the competition (the weight cannot become equal to zero, that is, it must remain positive). Weight is always an integer number. It is necessary to choose the largest boxing team in terms of the number of people, that all the boxers' weights in the team are different (i.e. unique). Write a program that for given current values โ€‹$a_i$ will find the maximum possible number of boxers in a team. It is possible that after some change the weight of some boxer is $150001$ (but no more). -----Input----- The first line contains an integer $n$ ($1 \le n \le 150000$) โ€” the number of boxers. The next line contains $n$ integers $a_1, a_2, \dots, a_n$, where $a_i$ ($1 \le a_i \le 150000$) is the weight of the $i$-th boxer. -----Output----- Print a single integer โ€” the maximum possible number of people in a team. -----Examples----- Input 4 3 2 4 1 Output 4 Input 6 1 1 1 4 4 4 Output 5 -----Note----- In the first example, boxers should not change their weights โ€” you can just make a team out of all of them. In the second example, one boxer with a weight of $1$ can be increased by one (get the weight of $2$), one boxer with a weight of $4$ can be reduced by one, and the other can be increased by one (resulting the boxers with a weight of $3$ and $5$, respectively). Thus, you can get a team consisting of boxers with weights of $5, 4, 3, 2, 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()) arr=list(map(int,input().split())) arr=sorted(arr) s=set() for val in arr: if val!=1 and val-1 not in s: s.add(val-1) elif val not in s: s.add(val) elif val+1 not in s: s.add(val+1) print(len(s)) ```
vfc_25227
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1203/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n3 2 4 1\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n1 1 1 4 4 4\n", "output": "5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n8 9 4 9 6 10 8 2 7 1\n", "output": "10\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7\n1 1 2 3 5 6 6\n", "output": "7\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n1\n", "output": "1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4133
Solve the following coding problem using the programming language python: Golorps are mysterious creatures who feed on variables. Golorp's name is a program in some programming language. Some scientists believe that this language is Befunge; golorps are tantalizingly silent. Variables consumed by golorps can take values from 0 to 9, inclusive. For each golorp its daily diet is defined by its name. Some golorps are so picky that they can't be fed at all. Besides, all golorps are very health-conscious and try to eat as little as possible. Given a choice of several valid sequences of variable values, each golorp will choose lexicographically smallest one. For the purposes of this problem you can assume that a golorp consists of jaws and a stomach. The number of variables necessary to feed a golorp is defined by the shape of its jaws. Variables can get to the stomach only via the jaws. A hungry golorp is visiting you. You know its name; feed it or figure out that it's impossible. -----Input----- The input is a single string (between 13 and 1024 characters long) โ€” the name of the visiting golorp. All names are similar and will resemble the ones given in the samples. The name is guaranteed to be valid. -----Output----- Output lexicographically smallest sequence of variable values fit for feeding this golorp. Values should be listed in the order in which they get into the jaws. If the golorp is impossible to feed, output "false". -----Examples----- Input ?(_-_/___*__):-___>__. Output 0010 Input ?(__-_+_/_____):-__>__,_____<__. Output false Input ?(______________________/____+_______*__-_____*______-___):-__<___,___<____,____<_____,_____<______,______<_______. Output 0250341 Input ?(__+___+__-___):-___>__. Output 0101 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python """ Codeforces April Fools Contest 2014 Problem I Author : chaotic_iak Language: Python 3.3.4 """ class InputHandlerObject(object): inputs = [] def getInput(self, n = 0): res = "" inputs = self.inputs if not inputs: inputs.extend(input().split(" ")) if n == 0: res = inputs[:] inputs[:] = [] while n > len(inputs): inputs.extend(input().split(" ")) if n > 0: res = inputs[:n] inputs[:n] = [] return res InputHandler = InputHandlerObject() g = InputHandler.getInput ############################## SOLUTION ############################## # ?(_/____+_______*__-_____*______-___):-__<___,___<____,____<_____,_____<______,______<_______. golorp = input().split(":-") golorp[0] = golorp[0][2:] ct = 0 jaws = [] for x in range(len(golorp[0])): if golorp[0][x] == "_": ct += 1 else: jaws.append(ct) ct = 0 ct = 0 conditionsraw = [] for x in range(len(golorp[1])): if golorp[1][x] == "_": ct += 1 else: conditionsraw.append(ct) conditionsraw.append(golorp[1][x]) ct = 0 conditions = [] for x in range(0, len(conditionsraw)//4): if conditionsraw[4*x+1] == ">": conditions.append((conditionsraw[4*x+2], conditionsraw[4*x])) else: conditions.append((conditionsraw[4*x], conditionsraw[4*x+2])) inedges = [[-1]] * (max(jaws) + 1) outedges = [[-1]] * (max(jaws) + 1) val = [-1] * (max(jaws) + 1) processed = [False] * (max(jaws) + 1) for x in jaws: inedges[x] = [] outedges[x] = [] for x, y in conditions: inedges[y].append(x) outedges[x].append(y) for i in range(10): for x in jaws: if not inedges[x] and not processed[x]: val[x] += 1 processed[x] = True for y in outedges[x]: val[y] = max(val[y], val[x]) inedges[y].remove(x) failure = False for x in jaws: if not processed[x] or val[x] > 9: failure = True break if failure: print("false") else: s = "" for x in jaws: s += str(val[x]) print(s) ```
vfc_25239
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/409/I", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "?(_-_/___*__):-___>__.\n", "output": "0010\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "?(__-_+_/_____):-__>__,_____<__.\n", "output": "false\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "?(______________________/____+_______*__-_____*______-___):-__<___,___<____,____<_____,_____<______,______<_______.\n", "output": "0250341\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "?(__+___+__-___):-___>__.\n", "output": "0101\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "?(__*___+_-____):-___>__,____<__.\n", "output": "1200\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "?(__):-__>__.\n", "output": "false\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4135
Solve the following coding problem using the programming language python: A string $s$ of length $n$ can be encrypted by the following algorithm: iterate over all divisors of $n$ in decreasing order (i.e. from $n$ to $1$), for each divisor $d$, reverse the substring $s[1 \dots d]$ (i.e. the substring which starts at position $1$ and ends at position $d$). For example, the above algorithm applied to the string $s$="codeforces" leads to the following changes: "codeforces" $\to$ "secrofedoc" $\to$ "orcesfedoc" $\to$ "rocesfedoc" $\to$ "rocesfedoc" (obviously, the last reverse operation doesn't change the string because $d=1$). You are given the encrypted string $t$. Your task is to decrypt this string, i.e., to find a string $s$ such that the above algorithm results in string $t$. It can be proven that this string $s$ always exists and is unique. -----Input----- The first line of input consists of a single integer $n$ ($1 \le n \le 100$) โ€” the length of the string $t$. The second line of input consists of the string $t$. The length of $t$ is $n$, and it consists only of lowercase Latin letters. -----Output----- Print a string $s$ such that the above algorithm results in $t$. -----Examples----- Input 10 rocesfedoc Output codeforces Input 16 plmaetwoxesisiht Output thisisexampletwo Input 1 z Output z -----Note----- The first example is described in the problem statement. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = int(input()) s = input() for d in range(1, n+1): if n%d == 0: t1 = s[:d] t2 = s[d:] s = t1[::-1] + t2 print(s) ```
vfc_25247
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/999/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "10\nrocesfedoc\n", "output": "codeforces\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "16\nplmaetwoxesisiht\n", "output": "thisisexampletwo\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4136
Solve the following coding problem using the programming language python: A + B is often used as an example of the easiest problem possible to show some contest platform. However, some scientists have observed that sometimes this problem is not so easy to get accepted. Want to try? -----Input----- The input contains two integers a and b (0 โ‰ค a, b โ‰ค 10^3), separated by a single space. -----Output----- Output the sum of the given integers. -----Examples----- Input 5 14 Output 19 Input 381 492 Output 873 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python a, b = map(int,input().split()) print(a+b) ```
vfc_25251
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/409/H", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5 14\n", "output": "19\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "381 492\n", "output": "873\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "536 298\n", "output": "834\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4137
Solve the following coding problem using the programming language python: In this problem you will write a simple generator of Brainfuck (https://en.wikipedia.org/wiki/Brainfuck) calculators. You are given an arithmetic expression consisting of integers from 0 to 255 and addition/subtraction signs between them. Output a Brainfuck program which, when executed, will print the result of evaluating this expression. We use a fairly standard Brainfuck interpreter for checking the programs: 30000 memory cells. memory cells store integers from 0 to 255 with unsigned 8-bit wraparound. console input (, command) is not supported, but it's not needed for this problem. -----Input----- The only line of input data contains the arithmetic expression. The expression will contain between 2 and 10 operands, separated with arithmetic signs plus and/or minus. Each operand will be an integer between 0 and 255, inclusive. The calculations result is guaranteed to be an integer between 0 and 255, inclusive (results of intermediary calculations might be outside of these boundaries). -----Output----- Output a Brainfuck program which, when executed, will print the result of evaluating this expression. The program must be at most 5000000 characters long (including the non-command characters), and its execution must be complete in at most 50000000 steps. -----Examples----- Input 2+3 Output ++> +++> <[<+>-]< ++++++++++++++++++++++++++++++++++++++++++++++++. Input 9-7 Output +++++++++> +++++++> <[<->-]< ++++++++++++++++++++++++++++++++++++++++++++++++. -----Note----- You can download the source code of the Brainfuck interpreter by the link http://assets.codeforces.com/rounds/784/bf.cpp. We use this code to interpret outputs. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import re s = input() ops = re.split('([+-])', s) assert len(ops) % 2 == 1 ops = ['+'] + ops total = 0 for i in range(0, len(ops), 2): if ops[i] == '+': total += int(ops[i+1]) elif ops[i] == '-': total -= int(ops[i+1]) else: assert False for b in bytes(str(total), 'ascii'): print('+' * b + '.>') ```
vfc_25255
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/784/G", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2+3\n", "output": "+++++++++++++++++++++++++++++++++++++++++++++++++++++.>\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "9-7\n", "output": "++++++++++++++++++++++++++++++++++++++++++++++++++.>\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1+1+1\n", "output": "+++++++++++++++++++++++++++++++++++++++++++++++++++.>\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4138
Solve the following coding problem using the programming language python: The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one โ€” from $1$ to $2$, the third one โ€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) โ€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^{18})$ โ€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ โ€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999999999999 1000000000000000000 Output 8 2 4 1 -----Note----- Answers on queries from the first example are described in the problem statement. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python l = [0] def count(size): nums = (10**size - 10**(size - 1)) small = l[size-1] + size large = l[size-1] + nums * size if len(l) <= size: l.append(large) return (nums * (small + large))//2 def test(minSize, size, val): out = minSize * val + size * ((val + 1) * val)//2 return out q = int(input()) for _ in range(q): want = int(input()) size = 1 while want > count(size): want -= count(size) size += 1 minSize = l[size - 1] lo = 0 #Impossible hi = (10**size - 10**(size - 1)) #Possible while hi - lo > 1: testV = (lo + hi) // 2 out = test(minSize, size, testV) if out < want: lo = testV else: hi = testV want -= test(minSize, size, lo) newS = 1 while 9 * (10**(newS - 1)) * newS < want: want -= 9 * (10**(newS - 1)) * newS newS += 1 want -= 1 more = want//newS dig = want % newS value = 10**(newS - 1) + more print(str(value)[dig]) ```
vfc_25259
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1216/E2", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n1\n3\n20\n38\n56\n", "output": "1\n2\n5\n2\n0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4149
Solve the following coding problem using the programming language python: Authors guessed an array $a$ consisting of $n$ integers; each integer is not less than $2$ and not greater than $2 \cdot 10^5$. You don't know the array $a$, but you know the array $b$ which is formed from it with the following sequence of operations: Firstly, let the array $b$ be equal to the array $a$; Secondly, for each $i$ from $1$ to $n$: if $a_i$ is a prime number, then one integer $p_{a_i}$ is appended to array $b$, where $p$ is an infinite sequence of prime numbers ($2, 3, 5, \dots$); otherwise (if $a_i$ is not a prime number), the greatest divisor of $a_i$ which is not equal to $a_i$ is appended to $b$; Then the obtained array of length $2n$ is shuffled and given to you in the input. Here $p_{a_i}$ means the $a_i$-th prime number. The first prime $p_1 = 2$, the second one is $p_2 = 3$, and so on. Your task is to recover any suitable array $a$ that forms the given array $b$. It is guaranteed that the answer exists (so the array $b$ is obtained from some suitable array $a$). If there are multiple answers, you can print any. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) โ€” the number of elements in $a$. The second line of the input contains $2n$ integers $b_1, b_2, \dots, b_{2n}$ ($2 \le b_i \le 2750131$), where $b_i$ is the $i$-th element of $b$. $2750131$ is the $199999$-th prime number. -----Output----- In the only line of the output print $n$ integers $a_1, a_2, \dots, a_n$ ($2 \le a_i \le 2 \cdot 10^5$) in any order โ€” the array $a$ from which the array $b$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. -----Examples----- Input 3 3 5 2 3 2 4 Output 3 4 2 Input 1 2750131 199999 Output 199999 Input 1 3 6 Output 6 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 from collections import Counter from math import sqrt m = 2750131 + 100 f = list(range(m)) for d in range(2, int(sqrt(m)) + 10): if f[d] == d: i, k = 2, d << 1 while k < m: if f[k] == k: f[k] = i k += d i += 1 np = list(range(m)) c = 1 for i in range(2, m): if f[i] == i: np[i] = c c += 1 n = int(input()) b = sorted(list(map(int, input().split())), reverse=True) d = Counter(b) a = [] i, la = 0, 0 while la < n: if d[b[i]] > 0: la += 1 if f[b[i]] == b[i]: a.append(np[b[i]]) d[b[i]] -= 1 d[np[b[i]]] -= 1 else: a.append(b[i]) d[b[i]] -= 1 d[f[b[i]]] -= 1 i += 1 print(*a) ```
vfc_25303
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1176/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n3 5 2 3 2 4\n", "output": "3 4 2 ", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n2750131 199999\n", "output": "199999 ", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n3 6\n", "output": "6 ", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n3 2\n", "output": "2 ", "type": "stdin_stdout" } ] }
apps
verifiable_code
4150
Solve the following coding problem using the programming language python: There are $n$ students standing in a row. Two coaches are forming two teams โ€” the first coach chooses the first team and the second coach chooses the second team. The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive. Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team). Your problem is to determine which students will be taken into the first team and which students will be taken into the second team. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) โ€” the number of students and the value determining the range of chosen students during each move, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct. -----Output----- Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise. -----Examples----- Input 5 2 2 4 5 3 1 Output 11111 Input 5 1 2 1 3 5 4 Output 22111 Input 7 1 7 2 1 3 5 4 6 Output 1121122 Input 5 1 2 4 5 3 1 Output 21112 -----Note----- In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team). In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team). In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python IN = input rint = lambda: int(IN()) rmint = lambda: map(int, IN().split()) rlist = lambda: list(rmint()) n, k = rmint() pr = [i for i in range(-1, n - 1)] nx = [i for i in range(+1, n + 1)] ans = [0] * n p = [0] * n i = 0 for g in rlist(): p[n-(g-1)-1] = i i += 1 def dl(x, t): ans[x] = t if nx[x] < n: pr[nx[x]] = pr[x] if pr[x] >= 0: nx[pr[x]] = nx[x] t = 1 for c in p: #print(ans) #print(pr) #print(nx) if ans[c]: continue dl(c, t) j = pr[c] for i in range(k): if j < 0: break dl(j, t) j = pr[j] j = nx[c] for i in range(k): if j >= n: break dl(j, t) j = nx[j] t = 3 - t for o in ans: print(o, end='') ```
vfc_25307
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1154/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5 2\n2 4 5 3 1\n", "output": "11111\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 1\n2 1 3 5 4\n", "output": "22111\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4152
Solve the following coding problem using the programming language python: A sequence $a_1, a_2, \dots, a_n$ is called good if, for each element $a_i$, there exists an element $a_j$ ($i \ne j$) such that $a_i+a_j$ is a power of two (that is, $2^d$ for some non-negative integer $d$). For example, the following sequences are good: $[5, 3, 11]$ (for example, for $a_1=5$ we can choose $a_2=3$. Note that their sum is a power of two. Similarly, such an element can be found for $a_2$ and $a_3$), $[1, 1, 1, 1023]$, $[7, 39, 89, 25, 89]$, $[]$. Note that, by definition, an empty sequence (with a length of $0$) is good. For example, the following sequences are not good: $[16]$ (for $a_1=16$, it is impossible to find another element $a_j$ such that their sum is a power of two), $[4, 16]$ (for $a_1=4$, it is impossible to find another element $a_j$ such that their sum is a power of two), $[1, 3, 2, 8, 8, 8]$ (for $a_3=2$, it is impossible to find another element $a_j$ such that their sum is a power of two). You are given a sequence $a_1, a_2, \dots, a_n$. What is the minimum number of elements you need to remove to make it good? You can delete an arbitrary set of elements. -----Input----- The first line contains the integer $n$ ($1 \le n \le 120000$) โ€” the length of the given sequence. The second line contains the sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print the minimum number of elements needed to be removed from the given sequence in order to make it good. It is possible that you need to delete all $n$ elements, make it empty, and thus get a good sequence. -----Examples----- Input 6 4 7 1 5 4 9 Output 1 Input 5 1 2 3 4 5 Output 2 Input 1 16 Output 1 Input 4 1 1 1 1023 Output 0 -----Note----- In the first example, it is enough to delete one element $a_4=5$. The remaining elements form the sequence $[4, 7, 1, 4, 9]$, which 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 collections import defaultdict maxn = 2000000000 mk = [] bs = 1 while bs <= maxn: mk.append(bs) bs *= 2 n = int(input()) ls = [int(i) for i in input().split()] dic = defaultdict(int) for i in ls: dic[i] += 1 cnt = 0 for i in ls: dic[i] -= 1 flag = False for j in mk: if dic[j - i] > 0: flag = True break if not flag: cnt += 1 dic[i] += 1 print(cnt) ```
vfc_25315
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1005/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6\n4 7 1 5 4 9\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n1 2 3 4 5\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n16\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n1 1 1 1023\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4156
Solve the following coding problem using the programming language python: The busses in Berland are equipped with a video surveillance system. The system records information about changes in the number of passengers in a bus after stops. If $x$ is the number of passengers in a bus just before the current bus stop and $y$ is the number of passengers in the bus just after current bus stop, the system records the number $y-x$. So the system records show how number of passengers changed. The test run was made for single bus and $n$ bus stops. Thus, the system recorded the sequence of integers $a_1, a_2, \dots, a_n$ (exactly one number for each bus stop), where $a_i$ is the record for the bus stop $i$. The bus stops are numbered from $1$ to $n$ in chronological order. Determine the number of possible ways how many people could be in the bus before the first bus stop, if the bus has a capacity equals to $w$ (that is, at any time in the bus there should be from $0$ to $w$ passengers inclusive). -----Input----- The first line contains two integers $n$ and $w$ $(1 \le n \le 1\,000, 1 \le w \le 10^{9})$ โ€” the number of bus stops and the capacity of the bus. The second line contains a sequence $a_1, a_2, \dots, a_n$ $(-10^{6} \le a_i \le 10^{6})$, where $a_i$ equals to the number, which has been recorded by the video system after the $i$-th bus stop. -----Output----- Print the number of possible ways how many people could be in the bus before the first bus stop, if the bus has a capacity equals to $w$. If the situation is contradictory (i.e. for any initial number of passengers there will be a contradiction), print 0. -----Examples----- Input 3 5 2 1 -3 Output 3 Input 2 4 -1 1 Output 4 Input 4 10 2 4 1 2 Output 2 -----Note----- In the first example initially in the bus could be $0$, $1$ or $2$ passengers. In the second example initially in the bus could be $1$, $2$, $3$ or $4$ passengers. In the third example initially in the bus could be $0$ or $1$ passenger. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python stops, cap = map(int,input().split()) a = list(map(int,input().split())) start_max = cap start_min = 0 current = 0 for x in a: current += x start_max = min(cap-current, start_max) start_min = max(start_min,-current) if abs(current) > cap: print (0) break else: if start_max<start_min: print (0) else: print (start_max-start_min + 1) ```
vfc_25331
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/978/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 5\n2 1 -3\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 4\n-1 1\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 10\n2 4 1 2\n", "output": "2\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4157
Solve the following coding problem using the programming language python: Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds: divide the number $x$ by $3$ ($x$ must be divisible by $3$); multiply the number $x$ by $2$. After each operation, Polycarp writes down the result on the board and replaces $x$ by the result. So there will be $n$ numbers on the board after all. You are given a sequence of length $n$ โ€” the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board. Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number. It is guaranteed that the answer exists. -----Input----- The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) โ€” the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) โ€” rearranged (reordered) sequence that Polycarp can wrote down on the board. -----Output----- Print $n$ integer numbers โ€” rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board. It is guaranteed that the answer exists. -----Examples----- Input 6 4 8 6 3 12 9 Output 9 3 6 12 4 8 Input 4 42 28 84 126 Output 126 42 84 28 Input 2 1000000000000000000 3000000000000000000 Output 3000000000000000000 1000000000000000000 -----Note----- In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def powof3(x): ans=0 while(x%3==0): x=x//3 ans+=1 return ans n=int(input()) a=list(map(int,input().split())) for i in range(n): a[i]=[-1*powof3(a[i]),a[i]] a.sort() for i in range(n): a[i]=a[i][1] print(*a) ```
vfc_25335
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/977/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6\n4 8 6 3 12 9\n", "output": "9 3 6 12 4 8 \n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4158
Solve the following coding problem using the programming language python: There are $n$ distinct points on a coordinate line, the coordinate of $i$-th point equals to $x_i$. Choose a subset of the given set of points such that the distance between each pair of points in a subset is an integral power of two. It is necessary to consider each pair of points, not only adjacent. Note that any subset containing one element satisfies the condition above. Among all these subsets, choose a subset with maximum possible size. In other words, you have to choose the maximum possible number of points $x_{i_1}, x_{i_2}, \dots, x_{i_m}$ such that for each pair $x_{i_j}$, $x_{i_k}$ it is true that $|x_{i_j} - x_{i_k}| = 2^d$ where $d$ is some non-negative integer number (not necessarily the same for each pair of points). -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) โ€” the number of points. The second line contains $n$ pairwise distinct integers $x_1, x_2, \dots, x_n$ ($-10^9 \le x_i \le 10^9$) โ€” the coordinates of points. -----Output----- In the first line print $m$ โ€” the maximum possible number of points in a subset that satisfies the conditions described above. In the second line print $m$ integers โ€” the coordinates of points in the subset you have chosen. If there are multiple answers, print any of them. -----Examples----- Input 6 3 5 4 7 10 12 Output 3 7 3 5 Input 5 -1 2 5 8 11 Output 1 8 -----Note----- In the first example the answer is $[7, 3, 5]$. Note, that $|7-3|=4=2^2$, $|7-5|=2=2^1$ and $|3-5|=2=2^1$. You can't find a subset having more points satisfying the required property. 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()) points = set(int(x) for x in input().strip().split()) powers = [2**i for i in range(31)] for point in points: for power in powers: if point + power in points and point + power + power in points: print(3) print(point, point + power, point + power + power) return for point in points: for power in powers: if point + power in points: print(2) print(point, point + power) return print(1) print(points.pop()) ```
vfc_25339
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/988/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6\n3 5 4 7 10 12\n", "output": "3\n3 4 5 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n-1 2 5 8 11\n", "output": "1\n-1 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n42\n", "output": "1\n42 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n0 -536870912 536870912\n", "output": "3\n-536870912 0 536870912 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n536870912 -536870912\n", "output": "2\n-536870912 536870912 \n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4171
Solve the following coding problem using the programming language python: The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) โ€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer โ€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!! # TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!! # TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!! from sys import stdin, stdout from collections import defaultdict from collections import deque import math import copy #T = int(input()) #N = int(input()) #s1 = input() #s2 = input() N,K = [int(x) for x in stdin.readline().split()] arr = [int(x) for x in stdin.readline().split()] arr.sort() freq = {} for i in range(N): num = arr[i] if num not in freq: freq[num] = [] round = 0 freq[num].append(0) while num!=0: round += 1 num = num//2 if num not in freq: freq[num] = [] freq[num].append(round) res = 999999999999 for key in freq: if len(freq[key])<K: continue else: s = sum(freq[key][:K]) res = min(res,s) print(res) ```
vfc_25391
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1213/D2", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5 3\n1 2 2 4 5\n", "output": "1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4172
Solve the following coding problem using the programming language python: You are given the array $a$ consisting of $n$ elements and the integer $k \le n$. You want to obtain at least $k$ equal elements in the array $a$. In one move, you can make one of the following two operations: Take one of the minimum elements of the array and increase its value by one (more formally, if the minimum value of $a$ is $mn$ then you choose such index $i$ that $a_i = mn$ and set $a_i := a_i + 1$); take one of the maximum elements of the array and decrease its value by one (more formally, if the maximum value of $a$ is $mx$ then you choose such index $i$ that $a_i = mx$ and set $a_i := a_i - 1$). Your task is to calculate the minimum number of moves required to obtain at least $k$ equal elements in the array. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) โ€” the number of elements in $a$ and the required number of equal elements. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer โ€” the minimum number of moves required to obtain at least $k$ equal elements in the array. -----Examples----- Input 6 5 1 2 2 4 2 3 Output 3 Input 7 5 3 3 2 1 1 1 3 Output 4 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python #!usr/bin/env python3 from collections import defaultdict, deque from heapq import heappush, heappop from itertools import permutations, accumulate import sys import math import bisect def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS():return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == "\n": return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 1000000007 def solve(): n,k = LI() a = LI() a.sort() d = defaultdict(lambda : 0) c = defaultdict(lambda : 0) s = [0] for i in a: d[i] += i c[i] += 1 s.append(s[-1]+i) ans = float("inf") p = -1 for i in a: if i == p: continue if k <= c[i]: ans = 0 break l,r = bisect.bisect_left(a,i),bisect.bisect_right(a,i) m = r if m >= k: ns = l*(i-1)-s[l] su = ns+k-c[i] if su < ans: ans = su m = n-l if m >= k: ns = s[n]-s[r]-(n-r)*(i+1) su = ns+k-c[i] if su < ans: ans = su ns = s[n]-s[r]-(n-r)*(i+1)+l*(i-1)-s[l] su = ns+k-c[i] if su < ans: ans = su p = i print(ans) return #Solve def __starting_point(): solve() __starting_point() ```
vfc_25395
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1328/F", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6 5\n1 2 2 4 2 3\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7 5\n3 3 2 1 1 1 3\n", "output": "4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1\n1\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1\n1000000000\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4173
Solve the following coding problem using the programming language python: Polycarp wants to cook a soup. To do it, he needs to buy exactly $n$ liters of water. There are only two types of water bottles in the nearby shop โ€” $1$-liter bottles and $2$-liter bottles. There are infinitely many bottles of these two types in the shop. The bottle of the first type costs $a$ burles and the bottle of the second type costs $b$ burles correspondingly. Polycarp wants to spend as few money as possible. Your task is to find the minimum amount of money (in burles) Polycarp needs to buy exactly $n$ liters of water in the nearby shop if the bottle of the first type costs $a$ burles and the bottle of the second type costs $b$ burles. You also have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) โ€” the number of queries. The next $n$ lines contain queries. The $i$-th query is given as three space-separated integers $n_i$, $a_i$ and $b_i$ ($1 \le n_i \le 10^{12}, 1 \le a_i, b_i \le 1000$) โ€” how many liters Polycarp needs in the $i$-th query, the cost (in burles) of the bottle of the first type in the $i$-th query and the cost (in burles) of the bottle of the second type in the $i$-th query, respectively. -----Output----- Print $q$ integers. The $i$-th integer should be equal to the minimum amount of money (in burles) Polycarp needs to buy exactly $n_i$ liters of water in the nearby shop if the bottle of the first type costs $a_i$ burles and the bottle of the second type costs $b_i$ burles. -----Example----- Input 4 10 1 3 7 3 2 1 1000 1 1000000000000 42 88 Output 10 9 1000 42000000000000 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python q = int(input()) for i in range(q): n, a, b = list(map(int, input().split())) if n % 2 == 0: print(min(n * a, n // 2 * b)) else: print(min(n * a, (n // 2) * b + a)) ```
vfc_25399
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1118/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n10 1 3\n7 3 2\n1 1000 1\n1000000000000 42 88\n", "output": "10\n9\n1000\n42000000000000\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4186
Solve the following coding problem using the programming language python: There are $n$ students in a university. The number of students is even. The $i$-th student has programming skill equal to $a_i$. The coach wants to form $\frac{n}{2}$ teams. Each team should consist of exactly two students, and each student should belong to exactly one team. Two students can form a team only if their skills are equal (otherwise they cannot understand each other and cannot form a team). Students can solve problems to increase their skill. One solved problem increases the skill by one. The coach wants to know the minimum total number of problems students should solve to form exactly $\frac{n}{2}$ teams (i.e. each pair of students should form a team). Your task is to find this number. -----Input----- The first line of the input contains one integer $n$ ($2 \le n \le 100$) โ€” the number of students. It is guaranteed that $n$ is even. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 100$), where $a_i$ is the skill of the $i$-th student. -----Output----- Print one number โ€” the minimum total number of problems students should solve to form exactly $\frac{n}{2}$ teams. -----Examples----- Input 6 5 10 2 3 14 5 Output 5 Input 2 1 100 Output 99 -----Note----- In the first example the optimal teams will be: $(3, 4)$, $(1, 6)$ and $(2, 5)$, where numbers in brackets are indices of students. Then, to form the first team the third student should solve $1$ problem, to form the second team nobody needs to solve problems and to form the third team the second student should solve $4$ problems so the answer is $1 + 4 = 5$. In the second example the first student should solve $99$ problems to form a team with the second one. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def solve(): n = int(input()) A = [int(k) for k in input().split()] A.sort() ans = 0 for i in range(0,n,2): ans += (A[i+1] - A[i]) print (ans) solve() ```
vfc_25451
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1092/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6\n5 10 2 3 14 5\n", "output": "5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n1 100\n", "output": "99\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4187
Solve the following coding problem using the programming language python: Each day in Berland consists of $n$ hours. Polycarp likes time management. That's why he has a fixed schedule for each day โ€” it is a sequence $a_1, a_2, \dots, a_n$ (each $a_i$ is either $0$ or $1$), where $a_i=0$ if Polycarp works during the $i$-th hour of the day and $a_i=1$ if Polycarp rests during the $i$-th hour of the day. Days go one after another endlessly and Polycarp uses the same schedule for each day. What is the maximal number of continuous hours during which Polycarp rests? It is guaranteed that there is at least one working hour in a day. -----Input----- The first line contains $n$ ($1 \le n \le 2\cdot10^5$) โ€” number of hours per day. The second line contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 1$), where $a_i=0$ if the $i$-th hour in a day is working and $a_i=1$ if the $i$-th hour is resting. It is guaranteed that $a_i=0$ for at least one $i$. -----Output----- Print the maximal number of continuous hours during which Polycarp rests. Remember that you should consider that days go one after another endlessly and Polycarp uses the same schedule for each day. -----Examples----- Input 5 1 0 1 0 1 Output 2 Input 6 0 1 0 1 1 0 Output 2 Input 7 1 0 1 1 1 0 1 Output 3 Input 3 0 0 0 Output 0 -----Note----- In the first example, the maximal rest starts in last hour and goes to the first hour of the next day. In the second example, Polycarp has maximal rest from the $4$-th to the $5$-th hour. In the third example, Polycarp has maximal rest from the $3$-rd to the $5$-th hour. In the fourth example, Polycarp has no rest 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()) a = list(map(int, input().split())) b = [] for i in range(len(a)): b.append(a[i]) for i in range(len(a)): b.append(a[i]) q = 0 r = set() for i in b: if i: q += 1 else: r.add(q) q = 0 print(max(r)) ```
vfc_25455
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1141/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n1 0 1 0 1\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n0 1 0 1 1 0\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7\n1 0 1 1 1 0 1\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n0 0 0\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4188
Solve the following coding problem using the programming language python: Little Petya wanted to give an April Fools Day present to some scientists. After some hesitation he decided to give them the array that he got as a present in Codeforces Round #153 (Div.2). The scientists rejoiced at the gift and decided to put some important facts to this array. Here are the first few of the facts: The highest mountain above sea level in the world is Mount Everest. Its peak rises to 8848 m. The largest board game tournament consisted of 958 participants playing chapaev. The largest online maths competition consisted of 12766 participants. The Nile is credited as the longest river in the world. From its farthest stream in Burundi, it extends 6695 km in length. While not in flood, the main stretches of the Amazon river in South America can reach widths of up to 1100 km at its widest points. Angel Falls is the highest waterfall. Its greatest single drop measures 807 m. The Hotel Everest View above Namche, Nepal โ€” the village closest to Everest base camp โ€“ is at a record height of 31962 m Uranium is the heaviest of all the naturally occurring elements. Its most common isotope has a nucleus containing 146 neutrons. The coldest permanently inhabited place is the Siberian village of Oymyakon, where the temperature of -68ยฐC was registered in the twentieth century. The longest snake held in captivity is over 25 feet long. Its name is Medusa. Colonel Meow holds the world record for longest fur on a cat โ€” almost 134 centimeters. Sea otters can have up to 10000 hairs per square inch. This is the most dense fur in the animal kingdom. The largest state of USA is Alaska; its area is 663268 square miles Alaska has a longer coastline than all of the other 49 U.S. States put together: it is 154103 miles long. Lake Baikal is the largest freshwater lake in the world. It reaches 1642ย meters in depth and contains around one-fifth of the worldโ€™s unfrozen fresh water. The most colorful national flag is the one of Turkmenistan, with 106 colors. -----Input----- The input will contain a single integer between 1 and 16. -----Output----- Output a single integer. -----Examples----- Input 1 Output 1 Input 7 Output 0 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # arr = [ # 1, # 1 # 0, # 0, # 1, # think so # 1, # not sure # 0, # not sure # 0, # 2 # 1, # 1, # think so # 1, # 0, # 0, # 1, # 0, # 1, # 0, # ] arr = [ 1, # 1 0, 0, 1, # think so 0, # not sure 1, # not sure 0, # 2 1, 1, # think so 1, 0, 0, 1, 0, 1, 0, ] n = int(input()) - 1 print(arr[n]) # assert n in {1, 7} or n <= ```
vfc_25459
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/409/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "1\n", "output": "1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4189
Solve the following coding problem using the programming language python: Not to be confused with chessboard. [Image] -----Input----- The first line of input contains a single integer N (1 โ‰ค N โ‰ค 100) โ€” the number of cheeses you have. The next N lines describe the cheeses you have. Each line contains two space-separated strings: the name of the cheese and its type. The name is a string of lowercase English letters between 1 and 10 characters long. The type is either "soft" or "hard. All cheese names are distinct. -----Output----- Output a single number. -----Examples----- Input 9 brie soft camembert soft feta soft goat soft muenster soft asiago hard cheddar hard gouda hard swiss hard Output 3 Input 6 parmesan hard emmental hard edam hard colby hard gruyere hard asiago hard Output 4 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # python3 def main(): n = int(input()) hard, soft = 0, 0 while n: n -= 1 if input().split()[1] == "hard": hard += 1 else: soft += 1 if hard < soft: hard, soft = soft, hard assert soft <= hard side = 1 while side ** 2 / 2 < soft or side ** 2 / 2 + (side & 1) < hard: side += 1 print(side) main() ```
vfc_25463
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/952/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "9\nbrie soft\ncamembert soft\nfeta soft\ngoat soft\nmuenster soft\nasiago hard\ncheddar hard\ngouda hard\nswiss hard\n", "output": "3\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4190
Solve the following coding problem using the programming language python: You are given two arrays $a$ and $b$, both of length $n$. All elements of both arrays are from $0$ to $n-1$. You can reorder elements of the array $b$ (if you want, you may leave the order of elements as it is). After that, let array $c$ be the array of length $n$, the $i$-th element of this array is $c_i = (a_i + b_i) \% n$, where $x \% y$ is $x$ modulo $y$. Your task is to reorder elements of the array $b$ to obtain the lexicographically minimum possible array $c$. Array $x$ of length $n$ is lexicographically less than array $y$ of length $n$, if there exists such $i$ ($1 \le i \le n$), that $x_i < y_i$, and for any $j$ ($1 \le j < i$) $x_j = y_j$. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) โ€” the number of elements in $a$, $b$ and $c$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i < n$), where $a_i$ is the $i$-th element of $a$. The third line of the input contains $n$ integers $b_1, b_2, \dots, b_n$ ($0 \le b_i < n$), where $b_i$ is the $i$-th element of $b$. -----Output----- Print the lexicographically minimum possible array $c$. Recall that your task is to reorder elements of the array $b$ and obtain the lexicographically minimum possible array $c$, where the $i$-th element of $c$ is $c_i = (a_i + b_i) \% n$. -----Examples----- Input 4 0 1 2 1 3 2 1 1 Output 1 0 0 2 Input 7 2 5 1 5 3 4 3 2 4 3 5 6 5 1 Output 0 0 0 1 0 2 4 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline # input = io.StringIO(os.read(0, os.fstat(0).st_size).decode()).readline ii = lambda:int(input()) kk=lambda:map(int,input().split()) # k2=lambda:map(lambda x:int(x)-1, input().split()) ll=lambda:list(kk()) n = ii() parents, rank = [-1]*n, [0]*n loc = [i for i in range(n)] def findParent(x): stack = [] curr = x while parents[curr] != -1: stack.append(curr) curr = parents[curr] for v in stack: parents[v] = curr return curr def union(x, y): best = None xP, yP = findParent(x), findParent(y) if rank[x] < rank[y]: best=parents[xP] = yP; elif rank[x] > rank[y]: best=parents[yP] = xP else: best=parents[yP] = xP rank[xP]+=1 if values[loc[best]] == 0: loc[best] = loc[xP] if yP is best else loc[yP] a = kk() values = [0]*n tbp=[] for x in kk(): values[x]+=1 for i in range(n): if values[i] == 0: union(i, (i+1)%n) for v in a: p = loc[findParent((n-v)%n)] tbp.append((v+p)%n) values[p]-=1 if values[p] == 0: union(p, (p+1)%n) print(*tbp) ```
vfc_25467
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1157/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n0 1 2 1\n3 2 1 1\n", "output": "1 0 0 2 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7\n2 5 1 5 3 4 3\n2 4 3 5 6 5 1\n", "output": "0 0 0 1 0 2 4 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n0\n0\n", "output": "0 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n0 0\n1 1\n", "output": "1 1 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n0 1 1\n0 1 0\n", "output": "0 1 2 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n1 1\n0 1\n", "output": "0 1 \n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4191
Solve the following coding problem using the programming language python: [Image] -----Input----- The input consists of four lines, each line containing a single digit 0 or 1. -----Output----- Output a single digit, 0 or 1. -----Example----- Input 0 1 1 0 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 collections as col import itertools as its import sys import operator from bisect import bisect_left, bisect_right from copy import copy, deepcopy class Solver: def __init__(self): pass def solve(self): a = int(input()) b = int(input()) c = int(input()) d = int(input()) l11 = a ^ b l12 = c | d l13 = b & c l14 = a ^ d l21 = l11 & l12 l22 = l13 | l14 print(l21 ^ l22) def __starting_point(): s = Solver() s.solve() __starting_point() ```
vfc_25471
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/784/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "0\n1\n1\n0\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "0\n0\n0\n0\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n0\n0\n0\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "0\n1\n0\n0\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4206
Solve the following coding problem using the programming language python: Polycarp likes numbers that are divisible by 3. He has a huge number $s$. Polycarp wants to cut from it the maximum number of numbers that are divisible by $3$. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after $m$ such cuts, there will be $m+1$ parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by $3$. For example, if the original number is $s=3121$, then Polycarp can cut it into three parts with two cuts: $3|1|21$. As a result, he will get two numbers that are divisible by $3$. Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid. What is the maximum number of numbers divisible by $3$ that Polycarp can obtain? -----Input----- The first line of the input contains a positive integer $s$. The number of digits of the number $s$ is between $1$ and $2\cdot10^5$, inclusive. The first (leftmost) digit is not equal to 0. -----Output----- Print the maximum number of numbers divisible by $3$ that Polycarp can get by making vertical cuts in the given number $s$. -----Examples----- Input 3121 Output 2 Input 6 Output 1 Input 1000000000000000000000000000000000 Output 33 Input 201920181 Output 4 -----Note----- In the first example, an example set of optimal cuts on the number is 3|1|21. In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by $3$. In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and $33$ digits 0. Each of the $33$ digits 0 forms a number that is divisible by $3$. In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers $0$, $9$, $201$ and $81$ are divisible by $3$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n=input() ls='' t=0 for i in range(len(n)): if int(n[i])%3==0: ls='' t+=1 else: ls+=n[i] for j in range(0,len(ls)): if int(ls[j:])%3==0: t+=1 ls='' break print(t) ''' //////////////// ////// /////// // /////// // // // //// // /// /// /// /// // /// /// //// // //// //// /// /// /// /// // ///////// //// /////// //// ///// /// /// /// /// // /// /// //// // // ////////////// /////////// /////////// ////// /// /// // // // // ''' ```
vfc_25531
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1005/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3121\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1000000000000000000000000000000000\n", "output": "33\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4207
Solve the following coding problem using the programming language python: You are given two arrays $a$ and $b$, each contains $n$ integers. You want to create a new array $c$ as follows: choose some real (i.e. not necessarily integer) number $d$, and then for every $i \in [1, n]$ let $c_i := d \cdot a_i + b_i$. Your goal is to maximize the number of zeroes in array $c$. What is the largest possible answer, if you choose $d$ optimally? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) โ€” the number of elements in both arrays. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($-10^9 \le a_i \le 10^9$). The third line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($-10^9 \le b_i \le 10^9$). -----Output----- Print one integer โ€” the maximum number of zeroes in array $c$, if you choose $d$ optimally. -----Examples----- Input 5 1 2 3 4 5 2 4 7 11 3 Output 2 Input 3 13 37 39 1 2 3 Output 2 Input 4 0 0 0 0 1 2 3 4 Output 0 Input 3 1 2 -1 -6 -12 6 Output 3 -----Note----- In the first example, we may choose $d = -2$. In the second example, we may choose $d = -\frac{1}{13}$. In the third example, we cannot obtain any zero in array $c$, no matter which $d$ we choose. In the fourth example, we may choose $d = 6$. 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 input() a = list(map(int, input().split())) b = list(map(int, input().split())) counts = collections.defaultdict(int) arbitrary = 0 for ai, bi in zip(a, b): if ai == 0: if bi == 0: arbitrary += 1 else: if bi == 0: counts[(0, 0)] += 1 else: if (ai < 0 and bi < 0) or (ai >= 0 and bi < 0): ai = -ai bi = -bi g = math.gcd(-bi, ai) counts[(-bi // g, ai // g)] += 1 if counts: print(max(counts.values()) + arbitrary) else: print(arbitrary) ```
vfc_25535
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1133/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n1 2 3 4 5\n2 4 7 11 3\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n13 37 39\n1 2 3\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n0 0 0 0\n1 2 3 4\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4208
Solve the following coding problem using the programming language python: There are $n$ left boots and $n$ right boots. Each boot has a color which is denoted as a lowercase Latin letter or a question mark ('?'). Thus, you are given two strings $l$ and $r$, both of length $n$. The character $l_i$ stands for the color of the $i$-th left boot and the character $r_i$ stands for the color of the $i$-th right boot. A lowercase Latin letter denotes a specific color, but the question mark ('?') denotes an indefinite color. Two specific colors are compatible if they are exactly the same. An indefinite color is compatible with any (specific or indefinite) color. For example, the following pairs of colors are compatible: ('f', 'f'), ('?', 'z'), ('a', '?') and ('?', '?'). The following pairs of colors are not compatible: ('f', 'g') and ('a', 'z'). Compute the maximum number of pairs of boots such that there is one left and one right boot in a pair and their colors are compatible. Print the maximum number of such pairs and the pairs themselves. A boot can be part of at most one pair. -----Input----- The first line contains $n$ ($1 \le n \le 150000$), denoting the number of boots for each leg (i.e. the number of left boots and the number of right boots). The second line contains the string $l$ of length $n$. It contains only lowercase Latin letters or question marks. The $i$-th character stands for the color of the $i$-th left boot. The third line contains the string $r$ of length $n$. It contains only lowercase Latin letters or question marks. The $i$-th character stands for the color of the $i$-th right boot. -----Output----- Print $k$ โ€” the maximum number of compatible left-right pairs of boots, i.e. pairs consisting of one left and one right boot which have compatible colors. The following $k$ lines should contain pairs $a_j, b_j$ ($1 \le a_j, b_j \le n$). The $j$-th of these lines should contain the index $a_j$ of the left boot in the $j$-th pair and index $b_j$ of the right boot in the $j$-th pair. All the numbers $a_j$ should be distinct (unique), all the numbers $b_j$ should be distinct (unique). If there are many optimal answers, print any of them. -----Examples----- Input 10 codeforces dodivthree Output 5 7 8 4 9 2 2 9 10 3 1 Input 7 abaca?b zabbbcc Output 5 6 5 2 3 4 6 7 4 1 2 Input 9 bambarbia hellocode Output 0 Input 10 code?????? ??????test Output 10 6 2 1 6 7 3 3 5 4 8 9 7 5 1 2 4 10 9 8 10 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python ii = lambda: int(input()) mi = lambda: map(int, input().split()) li = lambda: list(mi()) from collections import defaultdict as dd n = ii() a, b = input().strip(), input().strip() da, db = dd(list), dd(list) qa, qb = [], [] for i in range(n): if a[i] == '?': qa.append(i) else: da[a[i]].append(i) if b[i] == '?': qb.append(i) else: db[b[i]].append(i) ans = [] for c in 'abcdefghijklmnopqrstuvwxyz': u, v = da[c], db[c] while u and v: ans.append((u.pop(), v.pop())) while u and qb: ans.append((u.pop(), qb.pop())) while v and qa: ans.append((qa.pop(), v.pop())) while qa and qb: ans.append((qa.pop(), qb.pop())) print(len(ans)) print(*('%d %d' % (i + 1, j + 1) for i, j in ans), sep='\n') ```
vfc_25539
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1141/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "10\ncodeforces\ndodivthree\n", "output": "5\n7 8\n4 9\n2 2\n9 10\n3 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7\nabaca?b\nzabbbcc\n", "output": "5\n6 5\n2 3\n4 6\n7 4\n1 2\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4209
Solve the following coding problem using the programming language python: This problem is given in two editions, which differ exclusively in the constraints on the number $n$. You are given an array of integers $a[1], a[2], \dots, a[n].$ A block is a sequence of contiguous (consecutive) elements $a[l], a[l+1], \dots, a[r]$ ($1 \le l \le r \le n$). Thus, a block is defined by a pair of indices $(l, r)$. Find a set of blocks $(l_1, r_1), (l_2, r_2), \dots, (l_k, r_k)$ such that: They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks $(l_i, r_i)$ and $(l_j, r_j$) where $i \neq j$ either $r_i < l_j$ or $r_j < l_i$. For each block the sum of its elements is the same. Formally, $$a[l_1]+a[l_1+1]+\dots+a[r_1]=a[l_2]+a[l_2+1]+\dots+a[r_2]=$$ $$\dots =$$ $$a[l_k]+a[l_k+1]+\dots+a[r_k].$$ The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks $(l_1', r_1'), (l_2', r_2'), \dots, (l_{k'}', r_{k'}')$ satisfying the above two requirements with $k' > k$. $\left. \begin{array}{|l|l|l|l|l|l|} \hline 4 & {1} & {2} & {2} & {1} & {5} & {3} \\ \hline \end{array} \right.$ The picture corresponds to the first example. Blue boxes illustrate blocks. Write a program to find such a set of blocks. -----Input----- The first line contains integer $n$ ($1 \le n \le 1500$) โ€” the length of the given array. The second line contains the sequence of elements $a[1], a[2], \dots, a[n]$ ($-10^5 \le a_i \le 10^5$). -----Output----- In the first line print the integer $k$ ($1 \le k \le n$). The following $k$ lines should contain blocks, one per line. In each line print a pair of indices $l_i, r_i$ ($1 \le l_i \le r_i \le n$) โ€” the bounds of the $i$-th block. You can print blocks in any order. If there are multiple answers, print any of them. -----Examples----- Input 7 4 1 2 2 1 5 3 Output 3 7 7 2 3 4 5 Input 11 -5 -4 -3 -2 -1 0 1 2 3 4 5 Output 2 3 4 1 1 Input 4 1 1 1 1 Output 4 4 4 1 1 2 2 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 = [int(x) for x in input().split()] res = {} for i in range(n): sm = 0 for j in range(i, n): sm += a[j] if sm in res: res[sm].append((i, j)) else: res[sm] = [(i, j)] best = 0 bestI = -1 for key in res: r = -1 cnt = 0 for (a,b) in sorted(res[key]): if a > r: cnt += 1 r = b elif b < r: r = b if cnt > best: best = cnt bestI = key x = [] r = -1 for (a, b) in sorted(res[bestI]): if a > r: x.append(str(a+1) + " " + str(b+1)) r = b elif b < r: r = b x.pop() x.append(str(a+1) + " " + str(b+1)) print(best) print("\n".join(x)) ```
vfc_25543
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1141/F2", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "7\n4 1 2 2 1 5 3\n", "output": "3\n7 7\n2 3\n4 5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "11\n-5 -4 -3 -2 -1 0 1 2 3 4 5\n", "output": "2\n3 4\n1 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n1 1 1 1\n", "output": "4\n4 4\n1 1\n2 2\n3 3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n100000\n", "output": "1\n1 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n-100000 -100000\n", "output": "2\n2 2\n1 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n10000 99999\n", "output": "1\n1 1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4210
Solve the following coding problem using the programming language python: You are given an array $a$, consisting of $n$ positive integers. Let's call a concatenation of numbers $x$ and $y$ the number that is obtained by writing down numbers $x$ and $y$ one right after another without changing the order. For example, a concatenation of numbers $12$ and $3456$ is a number $123456$. Count the number of ordered pairs of positions $(i, j)$ ($i \neq j$) in array $a$ such that the concatenation of $a_i$ and $a_j$ is divisible by $k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $2 \le k \le 10^9$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer โ€” the number of ordered pairs of positions $(i, j)$ ($i \neq j$) in array $a$ such that the concatenation of $a_i$ and $a_j$ is divisible by $k$. -----Examples----- Input 6 11 45 1 10 12 11 7 Output 7 Input 4 2 2 78 4 10 Output 12 Input 5 2 3 7 19 3 3 Output 0 -----Note----- In the first example pairs $(1, 2)$, $(1, 3)$, $(2, 3)$, $(3, 1)$, $(3, 4)$, $(4, 2)$, $(4, 3)$ suffice. They produce numbers $451$, $4510$, $110$, $1045$, $1012$, $121$, $1210$, respectively, each of them is divisible by $11$. In the second example all $n(n - 1)$ pairs suffice. In the third example no pair is sufficient. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python inp = input().split() n = int(inp[0]) k = int(inp[1]) a = input().split() for i in range(n): a[i] = int(a[i]) # End of Input bank = {} # Preparation for i in range(n): arg = (len(str(a[i])),a[i]%k) #print("Detect:",arg) bank[arg] = bank.get(arg,0)+1 ans = 0 # Query for i in range(n): ten = 1 for j in range(1,11): ten*=10 frontMod = (a[i]*ten)%k #print("FRONT MOD:",frontMod) req = (k-frontMod)%k #print("WANT:",req) got = bank.get((j,req),0) ans += got # Deal with Same Index for i in range(n): cur = str(a[i]) cur = cur*2 tst = int(cur) if(tst%k==0): ans-=1 # Print Answer print(ans) ```
vfc_25547
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1029/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6 11\n45 1 10 12 11 7\n", "output": "7\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 2\n2 78 4 10\n", "output": "12\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 2\n3 7 19 3 3\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "100 2\n38 415 2960 8618 8531 6194 6811 4778 3955 1056 7147 9144 8424 5766 4492 2994 978 1102 2028 3569 8960 6106 9363 7714 5179 7801 4768 9025 6562 4469 5578 5224 6845 362 3229 4769 4191 7895 7490 2200 6215 3458 5037 8201 7914 8300 7398 1620 3428 6878 8069 3748 4808 6460 6711 3492 9121 4011 2210 8480 9092 2689 2005 9802 2168 9196 3366 8098 2706 2293 4952 2099 6742 5529 1424 431 2636 5923 1502 7921 7780 9162 3741 3238 9068 4370 941 625 2131 2575 288 8187 9556 8930 4981 6335 9312 1148 4166 6933\n", "output": "5940\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4232
Solve the following coding problem using the programming language python: You are given a sequence of integers of length $n$ and integer number $k$. You should print any integer number $x$ in the range of $[1; 10^9]$ (i.e. $1 \le x \le 10^9$) such that exactly $k$ elements of given sequence are less than or equal to $x$. Note that the sequence can contain equal elements. If there is no such $x$, print "-1" (without quotes). -----Input----- The first line of the input contains integer numbers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $0 \le k \le n$). The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) โ€” the sequence itself. -----Output----- Print any integer number $x$ from range $[1; 10^9]$ such that exactly $k$ elements of given sequence is less or equal to $x$. If there is no such $x$, print "-1" (without quotes). -----Examples----- Input 7 4 3 7 5 1 10 3 20 Output 6 Input 7 2 3 7 5 1 10 3 20 Output -1 -----Note----- In the first example $5$ is also a valid answer because the elements with indices $[1, 3, 4, 6]$ is less than or equal to $5$ and obviously less than or equal to $6$. In the second example you cannot choose any number that only $2$ elements of the given sequence will be less than or equal to this number because $3$ elements of the given sequence will be also less than or equal to this number. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, k = map(int, input().split()) a = list(sorted(map(int, input().split()))) x = -1 if k == 0: x = max(1, a[0] - 1) else: x = a[k - 1] s = 0 for i in range(n): s += (a[i] <= x) if s == k: print(x) else: print(-1) ```
vfc_25635
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/977/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "7 4\n3 7 5 1 10 3 20\n", "output": "5\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4233
Solve the following coding problem using the programming language python: A star is a figure of the following type: an asterisk character '*' in the center of the figure and four rays (to the left, right, top, bottom) of the same positive length. The size of a star is the length of its rays. The size of a star must be a positive number (i.e. rays of length $0$ are not allowed). Let's consider empty cells are denoted by '.', then the following figures are stars: [Image] The leftmost figure is a star of size $1$, the middle figure is a star of size $2$ and the rightmost figure is a star of size $3$. You are given a rectangular grid of size $n \times m$ consisting only of asterisks '*' and periods (dots) '.'. Rows are numbered from $1$ to $n$, columns are numbered from $1$ to $m$. Your task is to draw this grid using any number of stars or find out that it is impossible. Stars can intersect, overlap or even coincide with each other. The number of stars in the output can't exceed $n \cdot m$. Each star should be completely inside the grid. You can use stars of same and arbitrary sizes. In this problem, you do not need to minimize the number of stars. Just find any way to draw the given grid with at most $n \cdot m$ stars. -----Input----- The first line of the input contains two integers $n$ and $m$ ($3 \le n, m \le 100$) โ€” the sizes of the given grid. The next $n$ lines contains $m$ characters each, the $i$-th line describes the $i$-th row of the grid. It is guaranteed that grid consists of characters '*' and '.' only. -----Output----- If it is impossible to draw the given grid using stars only, print "-1". Otherwise in the first line print one integer $k$ ($0 \le k \le n \cdot m$) โ€” the number of stars needed to draw the given grid. The next $k$ lines should contain three integers each โ€” $x_j$, $y_j$ and $s_j$, where $x_j$ is the row index of the central star character, $y_j$ is the column index of the central star character and $s_j$ is the size of the star. Each star should be completely inside the grid. -----Examples----- Input 6 8 ....*... ...**... ..*****. ...**... ....*... ........ Output 3 3 4 1 3 5 2 3 5 1 Input 5 5 .*... ****. .**** ..**. ..... Output 3 2 2 1 3 3 1 3 4 1 Input 5 5 .*... ***.. .*... .*... ..... Output -1 Input 3 3 *.* .*. *.* Output -1 -----Note----- In the first example the output 2 3 4 1 3 5 2 is also correct. 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()) pole = [] metka = [] for i in range(n): pole.append([]) metka.append([]) s = input() for j in range(m): pole[i].append(s[j]) if s[j] == '.': metka[i].append(0) else: metka[i].append(1) k = 0 ans = [] for i in range(n): for j in range(m): if pole[i][j] == '*': e = 0 while i - e - 1>= 0 and j - e - 1>= 0 and i + e + 1 < n and j + e + 1< m and pole[i - e - 1][j] == '*' and pole[i][j - e - 1] == '*' and pole[i + e + 1][j] == '*' and pole[i][j + e + 1] == '*': e = e + 1 metka[i][j] = 0 metka[i - e][j] = 0 metka[i][j - e] = 0 metka[i + e][j] = 0 metka[i][j + e] = 0 if e != 0: k = k + 1 ans.append((i + 1,j + 1, e)) flag = True for i in range(n): if 1 in metka[i]: flag = False break if not flag: print(-1) else: print(k) for i in range(k): print(ans[i][0], ans[i][1], ans[i][2], end='\n') ```
vfc_25639
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1015/E1", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6 8\n....*...\n...**...\n..*****.\n...**...\n....*...\n........\n", "output": "2\n3 4 1\n3 5 2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 5\n.*...\n****.\n.****\n..**.\n.....\n", "output": "3\n2 2 1\n3 3 1\n3 4 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 5\n.*...\n***..\n.*...\n.*...\n.....\n", "output": "-1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4234
Solve the following coding problem using the programming language python: Let's call (yet again) a string good if its length is even, and every character in odd position of this string is different from the next character (the first character is different from the second, the third is different from the fourth, and so on). For example, the strings good, string and xyyx are good strings, and the strings bad, aa and aabc are not good. Note that the empty string is considered good. You are given a string $s$, you have to delete minimum number of characters from this string so that it becomes good. -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) โ€” the number of characters in $s$. The second line contains the string $s$, consisting of exactly $n$ lowercase Latin letters. -----Output----- In the first line, print one integer $k$ ($0 \le k \le n$) โ€” the minimum number of characters you have to delete from $s$ to make it good. In the second line, print the resulting string $s$. If it is empty, you may leave the second line blank, or not print it at all. -----Examples----- Input 4 good Output 0 good Input 4 aabc Output 2 ab Input 3 aaa Output 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()) s = input() t = '' lst = '1' for i in s: if lst == '1': lst = i nw = '' else: if lst != i: t += lst t += i lst = '1' print (len(s) - len(t)) print (t) ```
vfc_25643
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1165/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\ngood\n", "output": "0\ngood\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\naabc\n", "output": "2\nab\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\naaa\n", "output": "3\n\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\na\n", "output": "1\n\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4235
Solve the following coding problem using the programming language python: You are given a connected undirected graph consisting of $n$ vertices and $m$ edges. There are no self-loops or multiple edges in the given graph. You have to direct its edges in such a way that the obtained directed graph does not contain any paths of length two or greater (where the length of path is denoted as the number of traversed edges). -----Input----- The first line contains two integer numbers $n$ and $m$ ($2 \le n \le 2 \cdot 10^5$, $n - 1 \le m \le 2 \cdot 10^5$) โ€” the number of vertices and edges, respectively. The following $m$ lines contain edges: edge $i$ is given as a pair of vertices $u_i$, $v_i$ ($1 \le u_i, v_i \le n$, $u_i \ne v_i$). There are no multiple edges in the given graph, i. e. for each pair ($u_i, v_i$) there are no other pairs ($u_i, v_i$) and ($v_i, u_i$) in the list of edges. It is also guaranteed that the given graph is connected (there is a path between any pair of vertex in the given graph). -----Output----- If it is impossible to direct edges of the given graph in such a way that the obtained directed graph does not contain paths of length at least two, print "NO" in the first line. Otherwise print "YES" in the first line, and then print any suitable orientation of edges: a binary string (the string consisting only of '0' and '1') of length $m$. The $i$-th element of this string should be '0' if the $i$-th edge of the graph should be directed from $u_i$ to $v_i$, and '1' otherwise. Edges are numbered in the order they are given in the input. -----Example----- Input 6 5 1 5 2 1 1 4 3 1 6 1 Output YES 10100 -----Note----- The picture corresponding to the first example: [Image] And one of possible answers: $\text{of}$ 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()) clr = [-1 for i in range(0, n)] eds = [] def dfs(): cur = 0 st = [-1 for i in range(0, n + 1)] st[cur] = 0 cur += 1 while cur > 0: v = st[cur - 1] cur -= 1 for x in g[v]: if clr[x] != -1: if clr[x] == clr[v]: return False continue clr[x] = clr[v] ^ 1 st[cur] = x cur += 1 return True g = [[] for i in range(0, n)] for i in range(0, m): u, v = map(int, input().split()) u -= 1 v -= 1 g[u].append(v) g[v].append(u) eds.append((u, v)) clr[0] = 0 if dfs(): print("YES") print("".join("1" if clr[u] < clr[v] else "0" for (u, v) in eds)) else: print("NO") ```
vfc_25647
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1144/F", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6 5\n1 5\n2 1\n1 4\n3 1\n6 1\n", "output": "YES\n10100\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 10\n6 8\n9 10\n10 1\n2 10\n10 7\n3 1\n8 1\n2 1\n4 1\n5 10\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 10\n6 3\n4 7\n7 1\n9 8\n8 10\n7 2\n5 2\n4 8\n6 7\n8 7\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 10\n4 3\n6 8\n5 3\n4 1\n2 9\n7 8\n9 6\n10 2\n9 3\n6 3\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 10\n1 3\n9 6\n4 5\n1 9\n8 5\n9 7\n3 2\n5 7\n5 3\n10 5\n", "output": "NO\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4236
Solve the following coding problem using the programming language python: You are given a set of $n$ segments on the axis $Ox$, each segment has integer endpoints between $1$ and $m$ inclusive. Segments may intersect, overlap or even coincide with each other. Each segment is characterized by two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le m$) โ€” coordinates of the left and of the right endpoints. Consider all integer points between $1$ and $m$ inclusive. Your task is to print all such points that don't belong to any segment. The point $x$ belongs to the segment $[l; r]$ if and only if $l \le x \le r$. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n, m \le 100$) โ€” the number of segments and the upper bound for coordinates. The next $n$ lines contain two integers each $l_i$ and $r_i$ ($1 \le l_i \le r_i \le m$) โ€” the endpoints of the $i$-th segment. Segments may intersect, overlap or even coincide with each other. Note, it is possible that $l_i=r_i$, i.e. a segment can degenerate to a point. -----Output----- In the first line print one integer $k$ โ€” the number of points that don't belong to any segment. In the second line print exactly $k$ integers in any order โ€” the points that don't belong to any segment. All points you print should be distinct. If there are no such points at all, print a single integer $0$ in the first line and either leave the second line empty or do not print it at all. -----Examples----- Input 3 5 2 2 1 2 5 5 Output 2 3 4 Input 1 7 1 7 Output 0 -----Note----- In the first example the point $1$ belongs to the second segment, the point $2$ belongs to the first and the second segments and the point $5$ belongs to the third segment. The points $3$ and $4$ do not belong to any segment. In the second example all the points from $1$ to $7$ belong to the first segment. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def mi(): return map(int, input().split()) n,m = mi() a = [0]*m for i in range(n): l,r = mi() for i in range(l-1, r): a[i] = 1 print (a.count(0)) for i in range(m): if a[i]==0: print (i+1, end = ' ') ```
vfc_25651
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1015/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 5\n2 2\n1 2\n5 5\n", "output": "2\n3 4 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 7\n1 7\n", "output": "0\n\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "100 100\n1 2\n1 3\n1 3\n2 3\n1 1\n1 2\n1 1\n1 2\n1 3\n1 2\n1 2\n1 5\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 2\n1 1\n1 1\n1 2\n2 2\n1 1\n1 5\n1 4\n1 1\n2 2\n2 9\n1 1\n1 5\n2 3\n2 3\n1 5\n1 2\n2 2\n2 2\n1 2\n1 2\n3 4\n1 5\n1 1\n1 1\n1 1\n1 1\n2 2\n1 3\n1 2\n1 2\n1 2\n1 1\n2 2\n1 4\n1 3\n1 1\n1 2\n1 1\n2 3\n1 2\n2 2\n1 1\n1 5\n1 2\n2 2\n1 1\n1 1\n1 2\n1 4\n2 3\n1 2\n1 1\n2 2\n1 5\n1 1\n1 6\n1 1\n1 1\n1 2\n1 1\n1 4\n2 2\n1 1\n1 1\n1 2\n1 2\n1 2\n1 1\n1 2\n2 3\n1 1\n1 1\n1 3\n1 3\n1 2\n1 2\n1 1\n1 2\n1 2\n1 1\n1 2\n", "output": "91\n10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 \n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4248
Solve the following coding problem using the programming language python: -----Input----- The first line contains a single integer n (1 โ‰ค n โ‰ค 1000) โ€” the number of points on a plane. Each of the next n lines contains two real coordinates x_{i} and y_{i} of the $i^{\text{th}}$ point, specified with exactly 2 fractional digits. All coordinates are between - 1000 and 1000, inclusive. -----Output----- Output a single real number ฮธ โ€” the answer to the problem statement. The absolute or relative error of your answer should be at most 10^{ - 2}. -----Examples----- Input 8 -2.14 2.06 -1.14 2.04 -2.16 1.46 -2.14 0.70 -1.42 0.40 -0.94 -0.48 -1.42 -1.28 -2.16 -1.62 Output 5.410 Input 5 2.26 1.44 2.28 0.64 2.30 -0.30 1.58 0.66 3.24 0.66 Output 5.620 Input 8 6.98 2.06 6.40 1.12 5.98 0.24 5.54 -0.60 7.16 0.30 7.82 1.24 8.34 0.24 8.74 -0.76 Output 5.480 Input 5 10.44 2.06 10.90 0.80 11.48 -0.48 12.06 0.76 12.54 2.06 Output 6.040 Input 8 16.94 2.42 15.72 2.38 14.82 1.58 14.88 0.50 15.76 -0.16 16.86 -0.20 17.00 0.88 16.40 0.92 Output 6.040 Input 7 20.62 3.00 21.06 2.28 21.56 1.36 21.66 0.56 21.64 -0.52 22.14 2.32 22.62 3.04 Output 6.720 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n=int(input()) print(sum([float(input().split()[1])for i in range(n)])/n+5) ```
vfc_25699
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/409/G", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "8\n-2.14 2.06\n-1.14 2.04\n-2.16 1.46\n-2.14 0.70\n-1.42 0.40\n-0.94 -0.48\n-1.42 -1.28\n-2.16 -1.62\n", "output": "5.410\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n2.26 1.44\n2.28 0.64\n2.30 -0.30\n1.58 0.66\n3.24 0.66\n", "output": "5.620\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "8\n6.98 2.06\n6.40 1.12\n5.98 0.24\n5.54 -0.60\n7.16 0.30\n7.82 1.24\n8.34 0.24\n8.74 -0.76\n", "output": "5.480\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n10.44 2.06\n10.90 0.80\n11.48 -0.48\n12.06 0.76\n12.54 2.06\n", "output": "6.040\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "8\n16.94 2.42\n15.72 2.38\n14.82 1.58\n14.88 0.50\n15.76 -0.16\n16.86 -0.20\n17.00 0.88\n16.40 0.92\n", "output": "6.040\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4249
Solve the following coding problem using the programming language python: The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) โ€” the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, m = [int(x) for x in input().split()] a = [int(x) for x in input().split()] a.sort(reverse=True) def check(d): s=0 for i in range(len(a)): s+=max(0,a[i]-i//d) return s>=m if sum(a)<m: print(-1) else: l, r = 1,n mid = l+r>>1 while l<r: if check(mid): r=mid else: l=mid+1 mid=l+r>>1 print(l) ```
vfc_25703
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1118/D2", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5 8\n2 3 1 1 2\n", "output": "4\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4250
Solve the following coding problem using the programming language python: You are given an array $s$ consisting of $n$ integers. You have to find any array $t$ of length $k$ such that you can cut out maximum number of copies of array $t$ from array $s$. Cutting out the copy of $t$ means that for each element $t_i$ of array $t$ you have to find $t_i$ in $s$ and remove it from $s$. If for some $t_i$ you cannot find such element in $s$, then you cannot cut out one more copy of $t$. The both arrays can contain duplicate elements. For example, if $s = [1, 2, 3, 2, 4, 3, 1]$ and $k = 3$ then one of the possible answers is $t = [1, 2, 3]$. This array $t$ can be cut out $2$ times. To cut out the first copy of $t$ you can use the elements $[1, \underline{\textbf{2}}, 3, 2, 4, \underline{\textbf{3}}, \underline{\textbf{1}}]$ (use the highlighted elements). After cutting out the first copy of $t$ the array $s$ can look like $[1, 3, 2, 4]$. To cut out the second copy of $t$ you can use the elements $[\underline{\textbf{1}}, \underline{\textbf{3}}, \underline{\textbf{2}}, 4]$. After cutting out the second copy of $t$ the array $s$ will be $[4]$. Your task is to find such array $t$ that you can cut out the copy of $t$ from $s$ maximum number of times. If there are multiple answers, you may choose any of them. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) โ€” the number of elements in $s$ and the desired number of elements in $t$, respectively. The second line of the input contains exactly $n$ integers $s_1, s_2, \dots, s_n$ ($1 \le s_i \le 2 \cdot 10^5$). -----Output----- Print $k$ integers โ€” the elements of array $t$ such that you can cut out maximum possible number of copies of this array from $s$. If there are multiple answers, print any of them. The required array $t$ can contain duplicate elements. All the elements of $t$ ($t_1, t_2, \dots, t_k$) should satisfy the following condition: $1 \le t_i \le 2 \cdot 10^5$. -----Examples----- Input 7 3 1 2 3 2 4 3 1 Output 1 2 3 Input 10 4 1 3 1 3 10 3 7 7 12 3 Output 7 3 1 3 Input 15 2 1 2 1 1 1 2 1 1 2 1 2 1 1 1 1 Output 1 1 -----Note----- The first example is described in the problem statement. In the second example the only answer is $[7, 3, 1, 3]$ and any its permutations. It can be shown that you cannot choose any other array such that the maximum number of copies you can cut out would be equal to $2$. In the third example the array $t$ can be cut out $5$ times. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from operator import itemgetter #int(input()) #map(int,input().split()) #[list(map(int,input().split())) for i in range(q)] #print("YES" * ans + "NO" * (1-ans)) n,k = map(int,input().split()) si = list(map(int,input().split())) num = 10**5 * 2 + 1 ai = [0] * num for i in range(n): ai[si[i]] += 1 num3 = num num = max(ai) + 1 ai2 = [[] for i in range(num)] for i in range(num3): if ai[i] != 0: ai2[ai[i]] += [[i,1,ai[i]]] i = num-1 while k > 0: for j in ai2[i]: if k == 0: break num2 = j[2] // (j[1]+1) ai2[num2] += [[j[0],j[1]+1,j[2]]] print(j[0],end=" ") k -= 1 i -= 1 ```
vfc_25707
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1077/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "7 3\n1 2 3 2 4 3 1\n", "output": "1 2 3 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 4\n1 3 1 3 10 3 7 7 12 3\n", "output": "1 3 3 7 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "15 2\n1 2 1 1 1 2 1 1 2 1 2 1 1 1 1\n", "output": "1 1 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 2\n1 1\n", "output": "1 1 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 2\n2 2 2 3 1\n", "output": "1 2 \n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4251
Solve the following coding problem using the programming language python: You are given a matrix $a$, consisting of $n$ rows and $m$ columns. Each cell contains an integer in it. You can change the order of rows arbitrarily (including leaving the initial order), but you can't change the order of cells in a row. After you pick some order of rows, you traverse the whole matrix the following way: firstly visit all cells of the first column from the top row to the bottom one, then the same for the second column and so on. During the traversal you write down the sequence of the numbers on the cells in the same order you visited them. Let that sequence be $s_1, s_2, \dots, s_{nm}$. The traversal is $k$-acceptable if for all $i$ ($1 \le i \le nm - 1$) $|s_i - s_{i + 1}| \ge k$. Find the maximum integer $k$ such that there exists some order of rows of matrix $a$ that it produces a $k$-acceptable traversal. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 16$, $1 \le m \le 10^4$, $2 \le nm$) โ€” the number of rows and the number of columns, respectively. Each of the next $n$ lines contains $m$ integers ($1 \le a_{i, j} \le 10^9$) โ€” the description of the matrix. -----Output----- Print a single integer $k$ โ€” the maximum number such that there exists some order of rows of matrix $a$ that it produces an $k$-acceptable traversal. -----Examples----- Input 4 2 9 9 10 8 5 3 4 3 Output 5 Input 2 4 1 2 3 4 10 3 7 3 Output 0 Input 6 1 3 6 2 5 1 4 Output 3 -----Note----- In the first example you can rearrange rows as following to get the $5$-acceptable traversal: 5 3 10 8 4 3 9 9 Then the sequence $s$ will be $[5, 10, 4, 9, 3, 8, 3, 9]$. Each pair of neighbouring elements have at least $k = 5$ difference between them. In the second example the maximum $k = 0$, any order is $0$-acceptable. In the third example the given order is already $3$-acceptable, you can leave it as it is. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys import copy input = sys.stdin.readline n,m=list(map(int,input().split())) MAT=[list(map(int,input().split())) for i in range(n)] #n=15 #m=10000 #MAT=[list(range(j*j,j*j*(m+1),j*j)) for j in range(1,n+1)] if n==1: ANS=10**10 for i in range(1,m): if ANS>abs(MAT[0][i]-MAT[0][i-1]): ANS=abs(MAT[0][i]-MAT[0][i-1]) print(ANS) return EDGE0=[[10**10]*n for i in range(n)]#iใŒ0่กŒ็›ฎ,jใŒๆœ€็ต‚่กŒ EDGE1=[[10**10]*n for i in range(n)] MAX=0 MIN=0 if m!=1: for i in range(n): for j in range(n): EDGE1[i][j]=EDGE1[j][i]=min([abs(MAT[i][k]-MAT[j][k]) for k in range(m)]) if EDGE1[i][j]>MAX: MAX=EDGE1[i][j] EDGE0[i][j]=min([abs(MAT[i][k]-MAT[j][k-1]) for k in range(1,m)]) else: for i in range(n): for j in range(n): EDGE1[i][j]=EDGE1[j][i]=min([abs(MAT[i][k]-MAT[j][k]) for k in range(m)]) if EDGE1[i][j]>MAX: MAX=EDGE1[i][j] def Hamilton(start,USED,rest,last,weight): #print(start,USED,rest,last,weight,last*(1<<n)+USED) if MEMO[last*(1<<n)+USED]!=2: return MEMO[last*(1<<n)+USED] if rest==1: for i in range(n): if USED & (1<<i)==0: final=i break if EDGE0[start][final]>=weight and EDGE1[last][final]>=weight: #print(start,USED,rest,last,weight) MEMO[last*(1<<n)+USED]=1 return 1 else: #print(start,USED,weight,"!") MEMO[last*(1<<n)+USED]=0 return 0 for j in range(n): if USED & (1<<j)==0 and EDGE1[last][j]>=weight: NEXT=USED+(1<<j) if Hamilton(start,NEXT,rest-1,j,weight)==1: #print(start,USED,rest,last,weight) MEMO[last*(1<<n)+USED]=1 return 1 else: #print(start,USED,weight,"?") MEMO[last*(1<<n)+USED]=0 return 0 while MAX!=MIN: #print(MAX,MIN) aveweight=(MAX+MIN+1)//2 for start in range(n): MEMO=[2]*(n*1<<(n+1)) START=1<<start if Hamilton(start,START,n-1,start,aveweight)==1: MIN=aveweight break else: MAX=aveweight-1 print(MAX) ```
vfc_25711
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1102/F", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4 2\n9 9\n10 8\n5 3\n4 3\n", "output": "5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 4\n1 2 3 4\n10 3 7 3\n", "output": "0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6 1\n3\n6\n2\n5\n1\n4\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 2\n11 21\n", "output": "10\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 2\n85 6\n64 71\n1 83\n", "output": "21\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4252
Solve the following coding problem using the programming language python: You can not just take the file and send it. When Polycarp trying to send a file in the social network "Codehorses", he encountered an unexpected problem. If the name of the file contains three or more "x" (lowercase Latin letters "x") in a row, the system considers that the file content does not correspond to the social network topic. In this case, the file is not sent and an error message is displayed. Determine the minimum number of characters to remove from the file name so after that the name does not contain "xxx" as a substring. Print 0 if the file name does not initially contain a forbidden substring "xxx". You can delete characters in arbitrary positions (not necessarily consecutive). If you delete a character, then the length of a string is reduced by $1$. For example, if you delete the character in the position $2$ from the string "exxxii", then the resulting string is "exxii". -----Input----- The first line contains integer $n$ $(3 \le n \le 100)$ โ€” the length of the file name. The second line contains a string of length $n$ consisting of lowercase Latin letters only โ€” the file name. -----Output----- Print the minimum number of characters to remove from the file name so after that the name does not contain "xxx" as a substring. If initially the file name dost not contain a forbidden substring "xxx", print 0. -----Examples----- Input 6 xxxiii Output 1 Input 5 xxoxx Output 0 Input 10 xxxxxxxxxx Output 8 -----Note----- In the first example Polycarp tried to send a file with name contains number $33$, written in Roman numerals. But he can not just send the file, because it name contains three letters "x" in a row. To send the file he needs to remove any one of this letters. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = int(input()) s = input() res = 0 x_count = 0 for c in s: if c == 'x': x_count += 1 else: x_count = 0 if x_count > 2: res += 1 print(res) ```
vfc_25715
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/978/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6\nxxxiii\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\nxxoxx\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4279
Solve the following coding problem using the programming language python: The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one โ€” from $1$ to $2$, the third one โ€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) โ€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ โ€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ โ€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python l = [0] def count(size): nums = (10**size - 10**(size - 1)) small = l[size-1] + size large = l[size-1] + nums * size if len(l) <= size: l.append(large) return (nums * (small + large))//2 def test(minSize, size, val): out = minSize * val + size * ((val + 1) * val)//2 return out q = int(input()) for _ in range(q): want = int(input()) size = 1 while want > count(size): want -= count(size) size += 1 minSize = l[size - 1] lo = 0 #Impossible hi = (10**size - 10**(size - 1)) #Possible while hi - lo > 1: testV = (lo + hi) // 2 out = test(minSize, size, testV) if out < want: lo = testV else: hi = testV want -= test(minSize, size, lo) newS = 1 while 9 * (10**(newS - 1)) * newS < want: want -= 9 * (10**(newS - 1)) * newS newS += 1 want -= 1 more = want//newS dig = want % newS value = 10**(newS - 1) + more print(str(value)[dig]) ```
vfc_25823
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1216/E1", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n1\n3\n20\n38\n56\n", "output": "1\n2\n5\n2\n0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4280
Solve the following coding problem using the programming language python: Treeland consists of $n$ cities and $n-1$ roads. Each road is bidirectional and connects two distinct cities. From any city you can get to any other city by roads. Yes, you are right โ€” the country's topology is an undirected tree. There are some private road companies in Treeland. The government decided to sell roads to the companies. Each road will belong to one company and a company can own multiple roads. The government is afraid to look unfair. They think that people in a city can consider them unfair if there is one company which owns two or more roads entering the city. The government wants to make such privatization that the number of such cities doesn't exceed $k$ and the number of companies taking part in the privatization is minimal. Choose the number of companies $r$ such that it is possible to assign each road to one company in such a way that the number of cities that have two or more roads of one company is at most $k$. In other words, if for a city all the roads belong to the different companies then the city is good. Your task is to find the minimal $r$ that there is such assignment to companies from $1$ to $r$ that the number of cities which are not good doesn't exceed $k$. [Image] The picture illustrates the first example ($n=6, k=2$). The answer contains $r=2$ companies. Numbers on the edges denote edge indices. Edge colors mean companies: red corresponds to the first company, blue corresponds to the second company. The gray vertex (number $3$) is not good. The number of such vertices (just one) doesn't exceed $k=2$. It is impossible to have at most $k=2$ not good cities in case of one company. -----Input----- The first line contains two integers $n$ and $k$ ($2 \le n \le 200000, 0 \le k \le n - 1$) โ€” the number of cities and the maximal number of cities which can have two or more roads belonging to one company. The following $n-1$ lines contain roads, one road per line. Each line contains a pair of integers $x_i$, $y_i$ ($1 \le x_i, y_i \le n$), where $x_i$, $y_i$ are cities connected with the $i$-th road. -----Output----- In the first line print the required $r$ ($1 \le r \le n - 1$). In the second line print $n-1$ numbers $c_1, c_2, \dots, c_{n-1}$ ($1 \le c_i \le r$), where $c_i$ is the company to own the $i$-th road. If there are multiple answers, print any of them. -----Examples----- Input 6 2 1 4 4 3 3 5 3 6 5 2 Output 2 1 2 1 1 2 Input 4 2 3 1 1 4 1 2 Output 1 1 1 1 Input 10 2 10 3 1 2 1 3 1 4 2 5 2 6 2 7 3 8 3 9 Output 3 1 1 2 3 2 3 1 3 1 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys input = sys.stdin.readline n,k=list(map(int,input().split())) R=[list(map(int,input().split())) for i in range(n-1)] RDICT={tuple(sorted([R[i][0],R[i][1]])):i for i in range(n-1)} C=[[] for i in range(n+1)] for x,y in R: C[x].append(y) C[y].append(x) CLEN=[] for i in range(1,n+1): CLEN.append(len(C[i])) from collections import Counter counter=Counter(CLEN) CV=sorted(list(counter.keys()),reverse=True) cvsum=0 cities=1 for cv in CV: cvsum+=counter[cv] if cvsum>k: cities=cv break print(cities) ANS=[0]*(n-1) from collections import deque QUE = deque() QUE.append([1,1]) VISITED=[0]*(n+1) while QUE: city,roadnum=QUE.pop() VISITED[city]=1 for to in C[city]: if VISITED[to]==0: ANS[RDICT[tuple(sorted([city,to]))]]=roadnum roadnum=roadnum%cities+1 QUE.append([to,roadnum]) print(*ANS) ```
vfc_25827
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1141/G", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6 2\n1 4\n4 3\n3 5\n3 6\n5 2\n", "output": "2\n1 2 1 1 2 ", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 2\n3 1\n1 4\n1 2\n", "output": "1\n1 1 1 ", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 2\n10 3\n1 2\n1 3\n1 4\n2 5\n2 6\n2 7\n3 8\n3 9\n", "output": "3\n1 1 2 3 2 3 1 3 1 ", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 0\n2 1\n", "output": "1\n1 ", "type": "stdin_stdout" } ] }
apps
verifiable_code
4281
Solve the following coding problem using the programming language python: Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year... $n$ friends live in a city which can be represented as a number line. The $i$-th friend lives in a house with an integer coordinate $x_i$. The $i$-th friend can come celebrate the New Year to the house with coordinate $x_i-1$, $x_i+1$ or stay at $x_i$. Each friend is allowed to move no more than once. For all friends $1 \le x_i \le n$ holds, however, they can come to houses with coordinates $0$ and $n+1$ (if their houses are at $1$ or $n$, respectively). For example, let the initial positions be $x = [1, 2, 4, 4]$. The final ones then can be $[1, 3, 3, 4]$, $[0, 2, 3, 3]$, $[2, 2, 5, 5]$, $[2, 1, 3, 5]$ and so on. The number of occupied houses is the number of distinct positions among the final ones. So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) โ€” the number of friends. The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le n$) โ€” the coordinates of the houses of the friends. -----Output----- Print two integers โ€” the minimum and the maximum possible number of occupied houses after all moves are performed. -----Examples----- Input 4 1 2 4 4 Output 2 4 Input 9 1 1 8 8 8 4 4 4 4 Output 3 8 Input 7 4 3 7 1 4 3 3 Output 3 6 -----Note----- In the first example friends can go to $[2, 2, 3, 3]$. So friend $1$ goes to $x_1+1$, friend $2$ stays at his house $x_2$, friend $3$ goes to $x_3-1$ and friend $4$ goes to $x_4-1$. $[1, 1, 3, 3]$, $[2, 2, 3, 3]$ or $[2, 2, 4, 4]$ are also all valid options to obtain $2$ occupied houses. For the maximum number of occupied houses friends can go to $[1, 2, 3, 4]$ or to $[0, 2, 4, 5]$, for example. 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=-2 c=0 for i in a: dif=i-prev if dif > 1: prev=i+1 c+=1 ac=0 lc=-2 for i in a: if lc < i-1: lc=i-1 ac+=1 elif lc == i-1: lc=i ac+=1 elif lc == i: lc=i+1 ac+=1 print(c,ac) ```
vfc_25831
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1283/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n1 2 4 4\n", "output": "2 4\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "9\n1 1 8 8 8 4 4 4 4\n", "output": "3 8\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7\n4 3 7 1 4 3 3\n", "output": "3 6\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n1\n", "output": "1 1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4282
Solve the following coding problem using the programming language python: There are $n$ kids, numbered from $1$ to $n$, dancing in a circle around the Christmas tree. Let's enumerate them in a clockwise direction as $p_1$, $p_2$, ..., $p_n$ (all these numbers are from $1$ to $n$ and are distinct, so $p$ is a permutation). Let the next kid for a kid $p_i$ be kid $p_{i + 1}$ if $i < n$ and $p_1$ otherwise. After the dance, each kid remembered two kids: the next kid (let's call him $x$) and the next kid for $x$. Each kid told you which kids he/she remembered: the kid $i$ remembered kids $a_{i, 1}$ and $a_{i, 2}$. However, the order of $a_{i, 1}$ and $a_{i, 2}$ can differ from their order in the circle. [Image] Example: 5 kids in a circle, $p=[3, 2, 4, 1, 5]$ (or any cyclic shift). The information kids remembered is: $a_{1,1}=3$, $a_{1,2}=5$; $a_{2,1}=1$, $a_{2,2}=4$; $a_{3,1}=2$, $a_{3,2}=4$; $a_{4,1}=1$, $a_{4,2}=5$; $a_{5,1}=2$, $a_{5,2}=3$. You have to restore the order of the kids in the circle using this information. If there are several answers, you may print any. It is guaranteed that at least one solution exists. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $n$ ($3 \le n \le 2 \cdot 10^5$) โ€” the number of the kids. The next $n$ lines contain $2$ integers each. The $i$-th line contains two integers $a_{i, 1}$ and $a_{i, 2}$ ($1 \le a_{i, 1}, a_{i, 2} \le n, a_{i, 1} \ne a_{i, 2}$) โ€” the kids the $i$-th kid remembered, given in arbitrary order. -----Output----- Print $n$ integers $p_1$, $p_2$, ..., $p_n$ โ€” permutation of integers from $1$ to $n$, which corresponds to the order of kids in the circle. If there are several answers, you may print any (for example, it doesn't matter which kid is the first in the circle). It is guaranteed that at least one solution exists. -----Examples----- Input 5 3 5 1 4 2 4 1 5 2 3 Output 3 2 4 1 5 Input 3 2 3 3 1 1 2 Output 3 1 2 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = int(input()) A = [-1] ans = [1] for i in range(n): A.append(tuple(map(int, input().split()))) a = A[1][0] b = A[1][1] c = A[a][0] d = A[a][1] if b == c or b == d: ans.append(a) ans.append(b) else: ans.append(b) ans.append(a) while len(ans) != n: p = A[ans[-2]] if p[0] == ans[-1]: ans.append(p[1]) else: ans.append(p[0]) for i in ans: print(i, end=' ') ```
vfc_25835
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1095/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n3 5\n1 4\n2 4\n1 5\n2 3\n", "output": "3 2 4 1 5 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n2 3\n3 1\n1 2\n", "output": "3 1 2 \n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4283
Solve the following coding problem using the programming language python: You are a coach at your local university. There are $n$ students under your supervision, the programming skill of the $i$-th student is $a_i$. You have to create a team for a new programming competition. As you know, the more students some team has the more probable its victory is! So you have to create a team with the maximum number of students. But you also know that a team should be balanced. It means that the programming skill of each pair of students in a created team should differ by no more than $5$. Your task is to report the maximum possible number of students in a balanced team. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) โ€” the number of students. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is a programming skill of the $i$-th student. -----Output----- Print one integer โ€” the maximum possible number of students in a balanced team. -----Examples----- Input 6 1 10 17 12 15 2 Output 3 Input 10 1337 1337 1337 1337 1337 1337 1337 1337 1337 1337 Output 10 Input 6 1 1000 10000 10 100 1000000000 Output 1 -----Note----- In the first example you can create a team with skills $[12, 17, 15]$. In the second example you can take all students in a team because their programming skills are equal. In the third example you can create a team consisting of a single student (and you cannot create a team consisting of at least two students). 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() i = 0 j = 0 ans = 0 while j < len(A): ans = max(ans, j - i) if i == j or A[j] - A[i] <= 5: j += 1 else: i += 1 print(max(ans, j - i)) ```
vfc_25839
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1133/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6\n1 10 17 12 15 2\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n1337 1337 1337 1337 1337 1337 1337 1337 1337 1337\n", "output": "10\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4284
Solve the following coding problem using the programming language python: Vova is playing a computer game. There are in total $n$ turns in the game and Vova really wants to play all of them. The initial charge of his laptop battery (i.e. the charge before the start of the game) is $k$. During each turn Vova can choose what to do: If the current charge of his laptop battery is strictly greater than $a$, Vova can just play, and then the charge of his laptop battery will decrease by $a$; if the current charge of his laptop battery is strictly greater than $b$ ($b<a$), Vova can play and charge his laptop, and then the charge of his laptop battery will decrease by $b$; if the current charge of his laptop battery is less than or equal to $a$ and $b$ at the same time then Vova cannot do anything and loses the game. Regardless of Vova's turns the charge of the laptop battery is always decreases. Vova wants to complete the game (Vova can complete the game if after each of $n$ turns the charge of the laptop battery is strictly greater than $0$). Vova has to play exactly $n$ turns. Among all possible ways to complete the game, Vova wants to choose the one where the number of turns when he just plays (first type turn) is the maximum possible. It is possible that Vova cannot complete the game at all. Your task is to find out the maximum possible number of turns Vova can just play (make the first type turn) or report that Vova cannot complete the game. You have to answer $q$ independent queries. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 10^5$) โ€” the number of queries. Each query is presented by a single line. The only line of the query contains four integers $k, n, a$ and $b$ ($1 \le k, n \le 10^9, 1 \le b < a \le 10^9$) โ€” the initial charge of Vova's laptop battery, the number of turns in the game and values $a$ and $b$, correspondingly. -----Output----- For each query print one integer: -1 if Vova cannot complete the game or the maximum number of turns Vova can just play (make the first type turn) otherwise. -----Example----- Input 6 15 5 3 2 15 5 4 3 15 5 2 1 15 5 5 1 16 7 5 2 20 5 7 3 Output 4 -1 5 2 0 1 -----Note----- In the first example query Vova can just play $4$ turns and spend $12$ units of charge and then one turn play and charge and spend $2$ more units. So the remaining charge of the battery will be $1$. In the second example query Vova cannot complete the game because even if he will play and charge the battery during each turn then the charge of the laptop battery will be $0$ after the last turn. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python q = int(input()) for query in range(q): k, n, a, b = list(map(int,input().split())) if n * b > k: print(-1) else: print(min(n, (k-n*b-1)//(a-b))) ```
vfc_25843
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1183/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6\n15 5 3 2\n15 5 4 3\n15 5 2 1\n15 5 5 1\n16 7 5 2\n20 5 7 3\n", "output": "4\n-1\n5\n2\n0\n1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n1000000000 499999999 3 2\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n1000000000 499999999 2 1\n1000000000 499999999 2 1\n1000000000 499999999 2 1\n1000000000 499999999 2 1\n1000000000 499999999 2 1\n1000000000 499999999 2 1\n1000000000 499999999 2 1\n1000000000 499999999 2 1\n1000000000 499999999 2 1\n1000000000 499999999 2 1\n", "output": "499999999\n499999999\n499999999\n499999999\n499999999\n499999999\n499999999\n499999999\n499999999\n499999999\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n1000000000 499999999 3 2\n1000000000 499999999 3 2\n1000000000 499999999 3 2\n1000000000 499999999 3 2\n1000000000 499999999 3 2\n1000000000 499999999 3 2\n1000000000 499999999 3 2\n1000000000 499999999 3 2\n1000000000 499999999 3 2\n1000000000 499999999 3 2\n", "output": "1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n1000000000 999999999 2 1\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4285
Solve the following coding problem using the programming language python: You are given a string $s$ consisting of lowercase Latin letters "a", "b" and "c" and question marks "?". Let the number of question marks in the string $s$ be $k$. Let's replace each question mark with one of the letters "a", "b" and "c". Here we can obtain all $3^{k}$ possible strings consisting only of letters "a", "b" and "c". For example, if $s = $"ac?b?c" then we can obtain the following strings: $[$"acabac", "acabbc", "acabcc", "acbbac", "acbbbc", "acbbcc", "accbac", "accbbc", "accbcc"$]$. Your task is to count the total number of subsequences "abc" in all resulting strings. Since the answer can be very large, print it modulo $10^{9} + 7$. A subsequence of the string $t$ is such a sequence that can be derived from the string $t$ after removing some (possibly, zero) number of letters without changing the order of remaining letters. For example, the string "baacbc" contains two subsequences "abc" โ€” a subsequence consisting of letters at positions $(2, 5, 6)$ and a subsequence consisting of letters at positions $(3, 5, 6)$. -----Input----- The first line of the input contains one integer $n$ $(3 \le n \le 200\,000)$ โ€” the length of $s$. The second line of the input contains the string $s$ of length $n$ consisting of lowercase Latin letters "a", "b" and "c" and question marks"?". -----Output----- Print the total number of subsequences "abc" in all strings you can obtain if you replace all question marks with letters "a", "b" and "c", modulo $10^{9} + 7$. -----Examples----- Input 6 ac?b?c Output 24 Input 7 ??????? Output 2835 Input 9 cccbbbaaa Output 0 Input 5 a???c Output 46 -----Note----- In the first example, we can obtain $9$ strings: "acabac" โ€” there are $2$ subsequences "abc", "acabbc" โ€” there are $4$ subsequences "abc", "acabcc" โ€” there are $4$ subsequences "abc", "acbbac" โ€” there are $2$ subsequences "abc", "acbbbc" โ€” there are $3$ subsequences "abc", "acbbcc" โ€” there are $4$ subsequences "abc", "accbac" โ€” there is $1$ subsequence "abc", "accbbc" โ€” there are $2$ subsequences "abc", "accbcc" โ€” there are $2$ subsequences "abc". So, there are $2 + 4 + 4 + 2 + 3 + 4 + 1 + 2 + 2 = 24$ subsequences "abc" in total. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def num(s): ans1 = [0]*n for q in range(n): ans1[q] = s[q] == 'a' sum1 = 0 for q in range(n): w = sum1*(s[q] == 'b') sum1 += ans1[q] ans1[q] = w sum1 = 0 for q in range(n): w = sum1*(s[q] == 'c') sum1 += ans1[q] ans1[q] = w sum1 = 0 for q in range(n): sum1 += ans1[q] return sum1 % C n = int(input()) s = list(input()) C, k = 10**9+7, 0 ans, ans1, deg = [0]*n, [0]*n, [1] for q in range(n): deg.append(deg[-1]*3 % C) k += s[q] == '?' if k == 0: print(num(s)) elif k == 1: ans = 0 for q in range(n): if s[q] == '?': for q1 in ['a', 'b', 'c']: s[q] = q1 ans += num(s) break print(ans % C) elif k == 2: ans = 0 ind1 = ind2 = -1 for q in range(n): if s[q] == '?' and ind1 == -1: ind1 = q elif s[q] == '?': ind2 = q for q in ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']: s[ind1], s[ind2] = q[0], q[1] ans += num(s) print(ans % C) else: ans = 0 for q1 in ['???', '??c', '?b?', '?bc', 'a??', 'a?c', 'ab?', 'abc']: t = q1.count('?') ans1 = [0] * n for q in range(n): ans1[q] = (s[q] == q1[0])*deg[k-t] sum1 = 0 for q in range(n): w = sum1 * (s[q] == q1[1]) sum1 += ans1[q] ans1[q] = w % C sum1 = 0 for q in range(n): w = sum1 * (s[q] == q1[2]) sum1 += ans1[q] ans1[q] = w % C sum1 = 0 for q in range(n): sum1 += ans1[q] ans += sum1 print(ans % C) ```
vfc_25847
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1426/F", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6\nac?b?c\n", "output": "24\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7\n???????\n", "output": "2835\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4286
Solve the following coding problem using the programming language python: You are given an undirected graph consisting of $n$ vertices. A number is written on each vertex; the number on vertex $i$ is $a_i$. Initially there are no edges in the graph. You may add some edges to this graph, but you have to pay for them. The cost of adding an edge between vertices $x$ and $y$ is $a_x + a_y$ coins. There are also $m$ special offers, each of them is denoted by three numbers $x$, $y$ and $w$, and means that you can add an edge connecting vertices $x$ and $y$ and pay $w$ coins for it. You don't have to use special offers: if there is a pair of vertices $x$ and $y$ that has a special offer associated with it, you still may connect these two vertices paying $a_x + a_y$ coins for it. What is the minimum number of coins you have to spend to make the graph connected? Recall that a graph is connected if it's possible to get from any vertex to any other vertex using only the edges belonging to this graph. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $0 \le m \le 2 \cdot 10^5$) โ€” the number of vertices in the graph and the number of special offers, respectively. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^{12}$) โ€” the numbers written on the vertices. Then $m$ lines follow, each containing three integers $x$, $y$ and $w$ ($1 \le x, y \le n$, $1 \le w \le 10^{12}$, $x \ne y$) denoting a special offer: you may add an edge connecting vertex $x$ and vertex $y$, and this edge will cost $w$ coins. -----Output----- Print one integer โ€” the minimum number of coins you have to pay to make the graph connected. -----Examples----- Input 3 2 1 3 3 2 3 5 2 1 1 Output 5 Input 4 0 1 3 3 7 Output 16 Input 5 4 1 2 3 4 5 1 2 8 1 3 10 1 4 7 1 5 15 Output 18 -----Note----- In the first example it is possible to connect $1$ to $2$ using special offer $2$, and then $1$ to $3$ without using any offers. In next two examples the optimal answer may be achieved without using special offers. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys input = sys.stdin.readline n,m=list(map(int,input().split())) A=list(map(int,input().split())) SP=[list(map(int,input().split())) for i in range(m)] MIN=min(A) x=A.index(MIN) EDGE_x=[[x+1,i+1,A[x]+A[i]] for i in range(n) if x!=i] EDGE=EDGE_x+SP EDGE.sort(key=lambda x:x[2]) #UnionFind Group=[i for i in range(n+1)] def find(x): while Group[x] != x: x=Group[x] return x def Union(x,y): if find(x) != find(y): Group[find(y)]=Group[find(x)]=min(find(y),find(x)) ANS=0 for i,j,x in EDGE: if find(i)!=find(j): ANS+=x Union(i,j) print(ANS) ```
vfc_25851
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1095/F", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 2\n1 3 3\n2 3 5\n2 1 1\n", "output": "5\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 0\n1 3 3 7\n", "output": "16\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4287
Solve the following coding problem using the programming language python: Polycarp lives on a coordinate line at the point $x = 0$. He goes to his friend that lives at the point $x = a$. Polycarp can move only from left to right, he can pass one unit of length each second. Now it's raining, so some segments of his way are in the rain. Formally, it's raining on $n$ non-intersecting segments, the $i$-th segment which is in the rain is represented as $[l_i, r_i]$ ($0 \le l_i < r_i \le a$). There are $m$ umbrellas lying on the line, the $i$-th umbrella is located at point $x_i$ ($0 \le x_i \le a$) and has weight $p_i$. When Polycarp begins his journey, he doesn't have any umbrellas. During his journey from $x = 0$ to $x = a$ Polycarp can pick up and throw away umbrellas. Polycarp picks up and throws down any umbrella instantly. He can carry any number of umbrellas at any moment of time. Because Polycarp doesn't want to get wet, he must carry at least one umbrella while he moves from $x$ to $x + 1$ if a segment $[x, x + 1]$ is in the rain (i.e. if there exists some $i$ such that $l_i \le x$ and $x + 1 \le r_i$). The condition above is the only requirement. For example, it is possible to go without any umbrellas to a point where some rain segment starts, pick up an umbrella at this point and move along with an umbrella. Polycarp can swap umbrellas while he is in the rain. Each unit of length passed increases Polycarp's fatigue by the sum of the weights of umbrellas he carries while moving. Can Polycarp make his way from point $x = 0$ to point $x = a$? If yes, find the minimum total fatigue after reaching $x = a$, if Polycarp picks up and throws away umbrellas optimally. -----Input----- The first line contains three integers $a$, $n$ and $m$ ($1 \le a, m \le 2000, 1 \le n \le \lceil\frac{a}{2}\rceil$) โ€” the point at which Polycarp's friend lives, the number of the segments in the rain and the number of umbrellas. Each of the next $n$ lines contains two integers $l_i$ and $r_i$ ($0 \le l_i < r_i \le a$) โ€” the borders of the $i$-th segment under rain. It is guaranteed that there is no pair of intersecting segments. In other words, for each pair of segments $i$ and $j$ either $r_i < l_j$ or $r_j < l_i$. Each of the next $m$ lines contains two integers $x_i$ and $p_i$ ($0 \le x_i \le a$, $1 \le p_i \le 10^5$) โ€” the location and the weight of the $i$-th umbrella. -----Output----- Print "-1" (without quotes) if Polycarp can't make his way from point $x = 0$ to point $x = a$. Otherwise print one integer โ€” the minimum total fatigue after reaching $x = a$, if Polycarp picks up and throws away umbrellas optimally. -----Examples----- Input 10 2 4 3 7 8 10 0 10 3 4 8 1 1 2 Output 14 Input 10 1 1 0 9 0 5 Output 45 Input 10 1 1 0 9 1 5 Output -1 -----Note----- In the first example the only possible strategy is to take the fourth umbrella at the point $x = 1$, keep it till the point $x = 7$ (the total fatigue at $x = 7$ will be equal to $12$), throw it away, move on from $x = 7$ to $x = 8$ without an umbrella, take the third umbrella at $x = 8$ and keep it till the end (the total fatigue at $x = 10$ will be equal to $14$). In the second example the only possible strategy is to take the first umbrella, move with it till the point $x = 9$, throw it away and proceed without an umbrella till the end. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys a,m,n=list(map(int,input().split())) aux=[0]*(a+1) inf=10**15 dp=[aux.copy() for i in range(n+1)] m1=10**12 m2=10**12 for i in range(m): l,r=list(map(int,input().split())) if l<m1: m1=l for j in range(l,r): dp[0][j+1]=inf s=[] for i in range(1,n+1): x,w=list(map(int,input().split())) s.append(tuple([x,w])) if x<m2: m2=x if m2>m1: print(-1) return s.sort() for i in range(1,n+1): x=s[i-1][0] w=s[i-1][1] for j in range(x+1): dp[i][j]=dp[i-1][j] for j in range(x+1,a+1): if i!=1: dp[i][j]=min(dp[0][j]+dp[i][j-1],dp[i-1][j],w*(j-x)+dp[i][x]) else: dp[i][j]=min(dp[0][j]+dp[i][j-1],w*(j-x)+dp[i][x]) ans=dp[-1][-1] if ans>=inf: print(-1) else: print(ans) ```
vfc_25855
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/988/F", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "10 2 4\n3 7\n8 10\n0 10\n3 4\n8 1\n1 2\n", "output": "14\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 1 1\n0 9\n0 5\n", "output": "45\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 1 1\n0 9\n1 5\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1 1\n0 1\n1 100000\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1 1\n0 1\n0 100000\n", "output": "100000\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4319
Solve the following coding problem using the programming language python: Little girl Tanya climbs the stairs inside a multi-storey building. Every time Tanya climbs a stairway, she starts counting steps from $1$ to the number of steps in this stairway. She speaks every number aloud. For example, if she climbs two stairways, the first of which contains $3$ steps, and the second contains $4$ steps, she will pronounce the numbers $1, 2, 3, 1, 2, 3, 4$. You are given all the numbers pronounced by Tanya. How many stairways did she climb? Also, output the number of steps in each stairway. The given sequence will be a valid sequence that Tanya could have pronounced when climbing one or more stairways. -----Input----- The first line contains $n$ ($1 \le n \le 1000$) โ€” the total number of numbers pronounced by Tanya. The second line contains integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 1000$) โ€” all the numbers Tanya pronounced while climbing the stairs, in order from the first to the last pronounced number. Passing a stairway with $x$ steps, she will pronounce the numbers $1, 2, \dots, x$ in that order. The given sequence will be a valid sequence that Tanya could have pronounced when climbing one or more stairways. -----Output----- In the first line, output $t$ โ€” the number of stairways that Tanya climbed. In the second line, output $t$ numbers โ€” the number of steps in each stairway she climbed. Write the numbers in the correct order of passage of the stairways. -----Examples----- Input 7 1 2 3 1 2 3 4 Output 2 3 4 Input 4 1 1 1 1 Output 4 1 1 1 1 Input 5 1 2 3 4 5 Output 1 5 Input 5 1 2 1 2 1 Output 3 2 2 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())) o = [] cur = 0 for x in a: if x == 1: if cur > 0: o.append(cur) cur = 1 else: cur += 1 o.append(cur) print(len(o)) print(' '.join(str(x) for x in o)) ```
vfc_25983
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1005/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "7\n1 2 3 1 2 3 4\n", "output": "2\n3 4 ", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n1 1 1 1\n", "output": "4\n1 1 1 1 ", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n1 2 3 4 5\n", "output": "1\n5 ", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n1 2 1 2 1\n", "output": "3\n2 2 1 ", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n1\n", "output": "1\n1 ", "type": "stdin_stdout" } ] }
apps
verifiable_code
4320
Solve the following coding problem using the programming language python: Recently Vova found $n$ candy wrappers. He remembers that he bought $x$ candies during the first day, $2x$ candies during the second day, $4x$ candies during the third day, $\dots$, $2^{k-1} x$ candies during the $k$-th day. But there is an issue: Vova remembers neither $x$ nor $k$ but he is sure that $x$ and $k$ are positive integers and $k > 1$. Vova will be satisfied if you tell him any positive integer $x$ so there is an integer $k>1$ that $x + 2x + 4x + \dots + 2^{k-1} x = n$. It is guaranteed that at least one solution exists. Note that $k > 1$. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) โ€” the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($3 \le n \le 10^9$) โ€” the number of candy wrappers Vova found. It is guaranteed that there is some positive integer $x$ and integer $k>1$ that $x + 2x + 4x + \dots + 2^{k-1} x = n$. -----Output----- Print one integer โ€” any positive integer value of $x$ so there is an integer $k>1$ that $x + 2x + 4x + \dots + 2^{k-1} x = n$. -----Example----- Input 7 3 6 7 21 28 999999999 999999984 Output 1 2 1 7 4 333333333 333333328 -----Note----- In the first test case of the example, one of the possible answers is $x=1, k=2$. Then $1 \cdot 1 + 2 \cdot 1$ equals $n=3$. In the second test case of the example, one of the possible answers is $x=2, k=2$. Then $1 \cdot 2 + 2 \cdot 2$ equals $n=6$. In the third test case of the example, one of the possible answers is $x=1, k=3$. Then $1 \cdot 1 + 2 \cdot 1 + 4 \cdot 1$ equals $n=7$. In the fourth test case of the example, one of the possible answers is $x=7, k=2$. Then $1 \cdot 7 + 2 \cdot 7$ equals $n=21$. In the fifth test case of the example, one of the possible answers is $x=4, k=3$. Then $1 \cdot 4 + 2 \cdot 4 + 4 \cdot 4$ equals $n=28$. 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 * test = int(input()) for test_case in range(test): n = int(input()) ct = 3 p = 2 while(1): if(n%ct == 0): print(n//ct) break p *= 2 ct += p ```
vfc_25987
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1343/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "7\n3\n6\n7\n21\n28\n999999999\n999999984\n", "output": "1\n2\n1\n7\n4\n333333333\n333333328\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n6\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n109123\n", "output": "15589\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n9823263\n", "output": "3274421\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n7\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n36996333\n", "output": "12332111\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4321
Solve the following coding problem using the programming language python: Little girl Tanya is learning how to decrease a number by one, but she does it wrong with a number consisting of two or more digits. Tanya subtracts one from a number by the following algorithm: if the last digit of the number is non-zero, she decreases the number by one; if the last digit of the number is zero, she divides the number by 10 (i.e. removes the last digit). You are given an integer number $n$. Tanya will subtract one from it $k$ times. Your task is to print the result after all $k$ subtractions. It is guaranteed that the result will be positive integer number. -----Input----- The first line of the input contains two integer numbers $n$ and $k$ ($2 \le n \le 10^9$, $1 \le k \le 50$) โ€” the number from which Tanya will subtract and the number of subtractions correspondingly. -----Output----- Print one integer number โ€” the result of the decreasing $n$ by one $k$ times. It is guaranteed that the result will be positive integer number. -----Examples----- Input 512 4 Output 50 Input 1000000000 9 Output 1 -----Note----- The first example corresponds to the following sequence: $512 \rightarrow 511 \rightarrow 510 \rightarrow 51 \rightarrow 50$. 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())) for i in range(k): if n%10==0: n //= 10 else: n -= 1 print(n) ```
vfc_25991
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/977/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "512 4\n", "output": "50\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1000000000 9\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "131203 11\n", "output": "12\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "999999999 50\n", "output": "9999\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "999999999 49\n", "output": "99990\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "131203 9\n", "output": "130\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4322
Solve the following coding problem using the programming language python: There are $n$ people in a row. The height of the $i$-th person is $a_i$. You can choose any subset of these people and try to arrange them into a balanced circle. A balanced circle is such an order of people that the difference between heights of any adjacent people is no more than $1$. For example, let heights of chosen people be $[a_{i_1}, a_{i_2}, \dots, a_{i_k}]$, where $k$ is the number of people you choose. Then the condition $|a_{i_j} - a_{i_{j + 1}}| \le 1$ should be satisfied for all $j$ from $1$ to $k-1$ and the condition $|a_{i_1} - a_{i_k}| \le 1$ should be also satisfied. $|x|$ means the absolute value of $x$. It is obvious that the circle consisting of one person is balanced. Your task is to choose the maximum number of people and construct a balanced circle consisting of all chosen people. It is obvious that the circle consisting of one person is balanced so the answer always exists. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) โ€” the number of people. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the height of the $i$-th person. -----Output----- In the first line of the output print $k$ โ€” the number of people in the maximum balanced circle. In the second line print $k$ integers $res_1, res_2, \dots, res_k$, where $res_j$ is the height of the $j$-th person in the maximum balanced circle. The condition $|res_{j} - res_{j + 1}| \le 1$ should be satisfied for all $j$ from $1$ to $k-1$ and the condition $|res_{1} - res_{k}| \le 1$ should be also satisfied. -----Examples----- Input 7 4 3 5 1 2 2 1 Output 5 2 1 1 2 3 Input 5 3 7 5 1 5 Output 2 5 5 Input 3 5 1 4 Output 2 4 5 Input 7 2 2 3 2 1 2 2 Output 7 1 2 2 2 2 3 2 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # AC import sys class Main: def __init__(self): self.buff = None self.index = 0 def __next__(self): if self.buff is None or self.index == len(self.buff): self.buff = self.next_line() self.index = 0 val = self.buff[self.index] self.index += 1 return val def next_line(self, _map=str): return list(map(_map, sys.stdin.readline().split())) def next_int(self): return int(next(self)) def solve(self): n = self.next_int() x = sorted([self.next_int() for _ in range(0, n)]) ml = -1 _i = 0 _j = 0 j = 0 for i in range(0, n): j = max(j, i) while j + 1 < n and x[j + 1] == x[i]: j += 1 while j + 2 < n and x[j + 2] == x[j] + 1: j += 2 while j + 1 < n and x[j + 1] == x[j]: j += 1 jj = j if j + 1 < n and x[j + 1] == x[j] + 1: jj += 1 if jj - i > ml: ml = jj - i _i = i _j = jj a = [] b = [] i = _i while i <= _j: a.append(x[i]) i += 1 while i <= _j and x[i] == a[-1]: b.append(x[i]) i += 1 print(ml + 1) print(' '.join([str(x) for x in (a + b[::-1])])) def __starting_point(): Main().solve() __starting_point() ```
vfc_25995
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1157/F", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "7\n4 3 5 1 2 2 1\n", "output": "5\n1 1 2 3 2 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n3 7 5 1 5\n", "output": "2\n5 5 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n5 1 4\n", "output": "2\n4 5 \n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4323
Solve the following coding problem using the programming language python: Ivan has $n$ songs on his phone. The size of the $i$-th song is $a_i$ bytes. Ivan also has a flash drive which can hold at most $m$ bytes in total. Initially, his flash drive is empty. Ivan wants to copy all $n$ songs to the flash drive. He can compress the songs. If he compresses the $i$-th song, the size of the $i$-th song reduces from $a_i$ to $b_i$ bytes ($b_i < a_i$). Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most $m$. He can compress any subset of the songs (not necessarily contiguous). Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to $m$). If it is impossible to copy all the songs (even if Ivan compresses all the songs), print "-1". Otherwise print the minimum number of songs Ivan needs to compress. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 10^5, 1 \le m \le 10^9$) โ€” the number of the songs on Ivan's phone and the capacity of Ivan's flash drive. The next $n$ lines contain two integers each: the $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^9$, $a_i > b_i$) โ€” the initial size of the $i$-th song and the size of the $i$-th song after compression. -----Output----- If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print "-1". Otherwise print the minimum number of the songs to compress. -----Examples----- Input 4 21 10 8 7 4 3 1 5 4 Output 2 Input 4 16 10 8 7 4 3 1 5 4 Output -1 -----Note----- In the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to $8 + 7 + 1 + 5 = 21 \le 21$. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal $8 + 4 + 3 + 5 = 20 \le 21$. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to $10 + 4 + 3 + 5 = 22 > 21$). In the second example even if Ivan compresses all the songs the sum of sizes will be equal $8 + 4 + 1 + 4 = 17 > 16$. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # import collections, atexit, math, sys from functools import cmp_to_key #key=cmp_to_key(lambda x,y: 1 if x not in y else -1 ) sys.setrecursionlimit(1000000) def getIntList(): return list(map(int, input().split())) import bisect try : #raise ModuleNotFoundError import numpy def dprint(*args, **kwargs): print(*args, **kwargs, file=sys.stderr) dprint('debug mode') except ModuleNotFoundError: def dprint(*args, **kwargs): pass def memo(func): cache={} def wrap(*args): if args not in cache: cache[args]=func(*args) return cache[args] return wrap @memo def comb (n,k): if k==0: return 1 if n==k: return 1 return comb(n-1,k-1) + comb(n-1,k) inId = 0 outId = 0 if inId>0: dprint('use input', inId) sys.stdin = open('input'+ str(inId) + '.txt', 'r') #ๆ ‡ๅ‡†่พ“ๅ‡บ้‡ๅฎšๅ‘่‡ณๆ–‡ไปถ if outId>0: dprint('use output', outId) sys.stdout = open('stdout'+ str(outId) + '.txt', 'w') #ๆ ‡ๅ‡†่พ“ๅ‡บ้‡ๅฎšๅ‘่‡ณๆ–‡ไปถ atexit.register(lambda :sys.stdout.close()) #idle ไธญไธไผšๆ‰ง่กŒ atexit N, M = getIntList() total = 0 zz = [0 for i in range(N)] for i in range(N): a,b = getIntList() total+=a zz[i] = a-b zz.sort(reverse = True) if total <= M: print(0) return for i in range(N): total -= zz[i] if total <= M: print(i+1) return print(-1) ```
vfc_25999
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1015/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4 21\n10 8\n7 4\n3 1\n5 4\n", "output": "2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 16\n10 8\n7 4\n3 1\n5 4\n", "output": "-1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4324
Solve the following coding problem using the programming language python: You are given three positive integers $n$, $a$ and $b$. You have to construct a string $s$ of length $n$ consisting of lowercase Latin letters such that each substring of length $a$ has exactly $b$ distinct letters. It is guaranteed that the answer exists. You have to answer $t$ independent test cases. Recall that the substring $s[l \dots r]$ is the string $s_l, s_{l+1}, \dots, s_{r}$ and its length is $r - l + 1$. In this problem you are only interested in substrings of length $a$. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2000$) โ€” the number of test cases. Then $t$ test cases follow. The only line of a test case contains three space-separated integers $n$, $a$ and $b$ ($1 \le a \le n \le 2000, 1 \le b \le \min(26, a)$), where $n$ is the length of the required string, $a$ is the length of a substring and $b$ is the required number of distinct letters in each substring of length $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ ($\sum n \le 2000$). -----Output----- For each test case, print the answer โ€” such a string $s$ of length $n$ consisting of lowercase Latin letters that each substring of length $a$ has exactly $b$ distinct letters. If there are multiple valid answers, print any of them. It is guaranteed that the answer exists. -----Example----- Input 4 7 5 3 6 1 1 6 6 1 5 2 2 Output tleelte qwerty vvvvvv abcde -----Note----- In the first test case of the example, consider all the substrings of length $5$: "tleel": it contains $3$ distinct (unique) letters, "leelt": it contains $3$ distinct (unique) letters, "eelte": it contains $3$ distinct (unique) letters. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python TC = int(input()) while TC > 0: n, a, b = list(map(int, input().split())) ans = "" for i in range(b): ans += chr(ord('a') + i) ans = ans * n print(ans[:n]) TC -= 1 ```
vfc_26003
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1335/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n7 5 3\n6 1 1\n6 6 1\n5 2 2\n", "output": "abcabca\naaaaaa\naaaaaa\nababa\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4338
Solve the following coding problem using the programming language python: You are policeman and you are playing a game with Slavik. The game is turn-based and each turn consists of two phases. During the first phase you make your move and during the second phase Slavik makes his move. There are $n$ doors, the $i$-th door initially has durability equal to $a_i$. During your move you can try to break one of the doors. If you choose door $i$ and its current durability is $b_i$ then you reduce its durability to $max(0, b_i - x)$ (the value $x$ is given). During Slavik's move he tries to repair one of the doors. If he chooses door $i$ and its current durability is $b_i$ then he increases its durability to $b_i + y$ (the value $y$ is given). Slavik cannot repair doors with current durability equal to $0$. The game lasts $10^{100}$ turns. If some player cannot make his move then he has to skip it. Your goal is to maximize the number of doors with durability equal to $0$ at the end of the game. You can assume that Slavik wants to minimize the number of such doors. What is the number of such doors in the end if you both play optimally? -----Input----- The first line of the input contains three integers $n$, $x$ and $y$ ($1 \le n \le 100$, $1 \le x, y \le 10^5$) โ€” the number of doors, value $x$ and value $y$, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^5$), where $a_i$ is the initial durability of the $i$-th door. -----Output----- Print one integer โ€” the number of doors with durability equal to $0$ at the end of the game, if you and Slavik both play optimally. -----Examples----- Input 6 3 2 2 3 1 3 4 2 Output 6 Input 5 3 3 1 2 4 2 3 Output 2 Input 5 5 6 1 2 6 10 3 Output 2 -----Note----- Clarifications about the optimal strategy will be ignored. 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 sin, tan, cos, pi, atan2, sqrt, acos, atan, factorial from random import randint n, x, y = map(int, stdin.readline().split()) values = list(map(int, stdin.readline().split())) if x > y: stdout.write(str(n)) else: v = sum(map(lambda v: int(v <= x), values)) stdout.write(str(v // 2 + (v & 1))) ```
vfc_26059
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1102/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6 3 2\n2 3 1 3 4 2\n", "output": "6\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 3 3\n1 2 4 2 3\n", "output": "2\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4340
Solve the following coding problem using the programming language python: Mishka got an integer array $a$ of length $n$ as a birthday present (what a surprise!). Mishka doesn't like this present and wants to change it somehow. He has invented an algorithm and called it "Mishka's Adjacent Replacements Algorithm". This algorithm can be represented as a sequence of steps: Replace each occurrence of $1$ in the array $a$ with $2$; Replace each occurrence of $2$ in the array $a$ with $1$; Replace each occurrence of $3$ in the array $a$ with $4$; Replace each occurrence of $4$ in the array $a$ with $3$; Replace each occurrence of $5$ in the array $a$ with $6$; Replace each occurrence of $6$ in the array $a$ with $5$; $\dots$ Replace each occurrence of $10^9 - 1$ in the array $a$ with $10^9$; Replace each occurrence of $10^9$ in the array $a$ with $10^9 - 1$. Note that the dots in the middle of this algorithm mean that Mishka applies these replacements for each pair of adjacent integers ($2i - 1, 2i$) for each $i \in\{1, 2, \ldots, 5 \cdot 10^8\}$ as described above. For example, for the array $a = [1, 2, 4, 5, 10]$, the following sequence of arrays represents the algorithm: $[1, 2, 4, 5, 10]$ $\rightarrow$ (replace all occurrences of $1$ with $2$) $\rightarrow$ $[2, 2, 4, 5, 10]$ $\rightarrow$ (replace all occurrences of $2$ with $1$) $\rightarrow$ $[1, 1, 4, 5, 10]$ $\rightarrow$ (replace all occurrences of $3$ with $4$) $\rightarrow$ $[1, 1, 4, 5, 10]$ $\rightarrow$ (replace all occurrences of $4$ with $3$) $\rightarrow$ $[1, 1, 3, 5, 10]$ $\rightarrow$ (replace all occurrences of $5$ with $6$) $\rightarrow$ $[1, 1, 3, 6, 10]$ $\rightarrow$ (replace all occurrences of $6$ with $5$) $\rightarrow$ $[1, 1, 3, 5, 10]$ $\rightarrow$ $\dots$ $\rightarrow$ $[1, 1, 3, 5, 10]$ $\rightarrow$ (replace all occurrences of $10$ with $9$) $\rightarrow$ $[1, 1, 3, 5, 9]$. The later steps of the algorithm do not change the array. Mishka is very lazy and he doesn't want to apply these changes by himself. But he is very interested in their result. Help him find it. -----Input----- The first line of the input contains one integer number $n$ ($1 \le n \le 1000$) โ€” the number of elements in Mishka's birthday present (surprisingly, an array). The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) โ€” the elements of the array. -----Output----- Print $n$ integers โ€” $b_1, b_2, \dots, b_n$, where $b_i$ is the final value of the $i$-th element of the array after applying "Mishka's Adjacent Replacements Algorithm" to the array $a$. Note that you cannot change the order of elements in the array. -----Examples----- Input 5 1 2 4 5 10 Output 1 1 3 5 9 Input 10 10000 10 50605065 1 5 89 5 999999999 60506056 1000000000 Output 9999 9 50605065 1 5 89 5 999999999 60506055 999999999 -----Note----- The first example is described in the problem statement. 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()] print(*[x - ((x ^ 1) & 1) for x in a]) ```
vfc_26067
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1006/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n1 2 4 5 10\n", "output": "1 1 3 5 9\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\n10000 10 50605065 1 5 89 5 999999999 60506056 1000000000\n", "output": "9999 9 50605065 1 5 89 5 999999999 60506055 999999999\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4343
Solve the following coding problem using the programming language python: You are given two strings $s$ and $t$, both consisting of exactly $k$ lowercase Latin letters, $s$ is lexicographically less than $t$. Let's consider list of all strings consisting of exactly $k$ lowercase Latin letters, lexicographically not less than $s$ and not greater than $t$ (including $s$ and $t$) in lexicographical order. For example, for $k=2$, $s=$"az" and $t=$"bf" the list will be ["az", "ba", "bb", "bc", "bd", "be", "bf"]. Your task is to print the median (the middle element) of this list. For the example above this will be "bc". It is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$. -----Input----- The first line of the input contains one integer $k$ ($1 \le k \le 2 \cdot 10^5$) โ€” the length of strings. The second line of the input contains one string $s$ consisting of exactly $k$ lowercase Latin letters. The third line of the input contains one string $t$ consisting of exactly $k$ lowercase Latin letters. It is guaranteed that $s$ is lexicographically less than $t$. It is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$. -----Output----- Print one string consisting exactly of $k$ lowercase Latin letters โ€” the median (the middle element) of list of strings of length $k$ lexicographically not less than $s$ and not greater than $t$. -----Examples----- Input 2 az bf Output bc Input 5 afogk asdji Output alvuw Input 6 nijfvj tvqhwp Output qoztvz The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python k = int(input()) a = reversed(input()) b = reversed(input()) aa = [0] * (k + 1) bb = [0] * (k + 1) for i, x in enumerate(a): aa[i] = ord(x) - 97 for i, x in enumerate(b): bb[i] = ord(x) - 97 carry = 0 cc = [0] * (k + 1) for i in range(k + 1): cc[i] = aa[i] + bb[i] + carry if cc[i] >= 26: carry = 1 cc[i] -= 26 else: carry = 0 carry = 0 for i in reversed(list(range(k+1))): value = carry * 26 + cc[i] carry = value % 2 cc[i] = value // 2 answer = "" for x in reversed(cc[:-1]): answer += chr(x + 97) print(answer) ```
vfc_26079
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1144/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "2\naz\nbf\n", "output": "bc\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4344
Solve the following coding problem using the programming language python: There are $n$ students in a school class, the rating of the $i$-th student on Codehorses is $a_i$. You have to form a team consisting of $k$ students ($1 \le k \le n$) such that the ratings of all team members are distinct. If it is impossible to form a suitable team, print "NO" (without quotes). Otherwise print "YES", and then print $k$ distinct numbers which should be the indices of students in the team you form. If there are multiple answers, print any of them. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 100$) โ€” the number of students and the size of the team you have to form. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 100$), where $a_i$ is the rating of $i$-th student. -----Output----- If it is impossible to form a suitable team, print "NO" (without quotes). Otherwise print "YES", and then print $k$ distinct integers from $1$ to $n$ which should be the indices of students in the team you form. All the ratings of the students in the team should be distinct. You may print the indices in any order. If there are multiple answers, print any of them. Assume that the students are numbered from $1$ to $n$. -----Examples----- Input 5 3 15 13 15 15 12 Output YES 1 2 5 Input 5 4 15 13 15 15 12 Output NO Input 4 4 20 10 40 30 Output YES 1 2 3 4 -----Note----- All possible answers for the first example: {1 2 5} {2 3 5} {2 4 5} Note that the order does not matter. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n, k = list(map(int, input().split())) a = list(map(int, input().split())) uniq = [] seen = set() for i, x in enumerate(a): if x not in seen: seen.add(x) uniq.append((i + 1, x)) if len(uniq) < k: print('NO') else: print('YES') b = [str(i) for i, _ in uniq[:k]] print(' '.join(b)) ```
vfc_26083
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/988/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5 3\n15 13 15 15 12\n", "output": "YES\n1 2 5 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 4\n15 13 15 15 12\n", "output": "NO\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 4\n20 10 40 30\n", "output": "YES\n1 2 3 4 \n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4346
Solve the following coding problem using the programming language python: Vova plans to go to the conference by train. Initially, the train is at the point $1$ and the destination point of the path is the point $L$. The speed of the train is $1$ length unit per minute (i.e. at the first minute the train is at the point $1$, at the second minute โ€” at the point $2$ and so on). There are lanterns on the path. They are placed at the points with coordinates divisible by $v$ (i.e. the first lantern is at the point $v$, the second is at the point $2v$ and so on). There is also exactly one standing train which occupies all the points from $l$ to $r$ inclusive. Vova can see the lantern at the point $p$ if $p$ is divisible by $v$ and there is no standing train at this position ($p \not\in [l; r]$). Thus, if the point with the lantern is one of the points covered by the standing train, Vova can't see this lantern. Your problem is to say the number of lanterns Vova will see during the path. Vova plans to go to $t$ different conferences, so you should answer $t$ independent queries. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) โ€” the number of queries. Then $t$ lines follow. The $i$-th line contains four integers $L_i, v_i, l_i, r_i$ ($1 \le L, v \le 10^9$, $1 \le l \le r \le L$) โ€” destination point of the $i$-th path, the period of the lantern appearance and the segment occupied by the standing train. -----Output----- Print $t$ lines. The $i$-th line should contain one integer โ€” the answer for the $i$-th query. -----Example----- Input 4 10 2 3 7 100 51 51 51 1234 1 100 199 1000000000 1 1 1000000000 Output 3 0 1134 0 -----Note----- For the first example query, the answer is $3$. There are lanterns at positions $2$, $4$, $6$, $8$ and $10$, but Vova didn't see the lanterns at positions $4$ and $6$ because of the standing train. For the second example query, the answer is $0$ because the only lantern is at the point $51$ and there is also a standing train at this point. For the third example query, the answer is $1134$ because there are $1234$ lanterns, but Vova didn't see the lanterns from the position $100$ to the position $199$ inclusive. For the fourth example query, the answer is $0$ because the standing train covers the whole path. 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 i in range(t): d,v,l,r = list(map(int, input().split())) ans = d // v ans -= r//v - (l-1)//v print(ans) ```
vfc_26091
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1066/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n10 2 3 7\n100 51 51 51\n1234 1 100 199\n1000000000 1 1 1000000000\n", "output": "3\n0\n1134\n0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4348
Solve the following coding problem using the programming language python: You are given a string $s$ consisting of $n$ lowercase Latin letters. Polycarp wants to remove exactly $k$ characters ($k \le n$) from the string $s$. Polycarp uses the following algorithm $k$ times: if there is at least one letter 'a', remove the leftmost occurrence and stop the algorithm, otherwise go to next item; if there is at least one letter 'b', remove the leftmost occurrence and stop the algorithm, otherwise go to next item; ... remove the leftmost occurrence of the letter 'z' and stop the algorithm. This algorithm removes a single letter from the string. Polycarp performs this algorithm exactly $k$ times, thus removing exactly $k$ characters. Help Polycarp find the resulting string. -----Input----- The first line of input contains two integers $n$ and $k$ ($1 \le k \le n \le 4 \cdot 10^5$) โ€” the length of the string and the number of letters Polycarp will remove. The second line contains the string $s$ consisting of $n$ lowercase Latin letters. -----Output----- Print the string that will be obtained from $s$ after Polycarp removes exactly $k$ letters using the above algorithm $k$ times. If the resulting string is empty, print nothing. It is allowed to print nothing or an empty line (line break). -----Examples----- Input 15 3 cccaabababaccbc Output cccbbabaccbc Input 15 9 cccaabababaccbc Output cccccc Input 1 1 u Output 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 Counter, defaultdict from string import ascii_lowercase as al n, k = list(map(int, input().split())) s = list(input()) C = defaultdict(int, Counter(s)) C_ = defaultdict(int) k_ = k for char in al: temp = min(C[char], k_) C_[char] += temp k_ -= temp for i, el in enumerate(s): if C_[el] > 0: s[i] = '' C_[el] -= 1 print(''.join(s)) ```
vfc_26099
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/999/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "15 3\ncccaabababaccbc\n", "output": "cccbbabaccbc\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "15 9\ncccaabababaccbc\n", "output": "cccccc\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4349
Solve the following coding problem using the programming language python: The only difference between easy and hard versions is constraints. Ivan plays a computer game that contains some microtransactions to make characters look cooler. Since Ivan wants his character to be really cool, he wants to use some of these microtransactions โ€” and he won't start playing until he gets all of them. Each day (during the morning) Ivan earns exactly one burle. There are $n$ types of microtransactions in the game. Each microtransaction costs $2$ burles usually and $1$ burle if it is on sale. Ivan has to order exactly $k_i$ microtransactions of the $i$-th type (he orders microtransactions during the evening). Ivan can order any (possibly zero) number of microtransactions of any types during any day (of course, if he has enough money to do it). If the microtransaction he wants to order is on sale then he can buy it for $1$ burle and otherwise he can buy it for $2$ burles. There are also $m$ special offers in the game shop. The $j$-th offer $(d_j, t_j)$ means that microtransactions of the $t_j$-th type are on sale during the $d_j$-th day. Ivan wants to order all microtransactions as soon as possible. Your task is to calculate the minimum day when he can buy all microtransactions he want and actually start playing. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) โ€” the number of types of microtransactions and the number of special offers in the game shop. The second line of the input contains $n$ integers $k_1, k_2, \dots, k_n$ ($0 \le k_i \le 2 \cdot 10^5$), where $k_i$ is the number of copies of microtransaction of the $i$-th type Ivan has to order. It is guaranteed that sum of all $k_i$ is not less than $1$ and not greater than $2 \cdot 10^5$. The next $m$ lines contain special offers. The $j$-th of these lines contains the $j$-th special offer. It is given as a pair of integers $(d_j, t_j)$ ($1 \le d_j \le 2 \cdot 10^5, 1 \le t_j \le n$) and means that microtransactions of the $t_j$-th type are on sale during the $d_j$-th day. -----Output----- Print one integer โ€” the minimum day when Ivan can order all microtransactions he wants and actually start playing. -----Examples----- Input 5 6 1 2 0 2 0 2 4 3 3 1 5 1 2 1 5 2 3 Output 8 Input 5 3 4 2 1 3 2 3 5 4 2 2 5 Output 20 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import collections def main(): from sys import stdin, stdout def read(): return stdin.readline().rstrip('\n') def read_array(sep=None, maxsplit=-1): return read().split(sep, maxsplit) def read_int(): return int(read()) def read_int_array(sep=None, maxsplit=-1): return [int(a) for a in read_array(sep, maxsplit)] def write(*args, **kwargs): sep = kwargs.get('sep', ' ') end = kwargs.get('end', '\n') stdout.write(sep.join(str(a) for a in args) + end) def write_array(array, **kwargs): sep = kwargs.get('sep', ' ') end = kwargs.get('end', '\n') stdout.write(sep.join(str(a) for a in array) + end) def enough(days): bought = [] # (type, amount) bought_total = 0 used_from = days for d in range(days, 0, -1): used_from = min(d, used_from) for t in offers.get(d, []): if K[t] > 0: x = min(K[t], used_from) K[t] -= x bought.append((t, x)) bought_total += x used_from -= x if not used_from: break remaining_money = days - bought_total ans = (total_transaction - bought_total) * 2 <= remaining_money for t, a in bought: K[t] += a return ans n, m = read_int_array() K = read_int_array() total_transaction = sum(K) offers = collections.defaultdict(list) for _ in range(m): d, t = read_int_array() offers[d].append(t-1) low = total_transaction high = low * 2 ans = high while low <= high: mid = (low + high) // 2 if enough(mid): ans = mid high = mid - 1 else: low = mid + 1 write(ans) main() ```
vfc_26103
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1165/F2", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5 6\n1 2 0 2 0\n2 4\n3 3\n1 5\n1 2\n1 5\n2 3\n", "output": "8\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 3\n4 2 1 3 2\n3 5\n4 2\n2 5\n", "output": "20\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "78 36\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0\n1 52\n2 6\n9 43\n10 52\n4 63\n9 35\n10 67\n9 17\n3 43\n4 38\n1 27\n9 44\n6 74\n7 3\n8 18\n1 52\n1 68\n5 51\n5 2\n7 50\n1 72\n1 37\n8 64\n10 30\n2 68\n1 59\n5 12\n9 11\n10 23\n2 51\n10 56\n6 17\n1 49\n3 20\n10 62\n10 40\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 47\n1 0 0 1 2 1 1 3 1 3\n4 9\n15 5\n6 2\n4 1\n23 3\n9 10\n12 2\n5 10\n2 4\n2 4\n18 4\n23 5\n17 1\n22 3\n24 4\n20 5\n7 3\n17 10\n3 10\n12 10\n4 6\n3 10\n24 2\n12 1\n25 9\n12 5\n25 2\n13 5\n6 5\n4 9\n6 10\n7 2\n7 9\n11 7\n9 4\n1 1\n7 2\n8 1\n11 9\n25 9\n7 8\n9 9\n8 1\n6 4\n22 8\n16 6\n22 6\n", "output": "13\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4 7\n23 78 12 46\n100 1\n41 3\n213 2\n321 3\n12 2\n87 1\n76 2\n", "output": "213\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4350
Solve the following coding problem using the programming language python: A star is a figure of the following type: an asterisk character '*' in the center of the figure and four rays (to the left, right, top, bottom) of the same positive length. The size of a star is the length of its rays. The size of a star must be a positive number (i.e. rays of length $0$ are not allowed). Let's consider empty cells are denoted by '.', then the following figures are stars: [Image] The leftmost figure is a star of size $1$, the middle figure is a star of size $2$ and the rightmost figure is a star of size $3$. You are given a rectangular grid of size $n \times m$ consisting only of asterisks '*' and periods (dots) '.'. Rows are numbered from $1$ to $n$, columns are numbered from $1$ to $m$. Your task is to draw this grid using any number of stars or find out that it is impossible. Stars can intersect, overlap or even coincide with each other. The number of stars in the output can't exceed $n \cdot m$. Each star should be completely inside the grid. You can use stars of same and arbitrary sizes. In this problem, you do not need to minimize the number of stars. Just find any way to draw the given grid with at most $n \cdot m$ stars. -----Input----- The first line of the input contains two integers $n$ and $m$ ($3 \le n, m \le 1000$) โ€” the sizes of the given grid. The next $n$ lines contains $m$ characters each, the $i$-th line describes the $i$-th row of the grid. It is guaranteed that grid consists of characters '*' and '.' only. -----Output----- If it is impossible to draw the given grid using stars only, print "-1". Otherwise in the first line print one integer $k$ ($0 \le k \le n \cdot m$) โ€” the number of stars needed to draw the given grid. The next $k$ lines should contain three integers each โ€” $x_j$, $y_j$ and $s_j$, where $x_j$ is the row index of the central star character, $y_j$ is the column index of the central star character and $s_j$ is the size of the star. Each star should be completely inside the grid. -----Examples----- Input 6 8 ....*... ...**... ..*****. ...**... ....*... ........ Output 3 3 4 1 3 5 2 3 5 1 Input 5 5 .*... ****. .**** ..**. ..... Output 3 2 2 1 3 3 1 3 4 1 Input 5 5 .*... ***.. .*... .*... ..... Output -1 Input 3 3 *.* .*. *.* Output -1 -----Note----- In the first example the output 2 3 4 1 3 5 2 is also correct. 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()) g=[[*input()] for _ in range(n)] c=[[0 for _ in range(m)] for _ in range(n)] for i in range(n): v=0 for j in range(m): v=(v+1)*(g[i][j]=='*') c[i][j]=v v=0 for j in range(m-1,-1,-1): v=(v+1)*(g[i][j]=='*') c[i][j]=min(c[i][j],v) for j in range(m): v=0 for i in range(n): v=(v+1)*(g[i][j]=='*') c[i][j]=min(c[i][j],v) v=0 for i in range(n-1,-1,-1): v=(v+1)*(g[i][j]=='*') c[i][j]=min(c[i][j],v) for i in range(n): for j in range(m): if c[i][j]==1: c[i][j]=0 for i in range(n): v=0 for j in range(m): v=max(v-1,c[i][j]) if v:g[i][j]='.' v=0 for j in range(m-1,-1,-1): v=max(v-1,c[i][j]) if v:g[i][j]='.' for j in range(m): v=0 for i in range(n): v=max(v-1,c[i][j]) if v:g[i][j]='.' for i in range(n-1,-1,-1): v=max(v-1,c[i][j]) if v:g[i][j]='.' if all(g[i][j]=='.' for i in range(n) for j in range(m)): r=[(i+1,j+1,c[i][j]-1) for i in range(n) for j in range(m) if c[i][j]] print(len(r)) for t in r: print(*t) else: print(-1) ```
vfc_26107
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1015/E2", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6 8\n....*...\n...**...\n..*****.\n...**...\n....*...\n........\n", "output": "2\n3 4 1\n3 5 2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 5\n.*...\n****.\n.****\n..**.\n.....\n", "output": "3\n2 2 1\n3 3 1\n3 4 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 5\n.*...\n***..\n.*...\n.*...\n.....\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 3\n*.*\n.*.\n*.*\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 3\n.*.\n***\n**.\n", "output": "-1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 3\n.*.\n***\n.*.\n", "output": "1\n2 2 1\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4372
Solve the following coding problem using the programming language python: There were $n$ types of swords in the theater basement which had been used during the plays. Moreover there were exactly $x$ swords of each type. $y$ people have broken into the theater basement and each of them has taken exactly $z$ swords of some single type. Note that different people might have taken different types of swords. Note that the values $x, y$ and $z$ are unknown for you. The next morning the director of the theater discovers the loss. He counts all swords โ€” exactly $a_i$ swords of the $i$-th type are left untouched. The director has no clue about the initial number of swords of each type in the basement, the number of people who have broken into the basement and how many swords each of them have taken. For example, if $n=3$, $a = [3, 12, 6]$ then one of the possible situations is $x=12$, $y=5$ and $z=3$. Then the first three people took swords of the first type and the other two people took swords of the third type. Note that you don't know values $x, y$ and $z$ beforehand but know values of $n$ and $a$. Thus he seeks for your help. Determine the minimum number of people $y$, which could have broken into the theater basement, and the number of swords $z$ each of them has taken. -----Input----- The first line of the input contains one integer $n$ $(2 \le n \le 2 \cdot 10^{5})$ โ€” the number of types of swords. The second line of the input contains the sequence $a_1, a_2, \dots, a_n$ $(0 \le a_i \le 10^{9})$, where $a_i$ equals to the number of swords of the $i$-th type, which have remained in the basement after the theft. It is guaranteed that there exists at least one such pair of indices $(j, k)$ that $a_j \neq a_k$. -----Output----- Print two integers $y$ and $z$ โ€” the minimum number of people which could have broken into the basement and the number of swords each of them has taken. -----Examples----- Input 3 3 12 6 Output 5 3 Input 2 2 9 Output 1 7 Input 7 2 1000000000 4 6 8 4 2 Output 2999999987 2 Input 6 13 52 0 13 26 52 Output 12 13 -----Note----- In the first example the minimum value of $y$ equals to $5$, i.e. the minimum number of people who could have broken into the basement, is $5$. Each of them has taken $3$ swords: three of them have taken $3$ swords of the first type, and two others have taken $3$ swords of the third type. In the second example the minimum value of $y$ is $1$, i.e. the minimum number of people who could have broken into the basement, equals to $1$. He has taken $7$ swords of the first type. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python from bisect import * from collections import * from itertools import * import functools import sys import math from decimal import * from copy import * from heapq import * getcontext().prec = 30 MAX = sys.maxsize MAXN = 10**6+1 MOD = 10**9+7 spf = [i for i in range(MAXN)] def sieve(): for i in range(2,MAXN,2): spf[i] = 2 for i in range(3,int(MAXN**0.5)+1): if spf[i]==i: for j in range(i*i,MAXN,i): if spf[j]==j: spf[j]=i def mhd(a,b): return abs(a[0]-b[0])+abs(b[1]-a[1]) def charIN(x= ' '): return(sys.stdin.readline().strip().split(x)) def arrIN(x = ' '): return list(map(int,sys.stdin.readline().strip().split(x))) def eld(x,y): a = y[0]-x[0] b = x[1]-y[1] return (a*a+b*b)**0.5 def lgcd(a): g = a[0] for i in range(1,len(a)): g = math.gcd(g,a[i]) return g def ms(a): msf = -MAX meh = 0 st = en = be = 0 for i in range(len(a)): meh+=a[i] if msf<meh: msf = meh st = be en = i if meh<0: meh = 0 be = i+1 return msf,st,en def ncr(n,r): num=den=1 for i in range(r): num = (num*(n-i))%MOD den = (den*(i+1))%MOD return (num*(pow(den,MOD-2,MOD)))%MOD def flush(): return sys.stdout.flush() '''*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*''' n = int(input()) a = arrIN() mx = max(a) cnt = 0 temp = [] for i in a: cnt+=mx-i temp.append(mx-i) g = lgcd(temp) print(cnt//g,g) ```
vfc_26195
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1216/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\n3 12 6\n", "output": "5 3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\n2 9\n", "output": "1 7\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7\n2 1000000000 4 6 8 4 2\n", "output": "2999999987 2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n13 52 0 13 26 52\n", "output": "12 13\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4373
Solve the following coding problem using the programming language python: Polycarp wants to train before another programming competition. During the first day of his training he should solve exactly $1$ problem, during the second day โ€” exactly $2$ problems, during the third day โ€” exactly $3$ problems, and so on. During the $k$-th day he should solve $k$ problems. Polycarp has a list of $n$ contests, the $i$-th contest consists of $a_i$ problems. During each day Polycarp has to choose exactly one of the contests he didn't solve yet and solve it. He solves exactly $k$ problems from this contest. Other problems are discarded from it. If there are no contests consisting of at least $k$ problems that Polycarp didn't solve yet during the $k$-th day, then Polycarp stops his training. How many days Polycarp can train if he chooses the contests optimally? -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) โ€” the number of contests. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$) โ€” the number of problems in the $i$-th contest. -----Output----- Print one integer โ€” the maximum number of days Polycarp can train if he chooses the contests optimally. -----Examples----- Input 4 3 1 4 1 Output 3 Input 3 1 1 1 Output 1 Input 5 1 1 1 2 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 def main(): n = int(input()) arr = sorted(map(int, input().split())) j = 1 for x in arr: if x >= j: j += 1 print(j - 1) return 0 main() ```
vfc_26199
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1165/B", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\n3 1 4 1\n", "output": "3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\n1 1 1\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5\n1 1 1 2 2\n", "output": "2\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4374
Solve the following coding problem using the programming language python: You are given a forest โ€” an undirected graph with $n$ vertices such that each its connected component is a tree. The diameter (aka "longest shortest path") of a connected undirected graph is the maximum number of edges in the shortest path between any pair of its vertices. You task is to add some edges (possibly zero) to the graph so that it becomes a tree and the diameter of the tree is minimal possible. If there are multiple correct answers, print any of them. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n \le 1000$, $0 \le m \le n - 1$) โ€” the number of vertices of the graph and the number of edges, respectively. Each of the next $m$ lines contains two integers $v$ and $u$ ($1 \le v, u \le n$, $v \ne u$) โ€” the descriptions of the edges. It is guaranteed that the given graph is a forest. -----Output----- In the first line print the diameter of the resulting tree. Each of the next $(n - 1) - m$ lines should contain two integers $v$ and $u$ ($1 \le v, u \le n$, $v \ne u$) โ€” the descriptions of the added edges. The resulting graph should be a tree and its diameter should be minimal possible. For $m = n - 1$ no edges are added, thus the output consists of a single integer โ€” diameter of the given tree. If there are multiple correct answers, print any of them. -----Examples----- Input 4 2 1 2 2 3 Output 2 4 2 Input 2 0 Output 1 1 2 Input 3 2 1 3 2 3 Output 2 -----Note----- In the first example adding edges (1, 4) or (3, 4) will lead to a total diameter of 3. Adding edge (2, 4), however, will make it 2. Edge (1, 2) is the only option you have for the second example. The diameter is 1. You can't add any edges in the third example. The diameter is already 2. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import math n,m=map(int,input().split()) neigh=[] for i in range(n): neigh.append([]) for i in range(m): a,b=map(int,input().split()) neigh[a-1].append(b-1) neigh[b-1].append(a-1) seen=set() index=[0]*n diams=[] trees=0 for i in range(n): if i not in seen: trees+=1 index[i]=trees seen.add(i) layer=[i] prev=None pars=[None] while layer!=[]: newlayer=[] newpars=[] for i in range(len(layer)): vert=layer[i] par=pars[i] for child in neigh[vert]: if child!=par: newlayer.append(child) newpars.append(vert) index[child]=trees seen.add(child) prev=layer layer=newlayer pars=newpars far=prev[0] layer=[[far]] pars=[None] prev=None while layer!=[]: newlayer=[] newpars=[] for i in range(len(layer)): vert=layer[i][-1] par=pars[i] for child in neigh[vert]: if child!=par: newlayer.append(layer[i]+[child]) newpars.append(vert) prev=layer layer=newlayer pars=newpars diam=prev[0] lent=len(diam) mid=diam[lent//2] diams.append((lent-1,mid)) diams.sort(reverse=True) poss=[diams[0][0]] if len(diams)>1: poss.append(math.ceil(diams[0][0]/2)+1+math.ceil(diams[1][0]/2)) if len(diams)>2: poss.append(math.ceil(diams[1][0]/2)+2+math.ceil(diams[2][0]/2)) print(max(poss)) cent=diams[0][1] for i in range(len(diams)-1): print(cent+1,diams[i+1][1]+1) ```
vfc_26203
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1092/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4 2\n1 2\n2 3\n", "output": "2\n4 2\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 0\n", "output": "1\n1 2\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4375
Solve the following coding problem using the programming language python: You are given a tree, which consists of $n$ vertices. Recall that a tree is a connected undirected graph without cycles. [Image] Example of a tree. Vertices are numbered from $1$ to $n$. All vertices have weights, the weight of the vertex $v$ is $a_v$. Recall that the distance between two vertices in the tree is the number of edges on a simple path between them. Your task is to find the subset of vertices with the maximum total weight (the weight of the subset is the sum of weights of all vertices in it) such that there is no pair of vertices with the distance $k$ or less between them in this subset. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 200$) โ€” the number of vertices in the tree and the distance restriction, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^5$), where $a_i$ is the weight of the vertex $i$. The next $n - 1$ lines contain edges of the tree. Edge $i$ is denoted by two integers $u_i$ and $v_i$ โ€” the labels of vertices it connects ($1 \le u_i, v_i \le n$, $u_i \ne v_i$). It is guaranteed that the given edges form a tree. -----Output----- Print one integer โ€” the maximum total weight of the subset in which all pairs of vertices have distance more than $k$. -----Examples----- Input 5 1 1 2 3 4 5 1 2 2 3 3 4 3 5 Output 11 Input 7 2 2 1 2 1 2 1 1 6 4 1 5 3 1 2 3 7 5 7 4 Output 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())) a = list(map(int, input().split())) g = {} def dfs(v, p=-1): c = [dfs(child, v) for child in g.get(v, set()) - {p}] c.sort(key=len, reverse=True) r = [] i = 0 while c: if i >= len(c[-1]): c.pop() else: o = max(i, k - i - 1) s = q = 0 for x in c: if len(x) <= o: q = max(q, x[i]) else: s += x[o] q = max(q, x[i] - x[o]) r.append(q + s) i += 1 r.append(0) for i in range(len(r) - 1, 0, -1): r[i - 1] = max(r[i - 1], r[i]) while len(r) > 1 and r[-2] == 0: r.pop() o = (r[k] if k < len(r) else 0) + a[v] r.insert(0, max(o, r[0])) return r for _ in range(1, n): u, v = [int(x) - 1 for x in input().split()] g.setdefault(u, set()).add(v) g.setdefault(v, set()).add(u) print(dfs(0)[0]) ```
vfc_26207
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1249/F", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5 1\n1 2 3 4 5\n1 2\n2 3\n3 4\n3 5\n", "output": "11\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4376
Solve the following coding problem using the programming language python: There are $n$ dormitories in Berland State University, they are numbered with integers from $1$ to $n$. Each dormitory consists of rooms, there are $a_i$ rooms in $i$-th dormitory. The rooms in $i$-th dormitory are numbered from $1$ to $a_i$. A postman delivers letters. Sometimes there is no specific dormitory and room number in it on an envelope. Instead of it only a room number among all rooms of all $n$ dormitories is written on an envelope. In this case, assume that all the rooms are numbered from $1$ to $a_1 + a_2 + \dots + a_n$ and the rooms of the first dormitory go first, the rooms of the second dormitory go after them and so on. For example, in case $n=2$, $a_1=3$ and $a_2=5$ an envelope can have any integer from $1$ to $8$ written on it. If the number $7$ is written on an envelope, it means that the letter should be delivered to the room number $4$ of the second dormitory. For each of $m$ letters by the room number among all $n$ dormitories, determine the particular dormitory and the room number in a dormitory where this letter should be delivered. -----Input----- The first line contains two integers $n$ and $m$ $(1 \le n, m \le 2 \cdot 10^{5})$ โ€” the number of dormitories and the number of letters. The second line contains a sequence $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^{10})$, where $a_i$ equals to the number of rooms in the $i$-th dormitory. The third line contains a sequence $b_1, b_2, \dots, b_m$ $(1 \le b_j \le a_1 + a_2 + \dots + a_n)$, where $b_j$ equals to the room number (among all rooms of all dormitories) for the $j$-th letter. All $b_j$ are given in increasing order. -----Output----- Print $m$ lines. For each letter print two integers $f$ and $k$ โ€” the dormitory number $f$ $(1 \le f \le n)$ and the room number $k$ in this dormitory $(1 \le k \le a_f)$ to deliver the letter. -----Examples----- Input 3 6 10 15 12 1 9 12 23 26 37 Output 1 1 1 9 2 2 2 13 3 1 3 12 Input 2 3 5 10000000000 5 6 9999999999 Output 1 5 2 1 2 9999999994 -----Note----- In the first example letters should be delivered in the following order: the first letter in room $1$ of the first dormitory the second letter in room $9$ of the first dormitory the third letter in room $2$ of the second dormitory the fourth letter in room $13$ of the second dormitory the fifth letter in room $1$ of the third dormitory the sixth letter in room $12$ of the third dormitory The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python d,l = list(map(int,input().split())) a = list(map(int,input().split())) letters = list(map(int,input().split())) pos = [0] * (1+d) for i,x in enumerate(a): pos[i+1] = pos[i] + x res = [] i = 1 for letter in letters: while pos[i]<letter: i+=1 res.append(str(i) + " " + str(letter - pos[i-1])) print('\n'.join(res)) ```
vfc_26211
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/978/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 6\n10 15 12\n1 9 12 23 26 37\n", "output": "1 1\n1 9\n2 2\n2 13\n3 1\n3 12\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2 3\n5 10000000000\n5 6 9999999999\n", "output": "1 5\n2 1\n2 9999999994\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1 1\n1\n1\n", "output": "1 1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 15\n10 20 30 20 10\n1 6 10 11 15 30 31 54 60 61 76 80 81 84 90\n", "output": "1 1\n1 6\n1 10\n2 1\n2 5\n2 20\n3 1\n3 24\n3 30\n4 1\n4 16\n4 20\n5 1\n5 4\n5 10\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4377
Solve the following coding problem using the programming language python: Polycarp has guessed three positive integers $a$, $b$ and $c$. He keeps these numbers in secret, but he writes down four numbers on a board in arbitrary order โ€” their pairwise sums (three numbers) and sum of all three numbers (one number). So, there are four numbers on a board in random order: $a+b$, $a+c$, $b+c$ and $a+b+c$. You have to guess three numbers $a$, $b$ and $c$ using given numbers. Print three guessed integers in any order. Pay attention that some given numbers $a$, $b$ and $c$ can be equal (it is also possible that $a=b=c$). -----Input----- The only line of the input contains four positive integers $x_1, x_2, x_3, x_4$ ($2 \le x_i \le 10^9$) โ€” numbers written on a board in random order. It is guaranteed that the answer exists for the given number $x_1, x_2, x_3, x_4$. -----Output----- Print such positive integers $a$, $b$ and $c$ that four numbers written on a board are values $a+b$, $a+c$, $b+c$ and $a+b+c$ written in some order. Print $a$, $b$ and $c$ in any order. If there are several answers, you can print any. It is guaranteed that the answer exists. -----Examples----- Input 3 6 5 4 Output 2 1 3 Input 40 40 40 60 Output 20 20 20 Input 201 101 101 200 Output 1 100 100 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python A = list(map(int, input().split())) A.sort() print(A[3] - A[0], A[3] - A[2], A[3] - A[1]) ```
vfc_26215
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1154/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3 6 5 4\n", "output": "1 2 3\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "40 40 40 60\n", "output": "20 20 20\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "201 101 101 200\n", "output": "1 100 100\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1000000000 666666667 666666667 666666666\n", "output": "333333333 333333333 333333334\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "600000000 900000000 500000000 1000000000\n", "output": "100000000 400000000 500000000\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4378
Solve the following coding problem using the programming language python: You have a garland consisting of $n$ lamps. Each lamp is colored red, green or blue. The color of the $i$-th lamp is $s_i$ ('R', 'G' and 'B' โ€” colors of lamps in the garland). You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is diverse. A garland is called diverse if any two adjacent (consecutive) lamps (i. e. such lamps that the distance between their positions is $1$) have distinct colors. In other words, if the obtained garland is $t$ then for each $i$ from $1$ to $n-1$ the condition $t_i \ne t_{i + 1}$ should be satisfied. Among all ways to recolor the initial garland to make it diverse you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) โ€” the number of lamps. The second line of the input contains the string $s$ consisting of $n$ characters 'R', 'G' and 'B' โ€” colors of lamps in the garland. -----Output----- In the first line of the output print one integer $r$ โ€” the minimum number of recolors needed to obtain a diverse garland from the given one. In the second line of the output print one string $t$ of length $n$ โ€” a diverse garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them. -----Examples----- Input 9 RBGRRBRGG Output 2 RBGRGBRGR Input 8 BBBGBRRR Output 2 BRBGBRGR Input 13 BBRRRRGGGGGRR Output 6 BGRBRBGBGBGRG The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python def deal(a,b,c='0'): if(c=='0' or a==c): if(a=='R'): return 'B' if(a=='B'): return 'R' if(a=='G'): return 'B' if(a=='R' and c=='B'): b = 'G' if (a == 'R' and c == 'G'): b = 'B' if (a == 'B' and c == 'R'): b = 'G' if (a == 'B' and c == 'G'): b = 'R' if (a == 'G' and c == 'B'): b = 'R' if (a == 'G' and c == 'R'): b = 'B' return b n = int(input()) ss = input() s = list(ss) answer = [s[0]] number = 0 for i in range(0,n-1): ans = "" if (s[i]==s[i+1]): number += 1 if(i==n-2): ans = deal(s[i],s[i+1]) else: ans = deal(s[i],s[i+1],s[i+2]) s[i+1] = ans answer.append(ans) else: answer.append(s[i+1]) s = "".join(answer) print(number) print(s) ```
vfc_26219
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1108/D", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "9\nRBGRRBRGG\n", "output": "2\nRBGRGBRGR\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "8\nBBBGBRRR\n", "output": "2\nBRBGBRGR\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "13\nBBRRRRGGGGGRR\n", "output": "6\nBGRBRBGBGBGRG\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4379
Solve the following coding problem using the programming language python: You are given an integer array of length $n$. You have to choose some subsequence of this array of maximum length such that this subsequence forms a increasing sequence of consecutive integers. In other words the required sequence should be equal to $[x, x + 1, \dots, x + k - 1]$ for some value $x$ and length $k$. Subsequence of an array can be obtained by erasing some (possibly zero) elements from the array. You can erase any elements, not necessarily going successively. The remaining elements preserve their order. For example, for the array $[5, 3, 1, 2, 4]$ the following arrays are subsequences: $[3]$, $[5, 3, 1, 2, 4]$, $[5, 1, 4]$, but the array $[1, 3]$ is not. -----Input----- The first line of the input containing integer number $n$ ($1 \le n \le 2 \cdot 10^5$) โ€” the length of the array. The second line of the input containing $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) โ€” the array itself. -----Output----- On the first line print $k$ โ€” the maximum length of the subsequence of the given array that forms an increasing sequence of consecutive integers. On the second line print the sequence of the indices of the any maximum length subsequence of the given array that forms an increasing sequence of consecutive integers. -----Examples----- Input 7 3 3 4 7 5 6 8 Output 4 2 3 5 6 Input 6 1 3 5 2 4 6 Output 2 1 4 Input 4 10 9 8 7 Output 1 1 Input 9 6 7 8 3 4 5 9 10 11 Output 6 1 2 3 7 8 9 -----Note----- All valid answers for the first example (as sequences of indices): $[1, 3, 5, 6]$ $[2, 3, 5, 6]$ All valid answers for the second example: $[1, 4]$ $[2, 5]$ $[3, 6]$ All valid answers for the third example: $[1]$ $[2]$ $[3]$ $[4]$ All valid answers for the fourth example: $[1, 2, 3, 7, 8, 9]$ 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 * input() a = list(map(int, input().split())) m = defaultdict(int) for x in reversed(a): m[x] = m[x + 1] + 1 v = max(list(m.keys()), key=m.get) seq = [] for i, x in enumerate(a): if v == x: seq.append(i + 1) v += 1 print(len(seq)) print(*seq) ```
vfc_26223
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/977/F", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "7\n3 3 4 7 5 6 8\n", "output": "4\n2 3 5 6 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "6\n1 3 5 2 4 6\n", "output": "2\n1 4 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "4\n10 9 8 7\n", "output": "1\n1 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "9\n6 7 8 3 4 5 9 10 11\n", "output": "6\n1 2 3 7 8 9 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n1337\n", "output": "1\n1 \n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4389
Solve the following coding problem using the programming language python: Alice guesses the strings that Bob made for her. At first, Bob came up with the secret string $a$ consisting of lowercase English letters. The string $a$ has a length of $2$ or more characters. Then, from string $a$ he builds a new string $b$ and offers Alice the string $b$ so that she can guess the string $a$. Bob builds $b$ from $a$ as follows: he writes all the substrings of length $2$ of the string $a$ in the order from left to right, and then joins them in the same order into the string $b$. For example, if Bob came up with the string $a$="abac", then all the substrings of length $2$ of the string $a$ are: "ab", "ba", "ac". Therefore, the string $b$="abbaac". You are given the string $b$. Help Alice to guess the string $a$ that Bob came up with. It is guaranteed that $b$ was built according to the algorithm given above. It can be proved that the answer to the problem is unique. -----Input----- The first line contains a single positive integer $t$ ($1 \le t \le 1000$)ย โ€” the number of test cases in the test. Then $t$ test cases follow. Each test case consists of one line in which the string $b$ is written, consisting of lowercase English letters ($2 \le |b| \le 100$)ย โ€” the string Bob came up with, where $|b|$ is the length of the string $b$. It is guaranteed that $b$ was built according to the algorithm given above. -----Output----- Output $t$ answers to test cases. Each answer is the secret string $a$, consisting of lowercase English letters, that Bob came up with. -----Example----- Input 4 abbaac ac bccddaaf zzzzzzzzzz Output abac ac bcdaf zzzzzz -----Note----- The first test case is explained in the statement. In the second test case, Bob came up with the string $a$="ac", the string $a$ has a length $2$, so the string $b$ is equal to the string $a$. In the third test case, Bob came up with the string $a$="bcdaf", substrings of length $2$ of string $a$ are: "bc", "cd", "da", "af", so the string $b$="bccddaaf". The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python import sys import random from math import * def input(): return sys.stdin.readline().strip() def iinput(): return int(input()) def finput(): return float(input()) def tinput(): return input().split() def linput(): return list(input()) def rinput(): return map(int, tinput()) def fiinput(): return map(float, tinput()) def rlinput(): return list(map(int, input().split())) def trinput(): return tuple(rinput()) def srlinput(): return sorted(list(map(int, input().split()))) def YESNO(fl): if fl: print("NO") else: print("YES") def main(): #n = iinput() #k = iinput() #m = iinput() #n = int(sys.stdin.readline().strip()) #n, k = rinput() #n, m = rinput() #m, k = rinput() #n, k, m = rinput() #n, m, k = rinput() #k, n, m = rinput() #k, m, n = rinput() #m, k, n = rinput() #m, n, k = rinput() #q = srlinput() #q = rlinput() s = input() print(s[:2] + s[3::2]) for inytd in range(iinput()): main() ```
vfc_26263
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1367/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4\nabbaac\nac\nbccddaaf\nzzzzzzzzzz\n", "output": "abac\nac\nbcdaf\nzzzzzz\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4390
Solve the following coding problem using the programming language python: You are given two positive integers $a$ and $b$. In one move you can increase $a$ by $1$ (replace $a$ with $a+1$). Your task is to find the minimum number of moves you need to do in order to make $a$ divisible by $b$. It is possible, that you have to make $0$ moves, as $a$ is already divisible by $b$. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) โ€” the number of test cases. Then $t$ test cases follow. The only line of the test case contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- For each test case print the answer โ€” the minimum number of moves you need to do in order to make $a$ divisible by $b$. -----Example----- Input 5 10 4 13 9 100 13 123 456 92 46 Output 2 5 4 333 0 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python q = int(input()) for _ in range(q): a, b = list(map(int, input().split())) print((b - a % b) % b) ```
vfc_26267
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1328/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "5\n10 4\n13 9\n100 13\n123 456\n92 46\n", "output": "2\n5\n4\n333\n0\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n914 78\n", "output": "22\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n2232 7\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n100 9090\n", "output": "8990\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n515151 2\n", "output": "1\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\n9987 1\n", "output": "0\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4391
Solve the following coding problem using the programming language python: The heat during the last few days has been really intense. Scientists from all over the Berland study how the temperatures and weather change, and they claim that this summer is abnormally hot. But any scientific claim sounds a lot more reasonable if there are some numbers involved, so they have decided to actually calculate some value which would represent how high the temperatures are. Mathematicians of Berland State University came up with a special heat intensity value. This value is calculated as follows: Suppose we want to analyze the segment of $n$ consecutive days. We have measured the temperatures during these $n$ days; the temperature during $i$-th day equals $a_i$. We denote the average temperature of a segment of some consecutive days as the arithmetic mean of the temperature measures during this segment of days. So, if we want to analyze the average temperature from day $x$ to day $y$, we calculate it as $\frac{\sum \limits_{i = x}^{y} a_i}{y - x + 1}$ (note that division is performed without any rounding). The heat intensity value is the maximum of average temperatures over all segments of not less than $k$ consecutive days. For example, if analyzing the measures $[3, 4, 1, 2]$ and $k = 3$, we are interested in segments $[3, 4, 1]$, $[4, 1, 2]$ and $[3, 4, 1, 2]$ (we want to find the maximum value of average temperature over these segments). You have been hired by Berland State University to write a program that would compute the heat intensity value of a given period of days. Are you up to this task? -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 5000$) โ€” the number of days in the given period, and the minimum number of days in a segment we consider when calculating heat intensity value, respectively. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 5000$) โ€” the temperature measures during given $n$ days. -----Output----- Print one real number โ€” the heat intensity value, i. e., the maximum of average temperatures over all segments of not less than $k$ consecutive days. Your answer will be considered correct if the following condition holds: $|res - res_0| < 10^{-6}$, where $res$ is your answer, and $res_0$ is the answer given by the jury's solution. -----Example----- Input 4 3 3 4 1 2 Output 2.666666666666667 The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n,k=list(map(int,input().split())) a=list(map(int,input().split())) prefix=[a[0]] for i in range(1,n): prefix.append(prefix[-1]+a[i]) ans=[] for i in range(n): for j in range(i+k-1,n): if(i==0): ans.append(prefix[j]/(j-i+1)) else: ans.append((prefix[j]-prefix[i-1])/(j-i+1)) print(max(ans)) ```
vfc_26271
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1003/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "4 3\n3 4 1 2\n", "output": "2.666666666666667\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 1\n3 10 9 10 6\n", "output": "10.000000000000000\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 2\n7 3 3 1 8\n", "output": "5.000000000000000\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "5 3\n1 7 6 9 1\n", "output": "7.333333333333333\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4393
Solve the following coding problem using the programming language python: Polycarp loves ciphers. He has invented his own cipher called repeating. Repeating cipher is used for strings. To encrypt the string $s=s_{1}s_{2} \dots s_{m}$ ($1 \le m \le 10$), Polycarp uses the following algorithm: he writes down $s_1$ ones, he writes down $s_2$ twice, he writes down $s_3$ three times, ... he writes down $s_m$ $m$ times. For example, if $s$="bab" the process is: "b" $\to$ "baa" $\to$ "baabbb". So the encrypted $s$="bab" is "baabbb". Given string $t$ โ€” the result of encryption of some string $s$. Your task is to decrypt it, i. e. find the string $s$. -----Input----- The first line contains integer $n$ ($1 \le n \le 55$) โ€” the length of the encrypted string. The second line of the input contains $t$ โ€” the result of encryption of some string $s$. It contains only lowercase Latin letters. The length of $t$ is exactly $n$. It is guaranteed that the answer to the test exists. -----Output----- Print such string $s$ that after encryption it equals $t$. -----Examples----- Input 6 baabbb Output bab Input 10 ooopppssss Output oops Input 1 z Output z The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python n = int(input()) s = input() i = 0 d = 1 t = [] while i < n: t.append(s[i]) i += d d += 1 print(''.join(t)) ```
vfc_26279
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1095/A", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "6\nbaabbb\n", "output": "bab", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\nooopppssss\n", "output": "oops", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\nz\n", "output": "z", "type": "stdin_stdout" }, { "fn_name": null, "input": "3\nzww\n", "output": "zw", "type": "stdin_stdout" }, { "fn_name": null, "input": "55\ncooooonnnnttttteeeeeeeeeeeeessssssssttttttttttttttttttt\n", "output": "coonteestt", "type": "stdin_stdout" } ] }
apps
verifiable_code
4395
Solve the following coding problem using the programming language python: You have a garland consisting of $n$ lamps. Each lamp is colored red, green or blue. The color of the $i$-th lamp is $s_i$ ('R', 'G' and 'B' โ€” colors of lamps in the garland). You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is nice. A garland is called nice if any two lamps of the same color have distance divisible by three between them. I.e. if the obtained garland is $t$, then for each $i, j$ such that $t_i = t_j$ should be satisfied $|i-j|~ mod~ 3 = 0$. The value $|x|$ means absolute value of $x$, the operation $x~ mod~ y$ means remainder of $x$ when divided by $y$. For example, the following garlands are nice: "RGBRGBRG", "GB", "R", "GRBGRBG", "BRGBRGB". The following garlands are not nice: "RR", "RGBG". Among all ways to recolor the initial garland to make it nice you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) โ€” the number of lamps. The second line of the input contains the string $s$ consisting of $n$ characters 'R', 'G' and 'B' โ€” colors of lamps in the garland. -----Output----- In the first line of the output print one integer $r$ โ€” the minimum number of recolors needed to obtain a nice garland from the given one. In the second line of the output print one string $t$ of length $n$ โ€” a nice garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them. -----Examples----- Input 3 BRB Output 1 GRB Input 7 RGBGRBB Output 3 RGBRGBR The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python starts = ["RGB", "RBG", "BRG", "BGR", "GRB", "GBR"] N = int(input()) s = input() min_cost = float("inf") min_str = "" for k in starts: sp = (k * -(-N // 3))[:N] cost = sum(x != y for x, y in zip(s, sp)) if cost < min_cost: min_cost = cost min_str = sp print(min_cost) print(min_str) ```
vfc_26287
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1108/C", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "3\nBRB\n", "output": "1\nGRB\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "7\nRGBGRBB\n", "output": "3\nRGBRGBR\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "1\nB\n", "output": "0\nB\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "2\nBB\n", "output": "1\nRB\n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10\nGGBRGRBGGR\n", "output": "4\nRGBRGBRGBR\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4405
Solve the following coding problem using the programming language python: Polycarp has prepared $n$ competitive programming problems. The topic of the $i$-th problem is $a_i$, and some problems' topics may coincide. Polycarp has to host several thematic contests. All problems in each contest should have the same topic, and all contests should have pairwise distinct topics. He may not use all the problems. It is possible that there are no contests for some topics. Polycarp wants to host competitions on consecutive days, one contest per day. Polycarp wants to host a set of contests in such a way that: number of problems in each contest is exactly twice as much as in the previous contest (one day ago), the first contest can contain arbitrary number of problems; the total number of problems in all the contests should be maximized. Your task is to calculate the maximum number of problems in the set of thematic contests. Note, that you should not maximize the number of contests. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) โ€” the number of problems Polycarp has prepared. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) where $a_i$ is the topic of the $i$-th problem. -----Output----- Print one integer โ€” the maximum number of problems in the set of thematic contests. -----Examples----- Input 18 2 1 2 10 2 10 10 2 2 1 10 10 10 10 1 1 10 10 Output 14 Input 10 6 6 6 3 6 1000000000 3 3 6 6 Output 9 Input 3 1337 1337 1337 Output 3 -----Note----- In the first example the optimal sequence of contests is: $2$ problems of the topic $1$, $4$ problems of the topic $2$, $8$ problems of the topic $10$. In the second example the optimal sequence of contests is: $3$ problems of the topic $3$, $6$ problems of the topic $6$. In the third example you can take all the problems with the topic $1337$ (the number of such problems is $3$ so the answer is $3$) and host a single contest. The input will be stdin and you should print your solution to stdout Now solve the problem and return the code.
```python # import collections, atexit, math, sys, bisect sys.setrecursionlimit(1000000) def getIntList(): return list(map(int, input().split())) try : #raise ModuleNotFoundError import numpy def dprint(*args, **kwargs): #print(*args, **kwargs, file=sys.stderr) # in python 3.4 **kwargs is invalid??? print(*args, file=sys.stderr) dprint('debug mode') except Exception: def dprint(*args, **kwargs): pass inId = 0 outId = 0 if inId>0: dprint('use input', inId) sys.stdin = open('input'+ str(inId) + '.txt', 'r') #ๆ ‡ๅ‡†่พ“ๅ‡บ้‡ๅฎšๅ‘่‡ณๆ–‡ไปถ if outId>0: dprint('use output', outId) sys.stdout = open('stdout'+ str(outId) + '.txt', 'w') #ๆ ‡ๅ‡†่พ“ๅ‡บ้‡ๅฎšๅ‘่‡ณๆ–‡ไปถ atexit.register(lambda :sys.stdout.close()) #idle ไธญไธไผšๆ‰ง่กŒ atexit N = getIntList() za = getIntList() cc = collections.Counter(za) zt = [] for x in cc: zt.append( cc[x] ) zt.sort( ) re = zt[-1] def findmid(l,r, e): if l>= len(zt): return -1 if e<=zt[l]: return l; if e>zt[r]: return -1 while l+1 <r: mid = (l+r)//2 if zt[mid] < e: l = mid else: r = mid return r for first in range(1, re//2 + 1): nowr = 0 t = first ind = -1 while 1: ind = findmid(ind+1,len(zt)-1, t) if ind<0: break nowr += t t*=2 re = max(re, nowr) print(re) ```
vfc_26327
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1077/E", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "18\n2 1 2 10 2 10 10 2 2 1 10 10 10 10 1 1 10 10\n", "output": "14\n", "type": "stdin_stdout" } ] }
apps
verifiable_code
4406
Solve the following coding problem using the programming language python: The only difference between easy and hard versions are constraints on $n$ and $k$. You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most $k$ most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals $0$). Each conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend. You (suddenly!) have the ability to see the future. You know that during the day you will receive $n$ messages, the $i$-th message will be received from the friend with ID $id_i$ ($1 \le id_i \le 10^9$). If you receive a message from $id_i$ in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages. Otherwise (i.e. if there is no conversation with $id_i$ on the screen): Firstly, if the number of conversations displayed on the screen is $k$, the last conversation (which has the position $k$) is removed from the screen. Now the number of conversations on the screen is guaranteed to be less than $k$ and the conversation with the friend $id_i$ is not displayed on the screen. The conversation with the friend $id_i$ appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down. Your task is to find the list of conversations (in the order they are displayed on the screen) after processing all $n$ messages. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 200)$ โ€” the number of messages and the number of conversations your smartphone can show. The second line of the input contains $n$ integers $id_1, id_2, \dots, id_n$ ($1 \le id_i \le 10^9$), where $id_i$ is the ID of the friend which sends you the $i$-th message. -----Output----- In the first line of the output print one integer $m$ ($1 \le m \le min(n, k)$) โ€” the number of conversations shown after receiving all $n$ messages. In the second line print $m$ integers $ids_1, ids_2, \dots, ids_m$, where $ids_i$ should be equal to the ID of the friend corresponding to the conversation displayed on the position $i$ after receiving all $n$ messages. -----Examples----- Input 7 2 1 2 3 2 1 3 2 Output 2 2 1 Input 10 4 2 3 3 1 1 2 1 2 3 3 Output 3 1 3 2 -----Note----- In the first example the list of conversations will change in the following way (in order from the first to last message): $[]$; $[1]$; $[2, 1]$; $[3, 2]$; $[3, 2]$; $[1, 3]$; $[1, 3]$; $[2, 1]$. In the second example the list of conversations will change in the following way: $[]$; $[2]$; $[3, 2]$; $[3, 2]$; $[1, 3, 2]$; and then the list will not change till the end. 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 deque n, k = map(int, input().split()) A = list(map(int, input().split())) B = deque() B.append(A[0]) for i in range(1, n): if A[i] not in B: if len(B) == k: B.popleft() B.append(A[i]) else: pass print(len(B)) print(*list(B)[::-1]) ```
vfc_26331
{ "difficulty": "introductory", "memory_limit": null, "memory_limit_bytes": null, "problem_url": "https://codeforces.com/problemset/problem/1234/B1", "time_limit": "None" }
{ "language": "python", "test_cases": [ { "fn_name": null, "input": "7 2\n1 2 3 2 1 3 2\n", "output": "2\n2 1 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "10 4\n2 3 3 1 1 2 1 2 3 3\n", "output": "3\n1 3 2 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "3 4\n1 1 2\n", "output": "2\n2 1 \n", "type": "stdin_stdout" }, { "fn_name": null, "input": "9 2\n1 2 3 4 5 6 7 8 9\n", "output": "2\n9 8 \n", "type": "stdin_stdout" } ] }