message
stringlengths
2
65.1k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
0
108k
cluster
float64
14
14
__index_level_0__
int64
0
217k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Β«Next pleaseΒ», β€” the princess called and cast an estimating glance at the next groom. The princess intends to choose the most worthy groom, this is, the richest one. Whenever she sees a groom who is more rich than each of the previous ones, she says a measured Β«Oh...Β». Whenever the groom is richer than all previous ones added together, she exclaims Β«Wow!Β» (no Β«Oh...Β» in this case). At the sight of the first groom the princess stays calm and says nothing. The fortune of each groom is described with an integer between 1 and 50000. You know that during the day the princess saw n grooms, said Β«Oh...Β» exactly a times and exclaimed Β«Wow!Β» exactly b times. Your task is to output a sequence of n integers t1, t2, ..., tn, where ti describes the fortune of i-th groom. If several sequences are possible, output any of them. If no sequence exists that would satisfy all the requirements, output a single number -1. Input The only line of input data contains three integer numbers n, a and b (1 ≀ n ≀ 100, 0 ≀ a, b ≀ 15, n > a + b), separated with single spaces. Output Output any sequence of integers t1, t2, ..., tn, where ti (1 ≀ ti ≀ 50000) is the fortune of i-th groom, that satisfies the given constraints. If no sequence exists that would satisfy all the requirements, output a single number -1. Examples Input 10 2 3 Output 5 1 3 6 16 35 46 4 200 99 Input 5 0 0 Output 10 10 6 6 5 Note Let's have a closer look at the answer for the first sample test. * The princess said Β«Oh...Β» (highlighted in bold): 5 1 3 6 16 35 46 4 200 99. * The princess exclaimed Β«Wow!Β» (highlighted in bold): 5 1 3 6 16 35 46 4 200 99. Submitted Solution: ``` n,a,b=map(int,input().split()) x=1 buf=[1] sum=1 if a+b==n or b==0: print(-1) exit() for i in range(a+b): if b: x=sum+1 buf.append(x) sum+=x b-=1 else: x+=1 sum+=x buf.append(x) a-=1 for i in range(n-a-b-1): buf.append(x) if buf[-1]>50000: print(-1) else: print(buf[0],end='') for i in range(1,len(buf)): print('',buf[i],end='') print('') ```
instruction
0
36,328
14
72,656
No
output
1
36,328
14
72,657
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Smart Beaver has recently designed and built an innovative nanotechnologic all-purpose beaver mass shaving machine, "Beavershave 5000". Beavershave 5000 can shave beavers by families! How does it work? Very easily! There are n beavers, each of them has a unique id from 1 to n. Consider a permutation a1, a2, ..., an of n these beavers. Beavershave 5000 needs one session to shave beavers with ids from x to y (inclusive) if and only if there are such indices i1 < i2 < ... < ik, that ai1 = x, ai2 = x + 1, ..., aik - 1 = y - 1, aik = y. And that is really convenient. For example, it needs one session to shave a permutation of beavers 1, 2, 3, ..., n. If we can't shave beavers from x to y in one session, then we can split these beavers into groups [x, p1], [p1 + 1, p2], ..., [pm + 1, y] (x ≀ p1 < p2 < ... < pm < y), in such a way that the machine can shave beavers in each group in one session. But then Beavershave 5000 needs m + 1 working sessions to shave beavers from x to y. All beavers are restless and they keep trying to swap. So if we consider the problem more formally, we can consider queries of two types: * what is the minimum number of sessions that Beavershave 5000 needs to shave beavers with ids from x to y, inclusive? * two beavers on positions x and y (the beavers ax and ay) swapped. You can assume that any beaver can be shaved any number of times. Input The first line contains integer n β€” the total number of beavers, 2 ≀ n. The second line contains n space-separated integers β€” the initial beaver permutation. The third line contains integer q β€” the number of queries, 1 ≀ q ≀ 105. The next q lines contain the queries. Each query i looks as pi xi yi, where pi is the query type (1 is to shave beavers from xi to yi, inclusive, 2 is to swap beavers on positions xi and yi). All queries meet the condition: 1 ≀ xi < yi ≀ n. * to get 30 points, you need to solve the problem with constraints: n ≀ 100 (subproblem B1); * to get 100 points, you need to solve the problem with constraints: n ≀ 3Β·105 (subproblems B1+B2). Note that the number of queries q is limited 1 ≀ q ≀ 105 in both subproblem B1 and subproblem B2. Output For each query with pi = 1, print the minimum number of Beavershave 5000 sessions. Examples Input 5 1 3 4 2 5 6 1 1 5 1 3 4 2 2 3 1 1 5 2 1 5 1 1 5 Output 2 1 3 5 Submitted Solution: ``` import sys n=int(input()) B=sys.stdin.readline().split() for i in range(n): B[i]=int(B[i]) q=int(input()) for i in range(q): s=sys.stdin.readline().split() if(s[0]=='2'): m=int(s[1])-1 mm=int(s[2])-1 t=B[m] B[m]=B[mm] B[mm]=t else: m=B.index(int(s[1])) mm=B.index(int(s[2])) p=min(m,mm) pp=max(m,mm) session=0 j=p while(j<=pp): c=j+1 while(c<=pp and B[c]>B[c-1]): c+=1 j=c session+=1 print(session) ```
instruction
0
36,426
14
72,852
No
output
1
36,426
14
72,853
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Smart Beaver has recently designed and built an innovative nanotechnologic all-purpose beaver mass shaving machine, "Beavershave 5000". Beavershave 5000 can shave beavers by families! How does it work? Very easily! There are n beavers, each of them has a unique id from 1 to n. Consider a permutation a1, a2, ..., an of n these beavers. Beavershave 5000 needs one session to shave beavers with ids from x to y (inclusive) if and only if there are such indices i1 < i2 < ... < ik, that ai1 = x, ai2 = x + 1, ..., aik - 1 = y - 1, aik = y. And that is really convenient. For example, it needs one session to shave a permutation of beavers 1, 2, 3, ..., n. If we can't shave beavers from x to y in one session, then we can split these beavers into groups [x, p1], [p1 + 1, p2], ..., [pm + 1, y] (x ≀ p1 < p2 < ... < pm < y), in such a way that the machine can shave beavers in each group in one session. But then Beavershave 5000 needs m + 1 working sessions to shave beavers from x to y. All beavers are restless and they keep trying to swap. So if we consider the problem more formally, we can consider queries of two types: * what is the minimum number of sessions that Beavershave 5000 needs to shave beavers with ids from x to y, inclusive? * two beavers on positions x and y (the beavers ax and ay) swapped. You can assume that any beaver can be shaved any number of times. Input The first line contains integer n β€” the total number of beavers, 2 ≀ n. The second line contains n space-separated integers β€” the initial beaver permutation. The third line contains integer q β€” the number of queries, 1 ≀ q ≀ 105. The next q lines contain the queries. Each query i looks as pi xi yi, where pi is the query type (1 is to shave beavers from xi to yi, inclusive, 2 is to swap beavers on positions xi and yi). All queries meet the condition: 1 ≀ xi < yi ≀ n. * to get 30 points, you need to solve the problem with constraints: n ≀ 100 (subproblem B1); * to get 100 points, you need to solve the problem with constraints: n ≀ 3Β·105 (subproblems B1+B2). Note that the number of queries q is limited 1 ≀ q ≀ 105 in both subproblem B1 and subproblem B2. Output For each query with pi = 1, print the minimum number of Beavershave 5000 sessions. Examples Input 5 1 3 4 2 5 6 1 1 5 1 3 4 2 2 3 1 1 5 2 1 5 1 1 5 Output 2 1 3 5 Submitted Solution: ``` import sys n=int(input()) B=sys.stdin.readline().split() q=int(input()) for i in range(q): s=sys.stdin.readline().split() if(s[0]=='2'): m=int(s[1])-1 mm=int(s[2])-1 t=B[m] B[m]=B[mm] B[mm]=t else: m=B.index(s[1]) mm=B.index(s[2]) p=min(m,mm) pp=max(m,mm) session=0 j=p while(j<=pp): c=j+1 while(c<=pp and B[c]>B[c-1]): c+=1 j=c session+=1 print(session) ```
instruction
0
36,427
14
72,854
No
output
1
36,427
14
72,855
Provide tags and a correct Python 3 solution for this coding contest problem. Valera has got n domino pieces in a row. Each piece consists of two halves β€” the upper one and the lower one. Each of the halves contains a number from 1 to 6. Valera loves even integers very much, so he wants the sum of the numbers on the upper halves and the sum of the numbers on the lower halves to be even. To do that, Valera can rotate the dominoes by 180 degrees. After the rotation the upper and the lower halves swap places. This action takes one second. Help Valera find out the minimum time he must spend rotating dominoes to make his wish come true. Input The first line contains integer n (1 ≀ n ≀ 100), denoting the number of dominoes Valera has. Next n lines contain two space-separated integers xi, yi (1 ≀ xi, yi ≀ 6). Number xi is initially written on the upper half of the i-th domino, yi is initially written on the lower half. Output Print a single number β€” the minimum required number of seconds. If Valera can't do the task in any time, print - 1. Examples Input 2 4 2 6 4 Output 0 Input 1 2 3 Output -1 Input 3 1 4 2 3 4 4 Output 1 Note In the first test case the sum of the numbers on the upper halves equals 10 and the sum of the numbers on the lower halves equals 6. Both numbers are even, so Valera doesn't required to do anything. In the second sample Valera has only one piece of domino. It is written 3 on the one of its halves, therefore one of the sums will always be odd. In the third case Valera can rotate the first piece, and after that the sum on the upper halves will be equal to 10, and the sum on the lower halves will be equal to 8.
instruction
0
36,428
14
72,856
Tags: implementation, math Correct Solution: ``` n=int(input()) l=[] t=[] for _ in range(n): x,y=[int(x) for x in input().split()] l.append(x) t.append(y) if sum(l)%2==0 and sum(t)%2==0: print(0) exit() elif sum(l)%2!=sum(t)%2: print(-1) exit() v=0 for i in range(len(l)): if (l[i]%2==0 and t[i]%2!=0) or (l[i]%2!=0 and t[i]%2==0): v=1 break if v==0: print(-1) else: print(1) ```
output
1
36,428
14
72,857
Provide tags and a correct Python 3 solution for this coding contest problem. Valera has got n domino pieces in a row. Each piece consists of two halves β€” the upper one and the lower one. Each of the halves contains a number from 1 to 6. Valera loves even integers very much, so he wants the sum of the numbers on the upper halves and the sum of the numbers on the lower halves to be even. To do that, Valera can rotate the dominoes by 180 degrees. After the rotation the upper and the lower halves swap places. This action takes one second. Help Valera find out the minimum time he must spend rotating dominoes to make his wish come true. Input The first line contains integer n (1 ≀ n ≀ 100), denoting the number of dominoes Valera has. Next n lines contain two space-separated integers xi, yi (1 ≀ xi, yi ≀ 6). Number xi is initially written on the upper half of the i-th domino, yi is initially written on the lower half. Output Print a single number β€” the minimum required number of seconds. If Valera can't do the task in any time, print - 1. Examples Input 2 4 2 6 4 Output 0 Input 1 2 3 Output -1 Input 3 1 4 2 3 4 4 Output 1 Note In the first test case the sum of the numbers on the upper halves equals 10 and the sum of the numbers on the lower halves equals 6. Both numbers are even, so Valera doesn't required to do anything. In the second sample Valera has only one piece of domino. It is written 3 on the one of its halves, therefore one of the sums will always be odd. In the third case Valera can rotate the first piece, and after that the sum on the upper halves will be equal to 10, and the sum on the lower halves will be equal to 8.
instruction
0
36,429
14
72,858
Tags: implementation, math Correct Solution: ``` import sys,math,bisect inf = float('inf') mod = 998244353 "========================================" def lcm(a,b): return int((a/math.gcd(a,b))*b) def gcd(a,b): return int(math.gcd(a,b)) def tobinary(n): return bin(n)[2:] def binarySearch(a,x): i = bisect.bisect_left(a,x) if i!=len(a) and a[i]==x: return i else: return -1 def lowerBound(a, x): i = bisect.bisect_left(a, x) if i: return (i-1) else: return -1 def upperBound(a,x): i = bisect.bisect_right(a,x) if i!= len(a)+1 and a[i-1]==x: return (i-1) else: return -1 def primesInRange(n): ans = [] prime = [True for i in range(n+1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n+1, p): prime[i] = False p += 1 for p in range(2, n+1): if prime[p]: ans.append(p) return ans def primeFactors(n): factors = [] while n % 2 == 0: factors.append(2) n = n // 2 for i in range(3,int(math.sqrt(n))+1,2): while n % i== 0: factors.append(i) n = n // i if n > 2: factors.append(n) return factors "=========================================" """ n = int(input()) n,k = map(int,input().split()) arr = list(map(int,input().split())) """ from collections import deque,defaultdict,Counter import heapq for _ in range(1): n=int(input()) upper = [] lower = [] for i in range(n): i=list(map(int,input().split())) upper.append(i[0]) lower.append(i[1]) sumup = sum(upper) sumlow = sum(lower) if sumup%2==0 and sumlow%2==0: print(0) else: cnt=0 flag=False for i in range(n): if upper[i]%2!=lower[i]%2: sumup-=upper[i] sumup+=lower[i] sumlow-=lower[i] sumlow+=upper[i] upper[i],lower[i]=lower[i],upper[i] cnt+=1 if sumup%2==0 and sumlow%2==0: flag=True break if n==1: if upper[0]%2==1 or lower[0]%2==1: print(-1) else: print(0) elif not flag: print(-1) else: print(cnt) ```
output
1
36,429
14
72,859
Provide tags and a correct Python 3 solution for this coding contest problem. Valera has got n domino pieces in a row. Each piece consists of two halves β€” the upper one and the lower one. Each of the halves contains a number from 1 to 6. Valera loves even integers very much, so he wants the sum of the numbers on the upper halves and the sum of the numbers on the lower halves to be even. To do that, Valera can rotate the dominoes by 180 degrees. After the rotation the upper and the lower halves swap places. This action takes one second. Help Valera find out the minimum time he must spend rotating dominoes to make his wish come true. Input The first line contains integer n (1 ≀ n ≀ 100), denoting the number of dominoes Valera has. Next n lines contain two space-separated integers xi, yi (1 ≀ xi, yi ≀ 6). Number xi is initially written on the upper half of the i-th domino, yi is initially written on the lower half. Output Print a single number β€” the minimum required number of seconds. If Valera can't do the task in any time, print - 1. Examples Input 2 4 2 6 4 Output 0 Input 1 2 3 Output -1 Input 3 1 4 2 3 4 4 Output 1 Note In the first test case the sum of the numbers on the upper halves equals 10 and the sum of the numbers on the lower halves equals 6. Both numbers are even, so Valera doesn't required to do anything. In the second sample Valera has only one piece of domino. It is written 3 on the one of its halves, therefore one of the sums will always be odd. In the third case Valera can rotate the first piece, and after that the sum on the upper halves will be equal to 10, and the sum on the lower halves will be equal to 8.
instruction
0
36,431
14
72,862
Tags: implementation, math Correct Solution: ``` def sum(T): s=0 for i in range(len(T)): s+=T[i] return s td=[] bd=[] n=int(input()) for i in range(n): t,b=map(int,input().split()) td.append(t) bd.append(b) ts=sum(td) bs=sum(bd) if ts%2==0 and bs%2==0: print("0") else: v=False for i in range(len(td)): s=0 td1=[] bd1=[] if td[i]==bd[i]: continue for k in range(0,i): td1.append(td[k]) bd1.append(bd[k]) td1.append(bd[i]) bd1.append(td[i]) for v in range(i+1,len(td)): td1.append(td[v]) bd1.append(bd[v]) ts=sum(td1) bs=sum(bd1) s+=1 if ts%2==0 and bs%2==0: v=True break if v==True: print(s) else: print("-1") ```
output
1
36,431
14
72,863
Provide tags and a correct Python 3 solution for this coding contest problem. Valera has got n domino pieces in a row. Each piece consists of two halves β€” the upper one and the lower one. Each of the halves contains a number from 1 to 6. Valera loves even integers very much, so he wants the sum of the numbers on the upper halves and the sum of the numbers on the lower halves to be even. To do that, Valera can rotate the dominoes by 180 degrees. After the rotation the upper and the lower halves swap places. This action takes one second. Help Valera find out the minimum time he must spend rotating dominoes to make his wish come true. Input The first line contains integer n (1 ≀ n ≀ 100), denoting the number of dominoes Valera has. Next n lines contain two space-separated integers xi, yi (1 ≀ xi, yi ≀ 6). Number xi is initially written on the upper half of the i-th domino, yi is initially written on the lower half. Output Print a single number β€” the minimum required number of seconds. If Valera can't do the task in any time, print - 1. Examples Input 2 4 2 6 4 Output 0 Input 1 2 3 Output -1 Input 3 1 4 2 3 4 4 Output 1 Note In the first test case the sum of the numbers on the upper halves equals 10 and the sum of the numbers on the lower halves equals 6. Both numbers are even, so Valera doesn't required to do anything. In the second sample Valera has only one piece of domino. It is written 3 on the one of its halves, therefore one of the sums will always be odd. In the third case Valera can rotate the first piece, and after that the sum on the upper halves will be equal to 10, and the sum on the lower halves will be equal to 8.
instruction
0
36,434
14
72,868
Tags: implementation, math Correct Solution: ``` n = int(input()) x = [[], []] def odd_count(l: list) -> int: count = 0 for j in range(n): if l[j] % 2 != 0: count += 1 return count for i in range(n): a, b = map(int, input().split()) x[0].append(a) x[1].append(b) o0 = odd_count(x[0]) e0 = len(x[0]) - o0 o1 = odd_count(x[1]) e1 = len(x[1]) - o1 if o0 % 2 == 0 and o1 % 2 == 0: print(0) elif abs(o0 - o1) % 2 == 0 and (e0 > 0 or e1 > 0): count = 0 for i in range(n): if (x[0][i] + x[1][i]) % 2 == 0: count +=1 if count == len(x[0]): print(-1) else: print(1) else: print(-1) ```
output
1
36,434
14
72,869
Provide tags and a correct Python 3 solution for this coding contest problem. Valera has got n domino pieces in a row. Each piece consists of two halves β€” the upper one and the lower one. Each of the halves contains a number from 1 to 6. Valera loves even integers very much, so he wants the sum of the numbers on the upper halves and the sum of the numbers on the lower halves to be even. To do that, Valera can rotate the dominoes by 180 degrees. After the rotation the upper and the lower halves swap places. This action takes one second. Help Valera find out the minimum time he must spend rotating dominoes to make his wish come true. Input The first line contains integer n (1 ≀ n ≀ 100), denoting the number of dominoes Valera has. Next n lines contain two space-separated integers xi, yi (1 ≀ xi, yi ≀ 6). Number xi is initially written on the upper half of the i-th domino, yi is initially written on the lower half. Output Print a single number β€” the minimum required number of seconds. If Valera can't do the task in any time, print - 1. Examples Input 2 4 2 6 4 Output 0 Input 1 2 3 Output -1 Input 3 1 4 2 3 4 4 Output 1 Note In the first test case the sum of the numbers on the upper halves equals 10 and the sum of the numbers on the lower halves equals 6. Both numbers are even, so Valera doesn't required to do anything. In the second sample Valera has only one piece of domino. It is written 3 on the one of its halves, therefore one of the sums will always be odd. In the third case Valera can rotate the first piece, and after that the sum on the upper halves will be equal to 10, and the sum on the lower halves will be equal to 8.
instruction
0
36,435
14
72,870
Tags: implementation, math Correct Solution: ``` # odd + odd = even + even = even # odd + even = even + odd = odd n = int(input().strip()) a, b = [0]*n, [0]*n sumA, sumB = 0, 0 for i in range(n): a[i], b[i] = map(int, input().strip().split()) sumA += a[i] sumB += b[i] if (sumA%2 or sumB%2) == 0: # if both are already even print(0) elif (sumA%2)^(sumB%2): # if either one is even print(-1) else: # find a swappable pair if any((a[i]%2)^(b[i]%2) for i in range(n)): print(1) else: print(-1) ```
output
1
36,435
14
72,871
Provide tags and a correct Python 3 solution for this coding contest problem. One day Sasha visited the farmer 2D and his famous magnetic farm. On this farm, the crop grows due to the influence of a special magnetic field. Maintaining of the magnetic field is provided by n machines, and the power of the i-th machine is a_i. This year 2D decided to cultivate a new culture, but what exactly he didn't say. For the successful growth of the new culture, it is necessary to slightly change the powers of the machines. 2D can at most once choose an arbitrary integer x, then choose one machine and reduce the power of its machine by x times, and at the same time increase the power of one another machine by x times (powers of all the machines must stay positive integers). Note that he may not do that if he wants. More formally, 2D can choose two such indices i and j, and one integer x such that x is a divisor of a_i, and change powers as following: a_i = (a_i)/(x), a_j = a_j β‹… x Sasha is very curious, that's why he wants to calculate the minimum total power the farmer can reach. There are too many machines, and Sasha can't cope with computations, help him! Input The first line contains one integer n (2 ≀ n ≀ 5 β‹… 10^4) β€” the number of machines. The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 100) β€” the powers of the machines. Output Print one integer β€” minimum total power. Examples Input 5 1 2 3 4 5 Output 14 Input 4 4 2 4 4 Output 14 Input 5 2 4 2 3 7 Output 18 Note In the first example, the farmer can reduce the power of the 4-th machine by 2 times, and increase the power of the 1-st machine by 2 times, then the powers will be: [2, 2, 3, 2, 5]. In the second example, the farmer can reduce the power of the 3-rd machine by 2 times, and increase the power of the 2-nd machine by 2 times. At the same time, the farmer can leave is be as it is and the total power won't change. In the third example, it is optimal to leave it be as it is.
instruction
0
36,895
14
73,790
Tags: greedy, number theory Correct Solution: ``` n = int(input()) a = [int(s) for s in input().split()] summa = sum(a) mini = summa y = min(a) y_idx = a.index(y) z_min = 0 for z in range(len(a)): if z != y_idx: for x in range(1, 101): if a[z] % x == 0: k = summa - (a[z]+y) + (a[z] // x + y*x) if k < mini: mini = k print(mini) ```
output
1
36,895
14
73,791
Provide tags and a correct Python 3 solution for this coding contest problem. One day Sasha visited the farmer 2D and his famous magnetic farm. On this farm, the crop grows due to the influence of a special magnetic field. Maintaining of the magnetic field is provided by n machines, and the power of the i-th machine is a_i. This year 2D decided to cultivate a new culture, but what exactly he didn't say. For the successful growth of the new culture, it is necessary to slightly change the powers of the machines. 2D can at most once choose an arbitrary integer x, then choose one machine and reduce the power of its machine by x times, and at the same time increase the power of one another machine by x times (powers of all the machines must stay positive integers). Note that he may not do that if he wants. More formally, 2D can choose two such indices i and j, and one integer x such that x is a divisor of a_i, and change powers as following: a_i = (a_i)/(x), a_j = a_j β‹… x Sasha is very curious, that's why he wants to calculate the minimum total power the farmer can reach. There are too many machines, and Sasha can't cope with computations, help him! Input The first line contains one integer n (2 ≀ n ≀ 5 β‹… 10^4) β€” the number of machines. The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 100) β€” the powers of the machines. Output Print one integer β€” minimum total power. Examples Input 5 1 2 3 4 5 Output 14 Input 4 4 2 4 4 Output 14 Input 5 2 4 2 3 7 Output 18 Note In the first example, the farmer can reduce the power of the 4-th machine by 2 times, and increase the power of the 1-st machine by 2 times, then the powers will be: [2, 2, 3, 2, 5]. In the second example, the farmer can reduce the power of the 3-rd machine by 2 times, and increase the power of the 2-nd machine by 2 times. At the same time, the farmer can leave is be as it is and the total power won't change. In the third example, it is optimal to leave it be as it is.
instruction
0
36,896
14
73,792
Tags: greedy, number theory Correct Solution: ``` import sys input_=lambda: sys.stdin.readline().strip("\r\n") from math import gcd sa=lambda :input_() sb=lambda:int(input_()) sc=lambda:input_().split() sd=lambda:list(map(int,input_().split())) se=lambda:float(input_()) sf=lambda:list(input_()) flsh=lambda: sys.stdout.flush() mod=10**9+7 def hnbhai(): n=sb() a=sd() b=list(set(a)) sum_=sum(a) min_=min(a) minus=0 for i in b: fact=2 while(i//fact)>min_: if i%fact==0: temp=i//fact+min_*fact-i-min_ minus=min(minus,temp) fact+=1 print(sum_+minus) for _ in range(1): hnbhai() ```
output
1
36,896
14
73,793
Provide tags and a correct Python 3 solution for this coding contest problem. One day Sasha visited the farmer 2D and his famous magnetic farm. On this farm, the crop grows due to the influence of a special magnetic field. Maintaining of the magnetic field is provided by n machines, and the power of the i-th machine is a_i. This year 2D decided to cultivate a new culture, but what exactly he didn't say. For the successful growth of the new culture, it is necessary to slightly change the powers of the machines. 2D can at most once choose an arbitrary integer x, then choose one machine and reduce the power of its machine by x times, and at the same time increase the power of one another machine by x times (powers of all the machines must stay positive integers). Note that he may not do that if he wants. More formally, 2D can choose two such indices i and j, and one integer x such that x is a divisor of a_i, and change powers as following: a_i = (a_i)/(x), a_j = a_j β‹… x Sasha is very curious, that's why he wants to calculate the minimum total power the farmer can reach. There are too many machines, and Sasha can't cope with computations, help him! Input The first line contains one integer n (2 ≀ n ≀ 5 β‹… 10^4) β€” the number of machines. The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 100) β€” the powers of the machines. Output Print one integer β€” minimum total power. Examples Input 5 1 2 3 4 5 Output 14 Input 4 4 2 4 4 Output 14 Input 5 2 4 2 3 7 Output 18 Note In the first example, the farmer can reduce the power of the 4-th machine by 2 times, and increase the power of the 1-st machine by 2 times, then the powers will be: [2, 2, 3, 2, 5]. In the second example, the farmer can reduce the power of the 3-rd machine by 2 times, and increase the power of the 2-nd machine by 2 times. At the same time, the farmer can leave is be as it is and the total power won't change. In the third example, it is optimal to leave it be as it is.
instruction
0
36,897
14
73,794
Tags: greedy, number theory Correct Solution: ``` def getMin(i): a = (pref[i - 1] if i - 1 >= 0 else 10**10) b = (suf[i + 1] if i + 1 < n else 10**10) return min(a, b) n = int(input()) v = [int(i) for i in input().split()] pref = [0] * n suf = [0] * n pref[0] = v[0] suf[n - 1] = v[n - 1] j = n - 2 for i in range(1, n): pref[i] = min(pref[i - 1], v[i]) suf[j] = min(suf[j + 1], v[j]) j -= 1 tot = sum(v) ans = tot for i in range(n): div = 1 while div * div <= v[i]: if v[i] % div == 0: mn = getMin(i) ans = min(ans, tot - v[i] - mn + (v[i] / div) + mn * div) div += 1 print(int(ans)) ```
output
1
36,897
14
73,795
Provide tags and a correct Python 3 solution for this coding contest problem. One day Sasha visited the farmer 2D and his famous magnetic farm. On this farm, the crop grows due to the influence of a special magnetic field. Maintaining of the magnetic field is provided by n machines, and the power of the i-th machine is a_i. This year 2D decided to cultivate a new culture, but what exactly he didn't say. For the successful growth of the new culture, it is necessary to slightly change the powers of the machines. 2D can at most once choose an arbitrary integer x, then choose one machine and reduce the power of its machine by x times, and at the same time increase the power of one another machine by x times (powers of all the machines must stay positive integers). Note that he may not do that if he wants. More formally, 2D can choose two such indices i and j, and one integer x such that x is a divisor of a_i, and change powers as following: a_i = (a_i)/(x), a_j = a_j β‹… x Sasha is very curious, that's why he wants to calculate the minimum total power the farmer can reach. There are too many machines, and Sasha can't cope with computations, help him! Input The first line contains one integer n (2 ≀ n ≀ 5 β‹… 10^4) β€” the number of machines. The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 100) β€” the powers of the machines. Output Print one integer β€” minimum total power. Examples Input 5 1 2 3 4 5 Output 14 Input 4 4 2 4 4 Output 14 Input 5 2 4 2 3 7 Output 18 Note In the first example, the farmer can reduce the power of the 4-th machine by 2 times, and increase the power of the 1-st machine by 2 times, then the powers will be: [2, 2, 3, 2, 5]. In the second example, the farmer can reduce the power of the 3-rd machine by 2 times, and increase the power of the 2-nd machine by 2 times. At the same time, the farmer can leave is be as it is and the total power won't change. In the third example, it is optimal to leave it be as it is.
instruction
0
36,898
14
73,796
Tags: greedy, number theory Correct Solution: ``` n = int(input()) arr = list(map(int, input().split())) d = dict() for el in arr: d[el] = d.get(el, 0) + 1 min_ans = 0 for el1 in d: for el2 in d: if el1 != el2: for x in range(2, 101): if el1 % x == 0: min_ans = min(min_ans, el1 // x + el2 * x - el1 - el2) print(sum(arr) + min_ans) ```
output
1
36,898
14
73,797
Provide tags and a correct Python 3 solution for this coding contest problem. One day Sasha visited the farmer 2D and his famous magnetic farm. On this farm, the crop grows due to the influence of a special magnetic field. Maintaining of the magnetic field is provided by n machines, and the power of the i-th machine is a_i. This year 2D decided to cultivate a new culture, but what exactly he didn't say. For the successful growth of the new culture, it is necessary to slightly change the powers of the machines. 2D can at most once choose an arbitrary integer x, then choose one machine and reduce the power of its machine by x times, and at the same time increase the power of one another machine by x times (powers of all the machines must stay positive integers). Note that he may not do that if he wants. More formally, 2D can choose two such indices i and j, and one integer x such that x is a divisor of a_i, and change powers as following: a_i = (a_i)/(x), a_j = a_j β‹… x Sasha is very curious, that's why he wants to calculate the minimum total power the farmer can reach. There are too many machines, and Sasha can't cope with computations, help him! Input The first line contains one integer n (2 ≀ n ≀ 5 β‹… 10^4) β€” the number of machines. The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 100) β€” the powers of the machines. Output Print one integer β€” minimum total power. Examples Input 5 1 2 3 4 5 Output 14 Input 4 4 2 4 4 Output 14 Input 5 2 4 2 3 7 Output 18 Note In the first example, the farmer can reduce the power of the 4-th machine by 2 times, and increase the power of the 1-st machine by 2 times, then the powers will be: [2, 2, 3, 2, 5]. In the second example, the farmer can reduce the power of the 3-rd machine by 2 times, and increase the power of the 2-nd machine by 2 times. At the same time, the farmer can leave is be as it is and the total power won't change. In the third example, it is optimal to leave it be as it is.
instruction
0
36,899
14
73,798
Tags: greedy, number theory Correct Solution: ``` n = int(input()) a = list(sorted(list(map(int, input().split())), reverse=True)) original_sum = sum(a) minimum_sum = original_sum min_v = a[-1] for i in range(n - 1): for j in range(2, a[i] - 1): if a[i] % j == 0: current_sum = original_sum - a[i] - min_v + a[i] // j + min_v * j minimum_sum = min(minimum_sum, current_sum) print(minimum_sum) ```
output
1
36,899
14
73,799
Provide tags and a correct Python 3 solution for this coding contest problem. One day Sasha visited the farmer 2D and his famous magnetic farm. On this farm, the crop grows due to the influence of a special magnetic field. Maintaining of the magnetic field is provided by n machines, and the power of the i-th machine is a_i. This year 2D decided to cultivate a new culture, but what exactly he didn't say. For the successful growth of the new culture, it is necessary to slightly change the powers of the machines. 2D can at most once choose an arbitrary integer x, then choose one machine and reduce the power of its machine by x times, and at the same time increase the power of one another machine by x times (powers of all the machines must stay positive integers). Note that he may not do that if he wants. More formally, 2D can choose two such indices i and j, and one integer x such that x is a divisor of a_i, and change powers as following: a_i = (a_i)/(x), a_j = a_j β‹… x Sasha is very curious, that's why he wants to calculate the minimum total power the farmer can reach. There are too many machines, and Sasha can't cope with computations, help him! Input The first line contains one integer n (2 ≀ n ≀ 5 β‹… 10^4) β€” the number of machines. The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 100) β€” the powers of the machines. Output Print one integer β€” minimum total power. Examples Input 5 1 2 3 4 5 Output 14 Input 4 4 2 4 4 Output 14 Input 5 2 4 2 3 7 Output 18 Note In the first example, the farmer can reduce the power of the 4-th machine by 2 times, and increase the power of the 1-st machine by 2 times, then the powers will be: [2, 2, 3, 2, 5]. In the second example, the farmer can reduce the power of the 3-rd machine by 2 times, and increase the power of the 2-nd machine by 2 times. At the same time, the farmer can leave is be as it is and the total power won't change. In the third example, it is optimal to leave it be as it is.
instruction
0
36,900
14
73,800
Tags: greedy, number theory Correct Solution: ``` import math n = int(input()) a = sorted(list(map(int, input().split()))) s = sum(a) ans = s for i in range(n - 1, 0, -1): cur = s - a[0] - a[i] for j in range(2, int(math.sqrt(a[i]) + 1)): if a[i] % j == 0: ans = min(ans, cur + a[0] * j + a[i] // j) print(ans) ```
output
1
36,900
14
73,801
Provide tags and a correct Python 3 solution for this coding contest problem. One day Sasha visited the farmer 2D and his famous magnetic farm. On this farm, the crop grows due to the influence of a special magnetic field. Maintaining of the magnetic field is provided by n machines, and the power of the i-th machine is a_i. This year 2D decided to cultivate a new culture, but what exactly he didn't say. For the successful growth of the new culture, it is necessary to slightly change the powers of the machines. 2D can at most once choose an arbitrary integer x, then choose one machine and reduce the power of its machine by x times, and at the same time increase the power of one another machine by x times (powers of all the machines must stay positive integers). Note that he may not do that if he wants. More formally, 2D can choose two such indices i and j, and one integer x such that x is a divisor of a_i, and change powers as following: a_i = (a_i)/(x), a_j = a_j β‹… x Sasha is very curious, that's why he wants to calculate the minimum total power the farmer can reach. There are too many machines, and Sasha can't cope with computations, help him! Input The first line contains one integer n (2 ≀ n ≀ 5 β‹… 10^4) β€” the number of machines. The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 100) β€” the powers of the machines. Output Print one integer β€” minimum total power. Examples Input 5 1 2 3 4 5 Output 14 Input 4 4 2 4 4 Output 14 Input 5 2 4 2 3 7 Output 18 Note In the first example, the farmer can reduce the power of the 4-th machine by 2 times, and increase the power of the 1-st machine by 2 times, then the powers will be: [2, 2, 3, 2, 5]. In the second example, the farmer can reduce the power of the 3-rd machine by 2 times, and increase the power of the 2-nd machine by 2 times. At the same time, the farmer can leave is be as it is and the total power won't change. In the third example, it is optimal to leave it be as it is.
instruction
0
36,901
14
73,802
Tags: greedy, number theory Correct Solution: ``` import math; def replace(a,b): if (a==b): return a+b; if a>b: atemp = a; a=b; b = atemp; #for i in range(math.ceil(math.sqrt(a*b)),0,-1): # if ((a*b)%i==0 and b%i==0): return i+(a*b)//i; x = a+b; for i in range(1,1+math.ceil(math.sqrt(b))): if (b%i==0): x = min(x,a*i+b//i); return x; n = int(input()); a = list(map(int,input().split())); if (n==1): print(sum(a)); else: #a = sorted(a); sum1=sum(a); #for i in range(n-1,n//2-1,-1): # for j in range(0,n//2+1): # sum1 = min(sum1,sum1+replace(a[j],a[i])-a[j]-a[i]); a = sorted(a); sum1 = sum(a); rq = 0; #for i in range(n): # for j in range(i,n): for i in range(n-1,-1,-1): #if (2*math.sqrt(a[i]*a[0])-a[i]-a[0] > rq): break; #for j in range(0,n//2+1): #for j in range(0,n//1000+1): rq = min(rq, replace(a[0],a[i])-a[0]-a[i]); print(sum1+rq); ```
output
1
36,901
14
73,803
Provide tags and a correct Python 3 solution for this coding contest problem. One day Sasha visited the farmer 2D and his famous magnetic farm. On this farm, the crop grows due to the influence of a special magnetic field. Maintaining of the magnetic field is provided by n machines, and the power of the i-th machine is a_i. This year 2D decided to cultivate a new culture, but what exactly he didn't say. For the successful growth of the new culture, it is necessary to slightly change the powers of the machines. 2D can at most once choose an arbitrary integer x, then choose one machine and reduce the power of its machine by x times, and at the same time increase the power of one another machine by x times (powers of all the machines must stay positive integers). Note that he may not do that if he wants. More formally, 2D can choose two such indices i and j, and one integer x such that x is a divisor of a_i, and change powers as following: a_i = (a_i)/(x), a_j = a_j β‹… x Sasha is very curious, that's why he wants to calculate the minimum total power the farmer can reach. There are too many machines, and Sasha can't cope with computations, help him! Input The first line contains one integer n (2 ≀ n ≀ 5 β‹… 10^4) β€” the number of machines. The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 100) β€” the powers of the machines. Output Print one integer β€” minimum total power. Examples Input 5 1 2 3 4 5 Output 14 Input 4 4 2 4 4 Output 14 Input 5 2 4 2 3 7 Output 18 Note In the first example, the farmer can reduce the power of the 4-th machine by 2 times, and increase the power of the 1-st machine by 2 times, then the powers will be: [2, 2, 3, 2, 5]. In the second example, the farmer can reduce the power of the 3-rd machine by 2 times, and increase the power of the 2-nd machine by 2 times. At the same time, the farmer can leave is be as it is and the total power won't change. In the third example, it is optimal to leave it be as it is.
instruction
0
36,902
14
73,804
Tags: greedy, number theory Correct Solution: ``` input() a=[*map(int,input().split())] m=min(a) print(sum(a)-max(y+m-y//x-m*x for y in set(a) for x in range(1,int(y**.5)+1)if y%x==0)) ```
output
1
36,902
14
73,805
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarpus has a hobby β€” he develops an unusual social network. His work is almost completed, and there is only one more module to implement β€” the module which determines friends. Oh yes, in this social network one won't have to add friends manually! Pairs of friends are deduced in the following way. Let's assume that user A sent user B a message at time t1, and user B sent user A a message at time t2. If 0 < t2 - t1 ≀ d, then user B's message was an answer to user A's one. Users A and B are considered to be friends if A answered at least one B's message or B answered at least one A's message. You are given the log of messages in chronological order and a number d. Find all pairs of users who will be considered to be friends. Input The first line of the input contains two integers n and d (1 ≀ n, d ≀ 1000). The next n lines contain the messages log. The i-th line contains one line of the log formatted as "Ai Bi ti" (without the quotes), which means that user Ai sent a message to user Bi at time ti (1 ≀ i ≀ n). Ai and Bi are non-empty strings at most 20 characters long, consisting of lowercase letters ('a' ... 'z'), and ti is an integer (0 ≀ ti ≀ 10000). It is guaranteed that the lines are given in non-decreasing order of ti's and that no user sent a message to himself. The elements in the lines are separated by single spaces. Output In the first line print integer k β€” the number of pairs of friends. In the next k lines print pairs of friends as "Ai Bi" (without the quotes). You can print users in pairs and the pairs themselves in any order. Each pair must be printed exactly once. Examples Input 4 1 vasya petya 1 petya vasya 2 anya ivan 2 ivan anya 4 Output 1 petya vasya Input 1 1000 a b 0 Output 0 Note In the first sample test case Vasya and Petya are friends because their messages' sending times are one second apart. Anya and Ivan are not, because their messages' sending times differ by more than one second.
instruction
0
37,079
14
74,158
Tags: *special, greedy, implementation Correct Solution: ``` l=input().split() n=int(l[0]) d=int(l[1]) lfi=[] lofpairs=[] for you in range(n): s=input().split() for i in lfi: if(s[0]==i[1] and s[1]==i[0]): if(int(s[2])-int(i[2])<=d and int(s[2])-int(i[2])!=0): if((i[0],i[1]) not in lofpairs and (i[1],i[0]) not in lofpairs): lofpairs.append((i[0],i[1])) lfi.append(s) print(len(lofpairs)) for i in lofpairs: print(i[0],i[1]) ```
output
1
37,079
14
74,159
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarpus has a hobby β€” he develops an unusual social network. His work is almost completed, and there is only one more module to implement β€” the module which determines friends. Oh yes, in this social network one won't have to add friends manually! Pairs of friends are deduced in the following way. Let's assume that user A sent user B a message at time t1, and user B sent user A a message at time t2. If 0 < t2 - t1 ≀ d, then user B's message was an answer to user A's one. Users A and B are considered to be friends if A answered at least one B's message or B answered at least one A's message. You are given the log of messages in chronological order and a number d. Find all pairs of users who will be considered to be friends. Input The first line of the input contains two integers n and d (1 ≀ n, d ≀ 1000). The next n lines contain the messages log. The i-th line contains one line of the log formatted as "Ai Bi ti" (without the quotes), which means that user Ai sent a message to user Bi at time ti (1 ≀ i ≀ n). Ai and Bi are non-empty strings at most 20 characters long, consisting of lowercase letters ('a' ... 'z'), and ti is an integer (0 ≀ ti ≀ 10000). It is guaranteed that the lines are given in non-decreasing order of ti's and that no user sent a message to himself. The elements in the lines are separated by single spaces. Output In the first line print integer k β€” the number of pairs of friends. In the next k lines print pairs of friends as "Ai Bi" (without the quotes). You can print users in pairs and the pairs themselves in any order. Each pair must be printed exactly once. Examples Input 4 1 vasya petya 1 petya vasya 2 anya ivan 2 ivan anya 4 Output 1 petya vasya Input 1 1000 a b 0 Output 0 Note In the first sample test case Vasya and Petya are friends because their messages' sending times are one second apart. Anya and Ivan are not, because their messages' sending times differ by more than one second.
instruction
0
37,080
14
74,160
Tags: *special, greedy, implementation Correct Solution: ``` n, d = map(int, input().split()) L=[] pair=[] for it in range(n): j=input().split() j[2]=int(j[2]) for i in L: if(0<j[2]-i[2]<=d) & (i[0]==j[1]) & (i[1]==j[0]): if i[0]>i[1]: fri=[i[1],i[0]] else: fri=[i[0],i[1]] if pair.count(fri)==0: pair.append(fri) L.append(j) print(len(pair)) for i in pair: print(i[0],i[1]) ```
output
1
37,080
14
74,161
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarpus has a hobby β€” he develops an unusual social network. His work is almost completed, and there is only one more module to implement β€” the module which determines friends. Oh yes, in this social network one won't have to add friends manually! Pairs of friends are deduced in the following way. Let's assume that user A sent user B a message at time t1, and user B sent user A a message at time t2. If 0 < t2 - t1 ≀ d, then user B's message was an answer to user A's one. Users A and B are considered to be friends if A answered at least one B's message or B answered at least one A's message. You are given the log of messages in chronological order and a number d. Find all pairs of users who will be considered to be friends. Input The first line of the input contains two integers n and d (1 ≀ n, d ≀ 1000). The next n lines contain the messages log. The i-th line contains one line of the log formatted as "Ai Bi ti" (without the quotes), which means that user Ai sent a message to user Bi at time ti (1 ≀ i ≀ n). Ai and Bi are non-empty strings at most 20 characters long, consisting of lowercase letters ('a' ... 'z'), and ti is an integer (0 ≀ ti ≀ 10000). It is guaranteed that the lines are given in non-decreasing order of ti's and that no user sent a message to himself. The elements in the lines are separated by single spaces. Output In the first line print integer k β€” the number of pairs of friends. In the next k lines print pairs of friends as "Ai Bi" (without the quotes). You can print users in pairs and the pairs themselves in any order. Each pair must be printed exactly once. Examples Input 4 1 vasya petya 1 petya vasya 2 anya ivan 2 ivan anya 4 Output 1 petya vasya Input 1 1000 a b 0 Output 0 Note In the first sample test case Vasya and Petya are friends because their messages' sending times are one second apart. Anya and Ivan are not, because their messages' sending times differ by more than one second.
instruction
0
37,081
14
74,162
Tags: *special, greedy, implementation Correct Solution: ``` n, d = map(int, input().split()) f = list() m = dict() for i in range(n): add = False a, b, t2 = input().split() t2 = int(t2) c = m.get(b + ' ' + a, 0) if c != -1: if c != 0: for t1 in c: if 0 < t2 - t1 <= d: f.append(a + ' ' + b) m[b + ' ' + a] = -1 m[a + ' ' + b] = -1 add = True break if not add: c = m.get(a + ' ' + b, 0) if c == 0: m[a + ' ' + b] = [t2] elif c[0] != t2: m[a + ' ' + b] = [t2, c[0]] print(len(f)) for i in f: print(i) ```
output
1
37,081
14
74,163
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarpus has a hobby β€” he develops an unusual social network. His work is almost completed, and there is only one more module to implement β€” the module which determines friends. Oh yes, in this social network one won't have to add friends manually! Pairs of friends are deduced in the following way. Let's assume that user A sent user B a message at time t1, and user B sent user A a message at time t2. If 0 < t2 - t1 ≀ d, then user B's message was an answer to user A's one. Users A and B are considered to be friends if A answered at least one B's message or B answered at least one A's message. You are given the log of messages in chronological order and a number d. Find all pairs of users who will be considered to be friends. Input The first line of the input contains two integers n and d (1 ≀ n, d ≀ 1000). The next n lines contain the messages log. The i-th line contains one line of the log formatted as "Ai Bi ti" (without the quotes), which means that user Ai sent a message to user Bi at time ti (1 ≀ i ≀ n). Ai and Bi are non-empty strings at most 20 characters long, consisting of lowercase letters ('a' ... 'z'), and ti is an integer (0 ≀ ti ≀ 10000). It is guaranteed that the lines are given in non-decreasing order of ti's and that no user sent a message to himself. The elements in the lines are separated by single spaces. Output In the first line print integer k β€” the number of pairs of friends. In the next k lines print pairs of friends as "Ai Bi" (without the quotes). You can print users in pairs and the pairs themselves in any order. Each pair must be printed exactly once. Examples Input 4 1 vasya petya 1 petya vasya 2 anya ivan 2 ivan anya 4 Output 1 petya vasya Input 1 1000 a b 0 Output 0 Note In the first sample test case Vasya and Petya are friends because their messages' sending times are one second apart. Anya and Ivan are not, because their messages' sending times differ by more than one second.
instruction
0
37,082
14
74,164
Tags: *special, greedy, implementation Correct Solution: ``` # n=int(input()) #q.sort(key=lambda x:((x[1]-x[0]),-x[0])) # n,k=map(int,input().split()) # arr=list(map(int,input().split())) #ls=list(map(int,input().split())) #for i in range(m): #from sys import stdin #n=int(stdin.readline()) #for _ in range(int(input())): import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") ########################################################## import sys from collections import Counter n,k=map(int,input().split()) l=[] for i in range(n) : p,q,t=map(str,sys.stdin.readline().split()) t=int(t) l.append([[p,q],t]) #print(l) p=[] for i in range(n) : for j in range(i+1,n) : if l[j][1]-l[i][1] >k : break if Counter(l[i][0]) == Counter(l[j][0]) and l[j][1]-l[i][1]<=k and l[j][1]-l[i][1] >0 and l[i][0]!=l[j][0]: #print(l[i][0]) if l[i][0] not in p and l[i][0][::-1] not in p : p.append(l[i][0]) print(len(p)) if len(p)==0 : exit(0) for i in p: print(i[0],i[1]) ```
output
1
37,082
14
74,165
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarpus has a hobby β€” he develops an unusual social network. His work is almost completed, and there is only one more module to implement β€” the module which determines friends. Oh yes, in this social network one won't have to add friends manually! Pairs of friends are deduced in the following way. Let's assume that user A sent user B a message at time t1, and user B sent user A a message at time t2. If 0 < t2 - t1 ≀ d, then user B's message was an answer to user A's one. Users A and B are considered to be friends if A answered at least one B's message or B answered at least one A's message. You are given the log of messages in chronological order and a number d. Find all pairs of users who will be considered to be friends. Input The first line of the input contains two integers n and d (1 ≀ n, d ≀ 1000). The next n lines contain the messages log. The i-th line contains one line of the log formatted as "Ai Bi ti" (without the quotes), which means that user Ai sent a message to user Bi at time ti (1 ≀ i ≀ n). Ai and Bi are non-empty strings at most 20 characters long, consisting of lowercase letters ('a' ... 'z'), and ti is an integer (0 ≀ ti ≀ 10000). It is guaranteed that the lines are given in non-decreasing order of ti's and that no user sent a message to himself. The elements in the lines are separated by single spaces. Output In the first line print integer k β€” the number of pairs of friends. In the next k lines print pairs of friends as "Ai Bi" (without the quotes). You can print users in pairs and the pairs themselves in any order. Each pair must be printed exactly once. Examples Input 4 1 vasya petya 1 petya vasya 2 anya ivan 2 ivan anya 4 Output 1 petya vasya Input 1 1000 a b 0 Output 0 Note In the first sample test case Vasya and Petya are friends because their messages' sending times are one second apart. Anya and Ivan are not, because their messages' sending times differ by more than one second.
instruction
0
37,083
14
74,166
Tags: *special, greedy, implementation Correct Solution: ``` def min_positive_difference(numbers1, numbers2): min_difference = float('inf') if not numbers1 or not numbers2: return min_difference for n in numbers1: for m in numbers2: candidate = abs(n - m) if candidate < min_difference and candidate > 0: min_difference = candidate return min_difference n, d = map(int, input().split()) messages = {} for _ in range(n): first, second, time = input().split() if (first, second) in messages: messages[(first, second)].append(int(time)) else: messages[(first, second)] = [int(time)] result = [] for first, second in messages: if (second, first) in result: continue if 0 < min_positive_difference(messages[(first, second)], messages.get((second, first))) <= d: result.append((first, second)) print(len(result)) for first, second in result: print(first, second) ```
output
1
37,083
14
74,167
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarpus has a hobby β€” he develops an unusual social network. His work is almost completed, and there is only one more module to implement β€” the module which determines friends. Oh yes, in this social network one won't have to add friends manually! Pairs of friends are deduced in the following way. Let's assume that user A sent user B a message at time t1, and user B sent user A a message at time t2. If 0 < t2 - t1 ≀ d, then user B's message was an answer to user A's one. Users A and B are considered to be friends if A answered at least one B's message or B answered at least one A's message. You are given the log of messages in chronological order and a number d. Find all pairs of users who will be considered to be friends. Input The first line of the input contains two integers n and d (1 ≀ n, d ≀ 1000). The next n lines contain the messages log. The i-th line contains one line of the log formatted as "Ai Bi ti" (without the quotes), which means that user Ai sent a message to user Bi at time ti (1 ≀ i ≀ n). Ai and Bi are non-empty strings at most 20 characters long, consisting of lowercase letters ('a' ... 'z'), and ti is an integer (0 ≀ ti ≀ 10000). It is guaranteed that the lines are given in non-decreasing order of ti's and that no user sent a message to himself. The elements in the lines are separated by single spaces. Output In the first line print integer k β€” the number of pairs of friends. In the next k lines print pairs of friends as "Ai Bi" (without the quotes). You can print users in pairs and the pairs themselves in any order. Each pair must be printed exactly once. Examples Input 4 1 vasya petya 1 petya vasya 2 anya ivan 2 ivan anya 4 Output 1 petya vasya Input 1 1000 a b 0 Output 0 Note In the first sample test case Vasya and Petya are friends because their messages' sending times are one second apart. Anya and Ivan are not, because their messages' sending times differ by more than one second.
instruction
0
37,084
14
74,168
Tags: *special, greedy, implementation Correct Solution: ``` from itertools import dropwhile n, d = map(int, input().split()) queue = [] ans = set() for i in range(n): a, b, time = input().split() time = int(time) queue = list(dropwhile(lambda x: time - x[2] > d, queue)) for msg in queue: if msg[0] == b and msg[1] == a and msg[2] != time: ans.add((min(a, b), max(a,b))) queue.append((a, b, time)) print(len(ans)) while 1: try: t = ans.pop() print(t[0], t[1]) except KeyError: break ```
output
1
37,084
14
74,169
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarpus has a hobby β€” he develops an unusual social network. His work is almost completed, and there is only one more module to implement β€” the module which determines friends. Oh yes, in this social network one won't have to add friends manually! Pairs of friends are deduced in the following way. Let's assume that user A sent user B a message at time t1, and user B sent user A a message at time t2. If 0 < t2 - t1 ≀ d, then user B's message was an answer to user A's one. Users A and B are considered to be friends if A answered at least one B's message or B answered at least one A's message. You are given the log of messages in chronological order and a number d. Find all pairs of users who will be considered to be friends. Input The first line of the input contains two integers n and d (1 ≀ n, d ≀ 1000). The next n lines contain the messages log. The i-th line contains one line of the log formatted as "Ai Bi ti" (without the quotes), which means that user Ai sent a message to user Bi at time ti (1 ≀ i ≀ n). Ai and Bi are non-empty strings at most 20 characters long, consisting of lowercase letters ('a' ... 'z'), and ti is an integer (0 ≀ ti ≀ 10000). It is guaranteed that the lines are given in non-decreasing order of ti's and that no user sent a message to himself. The elements in the lines are separated by single spaces. Output In the first line print integer k β€” the number of pairs of friends. In the next k lines print pairs of friends as "Ai Bi" (without the quotes). You can print users in pairs and the pairs themselves in any order. Each pair must be printed exactly once. Examples Input 4 1 vasya petya 1 petya vasya 2 anya ivan 2 ivan anya 4 Output 1 petya vasya Input 1 1000 a b 0 Output 0 Note In the first sample test case Vasya and Petya are friends because their messages' sending times are one second apart. Anya and Ivan are not, because their messages' sending times differ by more than one second.
instruction
0
37,085
14
74,170
Tags: *special, greedy, implementation Correct Solution: ``` def readln(): return tuple(map(int, input().split())) n, d = readln() a = [] b = [] for _ in range(n): p, q, t = input().split() t = int(t) if p > q: a.append((p, q, t)) else: b.append((q, p, t)) ans = set() for p, q, t in a: for p1, q1, t1 in b: if p == p1 and q == q1 and 0 < abs(t - t1) <= d: ans.add(p + ' ' + q) print(len(ans)) print('\n'.join(ans)) ```
output
1
37,085
14
74,171
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarpus has a hobby β€” he develops an unusual social network. His work is almost completed, and there is only one more module to implement β€” the module which determines friends. Oh yes, in this social network one won't have to add friends manually! Pairs of friends are deduced in the following way. Let's assume that user A sent user B a message at time t1, and user B sent user A a message at time t2. If 0 < t2 - t1 ≀ d, then user B's message was an answer to user A's one. Users A and B are considered to be friends if A answered at least one B's message or B answered at least one A's message. You are given the log of messages in chronological order and a number d. Find all pairs of users who will be considered to be friends. Input The first line of the input contains two integers n and d (1 ≀ n, d ≀ 1000). The next n lines contain the messages log. The i-th line contains one line of the log formatted as "Ai Bi ti" (without the quotes), which means that user Ai sent a message to user Bi at time ti (1 ≀ i ≀ n). Ai and Bi are non-empty strings at most 20 characters long, consisting of lowercase letters ('a' ... 'z'), and ti is an integer (0 ≀ ti ≀ 10000). It is guaranteed that the lines are given in non-decreasing order of ti's and that no user sent a message to himself. The elements in the lines are separated by single spaces. Output In the first line print integer k β€” the number of pairs of friends. In the next k lines print pairs of friends as "Ai Bi" (without the quotes). You can print users in pairs and the pairs themselves in any order. Each pair must be printed exactly once. Examples Input 4 1 vasya petya 1 petya vasya 2 anya ivan 2 ivan anya 4 Output 1 petya vasya Input 1 1000 a b 0 Output 0 Note In the first sample test case Vasya and Petya are friends because their messages' sending times are one second apart. Anya and Ivan are not, because their messages' sending times differ by more than one second.
instruction
0
37,086
14
74,172
Tags: *special, greedy, implementation Correct Solution: ``` import re from collections import Counter class Task: data = [] delta = 0 friends = set() def getData(self): n, self.delta = [int(x) for x in input().split(" ")] for i in range(0, n): self.data += [input()] def solve(self): messages = dict() for line1 in self.data: A1, B1, time1 = line1.split(" ") for line2 in self.data: A2, B2, time2 = line2.split(" ") if A2 == B1 and B2 == A1 and \ 0 < abs(int(time2) - int(time1)) <= self.delta: self.friends.add(min(A2 + " " + B2, B2 + " " + A2)) def printAnswer(self): print(len(self.friends)) for pair in self.friends: print(pair) task = Task(); task.getData(); task.solve(); task.printAnswer(); ```
output
1
37,086
14
74,173
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarpus has a hobby β€” he develops an unusual social network. His work is almost completed, and there is only one more module to implement β€” the module which determines friends. Oh yes, in this social network one won't have to add friends manually! Pairs of friends are deduced in the following way. Let's assume that user A sent user B a message at time t1, and user B sent user A a message at time t2. If 0 < t2 - t1 ≀ d, then user B's message was an answer to user A's one. Users A and B are considered to be friends if A answered at least one B's message or B answered at least one A's message. You are given the log of messages in chronological order and a number d. Find all pairs of users who will be considered to be friends. Input The first line of the input contains two integers n and d (1 ≀ n, d ≀ 1000). The next n lines contain the messages log. The i-th line contains one line of the log formatted as "Ai Bi ti" (without the quotes), which means that user Ai sent a message to user Bi at time ti (1 ≀ i ≀ n). Ai and Bi are non-empty strings at most 20 characters long, consisting of lowercase letters ('a' ... 'z'), and ti is an integer (0 ≀ ti ≀ 10000). It is guaranteed that the lines are given in non-decreasing order of ti's and that no user sent a message to himself. The elements in the lines are separated by single spaces. Output In the first line print integer k β€” the number of pairs of friends. In the next k lines print pairs of friends as "Ai Bi" (without the quotes). You can print users in pairs and the pairs themselves in any order. Each pair must be printed exactly once. Examples Input 4 1 vasya petya 1 petya vasya 2 anya ivan 2 ivan anya 4 Output 1 petya vasya Input 1 1000 a b 0 Output 0 Note In the first sample test case Vasya and Petya are friends because their messages' sending times are one second apart. Anya and Ivan are not, because their messages' sending times differ by more than one second. Submitted Solution: ``` n,d=map(int,input().split()) ans,hist=[],[] for i in range(n): s=input().split() t=int(s[2]) for x in hist: if 0<t-x[2]<=d and s[0]==x[1] and s[1]==x[0]: a=sorted([x[0],x[1]]) if not a in ans: ans.append(a) hist.append([s[0],s[1],t]) print(len(ans)) for x in ans: print(x[0],x[1]) ```
instruction
0
37,087
14
74,174
Yes
output
1
37,087
14
74,175
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarpus has a hobby β€” he develops an unusual social network. His work is almost completed, and there is only one more module to implement β€” the module which determines friends. Oh yes, in this social network one won't have to add friends manually! Pairs of friends are deduced in the following way. Let's assume that user A sent user B a message at time t1, and user B sent user A a message at time t2. If 0 < t2 - t1 ≀ d, then user B's message was an answer to user A's one. Users A and B are considered to be friends if A answered at least one B's message or B answered at least one A's message. You are given the log of messages in chronological order and a number d. Find all pairs of users who will be considered to be friends. Input The first line of the input contains two integers n and d (1 ≀ n, d ≀ 1000). The next n lines contain the messages log. The i-th line contains one line of the log formatted as "Ai Bi ti" (without the quotes), which means that user Ai sent a message to user Bi at time ti (1 ≀ i ≀ n). Ai and Bi are non-empty strings at most 20 characters long, consisting of lowercase letters ('a' ... 'z'), and ti is an integer (0 ≀ ti ≀ 10000). It is guaranteed that the lines are given in non-decreasing order of ti's and that no user sent a message to himself. The elements in the lines are separated by single spaces. Output In the first line print integer k β€” the number of pairs of friends. In the next k lines print pairs of friends as "Ai Bi" (without the quotes). You can print users in pairs and the pairs themselves in any order. Each pair must be printed exactly once. Examples Input 4 1 vasya petya 1 petya vasya 2 anya ivan 2 ivan anya 4 Output 1 petya vasya Input 1 1000 a b 0 Output 0 Note In the first sample test case Vasya and Petya are friends because their messages' sending times are one second apart. Anya and Ivan are not, because their messages' sending times differ by more than one second. Submitted Solution: ``` n, d = map(int, input().split()) f, p, q, r = [], [], {}, 0 for i in range(n): a, b, t = input().split() u, v, t = a + ' ' + b, b + ' ' + a, int(t) if t == r: p.append((u, v)) else: f += [min(x, y) for x, y in p if (y in q and q[y] >= r - d)] for x, y in p: q[x] = r p, r = [(u, v)], t f = set(f + [min(x, y) for x, y in p if (y in q and q[y] >= r - d)]) print(len(f)) print('\n'.join(f)) ```
instruction
0
37,088
14
74,176
Yes
output
1
37,088
14
74,177
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarpus has a hobby β€” he develops an unusual social network. His work is almost completed, and there is only one more module to implement β€” the module which determines friends. Oh yes, in this social network one won't have to add friends manually! Pairs of friends are deduced in the following way. Let's assume that user A sent user B a message at time t1, and user B sent user A a message at time t2. If 0 < t2 - t1 ≀ d, then user B's message was an answer to user A's one. Users A and B are considered to be friends if A answered at least one B's message or B answered at least one A's message. You are given the log of messages in chronological order and a number d. Find all pairs of users who will be considered to be friends. Input The first line of the input contains two integers n and d (1 ≀ n, d ≀ 1000). The next n lines contain the messages log. The i-th line contains one line of the log formatted as "Ai Bi ti" (without the quotes), which means that user Ai sent a message to user Bi at time ti (1 ≀ i ≀ n). Ai and Bi are non-empty strings at most 20 characters long, consisting of lowercase letters ('a' ... 'z'), and ti is an integer (0 ≀ ti ≀ 10000). It is guaranteed that the lines are given in non-decreasing order of ti's and that no user sent a message to himself. The elements in the lines are separated by single spaces. Output In the first line print integer k β€” the number of pairs of friends. In the next k lines print pairs of friends as "Ai Bi" (without the quotes). You can print users in pairs and the pairs themselves in any order. Each pair must be printed exactly once. Examples Input 4 1 vasya petya 1 petya vasya 2 anya ivan 2 ivan anya 4 Output 1 petya vasya Input 1 1000 a b 0 Output 0 Note In the first sample test case Vasya and Petya are friends because their messages' sending times are one second apart. Anya and Ivan are not, because their messages' sending times differ by more than one second. Submitted Solution: ``` import sys from collections import Counter n,k=map(int,input().split()) l=[] for i in range(n) : p,q,t=map(str,sys.stdin.readline().split()) t=int(t) l.append([[p,q],t]) #print(l) p=[] for i in range(n) : for j in range(i+1,n) : if l[j][1]-l[i][1] >k : break if Counter(l[i][0]) == Counter(l[j][0]) and l[j][1]-l[i][1]<=k and l[j][1]-l[i][1] >0 and l[i][0]!=l[j][0]: #print(l[i][0]) if l[i][0] not in p and l[i][0][::-1] not in p : p.append(l[i][0]) print(len(p)) if len(p)==0 : exit(0) for i in p: print(i[0],i[1]) ```
instruction
0
37,089
14
74,178
Yes
output
1
37,089
14
74,179
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarpus has a hobby β€” he develops an unusual social network. His work is almost completed, and there is only one more module to implement β€” the module which determines friends. Oh yes, in this social network one won't have to add friends manually! Pairs of friends are deduced in the following way. Let's assume that user A sent user B a message at time t1, and user B sent user A a message at time t2. If 0 < t2 - t1 ≀ d, then user B's message was an answer to user A's one. Users A and B are considered to be friends if A answered at least one B's message or B answered at least one A's message. You are given the log of messages in chronological order and a number d. Find all pairs of users who will be considered to be friends. Input The first line of the input contains two integers n and d (1 ≀ n, d ≀ 1000). The next n lines contain the messages log. The i-th line contains one line of the log formatted as "Ai Bi ti" (without the quotes), which means that user Ai sent a message to user Bi at time ti (1 ≀ i ≀ n). Ai and Bi are non-empty strings at most 20 characters long, consisting of lowercase letters ('a' ... 'z'), and ti is an integer (0 ≀ ti ≀ 10000). It is guaranteed that the lines are given in non-decreasing order of ti's and that no user sent a message to himself. The elements in the lines are separated by single spaces. Output In the first line print integer k β€” the number of pairs of friends. In the next k lines print pairs of friends as "Ai Bi" (without the quotes). You can print users in pairs and the pairs themselves in any order. Each pair must be printed exactly once. Examples Input 4 1 vasya petya 1 petya vasya 2 anya ivan 2 ivan anya 4 Output 1 petya vasya Input 1 1000 a b 0 Output 0 Note In the first sample test case Vasya and Petya are friends because their messages' sending times are one second apart. Anya and Ivan are not, because their messages' sending times differ by more than one second. Submitted Solution: ``` n,d=map(int,input().split()) arr = [] for _ in range(n): a,b,c=input().split() curr = [a,b,int(c)] arr.append(curr) ans = [] for i in range(n): for j in range(i+1,n): if abs(arr[i][2]-arr[j][2])<=d and abs(arr[i][2]-arr[j][2])>0: if arr[i][0]==arr[j][1] and arr[i][1]==arr[j][0]: ans.append((arr[i][1],arr[i][0])) elif abs(arr[i][2]-arr[j][2])>d: break if ans==[]: print(0) else: distinct_ans=[] for i in ans: i=list(i) x=i.sort() distinct_ans.append(i) stack = [] for i in distinct_ans: if i not in stack: stack.append(i) print(len(stack)) for i in stack: for j in i: print(j,end=' ') print() ```
instruction
0
37,090
14
74,180
Yes
output
1
37,090
14
74,181
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarpus has a hobby β€” he develops an unusual social network. His work is almost completed, and there is only one more module to implement β€” the module which determines friends. Oh yes, in this social network one won't have to add friends manually! Pairs of friends are deduced in the following way. Let's assume that user A sent user B a message at time t1, and user B sent user A a message at time t2. If 0 < t2 - t1 ≀ d, then user B's message was an answer to user A's one. Users A and B are considered to be friends if A answered at least one B's message or B answered at least one A's message. You are given the log of messages in chronological order and a number d. Find all pairs of users who will be considered to be friends. Input The first line of the input contains two integers n and d (1 ≀ n, d ≀ 1000). The next n lines contain the messages log. The i-th line contains one line of the log formatted as "Ai Bi ti" (without the quotes), which means that user Ai sent a message to user Bi at time ti (1 ≀ i ≀ n). Ai and Bi are non-empty strings at most 20 characters long, consisting of lowercase letters ('a' ... 'z'), and ti is an integer (0 ≀ ti ≀ 10000). It is guaranteed that the lines are given in non-decreasing order of ti's and that no user sent a message to himself. The elements in the lines are separated by single spaces. Output In the first line print integer k β€” the number of pairs of friends. In the next k lines print pairs of friends as "Ai Bi" (without the quotes). You can print users in pairs and the pairs themselves in any order. Each pair must be printed exactly once. Examples Input 4 1 vasya petya 1 petya vasya 2 anya ivan 2 ivan anya 4 Output 1 petya vasya Input 1 1000 a b 0 Output 0 Note In the first sample test case Vasya and Petya are friends because their messages' sending times are one second apart. Anya and Ivan are not, because their messages' sending times differ by more than one second. Submitted Solution: ``` n , d = map(int,input().split()) d1={} ; d2={} ; t=1 li = [ [[] for i in range(1002)] for j in range(1002) ] #print(li) p1 = {} l2 = [ [] for i in range (1002)] for _ in range (n) : x , y , z = map(str,input().split()) z = int(z) if x not in d1 and y not in d1 : d1[x]=t p1[t]=x t+=1 d1[y]=t p1[t]=y t+=1 elif x not in d1 : d1[x]=t p1[t]=x t+=1 elif y not in d1 : d1[y]=t p1[t]=y t+=1 li[d1[x]][d1[y]] += [z] l2[d1[x]] += [d1[y]] answer = [ [0 for i in range(1002)] for j in range(1002) ] ans = 0 an1 =[ ] #print(d1) #print(li) for i in range (1,t) : for j in l2[i] : if j<=i : continue t = 0 if answer[i][j] == 0 : for p in li[i][j] : for q in li[j][i] : if 0 <abs(p-q) <= d : t=1 answer[i][j]=1 an1.append([i,j]) ans+=1 break if 1 == t : break if t==1 : break print(ans) for i in range(ans) : x = an1[i][0] ; y = an1[i][1] ; print(p1[x],p1[y]) ```
instruction
0
37,091
14
74,182
No
output
1
37,091
14
74,183
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarpus has a hobby β€” he develops an unusual social network. His work is almost completed, and there is only one more module to implement β€” the module which determines friends. Oh yes, in this social network one won't have to add friends manually! Pairs of friends are deduced in the following way. Let's assume that user A sent user B a message at time t1, and user B sent user A a message at time t2. If 0 < t2 - t1 ≀ d, then user B's message was an answer to user A's one. Users A and B are considered to be friends if A answered at least one B's message or B answered at least one A's message. You are given the log of messages in chronological order and a number d. Find all pairs of users who will be considered to be friends. Input The first line of the input contains two integers n and d (1 ≀ n, d ≀ 1000). The next n lines contain the messages log. The i-th line contains one line of the log formatted as "Ai Bi ti" (without the quotes), which means that user Ai sent a message to user Bi at time ti (1 ≀ i ≀ n). Ai and Bi are non-empty strings at most 20 characters long, consisting of lowercase letters ('a' ... 'z'), and ti is an integer (0 ≀ ti ≀ 10000). It is guaranteed that the lines are given in non-decreasing order of ti's and that no user sent a message to himself. The elements in the lines are separated by single spaces. Output In the first line print integer k β€” the number of pairs of friends. In the next k lines print pairs of friends as "Ai Bi" (without the quotes). You can print users in pairs and the pairs themselves in any order. Each pair must be printed exactly once. Examples Input 4 1 vasya petya 1 petya vasya 2 anya ivan 2 ivan anya 4 Output 1 petya vasya Input 1 1000 a b 0 Output 0 Note In the first sample test case Vasya and Petya are friends because their messages' sending times are one second apart. Anya and Ivan are not, because their messages' sending times differ by more than one second. Submitted Solution: ``` import re from collections import Counter class Task: data = [] delta = 0 friends = set() def getData(self): n, self.delta = [int(x) for x in input().split(" ")] for i in range(0, n): self.data += [input()] def solve(self): pairs = dict() for line in self.data: first, second, time = line.split(" ") if second + " " + first in pairs and \ int(time) - pairs[second + " " + first] <= self.delta: if second + " " + first not in self.friends: self.friends.add(first + " " + second) else: pairs[first + " " + second] = int(time) def printAnswer(self): print(len(self.friends)) for pair in self.friends: print(pair) task = Task(); task.getData(); task.solve(); task.printAnswer(); ```
instruction
0
37,092
14
74,184
No
output
1
37,092
14
74,185
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarpus has a hobby β€” he develops an unusual social network. His work is almost completed, and there is only one more module to implement β€” the module which determines friends. Oh yes, in this social network one won't have to add friends manually! Pairs of friends are deduced in the following way. Let's assume that user A sent user B a message at time t1, and user B sent user A a message at time t2. If 0 < t2 - t1 ≀ d, then user B's message was an answer to user A's one. Users A and B are considered to be friends if A answered at least one B's message or B answered at least one A's message. You are given the log of messages in chronological order and a number d. Find all pairs of users who will be considered to be friends. Input The first line of the input contains two integers n and d (1 ≀ n, d ≀ 1000). The next n lines contain the messages log. The i-th line contains one line of the log formatted as "Ai Bi ti" (without the quotes), which means that user Ai sent a message to user Bi at time ti (1 ≀ i ≀ n). Ai and Bi are non-empty strings at most 20 characters long, consisting of lowercase letters ('a' ... 'z'), and ti is an integer (0 ≀ ti ≀ 10000). It is guaranteed that the lines are given in non-decreasing order of ti's and that no user sent a message to himself. The elements in the lines are separated by single spaces. Output In the first line print integer k β€” the number of pairs of friends. In the next k lines print pairs of friends as "Ai Bi" (without the quotes). You can print users in pairs and the pairs themselves in any order. Each pair must be printed exactly once. Examples Input 4 1 vasya petya 1 petya vasya 2 anya ivan 2 ivan anya 4 Output 1 petya vasya Input 1 1000 a b 0 Output 0 Note In the first sample test case Vasya and Petya are friends because their messages' sending times are one second apart. Anya and Ivan are not, because their messages' sending times differ by more than one second. Submitted Solution: ``` import re from collections import Counter class Task: data = [] delta = 0 friends = set() def getData(self): n, self.delta = [int(x) for x in input().split(" ")] for i in range(0, n): self.data += [input()] def solve(self): pairs = dict() for line in self.data: first, second, time = line.split(" ") if second + " " + first in pairs and \ 0 < int(time) - pairs[second + " " + first] <= self.delta: if second + " " + first <= first + " " + second: self.friends.add(second + " " + first) else: self.friends.add(first + " " + second) else: pairs[first + " " + second] = int(time) def printAnswer(self): print(len(self.friends)) for pair in self.friends: print(pair) task = Task(); task.getData(); task.solve(); task.printAnswer(); ```
instruction
0
37,093
14
74,186
No
output
1
37,093
14
74,187
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarpus has a hobby β€” he develops an unusual social network. His work is almost completed, and there is only one more module to implement β€” the module which determines friends. Oh yes, in this social network one won't have to add friends manually! Pairs of friends are deduced in the following way. Let's assume that user A sent user B a message at time t1, and user B sent user A a message at time t2. If 0 < t2 - t1 ≀ d, then user B's message was an answer to user A's one. Users A and B are considered to be friends if A answered at least one B's message or B answered at least one A's message. You are given the log of messages in chronological order and a number d. Find all pairs of users who will be considered to be friends. Input The first line of the input contains two integers n and d (1 ≀ n, d ≀ 1000). The next n lines contain the messages log. The i-th line contains one line of the log formatted as "Ai Bi ti" (without the quotes), which means that user Ai sent a message to user Bi at time ti (1 ≀ i ≀ n). Ai and Bi are non-empty strings at most 20 characters long, consisting of lowercase letters ('a' ... 'z'), and ti is an integer (0 ≀ ti ≀ 10000). It is guaranteed that the lines are given in non-decreasing order of ti's and that no user sent a message to himself. The elements in the lines are separated by single spaces. Output In the first line print integer k β€” the number of pairs of friends. In the next k lines print pairs of friends as "Ai Bi" (without the quotes). You can print users in pairs and the pairs themselves in any order. Each pair must be printed exactly once. Examples Input 4 1 vasya petya 1 petya vasya 2 anya ivan 2 ivan anya 4 Output 1 petya vasya Input 1 1000 a b 0 Output 0 Note In the first sample test case Vasya and Petya are friends because their messages' sending times are one second apart. Anya and Ivan are not, because their messages' sending times differ by more than one second. Submitted Solution: ``` (n, d) = [int(x) for x in input().split()] messages = [] friends = [] for _ in range(n): (sender, reciever, time) = input().split() time = int(time) for i in messages: (_sender, _reciever, _time) = i correct = reciever == _sender or sender == _reciever if correct and time-_time <= d: friends.append([sender, reciever]) messages.append([sender, reciever, time]) if len(friends) == 0: print(0) else: print(len(friends)) for x in friends: print(x[0], x[1]) ```
instruction
0
37,094
14
74,188
No
output
1
37,094
14
74,189
Provide tags and a correct Python 3 solution for this coding contest problem. The employees of the R1 company often spend time together: they watch football, they go camping, they solve contests. So, it's no big deal that sometimes someone pays for someone else. Today is the day of giving out money rewards. The R1 company CEO will invite employees into his office one by one, rewarding each one for the hard work this month. The CEO knows who owes money to whom. And he also understands that if he invites person x to his office for a reward, and then immediately invite person y, who has lent some money to person x, then they can meet. Of course, in such a situation, the joy of person x from his brand new money reward will be much less. Therefore, the R1 CEO decided to invite the staff in such an order that the described situation will not happen for any pair of employees invited one after another. However, there are a lot of employees in the company, and the CEO doesn't have a lot of time. Therefore, the task has been assigned to you. Given the debt relationships between all the employees, determine in which order they should be invited to the office of the R1 company CEO, or determine that the described order does not exist. Input The first line contains space-separated integers n and m <image> β€” the number of employees in R1 and the number of debt relations. Each of the following m lines contains two space-separated integers ai, bi (1 ≀ ai, bi ≀ n; ai β‰  bi), these integers indicate that the person number ai owes money to a person a number bi. Assume that all the employees are numbered from 1 to n. It is guaranteed that each pair of people p, q is mentioned in the input data at most once. In particular, the input data will not contain pairs p, q and q, p simultaneously. Output Print -1 if the described order does not exist. Otherwise, print the permutation of n distinct integers. The first number should denote the number of the person who goes to the CEO office first, the second number denote the person who goes second and so on. If there are multiple correct orders, you are allowed to print any of them. Examples Input 2 1 1 2 Output 2 1 Input 3 3 1 2 2 3 3 1 Output 2 1 3
instruction
0
37,169
14
74,338
Tags: dfs and similar Correct Solution: ``` n,m = map(int,input().split()) g = [set() for i in range(n)] from sys import stdin for i in range(m): a,b = map(int,stdin.readline().split()) g[a-1].add(b-1) c = [0]*n for i in range(n): c[i]=i for i in range(n): j=i while j>0 and c[j] in g[c[j-1]]: c[j],c[j-1]=c[j-1],c[j] j-=1 res = '' for i in c:res+=str(i+1)+' ' print(res) ```
output
1
37,169
14
74,339
Provide tags and a correct Python 3 solution for this coding contest problem. The employees of the R1 company often spend time together: they watch football, they go camping, they solve contests. So, it's no big deal that sometimes someone pays for someone else. Today is the day of giving out money rewards. The R1 company CEO will invite employees into his office one by one, rewarding each one for the hard work this month. The CEO knows who owes money to whom. And he also understands that if he invites person x to his office for a reward, and then immediately invite person y, who has lent some money to person x, then they can meet. Of course, in such a situation, the joy of person x from his brand new money reward will be much less. Therefore, the R1 CEO decided to invite the staff in such an order that the described situation will not happen for any pair of employees invited one after another. However, there are a lot of employees in the company, and the CEO doesn't have a lot of time. Therefore, the task has been assigned to you. Given the debt relationships between all the employees, determine in which order they should be invited to the office of the R1 company CEO, or determine that the described order does not exist. Input The first line contains space-separated integers n and m <image> β€” the number of employees in R1 and the number of debt relations. Each of the following m lines contains two space-separated integers ai, bi (1 ≀ ai, bi ≀ n; ai β‰  bi), these integers indicate that the person number ai owes money to a person a number bi. Assume that all the employees are numbered from 1 to n. It is guaranteed that each pair of people p, q is mentioned in the input data at most once. In particular, the input data will not contain pairs p, q and q, p simultaneously. Output Print -1 if the described order does not exist. Otherwise, print the permutation of n distinct integers. The first number should denote the number of the person who goes to the CEO office first, the second number denote the person who goes second and so on. If there are multiple correct orders, you are allowed to print any of them. Examples Input 2 1 1 2 Output 2 1 Input 3 3 1 2 2 3 3 1 Output 2 1 3
instruction
0
37,170
14
74,340
Tags: dfs and similar Correct Solution: ``` n, m = map(int,input().split()) g = [set() for i in range(n)] for i in range(m): a, b = map(int,input().split()) g[a-1].add(b-1) c = [0] * n for i in range(n): c[i] = i for i in range(n): j = i while (j > 0)and(c[j] in g[c[j - 1]]): c[j], c[j-1] = c[j-1], c[j] j -= 1 for i in c: print(i + 1, end = ' ') ```
output
1
37,170
14
74,341
Provide tags and a correct Python 3 solution for this coding contest problem. The employees of the R1 company often spend time together: they watch football, they go camping, they solve contests. So, it's no big deal that sometimes someone pays for someone else. Today is the day of giving out money rewards. The R1 company CEO will invite employees into his office one by one, rewarding each one for the hard work this month. The CEO knows who owes money to whom. And he also understands that if he invites person x to his office for a reward, and then immediately invite person y, who has lent some money to person x, then they can meet. Of course, in such a situation, the joy of person x from his brand new money reward will be much less. Therefore, the R1 CEO decided to invite the staff in such an order that the described situation will not happen for any pair of employees invited one after another. However, there are a lot of employees in the company, and the CEO doesn't have a lot of time. Therefore, the task has been assigned to you. Given the debt relationships between all the employees, determine in which order they should be invited to the office of the R1 company CEO, or determine that the described order does not exist. Input The first line contains space-separated integers n and m <image> β€” the number of employees in R1 and the number of debt relations. Each of the following m lines contains two space-separated integers ai, bi (1 ≀ ai, bi ≀ n; ai β‰  bi), these integers indicate that the person number ai owes money to a person a number bi. Assume that all the employees are numbered from 1 to n. It is guaranteed that each pair of people p, q is mentioned in the input data at most once. In particular, the input data will not contain pairs p, q and q, p simultaneously. Output Print -1 if the described order does not exist. Otherwise, print the permutation of n distinct integers. The first number should denote the number of the person who goes to the CEO office first, the second number denote the person who goes second and so on. If there are multiple correct orders, you are allowed to print any of them. Examples Input 2 1 1 2 Output 2 1 Input 3 3 1 2 2 3 3 1 Output 2 1 3
instruction
0
37,171
14
74,342
Tags: dfs and similar Correct Solution: ``` n,m = map(int,input().split()) g = [set() for i in range(n)] for i in range(m): a,b = map(int,input().split()) g[a-1].add(b-1) c = [0]*n for i in range(n): c[i]=i for i in range(n): j=i while j>0 and c[j] in g[c[j-1]]: c[j],c[j-1]=c[j-1],c[j] j-=1 for i in c: print(i+1,end=' ') ```
output
1
37,171
14
74,343
Provide tags and a correct Python 3 solution for this coding contest problem. The employees of the R1 company often spend time together: they watch football, they go camping, they solve contests. So, it's no big deal that sometimes someone pays for someone else. Today is the day of giving out money rewards. The R1 company CEO will invite employees into his office one by one, rewarding each one for the hard work this month. The CEO knows who owes money to whom. And he also understands that if he invites person x to his office for a reward, and then immediately invite person y, who has lent some money to person x, then they can meet. Of course, in such a situation, the joy of person x from his brand new money reward will be much less. Therefore, the R1 CEO decided to invite the staff in such an order that the described situation will not happen for any pair of employees invited one after another. However, there are a lot of employees in the company, and the CEO doesn't have a lot of time. Therefore, the task has been assigned to you. Given the debt relationships between all the employees, determine in which order they should be invited to the office of the R1 company CEO, or determine that the described order does not exist. Input The first line contains space-separated integers n and m <image> β€” the number of employees in R1 and the number of debt relations. Each of the following m lines contains two space-separated integers ai, bi (1 ≀ ai, bi ≀ n; ai β‰  bi), these integers indicate that the person number ai owes money to a person a number bi. Assume that all the employees are numbered from 1 to n. It is guaranteed that each pair of people p, q is mentioned in the input data at most once. In particular, the input data will not contain pairs p, q and q, p simultaneously. Output Print -1 if the described order does not exist. Otherwise, print the permutation of n distinct integers. The first number should denote the number of the person who goes to the CEO office first, the second number denote the person who goes second and so on. If there are multiple correct orders, you are allowed to print any of them. Examples Input 2 1 1 2 Output 2 1 Input 3 3 1 2 2 3 3 1 Output 2 1 3
instruction
0
37,172
14
74,344
Tags: dfs and similar Correct Solution: ``` n,m = map(int,input().split()) g = [set() for i in range(n)] from sys import stdin for i in range(m): a,b = map(int,stdin.readline().split()) g[a-1].add(b-1) c = [0]*n for i in range(n): c[i]=i for i in range(n): j=i while j>0 and c[j] in g[c[j-1]]: c[j],c[j-1]=c[j-1],c[j] j-=1 for i in c: print(i+1,end=' ') ```
output
1
37,172
14
74,345
Provide tags and a correct Python 3 solution for this coding contest problem. The employees of the R1 company often spend time together: they watch football, they go camping, they solve contests. So, it's no big deal that sometimes someone pays for someone else. Today is the day of giving out money rewards. The R1 company CEO will invite employees into his office one by one, rewarding each one for the hard work this month. The CEO knows who owes money to whom. And he also understands that if he invites person x to his office for a reward, and then immediately invite person y, who has lent some money to person x, then they can meet. Of course, in such a situation, the joy of person x from his brand new money reward will be much less. Therefore, the R1 CEO decided to invite the staff in such an order that the described situation will not happen for any pair of employees invited one after another. However, there are a lot of employees in the company, and the CEO doesn't have a lot of time. Therefore, the task has been assigned to you. Given the debt relationships between all the employees, determine in which order they should be invited to the office of the R1 company CEO, or determine that the described order does not exist. Input The first line contains space-separated integers n and m <image> β€” the number of employees in R1 and the number of debt relations. Each of the following m lines contains two space-separated integers ai, bi (1 ≀ ai, bi ≀ n; ai β‰  bi), these integers indicate that the person number ai owes money to a person a number bi. Assume that all the employees are numbered from 1 to n. It is guaranteed that each pair of people p, q is mentioned in the input data at most once. In particular, the input data will not contain pairs p, q and q, p simultaneously. Output Print -1 if the described order does not exist. Otherwise, print the permutation of n distinct integers. The first number should denote the number of the person who goes to the CEO office first, the second number denote the person who goes second and so on. If there are multiple correct orders, you are allowed to print any of them. Examples Input 2 1 1 2 Output 2 1 Input 3 3 1 2 2 3 3 1 Output 2 1 3
instruction
0
37,173
14
74,346
Tags: dfs and similar Correct Solution: ``` n,m=map(int,input().split()) graph=[] from sys import * for _ in range(0,n+2): graph.append([]) for _ in range(0,m): a,b=map(int,stdin.readline().split()) graph[b].append(a) vis=[0]*(n+2) f=[] def topo(start): global f vis[start]=1 s=[start] ans=[] while s: temp=s[-1] vis[temp]=1 for i in graph[temp]: if vis[i]==0: s.append(i) break else: f.append(s.pop()) #f=f+ans for i in range(1,n+1): if vis[i]==0: topo(i) f.reverse() for i in f: print (i,end=" ") ```
output
1
37,173
14
74,347
Provide tags and a correct Python 3 solution for this coding contest problem. The employees of the R1 company often spend time together: they watch football, they go camping, they solve contests. So, it's no big deal that sometimes someone pays for someone else. Today is the day of giving out money rewards. The R1 company CEO will invite employees into his office one by one, rewarding each one for the hard work this month. The CEO knows who owes money to whom. And he also understands that if he invites person x to his office for a reward, and then immediately invite person y, who has lent some money to person x, then they can meet. Of course, in such a situation, the joy of person x from his brand new money reward will be much less. Therefore, the R1 CEO decided to invite the staff in such an order that the described situation will not happen for any pair of employees invited one after another. However, there are a lot of employees in the company, and the CEO doesn't have a lot of time. Therefore, the task has been assigned to you. Given the debt relationships between all the employees, determine in which order they should be invited to the office of the R1 company CEO, or determine that the described order does not exist. Input The first line contains space-separated integers n and m <image> β€” the number of employees in R1 and the number of debt relations. Each of the following m lines contains two space-separated integers ai, bi (1 ≀ ai, bi ≀ n; ai β‰  bi), these integers indicate that the person number ai owes money to a person a number bi. Assume that all the employees are numbered from 1 to n. It is guaranteed that each pair of people p, q is mentioned in the input data at most once. In particular, the input data will not contain pairs p, q and q, p simultaneously. Output Print -1 if the described order does not exist. Otherwise, print the permutation of n distinct integers. The first number should denote the number of the person who goes to the CEO office first, the second number denote the person who goes second and so on. If there are multiple correct orders, you are allowed to print any of them. Examples Input 2 1 1 2 Output 2 1 Input 3 3 1 2 2 3 3 1 Output 2 1 3
instruction
0
37,174
14
74,348
Tags: dfs and similar Correct Solution: ``` import sys n,m=map(int,sys.stdin.readline().split()) P={} for i in range(m): a,b=map(int,sys.stdin.readline().split()) P[(a-1,b-1)]=1 A=[-1]*n A[0]=0 for i in range(1,n): j=1 A[i]=i x=i while(x>0 and (A[x-1],A[x]) in P): A[x-1],A[x]=A[x],A[x-1] x-=1 Anss="" for i in range(n): Anss+=str(A[i]+1)+" " sys.stdout.write(Anss) ```
output
1
37,174
14
74,349
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The employees of the R1 company often spend time together: they watch football, they go camping, they solve contests. So, it's no big deal that sometimes someone pays for someone else. Today is the day of giving out money rewards. The R1 company CEO will invite employees into his office one by one, rewarding each one for the hard work this month. The CEO knows who owes money to whom. And he also understands that if he invites person x to his office for a reward, and then immediately invite person y, who has lent some money to person x, then they can meet. Of course, in such a situation, the joy of person x from his brand new money reward will be much less. Therefore, the R1 CEO decided to invite the staff in such an order that the described situation will not happen for any pair of employees invited one after another. However, there are a lot of employees in the company, and the CEO doesn't have a lot of time. Therefore, the task has been assigned to you. Given the debt relationships between all the employees, determine in which order they should be invited to the office of the R1 company CEO, or determine that the described order does not exist. Input The first line contains space-separated integers n and m <image> β€” the number of employees in R1 and the number of debt relations. Each of the following m lines contains two space-separated integers ai, bi (1 ≀ ai, bi ≀ n; ai β‰  bi), these integers indicate that the person number ai owes money to a person a number bi. Assume that all the employees are numbered from 1 to n. It is guaranteed that each pair of people p, q is mentioned in the input data at most once. In particular, the input data will not contain pairs p, q and q, p simultaneously. Output Print -1 if the described order does not exist. Otherwise, print the permutation of n distinct integers. The first number should denote the number of the person who goes to the CEO office first, the second number denote the person who goes second and so on. If there are multiple correct orders, you are allowed to print any of them. Examples Input 2 1 1 2 Output 2 1 Input 3 3 1 2 2 3 3 1 Output 2 1 3 Submitted Solution: ``` a=input() kol_sotr=int(a.split()[0]) kol_otn=int(a.split()[1]) matrix= [ [0*kol_sotr]*kol_sotr ]*kol_sotr print(matrix) while (kol_otn>0): s=input() s1=int(s.split()[0]) s2=int(s.split()[1]) matrix[s1-1][s2-1]=1 print(matrix) kol_otn=kol_otn-1 for i in range(0,kol_sotr): for j in range(0,kol_sotr): if i==j: matrix[i][j]=2 s='' for i in range (0,kol_sotr): for j in range(0,kol_sotr): if matrix[i][j]==0: s+=str(i+1)+str(j+1) i=j s=s[0:kol_sotr] print(s) ```
instruction
0
37,175
14
74,350
No
output
1
37,175
14
74,351
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The employees of the R1 company often spend time together: they watch football, they go camping, they solve contests. So, it's no big deal that sometimes someone pays for someone else. Today is the day of giving out money rewards. The R1 company CEO will invite employees into his office one by one, rewarding each one for the hard work this month. The CEO knows who owes money to whom. And he also understands that if he invites person x to his office for a reward, and then immediately invite person y, who has lent some money to person x, then they can meet. Of course, in such a situation, the joy of person x from his brand new money reward will be much less. Therefore, the R1 CEO decided to invite the staff in such an order that the described situation will not happen for any pair of employees invited one after another. However, there are a lot of employees in the company, and the CEO doesn't have a lot of time. Therefore, the task has been assigned to you. Given the debt relationships between all the employees, determine in which order they should be invited to the office of the R1 company CEO, or determine that the described order does not exist. Input The first line contains space-separated integers n and m <image> β€” the number of employees in R1 and the number of debt relations. Each of the following m lines contains two space-separated integers ai, bi (1 ≀ ai, bi ≀ n; ai β‰  bi), these integers indicate that the person number ai owes money to a person a number bi. Assume that all the employees are numbered from 1 to n. It is guaranteed that each pair of people p, q is mentioned in the input data at most once. In particular, the input data will not contain pairs p, q and q, p simultaneously. Output Print -1 if the described order does not exist. Otherwise, print the permutation of n distinct integers. The first number should denote the number of the person who goes to the CEO office first, the second number denote the person who goes second and so on. If there are multiple correct orders, you are allowed to print any of them. Examples Input 2 1 1 2 Output 2 1 Input 3 3 1 2 2 3 3 1 Output 2 1 3 Submitted Solution: ``` n, p = map(int, input().split()) arr = [i + 1 for i in range(n)] operations = [] for i in range(p): t, y = map(int, input().split()) operations.append([t, y]) for elem in operations: a, b = elem[0], elem[1] if abs(arr.index(a) - arr.index(b)) > 1: continue else: k, l = arr.index(a), arr.index(b) if k == l - 1: arr[k] = b arr[l] = a can = 1 for elem in operations: a, b = elem[0], elem[1] if abs(arr.index(a) - arr.index(b)) > 1: continue else: k, l = arr.index(a), arr.index(b) if k == l - 1: can = 0 break if (can == 1): print(' '.join(map(str, arr))) else: print(-1) ```
instruction
0
37,176
14
74,352
No
output
1
37,176
14
74,353
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The employees of the R1 company often spend time together: they watch football, they go camping, they solve contests. So, it's no big deal that sometimes someone pays for someone else. Today is the day of giving out money rewards. The R1 company CEO will invite employees into his office one by one, rewarding each one for the hard work this month. The CEO knows who owes money to whom. And he also understands that if he invites person x to his office for a reward, and then immediately invite person y, who has lent some money to person x, then they can meet. Of course, in such a situation, the joy of person x from his brand new money reward will be much less. Therefore, the R1 CEO decided to invite the staff in such an order that the described situation will not happen for any pair of employees invited one after another. However, there are a lot of employees in the company, and the CEO doesn't have a lot of time. Therefore, the task has been assigned to you. Given the debt relationships between all the employees, determine in which order they should be invited to the office of the R1 company CEO, or determine that the described order does not exist. Input The first line contains space-separated integers n and m <image> β€” the number of employees in R1 and the number of debt relations. Each of the following m lines contains two space-separated integers ai, bi (1 ≀ ai, bi ≀ n; ai β‰  bi), these integers indicate that the person number ai owes money to a person a number bi. Assume that all the employees are numbered from 1 to n. It is guaranteed that each pair of people p, q is mentioned in the input data at most once. In particular, the input data will not contain pairs p, q and q, p simultaneously. Output Print -1 if the described order does not exist. Otherwise, print the permutation of n distinct integers. The first number should denote the number of the person who goes to the CEO office first, the second number denote the person who goes second and so on. If there are multiple correct orders, you are allowed to print any of them. Examples Input 2 1 1 2 Output 2 1 Input 3 3 1 2 2 3 3 1 Output 2 1 3 Submitted Solution: ``` def s(a): return a[0], a[1] n, m = list(map(int, input().split())) res = [i + 1 for i in range(n)] no = sorted([list(map(int, input().split())) for i in range(m)], key = s) for i in range(m): for j in range(n - 1): if res[i:i + 2] == no[i]: res[i], res[i + 1] = res[i + 1], res[i] for i in range(n - 1): if res[i:i + 2] in no: print(-1) break else: print(" ".join(map(str, res))) ```
instruction
0
37,177
14
74,354
No
output
1
37,177
14
74,355
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The employees of the R1 company often spend time together: they watch football, they go camping, they solve contests. So, it's no big deal that sometimes someone pays for someone else. Today is the day of giving out money rewards. The R1 company CEO will invite employees into his office one by one, rewarding each one for the hard work this month. The CEO knows who owes money to whom. And he also understands that if he invites person x to his office for a reward, and then immediately invite person y, who has lent some money to person x, then they can meet. Of course, in such a situation, the joy of person x from his brand new money reward will be much less. Therefore, the R1 CEO decided to invite the staff in such an order that the described situation will not happen for any pair of employees invited one after another. However, there are a lot of employees in the company, and the CEO doesn't have a lot of time. Therefore, the task has been assigned to you. Given the debt relationships between all the employees, determine in which order they should be invited to the office of the R1 company CEO, or determine that the described order does not exist. Input The first line contains space-separated integers n and m <image> β€” the number of employees in R1 and the number of debt relations. Each of the following m lines contains two space-separated integers ai, bi (1 ≀ ai, bi ≀ n; ai β‰  bi), these integers indicate that the person number ai owes money to a person a number bi. Assume that all the employees are numbered from 1 to n. It is guaranteed that each pair of people p, q is mentioned in the input data at most once. In particular, the input data will not contain pairs p, q and q, p simultaneously. Output Print -1 if the described order does not exist. Otherwise, print the permutation of n distinct integers. The first number should denote the number of the person who goes to the CEO office first, the second number denote the person who goes second and so on. If there are multiple correct orders, you are allowed to print any of them. Examples Input 2 1 1 2 Output 2 1 Input 3 3 1 2 2 3 3 1 Output 2 1 3 Submitted Solution: ``` from sys import setrecursionlimit setrecursionlimit(100500) def dfs(u): print(u + 1, end = ' ') visited[u] = True for x in edges[u]: if not visited[x]: dfs(x) n, m = map(int, input().split()) visited, drains, edges = [False] * n, [0] * n, [[] for x in range(n)] for x in range(m): a, b = map(int, input().split()) edges[b - 1].append(a - 1) drains[a - 1] += 1 if max(drains) > 2: print(-1) exit(0) for x in sorted(range(n), key = lambda a: drains[a]): if not visited[x]: dfs(x) ```
instruction
0
37,178
14
74,356
No
output
1
37,178
14
74,357
Provide tags and a correct Python 3 solution for this coding contest problem. Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow. He knows that the receptionist starts working after ts minutes have passed after midnight and closes after tf minutes have passed after midnight (so that (tf - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves). Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately. <image> "Reception 1" For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them. Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him! Input The first line contains three integers: the point of time when the receptionist begins to work ts, the point of time when the receptionist stops working tf and the time the receptionist spends on each visitor t. The second line contains one integer n β€” the amount of visitors (0 ≀ n ≀ 100 000). The third line contains positive integers in non-decreasing order β€” the points of time when the visitors arrive to the passport office. All times are set in minutes and do not exceed 1012; it is guaranteed that ts < tf. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist. Output Print single non-negative integer β€” the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them. Examples Input 10 15 2 2 10 13 Output 12 Input 8 17 3 4 3 4 5 8 Output 2 Note In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight. In the second example, Vasya has to come before anyone else to be served.
instruction
0
37,343
14
74,686
Tags: brute force, greedy Correct Solution: ``` INF = 10 ** 19 ts, tf, t = map(int, input().split()) n = int(input()) u = [] if n > 0: u = list(map(int, input().split())) a = [] b = [] i = 0 while i < n: a.append(u[i]) if i == 0: start = max(ts, u[i]) else: start = max(b[-1], a[-1]) ng = 1 while i + 1 < n and u[i] == u[i+1]: i += 1 ng += 1 end = start + ng * t b.append(end) i += 1 res = None if n == 0: res = ts else: res = a[0] - 1 min_w = max(0, ts - a[0] + 1) m = len(a) for j in range(1, m): start = a[j] - 1 stop = max(b[j-1], start) w = stop - start if w < min_w: min_w = w if stop < tf: res = start for j in range(1, m): start = a[j] stop = b[j] w = stop - start if w < min_w: min_w = w if stop < tf: res = start if b[m - 1] <= tf - t: res = tf - t if ts < a[0]: res = ts print(res) ```
output
1
37,343
14
74,687
Provide tags and a correct Python 3 solution for this coding contest problem. Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow. He knows that the receptionist starts working after ts minutes have passed after midnight and closes after tf minutes have passed after midnight (so that (tf - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves). Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately. <image> "Reception 1" For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them. Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him! Input The first line contains three integers: the point of time when the receptionist begins to work ts, the point of time when the receptionist stops working tf and the time the receptionist spends on each visitor t. The second line contains one integer n β€” the amount of visitors (0 ≀ n ≀ 100 000). The third line contains positive integers in non-decreasing order β€” the points of time when the visitors arrive to the passport office. All times are set in minutes and do not exceed 1012; it is guaranteed that ts < tf. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist. Output Print single non-negative integer β€” the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them. Examples Input 10 15 2 2 10 13 Output 12 Input 8 17 3 4 3 4 5 8 Output 2 Note In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight. In the second example, Vasya has to come before anyone else to be served.
instruction
0
37,344
14
74,688
Tags: brute force, greedy Correct Solution: ``` f = lambda: map(int, input().split()) a, b, t = f() k, = f() s = d = 1e12 for q in (f() if k else []): if q > a: s = a if q > a or a + t > b: break elif a - q < d: s, d = q - 1, a - q a += t print(s if a + t > b else a) ```
output
1
37,344
14
74,689
Provide tags and a correct Python 3 solution for this coding contest problem. Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow. He knows that the receptionist starts working after ts minutes have passed after midnight and closes after tf minutes have passed after midnight (so that (tf - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves). Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately. <image> "Reception 1" For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them. Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him! Input The first line contains three integers: the point of time when the receptionist begins to work ts, the point of time when the receptionist stops working tf and the time the receptionist spends on each visitor t. The second line contains one integer n β€” the amount of visitors (0 ≀ n ≀ 100 000). The third line contains positive integers in non-decreasing order β€” the points of time when the visitors arrive to the passport office. All times are set in minutes and do not exceed 1012; it is guaranteed that ts < tf. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist. Output Print single non-negative integer β€” the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them. Examples Input 10 15 2 2 10 13 Output 12 Input 8 17 3 4 3 4 5 8 Output 2 Note In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight. In the second example, Vasya has to come before anyone else to be served.
instruction
0
37,345
14
74,690
Tags: brute force, greedy Correct Solution: ``` ts, tf, te = map(int, input().split(' ')) n = int(input()) if n == 0: print(ts) exit(0) arr = list(map(int, input().split(' '))) idx = 0 curr_time = ts wait_time = max(0, ts-arr[0]+1) ans = min(arr[0]-1, ts) while wait_time > 0: curr_time+=te idx += 1 if idx == n or curr_time >= tf: break next_wait = max(0, curr_time-arr[idx]+1) if next_wait < wait_time: wait_time = next_wait ans = arr[idx]-1 if wait_time > 0 and curr_time+te <= tf: ans = curr_time print(ans) #answer is guaranteed ```
output
1
37,345
14
74,691
Provide tags and a correct Python 3 solution for this coding contest problem. Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow. He knows that the receptionist starts working after ts minutes have passed after midnight and closes after tf minutes have passed after midnight (so that (tf - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves). Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately. <image> "Reception 1" For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them. Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him! Input The first line contains three integers: the point of time when the receptionist begins to work ts, the point of time when the receptionist stops working tf and the time the receptionist spends on each visitor t. The second line contains one integer n β€” the amount of visitors (0 ≀ n ≀ 100 000). The third line contains positive integers in non-decreasing order β€” the points of time when the visitors arrive to the passport office. All times are set in minutes and do not exceed 1012; it is guaranteed that ts < tf. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist. Output Print single non-negative integer β€” the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them. Examples Input 10 15 2 2 10 13 Output 12 Input 8 17 3 4 3 4 5 8 Output 2 Note In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight. In the second example, Vasya has to come before anyone else to be served.
instruction
0
37,346
14
74,692
Tags: brute force, greedy Correct Solution: ``` import math start,finish,time = map(int, input().split()) b = int(input()) def solve(): firstAvailableSpot = start minimumTimeInQueue = 10**12 + 1 minimum = 23123 last = 0 for i in range(len(q)): if (q[i] > firstAvailableSpot) and (firstAvailableSpot + time < finish): return firstAvailableSpot timeInQueue = firstAvailableSpot - q[i] firstAvailableSpot += time if q[i] != last: if timeInQueue + 1 < minimumTimeInQueue: minimumTimeInQueue = timeInQueue + 1 minimum = q[i] - 1 last = q[i] if firstAvailableSpot >= finish or len(q) -1 == i: if firstAvailableSpot + time <= finish: return firstAvailableSpot return minimum if b != 0: q = list(map(int, input().split())) print(solve()) else: print(str(start)) ```
output
1
37,346
14
74,693
Provide tags and a correct Python 3 solution for this coding contest problem. Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow. He knows that the receptionist starts working after ts minutes have passed after midnight and closes after tf minutes have passed after midnight (so that (tf - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves). Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately. <image> "Reception 1" For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them. Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him! Input The first line contains three integers: the point of time when the receptionist begins to work ts, the point of time when the receptionist stops working tf and the time the receptionist spends on each visitor t. The second line contains one integer n β€” the amount of visitors (0 ≀ n ≀ 100 000). The third line contains positive integers in non-decreasing order β€” the points of time when the visitors arrive to the passport office. All times are set in minutes and do not exceed 1012; it is guaranteed that ts < tf. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist. Output Print single non-negative integer β€” the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them. Examples Input 10 15 2 2 10 13 Output 12 Input 8 17 3 4 3 4 5 8 Output 2 Note In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight. In the second example, Vasya has to come before anyone else to be served.
instruction
0
37,347
14
74,694
Tags: brute force, greedy Correct Solution: ``` import sys t1, t2, t = map(int, input().split()) u = 1 n = int(input()) if n == 0: print(t1) sys.exit() a = [int(i) for i in input().split()] mini = 10 ** 20 ans = 0 time = t1 for x in a: if x <= t2 - t: if time - x + 1 < mini and x > 0: mini = time - x + 1 if x - 1 > time: ans = time else: ans = x - 1 time = t + max(time, x) if time > t2 - t: print(ans) else: print(time) ```
output
1
37,347
14
74,695
Provide tags and a correct Python 3 solution for this coding contest problem. Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow. He knows that the receptionist starts working after ts minutes have passed after midnight and closes after tf minutes have passed after midnight (so that (tf - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves). Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately. <image> "Reception 1" For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them. Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him! Input The first line contains three integers: the point of time when the receptionist begins to work ts, the point of time when the receptionist stops working tf and the time the receptionist spends on each visitor t. The second line contains one integer n β€” the amount of visitors (0 ≀ n ≀ 100 000). The third line contains positive integers in non-decreasing order β€” the points of time when the visitors arrive to the passport office. All times are set in minutes and do not exceed 1012; it is guaranteed that ts < tf. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist. Output Print single non-negative integer β€” the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them. Examples Input 10 15 2 2 10 13 Output 12 Input 8 17 3 4 3 4 5 8 Output 2 Note In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight. In the second example, Vasya has to come before anyone else to be served.
instruction
0
37,348
14
74,696
Tags: brute force, greedy Correct Solution: ``` #!/usr/bin/python3.5 a,b,c=[int(x) for x in input().split()] n=int(input()) if n==0: print(a) quit() mas=[int(x) for x in input().split()] m=1e14 p=0 for i in mas: if i<=b-c: if i and a-i+1<m: m=a-i+1; p=min(i-1,a) a=max(a,i)+c if a<=b-c: p=a print(p) # Made By Mostafa_Khaled ```
output
1
37,348
14
74,697
Provide tags and a correct Python 3 solution for this coding contest problem. Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow. He knows that the receptionist starts working after ts minutes have passed after midnight and closes after tf minutes have passed after midnight (so that (tf - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves). Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately. <image> "Reception 1" For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them. Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him! Input The first line contains three integers: the point of time when the receptionist begins to work ts, the point of time when the receptionist stops working tf and the time the receptionist spends on each visitor t. The second line contains one integer n β€” the amount of visitors (0 ≀ n ≀ 100 000). The third line contains positive integers in non-decreasing order β€” the points of time when the visitors arrive to the passport office. All times are set in minutes and do not exceed 1012; it is guaranteed that ts < tf. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist. Output Print single non-negative integer β€” the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them. Examples Input 10 15 2 2 10 13 Output 12 Input 8 17 3 4 3 4 5 8 Output 2 Note In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight. In the second example, Vasya has to come before anyone else to be served.
instruction
0
37,349
14
74,698
Tags: brute force, greedy Correct Solution: ``` from math import floor s, f, k = input().split() s = int(s) f = int(f) k = int(k) n = int(input()) w = [] t = [] if (n > 0): a = list(map(int, input().split())) te = s diff = f-s m = floor(diff/k) if n < m: m = n if a[0] != 0: w.append(a[0]-1) t.append(s) p = 0 for i in range(m): #if (te < a[i]): # w.append(a[i]-1) if (a[i] - a[p] > k): w.append(a[i]-1) t.append(te-(a[i]-1)) p = i te += k if te <= f-k: w.append(te) t.append(0) min = 0 for ww in range(len(w)): if t[ww] < t[min]: min = ww print(w[min]) else: print(s) ```
output
1
37,349
14
74,699
Provide tags and a correct Python 3 solution for this coding contest problem. Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow. He knows that the receptionist starts working after ts minutes have passed after midnight and closes after tf minutes have passed after midnight (so that (tf - 1) is the last minute when the receptionist is still working). The receptionist spends exactly t minutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves). Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately. <image> "Reception 1" For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them. Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him! Input The first line contains three integers: the point of time when the receptionist begins to work ts, the point of time when the receptionist stops working tf and the time the receptionist spends on each visitor t. The second line contains one integer n β€” the amount of visitors (0 ≀ n ≀ 100 000). The third line contains positive integers in non-decreasing order β€” the points of time when the visitors arrive to the passport office. All times are set in minutes and do not exceed 1012; it is guaranteed that ts < tf. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist. Output Print single non-negative integer β€” the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them. Examples Input 10 15 2 2 10 13 Output 12 Input 8 17 3 4 3 4 5 8 Output 2 Note In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight. In the second example, Vasya has to come before anyone else to be served.
instruction
0
37,350
14
74,700
Tags: brute force, greedy Correct Solution: ``` import bisect import sys ts, tf, T = [int(x) for x in input().split()] N = int(input()) if N == 0: print(ts) sys.exit() Ts = [int(x) for x in input().strip().split()] # f = open('test.B.in.3', 'r') # ts, tf, T = [int(x) for x in f.readline().split()] # N = int(f.readline()) # Ts = [int(x) for x in f.readline().strip().split()] check_times = sorted(set([x - 1 for x in Ts + [Ts[-1] + 2] if x - 1 >= 0] + [ts])) best_waiting_time, best_time = float('inf'), -1 last_waiting = None all_times = [] for t in check_times: idx = bisect.bisect_right(Ts, t) if t < ts: # Number of people who have come before will be served first wt_time = (ts - t) + idx * T else: if last_waiting is not None: last_idx = last_waiting['idx'] last_time = last_waiting['time'] last_waiting_time = last_waiting['wt_time'] wt_time = max(last_waiting_time + (idx - last_idx) * T - (t - last_time), 0) else: wt_time = idx * T if wt_time < best_waiting_time and t + wt_time <= tf - T: best_waiting_time = wt_time best_time = t last_waiting = { 'idx': idx, 'time': t, 'wt_time': wt_time } all_times.append(last_waiting) tail_end = last_waiting['time'] + last_waiting['wt_time'] if tail_end <= tf - T and best_waiting_time > 0: best_waiting_time = 0 best_time = tail_end # print(sorted(waiting_times, key=lambda x: x['wt_time'])[0]) print(best_time) ```
output
1
37,350
14
74,701